blob: 986975b296b49f7b8016cdd7a8722a990b5f4dae [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
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
Brian Salomon201cdbb2019-08-14 17:00:30 -040011#include "src/gpu/GrSamplerState.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrSurfaceProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070013
Robert Phillips84a81202016-11-04 11:59:10 -040014class GrCaps;
Brian Osman099fa0f2017-10-02 16:38:32 -040015class GrDeferredProxyUploader;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050016class GrProxyProvider;
Brian Osman32342f02017-03-04 08:12:46 -050017class GrResourceProvider;
Robert Phillips420c4cf2017-09-28 09:00:45 -040018class GrTextureProxyPriv;
robertphillips76948d42016-05-04 12:47:41 -070019
20// This class delays the acquisition of textures until they are actually required
Robert Phillips84a81202016-11-04 11:59:10 -040021class GrTextureProxy : virtual public GrSurfaceProxy {
robertphillips76948d42016-05-04 12:47:41 -070022public:
robertphillips76948d42016-05-04 12:47:41 -070023 GrTextureProxy* asTextureProxy() override { return this; }
24 const GrTextureProxy* asTextureProxy() const override { return this; }
25
26 // Actually instantiate the backing texture, if necessary
Robert Phillips10d17212019-04-24 14:09:10 -040027 bool instantiate(GrResourceProvider*) override;
robertphillips76948d42016-05-04 12:47:41 -070028
Brian Salomon2bbdcc42017-09-07 12:36:34 -040029 GrSamplerState::Filter highestFilterMode() const;
Robert Phillips49081d12017-05-08 13:41:35 -040030
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050031 // If we are instantiated and have a target, return the mip state of that target. Otherwise
32 // returns the proxy's mip state from creation time. This is useful for lazy proxies which may
33 // claim to not need mips at creation time, but the instantiation happens to give us a mipped
34 // target. In that case we should use that for our benefit to avoid possible copies/mip
35 // generation later.
36 GrMipMapped mipMapped() const;
Brian Salomonbb5711a2017-05-17 13:49:59 -040037
Chris Dalton95d8ceb2019-07-30 11:17:59 -060038 bool mipMapsAreDirty() const {
39 SkASSERT((GrMipMapped::kNo == fMipMapped) ==
40 (GrMipMapsStatus::kNotAllocated == fMipMapsStatus));
41 return GrMipMapped::kYes == fMipMapped && GrMipMapsStatus::kValid != fMipMapsStatus;
42 }
Chris Dalton3d770272019-08-14 09:24:37 -060043 void markMipMapsDirty() {
44 SkASSERT(GrMipMapped::kYes == fMipMapped);
45 fMipMapsStatus = GrMipMapsStatus::kDirty;
46 }
47 void markMipMapsClean() {
48 SkASSERT(GrMipMapped::kYes == fMipMapped);
49 fMipMapsStatus = GrMipMapsStatus::kValid;
50 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -060051
Brian Salomonfd98c2c2018-07-31 17:25:29 -040052 // Returns the GrMipMapped value of the proxy from creation time regardless of whether it has
53 // been instantiated or not.
54 GrMipMapped proxyMipMapped() const { return fMipMapped; }
55
Brian Salomon7da2fc72018-12-10 13:40:07 -050056 GrTextureType textureType() const { return this->backendFormat().textureType(); }
57
Brian Salomonfd98c2c2018-07-31 17:25:29 -040058 /** If true then the texture does not support MIP maps and only supports clamp wrap mode. */
Greg Daniel4065d452018-11-16 15:43:41 -050059 bool hasRestrictedSampling() const {
60 return GrTextureTypeHasRestrictedSampling(this->textureType());
61 }
Greg Daniel45723ac2018-11-30 10:12:43 -050062
63 // Returns true if the passed in proxies can be used as dynamic state together when flushing
64 // draws to the gpu.
65 static bool ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first,
66 const GrTextureProxy* second);
67
Robert Phillipsae7d3f32017-09-21 08:26:08 -040068 /**
69 * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one.
70 */
71 const GrUniqueKey& getUniqueKey() const {
72#ifdef SK_DEBUG
Robert Phillipsb5204762019-06-19 14:12:13 -040073 if (this->isInstantiated() && fUniqueKey.isValid() && fSyncTargetKey) {
74 GrSurface* surface = this->peekSurface();
75 SkASSERT(surface);
76
77 SkASSERT(surface->getUniqueKey().isValid());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040078 // It is possible for a non-keyed proxy to have a uniquely keyed resource assigned to
79 // it. This just means that a future user of the resource will be filling it with unique
80 // data. However, if the proxy has a unique key its attached resource should also
81 // have that key.
Robert Phillipsb5204762019-06-19 14:12:13 -040082 SkASSERT(fUniqueKey == surface->getUniqueKey());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040083 }
84#endif
85
86 return fUniqueKey;
87 }
88
89 /**
90 * Internal-only helper class used for manipulations of the resource by the cache.
91 */
92 class CacheAccess;
93 inline CacheAccess cacheAccess();
94 inline const CacheAccess cacheAccess() const;
95
Robert Phillips420c4cf2017-09-28 09:00:45 -040096 // Provides access to special purpose functions.
97 GrTextureProxyPriv texPriv();
98 const GrTextureProxyPriv texPriv() const;
99
Robert Phillips84a81202016-11-04 11:59:10 -0400100protected:
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500101 // DDL TODO: rm the GrSurfaceProxy friending
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400102 friend class GrSurfaceProxy; // for ctors
103 friend class GrProxyProvider; // for ctors
Brian Osman099fa0f2017-10-02 16:38:32 -0400104 friend class GrTextureProxyPriv;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400105 friend class GrSurfaceProxyPriv; // ability to change key sync state after lazy instantiation.
Robert Phillips37430132016-11-09 06:50:43 -0500106
Brian Salomon58389b92018-03-07 13:01:25 -0500107 // Deferred version - no data.
Greg Daniel4065d452018-11-16 15:43:41 -0500108 GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrSurfaceOrigin,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600109 GrMipMapped, GrMipMapsStatus, const GrSwizzle& textureSwizzle, SkBackingFit,
110 SkBudgeted, GrProtected, GrInternalSurfaceFlags);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700111
112 // Lazy-callback version
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500113 // There are two main use cases for lazily-instantiated proxies:
114 // basic knowledge - width, height, config, origin are known
115 // minimal knowledge - only config is known.
116 //
117 // The basic knowledge version is used for DDL where we know the type of proxy we are going to
118 // use, but we don't have access to the GPU yet to instantiate it.
119 //
120 // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not
121 // know the final size until flush time.
Greg Daniel4065d452018-11-16 15:43:41 -0500122 GrTextureProxy(LazyInstantiateCallback&&, LazyInstantiationType, const GrBackendFormat&,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600123 const GrSurfaceDesc& desc, GrSurfaceOrigin, GrMipMapped, GrMipMapsStatus,
124 const GrSwizzle&, SkBackingFit, SkBudgeted, GrProtected, GrInternalSurfaceFlags);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700125
robertphillips76948d42016-05-04 12:47:41 -0700126 // Wrapped version
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400127 GrTextureProxy(sk_sp<GrSurface>, GrSurfaceOrigin, const GrSwizzle&);
robertphillips76948d42016-05-04 12:47:41 -0700128
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400129 ~GrTextureProxy() override;
130
Robert Phillips5af44de2017-07-18 14:49:38 -0400131 sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
132
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400133 void setTargetKeySync(bool sync) { fSyncTargetKey = sync; }
134
Robert Phillips84a81202016-11-04 11:59:10 -0400135private:
Brian Osman086679b2018-09-10 16:26:51 -0400136 // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings
137 // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes
138 // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for
139 // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this'
140 // in the constructors, and always looks for the full 16 byte alignment, even if the fields in
141 // that particular class don't require it. Changing the size of this object can move the start
142 // address of other types, leading to this problem.
143
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400144 GrMipMapped fMipMapped;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600145
146 // This tracks the mipmap status at the proxy level and is thus somewhat distinct from the
147 // backing GrTexture's mipmap status. In particular, this status is used to determine when
148 // mipmap levels need to be explicitly regenerated during the execution of a DAG of opLists.
149 GrMipMapsStatus fMipMapsStatus;
150 // TEMPORARY: We are in the process of moving GrMipMapsStatus from the texture to the proxy.
151 // We track the fInitialMipMapsStatus here so we can assert that the proxy did indeed expect
152 // the correct mipmap status immediately after instantiation.
153 //
154 // NOTE: fMipMapsStatus may no longer be equal to fInitialMipMapsStatus by the time the texture
155 // is instantiated, since it tracks mipmaps in the time frame in which the DAG is being built.
156 SkDEBUGCODE(const GrMipMapsStatus fInitialMipMapsStatus);
157
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400158 bool fSyncTargetKey = true; // Should target's unique key be sync'ed with ours.
Brian Salomonbb5711a2017-05-17 13:49:59 -0400159
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500160 GrUniqueKey fUniqueKey;
161 GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400162
Brian Osman099fa0f2017-10-02 16:38:32 -0400163 // Only used for proxies whose contents are being prepared on a worker thread. This object
164 // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that
165 // point, the proxy is instantiated, and this data is used to perform an ASAP upload.
166 std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader;
167
Brian Salomonbb5711a2017-05-17 13:49:59 -0400168 size_t onUninstantiatedGpuMemorySize() const override;
Robert Phillips8bc06d02016-11-01 17:28:40 -0400169
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400170 // Methods made available via GrTextureProxy::CacheAccess
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500171 void setUniqueKey(GrProxyProvider*, const GrUniqueKey&);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400172 void clearUniqueKey();
173
Greg Daniel849dce12018-04-24 14:32:53 -0400174 SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700175
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400176 // For wrapped proxies the GrTexture pointer is stored in GrIORefProxy.
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400177 // For deferred proxies that pointer will be filled in when we need to instantiate
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400178 // the deferred resource
robertphillips76948d42016-05-04 12:47:41 -0700179
180 typedef GrSurfaceProxy INHERITED;
181};
182
183#endif