blob: bfdf3e7dd08981c821631523259b1d861e5494af [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkShader.h"
10#include "include/effects/SkGradientShader.h"
11#include "src/shaders/SkColorShader.h"
12#include "tests/Test.h"
junov@chromium.orgb6e16192011-12-09 15:48:03 +000013
14static void test_bitmap(skiatest::Reporter* reporter) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000015 SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
16
junov@chromium.orgb6e16192011-12-09 15:48:03 +000017 SkBitmap bmp;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000018 bmp.setInfo(info);
rmistry@google.comd6176b02012-08-23 18:14:13 +000019
junov@chromium.orgb6e16192011-12-09 15:48:03 +000020 // test 1: bitmap without pixel data
Mike Reed50acf8f2019-04-08 13:20:23 -040021 auto shader = bmp.makeShader(SkTileMode::kClamp, SkTileMode::kClamp);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000022 REPORTER_ASSERT(reporter, shader);
23 REPORTER_ASSERT(reporter, !shader->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000024
25 // From this point on, we have pixels
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000026 bmp.allocPixels(info);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000027
28 // test 2: not opaque by default
Mike Reed50acf8f2019-04-08 13:20:23 -040029 shader = bmp.makeShader();
junov@chromium.orgb6e16192011-12-09 15:48:03 +000030 REPORTER_ASSERT(reporter, shader);
31 REPORTER_ASSERT(reporter, !shader->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000032
33 // test 3: explicitly opaque
reed@google.com383a6972013-10-21 14:00:07 +000034 bmp.setAlphaType(kOpaque_SkAlphaType);
Mike Reed50acf8f2019-04-08 13:20:23 -040035 shader = bmp.makeShader();
junov@chromium.orgb6e16192011-12-09 15:48:03 +000036 REPORTER_ASSERT(reporter, shader);
37 REPORTER_ASSERT(reporter, shader->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000038
39 // test 4: explicitly not opaque
reed@google.com383a6972013-10-21 14:00:07 +000040 bmp.setAlphaType(kPremul_SkAlphaType);
Mike Reed50acf8f2019-04-08 13:20:23 -040041 shader = bmp.makeShader();
junov@chromium.orgb6e16192011-12-09 15:48:03 +000042 REPORTER_ASSERT(reporter, shader);
43 REPORTER_ASSERT(reporter, !shader->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000044}
45
reed1a9b9642016-03-13 14:13:58 -070046static void test_gradient(skiatest::Reporter* reporter) {
junov@chromium.orgb6e16192011-12-09 15:48:03 +000047 SkPoint pts[2];
48 pts[0].iset(0, 0);
49 pts[1].iset(1, 0);
50 SkColor colors[2];
51 SkScalar pos[2] = {SkIntToScalar(0), SkIntToScalar(1)};
52 int count = 2;
Mike Reedfae8fce2019-04-03 10:27:45 -040053 SkTileMode mode = SkTileMode::kClamp;
junov@chromium.orgb6e16192011-12-09 15:48:03 +000054
55 // test 1: all opaque
56 colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
57 colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
reed1a9b9642016-03-13 14:13:58 -070058 auto grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000059 REPORTER_ASSERT(reporter, grad);
60 REPORTER_ASSERT(reporter, grad->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000061
62 // test 2: all 0 alpha
63 colors[0] = SkColorSetARGB(0, 0, 0, 0);
64 colors[1] = SkColorSetARGB(0, 0, 0, 0);
reed1a9b9642016-03-13 14:13:58 -070065 grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000066 REPORTER_ASSERT(reporter, grad);
67 REPORTER_ASSERT(reporter, !grad->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000068
69 // test 3: one opaque, one transparent
70 colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
71 colors[1] = SkColorSetARGB(0x40, 0, 0, 0);
reed1a9b9642016-03-13 14:13:58 -070072 grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000073 REPORTER_ASSERT(reporter, grad);
74 REPORTER_ASSERT(reporter, !grad->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000075
76 // test 4: test 3, swapped
77 colors[0] = SkColorSetARGB(0x40, 0, 0, 0);
78 colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
reed1a9b9642016-03-13 14:13:58 -070079 grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000080 REPORTER_ASSERT(reporter, grad);
81 REPORTER_ASSERT(reporter, !grad->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000082}
83
reed1a9b9642016-03-13 14:13:58 -070084static void test_color(skiatest::Reporter* reporter) {
junov@chromium.orgb6e16192011-12-09 15:48:03 +000085 SkColorShader colorShader1(SkColorSetARGB(0,0,0,0));
86 REPORTER_ASSERT(reporter, !colorShader1.isOpaque());
87 SkColorShader colorShader2(SkColorSetARGB(0xFF,0,0,0));
88 REPORTER_ASSERT(reporter, colorShader2.isOpaque());
89 SkColorShader colorShader3(SkColorSetARGB(0x7F,0,0,0));
90 REPORTER_ASSERT(reporter, !colorShader3.isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000091}
92
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000093DEF_TEST(ShaderOpacity, reporter) {
junov@chromium.orgb6e16192011-12-09 15:48:03 +000094 test_gradient(reporter);
95 test_color(reporter);
96 test_bitmap(reporter);
97}