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 11 #include "GrBuffer.h" 12 #include "GrContextOptions.h" 13 #include "GrResourceCache.h" 14 #include "SkImageInfoPriv.h" 15 #include "SkScalerContext.h" 16 17 class GrBackendRenderTarget; 18 class GrBackendSemaphore; 19 class GrBackendTexture; 20 class GrGpu; 21 class GrPath; 22 class GrRenderTarget; 23 class GrResourceProviderPriv; 24 class GrSemaphore; 25 class GrSingleOwner; 26 class GrStencilAttachment; 27 class GrTexture; 28 struct GrVkDrawableInfo; 29 30 class GrStyle; 31 class SkDescriptor; 32 class SkPath; 33 class SkTypeface; 34 35 /** 36 * A factory for arbitrary resource types. This class is intended for use within the Gr code base. 37 * 38 * Some members force callers to make a flags (pendingIO) decision. This can be relaxed once 39 * https://bug.skia.org/4156 is fixed. 40 */ 41 class GrResourceProvider { 42 public: 43 /** These flags govern which scratch resources we are allowed to return */ 44 enum class Flags { 45 kNone = 0x0, 46 47 /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be 48 * set when accessing resources during a GrOpList flush. This includes the execution of 49 * GrOp objects. The reason is that these memory operations are done immediately and 50 * will occur out of order WRT the operations being flushed. 51 * Make this automatic: https://bug.skia.org/4156 52 */ 53 kNoPendingIO = 0x1, 54 55 /** Normally the caps may indicate a preference for client-side buffers. Set this flag when 56 * creating a buffer to guarantee it resides in GPU memory. 57 */ 58 kRequireGpuMemory = 0x2, 59 }; 60 61 GrResourceProvider(GrGpu*, GrResourceCache*, GrSingleOwner*, 62 GrContextOptions::Enable explicitlyAllocateGPUResources); 63 64 /** 65 * Finds a resource in the cache, based on the specified key. Prior to calling this, the caller 66 * must be sure that if a resource of exists in the cache with the given unique key then it is 67 * of type T. 68 */ findByUniqueKey(const GrUniqueKey & key)69 template <typename T = GrGpuResource> sk_sp<T> findByUniqueKey(const GrUniqueKey& key) { 70 return sk_sp<T>(static_cast<T*>(this->findResourceByUniqueKey(key).release())); 71 } 72 73 /////////////////////////////////////////////////////////////////////////// 74 // Textures 75 76 /** 77 * Finds a texture that approximately matches the descriptor. Will be at least as large in width 78 * and height as desc specifies. If desc specifies that the texture should be a render target 79 * then result will be a render target. Format and sample count will always match the request. 80 * The contents of the texture are undefined. 81 */ 82 sk_sp<GrTexture> createApproxTexture(const GrSurfaceDesc&, Flags); 83 84 /** Create an exact fit texture with no initial data to upload. 85 */ 86 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, Flags = Flags::kNone); 87 88 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, const GrMipLevel texels[], 89 int mipLevelCount); 90 91 // Create a potentially loose fit texture with the provided data 92 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, SkBackingFit, 93 const GrMipLevel&, Flags); 94 95 /////////////////////////////////////////////////////////////////////////// 96 // Wrapped Backend Surfaces 97 98 /** 99 * Wraps an existing texture with a GrTexture object. 100 * 101 * GrIOType must either be kRead or kRW. kRead blocks any operations that would modify the 102 * pixels (e.g. dst for a copy, regenerating MIP levels, write pixels). 103 * 104 * OpenGL: if the object is a texture Gr may change its GL texture params 105 * when it is drawn. 106 * 107 * @return GrTexture object or NULL on failure. 108 */ 109 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture& tex, GrWrapOwnership, 110 GrWrapCacheable, GrIOType); 111 112 /** 113 * This makes the backend texture be renderable. If sampleCnt is > 1 and the underlying API 114 * uses separate MSAA render buffers then a MSAA render buffer is created that resolves 115 * to the texture. 116 */ 117 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture& tex, 118 int sampleCnt, 119 GrWrapOwnership, 120 GrWrapCacheable); 121 122 /** 123 * Wraps an existing render target with a GrRenderTarget object. It is 124 * similar to wrapBackendTexture but can be used to draw into surfaces 125 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that 126 * the client will resolve to a texture). Currently wrapped render targets 127 * always use the kBorrow_GrWrapOwnership and GrWrapCacheable::kNo semantics. 128 * 129 * @return GrRenderTarget object or NULL on failure. 130 */ 131 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&); 132 133 sk_sp<GrRenderTarget> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&, 134 const GrVkDrawableInfo&); 135 136 static const uint32_t kMinScratchTextureSize; 137 138 /** 139 * Either finds and refs, or creates a static buffer with the given parameters and contents. 140 * 141 * @param intendedType hint to the graphics subsystem about what the buffer will be used for. 142 * @param size minimum size of buffer to return. 143 * @param data optional data with which to initialize the buffer. 144 * @param key Key to be assigned to the buffer. 145 * 146 * @return The buffer if successful, otherwise nullptr. 147 */ 148 sk_sp<const GrBuffer> findOrMakeStaticBuffer(GrBufferType intendedType, size_t size, 149 const void* data, const GrUniqueKey& key); 150 151 /** 152 * Either finds and refs, or creates an index buffer with a repeating pattern for drawing 153 * contiguous vertices of a repeated mesh. If the return is non-null, the caller owns a ref on 154 * the returned GrBuffer. 155 * 156 * @param pattern the pattern of indices to repeat 157 * @param patternSize size in bytes of the pattern 158 * @param reps number of times to repeat the pattern 159 * @param vertCount number of vertices the pattern references 160 * @param key Key to be assigned to the index buffer. 161 * 162 * @return The index buffer if successful, otherwise nullptr. 163 */ findOrCreatePatternedIndexBuffer(const uint16_t * pattern,int patternSize,int reps,int vertCount,const GrUniqueKey & key)164 sk_sp<const GrBuffer> findOrCreatePatternedIndexBuffer(const uint16_t* pattern, 165 int patternSize, 166 int reps, 167 int vertCount, 168 const GrUniqueKey& key) { 169 if (auto buffer = this->findByUniqueKey<GrBuffer>(key)) { 170 return std::move(buffer); 171 } 172 return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, key); 173 } 174 175 /** 176 * Returns an index buffer that can be used to render quads. 177 * Six indices per quad: 0, 1, 2, 2, 1, 3, etc. 178 * The max number of quads is the buffer's index capacity divided by 6. 179 * Draw with GrPrimitiveType::kTriangles 180 * @ return the quad index buffer 181 */ refQuadIndexBuffer()182 sk_sp<const GrBuffer> refQuadIndexBuffer() { 183 if (auto buffer = this->findByUniqueKey<const GrBuffer>(fQuadIndexBufferKey)) { 184 return buffer; 185 } 186 return this->createQuadIndexBuffer(); 187 } 188 189 static int QuadCountOfQuadBuffer(); 190 191 /** 192 * Factories for GrPath objects. It's an error to call these if path rendering 193 * is not supported. 194 */ 195 sk_sp<GrPath> createPath(const SkPath&, const GrStyle&); 196 197 /** 198 * Returns a buffer. 199 * 200 * @param size minimum size of buffer to return. 201 * @param intendedType hint to the graphics subsystem about what the buffer will be used for. 202 * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed. 203 * @param flags see Flags enum. 204 * @param data optional data with which to initialize the buffer. 205 * 206 * @return the buffer if successful, otherwise nullptr. 207 */ 208 sk_sp<GrBuffer> createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern, Flags, 209 const void* data = nullptr); 210 211 /** 212 * If passed in render target already has a stencil buffer, return true. Otherwise attempt to 213 * attach one and return true on success. 214 */ 215 bool attachStencilAttachment(GrRenderTarget* rt); 216 217 /** 218 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided 219 * texture has a format that cannot be textured from by Skia, but we want to raster to it. 220 * 221 * The texture is wrapped as borrowed. The texture object will not be freed once the 222 * render target is destroyed. 223 * 224 * @return GrRenderTarget object or NULL on failure. 225 */ 226 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&, 227 int sampleCnt); 228 229 /** 230 * Assigns a unique key to a resource. If the key is associated with another resource that 231 * association is removed and replaced by this resource. 232 */ 233 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*); 234 235 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true); 236 237 enum class SemaphoreWrapType { 238 kWillSignal, 239 kWillWait, 240 }; 241 242 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&, 243 SemaphoreWrapType wrapType, 244 GrWrapOwnership = kBorrow_GrWrapOwnership); 245 abandon()246 void abandon() { 247 fCache = nullptr; 248 fGpu = nullptr; 249 } 250 contextUniqueID()251 uint32_t contextUniqueID() const { return fCache->contextUniqueID(); } caps()252 const GrCaps* caps() const { return fCaps.get(); } overBudget()253 bool overBudget() const { return fCache->overBudget(); } 254 255 inline GrResourceProviderPriv priv(); 256 inline const GrResourceProviderPriv priv() const; 257 explicitlyAllocateGPUResources()258 bool explicitlyAllocateGPUResources() const { return fExplicitlyAllocateGPUResources; } 259 260 bool testingOnly_setExplicitlyAllocateGPUResources(bool newValue); 261 262 private: 263 sk_sp<GrGpuResource> findResourceByUniqueKey(const GrUniqueKey&); 264 265 // Attempts to find a resource in the cache that exactly matches the GrSurfaceDesc. Failing that 266 // it returns null. If non-null, the resulting texture is always budgeted. 267 sk_sp<GrTexture> refScratchTexture(const GrSurfaceDesc&, Flags); 268 269 /* 270 * Try to find an existing scratch texture that exactly matches 'desc'. If successful 271 * update the budgeting accordingly. 272 */ 273 sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc&, SkBudgeted, Flags); 274 cache()275 GrResourceCache* cache() { return fCache; } cache()276 const GrResourceCache* cache() const { return fCache; } 277 278 friend class GrResourceProviderPriv; 279 280 // Method made available via GrResourceProviderPriv gpu()281 GrGpu* gpu() { return fGpu; } gpu()282 const GrGpu* gpu() const { return fGpu; } 283 isAbandoned()284 bool isAbandoned() const { 285 SkASSERT(SkToBool(fGpu) == SkToBool(fCache)); 286 return !SkToBool(fCache); 287 } 288 289 sk_sp<const GrBuffer> createPatternedIndexBuffer(const uint16_t* pattern, 290 int patternSize, 291 int reps, 292 int vertCount, 293 const GrUniqueKey& key); 294 295 sk_sp<const GrBuffer> createQuadIndexBuffer(); 296 297 GrResourceCache* fCache; 298 GrGpu* fGpu; 299 sk_sp<const GrCaps> fCaps; 300 GrUniqueKey fQuadIndexBufferKey; 301 bool fExplicitlyAllocateGPUResources; 302 303 // In debug builds we guard against improper thread handling 304 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) 305 }; 306 307 GR_MAKE_BITFIELD_CLASS_OPS(GrResourceProvider::Flags); 308 309 #endif 310