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