blob: fb0c67d3d10240f59bd140690160470d726360fc [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) {
bsalomonb62da802015-01-31 07:51:14 -080069 this->drawGpuOnlyMessage(canvas);
sugoi24dcac22014-07-07 15:09:48 -070070 return;
71 }
72
73 GrTestTarget tt;
74 context->getTestTarget(&tt);
75 if (NULL == tt.target()) {
76 SkDEBUGFAIL("Couldn't get Gr test target.");
77 return;
78 }
79
bsalomonbcf0a522014-10-08 08:40:09 -070080 SkAutoTUnref<GrTexture> texture[3];
81 texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0], NULL));
82 texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1], NULL));
83 texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[2], NULL));
84
85 if (!texture[0] || !texture[1] || !texture[2]) {
sugoi24dcac22014-07-07 15:09:48 -070086 return;
87 }
88
89 static const SkScalar kDrawPad = 10.f;
90 static const SkScalar kTestPad = 10.f;
rileyaabaef862014-09-12 17:45:58 -070091 static const SkScalar kColorSpaceOffset = 64.f;
sugoi24dcac22014-07-07 15:09:48 -070092
rileyaabaef862014-09-12 17:45:58 -070093 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace;
94 ++space) {
bsalomonbcf0a522014-10-08 08:40:09 -070095 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
96 SkIntToScalar(fBmp[0].height()));
97 renderRect.outset(kDrawPad, kDrawPad);
sugoi24dcac22014-07-07 15:09:48 -070098
bsalomonbcf0a522014-10-08 08:40:09 -070099 SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
100 SkScalar x = kDrawPad + kTestPad;
sugoi24dcac22014-07-07 15:09:48 -0700101
bsalomonbcf0a522014-10-08 08:40:09 -0700102 const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
103 {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
sugoi24dcac22014-07-07 15:09:48 -0700104
bsalomonbcf0a522014-10-08 08:40:09 -0700105 for (int i = 0; i < 6; ++i) {
106 SkAutoTUnref<GrFragmentProcessor> fp(
107 GrYUVtoRGBEffect::Create(texture[indices[i][0]],
108 texture[indices[i][1]],
109 texture[indices[i][2]],
110 static_cast<SkYUVColorSpace>(space)));
111 if (fp) {
112 SkMatrix viewMatrix;
113 viewMatrix.setTranslate(x, y);
egdaniel8dd688b2015-01-22 10:16:09 -0800114 GrPipelineBuilder pipelineBuilder;
115 pipelineBuilder.setRenderTarget(rt);
116 pipelineBuilder.addColorProcessor(fp);
117 tt.target()->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, viewMatrix,
118 renderRect);
bsalomonbcf0a522014-10-08 08:40:09 -0700119 }
120 x += renderRect.width() + kTestPad;
121 }
sugoi24dcac22014-07-07 15:09:48 -0700122 }
bsalomonbcf0a522014-10-08 08:40:09 -0700123 }
sugoi24dcac22014-07-07 15:09:48 -0700124
125private:
126 SkBitmap fBmp[3];
127
128 typedef GM INHERITED;
129};
130
131DEF_GM( return SkNEW(YUVtoRGBEffect); )
132}
133
134#endif