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