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"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
2019-08-05 14:41:10 +00:00
|
|
|
#include "include/effects/SkImageFilters.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);
|
|
|
|
|
2016-03-13 20:01:57 +00:00
|
|
|
SkPaint gradientPaint;
|
2016-03-13 21:13:58 +00:00
|
|
|
gradientPaint.setShader(SkGradientShader::MakeRadial(
|
2019-04-03 14:27:45 +00:00
|
|
|
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;
|
2019-08-05 14:41:10 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Paint(gradientPaint, &ir));
|
|
|
|
canvasFilter.drawRect(SkRect::Make(ir), paint);
|
2016-01-08 22:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test using the paint directly
|
|
|
|
{
|
2019-08-05 14:41:10 +00:00
|
|
|
canvasPaint.drawRect(SkRect::Make(ir), gradientPaint);
|
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);
|
|
|
|
|
2016-03-13 20:01:57 +00:00
|
|
|
SkPaint gradientPaint;
|
2016-03-13 21:13:58 +00:00
|
|
|
gradientPaint.setShader(SkGradientShader::MakeRadial(
|
2019-04-03 14:27:45 +00:00
|
|
|
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;
|
2019-08-05 14:41:10 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Paint(gradientPaint, &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
|
|
|
|
{
|
|
|
|
canvasPaint.scale(SkIntToScalar(2), SkIntToScalar(2));
|
2019-08-05 14:41:10 +00:00
|
|
|
canvasPaint.drawRect(SkRect::Make(ir), gradientPaint);
|
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);
|
|
|
|
}
|