Flutter Engine
The Flutter Engine
GrResourceProvider.h
Go to the documentation of this file.
1/*
2 * Copyright 2015 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
8#ifndef GrResourceProvider_DEFINED
9#define GrResourceProvider_DEFINED
10
22
23#include <cstddef>
24#include <cstdint>
25#include <memory>
26#include <string_view>
27#include <type_traits>
28
29class GrAttachment;
30class GrBackendFormat;
34class GrGpu;
35class GrRenderTarget;
37class GrSemaphore;
38class GrTexture;
39class SkData;
40
41enum class SkBackingFit;
42struct GrVkDrawableInfo;
43struct SkImageInfo;
44struct SkISize;
45
46namespace skgpu {
47class ScratchKey;
48class SingleOwner;
49class UniqueKey;
50enum class Budgeted : bool;
51enum class Mipmapped : bool;
52enum class Protected : bool;
53enum class Renderable : bool;
54}
55
56/**
57 * A factory for arbitrary resource types.
58 */
60public:
62
63 /**
64 * Finds a resource in the cache, based on the specified key. Prior to calling this, the caller
65 * must be sure that if a resource of exists in the cache with the given unique key then it is
66 * of type T.
67 */
68 template <typename T = GrGpuResource>
71 return sk_sp<T>(static_cast<T*>(this->findResourceByUniqueKey(key).release()));
72 }
73
74 ///////////////////////////////////////////////////////////////////////////
75 // Textures
76
77 /**
78 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
79 * and height as desc specifies. If renderable is kYes then the GrTexture will also be a
80 * GrRenderTarget. The texture's format and sample count will always match the request.
81 * The contents of the texture are undefined.
82 */
85 GrTextureType textureType,
86 skgpu::Renderable renderable,
87 int renderTargetSampleCnt,
88 skgpu::Protected isProtected,
89 std::string_view label);
90
91 /** Create an exact fit texture with no initial data to upload. */
94 GrTextureType textureType,
95 skgpu::Renderable renderable,
96 int renderTargetSampleCnt,
97 skgpu::Mipmapped mipmapped,
98 skgpu::Budgeted budgeted,
99 skgpu::Protected isProtected,
100 std::string_view label);
101
102 /**
103 * Create an exact fit texture with initial data to upload. The color type must be valid
104 * for the format and also describe the texel data. This will ensure any conversions that
105 * need to get applied to the data before upload are applied.
106 */
108 const GrBackendFormat& format,
109 GrTextureType textureType,
111 skgpu::Renderable renderable,
112 int renderTargetSampleCnt,
113 skgpu::Budgeted budgeted,
114 skgpu::Mipmapped mipmapped,
115 skgpu::Protected isProtected,
116 const GrMipLevel texels[],
117 std::string_view label);
118
119 /**
120 * Create a potentially loose fit texture with the provided data. The color type must be valid
121 * for the format and also describe the texel data. This will ensure any conversions that
122 * need to get applied to the data before upload are applied.
123 */
125 const GrBackendFormat&,
126 GrTextureType textureType,
127 GrColorType srcColorType,
129 int renderTargetSampleCnt,
133 const GrMipLevel& mipLevel,
134 std::string_view label);
135
136 /**
137 * Search the cache for a scratch texture matching the provided arguments. Failing that
138 * it returns null. If non-null, the resulting texture is always budgeted.
139 */
140 sk_sp<GrTexture> findAndRefScratchTexture(const skgpu::ScratchKey&, std::string_view label);
142 const GrBackendFormat&,
143 GrTextureType textureType,
145 int renderTargetSampleCnt,
148 std::string_view label);
149
150 /**
151 * Creates a compressed texture. The GrGpu must support the SkImageImage::Compression type.
152 * It will not be renderable.
153 */
155 const GrBackendFormat&,
159 SkData* data,
160 std::string_view label);
161
162 ///////////////////////////////////////////////////////////////////////////
163 // Wrapped Backend Surfaces
164
165 /**
166 * Wraps an existing texture with a GrTexture object.
167 *
168 * GrIOType must either be kRead or kRW. kRead blocks any operations that would modify the
169 * pixels (e.g. dst for a copy, regenerating MIP levels, write pixels).
170 *
171 * OpenGL: if the object is a texture Gr may change its GL texture params
172 * when it is drawn.
173 *
174 * @return GrTexture object or NULL on failure.
175 */
179 GrIOType);
180
184
185 /**
186 * This makes the backend texture be renderable. If sampleCnt is > 1 and the underlying API
187 * uses separate MSAA render buffers then a MSAA render buffer is created that resolves
188 * to the texture.
189 */
191 int sampleCnt,
194
195 /**
196 * Wraps an existing render target with a GrRenderTarget object. It is
197 * similar to wrapBackendTexture but can be used to draw into surfaces
198 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
199 * the client will resolve to a texture). Currently wrapped render targets
200 * always use the kBorrow_GrWrapOwnership and GrWrapCacheable::kNo semantics.
201 *
202 * @return GrRenderTarget object or NULL on failure.
203 */
205
207 const GrVkDrawableInfo&);
208
209 /**
210 * Either finds and refs a buffer with the given unique key, or creates a new new, fills its
211 * contents with the InitializeBufferDataFn() callback, and assigns it the unique key.
212 *
213 * @param intendedType hint to the graphics subsystem about how the buffer will be used.
214 * @param size minimum size of buffer to return.
215 * @param key Key to be assigned to the buffer.
216 * @param InitializeBufferFn callback with which to initialize the buffer.
217 *
218 * @return The buffer if successful, otherwise nullptr.
219 */
220 using InitializeBufferFn = void(*)(skgpu::VertexWriter, size_t bufferSize);
222 size_t size,
223 const skgpu::UniqueKey& key,
225
226 /**
227 * Either finds and refs, or creates a static buffer with the given parameters and contents.
228 *
229 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
230 * @param size minimum size of buffer to return.
231 * @param data optional data with which to initialize the buffer.
232 * @param key Key to be assigned to the buffer.
233 *
234 * @return The buffer if successful, otherwise nullptr.
235 */
237 size_t size,
238 const void* staticData,
239 const skgpu::UniqueKey& key);
240
241 /**
242 * Either finds and refs, or creates an index buffer with a repeating pattern for drawing
243 * contiguous vertices of a repeated mesh. If the return is non-null, the caller owns a ref on
244 * the returned GrBuffer.
245 *
246 * @param pattern the pattern of indices to repeat
247 * @param patternSize size in bytes of the pattern
248 * @param reps number of times to repeat the pattern
249 * @param vertCount number of vertices the pattern references
250 * @param key Key to be assigned to the index buffer.
251 *
252 * @return The index buffer if successful, otherwise nullptr.
253 */
255 int patternSize,
256 int reps,
257 int vertCount,
258 const skgpu::UniqueKey& key) {
259 if (auto buffer = this->findByUniqueKey<const GrGpuBuffer>(key)) {
260 return buffer;
261 }
262 return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, &key);
263 }
264
265 /**
266 * Returns an index buffer that can be used to render non-antialiased quads.
267 * Each quad consumes 6 indices (0, 1, 2, 2, 1, 3) and 4 vertices.
268 * Call MaxNumNonAAQuads to get the max allowed number of non-AA quads.
269 * Draw with GrPrimitiveType::kTriangles
270 * @ return the non-AA quad index buffer
271 */
273 if (!fNonAAQuadIndexBuffer) {
274 fNonAAQuadIndexBuffer = this->createNonAAQuadIndexBuffer();
275 }
276 return fNonAAQuadIndexBuffer;
277 }
278
279 static int MaxNumNonAAQuads();
280 static int NumVertsPerNonAAQuad();
281 static int NumIndicesPerNonAAQuad();
282
283 /**
284 * Returns an index buffer that can be used to render antialiased quads.
285 * Each quad consumes 30 indices and 8 vertices.
286 * Call MaxNumAAQuads to get the max allowed number of AA quads.
287 * Draw with GrPrimitiveType::kTriangles
288 * @ return the AA quad index buffer
289 */
291 if (!fAAQuadIndexBuffer) {
292 fAAQuadIndexBuffer = this->createAAQuadIndexBuffer();
293 }
294 return fAAQuadIndexBuffer;
295 }
296
297 static int MaxNumAAQuads();
298 static int NumVertsPerAAQuad();
299 static int NumIndicesPerAAQuad();
300
301 enum class ZeroInit : bool { kNo = false, kYes = true };
302
303 /**
304 * Returns a buffer.
305 *
306 * @param size minimum size of buffer to return.
307 * @param GrGpuBufferType hint to the graphics subsystem about what the buffer will be used for.
308 * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed.
309 * @param ZeroInit if kYes zero-initialize the buffer. Otherwise, contents are undefined.
310 *
311 * @return the buffer if successful, otherwise nullptr.
312 */
314
315 /** Same as above but also fills the buffer from data. */
317 size_t size,
319 GrAccessPattern pattern);
320
321 /**
322 * If passed in render target already has a stencil buffer on the specified surface, return
323 * true. Otherwise attempt to attach one and return true on success.
324 */
325 bool attachStencilAttachment(GrRenderTarget* rt, bool useMSAASurface);
326
328 const GrBackendFormat& format,
329 int sampleCnt,
330 skgpu::Protected isProtected,
331 GrMemoryless isMemoryless);
332
333 /**
334 * Gets a GrAttachment that can be used for MSAA rendering. This attachment may be shared by
335 * other users. Thus any renderpass that uses the attachment should not assume any specific
336 * data at the start and should not try to save written data at the end. Ideally the render pass
337 * should discard the data at the end.
338 */
340 const GrBackendFormat& format,
341 int sampleCnt,
342 skgpu::Protected isProtected,
343 GrMemoryless memoryless);
344
345 /**
346 * Assigns a unique key to a resource. If the key is associated with another resource that
347 * association is removed and replaced by this resource.
348 */
350
351 [[nodiscard]] std::unique_ptr<GrSemaphore> makeSemaphore(bool isOwned = true);
352
353 std::unique_ptr<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
356
357 void abandon() {
358 fCache = nullptr;
359 fGpu = nullptr;
360 }
361
362 uint32_t contextUniqueID() const { return fCache->contextUniqueID(); }
363 const GrCaps* caps() const { return fCaps.get(); }
364 bool overBudget() const { return fCache->overBudget(); }
365
367 inline const GrResourceProviderPriv priv() const; // NOLINT(readability-const-return-type)
368
369private:
370 sk_sp<GrGpuResource> findResourceByUniqueKey(const skgpu::UniqueKey&);
371
372 /*
373 * Try to find an existing scratch texture that exactly matches 'desc'. If successful
374 * update the budgeting accordingly.
375 */
376 sk_sp<GrTexture> getExactScratch(SkISize dimensions,
377 const GrBackendFormat&,
380 int renderTargetSampleCnt,
384 std::string_view label);
385
386 // Attempts to find a resource in the cache that exactly matches the SkISize. Failing that
387 // it returns null. If non-null, the resulting msaa attachment is always budgeted.
388 sk_sp<GrAttachment> refScratchMSAAAttachment(SkISize dimensions,
389 const GrBackendFormat&,
390 int sampleCnt,
392 GrMemoryless memoryless,
393 std::string_view label);
394
395 // Used to perform any conversions necessary to texel data before creating a texture with
396 // existing data or uploading to a scratch texture.
399 GrColorType prepareLevels(const GrBackendFormat& format,
401 SkISize baseSize,
402 const GrMipLevel texels[],
403 int mipLevelCount,
404 TempLevels*,
405 TempLevelDatas*) const;
406
407 // GrResourceProvider may be asked to "create" a new texture with initial pixel data to populate
408 // it. In implementation it may pull an existing texture from GrResourceCache and then write the
409 // pixel data to the texture. It takes a width/height for the base level because we may be
410 // using an approximate-sized scratch texture. On success the texture is returned and nullptr
411 // on failure.
414 SkISize baseSize,
415 const GrMipLevel texels[],
416 int mipLevelCount) const;
417
418 GrResourceCache* cache() { return fCache; }
419 const GrResourceCache* cache() const { return fCache; }
420
422
423 // Method made available via GrResourceProviderPriv
424 GrGpu* gpu() { return fGpu; }
425 const GrGpu* gpu() const { return fGpu; }
426
427 bool isAbandoned() const {
428 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
429 return !SkToBool(fCache);
430 }
431
432 sk_sp<const GrGpuBuffer> createPatternedIndexBuffer(const uint16_t* pattern,
433 int patternSize,
434 int reps,
435 int vertCount,
436 const skgpu::UniqueKey* key);
437
438 sk_sp<const GrGpuBuffer> createNonAAQuadIndexBuffer();
439 sk_sp<const GrGpuBuffer> createAAQuadIndexBuffer();
440
441 GrResourceCache* fCache;
442 GrGpu* fGpu;
444 sk_sp<const GrGpuBuffer> fNonAAQuadIndexBuffer;
445 sk_sp<const GrGpuBuffer> fAAQuadIndexBuffer;
446
447 // In debug builds we guard against improper thread handling
448 SkDEBUGCODE(mutable skgpu::SingleOwner* fSingleOwner;)
449};
450
451#endif
GrWrapCacheable
Definition: GrTypesPriv.h:85
GrIOType
Definition: GrTypesPriv.h:402
GrWrapOwnership
Definition: GrTypesPriv.h:77
@ kBorrow_GrWrapOwnership
Definition: GrTypesPriv.h:79
GrGpuBufferType
Definition: GrTypesPriv.h:411
GrMemoryless
Definition: GrTypesPriv.h:123
GrSemaphoreWrapType
Definition: GrTypesPriv.h:146
GrTextureType
Definition: GrTypesPriv.h:268
GrColorType
Definition: GrTypesPriv.h:540
GrAccessPattern
Definition: GrTypesPriv.h:424
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkBackingFit
Definition: SkBackingFit.h:16
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
@ kYes
Do pre-clip the geometry before applying the (perspective) matrix.
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
GLenum type
Definition: GrCaps.h:57
Definition: GrGpu.h:62
bool overBudget() const
uint32_t contextUniqueID() const
bool attachStencilAttachment(GrRenderTarget *rt, bool useMSAASurface)
static int NumIndicesPerNonAAQuad()
sk_sp< GrRenderTarget > wrapBackendRenderTarget(const GrBackendRenderTarget &)
uint32_t contextUniqueID() const
sk_sp< const GrGpuBuffer > findOrCreatePatternedIndexBuffer(const uint16_t *pattern, int patternSize, int reps, int vertCount, const skgpu::UniqueKey &key)
sk_sp< GrAttachment > getDiscardableMSAAAttachment(SkISize dimensions, const GrBackendFormat &format, int sampleCnt, skgpu::Protected isProtected, GrMemoryless memoryless)
GrResourceProvider(GrGpu *, GrResourceCache *, skgpu::SingleOwner *)
std::enable_if< std::is_base_of< GrGpuResource, T >::value, sk_sp< T > >::type findByUniqueKey(const skgpu::UniqueKey &key)
sk_sp< GrTexture > createApproxTexture(SkISize dimensions, const GrBackendFormat &format, GrTextureType textureType, skgpu::Renderable renderable, int renderTargetSampleCnt, skgpu::Protected isProtected, std::string_view label)
void assignUniqueKeyToResource(const skgpu::UniqueKey &, GrGpuResource *)
std::unique_ptr< GrSemaphore > makeSemaphore(bool isOwned=true)
void(*)(skgpu::VertexWriter, size_t bufferSize) InitializeBufferFn
std::unique_ptr< GrSemaphore > wrapBackendSemaphore(const GrBackendSemaphore &, GrSemaphoreWrapType, GrWrapOwnership=kBorrow_GrWrapOwnership)
sk_sp< GrTexture > wrapBackendTexture(const GrBackendTexture &tex, GrWrapOwnership, GrWrapCacheable, GrIOType)
static int NumIndicesPerAAQuad()
sk_sp< GrGpuBuffer > createBuffer(size_t size, GrGpuBufferType, GrAccessPattern, ZeroInit)
sk_sp< GrTexture > findAndRefScratchTexture(const skgpu::ScratchKey &, std::string_view label)
static int NumVertsPerAAQuad()
static int NumVertsPerNonAAQuad()
GrResourceProviderPriv priv()
sk_sp< const GrGpuBuffer > refNonAAQuadIndexBuffer()
sk_sp< const GrGpuBuffer > refAAQuadIndexBuffer()
sk_sp< GrTexture > createTexture(SkISize dimensions, const GrBackendFormat &format, GrTextureType textureType, skgpu::Renderable renderable, int renderTargetSampleCnt, skgpu::Mipmapped mipmapped, skgpu::Budgeted budgeted, skgpu::Protected isProtected, std::string_view label)
sk_sp< const GrGpuBuffer > findOrMakeStaticBuffer(GrGpuBufferType intendedType, size_t size, const skgpu::UniqueKey &key, InitializeBufferFn)
sk_sp< GrRenderTarget > wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo &, const GrVkDrawableInfo &)
sk_sp< GrTexture > createCompressedTexture(SkISize dimensions, const GrBackendFormat &, skgpu::Budgeted, skgpu::Mipmapped, skgpu::Protected, SkData *data, std::string_view label)
sk_sp< GrAttachment > makeMSAAAttachment(SkISize dimensions, const GrBackendFormat &format, int sampleCnt, skgpu::Protected isProtected, GrMemoryless isMemoryless)
sk_sp< GrTexture > wrapCompressedBackendTexture(const GrBackendTexture &tex, GrWrapOwnership, GrWrapCacheable)
sk_sp< GrTexture > wrapRenderableBackendTexture(const GrBackendTexture &tex, int sampleCnt, GrWrapOwnership, GrWrapCacheable)
const GrCaps * caps() const
Definition: SkData.h:25
T * get() const
Definition: SkRefCnt.h:303
uint8_t value
uint32_t uint32_t * format
FlTexture * texture
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
Definition: GpuTools.h:21
Budgeted
Definition: GpuTypes.h:35
Renderable
Definition: GpuTypes.h:69
Mipmapped
Definition: GpuTypes.h:53
Protected
Definition: GpuTypes.h:61
#define T
Definition: precompiler.cc:65
Definition: SkSize.h:16
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63