blob: d3b75b370ba41ef9c2a3b97408be4e89cd713168 [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"
bungemand3ebb482015-08-05 13:57:49 -07009#include "SkPath.h"
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000010#include "SkRandom.h"
11#include "SkRRect.h"
12
13namespace skiagm {
14
15// Test out various combinations of nested rects, ovals and rrects.
16class NestedGM : public GM {
17public:
robertphillips2e5b4c52015-05-19 04:35:38 -070018 NestedGM(bool doAA, bool flipped) : fDoAA(doAA), fFlipped(flipped) {
caryclark65cdba62015-06-15 06:51:08 -070019 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000020 }
21
22protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000023
mtklein36352bf2015-03-25 18:17:31 -070024 SkString onShortName() override {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000025 SkString name("nested");
robertphillips2e5b4c52015-05-19 04:35:38 -070026 if (fFlipped) {
27 name.append("_flipY");
28 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000029 if (fDoAA) {
30 name.append("_aa");
31 } else {
32 name.append("_bw");
33 }
34 return name;
35 }
36
mtklein36352bf2015-03-25 18:17:31 -070037 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070038 return SkISize::Make(kImageWidth, kImageHeight);
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000039 }
40
41 enum Shapes {
42 kRect_Shape = 0,
43 kRRect_Shape,
44 kOval_Shape,
45 kShapeCount
46 };
47
48 static void AddShape(SkPath* path, const SkRect& rect, Shapes shape, SkPath::Direction dir) {
49 switch (shape) {
50 case kRect_Shape:
51 path->addRect(rect, dir);
52 break;
53 case kRRect_Shape: {
54 SkRRect rr;
55 rr.setRectXY(rect, 5, 5);
56 path->addRRect(rr, dir);
57 break;
58 }
59 case kOval_Shape:
60 path->addOval(rect, dir);
61 break;
62 default:
63 break;
64 }
65 }
66
mtklein36352bf2015-03-25 18:17:31 -070067 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000068
69 SkPaint shapePaint;
70 shapePaint.setColor(SK_ColorBLACK);
71 shapePaint.setAntiAlias(fDoAA);
72
73 SkRect outerRect = SkRect::MakeWH(40, 40);
74
75 SkRect innerRects[] = {
76 { 10, 10, 30, 30 }, // small
77 { .5f, 18, 4.5f, 22 } // smaller and offset to left
78 };
79
80 // draw a background pattern to make transparency errors more apparent
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000081 SkRandom rand;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000082
83 for (int y = 0; y < kImageHeight; y += 10) {
84 for (int x = 0; x < kImageWidth; x += 10) {
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +000085 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x),
86 SkIntToScalar(y),
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000087 10, 10);
88 SkPaint p;
89 p.setColor(rand.nextU() | 0xFF000000);
90 canvas->drawRect(r, p);
91 }
92 }
93
robertphillips2e5b4c52015-05-19 04:35:38 -070094 SkScalar xOff = 2, yOff = 2;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000095 for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000096 for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) {
97 for (size_t innerRect = 0; innerRect < SK_ARRAY_COUNT(innerRects); ++innerRect) {
98 SkPath path;
99
100 AddShape(&path, outerRect, (Shapes) outerShape, SkPath::kCW_Direction);
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000101 AddShape(&path, innerRects[innerRect], (Shapes) innerShape,
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000102 SkPath::kCCW_Direction);
103
robertphillips2e5b4c52015-05-19 04:35:38 -0700104 canvas->save();
105 if (fFlipped) {
106 canvas->scale(1.0f, -1.0f);
107 canvas->translate(xOff, -yOff - 40.0f);
108 } else {
109 canvas->translate(xOff, yOff);
110 }
111
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000112 canvas->drawPath(path, shapePaint);
robertphillips2e5b4c52015-05-19 04:35:38 -0700113 canvas->restore();
114
115 xOff += 45;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000116 }
117 }
robertphillips2e5b4c52015-05-19 04:35:38 -0700118
119 xOff = 2;
120 yOff += 45;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000121 }
122
123 }
124
125private:
126 static const int kImageWidth = 269;
127 static const int kImageHeight = 134;
128
129 bool fDoAA;
robertphillips2e5b4c52015-05-19 04:35:38 -0700130 bool fFlipped;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000131
132 typedef GM INHERITED;
133};
134
135///////////////////////////////////////////////////////////////////////////////
136
robertphillips2e5b4c52015-05-19 04:35:38 -0700137DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ false); )
138DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ false); )
139DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ true); )
140DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ true); )
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000141
142}