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