Flutter Engine
The Flutter Engine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
TextureProxy.cpp
Go to the documentation of this file.
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
9
11#include "src/core/SkMipmap.h"
18
19namespace skgpu::graphite {
20
22 const TextureInfo& info,
23 std::string_view label,
24 skgpu::Budgeted budgeted)
25 : fDimensions(dimensions)
26 , fInfo(info)
27 , fLabel(label)
28 , fBudgeted(budgeted)
29 , fVolatile(Volatile::kNo) {
30 SkASSERT(fInfo.isValid());
31}
32
33TextureProxy::TextureProxy(sk_sp<Texture> texture)
34 : fDimensions(texture->dimensions())
35 , fInfo(texture->textureInfo())
36 , fLabel(texture->getLabel())
37 , fBudgeted(texture->budgeted())
38 , fVolatile(Volatile::kNo)
39 , fTexture(std::move(texture)) {
40 SkASSERT(fInfo.isValid());
41}
42
43TextureProxy::TextureProxy(SkISize dimensions,
44 const TextureInfo& textureInfo,
45 skgpu::Budgeted budgeted,
46 Volatile isVolatile,
47 LazyInstantiateCallback&& callback)
48 : fDimensions(dimensions)
49 , fInfo(textureInfo)
50 , fBudgeted(budgeted)
51 , fVolatile(isVolatile)
52 , fLazyInstantiateCallback(std::move(callback)) {
53 SkASSERT(fInfo.isValid());
54 SkASSERT(fLazyInstantiateCallback);
55}
56
57TextureProxy::~TextureProxy() {}
58
59SkISize TextureProxy::dimensions() const {
60 SkASSERT(!this->isFullyLazy() || this->isInstantiated());
61 return this->isInstantiated() ? fTexture->dimensions() : fDimensions;
62}
63
64bool TextureProxy::isLazy() const {
65 return SkToBool(fLazyInstantiateCallback);
66}
67
68bool TextureProxy::isFullyLazy() const {
69 bool result = fDimensions.width() < 0;
70 SkASSERT(result == (fDimensions.height() < 0));
71 SkASSERT(!result || this->isLazy());
72 return result;
73}
74
75bool TextureProxy::isVolatile() const {
76 SkASSERT(fVolatile == Volatile::kNo || SkToBool(fLazyInstantiateCallback));
77
78 return fVolatile == Volatile::kYes;
79}
80
81bool TextureProxy::isProtected() const {
82 return fInfo.isProtected() == Protected::kYes;
83}
84
85size_t TextureProxy::uninstantiatedGpuMemorySize() const {
86 return ComputeSize(fDimensions, fInfo);
87}
88
89bool TextureProxy::instantiate(ResourceProvider* resourceProvider) {
90 SkASSERT(!this->isLazy());
91
92 if (fTexture) {
93 return true;
94 }
95
96 fTexture = resourceProvider->findOrCreateScratchTexture(fDimensions, fInfo, fLabel, fBudgeted);
97 if (!fTexture) {
98 return false;
99 }
100 SkDEBUGCODE(this->validateTexture(fTexture.get()));
101 return true;
102}
103
104bool TextureProxy::lazyInstantiate(ResourceProvider* resourceProvider) {
105 SkASSERT(this->isLazy());
106
107 if (fTexture) {
108 return true;
109 }
110
111 fTexture = fLazyInstantiateCallback(resourceProvider);
112 if (!fTexture) {
113 return false;
114 }
115 SkDEBUGCODE(this->validateTexture(fTexture.get()));
116 return true;
117}
118
119bool TextureProxy::InstantiateIfNotLazy(ResourceProvider* resourceProvider,
120 TextureProxy* textureProxy) {
121 if (textureProxy->isLazy()) {
122 return true;
123 }
124
125 return textureProxy->instantiate(resourceProvider);
126}
127
128bool TextureProxy::InstantiateIfNotLazy(ScratchResourceManager* scratchManager,
129 TextureProxy* textureProxy) {
130 if (textureProxy->isLazy() || textureProxy->isInstantiated()) {
131 return true;
132 }
133
134 textureProxy->fTexture = scratchManager->getScratchTexture(textureProxy->dimensions(),
135 textureProxy->textureInfo(),
136 textureProxy->fLabel);
137 if (!textureProxy->fTexture) {
138 return false;
139 }
140 SkDEBUGCODE(textureProxy->validateTexture(textureProxy->fTexture.get()));
141 return true;
142}
143
144
145void TextureProxy::deinstantiate() {
146 SkASSERT(fVolatile == Volatile::kYes && SkToBool(fLazyInstantiateCallback));
147
148 fTexture.reset();
149}
150
151sk_sp<Texture> TextureProxy::refTexture() const {
152 return fTexture;
153}
154
156 return fTexture.get();
157}
158
160 ResourceProvider* resourceProvider,
161 SkISize dimensions,
162 const TextureInfo& textureInfo,
163 std::string_view label,
164 skgpu::Budgeted budgeted) {
165 if (dimensions.width() < 1 || dimensions.height() < 1 ||
166 dimensions.width() > caps->maxTextureSize() ||
167 dimensions.height() > caps->maxTextureSize() ||
168 !textureInfo.isValid()) {
169 return nullptr;
170 }
171
172 sk_sp<TextureProxy> proxy{new TextureProxy(dimensions,
173 textureInfo,
174 std::move(label),
175 budgeted)};
176 if (budgeted == Budgeted::kNo) {
177 // Instantiate immediately to avoid races later on if the client starts to use the wrapping
178 // object on multiple threads.
179 if (!proxy->instantiate(resourceProvider)) {
180 return nullptr;
181 }
182 }
183 return proxy;
184}
185
186sk_sp<TextureProxy> TextureProxy::MakeLazy(const Caps* caps,
187 SkISize dimensions,
188 const TextureInfo& textureInfo,
189 skgpu::Budgeted budgeted,
190 Volatile isVolatile,
192 SkASSERT(textureInfo.isValid());
193 if (dimensions.width() < 1 || dimensions.height() < 1 ||
194 dimensions.width() > caps->maxTextureSize() ||
195 dimensions.height() > caps->maxTextureSize()) {
196 return nullptr;
197 }
198
199 return sk_sp<TextureProxy>(new TextureProxy(dimensions,
200 textureInfo,
201 budgeted,
202 isVolatile,
203 std::move(callback)));
204}
205
206sk_sp<TextureProxy> TextureProxy::MakeFullyLazy(const TextureInfo& textureInfo,
207 skgpu::Budgeted budgeted,
208 Volatile isVolatile,
210 SkASSERT(textureInfo.isValid());
211
213 textureInfo,
214 budgeted,
215 isVolatile,
216 std::move(callback)));
217}
218
220 return sk_sp<TextureProxy>(new TextureProxy(std::move(texture)));
221}
222
223#ifdef SK_DEBUG
224void TextureProxy::validateTexture(const Texture* texture) {
225 SkASSERT(this->isFullyLazy() || fDimensions == texture->dimensions());
226 SkASSERTF(fInfo.isCompatible(texture->textureInfo()),
227 "proxy->fInfo[%s] incompatible with texture->fInfo[%s]",
228 fInfo.toString().c_str(),
229 texture->textureInfo().toString().c_str());
230}
231#endif
232
233} // namespace skgpu::graphite
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SkASSERTF(cond, fmt,...)
Definition: SkAssert.h:117
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
int maxTextureSize() const
Definition: Caps.h:141
sk_sp< Texture > findOrCreateScratchTexture(SkISize, const TextureInfo &, std::string_view label, skgpu::Budgeted)
sk_sp< Texture > getScratchTexture(SkISize, const TextureInfo &, std::string_view label)
bool instantiate(ResourceProvider *)
std::function< sk_sp< Texture >(ResourceProvider *)> LazyInstantiateCallback
Definition: TextureProxy.h:92
const TextureInfo & textureInfo() const
Definition: TextureProxy.h:38
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
FlTexture * texture
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
size_t ComputeSize(SkISize dimensions, const TextureInfo &info)
Budgeted
Definition: GpuTypes.h:35
Definition: ref_ptr.h:256
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
GrSamplerState::WrapMode Wrap
Definition: texelsubset.cpp:40