blob: 90d25d731acfaa43467f0b60c0b5b3c52c963a91 [file] [log] [blame]
junov@chromium.orgb6e16192011-12-09 15:48:03 +00001/*
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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
junov@chromium.orgb6e16192011-12-09 15:48:03 +00008#include "SkColorShader.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "SkGradientShader.h"
10#include "SkShader.h"
11#include "Test.h"
junov@chromium.orgb6e16192011-12-09 15:48:03 +000012
13static void test_bitmap(skiatest::Reporter* reporter) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000014 SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
15
junov@chromium.orgb6e16192011-12-09 15:48:03 +000016 SkBitmap bmp;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000017 bmp.setInfo(info);
rmistry@google.comd6176b02012-08-23 18:14:13 +000018
junov@chromium.orgb6e16192011-12-09 15:48:03 +000019 // test 1: bitmap without pixel data
rmistry@google.comd6176b02012-08-23 18:14:13 +000020 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000021 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
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000027 bmp.allocPixels(info);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000028
29 // test 2: not opaque by default
rmistry@google.comd6176b02012-08-23 18:14:13 +000030 shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000031 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
reed@google.com383a6972013-10-21 14:00:07 +000037 bmp.setAlphaType(kOpaque_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000038 shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000039 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
reed@google.com383a6972013-10-21 14:00:07 +000045 bmp.setAlphaType(kPremul_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000046 shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000047 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
48 REPORTER_ASSERT(reporter, shader);
49 REPORTER_ASSERT(reporter, !shader->isOpaque());
50 shader->unref();
51
52}
53
54static 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
98static 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());
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000106}
107
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000108DEF_TEST(ShaderOpacity, reporter) {
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000109 test_gradient(reporter);
110 test_color(reporter);
111 test_bitmap(reporter);
112}