1 /*
2 * Copyright 2011 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
8 #include "SkBitmap.h"
9 #include "SkColorShader.h"
10 #include "SkGradientShader.h"
11 #include "SkShader.h"
12 #include "Test.h"
13
test_bitmap(skiatest::Reporter * reporter)14 static void test_bitmap(skiatest::Reporter* reporter) {
15 SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
16
17 SkBitmap bmp;
18 bmp.setInfo(info);
19
20 // test 1: bitmap without pixel data
21 auto shader = SkShader::MakeBitmapShader(bmp,
22 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
23 REPORTER_ASSERT(reporter, shader);
24 REPORTER_ASSERT(reporter, !shader->isOpaque());
25
26 // From this point on, we have pixels
27 bmp.allocPixels(info);
28
29 // test 2: not opaque by default
30 shader = SkShader::MakeBitmapShader(bmp,
31 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
32 REPORTER_ASSERT(reporter, shader);
33 REPORTER_ASSERT(reporter, !shader->isOpaque());
34
35 // test 3: explicitly opaque
36 bmp.setAlphaType(kOpaque_SkAlphaType);
37 shader = SkShader::MakeBitmapShader(bmp,
38 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
39 REPORTER_ASSERT(reporter, shader);
40 REPORTER_ASSERT(reporter, shader->isOpaque());
41
42 // test 4: explicitly not opaque
43 bmp.setAlphaType(kPremul_SkAlphaType);
44 shader = SkShader::MakeBitmapShader(bmp,
45 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
46 REPORTER_ASSERT(reporter, shader);
47 REPORTER_ASSERT(reporter, !shader->isOpaque());
48 }
49
test_gradient(skiatest::Reporter * reporter)50 static void test_gradient(skiatest::Reporter* reporter) {
51 SkPoint pts[2];
52 pts[0].iset(0, 0);
53 pts[1].iset(1, 0);
54 SkColor colors[2];
55 SkScalar pos[2] = {SkIntToScalar(0), SkIntToScalar(1)};
56 int count = 2;
57 SkShader::TileMode mode = SkShader::kClamp_TileMode;
58
59 // test 1: all opaque
60 colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
61 colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
62 auto grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
63 REPORTER_ASSERT(reporter, grad);
64 REPORTER_ASSERT(reporter, grad->isOpaque());
65
66 // test 2: all 0 alpha
67 colors[0] = SkColorSetARGB(0, 0, 0, 0);
68 colors[1] = SkColorSetARGB(0, 0, 0, 0);
69 grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
70 REPORTER_ASSERT(reporter, grad);
71 REPORTER_ASSERT(reporter, !grad->isOpaque());
72
73 // test 3: one opaque, one transparent
74 colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
75 colors[1] = SkColorSetARGB(0x40, 0, 0, 0);
76 grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
77 REPORTER_ASSERT(reporter, grad);
78 REPORTER_ASSERT(reporter, !grad->isOpaque());
79
80 // test 4: test 3, swapped
81 colors[0] = SkColorSetARGB(0x40, 0, 0, 0);
82 colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
83 grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
84 REPORTER_ASSERT(reporter, grad);
85 REPORTER_ASSERT(reporter, !grad->isOpaque());
86 }
87
test_color(skiatest::Reporter * reporter)88 static void test_color(skiatest::Reporter* reporter) {
89 SkColorShader colorShader1(SkColorSetARGB(0,0,0,0));
90 REPORTER_ASSERT(reporter, !colorShader1.isOpaque());
91 SkColorShader colorShader2(SkColorSetARGB(0xFF,0,0,0));
92 REPORTER_ASSERT(reporter, colorShader2.isOpaque());
93 SkColorShader colorShader3(SkColorSetARGB(0x7F,0,0,0));
94 REPORTER_ASSERT(reporter, !colorShader3.isOpaque());
95 }
96
DEF_TEST(ShaderOpacity,reporter)97 DEF_TEST(ShaderOpacity, reporter) {
98 test_gradient(reporter);
99 test_color(reporter);
100 test_bitmap(reporter);
101 }
102