Flutter Impeller
allocator_mtl.h
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 #ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_ALLOCATOR_MTL_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_ALLOCATOR_MTL_H_
7 
8 #include <Metal/Metal.h>
9 #include <atomic>
10 
11 #include "impeller/base/thread.h"
13 
14 namespace impeller {
15 
17  public:
19 
21 
22  /// Increment the tracked allocation size in bytes.
23  void Increment(size_t size);
24 
25  /// Decrement the tracked allocation size in bytes.
26  void Decrement(size_t size);
27 
28  /// Get the current tracked allocation size.
30 
31  private:
32  std::atomic<size_t> size_ = 0;
33 };
34 
35 ISize DeviceMaxTextureSizeSupported(id<MTLDevice> device);
36 
37 class AllocatorMTL final : public Allocator {
38  public:
40 
41  // |Allocator|
42  ~AllocatorMTL() override;
43 
44  // |Allocator|
45  Bytes DebugGetHeapUsage() const override;
46 
47  // visible for testing.
48  void DebugSetSupportsUMA(bool value);
49 
50  AllocatorMTL(id<MTLDevice> device, std::string label);
51 
52  private:
53  friend class ContextMTL;
54 
55  id<MTLDevice> device_;
56  std::string allocator_label_;
57  bool supports_memoryless_targets_ = false;
58  bool supports_uma_ = false;
59  bool is_valid_ = false;
60 
61 #ifdef IMPELLER_DEBUG
62  std::shared_ptr<DebugAllocatorStats> debug_allocater_ =
63  std::make_shared<DebugAllocatorStats>();
64 #endif // IMPELLER_DEBUG
65 
66  ISize max_texture_supported_;
67 
68  // |Allocator|
69  bool IsValid() const;
70 
71  // |Allocator|
72  std::shared_ptr<DeviceBuffer> OnCreateBuffer(
73  const DeviceBufferDescriptor& desc) override;
74 
75  // |Allocator|
76  std::shared_ptr<Texture> OnCreateTexture(
77  const TextureDescriptor& desc) override;
78 
79  // |Allocator|
80  uint16_t MinimumBytesPerRow(PixelFormat format) const override;
81 
82  // |Allocator|
83  ISize GetMaxTextureSizeSupported() const override;
84 
85  // |Allocator|
86  void DebugTraceMemoryStatistics() const override;
87 
88  AllocatorMTL(const AllocatorMTL&) = delete;
89 
90  AllocatorMTL& operator=(const AllocatorMTL&) = delete;
91 };
92 
93 } // namespace impeller
94 
95 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_ALLOCATOR_MTL_H_
An object that allocates device memory.
Definition: allocator.h:24
void DebugSetSupportsUMA(bool value)
Bytes DebugGetHeapUsage() const override
Bytes GetAllocationSize()
Get the current tracked allocation size.
void Increment(size_t size)
Increment the tracked allocation size in bytes.
void Decrement(size_t size)
Decrement the tracked allocation size in bytes.
int32_t value
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
ISize DeviceMaxTextureSizeSupported(id< MTLDevice > device)
ISize64 ISize
Definition: size.h:162
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...