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" |
| 15 | #include "include/core/SkColorPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkImageInfo.h" |
| 18 | #include "include/core/SkPaint.h" |
| 19 | #include "include/core/SkPixmap.h" |
| 20 | #include "include/core/SkPoint.h" |
| 21 | #include "include/core/SkRefCnt.h" |
| 22 | #include "include/core/SkScalar.h" |
| 23 | #include "include/core/SkShader.h" |
| 24 | #include "include/core/SkSize.h" |
| 25 | #include "include/core/SkString.h" |
| 26 | #include "include/core/SkTileMode.h" |
| 27 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "include/effects/SkGradientShader.h" |
| 29 | #include "include/gpu/GrBackendSurface.h" |
| 30 | #include "include/gpu/GrContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 31 | #include "include/gpu/GrTypes.h" |
| 32 | #include "include/private/GrTypesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 33 | #include "include/private/SkTo.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 34 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 35 | class GrRenderTargetContext; |
| 36 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 37 | static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() { |
| 38 | static const float kJPEGConversionMatrix[20] = { |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 39 | 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255, |
| 40 | 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255, |
| 41 | 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255, |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 42 | 0.0f, 0.0f, 0.0f, 1.0f, 0.0f |
| 43 | }; |
| 44 | |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 45 | return SkColorFilters::Matrix(kJPEGConversionMatrix); |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 46 | } |
| 47 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 48 | namespace skiagm { |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 49 | class ImageFromYUVTextures : public GpuGM { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 50 | public: |
| 51 | ImageFromYUVTextures() { |
| 52 | this->setBGColor(0xFFFFFFFF); |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | SkString onShortName() override { |
| 57 | return SkString("image_from_yuv_textures"); |
| 58 | } |
| 59 | |
| 60 | SkISize onISize() override { |
Brian Osman | 2b73e66 | 2019-11-01 10:02:24 -0400 | [diff] [blame] | 61 | // Original image, plus each color space drawn twice |
| 62 | int numBitmaps = 2 * (kLastEnum_SkYUVColorSpace + 1) + 1; |
| 63 | return SkISize::Make(kBmpSize + 2 * kPad, numBitmaps * (kBmpSize + kPad) + kPad); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void onOnceBeforeDraw() override { |
| 67 | // We create an RGB bitmap and then extract YUV bmps where the U and V bitmaps are |
| 68 | // subsampled by 2 in both dimensions. |
| 69 | SkPaint paint; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 70 | constexpr SkColor kColors[] = |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 71 | { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 72 | paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(0,0), kBmpSize / 2.f, kColors, |
| 73 | nullptr, SK_ARRAY_COUNT(kColors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 74 | SkTileMode::kMirror)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 75 | SkBitmap rgbBmp; |
| 76 | rgbBmp.allocN32Pixels(kBmpSize, kBmpSize, true); |
| 77 | SkCanvas canvas(rgbBmp); |
| 78 | canvas.drawPaint(paint); |
| 79 | SkPMColor* rgbColors = static_cast<SkPMColor*>(rgbBmp.getPixels()); |
| 80 | |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 81 | SkImageInfo yinfo = SkImageInfo::Make(kBmpSize, kBmpSize, kGray_8_SkColorType, |
| 82 | kUnpremul_SkAlphaType); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 83 | fYUVBmps[0].allocPixels(yinfo); |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 84 | SkImageInfo uinfo = SkImageInfo::Make(kBmpSize / 2, kBmpSize / 2, kGray_8_SkColorType, |
| 85 | kUnpremul_SkAlphaType); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 86 | fYUVBmps[1].allocPixels(uinfo); |
Robert Phillips | 00c9f0d | 2019-08-02 17:17:35 -0400 | [diff] [blame] | 87 | SkImageInfo vinfo = SkImageInfo::Make(kBmpSize / 2, kBmpSize / 2, kGray_8_SkColorType, |
| 88 | kUnpremul_SkAlphaType); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 89 | fYUVBmps[2].allocPixels(vinfo); |
| 90 | unsigned char* yPixels; |
| 91 | signed char* uvPixels[2]; |
| 92 | yPixels = static_cast<unsigned char*>(fYUVBmps[0].getPixels()); |
| 93 | uvPixels[0] = static_cast<signed char*>(fYUVBmps[1].getPixels()); |
jvanverth | 3e5f555 | 2015-07-16 07:46:07 -0700 | [diff] [blame] | 94 | uvPixels[1] = static_cast<signed char*>(fYUVBmps[2].getPixels()); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 95 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 96 | // Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though |
| 97 | // we will draw it with all the supported yuv color spaces when converted back to RGB |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 98 | for (int i = 0; i < kBmpSize * kBmpSize; ++i) { |
| 99 | yPixels[i] = static_cast<unsigned char>(0.299f * SkGetPackedR32(rgbColors[i]) + |
| 100 | 0.587f * SkGetPackedG32(rgbColors[i]) + |
| 101 | 0.114f * SkGetPackedB32(rgbColors[i])); |
| 102 | } |
| 103 | for (int j = 0; j < kBmpSize / 2; ++j) { |
| 104 | for (int i = 0; i < kBmpSize / 2; ++i) { |
| 105 | // Average together 4 pixels of RGB. |
| 106 | int rgb[] = { 0, 0, 0 }; |
| 107 | for (int y = 0; y < 2; ++y) { |
| 108 | for (int x = 0; x < 2; ++x) { |
| 109 | int rgbIndex = (2 * j + y) * kBmpSize + 2 * i + x; |
| 110 | rgb[0] += SkGetPackedR32(rgbColors[rgbIndex]); |
| 111 | rgb[1] += SkGetPackedG32(rgbColors[rgbIndex]); |
| 112 | rgb[2] += SkGetPackedB32(rgbColors[rgbIndex]); |
| 113 | } |
| 114 | } |
| 115 | for (int c = 0; c < 3; ++c) { |
| 116 | rgb[c] /= 4; |
| 117 | } |
| 118 | int uvIndex = j * kBmpSize / 2 + i; |
| 119 | uvPixels[0][uvIndex] = static_cast<signed char>( |
| 120 | ((-38 * rgb[0] - 74 * rgb[1] + 112 * rgb[2] + 128) >> 8) + 128); |
| 121 | uvPixels[1][uvIndex] = static_cast<signed char>( |
| 122 | ((112 * rgb[0] - 94 * rgb[1] - 18 * rgb[2] + 128) >> 8) + 128); |
| 123 | } |
| 124 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 125 | fRGBImage = SkImage::MakeRasterCopy(SkPixmap(rgbBmp.info(), rgbColors, rgbBmp.rowBytes())); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 128 | void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 129 | for (int i = 0; i < 3; ++i) { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 130 | SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes())); |
Robert Phillips | 6694440 | 2019-09-30 13:21:25 -0400 | [diff] [blame] | 131 | yuvTextures[i] = context->createBackendTexture(&fYUVBmps[i].pixmap(), 1, |
| 132 | GrRenderable::kNo, GrProtected::kNo); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 133 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Brian Salomon | 279f873 | 2018-09-18 11:19:33 -0400 | [diff] [blame] | 136 | void createResultTexture(GrContext* context, int width, int height, |
| 137 | GrBackendTexture* resultTexture) { |
Robert Phillips | 4bdd36f | 2019-06-04 11:03:06 -0400 | [diff] [blame] | 138 | *resultTexture = context->createBackendTexture( |
Robert Phillips | 8062679 | 2019-06-04 07:16:10 -0400 | [diff] [blame] | 139 | width, height, kRGBA_8888_SkColorType, SkColors::kTransparent, |
Robert Phillips | da2e67a | 2019-07-01 15:04:06 -0400 | [diff] [blame] | 140 | GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo); |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Brian Salomon | 279f873 | 2018-09-18 11:19:33 -0400 | [diff] [blame] | 143 | void deleteBackendTextures(GrContext* context, GrBackendTexture textures[], int n) { |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 144 | if (context->abandoned()) { |
Robert Phillips | 6d36370 | 2018-06-25 08:53:09 -0400 | [diff] [blame] | 145 | return; |
| 146 | } |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 147 | |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 148 | GrFlushInfo flushInfo; |
| 149 | flushInfo.fFlags = kSyncCpu_GrFlushFlag; |
| 150 | context->flush(flushInfo); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 151 | |
Brian Salomon | 279f873 | 2018-09-18 11:19:33 -0400 | [diff] [blame] | 152 | for (int i = 0; i < n; ++i) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 153 | context->deleteBackendTexture(textures[i]); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 154 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 157 | void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override { |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 158 | // draw the original |
| 159 | SkScalar yOffset = kPad; |
| 160 | canvas->drawImage(fRGBImage.get(), kPad, yOffset); |
| 161 | yOffset += kBmpSize + kPad; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 162 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 163 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 164 | GrBackendTexture yuvTextures[3]; |
| 165 | this->createYUVTextures(context, yuvTextures); |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 166 | auto image = SkImage::MakeFromYUVTexturesCopy(context, |
| 167 | static_cast<SkYUVColorSpace>(space), |
| 168 | yuvTextures, |
| 169 | kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 279f873 | 2018-09-18 11:19:33 -0400 | [diff] [blame] | 170 | this->deleteBackendTextures(context, yuvTextures, 3); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 171 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 172 | SkPaint paint; |
| 173 | if (kIdentity_SkYUVColorSpace == space) { |
| 174 | // The identity color space needs post-processing to appear correct |
| 175 | paint.setColorFilter(yuv_to_rgb_colorfilter()); |
| 176 | } |
| 177 | |
| 178 | canvas->drawImage(image.get(), kPad, yOffset, &paint); |
| 179 | yOffset += kBmpSize + kPad; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 180 | } |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 181 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 182 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 183 | GrBackendTexture yuvTextures[3]; |
Brian Salomon | 279f873 | 2018-09-18 11:19:33 -0400 | [diff] [blame] | 184 | GrBackendTexture resultTexture; |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 185 | this->createYUVTextures(context, yuvTextures); |
Brian Salomon | 279f873 | 2018-09-18 11:19:33 -0400 | [diff] [blame] | 186 | this->createResultTexture( |
| 187 | context, yuvTextures[0].width(), yuvTextures[0].height(), &resultTexture); |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 188 | auto image = SkImage::MakeFromYUVTexturesCopyWithExternalBackend( |
| 189 | context, |
| 190 | static_cast<SkYUVColorSpace>(space), |
| 191 | yuvTextures, |
| 192 | kTopLeft_GrSurfaceOrigin, |
| 193 | resultTexture); |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 194 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 195 | SkPaint paint; |
| 196 | if (kIdentity_SkYUVColorSpace == space) { |
| 197 | // The identity color space needs post-processing to appear correct |
| 198 | paint.setColorFilter(yuv_to_rgb_colorfilter()); |
| 199 | } |
| 200 | canvas->drawImage(image.get(), kPad, yOffset, &paint); |
| 201 | yOffset += kBmpSize + kPad; |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 202 | |
Brian Salomon | 279f873 | 2018-09-18 11:19:33 -0400 | [diff] [blame] | 203 | GrBackendTexture texturesToDelete[4]{ |
| 204 | yuvTextures[0], |
| 205 | yuvTextures[1], |
| 206 | yuvTextures[2], |
| 207 | resultTexture, |
| 208 | }; |
| 209 | this->deleteBackendTextures(context, texturesToDelete, 4); |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 210 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 214 | sk_sp<SkImage> fRGBImage; |
| 215 | SkBitmap fYUVBmps[3]; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 216 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 217 | static constexpr SkScalar kPad = 10.0f; |
| 218 | static constexpr int kBmpSize = 32; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 219 | |
| 220 | typedef GM INHERITED; |
| 221 | }; |
| 222 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 223 | DEF_GM(return new ImageFromYUVTextures;) |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 224 | } |