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 Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 8 | #include "SkBitmap.h" |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 9 | #include "SkColorShader.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
| 11 | #include "SkShader.h" |
| 12 | #include "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 |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 21 | auto shader = SkShader::MakeBitmapShader(bmp, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 22 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 23 | REPORTER_ASSERT(reporter, shader); |
| 24 | REPORTER_ASSERT(reporter, !shader->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 25 | |
| 26 | // From this point on, we have pixels |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 27 | bmp.allocPixels(info); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 28 | |
| 29 | // test 2: not opaque by default |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 30 | shader = SkShader::MakeBitmapShader(bmp, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 31 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 32 | REPORTER_ASSERT(reporter, shader); |
| 33 | REPORTER_ASSERT(reporter, !shader->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 34 | |
| 35 | // test 3: explicitly opaque |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 36 | bmp.setAlphaType(kOpaque_SkAlphaType); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 37 | shader = SkShader::MakeBitmapShader(bmp, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 38 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 39 | REPORTER_ASSERT(reporter, shader); |
| 40 | REPORTER_ASSERT(reporter, shader->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 41 | |
| 42 | // test 4: explicitly not opaque |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 43 | bmp.setAlphaType(kPremul_SkAlphaType); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 44 | shader = SkShader::MakeBitmapShader(bmp, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 45 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 46 | REPORTER_ASSERT(reporter, shader); |
| 47 | REPORTER_ASSERT(reporter, !shader->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 48 | } |
| 49 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 50 | static void test_gradient(skiatest::Reporter* reporter) { |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 51 | 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); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 62 | auto grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 63 | REPORTER_ASSERT(reporter, grad); |
| 64 | REPORTER_ASSERT(reporter, grad->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 65 | |
| 66 | // test 2: all 0 alpha |
| 67 | colors[0] = SkColorSetARGB(0, 0, 0, 0); |
| 68 | colors[1] = SkColorSetARGB(0, 0, 0, 0); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 69 | grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, grad); |
| 71 | REPORTER_ASSERT(reporter, !grad->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 72 | |
| 73 | // test 3: one opaque, one transparent |
| 74 | colors[0] = SkColorSetARGB(0xFF, 0, 0, 0); |
| 75 | colors[1] = SkColorSetARGB(0x40, 0, 0, 0); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 76 | grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, grad); |
| 78 | REPORTER_ASSERT(reporter, !grad->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 79 | |
| 80 | // test 4: test 3, swapped |
| 81 | colors[0] = SkColorSetARGB(0x40, 0, 0, 0); |
| 82 | colors[1] = SkColorSetARGB(0xFF, 0, 0, 0); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 83 | grad = SkGradientShader::MakeLinear(pts, colors, pos, count, mode); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 84 | REPORTER_ASSERT(reporter, grad); |
| 85 | REPORTER_ASSERT(reporter, !grad->isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 86 | } |
| 87 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 88 | static void test_color(skiatest::Reporter* reporter) { |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 89 | 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.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 97 | DEF_TEST(ShaderOpacity, reporter) { |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 98 | test_gradient(reporter); |
| 99 | test_color(reporter); |
| 100 | test_bitmap(reporter); |
| 101 | } |