blob: 0af25d1ec21e9e09e7c79c49a248f1bce2357f53 [file] [log] [blame]
junov@chromium.orgb6e16192011-12-09 15:48:03 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "Test.h"
9#include "SkShader.h"
10#include "SkGradientShader.h"
11#include "SkColorShader.h"
12
13static void test_bitmap(skiatest::Reporter* reporter) {
14 SkBitmap bmp;
15 bmp.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +000016
junov@chromium.orgb6e16192011-12-09 15:48:03 +000017 // test 1: bitmap without pixel data
rmistry@google.comd6176b02012-08-23 18:14:13 +000018 SkShader* shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000019 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
20 REPORTER_ASSERT(reporter, shader);
21 REPORTER_ASSERT(reporter, !shader->isOpaque());
22 shader->unref();
23
24 // From this point on, we have pixels
25 bmp.allocPixels();
26
27 // test 2: not opaque by default
rmistry@google.comd6176b02012-08-23 18:14:13 +000028 shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000029 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
30 REPORTER_ASSERT(reporter, shader);
31 REPORTER_ASSERT(reporter, !shader->isOpaque());
32 shader->unref();
33
34 // test 3: explicitly opaque
35 bmp.setIsOpaque(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +000036 shader = SkShader::CreateBitmapShader(bmp,
junov@chromium.orgb6e16192011-12-09 15:48:03 +000037 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
38 REPORTER_ASSERT(reporter, shader);
39 REPORTER_ASSERT(reporter, shader->isOpaque());
40 shader->unref();
41
42 // test 4: explicitly not opaque
43 bmp.setIsOpaque(false);
rmistry@google.comd6176b02012-08-23 18:14:13 +000044 shader = SkShader::CreateBitmapShader(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());
48 shader->unref();
49
50}
51
52static void test_gradient(skiatest::Reporter* reporter)
53{
54 SkPoint pts[2];
55 pts[0].iset(0, 0);
56 pts[1].iset(1, 0);
57 SkColor colors[2];
58 SkScalar pos[2] = {SkIntToScalar(0), SkIntToScalar(1)};
59 int count = 2;
60 SkShader::TileMode mode = SkShader::kClamp_TileMode;
61
62 // test 1: all opaque
63 colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
64 colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
65 SkShader* grad = SkGradientShader::CreateLinear(pts, colors, pos, count,
66 mode);
67 REPORTER_ASSERT(reporter, grad);
68 REPORTER_ASSERT(reporter, grad->isOpaque());
69 grad->unref();
70
71 // test 2: all 0 alpha
72 colors[0] = SkColorSetARGB(0, 0, 0, 0);
73 colors[1] = SkColorSetARGB(0, 0, 0, 0);
74 grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode);
75 REPORTER_ASSERT(reporter, grad);
76 REPORTER_ASSERT(reporter, !grad->isOpaque());
77 grad->unref();
78
79 // test 3: one opaque, one transparent
80 colors[0] = SkColorSetARGB(0xFF, 0, 0, 0);
81 colors[1] = SkColorSetARGB(0x40, 0, 0, 0);
82 grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode);
83 REPORTER_ASSERT(reporter, grad);
84 REPORTER_ASSERT(reporter, !grad->isOpaque());
85 grad->unref();
86
87 // test 4: test 3, swapped
88 colors[0] = SkColorSetARGB(0x40, 0, 0, 0);
89 colors[1] = SkColorSetARGB(0xFF, 0, 0, 0);
90 grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode);
91 REPORTER_ASSERT(reporter, grad);
92 REPORTER_ASSERT(reporter, !grad->isOpaque());
93 grad->unref();
94}
95
96static void test_color(skiatest::Reporter* reporter)
97{
98 SkColorShader colorShader1(SkColorSetARGB(0,0,0,0));
99 REPORTER_ASSERT(reporter, !colorShader1.isOpaque());
100 SkColorShader colorShader2(SkColorSetARGB(0xFF,0,0,0));
101 REPORTER_ASSERT(reporter, colorShader2.isOpaque());
102 SkColorShader colorShader3(SkColorSetARGB(0x7F,0,0,0));
103 REPORTER_ASSERT(reporter, !colorShader3.isOpaque());
104
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105 // with inherrited color, shader must declare itself as opaque,
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000106 // since lack of opacity will depend solely on the paint
107 SkColorShader colorShader4;
108 REPORTER_ASSERT(reporter, colorShader4.isOpaque());
109}
110
111static void test_shader_opacity(skiatest::Reporter* reporter)
112{
113 test_gradient(reporter);
114 test_color(reporter);
115 test_bitmap(reporter);
116}
117
118#include "TestClassDef.h"
119DEFINE_TESTCLASS("ShaderOpacity", ShaderOpacityTestClass, test_shader_opacity)