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