blob: d55319dab1ecb178387631a6c318902c1a9ded89 [file] [log] [blame]
/*
* 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/SkColorFilter.h"
#include "include/core/SkData.h"
#include "include/core/SkImage.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/effects/SkRuntimeEffect.h"
#include "tools/Resources.h"
#include <stddef.h>
#include <utility>
const char* gNoop = R"(
half4 main(half4 color) {
return color;
}
)";
const char* gLumaSrc = R"(
half4 main(half4 color) {
return dot(color.rgb, half3(0.3, 0.6, 0.1)).000r;
}
)";
// Build up the same effect with increasingly complex control flow syntax.
// All of these are semantically equivalent and can be reduced in principle to one basic block.
// Simplest to run; hardest to write?
const char* gTernary = R"(
half4 main(half4 color) {
half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
half scale = luma < 0.33333 ? 0.5
: luma < 0.66666 ? (0.166666 + 2.0 * (luma - 0.33333)) / luma
: /* else */ (0.833333 + 0.5 * (luma - 0.66666)) / luma;
return half4(color.rgb * scale, color.a);
}
)";
// Uses conditional if statements but no early return.
const char* gIfs = R"(
half4 main(half4 color) {
half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
half scale = 0;
if (luma < 0.33333) {
scale = 0.5;
} else if (luma < 0.66666) {
scale = (0.166666 + 2.0 * (luma - 0.33333)) / luma;
} else {
scale = (0.833333 + 0.5 * (luma - 0.66666)) / luma;
}
return half4(color.rgb * scale, color.a);
}
)";
// Distilled from AOSP tone mapping shaders, more like what people tend to write.
const char* gEarlyReturn = R"(
half4 main(half4 color) {
half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
half scale = 0;
if (luma < 0.33333) {
return half4(color.rgb * 0.5, color.a);
} else if (luma < 0.66666) {
scale = 0.166666 + 2.0 * (luma - 0.33333);
} else {
scale = 0.833333 + 0.5 * (luma - 0.66666);
}
return half4(color.rgb * (scale/luma), color.a);
}
)";
DEF_SIMPLE_GM(runtimecolorfilter, canvas, 256 * 3, 256 * 2) {
sk_sp<SkImage> img = GetResourceAsImage("images/mandrill_256.png");
auto draw_filter = [&](const char* src) {
auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString(src));
if (!effect) {
SkDebugf("%s\n%s\n", src, err.c_str());
}
SkASSERT(effect);
SkPaint p;
p.setColorFilter(effect->makeColorFilter(nullptr));
canvas->drawImage(img, 0, 0, SkSamplingOptions(), &p);
canvas->translate(256, 0);
};
for (const char* src : { gNoop, gLumaSrc }) {
draw_filter(src);
}
canvas->translate(-256*2, 256);
for (const char* src : { gTernary, gIfs, gEarlyReturn}) {
draw_filter(src);
}
}