blob: 5ecaac3473649ed7db0ce203d7455ba936a8de92 [file] [log] [blame]
bsalomond309e7a2015-04-30 14:18:54 -07001/*
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
cdalton397536c2016-03-25 12:15:03 -070011#include "GrBuffer.h"
Greg Daniel77b53f62016-10-18 11:48:51 -040012#include "GrGpu.h"
bsalomon706f08f2015-05-22 07:35:58 -070013#include "GrPathRange.h"
bsalomond309e7a2015-04-30 14:18:54 -070014
bsalomon706f08f2015-05-22 07:35:58 -070015class GrPath;
egdanielec00d942015-09-14 12:56:10 -070016class GrRenderTarget;
joshualitt6d0872d2016-01-11 08:27:48 -080017class GrSingleOwner;
egdanielec00d942015-09-14 12:56:10 -070018class GrStencilAttachment;
bsalomon6663acf2016-05-10 09:14:17 -070019class GrStyle;
bsalomon706f08f2015-05-22 07:35:58 -070020class SkDescriptor;
21class SkPath;
22class SkTypeface;
bsalomoned0bcad2015-05-04 10:36:42 -070023
bsalomond309e7a2015-04-30 14:18:54 -070024/**
Brian Osman32342f02017-03-04 08:12:46 -050025 * A factory for arbitrary resource types. This class is intended for use within the Gr code base.
bsalomoneae62002015-07-31 13:59:30 -070026 *
Brian Osman32342f02017-03-04 08:12:46 -050027 * Some members force callers to make a flags (pendingIO) decision. This can be relaxed once
28 * https://bug.skia.org/4156 is fixed.
bsalomond309e7a2015-04-30 14:18:54 -070029 */
Brian Osman32342f02017-03-04 08:12:46 -050030class GrResourceProvider {
bsalomond309e7a2015-04-30 14:18:54 -070031public:
joshualitt6d0872d2016-01-11 08:27:48 -080032 GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner);
bsalomoned0bcad2015-05-04 10:36:42 -070033
34 template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) {
35 return static_cast<T*>(this->findAndRefResourceByUniqueKey(key));
36 }
37
Brian Osman32342f02017-03-04 08:12:46 -050038 ///////////////////////////////////////////////////////////////////////////
39 // Textures
40
41 /**
42 * Creates a new texture in the resource cache and returns it. The caller owns a
43 * ref on the returned texture which must be balanced by a call to unref.
44 *
45 * @param desc Description of the texture properties.
46 * @param budgeted Does the texture count against the resource cache budget?
47 * @param texels A contiguous array of mipmap levels
48 * @param mipLevelCount The amount of elements in the texels array
49 */
50 GrTexture* createMipMappedTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
51 const GrMipLevel* texels, int mipLevelCount,
52 uint32_t flags = 0);
53
54 /**
55 * This function is a shim which creates a SkTArray<GrMipLevel> of size 1.
56 * It then calls createTexture with that SkTArray.
57 *
58 * @param srcData Pointer to the pixel values (optional).
59 * @param rowBytes The number of bytes between rows of the texture. Zero
60 * implies tightly packed rows. For compressed pixel configs, this
61 * field is ignored.
62 */
63 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, const void* srcData,
64 size_t rowBytes, uint32_t flags = 0);
65
66 /** Shortcut for creating a texture with no initial data to upload. */
67 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, uint32_t flags = 0) {
68 return this->createTexture(desc, budgeted, nullptr, 0, flags);
69 }
70
71 /** Assigns a unique key to the texture. The texture will be findable via this key using
72 findTextureByUniqueKey(). If an existing texture has this key, it's key will be removed. */
73 void assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) {
74 SkASSERT(key.isValid());
75 this->assignUniqueKeyToResource(key, texture);
76 }
77
78 /** Finds a texture by unique key. If the texture is found it is ref'ed and returned. */
79 GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key);
80
81 /**
82 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
83 * and height as desc specifies. If desc specifies that the texture should be a render target
84 * then result will be a render target. Format and sample count will always match the request.
85 * The contents of the texture are undefined. The caller owns a ref on the returned texture and
86 * must balance with a call to unref.
87 */
88 GrTexture* createApproxTexture(const GrSurfaceDesc&, uint32_t flags);
89
90 ///////////////////////////////////////////////////////////////////////////
91 // Wrapped Backend Surfaces
92
93 /**
94 * Wraps an existing texture with a GrTexture object.
95 *
96 * OpenGL: if the object is a texture Gr may change its GL texture params
97 * when it is drawn.
98 *
99 * @return GrTexture object or NULL on failure.
100 */
101 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTextureDesc& desc,
102 GrWrapOwnership = kBorrow_GrWrapOwnership);
103
104 /**
105 * Wraps an existing render target with a GrRenderTarget object. It is
106 * similar to wrapBackendTexture but can be used to draw into surfaces
107 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
108 * the client will resolve to a texture). Currently wrapped render targets
109 * always use the kBorrow_GrWrapOwnership semantics.
110 *
111 * @return GrRenderTarget object or NULL on failure.
112 */
113 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
114
115 static const int kMinScratchTextureSize;
116
bsalomoned0bcad2015-05-04 10:36:42 -0700117 /**
118 * Either finds and refs, or creates an index buffer for instanced drawing with a specific
119 * pattern if the index buffer is not found. If the return is non-null, the caller owns
cdalton397536c2016-03-25 12:15:03 -0700120 * a ref on the returned GrBuffer.
bsalomoned0bcad2015-05-04 10:36:42 -0700121 *
122 * @param pattern the pattern of indices to repeat
123 * @param patternSize size in bytes of the pattern
124 * @param reps number of times to repeat the pattern
125 * @param vertCount number of vertices the pattern references
126 * @param key Key to be assigned to the index buffer.
127 *
halcanary96fcdcc2015-08-27 07:41:13 -0700128 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -0700129 */
cdalton397536c2016-03-25 12:15:03 -0700130 const GrBuffer* findOrCreateInstancedIndexBuffer(const uint16_t* pattern,
131 int patternSize,
132 int reps,
133 int vertCount,
134 const GrUniqueKey& key) {
135 if (GrBuffer* buffer = this->findAndRefTByUniqueKey<GrBuffer>(key)) {
bsalomoned0bcad2015-05-04 10:36:42 -0700136 return buffer;
137 }
138 return this->createInstancedIndexBuffer(pattern, patternSize, reps, vertCount, key);
139 }
140
141 /**
142 * Returns an index buffer that can be used to render quads.
143 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
cdalton397536c2016-03-25 12:15:03 -0700144 * The max number of quads is the buffer's index capacity divided by 6.
bsalomoned0bcad2015-05-04 10:36:42 -0700145 * Draw with kTriangles_GrPrimitiveType
146 * @ return the quad index buffer
147 */
cdalton397536c2016-03-25 12:15:03 -0700148 const GrBuffer* refQuadIndexBuffer() {
149 if (GrBuffer* buffer =
150 this->findAndRefTByUniqueKey<GrBuffer>(fQuadIndexBufferKey)) {
bsalomoned0bcad2015-05-04 10:36:42 -0700151 return buffer;
152 }
153 return this->createQuadIndexBuffer();
154 }
155
bsalomon706f08f2015-05-22 07:35:58 -0700156 /**
157 * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering
158 * is not supported.
159 */
bsalomon6663acf2016-05-10 09:14:17 -0700160 GrPath* createPath(const SkPath&, const GrStyle&);
161 GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStyle&);
reeda9322c22016-04-12 06:47:05 -0700162 GrPathRange* createGlyphs(const SkTypeface*, const SkScalerContextEffects&,
bsalomon6663acf2016-05-10 09:14:17 -0700163 const SkDescriptor*, const GrStyle&);
bsalomon706f08f2015-05-22 07:35:58 -0700164
Brian Osman32342f02017-03-04 08:12:46 -0500165 /** These flags govern which scratch resources we are allowed to return */
bsalomoneae62002015-07-31 13:59:30 -0700166 enum Flags {
Brian Osman32342f02017-03-04 08:12:46 -0500167 kExact_Flag = 0x1,
168
bsalomoneae62002015-07-31 13:59:30 -0700169 /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be
Robert Phillipsf2361d22016-10-25 14:20:06 -0400170 * set when accessing resources during a GrOpList flush. This includes the execution of
Brian Salomon25a88092016-12-01 09:36:50 -0500171 * GrOp objects. The reason is that these memory operations are done immediately and
bsalomoneae62002015-07-31 13:59:30 -0700172 * will occur out of order WRT the operations being flushed.
halcanary6950de62015-11-07 05:29:00 -0800173 * Make this automatic: https://bug.skia.org/4156
bsalomoneae62002015-07-31 13:59:30 -0700174 */
Brian Osman32342f02017-03-04 08:12:46 -0500175 kNoPendingIO_Flag = 0x2,
176
177 kNoCreate_Flag = 0x4,
csmartdalton485a1202016-07-13 10:16:32 -0700178
179 /** Normally the caps may indicate a preference for client-side buffers. Set this flag when
180 * creating a buffer to guarantee it resides in GPU memory.
181 */
Brian Osman32342f02017-03-04 08:12:46 -0500182 kRequireGpuMemory_Flag = 0x8,
bsalomoneae62002015-07-31 13:59:30 -0700183 };
184
cdaltone2e71c22016-04-07 18:13:29 -0700185 /**
186 * Returns a buffer.
187 *
188 * @param size minimum size of buffer to return.
189 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
190 * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed.
191 * @param flags see Flags enum.
cdalton1bf3e712016-04-19 10:00:02 -0700192 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700193 *
194 * @return the buffer if successful, otherwise nullptr.
195 */
cdalton1bf3e712016-04-19 10:00:02 -0700196 GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern, uint32_t flags,
197 const void* data = nullptr);
bsalomoneae62002015-07-31 13:59:30 -0700198
robertphillips1b8e1b52015-06-24 06:54:10 -0700199
Brian Salomon2ee084e2016-12-16 18:59:19 -0500200 /**
egdanielec00d942015-09-14 12:56:10 -0700201 * If passed in render target already has a stencil buffer, return it. Otherwise attempt to
202 * attach one.
203 */
204 GrStencilAttachment* attachStencilAttachment(GrRenderTarget* rt);
205
ericrkf7b8b8a2016-02-24 14:49:51 -0800206 /**
207 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
208 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
209 *
kkinnunen49c4c222016-04-01 04:50:37 -0700210 * The texture is wrapped as borrowed. The texture object will not be freed once the
211 * render target is destroyed.
212 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800213 * @return GrRenderTarget object or NULL on failure.
214 */
bungeman6bd52842016-10-27 09:30:08 -0700215 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc);
ericrkf7b8b8a2016-02-24 14:49:51 -0800216
Brian Osman32342f02017-03-04 08:12:46 -0500217 /**
218 * Assigns a unique key to a resource. If the key is associated with another resource that
219 * association is removed and replaced by this resource.
220 */
221 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
222
223 /**
224 * Finds a resource in the cache, based on the specified key. This is intended for use in
225 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
226 * caller must balance with a call to unref().
227 */
228 GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&);
229
230 void abandon() {
231 fCache = NULL;
232 fGpu = NULL;
233 }
234
bsalomoned0bcad2015-05-04 10:36:42 -0700235private:
Brian Osman32342f02017-03-04 08:12:46 -0500236 GrTexture* internalCreateApproxTexture(const GrSurfaceDesc& desc, uint32_t scratchTextureFlags);
237
238 GrTexture* refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags);
239
240 GrResourceCache* cache() { return fCache; }
241 const GrResourceCache* cache() const { return fCache; }
242
243 GrGpu* gpu() { return fGpu; }
244 const GrGpu* gpu() const { return fGpu; }
245
246 bool isAbandoned() const {
247 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
248 return !SkToBool(fCache);
249 }
250
cdalton397536c2016-03-25 12:15:03 -0700251 const GrBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
252 int patternSize,
253 int reps,
254 int vertCount,
255 const GrUniqueKey& key);
bsalomoned0bcad2015-05-04 10:36:42 -0700256
cdalton397536c2016-03-25 12:15:03 -0700257 const GrBuffer* createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700258
Brian Osman32342f02017-03-04 08:12:46 -0500259 GrResourceCache* fCache;
260 GrGpu* fGpu;
bsalomoned0bcad2015-05-04 10:36:42 -0700261 GrUniqueKey fQuadIndexBufferKey;
262
Brian Osman32342f02017-03-04 08:12:46 -0500263 // In debug builds we guard against improper thread handling
264 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700265};
266
267#endif