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