skia2/tests/ShaderImageFilterTest.cpp
Michael Ludwig 7d0f853158 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-08 22:51:13 +00:00

117 lines
3.7 KiB
C++

/*
* Copyright 2016 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/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkShader.h"
#include "include/effects/SkGradientShader.h"
#include "include/effects/SkImageFilters.h"
#include "tests/Test.h"
static void test_unscaled(skiatest::Reporter* reporter) {
static const int kWidth = 10;
static const int kHeight = 10;
SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
SkBitmap filterResult, paintResult;
filterResult.allocN32Pixels(kWidth, kHeight);
SkCanvas canvasFilter(filterResult);
canvasFilter.clear(0x00000000);
paintResult.allocN32Pixels(kWidth, kHeight);
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);
sk_sp<SkShader> gradient = SkGradientShader::MakeRadial(
center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkTileMode::kClamp);
// Test using the image filter
{
SkPaint paint;
paint.setImageFilter(SkImageFilters::Shader(gradient, &ir));
canvasFilter.drawRect(SkRect::Make(ir), paint);
}
// Test using the paint directly
{
SkPaint paint;
paint.setShader(gradient);
canvasPaint.drawRect(SkRect::Make(ir), paint);
}
// Assert that both paths yielded the same result
for (int y = 0; y < kHeight; ++y) {
const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
}
}
}
static void test_scaled(skiatest::Reporter* reporter) {
static const int kWidth = 10;
static const int kHeight = 10;
SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
SkBitmap filterResult, paintResult;
filterResult.allocN32Pixels(kWidth, kHeight);
SkCanvas canvasFilter(filterResult);
canvasFilter.clear(0x00000000);
paintResult.allocN32Pixels(kWidth, kHeight);
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);
sk_sp<SkShader> gradient = SkGradientShader::MakeRadial(
center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkTileMode::kClamp);
// Test using the image filter
{
SkPaint paint;
paint.setImageFilter(SkImageFilters::Shader(gradient, &ir));
canvasFilter.scale(SkIntToScalar(2), SkIntToScalar(2));
canvasFilter.drawRect(SkRect::Make(ir), paint);
}
// Test using the paint directly
{
SkPaint paint;
paint.setShader(gradient);
canvasPaint.scale(SkIntToScalar(2), SkIntToScalar(2));
canvasPaint.drawRect(SkRect::Make(ir), paint);
}
// Assert that both paths yielded the same result
for (int y = 0; y < kHeight; ++y) {
const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
}
}
}
DEF_TEST(PaintImageFilter, reporter) {
test_unscaled(reporter);
test_scaled(reporter);
}