blob: b69ae1cd4b5fd4379feb016b489057a2089f0fbb [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) {
Robert Phillips712f0a72019-06-20 13:14:36 -0400103 return kP016_YUVFormat == yuvFormat || // bc missing SkColorType::kRG_1616 and kR_16
104 kP010_YUVFormat == yuvFormat || // bc missing SkColorType::kRG_1616 and kR_16
105 kY416_YUVFormat == yuvFormat || // bc missing SkColorType::kRGBA_16161616
106 kNV12_YUVFormat == yuvFormat || // bc missing SkColorType::kRG_88
107 kNV21_YUVFormat == yuvFormat; // bc missing SkColorType::kRG_88
Robert Phillipsbb749902019-06-10 17:20:12 -0400108}
109
Robert Phillipsdc62b642019-05-16 10:10:45 -0400110// Helper to setup the SkYUVAIndex array correctly
111// Skia allows the client to tack an additional alpha plane onto any of the standard opaque
112// formats (via the addExtraAlpha) flag. In this case it is assumed to be a stand-alone single-
113// channel plane.
114static void setup_yuv_indices(YUVFormat yuvFormat, bool addExtraAlpha, SkYUVAIndex yuvaIndices[4]) {
115 switch (yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400116 case kP016_YUVFormat: // fall through
117 case kP010_YUVFormat:
118 yuvaIndices[0].fIndex = 0;
119 yuvaIndices[0].fChannel = SkColorChannel::kR; // bc 16bit is stored in R16 format
120 yuvaIndices[1].fIndex = 1;
121 yuvaIndices[1].fChannel = SkColorChannel::kR;
122 yuvaIndices[2].fIndex = 1;
123 yuvaIndices[2].fChannel = SkColorChannel::kG;
124 if (addExtraAlpha) {
125 yuvaIndices[3].fIndex = 2;
126 yuvaIndices[3].fChannel = SkColorChannel::kR; // bc 16bit is stored in R16 format
127 } else {
128 yuvaIndices[3].fIndex = -1; // No alpha channel
129 }
130 break;
131 case kY416_YUVFormat:
132 SkASSERT(!addExtraAlpha); // this format already has an alpha channel
133 yuvaIndices[0].fIndex = 0;
134 yuvaIndices[0].fChannel = SkColorChannel::kG;
135 yuvaIndices[1].fIndex = 0;
136 yuvaIndices[1].fChannel = SkColorChannel::kB;
137 yuvaIndices[2].fIndex = 0;
138 yuvaIndices[2].fChannel = SkColorChannel::kR;
139 yuvaIndices[3].fIndex = 0;
140 yuvaIndices[3].fChannel = SkColorChannel::kA;
141 break;
Robert Phillipsdc62b642019-05-16 10:10:45 -0400142 case kAYUV_YUVFormat:
143 SkASSERT(!addExtraAlpha); // this format already has an alpha channel
144 yuvaIndices[0].fIndex = 0;
145 yuvaIndices[0].fChannel = SkColorChannel::kR;
146 yuvaIndices[1].fIndex = 0;
147 yuvaIndices[1].fChannel = SkColorChannel::kG;
148 yuvaIndices[2].fIndex = 0;
149 yuvaIndices[2].fChannel = SkColorChannel::kB;
150 yuvaIndices[3].fIndex = 0;
151 yuvaIndices[3].fChannel = SkColorChannel::kA;
152 break;
153 case kY410_YUVFormat:
154 SkASSERT(!addExtraAlpha); // this format already has an alpha channel
155 yuvaIndices[0].fIndex = 0;
156 yuvaIndices[0].fChannel = SkColorChannel::kG;
157 yuvaIndices[1].fIndex = 0;
158 yuvaIndices[1].fChannel = SkColorChannel::kB;
159 yuvaIndices[2].fIndex = 0;
160 yuvaIndices[2].fChannel = SkColorChannel::kR;
161 yuvaIndices[3].fIndex = 0;
162 yuvaIndices[3].fChannel = SkColorChannel::kA;
163 break;
164 case kNV12_YUVFormat:
165 yuvaIndices[0].fIndex = 0;
166 yuvaIndices[0].fChannel = SkColorChannel::kA;
167 yuvaIndices[1].fIndex = 1;
168 yuvaIndices[1].fChannel = SkColorChannel::kR;
169 yuvaIndices[2].fIndex = 1;
170 yuvaIndices[2].fChannel = SkColorChannel::kG;
171 if (addExtraAlpha) {
172 yuvaIndices[3].fIndex = 2;
173 yuvaIndices[3].fChannel = SkColorChannel::kA;
174 } else {
175 yuvaIndices[3].fIndex = -1; // No alpha channel
176 }
177 break;
178 case kNV21_YUVFormat:
179 yuvaIndices[0].fIndex = 0;
180 yuvaIndices[0].fChannel = SkColorChannel::kA;
181 yuvaIndices[1].fIndex = 1;
182 yuvaIndices[1].fChannel = SkColorChannel::kG;
183 yuvaIndices[2].fIndex = 1;
184 yuvaIndices[2].fChannel = SkColorChannel::kR;
185 if (addExtraAlpha) {
186 yuvaIndices[3].fIndex = 2;
187 yuvaIndices[3].fChannel = SkColorChannel::kA;
188 } else {
189 yuvaIndices[3].fIndex = -1; // No alpha channel
190 }
191 break;
192 case kI420_YUVFormat:
193 yuvaIndices[0].fIndex = 0;
194 yuvaIndices[0].fChannel = SkColorChannel::kA;
195 yuvaIndices[1].fIndex = 1;
196 yuvaIndices[1].fChannel = SkColorChannel::kA;
197 yuvaIndices[2].fIndex = 2;
198 yuvaIndices[2].fChannel = SkColorChannel::kA;
199 if (addExtraAlpha) {
200 yuvaIndices[3].fIndex = 3;
201 yuvaIndices[3].fChannel = SkColorChannel::kA;
202 } else {
203 yuvaIndices[3].fIndex = -1; // No alpha channel
204 }
205 break;
206 case kYV12_YUVFormat:
207 yuvaIndices[0].fIndex = 0;
208 yuvaIndices[0].fChannel = SkColorChannel::kA;
209 yuvaIndices[1].fIndex = 2;
210 yuvaIndices[1].fChannel = SkColorChannel::kA;
211 yuvaIndices[2].fIndex = 1;
212 yuvaIndices[2].fChannel = SkColorChannel::kA;
213 if (addExtraAlpha) {
214 yuvaIndices[3].fIndex = 3;
215 yuvaIndices[3].fChannel = SkColorChannel::kA;
216 } else {
217 yuvaIndices[3].fIndex = -1; // No alpha channel
218 }
219 break;
220 }
221}
222
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400223// All the planes we need to construct the various YUV formats
224struct PlaneData {
225 SkBitmap fYFull;
226 SkBitmap fUFull;
227 SkBitmap fVFull;
228 SkBitmap fAFull;
229 SkBitmap fUQuarter; // 2x2 downsampled U channel
230 SkBitmap fVQuarter; // 2x2 downsampled V channel
231};
232
233// Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle
234// and have tangents 'v1' and 'v2'.
235static void add_arc(SkPath* path,
236 const SkPoint& o1, const SkVector& v1,
237 const SkPoint& o2, const SkVector& v2,
238 SkTDArray<SkRect>* circles, bool takeLongWayRound) {
239
240 SkVector v3 = { -v1.fY, v1.fX };
241 SkVector v4 = { v2.fY, -v2.fX };
242
243 SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4);
244 SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY };
245
246 SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t };
247
248 if (circles) {
249 circles->push_back(r);
250 }
251
252 SkVector startV = o1 - center, endV = o2 - center;
253 startV.normalize();
254 endV.normalize();
255
256 SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX));
257 SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX));
258
259 startDeg += 360.0f;
260 startDeg = fmodf(startDeg, 360.0f);
261
262 endDeg += 360.0f;
263 endDeg = fmodf(endDeg, 360.0f);
264
265 if (endDeg < startDeg) {
266 endDeg += 360.0f;
267 }
268
269 SkScalar sweepDeg = SkTAbs(endDeg - startDeg);
270 if (!takeLongWayRound) {
271 sweepDeg = sweepDeg - 360;
272 }
273
274 path->arcTo(r, startDeg, sweepDeg, false);
275}
276
277static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius,
278 SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) {
279 if (numLobes <= 1) {
280 return SkPath();
281 }
282
283 SkPath p;
284
285 int numDivisions = 2 * numLobes;
286 SkScalar fullLobeDegrees = 360.0f / numLobes;
287 SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f);
288 SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f);
289 SkMatrix outerStep, innerStep;
290 outerStep.setRotate(outDegrees);
291 innerStep.setRotate(innerDegrees);
292 SkVector curV = SkVector::Make(0.0f, 1.0f);
293
294 if (circles) {
295 circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius,
296 o.fX + innerRadius, o.fY + innerRadius));
297 }
298
299 p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY);
300
301 for (int i = 0; i < numDivisions; ++i) {
302
303 SkVector nextV;
304 if (0 == (i % 2)) {
305 nextV = outerStep.mapVector(curV.fX, curV.fY);
306
307 SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX,
308 o.fY + outerRadius * curV.fY);
309 SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX,
310 o.fY + outerRadius * nextV.fY);
311
312 p.lineTo(top);
313 add_arc(&p, top, curV, nextTop, nextV, circles, true);
314 } else {
315 nextV = innerStep.mapVector(curV.fX, curV.fY);
316
317 SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX,
318 o.fY + innerRadius * curV.fY);
319 SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX,
320 o.fY + innerRadius * nextV.fY);
321
322 p.lineTo(bot);
323 add_arc(&p, bot, curV, nextBot, nextV, nullptr, false);
324 }
325
326 curV = nextV;
327 }
328
329 p.close();
330
331 return p;
332}
333
Robert Phillips2dd1b472019-03-21 09:00:20 -0400334static SkBitmap make_bitmap(SkColorType colorType, const SkPath& path,
Michael Ludwiga6a84002019-04-12 15:03:02 -0400335 const SkTDArray<SkRect>& circles, bool opaque, bool padWithRed) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500336 const SkColor kGreen = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104));
337 const SkColor kBlue = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252));
338 const SkColor kYellow = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400339
Michael Ludwiga6a84002019-04-12 15:03:02 -0400340 int widthHeight = kTileWidthHeight + (padWithRed ? 2 * kDomainPadding : 0);
341
342 SkImageInfo ii = SkImageInfo::Make(widthHeight, widthHeight,
Robert Phillips2dd1b472019-03-21 09:00:20 -0400343 colorType, kPremul_SkAlphaType);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400344
345 SkBitmap bm;
346 bm.allocPixels(ii);
347
Robert Phillips2dd1b472019-03-21 09:00:20 -0400348 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(ii,
349 bm.getPixels(),
350 bm.rowBytes());
Michael Ludwiga6a84002019-04-12 15:03:02 -0400351 if (padWithRed) {
352 canvas->clear(SK_ColorRED);
353 canvas->translate(kDomainPadding, kDomainPadding);
354 canvas->clipRect(SkRect::MakeWH(kTileWidthHeight, kTileWidthHeight));
355 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400356 canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT);
357
358 SkPaint paint;
359 paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency
360 paint.setColor(kBlue);
361
362 canvas->drawPath(path, paint);
363
364 paint.setColor(opaque ? kYellow : SK_ColorTRANSPARENT);
365 paint.setBlendMode(SkBlendMode::kSrc);
366 for (int i = 0; i < circles.count(); ++i) {
367 SkRect r = circles[i];
368 r.inset(r.width()/4, r.height()/4);
369 canvas->drawOval(r, paint);
370 }
371
372 return bm;
373}
374
Robert Phillips1c7062d2018-10-04 10:44:53 -0400375static void convert_rgba_to_yuva_601_shared(SkColor col, uint8_t yuv[4],
376 uint8_t off, uint8_t range) {
377 static const float Kr = 0.299f;
378 static const float Kb = 0.114f;
379 static const float Kg = 1.0f - Kr - Kb;
380
381 float r = SkColorGetR(col) / 255.0f;
382 float g = SkColorGetG(col) / 255.0f;
383 float b = SkColorGetB(col) / 255.0f;
384
385 float Ey = Kr * r + Kg * g + Kb * b;
386 float Ecb = (b - Ey) / 1.402f;
387 float Ecr = (r - Ey) / 1.772;
Robert Phillipsbb749902019-06-10 17:20:12 -0400388 SkASSERT(Ey >= 0.0f && Ey <= 1.0f);
389 SkASSERT(Ecb >= -0.5f && Ecb <= 0.5f);
390 SkASSERT(Ecr >= -0.5f && Ecr <= 0.5f);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400391
392 yuv[0] = SkScalarRoundToInt( range * Ey + off );
393 yuv[1] = SkScalarRoundToInt( 224 * Ecb + 128 );
394 yuv[2] = SkScalarRoundToInt( 224 * Ecr + 128 );
395 yuv[3] = SkColorGetA(col);
396}
397
398static void convert_rgba_to_yuva_jpeg(SkColor col, uint8_t yuv[4]) {
399 // full swing from 0..255
400 convert_rgba_to_yuva_601_shared(col, yuv, 0, 255);
401}
402
403static void convert_rgba_to_yuva_601(SkColor col, uint8_t yuv[4]) {
404 // partial swing from 16..235
405 convert_rgba_to_yuva_601_shared(col, yuv, 16, 219);
406
407}
408
409static void convert_rgba_to_yuva_709(SkColor col, uint8_t yuv[4]) {
410 static const float Kr = 0.2126f;
411 static const float Kb = 0.0722f;
412 static const float Kg = 1.0f - Kr - Kb;
413
414 float r = SkColorGetR(col) / 255.0f;
415 float g = SkColorGetG(col) / 255.0f;
416 float b = SkColorGetB(col) / 255.0f;
417
418 float Ey = Kr * r + Kg * g + Kb * b;
419 float Ecb = (b - Ey) / 1.8556f;
420 float Ecr = (r - Ey) / 1.5748;
Robert Phillipsbb749902019-06-10 17:20:12 -0400421 SkASSERT(Ey >= 0.0f && Ey <= 1.0f);
422 SkASSERT(Ecb >= -0.5f && Ecb <= 0.5f);
423 SkASSERT(Ecr >= -0.5f && Ecr <= 0.5f);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400424
425 yuv[0] = SkScalarRoundToInt( 219 * Ey + 16 );
426 yuv[1] = SkScalarRoundToInt( 224 * Ecb + 128 );
427 yuv[2] = SkScalarRoundToInt( 224 * Ecr + 128 );
428
429 yuv[3] = SkColorGetA(col);
430}
431
432
433static 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 -0400434 uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.0f * y + 1.402f * v - 0.703749f * 255),
Robert Phillips1c7062d2018-10-04 10:44:53 -0400435 0, 255);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400436 uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.0f * y - (0.344136f * u) - (0.714136f * v) + 0.531211f * 255),
Robert Phillips1c7062d2018-10-04 10:44:53 -0400437 0, 255);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400438 uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.0f * y + 1.772f * u - 0.889475f * 255),
Robert Phillips1c7062d2018-10-04 10:44:53 -0400439 0, 255);
440
Robert Phillips2dd1b472019-03-21 09:00:20 -0400441 SkPMColor c = SkPremultiplyARGBInline(a, b, g, r);
442 return c;
Robert Phillips1c7062d2018-10-04 10:44:53 -0400443}
444
445static 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 -0400446 uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.164f * y + 1.596f * v - 0.87075f * 255), 0, 255);
447 uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.164f * y - (0.391f * u) - (0.813f * v) + 0.52925f * 255), 0, 255);
448 uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.164f * y + 2.018f * u - 1.08175f * 255), 0, 255);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400449
Robert Phillips2dd1b472019-03-21 09:00:20 -0400450 SkPMColor c = SkPremultiplyARGBInline(a, b, g, r);
451 return c;
Robert Phillips1c7062d2018-10-04 10:44:53 -0400452}
453
454static 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 -0400455 uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.164f * y + (1.793f * v) - 0.96925f * 255), 0, 255);
456 uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.164f * y - (0.213f * u) - (0.533f * v) + 0.30025f * 255), 0, 255);
457 uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.164f * y + (2.112f * u) - 1.12875f * 255), 0, 255);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400458
Robert Phillips2dd1b472019-03-21 09:00:20 -0400459 SkPMColor c = SkPremultiplyARGBInline(a, b, g, r);
460 return c;
Robert Phillips1c7062d2018-10-04 10:44:53 -0400461}
462
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400463static void extract_planes(const SkBitmap& bm, SkYUVColorSpace yuvColorSpace, PlaneData* planes) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500464 if (kIdentity_SkYUVColorSpace == yuvColorSpace) {
465 // To test the identity color space we use JPEG YUV planes
466 yuvColorSpace = kJPEG_SkYUVColorSpace;
467 }
468
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400469 SkASSERT(!(bm.width() % 2));
470 SkASSERT(!(bm.height() % 2));
471
472 planes->fYFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
473 planes->fUFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
474 planes->fVFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
475 planes->fAFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
476 planes->fUQuarter.allocPixels(SkImageInfo::MakeA8(bm.width()/2, bm.height()/2));
477 planes->fVQuarter.allocPixels(SkImageInfo::MakeA8(bm.width()/2, bm.height()/2));
478
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400479 for (int y = 0; y < bm.height(); ++y) {
480 for (int x = 0; x < bm.width(); ++x) {
481 SkColor col = bm.getColor(x, y);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400482
Robert Phillips1c7062d2018-10-04 10:44:53 -0400483 uint8_t yuva[4];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400484
Robert Phillips1c7062d2018-10-04 10:44:53 -0400485 if (kJPEG_SkYUVColorSpace == yuvColorSpace) {
486 convert_rgba_to_yuva_jpeg(col, yuva);
487 } else if (kRec601_SkYUVColorSpace == yuvColorSpace) {
488 convert_rgba_to_yuva_601(col, yuva);
489 } else {
490 SkASSERT(kRec709_SkYUVColorSpace == yuvColorSpace);
491 convert_rgba_to_yuva_709(col, yuva);
492 }
493
494 *planes->fYFull.getAddr8(x, y) = yuva[0];
495 *planes->fUFull.getAddr8(x, y) = yuva[1];
496 *planes->fVFull.getAddr8(x, y) = yuva[2];
497 *planes->fAFull.getAddr8(x, y) = yuva[3];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400498 }
499 }
500
501 for (int y = 0; y < bm.height()/2; ++y) {
502 for (int x = 0; x < bm.width()/2; ++x) {
503 uint32_t uAccum = 0, vAccum = 0;
504
505 uAccum += *planes->fUFull.getAddr8(2*x, 2*y);
506 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y);
507 uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1);
508 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1);
509
510 *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f;
511
512 vAccum += *planes->fVFull.getAddr8(2*x, 2*y);
513 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y);
514 vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1);
515 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1);
516
Robert Phillips1c7062d2018-10-04 10:44:53 -0400517 *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400518 }
519 }
520}
521
Robert Phillipsbb749902019-06-10 17:20:12 -0400522// Create a 2x2 downsampled SkBitmap. It is stored in an RGBA texture. It can optionally be
523// uv (i.e., for P016, P010 and NV12) or vu (i.e., NV21).
524static SkBitmap make_quarter_2_channel(const SkBitmap& fullY,
525 const SkBitmap& quarterU,
526 const SkBitmap& quarterV,
527 bool uv) {
528 SkBitmap result;
529
530 // There isn't a RG color type. Approx w/ RGBA.
531 result.allocPixels(SkImageInfo::Make(fullY.width()/2,
532 fullY.height()/2,
533 kRGBA_8888_SkColorType,
534 kUnpremul_SkAlphaType));
535
536 for (int y = 0; y < fullY.height()/2; ++y) {
537 for (int x = 0; x < fullY.width()/2; ++x) {
538 uint8_t u8 = *quarterU.getAddr8(x, y);
539 uint8_t v8 = *quarterV.getAddr8(x, y);
540
541 if (uv) {
542 // NOT premul!
543 // U and 0 swapped to match RGBA layout
544 *result.getAddr32(x, y) = SkColorSetARGB(0xFF, 0, v8, u8);
545 } else {
546 // NOT premul!
547 // V and 0 swapped to match RGBA layout
548 *result.getAddr32(x, y) = SkColorSetARGB(0xFF, 0, u8, v8);
549 }
550 }
551 }
552
553 return result;
554}
555
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400556// Recombine the separate planes into some YUV format
557static void create_YUV(const PlaneData& planes, YUVFormat yuvFormat,
558 SkBitmap resultBMs[], SkYUVAIndex yuvaIndices[4], bool opaque) {
559 int nextLayer = 0;
560
561 switch (yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400562 case kY416_YUVFormat: {
563 // Although this is 16 bpp, store the data in an 8 bpp SkBitmap
564 SkBitmap yuvaFull;
565
566 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
567 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
568
569 for (int y = 0; y < planes.fYFull.height(); ++y) {
570 for (int x = 0; x < planes.fYFull.width(); ++x) {
571
572 uint8_t Y = *planes.fYFull.getAddr8(x, y);
573 uint8_t U = *planes.fUFull.getAddr8(x, y);
574 uint8_t V = *planes.fVFull.getAddr8(x, y);
575 uint8_t A = *planes.fAFull.getAddr8(x, y);
576
577 // NOT premul!
578 // U and V swapped to match RGBA layout
579 SkColor c = SkColorSetARGB(A, U, Y, V);
580 *yuvaFull.getAddr32(x, y) = c;
581 }
582 }
583
584 resultBMs[nextLayer++] = yuvaFull;
585
586 setup_yuv_indices(yuvFormat, false, yuvaIndices);
587 break;
588 }
Jim Van Verth976a6b02018-10-17 15:27:19 -0400589 case kAYUV_YUVFormat: {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400590 SkBitmap yuvaFull;
591
Jim Van Verth47133fd2018-10-19 22:09:28 -0400592 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
593 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400594
595 for (int y = 0; y < planes.fYFull.height(); ++y) {
596 for (int x = 0; x < planes.fYFull.width(); ++x) {
597
598 uint8_t Y = *planes.fYFull.getAddr8(x, y);
599 uint8_t U = *planes.fUFull.getAddr8(x, y);
600 uint8_t V = *planes.fVFull.getAddr8(x, y);
601 uint8_t A = *planes.fAFull.getAddr8(x, y);
602
603 // NOT premul!
Jim Van Verth47133fd2018-10-19 22:09:28 -0400604 // V and Y swapped to match RGBA layout
Robert Phillips2dd1b472019-03-21 09:00:20 -0400605 SkColor c = SkColorSetARGB(A, V, U, Y);
606 *yuvaFull.getAddr32(x, y) = c;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400607 }
608 }
609
610 resultBMs[nextLayer++] = yuvaFull;
611
Robert Phillipsdc62b642019-05-16 10:10:45 -0400612 setup_yuv_indices(yuvFormat, false, yuvaIndices);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400613 break;
614 }
Robert Phillips2dd1b472019-03-21 09:00:20 -0400615 case kY410_YUVFormat: {
616 SkBitmap yuvaFull;
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400617 uint32_t Y, U, V;
618 uint8_t A;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400619
620 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
621 kRGBA_1010102_SkColorType,
622 kUnpremul_SkAlphaType));
623
624 for (int y = 0; y < planes.fYFull.height(); ++y) {
625 for (int x = 0; x < planes.fYFull.width(); ++x) {
626
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400627 Y = SkScalarRoundToInt((*planes.fYFull.getAddr8(x, y) / 255.0f) * 1023.0f);
628 U = SkScalarRoundToInt((*planes.fUFull.getAddr8(x, y) / 255.0f) * 1023.0f);
629 V = SkScalarRoundToInt((*planes.fVFull.getAddr8(x, y) / 255.0f) * 1023.0f);
630 A = SkScalarRoundToInt((*planes.fAFull.getAddr8(x, y) / 255.0f) * 3.0f);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400631
632 // NOT premul!
633 // AVYU but w/ V and U swapped to match RGBA layout
634 *yuvaFull.getAddr32(x, y) = (A << 30) | (U << 20) | (Y << 10) | (V << 0);
635 }
636 }
637
638 resultBMs[nextLayer++] = yuvaFull;
639
Robert Phillipsdc62b642019-05-16 10:10:45 -0400640 setup_yuv_indices(yuvFormat, false, yuvaIndices);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400641 break;
642 }
Robert Phillipsbb749902019-06-10 17:20:12 -0400643 case kP016_YUVFormat: // fall through
644 case kP010_YUVFormat: // fall through
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400645 case kNV12_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400646 SkBitmap uvQuarter = make_quarter_2_channel(planes.fYFull,
647 planes.fUQuarter,
648 planes.fVQuarter, true);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400649 resultBMs[nextLayer++] = planes.fYFull;
650 resultBMs[nextLayer++] = uvQuarter;
651
Robert Phillipsdc62b642019-05-16 10:10:45 -0400652 setup_yuv_indices(yuvFormat, !opaque, yuvaIndices);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400653 break;
654 }
655 case kNV21_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400656 SkBitmap vuQuarter = make_quarter_2_channel(planes.fYFull,
657 planes.fUQuarter,
658 planes.fVQuarter, false);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400659 resultBMs[nextLayer++] = planes.fYFull;
660 resultBMs[nextLayer++] = vuQuarter;
661
Robert Phillipsdc62b642019-05-16 10:10:45 -0400662 setup_yuv_indices(yuvFormat, !opaque, yuvaIndices);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400663 break;
664 }
665 case kI420_YUVFormat:
666 resultBMs[nextLayer++] = planes.fYFull;
667 resultBMs[nextLayer++] = planes.fUQuarter;
668 resultBMs[nextLayer++] = planes.fVQuarter;
669
Robert Phillipsdc62b642019-05-16 10:10:45 -0400670 setup_yuv_indices(yuvFormat, !opaque, yuvaIndices);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400671 break;
672 case kYV12_YUVFormat:
673 resultBMs[nextLayer++] = planes.fYFull;
674 resultBMs[nextLayer++] = planes.fVQuarter;
675 resultBMs[nextLayer++] = planes.fUQuarter;
676
Robert Phillipsdc62b642019-05-16 10:10:45 -0400677 setup_yuv_indices(yuvFormat, !opaque, yuvaIndices);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400678 break;
679 }
680
Robert Phillipsbb749902019-06-10 17:20:12 -0400681 if (!format_has_builtin_alpha(yuvFormat) && !opaque) {
Robert Phillipsdc62b642019-05-16 10:10:45 -0400682 resultBMs[nextLayer] = planes.fAFull;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400683 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400684}
685
Robert Phillips2dd1b472019-03-21 09:00:20 -0400686static uint8_t look_up(float x1, float y1, const SkBitmap& bm, SkColorChannel channel) {
Robert Phillips1c7062d2018-10-04 10:44:53 -0400687 uint8_t result;
688
Robert Phillips2dd1b472019-03-21 09:00:20 -0400689 SkASSERT(x1 > 0 && x1 < 1.0f);
690 SkASSERT(y1 > 0 && y1 < 1.0f);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400691 int x = SkScalarFloorToInt(x1 * bm.width());
692 int y = SkScalarFloorToInt(y1 * bm.height());
693
694 if (kAlpha_8_SkColorType == bm.colorType()) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400695 SkASSERT(SkColorChannel::kA == channel || SkColorChannel::kR == channel);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400696 result = *bm.getAddr8(x, y);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400697 } else if (kRGBA_8888_SkColorType == bm.colorType()) {
698 SkColor c = *bm.getAddr32(x, y);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400699
700 switch (channel) {
701 case SkColorChannel::kR:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400702 result = SkColorGetB(c);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400703 break;
704 case SkColorChannel::kG:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400705 result = SkColorGetG(c);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400706 break;
707 case SkColorChannel::kB:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400708 result = SkColorGetR(c);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400709 break;
710 case SkColorChannel::kA:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400711 result = SkColorGetA(c);
712 break;
713 }
714 } else {
715 SkASSERT(kRGBA_1010102_SkColorType == bm.colorType());
716
717 SkColor c = *bm.getAddr32(x, y);
718
719 switch (channel) {
720 case SkColorChannel::kR:
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400721 result = SkScalarRoundToInt(((c >> 0) & 0x3ff) * (255.0f/1023.0f));
Robert Phillips2dd1b472019-03-21 09:00:20 -0400722 break;
723 case SkColorChannel::kG:
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400724 result = SkScalarRoundToInt(((c >> 10) & 0x3ff) * (255.0f/1023.0f));
Robert Phillips2dd1b472019-03-21 09:00:20 -0400725 break;
726 case SkColorChannel::kB:
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400727 result = SkScalarRoundToInt(((c >> 20) & 0x3ff) * (255.0f/1023.0f));
Robert Phillips2dd1b472019-03-21 09:00:20 -0400728 break;
729 case SkColorChannel::kA:
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400730 result = SkScalarRoundToInt(((c >> 30) & 0x3) * (255.0f/3.0f));
Robert Phillips1c7062d2018-10-04 10:44:53 -0400731 break;
732 }
733 }
734
735 return result;
736}
737
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400738class YUVGenerator : public SkImageGenerator {
739public:
740 YUVGenerator(const SkImageInfo& ii,
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400741 SkYUVColorSpace yuvColorSpace,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400742 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
Jim Van Verthe24b5872018-10-29 16:26:02 -0400743 SkBitmap bitmaps[SkYUVASizeInfo::kMaxCount])
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400744 : SkImageGenerator(ii)
Robert Phillips2dd1b472019-03-21 09:00:20 -0400745 , fYUVColorSpace(yuvColorSpace)
746 , fAllA8(true) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400747 memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices));
748
Jim Van Verth8f11e432018-10-18 14:36:59 -0400749 SkAssertResult(SkYUVAIndex::AreValidIndices(fYUVAIndices, &fNumBitmaps));
Jim Van Verthe24b5872018-10-29 16:26:02 -0400750 SkASSERT(fNumBitmaps > 0 && fNumBitmaps <= SkYUVASizeInfo::kMaxCount);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400751
Jim Van Verth8f11e432018-10-18 14:36:59 -0400752 for (int i = 0; i < fNumBitmaps; ++i) {
753 fYUVBitmaps[i] = bitmaps[i];
Robert Phillips2dd1b472019-03-21 09:00:20 -0400754 if (kAlpha_8_SkColorType != fYUVBitmaps[i].colorType()) {
755 fAllA8 = false;
756 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400757 }
758 }
759
760protected:
761 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
762 const Options&) override {
763
764 if (kUnknown_SkColorType == fFlattened.colorType()) {
Robert Phillips2dd1b472019-03-21 09:00:20 -0400765 fFlattened.allocPixels(info);
766 SkASSERT(kPremul_SkAlphaType == info.alphaType());
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400767
768 for (int y = 0; y < info.height(); ++y) {
769 for (int x = 0; x < info.width(); ++x) {
770
Robert Phillips1c7062d2018-10-04 10:44:53 -0400771 float x1 = (x + 0.5f) / info.width();
772 float y1 = (y + 0.5f) / info.height();
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400773
Robert Phillips1c7062d2018-10-04 10:44:53 -0400774 uint8_t Y = look_up(x1, y1,
775 fYUVBitmaps[fYUVAIndices[0].fIndex],
776 fYUVAIndices[0].fChannel);
777
778 uint8_t U = look_up(x1, y1,
779 fYUVBitmaps[fYUVAIndices[1].fIndex],
780 fYUVAIndices[1].fChannel);
781
782
783 uint8_t V = look_up(x1, y1,
784 fYUVBitmaps[fYUVAIndices[2].fIndex],
785 fYUVAIndices[2].fChannel);
786
787 uint8_t A = 255;
788 if (fYUVAIndices[3].fIndex >= 0) {
789 A = look_up(x1, y1,
790 fYUVBitmaps[fYUVAIndices[3].fIndex],
791 fYUVAIndices[3].fChannel);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400792 }
793
794 // Making premul here.
Robert Phillips1c7062d2018-10-04 10:44:53 -0400795 switch (fYUVColorSpace) {
796 case kJPEG_SkYUVColorSpace:
797 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_jpeg(Y, U, V, A);
798 break;
799 case kRec601_SkYUVColorSpace:
800 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_601(Y, U, V, A);
801 break;
802 case kRec709_SkYUVColorSpace:
803 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_709(Y, U, V, A);
804 break;
Robert Phillips0a22ba82019-03-06 12:36:47 -0500805 case kIdentity_SkYUVColorSpace:
Robert Phillips2dd1b472019-03-21 09:00:20 -0400806 *fFlattened.getAddr32(x, y) = SkPremultiplyARGBInline(A, V, U, Y);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500807 break;
Robert Phillips1c7062d2018-10-04 10:44:53 -0400808 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400809 }
810 }
811 }
812
813 return fFlattened.readPixels(info, pixels, rowBytes, 0, 0);
814 }
815
Jim Van Verthe24b5872018-10-29 16:26:02 -0400816 bool onQueryYUVA8(SkYUVASizeInfo* size,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400817 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
818 SkYUVColorSpace* yuvColorSpace) const override {
819
Robert Phillips2dd1b472019-03-21 09:00:20 -0400820 // The onQueryYUVA8/onGetYUVA8Planes can only handle A8 planes
821 if (!fAllA8) {
822 return false;
823 }
824
Jim Van Verth8f11e432018-10-18 14:36:59 -0400825 memcpy(yuvaIndices, fYUVAIndices, sizeof(fYUVAIndices));
826 *yuvColorSpace = fYUVColorSpace;
827
828 int i = 0;
829 for ( ; i < fNumBitmaps; ++i) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400830 size->fSizes[i].fWidth = fYUVBitmaps[i].width();
831 size->fSizes[i].fHeight = fYUVBitmaps[i].height();
832 size->fWidthBytes[i] = fYUVBitmaps[i].rowBytes();
833 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400834 for ( ; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400835 size->fSizes[i].fWidth = 0;
836 size->fSizes[i].fHeight = 0;
837 size->fWidthBytes[i] = 0;
Jim Van Verthf99a6742018-10-18 16:13:18 +0000838 }
Jim Van Verth0c583af2018-10-18 10:31:59 -0400839
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400840 return true;
841 }
842
Jim Van Verthe24b5872018-10-29 16:26:02 -0400843 bool onGetYUVA8Planes(const SkYUVASizeInfo&, const SkYUVAIndex[SkYUVAIndex::kIndexCount],
844 void* planes[SkYUVASizeInfo::kMaxCount]) override {
Robert Phillips2dd1b472019-03-21 09:00:20 -0400845 SkASSERT(fAllA8);
Jim Van Verth8f11e432018-10-18 14:36:59 -0400846 for (int i = 0; i < fNumBitmaps; ++i) {
847 planes[i] = fYUVBitmaps[i].getPixels();
848 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400849 return true;
850 }
851
852private:
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400853 SkYUVColorSpace fYUVColorSpace;
Jim Van Verth8f11e432018-10-18 14:36:59 -0400854 SkYUVAIndex fYUVAIndices[SkYUVAIndex::kIndexCount];
855 int fNumBitmaps;
Jim Van Verthe24b5872018-10-29 16:26:02 -0400856 SkBitmap fYUVBitmaps[SkYUVASizeInfo::kMaxCount];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400857 SkBitmap fFlattened;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400858 bool fAllA8; // are all the SkBitmaps in "fYUVBitmaps" A8?
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400859
860};
861
862static sk_sp<SkImage> make_yuv_gen_image(const SkImageInfo& ii,
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400863 SkYUVColorSpace yuvColorSpace,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400864 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400865 SkBitmap bitmaps[]) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400866 std::unique_ptr<SkImageGenerator> gen(new YUVGenerator(ii, yuvColorSpace,
867 yuvaIndices, bitmaps));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400868
869 return SkImage::MakeFromGenerator(std::move(gen));
870}
871
872static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500873 static const char* kYUVColorSpaceNames[] = { "JPEG", "601", "709", "Identity" };
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400874 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace+1);
875
Mike Reed91919132019-01-02 12:21:01 -0500876 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500877 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500878 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400879
880 SkRect textRect;
881 SkString colLabel;
882
883 colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400884 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400885 int y = textRect.height();
886
Mike Reed91919132019-01-02 12:21:01 -0500887 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400888
889 colLabel.printf("%s", opaque ? "Opaque" : "Transparent");
Mike Reed71f5a0b2018-10-25 16:12:39 -0400890
Ben Wagner51e15a62019-05-07 15:38:46 -0400891 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400892 y += textRect.height();
893
Mike Reed91919132019-01-02 12:21:01 -0500894 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400895}
896
897static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400898 static const char* kYUVFormatNames[] = {
899 "P016", "P010", "Y416", "AYUV", "Y410", "NV12", "NV21", "I420", "YV12"
900 };
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400901 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat+1);
902
Mike Reed91919132019-01-02 12:21:01 -0500903 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500904 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500905 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400906
907 SkRect textRect;
908 SkString rowLabel;
909
910 rowLabel.printf("%s", kYUVFormatNames[yuvFormat]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400911 font.measureText(rowLabel.c_str(), rowLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400912 y += kTileWidthHeight/2 + textRect.height()/2;
913
Hal Canary89a644b2019-01-07 09:36:09 -0500914 canvas->drawString(rowLabel, 0, y, font, paint);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400915}
916
Robert Phillipsbb749902019-06-10 17:20:12 -0400917static void make_RG_88(const GrCaps* caps,
918 const SkBitmap& bm, YUVFormat yuvFormat,
919 SkAutoTMalloc<uint8_t>* pixels,
920 GrBackendFormat* format) {
921 SkASSERT(kNV12_YUVFormat == yuvFormat || kNV21_YUVFormat == yuvFormat);
922 SkASSERT(kRGBA_8888_SkColorType == bm.colorType()); // uv stored in rg
923
924 pixels->reset(2 * sizeof(uint8_t) * bm.width() * bm.height());
925 uint8_t* currPixel = pixels->get();
926 for (int y = 0; y < bm.height(); ++y) {
927 for (int x = 0; x < bm.width(); ++x) {
928 SkColor color = bm.getColor(x, y);
929 uint8_t u8 = SkColorGetR(color);
930 uint8_t v8 = SkColorGetG(color);
931
932 currPixel[0] = u8;
933 currPixel[1] = v8;
934 currPixel += 2;
935 }
936 }
937 *format = caps->getBackendFormatFromGrColorType(GrColorType::kRG_88,
938 GrSRGBEncoded::kNo);
939}
940
941static void make_RG_1616(const GrCaps* caps,
942 const SkBitmap& bm, YUVFormat yuvFormat,
943 SkAutoTMalloc<uint8_t>* pixels,
944 GrBackendFormat* format) {
945 SkASSERT(kP016_YUVFormat == yuvFormat || kP010_YUVFormat == yuvFormat);
946 SkASSERT(kRGBA_8888_SkColorType == bm.colorType()); // uv stored in rg
947
948 uint16_t u16, v16;
949 pixels->reset(2 * sizeof(uint16_t) * bm.width() * bm.height());
950 uint16_t* currPixel = (uint16_t*) pixels->get();
951 for (int y = 0; y < bm.height(); ++y) {
952 for (int x = 0; x < bm.width(); ++x) {
953 SkColor color = bm.getColor(x, y);
954
955 if (kP016_YUVFormat == yuvFormat) {
956 u16 = SkScalarRoundToInt((SkColorGetR(color) / 255.0f) * 65535.0f);
957 v16 = SkScalarRoundToInt((SkColorGetG(color) / 255.0f) * 65535.0f);
958 } else {
959 u16 = SkScalarRoundToInt((SkColorGetR(color) / 255.0f) * 1023.0f);
960 v16 = SkScalarRoundToInt((SkColorGetG(color) / 255.0f) * 1023.0f);
961 u16 <<= 6;
962 v16 <<= 6;
963 }
964
965 currPixel[0] = u16;
966 currPixel[1] = v16;
967 currPixel += 2;
968 }
969 }
970
971 *format = caps->getBackendFormatFromGrColorType(GrColorType::kRG_1616,
972 GrSRGBEncoded::kNo);
973}
974
975static void make_RGBA_16(const GrCaps* caps,
976 const SkBitmap& bm,
977 YUVFormat yuvFormat,
978 SkAutoTMalloc<uint8_t>* pixels,
979 GrBackendFormat* format) {
980 SkASSERT(kY416_YUVFormat == yuvFormat);
981 SkASSERT(kRGBA_8888_SkColorType == bm.colorType());
982
983 uint16_t y16, u16, v16, a16;
984 pixels->reset(4 * sizeof(uint16_t) * bm.width() * bm.height());
985 uint16_t* currPixel = (uint16_t*) pixels->get();
986 for (int y = 0; y < bm.height(); ++y) {
987 for (int x = 0; x < bm.width(); ++x) {
988 SkColor color = bm.getColor(x, y);
989
990 y16 = SkScalarRoundToInt((SkColorGetR(color) / 255.0f) * 65535.0f);
991 u16 = SkScalarRoundToInt((SkColorGetG(color) / 255.0f) * 65535.0f);
992 v16 = SkScalarRoundToInt((SkColorGetB(color) / 255.0f) * 65535.0f);
993 a16 = SkScalarRoundToInt((SkColorGetA(color) / 255.0f) * 65535.0f);
994
995 currPixel[0] = y16;
996 currPixel[1] = u16;
997 currPixel[2] = v16;
998 currPixel[3] = a16;
999 currPixel += 4;
1000 }
1001 }
1002
Robert Phillips6b973872019-06-20 11:30:05 -04001003 *format = caps->getBackendFormatFromGrColorType(GrColorType::kRGBA_16161616,
1004 GrSRGBEncoded::kNo);
Robert Phillipsbb749902019-06-10 17:20:12 -04001005 return;
1006}
1007
1008static void make_R_16(const GrCaps* caps,
1009 const SkBitmap& bm,
1010 YUVFormat yuvFormat,
1011 SkAutoTMalloc<uint8_t>* pixels,
1012 GrBackendFormat* format) {
1013 SkASSERT(kP016_YUVFormat == yuvFormat || kP010_YUVFormat == yuvFormat);
1014 SkASSERT(kAlpha_8_SkColorType == bm.colorType());
1015
1016 uint16_t y16;
1017 pixels->reset(sizeof(uint16_t) * bm.width() * bm.height());
1018 uint16_t* currPixel = (uint16_t*) pixels->get();
1019 for (int y = 0; y < bm.height(); ++y) {
1020 for (int x = 0; x < bm.width(); ++x) {
1021 uint8_t y8 = *bm.getAddr8(x, y);
1022
1023 if (kP016_YUVFormat == yuvFormat) {
1024 y16 = SkScalarRoundToInt((y8 / 255.0f) * 65535.0f);
1025 } else {
1026 y16 = SkScalarRoundToInt((y8 / 255.0f) * 1023.0f);
1027 y16 <<= 6;
1028 }
1029
1030 currPixel[0] = y16;
1031 currPixel += 1;
1032 }
1033 }
1034
1035 *format = caps->getBackendFormatFromGrColorType(GrColorType::kR_16, GrSRGBEncoded::kNo);
1036}
1037
Robert Phillipscb1adb42019-06-10 15:09:34 -04001038static GrBackendTexture create_yuva_texture(GrContext* context, const SkBitmap& bm,
Robert Phillipsbb749902019-06-10 17:20:12 -04001039 SkYUVAIndex yuvaIndices[4], int texIndex,
1040 YUVFormat yuvFormat) {
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001041 SkASSERT(texIndex >= 0 && texIndex <= 3);
1042 int channelCount = 0;
1043 for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
1044 if (yuvaIndices[i].fIndex == texIndex) {
1045 ++channelCount;
1046 }
1047 }
Robert Phillipsbb749902019-06-10 17:20:12 -04001048
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001049 GrBackendTexture tex;
Robert Phillipsbb749902019-06-10 17:20:12 -04001050
1051 if (format_uses_16_bpp(yuvFormat) || 2 == channelCount) {
1052 // Due to the limitations of SkPixmap these cases need to be handled separately
Robert Phillipscb1adb42019-06-10 15:09:34 -04001053 const GrCaps* caps = context->priv().caps();
1054 GrGpu* gpu = context->priv().getGpu();
1055
Robert Phillipsbb749902019-06-10 17:20:12 -04001056 SkAutoTMalloc<uint8_t> pixels;
1057 GrBackendFormat format;
1058
1059 if (2 == channelCount) {
1060 if (format_uses_16_bpp(yuvFormat)) {
1061 make_RG_1616(caps, bm, yuvFormat, &pixels, &format);
1062 } else {
1063 make_RG_88(caps, bm, yuvFormat, &pixels, &format);
1064 }
1065 } else {
1066 if (kRGBA_8888_SkColorType == bm.colorType()) {
1067 make_RGBA_16(caps, bm, yuvFormat, &pixels, &format);
1068 } else {
1069 make_R_16(caps, bm, yuvFormat, &pixels, &format);
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001070 }
1071 }
Robert Phillipsbb749902019-06-10 17:20:12 -04001072
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001073 tex = gpu->createBackendTexture(bm.width(), bm.height(), format,
1074 GrMipMapped::kNo, GrRenderable::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001075 pixels, 0, nullptr, GrProtected::kNo);
Robert Phillipsbb749902019-06-10 17:20:12 -04001076 } else {
Robert Phillipscb1adb42019-06-10 15:09:34 -04001077 tex = context->priv().createBackendTexture(&bm.pixmap(), 1, GrRenderable::kNo);
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001078 }
Robert Phillipsbb749902019-06-10 17:20:12 -04001079
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001080 return tex;
1081}
1082
Robert Phillips0a22ba82019-03-06 12:36:47 -05001083static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
1084 static const float kJPEGConversionMatrix[20] = {
Mike Reede869a1e2019-04-30 12:18:54 -04001085 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
1086 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
1087 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
Robert Phillips0a22ba82019-03-06 12:36:47 -05001088 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
1089 };
1090
Mike Reede869a1e2019-04-30 12:18:54 -04001091 return SkColorFilters::Matrix(kJPEGConversionMatrix);
Robert Phillips0a22ba82019-03-06 12:36:47 -05001092}
1093
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001094namespace skiagm {
1095
1096// This GM creates an opaque and transparent bitmap, extracts the planes and then recombines
1097// them into various YUV formats. It then renders the results in the grid:
1098//
Robert Phillips2dd1b472019-03-21 09:00:20 -04001099// JPEG 601 709 Identity
1100// Transparent Opaque Transparent Opaque Transparent Opaque Transparent Opaque
Robert Phillipsbb749902019-06-10 17:20:12 -04001101// originals
1102// P016
1103// P010
1104// Y416
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001105// AYUV
Robert Phillips2dd1b472019-03-21 09:00:20 -04001106// Y410
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001107// NV12
1108// NV21
1109// I420
1110// YV12
1111class WackyYUVFormatsGM : public GM {
1112public:
Michael Ludwiga6a84002019-04-12 15:03:02 -04001113 WackyYUVFormatsGM(bool useTargetColorSpace, bool useDomain)
1114 : fUseTargetColorSpace(useTargetColorSpace)
1115 , fUseDomain(useDomain) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001116 this->setBGColor(0xFFCCCCCC);
1117 }
1118
1119protected:
1120
1121 SkString onShortName() override {
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001122 SkString name("wacky_yuv_formats");
1123 if (fUseTargetColorSpace) {
1124 name += "_cs";
1125 }
Michael Ludwiga6a84002019-04-12 15:03:02 -04001126 if (fUseDomain) {
1127 name += "_domain";
1128 }
Robert Phillips2dd1b472019-03-21 09:00:20 -04001129
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001130 return name;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001131 }
1132
1133 SkISize onISize() override {
1134 int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x color-space
1135 int numRows = 1 + (kLast_YUVFormat + 1); // origin + # yuv formats
Michael Ludwiga6a84002019-04-12 15:03:02 -04001136 int wh = SkScalarCeilToInt(kTileWidthHeight * (fUseDomain ? 1.5f : 1.f));
1137 return SkISize::Make(kLabelWidth + numCols * (wh + kPad),
1138 kLabelHeight + numRows * (wh + kPad));
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001139 }
1140
1141 void onOnceBeforeDraw() override {
1142 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
1143 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
1144 float innerRadius = 20.0f;
1145
1146 {
1147 // transparent
1148 SkTDArray<SkRect> circles;
1149 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001150 fOriginalBMs[0] = make_bitmap(kRGBA_8888_SkColorType, path, circles, false, fUseDomain);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001151 }
1152
1153 {
1154 // opaque
1155 SkTDArray<SkRect> circles;
1156 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001157 fOriginalBMs[1] = make_bitmap(kRGBA_8888_SkColorType, path, circles, true, fUseDomain);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001158 }
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001159
1160 if (fUseTargetColorSpace) {
1161 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
1162 }
Robert Phillips51c89e42018-10-05 13:30:43 -04001163 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001164
Robert Phillips51c89e42018-10-05 13:30:43 -04001165 void createImages(GrContext* context) {
Jim Van Verth9bf81202018-10-30 15:53:36 -04001166 int counter = 0;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001167 for (bool opaque : { false, true }) {
1168 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
1169 PlaneData planes;
1170 extract_planes(fOriginalBMs[opaque], (SkYUVColorSpace) cs, &planes);
1171
Robert Phillipsbb749902019-06-10 17:20:12 -04001172 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001173 SkBitmap resultBMs[4];
1174 SkYUVAIndex yuvaIndices[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001175
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001176 create_YUV(planes, (YUVFormat) format, resultBMs, yuvaIndices, opaque);
Robert Phillips2dd1b472019-03-21 09:00:20 -04001177
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001178 int numTextures;
1179 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) {
1180 continue;
1181 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001182
Robert Phillips51c89e42018-10-05 13:30:43 -04001183 if (context) {
Chris Dalton382b1222019-02-07 10:05:55 +00001184 if (context->abandoned()) {
1185 return;
1186 }
1187
Robert Phillips51c89e42018-10-05 13:30:43 -04001188 GrBackendTexture yuvaTextures[4];
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001189 SkPixmap yuvaPixmaps[4];
Robert Phillips51c89e42018-10-05 13:30:43 -04001190
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001191 for (int i = 0; i < numTextures; ++i) {
Robert Phillipscb1adb42019-06-10 15:09:34 -04001192 yuvaTextures[i] = create_yuva_texture(context, resultBMs[i],
Robert Phillipsbb749902019-06-10 17:20:12 -04001193 yuvaIndices, i,
1194 (YUVFormat) format);
Brian Salomonf2580c02019-01-30 09:13:31 -05001195 if (yuvaTextures[i].isValid()) {
1196 fBackendTextures.push_back(yuvaTextures[i]);
1197 }
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001198 yuvaPixmaps[i] = resultBMs[i].pixmap();
Robert Phillips51c89e42018-10-05 13:30:43 -04001199 }
1200
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001201 int counterMod = counter % 3;
Robert Phillipsbb749902019-06-10 17:20:12 -04001202 if (format_cant_be_represented_with_pixmaps((YUVFormat) format) &&
1203 counterMod == 2) {
1204 // These formats don't work as pixmaps
Michael Ludwiga6a84002019-04-12 15:03:02 -04001205 counterMod = 1;
1206 } else if (fUseDomain && counterMod == 0) {
1207 // Copies flatten to RGB when they copy the YUVA data, which doesn't
1208 // know about the intended domain and the domain padding bleeds in
1209 counterMod = 1;
1210 }
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001211 switch (counterMod) {
1212 case 0:
Jim Van Verth9bf81202018-10-30 15:53:36 -04001213 fImages[opaque][cs][format] = SkImage::MakeFromYUVATexturesCopy(
1214 context,
1215 (SkYUVColorSpace)cs,
1216 yuvaTextures,
1217 yuvaIndices,
1218 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1219 kTopLeft_GrSurfaceOrigin);
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001220 break;
1221 case 1:
Jim Van Verth9bf81202018-10-30 15:53:36 -04001222 fImages[opaque][cs][format] = SkImage::MakeFromYUVATextures(
1223 context,
1224 (SkYUVColorSpace)cs,
1225 yuvaTextures,
1226 yuvaIndices,
1227 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1228 kTopLeft_GrSurfaceOrigin);
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001229 break;
1230 case 2:
1231 default:
1232 fImages[opaque][cs][format] = SkImage::MakeFromYUVAPixmaps(
1233 context,
1234 (SkYUVColorSpace)cs,
1235 yuvaPixmaps,
1236 yuvaIndices,
1237 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1238 kTopLeft_GrSurfaceOrigin, true);
1239 break;
Jim Van Verth9bf81202018-10-30 15:53:36 -04001240 }
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001241 ++counter;
Jim Van Verth9bf81202018-10-30 15:53:36 -04001242 } else {
Robert Phillips51c89e42018-10-05 13:30:43 -04001243 fImages[opaque][cs][format] = make_yuv_gen_image(
1244 fOriginalBMs[opaque].info(),
Robert Phillips51c89e42018-10-05 13:30:43 -04001245 (SkYUVColorSpace) cs,
1246 yuvaIndices,
1247 resultBMs);
1248 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001249 }
1250 }
1251 }
1252 }
1253
1254 void onDraw(SkCanvas* canvas) override {
Robert Phillips51c89e42018-10-05 13:30:43 -04001255 this->createImages(canvas->getGrContext());
1256
Michael Ludwiga6a84002019-04-12 15:03:02 -04001257 SkRect srcRect = SkRect::MakeWH(fOriginalBMs[0].width(), fOriginalBMs[0].height());
1258 SkRect dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, srcRect.width(), srcRect.height());
1259
1260 SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint;
1261 if (fUseDomain) {
1262 srcRect.inset(kDomainPadding, kDomainPadding);
1263 // Draw a larger rectangle to ensure bilerp filtering would normally read outside the
1264 // srcRect and hit the red pixels, if strict constraint weren't used.
1265 dstRect.fRight = kLabelWidth + 1.5f * srcRect.width();
1266 dstRect.fBottom = 1.5f * srcRect.height();
1267 constraint = SkCanvas::kStrict_SrcRectConstraint;
1268 }
1269
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001270 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
Robert Phillips0a22ba82019-03-06 12:36:47 -05001271 SkPaint paint;
Michael Ludwiga6a84002019-04-12 15:03:02 -04001272 paint.setFilterQuality(kLow_SkFilterQuality);
Robert Phillips0a22ba82019-03-06 12:36:47 -05001273 if (kIdentity_SkYUVColorSpace == cs) {
1274 // The identity color space needs post processing to appear correctly
1275 paint.setColorFilter(yuv_to_rgb_colorfilter());
1276 }
1277
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001278 for (int opaque : { 0, 1 }) {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001279 dstRect.offsetTo(dstRect.fLeft, kLabelHeight);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001280
Michael Ludwiga6a84002019-04-12 15:03:02 -04001281 draw_col_label(canvas, dstRect.fLeft + dstRect.height() / 2, cs, opaque);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001282
Michael Ludwiga6a84002019-04-12 15:03:02 -04001283 canvas->drawBitmapRect(fOriginalBMs[opaque], srcRect, dstRect, nullptr, constraint);
1284 dstRect.offset(0.f, dstRect.height() + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001285
Robert Phillipsbb749902019-06-10 17:20:12 -04001286 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001287 draw_row_label(canvas, dstRect.fTop, format);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001288 if (fUseTargetColorSpace && fImages[opaque][cs][format]) {
Robert Phillips0a22ba82019-03-06 12:36:47 -05001289 // Making a CS-specific version of a kIdentity_SkYUVColorSpace YUV image
1290 // doesn't make a whole lot of sense. The colorSpace conversion will
1291 // operate on the YUV components rather than the RGB components.
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001292 sk_sp<SkImage> csImage =
1293 fImages[opaque][cs][format]->makeColorSpace(fTargetColorSpace);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001294 canvas->drawImageRect(csImage, srcRect, dstRect, &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001295 } else {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001296 canvas->drawImageRect(fImages[opaque][cs][format], srcRect, dstRect, &paint,
1297 constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001298 }
Michael Ludwiga6a84002019-04-12 15:03:02 -04001299 dstRect.offset(0.f, dstRect.height() + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001300 }
1301
Michael Ludwiga6a84002019-04-12 15:03:02 -04001302 dstRect.offset(dstRect.width() + kPad, 0.f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001303 }
1304 }
Brian Salomon8f388ce2019-01-29 14:50:53 -05001305 if (auto context = canvas->getGrContext()) {
Chris Dalton382b1222019-02-07 10:05:55 +00001306 if (!context->abandoned()) {
1307 context->flush();
1308 GrGpu* gpu = context->priv().getGpu();
1309 SkASSERT(gpu);
1310 gpu->testingOnly_flushGpuAndSync();
1311 for (const auto& tex : fBackendTextures) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001312 context->deleteBackendTexture(tex);
Chris Dalton382b1222019-02-07 10:05:55 +00001313 }
1314 fBackendTextures.reset();
Brian Salomon8f388ce2019-01-29 14:50:53 -05001315 }
Brian Salomon8f388ce2019-01-29 14:50:53 -05001316 }
1317 SkASSERT(!fBackendTextures.count());
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001318 }
1319
1320private:
Robert Phillips2dd1b472019-03-21 09:00:20 -04001321 SkBitmap fOriginalBMs[2];
1322 sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace + 1][kLast_YUVFormat + 1];
Brian Salomon8f388ce2019-01-29 14:50:53 -05001323 SkTArray<GrBackendTexture> fBackendTextures;
Robert Phillips2dd1b472019-03-21 09:00:20 -04001324 bool fUseTargetColorSpace;
Michael Ludwiga6a84002019-04-12 15:03:02 -04001325 bool fUseDomain;
Robert Phillips2dd1b472019-03-21 09:00:20 -04001326 sk_sp<SkColorSpace> fTargetColorSpace;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001327
1328 typedef GM INHERITED;
1329};
1330
1331//////////////////////////////////////////////////////////////////////////////
1332
Michael Ludwiga6a84002019-04-12 15:03:02 -04001333DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ false);)
1334DEF_GM(return new WackyYUVFormatsGM(/* cs */ true, /* domain */ false);)
1335DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ true);)
Brian Osmane9560492019-02-05 17:00:03 -05001336
Chris Dalton3a778372019-02-07 15:23:36 -07001337class YUVMakeColorSpaceGM : public GpuGM {
Brian Osmane9560492019-02-05 17:00:03 -05001338public:
1339 YUVMakeColorSpaceGM() {
1340 this->setBGColor(0xFFCCCCCC);
1341 }
1342
1343protected:
1344 SkString onShortName() override {
1345 return SkString("yuv_make_color_space");
1346 }
1347
1348 SkISize onISize() override {
1349 int numCols = 4; // (transparent, opaque) x (untagged, tagged)
1350 int numRows = 5; // original, YUV, subset, readPixels, makeNonTextureImage
1351 return SkISize::Make(numCols * (kTileWidthHeight + kPad) + kPad,
1352 numRows * (kTileWidthHeight + kPad) + kPad);
1353 }
1354
1355 void onOnceBeforeDraw() override {
1356 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
1357 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
1358 float innerRadius = 20.0f;
1359
1360 {
1361 // transparent
1362 SkTDArray<SkRect> circles;
1363 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001364 fOriginalBMs[0] = make_bitmap(kN32_SkColorType, path, circles, false, false);
Brian Osmane9560492019-02-05 17:00:03 -05001365 }
1366
1367 {
1368 // opaque
1369 SkTDArray<SkRect> circles;
1370 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001371 fOriginalBMs[1] = make_bitmap(kN32_SkColorType, path, circles, true, false);
Brian Osmane9560492019-02-05 17:00:03 -05001372 }
1373
1374 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
1375 }
1376
1377 void createImages(GrContext* context) {
1378 for (bool opaque : { false, true }) {
1379 PlaneData planes;
1380 extract_planes(fOriginalBMs[opaque], kJPEG_SkYUVColorSpace, &planes);
1381
1382 SkBitmap resultBMs[4];
1383 SkYUVAIndex yuvaIndices[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001384
Brian Osmane9560492019-02-05 17:00:03 -05001385 create_YUV(planes, kAYUV_YUVFormat, resultBMs, yuvaIndices, opaque);
Robert Phillips2dd1b472019-03-21 09:00:20 -04001386
Brian Osmane9560492019-02-05 17:00:03 -05001387 int numTextures;
1388 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) {
1389 continue;
1390 }
1391
Brian Osmane9560492019-02-05 17:00:03 -05001392 GrBackendTexture yuvaTextures[4];
1393 for (int i = 0; i < numTextures; ++i) {
Robert Phillipsbb749902019-06-10 17:20:12 -04001394 yuvaTextures[i] = create_yuva_texture(context, resultBMs[i], yuvaIndices, i,
1395 kAYUV_YUVFormat);
Brian Osmane9560492019-02-05 17:00:03 -05001396 if (yuvaTextures[i].isValid()) {
1397 fBackendTextures.push_back(yuvaTextures[i]);
1398 }
1399 }
1400
1401 fImages[opaque][0] = SkImage::MakeFromYUVATextures(
1402 context,
1403 kJPEG_SkYUVColorSpace,
1404 yuvaTextures,
1405 yuvaIndices,
1406 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1407 kTopLeft_GrSurfaceOrigin);
1408 fImages[opaque][1] = SkImage::MakeFromYUVATextures(
1409 context,
1410 kJPEG_SkYUVColorSpace,
1411 yuvaTextures,
1412 yuvaIndices,
1413 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1414 kTopLeft_GrSurfaceOrigin,
1415 SkColorSpace::MakeSRGB());
1416 }
1417 }
1418
Chris Dalton3a778372019-02-07 15:23:36 -07001419 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Brian Osmane9560492019-02-05 17:00:03 -05001420 this->createImages(context);
1421
1422 int x = kPad;
1423 for (int tagged : { 0, 1 }) {
1424 for (int opaque : { 0, 1 }) {
1425 int y = kPad;
1426
1427 auto raster = SkImage::MakeFromBitmap(fOriginalBMs[opaque])
1428 ->makeColorSpace(fTargetColorSpace);
1429 canvas->drawImage(raster, x, y);
1430 y += kTileWidthHeight + kPad;
1431
1432 auto yuv = fImages[opaque][tagged]->makeColorSpace(fTargetColorSpace);
1433 SkASSERT(SkColorSpace::Equals(yuv->colorSpace(), fTargetColorSpace.get()));
1434 canvas->drawImage(yuv, x, y);
1435 y += kTileWidthHeight + kPad;
1436
1437 auto subset = yuv->makeSubset(SkIRect::MakeWH(kTileWidthHeight / 2,
1438 kTileWidthHeight / 2));
1439 canvas->drawImage(subset, x, y);
1440 y += kTileWidthHeight + kPad;
1441
1442 auto nonTexture = yuv->makeNonTextureImage();
1443 canvas->drawImage(nonTexture, x, y);
1444 y += kTileWidthHeight + kPad;
1445
1446 SkBitmap readBack;
Brian Salomon5ad6fd32019-03-21 15:30:08 -04001447 readBack.allocPixels(yuv->imageInfo());
Brian Osmane9560492019-02-05 17:00:03 -05001448 yuv->readPixels(readBack.pixmap(), 0, 0);
1449 canvas->drawBitmap(readBack, x, y);
1450
1451 x += kTileWidthHeight + kPad;
1452 }
1453 }
1454
1455 context->flush();
1456 GrGpu* gpu = context->priv().getGpu();
1457 SkASSERT(gpu);
1458 gpu->testingOnly_flushGpuAndSync();
1459 for (const auto& tex : fBackendTextures) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001460 context->deleteBackendTexture(tex);
Brian Osmane9560492019-02-05 17:00:03 -05001461 }
1462 fBackendTextures.reset();
1463 }
1464
1465private:
1466 SkBitmap fOriginalBMs[2];
1467 sk_sp<SkImage> fImages[2][2];
1468 SkTArray<GrBackendTexture> fBackendTextures;
1469 sk_sp<SkColorSpace> fTargetColorSpace;
1470
1471 typedef GM INHERITED;
1472};
1473
1474DEF_GM(return new YUVMakeColorSpaceGM();)
1475
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001476}
Mike Reed6a5f7e22019-05-23 15:30:07 -04001477
1478///////////////
1479
Mike Reed6a5f7e22019-05-23 15:30:07 -04001480#include "include/effects/SkColorMatrix.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001481#include "src/core/SkAutoPixmapStorage.h"
1482#include "src/core/SkYUVMath.h"
1483#include "tools/Resources.h"
Mike Reed6a5f7e22019-05-23 15:30:07 -04001484
1485static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) {
1486 auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
1487 canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height());
1488 SkPaint paint;
1489 paint.setFilterQuality(kLow_SkFilterQuality);
1490 paint.setColorFilter(cf);
1491 paint.setBlendMode(SkBlendMode::kSrc);
1492 canvas->drawImage(img, 0, 0, &paint);
1493}
1494
1495static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) {
1496 float m[20];
1497 SkColorMatrix_RGB2YUV(cs, m);
1498
1499 memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A
1500 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]);
1501
1502 memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A
1503 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]);
1504
1505 memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A
1506 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]);
1507}
1508
1509static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y,
1510 const SkImage* a, const SkImage* b) {
1511 auto sh = SkShaders::Blend(SkBlendMode::kDifference, a->makeShader(), b->makeShader());
1512 SkPaint paint;
1513 paint.setShader(sh);
1514 canvas->save();
1515 canvas->translate(x, y);
1516 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1517
1518 SkColorMatrix cm;
1519 cm.setScale(64, 64, 64);
1520 paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm)));
1521 canvas->translate(0, a->height());
1522 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1523
1524 canvas->restore();
1525}
1526
1527// Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the
1528// resulting (recombined) images (gpu only for now).
1529//
1530class YUVSplitterGM : public skiagm::GM {
1531 sk_sp<SkImage> fOrig;
1532 SkAutoPixmapStorage fStorage[3];
1533 SkPixmap fPM[3];
1534
1535public:
1536 YUVSplitterGM() {}
1537
1538protected:
1539
1540 SkString onShortName() override {
1541 return SkString("yuv_splitter");
1542 }
1543
1544 SkISize onISize() override {
1545 return SkISize::Make(1024, 768);
1546 }
1547
1548 void onOnceBeforeDraw() override {
1549 fOrig = GetResourceAsImage("images/mandrill_256.png");
1550
1551 SkImageInfo info = SkImageInfo::Make(fOrig->width(), fOrig->height(), kAlpha_8_SkColorType,
1552 kPremul_SkAlphaType);
1553 fStorage[0].alloc(info);
1554 if (0) {
1555 // if you want to scale U,V down by 1/2
1556 info = info.makeWH(info.width()/2, info.height()/2);
1557 }
1558 fStorage[1].alloc(info);
1559 fStorage[2].alloc(info);
1560 for (int i = 0; i < 3; ++i) {
1561 fPM[i] = fStorage[i];
1562 }
1563 }
1564
1565 void onDraw(SkCanvas* canvas) override {
1566 SkYUVAIndex indices[4];
1567 indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
1568 indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
1569 indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR};
1570 indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kR};
1571
1572 canvas->translate(fOrig->width(), 0);
1573 canvas->save();
1574 for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace}) {
1575 split_into_yuv(fOrig.get(), cs, fPM);
1576 auto img = SkImage::MakeFromYUVAPixmaps(canvas->getGrContext(), cs, fPM, indices,
1577 fPM[0].info().dimensions(),
1578 kTopLeft_GrSurfaceOrigin,
1579 false, false, nullptr);
1580 if (img) {
1581 canvas->drawImage(img, 0, 0, nullptr);
1582 draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get());
1583 }
1584 canvas->translate(fOrig->width(), 0);
1585 }
1586 canvas->restore();
1587 canvas->translate(-fOrig->width(), 0);
1588
1589 canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0, nullptr);
1590 canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height(), nullptr);
1591 canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]),
1592 0, fPM[0].height() + fPM[1].height(), nullptr);
1593 }
1594
1595private:
1596 typedef GM INHERITED;
1597};
1598DEF_GM( return new YUVSplitterGM; )