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 "include/core/SkData.h"
9 #include "include/effects/SkOverdrawColorFilter.h"
10 #include "include/effects/SkRuntimeEffect.h"
11 #include "include/private/SkColorData.h"
12 #include "src/core/SkRuntimeEffectPriv.h"
13 
MakeWithSkColors(const SkColor colors[kNumColors])14 sk_sp<SkColorFilter> SkOverdrawColorFilter::MakeWithSkColors(const SkColor colors[kNumColors]) {
15     sk_sp<SkRuntimeEffect> effect = SkMakeCachedRuntimeEffect(
16             SkRuntimeEffect::MakeForColorFilter,
17             R"(uniform half4 color0;
18                uniform half4 color1;
19                uniform half4 color2;
20                uniform half4 color3;
21                uniform half4 color4;
22                uniform half4 color5;
23 
24                half4 main(half4 color) {
25                    half alpha = 255.0 * color.a;
26                    color = alpha < 0.5 ? color0
27                          : alpha < 1.5 ? color1
28                          : alpha < 2.5 ? color2
29                          : alpha < 3.5 ? color3
30                          : alpha < 4.5 ? color4 : color5;
31                    return color;
32                }
33             )");
34     if (effect) {
35         auto data = SkData::MakeUninitialized(kNumColors * sizeof(SkPMColor4f));
36         SkPMColor4f* premul = (SkPMColor4f*)data->writable_data();
37         for (int i = 0; i < kNumColors; ++i) {
38             premul[i] = SkColor4f::FromColor(colors[i]).premul();
39         }
40         return effect->makeColorFilter(std::move(data));
41     }
42     return nullptr;
43 }
44