blob: d4b96ec38a8d4f4a1afed2fcfb968c111fa8ed84 [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"
bsalomon993a4212015-05-29 11:37:25 -070034
Ben Wagner7fde8e12019-05-01 17:28:53 -040035class GrRenderTargetContext;
36
Robert Phillips0a22ba82019-03-06 12:36:47 -050037static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
38 static const float kJPEGConversionMatrix[20] = {
Mike Reede869a1e2019-04-30 12:18:54 -040039 1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
40 1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
41 1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
Robert Phillips0a22ba82019-03-06 12:36:47 -050042 0.0f, 0.0f, 0.0f, 1.0f, 0.0f
43 };
44
Mike Reede869a1e2019-04-30 12:18:54 -040045 return SkColorFilters::Matrix(kJPEGConversionMatrix);
Robert Phillips0a22ba82019-03-06 12:36:47 -050046}
47
bsalomon993a4212015-05-29 11:37:25 -070048namespace skiagm {
Chris Dalton3a778372019-02-07 15:23:36 -070049class ImageFromYUVTextures : public GpuGM {
bsalomon993a4212015-05-29 11:37:25 -070050public:
51 ImageFromYUVTextures() {
52 this->setBGColor(0xFFFFFFFF);
53 }
54
55protected:
56 SkString onShortName() override {
57 return SkString("image_from_yuv_textures");
58 }
59
60 SkISize onISize() override {
Brian Osman2b73e662019-11-01 10:02:24 -040061 // Original image, plus each color space drawn twice
62 int numBitmaps = 2 * (kLastEnum_SkYUVColorSpace + 1) + 1;
63 return SkISize::Make(kBmpSize + 2 * kPad, numBitmaps * (kBmpSize + kPad) + kPad);
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
Robert Phillips00c9f0d2019-08-02 17:17:35 -040081 SkImageInfo yinfo = SkImageInfo::Make(kBmpSize, kBmpSize, kGray_8_SkColorType,
82 kUnpremul_SkAlphaType);
bsalomon993a4212015-05-29 11:37:25 -070083 fYUVBmps[0].allocPixels(yinfo);
Robert Phillips00c9f0d2019-08-02 17:17:35 -040084 SkImageInfo uinfo = SkImageInfo::Make(kBmpSize / 2, kBmpSize / 2, kGray_8_SkColorType,
85 kUnpremul_SkAlphaType);
bsalomon993a4212015-05-29 11:37:25 -070086 fYUVBmps[1].allocPixels(uinfo);
Robert Phillips00c9f0d2019-08-02 17:17:35 -040087 SkImageInfo vinfo = SkImageInfo::Make(kBmpSize / 2, kBmpSize / 2, kGray_8_SkColorType,
88 kUnpremul_SkAlphaType);
bsalomon993a4212015-05-29 11:37:25 -070089 fYUVBmps[2].allocPixels(vinfo);
90 unsigned char* yPixels;
91 signed char* uvPixels[2];
92 yPixels = static_cast<unsigned char*>(fYUVBmps[0].getPixels());
93 uvPixels[0] = static_cast<signed char*>(fYUVBmps[1].getPixels());
jvanverth3e5f5552015-07-16 07:46:07 -070094 uvPixels[1] = static_cast<signed char*>(fYUVBmps[2].getPixels());
bsalomon993a4212015-05-29 11:37:25 -070095
Robert Phillips0a22ba82019-03-06 12:36:47 -050096 // Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though
97 // we will draw it with all the supported yuv color spaces when converted back to RGB
bsalomon993a4212015-05-29 11:37:25 -070098 for (int i = 0; i < kBmpSize * kBmpSize; ++i) {
99 yPixels[i] = static_cast<unsigned char>(0.299f * SkGetPackedR32(rgbColors[i]) +
100 0.587f * SkGetPackedG32(rgbColors[i]) +
101 0.114f * SkGetPackedB32(rgbColors[i]));
102 }
103 for (int j = 0; j < kBmpSize / 2; ++j) {
104 for (int i = 0; i < kBmpSize / 2; ++i) {
105 // Average together 4 pixels of RGB.
106 int rgb[] = { 0, 0, 0 };
107 for (int y = 0; y < 2; ++y) {
108 for (int x = 0; x < 2; ++x) {
109 int rgbIndex = (2 * j + y) * kBmpSize + 2 * i + x;
110 rgb[0] += SkGetPackedR32(rgbColors[rgbIndex]);
111 rgb[1] += SkGetPackedG32(rgbColors[rgbIndex]);
112 rgb[2] += SkGetPackedB32(rgbColors[rgbIndex]);
113 }
114 }
115 for (int c = 0; c < 3; ++c) {
116 rgb[c] /= 4;
117 }
118 int uvIndex = j * kBmpSize / 2 + i;
119 uvPixels[0][uvIndex] = static_cast<signed char>(
120 ((-38 * rgb[0] - 74 * rgb[1] + 112 * rgb[2] + 128) >> 8) + 128);
121 uvPixels[1][uvIndex] = static_cast<signed char>(
122 ((112 * rgb[0] - 94 * rgb[1] - 18 * rgb[2] + 128) >> 8) + 128);
123 }
124 }
reed9ce9d672016-03-17 10:51:11 -0700125 fRGBImage = SkImage::MakeRasterCopy(SkPixmap(rgbBmp.info(), rgbColors, rgbBmp.rowBytes()));
bsalomon993a4212015-05-29 11:37:25 -0700126 }
127
Robert Phillipsc25db632017-12-13 09:22:45 -0500128 void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) {
bsalomon993a4212015-05-29 11:37:25 -0700129 for (int i = 0; i < 3; ++i) {
bsalomon993a4212015-05-29 11:37:25 -0700130 SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes()));
Robert Phillips66944402019-09-30 13:21:25 -0400131 yuvTextures[i] = context->createBackendTexture(&fYUVBmps[i].pixmap(), 1,
132 GrRenderable::kNo, GrProtected::kNo);
bsalomon993a4212015-05-29 11:37:25 -0700133 }
bsalomon993a4212015-05-29 11:37:25 -0700134 }
135
Brian Salomon279f8732018-09-18 11:19:33 -0400136 void createResultTexture(GrContext* context, int width, int height,
137 GrBackendTexture* resultTexture) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400138 *resultTexture = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400139 width, height, kRGBA_8888_SkColorType, SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400140 GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400141 }
142
Brian Salomon279f8732018-09-18 11:19:33 -0400143 void deleteBackendTextures(GrContext* context, GrBackendTexture textures[], int n) {
Khushalc421ca12018-06-26 14:38:34 -0700144 if (context->abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -0400145 return;
146 }
jvanverth672bb7f2015-07-13 07:19:57 -0700147
Robert Phillipscb1adb42019-06-10 15:09:34 -0400148 GrFlushInfo flushInfo;
149 flushInfo.fFlags = kSyncCpu_GrFlushFlag;
150 context->flush(flushInfo);
bsalomon993a4212015-05-29 11:37:25 -0700151
Brian Salomon279f8732018-09-18 11:19:33 -0400152 for (int i = 0; i < n; ++i) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400153 context->deleteBackendTexture(textures[i]);
bsalomon993a4212015-05-29 11:37:25 -0700154 }
bsalomon993a4212015-05-29 11:37:25 -0700155 }
156
Chris Dalton3a778372019-02-07 15:23:36 -0700157 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Robert Phillips0a22ba82019-03-06 12:36:47 -0500158 // draw the original
159 SkScalar yOffset = kPad;
160 canvas->drawImage(fRGBImage.get(), kPad, yOffset);
161 yOffset += kBmpSize + kPad;
bsalomon993a4212015-05-29 11:37:25 -0700162
bsalomon993a4212015-05-29 11:37:25 -0700163 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
Robert Phillipsc25db632017-12-13 09:22:45 -0500164 GrBackendTexture yuvTextures[3];
165 this->createYUVTextures(context, yuvTextures);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500166 auto image = SkImage::MakeFromYUVTexturesCopy(context,
167 static_cast<SkYUVColorSpace>(space),
168 yuvTextures,
169 kTopLeft_GrSurfaceOrigin);
Brian Salomon279f8732018-09-18 11:19:33 -0400170 this->deleteBackendTextures(context, yuvTextures, 3);
bsalomon993a4212015-05-29 11:37:25 -0700171
Robert Phillips0a22ba82019-03-06 12:36:47 -0500172 SkPaint paint;
173 if (kIdentity_SkYUVColorSpace == space) {
174 // The identity color space needs post-processing to appear correct
175 paint.setColorFilter(yuv_to_rgb_colorfilter());
176 }
177
178 canvas->drawImage(image.get(), kPad, yOffset, &paint);
179 yOffset += kBmpSize + kPad;
bsalomon993a4212015-05-29 11:37:25 -0700180 }
Brian Salomond2fcfb52018-09-17 21:57:11 -0400181
Robert Phillips0a22ba82019-03-06 12:36:47 -0500182 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
Brian Salomond2fcfb52018-09-17 21:57:11 -0400183 GrBackendTexture yuvTextures[3];
Brian Salomon279f8732018-09-18 11:19:33 -0400184 GrBackendTexture resultTexture;
Brian Salomond2fcfb52018-09-17 21:57:11 -0400185 this->createYUVTextures(context, yuvTextures);
Brian Salomon279f8732018-09-18 11:19:33 -0400186 this->createResultTexture(
187 context, yuvTextures[0].width(), yuvTextures[0].height(), &resultTexture);
Robert Phillips0a22ba82019-03-06 12:36:47 -0500188 auto image = SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
189 context,
190 static_cast<SkYUVColorSpace>(space),
191 yuvTextures,
192 kTopLeft_GrSurfaceOrigin,
193 resultTexture);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400194
Robert Phillips0a22ba82019-03-06 12:36:47 -0500195 SkPaint paint;
196 if (kIdentity_SkYUVColorSpace == space) {
197 // The identity color space needs post-processing to appear correct
198 paint.setColorFilter(yuv_to_rgb_colorfilter());
199 }
200 canvas->drawImage(image.get(), kPad, yOffset, &paint);
201 yOffset += kBmpSize + kPad;
Brian Salomond2fcfb52018-09-17 21:57:11 -0400202
Brian Salomon279f8732018-09-18 11:19:33 -0400203 GrBackendTexture texturesToDelete[4]{
204 yuvTextures[0],
205 yuvTextures[1],
206 yuvTextures[2],
207 resultTexture,
208 };
209 this->deleteBackendTextures(context, texturesToDelete, 4);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400210 }
bsalomon993a4212015-05-29 11:37:25 -0700211 }
212
213private:
reed9ce9d672016-03-17 10:51:11 -0700214 sk_sp<SkImage> fRGBImage;
215 SkBitmap fYUVBmps[3];
bsalomon993a4212015-05-29 11:37:25 -0700216
Robert Phillips0a22ba82019-03-06 12:36:47 -0500217 static constexpr SkScalar kPad = 10.0f;
218 static constexpr int kBmpSize = 32;
bsalomon993a4212015-05-29 11:37:25 -0700219
220 typedef GM INHERITED;
221};
222
halcanary385fe4d2015-08-26 13:07:48 -0700223DEF_GM(return new ImageFromYUVTextures;)
bsalomon993a4212015-05-29 11:37:25 -0700224}