2019-06-20 16:40:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkData.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2020-02-13 17:36:28 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2020-01-02 16:55:24 +00:00
|
|
|
#include "include/effects/SkRuntimeEffect.h"
|
2020-02-13 17:36:28 +00:00
|
|
|
#include "tools/Resources.h"
|
2019-06-20 16:40:30 +00:00
|
|
|
|
|
|
|
const char* gProg = R"(
|
2019-12-09 17:07:09 +00:00
|
|
|
uniform half4 gColor;
|
2019-06-20 16:40:30 +00:00
|
|
|
|
2020-02-07 18:37:12 +00:00
|
|
|
void main(float2 p, inout half4 color) {
|
|
|
|
color = half4(half2(p)*(1.0/255), gColor.b, 1);
|
2019-06-20 16:40:30 +00:00
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
class RuntimeShader : public skiagm::GM {
|
|
|
|
bool runAsBench() const override { return true; }
|
|
|
|
|
2019-07-18 17:35:49 +00:00
|
|
|
SkString onShortName() override { return SkString("runtime_shader"); }
|
2019-06-20 16:40:30 +00:00
|
|
|
|
2019-07-18 17:35:49 +00:00
|
|
|
SkISize onISize() override { return {512, 256}; }
|
2019-06-20 16:40:30 +00:00
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2020-02-05 16:37:08 +00:00
|
|
|
sk_sp<SkRuntimeEffect> gEffect = std::get<0>(SkRuntimeEffect::Make(SkString(gProg)));
|
2019-12-26 13:43:05 +00:00
|
|
|
SkASSERT(gEffect);
|
|
|
|
|
|
|
|
SkMatrix localM;
|
|
|
|
localM.setRotate(90, 128, 128);
|
|
|
|
|
|
|
|
SkColor4f inputColor = { 1, 0, 0, 1 };
|
|
|
|
auto shader = gEffect->makeShader(SkData::MakeWithCopy(&inputColor, sizeof(inputColor)),
|
|
|
|
nullptr, 0, &localM, true);
|
2019-06-20 16:40:30 +00:00
|
|
|
SkPaint p;
|
2019-12-26 13:43:05 +00:00
|
|
|
p.setShader(std::move(shader));
|
2019-06-20 16:40:30 +00:00
|
|
|
canvas->drawRect({0, 0, 256, 256}, p);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_GM(return new RuntimeShader;)
|
2020-02-13 17:36:28 +00:00
|
|
|
|
|
|
|
static sk_sp<SkShader> make_shader(sk_sp<SkImage> img, SkISize size) {
|
|
|
|
SkMatrix scale = SkMatrix::MakeScale(size.width() / (float)img->width(),
|
|
|
|
size.height() / (float)img->height());
|
|
|
|
return img->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, &scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
|
|
|
|
static sk_sp<SkShader> make_threshold(SkISize size) {
|
|
|
|
auto info = SkImageInfo::Make(size.width(), size.height(), kAlpha_8_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
|
|
|
auto surf = SkSurface::MakeRaster(info);
|
|
|
|
auto canvas = surf->getCanvas();
|
|
|
|
|
|
|
|
const SkScalar rad = 50;
|
|
|
|
SkColor colors[] = {SK_ColorBLACK, 0};
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setShader(SkGradientShader::MakeRadial({0,0}, rad, colors, nullptr, 2, SkTileMode::kClamp));
|
|
|
|
|
|
|
|
SkPaint layerPaint;
|
|
|
|
const SkScalar sigma = 16.0f;
|
|
|
|
layerPaint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr));
|
|
|
|
canvas->saveLayer(nullptr, &layerPaint);
|
|
|
|
|
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < 25; ++i) {
|
|
|
|
SkScalar x = rand.nextF() * size.width();
|
|
|
|
SkScalar y = rand.nextF() * size.height();
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->drawCircle(0, 0, rad, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->restore(); // apply the blur
|
|
|
|
|
|
|
|
return surf->makeImageSnapshot()->makeShader();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ThresholdRT : public skiagm::GM {
|
|
|
|
sk_sp<SkShader> fBefore, fAfter, fThreshold;
|
|
|
|
sk_sp<SkRuntimeEffect> fEffect;
|
|
|
|
|
|
|
|
float fCutoff = 0.5; // so we get something interested when we're not animated
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
const SkISize size = {256, 256};
|
|
|
|
fThreshold = make_threshold(size);
|
|
|
|
fBefore = make_shader(GetResourceAsImage("images/mandrill_256.png"), size);
|
|
|
|
fAfter = make_shader(GetResourceAsImage("images/dog.jpg"), size);
|
|
|
|
|
|
|
|
const char code[] = R"(
|
2020-03-23 20:59:08 +00:00
|
|
|
in shader before_map;
|
|
|
|
in shader after_map;
|
|
|
|
in shader threshold_map;
|
2020-02-13 17:36:28 +00:00
|
|
|
|
|
|
|
uniform float cutoff;
|
|
|
|
uniform float slope;
|
|
|
|
|
|
|
|
float smooth_cutoff(float x) {
|
|
|
|
x = x * slope + (0.5 - slope * cutoff);
|
|
|
|
return clamp(x, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void main(float2 xy, inout half4 color) {
|
|
|
|
half4 before = sample(before_map, xy);
|
|
|
|
half4 after = sample(after_map, xy);
|
|
|
|
|
|
|
|
float m = smooth_cutoff(sample(threshold_map, xy).r);
|
|
|
|
color = mix(before, after, half(m));
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
auto [effect, error] = SkRuntimeEffect::Make(SkString(code));
|
|
|
|
if (!effect) {
|
|
|
|
SkDebugf("runtime error %s\n", error.c_str());
|
|
|
|
}
|
|
|
|
fEffect = effect;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runAsBench() const override { return true; }
|
|
|
|
|
|
|
|
SkString onShortName() override { return SkString("threshold_rt"); }
|
|
|
|
|
|
|
|
SkISize onISize() override { return {256, 256}; }
|
|
|
|
|
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
|
|
|
if (canvas->getGrContext() == nullptr) {
|
|
|
|
// until SkSL can handle child processors on the raster backend
|
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct {
|
|
|
|
float cutoff, slope;
|
|
|
|
} uni = {
|
|
|
|
fCutoff, 10
|
|
|
|
};
|
|
|
|
sk_sp<SkData> data = SkData::MakeWithCopy(&uni, sizeof(uni));
|
|
|
|
sk_sp<SkShader> children[] = { fBefore, fAfter, fThreshold };
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(fEffect->makeShader(data, children, SK_ARRAY_COUNT(children),
|
|
|
|
nullptr, true));
|
|
|
|
canvas->drawRect({0, 0, 256, 256}, paint);
|
|
|
|
|
|
|
|
auto draw = [&](SkScalar x, SkScalar y, sk_sp<SkShader> shader) {
|
|
|
|
paint.setShader(shader);
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->drawRect({0, 0, 256, 256}, paint);
|
|
|
|
canvas->restore();
|
|
|
|
};
|
|
|
|
draw(256, 0, fThreshold);
|
|
|
|
draw( 0, 256, fBefore);
|
|
|
|
draw(256, 256, fAfter);
|
|
|
|
|
|
|
|
return DrawResult::kOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
double t = sin(nanos / (1000 * 1000 * 1000));
|
|
|
|
fCutoff = float(t + 1) * 0.55f - 0.05f;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_GM(return new ThresholdRT;)
|
2020-04-30 16:06:23 +00:00
|
|
|
|
|
|
|
class SpiralRT : public skiagm::GM {
|
|
|
|
sk_sp<SkRuntimeEffect> fEffect;
|
|
|
|
float fSecs = 4; // so we get something interested when we're not animated
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
const char code[] = R"(
|
|
|
|
uniform float rad_scale;
|
|
|
|
uniform float2 in_center;
|
2020-04-30 21:12:03 +00:00
|
|
|
layout(srgb_unpremul) uniform float4 in_colors0;
|
|
|
|
layout(srgb_unpremul) uniform float4 in_colors1;
|
2020-04-30 16:06:23 +00:00
|
|
|
|
|
|
|
void main(float2 p, inout half4 color) {
|
|
|
|
float2 pp = p - in_center;
|
2020-05-01 14:13:18 +00:00
|
|
|
float radius = length(pp);
|
2020-04-30 16:06:23 +00:00
|
|
|
radius = sqrt(radius);
|
|
|
|
float angle = atan(pp.y / pp.x);
|
|
|
|
float t = (angle + 3.1415926/2) / (3.1415926);
|
|
|
|
t += radius * rad_scale;
|
|
|
|
t = fract(t);
|
|
|
|
float4 m = in_colors0 * (1-t) + in_colors1 * t;
|
|
|
|
color = half4(m);
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
auto [effect, error] = SkRuntimeEffect::Make(SkString(code));
|
|
|
|
if (!effect) {
|
|
|
|
SkDebugf("runtime error %s\n", error.c_str());
|
|
|
|
}
|
|
|
|
fEffect = effect;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runAsBench() const override { return true; }
|
|
|
|
|
|
|
|
SkString onShortName() override { return SkString("spiral_rt"); }
|
|
|
|
|
|
|
|
SkISize onISize() override { return {512, 512}; }
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
struct {
|
|
|
|
float rad_scale;
|
|
|
|
SkV2 in_center;
|
|
|
|
SkV4 in_colors0;
|
|
|
|
SkV4 in_colors1;
|
|
|
|
} uni {
|
|
|
|
std::sin(fSecs / 2) / 5,
|
|
|
|
{256, 256}, // center
|
|
|
|
{1, 0, 0, 1}, // color0
|
|
|
|
{0, 1, 0, 1}, // color1
|
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(fEffect->makeShader(SkData::MakeWithCopy(&uni, sizeof(uni)),
|
|
|
|
nullptr, 0, nullptr, true));
|
|
|
|
canvas->drawRect({0, 0, 512, 512}, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
fSecs = nanos / (1000 * 1000 * 1000);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_GM(return new SpiralRT;)
|