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