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