bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 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 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 11 | #include "GrBuffer.h" |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 12 | #include "GrGpu.h" |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 13 | #include "GrPathRange.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 14 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 15 | class GrPath; |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 16 | class GrRenderTarget; |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 17 | class GrSingleOwner; |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 18 | class GrStencilAttachment; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 19 | class GrStyle; |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 20 | class SkDescriptor; |
| 21 | class SkPath; |
| 22 | class SkTypeface; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 23 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 24 | /** |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 25 | * A factory for arbitrary resource types. This class is intended for use within the Gr code base. |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 26 | * |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 27 | * Some members force callers to make a flags (pendingIO) decision. This can be relaxed once |
| 28 | * https://bug.skia.org/4156 is fixed. |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 29 | */ |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 30 | class GrResourceProvider { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 31 | public: |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 32 | GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 33 | |
| 34 | template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) { |
| 35 | return static_cast<T*>(this->findAndRefResourceByUniqueKey(key)); |
| 36 | } |
| 37 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 38 | /////////////////////////////////////////////////////////////////////////// |
| 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 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 117 | /** |
| 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 |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 120 | * a ref on the returned GrBuffer. |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 121 | * |
| 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 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 128 | * @return The index buffer if successful, otherwise nullptr. |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 129 | */ |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 130 | 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)) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 136 | 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. |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 144 | * The max number of quads is the buffer's index capacity divided by 6. |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 145 | * Draw with kTriangles_GrPrimitiveType |
| 146 | * @ return the quad index buffer |
| 147 | */ |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 148 | const GrBuffer* refQuadIndexBuffer() { |
| 149 | if (GrBuffer* buffer = |
| 150 | this->findAndRefTByUniqueKey<GrBuffer>(fQuadIndexBufferKey)) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 151 | return buffer; |
| 152 | } |
| 153 | return this->createQuadIndexBuffer(); |
| 154 | } |
| 155 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering |
| 158 | * is not supported. |
| 159 | */ |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 160 | GrPath* createPath(const SkPath&, const GrStyle&); |
| 161 | GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStyle&); |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 162 | GrPathRange* createGlyphs(const SkTypeface*, const SkScalerContextEffects&, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 163 | const SkDescriptor*, const GrStyle&); |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 164 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 165 | /** These flags govern which scratch resources we are allowed to return */ |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 166 | enum Flags { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 167 | kExact_Flag = 0x1, |
| 168 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 169 | /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 170 | * set when accessing resources during a GrOpList flush. This includes the execution of |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 171 | * GrOp objects. The reason is that these memory operations are done immediately and |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 172 | * will occur out of order WRT the operations being flushed. |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 173 | * Make this automatic: https://bug.skia.org/4156 |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 174 | */ |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 175 | kNoPendingIO_Flag = 0x2, |
| 176 | |
| 177 | kNoCreate_Flag = 0x4, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 178 | |
| 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 Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 182 | kRequireGpuMemory_Flag = 0x8, |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 185 | /** |
| 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. |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 192 | * @param data optional data with which to initialize the buffer. |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 193 | * |
| 194 | * @return the buffer if successful, otherwise nullptr. |
| 195 | */ |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 196 | GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern, uint32_t flags, |
| 197 | const void* data = nullptr); |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 198 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 199 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 200 | /** |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 201 | * 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 | |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 206 | /** |
| 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 | * |
kkinnunen | 49c4c22 | 2016-04-01 04:50:37 -0700 | [diff] [blame] | 210 | * The texture is wrapped as borrowed. The texture object will not be freed once the |
| 211 | * render target is destroyed. |
| 212 | * |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 213 | * @return GrRenderTarget object or NULL on failure. |
| 214 | */ |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 215 | sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 216 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 217 | /** |
| 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 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 235 | private: |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 236 | 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 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 251 | const GrBuffer* createInstancedIndexBuffer(const uint16_t* pattern, |
| 252 | int patternSize, |
| 253 | int reps, |
| 254 | int vertCount, |
| 255 | const GrUniqueKey& key); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 256 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 257 | const GrBuffer* createQuadIndexBuffer(); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 258 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 259 | GrResourceCache* fCache; |
| 260 | GrGpu* fGpu; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 261 | GrUniqueKey fQuadIndexBufferKey; |
| 262 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 263 | // In debug builds we guard against improper thread handling |
| 264 | SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | #endif |