blob: 6d854e80ca45dc76ffef6d8ca32a8b9790bd0362 [file] [log] [blame]
reed@google.com47ac84e2011-10-06 13:11:25 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SkAAClip.h"
9#include "SampleCode.h"
10#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkPath.h"
14#include "SkRegion.h"
15#include "SkShader.h"
16#include "SkUtils.h"
17#include "SkImageDecoder.h"
18
19#ifdef SK_BUILD_FOR_WIN
20// windows doesn't have roundf
21inline float roundf(float x) { return (x-floor(x))>0.5 ? ceil(x) : floor(x); }
22#endif
23
24static void drawClip(SkCanvas* canvas, const SkAAClip& clip) {
25 SkMask mask;
26 SkBitmap bm;
27
28 clip.copyToMask(&mask);
29 bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(),
30 mask.fBounds.height(), mask.fRowBytes);
31 bm.setPixels(mask.fImage);
32
33 SkPaint paint;
34 canvas->drawBitmap(bm, mask.fBounds.fLeft, mask.fBounds.fTop, &paint);
35}
36
37static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip,
38 const SkPaint& paint) {
39 SkMask mask;
40 SkBitmap bm;
41
42 clip.copyToMask(&mask);
43 bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(),
44 mask.fBounds.height(), mask.fRowBytes);
45 bm.setPixels(mask.fImage);
46
47 canvas->drawBitmap(bm, mask.fBounds.fLeft, mask.fBounds.fTop, &paint);
48}
49
50class AAClipView2 : public SampleView {
51public:
52 AAClipView2() {
53 fBase.set(100, 100, 150, 150);
54 fRect = fBase;
55 fRect.inset(5, 5);
56 fRect.offset(25, 25);
57 this->setBGColor(0xFFDDDDDD);
58 }
59
reed@google.com1c04bf92011-10-10 12:57:12 +000060 static void setAAClip(SkAAClip* clip, const SkIRect& rect) {
reed@google.com47ac84e2011-10-06 13:11:25 +000061 SkRect r;
reed@google.com1c04bf92011-10-10 12:57:12 +000062 r.set(rect);
reed@google.com47ac84e2011-10-06 13:11:25 +000063 SkPath path;
reed@google.com1c04bf92011-10-10 12:57:12 +000064 path.addRoundRect(r, SkIntToScalar(5), SkIntToScalar(5));
65 clip->setPath(path);
66 }
67
68 void build_rgn(SkAAClip* clip, SkRegion::Op op) {
69 setAAClip(clip, fBase);
70
reed@google.com47ac84e2011-10-06 13:11:25 +000071 SkAAClip clip2;
reed@google.com1c04bf92011-10-10 12:57:12 +000072 setAAClip(&clip2, fRect);
reed@google.com47ac84e2011-10-06 13:11:25 +000073 clip->op(clip2, op);
74 }
75
76
77protected:
78 // overrides from SkEventSink
79 virtual bool onQuery(SkEvent* evt) {
80 if (SampleCode::TitleQ(*evt)) {
81 SampleCode::TitleR(evt, "AAClips");
82 return true;
83 }
84 return this->INHERITED::onQuery(evt);
85 }
86
87 void drawOrig(SkCanvas* canvas, bool bg) {
88 SkRect r;
89 SkPaint paint;
90
91 paint.setStyle(SkPaint::kStroke_Style);
92 if (bg)
93 paint.setColor(0xFFBBBBBB);
94
95 r.set(fBase);
96 canvas->drawRect(r, paint);
97 r.set(fRect);
98 canvas->drawRect(r, paint);
99 }
100
reed@google.com1c04bf92011-10-10 12:57:12 +0000101 static void outer_frame(SkCanvas* canvas, const SkIRect& rect) {
102 SkRect r;
103 r.set(rect);
104 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
105
106 SkPaint paint;
107 paint.setColor(SK_ColorGRAY);
108 paint.setStyle(SkPaint::kStroke_Style);
109 canvas->drawRect(r, paint);
110 }
111
reed@google.com47ac84e2011-10-06 13:11:25 +0000112 void drawRgnOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) {
113 SkAAClip clip;
114
115 this->build_rgn(&clip, op);
116
117 this->drawOrig(canvas, true);
118
119 SkPaint paint;
120 paint.setColor((color & ~(0xFF << 24)) | (0x44 << 24));
121 paint_rgn(canvas, clip, paint);
122
123 paint.setStyle(SkPaint::kStroke_Style);
124 paint.setColor(color);
125 paint_rgn(canvas, clip, paint);
reed@google.com1c04bf92011-10-10 12:57:12 +0000126
127 SkAAClip clip2(clip);
reed@google.com29ffc032011-10-13 15:18:49 +0000128 clip2.translate(0, 80);
reed@google.com1c04bf92011-10-10 12:57:12 +0000129 outer_frame(canvas, clip2.getBounds());
130 paint_rgn(canvas, clip2, paint);
reed@google.com47ac84e2011-10-06 13:11:25 +0000131 }
132
133 virtual void onDrawContent(SkCanvas* canvas) {
134
135 static const struct {
136 SkColor fColor;
137 const char* fName;
138 SkRegion::Op fOp;
139 } gOps[] = {
140 { SK_ColorBLACK, "Difference", SkRegion::kDifference_Op },
141 { SK_ColorRED, "Intersect", SkRegion::kIntersect_Op },
142 { 0xFF008800, "Union", SkRegion::kUnion_Op },
143 { SK_ColorBLUE, "XOR", SkRegion::kXOR_Op }
144 };
145
146 SkPaint textPaint;
147 textPaint.setAntiAlias(true);
148 textPaint.setTextSize(SK_Scalar1*24);
149
150 this->drawOrig(canvas, false);
reed@google.com47ac84e2011-10-06 13:11:25 +0000151
152 canvas->translate(0, SkIntToScalar(200));
153
154 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) {
155 canvas->drawText(gOps[op].fName, strlen(gOps[op].fName), SkIntToScalar(75), SkIntToScalar(50), textPaint);
156
157 this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
158
159 canvas->translate(SkIntToScalar(200), 0);
160 }
161 }
162
163 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
164 return fRect.contains(SkScalarRound(x), SkScalarRound(y)) ? new Click(this) : NULL;
165 }
166
167 virtual bool onClick(Click* click) {
168 fRect.offset(click->fICurr.fX - click->fIPrev.fX,
169 click->fICurr.fY - click->fIPrev.fY);
170 this->inval(NULL);
171 return true;
172 }
173
174private:
175 SkIRect fBase, fRect;
176
177 typedef SampleView INHERITED;
178};
179
180//////////////////////////////////////////////////////////////////////////////
181
182static SkView* MyFactory() { return new AAClipView2; }
183static SkViewRegister reg(MyFactory);
184