OLD | NEW |
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" |
| 10 #include "SkGradientShader.h" |
| 11 #include "SkImage.h" |
9 #include "SkShader.h" | 12 #include "SkShader.h" |
10 #include "SkColorMatrixFilter.h" | 13 |
11 #include "Test.h" | 14 #include "Test.h" |
12 #include "SkRandom.h" | 15 #include "SkRandom.h" |
13 | 16 |
| 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 |
14 DEF_TEST(SkColor4f_FromColor, reporter) { | 33 DEF_TEST(SkColor4f_FromColor, reporter) { |
15 const struct { | 34 const struct { |
16 SkColor fC; | 35 SkColor fC; |
17 SkColor4f fC4; | 36 SkColor4f fC4; |
18 } recs[] = { | 37 } recs[] = { |
19 { SK_ColorBLACK, { 1, 0, 0, 0 } }, | 38 { SK_ColorBLACK, { 1, 0, 0, 0 } }, |
20 { SK_ColorWHITE, { 1, 1, 1, 1 } }, | 39 { SK_ColorWHITE, { 1, 1, 1, 1 } }, |
21 { SK_ColorRED, { 1, 1, 0, 0 } }, | 40 { SK_ColorRED, { 1, 1, 0, 0 } }, |
22 { SK_ColorGREEN, { 1, 0, 1, 0 } }, | 41 { SK_ColorGREEN, { 1, 0, 1, 0 } }, |
23 { SK_ColorBLUE, { 1, 0, 0, 1 } }, | 42 { SK_ColorBLUE, { 1, 0, 0, 1 } }, |
24 { 0, { 0, 0, 0, 0 } }, | 43 { 0, { 0, 0, 0, 0 } }, |
25 { 0x55AAFF00, { 1/3.0f, 2/3.0f, 1, 0 } }, | 44 { 0x55AAFF00, { 1/3.0f, 2/3.0f, 1, 0 } }, |
26 }; | 45 }; |
27 | 46 |
28 for (const auto& r : recs) { | 47 for (const auto& r : recs) { |
29 SkColor4f c4 = SkColor4f::FromColor(r.fC); | 48 SkColor4f c4 = SkColor4f::FromColor(r.fC); |
30 REPORTER_ASSERT(reporter, c4 == r.fC4); | 49 REPORTER_ASSERT(reporter, c4 == r.fC4); |
31 } | 50 } |
32 } | 51 } |
33 | 52 |
34 static bool nearly_equal(float a, float b) { | 53 DEF_TEST(Color4f_premul, reporter) { |
35 const float kTolerance = 1.0f / (1 << 20); | |
36 return fabsf(a - b) < kTolerance; | |
37 } | |
38 | |
39 DEF_TEST(SkColor4f_premul, reporter) { | |
40 SkRandom rand; | 54 SkRandom rand; |
41 | 55 |
42 for (int i = 0; i < 1000000; ++i) { | 56 for (int i = 0; i < 1000000; ++i) { |
43 // First just test opaque colors, so that the premul should be exact | 57 // First just test opaque colors, so that the premul should be exact |
44 SkColor4f c4 { | 58 SkColor4f c4 { |
45 1, rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1() | 59 1, rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1() |
46 }; | 60 }; |
47 SkPM4f pm4 = c4.premul(); | 61 SkPM4f pm4 = c4.premul(); |
48 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); | 62 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); |
49 REPORTER_ASSERT(reporter, pm4.fVec[SK_R_INDEX] == c4.fA * c4.fR); | 63 REPORTER_ASSERT(reporter, pm4.fVec[SK_R_INDEX] == c4.fA * c4.fR); |
50 REPORTER_ASSERT(reporter, pm4.fVec[SK_G_INDEX] == c4.fA * c4.fG); | 64 REPORTER_ASSERT(reporter, pm4.fVec[SK_G_INDEX] == c4.fA * c4.fG); |
51 REPORTER_ASSERT(reporter, pm4.fVec[SK_B_INDEX] == c4.fA * c4.fB); | 65 REPORTER_ASSERT(reporter, pm4.fVec[SK_B_INDEX] == c4.fA * c4.fB); |
52 | 66 |
53 // We compare with a tolerance, in case our premul multiply is implement
ed at slightly | 67 // We compare with a tolerance, in case our premul multiply is implement
ed at slightly |
54 // different precision than the test code. | 68 // different precision than the test code. |
55 c4.fA = rand.nextUScalar1(); | 69 c4.fA = rand.nextUScalar1(); |
56 pm4 = c4.premul(); | 70 pm4 = c4.premul(); |
57 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); | 71 REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA); |
58 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)); |
59 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)); |
60 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)); |
61 } | 75 } |
62 } | 76 } |
| 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 } |
OLD | NEW |