blob: 1c646988fa28880cc441455c3e1c11fa5cc81d2d [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() {
19 SkScalar xA = 0 * SK_Scalar1;
20 SkScalar xB = 10 * SK_Scalar1;
21 SkScalar xC = 20 * SK_Scalar1;
22 SkScalar xD = 30 * SK_Scalar1;
23 SkScalar xE = 40 * SK_Scalar1;
24 SkScalar xF = 50 * SK_Scalar1;
25
26 SkScalar yA = 0 * SK_Scalar1;
27 SkScalar yB = 10 * SK_Scalar1;
28 SkScalar yC = 20 * SK_Scalar1;
29 SkScalar yD = 30 * SK_Scalar1;
30 SkScalar yE = 40 * SK_Scalar1;
31 SkScalar yF = 50 * SK_Scalar1;
32
33 fWidth = xF - xA;
34 fHeight = yF - yA;
35
bsalomon@google.com574f29d2011-08-29 18:05:05 +000036 fRects[0].set(xB, yB, xE, yE);
reed@google.com30db5992011-08-29 17:41:02 +000037 fRectColors[0] = SK_ColorRED;
38
bsalomon@google.com574f29d2011-08-29 18:05:05 +000039 fRects[1].set(xA, yA, xD, yD);
reed@google.com30db5992011-08-29 17:41:02 +000040 fRectColors[1] = SK_ColorGREEN;
41
bsalomon@google.com574f29d2011-08-29 18:05:05 +000042 fRects[2].set(xC, yA, xF, yD);
reed@google.com30db5992011-08-29 17:41:02 +000043 fRectColors[2] = SK_ColorBLUE;
44
bsalomon@google.com574f29d2011-08-29 18:05:05 +000045 fRects[3].set(xA, yC, xD, yF);
reed@google.com30db5992011-08-29 17:41:02 +000046 fRectColors[3] = SK_ColorYELLOW;
47
bsalomon@google.com574f29d2011-08-29 18:05:05 +000048 fRects[4].set(xC, yC, xF, yF);
reed@google.com30db5992011-08-29 17:41:02 +000049 fRectColors[4] = SK_ColorCYAN;
50
51 fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
52 fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
bsalomon@google.com574f29d2011-08-29 18:05:05 +000053
54 SkRegion::Op ops[] = {
55 SkRegion::kDifference_Op,
56 SkRegion::kIntersect_Op,
57 SkRegion::kUnion_Op,
58 SkRegion::kXOR_Op,
59 SkRegion::kReverseDifference_Op,
60 SkRegion::kReplace_Op,
61 };
62
63 SkRandom r;
64 for (int i = 0; i < kRows; ++i) {
65 for (int j = 0; j < kCols; ++j) {
66 for (int k = 0; k < 5; ++k) {
67 fOps[j*kRows+i][k] = ops[r.nextU() % SK_ARRAY_COUNT(ops)];
68 }
69 }
70 }
reed@google.com30db5992011-08-29 17:41:02 +000071 }
72
73 virtual bool validForPicture() const { return false; }
74
75protected:
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
82 SkString onShortName() {
83 return SkString("complexclip2");
84 }
85
86 SkISize onISize() { return make_isize(fTotalWidth, fTotalHeight); }
87
88 void drawBG(SkCanvas* canvas) {
89 canvas->drawColor(SkColorSetRGB(0xDD,0xA0,0xDD));
reed@google.com30db5992011-08-29 17:41:02 +000090 }
91
92 virtual void onDraw(SkCanvas* canvas) {
93 this->drawBG(canvas);
94 SkPaint rectPaint;
95 rectPaint.setStyle(SkPaint::kStroke_Style);
96 rectPaint.setStrokeWidth(-1);
97
98 SkPaint fillPaint;
99 fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0));
100
101 for (int i = 0; i < kRows; ++i) {
102 for (int j = 0; j < kCols; ++j) {
103 canvas->save();
104 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
105 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
106 canvas->save();
107 for (int k = 0; k < 5; ++k) {
108 canvas->clipRect(fRects[k], fOps[j*kRows+i][k]);
109 }
110 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint);
111 canvas->restore();
112 for (int k = 0; k < 5; ++k) {
113 rectPaint.setColor(fRectColors[k]);
114 canvas->drawRect(fRects[k], rectPaint);
115 }
116 canvas->restore();
117 }
118 }
119 }
120private:
121 SkRect fRects[5];
122 SkColor fRectColors[5];
123 SkRegion::Op fOps[kRows * kCols][5];
124 SkScalar fWidth;
125 SkScalar fHeight;
126 SkScalar fTotalWidth;
127 SkScalar fTotalHeight;
128
129 typedef GM INHERITED;
130};
131
132//////////////////////////////////////////////////////////////////////////////
133
134static GM* MyFactory(void*) { return new ComplexClip2GM; }
135static GMRegistry reg(MyFactory);
136
137}