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