blob: a73025502bbac9a16ef4980d85c70077288f296d [file] [log] [blame]
sugoi24dcac22014-07-07 15:09:48 -07001/*
2 * Copyright 2014 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
sugoi24dcac22014-07-07 15:09:48 -070012#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050013#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050014#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContextPriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050016#include "GrTextureProxy.h"
sugoi24dcac22014-07-07 15:09:48 -070017#include "SkBitmap.h"
18#include "SkGr.h"
19#include "SkGradientShader.h"
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050020#include "effects/GrYUVtoRGBEffect.h"
Brian Salomon89527432016-12-16 09:52:16 -050021#include "ops/GrDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040022#include "ops/GrRectOpFactory.h"
sugoi24dcac22014-07-07 15:09:48 -070023
sugoi4ccce7e2015-02-13 13:57:09 -080024#define YSIZE 8
25#define USIZE 4
26#define VSIZE 4
27
sugoi24dcac22014-07-07 15:09:48 -070028namespace skiagm {
29/**
30 * This GM directly exercises GrYUVtoRGBEffect.
31 */
32class YUVtoRGBEffect : public GM {
33public:
34 YUVtoRGBEffect() {
35 this->setBGColor(0xFFFFFFFF);
36 }
37
38protected:
mtklein36352bf2015-03-25 18:17:31 -070039 SkString onShortName() override {
sugoi24dcac22014-07-07 15:09:48 -070040 return SkString("yuv_to_rgb_effect");
41 }
42
mtklein36352bf2015-03-25 18:17:31 -070043 SkISize onISize() override {
rileya13400392015-07-20 15:00:03 -070044 return SkISize::Make(238, 120);
sugoi24dcac22014-07-07 15:09:48 -070045 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 void onOnceBeforeDraw() override {
sugoi4ccce7e2015-02-13 13:57:09 -080048 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
49 fBmp[0].allocPixels(yinfo);
50 SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE);
51 fBmp[1].allocPixels(uinfo);
52 SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE);
53 fBmp[2].allocPixels(vinfo);
sugoi24dcac22014-07-07 15:09:48 -070054 unsigned char* pixels[3];
55 for (int i = 0; i < 3; ++i) {
56 pixels[i] = (unsigned char*)fBmp[i].getPixels();
57 }
58 int color[] = {0, 85, 170};
59 const int limit[] = {255, 0, 255};
60 const int invl[] = {0, 255, 0};
61 const int inc[] = {1, -1, 1};
sugoi4ccce7e2015-02-13 13:57:09 -080062 for (int i = 0; i < 3; ++i) {
63 const size_t nbBytes = fBmp[i].rowBytes() * fBmp[i].height();
64 for (size_t j = 0; j < nbBytes; ++j) {
sugoi24dcac22014-07-07 15:09:48 -070065 pixels[i][j] = (unsigned char)color[i];
66 color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i];
67 }
68 }
69 }
70
mtklein36352bf2015-03-25 18:17:31 -070071 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -040072 GrRenderTargetContext* renderTargetContext =
73 canvas->internal_private_accessTopLayerRenderTargetContext();
74 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -070075 skiagm::GM::DrawGpuOnlyMessage(canvas);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050076 return;
sugoi24dcac22014-07-07 15:09:48 -070077 }
78
robertphillips175dd9b2016-04-28 14:32:04 -070079 GrContext* context = canvas->getGrContext();
80 if (!context) {
sugoi24dcac22014-07-07 15:09:48 -070081 return;
82 }
83
Robert Phillips0bd24dc2018-01-16 08:06:32 -050084 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips2f493142017-03-02 18:18:38 -050085 sk_sp<GrTextureProxy> proxy[3];
bsalomonbcf0a522014-10-08 08:40:09 -070086
Brian Salomon2a4f9832018-03-03 22:43:43 -050087 for (int i = 0; i < 3; ++i) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050088 GrSurfaceDesc desc;
Brian Salomon2a4f9832018-03-03 22:43:43 -050089 desc.fWidth = fBmp[i].width();
90 desc.fHeight = fBmp[i].height();
Brian Osman2b23c4b2018-06-01 12:25:08 -040091 desc.fConfig = SkColorType2GrPixelConfig(fBmp[i].colorType());
Brian Salomon2a4f9832018-03-03 22:43:43 -050092 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050093
Brian Salomon58389b92018-03-07 13:01:25 -050094 proxy[i] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes,
95 fBmp[i].getPixels(), fBmp[i].rowBytes());
Brian Salomon2a4f9832018-03-03 22:43:43 -050096 if (!proxy[i]) {
97 return;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050098 }
sugoi24dcac22014-07-07 15:09:48 -070099 }
100
mtkleindbfd7ab2016-09-01 11:24:54 -0700101 constexpr SkScalar kDrawPad = 10.f;
102 constexpr SkScalar kTestPad = 10.f;
103 constexpr SkScalar kColorSpaceOffset = 36.f;
sugoi4ccce7e2015-02-13 13:57:09 -0800104 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
sugoi24dcac22014-07-07 15:09:48 -0700105
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500106 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -0700107 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
108 SkIntToScalar(fBmp[0].height()));
109 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -0700110
bsalomonbcf0a522014-10-08 08:40:09 -0700111 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
112 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700113
bsalomonbcf0a522014-10-08 08:40:09 -0700114 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
115 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700116
bsalomonbcf0a522014-10-08 08:40:09 -0700117 for (int i = 0; i < 6; ++i) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400118 std::unique_ptr<GrFragmentProcessor> fp(
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500119 GrYUVtoRGBEffect::Make(proxy[indices[i][0]],
120 proxy[indices[i][1]],
121 proxy[indices[i][2]],
122 sizes,
123 static_cast<SkYUVColorSpace>(space),
124 false));
bsalomonbcf0a522014-10-08 08:40:09 -0700125 if (fp) {
Brian Salomon82f44312017-01-11 13:42:54 -0500126 GrPaint grPaint;
127 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
128 grPaint.addColorFragmentProcessor(std::move(fp));
bsalomonbcf0a522014-10-08 08:40:09 -0700129 SkMatrix viewMatrix;
130 viewMatrix.setTranslate(x, y);
Brian Salomonac70f842017-05-08 10:43:33 -0400131 renderTargetContext->priv().testingOnly_addDrawOp(
Robert Phillips7c525e62018-06-12 10:11:12 -0400132 GrRectOpFactory::MakeNonAAFill(context, std::move(grPaint), viewMatrix,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400133 renderRect, GrAAType::kNone));
bsalomonbcf0a522014-10-08 08:40:09 -0700134 }
135 x += renderRect.width() + kTestPad;
136 }
sugoi24dcac22014-07-07 15:09:48 -0700137 }
bsalomonbcf0a522014-10-08 08:40:09 -0700138 }
sugoi24dcac22014-07-07 15:09:48 -0700139
140private:
141 SkBitmap fBmp[3];
142
143 typedef GM INHERITED;
144};
145
halcanary385fe4d2015-08-26 13:07:48 -0700146DEF_GM(return new YUVtoRGBEffect;)
jbaumanb445a572016-06-09 13:24:48 -0700147
148//////////////////////////////////////////////////////////////////////////////
149
150class YUVNV12toRGBEffect : public GM {
151public:
152 YUVNV12toRGBEffect() {
153 this->setBGColor(0xFFFFFFFF);
154 }
155
156protected:
157 SkString onShortName() override {
158 return SkString("yuv_nv12_to_rgb_effect");
159 }
160
161 SkISize onISize() override {
162 return SkISize::Make(48, 120);
163 }
164
165 void onOnceBeforeDraw() override {
166 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
167 fBmp[0].allocPixels(yinfo);
168 SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE);
169 fBmp[1].allocPixels(uvinfo);
170 int color[] = {0, 85, 170};
171 const int limit[] = {255, 0, 255};
172 const int invl[] = {0, 255, 0};
173 const int inc[] = {1, -1, 1};
174
175 {
176 unsigned char* pixels = (unsigned char*)fBmp[0].getPixels();
177 const size_t nbBytes = fBmp[0].rowBytes() * fBmp[0].height();
178 for (size_t j = 0; j < nbBytes; ++j) {
179 pixels[j] = (unsigned char)color[0];
180 color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0];
181 }
182 }
183
184 {
185 for (int y = 0; y < fBmp[1].height(); ++y) {
186 uint32_t* pixels = fBmp[1].getAddr32(0, y);
187 for (int j = 0; j < fBmp[1].width(); ++j) {
188 pixels[j] = SkColorSetARGB(0, color[1], color[2], 0);
189 color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1];
190 color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2];
191 }
192 }
193 }
194 }
195
196 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400197 GrRenderTargetContext* renderTargetContext =
198 canvas->internal_private_accessTopLayerRenderTargetContext();
199 if (!renderTargetContext) {
jbaumanb445a572016-06-09 13:24:48 -0700200 skiagm::GM::DrawGpuOnlyMessage(canvas);
201 return;
202 }
203
204 GrContext* context = canvas->getGrContext();
205 if (!context) {
206 return;
207 }
208
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500209 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips2f493142017-03-02 18:18:38 -0500210 sk_sp<GrTextureProxy> proxy[3];
jbaumanb445a572016-06-09 13:24:48 -0700211
Brian Salomon2a4f9832018-03-03 22:43:43 -0500212 for (int i = 0; i < 3; ++i) {
213 int index = (0 == i) ? 0 : 1;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500214 GrSurfaceDesc desc;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500215 desc.fWidth = fBmp[index].width();
216 desc.fHeight = fBmp[index].height();
Brian Osman2b23c4b2018-06-01 12:25:08 -0400217 desc.fConfig = SkColorType2GrPixelConfig(fBmp[index].colorType());
Brian Salomon2a4f9832018-03-03 22:43:43 -0500218 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500219
Brian Salomon58389b92018-03-07 13:01:25 -0500220 proxy[i] = proxyProvider->createTextureProxy(
221 desc, SkBudgeted::kYes, fBmp[index].getPixels(), fBmp[index].rowBytes());
Brian Salomon2a4f9832018-03-03 22:43:43 -0500222 if (!proxy[i]) {
223 return;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500224 }
jbaumanb445a572016-06-09 13:24:48 -0700225 }
226
mtkleindbfd7ab2016-09-01 11:24:54 -0700227 constexpr SkScalar kDrawPad = 10.f;
228 constexpr SkScalar kTestPad = 10.f;
229 constexpr SkScalar kColorSpaceOffset = 36.f;
jbaumanb445a572016-06-09 13:24:48 -0700230 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
231
232 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
233 SkRect renderRect =
234 SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), SkIntToScalar(fBmp[0].height()));
235 renderRect.outset(kDrawPad, kDrawPad);
236
237 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
238 SkScalar x = kDrawPad + kTestPad;
239
robertphillips28a838e2016-06-23 14:07:00 -0700240 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500241 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500242 auto fp = GrYUVtoRGBEffect::Make(proxy[0], proxy[1], proxy[2], sizes,
243 static_cast<SkYUVColorSpace>(space), true);
jbaumanb445a572016-06-09 13:24:48 -0700244 if (fp) {
245 SkMatrix viewMatrix;
246 viewMatrix.setTranslate(x, y);
Brian Salomonaff329b2017-08-11 09:40:37 -0400247 grPaint.addColorFragmentProcessor(std::move(fp));
Robert Phillips09dfc472017-09-13 15:25:47 -0400248 std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
Robert Phillips7c525e62018-06-12 10:11:12 -0400249 context, std::move(grPaint), viewMatrix, renderRect, GrAAType::kNone));
Robert Phillips09dfc472017-09-13 15:25:47 -0400250 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
jbaumanb445a572016-06-09 13:24:48 -0700251 }
252 }
253 }
254
255private:
256 SkBitmap fBmp[2];
257
258 typedef GM INHERITED;
259};
260
261DEF_GM(return new YUVNV12toRGBEffect;)
sugoi24dcac22014-07-07 15:09:48 -0700262}