blob: d6b653e384d6faf306863bbd753abacce6326fec [file] [log] [blame]
reed@google.com30db5992011-08-29 17:41:02 +00001/*
2 * Copyright 2011 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 */
bsalomon@google.com574f29d2011-08-29 18:05:05 +00007
8
reed@google.com30db5992011-08-29 17:41:02 +00009#include "gm.h"
10#include "SkCanvas.h"
11#include "SkPath.h"
12#include "SkRandom.h"
13
14namespace skiagm {
15
16class ComplexClip2GM : public GM {
17public:
18 ComplexClip2GM() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000019 this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
20
reed@google.com30db5992011-08-29 17:41:02 +000021 SkScalar xA = 0 * SK_Scalar1;
22 SkScalar xB = 10 * SK_Scalar1;
23 SkScalar xC = 20 * SK_Scalar1;
24 SkScalar xD = 30 * SK_Scalar1;
25 SkScalar xE = 40 * SK_Scalar1;
26 SkScalar xF = 50 * SK_Scalar1;
27
28 SkScalar yA = 0 * SK_Scalar1;
29 SkScalar yB = 10 * SK_Scalar1;
30 SkScalar yC = 20 * SK_Scalar1;
31 SkScalar yD = 30 * SK_Scalar1;
32 SkScalar yE = 40 * SK_Scalar1;
33 SkScalar yF = 50 * SK_Scalar1;
34
35 fWidth = xF - xA;
36 fHeight = yF - yA;
37
bsalomon@google.com574f29d2011-08-29 18:05:05 +000038 fRects[0].set(xB, yB, xE, yE);
reed@google.com30db5992011-08-29 17:41:02 +000039 fRectColors[0] = SK_ColorRED;
40
bsalomon@google.com574f29d2011-08-29 18:05:05 +000041 fRects[1].set(xA, yA, xD, yD);
reed@google.com30db5992011-08-29 17:41:02 +000042 fRectColors[1] = SK_ColorGREEN;
43
bsalomon@google.com574f29d2011-08-29 18:05:05 +000044 fRects[2].set(xC, yA, xF, yD);
reed@google.com30db5992011-08-29 17:41:02 +000045 fRectColors[2] = SK_ColorBLUE;
46
bsalomon@google.com574f29d2011-08-29 18:05:05 +000047 fRects[3].set(xA, yC, xD, yF);
reed@google.com30db5992011-08-29 17:41:02 +000048 fRectColors[3] = SK_ColorYELLOW;
49
bsalomon@google.com574f29d2011-08-29 18:05:05 +000050 fRects[4].set(xC, yC, xF, yF);
reed@google.com30db5992011-08-29 17:41:02 +000051 fRectColors[4] = SK_ColorCYAN;
52
53 fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
54 fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
bsalomon@google.com574f29d2011-08-29 18:05:05 +000055
56 SkRegion::Op ops[] = {
57 SkRegion::kDifference_Op,
58 SkRegion::kIntersect_Op,
59 SkRegion::kUnion_Op,
60 SkRegion::kXOR_Op,
61 SkRegion::kReverseDifference_Op,
62 SkRegion::kReplace_Op,
63 };
64
65 SkRandom r;
66 for (int i = 0; i < kRows; ++i) {
67 for (int j = 0; j < kCols; ++j) {
68 for (int k = 0; k < 5; ++k) {
69 fOps[j*kRows+i][k] = ops[r.nextU() % SK_ARRAY_COUNT(ops)];
70 }
71 }
72 }
reed@google.com30db5992011-08-29 17:41:02 +000073 }
74
reed@google.com30db5992011-08-29 17:41:02 +000075protected:
76
77 static const int kRows = 5;
78 static const int kCols = 5;
79 static const int kPadX = 20;
80 static const int kPadY = 20;
81
reed@google.com45482d12011-08-29 19:02:39 +000082 virtual SkString onShortName() {
reed@google.com30db5992011-08-29 17:41:02 +000083 return SkString("complexclip2");
84 }
85
reed@google.com45482d12011-08-29 19:02:39 +000086 virtual SkISize onISize() {
87 return make_isize(SkScalarRoundToInt(fTotalWidth),
88 SkScalarRoundToInt(fTotalHeight));
89 }
reed@google.com30db5992011-08-29 17:41:02 +000090
reed@google.com30db5992011-08-29 17:41:02 +000091 virtual void onDraw(SkCanvas* canvas) {
reed@google.com30db5992011-08-29 17:41:02 +000092 SkPaint rectPaint;
93 rectPaint.setStyle(SkPaint::kStroke_Style);
94 rectPaint.setStrokeWidth(-1);
95
96 SkPaint fillPaint;
97 fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0));
98
99 for (int i = 0; i < kRows; ++i) {
100 for (int j = 0; j < kCols; ++j) {
101 canvas->save();
102 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
103 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
104 canvas->save();
105 for (int k = 0; k < 5; ++k) {
106 canvas->clipRect(fRects[k], fOps[j*kRows+i][k]);
107 }
108 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint);
109 canvas->restore();
110 for (int k = 0; k < 5; ++k) {
111 rectPaint.setColor(fRectColors[k]);
112 canvas->drawRect(fRects[k], rectPaint);
113 }
114 canvas->restore();
115 }
116 }
117 }
118private:
119 SkRect fRects[5];
120 SkColor fRectColors[5];
121 SkRegion::Op fOps[kRows * kCols][5];
122 SkScalar fWidth;
123 SkScalar fHeight;
124 SkScalar fTotalWidth;
125 SkScalar fTotalHeight;
126
127 typedef GM INHERITED;
128};
129
130//////////////////////////////////////////////////////////////////////////////
131
132static GM* MyFactory(void*) { return new ComplexClip2GM; }
133static GMRegistry reg(MyFactory);
134
135}