Mike Reed | c723b70 | 2019-03-12 11:49:00 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019 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 "SkMixer.h" |
| 9 | #include "Test.h" |
| 10 | #include "SkColorData.h" |
| 11 | #include "SkMixerBase.h" |
| 12 | |
| 13 | static void check_color(skiatest::Reporter* reporter, |
| 14 | const SkPMColor4f& result, const SkPMColor4f& expected) { |
| 15 | const float tol = 1/510.f; // made up |
| 16 | for (int i = 0; i < 4; ++i) { |
| 17 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[i], expected[i], tol)); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | static void check_mixer(skiatest::Reporter* reporter, sk_sp<SkMixer> mixer, |
| 22 | const SkPMColor4f& a, const SkPMColor4f& b, const SkPMColor4f& expected) { |
| 23 | SkPMColor4f result = as_MB(mixer)->test_mix(a, b); |
| 24 | check_color(reporter, result, expected); |
| 25 | |
| 26 | auto data = mixer->serialize(); |
| 27 | auto m2 = SkMixerBase::Deserialize(data->data(), data->size()); |
| 28 | SkPMColor4f r2 = as_MB(m2)->test_mix(a, b); |
| 29 | REPORTER_ASSERT(reporter, result == r2); |
| 30 | } |
| 31 | |
| 32 | DEF_TEST(Mixer, r) { |
| 33 | const SkPMColor4f transparent = {0, 0, 0, 0}; |
| 34 | const SkPMColor4f red = {1, 0, 0, 1}; |
| 35 | const SkPMColor4f magenta = {1, 0, 1, 1}; |
| 36 | const SkPMColor4f blue = {0, 0, 1, 1}; |
| 37 | |
| 38 | auto first = SkMixer::MakeFirst(); |
| 39 | auto second = SkMixer::MakeSecond(); |
| 40 | check_mixer(r, first, red, blue, red); |
| 41 | check_mixer(r, second, red, blue, blue); |
| 42 | |
| 43 | check_mixer(r, first->makeReverse(), red, blue, blue); |
| 44 | check_mixer(r, second->makeReverse(), red, blue, red); |
| 45 | |
| 46 | check_mixer(r, SkMixer::MakeLerp(0), red, blue, red); |
| 47 | check_mixer(r, SkMixer::MakeLerp(0.0001f), red, blue, red); |
| 48 | check_mixer(r, SkMixer::MakeLerp(0.5f), red, blue, {0.5f, 0, 0.5f, 1}); |
| 49 | check_mixer(r, SkMixer::MakeLerp(0.9999f), red, blue, blue); |
| 50 | check_mixer(r, SkMixer::MakeLerp(1), red, blue, blue); |
| 51 | |
| 52 | check_mixer(r, SkMixer::MakeBlend(SkBlendMode::kClear), red, blue, transparent); |
| 53 | check_mixer(r, SkMixer::MakeBlend(SkBlendMode::kSrc), red, blue, red); |
| 54 | check_mixer(r, SkMixer::MakeBlend(SkBlendMode::kDst), red, blue, blue); |
| 55 | check_mixer(r, SkMixer::MakeBlend(SkBlendMode::kPlus), red, blue, magenta); |
| 56 | |
| 57 | // mx should average the results of Plus and Clear |
| 58 | auto mx = SkMixer::MakeLerp(0.5f)->makeMerge(SkMixer::MakeBlend(SkBlendMode::kPlus), |
| 59 | SkMixer::MakeConst(0)); |
| 60 | check_mixer(r, mx, red, blue, {0.5f, 0, 0.5f, 0.5f}); |
| 61 | } |