blob: 4e4224605b6d507a80565bf076812e8a1df9e9d4 [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"
39#include "include/gpu/GrContext.h"
40#include "include/gpu/GrTypes.h"
41#include "include/private/GrTypesPriv.h"
42#include "include/private/SkTArray.h"
43#include "include/private/SkTDArray.h"
44#include "include/private/SkTemplates.h"
45#include "include/utils/SkTextUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/gpu/GrContextPriv.h"
47#include "src/gpu/GrGpu.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040048#include "tools/ToolUtils.h"
49
50#include <math.h>
51#include <string.h>
52#include <initializer_list>
53#include <memory>
54#include <utility>
55
56class GrRenderTargetContext;
Robert Phillips51c89e42018-10-05 13:30:43 -040057
Robert Phillipsbfa76f22018-10-03 12:12:26 -040058static const int kTileWidthHeight = 128;
59static const int kLabelWidth = 64;
60static const int kLabelHeight = 32;
Michael Ludwiga6a84002019-04-12 15:03:02 -040061static const int kDomainPadding = 8;
Robert Phillipsbfa76f22018-10-03 12:12:26 -040062static const int kPad = 1;
Robert Phillipsbfa76f22018-10-03 12:12:26 -040063
64enum YUVFormat {
Robert Phillipsbb749902019-06-10 17:20:12 -040065 // 4:2:0 formats, 24 bpp
66 kP016_YUVFormat, // 16-bit Y plane + 2x2 down sampled interleaved U/V plane (2 textures)
67 // 4:2:0 formats, "15 bpp" (but really 24 bpp)
68 kP010_YUVFormat, // same as kP016 except "10 bpp". Note that it is the same memory layout
69 // except that the bottom 6 bits are zeroed out (2 textures)
70 // TODO: we're cheating a bit w/ P010 and just treating it as unorm 16. This means its
71 // fully saturated values are 65504 rather than 65535 (that is just .9995 out of 1.0 though).
72
73 // 4:4:4 formats, 64 bpp
74 kY416_YUVFormat, // 16-bit AVYU values all interleaved (1 texture)
75
Robert Phillipsbfa76f22018-10-03 12:12:26 -040076 // 4:4:4 formats, 32 bpp
Robert Phillipsbb749902019-06-10 17:20:12 -040077 kAYUV_YUVFormat, // 8-bit YUVA values all interleaved (1 texture)
78 kY410_YUVFormat, // AVYU w/ 10bpp for YUV and 2 for A all interleaved (1 texture)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040079
80 // 4:2:0 formats, 12 bpp
Robert Phillipsbb749902019-06-10 17:20:12 -040081 kNV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled interleaved U/V planes (2 textures)
82 kNV21_YUVFormat, // same as kNV12 but w/ U/V reversed in the interleaved texture (2 textures)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040083
Robert Phillipsbb749902019-06-10 17:20:12 -040084 kI420_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled U and V planes (3 textures)
85 kYV12_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled V and U planes (3 textures)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040086
87 kLast_YUVFormat = kYV12_YUVFormat
88};
89
Robert Phillipsbb749902019-06-10 17:20:12 -040090static bool format_uses_16_bpp(YUVFormat yuvFormat) {
91 return kP016_YUVFormat == yuvFormat ||
92 kP010_YUVFormat == yuvFormat ||
93 kY416_YUVFormat == yuvFormat;
94}
95
96static bool format_has_builtin_alpha(YUVFormat yuvFormat) {
97 return kY416_YUVFormat == yuvFormat ||
98 kAYUV_YUVFormat == yuvFormat ||
99 kY410_YUVFormat == yuvFormat;
100}
101
102static bool format_cant_be_represented_with_pixmaps(YUVFormat yuvFormat) {
103 return kP016_YUVFormat == yuvFormat ||
104 kP010_YUVFormat == yuvFormat ||
105 kY416_YUVFormat == yuvFormat ||
106 kY410_YUVFormat == yuvFormat;
107}
108
Robert Phillipsdc62b642019-05-16 10:10:45 -0400109// Helper to setup the SkYUVAIndex array correctly
110// Skia allows the client to tack an additional alpha plane onto any of the standard opaque
111// formats (via the addExtraAlpha) flag. In this case it is assumed to be a stand-alone single-
112// channel plane.
113static void setup_yuv_indices(YUVFormat yuvFormat, bool addExtraAlpha, SkYUVAIndex yuvaIndices[4]) {
114 switch (yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400115 case kP016_YUVFormat: // fall through
116 case kP010_YUVFormat:
117 yuvaIndices[0].fIndex = 0;
118 yuvaIndices[0].fChannel = SkColorChannel::kR; // bc 16bit is stored in R16 format
119 yuvaIndices[1].fIndex = 1;
120 yuvaIndices[1].fChannel = SkColorChannel::kR;
121 yuvaIndices[2].fIndex = 1;
122 yuvaIndices[2].fChannel = SkColorChannel::kG;
123 if (addExtraAlpha) {
124 yuvaIndices[3].fIndex = 2;
125 yuvaIndices[3].fChannel = SkColorChannel::kR; // bc 16bit is stored in R16 format
126 } else {
127 yuvaIndices[3].fIndex = -1; // No alpha channel
128 }
129 break;
130 case kY416_YUVFormat:
131 SkASSERT(!addExtraAlpha); // this format already has an alpha channel
132 yuvaIndices[0].fIndex = 0;
133 yuvaIndices[0].fChannel = SkColorChannel::kG;
134 yuvaIndices[1].fIndex = 0;
135 yuvaIndices[1].fChannel = SkColorChannel::kB;
136 yuvaIndices[2].fIndex = 0;
137 yuvaIndices[2].fChannel = SkColorChannel::kR;
138 yuvaIndices[3].fIndex = 0;
139 yuvaIndices[3].fChannel = SkColorChannel::kA;
140 break;
Robert Phillipsdc62b642019-05-16 10:10:45 -0400141 case kAYUV_YUVFormat:
142 SkASSERT(!addExtraAlpha); // this format already has an alpha channel
143 yuvaIndices[0].fIndex = 0;
144 yuvaIndices[0].fChannel = SkColorChannel::kR;
145 yuvaIndices[1].fIndex = 0;
146 yuvaIndices[1].fChannel = SkColorChannel::kG;
147 yuvaIndices[2].fIndex = 0;
148 yuvaIndices[2].fChannel = SkColorChannel::kB;
149 yuvaIndices[3].fIndex = 0;
150 yuvaIndices[3].fChannel = SkColorChannel::kA;
151 break;
152 case kY410_YUVFormat:
153 SkASSERT(!addExtraAlpha); // this format already has an alpha channel
154 yuvaIndices[0].fIndex = 0;
155 yuvaIndices[0].fChannel = SkColorChannel::kG;
156 yuvaIndices[1].fIndex = 0;
157 yuvaIndices[1].fChannel = SkColorChannel::kB;
158 yuvaIndices[2].fIndex = 0;
159 yuvaIndices[2].fChannel = SkColorChannel::kR;
160 yuvaIndices[3].fIndex = 0;
161 yuvaIndices[3].fChannel = SkColorChannel::kA;
162 break;
163 case kNV12_YUVFormat:
164 yuvaIndices[0].fIndex = 0;
165 yuvaIndices[0].fChannel = SkColorChannel::kA;
166 yuvaIndices[1].fIndex = 1;
167 yuvaIndices[1].fChannel = SkColorChannel::kR;
168 yuvaIndices[2].fIndex = 1;
169 yuvaIndices[2].fChannel = SkColorChannel::kG;
170 if (addExtraAlpha) {
171 yuvaIndices[3].fIndex = 2;
172 yuvaIndices[3].fChannel = SkColorChannel::kA;
173 } else {
174 yuvaIndices[3].fIndex = -1; // No alpha channel
175 }
176 break;
177 case kNV21_YUVFormat:
178 yuvaIndices[0].fIndex = 0;
179 yuvaIndices[0].fChannel = SkColorChannel::kA;
180 yuvaIndices[1].fIndex = 1;
181 yuvaIndices[1].fChannel = SkColorChannel::kG;
182 yuvaIndices[2].fIndex = 1;
183 yuvaIndices[2].fChannel = SkColorChannel::kR;
184 if (addExtraAlpha) {
185 yuvaIndices[3].fIndex = 2;
186 yuvaIndices[3].fChannel = SkColorChannel::kA;
187 } else {
188 yuvaIndices[3].fIndex = -1; // No alpha channel
189 }
190 break;
191 case kI420_YUVFormat:
192 yuvaIndices[0].fIndex = 0;
193 yuvaIndices[0].fChannel = SkColorChannel::kA;
194 yuvaIndices[1].fIndex = 1;
195 yuvaIndices[1].fChannel = SkColorChannel::kA;
196 yuvaIndices[2].fIndex = 2;
197 yuvaIndices[2].fChannel = SkColorChannel::kA;
198 if (addExtraAlpha) {
199 yuvaIndices[3].fIndex = 3;
200 yuvaIndices[3].fChannel = SkColorChannel::kA;
201 } else {
202 yuvaIndices[3].fIndex = -1; // No alpha channel
203 }
204 break;
205 case kYV12_YUVFormat:
206 yuvaIndices[0].fIndex = 0;
207 yuvaIndices[0].fChannel = SkColorChannel::kA;
208 yuvaIndices[1].fIndex = 2;
209 yuvaIndices[1].fChannel = SkColorChannel::kA;
210 yuvaIndices[2].fIndex = 1;
211 yuvaIndices[2].fChannel = SkColorChannel::kA;
212 if (addExtraAlpha) {
213 yuvaIndices[3].fIndex = 3;
214 yuvaIndices[3].fChannel = SkColorChannel::kA;
215 } else {
216 yuvaIndices[3].fIndex = -1; // No alpha channel
217 }
218 break;
219 }
220}
221
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400222// All the planes we need to construct the various YUV formats
223struct PlaneData {
224 SkBitmap fYFull;
225 SkBitmap fUFull;
226 SkBitmap fVFull;
227 SkBitmap fAFull;
228 SkBitmap fUQuarter; // 2x2 downsampled U channel
229 SkBitmap fVQuarter; // 2x2 downsampled V channel
230};
231
232// Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle
233// and have tangents 'v1' and 'v2'.
234static void add_arc(SkPath* path,
235 const SkPoint& o1, const SkVector& v1,
236 const SkPoint& o2, const SkVector& v2,
237 SkTDArray<SkRect>* circles, bool takeLongWayRound) {
238
239 SkVector v3 = { -v1.fY, v1.fX };
240 SkVector v4 = { v2.fY, -v2.fX };
241
242 SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4);
243 SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY };
244
245 SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t };
246
247 if (circles) {
248 circles->push_back(r);
249 }
250
251 SkVector startV = o1 - center, endV = o2 - center;
252 startV.normalize();
253 endV.normalize();
254
255 SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX));
256 SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX));
257
258 startDeg += 360.0f;
259 startDeg = fmodf(startDeg, 360.0f);
260
261 endDeg += 360.0f;
262 endDeg = fmodf(endDeg, 360.0f);
263
264 if (endDeg < startDeg) {
265 endDeg += 360.0f;
266 }
267
268 SkScalar sweepDeg = SkTAbs(endDeg - startDeg);
269 if (!takeLongWayRound) {
270 sweepDeg = sweepDeg - 360;
271 }
272
273 path->arcTo(r, startDeg, sweepDeg, false);
274}
275
276static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius,
277 SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) {
278 if (numLobes <= 1) {
279 return SkPath();
280 }
281
282 SkPath p;
283
284 int numDivisions = 2 * numLobes;
285 SkScalar fullLobeDegrees = 360.0f / numLobes;
286 SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f);
287 SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f);
288 SkMatrix outerStep, innerStep;
289 outerStep.setRotate(outDegrees);
290 innerStep.setRotate(innerDegrees);
291 SkVector curV = SkVector::Make(0.0f, 1.0f);
292
293 if (circles) {
294 circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius,
295 o.fX + innerRadius, o.fY + innerRadius));
296 }
297
298 p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY);
299
300 for (int i = 0; i < numDivisions; ++i) {
301
302 SkVector nextV;
303 if (0 == (i % 2)) {
304 nextV = outerStep.mapVector(curV.fX, curV.fY);
305
306 SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX,
307 o.fY + outerRadius * curV.fY);
308 SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX,
309 o.fY + outerRadius * nextV.fY);
310
311 p.lineTo(top);
312 add_arc(&p, top, curV, nextTop, nextV, circles, true);
313 } else {
314 nextV = innerStep.mapVector(curV.fX, curV.fY);
315
316 SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX,
317 o.fY + innerRadius * curV.fY);
318 SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX,
319 o.fY + innerRadius * nextV.fY);
320
321 p.lineTo(bot);
322 add_arc(&p, bot, curV, nextBot, nextV, nullptr, false);
323 }
324
325 curV = nextV;
326 }
327
328 p.close();
329
330 return p;
331}
332
Robert Phillips2dd1b472019-03-21 09:00:20 -0400333static SkBitmap make_bitmap(SkColorType colorType, const SkPath& path,
Michael Ludwiga6a84002019-04-12 15:03:02 -0400334 const SkTDArray<SkRect>& circles, bool opaque, bool padWithRed) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500335 const SkColor kGreen = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104));
336 const SkColor kBlue = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252));
337 const SkColor kYellow = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400338
Michael Ludwiga6a84002019-04-12 15:03:02 -0400339 int widthHeight = kTileWidthHeight + (padWithRed ? 2 * kDomainPadding : 0);
340
341 SkImageInfo ii = SkImageInfo::Make(widthHeight, widthHeight,
Robert Phillips2dd1b472019-03-21 09:00:20 -0400342 colorType, kPremul_SkAlphaType);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400343
344 SkBitmap bm;
345 bm.allocPixels(ii);
346
Robert Phillips2dd1b472019-03-21 09:00:20 -0400347 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(ii,
348 bm.getPixels(),
349 bm.rowBytes());
Michael Ludwiga6a84002019-04-12 15:03:02 -0400350 if (padWithRed) {
351 canvas->clear(SK_ColorRED);
352 canvas->translate(kDomainPadding, kDomainPadding);
353 canvas->clipRect(SkRect::MakeWH(kTileWidthHeight, kTileWidthHeight));
354 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400355 canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT);
356
357 SkPaint paint;
358 paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency
359 paint.setColor(kBlue);
360
361 canvas->drawPath(path, paint);
362
363 paint.setColor(opaque ? kYellow : SK_ColorTRANSPARENT);
364 paint.setBlendMode(SkBlendMode::kSrc);
365 for (int i = 0; i < circles.count(); ++i) {
366 SkRect r = circles[i];
367 r.inset(r.width()/4, r.height()/4);
368 canvas->drawOval(r, paint);
369 }
370
371 return bm;
372}
373
Robert Phillips1c7062d2018-10-04 10:44:53 -0400374static void convert_rgba_to_yuva_601_shared(SkColor col, uint8_t yuv[4],
375 uint8_t off, uint8_t range) {
376 static const float Kr = 0.299f;
377 static const float Kb = 0.114f;
378 static const float Kg = 1.0f - Kr - Kb;
379
380 float r = SkColorGetR(col) / 255.0f;
381 float g = SkColorGetG(col) / 255.0f;
382 float b = SkColorGetB(col) / 255.0f;
383
384 float Ey = Kr * r + Kg * g + Kb * b;
385 float Ecb = (b - Ey) / 1.402f;
386 float Ecr = (r - Ey) / 1.772;
Robert Phillipsbb749902019-06-10 17:20:12 -0400387 SkASSERT(Ey >= 0.0f && Ey <= 1.0f);
388 SkASSERT(Ecb >= -0.5f && Ecb <= 0.5f);
389 SkASSERT(Ecr >= -0.5f && Ecr <= 0.5f);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400390
391 yuv[0] = SkScalarRoundToInt( range * Ey + off );
392 yuv[1] = SkScalarRoundToInt( 224 * Ecb + 128 );
393 yuv[2] = SkScalarRoundToInt( 224 * Ecr + 128 );
394 yuv[3] = SkColorGetA(col);
395}
396
397static void convert_rgba_to_yuva_jpeg(SkColor col, uint8_t yuv[4]) {
398 // full swing from 0..255
399 convert_rgba_to_yuva_601_shared(col, yuv, 0, 255);
400}
401
402static void convert_rgba_to_yuva_601(SkColor col, uint8_t yuv[4]) {
403 // partial swing from 16..235
404 convert_rgba_to_yuva_601_shared(col, yuv, 16, 219);
405
406}
407
408static void convert_rgba_to_yuva_709(SkColor col, uint8_t yuv[4]) {
409 static const float Kr = 0.2126f;
410 static const float Kb = 0.0722f;
411 static const float Kg = 1.0f - Kr - Kb;
412
413 float r = SkColorGetR(col) / 255.0f;
414 float g = SkColorGetG(col) / 255.0f;
415 float b = SkColorGetB(col) / 255.0f;
416
417 float Ey = Kr * r + Kg * g + Kb * b;
418 float Ecb = (b - Ey) / 1.8556f;
419 float Ecr = (r - Ey) / 1.5748;
Robert Phillipsbb749902019-06-10 17:20:12 -0400420 SkASSERT(Ey >= 0.0f && Ey <= 1.0f);
421 SkASSERT(Ecb >= -0.5f && Ecb <= 0.5f);
422 SkASSERT(Ecr >= -0.5f && Ecr <= 0.5f);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400423
424 yuv[0] = SkScalarRoundToInt( 219 * Ey + 16 );
425 yuv[1] = SkScalarRoundToInt( 224 * Ecb + 128 );
426 yuv[2] = SkScalarRoundToInt( 224 * Ecr + 128 );
427
428 yuv[3] = SkColorGetA(col);
429}
430
431
432static SkPMColor convert_yuva_to_rgba_jpeg(uint8_t y, uint8_t u, uint8_t v, uint8_t a) {
Robert Phillips2dd1b472019-03-21 09:00:20 -0400433 uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.0f * y + 1.402f * v - 0.703749f * 255),
Robert Phillips1c7062d2018-10-04 10:44:53 -0400434 0, 255);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400435 uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.0f * y - (0.344136f * u) - (0.714136f * v) + 0.531211f * 255),
Robert Phillips1c7062d2018-10-04 10:44:53 -0400436 0, 255);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400437 uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.0f * y + 1.772f * u - 0.889475f * 255),
Robert Phillips1c7062d2018-10-04 10:44:53 -0400438 0, 255);
439
Robert Phillips2dd1b472019-03-21 09:00:20 -0400440 SkPMColor c = SkPremultiplyARGBInline(a, b, g, r);
441 return c;
Robert Phillips1c7062d2018-10-04 10:44:53 -0400442}
443
444static SkPMColor convert_yuva_to_rgba_601(uint8_t y, uint8_t u, uint8_t v, uint8_t a) {
Robert Phillips2dd1b472019-03-21 09:00:20 -0400445 uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.164f * y + 1.596f * v - 0.87075f * 255), 0, 255);
446 uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.164f * y - (0.391f * u) - (0.813f * v) + 0.52925f * 255), 0, 255);
447 uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.164f * y + 2.018f * u - 1.08175f * 255), 0, 255);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400448
Robert Phillips2dd1b472019-03-21 09:00:20 -0400449 SkPMColor c = SkPremultiplyARGBInline(a, b, g, r);
450 return c;
Robert Phillips1c7062d2018-10-04 10:44:53 -0400451}
452
453static SkPMColor convert_yuva_to_rgba_709(uint8_t y, uint8_t u, uint8_t v, uint8_t a) {
Robert Phillips2dd1b472019-03-21 09:00:20 -0400454 uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.164f * y + (1.793f * v) - 0.96925f * 255), 0, 255);
455 uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.164f * y - (0.213f * u) - (0.533f * v) + 0.30025f * 255), 0, 255);
456 uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.164f * y + (2.112f * u) - 1.12875f * 255), 0, 255);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400457
Robert Phillips2dd1b472019-03-21 09:00:20 -0400458 SkPMColor c = SkPremultiplyARGBInline(a, b, g, r);
459 return c;
Robert Phillips1c7062d2018-10-04 10:44:53 -0400460}
461
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400462static void extract_planes(const SkBitmap& bm, SkYUVColorSpace yuvColorSpace, PlaneData* planes) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500463 if (kIdentity_SkYUVColorSpace == yuvColorSpace) {
464 // To test the identity color space we use JPEG YUV planes
465 yuvColorSpace = kJPEG_SkYUVColorSpace;
466 }
467
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400468 SkASSERT(!(bm.width() % 2));
469 SkASSERT(!(bm.height() % 2));
470
471 planes->fYFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
472 planes->fUFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
473 planes->fVFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
474 planes->fAFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
475 planes->fUQuarter.allocPixels(SkImageInfo::MakeA8(bm.width()/2, bm.height()/2));
476 planes->fVQuarter.allocPixels(SkImageInfo::MakeA8(bm.width()/2, bm.height()/2));
477
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400478 for (int y = 0; y < bm.height(); ++y) {
479 for (int x = 0; x < bm.width(); ++x) {
480 SkColor col = bm.getColor(x, y);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400481
Robert Phillips1c7062d2018-10-04 10:44:53 -0400482 uint8_t yuva[4];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400483
Robert Phillips1c7062d2018-10-04 10:44:53 -0400484 if (kJPEG_SkYUVColorSpace == yuvColorSpace) {
485 convert_rgba_to_yuva_jpeg(col, yuva);
486 } else if (kRec601_SkYUVColorSpace == yuvColorSpace) {
487 convert_rgba_to_yuva_601(col, yuva);
488 } else {
489 SkASSERT(kRec709_SkYUVColorSpace == yuvColorSpace);
490 convert_rgba_to_yuva_709(col, yuva);
491 }
492
493 *planes->fYFull.getAddr8(x, y) = yuva[0];
494 *planes->fUFull.getAddr8(x, y) = yuva[1];
495 *planes->fVFull.getAddr8(x, y) = yuva[2];
496 *planes->fAFull.getAddr8(x, y) = yuva[3];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400497 }
498 }
499
500 for (int y = 0; y < bm.height()/2; ++y) {
501 for (int x = 0; x < bm.width()/2; ++x) {
502 uint32_t uAccum = 0, vAccum = 0;
503
504 uAccum += *planes->fUFull.getAddr8(2*x, 2*y);
505 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y);
506 uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1);
507 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1);
508
509 *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f;
510
511 vAccum += *planes->fVFull.getAddr8(2*x, 2*y);
512 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y);
513 vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1);
514 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1);
515
Robert Phillips1c7062d2018-10-04 10:44:53 -0400516 *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400517 }
518 }
519}
520
Robert Phillipsbb749902019-06-10 17:20:12 -0400521// Create a 2x2 downsampled SkBitmap. It is stored in an RGBA texture. It can optionally be
522// uv (i.e., for P016, P010 and NV12) or vu (i.e., NV21).
523static SkBitmap make_quarter_2_channel(const SkBitmap& fullY,
524 const SkBitmap& quarterU,
525 const SkBitmap& quarterV,
526 bool uv) {
527 SkBitmap result;
528
529 // There isn't a RG color type. Approx w/ RGBA.
530 result.allocPixels(SkImageInfo::Make(fullY.width()/2,
531 fullY.height()/2,
532 kRGBA_8888_SkColorType,
533 kUnpremul_SkAlphaType));
534
535 for (int y = 0; y < fullY.height()/2; ++y) {
536 for (int x = 0; x < fullY.width()/2; ++x) {
537 uint8_t u8 = *quarterU.getAddr8(x, y);
538 uint8_t v8 = *quarterV.getAddr8(x, y);
539
540 if (uv) {
541 // NOT premul!
542 // U and 0 swapped to match RGBA layout
543 *result.getAddr32(x, y) = SkColorSetARGB(0xFF, 0, v8, u8);
544 } else {
545 // NOT premul!
546 // V and 0 swapped to match RGBA layout
547 *result.getAddr32(x, y) = SkColorSetARGB(0xFF, 0, u8, v8);
548 }
549 }
550 }
551
552 return result;
553}
554
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400555// Recombine the separate planes into some YUV format
556static void create_YUV(const PlaneData& planes, YUVFormat yuvFormat,
557 SkBitmap resultBMs[], SkYUVAIndex yuvaIndices[4], bool opaque) {
558 int nextLayer = 0;
559
560 switch (yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400561 case kY416_YUVFormat: {
562 // Although this is 16 bpp, store the data in an 8 bpp SkBitmap
563 SkBitmap yuvaFull;
564
565 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
566 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
567
568 for (int y = 0; y < planes.fYFull.height(); ++y) {
569 for (int x = 0; x < planes.fYFull.width(); ++x) {
570
571 uint8_t Y = *planes.fYFull.getAddr8(x, y);
572 uint8_t U = *planes.fUFull.getAddr8(x, y);
573 uint8_t V = *planes.fVFull.getAddr8(x, y);
574 uint8_t A = *planes.fAFull.getAddr8(x, y);
575
576 // NOT premul!
577 // U and V swapped to match RGBA layout
578 SkColor c = SkColorSetARGB(A, U, Y, V);
579 *yuvaFull.getAddr32(x, y) = c;
580 }
581 }
582
583 resultBMs[nextLayer++] = yuvaFull;
584
585 setup_yuv_indices(yuvFormat, false, yuvaIndices);
586 break;
587 }
Jim Van Verth976a6b02018-10-17 15:27:19 -0400588 case kAYUV_YUVFormat: {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400589 SkBitmap yuvaFull;
590
Jim Van Verth47133fd2018-10-19 22:09:28 -0400591 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
592 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400593
594 for (int y = 0; y < planes.fYFull.height(); ++y) {
595 for (int x = 0; x < planes.fYFull.width(); ++x) {
596
597 uint8_t Y = *planes.fYFull.getAddr8(x, y);
598 uint8_t U = *planes.fUFull.getAddr8(x, y);
599 uint8_t V = *planes.fVFull.getAddr8(x, y);
600 uint8_t A = *planes.fAFull.getAddr8(x, y);
601
602 // NOT premul!
Jim Van Verth47133fd2018-10-19 22:09:28 -0400603 // V and Y swapped to match RGBA layout
Robert Phillips2dd1b472019-03-21 09:00:20 -0400604 SkColor c = SkColorSetARGB(A, V, U, Y);
605 *yuvaFull.getAddr32(x, y) = c;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400606 }
607 }
608
609 resultBMs[nextLayer++] = yuvaFull;
610
Robert Phillipsdc62b642019-05-16 10:10:45 -0400611 setup_yuv_indices(yuvFormat, false, yuvaIndices);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400612 break;
613 }
Robert Phillips2dd1b472019-03-21 09:00:20 -0400614 case kY410_YUVFormat: {
615 SkBitmap yuvaFull;
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400616 uint32_t Y, U, V;
617 uint8_t A;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400618
619 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
620 kRGBA_1010102_SkColorType,
621 kUnpremul_SkAlphaType));
622
623 for (int y = 0; y < planes.fYFull.height(); ++y) {
624 for (int x = 0; x < planes.fYFull.width(); ++x) {
625
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400626 Y = SkScalarRoundToInt((*planes.fYFull.getAddr8(x, y) / 255.0f) * 1023.0f);
627 U = SkScalarRoundToInt((*planes.fUFull.getAddr8(x, y) / 255.0f) * 1023.0f);
628 V = SkScalarRoundToInt((*planes.fVFull.getAddr8(x, y) / 255.0f) * 1023.0f);
629 A = SkScalarRoundToInt((*planes.fAFull.getAddr8(x, y) / 255.0f) * 3.0f);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400630
631 // NOT premul!
632 // AVYU but w/ V and U swapped to match RGBA layout
633 *yuvaFull.getAddr32(x, y) = (A << 30) | (U << 20) | (Y << 10) | (V << 0);
634 }
635 }
636
637 resultBMs[nextLayer++] = yuvaFull;
638
Robert Phillipsdc62b642019-05-16 10:10:45 -0400639 setup_yuv_indices(yuvFormat, false, yuvaIndices);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400640 break;
641 }
Robert Phillipsbb749902019-06-10 17:20:12 -0400642 case kP016_YUVFormat: // fall through
643 case kP010_YUVFormat: // fall through
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400644 case kNV12_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400645 SkBitmap uvQuarter = make_quarter_2_channel(planes.fYFull,
646 planes.fUQuarter,
647 planes.fVQuarter, true);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400648 resultBMs[nextLayer++] = planes.fYFull;
649 resultBMs[nextLayer++] = uvQuarter;
650
Robert Phillipsdc62b642019-05-16 10:10:45 -0400651 setup_yuv_indices(yuvFormat, !opaque, yuvaIndices);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400652 break;
653 }
654 case kNV21_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400655 SkBitmap vuQuarter = make_quarter_2_channel(planes.fYFull,
656 planes.fUQuarter,
657 planes.fVQuarter, false);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400658 resultBMs[nextLayer++] = planes.fYFull;
659 resultBMs[nextLayer++] = vuQuarter;
660
Robert Phillipsdc62b642019-05-16 10:10:45 -0400661 setup_yuv_indices(yuvFormat, !opaque, yuvaIndices);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400662 break;
663 }
664 case kI420_YUVFormat:
665 resultBMs[nextLayer++] = planes.fYFull;
666 resultBMs[nextLayer++] = planes.fUQuarter;
667 resultBMs[nextLayer++] = planes.fVQuarter;
668
Robert Phillipsdc62b642019-05-16 10:10:45 -0400669 setup_yuv_indices(yuvFormat, !opaque, yuvaIndices);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400670 break;
671 case kYV12_YUVFormat:
672 resultBMs[nextLayer++] = planes.fYFull;
673 resultBMs[nextLayer++] = planes.fVQuarter;
674 resultBMs[nextLayer++] = planes.fUQuarter;
675
Robert Phillipsdc62b642019-05-16 10:10:45 -0400676 setup_yuv_indices(yuvFormat, !opaque, yuvaIndices);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400677 break;
678 }
679
Robert Phillipsbb749902019-06-10 17:20:12 -0400680 if (!format_has_builtin_alpha(yuvFormat) && !opaque) {
Robert Phillipsdc62b642019-05-16 10:10:45 -0400681 resultBMs[nextLayer] = planes.fAFull;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400682 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400683}
684
Robert Phillips2dd1b472019-03-21 09:00:20 -0400685static uint8_t look_up(float x1, float y1, const SkBitmap& bm, SkColorChannel channel) {
Robert Phillips1c7062d2018-10-04 10:44:53 -0400686 uint8_t result;
687
Robert Phillips2dd1b472019-03-21 09:00:20 -0400688 SkASSERT(x1 > 0 && x1 < 1.0f);
689 SkASSERT(y1 > 0 && y1 < 1.0f);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400690 int x = SkScalarFloorToInt(x1 * bm.width());
691 int y = SkScalarFloorToInt(y1 * bm.height());
692
693 if (kAlpha_8_SkColorType == bm.colorType()) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400694 SkASSERT(SkColorChannel::kA == channel || SkColorChannel::kR == channel);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400695 result = *bm.getAddr8(x, y);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400696 } else if (kRGBA_8888_SkColorType == bm.colorType()) {
697 SkColor c = *bm.getAddr32(x, y);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400698
699 switch (channel) {
700 case SkColorChannel::kR:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400701 result = SkColorGetB(c);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400702 break;
703 case SkColorChannel::kG:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400704 result = SkColorGetG(c);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400705 break;
706 case SkColorChannel::kB:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400707 result = SkColorGetR(c);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400708 break;
709 case SkColorChannel::kA:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400710 result = SkColorGetA(c);
711 break;
712 }
713 } else {
714 SkASSERT(kRGBA_1010102_SkColorType == bm.colorType());
715
716 SkColor c = *bm.getAddr32(x, y);
717
718 switch (channel) {
719 case SkColorChannel::kR:
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400720 result = SkScalarRoundToInt(((c >> 0) & 0x3ff) * (255.0f/1023.0f));
Robert Phillips2dd1b472019-03-21 09:00:20 -0400721 break;
722 case SkColorChannel::kG:
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400723 result = SkScalarRoundToInt(((c >> 10) & 0x3ff) * (255.0f/1023.0f));
Robert Phillips2dd1b472019-03-21 09:00:20 -0400724 break;
725 case SkColorChannel::kB:
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400726 result = SkScalarRoundToInt(((c >> 20) & 0x3ff) * (255.0f/1023.0f));
Robert Phillips2dd1b472019-03-21 09:00:20 -0400727 break;
728 case SkColorChannel::kA:
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400729 result = SkScalarRoundToInt(((c >> 30) & 0x3) * (255.0f/3.0f));
Robert Phillips1c7062d2018-10-04 10:44:53 -0400730 break;
731 }
732 }
733
734 return result;
735}
736
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400737class YUVGenerator : public SkImageGenerator {
738public:
739 YUVGenerator(const SkImageInfo& ii,
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400740 SkYUVColorSpace yuvColorSpace,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400741 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
Jim Van Verthe24b5872018-10-29 16:26:02 -0400742 SkBitmap bitmaps[SkYUVASizeInfo::kMaxCount])
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400743 : SkImageGenerator(ii)
Robert Phillips2dd1b472019-03-21 09:00:20 -0400744 , fYUVColorSpace(yuvColorSpace)
745 , fAllA8(true) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400746 memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices));
747
Jim Van Verth8f11e432018-10-18 14:36:59 -0400748 SkAssertResult(SkYUVAIndex::AreValidIndices(fYUVAIndices, &fNumBitmaps));
Jim Van Verthe24b5872018-10-29 16:26:02 -0400749 SkASSERT(fNumBitmaps > 0 && fNumBitmaps <= SkYUVASizeInfo::kMaxCount);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400750
Jim Van Verth8f11e432018-10-18 14:36:59 -0400751 for (int i = 0; i < fNumBitmaps; ++i) {
752 fYUVBitmaps[i] = bitmaps[i];
Robert Phillips2dd1b472019-03-21 09:00:20 -0400753 if (kAlpha_8_SkColorType != fYUVBitmaps[i].colorType()) {
754 fAllA8 = false;
755 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400756 }
757 }
758
759protected:
760 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
761 const Options&) override {
762
763 if (kUnknown_SkColorType == fFlattened.colorType()) {
Robert Phillips2dd1b472019-03-21 09:00:20 -0400764 fFlattened.allocPixels(info);
765 SkASSERT(kPremul_SkAlphaType == info.alphaType());
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400766
767 for (int y = 0; y < info.height(); ++y) {
768 for (int x = 0; x < info.width(); ++x) {
769
Robert Phillips1c7062d2018-10-04 10:44:53 -0400770 float x1 = (x + 0.5f) / info.width();
771 float y1 = (y + 0.5f) / info.height();
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400772
Robert Phillips1c7062d2018-10-04 10:44:53 -0400773 uint8_t Y = look_up(x1, y1,
774 fYUVBitmaps[fYUVAIndices[0].fIndex],
775 fYUVAIndices[0].fChannel);
776
777 uint8_t U = look_up(x1, y1,
778 fYUVBitmaps[fYUVAIndices[1].fIndex],
779 fYUVAIndices[1].fChannel);
780
781
782 uint8_t V = look_up(x1, y1,
783 fYUVBitmaps[fYUVAIndices[2].fIndex],
784 fYUVAIndices[2].fChannel);
785
786 uint8_t A = 255;
787 if (fYUVAIndices[3].fIndex >= 0) {
788 A = look_up(x1, y1,
789 fYUVBitmaps[fYUVAIndices[3].fIndex],
790 fYUVAIndices[3].fChannel);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400791 }
792
793 // Making premul here.
Robert Phillips1c7062d2018-10-04 10:44:53 -0400794 switch (fYUVColorSpace) {
795 case kJPEG_SkYUVColorSpace:
796 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_jpeg(Y, U, V, A);
797 break;
798 case kRec601_SkYUVColorSpace:
799 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_601(Y, U, V, A);
800 break;
801 case kRec709_SkYUVColorSpace:
802 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_709(Y, U, V, A);
803 break;
Robert Phillips0a22ba82019-03-06 12:36:47 -0500804 case kIdentity_SkYUVColorSpace:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400805 *fFlattened.getAddr32(x, y) = SkPremultiplyARGBInline(A, V, U, Y);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500806 break;
Robert Phillips1c7062d2018-10-04 10:44:53 -0400807 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400808 }
809 }
810 }
811
812 return fFlattened.readPixels(info, pixels, rowBytes, 0, 0);
813 }
814
Jim Van Verthe24b5872018-10-29 16:26:02 -0400815 bool onQueryYUVA8(SkYUVASizeInfo* size,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400816 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
817 SkYUVColorSpace* yuvColorSpace) const override {
818
Robert Phillips2dd1b472019-03-21 09:00:20 -0400819 // The onQueryYUVA8/onGetYUVA8Planes can only handle A8 planes
820 if (!fAllA8) {
821 return false;
822 }
823
Jim Van Verth8f11e432018-10-18 14:36:59 -0400824 memcpy(yuvaIndices, fYUVAIndices, sizeof(fYUVAIndices));
825 *yuvColorSpace = fYUVColorSpace;
826
827 int i = 0;
828 for ( ; i < fNumBitmaps; ++i) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400829 size->fSizes[i].fWidth = fYUVBitmaps[i].width();
830 size->fSizes[i].fHeight = fYUVBitmaps[i].height();
831 size->fWidthBytes[i] = fYUVBitmaps[i].rowBytes();
832 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400833 for ( ; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400834 size->fSizes[i].fWidth = 0;
835 size->fSizes[i].fHeight = 0;
836 size->fWidthBytes[i] = 0;
Jim Van Verthf99a6742018-10-18 16:13:18 +0000837 }
Jim Van Verth0c583af2018-10-18 10:31:59 -0400838
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400839 return true;
840 }
841
Jim Van Verthe24b5872018-10-29 16:26:02 -0400842 bool onGetYUVA8Planes(const SkYUVASizeInfo&, const SkYUVAIndex[SkYUVAIndex::kIndexCount],
843 void* planes[SkYUVASizeInfo::kMaxCount]) override {
Robert Phillips2dd1b472019-03-21 09:00:20 -0400844 SkASSERT(fAllA8);
Jim Van Verth8f11e432018-10-18 14:36:59 -0400845 for (int i = 0; i < fNumBitmaps; ++i) {
846 planes[i] = fYUVBitmaps[i].getPixels();
847 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400848 return true;
849 }
850
851private:
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400852 SkYUVColorSpace fYUVColorSpace;
Jim Van Verth8f11e432018-10-18 14:36:59 -0400853 SkYUVAIndex fYUVAIndices[SkYUVAIndex::kIndexCount];
854 int fNumBitmaps;
Jim Van Verthe24b5872018-10-29 16:26:02 -0400855 SkBitmap fYUVBitmaps[SkYUVASizeInfo::kMaxCount];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400856 SkBitmap fFlattened;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400857 bool fAllA8; // are all the SkBitmaps in "fYUVBitmaps" A8?
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400858
859};
860
861static sk_sp<SkImage> make_yuv_gen_image(const SkImageInfo& ii,
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400862 SkYUVColorSpace yuvColorSpace,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400863 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400864 SkBitmap bitmaps[]) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400865 std::unique_ptr<SkImageGenerator> gen(new YUVGenerator(ii, yuvColorSpace,
866 yuvaIndices, bitmaps));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400867
868 return SkImage::MakeFromGenerator(std::move(gen));
869}
870
871static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500872 static const char* kYUVColorSpaceNames[] = { "JPEG", "601", "709", "Identity" };
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400873 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace+1);
874
Mike Reed91919132019-01-02 12:21:01 -0500875 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500876 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500877 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400878
879 SkRect textRect;
880 SkString colLabel;
881
882 colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400883 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400884 int y = textRect.height();
885
Mike Reed91919132019-01-02 12:21:01 -0500886 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400887
888 colLabel.printf("%s", opaque ? "Opaque" : "Transparent");
Mike Reed71f5a0b2018-10-25 16:12:39 -0400889
Ben Wagner51e15a62019-05-07 15:38:46 -0400890 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400891 y += textRect.height();
892
Mike Reed91919132019-01-02 12:21:01 -0500893 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400894}
895
896static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400897 static const char* kYUVFormatNames[] = {
898 "P016", "P010", "Y416", "AYUV", "Y410", "NV12", "NV21", "I420", "YV12"
899 };
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400900 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat+1);
901
Mike Reed91919132019-01-02 12:21:01 -0500902 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500903 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500904 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400905
906 SkRect textRect;
907 SkString rowLabel;
908
909 rowLabel.printf("%s", kYUVFormatNames[yuvFormat]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400910 font.measureText(rowLabel.c_str(), rowLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400911 y += kTileWidthHeight/2 + textRect.height()/2;
912
Hal Canary89a644b2019-01-07 09:36:09 -0500913 canvas->drawString(rowLabel, 0, y, font, paint);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400914}
915
Robert Phillipsbb749902019-06-10 17:20:12 -0400916static void make_RG_88(const GrCaps* caps,
917 const SkBitmap& bm, YUVFormat yuvFormat,
918 SkAutoTMalloc<uint8_t>* pixels,
919 GrBackendFormat* format) {
920 SkASSERT(kNV12_YUVFormat == yuvFormat || kNV21_YUVFormat == yuvFormat);
921 SkASSERT(kRGBA_8888_SkColorType == bm.colorType()); // uv stored in rg
922
923 pixels->reset(2 * sizeof(uint8_t) * bm.width() * bm.height());
924 uint8_t* currPixel = pixels->get();
925 for (int y = 0; y < bm.height(); ++y) {
926 for (int x = 0; x < bm.width(); ++x) {
927 SkColor color = bm.getColor(x, y);
928 uint8_t u8 = SkColorGetR(color);
929 uint8_t v8 = SkColorGetG(color);
930
931 currPixel[0] = u8;
932 currPixel[1] = v8;
933 currPixel += 2;
934 }
935 }
936 *format = caps->getBackendFormatFromGrColorType(GrColorType::kRG_88,
937 GrSRGBEncoded::kNo);
938}
939
940static void make_RG_1616(const GrCaps* caps,
941 const SkBitmap& bm, YUVFormat yuvFormat,
942 SkAutoTMalloc<uint8_t>* pixels,
943 GrBackendFormat* format) {
944 SkASSERT(kP016_YUVFormat == yuvFormat || kP010_YUVFormat == yuvFormat);
945 SkASSERT(kRGBA_8888_SkColorType == bm.colorType()); // uv stored in rg
946
947 uint16_t u16, v16;
948 pixels->reset(2 * sizeof(uint16_t) * bm.width() * bm.height());
949 uint16_t* currPixel = (uint16_t*) pixels->get();
950 for (int y = 0; y < bm.height(); ++y) {
951 for (int x = 0; x < bm.width(); ++x) {
952 SkColor color = bm.getColor(x, y);
953
954 if (kP016_YUVFormat == yuvFormat) {
955 u16 = SkScalarRoundToInt((SkColorGetR(color) / 255.0f) * 65535.0f);
956 v16 = SkScalarRoundToInt((SkColorGetG(color) / 255.0f) * 65535.0f);
957 } else {
958 u16 = SkScalarRoundToInt((SkColorGetR(color) / 255.0f) * 1023.0f);
959 v16 = SkScalarRoundToInt((SkColorGetG(color) / 255.0f) * 1023.0f);
960 u16 <<= 6;
961 v16 <<= 6;
962 }
963
964 currPixel[0] = u16;
965 currPixel[1] = v16;
966 currPixel += 2;
967 }
968 }
969
970 *format = caps->getBackendFormatFromGrColorType(GrColorType::kRG_1616,
971 GrSRGBEncoded::kNo);
972}
973
974static void make_RGBA_16(const GrCaps* caps,
975 const SkBitmap& bm,
976 YUVFormat yuvFormat,
977 SkAutoTMalloc<uint8_t>* pixels,
978 GrBackendFormat* format) {
979 SkASSERT(kY416_YUVFormat == yuvFormat);
980 SkASSERT(kRGBA_8888_SkColorType == bm.colorType());
981
982 uint16_t y16, u16, v16, a16;
983 pixels->reset(4 * sizeof(uint16_t) * bm.width() * bm.height());
984 uint16_t* currPixel = (uint16_t*) pixels->get();
985 for (int y = 0; y < bm.height(); ++y) {
986 for (int x = 0; x < bm.width(); ++x) {
987 SkColor color = bm.getColor(x, y);
988
989 y16 = SkScalarRoundToInt((SkColorGetR(color) / 255.0f) * 65535.0f);
990 u16 = SkScalarRoundToInt((SkColorGetG(color) / 255.0f) * 65535.0f);
991 v16 = SkScalarRoundToInt((SkColorGetB(color) / 255.0f) * 65535.0f);
992 a16 = SkScalarRoundToInt((SkColorGetA(color) / 255.0f) * 65535.0f);
993
994 currPixel[0] = y16;
995 currPixel[1] = u16;
996 currPixel[2] = v16;
997 currPixel[3] = a16;
998 currPixel += 4;
999 }
1000 }
1001
1002 // For this to work we need GrColorType::kRGBA_16 support, i.e.:
1003 // GL: RGBA16 (required in GL 3.0, added by GL_EXT_texture_norm16 for ES3.1)
1004 // Vk: VK_FORMAT_R16G16B16A16_UNORM
1005 // Mtl: MTLPixelFormatRGBA16Unorm
1006// *format = caps->getBackendFormatFromGrColorType(GrColorType::kRGBA_16161616,
1007// GrSRGBEncoded::kNo);
1008 return;
1009}
1010
1011static void make_R_16(const GrCaps* caps,
1012 const SkBitmap& bm,
1013 YUVFormat yuvFormat,
1014 SkAutoTMalloc<uint8_t>* pixels,
1015 GrBackendFormat* format) {
1016 SkASSERT(kP016_YUVFormat == yuvFormat || kP010_YUVFormat == yuvFormat);
1017 SkASSERT(kAlpha_8_SkColorType == bm.colorType());
1018
1019 uint16_t y16;
1020 pixels->reset(sizeof(uint16_t) * bm.width() * bm.height());
1021 uint16_t* currPixel = (uint16_t*) pixels->get();
1022 for (int y = 0; y < bm.height(); ++y) {
1023 for (int x = 0; x < bm.width(); ++x) {
1024 uint8_t y8 = *bm.getAddr8(x, y);
1025
1026 if (kP016_YUVFormat == yuvFormat) {
1027 y16 = SkScalarRoundToInt((y8 / 255.0f) * 65535.0f);
1028 } else {
1029 y16 = SkScalarRoundToInt((y8 / 255.0f) * 1023.0f);
1030 y16 <<= 6;
1031 }
1032
1033 currPixel[0] = y16;
1034 currPixel += 1;
1035 }
1036 }
1037
1038 *format = caps->getBackendFormatFromGrColorType(GrColorType::kR_16, GrSRGBEncoded::kNo);
1039}
1040
Robert Phillipscb1adb42019-06-10 15:09:34 -04001041static GrBackendTexture create_yuva_texture(GrContext* context, const SkBitmap& bm,
Robert Phillipsbb749902019-06-10 17:20:12 -04001042 SkYUVAIndex yuvaIndices[4], int texIndex,
1043 YUVFormat yuvFormat) {
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001044 SkASSERT(texIndex >= 0 && texIndex <= 3);
1045 int channelCount = 0;
1046 for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
1047 if (yuvaIndices[i].fIndex == texIndex) {
1048 ++channelCount;
1049 }
1050 }
Robert Phillipsbb749902019-06-10 17:20:12 -04001051
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001052 GrBackendTexture tex;
Robert Phillipsbb749902019-06-10 17:20:12 -04001053
1054 if (format_uses_16_bpp(yuvFormat) || 2 == channelCount) {
1055 // Due to the limitations of SkPixmap these cases need to be handled separately
Robert Phillipscb1adb42019-06-10 15:09:34 -04001056 const GrCaps* caps = context->priv().caps();
1057 GrGpu* gpu = context->priv().getGpu();
1058
Robert Phillipsbb749902019-06-10 17:20:12 -04001059 SkAutoTMalloc<uint8_t> pixels;
1060 GrBackendFormat format;
1061
1062 if (2 == channelCount) {
1063 if (format_uses_16_bpp(yuvFormat)) {
1064 make_RG_1616(caps, bm, yuvFormat, &pixels, &format);
1065 } else {
1066 make_RG_88(caps, bm, yuvFormat, &pixels, &format);
1067 }
1068 } else {
1069 if (kRGBA_8888_SkColorType == bm.colorType()) {
1070 make_RGBA_16(caps, bm, yuvFormat, &pixels, &format);
1071 } else {
1072 make_R_16(caps, bm, yuvFormat, &pixels, &format);
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001073 }
1074 }
Robert Phillipsbb749902019-06-10 17:20:12 -04001075
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001076 tex = gpu->createBackendTexture(bm.width(), bm.height(), format,
1077 GrMipMapped::kNo, GrRenderable::kNo,
Robert Phillipsbb749902019-06-10 17:20:12 -04001078 pixels, 0, nullptr);
1079 } else {
Robert Phillipscb1adb42019-06-10 15:09:34 -04001080 tex = context->priv().createBackendTexture(&bm.pixmap(), 1, GrRenderable::kNo);
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001081 }
Robert Phillipsbb749902019-06-10 17:20:12 -04001082
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001083 return tex;
1084}
1085
Robert Phillips0a22ba82019-03-06 12:36:47 -05001086static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
1087 static const float kJPEGConversionMatrix[20] = {
Mike Reede869a1e2019-04-30 12:18:54 -04001088 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
1089 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
1090 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
Robert Phillips0a22ba82019-03-06 12:36:47 -05001091 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
1092 };
1093
Mike Reede869a1e2019-04-30 12:18:54 -04001094 return SkColorFilters::Matrix(kJPEGConversionMatrix);
Robert Phillips0a22ba82019-03-06 12:36:47 -05001095}
1096
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001097namespace skiagm {
1098
1099// This GM creates an opaque and transparent bitmap, extracts the planes and then recombines
1100// them into various YUV formats. It then renders the results in the grid:
1101//
Robert Phillips2dd1b472019-03-21 09:00:20 -04001102// JPEG 601 709 Identity
1103// Transparent Opaque Transparent Opaque Transparent Opaque Transparent Opaque
Robert Phillipsbb749902019-06-10 17:20:12 -04001104// originals
1105// P016
1106// P010
1107// Y416
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001108// AYUV
Robert Phillips2dd1b472019-03-21 09:00:20 -04001109// Y410
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001110// NV12
1111// NV21
1112// I420
1113// YV12
1114class WackyYUVFormatsGM : public GM {
1115public:
Michael Ludwiga6a84002019-04-12 15:03:02 -04001116 WackyYUVFormatsGM(bool useTargetColorSpace, bool useDomain)
1117 : fUseTargetColorSpace(useTargetColorSpace)
1118 , fUseDomain(useDomain) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001119 this->setBGColor(0xFFCCCCCC);
1120 }
1121
1122protected:
1123
1124 SkString onShortName() override {
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001125 SkString name("wacky_yuv_formats");
1126 if (fUseTargetColorSpace) {
1127 name += "_cs";
1128 }
Michael Ludwiga6a84002019-04-12 15:03:02 -04001129 if (fUseDomain) {
1130 name += "_domain";
1131 }
Robert Phillips2dd1b472019-03-21 09:00:20 -04001132
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001133 return name;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001134 }
1135
1136 SkISize onISize() override {
1137 int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x color-space
1138 int numRows = 1 + (kLast_YUVFormat + 1); // origin + # yuv formats
Michael Ludwiga6a84002019-04-12 15:03:02 -04001139 int wh = SkScalarCeilToInt(kTileWidthHeight * (fUseDomain ? 1.5f : 1.f));
1140 return SkISize::Make(kLabelWidth + numCols * (wh + kPad),
1141 kLabelHeight + numRows * (wh + kPad));
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001142 }
1143
1144 void onOnceBeforeDraw() override {
1145 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
1146 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
1147 float innerRadius = 20.0f;
1148
1149 {
1150 // transparent
1151 SkTDArray<SkRect> circles;
1152 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001153 fOriginalBMs[0] = make_bitmap(kRGBA_8888_SkColorType, path, circles, false, fUseDomain);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001154 }
1155
1156 {
1157 // opaque
1158 SkTDArray<SkRect> circles;
1159 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001160 fOriginalBMs[1] = make_bitmap(kRGBA_8888_SkColorType, path, circles, true, fUseDomain);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001161 }
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001162
1163 if (fUseTargetColorSpace) {
1164 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
1165 }
Robert Phillips51c89e42018-10-05 13:30:43 -04001166 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001167
Robert Phillips51c89e42018-10-05 13:30:43 -04001168 void createImages(GrContext* context) {
Jim Van Verth9bf81202018-10-30 15:53:36 -04001169 int counter = 0;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001170 for (bool opaque : { false, true }) {
1171 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
1172 PlaneData planes;
1173 extract_planes(fOriginalBMs[opaque], (SkYUVColorSpace) cs, &planes);
1174
Robert Phillipsbb749902019-06-10 17:20:12 -04001175 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001176 SkBitmap resultBMs[4];
1177 SkYUVAIndex yuvaIndices[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001178
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001179 create_YUV(planes, (YUVFormat) format, resultBMs, yuvaIndices, opaque);
Robert Phillips2dd1b472019-03-21 09:00:20 -04001180
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001181 int numTextures;
1182 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) {
1183 continue;
1184 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001185
Robert Phillips51c89e42018-10-05 13:30:43 -04001186 if (context) {
Chris Dalton382b1222019-02-07 10:05:55 +00001187 if (context->abandoned()) {
1188 return;
1189 }
1190
Robert Phillips51c89e42018-10-05 13:30:43 -04001191 GrBackendTexture yuvaTextures[4];
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001192 SkPixmap yuvaPixmaps[4];
Robert Phillips51c89e42018-10-05 13:30:43 -04001193
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001194 for (int i = 0; i < numTextures; ++i) {
Robert Phillipscb1adb42019-06-10 15:09:34 -04001195 yuvaTextures[i] = create_yuva_texture(context, resultBMs[i],
Robert Phillipsbb749902019-06-10 17:20:12 -04001196 yuvaIndices, i,
1197 (YUVFormat) format);
Brian Salomonf2580c02019-01-30 09:13:31 -05001198 if (yuvaTextures[i].isValid()) {
1199 fBackendTextures.push_back(yuvaTextures[i]);
1200 }
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001201 yuvaPixmaps[i] = resultBMs[i].pixmap();
Robert Phillips51c89e42018-10-05 13:30:43 -04001202 }
1203
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001204 int counterMod = counter % 3;
Robert Phillipsbb749902019-06-10 17:20:12 -04001205 if (format_cant_be_represented_with_pixmaps((YUVFormat) format) &&
1206 counterMod == 2) {
1207 // These formats don't work as pixmaps
Michael Ludwiga6a84002019-04-12 15:03:02 -04001208 counterMod = 1;
1209 } else if (fUseDomain && counterMod == 0) {
1210 // Copies flatten to RGB when they copy the YUVA data, which doesn't
1211 // know about the intended domain and the domain padding bleeds in
1212 counterMod = 1;
1213 }
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001214 switch (counterMod) {
1215 case 0:
Jim Van Verth9bf81202018-10-30 15:53:36 -04001216 fImages[opaque][cs][format] = SkImage::MakeFromYUVATexturesCopy(
1217 context,
1218 (SkYUVColorSpace)cs,
1219 yuvaTextures,
1220 yuvaIndices,
1221 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1222 kTopLeft_GrSurfaceOrigin);
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001223 break;
1224 case 1:
Jim Van Verth9bf81202018-10-30 15:53:36 -04001225 fImages[opaque][cs][format] = SkImage::MakeFromYUVATextures(
1226 context,
1227 (SkYUVColorSpace)cs,
1228 yuvaTextures,
1229 yuvaIndices,
1230 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1231 kTopLeft_GrSurfaceOrigin);
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001232 break;
1233 case 2:
1234 default:
1235 fImages[opaque][cs][format] = SkImage::MakeFromYUVAPixmaps(
1236 context,
1237 (SkYUVColorSpace)cs,
1238 yuvaPixmaps,
1239 yuvaIndices,
1240 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1241 kTopLeft_GrSurfaceOrigin, true);
1242 break;
Jim Van Verth9bf81202018-10-30 15:53:36 -04001243 }
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001244 ++counter;
Jim Van Verth9bf81202018-10-30 15:53:36 -04001245 } else {
Robert Phillips51c89e42018-10-05 13:30:43 -04001246 fImages[opaque][cs][format] = make_yuv_gen_image(
1247 fOriginalBMs[opaque].info(),
Robert Phillips51c89e42018-10-05 13:30:43 -04001248 (SkYUVColorSpace) cs,
1249 yuvaIndices,
1250 resultBMs);
1251 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001252 }
1253 }
1254 }
1255 }
1256
1257 void onDraw(SkCanvas* canvas) override {
Robert Phillips51c89e42018-10-05 13:30:43 -04001258 this->createImages(canvas->getGrContext());
1259
Michael Ludwiga6a84002019-04-12 15:03:02 -04001260 SkRect srcRect = SkRect::MakeWH(fOriginalBMs[0].width(), fOriginalBMs[0].height());
1261 SkRect dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, srcRect.width(), srcRect.height());
1262
1263 SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint;
1264 if (fUseDomain) {
1265 srcRect.inset(kDomainPadding, kDomainPadding);
1266 // Draw a larger rectangle to ensure bilerp filtering would normally read outside the
1267 // srcRect and hit the red pixels, if strict constraint weren't used.
1268 dstRect.fRight = kLabelWidth + 1.5f * srcRect.width();
1269 dstRect.fBottom = 1.5f * srcRect.height();
1270 constraint = SkCanvas::kStrict_SrcRectConstraint;
1271 }
1272
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001273 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
Robert Phillips0a22ba82019-03-06 12:36:47 -05001274 SkPaint paint;
Michael Ludwiga6a84002019-04-12 15:03:02 -04001275 paint.setFilterQuality(kLow_SkFilterQuality);
Robert Phillips0a22ba82019-03-06 12:36:47 -05001276 if (kIdentity_SkYUVColorSpace == cs) {
1277 // The identity color space needs post processing to appear correctly
1278 paint.setColorFilter(yuv_to_rgb_colorfilter());
1279 }
1280
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001281 for (int opaque : { 0, 1 }) {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001282 dstRect.offsetTo(dstRect.fLeft, kLabelHeight);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001283
Michael Ludwiga6a84002019-04-12 15:03:02 -04001284 draw_col_label(canvas, dstRect.fLeft + dstRect.height() / 2, cs, opaque);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001285
Michael Ludwiga6a84002019-04-12 15:03:02 -04001286 canvas->drawBitmapRect(fOriginalBMs[opaque], srcRect, dstRect, nullptr, constraint);
1287 dstRect.offset(0.f, dstRect.height() + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001288
Robert Phillipsbb749902019-06-10 17:20:12 -04001289 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001290 draw_row_label(canvas, dstRect.fTop, format);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001291 if (fUseTargetColorSpace && fImages[opaque][cs][format]) {
Robert Phillips0a22ba82019-03-06 12:36:47 -05001292 // Making a CS-specific version of a kIdentity_SkYUVColorSpace YUV image
1293 // doesn't make a whole lot of sense. The colorSpace conversion will
1294 // operate on the YUV components rather than the RGB components.
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001295 sk_sp<SkImage> csImage =
1296 fImages[opaque][cs][format]->makeColorSpace(fTargetColorSpace);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001297 canvas->drawImageRect(csImage, srcRect, dstRect, &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001298 } else {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001299 canvas->drawImageRect(fImages[opaque][cs][format], srcRect, dstRect, &paint,
1300 constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001301 }
Michael Ludwiga6a84002019-04-12 15:03:02 -04001302 dstRect.offset(0.f, dstRect.height() + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001303 }
1304
Michael Ludwiga6a84002019-04-12 15:03:02 -04001305 dstRect.offset(dstRect.width() + kPad, 0.f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001306 }
1307 }
Brian Salomon8f388ce2019-01-29 14:50:53 -05001308 if (auto context = canvas->getGrContext()) {
Chris Dalton382b1222019-02-07 10:05:55 +00001309 if (!context->abandoned()) {
1310 context->flush();
1311 GrGpu* gpu = context->priv().getGpu();
1312 SkASSERT(gpu);
1313 gpu->testingOnly_flushGpuAndSync();
1314 for (const auto& tex : fBackendTextures) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001315 context->deleteBackendTexture(tex);
Chris Dalton382b1222019-02-07 10:05:55 +00001316 }
1317 fBackendTextures.reset();
Brian Salomon8f388ce2019-01-29 14:50:53 -05001318 }
Brian Salomon8f388ce2019-01-29 14:50:53 -05001319 }
1320 SkASSERT(!fBackendTextures.count());
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001321 }
1322
1323private:
Robert Phillips2dd1b472019-03-21 09:00:20 -04001324 SkBitmap fOriginalBMs[2];
1325 sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace + 1][kLast_YUVFormat + 1];
Brian Salomon8f388ce2019-01-29 14:50:53 -05001326 SkTArray<GrBackendTexture> fBackendTextures;
Robert Phillips2dd1b472019-03-21 09:00:20 -04001327 bool fUseTargetColorSpace;
Michael Ludwiga6a84002019-04-12 15:03:02 -04001328 bool fUseDomain;
Robert Phillips2dd1b472019-03-21 09:00:20 -04001329 sk_sp<SkColorSpace> fTargetColorSpace;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001330
1331 typedef GM INHERITED;
1332};
1333
1334//////////////////////////////////////////////////////////////////////////////
1335
Michael Ludwiga6a84002019-04-12 15:03:02 -04001336DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ false);)
1337DEF_GM(return new WackyYUVFormatsGM(/* cs */ true, /* domain */ false);)
1338DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ true);)
Brian Osmane9560492019-02-05 17:00:03 -05001339
Chris Dalton3a778372019-02-07 15:23:36 -07001340class YUVMakeColorSpaceGM : public GpuGM {
Brian Osmane9560492019-02-05 17:00:03 -05001341public:
1342 YUVMakeColorSpaceGM() {
1343 this->setBGColor(0xFFCCCCCC);
1344 }
1345
1346protected:
1347 SkString onShortName() override {
1348 return SkString("yuv_make_color_space");
1349 }
1350
1351 SkISize onISize() override {
1352 int numCols = 4; // (transparent, opaque) x (untagged, tagged)
1353 int numRows = 5; // original, YUV, subset, readPixels, makeNonTextureImage
1354 return SkISize::Make(numCols * (kTileWidthHeight + kPad) + kPad,
1355 numRows * (kTileWidthHeight + kPad) + kPad);
1356 }
1357
1358 void onOnceBeforeDraw() override {
1359 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
1360 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
1361 float innerRadius = 20.0f;
1362
1363 {
1364 // transparent
1365 SkTDArray<SkRect> circles;
1366 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001367 fOriginalBMs[0] = make_bitmap(kN32_SkColorType, path, circles, false, false);
Brian Osmane9560492019-02-05 17:00:03 -05001368 }
1369
1370 {
1371 // opaque
1372 SkTDArray<SkRect> circles;
1373 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001374 fOriginalBMs[1] = make_bitmap(kN32_SkColorType, path, circles, true, false);
Brian Osmane9560492019-02-05 17:00:03 -05001375 }
1376
1377 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
1378 }
1379
1380 void createImages(GrContext* context) {
1381 for (bool opaque : { false, true }) {
1382 PlaneData planes;
1383 extract_planes(fOriginalBMs[opaque], kJPEG_SkYUVColorSpace, &planes);
1384
1385 SkBitmap resultBMs[4];
1386 SkYUVAIndex yuvaIndices[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001387
Brian Osmane9560492019-02-05 17:00:03 -05001388 create_YUV(planes, kAYUV_YUVFormat, resultBMs, yuvaIndices, opaque);
Robert Phillips2dd1b472019-03-21 09:00:20 -04001389
Brian Osmane9560492019-02-05 17:00:03 -05001390 int numTextures;
1391 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) {
1392 continue;
1393 }
1394
Brian Osmane9560492019-02-05 17:00:03 -05001395 GrBackendTexture yuvaTextures[4];
1396 for (int i = 0; i < numTextures; ++i) {
Robert Phillipsbb749902019-06-10 17:20:12 -04001397 yuvaTextures[i] = create_yuva_texture(context, resultBMs[i], yuvaIndices, i,
1398 kAYUV_YUVFormat);
Brian Osmane9560492019-02-05 17:00:03 -05001399 if (yuvaTextures[i].isValid()) {
1400 fBackendTextures.push_back(yuvaTextures[i]);
1401 }
1402 }
1403
1404 fImages[opaque][0] = SkImage::MakeFromYUVATextures(
1405 context,
1406 kJPEG_SkYUVColorSpace,
1407 yuvaTextures,
1408 yuvaIndices,
1409 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1410 kTopLeft_GrSurfaceOrigin);
1411 fImages[opaque][1] = SkImage::MakeFromYUVATextures(
1412 context,
1413 kJPEG_SkYUVColorSpace,
1414 yuvaTextures,
1415 yuvaIndices,
1416 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1417 kTopLeft_GrSurfaceOrigin,
1418 SkColorSpace::MakeSRGB());
1419 }
1420 }
1421
Chris Dalton3a778372019-02-07 15:23:36 -07001422 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Brian Osmane9560492019-02-05 17:00:03 -05001423 this->createImages(context);
1424
1425 int x = kPad;
1426 for (int tagged : { 0, 1 }) {
1427 for (int opaque : { 0, 1 }) {
1428 int y = kPad;
1429
1430 auto raster = SkImage::MakeFromBitmap(fOriginalBMs[opaque])
1431 ->makeColorSpace(fTargetColorSpace);
1432 canvas->drawImage(raster, x, y);
1433 y += kTileWidthHeight + kPad;
1434
1435 auto yuv = fImages[opaque][tagged]->makeColorSpace(fTargetColorSpace);
1436 SkASSERT(SkColorSpace::Equals(yuv->colorSpace(), fTargetColorSpace.get()));
1437 canvas->drawImage(yuv, x, y);
1438 y += kTileWidthHeight + kPad;
1439
1440 auto subset = yuv->makeSubset(SkIRect::MakeWH(kTileWidthHeight / 2,
1441 kTileWidthHeight / 2));
1442 canvas->drawImage(subset, x, y);
1443 y += kTileWidthHeight + kPad;
1444
1445 auto nonTexture = yuv->makeNonTextureImage();
1446 canvas->drawImage(nonTexture, x, y);
1447 y += kTileWidthHeight + kPad;
1448
1449 SkBitmap readBack;
Brian Salomon5ad6fd32019-03-21 15:30:08 -04001450 readBack.allocPixels(yuv->imageInfo());
Brian Osmane9560492019-02-05 17:00:03 -05001451 yuv->readPixels(readBack.pixmap(), 0, 0);
1452 canvas->drawBitmap(readBack, x, y);
1453
1454 x += kTileWidthHeight + kPad;
1455 }
1456 }
1457
1458 context->flush();
1459 GrGpu* gpu = context->priv().getGpu();
1460 SkASSERT(gpu);
1461 gpu->testingOnly_flushGpuAndSync();
1462 for (const auto& tex : fBackendTextures) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001463 context->deleteBackendTexture(tex);
Brian Osmane9560492019-02-05 17:00:03 -05001464 }
1465 fBackendTextures.reset();
1466 }
1467
1468private:
1469 SkBitmap fOriginalBMs[2];
1470 sk_sp<SkImage> fImages[2][2];
1471 SkTArray<GrBackendTexture> fBackendTextures;
1472 sk_sp<SkColorSpace> fTargetColorSpace;
1473
1474 typedef GM INHERITED;
1475};
1476
1477DEF_GM(return new YUVMakeColorSpaceGM();)
1478
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001479}
Mike Reed6a5f7e22019-05-23 15:30:07 -04001480
1481///////////////
1482
Mike Reed6a5f7e22019-05-23 15:30:07 -04001483#include "include/effects/SkColorMatrix.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001484#include "src/core/SkAutoPixmapStorage.h"
1485#include "src/core/SkYUVMath.h"
1486#include "tools/Resources.h"
Mike Reed6a5f7e22019-05-23 15:30:07 -04001487
1488static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) {
1489 auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
1490 canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height());
1491 SkPaint paint;
1492 paint.setFilterQuality(kLow_SkFilterQuality);
1493 paint.setColorFilter(cf);
1494 paint.setBlendMode(SkBlendMode::kSrc);
1495 canvas->drawImage(img, 0, 0, &paint);
1496}
1497
1498static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) {
1499 float m[20];
1500 SkColorMatrix_RGB2YUV(cs, m);
1501
1502 memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A
1503 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]);
1504
1505 memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A
1506 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]);
1507
1508 memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A
1509 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]);
1510}
1511
1512static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y,
1513 const SkImage* a, const SkImage* b) {
1514 auto sh = SkShaders::Blend(SkBlendMode::kDifference, a->makeShader(), b->makeShader());
1515 SkPaint paint;
1516 paint.setShader(sh);
1517 canvas->save();
1518 canvas->translate(x, y);
1519 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1520
1521 SkColorMatrix cm;
1522 cm.setScale(64, 64, 64);
1523 paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm)));
1524 canvas->translate(0, a->height());
1525 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1526
1527 canvas->restore();
1528}
1529
1530// Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the
1531// resulting (recombined) images (gpu only for now).
1532//
1533class YUVSplitterGM : public skiagm::GM {
1534 sk_sp<SkImage> fOrig;
1535 SkAutoPixmapStorage fStorage[3];
1536 SkPixmap fPM[3];
1537
1538public:
1539 YUVSplitterGM() {}
1540
1541protected:
1542
1543 SkString onShortName() override {
1544 return SkString("yuv_splitter");
1545 }
1546
1547 SkISize onISize() override {
1548 return SkISize::Make(1024, 768);
1549 }
1550
1551 void onOnceBeforeDraw() override {
1552 fOrig = GetResourceAsImage("images/mandrill_256.png");
1553
1554 SkImageInfo info = SkImageInfo::Make(fOrig->width(), fOrig->height(), kAlpha_8_SkColorType,
1555 kPremul_SkAlphaType);
1556 fStorage[0].alloc(info);
1557 if (0) {
1558 // if you want to scale U,V down by 1/2
1559 info = info.makeWH(info.width()/2, info.height()/2);
1560 }
1561 fStorage[1].alloc(info);
1562 fStorage[2].alloc(info);
1563 for (int i = 0; i < 3; ++i) {
1564 fPM[i] = fStorage[i];
1565 }
1566 }
1567
1568 void onDraw(SkCanvas* canvas) override {
1569 SkYUVAIndex indices[4];
1570 indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
1571 indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
1572 indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR};
1573 indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kR};
1574
1575 canvas->translate(fOrig->width(), 0);
1576 canvas->save();
1577 for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace}) {
1578 split_into_yuv(fOrig.get(), cs, fPM);
1579 auto img = SkImage::MakeFromYUVAPixmaps(canvas->getGrContext(), cs, fPM, indices,
1580 fPM[0].info().dimensions(),
1581 kTopLeft_GrSurfaceOrigin,
1582 false, false, nullptr);
1583 if (img) {
1584 canvas->drawImage(img, 0, 0, nullptr);
1585 draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get());
1586 }
1587 canvas->translate(fOrig->width(), 0);
1588 }
1589 canvas->restore();
1590 canvas->translate(-fOrig->width(), 0);
1591
1592 canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0, nullptr);
1593 canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height(), nullptr);
1594 canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]),
1595 0, fPM[0].height() + fPM[1].height(), nullptr);
1596 }
1597
1598private:
1599 typedef GM INHERITED;
1600};
1601DEF_GM( return new YUVSplitterGM; )