blob: cf8fbee20635614d275c9ae93c07298dd3bcdee1 [file] [log] [blame]
reed@google.com83226972012-06-07 20:26:47 +00001
2/*
3 * Copyright 2011 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#include "Test.h"
junov@chromium.orge94b5e42013-01-30 15:52:06 +00009#include "SkDevice.h"
reed@google.com83226972012-06-07 20:26:47 +000010#include "SkTemplates.h"
11#include "SkShader.h"
12#include "SkColorShader.h"
13#include "SkEmptyShader.h"
14#include "SkGradientShader.h"
15
16struct GradRec {
17 int fColorCount;
18 const SkColor* fColors;
19 const SkScalar* fPos;
20 const SkPoint* fPoint; // 2
21 const SkScalar* fRadius; // 2
22 SkShader::TileMode fTileMode;
23
24 void gradCheck(skiatest::Reporter* reporter, SkShader* shader,
25 SkShader::GradientInfo* info,
26 SkShader::GradientType gt) const {
27 SkAutoTMalloc<SkColor> colorStorage(fColorCount);
28 SkAutoTMalloc<SkScalar> posStorage(fColorCount);
29
30 info->fColorCount = fColorCount;
31 info->fColors = colorStorage;
32 info->fColorOffsets = posStorage.get();
33 REPORTER_ASSERT(reporter, shader->asAGradient(info) == gt);
34
35 REPORTER_ASSERT(reporter, info->fColorCount == fColorCount);
36 REPORTER_ASSERT(reporter,
37 !memcmp(info->fColors, fColors, fColorCount * sizeof(SkColor)));
38 REPORTER_ASSERT(reporter,
39 !memcmp(info->fColorOffsets, fPos, fColorCount * sizeof(SkScalar)));
40 REPORTER_ASSERT(reporter, fTileMode == info->fTileMode);
41 }
42};
43
44
sugoi@google.come0e385c2013-03-11 18:50:03 +000045static void none_gradproc(skiatest::Reporter* reporter, const GradRec&) {
reed@google.com83226972012-06-07 20:26:47 +000046 SkAutoTUnref<SkShader> s(new SkEmptyShader);
47 REPORTER_ASSERT(reporter, SkShader::kNone_GradientType == s->asAGradient(NULL));
48}
49
50static void color_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
51 SkAutoTUnref<SkShader> s(new SkColorShader(rec.fColors[0]));
52 REPORTER_ASSERT(reporter, SkShader::kColor_GradientType == s->asAGradient(NULL));
53
54 SkShader::GradientInfo info;
robertphillips@google.com37ebe3f2013-03-04 20:03:44 +000055 info.fColors = NULL;
reed@google.com83226972012-06-07 20:26:47 +000056 info.fColorCount = 0;
57 s->asAGradient(&info);
58 REPORTER_ASSERT(reporter, 1 == info.fColorCount);
59}
60
61static void linear_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
62 SkAutoTUnref<SkShader> s(SkGradientShader::CreateLinear(rec.fPoint,
63 rec.fColors,
64 rec.fPos,
65 rec.fColorCount,
66 rec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +000067
reed@google.com83226972012-06-07 20:26:47 +000068 SkShader::GradientInfo info;
69 rec.gradCheck(reporter, s, &info, SkShader::kLinear_GradientType);
70 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint)));
71}
72
73static void radial_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
74 SkAutoTUnref<SkShader> s(SkGradientShader::CreateRadial(rec.fPoint[0],
75 rec.fRadius[0],
76 rec.fColors,
77 rec.fPos,
78 rec.fColorCount,
79 rec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
reed@google.com83226972012-06-07 20:26:47 +000081 SkShader::GradientInfo info;
82 rec.gradCheck(reporter, s, &info, SkShader::kRadial_GradientType);
83 REPORTER_ASSERT(reporter, info.fPoint[0] == rec.fPoint[0]);
84 REPORTER_ASSERT(reporter, info.fRadius[0] == rec.fRadius[0]);
85}
86
87static void radial2_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
88 SkAutoTUnref<SkShader> s(SkGradientShader::CreateTwoPointRadial(rec.fPoint[0],
89 rec.fRadius[0],
90 rec.fPoint[1],
91 rec.fRadius[1],
92 rec.fColors,
93 rec.fPos,
94 rec.fColorCount,
95 rec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +000096
reed@google.com83226972012-06-07 20:26:47 +000097 SkShader::GradientInfo info;
98 rec.gradCheck(reporter, s, &info, SkShader::kRadial2_GradientType);
99 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint)));
100 REPORTER_ASSERT(reporter, !memcmp(info.fRadius, rec.fRadius, 2 * sizeof(SkScalar)));
101}
102
103static void sweep_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
104 SkAutoTUnref<SkShader> s(SkGradientShader::CreateSweep(rec.fPoint[0].fX,
105 rec.fPoint[0].fY,
106 rec.fColors,
107 rec.fPos,
108 rec.fColorCount));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000109
reed@google.com83226972012-06-07 20:26:47 +0000110 SkShader::GradientInfo info;
111 rec.gradCheck(reporter, s, &info, SkShader::kSweep_GradientType);
112 REPORTER_ASSERT(reporter, info.fPoint[0] == rec.fPoint[0]);
113}
114
115static void conical_gradproc(skiatest::Reporter* reporter, const GradRec& rec) {
116 SkAutoTUnref<SkShader> s(SkGradientShader::CreateTwoPointConical(rec.fPoint[0],
117 rec.fRadius[0],
118 rec.fPoint[1],
119 rec.fRadius[1],
120 rec.fColors,
121 rec.fPos,
122 rec.fColorCount,
123 rec.fTileMode));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124
reed@google.com83226972012-06-07 20:26:47 +0000125 SkShader::GradientInfo info;
126 rec.gradCheck(reporter, s, &info, SkShader::kConical_GradientType);
127 REPORTER_ASSERT(reporter, !memcmp(info.fPoint, rec.fPoint, 2 * sizeof(SkPoint)));
128 REPORTER_ASSERT(reporter, !memcmp(info.fRadius, rec.fRadius, 2 * sizeof(SkScalar)));
129}
130
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000131// Ensure that repeated color gradients behave like drawing a single color
sugoi@google.come0e385c2013-03-11 18:50:03 +0000132static void TestConstantGradient(skiatest::Reporter*) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000133 const SkPoint pts[] = {
134 { 0, 0 },
135 { SkIntToScalar(10), 0 }
136 };
137 SkColor colors[] = { SK_ColorBLUE, SK_ColorBLUE };
138 const SkScalar pos[] = { 0, SK_Scalar1 };
139 SkAutoTUnref<SkShader> s(SkGradientShader::CreateLinear(pts,
140 colors,
141 pos,
142 2,
143 SkShader::kClamp_TileMode));
144 SkBitmap outBitmap;
145 outBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 1);
146 outBitmap.allocPixels();
147 SkPaint paint;
148 paint.setShader(s.get());
149 SkDevice device(outBitmap);
150 SkCanvas canvas(&device);
151 canvas.drawPaint(paint);
152 SkAutoLockPixels alp(outBitmap);
153 for (int i = 0; i < 10; i++) {
154 // The following is commented out because it currently fails
155 // Related bug: https://code.google.com/p/skia/issues/detail?id=1098
156
157 // REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0));
158 }
159}
160
reed@google.com83226972012-06-07 20:26:47 +0000161typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&);
162
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000163static void TestGradientShaders(skiatest::Reporter* reporter) {
reed@google.com83226972012-06-07 20:26:47 +0000164 static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
165 static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
166 static const SkPoint gPts[] = {
167 { 0, 0 },
168 { SkIntToScalar(10), SkIntToScalar(20) }
169 };
170 static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) };
171
172 GradRec rec;
173 rec.fColorCount = SK_ARRAY_COUNT(gColors);
174 rec.fColors = gColors;
175 rec.fPos = gPos;
176 rec.fPoint = gPts;
177 rec.fRadius = gRad;
178 rec.fTileMode = SkShader::kClamp_TileMode;
179
180 static const GradProc gProcs[] = {
181 none_gradproc,
182 color_gradproc,
183 linear_gradproc,
184 radial_gradproc,
185 radial2_gradproc,
186 sweep_gradproc,
187 conical_gradproc,
188 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000189
reed@google.com83226972012-06-07 20:26:47 +0000190 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
191 gProcs[i](reporter, rec);
192 }
193}
194
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000195static void TestGradients(skiatest::Reporter* reporter) {
196 TestGradientShaders(reporter);
197 TestConstantGradient(reporter);
198}
reed@google.com83226972012-06-07 20:26:47 +0000199#include "TestClassDef.h"
200DEFINE_TESTCLASS("Gradients", TestGradientsClass, TestGradients)