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