blob: b639ba337e33f90af1bf76c2d05ad8fcd496ce1d [file] [log] [blame]
sugoi24dcac22014-07-07 15:09:48 -07001
2/*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9// This test only works with the GPU backend.
10
11#include "gm.h"
12
13#if SK_SUPPORT_GPU
14
15#include "GrContext.h"
16#include "GrTest.h"
17#include "effects/GrYUVtoRGBEffect.h"
18#include "SkBitmap.h"
19#include "SkGr.h"
20#include "SkGradientShader.h"
21
sugoi4ccce7e2015-02-13 13:57:09 -080022#define YSIZE 8
23#define USIZE 4
24#define VSIZE 4
25
sugoi24dcac22014-07-07 15:09:48 -070026namespace skiagm {
27/**
28 * This GM directly exercises GrYUVtoRGBEffect.
29 */
30class YUVtoRGBEffect : public GM {
31public:
32 YUVtoRGBEffect() {
33 this->setBGColor(0xFFFFFFFF);
34 }
35
36protected:
mtklein36352bf2015-03-25 18:17:31 -070037 SkString onShortName() override {
sugoi24dcac22014-07-07 15:09:48 -070038 return SkString("yuv_to_rgb_effect");
39 }
40
mtklein36352bf2015-03-25 18:17:31 -070041 SkISize onISize() override {
rileya13400392015-07-20 15:00:03 -070042 return SkISize::Make(238, 120);
sugoi24dcac22014-07-07 15:09:48 -070043 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 void onOnceBeforeDraw() override {
sugoi4ccce7e2015-02-13 13:57:09 -080046 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
47 fBmp[0].allocPixels(yinfo);
48 SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE);
49 fBmp[1].allocPixels(uinfo);
50 SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE);
51 fBmp[2].allocPixels(vinfo);
sugoi24dcac22014-07-07 15:09:48 -070052 unsigned char* pixels[3];
53 for (int i = 0; i < 3; ++i) {
54 pixels[i] = (unsigned char*)fBmp[i].getPixels();
55 }
56 int color[] = {0, 85, 170};
57 const int limit[] = {255, 0, 255};
58 const int invl[] = {0, 255, 0};
59 const int inc[] = {1, -1, 1};
sugoi4ccce7e2015-02-13 13:57:09 -080060 for (int i = 0; i < 3; ++i) {
61 const size_t nbBytes = fBmp[i].rowBytes() * fBmp[i].height();
62 for (size_t j = 0; j < nbBytes; ++j) {
sugoi24dcac22014-07-07 15:09:48 -070063 pixels[i][j] = (unsigned char)color[i];
64 color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i];
65 }
66 }
67 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 void onDraw(SkCanvas* canvas) override {
sugoi24dcac22014-07-07 15:09:48 -070070 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
halcanary96fcdcc2015-08-27 07:41:13 -070071 if (nullptr == rt) {
sugoi24dcac22014-07-07 15:09:48 -070072 return;
73 }
74 GrContext* context = rt->getContext();
halcanary96fcdcc2015-08-27 07:41:13 -070075 if (nullptr == context) {
halcanary2a243382015-09-09 08:16:41 -070076 skiagm::GM::DrawGpuOnlyMessage(canvas);
sugoi24dcac22014-07-07 15:09:48 -070077 return;
78 }
79
80 GrTestTarget tt;
81 context->getTestTarget(&tt);
halcanary96fcdcc2015-08-27 07:41:13 -070082 if (nullptr == tt.target()) {
sugoi24dcac22014-07-07 15:09:48 -070083 SkDEBUGFAIL("Couldn't get Gr test target.");
84 return;
85 }
86
bsalomonbcf0a522014-10-08 08:40:09 -070087 SkAutoTUnref<GrTexture> texture[3];
halcanary96fcdcc2015-08-27 07:41:13 -070088 texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0], nullptr));
89 texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1], nullptr));
90 texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[2], nullptr));
bsalomonbcf0a522014-10-08 08:40:09 -070091
92 if (!texture[0] || !texture[1] || !texture[2]) {
sugoi24dcac22014-07-07 15:09:48 -070093 return;
94 }
95
96 static const SkScalar kDrawPad = 10.f;
97 static const SkScalar kTestPad = 10.f;
sugoi4ccce7e2015-02-13 13:57:09 -080098 static const SkScalar kColorSpaceOffset = 36.f;
99 SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
sugoi24dcac22014-07-07 15:09:48 -0700100
rileyaabaef862014-09-12 17:45:58 -0700101 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace;
102 ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -0700103 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
104 SkIntToScalar(fBmp[0].height()));
105 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -0700106
bsalomonbcf0a522014-10-08 08:40:09 -0700107 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
108 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700109
bsalomonbcf0a522014-10-08 08:40:09 -0700110 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
111 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700112
bsalomonbcf0a522014-10-08 08:40:09 -0700113 for (int i = 0; i < 6; ++i) {
joshualitt2cdec312015-07-09 07:31:31 -0700114 GrPipelineBuilder pipelineBuilder;
bsalomonbcf0a522014-10-08 08:40:09 -0700115 SkAutoTUnref<GrFragmentProcessor> fp(
joshualitt2cdec312015-07-09 07:31:31 -0700116 GrYUVtoRGBEffect::Create(pipelineBuilder.getProcessorDataManager(),
117 texture[indices[i][0]],
sugoi4ccce7e2015-02-13 13:57:09 -0800118 texture[indices[i][1]],
119 texture[indices[i][2]],
120 sizes,
121 static_cast<SkYUVColorSpace>(space)));
bsalomonbcf0a522014-10-08 08:40:09 -0700122 if (fp) {
123 SkMatrix viewMatrix;
124 viewMatrix.setTranslate(x, y);
egdaniel8dd688b2015-01-22 10:16:09 -0800125 pipelineBuilder.setRenderTarget(rt);
bsalomonac856c92015-08-27 06:30:17 -0700126 pipelineBuilder.addColorFragmentProcessor(fp);
joshualittd2b23e02015-08-21 10:53:34 -0700127 tt.target()->drawNonAARect(pipelineBuilder,
128 GrColor_WHITE,
129 viewMatrix,
130 renderRect);
bsalomonbcf0a522014-10-08 08:40:09 -0700131 }
132 x += renderRect.width() + kTestPad;
133 }
sugoi24dcac22014-07-07 15:09:48 -0700134 }
bsalomonbcf0a522014-10-08 08:40:09 -0700135 }
sugoi24dcac22014-07-07 15:09:48 -0700136
137private:
138 SkBitmap fBmp[3];
139
140 typedef GM INHERITED;
141};
142
halcanary385fe4d2015-08-26 13:07:48 -0700143DEF_GM(return new YUVtoRGBEffect;)
sugoi24dcac22014-07-07 15:09:48 -0700144}
145
146#endif