Flutter Engine
The Flutter Engine
GrD3DRenderTarget.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google LLC
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#include "src/gpu/KeyBuilder.h"
18
19// We're virtually derived from GrSurface (via GrRenderTarget) so its
20// constructor must be explicitly called.
22 SkISize dimensions,
25 const GrD3DTextureResourceInfo& msaaInfo,
27 const GrD3DDescriptorHeap::CPUHandle& colorRenderTargetView,
28 const GrD3DDescriptorHeap::CPUHandle& resolveRenderTargetView,
29 Wrapped,
30 std::string_view label)
31 : GrSurface(gpu, dimensions, info.fProtected, label)
33 // for the moment we only support 1:1 color to stencil
34 , GrRenderTarget(gpu, dimensions, msaaInfo.fSampleCount, info.fProtected, label)
35 , fMSAATextureResource(new GrD3DTextureResource(msaaInfo, std::move(msaaState)))
36 , fColorRenderTargetView(colorRenderTargetView)
37 , fResolveRenderTargetView(resolveRenderTargetView) {
38 SkASSERT(info.fProtected == msaaInfo.fProtected);
39 SkASSERT(msaaInfo.fSampleCount > 1);
40 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
41}
42
43// We're virtually derived from GrSurface (via GrRenderTarget) so its
44// constructor must be explicitly called.
46 SkISize dimensions,
49 const GrD3DTextureResourceInfo& msaaInfo,
51 const GrD3DDescriptorHeap::CPUHandle& colorRenderTargetView,
52 const GrD3DDescriptorHeap::CPUHandle& resolveRenderTargetView,
53 std::string_view label)
54 : GrSurface(gpu, dimensions, info.fProtected, label)
56 // for the moment we only support 1:1 color to stencil
57 , GrRenderTarget(gpu, dimensions, msaaInfo.fSampleCount, info.fProtected, label)
58 , fMSAATextureResource(new GrD3DTextureResource(msaaInfo, std::move(msaaState)))
59 , fColorRenderTargetView(colorRenderTargetView)
60 , fResolveRenderTargetView(resolveRenderTargetView) {
61 SkASSERT(info.fProtected == msaaInfo.fProtected);
62 SkASSERT(msaaInfo.fSampleCount > 1);
63}
64
65// We're virtually derived from GrSurface (via GrRenderTarget) so its
66// constructor must be explicitly called.
68 SkISize dimensions,
71 const GrD3DDescriptorHeap::CPUHandle& renderTargetView,
72 Wrapped,
73 std::string_view label)
74 : GrSurface(gpu, dimensions, info.fProtected, label)
76 , GrRenderTarget(gpu, dimensions, info.fSampleCount, info.fProtected, label)
77 , fMSAATextureResource(nullptr)
78 , fColorRenderTargetView(renderTargetView) {
80}
81
82// We're virtually derived from GrSurface (via GrRenderTarget) so its
83// constructor must be explicitly called.
85 SkISize dimensions,
88 const GrD3DDescriptorHeap::CPUHandle& renderTargetView,
89 std::string_view label)
90 : GrSurface(gpu, dimensions, info.fProtected, label)
92 , GrRenderTarget(gpu, dimensions, info.fSampleCount, info.fProtected, label)
93 , fMSAATextureResource(nullptr)
94 , fColorRenderTargetView(renderTargetView) {}
95
97 GrD3DGpu* gpu, SkISize dimensions, int sampleCnt, const GrD3DTextureResourceInfo& info,
99 SkASSERT(info.fResource.get());
100 SkASSERT(info.fLevelCount == 1);
101 SkASSERT(sampleCnt >= 1 && info.fSampleCount >= 1);
102
103 int wrappedTextureSampleCnt = static_cast<int>(info.fSampleCount);
104 if (sampleCnt != wrappedTextureSampleCnt && wrappedTextureSampleCnt != 1) {
105 return nullptr;
106 }
107
108 GrD3DDescriptorHeap::CPUHandle renderTargetView =
109 gpu->resourceProvider().createRenderTargetView(info.fResource.get());
110
111 // create msaa surface if necessary
112 GrD3DRenderTarget* d3dRT;
113 if (sampleCnt != wrappedTextureSampleCnt) {
116 // for wrapped MSAA surface we assume clear to white
117 SkColor4f clearColor = { 1, 1, 1, 1 };
118 std::tie(msInfo, msState) =
119 GrD3DTextureResource::CreateMSAA(gpu, dimensions, sampleCnt, info, clearColor);
120
121 GrD3DDescriptorHeap::CPUHandle msaaRenderTargetView =
123
124 d3dRT = new GrD3DRenderTarget(gpu, dimensions, info, std::move(state), msInfo,
125 std::move(msState), msaaRenderTargetView, renderTargetView,
126 kWrapped,
127 /*label=*/"MakeWrappedRenderTargetWithMSAASurface");
128 } else {
129 d3dRT = new GrD3DRenderTarget(
130 gpu, dimensions, info, std::move(state), renderTargetView, kWrapped,
131 /*label=*/"MakeWrappedRenderTarget");
132 }
133
134 return sk_sp<GrD3DRenderTarget>(d3dRT);
135}
136
138 // either release or abandon should have been called by the owner of this object.
139 SkASSERT(!fMSAATextureResource);
140}
141
143 if (this->numSamples() == 1) {
144 SkASSERT(!fMSAATextureResource);
145 return nullptr;
146 }
147 if (fMSAATextureResource) {
148 return fMSAATextureResource.get();
149 }
150 SkASSERT(!fMSAATextureResource);
151 return this;
152}
153
155 auto* constThis = const_cast<const GrD3DRenderTarget*>(this);
156 return const_cast<GrD3DTextureResource*>(constThis->msaaTextureResource());
157}
158
159void GrD3DRenderTarget::releaseInternalObjects() {
160 GrD3DGpu* gpu = this->getD3DGpu();
161
162 if (fMSAATextureResource) {
163 fMSAATextureResource->releaseResource(gpu);
164 fMSAATextureResource.reset();
165 gpu->resourceProvider().recycleRenderTargetView(fResolveRenderTargetView);
166 }
167
168 gpu->resourceProvider().recycleRenderTargetView(fColorRenderTargetView);
169}
170
172 this->releaseInternalObjects();
173 this->releaseResource(this->getD3DGpu());
175}
176
178 this->releaseInternalObjects();
179 this->releaseResource(this->getD3DGpu());
181}
182
184 return GrBackendRenderTarget(this->width(), this->height(), fInfo, this->grD3DResourceState());
185}
186
187GrD3DGpu* GrD3DRenderTarget::getD3DGpu() const {
188 SkASSERT(!this->wasDestroyed());
189 return static_cast<GrD3DGpu*>(this->getGpu());
190}
191
193 if (auto stencil = this->getStencilAttachment()) {
194 auto d3dStencil = static_cast<GrD3DAttachment*>(stencil);
195 return d3dStencil->dxgiFormat();
196 }
197 return DXGI_FORMAT_UNKNOWN;
198}
199
201 b->add32(this->dxgiFormat());
202 b->add32(this->numSamples());
203 b->add32(this->stencilDxgiFormat());
204#ifdef SK_DEBUG
205 if (const GrAttachment* stencil = this->getStencilAttachment()) {
206 SkASSERT(stencil->numSamples() == this->numSamples());
207 }
208#endif
209 b->add32(this->sampleQualityPattern());
210}
211
213 SkASSERT(this->d3dResource());
214 if (!this->getLabel().empty()) {
215 if (fMSAATextureResource) {
216 SkASSERT(fMSAATextureResource->d3dResource());
217 const std::wstring suffix = GrD3DMultiByteToWide(this->getLabel());
218 const std::wstring msaaLabel = L"_Skia_MSAA_" + suffix;
219 fMSAATextureResource->d3dResource()->SetName(msaaLabel.c_str());
220 const std::wstring resolveLabel = L"_Skia_Resolve_" + suffix;
221 this->d3dResource()->SetName(resolveLabel.c_str());
222 } else {
223 const std::wstring label = L"_Skia_" + GrD3DMultiByteToWide(this->getLabel());
224 this->d3dResource()->SetName(label.c_str());
225 }
226 }
227}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
std::wstring GrD3DMultiByteToWide(const std::string &str)
Definition: GrD3DUtil.cpp:25
#define SkASSERT(cond)
Definition: SkAssert.h:116
GrD3DResourceProvider & resourceProvider()
Definition: GrD3DGpu.h:38
void genKey(skgpu::KeyBuilder *b) const
DXGI_FORMAT stencilDxgiFormat() const
void onRelease() override
static sk_sp< GrD3DRenderTarget > MakeWrappedRenderTarget(GrD3DGpu *, SkISize, int sampleCnt, const GrD3DTextureResourceInfo &, sk_sp< GrD3DResourceState >)
const GrD3DTextureResource * msaaTextureResource() const
GrBackendRenderTarget getBackendRenderTarget() const override
void onAbandon() override
void onSetLabel() override
GrD3DDescriptorHeap::CPUHandle createRenderTargetView(ID3D12Resource *textureResource)
void recycleRenderTargetView(const GrD3DDescriptorHeap::CPUHandle &)
DXGI_FORMAT dxgiFormat() const
GrD3DTextureResourceInfo fInfo
ID3D12Resource * d3dResource() const
void releaseResource(GrD3DGpu *gpu)
sk_sp< GrD3DResourceState > grD3DResourceState() const
static std::pair< GrD3DTextureResourceInfo, sk_sp< GrD3DResourceState > > CreateMSAA(GrD3DGpu *gpu, SkISize dimensions, int sampleCnt, const GrD3DTextureResourceInfo &info, SkColor4f clearColor)
unsigned int sampleQualityPattern() const
std::string getLabel() const
GrGpu * getGpu() const
bool wasDestroyed() const
void registerWithCacheWrapped(GrWrapCacheable)
GrAttachment * getStencilAttachment() const
void onAbandon() override
void onRelease() override
int numSamples() const
SkISize dimensions() const
Definition: GrSurface.h:27
int height() const
Definition: GrSurface.h:37
int width() const
Definition: GrSurface.h:32
T * get() const
Definition: GrD3DTypes.h:108
void add32(uint32_t v, std::string_view label="unknown")
Definition: KeyBuilder.h:58
static bool b
EMSCRIPTEN_KEEPALIVE void empty()
AtkStateType state
Definition: ref_ptr.h:256
gr_cp< ID3D12Resource > fResource
Definition: GrD3DTypes.h:180
skgpu::Protected fProtected
Definition: GrD3DTypes.h:187
Definition: SkSize.h:16