xref: /aosp_15_r20/external/skia/src/gpu/graphite/TextureProxy.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 Google LLC
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 skgpu_graphite_TextureProxy_DEFINED
9 #define skgpu_graphite_TextureProxy_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkSize.h"
13 #include "include/gpu/graphite/TextureInfo.h"
14 #include "include/private/base/SkTo.h"
15 
16 #include <functional>
17 
18 enum SkColorType : int;
19 
20 namespace skgpu::graphite {
21 
22 class Caps;
23 class Recorder;
24 class ResourceProvider;
25 class ScratchResourceManager;
26 class Texture;
27 
28 class TextureProxy : public SkRefCnt {
29 public:
30     TextureProxy() = delete;
31 
32     ~TextureProxy() override;
33 
numSamples()34     int numSamples() const { return fInfo.numSamples(); }
mipmapped()35     Mipmapped mipmapped() const { return fInfo.mipmapped(); }
36 
37     SkISize dimensions() const;
textureInfo()38     const TextureInfo& textureInfo() const { return fInfo; }
39 
label()40     const char* label() const { return fLabel.c_str(); }
41 
42     bool isLazy() const;
43     bool isFullyLazy() const;
44     bool isVolatile() const;
45     bool isProtected() const;
46 
47     size_t uninstantiatedGpuMemorySize() const;
48 
49     bool instantiate(ResourceProvider*);
50     /*
51      * We currently only instantiate lazy proxies at insertion-time. Snap-time 'instantiate'
52      * calls should be wrapped in 'InstantiateIfNotLazy'.
53      *
54      * Unlike Ganesh, in Graphite we do not update the proxy's dimensions with the instantiating
55      * texture's dimensions. This means that when a fully-lazy proxy is instantiated and
56      * deinstantiated, it goes back to being fully-lazy and without dimensions, and can be
57      * re-instantiated with a new texture with different dimensions than the first.
58      */
59     bool lazyInstantiate(ResourceProvider*);
60     /*
61      * For Lazy proxies this will return true. Otherwise, it will return the result of
62      * calling instantiate on the texture proxy.
63      *
64      * DEPRECATED: Eventually all un-instantiated non-lazy proxies should use the
65      *             ScratchResourceManager function instead of the ResourceProvider directly.
66      */
67     static bool InstantiateIfNotLazy(ResourceProvider*, TextureProxy*);
68 
69     /*
70      * Instantiate any scratch proxy (not already instantiated and not lazy) by using a texture
71      * from the ScratchResourceManager. When possible, this will be a texture that has been returned
72      * for reuse by a prior task. Lazy proxies and already instantiated proxies will return true.
73      *
74      * False is returned if instantiation fails.
75      */
76     static bool InstantiateIfNotLazy(ScratchResourceManager*, TextureProxy*);
77 
isInstantiated()78     bool isInstantiated() const { return SkToBool(fTexture); }
79     void deinstantiate();
80     sk_sp<Texture> refTexture() const;
81     const Texture* texture() const;
texture()82     Texture* texture() { return fTexture.get(); }
83 
84     // Make() will immediately instantiate non-budgeted proxies.
85     static sk_sp<TextureProxy> Make(const Caps*,
86                                     ResourceProvider*,
87                                     SkISize dimensions,
88                                     const TextureInfo&,
89                                     std::string_view label,
90                                     skgpu::Budgeted);
91 
92     using LazyInstantiateCallback = std::function<sk_sp<Texture> (ResourceProvider*)>;
93 
94     static sk_sp<TextureProxy> MakeLazy(const Caps*,
95                                         SkISize dimensions,
96                                         const TextureInfo&,
97                                         skgpu::Budgeted,
98                                         Volatile,
99                                         LazyInstantiateCallback&&);
100     static sk_sp<TextureProxy> MakeFullyLazy(const TextureInfo&,
101                                              skgpu::Budgeted,
102                                              Volatile,
103                                              LazyInstantiateCallback&&);
104 
105     static sk_sp<TextureProxy> Wrap(sk_sp<Texture>);
106 
107 private:
108     TextureProxy(SkISize dimensions,
109                  const TextureInfo& info,
110                  std::string_view label,
111                  skgpu::Budgeted budgeted);
112     TextureProxy(SkISize dimensions,
113                  const TextureInfo&,
114                  skgpu::Budgeted,
115                  Volatile,
116                  LazyInstantiateCallback&&);
117     TextureProxy(sk_sp<Texture>);
118 
119 #ifdef SK_DEBUG
120     void validateTexture(const Texture*);
121 #endif
122 
123     // In the following, 'fVolatile' and 'fLazyInstantiateCallback' can be accessed from
124     // multiple threads so need to remain immutable.
125     SkISize fDimensions;
126     const TextureInfo fInfo;
127 
128     // String used to describe the current use of this TextureProxy. It will be set on its
129     // Texture object when the proxy gets instantiated.
130     std::string fLabel;
131 
132     skgpu::Budgeted fBudgeted;
133     const Volatile fVolatile;
134 
135     sk_sp<Texture> fTexture;
136 
137     const LazyInstantiateCallback fLazyInstantiateCallback;
138 };
139 
140 // Volatile texture proxy that deinstantiates itself on destruction.
141 class AutoDeinstantiateTextureProxy {
142 public:
AutoDeinstantiateTextureProxy(TextureProxy * textureProxy)143     AutoDeinstantiateTextureProxy(TextureProxy* textureProxy) : fTextureProxy(textureProxy) {}
144 
~AutoDeinstantiateTextureProxy()145     ~AutoDeinstantiateTextureProxy() {
146         if (fTextureProxy) {
147             fTextureProxy->deinstantiate();
148         }
149     }
150 
151 private:
152     TextureProxy* const fTextureProxy;
153 };
154 
155 }  // namespace skgpu::graphite
156 
157 #endif // skgpu_graphite_TextureProxy_DEFINED
158