reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | */ |
| 7 | |
| 8 | #include "SkColor.h" |
reed | 5f34e8e | 2016-01-23 14:19:06 -0800 | [diff] [blame] | 9 | #include "SkColorMatrixFilter.h" |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
| 11 | #include "SkImage.h" |
reed | dd9ffea | 2016-02-18 12:39:14 -0800 | [diff] [blame^] | 12 | #include "SkPM4f.h" |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 13 | #include "SkShader.h" |
| 14 | |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 15 | #include "Test.h" |
| 16 | #include "SkRandom.h" |
| 17 | |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 18 | const float kTolerance = 1.0f / (1 << 20); |
| 19 | |
| 20 | static bool nearly_equal(float a, float b, float tol = kTolerance) { |
| 21 | SkASSERT(tol >= 0); |
| 22 | return fabsf(a - b) <= tol; |
| 23 | } |
| 24 | |
| 25 | static bool nearly_equal(const SkPM4f a, const SkPM4f& b, float tol = kTolerance) { |
| 26 | for (int i = 0; i < 4; ++i) { |
| 27 | if (!nearly_equal(a.fVec[i], b.fVec[i], tol)) { |
| 28 | return false; |
| 29 | } |
| 30 | } |
| 31 | return true; |
| 32 | } |
| 33 | |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 34 | DEF_TEST(SkColor4f_FromColor, reporter) { |
| 35 | const struct { |
| 36 | SkColor fC; |
| 37 | SkColor4f fC4; |
| 38 | } recs[] = { |
| 39 | { SK_ColorBLACK, { 1, 0, 0, 0 } }, |
| 40 | { SK_ColorWHITE, { 1, 1, 1, 1 } }, |
| 41 | { SK_ColorRED, { 1, 1, 0, 0 } }, |
| 42 | { SK_ColorGREEN, { 1, 0, 1, 0 } }, |
| 43 | { SK_ColorBLUE, { 1, 0, 0, 1 } }, |
| 44 | { 0, { 0, 0, 0, 0 } }, |
| 45 | { 0x55AAFF00, { 1/3.0f, 2/3.0f, 1, 0 } }, |
| 46 | }; |
| 47 | |
| 48 | for (const auto& r : recs) { |
| 49 | SkColor4f c4 = SkColor4f::FromColor(r.fC); |
| 50 | REPORTER_ASSERT(reporter, c4 == r.fC4); |
| 51 | } |
| 52 | } |
| 53 | |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 54 | DEF_TEST(Color4f_premul, reporter) { |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 55 | SkRandom rand; |
| 56 | |
| 57 | for (int i = 0; i < 1000000; ++i) { |
| 58 | // First just test opaque colors, so that the premul should be exact |
| 59 | SkColor4f c4 { |
| 60 | 1, rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1() |
| 61 | }; |
| 62 | SkPM4f pm4 = c4.premul(); |
| 63 | REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); |
| 64 | REPORTER_ASSERT(reporter, pm4.fVec[SK_R_INDEX] == c4.fA * c4.fR); |
| 65 | REPORTER_ASSERT(reporter, pm4.fVec[SK_G_INDEX] == c4.fA * c4.fG); |
| 66 | REPORTER_ASSERT(reporter, pm4.fVec[SK_B_INDEX] == c4.fA * c4.fB); |
| 67 | |
| 68 | // We compare with a tolerance, in case our premul multiply is implemented at slightly |
| 69 | // different precision than the test code. |
| 70 | c4.fA = rand.nextUScalar1(); |
| 71 | pm4 = c4.premul(); |
| 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)); |
| 74 | REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_G_INDEX], c4.fA * c4.fG)); |
| 75 | REPORTER_ASSERT(reporter, nearly_equal(pm4.fVec[SK_B_INDEX], c4.fA * c4.fB)); |
| 76 | } |
| 77 | } |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 78 | |
| 79 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 80 | |
reed | 3b69b82 | 2016-01-24 11:15:14 -0800 | [diff] [blame] | 81 | static SkColorFilter* make_mode_cf() { |
| 82 | return SkColorFilter::CreateModeFilter(0xFFBB8855, SkXfermode::kPlus_Mode); |
| 83 | } |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 84 | |
reed | 3b69b82 | 2016-01-24 11:15:14 -0800 | [diff] [blame] | 85 | static SkColorFilter* make_mx_cf() { |
| 86 | const float mx[] = { |
| 87 | 0.5f, 0, 0, 0, 0.1f, |
| 88 | 0, 0.5f, 0, 0, 0.2f, |
| 89 | 0, 0, 1, 0, -0.1f, |
| 90 | 0, 0, 0, 1, 0, |
| 91 | }; |
| 92 | return SkColorMatrixFilter::Create(mx); |
| 93 | } |
| 94 | |
| 95 | static SkColorFilter* make_compose_cf() { |
| 96 | SkAutoTUnref<SkColorFilter> cf0(make_mode_cf()); |
| 97 | SkAutoTUnref<SkColorFilter> cf1(make_mx_cf()); |
| 98 | return SkColorFilter::CreateComposeFilter(cf0, cf1); |
| 99 | } |
| 100 | |
| 101 | static SkShader* make_color_sh() { return SkShader::CreateColorShader(0xFFBB8855); } |
| 102 | |
| 103 | static SkShader* make_image_sh() { |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 104 | const SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2); |
| 105 | const SkPMColor pixels[] { |
| 106 | SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), |
| 107 | SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), |
| 108 | SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), |
| 109 | SkPackARGB32(0xFF, 0xBB, 0x88, 0x55), |
| 110 | }; |
| 111 | SkAutoTUnref<SkImage> image(SkImage::NewRasterCopy(info, pixels, sizeof(SkPMColor) * 2)); |
| 112 | return image->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 113 | } |
| 114 | |
reed | 3b69b82 | 2016-01-24 11:15:14 -0800 | [diff] [blame] | 115 | static SkShader* make_grad_sh() { |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 116 | const SkPoint pts[] {{ 0, 0 }, { 100, 100 }}; |
| 117 | const SkColor colors[] { SK_ColorRED, SK_ColorBLUE }; |
| 118 | return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); |
| 119 | } |
| 120 | |
reed | 3b69b82 | 2016-01-24 11:15:14 -0800 | [diff] [blame] | 121 | static SkShader* make_cf_sh() { |
| 122 | SkAutoTUnref<SkColorFilter> filter(make_mx_cf()); |
| 123 | SkAutoTUnref<SkShader> shader(make_color_sh()); |
| 124 | return shader->newWithColorFilter(filter); |
| 125 | } |
| 126 | |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 127 | static void compare_spans(const SkPM4f span4f[], const SkPMColor span4b[], int count, |
| 128 | skiatest::Reporter* reporter) { |
| 129 | for (int i = 0; i < count; ++i) { |
| 130 | SkPM4f c0 = SkPM4f::FromPMColor(span4b[i]); |
| 131 | SkPM4f c1 = span4f[i]; |
| 132 | REPORTER_ASSERT(reporter, nearly_equal(c0, c1, 1.0f/255)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | DEF_TEST(Color4f_shader, reporter) { |
| 137 | struct { |
| 138 | SkShader* (*fFact)(); |
| 139 | bool fSupports4f; |
| 140 | } recs[] = { |
reed | 3b69b82 | 2016-01-24 11:15:14 -0800 | [diff] [blame] | 141 | { make_color_sh, true }, |
| 142 | { make_grad_sh, false }, |
| 143 | { make_image_sh, false }, |
| 144 | { make_cf_sh, true }, |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | SkPaint paint; |
| 148 | for (const auto& rec : recs) { |
| 149 | uint32_t storage[200]; |
| 150 | paint.setShader(rec.fFact())->unref(); |
| 151 | SkASSERT(paint.getShader()->contextSize() <= sizeof(storage)); |
| 152 | SkShader::Context* ctx = paint.getShader()->createContext({paint, SkMatrix::I(), nullptr}, |
| 153 | storage); |
| 154 | REPORTER_ASSERT(reporter, ctx->supports4f() == rec.fSupports4f); |
| 155 | if (ctx->supports4f()) { |
| 156 | const int N = 100; |
| 157 | SkPM4f buffer4f[N]; |
| 158 | ctx->shadeSpan4f(0, 0, buffer4f, N); |
| 159 | SkPMColor buffer4b[N]; |
| 160 | ctx->shadeSpan(0, 0, buffer4b, N); |
| 161 | compare_spans(buffer4f, buffer4b, N, reporter); |
| 162 | } |
| 163 | ctx->~Context(); |
| 164 | } |
| 165 | } |
| 166 | |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 167 | DEF_TEST(Color4f_colorfilter, reporter) { |
| 168 | struct { |
| 169 | SkColorFilter* (*fFact)(); |
| 170 | bool fSupports4f; |
| 171 | } recs[] = { |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 172 | { make_mode_cf, true }, |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 173 | { make_mx_cf, true }, |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 174 | { make_compose_cf, true }, |
reed | 7f225cf | 2016-01-23 19:37:56 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | // prepare the src |
| 178 | const int N = 100; |
| 179 | SkPMColor src4b[N]; |
| 180 | SkPM4f src4f[N]; |
| 181 | SkRandom rand; |
| 182 | for (int i = 0; i < N; ++i) { |
| 183 | src4b[i] = SkPreMultiplyColor(rand.nextU()); |
| 184 | src4f[i] = SkPM4f::FromPMColor(src4b[i]); |
| 185 | } |
| 186 | // confirm that our srcs are (nearly) equal |
| 187 | compare_spans(src4f, src4b, N, reporter); |
| 188 | |
| 189 | for (const auto& rec : recs) { |
| 190 | SkAutoTUnref<SkColorFilter> filter(rec.fFact()); |
| 191 | REPORTER_ASSERT(reporter, filter->supports4f() == rec.fSupports4f); |
| 192 | if (filter->supports4f()) { |
| 193 | SkPMColor dst4b[N]; |
| 194 | filter->filterSpan(src4b, N, dst4b); |
| 195 | SkPM4f dst4f[N]; |
| 196 | filter->filterSpan4f(src4f, N, dst4f); |
| 197 | compare_spans(dst4f, dst4b, N, reporter); |
| 198 | } |
| 199 | } |
| 200 | } |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 201 | |
| 202 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 203 | |
| 204 | typedef SkPM4f (*SkXfermodeProc4f)(const SkPM4f& src, const SkPM4f& dst); |
| 205 | |
| 206 | static bool compare_procs(SkXfermodeProc proc32, SkXfermodeProc4f proc4f) { |
| 207 | const float kTolerance = 1.0f / 255; |
| 208 | |
| 209 | const SkColor colors[] = { |
| 210 | 0, 0xFF000000, 0xFFFFFFFF, 0x80FF0000 |
| 211 | }; |
| 212 | |
| 213 | for (auto s32 : colors) { |
| 214 | SkPMColor s_pm32 = SkPreMultiplyColor(s32); |
| 215 | SkPM4f s_pm4f = SkColor4f::FromColor(s32).premul(); |
| 216 | for (auto d32 : colors) { |
| 217 | SkPMColor d_pm32 = SkPreMultiplyColor(d32); |
| 218 | SkPM4f d_pm4f = SkColor4f::FromColor(d32).premul(); |
| 219 | |
| 220 | SkPMColor r32 = proc32(s_pm32, d_pm32); |
| 221 | SkPM4f r4f = proc4f(s_pm4f, d_pm4f); |
| 222 | |
| 223 | SkPM4f r32_4f = SkPM4f::FromPMColor(r32); |
| 224 | if (!nearly_equal(r4f, r32_4f, kTolerance)) { |
| 225 | return false; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | // Check that our Proc and Proc4f return (nearly) the same results |
| 233 | // |
| 234 | DEF_TEST(Color4f_xfermode_proc4f, reporter) { |
| 235 | // TODO: extend xfermodes so that all cases can be tested. |
| 236 | // |
| 237 | for (int mode = SkXfermode::kClear_Mode; mode <= SkXfermode::kScreen_Mode; ++mode) { |
| 238 | SkXfermodeProc proc32 = SkXfermode::GetProc((SkXfermode::Mode)mode); |
| 239 | SkXfermodeProc4f proc4f = SkXfermode::GetProc4f((SkXfermode::Mode)mode); |
| 240 | REPORTER_ASSERT(reporter, compare_procs(proc32, proc4f)); |
| 241 | } |
| 242 | } |