blob: 103c2e41ffa7d9ecfa3ea0e6a95fa5a1474972e0 [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"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContextPriv.h"
sugoi24dcac22014-07-07 15:09:48 -070016#include "SkBitmap.h"
17#include "SkGr.h"
18#include "SkGradientShader.h"
bsalomonf267c1e2016-02-01 13:16:14 -080019#include "effects/GrYUVEffect.h"
Brian Salomon89527432016-12-16 09:52:16 -050020#include "ops/GrDrawOp.h"
21#include "ops/GrRectOpFactory.h"
sugoi24dcac22014-07-07 15:09:48 -070022
sugoi4ccce7e2015-02-13 13:57:09 -080023#define YSIZE 8
24#define USIZE 4
25#define VSIZE 4
26
sugoi24dcac22014-07-07 15:09:48 -070027namespace skiagm {
28/**
29 * This GM directly exercises GrYUVtoRGBEffect.
30 */
31class YUVtoRGBEffect : public GM {
32public:
33 YUVtoRGBEffect() {
34 this->setBGColor(0xFFFFFFFF);
35 }
36
37protected:
mtklein36352bf2015-03-25 18:17:31 -070038 SkString onShortName() override {
sugoi24dcac22014-07-07 15:09:48 -070039 return SkString("yuv_to_rgb_effect");
40 }
41
mtklein36352bf2015-03-25 18:17:31 -070042 SkISize onISize() override {
rileya13400392015-07-20 15:00:03 -070043 return SkISize::Make(238, 120);
sugoi24dcac22014-07-07 15:09:48 -070044 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 void onOnceBeforeDraw() override {
sugoi4ccce7e2015-02-13 13:57:09 -080047 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
48 fBmp[0].allocPixels(yinfo);
49 SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE);
50 fBmp[1].allocPixels(uinfo);
51 SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE);
52 fBmp[2].allocPixels(vinfo);
sugoi24dcac22014-07-07 15:09:48 -070053 unsigned char* pixels[3];
54 for (int i = 0; i < 3; ++i) {
55 pixels[i] = (unsigned char*)fBmp[i].getPixels();
56 }
57 int color[] = {0, 85, 170};
58 const int limit[] = {255, 0, 255};
59 const int invl[] = {0, 255, 0};
60 const int inc[] = {1, -1, 1};
sugoi4ccce7e2015-02-13 13:57:09 -080061 for (int i = 0; i < 3; ++i) {
62 const size_t nbBytes = fBmp[i].rowBytes() * fBmp[i].height();
63 for (size_t j = 0; j < nbBytes; ++j) {
sugoi24dcac22014-07-07 15:09:48 -070064 pixels[i][j] = (unsigned char)color[i];
65 color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i];
66 }
67 }
68 }
69
mtklein36352bf2015-03-25 18:17:31 -070070 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -040071 GrRenderTargetContext* renderTargetContext =
72 canvas->internal_private_accessTopLayerRenderTargetContext();
73 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -070074 skiagm::GM::DrawGpuOnlyMessage(canvas);
robertphillips175dd9b2016-04-28 14:32:04 -070075 return;
sugoi24dcac22014-07-07 15:09:48 -070076 }
77
robertphillips175dd9b2016-04-28 14:32:04 -070078 GrContext* context = canvas->getGrContext();
79 if (!context) {
sugoi24dcac22014-07-07 15:09:48 -070080 return;
81 }
82
Hal Canarycefc4312016-11-04 16:26:16 -040083 sk_sp<GrTexture> texture[3];
Brian Osman7b8400d2016-11-08 17:08:54 -050084 texture[0].reset(
Brian Osman61624f02016-12-09 14:51:59 -050085 GrRefCachedBitmapTexture(context, fBmp[0], GrSamplerParams::ClampBilerp()));
Brian Osman7b8400d2016-11-08 17:08:54 -050086 texture[1].reset(
Brian Osman61624f02016-12-09 14:51:59 -050087 GrRefCachedBitmapTexture(context, fBmp[1], GrSamplerParams::ClampBilerp()));
Brian Osman7b8400d2016-11-08 17:08:54 -050088 texture[2].reset(
Brian Osman61624f02016-12-09 14:51:59 -050089 GrRefCachedBitmapTexture(context, fBmp[2], GrSamplerParams::ClampBilerp()));
bsalomonbcf0a522014-10-08 08:40:09 -070090
91 if (!texture[0] || !texture[1] || !texture[2]) {
sugoi24dcac22014-07-07 15:09:48 -070092 return;
93 }
94
mtkleindbfd7ab2016-09-01 11:24:54 -070095 constexpr SkScalar kDrawPad = 10.f;
96 constexpr SkScalar kTestPad = 10.f;
97 constexpr SkScalar kColorSpaceOffset = 36.f;
sugoi4ccce7e2015-02-13 13:57:09 -080098 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
sugoi24dcac22014-07-07 15:09:48 -070099
rileyaabaef862014-09-12 17:45:58 -0700100 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace;
101 ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -0700102 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
103 SkIntToScalar(fBmp[0].height()));
104 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -0700105
bsalomonbcf0a522014-10-08 08:40:09 -0700106 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
107 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700108
bsalomonbcf0a522014-10-08 08:40:09 -0700109 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
110 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700111
bsalomonbcf0a522014-10-08 08:40:09 -0700112 for (int i = 0; i < 6; ++i) {
robertphillips28a838e2016-06-23 14:07:00 -0700113 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500114 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Hal Canarycefc4312016-11-04 16:26:16 -0400115 sk_sp<GrFragmentProcessor> fp(
116 GrYUVEffect::MakeYUVToRGB(texture[indices[i][0]].get(),
117 texture[indices[i][1]].get(),
118 texture[indices[i][2]].get(),
119 sizes,
120 static_cast<SkYUVColorSpace>(space),
121 false));
bsalomonbcf0a522014-10-08 08:40:09 -0700122 if (fp) {
123 SkMatrix viewMatrix;
124 viewMatrix.setTranslate(x, y);
robertphillips28a838e2016-06-23 14:07:00 -0700125 grPaint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf8334782017-01-03 09:42:58 -0500126 std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
Brian Salomon6a639042016-12-14 11:08:17 -0500127 GrColor_WHITE, viewMatrix, renderRect, nullptr, nullptr));
Brian Salomon1951f3d2016-12-09 16:07:12 -0500128 renderTargetContext->priv().testingOnly_addDrawOp(grPaint, GrAAType::kNone,
129 std::move(op));
bsalomonbcf0a522014-10-08 08:40:09 -0700130 }
131 x += renderRect.width() + kTestPad;
132 }
sugoi24dcac22014-07-07 15:09:48 -0700133 }
bsalomonbcf0a522014-10-08 08:40:09 -0700134 }
sugoi24dcac22014-07-07 15:09:48 -0700135
136private:
137 SkBitmap fBmp[3];
138
139 typedef GM INHERITED;
140};
141
halcanary385fe4d2015-08-26 13:07:48 -0700142DEF_GM(return new YUVtoRGBEffect;)
jbaumanb445a572016-06-09 13:24:48 -0700143
144//////////////////////////////////////////////////////////////////////////////
145
146class YUVNV12toRGBEffect : public GM {
147public:
148 YUVNV12toRGBEffect() {
149 this->setBGColor(0xFFFFFFFF);
150 }
151
152protected:
153 SkString onShortName() override {
154 return SkString("yuv_nv12_to_rgb_effect");
155 }
156
157 SkISize onISize() override {
158 return SkISize::Make(48, 120);
159 }
160
161 void onOnceBeforeDraw() override {
162 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
163 fBmp[0].allocPixels(yinfo);
164 SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE);
165 fBmp[1].allocPixels(uvinfo);
166 int color[] = {0, 85, 170};
167 const int limit[] = {255, 0, 255};
168 const int invl[] = {0, 255, 0};
169 const int inc[] = {1, -1, 1};
170
171 {
172 unsigned char* pixels = (unsigned char*)fBmp[0].getPixels();
173 const size_t nbBytes = fBmp[0].rowBytes() * fBmp[0].height();
174 for (size_t j = 0; j < nbBytes; ++j) {
175 pixels[j] = (unsigned char)color[0];
176 color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0];
177 }
178 }
179
180 {
181 for (int y = 0; y < fBmp[1].height(); ++y) {
182 uint32_t* pixels = fBmp[1].getAddr32(0, y);
183 for (int j = 0; j < fBmp[1].width(); ++j) {
184 pixels[j] = SkColorSetARGB(0, color[1], color[2], 0);
185 color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1];
186 color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2];
187 }
188 }
189 }
190 }
191
192 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400193 GrRenderTargetContext* renderTargetContext =
194 canvas->internal_private_accessTopLayerRenderTargetContext();
195 if (!renderTargetContext) {
jbaumanb445a572016-06-09 13:24:48 -0700196 skiagm::GM::DrawGpuOnlyMessage(canvas);
197 return;
198 }
199
200 GrContext* context = canvas->getGrContext();
201 if (!context) {
202 return;
203 }
204
Hal Canarycefc4312016-11-04 16:26:16 -0400205 sk_sp<GrTexture> texture[3];
Brian Osman7b8400d2016-11-08 17:08:54 -0500206 texture[0].reset(
Brian Osman61624f02016-12-09 14:51:59 -0500207 GrRefCachedBitmapTexture(context, fBmp[0], GrSamplerParams::ClampBilerp()));
Brian Osman7b8400d2016-11-08 17:08:54 -0500208 texture[1].reset(
Brian Osman61624f02016-12-09 14:51:59 -0500209 GrRefCachedBitmapTexture(context, fBmp[1], GrSamplerParams::ClampBilerp()));
Brian Osman7b8400d2016-11-08 17:08:54 -0500210 texture[2].reset(
Brian Osman61624f02016-12-09 14:51:59 -0500211 GrRefCachedBitmapTexture(context, fBmp[1], GrSamplerParams::ClampBilerp()));
jbaumanb445a572016-06-09 13:24:48 -0700212
213 if (!texture[0] || !texture[1] || !texture[2]) {
214 return;
215 }
216
mtkleindbfd7ab2016-09-01 11:24:54 -0700217 constexpr SkScalar kDrawPad = 10.f;
218 constexpr SkScalar kTestPad = 10.f;
219 constexpr SkScalar kColorSpaceOffset = 36.f;
jbaumanb445a572016-06-09 13:24:48 -0700220 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
221
222 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
223 SkRect renderRect =
224 SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), SkIntToScalar(fBmp[0].height()));
225 renderRect.outset(kDrawPad, kDrawPad);
226
227 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
228 SkScalar x = kDrawPad + kTestPad;
229
robertphillips28a838e2016-06-23 14:07:00 -0700230 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500231 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
jbaumanb445a572016-06-09 13:24:48 -0700232 sk_sp<GrFragmentProcessor> fp(
Hal Canarycefc4312016-11-04 16:26:16 -0400233 GrYUVEffect::MakeYUVToRGB(texture[0].get(), texture[1].get(), texture[2].get(),
234 sizes, static_cast<SkYUVColorSpace>(space), true));
jbaumanb445a572016-06-09 13:24:48 -0700235 if (fp) {
236 SkMatrix viewMatrix;
237 viewMatrix.setTranslate(x, y);
robertphillips28a838e2016-06-23 14:07:00 -0700238 grPaint.addColorFragmentProcessor(fp);
Brian Salomonf8334782017-01-03 09:42:58 -0500239 std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
240 GrColor_WHITE, viewMatrix, renderRect, nullptr, nullptr));
Brian Salomon1951f3d2016-12-09 16:07:12 -0500241 renderTargetContext->priv().testingOnly_addDrawOp(grPaint, GrAAType::kNone,
242 std::move(op));
jbaumanb445a572016-06-09 13:24:48 -0700243 }
244 }
245 }
246
247private:
248 SkBitmap fBmp[2];
249
250 typedef GM INHERITED;
251};
252
253DEF_GM(return new YUVNV12toRGBEffect;)
sugoi24dcac22014-07-07 15:09:48 -0700254}
255
256#endif