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 GrTextureProvider_DEFINED 9 #define GrTextureProvider_DEFINED 10 11 #include "GrTexture.h" 12 #include "SkImageFilter.h" 13 14 class GrSingleOwner; 15 16 class SK_API GrTextureProvider { 17 public: 18 /////////////////////////////////////////////////////////////////////////// 19 // Textures 20 21 /** 22 * Creates a new texture in the resource cache and returns it. The caller owns a 23 * ref on the returned texture which must be balanced by a call to unref. 24 * 25 * @param desc Description of the texture properties. 26 * @param budgeted Does the texture count against the resource cache budget? 27 * @param srcData Pointer to the pixel values (optional). 28 * @param rowBytes The number of bytes between rows of the texture. Zero 29 * implies tightly packed rows. For compressed pixel configs, this 30 * field is ignored. 31 */ 32 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, const void* srcData, 33 size_t rowBytes); 34 35 /** Shortcut for creating a texture with no initial data to upload. */ createTexture(const GrSurfaceDesc & desc,SkBudgeted budgeted)36 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) { 37 return this->createTexture(desc, budgeted, NULL, 0); 38 } 39 40 /** Assigns a unique key to the texture. The texture will be findable via this key using 41 findTextureByUniqueKey(). If an existing texture has this key, it's key will be removed. */ assignUniqueKeyToTexture(const GrUniqueKey & key,GrTexture * texture)42 void assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) { 43 this->assignUniqueKeyToResource(key, texture); 44 } 45 46 /** Finds a texture by unique key. If the texture is found it is ref'ed and returned. */ 47 GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key); 48 49 /** 50 * Determines whether a texture is associated with the unique key. If the texture is found it 51 * will not be locked or returned. This call does not affect the priority of the resource for 52 * deletion. 53 */ existsTextureWithUniqueKey(const GrUniqueKey & key)54 bool existsTextureWithUniqueKey(const GrUniqueKey& key) const { 55 return this->existsResourceWithUniqueKey(key); 56 } 57 58 /** 59 * Finds a texture that approximately matches the descriptor. Will be at least as large in width 60 * and height as desc specifies. If desc specifies that the texture should be a render target 61 * then result will be a render target. Format and sample count will always match the request. 62 * The contents of the texture are undefined. The caller owns a ref on the returned texture and 63 * must balance with a call to unref. 64 */ 65 GrTexture* createApproxTexture(const GrSurfaceDesc&); 66 67 /** Legacy function that no longer should be used. */ 68 enum ScratchTexMatch { 69 kExact_ScratchTexMatch, 70 kApprox_ScratchTexMatch 71 }; refScratchTexture(const GrSurfaceDesc & desc,ScratchTexMatch match)72 GrTexture* refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match) { 73 if (kApprox_ScratchTexMatch == match) { 74 return this->createApproxTexture(desc); 75 } else { 76 return this->createTexture(desc, SkBudgeted::kYes); 77 } 78 } 79 80 /////////////////////////////////////////////////////////////////////////// 81 // Wrapped Backend Surfaces 82 83 /** 84 * Wraps an existing texture with a GrTexture object. 85 * 86 * OpenGL: if the object is a texture Gr may change its GL texture params 87 * when it is drawn. 88 * 89 * @return GrTexture object or NULL on failure. 90 */ 91 GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc, 92 GrWrapOwnership = kBorrow_GrWrapOwnership); 93 94 /** 95 * Wraps an existing render target with a GrRenderTarget object. It is 96 * similar to wrapBackendTexture but can be used to draw into surfaces 97 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that 98 * the client will resolve to a texture). Currently wrapped render targets 99 * always use the kBorrow_GrWrapOwnership semantics. 100 * 101 * @return GrRenderTarget object or NULL on failure. 102 */ 103 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc); 104 105 protected: 106 GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner); 107 108 /** 109 * Assigns a unique key to a resource. If the key is associated with another resource that 110 * association is removed and replaced by this resource. 111 */ 112 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*); 113 114 /** 115 * Finds a resource in the cache, based on the specified key. This is intended for use in 116 * conjunction with addResourceToCache(). The return value will be NULL if not found. The 117 * caller must balance with a call to unref(). 118 */ 119 GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&); 120 121 /** 122 * Determines whether a resource is in the cache. If the resource is found it 123 * will not be locked or returned. This call does not affect the priority of 124 * the resource for deletion. 125 */ 126 bool existsResourceWithUniqueKey(const GrUniqueKey& key) const; 127 128 enum ScratchTextureFlags { 129 kExact_ScratchTextureFlag = 0x1, 130 kNoPendingIO_ScratchTextureFlag = 0x2, // (http://skbug.com/4156) 131 kNoCreate_ScratchTextureFlag = 0x4, 132 }; 133 134 /** A common impl for GrTextureProvider and GrResourceProvider variants. */ 135 GrTexture* internalCreateApproxTexture(const GrSurfaceDesc& desc, uint32_t scratchTextureFlags); 136 137 GrTexture* refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags); 138 abandon()139 void abandon() { 140 fCache = NULL; 141 fGpu = NULL; 142 } 143 cache()144 GrResourceCache* cache() { return fCache; } cache()145 const GrResourceCache* cache() const { return fCache; } 146 gpu()147 GrGpu* gpu() { return fGpu; } gpu()148 const GrGpu* gpu() const { return fGpu; } 149 isAbandoned()150 bool isAbandoned() const { 151 SkASSERT(SkToBool(fGpu) == SkToBool(fCache)); 152 return !SkToBool(fCache); 153 } 154 155 private: 156 GrResourceCache* fCache; 157 GrGpu* fGpu; 158 159 // In debug builds we guard against improper thread handling 160 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) 161 }; 162 163 #endif 164