blob: afdc4cf9cd042e54ed2a85d6850bab0ecc12168b [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
61class GrRenderTargetContext;
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) {
126 fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_UV_420;
127 } else {
Brian Salomon94f65d72020-09-18 14:09:51 -0400128 fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_UV_A_4204;
129 }
130 break;
131 case kY416_YUVFormat:
132 case kY410_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400133 if (opaque) {
134 fPlanarConfig = SkYUVAInfo::PlanarConfig::kUYV_444;
135 } else {
Brian Salomon94f65d72020-09-18 14:09:51 -0400136 fPlanarConfig = SkYUVAInfo::PlanarConfig::kUYVA_4444;
137 }
138 break;
139 case kAYUV_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400140 if (opaque) {
141 fPlanarConfig = SkYUVAInfo::PlanarConfig::kYUV_444;
142 } else {
Brian Salomon94f65d72020-09-18 14:09:51 -0400143 fPlanarConfig = SkYUVAInfo::PlanarConfig::kYUVA_4444;
144 }
145 break;
146 case kNV21_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400147 if (opaque) {
148 fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_VU_420;
149 } else {
Brian Salomon94f65d72020-09-18 14:09:51 -0400150 fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_VU_A_4204;
151 }
152 break;
153 case kI420_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400154 if (opaque) {
155 fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_U_V_420;
156 } else {
Brian Salomon94f65d72020-09-18 14:09:51 -0400157 fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_U_V_A_4204;
158 }
159 break;
160 case kYV12_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400161 if (opaque) {
162 fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_V_U_420;
163 } else {
Brian Salomon94f65d72020-09-18 14:09:51 -0400164 fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_V_U_A_4204;
165 }
166 break;
167 }
168 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400169
Brian Salomon94f65d72020-09-18 14:09:51 -0400170 int numPlanes() const { return SkYUVAInfo::NumPlanes(fPlanarConfig); }
Brian Salomonf7255d72020-04-08 15:57:08 -0400171
Brian Salomon6c6678f2020-09-21 10:50:30 -0400172 SkYUVAPixmaps makeYUVAPixmaps(SkISize dimensions,
173 SkYUVColorSpace yuvColorSpace,
174 const SkBitmap bitmaps[],
175 int numBitmaps) const;
176
Brian Salomonf7255d72020-04-08 15:57:08 -0400177private:
Brian Salomon94f65d72020-09-18 14:09:51 -0400178 SkYUVAInfo::PlanarConfig fPlanarConfig;
Brian Salomonf7255d72020-04-08 15:57:08 -0400179};
180
Brian Salomon6c6678f2020-09-21 10:50:30 -0400181SkYUVAPixmaps YUVAPlanarConfig::makeYUVAPixmaps(SkISize dimensions,
182 SkYUVColorSpace yuvColorSpace,
183 const SkBitmap bitmaps[],
184 int numBitmaps) const {
Brian Salomon7db71392020-10-16 10:05:21 -0400185 SkYUVAInfo info(dimensions, fPlanarConfig, yuvColorSpace);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400186 SkPixmap pmaps[SkYUVAInfo::kMaxPlanes];
187 int n = info.numPlanes();
188 if (numBitmaps < n) {
189 return {};
190 }
191 for (int i = 0; i < n; ++i) {
192 pmaps[i] = bitmaps[i].pixmap();
193 }
194 return SkYUVAPixmaps::FromExternalPixmaps(info, pmaps);
195}
196
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400197// All the planes we need to construct the various YUV formats
198struct PlaneData {
199 SkBitmap fYFull;
200 SkBitmap fUFull;
201 SkBitmap fVFull;
202 SkBitmap fAFull;
203 SkBitmap fUQuarter; // 2x2 downsampled U channel
204 SkBitmap fVQuarter; // 2x2 downsampled V channel
Robert Phillips429f0d32019-09-11 17:03:28 -0400205
206 SkBitmap fFull;
207 SkBitmap fQuarter; // 2x2 downsampled YUVA
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400208};
209
210// Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle
211// and have tangents 'v1' and 'v2'.
212static void add_arc(SkPath* path,
213 const SkPoint& o1, const SkVector& v1,
214 const SkPoint& o2, const SkVector& v2,
215 SkTDArray<SkRect>* circles, bool takeLongWayRound) {
216
217 SkVector v3 = { -v1.fY, v1.fX };
218 SkVector v4 = { v2.fY, -v2.fX };
219
220 SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4);
221 SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY };
222
223 SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t };
224
225 if (circles) {
226 circles->push_back(r);
227 }
228
229 SkVector startV = o1 - center, endV = o2 - center;
230 startV.normalize();
231 endV.normalize();
232
233 SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX));
234 SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX));
235
236 startDeg += 360.0f;
237 startDeg = fmodf(startDeg, 360.0f);
238
239 endDeg += 360.0f;
240 endDeg = fmodf(endDeg, 360.0f);
241
242 if (endDeg < startDeg) {
243 endDeg += 360.0f;
244 }
245
246 SkScalar sweepDeg = SkTAbs(endDeg - startDeg);
247 if (!takeLongWayRound) {
248 sweepDeg = sweepDeg - 360;
249 }
250
251 path->arcTo(r, startDeg, sweepDeg, false);
252}
253
254static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius,
255 SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) {
256 if (numLobes <= 1) {
257 return SkPath();
258 }
259
260 SkPath p;
261
262 int numDivisions = 2 * numLobes;
263 SkScalar fullLobeDegrees = 360.0f / numLobes;
264 SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f);
265 SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f);
266 SkMatrix outerStep, innerStep;
267 outerStep.setRotate(outDegrees);
268 innerStep.setRotate(innerDegrees);
269 SkVector curV = SkVector::Make(0.0f, 1.0f);
270
271 if (circles) {
272 circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius,
273 o.fX + innerRadius, o.fY + innerRadius));
274 }
275
276 p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY);
277
278 for (int i = 0; i < numDivisions; ++i) {
279
280 SkVector nextV;
281 if (0 == (i % 2)) {
282 nextV = outerStep.mapVector(curV.fX, curV.fY);
283
284 SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX,
285 o.fY + outerRadius * curV.fY);
286 SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX,
287 o.fY + outerRadius * nextV.fY);
288
289 p.lineTo(top);
290 add_arc(&p, top, curV, nextTop, nextV, circles, true);
291 } else {
292 nextV = innerStep.mapVector(curV.fX, curV.fY);
293
294 SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX,
295 o.fY + innerRadius * curV.fY);
296 SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX,
297 o.fY + innerRadius * nextV.fY);
298
299 p.lineTo(bot);
300 add_arc(&p, bot, curV, nextBot, nextV, nullptr, false);
301 }
302
303 curV = nextV;
304 }
305
306 p.close();
307
308 return p;
309}
310
Robert Phillips2dd1b472019-03-21 09:00:20 -0400311static SkBitmap make_bitmap(SkColorType colorType, const SkPath& path,
Michael Ludwiga6a84002019-04-12 15:03:02 -0400312 const SkTDArray<SkRect>& circles, bool opaque, bool padWithRed) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500313 const SkColor kGreen = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104));
314 const SkColor kBlue = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252));
315 const SkColor kYellow = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400316
Brian Salomon6c6678f2020-09-21 10:50:30 -0400317 int widthHeight = kTileWidthHeight + (padWithRed ? 2 * kSubsetPadding : 0);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400318
319 SkImageInfo ii = SkImageInfo::Make(widthHeight, widthHeight,
Robert Phillips2dd1b472019-03-21 09:00:20 -0400320 colorType, kPremul_SkAlphaType);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400321
322 SkBitmap bm;
323 bm.allocPixels(ii);
324
Robert Phillips2dd1b472019-03-21 09:00:20 -0400325 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(ii,
326 bm.getPixels(),
327 bm.rowBytes());
Michael Ludwiga6a84002019-04-12 15:03:02 -0400328 if (padWithRed) {
329 canvas->clear(SK_ColorRED);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400330 canvas->translate(kSubsetPadding, kSubsetPadding);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400331 canvas->clipRect(SkRect::MakeWH(kTileWidthHeight, kTileWidthHeight));
332 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400333 canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT);
334
335 SkPaint paint;
336 paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency
337 paint.setColor(kBlue);
338
339 canvas->drawPath(path, paint);
340
341 paint.setColor(opaque ? kYellow : SK_ColorTRANSPARENT);
342 paint.setBlendMode(SkBlendMode::kSrc);
343 for (int i = 0; i < circles.count(); ++i) {
344 SkRect r = circles[i];
345 r.inset(r.width()/4, r.height()/4);
346 canvas->drawOval(r, paint);
347 }
348
349 return bm;
350}
351
Brian Osmanac8a16c2019-10-31 10:24:12 -0400352static void convert_rgba_to_yuva(const float mtx[20], SkColor col, uint8_t yuv[4]) {
353 const uint8_t r = SkColorGetR(col);
354 const uint8_t g = SkColorGetG(col);
355 const uint8_t b = SkColorGetB(col);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400356
Brian Osmanaba642c2020-02-06 12:52:25 -0500357 yuv[0] = SkTPin(SkScalarRoundToInt(mtx[ 0]*r + mtx[ 1]*g + mtx[ 2]*b + mtx[ 4]*255), 0, 255);
358 yuv[1] = SkTPin(SkScalarRoundToInt(mtx[ 5]*r + mtx[ 6]*g + mtx[ 7]*b + mtx[ 9]*255), 0, 255);
359 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 -0400360 yuv[3] = SkColorGetA(col);
361}
362
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400363static void extract_planes(const SkBitmap& bm, SkYUVColorSpace yuvColorSpace, PlaneData* planes) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500364 if (kIdentity_SkYUVColorSpace == yuvColorSpace) {
365 // To test the identity color space we use JPEG YUV planes
366 yuvColorSpace = kJPEG_SkYUVColorSpace;
367 }
368
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400369 SkASSERT(!(bm.width() % 2));
370 SkASSERT(!(bm.height() % 2));
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400371 planes->fYFull.allocPixels(
372 SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
373 planes->fUFull.allocPixels(
374 SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
375 planes->fVFull.allocPixels(
376 SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400377 planes->fAFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400378 planes->fUQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2,
379 kGray_8_SkColorType, kUnpremul_SkAlphaType));
380 planes->fVQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2,
381 kGray_8_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400382
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400383 planes->fFull.allocPixels(
384 SkImageInfo::Make(bm.dimensions(), kRGBA_F32_SkColorType, kUnpremul_SkAlphaType));
Robert Phillips429f0d32019-09-11 17:03:28 -0400385 planes->fQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2,
386 kRGBA_F32_SkColorType, kUnpremul_SkAlphaType));
387
Brian Osmanac8a16c2019-10-31 10:24:12 -0400388 float mtx[20];
389 SkColorMatrix_RGB2YUV(yuvColorSpace, mtx);
390
Robert Phillips429f0d32019-09-11 17:03:28 -0400391 SkColor4f* dst = (SkColor4f *) planes->fFull.getAddr(0, 0);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400392 for (int y = 0; y < bm.height(); ++y) {
393 for (int x = 0; x < bm.width(); ++x) {
394 SkColor col = bm.getColor(x, y);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400395
Robert Phillips1c7062d2018-10-04 10:44:53 -0400396 uint8_t yuva[4];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400397
Brian Osmanac8a16c2019-10-31 10:24:12 -0400398 convert_rgba_to_yuva(mtx, col, yuva);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400399
400 *planes->fYFull.getAddr8(x, y) = yuva[0];
401 *planes->fUFull.getAddr8(x, y) = yuva[1];
402 *planes->fVFull.getAddr8(x, y) = yuva[2];
403 *planes->fAFull.getAddr8(x, y) = yuva[3];
Robert Phillips429f0d32019-09-11 17:03:28 -0400404
405 // TODO: render in F32 rather than converting here
406 dst->fR = yuva[0] / 255.0f;
407 dst->fG = yuva[1] / 255.0f;
408 dst->fB = yuva[2] / 255.0f;
409 dst->fA = yuva[3] / 255.0f;
410 ++dst;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400411 }
412 }
413
Robert Phillips429f0d32019-09-11 17:03:28 -0400414 dst = (SkColor4f *) planes->fQuarter.getAddr(0, 0);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400415 for (int y = 0; y < bm.height()/2; ++y) {
416 for (int x = 0; x < bm.width()/2; ++x) {
Robert Phillips429f0d32019-09-11 17:03:28 -0400417 uint32_t yAccum = 0, uAccum = 0, vAccum = 0, aAccum = 0;
418
419 yAccum += *planes->fYFull.getAddr8(2*x, 2*y);
420 yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y);
421 yAccum += *planes->fYFull.getAddr8(2*x, 2*y+1);
422 yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y+1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400423
424 uAccum += *planes->fUFull.getAddr8(2*x, 2*y);
425 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y);
426 uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1);
427 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1);
428
429 *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f;
430
431 vAccum += *planes->fVFull.getAddr8(2*x, 2*y);
432 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y);
433 vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1);
434 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1);
435
Robert Phillips1c7062d2018-10-04 10:44:53 -0400436 *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f;
Robert Phillips429f0d32019-09-11 17:03:28 -0400437
438 aAccum += *planes->fAFull.getAddr8(2*x, 2*y);
439 aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y);
440 aAccum += *planes->fAFull.getAddr8(2*x, 2*y+1);
441 aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y+1);
442
443 // TODO: render in F32 rather than converting here
444 dst->fR = yAccum / (4.0f * 255.0f);
445 dst->fG = uAccum / (4.0f * 255.0f);
446 dst->fB = vAccum / (4.0f * 255.0f);
447 dst->fA = aAccum / (4.0f * 255.0f);
448 ++dst;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400449 }
450 }
451}
452
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400453// Create a 2x2 downsampled SkBitmap. It is stored in an RG texture. It can optionally be
Robert Phillips429f0d32019-09-11 17:03:28 -0400454// uv (i.e., NV12) or vu (i.e., NV21).
Robert Phillipsbb749902019-06-10 17:20:12 -0400455static SkBitmap make_quarter_2_channel(const SkBitmap& fullY,
456 const SkBitmap& quarterU,
457 const SkBitmap& quarterV,
458 bool uv) {
459 SkBitmap result;
460
Robert Phillipsbb749902019-06-10 17:20:12 -0400461 result.allocPixels(SkImageInfo::Make(fullY.width()/2,
462 fullY.height()/2,
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400463 kR8G8_unorm_SkColorType,
Robert Phillipsbb749902019-06-10 17:20:12 -0400464 kUnpremul_SkAlphaType));
465
466 for (int y = 0; y < fullY.height()/2; ++y) {
467 for (int x = 0; x < fullY.width()/2; ++x) {
468 uint8_t u8 = *quarterU.getAddr8(x, y);
469 uint8_t v8 = *quarterV.getAddr8(x, y);
470
471 if (uv) {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400472 *result.getAddr16(x, y) = (v8 << 8) | u8;
Robert Phillipsbb749902019-06-10 17:20:12 -0400473 } else {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400474 *result.getAddr16(x, y) = (u8 << 8) | v8;
Robert Phillipsbb749902019-06-10 17:20:12 -0400475 }
476 }
477 }
478
479 return result;
480}
481
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400482// Create some flavor of a 16bits/channel bitmap from a RGBA_F32 source
483static SkBitmap make_16(const SkBitmap& src, SkColorType dstCT,
484 std::function<void(uint16_t* dstPixel, const float* srcPixel)> convert) {
Robert Phillips429f0d32019-09-11 17:03:28 -0400485 SkASSERT(src.colorType() == kRGBA_F32_SkColorType);
486
487 SkBitmap result;
488
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400489 result.allocPixels(SkImageInfo::Make(src.dimensions(), dstCT, kUnpremul_SkAlphaType));
Robert Phillips429f0d32019-09-11 17:03:28 -0400490
Robert Phillips429f0d32019-09-11 17:03:28 -0400491 for (int y = 0; y < src.height(); ++y) {
492 for (int x = 0; x < src.width(); ++x) {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400493 const float* srcPixel = (const float*) src.getAddr(x, y);
494 uint16_t* dstPixel = (uint16_t*) result.getAddr(x, y);
Robert Phillips429f0d32019-09-11 17:03:28 -0400495
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400496 convert(dstPixel, srcPixel);
Robert Phillips429f0d32019-09-11 17:03:28 -0400497 }
498 }
499
500 return result;
501}
502
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400503static uint16_t flt_2_uint16(float flt) { return SkScalarRoundToInt(flt * 65535.0f); }
Robert Phillips429f0d32019-09-11 17:03:28 -0400504
Brian Salomonf7255d72020-04-08 15:57:08 -0400505// Recombine the separate planes into some YUV format. Returns the number of planes.
506static int create_YUV(const PlaneData& planes,
507 YUVFormat yuvFormat,
508 SkBitmap resultBMs[],
509 bool opaque) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400510 int nextLayer = 0;
511
512 switch (yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400513 case kY416_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400514 resultBMs[nextLayer++] = make_16(planes.fFull, kR16G16B16A16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400515 [] (uint16_t* dstPixel, const float* srcPixel) {
516 dstPixel[0] = flt_2_uint16(srcPixel[1]); // U
517 dstPixel[1] = flt_2_uint16(srcPixel[0]); // Y
518 dstPixel[2] = flt_2_uint16(srcPixel[2]); // V
519 dstPixel[3] = flt_2_uint16(srcPixel[3]); // A
520 });
Robert Phillipsbb749902019-06-10 17:20:12 -0400521 break;
522 }
Jim Van Verth976a6b02018-10-17 15:27:19 -0400523 case kAYUV_YUVFormat: {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400524 SkBitmap yuvaFull;
525
Jim Van Verth47133fd2018-10-19 22:09:28 -0400526 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
527 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400528
529 for (int y = 0; y < planes.fYFull.height(); ++y) {
530 for (int x = 0; x < planes.fYFull.width(); ++x) {
531
532 uint8_t Y = *planes.fYFull.getAddr8(x, y);
533 uint8_t U = *planes.fUFull.getAddr8(x, y);
534 uint8_t V = *planes.fVFull.getAddr8(x, y);
535 uint8_t A = *planes.fAFull.getAddr8(x, y);
536
537 // NOT premul!
Jim Van Verth47133fd2018-10-19 22:09:28 -0400538 // V and Y swapped to match RGBA layout
Robert Phillips2dd1b472019-03-21 09:00:20 -0400539 SkColor c = SkColorSetARGB(A, V, U, Y);
540 *yuvaFull.getAddr32(x, y) = c;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400541 }
542 }
543
544 resultBMs[nextLayer++] = yuvaFull;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400545 break;
546 }
Robert Phillips2dd1b472019-03-21 09:00:20 -0400547 case kY410_YUVFormat: {
548 SkBitmap yuvaFull;
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400549 uint32_t Y, U, V;
550 uint8_t A;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400551
552 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
553 kRGBA_1010102_SkColorType,
554 kUnpremul_SkAlphaType));
555
556 for (int y = 0; y < planes.fYFull.height(); ++y) {
557 for (int x = 0; x < planes.fYFull.width(); ++x) {
558
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400559 Y = SkScalarRoundToInt((*planes.fYFull.getAddr8(x, y) / 255.0f) * 1023.0f);
560 U = SkScalarRoundToInt((*planes.fUFull.getAddr8(x, y) / 255.0f) * 1023.0f);
561 V = SkScalarRoundToInt((*planes.fVFull.getAddr8(x, y) / 255.0f) * 1023.0f);
562 A = SkScalarRoundToInt((*planes.fAFull.getAddr8(x, y) / 255.0f) * 3.0f);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400563
564 // NOT premul!
Brian Salomonf7255d72020-04-08 15:57:08 -0400565 *yuvaFull.getAddr32(x, y) = (A << 30) | (V << 20) | (Y << 10) | (U << 0);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400566 }
567 }
568
569 resultBMs[nextLayer++] = yuvaFull;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400570 break;
571 }
Robert Phillipsbb749902019-06-10 17:20:12 -0400572 case kP016_YUVFormat: // fall through
Robert Phillips429f0d32019-09-11 17:03:28 -0400573 case kP010_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400574 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400575 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
576 (uint16_t* dstPixel, const float* srcPixel) {
577 uint16_t val16 = flt_2_uint16(srcPixel[0]);
578 dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0)
579 : val16;
580 });
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400581 resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400582 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
583 (uint16_t* dstPixel, const float* srcPixel) {
584 uint16_t u16 = flt_2_uint16(srcPixel[1]);
585 uint16_t v16 = flt_2_uint16(srcPixel[2]);
586 dstPixel[0] = tenBitsPP ? (u16 & 0xFFC0) : u16;
587 dstPixel[1] = tenBitsPP ? (v16 & 0xFFC0) : v16;
588 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400589 if (!opaque) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400590 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType,
591 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
592 (uint16_t* dstPixel, const float* srcPixel) {
593 uint16_t val16 = flt_2_uint16(srcPixel[3]);
594 dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0)
595 : val16;
596 });
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400597 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400598 return nextLayer;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400599 }
600 case kP016F_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400601 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400602 [] (uint16_t* dstPixel, const float* srcPixel) {
603 dstPixel[0] = SkFloatToHalf(srcPixel[0]);
604 });
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400605 resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_float_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400606 [] (uint16_t* dstPixel, const float* srcPixel) {
607 dstPixel[0] = SkFloatToHalf(srcPixel[1]);
608 dstPixel[1] = SkFloatToHalf(srcPixel[2]);
609 });
610 if (!opaque) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400611 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType,
612 [] (uint16_t* dstPixel, const float* srcPixel) {
613 dstPixel[0] = SkFloatToHalf(srcPixel[3]);
614 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400615 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400616 return nextLayer;
Robert Phillips429f0d32019-09-11 17:03:28 -0400617 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400618 case kNV12_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400619 SkBitmap uvQuarter = make_quarter_2_channel(planes.fYFull,
620 planes.fUQuarter,
621 planes.fVQuarter, true);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400622 resultBMs[nextLayer++] = planes.fYFull;
623 resultBMs[nextLayer++] = uvQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400624 break;
625 }
626 case kNV21_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400627 SkBitmap vuQuarter = make_quarter_2_channel(planes.fYFull,
628 planes.fUQuarter,
629 planes.fVQuarter, false);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400630 resultBMs[nextLayer++] = planes.fYFull;
631 resultBMs[nextLayer++] = vuQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400632 break;
633 }
634 case kI420_YUVFormat:
635 resultBMs[nextLayer++] = planes.fYFull;
636 resultBMs[nextLayer++] = planes.fUQuarter;
637 resultBMs[nextLayer++] = planes.fVQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400638 break;
639 case kYV12_YUVFormat:
640 resultBMs[nextLayer++] = planes.fYFull;
641 resultBMs[nextLayer++] = planes.fVQuarter;
642 resultBMs[nextLayer++] = planes.fUQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400643 break;
644 }
645
Brian Salomon94f65d72020-09-18 14:09:51 -0400646 if (!opaque && !has_alpha_channel(yuvFormat)) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400647 resultBMs[nextLayer++] = planes.fAFull;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400648 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400649 return nextLayer;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400650}
651
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400652static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) {
Brian Osmanb107a112020-07-23 14:22:22 -0400653 static const char* kYUVColorSpaceNames[] = {"JPEG", "601", "709F", "709L",
654 "2020_8F", "2020_8L", "2020_10F", "2020_10L",
655 "2020_12F", "2020_12L", "Identity"};
Brian Salomon4dea72a2019-12-18 10:43:10 -0500656 static_assert(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace + 1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400657
Mike Reed91919132019-01-02 12:21:01 -0500658 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500659 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500660 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400661
662 SkRect textRect;
663 SkString colLabel;
664
665 colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400666 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400667 int y = textRect.height();
668
Mike Reed91919132019-01-02 12:21:01 -0500669 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400670
671 colLabel.printf("%s", opaque ? "Opaque" : "Transparent");
Mike Reed71f5a0b2018-10-25 16:12:39 -0400672
Ben Wagner51e15a62019-05-07 15:38:46 -0400673 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400674 y += textRect.height();
675
Mike Reed91919132019-01-02 12:21:01 -0500676 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400677}
678
679static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400680 static const char* kYUVFormatNames[] = {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400681 "P016", "P010", "P016F", "Y416", "AYUV", "Y410", "NV12", "NV21", "I420", "YV12"
Robert Phillipsbb749902019-06-10 17:20:12 -0400682 };
Brian Salomon4dea72a2019-12-18 10:43:10 -0500683 static_assert(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat + 1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400684
Mike Reed91919132019-01-02 12:21:01 -0500685 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500686 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500687 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400688
689 SkRect textRect;
690 SkString rowLabel;
691
692 rowLabel.printf("%s", kYUVFormatNames[yuvFormat]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400693 font.measureText(rowLabel.c_str(), rowLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400694 y += kTileWidthHeight/2 + textRect.height()/2;
695
Hal Canary89a644b2019-01-07 09:36:09 -0500696 canvas->drawString(rowLabel, 0, y, font, paint);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400697}
698
Robert Phillips0a22ba82019-03-06 12:36:47 -0500699static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
700 static const float kJPEGConversionMatrix[20] = {
Mike Reede869a1e2019-04-30 12:18:54 -0400701 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
702 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
703 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
Robert Phillips0a22ba82019-03-06 12:36:47 -0500704 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
705 };
706
Mike Reede869a1e2019-04-30 12:18:54 -0400707 return SkColorFilters::Matrix(kJPEGConversionMatrix);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500708}
709
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400710namespace skiagm {
711
712// This GM creates an opaque and transparent bitmap, extracts the planes and then recombines
713// them into various YUV formats. It then renders the results in the grid:
714//
Robert Phillips2dd1b472019-03-21 09:00:20 -0400715// JPEG 601 709 Identity
716// Transparent Opaque Transparent Opaque Transparent Opaque Transparent Opaque
Robert Phillipsbb749902019-06-10 17:20:12 -0400717// originals
718// P016
719// P010
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400720// P016F
Robert Phillipsbb749902019-06-10 17:20:12 -0400721// Y416
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400722// AYUV
Robert Phillips2dd1b472019-03-21 09:00:20 -0400723// Y410
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400724// NV12
725// NV21
726// I420
727// YV12
728class WackyYUVFormatsGM : public GM {
729public:
Brian Salomon7db71392020-10-16 10:05:21 -0400730 using Type = sk_gpu_test::LazyYUVImage::Type;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400731
Brian Salomon7db71392020-10-16 10:05:21 -0400732 WackyYUVFormatsGM(bool useTargetColorSpace, bool useSubset, Type type)
733 : fUseTargetColorSpace(useTargetColorSpace), fUseSubset(useSubset), fImageType(type) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400734 this->setBGColor(0xFFCCCCCC);
735 }
736
737protected:
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400738 SkString onShortName() override {
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500739 SkString name("wacky_yuv_formats");
740 if (fUseTargetColorSpace) {
741 name += "_cs";
742 }
Brian Salomon6c6678f2020-09-21 10:50:30 -0400743 if (fUseSubset) {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400744 name += "_domain";
745 }
Brian Salomon6c6678f2020-09-21 10:50:30 -0400746 switch (fImageType) {
Brian Salomon7db71392020-10-16 10:05:21 -0400747 case Type::kFromPixmaps:
Brian Salomon6c6678f2020-09-21 10:50:30 -0400748 name += "_frompixmaps";
749 break;
Brian Salomon7db71392020-10-16 10:05:21 -0400750 case Type::kFromTextures:
Brian Salomon6c6678f2020-09-21 10:50:30 -0400751 break;
Brian Salomon7db71392020-10-16 10:05:21 -0400752 case Type::kFromGenerator:
Brian Salomon6c6678f2020-09-21 10:50:30 -0400753 name += "_imggen";
754 break;
Brian Salomon94f65d72020-09-18 14:09:51 -0400755 }
Robert Phillips2dd1b472019-03-21 09:00:20 -0400756
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500757 return name;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400758 }
759
760 SkISize onISize() override {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400761 int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x #-color-spaces
762 int numRows = 1 + (kLast_YUVFormat + 1); // original + #-yuv-formats
Brian Salomon6c6678f2020-09-21 10:50:30 -0400763 int wh = SkScalarCeilToInt(kTileWidthHeight * (fUseSubset ? 1.5f : 1.f));
Michael Ludwiga6a84002019-04-12 15:03:02 -0400764 return SkISize::Make(kLabelWidth + numCols * (wh + kPad),
765 kLabelHeight + numRows * (wh + kPad));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400766 }
767
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400768 void createBitmaps() {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400769 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
770 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
771 float innerRadius = 20.0f;
772
773 {
774 // transparent
775 SkTDArray<SkRect> circles;
776 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400777 fOriginalBMs[0] = make_bitmap(kRGBA_8888_SkColorType, path, circles, false, fUseSubset);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400778 }
779
780 {
781 // opaque
782 SkTDArray<SkRect> circles;
783 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400784 fOriginalBMs[1] = make_bitmap(kRGBA_8888_SkColorType, path, circles, true, fUseSubset);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400785 }
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500786
787 if (fUseTargetColorSpace) {
788 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
789 }
Robert Phillips51c89e42018-10-05 13:30:43 -0400790 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400791
Robert Phillips057c33f2020-07-17 11:59:01 -0400792 bool createImages(GrDirectContext* dContext) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400793 for (bool opaque : { false, true }) {
794 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
795 PlaneData planes;
796 extract_planes(fOriginalBMs[opaque], (SkYUVColorSpace) cs, &planes);
797
Brian Salomonf7255d72020-04-08 15:57:08 -0400798 for (int f = kP016_YUVFormat; f <= kLast_YUVFormat; ++f) {
799 auto format = static_cast<YUVFormat>(f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400800 SkBitmap resultBMs[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -0400801
Brian Salomon6c6678f2020-09-21 10:50:30 -0400802 int numPlanes = create_YUV(planes, format, resultBMs, opaque);
Brian Salomon94f65d72020-09-18 14:09:51 -0400803 const YUVAPlanarConfig planarConfig(format, opaque);
Brian Salomon7db71392020-10-16 10:05:21 -0400804 SkYUVAPixmaps pixmaps =
805 planarConfig.makeYUVAPixmaps(fOriginalBMs[opaque].dimensions(),
806 static_cast<SkYUVColorSpace>(cs),
807 resultBMs,
808 numPlanes);
809 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(std::move(pixmaps));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400810
Brian Salomon7db71392020-10-16 10:05:21 -0400811 fImages[opaque][cs][format] = lazyYUV->refImage(dContext, fImageType);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400812 }
813 }
814 }
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400815
Robert Phillips057c33f2020-07-17 11:59:01 -0400816 if (dContext) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400817 // Some backends (e.g., Vulkan) require all work be completed for backend textures
818 // before they are deleted. Since we don't know when we'll next have access to a
819 // direct context, flush all the work now.
Robert Phillips057c33f2020-07-17 11:59:01 -0400820 dContext->flush();
821 dContext->submit(true);
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400822 }
823
824 return true;
825 }
826
Robert Phillips057c33f2020-07-17 11:59:01 -0400827 DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400828 this->createBitmaps();
829
Robert Phillips057c33f2020-07-17 11:59:01 -0400830 if (dContext && dContext->abandoned()) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400831 // This isn't a GpuGM so a null 'context' is okay but an abandoned context
832 // if forbidden.
833 return DrawResult::kSkip;
834 }
835
Brian Salomon6c6678f2020-09-21 10:50:30 -0400836 // Only the generator is expected to work with the CPU backend.
Brian Salomon7db71392020-10-16 10:05:21 -0400837 if (fImageType != Type::kFromGenerator && !dContext) {
Brian Salomon6c6678f2020-09-21 10:50:30 -0400838 return DrawResult::kSkip;
839 }
840
Robert Phillips057c33f2020-07-17 11:59:01 -0400841 if (!this->createImages(dContext)) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400842 *errorMsg = "Failed to create YUV images";
843 return DrawResult::kFail;
844 }
845
846 return DrawResult::kOk;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400847 }
848
Robert Phillipsb795bea2020-06-25 12:38:53 -0400849 void onGpuTeardown() override {
850 for (int i = 0; i < 2; ++i) {
851 for (int j = 0; j <= kLastEnum_SkYUVColorSpace; ++j) {
852 for (int k = 0; k <= kLast_YUVFormat; ++k) {
853 fImages[i][j][k] = nullptr;
854 }
855 }
856 }
857 }
858
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400859 void onDraw(SkCanvas* canvas) override {
Brian Salomon6c6678f2020-09-21 10:50:30 -0400860 auto direct = GrAsDirectContext(canvas->recordingContext());
861
Robert Phillips99044e12020-01-29 08:37:01 -0500862 float cellWidth = kTileWidthHeight, cellHeight = kTileWidthHeight;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400863 if (fUseSubset) {
Robert Phillips99044e12020-01-29 08:37:01 -0500864 cellWidth *= 1.5f;
865 cellHeight *= 1.5f;
866 }
867
Brian Salomon6c6678f2020-09-21 10:50:30 -0400868 SkRect srcRect = SkRect::Make(fOriginalBMs[0].dimensions());
Michael Ludwiga6a84002019-04-12 15:03:02 -0400869 SkRect dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, srcRect.width(), srcRect.height());
Brian Salomon6c6678f2020-09-21 10:50:30 -0400870
Michael Ludwiga6a84002019-04-12 15:03:02 -0400871 SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400872 if (fUseSubset) {
873 srcRect.inset(kSubsetPadding, kSubsetPadding);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400874 // Draw a larger rectangle to ensure bilerp filtering would normally read outside the
875 // srcRect and hit the red pixels, if strict constraint weren't used.
876 dstRect.fRight = kLabelWidth + 1.5f * srcRect.width();
877 dstRect.fBottom = 1.5f * srcRect.height();
878 constraint = SkCanvas::kStrict_SrcRectConstraint;
879 }
880
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400881 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500882 SkPaint paint;
Michael Ludwiga6a84002019-04-12 15:03:02 -0400883 paint.setFilterQuality(kLow_SkFilterQuality);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500884 if (kIdentity_SkYUVColorSpace == cs) {
885 // The identity color space needs post processing to appear correctly
886 paint.setColorFilter(yuv_to_rgb_colorfilter());
887 }
888
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400889 for (int opaque : { 0, 1 }) {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400890 dstRect.offsetTo(dstRect.fLeft, kLabelHeight);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400891
Robert Phillips99044e12020-01-29 08:37:01 -0500892 draw_col_label(canvas, dstRect.fLeft + cellWidth / 2, cs, opaque);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400893
Brian Salomon68b245c2020-10-13 12:34:31 -0400894 canvas->drawBitmapRect(fOriginalBMs[opaque], srcRect, dstRect, nullptr, constraint);
Robert Phillips99044e12020-01-29 08:37:01 -0500895 dstRect.offset(0.f, cellHeight + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400896
Robert Phillipsbb749902019-06-10 17:20:12 -0400897 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400898 draw_row_label(canvas, dstRect.fTop, format);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500899 if (fUseTargetColorSpace && fImages[opaque][cs][format]) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500900 // Making a CS-specific version of a kIdentity_SkYUVColorSpace YUV image
901 // doesn't make a whole lot of sense. The colorSpace conversion will
902 // operate on the YUV components rather than the RGB components.
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500903 sk_sp<SkImage> csImage =
Adlai Holler3a220172020-07-15 10:37:50 -0400904 fImages[opaque][cs][format]->makeColorSpace(fTargetColorSpace, direct);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400905 canvas->drawImageRect(csImage, srcRect, dstRect, &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500906 } else {
Robert Phillips99044e12020-01-29 08:37:01 -0500907 canvas->drawImageRect(fImages[opaque][cs][format], srcRect, dstRect,
908 &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500909 }
Robert Phillips99044e12020-01-29 08:37:01 -0500910 dstRect.offset(0.f, cellHeight + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400911 }
912
Robert Phillips99044e12020-01-29 08:37:01 -0500913 dstRect.offset(cellWidth + kPad, 0.f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400914 }
915 }
916 }
917
918private:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400919 SkBitmap fOriginalBMs[2];
920 sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace + 1][kLast_YUVFormat + 1];
Robert Phillips2dd1b472019-03-21 09:00:20 -0400921 bool fUseTargetColorSpace;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400922 bool fUseSubset;
Brian Salomon7db71392020-10-16 10:05:21 -0400923 Type fImageType;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400924 sk_sp<SkColorSpace> fTargetColorSpace;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400925
John Stiles7571f9e2020-09-02 22:42:33 -0400926 using INHERITED = GM;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400927};
928
929//////////////////////////////////////////////////////////////////////////////
930
Brian Salomon6c6678f2020-09-21 10:50:30 -0400931DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
932 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400933 WackyYUVFormatsGM::Type::kFromTextures);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400934DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
935 /* subset */ true,
Brian Salomon7db71392020-10-16 10:05:21 -0400936 WackyYUVFormatsGM::Type::kFromTextures);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400937DEF_GM(return new WackyYUVFormatsGM(/* target cs */ true,
938 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400939 WackyYUVFormatsGM::Type::kFromTextures);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400940DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
941 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400942 WackyYUVFormatsGM::Type::kFromGenerator);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400943DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
944 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400945 WackyYUVFormatsGM::Type::kFromPixmaps);)
Brian Osmane9560492019-02-05 17:00:03 -0500946
Chris Dalton3a778372019-02-07 15:23:36 -0700947class YUVMakeColorSpaceGM : public GpuGM {
Brian Osmane9560492019-02-05 17:00:03 -0500948public:
949 YUVMakeColorSpaceGM() {
950 this->setBGColor(0xFFCCCCCC);
951 }
952
953protected:
954 SkString onShortName() override {
955 return SkString("yuv_make_color_space");
956 }
957
958 SkISize onISize() override {
959 int numCols = 4; // (transparent, opaque) x (untagged, tagged)
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400960 int numRows = 5; // original, YUV, subset, makeNonTextureImage, readPixels
Brian Osmane9560492019-02-05 17:00:03 -0500961 return SkISize::Make(numCols * (kTileWidthHeight + kPad) + kPad,
962 numRows * (kTileWidthHeight + kPad) + kPad);
963 }
964
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400965 void createBitmaps() {
Brian Osmane9560492019-02-05 17:00:03 -0500966 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
967 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
968 float innerRadius = 20.0f;
969
970 {
971 // transparent
972 SkTDArray<SkRect> circles;
973 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400974 fOriginalBMs[0] = make_bitmap(kN32_SkColorType, path, circles, false, false);
Brian Osmane9560492019-02-05 17:00:03 -0500975 }
976
977 {
978 // opaque
979 SkTDArray<SkRect> circles;
980 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400981 fOriginalBMs[1] = make_bitmap(kN32_SkColorType, path, circles, true, false);
Brian Osmane9560492019-02-05 17:00:03 -0500982 }
983
984 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
985 }
986
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400987 bool createImages(GrDirectContext* context) {
Brian Osmane9560492019-02-05 17:00:03 -0500988 for (bool opaque : { false, true }) {
989 PlaneData planes;
990 extract_planes(fOriginalBMs[opaque], kJPEG_SkYUVColorSpace, &planes);
991
992 SkBitmap resultBMs[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -0400993
Brian Salomonf7255d72020-04-08 15:57:08 -0400994 create_YUV(planes, kAYUV_YUVFormat, resultBMs, opaque);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400995
Brian Salomon94f65d72020-09-18 14:09:51 -0400996 YUVAPlanarConfig planarConfig(kAYUV_YUVFormat, opaque);
Brian Osmane9560492019-02-05 17:00:03 -0500997
Brian Salomon7db71392020-10-16 10:05:21 -0400998 auto yuvaPixmaps = planarConfig.makeYUVAPixmaps(fOriginalBMs[opaque].dimensions(),
999 kJPEG_Full_SkYUVColorSpace,
1000 resultBMs,
1001 SK_ARRAY_COUNT(resultBMs));
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001002
Brian Salomon7db71392020-10-16 10:05:21 -04001003 int i = 0;
1004 for (sk_sp<SkColorSpace> cs : {sk_sp<SkColorSpace>(nullptr),
1005 SkColorSpace::MakeSRGB()}) {
1006 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(yuvaPixmaps,
1007 GrMipmapped::kNo,
1008 std::move(cs));
1009 fImages[opaque][i++] =
1010 lazyYUV->refImage(context, sk_gpu_test::LazyYUVImage::Type::kFromTextures);
Brian Osmane9560492019-02-05 17:00:03 -05001011 }
Brian Osmane9560492019-02-05 17:00:03 -05001012 }
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001013
1014 // Some backends (e.g., Vulkan) require all work be completed for backend textures before
1015 // they are deleted. Since we don't know when we'll next have access to a direct context,
1016 // flush all the work now.
Greg Danielce9f0162020-06-30 13:42:46 -04001017 context->flush();
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001018 context->submit(true);
1019
1020 return true;
1021 }
1022
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001023 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001024 if (!context || context->abandoned()) {
1025 return DrawResult::kSkip;
1026 }
1027
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001028 this->createBitmaps();
1029 if (!this->createImages(context)) {
1030 *errorMsg = "Failed to create YUV images";
1031 return DrawResult::kFail;
1032 }
1033
1034 return DrawResult::kOk;
Brian Osmane9560492019-02-05 17:00:03 -05001035 }
1036
Robert Phillipsb795bea2020-06-25 12:38:53 -04001037 void onGpuTeardown() override {
1038 fImages[0][0] = fImages[0][1] = fImages[1][0] = fImages[1][1] = nullptr;
1039 }
1040
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001041 DrawResult onDraw(GrRecordingContext* rContext, GrRenderTargetContext*,
Adlai Holler3a220172020-07-15 10:37:50 -04001042 SkCanvas* canvas, SkString* msg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001043 SkASSERT(fImages[0][0] && fImages[0][1] && fImages[1][0] && fImages[1][1]);
Brian Osmane9560492019-02-05 17:00:03 -05001044
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001045 auto dContext = GrAsDirectContext(rContext);
1046 if (rContext && !dContext) {
Adlai Holler3a220172020-07-15 10:37:50 -04001047 *msg = "YUV ColorSpace image creation requires a direct context.";
1048 return DrawResult::kSkip;
1049 }
1050
Brian Osmane9560492019-02-05 17:00:03 -05001051 int x = kPad;
1052 for (int tagged : { 0, 1 }) {
1053 for (int opaque : { 0, 1 }) {
1054 int y = kPad;
1055
1056 auto raster = SkImage::MakeFromBitmap(fOriginalBMs[opaque])
Adlai Holler3a220172020-07-15 10:37:50 -04001057 ->makeColorSpace(fTargetColorSpace, nullptr);
Brian Osmane9560492019-02-05 17:00:03 -05001058 canvas->drawImage(raster, x, y);
1059 y += kTileWidthHeight + kPad;
1060
Greg Daniel85da3362020-03-09 15:18:35 -04001061 if (fImages[opaque][tagged]) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001062 auto yuv = fImages[opaque][tagged]->makeColorSpace(fTargetColorSpace, dContext);
Adlai Holler3a220172020-07-15 10:37:50 -04001063 SkASSERT(yuv);
Greg Daniel85da3362020-03-09 15:18:35 -04001064 SkASSERT(SkColorSpace::Equals(yuv->colorSpace(), fTargetColorSpace.get()));
1065 canvas->drawImage(yuv, x, y);
1066 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001067
Adlai Holler872a32c2020-07-10 14:33:22 -04001068 SkIRect bounds = SkIRect::MakeWH(kTileWidthHeight / 2, kTileWidthHeight / 2);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001069 auto subset = yuv->makeSubset(bounds, dContext);
Adlai Holler3a220172020-07-15 10:37:50 -04001070 SkASSERT(subset);
Greg Daniel85da3362020-03-09 15:18:35 -04001071 canvas->drawImage(subset, x, y);
1072 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001073
Greg Daniel85da3362020-03-09 15:18:35 -04001074 auto nonTexture = yuv->makeNonTextureImage();
Adlai Holler3a220172020-07-15 10:37:50 -04001075 SkASSERT(nonTexture);
Greg Daniel85da3362020-03-09 15:18:35 -04001076 canvas->drawImage(nonTexture, x, y);
1077 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001078
Greg Daniel85da3362020-03-09 15:18:35 -04001079 SkBitmap readBack;
1080 readBack.allocPixels(yuv->imageInfo());
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001081 SkAssertResult(yuv->readPixels(dContext, readBack.pixmap(), 0, 0));
Greg Daniel85da3362020-03-09 15:18:35 -04001082 canvas->drawBitmap(readBack, x, y);
1083 }
Brian Osmane9560492019-02-05 17:00:03 -05001084 x += kTileWidthHeight + kPad;
1085 }
1086 }
Adlai Holler3a220172020-07-15 10:37:50 -04001087 return DrawResult::kOk;
Brian Osmane9560492019-02-05 17:00:03 -05001088 }
1089
1090private:
1091 SkBitmap fOriginalBMs[2];
1092 sk_sp<SkImage> fImages[2][2];
Brian Osmane9560492019-02-05 17:00:03 -05001093 sk_sp<SkColorSpace> fTargetColorSpace;
1094
John Stiles7571f9e2020-09-02 22:42:33 -04001095 using INHERITED = GM;
Brian Osmane9560492019-02-05 17:00:03 -05001096};
1097
1098DEF_GM(return new YUVMakeColorSpaceGM();)
1099
John Stilesa6841be2020-08-06 14:11:56 -04001100} // namespace skiagm
Mike Reed6a5f7e22019-05-23 15:30:07 -04001101
1102///////////////
1103
Mike Reed6a5f7e22019-05-23 15:30:07 -04001104#include "include/effects/SkColorMatrix.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001105#include "src/core/SkAutoPixmapStorage.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001106#include "tools/Resources.h"
Mike Reed6a5f7e22019-05-23 15:30:07 -04001107
1108static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) {
1109 auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
1110 canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height());
1111 SkPaint paint;
1112 paint.setFilterQuality(kLow_SkFilterQuality);
1113 paint.setColorFilter(cf);
1114 paint.setBlendMode(SkBlendMode::kSrc);
1115 canvas->drawImage(img, 0, 0, &paint);
1116}
1117
1118static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) {
1119 float m[20];
1120 SkColorMatrix_RGB2YUV(cs, m);
1121
1122 memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A
1123 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]);
1124
1125 memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A
1126 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]);
1127
1128 memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A
1129 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]);
1130}
1131
1132static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y,
1133 const SkImage* a, const SkImage* b) {
1134 auto sh = SkShaders::Blend(SkBlendMode::kDifference, a->makeShader(), b->makeShader());
1135 SkPaint paint;
1136 paint.setShader(sh);
1137 canvas->save();
1138 canvas->translate(x, y);
1139 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1140
1141 SkColorMatrix cm;
1142 cm.setScale(64, 64, 64);
1143 paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm)));
1144 canvas->translate(0, a->height());
1145 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1146
1147 canvas->restore();
1148}
1149
1150// Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the
1151// resulting (recombined) images (gpu only for now).
1152//
1153class YUVSplitterGM : public skiagm::GM {
1154 sk_sp<SkImage> fOrig;
1155 SkAutoPixmapStorage fStorage[3];
1156 SkPixmap fPM[3];
1157
1158public:
1159 YUVSplitterGM() {}
1160
1161protected:
1162
1163 SkString onShortName() override {
1164 return SkString("yuv_splitter");
1165 }
1166
1167 SkISize onISize() override {
Brian Osman2b73e662019-11-01 10:02:24 -04001168 return SkISize::Make(1280, 768);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001169 }
1170
1171 void onOnceBeforeDraw() override {
1172 fOrig = GetResourceAsImage("images/mandrill_256.png");
1173
Brian Salomonf165f792020-10-05 13:25:05 -04001174 SkImageInfo info = SkImageInfo::MakeA8(fOrig->dimensions());
Mike Reed6a5f7e22019-05-23 15:30:07 -04001175 fStorage[0].alloc(info);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001176 fStorage[1].alloc(info);
1177 fStorage[2].alloc(info);
1178 for (int i = 0; i < 3; ++i) {
1179 fPM[i] = fStorage[i];
1180 }
1181 }
1182
1183 void onDraw(SkCanvas* canvas) override {
1184 SkYUVAIndex indices[4];
Robert Phillipse0735522020-01-31 11:03:32 -05001185 indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
1186 indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
1187 indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR};
Mike Reed6a5f7e22019-05-23 15:30:07 -04001188 indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kR};
1189
1190 canvas->translate(fOrig->width(), 0);
1191 canvas->save();
Brian Osman2b73e662019-11-01 10:02:24 -04001192 for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace,
1193 kBT2020_SkYUVColorSpace}) {
Mike Reed6a5f7e22019-05-23 15:30:07 -04001194 split_into_yuv(fOrig.get(), cs, fPM);
Brian Salomonf165f792020-10-05 13:25:05 -04001195 SkYUVAInfo yuvaInfo(fOrig->dimensions(), SkYUVAInfo::PlanarConfig::kY_U_V_444, cs);
1196 auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, fPM);
1197 auto img = SkImage::MakeFromYUVAPixmaps(canvas->recordingContext(),
1198 yuvaPixmaps,
1199 GrMipMapped::kNo,
1200 /* limit to max tex size */ false,
1201 /* color space */ nullptr);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001202 if (img) {
1203 canvas->drawImage(img, 0, 0, nullptr);
1204 draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get());
1205 }
1206 canvas->translate(fOrig->width(), 0);
1207 }
1208 canvas->restore();
1209 canvas->translate(-fOrig->width(), 0);
1210
1211 canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0, nullptr);
1212 canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height(), nullptr);
1213 canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]),
1214 0, fPM[0].height() + fPM[1].height(), nullptr);
1215 }
1216
1217private:
John Stiles7571f9e2020-09-02 22:42:33 -04001218 using INHERITED = GM;
Mike Reed6a5f7e22019-05-23 15:30:07 -04001219};
1220DEF_GM( return new YUVSplitterGM; )