Flutter Engine
The Flutter Engine
GrD3DTypes.h
Go to the documentation of this file.
1
2/*
3 * Copyright 2020 Google LLC
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef GrD3DTypes_DEFINED
10#define GrD3DTypes_DEFINED
11
12// This file includes d3d12.h, which in turn includes windows.h, which redefines many
13// common identifiers such as:
14// * interface
15// * small
16// * near
17// * far
18// * CreateSemaphore
19// * MemoryBarrier
20//
21// You should only include this header if you need the Direct3D definitions and are
22// prepared to rename those identifiers.
23
26#include <d3d12.h>
27#include <dxgi1_4.h>
28
29class GrD3DGpu;
30
31 /** Check if the argument is non-null, and if so, call obj->AddRef() and return obj.
32 */
33template <typename T> static inline T* GrSafeComAddRef(T* obj) {
34 if (obj) {
35 obj->AddRef();
36 }
37 return obj;
38}
39
40/** Check if the argument is non-null, and if so, call obj->Release()
41 */
42template <typename T> static inline void GrSafeComRelease(T* obj) {
43 if (obj) {
44 obj->Release();
45 }
46}
47
48template <typename T> class gr_cp {
49public:
50 using element_type = T;
51
52 constexpr gr_cp() : fObject(nullptr) {}
53 constexpr gr_cp(std::nullptr_t) : fObject(nullptr) {}
54
55 /**
56 * Shares the underlying object by calling AddRef(), so that both the argument and the newly
57 * created gr_cp both have a reference to it.
58 */
59 gr_cp(const gr_cp<T>& that) : fObject(GrSafeComAddRef(that.get())) {}
60
61 /**
62 * Move the underlying object from the argument to the newly created gr_cp. Afterwards only
63 * the new gr_cp will have a reference to the object, and the argument will point to null.
64 * No call to AddRef() or Release() will be made.
65 */
66 gr_cp(gr_cp<T>&& that) : fObject(that.release()) {}
67
68 /**
69 * Adopt the bare object into the newly created gr_cp.
70 * No call to AddRef() or Release() will be made.
71 */
72 explicit gr_cp(T* obj) {
73 fObject = obj;
74 }
75
76 /**
77 * Calls Release() on the underlying object pointer.
78 */
80 GrSafeComRelease(fObject);
81 SkDEBUGCODE(fObject = nullptr);
82 }
83
84 /**
85 * Shares the underlying object referenced by the argument by calling AddRef() on it. If this
86 * gr_cp previously had a reference to an object (i.e. not null) it will call Release()
87 * on that object.
88 */
89 gr_cp<T>& operator=(const gr_cp<T>& that) {
90 if (this != &that) {
91 this->reset(GrSafeComAddRef(that.get()));
92 }
93 return *this;
94 }
95
96 /**
97 * Move the underlying object from the argument to the gr_cp. If the gr_cp
98 * previously held a reference to another object, Release() will be called on that object.
99 * No call to AddRef() will be made.
100 */
102 this->reset(that.release());
103 return *this;
104 }
105
106 explicit operator bool() const { return this->get() != nullptr; }
107
108 T* get() const { return fObject; }
109 T* operator->() const { return fObject; }
110 T** operator&() { return &fObject; }
111
112 /**
113 * Adopt the new object, and call Release() on any previously held object (if not null).
114 * No call to AddRef() will be made.
115 */
116 void reset(T* object = nullptr) {
117 T* oldObject = fObject;
118 fObject = object;
119 GrSafeComRelease(oldObject);
120 }
121
122 /**
123 * Shares the new object by calling AddRef() on it. If this gr_cp previously had a
124 * reference to an object (i.e. not null) it will call Release() on that object.
125 */
126 void retain(T* object) {
127 if (this->fObject != object) {
128 this->reset(GrSafeComAddRef(object));
129 }
130 }
131
132 /**
133 * Return the original object, and set the internal object to nullptr.
134 * The caller must assume ownership of the object, and manage its reference count directly.
135 * No call to Release() will be made.
136 */
137 [[nodiscard]] T* release() {
138 T* obj = fObject;
139 fObject = nullptr;
140 return obj;
141 }
142
143private:
144 T* fObject;
145};
146
147template <typename T> inline bool operator==(const gr_cp<T>& a,
148 const gr_cp<T>& b) {
149 return a.get() == b.get();
150}
151
152template <typename T> inline bool operator!=(const gr_cp<T>& a,
153 const gr_cp<T>& b) {
154 return a.get() != b.get();
155}
156
157// interface classes for the GPU memory allocator
158class GrD3DAlloc : public SkRefCnt {
159public:
160 ~GrD3DAlloc() override = default;
161};
162
164public:
165 virtual gr_cp<ID3D12Resource> createResource(D3D12_HEAP_TYPE, const D3D12_RESOURCE_DESC*,
166 D3D12_RESOURCE_STATES initialResourceState,
167 sk_sp<GrD3DAlloc>* allocation,
168 const D3D12_CLEAR_VALUE*) = 0;
170 uint64_t localOffset,
171 const D3D12_RESOURCE_DESC*,
172 D3D12_RESOURCE_STATES initialResourceState,
173 const D3D12_CLEAR_VALUE*) = 0;
174};
175
176// Note: there is no notion of Borrowed or Adopted resources in the D3D backend,
177// so Ganesh will ref fResource once it's asked to wrap it.
178// Clients are responsible for releasing their own ref to avoid memory leaks.
182 D3D12_RESOURCE_STATES fResourceState = D3D12_RESOURCE_STATE_COMMON;
183 DXGI_FORMAT fFormat = DXGI_FORMAT_UNKNOWN;
184 uint32_t fSampleCount = 1;
185 uint32_t fLevelCount = 0;
186 unsigned int fSampleQualityPattern = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
188
190
192 const sk_sp<GrD3DAlloc> alloc,
193 D3D12_RESOURCE_STATES resourceState,
194 DXGI_FORMAT format,
195 uint32_t sampleCount,
196 uint32_t levelCount,
197 unsigned int sampleQualityLevel,
200 , fAlloc(alloc)
201 , fResourceState(resourceState)
202 , fFormat(format)
203 , fSampleCount(sampleCount)
204 , fLevelCount(levelCount)
205 , fSampleQualityPattern(sampleQualityLevel)
206 , fProtected(isProtected) {}
207
209 D3D12_RESOURCE_STATES resourceState)
212 , fResourceState(resourceState)
218
219#if defined(GR_TEST_UTILS)
220 bool operator==(const GrD3DTextureResourceInfo& that) const {
221 return fResource == that.fResource && fResourceState == that.fResourceState &&
222 fFormat == that.fFormat && fSampleCount == that.fSampleCount &&
223 fLevelCount == that.fLevelCount &&
225 }
226#endif
227};
228
231 : fFence(nullptr)
232 , fValue(0) {
233 }
234
236 uint64_t fValue; // signal value for the fence
237};
238
240 uint32_t fSampleCount = 1;
241 uint32_t fLevelCount = 0;
243
244 DXGI_FORMAT fFormat = DXGI_FORMAT_UNKNOWN;
245 unsigned int fSampleQualityPattern = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
246};
247
248#endif
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
bool operator==(const gr_cp< T > &a, const gr_cp< T > &b)
Definition: GrD3DTypes.h:147
static void GrSafeComRelease(T *obj)
Definition: GrD3DTypes.h:42
bool operator!=(const gr_cp< T > &a, const gr_cp< T > &b)
Definition: GrD3DTypes.h:152
static T * GrSafeComAddRef(T *obj)
Definition: GrD3DTypes.h:33
static SkString resource(SkPDFResourceType type, int index)
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
~GrD3DAlloc() override=default
virtual gr_cp< ID3D12Resource > createResource(D3D12_HEAP_TYPE, const D3D12_RESOURCE_DESC *, D3D12_RESOURCE_STATES initialResourceState, sk_sp< GrD3DAlloc > *allocation, const D3D12_CLEAR_VALUE *)=0
virtual gr_cp< ID3D12Resource > createAliasingResource(sk_sp< GrD3DAlloc > &allocation, uint64_t localOffset, const D3D12_RESOURCE_DESC *, D3D12_RESOURCE_STATES initialResourceState, const D3D12_CLEAR_VALUE *)=0
gr_cp(const gr_cp< T > &that)
Definition: GrD3DTypes.h:59
gr_cp(gr_cp< T > &&that)
Definition: GrD3DTypes.h:66
T * release()
Definition: GrD3DTypes.h:137
void retain(T *object)
Definition: GrD3DTypes.h:126
T * get() const
Definition: GrD3DTypes.h:108
T ** operator&()
Definition: GrD3DTypes.h:110
constexpr gr_cp(std::nullptr_t)
Definition: GrD3DTypes.h:53
gr_cp(T *obj)
Definition: GrD3DTypes.h:72
T element_type
Definition: GrD3DTypes.h:50
gr_cp< T > & operator=(const gr_cp< T > &that)
Definition: GrD3DTypes.h:89
void reset(T *object=nullptr)
Definition: GrD3DTypes.h:116
constexpr gr_cp()
Definition: GrD3DTypes.h:52
~gr_cp()
Definition: GrD3DTypes.h:79
gr_cp< T > & operator=(gr_cp< T > &&that)
Definition: GrD3DTypes.h:101
T * operator->() const
Definition: GrD3DTypes.h:109
static bool b
struct MyStruct a[10]
uint32_t uint32_t * format
Protected
Definition: GpuTypes.h:61
#define T
Definition: precompiler.cc:65
gr_cp< ID3D12Fence > fFence
Definition: GrD3DTypes.h:235
uint64_t fValue
Definition: GrD3DTypes.h:236
skgpu::Protected fProtected
Definition: GrD3DTypes.h:242
uint32_t fLevelCount
Definition: GrD3DTypes.h:241
DXGI_FORMAT fFormat
Definition: GrD3DTypes.h:244
unsigned int fSampleQualityPattern
Definition: GrD3DTypes.h:245
uint32_t fSampleCount
Definition: GrD3DTypes.h:240
unsigned int fSampleQualityPattern
Definition: GrD3DTypes.h:186
D3D12_RESOURCE_STATES fResourceState
Definition: GrD3DTypes.h:182
gr_cp< ID3D12Resource > fResource
Definition: GrD3DTypes.h:180
skgpu::Protected fProtected
Definition: GrD3DTypes.h:187
GrD3DTextureResourceInfo(ID3D12Resource *resource, const sk_sp< GrD3DAlloc > alloc, D3D12_RESOURCE_STATES resourceState, DXGI_FORMAT format, uint32_t sampleCount, uint32_t levelCount, unsigned int sampleQualityLevel, skgpu::Protected isProtected=skgpu::Protected::kNo)
Definition: GrD3DTypes.h:191
GrD3DTextureResourceInfo()=default
GrD3DTextureResourceInfo(const GrD3DTextureResourceInfo &info, D3D12_RESOURCE_STATES resourceState)
Definition: GrD3DTypes.h:208
sk_sp< GrD3DAlloc > fAlloc
Definition: GrD3DTypes.h:181