blob: 3e400103fbd1f1e56f465f08ce06b049676eece2 [file] [log] [blame]
reed@google.com83226972012-06-07 20:26:47 +00001/*
2 * Copyright 2011 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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00008#include "SkBitmapDevice.h"
reed@google.com83226972012-06-07 20:26:47 +00009#include "SkColorShader.h"
10#include "SkEmptyShader.h"
11#include "SkGradientShader.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000012#include "SkShader.h"
13#include "SkTemplates.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000014#include "Test.h"
reed@google.com83226972012-06-07 20:26:47 +000015
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;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000145 outBitmap.allocN32Pixels(10, 1);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000146 SkPaint paint;
147 paint.setShader(s.get());
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000148 SkCanvas canvas(outBitmap);
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000149 canvas.drawPaint(paint);
150 SkAutoLockPixels alp(outBitmap);
151 for (int i = 0; i < 10; i++) {
152 // The following is commented out because it currently fails
153 // Related bug: https://code.google.com/p/skia/issues/detail?id=1098
154
155 // REPORTER_ASSERT(reporter, SK_ColorBLUE == outBitmap.getColor(i, 0));
156 }
157}
158
reed@google.com83226972012-06-07 20:26:47 +0000159typedef void (*GradProc)(skiatest::Reporter* reporter, const GradRec&);
160
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000161static void TestGradientShaders(skiatest::Reporter* reporter) {
reed@google.com83226972012-06-07 20:26:47 +0000162 static const SkColor gColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
163 static const SkScalar gPos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
164 static const SkPoint gPts[] = {
165 { 0, 0 },
166 { SkIntToScalar(10), SkIntToScalar(20) }
167 };
168 static const SkScalar gRad[] = { SkIntToScalar(1), SkIntToScalar(2) };
169
170 GradRec rec;
171 rec.fColorCount = SK_ARRAY_COUNT(gColors);
172 rec.fColors = gColors;
173 rec.fPos = gPos;
174 rec.fPoint = gPts;
175 rec.fRadius = gRad;
176 rec.fTileMode = SkShader::kClamp_TileMode;
177
178 static const GradProc gProcs[] = {
179 none_gradproc,
180 color_gradproc,
181 linear_gradproc,
182 radial_gradproc,
183 radial2_gradproc,
184 sweep_gradproc,
185 conical_gradproc,
186 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187
reed@google.com83226972012-06-07 20:26:47 +0000188 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
189 gProcs[i](reporter, rec);
190 }
191}
192
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000193DEF_TEST(Gradient, reporter) {
junov@chromium.orge94b5e42013-01-30 15:52:06 +0000194 TestGradientShaders(reporter);
195 TestConstantGradient(reporter);
196}