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