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 | |
| 10 | #include "gm.h" |
| 11 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 12 | #include "GrContext.h" |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
robertphillips | 87f15c8 | 2016-05-20 11:14:33 -0700 | [diff] [blame] | 14 | #include "GrGpu.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 15 | #include "GrTest.h" |
| 16 | #include "SkBitmap.h" |
| 17 | #include "SkGradientShader.h" |
| 18 | #include "SkImage.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 19 | #include "SkTo.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 20 | |
| 21 | namespace skiagm { |
| 22 | class ImageFromYUVTextures : public GM { |
| 23 | public: |
| 24 | ImageFromYUVTextures() { |
| 25 | this->setBGColor(0xFFFFFFFF); |
| 26 | } |
| 27 | |
| 28 | protected: |
| 29 | SkString onShortName() override { |
| 30 | return SkString("image_from_yuv_textures"); |
| 31 | } |
| 32 | |
| 33 | SkISize onISize() override { |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 34 | return SkISize::Make(50, 175); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void onOnceBeforeDraw() override { |
| 38 | // We create an RGB bitmap and then extract YUV bmps where the U and V bitmaps are |
| 39 | // subsampled by 2 in both dimensions. |
| 40 | SkPaint paint; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 41 | constexpr SkColor kColors[] = |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 42 | { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 43 | paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(0,0), kBmpSize / 2.f, kColors, |
| 44 | nullptr, SK_ARRAY_COUNT(kColors), |
| 45 | SkShader::kMirror_TileMode)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 46 | SkBitmap rgbBmp; |
| 47 | rgbBmp.allocN32Pixels(kBmpSize, kBmpSize, true); |
| 48 | SkCanvas canvas(rgbBmp); |
| 49 | canvas.drawPaint(paint); |
| 50 | SkPMColor* rgbColors = static_cast<SkPMColor*>(rgbBmp.getPixels()); |
| 51 | |
| 52 | SkImageInfo yinfo = SkImageInfo::MakeA8(kBmpSize, kBmpSize); |
| 53 | fYUVBmps[0].allocPixels(yinfo); |
| 54 | SkImageInfo uinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2); |
| 55 | fYUVBmps[1].allocPixels(uinfo); |
| 56 | SkImageInfo vinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2); |
| 57 | fYUVBmps[2].allocPixels(vinfo); |
| 58 | unsigned char* yPixels; |
| 59 | signed char* uvPixels[2]; |
| 60 | yPixels = static_cast<unsigned char*>(fYUVBmps[0].getPixels()); |
| 61 | uvPixels[0] = static_cast<signed char*>(fYUVBmps[1].getPixels()); |
jvanverth | 3e5f555 | 2015-07-16 07:46:07 -0700 | [diff] [blame] | 62 | uvPixels[1] = static_cast<signed char*>(fYUVBmps[2].getPixels()); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 63 | |
| 64 | // Here we encode using the NTC encoding (even though we will draw it with all the supported |
| 65 | // yuv color spaces when converted back to RGB) |
| 66 | for (int i = 0; i < kBmpSize * kBmpSize; ++i) { |
| 67 | yPixels[i] = static_cast<unsigned char>(0.299f * SkGetPackedR32(rgbColors[i]) + |
| 68 | 0.587f * SkGetPackedG32(rgbColors[i]) + |
| 69 | 0.114f * SkGetPackedB32(rgbColors[i])); |
| 70 | } |
| 71 | for (int j = 0; j < kBmpSize / 2; ++j) { |
| 72 | for (int i = 0; i < kBmpSize / 2; ++i) { |
| 73 | // Average together 4 pixels of RGB. |
| 74 | int rgb[] = { 0, 0, 0 }; |
| 75 | for (int y = 0; y < 2; ++y) { |
| 76 | for (int x = 0; x < 2; ++x) { |
| 77 | int rgbIndex = (2 * j + y) * kBmpSize + 2 * i + x; |
| 78 | rgb[0] += SkGetPackedR32(rgbColors[rgbIndex]); |
| 79 | rgb[1] += SkGetPackedG32(rgbColors[rgbIndex]); |
| 80 | rgb[2] += SkGetPackedB32(rgbColors[rgbIndex]); |
| 81 | } |
| 82 | } |
| 83 | for (int c = 0; c < 3; ++c) { |
| 84 | rgb[c] /= 4; |
| 85 | } |
| 86 | int uvIndex = j * kBmpSize / 2 + i; |
| 87 | uvPixels[0][uvIndex] = static_cast<signed char>( |
| 88 | ((-38 * rgb[0] - 74 * rgb[1] + 112 * rgb[2] + 128) >> 8) + 128); |
| 89 | uvPixels[1][uvIndex] = static_cast<signed char>( |
| 90 | ((112 * rgb[0] - 94 * rgb[1] - 18 * rgb[2] + 128) >> 8) + 128); |
| 91 | } |
| 92 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 93 | fRGBImage = SkImage::MakeRasterCopy(SkPixmap(rgbBmp.info(), rgbColors, rgbBmp.rowBytes())); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 96 | void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) { |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 97 | if (context->abandoned()) { |
Robert Phillips | 6d36370 | 2018-06-25 08:53:09 -0400 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 101 | GrGpu* gpu = context->contextPriv().getGpu(); |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 102 | if (!gpu) { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 106 | for (int i = 0; i < 3; ++i) { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 107 | SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes())); |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 108 | yuvTextures[i] = gpu->createTestingOnlyBackendTexture(fYUVBmps[i].getPixels(), |
| 109 | fYUVBmps[i].width(), |
| 110 | fYUVBmps[i].height(), |
| 111 | kAlpha_8_GrPixelConfig, |
| 112 | false, GrMipMapped::kNo); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 113 | } |
| 114 | context->resetContext(); |
| 115 | } |
| 116 | |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 117 | void deleteYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) { |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 118 | if (context->abandoned()) { |
Robert Phillips | 6d36370 | 2018-06-25 08:53:09 -0400 | [diff] [blame] | 119 | return; |
| 120 | } |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 121 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 122 | GrGpu* gpu = context->contextPriv().getGpu(); |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 123 | if (!gpu) { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
Greg Daniel | 4231401 | 2018-04-23 10:57:37 -0400 | [diff] [blame] | 127 | context->flush(); |
| 128 | gpu->testingOnly_flushGpuAndSync(); |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 129 | for (int i = 0; i < 3; ++i) { |
Robert Phillips | 579f094 | 2018-01-08 14:53:35 -0500 | [diff] [blame] | 130 | if (yuvTextures[i].isValid()) { |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 131 | gpu->deleteTestingOnlyBackendTexture(yuvTextures[i]); |
Robert Phillips | 579f094 | 2018-01-08 14:53:35 -0500 | [diff] [blame] | 132 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 133 | } |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 134 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 135 | context->resetContext(); |
| 136 | } |
| 137 | |
| 138 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 139 | GrContext* context = canvas->getGrContext(); |
| 140 | if (!context) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 141 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 145 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 146 | constexpr SkScalar kPad = 10.f; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 147 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 148 | SkTArray<sk_sp<SkImage>> images; |
| 149 | images.push_back(fRGBImage); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 150 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 151 | GrBackendTexture yuvTextures[3]; |
| 152 | this->createYUVTextures(context, yuvTextures); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 153 | images.push_back(SkImage::MakeFromYUVTexturesCopy(context, |
| 154 | static_cast<SkYUVColorSpace>(space), |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 155 | yuvTextures, |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 156 | kTopLeft_GrSurfaceOrigin)); |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 157 | this->deleteYUVTextures(context, yuvTextures); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 158 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 159 | for (int i = 0; i < images.count(); ++ i) { |
| 160 | SkScalar y = (i + 1) * kPad + i * fYUVBmps[0].height(); |
| 161 | SkScalar x = kPad; |
| 162 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 163 | canvas->drawImage(images[i].get(), x, y); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 168 | sk_sp<SkImage> fRGBImage; |
| 169 | SkBitmap fYUVBmps[3]; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 170 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 171 | static constexpr int kBmpSize = 32; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 172 | |
| 173 | typedef GM INHERITED; |
| 174 | }; |
| 175 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 176 | DEF_GM(return new ImageFromYUVTextures;) |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 177 | } |