blob: 389d7e3b875cbd375e190de9530cbbfeca82aee0 [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"
9
robertphillips028b98a2015-01-14 09:44:02 -080010// This GM exercises the use case found in crbug.com/423834.
11// The following pattern:
robertphillipse85a32d2015-02-10 08:16:55 -080012// save();
13// clipRect(rect, noAA);
14// drawRect(bigRect, noAA);
15// restore();
16//
17// drawRect(rect, noAA);
robertphillips028b98a2015-01-14 09:44:02 -080018// can leave 1 pixel wide remnants of the first rect.
halcanary2a243382015-09-09 08:16:41 -070019static void Draw(SkCanvas* canvas, const SkRect& rect) {
robertphillips028b98a2015-01-14 09:44:02 -080020 SkPaint p;
robertphillips028b98a2015-01-14 09:44:02 -080021 p.setAntiAlias(false);
robertphillips028b98a2015-01-14 09:44:02 -080022
robertphillipse85a32d2015-02-10 08:16:55 -080023 const SkRect bigRect = SkRect::MakeWH(600, 600);
robertphillips028b98a2015-01-14 09:44:02 -080024
25 canvas->save();
robertphillipse85a32d2015-02-10 08:16:55 -080026 // draw a black rect through the clip
27 canvas->save();
28 canvas->clipRect(rect);
29 canvas->drawRect(bigRect, p);
30 canvas->restore();
robertphillips028b98a2015-01-14 09:44:02 -080031
robertphillipse85a32d2015-02-10 08:16:55 -080032 // now draw the white rect on top
33 p.setColor(SK_ColorWHITE);
34 canvas->drawRect(rect, p);
robertphillips028b98a2015-01-14 09:44:02 -080035 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070036}
robertphillips028b98a2015-01-14 09:44:02 -080037
halcanary2a243382015-09-09 08:16:41 -070038DEF_SIMPLE_GM_BG(clipdrawdraw, canvas, 512, 512,
39 sk_tool_utils::color_to_565(0xFFCCCCCC)) {
robertphillipse85a32d2015-02-10 08:16:55 -080040 // 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);
robertphillips028b98a2015-01-14 09:44:02 -080049}
Mike Reed1e06d3d2017-02-13 15:33:12 -050050
51///////////////////////////////////////////////////////////////////////////////////////////////////
52
53DEF_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}