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; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 15 | class GrProxyProvider; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 16 | class GrResourceProvider; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 17 | |
| 18 | // This class delays the acquisition of textures until they are actually required |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 19 | class GrTextureProxy : virtual public GrSurfaceProxy { |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 20 | public: |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 21 | GrTextureProxy* asTextureProxy() override { return this; } |
| 22 | const GrTextureProxy* asTextureProxy() const override { return this; } |
| 23 | |
| 24 | // Actually instantiate the backing texture, if necessary |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 25 | bool instantiate(GrResourceProvider*) override; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 26 | |
Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 27 | // If we are instantiated and have a target, return the mip state of that target. Otherwise |
| 28 | // returns the proxy's mip state from creation time. This is useful for lazy proxies which may |
| 29 | // claim to not need mips at creation time, but the instantiation happens to give us a mipped |
| 30 | // target. In that case we should use that for our benefit to avoid possible copies/mip |
| 31 | // generation later. |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 32 | GrMipmapped mipmapped() const; |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 33 | |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 34 | bool mipmapsAreDirty() const { |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 35 | SkASSERT((GrMipmapped::kNo == fMipmapped) == |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 36 | (GrMipmapStatus::kNotAllocated == fMipmapStatus)); |
| 37 | return GrMipmapped::kYes == fMipmapped && GrMipmapStatus::kValid != fMipmapStatus; |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 38 | } |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 39 | void markMipmapsDirty() { |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 40 | SkASSERT(GrMipmapped::kYes == fMipmapped); |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 41 | fMipmapStatus = GrMipmapStatus::kDirty; |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 42 | } |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 43 | void markMipmapsClean() { |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 44 | SkASSERT(GrMipmapped::kYes == fMipmapped); |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 45 | fMipmapStatus = GrMipmapStatus::kValid; |
Chris Dalton | 3d77027 | 2019-08-14 09:24:37 -0600 | [diff] [blame] | 46 | } |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 47 | |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 48 | // Returns the GrMipmapped value of the proxy from creation time regardless of whether it has |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 49 | // been instantiated or not. |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 50 | GrMipmapped proxyMipmapped() const { return fMipmapped; } |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 51 | |
Brian Salomon | 7da2fc7 | 2018-12-10 13:40:07 -0500 | [diff] [blame] | 52 | GrTextureType textureType() const { return this->backendFormat().textureType(); } |
| 53 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 54 | /** 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] | 55 | bool hasRestrictedSampling() const { |
| 56 | return GrTextureTypeHasRestrictedSampling(this->textureType()); |
| 57 | } |
Greg Daniel | 45723ac | 2018-11-30 10:12:43 -0500 | [diff] [blame] | 58 | |
| 59 | // 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] | 60 | // draws to the gpu. This accepts GrSurfaceProxy since the information needed is defined on |
| 61 | // that type, but this function exists in GrTextureProxy because it's only relevant when the |
| 62 | // proxies are being used as textures. |
| 63 | static bool ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first, |
| 64 | const GrSurfaceProxy* second); |
Greg Daniel | 45723ac | 2018-11-30 10:12:43 -0500 | [diff] [blame] | 65 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 66 | /** |
| 67 | * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one. |
| 68 | */ |
| 69 | const GrUniqueKey& getUniqueKey() const { |
| 70 | #ifdef SK_DEBUG |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 71 | if (this->isInstantiated() && fUniqueKey.isValid() && fSyncTargetKey && |
| 72 | fCreatingProvider == GrDDLProvider::kNo) { |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 73 | GrSurface* surface = this->peekSurface(); |
| 74 | SkASSERT(surface); |
| 75 | |
| 76 | SkASSERT(surface->getUniqueKey().isValid()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 77 | // It is possible for a non-keyed proxy to have a uniquely keyed resource assigned to |
| 78 | // it. This just means that a future user of the resource will be filling it with unique |
| 79 | // data. However, if the proxy has a unique key its attached resource should also |
| 80 | // have that key. |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 81 | SkASSERT(fUniqueKey == surface->getUniqueKey()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 82 | } |
| 83 | #endif |
| 84 | |
| 85 | return fUniqueKey; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Internal-only helper class used for manipulations of the resource by the cache. |
| 90 | */ |
| 91 | class CacheAccess; |
| 92 | inline CacheAccess cacheAccess(); |
John Stiles | ec9b4aa | 2020-08-07 13:05:14 -0400 | [diff] [blame] | 93 | inline const CacheAccess cacheAccess() const; // NOLINT(readability-const-return-type) |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 94 | |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 95 | SkDEBUGCODE(GrDDLProvider creatingProvider() const { return fCreatingProvider; }) |
| 96 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 97 | protected: |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 98 | // DDL TODO: rm the GrSurfaceProxy friending |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 99 | friend class GrSurfaceProxy; // for ctors |
| 100 | friend class GrProxyProvider; // for ctors |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 101 | friend class GrSurfaceProxyPriv; // ability to change key sync state after lazy instantiation. |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 102 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 103 | // Deferred version - no data. |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 104 | GrTextureProxy(const GrBackendFormat&, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 105 | SkISize, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 106 | GrMipmapped, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 107 | GrMipmapStatus, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 108 | SkBackingFit, |
| 109 | SkBudgeted, |
| 110 | GrProtected, |
| 111 | GrInternalSurfaceFlags, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 112 | UseAllocator, |
| 113 | GrDDLProvider creatingProvider); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 114 | |
| 115 | // Lazy-callback version |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 116 | // There are two main use cases for lazily-instantiated proxies: |
| 117 | // basic knowledge - width, height, config, origin are known |
| 118 | // minimal knowledge - only config is known. |
| 119 | // |
| 120 | // The basic knowledge version is used for DDL where we know the type of proxy we are going to |
| 121 | // use, but we don't have access to the GPU yet to instantiate it. |
| 122 | // |
| 123 | // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not |
| 124 | // know the final size until flush time. |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 125 | GrTextureProxy(LazyInstantiateCallback&&, |
| 126 | const GrBackendFormat&, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 127 | SkISize, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 128 | GrMipmapped, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 129 | GrMipmapStatus, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 130 | SkBackingFit, |
| 131 | SkBudgeted, |
| 132 | GrProtected, |
| 133 | GrInternalSurfaceFlags, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 134 | UseAllocator, |
| 135 | GrDDLProvider creatingProvider); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 136 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 137 | // Wrapped version |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 138 | GrTextureProxy(sk_sp<GrSurface>, UseAllocator, GrDDLProvider creatingProvider); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 139 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 140 | ~GrTextureProxy() override; |
| 141 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 142 | sk_sp<GrSurface> createSurface(GrResourceProvider*) const override; |
| 143 | |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 144 | // By default uniqueKeys are propagated from a textureProxy to its backing GrTexture. |
| 145 | // Setting syncTargetKey to false disables this behavior and only keeps the unique key |
| 146 | // on the proxy. |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 147 | void setTargetKeySync(bool sync) { fSyncTargetKey = sync; } |
| 148 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 149 | private: |
Brian Osman | 086679b | 2018-09-10 16:26:51 -0400 | [diff] [blame] | 150 | // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings |
| 151 | // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes |
| 152 | // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for |
| 153 | // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this' |
| 154 | // in the constructors, and always looks for the full 16 byte alignment, even if the fields in |
| 155 | // that particular class don't require it. Changing the size of this object can move the start |
| 156 | // address of other types, leading to this problem. |
| 157 | |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 158 | GrMipmapped fMipmapped; |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 159 | |
| 160 | // This tracks the mipmap status at the proxy level and is thus somewhat distinct from the |
| 161 | // 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] | 162 | // mipmap levels need to be explicitly regenerated during the execution of a DAG of opsTasks. |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 163 | GrMipmapStatus fMipmapStatus; |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 164 | // TEMPORARY: We are in the process of moving GrMipmapStatus from the texture to the proxy. |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 165 | // We track the fInitialMipmapStatus here so we can assert that the proxy did indeed expect |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 166 | // the correct mipmap status immediately after instantiation. |
| 167 | // |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 168 | // NOTE: fMipmapStatus may no longer be equal to fInitialMipmapStatus by the time the texture |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 169 | // is instantiated, since it tracks mipmaps in the time frame in which the DAG is being built. |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 170 | SkDEBUGCODE(const GrMipmapStatus fInitialMipmapStatus;) |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 171 | |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 172 | 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] | 173 | |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 174 | // For GrTextureProxies created in a DDL recording thread it is possible for the uniqueKey |
| 175 | // to be cleared on the backing GrTexture while the uniqueKey remains on the proxy. |
| 176 | // A fCreatingProvider of DDLProvider::kYes loosens up asserts that the key of an instantiated |
| 177 | // uniquely-keyed textureProxy is also always set on the backing GrTexture. |
| 178 | GrDDLProvider fCreatingProvider = GrDDLProvider::kNo; |
| 179 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 180 | GrUniqueKey fUniqueKey; |
| 181 | GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 182 | |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 183 | LazySurfaceDesc callbackDesc() const override; |
| 184 | |
Greg Daniel | 0eca74c | 2020-10-01 13:46:00 -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 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 193 | using INHERITED = GrSurfaceProxy; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | #endif |