bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2015 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | // This test only works with the GPU backend. |
| 10 | |
| 11 | #include "gm.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU |
| 14 | |
| 15 | #include "GrContext.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 16 | #include "GrTest.h" |
| 17 | #include "SkBitmap.h" |
| 18 | #include "SkGradientShader.h" |
| 19 | #include "SkImage.h" |
| 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 { |
| 34 | return SkISize::Make(50, 135); |
| 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; |
| 41 | static const SkColor kColors[] = |
| 42 | { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE }; |
| 43 | paint.setShader(SkGradientShader::CreateRadial(SkPoint::Make(0,0), kBmpSize / 2.f, kColors, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 44 | nullptr, SK_ARRAY_COUNT(kColors), |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 45 | SkShader::kMirror_TileMode))->unref(); |
| 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 | } |
| 93 | fRGBImage.reset(SkImage::NewRasterCopy(rgbBmp.info(), rgbColors, rgbBmp.rowBytes())); |
| 94 | } |
| 95 | |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 96 | void createYUVTextures(GrContext* context, GrBackendObject yuvHandles[3]) { |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 97 | const GrGpu* gpu = context->getGpu(); |
| 98 | if (!gpu) { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 102 | for (int i = 0; i < 3; ++i) { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 103 | SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes())); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 104 | yuvHandles[i] = gpu->createTestingOnlyBackendTexture(fYUVBmps[i].getPixels(), |
| 105 | fYUVBmps[i].width(), |
| 106 | fYUVBmps[i].height(), |
| 107 | kAlpha_8_GrPixelConfig); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 108 | } |
| 109 | context->resetContext(); |
| 110 | } |
| 111 | |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 112 | void deleteYUVTextures(GrContext* context, const GrBackendObject yuvHandles[3]) { |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 113 | |
| 114 | const GrGpu* gpu = context->getGpu(); |
| 115 | if (!gpu) { |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 119 | for (int i = 0; i < 3; ++i) { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 120 | gpu->deleteTestingOnlyBackendTexture(yuvHandles[i]); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 121 | } |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 122 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 123 | context->resetContext(); |
| 124 | } |
| 125 | |
| 126 | void onDraw(SkCanvas* canvas) override { |
| 127 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
| 128 | GrContext* context; |
| 129 | if (!rt || !(context = rt->getContext())) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 130 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 134 | GrBackendObject yuvHandles[3]; |
| 135 | this->createYUVTextures(context, yuvHandles); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 136 | |
| 137 | static const SkScalar kPad = 10.f; |
| 138 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 139 | SkISize sizes[] = { |
| 140 | { fYUVBmps[0].width(), fYUVBmps[0].height()}, |
| 141 | { fYUVBmps[1].width(), fYUVBmps[1].height()}, |
| 142 | { fYUVBmps[2].width(), fYUVBmps[2].height()}, |
| 143 | }; |
| 144 | SkTArray<SkImage*> images; |
| 145 | images.push_back(SkRef(fRGBImage.get())); |
| 146 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { |
| 147 | images.push_back(SkImage::NewFromYUVTexturesCopy(context, |
| 148 | static_cast<SkYUVColorSpace>(space), |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 149 | yuvHandles, sizes, |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 150 | kTopLeft_GrSurfaceOrigin)); |
| 151 | } |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 152 | this->deleteYUVTextures(context, yuvHandles); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 153 | for (int i = 0; i < images.count(); ++ i) { |
| 154 | SkScalar y = (i + 1) * kPad + i * fYUVBmps[0].height(); |
| 155 | SkScalar x = kPad; |
| 156 | |
| 157 | canvas->drawImage(images[i], x, y); |
| 158 | images[i]->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 159 | images[i] = nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | SkAutoTUnref<SkImage> fRGBImage; |
| 165 | SkBitmap fYUVBmps[3]; |
| 166 | |
| 167 | static const int kBmpSize = 32; |
| 168 | |
| 169 | typedef GM INHERITED; |
| 170 | }; |
| 171 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 172 | DEF_GM(return new ImageFromYUVTextures;) |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | #endif |