Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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"
17
18namespace skgpu::graphite {
19
20TextureProxy::TextureProxy(SkISize dimensions, const TextureInfo& info, skgpu::Budgeted budgeted)
21 : fDimensions(dimensions), fInfo(info), fBudgeted(budgeted), fVolatile(Volatile::kNo) {
22 SkASSERT(fInfo.isValid());
23}
24
25TextureProxy::TextureProxy(sk_sp<Texture> texture)
26 : fDimensions(texture->dimensions())
27 , fInfo(texture->textureInfo())
28 , fBudgeted(texture->budgeted())
29 , fVolatile(Volatile::kNo)
30 , fTexture(std::move(texture)) {
31 SkASSERT(fInfo.isValid());
32}
33
34TextureProxy::TextureProxy(SkISize dimensions,
35 const TextureInfo& textureInfo,
36 skgpu::Budgeted budgeted,
37 Volatile isVolatile,
38 LazyInstantiateCallback&& callback)
39 : fDimensions(dimensions)
40 , fInfo(textureInfo)
41 , fBudgeted(budgeted)
42 , fVolatile(isVolatile)
43 , fLazyInstantiateCallback(std::move(callback)) {
44 SkASSERT(fInfo.isValid());
45 SkASSERT(fLazyInstantiateCallback);
46}
47
48TextureProxy::~TextureProxy() {}
49
50SkISize TextureProxy::dimensions() const {
51 SkASSERT(!this->isFullyLazy() || this->isInstantiated());
52 return this->isInstantiated() ? fTexture->dimensions() : fDimensions;
53}
54
55bool TextureProxy::isLazy() const {
56 return SkToBool(fLazyInstantiateCallback);
57}
58
59bool TextureProxy::isFullyLazy() const {
60 bool result = fDimensions.width() < 0;
61 SkASSERT(result == (fDimensions.height() < 0));
62 SkASSERT(!result || this->isLazy());
63 return result;
64}
65
66bool TextureProxy::isVolatile() const {
67 SkASSERT(fVolatile == Volatile::kNo || SkToBool(fLazyInstantiateCallback));
68
69 return fVolatile == Volatile::kYes;
70}
71
72bool TextureProxy::isProtected() const {
73 return fInfo.isProtected() == Protected::kYes;
74}
75
76size_t TextureProxy::uninstantiatedGpuMemorySize() const {
77 return ComputeSize(fDimensions, fInfo);
78}
79
80bool TextureProxy::instantiate(ResourceProvider* resourceProvider) {
81 SkASSERT(!this->isLazy());
82
83 if (fTexture) {
84 return true;
85 }
86
87 fTexture = resourceProvider->findOrCreateScratchTexture(fDimensions, fInfo, fBudgeted);
88 if (!fTexture) {
89 return false;
90 }
91 SkDEBUGCODE(this->validateTexture(fTexture.get()));
92 return true;
93}
94
95bool TextureProxy::lazyInstantiate(ResourceProvider* resourceProvider) {
96 SkASSERT(this->isLazy());
97
98 if (fTexture) {
99 return true;
100 }
101
102 fTexture = fLazyInstantiateCallback(resourceProvider);
103 if (!fTexture) {
104 return false;
105 }
106 SkDEBUGCODE(this->validateTexture(fTexture.get()));
107 return true;
108}
109
110bool TextureProxy::InstantiateIfNotLazy(ResourceProvider* resourceProvider,
111 TextureProxy* textureProxy) {
112 if (textureProxy->isLazy()) {
113 return true;
114 }
115
116 return textureProxy->instantiate(resourceProvider);
117}
118
119void TextureProxy::deinstantiate() {
120 SkASSERT(fVolatile == Volatile::kYes && SkToBool(fLazyInstantiateCallback));
121
122 fTexture.reset();
123}
124
125sk_sp<Texture> TextureProxy::refTexture() const {
126 return fTexture;
127}
128
129const Texture* TextureProxy::texture() const {
130 return fTexture.get();
131}
132
133sk_sp<TextureProxy> TextureProxy::Make(const Caps* caps,
134 ResourceProvider* resourceProvider,
135 SkISize dimensions,
136 const TextureInfo& textureInfo,
137 skgpu::Budgeted budgeted) {
138 if (dimensions.width() < 1 || dimensions.height() < 1 ||
139 dimensions.width() > caps->maxTextureSize() ||
140 dimensions.height() > caps->maxTextureSize() ||
141 !textureInfo.isValid()) {
142 return nullptr;
143 }
144
145 sk_sp<TextureProxy> proxy{new TextureProxy(dimensions, textureInfo, budgeted)};
146 if (budgeted == Budgeted::kNo) {
147 // Instantiate immediately to avoid races later on if the client starts to use the wrapping
148 // object on multiple threads.
149 if (!proxy->instantiate(resourceProvider)) {
150 return nullptr;
151 }
152 }
153 return proxy;
154}
155
156sk_sp<TextureProxy> TextureProxy::MakeLazy(const Caps* caps,
157 SkISize dimensions,
158 const TextureInfo& textureInfo,
159 skgpu::Budgeted budgeted,
160 Volatile isVolatile,
162 SkASSERT(textureInfo.isValid());
163 if (dimensions.width() < 1 || dimensions.height() < 1 ||
164 dimensions.width() > caps->maxTextureSize() ||
165 dimensions.height() > caps->maxTextureSize()) {
166 return nullptr;
167 }
168
169 return sk_sp<TextureProxy>(new TextureProxy(dimensions, textureInfo, budgeted,
170 isVolatile, std::move(callback)));
171}
172
173sk_sp<TextureProxy> TextureProxy::MakeFullyLazy(const TextureInfo& textureInfo,
174 skgpu::Budgeted budgeted,
175 Volatile isVolatile,
177 SkASSERT(textureInfo.isValid());
178
180 SkISize::Make(-1, -1), textureInfo, budgeted, isVolatile, std::move(callback)));
181}
182
184 return sk_sp<TextureProxy>(new TextureProxy(std::move(texture)));
185}
186
187#ifdef SK_DEBUG
188void TextureProxy::validateTexture(const Texture* texture) {
189 SkASSERT(this->isFullyLazy() || fDimensions == texture->dimensions());
190 SkASSERTF(fInfo.isCompatible(texture->textureInfo()),
191 "proxy->fInfo[%s] incompatible with texture->fInfo[%s]",
192 fInfo.toString().c_str(),
193 texture->textureInfo().toString().c_str());
194}
195#endif
196
197} // 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
#define SkDEBUGCODE(...)
Definition SkDebug.h:23
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
int maxTextureSize() const
Definition Caps.h:134
sk_sp< Texture > findOrCreateScratchTexture(SkISize, const TextureInfo &, skgpu::Budgeted)
bool instantiate(ResourceProvider *)
std::function< sk_sp< Texture >(ResourceProvider *)> LazyInstantiateCallback
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
FlTexture * texture
size_t ComputeSize(SkISize dimensions, const TextureInfo &info)
Budgeted
Definition GpuTypes.h:35
Definition ref_ptr.h:256
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