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 | |
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 | * |
| 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 | */ |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 32 | GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, const void* srcData, |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 33 | size_t rowBytes); |
| 34 | |
| 35 | /** Shortcut for creating a texture with no initial data to upload. */ |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 36 | GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 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. */ |
| 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. */ |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 47 | GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 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 | */ |
| 54 | bool existsTextureWithUniqueKey(const GrUniqueKey& key) const { |
| 55 | return this->existsResourceWithUniqueKey(key); |
| 56 | } |
| 57 | |
| 58 | /** |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 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. |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 64 | */ |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 65 | GrTexture* createApproxTexture(const GrSurfaceDesc&); |
| 66 | |
| 67 | /** Legacy function that no longer should be used. */ |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 68 | enum ScratchTexMatch { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 69 | kExact_ScratchTexMatch, |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 70 | kApprox_ScratchTexMatch |
| 71 | }; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 72 | GrTexture* refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match) { |
| 73 | if (kApprox_ScratchTexMatch == match) { |
| 74 | return this->createApproxTexture(desc); |
| 75 | } else { |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 76 | return this->createTexture(desc, SkBudgeted::kYes); |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 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 | * |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 89 | * @return GrTexture object or NULL on failure. |
| 90 | */ |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 91 | GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc, |
| 92 | GrWrapOwnership = kBorrow_GrWrapOwnership); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 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 |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 98 | * the client will resolve to a texture). Currently wrapped render targets |
| 99 | * always use the kBorrow_GrWrapOwnership semantics. |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 100 | * |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 101 | * @return GrRenderTarget object or NULL on failure. |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 102 | */ |
| 103 | GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc); |
| 104 | |
| 105 | protected: |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 106 | GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 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 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 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); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 138 | |
| 139 | void abandon() { |
| 140 | fCache = NULL; |
| 141 | fGpu = NULL; |
| 142 | } |
| 143 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 144 | GrResourceCache* cache() { return fCache; } |
| 145 | const GrResourceCache* cache() const { return fCache; } |
| 146 | |
| 147 | GrGpu* gpu() { return fGpu; } |
| 148 | const GrGpu* gpu() const { return fGpu; } |
| 149 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 150 | bool isAbandoned() const { |
| 151 | SkASSERT(SkToBool(fGpu) == SkToBool(fCache)); |
| 152 | return !SkToBool(fCache); |
| 153 | } |
| 154 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 155 | private: |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 156 | GrResourceCache* fCache; |
| 157 | GrGpu* fGpu; |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 158 | |
| 159 | // In debug builds we guard against improper thread handling |
| 160 | SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | #endif |