blob: f518caebc39d9e5c66437faaf36eefb95e4a7de6 [file] [log] [blame]
bsalomon@google.coma3108262011-10-10 14:08:47 +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 */
7
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
11#include "SkShader.h"
12
reed@google.com76166152011-11-14 13:14:58 +000013static void test4(SkCanvas* canvas) {
14 SkPaint paint;
15 paint.setAntiAlias(true);
16 SkPoint pts[] = {
17 {10, 160}, {610, 160},
18 {610, 160}, {10, 160},
19
20 {610, 160}, {610, 160},
21 {610, 199}, {610, 199},
22
23 {10, 198}, {610, 198},
24 {610, 199}, {10, 199},
25
26 {10, 160}, {10, 160},
27 {10, 199}, {10, 199}
28 };
29 char verbs[] = {
30 0, 1, 1, 1, 4,
31 0, 1, 1, 1, 4,
32 0, 1, 1, 1, 4,
33 0, 1, 1, 1, 4
34 };
35 SkPath path;
36 SkPoint* ptPtr = pts;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000037 for (size_t i = 0; i < sizeof(verbs); ++i) {
reed@google.com76166152011-11-14 13:14:58 +000038 switch ((SkPath::Verb) verbs[i]) {
39 case SkPath::kMove_Verb:
40 path.moveTo(ptPtr->fX, ptPtr->fY);
41 ++ptPtr;
42 break;
43 case SkPath::kLine_Verb:
44 path.lineTo(ptPtr->fX, ptPtr->fY);
45 ++ptPtr;
46 break;
47 case SkPath::kClose_Verb:
48 path.close();
49 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000050 default:
51 SkASSERT(false);
52 break;
reed@google.com76166152011-11-14 13:14:58 +000053 }
54 }
55 SkRect clip = {0, 130, 772, 531};
56 canvas->clipRect(clip);
57 canvas->drawPath(path, paint);
58}
59
bsalomon@google.coma3108262011-10-10 14:08:47 +000060static SkCanvas* create_canvas(int w, int h) {
61 SkBitmap bm;
62 bm.setConfig(SkBitmap::kARGB_8888_Config, w, h);
63 bm.allocPixels();
64 bm.eraseColor(0);
65 return new SkCanvas(bm);
66}
67
68static const SkBitmap& extract_bitmap(SkCanvas* canvas) {
69 return canvas->getDevice()->accessBitmap(false);
70}
71
72static const struct {
73 SkXfermode::Mode fMode;
74 const char* fLabel;
75} gModes[] = {
76 { SkXfermode::kClear_Mode, "Clear" },
77 { SkXfermode::kSrc_Mode, "Src" },
78 { SkXfermode::kDst_Mode, "Dst" },
79 { SkXfermode::kSrcOver_Mode, "SrcOver" },
80 { SkXfermode::kDstOver_Mode, "DstOver" },
81 { SkXfermode::kSrcIn_Mode, "SrcIn" },
82 { SkXfermode::kDstIn_Mode, "DstIn" },
83 { SkXfermode::kSrcOut_Mode, "SrcOut" },
84 { SkXfermode::kDstOut_Mode, "DstOut" },
85 { SkXfermode::kSrcATop_Mode, "SrcATop" },
86 { SkXfermode::kDstATop_Mode, "DstATop" },
87 { SkXfermode::kXor_Mode, "Xor" },
88};
89
90const int gWidth = 64;
91const int gHeight = 64;
92const SkScalar W = SkIntToScalar(gWidth);
93const SkScalar H = SkIntToScalar(gHeight);
94
95static SkScalar drawCell(SkCanvas* canvas, SkXfermode* mode,
96 SkAlpha a0, SkAlpha a1) {
97
98 SkPaint paint;
99 paint.setAntiAlias(true);
100
101 SkRect r = SkRect::MakeWH(W, H);
102 r.inset(W/10, H/10);
103
104 paint.setColor(SK_ColorBLUE);
105 paint.setAlpha(a0);
106 canvas->drawOval(r, paint);
107
108 paint.setColor(SK_ColorRED);
109 paint.setAlpha(a1);
110 paint.setXfermode(mode);
111
112 SkScalar offset = SK_Scalar1 / 3;
113 SkRect rect = SkRect::MakeXYWH(W / 4 + offset,
114 H / 4 + offset,
115 W / 2, H / 2);
116 canvas->drawRect(rect, paint);
117
118 return H;
119}
120
121static SkShader* make_bg_shader() {
122 SkBitmap bm;
123 bm.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
124 bm.allocPixels();
125 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
126 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC,
127 0xCC, 0xCC);
128
129 SkShader* s = SkShader::CreateBitmapShader(bm,
130 SkShader::kRepeat_TileMode,
131 SkShader::kRepeat_TileMode);
132
133 SkMatrix m;
134 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
135 s->setLocalMatrix(m);
136 return s;
137}
138
139namespace skiagm {
140
141 class AARectModesGM : public GM {
142 SkPaint fBGPaint;
143 public:
144 AARectModesGM () {
145 fBGPaint.setShader(make_bg_shader())->unref();
146 }
147
148 protected:
149
150 virtual SkString onShortName() {
151 return SkString("aarectmodes");
152 }
153
154 virtual SkISize onISize() { return make_isize(640, 480); }
155
156 virtual void onDraw(SkCanvas* canvas) {
reed@google.com76166152011-11-14 13:14:58 +0000157// test4(canvas);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000158 const SkRect bounds = SkRect::MakeWH(W, H);
159 static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
160
161 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
162
163 for (int alpha = 0; alpha < 4; ++alpha) {
164 canvas->save();
165 canvas->save();
166 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
167 if (6 == i) {
168 canvas->restore();
169 canvas->translate(W * 5, 0);
170 canvas->save();
171 }
172 SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
173
174 canvas->drawRect(bounds, fBGPaint);
175 canvas->saveLayer(&bounds, NULL);
176 SkScalar dy = drawCell(canvas, mode,
177 gAlphaValue[alpha & 1],
178 gAlphaValue[alpha & 2]);
179 canvas->restore();
180
181 canvas->translate(0, dy * 5 / 4);
182 SkSafeUnref(mode);
183 }
184 canvas->restore();
185 canvas->restore();
186 canvas->translate(W * 5 / 4, 0);
187 }
188 }
189
190 // disable pdf for now, since it crashes on mac
191 virtual uint32_t onGetFlags() const { return kSkipPDF_Flag; }
192
193 private:
194 typedef GM INHERITED;
195 };
196
197//////////////////////////////////////////////////////////////////////////////
198
199 static GM* MyFactory(void*) { return new AARectModesGM; }
200 static GMRegistry reg(MyFactory);
201
202}
203