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