robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
Mike Reed | c5aa266 | 2017-02-13 16:48:17 -0500 | [diff] [blame] | 10 | #include "SkRegion.h" |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 11 | |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 12 | // This GM exercises the use case found in crbug.com/423834. |
| 13 | // The following pattern: |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 14 | // save(); |
| 15 | // clipRect(rect, noAA); |
| 16 | // drawRect(bigRect, noAA); |
| 17 | // restore(); |
| 18 | // |
| 19 | // drawRect(rect, noAA); |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 20 | // can leave 1 pixel wide remnants of the first rect. |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 21 | static void Draw(SkCanvas* canvas, const SkRect& rect) { |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 22 | SkPaint p; |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 23 | p.setAntiAlias(false); |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 24 | |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 25 | const SkRect bigRect = SkRect::MakeWH(600, 600); |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 26 | |
| 27 | canvas->save(); |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 28 | // draw a black rect through the clip |
| 29 | canvas->save(); |
| 30 | canvas->clipRect(rect); |
| 31 | canvas->drawRect(bigRect, p); |
| 32 | canvas->restore(); |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 33 | |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 34 | // now draw the white rect on top |
| 35 | p.setColor(SK_ColorWHITE); |
| 36 | canvas->drawRect(rect, p); |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 37 | canvas->restore(); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 38 | } |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 39 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 40 | DEF_SIMPLE_GM_BG(clipdrawdraw, canvas, 512, 512, |
| 41 | sk_tool_utils::color_to_565(0xFFCCCCCC)) { |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 42 | // Vertical remnant |
| 43 | const SkRect rect1 = SkRect::MakeLTRB(136.5f, 137.5f, 338.5f, 293.5f); |
| 44 | |
| 45 | // Horizontal remnant |
| 46 | // 179.488 rounds the right way (i.e., 179), 179.499 rounds the wrong way (i.e., 180) |
| 47 | const SkRect rect2 = SkRect::MakeLTRB(207.5f, 179.499f, 530.5f, 429.5f); |
| 48 | |
| 49 | Draw(canvas, rect1); |
| 50 | Draw(canvas, rect2); |
robertphillips | 028b98a | 2015-01-14 09:44:02 -0800 | [diff] [blame] | 51 | } |
Mike Reed | 1e06d3d | 2017-02-13 15:33:12 -0500 | [diff] [blame] | 52 | |
| 53 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 54 | |
| 55 | DEF_SIMPLE_GM(clip_region, canvas, 256, 256) { |
| 56 | SkRegion rgn({ 10, 10, 100, 100 }); |
| 57 | |
| 58 | canvas->save(); |
| 59 | canvas->clipRegion(rgn); |
| 60 | canvas->drawColor(SK_ColorRED); |
| 61 | canvas->restore(); |
| 62 | |
| 63 | SkRect bounds = { 30, 30, 80, 80 }; |
| 64 | canvas->saveLayer(&bounds, nullptr); |
| 65 | canvas->clipRegion(rgn); |
| 66 | canvas->drawColor(SK_ColorBLUE); |
| 67 | canvas->restore(); |
| 68 | } |