blob: 307ea7c6dd4a70294e9419c2435d711892c42c19 [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"
joshualitt04194f32016-01-13 10:08:27 -080019#include "batches/GrDrawBatch.h"
20#include "batches/GrRectBatchFactory.h"
bsalomonf267c1e2016-02-01 13:16:14 -080021#include "effects/GrYUVEffect.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(
85 GrRefCachedBitmapTexture(context, fBmp[0], GrTextureParams::ClampBilerp(),
86 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware));
87 texture[1].reset(
88 GrRefCachedBitmapTexture(context, fBmp[1], GrTextureParams::ClampBilerp(),
89 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware));
90 texture[2].reset(
91 GrRefCachedBitmapTexture(context, fBmp[2], GrTextureParams::ClampBilerp(),
92 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware));
bsalomonbcf0a522014-10-08 08:40:09 -070093
94 if (!texture[0] || !texture[1] || !texture[2]) {
sugoi24dcac22014-07-07 15:09:48 -070095 return;
96 }
97
mtkleindbfd7ab2016-09-01 11:24:54 -070098 constexpr SkScalar kDrawPad = 10.f;
99 constexpr SkScalar kTestPad = 10.f;
100 constexpr SkScalar kColorSpaceOffset = 36.f;
sugoi4ccce7e2015-02-13 13:57:09 -0800101 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
sugoi24dcac22014-07-07 15:09:48 -0700102
rileyaabaef862014-09-12 17:45:58 -0700103 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace;
104 ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -0700105 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
106 SkIntToScalar(fBmp[0].height()));
107 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -0700108
bsalomonbcf0a522014-10-08 08:40:09 -0700109 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
110 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700111
bsalomonbcf0a522014-10-08 08:40:09 -0700112 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
113 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700114
bsalomonbcf0a522014-10-08 08:40:09 -0700115 for (int i = 0; i < 6; ++i) {
robertphillips28a838e2016-06-23 14:07:00 -0700116 GrPaint grPaint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400117 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkBlendMode::kSrc));
Hal Canarycefc4312016-11-04 16:26:16 -0400118 sk_sp<GrFragmentProcessor> fp(
119 GrYUVEffect::MakeYUVToRGB(texture[indices[i][0]].get(),
120 texture[indices[i][1]].get(),
121 texture[indices[i][2]].get(),
122 sizes,
123 static_cast<SkYUVColorSpace>(space),
124 false));
bsalomonbcf0a522014-10-08 08:40:09 -0700125 if (fp) {
126 SkMatrix viewMatrix;
127 viewMatrix.setTranslate(x, y);
robertphillips28a838e2016-06-23 14:07:00 -0700128 grPaint.addColorFragmentProcessor(std::move(fp));
Hal Canarycefc4312016-11-04 16:26:16 -0400129 sk_sp<GrDrawBatch> batch(
joshualitt04194f32016-01-13 10:08:27 -0800130 GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix,
131 renderRect, nullptr, nullptr));
Hal Canarycefc4312016-11-04 16:26:16 -0400132 renderTargetContext->priv().testingOnly_drawBatch(grPaint, batch.get());
bsalomonbcf0a522014-10-08 08:40:09 -0700133 }
134 x += renderRect.width() + kTestPad;
135 }
sugoi24dcac22014-07-07 15:09:48 -0700136 }
bsalomonbcf0a522014-10-08 08:40:09 -0700137 }
sugoi24dcac22014-07-07 15:09:48 -0700138
139private:
140 SkBitmap fBmp[3];
141
142 typedef GM INHERITED;
143};
144
halcanary385fe4d2015-08-26 13:07:48 -0700145DEF_GM(return new YUVtoRGBEffect;)
jbaumanb445a572016-06-09 13:24:48 -0700146
147//////////////////////////////////////////////////////////////////////////////
148
149class YUVNV12toRGBEffect : public GM {
150public:
151 YUVNV12toRGBEffect() {
152 this->setBGColor(0xFFFFFFFF);
153 }
154
155protected:
156 SkString onShortName() override {
157 return SkString("yuv_nv12_to_rgb_effect");
158 }
159
160 SkISize onISize() override {
161 return SkISize::Make(48, 120);
162 }
163
164 void onOnceBeforeDraw() override {
165 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
166 fBmp[0].allocPixels(yinfo);
167 SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE);
168 fBmp[1].allocPixels(uvinfo);
169 int color[] = {0, 85, 170};
170 const int limit[] = {255, 0, 255};
171 const int invl[] = {0, 255, 0};
172 const int inc[] = {1, -1, 1};
173
174 {
175 unsigned char* pixels = (unsigned char*)fBmp[0].getPixels();
176 const size_t nbBytes = fBmp[0].rowBytes() * fBmp[0].height();
177 for (size_t j = 0; j < nbBytes; ++j) {
178 pixels[j] = (unsigned char)color[0];
179 color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0];
180 }
181 }
182
183 {
184 for (int y = 0; y < fBmp[1].height(); ++y) {
185 uint32_t* pixels = fBmp[1].getAddr32(0, y);
186 for (int j = 0; j < fBmp[1].width(); ++j) {
187 pixels[j] = SkColorSetARGB(0, color[1], color[2], 0);
188 color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1];
189 color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2];
190 }
191 }
192 }
193 }
194
195 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400196 GrRenderTargetContext* renderTargetContext =
197 canvas->internal_private_accessTopLayerRenderTargetContext();
198 if (!renderTargetContext) {
jbaumanb445a572016-06-09 13:24:48 -0700199 skiagm::GM::DrawGpuOnlyMessage(canvas);
200 return;
201 }
202
203 GrContext* context = canvas->getGrContext();
204 if (!context) {
205 return;
206 }
207
Hal Canarycefc4312016-11-04 16:26:16 -0400208 sk_sp<GrTexture> texture[3];
Brian Osman7b8400d2016-11-08 17:08:54 -0500209 texture[0].reset(
210 GrRefCachedBitmapTexture(context, fBmp[0], GrTextureParams::ClampBilerp(),
211 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware));
212 texture[1].reset(
213 GrRefCachedBitmapTexture(context, fBmp[1], GrTextureParams::ClampBilerp(),
214 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware));
215 texture[2].reset(
216 GrRefCachedBitmapTexture(context, fBmp[1], GrTextureParams::ClampBilerp(),
217 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware));
jbaumanb445a572016-06-09 13:24:48 -0700218
219 if (!texture[0] || !texture[1] || !texture[2]) {
220 return;
221 }
222
mtkleindbfd7ab2016-09-01 11:24:54 -0700223 constexpr SkScalar kDrawPad = 10.f;
224 constexpr SkScalar kTestPad = 10.f;
225 constexpr SkScalar kColorSpaceOffset = 36.f;
jbaumanb445a572016-06-09 13:24:48 -0700226 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
227
228 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
229 SkRect renderRect =
230 SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), SkIntToScalar(fBmp[0].height()));
231 renderRect.outset(kDrawPad, kDrawPad);
232
233 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
234 SkScalar x = kDrawPad + kTestPad;
235
robertphillips28a838e2016-06-23 14:07:00 -0700236 GrPaint grPaint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400237 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkBlendMode::kSrc));
jbaumanb445a572016-06-09 13:24:48 -0700238 sk_sp<GrFragmentProcessor> fp(
Hal Canarycefc4312016-11-04 16:26:16 -0400239 GrYUVEffect::MakeYUVToRGB(texture[0].get(), texture[1].get(), texture[2].get(),
240 sizes, static_cast<SkYUVColorSpace>(space), true));
jbaumanb445a572016-06-09 13:24:48 -0700241 if (fp) {
242 SkMatrix viewMatrix;
243 viewMatrix.setTranslate(x, y);
robertphillips28a838e2016-06-23 14:07:00 -0700244 grPaint.addColorFragmentProcessor(fp);
Hal Canarycefc4312016-11-04 16:26:16 -0400245 sk_sp<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(
jbaumanb445a572016-06-09 13:24:48 -0700246 GrColor_WHITE, viewMatrix, renderRect, nullptr, nullptr));
Hal Canarycefc4312016-11-04 16:26:16 -0400247 renderTargetContext->priv().testingOnly_drawBatch(grPaint, batch.get());
jbaumanb445a572016-06-09 13:24:48 -0700248 }
249 }
250 }
251
252private:
253 SkBitmap fBmp[2];
254
255 typedef GM INHERITED;
256};
257
258DEF_GM(return new YUVNV12toRGBEffect;)
sugoi24dcac22014-07-07 15:09:48 -0700259}
260
261#endif