halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 7 | |
| 8 | #include "SkGradientShader.h" |
| 9 | #include "SkSurface.h" |
| 10 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 11 | #include "sk_tool_utils.h" |
halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 12 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 13 | static void make_transparency(SkCanvas* canvas, SkScalar width, SkScalar height) { |
halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 14 | SkPoint pts[2]; |
| 15 | pts[0] = SkPoint::Make(0, 0); |
| 16 | pts[1] = SkPoint::Make(width, 0); |
| 17 | const SkColor kColors[] = { |
| 18 | SK_ColorBLACK, |
| 19 | SK_ColorGRAY, |
| 20 | SK_ColorWHITE, |
| 21 | SK_ColorRED, |
| 22 | SK_ColorYELLOW, |
| 23 | SK_ColorGREEN, |
| 24 | SK_ColorCYAN, |
| 25 | SK_ColorBLUE, |
| 26 | SK_ColorMAGENTA, |
| 27 | }; |
| 28 | const SkScalar kRowHeight = height / SK_ARRAY_COUNT(kColors); |
| 29 | for (size_t i = 0; i < SK_ARRAY_COUNT(kColors); ++i) { |
| 30 | SkColor shaderColors[2]; |
| 31 | shaderColors[0] = SK_AlphaTRANSPARENT; |
| 32 | shaderColors[1] = kColors[i]; |
halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 33 | SkPaint p; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 34 | p.setShader(SkGradientShader::MakeLinear(pts, shaderColors, nullptr, 2, |
| 35 | SkShader::kClamp_TileMode)); |
| 36 | canvas->drawRect(SkRect::MakeXYWH(0, i * kRowHeight, width, kRowHeight), p); |
halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | // http://crrev.com/834303005 |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 41 | static sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) { |
halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 42 | SkBitmap bm; |
| 43 | bm.allocN32Pixels(2 * size, 2 * size); |
| 44 | bm.eraseColor(c1); |
| 45 | bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2); |
| 46 | bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 47 | return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); |
halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // http://crrev.com/834303005 |
| 51 | static void checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) { |
| 52 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 53 | paint.setShader(create_checkerboard_shader(c1, c2, size)); |
halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 54 | canvas->drawPaint(paint); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * This GM verifies that a transparent bitmap drawn over a |
| 59 | * checkerboard pattern looks correct. |
| 60 | */ |
halcanary | da9170f | 2015-01-08 12:11:42 -0800 | [diff] [blame] | 61 | DEF_SIMPLE_GM(transparency_check, canvas, 1792, 1080) { |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 62 | checkerboard(canvas, sk_tool_utils::color_to_565(0xFF999999), |
| 63 | sk_tool_utils::color_to_565(0xFF666666), 8); |
halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 64 | { |
| 65 | SkAutoCanvasRestore autoCanvasRestore(canvas, true); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 66 | auto surface(SkSurface::MakeRasterN32Premul(256, 9)); |
halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 67 | make_transparency(surface->getCanvas(), 256.0f, 9.0f); |
halcanary | da9170f | 2015-01-08 12:11:42 -0800 | [diff] [blame] | 68 | canvas->scale(7.0f, 120.0f); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | surface->draw(canvas, 0, 0, nullptr); |
halcanary | a7976be | 2015-01-07 13:23:09 -0800 | [diff] [blame] | 70 | } |
| 71 | } |