blob: 5f0ac590fbc751f83fb99258b79559996dd7ee4d [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"
Robert Phillipsb87b39b2020-07-01 14:45:24 -040039#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040040#include "include/gpu/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040041#include "include/gpu/GrTypes.h"
42#include "include/private/GrTypesPriv.h"
43#include "include/private/SkTArray.h"
44#include "include/private/SkTDArray.h"
45#include "include/private/SkTemplates.h"
46#include "include/utils/SkTextUtils.h"
Brian Osmanac8a16c2019-10-31 10:24:12 -040047#include "src/core/SkYUVMath.h"
Robert Phillips95c250c2020-06-29 15:36:12 -040048#include "src/gpu/GrCaps.h"
Robert Phillips95c250c2020-06-29 15:36:12 -040049#include "src/gpu/GrRecordingContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040050#include "tools/ToolUtils.h"
Robert Phillipsf105d382020-06-19 14:27:14 -040051#include "tools/gpu/YUVUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040052
53#include <math.h>
54#include <string.h>
55#include <initializer_list>
56#include <memory>
57#include <utility>
58
Robert Phillipsf105d382020-06-19 14:27:14 -040059using sk_gpu_test::YUVABackendReleaseContext;
Ben Wagner7fde8e12019-05-01 17:28:53 -040060class GrRenderTargetContext;
Robert Phillips51c89e42018-10-05 13:30:43 -040061
Robert Phillipsbfa76f22018-10-03 12:12:26 -040062static const int kTileWidthHeight = 128;
63static const int kLabelWidth = 64;
64static const int kLabelHeight = 32;
Michael Ludwiga6a84002019-04-12 15:03:02 -040065static const int kDomainPadding = 8;
Robert Phillipsbfa76f22018-10-03 12:12:26 -040066static const int kPad = 1;
Robert Phillipsbfa76f22018-10-03 12:12:26 -040067
68enum YUVFormat {
Robert Phillipsbb749902019-06-10 17:20:12 -040069 // 4:2:0 formats, 24 bpp
70 kP016_YUVFormat, // 16-bit Y plane + 2x2 down sampled interleaved U/V plane (2 textures)
71 // 4:2:0 formats, "15 bpp" (but really 24 bpp)
72 kP010_YUVFormat, // same as kP016 except "10 bpp". Note that it is the same memory layout
73 // except that the bottom 6 bits are zeroed out (2 textures)
74 // TODO: we're cheating a bit w/ P010 and just treating it as unorm 16. This means its
75 // fully saturated values are 65504 rather than 65535 (that is just .9995 out of 1.0 though).
76
Robert Phillips17a3a0b2019-09-18 13:56:54 -040077 // This is laid out the same as kP016 and kP010 but uses F16 unstead of U16. In this case
78 // the 10 bits/channel vs 16 bits/channel distinction isn't relevant.
79 kP016F_YUVFormat,
80
Robert Phillipsbb749902019-06-10 17:20:12 -040081 // 4:4:4 formats, 64 bpp
82 kY416_YUVFormat, // 16-bit AVYU values all interleaved (1 texture)
83
Robert Phillipsbfa76f22018-10-03 12:12:26 -040084 // 4:4:4 formats, 32 bpp
Robert Phillipsbb749902019-06-10 17:20:12 -040085 kAYUV_YUVFormat, // 8-bit YUVA values all interleaved (1 texture)
86 kY410_YUVFormat, // AVYU w/ 10bpp for YUV and 2 for A all interleaved (1 texture)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040087
88 // 4:2:0 formats, 12 bpp
Robert Phillipsbb749902019-06-10 17:20:12 -040089 kNV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled interleaved U/V planes (2 textures)
90 kNV21_YUVFormat, // same as kNV12 but w/ U/V reversed in the interleaved texture (2 textures)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040091
Robert Phillipsbb749902019-06-10 17:20:12 -040092 kI420_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled U and V planes (3 textures)
93 kYV12_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled V and U planes (3 textures)
Robert Phillipsbfa76f22018-10-03 12:12:26 -040094
95 kLast_YUVFormat = kYV12_YUVFormat
96};
97
Brian Salomonf7255d72020-04-08 15:57:08 -040098class YUVAPlanarConfig {
99public:
100 struct YUVALocation {
101 int fPlaneIdx = -1;
102 int fChannelIdx = -1;
103 };
Robert Phillipsbb749902019-06-10 17:20:12 -0400104
Brian Salomonf7255d72020-04-08 15:57:08 -0400105 enum class YUVAChannel { kY, kU, kV, kA };
106
Robert Phillips404b7cf2020-04-10 11:27:44 -0400107 explicit YUVAPlanarConfig(const std::initializer_list<YUVALocation>& yuvaLocations);
Brian Salomonf7255d72020-04-08 15:57:08 -0400108
109 constexpr int numPlanes() const { return fNumPlanes; }
110
111 int planeIndex(YUVAChannel c) const { return fLocations[static_cast<int>(c)].fPlaneIdx; }
112
113 int channelIndex(YUVAChannel c) const { return fLocations[static_cast<int>(c)].fChannelIdx; }
114
115 constexpr bool hasAlpha() const { return fLocations[3].fPlaneIdx >= 0; }
116
117 /**
118 * Given a mask of SkColorChannelFlags choose a channel by index. Legal 'channelMask' values
119 * are:
120 * kAlpha, kGray, kRed, kRG, kRGB, kRGBA.
121 * The channel index must be less than the number of bits set in the mask. The index order is
122 * the order listed above (e.g. if 'channelMask' is kRGB and 'channelIdx' is 1 then
123 * SkColorChannel::kG is returned as 'channel'). The function fails if 'channelMask' is not one
124 * of the listed allowed values or 'channelIdx' is invalid for the mask.
125 */
126 static bool ChannelIndexToChannel(uint32_t channelMask,
127 int channelIdx,
128 SkColorChannel* channel);
129
130 /**
131 * Goes from channel indices to actual channels given texture formats. Also supports adding
132 * on an external alpha plane if this format doesn't already have alpha. The extra alpha texture
133 * must be the last texture and the channel index is assumed to be 0.
134 */
135 bool getYUVAIndices(const GrBackendTexture textures[],
136 int numTextures,
137 bool externalAlphaPlane,
138 SkYUVAIndex indices[4]) const;
139
140 /** Same as above but with pixmaps instead of textures. */
141 bool getYUVAIndices(const SkBitmap planes[],
142 int numBitmaps,
143 bool externalAlphaPlane,
144 SkYUVAIndex indices[4]) const;
145
146private:
147 bool getYUVAIndices(const uint32_t channelMasks[],
148 int numPlanes,
149 bool externalAlphaPlane,
150 SkYUVAIndex indices[4]) const;
151
152 YUVALocation fLocations[4] = {};
153 int fNumPlanes = 0;
154};
155
Robert Phillips404b7cf2020-04-10 11:27:44 -0400156YUVAPlanarConfig::YUVAPlanarConfig(const std::initializer_list<YUVALocation>& yuvaLocations) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400157 SkASSERT(yuvaLocations.size() == 3 || yuvaLocations.size() == 4);
158 uint32_t planeMask[5] = {};
159 int l = 0;
160 for (const auto& location : yuvaLocations) {
161 SkASSERT(location.fChannelIdx >= 0 && location.fChannelIdx <= 3);
162 SkASSERT(location.fPlaneIdx >= 0 && location.fPlaneIdx <= 3);
163 fLocations[l++] = location;
164 fNumPlanes = std::max(fNumPlanes, location.fPlaneIdx + 1);
165 int mask = 1 << location.fChannelIdx;
166 SkASSERT(!(planeMask[location.fPlaneIdx] & mask));
167 planeMask[location.fPlaneIdx] |= mask;
168 }
169
170 // Check that no plane is skipped and channel usage in each plane is tightly packed.
171 for (int i = 0; i < fNumPlanes; ++i) {
172 switch (planeMask[i]) {
173 case 0b0001: break;
174 case 0b0011: break;
175 case 0b0111: break;
176 case 0b1111: break;
177 default: SK_ABORT("Illegal channel configuration. "
178 "Maximum of 4 channels per plane. "
179 "No skipped channels in any plane.");
180 }
181 }
182}
183
184bool YUVAPlanarConfig::ChannelIndexToChannel(uint32_t channelFlags,
185 int channelIdx,
186 SkColorChannel* channel) {
187 switch (channelFlags) {
188 case kGray_SkColorChannelFlag: // For gray returning any of R, G, or B for index 0 is ok.
189 case kRed_SkColorChannelFlag:
190 if (channelIdx == 0) {
191 *channel = SkColorChannel::kR;
192 return true;
193 }
194 return false;
195 case kAlpha_SkColorChannelFlag:
196 if (channelIdx == 0) {
197 *channel = SkColorChannel::kA;
198 return true;
199 }
200 return false;
201 case kRG_SkColorChannelFlags:
202 if (channelIdx == 0 || channelIdx == 1) {
203 *channel = static_cast<SkColorChannel>(channelIdx);
204 return true;
205 }
206 return false;
207 case kRGB_SkColorChannelFlags:
208 if (channelIdx >= 0 && channelIdx <= 2) {
209 *channel = static_cast<SkColorChannel>(channelIdx);
210 return true;
211 }
212 return false;
213 case kRGBA_SkColorChannelFlags:
214 if (channelIdx >= 0 && channelIdx <= 3) {
215 *channel = static_cast<SkColorChannel>(channelIdx);
216 return true;
217 }
218 return false;
219 default:
220 return false;
221 }
222}
223
224bool YUVAPlanarConfig::getYUVAIndices(const GrBackendTexture textures[],
225 int numTextures,
226 bool externalAlphaPlane,
227 SkYUVAIndex indices[4]) const {
228 uint32_t channelMasks[4] = {};
229 for (int i = 0; i < numTextures; ++i) {
230 channelMasks[i] = textures[i].getBackendFormat().channelMask();
231 }
232 return this->getYUVAIndices(channelMasks, numTextures, externalAlphaPlane, indices);
233}
234
235bool YUVAPlanarConfig::getYUVAIndices(const SkBitmap bitmaps[],
236 int numBitmaps,
237 bool externalAlphaPlane,
238 SkYUVAIndex indices[4]) const {
239 uint32_t channelMasks[4] = {};
240 for (int i = 0; i < numBitmaps; ++i) {
241 channelMasks[i] = SkColorTypeChannelFlags(bitmaps[i].colorType());
242 }
243 return this->getYUVAIndices(channelMasks, numBitmaps, externalAlphaPlane, indices);
244}
245
246bool YUVAPlanarConfig::getYUVAIndices(const uint32_t planeChannelMasks[],
247 int numPlanes,
248 bool externalAlphaPlane,
249 SkYUVAIndex indices[4]) const {
250 if (this->hasAlpha() && externalAlphaPlane) {
251 return false;
252 }
253 if (numPlanes != fNumPlanes + SkToInt(externalAlphaPlane)) {
254 return false;
255 }
256 for (int i = 0; i < 4; ++i) {
257 int plane = fLocations[i].fPlaneIdx;
258 if (plane < 0) {
259 indices[i].fIndex = -1;
Brian Salomon12632f12020-04-10 09:57:17 -0400260 indices[i].fChannel = SkColorChannel::kR;
Brian Salomonf7255d72020-04-08 15:57:08 -0400261 } else {
262 indices[i].fIndex = plane;
263 if (!ChannelIndexToChannel(planeChannelMasks[plane], fLocations[i].fChannelIdx,
264 &indices[i].fChannel)) {
265 return false;
266 }
267 }
268 }
269 if (externalAlphaPlane) {
270 if (!ChannelIndexToChannel(planeChannelMasks[numPlanes - 1], 0, &indices[3].fChannel)) {
271 return false;
272 }
273 indices[3].fIndex = numPlanes - 1;
274 }
275 SkDEBUGCODE(int checkNumPlanes;)
276 SkASSERT(SkYUVAIndex::AreValidIndices(indices, &checkNumPlanes));
277 SkASSERT(checkNumPlanes == numPlanes);
278 return true;
279}
280
281static const YUVAPlanarConfig& YUVAFormatPlanarConfig(YUVFormat format) {
282 switch (format) {
283 case kP016_YUVFormat: // These all share the same plane/channel indices.
284 case kP010_YUVFormat:
285 case kP016F_YUVFormat: {
286 static const YUVAPlanarConfig kConfig({{0, 0}, {1, 0}, {1, 1}});
287 return kConfig;
288 }
289 case kY416_YUVFormat: {
290 static const YUVAPlanarConfig kConfig({{0, 1}, {0, 0}, {0, 2}, {0, 3}});
291 return kConfig;
292 }
293 case kAYUV_YUVFormat: {
294 static const YUVAPlanarConfig kConfig({{0, 0}, {0, 1}, {0, 2}, {0, 3}});
295 return kConfig;
296 }
297 case kY410_YUVFormat: {
298 static const YUVAPlanarConfig kConfig({{0, 1}, {0, 0}, {0, 2}, {0, 3}});
299 return kConfig;
300 }
301 case kNV12_YUVFormat: {
302 static const YUVAPlanarConfig kConfig({{0, 0}, {1, 0}, {1, 1}});
303 return kConfig;
304 }
305 case kNV21_YUVFormat: {
306 static const YUVAPlanarConfig kConfig({{0, 0}, {1, 1}, {1, 0}});
307 return kConfig;
308 }
309 case kI420_YUVFormat: {
310 static const YUVAPlanarConfig kConfig({{0, 0}, {1, 0}, {2, 0}});
311 return kConfig;
312 }
313 case kYV12_YUVFormat: {
314 static const YUVAPlanarConfig kConfig({{0, 0}, {2, 0}, {1, 0}});
315 return kConfig;
316 }
317 }
318 SkUNREACHABLE;
Robert Phillipsbb749902019-06-10 17:20:12 -0400319}
320
Robert Phillipsf47717a2019-09-19 13:04:33 -0400321static bool is_colorType_texturable(const GrCaps* caps, GrColorType ct) {
322 GrBackendFormat format = caps->getDefaultBackendFormat(ct, GrRenderable::kNo);
323 if (!format.isValid()) {
324 return false;
325 }
326
327 return caps->isFormatTexturable(format);
328}
329
Robert Phillips95c250c2020-06-29 15:36:12 -0400330static bool is_format_natively_supported(GrRecordingContext* context, YUVFormat yuvFormat) {
Robert Phillipsf47717a2019-09-19 13:04:33 -0400331
332 const GrCaps* caps = context->priv().caps();
333
334 switch (yuvFormat) {
335 case kP016_YUVFormat: // fall through
336 case kP010_YUVFormat: return is_colorType_texturable(caps, GrColorType::kAlpha_16) &&
337 is_colorType_texturable(caps, GrColorType::kRG_1616);
338 case kP016F_YUVFormat: return is_colorType_texturable(caps, GrColorType::kAlpha_F16) &&
339 is_colorType_texturable(caps, GrColorType::kRG_F16);
340 case kY416_YUVFormat: return is_colorType_texturable(caps, GrColorType::kRGBA_16161616);
341 case kAYUV_YUVFormat: return is_colorType_texturable(caps, GrColorType::kRGBA_8888);
342 case kY410_YUVFormat: return is_colorType_texturable(caps, GrColorType::kRGBA_1010102);
343 case kNV12_YUVFormat: // fall through
344 case kNV21_YUVFormat: return is_colorType_texturable(caps, GrColorType::kGray_8) &&
345 is_colorType_texturable(caps, GrColorType::kRG_88);
346 case kI420_YUVFormat: // fall through
347 case kYV12_YUVFormat: return is_colorType_texturable(caps, GrColorType::kGray_8);
348 }
349
350 SkUNREACHABLE;
351}
352
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400353// All the planes we need to construct the various YUV formats
354struct PlaneData {
355 SkBitmap fYFull;
356 SkBitmap fUFull;
357 SkBitmap fVFull;
358 SkBitmap fAFull;
359 SkBitmap fUQuarter; // 2x2 downsampled U channel
360 SkBitmap fVQuarter; // 2x2 downsampled V channel
Robert Phillips429f0d32019-09-11 17:03:28 -0400361
362 SkBitmap fFull;
363 SkBitmap fQuarter; // 2x2 downsampled YUVA
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400364};
365
366// Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle
367// and have tangents 'v1' and 'v2'.
368static void add_arc(SkPath* path,
369 const SkPoint& o1, const SkVector& v1,
370 const SkPoint& o2, const SkVector& v2,
371 SkTDArray<SkRect>* circles, bool takeLongWayRound) {
372
373 SkVector v3 = { -v1.fY, v1.fX };
374 SkVector v4 = { v2.fY, -v2.fX };
375
376 SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4);
377 SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY };
378
379 SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t };
380
381 if (circles) {
382 circles->push_back(r);
383 }
384
385 SkVector startV = o1 - center, endV = o2 - center;
386 startV.normalize();
387 endV.normalize();
388
389 SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX));
390 SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX));
391
392 startDeg += 360.0f;
393 startDeg = fmodf(startDeg, 360.0f);
394
395 endDeg += 360.0f;
396 endDeg = fmodf(endDeg, 360.0f);
397
398 if (endDeg < startDeg) {
399 endDeg += 360.0f;
400 }
401
402 SkScalar sweepDeg = SkTAbs(endDeg - startDeg);
403 if (!takeLongWayRound) {
404 sweepDeg = sweepDeg - 360;
405 }
406
407 path->arcTo(r, startDeg, sweepDeg, false);
408}
409
410static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius,
411 SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) {
412 if (numLobes <= 1) {
413 return SkPath();
414 }
415
416 SkPath p;
417
418 int numDivisions = 2 * numLobes;
419 SkScalar fullLobeDegrees = 360.0f / numLobes;
420 SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f);
421 SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f);
422 SkMatrix outerStep, innerStep;
423 outerStep.setRotate(outDegrees);
424 innerStep.setRotate(innerDegrees);
425 SkVector curV = SkVector::Make(0.0f, 1.0f);
426
427 if (circles) {
428 circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius,
429 o.fX + innerRadius, o.fY + innerRadius));
430 }
431
432 p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY);
433
434 for (int i = 0; i < numDivisions; ++i) {
435
436 SkVector nextV;
437 if (0 == (i % 2)) {
438 nextV = outerStep.mapVector(curV.fX, curV.fY);
439
440 SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX,
441 o.fY + outerRadius * curV.fY);
442 SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX,
443 o.fY + outerRadius * nextV.fY);
444
445 p.lineTo(top);
446 add_arc(&p, top, curV, nextTop, nextV, circles, true);
447 } else {
448 nextV = innerStep.mapVector(curV.fX, curV.fY);
449
450 SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX,
451 o.fY + innerRadius * curV.fY);
452 SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX,
453 o.fY + innerRadius * nextV.fY);
454
455 p.lineTo(bot);
456 add_arc(&p, bot, curV, nextBot, nextV, nullptr, false);
457 }
458
459 curV = nextV;
460 }
461
462 p.close();
463
464 return p;
465}
466
Robert Phillips2dd1b472019-03-21 09:00:20 -0400467static SkBitmap make_bitmap(SkColorType colorType, const SkPath& path,
Michael Ludwiga6a84002019-04-12 15:03:02 -0400468 const SkTDArray<SkRect>& circles, bool opaque, bool padWithRed) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500469 const SkColor kGreen = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104));
470 const SkColor kBlue = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252));
471 const SkColor kYellow = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400472
Michael Ludwiga6a84002019-04-12 15:03:02 -0400473 int widthHeight = kTileWidthHeight + (padWithRed ? 2 * kDomainPadding : 0);
474
475 SkImageInfo ii = SkImageInfo::Make(widthHeight, widthHeight,
Robert Phillips2dd1b472019-03-21 09:00:20 -0400476 colorType, kPremul_SkAlphaType);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400477
478 SkBitmap bm;
479 bm.allocPixels(ii);
480
Robert Phillips2dd1b472019-03-21 09:00:20 -0400481 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(ii,
482 bm.getPixels(),
483 bm.rowBytes());
Michael Ludwiga6a84002019-04-12 15:03:02 -0400484 if (padWithRed) {
485 canvas->clear(SK_ColorRED);
486 canvas->translate(kDomainPadding, kDomainPadding);
487 canvas->clipRect(SkRect::MakeWH(kTileWidthHeight, kTileWidthHeight));
488 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400489 canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT);
490
491 SkPaint paint;
492 paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency
493 paint.setColor(kBlue);
494
495 canvas->drawPath(path, paint);
496
497 paint.setColor(opaque ? kYellow : SK_ColorTRANSPARENT);
498 paint.setBlendMode(SkBlendMode::kSrc);
499 for (int i = 0; i < circles.count(); ++i) {
500 SkRect r = circles[i];
501 r.inset(r.width()/4, r.height()/4);
502 canvas->drawOval(r, paint);
503 }
504
505 return bm;
506}
507
Brian Osmanac8a16c2019-10-31 10:24:12 -0400508static void convert_rgba_to_yuva(const float mtx[20], SkColor col, uint8_t yuv[4]) {
509 const uint8_t r = SkColorGetR(col);
510 const uint8_t g = SkColorGetG(col);
511 const uint8_t b = SkColorGetB(col);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400512
Brian Osmanaba642c2020-02-06 12:52:25 -0500513 yuv[0] = SkTPin(SkScalarRoundToInt(mtx[ 0]*r + mtx[ 1]*g + mtx[ 2]*b + mtx[ 4]*255), 0, 255);
514 yuv[1] = SkTPin(SkScalarRoundToInt(mtx[ 5]*r + mtx[ 6]*g + mtx[ 7]*b + mtx[ 9]*255), 0, 255);
515 yuv[2] = SkTPin(SkScalarRoundToInt(mtx[10]*r + mtx[11]*g + mtx[12]*b + mtx[14]*255), 0, 255);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400516 yuv[3] = SkColorGetA(col);
517}
518
Brian Salomonf7255d72020-04-08 15:57:08 -0400519static SkPMColor convert_yuva_to_rgba(const float mtx[20], uint8_t yuva[4]) {
520 uint8_t y = yuva[0];
521 uint8_t u = yuva[1];
522 uint8_t v = yuva[2];
523 uint8_t a = yuva[3];
524
Brian Osmanaba642c2020-02-06 12:52:25 -0500525 uint8_t r = SkTPin(SkScalarRoundToInt(mtx[ 0]*y + mtx[ 1]*u + mtx[ 2]*v + mtx[ 4]*255), 0, 255);
526 uint8_t g = SkTPin(SkScalarRoundToInt(mtx[ 5]*y + mtx[ 6]*u + mtx[ 7]*v + mtx[ 9]*255), 0, 255);
527 uint8_t b = SkTPin(SkScalarRoundToInt(mtx[10]*y + mtx[11]*u + mtx[12]*v + mtx[14]*255), 0, 255);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400528
Robert Phillipse0735522020-01-31 11:03:32 -0500529 return SkPremultiplyARGBInline(a, r, g, b);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400530}
531
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400532static void extract_planes(const SkBitmap& bm, SkYUVColorSpace yuvColorSpace, PlaneData* planes) {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500533 if (kIdentity_SkYUVColorSpace == yuvColorSpace) {
534 // To test the identity color space we use JPEG YUV planes
535 yuvColorSpace = kJPEG_SkYUVColorSpace;
536 }
537
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400538 SkASSERT(!(bm.width() % 2));
539 SkASSERT(!(bm.height() % 2));
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400540 planes->fYFull.allocPixels(
541 SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
542 planes->fUFull.allocPixels(
543 SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
544 planes->fVFull.allocPixels(
545 SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400546 planes->fAFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height()));
Robert Phillips00c9f0d2019-08-02 17:17:35 -0400547 planes->fUQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2,
548 kGray_8_SkColorType, kUnpremul_SkAlphaType));
549 planes->fVQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2,
550 kGray_8_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400551
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400552 planes->fFull.allocPixels(
553 SkImageInfo::Make(bm.dimensions(), kRGBA_F32_SkColorType, kUnpremul_SkAlphaType));
Robert Phillips429f0d32019-09-11 17:03:28 -0400554 planes->fQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2,
555 kRGBA_F32_SkColorType, kUnpremul_SkAlphaType));
556
Brian Osmanac8a16c2019-10-31 10:24:12 -0400557 float mtx[20];
558 SkColorMatrix_RGB2YUV(yuvColorSpace, mtx);
559
Robert Phillips429f0d32019-09-11 17:03:28 -0400560 SkColor4f* dst = (SkColor4f *) planes->fFull.getAddr(0, 0);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400561 for (int y = 0; y < bm.height(); ++y) {
562 for (int x = 0; x < bm.width(); ++x) {
563 SkColor col = bm.getColor(x, y);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400564
Robert Phillips1c7062d2018-10-04 10:44:53 -0400565 uint8_t yuva[4];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400566
Brian Osmanac8a16c2019-10-31 10:24:12 -0400567 convert_rgba_to_yuva(mtx, col, yuva);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400568
569 *planes->fYFull.getAddr8(x, y) = yuva[0];
570 *planes->fUFull.getAddr8(x, y) = yuva[1];
571 *planes->fVFull.getAddr8(x, y) = yuva[2];
572 *planes->fAFull.getAddr8(x, y) = yuva[3];
Robert Phillips429f0d32019-09-11 17:03:28 -0400573
574 // TODO: render in F32 rather than converting here
575 dst->fR = yuva[0] / 255.0f;
576 dst->fG = yuva[1] / 255.0f;
577 dst->fB = yuva[2] / 255.0f;
578 dst->fA = yuva[3] / 255.0f;
579 ++dst;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400580 }
581 }
582
Robert Phillips429f0d32019-09-11 17:03:28 -0400583 dst = (SkColor4f *) planes->fQuarter.getAddr(0, 0);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400584 for (int y = 0; y < bm.height()/2; ++y) {
585 for (int x = 0; x < bm.width()/2; ++x) {
Robert Phillips429f0d32019-09-11 17:03:28 -0400586 uint32_t yAccum = 0, uAccum = 0, vAccum = 0, aAccum = 0;
587
588 yAccum += *planes->fYFull.getAddr8(2*x, 2*y);
589 yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y);
590 yAccum += *planes->fYFull.getAddr8(2*x, 2*y+1);
591 yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y+1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400592
593 uAccum += *planes->fUFull.getAddr8(2*x, 2*y);
594 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y);
595 uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1);
596 uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1);
597
598 *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f;
599
600 vAccum += *planes->fVFull.getAddr8(2*x, 2*y);
601 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y);
602 vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1);
603 vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1);
604
Robert Phillips1c7062d2018-10-04 10:44:53 -0400605 *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f;
Robert Phillips429f0d32019-09-11 17:03:28 -0400606
607 aAccum += *planes->fAFull.getAddr8(2*x, 2*y);
608 aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y);
609 aAccum += *planes->fAFull.getAddr8(2*x, 2*y+1);
610 aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y+1);
611
612 // TODO: render in F32 rather than converting here
613 dst->fR = yAccum / (4.0f * 255.0f);
614 dst->fG = uAccum / (4.0f * 255.0f);
615 dst->fB = vAccum / (4.0f * 255.0f);
616 dst->fA = aAccum / (4.0f * 255.0f);
617 ++dst;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400618 }
619 }
620}
621
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400622// Create a 2x2 downsampled SkBitmap. It is stored in an RG texture. It can optionally be
Robert Phillips429f0d32019-09-11 17:03:28 -0400623// uv (i.e., NV12) or vu (i.e., NV21).
Robert Phillipsbb749902019-06-10 17:20:12 -0400624static SkBitmap make_quarter_2_channel(const SkBitmap& fullY,
625 const SkBitmap& quarterU,
626 const SkBitmap& quarterV,
627 bool uv) {
628 SkBitmap result;
629
Robert Phillipsbb749902019-06-10 17:20:12 -0400630 result.allocPixels(SkImageInfo::Make(fullY.width()/2,
631 fullY.height()/2,
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400632 kR8G8_unorm_SkColorType,
Robert Phillipsbb749902019-06-10 17:20:12 -0400633 kUnpremul_SkAlphaType));
634
635 for (int y = 0; y < fullY.height()/2; ++y) {
636 for (int x = 0; x < fullY.width()/2; ++x) {
637 uint8_t u8 = *quarterU.getAddr8(x, y);
638 uint8_t v8 = *quarterV.getAddr8(x, y);
639
640 if (uv) {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400641 *result.getAddr16(x, y) = (v8 << 8) | u8;
Robert Phillipsbb749902019-06-10 17:20:12 -0400642 } else {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400643 *result.getAddr16(x, y) = (u8 << 8) | v8;
Robert Phillipsbb749902019-06-10 17:20:12 -0400644 }
645 }
646 }
647
648 return result;
649}
650
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400651// Create some flavor of a 16bits/channel bitmap from a RGBA_F32 source
652static SkBitmap make_16(const SkBitmap& src, SkColorType dstCT,
653 std::function<void(uint16_t* dstPixel, const float* srcPixel)> convert) {
Robert Phillips429f0d32019-09-11 17:03:28 -0400654 SkASSERT(src.colorType() == kRGBA_F32_SkColorType);
655
656 SkBitmap result;
657
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400658 result.allocPixels(SkImageInfo::Make(src.dimensions(), dstCT, kUnpremul_SkAlphaType));
Robert Phillips429f0d32019-09-11 17:03:28 -0400659
Robert Phillips429f0d32019-09-11 17:03:28 -0400660 for (int y = 0; y < src.height(); ++y) {
661 for (int x = 0; x < src.width(); ++x) {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400662 const float* srcPixel = (const float*) src.getAddr(x, y);
663 uint16_t* dstPixel = (uint16_t*) result.getAddr(x, y);
Robert Phillips429f0d32019-09-11 17:03:28 -0400664
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400665 convert(dstPixel, srcPixel);
Robert Phillips429f0d32019-09-11 17:03:28 -0400666 }
667 }
668
669 return result;
670}
671
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400672static uint16_t flt_2_uint16(float flt) { return SkScalarRoundToInt(flt * 65535.0f); }
Robert Phillips429f0d32019-09-11 17:03:28 -0400673
Brian Salomonf7255d72020-04-08 15:57:08 -0400674// Recombine the separate planes into some YUV format. Returns the number of planes.
675static int create_YUV(const PlaneData& planes,
676 YUVFormat yuvFormat,
677 SkBitmap resultBMs[],
678 bool opaque) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400679 int nextLayer = 0;
680
681 switch (yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400682 case kY416_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400683 resultBMs[nextLayer++] = make_16(planes.fFull, kR16G16B16A16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400684 [] (uint16_t* dstPixel, const float* srcPixel) {
685 dstPixel[0] = flt_2_uint16(srcPixel[1]); // U
686 dstPixel[1] = flt_2_uint16(srcPixel[0]); // Y
687 dstPixel[2] = flt_2_uint16(srcPixel[2]); // V
688 dstPixel[3] = flt_2_uint16(srcPixel[3]); // A
689 });
Robert Phillipsbb749902019-06-10 17:20:12 -0400690 break;
691 }
Jim Van Verth976a6b02018-10-17 15:27:19 -0400692 case kAYUV_YUVFormat: {
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400693 SkBitmap yuvaFull;
694
Jim Van Verth47133fd2018-10-19 22:09:28 -0400695 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
696 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400697
698 for (int y = 0; y < planes.fYFull.height(); ++y) {
699 for (int x = 0; x < planes.fYFull.width(); ++x) {
700
701 uint8_t Y = *planes.fYFull.getAddr8(x, y);
702 uint8_t U = *planes.fUFull.getAddr8(x, y);
703 uint8_t V = *planes.fVFull.getAddr8(x, y);
704 uint8_t A = *planes.fAFull.getAddr8(x, y);
705
706 // NOT premul!
Jim Van Verth47133fd2018-10-19 22:09:28 -0400707 // V and Y swapped to match RGBA layout
Robert Phillips2dd1b472019-03-21 09:00:20 -0400708 SkColor c = SkColorSetARGB(A, V, U, Y);
709 *yuvaFull.getAddr32(x, y) = c;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400710 }
711 }
712
713 resultBMs[nextLayer++] = yuvaFull;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400714 break;
715 }
Robert Phillips2dd1b472019-03-21 09:00:20 -0400716 case kY410_YUVFormat: {
717 SkBitmap yuvaFull;
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400718 uint32_t Y, U, V;
719 uint8_t A;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400720
721 yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(),
722 kRGBA_1010102_SkColorType,
723 kUnpremul_SkAlphaType));
724
725 for (int y = 0; y < planes.fYFull.height(); ++y) {
726 for (int x = 0; x < planes.fYFull.width(); ++x) {
727
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400728 Y = SkScalarRoundToInt((*planes.fYFull.getAddr8(x, y) / 255.0f) * 1023.0f);
729 U = SkScalarRoundToInt((*planes.fUFull.getAddr8(x, y) / 255.0f) * 1023.0f);
730 V = SkScalarRoundToInt((*planes.fVFull.getAddr8(x, y) / 255.0f) * 1023.0f);
731 A = SkScalarRoundToInt((*planes.fAFull.getAddr8(x, y) / 255.0f) * 3.0f);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400732
733 // NOT premul!
Brian Salomonf7255d72020-04-08 15:57:08 -0400734 *yuvaFull.getAddr32(x, y) = (A << 30) | (V << 20) | (Y << 10) | (U << 0);
Robert Phillips2dd1b472019-03-21 09:00:20 -0400735 }
736 }
737
738 resultBMs[nextLayer++] = yuvaFull;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400739 break;
740 }
Robert Phillipsbb749902019-06-10 17:20:12 -0400741 case kP016_YUVFormat: // fall through
Robert Phillips429f0d32019-09-11 17:03:28 -0400742 case kP010_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400743 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400744 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
745 (uint16_t* dstPixel, const float* srcPixel) {
746 uint16_t val16 = flt_2_uint16(srcPixel[0]);
747 dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0)
748 : val16;
749 });
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400750 resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_unorm_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400751 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
752 (uint16_t* dstPixel, const float* srcPixel) {
753 uint16_t u16 = flt_2_uint16(srcPixel[1]);
754 uint16_t v16 = flt_2_uint16(srcPixel[2]);
755 dstPixel[0] = tenBitsPP ? (u16 & 0xFFC0) : u16;
756 dstPixel[1] = tenBitsPP ? (v16 & 0xFFC0) : v16;
757 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400758 if (!opaque) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400759 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType,
760 [tenBitsPP = (yuvFormat == kP010_YUVFormat)]
761 (uint16_t* dstPixel, const float* srcPixel) {
762 uint16_t val16 = flt_2_uint16(srcPixel[3]);
763 dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0)
764 : val16;
765 });
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400766 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400767 return nextLayer;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400768 }
769 case kP016F_YUVFormat: {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400770 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400771 [] (uint16_t* dstPixel, const float* srcPixel) {
772 dstPixel[0] = SkFloatToHalf(srcPixel[0]);
773 });
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400774 resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_float_SkColorType,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400775 [] (uint16_t* dstPixel, const float* srcPixel) {
776 dstPixel[0] = SkFloatToHalf(srcPixel[1]);
777 dstPixel[1] = SkFloatToHalf(srcPixel[2]);
778 });
779 if (!opaque) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400780 resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType,
781 [] (uint16_t* dstPixel, const float* srcPixel) {
782 dstPixel[0] = SkFloatToHalf(srcPixel[3]);
783 });
Robert Phillips429f0d32019-09-11 17:03:28 -0400784 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400785 return nextLayer;
Robert Phillips429f0d32019-09-11 17:03:28 -0400786 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400787 case kNV12_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400788 SkBitmap uvQuarter = make_quarter_2_channel(planes.fYFull,
789 planes.fUQuarter,
790 planes.fVQuarter, true);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400791 resultBMs[nextLayer++] = planes.fYFull;
792 resultBMs[nextLayer++] = uvQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400793 break;
794 }
795 case kNV21_YUVFormat: {
Robert Phillipsbb749902019-06-10 17:20:12 -0400796 SkBitmap vuQuarter = make_quarter_2_channel(planes.fYFull,
797 planes.fUQuarter,
798 planes.fVQuarter, false);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400799 resultBMs[nextLayer++] = planes.fYFull;
800 resultBMs[nextLayer++] = vuQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400801 break;
802 }
803 case kI420_YUVFormat:
804 resultBMs[nextLayer++] = planes.fYFull;
805 resultBMs[nextLayer++] = planes.fUQuarter;
806 resultBMs[nextLayer++] = planes.fVQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400807 break;
808 case kYV12_YUVFormat:
809 resultBMs[nextLayer++] = planes.fYFull;
810 resultBMs[nextLayer++] = planes.fVQuarter;
811 resultBMs[nextLayer++] = planes.fUQuarter;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400812 break;
813 }
814
Brian Salomonf7255d72020-04-08 15:57:08 -0400815 if (!YUVAFormatPlanarConfig(yuvFormat).hasAlpha() && !opaque) {
816 resultBMs[nextLayer++] = planes.fAFull;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400817 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400818 return nextLayer;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400819}
820
Brian Salomonf7255d72020-04-08 15:57:08 -0400821static uint8_t look_up(float x1, float y1, const SkBitmap& bm, int channelIdx) {
Robert Phillips2dd1b472019-03-21 09:00:20 -0400822 SkASSERT(x1 > 0 && x1 < 1.0f);
823 SkASSERT(y1 > 0 && y1 < 1.0f);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400824 int x = SkScalarFloorToInt(x1 * bm.width());
825 int y = SkScalarFloorToInt(y1 * bm.height());
826
Brian Salomonf7255d72020-04-08 15:57:08 -0400827 auto channelMask = SkColorTypeChannelFlags(bm.colorType());
828 SkColorChannel channel;
829 SkAssertResult(YUVAPlanarConfig::ChannelIndexToChannel(channelMask, channelIdx, &channel));
830 auto ii = SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, bm.alphaType(), bm.refColorSpace());
831 uint32_t pixel;
832 SkAssertResult(bm.readPixels(ii, &pixel, sizeof(pixel), x, y));
833 int shift = static_cast<int>(channel) * 8;
834 return static_cast<uint8_t>((pixel >> shift) & 0xff);
Robert Phillips1c7062d2018-10-04 10:44:53 -0400835}
836
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400837class YUVGenerator : public SkImageGenerator {
838public:
839 YUVGenerator(const SkImageInfo& ii,
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400840 SkYUVColorSpace yuvColorSpace,
Brian Salomonf7255d72020-04-08 15:57:08 -0400841 YUVFormat yuvFormat,
842 bool externalAlphaPlane,
Jim Van Verthe24b5872018-10-29 16:26:02 -0400843 SkBitmap bitmaps[SkYUVASizeInfo::kMaxCount])
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400844 : SkImageGenerator(ii)
Brian Salomonf7255d72020-04-08 15:57:08 -0400845 , fYUVFormat(yuvFormat)
Robert Phillips2dd1b472019-03-21 09:00:20 -0400846 , fYUVColorSpace(yuvColorSpace)
Brian Salomonf7255d72020-04-08 15:57:08 -0400847 , fExternalAlphaPlane(externalAlphaPlane)
Robert Phillips2dd1b472019-03-21 09:00:20 -0400848 , fAllA8(true) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400849 SkASSERT(!externalAlphaPlane || !YUVAFormatPlanarConfig(fYUVFormat).hasAlpha());
850 int numPlanes = this->numPlanes();
851 for (int i = 0; i < numPlanes; ++i) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400852 fYUVBitmaps[i] = bitmaps[i];
Robert Phillips2dd1b472019-03-21 09:00:20 -0400853 if (kAlpha_8_SkColorType != fYUVBitmaps[i].colorType()) {
854 fAllA8 = false;
855 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400856 }
857 }
858
859protected:
860 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
861 const Options&) override {
862
863 if (kUnknown_SkColorType == fFlattened.colorType()) {
Robert Phillips2dd1b472019-03-21 09:00:20 -0400864 fFlattened.allocPixels(info);
Robert Phillipse0735522020-01-31 11:03:32 -0500865 SkASSERT(kN32_SkColorType == info.colorType());
Robert Phillips2dd1b472019-03-21 09:00:20 -0400866 SkASSERT(kPremul_SkAlphaType == info.alphaType());
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400867
Brian Osmanac8a16c2019-10-31 10:24:12 -0400868 float mtx[20];
869 SkColorMatrix_YUV2RGB(fYUVColorSpace, mtx);
870
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400871 for (int y = 0; y < info.height(); ++y) {
872 for (int x = 0; x < info.width(); ++x) {
873
Robert Phillips1c7062d2018-10-04 10:44:53 -0400874 float x1 = (x + 0.5f) / info.width();
875 float y1 = (y + 0.5f) / info.height();
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400876
Brian Salomonf7255d72020-04-08 15:57:08 -0400877 uint8_t yuva[4] = {0, 0, 0, 255};
Robert Phillips1c7062d2018-10-04 10:44:53 -0400878
Brian Salomonf7255d72020-04-08 15:57:08 -0400879 const auto& planarConfig = YUVAFormatPlanarConfig(fYUVFormat);
880 using YUVAChannel = YUVAPlanarConfig::YUVAChannel;
881 for (auto c : {YUVAChannel::kY, YUVAChannel::kU, YUVAChannel::kV}) {
882 const auto& bmp = fYUVBitmaps[planarConfig.planeIndex(c)];
883 int channelIdx = planarConfig.channelIndex(c);
884 yuva[static_cast<int>(c)] = look_up(x1, y1, bmp, channelIdx);
885 }
886 if (planarConfig.hasAlpha()) {
887 const auto& bmp = fYUVBitmaps[planarConfig.planeIndex(YUVAChannel::kA)];
888 int channelIdx = planarConfig.channelIndex(YUVAChannel::kA);
889 yuva[3] = look_up(x1, y1, bmp, channelIdx);
890 } else if (fExternalAlphaPlane) {
891 const auto& bmp = fYUVBitmaps[this->numPlanes() - 1];
892 yuva[3] = look_up(x1, y1, bmp, 0);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400893 }
894
895 // Making premul here.
Brian Salomonf7255d72020-04-08 15:57:08 -0400896 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba(mtx, yuva);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400897 }
898 }
899 }
900
901 return fFlattened.readPixels(info, pixels, rowBytes, 0, 0);
902 }
903
Jim Van Verthe24b5872018-10-29 16:26:02 -0400904 bool onQueryYUVA8(SkYUVASizeInfo* size,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400905 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
906 SkYUVColorSpace* yuvColorSpace) const override {
907
Robert Phillips2dd1b472019-03-21 09:00:20 -0400908 if (!fAllA8) {
909 return false;
910 }
Brian Salomonf7255d72020-04-08 15:57:08 -0400911 const auto& planarConfig = YUVAFormatPlanarConfig(fYUVFormat);
912 if (!planarConfig.getYUVAIndices(fYUVBitmaps, this->numPlanes(), fExternalAlphaPlane,
913 yuvaIndices)) {
914 return false;
915 }
Jim Van Verth8f11e432018-10-18 14:36:59 -0400916 *yuvColorSpace = fYUVColorSpace;
917
Brian Salomonf7255d72020-04-08 15:57:08 -0400918 int numPlanes = this->numPlanes();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400919 int i = 0;
Brian Salomonf7255d72020-04-08 15:57:08 -0400920 for (; i < numPlanes; ++i) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400921 size->fSizes[i].fWidth = fYUVBitmaps[i].width();
922 size->fSizes[i].fHeight = fYUVBitmaps[i].height();
923 size->fWidthBytes[i] = fYUVBitmaps[i].rowBytes();
924 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400925 for ( ; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400926 size->fSizes[i].fWidth = 0;
927 size->fSizes[i].fHeight = 0;
928 size->fWidthBytes[i] = 0;
Jim Van Verthf99a6742018-10-18 16:13:18 +0000929 }
Jim Van Verth0c583af2018-10-18 10:31:59 -0400930
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400931 return true;
932 }
933
Jim Van Verthe24b5872018-10-29 16:26:02 -0400934 bool onGetYUVA8Planes(const SkYUVASizeInfo&, const SkYUVAIndex[SkYUVAIndex::kIndexCount],
935 void* planes[SkYUVASizeInfo::kMaxCount]) override {
Robert Phillips2dd1b472019-03-21 09:00:20 -0400936 SkASSERT(fAllA8);
Brian Salomonf7255d72020-04-08 15:57:08 -0400937 int numPlanes = this->numPlanes();
938 for (int i = 0; i < numPlanes; ++i) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400939 planes[i] = fYUVBitmaps[i].getPixels();
940 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400941 return true;
942 }
943
944private:
Brian Salomonf7255d72020-04-08 15:57:08 -0400945 int numPlanes() const {
946 return YUVAFormatPlanarConfig(fYUVFormat).numPlanes() + SkToInt(fExternalAlphaPlane);
947 }
948
949 YUVFormat fYUVFormat;
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400950 SkYUVColorSpace fYUVColorSpace;
Brian Salomonf7255d72020-04-08 15:57:08 -0400951 bool fExternalAlphaPlane;
Jim Van Verthe24b5872018-10-29 16:26:02 -0400952 SkBitmap fYUVBitmaps[SkYUVASizeInfo::kMaxCount];
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400953 SkBitmap fFlattened;
Robert Phillips2dd1b472019-03-21 09:00:20 -0400954 bool fAllA8; // are all the SkBitmaps in "fYUVBitmaps" A8?
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400955};
956
957static sk_sp<SkImage> make_yuv_gen_image(const SkImageInfo& ii,
Brian Salomonf7255d72020-04-08 15:57:08 -0400958 YUVFormat yuvFormat,
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400959 SkYUVColorSpace yuvColorSpace,
Brian Salomonf7255d72020-04-08 15:57:08 -0400960 bool opaque,
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400961 SkBitmap bitmaps[]) {
Brian Salomonf7255d72020-04-08 15:57:08 -0400962 bool externalAlphaPlane = !opaque && !YUVAFormatPlanarConfig(yuvFormat).hasAlpha();
963 std::unique_ptr<SkImageGenerator> gen(
964 new YUVGenerator(ii, yuvColorSpace, yuvFormat, externalAlphaPlane, bitmaps));
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400965
966 return SkImage::MakeFromGenerator(std::move(gen));
967}
968
969static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) {
Brian Osman2b73e662019-11-01 10:02:24 -0400970 static const char* kYUVColorSpaceNames[] = { "JPEG", "601", "709", "2020", "Identity" };
Brian Salomon4dea72a2019-12-18 10:43:10 -0500971 static_assert(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace + 1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400972
Mike Reed91919132019-01-02 12:21:01 -0500973 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500974 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -0500975 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400976
977 SkRect textRect;
978 SkString colLabel;
979
980 colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400981 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400982 int y = textRect.height();
983
Mike Reed91919132019-01-02 12:21:01 -0500984 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400985
986 colLabel.printf("%s", opaque ? "Opaque" : "Transparent");
Mike Reed71f5a0b2018-10-25 16:12:39 -0400987
Ben Wagner51e15a62019-05-07 15:38:46 -0400988 font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400989 y += textRect.height();
990
Mike Reed91919132019-01-02 12:21:01 -0500991 SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400992}
993
994static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) {
Robert Phillipsbb749902019-06-10 17:20:12 -0400995 static const char* kYUVFormatNames[] = {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400996 "P016", "P010", "P016F", "Y416", "AYUV", "Y410", "NV12", "NV21", "I420", "YV12"
Robert Phillipsbb749902019-06-10 17:20:12 -0400997 };
Brian Salomon4dea72a2019-12-18 10:43:10 -0500998 static_assert(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat + 1);
Robert Phillipsbfa76f22018-10-03 12:12:26 -0400999
Mike Reed91919132019-01-02 12:21:01 -05001000 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -05001001 SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16);
Mike Reed91919132019-01-02 12:21:01 -05001002 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001003
1004 SkRect textRect;
1005 SkString rowLabel;
1006
1007 rowLabel.printf("%s", kYUVFormatNames[yuvFormat]);
Ben Wagner51e15a62019-05-07 15:38:46 -04001008 font.measureText(rowLabel.c_str(), rowLabel.size(), SkTextEncoding::kUTF8, &textRect);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001009 y += kTileWidthHeight/2 + textRect.height()/2;
1010
Hal Canary89a644b2019-01-07 09:36:09 -05001011 canvas->drawString(rowLabel, 0, y, font, paint);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001012}
1013
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001014static GrBackendTexture create_yuva_texture(GrDirectContext* context, const SkBitmap& bm, int index,
Robert Phillips98c39ba2020-06-26 10:01:19 -04001015 YUVABackendReleaseContext* releaseContext) {
1016 return context->createBackendTexture(bm.pixmap(), GrRenderable::kNo, GrProtected::kNo,
1017 YUVABackendReleaseContext::CreationCompleteProc(index),
1018 releaseContext);
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001019}
1020
Robert Phillips0a22ba82019-03-06 12:36:47 -05001021static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
1022 static const float kJPEGConversionMatrix[20] = {
Mike Reede869a1e2019-04-30 12:18:54 -04001023 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
1024 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
1025 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
Robert Phillips0a22ba82019-03-06 12:36:47 -05001026 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
1027 };
1028
Mike Reede869a1e2019-04-30 12:18:54 -04001029 return SkColorFilters::Matrix(kJPEGConversionMatrix);
Robert Phillips0a22ba82019-03-06 12:36:47 -05001030}
1031
Robert Phillips99044e12020-01-29 08:37:01 -05001032// Get the SkColorType to use when creating an SkSurface wrapping 'format'.
1033static SkColorType get_color_type(const GrBackendFormat& format) {
1034
1035 GrGLFormat glFormat = format.asGLFormat();
1036 if (GrGLFormat::kUnknown != glFormat) {
1037 switch (glFormat) {
1038 case GrGLFormat::kLUMINANCE8: // fall through
1039 case GrGLFormat::kR8: // fall through
1040 case GrGLFormat::kALPHA8: return kAlpha_8_SkColorType;
1041 case GrGLFormat::kRG8: return kR8G8_unorm_SkColorType;
1042 case GrGLFormat::kRGB8: return kRGB_888x_SkColorType;
1043 case GrGLFormat::kRGBA8: return kRGBA_8888_SkColorType;
1044 case GrGLFormat::kBGRA8: return kBGRA_8888_SkColorType;
1045 case GrGLFormat::kRGB10_A2: return kRGBA_1010102_SkColorType;
1046 case GrGLFormat::kLUMINANCE16F: // fall through
1047 case GrGLFormat::kR16F: return kA16_float_SkColorType;
1048 case GrGLFormat::kRG16F: return kR16G16_float_SkColorType;
1049 case GrGLFormat::kR16: return kA16_unorm_SkColorType;
1050 case GrGLFormat::kRG16: return kR16G16_unorm_SkColorType;
1051 case GrGLFormat::kRGBA16: return kR16G16B16A16_unorm_SkColorType;
1052 default: return kUnknown_SkColorType;
1053 }
1054
1055 SkUNREACHABLE;
1056 }
1057
1058 VkFormat vkFormat;
1059 if (format.asVkFormat(&vkFormat)) {
1060 switch (vkFormat) {
1061 case VK_FORMAT_R8_UNORM: return kAlpha_8_SkColorType;
1062 case VK_FORMAT_R8G8_UNORM: return kR8G8_unorm_SkColorType;
1063 case VK_FORMAT_R8G8B8_UNORM: return kRGB_888x_SkColorType;
1064 case VK_FORMAT_R8G8B8A8_UNORM: return kRGBA_8888_SkColorType;
1065 case VK_FORMAT_B8G8R8A8_UNORM: return kBGRA_8888_SkColorType;
1066 case VK_FORMAT_A2B10G10R10_UNORM_PACK32: return kRGBA_1010102_SkColorType;
Robert Phillips9a30ee02020-04-29 08:58:39 -04001067 case VK_FORMAT_A2R10G10B10_UNORM_PACK32: return kBGRA_1010102_SkColorType;
Robert Phillips99044e12020-01-29 08:37:01 -05001068 case VK_FORMAT_R16_SFLOAT: return kA16_float_SkColorType;
1069 case VK_FORMAT_R16G16_SFLOAT: return kR16G16_float_SkColorType;
1070 case VK_FORMAT_R16_UNORM: return kA16_unorm_SkColorType;
1071 case VK_FORMAT_R16G16_UNORM: return kR16G16_unorm_SkColorType;
1072 case VK_FORMAT_R16G16B16A16_UNORM: return kR16G16B16A16_unorm_SkColorType;
1073 default: return kUnknown_SkColorType;
1074 }
1075
1076 SkUNREACHABLE;
1077 }
1078
1079 return kUnknown_SkColorType;
1080}
1081
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001082namespace skiagm {
1083
1084// This GM creates an opaque and transparent bitmap, extracts the planes and then recombines
1085// them into various YUV formats. It then renders the results in the grid:
1086//
Robert Phillips2dd1b472019-03-21 09:00:20 -04001087// JPEG 601 709 Identity
1088// Transparent Opaque Transparent Opaque Transparent Opaque Transparent Opaque
Robert Phillipsbb749902019-06-10 17:20:12 -04001089// originals
1090// P016
1091// P010
Robert Phillips17a3a0b2019-09-18 13:56:54 -04001092// P016F
Robert Phillipsbb749902019-06-10 17:20:12 -04001093// Y416
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001094// AYUV
Robert Phillips2dd1b472019-03-21 09:00:20 -04001095// Y410
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001096// NV12
1097// NV21
1098// I420
1099// YV12
1100class WackyYUVFormatsGM : public GM {
1101public:
Robert Phillips99044e12020-01-29 08:37:01 -05001102 WackyYUVFormatsGM(bool useTargetColorSpace, bool useDomain, bool quarterSize)
Michael Ludwiga6a84002019-04-12 15:03:02 -04001103 : fUseTargetColorSpace(useTargetColorSpace)
Robert Phillips99044e12020-01-29 08:37:01 -05001104 , fUseDomain(useDomain)
1105 , fQuarterSize(quarterSize) {
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001106 this->setBGColor(0xFFCCCCCC);
1107 }
1108
1109protected:
1110
1111 SkString onShortName() override {
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001112 SkString name("wacky_yuv_formats");
1113 if (fUseTargetColorSpace) {
1114 name += "_cs";
1115 }
Michael Ludwiga6a84002019-04-12 15:03:02 -04001116 if (fUseDomain) {
1117 name += "_domain";
1118 }
Robert Phillips99044e12020-01-29 08:37:01 -05001119 if (fQuarterSize) {
1120 name += "_qtr";
1121 }
Robert Phillips2dd1b472019-03-21 09:00:20 -04001122
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001123 return name;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001124 }
1125
1126 SkISize onISize() override {
Robert Phillips17a3a0b2019-09-18 13:56:54 -04001127 int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x #-color-spaces
1128 int numRows = 1 + (kLast_YUVFormat + 1); // original + #-yuv-formats
Michael Ludwiga6a84002019-04-12 15:03:02 -04001129 int wh = SkScalarCeilToInt(kTileWidthHeight * (fUseDomain ? 1.5f : 1.f));
1130 return SkISize::Make(kLabelWidth + numCols * (wh + kPad),
1131 kLabelHeight + numRows * (wh + kPad));
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001132 }
1133
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001134 void createBitmaps() {
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001135 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
1136 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
1137 float innerRadius = 20.0f;
1138
1139 {
1140 // transparent
1141 SkTDArray<SkRect> circles;
1142 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001143 fOriginalBMs[0] = make_bitmap(kRGBA_8888_SkColorType, path, circles, false, fUseDomain);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001144 }
1145
1146 {
1147 // opaque
1148 SkTDArray<SkRect> circles;
1149 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001150 fOriginalBMs[1] = make_bitmap(kRGBA_8888_SkColorType, path, circles, true, fUseDomain);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001151 }
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001152
1153 if (fUseTargetColorSpace) {
1154 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
1155 }
Robert Phillips51c89e42018-10-05 13:30:43 -04001156 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001157
Robert Phillips99044e12020-01-29 08:37:01 -05001158 // Resize all the backend textures in 'yuvaTextures' to a quarter their size.
1159 sk_sp<SkImage> resizeOnGpu(GrContext* context,
1160 YUVFormat yuvFormat,
1161 SkYUVColorSpace yuvColorSpace,
1162 bool opaque,
1163 const GrBackendTexture yuvaTextures[],
1164 const SkYUVAIndex yuvaIndices[4],
1165 int numTextures,
1166 SkISize imageSize) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001167 auto releaseContext = new YUVABackendReleaseContext(context);
Robert Phillips99044e12020-01-29 08:37:01 -05001168
1169 for (int i = 0; i < numTextures; ++i) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001170 const GrBackendTexture& curTex = yuvaTextures[i];
1171
1172 SkColorType ct = get_color_type(curTex.getBackendFormat());
Robert Phillips99044e12020-01-29 08:37:01 -05001173 if (ct == kUnknown_SkColorType || !context->colorTypeSupportedAsSurface(ct)) {
Robert Phillips98c39ba2020-06-26 10:01:19 -04001174 YUVABackendReleaseContext::Unwind(context, releaseContext, true);
Robert Phillips99044e12020-01-29 08:37:01 -05001175 return nullptr;
1176 }
1177
Robert Phillipse0735522020-01-31 11:03:32 -05001178 if (ct == kRGBA_8888_SkColorType || ct == kRGBA_1010102_SkColorType) {
1179 // We disallow resizing AYUV and Y410 formats on the GPU bc resizing them w/ a
1180 // premul draw combines the YUV channels w/ the A channel in an inappropriate
1181 // manner.
Robert Phillips98c39ba2020-06-26 10:01:19 -04001182 YUVABackendReleaseContext::Unwind(context, releaseContext, true);
Robert Phillipse0735522020-01-31 11:03:32 -05001183 return nullptr;
1184 }
1185
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001186 SkISize shrunkPlaneSize = {curTex.width() / 2, curTex.height() / 2 };
Robert Phillips99044e12020-01-29 08:37:01 -05001187
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001188 sk_sp<SkImage> wrappedOrig = SkImage::MakeFromTexture(context, curTex,
Robert Phillips99044e12020-01-29 08:37:01 -05001189 kTopLeft_GrSurfaceOrigin,
1190 ct,
1191 kPremul_SkAlphaType,
1192 nullptr);
1193
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001194 GrBackendTexture tmp = context->createBackendTexture(shrunkPlaneSize.width(),
1195 shrunkPlaneSize.height(),
1196 curTex.getBackendFormat(),
1197 GrMipMapped::kNo,
1198 GrRenderable::kYes);
1199 if (!tmp.isValid()) {
Robert Phillips98c39ba2020-06-26 10:01:19 -04001200 YUVABackendReleaseContext::Unwind(context, releaseContext, true);
Robert Phillips99044e12020-01-29 08:37:01 -05001201 return nullptr;
1202 }
1203
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001204 releaseContext->set(i, tmp);
Robert Phillips98c39ba2020-06-26 10:01:19 -04001205 // uninitialized beTextures don't have any pending work
1206 releaseContext->setCreationComplete(i);
Robert Phillips99044e12020-01-29 08:37:01 -05001207
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001208 sk_sp<SkSurface> s = SkSurface::MakeFromBackendTexture(context, tmp,
Robert Phillips99044e12020-01-29 08:37:01 -05001209 kTopLeft_GrSurfaceOrigin, 0,
1210 ct, nullptr, nullptr);
1211 if (!s) {
Robert Phillips98c39ba2020-06-26 10:01:19 -04001212 YUVABackendReleaseContext::Unwind(context, releaseContext, true);
Robert Phillips99044e12020-01-29 08:37:01 -05001213 return nullptr;
1214 }
1215 SkCanvas* c = s->getCanvas();
1216
1217 SkPaint paint;
1218 paint.setBlendMode(SkBlendMode::kSrc);
1219
1220 c->drawImageRect(wrappedOrig,
1221 SkRect::MakeWH(shrunkPlaneSize.width(), shrunkPlaneSize.height()),
1222 &paint);
1223
Greg Daniel0a2464f2020-05-14 15:45:44 -04001224 s->flushAndSubmit();
Robert Phillips99044e12020-01-29 08:37:01 -05001225 }
1226
1227 SkISize shrunkImageSize = { imageSize.width() / 2, imageSize.height() / 2 };
1228
1229 return SkImage::MakeFromYUVATextures(context,
1230 yuvColorSpace,
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001231 releaseContext->beTextures(),
Robert Phillips99044e12020-01-29 08:37:01 -05001232 yuvaIndices,
1233 shrunkImageSize,
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001234 kTopLeft_GrSurfaceOrigin,
1235 nullptr,
1236 YUVABackendReleaseContext::Release,
1237 releaseContext);
Robert Phillips99044e12020-01-29 08:37:01 -05001238 }
1239
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001240 bool createImages(GrDirectContext* context) {
Jim Van Verth9bf81202018-10-30 15:53:36 -04001241 int counter = 0;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001242 for (bool opaque : { false, true }) {
1243 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
1244 PlaneData planes;
1245 extract_planes(fOriginalBMs[opaque], (SkYUVColorSpace) cs, &planes);
1246
Brian Salomonf7255d72020-04-08 15:57:08 -04001247 for (int f = kP016_YUVFormat; f <= kLast_YUVFormat; ++f) {
1248 auto format = static_cast<YUVFormat>(f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001249 SkBitmap resultBMs[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001250
Brian Salomonf7255d72020-04-08 15:57:08 -04001251 int numTextures = create_YUV(planes, format, resultBMs, opaque);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001252
Robert Phillips51c89e42018-10-05 13:30:43 -04001253 if (context) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001254 fGpuGeneratedImages = true;
1255
Chris Dalton382b1222019-02-07 10:05:55 +00001256 if (context->abandoned()) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001257 return false;
Chris Dalton382b1222019-02-07 10:05:55 +00001258 }
1259
Brian Salomonf7255d72020-04-08 15:57:08 -04001260 if (!is_format_natively_supported(context, format)) {
Robert Phillipsf47717a2019-09-19 13:04:33 -04001261 continue;
1262 }
1263
Robert Phillips98c39ba2020-06-26 10:01:19 -04001264 auto releaseCtx = new YUVABackendReleaseContext(context);
Robert Phillips51c89e42018-10-05 13:30:43 -04001265
Jim Van Verth60ac5d02018-12-06 13:11:53 -05001266 for (int i = 0; i < numTextures; ++i) {
Robert Phillips98c39ba2020-06-26 10:01:19 -04001267 GrBackendTexture tmp = create_yuva_texture(context, resultBMs[i], i,
1268 releaseCtx);
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001269 if (!tmp.isValid()) {
Robert Phillips98c39ba2020-06-26 10:01:19 -04001270 YUVABackendReleaseContext::Unwind(context, releaseCtx, false);
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001271 return false;
Brian Salomonf2580c02019-01-30 09:13:31 -05001272 }
Robert Phillips98c39ba2020-06-26 10:01:19 -04001273 releaseCtx->set(i, tmp);
Robert Phillips51c89e42018-10-05 13:30:43 -04001274 }
1275
Brian Salomonf7255d72020-04-08 15:57:08 -04001276 SkYUVAIndex yuvaIndices[4];
1277 const auto& planarConfig = YUVAFormatPlanarConfig(format);
1278 bool externalAlphaPlane = !opaque && !planarConfig.hasAlpha();
Robert Phillips98c39ba2020-06-26 10:01:19 -04001279 if (!planarConfig.getYUVAIndices(releaseCtx->beTextures(), numTextures,
Brian Salomonf7255d72020-04-08 15:57:08 -04001280 externalAlphaPlane, yuvaIndices)) {
Robert Phillips98c39ba2020-06-26 10:01:19 -04001281 YUVABackendReleaseContext::Unwind(context, releaseCtx, false);
Brian Salomonf7255d72020-04-08 15:57:08 -04001282 continue;
1283 }
1284
Robert Phillips99044e12020-01-29 08:37:01 -05001285 if (fQuarterSize) {
Brian Salomonf7255d72020-04-08 15:57:08 -04001286 fImages[opaque][cs][format] =
1287 this->resizeOnGpu(context,
1288 format,
1289 (SkYUVColorSpace)cs,
1290 opaque,
Robert Phillips98c39ba2020-06-26 10:01:19 -04001291 releaseCtx->beTextures(),
Brian Salomonf7255d72020-04-08 15:57:08 -04001292 yuvaIndices,
1293 numTextures,
1294 fOriginalBMs[opaque].dimensions());
Robert Phillips98c39ba2020-06-26 10:01:19 -04001295 YUVABackendReleaseContext::Unwind(context, releaseCtx, true);
Robert Phillips99044e12020-01-29 08:37:01 -05001296 } else {
1297 int counterMod = counter % 3;
1298 if (fUseDomain && counterMod == 0) {
1299 // Copies flatten to RGB when they copy the YUVA data, which doesn't
1300 // know about the intended domain and the domain padding bleeds in
1301 counterMod = 1;
1302 }
1303
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001304 const SkISize imgSize { fOriginalBMs[opaque].width(),
1305 fOriginalBMs[opaque].height() };
1306
Robert Phillips99044e12020-01-29 08:37:01 -05001307 switch (counterMod) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001308 case 0:
1309 fImages[opaque][cs][format] = SkImage::MakeFromYUVATexturesCopy(
1310 context,
1311 (SkYUVColorSpace)cs,
Robert Phillips98c39ba2020-06-26 10:01:19 -04001312 releaseCtx->beTextures(),
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001313 yuvaIndices,
1314 imgSize,
1315 kTopLeft_GrSurfaceOrigin);
Robert Phillips98c39ba2020-06-26 10:01:19 -04001316 YUVABackendReleaseContext::Unwind(context, releaseCtx, true);
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001317 break;
1318 case 1:
1319 fImages[opaque][cs][format] = SkImage::MakeFromYUVATextures(
1320 context,
1321 (SkYUVColorSpace)cs,
Robert Phillips98c39ba2020-06-26 10:01:19 -04001322 releaseCtx->beTextures(),
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001323 yuvaIndices,
1324 imgSize,
1325 kTopLeft_GrSurfaceOrigin,
1326 nullptr,
1327 YUVABackendReleaseContext::Release,
Robert Phillips98c39ba2020-06-26 10:01:19 -04001328 releaseCtx);
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001329 break;
1330 case 2:
1331 default: {
1332 // TODO: we did a lot of work to delete these here
Robert Phillips98c39ba2020-06-26 10:01:19 -04001333 YUVABackendReleaseContext::Unwind(context, releaseCtx, false);
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001334
1335 SkPixmap yuvaPixmaps[4];
1336 for (int i = 0; i < numTextures; ++i) {
1337 yuvaPixmaps[i] = resultBMs[i].pixmap();
1338 }
1339
1340 fImages[opaque][cs][format] = SkImage::MakeFromYUVAPixmaps(
1341 context,
1342 (SkYUVColorSpace)cs,
1343 yuvaPixmaps,
1344 yuvaIndices,
1345 imgSize,
1346 kTopLeft_GrSurfaceOrigin, true);
1347 break;
1348 }
Robert Phillips99044e12020-01-29 08:37:01 -05001349 }
1350 ++counter;
Jim Van Verth9bf81202018-10-30 15:53:36 -04001351 }
1352 } else {
Robert Phillipse0735522020-01-31 11:03:32 -05001353 SkImageInfo ii = SkImageInfo::MakeN32(fOriginalBMs[opaque].width(),
1354 fOriginalBMs[opaque].height(),
1355 kPremul_SkAlphaType);
Brian Salomonf7255d72020-04-08 15:57:08 -04001356 fImages[opaque][cs][format] = make_yuv_gen_image(
1357 ii, format, (SkYUVColorSpace)cs, opaque, resultBMs);
Robert Phillips51c89e42018-10-05 13:30:43 -04001358 }
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001359 }
1360 }
1361 }
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001362
1363 if (context) {
1364 // Some backends (e.g., Vulkan) require all work be completed for backend textures
1365 // before they are deleted. Since we don't know when we'll next have access to a
1366 // direct context, flush all the work now.
Greg Danielce9f0162020-06-30 13:42:46 -04001367 context->flush();
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001368 context->submit(true);
1369 }
1370
1371 return true;
1372 }
1373
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001374 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001375 this->createBitmaps();
1376
1377 if (context && context->abandoned()) {
1378 // This isn't a GpuGM so a null 'context' is okay but an abandoned context
1379 // if forbidden.
1380 return DrawResult::kSkip;
1381 }
1382
1383 if (!this->createImages(context)) {
1384 *errorMsg = "Failed to create YUV images";
1385 return DrawResult::kFail;
1386 }
1387
1388 return DrawResult::kOk;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001389 }
1390
Robert Phillipsb795bea2020-06-25 12:38:53 -04001391 void onGpuTeardown() override {
1392 for (int i = 0; i < 2; ++i) {
1393 for (int j = 0; j <= kLastEnum_SkYUVColorSpace; ++j) {
1394 for (int k = 0; k <= kLast_YUVFormat; ++k) {
1395 fImages[i][j][k] = nullptr;
1396 }
1397 }
1398 }
1399 }
1400
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001401 void onDraw(SkCanvas* canvas) override {
Robert Phillips99044e12020-01-29 08:37:01 -05001402 float cellWidth = kTileWidthHeight, cellHeight = kTileWidthHeight;
1403 if (fUseDomain) {
1404 cellWidth *= 1.5f;
1405 cellHeight *= 1.5f;
1406 }
1407
1408 SkRect origSrcRect = SkRect::MakeWH(fOriginalBMs[0].width(), fOriginalBMs[0].height());
1409
Michael Ludwiga6a84002019-04-12 15:03:02 -04001410 SkRect srcRect = SkRect::MakeWH(fOriginalBMs[0].width(), fOriginalBMs[0].height());
1411 SkRect dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, srcRect.width(), srcRect.height());
Robert Phillips99044e12020-01-29 08:37:01 -05001412 if (fQuarterSize) {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001413 if (fGpuGeneratedImages) {
Robert Phillips99044e12020-01-29 08:37:01 -05001414 // The src is only shrunk on the GPU
1415 srcRect = SkRect::MakeWH(fOriginalBMs[0].width()/2.0f,
1416 fOriginalBMs[0].height()/2.0f);
1417 }
1418 // but the dest is always drawn smaller
1419 dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f,
1420 fOriginalBMs[0].width()/2.0f,
1421 fOriginalBMs[0].height()/2.0f);
1422 }
Michael Ludwiga6a84002019-04-12 15:03:02 -04001423
1424 SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint;
1425 if (fUseDomain) {
1426 srcRect.inset(kDomainPadding, kDomainPadding);
Robert Phillips99044e12020-01-29 08:37:01 -05001427 origSrcRect.inset(kDomainPadding, kDomainPadding);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001428 // Draw a larger rectangle to ensure bilerp filtering would normally read outside the
1429 // srcRect and hit the red pixels, if strict constraint weren't used.
1430 dstRect.fRight = kLabelWidth + 1.5f * srcRect.width();
1431 dstRect.fBottom = 1.5f * srcRect.height();
1432 constraint = SkCanvas::kStrict_SrcRectConstraint;
1433 }
1434
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001435 for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) {
Robert Phillips0a22ba82019-03-06 12:36:47 -05001436 SkPaint paint;
Michael Ludwiga6a84002019-04-12 15:03:02 -04001437 paint.setFilterQuality(kLow_SkFilterQuality);
Robert Phillips0a22ba82019-03-06 12:36:47 -05001438 if (kIdentity_SkYUVColorSpace == cs) {
1439 // The identity color space needs post processing to appear correctly
1440 paint.setColorFilter(yuv_to_rgb_colorfilter());
1441 }
1442
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001443 for (int opaque : { 0, 1 }) {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001444 dstRect.offsetTo(dstRect.fLeft, kLabelHeight);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001445
Robert Phillips99044e12020-01-29 08:37:01 -05001446 draw_col_label(canvas, dstRect.fLeft + cellWidth / 2, cs, opaque);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001447
Robert Phillips99044e12020-01-29 08:37:01 -05001448 canvas->drawBitmapRect(fOriginalBMs[opaque], origSrcRect, dstRect,
1449 nullptr, constraint);
1450 dstRect.offset(0.f, cellHeight + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001451
Robert Phillipsbb749902019-06-10 17:20:12 -04001452 for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) {
Michael Ludwiga6a84002019-04-12 15:03:02 -04001453 draw_row_label(canvas, dstRect.fTop, format);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001454 if (fUseTargetColorSpace && fImages[opaque][cs][format]) {
Robert Phillips0a22ba82019-03-06 12:36:47 -05001455 // Making a CS-specific version of a kIdentity_SkYUVColorSpace YUV image
1456 // doesn't make a whole lot of sense. The colorSpace conversion will
1457 // operate on the YUV components rather than the RGB components.
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001458 sk_sp<SkImage> csImage =
1459 fImages[opaque][cs][format]->makeColorSpace(fTargetColorSpace);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001460 canvas->drawImageRect(csImage, srcRect, dstRect, &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001461 } else {
Robert Phillips99044e12020-01-29 08:37:01 -05001462 canvas->drawImageRect(fImages[opaque][cs][format], srcRect, dstRect,
1463 &paint, constraint);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -05001464 }
Robert Phillips99044e12020-01-29 08:37:01 -05001465 dstRect.offset(0.f, cellHeight + kPad);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001466 }
1467
Robert Phillips99044e12020-01-29 08:37:01 -05001468 dstRect.offset(cellWidth + kPad, 0.f);
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001469 }
1470 }
1471 }
1472
1473private:
Robert Phillips2dd1b472019-03-21 09:00:20 -04001474 SkBitmap fOriginalBMs[2];
1475 sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace + 1][kLast_YUVFormat + 1];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001476 bool fUseTargetColorSpace;
Michael Ludwiga6a84002019-04-12 15:03:02 -04001477 bool fUseDomain;
Robert Phillips99044e12020-01-29 08:37:01 -05001478 bool fQuarterSize;
Robert Phillips2dd1b472019-03-21 09:00:20 -04001479 sk_sp<SkColorSpace> fTargetColorSpace;
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001480 bool fGpuGeneratedImages = false;
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001481
1482 typedef GM INHERITED;
1483};
1484
1485//////////////////////////////////////////////////////////////////////////////
1486
Robert Phillips99044e12020-01-29 08:37:01 -05001487DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ false, /* quarterSize */ false);)
1488DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ false, /* quarterSize */ true);)
1489DEF_GM(return new WackyYUVFormatsGM(/* cs */ true, /* domain */ false, /* quarterSize */ false);)
1490DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ true, /* quarterSize */ false);)
Brian Osmane9560492019-02-05 17:00:03 -05001491
Chris Dalton3a778372019-02-07 15:23:36 -07001492class YUVMakeColorSpaceGM : public GpuGM {
Brian Osmane9560492019-02-05 17:00:03 -05001493public:
1494 YUVMakeColorSpaceGM() {
1495 this->setBGColor(0xFFCCCCCC);
1496 }
1497
1498protected:
1499 SkString onShortName() override {
1500 return SkString("yuv_make_color_space");
1501 }
1502
1503 SkISize onISize() override {
1504 int numCols = 4; // (transparent, opaque) x (untagged, tagged)
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001505 int numRows = 5; // original, YUV, subset, makeNonTextureImage, readPixels
Brian Osmane9560492019-02-05 17:00:03 -05001506 return SkISize::Make(numCols * (kTileWidthHeight + kPad) + kPad,
1507 numRows * (kTileWidthHeight + kPad) + kPad);
1508 }
1509
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001510 void createBitmaps() {
Brian Osmane9560492019-02-05 17:00:03 -05001511 SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f };
1512 float outerRadius = kTileWidthHeight/2.0f - 20.0f;
1513 float innerRadius = 20.0f;
1514
1515 {
1516 // transparent
1517 SkTDArray<SkRect> circles;
1518 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001519 fOriginalBMs[0] = make_bitmap(kN32_SkColorType, path, circles, false, false);
Brian Osmane9560492019-02-05 17:00:03 -05001520 }
1521
1522 {
1523 // opaque
1524 SkTDArray<SkRect> circles;
1525 SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles);
Michael Ludwiga6a84002019-04-12 15:03:02 -04001526 fOriginalBMs[1] = make_bitmap(kN32_SkColorType, path, circles, true, false);
Brian Osmane9560492019-02-05 17:00:03 -05001527 }
1528
1529 fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin();
1530 }
1531
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001532 bool createImages(GrDirectContext* context) {
Brian Osmane9560492019-02-05 17:00:03 -05001533 for (bool opaque : { false, true }) {
1534 PlaneData planes;
1535 extract_planes(fOriginalBMs[opaque], kJPEG_SkYUVColorSpace, &planes);
1536
1537 SkBitmap resultBMs[4];
Robert Phillips2dd1b472019-03-21 09:00:20 -04001538
Brian Salomonf7255d72020-04-08 15:57:08 -04001539 create_YUV(planes, kAYUV_YUVFormat, resultBMs, opaque);
Robert Phillips2dd1b472019-03-21 09:00:20 -04001540
Brian Salomonf7255d72020-04-08 15:57:08 -04001541 auto& planarConfig = YUVAFormatPlanarConfig(kAYUV_YUVFormat);
1542 int numPlanes = planarConfig.numPlanes();
Brian Osmane9560492019-02-05 17:00:03 -05001543
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001544 auto releaseContext = new YUVABackendReleaseContext(context);
1545 auto srgbReleaseContext = new YUVABackendReleaseContext(context);
1546
Brian Salomonf7255d72020-04-08 15:57:08 -04001547 for (int i = 0; i < numPlanes; ++i) {
Robert Phillips98c39ba2020-06-26 10:01:19 -04001548 GrBackendTexture tmp = create_yuva_texture(context, resultBMs[i], i,
1549 releaseContext);
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001550 if (!tmp.isValid()) {
Robert Phillips98c39ba2020-06-26 10:01:19 -04001551 YUVABackendReleaseContext::Unwind(context, releaseContext, false);
1552 YUVABackendReleaseContext::Unwind(context, srgbReleaseContext, false);
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001553 return false;
Brian Osmane9560492019-02-05 17:00:03 -05001554 }
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001555
1556 releaseContext->set(i, tmp);
1557
Robert Phillips98c39ba2020-06-26 10:01:19 -04001558 tmp = create_yuva_texture(context, resultBMs[i], i, srgbReleaseContext);
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001559 if (!tmp.isValid()) {
Robert Phillips98c39ba2020-06-26 10:01:19 -04001560 YUVABackendReleaseContext::Unwind(context, releaseContext, false);
1561 YUVABackendReleaseContext::Unwind(context, srgbReleaseContext, false);
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001562 return false;
1563 }
1564
1565 srgbReleaseContext->set(i, tmp);
Brian Osmane9560492019-02-05 17:00:03 -05001566 }
1567
Brian Salomonf7255d72020-04-08 15:57:08 -04001568 SkYUVAIndex yuvaIndices[4];
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001569 planarConfig.getYUVAIndices(releaseContext->beTextures(), numPlanes,
1570 false, yuvaIndices);
1571
Brian Osmane9560492019-02-05 17:00:03 -05001572 fImages[opaque][0] = SkImage::MakeFromYUVATextures(
1573 context,
1574 kJPEG_SkYUVColorSpace,
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001575 releaseContext->beTextures(),
Brian Osmane9560492019-02-05 17:00:03 -05001576 yuvaIndices,
1577 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001578 kTopLeft_GrSurfaceOrigin, nullptr,
1579 YUVABackendReleaseContext::Release, releaseContext);
Brian Osmane9560492019-02-05 17:00:03 -05001580 fImages[opaque][1] = SkImage::MakeFromYUVATextures(
1581 context,
1582 kJPEG_SkYUVColorSpace,
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001583 srgbReleaseContext->beTextures(),
Brian Osmane9560492019-02-05 17:00:03 -05001584 yuvaIndices,
1585 { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() },
1586 kTopLeft_GrSurfaceOrigin,
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001587 SkColorSpace::MakeSRGB(),
1588 YUVABackendReleaseContext::Release, srgbReleaseContext);
Brian Osmane9560492019-02-05 17:00:03 -05001589 }
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001590
1591 // Some backends (e.g., Vulkan) require all work be completed for backend textures before
1592 // they are deleted. Since we don't know when we'll next have access to a direct context,
1593 // flush all the work now.
Greg Danielce9f0162020-06-30 13:42:46 -04001594 context->flush();
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001595 context->submit(true);
1596
1597 return true;
1598 }
1599
Robert Phillipsb87b39b2020-07-01 14:45:24 -04001600 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001601 if (!context || context->abandoned()) {
1602 return DrawResult::kSkip;
1603 }
1604
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001605 this->createBitmaps();
1606 if (!this->createImages(context)) {
1607 *errorMsg = "Failed to create YUV images";
1608 return DrawResult::kFail;
1609 }
1610
1611 return DrawResult::kOk;
Brian Osmane9560492019-02-05 17:00:03 -05001612 }
1613
Robert Phillipsb795bea2020-06-25 12:38:53 -04001614 void onGpuTeardown() override {
1615 fImages[0][0] = fImages[0][1] = fImages[1][0] = fImages[1][1] = nullptr;
1616 }
1617
Robert Phillips95c250c2020-06-29 15:36:12 -04001618 void onDraw(GrRecordingContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001619 SkASSERT(fImages[0][0] && fImages[0][1] && fImages[1][0] && fImages[1][1]);
Brian Osmane9560492019-02-05 17:00:03 -05001620
1621 int x = kPad;
1622 for (int tagged : { 0, 1 }) {
1623 for (int opaque : { 0, 1 }) {
1624 int y = kPad;
1625
1626 auto raster = SkImage::MakeFromBitmap(fOriginalBMs[opaque])
1627 ->makeColorSpace(fTargetColorSpace);
1628 canvas->drawImage(raster, x, y);
1629 y += kTileWidthHeight + kPad;
1630
Greg Daniel85da3362020-03-09 15:18:35 -04001631 if (fImages[opaque][tagged]) {
1632 auto yuv = fImages[opaque][tagged]->makeColorSpace(fTargetColorSpace);
1633 SkASSERT(SkColorSpace::Equals(yuv->colorSpace(), fTargetColorSpace.get()));
1634 canvas->drawImage(yuv, x, y);
1635 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001636
Greg Daniel85da3362020-03-09 15:18:35 -04001637 auto subset = yuv->makeSubset(SkIRect::MakeWH(kTileWidthHeight / 2,
Robert Phillipse22c5ca2020-06-19 12:29:57 -04001638 kTileWidthHeight / 2));
Greg Daniel85da3362020-03-09 15:18:35 -04001639 canvas->drawImage(subset, x, y);
1640 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001641
Greg Daniel85da3362020-03-09 15:18:35 -04001642 auto nonTexture = yuv->makeNonTextureImage();
1643 canvas->drawImage(nonTexture, x, y);
1644 y += kTileWidthHeight + kPad;
Brian Osmane9560492019-02-05 17:00:03 -05001645
Greg Daniel85da3362020-03-09 15:18:35 -04001646 SkBitmap readBack;
1647 readBack.allocPixels(yuv->imageInfo());
1648 yuv->readPixels(readBack.pixmap(), 0, 0);
1649 canvas->drawBitmap(readBack, x, y);
1650 }
Brian Osmane9560492019-02-05 17:00:03 -05001651 x += kTileWidthHeight + kPad;
1652 }
1653 }
Brian Osmane9560492019-02-05 17:00:03 -05001654 }
1655
1656private:
1657 SkBitmap fOriginalBMs[2];
1658 sk_sp<SkImage> fImages[2][2];
Brian Osmane9560492019-02-05 17:00:03 -05001659 sk_sp<SkColorSpace> fTargetColorSpace;
1660
1661 typedef GM INHERITED;
1662};
1663
1664DEF_GM(return new YUVMakeColorSpaceGM();)
1665
Robert Phillipsbfa76f22018-10-03 12:12:26 -04001666}
Mike Reed6a5f7e22019-05-23 15:30:07 -04001667
1668///////////////
1669
Mike Reed6a5f7e22019-05-23 15:30:07 -04001670#include "include/effects/SkColorMatrix.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001671#include "src/core/SkAutoPixmapStorage.h"
Mike Klein4b432fa2019-06-06 11:44:05 -05001672#include "tools/Resources.h"
Mike Reed6a5f7e22019-05-23 15:30:07 -04001673
1674static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) {
1675 auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
1676 canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height());
1677 SkPaint paint;
1678 paint.setFilterQuality(kLow_SkFilterQuality);
1679 paint.setColorFilter(cf);
1680 paint.setBlendMode(SkBlendMode::kSrc);
1681 canvas->drawImage(img, 0, 0, &paint);
1682}
1683
1684static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) {
1685 float m[20];
1686 SkColorMatrix_RGB2YUV(cs, m);
1687
1688 memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A
1689 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]);
1690
1691 memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A
1692 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]);
1693
1694 memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A
1695 draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]);
1696}
1697
1698static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y,
1699 const SkImage* a, const SkImage* b) {
1700 auto sh = SkShaders::Blend(SkBlendMode::kDifference, a->makeShader(), b->makeShader());
1701 SkPaint paint;
1702 paint.setShader(sh);
1703 canvas->save();
1704 canvas->translate(x, y);
1705 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1706
1707 SkColorMatrix cm;
1708 cm.setScale(64, 64, 64);
1709 paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm)));
1710 canvas->translate(0, a->height());
1711 canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
1712
1713 canvas->restore();
1714}
1715
1716// Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the
1717// resulting (recombined) images (gpu only for now).
1718//
1719class YUVSplitterGM : public skiagm::GM {
1720 sk_sp<SkImage> fOrig;
1721 SkAutoPixmapStorage fStorage[3];
1722 SkPixmap fPM[3];
1723
1724public:
1725 YUVSplitterGM() {}
1726
1727protected:
1728
1729 SkString onShortName() override {
1730 return SkString("yuv_splitter");
1731 }
1732
1733 SkISize onISize() override {
Brian Osman2b73e662019-11-01 10:02:24 -04001734 return SkISize::Make(1280, 768);
Mike Reed6a5f7e22019-05-23 15:30:07 -04001735 }
1736
1737 void onOnceBeforeDraw() override {
1738 fOrig = GetResourceAsImage("images/mandrill_256.png");
1739
1740 SkImageInfo info = SkImageInfo::Make(fOrig->width(), fOrig->height(), kAlpha_8_SkColorType,
1741 kPremul_SkAlphaType);
1742 fStorage[0].alloc(info);
1743 if (0) {
1744 // if you want to scale U,V down by 1/2
1745 info = info.makeWH(info.width()/2, info.height()/2);
1746 }
1747 fStorage[1].alloc(info);
1748 fStorage[2].alloc(info);
1749 for (int i = 0; i < 3; ++i) {
1750 fPM[i] = fStorage[i];
1751 }
1752 }
1753
1754 void onDraw(SkCanvas* canvas) override {
1755 SkYUVAIndex indices[4];
Robert Phillipse0735522020-01-31 11:03:32 -05001756 indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
1757 indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
1758 indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR};
Mike Reed6a5f7e22019-05-23 15:30:07 -04001759 indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kR};
1760
1761 canvas->translate(fOrig->width(), 0);
1762 canvas->save();
Brian Osman2b73e662019-11-01 10:02:24 -04001763 for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace,
1764 kBT2020_SkYUVColorSpace}) {
Mike Reed6a5f7e22019-05-23 15:30:07 -04001765 split_into_yuv(fOrig.get(), cs, fPM);
1766 auto img = SkImage::MakeFromYUVAPixmaps(canvas->getGrContext(), cs, fPM, indices,
1767 fPM[0].info().dimensions(),
1768 kTopLeft_GrSurfaceOrigin,
1769 false, false, nullptr);
1770 if (img) {
1771 canvas->drawImage(img, 0, 0, nullptr);
1772 draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get());
1773 }
1774 canvas->translate(fOrig->width(), 0);
1775 }
1776 canvas->restore();
1777 canvas->translate(-fOrig->width(), 0);
1778
1779 canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0, nullptr);
1780 canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height(), nullptr);
1781 canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]),
1782 0, fPM[0].height() + fPM[1].height(), nullptr);
1783 }
1784
1785private:
1786 typedef GM INHERITED;
1787};
1788DEF_GM( return new YUVSplitterGM; )