blob: b3d75cfa396e6b06b64a292bacced94ae3287b67 [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:
22 virtual SkString onShortName() SK_OVERRIDE {
23 SkString name("nested");
24 if (fDoAA) {
25 name.append("_aa");
26 } else {
27 name.append("_bw");
28 }
29 return name;
30 }
31
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +000032 virtual SkISize onISize() SK_OVERRIDE {
33 return make_isize(kImageWidth, kImageHeight);
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000034 }
35
36 enum Shapes {
37 kRect_Shape = 0,
38 kRRect_Shape,
39 kOval_Shape,
40 kShapeCount
41 };
42
43 static void AddShape(SkPath* path, const SkRect& rect, Shapes shape, SkPath::Direction dir) {
44 switch (shape) {
45 case kRect_Shape:
46 path->addRect(rect, dir);
47 break;
48 case kRRect_Shape: {
49 SkRRect rr;
50 rr.setRectXY(rect, 5, 5);
51 path->addRRect(rr, dir);
52 break;
53 }
54 case kOval_Shape:
55 path->addOval(rect, dir);
56 break;
57 default:
58 break;
59 }
60 }
61
62 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
63
64 SkPaint shapePaint;
65 shapePaint.setColor(SK_ColorBLACK);
66 shapePaint.setAntiAlias(fDoAA);
67
68 SkRect outerRect = SkRect::MakeWH(40, 40);
69
70 SkRect innerRects[] = {
71 { 10, 10, 30, 30 }, // small
72 { .5f, 18, 4.5f, 22 } // smaller and offset to left
73 };
74
75 // draw a background pattern to make transparency errors more apparent
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000076 SkRandom rand;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000077
78 for (int y = 0; y < kImageHeight; y += 10) {
79 for (int x = 0; x < kImageWidth; x += 10) {
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +000080 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x),
81 SkIntToScalar(y),
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000082 10, 10);
83 SkPaint p;
84 p.setColor(rand.nextU() | 0xFF000000);
85 canvas->drawRect(r, p);
86 }
87 }
88
89 canvas->translate(2, 2);
90 for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) {
91 canvas->save();
92 for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) {
93 for (size_t innerRect = 0; innerRect < SK_ARRAY_COUNT(innerRects); ++innerRect) {
94 SkPath path;
95
96 AddShape(&path, outerRect, (Shapes) outerShape, SkPath::kCW_Direction);
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +000097 AddShape(&path, innerRects[innerRect], (Shapes) innerShape,
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000098 SkPath::kCCW_Direction);
99
100 canvas->drawPath(path, shapePaint);
101 canvas->translate(45, 0);
102 }
103 }
104 canvas->restore();
105 canvas->translate(0, 45);
106 }
107
108 }
109
110private:
111 static const int kImageWidth = 269;
112 static const int kImageHeight = 134;
113
114 bool fDoAA;
115
116 typedef GM INHERITED;
117};
118
119///////////////////////////////////////////////////////////////////////////////
120
121DEF_GM( return new NestedGM(true); )
122DEF_GM( return new NestedGM(false); )
123
124
125}