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

Side by Side Diff: tests/SkColor4fTest.cpp

Issue 1625263002: add 4f support to colorfiltershader (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/SkColorFilterShader.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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 pm4 = c4.premul(); 70 pm4 = c4.premul();
71 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); 71 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)); 72 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)); 73 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)); 74 REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_B_INDEX], c4.fA * c4. fB));
75 } 75 }
76 } 76 }
77 77
78 //////////////////////////////////////////////////////////////////////////////// ////////////////// 78 //////////////////////////////////////////////////////////////////////////////// //////////////////
79 79
80 static SkShader* make_color() { return SkShader::CreateColorShader(0xFFBB8855); } 80 static SkColorFilter* make_mode_cf() {
81 return SkColorFilter::CreateModeFilter(0xFFBB8855, SkXfermode::kPlus_Mode);
82 }
81 83
82 static SkShader* make_image() { 84 static SkColorFilter* make_mx_cf() {
85 const float mx[] = {
86 0.5f, 0, 0, 0, 0.1f,
87 0, 0.5f, 0, 0, 0.2f,
88 0, 0, 1, 0, -0.1f,
89 0, 0, 0, 1, 0,
90 };
91 return SkColorMatrixFilter::Create(mx);
92 }
93
94 static SkColorFilter* make_compose_cf() {
95 SkAutoTUnref<SkColorFilter> cf0(make_mode_cf());
96 SkAutoTUnref<SkColorFilter> cf1(make_mx_cf());
97 return SkColorFilter::CreateComposeFilter(cf0, cf1);
98 }
99
100 static SkShader* make_color_sh() { return SkShader::CreateColorShader(0xFFBB8855 ); }
101
102 static SkShader* make_image_sh() {
83 const SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2); 103 const SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
84 const SkPMColor pixels[] { 104 const SkPMColor pixels[] {
85 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), 105 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
86 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), 106 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
87 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), 107 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
88 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), 108 SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
89 }; 109 };
90 SkAutoTUnref<SkImage> image(SkImage::NewRasterCopy(info, pixels, sizeof(SkPM Color) * 2)); 110 SkAutoTUnref<SkImage> image(SkImage::NewRasterCopy(info, pixels, sizeof(SkPM Color) * 2));
91 return image->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode ); 111 return image->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode );
92 } 112 }
93 113
94 static SkShader* make_grad() { 114 static SkShader* make_grad_sh() {
95 const SkPoint pts[] {{ 0, 0 }, { 100, 100 }}; 115 const SkPoint pts[] {{ 0, 0 }, { 100, 100 }};
96 const SkColor colors[] { SK_ColorRED, SK_ColorBLUE }; 116 const SkColor colors[] { SK_ColorRED, SK_ColorBLUE };
97 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kCl amp_TileMode); 117 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kCl amp_TileMode);
98 } 118 }
99 119
120 static SkShader* make_cf_sh() {
121 SkAutoTUnref<SkColorFilter> filter(make_mx_cf());
122 SkAutoTUnref<SkShader> shader(make_color_sh());
123 return shader->newWithColorFilter(filter);
124 }
125
100 static void compare_spans(const SkPM4f span4f[], const SkPMColor span4b[], int c ount, 126 static void compare_spans(const SkPM4f span4f[], const SkPMColor span4b[], int c ount,
101 skiatest::Reporter* reporter) { 127 skiatest::Reporter* reporter) {
102 for (int i = 0; i < count; ++i) { 128 for (int i = 0; i < count; ++i) {
103 SkPM4f c0 = SkPM4f::FromPMColor(span4b[i]); 129 SkPM4f c0 = SkPM4f::FromPMColor(span4b[i]);
104 SkPM4f c1 = span4f[i]; 130 SkPM4f c1 = span4f[i];
105 REPORTER_ASSERT(reporter, nearly_equal(c0, c1, 1.0f/255)); 131 REPORTER_ASSERT(reporter, nearly_equal(c0, c1, 1.0f/255));
106 } 132 }
107 } 133 }
108 134
109 DEF_TEST(Color4f_shader, reporter) { 135 DEF_TEST(Color4f_shader, reporter) {
110 struct { 136 struct {
111 SkShader* (*fFact)(); 137 SkShader* (*fFact)();
112 bool fSupports4f; 138 bool fSupports4f;
113 } recs[] = { 139 } recs[] = {
114 { make_color, true }, 140 { make_color_sh, true },
115 { make_grad, false }, 141 { make_grad_sh, false },
116 { make_image, false }, 142 { make_image_sh, false },
143 { make_cf_sh, true },
117 }; 144 };
118 145
119 SkPaint paint; 146 SkPaint paint;
120 for (const auto& rec : recs) { 147 for (const auto& rec : recs) {
121 uint32_t storage[200]; 148 uint32_t storage[200];
122 paint.setShader(rec.fFact())->unref(); 149 paint.setShader(rec.fFact())->unref();
123 SkASSERT(paint.getShader()->contextSize() <= sizeof(storage)); 150 SkASSERT(paint.getShader()->contextSize() <= sizeof(storage));
124 SkShader::Context* ctx = paint.getShader()->createContext({paint, SkMatr ix::I(), nullptr}, 151 SkShader::Context* ctx = paint.getShader()->createContext({paint, SkMatr ix::I(), nullptr},
125 storage); 152 storage);
126 REPORTER_ASSERT(reporter, ctx->supports4f() == rec.fSupports4f); 153 REPORTER_ASSERT(reporter, ctx->supports4f() == rec.fSupports4f);
127 if (ctx->supports4f()) { 154 if (ctx->supports4f()) {
128 const int N = 100; 155 const int N = 100;
129 SkPM4f buffer4f[N]; 156 SkPM4f buffer4f[N];
130 ctx->shadeSpan4f(0, 0, buffer4f, N); 157 ctx->shadeSpan4f(0, 0, buffer4f, N);
131 SkPMColor buffer4b[N]; 158 SkPMColor buffer4b[N];
132 ctx->shadeSpan(0, 0, buffer4b, N); 159 ctx->shadeSpan(0, 0, buffer4b, N);
133 compare_spans(buffer4f, buffer4b, N, reporter); 160 compare_spans(buffer4f, buffer4b, N, reporter);
134 } 161 }
135 ctx->~Context(); 162 ctx->~Context();
136 } 163 }
137 } 164 }
138 165
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) { 166 DEF_TEST(Color4f_colorfilter, reporter) {
160 struct { 167 struct {
161 SkColorFilter* (*fFact)(); 168 SkColorFilter* (*fFact)();
162 bool fSupports4f; 169 bool fSupports4f;
163 } recs[] = { 170 } recs[] = {
164 { make_mode_cf, false }, 171 { make_mode_cf, false },
165 { make_mx_cf, true }, 172 { make_mx_cf, true },
166 { make_compose_cf, false }, 173 { make_compose_cf, false },
167 }; 174 };
168 175
(...skipping 14 matching lines...) Expand all
183 REPORTER_ASSERT(reporter, filter->supports4f() == rec.fSupports4f); 190 REPORTER_ASSERT(reporter, filter->supports4f() == rec.fSupports4f);
184 if (filter->supports4f()) { 191 if (filter->supports4f()) {
185 SkPMColor dst4b[N]; 192 SkPMColor dst4b[N];
186 filter->filterSpan(src4b, N, dst4b); 193 filter->filterSpan(src4b, N, dst4b);
187 SkPM4f dst4f[N]; 194 SkPM4f dst4f[N];
188 filter->filterSpan4f(src4f, N, dst4f); 195 filter->filterSpan4f(src4f, N, dst4f);
189 compare_spans(dst4f, dst4b, N, reporter); 196 compare_spans(dst4f, dst4b, N, reporter);
190 } 197 }
191 } 198 }
192 } 199 }
OLDNEW
« no previous file with comments | « src/core/SkColorFilterShader.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698