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