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"
16 
17 #include <QuartzCore/CAMetalLayer.h>
18 #include <memory>
19 #include <thread>
20 
21 #include "gtest/gtest.h"
22 
23 namespace impeller {
24 namespace testing {
25 
28 
29 TEST_P(AllocatorMTLTest, DebugTraceMemoryStatistics) {
30  auto& context_mtl = ContextMTL::Cast(*GetContext());
31  const auto& allocator = context_mtl.GetResourceAllocator();
32 
33  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
34  0u);
35 
36  // Memoryless texture does not increase allocated size.
37  {
38  TextureDescriptor desc;
41  desc.size = {1024, 1024};
42  auto texture_1 = allocator->CreateTexture(desc);
43 
44  // Private storage texture increases allocated size.
46  auto texture_2 = allocator->CreateTexture(desc);
47 
48 #ifdef IMPELLER_DEBUG
49  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
50  4u);
51 #else
52  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
53  0u);
54 #endif // IMPELLER_DEBUG
55 
56  // Host storage texture increases allocated size.
58  auto texture_3 = allocator->CreateTexture(desc);
59 
60 #ifdef IMPELLER_DEBUG
61  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
62  8u);
63 #else
64  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
65  0u);
66 #endif // IMPELLER_DEBUG
67  }
68 
69  // After all textures are out of scope, memory has been decremented.
70  EXPECT_EQ(allocator->DebugGetHeapUsage().ConvertTo<MebiBytes>().GetSize(),
71  0u);
72 }
73 
74 TEST_P(AllocatorMTLTest, ManagedMemory) {
75  auto& context_mtl = ContextMTL::Cast(*GetContext());
76  auto allocator = std::make_unique<AllocatorMTL>(context_mtl.GetMTLDevice(),
77  "test-allocator");
78  allocator->DebugSetSupportsUMA(false);
79 
81  desc.size = 100;
83 
84  auto buffer = allocator->CreateBuffer(desc);
85  ASSERT_TRUE(buffer);
86 
87  EXPECT_NE(buffer->OnGetContents(), nullptr);
88 }
89 
90 } // namespace testing
91 } // 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...