blob: d7c968223711ebbbd371325c7fd3d7dbbf6a21aa [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,
Brian Salomon4b40a1d2019-07-10 10:12:14 -0400920 GrBackendFormat* format, size_t* rowBytes) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400921 SkASSERT(kNV12_YUVFormat == yuvFormat || kNV21_YUVFormat == yuvFormat);
922 SkASSERT(kRGBA_8888_SkColorType == bm.colorType()); // uv stored in rg
923
Brian Salomon4b40a1d2019-07-10 10:12:14 -0400924 *rowBytes = bm.width() * 2 * sizeof(uint8_t);
925 pixels->reset(*rowBytes * bm.height());
Robert Phillipsbb749902019-06-10 17:20:12 -0400926 uint8_t* currPixel = pixels->get();
927 for (int y = 0; y < bm.height(); ++y) {
928 for (int x = 0; x < bm.width(); ++x) {
929 SkColor color = bm.getColor(x, y);
930 uint8_t u8 = SkColorGetR(color);
931 uint8_t v8 = SkColorGetG(color);
932
933 currPixel[0] = u8;
934 currPixel[1] = v8;
935 currPixel += 2;
936 }
937 }
Greg Daniel627d0532019-07-08 16:48:14 -0400938 *format = caps->getBackendFormatFromColorType(GrColorType::kRG_88, GrSRGBEncoded::kNo);
Robert Phillipsbb749902019-06-10 17:20:12 -0400939}
940
941static void make_RG_1616(const GrCaps* caps,
942 const SkBitmap& bm, YUVFormat yuvFormat,
943 SkAutoTMalloc<uint8_t>* pixels,
Brian Salomon4b40a1d2019-07-10 10:12:14 -0400944 GrBackendFormat* format, size_t* rowBytes) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400945 SkASSERT(kP016_YUVFormat == yuvFormat || kP010_YUVFormat == yuvFormat);
946 SkASSERT(kRGBA_8888_SkColorType == bm.colorType()); // uv stored in rg
947
948 uint16_t u16, v16;
Brian Salomon4b40a1d2019-07-10 10:12:14 -0400949 *rowBytes = bm.width() * 2 * sizeof(uint16_t);
950 pixels->reset(*rowBytes * bm.height());
Robert Phillipsbb749902019-06-10 17:20:12 -0400951 uint16_t* currPixel = (uint16_t*) pixels->get();
952 for (int y = 0; y < bm.height(); ++y) {
953 for (int x = 0; x < bm.width(); ++x) {
954 SkColor color = bm.getColor(x, y);
955
956 if (kP016_YUVFormat == yuvFormat) {
957 u16 = SkScalarRoundToInt((SkColorGetR(color) / 255.0f) * 65535.0f);
958 v16 = SkScalarRoundToInt((SkColorGetG(color) / 255.0f) * 65535.0f);
959 } else {
960 u16 = SkScalarRoundToInt((SkColorGetR(color) / 255.0f) * 1023.0f);
961 v16 = SkScalarRoundToInt((SkColorGetG(color) / 255.0f) * 1023.0f);
962 u16 <<= 6;
963 v16 <<= 6;
964 }
965
966 currPixel[0] = u16;
967 currPixel[1] = v16;
968 currPixel += 2;
969 }
970 }
971
Greg Daniel627d0532019-07-08 16:48:14 -0400972 *format = caps->getBackendFormatFromColorType(GrColorType::kRG_1616, GrSRGBEncoded::kNo);
Robert Phillipsbb749902019-06-10 17:20:12 -0400973}
974
975static void make_RGBA_16(const GrCaps* caps,
976 const SkBitmap& bm,
977 YUVFormat yuvFormat,
978 SkAutoTMalloc<uint8_t>* pixels,
Brian Salomon4b40a1d2019-07-10 10:12:14 -0400979 GrBackendFormat* format,
980 size_t* rowBytes) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400981 SkASSERT(kY416_YUVFormat == yuvFormat);
982 SkASSERT(kRGBA_8888_SkColorType == bm.colorType());
983
984 uint16_t y16, u16, v16, a16;
Brian Salomon4b40a1d2019-07-10 10:12:14 -0400985 *rowBytes = 4 * sizeof(uint16_t) * bm.width();
986 pixels->reset(*rowBytes * bm.height());
Robert Phillipsbb749902019-06-10 17:20:12 -0400987 uint16_t* currPixel = (uint16_t*) pixels->get();
988 for (int y = 0; y < bm.height(); ++y) {
989 for (int x = 0; x < bm.width(); ++x) {
990 SkColor color = bm.getColor(x, y);
991
992 y16 = SkScalarRoundToInt((SkColorGetR(color) / 255.0f) * 65535.0f);
993 u16 = SkScalarRoundToInt((SkColorGetG(color) / 255.0f) * 65535.0f);
994 v16 = SkScalarRoundToInt((SkColorGetB(color) / 255.0f) * 65535.0f);
995 a16 = SkScalarRoundToInt((SkColorGetA(color) / 255.0f) * 65535.0f);
996
997 currPixel[0] = y16;
998 currPixel[1] = u16;
999 currPixel[2] = v16;
1000 currPixel[3] = a16;
1001 currPixel += 4;
1002 }
1003 }
1004
Greg Daniel627d0532019-07-08 16:48:14 -04001005 *format = caps->getBackendFormatFromColorType(GrColorType::kRGBA_16161616, GrSRGBEncoded::kNo);
Robert Phillipsbb749902019-06-10 17:20:12 -04001006 return;
1007}
1008
1009static void make_R_16(const GrCaps* caps,
1010 const SkBitmap& bm,
1011 YUVFormat yuvFormat,
1012 SkAutoTMalloc<uint8_t>* pixels,
Brian Salomon4b40a1d2019-07-10 10:12:14 -04001013 GrBackendFormat* format,
1014 size_t* rowBytes) {
Robert Phillipsbb749902019-06-10 17:20:12 -04001015 SkASSERT(kP016_YUVFormat == yuvFormat || kP010_YUVFormat == yuvFormat);
1016 SkASSERT(kAlpha_8_SkColorType == bm.colorType());
1017
1018 uint16_t y16;
Brian Salomon4b40a1d2019-07-10 10:12:14 -04001019 *rowBytes = sizeof(uint16_t) * bm.width();
1020 pixels->reset(*rowBytes * bm.height());
Robert Phillipsbb749902019-06-10 17:20:12 -04001021 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
Greg Daniel627d0532019-07-08 16:48:14 -04001038 *format = caps->getBackendFormatFromColorType(GrColorType::kR_16, GrSRGBEncoded::kNo);
Robert Phillipsbb749902019-06-10 17:20:12 -04001039}
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;
Brian Salomon4b40a1d2019-07-10 10:12:14 -04001061 size_t rowBytes;
Robert Phillipsbb749902019-06-10 17:20:12 -04001062
1063 if (2 == channelCount) {
1064 if (format_uses_16_bpp(yuvFormat)) {
Brian Salomon4b40a1d2019-07-10 10:12:14 -04001065 make_RG_1616(caps, bm, yuvFormat, &pixels, &format, &rowBytes);
Robert Phillipsbb749902019-06-10 17:20:12 -04001066 } else {
Brian Salomon4b40a1d2019-07-10 10:12:14 -04001067 make_RG_88(caps, bm, yuvFormat, &pixels, &format, &rowBytes);
Robert Phillipsbb749902019-06-10 17:20:12 -04001068 }
1069 } else {
1070 if (kRGBA_8888_SkColorType == bm.colorType()) {
Brian Salomon4b40a1d2019-07-10 10:12:14 -04001071 make_RGBA_16(caps, bm, yuvFormat, &pixels, &format, &rowBytes);
Robert Phillipsbb749902019-06-10 17:20:12 -04001072 } else {
Brian Salomon4b40a1d2019-07-10 10:12:14 -04001073 make_R_16(caps, bm, yuvFormat, &pixels, &format, &rowBytes);
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001074 }
1075 }
Robert Phillipsbb749902019-06-10 17:20:12 -04001076
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001077 tex = gpu->createBackendTexture(bm.width(), bm.height(), format,
1078 GrMipMapped::kNo, GrRenderable::kNo,
Brian Salomon4b40a1d2019-07-10 10:12:14 -04001079 pixels, rowBytes, nullptr, GrProtected::kNo);
Robert Phillipsbb749902019-06-10 17:20:12 -04001080 } else {
Robert Phillipsda2e67a2019-07-01 15:04:06 -04001081 tex = context->priv().createBackendTexture(&bm.pixmap(), 1,
1082 GrRenderable::kNo, GrProtected::kNo);
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001083 }
Robert Phillipsbb749902019-06-10 17:20:12 -04001084
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001085 return tex;
1086}
1087
Robert Phillips0a22ba82019-03-06 12:36:47 -05001088static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
1089 static const float kJPEGConversionMatrix[20] = {
Mike Reede869a1e2019-04-30 12:18:54 -04001090 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
1091 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
1092 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
Robert Phillips0a22ba82019-03-06 12:36:47 -05001093 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
1094 };
1095
Mike Reede869a1e2019-04-30 12:18:54 -04001096 return SkColorFilters::Matrix(kJPEGConversionMatrix);
Robert Phillips0a22ba82019-03-06 12:36:47 -05001097}
1098
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001099namespace skiagm {
1100
1101// This GM creates an opaque and transparent bitmap, extracts the planes and then recombines
1102// them into various YUV formats. It then renders the results in the grid:
1103//
Robert Phillips2dd1b472019-03-21 09:00:20 -04001104// JPEG 601 709 Identity
1105// Transparent Opaque Transparent Opaque Transparent Opaque Transparent Opaque
Robert Phillipsbb749902019-06-10 17:20:12 -04001106// originals
1107// P016
1108// P010
1109// Y416
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001110// AYUV
Robert Phillips2dd1b472019-03-21 09:00:20 -04001111// Y410
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001112// NV12
1113// NV21
1114// I420
1115// YV12
1116class WackyYUVFormatsGM : public GM {
1117public:
Michael Ludwiga6a84002019-04-12 15:03:02 -04001118 WackyYUVFormatsGM(bool useTargetColorSpace, bool useDomain)
1119 : fUseTargetColorSpace(useTargetColorSpace)
1120 , fUseDomain(useDomain) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001121 this->setBGColor(0xFFCCCCCC);
1122 }
1123
1124protected:
1125
1126 SkString onShortName() override {
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001127 SkString name("wacky_yuv_formats");
1128 if (fUseTargetColorSpace) {
1129 name += "_cs";
1130 }
Michael Ludwiga6a84002019-04-12 15:03:02 -04001131 if (fUseDomain) {
1132 name += "_domain";
1133 }
Robert Phillips2dd1b472019-03-21 09:00:20 -04001134
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001135 return name;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001136 }
1137
1138 SkISize onISize() override {
1139 int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x color-space
1140 int numRows = 1 + (kLast_YUVFormat + 1); // origin + # yuv formats
Michael Ludwiga6a84002019-04-12 15:03:02 -04001141 int wh = SkScalarCeilToInt(kTileWidthHeight * (fUseDomain ? 1.5f : 1.f));
1142 return SkISize::Make(kLabelWidth + numCols * (wh + kPad),
1143 kLabelHeight + numRows * (wh + kPad));
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001144 }
1145
1146 void onOnceBeforeDraw() override {
1147 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
1148 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
1149 float innerRadius = 20.0f;
1150
1151 {
1152 // transparent
1153 SkTDArray<SkRect> circles;
1154 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001155 fOriginalBMs[0] = make_bitmap(kRGBA_8888_SkColorType, path, circles, false, fUseDomain);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001156 }
1157
1158 {
1159 // opaque
1160 SkTDArray<SkRect> circles;
1161 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001162 fOriginalBMs[1] = make_bitmap(kRGBA_8888_SkColorType, path, circles, true, fUseDomain);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001163 }
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001164
1165 if (fUseTargetColorSpace) {
1166 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
1167 }
Robert Phillips51c89e42018-10-05 13:30:43 -04001168 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001169
Robert Phillips51c89e42018-10-05 13:30:43 -04001170 void createImages(GrContext* context) {
Jim Van Verth9bf81202018-10-30 15:53:36 -04001171 int counter = 0;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001172 for (bool opaque : { false, true }) {
1173 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
1174 PlaneData planes;
1175 extract_planes(fOriginalBMs[opaque], (SkYUVColorSpace) cs, &planes);
1176
Robert Phillipsbb749902019-06-10 17:20:12 -04001177 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001178 SkBitmap resultBMs[4];
1179 SkYUVAIndex yuvaIndices[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001180
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001181 create_YUV(planes, (YUVFormat) format, resultBMs, yuvaIndices, opaque);
Robert Phillips2dd1b472019-03-21 09:00:20 -04001182
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001183 int numTextures;
1184 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) {
1185 continue;
1186 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001187
Robert Phillips51c89e42018-10-05 13:30:43 -04001188 if (context) {
Chris Dalton382b1222019-02-07 10:05:55 +00001189 if (context->abandoned()) {
1190 return;
1191 }
1192
Robert Phillips51c89e42018-10-05 13:30:43 -04001193 GrBackendTexture yuvaTextures[4];
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001194 SkPixmap yuvaPixmaps[4];
Robert Phillips51c89e42018-10-05 13:30:43 -04001195
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001196 for (int i = 0; i < numTextures; ++i) {
Robert Phillipscb1adb42019-06-10 15:09:34 -04001197 yuvaTextures[i] = create_yuva_texture(context, resultBMs[i],
Robert Phillipsbb749902019-06-10 17:20:12 -04001198 yuvaIndices, i,
1199 (YUVFormat) format);
Brian Salomonf2580c02019-01-30 09:13:31 -05001200 if (yuvaTextures[i].isValid()) {
1201 fBackendTextures.push_back(yuvaTextures[i]);
1202 }
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001203 yuvaPixmaps[i] = resultBMs[i].pixmap();
Robert Phillips51c89e42018-10-05 13:30:43 -04001204 }
1205
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001206 int counterMod = counter % 3;
Robert Phillipsbb749902019-06-10 17:20:12 -04001207 if (format_cant_be_represented_with_pixmaps((YUVFormat) format) &&
1208 counterMod == 2) {
1209 // These formats don't work as pixmaps
Michael Ludwiga6a84002019-04-12 15:03:02 -04001210 counterMod = 1;
1211 } else if (fUseDomain && counterMod == 0) {
1212 // Copies flatten to RGB when they copy the YUVA data, which doesn't
1213 // know about the intended domain and the domain padding bleeds in
1214 counterMod = 1;
1215 }
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001216 switch (counterMod) {
1217 case 0:
Jim Van Verth9bf81202018-10-30 15:53:36 -04001218 fImages[opaque][cs][format] = SkImage::MakeFromYUVATexturesCopy(
1219 context,
1220 (SkYUVColorSpace)cs,
1221 yuvaTextures,
1222 yuvaIndices,
1223 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1224 kTopLeft_GrSurfaceOrigin);
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001225 break;
1226 case 1:
Jim Van Verth9bf81202018-10-30 15:53:36 -04001227 fImages[opaque][cs][format] = SkImage::MakeFromYUVATextures(
1228 context,
1229 (SkYUVColorSpace)cs,
1230 yuvaTextures,
1231 yuvaIndices,
1232 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1233 kTopLeft_GrSurfaceOrigin);
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001234 break;
1235 case 2:
1236 default:
1237 fImages[opaque][cs][format] = SkImage::MakeFromYUVAPixmaps(
1238 context,
1239 (SkYUVColorSpace)cs,
1240 yuvaPixmaps,
1241 yuvaIndices,
1242 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1243 kTopLeft_GrSurfaceOrigin, true);
1244 break;
Jim Van Verth9bf81202018-10-30 15:53:36 -04001245 }
Jim Van Verthc8429ad2018-11-20 11:12:37 -05001246 ++counter;
Jim Van Verth9bf81202018-10-30 15:53:36 -04001247 } else {
Robert Phillips51c89e42018-10-05 13:30:43 -04001248 fImages[opaque][cs][format] = make_yuv_gen_image(
1249 fOriginalBMs[opaque].info(),
Robert Phillips51c89e42018-10-05 13:30:43 -04001250 (SkYUVColorSpace) cs,
1251 yuvaIndices,
1252 resultBMs);
1253 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001254 }
1255 }
1256 }
1257 }
1258
1259 void onDraw(SkCanvas* canvas) override {
Robert Phillips51c89e42018-10-05 13:30:43 -04001260 this->createImages(canvas->getGrContext());
1261
Michael Ludwiga6a84002019-04-12 15:03:02 -04001262 SkRect srcRect = SkRect::MakeWH(fOriginalBMs[0].width(), fOriginalBMs[0].height());
1263 SkRect dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, srcRect.width(), srcRect.height());
1264
1265 SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint;
1266 if (fUseDomain) {
1267 srcRect.inset(kDomainPadding, kDomainPadding);
1268 // Draw a larger rectangle to ensure bilerp filtering would normally read outside the
1269 // srcRect and hit the red pixels, if strict constraint weren't used.
1270 dstRect.fRight = kLabelWidth + 1.5f * srcRect.width();
1271 dstRect.fBottom = 1.5f * srcRect.height();
1272 constraint = SkCanvas::kStrict_SrcRectConstraint;
1273 }
1274
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001275 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
Robert Phillips0a22ba82019-03-06 12:36:47 -05001276 SkPaint paint;
Michael Ludwiga6a84002019-04-12 15:03:02 -04001277 paint.setFilterQuality(kLow_SkFilterQuality);
Robert Phillips0a22ba82019-03-06 12:36:47 -05001278 if (kIdentity_SkYUVColorSpace == cs) {
1279 // The identity color space needs post processing to appear correctly
1280 paint.setColorFilter(yuv_to_rgb_colorfilter());
1281 }
1282
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001283 for (int opaque : { 0, 1 }) {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001284 dstRect.offsetTo(dstRect.fLeft, kLabelHeight);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001285
Michael Ludwiga6a84002019-04-12 15:03:02 -04001286 draw_col_label(canvas, dstRect.fLeft + dstRect.height() / 2, cs, opaque);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001287
Michael Ludwiga6a84002019-04-12 15:03:02 -04001288 canvas->drawBitmapRect(fOriginalBMs[opaque], srcRect, dstRect, nullptr, constraint);
1289 dstRect.offset(0.f, dstRect.height() + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001290
Robert Phillipsbb749902019-06-10 17:20:12 -04001291 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001292 draw_row_label(canvas, dstRect.fTop, format);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001293 if (fUseTargetColorSpace && fImages[opaque][cs][format]) {
Robert Phillips0a22ba82019-03-06 12:36:47 -05001294 // Making a CS-specific version of a kIdentity_SkYUVColorSpace YUV image
1295 // doesn't make a whole lot of sense. The colorSpace conversion will
1296 // operate on the YUV components rather than the RGB components.
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001297 sk_sp<SkImage> csImage =
1298 fImages[opaque][cs][format]->makeColorSpace(fTargetColorSpace);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001299 canvas->drawImageRect(csImage, srcRect, dstRect, &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001300 } else {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001301 canvas->drawImageRect(fImages[opaque][cs][format], srcRect, dstRect, &paint,
1302 constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001303 }
Michael Ludwiga6a84002019-04-12 15:03:02 -04001304 dstRect.offset(0.f, dstRect.height() + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001305 }
1306
Michael Ludwiga6a84002019-04-12 15:03:02 -04001307 dstRect.offset(dstRect.width() + kPad, 0.f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001308 }
1309 }
Brian Salomon8f388ce2019-01-29 14:50:53 -05001310 if (auto context = canvas->getGrContext()) {
Chris Dalton382b1222019-02-07 10:05:55 +00001311 if (!context->abandoned()) {
1312 context->flush();
1313 GrGpu* gpu = context->priv().getGpu();
1314 SkASSERT(gpu);
1315 gpu->testingOnly_flushGpuAndSync();
1316 for (const auto& tex : fBackendTextures) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001317 context->deleteBackendTexture(tex);
Chris Dalton382b1222019-02-07 10:05:55 +00001318 }
1319 fBackendTextures.reset();
Brian Salomon8f388ce2019-01-29 14:50:53 -05001320 }
Brian Salomon8f388ce2019-01-29 14:50:53 -05001321 }
1322 SkASSERT(!fBackendTextures.count());
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001323 }
1324
1325private:
Robert Phillips2dd1b472019-03-21 09:00:20 -04001326 SkBitmap fOriginalBMs[2];
1327 sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace + 1][kLast_YUVFormat + 1];
Brian Salomon8f388ce2019-01-29 14:50:53 -05001328 SkTArray<GrBackendTexture> fBackendTextures;
Robert Phillips2dd1b472019-03-21 09:00:20 -04001329 bool fUseTargetColorSpace;
Michael Ludwiga6a84002019-04-12 15:03:02 -04001330 bool fUseDomain;
Robert Phillips2dd1b472019-03-21 09:00:20 -04001331 sk_sp<SkColorSpace> fTargetColorSpace;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001332
1333 typedef GM INHERITED;
1334};
1335
1336//////////////////////////////////////////////////////////////////////////////
1337
Michael Ludwiga6a84002019-04-12 15:03:02 -04001338DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ false);)
1339DEF_GM(return new WackyYUVFormatsGM(/* cs */ true, /* domain */ false);)
1340DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ true);)
Brian Osmane9560492019-02-05 17:00:03 -05001341
Chris Dalton3a778372019-02-07 15:23:36 -07001342class YUVMakeColorSpaceGM : public GpuGM {
Brian Osmane9560492019-02-05 17:00:03 -05001343public:
1344 YUVMakeColorSpaceGM() {
1345 this->setBGColor(0xFFCCCCCC);
1346 }
1347
1348protected:
1349 SkString onShortName() override {
1350 return SkString("yuv_make_color_space");
1351 }
1352
1353 SkISize onISize() override {
1354 int numCols = 4; // (transparent, opaque) x (untagged, tagged)
1355 int numRows = 5; // original, YUV, subset, readPixels, makeNonTextureImage
1356 return SkISize::Make(numCols * (kTileWidthHeight + kPad) + kPad,
1357 numRows * (kTileWidthHeight + kPad) + kPad);
1358 }
1359
1360 void onOnceBeforeDraw() override {
1361 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
1362 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
1363 float innerRadius = 20.0f;
1364
1365 {
1366 // transparent
1367 SkTDArray<SkRect> circles;
1368 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001369 fOriginalBMs[0] = make_bitmap(kN32_SkColorType, path, circles, false, false);
Brian Osmane9560492019-02-05 17:00:03 -05001370 }
1371
1372 {
1373 // opaque
1374 SkTDArray<SkRect> circles;
1375 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001376 fOriginalBMs[1] = make_bitmap(kN32_SkColorType, path, circles, true, false);
Brian Osmane9560492019-02-05 17:00:03 -05001377 }
1378
1379 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
1380 }
1381
1382 void createImages(GrContext* context) {
1383 for (bool opaque : { false, true }) {
1384 PlaneData planes;
1385 extract_planes(fOriginalBMs[opaque], kJPEG_SkYUVColorSpace, &planes);
1386
1387 SkBitmap resultBMs[4];
1388 SkYUVAIndex yuvaIndices[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001389
Brian Osmane9560492019-02-05 17:00:03 -05001390 create_YUV(planes, kAYUV_YUVFormat, resultBMs, yuvaIndices, opaque);
Robert Phillips2dd1b472019-03-21 09:00:20 -04001391
Brian Osmane9560492019-02-05 17:00:03 -05001392 int numTextures;
1393 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) {
1394 continue;
1395 }
1396
Brian Osmane9560492019-02-05 17:00:03 -05001397 GrBackendTexture yuvaTextures[4];
1398 for (int i = 0; i < numTextures; ++i) {
Robert Phillipsbb749902019-06-10 17:20:12 -04001399 yuvaTextures[i] = create_yuva_texture(context, resultBMs[i], yuvaIndices, i,
1400 kAYUV_YUVFormat);
Brian Osmane9560492019-02-05 17:00:03 -05001401 if (yuvaTextures[i].isValid()) {
1402 fBackendTextures.push_back(yuvaTextures[i]);
1403 }
1404 }
1405
1406 fImages[opaque][0] = SkImage::MakeFromYUVATextures(
1407 context,
1408 kJPEG_SkYUVColorSpace,
1409 yuvaTextures,
1410 yuvaIndices,
1411 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1412 kTopLeft_GrSurfaceOrigin);
1413 fImages[opaque][1] = SkImage::MakeFromYUVATextures(
1414 context,
1415 kJPEG_SkYUVColorSpace,
1416 yuvaTextures,
1417 yuvaIndices,
1418 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1419 kTopLeft_GrSurfaceOrigin,
1420 SkColorSpace::MakeSRGB());
1421 }
1422 }
1423
Chris Dalton3a778372019-02-07 15:23:36 -07001424 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Brian Osmane9560492019-02-05 17:00:03 -05001425 this->createImages(context);
1426
1427 int x = kPad;
1428 for (int tagged : { 0, 1 }) {
1429 for (int opaque : { 0, 1 }) {
1430 int y = kPad;
1431
1432 auto raster = SkImage::MakeFromBitmap(fOriginalBMs[opaque])
1433 ->makeColorSpace(fTargetColorSpace);
1434 canvas->drawImage(raster, x, y);
1435 y += kTileWidthHeight + kPad;
1436
1437 auto yuv = fImages[opaque][tagged]->makeColorSpace(fTargetColorSpace);
1438 SkASSERT(SkColorSpace::Equals(yuv->colorSpace(), fTargetColorSpace.get()));
1439 canvas->drawImage(yuv, x, y);
1440 y += kTileWidthHeight + kPad;
1441
1442 auto subset = yuv->makeSubset(SkIRect::MakeWH(kTileWidthHeight / 2,
1443 kTileWidthHeight / 2));
1444 canvas->drawImage(subset, x, y);
1445 y += kTileWidthHeight + kPad;
1446
1447 auto nonTexture = yuv->makeNonTextureImage();
1448 canvas->drawImage(nonTexture, x, y);
1449 y += kTileWidthHeight + kPad;
1450
1451 SkBitmap readBack;
Brian Salomon5ad6fd32019-03-21 15:30:08 -04001452 readBack.allocPixels(yuv->imageInfo());
Brian Osmane9560492019-02-05 17:00:03 -05001453 yuv->readPixels(readBack.pixmap(), 0, 0);
1454 canvas->drawBitmap(readBack, x, y);
1455
1456 x += kTileWidthHeight + kPad;
1457 }
1458 }
1459
1460 context->flush();
1461 GrGpu* gpu = context->priv().getGpu();
1462 SkASSERT(gpu);
1463 gpu->testingOnly_flushGpuAndSync();
1464 for (const auto& tex : fBackendTextures) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -04001465 context->deleteBackendTexture(tex);
Brian Osmane9560492019-02-05 17:00:03 -05001466 }
1467 fBackendTextures.reset();
1468 }
1469
1470private:
1471 SkBitmap fOriginalBMs[2];
1472 sk_sp<SkImage> fImages[2][2];
1473 SkTArray<GrBackendTexture> fBackendTextures;
1474 sk_sp<SkColorSpace> fTargetColorSpace;
1475
1476 typedef GM INHERITED;
1477};
1478
1479DEF_GM(return new YUVMakeColorSpaceGM();)
1480
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001481}
Mike Reed6a5f7e22019-05-23 15:30:07 -04001482
1483///////////////
1484
Mike Reed6a5f7e22019-05-23 15:30:07 -04001485#include "include/effects/SkColorMatrix.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001486#include "src/core/SkAutoPixmapStorage.h"
1487#include "src/core/SkYUVMath.h"
1488#include "tools/Resources.h"
Mike Reed6a5f7e22019-05-23 15:30:07 -04001489
1490static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) {
1491 auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
1492 canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height());
1493 SkPaint paint;
1494 paint.setFilterQuality(kLow_SkFilterQuality);
1495 paint.setColorFilter(cf);
1496 paint.setBlendMode(SkBlendMode::kSrc);
1497 canvas->drawImage(img, 0, 0, &paint);
1498}
1499
1500static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) {
1501 float m[20];
1502 SkColorMatrix_RGB2YUV(cs, m);
1503
1504 memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A
1505 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]);
1506
1507 memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A
1508 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]);
1509
1510 memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A
1511 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]);
1512}
1513
1514static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y,
1515 const SkImage* a, const SkImage* b) {
1516 auto sh = SkShaders::Blend(SkBlendMode::kDifference, a->makeShader(), b->makeShader());
1517 SkPaint paint;
1518 paint.setShader(sh);
1519 canvas->save();
1520 canvas->translate(x, y);
1521 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1522
1523 SkColorMatrix cm;
1524 cm.setScale(64, 64, 64);
1525 paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm)));
1526 canvas->translate(0, a->height());
1527 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1528
1529 canvas->restore();
1530}
1531
1532// Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the
1533// resulting (recombined) images (gpu only for now).
1534//
1535class YUVSplitterGM : public skiagm::GM {
1536 sk_sp<SkImage> fOrig;
1537 SkAutoPixmapStorage fStorage[3];
1538 SkPixmap fPM[3];
1539
1540public:
1541 YUVSplitterGM() {}
1542
1543protected:
1544
1545 SkString onShortName() override {
1546 return SkString("yuv_splitter");
1547 }
1548
1549 SkISize onISize() override {
1550 return SkISize::Make(1024, 768);
1551 }
1552
1553 void onOnceBeforeDraw() override {
1554 fOrig = GetResourceAsImage("images/mandrill_256.png");
1555
1556 SkImageInfo info = SkImageInfo::Make(fOrig->width(), fOrig->height(), kAlpha_8_SkColorType,
1557 kPremul_SkAlphaType);
1558 fStorage[0].alloc(info);
1559 if (0) {
1560 // if you want to scale U,V down by 1/2
1561 info = info.makeWH(info.width()/2, info.height()/2);
1562 }
1563 fStorage[1].alloc(info);
1564 fStorage[2].alloc(info);
1565 for (int i = 0; i < 3; ++i) {
1566 fPM[i] = fStorage[i];
1567 }
1568 }
1569
1570 void onDraw(SkCanvas* canvas) override {
1571 SkYUVAIndex indices[4];
1572 indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
1573 indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
1574 indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR};
1575 indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kR};
1576
1577 canvas->translate(fOrig->width(), 0);
1578 canvas->save();
1579 for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace}) {
1580 split_into_yuv(fOrig.get(), cs, fPM);
1581 auto img = SkImage::MakeFromYUVAPixmaps(canvas->getGrContext(), cs, fPM, indices,
1582 fPM[0].info().dimensions(),
1583 kTopLeft_GrSurfaceOrigin,
1584 false, false, nullptr);
1585 if (img) {
1586 canvas->drawImage(img, 0, 0, nullptr);
1587 draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get());
1588 }
1589 canvas->translate(fOrig->width(), 0);
1590 }
1591 canvas->restore();
1592 canvas->translate(-fOrig->width(), 0);
1593
1594 canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0, nullptr);
1595 canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height(), nullptr);
1596 canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]),
1597 0, fPM[0].height() + fPM[1].height(), nullptr);
1598 }
1599
1600private:
1601 typedef GM INHERITED;
1602};
1603DEF_GM( return new YUVSplitterGM; )