blob: 7bc752852610f5b3d11f7d6af3cc1bd8c2dfcb3c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrSamplerState.h"
12#include "include/private/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;
Brian Osman45580d32016-11-23 09:37:01 -050018class GrTextureOpList;
Robert Phillips420c4cf2017-09-28 09:00:45 -040019class 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
Brian Salomon2bbdcc42017-09-07 12:36:34 -040030 GrSamplerState::Filter highestFilterMode() const;
Robert Phillips49081d12017-05-08 13:41:35 -040031
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050032 // 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;
Brian Salomonbb5711a2017-05-17 13:49:59 -040038
Brian Salomonfd98c2c2018-07-31 17:25:29 -040039 // Returns the GrMipMapped value of the proxy from creation time regardless of whether it has
40 // been instantiated or not.
41 GrMipMapped proxyMipMapped() const { return fMipMapped; }
42
Brian Salomon7da2fc72018-12-10 13:40:07 -050043 GrTextureType textureType() const { return this->backendFormat().textureType(); }
44
Brian Salomonfd98c2c2018-07-31 17:25:29 -040045 /** If true then the texture does not support MIP maps and only supports clamp wrap mode. */
Greg Daniel4065d452018-11-16 15:43:41 -050046 bool hasRestrictedSampling() const {
47 return GrTextureTypeHasRestrictedSampling(this->textureType());
48 }
Greg Daniel45723ac2018-11-30 10:12:43 -050049
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
Robert Phillipsae7d3f32017-09-21 08:26:08 -040055 /**
56 * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one.
57 */
58 const GrUniqueKey& getUniqueKey() const {
59#ifdef SK_DEBUG
Brian Salomonb6a3a3b2019-04-01 12:29:34 -040060 if (fTarget && fUniqueKey.isValid() && fSyncTargetKey) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040061 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
Robert Phillips420c4cf2017-09-28 09:00:45 -040080 // Provides access to special purpose functions.
81 GrTextureProxyPriv texPriv();
82 const GrTextureProxyPriv texPriv() const;
83
Robert Phillips84a81202016-11-04 11:59:10 -040084protected:
Robert Phillips0bd24dc2018-01-16 08:06:32 -050085 // DDL TODO: rm the GrSurfaceProxy friending
Brian Salomonb6a3a3b2019-04-01 12:29:34 -040086 friend class GrSurfaceProxy; // for ctors
87 friend class GrProxyProvider; // for ctors
Brian Osman099fa0f2017-10-02 16:38:32 -040088 friend class GrTextureProxyPriv;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -040089 friend class GrSurfaceProxyPriv; // ability to change key sync state after lazy instantiation.
Robert Phillips37430132016-11-09 06:50:43 -050090
Brian Salomon58389b92018-03-07 13:01:25 -050091 // Deferred version - no data.
Greg Daniel4065d452018-11-16 15:43:41 -050092 GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrSurfaceOrigin,
Greg Daniel23093132019-06-17 11:10:48 -040093 GrMipMapped, const GrSwizzle& textureSwizzle, SkBackingFit, SkBudgeted,
94 GrInternalSurfaceFlags);
Chris Dalton706a6ff2017-11-29 22:01:06 -070095
96 // Lazy-callback version
Greg Daniel65fa8ca2018-01-10 17:06:31 -050097 // There are two main use cases for lazily-instantiated proxies:
98 // basic knowledge - width, height, config, origin are known
99 // minimal knowledge - only config is known.
100 //
101 // The basic knowledge version is used for DDL where we know the type of proxy we are going to
102 // use, but we don't have access to the GPU yet to instantiate it.
103 //
104 // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not
105 // know the final size until flush time.
Greg Daniel4065d452018-11-16 15:43:41 -0500106 GrTextureProxy(LazyInstantiateCallback&&, LazyInstantiationType, const GrBackendFormat&,
Greg Daniel23093132019-06-17 11:10:48 -0400107 const GrSurfaceDesc& desc, GrSurfaceOrigin, GrMipMapped, const GrSwizzle&,
108 SkBackingFit, SkBudgeted, GrInternalSurfaceFlags);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700109
robertphillips76948d42016-05-04 12:47:41 -0700110 // Wrapped version
Greg Daniel23093132019-06-17 11:10:48 -0400111 GrTextureProxy(sk_sp<GrSurface>, GrSurfaceOrigin, const GrSwizzle&);
robertphillips76948d42016-05-04 12:47:41 -0700112
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400113 ~GrTextureProxy() override;
114
Robert Phillips5af44de2017-07-18 14:49:38 -0400115 sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
116
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400117 void setTargetKeySync(bool sync) { fSyncTargetKey = sync; }
118
Robert Phillips84a81202016-11-04 11:59:10 -0400119private:
Brian Osman086679b2018-09-10 16:26:51 -0400120 // 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
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400128 GrMipMapped fMipMapped;
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400129 bool fSyncTargetKey = true; // Should target's unique key be sync'ed with ours.
Brian Salomonbb5711a2017-05-17 13:49:59 -0400130
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500131 GrUniqueKey fUniqueKey;
132 GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400133
Brian Osman099fa0f2017-10-02 16:38:32 -0400134 // Only used for proxies whose contents are being prepared on a worker thread. This object
135 // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that
136 // point, the proxy is instantiated, and this data is used to perform an ASAP upload.
137 std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader;
138
Brian Salomonbb5711a2017-05-17 13:49:59 -0400139 size_t onUninstantiatedGpuMemorySize() const override;
Robert Phillips8bc06d02016-11-01 17:28:40 -0400140
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400141 // Methods made available via GrTextureProxy::CacheAccess
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500142 void setUniqueKey(GrProxyProvider*, const GrUniqueKey&);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400143 void clearUniqueKey();
144
Greg Daniel849dce12018-04-24 14:32:53 -0400145 SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700146
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400147 // For wrapped proxies the GrTexture pointer is stored in GrIORefProxy.
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400148 // For deferred proxies that pointer will be filled in when we need to instantiate
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400149 // the deferred resource
robertphillips76948d42016-05-04 12:47:41 -0700150
151 typedef GrSurfaceProxy INHERITED;
152};
153
154#endif