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