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