1 /*
2 * Copyright 2018 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 "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorFilter.h"
14 #include "include/core/SkColorPriv.h"
15 #include "include/core/SkColorSpace.h"
16 #include "include/core/SkFilterQuality.h"
17 #include "include/core/SkFont.h"
18 #include "include/core/SkFontStyle.h"
19 #include "include/core/SkFontTypes.h"
20 #include "include/core/SkImage.h"
21 #include "include/core/SkImageGenerator.h"
22 #include "include/core/SkImageInfo.h"
23 #include "include/core/SkMatrix.h"
24 #include "include/core/SkPaint.h"
25 #include "include/core/SkPath.h"
26 #include "include/core/SkPixmap.h"
27 #include "include/core/SkPoint.h"
28 #include "include/core/SkRect.h"
29 #include "include/core/SkRefCnt.h"
30 #include "include/core/SkScalar.h"
31 #include "include/core/SkSize.h"
32 #include "include/core/SkString.h"
33 #include "include/core/SkTypeface.h"
34 #include "include/core/SkTypes.h"
35 #include "include/gpu/GrBackendSurface.h"
36 #include "include/gpu/GrConfig.h"
37 #include "include/gpu/GrDirectContext.h"
38 #include "include/gpu/GrRecordingContext.h"
39 #include "include/gpu/GrTypes.h"
40 #include "include/private/GrTypesPriv.h"
41 #include "include/private/SkTArray.h"
42 #include "include/private/SkTDArray.h"
43 #include "include/private/SkTPin.h"
44 #include "include/private/SkTemplates.h"
45 #include "include/utils/SkTextUtils.h"
46 #include "src/core/SkConvertPixels.h"
47 #include "src/core/SkYUVMath.h"
48 #include "src/gpu/GrCaps.h"
49 #include "src/gpu/GrRecordingContextPriv.h"
50 #include "tools/ToolUtils.h"
51 #include "tools/gpu/YUVUtils.h"
52
53 #include <math.h>
54 #include <string.h>
55 #include <initializer_list>
56 #include <memory>
57 #include <utility>
58
59 class GrSurfaceDrawContext;
60
61 static const int kTileWidthHeight = 128;
62 static const int kLabelWidth = 64;
63 static const int kLabelHeight = 32;
64 static const int kSubsetPadding = 8;
65 static const int kPad = 1;
66
67 enum YUVFormat {
68 // 4:2:0 formats, 24 bpp
69 kP016_YUVFormat, // 16-bit Y plane + 2x2 down sampled interleaved U/V plane (2 textures)
70 // 4:2:0 formats, "15 bpp" (but really 24 bpp)
71 kP010_YUVFormat, // same as kP016 except "10 bpp". Note that it is the same memory layout
72 // except that the bottom 6 bits are zeroed out (2 textures)
73 // TODO: we're cheating a bit w/ P010 and just treating it as unorm 16. This means its
74 // fully saturated values are 65504 rather than 65535 (that is just .9995 out of 1.0 though).
75
76 // This is laid out the same as kP016 and kP010 but uses F16 unstead of U16. In this case
77 // the 10 bits/channel vs 16 bits/channel distinction isn't relevant.
78 kP016F_YUVFormat,
79
80 // 4:4:4 formats, 64 bpp
81 kY416_YUVFormat, // 16-bit AVYU values all interleaved (1 texture)
82
83 // 4:4:4 formats, 32 bpp
84 kAYUV_YUVFormat, // 8-bit YUVA values all interleaved (1 texture)
85 kY410_YUVFormat, // AVYU w/ 10bpp for YUV and 2 for A all interleaved (1 texture)
86
87 // 4:2:0 formats, 12 bpp
88 kNV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled interleaved U/V planes (2 textures)
89 kNV21_YUVFormat, // same as kNV12 but w/ U/V reversed in the interleaved texture (2 textures)
90
91 kI420_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled U and V planes (3 textures)
92 kYV12_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled V and U planes (3 textures)
93
94 kLast_YUVFormat = kYV12_YUVFormat
95 };
96
97 // Does the YUVFormat contain a slot for alpha? If not an external alpha plane is required for
98 // transparency.
has_alpha_channel(YUVFormat format)99 static bool has_alpha_channel(YUVFormat format) {
100 switch (format) {
101 case kP016_YUVFormat: return false;
102 case kP010_YUVFormat: return false;
103 case kP016F_YUVFormat: return false;
104 case kY416_YUVFormat: return true;
105 case kAYUV_YUVFormat: return true;
106 case kY410_YUVFormat: return true;
107 case kNV12_YUVFormat: return false;
108 case kNV21_YUVFormat: return false;
109 case kI420_YUVFormat: return false;
110 case kYV12_YUVFormat: return false;
111 }
112 SkUNREACHABLE;
113 }
114
115 class YUVAPlanarConfig {
116 public:
YUVAPlanarConfig(YUVFormat format,bool opaque,SkEncodedOrigin origin)117 YUVAPlanarConfig(YUVFormat format, bool opaque, SkEncodedOrigin origin) : fOrigin(origin) {
118 switch (format) {
119 case kP016_YUVFormat:
120 case kP010_YUVFormat:
121 case kP016F_YUVFormat:
122 case kNV12_YUVFormat:
123 if (opaque) {
124 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_UV;
125 fSubsampling = SkYUVAInfo::Subsampling::k420;
126 } else {
127 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_UV_A;
128 fSubsampling = SkYUVAInfo::Subsampling::k420;
129 }
130 break;
131 case kY416_YUVFormat:
132 case kY410_YUVFormat:
133 if (opaque) {
134 fPlaneConfig = SkYUVAInfo::PlaneConfig::kUYV;
135 fSubsampling = SkYUVAInfo::Subsampling::k444;
136 } else {
137 fPlaneConfig = SkYUVAInfo::PlaneConfig::kUYVA;
138 fSubsampling = SkYUVAInfo::Subsampling::k444;
139 }
140 break;
141 case kAYUV_YUVFormat:
142 if (opaque) {
143 fPlaneConfig = SkYUVAInfo::PlaneConfig::kYUV;
144 fSubsampling = SkYUVAInfo::Subsampling::k444;
145 } else {
146 fPlaneConfig = SkYUVAInfo::PlaneConfig::kYUVA;
147 fSubsampling = SkYUVAInfo::Subsampling::k444;
148 }
149 break;
150 case kNV21_YUVFormat:
151 if (opaque) {
152 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_VU;
153 fSubsampling = SkYUVAInfo::Subsampling::k420;
154 } else {
155 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_VU_A;
156 fSubsampling = SkYUVAInfo::Subsampling::k420;
157 }
158 break;
159 case kI420_YUVFormat:
160 if (opaque) {
161 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_U_V;
162 fSubsampling = SkYUVAInfo::Subsampling::k420;
163 } else {
164 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_U_V_A;
165 fSubsampling = SkYUVAInfo::Subsampling::k420;
166 }
167 break;
168 case kYV12_YUVFormat:
169 if (opaque) {
170 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_V_U;
171 fSubsampling = SkYUVAInfo::Subsampling::k420;
172 } else {
173 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_V_U_A;
174 fSubsampling = SkYUVAInfo::Subsampling::k420;
175 }
176 break;
177 }
178 }
179
numPlanes() const180 int numPlanes() const { return SkYUVAInfo::NumPlanes(fPlaneConfig); }
181
182 SkYUVAPixmaps makeYUVAPixmaps(SkISize dimensions,
183 SkYUVColorSpace yuvColorSpace,
184 const SkBitmap bitmaps[],
185 int numBitmaps) const;
186
187 private:
188 SkYUVAInfo::PlaneConfig fPlaneConfig;
189 SkYUVAInfo::Subsampling fSubsampling;
190 SkEncodedOrigin fOrigin;
191 };
192
makeYUVAPixmaps(SkISize dimensions,SkYUVColorSpace yuvColorSpace,const SkBitmap bitmaps[],int numBitmaps) const193 SkYUVAPixmaps YUVAPlanarConfig::makeYUVAPixmaps(SkISize dimensions,
194 SkYUVColorSpace yuvColorSpace,
195 const SkBitmap bitmaps[],
196 int numBitmaps) const {
197 SkYUVAInfo info(dimensions, fPlaneConfig, fSubsampling, yuvColorSpace, fOrigin);
198 SkPixmap pmaps[SkYUVAInfo::kMaxPlanes];
199 int n = info.numPlanes();
200 if (numBitmaps < n) {
201 return {};
202 }
203 for (int i = 0; i < n; ++i) {
204 pmaps[i] = bitmaps[i].pixmap();
205 }
206 return SkYUVAPixmaps::FromExternalPixmaps(info, pmaps);
207 }
208
209 // All the planes we need to construct the various YUV formats
210 struct PlaneData {
211 SkBitmap fYFull;
212 SkBitmap fUFull;
213 SkBitmap fVFull;
214 SkBitmap fAFull;
215 SkBitmap fUQuarter; // 2x2 downsampled U channel
216 SkBitmap fVQuarter; // 2x2 downsampled V channel
217
218 SkBitmap fFull;
219 SkBitmap fQuarter; // 2x2 downsampled YUVA
220 };
221
222 // Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle
223 // and have tangents 'v1' and 'v2'.
add_arc(SkPath * path,const SkPoint & o1,const SkVector & v1,const SkPoint & o2,const SkVector & v2,SkTDArray<SkRect> * circles,bool takeLongWayRound)224 static void add_arc(SkPath* path,
225 const SkPoint& o1, const SkVector& v1,
226 const SkPoint& o2, const SkVector& v2,
227 SkTDArray<SkRect>* circles, bool takeLongWayRound) {
228
229 SkVector v3 = { -v1.fY, v1.fX };
230 SkVector v4 = { v2.fY, -v2.fX };
231
232 SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4);
233 SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY };
234
235 SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t };
236
237 if (circles) {
238 circles->push_back(r);
239 }
240
241 SkVector startV = o1 - center, endV = o2 - center;
242 startV.normalize();
243 endV.normalize();
244
245 SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX));
246 SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX));
247
248 startDeg += 360.0f;
249 startDeg = fmodf(startDeg, 360.0f);
250
251 endDeg += 360.0f;
252 endDeg = fmodf(endDeg, 360.0f);
253
254 if (endDeg < startDeg) {
255 endDeg += 360.0f;
256 }
257
258 SkScalar sweepDeg = SkTAbs(endDeg - startDeg);
259 if (!takeLongWayRound) {
260 sweepDeg = sweepDeg - 360;
261 }
262
263 path->arcTo(r, startDeg, sweepDeg, false);
264 }
265
create_splat(const SkPoint & o,SkScalar innerRadius,SkScalar outerRadius,SkScalar ratio,int numLobes,SkTDArray<SkRect> * circles)266 static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius,
267 SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) {
268 if (numLobes <= 1) {
269 return SkPath();
270 }
271
272 SkPath p;
273
274 int numDivisions = 2 * numLobes;
275 SkScalar fullLobeDegrees = 360.0f / numLobes;
276 SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f);
277 SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f);
278 SkMatrix outerStep, innerStep;
279 outerStep.setRotate(outDegrees);
280 innerStep.setRotate(innerDegrees);
281 SkVector curV = SkVector::Make(0.0f, 1.0f);
282
283 if (circles) {
284 circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius,
285 o.fX + innerRadius, o.fY + innerRadius));
286 }
287
288 p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY);
289
290 for (int i = 0; i < numDivisions; ++i) {
291
292 SkVector nextV;
293 if (0 == (i % 2)) {
294 nextV = outerStep.mapVector(curV.fX, curV.fY);
295
296 SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX,
297 o.fY + outerRadius * curV.fY);
298 SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX,
299 o.fY + outerRadius * nextV.fY);
300
301 p.lineTo(top);
302 add_arc(&p, top, curV, nextTop, nextV, circles, true);
303 } else {
304 nextV = innerStep.mapVector(curV.fX, curV.fY);
305
306 SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX,
307 o.fY + innerRadius * curV.fY);
308 SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX,
309 o.fY + innerRadius * nextV.fY);
310
311 p.lineTo(bot);
312 add_arc(&p, bot, curV, nextBot, nextV, nullptr, false);
313 }
314
315 curV = nextV;
316 }
317
318 p.close();
319
320 return p;
321 }
322
make_bitmap(SkColorType colorType,const SkPath & path,const SkTDArray<SkRect> & circles,bool opaque,bool padWithRed)323 static SkBitmap make_bitmap(SkColorType colorType, const SkPath& path,
324 const SkTDArray<SkRect>& circles, bool opaque, bool padWithRed) {
325 const SkColor kGreen = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104));
326 const SkColor kBlue = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252));
327 const SkColor kYellow = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117));
328 const SkColor kMagenta = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 60, 217));
329 const SkColor kCyan = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 45, 237, 205));
330
331 int widthHeight = kTileWidthHeight + (padWithRed ? 2 * kSubsetPadding : 0);
332
333 SkImageInfo ii = SkImageInfo::Make(widthHeight, widthHeight,
334 colorType, kPremul_SkAlphaType);
335
336 SkBitmap bm;
337 bm.allocPixels(ii);
338
339 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(ii,
340 bm.getPixels(),
341 bm.rowBytes());
342 if (padWithRed) {
343 canvas->clear(SK_ColorRED);
344 canvas->translate(kSubsetPadding, kSubsetPadding);
345 canvas->clipRect(SkRect::MakeWH(kTileWidthHeight, kTileWidthHeight));
346 }
347 canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT);
348
349 SkPaint paint;
350 paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency
351 paint.setColor(kBlue);
352
353 canvas->drawPath(path, paint);
354
355 paint.setBlendMode(SkBlendMode::kSrc);
356 for (int i = 0; i < circles.count(); ++i) {
357 SkColor color;
358 switch (i % 3) {
359 case 0: color = kYellow; break;
360 case 1: color = kMagenta; break;
361 default: color = kCyan; break;
362 }
363 paint.setColor(color);
364 paint.setAlpha(opaque ? 0xFF : 0x40);
365 SkRect r = circles[i];
366 r.inset(r.width()/4, r.height()/4);
367 canvas->drawOval(r, paint);
368 }
369
370 return bm;
371 }
372
convert_rgba_to_yuva(const float mtx[20],SkColor col,uint8_t yuv[4])373 static void convert_rgba_to_yuva(const float mtx[20], SkColor col, uint8_t yuv[4]) {
374 const uint8_t r = SkColorGetR(col);
375 const uint8_t g = SkColorGetG(col);
376 const uint8_t b = SkColorGetB(col);
377
378 yuv[0] = SkTPin(SkScalarRoundToInt(mtx[ 0]*r + mtx[ 1]*g + mtx[ 2]*b + mtx[ 4]*255), 0, 255);
379 yuv[1] = SkTPin(SkScalarRoundToInt(mtx[ 5]*r + mtx[ 6]*g + mtx[ 7]*b + mtx[ 9]*255), 0, 255);
380 yuv[2] = SkTPin(SkScalarRoundToInt(mtx[10]*r + mtx[11]*g + mtx[12]*b + mtx[14]*255), 0, 255);
381 yuv[3] = SkColorGetA(col);
382 }
383
extract_planes(const SkBitmap & origBM,SkYUVColorSpace yuvColorSpace,SkEncodedOrigin origin,PlaneData * planes)384 static void extract_planes(const SkBitmap& origBM,
385 SkYUVColorSpace yuvColorSpace,
386 SkEncodedOrigin origin,
387 PlaneData* planes) {
388 SkImageInfo ii = origBM.info();
389 if (SkEncodedOriginSwapsWidthHeight(origin)) {
390 ii = ii.makeWH(ii.height(), ii.width());
391 }
392 SkBitmap orientedBM;
393 orientedBM.allocPixels(ii);
394 SkCanvas canvas(orientedBM);
395 SkMatrix matrix = SkEncodedOriginToMatrix(origin, origBM.width(), origBM.height());
396 SkAssertResult(matrix.invert(&matrix));
397 canvas.concat(matrix);
398 canvas.drawImage(origBM.asImage(), 0, 0);
399
400 if (yuvColorSpace == kIdentity_SkYUVColorSpace) {
401 // To test the identity color space we use JPEG YUV planes
402 yuvColorSpace = kJPEG_SkYUVColorSpace;
403 }
404
405 SkASSERT(!(ii.width() % 2));
406 SkASSERT(!(ii.height() % 2));
407 planes->fYFull.allocPixels(
408 SkImageInfo::Make(ii.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
409 planes->fUFull.allocPixels(
410 SkImageInfo::Make(ii.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
411 planes->fVFull.allocPixels(
412 SkImageInfo::Make(ii.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
413 planes->fAFull.allocPixels(SkImageInfo::MakeA8(ii.dimensions()));
414 planes->fUQuarter.allocPixels(SkImageInfo::Make(ii.width()/2, ii.height()/2,
415 kGray_8_SkColorType, kUnpremul_SkAlphaType));
416 planes->fVQuarter.allocPixels(SkImageInfo::Make(ii.width()/2, ii.height()/2,
417 kGray_8_SkColorType, kUnpremul_SkAlphaType));
418
419 planes->fFull.allocPixels(
420 SkImageInfo::Make(ii.dimensions(), kRGBA_F32_SkColorType, kUnpremul_SkAlphaType));
421 planes->fQuarter.allocPixels(SkImageInfo::Make(ii.width()/2, ii.height()/2,
422 kRGBA_F32_SkColorType, kUnpremul_SkAlphaType));
423
424 float mtx[20];
425 SkColorMatrix_RGB2YUV(yuvColorSpace, mtx);
426
427 SkColor4f* dst = (SkColor4f *) planes->fFull.getAddr(0, 0);
428 for (int y = 0; y < orientedBM.height(); ++y) {
429 for (int x = 0; x < orientedBM.width(); ++x) {
430 SkColor col = orientedBM.getColor(x, y);
431
432 uint8_t yuva[4];
433
434 convert_rgba_to_yuva(mtx, col, yuva);
435
436 *planes->fYFull.getAddr8(x, y) = yuva[0];
437 *planes->fUFull.getAddr8(x, y) = yuva[1];
438 *planes->fVFull.getAddr8(x, y) = yuva[2];
439 *planes->fAFull.getAddr8(x, y) = yuva[3];
440
441 // TODO: render in F32 rather than converting here
442 dst->fR = yuva[0] / 255.0f;
443 dst->fG = yuva[1] / 255.0f;
444 dst->fB = yuva[2] / 255.0f;
445 dst->fA = yuva[3] / 255.0f;
446 ++dst;
447 }
448 }
449
450 dst = (SkColor4f *) planes->fQuarter.getAddr(0, 0);
451 for (int y = 0; y < orientedBM.height()/2; ++y) {
452 for (int x = 0; x < orientedBM.width()/2; ++x) {
453 uint32_t yAccum = 0, uAccum = 0, vAccum = 0, aAccum = 0;
454
455 yAccum += *planes->fYFull.getAddr8(2*x, 2*y);
456 yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y);
457 yAccum += *planes->fYFull.getAddr8(2*x, 2*y+1);
458 yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y+1);
459
460 uAccum += *planes->fUFull.getAddr8(2*x, 2*y);
461 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y);
462 uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1);
463 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1);
464
465 *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f;
466
467 vAccum += *planes->fVFull.getAddr8(2*x, 2*y);
468 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y);
469 vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1);
470 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1);
471
472 *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f;
473
474 aAccum += *planes->fAFull.getAddr8(2*x, 2*y);
475 aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y);
476 aAccum += *planes->fAFull.getAddr8(2*x, 2*y+1);
477 aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y+1);
478
479 // TODO: render in F32 rather than converting here
480 dst->fR = yAccum / (4.0f * 255.0f);
481 dst->fG = uAccum / (4.0f * 255.0f);
482 dst->fB = vAccum / (4.0f * 255.0f);
483 dst->fA = aAccum / (4.0f * 255.0f);
484 ++dst;
485 }
486 }
487 }
488
489 // Create a 2x2 downsampled SkBitmap. It is stored in an RG texture. It can optionally be
490 // uv (i.e., NV12) or vu (i.e., NV21).
make_quarter_2_channel(const SkBitmap & fullY,const SkBitmap & quarterU,const SkBitmap & quarterV,bool uv)491 static SkBitmap make_quarter_2_channel(const SkBitmap& fullY,
492 const SkBitmap& quarterU,
493 const SkBitmap& quarterV,
494 bool uv) {
495 SkBitmap result;
496
497 result.allocPixels(SkImageInfo::Make(fullY.width()/2,
498 fullY.height()/2,
499 kR8G8_unorm_SkColorType,
500 kUnpremul_SkAlphaType));
501
502 for (int y = 0; y < fullY.height()/2; ++y) {
503 for (int x = 0; x < fullY.width()/2; ++x) {
504 uint8_t u8 = *quarterU.getAddr8(x, y);
505 uint8_t v8 = *quarterV.getAddr8(x, y);
506
507 if (uv) {
508 *result.getAddr16(x, y) = (v8 << 8) | u8;
509 } else {
510 *result.getAddr16(x, y) = (u8 << 8) | v8;
511 }
512 }
513 }
514
515 return result;
516 }
517
518 // Create some flavor of a 16bits/channel bitmap from a RGBA_F32 source
make_16(const SkBitmap & src,SkColorType dstCT,std::function<void (uint16_t * dstPixel,const float * srcPixel)> convert)519 static SkBitmap make_16(const SkBitmap& src, SkColorType dstCT,
520 std::function<void(uint16_t* dstPixel, const float* srcPixel)> convert) {
521 SkASSERT(src.colorType() == kRGBA_F32_SkColorType);
522
523 SkBitmap result;
524
525 result.allocPixels(SkImageInfo::Make(src.dimensions(), dstCT, kUnpremul_SkAlphaType));
526
527 for (int y = 0; y < src.height(); ++y) {
528 for (int x = 0; x < src.width(); ++x) {
529 const float* srcPixel = (const float*) src.getAddr(x, y);
530 uint16_t* dstPixel = (uint16_t*) result.getAddr(x, y);
531
532 convert(dstPixel, srcPixel);
533 }
534 }
535
536 return result;
537 }
538
flt_2_uint16(float flt)539 static uint16_t flt_2_uint16(float flt) { return SkScalarRoundToInt(flt * 65535.0f); }
540
541 // Recombine the separate planes into some YUV format. Returns the number of planes.
create_YUV(const PlaneData & planes,YUVFormat yuvFormat,SkBitmap resultBMs[],bool opaque)542 static int create_YUV(const PlaneData& planes,
543 YUVFormat yuvFormat,
544 SkBitmap resultBMs[],
545 bool opaque) {
546 int nextLayer = 0;
547
548 switch (yuvFormat) {
549 case kY416_YUVFormat: {
550 resultBMs[nextLayer++] = make_16(planes.fFull, kR16G16B16A16_unorm_SkColorType,
551 [] (uint16_t* dstPixel, const float* srcPixel) {
552 dstPixel[0] = flt_2_uint16(srcPixel[1]); // U
553 dstPixel[1] = flt_2_uint16(srcPixel[0]); // Y
554 dstPixel[2] = flt_2_uint16(srcPixel[2]); // V
555 dstPixel[3] = flt_2_uint16(srcPixel[3]); // A
556 });
557 break;
558 }
559 case kAYUV_YUVFormat: {
560 SkBitmap yuvaFull;
561
562 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
563 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
564
565 for (int y = 0; y < planes.fYFull.height(); ++y) {
566 for (int x = 0; x < planes.fYFull.width(); ++x) {
567
568 uint8_t Y = *planes.fYFull.getAddr8(x, y);
569 uint8_t U = *planes.fUFull.getAddr8(x, y);
570 uint8_t V = *planes.fVFull.getAddr8(x, y);
571 uint8_t A = *planes.fAFull.getAddr8(x, y);
572
573 // NOT premul!
574 // V and Y swapped to match RGBA layout
575 SkColor c = SkColorSetARGB(A, V, U, Y);
576 *yuvaFull.getAddr32(x, y) = c;
577 }
578 }
579
580 resultBMs[nextLayer++] = yuvaFull;
581 break;
582 }
583 case kY410_YUVFormat: {
584 SkBitmap yuvaFull;
585 uint32_t Y, U, V;
586 uint8_t A;
587
588 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
589 kRGBA_1010102_SkColorType,
590 kUnpremul_SkAlphaType));
591
592 for (int y = 0; y < planes.fYFull.height(); ++y) {
593 for (int x = 0; x < planes.fYFull.width(); ++x) {
594
595 Y = SkScalarRoundToInt((*planes.fYFull.getAddr8(x, y) / 255.0f) * 1023.0f);
596 U = SkScalarRoundToInt((*planes.fUFull.getAddr8(x, y) / 255.0f) * 1023.0f);
597 V = SkScalarRoundToInt((*planes.fVFull.getAddr8(x, y) / 255.0f) * 1023.0f);
598 A = SkScalarRoundToInt((*planes.fAFull.getAddr8(x, y) / 255.0f) * 3.0f);
599
600 // NOT premul!
601 *yuvaFull.getAddr32(x, y) = (A << 30) | (V << 20) | (Y << 10) | (U << 0);
602 }
603 }
604
605 resultBMs[nextLayer++] = yuvaFull;
606 break;
607 }
608 case kP016_YUVFormat: // fall through
609 case kP010_YUVFormat: {
610 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType,
611 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
612 (uint16_t* dstPixel, const float* srcPixel) {
613 uint16_t val16 = flt_2_uint16(srcPixel[0]);
614 dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0)
615 : val16;
616 });
617 resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_unorm_SkColorType,
618 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
619 (uint16_t* dstPixel, const float* srcPixel) {
620 uint16_t u16 = flt_2_uint16(srcPixel[1]);
621 uint16_t v16 = flt_2_uint16(srcPixel[2]);
622 dstPixel[0] = tenBitsPP ? (u16 & 0xFFC0) : u16;
623 dstPixel[1] = tenBitsPP ? (v16 & 0xFFC0) : v16;
624 });
625 if (!opaque) {
626 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType,
627 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
628 (uint16_t* dstPixel, const float* srcPixel) {
629 uint16_t val16 = flt_2_uint16(srcPixel[3]);
630 dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0)
631 : val16;
632 });
633 }
634 return nextLayer;
635 }
636 case kP016F_YUVFormat: {
637 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType,
638 [] (uint16_t* dstPixel, const float* srcPixel) {
639 dstPixel[0] = SkFloatToHalf(srcPixel[0]);
640 });
641 resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_float_SkColorType,
642 [] (uint16_t* dstPixel, const float* srcPixel) {
643 dstPixel[0] = SkFloatToHalf(srcPixel[1]);
644 dstPixel[1] = SkFloatToHalf(srcPixel[2]);
645 });
646 if (!opaque) {
647 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType,
648 [] (uint16_t* dstPixel, const float* srcPixel) {
649 dstPixel[0] = SkFloatToHalf(srcPixel[3]);
650 });
651 }
652 return nextLayer;
653 }
654 case kNV12_YUVFormat: {
655 SkBitmap uvQuarter = make_quarter_2_channel(planes.fYFull,
656 planes.fUQuarter,
657 planes.fVQuarter, true);
658 resultBMs[nextLayer++] = planes.fYFull;
659 resultBMs[nextLayer++] = uvQuarter;
660 break;
661 }
662 case kNV21_YUVFormat: {
663 SkBitmap vuQuarter = make_quarter_2_channel(planes.fYFull,
664 planes.fUQuarter,
665 planes.fVQuarter, false);
666 resultBMs[nextLayer++] = planes.fYFull;
667 resultBMs[nextLayer++] = vuQuarter;
668 break;
669 }
670 case kI420_YUVFormat:
671 resultBMs[nextLayer++] = planes.fYFull;
672 resultBMs[nextLayer++] = planes.fUQuarter;
673 resultBMs[nextLayer++] = planes.fVQuarter;
674 break;
675 case kYV12_YUVFormat:
676 resultBMs[nextLayer++] = planes.fYFull;
677 resultBMs[nextLayer++] = planes.fVQuarter;
678 resultBMs[nextLayer++] = planes.fUQuarter;
679 break;
680 }
681
682 if (!opaque && !has_alpha_channel(yuvFormat)) {
683 resultBMs[nextLayer++] = planes.fAFull;
684 }
685 return nextLayer;
686 }
687
draw_col_label(SkCanvas * canvas,int x,int yuvColorSpace,bool opaque)688 static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) {
689 static const char* kYUVColorSpaceNames[] = {"JPEG", "601", "709F", "709L",
690 "2020_8F", "2020_8L", "2020_10F", "2020_10L",
691 "2020_12F", "2020_12L", "Identity"};
692 static_assert(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace + 1);
693
694 SkPaint paint;
695 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
696 font.setEdging(SkFont::Edging::kAlias);
697
698 SkRect textRect;
699 SkString colLabel;
700
701 colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]);
702 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
703 int y = textRect.height();
704
705 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
706
707 colLabel.printf("%s", opaque ? "Opaque" : "Transparent");
708
709 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
710 y += textRect.height();
711
712 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
713 }
714
draw_row_label(SkCanvas * canvas,int y,int yuvFormat)715 static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) {
716 static const char* kYUVFormatNames[] = {
717 "P016", "P010", "P016F", "Y416", "AYUV", "Y410", "NV12", "NV21", "I420", "YV12"
718 };
719 static_assert(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat + 1);
720
721 SkPaint paint;
722 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
723 font.setEdging(SkFont::Edging::kAlias);
724
725 SkRect textRect;
726 SkString rowLabel;
727
728 rowLabel.printf("%s", kYUVFormatNames[yuvFormat]);
729 font.measureText(rowLabel.c_str(), rowLabel.size(), SkTextEncoding::kUTF8, &textRect);
730 y += kTileWidthHeight/2 + textRect.height()/2;
731
732 canvas->drawString(rowLabel, 0, y, font, paint);
733 }
734
yuv_to_rgb_colorfilter()735 static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
736 static const float kJPEGConversionMatrix[20] = {
737 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
738 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
739 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
740 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
741 };
742
743 return SkColorFilters::Matrix(kJPEGConversionMatrix);
744 }
745
746 namespace skiagm {
747
748 // This GM creates an opaque and transparent bitmap, extracts the planes and then recombines
749 // them into various YUV formats. It then renders the results in the grid:
750 //
751 // JPEG 601 709 Identity
752 // Transparent Opaque Transparent Opaque Transparent Opaque Transparent Opaque
753 // originals
754 // P016
755 // P010
756 // P016F
757 // Y416
758 // AYUV
759 // Y410
760 // NV12
761 // NV21
762 // I420
763 // YV12
764 class WackyYUVFormatsGM : public GM {
765 public:
766 using Type = sk_gpu_test::LazyYUVImage::Type;
767
WackyYUVFormatsGM(bool useTargetColorSpace,bool useSubset,Type type)768 WackyYUVFormatsGM(bool useTargetColorSpace, bool useSubset, Type type)
769 : fUseTargetColorSpace(useTargetColorSpace), fUseSubset(useSubset), fImageType(type) {
770 this->setBGColor(0xFFCCCCCC);
771 }
772
773 protected:
onShortName()774 SkString onShortName() override {
775 SkString name("wacky_yuv_formats");
776 if (fUseTargetColorSpace) {
777 name += "_cs";
778 }
779 if (fUseSubset) {
780 name += "_domain";
781 }
782 switch (fImageType) {
783 case Type::kFromPixmaps:
784 name += "_frompixmaps";
785 break;
786 case Type::kFromTextures:
787 break;
788 case Type::kFromGenerator:
789 name += "_imggen";
790 break;
791 }
792
793 return name;
794 }
795
onISize()796 SkISize onISize() override {
797 int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x #-color-spaces
798 int numRows = 1 + (kLast_YUVFormat + 1); // original + #-yuv-formats
799 int wh = SkScalarCeilToInt(kTileWidthHeight * (fUseSubset ? 1.5f : 1.f));
800 return SkISize::Make(kLabelWidth + numCols * (wh + kPad),
801 kLabelHeight + numRows * (wh + kPad));
802 }
803
createBitmaps()804 void createBitmaps() {
805 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
806 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
807 float innerRadius = 20.0f;
808
809 {
810 // transparent
811 SkTDArray<SkRect> circles;
812 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
813 fOriginalBMs[0] = make_bitmap(kRGBA_8888_SkColorType, path, circles, false, fUseSubset);
814 }
815
816 {
817 // opaque
818 SkTDArray<SkRect> circles;
819 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
820 fOriginalBMs[1] = make_bitmap(kRGBA_8888_SkColorType, path, circles, true, fUseSubset);
821 }
822
823 if (fUseTargetColorSpace) {
824 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
825 }
826 }
827
createImages(GrDirectContext * dContext)828 bool createImages(GrDirectContext* dContext) {
829 int origin = 0;
830 for (bool opaque : { false, true }) {
831 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
832 PlaneData planes;
833 extract_planes(fOriginalBMs[opaque],
834 static_cast<SkYUVColorSpace>(cs),
835 static_cast<SkEncodedOrigin>(origin + 1), // valid origins are 1...8
836 &planes);
837
838 for (int f = kP016_YUVFormat; f <= kLast_YUVFormat; ++f) {
839 auto format = static_cast<YUVFormat>(f);
840 SkBitmap resultBMs[4];
841
842 int numPlanes = create_YUV(planes, format, resultBMs, opaque);
843 const YUVAPlanarConfig planarConfig(format,
844 opaque,
845 static_cast<SkEncodedOrigin>(origin + 1));
846 SkYUVAPixmaps pixmaps =
847 planarConfig.makeYUVAPixmaps(fOriginalBMs[opaque].dimensions(),
848 static_cast<SkYUVColorSpace>(cs),
849 resultBMs,
850 numPlanes);
851 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(std::move(pixmaps));
852
853 fImages[opaque][cs][format] = lazyYUV->refImage(dContext, fImageType);
854 }
855 origin = (origin + 1) % 8;
856 }
857 }
858
859 if (dContext) {
860 // Some backends (e.g., Vulkan) require all work be completed for backend textures
861 // before they are deleted. Since we don't know when we'll next have access to a
862 // direct context, flush all the work now.
863 dContext->flush();
864 dContext->submit(true);
865 }
866
867 return true;
868 }
869
onGpuSetup(GrDirectContext * dContext,SkString * errorMsg)870 DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override {
871 this->createBitmaps();
872
873 if (dContext && dContext->abandoned()) {
874 // This isn't a GpuGM so a null 'context' is okay but an abandoned context
875 // if forbidden.
876 return DrawResult::kSkip;
877 }
878
879 // Only the generator is expected to work with the CPU backend.
880 if (fImageType != Type::kFromGenerator && !dContext) {
881 return DrawResult::kSkip;
882 }
883
884 if (!this->createImages(dContext)) {
885 *errorMsg = "Failed to create YUV images";
886 return DrawResult::kFail;
887 }
888
889 return DrawResult::kOk;
890 }
891
onGpuTeardown()892 void onGpuTeardown() override {
893 for (int i = 0; i < 2; ++i) {
894 for (int j = 0; j <= kLastEnum_SkYUVColorSpace; ++j) {
895 for (int k = 0; k <= kLast_YUVFormat; ++k) {
896 fImages[i][j][k] = nullptr;
897 }
898 }
899 }
900 }
901
onDraw(SkCanvas * canvas)902 void onDraw(SkCanvas* canvas) override {
903 auto direct = GrAsDirectContext(canvas->recordingContext());
904
905 float cellWidth = kTileWidthHeight, cellHeight = kTileWidthHeight;
906 if (fUseSubset) {
907 cellWidth *= 1.5f;
908 cellHeight *= 1.5f;
909 }
910
911 SkRect srcRect = SkRect::Make(fOriginalBMs[0].dimensions());
912 SkRect dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, srcRect.width(), srcRect.height());
913
914 SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint;
915 if (fUseSubset) {
916 srcRect.inset(kSubsetPadding, kSubsetPadding);
917 // Draw a larger rectangle to ensure bilerp filtering would normally read outside the
918 // srcRect and hit the red pixels, if strict constraint weren't used.
919 dstRect.fRight = kLabelWidth + 1.5f * srcRect.width();
920 dstRect.fBottom = 1.5f * srcRect.height();
921 constraint = SkCanvas::kStrict_SrcRectConstraint;
922 }
923
924 SkSamplingOptions sampling(SkFilterMode::kLinear);
925 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
926 SkPaint paint;
927 if (kIdentity_SkYUVColorSpace == cs) {
928 // The identity color space needs post processing to appear correctly
929 paint.setColorFilter(yuv_to_rgb_colorfilter());
930 }
931
932 for (int opaque : { 0, 1 }) {
933 dstRect.offsetTo(dstRect.fLeft, kLabelHeight);
934
935 draw_col_label(canvas, dstRect.fLeft + cellWidth / 2, cs, opaque);
936
937 canvas->drawImageRect(fOriginalBMs[opaque].asImage(), srcRect, dstRect,
938 SkSamplingOptions(), nullptr, constraint);
939 dstRect.offset(0.f, cellHeight + kPad);
940
941 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
942 draw_row_label(canvas, dstRect.fTop, format);
943 if (fUseTargetColorSpace && fImages[opaque][cs][format]) {
944 // Making a CS-specific version of a kIdentity_SkYUVColorSpace YUV image
945 // doesn't make a whole lot of sense. The colorSpace conversion will
946 // operate on the YUV components rather than the RGB components.
947 sk_sp<SkImage> csImage =
948 fImages[opaque][cs][format]->makeColorSpace(fTargetColorSpace, direct);
949 canvas->drawImageRect(csImage, srcRect, dstRect, sampling,
950 &paint, constraint);
951 } else {
952 canvas->drawImageRect(fImages[opaque][cs][format], srcRect, dstRect,
953 sampling, &paint, constraint);
954 }
955 dstRect.offset(0.f, cellHeight + kPad);
956 }
957
958 dstRect.offset(cellWidth + kPad, 0.f);
959 }
960 }
961 }
962
963 private:
964 SkBitmap fOriginalBMs[2];
965 sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace + 1][kLast_YUVFormat + 1];
966 bool fUseTargetColorSpace;
967 bool fUseSubset;
968 Type fImageType;
969 sk_sp<SkColorSpace> fTargetColorSpace;
970
971 using INHERITED = GM;
972 };
973
974 //////////////////////////////////////////////////////////////////////////////
975
976 DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
977 /* subset */ false,
978 WackyYUVFormatsGM::Type::kFromTextures);)
979 DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
980 /* subset */ true,
981 WackyYUVFormatsGM::Type::kFromTextures);)
982 DEF_GM(return new WackyYUVFormatsGM(/* target cs */ true,
983 /* subset */ false,
984 WackyYUVFormatsGM::Type::kFromTextures);)
985 DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
986 /* subset */ false,
987 WackyYUVFormatsGM::Type::kFromGenerator);)
988 DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
989 /* subset */ false,
990 WackyYUVFormatsGM::Type::kFromPixmaps);)
991
992 class YUVMakeColorSpaceGM : public GpuGM {
993 public:
YUVMakeColorSpaceGM()994 YUVMakeColorSpaceGM() {
995 this->setBGColor(0xFFCCCCCC);
996 }
997
998 protected:
onShortName()999 SkString onShortName() override {
1000 return SkString("yuv_make_color_space");
1001 }
1002
onISize()1003 SkISize onISize() override {
1004 int numCols = 4; // (transparent, opaque) x (untagged, tagged)
1005 int numRows = 5; // original, YUV, subset, makeNonTextureImage, readPixels
1006 return SkISize::Make(numCols * (kTileWidthHeight + kPad) + kPad,
1007 numRows * (kTileWidthHeight + kPad) + kPad);
1008 }
1009
createBitmaps()1010 void createBitmaps() {
1011 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
1012 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
1013 float innerRadius = 20.0f;
1014
1015 {
1016 // transparent
1017 SkTDArray<SkRect> circles;
1018 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
1019 fOriginalBMs[0] = make_bitmap(kN32_SkColorType, path, circles, false, false);
1020 }
1021
1022 {
1023 // opaque
1024 SkTDArray<SkRect> circles;
1025 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
1026 fOriginalBMs[1] = make_bitmap(kN32_SkColorType, path, circles, true, false);
1027 }
1028
1029 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
1030 }
1031
createImages(GrDirectContext * context)1032 bool createImages(GrDirectContext* context) {
1033 for (bool opaque : { false, true }) {
1034 PlaneData planes;
1035 extract_planes(fOriginalBMs[opaque],
1036 kJPEG_SkYUVColorSpace,
1037 kTopLeft_SkEncodedOrigin,
1038 &planes);
1039
1040 SkBitmap resultBMs[4];
1041
1042 create_YUV(planes, kAYUV_YUVFormat, resultBMs, opaque);
1043
1044 YUVAPlanarConfig planarConfig(kAYUV_YUVFormat, opaque, kTopLeft_SkEncodedOrigin);
1045
1046 auto yuvaPixmaps = planarConfig.makeYUVAPixmaps(fOriginalBMs[opaque].dimensions(),
1047 kJPEG_Full_SkYUVColorSpace,
1048 resultBMs,
1049 SK_ARRAY_COUNT(resultBMs));
1050
1051 int i = 0;
1052 for (sk_sp<SkColorSpace> cs : {sk_sp<SkColorSpace>(nullptr),
1053 SkColorSpace::MakeSRGB()}) {
1054 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(yuvaPixmaps,
1055 GrMipmapped::kNo,
1056 std::move(cs));
1057 fImages[opaque][i++] =
1058 lazyYUV->refImage(context, sk_gpu_test::LazyYUVImage::Type::kFromTextures);
1059 }
1060 }
1061
1062 // Some backends (e.g., Vulkan) require all work be completed for backend textures before
1063 // they are deleted. Since we don't know when we'll next have access to a direct context,
1064 // flush all the work now.
1065 context->flush();
1066 context->submit(true);
1067
1068 return true;
1069 }
1070
onGpuSetup(GrDirectContext * context,SkString * errorMsg)1071 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
1072 if (!context || context->abandoned()) {
1073 return DrawResult::kSkip;
1074 }
1075
1076 this->createBitmaps();
1077 if (!this->createImages(context)) {
1078 *errorMsg = "Failed to create YUV images";
1079 return DrawResult::kFail;
1080 }
1081
1082 return DrawResult::kOk;
1083 }
1084
onGpuTeardown()1085 void onGpuTeardown() override {
1086 fImages[0][0] = fImages[0][1] = fImages[1][0] = fImages[1][1] = nullptr;
1087 }
1088
onDraw(GrRecordingContext * rContext,GrSurfaceDrawContext *,SkCanvas * canvas,SkString * msg)1089 DrawResult onDraw(GrRecordingContext* rContext, GrSurfaceDrawContext*,
1090 SkCanvas* canvas, SkString* msg) override {
1091 SkASSERT(fImages[0][0] && fImages[0][1] && fImages[1][0] && fImages[1][1]);
1092
1093 auto dContext = GrAsDirectContext(rContext);
1094 if (rContext && !dContext) {
1095 *msg = "YUV ColorSpace image creation requires a direct context.";
1096 return DrawResult::kSkip;
1097 }
1098
1099 int x = kPad;
1100 for (int tagged : { 0, 1 }) {
1101 for (int opaque : { 0, 1 }) {
1102 int y = kPad;
1103
1104 auto raster = fOriginalBMs[opaque].asImage()
1105 ->makeColorSpace(fTargetColorSpace, nullptr);
1106 canvas->drawImage(raster, x, y);
1107 y += kTileWidthHeight + kPad;
1108
1109 if (fImages[opaque][tagged]) {
1110 auto yuv = fImages[opaque][tagged]->makeColorSpace(fTargetColorSpace, dContext);
1111 SkASSERT(yuv);
1112 SkASSERT(SkColorSpace::Equals(yuv->colorSpace(), fTargetColorSpace.get()));
1113 canvas->drawImage(yuv, x, y);
1114 y += kTileWidthHeight + kPad;
1115
1116 SkIRect bounds = SkIRect::MakeWH(kTileWidthHeight / 2, kTileWidthHeight / 2);
1117 auto subset = yuv->makeSubset(bounds, dContext);
1118 SkASSERT(subset);
1119 canvas->drawImage(subset, x, y);
1120 y += kTileWidthHeight + kPad;
1121
1122 auto nonTexture = yuv->makeNonTextureImage();
1123 SkASSERT(nonTexture);
1124 canvas->drawImage(nonTexture, x, y);
1125 y += kTileWidthHeight + kPad;
1126
1127 SkBitmap readBack;
1128 readBack.allocPixels(yuv->imageInfo());
1129 SkAssertResult(yuv->readPixels(dContext, readBack.pixmap(), 0, 0));
1130 canvas->drawImage(readBack.asImage(), x, y);
1131 }
1132 x += kTileWidthHeight + kPad;
1133 }
1134 }
1135 return DrawResult::kOk;
1136 }
1137
1138 private:
1139 SkBitmap fOriginalBMs[2];
1140 sk_sp<SkImage> fImages[2][2];
1141 sk_sp<SkColorSpace> fTargetColorSpace;
1142
1143 using INHERITED = GM;
1144 };
1145
1146 DEF_GM(return new YUVMakeColorSpaceGM();)
1147
1148 } // namespace skiagm
1149
1150 ///////////////
1151
1152 #include "include/effects/SkColorMatrix.h"
1153 #include "src/core/SkAutoPixmapStorage.h"
1154 #include "tools/Resources.h"
1155
draw_into_alpha(const SkImage * img,sk_sp<SkColorFilter> cf,const SkPixmap & dst)1156 static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) {
1157 auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
1158 canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height());
1159 SkPaint paint;
1160 paint.setColorFilter(cf);
1161 paint.setBlendMode(SkBlendMode::kSrc);
1162 canvas->drawImage(img, 0, 0, SkSamplingOptions(SkFilterMode::kLinear), &paint);
1163 }
1164
split_into_yuv(const SkImage * img,SkYUVColorSpace cs,const SkPixmap dst[3])1165 static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) {
1166 float m[20];
1167 SkColorMatrix_RGB2YUV(cs, m);
1168
1169 memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A
1170 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]);
1171
1172 memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A
1173 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]);
1174
1175 memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A
1176 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]);
1177 }
1178
draw_diff(SkCanvas * canvas,SkScalar x,SkScalar y,const SkImage * a,const SkImage * b)1179 static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y,
1180 const SkImage* a, const SkImage* b) {
1181 auto sh = SkShaders::Blend(SkBlendMode::kDifference,
1182 a->makeShader(SkSamplingOptions()),
1183 b->makeShader(SkSamplingOptions()));
1184 SkPaint paint;
1185 paint.setShader(sh);
1186 canvas->save();
1187 canvas->translate(x, y);
1188 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1189
1190 SkColorMatrix cm;
1191 cm.setScale(64, 64, 64);
1192 paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm)));
1193 canvas->translate(0, a->height());
1194 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1195
1196 canvas->restore();
1197 }
1198
1199 // Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the
1200 // resulting (recombined) images (gpu only for now).
1201 //
1202 class YUVSplitterGM : public skiagm::GM {
1203 sk_sp<SkImage> fOrig;
1204 SkAutoPixmapStorage fStorage[3];
1205 SkPixmap fPM[3];
1206
1207 public:
YUVSplitterGM()1208 YUVSplitterGM() {}
1209
1210 protected:
1211
onShortName()1212 SkString onShortName() override {
1213 return SkString("yuv_splitter");
1214 }
1215
onISize()1216 SkISize onISize() override {
1217 return SkISize::Make(1280, 768);
1218 }
1219
onOnceBeforeDraw()1220 void onOnceBeforeDraw() override {
1221 fOrig = GetResourceAsImage("images/mandrill_256.png");
1222
1223 SkImageInfo info = SkImageInfo::MakeA8(fOrig->dimensions());
1224 fStorage[0].alloc(info);
1225 fStorage[1].alloc(info);
1226 fStorage[2].alloc(info);
1227 for (int i = 0; i < 3; ++i) {
1228 fPM[i] = fStorage[i];
1229 }
1230 }
1231
onDraw(SkCanvas * canvas)1232 void onDraw(SkCanvas* canvas) override {
1233 canvas->translate(fOrig->width(), 0);
1234 canvas->save();
1235 for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace,
1236 kBT2020_SkYUVColorSpace}) {
1237 split_into_yuv(fOrig.get(), cs, fPM);
1238 SkYUVAInfo yuvaInfo(fOrig->dimensions(),
1239 SkYUVAInfo::PlaneConfig::kY_U_V,
1240 SkYUVAInfo::Subsampling::k444,
1241 cs);
1242 auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, fPM);
1243 auto img = SkImage::MakeFromYUVAPixmaps(canvas->recordingContext(),
1244 yuvaPixmaps,
1245 GrMipMapped::kNo,
1246 /* limit to max tex size */ false,
1247 /* color space */ nullptr);
1248 if (img) {
1249 canvas->drawImage(img, 0, 0);
1250 draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get());
1251 }
1252 canvas->translate(fOrig->width(), 0);
1253 }
1254 canvas->restore();
1255 canvas->translate(-fOrig->width(), 0);
1256
1257 canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0);
1258 canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height());
1259 canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]),
1260 0, fPM[0].height() + fPM[1].height());
1261 }
1262
1263 private:
1264 using INHERITED = GM;
1265 };
1266 DEF_GM( return new YUVSplitterGM; )
1267