2016-01-08 22:58:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkShader.h"
|
2021-11-29 21:41:31 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2019-08-05 14:41:10 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2021-11-29 21:41:31 +00:00
|
|
|
#include "include/effects/SkRuntimeEffect.h"
|
|
|
|
#include "src/effects/imagefilters/SkRuntimeImageFilter.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2016-01-08 22:58:35 +00:00
|
|
|
|
|
|
|
static void test_unscaled(skiatest::Reporter* reporter) {
|
2019-08-05 14:41:10 +00:00
|
|
|
static const int kWidth = 10;
|
|
|
|
static const int kHeight = 10;
|
|
|
|
|
|
|
|
SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
|
2016-01-08 22:58:35 +00:00
|
|
|
|
|
|
|
SkBitmap filterResult, paintResult;
|
|
|
|
|
2019-08-05 14:41:10 +00:00
|
|
|
filterResult.allocN32Pixels(kWidth, kHeight);
|
2016-01-08 22:58:35 +00:00
|
|
|
SkCanvas canvasFilter(filterResult);
|
|
|
|
canvasFilter.clear(0x00000000);
|
|
|
|
|
2019-08-05 14:41:10 +00:00
|
|
|
paintResult.allocN32Pixels(kWidth, kHeight);
|
2016-01-08 22:58:35 +00:00
|
|
|
SkCanvas canvasPaint(paintResult);
|
|
|
|
canvasPaint.clear(0x00000000);
|
|
|
|
|
|
|
|
SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
|
|
|
|
SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
|
|
|
|
SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
|
|
|
|
SkScalar radius = SkIntToScalar(5);
|
|
|
|
|
Add SkImageFilters::Shader in place of Paint factory
SkImageFilters::Paint did not use every slot of the SkPaint, with only
its color, alpha, color filter, and shader having a meaningful effect on
the image filter result. It was always blended into a transparent dst,
so blend mode wasn't very relevant, and it was always filled to whatever
required geometry, so stroke style, path effect, and mask filters were
ignored or not well specified.
Color, alpha, and color filter can all be combined into an SkShader, so
a more constrained SkImageFilters::Shader provides the same useful
capabilities without as many surprises.
SkImageFilters::Paint still exists, but is deprecated to be removed
once I've confirmed clients aren't depending on it.
Bug: skia:9310
Change-Id: I11a82bda1a5d440726cf4e2b5bfaae4929568679
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323680
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-10-07 19:27:20 +00:00
|
|
|
sk_sp<SkShader> gradient = SkGradientShader::MakeRadial(
|
|
|
|
center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkTileMode::kClamp);
|
2016-01-08 22:58:35 +00:00
|
|
|
|
|
|
|
// Test using the image filter
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
Add SkImageFilters::Shader in place of Paint factory
SkImageFilters::Paint did not use every slot of the SkPaint, with only
its color, alpha, color filter, and shader having a meaningful effect on
the image filter result. It was always blended into a transparent dst,
so blend mode wasn't very relevant, and it was always filled to whatever
required geometry, so stroke style, path effect, and mask filters were
ignored or not well specified.
Color, alpha, and color filter can all be combined into an SkShader, so
a more constrained SkImageFilters::Shader provides the same useful
capabilities without as many surprises.
SkImageFilters::Paint still exists, but is deprecated to be removed
once I've confirmed clients aren't depending on it.
Bug: skia:9310
Change-Id: I11a82bda1a5d440726cf4e2b5bfaae4929568679
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323680
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-10-07 19:27:20 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Shader(gradient, &ir));
|
2019-08-05 14:41:10 +00:00
|
|
|
canvasFilter.drawRect(SkRect::Make(ir), paint);
|
2016-01-08 22:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test using the paint directly
|
|
|
|
{
|
Add SkImageFilters::Shader in place of Paint factory
SkImageFilters::Paint did not use every slot of the SkPaint, with only
its color, alpha, color filter, and shader having a meaningful effect on
the image filter result. It was always blended into a transparent dst,
so blend mode wasn't very relevant, and it was always filled to whatever
required geometry, so stroke style, path effect, and mask filters were
ignored or not well specified.
Color, alpha, and color filter can all be combined into an SkShader, so
a more constrained SkImageFilters::Shader provides the same useful
capabilities without as many surprises.
SkImageFilters::Paint still exists, but is deprecated to be removed
once I've confirmed clients aren't depending on it.
Bug: skia:9310
Change-Id: I11a82bda1a5d440726cf4e2b5bfaae4929568679
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323680
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-10-07 19:27:20 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(gradient);
|
|
|
|
canvasPaint.drawRect(SkRect::Make(ir), paint);
|
2016-01-08 22:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that both paths yielded the same result
|
2019-08-05 14:41:10 +00:00
|
|
|
for (int y = 0; y < kHeight; ++y) {
|
2016-01-08 22:58:35 +00:00
|
|
|
const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
|
|
|
|
const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
|
2019-08-05 14:41:10 +00:00
|
|
|
for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
|
2016-01-08 22:58:35 +00:00
|
|
|
REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_scaled(skiatest::Reporter* reporter) {
|
2019-08-05 14:41:10 +00:00
|
|
|
static const int kWidth = 10;
|
|
|
|
static const int kHeight = 10;
|
|
|
|
|
|
|
|
SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
|
2016-01-08 22:58:35 +00:00
|
|
|
|
|
|
|
SkBitmap filterResult, paintResult;
|
|
|
|
|
2019-08-05 14:41:10 +00:00
|
|
|
filterResult.allocN32Pixels(kWidth, kHeight);
|
2016-01-08 22:58:35 +00:00
|
|
|
SkCanvas canvasFilter(filterResult);
|
|
|
|
canvasFilter.clear(0x00000000);
|
|
|
|
|
2019-08-05 14:41:10 +00:00
|
|
|
paintResult.allocN32Pixels(kWidth, kHeight);
|
2016-01-08 22:58:35 +00:00
|
|
|
SkCanvas canvasPaint(paintResult);
|
|
|
|
canvasPaint.clear(0x00000000);
|
|
|
|
|
|
|
|
SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
|
|
|
|
SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
|
|
|
|
SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
|
|
|
|
SkScalar radius = SkIntToScalar(5);
|
|
|
|
|
Add SkImageFilters::Shader in place of Paint factory
SkImageFilters::Paint did not use every slot of the SkPaint, with only
its color, alpha, color filter, and shader having a meaningful effect on
the image filter result. It was always blended into a transparent dst,
so blend mode wasn't very relevant, and it was always filled to whatever
required geometry, so stroke style, path effect, and mask filters were
ignored or not well specified.
Color, alpha, and color filter can all be combined into an SkShader, so
a more constrained SkImageFilters::Shader provides the same useful
capabilities without as many surprises.
SkImageFilters::Paint still exists, but is deprecated to be removed
once I've confirmed clients aren't depending on it.
Bug: skia:9310
Change-Id: I11a82bda1a5d440726cf4e2b5bfaae4929568679
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323680
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-10-07 19:27:20 +00:00
|
|
|
sk_sp<SkShader> gradient = SkGradientShader::MakeRadial(
|
|
|
|
center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkTileMode::kClamp);
|
2016-01-08 22:58:35 +00:00
|
|
|
|
|
|
|
// Test using the image filter
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
Add SkImageFilters::Shader in place of Paint factory
SkImageFilters::Paint did not use every slot of the SkPaint, with only
its color, alpha, color filter, and shader having a meaningful effect on
the image filter result. It was always blended into a transparent dst,
so blend mode wasn't very relevant, and it was always filled to whatever
required geometry, so stroke style, path effect, and mask filters were
ignored or not well specified.
Color, alpha, and color filter can all be combined into an SkShader, so
a more constrained SkImageFilters::Shader provides the same useful
capabilities without as many surprises.
SkImageFilters::Paint still exists, but is deprecated to be removed
once I've confirmed clients aren't depending on it.
Bug: skia:9310
Change-Id: I11a82bda1a5d440726cf4e2b5bfaae4929568679
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323680
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-10-07 19:27:20 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Shader(gradient, &ir));
|
2016-01-08 22:58:35 +00:00
|
|
|
canvasFilter.scale(SkIntToScalar(2), SkIntToScalar(2));
|
2019-08-05 14:41:10 +00:00
|
|
|
canvasFilter.drawRect(SkRect::Make(ir), paint);
|
2016-01-08 22:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test using the paint directly
|
|
|
|
{
|
Add SkImageFilters::Shader in place of Paint factory
SkImageFilters::Paint did not use every slot of the SkPaint, with only
its color, alpha, color filter, and shader having a meaningful effect on
the image filter result. It was always blended into a transparent dst,
so blend mode wasn't very relevant, and it was always filled to whatever
required geometry, so stroke style, path effect, and mask filters were
ignored or not well specified.
Color, alpha, and color filter can all be combined into an SkShader, so
a more constrained SkImageFilters::Shader provides the same useful
capabilities without as many surprises.
SkImageFilters::Paint still exists, but is deprecated to be removed
once I've confirmed clients aren't depending on it.
Bug: skia:9310
Change-Id: I11a82bda1a5d440726cf4e2b5bfaae4929568679
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323680
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-10-07 19:27:20 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(gradient);
|
2016-01-08 22:58:35 +00:00
|
|
|
canvasPaint.scale(SkIntToScalar(2), SkIntToScalar(2));
|
Add SkImageFilters::Shader in place of Paint factory
SkImageFilters::Paint did not use every slot of the SkPaint, with only
its color, alpha, color filter, and shader having a meaningful effect on
the image filter result. It was always blended into a transparent dst,
so blend mode wasn't very relevant, and it was always filled to whatever
required geometry, so stroke style, path effect, and mask filters were
ignored or not well specified.
Color, alpha, and color filter can all be combined into an SkShader, so
a more constrained SkImageFilters::Shader provides the same useful
capabilities without as many surprises.
SkImageFilters::Paint still exists, but is deprecated to be removed
once I've confirmed clients aren't depending on it.
Bug: skia:9310
Change-Id: I11a82bda1a5d440726cf4e2b5bfaae4929568679
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323680
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-10-07 19:27:20 +00:00
|
|
|
canvasPaint.drawRect(SkRect::Make(ir), paint);
|
2016-01-08 22:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that both paths yielded the same result
|
2019-08-05 14:41:10 +00:00
|
|
|
for (int y = 0; y < kHeight; ++y) {
|
2016-01-08 22:58:35 +00:00
|
|
|
const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
|
|
|
|
const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
|
2019-08-05 14:41:10 +00:00
|
|
|
for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
|
2016-01-08 22:58:35 +00:00
|
|
|
REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(PaintImageFilter, reporter) {
|
|
|
|
test_unscaled(reporter);
|
|
|
|
test_scaled(reporter);
|
|
|
|
}
|
2021-11-29 21:41:31 +00:00
|
|
|
|
|
|
|
static void test_runtime_shader(skiatest::Reporter* r, SkSurface* surface) {
|
|
|
|
sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(R"(
|
|
|
|
uniform shader child;
|
|
|
|
vec4 main(vec2 coord) {
|
|
|
|
return child.eval(coord) * 0.5;
|
|
|
|
}
|
|
|
|
)"))
|
|
|
|
.effect;
|
|
|
|
SkRuntimeShaderBuilder builder(effect);
|
|
|
|
|
|
|
|
// create a red image filter to feed as input into the SkImageFilters::RuntimeShader
|
|
|
|
SkPaint redPaint;
|
|
|
|
redPaint.setColor(SK_ColorRED);
|
|
|
|
sk_sp<SkImageFilter> input = SkImageFilters::Paint(redPaint);
|
|
|
|
|
|
|
|
// Create the different variations of SkImageFilters::RuntimeShader
|
|
|
|
// All 3 variations should produce the same pixel output
|
|
|
|
std::vector<sk_sp<SkImageFilter>> filters = {
|
|
|
|
SkMakeRuntimeImageFilter(effect, /*uniforms=*/nullptr, input),
|
|
|
|
SkImageFilters::RuntimeShader(builder, /*childShaderName=*/nullptr, input),
|
|
|
|
SkImageFilters::RuntimeShader(builder, /*childShaderName=*/"child", input)};
|
|
|
|
|
|
|
|
for (auto&& filter : filters) {
|
|
|
|
auto canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
// clear to transparent
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorTRANSPARENT);
|
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
|
|
|
|
SkPaint filterPaint;
|
|
|
|
// the green color will be ignored by the filter within the runtime shader
|
|
|
|
filterPaint.setColor(SK_ColorGREEN);
|
|
|
|
filterPaint.setImageFilter(filter);
|
|
|
|
canvas->saveLayer(nullptr, &filterPaint);
|
|
|
|
// the blue color will be ignored by the filter because the input to the image filter is not
|
|
|
|
// null
|
|
|
|
canvas->drawColor(SK_ColorBLUE);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
// This is expected to read back the half transparent red pixel produced by the image filter
|
|
|
|
SkBitmap bitmap;
|
|
|
|
REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
|
|
|
|
REPORTER_ASSERT(r,
|
|
|
|
surface->readPixels(bitmap.info(),
|
|
|
|
bitmap.getPixels(),
|
|
|
|
bitmap.rowBytes(),
|
|
|
|
/*srcX=*/0,
|
|
|
|
/*srcY=*/0));
|
|
|
|
SkColor color = bitmap.getColor(/*x=*/0, /*y=*/0);
|
|
|
|
|
|
|
|
// check alpha with a small tolerance
|
|
|
|
SkAlpha alpha = SkColorGetA(color);
|
|
|
|
REPORTER_ASSERT(r, alpha >= 127 && alpha <= 129, "Expected: %d Actual: %d", 128, alpha);
|
|
|
|
|
|
|
|
// check each color channel
|
|
|
|
color = SkColorSetA(color, 255);
|
|
|
|
REPORTER_ASSERT(r, SK_ColorRED == color, "Expected: %08x Actual: %08x", SK_ColorRED, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkRuntimeShaderImageFilter_CPU, r) {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
|
|
|
|
test_runtime_shader(r, surface.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeShaderImageFilter_GPU, r, ctxInfo) {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
|
|
|
|
sk_sp<SkSurface> surface(
|
|
|
|
SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info));
|
|
|
|
test_runtime_shader(r, surface.get());
|
|
|
|
}
|