blob: 74c11a9a8e70b55ed95f832439f6b1b994ddba50 [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
12#if SK_SUPPORT_GPU
13
14#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050016#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040017#include "GrRenderTargetContextPriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050018#include "GrTextureProxy.h"
sugoi24dcac22014-07-07 15:09:48 -070019#include "SkBitmap.h"
20#include "SkGr.h"
21#include "SkGradientShader.h"
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050022#include "effects/GrYUVtoRGBEffect.h"
Brian Salomon89527432016-12-16 09:52:16 -050023#include "ops/GrDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040024#include "ops/GrRectOpFactory.h"
sugoi24dcac22014-07-07 15:09:48 -070025
sugoi4ccce7e2015-02-13 13:57:09 -080026#define YSIZE 8
27#define USIZE 4
28#define VSIZE 4
29
sugoi24dcac22014-07-07 15:09:48 -070030namespace skiagm {
31/**
32 * This GM directly exercises GrYUVtoRGBEffect.
33 */
34class YUVtoRGBEffect : public GM {
35public:
36 YUVtoRGBEffect() {
37 this->setBGColor(0xFFFFFFFF);
38 }
39
40protected:
mtklein36352bf2015-03-25 18:17:31 -070041 SkString onShortName() override {
sugoi24dcac22014-07-07 15:09:48 -070042 return SkString("yuv_to_rgb_effect");
43 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 SkISize onISize() override {
rileya13400392015-07-20 15:00:03 -070046 return SkISize::Make(238, 120);
sugoi24dcac22014-07-07 15:09:48 -070047 }
48
mtklein36352bf2015-03-25 18:17:31 -070049 void onOnceBeforeDraw() override {
sugoi4ccce7e2015-02-13 13:57:09 -080050 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
51 fBmp[0].allocPixels(yinfo);
52 SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE);
53 fBmp[1].allocPixels(uinfo);
54 SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE);
55 fBmp[2].allocPixels(vinfo);
sugoi24dcac22014-07-07 15:09:48 -070056 unsigned char* pixels[3];
57 for (int i = 0; i < 3; ++i) {
58 pixels[i] = (unsigned char*)fBmp[i].getPixels();
59 }
60 int color[] = {0, 85, 170};
61 const int limit[] = {255, 0, 255};
62 const int invl[] = {0, 255, 0};
63 const int inc[] = {1, -1, 1};
sugoi4ccce7e2015-02-13 13:57:09 -080064 for (int i = 0; i < 3; ++i) {
65 const size_t nbBytes = fBmp[i].rowBytes() * fBmp[i].height();
66 for (size_t j = 0; j < nbBytes; ++j) {
sugoi24dcac22014-07-07 15:09:48 -070067 pixels[i][j] = (unsigned char)color[i];
68 color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i];
69 }
70 }
71 }
72
mtklein36352bf2015-03-25 18:17:31 -070073 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -040074 GrRenderTargetContext* renderTargetContext =
75 canvas->internal_private_accessTopLayerRenderTargetContext();
76 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -070077 skiagm::GM::DrawGpuOnlyMessage(canvas);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050078 return;
sugoi24dcac22014-07-07 15:09:48 -070079 }
80
robertphillips175dd9b2016-04-28 14:32:04 -070081 GrContext* context = canvas->getGrContext();
82 if (!context) {
sugoi24dcac22014-07-07 15:09:48 -070083 return;
84 }
85
Robert Phillips0bd24dc2018-01-16 08:06:32 -050086 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips2f493142017-03-02 18:18:38 -050087 sk_sp<GrTextureProxy> proxy[3];
bsalomonbcf0a522014-10-08 08:40:09 -070088
Brian Salomon2a4f9832018-03-03 22:43:43 -050089 for (int i = 0; i < 3; ++i) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050090 GrSurfaceDesc desc;
Brian Salomon2a4f9832018-03-03 22:43:43 -050091 desc.fWidth = fBmp[i].width();
92 desc.fHeight = fBmp[i].height();
93 desc.fConfig = SkImageInfo2GrPixelConfig(fBmp[i].info(), *context->caps());
94 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050095
Brian Salomon58389b92018-03-07 13:01:25 -050096 proxy[i] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes,
97 fBmp[i].getPixels(), fBmp[i].rowBytes());
Brian Salomon2a4f9832018-03-03 22:43:43 -050098 if (!proxy[i]) {
99 return;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500100 }
sugoi24dcac22014-07-07 15:09:48 -0700101 }
102
mtkleindbfd7ab2016-09-01 11:24:54 -0700103 constexpr SkScalar kDrawPad = 10.f;
104 constexpr SkScalar kTestPad = 10.f;
105 constexpr SkScalar kColorSpaceOffset = 36.f;
sugoi4ccce7e2015-02-13 13:57:09 -0800106 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
sugoi24dcac22014-07-07 15:09:48 -0700107
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500108 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -0700109 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
110 SkIntToScalar(fBmp[0].height()));
111 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -0700112
bsalomonbcf0a522014-10-08 08:40:09 -0700113 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
114 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700115
bsalomonbcf0a522014-10-08 08:40:09 -0700116 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
117 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700118
bsalomonbcf0a522014-10-08 08:40:09 -0700119 for (int i = 0; i < 6; ++i) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400120 std::unique_ptr<GrFragmentProcessor> fp(
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500121 GrYUVtoRGBEffect::Make(proxy[indices[i][0]],
122 proxy[indices[i][1]],
123 proxy[indices[i][2]],
124 sizes,
125 static_cast<SkYUVColorSpace>(space),
126 false));
bsalomonbcf0a522014-10-08 08:40:09 -0700127 if (fp) {
Brian Salomon82f44312017-01-11 13:42:54 -0500128 GrPaint grPaint;
129 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
130 grPaint.addColorFragmentProcessor(std::move(fp));
bsalomonbcf0a522014-10-08 08:40:09 -0700131 SkMatrix viewMatrix;
132 viewMatrix.setTranslate(x, y);
Brian Salomonac70f842017-05-08 10:43:33 -0400133 renderTargetContext->priv().testingOnly_addDrawOp(
Brian Salomonbaaf4392017-06-15 09:59:23 -0400134 GrRectOpFactory::MakeNonAAFill(std::move(grPaint), viewMatrix,
135 renderRect, GrAAType::kNone));
bsalomonbcf0a522014-10-08 08:40:09 -0700136 }
137 x += renderRect.width() + kTestPad;
138 }
sugoi24dcac22014-07-07 15:09:48 -0700139 }
bsalomonbcf0a522014-10-08 08:40:09 -0700140 }
sugoi24dcac22014-07-07 15:09:48 -0700141
142private:
143 SkBitmap fBmp[3];
144
145 typedef GM INHERITED;
146};
147
halcanary385fe4d2015-08-26 13:07:48 -0700148DEF_GM(return new YUVtoRGBEffect;)
jbaumanb445a572016-06-09 13:24:48 -0700149
150//////////////////////////////////////////////////////////////////////////////
151
152class YUVNV12toRGBEffect : public GM {
153public:
154 YUVNV12toRGBEffect() {
155 this->setBGColor(0xFFFFFFFF);
156 }
157
158protected:
159 SkString onShortName() override {
160 return SkString("yuv_nv12_to_rgb_effect");
161 }
162
163 SkISize onISize() override {
164 return SkISize::Make(48, 120);
165 }
166
167 void onOnceBeforeDraw() override {
168 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
169 fBmp[0].allocPixels(yinfo);
170 SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE);
171 fBmp[1].allocPixels(uvinfo);
172 int color[] = {0, 85, 170};
173 const int limit[] = {255, 0, 255};
174 const int invl[] = {0, 255, 0};
175 const int inc[] = {1, -1, 1};
176
177 {
178 unsigned char* pixels = (unsigned char*)fBmp[0].getPixels();
179 const size_t nbBytes = fBmp[0].rowBytes() * fBmp[0].height();
180 for (size_t j = 0; j < nbBytes; ++j) {
181 pixels[j] = (unsigned char)color[0];
182 color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0];
183 }
184 }
185
186 {
187 for (int y = 0; y < fBmp[1].height(); ++y) {
188 uint32_t* pixels = fBmp[1].getAddr32(0, y);
189 for (int j = 0; j < fBmp[1].width(); ++j) {
190 pixels[j] = SkColorSetARGB(0, color[1], color[2], 0);
191 color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1];
192 color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2];
193 }
194 }
195 }
196 }
197
198 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400199 GrRenderTargetContext* renderTargetContext =
200 canvas->internal_private_accessTopLayerRenderTargetContext();
201 if (!renderTargetContext) {
jbaumanb445a572016-06-09 13:24:48 -0700202 skiagm::GM::DrawGpuOnlyMessage(canvas);
203 return;
204 }
205
206 GrContext* context = canvas->getGrContext();
207 if (!context) {
208 return;
209 }
210
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500211 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips2f493142017-03-02 18:18:38 -0500212 sk_sp<GrTextureProxy> proxy[3];
jbaumanb445a572016-06-09 13:24:48 -0700213
Brian Salomon2a4f9832018-03-03 22:43:43 -0500214 for (int i = 0; i < 3; ++i) {
215 int index = (0 == i) ? 0 : 1;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500216 GrSurfaceDesc desc;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500217 desc.fWidth = fBmp[index].width();
218 desc.fHeight = fBmp[index].height();
219 desc.fConfig = SkImageInfo2GrPixelConfig(fBmp[index].info(), *context->caps());
220 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500221
Brian Salomon58389b92018-03-07 13:01:25 -0500222 proxy[i] = proxyProvider->createTextureProxy(
223 desc, SkBudgeted::kYes, fBmp[index].getPixels(), fBmp[index].rowBytes());
Brian Salomon2a4f9832018-03-03 22:43:43 -0500224 if (!proxy[i]) {
225 return;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500226 }
jbaumanb445a572016-06-09 13:24:48 -0700227 }
228
mtkleindbfd7ab2016-09-01 11:24:54 -0700229 constexpr SkScalar kDrawPad = 10.f;
230 constexpr SkScalar kTestPad = 10.f;
231 constexpr SkScalar kColorSpaceOffset = 36.f;
jbaumanb445a572016-06-09 13:24:48 -0700232 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
233
234 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
235 SkRect renderRect =
236 SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), SkIntToScalar(fBmp[0].height()));
237 renderRect.outset(kDrawPad, kDrawPad);
238
239 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
240 SkScalar x = kDrawPad + kTestPad;
241
robertphillips28a838e2016-06-23 14:07:00 -0700242 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500243 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500244 auto fp = GrYUVtoRGBEffect::Make(proxy[0], proxy[1], proxy[2], sizes,
245 static_cast<SkYUVColorSpace>(space), true);
jbaumanb445a572016-06-09 13:24:48 -0700246 if (fp) {
247 SkMatrix viewMatrix;
248 viewMatrix.setTranslate(x, y);
Brian Salomonaff329b2017-08-11 09:40:37 -0400249 grPaint.addColorFragmentProcessor(std::move(fp));
Robert Phillips09dfc472017-09-13 15:25:47 -0400250 std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
251 std::move(grPaint), viewMatrix, renderRect, GrAAType::kNone));
252 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
jbaumanb445a572016-06-09 13:24:48 -0700253 }
254 }
255 }
256
257private:
258 SkBitmap fBmp[2];
259
260 typedef GM INHERITED;
261};
262
263DEF_GM(return new YUVNV12toRGBEffect;)
sugoi24dcac22014-07-07 15:09:48 -0700264}
265
266#endif