blob: c68d8b3af8c2ff0096a2c30e5b2a2df9c4396a83 [file] [log] [blame]
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkColorPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/core/SkImageGenerator.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "include/core/SkImageInfo.h"
23#include "include/core/SkMatrix.h"
24#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "include/core/SkYUVAIndex.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040036#include "include/core/SkYUVASizeInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "include/gpu/GrBackendSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040038#include "include/gpu/GrConfig.h"
Robert Phillipsb87b39b2020-07-01 14:45:24 -040039#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040040#include "include/gpu/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040041#include "include/gpu/GrTypes.h"
42#include "include/private/GrTypesPriv.h"
43#include "include/private/SkTArray.h"
44#include "include/private/SkTDArray.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050045#include "include/private/SkTPin.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040046#include "include/private/SkTemplates.h"
47#include "include/utils/SkTextUtils.h"
Brian Salomon94f65d72020-09-18 14:09:51 -040048#include "src/core/SkConvertPixels.h"
Brian Osmanac8a16c2019-10-31 10:24:12 -040049#include "src/core/SkYUVMath.h"
Robert Phillips95c250c2020-06-29 15:36:12 -040050#include "src/gpu/GrCaps.h"
Robert Phillips95c250c2020-06-29 15:36:12 -040051#include "src/gpu/GrRecordingContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040052#include "tools/ToolUtils.h"
Robert Phillipsf105d382020-06-19 14:27:14 -040053#include "tools/gpu/YUVUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040054
55#include <math.h>
56#include <string.h>
57#include <initializer_list>
58#include <memory>
59#include <utility>
60
Brian Salomoneebe7352020-12-09 16:37:04 -050061class GrSurfaceDrawContext;
Robert Phillips51c89e42018-10-05 13:30:43 -040062
Robert Phillipsbfa76f22018-10-03 12:12:26 -040063static const int kTileWidthHeight = 128;
64static const int kLabelWidth = 64;
65static const int kLabelHeight = 32;
Brian Salomon6c6678f2020-09-21 10:50:30 -040066static const int kSubsetPadding = 8;
Robert Phillipsbfa76f22018-10-03 12:12:26 -040067static const int kPad = 1;
Robert Phillipsbfa76f22018-10-03 12:12:26 -040068
69enum YUVFormat {
Robert Phillipsbb749902019-06-10 17:20:12 -040070 // 4:2:0 formats, 24 bpp
71 kP016_YUVFormat, // 16-bit Y plane + 2x2 down sampled interleaved U/V plane (2 textures)
72 // 4:2:0 formats, "15 bpp" (but really 24 bpp)
73 kP010_YUVFormat, // same as kP016 except "10 bpp". Note that it is the same memory layout
74 // except that the bottom 6 bits are zeroed out (2 textures)
75 // TODO: we're cheating a bit w/ P010 and just treating it as unorm 16. This means its
76 // fully saturated values are 65504 rather than 65535 (that is just .9995 out of 1.0 though).
77
Robert Phillips17a3a0b2019-09-18 13:56:54 -040078 // This is laid out the same as kP016 and kP010 but uses F16 unstead of U16. In this case
79 // the 10 bits/channel vs 16 bits/channel distinction isn't relevant.
80 kP016F_YUVFormat,
81
Robert Phillipsbb749902019-06-10 17:20:12 -040082 // 4:4:4 formats, 64 bpp
83 kY416_YUVFormat, // 16-bit AVYU values all interleaved (1 texture)
84
Robert Phillipsbfa76f22018-10-03 12:12:26 -040085 // 4:4:4 formats, 32 bpp
Robert Phillipsbb749902019-06-10 17:20:12 -040086 kAYUV_YUVFormat, // 8-bit YUVA values all interleaved (1 texture)
87 kY410_YUVFormat, // AVYU w/ 10bpp for YUV and 2 for A all interleaved (1 texture)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040088
89 // 4:2:0 formats, 12 bpp
Robert Phillipsbb749902019-06-10 17:20:12 -040090 kNV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled interleaved U/V planes (2 textures)
91 kNV21_YUVFormat, // same as kNV12 but w/ U/V reversed in the interleaved texture (2 textures)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040092
Robert Phillipsbb749902019-06-10 17:20:12 -040093 kI420_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled U and V planes (3 textures)
94 kYV12_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled V and U planes (3 textures)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040095
96 kLast_YUVFormat = kYV12_YUVFormat
97};
98
Brian Salomon94f65d72020-09-18 14:09:51 -040099// Does the YUVFormat contain a slot for alpha? If not an external alpha plane is required for
100// transparency.
101static bool has_alpha_channel(YUVFormat format) {
102 switch (format) {
103 case kP016_YUVFormat: return false;
104 case kP010_YUVFormat: return false;
105 case kP016F_YUVFormat: return false;
106 case kY416_YUVFormat: return true;
107 case kAYUV_YUVFormat: return true;
108 case kY410_YUVFormat: return true;
109 case kNV12_YUVFormat: return false;
110 case kNV21_YUVFormat: return false;
111 case kI420_YUVFormat: return false;
112 case kYV12_YUVFormat: return false;
113 }
114 SkUNREACHABLE;
115}
116
Brian Salomonf7255d72020-04-08 15:57:08 -0400117class YUVAPlanarConfig {
118public:
Brian Salomon94f65d72020-09-18 14:09:51 -0400119 YUVAPlanarConfig(YUVFormat format, bool opaque) {
120 switch (format) {
121 case kP016_YUVFormat:
122 case kP010_YUVFormat:
123 case kP016F_YUVFormat:
124 case kNV12_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400125 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500126 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_UV;
127 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400128 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500129 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_UV_A;
130 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400131 }
132 break;
133 case kY416_YUVFormat:
134 case kY410_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400135 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500136 fPlaneConfig = SkYUVAInfo::PlaneConfig::kUYV;
137 fSubsampling = SkYUVAInfo::Subsampling::k444;
Brian Salomon94f65d72020-09-18 14:09:51 -0400138 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500139 fPlaneConfig = SkYUVAInfo::PlaneConfig::kUYVA;
140 fSubsampling = SkYUVAInfo::Subsampling::k444;
Brian Salomon94f65d72020-09-18 14:09:51 -0400141 }
142 break;
143 case kAYUV_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400144 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500145 fPlaneConfig = SkYUVAInfo::PlaneConfig::kYUV;
146 fSubsampling = SkYUVAInfo::Subsampling::k444;
Brian Salomon94f65d72020-09-18 14:09:51 -0400147 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500148 fPlaneConfig = SkYUVAInfo::PlaneConfig::kYUVA;
149 fSubsampling = SkYUVAInfo::Subsampling::k444;
Brian Salomon94f65d72020-09-18 14:09:51 -0400150 }
151 break;
152 case kNV21_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400153 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500154 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_VU;
155 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400156 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500157 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_VU_A;
158 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400159 }
160 break;
161 case kI420_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400162 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500163 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_U_V;
164 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400165 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500166 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_U_V_A;
167 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400168 }
169 break;
170 case kYV12_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400171 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500172 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_V_U;
173 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400174 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500175 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_V_U_A;
176 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400177 }
178 break;
179 }
180 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400181
Brian Salomone4387382020-11-11 16:34:19 -0500182 int numPlanes() const { return SkYUVAInfo::NumPlanes(fPlaneConfig); }
Brian Salomonf7255d72020-04-08 15:57:08 -0400183
Brian Salomon6c6678f2020-09-21 10:50:30 -0400184 SkYUVAPixmaps makeYUVAPixmaps(SkISize dimensions,
185 SkYUVColorSpace yuvColorSpace,
186 const SkBitmap bitmaps[],
187 int numBitmaps) const;
188
Brian Salomonf7255d72020-04-08 15:57:08 -0400189private:
Brian Salomone4387382020-11-11 16:34:19 -0500190 SkYUVAInfo::PlaneConfig fPlaneConfig;
191 SkYUVAInfo::Subsampling fSubsampling;
Brian Salomonf7255d72020-04-08 15:57:08 -0400192};
193
Brian Salomon6c6678f2020-09-21 10:50:30 -0400194SkYUVAPixmaps YUVAPlanarConfig::makeYUVAPixmaps(SkISize dimensions,
195 SkYUVColorSpace yuvColorSpace,
196 const SkBitmap bitmaps[],
197 int numBitmaps) const {
Brian Salomone4387382020-11-11 16:34:19 -0500198 SkYUVAInfo info(dimensions, fPlaneConfig, fSubsampling, yuvColorSpace);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400199 SkPixmap pmaps[SkYUVAInfo::kMaxPlanes];
200 int n = info.numPlanes();
201 if (numBitmaps < n) {
202 return {};
203 }
204 for (int i = 0; i < n; ++i) {
205 pmaps[i] = bitmaps[i].pixmap();
206 }
207 return SkYUVAPixmaps::FromExternalPixmaps(info, pmaps);
208}
209
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400210// All the planes we need to construct the various YUV formats
211struct PlaneData {
212 SkBitmap fYFull;
213 SkBitmap fUFull;
214 SkBitmap fVFull;
215 SkBitmap fAFull;
216 SkBitmap fUQuarter; // 2x2 downsampled U channel
217 SkBitmap fVQuarter; // 2x2 downsampled V channel
Robert Phillips429f0d32019-09-11 17:03:28 -0400218
219 SkBitmap fFull;
220 SkBitmap fQuarter; // 2x2 downsampled YUVA
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400221};
222
223// Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle
224// and have tangents 'v1' and 'v2'.
225static void add_arc(SkPath* path,
226 const SkPoint& o1, const SkVector& v1,
227 const SkPoint& o2, const SkVector& v2,
228 SkTDArray<SkRect>* circles, bool takeLongWayRound) {
229
230 SkVector v3 = { -v1.fY, v1.fX };
231 SkVector v4 = { v2.fY, -v2.fX };
232
233 SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4);
234 SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY };
235
236 SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t };
237
238 if (circles) {
239 circles->push_back(r);
240 }
241
242 SkVector startV = o1 - center, endV = o2 - center;
243 startV.normalize();
244 endV.normalize();
245
246 SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX));
247 SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX));
248
249 startDeg += 360.0f;
250 startDeg = fmodf(startDeg, 360.0f);
251
252 endDeg += 360.0f;
253 endDeg = fmodf(endDeg, 360.0f);
254
255 if (endDeg < startDeg) {
256 endDeg += 360.0f;
257 }
258
259 SkScalar sweepDeg = SkTAbs(endDeg - startDeg);
260 if (!takeLongWayRound) {
261 sweepDeg = sweepDeg - 360;
262 }
263
264 path->arcTo(r, startDeg, sweepDeg, false);
265}
266
267static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius,
268 SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) {
269 if (numLobes <= 1) {
270 return SkPath();
271 }
272
273 SkPath p;
274
275 int numDivisions = 2 * numLobes;
276 SkScalar fullLobeDegrees = 360.0f / numLobes;
277 SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f);
278 SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f);
279 SkMatrix outerStep, innerStep;
280 outerStep.setRotate(outDegrees);
281 innerStep.setRotate(innerDegrees);
282 SkVector curV = SkVector::Make(0.0f, 1.0f);
283
284 if (circles) {
285 circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius,
286 o.fX + innerRadius, o.fY + innerRadius));
287 }
288
289 p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY);
290
291 for (int i = 0; i < numDivisions; ++i) {
292
293 SkVector nextV;
294 if (0 == (i % 2)) {
295 nextV = outerStep.mapVector(curV.fX, curV.fY);
296
297 SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX,
298 o.fY + outerRadius * curV.fY);
299 SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX,
300 o.fY + outerRadius * nextV.fY);
301
302 p.lineTo(top);
303 add_arc(&p, top, curV, nextTop, nextV, circles, true);
304 } else {
305 nextV = innerStep.mapVector(curV.fX, curV.fY);
306
307 SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX,
308 o.fY + innerRadius * curV.fY);
309 SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX,
310 o.fY + innerRadius * nextV.fY);
311
312 p.lineTo(bot);
313 add_arc(&p, bot, curV, nextBot, nextV, nullptr, false);
314 }
315
316 curV = nextV;
317 }
318
319 p.close();
320
321 return p;
322}
323
Robert Phillips2dd1b472019-03-21 09:00:20 -0400324static SkBitmap make_bitmap(SkColorType colorType, const SkPath& path,
Michael Ludwiga6a84002019-04-12 15:03:02 -0400325 const SkTDArray<SkRect>& circles, bool opaque, bool padWithRed) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500326 const SkColor kGreen = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104));
327 const SkColor kBlue = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252));
328 const SkColor kYellow = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400329
Brian Salomon6c6678f2020-09-21 10:50:30 -0400330 int widthHeight = kTileWidthHeight + (padWithRed ? 2 * kSubsetPadding : 0);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400331
332 SkImageInfo ii = SkImageInfo::Make(widthHeight, widthHeight,
Robert Phillips2dd1b472019-03-21 09:00:20 -0400333 colorType, kPremul_SkAlphaType);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400334
335 SkBitmap bm;
336 bm.allocPixels(ii);
337
Robert Phillips2dd1b472019-03-21 09:00:20 -0400338 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(ii,
339 bm.getPixels(),
340 bm.rowBytes());
Michael Ludwiga6a84002019-04-12 15:03:02 -0400341 if (padWithRed) {
342 canvas->clear(SK_ColorRED);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400343 canvas->translate(kSubsetPadding, kSubsetPadding);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400344 canvas->clipRect(SkRect::MakeWH(kTileWidthHeight, kTileWidthHeight));
345 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400346 canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT);
347
348 SkPaint paint;
349 paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency
350 paint.setColor(kBlue);
351
352 canvas->drawPath(path, paint);
353
354 paint.setColor(opaque ? kYellow : SK_ColorTRANSPARENT);
355 paint.setBlendMode(SkBlendMode::kSrc);
356 for (int i = 0; i < circles.count(); ++i) {
357 SkRect r = circles[i];
358 r.inset(r.width()/4, r.height()/4);
359 canvas->drawOval(r, paint);
360 }
361
362 return bm;
363}
364
Brian Osmanac8a16c2019-10-31 10:24:12 -0400365static void convert_rgba_to_yuva(const float mtx[20], SkColor col, uint8_t yuv[4]) {
366 const uint8_t r = SkColorGetR(col);
367 const uint8_t g = SkColorGetG(col);
368 const uint8_t b = SkColorGetB(col);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400369
Brian Osmanaba642c2020-02-06 12:52:25 -0500370 yuv[0] = SkTPin(SkScalarRoundToInt(mtx[ 0]*r + mtx[ 1]*g + mtx[ 2]*b + mtx[ 4]*255), 0, 255);
371 yuv[1] = SkTPin(SkScalarRoundToInt(mtx[ 5]*r + mtx[ 6]*g + mtx[ 7]*b + mtx[ 9]*255), 0, 255);
372 yuv[2] = SkTPin(SkScalarRoundToInt(mtx[10]*r + mtx[11]*g + mtx[12]*b + mtx[14]*255), 0, 255);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400373 yuv[3] = SkColorGetA(col);
374}
375
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400376static void extract_planes(const SkBitmap& bm, SkYUVColorSpace yuvColorSpace, PlaneData* planes) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500377 if (kIdentity_SkYUVColorSpace == yuvColorSpace) {
378 // To test the identity color space we use JPEG YUV planes
379 yuvColorSpace = kJPEG_SkYUVColorSpace;
380 }
381
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400382 SkASSERT(!(bm.width() % 2));
383 SkASSERT(!(bm.height() % 2));
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400384 planes->fYFull.allocPixels(
385 SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
386 planes->fUFull.allocPixels(
387 SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
388 planes->fVFull.allocPixels(
389 SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400390 planes->fAFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400391 planes->fUQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2,
392 kGray_8_SkColorType, kUnpremul_SkAlphaType));
393 planes->fVQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2,
394 kGray_8_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400395
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400396 planes->fFull.allocPixels(
397 SkImageInfo::Make(bm.dimensions(), kRGBA_F32_SkColorType, kUnpremul_SkAlphaType));
Robert Phillips429f0d32019-09-11 17:03:28 -0400398 planes->fQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2,
399 kRGBA_F32_SkColorType, kUnpremul_SkAlphaType));
400
Brian Osmanac8a16c2019-10-31 10:24:12 -0400401 float mtx[20];
402 SkColorMatrix_RGB2YUV(yuvColorSpace, mtx);
403
Robert Phillips429f0d32019-09-11 17:03:28 -0400404 SkColor4f* dst = (SkColor4f *) planes->fFull.getAddr(0, 0);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400405 for (int y = 0; y < bm.height(); ++y) {
406 for (int x = 0; x < bm.width(); ++x) {
407 SkColor col = bm.getColor(x, y);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400408
Robert Phillips1c7062d2018-10-04 10:44:53 -0400409 uint8_t yuva[4];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400410
Brian Osmanac8a16c2019-10-31 10:24:12 -0400411 convert_rgba_to_yuva(mtx, col, yuva);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400412
413 *planes->fYFull.getAddr8(x, y) = yuva[0];
414 *planes->fUFull.getAddr8(x, y) = yuva[1];
415 *planes->fVFull.getAddr8(x, y) = yuva[2];
416 *planes->fAFull.getAddr8(x, y) = yuva[3];
Robert Phillips429f0d32019-09-11 17:03:28 -0400417
418 // TODO: render in F32 rather than converting here
419 dst->fR = yuva[0] / 255.0f;
420 dst->fG = yuva[1] / 255.0f;
421 dst->fB = yuva[2] / 255.0f;
422 dst->fA = yuva[3] / 255.0f;
423 ++dst;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400424 }
425 }
426
Robert Phillips429f0d32019-09-11 17:03:28 -0400427 dst = (SkColor4f *) planes->fQuarter.getAddr(0, 0);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400428 for (int y = 0; y < bm.height()/2; ++y) {
429 for (int x = 0; x < bm.width()/2; ++x) {
Robert Phillips429f0d32019-09-11 17:03:28 -0400430 uint32_t yAccum = 0, uAccum = 0, vAccum = 0, aAccum = 0;
431
432 yAccum += *planes->fYFull.getAddr8(2*x, 2*y);
433 yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y);
434 yAccum += *planes->fYFull.getAddr8(2*x, 2*y+1);
435 yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y+1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400436
437 uAccum += *planes->fUFull.getAddr8(2*x, 2*y);
438 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y);
439 uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1);
440 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1);
441
442 *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f;
443
444 vAccum += *planes->fVFull.getAddr8(2*x, 2*y);
445 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y);
446 vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1);
447 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1);
448
Robert Phillips1c7062d2018-10-04 10:44:53 -0400449 *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f;
Robert Phillips429f0d32019-09-11 17:03:28 -0400450
451 aAccum += *planes->fAFull.getAddr8(2*x, 2*y);
452 aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y);
453 aAccum += *planes->fAFull.getAddr8(2*x, 2*y+1);
454 aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y+1);
455
456 // TODO: render in F32 rather than converting here
457 dst->fR = yAccum / (4.0f * 255.0f);
458 dst->fG = uAccum / (4.0f * 255.0f);
459 dst->fB = vAccum / (4.0f * 255.0f);
460 dst->fA = aAccum / (4.0f * 255.0f);
461 ++dst;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400462 }
463 }
464}
465
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400466// Create a 2x2 downsampled SkBitmap. It is stored in an RG texture. It can optionally be
Robert Phillips429f0d32019-09-11 17:03:28 -0400467// uv (i.e., NV12) or vu (i.e., NV21).
Robert Phillipsbb749902019-06-10 17:20:12 -0400468static SkBitmap make_quarter_2_channel(const SkBitmap& fullY,
469 const SkBitmap& quarterU,
470 const SkBitmap& quarterV,
471 bool uv) {
472 SkBitmap result;
473
Robert Phillipsbb749902019-06-10 17:20:12 -0400474 result.allocPixels(SkImageInfo::Make(fullY.width()/2,
475 fullY.height()/2,
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400476 kR8G8_unorm_SkColorType,
Robert Phillipsbb749902019-06-10 17:20:12 -0400477 kUnpremul_SkAlphaType));
478
479 for (int y = 0; y < fullY.height()/2; ++y) {
480 for (int x = 0; x < fullY.width()/2; ++x) {
481 uint8_t u8 = *quarterU.getAddr8(x, y);
482 uint8_t v8 = *quarterV.getAddr8(x, y);
483
484 if (uv) {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400485 *result.getAddr16(x, y) = (v8 << 8) | u8;
Robert Phillipsbb749902019-06-10 17:20:12 -0400486 } else {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400487 *result.getAddr16(x, y) = (u8 << 8) | v8;
Robert Phillipsbb749902019-06-10 17:20:12 -0400488 }
489 }
490 }
491
492 return result;
493}
494
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400495// Create some flavor of a 16bits/channel bitmap from a RGBA_F32 source
496static SkBitmap make_16(const SkBitmap& src, SkColorType dstCT,
497 std::function<void(uint16_t* dstPixel, const float* srcPixel)> convert) {
Robert Phillips429f0d32019-09-11 17:03:28 -0400498 SkASSERT(src.colorType() == kRGBA_F32_SkColorType);
499
500 SkBitmap result;
501
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400502 result.allocPixels(SkImageInfo::Make(src.dimensions(), dstCT, kUnpremul_SkAlphaType));
Robert Phillips429f0d32019-09-11 17:03:28 -0400503
Robert Phillips429f0d32019-09-11 17:03:28 -0400504 for (int y = 0; y < src.height(); ++y) {
505 for (int x = 0; x < src.width(); ++x) {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400506 const float* srcPixel = (const float*) src.getAddr(x, y);
507 uint16_t* dstPixel = (uint16_t*) result.getAddr(x, y);
Robert Phillips429f0d32019-09-11 17:03:28 -0400508
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400509 convert(dstPixel, srcPixel);
Robert Phillips429f0d32019-09-11 17:03:28 -0400510 }
511 }
512
513 return result;
514}
515
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400516static uint16_t flt_2_uint16(float flt) { return SkScalarRoundToInt(flt * 65535.0f); }
Robert Phillips429f0d32019-09-11 17:03:28 -0400517
Brian Salomonf7255d72020-04-08 15:57:08 -0400518// Recombine the separate planes into some YUV format. Returns the number of planes.
519static int create_YUV(const PlaneData& planes,
520 YUVFormat yuvFormat,
521 SkBitmap resultBMs[],
522 bool opaque) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400523 int nextLayer = 0;
524
525 switch (yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400526 case kY416_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400527 resultBMs[nextLayer++] = make_16(planes.fFull, kR16G16B16A16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400528 [] (uint16_t* dstPixel, const float* srcPixel) {
529 dstPixel[0] = flt_2_uint16(srcPixel[1]); // U
530 dstPixel[1] = flt_2_uint16(srcPixel[0]); // Y
531 dstPixel[2] = flt_2_uint16(srcPixel[2]); // V
532 dstPixel[3] = flt_2_uint16(srcPixel[3]); // A
533 });
Robert Phillipsbb749902019-06-10 17:20:12 -0400534 break;
535 }
Jim Van Verth976a6b02018-10-17 15:27:19 -0400536 case kAYUV_YUVFormat: {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400537 SkBitmap yuvaFull;
538
Jim Van Verth47133fd2018-10-19 22:09:28 -0400539 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
540 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400541
542 for (int y = 0; y < planes.fYFull.height(); ++y) {
543 for (int x = 0; x < planes.fYFull.width(); ++x) {
544
545 uint8_t Y = *planes.fYFull.getAddr8(x, y);
546 uint8_t U = *planes.fUFull.getAddr8(x, y);
547 uint8_t V = *planes.fVFull.getAddr8(x, y);
548 uint8_t A = *planes.fAFull.getAddr8(x, y);
549
550 // NOT premul!
Jim Van Verth47133fd2018-10-19 22:09:28 -0400551 // V and Y swapped to match RGBA layout
Robert Phillips2dd1b472019-03-21 09:00:20 -0400552 SkColor c = SkColorSetARGB(A, V, U, Y);
553 *yuvaFull.getAddr32(x, y) = c;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400554 }
555 }
556
557 resultBMs[nextLayer++] = yuvaFull;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400558 break;
559 }
Robert Phillips2dd1b472019-03-21 09:00:20 -0400560 case kY410_YUVFormat: {
561 SkBitmap yuvaFull;
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400562 uint32_t Y, U, V;
563 uint8_t A;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400564
565 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
566 kRGBA_1010102_SkColorType,
567 kUnpremul_SkAlphaType));
568
569 for (int y = 0; y < planes.fYFull.height(); ++y) {
570 for (int x = 0; x < planes.fYFull.width(); ++x) {
571
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400572 Y = SkScalarRoundToInt((*planes.fYFull.getAddr8(x, y) / 255.0f) * 1023.0f);
573 U = SkScalarRoundToInt((*planes.fUFull.getAddr8(x, y) / 255.0f) * 1023.0f);
574 V = SkScalarRoundToInt((*planes.fVFull.getAddr8(x, y) / 255.0f) * 1023.0f);
575 A = SkScalarRoundToInt((*planes.fAFull.getAddr8(x, y) / 255.0f) * 3.0f);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400576
577 // NOT premul!
Brian Salomonf7255d72020-04-08 15:57:08 -0400578 *yuvaFull.getAddr32(x, y) = (A << 30) | (V << 20) | (Y << 10) | (U << 0);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400579 }
580 }
581
582 resultBMs[nextLayer++] = yuvaFull;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400583 break;
584 }
Robert Phillipsbb749902019-06-10 17:20:12 -0400585 case kP016_YUVFormat: // fall through
Robert Phillips429f0d32019-09-11 17:03:28 -0400586 case kP010_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400587 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400588 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
589 (uint16_t* dstPixel, const float* srcPixel) {
590 uint16_t val16 = flt_2_uint16(srcPixel[0]);
591 dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0)
592 : val16;
593 });
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400594 resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400595 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
596 (uint16_t* dstPixel, const float* srcPixel) {
597 uint16_t u16 = flt_2_uint16(srcPixel[1]);
598 uint16_t v16 = flt_2_uint16(srcPixel[2]);
599 dstPixel[0] = tenBitsPP ? (u16 & 0xFFC0) : u16;
600 dstPixel[1] = tenBitsPP ? (v16 & 0xFFC0) : v16;
601 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400602 if (!opaque) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400603 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType,
604 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
605 (uint16_t* dstPixel, const float* srcPixel) {
606 uint16_t val16 = flt_2_uint16(srcPixel[3]);
607 dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0)
608 : val16;
609 });
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400610 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400611 return nextLayer;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400612 }
613 case kP016F_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400614 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400615 [] (uint16_t* dstPixel, const float* srcPixel) {
616 dstPixel[0] = SkFloatToHalf(srcPixel[0]);
617 });
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400618 resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_float_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400619 [] (uint16_t* dstPixel, const float* srcPixel) {
620 dstPixel[0] = SkFloatToHalf(srcPixel[1]);
621 dstPixel[1] = SkFloatToHalf(srcPixel[2]);
622 });
623 if (!opaque) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400624 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType,
625 [] (uint16_t* dstPixel, const float* srcPixel) {
626 dstPixel[0] = SkFloatToHalf(srcPixel[3]);
627 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400628 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400629 return nextLayer;
Robert Phillips429f0d32019-09-11 17:03:28 -0400630 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400631 case kNV12_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400632 SkBitmap uvQuarter = make_quarter_2_channel(planes.fYFull,
633 planes.fUQuarter,
634 planes.fVQuarter, true);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400635 resultBMs[nextLayer++] = planes.fYFull;
636 resultBMs[nextLayer++] = uvQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400637 break;
638 }
639 case kNV21_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400640 SkBitmap vuQuarter = make_quarter_2_channel(planes.fYFull,
641 planes.fUQuarter,
642 planes.fVQuarter, false);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400643 resultBMs[nextLayer++] = planes.fYFull;
644 resultBMs[nextLayer++] = vuQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400645 break;
646 }
647 case kI420_YUVFormat:
648 resultBMs[nextLayer++] = planes.fYFull;
649 resultBMs[nextLayer++] = planes.fUQuarter;
650 resultBMs[nextLayer++] = planes.fVQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400651 break;
652 case kYV12_YUVFormat:
653 resultBMs[nextLayer++] = planes.fYFull;
654 resultBMs[nextLayer++] = planes.fVQuarter;
655 resultBMs[nextLayer++] = planes.fUQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400656 break;
657 }
658
Brian Salomon94f65d72020-09-18 14:09:51 -0400659 if (!opaque && !has_alpha_channel(yuvFormat)) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400660 resultBMs[nextLayer++] = planes.fAFull;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400661 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400662 return nextLayer;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400663}
664
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400665static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) {
Brian Osmanb107a112020-07-23 14:22:22 -0400666 static const char* kYUVColorSpaceNames[] = {"JPEG", "601", "709F", "709L",
667 "2020_8F", "2020_8L", "2020_10F", "2020_10L",
668 "2020_12F", "2020_12L", "Identity"};
Brian Salomon4dea72a2019-12-18 10:43:10 -0500669 static_assert(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace + 1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400670
Mike Reed91919132019-01-02 12:21:01 -0500671 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500672 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500673 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400674
675 SkRect textRect;
676 SkString colLabel;
677
678 colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400679 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400680 int y = textRect.height();
681
Mike Reed91919132019-01-02 12:21:01 -0500682 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400683
684 colLabel.printf("%s", opaque ? "Opaque" : "Transparent");
Mike Reed71f5a0b2018-10-25 16:12:39 -0400685
Ben Wagner51e15a62019-05-07 15:38:46 -0400686 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400687 y += textRect.height();
688
Mike Reed91919132019-01-02 12:21:01 -0500689 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400690}
691
692static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400693 static const char* kYUVFormatNames[] = {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400694 "P016", "P010", "P016F", "Y416", "AYUV", "Y410", "NV12", "NV21", "I420", "YV12"
Robert Phillipsbb749902019-06-10 17:20:12 -0400695 };
Brian Salomon4dea72a2019-12-18 10:43:10 -0500696 static_assert(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat + 1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400697
Mike Reed91919132019-01-02 12:21:01 -0500698 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500699 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500700 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400701
702 SkRect textRect;
703 SkString rowLabel;
704
705 rowLabel.printf("%s", kYUVFormatNames[yuvFormat]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400706 font.measureText(rowLabel.c_str(), rowLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400707 y += kTileWidthHeight/2 + textRect.height()/2;
708
Hal Canary89a644b2019-01-07 09:36:09 -0500709 canvas->drawString(rowLabel, 0, y, font, paint);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400710}
711
Robert Phillips0a22ba82019-03-06 12:36:47 -0500712static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
713 static const float kJPEGConversionMatrix[20] = {
Mike Reede869a1e2019-04-30 12:18:54 -0400714 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
715 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
716 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
Robert Phillips0a22ba82019-03-06 12:36:47 -0500717 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
718 };
719
Mike Reede869a1e2019-04-30 12:18:54 -0400720 return SkColorFilters::Matrix(kJPEGConversionMatrix);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500721}
722
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400723namespace skiagm {
724
725// This GM creates an opaque and transparent bitmap, extracts the planes and then recombines
726// them into various YUV formats. It then renders the results in the grid:
727//
Robert Phillips2dd1b472019-03-21 09:00:20 -0400728// JPEG 601 709 Identity
729// Transparent Opaque Transparent Opaque Transparent Opaque Transparent Opaque
Robert Phillipsbb749902019-06-10 17:20:12 -0400730// originals
731// P016
732// P010
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400733// P016F
Robert Phillipsbb749902019-06-10 17:20:12 -0400734// Y416
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400735// AYUV
Robert Phillips2dd1b472019-03-21 09:00:20 -0400736// Y410
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400737// NV12
738// NV21
739// I420
740// YV12
741class WackyYUVFormatsGM : public GM {
742public:
Brian Salomon7db71392020-10-16 10:05:21 -0400743 using Type = sk_gpu_test::LazyYUVImage::Type;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400744
Brian Salomon7db71392020-10-16 10:05:21 -0400745 WackyYUVFormatsGM(bool useTargetColorSpace, bool useSubset, Type type)
746 : fUseTargetColorSpace(useTargetColorSpace), fUseSubset(useSubset), fImageType(type) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400747 this->setBGColor(0xFFCCCCCC);
748 }
749
750protected:
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400751 SkString onShortName() override {
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500752 SkString name("wacky_yuv_formats");
753 if (fUseTargetColorSpace) {
754 name += "_cs";
755 }
Brian Salomon6c6678f2020-09-21 10:50:30 -0400756 if (fUseSubset) {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400757 name += "_domain";
758 }
Brian Salomon6c6678f2020-09-21 10:50:30 -0400759 switch (fImageType) {
Brian Salomon7db71392020-10-16 10:05:21 -0400760 case Type::kFromPixmaps:
Brian Salomon6c6678f2020-09-21 10:50:30 -0400761 name += "_frompixmaps";
762 break;
Brian Salomon7db71392020-10-16 10:05:21 -0400763 case Type::kFromTextures:
Brian Salomon6c6678f2020-09-21 10:50:30 -0400764 break;
Brian Salomon7db71392020-10-16 10:05:21 -0400765 case Type::kFromGenerator:
Brian Salomon6c6678f2020-09-21 10:50:30 -0400766 name += "_imggen";
767 break;
Brian Salomon94f65d72020-09-18 14:09:51 -0400768 }
Robert Phillips2dd1b472019-03-21 09:00:20 -0400769
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500770 return name;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400771 }
772
773 SkISize onISize() override {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400774 int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x #-color-spaces
775 int numRows = 1 + (kLast_YUVFormat + 1); // original + #-yuv-formats
Brian Salomon6c6678f2020-09-21 10:50:30 -0400776 int wh = SkScalarCeilToInt(kTileWidthHeight * (fUseSubset ? 1.5f : 1.f));
Michael Ludwiga6a84002019-04-12 15:03:02 -0400777 return SkISize::Make(kLabelWidth + numCols * (wh + kPad),
778 kLabelHeight + numRows * (wh + kPad));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400779 }
780
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400781 void createBitmaps() {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400782 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
783 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
784 float innerRadius = 20.0f;
785
786 {
787 // transparent
788 SkTDArray<SkRect> circles;
789 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400790 fOriginalBMs[0] = make_bitmap(kRGBA_8888_SkColorType, path, circles, false, fUseSubset);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400791 }
792
793 {
794 // opaque
795 SkTDArray<SkRect> circles;
796 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400797 fOriginalBMs[1] = make_bitmap(kRGBA_8888_SkColorType, path, circles, true, fUseSubset);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400798 }
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500799
800 if (fUseTargetColorSpace) {
801 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
802 }
Robert Phillips51c89e42018-10-05 13:30:43 -0400803 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400804
Robert Phillips057c33f2020-07-17 11:59:01 -0400805 bool createImages(GrDirectContext* dContext) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400806 for (bool opaque : { false, true }) {
807 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
808 PlaneData planes;
809 extract_planes(fOriginalBMs[opaque], (SkYUVColorSpace) cs, &planes);
810
Brian Salomonf7255d72020-04-08 15:57:08 -0400811 for (int f = kP016_YUVFormat; f <= kLast_YUVFormat; ++f) {
812 auto format = static_cast<YUVFormat>(f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400813 SkBitmap resultBMs[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -0400814
Brian Salomon6c6678f2020-09-21 10:50:30 -0400815 int numPlanes = create_YUV(planes, format, resultBMs, opaque);
Brian Salomon94f65d72020-09-18 14:09:51 -0400816 const YUVAPlanarConfig planarConfig(format, opaque);
Brian Salomon7db71392020-10-16 10:05:21 -0400817 SkYUVAPixmaps pixmaps =
818 planarConfig.makeYUVAPixmaps(fOriginalBMs[opaque].dimensions(),
819 static_cast<SkYUVColorSpace>(cs),
820 resultBMs,
821 numPlanes);
822 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(std::move(pixmaps));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400823
Brian Salomon7db71392020-10-16 10:05:21 -0400824 fImages[opaque][cs][format] = lazyYUV->refImage(dContext, fImageType);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400825 }
826 }
827 }
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400828
Robert Phillips057c33f2020-07-17 11:59:01 -0400829 if (dContext) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400830 // Some backends (e.g., Vulkan) require all work be completed for backend textures
831 // before they are deleted. Since we don't know when we'll next have access to a
832 // direct context, flush all the work now.
Robert Phillips057c33f2020-07-17 11:59:01 -0400833 dContext->flush();
834 dContext->submit(true);
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400835 }
836
837 return true;
838 }
839
Robert Phillips057c33f2020-07-17 11:59:01 -0400840 DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400841 this->createBitmaps();
842
Robert Phillips057c33f2020-07-17 11:59:01 -0400843 if (dContext && dContext->abandoned()) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400844 // This isn't a GpuGM so a null 'context' is okay but an abandoned context
845 // if forbidden.
846 return DrawResult::kSkip;
847 }
848
Brian Salomon6c6678f2020-09-21 10:50:30 -0400849 // Only the generator is expected to work with the CPU backend.
Brian Salomon7db71392020-10-16 10:05:21 -0400850 if (fImageType != Type::kFromGenerator && !dContext) {
Brian Salomon6c6678f2020-09-21 10:50:30 -0400851 return DrawResult::kSkip;
852 }
853
Robert Phillips057c33f2020-07-17 11:59:01 -0400854 if (!this->createImages(dContext)) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400855 *errorMsg = "Failed to create YUV images";
856 return DrawResult::kFail;
857 }
858
859 return DrawResult::kOk;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400860 }
861
Robert Phillipsb795bea2020-06-25 12:38:53 -0400862 void onGpuTeardown() override {
863 for (int i = 0; i < 2; ++i) {
864 for (int j = 0; j <= kLastEnum_SkYUVColorSpace; ++j) {
865 for (int k = 0; k <= kLast_YUVFormat; ++k) {
866 fImages[i][j][k] = nullptr;
867 }
868 }
869 }
870 }
871
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400872 void onDraw(SkCanvas* canvas) override {
Brian Salomon6c6678f2020-09-21 10:50:30 -0400873 auto direct = GrAsDirectContext(canvas->recordingContext());
874
Robert Phillips99044e12020-01-29 08:37:01 -0500875 float cellWidth = kTileWidthHeight, cellHeight = kTileWidthHeight;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400876 if (fUseSubset) {
Robert Phillips99044e12020-01-29 08:37:01 -0500877 cellWidth *= 1.5f;
878 cellHeight *= 1.5f;
879 }
880
Brian Salomon6c6678f2020-09-21 10:50:30 -0400881 SkRect srcRect = SkRect::Make(fOriginalBMs[0].dimensions());
Michael Ludwiga6a84002019-04-12 15:03:02 -0400882 SkRect dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, srcRect.width(), srcRect.height());
Brian Salomon6c6678f2020-09-21 10:50:30 -0400883
Michael Ludwiga6a84002019-04-12 15:03:02 -0400884 SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400885 if (fUseSubset) {
886 srcRect.inset(kSubsetPadding, kSubsetPadding);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400887 // Draw a larger rectangle to ensure bilerp filtering would normally read outside the
888 // srcRect and hit the red pixels, if strict constraint weren't used.
889 dstRect.fRight = kLabelWidth + 1.5f * srcRect.width();
890 dstRect.fBottom = 1.5f * srcRect.height();
891 constraint = SkCanvas::kStrict_SrcRectConstraint;
892 }
893
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400894 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500895 SkPaint paint;
Michael Ludwiga6a84002019-04-12 15:03:02 -0400896 paint.setFilterQuality(kLow_SkFilterQuality);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500897 if (kIdentity_SkYUVColorSpace == cs) {
898 // The identity color space needs post processing to appear correctly
899 paint.setColorFilter(yuv_to_rgb_colorfilter());
900 }
901
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400902 for (int opaque : { 0, 1 }) {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400903 dstRect.offsetTo(dstRect.fLeft, kLabelHeight);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400904
Robert Phillips99044e12020-01-29 08:37:01 -0500905 draw_col_label(canvas, dstRect.fLeft + cellWidth / 2, cs, opaque);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400906
Brian Salomon68b245c2020-10-13 12:34:31 -0400907 canvas->drawBitmapRect(fOriginalBMs[opaque], srcRect, dstRect, nullptr, constraint);
Robert Phillips99044e12020-01-29 08:37:01 -0500908 dstRect.offset(0.f, cellHeight + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400909
Robert Phillipsbb749902019-06-10 17:20:12 -0400910 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400911 draw_row_label(canvas, dstRect.fTop, format);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500912 if (fUseTargetColorSpace && fImages[opaque][cs][format]) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500913 // Making a CS-specific version of a kIdentity_SkYUVColorSpace YUV image
914 // doesn't make a whole lot of sense. The colorSpace conversion will
915 // operate on the YUV components rather than the RGB components.
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500916 sk_sp<SkImage> csImage =
Adlai Holler3a220172020-07-15 10:37:50 -0400917 fImages[opaque][cs][format]->makeColorSpace(fTargetColorSpace, direct);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400918 canvas->drawImageRect(csImage, srcRect, dstRect, &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500919 } else {
Robert Phillips99044e12020-01-29 08:37:01 -0500920 canvas->drawImageRect(fImages[opaque][cs][format], srcRect, dstRect,
921 &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500922 }
Robert Phillips99044e12020-01-29 08:37:01 -0500923 dstRect.offset(0.f, cellHeight + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400924 }
925
Robert Phillips99044e12020-01-29 08:37:01 -0500926 dstRect.offset(cellWidth + kPad, 0.f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400927 }
928 }
929 }
930
931private:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400932 SkBitmap fOriginalBMs[2];
933 sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace + 1][kLast_YUVFormat + 1];
Robert Phillips2dd1b472019-03-21 09:00:20 -0400934 bool fUseTargetColorSpace;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400935 bool fUseSubset;
Brian Salomon7db71392020-10-16 10:05:21 -0400936 Type fImageType;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400937 sk_sp<SkColorSpace> fTargetColorSpace;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400938
John Stiles7571f9e2020-09-02 22:42:33 -0400939 using INHERITED = GM;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400940};
941
942//////////////////////////////////////////////////////////////////////////////
943
Brian Salomon6c6678f2020-09-21 10:50:30 -0400944DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
945 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400946 WackyYUVFormatsGM::Type::kFromTextures);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400947DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
948 /* subset */ true,
Brian Salomon7db71392020-10-16 10:05:21 -0400949 WackyYUVFormatsGM::Type::kFromTextures);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400950DEF_GM(return new WackyYUVFormatsGM(/* target cs */ true,
951 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400952 WackyYUVFormatsGM::Type::kFromTextures);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400953DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
954 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400955 WackyYUVFormatsGM::Type::kFromGenerator);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400956DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
957 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400958 WackyYUVFormatsGM::Type::kFromPixmaps);)
Brian Osmane9560492019-02-05 17:00:03 -0500959
Chris Dalton3a778372019-02-07 15:23:36 -0700960class YUVMakeColorSpaceGM : public GpuGM {
Brian Osmane9560492019-02-05 17:00:03 -0500961public:
962 YUVMakeColorSpaceGM() {
963 this->setBGColor(0xFFCCCCCC);
964 }
965
966protected:
967 SkString onShortName() override {
968 return SkString("yuv_make_color_space");
969 }
970
971 SkISize onISize() override {
972 int numCols = 4; // (transparent, opaque) x (untagged, tagged)
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400973 int numRows = 5; // original, YUV, subset, makeNonTextureImage, readPixels
Brian Osmane9560492019-02-05 17:00:03 -0500974 return SkISize::Make(numCols * (kTileWidthHeight + kPad) + kPad,
975 numRows * (kTileWidthHeight + kPad) + kPad);
976 }
977
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400978 void createBitmaps() {
Brian Osmane9560492019-02-05 17:00:03 -0500979 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
980 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
981 float innerRadius = 20.0f;
982
983 {
984 // transparent
985 SkTDArray<SkRect> circles;
986 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400987 fOriginalBMs[0] = make_bitmap(kN32_SkColorType, path, circles, false, false);
Brian Osmane9560492019-02-05 17:00:03 -0500988 }
989
990 {
991 // opaque
992 SkTDArray<SkRect> circles;
993 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400994 fOriginalBMs[1] = make_bitmap(kN32_SkColorType, path, circles, true, false);
Brian Osmane9560492019-02-05 17:00:03 -0500995 }
996
997 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
998 }
999
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001000 bool createImages(GrDirectContext* context) {
Brian Osmane9560492019-02-05 17:00:03 -05001001 for (bool opaque : { false, true }) {
1002 PlaneData planes;
1003 extract_planes(fOriginalBMs[opaque], kJPEG_SkYUVColorSpace, &planes);
1004
1005 SkBitmap resultBMs[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001006
Brian Salomonf7255d72020-04-08 15:57:08 -04001007 create_YUV(planes, kAYUV_YUVFormat, resultBMs, opaque);
Robert Phillips2dd1b472019-03-21 09:00:20 -04001008
Brian Salomon94f65d72020-09-18 14:09:51 -04001009 YUVAPlanarConfig planarConfig(kAYUV_YUVFormat, opaque);
Brian Osmane9560492019-02-05 17:00:03 -05001010
Brian Salomon7db71392020-10-16 10:05:21 -04001011 auto yuvaPixmaps = planarConfig.makeYUVAPixmaps(fOriginalBMs[opaque].dimensions(),
1012 kJPEG_Full_SkYUVColorSpace,
1013 resultBMs,
1014 SK_ARRAY_COUNT(resultBMs));
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001015
Brian Salomon7db71392020-10-16 10:05:21 -04001016 int i = 0;
1017 for (sk_sp<SkColorSpace> cs : {sk_sp<SkColorSpace>(nullptr),
1018 SkColorSpace::MakeSRGB()}) {
1019 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(yuvaPixmaps,
1020 GrMipmapped::kNo,
1021 std::move(cs));
1022 fImages[opaque][i++] =
1023 lazyYUV->refImage(context, sk_gpu_test::LazyYUVImage::Type::kFromTextures);
Brian Osmane9560492019-02-05 17:00:03 -05001024 }
Brian Osmane9560492019-02-05 17:00:03 -05001025 }
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001026
1027 // Some backends (e.g., Vulkan) require all work be completed for backend textures before
1028 // they are deleted. Since we don't know when we'll next have access to a direct context,
1029 // flush all the work now.
Greg Danielce9f0162020-06-30 13:42:46 -04001030 context->flush();
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001031 context->submit(true);
1032
1033 return true;
1034 }
1035
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001036 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001037 if (!context || context->abandoned()) {
1038 return DrawResult::kSkip;
1039 }
1040
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001041 this->createBitmaps();
1042 if (!this->createImages(context)) {
1043 *errorMsg = "Failed to create YUV images";
1044 return DrawResult::kFail;
1045 }
1046
1047 return DrawResult::kOk;
Brian Osmane9560492019-02-05 17:00:03 -05001048 }
1049
Robert Phillipsb795bea2020-06-25 12:38:53 -04001050 void onGpuTeardown() override {
1051 fImages[0][0] = fImages[0][1] = fImages[1][0] = fImages[1][1] = nullptr;
1052 }
1053
Brian Salomoneebe7352020-12-09 16:37:04 -05001054 DrawResult onDraw(GrRecordingContext* rContext, GrSurfaceDrawContext*,
Adlai Holler3a220172020-07-15 10:37:50 -04001055 SkCanvas* canvas, SkString* msg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001056 SkASSERT(fImages[0][0] && fImages[0][1] && fImages[1][0] && fImages[1][1]);
Brian Osmane9560492019-02-05 17:00:03 -05001057
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001058 auto dContext = GrAsDirectContext(rContext);
1059 if (rContext && !dContext) {
Adlai Holler3a220172020-07-15 10:37:50 -04001060 *msg = "YUV ColorSpace image creation requires a direct context.";
1061 return DrawResult::kSkip;
1062 }
1063
Brian Osmane9560492019-02-05 17:00:03 -05001064 int x = kPad;
1065 for (int tagged : { 0, 1 }) {
1066 for (int opaque : { 0, 1 }) {
1067 int y = kPad;
1068
Mike Reedac9f0c92020-12-23 10:11:33 -05001069 auto raster = fOriginalBMs[opaque].asImage()
Adlai Holler3a220172020-07-15 10:37:50 -04001070 ->makeColorSpace(fTargetColorSpace, nullptr);
Brian Osmane9560492019-02-05 17:00:03 -05001071 canvas->drawImage(raster, x, y);
1072 y += kTileWidthHeight + kPad;
1073
Greg Daniel85da3362020-03-09 15:18:35 -04001074 if (fImages[opaque][tagged]) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001075 auto yuv = fImages[opaque][tagged]->makeColorSpace(fTargetColorSpace, dContext);
Adlai Holler3a220172020-07-15 10:37:50 -04001076 SkASSERT(yuv);
Greg Daniel85da3362020-03-09 15:18:35 -04001077 SkASSERT(SkColorSpace::Equals(yuv->colorSpace(), fTargetColorSpace.get()));
1078 canvas->drawImage(yuv, x, y);
1079 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001080
Adlai Holler872a32c2020-07-10 14:33:22 -04001081 SkIRect bounds = SkIRect::MakeWH(kTileWidthHeight / 2, kTileWidthHeight / 2);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001082 auto subset = yuv->makeSubset(bounds, dContext);
Adlai Holler3a220172020-07-15 10:37:50 -04001083 SkASSERT(subset);
Greg Daniel85da3362020-03-09 15:18:35 -04001084 canvas->drawImage(subset, x, y);
1085 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001086
Greg Daniel85da3362020-03-09 15:18:35 -04001087 auto nonTexture = yuv->makeNonTextureImage();
Adlai Holler3a220172020-07-15 10:37:50 -04001088 SkASSERT(nonTexture);
Greg Daniel85da3362020-03-09 15:18:35 -04001089 canvas->drawImage(nonTexture, x, y);
1090 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001091
Greg Daniel85da3362020-03-09 15:18:35 -04001092 SkBitmap readBack;
1093 readBack.allocPixels(yuv->imageInfo());
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001094 SkAssertResult(yuv->readPixels(dContext, readBack.pixmap(), 0, 0));
Greg Daniel85da3362020-03-09 15:18:35 -04001095 canvas->drawBitmap(readBack, x, y);
1096 }
Brian Osmane9560492019-02-05 17:00:03 -05001097 x += kTileWidthHeight + kPad;
1098 }
1099 }
Adlai Holler3a220172020-07-15 10:37:50 -04001100 return DrawResult::kOk;
Brian Osmane9560492019-02-05 17:00:03 -05001101 }
1102
1103private:
1104 SkBitmap fOriginalBMs[2];
1105 sk_sp<SkImage> fImages[2][2];
Brian Osmane9560492019-02-05 17:00:03 -05001106 sk_sp<SkColorSpace> fTargetColorSpace;
1107
John Stiles7571f9e2020-09-02 22:42:33 -04001108 using INHERITED = GM;
Brian Osmane9560492019-02-05 17:00:03 -05001109};
1110
1111DEF_GM(return new YUVMakeColorSpaceGM();)
1112
John Stilesa6841be2020-08-06 14:11:56 -04001113} // namespace skiagm
Mike Reed6a5f7e22019-05-23 15:30:07 -04001114
1115///////////////
1116
Mike Reed6a5f7e22019-05-23 15:30:07 -04001117#include "include/effects/SkColorMatrix.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001118#include "src/core/SkAutoPixmapStorage.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001119#include "tools/Resources.h"
Mike Reed6a5f7e22019-05-23 15:30:07 -04001120
1121static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) {
1122 auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
1123 canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height());
1124 SkPaint paint;
1125 paint.setFilterQuality(kLow_SkFilterQuality);
1126 paint.setColorFilter(cf);
1127 paint.setBlendMode(SkBlendMode::kSrc);
1128 canvas->drawImage(img, 0, 0, &paint);
1129}
1130
1131static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) {
1132 float m[20];
1133 SkColorMatrix_RGB2YUV(cs, m);
1134
1135 memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A
1136 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]);
1137
1138 memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A
1139 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]);
1140
1141 memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A
1142 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]);
1143}
1144
1145static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y,
1146 const SkImage* a, const SkImage* b) {
Mike Reed41068192020-12-09 21:48:52 -05001147 auto sh = SkShaders::Blend(SkBlendMode::kDifference,
1148 a->makeShader(SkSamplingOptions()),
1149 b->makeShader(SkSamplingOptions()));
Mike Reed6a5f7e22019-05-23 15:30:07 -04001150 SkPaint paint;
1151 paint.setShader(sh);
1152 canvas->save();
1153 canvas->translate(x, y);
1154 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1155
1156 SkColorMatrix cm;
1157 cm.setScale(64, 64, 64);
1158 paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm)));
1159 canvas->translate(0, a->height());
1160 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1161
1162 canvas->restore();
1163}
1164
1165// Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the
1166// resulting (recombined) images (gpu only for now).
1167//
1168class YUVSplitterGM : public skiagm::GM {
1169 sk_sp<SkImage> fOrig;
1170 SkAutoPixmapStorage fStorage[3];
1171 SkPixmap fPM[3];
1172
1173public:
1174 YUVSplitterGM() {}
1175
1176protected:
1177
1178 SkString onShortName() override {
1179 return SkString("yuv_splitter");
1180 }
1181
1182 SkISize onISize() override {
Brian Osman2b73e662019-11-01 10:02:24 -04001183 return SkISize::Make(1280, 768);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001184 }
1185
1186 void onOnceBeforeDraw() override {
1187 fOrig = GetResourceAsImage("images/mandrill_256.png");
1188
Brian Salomonf165f792020-10-05 13:25:05 -04001189 SkImageInfo info = SkImageInfo::MakeA8(fOrig->dimensions());
Mike Reed6a5f7e22019-05-23 15:30:07 -04001190 fStorage[0].alloc(info);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001191 fStorage[1].alloc(info);
1192 fStorage[2].alloc(info);
1193 for (int i = 0; i < 3; ++i) {
1194 fPM[i] = fStorage[i];
1195 }
1196 }
1197
1198 void onDraw(SkCanvas* canvas) override {
1199 SkYUVAIndex indices[4];
Robert Phillipse0735522020-01-31 11:03:32 -05001200 indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
1201 indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
1202 indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR};
Mike Reed6a5f7e22019-05-23 15:30:07 -04001203 indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kR};
1204
1205 canvas->translate(fOrig->width(), 0);
1206 canvas->save();
Brian Osman2b73e662019-11-01 10:02:24 -04001207 for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace,
1208 kBT2020_SkYUVColorSpace}) {
Mike Reed6a5f7e22019-05-23 15:30:07 -04001209 split_into_yuv(fOrig.get(), cs, fPM);
Brian Salomone4387382020-11-11 16:34:19 -05001210 SkYUVAInfo yuvaInfo(fOrig->dimensions(),
1211 SkYUVAInfo::PlaneConfig::kY_U_V,
1212 SkYUVAInfo::Subsampling::k444,
1213 cs);
Brian Salomonf165f792020-10-05 13:25:05 -04001214 auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, fPM);
1215 auto img = SkImage::MakeFromYUVAPixmaps(canvas->recordingContext(),
1216 yuvaPixmaps,
1217 GrMipMapped::kNo,
1218 /* limit to max tex size */ false,
1219 /* color space */ nullptr);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001220 if (img) {
1221 canvas->drawImage(img, 0, 0, nullptr);
1222 draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get());
1223 }
1224 canvas->translate(fOrig->width(), 0);
1225 }
1226 canvas->restore();
1227 canvas->translate(-fOrig->width(), 0);
1228
1229 canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0, nullptr);
1230 canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height(), nullptr);
1231 canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]),
1232 0, fPM[0].height() + fPM[1].height(), nullptr);
1233 }
1234
1235private:
John Stiles7571f9e2020-09-02 22:42:33 -04001236 using INHERITED = GM;
Mike Reed6a5f7e22019-05-23 15:30:07 -04001237};
1238DEF_GM( return new YUVSplitterGM; )