Flutter Engine
The Flutter Engine
ManagedBackendTexture.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 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
12#include "src/core/SkMipmap.h"
14#ifdef SK_GANESH
16#endif
17#ifdef SK_GRAPHITE
20#endif
21
25
26#ifdef SK_GANESH
27namespace {
28
29struct Context {
30 GrGpuFinishedProc fWrappedProc = nullptr;
31 GrGpuFinishedContext fWrappedContext = nullptr;
33};
34
35} // anonymous namespace
36
37namespace sk_gpu_test {
38
39void ManagedBackendTexture::ReleaseProc(void* ctx) {
40 std::unique_ptr<Context> context(static_cast<Context*>(ctx));
41 if (context->fWrappedProc) {
42 context->fWrappedProc(context->fWrappedContext);
43 }
44}
45
46ManagedBackendTexture::~ManagedBackendTexture() {
47 if (fDContext && fTexture.isValid()) {
49 }
50}
51
52void* ManagedBackendTexture::releaseContext(GrGpuFinishedProc wrappedProc,
53 GrGpuFinishedContext wrappedCtx) const {
54 // Make sure we don't get a wrapped ctx without a wrapped proc
55 SkASSERT(!wrappedCtx || wrappedProc);
56 return new Context{wrappedProc, wrappedCtx, {sk_ref_sp(this)}};
57}
58
59void* ManagedBackendTexture::MakeYUVAReleaseContext(
61 auto context = new Context;
62 for (int i = 0; i < SkYUVAInfo::kMaxPlanes; ++i) {
63 context->fMBETs[i] = mbets[i];
64 }
65 return context;
66}
67
68sk_sp<skgpu::RefCntedCallback> ManagedBackendTexture::refCountedCallback() const {
69 return skgpu::RefCntedCallback::Make(ReleaseProc, this->releaseContext());
70}
71
72void ManagedBackendTexture::wasAdopted() { fTexture = {}; }
73
74sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromInfo(GrDirectContext* dContext,
75 const SkImageInfo& ii,
76 Mipmapped mipmapped,
77 Renderable renderable,
78 Protected isProtected) {
79 return MakeWithoutData(dContext,
80 ii.width(),
81 ii.height(),
82 ii.colorType(),
83 mipmapped,
84 renderable,
85 isProtected);
86}
87
89 const SkBitmap& src,
90 Mipmapped mipmapped,
91 Renderable renderable,
92 Protected isProtected) {
93 SkPixmap srcPixmap;
94 if (!src.peekPixels(&srcPixmap)) {
95 return nullptr;
96 }
97
98 return MakeFromPixmap(dContext, srcPixmap, mipmapped, renderable, isProtected);
99}
100
101sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromPixmap(GrDirectContext* dContext,
102 const SkPixmap& src,
103 Mipmapped mipmapped,
104 Renderable renderable,
105 Protected isProtected) {
106 std::vector<SkPixmap> levels({src});
107 std::unique_ptr<SkMipmap> mm;
108
109 if (mipmapped == Mipmapped::kYes) {
110 mm.reset(SkMipmap::Build(src, nullptr));
111 if (!mm) {
112 return nullptr;
113 }
114 for (int i = 0; i < mm->countLevels(); ++i) {
116 SkAssertResult(mm->getLevel(i, &level));
117 levels.push_back(level.fPixmap);
118 }
119 }
120 return MakeWithData(dContext,
121 levels.data(),
122 static_cast<int>(levels.size()),
124 renderable,
125 isProtected);
126}
127
128} // namespace sk_gpu_test
129
130#endif // SK_GANESH
131
132#ifdef SK_GRAPHITE
134
135namespace {
136
137struct MBETContext {
138 MBETContext(const sk_sp<sk_gpu_test::ManagedGraphiteTexture>& tex)
139 : fMBETs{tex, nullptr, nullptr, nullptr} {}
141 : fMBETs{mbets[0], mbets[1], mbets[2], mbets[3]} {}
143};
144
145} // anonymous namespace
146
147namespace sk_gpu_test {
148
149void ManagedGraphiteTexture::ReleaseProc(void* ctx) {
150 std::unique_ptr<MBETContext> context(static_cast<MBETContext*>(ctx));
151}
152
153void ManagedGraphiteTexture::FinishedProc(void* ctx, skgpu::CallbackResult) {
154 std::unique_ptr<MBETContext> context(static_cast<MBETContext*>(ctx));
155}
156void ManagedGraphiteTexture::ImageReleaseProc(void* ctx) {
157 std::unique_ptr<MBETContext> context(static_cast<MBETContext*>(ctx));
158}
159
160ManagedGraphiteTexture::~ManagedGraphiteTexture() {
161 if (fContext && fTexture.isValid()) {
162 fContext->deleteBackendTexture(fTexture);
163 }
164}
165
166void* ManagedGraphiteTexture::releaseContext() const {
167 return new MBETContext{{sk_ref_sp(this)}};
168}
169
170void* ManagedGraphiteTexture::MakeYUVAReleaseContext(
172 return new MBETContext(mbets);
173}
174
175sk_sp<skgpu::RefCntedCallback> ManagedGraphiteTexture::refCountedCallback() const {
176 return skgpu::RefCntedCallback::Make(FinishedProc, this->releaseContext());
177}
178
179sk_sp<ManagedGraphiteTexture> ManagedGraphiteTexture::MakeUnInit(Recorder* recorder,
180 const SkImageInfo& ii,
181 Mipmapped mipmapped,
182 Renderable renderable,
183 Protected isProtected) {
184 sk_sp<ManagedGraphiteTexture> mbet(new ManagedGraphiteTexture);
185 mbet->fContext = recorder->priv().context();
186 const skgpu::graphite::Caps* caps = recorder->priv().caps();
187
189 mipmapped,
190 isProtected,
191 renderable);
192
193 mbet->fTexture = recorder->createBackendTexture(ii.dimensions(), info);
194 if (!mbet->fTexture.isValid()) {
195 return nullptr;
196 }
197
198 recorder->addFinishInfo({mbet->releaseContext(), FinishedProc});
199
200 return mbet;
201}
202
203sk_sp<ManagedGraphiteTexture> ManagedGraphiteTexture::MakeFromPixmap(Recorder* recorder,
204 const SkPixmap& src,
205 Mipmapped mipmapped,
206 Renderable renderable,
207 Protected isProtected) {
208 sk_sp<ManagedGraphiteTexture> mbet = MakeUnInit(recorder, src.info(), mipmapped, renderable,
209 isProtected);
210 if (!mbet) {
211 return nullptr;
212 }
213
214 std::vector<SkPixmap> levels({src});
215 std::unique_ptr<SkMipmap> mm;
216
217 if (mipmapped == Mipmapped::kYes) {
218 mm.reset(SkMipmap::Build(src, nullptr));
219 if (!mm) {
220 return nullptr;
221 }
222 for (int i = 0; i < mm->countLevels(); ++i) {
224 SkAssertResult(mm->getLevel(i, &level));
225 levels.push_back(level.fPixmap);
226 }
227 }
228
229 if (!recorder->updateBackendTexture(mbet->fTexture,
230 levels.data(),
231 static_cast<int>(levels.size()))) {
232 return nullptr;
233 }
234
235 return mbet;
236}
237
238sk_sp<ManagedGraphiteTexture> ManagedGraphiteTexture::MakeMipmappedFromPixmaps(
239 Recorder* recorder,
241 skgpu::Renderable renderable,
242 skgpu::Protected isProtected) {
243 if (levels.empty()) {
244 return nullptr;
245 }
246 sk_sp<ManagedGraphiteTexture> mbet = MakeUnInit(recorder,
247 levels[0].info(),
249 renderable,
250 isProtected);
251 if (!recorder->updateBackendTexture(mbet->fTexture,
252 levels.data(),
253 static_cast<int>(levels.size()))) {
254 return nullptr;
255 }
256
257 return mbet;
258}
259
260sk_sp<ManagedGraphiteTexture> ManagedGraphiteTexture::MakeFromCompressedData(
261 Recorder* recorder,
262 SkISize dimensions,
263 SkTextureCompressionType compression,
265 skgpu::Mipmapped mipmapped,
266 skgpu::Protected isProtected) {
267 sk_sp<ManagedGraphiteTexture> mbet(new ManagedGraphiteTexture);
268 mbet->fContext = recorder->priv().context();
269 const skgpu::graphite::Caps* caps = recorder->priv().caps();
270
272 mipmapped,
273 isProtected);
274
275 mbet->fTexture = recorder->createBackendTexture(dimensions, info);
276 if (!mbet->fTexture.isValid()) {
277 return nullptr;
278 }
279
280 recorder->addFinishInfo({mbet->releaseContext(), FinishedProc});
281
282 if (!recorder->updateCompressedBackendTexture(mbet->fTexture,
283 src->data(),
284 src->size())) {
285 return nullptr;
286 }
287
288 return mbet;
289}
290
291} // namespace sk_gpu_test
292
293#endif // SK_GRAPHITE
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
void * GrGpuFinishedContext
Definition: GrTypes.h:178
void(* GrGpuFinishedProc)(GrGpuFinishedContext finishedContext)
Definition: GrTypes.h:179
#define SkASSERT(cond)
Definition: SkAssert.h:116
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
const Context & fContext
SkTextureCompressionType
GrDirectContext * fDContext
void deleteBackendTexture(const GrBackendTexture &)
static SkMipmap * Build(const SkPixmap &src, SkDiscardableFactoryProc, bool computeContents=true)
Definition: SkMipmap.cpp:45
constexpr T * data() const
Definition: SkSpan_impl.h:94
constexpr bool empty() const
Definition: SkSpan_impl.h:96
constexpr size_t size() const
Definition: SkSpan_impl.h:95
static constexpr int kMaxPlanes
Definition: SkYUVAInfo.h:98
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310
static sk_sp< RefCntedCallback > Make(Callback proc, Context ctx)
virtual TextureInfo getDefaultCompressedTextureInfo(SkTextureCompressionType, Mipmapped mipmapped, Protected) const =0
virtual TextureInfo getDefaultSampledTextureInfo(SkColorType, Mipmapped mipmapped, Protected, Renderable) const =0
const Caps * caps() const
Definition: RecorderPriv.h:31
bool updateCompressedBackendTexture(const BackendTexture &, const void *data, size_t dataSize)
Definition: Recorder.cpp:384
BackendTexture createBackendTexture(SkISize dimensions, const TextureInfo &)
Definition: Recorder.cpp:284
void addFinishInfo(const InsertFinishInfo &)
Definition: Recorder.cpp:432
bool updateBackendTexture(const BackendTexture &, const SkPixmap srcData[], int numLevels)
Definition: Recorder.cpp:314
SK_API sk_sp< SkPixelRef > MakeWithData(const SkImageInfo &, size_t rowBytes, sk_sp< SkData > data)
sk_sp< SkImage > MakeFromBitmap(Recorder *recorder, const SkColorInfo &colorInfo, const SkBitmap &bitmap, sk_sp< SkMipmap > mipmaps, Budgeted budgeted, SkImage::RequiredProperties requiredProps, std::string_view label)
Renderable
Definition: GpuTypes.h:69
CallbackResult
Definition: GpuTypes.h:45
Mipmapped
Definition: GpuTypes.h:53
Protected
Definition: GpuTypes.h:61
Definition: SkSize.h:16
SkISize dimensions() const
Definition: SkImageInfo.h:421
int width() const
Definition: SkImageInfo.h:365
SkColorType colorType() const
Definition: SkImageInfo.h:373
int height() const
Definition: SkImageInfo.h:371
skgpu::graphite::Recorder Recorder