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
14class GrCaps;
15class GrDeferredProxyUploader;
16class GrProxyProvider;
17class GrResourceProvider;
18class GrTextureProxyPriv;
19
20// This class delays the acquisition of textures until they are actually required
21class GrTextureProxy : virtual public GrSurfaceProxy {
22public:
23 GrTextureProxy* asTextureProxy() override { return this; }
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
36 bool mipMapsAreDirty() const {
37 SkASSERT((GrMipMapped::kNo == fMipMapped) ==
38 (GrMipMapsStatus::kNotAllocated == fMipMapsStatus));
39 return GrMipMapped::kYes == fMipMapped && GrMipMapsStatus::kValid != fMipMapsStatus;
40 }
41 void markMipMapsDirty() {
42 SkASSERT(GrMipMapped::kYes == fMipMapped);
43 fMipMapsStatus = GrMipMapsStatus::kDirty;
44 }
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.
52 GrMipMapped proxyMipMapped() const { return fMipMapped; }
53
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. */
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 */
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
103protected:
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 SkBackingFit,
116 SkBudgeted,
117 GrProtected,
118 GrInternalSurfaceFlags,
119 UseAllocator);
120
121 // Lazy-callback version
122 // There are two main use cases for lazily-instantiated proxies:
123 // basic knowledge - width, height, config, origin are known
124 // minimal knowledge - only config is known.
125 //
126 // The basic knowledge version is used for DDL where we know the type of proxy we are going to
127 // use, but we don't have access to the GPU yet to instantiate it.
128 //
129 // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not
130 // know the final size until flush time.
131 GrTextureProxy(LazyInstantiateCallback&&,
132 const GrBackendFormat&,
133 SkISize,
134 GrMipMapped,
135 GrMipMapsStatus,
136 SkBackingFit,
137 SkBudgeted,
138 GrProtected,
139 GrInternalSurfaceFlags,
140 UseAllocator);
141
142 // Wrapped version
143 GrTextureProxy(sk_sp<GrSurface>, UseAllocator);
144
145 ~GrTextureProxy() override;
146
147 sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
148
149 void setTargetKeySync(bool sync) { fSyncTargetKey = sync; }
150
151private:
152 // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings
153 // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes
154 // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for
155 // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this'
156 // in the constructors, and always looks for the full 16 byte alignment, even if the fields in
157 // that particular class don't require it. Changing the size of this object can move the start
158 // address of other types, leading to this problem.
159
160 GrMipMapped fMipMapped;
161
162 // This tracks the mipmap status at the proxy level and is thus somewhat distinct from the
163 // backing GrTexture's mipmap status. In particular, this status is used to determine when
164 // mipmap levels need to be explicitly regenerated during the execution of a DAG of opsTasks.
165 GrMipMapsStatus fMipMapsStatus;
166 // TEMPORARY: We are in the process of moving GrMipMapsStatus from the texture to the proxy.
167 // We track the fInitialMipMapsStatus here so we can assert that the proxy did indeed expect
168 // the correct mipmap status immediately after instantiation.
169 //
170 // NOTE: fMipMapsStatus may no longer be equal to fInitialMipMapsStatus by the time the texture
171 // is instantiated, since it tracks mipmaps in the time frame in which the DAG is being built.
172 SkDEBUGCODE(const GrMipMapsStatus fInitialMipMapsStatus;)
173
174 bool fSyncTargetKey = true; // Should target's unique key be sync'ed with ours.
175
176 GrUniqueKey fUniqueKey;
177 GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid
178
179 // Only used for proxies whose contents are being prepared on a worker thread. This object
180 // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that
181 // point, the proxy is instantiated, and this data is used to perform an ASAP upload.
182 std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader;
183
184 size_t onUninstantiatedGpuMemorySize(const GrCaps&) const override;
185
186 // Methods made available via GrTextureProxy::CacheAccess
187 void setUniqueKey(GrProxyProvider*, const GrUniqueKey&);
188 void clearUniqueKey();
189
190 SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
191
192 typedef GrSurfaceProxy INHERITED;
193};
194
195#endif
196