blob: b154446e68bddf7da2645cf7870ef06c29aee774 [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 Reed75ae4212018-01-23 11:24:08 -05008#include "SkBitmap.h"
junov@chromium.orgb6e16192011-12-09 15:48:03 +00009#include "SkColorShader.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "SkGradientShader.h"
11#include "SkShader.h"
12#include "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
reed1a9b9642016-03-13 14:13:58 -070021 auto shader = SkShader::MakeBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000022 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
23 REPORTER_ASSERT(reporter, shader);
24 REPORTER_ASSERT(reporter, !shader->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000025
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
reed1a9b9642016-03-13 14:13:58 -070030 shader = SkShader::MakeBitmapShader(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());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000034
35 // test 3: explicitly opaque
reed@google.com383a6972013-10-21 14:00:07 +000036 bmp.setAlphaType(kOpaque_SkAlphaType);
reed1a9b9642016-03-13 14:13:58 -070037 shader = SkShader::MakeBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000038 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
39 REPORTER_ASSERT(reporter, shader);
40 REPORTER_ASSERT(reporter, shader->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000041
42 // test 4: explicitly not opaque
reed@google.com383a6972013-10-21 14:00:07 +000043 bmp.setAlphaType(kPremul_SkAlphaType);
reed1a9b9642016-03-13 14:13:58 -070044 shader = SkShader::MakeBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000045 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
46 REPORTER_ASSERT(reporter, shader);
47 REPORTER_ASSERT(reporter, !shader->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000048}
49
reed1a9b9642016-03-13 14:13:58 -070050static void test_gradient(skiatest::Reporter* reporter) {
junov@chromium.orgb6e16192011-12-09 15:48:03 +000051 SkPoint pts[2];
52 pts[0].iset(0, 0);
53 pts[1].iset(1, 0);
54 SkColor colors[2];
55 SkScalar pos[2] = {SkIntToScalar(0), SkIntToScalar(1)};
56 int count = 2;
57 SkShader::TileMode mode = SkShader::kClamp_TileMode;
58
59 // test 1: all opaque
60 colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
61 colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
reed1a9b9642016-03-13 14:13:58 -070062 auto grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000063 REPORTER_ASSERT(reporter, grad);
64 REPORTER_ASSERT(reporter, grad->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000065
66 // test 2: all 0 alpha
67 colors[0] = SkColorSetARGB(0, 0, 0, 0);
68 colors[1] = SkColorSetARGB(0, 0, 0, 0);
reed1a9b9642016-03-13 14:13:58 -070069 grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000070 REPORTER_ASSERT(reporter, grad);
71 REPORTER_ASSERT(reporter, !grad->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000072
73 // test 3: one opaque, one transparent
74 colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
75 colors[1] = SkColorSetARGB(0x40, 0, 0, 0);
reed1a9b9642016-03-13 14:13:58 -070076 grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000077 REPORTER_ASSERT(reporter, grad);
78 REPORTER_ASSERT(reporter, !grad->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000079
80 // test 4: test 3, swapped
81 colors[0] = SkColorSetARGB(0x40, 0, 0, 0);
82 colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
reed1a9b9642016-03-13 14:13:58 -070083 grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode);
junov@chromium.orgb6e16192011-12-09 15:48:03 +000084 REPORTER_ASSERT(reporter, grad);
85 REPORTER_ASSERT(reporter, !grad->isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000086}
87
reed1a9b9642016-03-13 14:13:58 -070088static void test_color(skiatest::Reporter* reporter) {
junov@chromium.orgb6e16192011-12-09 15:48:03 +000089 SkColorShader colorShader1(SkColorSetARGB(0,0,0,0));
90 REPORTER_ASSERT(reporter, !colorShader1.isOpaque());
91 SkColorShader colorShader2(SkColorSetARGB(0xFF,0,0,0));
92 REPORTER_ASSERT(reporter, colorShader2.isOpaque());
93 SkColorShader colorShader3(SkColorSetARGB(0x7F,0,0,0));
94 REPORTER_ASSERT(reporter, !colorShader3.isOpaque());
junov@chromium.orgb6e16192011-12-09 15:48:03 +000095}
96
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000097DEF_TEST(ShaderOpacity, reporter) {
junov@chromium.orgb6e16192011-12-09 15:48:03 +000098 test_gradient(reporter);
99 test_color(reporter);
100 test_bitmap(reporter);
101}