Flutter Engine
The Flutter Engine
GrMockGpu.cpp
Go to the documentation of this file.
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 */
8
16#include "src/gpu/ganesh/GrThreadSafePipelineBuilder.h" // IWYU pragma: keep
22
23#include <atomic>
24
25using namespace skia_private;
26
27int GrMockGpu::NextInternalTextureID() {
28 static std::atomic<int> nextID{1};
29 int id;
30 do {
31 id = nextID.fetch_add(1, std::memory_order_relaxed);
32 } while (0 == id); // Reserve 0 for an invalid ID.
33 return id;
34}
35
36int GrMockGpu::NextExternalTextureID() {
37 // We use negative ints for the "testing only external textures" so they can easily be
38 // identified when debugging.
39 static std::atomic<int> nextID{-1};
40 return nextID.fetch_add(-1, std::memory_order_relaxed);
41}
42
43int GrMockGpu::NextInternalRenderTargetID() {
44 // We start off with large numbers to differentiate from texture IDs, even though they're
45 // technically in a different space.
46 static std::atomic<int> nextID{SK_MaxS32};
47 return nextID.fetch_add(-1, std::memory_order_relaxed);
48}
49
50int GrMockGpu::NextExternalRenderTargetID() {
51 // We use large negative ints for the "testing only external render targets" so they can easily
52 // be identified when debugging.
53 static std::atomic<int> nextID{SK_MinS32};
54 return nextID.fetch_add(1, std::memory_order_relaxed);
55}
56
57std::unique_ptr<GrGpu> GrMockGpu::Make(const GrMockOptions* mockOptions,
58 const GrContextOptions& contextOptions,
59 GrDirectContext* direct) {
60 static const GrMockOptions kDefaultOptions = GrMockOptions();
61 if (!mockOptions) {
62 mockOptions = &kDefaultOptions;
63 }
64 return std::unique_ptr<GrGpu>(new GrMockGpu(direct, *mockOptions, contextOptions));
65}
66
67GrOpsRenderPass* GrMockGpu::onGetOpsRenderPass(GrRenderTarget* rt,
68 bool /*useMSAASurface*/,
70 GrSurfaceOrigin origin,
71 const SkIRect& bounds,
72 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
74 const TArray<GrSurfaceProxy*,true>& sampledProxies,
75 GrXferBarrierFlags renderPassXferBarriers) {
76 return new GrMockOpsRenderPass(this, rt, origin, colorInfo);
77}
78
80 for (int i = 0; i < static_cast<GrMockOpsRenderPass*>(renderPass)->numDraws(); ++i) {
82 }
83 delete renderPass;
84}
85
86GrMockGpu::GrMockGpu(GrDirectContext* direct, const GrMockOptions& options,
87 const GrContextOptions& contextOptions)
88 : INHERITED(direct)
89 , fMockOptions(options) {
90 this->initCaps(sk_make_sp<GrMockCaps>(contextOptions, options));
91}
92
94
96 return nullptr;
97}
98
100 return nullptr;
101}
102
103sk_sp<GrTexture> GrMockGpu::onCreateTexture(SkISize dimensions,
104 const GrBackendFormat& format,
105 GrRenderable renderable,
106 int renderTargetSampleCnt,
107 skgpu::Budgeted budgeted,
108 GrProtected isProtected,
109 int mipLevelCount,
110 uint32_t levelClearMask,
111 std::string_view label) {
112 if (fMockOptions.fFailTextureAllocations) {
113 return nullptr;
114 }
115
116 // Compressed formats should go through onCreateCompressedTexture
117 SkASSERT(format.asMockCompressionType() == SkTextureCompressionType::kNone);
118
119 GrColorType ct = format.asMockColorType();
121
122 GrMipmapStatus mipmapStatus =
124 GrMockTextureInfo texInfo(ct, SkTextureCompressionType::kNone, NextInternalTextureID(),
125 isProtected);
126 if (renderable == GrRenderable::kYes) {
127 GrMockRenderTargetInfo rtInfo(ct, NextInternalRenderTargetID(), isProtected);
128 return sk_sp<GrTexture>(new GrMockTextureRenderTarget(this, budgeted, dimensions,
129 renderTargetSampleCnt,
130 mipmapStatus,
131 texInfo,
132 rtInfo,
133 label));
134 }
136 this, budgeted, dimensions, mipmapStatus, texInfo, label));
137}
138
139// TODO: why no 'isProtected' ?!
140sk_sp<GrTexture> GrMockGpu::onCreateCompressedTexture(SkISize dimensions,
141 const GrBackendFormat& format,
142 skgpu::Budgeted budgeted,
143 skgpu::Mipmapped mipmapped,
144 GrProtected isProtected,
145 const void* data,
146 size_t dataSize) {
147 if (fMockOptions.fFailTextureAllocations) {
148 return nullptr;
149 }
150
151#ifdef SK_DEBUG
152 // Uncompressed formats should go through onCreateTexture
153 SkTextureCompressionType compression = format.asMockCompressionType();
155#endif
156
157 GrMipmapStatus mipmapStatus = (mipmapped == skgpu::Mipmapped::kYes)
161 format.asMockCompressionType(),
162 NextInternalTextureID(),
163 isProtected);
164
166 this, budgeted, dimensions, mipmapStatus, texInfo,
167 /*label=*/"MockGpu_CreateCompressedTexture"));
168}
169
170sk_sp<GrTexture> GrMockGpu::onWrapBackendTexture(const GrBackendTexture& tex,
171 GrWrapOwnership ownership,
172 GrWrapCacheable wrapType,
173 GrIOType ioType) {
174 GrMockTextureInfo texInfo;
175 SkAssertResult(tex.getMockTextureInfo(&texInfo));
176
177 SkTextureCompressionType compression = texInfo.compressionType();
178 if (compression != SkTextureCompressionType::kNone) {
179 return nullptr;
180 }
181
184 return sk_sp<GrTexture>(new GrMockTexture(this,
185 tex.dimensions(),
186 mipmapStatus,
187 texInfo,
188 wrapType,
189 ioType,
190 /*label=*/"MockGpu_WrapBackendTexture"));
191}
192
193sk_sp<GrTexture> GrMockGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
194 GrWrapOwnership ownership,
195 GrWrapCacheable wrapType) {
196 return nullptr;
197}
198
199sk_sp<GrTexture> GrMockGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
200 int sampleCnt,
201 GrWrapOwnership ownership,
202 GrWrapCacheable cacheable) {
203 GrMockTextureInfo texInfo;
204 SkAssertResult(tex.getMockTextureInfo(&texInfo));
206
207 GrMipmapStatus mipmapStatus =
209
210 // The client gave us the texture ID but we supply the render target ID.
211 GrMockRenderTargetInfo rtInfo(texInfo.colorType(), NextInternalRenderTargetID(),
212 texInfo.getProtected());
213
214 return sk_sp<GrTexture>(
216 tex.dimensions(),
217 sampleCnt,
218 mipmapStatus,
219 texInfo,
220 rtInfo,
221 cacheable,
222 /*label=*/"MockGpu_WrapRenderableBackendTexture"));
223}
224
225sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
228
230 new GrMockRenderTarget(this,
232 rt.dimensions(),
233 rt.sampleCnt(),
234 info,
235 /*label=*/"MockGpu_WrapBackendRenderTarget"));
236}
237
238sk_sp<GrGpuBuffer> GrMockGpu::onCreateBuffer(size_t sizeInBytes,
240 GrAccessPattern accessPattern) {
241 return sk_sp<GrGpuBuffer>(
242 new GrMockBuffer(this, sizeInBytes, type, accessPattern,
243 /*label=*/"MockGpu_CreateBuffer"));
244}
245
246sk_sp<GrAttachment> GrMockGpu::makeStencilAttachment(const GrBackendFormat& /*colorFormat*/,
247 SkISize dimensions, int numStencilSamples) {
250 dimensions,
252 numStencilSamples,
253 /*label=*/"MockGpu_MakeStencilAttachment"));
254}
255
256GrBackendTexture GrMockGpu::onCreateBackendTexture(SkISize dimensions,
257 const GrBackendFormat& format,
259 skgpu::Mipmapped mipmapped,
260 GrProtected isProtected,
261 std::string_view label) {
262 SkTextureCompressionType compression = format.asMockCompressionType();
263 if (compression != SkTextureCompressionType::kNone) {
264 return {}; // should go through onCreateCompressedBackendTexture
265 }
266
267 auto colorType = format.asMockColorType();
268 if (!this->caps()->isFormatTexturable(format, GrTextureType::k2D)) {
269 return GrBackendTexture(); // invalid
270 }
271
273 isProtected);
274
275 fOutstandingTestingOnlyTextureIDs.add(info.id());
276 return GrBackendTexture(dimensions.width(), dimensions.height(), mipmapped, info);
277}
278
279GrBackendTexture GrMockGpu::onCreateCompressedBackendTexture(SkISize dimensions,
280 const GrBackendFormat& format,
281 skgpu::Mipmapped mipmapped,
282 GrProtected isProtected) {
283 SkTextureCompressionType compression = format.asMockCompressionType();
284 if (compression == SkTextureCompressionType::kNone) {
285 return {}; // should go through onCreateBackendTexture
286 }
287
288 if (!this->caps()->isFormatTexturable(format, GrTextureType::k2D)) {
289 return {};
290 }
291
292 GrMockTextureInfo info(GrColorType::kUnknown, compression, NextExternalTextureID(),
293 isProtected);
294
295 fOutstandingTestingOnlyTextureIDs.add(info.id());
296 return GrBackendTexture(dimensions.width(), dimensions.height(), mipmapped, info);
297}
298
299void GrMockGpu::deleteBackendTexture(const GrBackendTexture& tex) {
301
303 if (tex.getMockTextureInfo(&info)) {
304 fOutstandingTestingOnlyTextureIDs.remove(info.id());
305 }
306}
307
308#if defined(GR_TEST_UTILS)
309bool GrMockGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
311
313 if (!tex.getMockTextureInfo(&info)) {
314 return false;
315 }
316
317 return fOutstandingTestingOnlyTextureIDs.contains(info.id());
318}
319
320GrBackendRenderTarget GrMockGpu::createTestingOnlyBackendRenderTarget(SkISize dimensions,
322 int sampleCnt,
323 GrProtected isProtected) {
324 GrMockRenderTargetInfo info(colorType, NextExternalRenderTargetID(), isProtected);
325 static constexpr int kStencilBits = 8;
326 return GrBackendRenderTarget(dimensions.width(), dimensions.height(), sampleCnt, kStencilBits,
327 info);
328}
329
330void GrMockGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) {}
331#endif
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
GrWrapCacheable
Definition: GrTypesPriv.h:85
GrIOType
Definition: GrTypesPriv.h:402
GrMipmapStatus
Definition: GrTypesPriv.h:523
GrWrapOwnership
Definition: GrTypesPriv.h:77
GrGpuBufferType
Definition: GrTypesPriv.h:411
GrColorType
Definition: GrTypesPriv.h:540
GrAccessPattern
Definition: GrTypesPriv.h:424
GrSurfaceOrigin
Definition: GrTypes.h:147
GrXferBarrierFlags
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
static constexpr int32_t SK_MinS32
Definition: SkMath.h:22
static constexpr int32_t SK_MaxS32
Definition: SkMath.h:21
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
SkTextureCompressionType
GLenum type
bool getMockRenderTargetInfo(GrMockRenderTargetInfo *) const
SkISize dimensions() const
SkISize dimensions() const
bool hasMipmaps() const
bool getMockTextureInfo(GrMockTextureInfo *) const
GrBackendApi backend() const
void incStencilAttachmentCreates()
Definition: GrGpu.h:539
void incNumDraws()
Definition: GrGpu.h:541
const GrCaps * caps() const
Definition: GrGpu.h:73
void initCaps(sk_sp< const GrCaps > caps)
Definition: GrGpu.cpp:47
Stats fStats
Definition: GrGpu.h:703
sk_sp< GrThreadSafePipelineBuilder > refPipelineBuilder() override
Definition: GrMockGpu.cpp:99
static std::unique_ptr< GrGpu > Make(const GrMockOptions *, const GrContextOptions &, GrDirectContext *)
Definition: GrMockGpu.cpp:57
void submit(GrOpsRenderPass *renderPass) override
Definition: GrMockGpu.cpp:79
GrThreadSafePipelineBuilder * pipelineBuilder() override
Definition: GrMockGpu.cpp:95
~GrMockGpu() override
Definition: GrMockGpu.cpp:93
void add(T item)
Definition: SkTHash.h:592
bool contains(const T &item) const
Definition: SkTHash.h:595
void remove(const T &item)
Definition: SkTHash.h:602
uint32_t uint32_t * format
Optional< SkRect > bounds
Definition: SkRecords.h:189
Budgeted
Definition: GpuTypes.h:35
Renderable
Definition: GpuTypes.h:69
Mipmapped
Definition: GpuTypes.h:53
Protected
Definition: GpuTypes.h:61
bool fFailTextureAllocations
Definition: GrMockTypes.h:163
SkTextureCompressionType compressionType() const
Definition: GrMockTypes.h:49
GrColorType colorType() const
Definition: GrMockTypes.h:51
skgpu::Protected getProtected() const
Definition: GrMockTypes.h:58
Definition: SkRect.h:32
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
const uintptr_t id