From b6af407d67b6b164b06edc16c5fa2421b68ac77d Mon Sep 17 00:00:00 2001 From: Mike Reed Date: Thu, 16 Apr 2020 13:47:21 -0400 Subject: [PATCH] change Overdraw colorfilter to be pure sksl About same speed on CPU as pipeline-callback Change-Id: If8769bebb3c51b5ea61fc34d76dde0cfc16b8473 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/283871 Reviewed-by: Mike Klein Commit-Queue: Mike Reed --- include/effects/SkOverdrawColorFilter.h | 37 +----- src/core/SkColorFilterPriv.h | 3 + src/effects/SkOverdrawColorFilter.cpp | 130 ++++++------------- src/ports/SkGlobalInitialization_default.cpp | 3 +- 4 files changed, 48 insertions(+), 125 deletions(-) diff --git a/include/effects/SkOverdrawColorFilter.h b/include/effects/SkOverdrawColorFilter.h index 344aef4d39..0d6fc9282c 100644 --- a/include/effects/SkOverdrawColorFilter.h +++ b/include/effects/SkOverdrawColorFilter.h @@ -13,44 +13,17 @@ /** * Uses the value in the src alpha channel to set the dst pixel. - * 0 -> fColors[0] - * 1 -> fColors[1] + * 0 -> colors[0] + * 1 -> colors[1] * ... - * 5 (or larger) -> fColors[5] + * 5 (or larger) -> colors[5] * */ -class SK_API SkOverdrawColorFilter : public SkColorFilter { +class SK_API SkOverdrawColorFilter { public: static constexpr int kNumColors = 6; - static sk_sp MakeWithSkColors(const SkColor colors[kNumColors]) { - return sk_sp(new SkOverdrawColorFilter(colors)); - } - -#if SK_SUPPORT_GPU - std::unique_ptr asFragmentProcessor(GrRecordingContext*, - const GrColorInfo&) const override; -#endif - - static void RegisterFlattenables(); - -protected: - void flatten(SkWriteBuffer& buffer) const override; - -private: - SK_FLATTENABLE_HOOKS(SkOverdrawColorFilter) - - SkOverdrawColorFilter(const SkColor colors[kNumColors]) { - memcpy(fColors, colors, kNumColors * sizeof(SkColor)); - } - - bool onAppendStages(const SkStageRec&, bool) const override; - skvm::Color onProgram(skvm::Builder*, skvm::Color, SkColorSpace*, skvm::Uniforms*, - SkArenaAlloc*) const override; - - SkColor fColors[kNumColors]; - - typedef SkColorFilter INHERITED; + static sk_sp MakeWithSkColors(const SkColor[kNumColors]); }; #endif // SkOverdrawColorFilter_DEFINED diff --git a/src/core/SkColorFilterPriv.h b/src/core/SkColorFilterPriv.h index c86bb3e41b..d98bd04c0e 100644 --- a/src/core/SkColorFilterPriv.h +++ b/src/core/SkColorFilterPriv.h @@ -13,6 +13,9 @@ class SkColorFilterPriv { public: static sk_sp MakeGaussian(); + + static void RegisterLegacyOverdraw(); + }; #endif diff --git a/src/effects/SkOverdrawColorFilter.cpp b/src/effects/SkOverdrawColorFilter.cpp index d290cc4a91..8df023941b 100644 --- a/src/effects/SkOverdrawColorFilter.cpp +++ b/src/effects/SkOverdrawColorFilter.cpp @@ -5,19 +5,15 @@ * found in the LICENSE file. */ +#include "include/core/SkData.h" #include "include/core/SkUnPreMultiply.h" #include "include/effects/SkOverdrawColorFilter.h" -#include "src/core/SkArenaAlloc.h" -#include "src/core/SkEffectPriv.h" -#include "src/core/SkRasterPipeline.h" -#include "src/core/SkReadBuffer.h" -#include "src/core/SkVM.h" - -#if SK_SUPPORT_GPU #include "include/effects/SkRuntimeEffect.h" -#include "src/gpu/effects/GrSkSLFP.h" +#include "include/private/SkColorData.h" +#include "src/core/SkColorFilterPriv.h" +#include "src/core/SkReadBuffer.h" -GR_FP_SRC_STRING SKSL_OVERDRAW_SRC = R"( +const char* SKSL_OVERDRAW_SRC = R"( uniform half4 color0; uniform half4 color1; uniform half4 color2; @@ -27,94 +23,44 @@ uniform half4 color5; void main(inout half4 color) { half alpha = 255.0 * color.a; - if (alpha < 0.5) { - color = color0; - } else if (alpha < 1.5) { - color = color1; - } else if (alpha < 2.5) { - color = color2; - } else if (alpha < 3.5) { - color = color3; - } else if (alpha < 4.5) { - color = color4; - } else { - color = color5; - } + color = alpha < 0.5 ? color0 + : alpha < 1.5 ? color1 + : alpha < 2.5 ? color2 + : alpha < 3.5 ? color3 + : alpha < 4.5 ? color4 : color5; } )"; -#endif -static void convert_to_pm4f(SkPMColor4f dst[], const SkColor src[]) { - for (int i = 0; i < SkOverdrawColorFilter::kNumColors; ++i) { - dst[i] = SkColor4f::FromColor(src[i]).premul(); - } -} - -bool SkOverdrawColorFilter::onAppendStages(const SkStageRec& rec, bool shader_is_opaque) const { - struct Ctx : public SkRasterPipeline_CallbackCtx { - SkPMColor4f colors[kNumColors]; - }; - // TODO: do we care about transforming to dstCS? - auto ctx = rec.fAlloc->make(); - convert_to_pm4f(ctx->colors, fColors); - - ctx->fn = [](SkRasterPipeline_CallbackCtx* arg, int active_pixels) { - auto ctx = (Ctx*)arg; - auto pixels = (SkPMColor4f*)ctx->rgba; - for (int i = 0; i < active_pixels; i++) { - uint8_t alpha = (int)(pixels[i].fA * 255); - if (alpha >= kNumColors) { - alpha = kNumColors - 1; - } - pixels[i] = ctx->colors[alpha]; +sk_sp SkOverdrawColorFilter::MakeWithSkColors(const SkColor colors[kNumColors]) { + auto [effect, err] = SkRuntimeEffect::Make(SkString(SKSL_OVERDRAW_SRC)); + if (effect) { + auto data = SkData::MakeUninitialized(kNumColors * sizeof(SkPMColor4f)); + SkPMColor4f* premul = (SkPMColor4f*)data->writable_data(); + for (int i = 0; i < kNumColors; ++i) { + premul[i] = SkColor4f::FromColor(colors[i]).premul(); } - }; - rec.fPipeline->append(SkRasterPipeline::callback, ctx); - return true; -} - -skvm::Color SkOverdrawColorFilter::onProgram(skvm::Builder* p, skvm::Color c, - SkColorSpace* /*dstCS*/, skvm::Uniforms* uniforms, - SkArenaAlloc* alloc) const { - skvm::I32 index = min(to_unorm(8,c.a), kNumColors - 1); - c = unpack_8888(gather32(uniforms->pushPtr(fColors), index)); - std::swap(c.r, c.b); // The SkColors in fColors are BGRA, but we want RGBA - return c; -} - -void SkOverdrawColorFilter::flatten(SkWriteBuffer& buffer) const { - buffer.writeByteArray(fColors, kNumColors * sizeof(SkPMColor)); -} - -sk_sp SkOverdrawColorFilter::CreateProc(SkReadBuffer& buffer) { - SkPMColor colors[kNumColors]; - size_t size = buffer.getArrayCount(); - if (!buffer.validate(size == sizeof(colors))) { - return nullptr; + return effect->makeColorFilter(std::move(data)); } - if (!buffer.readByteArray(colors, sizeof(colors))) { - return nullptr; - } - - return sk_sp(new SkOverdrawColorFilter(colors)); + return nullptr; } -void SkOverdrawColorFilter::RegisterFlattenables() { - SK_REGISTER_FLATTENABLE(SkOverdrawColorFilter); +void SkColorFilterPriv::RegisterLegacyOverdraw() { + SkFlattenable::Register("SkOverdrawColorFilter", + [](SkReadBuffer& buffer) -> sk_sp { + constexpr int N = SkOverdrawColorFilter::kNumColors; + SkPMColor premul[N]; + size_t size = buffer.getArrayCount(); + if (!buffer.validate(size == sizeof(premul))) { + return nullptr; + } + if (!buffer.readByteArray(premul, sizeof(premul))) { + return nullptr; + } + + SkColor colors[N]; + for (int i = 0; i < N; ++i) { + colors[i] = SkUnPreMultiply::PMColorToColor(premul[i]); + } + return SkOverdrawColorFilter::MakeWithSkColors(colors); + }); } -#if SK_SUPPORT_GPU - -#include "include/private/GrRecordingContext.h" - -std::unique_ptr SkOverdrawColorFilter::asFragmentProcessor( - GrRecordingContext* context, const GrColorInfo&) const { - static auto effect = std::get<0>(SkRuntimeEffect::Make(SkString(SKSL_OVERDRAW_SRC))); - SkASSERT(effect->inputSize() == (kNumColors * sizeof(SkPMColor4f))); - - auto inputs = SkData::MakeUninitialized(kNumColors * sizeof(SkPMColor4f)); - convert_to_pm4f(reinterpret_cast(inputs->writable_data()), fColors); - - return GrSkSLFP::Make(context, effect, "Overdraw", std::move(inputs)); -} - -#endif diff --git a/src/ports/SkGlobalInitialization_default.cpp b/src/ports/SkGlobalInitialization_default.cpp index de85628a31..e2364fb9a3 100644 --- a/src/ports/SkGlobalInitialization_default.cpp +++ b/src/ports/SkGlobalInitialization_default.cpp @@ -30,6 +30,7 @@ #include "include/effects/SkRuntimeEffect.h" #include "include/effects/SkShaderMaskFilter.h" #include "include/effects/SkTableColorFilter.h" + #include "src/core/SkColorFilterPriv.h" #include "src/core/SkColorFilter_Matrix.h" #include "src/core/SkRecordedDrawable.h" #include "src/effects/SkDashImpl.h" @@ -76,7 +77,7 @@ SK_REGISTER_FLATTENABLE(SkLumaColorFilter); SkColorFilter::RegisterFlattenables(); SkHighContrastFilter::RegisterFlattenables(); - SkOverdrawColorFilter::RegisterFlattenables(); + SkColorFilterPriv::RegisterLegacyOverdraw(); SkTableColorFilter::RegisterFlattenables(); // Shader & color filter.