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 GrTextureProvider_DEFINED |
| 9 | #define GrTextureProvider_DEFINED |
| 10 | |
| 11 | #include "GrTexture.h" |
| 12 | |
| 13 | class SK_API GrTextureProvider { |
| 14 | public: |
| 15 | /////////////////////////////////////////////////////////////////////////// |
| 16 | // Textures |
| 17 | |
| 18 | /** |
| 19 | * Creates a new texture in the resource cache and returns it. The caller owns a |
| 20 | * ref on the returned texture which must be balanced by a call to unref. |
| 21 | * |
| 22 | * @param desc Description of the texture properties. |
| 23 | * @param budgeted Does the texture count against the resource cache budget? |
| 24 | * @param srcData Pointer to the pixel values (optional). |
| 25 | * @param rowBytes The number of bytes between rows of the texture. Zero |
| 26 | * implies tightly packed rows. For compressed pixel configs, this |
| 27 | * field is ignored. |
| 28 | */ |
| 29 | GrTexture* createTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData, |
| 30 | size_t rowBytes); |
| 31 | |
| 32 | /** Shortcut for creating a texture with no initial data to upload. */ |
| 33 | GrTexture* createTexture(const GrSurfaceDesc& desc, bool budgeted) { |
| 34 | return this->createTexture(desc, budgeted, NULL, 0); |
| 35 | } |
| 36 | |
| 37 | /** Assigns a unique key to the texture. The texture will be findable via this key using |
| 38 | findTextureByUniqueKey(). If an existing texture has this key, it's key will be removed. */ |
| 39 | void assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) { |
| 40 | this->assignUniqueKeyToResource(key, texture); |
| 41 | } |
| 42 | |
| 43 | /** Finds a texture by unique key. If the texture is found it is ref'ed and returned. */ |
| 44 | GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key) { |
| 45 | GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key); |
| 46 | if (resource) { |
| 47 | GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture(); |
| 48 | SkASSERT(texture); |
| 49 | return texture; |
| 50 | } |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Determines whether a texture is associated with the unique key. If the texture is found it |
| 56 | * will not be locked or returned. This call does not affect the priority of the resource for |
| 57 | * deletion. |
| 58 | */ |
| 59 | bool existsTextureWithUniqueKey(const GrUniqueKey& key) const { |
| 60 | return this->existsResourceWithUniqueKey(key); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Enum that determines how closely a returned scratch texture must match |
| 65 | * a provided GrSurfaceDesc. TODO: Remove this. createTexture() should be used |
| 66 | * for exact match and refScratchTexture() should be replaced with createApproxTexture(). |
| 67 | */ |
| 68 | enum ScratchTexMatch { |
| 69 | /** |
| 70 | * Finds a texture that exactly matches the descriptor. |
| 71 | */ |
| 72 | kExact_ScratchTexMatch, |
| 73 | /** |
| 74 | * Finds a texture that approximately matches the descriptor. Will be |
| 75 | * at least as large in width and height as desc specifies. If desc |
| 76 | * specifies that texture is a render target then result will be a |
| 77 | * render target. If desc specifies a render target and doesn't set the |
| 78 | * no stencil flag then result will have a stencil. Format and aa level |
| 79 | * will always match. |
| 80 | */ |
| 81 | kApprox_ScratchTexMatch |
| 82 | }; |
| 83 | |
| 84 | /** |
| 85 | * Returns a texture matching the desc. It's contents are unknown. The caller |
| 86 | * owns a ref on the returned texture and must balance with a call to unref. |
| 87 | * It is guaranteed that the same texture will not be returned in subsequent |
| 88 | * calls until all refs to the texture are dropped. |
| 89 | * |
| 90 | * internalFlag is a temporary workaround until changes in the internal |
| 91 | * architecture are complete. Use the default value. |
| 92 | * |
| 93 | * TODO: Once internal flag can be removed, this should be replaced with |
| 94 | * createApproxTexture() and exact textures should be created with |
| 95 | * createTexture(). |
| 96 | */ |
| 97 | GrTexture* refScratchTexture(const GrSurfaceDesc&, ScratchTexMatch match, |
| 98 | bool internalFlag = false); |
| 99 | |
| 100 | /////////////////////////////////////////////////////////////////////////// |
| 101 | // Wrapped Backend Surfaces |
| 102 | |
| 103 | /** |
| 104 | * Wraps an existing texture with a GrTexture object. |
| 105 | * |
| 106 | * OpenGL: if the object is a texture Gr may change its GL texture params |
| 107 | * when it is drawn. |
| 108 | * |
| 109 | * @param desc description of the object to create. |
| 110 | * |
| 111 | * @return GrTexture object or NULL on failure. |
| 112 | */ |
| 113 | GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc); |
| 114 | |
| 115 | /** |
| 116 | * Wraps an existing render target with a GrRenderTarget object. It is |
| 117 | * similar to wrapBackendTexture but can be used to draw into surfaces |
| 118 | * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that |
| 119 | * the client will resolve to a texture). |
| 120 | * |
| 121 | * @param desc description of the object to create. |
| 122 | * |
| 123 | * @return GrTexture object or NULL on failure. |
| 124 | */ |
| 125 | GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc); |
| 126 | |
| 127 | protected: |
| 128 | GrTextureProvider(GrGpu* gpu, GrResourceCache* cache) : fCache(cache), fGpu(gpu) {} |
| 129 | |
| 130 | /** |
| 131 | * Assigns a unique key to a resource. If the key is associated with another resource that |
| 132 | * association is removed and replaced by this resource. |
| 133 | */ |
| 134 | void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*); |
| 135 | |
| 136 | /** |
| 137 | * Finds a resource in the cache, based on the specified key. This is intended for use in |
| 138 | * conjunction with addResourceToCache(). The return value will be NULL if not found. The |
| 139 | * caller must balance with a call to unref(). |
| 140 | */ |
| 141 | GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&); |
| 142 | |
| 143 | /** |
| 144 | * Determines whether a resource is in the cache. If the resource is found it |
| 145 | * will not be locked or returned. This call does not affect the priority of |
| 146 | * the resource for deletion. |
| 147 | */ |
| 148 | bool existsResourceWithUniqueKey(const GrUniqueKey& key) const; |
| 149 | |
| 150 | GrTexture* internalRefScratchTexture(const GrSurfaceDesc&, uint32_t flags); |
| 151 | |
| 152 | void abandon() { |
| 153 | fCache = NULL; |
| 154 | fGpu = NULL; |
| 155 | } |
| 156 | |
| 157 | private: |
| 158 | bool isAbandoned() const { |
| 159 | SkASSERT(SkToBool(fGpu) == SkToBool(fCache)); |
| 160 | return !SkToBool(fCache); |
| 161 | } |
| 162 | |
| 163 | GrResourceCache* fCache; |
| 164 | GrGpu* fGpu; |
| 165 | }; |
| 166 | |
| 167 | #endif |