Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
MtlBuffer.mm
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
12
13namespace skgpu::graphite {
14
15// TODO: Remove this once we get all graphite labels full plumbed with options to set them on
16// the backend objects
17#ifdef SK_ENABLE_MTL_DEBUG_INFO
19 @"Vertex",
20 @"Index",
21 @"Xfer CPU to GPU",
22 @"Xfer GPU to CPU",
23 @"Uniform",
24 @"Storage",
25 @"Indirect",
26 @"VertexStorage",
27 @"IndexStorage",
28};
29#endif
30
32 size_t size,
34 AccessPattern accessPattern,
35 std::string_view label) {
36 if (size <= 0) {
37 return nullptr;
38 }
39
40 NSUInteger options = 0;
41 if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) {
42 if (accessPattern == AccessPattern::kHostVisible) {
43#ifdef SK_BUILD_FOR_MAC
44 const MtlCaps& mtlCaps = sharedContext->mtlCaps();
45 if (mtlCaps.isMac()) {
46 options |= MTLResourceStorageModeManaged;
47 } else {
48 SkASSERT(mtlCaps.isApple());
49 options |= MTLResourceStorageModeShared;
50 }
51#else
52 options |= MTLResourceStorageModeShared;
53#endif
54 } else {
55 options |= MTLResourceStorageModePrivate;
56 }
57 }
58
59 sk_cfp<id<MTLBuffer>> buffer([sharedContext->device() newBufferWithLength:size
61#ifdef SK_ENABLE_MTL_DEBUG_INFO
62 (*buffer).label = kBufferTypeNames[(int)type];
63#endif
64
66 size,
67 std::move(buffer),
68 std::move(label)));
69}
70
71MtlBuffer::MtlBuffer(const MtlSharedContext* sharedContext,
72 size_t size,
73 sk_cfp<id<MTLBuffer>> buffer,
74 std::string_view label)
75 : Buffer(sharedContext, size, std::move(label))
76 , fBuffer(std::move(buffer)) {}
77
79 SkASSERT(fBuffer);
80 SkASSERT(!this->isMapped());
81
82 if ((*fBuffer).storageMode == MTLStorageModePrivate) {
83 return;
84 }
85
86 fMapPtr = static_cast<char*>((*fBuffer).contents);
87}
88
90 SkASSERT(fBuffer);
91 SkASSERT(this->isMapped());
92#ifdef SK_BUILD_FOR_MAC
93 if ((*fBuffer).storageMode == MTLStorageModeManaged) {
94 [*fBuffer didModifyRange: NSMakeRange(0, this->size())];
95 }
96#endif
97 fMapPtr = nullptr;
98}
99
101 fBuffer.reset();
102}
103
104} // namespace skgpu::graphite
const char * options
#define SkASSERT(cond)
Definition SkAssert.h:116
Type::kYUV Type::kRGBA() int(0.7 *637)
size_t size() const
Definition Buffer.h:19
bool isMapped() const
Definition Buffer.h:30
static sk_sp< Buffer > Make(const MtlSharedContext *, size_t size, BufferType type, AccessPattern, std::string_view label)
Definition MtlBuffer.mm:31
void onUnmap() override
Definition MtlBuffer.mm:89
void freeGpuData() override
Definition MtlBuffer.mm:100
void onMap() override
Definition MtlBuffer.mm:78
bool isApple() const
Definition MtlCaps.h:60
bool isMac() const
Definition MtlCaps.h:59
const SharedContext * sharedContext() const
Definition Resource.h:187
static const uint8_t buffer[]
static const char * kBufferTypeNames[kBufferTypeCount]
static const int kBufferTypeCount
Definition ref_ptr.h:256