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 "GrSamplerState.h"
12 #include "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 
39     // Returns the GrMipMapped value of the proxy from creation time regardless of whether it has
40     // been instantiated or not.
proxyMipMapped()41     GrMipMapped proxyMipMapped() const { return fMipMapped; }
42 
textureType()43     GrTextureType textureType() const { return this->backendFormat().textureType(); }
44 
45     /** If true then the texture does not support MIP maps and only supports clamp wrap mode. */
hasRestrictedSampling()46     bool hasRestrictedSampling() const {
47         return GrTextureTypeHasRestrictedSampling(this->textureType());
48     }
49 
50     // Returns true if the passed in proxies can be used as dynamic state together when flushing
51     // draws to the gpu.
52     static bool ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first,
53                                                    const GrTextureProxy* second);
54 
55     /**
56      * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one.
57      */
getUniqueKey()58     const GrUniqueKey& getUniqueKey() const {
59 #ifdef SK_DEBUG
60         if (fTarget && fUniqueKey.isValid()) {
61             SkASSERT(fTarget->getUniqueKey().isValid());
62             // It is possible for a non-keyed proxy to have a uniquely keyed resource assigned to
63             // it. This just means that a future user of the resource will be filling it with unique
64             // data. However, if the proxy has a unique key its attached resource should also
65             // have that key.
66             SkASSERT(fUniqueKey == fTarget->getUniqueKey());
67         }
68 #endif
69 
70         return fUniqueKey;
71     }
72 
73     /**
74      * Internal-only helper class used for manipulations of the resource by the cache.
75      */
76     class CacheAccess;
77     inline CacheAccess cacheAccess();
78     inline const CacheAccess cacheAccess() const;
79 
80     // Provides access to special purpose functions.
81     GrTextureProxyPriv texPriv();
82     const GrTextureProxyPriv texPriv() const;
83 
84 protected:
85     // DDL TODO: rm the GrSurfaceProxy friending
86     friend class GrSurfaceProxy; // for ctors
87     friend class GrProxyProvider; // for ctors
88     friend class GrTextureProxyPriv;
89 
90     // Deferred version - when constructed with data the origin is always kTopLeft.
91     GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrMipMapped, SkBackingFit,
92                    SkBudgeted, const void* srcData, size_t srcRowBytes, GrInternalSurfaceFlags);
93 
94     // Deferred version - no data.
95     GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrSurfaceOrigin,
96                    GrMipMapped, SkBackingFit, SkBudgeted, GrInternalSurfaceFlags);
97 
98     // Lazy-callback version
99     // There are two main use cases for lazily-instantiated proxies:
100     //   basic knowledge - width, height, config, origin are known
101     //   minimal knowledge - only config is known.
102     //
103     // The basic knowledge version is used for DDL where we know the type of proxy we are going to
104     // use, but we don't have access to the GPU yet to instantiate it.
105     //
106     // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not
107     // know the final size until flush time.
108     GrTextureProxy(LazyInstantiateCallback&&, LazyInstantiationType, const GrBackendFormat&,
109                    const GrSurfaceDesc& desc, GrSurfaceOrigin, GrMipMapped, SkBackingFit,
110                    SkBudgeted, GrInternalSurfaceFlags);
111 
112     // Wrapped version
113     GrTextureProxy(sk_sp<GrSurface>, GrSurfaceOrigin);
114 
115     ~GrTextureProxy() override;
116 
117     sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
118 
119 private:
120     // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings
121     // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes
122     // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for
123     // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this'
124     // in the constructors, and always looks for the full 16 byte alignment, even if the fields in
125     // that particular class don't require it. Changing the size of this object can move the start
126     // address of other types, leading to this problem.
127 
128     GrMipMapped      fMipMapped;
129 
130     GrUniqueKey      fUniqueKey;
131     GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid
132 
133     // Only used for proxies whose contents are being prepared on a worker thread. This object
134     // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that
135     // point, the proxy is instantiated, and this data is used to perform an ASAP upload.
136     std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader;
137 
138     size_t onUninstantiatedGpuMemorySize() const override;
139 
140     // Methods made available via GrTextureProxy::CacheAccess
141     void setUniqueKey(GrProxyProvider*, const GrUniqueKey&);
142     void clearUniqueKey();
143 
144     SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
145 
146     // For wrapped proxies the GrTexture pointer is stored in GrIORefProxy.
147     // For deferred proxies that pointer will be filled in when we need to instantiate
148     // the deferred resource
149 
150     typedef GrSurfaceProxy INHERITED;
151 };
152 
153 #endif
154