Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
clipdrawdraw.cpp
Go to the documentation of this file.
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/gm.h"
12#include "include/core/SkRect.h"
14
15// This GM exercises the use case found in crbug.com/423834.
16// The following pattern:
17// save();
18// clipRect(rect, noAA);
19// drawRect(bigRect, noAA);
20// restore();
21//
22// drawRect(rect, noAA);
23// can leave 1 pixel wide remnants of the first rect.
24static void Draw(SkCanvas* canvas, const SkRect& rect) {
25 SkPaint p;
26 p.setAntiAlias(false);
27
28 const SkRect bigRect = SkRect::MakeWH(600, 600);
29
30 canvas->save();
31 // draw a black rect through the clip
32 canvas->save();
33 canvas->clipRect(rect);
34 canvas->drawRect(bigRect, p);
35 canvas->restore();
36
37 // now draw the white rect on top
38 p.setColor(SK_ColorWHITE);
39 canvas->drawRect(rect, p);
40 canvas->restore();
41}
42
43DEF_SIMPLE_GM_BG(clipdrawdraw, canvas, 512, 512, 0xFFCCCCCC) {
44 // 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);
53}
54
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}
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
int save()
Definition SkCanvas.cpp:451
static void Draw(SkCanvas *canvas, const SkRect &rect)
#define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR)
Definition gm.h:52
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646