bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | // This test only works with the GPU backend. |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkBitmap.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "include/core/SkColor.h" |
| 14 | #include "include/core/SkColorFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 16 | #include "include/core/SkImageInfo.h" |
| 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkPixmap.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 19 | #include "include/core/SkRefCnt.h" |
| 20 | #include "include/core/SkScalar.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "include/core/SkSize.h" |
| 22 | #include "include/core/SkString.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 23 | #include "include/core/SkTypes.h" |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 24 | #include "include/core/SkYUVAIndex.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/gpu/GrBackendSurface.h" |
| 26 | #include "include/gpu/GrContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #include "include/gpu/GrTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "include/private/SkTo.h" |
Brian Salomon | c505a45 | 2020-04-06 10:29:02 -0400 | [diff] [blame] | 29 | #include "src/core/SkMathPriv.h" |
Brian Salomon | 7c94d18 | 2020-03-18 17:24:02 -0400 | [diff] [blame] | 30 | #include "src/core/SkYUVMath.h" |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 31 | #include "tools/Resources.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 32 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 33 | class GrRenderTargetContext; |
| 34 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 35 | namespace skiagm { |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 36 | class ImageFromYUVTextures : public GpuGM { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 37 | public: |
| 38 | ImageFromYUVTextures() { |
| 39 | this->setBGColor(0xFFFFFFFF); |
| 40 | } |
| 41 | |
| 42 | protected: |
| 43 | SkString onShortName() override { |
| 44 | return SkString("image_from_yuv_textures"); |
| 45 | } |
| 46 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 47 | SkISize onISize() override { return {1420, 610}; } |
Brian Salomon | 9b22838 | 2020-03-24 12:55:29 -0400 | [diff] [blame] | 48 | |
Brian Salomon | 5575e3c | 2020-03-25 15:19:37 +0000 | [diff] [blame] | 49 | void onOnceBeforeDraw() override { |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 50 | fRGBABmp = this->createBmpAndPlanes("images/mandrill_32.png", fYUVABmps); |
| 51 | } |
Brian Salomon | 3673748 | 2020-03-24 16:47:52 -0400 | [diff] [blame] | 52 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 53 | SkBitmap createBmpAndPlanes(const char* name, SkBitmap yuvaBmps[4]) { |
| 54 | SkBitmap bmp; |
| 55 | if (!GetResourceAsBitmap(name, &bmp)) { |
| 56 | return {}; |
| 57 | } |
| 58 | auto ii = SkImageInfo::Make(bmp.dimensions(), kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 59 | |
| 60 | SkBitmap rgbaBmp; |
| 61 | rgbaBmp.allocPixels(ii); |
| 62 | bmp.readPixels(rgbaBmp.pixmap(), 0, 0); |
| 63 | |
| 64 | SkImageInfo yaInfo = SkImageInfo::Make(rgbaBmp.dimensions(), kAlpha_8_SkColorType, |
| 65 | kUnpremul_SkAlphaType); |
| 66 | yuvaBmps[0].allocPixels(yaInfo); |
| 67 | SkISize uvSize = {rgbaBmp.width()/2, rgbaBmp.height()/2}; |
| 68 | SkImageInfo uvInfo = SkImageInfo::Make(uvSize, kAlpha_8_SkColorType, kUnpremul_SkAlphaType); |
| 69 | yuvaBmps[1].allocPixels(uvInfo); |
| 70 | yuvaBmps[2].allocPixels(uvInfo); |
| 71 | yuvaBmps[3].allocPixels(yaInfo); |
| 72 | |
| 73 | unsigned char* yuvPixels[] = { |
| 74 | static_cast<unsigned char*>(yuvaBmps[0].getPixels()), |
| 75 | static_cast<unsigned char*>(yuvaBmps[1].getPixels()), |
| 76 | static_cast<unsigned char*>(yuvaBmps[2].getPixels()), |
| 77 | static_cast<unsigned char*>(yuvaBmps[3].getPixels()), |
| 78 | }; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 79 | |
Brian Salomon | 7c94d18 | 2020-03-18 17:24:02 -0400 | [diff] [blame] | 80 | float m[20]; |
| 81 | SkColorMatrix_RGB2YUV(kJPEG_SkYUVColorSpace, m); |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 82 | // Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though |
| 83 | // we will draw it with all the supported yuv color spaces when converted back to RGB |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 84 | for (int j = 0; j < yaInfo.height(); ++j) { |
| 85 | for (int i = 0; i < yaInfo.width(); ++i) { |
| 86 | auto rgba = *rgbaBmp.getAddr32(i, j); |
| 87 | auto r = (rgba & 0x000000ff) >> 0; |
| 88 | auto g = (rgba & 0x0000ff00) >> 8; |
| 89 | auto b = (rgba & 0x00ff0000) >> 16; |
| 90 | auto a = (rgba & 0xff000000) >> 24; |
| 91 | yuvPixels[0][j*yaInfo.width() + i] = SkToU8( |
| 92 | sk_float_round2int(m[0]*r + m[1]*g + m[2]*b + m[3]*a + 255*m[4])); |
| 93 | yuvPixels[3][j*yaInfo.width() + i] = SkToU8(sk_float_round2int( |
| 94 | m[15]*r + m[16]*g + m[17]*b + m[18]*a + 255*m[19])); |
| 95 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 96 | } |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 97 | for (int j = 0; j < uvInfo.height(); ++j) { |
| 98 | for (int i = 0; i < uvInfo.width(); ++i) { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 99 | // Average together 4 pixels of RGB. |
Brian Salomon | 7c94d18 | 2020-03-18 17:24:02 -0400 | [diff] [blame] | 100 | int rgba[] = {0, 0, 0, 0}; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 101 | for (int y = 0; y < 2; ++y) { |
| 102 | for (int x = 0; x < 2; ++x) { |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 103 | auto src = *rgbaBmp.getAddr32(2 * i + x, 2 * j + y); |
| 104 | rgba[0] += (src & 0x000000ff) >> 0; |
| 105 | rgba[1] += (src & 0x0000ff00) >> 8; |
| 106 | rgba[2] += (src & 0x00ff0000) >> 16; |
| 107 | rgba[3] += (src & 0xff000000) >> 24; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
Brian Salomon | 7c94d18 | 2020-03-18 17:24:02 -0400 | [diff] [blame] | 110 | for (int c = 0; c < 4; ++c) { |
| 111 | rgba[c] /= 4; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 112 | } |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 113 | int uvIndex = j*uvInfo.width() + i; |
| 114 | yuvPixels[1][uvIndex] = SkToU8(sk_float_round2int( |
Brian Salomon | 7c94d18 | 2020-03-18 17:24:02 -0400 | [diff] [blame] | 115 | m[5]*rgba[0] + m[6]*rgba[1] + m[7]*rgba[2] + m[8]*rgba[3] + 255*m[9])); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 116 | yuvPixels[2][uvIndex] = SkToU8(sk_float_round2int( |
Brian Salomon | 7c94d18 | 2020-03-18 17:24:02 -0400 | [diff] [blame] | 117 | m[10]*rgba[0] + m[11]*rgba[1] + m[12]*rgba[2] + m[13]*rgba[3] + 255*m[14])); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 120 | return rgbaBmp; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 123 | void createYUVTextures(SkBitmap bmps[4], GrContext* context, GrBackendTexture textures[4]) { |
| 124 | for (int i = 0; i < 4; ++i) { |
Brian Salomon | c505a45 | 2020-04-06 10:29:02 -0400 | [diff] [blame] | 125 | textures[i] = context->createBackendTexture(bmps[i].pixmap(), GrRenderable::kNo, |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 126 | GrProtected::kNo); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 127 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 130 | void createResultTexture(GrContext* context, SkISize size, GrBackendTexture* resultTexture) { |
Robert Phillips | 4bdd36f | 2019-06-04 11:03:06 -0400 | [diff] [blame] | 131 | *resultTexture = context->createBackendTexture( |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 132 | size.width(), size.height(), kRGBA_8888_SkColorType, SkColors::kTransparent, |
Robert Phillips | da2e67a | 2019-07-01 15:04:06 -0400 | [diff] [blame] | 133 | GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo); |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Brian Salomon | 279f873 | 2018-09-18 11:19:33 -0400 | [diff] [blame] | 136 | void deleteBackendTextures(GrContext* context, GrBackendTexture textures[], int n) { |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 137 | if (context->abandoned()) { |
Robert Phillips | 6d36370 | 2018-06-25 08:53:09 -0400 | [diff] [blame] | 138 | return; |
| 139 | } |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 140 | |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 141 | GrFlushInfo flushInfo; |
| 142 | flushInfo.fFlags = kSyncCpu_GrFlushFlag; |
| 143 | context->flush(flushInfo); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 144 | context->submit(true); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 145 | |
Brian Salomon | 279f873 | 2018-09-18 11:19:33 -0400 | [diff] [blame] | 146 | for (int i = 0; i < n; ++i) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 147 | context->deleteBackendTexture(textures[i]); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 148 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 151 | void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override { |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 152 | GrBackendTexture yuvaTextures[4]; |
Brian Salomon | c505a45 | 2020-04-06 10:29:02 -0400 | [diff] [blame] | 153 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 154 | this->createYUVTextures(fYUVABmps, context, yuvaTextures); |
Brian Salomon | c505a45 | 2020-04-06 10:29:02 -0400 | [diff] [blame] | 155 | SkYUVAIndex indices[4]; |
| 156 | for (int i = 0; i < 4; ++i) { |
| 157 | if (!yuvaTextures[i].isValid()) { |
| 158 | return; |
| 159 | } |
| 160 | auto chanMask = yuvaTextures[i].getBackendFormat().channelMask(); |
| 161 | // We expect the single channel bitmaps to produce single channel textures. |
| 162 | SkASSERT(chanMask && SkIsPow2(chanMask)); |
| 163 | if (chanMask & kGray_SkColorChannelFlag) { |
| 164 | indices[i].fChannel = SkColorChannel::kR; |
| 165 | } else { |
| 166 | indices[i].fChannel = static_cast<SkColorChannel>(31 - SkCLZ(chanMask)); |
| 167 | } |
| 168 | indices[i].fIndex = i; |
| 169 | } |
Brian Salomon | 8efbbbc | 2020-04-02 16:59:19 -0400 | [diff] [blame] | 170 | |
| 171 | // We remake this image before each draw because if any draw flattens it to RGBA then |
| 172 | // all subsequent draws use the RGBA texture. |
| 173 | auto makeImage1 = [&]() { |
| 174 | return SkImage::MakeFromYUVATextures(context, |
| 175 | kJPEG_SkYUVColorSpace, |
| 176 | yuvaTextures, |
Brian Salomon | c505a45 | 2020-04-06 10:29:02 -0400 | [diff] [blame] | 177 | indices, |
Brian Salomon | 8efbbbc | 2020-04-02 16:59:19 -0400 | [diff] [blame] | 178 | fRGBABmp.dimensions(), |
| 179 | kTopLeft_GrSurfaceOrigin); |
| 180 | }; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 181 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 182 | GrBackendTexture resultTexture; |
| 183 | this->createResultTexture(context, fRGBABmp.dimensions(), &resultTexture); |
| 184 | auto image2 = SkImage::MakeFromYUVTexturesCopyWithExternalBackend(context, |
| 185 | kJPEG_SkYUVColorSpace, |
| 186 | yuvaTextures, |
| 187 | kTopLeft_GrSurfaceOrigin, |
| 188 | resultTexture); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 189 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 190 | auto draw_image = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize { |
| 191 | if (!image) { |
| 192 | return {0, 0}; |
Brian Salomon | 3673748 | 2020-03-24 16:47:52 -0400 | [diff] [blame] | 193 | } |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 194 | SkPaint paint; |
| 195 | paint.setFilterQuality(fq); |
| 196 | canvas->drawImage(image, 0, 0, &paint); |
| 197 | return {SkIntToScalar(image->width()), SkIntToScalar(image->height())}; |
| 198 | }; |
Brian Salomon | 3673748 | 2020-03-24 16:47:52 -0400 | [diff] [blame] | 199 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 200 | auto draw_image_rect = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize { |
| 201 | if (!image) { |
| 202 | return {0, 0}; |
| 203 | } |
| 204 | SkPaint paint; |
| 205 | paint.setFilterQuality(fq); |
| 206 | auto subset = SkRect::Make(image->dimensions()); |
| 207 | subset.inset(subset.width() * .05f, subset.height() * .1f); |
| 208 | auto dst = SkRect::MakeWH(subset.width(), subset.height()); |
| 209 | canvas->drawImageRect(image, subset, dst, &paint); |
| 210 | return {dst.width(), dst.height()}; |
| 211 | }; |
| 212 | |
| 213 | auto draw_image_shader = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize { |
| 214 | if (!image) { |
| 215 | return {0, 0}; |
| 216 | } |
| 217 | SkMatrix m; |
| 218 | m.setRotate(45, image->width()/2.f, image->height()/2.f); |
| 219 | auto shader = image->makeShader(SkTileMode::kMirror, SkTileMode::kDecal, m); |
| 220 | SkPaint paint; |
| 221 | paint.setFilterQuality(fq); |
| 222 | paint.setShader(std::move(shader)); |
| 223 | auto rect = SkRect::MakeWH(image->width() * 1.3f, image->height()); |
| 224 | canvas->drawRect(rect, paint); |
| 225 | return {rect.width(), rect.height()}; |
| 226 | }; |
| 227 | |
| 228 | canvas->translate(kPad, kPad); |
| 229 | using DrawSig = SkSize(SkImage* image, SkFilterQuality fq); |
| 230 | using DF = std::function<DrawSig>; |
| 231 | for (const auto& draw : {DF(draw_image), DF(draw_image_rect), DF(draw_image_shader)}) { |
| 232 | for (auto scale : {1.f, 4.f, 0.75f}) { |
| 233 | SkScalar h = 0; |
| 234 | canvas->save(); |
| 235 | for (auto fq : {kNone_SkFilterQuality, kLow_SkFilterQuality, |
| 236 | kMedium_SkFilterQuality, kHigh_SkFilterQuality}) { |
| 237 | canvas->save(); |
| 238 | canvas->scale(scale, scale); |
Brian Salomon | 8efbbbc | 2020-04-02 16:59:19 -0400 | [diff] [blame] | 239 | auto s1 = draw(makeImage1().get(), fq); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 240 | canvas->restore(); |
| 241 | canvas->translate(kPad + SkScalarCeilToScalar(scale*s1.width()), 0); |
| 242 | canvas->save(); |
| 243 | canvas->scale(scale, scale); |
| 244 | auto s2 = draw(image2.get(), fq); |
| 245 | canvas->restore(); |
| 246 | canvas->translate(kPad + SkScalarCeilToScalar(scale*s2.width()), 0); |
| 247 | h = std::max({h, s1.height(), s2.height()}); |
| 248 | } |
| 249 | canvas->restore(); |
| 250 | canvas->translate(0, kPad + SkScalarCeilToScalar(scale*h)); |
| 251 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 252 | } |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 253 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 254 | this->deleteBackendTextures(context, &resultTexture, 1); |
| 255 | this->deleteBackendTextures(context, yuvaTextures, 4); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | private: |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 259 | SkBitmap fRGBABmp; |
| 260 | SkBitmap fYUVABmps[4]; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 261 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 262 | static constexpr SkScalar kPad = 10.0f; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 263 | |
| 264 | typedef GM INHERITED; |
| 265 | }; |
| 266 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 267 | DEF_GM(return new ImageFromYUVTextures;) |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 268 | } |