blob: 3dd2c2c27e3e584331d838219639905dadd9e73e [file] [log] [blame]
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001/*
2 * Copyright 2013 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 "SkRandom.h"
10#include "SkRRect.h"
11
12namespace skiagm {
13
14// Test out various combinations of nested rects, ovals and rrects.
15class NestedGM : public GM {
16public:
17 NestedGM(bool doAA) : fDoAA(doAA) {
18 this->setBGColor(0xFFDDDDDD);
19 }
20
21protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000022
mtklein36352bf2015-03-25 18:17:31 -070023 SkString onShortName() override {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000024 SkString name("nested");
25 if (fDoAA) {
26 name.append("_aa");
27 } else {
28 name.append("_bw");
29 }
30 return name;
31 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070034 return SkISize::Make(kImageWidth, kImageHeight);
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000035 }
36
37 enum Shapes {
38 kRect_Shape = 0,
39 kRRect_Shape,
40 kOval_Shape,
41 kShapeCount
42 };
43
44 static void AddShape(SkPath* path, const SkRect& rect, Shapes shape, SkPath::Direction dir) {
45 switch (shape) {
46 case kRect_Shape:
47 path->addRect(rect, dir);
48 break;
49 case kRRect_Shape: {
50 SkRRect rr;
51 rr.setRectXY(rect, 5, 5);
52 path->addRRect(rr, dir);
53 break;
54 }
55 case kOval_Shape:
56 path->addOval(rect, dir);
57 break;
58 default:
59 break;
60 }
61 }
62
mtklein36352bf2015-03-25 18:17:31 -070063 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000064
65 SkPaint shapePaint;
66 shapePaint.setColor(SK_ColorBLACK);
67 shapePaint.setAntiAlias(fDoAA);
68
69 SkRect outerRect = SkRect::MakeWH(40, 40);
70
71 SkRect innerRects[] = {
72 { 10, 10, 30, 30 }, // small
73 { .5f, 18, 4.5f, 22 } // smaller and offset to left
74 };
75
76 // draw a background pattern to make transparency errors more apparent
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000077 SkRandom rand;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000078
79 for (int y = 0; y < kImageHeight; y += 10) {
80 for (int x = 0; x < kImageWidth; x += 10) {
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +000081 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x),
82 SkIntToScalar(y),
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000083 10, 10);
84 SkPaint p;
85 p.setColor(rand.nextU() | 0xFF000000);
86 canvas->drawRect(r, p);
87 }
88 }
89
90 canvas->translate(2, 2);
91 for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) {
92 canvas->save();
93 for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) {
94 for (size_t innerRect = 0; innerRect < SK_ARRAY_COUNT(innerRects); ++innerRect) {
95 SkPath path;
96
97 AddShape(&path, outerRect, (Shapes) outerShape, SkPath::kCW_Direction);
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +000098 AddShape(&path, innerRects[innerRect], (Shapes) innerShape,
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000099 SkPath::kCCW_Direction);
100
101 canvas->drawPath(path, shapePaint);
102 canvas->translate(45, 0);
103 }
104 }
105 canvas->restore();
106 canvas->translate(0, 45);
107 }
108
109 }
110
111private:
112 static const int kImageWidth = 269;
113 static const int kImageHeight = 134;
114
115 bool fDoAA;
116
117 typedef GM INHERITED;
118};
119
120///////////////////////////////////////////////////////////////////////////////
121
122DEF_GM( return new NestedGM(true); )
123DEF_GM( return new NestedGM(false); )
124
125
126}