blob: f0605557064cf32648147b8e6876fdc8e2ad8eae [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 Salomon10461f32021-06-02 10:49:10 -040011#include "include/gpu/GrBackendSurface.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040012#include "src/gpu/GrSamplerState.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrSurfaceProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070014
Robert Phillips84a81202016-11-04 11:59:10 -040015class GrCaps;
Adlai Holler9e2c50e2021-02-09 14:41:52 -050016class GrDeferredProxyUploader;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050017class GrProxyProvider;
Brian Osman32342f02017-03-04 08:12:46 -050018class GrResourceProvider;
Adlai Holler9e2c50e2021-02-09 14:41:52 -050019class GrTextureProxyPriv;
robertphillips76948d42016-05-04 12:47:41 -070020
21// This class delays the acquisition of textures until they are actually required
Robert Phillips84a81202016-11-04 11:59:10 -040022class GrTextureProxy : virtual public GrSurfaceProxy {
robertphillips76948d42016-05-04 12:47:41 -070023public:
robertphillips76948d42016-05-04 12:47:41 -070024 GrTextureProxy* asTextureProxy() override { return this; }
25 const GrTextureProxy* asTextureProxy() const override { return this; }
26
27 // Actually instantiate the backing texture, if necessary
Robert Phillips10d17212019-04-24 14:09:10 -040028 bool instantiate(GrResourceProvider*) override;
robertphillips76948d42016-05-04 12:47:41 -070029
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050030 // 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.
Brian Salomon8c82a872020-07-21 12:09:58 -040035 GrMipmapped mipmapped() const;
Brian Salomonbb5711a2017-05-17 13:49:59 -040036
Brian Salomon8c82a872020-07-21 12:09:58 -040037 bool mipmapsAreDirty() const {
Brian Salomon40a40622020-07-21 10:32:07 -040038 SkASSERT((GrMipmapped::kNo == fMipmapped) ==
Brian Salomon8c82a872020-07-21 12:09:58 -040039 (GrMipmapStatus::kNotAllocated == fMipmapStatus));
40 return GrMipmapped::kYes == fMipmapped && GrMipmapStatus::kValid != fMipmapStatus;
Chris Dalton95d8ceb2019-07-30 11:17:59 -060041 }
Brian Salomon8c82a872020-07-21 12:09:58 -040042 void markMipmapsDirty() {
Brian Salomon40a40622020-07-21 10:32:07 -040043 SkASSERT(GrMipmapped::kYes == fMipmapped);
Brian Salomon8c82a872020-07-21 12:09:58 -040044 fMipmapStatus = GrMipmapStatus::kDirty;
Chris Dalton3d770272019-08-14 09:24:37 -060045 }
Brian Salomon8c82a872020-07-21 12:09:58 -040046 void markMipmapsClean() {
Brian Salomon40a40622020-07-21 10:32:07 -040047 SkASSERT(GrMipmapped::kYes == fMipmapped);
Brian Salomon8c82a872020-07-21 12:09:58 -040048 fMipmapStatus = GrMipmapStatus::kValid;
Chris Dalton3d770272019-08-14 09:24:37 -060049 }
Chris Dalton95d8ceb2019-07-30 11:17:59 -060050
Brian Salomon7e67dca2020-07-21 09:27:25 -040051 // Returns the GrMipmapped value of the proxy from creation time regardless of whether it has
Brian Salomonfd98c2c2018-07-31 17:25:29 -040052 // been instantiated or not.
Brian Salomon8c82a872020-07-21 12:09:58 -040053 GrMipmapped proxyMipmapped() const { return fMipmapped; }
Brian Salomonfd98c2c2018-07-31 17:25:29 -040054
Brian Salomon7da2fc72018-12-10 13:40:07 -050055 GrTextureType textureType() const { return this->backendFormat().textureType(); }
56
Brian Salomonfd98c2c2018-07-31 17:25:29 -040057 /** If true then the texture does not support MIP maps and only supports clamp wrap mode. */
Greg Daniel4065d452018-11-16 15:43:41 -050058 bool hasRestrictedSampling() const {
59 return GrTextureTypeHasRestrictedSampling(this->textureType());
60 }
Greg Daniel45723ac2018-11-30 10:12:43 -050061
62 // Returns true if the passed in proxies can be used as dynamic state together when flushing
Michael Ludwigfcdd0612019-11-25 08:34:31 -050063 // 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);
Greg Daniel45723ac2018-11-30 10:12:43 -050068
Robert Phillipsae7d3f32017-09-21 08:26:08 -040069 /**
70 * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one.
71 */
Adlai Hollercc119d92021-03-16 15:17:25 -040072 const GrUniqueKey& getUniqueKey() const override {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040073#ifdef SK_DEBUG
Robert Phillipsf10b2a52020-05-15 10:20:04 -040074 if (this->isInstantiated() && fUniqueKey.isValid() && fSyncTargetKey &&
75 fCreatingProvider == GrDDLProvider::kNo) {
Robert Phillipsb5204762019-06-19 14:12:13 -040076 GrSurface* surface = this->peekSurface();
77 SkASSERT(surface);
78
79 SkASSERT(surface->getUniqueKey().isValid());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040080 // 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.
Robert Phillipsb5204762019-06-19 14:12:13 -040084 SkASSERT(fUniqueKey == surface->getUniqueKey());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040085 }
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();
John Stilesec9b4aa2020-08-07 13:05:14 -040096 inline const CacheAccess cacheAccess() const; // NOLINT(readability-const-return-type)
Robert Phillipsae7d3f32017-09-21 08:26:08 -040097
Adlai Holler9e2c50e2021-02-09 14:41:52 -050098 // Provides access to special purpose functions.
99 GrTextureProxyPriv texPriv();
100 const GrTextureProxyPriv texPriv() const; // NOLINT(readability-const-return-type)
101
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400102 SkDEBUGCODE(GrDDLProvider creatingProvider() const { return fCreatingProvider; })
103
Robert Phillips84a81202016-11-04 11:59:10 -0400104protected:
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500105 // DDL TODO: rm the GrSurfaceProxy friending
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400106 friend class GrSurfaceProxy; // for ctors
107 friend class GrProxyProvider; // for ctors
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500108 friend class GrTextureProxyPriv;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400109 friend class GrSurfaceProxyPriv; // ability to change key sync state after lazy instantiation.
Robert Phillips37430132016-11-09 06:50:43 -0500110
Brian Salomon58389b92018-03-07 13:01:25 -0500111 // Deferred version - no data.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400112 GrTextureProxy(const GrBackendFormat&,
Brian Salomona56a7462020-02-07 14:17:25 -0500113 SkISize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400114 GrMipmapped,
Brian Salomona6db5102020-07-21 09:56:23 -0400115 GrMipmapStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400116 SkBackingFit,
117 SkBudgeted,
118 GrProtected,
119 GrInternalSurfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400120 UseAllocator,
121 GrDDLProvider creatingProvider);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700122
123 // Lazy-callback version
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500124 // There are two main use cases for lazily-instantiated proxies:
125 // basic knowledge - width, height, config, origin are known
126 // minimal knowledge - only config is known.
127 //
128 // The basic knowledge version is used for DDL where we know the type of proxy we are going to
129 // use, but we don't have access to the GPU yet to instantiate it.
130 //
Chris Dalton685e09b2021-06-19 11:50:17 -0600131 // The minimal knowledge version is used for when we are generating an atlas but we do not know
132 // the final size until we have finished adding to it.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400133 GrTextureProxy(LazyInstantiateCallback&&,
134 const GrBackendFormat&,
Brian Salomona56a7462020-02-07 14:17:25 -0500135 SkISize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400136 GrMipmapped,
Brian Salomona6db5102020-07-21 09:56:23 -0400137 GrMipmapStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400138 SkBackingFit,
139 SkBudgeted,
140 GrProtected,
141 GrInternalSurfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400142 UseAllocator,
143 GrDDLProvider creatingProvider);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700144
robertphillips76948d42016-05-04 12:47:41 -0700145 // Wrapped version
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400146 GrTextureProxy(sk_sp<GrSurface>, UseAllocator, GrDDLProvider creatingProvider);
robertphillips76948d42016-05-04 12:47:41 -0700147
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400148 ~GrTextureProxy() override;
149
Robert Phillips5af44de2017-07-18 14:49:38 -0400150 sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
151
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400152 // By default uniqueKeys are propagated from a textureProxy to its backing GrTexture.
153 // Setting syncTargetKey to false disables this behavior and only keeps the unique key
154 // on the proxy.
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400155 void setTargetKeySync(bool sync) { fSyncTargetKey = sync; }
156
Robert Phillips84a81202016-11-04 11:59:10 -0400157private:
Brian Osman086679b2018-09-10 16:26:51 -0400158 // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings
159 // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes
160 // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for
161 // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this'
162 // in the constructors, and always looks for the full 16 byte alignment, even if the fields in
163 // that particular class don't require it. Changing the size of this object can move the start
164 // address of other types, leading to this problem.
165
Brian Salomon40a40622020-07-21 10:32:07 -0400166 GrMipmapped fMipmapped;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600167
168 // This tracks the mipmap status at the proxy level and is thus somewhat distinct from the
169 // backing GrTexture's mipmap status. In particular, this status is used to determine when
Greg Danielf41b2bd2019-08-22 16:19:24 -0400170 // mipmap levels need to be explicitly regenerated during the execution of a DAG of opsTasks.
Brian Salomon8c82a872020-07-21 12:09:58 -0400171 GrMipmapStatus fMipmapStatus;
Brian Salomona6db5102020-07-21 09:56:23 -0400172 // TEMPORARY: We are in the process of moving GrMipmapStatus from the texture to the proxy.
Brian Salomon8c82a872020-07-21 12:09:58 -0400173 // We track the fInitialMipmapStatus here so we can assert that the proxy did indeed expect
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600174 // the correct mipmap status immediately after instantiation.
175 //
Brian Salomon8c82a872020-07-21 12:09:58 -0400176 // NOTE: fMipmapStatus may no longer be equal to fInitialMipmapStatus by the time the texture
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600177 // is instantiated, since it tracks mipmaps in the time frame in which the DAG is being built.
Brian Salomon8c82a872020-07-21 12:09:58 -0400178 SkDEBUGCODE(const GrMipmapStatus fInitialMipmapStatus;)
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600179
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400180 bool fSyncTargetKey = true; // Should target's unique key be sync'ed with ours.
Brian Salomonbb5711a2017-05-17 13:49:59 -0400181
Robert Phillipsf10b2a52020-05-15 10:20:04 -0400182 // For GrTextureProxies created in a DDL recording thread it is possible for the uniqueKey
183 // to be cleared on the backing GrTexture while the uniqueKey remains on the proxy.
184 // A fCreatingProvider of DDLProvider::kYes loosens up asserts that the key of an instantiated
185 // uniquely-keyed textureProxy is also always set on the backing GrTexture.
186 GrDDLProvider fCreatingProvider = GrDDLProvider::kNo;
187
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500188 GrUniqueKey fUniqueKey;
189 GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400190
Brian Salomon63410e92020-03-23 18:32:50 -0400191 LazySurfaceDesc callbackDesc() const override;
192
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500193 // Only used for proxies whose contents are being prepared on a worker thread. This object
194 // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that
195 // point, the proxy is instantiated, and this data is used to perform an ASAP upload.
196 std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader;
197
Greg Daniel0eca74c2020-10-01 13:46:00 -0400198 size_t onUninstantiatedGpuMemorySize() const override;
Robert Phillips8bc06d02016-11-01 17:28:40 -0400199
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400200 // Methods made available via GrTextureProxy::CacheAccess
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500201 void setUniqueKey(GrProxyProvider*, const GrUniqueKey&);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400202 void clearUniqueKey();
203
Greg Daniel849dce12018-04-24 14:32:53 -0400204 SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700205
John Stiles7571f9e2020-09-02 22:42:33 -0400206 using INHERITED = GrSurfaceProxy;
robertphillips76948d42016-05-04 12:47:41 -0700207};
208
209#endif