blob: 7ba86c0995f750f33a73703f99cc6973a6f9639a [file] [log] [blame]
bsalomon993a4212015-05-29 11:37:25 -07001/*
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
Herb Derbyd3895d82018-09-04 13:27:00 -040012#include "GrBackendSurface.h"
bsalomon993a4212015-05-29 11:37:25 -070013#include "GrContext.h"
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050014#include "GrContextPriv.h"
robertphillips87f15c82016-05-20 11:14:33 -070015#include "GrGpu.h"
bsalomon993a4212015-05-29 11:37:25 -070016#include "SkBitmap.h"
17#include "SkGradientShader.h"
18#include "SkImage.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040019#include "SkTo.h"
bsalomon993a4212015-05-29 11:37:25 -070020
21namespace skiagm {
22class ImageFromYUVTextures : public GM {
23public:
24 ImageFromYUVTextures() {
25 this->setBGColor(0xFFFFFFFF);
26 }
27
28protected:
29 SkString onShortName() override {
30 return SkString("image_from_yuv_textures");
31 }
32
33 SkISize onISize() override {
Brian Salomond2fcfb52018-09-17 21:57:11 -040034 return SkISize::Make(50, 300);
bsalomon993a4212015-05-29 11:37:25 -070035 }
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;
mtkleindbfd7ab2016-09-01 11:24:54 -070041 constexpr SkColor kColors[] =
bsalomon993a4212015-05-29 11:37:25 -070042 { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE };
reed1a9b9642016-03-13 14:13:58 -070043 paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(0,0), kBmpSize / 2.f, kColors,
44 nullptr, SK_ARRAY_COUNT(kColors),
45 SkShader::kMirror_TileMode));
bsalomon993a4212015-05-29 11:37:25 -070046 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());
jvanverth3e5f5552015-07-16 07:46:07 -070062 uvPixels[1] = static_cast<signed char*>(fYUVBmps[2].getPixels());
bsalomon993a4212015-05-29 11:37:25 -070063
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 }
reed9ce9d672016-03-17 10:51:11 -070093 fRGBImage = SkImage::MakeRasterCopy(SkPixmap(rgbBmp.info(), rgbColors, rgbBmp.rowBytes()));
bsalomon993a4212015-05-29 11:37:25 -070094 }
95
Robert Phillipsc25db632017-12-13 09:22:45 -050096 void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) {
Khushalc421ca12018-06-26 14:38:34 -070097 if (context->abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -040098 return;
99 }
100
Robert Phillips9da87e02019-02-04 13:26:26 -0500101 GrGpu* gpu = context->priv().getGpu();
jvanverth672bb7f2015-07-13 07:19:57 -0700102 if (!gpu) {
bsalomon993a4212015-05-29 11:37:25 -0700103 return;
104 }
105
bsalomon993a4212015-05-29 11:37:25 -0700106 for (int i = 0; i < 3; ++i) {
bsalomon993a4212015-05-29 11:37:25 -0700107 SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes()));
Robert Phillipsc25db632017-12-13 09:22:45 -0500108 yuvTextures[i] = gpu->createTestingOnlyBackendTexture(fYUVBmps[i].getPixels(),
109 fYUVBmps[i].width(),
110 fYUVBmps[i].height(),
Robert Phillips646f6372018-09-25 09:31:10 -0400111 GrColorType::kAlpha_8,
Robert Phillipsc25db632017-12-13 09:22:45 -0500112 false, GrMipMapped::kNo);
bsalomon993a4212015-05-29 11:37:25 -0700113 }
114 context->resetContext();
115 }
116
Brian Salomon279f8732018-09-18 11:19:33 -0400117 void createResultTexture(GrContext* context, int width, int height,
118 GrBackendTexture* resultTexture) {
Brian Salomond2fcfb52018-09-17 21:57:11 -0400119 if (context->abandoned()) {
120 return;
121 }
122
Robert Phillips9da87e02019-02-04 13:26:26 -0500123 GrGpu* gpu = context->priv().getGpu();
Brian Salomond2fcfb52018-09-17 21:57:11 -0400124 if (!gpu) {
125 return;
126 }
127
Brian Salomon279f8732018-09-18 11:19:33 -0400128 *resultTexture = gpu->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -0400129 nullptr, width, height, GrColorType::kRGBA_8888, true, GrMipMapped::kNo);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400130
131 context->resetContext();
132 }
133
Brian Salomon279f8732018-09-18 11:19:33 -0400134 void deleteBackendTextures(GrContext* context, GrBackendTexture textures[], int n) {
Khushalc421ca12018-06-26 14:38:34 -0700135 if (context->abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -0400136 return;
137 }
jvanverth672bb7f2015-07-13 07:19:57 -0700138
Robert Phillips9da87e02019-02-04 13:26:26 -0500139 GrGpu* gpu = context->priv().getGpu();
jvanverth672bb7f2015-07-13 07:19:57 -0700140 if (!gpu) {
bsalomon993a4212015-05-29 11:37:25 -0700141 return;
142 }
143
Greg Daniel42314012018-04-23 10:57:37 -0400144 context->flush();
145 gpu->testingOnly_flushGpuAndSync();
Brian Salomon279f8732018-09-18 11:19:33 -0400146 for (int i = 0; i < n; ++i) {
147 if (textures[i].isValid()) {
148 gpu->deleteTestingOnlyBackendTexture(textures[i]);
Robert Phillips579f0942018-01-08 14:53:35 -0500149 }
bsalomon993a4212015-05-29 11:37:25 -0700150 }
jvanverth672bb7f2015-07-13 07:19:57 -0700151
bsalomon993a4212015-05-29 11:37:25 -0700152 context->resetContext();
153 }
154
155 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -0700156 GrContext* context = canvas->getGrContext();
157 if (!context) {
halcanary2a243382015-09-09 08:16:41 -0700158 skiagm::GM::DrawGpuOnlyMessage(canvas);
bsalomon993a4212015-05-29 11:37:25 -0700159 return;
160 }
161
bsalomon993a4212015-05-29 11:37:25 -0700162
mtkleindbfd7ab2016-09-01 11:24:54 -0700163 constexpr SkScalar kPad = 10.f;
bsalomon993a4212015-05-29 11:37:25 -0700164
reed9ce9d672016-03-17 10:51:11 -0700165 SkTArray<sk_sp<SkImage>> images;
166 images.push_back(fRGBImage);
bsalomon993a4212015-05-29 11:37:25 -0700167 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
Robert Phillipsc25db632017-12-13 09:22:45 -0500168 GrBackendTexture yuvTextures[3];
169 this->createYUVTextures(context, yuvTextures);
reed9ce9d672016-03-17 10:51:11 -0700170 images.push_back(SkImage::MakeFromYUVTexturesCopy(context,
171 static_cast<SkYUVColorSpace>(space),
Brian Salomon6a426c12018-03-15 12:16:02 -0400172 yuvTextures,
reed9ce9d672016-03-17 10:51:11 -0700173 kTopLeft_GrSurfaceOrigin));
Brian Salomon279f8732018-09-18 11:19:33 -0400174 this->deleteBackendTextures(context, yuvTextures, 3);
bsalomon993a4212015-05-29 11:37:25 -0700175 }
bsalomon993a4212015-05-29 11:37:25 -0700176 for (int i = 0; i < images.count(); ++ i) {
177 SkScalar y = (i + 1) * kPad + i * fYUVBmps[0].height();
178 SkScalar x = kPad;
179
reed9ce9d672016-03-17 10:51:11 -0700180 canvas->drawImage(images[i].get(), x, y);
bsalomon993a4212015-05-29 11:37:25 -0700181 }
Brian Salomond2fcfb52018-09-17 21:57:11 -0400182
183 sk_sp<SkImage> image;
184 for (int space = kJPEG_SkYUVColorSpace, i = images.count();
185 space <= kLastEnum_SkYUVColorSpace; ++space, ++i) {
186 GrBackendTexture yuvTextures[3];
Brian Salomon279f8732018-09-18 11:19:33 -0400187 GrBackendTexture resultTexture;
Brian Salomond2fcfb52018-09-17 21:57:11 -0400188 this->createYUVTextures(context, yuvTextures);
Brian Salomon279f8732018-09-18 11:19:33 -0400189 this->createResultTexture(
190 context, yuvTextures[0].width(), yuvTextures[0].height(), &resultTexture);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400191 image = SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
192 context,
193 static_cast<SkYUVColorSpace>(space),
194 yuvTextures,
195 kTopLeft_GrSurfaceOrigin,
Brian Salomon279f8732018-09-18 11:19:33 -0400196 resultTexture);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400197
198 SkScalar y = (i + 1) * kPad + i * fYUVBmps[0].height();
199 SkScalar x = kPad;
200
201 canvas->drawImage(image.get(), x, y);
Brian Salomon279f8732018-09-18 11:19:33 -0400202 GrBackendTexture texturesToDelete[4]{
203 yuvTextures[0],
204 yuvTextures[1],
205 yuvTextures[2],
206 resultTexture,
207 };
208 this->deleteBackendTextures(context, texturesToDelete, 4);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400209 }
bsalomon993a4212015-05-29 11:37:25 -0700210 }
211
212private:
reed9ce9d672016-03-17 10:51:11 -0700213 sk_sp<SkImage> fRGBImage;
214 SkBitmap fYUVBmps[3];
bsalomon993a4212015-05-29 11:37:25 -0700215
mtkleindbfd7ab2016-09-01 11:24:54 -0700216 static constexpr int kBmpSize = 32;
bsalomon993a4212015-05-29 11:37:25 -0700217
218 typedef GM INHERITED;
219};
220
halcanary385fe4d2015-08-26 13:07:48 -0700221DEF_GM(return new ImageFromYUVTextures;)
bsalomon993a4212015-05-29 11:37:25 -0700222}