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