blob: a153c991fc548c1ff298f17a6edf299ab833f9ad [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"
robertphillips391395d2016-03-02 09:26:36 -080015#include "GrDrawContextPriv.h"
joshualitt04194f32016-01-13 10:08:27 -080016#include "GrPipelineBuilder.h"
sugoi24dcac22014-07-07 15:09:48 -070017#include "SkBitmap.h"
18#include "SkGr.h"
19#include "SkGradientShader.h"
joshualitt04194f32016-01-13 10:08:27 -080020#include "batches/GrDrawBatch.h"
21#include "batches/GrRectBatchFactory.h"
bsalomonf267c1e2016-02-01 13:16:14 -080022#include "effects/GrYUVEffect.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 {
robertphillips175dd9b2016-04-28 14:32:04 -070072 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
73 if (!drawContext) {
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
bsalomonbcf0a522014-10-08 08:40:09 -070083 SkAutoTUnref<GrTexture> texture[3];
bsalomonafa95e22015-10-12 10:39:46 -070084 texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0],
brianosman982eb7f2016-06-06 13:10:58 -070085 GrTextureParams::ClampBilerp(),
86 SkSourceGammaTreatment::kRespect));
bsalomonafa95e22015-10-12 10:39:46 -070087 texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1],
brianosman982eb7f2016-06-06 13:10:58 -070088 GrTextureParams::ClampBilerp(),
89 SkSourceGammaTreatment::kRespect));
bsalomonafa95e22015-10-12 10:39:46 -070090 texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[2],
brianosman982eb7f2016-06-06 13:10:58 -070091 GrTextureParams::ClampBilerp(),
92 SkSourceGammaTreatment::kRespect));
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
98 static const SkScalar kDrawPad = 10.f;
99 static const SkScalar kTestPad = 10.f;
sugoi4ccce7e2015-02-13 13:57:09 -0800100 static const SkScalar kColorSpaceOffset = 36.f;
101 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) {
joshualitt2cdec312015-07-09 07:31:31 -0700116 GrPipelineBuilder pipelineBuilder;
bungeman06ca8ec2016-06-09 08:01:03 -0700117 pipelineBuilder.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
jbaumanb445a572016-06-09 13:24:48 -0700118 sk_sp<GrFragmentProcessor> fp(GrYUVEffect::MakeYUVToRGB(
119 texture[indices[i][0]], texture[indices[i][1]], texture[indices[i][2]], sizes,
120 static_cast<SkYUVColorSpace>(space), false));
bsalomonbcf0a522014-10-08 08:40:09 -0700121 if (fp) {
122 SkMatrix viewMatrix;
123 viewMatrix.setTranslate(x, y);
bungeman06ca8ec2016-06-09 08:01:03 -0700124 pipelineBuilder.addColorFragmentProcessor(std::move(fp));
joshualitt04194f32016-01-13 10:08:27 -0800125 SkAutoTUnref<GrDrawBatch> batch(
126 GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix,
127 renderRect, nullptr, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800128 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
bsalomonbcf0a522014-10-08 08:40:09 -0700129 }
130 x += renderRect.width() + kTestPad;
131 }
sugoi24dcac22014-07-07 15:09:48 -0700132 }
bsalomonbcf0a522014-10-08 08:40:09 -0700133 }
sugoi24dcac22014-07-07 15:09:48 -0700134
135private:
136 SkBitmap fBmp[3];
137
138 typedef GM INHERITED;
139};
140
halcanary385fe4d2015-08-26 13:07:48 -0700141DEF_GM(return new YUVtoRGBEffect;)
jbaumanb445a572016-06-09 13:24:48 -0700142
143//////////////////////////////////////////////////////////////////////////////
144
145class YUVNV12toRGBEffect : public GM {
146public:
147 YUVNV12toRGBEffect() {
148 this->setBGColor(0xFFFFFFFF);
149 }
150
151protected:
152 SkString onShortName() override {
153 return SkString("yuv_nv12_to_rgb_effect");
154 }
155
156 SkISize onISize() override {
157 return SkISize::Make(48, 120);
158 }
159
160 void onOnceBeforeDraw() override {
161 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
162 fBmp[0].allocPixels(yinfo);
163 SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE);
164 fBmp[1].allocPixels(uvinfo);
165 int color[] = {0, 85, 170};
166 const int limit[] = {255, 0, 255};
167 const int invl[] = {0, 255, 0};
168 const int inc[] = {1, -1, 1};
169
170 {
171 unsigned char* pixels = (unsigned char*)fBmp[0].getPixels();
172 const size_t nbBytes = fBmp[0].rowBytes() * fBmp[0].height();
173 for (size_t j = 0; j < nbBytes; ++j) {
174 pixels[j] = (unsigned char)color[0];
175 color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0];
176 }
177 }
178
179 {
180 for (int y = 0; y < fBmp[1].height(); ++y) {
181 uint32_t* pixels = fBmp[1].getAddr32(0, y);
182 for (int j = 0; j < fBmp[1].width(); ++j) {
183 pixels[j] = SkColorSetARGB(0, color[1], color[2], 0);
184 color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1];
185 color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2];
186 }
187 }
188 }
189 }
190
191 void onDraw(SkCanvas* canvas) override {
192 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
193 if (!drawContext) {
194 skiagm::GM::DrawGpuOnlyMessage(canvas);
195 return;
196 }
197
198 GrContext* context = canvas->getGrContext();
199 if (!context) {
200 return;
201 }
202
203 SkAutoTUnref<GrTexture> texture[3];
204 texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0], GrTextureParams::ClampBilerp(),
205 SkSourceGammaTreatment::kRespect));
206 texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1], GrTextureParams::ClampBilerp(),
207 SkSourceGammaTreatment::kRespect));
208 texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[1], GrTextureParams::ClampBilerp(),
209 SkSourceGammaTreatment::kRespect));
210
211 if (!texture[0] || !texture[1] || !texture[2]) {
212 return;
213 }
214
215 static const SkScalar kDrawPad = 10.f;
216 static const SkScalar kTestPad = 10.f;
217 static const SkScalar kColorSpaceOffset = 36.f;
218 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
219
220 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
221 SkRect renderRect =
222 SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), SkIntToScalar(fBmp[0].height()));
223 renderRect.outset(kDrawPad, kDrawPad);
224
225 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
226 SkScalar x = kDrawPad + kTestPad;
227
228 GrPipelineBuilder pipelineBuilder;
229 pipelineBuilder.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
230 sk_sp<GrFragmentProcessor> fp(
231 GrYUVEffect::MakeYUVToRGB(texture[0], texture[1], texture[2], sizes,
232 static_cast<SkYUVColorSpace>(space), true));
233 if (fp) {
234 SkMatrix viewMatrix;
235 viewMatrix.setTranslate(x, y);
236 pipelineBuilder.addColorFragmentProcessor(fp);
237 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(
238 GrColor_WHITE, viewMatrix, renderRect, nullptr, nullptr));
239 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
240 }
241 }
242 }
243
244private:
245 SkBitmap fBmp[2];
246
247 typedef GM INHERITED;
248};
249
250DEF_GM(return new YUVNV12toRGBEffect;)
sugoi24dcac22014-07-07 15:09:48 -0700251}
252
253#endif