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