blob: 59cf9daf38f29e107b1f3e7b1e533b6caa3ede2b [file] [log] [blame]
robertphillips028b98a2015-01-14 09:44:02 -08001/*
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 Reedc5aa2662017-02-13 16:48:17 -05009#include "SkRegion.h"
robertphillips028b98a2015-01-14 09:44:02 -080010
robertphillips028b98a2015-01-14 09:44:02 -080011// This GM exercises the use case found in crbug.com/423834.
12// The following pattern:
robertphillipse85a32d2015-02-10 08:16:55 -080013// save();
14// clipRect(rect, noAA);
15// drawRect(bigRect, noAA);
16// restore();
17//
18// drawRect(rect, noAA);
robertphillips028b98a2015-01-14 09:44:02 -080019// can leave 1 pixel wide remnants of the first rect.
halcanary2a243382015-09-09 08:16:41 -070020static void Draw(SkCanvas* canvas, const SkRect& rect) {
robertphillips028b98a2015-01-14 09:44:02 -080021 SkPaint p;
robertphillips028b98a2015-01-14 09:44:02 -080022 p.setAntiAlias(false);
robertphillips028b98a2015-01-14 09:44:02 -080023
robertphillipse85a32d2015-02-10 08:16:55 -080024 const SkRect bigRect = SkRect::MakeWH(600, 600);
robertphillips028b98a2015-01-14 09:44:02 -080025
26 canvas->save();
robertphillipse85a32d2015-02-10 08:16:55 -080027 // draw a black rect through the clip
28 canvas->save();
29 canvas->clipRect(rect);
30 canvas->drawRect(bigRect, p);
31 canvas->restore();
robertphillips028b98a2015-01-14 09:44:02 -080032
robertphillipse85a32d2015-02-10 08:16:55 -080033 // now draw the white rect on top
34 p.setColor(SK_ColorWHITE);
35 canvas->drawRect(rect, p);
robertphillips028b98a2015-01-14 09:44:02 -080036 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070037}
robertphillips028b98a2015-01-14 09:44:02 -080038
halcanary2a243382015-09-09 08:16:41 -070039DEF_SIMPLE_GM_BG(clipdrawdraw, canvas, 512, 512,
40 sk_tool_utils::color_to_565(0xFFCCCCCC)) {
robertphillipse85a32d2015-02-10 08:16:55 -080041 // Vertical remnant
42 const SkRect rect1 = SkRect::MakeLTRB(136.5f, 137.5f, 338.5f, 293.5f);
43
44 // Horizontal remnant
45 // 179.488 rounds the right way (i.e., 179), 179.499 rounds the wrong way (i.e., 180)
46 const SkRect rect2 = SkRect::MakeLTRB(207.5f, 179.499f, 530.5f, 429.5f);
47
48 Draw(canvas, rect1);
49 Draw(canvas, rect2);
robertphillips028b98a2015-01-14 09:44:02 -080050}
Mike Reed1e06d3d2017-02-13 15:33:12 -050051
52///////////////////////////////////////////////////////////////////////////////////////////////////
53
54DEF_SIMPLE_GM(clip_region, canvas, 256, 256) {
55 SkRegion rgn({ 10, 10, 100, 100 });
56
57 canvas->save();
58 canvas->clipRegion(rgn);
59 canvas->drawColor(SK_ColorRED);
60 canvas->restore();
61
62 SkRect bounds = { 30, 30, 80, 80 };
63 canvas->saveLayer(&bounds, nullptr);
64 canvas->clipRegion(rgn);
65 canvas->drawColor(SK_ColorBLUE);
66 canvas->restore();
67}