| 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 | |
| 11 | #include "src/gpu/GrSamplerState.h" |
| 12 | #include "src/gpu/GrSurfaceProxy.h" |
| 13 | |
| 14 | class GrCaps; |
| 15 | class GrDeferredProxyUploader; |
| 16 | class GrProxyProvider; |
| 17 | class GrResourceProvider; |
| 18 | class GrTextureProxyPriv; |
| 19 | |
| 20 | // This class delays the acquisition of textures until they are actually required |
| 21 | class GrTextureProxy : virtual public GrSurfaceProxy { |
| 22 | public: |
| 23 | GrTextureProxy* asTextureProxy() override { return this; } |
| 24 | const GrTextureProxy* asTextureProxy() const override { return this; } |
| 25 | |
| 26 | // Actually instantiate the backing texture, if necessary |
| 27 | bool instantiate(GrResourceProvider*) override; |
| 28 | |
| 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; |
| 35 | |
| 36 | bool mipmapsAreDirty() const { |
| 37 | SkASSERT((GrMipmapped::kNo == fMipmapped) == |
| 38 | (GrMipmapStatus::kNotAllocated == fMipmapStatus)); |
| 39 | return GrMipmapped::kYes == fMipmapped && GrMipmapStatus::kValid != fMipmapStatus; |
| 40 | } |
| 41 | void markMipmapsDirty() { |
| 42 | SkASSERT(GrMipmapped::kYes == fMipmapped); |
| 43 | fMipmapStatus = GrMipmapStatus::kDirty; |
| 44 | } |
| 45 | void markMipmapsClean() { |
| 46 | SkASSERT(GrMipmapped::kYes == fMipmapped); |
| 47 | fMipmapStatus = GrMipmapStatus::kValid; |
| 48 | } |
| 49 | |
| 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 | |
| 54 | GrTextureType textureType() const { return this->backendFormat().textureType(); } |
| 55 | |
| 56 | /** If true then the texture does not support MIP maps and only supports clamp wrap mode. */ |
| 57 | bool hasRestrictedSampling() const { |
| 58 | return GrTextureTypeHasRestrictedSampling(this->textureType()); |
| 59 | } |
| 60 | |
| 61 | // Returns true if the passed in proxies can be used as dynamic state together when flushing |
| 62 | // draws to the gpu. This accepts GrSurfaceProxy since the information needed is defined on |
| 63 | // that type, but this function exists in GrTextureProxy because it's only relevant when the |
| 64 | // proxies are being used as textures. |
| 65 | static bool ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first, |
| 66 | const GrSurfaceProxy* second); |
| 67 | |
| 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 |
| 73 | if (this->isInstantiated() && fUniqueKey.isValid() && fSyncTargetKey && |
| 74 | fCreatingProvider == GrDDLProvider::kNo) { |
| 75 | GrSurface* surface = this->peekSurface(); |
| 76 | SkASSERT(surface); |
| 77 | |
| 78 | SkASSERT(surface->getUniqueKey().isValid()); |
| 79 | // It is possible for a non-keyed proxy to have a uniquely keyed resource assigned to |
| 80 | // it. This just means that a future user of the resource will be filling it with unique |
| 81 | // data. However, if the proxy has a unique key its attached resource should also |
| 82 | // have that key. |
| 83 | SkASSERT(fUniqueKey == surface->getUniqueKey()); |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | return fUniqueKey; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Internal-only helper class used for manipulations of the resource by the cache. |
| 92 | */ |
| 93 | class CacheAccess; |
| 94 | inline CacheAccess cacheAccess(); |
| 95 | inline const CacheAccess cacheAccess() const; // NOLINT(readability-const-return-type) |
| 96 | |
| 97 | // Provides access to special purpose functions. |
| 98 | GrTextureProxyPriv texPriv(); |
| 99 | const GrTextureProxyPriv texPriv() const; // NOLINT(readability-const-return-type) |
| 100 | |
| 101 | SkDEBUGCODE(GrDDLProvider creatingProvider() const { return fCreatingProvider; }) |
| 102 | |
| 103 | protected: |
| 104 | // DDL TODO: rm the GrSurfaceProxy friending |
| 105 | friend class GrSurfaceProxy; // for ctors |
| 106 | friend class GrProxyProvider; // for ctors |
| 107 | friend class GrTextureProxyPriv; |
| 108 | friend class GrSurfaceProxyPriv; // ability to change key sync state after lazy instantiation. |
| 109 | |
| 110 | // Deferred version - no data. |
| 111 | GrTextureProxy(const GrBackendFormat&, |
| 112 | SkISize, |
| 113 | GrMipmapped, |
| 114 | GrMipmapStatus, |
| 115 | SkBackingFit, |
| 116 | SkBudgeted, |
| 117 | GrProtected, |
| 118 | GrInternalSurfaceFlags, |
| 119 | UseAllocator, |
| 120 | GrDDLProvider creatingProvider); |
| 121 | |
| 122 | // Lazy-callback version |
| 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. |
| 132 | GrTextureProxy(LazyInstantiateCallback&&, |
| 133 | const GrBackendFormat&, |
| 134 | SkISize, |
| 135 | GrMipmapped, |
| 136 | GrMipmapStatus, |
| 137 | SkBackingFit, |
| 138 | SkBudgeted, |
| 139 | GrProtected, |
| 140 | GrInternalSurfaceFlags, |
| 141 | UseAllocator, |
| 142 | GrDDLProvider creatingProvider); |
| 143 | |
| 144 | // Wrapped version |
| 145 | GrTextureProxy(sk_sp<GrSurface>, UseAllocator, GrDDLProvider creatingProvider); |
| 146 | |
| 147 | ~GrTextureProxy() override; |
| 148 | |
| 149 | sk_sp<GrSurface> createSurface(GrResourceProvider*) const override; |
| 150 | |
| 151 | // By default uniqueKeys are propagated from a textureProxy to its backing GrTexture. |
| 152 | // Setting syncTargetKey to false disables this behavior and only keeps the unique key |
| 153 | // on the proxy. |
| 154 | void setTargetKeySync(bool sync) { fSyncTargetKey = sync; } |
| 155 | |
| 156 | private: |
| 157 | // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings |
| 158 | // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes |
| 159 | // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for |
| 160 | // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this' |
| 161 | // in the constructors, and always looks for the full 16 byte alignment, even if the fields in |
| 162 | // that particular class don't require it. Changing the size of this object can move the start |
| 163 | // address of other types, leading to this problem. |
| 164 | |
| 165 | GrMipmapped fMipmapped; |
| 166 | |
| 167 | // This tracks the mipmap status at the proxy level and is thus somewhat distinct from the |
| 168 | // backing GrTexture's mipmap status. In particular, this status is used to determine when |
| 169 | // mipmap levels need to be explicitly regenerated during the execution of a DAG of opsTasks. |
| 170 | GrMipmapStatus fMipmapStatus; |
| 171 | // TEMPORARY: We are in the process of moving GrMipmapStatus from the texture to the proxy. |
| 172 | // We track the fInitialMipmapStatus here so we can assert that the proxy did indeed expect |
| 173 | // the correct mipmap status immediately after instantiation. |
| 174 | // |
| 175 | // NOTE: fMipmapStatus may no longer be equal to fInitialMipmapStatus by the time the texture |
| 176 | // is instantiated, since it tracks mipmaps in the time frame in which the DAG is being built. |
| 177 | SkDEBUGCODE(const GrMipmapStatus fInitialMipmapStatus;) |
| 178 | |
| 179 | bool fSyncTargetKey = true; // Should target's unique key be sync'ed with ours. |
| 180 | |
| 181 | // For GrTextureProxies created in a DDL recording thread it is possible for the uniqueKey |
| 182 | // to be cleared on the backing GrTexture while the uniqueKey remains on the proxy. |
| 183 | // A fCreatingProvider of DDLProvider::kYes loosens up asserts that the key of an instantiated |
| 184 | // uniquely-keyed textureProxy is also always set on the backing GrTexture. |
| 185 | GrDDLProvider fCreatingProvider = GrDDLProvider::kNo; |
| 186 | |
| 187 | GrUniqueKey fUniqueKey; |
| 188 | GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid |
| 189 | |
| 190 | LazySurfaceDesc callbackDesc() const override; |
| 191 | |
| 192 | // Only used for proxies whose contents are being prepared on a worker thread. This object |
| 193 | // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that |
| 194 | // point, the proxy is instantiated, and this data is used to perform an ASAP upload. |
| 195 | std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader; |
| 196 | |
| 197 | size_t onUninstantiatedGpuMemorySize(const GrCaps&) const override; |
| 198 | |
| 199 | // Methods made available via GrTextureProxy::CacheAccess |
| 200 | void setUniqueKey(GrProxyProvider*, const GrUniqueKey&); |
| 201 | void clearUniqueKey(); |
| 202 | |
| 203 | SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;) |
| 204 | |
| 205 | typedef GrSurfaceProxy INHERITED; |
| 206 | }; |
| 207 | |
| 208 | #endif |
| 209 | |