robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 GrTextureProxy_DEFINED |
| 9 | #define GrTextureProxy_DEFINED |
| 10 | |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrSamplerState.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrSurfaceProxy.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 13 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 14 | class GrCaps; |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 15 | class GrDeferredProxyUploader; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 16 | class GrProxyProvider; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 17 | class GrResourceProvider; |
Robert Phillips | 420c4cf | 2017-09-28 09:00:45 -0400 | [diff] [blame] | 18 | class GrTextureProxyPriv; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 19 | |
| 20 | // This class delays the acquisition of textures until they are actually required |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 21 | class GrTextureProxy : virtual public GrSurfaceProxy { |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 22 | public: |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 23 | GrTextureProxy* asTextureProxy() override { return this; } |
| 24 | const GrTextureProxy* asTextureProxy() const override { return this; } |
| 25 | |
| 26 | // Actually instantiate the backing texture, if necessary |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 27 | bool instantiate(GrResourceProvider*) override; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 28 | |
Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 29 | // If we are instantiated and have a target, return the mip state of that target. Otherwise |
| 30 | // returns the proxy's mip state from creation time. This is useful for lazy proxies which may |
| 31 | // claim to not need mips at creation time, but the instantiation happens to give us a mipped |
| 32 | // target. In that case we should use that for our benefit to avoid possible copies/mip |
| 33 | // generation later. |
| 34 | GrMipMapped mipMapped() const; |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 35 | |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 36 | bool mipMapsAreDirty() const { |
| 37 | SkASSERT((GrMipMapped::kNo == fMipMapped) == |
| 38 | (GrMipMapsStatus::kNotAllocated == fMipMapsStatus)); |
| 39 | return GrMipMapped::kYes == fMipMapped && GrMipMapsStatus::kValid != fMipMapsStatus; |
| 40 | } |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 41 | void markMipMapsDirty() { |
| 42 | SkASSERT(GrMipMapped::kYes == fMipMapped); |
| 43 | fMipMapsStatus = GrMipMapsStatus::kDirty; |
| 44 | } |
| 45 | void markMipMapsClean() { |
| 46 | SkASSERT(GrMipMapped::kYes == fMipMapped); |
| 47 | fMipMapsStatus = GrMipMapsStatus::kValid; |
| 48 | } |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 49 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 50 | // Returns the GrMipMapped value of the proxy from creation time regardless of whether it has |
| 51 | // been instantiated or not. |
| 52 | GrMipMapped proxyMipMapped() const { return fMipMapped; } |
| 53 | |
Brian Salomon | 7da2fc7 | 2018-12-10 13:40:07 -0500 | [diff] [blame] | 54 | GrTextureType textureType() const { return this->backendFormat().textureType(); } |
| 55 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 56 | /** If true then the texture does not support MIP maps and only supports clamp wrap mode. */ |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 57 | bool hasRestrictedSampling() const { |
| 58 | return GrTextureTypeHasRestrictedSampling(this->textureType()); |
| 59 | } |
Greg Daniel | 45723ac | 2018-11-30 10:12:43 -0500 | [diff] [blame] | 60 | |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 61 | // Returns the highest allowed filter mode for a given texture type |
| 62 | static GrSamplerState::Filter HighestFilterMode(const GrTextureType textureType); |
| 63 | |
Greg Daniel | 45723ac | 2018-11-30 10:12:43 -0500 | [diff] [blame] | 64 | // Returns true if the passed in proxies can be used as dynamic state together when flushing |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 65 | // draws to the gpu. This accepts GrSurfaceProxy since the information needed is defined on |
| 66 | // that type, but this function exists in GrTextureProxy because it's only relevant when the |
| 67 | // proxies are being used as textures. |
| 68 | static bool ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first, |
| 69 | const GrSurfaceProxy* second); |
Greg Daniel | 45723ac | 2018-11-30 10:12:43 -0500 | [diff] [blame] | 70 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 71 | /** |
| 72 | * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one. |
| 73 | */ |
| 74 | const GrUniqueKey& getUniqueKey() const { |
| 75 | #ifdef SK_DEBUG |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 76 | if (this->isInstantiated() && fUniqueKey.isValid() && fSyncTargetKey) { |
| 77 | GrSurface* surface = this->peekSurface(); |
| 78 | SkASSERT(surface); |
| 79 | |
| 80 | SkASSERT(surface->getUniqueKey().isValid()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 81 | // It is possible for a non-keyed proxy to have a uniquely keyed resource assigned to |
| 82 | // it. This just means that a future user of the resource will be filling it with unique |
| 83 | // data. However, if the proxy has a unique key its attached resource should also |
| 84 | // have that key. |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 85 | SkASSERT(fUniqueKey == surface->getUniqueKey()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 86 | } |
| 87 | #endif |
| 88 | |
| 89 | return fUniqueKey; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Internal-only helper class used for manipulations of the resource by the cache. |
| 94 | */ |
| 95 | class CacheAccess; |
| 96 | inline CacheAccess cacheAccess(); |
| 97 | inline const CacheAccess cacheAccess() const; |
| 98 | |
Robert Phillips | 420c4cf | 2017-09-28 09:00:45 -0400 | [diff] [blame] | 99 | // Provides access to special purpose functions. |
| 100 | GrTextureProxyPriv texPriv(); |
| 101 | const GrTextureProxyPriv texPriv() const; |
| 102 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 103 | protected: |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 104 | // DDL TODO: rm the GrSurfaceProxy friending |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 105 | friend class GrSurfaceProxy; // for ctors |
| 106 | friend class GrProxyProvider; // for ctors |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 107 | friend class GrTextureProxyPriv; |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 108 | friend class GrSurfaceProxyPriv; // ability to change key sync state after lazy instantiation. |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 109 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 110 | // Deferred version - no data. |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 111 | GrTextureProxy(const GrBackendFormat&, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 112 | SkISize, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 113 | GrMipMapped, |
| 114 | GrMipMapsStatus, |
| 115 | const GrSwizzle& textureSwizzle, |
| 116 | SkBackingFit, |
| 117 | SkBudgeted, |
| 118 | GrProtected, |
| 119 | GrInternalSurfaceFlags, |
| 120 | UseAllocator); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 121 | |
| 122 | // Lazy-callback version |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 123 | // There are two main use cases for lazily-instantiated proxies: |
| 124 | // basic knowledge - width, height, config, origin are known |
| 125 | // minimal knowledge - only config is known. |
| 126 | // |
| 127 | // The basic knowledge version is used for DDL where we know the type of proxy we are going to |
| 128 | // use, but we don't have access to the GPU yet to instantiate it. |
| 129 | // |
| 130 | // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not |
| 131 | // know the final size until flush time. |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 132 | GrTextureProxy(LazyInstantiateCallback&&, |
| 133 | const GrBackendFormat&, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 134 | SkISize, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 135 | GrMipMapped, |
| 136 | GrMipMapsStatus, |
| 137 | const GrSwizzle& textureSwizzle, |
| 138 | SkBackingFit, |
| 139 | SkBudgeted, |
| 140 | GrProtected, |
| 141 | GrInternalSurfaceFlags, |
| 142 | UseAllocator); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 143 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 144 | // Wrapped version |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 145 | GrTextureProxy(sk_sp<GrSurface>, const GrSwizzle&, UseAllocator); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 146 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 147 | ~GrTextureProxy() override; |
| 148 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 149 | sk_sp<GrSurface> createSurface(GrResourceProvider*) const override; |
| 150 | |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 151 | void setTargetKeySync(bool sync) { fSyncTargetKey = sync; } |
| 152 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 153 | private: |
Brian Osman | 086679b | 2018-09-10 16:26:51 -0400 | [diff] [blame] | 154 | // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings |
| 155 | // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes |
| 156 | // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for |
| 157 | // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this' |
| 158 | // in the constructors, and always looks for the full 16 byte alignment, even if the fields in |
| 159 | // that particular class don't require it. Changing the size of this object can move the start |
| 160 | // address of other types, leading to this problem. |
| 161 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 162 | GrMipMapped fMipMapped; |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 163 | |
| 164 | // This tracks the mipmap status at the proxy level and is thus somewhat distinct from the |
| 165 | // backing GrTexture's mipmap status. In particular, this status is used to determine when |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 166 | // mipmap levels need to be explicitly regenerated during the execution of a DAG of opsTasks. |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 167 | GrMipMapsStatus fMipMapsStatus; |
| 168 | // TEMPORARY: We are in the process of moving GrMipMapsStatus from the texture to the proxy. |
| 169 | // We track the fInitialMipMapsStatus here so we can assert that the proxy did indeed expect |
| 170 | // the correct mipmap status immediately after instantiation. |
| 171 | // |
| 172 | // NOTE: fMipMapsStatus may no longer be equal to fInitialMipMapsStatus by the time the texture |
| 173 | // is instantiated, since it tracks mipmaps in the time frame in which the DAG is being built. |
Michael Ludwig | 61eba6c | 2019-11-01 11:21:58 -0400 | [diff] [blame] | 174 | SkDEBUGCODE(const GrMipMapsStatus fInitialMipMapsStatus;) |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 175 | |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 176 | bool fSyncTargetKey = true; // Should target's unique key be sync'ed with ours. |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 177 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 178 | GrUniqueKey fUniqueKey; |
| 179 | GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 180 | |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 181 | // Only used for proxies whose contents are being prepared on a worker thread. This object |
| 182 | // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that |
| 183 | // point, the proxy is instantiated, and this data is used to perform an ASAP upload. |
| 184 | std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader; |
| 185 | |
Greg Daniel | 7fd7a8a | 2019-10-10 16:10:31 -0400 | [diff] [blame] | 186 | size_t onUninstantiatedGpuMemorySize(const GrCaps&) const override; |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 187 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 188 | // Methods made available via GrTextureProxy::CacheAccess |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 189 | void setUniqueKey(GrProxyProvider*, const GrUniqueKey&); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 190 | void clearUniqueKey(); |
| 191 | |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 192 | SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 193 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 194 | typedef GrSurfaceProxy INHERITED; |
| 195 | }; |
| 196 | |
| 197 | #endif |