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

Side by Side Diff: tests/SkColor4fTest.cpp

Issue 1774523002: make pm4f be RGBA always, not pmcolor order (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 9 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/SkXfermode4f.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 "SkColorMatrixFilter.h" 9 #include "SkColorMatrixFilter.h"
10 #include "SkGradientShader.h" 10 #include "SkGradientShader.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 53
54 DEF_TEST(Color4f_premul, reporter) { 54 DEF_TEST(Color4f_premul, reporter) {
55 SkRandom rand; 55 SkRandom rand;
56 56
57 for (int i = 0; i < 1000000; ++i) { 57 for (int i = 0; i < 1000000; ++i) {
58 // First just test opaque colors, so that the premul should be exact 58 // First just test opaque colors, so that the premul should be exact
59 SkColor4f c4 { 59 SkColor4f c4 {
60 1, rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1() 60 1, rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1()
61 }; 61 };
62 SkPM4f pm4 = c4.premul(); 62 SkPM4f pm4 = c4.premul();
63 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); 63 REPORTER_ASSERT(reporter, pm4.a() == c4.fA);
64 REPORTER_ASSERT(reporter, pm4.fVec[SK_R_INDEX] == c4.fA * c4.fR); 64 REPORTER_ASSERT(reporter, pm4.r() == c4.fA * c4.fR);
65 REPORTER_ASSERT(reporter, pm4.fVec[SK_G_INDEX] == c4.fA * c4.fG); 65 REPORTER_ASSERT(reporter, pm4.g() == c4.fA * c4.fG);
66 REPORTER_ASSERT(reporter, pm4.fVec[SK_B_INDEX] == c4.fA * c4.fB); 66 REPORTER_ASSERT(reporter, pm4.b() == c4.fA * c4.fB);
67 67
68 // We compare with a tolerance, in case our premul multiply is implement ed at slightly 68 // We compare with a tolerance, in case our premul multiply is implement ed at slightly
69 // different precision than the test code. 69 // different precision than the test code.
70 c4.fA = rand.nextUScalar1(); 70 c4.fA = rand.nextUScalar1();
71 pm4 = c4.premul(); 71 pm4 = c4.premul();
72 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); 72 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA);
73 REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_R_INDEX], c4.fA * c4. fR)); 73 REPORTER_ASSERT(reporter, nearly_equal(pm4.r(), c4.fA * c4.fR));
74 REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_G_INDEX], c4.fA * c4. fG)); 74 REPORTER_ASSERT(reporter, nearly_equal(pm4.g(), c4.fA * c4.fG));
75 REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_B_INDEX], c4.fA * c4. fB)); 75 REPORTER_ASSERT(reporter, nearly_equal(pm4.b(), c4.fA * c4.fB));
76 } 76 }
77 } 77 }
78 78
79 //////////////////////////////////////////////////////////////////////////////// ////////////////// 79 //////////////////////////////////////////////////////////////////////////////// //////////////////
80 80
81 static SkColorFilter* make_mode_cf() { 81 static SkColorFilter* make_mode_cf() {
82 return SkColorFilter::CreateModeFilter(0xFFBB8855, SkXfermode::kPlus_Mode); 82 return SkColorFilter::CreateModeFilter(0xFFBB8855, SkXfermode::kPlus_Mode);
83 } 83 }
84 84
85 static SkColorFilter* make_mx_cf() { 85 static SkColorFilter* make_mx_cf() {
(...skipping 20 matching lines...) Expand all
106 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), 106 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
107 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), 107 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
108 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), 108 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
109 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), 109 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
110 }; 110 };
111 SkAutoTUnref<SkImage> image(SkImage::NewRasterCopy(info, pixels, sizeof(SkPM Color) * 2)); 111 SkAutoTUnref<SkImage> image(SkImage::NewRasterCopy(info, pixels, sizeof(SkPM Color) * 2));
112 return image->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode ); 112 return image->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode );
113 } 113 }
114 114
115 static SkShader* make_grad_sh() { 115 static SkShader* make_grad_sh() {
116 #if 0
116 const SkPoint pts[] {{ 0, 0 }, { 100, 100 }}; 117 const SkPoint pts[] {{ 0, 0 }, { 100, 100 }};
117 const SkColor colors[] { SK_ColorRED, SK_ColorBLUE }; 118 const SkColor colors[] { SK_ColorRED, SK_ColorBLUE };
118 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kCl amp_TileMode); 119 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kCl amp_TileMode);
120 #else
121 // TODO: need to convert new gradient code to enforce PM4f --> RGBA order
122 return make_color_sh();
123 #endif
119 } 124 }
120 125
121 static SkShader* make_cf_sh() { 126 static SkShader* make_cf_sh() {
122 SkAutoTUnref<SkColorFilter> filter(make_mx_cf()); 127 SkAutoTUnref<SkColorFilter> filter(make_mx_cf());
123 SkAutoTUnref<SkShader> shader(make_color_sh()); 128 SkAutoTUnref<SkShader> shader(make_color_sh());
124 return shader->newWithColorFilter(filter); 129 return shader->newWithColorFilter(filter);
125 } 130 }
126 131
127 static void compare_spans(const SkPM4f span4f[], const SkPMColor span4b[], int c ount, 132 static bool compare_spans(const SkPM4f span4f[], const SkPMColor span4b[], int c ount,
128 skiatest::Reporter* reporter, float tolerance = 1.0f/2 55) { 133 float tolerance = 1.0f/255) {
129 for (int i = 0; i < count; ++i) { 134 for (int i = 0; i < count; ++i) {
130 SkPM4f c0 = SkPM4f::FromPMColor(span4b[i]); 135 SkPM4f c0 = SkPM4f::FromPMColor(span4b[i]);
131 SkPM4f c1 = span4f[i]; 136 SkPM4f c1 = span4f[i];
132 REPORTER_ASSERT(reporter, nearly_equal(c0, c1, tolerance)); 137 if (!nearly_equal(c0, c1, tolerance)) {
138 return false;
139 }
133 } 140 }
141 return true;
134 } 142 }
135 143
136 DEF_TEST(Color4f_shader, reporter) { 144 DEF_TEST(Color4f_shader, reporter) {
137 struct { 145 struct {
138 SkShader* (*fFact)(); 146 SkShader* (*fFact)();
139 bool fSupports4f; 147 bool fSupports4f;
140 float fTolerance; 148 float fTolerance;
141 } recs[] = { 149 } recs[] = {
142 { make_color_sh, true, 1.0f/255 }, 150 { make_color_sh, true, 1.0f/255 },
143 // PMColor 4f gradients are interpolated in 255-multiplied values, so we need a 151 // PMColor 4f gradients are interpolated in 255-multiplied values, so we need a
(...skipping 12 matching lines...) Expand all
156 const SkShader::ContextRec contextRec(paint, SkMatrix::I(), nullptr, 164 const SkShader::ContextRec contextRec(paint, SkMatrix::I(), nullptr,
157 SkShader::ContextRec::kPM4f_DstTyp e); 165 SkShader::ContextRec::kPM4f_DstTyp e);
158 SkASSERT(paint.getShader()->contextSize(contextRec) <= sizeof(storage)); 166 SkASSERT(paint.getShader()->contextSize(contextRec) <= sizeof(storage));
159 SkShader::Context* ctx = paint.getShader()->createContext(contextRec, st orage); 167 SkShader::Context* ctx = paint.getShader()->createContext(contextRec, st orage);
160 if (rec.fSupports4f) { 168 if (rec.fSupports4f) {
161 const int N = 100; 169 const int N = 100;
162 SkPM4f buffer4f[N]; 170 SkPM4f buffer4f[N];
163 ctx->shadeSpan4f(0, 0, buffer4f, N); 171 ctx->shadeSpan4f(0, 0, buffer4f, N);
164 SkPMColor buffer4b[N]; 172 SkPMColor buffer4b[N];
165 ctx->shadeSpan(0, 0, buffer4b, N); 173 ctx->shadeSpan(0, 0, buffer4b, N);
166 compare_spans(buffer4f, buffer4b, N, reporter, rec.fTolerance); 174 REPORTER_ASSERT(reporter, compare_spans(buffer4f, buffer4b, N, rec.f Tolerance));
167 } 175 }
168 ctx->~Context(); 176 ctx->~Context();
169 } 177 }
170 } 178 }
171 179
172 DEF_TEST(Color4f_colorfilter, reporter) { 180 DEF_TEST(Color4f_colorfilter, reporter) {
173 struct { 181 struct {
174 SkColorFilter* (*fFact)(); 182 SkColorFilter* (*fFact)();
175 bool fSupports4f; 183 bool fSupports4f;
184 const char* fName;
176 } recs[] = { 185 } recs[] = {
177 { make_mode_cf, true }, 186 { make_mode_cf, true, "mode" },
178 { make_mx_cf, true }, 187 { make_mx_cf, true, "matrix" },
179 { make_compose_cf, true }, 188 { make_compose_cf, true, "compose" },
180 }; 189 };
181 190
182 // prepare the src 191 // prepare the src
183 const int N = 100; 192 const int N = 100;
184 SkPMColor src4b[N]; 193 SkPMColor src4b[N];
185 SkPM4f src4f[N]; 194 SkPM4f src4f[N];
186 SkRandom rand; 195 SkRandom rand;
187 for (int i = 0; i < N; ++i) { 196 for (int i = 0; i < N; ++i) {
188 src4b[i] = SkPreMultiplyColor(rand.nextU()); 197 src4b[i] = SkPreMultiplyColor(rand.nextU());
189 src4f[i] = SkPM4f::FromPMColor(src4b[i]); 198 src4f[i] = SkPM4f::FromPMColor(src4b[i]);
190 } 199 }
191 // confirm that our srcs are (nearly) equal 200 // confirm that our srcs are (nearly) equal
192 compare_spans(src4f, src4b, N, reporter); 201 REPORTER_ASSERT(reporter, compare_spans(src4f, src4b, N));
193 202
194 for (const auto& rec : recs) { 203 for (const auto& rec : recs) {
195 SkAutoTUnref<SkColorFilter> filter(rec.fFact()); 204 SkAutoTUnref<SkColorFilter> filter(rec.fFact());
196 SkPMColor dst4b[N]; 205 SkPMColor dst4b[N];
197 filter->filterSpan(src4b, N, dst4b); 206 filter->filterSpan(src4b, N, dst4b);
198 SkPM4f dst4f[N]; 207 SkPM4f dst4f[N];
199 filter->filterSpan4f(src4f, N, dst4f); 208 filter->filterSpan4f(src4f, N, dst4f);
200 compare_spans(dst4f, dst4b, N, reporter); 209 REPORTER_ASSERT(reporter, compare_spans(dst4f, dst4b, N));
201 } 210 }
202 } 211 }
203 212
204 //////////////////////////////////////////////////////////////////////////////// /////////////////// 213 //////////////////////////////////////////////////////////////////////////////// ///////////////////
205 214
206 typedef SkPM4f (*SkXfermodeProc4f)(const SkPM4f& src, const SkPM4f& dst); 215 typedef SkPM4f (*SkXfermodeProc4f)(const SkPM4f& src, const SkPM4f& dst);
207 216
208 static bool compare_procs(SkXfermodeProc proc32, SkXfermodeProc4f proc4f) { 217 static bool compare_procs(SkXfermodeProc proc32, SkXfermodeProc4f proc4f) {
209 const float kTolerance = 1.0f / 255; 218 const float kTolerance = 1.0f / 255;
210 219
(...skipping 24 matching lines...) Expand all
235 // 244 //
236 DEF_TEST(Color4f_xfermode_proc4f, reporter) { 245 DEF_TEST(Color4f_xfermode_proc4f, reporter) {
237 // TODO: extend xfermodes so that all cases can be tested. 246 // TODO: extend xfermodes so that all cases can be tested.
238 // 247 //
239 for (int mode = SkXfermode::kClear_Mode; mode <= SkXfermode::kScreen_Mode; + +mode) { 248 for (int mode = SkXfermode::kClear_Mode; mode <= SkXfermode::kScreen_Mode; + +mode) {
240 SkXfermodeProc proc32 = SkXfermode::GetProc((SkXfermode::Mode)mode); 249 SkXfermodeProc proc32 = SkXfermode::GetProc((SkXfermode::Mode)mode);
241 SkXfermodeProc4f proc4f = SkXfermode::GetProc4f((SkXfermode::Mode)mode); 250 SkXfermodeProc4f proc4f = SkXfermode::GetProc4f((SkXfermode::Mode)mode);
242 REPORTER_ASSERT(reporter, compare_procs(proc32, proc4f)); 251 REPORTER_ASSERT(reporter, compare_procs(proc32, proc4f));
243 } 252 }
244 } 253 }
OLDNEW
« no previous file with comments | « src/core/SkXfermode4f.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698