blob: 9c1370c56918c9a084dc3112a371a399a65dd940 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
13#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkRect.h"
15#include "include/core/SkScalar.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/utils/SkRandom.h"
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000020
21namespace skiagm {
22
23// Test out various combinations of nested rects, ovals and rrects.
24class NestedGM : public GM {
25public:
robertphillips2e5b4c52015-05-19 04:35:38 -070026 NestedGM(bool doAA, bool flipped) : fDoAA(doAA), fFlipped(flipped) {
Mike Kleind46dce32018-08-16 10:17:03 -040027 this->setBGColor(0xFFDDDDDD);
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000028 }
29
30protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000031
mtklein36352bf2015-03-25 18:17:31 -070032 SkString onShortName() override {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000033 SkString name("nested");
robertphillips2e5b4c52015-05-19 04:35:38 -070034 if (fFlipped) {
35 name.append("_flipY");
36 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000037 if (fDoAA) {
38 name.append("_aa");
39 } else {
40 name.append("_bw");
41 }
42 return name;
43 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070046 return SkISize::Make(kImageWidth, kImageHeight);
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000047 }
48
49 enum Shapes {
50 kRect_Shape = 0,
51 kRRect_Shape,
52 kOval_Shape,
53 kShapeCount
54 };
55
Mike Reed30bc5272019-11-22 18:34:02 +000056 static void AddShape(SkPath* path, const SkRect& rect, Shapes shape, SkPathDirection dir) {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000057 switch (shape) {
58 case kRect_Shape:
59 path->addRect(rect, dir);
60 break;
61 case kRRect_Shape: {
62 SkRRect rr;
63 rr.setRectXY(rect, 5, 5);
64 path->addRRect(rr, dir);
65 break;
66 }
67 case kOval_Shape:
68 path->addOval(rect, dir);
69 break;
70 default:
71 break;
72 }
73 }
74
mtklein36352bf2015-03-25 18:17:31 -070075 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000076
77 SkPaint shapePaint;
78 shapePaint.setColor(SK_ColorBLACK);
79 shapePaint.setAntiAlias(fDoAA);
80
81 SkRect outerRect = SkRect::MakeWH(40, 40);
82
83 SkRect innerRects[] = {
84 { 10, 10, 30, 30 }, // small
85 { .5f, 18, 4.5f, 22 } // smaller and offset to left
86 };
87
88 // draw a background pattern to make transparency errors more apparent
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000089 SkRandom rand;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000090
91 for (int y = 0; y < kImageHeight; y += 10) {
92 for (int x = 0; x < kImageWidth; x += 10) {
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +000093 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x),
94 SkIntToScalar(y),
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000095 10, 10);
96 SkPaint p;
97 p.setColor(rand.nextU() | 0xFF000000);
98 canvas->drawRect(r, p);
99 }
100 }
101
robertphillips2e5b4c52015-05-19 04:35:38 -0700102 SkScalar xOff = 2, yOff = 2;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000103 for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000104 for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) {
105 for (size_t innerRect = 0; innerRect < SK_ARRAY_COUNT(innerRects); ++innerRect) {
106 SkPath path;
107
Mike Reed30bc5272019-11-22 18:34:02 +0000108 AddShape(&path, outerRect, (Shapes) outerShape, SkPathDirection::kCW);
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000109 AddShape(&path, innerRects[innerRect], (Shapes) innerShape,
Mike Reed30bc5272019-11-22 18:34:02 +0000110 SkPathDirection::kCCW);
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000111
robertphillips2e5b4c52015-05-19 04:35:38 -0700112 canvas->save();
113 if (fFlipped) {
114 canvas->scale(1.0f, -1.0f);
115 canvas->translate(xOff, -yOff - 40.0f);
116 } else {
117 canvas->translate(xOff, yOff);
118 }
119
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000120 canvas->drawPath(path, shapePaint);
robertphillips2e5b4c52015-05-19 04:35:38 -0700121 canvas->restore();
122
123 xOff += 45;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000124 }
125 }
robertphillips2e5b4c52015-05-19 04:35:38 -0700126
127 xOff = 2;
128 yOff += 45;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000129 }
130
131 }
132
133private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700134 static constexpr int kImageWidth = 269;
135 static constexpr int kImageHeight = 134;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000136
137 bool fDoAA;
robertphillips2e5b4c52015-05-19 04:35:38 -0700138 bool fFlipped;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000139
140 typedef GM INHERITED;
141};
142
143///////////////////////////////////////////////////////////////////////////////
144
robertphillips2e5b4c52015-05-19 04:35:38 -0700145DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ false); )
146DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ false); )
147DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ true); )
148DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ true); )
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000149
150}