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 "Test.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "TestClassDef.h" |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 10 | #include "SkShader.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkColorShader.h" |
| 13 | |
| 14 | static void test_bitmap(skiatest::Reporter* reporter) { |
| 15 | SkBitmap bmp; |
| 16 | bmp.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 17 | |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 18 | // test 1: bitmap without pixel data |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 19 | SkShader* shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 20 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 29 | shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 30 | 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.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 36 | bmp.setAlphaType(kOpaque_SkAlphaType); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 37 | shader = SkShader::CreateBitmapShader(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()); |
| 41 | shader->unref(); |
| 42 | |
| 43 | // test 4: explicitly not opaque |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 44 | bmp.setAlphaType(kPremul_SkAlphaType); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 45 | shader = SkShader::CreateBitmapShader(bmp, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 46 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); |
| 47 | REPORTER_ASSERT(reporter, shader); |
| 48 | REPORTER_ASSERT(reporter, !shader->isOpaque()); |
| 49 | shader->unref(); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | static 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 | |
| 97 | static 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 106 | // with inherrited color, shader must declare itself as opaque, |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 107 | // since lack of opacity will depend solely on the paint |
| 108 | SkColorShader colorShader4; |
| 109 | REPORTER_ASSERT(reporter, colorShader4.isOpaque()); |
| 110 | } |
| 111 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 112 | DEF_TEST(ShaderOpacity, reporter) { |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 113 | test_gradient(reporter); |
| 114 | test_color(reporter); |
| 115 | test_bitmap(reporter); |
| 116 | } |