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