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 // 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 mipMapsAreDirty()36 bool mipMapsAreDirty() const { 37 SkASSERT((GrMipMapped::kNo == fMipMapped) == 38 (GrMipMapsStatus::kNotAllocated == fMipMapsStatus)); 39 return GrMipMapped::kYes == fMipMapped && GrMipMapsStatus::kValid != fMipMapsStatus; 40 } markMipMapsDirty()41 void markMipMapsDirty() { 42 SkASSERT(GrMipMapped::kYes == fMipMapped); 43 fMipMapsStatus = GrMipMapsStatus::kDirty; 44 } markMipMapsClean()45 void markMipMapsClean() { 46 SkASSERT(GrMipMapped::kYes == fMipMapped); 47 fMipMapsStatus = GrMipMapsStatus::kValid; 48 } 49 50 // Returns the GrMipMapped value of the proxy from creation time regardless of whether it has 51 // been instantiated or not. proxyMipMapped()52 GrMipMapped proxyMipMapped() const { return fMipMapped; } 53 textureType()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. */ hasRestrictedSampling()57 bool hasRestrictedSampling() const { 58 return GrTextureTypeHasRestrictedSampling(this->textureType()); 59 } 60 61 // Returns the highest allowed filter mode for a given texture type 62 static GrSamplerState::Filter HighestFilterMode(const GrTextureType textureType); 63 64 // Returns true if the passed in proxies can be used as dynamic state together when flushing 65 // draws to the gpu. This accepts GrSurfaceProxy since the information needed is defined on 66 // that type, but this function exists in GrTextureProxy because it's only relevant when the 67 // proxies are being used as textures. 68 static bool ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first, 69 const GrSurfaceProxy* second); 70 71 /** 72 * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one. 73 */ getUniqueKey()74 const GrUniqueKey& getUniqueKey() const { 75 #ifdef SK_DEBUG 76 if (this->isInstantiated() && fUniqueKey.isValid() && fSyncTargetKey) { 77 GrSurface* surface = this->peekSurface(); 78 SkASSERT(surface); 79 80 SkASSERT(surface->getUniqueKey().isValid()); 81 // It is possible for a non-keyed proxy to have a uniquely keyed resource assigned to 82 // it. This just means that a future user of the resource will be filling it with unique 83 // data. However, if the proxy has a unique key its attached resource should also 84 // have that key. 85 SkASSERT(fUniqueKey == surface->getUniqueKey()); 86 } 87 #endif 88 89 return fUniqueKey; 90 } 91 92 /** 93 * Internal-only helper class used for manipulations of the resource by the cache. 94 */ 95 class CacheAccess; 96 inline CacheAccess cacheAccess(); 97 inline const CacheAccess cacheAccess() const; 98 99 // Provides access to special purpose functions. 100 GrTextureProxyPriv texPriv(); 101 const GrTextureProxyPriv texPriv() const; 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 GrMipMapsStatus, 115 const GrSwizzle& textureSwizzle, 116 SkBackingFit, 117 SkBudgeted, 118 GrProtected, 119 GrInternalSurfaceFlags, 120 UseAllocator); 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 GrMipMapsStatus, 137 const GrSwizzle& textureSwizzle, 138 SkBackingFit, 139 SkBudgeted, 140 GrProtected, 141 GrInternalSurfaceFlags, 142 UseAllocator); 143 144 // Wrapped version 145 GrTextureProxy(sk_sp<GrSurface>, const GrSwizzle&, UseAllocator); 146 147 ~GrTextureProxy() override; 148 149 sk_sp<GrSurface> createSurface(GrResourceProvider*) const override; 150 setTargetKeySync(bool sync)151 void setTargetKeySync(bool sync) { fSyncTargetKey = sync; } 152 153 private: 154 // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings 155 // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes 156 // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for 157 // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this' 158 // in the constructors, and always looks for the full 16 byte alignment, even if the fields in 159 // that particular class don't require it. Changing the size of this object can move the start 160 // address of other types, leading to this problem. 161 162 GrMipMapped fMipMapped; 163 164 // This tracks the mipmap status at the proxy level and is thus somewhat distinct from the 165 // backing GrTexture's mipmap status. In particular, this status is used to determine when 166 // mipmap levels need to be explicitly regenerated during the execution of a DAG of opsTasks. 167 GrMipMapsStatus fMipMapsStatus; 168 // TEMPORARY: We are in the process of moving GrMipMapsStatus from the texture to the proxy. 169 // We track the fInitialMipMapsStatus here so we can assert that the proxy did indeed expect 170 // the correct mipmap status immediately after instantiation. 171 // 172 // NOTE: fMipMapsStatus may no longer be equal to fInitialMipMapsStatus by the time the texture 173 // is instantiated, since it tracks mipmaps in the time frame in which the DAG is being built. 174 SkDEBUGCODE(const GrMipMapsStatus fInitialMipMapsStatus;) 175 176 bool fSyncTargetKey = true; // Should target's unique key be sync'ed with ours. 177 178 GrUniqueKey fUniqueKey; 179 GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid 180 181 // Only used for proxies whose contents are being prepared on a worker thread. This object 182 // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that 183 // point, the proxy is instantiated, and this data is used to perform an ASAP upload. 184 std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader; 185 186 size_t onUninstantiatedGpuMemorySize(const GrCaps&) const override; 187 188 // Methods made available via GrTextureProxy::CacheAccess 189 void setUniqueKey(GrProxyProvider*, const GrUniqueKey&); 190 void clearUniqueKey(); 191 192 SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;) 193 194 typedef GrSurfaceProxy INHERITED; 195 }; 196 197 #endif 198