blob: d514516808cafdf26022e93b2aaf53a88f630dc6 [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 "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
junov@chromium.orgb6e16192011-12-09 15:48:03 +000010#include "SkShader.h"
11#include "SkGradientShader.h"
12#include "SkColorShader.h"
13
14static void test_bitmap(skiatest::Reporter* reporter) {
15 SkBitmap bmp;
16 bmp.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +000017
junov@chromium.orgb6e16192011-12-09 15:48:03 +000018 // test 1: bitmap without pixel data
rmistry@google.comd6176b02012-08-23 18:14:13 +000019 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000020 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
21 REPORTER_ASSERT(reporter, shader);
22 REPORTER_ASSERT(reporter, !shader->isOpaque());
23 shader->unref();
24
25 // From this point on, we have pixels
26 bmp.allocPixels();
27
28 // test 2: not opaque by default
rmistry@google.comd6176b02012-08-23 18:14:13 +000029 shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000030 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
31 REPORTER_ASSERT(reporter, shader);
32 REPORTER_ASSERT(reporter, !shader->isOpaque());
33 shader->unref();
34
35 // test 3: explicitly opaque
reed@google.com383a6972013-10-21 14:00:07 +000036 bmp.setAlphaType(kOpaque_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000037 shader = SkShader::CreateBitmapShader(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());
41 shader->unref();
42
43 // test 4: explicitly not opaque
reed@google.com383a6972013-10-21 14:00:07 +000044 bmp.setAlphaType(kPremul_SkAlphaType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000045 shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000046 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
47 REPORTER_ASSERT(reporter, shader);
48 REPORTER_ASSERT(reporter, !shader->isOpaque());
49 shader->unref();
50
51}
52
53static void test_gradient(skiatest::Reporter* reporter)
54{
55 SkPoint pts[2];
56 pts[0].iset(0, 0);
57 pts[1].iset(1, 0);
58 SkColor colors[2];
59 SkScalar pos[2] = {SkIntToScalar(0), SkIntToScalar(1)};
60 int count = 2;
61 SkShader::TileMode mode = SkShader::kClamp_TileMode;
62
63 // test 1: all opaque
64 colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
65 colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
66 SkShader* grad = SkGradientShader::CreateLinear(pts, colors, pos, count,
67 mode);
68 REPORTER_ASSERT(reporter, grad);
69 REPORTER_ASSERT(reporter, grad->isOpaque());
70 grad->unref();
71
72 // test 2: all 0 alpha
73 colors[0] = SkColorSetARGB(0, 0, 0, 0);
74 colors[1] = SkColorSetARGB(0, 0, 0, 0);
75 grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode);
76 REPORTER_ASSERT(reporter, grad);
77 REPORTER_ASSERT(reporter, !grad->isOpaque());
78 grad->unref();
79
80 // test 3: one opaque, one transparent
81 colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
82 colors[1] = SkColorSetARGB(0x40, 0, 0, 0);
83 grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode);
84 REPORTER_ASSERT(reporter, grad);
85 REPORTER_ASSERT(reporter, !grad->isOpaque());
86 grad->unref();
87
88 // test 4: test 3, swapped
89 colors[0] = SkColorSetARGB(0x40, 0, 0, 0);
90 colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
91 grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode);
92 REPORTER_ASSERT(reporter, grad);
93 REPORTER_ASSERT(reporter, !grad->isOpaque());
94 grad->unref();
95}
96
97static void test_color(skiatest::Reporter* reporter)
98{
99 SkColorShader colorShader1(SkColorSetARGB(0,0,0,0));
100 REPORTER_ASSERT(reporter, !colorShader1.isOpaque());
101 SkColorShader colorShader2(SkColorSetARGB(0xFF,0,0,0));
102 REPORTER_ASSERT(reporter, colorShader2.isOpaque());
103 SkColorShader colorShader3(SkColorSetARGB(0x7F,0,0,0));
104 REPORTER_ASSERT(reporter, !colorShader3.isOpaque());
105
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106 // with inherrited color, shader must declare itself as opaque,
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000107 // since lack of opacity will depend solely on the paint
108 SkColorShader colorShader4;
109 REPORTER_ASSERT(reporter, colorShader4.isOpaque());
110}
111
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000112DEF_TEST(ShaderOpacity, reporter) {
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000113 test_gradient(reporter);
114 test_color(reporter);
115 test_bitmap(reporter);
116}