blob: 9040ba6fba6fbfc6a7ae0b4faa4b76208c865506 [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:
robertphillips2e5b4c52015-05-19 04:35:38 -070017 NestedGM(bool doAA, bool flipped) : fDoAA(doAA), fFlipped(flipped) {
caryclark65cdba62015-06-15 06:51:08 -070018 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000019 }
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");
robertphillips2e5b4c52015-05-19 04:35:38 -070025 if (fFlipped) {
26 name.append("_flipY");
27 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000028 if (fDoAA) {
29 name.append("_aa");
30 } else {
31 name.append("_bw");
32 }
33 return name;
34 }
35
mtklein36352bf2015-03-25 18:17:31 -070036 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070037 return SkISize::Make(kImageWidth, kImageHeight);
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000038 }
39
40 enum Shapes {
41 kRect_Shape = 0,
42 kRRect_Shape,
43 kOval_Shape,
44 kShapeCount
45 };
46
47 static void AddShape(SkPath* path, const SkRect& rect, Shapes shape, SkPath::Direction dir) {
48 switch (shape) {
49 case kRect_Shape:
50 path->addRect(rect, dir);
51 break;
52 case kRRect_Shape: {
53 SkRRect rr;
54 rr.setRectXY(rect, 5, 5);
55 path->addRRect(rr, dir);
56 break;
57 }
58 case kOval_Shape:
59 path->addOval(rect, dir);
60 break;
61 default:
62 break;
63 }
64 }
65
mtklein36352bf2015-03-25 18:17:31 -070066 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000067
68 SkPaint shapePaint;
69 shapePaint.setColor(SK_ColorBLACK);
70 shapePaint.setAntiAlias(fDoAA);
71
72 SkRect outerRect = SkRect::MakeWH(40, 40);
73
74 SkRect innerRects[] = {
75 { 10, 10, 30, 30 }, // small
76 { .5f, 18, 4.5f, 22 } // smaller and offset to left
77 };
78
79 // draw a background pattern to make transparency errors more apparent
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000080 SkRandom rand;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000081
82 for (int y = 0; y < kImageHeight; y += 10) {
83 for (int x = 0; x < kImageWidth; x += 10) {
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +000084 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x),
85 SkIntToScalar(y),
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000086 10, 10);
87 SkPaint p;
88 p.setColor(rand.nextU() | 0xFF000000);
89 canvas->drawRect(r, p);
90 }
91 }
92
robertphillips2e5b4c52015-05-19 04:35:38 -070093 SkScalar xOff = 2, yOff = 2;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000094 for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000095 for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) {
96 for (size_t innerRect = 0; innerRect < SK_ARRAY_COUNT(innerRects); ++innerRect) {
97 SkPath path;
98
99 AddShape(&path, outerRect, (Shapes) outerShape, SkPath::kCW_Direction);
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000100 AddShape(&path, innerRects[innerRect], (Shapes) innerShape,
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000101 SkPath::kCCW_Direction);
102
robertphillips2e5b4c52015-05-19 04:35:38 -0700103 canvas->save();
104 if (fFlipped) {
105 canvas->scale(1.0f, -1.0f);
106 canvas->translate(xOff, -yOff - 40.0f);
107 } else {
108 canvas->translate(xOff, yOff);
109 }
110
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000111 canvas->drawPath(path, shapePaint);
robertphillips2e5b4c52015-05-19 04:35:38 -0700112 canvas->restore();
113
114 xOff += 45;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000115 }
116 }
robertphillips2e5b4c52015-05-19 04:35:38 -0700117
118 xOff = 2;
119 yOff += 45;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000120 }
121
122 }
123
124private:
125 static const int kImageWidth = 269;
126 static const int kImageHeight = 134;
127
128 bool fDoAA;
robertphillips2e5b4c52015-05-19 04:35:38 -0700129 bool fFlipped;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000130
131 typedef GM INHERITED;
132};
133
134///////////////////////////////////////////////////////////////////////////////
135
robertphillips2e5b4c52015-05-19 04:35:38 -0700136DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ false); )
137DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ false); )
138DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ true); )
139DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ true); )
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000140
141}