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 | |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 8 | #include "SkColorShader.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "SkGradientShader.h" |
| 10 | #include "SkShader.h" |
| 11 | #include "Test.h" |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 12 | |
| 13 | static void test_bitmap(skiatest::Reporter* reporter) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 14 | SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2); |
| 15 | |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 16 | SkBitmap bmp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 17 | bmp.setConfig(info); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 18 | |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 19 | // test 1: bitmap without pixel data |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 20 | SkShader* shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 21 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 22 | REPORTER_ASSERT(reporter, shader); |
| 23 | REPORTER_ASSERT(reporter, !shader->isOpaque()); |
| 24 | shader->unref(); |
| 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 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 30 | shader = SkShader::CreateBitmapShader(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()); |
| 34 | shader->unref(); |
| 35 | |
| 36 | // test 3: explicitly opaque |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 37 | bmp.setAlphaType(kOpaque_SkAlphaType); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 38 | shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 39 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 40 | REPORTER_ASSERT(reporter, shader); |
| 41 | REPORTER_ASSERT(reporter, shader->isOpaque()); |
| 42 | shader->unref(); |
| 43 | |
| 44 | // test 4: explicitly not opaque |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 45 | bmp.setAlphaType(kPremul_SkAlphaType); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 46 | shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 47 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 48 | REPORTER_ASSERT(reporter, shader); |
| 49 | REPORTER_ASSERT(reporter, !shader->isOpaque()); |
| 50 | shader->unref(); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | static void test_gradient(skiatest::Reporter* reporter) |
| 55 | { |
| 56 | SkPoint pts[2]; |
| 57 | pts[0].iset(0, 0); |
| 58 | pts[1].iset(1, 0); |
| 59 | SkColor colors[2]; |
| 60 | SkScalar pos[2] = {SkIntToScalar(0), SkIntToScalar(1)}; |
| 61 | int count = 2; |
| 62 | SkShader::TileMode mode = SkShader::kClamp_TileMode; |
| 63 | |
| 64 | // test 1: all opaque |
| 65 | colors[0] = SkColorSetARGB(0xFF, 0, 0, 0); |
| 66 | colors[1] = SkColorSetARGB(0xFF, 0, 0, 0); |
| 67 | SkShader* grad = SkGradientShader::CreateLinear(pts, colors, pos, count, |
| 68 | mode); |
| 69 | REPORTER_ASSERT(reporter, grad); |
| 70 | REPORTER_ASSERT(reporter, grad->isOpaque()); |
| 71 | grad->unref(); |
| 72 | |
| 73 | // test 2: all 0 alpha |
| 74 | colors[0] = SkColorSetARGB(0, 0, 0, 0); |
| 75 | colors[1] = SkColorSetARGB(0, 0, 0, 0); |
| 76 | grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode); |
| 77 | REPORTER_ASSERT(reporter, grad); |
| 78 | REPORTER_ASSERT(reporter, !grad->isOpaque()); |
| 79 | grad->unref(); |
| 80 | |
| 81 | // test 3: one opaque, one transparent |
| 82 | colors[0] = SkColorSetARGB(0xFF, 0, 0, 0); |
| 83 | colors[1] = SkColorSetARGB(0x40, 0, 0, 0); |
| 84 | grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode); |
| 85 | REPORTER_ASSERT(reporter, grad); |
| 86 | REPORTER_ASSERT(reporter, !grad->isOpaque()); |
| 87 | grad->unref(); |
| 88 | |
| 89 | // test 4: test 3, swapped |
| 90 | colors[0] = SkColorSetARGB(0x40, 0, 0, 0); |
| 91 | colors[1] = SkColorSetARGB(0xFF, 0, 0, 0); |
| 92 | grad = SkGradientShader::CreateLinear(pts, colors, pos, count, mode); |
| 93 | REPORTER_ASSERT(reporter, grad); |
| 94 | REPORTER_ASSERT(reporter, !grad->isOpaque()); |
| 95 | grad->unref(); |
| 96 | } |
| 97 | |
| 98 | static void test_color(skiatest::Reporter* reporter) |
| 99 | { |
| 100 | SkColorShader colorShader1(SkColorSetARGB(0,0,0,0)); |
| 101 | REPORTER_ASSERT(reporter, !colorShader1.isOpaque()); |
| 102 | SkColorShader colorShader2(SkColorSetARGB(0xFF,0,0,0)); |
| 103 | REPORTER_ASSERT(reporter, colorShader2.isOpaque()); |
| 104 | SkColorShader colorShader3(SkColorSetARGB(0x7F,0,0,0)); |
| 105 | REPORTER_ASSERT(reporter, !colorShader3.isOpaque()); |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 106 | } |
| 107 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 108 | DEF_TEST(ShaderOpacity, reporter) { |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 109 | test_gradient(reporter); |
| 110 | test_color(reporter); |
| 111 | test_bitmap(reporter); |
| 112 | } |