blob: ed795d2fbfd14e427a21d51022db41cefabed586 [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 Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
Mike Reedc5aa2662017-02-13 16:48:17 -050010#include "SkRegion.h"
robertphillips028b98a2015-01-14 09:44:02 -080011
robertphillips028b98a2015-01-14 09:44:02 -080012// This GM exercises the use case found in crbug.com/423834.
13// The following pattern:
robertphillipse85a32d2015-02-10 08:16:55 -080014// save();
15// clipRect(rect, noAA);
16// drawRect(bigRect, noAA);
17// restore();
18//
19// drawRect(rect, noAA);
robertphillips028b98a2015-01-14 09:44:02 -080020// can leave 1 pixel wide remnants of the first rect.
halcanary2a243382015-09-09 08:16:41 -070021static void Draw(SkCanvas* canvas, const SkRect& rect) {
robertphillips028b98a2015-01-14 09:44:02 -080022 SkPaint p;
robertphillips028b98a2015-01-14 09:44:02 -080023 p.setAntiAlias(false);
robertphillips028b98a2015-01-14 09:44:02 -080024
robertphillipse85a32d2015-02-10 08:16:55 -080025 const SkRect bigRect = SkRect::MakeWH(600, 600);
robertphillips028b98a2015-01-14 09:44:02 -080026
27 canvas->save();
robertphillipse85a32d2015-02-10 08:16:55 -080028 // draw a black rect through the clip
29 canvas->save();
30 canvas->clipRect(rect);
31 canvas->drawRect(bigRect, p);
32 canvas->restore();
robertphillips028b98a2015-01-14 09:44:02 -080033
robertphillipse85a32d2015-02-10 08:16:55 -080034 // now draw the white rect on top
35 p.setColor(SK_ColorWHITE);
36 canvas->drawRect(rect, p);
robertphillips028b98a2015-01-14 09:44:02 -080037 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070038}
robertphillips028b98a2015-01-14 09:44:02 -080039
halcanary2a243382015-09-09 08:16:41 -070040DEF_SIMPLE_GM_BG(clipdrawdraw, canvas, 512, 512,
41 sk_tool_utils::color_to_565(0xFFCCCCCC)) {
robertphillipse85a32d2015-02-10 08:16:55 -080042 // 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);
robertphillips028b98a2015-01-14 09:44:02 -080051}
Mike Reed1e06d3d2017-02-13 15:33:12 -050052
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}