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" |
Mike Klein | 8aa0edf | 2020-10-16 11:04:18 -0500 | [diff] [blame] | 45 | #include "include/private/SkTPin.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 46 | #include "include/private/SkTemplates.h" |
| 47 | #include "include/utils/SkTextUtils.h" |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 48 | #include "src/core/SkConvertPixels.h" |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 49 | #include "src/core/SkYUVMath.h" |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 50 | #include "src/gpu/GrCaps.h" |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 51 | #include "src/gpu/GrRecordingContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 52 | #include "tools/ToolUtils.h" |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 53 | #include "tools/gpu/YUVUtils.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 54 | |
| 55 | #include <math.h> |
| 56 | #include <string.h> |
| 57 | #include <initializer_list> |
| 58 | #include <memory> |
| 59 | #include <utility> |
| 60 | |
| 61 | class GrRenderTargetContext; |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 62 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 63 | static const int kTileWidthHeight = 128; |
| 64 | static const int kLabelWidth = 64; |
| 65 | static const int kLabelHeight = 32; |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 66 | static const int kSubsetPadding = 8; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 67 | static const int kPad = 1; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 68 | |
| 69 | enum YUVFormat { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 70 | // 4:2:0 formats, 24 bpp |
| 71 | kP016_YUVFormat, // 16-bit Y plane + 2x2 down sampled interleaved U/V plane (2 textures) |
| 72 | // 4:2:0 formats, "15 bpp" (but really 24 bpp) |
| 73 | kP010_YUVFormat, // same as kP016 except "10 bpp". Note that it is the same memory layout |
| 74 | // except that the bottom 6 bits are zeroed out (2 textures) |
| 75 | // TODO: we're cheating a bit w/ P010 and just treating it as unorm 16. This means its |
| 76 | // fully saturated values are 65504 rather than 65535 (that is just .9995 out of 1.0 though). |
| 77 | |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 78 | // This is laid out the same as kP016 and kP010 but uses F16 unstead of U16. In this case |
| 79 | // the 10 bits/channel vs 16 bits/channel distinction isn't relevant. |
| 80 | kP016F_YUVFormat, |
| 81 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 82 | // 4:4:4 formats, 64 bpp |
| 83 | kY416_YUVFormat, // 16-bit AVYU values all interleaved (1 texture) |
| 84 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 85 | // 4:4:4 formats, 32 bpp |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 86 | kAYUV_YUVFormat, // 8-bit YUVA values all interleaved (1 texture) |
| 87 | 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] | 88 | |
| 89 | // 4:2:0 formats, 12 bpp |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 90 | kNV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled interleaved U/V planes (2 textures) |
| 91 | 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] | 92 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 93 | kI420_YUVFormat, // 8-bit Y plane + separate 2x2 down sampled U and V planes (3 textures) |
| 94 | 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] | 95 | |
| 96 | kLast_YUVFormat = kYV12_YUVFormat |
| 97 | }; |
| 98 | |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 99 | // Does the YUVFormat contain a slot for alpha? If not an external alpha plane is required for |
| 100 | // transparency. |
| 101 | static bool has_alpha_channel(YUVFormat format) { |
| 102 | switch (format) { |
| 103 | case kP016_YUVFormat: return false; |
| 104 | case kP010_YUVFormat: return false; |
| 105 | case kP016F_YUVFormat: return false; |
| 106 | case kY416_YUVFormat: return true; |
| 107 | case kAYUV_YUVFormat: return true; |
| 108 | case kY410_YUVFormat: return true; |
| 109 | case kNV12_YUVFormat: return false; |
| 110 | case kNV21_YUVFormat: return false; |
| 111 | case kI420_YUVFormat: return false; |
| 112 | case kYV12_YUVFormat: return false; |
| 113 | } |
| 114 | SkUNREACHABLE; |
| 115 | } |
| 116 | |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 117 | class YUVAPlanarConfig { |
| 118 | public: |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 119 | YUVAPlanarConfig(YUVFormat format, bool opaque) { |
| 120 | switch (format) { |
| 121 | case kP016_YUVFormat: |
| 122 | case kP010_YUVFormat: |
| 123 | case kP016F_YUVFormat: |
| 124 | case kNV12_YUVFormat: |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 125 | if (opaque) { |
| 126 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_UV_420; |
| 127 | } else { |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 128 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_UV_A_4204; |
| 129 | } |
| 130 | break; |
| 131 | case kY416_YUVFormat: |
| 132 | case kY410_YUVFormat: |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 133 | if (opaque) { |
| 134 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kUYV_444; |
| 135 | } else { |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 136 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kUYVA_4444; |
| 137 | } |
| 138 | break; |
| 139 | case kAYUV_YUVFormat: |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 140 | if (opaque) { |
| 141 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kYUV_444; |
| 142 | } else { |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 143 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kYUVA_4444; |
| 144 | } |
| 145 | break; |
| 146 | case kNV21_YUVFormat: |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 147 | if (opaque) { |
| 148 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_VU_420; |
| 149 | } else { |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 150 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_VU_A_4204; |
| 151 | } |
| 152 | break; |
| 153 | case kI420_YUVFormat: |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 154 | if (opaque) { |
| 155 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_U_V_420; |
| 156 | } else { |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 157 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_U_V_A_4204; |
| 158 | } |
| 159 | break; |
| 160 | case kYV12_YUVFormat: |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 161 | if (opaque) { |
| 162 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_V_U_420; |
| 163 | } else { |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 164 | fPlanarConfig = SkYUVAInfo::PlanarConfig::kY_V_U_A_4204; |
| 165 | } |
| 166 | break; |
| 167 | } |
| 168 | } |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 169 | |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 170 | int numPlanes() const { return SkYUVAInfo::NumPlanes(fPlanarConfig); } |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 171 | |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 172 | SkYUVAPixmaps makeYUVAPixmaps(SkISize dimensions, |
| 173 | SkYUVColorSpace yuvColorSpace, |
| 174 | const SkBitmap bitmaps[], |
| 175 | int numBitmaps) const; |
| 176 | |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 177 | private: |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 178 | SkYUVAInfo::PlanarConfig fPlanarConfig; |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 179 | }; |
| 180 | |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 181 | SkYUVAPixmaps YUVAPlanarConfig::makeYUVAPixmaps(SkISize dimensions, |
| 182 | SkYUVColorSpace yuvColorSpace, |
| 183 | const SkBitmap bitmaps[], |
| 184 | int numBitmaps) const { |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 185 | SkYUVAInfo info(dimensions, fPlanarConfig, yuvColorSpace); |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 186 | SkPixmap pmaps[SkYUVAInfo::kMaxPlanes]; |
| 187 | int n = info.numPlanes(); |
| 188 | if (numBitmaps < n) { |
| 189 | return {}; |
| 190 | } |
| 191 | for (int i = 0; i < n; ++i) { |
| 192 | pmaps[i] = bitmaps[i].pixmap(); |
| 193 | } |
| 194 | return SkYUVAPixmaps::FromExternalPixmaps(info, pmaps); |
| 195 | } |
| 196 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 197 | // All the planes we need to construct the various YUV formats |
| 198 | struct PlaneData { |
| 199 | SkBitmap fYFull; |
| 200 | SkBitmap fUFull; |
| 201 | SkBitmap fVFull; |
| 202 | SkBitmap fAFull; |
| 203 | SkBitmap fUQuarter; // 2x2 downsampled U channel |
| 204 | SkBitmap fVQuarter; // 2x2 downsampled V channel |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 205 | |
| 206 | SkBitmap fFull; |
| 207 | SkBitmap fQuarter; // 2x2 downsampled YUVA |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | // Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle |
| 211 | // and have tangents 'v1' and 'v2'. |
| 212 | static void add_arc(SkPath* path, |
| 213 | const SkPoint& o1, const SkVector& v1, |
| 214 | const SkPoint& o2, const SkVector& v2, |
| 215 | SkTDArray<SkRect>* circles, bool takeLongWayRound) { |
| 216 | |
| 217 | SkVector v3 = { -v1.fY, v1.fX }; |
| 218 | SkVector v4 = { v2.fY, -v2.fX }; |
| 219 | |
| 220 | SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4); |
| 221 | SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY }; |
| 222 | |
| 223 | SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t }; |
| 224 | |
| 225 | if (circles) { |
| 226 | circles->push_back(r); |
| 227 | } |
| 228 | |
| 229 | SkVector startV = o1 - center, endV = o2 - center; |
| 230 | startV.normalize(); |
| 231 | endV.normalize(); |
| 232 | |
| 233 | SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX)); |
| 234 | SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX)); |
| 235 | |
| 236 | startDeg += 360.0f; |
| 237 | startDeg = fmodf(startDeg, 360.0f); |
| 238 | |
| 239 | endDeg += 360.0f; |
| 240 | endDeg = fmodf(endDeg, 360.0f); |
| 241 | |
| 242 | if (endDeg < startDeg) { |
| 243 | endDeg += 360.0f; |
| 244 | } |
| 245 | |
| 246 | SkScalar sweepDeg = SkTAbs(endDeg - startDeg); |
| 247 | if (!takeLongWayRound) { |
| 248 | sweepDeg = sweepDeg - 360; |
| 249 | } |
| 250 | |
| 251 | path->arcTo(r, startDeg, sweepDeg, false); |
| 252 | } |
| 253 | |
| 254 | static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius, |
| 255 | SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) { |
| 256 | if (numLobes <= 1) { |
| 257 | return SkPath(); |
| 258 | } |
| 259 | |
| 260 | SkPath p; |
| 261 | |
| 262 | int numDivisions = 2 * numLobes; |
| 263 | SkScalar fullLobeDegrees = 360.0f / numLobes; |
| 264 | SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f); |
| 265 | SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f); |
| 266 | SkMatrix outerStep, innerStep; |
| 267 | outerStep.setRotate(outDegrees); |
| 268 | innerStep.setRotate(innerDegrees); |
| 269 | SkVector curV = SkVector::Make(0.0f, 1.0f); |
| 270 | |
| 271 | if (circles) { |
| 272 | circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius, |
| 273 | o.fX + innerRadius, o.fY + innerRadius)); |
| 274 | } |
| 275 | |
| 276 | p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY); |
| 277 | |
| 278 | for (int i = 0; i < numDivisions; ++i) { |
| 279 | |
| 280 | SkVector nextV; |
| 281 | if (0 == (i % 2)) { |
| 282 | nextV = outerStep.mapVector(curV.fX, curV.fY); |
| 283 | |
| 284 | SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX, |
| 285 | o.fY + outerRadius * curV.fY); |
| 286 | SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX, |
| 287 | o.fY + outerRadius * nextV.fY); |
| 288 | |
| 289 | p.lineTo(top); |
| 290 | add_arc(&p, top, curV, nextTop, nextV, circles, true); |
| 291 | } else { |
| 292 | nextV = innerStep.mapVector(curV.fX, curV.fY); |
| 293 | |
| 294 | SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX, |
| 295 | o.fY + innerRadius * curV.fY); |
| 296 | SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX, |
| 297 | o.fY + innerRadius * nextV.fY); |
| 298 | |
| 299 | p.lineTo(bot); |
| 300 | add_arc(&p, bot, curV, nextBot, nextV, nullptr, false); |
| 301 | } |
| 302 | |
| 303 | curV = nextV; |
| 304 | } |
| 305 | |
| 306 | p.close(); |
| 307 | |
| 308 | return p; |
| 309 | } |
| 310 | |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 311 | static SkBitmap make_bitmap(SkColorType colorType, const SkPath& path, |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 312 | const SkTDArray<SkRect>& circles, bool opaque, bool padWithRed) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 313 | const SkColor kGreen = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104)); |
| 314 | const SkColor kBlue = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252)); |
| 315 | const SkColor kYellow = ToolUtils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 316 | |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 317 | int widthHeight = kTileWidthHeight + (padWithRed ? 2 * kSubsetPadding : 0); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 318 | |
| 319 | SkImageInfo ii = SkImageInfo::Make(widthHeight, widthHeight, |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 320 | colorType, kPremul_SkAlphaType); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 321 | |
| 322 | SkBitmap bm; |
| 323 | bm.allocPixels(ii); |
| 324 | |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 325 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(ii, |
| 326 | bm.getPixels(), |
| 327 | bm.rowBytes()); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 328 | if (padWithRed) { |
| 329 | canvas->clear(SK_ColorRED); |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 330 | canvas->translate(kSubsetPadding, kSubsetPadding); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 331 | canvas->clipRect(SkRect::MakeWH(kTileWidthHeight, kTileWidthHeight)); |
| 332 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 333 | canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT); |
| 334 | |
| 335 | SkPaint paint; |
| 336 | paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency |
| 337 | paint.setColor(kBlue); |
| 338 | |
| 339 | canvas->drawPath(path, paint); |
| 340 | |
| 341 | paint.setColor(opaque ? kYellow : SK_ColorTRANSPARENT); |
| 342 | paint.setBlendMode(SkBlendMode::kSrc); |
| 343 | for (int i = 0; i < circles.count(); ++i) { |
| 344 | SkRect r = circles[i]; |
| 345 | r.inset(r.width()/4, r.height()/4); |
| 346 | canvas->drawOval(r, paint); |
| 347 | } |
| 348 | |
| 349 | return bm; |
| 350 | } |
| 351 | |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 352 | static void convert_rgba_to_yuva(const float mtx[20], SkColor col, uint8_t yuv[4]) { |
| 353 | const uint8_t r = SkColorGetR(col); |
| 354 | const uint8_t g = SkColorGetG(col); |
| 355 | const uint8_t b = SkColorGetB(col); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 356 | |
Brian Osman | aba642c | 2020-02-06 12:52:25 -0500 | [diff] [blame] | 357 | yuv[0] = SkTPin(SkScalarRoundToInt(mtx[ 0]*r + mtx[ 1]*g + mtx[ 2]*b + mtx[ 4]*255), 0, 255); |
| 358 | yuv[1] = SkTPin(SkScalarRoundToInt(mtx[ 5]*r + mtx[ 6]*g + mtx[ 7]*b + mtx[ 9]*255), 0, 255); |
| 359 | 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] | 360 | yuv[3] = SkColorGetA(col); |
| 361 | } |
| 362 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 363 | static void extract_planes(const SkBitmap& bm, SkYUVColorSpace yuvColorSpace, PlaneData* planes) { |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 364 | if (kIdentity_SkYUVColorSpace == yuvColorSpace) { |
| 365 | // To test the identity color space we use JPEG YUV planes |
| 366 | yuvColorSpace = kJPEG_SkYUVColorSpace; |
| 367 | } |
| 368 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 369 | SkASSERT(!(bm.width() % 2)); |
| 370 | SkASSERT(!(bm.height() % 2)); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 371 | planes->fYFull.allocPixels( |
| 372 | SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType)); |
| 373 | planes->fUFull.allocPixels( |
| 374 | SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType)); |
| 375 | planes->fVFull.allocPixels( |
| 376 | SkImageInfo::Make(bm.dimensions(), kGray_8_SkColorType, kUnpremul_SkAlphaType)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 377 | planes->fAFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height())); |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 378 | planes->fUQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2, |
| 379 | kGray_8_SkColorType, kUnpremul_SkAlphaType)); |
| 380 | planes->fVQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2, |
| 381 | kGray_8_SkColorType, kUnpremul_SkAlphaType)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 382 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 383 | planes->fFull.allocPixels( |
| 384 | SkImageInfo::Make(bm.dimensions(), kRGBA_F32_SkColorType, kUnpremul_SkAlphaType)); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 385 | planes->fQuarter.allocPixels(SkImageInfo::Make(bm.width()/2, bm.height()/2, |
| 386 | kRGBA_F32_SkColorType, kUnpremul_SkAlphaType)); |
| 387 | |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 388 | float mtx[20]; |
| 389 | SkColorMatrix_RGB2YUV(yuvColorSpace, mtx); |
| 390 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 391 | SkColor4f* dst = (SkColor4f *) planes->fFull.getAddr(0, 0); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 392 | for (int y = 0; y < bm.height(); ++y) { |
| 393 | for (int x = 0; x < bm.width(); ++x) { |
| 394 | SkColor col = bm.getColor(x, y); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 395 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 396 | uint8_t yuva[4]; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 397 | |
Brian Osman | ac8a16c | 2019-10-31 10:24:12 -0400 | [diff] [blame] | 398 | convert_rgba_to_yuva(mtx, col, yuva); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 399 | |
| 400 | *planes->fYFull.getAddr8(x, y) = yuva[0]; |
| 401 | *planes->fUFull.getAddr8(x, y) = yuva[1]; |
| 402 | *planes->fVFull.getAddr8(x, y) = yuva[2]; |
| 403 | *planes->fAFull.getAddr8(x, y) = yuva[3]; |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 404 | |
| 405 | // TODO: render in F32 rather than converting here |
| 406 | dst->fR = yuva[0] / 255.0f; |
| 407 | dst->fG = yuva[1] / 255.0f; |
| 408 | dst->fB = yuva[2] / 255.0f; |
| 409 | dst->fA = yuva[3] / 255.0f; |
| 410 | ++dst; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 414 | dst = (SkColor4f *) planes->fQuarter.getAddr(0, 0); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 415 | for (int y = 0; y < bm.height()/2; ++y) { |
| 416 | for (int x = 0; x < bm.width()/2; ++x) { |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 417 | uint32_t yAccum = 0, uAccum = 0, vAccum = 0, aAccum = 0; |
| 418 | |
| 419 | yAccum += *planes->fYFull.getAddr8(2*x, 2*y); |
| 420 | yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y); |
| 421 | yAccum += *planes->fYFull.getAddr8(2*x, 2*y+1); |
| 422 | yAccum += *planes->fYFull.getAddr8(2*x+1, 2*y+1); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 423 | |
| 424 | uAccum += *planes->fUFull.getAddr8(2*x, 2*y); |
| 425 | uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y); |
| 426 | uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1); |
| 427 | uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1); |
| 428 | |
| 429 | *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f; |
| 430 | |
| 431 | vAccum += *planes->fVFull.getAddr8(2*x, 2*y); |
| 432 | vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y); |
| 433 | vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1); |
| 434 | vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1); |
| 435 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 436 | *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f; |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 437 | |
| 438 | aAccum += *planes->fAFull.getAddr8(2*x, 2*y); |
| 439 | aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y); |
| 440 | aAccum += *planes->fAFull.getAddr8(2*x, 2*y+1); |
| 441 | aAccum += *planes->fAFull.getAddr8(2*x+1, 2*y+1); |
| 442 | |
| 443 | // TODO: render in F32 rather than converting here |
| 444 | dst->fR = yAccum / (4.0f * 255.0f); |
| 445 | dst->fG = uAccum / (4.0f * 255.0f); |
| 446 | dst->fB = vAccum / (4.0f * 255.0f); |
| 447 | dst->fA = aAccum / (4.0f * 255.0f); |
| 448 | ++dst; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 453 | // 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] | 454 | // uv (i.e., NV12) or vu (i.e., NV21). |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 455 | static SkBitmap make_quarter_2_channel(const SkBitmap& fullY, |
| 456 | const SkBitmap& quarterU, |
| 457 | const SkBitmap& quarterV, |
| 458 | bool uv) { |
| 459 | SkBitmap result; |
| 460 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 461 | result.allocPixels(SkImageInfo::Make(fullY.width()/2, |
| 462 | fullY.height()/2, |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 463 | kR8G8_unorm_SkColorType, |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 464 | kUnpremul_SkAlphaType)); |
| 465 | |
| 466 | for (int y = 0; y < fullY.height()/2; ++y) { |
| 467 | for (int x = 0; x < fullY.width()/2; ++x) { |
| 468 | uint8_t u8 = *quarterU.getAddr8(x, y); |
| 469 | uint8_t v8 = *quarterV.getAddr8(x, y); |
| 470 | |
| 471 | if (uv) { |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 472 | *result.getAddr16(x, y) = (v8 << 8) | u8; |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 473 | } else { |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 474 | *result.getAddr16(x, y) = (u8 << 8) | v8; |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return result; |
| 480 | } |
| 481 | |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 482 | // Create some flavor of a 16bits/channel bitmap from a RGBA_F32 source |
| 483 | static SkBitmap make_16(const SkBitmap& src, SkColorType dstCT, |
| 484 | std::function<void(uint16_t* dstPixel, const float* srcPixel)> convert) { |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 485 | SkASSERT(src.colorType() == kRGBA_F32_SkColorType); |
| 486 | |
| 487 | SkBitmap result; |
| 488 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 489 | result.allocPixels(SkImageInfo::Make(src.dimensions(), dstCT, kUnpremul_SkAlphaType)); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 490 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 491 | for (int y = 0; y < src.height(); ++y) { |
| 492 | for (int x = 0; x < src.width(); ++x) { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 493 | const float* srcPixel = (const float*) src.getAddr(x, y); |
| 494 | uint16_t* dstPixel = (uint16_t*) result.getAddr(x, y); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 495 | |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 496 | convert(dstPixel, srcPixel); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | return result; |
| 501 | } |
| 502 | |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 503 | 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] | 504 | |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 505 | // Recombine the separate planes into some YUV format. Returns the number of planes. |
| 506 | static int create_YUV(const PlaneData& planes, |
| 507 | YUVFormat yuvFormat, |
| 508 | SkBitmap resultBMs[], |
| 509 | bool opaque) { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 510 | int nextLayer = 0; |
| 511 | |
| 512 | switch (yuvFormat) { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 513 | case kY416_YUVFormat: { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 514 | resultBMs[nextLayer++] = make_16(planes.fFull, kR16G16B16A16_unorm_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 515 | [] (uint16_t* dstPixel, const float* srcPixel) { |
| 516 | dstPixel[0] = flt_2_uint16(srcPixel[1]); // U |
| 517 | dstPixel[1] = flt_2_uint16(srcPixel[0]); // Y |
| 518 | dstPixel[2] = flt_2_uint16(srcPixel[2]); // V |
| 519 | dstPixel[3] = flt_2_uint16(srcPixel[3]); // A |
| 520 | }); |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 521 | break; |
| 522 | } |
Jim Van Verth | 976a6b0 | 2018-10-17 15:27:19 -0400 | [diff] [blame] | 523 | case kAYUV_YUVFormat: { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 524 | SkBitmap yuvaFull; |
| 525 | |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 526 | yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(), |
| 527 | kRGBA_8888_SkColorType, kUnpremul_SkAlphaType)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 528 | |
| 529 | for (int y = 0; y < planes.fYFull.height(); ++y) { |
| 530 | for (int x = 0; x < planes.fYFull.width(); ++x) { |
| 531 | |
| 532 | uint8_t Y = *planes.fYFull.getAddr8(x, y); |
| 533 | uint8_t U = *planes.fUFull.getAddr8(x, y); |
| 534 | uint8_t V = *planes.fVFull.getAddr8(x, y); |
| 535 | uint8_t A = *planes.fAFull.getAddr8(x, y); |
| 536 | |
| 537 | // NOT premul! |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 538 | // V and Y swapped to match RGBA layout |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 539 | SkColor c = SkColorSetARGB(A, V, U, Y); |
| 540 | *yuvaFull.getAddr32(x, y) = c; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | resultBMs[nextLayer++] = yuvaFull; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 545 | break; |
| 546 | } |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 547 | case kY410_YUVFormat: { |
| 548 | SkBitmap yuvaFull; |
Robert Phillips | f0313ee | 2019-05-21 13:51:11 -0400 | [diff] [blame] | 549 | uint32_t Y, U, V; |
| 550 | uint8_t A; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 551 | |
| 552 | yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(), |
| 553 | kRGBA_1010102_SkColorType, |
| 554 | kUnpremul_SkAlphaType)); |
| 555 | |
| 556 | for (int y = 0; y < planes.fYFull.height(); ++y) { |
| 557 | for (int x = 0; x < planes.fYFull.width(); ++x) { |
| 558 | |
Robert Phillips | f0313ee | 2019-05-21 13:51:11 -0400 | [diff] [blame] | 559 | Y = SkScalarRoundToInt((*planes.fYFull.getAddr8(x, y) / 255.0f) * 1023.0f); |
| 560 | U = SkScalarRoundToInt((*planes.fUFull.getAddr8(x, y) / 255.0f) * 1023.0f); |
| 561 | V = SkScalarRoundToInt((*planes.fVFull.getAddr8(x, y) / 255.0f) * 1023.0f); |
| 562 | A = SkScalarRoundToInt((*planes.fAFull.getAddr8(x, y) / 255.0f) * 3.0f); |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 563 | |
| 564 | // NOT premul! |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 565 | *yuvaFull.getAddr32(x, y) = (A << 30) | (V << 20) | (Y << 10) | (U << 0); |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | resultBMs[nextLayer++] = yuvaFull; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 570 | break; |
| 571 | } |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 572 | case kP016_YUVFormat: // fall through |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 573 | case kP010_YUVFormat: { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 574 | resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 575 | [tenBitsPP = (yuvFormat == kP010_YUVFormat)] |
| 576 | (uint16_t* dstPixel, const float* srcPixel) { |
| 577 | uint16_t val16 = flt_2_uint16(srcPixel[0]); |
| 578 | dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0) |
| 579 | : val16; |
| 580 | }); |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 581 | resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_unorm_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 582 | [tenBitsPP = (yuvFormat == kP010_YUVFormat)] |
| 583 | (uint16_t* dstPixel, const float* srcPixel) { |
| 584 | uint16_t u16 = flt_2_uint16(srcPixel[1]); |
| 585 | uint16_t v16 = flt_2_uint16(srcPixel[2]); |
| 586 | dstPixel[0] = tenBitsPP ? (u16 & 0xFFC0) : u16; |
| 587 | dstPixel[1] = tenBitsPP ? (v16 & 0xFFC0) : v16; |
| 588 | }); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 589 | if (!opaque) { |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 590 | resultBMs[nextLayer++] = make_16(planes.fFull, kA16_unorm_SkColorType, |
| 591 | [tenBitsPP = (yuvFormat == kP010_YUVFormat)] |
| 592 | (uint16_t* dstPixel, const float* srcPixel) { |
| 593 | uint16_t val16 = flt_2_uint16(srcPixel[3]); |
| 594 | dstPixel[0] = tenBitsPP ? (val16 & 0xFFC0) |
| 595 | : val16; |
| 596 | }); |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 597 | } |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 598 | return nextLayer; |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 599 | } |
| 600 | case kP016F_YUVFormat: { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 601 | resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 602 | [] (uint16_t* dstPixel, const float* srcPixel) { |
| 603 | dstPixel[0] = SkFloatToHalf(srcPixel[0]); |
| 604 | }); |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 605 | resultBMs[nextLayer++] = make_16(planes.fQuarter, kR16G16_float_SkColorType, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 606 | [] (uint16_t* dstPixel, const float* srcPixel) { |
| 607 | dstPixel[0] = SkFloatToHalf(srcPixel[1]); |
| 608 | dstPixel[1] = SkFloatToHalf(srcPixel[2]); |
| 609 | }); |
| 610 | if (!opaque) { |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 611 | resultBMs[nextLayer++] = make_16(planes.fFull, kA16_float_SkColorType, |
| 612 | [] (uint16_t* dstPixel, const float* srcPixel) { |
| 613 | dstPixel[0] = SkFloatToHalf(srcPixel[3]); |
| 614 | }); |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 615 | } |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 616 | return nextLayer; |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 617 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 618 | case kNV12_YUVFormat: { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 619 | SkBitmap uvQuarter = make_quarter_2_channel(planes.fYFull, |
| 620 | planes.fUQuarter, |
| 621 | planes.fVQuarter, true); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 622 | resultBMs[nextLayer++] = planes.fYFull; |
| 623 | resultBMs[nextLayer++] = uvQuarter; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 624 | break; |
| 625 | } |
| 626 | case kNV21_YUVFormat: { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 627 | SkBitmap vuQuarter = make_quarter_2_channel(planes.fYFull, |
| 628 | planes.fUQuarter, |
| 629 | planes.fVQuarter, false); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 630 | resultBMs[nextLayer++] = planes.fYFull; |
| 631 | resultBMs[nextLayer++] = vuQuarter; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 632 | break; |
| 633 | } |
| 634 | case kI420_YUVFormat: |
| 635 | resultBMs[nextLayer++] = planes.fYFull; |
| 636 | resultBMs[nextLayer++] = planes.fUQuarter; |
| 637 | resultBMs[nextLayer++] = planes.fVQuarter; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 638 | break; |
| 639 | case kYV12_YUVFormat: |
| 640 | resultBMs[nextLayer++] = planes.fYFull; |
| 641 | resultBMs[nextLayer++] = planes.fVQuarter; |
| 642 | resultBMs[nextLayer++] = planes.fUQuarter; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 643 | break; |
| 644 | } |
| 645 | |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 646 | if (!opaque && !has_alpha_channel(yuvFormat)) { |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 647 | resultBMs[nextLayer++] = planes.fAFull; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 648 | } |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 649 | return nextLayer; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 650 | } |
| 651 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 652 | static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) { |
Brian Osman | b107a11 | 2020-07-23 14:22:22 -0400 | [diff] [blame] | 653 | static const char* kYUVColorSpaceNames[] = {"JPEG", "601", "709F", "709L", |
| 654 | "2020_8F", "2020_8L", "2020_10F", "2020_10L", |
| 655 | "2020_12F", "2020_12L", "Identity"}; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 656 | static_assert(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace + 1); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 657 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 658 | SkPaint paint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 659 | SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 660 | font.setEdging(SkFont::Edging::kAlias); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 661 | |
| 662 | SkRect textRect; |
| 663 | SkString colLabel; |
| 664 | |
| 665 | colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 666 | font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 667 | int y = textRect.height(); |
| 668 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 669 | 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] | 670 | |
| 671 | colLabel.printf("%s", opaque ? "Opaque" : "Transparent"); |
Mike Reed | 71f5a0b | 2018-10-25 16:12:39 -0400 | [diff] [blame] | 672 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 673 | font.measureText(colLabel.c_str(), colLabel.size(), SkTextEncoding::kUTF8, &textRect); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 674 | y += textRect.height(); |
| 675 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 676 | 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] | 677 | } |
| 678 | |
| 679 | static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) { |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 680 | static const char* kYUVFormatNames[] = { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 681 | "P016", "P010", "P016F", "Y416", "AYUV", "Y410", "NV12", "NV21", "I420", "YV12" |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 682 | }; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 683 | static_assert(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat + 1); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 684 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 685 | SkPaint paint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 686 | SkFont font(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()), 16); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 687 | font.setEdging(SkFont::Edging::kAlias); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 688 | |
| 689 | SkRect textRect; |
| 690 | SkString rowLabel; |
| 691 | |
| 692 | rowLabel.printf("%s", kYUVFormatNames[yuvFormat]); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 693 | font.measureText(rowLabel.c_str(), rowLabel.size(), SkTextEncoding::kUTF8, &textRect); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 694 | y += kTileWidthHeight/2 + textRect.height()/2; |
| 695 | |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 696 | canvas->drawString(rowLabel, 0, y, font, paint); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 697 | } |
| 698 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 699 | static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() { |
| 700 | static const float kJPEGConversionMatrix[20] = { |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 701 | 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255, |
| 702 | 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255, |
| 703 | 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255, |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 704 | 0.0f, 0.0f, 0.0f, 1.0f, 0.0f |
| 705 | }; |
| 706 | |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 707 | return SkColorFilters::Matrix(kJPEGConversionMatrix); |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 708 | } |
| 709 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 710 | namespace skiagm { |
| 711 | |
| 712 | // This GM creates an opaque and transparent bitmap, extracts the planes and then recombines |
| 713 | // them into various YUV formats. It then renders the results in the grid: |
| 714 | // |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 715 | // JPEG 601 709 Identity |
| 716 | // Transparent Opaque Transparent Opaque Transparent Opaque Transparent Opaque |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 717 | // originals |
| 718 | // P016 |
| 719 | // P010 |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 720 | // P016F |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 721 | // Y416 |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 722 | // AYUV |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 723 | // Y410 |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 724 | // NV12 |
| 725 | // NV21 |
| 726 | // I420 |
| 727 | // YV12 |
| 728 | class WackyYUVFormatsGM : public GM { |
| 729 | public: |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 730 | using Type = sk_gpu_test::LazyYUVImage::Type; |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 731 | |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 732 | WackyYUVFormatsGM(bool useTargetColorSpace, bool useSubset, Type type) |
| 733 | : fUseTargetColorSpace(useTargetColorSpace), fUseSubset(useSubset), fImageType(type) { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 734 | this->setBGColor(0xFFCCCCCC); |
| 735 | } |
| 736 | |
| 737 | protected: |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 738 | SkString onShortName() override { |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 739 | SkString name("wacky_yuv_formats"); |
| 740 | if (fUseTargetColorSpace) { |
| 741 | name += "_cs"; |
| 742 | } |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 743 | if (fUseSubset) { |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 744 | name += "_domain"; |
| 745 | } |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 746 | switch (fImageType) { |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 747 | case Type::kFromPixmaps: |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 748 | name += "_frompixmaps"; |
| 749 | break; |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 750 | case Type::kFromTextures: |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 751 | break; |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 752 | case Type::kFromGenerator: |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 753 | name += "_imggen"; |
| 754 | break; |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 755 | } |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 756 | |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 757 | return name; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | SkISize onISize() override { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 761 | int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x #-color-spaces |
| 762 | int numRows = 1 + (kLast_YUVFormat + 1); // original + #-yuv-formats |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 763 | int wh = SkScalarCeilToInt(kTileWidthHeight * (fUseSubset ? 1.5f : 1.f)); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 764 | return SkISize::Make(kLabelWidth + numCols * (wh + kPad), |
| 765 | kLabelHeight + numRows * (wh + kPad)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 766 | } |
| 767 | |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 768 | void createBitmaps() { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 769 | SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f }; |
| 770 | float outerRadius = kTileWidthHeight/2.0f - 20.0f; |
| 771 | float innerRadius = 20.0f; |
| 772 | |
| 773 | { |
| 774 | // transparent |
| 775 | SkTDArray<SkRect> circles; |
| 776 | SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles); |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 777 | fOriginalBMs[0] = make_bitmap(kRGBA_8888_SkColorType, path, circles, false, fUseSubset); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | { |
| 781 | // opaque |
| 782 | SkTDArray<SkRect> circles; |
| 783 | SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles); |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 784 | fOriginalBMs[1] = make_bitmap(kRGBA_8888_SkColorType, path, circles, true, fUseSubset); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 785 | } |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 786 | |
| 787 | if (fUseTargetColorSpace) { |
| 788 | fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin(); |
| 789 | } |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 790 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 791 | |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 792 | bool createImages(GrDirectContext* dContext) { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 793 | for (bool opaque : { false, true }) { |
| 794 | for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) { |
| 795 | PlaneData planes; |
| 796 | extract_planes(fOriginalBMs[opaque], (SkYUVColorSpace) cs, &planes); |
| 797 | |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 798 | for (int f = kP016_YUVFormat; f <= kLast_YUVFormat; ++f) { |
| 799 | auto format = static_cast<YUVFormat>(f); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 800 | SkBitmap resultBMs[4]; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 801 | |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 802 | int numPlanes = create_YUV(planes, format, resultBMs, opaque); |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 803 | const YUVAPlanarConfig planarConfig(format, opaque); |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 804 | SkYUVAPixmaps pixmaps = |
| 805 | planarConfig.makeYUVAPixmaps(fOriginalBMs[opaque].dimensions(), |
| 806 | static_cast<SkYUVColorSpace>(cs), |
| 807 | resultBMs, |
| 808 | numPlanes); |
| 809 | auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(std::move(pixmaps)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 810 | |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 811 | fImages[opaque][cs][format] = lazyYUV->refImage(dContext, fImageType); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | } |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 815 | |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 816 | if (dContext) { |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 817 | // Some backends (e.g., Vulkan) require all work be completed for backend textures |
| 818 | // before they are deleted. Since we don't know when we'll next have access to a |
| 819 | // direct context, flush all the work now. |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 820 | dContext->flush(); |
| 821 | dContext->submit(true); |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | return true; |
| 825 | } |
| 826 | |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 827 | DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override { |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 828 | this->createBitmaps(); |
| 829 | |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 830 | if (dContext && dContext->abandoned()) { |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 831 | // This isn't a GpuGM so a null 'context' is okay but an abandoned context |
| 832 | // if forbidden. |
| 833 | return DrawResult::kSkip; |
| 834 | } |
| 835 | |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 836 | // Only the generator is expected to work with the CPU backend. |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 837 | if (fImageType != Type::kFromGenerator && !dContext) { |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 838 | return DrawResult::kSkip; |
| 839 | } |
| 840 | |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 841 | if (!this->createImages(dContext)) { |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 842 | *errorMsg = "Failed to create YUV images"; |
| 843 | return DrawResult::kFail; |
| 844 | } |
| 845 | |
| 846 | return DrawResult::kOk; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 847 | } |
| 848 | |
Robert Phillips | b795bea | 2020-06-25 12:38:53 -0400 | [diff] [blame] | 849 | void onGpuTeardown() override { |
| 850 | for (int i = 0; i < 2; ++i) { |
| 851 | for (int j = 0; j <= kLastEnum_SkYUVColorSpace; ++j) { |
| 852 | for (int k = 0; k <= kLast_YUVFormat; ++k) { |
| 853 | fImages[i][j][k] = nullptr; |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 859 | void onDraw(SkCanvas* canvas) override { |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 860 | auto direct = GrAsDirectContext(canvas->recordingContext()); |
| 861 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 862 | float cellWidth = kTileWidthHeight, cellHeight = kTileWidthHeight; |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 863 | if (fUseSubset) { |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 864 | cellWidth *= 1.5f; |
| 865 | cellHeight *= 1.5f; |
| 866 | } |
| 867 | |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 868 | SkRect srcRect = SkRect::Make(fOriginalBMs[0].dimensions()); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 869 | SkRect dstRect = SkRect::MakeXYWH(kLabelWidth, 0.f, srcRect.width(), srcRect.height()); |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 870 | |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 871 | SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint; |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 872 | if (fUseSubset) { |
| 873 | srcRect.inset(kSubsetPadding, kSubsetPadding); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 874 | // Draw a larger rectangle to ensure bilerp filtering would normally read outside the |
| 875 | // srcRect and hit the red pixels, if strict constraint weren't used. |
| 876 | dstRect.fRight = kLabelWidth + 1.5f * srcRect.width(); |
| 877 | dstRect.fBottom = 1.5f * srcRect.height(); |
| 878 | constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 879 | } |
| 880 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 881 | for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) { |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 882 | SkPaint paint; |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 883 | paint.setFilterQuality(kLow_SkFilterQuality); |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 884 | if (kIdentity_SkYUVColorSpace == cs) { |
| 885 | // The identity color space needs post processing to appear correctly |
| 886 | paint.setColorFilter(yuv_to_rgb_colorfilter()); |
| 887 | } |
| 888 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 889 | for (int opaque : { 0, 1 }) { |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 890 | dstRect.offsetTo(dstRect.fLeft, kLabelHeight); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 891 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 892 | draw_col_label(canvas, dstRect.fLeft + cellWidth / 2, cs, opaque); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 893 | |
Brian Salomon | 68b245c | 2020-10-13 12:34:31 -0400 | [diff] [blame] | 894 | canvas->drawBitmapRect(fOriginalBMs[opaque], srcRect, dstRect, nullptr, constraint); |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 895 | dstRect.offset(0.f, cellHeight + kPad); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 896 | |
Robert Phillips | bb74990 | 2019-06-10 17:20:12 -0400 | [diff] [blame] | 897 | for (int format = kP016_YUVFormat; format <= kLast_YUVFormat; ++format) { |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 898 | draw_row_label(canvas, dstRect.fTop, format); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 899 | if (fUseTargetColorSpace && fImages[opaque][cs][format]) { |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 900 | // Making a CS-specific version of a kIdentity_SkYUVColorSpace YUV image |
| 901 | // doesn't make a whole lot of sense. The colorSpace conversion will |
| 902 | // operate on the YUV components rather than the RGB components. |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 903 | sk_sp<SkImage> csImage = |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 904 | fImages[opaque][cs][format]->makeColorSpace(fTargetColorSpace, direct); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 905 | canvas->drawImageRect(csImage, srcRect, dstRect, &paint, constraint); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 906 | } else { |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 907 | canvas->drawImageRect(fImages[opaque][cs][format], srcRect, dstRect, |
| 908 | &paint, constraint); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 909 | } |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 910 | dstRect.offset(0.f, cellHeight + kPad); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 911 | } |
| 912 | |
Robert Phillips | 99044e1 | 2020-01-29 08:37:01 -0500 | [diff] [blame] | 913 | dstRect.offset(cellWidth + kPad, 0.f); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | private: |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 919 | SkBitmap fOriginalBMs[2]; |
| 920 | sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace + 1][kLast_YUVFormat + 1]; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 921 | bool fUseTargetColorSpace; |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 922 | bool fUseSubset; |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 923 | Type fImageType; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 924 | sk_sp<SkColorSpace> fTargetColorSpace; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 925 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 926 | using INHERITED = GM; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 927 | }; |
| 928 | |
| 929 | ////////////////////////////////////////////////////////////////////////////// |
| 930 | |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 931 | DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false, |
| 932 | /* subset */ false, |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 933 | WackyYUVFormatsGM::Type::kFromTextures);) |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 934 | DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false, |
| 935 | /* subset */ true, |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 936 | WackyYUVFormatsGM::Type::kFromTextures);) |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 937 | DEF_GM(return new WackyYUVFormatsGM(/* target cs */ true, |
| 938 | /* subset */ false, |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 939 | WackyYUVFormatsGM::Type::kFromTextures);) |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 940 | DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false, |
| 941 | /* subset */ false, |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 942 | WackyYUVFormatsGM::Type::kFromGenerator);) |
Brian Salomon | 6c6678f | 2020-09-21 10:50:30 -0400 | [diff] [blame] | 943 | DEF_GM(return new WackyYUVFormatsGM(/* target cs */ false, |
| 944 | /* subset */ false, |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 945 | WackyYUVFormatsGM::Type::kFromPixmaps);) |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 946 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 947 | class YUVMakeColorSpaceGM : public GpuGM { |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 948 | public: |
| 949 | YUVMakeColorSpaceGM() { |
| 950 | this->setBGColor(0xFFCCCCCC); |
| 951 | } |
| 952 | |
| 953 | protected: |
| 954 | SkString onShortName() override { |
| 955 | return SkString("yuv_make_color_space"); |
| 956 | } |
| 957 | |
| 958 | SkISize onISize() override { |
| 959 | int numCols = 4; // (transparent, opaque) x (untagged, tagged) |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 960 | int numRows = 5; // original, YUV, subset, makeNonTextureImage, readPixels |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 961 | return SkISize::Make(numCols * (kTileWidthHeight + kPad) + kPad, |
| 962 | numRows * (kTileWidthHeight + kPad) + kPad); |
| 963 | } |
| 964 | |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 965 | void createBitmaps() { |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 966 | SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f }; |
| 967 | float outerRadius = kTileWidthHeight/2.0f - 20.0f; |
| 968 | float innerRadius = 20.0f; |
| 969 | |
| 970 | { |
| 971 | // transparent |
| 972 | SkTDArray<SkRect> circles; |
| 973 | SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 974 | fOriginalBMs[0] = make_bitmap(kN32_SkColorType, path, circles, false, false); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | { |
| 978 | // opaque |
| 979 | SkTDArray<SkRect> circles; |
| 980 | SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 981 | fOriginalBMs[1] = make_bitmap(kN32_SkColorType, path, circles, true, false); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | fTargetColorSpace = SkColorSpace::MakeSRGB()->makeColorSpin(); |
| 985 | } |
| 986 | |
Robert Phillips | b87b39b | 2020-07-01 14:45:24 -0400 | [diff] [blame] | 987 | bool createImages(GrDirectContext* context) { |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 988 | for (bool opaque : { false, true }) { |
| 989 | PlaneData planes; |
| 990 | extract_planes(fOriginalBMs[opaque], kJPEG_SkYUVColorSpace, &planes); |
| 991 | |
| 992 | SkBitmap resultBMs[4]; |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 993 | |
Brian Salomon | f7255d7 | 2020-04-08 15:57:08 -0400 | [diff] [blame] | 994 | create_YUV(planes, kAYUV_YUVFormat, resultBMs, opaque); |
Robert Phillips | 2dd1b47 | 2019-03-21 09:00:20 -0400 | [diff] [blame] | 995 | |
Brian Salomon | 94f65d7 | 2020-09-18 14:09:51 -0400 | [diff] [blame] | 996 | YUVAPlanarConfig planarConfig(kAYUV_YUVFormat, opaque); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 997 | |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 998 | auto yuvaPixmaps = planarConfig.makeYUVAPixmaps(fOriginalBMs[opaque].dimensions(), |
| 999 | kJPEG_Full_SkYUVColorSpace, |
| 1000 | resultBMs, |
| 1001 | SK_ARRAY_COUNT(resultBMs)); |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 1002 | |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 1003 | int i = 0; |
| 1004 | for (sk_sp<SkColorSpace> cs : {sk_sp<SkColorSpace>(nullptr), |
| 1005 | SkColorSpace::MakeSRGB()}) { |
| 1006 | auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(yuvaPixmaps, |
| 1007 | GrMipmapped::kNo, |
| 1008 | std::move(cs)); |
| 1009 | fImages[opaque][i++] = |
| 1010 | lazyYUV->refImage(context, sk_gpu_test::LazyYUVImage::Type::kFromTextures); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1011 | } |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1012 | } |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 1013 | |
| 1014 | // Some backends (e.g., Vulkan) require all work be completed for backend textures before |
| 1015 | // they are deleted. Since we don't know when we'll next have access to a direct context, |
| 1016 | // flush all the work now. |
Greg Daniel | ce9f016 | 2020-06-30 13:42:46 -0400 | [diff] [blame] | 1017 | context->flush(); |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 1018 | context->submit(true); |
| 1019 | |
| 1020 | return true; |
| 1021 | } |
| 1022 | |
Robert Phillips | b87b39b | 2020-07-01 14:45:24 -0400 | [diff] [blame] | 1023 | DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override { |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 1024 | if (!context || context->abandoned()) { |
| 1025 | return DrawResult::kSkip; |
| 1026 | } |
| 1027 | |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 1028 | this->createBitmaps(); |
| 1029 | if (!this->createImages(context)) { |
| 1030 | *errorMsg = "Failed to create YUV images"; |
| 1031 | return DrawResult::kFail; |
| 1032 | } |
| 1033 | |
| 1034 | return DrawResult::kOk; |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1035 | } |
| 1036 | |
Robert Phillips | b795bea | 2020-06-25 12:38:53 -0400 | [diff] [blame] | 1037 | void onGpuTeardown() override { |
| 1038 | fImages[0][0] = fImages[0][1] = fImages[1][0] = fImages[1][1] = nullptr; |
| 1039 | } |
| 1040 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 1041 | DrawResult onDraw(GrRecordingContext* rContext, GrRenderTargetContext*, |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 1042 | SkCanvas* canvas, SkString* msg) override { |
Robert Phillips | e22c5ca | 2020-06-19 12:29:57 -0400 | [diff] [blame] | 1043 | 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] | 1044 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 1045 | auto dContext = GrAsDirectContext(rContext); |
| 1046 | if (rContext && !dContext) { |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 1047 | *msg = "YUV ColorSpace image creation requires a direct context."; |
| 1048 | return DrawResult::kSkip; |
| 1049 | } |
| 1050 | |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1051 | int x = kPad; |
| 1052 | for (int tagged : { 0, 1 }) { |
| 1053 | for (int opaque : { 0, 1 }) { |
| 1054 | int y = kPad; |
| 1055 | |
| 1056 | auto raster = SkImage::MakeFromBitmap(fOriginalBMs[opaque]) |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 1057 | ->makeColorSpace(fTargetColorSpace, nullptr); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1058 | canvas->drawImage(raster, x, y); |
| 1059 | y += kTileWidthHeight + kPad; |
| 1060 | |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 1061 | if (fImages[opaque][tagged]) { |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 1062 | auto yuv = fImages[opaque][tagged]->makeColorSpace(fTargetColorSpace, dContext); |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 1063 | SkASSERT(yuv); |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 1064 | SkASSERT(SkColorSpace::Equals(yuv->colorSpace(), fTargetColorSpace.get())); |
| 1065 | canvas->drawImage(yuv, x, y); |
| 1066 | y += kTileWidthHeight + kPad; |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1067 | |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 1068 | SkIRect bounds = SkIRect::MakeWH(kTileWidthHeight / 2, kTileWidthHeight / 2); |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 1069 | auto subset = yuv->makeSubset(bounds, dContext); |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 1070 | SkASSERT(subset); |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 1071 | canvas->drawImage(subset, x, y); |
| 1072 | y += kTileWidthHeight + kPad; |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1073 | |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 1074 | auto nonTexture = yuv->makeNonTextureImage(); |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 1075 | SkASSERT(nonTexture); |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 1076 | canvas->drawImage(nonTexture, x, y); |
| 1077 | y += kTileWidthHeight + kPad; |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1078 | |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 1079 | SkBitmap readBack; |
| 1080 | readBack.allocPixels(yuv->imageInfo()); |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 1081 | SkAssertResult(yuv->readPixels(dContext, readBack.pixmap(), 0, 0)); |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 1082 | canvas->drawBitmap(readBack, x, y); |
| 1083 | } |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1084 | x += kTileWidthHeight + kPad; |
| 1085 | } |
| 1086 | } |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 1087 | return DrawResult::kOk; |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | private: |
| 1091 | SkBitmap fOriginalBMs[2]; |
| 1092 | sk_sp<SkImage> fImages[2][2]; |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1093 | sk_sp<SkColorSpace> fTargetColorSpace; |
| 1094 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 1095 | using INHERITED = GM; |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 1096 | }; |
| 1097 | |
| 1098 | DEF_GM(return new YUVMakeColorSpaceGM();) |
| 1099 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1100 | } // namespace skiagm |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1101 | |
| 1102 | /////////////// |
| 1103 | |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1104 | #include "include/effects/SkColorMatrix.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 1105 | #include "src/core/SkAutoPixmapStorage.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 1106 | #include "tools/Resources.h" |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1107 | |
| 1108 | static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) { |
| 1109 | auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes()); |
| 1110 | canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height()); |
| 1111 | SkPaint paint; |
| 1112 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 1113 | paint.setColorFilter(cf); |
| 1114 | paint.setBlendMode(SkBlendMode::kSrc); |
| 1115 | canvas->drawImage(img, 0, 0, &paint); |
| 1116 | } |
| 1117 | |
| 1118 | static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) { |
| 1119 | float m[20]; |
| 1120 | SkColorMatrix_RGB2YUV(cs, m); |
| 1121 | |
| 1122 | memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A |
| 1123 | draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]); |
| 1124 | |
| 1125 | memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A |
| 1126 | draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]); |
| 1127 | |
| 1128 | memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A |
| 1129 | draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]); |
| 1130 | } |
| 1131 | |
| 1132 | static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 1133 | const SkImage* a, const SkImage* b) { |
| 1134 | auto sh = SkShaders::Blend(SkBlendMode::kDifference, a->makeShader(), b->makeShader()); |
| 1135 | SkPaint paint; |
| 1136 | paint.setShader(sh); |
| 1137 | canvas->save(); |
| 1138 | canvas->translate(x, y); |
| 1139 | canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint); |
| 1140 | |
| 1141 | SkColorMatrix cm; |
| 1142 | cm.setScale(64, 64, 64); |
| 1143 | paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm))); |
| 1144 | canvas->translate(0, a->height()); |
| 1145 | canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint); |
| 1146 | |
| 1147 | canvas->restore(); |
| 1148 | } |
| 1149 | |
| 1150 | // Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the |
| 1151 | // resulting (recombined) images (gpu only for now). |
| 1152 | // |
| 1153 | class YUVSplitterGM : public skiagm::GM { |
| 1154 | sk_sp<SkImage> fOrig; |
| 1155 | SkAutoPixmapStorage fStorage[3]; |
| 1156 | SkPixmap fPM[3]; |
| 1157 | |
| 1158 | public: |
| 1159 | YUVSplitterGM() {} |
| 1160 | |
| 1161 | protected: |
| 1162 | |
| 1163 | SkString onShortName() override { |
| 1164 | return SkString("yuv_splitter"); |
| 1165 | } |
| 1166 | |
| 1167 | SkISize onISize() override { |
Brian Osman | 2b73e66 | 2019-11-01 10:02:24 -0400 | [diff] [blame] | 1168 | return SkISize::Make(1280, 768); |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | void onOnceBeforeDraw() override { |
| 1172 | fOrig = GetResourceAsImage("images/mandrill_256.png"); |
| 1173 | |
Brian Salomon | f165f79 | 2020-10-05 13:25:05 -0400 | [diff] [blame] | 1174 | SkImageInfo info = SkImageInfo::MakeA8(fOrig->dimensions()); |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1175 | fStorage[0].alloc(info); |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1176 | fStorage[1].alloc(info); |
| 1177 | fStorage[2].alloc(info); |
| 1178 | for (int i = 0; i < 3; ++i) { |
| 1179 | fPM[i] = fStorage[i]; |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | void onDraw(SkCanvas* canvas) override { |
| 1184 | SkYUVAIndex indices[4]; |
Robert Phillips | e073552 | 2020-01-31 11:03:32 -0500 | [diff] [blame] | 1185 | indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR}; |
| 1186 | indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR}; |
| 1187 | indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR}; |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1188 | indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kR}; |
| 1189 | |
| 1190 | canvas->translate(fOrig->width(), 0); |
| 1191 | canvas->save(); |
Brian Osman | 2b73e66 | 2019-11-01 10:02:24 -0400 | [diff] [blame] | 1192 | for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace, |
| 1193 | kBT2020_SkYUVColorSpace}) { |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1194 | split_into_yuv(fOrig.get(), cs, fPM); |
Brian Salomon | f165f79 | 2020-10-05 13:25:05 -0400 | [diff] [blame] | 1195 | SkYUVAInfo yuvaInfo(fOrig->dimensions(), SkYUVAInfo::PlanarConfig::kY_U_V_444, cs); |
| 1196 | auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, fPM); |
| 1197 | auto img = SkImage::MakeFromYUVAPixmaps(canvas->recordingContext(), |
| 1198 | yuvaPixmaps, |
| 1199 | GrMipMapped::kNo, |
| 1200 | /* limit to max tex size */ false, |
| 1201 | /* color space */ nullptr); |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1202 | if (img) { |
| 1203 | canvas->drawImage(img, 0, 0, nullptr); |
| 1204 | draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get()); |
| 1205 | } |
| 1206 | canvas->translate(fOrig->width(), 0); |
| 1207 | } |
| 1208 | canvas->restore(); |
| 1209 | canvas->translate(-fOrig->width(), 0); |
| 1210 | |
| 1211 | canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0, nullptr); |
| 1212 | canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height(), nullptr); |
| 1213 | canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]), |
| 1214 | 0, fPM[0].height() + fPM[1].height(), nullptr); |
| 1215 | } |
| 1216 | |
| 1217 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 1218 | using INHERITED = GM; |
Mike Reed | 6a5f7e2 | 2019-05-23 15:30:07 -0400 | [diff] [blame] | 1219 | }; |
| 1220 | DEF_GM( return new YUVSplitterGM; ) |