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