/src/skia/tools/gpu/ManagedBackendTexture.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
8 | | #include "tools/gpu/ManagedBackendTexture.h" |
9 | | |
10 | | #include "include/core/SkBitmap.h" |
11 | | #include "include/core/SkImageInfo.h" |
12 | | #include "src/core/SkMipmap.h" |
13 | | #include "src/gpu/RefCntedCallback.h" |
14 | | #ifdef SK_GANESH |
15 | | #include "include/private/gpu/ganesh/GrTypesPriv.h" |
16 | | #endif |
17 | | #ifdef SK_GRAPHITE |
18 | | #include "include/gpu/graphite/Recorder.h" |
19 | | #include "src/gpu/graphite/Caps.h" |
20 | | #endif |
21 | | |
22 | | using Mipmapped = skgpu::Mipmapped; |
23 | | using Protected = skgpu::Protected; |
24 | | using Renderable = skgpu::Renderable; |
25 | | |
26 | | #ifdef SK_GANESH |
27 | | namespace { |
28 | | |
29 | | struct Context { |
30 | | GrGpuFinishedProc fWrappedProc = nullptr; |
31 | | GrGpuFinishedContext fWrappedContext = nullptr; |
32 | | sk_sp<sk_gpu_test::ManagedBackendTexture> fMBETs[SkYUVAInfo::kMaxPlanes]; |
33 | | }; |
34 | | |
35 | | } // anonymous namespace |
36 | | |
37 | | namespace sk_gpu_test { |
38 | | |
39 | 0 | void ManagedBackendTexture::ReleaseProc(void* ctx) { |
40 | 0 | std::unique_ptr<Context> context(static_cast<Context*>(ctx)); |
41 | 0 | if (context->fWrappedProc) { |
42 | 0 | context->fWrappedProc(context->fWrappedContext); |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | 0 | ManagedBackendTexture::~ManagedBackendTexture() { |
47 | 0 | if (fDContext && fTexture.isValid()) { |
48 | 0 | fDContext->deleteBackendTexture(fTexture); |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | | void* ManagedBackendTexture::releaseContext(GrGpuFinishedProc wrappedProc, |
53 | 0 | GrGpuFinishedContext wrappedCtx) const { |
54 | | // Make sure we don't get a wrapped ctx without a wrapped proc |
55 | 0 | SkASSERT(!wrappedCtx || wrappedProc); |
56 | 0 | return new Context{wrappedProc, wrappedCtx, {sk_ref_sp(this)}}; |
57 | 0 | } Unexecuted instantiation: sk_gpu_test::ManagedBackendTexture::releaseContext(void (*)(void*), void*) const Unexecuted instantiation: sk_gpu_test::ManagedBackendTexture::releaseContext(void (*)(void*), void*) const |
58 | | |
59 | | void* ManagedBackendTexture::MakeYUVAReleaseContext( |
60 | 0 | const sk_sp<ManagedBackendTexture> mbets[SkYUVAInfo::kMaxPlanes]) { |
61 | 0 | auto context = new Context; |
62 | 0 | for (int i = 0; i < SkYUVAInfo::kMaxPlanes; ++i) { |
63 | 0 | context->fMBETs[i] = mbets[i]; |
64 | 0 | } |
65 | 0 | return context; |
66 | 0 | } |
67 | | |
68 | 0 | sk_sp<skgpu::RefCntedCallback> ManagedBackendTexture::refCountedCallback() const { |
69 | 0 | return skgpu::RefCntedCallback::Make(ReleaseProc, this->releaseContext()); |
70 | 0 | } |
71 | | |
72 | 0 | void ManagedBackendTexture::wasAdopted() { fTexture = {}; } |
73 | | |
74 | | sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromInfo(GrDirectContext* dContext, |
75 | | const SkImageInfo& ii, |
76 | | Mipmapped mipmapped, |
77 | | Renderable renderable, |
78 | 0 | Protected isProtected) { |
79 | 0 | return MakeWithoutData(dContext, |
80 | 0 | ii.width(), |
81 | 0 | ii.height(), |
82 | 0 | ii.colorType(), |
83 | 0 | mipmapped, |
84 | 0 | renderable, |
85 | 0 | isProtected); |
86 | 0 | } |
87 | | |
88 | | sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromBitmap(GrDirectContext* dContext, |
89 | | const SkBitmap& src, |
90 | | Mipmapped mipmapped, |
91 | | Renderable renderable, |
92 | 0 | Protected isProtected) { |
93 | 0 | SkPixmap srcPixmap; |
94 | 0 | if (!src.peekPixels(&srcPixmap)) { |
95 | 0 | return nullptr; |
96 | 0 | } |
97 | | |
98 | 0 | return MakeFromPixmap(dContext, srcPixmap, mipmapped, renderable, isProtected); |
99 | 0 | } |
100 | | |
101 | | sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromPixmap(GrDirectContext* dContext, |
102 | | const SkPixmap& src, |
103 | | Mipmapped mipmapped, |
104 | | Renderable renderable, |
105 | 0 | Protected isProtected) { |
106 | 0 | std::vector<SkPixmap> levels({src}); |
107 | 0 | std::unique_ptr<SkMipmap> mm; |
108 | |
|
109 | 0 | if (mipmapped == Mipmapped::kYes) { |
110 | 0 | mm.reset(SkMipmap::Build(src, nullptr)); |
111 | 0 | if (!mm) { |
112 | 0 | return nullptr; |
113 | 0 | } |
114 | 0 | for (int i = 0; i < mm->countLevels(); ++i) { |
115 | 0 | SkMipmap::Level level; |
116 | 0 | SkAssertResult(mm->getLevel(i, &level)); |
117 | 0 | levels.push_back(level.fPixmap); |
118 | 0 | } |
119 | 0 | } |
120 | 0 | return MakeWithData(dContext, |
121 | 0 | levels.data(), |
122 | 0 | static_cast<int>(levels.size()), |
123 | 0 | kTopLeft_GrSurfaceOrigin, |
124 | 0 | renderable, |
125 | 0 | isProtected); |
126 | 0 | } Unexecuted instantiation: sk_gpu_test::ManagedBackendTexture::MakeFromPixmap(GrDirectContext*, SkPixmap const&, skgpu::Mipmapped, skgpu::Renderable, skgpu::Protected) Unexecuted instantiation: sk_gpu_test::ManagedBackendTexture::MakeFromPixmap(GrDirectContext*, SkPixmap const&, skgpu::Mipmapped, skgpu::Renderable, skgpu::Protected) |
127 | | |
128 | | } // namespace sk_gpu_test |
129 | | |
130 | | #endif // SK_GANESH |
131 | | |
132 | | #ifdef SK_GRAPHITE |
133 | | using Recorder = skgpu::graphite::Recorder; |
134 | | |
135 | | namespace { |
136 | | |
137 | | struct MBETContext { |
138 | | MBETContext(const sk_sp<sk_gpu_test::ManagedGraphiteTexture>& tex) |
139 | 0 | : fMBETs{tex, nullptr, nullptr, nullptr} {} |
140 | | MBETContext(const sk_sp<sk_gpu_test::ManagedGraphiteTexture> mbets[SkYUVAInfo::kMaxPlanes]) |
141 | 0 | : fMBETs{mbets[0], mbets[1], mbets[2], mbets[3]} {} |
142 | | sk_sp<sk_gpu_test::ManagedGraphiteTexture> fMBETs[SkYUVAInfo::kMaxPlanes]; |
143 | | }; |
144 | | |
145 | | } // anonymous namespace |
146 | | |
147 | | namespace sk_gpu_test { |
148 | | |
149 | 0 | void ManagedGraphiteTexture::ReleaseProc(void* ctx) { |
150 | 0 | std::unique_ptr<MBETContext> context(static_cast<MBETContext*>(ctx)); |
151 | 0 | } |
152 | | |
153 | 0 | void ManagedGraphiteTexture::FinishedProc(void* ctx, skgpu::CallbackResult) { |
154 | 0 | std::unique_ptr<MBETContext> context(static_cast<MBETContext*>(ctx)); |
155 | 0 | } |
156 | 0 | void ManagedGraphiteTexture::ImageReleaseProc(void* ctx) { |
157 | 0 | std::unique_ptr<MBETContext> context(static_cast<MBETContext*>(ctx)); |
158 | 0 | } |
159 | | |
160 | 0 | ManagedGraphiteTexture::~ManagedGraphiteTexture() { |
161 | 0 | if (fContext && fTexture.isValid()) { |
162 | 0 | fContext->deleteBackendTexture(fTexture); |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | 0 | void* ManagedGraphiteTexture::releaseContext() const { |
167 | 0 | return new MBETContext{{sk_ref_sp(this)}}; |
168 | 0 | } |
169 | | |
170 | | void* ManagedGraphiteTexture::MakeYUVAReleaseContext( |
171 | 0 | const sk_sp<ManagedGraphiteTexture> mbets[SkYUVAInfo::kMaxPlanes]) { |
172 | 0 | return new MBETContext(mbets); |
173 | 0 | } |
174 | | |
175 | 0 | sk_sp<skgpu::RefCntedCallback> ManagedGraphiteTexture::refCountedCallback() const { |
176 | 0 | return skgpu::RefCntedCallback::Make(FinishedProc, this->releaseContext()); |
177 | 0 | } |
178 | | |
179 | | sk_sp<ManagedGraphiteTexture> ManagedGraphiteTexture::MakeUnInit(Recorder* recorder, |
180 | | const SkImageInfo& ii, |
181 | | Mipmapped mipmapped, |
182 | | Renderable renderable, |
183 | 0 | Protected isProtected) { |
184 | 0 | sk_sp<ManagedGraphiteTexture> mbet(new ManagedGraphiteTexture); |
185 | 0 | mbet->fContext = recorder->priv().context(); |
186 | 0 | const skgpu::graphite::Caps* caps = recorder->priv().caps(); |
187 | |
|
188 | 0 | skgpu::graphite::TextureInfo info = caps->getDefaultSampledTextureInfo(ii.colorType(), |
189 | 0 | mipmapped, |
190 | 0 | isProtected, |
191 | 0 | renderable); |
192 | |
|
193 | 0 | mbet->fTexture = recorder->createBackendTexture(ii.dimensions(), info); |
194 | 0 | if (!mbet->fTexture.isValid()) { |
195 | 0 | return nullptr; |
196 | 0 | } |
197 | | |
198 | 0 | recorder->addFinishInfo({mbet->releaseContext(), FinishedProc}); |
199 | |
|
200 | 0 | return mbet; |
201 | 0 | } |
202 | | |
203 | | sk_sp<ManagedGraphiteTexture> ManagedGraphiteTexture::MakeFromPixmap(Recorder* recorder, |
204 | | const SkPixmap& src, |
205 | | Mipmapped mipmapped, |
206 | | Renderable renderable, |
207 | 0 | Protected isProtected) { |
208 | 0 | sk_sp<ManagedGraphiteTexture> mbet = MakeUnInit(recorder, src.info(), mipmapped, renderable, |
209 | 0 | isProtected); |
210 | 0 | if (!mbet) { |
211 | 0 | return nullptr; |
212 | 0 | } |
213 | | |
214 | 0 | std::vector<SkPixmap> levels({src}); |
215 | 0 | std::unique_ptr<SkMipmap> mm; |
216 | |
|
217 | 0 | if (mipmapped == Mipmapped::kYes) { |
218 | 0 | mm.reset(SkMipmap::Build(src, nullptr)); |
219 | 0 | if (!mm) { |
220 | 0 | return nullptr; |
221 | 0 | } |
222 | 0 | for (int i = 0; i < mm->countLevels(); ++i) { |
223 | 0 | SkMipmap::Level level; |
224 | 0 | SkAssertResult(mm->getLevel(i, &level)); |
225 | 0 | levels.push_back(level.fPixmap); |
226 | 0 | } |
227 | 0 | } |
228 | | |
229 | 0 | if (!recorder->updateBackendTexture(mbet->fTexture, |
230 | 0 | levels.data(), |
231 | 0 | static_cast<int>(levels.size()))) { |
232 | 0 | return nullptr; |
233 | 0 | } |
234 | | |
235 | 0 | return mbet; |
236 | 0 | } Unexecuted instantiation: sk_gpu_test::ManagedGraphiteTexture::MakeFromPixmap(skgpu::graphite::Recorder*, SkPixmap const&, skgpu::Mipmapped, skgpu::Renderable, skgpu::Protected) Unexecuted instantiation: sk_gpu_test::ManagedGraphiteTexture::MakeFromPixmap(skgpu::graphite::Recorder*, SkPixmap const&, skgpu::Mipmapped, skgpu::Renderable, skgpu::Protected) |
237 | | |
238 | | sk_sp<ManagedGraphiteTexture> ManagedGraphiteTexture::MakeMipmappedFromPixmaps( |
239 | | Recorder* recorder, |
240 | | SkSpan<const SkPixmap> levels, |
241 | | skgpu::Renderable renderable, |
242 | 0 | skgpu::Protected isProtected) { |
243 | 0 | if (levels.empty()) { |
244 | 0 | return nullptr; |
245 | 0 | } |
246 | 0 | sk_sp<ManagedGraphiteTexture> mbet = MakeUnInit(recorder, |
247 | 0 | levels[0].info(), |
248 | 0 | Mipmapped::kYes, |
249 | 0 | renderable, |
250 | 0 | isProtected); |
251 | 0 | if (!recorder->updateBackendTexture(mbet->fTexture, |
252 | 0 | levels.data(), |
253 | 0 | static_cast<int>(levels.size()))) { |
254 | 0 | return nullptr; |
255 | 0 | } |
256 | | |
257 | 0 | return mbet; |
258 | 0 | } |
259 | | |
260 | | sk_sp<ManagedGraphiteTexture> ManagedGraphiteTexture::MakeFromCompressedData( |
261 | | Recorder* recorder, |
262 | | SkISize dimensions, |
263 | | SkTextureCompressionType compression, |
264 | | sk_sp<SkData> src, |
265 | | skgpu::Mipmapped mipmapped, |
266 | 0 | skgpu::Protected isProtected) { |
267 | 0 | sk_sp<ManagedGraphiteTexture> mbet(new ManagedGraphiteTexture); |
268 | 0 | mbet->fContext = recorder->priv().context(); |
269 | 0 | const skgpu::graphite::Caps* caps = recorder->priv().caps(); |
270 | |
|
271 | 0 | skgpu::graphite::TextureInfo info = caps->getDefaultCompressedTextureInfo(compression, |
272 | 0 | mipmapped, |
273 | 0 | isProtected); |
274 | |
|
275 | 0 | mbet->fTexture = recorder->createBackendTexture(dimensions, info); |
276 | 0 | if (!mbet->fTexture.isValid()) { |
277 | 0 | return nullptr; |
278 | 0 | } |
279 | | |
280 | 0 | recorder->addFinishInfo({mbet->releaseContext(), FinishedProc}); |
281 | |
|
282 | 0 | if (!recorder->updateCompressedBackendTexture(mbet->fTexture, |
283 | 0 | src->data(), |
284 | 0 | src->size())) { |
285 | 0 | return nullptr; |
286 | 0 | } |
287 | | |
288 | 0 | return mbet; |
289 | 0 | } |
290 | | |
291 | | } // namespace sk_gpu_test |
292 | | |
293 | | #endif // SK_GRAPHITE |