junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 1 | /* |
| 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #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.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 13 | |
| 14 | static void test_bitmap(skiatest::Reporter* reporter) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 15 | SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2); |
| 16 | |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 17 | SkBitmap bmp; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 18 | bmp.setInfo(info); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 19 | |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 20 | // test 1: bitmap without pixel data |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 21 | auto shader = bmp.makeShader(SkTileMode::kClamp, SkTileMode::kClamp); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 22 | REPORTER_ASSERT(reporter, shader); |
| 23 | REPORTER_ASSERT(reporter, !shader->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 24 | |
| 25 | // From this point on, we have pixels |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 26 | bmp.allocPixels(info); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 27 | |
| 28 | // test 2: not opaque by default |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 29 | shader = bmp.makeShader(); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 30 | REPORTER_ASSERT(reporter, shader); |
| 31 | REPORTER_ASSERT(reporter, !shader->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 32 | |
| 33 | // test 3: explicitly opaque |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 34 | bmp.setAlphaType(kOpaque_SkAlphaType); |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 35 | shader = bmp.makeShader(); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, shader); |
| 37 | REPORTER_ASSERT(reporter, shader->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 38 | |
| 39 | // test 4: explicitly not opaque |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 40 | bmp.setAlphaType(kPremul_SkAlphaType); |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 41 | shader = bmp.makeShader(); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 42 | REPORTER_ASSERT(reporter, shader); |
| 43 | REPORTER_ASSERT(reporter, !shader->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 44 | } |
| 45 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 46 | static void test_gradient(skiatest::Reporter* reporter) { |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 47 | 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 Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 53 | SkTileMode mode = SkTileMode::kClamp; |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 54 | |
| 55 | // test 1: all opaque |
| 56 | colors[0] = SkColorSetARGB(0xFF, 0, 0, 0); |
| 57 | colors[1] = SkColorSetARGB(0xFF, 0, 0, 0); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 58 | auto grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 59 | REPORTER_ASSERT(reporter, grad); |
| 60 | REPORTER_ASSERT(reporter, grad->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 61 | |
| 62 | // test 2: all 0 alpha |
| 63 | colors[0] = SkColorSetARGB(0, 0, 0, 0); |
| 64 | colors[1] = SkColorSetARGB(0, 0, 0, 0); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 65 | grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 66 | REPORTER_ASSERT(reporter, grad); |
| 67 | REPORTER_ASSERT(reporter, !grad->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 68 | |
| 69 | // test 3: one opaque, one transparent |
| 70 | colors[0] = SkColorSetARGB(0xFF, 0, 0, 0); |
| 71 | colors[1] = SkColorSetARGB(0x40, 0, 0, 0); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 72 | grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, grad); |
| 74 | REPORTER_ASSERT(reporter, !grad->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 75 | |
| 76 | // test 4: test 3, swapped |
| 77 | colors[0] = SkColorSetARGB(0x40, 0, 0, 0); |
| 78 | colors[1] = SkColorSetARGB(0xFF, 0, 0, 0); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 79 | grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 80 | REPORTER_ASSERT(reporter, grad); |
| 81 | REPORTER_ASSERT(reporter, !grad->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 82 | } |
| 83 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 84 | static void test_color(skiatest::Reporter* reporter) { |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 85 | 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.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 91 | } |
| 92 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 93 | DEF_TEST(ShaderOpacity, reporter) { |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 94 | test_gradient(reporter); |
| 95 | test_color(reporter); |
| 96 | test_bitmap(reporter); |
| 97 | } |