blob: 53a828ab27212cbfed96fc7adfaac798d166873a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkCanvas.h"
13#include "include/core/SkColor.h"
14#include "include/core/SkColorFilter.h"
15#include "include/core/SkColorPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#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 Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/effects/SkGradientShader.h"
29#include "include/gpu/GrBackendSurface.h"
30#include "include/gpu/GrContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include "include/gpu/GrTypes.h"
32#include "include/private/GrTypesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "include/private/SkTo.h"
34#include "src/gpu/GrContextPriv.h"
35#include "src/gpu/GrGpu.h"
bsalomon993a4212015-05-29 11:37:25 -070036
Ben Wagner7fde8e12019-05-01 17:28:53 -040037class GrRenderTargetContext;
38
Robert Phillips0a22ba82019-03-06 12:36:47 -050039static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
40 static const float kJPEGConversionMatrix[20] = {
Mike Reede869a1e2019-04-30 12:18:54 -040041 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
42 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
43 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
Robert Phillips0a22ba82019-03-06 12:36:47 -050044 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
45 };
46
Mike Reede869a1e2019-04-30 12:18:54 -040047 return SkColorFilters::Matrix(kJPEGConversionMatrix);
Robert Phillips0a22ba82019-03-06 12:36:47 -050048}
49
bsalomon993a4212015-05-29 11:37:25 -070050namespace skiagm {
Chris Dalton3a778372019-02-07 15:23:36 -070051class ImageFromYUVTextures : public GpuGM {
bsalomon993a4212015-05-29 11:37:25 -070052public:
53 ImageFromYUVTextures() {
54 this->setBGColor(0xFFFFFFFF);
55 }
56
57protected:
58 SkString onShortName() override {
59 return SkString("image_from_yuv_textures");
60 }
61
62 SkISize onISize() override {
Robert Phillips0a22ba82019-03-06 12:36:47 -050063 return SkISize::Make(kBmpSize + 2 * kPad, 390);
bsalomon993a4212015-05-29 11:37:25 -070064 }
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;
mtkleindbfd7ab2016-09-01 11:24:54 -070070 constexpr SkColor kColors[] =
bsalomon993a4212015-05-29 11:37:25 -070071 { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE };
reed1a9b9642016-03-13 14:13:58 -070072 paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(0,0), kBmpSize / 2.f, kColors,
73 nullptr, SK_ARRAY_COUNT(kColors),
Mike Reedfae8fce2019-04-03 10:27:45 -040074 SkTileMode::kMirror));
bsalomon993a4212015-05-29 11:37:25 -070075 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
81 SkImageInfo yinfo = SkImageInfo::MakeA8(kBmpSize, kBmpSize);
82 fYUVBmps[0].allocPixels(yinfo);
83 SkImageInfo uinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2);
84 fYUVBmps[1].allocPixels(uinfo);
85 SkImageInfo vinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2);
86 fYUVBmps[2].allocPixels(vinfo);
87 unsigned char* yPixels;
88 signed char* uvPixels[2];
89 yPixels = static_cast<unsigned char*>(fYUVBmps[0].getPixels());
90 uvPixels[0] = static_cast<signed char*>(fYUVBmps[1].getPixels());
jvanverth3e5f5552015-07-16 07:46:07 -070091 uvPixels[1] = static_cast<signed char*>(fYUVBmps[2].getPixels());
bsalomon993a4212015-05-29 11:37:25 -070092
Robert Phillips0a22ba82019-03-06 12:36:47 -050093 // Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though
94 // we will draw it with all the supported yuv color spaces when converted back to RGB
bsalomon993a4212015-05-29 11:37:25 -070095 for (int i = 0; i < kBmpSize * kBmpSize; ++i) {
96 yPixels[i] = static_cast<unsigned char>(0.299f * SkGetPackedR32(rgbColors[i]) +
97 0.587f * SkGetPackedG32(rgbColors[i]) +
98 0.114f * SkGetPackedB32(rgbColors[i]));
99 }
100 for (int j = 0; j < kBmpSize / 2; ++j) {
101 for (int i = 0; i < kBmpSize / 2; ++i) {
102 // Average together 4 pixels of RGB.
103 int rgb[] = { 0, 0, 0 };
104 for (int y = 0; y < 2; ++y) {
105 for (int x = 0; x < 2; ++x) {
106 int rgbIndex = (2 * j + y) * kBmpSize + 2 * i + x;
107 rgb[0] += SkGetPackedR32(rgbColors[rgbIndex]);
108 rgb[1] += SkGetPackedG32(rgbColors[rgbIndex]);
109 rgb[2] += SkGetPackedB32(rgbColors[rgbIndex]);
110 }
111 }
112 for (int c = 0; c < 3; ++c) {
113 rgb[c] /= 4;
114 }
115 int uvIndex = j * kBmpSize / 2 + i;
116 uvPixels[0][uvIndex] = static_cast<signed char>(
117 ((-38 * rgb[0] - 74 * rgb[1] + 112 * rgb[2] + 128) >> 8) + 128);
118 uvPixels[1][uvIndex] = static_cast<signed char>(
119 ((112 * rgb[0] - 94 * rgb[1] - 18 * rgb[2] + 128) >> 8) + 128);
120 }
121 }
reed9ce9d672016-03-17 10:51:11 -0700122 fRGBImage = SkImage::MakeRasterCopy(SkPixmap(rgbBmp.info(), rgbColors, rgbBmp.rowBytes()));
bsalomon993a4212015-05-29 11:37:25 -0700123 }
124
Robert Phillipsc25db632017-12-13 09:22:45 -0500125 void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500126 GrGpu* gpu = context->priv().getGpu();
jvanverth672bb7f2015-07-13 07:19:57 -0700127 if (!gpu) {
bsalomon993a4212015-05-29 11:37:25 -0700128 return;
129 }
130
bsalomon993a4212015-05-29 11:37:25 -0700131 for (int i = 0; i < 3; ++i) {
bsalomon993a4212015-05-29 11:37:25 -0700132 SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes()));
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400133 yuvTextures[i] = gpu->createTestingOnlyBackendTexture(fYUVBmps[i].width(),
Robert Phillipsc25db632017-12-13 09:22:45 -0500134 fYUVBmps[i].height(),
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400135 kAlpha_8_SkColorType,
136 GrMipMapped::kNo,
137 GrRenderable::kNo,
138 fYUVBmps[i].getPixels());
bsalomon993a4212015-05-29 11:37:25 -0700139 }
140 context->resetContext();
141 }
142
Brian Salomon279f8732018-09-18 11:19:33 -0400143 void createResultTexture(GrContext* context, int width, int height,
144 GrBackendTexture* resultTexture) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400145 *resultTexture = context->createBackendTexture(
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400146 width, height, kRGBA_8888_SkColorType, GrMipMapped::kNo, GrRenderable::kYes);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400147
148 context->resetContext();
149 }
150
Brian Salomon279f8732018-09-18 11:19:33 -0400151 void deleteBackendTextures(GrContext* context, GrBackendTexture textures[], int n) {
Khushalc421ca12018-06-26 14:38:34 -0700152 if (context->abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -0400153 return;
154 }
jvanverth672bb7f2015-07-13 07:19:57 -0700155
Robert Phillips9da87e02019-02-04 13:26:26 -0500156 GrGpu* gpu = context->priv().getGpu();
jvanverth672bb7f2015-07-13 07:19:57 -0700157 if (!gpu) {
bsalomon993a4212015-05-29 11:37:25 -0700158 return;
159 }
160
Greg Daniel42314012018-04-23 10:57:37 -0400161 context->flush();
162 gpu->testingOnly_flushGpuAndSync();
Brian Salomon279f8732018-09-18 11:19:33 -0400163 for (int i = 0; i < n; ++i) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400164 context->deleteBackendTexture(textures[i]);
bsalomon993a4212015-05-29 11:37:25 -0700165 }
jvanverth672bb7f2015-07-13 07:19:57 -0700166
bsalomon993a4212015-05-29 11:37:25 -0700167 context->resetContext();
168 }
169
Chris Dalton3a778372019-02-07 15:23:36 -0700170 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500171 // draw the original
172 SkScalar yOffset = kPad;
173 canvas->drawImage(fRGBImage.get(), kPad, yOffset);
174 yOffset += kBmpSize + kPad;
bsalomon993a4212015-05-29 11:37:25 -0700175
bsalomon993a4212015-05-29 11:37:25 -0700176 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
Robert Phillipsc25db632017-12-13 09:22:45 -0500177 GrBackendTexture yuvTextures[3];
178 this->createYUVTextures(context, yuvTextures);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500179 auto image = SkImage::MakeFromYUVTexturesCopy(context,
180 static_cast<SkYUVColorSpace>(space),
181 yuvTextures,
182 kTopLeft_GrSurfaceOrigin);
Brian Salomon279f8732018-09-18 11:19:33 -0400183 this->deleteBackendTextures(context, yuvTextures, 3);
bsalomon993a4212015-05-29 11:37:25 -0700184
Robert Phillips0a22ba82019-03-06 12:36:47 -0500185 SkPaint paint;
186 if (kIdentity_SkYUVColorSpace == space) {
187 // The identity color space needs post-processing to appear correct
188 paint.setColorFilter(yuv_to_rgb_colorfilter());
189 }
190
191 canvas->drawImage(image.get(), kPad, yOffset, &paint);
192 yOffset += kBmpSize + kPad;
bsalomon993a4212015-05-29 11:37:25 -0700193 }
Brian Salomond2fcfb52018-09-17 21:57:11 -0400194
Robert Phillips0a22ba82019-03-06 12:36:47 -0500195 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
Brian Salomond2fcfb52018-09-17 21:57:11 -0400196 GrBackendTexture yuvTextures[3];
Brian Salomon279f8732018-09-18 11:19:33 -0400197 GrBackendTexture resultTexture;
Brian Salomond2fcfb52018-09-17 21:57:11 -0400198 this->createYUVTextures(context, yuvTextures);
Brian Salomon279f8732018-09-18 11:19:33 -0400199 this->createResultTexture(
200 context, yuvTextures[0].width(), yuvTextures[0].height(), &resultTexture);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500201 auto image = SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
202 context,
203 static_cast<SkYUVColorSpace>(space),
204 yuvTextures,
205 kTopLeft_GrSurfaceOrigin,
206 resultTexture);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400207
Robert Phillips0a22ba82019-03-06 12:36:47 -0500208 SkPaint paint;
209 if (kIdentity_SkYUVColorSpace == space) {
210 // The identity color space needs post-processing to appear correct
211 paint.setColorFilter(yuv_to_rgb_colorfilter());
212 }
213 canvas->drawImage(image.get(), kPad, yOffset, &paint);
214 yOffset += kBmpSize + kPad;
Brian Salomond2fcfb52018-09-17 21:57:11 -0400215
Brian Salomon279f8732018-09-18 11:19:33 -0400216 GrBackendTexture texturesToDelete[4]{
217 yuvTextures[0],
218 yuvTextures[1],
219 yuvTextures[2],
220 resultTexture,
221 };
222 this->deleteBackendTextures(context, texturesToDelete, 4);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400223 }
bsalomon993a4212015-05-29 11:37:25 -0700224 }
225
226private:
reed9ce9d672016-03-17 10:51:11 -0700227 sk_sp<SkImage> fRGBImage;
228 SkBitmap fYUVBmps[3];
bsalomon993a4212015-05-29 11:37:25 -0700229
Robert Phillips0a22ba82019-03-06 12:36:47 -0500230 static constexpr SkScalar kPad = 10.0f;
231 static constexpr int kBmpSize = 32;
bsalomon993a4212015-05-29 11:37:25 -0700232
233 typedef GM INHERITED;
234};
235
halcanary385fe4d2015-08-26 13:07:48 -0700236DEF_GM(return new ImageFromYUVTextures;)
bsalomon993a4212015-05-29 11:37:25 -0700237}