Debian Codegraph @

src/gpu/ganesh/mock/GrMockTexture.h

raw
1 /*
2 * Copyright 2017 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 #ifndef GrMockTexture_DEFINED
8 #define GrMockTexture_DEFINED
9
10 #include "include/core/SkSize.h"
11 #include "include/gpu/GpuTypes.h"
12 #include "include/gpu/ganesh/GrBackendSurface.h"
13 #include "include/gpu/ganesh/SkImageGanesh.h"
14 #include "include/gpu/ganesh/mock/GrMockTypes.h"
15 #include "include/private/base/SkAssert.h"
16 #include "include/private/gpu/ganesh/GrTypesPriv.h"
17 #include "src/gpu/ganesh/GrAttachment.h"
18 #include "src/gpu/ganesh/GrBackendUtils.h"
19 #include "src/gpu/ganesh/GrRenderTarget.h"
20 #include "src/gpu/ganesh/GrSurface.h"
21 #include "src/gpu/ganesh/GrTexture.h"
22 #include "src/gpu/ganesh/mock/GrMockGpu.h"
23
24 #include <cstddef>
25 #include <string_view>
26
27 namespace skgpu { class ScratchKey; }
28
29 class GrMockTexture : public GrTexture {
30 public:
31 GrMockTexture(GrMockGpu* gpu,
32 skgpu::Budgeted budgeted,
33 SkISize dimensions,
34 GrMipmapStatus mipmapStatus,
35 const GrMockTextureInfo& info,
36 std::string_view label)
37 : GrMockTexture(gpu, dimensions, mipmapStatus, info, label) {
38 this->registerWithCache(budgeted);
39 }
40
41 GrMockTexture(GrMockGpu* gpu,
42 SkISize dimensions,
43 GrMipmapStatus mipmapStatus,
44 const GrMockTextureInfo& info,
45 GrWrapCacheable cacheable,
46 GrIOType ioType,
47 std::string_view label)
48 : GrMockTexture(gpu, dimensions, mipmapStatus, info, label) {
49 if (ioType == kRead_GrIOType) {
50 this->setReadOnly();
51 }
52 this->registerWithCacheWrapped(cacheable);
53 }
54
55 ~GrMockTexture() override {}
56
57 GrBackendTexture getBackendTexture() const override {
58 return GrBackendTexture(this->width(), this->height(), this->mipmapped(), fInfo);
59 }
60
61 GrBackendFormat backendFormat() const override {
62 return fInfo.getBackendFormat();
63 }
64
65 void textureParamsModified() override {}
66
67 protected:
68 // constructor for subclasses
69 GrMockTexture(GrMockGpu* gpu,
70 const SkISize& dims,
71 GrMipmapStatus mipmapStatus,
72 const GrMockTextureInfo& info,
73 std::string_view label)
74 : GrSurface(gpu, dims, info.getProtected(), label)
75 , GrTexture(gpu, dims, info.getProtected(), GrTextureType::k2D, mipmapStatus, label)
76 , fInfo(info) {}
77
78 void onRelease() override {
79 INHERITED::onRelease();
80 }
81
82 void onAbandon() override {
83 INHERITED::onAbandon();
84 }
85
86 bool onStealBackendTexture(GrBackendTexture*, SkImages::BackendTextureReleaseProc*) override {
87 return false;
88 }
89
90 private:
91 void onSetLabel() override{}
92
93 GrMockTextureInfo fInfo;
94
95 using INHERITED = GrTexture;
96 };
97
98 class GrMockRenderTarget : public GrRenderTarget {
99 public:
100 GrMockRenderTarget(GrMockGpu* gpu,
101 skgpu::Budgeted budgeted,
102 SkISize dimensions,
103 int sampleCnt,
104 const GrMockRenderTargetInfo& info,
105 std::string_view label)
106 : GrSurface(gpu, dimensions, info.getProtected(), label)
107 , GrRenderTarget(gpu, dimensions, sampleCnt, info.getProtected(), label)
108 , fInfo(info) {
109 this->registerWithCache(budgeted);
110 }
111
112 enum Wrapped { kWrapped };
113 GrMockRenderTarget(GrMockGpu* gpu, Wrapped, SkISize dimensions, int sampleCnt,
114 const GrMockRenderTargetInfo& info,
115 std::string_view label)
116 : GrSurface(gpu, dimensions, info.getProtected(), label)
117 , GrRenderTarget(gpu, dimensions, sampleCnt, info.getProtected(), label)
118 , fInfo(info) {
119 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
120 }
121
122 bool canAttemptStencilAttachment(bool useMSAASurface) const override {
123 SkASSERT(useMSAASurface == (this->numSamples() > 1));
124 return true;
125 }
126
127 bool completeStencilAttachment(GrAttachment*, bool useMSAASurface) override {
128 SkASSERT(useMSAASurface == (this->numSamples() > 1));
129 return true;
130 }
131
132 size_t onGpuMemorySize() const override {
133 int numColorSamples = this->numSamples();
134 if (numColorSamples > 1) {
135 // Add one to account for the resolve buffer.
136 ++numColorSamples;
137 }
138 return GrSurface::ComputeSize(
139 this->backendFormat(), this->dimensions(), numColorSamples, skgpu::Mipmapped::kNo);
140 }
141
142 GrBackendRenderTarget getBackendRenderTarget() const override {
143 int numStencilBits = 0;
144 if (GrAttachment* stencil = this->getStencilAttachment()) {
145 numStencilBits = GrBackendFormatStencilBits(stencil->backendFormat());
146 }
147 return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo};
148 }
149
150 GrBackendFormat backendFormat() const override {
151 return fInfo.getBackendFormat();
152 }
153
154 protected:
155 // constructor for subclasses
156 GrMockRenderTarget(GrMockGpu* gpu,
157 SkISize dimensions,
158 int sampleCnt,
159 const GrMockRenderTargetInfo& info,
160 std::string_view label)
161 : GrSurface(gpu, dimensions, info.getProtected(), label)
162 , GrRenderTarget(gpu, dimensions, sampleCnt, info.getProtected(), label)
163 , fInfo(info) {}
164
165 private:
166 void onSetLabel() override{}
167
168 GrMockRenderTargetInfo fInfo;
169
170 using INHERITED = GrRenderTarget;
171 };
172
173 class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
174 public:
175 // Internally created.
176 GrMockTextureRenderTarget(GrMockGpu* gpu,
177 skgpu::Budgeted budgeted,
178 SkISize dimensions,
179 int sampleCnt,
180 GrMipmapStatus mipmapStatus,
181 const GrMockTextureInfo& texInfo,
182 const GrMockRenderTargetInfo& rtInfo,
183 std::string_view label)
184 : GrSurface(gpu, dimensions, texInfo.getProtected(), label)
185 , GrMockTexture(gpu, dimensions, mipmapStatus, texInfo, label)
186 , GrMockRenderTarget(gpu, dimensions, sampleCnt, rtInfo, label) {
187 SkASSERT(texInfo.getProtected() == rtInfo.getProtected());
188 this->registerWithCache(budgeted);
189 }
190
191 // Renderable wrapped backend texture.
192 GrMockTextureRenderTarget(GrMockGpu* gpu,
193 SkISize dimensions,
194 int sampleCnt,
195 GrMipmapStatus mipmapStatus,
196 const GrMockTextureInfo& texInfo,
197 const GrMockRenderTargetInfo& rtInfo,
198 GrWrapCacheable cacheable,
199 std::string_view label)
200 : GrSurface(gpu, dimensions, texInfo.getProtected(), label)
201 , GrMockTexture(gpu, dimensions, mipmapStatus, texInfo, label)
202 , GrMockRenderTarget(gpu, dimensions, sampleCnt, rtInfo, label) {
203 SkASSERT(texInfo.getProtected() == rtInfo.getProtected());
204 this->registerWithCacheWrapped(cacheable);
205 }
206
207 GrTexture* asTexture() override { return this; }
208 GrRenderTarget* asRenderTarget() override { return this; }
209 const GrTexture* asTexture() const override { return this; }
210 const GrRenderTarget* asRenderTarget() const override { return this; }
211
212 GrBackendFormat backendFormat() const override {
213 return GrMockTexture::backendFormat();
214 }
215
216 private:
217 void onAbandon() override {
218 GrRenderTarget::onAbandon();
219 GrMockTexture::onAbandon();
220 }
221
222 void onRelease() override {
223 GrRenderTarget::onRelease();
224 GrMockTexture::onRelease();
225 }
226
227 size_t onGpuMemorySize() const override {
228 int numColorSamples = this->numSamples();
229 if (numColorSamples > 1) {
230 // Add one to account for the resolve buffer.
231 ++numColorSamples;
232 }
233 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
234 numColorSamples, this->mipmapped());
235 }
236
237 void onSetLabel() override{}
238
239 // This avoids an inherits via dominance warning on MSVC.
240 void computeScratchKey(skgpu::ScratchKey* key) const override {
241 GrTexture::computeScratchKey(key);
242 }
243 };
244
245 #endif