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