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