Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: tests/SkColor4fTest.cpp

Issue 1622923002: Revert of expand unitests for color4f (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkColor.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkColor.h" 8 #include "SkColor.h"
9 #include "SkShader.h"
9 #include "SkColorMatrixFilter.h" 10 #include "SkColorMatrixFilter.h"
10 #include "SkGradientShader.h"
11 #include "SkImage.h"
12 #include "SkShader.h"
13
14 #include "Test.h" 11 #include "Test.h"
15 #include "SkRandom.h" 12 #include "SkRandom.h"
16 13
17 const float kTolerance = 1.0f / (1 << 20);
18
19 static bool nearly_equal(float a, float b, float tol = kTolerance) {
20 SkASSERT(tol >= 0);
21 return fabsf(a - b) <= tol;
22 }
23
24 static bool nearly_equal(const SkPM4f a, const SkPM4f& b, float tol = kTolerance ) {
25 for (int i = 0; i < 4; ++i) {
26 if (!nearly_equal(a.fVec[i], b.fVec[i], tol)) {
27 return false;
28 }
29 }
30 return true;
31 }
32
33 DEF_TEST(SkColor4f_FromColor, reporter) { 14 DEF_TEST(SkColor4f_FromColor, reporter) {
34 const struct { 15 const struct {
35 SkColor fC; 16 SkColor fC;
36 SkColor4f fC4; 17 SkColor4f fC4;
37 } recs[] = { 18 } recs[] = {
38 { SK_ColorBLACK, { 1, 0, 0, 0 } }, 19 { SK_ColorBLACK, { 1, 0, 0, 0 } },
39 { SK_ColorWHITE, { 1, 1, 1, 1 } }, 20 { SK_ColorWHITE, { 1, 1, 1, 1 } },
40 { SK_ColorRED, { 1, 1, 0, 0 } }, 21 { SK_ColorRED, { 1, 1, 0, 0 } },
41 { SK_ColorGREEN, { 1, 0, 1, 0 } }, 22 { SK_ColorGREEN, { 1, 0, 1, 0 } },
42 { SK_ColorBLUE, { 1, 0, 0, 1 } }, 23 { SK_ColorBLUE, { 1, 0, 0, 1 } },
43 { 0, { 0, 0, 0, 0 } }, 24 { 0, { 0, 0, 0, 0 } },
44 { 0x55AAFF00, { 1/3.0f, 2/3.0f, 1, 0 } }, 25 { 0x55AAFF00, { 1/3.0f, 2/3.0f, 1, 0 } },
45 }; 26 };
46 27
47 for (const auto& r : recs) { 28 for (const auto& r : recs) {
48 SkColor4f c4 = SkColor4f::FromColor(r.fC); 29 SkColor4f c4 = SkColor4f::FromColor(r.fC);
49 REPORTER_ASSERT(reporter, c4 == r.fC4); 30 REPORTER_ASSERT(reporter, c4 == r.fC4);
50 } 31 }
51 } 32 }
52 33
53 DEF_TEST(Color4f_premul, reporter) { 34 static bool nearly_equal(float a, float b) {
35 const float kTolerance = 1.0f / (1 << 20);
36 return fabsf(a - b) < kTolerance;
37 }
38
39 DEF_TEST(SkColor4f_premul, reporter) {
54 SkRandom rand; 40 SkRandom rand;
55 41
56 for (int i = 0; i < 1000000; ++i) { 42 for (int i = 0; i < 1000000; ++i) {
57 // First just test opaque colors, so that the premul should be exact 43 // First just test opaque colors, so that the premul should be exact
58 SkColor4f c4 { 44 SkColor4f c4 {
59 1, rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1() 45 1, rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1()
60 }; 46 };
61 SkPM4f pm4 = c4.premul(); 47 SkPM4f pm4 = c4.premul();
62 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); 48 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA);
63 REPORTER_ASSERT(reporter, pm4.fVec[SK_R_INDEX] == c4.fA * c4.fR); 49 REPORTER_ASSERT(reporter, pm4.fVec[SK_R_INDEX] == c4.fA * c4.fR);
64 REPORTER_ASSERT(reporter, pm4.fVec[SK_G_INDEX] == c4.fA * c4.fG); 50 REPORTER_ASSERT(reporter, pm4.fVec[SK_G_INDEX] == c4.fA * c4.fG);
65 REPORTER_ASSERT(reporter, pm4.fVec[SK_B_INDEX] == c4.fA * c4.fB); 51 REPORTER_ASSERT(reporter, pm4.fVec[SK_B_INDEX] == c4.fA * c4.fB);
66 52
67 // We compare with a tolerance, in case our premul multiply is implement ed at slightly 53 // We compare with a tolerance, in case our premul multiply is implement ed at slightly
68 // different precision than the test code. 54 // different precision than the test code.
69 c4.fA = rand.nextUScalar1(); 55 c4.fA = rand.nextUScalar1();
70 pm4 = c4.premul(); 56 pm4 = c4.premul();
71 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); 57 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA);
72 REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_R_INDEX], c4.fA * c4. fR)); 58 REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_R_INDEX], c4.fA * c4. fR));
73 REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_G_INDEX], c4.fA * c4. fG)); 59 REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_G_INDEX], c4.fA * c4. fG));
74 REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_B_INDEX], c4.fA * c4. fB)); 60 REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_B_INDEX], c4.fA * c4. fB));
75 } 61 }
76 } 62 }
77
78 //////////////////////////////////////////////////////////////////////////////// //////////////////
79
80 static SkShader* make_color() { return SkShader::CreateColorShader(0xFFBB8855); }
81
82 static SkShader* make_image() {
83 const SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
84 const SkPMColor pixels[] {
85 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
86 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
87 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
88 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
89 };
90 SkAutoTUnref<SkImage> image(SkImage::NewRasterCopy(info, pixels, sizeof(SkPM Color) * 2));
91 return image->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode );
92 }
93
94 static SkShader* make_grad() {
95 const SkPoint pts[] {{ 0, 0 }, { 100, 100 }};
96 const SkColor colors[] { SK_ColorRED, SK_ColorBLUE };
97 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kCl amp_TileMode);
98 }
99
100 static void compare_spans(const SkPM4f span4f[], const SkPMColor span4b[], int c ount,
101 skiatest::Reporter* reporter) {
102 for (int i = 0; i < count; ++i) {
103 SkPM4f c0 = SkPM4f::FromPMColor(span4b[i]);
104 SkPM4f c1 = span4f[i];
105 REPORTER_ASSERT(reporter, nearly_equal(c0, c1, 1.0f/255));
106 }
107 }
108
109 DEF_TEST(Color4f_shader, reporter) {
110 struct {
111 SkShader* (*fFact)();
112 bool fSupports4f;
113 } recs[] = {
114 { make_color, true },
115 { make_grad, false },
116 { make_image, false },
117 };
118
119 SkPaint paint;
120 for (const auto& rec : recs) {
121 uint32_t storage[200];
122 paint.setShader(rec.fFact())->unref();
123 SkASSERT(paint.getShader()->contextSize() <= sizeof(storage));
124 SkShader::Context* ctx = paint.getShader()->createContext({paint, SkMatr ix::I(), nullptr},
125 storage);
126 REPORTER_ASSERT(reporter, ctx->supports4f() == rec.fSupports4f);
127 if (ctx->supports4f()) {
128 const int N = 100;
129 SkPM4f buffer4f[N];
130 ctx->shadeSpan4f(0, 0, buffer4f, N);
131 SkPMColor buffer4b[N];
132 ctx->shadeSpan(0, 0, buffer4b, N);
133 compare_spans(buffer4f, buffer4b, N, reporter);
134 }
135 ctx->SkShader::Context::~Context();
136 }
137 }
138
139 static SkColorFilter* make_mode_cf() {
140 return SkColorFilter::CreateModeFilter(0xFFBB8855, SkXfermode::kPlus_Mode);
141 }
142
143 static SkColorFilter* make_mx_cf() {
144 const float mx[] = {
145 0.5f, 0, 0, 0, 0.1f,
146 0, 0.5f, 0, 0, 0.2f,
147 0, 0, 1, 0, -0.1f,
148 0, 0, 0, 1, 0,
149 };
150 return SkColorMatrixFilter::Create(mx);
151 }
152
153 static SkColorFilter* make_compose_cf() {
154 SkAutoTUnref<SkColorFilter> cf0(make_mode_cf());
155 SkAutoTUnref<SkColorFilter> cf1(make_mx_cf());
156 return SkColorFilter::CreateComposeFilter(cf0, cf1);
157 }
158
159 DEF_TEST(Color4f_colorfilter, reporter) {
160 struct {
161 SkColorFilter* (*fFact)();
162 bool fSupports4f;
163 } recs[] = {
164 { make_mode_cf, false },
165 { make_mx_cf, true },
166 { make_compose_cf, false },
167 };
168
169 // prepare the src
170 const int N = 100;
171 SkPMColor src4b[N];
172 SkPM4f src4f[N];
173 SkRandom rand;
174 for (int i = 0; i < N; ++i) {
175 src4b[i] = SkPreMultiplyColor(rand.nextU());
176 src4f[i] = SkPM4f::FromPMColor(src4b[i]);
177 }
178 // confirm that our srcs are (nearly) equal
179 compare_spans(src4f, src4b, N, reporter);
180
181 for (const auto& rec : recs) {
182 SkAutoTUnref<SkColorFilter> filter(rec.fFact());
183 REPORTER_ASSERT(reporter, filter->supports4f() == rec.fSupports4f);
184 if (filter->supports4f()) {
185 SkPMColor dst4b[N];
186 filter->filterSpan(src4b, N, dst4b);
187 SkPM4f dst4f[N];
188 filter->filterSpan4f(src4f, N, dst4f);
189 compare_spans(dst4f, dst4b, N, reporter);
190 }
191 }
192 }
OLDNEW
« no previous file with comments | « src/core/SkColor.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698