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 <mtklein@google.com>
Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2020-04-16 13:47:21 -04:00 committed by Skia Commit-Bot
parent f3f822c791
commit b6af407d67
4 changed files with 48 additions and 125 deletions

View File

@ -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<SkColorFilter> MakeWithSkColors(const SkColor colors[kNumColors]) {
return sk_sp<SkColorFilter>(new SkOverdrawColorFilter(colors));
}
#if SK_SUPPORT_GPU
std::unique_ptr<GrFragmentProcessor> 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<SkColorFilter> MakeWithSkColors(const SkColor[kNumColors]);
};
#endif // SkOverdrawColorFilter_DEFINED

View File

@ -13,6 +13,9 @@
class SkColorFilterPriv {
public:
static sk_sp<SkColorFilter> MakeGaussian();
static void RegisterLegacyOverdraw();
};
#endif

View File

@ -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<Ctx>();
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<SkColorFilter> 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<SkFlattenable> 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<SkColorFilter>(new SkOverdrawColorFilter(colors));
return nullptr;
}
void SkOverdrawColorFilter::RegisterFlattenables() {
SK_REGISTER_FLATTENABLE(SkOverdrawColorFilter);
void SkColorFilterPriv::RegisterLegacyOverdraw() {
SkFlattenable::Register("SkOverdrawColorFilter",
[](SkReadBuffer& buffer) -> sk_sp<SkFlattenable> {
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<GrFragmentProcessor> 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<SkPMColor4f*>(inputs->writable_data()), fColors);
return GrSkSLFP::Make(context, effect, "Overdraw", std::move(inputs));
}
#endif

View File

@ -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.