blob: 93a77cb4d1d87f3de4080897b589a7be54604e23 [file] [log] [blame]
reed@google.com32287892011-10-05 16:27:44 +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
9#include "SampleCode.h"
10#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkAAClip.h"
13
14static void drawClip(SkCanvas* canvas, const SkAAClip& clip) {
15 SkMask mask;
16 SkBitmap bm;
17
18 clip.copyToMask(&mask);
reed@google.com045e62d2011-10-24 12:19:46 +000019 SkAutoMaskFreeImage amfi(mask.fImage);
20
reed@google.com32287892011-10-05 16:27:44 +000021 bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(),
22 mask.fBounds.height(), mask.fRowBytes);
23 bm.setPixels(mask.fImage);
24
25 SkPaint paint;
26 canvas->drawBitmap(bm, mask.fBounds.fLeft, mask.fBounds.fTop, &paint);
27}
28
29class AAClipView : public SampleView {
30public:
31 AAClipView() {
32 }
33
34protected:
35 // overrides from SkEventSink
36 virtual bool onQuery(SkEvent* evt) {
37 if (SampleCode::TitleQ(*evt)) {
38 SampleCode::TitleR(evt, "AAClip");
39 return true;
40 }
41 return this->INHERITED::onQuery(evt);
42 }
43
44 virtual void onDrawContent(SkCanvas* canvas) {
45#if 1
46 SkAAClip aaclip;
47 SkPath path;
48 SkRect bounds;
49
50 bounds.set(0, 0, 20, 20);
51 bounds.inset(SK_ScalarHalf, SK_ScalarHalf);
52
53// path.addRect(bounds);
54// path.addOval(bounds);
55 path.addRoundRect(bounds, 4, 4);
56 aaclip.setPath(path);
57 canvas->translate(30, 30);
58 drawClip(canvas, aaclip);
59
60 SkAAClip aaclip2;
61 path.offset(10, 10);
62 aaclip2.setPath(path);
63 canvas->translate(30, 0);
64 drawClip(canvas, aaclip2);
65
66 SkAAClip aaclip3;
67 aaclip3.op(aaclip, aaclip2, SkRegion::kIntersect_Op);
68 canvas->translate(30, 0);
69 drawClip(canvas, aaclip3);
70
71#endif
72
73#if 0
74 SkRect r;
75 r.set(0, 0, this->width(), this->height());
76 r.inset(20, 20);
77 canvas->clipRect(r);
78
79 SkPath path;
80 path.addRect(r);
81 SkPaint paint;
82 paint.setAntiAlias(true);
83 paint.setColor(SK_ColorRED);
84 canvas->drawPath(path, paint);
85#endif
86 }
87
88private:
89 typedef SkView INHERITED;
90};
91
92//////////////////////////////////////////////////////////////////////////////
93
94static SkView* MyFactory() { return new AAClipView; }
95static SkViewRegister reg(MyFactory);
96