Debian Codegraph @

tools/gpu/YUVUtils.cpp

raw
1 /*
2 * Copyright 2019 Google Inc.
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/YUVUtils.h"
9
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColorFilter.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkSurface.h"
15 #include "src/codec/SkCodecImageGenerator.h"
16 #include "src/core/SkColorPriv.h"
17 #include "src/core/SkYUVAInfoLocation.h"
18 #include "src/core/SkYUVMath.h"
19 #include "src/image/SkImage_Base.h"
20 #include "tools/gpu/ManagedBackendTexture.h"
21
22 #ifdef SK_GANESH
23 #include "include/gpu/ganesh/GrRecordingContext.h"
24 #include "include/gpu/ganesh/GrYUVABackendTextures.h"
25 #include "include/gpu/ganesh/SkImageGanesh.h"
26 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
27 #include "src/gpu/ganesh/GrDirectContextPriv.h"
28 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
29 #endif
30
31 #ifdef SK_GRAPHITE
32 #include "include/gpu/graphite/Image.h"
33 #include "include/gpu/graphite/YUVABackendTextures.h"
34 #include "include/private/base/SkTArray.h"
35 #include "src/core/SkAutoPixmapStorage.h"
36 #endif
37
38 namespace {
39
40 static SkPMColor convert_yuva_to_rgba(const float mtx[20], uint8_t yuva[4]) {
41 uint8_t y = yuva[0];
42 uint8_t u = yuva[1];
43 uint8_t v = yuva[2];
44 uint8_t a = yuva[3];
45
46 uint8_t r = SkTPin(SkScalarRoundToInt(mtx[ 0]*y + mtx[ 1]*u + mtx[ 2]*v + mtx[ 4]*255), 0, 255);
47 uint8_t g = SkTPin(SkScalarRoundToInt(mtx[ 5]*y + mtx[ 6]*u + mtx[ 7]*v + mtx[ 9]*255), 0, 255);
48 uint8_t b = SkTPin(SkScalarRoundToInt(mtx[10]*y + mtx[11]*u + mtx[12]*v + mtx[14]*255), 0, 255);
49
50 return SkPremultiplyARGBInline(a, r, g, b);
51 }
52
53 static uint8_t look_up(SkPoint normPt, const SkPixmap& pmap, SkColorChannel channel) {
54 SkASSERT(normPt.x() > 0 && normPt.x() < 1.0f);
55 SkASSERT(normPt.y() > 0 && normPt.y() < 1.0f);
56 int x = SkScalarFloorToInt(normPt.x() * pmap.width());
57 int y = SkScalarFloorToInt(normPt.y() * pmap.height());
58
59 auto ii = pmap.info().makeColorType(kRGBA_8888_SkColorType).makeWH(1, 1);
60 uint32_t pixel;
61 SkAssertResult(pmap.readPixels(ii, &pixel, sizeof(pixel), x, y));
62 int shift = static_cast<int>(channel) * 8;
63 return static_cast<uint8_t>((pixel >> shift) & 0xff);
64 }
65
66 class Generator : public SkImageGenerator {
67 public:
68 Generator(SkYUVAPixmaps pixmaps, sk_sp<SkColorSpace> cs)
69 : SkImageGenerator(SkImageInfo::Make(pixmaps.yuvaInfo().dimensions(),
70 kN32_SkColorType,
71 kPremul_SkAlphaType,
72 std::move(cs)))
73 , fPixmaps(std::move(pixmaps)) {}
74
75 protected:
76 bool onGetPixels(const SkImageInfo& info,
77 void* pixels,
78 size_t rowBytes,
79 const Options&) override {
80 if (kUnknown_SkColorType == fFlattened.colorType()) {
81 fFlattened.allocPixels(info);
82 SkASSERT(info == this->getInfo());
83
84 float mtx[20];
85 SkColorMatrix_YUV2RGB(fPixmaps.yuvaInfo().yuvColorSpace(), mtx);
86 SkYUVAInfo::YUVALocations yuvaLocations = fPixmaps.toYUVALocations();
87 SkASSERT(SkYUVAInfo::YUVALocation::AreValidLocations(yuvaLocations));
88
89 SkMatrix om = fPixmaps.yuvaInfo().inverseOriginMatrix();
90 float normX = 1.f/info.width();
91 float normY = 1.f/info.height();
92 if (SkEncodedOriginSwapsWidthHeight(fPixmaps.yuvaInfo().origin())) {
93 using std::swap;
94 swap(normX, normY);
95 }
96 for (int y = 0; y < info.height(); ++y) {
97 for (int x = 0; x < info.width(); ++x) {
98 SkPoint xy1 {(x + 0.5f),
99 (y + 0.5f)};
100 xy1 = om.mapPoint(xy1);
101 xy1.fX *= normX;
102 xy1.fY *= normY;
103
104 uint8_t yuva[4] = {0, 0, 0, 255};
105
106 for (auto c : {SkYUVAInfo::YUVAChannels::kY,
107 SkYUVAInfo::YUVAChannels::kU,
108 SkYUVAInfo::YUVAChannels::kV}) {
109 const auto& pmap = fPixmaps.plane(yuvaLocations[c].fPlane);
110 yuva[c] = look_up(xy1, pmap, yuvaLocations[c].fChannel);
111 }
112 auto [aPlane, aChan] = yuvaLocations[SkYUVAInfo::YUVAChannels::kA];
113 if (aPlane >= 0) {
114 const auto& pmap = fPixmaps.plane(aPlane);
115 yuva[3] = look_up(xy1, pmap, aChan);
116 }
117
118 // Making premul here.
119 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba(mtx, yuva);
120 }
121 }
122 }
123
124 return fFlattened.readPixels(info, pixels, rowBytes, 0, 0);
125 }
126
127 bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& types,
128 SkYUVAPixmapInfo* info) const override {
129 *info = fPixmaps.pixmapsInfo();
130 return info->isValid();
131 }
132
133 bool onGetYUVAPlanes(const SkYUVAPixmaps& pixmaps) override {
134 SkASSERT(pixmaps.yuvaInfo() == fPixmaps.yuvaInfo());
135 for (int i = 0; i < pixmaps.numPlanes(); ++i) {
136 SkASSERT(fPixmaps.plane(i).colorType() == pixmaps.plane(i).colorType());
137 SkASSERT(fPixmaps.plane(i).dimensions() == pixmaps.plane(i).dimensions());
138 SkASSERT(fPixmaps.plane(i).rowBytes() == pixmaps.plane(i).rowBytes());
139 fPixmaps.plane(i).readPixels(pixmaps.plane(i));
140 }
141 return true;
142 }
143
144 private:
145 SkYUVAPixmaps fPixmaps;
146 SkBitmap fFlattened;
147 };
148
149 } // anonymous namespace
150
151 namespace sk_gpu_test {
152
153 std::tuple<std::array<sk_sp<SkImage>, SkYUVAInfo::kMaxPlanes>, SkYUVAInfo>
154 MakeYUVAPlanesAsA8(SkImage* src,
155 SkYUVColorSpace cs,
156 SkYUVAInfo::Subsampling ss,
157 GrRecordingContext* rContext) {
158 float rgbToYUV[20];
159 SkColorMatrix_RGB2YUV(cs, rgbToYUV);
160
161 SkYUVAInfo::PlaneConfig config = src->isOpaque() ? SkYUVAInfo::PlaneConfig::kY_U_V
162 : SkYUVAInfo::PlaneConfig::kY_U_V_A;
163 SkISize dims[SkYUVAInfo::kMaxPlanes];
164 int n = SkYUVAInfo::PlaneDimensions(src->dimensions(),
165 config,
166 ss,
167 kTopLeft_SkEncodedOrigin,
168 dims);
169 std::array<sk_sp<SkImage>, 4> planes;
170 for (int i = 0; i < n; ++i) {
171 SkImageInfo info = SkImageInfo::MakeA8(dims[i]);
172 sk_sp<SkSurface> surf;
173 #if defined(SK_GANESH)
174 if (rContext) {
175 surf = SkSurfaces::RenderTarget(rContext, skgpu::Budgeted::kYes, info, 1, nullptr);
176 } else
177 #endif
178 {
179 surf = SkSurfaces::Raster(info);
180 }
181 if (!surf) {
182 return {};
183 }
184
185 SkPaint paint;
186 paint.setBlendMode(SkBlendMode::kSrc);
187
188 // Make a matrix with the ith row of rgbToYUV copied to the A row since we're drawing to A8.
189 float m[20] = {};
190 std::copy_n(rgbToYUV + 5*i, 5, m + 15);
191 paint.setColorFilter(SkColorFilters::Matrix(m));
192 surf->getCanvas()->drawImageRect(src,
193 SkRect::Make(dims[i]),
194 SkSamplingOptions(SkFilterMode::kLinear),
195 &paint);
196 planes[i] = surf->makeImageSnapshot();
197 if (!planes[i]) {
198 return {};
199 }
200 }
201 SkYUVAInfo info(src->dimensions(), config, ss, cs);
202 return {planes, info};
203 }
204
205 std::unique_ptr<LazyYUVImage> LazyYUVImage::Make(sk_sp<SkData> data,
206 skgpu::Mipmapped mipmapped,
207 sk_sp<SkColorSpace> cs) {
208 std::unique_ptr<LazyYUVImage> image(new LazyYUVImage());
209 if (image->reset(std::move(data), mipmapped, std::move(cs))) {
210 return image;
211 } else {
212 return nullptr;
213 }
214 }
215
216 std::unique_ptr<LazyYUVImage> LazyYUVImage::Make(SkYUVAPixmaps pixmaps,
217 skgpu::Mipmapped mipmapped,
218 sk_sp<SkColorSpace> cs) {
219 std::unique_ptr<LazyYUVImage> image(new LazyYUVImage());
220 if (image->reset(std::move(pixmaps), mipmapped, std::move(cs))) {
221 return image;
222 } else {
223 return nullptr;
224 }
225 }
226
227 sk_sp<SkImage> LazyYUVImage::refImage(GrRecordingContext* rContext, Type type) {
228 if (this->ensureYUVImage(rContext, type)) {
229 size_t idx = static_cast<size_t>(type);
230 SkASSERT(idx < std::size(fYUVImage));
231 return fYUVImage[idx];
232 } else {
233 return nullptr;
234 }
235 }
236
237 sk_sp<SkImage> LazyYUVImage::refImage(GrDirectContext* dContext, Type type) {
238 #if defined(SK_GANESH)
239 return this->refImage(static_cast<GrRecordingContext *>(dContext), type);
240 #else
241 SkASSERT(!dContext);
242 return this->refImage(static_cast<GrRecordingContext *>(nullptr), type);
243 #endif
244 }
245
246
247 #if defined(SK_GRAPHITE)
248 sk_sp<SkImage> LazyYUVImage::refImage(skgpu::graphite::Recorder* recorder, Type type) {
249 if (this->ensureYUVImage(recorder, type)) {
250 size_t idx = static_cast<size_t>(type);
251 SkASSERT(idx < std::size(fYUVImage));
252 return fYUVImage[idx];
253 } else {
254 return nullptr;
255 }
256 }
257 #endif
258
259 bool LazyYUVImage::reset(sk_sp<SkData> data, skgpu::Mipmapped mipmapped, sk_sp<SkColorSpace> cs) {
260 fMipmapped = mipmapped;
261 auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(data);
262 if (!codec) {
263 return false;
264 }
265
266 SkYUVAPixmapInfo yuvaPixmapInfo;
267 if (!codec->queryYUVAInfo(SkYUVAPixmapInfo::SupportedDataTypes::All(), &yuvaPixmapInfo)) {
268 return false;
269 }
270 fPixmaps = SkYUVAPixmaps::Allocate(yuvaPixmapInfo);
271 if (!fPixmaps.isValid()) {
272 return false;
273 }
274
275 if (!codec->getYUVAPlanes(fPixmaps)) {
276 return false;
277 }
278
279 fColorSpace = std::move(cs);
280
281 // The SkPixmap data is fully configured now for MakeFromYUVAPixmaps once we get a GrContext
282 return true;
283 }
284
285 bool LazyYUVImage::reset(SkYUVAPixmaps pixmaps,
286 skgpu::Mipmapped mipmapped,
287 sk_sp<SkColorSpace> cs) {
288 if (!pixmaps.isValid()) {
289 return false;
290 }
291 fMipmapped = mipmapped;
292 if (pixmaps.ownsStorage()) {
293 fPixmaps = std::move(pixmaps);
294 } else {
295 fPixmaps = SkYUVAPixmaps::MakeCopy(std::move(pixmaps));
296 }
297 fColorSpace = std::move(cs);
298 // The SkPixmap data is fully configured now for MakeFromYUVAPixmaps once we get a GrContext
299 return true;
300 }
301
302 bool LazyYUVImage::ensureYUVImage(GrRecordingContext* rContext, Type type) {
303 size_t idx = static_cast<size_t>(type);
304 SkASSERT(idx < std::size(fYUVImage));
305 #if defined(SK_GANESH)
306 if (fYUVImage[idx] && fYUVImage[idx]->isValid(rContext->asRecorder())) {
307 return true; // Have already made a YUV image valid for this context.
308 }
309 #endif
310 // Try to make a new YUV image for this context.
311 switch (type) {
312 case Type::kFromPixmaps:
313 #if defined(SK_GANESH)
314 if (!rContext || rContext->abandoned()) {
315 return false;
316 }
317 fYUVImage[idx] = SkImages::TextureFromYUVAPixmaps(rContext,
318 fPixmaps,
319 fMipmapped,
320 /*limit to max tex size*/ false,
321 fColorSpace);
322 #else
323 return false;
324 #endif
325 break;
326 case Type::kFromGenerator: {
327 // Make sure the generator has ownership of its backing planes.
328 auto generator = std::make_unique<Generator>(fPixmaps, fColorSpace);
329 fYUVImage[idx] = SkImages::DeferredFromGenerator(std::move(generator));
330 break;
331 }
332 case Type::kFromTextures:
333 #if defined(SK_GANESH)
334 if (!rContext || rContext->abandoned()) {
335 return false;
336 }
337 if (auto direct = rContext->asDirectContext()) {
338 sk_sp<sk_gpu_test::ManagedBackendTexture> mbets[SkYUVAInfo::kMaxPlanes];
339 GrBackendTexture textures[SkYUVAInfo::kMaxPlanes];
340 for (int i = 0; i < fPixmaps.numPlanes(); ++i) {
341 mbets[i] = sk_gpu_test::ManagedBackendTexture::MakeFromPixmap(
342 direct,
343 fPixmaps.plane(i),
344 fMipmapped,
345 skgpu::Renderable::kNo,
346 skgpu::Protected::kNo);
347 if (mbets[i]) {
348 textures[i] = mbets[i]->texture();
349 } else {
350 return false;
351 }
352 }
353 GrYUVABackendTextures yuvaTextures(fPixmaps.yuvaInfo(),
354 textures,
355 kTopLeft_GrSurfaceOrigin);
356 if (!yuvaTextures.isValid()) {
357 return false;
358 }
359 void* planeRelContext =
360 sk_gpu_test::ManagedBackendTexture::MakeYUVAReleaseContext(mbets);
361 fYUVImage[idx] = SkImages::TextureFromYUVATextures(
362 direct,
363 yuvaTextures,
364 fColorSpace,
365 sk_gpu_test::ManagedBackendTexture::ReleaseProc,
366 planeRelContext);
367 }
368 #else
369 return false;
370 #endif
371 break;
372 case Type::kFromImages:
373 // Not supported in Ganesh
374 return false;
375 }
376 return fYUVImage[idx] != nullptr;
377 }
378
379 #if defined(SK_GRAPHITE)
380 using BackendTexture = skgpu::graphite::BackendTexture;
381 using Recorder = skgpu::graphite::Recorder;
382 using YUVABackendTextures = skgpu::graphite::YUVABackendTextures;
383
384 bool LazyYUVImage::ensureYUVImage(Recorder* recorder, Type type) {
385 size_t idx = static_cast<size_t>(type);
386 SkASSERT(idx < std::size(fYUVImage));
387 if (fYUVImage[idx] && as_IB(fYUVImage[idx])->isGraphiteBacked()) {
388 return true; // Have already made a YUV image suitable for Graphite.
389 }
390 // Try to make a new Graphite YUV image
391 switch (type) {
392 case Type::kFromPixmaps:
393 if (!recorder) {
394 return false;
395 }
396 fYUVImage[idx] =
397 SkImages::TextureFromYUVAPixmaps(recorder,
398 fPixmaps,
399 {fMipmapped == skgpu::Mipmapped::kYes},
400 /*limitToMaxTextureSize=*/false,
401 fColorSpace);
402 break;
403 case Type::kFromGenerator: {
404 // Make sure the generator has ownership of its backing planes.
405 auto generator = std::make_unique<Generator>(fPixmaps, fColorSpace);
406 fYUVImage[idx] = SkImages::DeferredFromGenerator(std::move(generator));
407 break;
408 }
409 case Type::kFromTextures: {
410 if (!recorder) {
411 return false;
412 }
413
414 sk_sp<sk_gpu_test::ManagedGraphiteTexture> mbets[SkYUVAInfo::kMaxPlanes];
415 BackendTexture textures[SkYUVAInfo::kMaxPlanes];
416 for (int i = 0; i < fPixmaps.numPlanes(); ++i) {
417 // MakeFromPixmap will handle generating the upper mipmap levels if necessary.
418 mbets[i] = sk_gpu_test::ManagedGraphiteTexture::MakeFromPixmap(
419 recorder,
420 fPixmaps.plane(i),
421 fMipmapped,
422 skgpu::Renderable::kNo,
423 skgpu::Protected::kNo);
424 if (mbets[i]) {
425 textures[i] = mbets[i]->texture();
426 } else {
427 return false;
428 }
429 }
430 YUVABackendTextures yuvaTextures(fPixmaps.yuvaInfo(), textures);
431 if (!yuvaTextures.isValid()) {
432 return false;
433 }
434 void* imageRelContext =
435 sk_gpu_test::ManagedGraphiteTexture::MakeYUVAReleaseContext(mbets);
436 fYUVImage[idx] = SkImages::TextureFromYUVATextures(
437 recorder,
438 yuvaTextures,
439 fColorSpace,
440 sk_gpu_test::ManagedGraphiteTexture::ImageReleaseProc,
441 imageRelContext);
442 break;
443 }
444 case Type::kFromImages: {
445 if (!recorder) {
446 return false;
447 }
448
449 sk_sp<SkImage> planeImgs[SkYUVAInfo::kMaxPlanes];
450
451 using SkImages::GenerateMipmapsFromBase;
452 GenerateMipmapsFromBase genMipmaps = GenerateMipmapsFromBase::kNo;
453 if (fMipmapped == skgpu::Mipmapped::kYes) {
454 genMipmaps = GenerateMipmapsFromBase::kYes;
455 }
456
457 for (int i = 0; i < fPixmaps.numPlanes(); ++i) {
458 const auto& plane = fPixmaps.plane(i);
459 sk_sp<ManagedGraphiteTexture> mbet;
460 if (fMipmapped == skgpu::Mipmapped::kYes) {
461 mbet = ManagedGraphiteTexture::MakeUnInit(recorder,
462 plane.info(),
463 skgpu::Mipmapped::kYes,
464 skgpu::Renderable::kNo);
465 // We allocate a full mip set because updateBackendTexture requires it. However,
466 // the non-base levels are cleared to red. We rely on SkImages::WrapTexture
467 // to actually generate the contents from the base level for each plane on the
468 // GPU. This exercises the case where the client wants a mipmapped YUV image but
469 // only provides the base level contents.
470 int levelCnt = SkMipmap::ComputeLevelCount(plane.dimensions());
471 skia_private::TArray<SkAutoPixmapStorage> levelStorage(levelCnt);
472 skia_private::TArray<SkPixmap> levels(levelCnt + 1);
473 levels.push_back(plane);
474 for (int l = 0; l < levelCnt; ++l) {
475 SkISize dims = SkMipmap::ComputeLevelSize(plane.dimensions(), l);
476 SkAutoPixmapStorage level;
477 level.alloc(plane.info().makeDimensions(dims));
478 level.erase(SK_ColorRED);
479 levels.push_back(level);
480 levelStorage.push_back(std::move(level));
481 }
482 if (!mbet || !recorder->updateBackendTexture(mbet->texture(),
483 levels.data(),
484 levels.size())) {
485 return false;
486 }
487 } else {
488 mbet = ManagedGraphiteTexture::MakeFromPixmap(recorder,
489 plane,
490 skgpu::Mipmapped::kNo,
491 skgpu::Renderable::kNo);
492 if (!mbet) {
493 return false;
494 }
495 }
496 planeImgs[i] = SkImages::WrapTexture(recorder,
497 mbet->texture(),
498 plane.colorType(),
499 plane.alphaType(),
500 fColorSpace,
501 skgpu::Origin::kTopLeft,
502 genMipmaps,
503 ManagedGraphiteTexture::ImageReleaseProc,
504 mbet->releaseContext());
505 }
506
507 fYUVImage[idx] = SkImages::TextureFromYUVAImages(
508 recorder,
509 fPixmaps.yuvaInfo(),
510 planeImgs,
511 fColorSpace);
512 break;
513 }
514 }
515 return fYUVImage[idx] != nullptr;
516 }
517 #endif
518
519 } // namespace sk_gpu_test