skia2/tools/RuntimeBlendUtils.cpp
John Stiles 126788e087 Simplify GetRuntimeBlendForBlendMode.
Now that we support sampling from an SkBlender inside a Runtime Effect,
we can leverage that to reuse the blends from SkBlender::Mode. This lets
us avoid hard-coding copies of every built-in blend function.

Change-Id: I51f380490611fbde943c16648999b4fd4e6a14a9
Bug: skia:12257, skia:12085
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/434472
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2021-07-30 16:42:36 +00:00

26 lines
727 B
C++

/*
* Copyright 2021 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkBlendMode.h"
#include "include/core/SkRefCnt.h"
#include "include/effects/SkRuntimeEffect.h"
sk_sp<SkBlender> GetRuntimeBlendForBlendMode(SkBlendMode mode) {
static auto result = SkRuntimeEffect::MakeForBlender(SkString(R"(
uniform blender b;
half4 main(half4 src, half4 dst) {
return sample(b, src, dst);
}
)"));
SkASSERTF(result.effect, "%s", result.errorText.c_str());
SkRuntimeBlendBuilder builder(result.effect);
builder.child("b") = SkBlender::Mode(mode);
return builder.makeBlender();
}