blob: 1a2c9c9f39d14134e48ec9a4dc83eb0dbec380ad [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
22namespace skiagm {
23/**
24 * This GM directly exercises GrYUVtoRGBEffect.
25 */
26class YUVtoRGBEffect : public GM {
27public:
28 YUVtoRGBEffect() {
29 this->setBGColor(0xFFFFFFFF);
30 }
31
32protected:
mtklein72c9faa2015-01-09 10:06:39 -080033 SkString onShortName() SK_OVERRIDE {
sugoi24dcac22014-07-07 15:09:48 -070034 return SkString("yuv_to_rgb_effect");
35 }
36
mtklein72c9faa2015-01-09 10:06:39 -080037 SkISize onISize() SK_OVERRIDE {
rileyaabaef862014-09-12 17:45:58 -070038 return SkISize::Make(334, 128);
sugoi24dcac22014-07-07 15:09:48 -070039 }
40
mtklein72c9faa2015-01-09 10:06:39 -080041 void onOnceBeforeDraw() SK_OVERRIDE {
sugoi24dcac22014-07-07 15:09:48 -070042 SkImageInfo info = SkImageInfo::MakeA8(24, 24);
43 fBmp[0].allocPixels(info);
44 fBmp[1].allocPixels(info);
45 fBmp[2].allocPixels(info);
46 unsigned char* pixels[3];
47 for (int i = 0; i < 3; ++i) {
48 pixels[i] = (unsigned char*)fBmp[i].getPixels();
49 }
50 int color[] = {0, 85, 170};
51 const int limit[] = {255, 0, 255};
52 const int invl[] = {0, 255, 0};
53 const int inc[] = {1, -1, 1};
54 for (int j = 0; j < 576; ++j) {
55 for (int i = 0; i < 3; ++i) {
56 pixels[i][j] = (unsigned char)color[i];
57 color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i];
58 }
59 }
60 }
61
mtklein72c9faa2015-01-09 10:06:39 -080062 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
sugoi24dcac22014-07-07 15:09:48 -070063 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
64 if (NULL == rt) {
65 return;
66 }
67 GrContext* context = rt->getContext();
68 if (NULL == context) {
69 return;
70 }
71
72 GrTestTarget tt;
73 context->getTestTarget(&tt);
74 if (NULL == tt.target()) {
75 SkDEBUGFAIL("Couldn't get Gr test target.");
76 return;
77 }
78
bsalomonbcf0a522014-10-08 08:40:09 -070079 SkAutoTUnref<GrTexture> texture[3];
80 texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0], NULL));
81 texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1], NULL));
82 texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[2], NULL));
83
84 if (!texture[0] || !texture[1] || !texture[2]) {
sugoi24dcac22014-07-07 15:09:48 -070085 return;
86 }
87
88 static const SkScalar kDrawPad = 10.f;
89 static const SkScalar kTestPad = 10.f;
rileyaabaef862014-09-12 17:45:58 -070090 static const SkScalar kColorSpaceOffset = 64.f;
sugoi24dcac22014-07-07 15:09:48 -070091
rileyaabaef862014-09-12 17:45:58 -070092 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace;
93 ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -070094 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
95 SkIntToScalar(fBmp[0].height()));
96 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -070097
bsalomonbcf0a522014-10-08 08:40:09 -070098 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
99 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700100
bsalomonbcf0a522014-10-08 08:40:09 -0700101 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
102 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700103
bsalomonbcf0a522014-10-08 08:40:09 -0700104 for (int i = 0; i < 6; ++i) {
105 SkAutoTUnref<GrFragmentProcessor> fp(
106 GrYUVtoRGBEffect::Create(texture[indices[i][0]],
107 texture[indices[i][1]],
108 texture[indices[i][2]],
109 static_cast<SkYUVColorSpace>(space)));
110 if (fp) {
111 SkMatrix viewMatrix;
112 viewMatrix.setTranslate(x, y);
egdaniel8dd688b2015-01-22 10:16:09 -0800113 GrPipelineBuilder pipelineBuilder;
114 pipelineBuilder.setRenderTarget(rt);
115 pipelineBuilder.addColorProcessor(fp);
116 tt.target()->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, viewMatrix,
117 renderRect);
bsalomonbcf0a522014-10-08 08:40:09 -0700118 }
119 x += renderRect.width() + kTestPad;
120 }
sugoi24dcac22014-07-07 15:09:48 -0700121 }
bsalomonbcf0a522014-10-08 08:40:09 -0700122 }
sugoi24dcac22014-07-07 15:09:48 -0700123
124private:
125 SkBitmap fBmp[3];
126
127 typedef GM INHERITED;
128};
129
130DEF_GM( return SkNEW(YUVtoRGBEffect); )
131}
132
133#endif