blob: 6f769352b4ece62f3d2738fade9510c19cffbdc9 [file] [log] [blame]
ajuma77b6ba32016-01-08 14:58:35 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkShader.h"
11#include "include/effects/SkGradientShader.h"
Michael Ludwig55edb502019-08-05 10:41:10 -040012#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "tests/Test.h"
ajuma77b6ba32016-01-08 14:58:35 -080014
15static void test_unscaled(skiatest::Reporter* reporter) {
Michael Ludwig55edb502019-08-05 10:41:10 -040016 static const int kWidth = 10;
17 static const int kHeight = 10;
18
19 SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
ajuma77b6ba32016-01-08 14:58:35 -080020
21 SkBitmap filterResult, paintResult;
22
Michael Ludwig55edb502019-08-05 10:41:10 -040023 filterResult.allocN32Pixels(kWidth, kHeight);
ajuma77b6ba32016-01-08 14:58:35 -080024 SkCanvas canvasFilter(filterResult);
25 canvasFilter.clear(0x00000000);
26
Michael Ludwig55edb502019-08-05 10:41:10 -040027 paintResult.allocN32Pixels(kWidth, kHeight);
ajuma77b6ba32016-01-08 14:58:35 -080028 SkCanvas canvasPaint(paintResult);
29 canvasPaint.clear(0x00000000);
30
31 SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
32 SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
33 SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
34 SkScalar radius = SkIntToScalar(5);
35
reed9283d202016-03-13 13:01:57 -070036 SkPaint gradientPaint;
reed1a9b9642016-03-13 14:13:58 -070037 gradientPaint.setShader(SkGradientShader::MakeRadial(
Mike Reedfae8fce2019-04-03 10:27:45 -040038 center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
ajuma77b6ba32016-01-08 14:58:35 -080039
40 // Test using the image filter
41 {
42 SkPaint paint;
Michael Ludwig55edb502019-08-05 10:41:10 -040043 paint.setImageFilter(SkImageFilters::Paint(gradientPaint, &ir));
44 canvasFilter.drawRect(SkRect::Make(ir), paint);
ajuma77b6ba32016-01-08 14:58:35 -080045 }
46
47 // Test using the paint directly
48 {
Michael Ludwig55edb502019-08-05 10:41:10 -040049 canvasPaint.drawRect(SkRect::Make(ir), gradientPaint);
ajuma77b6ba32016-01-08 14:58:35 -080050 }
51
52 // Assert that both paths yielded the same result
Michael Ludwig55edb502019-08-05 10:41:10 -040053 for (int y = 0; y < kHeight; ++y) {
ajuma77b6ba32016-01-08 14:58:35 -080054 const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
55 const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
Michael Ludwig55edb502019-08-05 10:41:10 -040056 for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
ajuma77b6ba32016-01-08 14:58:35 -080057 REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
58 }
59 }
60}
61
62static void test_scaled(skiatest::Reporter* reporter) {
Michael Ludwig55edb502019-08-05 10:41:10 -040063 static const int kWidth = 10;
64 static const int kHeight = 10;
65
66 SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
ajuma77b6ba32016-01-08 14:58:35 -080067
68 SkBitmap filterResult, paintResult;
69
Michael Ludwig55edb502019-08-05 10:41:10 -040070 filterResult.allocN32Pixels(kWidth, kHeight);
ajuma77b6ba32016-01-08 14:58:35 -080071 SkCanvas canvasFilter(filterResult);
72 canvasFilter.clear(0x00000000);
73
Michael Ludwig55edb502019-08-05 10:41:10 -040074 paintResult.allocN32Pixels(kWidth, kHeight);
ajuma77b6ba32016-01-08 14:58:35 -080075 SkCanvas canvasPaint(paintResult);
76 canvasPaint.clear(0x00000000);
77
78 SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
79 SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
80 SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
81 SkScalar radius = SkIntToScalar(5);
82
reed9283d202016-03-13 13:01:57 -070083 SkPaint gradientPaint;
reed1a9b9642016-03-13 14:13:58 -070084 gradientPaint.setShader(SkGradientShader::MakeRadial(
Mike Reedfae8fce2019-04-03 10:27:45 -040085 center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
ajuma77b6ba32016-01-08 14:58:35 -080086
87 // Test using the image filter
88 {
89 SkPaint paint;
Michael Ludwig55edb502019-08-05 10:41:10 -040090 paint.setImageFilter(SkImageFilters::Paint(gradientPaint, &ir));
ajuma77b6ba32016-01-08 14:58:35 -080091 canvasFilter.scale(SkIntToScalar(2), SkIntToScalar(2));
Michael Ludwig55edb502019-08-05 10:41:10 -040092 canvasFilter.drawRect(SkRect::Make(ir), paint);
ajuma77b6ba32016-01-08 14:58:35 -080093 }
94
95 // Test using the paint directly
96 {
97 canvasPaint.scale(SkIntToScalar(2), SkIntToScalar(2));
Michael Ludwig55edb502019-08-05 10:41:10 -040098 canvasPaint.drawRect(SkRect::Make(ir), gradientPaint);
ajuma77b6ba32016-01-08 14:58:35 -080099 }
100
101 // Assert that both paths yielded the same result
Michael Ludwig55edb502019-08-05 10:41:10 -0400102 for (int y = 0; y < kHeight; ++y) {
ajuma77b6ba32016-01-08 14:58:35 -0800103 const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
104 const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
Michael Ludwig55edb502019-08-05 10:41:10 -0400105 for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
ajuma77b6ba32016-01-08 14:58:35 -0800106 REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
107 }
108 }
109}
110
111DEF_TEST(PaintImageFilter, reporter) {
112 test_unscaled(reporter);
113 test_scaled(reporter);
114}