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"
9#include "sk_tool_utils.h"
10#include "SkRegion.h"
11
12// This GM exercises the use case found in crbug.com/423834.
13// The following pattern:
14//    save();
15//    clipRect(rect, noAA);
16//    drawRect(bigRect, noAA);
17//    restore();
18//
19//    drawRect(rect, noAA);
20// can leave 1 pixel wide remnants of the first rect.
21static void Draw(SkCanvas* canvas, const SkRect& rect) {
22        SkPaint p;
23        p.setAntiAlias(false);
24
25        const SkRect bigRect = SkRect::MakeWH(600, 600);
26
27        canvas->save();
28            // draw a black rect through the clip
29            canvas->save();
30                canvas->clipRect(rect);
31                canvas->drawRect(bigRect, p);
32            canvas->restore();
33
34            // now draw the white rect on top
35            p.setColor(SK_ColorWHITE);
36            canvas->drawRect(rect, p);
37        canvas->restore();
38}
39
40DEF_SIMPLE_GM_BG(clipdrawdraw, canvas, 512, 512,
41                 sk_tool_utils::color_to_565(0xFFCCCCCC)) {
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);
51}
52
53///////////////////////////////////////////////////////////////////////////////////////////////////
54
55DEF_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}
69