blob: e314ea28bc9a03d506c56dad99034f2e30679acc [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
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050089 {
90 GrSurfaceDesc desc;
91 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050092
93 for (int i = 0; i < 3; ++i) {
94 desc.fWidth = fBmp[i].width();
95 desc.fHeight = fBmp[i].height();
Robert Phillips40fd7c92017-01-30 08:06:27 -050096 desc.fConfig = SkImageInfo2GrPixelConfig(fBmp[i].info(), *context->caps());
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050097
Robert Phillips0bd24dc2018-01-16 08:06:32 -050098 proxy[i] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes,
99 fBmp[i].getPixels(),
100 fBmp[i].rowBytes());
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500101 if (!proxy[i]) {
102 return;
103 }
104 }
sugoi24dcac22014-07-07 15:09:48 -0700105 }
106
mtkleindbfd7ab2016-09-01 11:24:54 -0700107 constexpr SkScalar kDrawPad = 10.f;
108 constexpr SkScalar kTestPad = 10.f;
109 constexpr SkScalar kColorSpaceOffset = 36.f;
sugoi4ccce7e2015-02-13 13:57:09 -0800110 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
sugoi24dcac22014-07-07 15:09:48 -0700111
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500112 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -0700113 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
114 SkIntToScalar(fBmp[0].height()));
115 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -0700116
bsalomonbcf0a522014-10-08 08:40:09 -0700117 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
118 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700119
bsalomonbcf0a522014-10-08 08:40:09 -0700120 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
121 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700122
bsalomonbcf0a522014-10-08 08:40:09 -0700123 for (int i = 0; i < 6; ++i) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400124 std::unique_ptr<GrFragmentProcessor> fp(
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500125 GrYUVtoRGBEffect::Make(proxy[indices[i][0]],
126 proxy[indices[i][1]],
127 proxy[indices[i][2]],
128 sizes,
129 static_cast<SkYUVColorSpace>(space),
130 false));
bsalomonbcf0a522014-10-08 08:40:09 -0700131 if (fp) {
Brian Salomon82f44312017-01-11 13:42:54 -0500132 GrPaint grPaint;
133 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
134 grPaint.addColorFragmentProcessor(std::move(fp));
bsalomonbcf0a522014-10-08 08:40:09 -0700135 SkMatrix viewMatrix;
136 viewMatrix.setTranslate(x, y);
Brian Salomonac70f842017-05-08 10:43:33 -0400137 renderTargetContext->priv().testingOnly_addDrawOp(
Brian Salomonbaaf4392017-06-15 09:59:23 -0400138 GrRectOpFactory::MakeNonAAFill(std::move(grPaint), viewMatrix,
139 renderRect, GrAAType::kNone));
bsalomonbcf0a522014-10-08 08:40:09 -0700140 }
141 x += renderRect.width() + kTestPad;
142 }
sugoi24dcac22014-07-07 15:09:48 -0700143 }
bsalomonbcf0a522014-10-08 08:40:09 -0700144 }
sugoi24dcac22014-07-07 15:09:48 -0700145
146private:
147 SkBitmap fBmp[3];
148
149 typedef GM INHERITED;
150};
151
halcanary385fe4d2015-08-26 13:07:48 -0700152DEF_GM(return new YUVtoRGBEffect;)
jbaumanb445a572016-06-09 13:24:48 -0700153
154//////////////////////////////////////////////////////////////////////////////
155
156class YUVNV12toRGBEffect : public GM {
157public:
158 YUVNV12toRGBEffect() {
159 this->setBGColor(0xFFFFFFFF);
160 }
161
162protected:
163 SkString onShortName() override {
164 return SkString("yuv_nv12_to_rgb_effect");
165 }
166
167 SkISize onISize() override {
168 return SkISize::Make(48, 120);
169 }
170
171 void onOnceBeforeDraw() override {
172 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
173 fBmp[0].allocPixels(yinfo);
174 SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE);
175 fBmp[1].allocPixels(uvinfo);
176 int color[] = {0, 85, 170};
177 const int limit[] = {255, 0, 255};
178 const int invl[] = {0, 255, 0};
179 const int inc[] = {1, -1, 1};
180
181 {
182 unsigned char* pixels = (unsigned char*)fBmp[0].getPixels();
183 const size_t nbBytes = fBmp[0].rowBytes() * fBmp[0].height();
184 for (size_t j = 0; j < nbBytes; ++j) {
185 pixels[j] = (unsigned char)color[0];
186 color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0];
187 }
188 }
189
190 {
191 for (int y = 0; y < fBmp[1].height(); ++y) {
192 uint32_t* pixels = fBmp[1].getAddr32(0, y);
193 for (int j = 0; j < fBmp[1].width(); ++j) {
194 pixels[j] = SkColorSetARGB(0, color[1], color[2], 0);
195 color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1];
196 color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2];
197 }
198 }
199 }
200 }
201
202 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400203 GrRenderTargetContext* renderTargetContext =
204 canvas->internal_private_accessTopLayerRenderTargetContext();
205 if (!renderTargetContext) {
jbaumanb445a572016-06-09 13:24:48 -0700206 skiagm::GM::DrawGpuOnlyMessage(canvas);
207 return;
208 }
209
210 GrContext* context = canvas->getGrContext();
211 if (!context) {
212 return;
213 }
214
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500215 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips2f493142017-03-02 18:18:38 -0500216 sk_sp<GrTextureProxy> proxy[3];
jbaumanb445a572016-06-09 13:24:48 -0700217
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500218 {
219 GrSurfaceDesc desc;
220 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
221
222 for (int i = 0; i < 3; ++i) {
223 int index = (0 == i) ? 0 : 1;
224
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500225 desc.fWidth = fBmp[index].width();
226 desc.fHeight = fBmp[index].height();
Robert Phillips40fd7c92017-01-30 08:06:27 -0500227 desc.fConfig = SkImageInfo2GrPixelConfig(fBmp[index].info(), *context->caps());
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500228
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500229 proxy[i] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes,
230 fBmp[index].getPixels(),
231 fBmp[index].rowBytes());
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500232 if (!proxy[i]) {
233 return;
234 }
235 }
jbaumanb445a572016-06-09 13:24:48 -0700236 }
237
mtkleindbfd7ab2016-09-01 11:24:54 -0700238 constexpr SkScalar kDrawPad = 10.f;
239 constexpr SkScalar kTestPad = 10.f;
240 constexpr SkScalar kColorSpaceOffset = 36.f;
jbaumanb445a572016-06-09 13:24:48 -0700241 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
242
243 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
244 SkRect renderRect =
245 SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), SkIntToScalar(fBmp[0].height()));
246 renderRect.outset(kDrawPad, kDrawPad);
247
248 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
249 SkScalar x = kDrawPad + kTestPad;
250
robertphillips28a838e2016-06-23 14:07:00 -0700251 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500252 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500253 auto fp = GrYUVtoRGBEffect::Make(proxy[0], proxy[1], proxy[2], sizes,
254 static_cast<SkYUVColorSpace>(space), true);
jbaumanb445a572016-06-09 13:24:48 -0700255 if (fp) {
256 SkMatrix viewMatrix;
257 viewMatrix.setTranslate(x, y);
Brian Salomonaff329b2017-08-11 09:40:37 -0400258 grPaint.addColorFragmentProcessor(std::move(fp));
Robert Phillips09dfc472017-09-13 15:25:47 -0400259 std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
260 std::move(grPaint), viewMatrix, renderRect, GrAAType::kNone));
261 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
jbaumanb445a572016-06-09 13:24:48 -0700262 }
263 }
264 }
265
266private:
267 SkBitmap fBmp[2];
268
269 typedef GM INHERITED;
270};
271
272DEF_GM(return new YUVNV12toRGBEffect;)
sugoi24dcac22014-07-07 15:09:48 -0700273}
274
275#endif