blob: bd1a611338edc2a1642df2a49db0eff568763719 [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
8#include "gm.h"
9#include "sk_tool_utils.h"
10
11#include "SkColorPriv.h"
12#include "SkImageGenerator.h"
Robert Phillipsbfa76f22018-10-03 12:12:26 -040013#include "SkPath.h"
Mike Reed71f5a0b2018-10-25 16:12:39 -040014#include "SkTextUtils.h"
Robert Phillips66a97342018-10-04 09:10:29 -040015#include "SkYUVAIndex.h"
Robert Phillipsbfa76f22018-10-03 12:12:26 -040016
Robert Phillips51c89e42018-10-05 13:30:43 -040017#if SK_SUPPORT_GPU
18#include "GrBackendSurface.h"
19#include "GrContextPriv.h"
20#include "GrGpu.h"
Jim Van Verth9bf81202018-10-30 15:53:36 -040021#include "SkImage_GpuYUVA.h"
Robert Phillips51c89e42018-10-05 13:30:43 -040022#endif
23
Robert Phillipsbfa76f22018-10-03 12:12:26 -040024static const int kTileWidthHeight = 128;
25static const int kLabelWidth = 64;
26static const int kLabelHeight = 32;
27static const int kPad = 1;
Robert Phillipsbfa76f22018-10-03 12:12:26 -040028
29enum YUVFormat {
30 // 4:4:4 formats, 32 bpp
Jim Van Verth976a6b02018-10-17 15:27:19 -040031 kAYUV_YUVFormat, // 8-bit YUVA values all interleaved
Robert Phillipsbfa76f22018-10-03 12:12:26 -040032
33 // 4:2:0 formats, 12 bpp
34 kNV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled interleaved U/V planes
35 kNV21_YUVFormat, // same as kNV12 but w/ U/V reversed in the interleaved plane
36
37 kI420_YUVFormat, // 8-bit Y plane + 2x2 down sampled U and V planes
38 kYV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled V and U planes
39
40 kLast_YUVFormat = kYV12_YUVFormat
41};
42
43// All the planes we need to construct the various YUV formats
44struct PlaneData {
45 SkBitmap fYFull;
46 SkBitmap fUFull;
47 SkBitmap fVFull;
48 SkBitmap fAFull;
49 SkBitmap fUQuarter; // 2x2 downsampled U channel
50 SkBitmap fVQuarter; // 2x2 downsampled V channel
51};
52
53// Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle
54// and have tangents 'v1' and 'v2'.
55static void add_arc(SkPath* path,
56 const SkPoint& o1, const SkVector& v1,
57 const SkPoint& o2, const SkVector& v2,
58 SkTDArray<SkRect>* circles, bool takeLongWayRound) {
59
60 SkVector v3 = { -v1.fY, v1.fX };
61 SkVector v4 = { v2.fY, -v2.fX };
62
63 SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4);
64 SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY };
65
66 SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t };
67
68 if (circles) {
69 circles->push_back(r);
70 }
71
72 SkVector startV = o1 - center, endV = o2 - center;
73 startV.normalize();
74 endV.normalize();
75
76 SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX));
77 SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX));
78
79 startDeg += 360.0f;
80 startDeg = fmodf(startDeg, 360.0f);
81
82 endDeg += 360.0f;
83 endDeg = fmodf(endDeg, 360.0f);
84
85 if (endDeg < startDeg) {
86 endDeg += 360.0f;
87 }
88
89 SkScalar sweepDeg = SkTAbs(endDeg - startDeg);
90 if (!takeLongWayRound) {
91 sweepDeg = sweepDeg - 360;
92 }
93
94 path->arcTo(r, startDeg, sweepDeg, false);
95}
96
97static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius,
98 SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) {
99 if (numLobes <= 1) {
100 return SkPath();
101 }
102
103 SkPath p;
104
105 int numDivisions = 2 * numLobes;
106 SkScalar fullLobeDegrees = 360.0f / numLobes;
107 SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f);
108 SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f);
109 SkMatrix outerStep, innerStep;
110 outerStep.setRotate(outDegrees);
111 innerStep.setRotate(innerDegrees);
112 SkVector curV = SkVector::Make(0.0f, 1.0f);
113
114 if (circles) {
115 circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius,
116 o.fX + innerRadius, o.fY + innerRadius));
117 }
118
119 p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY);
120
121 for (int i = 0; i < numDivisions; ++i) {
122
123 SkVector nextV;
124 if (0 == (i % 2)) {
125 nextV = outerStep.mapVector(curV.fX, curV.fY);
126
127 SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX,
128 o.fY + outerRadius * curV.fY);
129 SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX,
130 o.fY + outerRadius * nextV.fY);
131
132 p.lineTo(top);
133 add_arc(&p, top, curV, nextTop, nextV, circles, true);
134 } else {
135 nextV = innerStep.mapVector(curV.fX, curV.fY);
136
137 SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX,
138 o.fY + innerRadius * curV.fY);
139 SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX,
140 o.fY + innerRadius * nextV.fY);
141
142 p.lineTo(bot);
143 add_arc(&p, bot, curV, nextBot, nextV, nullptr, false);
144 }
145
146 curV = nextV;
147 }
148
149 p.close();
150
151 return p;
152}
153
154static SkBitmap make_bitmap(const SkPath& path, const SkTDArray<SkRect>& circles, bool opaque) {
Robert Phillips94ade752018-10-09 12:32:31 -0400155 const SkColor kGreen = sk_tool_utils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104));
156 const SkColor kBlue = sk_tool_utils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252));
157 const SkColor kYellow = sk_tool_utils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400158
Robert Phillips1c7062d2018-10-04 10:44:53 -0400159 SkImageInfo ii = SkImageInfo::MakeN32(kTileWidthHeight, kTileWidthHeight, kPremul_SkAlphaType);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400160
161 SkBitmap bm;
162 bm.allocPixels(ii);
163
164 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirectN32(ii.width(), ii.height(),
165 (SkPMColor*)bm.getPixels(),
166 bm.rowBytes());
167
168 canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT);
169
170 SkPaint paint;
171 paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency
172 paint.setColor(kBlue);
173
174 canvas->drawPath(path, paint);
175
176 paint.setColor(opaque ? kYellow : SK_ColorTRANSPARENT);
177 paint.setBlendMode(SkBlendMode::kSrc);
178 for (int i = 0; i < circles.count(); ++i) {
179 SkRect r = circles[i];
180 r.inset(r.width()/4, r.height()/4);
181 canvas->drawOval(r, paint);
182 }
183
184 return bm;
185}
186
Robert Phillips1c7062d2018-10-04 10:44:53 -0400187static void convert_rgba_to_yuva_601_shared(SkColor col, uint8_t yuv[4],
188 uint8_t off, uint8_t range) {
189 static const float Kr = 0.299f;
190 static const float Kb = 0.114f;
191 static const float Kg = 1.0f - Kr - Kb;
192
193 float r = SkColorGetR(col) / 255.0f;
194 float g = SkColorGetG(col) / 255.0f;
195 float b = SkColorGetB(col) / 255.0f;
196
197 float Ey = Kr * r + Kg * g + Kb * b;
198 float Ecb = (b - Ey) / 1.402f;
199 float Ecr = (r - Ey) / 1.772;
200
201 yuv[0] = SkScalarRoundToInt( range * Ey + off );
202 yuv[1] = SkScalarRoundToInt( 224 * Ecb + 128 );
203 yuv[2] = SkScalarRoundToInt( 224 * Ecr + 128 );
204 yuv[3] = SkColorGetA(col);
205}
206
207static void convert_rgba_to_yuva_jpeg(SkColor col, uint8_t yuv[4]) {
208 // full swing from 0..255
209 convert_rgba_to_yuva_601_shared(col, yuv, 0, 255);
210}
211
212static void convert_rgba_to_yuva_601(SkColor col, uint8_t yuv[4]) {
213 // partial swing from 16..235
214 convert_rgba_to_yuva_601_shared(col, yuv, 16, 219);
215
216}
217
218static void convert_rgba_to_yuva_709(SkColor col, uint8_t yuv[4]) {
219 static const float Kr = 0.2126f;
220 static const float Kb = 0.0722f;
221 static const float Kg = 1.0f - Kr - Kb;
222
223 float r = SkColorGetR(col) / 255.0f;
224 float g = SkColorGetG(col) / 255.0f;
225 float b = SkColorGetB(col) / 255.0f;
226
227 float Ey = Kr * r + Kg * g + Kb * b;
228 float Ecb = (b - Ey) / 1.8556f;
229 float Ecr = (r - Ey) / 1.5748;
230
231 yuv[0] = SkScalarRoundToInt( 219 * Ey + 16 );
232 yuv[1] = SkScalarRoundToInt( 224 * Ecb + 128 );
233 yuv[2] = SkScalarRoundToInt( 224 * Ecr + 128 );
234
235 yuv[3] = SkColorGetA(col);
236}
237
238
239static SkPMColor convert_yuva_to_rgba_jpeg(uint8_t y, uint8_t u, uint8_t v, uint8_t a) {
240 int c = y;
241 int d = u - 128;
242 int e = v - 128;
243
244 uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.0f * c + 1.402f * e ),
245 0, 255);
246 uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.0f * c - (0.344136f * d) - (0.714136f * e)),
247 0, 255);
248 uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.0f * c + 1.773f * d ),
249 0, 255);
250
251 return SkPremultiplyARGBInline(a, r, g, b);
252}
253
254static SkPMColor convert_yuva_to_rgba_601(uint8_t y, uint8_t u, uint8_t v, uint8_t a) {
255 int c = y - 16;
256 int d = u - 128;
257 int e = v - 128;
258
259 uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.164f * c + 1.596f * e ), 0, 255);
260 uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.164f * c - (0.391f * d) - (0.813f * e)), 0, 255);
261 uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.164f * c + 2.018f * d ), 0, 255);
262
263 return SkPremultiplyARGBInline(a, r, g, b);
264}
265
266static SkPMColor convert_yuva_to_rgba_709(uint8_t y, uint8_t u, uint8_t v, uint8_t a) {
267 int c = y - 16;
268 int d = u - 128;
269 int e = v - 128;
270
271 uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.164f * c + 1.793f * e ), 0, 255);
272 uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.164f * c - (0.213f * d) - (0.533f * e)), 0, 255);
273 uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.164f * c + 2.112f * d ), 0, 255);
274
275 return SkPremultiplyARGBInline(a, r, g, b);
276}
277
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400278static void extract_planes(const SkBitmap& bm, SkYUVColorSpace yuvColorSpace, PlaneData* planes) {
279 SkASSERT(!(bm.width() % 2));
280 SkASSERT(!(bm.height() % 2));
281
282 planes->fYFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
283 planes->fUFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
284 planes->fVFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
285 planes->fAFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
286 planes->fUQuarter.allocPixels(SkImageInfo::MakeA8(bm.width()/2, bm.height()/2));
287 planes->fVQuarter.allocPixels(SkImageInfo::MakeA8(bm.width()/2, bm.height()/2));
288
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400289 for (int y = 0; y < bm.height(); ++y) {
290 for (int x = 0; x < bm.width(); ++x) {
291 SkColor col = bm.getColor(x, y);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400292
Robert Phillips1c7062d2018-10-04 10:44:53 -0400293 uint8_t yuva[4];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400294
Robert Phillips1c7062d2018-10-04 10:44:53 -0400295 if (kJPEG_SkYUVColorSpace == yuvColorSpace) {
296 convert_rgba_to_yuva_jpeg(col, yuva);
297 } else if (kRec601_SkYUVColorSpace == yuvColorSpace) {
298 convert_rgba_to_yuva_601(col, yuva);
299 } else {
300 SkASSERT(kRec709_SkYUVColorSpace == yuvColorSpace);
301 convert_rgba_to_yuva_709(col, yuva);
302 }
303
304 *planes->fYFull.getAddr8(x, y) = yuva[0];
305 *planes->fUFull.getAddr8(x, y) = yuva[1];
306 *planes->fVFull.getAddr8(x, y) = yuva[2];
307 *planes->fAFull.getAddr8(x, y) = yuva[3];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400308 }
309 }
310
311 for (int y = 0; y < bm.height()/2; ++y) {
312 for (int x = 0; x < bm.width()/2; ++x) {
313 uint32_t uAccum = 0, vAccum = 0;
314
315 uAccum += *planes->fUFull.getAddr8(2*x, 2*y);
316 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y);
317 uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1);
318 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1);
319
320 *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f;
321
322 vAccum += *planes->fVFull.getAddr8(2*x, 2*y);
323 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y);
324 vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1);
325 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1);
326
Robert Phillips1c7062d2018-10-04 10:44:53 -0400327 *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400328 }
329 }
330}
331
332// Recombine the separate planes into some YUV format
333static void create_YUV(const PlaneData& planes, YUVFormat yuvFormat,
334 SkBitmap resultBMs[], SkYUVAIndex yuvaIndices[4], bool opaque) {
335 int nextLayer = 0;
336
337 switch (yuvFormat) {
Jim Van Verth976a6b02018-10-17 15:27:19 -0400338 case kAYUV_YUVFormat: {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400339 SkBitmap yuvaFull;
340
Jim Van Verth47133fd2018-10-19 22:09:28 -0400341 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
342 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400343
344 for (int y = 0; y < planes.fYFull.height(); ++y) {
345 for (int x = 0; x < planes.fYFull.width(); ++x) {
346
347 uint8_t Y = *planes.fYFull.getAddr8(x, y);
348 uint8_t U = *planes.fUFull.getAddr8(x, y);
349 uint8_t V = *planes.fVFull.getAddr8(x, y);
350 uint8_t A = *planes.fAFull.getAddr8(x, y);
351
352 // NOT premul!
Jim Van Verth47133fd2018-10-19 22:09:28 -0400353 // V and Y swapped to match RGBA layout
354 *yuvaFull.getAddr32(x, y) = SkColorSetARGB(A, V, U, Y);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400355 }
356 }
357
358 resultBMs[nextLayer++] = yuvaFull;
359
360 yuvaIndices[0].fIndex = 0;
Robert Phillips66a97342018-10-04 09:10:29 -0400361 yuvaIndices[0].fChannel = SkColorChannel::kR;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400362 yuvaIndices[1].fIndex = 0;
Robert Phillips66a97342018-10-04 09:10:29 -0400363 yuvaIndices[1].fChannel = SkColorChannel::kG;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400364 yuvaIndices[2].fIndex = 0;
Robert Phillips1c7062d2018-10-04 10:44:53 -0400365 yuvaIndices[2].fChannel = SkColorChannel::kB;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400366 yuvaIndices[3].fIndex = 0;
Robert Phillips66a97342018-10-04 09:10:29 -0400367 yuvaIndices[3].fChannel = SkColorChannel::kA;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400368 break;
369 }
370 case kNV12_YUVFormat: {
371 SkBitmap uvQuarter;
372
Robert Phillips1c7062d2018-10-04 10:44:53 -0400373 // There isn't a RG color type. Approx w/ RGBA.
Jim Van Verth47133fd2018-10-19 22:09:28 -0400374 uvQuarter.allocPixels(SkImageInfo::Make(planes.fYFull.width()/2,
375 planes.fYFull.height()/2,
376 kRGBA_8888_SkColorType,
377 kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400378
379 for (int y = 0; y < planes.fYFull.height()/2; ++y) {
380 for (int x = 0; x < planes.fYFull.width()/2; ++x) {
Robert Phillips1c7062d2018-10-04 10:44:53 -0400381 uint8_t U = *planes.fUQuarter.getAddr8(x, y);
382 uint8_t V = *planes.fVQuarter.getAddr8(x, y);
383
384 // NOT premul!
Jim Van Verth47133fd2018-10-19 22:09:28 -0400385 // U and 0 swapped to match RGBA layout
386 *uvQuarter.getAddr32(x, y) = SkColorSetARGB(0, 0, V, U);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400387 }
388 }
389
390 resultBMs[nextLayer++] = planes.fYFull;
391 resultBMs[nextLayer++] = uvQuarter;
392
393 yuvaIndices[0].fIndex = 0;
Robert Phillips66a97342018-10-04 09:10:29 -0400394 yuvaIndices[0].fChannel = SkColorChannel::kA;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400395 yuvaIndices[1].fIndex = 1;
Robert Phillips66a97342018-10-04 09:10:29 -0400396 yuvaIndices[1].fChannel = SkColorChannel::kR;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400397 yuvaIndices[2].fIndex = 1;
Robert Phillips66a97342018-10-04 09:10:29 -0400398 yuvaIndices[2].fChannel = SkColorChannel::kG;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400399 break;
400 }
401 case kNV21_YUVFormat: {
402 SkBitmap vuQuarter;
403
Robert Phillips1c7062d2018-10-04 10:44:53 -0400404 // There isn't a RG color type. Approx w/ RGBA.
Jim Van Verth47133fd2018-10-19 22:09:28 -0400405 vuQuarter.allocPixels(SkImageInfo::Make(planes.fYFull.width()/2,
406 planes.fYFull.height()/2,
407 kRGBA_8888_SkColorType,
408 kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400409
410 for (int y = 0; y < planes.fYFull.height()/2; ++y) {
411 for (int x = 0; x < planes.fYFull.width()/2; ++x) {
Robert Phillips1c7062d2018-10-04 10:44:53 -0400412 uint8_t U = *planes.fUQuarter.getAddr8(x, y);
413 uint8_t V = *planes.fVQuarter.getAddr8(x, y);
414
415 // NOT premul!
Jim Van Verth47133fd2018-10-19 22:09:28 -0400416 // V and 0 swapped to match RGBA layout
417 *vuQuarter.getAddr32(x, y) = SkColorSetARGB(0, 0, U, V);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400418 }
419 }
420
421 resultBMs[nextLayer++] = planes.fYFull;
422 resultBMs[nextLayer++] = vuQuarter;
423
424 yuvaIndices[0].fIndex = 0;
Robert Phillips66a97342018-10-04 09:10:29 -0400425 yuvaIndices[0].fChannel = SkColorChannel::kA;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400426 yuvaIndices[1].fIndex = 1;
Robert Phillips66a97342018-10-04 09:10:29 -0400427 yuvaIndices[1].fChannel = SkColorChannel::kG;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400428 yuvaIndices[2].fIndex = 1;
Robert Phillips66a97342018-10-04 09:10:29 -0400429 yuvaIndices[2].fChannel = SkColorChannel::kR;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400430 break;
431 }
432 case kI420_YUVFormat:
433 resultBMs[nextLayer++] = planes.fYFull;
434 resultBMs[nextLayer++] = planes.fUQuarter;
435 resultBMs[nextLayer++] = planes.fVQuarter;
436
437 yuvaIndices[0].fIndex = 0;
Robert Phillips66a97342018-10-04 09:10:29 -0400438 yuvaIndices[0].fChannel = SkColorChannel::kA;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400439 yuvaIndices[1].fIndex = 1;
Robert Phillips66a97342018-10-04 09:10:29 -0400440 yuvaIndices[1].fChannel = SkColorChannel::kA;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400441 yuvaIndices[2].fIndex = 2;
Robert Phillips66a97342018-10-04 09:10:29 -0400442 yuvaIndices[2].fChannel = SkColorChannel::kA;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400443 break;
444 case kYV12_YUVFormat:
445 resultBMs[nextLayer++] = planes.fYFull;
446 resultBMs[nextLayer++] = planes.fVQuarter;
447 resultBMs[nextLayer++] = planes.fUQuarter;
448
449 yuvaIndices[0].fIndex = 0;
Robert Phillips66a97342018-10-04 09:10:29 -0400450 yuvaIndices[0].fChannel = SkColorChannel::kA;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400451 yuvaIndices[1].fIndex = 2;
Robert Phillips66a97342018-10-04 09:10:29 -0400452 yuvaIndices[1].fChannel = SkColorChannel::kA;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400453 yuvaIndices[2].fIndex = 1;
Robert Phillips66a97342018-10-04 09:10:29 -0400454 yuvaIndices[2].fChannel = SkColorChannel::kA;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400455 break;
456 }
457
Jim Van Verth976a6b02018-10-17 15:27:19 -0400458 if (kAYUV_YUVFormat != yuvFormat) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400459 if (opaque) {
460 yuvaIndices[3].fIndex = -1;
461 } else {
462 resultBMs[nextLayer] = planes.fAFull;
463
464 yuvaIndices[3].fIndex = nextLayer;
Robert Phillips66a97342018-10-04 09:10:29 -0400465 yuvaIndices[3].fChannel = SkColorChannel::kA;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400466 }
467 }
468
469}
470
Robert Phillips1c7062d2018-10-04 10:44:53 -0400471static uint8_t look_up(float x1, float y1, const SkBitmap& bm, SkColorChannel channel) {
472 uint8_t result;
473
474 int x = SkScalarFloorToInt(x1 * bm.width());
475 int y = SkScalarFloorToInt(y1 * bm.height());
476
477 if (kAlpha_8_SkColorType == bm.colorType()) {
478 SkASSERT(SkColorChannel::kA == channel);
479 result = *bm.getAddr8(x, y);
480 } else {
Jim Van Verth47133fd2018-10-19 22:09:28 -0400481 SkASSERT(kRGBA_8888_SkColorType == bm.colorType());
Robert Phillips1c7062d2018-10-04 10:44:53 -0400482
483 switch (channel) {
484 case SkColorChannel::kR:
485 result = SkColorGetR(bm.getColor(x, y));
486 break;
487 case SkColorChannel::kG:
488 result = SkColorGetG(bm.getColor(x, y));
489 break;
490 case SkColorChannel::kB:
491 result = SkColorGetB(bm.getColor(x, y));
492 break;
493 case SkColorChannel::kA:
494 result = SkColorGetA(bm.getColor(x, y));
495 break;
496 }
497 }
498
499 return result;
500}
501
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400502class YUVGenerator : public SkImageGenerator {
503public:
504 YUVGenerator(const SkImageInfo& ii,
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400505 SkYUVColorSpace yuvColorSpace,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400506 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
Jim Van Verthe24b5872018-10-29 16:26:02 -0400507 SkBitmap bitmaps[SkYUVASizeInfo::kMaxCount])
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400508 : SkImageGenerator(ii)
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400509 , fYUVColorSpace(yuvColorSpace) {
510 memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices));
511
Jim Van Verth8f11e432018-10-18 14:36:59 -0400512 SkAssertResult(SkYUVAIndex::AreValidIndices(fYUVAIndices, &fNumBitmaps));
Jim Van Verthe24b5872018-10-29 16:26:02 -0400513 SkASSERT(fNumBitmaps > 0 && fNumBitmaps <= SkYUVASizeInfo::kMaxCount);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400514
Jim Van Verth8f11e432018-10-18 14:36:59 -0400515 for (int i = 0; i < fNumBitmaps; ++i) {
516 fYUVBitmaps[i] = bitmaps[i];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400517 }
518 }
519
520protected:
521 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
522 const Options&) override {
523
524 if (kUnknown_SkColorType == fFlattened.colorType()) {
Brian Osman69083b22018-10-19 13:48:22 -0400525 fFlattened.allocPixels(this->getInfo());
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400526
527 for (int y = 0; y < info.height(); ++y) {
528 for (int x = 0; x < info.width(); ++x) {
529
Robert Phillips1c7062d2018-10-04 10:44:53 -0400530 float x1 = (x + 0.5f) / info.width();
531 float y1 = (y + 0.5f) / info.height();
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400532
Robert Phillips1c7062d2018-10-04 10:44:53 -0400533 uint8_t Y = look_up(x1, y1,
534 fYUVBitmaps[fYUVAIndices[0].fIndex],
535 fYUVAIndices[0].fChannel);
536
537 uint8_t U = look_up(x1, y1,
538 fYUVBitmaps[fYUVAIndices[1].fIndex],
539 fYUVAIndices[1].fChannel);
540
541
542 uint8_t V = look_up(x1, y1,
543 fYUVBitmaps[fYUVAIndices[2].fIndex],
544 fYUVAIndices[2].fChannel);
545
546 uint8_t A = 255;
547 if (fYUVAIndices[3].fIndex >= 0) {
548 A = look_up(x1, y1,
549 fYUVBitmaps[fYUVAIndices[3].fIndex],
550 fYUVAIndices[3].fChannel);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400551 }
552
553 // Making premul here.
Robert Phillips1c7062d2018-10-04 10:44:53 -0400554 switch (fYUVColorSpace) {
555 case kJPEG_SkYUVColorSpace:
556 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_jpeg(Y, U, V, A);
557 break;
558 case kRec601_SkYUVColorSpace:
559 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_601(Y, U, V, A);
560 break;
561 case kRec709_SkYUVColorSpace:
562 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_709(Y, U, V, A);
563 break;
564 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400565 }
566 }
567 }
568
569 return fFlattened.readPixels(info, pixels, rowBytes, 0, 0);
570 }
571
Jim Van Verthe24b5872018-10-29 16:26:02 -0400572 bool onQueryYUVA8(SkYUVASizeInfo* size,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400573 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
574 SkYUVColorSpace* yuvColorSpace) const override {
575
576 memcpy(yuvaIndices, fYUVAIndices, sizeof(fYUVAIndices));
577 *yuvColorSpace = fYUVColorSpace;
578
579 int i = 0;
580 for ( ; i < fNumBitmaps; ++i) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400581 size->fSizes[i].fWidth = fYUVBitmaps[i].width();
582 size->fSizes[i].fHeight = fYUVBitmaps[i].height();
583 size->fWidthBytes[i] = fYUVBitmaps[i].rowBytes();
584 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400585 for ( ; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400586 size->fSizes[i].fWidth = 0;
587 size->fSizes[i].fHeight = 0;
588 size->fWidthBytes[i] = 0;
Jim Van Verthf99a6742018-10-18 16:13:18 +0000589 }
Jim Van Verth0c583af2018-10-18 10:31:59 -0400590
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400591 return true;
592 }
593
Jim Van Verthe24b5872018-10-29 16:26:02 -0400594 bool onGetYUVA8Planes(const SkYUVASizeInfo&, const SkYUVAIndex[SkYUVAIndex::kIndexCount],
595 void* planes[SkYUVASizeInfo::kMaxCount]) override {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400596 for (int i = 0; i < fNumBitmaps; ++i) {
597 planes[i] = fYUVBitmaps[i].getPixels();
598 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400599 return true;
600 }
601
602private:
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400603 SkYUVColorSpace fYUVColorSpace;
Jim Van Verth8f11e432018-10-18 14:36:59 -0400604 SkYUVAIndex fYUVAIndices[SkYUVAIndex::kIndexCount];
605 int fNumBitmaps;
Jim Van Verthe24b5872018-10-29 16:26:02 -0400606 SkBitmap fYUVBitmaps[SkYUVASizeInfo::kMaxCount];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400607 SkBitmap fFlattened;
608
609};
610
611static sk_sp<SkImage> make_yuv_gen_image(const SkImageInfo& ii,
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400612 SkYUVColorSpace yuvColorSpace,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400613 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400614 SkBitmap bitmaps[]) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400615 std::unique_ptr<SkImageGenerator> gen(new YUVGenerator(ii, yuvColorSpace,
616 yuvaIndices, bitmaps));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400617
618 return SkImage::MakeFromGenerator(std::move(gen));
619}
620
621static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) {
622 static const char* kYUVColorSpaceNames[] = { "JPEG", "601", "709" };
623 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace+1);
624
Mike Reed91919132019-01-02 12:21:01 -0500625 SkPaint paint;
626 SkFont font(sk_tool_utils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
627 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400628
629 SkRect textRect;
630 SkString colLabel;
631
632 colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]);
Mike Reed91919132019-01-02 12:21:01 -0500633 font.measureText(colLabel.c_str(), colLabel.size(), kUTF8_SkTextEncoding, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400634 int y = textRect.height();
635
Mike Reed91919132019-01-02 12:21:01 -0500636 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400637
638 colLabel.printf("%s", opaque ? "Opaque" : "Transparent");
Mike Reed71f5a0b2018-10-25 16:12:39 -0400639
Mike Reed91919132019-01-02 12:21:01 -0500640 font.measureText(colLabel.c_str(), colLabel.size(), kUTF8_SkTextEncoding, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400641 y += textRect.height();
642
Mike Reed91919132019-01-02 12:21:01 -0500643 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400644}
645
646static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) {
647 static const char* kYUVFormatNames[] = { "AYUV", "NV12", "NV21", "I420", "YV12" };
648 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat+1);
649
Mike Reed91919132019-01-02 12:21:01 -0500650 SkPaint paint;
651 SkFont font(sk_tool_utils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
652 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400653
654 SkRect textRect;
655 SkString rowLabel;
656
657 rowLabel.printf("%s", kYUVFormatNames[yuvFormat]);
Mike Reed91919132019-01-02 12:21:01 -0500658 font.measureText(rowLabel.c_str(), rowLabel.size(), kUTF8_SkTextEncoding, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400659 y += kTileWidthHeight/2 + textRect.height()/2;
660
Mike Reed91919132019-01-02 12:21:01 -0500661 canvas->drawSimpleText(rowLabel.c_str(), rowLabel.size(), kUTF8_SkTextEncoding, 0, y, font, paint);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400662}
663
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500664static GrBackendTexture create_yuva_texture(GrGpu* gpu, const SkBitmap& bm,
665 SkYUVAIndex yuvaIndices[4], int texIndex) {
666 SkASSERT(texIndex >= 0 && texIndex <= 3);
667 int channelCount = 0;
668 for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
669 if (yuvaIndices[i].fIndex == texIndex) {
670 ++channelCount;
671 }
672 }
673 // Need to create an RG texture for two-channel planes
674 GrBackendTexture tex;
675 if (2 == channelCount) {
676 SkASSERT(kRGBA_8888_SkColorType == bm.colorType());
677 SkAutoTMalloc<char> pixels(2 * bm.width()*bm.height());
678 char* currPixel = pixels;
679 for (int y = 0; y < bm.height(); ++y) {
680 for (int x = 0; x < bm.width(); ++x) {
681 SkColor color = bm.getColor(x, y);
682 currPixel[0] = SkColorGetR(color);
683 currPixel[1] = SkColorGetG(color);
684 currPixel += 2;
685 }
686 }
687 tex = gpu->createTestingOnlyBackendTexture(
688 pixels,
689 bm.width(),
690 bm.height(),
691 GrColorType::kRG_88,
692 false,
693 GrMipMapped::kNo,
694 2*bm.width());
695 } else {
696 tex = gpu->createTestingOnlyBackendTexture(
697 bm.getPixels(),
698 bm.width(),
699 bm.height(),
700 bm.colorType(),
701 false,
702 GrMipMapped::kNo,
703 bm.rowBytes());
704 }
705 return tex;
706}
707
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400708namespace skiagm {
709
710// This GM creates an opaque and transparent bitmap, extracts the planes and then recombines
711// them into various YUV formats. It then renders the results in the grid:
712//
713// JPEG 601 709
714// Transparent Opaque Transparent Opaque Transparent Opaque
715// AYUV
716// NV12
717// NV21
718// I420
719// YV12
720class WackyYUVFormatsGM : public GM {
721public:
722 WackyYUVFormatsGM() {
723 this->setBGColor(0xFFCCCCCC);
724 }
725
726protected:
727
728 SkString onShortName() override {
729 return SkString("wacky_yuv_formats");
730 }
731
732 SkISize onISize() override {
733 int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x color-space
734 int numRows = 1 + (kLast_YUVFormat + 1); // origin + # yuv formats
735 return SkISize::Make(kLabelWidth + numCols * (kTileWidthHeight + kPad),
736 kLabelHeight + numRows * (kTileWidthHeight + kPad));
737 }
738
739 void onOnceBeforeDraw() override {
740 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
741 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
742 float innerRadius = 20.0f;
743
744 {
745 // transparent
746 SkTDArray<SkRect> circles;
747 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
748 fOriginalBMs[0] = make_bitmap(path, circles, false);
749 }
750
751 {
752 // opaque
753 SkTDArray<SkRect> circles;
754 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
755 fOriginalBMs[1] = make_bitmap(path, circles, true);
756 }
Robert Phillips51c89e42018-10-05 13:30:43 -0400757 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400758
Robert Phillips51c89e42018-10-05 13:30:43 -0400759 void createImages(GrContext* context) {
Jim Van Verth9bf81202018-10-30 15:53:36 -0400760 int counter = 0;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400761 for (bool opaque : { false, true }) {
762 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
763 PlaneData planes;
764 extract_planes(fOriginalBMs[opaque], (SkYUVColorSpace) cs, &planes);
765
Jim Van Verth976a6b02018-10-17 15:27:19 -0400766 for (int format = kAYUV_YUVFormat; format <= kLast_YUVFormat; ++format) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400767 SkBitmap resultBMs[4];
768 SkYUVAIndex yuvaIndices[4];
769 create_YUV(planes, (YUVFormat) format, resultBMs, yuvaIndices, opaque);
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500770 int numTextures;
771 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) {
772 continue;
773 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400774
Robert Phillips51c89e42018-10-05 13:30:43 -0400775 if (context) {
776 if (context->abandoned()) {
777 return;
778 }
779
780 GrGpu* gpu = context->contextPriv().getGpu();
781 if (!gpu) {
782 return;
783 }
784
Robert Phillips51c89e42018-10-05 13:30:43 -0400785 GrBackendTexture yuvaTextures[4];
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500786 SkPixmap yuvaPixmaps[4];
Robert Phillips51c89e42018-10-05 13:30:43 -0400787
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500788 for (int i = 0; i < numTextures; ++i) {
789 yuvaTextures[i] = create_yuva_texture(gpu, resultBMs[i],
790 yuvaIndices, i);
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500791 yuvaPixmaps[i] = resultBMs[i].pixmap();
Robert Phillips51c89e42018-10-05 13:30:43 -0400792 }
793
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500794 int counterMod = counter % 3;
795 switch (counterMod) {
796 case 0:
Jim Van Verth9bf81202018-10-30 15:53:36 -0400797 fImages[opaque][cs][format] = SkImage::MakeFromYUVATexturesCopy(
798 context,
799 (SkYUVColorSpace)cs,
800 yuvaTextures,
801 yuvaIndices,
802 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
803 kTopLeft_GrSurfaceOrigin);
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500804 break;
805 case 1:
Jim Van Verth9bf81202018-10-30 15:53:36 -0400806 fImages[opaque][cs][format] = SkImage::MakeFromYUVATextures(
807 context,
808 (SkYUVColorSpace)cs,
809 yuvaTextures,
810 yuvaIndices,
811 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
812 kTopLeft_GrSurfaceOrigin);
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500813 break;
814 case 2:
815 default:
816 fImages[opaque][cs][format] = SkImage::MakeFromYUVAPixmaps(
817 context,
818 (SkYUVColorSpace)cs,
819 yuvaPixmaps,
820 yuvaIndices,
821 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
822 kTopLeft_GrSurfaceOrigin, true);
823 break;
Jim Van Verth9bf81202018-10-30 15:53:36 -0400824 }
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500825 ++counter;
Jim Van Verth9bf81202018-10-30 15:53:36 -0400826 } else {
Robert Phillips51c89e42018-10-05 13:30:43 -0400827 fImages[opaque][cs][format] = make_yuv_gen_image(
828 fOriginalBMs[opaque].info(),
Robert Phillips51c89e42018-10-05 13:30:43 -0400829 (SkYUVColorSpace) cs,
830 yuvaIndices,
831 resultBMs);
832 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400833 }
834 }
835 }
836 }
837
838 void onDraw(SkCanvas* canvas) override {
Robert Phillips51c89e42018-10-05 13:30:43 -0400839 this->createImages(canvas->getGrContext());
840
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400841 int x = kLabelWidth;
842 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
843 for (int opaque : { 0, 1 }) {
844 int y = kLabelHeight;
845
846 draw_col_label(canvas, x+kTileWidthHeight/2, cs, opaque);
847
848 canvas->drawBitmap(fOriginalBMs[opaque], x, y);
849 y += kTileWidthHeight + kPad;
850
Jim Van Verth976a6b02018-10-17 15:27:19 -0400851 for (int format = kAYUV_YUVFormat; format <= kLast_YUVFormat; ++format) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400852 draw_row_label(canvas, y, format);
853 canvas->drawImage(fImages[opaque][cs][format], x, y);
854
855 y += kTileWidthHeight + kPad;
856 }
857
858 x += kTileWidthHeight + kPad;
859 }
860 }
861 }
862
863private:
864 SkBitmap fOriginalBMs[2];
865 sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace+1][kLast_YUVFormat+1];
866
867 typedef GM INHERITED;
868};
869
870//////////////////////////////////////////////////////////////////////////////
871
872DEF_GM(return new WackyYUVFormatsGM;)
873}