Flutter Impeller
allocator_mtl_unittests.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/testing/testing.h"
15 
16 #include <QuartzCore/CAMetalLayer.h>
17 #include <memory>
18 #include <thread>
19 
20 #include "gtest/gtest.h"
21 
22 namespace impeller {
23 namespace testing {
24 
27 
28 TEST_P(AllocatorMTLTest, DebugTraceMemoryStatistics) {
29  auto& context_mtl = ContextMTL::Cast(*GetContext());
30  const auto& allocator = context_mtl.GetResourceAllocator();
31 
32  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
33  0u);
34 
35  // Memoryless texture does not increase allocated size.
36  {
37  TextureDescriptor desc;
40  desc.size = {1024, 1024};
41  auto texture_1 = allocator->CreateTexture(desc);
42 
43  // Private storage texture increases allocated size.
45  auto texture_2 = allocator->CreateTexture(desc);
46 
47 #ifdef IMPELLER_DEBUG
48  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
49  4u);
50 #else
51  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
52  0u);
53 #endif // IMPELLER_DEBUG
54 
55  // Host storage texture increases allocated size.
57  auto texture_3 = allocator->CreateTexture(desc);
58 
59 #ifdef IMPELLER_DEBUG
60  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
61  8u);
62 #else
63  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
64  0u);
65 #endif // IMPELLER_DEBUG
66  }
67 
68  // After all textures are out of scope, memory has been decremented.
69  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
70  0u);
71 }
72 
73 TEST_P(AllocatorMTLTest, ManagedMemory) {
74  auto& context_mtl = ContextMTL::Cast(*GetContext());
75  auto allocator = std::make_unique<AllocatorMTL>(context_mtl.GetMTLDevice(),
76  "test-allocator");
77  allocator->DebugSetSupportsUMA(false);
78 
80  desc.size = 100;
82 
83  auto buffer = allocator->CreateBuffer(desc);
84  ASSERT_TRUE(buffer);
85 
86  EXPECT_NE(buffer->OnGetContents(), nullptr);
87 }
88 
89 } // namespace testing
90 } // namespace impeller
Represents the size of an allocation in different units.
constexpr double GetSize() const
static ContextMTL & Cast(Context &base)
Definition: backend_cast.h:13
TEST_P(AiksTest, DrawAtlasNoColor)
INSTANTIATE_METAL_PLAYGROUND_SUITE(AllocatorMTLTest)
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...