| 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"
6 #include "impeller/core/device_buffer_descriptor.h"
7 #include "impeller/core/formats.h"
8 #include "impeller/core/texture_descriptor.h"
9 #include "impeller/playground/playground_test.h"
10 #include "impeller/renderer/backend/metal/allocator_mtl.h"
11 #include "impeller/renderer/backend/metal/context_mtl.h"
12 #include "impeller/renderer/backend/metal/formats_mtl.h"
13 #include "impeller/renderer/backend/metal/lazy_drawable_holder.h"
14 #include "impeller/renderer/backend/metal/texture_mtl.h"
15 #include "impeller/renderer/capabilities.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
26 using AllocatorMTLTest = PlaygroundTest;
27 INSTANTIATE_METAL_PLAYGROUND_SUITE(AllocatorMTLTest);
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;
39 desc.format = PixelFormat::kR8G8B8A8UNormInt;
40 desc.storage_mode = StorageMode::kDeviceTransient;
41 desc.size = {1024, 1024};
42 auto texture_1 = allocator->CreateTexture(desc);
43
44 // Private storage texture increases allocated size.
45 desc.storage_mode = StorageMode::kDevicePrivate;
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.
57 desc.storage_mode = StorageMode::kHostVisible;
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
80 DeviceBufferDescriptor desc;
81 desc.size = 100;
82 desc.storage_mode = StorageMode::kHostVisible;
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
allocator_mtl.h
capabilities.h
impeller::AllocationSize
Represents the size of an allocation in different units.
Definition: allocation_size.h:29
impeller::AllocationSize::GetSize
constexpr double GetSize() const
Definition: allocation_size.h:76
impeller::BackendCast< ContextMTL, Context >::Cast
static ContextMTL & Cast(Context &base)
Definition: backend_cast.h:13
impeller::PlaygroundTest
Definition: playground_test.h:22
formats.h
device_buffer_descriptor.h
formats_mtl.h
lazy_drawable_holder.h
impeller::testing::TEST_P
TEST_P(AiksTest, DrawAtlasNoColor)
Definition: aiks_dl_atlas_unittests.cc:60
impeller::testing::INSTANTIATE_METAL_PLAYGROUND_SUITE
INSTANTIATE_METAL_PLAYGROUND_SUITE(AllocatorMTLTest)
impeller
Definition: allocation.cc:12
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
playground_test.h
context_mtl.h
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:14
impeller::DeviceBufferDescriptor::storage_mode
StorageMode storage_mode
Definition: device_buffer_descriptor.h:15
impeller::DeviceBufferDescriptor::size
size_t size
Definition: device_buffer_descriptor.h:16
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:42
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:39
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:41
texture_descriptor.h
texture_mtl.h