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 | |
| 8 | #include "gm.h" |
| 9 | #include "sk_tool_utils.h" |
| 10 | |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkImageGenerator.h" |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 13 | #include "SkPath.h" |
Mike Reed | 71f5a0b | 2018-10-25 16:12:39 -0400 | [diff] [blame] | 14 | #include "SkTextUtils.h" |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 15 | #include "SkYUVAIndex.h" |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 16 | |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
| 18 | #include "GrBackendSurface.h" |
| 19 | #include "GrContextPriv.h" |
| 20 | #include "GrGpu.h" |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 21 | #include "SkImage_GpuYUVA.h" |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 22 | #endif |
| 23 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 24 | static const int kTileWidthHeight = 128; |
| 25 | static const int kLabelWidth = 64; |
| 26 | static const int kLabelHeight = 32; |
| 27 | static const int kPad = 1; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 28 | |
| 29 | enum YUVFormat { |
| 30 | // 4:4:4 formats, 32 bpp |
Jim Van Verth | 976a6b0 | 2018-10-17 15:27:19 -0400 | [diff] [blame] | 31 | kAYUV_YUVFormat, // 8-bit YUVA values all interleaved |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 32 | |
| 33 | // 4:2:0 formats, 12 bpp |
| 34 | kNV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled interleaved U/V planes |
| 35 | kNV21_YUVFormat, // same as kNV12 but w/ U/V reversed in the interleaved plane |
| 36 | |
| 37 | kI420_YUVFormat, // 8-bit Y plane + 2x2 down sampled U and V planes |
| 38 | kYV12_YUVFormat, // 8-bit Y plane + 2x2 down sampled V and U planes |
| 39 | |
| 40 | kLast_YUVFormat = kYV12_YUVFormat |
| 41 | }; |
| 42 | |
| 43 | // All the planes we need to construct the various YUV formats |
| 44 | struct PlaneData { |
| 45 | SkBitmap fYFull; |
| 46 | SkBitmap fUFull; |
| 47 | SkBitmap fVFull; |
| 48 | SkBitmap fAFull; |
| 49 | SkBitmap fUQuarter; // 2x2 downsampled U channel |
| 50 | SkBitmap fVQuarter; // 2x2 downsampled V channel |
| 51 | }; |
| 52 | |
| 53 | // Add a portion of a circle to 'path'. The points 'o1' and 'o2' are on the border of the circle |
| 54 | // and have tangents 'v1' and 'v2'. |
| 55 | static void add_arc(SkPath* path, |
| 56 | const SkPoint& o1, const SkVector& v1, |
| 57 | const SkPoint& o2, const SkVector& v2, |
| 58 | SkTDArray<SkRect>* circles, bool takeLongWayRound) { |
| 59 | |
| 60 | SkVector v3 = { -v1.fY, v1.fX }; |
| 61 | SkVector v4 = { v2.fY, -v2.fX }; |
| 62 | |
| 63 | SkScalar t = ((o2.fX - o1.fX) * v4.fY - (o2.fY - o1.fY) * v4.fX) / v3.cross(v4); |
| 64 | SkPoint center = { o1.fX + t * v3.fX, o1.fY + t * v3.fY }; |
| 65 | |
| 66 | SkRect r = { center.fX - t, center.fY - t, center.fX + t, center.fY + t }; |
| 67 | |
| 68 | if (circles) { |
| 69 | circles->push_back(r); |
| 70 | } |
| 71 | |
| 72 | SkVector startV = o1 - center, endV = o2 - center; |
| 73 | startV.normalize(); |
| 74 | endV.normalize(); |
| 75 | |
| 76 | SkScalar startDeg = SkRadiansToDegrees(SkScalarATan2(startV.fY, startV.fX)); |
| 77 | SkScalar endDeg = SkRadiansToDegrees(SkScalarATan2(endV.fY, endV.fX)); |
| 78 | |
| 79 | startDeg += 360.0f; |
| 80 | startDeg = fmodf(startDeg, 360.0f); |
| 81 | |
| 82 | endDeg += 360.0f; |
| 83 | endDeg = fmodf(endDeg, 360.0f); |
| 84 | |
| 85 | if (endDeg < startDeg) { |
| 86 | endDeg += 360.0f; |
| 87 | } |
| 88 | |
| 89 | SkScalar sweepDeg = SkTAbs(endDeg - startDeg); |
| 90 | if (!takeLongWayRound) { |
| 91 | sweepDeg = sweepDeg - 360; |
| 92 | } |
| 93 | |
| 94 | path->arcTo(r, startDeg, sweepDeg, false); |
| 95 | } |
| 96 | |
| 97 | static SkPath create_splat(const SkPoint& o, SkScalar innerRadius, SkScalar outerRadius, |
| 98 | SkScalar ratio, int numLobes, SkTDArray<SkRect>* circles) { |
| 99 | if (numLobes <= 1) { |
| 100 | return SkPath(); |
| 101 | } |
| 102 | |
| 103 | SkPath p; |
| 104 | |
| 105 | int numDivisions = 2 * numLobes; |
| 106 | SkScalar fullLobeDegrees = 360.0f / numLobes; |
| 107 | SkScalar outDegrees = ratio * fullLobeDegrees / (ratio + 1.0f); |
| 108 | SkScalar innerDegrees = fullLobeDegrees / (ratio + 1.0f); |
| 109 | SkMatrix outerStep, innerStep; |
| 110 | outerStep.setRotate(outDegrees); |
| 111 | innerStep.setRotate(innerDegrees); |
| 112 | SkVector curV = SkVector::Make(0.0f, 1.0f); |
| 113 | |
| 114 | if (circles) { |
| 115 | circles->push_back(SkRect::MakeLTRB(o.fX - innerRadius, o.fY - innerRadius, |
| 116 | o.fX + innerRadius, o.fY + innerRadius)); |
| 117 | } |
| 118 | |
| 119 | p.moveTo(o.fX + innerRadius * curV.fX, o.fY + innerRadius * curV.fY); |
| 120 | |
| 121 | for (int i = 0; i < numDivisions; ++i) { |
| 122 | |
| 123 | SkVector nextV; |
| 124 | if (0 == (i % 2)) { |
| 125 | nextV = outerStep.mapVector(curV.fX, curV.fY); |
| 126 | |
| 127 | SkPoint top = SkPoint::Make(o.fX + outerRadius * curV.fX, |
| 128 | o.fY + outerRadius * curV.fY); |
| 129 | SkPoint nextTop = SkPoint::Make(o.fX + outerRadius * nextV.fX, |
| 130 | o.fY + outerRadius * nextV.fY); |
| 131 | |
| 132 | p.lineTo(top); |
| 133 | add_arc(&p, top, curV, nextTop, nextV, circles, true); |
| 134 | } else { |
| 135 | nextV = innerStep.mapVector(curV.fX, curV.fY); |
| 136 | |
| 137 | SkPoint bot = SkPoint::Make(o.fX + innerRadius * curV.fX, |
| 138 | o.fY + innerRadius * curV.fY); |
| 139 | SkPoint nextBot = SkPoint::Make(o.fX + innerRadius * nextV.fX, |
| 140 | o.fY + innerRadius * nextV.fY); |
| 141 | |
| 142 | p.lineTo(bot); |
| 143 | add_arc(&p, bot, curV, nextBot, nextV, nullptr, false); |
| 144 | } |
| 145 | |
| 146 | curV = nextV; |
| 147 | } |
| 148 | |
| 149 | p.close(); |
| 150 | |
| 151 | return p; |
| 152 | } |
| 153 | |
| 154 | static SkBitmap make_bitmap(const SkPath& path, const SkTDArray<SkRect>& circles, bool opaque) { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 155 | const SkColor kGreen = sk_tool_utils::color_to_565(SkColorSetARGB(0xFF, 178, 240, 104)); |
| 156 | const SkColor kBlue = sk_tool_utils::color_to_565(SkColorSetARGB(0xFF, 173, 167, 252)); |
| 157 | const SkColor kYellow = sk_tool_utils::color_to_565(SkColorSetARGB(0xFF, 255, 221, 117)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 158 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 159 | SkImageInfo ii = SkImageInfo::MakeN32(kTileWidthHeight, kTileWidthHeight, kPremul_SkAlphaType); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 160 | |
| 161 | SkBitmap bm; |
| 162 | bm.allocPixels(ii); |
| 163 | |
| 164 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirectN32(ii.width(), ii.height(), |
| 165 | (SkPMColor*)bm.getPixels(), |
| 166 | bm.rowBytes()); |
| 167 | |
| 168 | canvas->clear(opaque ? kGreen : SK_ColorTRANSPARENT); |
| 169 | |
| 170 | SkPaint paint; |
| 171 | paint.setAntiAlias(false); // serialize-8888 doesn't seem to work well w/ partial transparency |
| 172 | paint.setColor(kBlue); |
| 173 | |
| 174 | canvas->drawPath(path, paint); |
| 175 | |
| 176 | paint.setColor(opaque ? kYellow : SK_ColorTRANSPARENT); |
| 177 | paint.setBlendMode(SkBlendMode::kSrc); |
| 178 | for (int i = 0; i < circles.count(); ++i) { |
| 179 | SkRect r = circles[i]; |
| 180 | r.inset(r.width()/4, r.height()/4); |
| 181 | canvas->drawOval(r, paint); |
| 182 | } |
| 183 | |
| 184 | return bm; |
| 185 | } |
| 186 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 187 | static void convert_rgba_to_yuva_601_shared(SkColor col, uint8_t yuv[4], |
| 188 | uint8_t off, uint8_t range) { |
| 189 | static const float Kr = 0.299f; |
| 190 | static const float Kb = 0.114f; |
| 191 | static const float Kg = 1.0f - Kr - Kb; |
| 192 | |
| 193 | float r = SkColorGetR(col) / 255.0f; |
| 194 | float g = SkColorGetG(col) / 255.0f; |
| 195 | float b = SkColorGetB(col) / 255.0f; |
| 196 | |
| 197 | float Ey = Kr * r + Kg * g + Kb * b; |
| 198 | float Ecb = (b - Ey) / 1.402f; |
| 199 | float Ecr = (r - Ey) / 1.772; |
| 200 | |
| 201 | yuv[0] = SkScalarRoundToInt( range * Ey + off ); |
| 202 | yuv[1] = SkScalarRoundToInt( 224 * Ecb + 128 ); |
| 203 | yuv[2] = SkScalarRoundToInt( 224 * Ecr + 128 ); |
| 204 | yuv[3] = SkColorGetA(col); |
| 205 | } |
| 206 | |
| 207 | static void convert_rgba_to_yuva_jpeg(SkColor col, uint8_t yuv[4]) { |
| 208 | // full swing from 0..255 |
| 209 | convert_rgba_to_yuva_601_shared(col, yuv, 0, 255); |
| 210 | } |
| 211 | |
| 212 | static void convert_rgba_to_yuva_601(SkColor col, uint8_t yuv[4]) { |
| 213 | // partial swing from 16..235 |
| 214 | convert_rgba_to_yuva_601_shared(col, yuv, 16, 219); |
| 215 | |
| 216 | } |
| 217 | |
| 218 | static void convert_rgba_to_yuva_709(SkColor col, uint8_t yuv[4]) { |
| 219 | static const float Kr = 0.2126f; |
| 220 | static const float Kb = 0.0722f; |
| 221 | static const float Kg = 1.0f - Kr - Kb; |
| 222 | |
| 223 | float r = SkColorGetR(col) / 255.0f; |
| 224 | float g = SkColorGetG(col) / 255.0f; |
| 225 | float b = SkColorGetB(col) / 255.0f; |
| 226 | |
| 227 | float Ey = Kr * r + Kg * g + Kb * b; |
| 228 | float Ecb = (b - Ey) / 1.8556f; |
| 229 | float Ecr = (r - Ey) / 1.5748; |
| 230 | |
| 231 | yuv[0] = SkScalarRoundToInt( 219 * Ey + 16 ); |
| 232 | yuv[1] = SkScalarRoundToInt( 224 * Ecb + 128 ); |
| 233 | yuv[2] = SkScalarRoundToInt( 224 * Ecr + 128 ); |
| 234 | |
| 235 | yuv[3] = SkColorGetA(col); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | static SkPMColor convert_yuva_to_rgba_jpeg(uint8_t y, uint8_t u, uint8_t v, uint8_t a) { |
| 240 | int c = y; |
| 241 | int d = u - 128; |
| 242 | int e = v - 128; |
| 243 | |
| 244 | uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.0f * c + 1.402f * e ), |
| 245 | 0, 255); |
| 246 | uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.0f * c - (0.344136f * d) - (0.714136f * e)), |
| 247 | 0, 255); |
| 248 | uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.0f * c + 1.773f * d ), |
| 249 | 0, 255); |
| 250 | |
| 251 | return SkPremultiplyARGBInline(a, r, g, b); |
| 252 | } |
| 253 | |
| 254 | static SkPMColor convert_yuva_to_rgba_601(uint8_t y, uint8_t u, uint8_t v, uint8_t a) { |
| 255 | int c = y - 16; |
| 256 | int d = u - 128; |
| 257 | int e = v - 128; |
| 258 | |
| 259 | uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.164f * c + 1.596f * e ), 0, 255); |
| 260 | uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.164f * c - (0.391f * d) - (0.813f * e)), 0, 255); |
| 261 | uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.164f * c + 2.018f * d ), 0, 255); |
| 262 | |
| 263 | return SkPremultiplyARGBInline(a, r, g, b); |
| 264 | } |
| 265 | |
| 266 | static SkPMColor convert_yuva_to_rgba_709(uint8_t y, uint8_t u, uint8_t v, uint8_t a) { |
| 267 | int c = y - 16; |
| 268 | int d = u - 128; |
| 269 | int e = v - 128; |
| 270 | |
| 271 | uint8_t r = SkScalarPin(SkScalarRoundToInt( 1.164f * c + 1.793f * e ), 0, 255); |
| 272 | uint8_t g = SkScalarPin(SkScalarRoundToInt( 1.164f * c - (0.213f * d) - (0.533f * e)), 0, 255); |
| 273 | uint8_t b = SkScalarPin(SkScalarRoundToInt( 1.164f * c + 2.112f * d ), 0, 255); |
| 274 | |
| 275 | return SkPremultiplyARGBInline(a, r, g, b); |
| 276 | } |
| 277 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 278 | static void extract_planes(const SkBitmap& bm, SkYUVColorSpace yuvColorSpace, PlaneData* planes) { |
| 279 | SkASSERT(!(bm.width() % 2)); |
| 280 | SkASSERT(!(bm.height() % 2)); |
| 281 | |
| 282 | planes->fYFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height())); |
| 283 | planes->fUFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height())); |
| 284 | planes->fVFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height())); |
| 285 | planes->fAFull.allocPixels(SkImageInfo::MakeA8(bm.width(), bm.height())); |
| 286 | planes->fUQuarter.allocPixels(SkImageInfo::MakeA8(bm.width()/2, bm.height()/2)); |
| 287 | planes->fVQuarter.allocPixels(SkImageInfo::MakeA8(bm.width()/2, bm.height()/2)); |
| 288 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 289 | for (int y = 0; y < bm.height(); ++y) { |
| 290 | for (int x = 0; x < bm.width(); ++x) { |
| 291 | SkColor col = bm.getColor(x, y); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 292 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 293 | uint8_t yuva[4]; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 294 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 295 | if (kJPEG_SkYUVColorSpace == yuvColorSpace) { |
| 296 | convert_rgba_to_yuva_jpeg(col, yuva); |
| 297 | } else if (kRec601_SkYUVColorSpace == yuvColorSpace) { |
| 298 | convert_rgba_to_yuva_601(col, yuva); |
| 299 | } else { |
| 300 | SkASSERT(kRec709_SkYUVColorSpace == yuvColorSpace); |
| 301 | convert_rgba_to_yuva_709(col, yuva); |
| 302 | } |
| 303 | |
| 304 | *planes->fYFull.getAddr8(x, y) = yuva[0]; |
| 305 | *planes->fUFull.getAddr8(x, y) = yuva[1]; |
| 306 | *planes->fVFull.getAddr8(x, y) = yuva[2]; |
| 307 | *planes->fAFull.getAddr8(x, y) = yuva[3]; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | for (int y = 0; y < bm.height()/2; ++y) { |
| 312 | for (int x = 0; x < bm.width()/2; ++x) { |
| 313 | uint32_t uAccum = 0, vAccum = 0; |
| 314 | |
| 315 | uAccum += *planes->fUFull.getAddr8(2*x, 2*y); |
| 316 | uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y); |
| 317 | uAccum += *planes->fUFull.getAddr8(2*x, 2*y+1); |
| 318 | uAccum += *planes->fUFull.getAddr8(2*x+1, 2*y+1); |
| 319 | |
| 320 | *planes->fUQuarter.getAddr8(x, y) = uAccum / 4.0f; |
| 321 | |
| 322 | vAccum += *planes->fVFull.getAddr8(2*x, 2*y); |
| 323 | vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y); |
| 324 | vAccum += *planes->fVFull.getAddr8(2*x, 2*y+1); |
| 325 | vAccum += *planes->fVFull.getAddr8(2*x+1, 2*y+1); |
| 326 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 327 | *planes->fVQuarter.getAddr8(x, y) = vAccum / 4.0f; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // Recombine the separate planes into some YUV format |
| 333 | static void create_YUV(const PlaneData& planes, YUVFormat yuvFormat, |
| 334 | SkBitmap resultBMs[], SkYUVAIndex yuvaIndices[4], bool opaque) { |
| 335 | int nextLayer = 0; |
| 336 | |
| 337 | switch (yuvFormat) { |
Jim Van Verth | 976a6b0 | 2018-10-17 15:27:19 -0400 | [diff] [blame] | 338 | case kAYUV_YUVFormat: { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 339 | SkBitmap yuvaFull; |
| 340 | |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 341 | yuvaFull.allocPixels(SkImageInfo::Make(planes.fYFull.width(), planes.fYFull.height(), |
| 342 | kRGBA_8888_SkColorType, kUnpremul_SkAlphaType)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 343 | |
| 344 | for (int y = 0; y < planes.fYFull.height(); ++y) { |
| 345 | for (int x = 0; x < planes.fYFull.width(); ++x) { |
| 346 | |
| 347 | uint8_t Y = *planes.fYFull.getAddr8(x, y); |
| 348 | uint8_t U = *planes.fUFull.getAddr8(x, y); |
| 349 | uint8_t V = *planes.fVFull.getAddr8(x, y); |
| 350 | uint8_t A = *planes.fAFull.getAddr8(x, y); |
| 351 | |
| 352 | // NOT premul! |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 353 | // V and Y swapped to match RGBA layout |
| 354 | *yuvaFull.getAddr32(x, y) = SkColorSetARGB(A, V, U, Y); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | resultBMs[nextLayer++] = yuvaFull; |
| 359 | |
| 360 | yuvaIndices[0].fIndex = 0; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 361 | yuvaIndices[0].fChannel = SkColorChannel::kR; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 362 | yuvaIndices[1].fIndex = 0; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 363 | yuvaIndices[1].fChannel = SkColorChannel::kG; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 364 | yuvaIndices[2].fIndex = 0; |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 365 | yuvaIndices[2].fChannel = SkColorChannel::kB; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 366 | yuvaIndices[3].fIndex = 0; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 367 | yuvaIndices[3].fChannel = SkColorChannel::kA; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | case kNV12_YUVFormat: { |
| 371 | SkBitmap uvQuarter; |
| 372 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 373 | // There isn't a RG color type. Approx w/ RGBA. |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 374 | uvQuarter.allocPixels(SkImageInfo::Make(planes.fYFull.width()/2, |
| 375 | planes.fYFull.height()/2, |
| 376 | kRGBA_8888_SkColorType, |
| 377 | kUnpremul_SkAlphaType)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 378 | |
| 379 | for (int y = 0; y < planes.fYFull.height()/2; ++y) { |
| 380 | for (int x = 0; x < planes.fYFull.width()/2; ++x) { |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 381 | uint8_t U = *planes.fUQuarter.getAddr8(x, y); |
| 382 | uint8_t V = *planes.fVQuarter.getAddr8(x, y); |
| 383 | |
| 384 | // NOT premul! |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 385 | // U and 0 swapped to match RGBA layout |
| 386 | *uvQuarter.getAddr32(x, y) = SkColorSetARGB(0, 0, V, U); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | resultBMs[nextLayer++] = planes.fYFull; |
| 391 | resultBMs[nextLayer++] = uvQuarter; |
| 392 | |
| 393 | yuvaIndices[0].fIndex = 0; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 394 | yuvaIndices[0].fChannel = SkColorChannel::kA; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 395 | yuvaIndices[1].fIndex = 1; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 396 | yuvaIndices[1].fChannel = SkColorChannel::kR; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 397 | yuvaIndices[2].fIndex = 1; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 398 | yuvaIndices[2].fChannel = SkColorChannel::kG; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 399 | break; |
| 400 | } |
| 401 | case kNV21_YUVFormat: { |
| 402 | SkBitmap vuQuarter; |
| 403 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 404 | // There isn't a RG color type. Approx w/ RGBA. |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 405 | vuQuarter.allocPixels(SkImageInfo::Make(planes.fYFull.width()/2, |
| 406 | planes.fYFull.height()/2, |
| 407 | kRGBA_8888_SkColorType, |
| 408 | kUnpremul_SkAlphaType)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 409 | |
| 410 | for (int y = 0; y < planes.fYFull.height()/2; ++y) { |
| 411 | for (int x = 0; x < planes.fYFull.width()/2; ++x) { |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 412 | uint8_t U = *planes.fUQuarter.getAddr8(x, y); |
| 413 | uint8_t V = *planes.fVQuarter.getAddr8(x, y); |
| 414 | |
| 415 | // NOT premul! |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 416 | // V and 0 swapped to match RGBA layout |
| 417 | *vuQuarter.getAddr32(x, y) = SkColorSetARGB(0, 0, U, V); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | resultBMs[nextLayer++] = planes.fYFull; |
| 422 | resultBMs[nextLayer++] = vuQuarter; |
| 423 | |
| 424 | yuvaIndices[0].fIndex = 0; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 425 | yuvaIndices[0].fChannel = SkColorChannel::kA; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 426 | yuvaIndices[1].fIndex = 1; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 427 | yuvaIndices[1].fChannel = SkColorChannel::kG; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 428 | yuvaIndices[2].fIndex = 1; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 429 | yuvaIndices[2].fChannel = SkColorChannel::kR; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | case kI420_YUVFormat: |
| 433 | resultBMs[nextLayer++] = planes.fYFull; |
| 434 | resultBMs[nextLayer++] = planes.fUQuarter; |
| 435 | resultBMs[nextLayer++] = planes.fVQuarter; |
| 436 | |
| 437 | yuvaIndices[0].fIndex = 0; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 438 | yuvaIndices[0].fChannel = SkColorChannel::kA; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 439 | yuvaIndices[1].fIndex = 1; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 440 | yuvaIndices[1].fChannel = SkColorChannel::kA; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 441 | yuvaIndices[2].fIndex = 2; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 442 | yuvaIndices[2].fChannel = SkColorChannel::kA; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 443 | break; |
| 444 | case kYV12_YUVFormat: |
| 445 | resultBMs[nextLayer++] = planes.fYFull; |
| 446 | resultBMs[nextLayer++] = planes.fVQuarter; |
| 447 | resultBMs[nextLayer++] = planes.fUQuarter; |
| 448 | |
| 449 | yuvaIndices[0].fIndex = 0; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 450 | yuvaIndices[0].fChannel = SkColorChannel::kA; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 451 | yuvaIndices[1].fIndex = 2; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 452 | yuvaIndices[1].fChannel = SkColorChannel::kA; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 453 | yuvaIndices[2].fIndex = 1; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 454 | yuvaIndices[2].fChannel = SkColorChannel::kA; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | |
Jim Van Verth | 976a6b0 | 2018-10-17 15:27:19 -0400 | [diff] [blame] | 458 | if (kAYUV_YUVFormat != yuvFormat) { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 459 | if (opaque) { |
| 460 | yuvaIndices[3].fIndex = -1; |
| 461 | } else { |
| 462 | resultBMs[nextLayer] = planes.fAFull; |
| 463 | |
| 464 | yuvaIndices[3].fIndex = nextLayer; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 465 | yuvaIndices[3].fChannel = SkColorChannel::kA; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | } |
| 470 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 471 | static uint8_t look_up(float x1, float y1, const SkBitmap& bm, SkColorChannel channel) { |
| 472 | uint8_t result; |
| 473 | |
| 474 | int x = SkScalarFloorToInt(x1 * bm.width()); |
| 475 | int y = SkScalarFloorToInt(y1 * bm.height()); |
| 476 | |
| 477 | if (kAlpha_8_SkColorType == bm.colorType()) { |
| 478 | SkASSERT(SkColorChannel::kA == channel); |
| 479 | result = *bm.getAddr8(x, y); |
| 480 | } else { |
Jim Van Verth | 47133fd | 2018-10-19 22:09:28 -0400 | [diff] [blame] | 481 | SkASSERT(kRGBA_8888_SkColorType == bm.colorType()); |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 482 | |
| 483 | switch (channel) { |
| 484 | case SkColorChannel::kR: |
| 485 | result = SkColorGetR(bm.getColor(x, y)); |
| 486 | break; |
| 487 | case SkColorChannel::kG: |
| 488 | result = SkColorGetG(bm.getColor(x, y)); |
| 489 | break; |
| 490 | case SkColorChannel::kB: |
| 491 | result = SkColorGetB(bm.getColor(x, y)); |
| 492 | break; |
| 493 | case SkColorChannel::kA: |
| 494 | result = SkColorGetA(bm.getColor(x, y)); |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return result; |
| 500 | } |
| 501 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 502 | class YUVGenerator : public SkImageGenerator { |
| 503 | public: |
| 504 | YUVGenerator(const SkImageInfo& ii, |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 505 | SkYUVColorSpace yuvColorSpace, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 506 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 507 | SkBitmap bitmaps[SkYUVASizeInfo::kMaxCount]) |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 508 | : SkImageGenerator(ii) |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 509 | , fYUVColorSpace(yuvColorSpace) { |
| 510 | memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices)); |
| 511 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 512 | SkAssertResult(SkYUVAIndex::AreValidIndices(fYUVAIndices, &fNumBitmaps)); |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 513 | SkASSERT(fNumBitmaps > 0 && fNumBitmaps <= SkYUVASizeInfo::kMaxCount); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 514 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 515 | for (int i = 0; i < fNumBitmaps; ++i) { |
| 516 | fYUVBitmaps[i] = bitmaps[i]; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | protected: |
| 521 | bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 522 | const Options&) override { |
| 523 | |
| 524 | if (kUnknown_SkColorType == fFlattened.colorType()) { |
Brian Osman | 69083b2 | 2018-10-19 13:48:22 -0400 | [diff] [blame] | 525 | fFlattened.allocPixels(this->getInfo()); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 526 | |
| 527 | for (int y = 0; y < info.height(); ++y) { |
| 528 | for (int x = 0; x < info.width(); ++x) { |
| 529 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 530 | float x1 = (x + 0.5f) / info.width(); |
| 531 | float y1 = (y + 0.5f) / info.height(); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 532 | |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 533 | uint8_t Y = look_up(x1, y1, |
| 534 | fYUVBitmaps[fYUVAIndices[0].fIndex], |
| 535 | fYUVAIndices[0].fChannel); |
| 536 | |
| 537 | uint8_t U = look_up(x1, y1, |
| 538 | fYUVBitmaps[fYUVAIndices[1].fIndex], |
| 539 | fYUVAIndices[1].fChannel); |
| 540 | |
| 541 | |
| 542 | uint8_t V = look_up(x1, y1, |
| 543 | fYUVBitmaps[fYUVAIndices[2].fIndex], |
| 544 | fYUVAIndices[2].fChannel); |
| 545 | |
| 546 | uint8_t A = 255; |
| 547 | if (fYUVAIndices[3].fIndex >= 0) { |
| 548 | A = look_up(x1, y1, |
| 549 | fYUVBitmaps[fYUVAIndices[3].fIndex], |
| 550 | fYUVAIndices[3].fChannel); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | // Making premul here. |
Robert Phillips | 1c7062d | 2018-10-04 10:44:53 -0400 | [diff] [blame] | 554 | switch (fYUVColorSpace) { |
| 555 | case kJPEG_SkYUVColorSpace: |
| 556 | *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_jpeg(Y, U, V, A); |
| 557 | break; |
| 558 | case kRec601_SkYUVColorSpace: |
| 559 | *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_601(Y, U, V, A); |
| 560 | break; |
| 561 | case kRec709_SkYUVColorSpace: |
| 562 | *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba_709(Y, U, V, A); |
| 563 | break; |
| 564 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | return fFlattened.readPixels(info, pixels, rowBytes, 0, 0); |
| 570 | } |
| 571 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 572 | bool onQueryYUVA8(SkYUVASizeInfo* size, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 573 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
| 574 | SkYUVColorSpace* yuvColorSpace) const override { |
| 575 | |
| 576 | memcpy(yuvaIndices, fYUVAIndices, sizeof(fYUVAIndices)); |
| 577 | *yuvColorSpace = fYUVColorSpace; |
| 578 | |
| 579 | int i = 0; |
| 580 | for ( ; i < fNumBitmaps; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 581 | size->fSizes[i].fWidth = fYUVBitmaps[i].width(); |
| 582 | size->fSizes[i].fHeight = fYUVBitmaps[i].height(); |
| 583 | size->fWidthBytes[i] = fYUVBitmaps[i].rowBytes(); |
| 584 | } |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 585 | for ( ; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 586 | size->fSizes[i].fWidth = 0; |
| 587 | size->fSizes[i].fHeight = 0; |
| 588 | size->fWidthBytes[i] = 0; |
Jim Van Verth | f99a674 | 2018-10-18 16:13:18 +0000 | [diff] [blame] | 589 | } |
Jim Van Verth | 0c583af | 2018-10-18 10:31:59 -0400 | [diff] [blame] | 590 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 591 | return true; |
| 592 | } |
| 593 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 594 | bool onGetYUVA8Planes(const SkYUVASizeInfo&, const SkYUVAIndex[SkYUVAIndex::kIndexCount], |
| 595 | void* planes[SkYUVASizeInfo::kMaxCount]) override { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 596 | for (int i = 0; i < fNumBitmaps; ++i) { |
| 597 | planes[i] = fYUVBitmaps[i].getPixels(); |
| 598 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 599 | return true; |
| 600 | } |
| 601 | |
| 602 | private: |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 603 | SkYUVColorSpace fYUVColorSpace; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 604 | SkYUVAIndex fYUVAIndices[SkYUVAIndex::kIndexCount]; |
| 605 | int fNumBitmaps; |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 606 | SkBitmap fYUVBitmaps[SkYUVASizeInfo::kMaxCount]; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 607 | SkBitmap fFlattened; |
| 608 | |
| 609 | }; |
| 610 | |
| 611 | static sk_sp<SkImage> make_yuv_gen_image(const SkImageInfo& ii, |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 612 | SkYUVColorSpace yuvColorSpace, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 613 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 614 | SkBitmap bitmaps[]) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 615 | std::unique_ptr<SkImageGenerator> gen(new YUVGenerator(ii, yuvColorSpace, |
| 616 | yuvaIndices, bitmaps)); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 617 | |
| 618 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 619 | } |
| 620 | |
| 621 | static void draw_col_label(SkCanvas* canvas, int x, int yuvColorSpace, bool opaque) { |
| 622 | static const char* kYUVColorSpaceNames[] = { "JPEG", "601", "709" }; |
| 623 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kYUVColorSpaceNames) == kLastEnum_SkYUVColorSpace+1); |
| 624 | |
| 625 | SkPaint textPaint; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 626 | sk_tool_utils::set_portable_typeface(&textPaint, nullptr, SkFontStyle::Bold()); |
| 627 | textPaint.setTextSize(16); |
| 628 | |
| 629 | SkRect textRect; |
| 630 | SkString colLabel; |
| 631 | |
| 632 | colLabel.printf("%s", kYUVColorSpaceNames[yuvColorSpace]); |
| 633 | textPaint.measureText(colLabel.c_str(), colLabel.size(), &textRect); |
| 634 | int y = textRect.height(); |
| 635 | |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 636 | SkTextUtils::DrawString(canvas, colLabel, x, y, textPaint, SkTextUtils::kCenter_Align); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 637 | |
| 638 | colLabel.printf("%s", opaque ? "Opaque" : "Transparent"); |
Mike Reed | 71f5a0b | 2018-10-25 16:12:39 -0400 | [diff] [blame] | 639 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 640 | textPaint.measureText(colLabel.c_str(), colLabel.size(), &textRect); |
| 641 | y += textRect.height(); |
| 642 | |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 643 | SkTextUtils::DrawString(canvas, colLabel, x, y, textPaint, SkTextUtils::kCenter_Align); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) { |
| 647 | static const char* kYUVFormatNames[] = { "AYUV", "NV12", "NV21", "I420", "YV12" }; |
| 648 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kYUVFormatNames) == kLast_YUVFormat+1); |
| 649 | |
| 650 | SkPaint textPaint; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 651 | sk_tool_utils::set_portable_typeface(&textPaint, nullptr, SkFontStyle::Bold()); |
| 652 | textPaint.setTextSize(16); |
| 653 | |
| 654 | SkRect textRect; |
| 655 | SkString rowLabel; |
| 656 | |
| 657 | rowLabel.printf("%s", kYUVFormatNames[yuvFormat]); |
| 658 | textPaint.measureText(rowLabel.c_str(), rowLabel.size(), &textRect); |
| 659 | y += kTileWidthHeight/2 + textRect.height()/2; |
| 660 | |
| 661 | canvas->drawText(rowLabel.c_str(), rowLabel.size(), 0, y, textPaint); |
| 662 | } |
| 663 | |
| 664 | namespace skiagm { |
| 665 | |
| 666 | // This GM creates an opaque and transparent bitmap, extracts the planes and then recombines |
| 667 | // them into various YUV formats. It then renders the results in the grid: |
| 668 | // |
| 669 | // JPEG 601 709 |
| 670 | // Transparent Opaque Transparent Opaque Transparent Opaque |
| 671 | // AYUV |
| 672 | // NV12 |
| 673 | // NV21 |
| 674 | // I420 |
| 675 | // YV12 |
| 676 | class WackyYUVFormatsGM : public GM { |
| 677 | public: |
| 678 | WackyYUVFormatsGM() { |
| 679 | this->setBGColor(0xFFCCCCCC); |
| 680 | } |
| 681 | |
| 682 | protected: |
| 683 | |
| 684 | SkString onShortName() override { |
| 685 | return SkString("wacky_yuv_formats"); |
| 686 | } |
| 687 | |
| 688 | SkISize onISize() override { |
| 689 | int numCols = 2 * (kLastEnum_SkYUVColorSpace + 1); // opacity x color-space |
| 690 | int numRows = 1 + (kLast_YUVFormat + 1); // origin + # yuv formats |
| 691 | return SkISize::Make(kLabelWidth + numCols * (kTileWidthHeight + kPad), |
| 692 | kLabelHeight + numRows * (kTileWidthHeight + kPad)); |
| 693 | } |
| 694 | |
| 695 | void onOnceBeforeDraw() override { |
| 696 | SkPoint origin = { kTileWidthHeight/2.0f, kTileWidthHeight/2.0f }; |
| 697 | float outerRadius = kTileWidthHeight/2.0f - 20.0f; |
| 698 | float innerRadius = 20.0f; |
| 699 | |
| 700 | { |
| 701 | // transparent |
| 702 | SkTDArray<SkRect> circles; |
| 703 | SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 5, &circles); |
| 704 | fOriginalBMs[0] = make_bitmap(path, circles, false); |
| 705 | } |
| 706 | |
| 707 | { |
| 708 | // opaque |
| 709 | SkTDArray<SkRect> circles; |
| 710 | SkPath path = create_splat(origin, innerRadius, outerRadius, 1.0f, 7, &circles); |
| 711 | fOriginalBMs[1] = make_bitmap(path, circles, true); |
| 712 | } |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 713 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 714 | |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 715 | void createImages(GrContext* context) { |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 716 | int counter = 0; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 717 | for (bool opaque : { false, true }) { |
| 718 | for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) { |
| 719 | PlaneData planes; |
| 720 | extract_planes(fOriginalBMs[opaque], (SkYUVColorSpace) cs, &planes); |
| 721 | |
Jim Van Verth | 976a6b0 | 2018-10-17 15:27:19 -0400 | [diff] [blame] | 722 | for (int format = kAYUV_YUVFormat; format <= kLast_YUVFormat; ++format) { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 723 | SkBitmap resultBMs[4]; |
| 724 | SkYUVAIndex yuvaIndices[4]; |
| 725 | create_YUV(planes, (YUVFormat) format, resultBMs, yuvaIndices, opaque); |
| 726 | |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 727 | if (context) { |
| 728 | if (context->abandoned()) { |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | GrGpu* gpu = context->contextPriv().getGpu(); |
| 733 | if (!gpu) { |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | bool used[4] = { false, false, false, false }; |
| 738 | for (int i = 0; i < 4; ++i) { |
| 739 | if (yuvaIndices[i].fIndex >= 0) { |
| 740 | SkASSERT(yuvaIndices[i].fIndex < 4); |
| 741 | used[yuvaIndices[i].fIndex] = true; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | GrBackendTexture yuvaTextures[4]; |
| 746 | |
| 747 | for (int i = 0; i < 4; ++i) { |
| 748 | if (!used[i]) { |
| 749 | continue; |
| 750 | } |
| 751 | |
| 752 | yuvaTextures[i] = gpu->createTestingOnlyBackendTexture( |
| 753 | resultBMs[i].getPixels(), |
| 754 | resultBMs[i].width(), |
| 755 | resultBMs[i].height(), |
| 756 | resultBMs[i].colorType(), |
| 757 | false, |
| 758 | GrMipMapped::kNo, |
| 759 | resultBMs[i].rowBytes()); |
| 760 | } |
| 761 | |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 762 | if (counter & 0x1) { |
| 763 | fImages[opaque][cs][format] = SkImage::MakeFromYUVATexturesCopy( |
| 764 | context, |
| 765 | (SkYUVColorSpace)cs, |
| 766 | yuvaTextures, |
| 767 | yuvaIndices, |
| 768 | { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() }, |
| 769 | kTopLeft_GrSurfaceOrigin); |
| 770 | } else { |
| 771 | fImages[opaque][cs][format] = SkImage::MakeFromYUVATextures( |
| 772 | context, |
| 773 | (SkYUVColorSpace)cs, |
| 774 | yuvaTextures, |
| 775 | yuvaIndices, |
| 776 | { fOriginalBMs[opaque].width(), fOriginalBMs[opaque].height() }, |
| 777 | kTopLeft_GrSurfaceOrigin); |
| 778 | } |
| 779 | } else { |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 780 | fImages[opaque][cs][format] = make_yuv_gen_image( |
| 781 | fOriginalBMs[opaque].info(), |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 782 | (SkYUVColorSpace) cs, |
| 783 | yuvaIndices, |
| 784 | resultBMs); |
| 785 | } |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 786 | } |
| 787 | } |
Jim Van Verth | 9bf8120 | 2018-10-30 15:53:36 -0400 | [diff] [blame] | 788 | ++counter; |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | void onDraw(SkCanvas* canvas) override { |
Robert Phillips | 51c89e4 | 2018-10-05 13:30:43 -0400 | [diff] [blame] | 793 | this->createImages(canvas->getGrContext()); |
| 794 | |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 795 | int x = kLabelWidth; |
| 796 | for (int cs = kJPEG_SkYUVColorSpace; cs <= kLastEnum_SkYUVColorSpace; ++cs) { |
| 797 | for (int opaque : { 0, 1 }) { |
| 798 | int y = kLabelHeight; |
| 799 | |
| 800 | draw_col_label(canvas, x+kTileWidthHeight/2, cs, opaque); |
| 801 | |
| 802 | canvas->drawBitmap(fOriginalBMs[opaque], x, y); |
| 803 | y += kTileWidthHeight + kPad; |
| 804 | |
Jim Van Verth | 976a6b0 | 2018-10-17 15:27:19 -0400 | [diff] [blame] | 805 | for (int format = kAYUV_YUVFormat; format <= kLast_YUVFormat; ++format) { |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 806 | draw_row_label(canvas, y, format); |
| 807 | canvas->drawImage(fImages[opaque][cs][format], x, y); |
| 808 | |
| 809 | y += kTileWidthHeight + kPad; |
| 810 | } |
| 811 | |
| 812 | x += kTileWidthHeight + kPad; |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | private: |
| 818 | SkBitmap fOriginalBMs[2]; |
| 819 | sk_sp<SkImage> fImages[2][kLastEnum_SkYUVColorSpace+1][kLast_YUVFormat+1]; |
| 820 | |
| 821 | typedef GM INHERITED; |
| 822 | }; |
| 823 | |
| 824 | ////////////////////////////////////////////////////////////////////////////// |
| 825 | |
| 826 | DEF_GM(return new WackyYUVFormatsGM;) |
| 827 | } |