reed@google.com | 47ac84e | 2011-10-06 13:11:25 +0000 | [diff] [blame^] | 1 | |
| 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 |
| 21 | inline float roundf(float x) { return (x-floor(x))>0.5 ? ceil(x) : floor(x); } |
| 22 | #endif |
| 23 | |
| 24 | static 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 | |
| 37 | static 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 | |
| 50 | class AAClipView2 : public SampleView { |
| 51 | public: |
| 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 | |
| 60 | void build_rgn(SkAAClip* clip, SkRegion::Op op) { |
| 61 | clip->setRect(fBase); |
| 62 | #if 0 |
| 63 | SkIRect r = fBase; |
| 64 | r.offset(75, 20); |
| 65 | rgn->op(r, SkRegion::kUnion_Op); |
| 66 | #endif |
| 67 | |
| 68 | SkRect r; |
| 69 | r.set(fRect); |
| 70 | SkPath path; |
| 71 | path.addOval(r); |
| 72 | SkAAClip clip2; |
| 73 | clip2.setPath(path); |
| 74 | // hack |
| 75 | op = SkRegion::kIntersect_Op; |
| 76 | clip->op(clip2, op); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | protected: |
| 81 | // overrides from SkEventSink |
| 82 | virtual bool onQuery(SkEvent* evt) { |
| 83 | if (SampleCode::TitleQ(*evt)) { |
| 84 | SampleCode::TitleR(evt, "AAClips"); |
| 85 | return true; |
| 86 | } |
| 87 | return this->INHERITED::onQuery(evt); |
| 88 | } |
| 89 | |
| 90 | void drawOrig(SkCanvas* canvas, bool bg) { |
| 91 | SkRect r; |
| 92 | SkPaint paint; |
| 93 | |
| 94 | paint.setStyle(SkPaint::kStroke_Style); |
| 95 | if (bg) |
| 96 | paint.setColor(0xFFBBBBBB); |
| 97 | |
| 98 | r.set(fBase); |
| 99 | canvas->drawRect(r, paint); |
| 100 | r.set(fRect); |
| 101 | canvas->drawRect(r, paint); |
| 102 | } |
| 103 | |
| 104 | void drawRgnOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) { |
| 105 | SkAAClip clip; |
| 106 | |
| 107 | this->build_rgn(&clip, op); |
| 108 | |
| 109 | this->drawOrig(canvas, true); |
| 110 | |
| 111 | SkPaint paint; |
| 112 | paint.setColor((color & ~(0xFF << 24)) | (0x44 << 24)); |
| 113 | paint_rgn(canvas, clip, paint); |
| 114 | |
| 115 | paint.setStyle(SkPaint::kStroke_Style); |
| 116 | paint.setColor(color); |
| 117 | paint_rgn(canvas, clip, paint); |
| 118 | } |
| 119 | |
| 120 | virtual void onDrawContent(SkCanvas* canvas) { |
| 121 | |
| 122 | static const struct { |
| 123 | SkColor fColor; |
| 124 | const char* fName; |
| 125 | SkRegion::Op fOp; |
| 126 | } gOps[] = { |
| 127 | { SK_ColorBLACK, "Difference", SkRegion::kDifference_Op }, |
| 128 | { SK_ColorRED, "Intersect", SkRegion::kIntersect_Op }, |
| 129 | { 0xFF008800, "Union", SkRegion::kUnion_Op }, |
| 130 | { SK_ColorBLUE, "XOR", SkRegion::kXOR_Op } |
| 131 | }; |
| 132 | |
| 133 | SkPaint textPaint; |
| 134 | textPaint.setAntiAlias(true); |
| 135 | textPaint.setTextSize(SK_Scalar1*24); |
| 136 | |
| 137 | this->drawOrig(canvas, false); |
| 138 | canvas->save(); |
| 139 | canvas->translate(SkIntToScalar(200), 0); |
| 140 | this->drawRgnOped(canvas, SkRegion::kUnion_Op, SK_ColorBLACK); |
| 141 | canvas->restore(); |
| 142 | |
| 143 | canvas->translate(0, SkIntToScalar(200)); |
| 144 | |
| 145 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) { |
| 146 | canvas->drawText(gOps[op].fName, strlen(gOps[op].fName), SkIntToScalar(75), SkIntToScalar(50), textPaint); |
| 147 | |
| 148 | this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 149 | |
| 150 | canvas->translate(SkIntToScalar(200), 0); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 155 | return fRect.contains(SkScalarRound(x), SkScalarRound(y)) ? new Click(this) : NULL; |
| 156 | } |
| 157 | |
| 158 | virtual bool onClick(Click* click) { |
| 159 | fRect.offset(click->fICurr.fX - click->fIPrev.fX, |
| 160 | click->fICurr.fY - click->fIPrev.fY); |
| 161 | this->inval(NULL); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | private: |
| 166 | SkIRect fBase, fRect; |
| 167 | |
| 168 | typedef SampleView INHERITED; |
| 169 | }; |
| 170 | |
| 171 | ////////////////////////////////////////////////////////////////////////////// |
| 172 | |
| 173 | static SkView* MyFactory() { return new AAClipView2; } |
| 174 | static SkViewRegister reg(MyFactory); |
| 175 | |