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