blob: 001c53d70174c802477c52a8fdc3a8761ca987a7 [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"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050016#include "GrTextureProxy.h"
sugoi24dcac22014-07-07 15:09:48 -070017#include "SkBitmap.h"
18#include "SkGr.h"
19#include "SkGradientShader.h"
bsalomonf267c1e2016-02-01 13:16:14 -080020#include "effects/GrYUVEffect.h"
Brian Salomon89527432016-12-16 09:52:16 -050021#include "ops/GrDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040022#include "ops/GrRectOpFactory.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 {
Brian Osman11052242016-10-27 14:47:55 -040072 GrRenderTargetContext* renderTargetContext =
73 canvas->internal_private_accessTopLayerRenderTargetContext();
74 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -070075 skiagm::GM::DrawGpuOnlyMessage(canvas);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050076 return;
sugoi24dcac22014-07-07 15:09:48 -070077 }
78
robertphillips175dd9b2016-04-28 14:32:04 -070079 GrContext* context = canvas->getGrContext();
80 if (!context) {
sugoi24dcac22014-07-07 15:09:48 -070081 return;
82 }
83
Robert Phillips2f493142017-03-02 18:18:38 -050084 sk_sp<GrTextureProxy> proxy[3];
bsalomonbcf0a522014-10-08 08:40:09 -070085
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050086 {
87 GrSurfaceDesc desc;
88 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050089
90 for (int i = 0; i < 3; ++i) {
91 desc.fWidth = fBmp[i].width();
92 desc.fHeight = fBmp[i].height();
Robert Phillips40fd7c92017-01-30 08:06:27 -050093 desc.fConfig = SkImageInfo2GrPixelConfig(fBmp[i].info(), *context->caps());
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050094
Robert Phillips26c90e02017-03-14 14:39:29 -040095 proxy[i] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050096 desc, SkBudgeted::kYes,
97 fBmp[i].getPixels(), fBmp[i].rowBytes());
98 if (!proxy[i]) {
99 return;
100 }
101 }
sugoi24dcac22014-07-07 15:09:48 -0700102 }
103
mtkleindbfd7ab2016-09-01 11:24:54 -0700104 constexpr SkScalar kDrawPad = 10.f;
105 constexpr SkScalar kTestPad = 10.f;
106 constexpr SkScalar kColorSpaceOffset = 36.f;
sugoi4ccce7e2015-02-13 13:57:09 -0800107 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
sugoi24dcac22014-07-07 15:09:48 -0700108
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500109 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -0700110 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
111 SkIntToScalar(fBmp[0].height()));
112 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -0700113
bsalomonbcf0a522014-10-08 08:40:09 -0700114 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
115 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700116
bsalomonbcf0a522014-10-08 08:40:09 -0700117 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
118 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700119
bsalomonbcf0a522014-10-08 08:40:09 -0700120 for (int i = 0; i < 6; ++i) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400121 std::unique_ptr<GrFragmentProcessor> fp(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400122 GrYUVEffect::MakeYUVToRGB(proxy[indices[i][0]],
Robert Phillips2f493142017-03-02 18:18:38 -0500123 proxy[indices[i][1]],
124 proxy[indices[i][2]],
Hal Canarycefc4312016-11-04 16:26:16 -0400125 sizes,
126 static_cast<SkYUVColorSpace>(space),
127 false));
bsalomonbcf0a522014-10-08 08:40:09 -0700128 if (fp) {
Brian Salomon82f44312017-01-11 13:42:54 -0500129 GrPaint grPaint;
130 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
131 grPaint.addColorFragmentProcessor(std::move(fp));
bsalomonbcf0a522014-10-08 08:40:09 -0700132 SkMatrix viewMatrix;
133 viewMatrix.setTranslate(x, y);
Brian Salomonac70f842017-05-08 10:43:33 -0400134 renderTargetContext->priv().testingOnly_addDrawOp(
Brian Salomonbaaf4392017-06-15 09:59:23 -0400135 GrRectOpFactory::MakeNonAAFill(std::move(grPaint), viewMatrix,
136 renderRect, GrAAType::kNone));
bsalomonbcf0a522014-10-08 08:40:09 -0700137 }
138 x += renderRect.width() + kTestPad;
139 }
sugoi24dcac22014-07-07 15:09:48 -0700140 }
bsalomonbcf0a522014-10-08 08:40:09 -0700141 }
sugoi24dcac22014-07-07 15:09:48 -0700142
143private:
144 SkBitmap fBmp[3];
145
146 typedef GM INHERITED;
147};
148
halcanary385fe4d2015-08-26 13:07:48 -0700149DEF_GM(return new YUVtoRGBEffect;)
jbaumanb445a572016-06-09 13:24:48 -0700150
151//////////////////////////////////////////////////////////////////////////////
152
153class YUVNV12toRGBEffect : public GM {
154public:
155 YUVNV12toRGBEffect() {
156 this->setBGColor(0xFFFFFFFF);
157 }
158
159protected:
160 SkString onShortName() override {
161 return SkString("yuv_nv12_to_rgb_effect");
162 }
163
164 SkISize onISize() override {
165 return SkISize::Make(48, 120);
166 }
167
168 void onOnceBeforeDraw() override {
169 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
170 fBmp[0].allocPixels(yinfo);
171 SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE);
172 fBmp[1].allocPixels(uvinfo);
173 int color[] = {0, 85, 170};
174 const int limit[] = {255, 0, 255};
175 const int invl[] = {0, 255, 0};
176 const int inc[] = {1, -1, 1};
177
178 {
179 unsigned char* pixels = (unsigned char*)fBmp[0].getPixels();
180 const size_t nbBytes = fBmp[0].rowBytes() * fBmp[0].height();
181 for (size_t j = 0; j < nbBytes; ++j) {
182 pixels[j] = (unsigned char)color[0];
183 color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0];
184 }
185 }
186
187 {
188 for (int y = 0; y < fBmp[1].height(); ++y) {
189 uint32_t* pixels = fBmp[1].getAddr32(0, y);
190 for (int j = 0; j < fBmp[1].width(); ++j) {
191 pixels[j] = SkColorSetARGB(0, color[1], color[2], 0);
192 color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1];
193 color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2];
194 }
195 }
196 }
197 }
198
199 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400200 GrRenderTargetContext* renderTargetContext =
201 canvas->internal_private_accessTopLayerRenderTargetContext();
202 if (!renderTargetContext) {
jbaumanb445a572016-06-09 13:24:48 -0700203 skiagm::GM::DrawGpuOnlyMessage(canvas);
204 return;
205 }
206
207 GrContext* context = canvas->getGrContext();
208 if (!context) {
209 return;
210 }
211
Robert Phillips2f493142017-03-02 18:18:38 -0500212 sk_sp<GrTextureProxy> proxy[3];
jbaumanb445a572016-06-09 13:24:48 -0700213
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500214 {
215 GrSurfaceDesc desc;
216 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
217
218 for (int i = 0; i < 3; ++i) {
219 int index = (0 == i) ? 0 : 1;
220
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500221 desc.fWidth = fBmp[index].width();
222 desc.fHeight = fBmp[index].height();
Robert Phillips40fd7c92017-01-30 08:06:27 -0500223 desc.fConfig = SkImageInfo2GrPixelConfig(fBmp[index].info(), *context->caps());
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500224
Robert Phillips26c90e02017-03-14 14:39:29 -0400225 proxy[i] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500226 desc, SkBudgeted::kYes,
227 fBmp[index].getPixels(),
228 fBmp[index].rowBytes());
229 if (!proxy[i]) {
230 return;
231 }
232 }
jbaumanb445a572016-06-09 13:24:48 -0700233 }
234
mtkleindbfd7ab2016-09-01 11:24:54 -0700235 constexpr SkScalar kDrawPad = 10.f;
236 constexpr SkScalar kTestPad = 10.f;
237 constexpr SkScalar kColorSpaceOffset = 36.f;
jbaumanb445a572016-06-09 13:24:48 -0700238 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
239
240 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
241 SkRect renderRect =
242 SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), SkIntToScalar(fBmp[0].height()));
243 renderRect.outset(kDrawPad, kDrawPad);
244
245 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
246 SkScalar x = kDrawPad + kTestPad;
247
robertphillips28a838e2016-06-23 14:07:00 -0700248 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500249 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Brian Salomonaff329b2017-08-11 09:40:37 -0400250 auto fp = GrYUVEffect::MakeYUVToRGB(proxy[0], proxy[1], proxy[2], sizes,
251 static_cast<SkYUVColorSpace>(space), true);
jbaumanb445a572016-06-09 13:24:48 -0700252 if (fp) {
253 SkMatrix viewMatrix;
254 viewMatrix.setTranslate(x, y);
Brian Salomonaff329b2017-08-11 09:40:37 -0400255 grPaint.addColorFragmentProcessor(std::move(fp));
Brian Salomonbaaf4392017-06-15 09:59:23 -0400256 renderTargetContext->priv().testingOnly_addDrawOp(GrRectOpFactory::MakeNonAAFill(
257 std::move(grPaint), viewMatrix, renderRect, GrAAType::kNone));
jbaumanb445a572016-06-09 13:24:48 -0700258 }
259 }
260 }
261
262private:
263 SkBitmap fBmp[2];
264
265 typedef GM INHERITED;
266};
267
268DEF_GM(return new YUVNV12toRGBEffect;)
sugoi24dcac22014-07-07 15:09:48 -0700269}
270
271#endif