blob: d87b62837cfceacab675646b1d02e228b15d3936 [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/gpu/GrBackendSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040036#include "include/gpu/GrConfig.h"
Robert Phillipsb87b39b2020-07-01 14:45:24 -040037#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040038#include "include/gpu/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040039#include "include/gpu/GrTypes.h"
40#include "include/private/GrTypesPriv.h"
41#include "include/private/SkTArray.h"
42#include "include/private/SkTDArray.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050043#include "include/private/SkTPin.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040044#include "include/private/SkTemplates.h"
45#include "include/utils/SkTextUtils.h"
Brian Salomon94f65d72020-09-18 14:09:51 -040046#include "src/core/SkConvertPixels.h"
Brian Osmanac8a16c2019-10-31 10:24:12 -040047#include "src/core/SkYUVMath.h"
Robert Phillips95c250c2020-06-29 15:36:12 -040048#include "src/gpu/GrCaps.h"
Robert Phillips95c250c2020-06-29 15:36:12 -040049#include "src/gpu/GrRecordingContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040050#include "tools/ToolUtils.h"
Robert Phillipsf105d382020-06-19 14:27:14 -040051#include "tools/gpu/YUVUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040052
53#include <math.h>
54#include <string.h>
55#include <initializer_list>
56#include <memory>
57#include <utility>
58
Brian Salomoneebe7352020-12-09 16:37:04 -050059class GrSurfaceDrawContext;
Robert Phillips51c89e42018-10-05 13:30:43 -040060
Robert Phillipsbfa76f22018-10-03 12:12:26 -040061static const int kTileWidthHeight = 128;
62static const int kLabelWidth = 64;
63static const int kLabelHeight = 32;
Brian Salomon6c6678f2020-09-21 10:50:30 -040064static const int kSubsetPadding = 8;
Robert Phillipsbfa76f22018-10-03 12:12:26 -040065static const int kPad = 1;
Robert Phillipsbfa76f22018-10-03 12:12:26 -040066
67enum YUVFormat {
Robert Phillipsbb749902019-06-10 17:20:12 -040068 // 4:2:0 formats, 24 bpp
69 kP016_YUVFormat, // 16-bit Y plane + 2x2 down sampled interleaved U/V plane (2 textures)
70 // 4:2:0 formats, "15 bpp" (but really 24 bpp)
71 kP010_YUVFormat, // same as kP016 except "10 bpp". Note that it is the same memory layout
72 // except that the bottom 6 bits are zeroed out (2 textures)
73 // TODO: we're cheating a bit w/ P010 and just treating it as unorm 16. This means its
74 // fully saturated values are 65504 rather than 65535 (that is just .9995 out of 1.0 though).
75
Robert Phillips17a3a0b2019-09-18 13:56:54 -040076 // This is laid out the same as kP016 and kP010 but uses F16 unstead of U16. In this case
77 // the 10 bits/channel vs 16 bits/channel distinction isn't relevant.
78 kP016F_YUVFormat,
79
Robert Phillipsbb749902019-06-10 17:20:12 -040080 // 4:4:4 formats, 64 bpp
81 kY416_YUVFormat, // 16-bit AVYU values all interleaved (1 texture)
82
Robert Phillipsbfa76f22018-10-03 12:12:26 -040083 // 4:4:4 formats, 32 bpp
Robert Phillipsbb749902019-06-10 17:20:12 -040084 kAYUV_YUVFormat, // 8-bit YUVA values all interleaved (1 texture)
85 kY410_YUVFormat, // AVYU w/ 10bpp for YUV and 2 for A all interleaved (1 texture)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040086
87 // 4:2:0 formats, 12 bpp
Robert Phillipsbb749902019-06-10 17:20:12 -040088 kNV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled interleaved U/V planes (2 textures)
89 kNV21_YUVFormat, // same as kNV12 but w/ U/V reversed in the interleaved texture (2 textures)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040090
Robert Phillipsbb749902019-06-10 17:20:12 -040091 kI420_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled U and V planes (3 textures)
92 kYV12_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled V and U planes (3 textures)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040093
94 kLast_YUVFormat = kYV12_YUVFormat
95};
96
Brian Salomon94f65d72020-09-18 14:09:51 -040097// Does the YUVFormat contain a slot for alpha? If not an external alpha plane is required for
98// transparency.
99static bool has_alpha_channel(YUVFormat format) {
100 switch (format) {
101 case kP016_YUVFormat: return false;
102 case kP010_YUVFormat: return false;
103 case kP016F_YUVFormat: return false;
104 case kY416_YUVFormat: return true;
105 case kAYUV_YUVFormat: return true;
106 case kY410_YUVFormat: return true;
107 case kNV12_YUVFormat: return false;
108 case kNV21_YUVFormat: return false;
109 case kI420_YUVFormat: return false;
110 case kYV12_YUVFormat: return false;
111 }
112 SkUNREACHABLE;
113}
114
Brian Salomonf7255d72020-04-08 15:57:08 -0400115class YUVAPlanarConfig {
116public:
Brian Salomon96796122021-01-19 12:11:07 -0500117 YUVAPlanarConfig(YUVFormat format, bool opaque, SkEncodedOrigin origin) : fOrigin(origin) {
Brian Salomon94f65d72020-09-18 14:09:51 -0400118 switch (format) {
119 case kP016_YUVFormat:
120 case kP010_YUVFormat:
121 case kP016F_YUVFormat:
122 case kNV12_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400123 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500124 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_UV;
125 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400126 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500127 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_UV_A;
128 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400129 }
130 break;
131 case kY416_YUVFormat:
132 case kY410_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400133 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500134 fPlaneConfig = SkYUVAInfo::PlaneConfig::kUYV;
135 fSubsampling = SkYUVAInfo::Subsampling::k444;
Brian Salomon94f65d72020-09-18 14:09:51 -0400136 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500137 fPlaneConfig = SkYUVAInfo::PlaneConfig::kUYVA;
138 fSubsampling = SkYUVAInfo::Subsampling::k444;
Brian Salomon94f65d72020-09-18 14:09:51 -0400139 }
140 break;
141 case kAYUV_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400142 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500143 fPlaneConfig = SkYUVAInfo::PlaneConfig::kYUV;
144 fSubsampling = SkYUVAInfo::Subsampling::k444;
Brian Salomon94f65d72020-09-18 14:09:51 -0400145 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500146 fPlaneConfig = SkYUVAInfo::PlaneConfig::kYUVA;
147 fSubsampling = SkYUVAInfo::Subsampling::k444;
Brian Salomon94f65d72020-09-18 14:09:51 -0400148 }
149 break;
150 case kNV21_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400151 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500152 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_VU;
153 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400154 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500155 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_VU_A;
156 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400157 }
158 break;
159 case kI420_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400160 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500161 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_U_V;
162 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400163 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500164 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_U_V_A;
165 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400166 }
167 break;
168 case kYV12_YUVFormat:
Brian Salomon94f65d72020-09-18 14:09:51 -0400169 if (opaque) {
Brian Salomone4387382020-11-11 16:34:19 -0500170 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_V_U;
171 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400172 } else {
Brian Salomone4387382020-11-11 16:34:19 -0500173 fPlaneConfig = SkYUVAInfo::PlaneConfig::kY_V_U_A;
174 fSubsampling = SkYUVAInfo::Subsampling::k420;
Brian Salomon94f65d72020-09-18 14:09:51 -0400175 }
176 break;
177 }
178 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400179
Brian Salomone4387382020-11-11 16:34:19 -0500180 int numPlanes() const { return SkYUVAInfo::NumPlanes(fPlaneConfig); }
Brian Salomonf7255d72020-04-08 15:57:08 -0400181
Brian Salomon6c6678f2020-09-21 10:50:30 -0400182 SkYUVAPixmaps makeYUVAPixmaps(SkISize dimensions,
183 SkYUVColorSpace yuvColorSpace,
184 const SkBitmap bitmaps[],
185 int numBitmaps) const;
186
Brian Salomonf7255d72020-04-08 15:57:08 -0400187private:
Brian Salomone4387382020-11-11 16:34:19 -0500188 SkYUVAInfo::PlaneConfig fPlaneConfig;
189 SkYUVAInfo::Subsampling fSubsampling;
Brian Salomon96796122021-01-19 12:11:07 -0500190 SkEncodedOrigin fOrigin;
Brian Salomonf7255d72020-04-08 15:57:08 -0400191};
192
Brian Salomon6c6678f2020-09-21 10:50:30 -0400193SkYUVAPixmaps YUVAPlanarConfig::makeYUVAPixmaps(SkISize dimensions,
194 SkYUVColorSpace yuvColorSpace,
195 const SkBitmap bitmaps[],
196 int numBitmaps) const {
Brian Salomon96796122021-01-19 12:11:07 -0500197 SkYUVAInfo info(dimensions, fPlaneConfig, fSubsampling, yuvColorSpace, fOrigin);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400198 SkPixmap pmaps[SkYUVAInfo::kMaxPlanes];
199 int n = info.numPlanes();
200 if (numBitmaps < n) {
201 return {};
202 }
203 for (int i = 0; i < n; ++i) {
204 pmaps[i] = bitmaps[i].pixmap();
205 }
206 return SkYUVAPixmaps::FromExternalPixmaps(info, pmaps);
207}
208
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400209// All the planes we need to construct the various YUV formats
210struct PlaneData {
211 SkBitmap fYFull;
212 SkBitmap fUFull;
213 SkBitmap fVFull;
214 SkBitmap fAFull;
215 SkBitmap fUQuarter; // 2x2 downsampled U channel
216 SkBitmap fVQuarter; // 2x2 downsampled V channel
Robert Phillips429f0d32019-09-11 17:03:28 -0400217
218 SkBitmap fFull;
219 SkBitmap fQuarter; // 2x2 downsampled YUVA
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400220};
221
222// Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle
223// and have tangents 'v1' and 'v2'.
224static void add_arc(SkPath* path,
225 const SkPoint& o1, const SkVector& v1,
226 const SkPoint& o2, const SkVector& v2,
227 SkTDArray<SkRect>* circles, bool takeLongWayRound) {
228
229 SkVector v3 = { -v1.fY, v1.fX };
230 SkVector v4 = { v2.fY, -v2.fX };
231
232 SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4);
233 SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY };
234
235 SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t };
236
237 if (circles) {
238 circles->push_back(r);
239 }
240
241 SkVector startV = o1 - center, endV = o2 - center;
242 startV.normalize();
243 endV.normalize();
244
245 SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX));
246 SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX));
247
248 startDeg += 360.0f;
249 startDeg = fmodf(startDeg, 360.0f);
250
251 endDeg += 360.0f;
252 endDeg = fmodf(endDeg, 360.0f);
253
254 if (endDeg < startDeg) {
255 endDeg += 360.0f;
256 }
257
258 SkScalar sweepDeg = SkTAbs(endDeg - startDeg);
259 if (!takeLongWayRound) {
260 sweepDeg = sweepDeg - 360;
261 }
262
263 path->arcTo(r, startDeg, sweepDeg, false);
264}
265
266static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius,
267 SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) {
268 if (numLobes <= 1) {
269 return SkPath();
270 }
271
272 SkPath p;
273
274 int numDivisions = 2 * numLobes;
275 SkScalar fullLobeDegrees = 360.0f / numLobes;
276 SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f);
277 SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f);
278 SkMatrix outerStep, innerStep;
279 outerStep.setRotate(outDegrees);
280 innerStep.setRotate(innerDegrees);
281 SkVector curV = SkVector::Make(0.0f, 1.0f);
282
283 if (circles) {
284 circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius,
285 o.fX + innerRadius, o.fY + innerRadius));
286 }
287
288 p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY);
289
290 for (int i = 0; i < numDivisions; ++i) {
291
292 SkVector nextV;
293 if (0 == (i % 2)) {
294 nextV = outerStep.mapVector(curV.fX, curV.fY);
295
296 SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX,
297 o.fY + outerRadius * curV.fY);
298 SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX,
299 o.fY + outerRadius * nextV.fY);
300
301 p.lineTo(top);
302 add_arc(&p, top, curV, nextTop, nextV, circles, true);
303 } else {
304 nextV = innerStep.mapVector(curV.fX, curV.fY);
305
306 SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX,
307 o.fY + innerRadius * curV.fY);
308 SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX,
309 o.fY + innerRadius * nextV.fY);
310
311 p.lineTo(bot);
312 add_arc(&p, bot, curV, nextBot, nextV, nullptr, false);
313 }
314
315 curV = nextV;
316 }
317
318 p.close();
319
320 return p;
321}
322
Robert Phillips2dd1b472019-03-21 09:00:20 -0400323static SkBitmap make_bitmap(SkColorType colorType, const SkPath& path,
Michael Ludwiga6a84002019-04-12 15:03:02 -0400324 const SkTDArray<SkRect>& circles, bool opaque, bool padWithRed) {
Brian Salomon96796122021-01-19 12:11:07 -0500325 const SkColor kGreen = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104));
326 const SkColor kBlue = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252));
327 const SkColor kYellow = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117));
328 const SkColor kMagenta = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 60, 217));
329 const SkColor kCyan = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 45, 237, 205));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400330
Brian Salomon6c6678f2020-09-21 10:50:30 -0400331 int widthHeight = kTileWidthHeight + (padWithRed ? 2 * kSubsetPadding : 0);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400332
333 SkImageInfo ii = SkImageInfo::Make(widthHeight, widthHeight,
Robert Phillips2dd1b472019-03-21 09:00:20 -0400334 colorType, kPremul_SkAlphaType);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400335
336 SkBitmap bm;
337 bm.allocPixels(ii);
338
Robert Phillips2dd1b472019-03-21 09:00:20 -0400339 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(ii,
340 bm.getPixels(),
341 bm.rowBytes());
Michael Ludwiga6a84002019-04-12 15:03:02 -0400342 if (padWithRed) {
343 canvas->clear(SK_ColorRED);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400344 canvas->translate(kSubsetPadding, kSubsetPadding);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400345 canvas->clipRect(SkRect::MakeWH(kTileWidthHeight, kTileWidthHeight));
346 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400347 canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT);
348
349 SkPaint paint;
350 paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency
351 paint.setColor(kBlue);
352
353 canvas->drawPath(path, paint);
354
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400355 paint.setBlendMode(SkBlendMode::kSrc);
356 for (int i = 0; i < circles.count(); ++i) {
Brian Salomon96796122021-01-19 12:11:07 -0500357 SkColor color;
358 switch (i % 3) {
359 case 0: color = kYellow; break;
360 case 1: color = kMagenta; break;
361 default: color = kCyan; break;
362 }
363 paint.setColor(color);
364 paint.setAlpha(opaque ? 0xFF : 0x40);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400365 SkRect r = circles[i];
366 r.inset(r.width()/4, r.height()/4);
367 canvas->drawOval(r, paint);
368 }
369
370 return bm;
371}
372
Brian Osmanac8a16c2019-10-31 10:24:12 -0400373static void convert_rgba_to_yuva(const float mtx[20], SkColor col, uint8_t yuv[4]) {
374 const uint8_t r = SkColorGetR(col);
375 const uint8_t g = SkColorGetG(col);
376 const uint8_t b = SkColorGetB(col);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400377
Brian Osmanaba642c2020-02-06 12:52:25 -0500378 yuv[0] = SkTPin(SkScalarRoundToInt(mtx[ 0]*r + mtx[ 1]*g + mtx[ 2]*b + mtx[ 4]*255), 0, 255);
379 yuv[1] = SkTPin(SkScalarRoundToInt(mtx[ 5]*r + mtx[ 6]*g + mtx[ 7]*b + mtx[ 9]*255), 0, 255);
380 yuv[2] = SkTPin(SkScalarRoundToInt(mtx[10]*r + mtx[11]*g + mtx[12]*b + mtx[14]*255), 0, 255);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400381 yuv[3] = SkColorGetA(col);
382}
383
Brian Salomon96796122021-01-19 12:11:07 -0500384static void extract_planes(const SkBitmap& origBM,
385 SkYUVColorSpace yuvColorSpace,
386 SkEncodedOrigin origin,
387 PlaneData* planes) {
388 SkImageInfo ii = origBM.info();
389 if (SkEncodedOriginSwapsWidthHeight(origin)) {
390 ii = ii.makeWH(ii.height(), ii.width());
391 }
392 SkBitmap orientedBM;
393 orientedBM.allocPixels(ii);
394 SkCanvas canvas(orientedBM);
395 SkMatrix matrix = SkEncodedOriginToMatrix(origin, origBM.width(), origBM.height());
396 SkAssertResult(matrix.invert(&matrix));
397 canvas.concat(matrix);
Mike Reed607a3822021-01-24 19:49:21 -0500398 canvas.drawImage(origBM.asImage(), 0, 0);
Brian Salomon96796122021-01-19 12:11:07 -0500399
400 if (yuvColorSpace == kIdentity_SkYUVColorSpace) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500401 // To test the identity color space we use JPEG YUV planes
402 yuvColorSpace = kJPEG_SkYUVColorSpace;
403 }
404
Brian Salomon96796122021-01-19 12:11:07 -0500405 SkASSERT(!(ii.width() % 2));
406 SkASSERT(!(ii.height() % 2));
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400407 planes->fYFull.allocPixels(
Brian Salomon96796122021-01-19 12:11:07 -0500408 SkImageInfo::Make(ii.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400409 planes->fUFull.allocPixels(
Brian Salomon96796122021-01-19 12:11:07 -0500410 SkImageInfo::Make(ii.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400411 planes->fVFull.allocPixels(
Brian Salomon96796122021-01-19 12:11:07 -0500412 SkImageInfo::Make(ii.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
413 planes->fAFull.allocPixels(SkImageInfo::MakeA8(ii.dimensions()));
414 planes->fUQuarter.allocPixels(SkImageInfo::Make(ii.width()/2, ii.height()/2,
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400415 kGray_8_SkColorType, kUnpremul_SkAlphaType));
Brian Salomon96796122021-01-19 12:11:07 -0500416 planes->fVQuarter.allocPixels(SkImageInfo::Make(ii.width()/2, ii.height()/2,
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400417 kGray_8_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400418
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400419 planes->fFull.allocPixels(
Brian Salomon96796122021-01-19 12:11:07 -0500420 SkImageInfo::Make(ii.dimensions(), kRGBA_F32_SkColorType, kUnpremul_SkAlphaType));
421 planes->fQuarter.allocPixels(SkImageInfo::Make(ii.width()/2, ii.height()/2,
Robert Phillips429f0d32019-09-11 17:03:28 -0400422 kRGBA_F32_SkColorType, kUnpremul_SkAlphaType));
423
Brian Osmanac8a16c2019-10-31 10:24:12 -0400424 float mtx[20];
425 SkColorMatrix_RGB2YUV(yuvColorSpace, mtx);
426
Robert Phillips429f0d32019-09-11 17:03:28 -0400427 SkColor4f* dst = (SkColor4f *) planes->fFull.getAddr(0, 0);
Brian Salomon96796122021-01-19 12:11:07 -0500428 for (int y = 0; y < orientedBM.height(); ++y) {
429 for (int x = 0; x < orientedBM.width(); ++x) {
430 SkColor col = orientedBM.getColor(x, y);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400431
Robert Phillips1c7062d2018-10-04 10:44:53 -0400432 uint8_t yuva[4];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400433
Brian Osmanac8a16c2019-10-31 10:24:12 -0400434 convert_rgba_to_yuva(mtx, col, yuva);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400435
436 *planes->fYFull.getAddr8(x, y) = yuva[0];
437 *planes->fUFull.getAddr8(x, y) = yuva[1];
438 *planes->fVFull.getAddr8(x, y) = yuva[2];
439 *planes->fAFull.getAddr8(x, y) = yuva[3];
Robert Phillips429f0d32019-09-11 17:03:28 -0400440
441 // TODO: render in F32 rather than converting here
442 dst->fR = yuva[0] / 255.0f;
443 dst->fG = yuva[1] / 255.0f;
444 dst->fB = yuva[2] / 255.0f;
445 dst->fA = yuva[3] / 255.0f;
446 ++dst;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400447 }
448 }
449
Robert Phillips429f0d32019-09-11 17:03:28 -0400450 dst = (SkColor4f *) planes->fQuarter.getAddr(0, 0);
Brian Salomon96796122021-01-19 12:11:07 -0500451 for (int y = 0; y < orientedBM.height()/2; ++y) {
452 for (int x = 0; x < orientedBM.width()/2; ++x) {
Robert Phillips429f0d32019-09-11 17:03:28 -0400453 uint32_t yAccum = 0, uAccum = 0, vAccum = 0, aAccum = 0;
454
455 yAccum += *planes->fYFull.getAddr8(2*x, 2*y);
456 yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y);
457 yAccum += *planes->fYFull.getAddr8(2*x, 2*y+1);
458 yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y+1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400459
460 uAccum += *planes->fUFull.getAddr8(2*x, 2*y);
461 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y);
462 uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1);
463 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1);
464
465 *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f;
466
467 vAccum += *planes->fVFull.getAddr8(2*x, 2*y);
468 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y);
469 vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1);
470 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1);
471
Robert Phillips1c7062d2018-10-04 10:44:53 -0400472 *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f;
Robert Phillips429f0d32019-09-11 17:03:28 -0400473
474 aAccum += *planes->fAFull.getAddr8(2*x, 2*y);
475 aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y);
476 aAccum += *planes->fAFull.getAddr8(2*x, 2*y+1);
477 aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y+1);
478
479 // TODO: render in F32 rather than converting here
480 dst->fR = yAccum / (4.0f * 255.0f);
481 dst->fG = uAccum / (4.0f * 255.0f);
482 dst->fB = vAccum / (4.0f * 255.0f);
483 dst->fA = aAccum / (4.0f * 255.0f);
484 ++dst;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400485 }
486 }
487}
488
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400489// Create a 2x2 downsampled SkBitmap. It is stored in an RG texture. It can optionally be
Robert Phillips429f0d32019-09-11 17:03:28 -0400490// uv (i.e., NV12) or vu (i.e., NV21).
Robert Phillipsbb749902019-06-10 17:20:12 -0400491static SkBitmap make_quarter_2_channel(const SkBitmap& fullY,
492 const SkBitmap& quarterU,
493 const SkBitmap& quarterV,
494 bool uv) {
495 SkBitmap result;
496
Robert Phillipsbb749902019-06-10 17:20:12 -0400497 result.allocPixels(SkImageInfo::Make(fullY.width()/2,
498 fullY.height()/2,
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400499 kR8G8_unorm_SkColorType,
Robert Phillipsbb749902019-06-10 17:20:12 -0400500 kUnpremul_SkAlphaType));
501
502 for (int y = 0; y < fullY.height()/2; ++y) {
503 for (int x = 0; x < fullY.width()/2; ++x) {
504 uint8_t u8 = *quarterU.getAddr8(x, y);
505 uint8_t v8 = *quarterV.getAddr8(x, y);
506
507 if (uv) {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400508 *result.getAddr16(x, y) = (v8 << 8) | u8;
Robert Phillipsbb749902019-06-10 17:20:12 -0400509 } else {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400510 *result.getAddr16(x, y) = (u8 << 8) | v8;
Robert Phillipsbb749902019-06-10 17:20:12 -0400511 }
512 }
513 }
514
515 return result;
516}
517
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400518// Create some flavor of a 16bits/channel bitmap from a RGBA_F32 source
519static SkBitmap make_16(const SkBitmap& src, SkColorType dstCT,
520 std::function<void(uint16_t* dstPixel, const float* srcPixel)> convert) {
Robert Phillips429f0d32019-09-11 17:03:28 -0400521 SkASSERT(src.colorType() == kRGBA_F32_SkColorType);
522
523 SkBitmap result;
524
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400525 result.allocPixels(SkImageInfo::Make(src.dimensions(), dstCT, kUnpremul_SkAlphaType));
Robert Phillips429f0d32019-09-11 17:03:28 -0400526
Robert Phillips429f0d32019-09-11 17:03:28 -0400527 for (int y = 0; y < src.height(); ++y) {
528 for (int x = 0; x < src.width(); ++x) {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400529 const float* srcPixel = (const float*) src.getAddr(x, y);
530 uint16_t* dstPixel = (uint16_t*) result.getAddr(x, y);
Robert Phillips429f0d32019-09-11 17:03:28 -0400531
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400532 convert(dstPixel, srcPixel);
Robert Phillips429f0d32019-09-11 17:03:28 -0400533 }
534 }
535
536 return result;
537}
538
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400539static uint16_t flt_2_uint16(float flt) { return SkScalarRoundToInt(flt * 65535.0f); }
Robert Phillips429f0d32019-09-11 17:03:28 -0400540
Brian Salomonf7255d72020-04-08 15:57:08 -0400541// Recombine the separate planes into some YUV format. Returns the number of planes.
542static int create_YUV(const PlaneData& planes,
543 YUVFormat yuvFormat,
544 SkBitmap resultBMs[],
545 bool opaque) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400546 int nextLayer = 0;
547
548 switch (yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400549 case kY416_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400550 resultBMs[nextLayer++] = make_16(planes.fFull, kR16G16B16A16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400551 [] (uint16_t* dstPixel, const float* srcPixel) {
552 dstPixel[0] = flt_2_uint16(srcPixel[1]); // U
553 dstPixel[1] = flt_2_uint16(srcPixel[0]); // Y
554 dstPixel[2] = flt_2_uint16(srcPixel[2]); // V
555 dstPixel[3] = flt_2_uint16(srcPixel[3]); // A
556 });
Robert Phillipsbb749902019-06-10 17:20:12 -0400557 break;
558 }
Jim Van Verth976a6b02018-10-17 15:27:19 -0400559 case kAYUV_YUVFormat: {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400560 SkBitmap yuvaFull;
561
Jim Van Verth47133fd2018-10-19 22:09:28 -0400562 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
563 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400564
565 for (int y = 0; y < planes.fYFull.height(); ++y) {
566 for (int x = 0; x < planes.fYFull.width(); ++x) {
567
568 uint8_t Y = *planes.fYFull.getAddr8(x, y);
569 uint8_t U = *planes.fUFull.getAddr8(x, y);
570 uint8_t V = *planes.fVFull.getAddr8(x, y);
571 uint8_t A = *planes.fAFull.getAddr8(x, y);
572
573 // NOT premul!
Jim Van Verth47133fd2018-10-19 22:09:28 -0400574 // V and Y swapped to match RGBA layout
Robert Phillips2dd1b472019-03-21 09:00:20 -0400575 SkColor c = SkColorSetARGB(A, V, U, Y);
576 *yuvaFull.getAddr32(x, y) = c;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400577 }
578 }
579
580 resultBMs[nextLayer++] = yuvaFull;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400581 break;
582 }
Robert Phillips2dd1b472019-03-21 09:00:20 -0400583 case kY410_YUVFormat: {
584 SkBitmap yuvaFull;
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400585 uint32_t Y, U, V;
586 uint8_t A;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400587
588 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
589 kRGBA_1010102_SkColorType,
590 kUnpremul_SkAlphaType));
591
592 for (int y = 0; y < planes.fYFull.height(); ++y) {
593 for (int x = 0; x < planes.fYFull.width(); ++x) {
594
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400595 Y = SkScalarRoundToInt((*planes.fYFull.getAddr8(x, y) / 255.0f) * 1023.0f);
596 U = SkScalarRoundToInt((*planes.fUFull.getAddr8(x, y) / 255.0f) * 1023.0f);
597 V = SkScalarRoundToInt((*planes.fVFull.getAddr8(x, y) / 255.0f) * 1023.0f);
598 A = SkScalarRoundToInt((*planes.fAFull.getAddr8(x, y) / 255.0f) * 3.0f);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400599
600 // NOT premul!
Brian Salomonf7255d72020-04-08 15:57:08 -0400601 *yuvaFull.getAddr32(x, y) = (A << 30) | (V << 20) | (Y << 10) | (U << 0);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400602 }
603 }
604
605 resultBMs[nextLayer++] = yuvaFull;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400606 break;
607 }
Robert Phillipsbb749902019-06-10 17:20:12 -0400608 case kP016_YUVFormat: // fall through
Robert Phillips429f0d32019-09-11 17:03:28 -0400609 case kP010_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400610 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400611 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
612 (uint16_t* dstPixel, const float* srcPixel) {
613 uint16_t val16 = flt_2_uint16(srcPixel[0]);
614 dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0)
615 : val16;
616 });
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400617 resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400618 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
619 (uint16_t* dstPixel, const float* srcPixel) {
620 uint16_t u16 = flt_2_uint16(srcPixel[1]);
621 uint16_t v16 = flt_2_uint16(srcPixel[2]);
622 dstPixel[0] = tenBitsPP ? (u16 & 0xFFC0) : u16;
623 dstPixel[1] = tenBitsPP ? (v16 & 0xFFC0) : v16;
624 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400625 if (!opaque) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400626 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType,
627 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
628 (uint16_t* dstPixel, const float* srcPixel) {
629 uint16_t val16 = flt_2_uint16(srcPixel[3]);
630 dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0)
631 : val16;
632 });
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400633 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400634 return nextLayer;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400635 }
636 case kP016F_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400637 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400638 [] (uint16_t* dstPixel, const float* srcPixel) {
639 dstPixel[0] = SkFloatToHalf(srcPixel[0]);
640 });
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400641 resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_float_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400642 [] (uint16_t* dstPixel, const float* srcPixel) {
643 dstPixel[0] = SkFloatToHalf(srcPixel[1]);
644 dstPixel[1] = SkFloatToHalf(srcPixel[2]);
645 });
646 if (!opaque) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400647 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType,
648 [] (uint16_t* dstPixel, const float* srcPixel) {
649 dstPixel[0] = SkFloatToHalf(srcPixel[3]);
650 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400651 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400652 return nextLayer;
Robert Phillips429f0d32019-09-11 17:03:28 -0400653 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400654 case kNV12_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400655 SkBitmap uvQuarter = make_quarter_2_channel(planes.fYFull,
656 planes.fUQuarter,
657 planes.fVQuarter, true);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400658 resultBMs[nextLayer++] = planes.fYFull;
659 resultBMs[nextLayer++] = uvQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400660 break;
661 }
662 case kNV21_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400663 SkBitmap vuQuarter = make_quarter_2_channel(planes.fYFull,
664 planes.fUQuarter,
665 planes.fVQuarter, false);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400666 resultBMs[nextLayer++] = planes.fYFull;
667 resultBMs[nextLayer++] = vuQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400668 break;
669 }
670 case kI420_YUVFormat:
671 resultBMs[nextLayer++] = planes.fYFull;
672 resultBMs[nextLayer++] = planes.fUQuarter;
673 resultBMs[nextLayer++] = planes.fVQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400674 break;
675 case kYV12_YUVFormat:
676 resultBMs[nextLayer++] = planes.fYFull;
677 resultBMs[nextLayer++] = planes.fVQuarter;
678 resultBMs[nextLayer++] = planes.fUQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400679 break;
680 }
681
Brian Salomon94f65d72020-09-18 14:09:51 -0400682 if (!opaque && !has_alpha_channel(yuvFormat)) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400683 resultBMs[nextLayer++] = planes.fAFull;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400684 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400685 return nextLayer;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400686}
687
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400688static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) {
Brian Osmanb107a112020-07-23 14:22:22 -0400689 static const char* kYUVColorSpaceNames[] = {"JPEG", "601", "709F", "709L",
690 "2020_8F", "2020_8L", "2020_10F", "2020_10L",
691 "2020_12F", "2020_12L", "Identity"};
Brian Salomon4dea72a2019-12-18 10:43:10 -0500692 static_assert(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace + 1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400693
Mike Reed91919132019-01-02 12:21:01 -0500694 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500695 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500696 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400697
698 SkRect textRect;
699 SkString colLabel;
700
701 colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400702 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400703 int y = textRect.height();
704
Mike Reed91919132019-01-02 12:21:01 -0500705 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400706
707 colLabel.printf("%s", opaque ? "Opaque" : "Transparent");
Mike Reed71f5a0b2018-10-25 16:12:39 -0400708
Ben Wagner51e15a62019-05-07 15:38:46 -0400709 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400710 y += textRect.height();
711
Mike Reed91919132019-01-02 12:21:01 -0500712 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400713}
714
715static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400716 static const char* kYUVFormatNames[] = {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400717 "P016", "P010", "P016F", "Y416", "AYUV", "Y410", "NV12", "NV21", "I420", "YV12"
Robert Phillipsbb749902019-06-10 17:20:12 -0400718 };
Brian Salomon4dea72a2019-12-18 10:43:10 -0500719 static_assert(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat + 1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400720
Mike Reed91919132019-01-02 12:21:01 -0500721 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500722 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500723 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400724
725 SkRect textRect;
726 SkString rowLabel;
727
728 rowLabel.printf("%s", kYUVFormatNames[yuvFormat]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400729 font.measureText(rowLabel.c_str(), rowLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400730 y += kTileWidthHeight/2 + textRect.height()/2;
731
Hal Canary89a644b2019-01-07 09:36:09 -0500732 canvas->drawString(rowLabel, 0, y, font, paint);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400733}
734
Robert Phillips0a22ba82019-03-06 12:36:47 -0500735static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
736 static const float kJPEGConversionMatrix[20] = {
Mike Reede869a1e2019-04-30 12:18:54 -0400737 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
738 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
739 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
Robert Phillips0a22ba82019-03-06 12:36:47 -0500740 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
741 };
742
Mike Reede869a1e2019-04-30 12:18:54 -0400743 return SkColorFilters::Matrix(kJPEGConversionMatrix);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500744}
745
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400746namespace skiagm {
747
748// This GM creates an opaque and transparent bitmap, extracts the planes and then recombines
749// them into various YUV formats. It then renders the results in the grid:
750//
Robert Phillips2dd1b472019-03-21 09:00:20 -0400751// JPEG 601 709 Identity
752// Transparent Opaque Transparent Opaque Transparent Opaque Transparent Opaque
Robert Phillipsbb749902019-06-10 17:20:12 -0400753// originals
754// P016
755// P010
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400756// P016F
Robert Phillipsbb749902019-06-10 17:20:12 -0400757// Y416
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400758// AYUV
Robert Phillips2dd1b472019-03-21 09:00:20 -0400759// Y410
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400760// NV12
761// NV21
762// I420
763// YV12
764class WackyYUVFormatsGM : public GM {
765public:
Brian Salomon7db71392020-10-16 10:05:21 -0400766 using Type = sk_gpu_test::LazyYUVImage::Type;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400767
Brian Salomon7db71392020-10-16 10:05:21 -0400768 WackyYUVFormatsGM(bool useTargetColorSpace, bool useSubset, Type type)
769 : fUseTargetColorSpace(useTargetColorSpace), fUseSubset(useSubset), fImageType(type) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400770 this->setBGColor(0xFFCCCCCC);
771 }
772
773protected:
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400774 SkString onShortName() override {
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500775 SkString name("wacky_yuv_formats");
776 if (fUseTargetColorSpace) {
777 name += "_cs";
778 }
Brian Salomon6c6678f2020-09-21 10:50:30 -0400779 if (fUseSubset) {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400780 name += "_domain";
781 }
Brian Salomon6c6678f2020-09-21 10:50:30 -0400782 switch (fImageType) {
Brian Salomon7db71392020-10-16 10:05:21 -0400783 case Type::kFromPixmaps:
Brian Salomon6c6678f2020-09-21 10:50:30 -0400784 name += "_frompixmaps";
785 break;
Brian Salomon7db71392020-10-16 10:05:21 -0400786 case Type::kFromTextures:
Brian Salomon6c6678f2020-09-21 10:50:30 -0400787 break;
Brian Salomon7db71392020-10-16 10:05:21 -0400788 case Type::kFromGenerator:
Brian Salomon6c6678f2020-09-21 10:50:30 -0400789 name += "_imggen";
790 break;
Brian Salomon94f65d72020-09-18 14:09:51 -0400791 }
Robert Phillips2dd1b472019-03-21 09:00:20 -0400792
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500793 return name;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400794 }
795
796 SkISize onISize() override {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400797 int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x #-color-spaces
798 int numRows = 1 + (kLast_YUVFormat + 1); // original + #-yuv-formats
Brian Salomon6c6678f2020-09-21 10:50:30 -0400799 int wh = SkScalarCeilToInt(kTileWidthHeight * (fUseSubset ? 1.5f : 1.f));
Michael Ludwiga6a84002019-04-12 15:03:02 -0400800 return SkISize::Make(kLabelWidth + numCols * (wh + kPad),
801 kLabelHeight + numRows * (wh + kPad));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400802 }
803
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400804 void createBitmaps() {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400805 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
806 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
807 float innerRadius = 20.0f;
808
809 {
810 // transparent
811 SkTDArray<SkRect> circles;
812 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400813 fOriginalBMs[0] = make_bitmap(kRGBA_8888_SkColorType, path, circles, false, fUseSubset);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400814 }
815
816 {
817 // opaque
818 SkTDArray<SkRect> circles;
819 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Brian Salomon6c6678f2020-09-21 10:50:30 -0400820 fOriginalBMs[1] = make_bitmap(kRGBA_8888_SkColorType, path, circles, true, fUseSubset);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400821 }
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500822
823 if (fUseTargetColorSpace) {
824 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
825 }
Robert Phillips51c89e42018-10-05 13:30:43 -0400826 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400827
Robert Phillips057c33f2020-07-17 11:59:01 -0400828 bool createImages(GrDirectContext* dContext) {
Brian Salomon96796122021-01-19 12:11:07 -0500829 int origin = 0;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400830 for (bool opaque : { false, true }) {
831 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
832 PlaneData planes;
Brian Salomon96796122021-01-19 12:11:07 -0500833 extract_planes(fOriginalBMs[opaque],
834 static_cast<SkYUVColorSpace>(cs),
835 static_cast<SkEncodedOrigin>(origin + 1), // valid origins are 1...8
836 &planes);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400837
Brian Salomonf7255d72020-04-08 15:57:08 -0400838 for (int f = kP016_YUVFormat; f <= kLast_YUVFormat; ++f) {
839 auto format = static_cast<YUVFormat>(f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400840 SkBitmap resultBMs[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -0400841
Brian Salomon6c6678f2020-09-21 10:50:30 -0400842 int numPlanes = create_YUV(planes, format, resultBMs, opaque);
Brian Salomon96796122021-01-19 12:11:07 -0500843 const YUVAPlanarConfig planarConfig(format,
844 opaque,
845 static_cast<SkEncodedOrigin>(origin + 1));
Brian Salomon7db71392020-10-16 10:05:21 -0400846 SkYUVAPixmaps pixmaps =
847 planarConfig.makeYUVAPixmaps(fOriginalBMs[opaque].dimensions(),
848 static_cast<SkYUVColorSpace>(cs),
849 resultBMs,
850 numPlanes);
851 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(std::move(pixmaps));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400852
Brian Salomon7db71392020-10-16 10:05:21 -0400853 fImages[opaque][cs][format] = lazyYUV->refImage(dContext, fImageType);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400854 }
Brian Salomon96796122021-01-19 12:11:07 -0500855 origin = (origin + 1) % 8;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400856 }
857 }
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400858
Robert Phillips057c33f2020-07-17 11:59:01 -0400859 if (dContext) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400860 // Some backends (e.g., Vulkan) require all work be completed for backend textures
861 // before they are deleted. Since we don't know when we'll next have access to a
862 // direct context, flush all the work now.
Robert Phillips057c33f2020-07-17 11:59:01 -0400863 dContext->flush();
864 dContext->submit(true);
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400865 }
866
867 return true;
868 }
869
Robert Phillips057c33f2020-07-17 11:59:01 -0400870 DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400871 this->createBitmaps();
872
Robert Phillips057c33f2020-07-17 11:59:01 -0400873 if (dContext && dContext->abandoned()) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400874 // This isn't a GpuGM so a null 'context' is okay but an abandoned context
875 // if forbidden.
876 return DrawResult::kSkip;
877 }
878
Brian Salomon6c6678f2020-09-21 10:50:30 -0400879 // Only the generator is expected to work with the CPU backend.
Brian Salomon7db71392020-10-16 10:05:21 -0400880 if (fImageType != Type::kFromGenerator && !dContext) {
Brian Salomon6c6678f2020-09-21 10:50:30 -0400881 return DrawResult::kSkip;
882 }
883
Robert Phillips057c33f2020-07-17 11:59:01 -0400884 if (!this->createImages(dContext)) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -0400885 *errorMsg = "Failed to create YUV images";
886 return DrawResult::kFail;
887 }
888
889 return DrawResult::kOk;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400890 }
891
Robert Phillipsb795bea2020-06-25 12:38:53 -0400892 void onGpuTeardown() override {
893 for (int i = 0; i < 2; ++i) {
894 for (int j = 0; j <= kLastEnum_SkYUVColorSpace; ++j) {
895 for (int k = 0; k <= kLast_YUVFormat; ++k) {
896 fImages[i][j][k] = nullptr;
897 }
898 }
899 }
900 }
901
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400902 void onDraw(SkCanvas* canvas) override {
Brian Salomon6c6678f2020-09-21 10:50:30 -0400903 auto direct = GrAsDirectContext(canvas->recordingContext());
904
Robert Phillips99044e12020-01-29 08:37:01 -0500905 float cellWidth = kTileWidthHeight, cellHeight = kTileWidthHeight;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400906 if (fUseSubset) {
Robert Phillips99044e12020-01-29 08:37:01 -0500907 cellWidth *= 1.5f;
908 cellHeight *= 1.5f;
909 }
910
Brian Salomon6c6678f2020-09-21 10:50:30 -0400911 SkRect srcRect = SkRect::Make(fOriginalBMs[0].dimensions());
Michael Ludwiga6a84002019-04-12 15:03:02 -0400912 SkRect dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, srcRect.width(), srcRect.height());
Brian Salomon6c6678f2020-09-21 10:50:30 -0400913
Michael Ludwiga6a84002019-04-12 15:03:02 -0400914 SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400915 if (fUseSubset) {
916 srcRect.inset(kSubsetPadding, kSubsetPadding);
Michael Ludwiga6a84002019-04-12 15:03:02 -0400917 // Draw a larger rectangle to ensure bilerp filtering would normally read outside the
918 // srcRect and hit the red pixels, if strict constraint weren't used.
919 dstRect.fRight = kLabelWidth + 1.5f * srcRect.width();
920 dstRect.fBottom = 1.5f * srcRect.height();
921 constraint = SkCanvas::kStrict_SrcRectConstraint;
922 }
923
Mike Reedd396cd52021-01-23 21:14:47 -0500924 SkSamplingOptions sampling(SkFilterMode::kLinear);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400925 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500926 SkPaint paint;
927 if (kIdentity_SkYUVColorSpace == cs) {
928 // The identity color space needs post processing to appear correctly
929 paint.setColorFilter(yuv_to_rgb_colorfilter());
930 }
931
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400932 for (int opaque : { 0, 1 }) {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400933 dstRect.offsetTo(dstRect.fLeft, kLabelHeight);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400934
Robert Phillips99044e12020-01-29 08:37:01 -0500935 draw_col_label(canvas, dstRect.fLeft + cellWidth / 2, cs, opaque);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400936
Mike Reed34a0c972021-01-25 17:49:32 -0500937 canvas->drawImageRect(fOriginalBMs[opaque].asImage(), srcRect, dstRect,
938 SkSamplingOptions(), nullptr, constraint);
Robert Phillips99044e12020-01-29 08:37:01 -0500939 dstRect.offset(0.f, cellHeight + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400940
Robert Phillipsbb749902019-06-10 17:20:12 -0400941 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
Michael Ludwiga6a84002019-04-12 15:03:02 -0400942 draw_row_label(canvas, dstRect.fTop, format);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500943 if (fUseTargetColorSpace && fImages[opaque][cs][format]) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500944 // Making a CS-specific version of a kIdentity_SkYUVColorSpace YUV image
945 // doesn't make a whole lot of sense. The colorSpace conversion will
946 // operate on the YUV components rather than the RGB components.
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500947 sk_sp<SkImage> csImage =
Adlai Holler3a220172020-07-15 10:37:50 -0400948 fImages[opaque][cs][format]->makeColorSpace(fTargetColorSpace, direct);
Mike Reedd396cd52021-01-23 21:14:47 -0500949 canvas->drawImageRect(csImage, srcRect, dstRect, sampling,
950 &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500951 } else {
Robert Phillips99044e12020-01-29 08:37:01 -0500952 canvas->drawImageRect(fImages[opaque][cs][format], srcRect, dstRect,
Mike Reedd396cd52021-01-23 21:14:47 -0500953 sampling, &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500954 }
Robert Phillips99044e12020-01-29 08:37:01 -0500955 dstRect.offset(0.f, cellHeight + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400956 }
957
Robert Phillips99044e12020-01-29 08:37:01 -0500958 dstRect.offset(cellWidth + kPad, 0.f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400959 }
960 }
961 }
962
963private:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400964 SkBitmap fOriginalBMs[2];
965 sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace + 1][kLast_YUVFormat + 1];
Robert Phillips2dd1b472019-03-21 09:00:20 -0400966 bool fUseTargetColorSpace;
Brian Salomon6c6678f2020-09-21 10:50:30 -0400967 bool fUseSubset;
Brian Salomon7db71392020-10-16 10:05:21 -0400968 Type fImageType;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400969 sk_sp<SkColorSpace> fTargetColorSpace;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400970
John Stiles7571f9e2020-09-02 22:42:33 -0400971 using INHERITED = GM;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400972};
973
974//////////////////////////////////////////////////////////////////////////////
975
Brian Salomon6c6678f2020-09-21 10:50:30 -0400976DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
977 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400978 WackyYUVFormatsGM::Type::kFromTextures);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400979DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
980 /* subset */ true,
Brian Salomon7db71392020-10-16 10:05:21 -0400981 WackyYUVFormatsGM::Type::kFromTextures);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400982DEF_GM(return new WackyYUVFormatsGM(/* target cs */ true,
983 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400984 WackyYUVFormatsGM::Type::kFromTextures);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400985DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
986 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400987 WackyYUVFormatsGM::Type::kFromGenerator);)
Brian Salomon6c6678f2020-09-21 10:50:30 -0400988DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false,
989 /* subset */ false,
Brian Salomon7db71392020-10-16 10:05:21 -0400990 WackyYUVFormatsGM::Type::kFromPixmaps);)
Brian Osmane9560492019-02-05 17:00:03 -0500991
Chris Dalton3a778372019-02-07 15:23:36 -0700992class YUVMakeColorSpaceGM : public GpuGM {
Brian Osmane9560492019-02-05 17:00:03 -0500993public:
994 YUVMakeColorSpaceGM() {
995 this->setBGColor(0xFFCCCCCC);
996 }
997
998protected:
999 SkString onShortName() override {
1000 return SkString("yuv_make_color_space");
1001 }
1002
1003 SkISize onISize() override {
1004 int numCols = 4; // (transparent, opaque) x (untagged, tagged)
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001005 int numRows = 5; // original, YUV, subset, makeNonTextureImage, readPixels
Brian Osmane9560492019-02-05 17:00:03 -05001006 return SkISize::Make(numCols * (kTileWidthHeight + kPad) + kPad,
1007 numRows * (kTileWidthHeight + kPad) + kPad);
1008 }
1009
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001010 void createBitmaps() {
Brian Osmane9560492019-02-05 17:00:03 -05001011 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
1012 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
1013 float innerRadius = 20.0f;
1014
1015 {
1016 // transparent
1017 SkTDArray<SkRect> circles;
1018 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001019 fOriginalBMs[0] = make_bitmap(kN32_SkColorType, path, circles, false, false);
Brian Osmane9560492019-02-05 17:00:03 -05001020 }
1021
1022 {
1023 // opaque
1024 SkTDArray<SkRect> circles;
1025 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001026 fOriginalBMs[1] = make_bitmap(kN32_SkColorType, path, circles, true, false);
Brian Osmane9560492019-02-05 17:00:03 -05001027 }
1028
1029 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
1030 }
1031
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001032 bool createImages(GrDirectContext* context) {
Brian Osmane9560492019-02-05 17:00:03 -05001033 for (bool opaque : { false, true }) {
1034 PlaneData planes;
Brian Salomon96796122021-01-19 12:11:07 -05001035 extract_planes(fOriginalBMs[opaque],
1036 kJPEG_SkYUVColorSpace,
1037 kTopLeft_SkEncodedOrigin,
1038 &planes);
Brian Osmane9560492019-02-05 17:00:03 -05001039
1040 SkBitmap resultBMs[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001041
Brian Salomonf7255d72020-04-08 15:57:08 -04001042 create_YUV(planes, kAYUV_YUVFormat, resultBMs, opaque);
Robert Phillips2dd1b472019-03-21 09:00:20 -04001043
Brian Salomon96796122021-01-19 12:11:07 -05001044 YUVAPlanarConfig planarConfig(kAYUV_YUVFormat, opaque, kTopLeft_SkEncodedOrigin);
Brian Osmane9560492019-02-05 17:00:03 -05001045
Brian Salomon7db71392020-10-16 10:05:21 -04001046 auto yuvaPixmaps = planarConfig.makeYUVAPixmaps(fOriginalBMs[opaque].dimensions(),
1047 kJPEG_Full_SkYUVColorSpace,
1048 resultBMs,
1049 SK_ARRAY_COUNT(resultBMs));
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001050
Brian Salomon7db71392020-10-16 10:05:21 -04001051 int i = 0;
1052 for (sk_sp<SkColorSpace> cs : {sk_sp<SkColorSpace>(nullptr),
1053 SkColorSpace::MakeSRGB()}) {
1054 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(yuvaPixmaps,
1055 GrMipmapped::kNo,
1056 std::move(cs));
1057 fImages[opaque][i++] =
1058 lazyYUV->refImage(context, sk_gpu_test::LazyYUVImage::Type::kFromTextures);
Brian Osmane9560492019-02-05 17:00:03 -05001059 }
Brian Osmane9560492019-02-05 17:00:03 -05001060 }
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001061
1062 // Some backends (e.g., Vulkan) require all work be completed for backend textures before
1063 // they are deleted. Since we don't know when we'll next have access to a direct context,
1064 // flush all the work now.
Greg Danielce9f0162020-06-30 13:42:46 -04001065 context->flush();
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001066 context->submit(true);
1067
1068 return true;
1069 }
1070
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001071 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001072 if (!context || context->abandoned()) {
1073 return DrawResult::kSkip;
1074 }
1075
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001076 this->createBitmaps();
1077 if (!this->createImages(context)) {
1078 *errorMsg = "Failed to create YUV images";
1079 return DrawResult::kFail;
1080 }
1081
1082 return DrawResult::kOk;
Brian Osmane9560492019-02-05 17:00:03 -05001083 }
1084
Robert Phillipsb795bea2020-06-25 12:38:53 -04001085 void onGpuTeardown() override {
1086 fImages[0][0] = fImages[0][1] = fImages[1][0] = fImages[1][1] = nullptr;
1087 }
1088
Brian Salomoneebe7352020-12-09 16:37:04 -05001089 DrawResult onDraw(GrRecordingContext* rContext, GrSurfaceDrawContext*,
Adlai Holler3a220172020-07-15 10:37:50 -04001090 SkCanvas* canvas, SkString* msg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001091 SkASSERT(fImages[0][0] && fImages[0][1] && fImages[1][0] && fImages[1][1]);
Brian Osmane9560492019-02-05 17:00:03 -05001092
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001093 auto dContext = GrAsDirectContext(rContext);
1094 if (rContext && !dContext) {
Adlai Holler3a220172020-07-15 10:37:50 -04001095 *msg = "YUV ColorSpace image creation requires a direct context.";
1096 return DrawResult::kSkip;
1097 }
1098
Brian Osmane9560492019-02-05 17:00:03 -05001099 int x = kPad;
1100 for (int tagged : { 0, 1 }) {
1101 for (int opaque : { 0, 1 }) {
1102 int y = kPad;
1103
Mike Reedac9f0c92020-12-23 10:11:33 -05001104 auto raster = fOriginalBMs[opaque].asImage()
Adlai Holler3a220172020-07-15 10:37:50 -04001105 ->makeColorSpace(fTargetColorSpace, nullptr);
Brian Osmane9560492019-02-05 17:00:03 -05001106 canvas->drawImage(raster, x, y);
1107 y += kTileWidthHeight + kPad;
1108
Greg Daniel85da3362020-03-09 15:18:35 -04001109 if (fImages[opaque][tagged]) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001110 auto yuv = fImages[opaque][tagged]->makeColorSpace(fTargetColorSpace, dContext);
Adlai Holler3a220172020-07-15 10:37:50 -04001111 SkASSERT(yuv);
Greg Daniel85da3362020-03-09 15:18:35 -04001112 SkASSERT(SkColorSpace::Equals(yuv->colorSpace(), fTargetColorSpace.get()));
1113 canvas->drawImage(yuv, x, y);
1114 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001115
Adlai Holler872a32c2020-07-10 14:33:22 -04001116 SkIRect bounds = SkIRect::MakeWH(kTileWidthHeight / 2, kTileWidthHeight / 2);
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001117 auto subset = yuv->makeSubset(bounds, dContext);
Adlai Holler3a220172020-07-15 10:37:50 -04001118 SkASSERT(subset);
Greg Daniel85da3362020-03-09 15:18:35 -04001119 canvas->drawImage(subset, x, y);
1120 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001121
Greg Daniel85da3362020-03-09 15:18:35 -04001122 auto nonTexture = yuv->makeNonTextureImage();
Adlai Holler3a220172020-07-15 10:37:50 -04001123 SkASSERT(nonTexture);
Greg Daniel85da3362020-03-09 15:18:35 -04001124 canvas->drawImage(nonTexture, x, y);
1125 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001126
Greg Daniel85da3362020-03-09 15:18:35 -04001127 SkBitmap readBack;
1128 readBack.allocPixels(yuv->imageInfo());
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001129 SkAssertResult(yuv->readPixels(dContext, readBack.pixmap(), 0, 0));
Mike Reed34a0c972021-01-25 17:49:32 -05001130 canvas->drawImage(readBack.asImage(), x, y);
Greg Daniel85da3362020-03-09 15:18:35 -04001131 }
Brian Osmane9560492019-02-05 17:00:03 -05001132 x += kTileWidthHeight + kPad;
1133 }
1134 }
Adlai Holler3a220172020-07-15 10:37:50 -04001135 return DrawResult::kOk;
Brian Osmane9560492019-02-05 17:00:03 -05001136 }
1137
1138private:
1139 SkBitmap fOriginalBMs[2];
1140 sk_sp<SkImage> fImages[2][2];
Brian Osmane9560492019-02-05 17:00:03 -05001141 sk_sp<SkColorSpace> fTargetColorSpace;
1142
John Stiles7571f9e2020-09-02 22:42:33 -04001143 using INHERITED = GM;
Brian Osmane9560492019-02-05 17:00:03 -05001144};
1145
1146DEF_GM(return new YUVMakeColorSpaceGM();)
1147
John Stilesa6841be2020-08-06 14:11:56 -04001148} // namespace skiagm
Mike Reed6a5f7e22019-05-23 15:30:07 -04001149
1150///////////////
1151
Mike Reed6a5f7e22019-05-23 15:30:07 -04001152#include "include/effects/SkColorMatrix.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001153#include "src/core/SkAutoPixmapStorage.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001154#include "tools/Resources.h"
Mike Reed6a5f7e22019-05-23 15:30:07 -04001155
1156static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) {
1157 auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
1158 canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height());
1159 SkPaint paint;
Mike Reed6a5f7e22019-05-23 15:30:07 -04001160 paint.setColorFilter(cf);
1161 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reedd396cd52021-01-23 21:14:47 -05001162 canvas->drawImage(img, 0, 0, SkSamplingOptions(SkFilterMode::kLinear), &paint);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001163}
1164
1165static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) {
1166 float m[20];
1167 SkColorMatrix_RGB2YUV(cs, m);
1168
1169 memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A
1170 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]);
1171
1172 memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A
1173 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]);
1174
1175 memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A
1176 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]);
1177}
1178
1179static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y,
1180 const SkImage* a, const SkImage* b) {
Mike Reed41068192020-12-09 21:48:52 -05001181 auto sh = SkShaders::Blend(SkBlendMode::kDifference,
1182 a->makeShader(SkSamplingOptions()),
1183 b->makeShader(SkSamplingOptions()));
Mike Reed6a5f7e22019-05-23 15:30:07 -04001184 SkPaint paint;
1185 paint.setShader(sh);
1186 canvas->save();
1187 canvas->translate(x, y);
1188 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1189
1190 SkColorMatrix cm;
1191 cm.setScale(64, 64, 64);
1192 paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm)));
1193 canvas->translate(0, a->height());
1194 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1195
1196 canvas->restore();
1197}
1198
1199// Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the
1200// resulting (recombined) images (gpu only for now).
1201//
1202class YUVSplitterGM : public skiagm::GM {
1203 sk_sp<SkImage> fOrig;
1204 SkAutoPixmapStorage fStorage[3];
1205 SkPixmap fPM[3];
1206
1207public:
1208 YUVSplitterGM() {}
1209
1210protected:
1211
1212 SkString onShortName() override {
1213 return SkString("yuv_splitter");
1214 }
1215
1216 SkISize onISize() override {
Brian Osman2b73e662019-11-01 10:02:24 -04001217 return SkISize::Make(1280, 768);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001218 }
1219
1220 void onOnceBeforeDraw() override {
1221 fOrig = GetResourceAsImage("images/mandrill_256.png");
1222
Brian Salomonf165f792020-10-05 13:25:05 -04001223 SkImageInfo info = SkImageInfo::MakeA8(fOrig->dimensions());
Mike Reed6a5f7e22019-05-23 15:30:07 -04001224 fStorage[0].alloc(info);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001225 fStorage[1].alloc(info);
1226 fStorage[2].alloc(info);
1227 for (int i = 0; i < 3; ++i) {
1228 fPM[i] = fStorage[i];
1229 }
1230 }
1231
1232 void onDraw(SkCanvas* canvas) override {
Mike Reed6a5f7e22019-05-23 15:30:07 -04001233 canvas->translate(fOrig->width(), 0);
1234 canvas->save();
Brian Osman2b73e662019-11-01 10:02:24 -04001235 for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace,
1236 kBT2020_SkYUVColorSpace}) {
Mike Reed6a5f7e22019-05-23 15:30:07 -04001237 split_into_yuv(fOrig.get(), cs, fPM);
Brian Salomone4387382020-11-11 16:34:19 -05001238 SkYUVAInfo yuvaInfo(fOrig->dimensions(),
1239 SkYUVAInfo::PlaneConfig::kY_U_V,
1240 SkYUVAInfo::Subsampling::k444,
1241 cs);
Brian Salomonf165f792020-10-05 13:25:05 -04001242 auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, fPM);
1243 auto img = SkImage::MakeFromYUVAPixmaps(canvas->recordingContext(),
1244 yuvaPixmaps,
1245 GrMipMapped::kNo,
1246 /* limit to max tex size */ false,
1247 /* color space */ nullptr);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001248 if (img) {
Mike Reedd396cd52021-01-23 21:14:47 -05001249 canvas->drawImage(img, 0, 0);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001250 draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get());
1251 }
1252 canvas->translate(fOrig->width(), 0);
1253 }
1254 canvas->restore();
1255 canvas->translate(-fOrig->width(), 0);
1256
Mike Reedd396cd52021-01-23 21:14:47 -05001257 canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0);
1258 canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height());
Mike Reed6a5f7e22019-05-23 15:30:07 -04001259 canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]),
Mike Reedd396cd52021-01-23 21:14:47 -05001260 0, fPM[0].height() + fPM[1].height());
Mike Reed6a5f7e22019-05-23 15:30:07 -04001261 }
1262
1263private:
John Stiles7571f9e2020-09-02 22:42:33 -04001264 using INHERITED = GM;
Mike Reed6a5f7e22019-05-23 15:30:07 -04001265};
1266DEF_GM( return new YUVSplitterGM; )