blob: f5a86a813da4009e4753eeac1a748b0f84e8fb93 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkRegion.h"
robertphillips028b98a2015-01-14 09:44:02 -080014
robertphillips028b98a2015-01-14 09:44:02 -080015// This GM exercises the use case found in crbug.com/423834.
16// The following pattern:
robertphillipse85a32d2015-02-10 08:16:55 -080017// save();
18// clipRect(rect, noAA);
19// drawRect(bigRect, noAA);
20// restore();
21//
22// drawRect(rect, noAA);
robertphillips028b98a2015-01-14 09:44:02 -080023// can leave 1 pixel wide remnants of the first rect.
halcanary2a243382015-09-09 08:16:41 -070024static void Draw(SkCanvas* canvas, const SkRect& rect) {
robertphillips028b98a2015-01-14 09:44:02 -080025 SkPaint p;
robertphillips028b98a2015-01-14 09:44:02 -080026 p.setAntiAlias(false);
robertphillips028b98a2015-01-14 09:44:02 -080027
robertphillipse85a32d2015-02-10 08:16:55 -080028 const SkRect bigRect = SkRect::MakeWH(600, 600);
robertphillips028b98a2015-01-14 09:44:02 -080029
30 canvas->save();
robertphillipse85a32d2015-02-10 08:16:55 -080031 // draw a black rect through the clip
32 canvas->save();
33 canvas->clipRect(rect);
34 canvas->drawRect(bigRect, p);
35 canvas->restore();
robertphillips028b98a2015-01-14 09:44:02 -080036
robertphillipse85a32d2015-02-10 08:16:55 -080037 // now draw the white rect on top
38 p.setColor(SK_ColorWHITE);
39 canvas->drawRect(rect, p);
robertphillips028b98a2015-01-14 09:44:02 -080040 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070041}
robertphillips028b98a2015-01-14 09:44:02 -080042
Mike Kleind46dce32018-08-16 10:17:03 -040043DEF_SIMPLE_GM_BG(clipdrawdraw, canvas, 512, 512, 0xFFCCCCCC) {
robertphillipse85a32d2015-02-10 08:16:55 -080044 // 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);
robertphillips028b98a2015-01-14 09:44:02 -080053}
Mike Reed1e06d3d2017-02-13 15:33:12 -050054
55///////////////////////////////////////////////////////////////////////////////////////////////////
56
57DEF_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}