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 114 SkDEBUGCODE(GrDDLProvider creatingProvider() const { return fCreatingProvider; }) 115 116 protected: 117 // DDL TODO: rm the GrSurfaceProxy friending 118 friend class GrSurfaceProxy; // for ctors 119 friend class GrProxyProvider; // for ctors 120 friend class GrTextureProxyPriv; 121 friend class GrSurfaceProxyPriv; // ability to change key sync state after lazy instantiation. 122 123 // Deferred version - no data. 124 GrTextureProxy(const GrBackendFormat&, 125 SkISize, 126 skgpu::Mipmapped, 127 GrMipmapStatus, 128 SkBackingFit, 129 skgpu::Budgeted, 130 skgpu::Protected, 131 GrInternalSurfaceFlags, 132 UseAllocator, 133 GrDDLProvider creatingProvider, 134 std::string_view label); 135 136 // Lazy-callback version 137 // There are two main use cases for lazily-instantiated proxies: 138 // basic knowledge - width, height, config, origin are known 139 // minimal knowledge - only config is known. 140 // 141 // The basic knowledge version is used for DDL where we know the type of proxy we are going to 142 // use, but we don't have access to the GPU yet to instantiate it. 143 // 144 // The minimal knowledge version is used for when we are generating an atlas but we do not know 145 // the final size until we have finished adding to it. 146 GrTextureProxy(LazyInstantiateCallback&&, 147 const GrBackendFormat&, 148 SkISize, 149 skgpu::Mipmapped, 150 GrMipmapStatus, 151 SkBackingFit, 152 skgpu::Budgeted, 153 skgpu::Protected, 154 GrInternalSurfaceFlags, 155 UseAllocator, 156 GrDDLProvider creatingProvider, 157 std::string_view label); 158 159 // Wrapped version 160 GrTextureProxy(sk_sp<GrSurface>, 161 UseAllocator, 162 GrDDLProvider creatingProvider); 163 164 ~GrTextureProxy() override; 165 166 sk_sp<GrSurface> createSurface(GrResourceProvider*) const override; 167 168 // By default uniqueKeys are propagated from a textureProxy to its backing GrTexture. 169 // Setting syncTargetKey to false disables this behavior and only keeps the unique key 170 // on the proxy. setTargetKeySync(bool sync)171 void setTargetKeySync(bool sync) { fSyncTargetKey = sync; } 172 173 private: 174 // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings 175 // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes 176 // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for 177 // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this' 178 // in the constructors, and always looks for the full 16 byte alignment, even if the fields in 179 // that particular class don't require it. Changing the size of this object can move the start 180 // address of other types, leading to this problem. 181 182 skgpu::Mipmapped fMipmapped; 183 184 // This tracks the mipmap status at the proxy level and is thus somewhat distinct from the 185 // backing GrTexture's mipmap status. In particular, this status is used to determine when 186 // mipmap levels need to be explicitly regenerated during the execution of a DAG of opsTasks. 187 GrMipmapStatus fMipmapStatus; 188 // TEMPORARY: We are in the process of moving GrMipmapStatus from the texture to the proxy. 189 // We track the fInitialMipmapStatus here so we can assert that the proxy did indeed expect 190 // the correct mipmap status immediately after instantiation. 191 // 192 // NOTE: fMipmapStatus may no longer be equal to fInitialMipmapStatus by the time the texture 193 // is instantiated, since it tracks mipmaps in the time frame in which the DAG is being built. 194 SkDEBUGCODE(const GrMipmapStatus fInitialMipmapStatus;) 195 196 bool fSyncTargetKey = true; // Should target's unique key be sync'ed with ours. 197 198 // For GrTextureProxies created in a DDL recording thread it is possible for the uniqueKey 199 // to be cleared on the backing GrTexture while the uniqueKey remains on the proxy. 200 // A fCreatingProvider of DDLProvider::kYes loosens up asserts that the key of an instantiated 201 // uniquely-keyed textureProxy is also always set on the backing GrTexture. 202 GrDDLProvider fCreatingProvider = GrDDLProvider::kNo; 203 204 skgpu::UniqueKey fUniqueKey; 205 GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid 206 207 LazySurfaceDesc callbackDesc() const override; 208 209 // Only used for proxies whose contents are being prepared on a worker thread. This object 210 // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that 211 // point, the proxy is instantiated, and this data is used to perform an ASAP upload. 212 std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader; 213 214 size_t onUninstantiatedGpuMemorySize() const override; 215 216 // Methods made available via GrTextureProxy::CacheAccess 217 void setUniqueKey(GrProxyProvider*, const skgpu::UniqueKey&); 218 void clearUniqueKey(); 219 220 SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;) 221 222 using INHERITED = GrSurfaceProxy; 223 }; 224 225 #endif 226