blob: 13e0300bbc9059af467b360bd4cdb27b002a7d30 [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
12#if SK_SUPPORT_GPU
13
14#include "GrContext.h"
robertphillips87f15c82016-05-20 11:14:33 -070015#include "GrGpu.h"
bsalomon993a4212015-05-29 11:37:25 -070016#include "GrTest.h"
17#include "SkBitmap.h"
18#include "SkGradientShader.h"
19#include "SkImage.h"
20
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 {
egdaniel3602d4f2016-08-12 11:58:53 -070034 return SkISize::Make(50, 175);
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
bsalomon091f60c2015-11-10 11:54:56 -080096 void createYUVTextures(GrContext* context, GrBackendObject yuvHandles[3]) {
bsalomone63ffef2016-02-05 07:17:34 -080097 GrGpu* gpu = context->getGpu();
jvanverth672bb7f2015-07-13 07:19:57 -070098 if (!gpu) {
bsalomon993a4212015-05-29 11:37:25 -070099 return;
100 }
101
bsalomon993a4212015-05-29 11:37:25 -0700102 for (int i = 0; i < 3; ++i) {
bsalomon993a4212015-05-29 11:37:25 -0700103 SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes()));
bsalomon091f60c2015-11-10 11:54:56 -0800104 yuvHandles[i] = gpu->createTestingOnlyBackendTexture(fYUVBmps[i].getPixels(),
halcanary9d524f22016-03-29 09:03:52 -0700105 fYUVBmps[i].width(),
bsalomon091f60c2015-11-10 11:54:56 -0800106 fYUVBmps[i].height(),
107 kAlpha_8_GrPixelConfig);
bsalomon993a4212015-05-29 11:37:25 -0700108 }
109 context->resetContext();
110 }
111
bsalomon091f60c2015-11-10 11:54:56 -0800112 void deleteYUVTextures(GrContext* context, const GrBackendObject yuvHandles[3]) {
jvanverth672bb7f2015-07-13 07:19:57 -0700113
bsalomone63ffef2016-02-05 07:17:34 -0800114 GrGpu* gpu = context->getGpu();
jvanverth672bb7f2015-07-13 07:19:57 -0700115 if (!gpu) {
bsalomon993a4212015-05-29 11:37:25 -0700116 return;
117 }
118
jvanverth672bb7f2015-07-13 07:19:57 -0700119 for (int i = 0; i < 3; ++i) {
bsalomon091f60c2015-11-10 11:54:56 -0800120 gpu->deleteTestingOnlyBackendTexture(yuvHandles[i]);
bsalomon993a4212015-05-29 11:37:25 -0700121 }
jvanverth672bb7f2015-07-13 07:19:57 -0700122
bsalomon993a4212015-05-29 11:37:25 -0700123 context->resetContext();
124 }
125
126 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -0700127 GrContext* context = canvas->getGrContext();
128 if (!context) {
halcanary2a243382015-09-09 08:16:41 -0700129 skiagm::GM::DrawGpuOnlyMessage(canvas);
bsalomon993a4212015-05-29 11:37:25 -0700130 return;
131 }
132
bsalomon993a4212015-05-29 11:37:25 -0700133
mtkleindbfd7ab2016-09-01 11:24:54 -0700134 constexpr SkScalar kPad = 10.f;
bsalomon993a4212015-05-29 11:37:25 -0700135
bsalomon993a4212015-05-29 11:37:25 -0700136 SkISize sizes[] = {
137 { fYUVBmps[0].width(), fYUVBmps[0].height()},
138 { fYUVBmps[1].width(), fYUVBmps[1].height()},
139 { fYUVBmps[2].width(), fYUVBmps[2].height()},
140 };
reed9ce9d672016-03-17 10:51:11 -0700141 SkTArray<sk_sp<SkImage>> images;
142 images.push_back(fRGBImage);
bsalomon993a4212015-05-29 11:37:25 -0700143 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
egdanielb2df0c22016-05-13 11:30:37 -0700144 GrBackendObject yuvHandles[3];
145 this->createYUVTextures(context, yuvHandles);
reed9ce9d672016-03-17 10:51:11 -0700146 images.push_back(SkImage::MakeFromYUVTexturesCopy(context,
147 static_cast<SkYUVColorSpace>(space),
148 yuvHandles, sizes,
149 kTopLeft_GrSurfaceOrigin));
egdanielb2df0c22016-05-13 11:30:37 -0700150 this->deleteYUVTextures(context, yuvHandles);
bsalomon993a4212015-05-29 11:37:25 -0700151 }
bsalomon993a4212015-05-29 11:37:25 -0700152 for (int i = 0; i < images.count(); ++ i) {
153 SkScalar y = (i + 1) * kPad + i * fYUVBmps[0].height();
154 SkScalar x = kPad;
155
reed9ce9d672016-03-17 10:51:11 -0700156 canvas->drawImage(images[i].get(), x, y);
bsalomon993a4212015-05-29 11:37:25 -0700157 }
158 }
159
160private:
reed9ce9d672016-03-17 10:51:11 -0700161 sk_sp<SkImage> fRGBImage;
162 SkBitmap fYUVBmps[3];
bsalomon993a4212015-05-29 11:37:25 -0700163
mtkleindbfd7ab2016-09-01 11:24:54 -0700164 static constexpr int kBmpSize = 32;
bsalomon993a4212015-05-29 11:37:25 -0700165
166 typedef GM INHERITED;
167};
168
halcanary385fe4d2015-08-26 13:07:48 -0700169DEF_GM(return new ImageFromYUVTextures;)
bsalomon993a4212015-05-29 11:37:25 -0700170}
171
172#endif