• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkRegion.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.
Draw(SkCanvas * canvas,const SkRect & rect)24 static 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 
43 DEF_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 
57 DEF_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 }
71