Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkColorPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/core/SkImageGenerator.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include "include/core/SkImageInfo.h" |
| 23 | #include "include/core/SkMatrix.h" |
| 24 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | #include "include/core/SkYUVAIndex.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 36 | #include "include/core/SkYUVASizeInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 37 | #include "include/gpu/GrBackendSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 38 | #include "include/gpu/GrConfig.h" |
| 39 | #include "include/gpu/GrContext.h" |
| 40 | #include "include/gpu/GrTypes.h" |
| 41 | #include "include/private/GrTypesPriv.h" |
| 42 | #include "include/private/SkTArray.h" |
| 43 | #include "include/private/SkTDArray.h" |
| 44 | #include "include/private/SkTemplates.h" |
| 45 | #include "include/utils/SkTextUtils.h" |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 46 | #include "src/core/SkYUVMath.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 47 | #include "src/gpu/GrContextPriv.h" |
| 48 | #include "src/gpu/GrGpu.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 49 | #include "tools/ToolUtils.h" |
| 50 | |
| 51 | #include <math.h> |
| 52 | #include <string.h> |
| 53 | #include <initializer_list> |
| 54 | #include <memory> |
| 55 | #include <utility> |
| 56 | |
| 57 | class GrRenderTargetContext; |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 58 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 59 | static const int kTileWidthHeight = 128; |
| 60 | static const int kLabelWidth = 64; |
| 61 | static const int kLabelHeight = 32; |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 62 | static const int kDomainPadding = 8; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 63 | static const int kPad = 1; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 64 | |
| 65 | enum YUVFormat { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 66 | // 4:2:0 formats, 24 bpp |
| 67 | kP016_YUVFormat, // 16-bit Y plane + 2x2 down sampled interleaved U/V plane (2 textures) |
| 68 | // 4:2:0 formats, "15 bpp" (but really 24 bpp) |
| 69 | kP010_YUVFormat, // same as kP016 except "10 bpp". Note that it is the same memory layout |
| 70 | // except that the bottom 6 bits are zeroed out (2 textures) |
| 71 | // TODO: we're cheating a bit w/ P010 and just treating it as unorm 16. This means its |
| 72 | // fully saturated values are 65504 rather than 65535 (that is just .9995 out of 1.0 though). |
| 73 | |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 74 | // This is laid out the same as kP016 and kP010 but uses F16 unstead of U16. In this case |
| 75 | // the 10 bits/channel vs 16 bits/channel distinction isn't relevant. |
| 76 | kP016F_YUVFormat, |
| 77 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 78 | // 4:4:4 formats, 64 bpp |
| 79 | kY416_YUVFormat, // 16-bit AVYU values all interleaved (1 texture) |
| 80 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 81 | // 4:4:4 formats, 32 bpp |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 82 | kAYUV_YUVFormat, // 8-bit YUVA values all interleaved (1 texture) |
| 83 | kY410_YUVFormat, // AVYU w/ 10bpp for YUV and 2 for A all interleaved (1 texture) |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 84 | |
| 85 | // 4:2:0 formats, 12 bpp |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 86 | kNV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled interleaved U/V planes (2 textures) |
| 87 | kNV21_YUVFormat, // same as kNV12 but w/ U/V reversed in the interleaved texture (2 textures) |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 88 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 89 | kI420_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled U and V planes (3 textures) |
| 90 | kYV12_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled V and U planes (3 textures) |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 91 | |
| 92 | kLast_YUVFormat = kYV12_YUVFormat |
| 93 | }; |
| 94 | |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 95 | #ifdef SK_DEBUG |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 96 | static bool format_uses_16_bpp(YUVFormat yuvFormat) { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 97 | return kP016_YUVFormat == yuvFormat || |
| 98 | kP010_YUVFormat == yuvFormat || |
| 99 | kP016F_YUVFormat == yuvFormat || |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 100 | kY416_YUVFormat == yuvFormat; |
| 101 | } |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 102 | #endif |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 103 | |
| 104 | static bool format_has_builtin_alpha(YUVFormat yuvFormat) { |
| 105 | return kY416_YUVFormat == yuvFormat || |
| 106 | kAYUV_YUVFormat == yuvFormat || |
| 107 | kY410_YUVFormat == yuvFormat; |
| 108 | } |
| 109 | |
Robert Phillips | f47717a | 2019-09-19 13:04:33 -0400 | [diff] [blame] | 110 | static bool is_colorType_texturable(const GrCaps* caps, GrColorType ct) { |
| 111 | GrBackendFormat format = caps->getDefaultBackendFormat(ct, GrRenderable::kNo); |
| 112 | if (!format.isValid()) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | return caps->isFormatTexturable(format); |
| 117 | } |
| 118 | |
| 119 | static bool is_format_natively_supported(GrContext* context, YUVFormat yuvFormat) { |
| 120 | |
| 121 | const GrCaps* caps = context->priv().caps(); |
| 122 | |
| 123 | switch (yuvFormat) { |
| 124 | case kP016_YUVFormat: // fall through |
| 125 | case kP010_YUVFormat: return is_colorType_texturable(caps, GrColorType::kAlpha_16) && |
| 126 | is_colorType_texturable(caps, GrColorType::kRG_1616); |
| 127 | case kP016F_YUVFormat: return is_colorType_texturable(caps, GrColorType::kAlpha_F16) && |
| 128 | is_colorType_texturable(caps, GrColorType::kRG_F16); |
| 129 | case kY416_YUVFormat: return is_colorType_texturable(caps, GrColorType::kRGBA_16161616); |
| 130 | case kAYUV_YUVFormat: return is_colorType_texturable(caps, GrColorType::kRGBA_8888); |
| 131 | case kY410_YUVFormat: return is_colorType_texturable(caps, GrColorType::kRGBA_1010102); |
| 132 | case kNV12_YUVFormat: // fall through |
| 133 | case kNV21_YUVFormat: return is_colorType_texturable(caps, GrColorType::kGray_8) && |
| 134 | is_colorType_texturable(caps, GrColorType::kRG_88); |
| 135 | case kI420_YUVFormat: // fall through |
| 136 | case kYV12_YUVFormat: return is_colorType_texturable(caps, GrColorType::kGray_8); |
| 137 | } |
| 138 | |
| 139 | SkUNREACHABLE; |
| 140 | } |
| 141 | |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 142 | // Helper to setup the SkYUVAIndex array correctly |
| 143 | // Skia allows the client to tack an additional alpha plane onto any of the standard opaque |
| 144 | // formats (via the addExtraAlpha) flag. In this case it is assumed to be a stand-alone single- |
| 145 | // channel plane. |
| 146 | static void setup_yuv_indices(YUVFormat yuvFormat, bool addExtraAlpha, SkYUVAIndex yuvaIndices[4]) { |
| 147 | switch (yuvFormat) { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 148 | case kP016_YUVFormat: // fall through |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 149 | case kP010_YUVFormat: // fall through |
| 150 | case kP016F_YUVFormat: |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 151 | yuvaIndices[0].fIndex = 0; |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 152 | yuvaIndices[0].fChannel = SkColorChannel::kA; // bc 16bit is stored in A16 or AF16 |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 153 | yuvaIndices[1].fIndex = 1; |
| 154 | yuvaIndices[1].fChannel = SkColorChannel::kR; |
| 155 | yuvaIndices[2].fIndex = 1; |
| 156 | yuvaIndices[2].fChannel = SkColorChannel::kG; |
| 157 | if (addExtraAlpha) { |
| 158 | yuvaIndices[3].fIndex = 2; |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 159 | yuvaIndices[3].fChannel = SkColorChannel::kA; // bc 16bit is stored in A16 or AF16 |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 160 | } else { |
| 161 | yuvaIndices[3].fIndex = -1; // No alpha channel |
| 162 | } |
| 163 | break; |
| 164 | case kY416_YUVFormat: |
| 165 | SkASSERT(!addExtraAlpha); // this format already has an alpha channel |
| 166 | yuvaIndices[0].fIndex = 0; |
| 167 | yuvaIndices[0].fChannel = SkColorChannel::kG; |
| 168 | yuvaIndices[1].fIndex = 0; |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 169 | yuvaIndices[1].fChannel = SkColorChannel::kR; |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 170 | yuvaIndices[2].fIndex = 0; |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 171 | yuvaIndices[2].fChannel = SkColorChannel::kB; |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 172 | yuvaIndices[3].fIndex = 0; |
| 173 | yuvaIndices[3].fChannel = SkColorChannel::kA; |
| 174 | break; |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 175 | case kAYUV_YUVFormat: |
| 176 | SkASSERT(!addExtraAlpha); // this format already has an alpha channel |
| 177 | yuvaIndices[0].fIndex = 0; |
| 178 | yuvaIndices[0].fChannel = SkColorChannel::kR; |
| 179 | yuvaIndices[1].fIndex = 0; |
| 180 | yuvaIndices[1].fChannel = SkColorChannel::kG; |
| 181 | yuvaIndices[2].fIndex = 0; |
| 182 | yuvaIndices[2].fChannel = SkColorChannel::kB; |
| 183 | yuvaIndices[3].fIndex = 0; |
| 184 | yuvaIndices[3].fChannel = SkColorChannel::kA; |
| 185 | break; |
| 186 | case kY410_YUVFormat: |
| 187 | SkASSERT(!addExtraAlpha); // this format already has an alpha channel |
| 188 | yuvaIndices[0].fIndex = 0; |
| 189 | yuvaIndices[0].fChannel = SkColorChannel::kG; |
| 190 | yuvaIndices[1].fIndex = 0; |
| 191 | yuvaIndices[1].fChannel = SkColorChannel::kB; |
| 192 | yuvaIndices[2].fIndex = 0; |
| 193 | yuvaIndices[2].fChannel = SkColorChannel::kR; |
| 194 | yuvaIndices[3].fIndex = 0; |
| 195 | yuvaIndices[3].fChannel = SkColorChannel::kA; |
| 196 | break; |
| 197 | case kNV12_YUVFormat: |
| 198 | yuvaIndices[0].fIndex = 0; |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 199 | yuvaIndices[0].fChannel = SkColorChannel::kR; |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 200 | yuvaIndices[1].fIndex = 1; |
| 201 | yuvaIndices[1].fChannel = SkColorChannel::kR; |
| 202 | yuvaIndices[2].fIndex = 1; |
| 203 | yuvaIndices[2].fChannel = SkColorChannel::kG; |
| 204 | if (addExtraAlpha) { |
| 205 | yuvaIndices[3].fIndex = 2; |
| 206 | yuvaIndices[3].fChannel = SkColorChannel::kA; |
| 207 | } else { |
| 208 | yuvaIndices[3].fIndex = -1; // No alpha channel |
| 209 | } |
| 210 | break; |
| 211 | case kNV21_YUVFormat: |
| 212 | yuvaIndices[0].fIndex = 0; |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 213 | yuvaIndices[0].fChannel = SkColorChannel::kR; |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 214 | yuvaIndices[1].fIndex = 1; |
| 215 | yuvaIndices[1].fChannel = SkColorChannel::kG; |
| 216 | yuvaIndices[2].fIndex = 1; |
| 217 | yuvaIndices[2].fChannel = SkColorChannel::kR; |
| 218 | if (addExtraAlpha) { |
| 219 | yuvaIndices[3].fIndex = 2; |
| 220 | yuvaIndices[3].fChannel = SkColorChannel::kA; |
| 221 | } else { |
| 222 | yuvaIndices[3].fIndex = -1; // No alpha channel |
| 223 | } |
| 224 | break; |
| 225 | case kI420_YUVFormat: |
| 226 | yuvaIndices[0].fIndex = 0; |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 227 | yuvaIndices[0].fChannel = SkColorChannel::kR; |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 228 | yuvaIndices[1].fIndex = 1; |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 229 | yuvaIndices[1].fChannel = SkColorChannel::kR; |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 230 | yuvaIndices[2].fIndex = 2; |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 231 | yuvaIndices[2].fChannel = SkColorChannel::kR; |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 232 | if (addExtraAlpha) { |
| 233 | yuvaIndices[3].fIndex = 3; |
| 234 | yuvaIndices[3].fChannel = SkColorChannel::kA; |
| 235 | } else { |
| 236 | yuvaIndices[3].fIndex = -1; // No alpha channel |
| 237 | } |
| 238 | break; |
| 239 | case kYV12_YUVFormat: |
| 240 | yuvaIndices[0].fIndex = 0; |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 241 | yuvaIndices[0].fChannel = SkColorChannel::kR; |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 242 | yuvaIndices[1].fIndex = 2; |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 243 | yuvaIndices[1].fChannel = SkColorChannel::kR; |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 244 | yuvaIndices[2].fIndex = 1; |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 245 | yuvaIndices[2].fChannel = SkColorChannel::kR; |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 246 | if (addExtraAlpha) { |
| 247 | yuvaIndices[3].fIndex = 3; |
| 248 | yuvaIndices[3].fChannel = SkColorChannel::kA; |
| 249 | } else { |
| 250 | yuvaIndices[3].fIndex = -1; // No alpha channel |
| 251 | } |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 256 | // All the planes we need to construct the various YUV formats |
| 257 | struct PlaneData { |
| 258 | SkBitmap fYFull; |
| 259 | SkBitmap fUFull; |
| 260 | SkBitmap fVFull; |
| 261 | SkBitmap fAFull; |
| 262 | SkBitmap fUQuarter; // 2x2 downsampled U channel |
| 263 | SkBitmap fVQuarter; // 2x2 downsampled V channel |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 264 | |
| 265 | SkBitmap fFull; |
| 266 | SkBitmap fQuarter; // 2x2 downsampled YUVA |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | // Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle |
| 270 | // and have tangents 'v1' and 'v2'. |
| 271 | static void add_arc(SkPath* path, |
| 272 | const SkPoint& o1, const SkVector& v1, |
| 273 | const SkPoint& o2, const SkVector& v2, |
| 274 | SkTDArray<SkRect>* circles, bool takeLongWayRound) { |
| 275 | |
| 276 | SkVector v3 = { -v1.fY, v1.fX }; |
| 277 | SkVector v4 = { v2.fY, -v2.fX }; |
| 278 | |
| 279 | SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4); |
| 280 | SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY }; |
| 281 | |
| 282 | SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t }; |
| 283 | |
| 284 | if (circles) { |
| 285 | circles->push_back(r); |
| 286 | } |
| 287 | |
| 288 | SkVector startV = o1 - center, endV = o2 - center; |
| 289 | startV.normalize(); |
| 290 | endV.normalize(); |
| 291 | |
| 292 | SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX)); |
| 293 | SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX)); |
| 294 | |
| 295 | startDeg += 360.0f; |
| 296 | startDeg = fmodf(startDeg, 360.0f); |
| 297 | |
| 298 | endDeg += 360.0f; |
| 299 | endDeg = fmodf(endDeg, 360.0f); |
| 300 | |
| 301 | if (endDeg < startDeg) { |
| 302 | endDeg += 360.0f; |
| 303 | } |
| 304 | |
| 305 | SkScalar sweepDeg = SkTAbs(endDeg - startDeg); |
| 306 | if (!takeLongWayRound) { |
| 307 | sweepDeg = sweepDeg - 360; |
| 308 | } |
| 309 | |
| 310 | path->arcTo(r, startDeg, sweepDeg, false); |
| 311 | } |
| 312 | |
| 313 | static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius, |
| 314 | SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) { |
| 315 | if (numLobes <= 1) { |
| 316 | return SkPath(); |
| 317 | } |
| 318 | |
| 319 | SkPath p; |
| 320 | |
| 321 | int numDivisions = 2 * numLobes; |
| 322 | SkScalar fullLobeDegrees = 360.0f / numLobes; |
| 323 | SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f); |
| 324 | SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f); |
| 325 | SkMatrix outerStep, innerStep; |
| 326 | outerStep.setRotate(outDegrees); |
| 327 | innerStep.setRotate(innerDegrees); |
| 328 | SkVector curV = SkVector::Make(0.0f, 1.0f); |
| 329 | |
| 330 | if (circles) { |
| 331 | circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius, |
| 332 | o.fX + innerRadius, o.fY + innerRadius)); |
| 333 | } |
| 334 | |
| 335 | p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY); |
| 336 | |
| 337 | for (int i = 0; i < numDivisions; ++i) { |
| 338 | |
| 339 | SkVector nextV; |
| 340 | if (0 == (i % 2)) { |
| 341 | nextV = outerStep.mapVector(curV.fX, curV.fY); |
| 342 | |
| 343 | SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX, |
| 344 | o.fY + outerRadius * curV.fY); |
| 345 | SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX, |
| 346 | o.fY + outerRadius * nextV.fY); |
| 347 | |
| 348 | p.lineTo(top); |
| 349 | add_arc(&p, top, curV, nextTop, nextV, circles, true); |
| 350 | } else { |
| 351 | nextV = innerStep.mapVector(curV.fX, curV.fY); |
| 352 | |
| 353 | SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX, |
| 354 | o.fY + innerRadius * curV.fY); |
| 355 | SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX, |
| 356 | o.fY + innerRadius * nextV.fY); |
| 357 | |
| 358 | p.lineTo(bot); |
| 359 | add_arc(&p, bot, curV, nextBot, nextV, nullptr, false); |
| 360 | } |
| 361 | |
| 362 | curV = nextV; |
| 363 | } |
| 364 | |
| 365 | p.close(); |
| 366 | |
| 367 | return p; |
| 368 | } |
| 369 | |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 370 | static SkBitmap make_bitmap(SkColorType colorType, const SkPath& path, |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 371 | const SkTDArray<SkRect>& circles, bool opaque, bool padWithRed) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 372 | const SkColor kGreen = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104)); |
| 373 | const SkColor kBlue = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252)); |
| 374 | const SkColor kYellow = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 375 | |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 376 | int widthHeight = kTileWidthHeight + (padWithRed ? 2 * kDomainPadding : 0); |
| 377 | |
| 378 | SkImageInfo ii = SkImageInfo::Make(widthHeight, widthHeight, |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 379 | colorType, kPremul_SkAlphaType); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 380 | |
| 381 | SkBitmap bm; |
| 382 | bm.allocPixels(ii); |
| 383 | |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 384 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(ii, |
| 385 | bm.getPixels(), |
| 386 | bm.rowBytes()); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 387 | if (padWithRed) { |
| 388 | canvas->clear(SK_ColorRED); |
| 389 | canvas->translate(kDomainPadding, kDomainPadding); |
| 390 | canvas->clipRect(SkRect::MakeWH(kTileWidthHeight, kTileWidthHeight)); |
| 391 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 392 | canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT); |
| 393 | |
| 394 | SkPaint paint; |
| 395 | paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency |
| 396 | paint.setColor(kBlue); |
| 397 | |
| 398 | canvas->drawPath(path, paint); |
| 399 | |
| 400 | paint.setColor(opaque ? kYellow : SK_ColorTRANSPARENT); |
| 401 | paint.setBlendMode(SkBlendMode::kSrc); |
| 402 | for (int i = 0; i < circles.count(); ++i) { |
| 403 | SkRect r = circles[i]; |
| 404 | r.inset(r.width()/4, r.height()/4); |
| 405 | canvas->drawOval(r, paint); |
| 406 | } |
| 407 | |
| 408 | return bm; |
| 409 | } |
| 410 | |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 411 | static void convert_rgba_to_yuva(const float mtx[20], SkColor col, uint8_t yuv[4]) { |
| 412 | const uint8_t r = SkColorGetR(col); |
| 413 | const uint8_t g = SkColorGetG(col); |
| 414 | const uint8_t b = SkColorGetB(col); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 415 | |
Brian Osman | aba642c | 2020-02-06 12:52:25 -0500 | [diff] [blame] | 416 | yuv[0] = SkTPin(SkScalarRoundToInt(mtx[ 0]*r + mtx[ 1]*g + mtx[ 2]*b + mtx[ 4]*255), 0, 255); |
| 417 | yuv[1] = SkTPin(SkScalarRoundToInt(mtx[ 5]*r + mtx[ 6]*g + mtx[ 7]*b + mtx[ 9]*255), 0, 255); |
| 418 | yuv[2] = SkTPin(SkScalarRoundToInt(mtx[10]*r + mtx[11]*g + mtx[12]*b + mtx[14]*255), 0, 255); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 419 | yuv[3] = SkColorGetA(col); |
| 420 | } |
| 421 | |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 422 | static SkPMColor convert_yuva_to_rgba(const float mtx[20], |
| 423 | uint8_t y, uint8_t u, uint8_t v, uint8_t a) { |
Brian Osman | aba642c | 2020-02-06 12:52:25 -0500 | [diff] [blame] | 424 | uint8_t r = SkTPin(SkScalarRoundToInt(mtx[ 0]*y + mtx[ 1]*u + mtx[ 2]*v + mtx[ 4]*255), 0, 255); |
| 425 | uint8_t g = SkTPin(SkScalarRoundToInt(mtx[ 5]*y + mtx[ 6]*u + mtx[ 7]*v + mtx[ 9]*255), 0, 255); |
| 426 | uint8_t b = SkTPin(SkScalarRoundToInt(mtx[10]*y + mtx[11]*u + mtx[12]*v + mtx[14]*255), 0, 255); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 427 | |
Robert Phillips | e073552 | 2020-01-31 11:03:32 -0500 | [diff] [blame] | 428 | return SkPremultiplyARGBInline(a, r, g, b); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 429 | } |
| 430 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 431 | static void extract_planes(const SkBitmap& bm, SkYUVColorSpace yuvColorSpace, PlaneData* planes) { |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 432 | if (kIdentity_SkYUVColorSpace == yuvColorSpace) { |
| 433 | // To test the identity color space we use JPEG YUV planes |
| 434 | yuvColorSpace = kJPEG_SkYUVColorSpace; |
| 435 | } |
| 436 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 437 | SkASSERT(!(bm.width() % 2)); |
| 438 | SkASSERT(!(bm.height() % 2)); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 439 | planes->fYFull.allocPixels( |
| 440 | SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType)); |
| 441 | planes->fUFull.allocPixels( |
| 442 | SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType)); |
| 443 | planes->fVFull.allocPixels( |
| 444 | SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 445 | planes->fAFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height())); |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 446 | planes->fUQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2, |
| 447 | kGray_8_SkColorType, kUnpremul_SkAlphaType)); |
| 448 | planes->fVQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2, |
| 449 | kGray_8_SkColorType, kUnpremul_SkAlphaType)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 450 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 451 | planes->fFull.allocPixels( |
| 452 | SkImageInfo::Make(bm.dimensions(), kRGBA_F32_SkColorType, kUnpremul_SkAlphaType)); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 453 | planes->fQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2, |
| 454 | kRGBA_F32_SkColorType, kUnpremul_SkAlphaType)); |
| 455 | |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 456 | float mtx[20]; |
| 457 | SkColorMatrix_RGB2YUV(yuvColorSpace, mtx); |
| 458 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 459 | SkColor4f* dst = (SkColor4f *) planes->fFull.getAddr(0, 0); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 460 | for (int y = 0; y < bm.height(); ++y) { |
| 461 | for (int x = 0; x < bm.width(); ++x) { |
| 462 | SkColor col = bm.getColor(x, y); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 463 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 464 | uint8_t yuva[4]; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 465 | |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 466 | convert_rgba_to_yuva(mtx, col, yuva); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 467 | |
| 468 | *planes->fYFull.getAddr8(x, y) = yuva[0]; |
| 469 | *planes->fUFull.getAddr8(x, y) = yuva[1]; |
| 470 | *planes->fVFull.getAddr8(x, y) = yuva[2]; |
| 471 | *planes->fAFull.getAddr8(x, y) = yuva[3]; |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 472 | |
| 473 | // TODO: render in F32 rather than converting here |
| 474 | dst->fR = yuva[0] / 255.0f; |
| 475 | dst->fG = yuva[1] / 255.0f; |
| 476 | dst->fB = yuva[2] / 255.0f; |
| 477 | dst->fA = yuva[3] / 255.0f; |
| 478 | ++dst; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 482 | dst = (SkColor4f *) planes->fQuarter.getAddr(0, 0); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 483 | for (int y = 0; y < bm.height()/2; ++y) { |
| 484 | for (int x = 0; x < bm.width()/2; ++x) { |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 485 | uint32_t yAccum = 0, uAccum = 0, vAccum = 0, aAccum = 0; |
| 486 | |
| 487 | yAccum += *planes->fYFull.getAddr8(2*x, 2*y); |
| 488 | yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y); |
| 489 | yAccum += *planes->fYFull.getAddr8(2*x, 2*y+1); |
| 490 | yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y+1); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 491 | |
| 492 | uAccum += *planes->fUFull.getAddr8(2*x, 2*y); |
| 493 | uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y); |
| 494 | uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1); |
| 495 | uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1); |
| 496 | |
| 497 | *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f; |
| 498 | |
| 499 | vAccum += *planes->fVFull.getAddr8(2*x, 2*y); |
| 500 | vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y); |
| 501 | vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1); |
| 502 | vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1); |
| 503 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 504 | *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f; |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 505 | |
| 506 | aAccum += *planes->fAFull.getAddr8(2*x, 2*y); |
| 507 | aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y); |
| 508 | aAccum += *planes->fAFull.getAddr8(2*x, 2*y+1); |
| 509 | aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y+1); |
| 510 | |
| 511 | // TODO: render in F32 rather than converting here |
| 512 | dst->fR = yAccum / (4.0f * 255.0f); |
| 513 | dst->fG = uAccum / (4.0f * 255.0f); |
| 514 | dst->fB = vAccum / (4.0f * 255.0f); |
| 515 | dst->fA = aAccum / (4.0f * 255.0f); |
| 516 | ++dst; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 521 | // Create a 2x2 downsampled SkBitmap. It is stored in an RG texture. It can optionally be |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 522 | // uv (i.e., NV12) or vu (i.e., NV21). |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 523 | static SkBitmap make_quarter_2_channel(const SkBitmap& fullY, |
| 524 | const SkBitmap& quarterU, |
| 525 | const SkBitmap& quarterV, |
| 526 | bool uv) { |
| 527 | SkBitmap result; |
| 528 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 529 | result.allocPixels(SkImageInfo::Make(fullY.width()/2, |
| 530 | fullY.height()/2, |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 531 | kR8G8_unorm_SkColorType, |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 532 | kUnpremul_SkAlphaType)); |
| 533 | |
| 534 | for (int y = 0; y < fullY.height()/2; ++y) { |
| 535 | for (int x = 0; x < fullY.width()/2; ++x) { |
| 536 | uint8_t u8 = *quarterU.getAddr8(x, y); |
| 537 | uint8_t v8 = *quarterV.getAddr8(x, y); |
| 538 | |
| 539 | if (uv) { |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 540 | *result.getAddr16(x, y) = (v8 << 8) | u8; |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 541 | } else { |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 542 | *result.getAddr16(x, y) = (u8 << 8) | v8; |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | return result; |
| 548 | } |
| 549 | |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 550 | // Create some flavor of a 16bits/channel bitmap from a RGBA_F32 source |
| 551 | static SkBitmap make_16(const SkBitmap& src, SkColorType dstCT, |
| 552 | std::function<void(uint16_t* dstPixel, const float* srcPixel)> convert) { |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 553 | SkASSERT(src.colorType() == kRGBA_F32_SkColorType); |
| 554 | |
| 555 | SkBitmap result; |
| 556 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 557 | result.allocPixels(SkImageInfo::Make(src.dimensions(), dstCT, kUnpremul_SkAlphaType)); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 558 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 559 | for (int y = 0; y < src.height(); ++y) { |
| 560 | for (int x = 0; x < src.width(); ++x) { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 561 | const float* srcPixel = (const float*) src.getAddr(x, y); |
| 562 | uint16_t* dstPixel = (uint16_t*) result.getAddr(x, y); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 563 | |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 564 | convert(dstPixel, srcPixel); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | return result; |
| 569 | } |
| 570 | |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 571 | static uint16_t flt_2_uint16(float flt) { return SkScalarRoundToInt(flt * 65535.0f); } |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 572 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 573 | // Recombine the separate planes into some YUV format |
| 574 | static void create_YUV(const PlaneData& planes, YUVFormat yuvFormat, |
| 575 | SkBitmap resultBMs[], SkYUVAIndex yuvaIndices[4], bool opaque) { |
| 576 | int nextLayer = 0; |
| 577 | |
| 578 | switch (yuvFormat) { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 579 | case kY416_YUVFormat: { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 580 | resultBMs[nextLayer++] = make_16(planes.fFull, kR16G16B16A16_unorm_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 581 | [] (uint16_t* dstPixel, const float* srcPixel) { |
| 582 | dstPixel[0] = flt_2_uint16(srcPixel[1]); // U |
| 583 | dstPixel[1] = flt_2_uint16(srcPixel[0]); // Y |
| 584 | dstPixel[2] = flt_2_uint16(srcPixel[2]); // V |
| 585 | dstPixel[3] = flt_2_uint16(srcPixel[3]); // A |
| 586 | }); |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 587 | setup_yuv_indices(yuvFormat, false, yuvaIndices); |
| 588 | break; |
| 589 | } |
Jim Van Verth | 976a6b0 | 2018-10-17 15:27:19 -0400 | [diff] [blame] | 590 | case kAYUV_YUVFormat: { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 591 | SkBitmap yuvaFull; |
| 592 | |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 593 | yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(), |
| 594 | kRGBA_8888_SkColorType, kUnpremul_SkAlphaType)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 595 | |
| 596 | for (int y = 0; y < planes.fYFull.height(); ++y) { |
| 597 | for (int x = 0; x < planes.fYFull.width(); ++x) { |
| 598 | |
| 599 | uint8_t Y = *planes.fYFull.getAddr8(x, y); |
| 600 | uint8_t U = *planes.fUFull.getAddr8(x, y); |
| 601 | uint8_t V = *planes.fVFull.getAddr8(x, y); |
| 602 | uint8_t A = *planes.fAFull.getAddr8(x, y); |
| 603 | |
| 604 | // NOT premul! |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 605 | // V and Y swapped to match RGBA layout |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 606 | SkColor c = SkColorSetARGB(A, V, U, Y); |
| 607 | *yuvaFull.getAddr32(x, y) = c; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | resultBMs[nextLayer++] = yuvaFull; |
| 612 | |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 613 | setup_yuv_indices(yuvFormat, false, yuvaIndices); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 614 | break; |
| 615 | } |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 616 | case kY410_YUVFormat: { |
| 617 | SkBitmap yuvaFull; |
Robert Phillips | f0313ee | 2019-05-21 13:51:11 -0400 | [diff] [blame] | 618 | uint32_t Y, U, V; |
| 619 | uint8_t A; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 620 | |
| 621 | yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(), |
| 622 | kRGBA_1010102_SkColorType, |
| 623 | kUnpremul_SkAlphaType)); |
| 624 | |
| 625 | for (int y = 0; y < planes.fYFull.height(); ++y) { |
| 626 | for (int x = 0; x < planes.fYFull.width(); ++x) { |
| 627 | |
Robert Phillips | f0313ee | 2019-05-21 13:51:11 -0400 | [diff] [blame] | 628 | Y = SkScalarRoundToInt((*planes.fYFull.getAddr8(x, y) / 255.0f) * 1023.0f); |
| 629 | U = SkScalarRoundToInt((*planes.fUFull.getAddr8(x, y) / 255.0f) * 1023.0f); |
| 630 | V = SkScalarRoundToInt((*planes.fVFull.getAddr8(x, y) / 255.0f) * 1023.0f); |
| 631 | A = SkScalarRoundToInt((*planes.fAFull.getAddr8(x, y) / 255.0f) * 3.0f); |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 632 | |
| 633 | // NOT premul! |
| 634 | // AVYU but w/ V and U swapped to match RGBA layout |
| 635 | *yuvaFull.getAddr32(x, y) = (A << 30) | (U << 20) | (Y << 10) | (V << 0); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | resultBMs[nextLayer++] = yuvaFull; |
| 640 | |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 641 | setup_yuv_indices(yuvFormat, false, yuvaIndices); |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 642 | break; |
| 643 | } |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 644 | case kP016_YUVFormat: // fall through |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 645 | case kP010_YUVFormat: { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 646 | resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 647 | [tenBitsPP = (yuvFormat == kP010_YUVFormat)] |
| 648 | (uint16_t* dstPixel, const float* srcPixel) { |
| 649 | uint16_t val16 = flt_2_uint16(srcPixel[0]); |
| 650 | dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0) |
| 651 | : val16; |
| 652 | }); |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 653 | resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_unorm_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 654 | [tenBitsPP = (yuvFormat == kP010_YUVFormat)] |
| 655 | (uint16_t* dstPixel, const float* srcPixel) { |
| 656 | uint16_t u16 = flt_2_uint16(srcPixel[1]); |
| 657 | uint16_t v16 = flt_2_uint16(srcPixel[2]); |
| 658 | dstPixel[0] = tenBitsPP ? (u16 & 0xFFC0) : u16; |
| 659 | dstPixel[1] = tenBitsPP ? (v16 & 0xFFC0) : v16; |
| 660 | }); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 661 | if (!opaque) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 662 | resultBMs[nextLayer] = make_16(planes.fFull, kA16_unorm_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 663 | [tenBitsPP = (yuvFormat == kP010_YUVFormat)] |
| 664 | (uint16_t* dstPixel, const float* srcPixel) { |
| 665 | uint16_t val16 = flt_2_uint16(srcPixel[3]); |
| 666 | dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0) |
| 667 | : val16; |
| 668 | }); |
| 669 | } |
| 670 | setup_yuv_indices(yuvFormat, !opaque, yuvaIndices); |
| 671 | return; |
| 672 | } |
| 673 | case kP016F_YUVFormat: { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 674 | resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 675 | [] (uint16_t* dstPixel, const float* srcPixel) { |
| 676 | dstPixel[0] = SkFloatToHalf(srcPixel[0]); |
| 677 | }); |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 678 | resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_float_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 679 | [] (uint16_t* dstPixel, const float* srcPixel) { |
| 680 | dstPixel[0] = SkFloatToHalf(srcPixel[1]); |
| 681 | dstPixel[1] = SkFloatToHalf(srcPixel[2]); |
| 682 | }); |
| 683 | if (!opaque) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 684 | resultBMs[nextLayer] = make_16(planes.fFull, kA16_float_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 685 | [] (uint16_t* dstPixel, const float* srcPixel) { |
| 686 | dstPixel[0] = SkFloatToHalf(srcPixel[3]); |
| 687 | }); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 688 | } |
| 689 | setup_yuv_indices(yuvFormat, !opaque, yuvaIndices); |
| 690 | return; |
| 691 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 692 | case kNV12_YUVFormat: { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 693 | SkBitmap uvQuarter = make_quarter_2_channel(planes.fYFull, |
| 694 | planes.fUQuarter, |
| 695 | planes.fVQuarter, true); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 696 | resultBMs[nextLayer++] = planes.fYFull; |
| 697 | resultBMs[nextLayer++] = uvQuarter; |
| 698 | |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 699 | setup_yuv_indices(yuvFormat, !opaque, yuvaIndices); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 700 | break; |
| 701 | } |
| 702 | case kNV21_YUVFormat: { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 703 | SkBitmap vuQuarter = make_quarter_2_channel(planes.fYFull, |
| 704 | planes.fUQuarter, |
| 705 | planes.fVQuarter, false); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 706 | resultBMs[nextLayer++] = planes.fYFull; |
| 707 | resultBMs[nextLayer++] = vuQuarter; |
| 708 | |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 709 | setup_yuv_indices(yuvFormat, !opaque, yuvaIndices); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 710 | break; |
| 711 | } |
| 712 | case kI420_YUVFormat: |
| 713 | resultBMs[nextLayer++] = planes.fYFull; |
| 714 | resultBMs[nextLayer++] = planes.fUQuarter; |
| 715 | resultBMs[nextLayer++] = planes.fVQuarter; |
| 716 | |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 717 | setup_yuv_indices(yuvFormat, !opaque, yuvaIndices); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 718 | break; |
| 719 | case kYV12_YUVFormat: |
| 720 | resultBMs[nextLayer++] = planes.fYFull; |
| 721 | resultBMs[nextLayer++] = planes.fVQuarter; |
| 722 | resultBMs[nextLayer++] = planes.fUQuarter; |
| 723 | |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 724 | setup_yuv_indices(yuvFormat, !opaque, yuvaIndices); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 725 | break; |
| 726 | } |
| 727 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 728 | if (!format_has_builtin_alpha(yuvFormat) && !opaque) { |
Robert Phillips | dc62b64 | 2019-05-16 10:10:45 -0400 | [diff] [blame] | 729 | resultBMs[nextLayer] = planes.fAFull; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 730 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 731 | } |
| 732 | |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 733 | static uint8_t look_up(float x1, float y1, const SkBitmap& bm, SkColorChannel channel) { |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 734 | uint8_t result; |
| 735 | |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 736 | SkASSERT(x1 > 0 && x1 < 1.0f); |
| 737 | SkASSERT(y1 > 0 && y1 < 1.0f); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 738 | int x = SkScalarFloorToInt(x1 * bm.width()); |
| 739 | int y = SkScalarFloorToInt(y1 * bm.height()); |
| 740 | |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 741 | if (kGray_8_SkColorType == bm.colorType()) { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 742 | SkASSERT(SkColorChannel::kA == channel || SkColorChannel::kR == channel); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 743 | result = *bm.getAddr8(x, y); |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 744 | } else if (kAlpha_8_SkColorType == bm.colorType() || |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 745 | kA16_unorm_SkColorType == bm.colorType() || |
| 746 | kA16_float_SkColorType == bm.colorType()) { |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 747 | SkASSERT(SkColorChannel::kA == channel); |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 748 | SkColor c = bm.getColor(x, y); |
| 749 | result = SkColorGetA(c); |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 750 | } else if (kR8G8_unorm_SkColorType == bm.colorType() || |
| 751 | kR16G16_unorm_SkColorType == bm.colorType() || |
| 752 | kR16G16_float_SkColorType == bm.colorType()) { |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 753 | SkASSERT(SkColorChannel::kR == channel || SkColorChannel::kG == channel); |
| 754 | SkColor c = bm.getColor(x, y); |
| 755 | |
| 756 | switch (channel) { |
| 757 | case SkColorChannel::kR: |
| 758 | result = SkColorGetR(c); |
| 759 | break; |
| 760 | case SkColorChannel::kG: |
| 761 | result = SkColorGetG(c); |
| 762 | break; |
| 763 | case SkColorChannel::kB: |
| 764 | result = 0; |
| 765 | break; |
| 766 | case SkColorChannel::kA: |
| 767 | result = 255; |
| 768 | break; |
| 769 | } |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 770 | } else { |
| 771 | SkASSERT(kRGBA_1010102_SkColorType == bm.colorType() || |
| 772 | kRGBA_8888_SkColorType == bm.colorType() || |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 773 | kR16G16B16A16_unorm_SkColorType == bm.colorType()); |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 774 | |
| 775 | SkColor c = bm.getColor(x, y); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 776 | |
| 777 | switch (channel) { |
| 778 | case SkColorChannel::kR: |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 779 | result = SkColorGetR(c); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 780 | break; |
| 781 | case SkColorChannel::kG: |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 782 | result = SkColorGetG(c); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 783 | break; |
| 784 | case SkColorChannel::kB: |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 785 | result = SkColorGetB(c); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 786 | break; |
| 787 | case SkColorChannel::kA: |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 788 | result = SkColorGetA(c); |
| 789 | break; |
| 790 | } |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | return result; |
| 794 | } |
| 795 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 796 | class YUVGenerator : public SkImageGenerator { |
| 797 | public: |
| 798 | YUVGenerator(const SkImageInfo& ii, |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 799 | SkYUVColorSpace yuvColorSpace, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 800 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 801 | SkBitmap bitmaps[SkYUVASizeInfo::kMaxCount]) |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 802 | : SkImageGenerator(ii) |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 803 | , fYUVColorSpace(yuvColorSpace) |
| 804 | , fAllA8(true) { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 805 | memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices)); |
| 806 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 807 | SkAssertResult(SkYUVAIndex::AreValidIndices(fYUVAIndices, &fNumBitmaps)); |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 808 | SkASSERT(fNumBitmaps > 0 && fNumBitmaps <= SkYUVASizeInfo::kMaxCount); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 809 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 810 | for (int i = 0; i < fNumBitmaps; ++i) { |
| 811 | fYUVBitmaps[i] = bitmaps[i]; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 812 | if (kAlpha_8_SkColorType != fYUVBitmaps[i].colorType()) { |
| 813 | fAllA8 = false; |
| 814 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | protected: |
| 819 | bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 820 | const Options&) override { |
| 821 | |
| 822 | if (kUnknown_SkColorType == fFlattened.colorType()) { |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 823 | fFlattened.allocPixels(info); |
Robert Phillips | e073552 | 2020-01-31 11:03:32 -0500 | [diff] [blame] | 824 | SkASSERT(kN32_SkColorType == info.colorType()); |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 825 | SkASSERT(kPremul_SkAlphaType == info.alphaType()); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 826 | |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 827 | float mtx[20]; |
| 828 | SkColorMatrix_YUV2RGB(fYUVColorSpace, mtx); |
| 829 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 830 | for (int y = 0; y < info.height(); ++y) { |
| 831 | for (int x = 0; x < info.width(); ++x) { |
| 832 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 833 | float x1 = (x + 0.5f) / info.width(); |
| 834 | float y1 = (y + 0.5f) / info.height(); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 835 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 836 | uint8_t Y = look_up(x1, y1, |
| 837 | fYUVBitmaps[fYUVAIndices[0].fIndex], |
| 838 | fYUVAIndices[0].fChannel); |
| 839 | |
| 840 | uint8_t U = look_up(x1, y1, |
| 841 | fYUVBitmaps[fYUVAIndices[1].fIndex], |
| 842 | fYUVAIndices[1].fChannel); |
| 843 | |
| 844 | |
| 845 | uint8_t V = look_up(x1, y1, |
| 846 | fYUVBitmaps[fYUVAIndices[2].fIndex], |
| 847 | fYUVAIndices[2].fChannel); |
| 848 | |
| 849 | uint8_t A = 255; |
| 850 | if (fYUVAIndices[3].fIndex >= 0) { |
| 851 | A = look_up(x1, y1, |
| 852 | fYUVBitmaps[fYUVAIndices[3].fIndex], |
| 853 | fYUVAIndices[3].fChannel); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | // Making premul here. |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 857 | *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba(mtx, Y, U, V, A); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | return fFlattened.readPixels(info, pixels, rowBytes, 0, 0); |
| 863 | } |
| 864 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 865 | bool onQueryYUVA8(SkYUVASizeInfo* size, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 866 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
| 867 | SkYUVColorSpace* yuvColorSpace) const override { |
| 868 | |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 869 | // The onQueryYUVA8/onGetYUVA8Planes can only handle A8 planes |
| 870 | if (!fAllA8) { |
| 871 | return false; |
| 872 | } |
| 873 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 874 | memcpy(yuvaIndices, fYUVAIndices, sizeof(fYUVAIndices)); |
| 875 | *yuvColorSpace = fYUVColorSpace; |
| 876 | |
| 877 | int i = 0; |
| 878 | for ( ; i < fNumBitmaps; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 879 | size->fSizes[i].fWidth = fYUVBitmaps[i].width(); |
| 880 | size->fSizes[i].fHeight = fYUVBitmaps[i].height(); |
| 881 | size->fWidthBytes[i] = fYUVBitmaps[i].rowBytes(); |
| 882 | } |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 883 | for ( ; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 884 | size->fSizes[i].fWidth = 0; |
| 885 | size->fSizes[i].fHeight = 0; |
| 886 | size->fWidthBytes[i] = 0; |
Jim Van Verth | f99a674 | 2018-10-18 16:13:18 +0000 | [diff] [blame] | 887 | } |
Jim Van Verth | 0c583af | 2018-10-18 10:31:59 -0400 | [diff] [blame] | 888 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 889 | return true; |
| 890 | } |
| 891 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 892 | bool onGetYUVA8Planes(const SkYUVASizeInfo&, const SkYUVAIndex[SkYUVAIndex::kIndexCount], |
| 893 | void* planes[SkYUVASizeInfo::kMaxCount]) override { |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 894 | SkASSERT(fAllA8); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 895 | for (int i = 0; i < fNumBitmaps; ++i) { |
| 896 | planes[i] = fYUVBitmaps[i].getPixels(); |
| 897 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 898 | return true; |
| 899 | } |
| 900 | |
| 901 | private: |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 902 | SkYUVColorSpace fYUVColorSpace; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 903 | SkYUVAIndex fYUVAIndices[SkYUVAIndex::kIndexCount]; |
| 904 | int fNumBitmaps; |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 905 | SkBitmap fYUVBitmaps[SkYUVASizeInfo::kMaxCount]; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 906 | SkBitmap fFlattened; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 907 | bool fAllA8; // are all the SkBitmaps in "fYUVBitmaps" A8? |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 908 | |
| 909 | }; |
| 910 | |
| 911 | static sk_sp<SkImage> make_yuv_gen_image(const SkImageInfo& ii, |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 912 | SkYUVColorSpace yuvColorSpace, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 913 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 914 | SkBitmap bitmaps[]) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 915 | std::unique_ptr<SkImageGenerator> gen(new YUVGenerator(ii, yuvColorSpace, |
| 916 | yuvaIndices, bitmaps)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 917 | |
| 918 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 919 | } |
| 920 | |
| 921 | static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) { |
Brian Osman | 2b73e66 | 2019-11-01 10:02:24 -0400 | [diff] [blame] | 922 | static const char* kYUVColorSpaceNames[] = { "JPEG", "601", "709", "2020", "Identity" }; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 923 | static_assert(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace + 1); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 924 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 925 | SkPaint paint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 926 | SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 927 | font.setEdging(SkFont::Edging::kAlias); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 928 | |
| 929 | SkRect textRect; |
| 930 | SkString colLabel; |
| 931 | |
| 932 | colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 933 | font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 934 | int y = textRect.height(); |
| 935 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 936 | SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 937 | |
| 938 | colLabel.printf("%s", opaque ? "Opaque" : "Transparent"); |
Mike Reed | 71f5a0b | 2018-10-25 16:12:39 -0400 | [diff] [blame] | 939 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 940 | font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 941 | y += textRect.height(); |
| 942 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 943 | SkTextUtils::DrawString(canvas, colLabel.c_str(), x, y, font, paint, SkTextUtils::kCenter_Align); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 947 | static const char* kYUVFormatNames[] = { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 948 | "P016", "P010", "P016F", "Y416", "AYUV", "Y410", "NV12", "NV21", "I420", "YV12" |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 949 | }; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 950 | static_assert(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat + 1); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 951 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 952 | SkPaint paint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 953 | SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 954 | font.setEdging(SkFont::Edging::kAlias); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 955 | |
| 956 | SkRect textRect; |
| 957 | SkString rowLabel; |
| 958 | |
| 959 | rowLabel.printf("%s", kYUVFormatNames[yuvFormat]); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 960 | font.measureText(rowLabel.c_str(), rowLabel.size(), SkTextEncoding::kUTF8, &textRect); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 961 | y += kTileWidthHeight/2 + textRect.height()/2; |
| 962 | |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 963 | canvas->drawString(rowLabel, 0, y, font, paint); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 964 | } |
| 965 | |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 966 | static GrBackendTexture create_yuva_texture(GrContext* context, const SkBitmap& bm, |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 967 | SkYUVAIndex yuvaIndices[4], int texIndex, |
| 968 | YUVFormat yuvFormat) { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 969 | #ifdef SK_DEBUG |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 970 | SkASSERT(texIndex >= 0 && texIndex <= 3); |
| 971 | int channelCount = 0; |
| 972 | for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) { |
| 973 | if (yuvaIndices[i].fIndex == texIndex) { |
| 974 | ++channelCount; |
| 975 | } |
| 976 | } |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 977 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 978 | if (format_uses_16_bpp(yuvFormat) || 2 == channelCount) { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 979 | if (2 == channelCount) { |
| 980 | if (format_uses_16_bpp(yuvFormat)) { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 981 | if (yuvFormat == kP016F_YUVFormat) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 982 | SkASSERT(kR16G16_float_SkColorType == bm.colorType()); |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 983 | } else { |
| 984 | SkASSERT(yuvFormat == kP016_YUVFormat || yuvFormat == kP010_YUVFormat); |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 985 | SkASSERT(kR16G16_unorm_SkColorType == bm.colorType()); |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 986 | } |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 987 | } else { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 988 | SkASSERT(kR8G8_unorm_SkColorType == bm.colorType()); |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 989 | } |
| 990 | } else { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 991 | if (yuvFormat == kY416_YUVFormat) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 992 | SkASSERT(kR16G16B16A16_unorm_SkColorType == bm.colorType()); |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 993 | } else if (yuvFormat == kP016F_YUVFormat) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 994 | SkASSERT(kA16_float_SkColorType == bm.colorType()); |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 995 | } else { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 996 | SkASSERT(yuvFormat == kP016_YUVFormat || yuvFormat == kP010_YUVFormat); |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 997 | SkASSERT(kA16_unorm_SkColorType == bm.colorType()); |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 998 | } |
| 999 | } |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 1000 | } |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 1001 | #endif |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 1002 | |
Robert Phillips | 6694440 | 2019-09-30 13:21:25 -0400 | [diff] [blame] | 1003 | return context->createBackendTexture(&bm.pixmap(), 1, GrRenderable::kNo, GrProtected::kNo); |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 1004 | } |
| 1005 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 1006 | static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() { |
| 1007 | static const float kJPEGConversionMatrix[20] = { |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 1008 | 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255, |
| 1009 | 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255, |
| 1010 | 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255, |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 1011 | 0.0f, 0.0f, 0.0f, 1.0f, 0.0f |
| 1012 | }; |
| 1013 | |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 1014 | return SkColorFilters::Matrix(kJPEGConversionMatrix); |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 1015 | } |
| 1016 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1017 | // Get the SkColorType to use when creating an SkSurface wrapping 'format'. |
| 1018 | static SkColorType get_color_type(const GrBackendFormat& format) { |
| 1019 | |
| 1020 | GrGLFormat glFormat = format.asGLFormat(); |
| 1021 | if (GrGLFormat::kUnknown != glFormat) { |
| 1022 | switch (glFormat) { |
| 1023 | case GrGLFormat::kLUMINANCE8: // fall through |
| 1024 | case GrGLFormat::kR8: // fall through |
| 1025 | case GrGLFormat::kALPHA8: return kAlpha_8_SkColorType; |
| 1026 | case GrGLFormat::kRG8: return kR8G8_unorm_SkColorType; |
| 1027 | case GrGLFormat::kRGB8: return kRGB_888x_SkColorType; |
| 1028 | case GrGLFormat::kRGBA8: return kRGBA_8888_SkColorType; |
| 1029 | case GrGLFormat::kBGRA8: return kBGRA_8888_SkColorType; |
| 1030 | case GrGLFormat::kRGB10_A2: return kRGBA_1010102_SkColorType; |
| 1031 | case GrGLFormat::kLUMINANCE16F: // fall through |
| 1032 | case GrGLFormat::kR16F: return kA16_float_SkColorType; |
| 1033 | case GrGLFormat::kRG16F: return kR16G16_float_SkColorType; |
| 1034 | case GrGLFormat::kR16: return kA16_unorm_SkColorType; |
| 1035 | case GrGLFormat::kRG16: return kR16G16_unorm_SkColorType; |
| 1036 | case GrGLFormat::kRGBA16: return kR16G16B16A16_unorm_SkColorType; |
| 1037 | default: return kUnknown_SkColorType; |
| 1038 | } |
| 1039 | |
| 1040 | SkUNREACHABLE; |
| 1041 | } |
| 1042 | |
| 1043 | VkFormat vkFormat; |
| 1044 | if (format.asVkFormat(&vkFormat)) { |
| 1045 | switch (vkFormat) { |
| 1046 | case VK_FORMAT_R8_UNORM: return kAlpha_8_SkColorType; |
| 1047 | case VK_FORMAT_R8G8_UNORM: return kR8G8_unorm_SkColorType; |
| 1048 | case VK_FORMAT_R8G8B8_UNORM: return kRGB_888x_SkColorType; |
| 1049 | case VK_FORMAT_R8G8B8A8_UNORM: return kRGBA_8888_SkColorType; |
| 1050 | case VK_FORMAT_B8G8R8A8_UNORM: return kBGRA_8888_SkColorType; |
| 1051 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: return kRGBA_1010102_SkColorType; |
| 1052 | case VK_FORMAT_R16_SFLOAT: return kA16_float_SkColorType; |
| 1053 | case VK_FORMAT_R16G16_SFLOAT: return kR16G16_float_SkColorType; |
| 1054 | case VK_FORMAT_R16_UNORM: return kA16_unorm_SkColorType; |
| 1055 | case VK_FORMAT_R16G16_UNORM: return kR16G16_unorm_SkColorType; |
| 1056 | case VK_FORMAT_R16G16B16A16_UNORM: return kR16G16B16A16_unorm_SkColorType; |
| 1057 | default: return kUnknown_SkColorType; |
| 1058 | } |
| 1059 | |
| 1060 | SkUNREACHABLE; |
| 1061 | } |
| 1062 | |
| 1063 | return kUnknown_SkColorType; |
| 1064 | } |
| 1065 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1066 | namespace skiagm { |
| 1067 | |
| 1068 | // This GM creates an opaque and transparent bitmap, extracts the planes and then recombines |
| 1069 | // them into various YUV formats. It then renders the results in the grid: |
| 1070 | // |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 1071 | // JPEG 601 709 Identity |
| 1072 | // Transparent Opaque Transparent Opaque Transparent Opaque Transparent Opaque |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 1073 | // originals |
| 1074 | // P016 |
| 1075 | // P010 |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 1076 | // P016F |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 1077 | // Y416 |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1078 | // AYUV |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 1079 | // Y410 |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1080 | // NV12 |
| 1081 | // NV21 |
| 1082 | // I420 |
| 1083 | // YV12 |
| 1084 | class WackyYUVFormatsGM : public GM { |
| 1085 | public: |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1086 | WackyYUVFormatsGM(bool useTargetColorSpace, bool useDomain, bool quarterSize) |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1087 | : fUseTargetColorSpace(useTargetColorSpace) |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1088 | , fUseDomain(useDomain) |
| 1089 | , fQuarterSize(quarterSize) { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1090 | this->setBGColor(0xFFCCCCCC); |
| 1091 | } |
| 1092 | |
| 1093 | protected: |
| 1094 | |
| 1095 | SkString onShortName() override { |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 1096 | SkString name("wacky_yuv_formats"); |
| 1097 | if (fUseTargetColorSpace) { |
| 1098 | name += "_cs"; |
| 1099 | } |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1100 | if (fUseDomain) { |
| 1101 | name += "_domain"; |
| 1102 | } |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1103 | if (fQuarterSize) { |
| 1104 | name += "_qtr"; |
| 1105 | } |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 1106 | |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 1107 | return name; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | SkISize onISize() override { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 1111 | int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x #-color-spaces |
| 1112 | int numRows = 1 + (kLast_YUVFormat + 1); // original + #-yuv-formats |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1113 | int wh = SkScalarCeilToInt(kTileWidthHeight * (fUseDomain ? 1.5f : 1.f)); |
| 1114 | return SkISize::Make(kLabelWidth + numCols * (wh + kPad), |
| 1115 | kLabelHeight + numRows * (wh + kPad)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | void onOnceBeforeDraw() override { |
| 1119 | SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f }; |
| 1120 | float outerRadius = kTileWidthHeight/2.0f - 20.0f; |
| 1121 | float innerRadius = 20.0f; |
| 1122 | |
| 1123 | { |
| 1124 | // transparent |
| 1125 | SkTDArray<SkRect> circles; |
| 1126 | SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1127 | fOriginalBMs[0] = make_bitmap(kRGBA_8888_SkColorType, path, circles, false, fUseDomain); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | { |
| 1131 | // opaque |
| 1132 | SkTDArray<SkRect> circles; |
| 1133 | SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1134 | fOriginalBMs[1] = make_bitmap(kRGBA_8888_SkColorType, path, circles, true, fUseDomain); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1135 | } |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 1136 | |
| 1137 | if (fUseTargetColorSpace) { |
| 1138 | fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin(); |
| 1139 | } |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 1140 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1141 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1142 | // Resize all the backend textures in 'yuvaTextures' to a quarter their size. |
| 1143 | sk_sp<SkImage> resizeOnGpu(GrContext* context, |
| 1144 | YUVFormat yuvFormat, |
| 1145 | SkYUVColorSpace yuvColorSpace, |
| 1146 | bool opaque, |
| 1147 | const GrBackendTexture yuvaTextures[], |
| 1148 | const SkYUVAIndex yuvaIndices[4], |
| 1149 | int numTextures, |
| 1150 | SkISize imageSize) { |
| 1151 | GrBackendTexture shrunkTextures[4]; |
| 1152 | |
| 1153 | for (int i = 0; i < numTextures; ++i) { |
| 1154 | SkColorType ct = get_color_type(yuvaTextures[i].getBackendFormat()); |
| 1155 | if (ct == kUnknown_SkColorType || !context->colorTypeSupportedAsSurface(ct)) { |
| 1156 | return nullptr; |
| 1157 | } |
| 1158 | |
Robert Phillips | e073552 | 2020-01-31 11:03:32 -0500 | [diff] [blame] | 1159 | if (ct == kRGBA_8888_SkColorType || ct == kRGBA_1010102_SkColorType) { |
| 1160 | // We disallow resizing AYUV and Y410 formats on the GPU bc resizing them w/ a |
| 1161 | // premul draw combines the YUV channels w/ the A channel in an inappropriate |
| 1162 | // manner. |
| 1163 | return nullptr; |
| 1164 | } |
| 1165 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1166 | SkISize shrunkPlaneSize = { yuvaTextures[i].width() / 2, yuvaTextures[i].height() / 2 }; |
| 1167 | |
| 1168 | sk_sp<SkImage> wrappedOrig = SkImage::MakeFromTexture(context, yuvaTextures[i], |
| 1169 | kTopLeft_GrSurfaceOrigin, |
| 1170 | ct, |
| 1171 | kPremul_SkAlphaType, |
| 1172 | nullptr); |
| 1173 | |
| 1174 | shrunkTextures[i] = context->createBackendTexture(shrunkPlaneSize.width(), |
| 1175 | shrunkPlaneSize.height(), |
| 1176 | yuvaTextures[i].getBackendFormat(), |
| 1177 | GrMipMapped::kNo, |
| 1178 | GrRenderable::kYes); |
| 1179 | if (!shrunkTextures[i].isValid()) { |
| 1180 | return nullptr; |
| 1181 | } |
| 1182 | |
| 1183 | // Store this away so it will be cleaned up at the end. |
| 1184 | fBackendTextures.push_back(shrunkTextures[i]); |
| 1185 | |
| 1186 | sk_sp<SkSurface> s = SkSurface::MakeFromBackendTexture(context, shrunkTextures[i], |
| 1187 | kTopLeft_GrSurfaceOrigin, 0, |
| 1188 | ct, nullptr, nullptr); |
| 1189 | if (!s) { |
| 1190 | return nullptr; |
| 1191 | } |
| 1192 | SkCanvas* c = s->getCanvas(); |
| 1193 | |
| 1194 | SkPaint paint; |
| 1195 | paint.setBlendMode(SkBlendMode::kSrc); |
| 1196 | |
| 1197 | c->drawImageRect(wrappedOrig, |
| 1198 | SkRect::MakeWH(shrunkPlaneSize.width(), shrunkPlaneSize.height()), |
| 1199 | &paint); |
| 1200 | |
| 1201 | s->flush(); |
| 1202 | } |
| 1203 | |
| 1204 | SkISize shrunkImageSize = { imageSize.width() / 2, imageSize.height() / 2 }; |
| 1205 | |
| 1206 | return SkImage::MakeFromYUVATextures(context, |
| 1207 | yuvColorSpace, |
| 1208 | shrunkTextures, |
| 1209 | yuvaIndices, |
| 1210 | shrunkImageSize, |
| 1211 | kTopLeft_GrSurfaceOrigin); |
| 1212 | } |
| 1213 | |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 1214 | void createImages(GrContext* context) { |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 1215 | int counter = 0; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1216 | for (bool opaque : { false, true }) { |
| 1217 | for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) { |
| 1218 | PlaneData planes; |
| 1219 | extract_planes(fOriginalBMs[opaque], (SkYUVColorSpace) cs, &planes); |
| 1220 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 1221 | for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1222 | SkBitmap resultBMs[4]; |
| 1223 | SkYUVAIndex yuvaIndices[4]; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 1224 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1225 | create_YUV(planes, (YUVFormat) format, resultBMs, yuvaIndices, opaque); |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 1226 | |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 1227 | int numTextures; |
| 1228 | if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) { |
| 1229 | continue; |
| 1230 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1231 | |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 1232 | if (context) { |
Chris Dalton | 382b122 | 2019-02-07 10:05:55 +0000 | [diff] [blame] | 1233 | if (context->abandoned()) { |
| 1234 | return; |
| 1235 | } |
| 1236 | |
Robert Phillips | f47717a | 2019-09-19 13:04:33 -0400 | [diff] [blame] | 1237 | if (!is_format_natively_supported(context, (YUVFormat) format)) { |
| 1238 | continue; |
| 1239 | } |
| 1240 | |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 1241 | GrBackendTexture yuvaTextures[4]; |
Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 1242 | SkPixmap yuvaPixmaps[4]; |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 1243 | |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 1244 | for (int i = 0; i < numTextures; ++i) { |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 1245 | yuvaTextures[i] = create_yuva_texture(context, resultBMs[i], |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 1246 | yuvaIndices, i, |
| 1247 | (YUVFormat) format); |
Brian Salomon | f2580c0 | 2019-01-30 09:13:31 -0500 | [diff] [blame] | 1248 | if (yuvaTextures[i].isValid()) { |
| 1249 | fBackendTextures.push_back(yuvaTextures[i]); |
| 1250 | } |
Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 1251 | yuvaPixmaps[i] = resultBMs[i].pixmap(); |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 1252 | } |
| 1253 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1254 | if (fQuarterSize) { |
| 1255 | fImages[opaque][cs][format] = this->resizeOnGpu( |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 1256 | context, |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1257 | (YUVFormat) format, |
| 1258 | (SkYUVColorSpace) cs, |
| 1259 | opaque, |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 1260 | yuvaTextures, |
| 1261 | yuvaIndices, |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1262 | numTextures, |
| 1263 | fOriginalBMs[opaque].dimensions()); |
| 1264 | } else { |
| 1265 | int counterMod = counter % 3; |
| 1266 | if (fUseDomain && counterMod == 0) { |
| 1267 | // Copies flatten to RGB when they copy the YUVA data, which doesn't |
| 1268 | // know about the intended domain and the domain padding bleeds in |
| 1269 | counterMod = 1; |
| 1270 | } |
| 1271 | |
| 1272 | switch (counterMod) { |
| 1273 | case 0: |
| 1274 | fImages[opaque][cs][format] = SkImage::MakeFromYUVATexturesCopy( |
| 1275 | context, |
| 1276 | (SkYUVColorSpace)cs, |
| 1277 | yuvaTextures, |
| 1278 | yuvaIndices, |
| 1279 | { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() }, |
| 1280 | kTopLeft_GrSurfaceOrigin); |
| 1281 | break; |
| 1282 | case 1: |
| 1283 | fImages[opaque][cs][format] = SkImage::MakeFromYUVATextures( |
| 1284 | context, |
| 1285 | (SkYUVColorSpace)cs, |
| 1286 | yuvaTextures, |
| 1287 | yuvaIndices, |
| 1288 | { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() }, |
| 1289 | kTopLeft_GrSurfaceOrigin); |
| 1290 | break; |
| 1291 | case 2: |
| 1292 | default: |
| 1293 | fImages[opaque][cs][format] = SkImage::MakeFromYUVAPixmaps( |
| 1294 | context, |
| 1295 | (SkYUVColorSpace)cs, |
| 1296 | yuvaPixmaps, |
| 1297 | yuvaIndices, |
| 1298 | { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() }, |
| 1299 | kTopLeft_GrSurfaceOrigin, true); |
| 1300 | break; |
| 1301 | } |
| 1302 | ++counter; |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 1303 | } |
| 1304 | } else { |
Robert Phillips | e073552 | 2020-01-31 11:03:32 -0500 | [diff] [blame] | 1305 | SkImageInfo ii = SkImageInfo::MakeN32(fOriginalBMs[opaque].width(), |
| 1306 | fOriginalBMs[opaque].height(), |
| 1307 | kPremul_SkAlphaType); |
| 1308 | |
| 1309 | fImages[opaque][cs][format] = make_yuv_gen_image(ii, |
| 1310 | (SkYUVColorSpace) cs, |
| 1311 | yuvaIndices, |
| 1312 | resultBMs); |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 1313 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | void onDraw(SkCanvas* canvas) override { |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 1320 | this->createImages(canvas->getGrContext()); |
| 1321 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1322 | float cellWidth = kTileWidthHeight, cellHeight = kTileWidthHeight; |
| 1323 | if (fUseDomain) { |
| 1324 | cellWidth *= 1.5f; |
| 1325 | cellHeight *= 1.5f; |
| 1326 | } |
| 1327 | |
| 1328 | SkRect origSrcRect = SkRect::MakeWH(fOriginalBMs[0].width(), fOriginalBMs[0].height()); |
| 1329 | |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1330 | SkRect srcRect = SkRect::MakeWH(fOriginalBMs[0].width(), fOriginalBMs[0].height()); |
| 1331 | SkRect dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, srcRect.width(), srcRect.height()); |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1332 | if (fQuarterSize) { |
| 1333 | if (canvas->getGrContext()) { |
| 1334 | // The src is only shrunk on the GPU |
| 1335 | srcRect = SkRect::MakeWH(fOriginalBMs[0].width()/2.0f, |
| 1336 | fOriginalBMs[0].height()/2.0f); |
| 1337 | } |
| 1338 | // but the dest is always drawn smaller |
| 1339 | dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, |
| 1340 | fOriginalBMs[0].width()/2.0f, |
| 1341 | fOriginalBMs[0].height()/2.0f); |
| 1342 | } |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1343 | |
| 1344 | SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint; |
| 1345 | if (fUseDomain) { |
| 1346 | srcRect.inset(kDomainPadding, kDomainPadding); |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1347 | origSrcRect.inset(kDomainPadding, kDomainPadding); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1348 | // Draw a larger rectangle to ensure bilerp filtering would normally read outside the |
| 1349 | // srcRect and hit the red pixels, if strict constraint weren't used. |
| 1350 | dstRect.fRight = kLabelWidth + 1.5f * srcRect.width(); |
| 1351 | dstRect.fBottom = 1.5f * srcRect.height(); |
| 1352 | constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 1353 | } |
| 1354 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1355 | for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) { |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 1356 | SkPaint paint; |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1357 | paint.setFilterQuality(kLow_SkFilterQuality); |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 1358 | if (kIdentity_SkYUVColorSpace == cs) { |
| 1359 | // The identity color space needs post processing to appear correctly |
| 1360 | paint.setColorFilter(yuv_to_rgb_colorfilter()); |
| 1361 | } |
| 1362 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1363 | for (int opaque : { 0, 1 }) { |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1364 | dstRect.offsetTo(dstRect.fLeft, kLabelHeight); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1365 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1366 | draw_col_label(canvas, dstRect.fLeft + cellWidth / 2, cs, opaque); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1367 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1368 | canvas->drawBitmapRect(fOriginalBMs[opaque], origSrcRect, dstRect, |
| 1369 | nullptr, constraint); |
| 1370 | dstRect.offset(0.f, cellHeight + kPad); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1371 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 1372 | for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) { |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1373 | draw_row_label(canvas, dstRect.fTop, format); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 1374 | if (fUseTargetColorSpace && fImages[opaque][cs][format]) { |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 1375 | // Making a CS-specific version of a kIdentity_SkYUVColorSpace YUV image |
| 1376 | // doesn't make a whole lot of sense. The colorSpace conversion will |
| 1377 | // operate on the YUV components rather than the RGB components. |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 1378 | sk_sp<SkImage> csImage = |
| 1379 | fImages[opaque][cs][format]->makeColorSpace(fTargetColorSpace); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1380 | canvas->drawImageRect(csImage, srcRect, dstRect, &paint, constraint); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 1381 | } else { |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1382 | canvas->drawImageRect(fImages[opaque][cs][format], srcRect, dstRect, |
| 1383 | &paint, constraint); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 1384 | } |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1385 | dstRect.offset(0.f, cellHeight + kPad); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1386 | } |
| 1387 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1388 | dstRect.offset(cellWidth + kPad, 0.f); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1389 | } |
| 1390 | } |
Brian Salomon | 8f388ce | 2019-01-29 14:50:53 -0500 | [diff] [blame] | 1391 | if (auto context = canvas->getGrContext()) { |
Chris Dalton | 382b122 | 2019-02-07 10:05:55 +0000 | [diff] [blame] | 1392 | if (!context->abandoned()) { |
| 1393 | context->flush(); |
| 1394 | GrGpu* gpu = context->priv().getGpu(); |
| 1395 | SkASSERT(gpu); |
| 1396 | gpu->testingOnly_flushGpuAndSync(); |
| 1397 | for (const auto& tex : fBackendTextures) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 1398 | context->deleteBackendTexture(tex); |
Chris Dalton | 382b122 | 2019-02-07 10:05:55 +0000 | [diff] [blame] | 1399 | } |
| 1400 | fBackendTextures.reset(); |
Brian Salomon | 8f388ce | 2019-01-29 14:50:53 -0500 | [diff] [blame] | 1401 | } |
Brian Salomon | 8f388ce | 2019-01-29 14:50:53 -0500 | [diff] [blame] | 1402 | } |
| 1403 | SkASSERT(!fBackendTextures.count()); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | private: |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 1407 | SkBitmap fOriginalBMs[2]; |
| 1408 | sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace + 1][kLast_YUVFormat + 1]; |
Brian Salomon | 8f388ce | 2019-01-29 14:50:53 -0500 | [diff] [blame] | 1409 | SkTArray<GrBackendTexture> fBackendTextures; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 1410 | bool fUseTargetColorSpace; |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1411 | bool fUseDomain; |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1412 | bool fQuarterSize; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 1413 | sk_sp<SkColorSpace> fTargetColorSpace; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1414 | |
| 1415 | typedef GM INHERITED; |
| 1416 | }; |
| 1417 | |
| 1418 | ////////////////////////////////////////////////////////////////////////////// |
| 1419 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 1420 | DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ false, /* quarterSize */ false);) |
| 1421 | DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ false, /* quarterSize */ true);) |
| 1422 | DEF_GM(return new WackyYUVFormatsGM(/* cs */ true, /* domain */ false, /* quarterSize */ false);) |
| 1423 | DEF_GM(return new WackyYUVFormatsGM(/* cs */ false, /* domain */ true, /* quarterSize */ false);) |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1424 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 1425 | class YUVMakeColorSpaceGM : public GpuGM { |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1426 | public: |
| 1427 | YUVMakeColorSpaceGM() { |
| 1428 | this->setBGColor(0xFFCCCCCC); |
| 1429 | } |
| 1430 | |
| 1431 | protected: |
| 1432 | SkString onShortName() override { |
| 1433 | return SkString("yuv_make_color_space"); |
| 1434 | } |
| 1435 | |
| 1436 | SkISize onISize() override { |
| 1437 | int numCols = 4; // (transparent, opaque) x (untagged, tagged) |
| 1438 | int numRows = 5; // original, YUV, subset, readPixels, makeNonTextureImage |
| 1439 | return SkISize::Make(numCols * (kTileWidthHeight + kPad) + kPad, |
| 1440 | numRows * (kTileWidthHeight + kPad) + kPad); |
| 1441 | } |
| 1442 | |
| 1443 | void onOnceBeforeDraw() override { |
| 1444 | SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f }; |
| 1445 | float outerRadius = kTileWidthHeight/2.0f - 20.0f; |
| 1446 | float innerRadius = 20.0f; |
| 1447 | |
| 1448 | { |
| 1449 | // transparent |
| 1450 | SkTDArray<SkRect> circles; |
| 1451 | SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1452 | fOriginalBMs[0] = make_bitmap(kN32_SkColorType, path, circles, false, false); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | { |
| 1456 | // opaque |
| 1457 | SkTDArray<SkRect> circles; |
| 1458 | SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 1459 | fOriginalBMs[1] = make_bitmap(kN32_SkColorType, path, circles, true, false); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin(); |
| 1463 | } |
| 1464 | |
| 1465 | void createImages(GrContext* context) { |
| 1466 | for (bool opaque : { false, true }) { |
| 1467 | PlaneData planes; |
| 1468 | extract_planes(fOriginalBMs[opaque], kJPEG_SkYUVColorSpace, &planes); |
| 1469 | |
| 1470 | SkBitmap resultBMs[4]; |
| 1471 | SkYUVAIndex yuvaIndices[4]; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 1472 | |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1473 | create_YUV(planes, kAYUV_YUVFormat, resultBMs, yuvaIndices, opaque); |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 1474 | |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1475 | int numTextures; |
| 1476 | if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) { |
| 1477 | continue; |
| 1478 | } |
| 1479 | |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1480 | GrBackendTexture yuvaTextures[4]; |
| 1481 | for (int i = 0; i < numTextures; ++i) { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 1482 | yuvaTextures[i] = create_yuva_texture(context, resultBMs[i], yuvaIndices, i, |
| 1483 | kAYUV_YUVFormat); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1484 | if (yuvaTextures[i].isValid()) { |
| 1485 | fBackendTextures.push_back(yuvaTextures[i]); |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | fImages[opaque][0] = SkImage::MakeFromYUVATextures( |
| 1490 | context, |
| 1491 | kJPEG_SkYUVColorSpace, |
| 1492 | yuvaTextures, |
| 1493 | yuvaIndices, |
| 1494 | { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() }, |
| 1495 | kTopLeft_GrSurfaceOrigin); |
| 1496 | fImages[opaque][1] = SkImage::MakeFromYUVATextures( |
| 1497 | context, |
| 1498 | kJPEG_SkYUVColorSpace, |
| 1499 | yuvaTextures, |
| 1500 | yuvaIndices, |
| 1501 | { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() }, |
| 1502 | kTopLeft_GrSurfaceOrigin, |
| 1503 | SkColorSpace::MakeSRGB()); |
| 1504 | } |
| 1505 | } |
| 1506 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 1507 | void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override { |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1508 | this->createImages(context); |
| 1509 | |
| 1510 | int x = kPad; |
| 1511 | for (int tagged : { 0, 1 }) { |
| 1512 | for (int opaque : { 0, 1 }) { |
| 1513 | int y = kPad; |
| 1514 | |
| 1515 | auto raster = SkImage::MakeFromBitmap(fOriginalBMs[opaque]) |
| 1516 | ->makeColorSpace(fTargetColorSpace); |
| 1517 | canvas->drawImage(raster, x, y); |
| 1518 | y += kTileWidthHeight + kPad; |
| 1519 | |
| 1520 | auto yuv = fImages[opaque][tagged]->makeColorSpace(fTargetColorSpace); |
| 1521 | SkASSERT(SkColorSpace::Equals(yuv->colorSpace(), fTargetColorSpace.get())); |
| 1522 | canvas->drawImage(yuv, x, y); |
| 1523 | y += kTileWidthHeight + kPad; |
| 1524 | |
| 1525 | auto subset = yuv->makeSubset(SkIRect::MakeWH(kTileWidthHeight / 2, |
| 1526 | kTileWidthHeight / 2)); |
| 1527 | canvas->drawImage(subset, x, y); |
| 1528 | y += kTileWidthHeight + kPad; |
| 1529 | |
| 1530 | auto nonTexture = yuv->makeNonTextureImage(); |
| 1531 | canvas->drawImage(nonTexture, x, y); |
| 1532 | y += kTileWidthHeight + kPad; |
| 1533 | |
| 1534 | SkBitmap readBack; |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 1535 | readBack.allocPixels(yuv->imageInfo()); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1536 | yuv->readPixels(readBack.pixmap(), 0, 0); |
| 1537 | canvas->drawBitmap(readBack, x, y); |
| 1538 | |
| 1539 | x += kTileWidthHeight + kPad; |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | context->flush(); |
| 1544 | GrGpu* gpu = context->priv().getGpu(); |
| 1545 | SkASSERT(gpu); |
| 1546 | gpu->testingOnly_flushGpuAndSync(); |
| 1547 | for (const auto& tex : fBackendTextures) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 1548 | context->deleteBackendTexture(tex); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1549 | } |
| 1550 | fBackendTextures.reset(); |
| 1551 | } |
| 1552 | |
| 1553 | private: |
| 1554 | SkBitmap fOriginalBMs[2]; |
| 1555 | sk_sp<SkImage> fImages[2][2]; |
| 1556 | SkTArray<GrBackendTexture> fBackendTextures; |
| 1557 | sk_sp<SkColorSpace> fTargetColorSpace; |
| 1558 | |
| 1559 | typedef GM INHERITED; |
| 1560 | }; |
| 1561 | |
| 1562 | DEF_GM(return new YUVMakeColorSpaceGM();) |
| 1563 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 1564 | } |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1565 | |
| 1566 | /////////////// |
| 1567 | |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1568 | #include "include/effects/SkColorMatrix.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 1569 | #include "src/core/SkAutoPixmapStorage.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 1570 | #include "tools/Resources.h" |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1571 | |
| 1572 | static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) { |
| 1573 | auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes()); |
| 1574 | canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height()); |
| 1575 | SkPaint paint; |
| 1576 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 1577 | paint.setColorFilter(cf); |
| 1578 | paint.setBlendMode(SkBlendMode::kSrc); |
| 1579 | canvas->drawImage(img, 0, 0, &paint); |
| 1580 | } |
| 1581 | |
| 1582 | static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) { |
| 1583 | float m[20]; |
| 1584 | SkColorMatrix_RGB2YUV(cs, m); |
| 1585 | |
| 1586 | memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A |
| 1587 | draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]); |
| 1588 | |
| 1589 | memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A |
| 1590 | draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]); |
| 1591 | |
| 1592 | memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A |
| 1593 | draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]); |
| 1594 | } |
| 1595 | |
| 1596 | static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 1597 | const SkImage* a, const SkImage* b) { |
| 1598 | auto sh = SkShaders::Blend(SkBlendMode::kDifference, a->makeShader(), b->makeShader()); |
| 1599 | SkPaint paint; |
| 1600 | paint.setShader(sh); |
| 1601 | canvas->save(); |
| 1602 | canvas->translate(x, y); |
| 1603 | canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint); |
| 1604 | |
| 1605 | SkColorMatrix cm; |
| 1606 | cm.setScale(64, 64, 64); |
| 1607 | paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm))); |
| 1608 | canvas->translate(0, a->height()); |
| 1609 | canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint); |
| 1610 | |
| 1611 | canvas->restore(); |
| 1612 | } |
| 1613 | |
| 1614 | // Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the |
| 1615 | // resulting (recombined) images (gpu only for now). |
| 1616 | // |
| 1617 | class YUVSplitterGM : public skiagm::GM { |
| 1618 | sk_sp<SkImage> fOrig; |
| 1619 | SkAutoPixmapStorage fStorage[3]; |
| 1620 | SkPixmap fPM[3]; |
| 1621 | |
| 1622 | public: |
| 1623 | YUVSplitterGM() {} |
| 1624 | |
| 1625 | protected: |
| 1626 | |
| 1627 | SkString onShortName() override { |
| 1628 | return SkString("yuv_splitter"); |
| 1629 | } |
| 1630 | |
| 1631 | SkISize onISize() override { |
Brian Osman | 2b73e66 | 2019-11-01 10:02:24 -0400 | [diff] [blame] | 1632 | return SkISize::Make(1280, 768); |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | void onOnceBeforeDraw() override { |
| 1636 | fOrig = GetResourceAsImage("images/mandrill_256.png"); |
| 1637 | |
| 1638 | SkImageInfo info = SkImageInfo::Make(fOrig->width(), fOrig->height(), kAlpha_8_SkColorType, |
| 1639 | kPremul_SkAlphaType); |
| 1640 | fStorage[0].alloc(info); |
| 1641 | if (0) { |
| 1642 | // if you want to scale U,V down by 1/2 |
| 1643 | info = info.makeWH(info.width()/2, info.height()/2); |
| 1644 | } |
| 1645 | fStorage[1].alloc(info); |
| 1646 | fStorage[2].alloc(info); |
| 1647 | for (int i = 0; i < 3; ++i) { |
| 1648 | fPM[i] = fStorage[i]; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | void onDraw(SkCanvas* canvas) override { |
| 1653 | SkYUVAIndex indices[4]; |
Robert Phillips | e073552 | 2020-01-31 11:03:32 -0500 | [diff] [blame] | 1654 | indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR}; |
| 1655 | indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR}; |
| 1656 | indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR}; |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1657 | indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kR}; |
| 1658 | |
| 1659 | canvas->translate(fOrig->width(), 0); |
| 1660 | canvas->save(); |
Brian Osman | 2b73e66 | 2019-11-01 10:02:24 -0400 | [diff] [blame] | 1661 | for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace, |
| 1662 | kBT2020_SkYUVColorSpace}) { |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1663 | split_into_yuv(fOrig.get(), cs, fPM); |
| 1664 | auto img = SkImage::MakeFromYUVAPixmaps(canvas->getGrContext(), cs, fPM, indices, |
| 1665 | fPM[0].info().dimensions(), |
| 1666 | kTopLeft_GrSurfaceOrigin, |
| 1667 | false, false, nullptr); |
| 1668 | if (img) { |
| 1669 | canvas->drawImage(img, 0, 0, nullptr); |
| 1670 | draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get()); |
| 1671 | } |
| 1672 | canvas->translate(fOrig->width(), 0); |
| 1673 | } |
| 1674 | canvas->restore(); |
| 1675 | canvas->translate(-fOrig->width(), 0); |
| 1676 | |
| 1677 | canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0, nullptr); |
| 1678 | canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height(), nullptr); |
| 1679 | canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]), |
| 1680 | 0, fPM[0].height() + fPM[1].height(), nullptr); |
| 1681 | } |
| 1682 | |
| 1683 | private: |
| 1684 | typedef GM INHERITED; |
| 1685 | }; |
| 1686 | DEF_GM( return new YUVSplitterGM; ) |