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); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame^] | 29 | SkAutoMaskFreeImage amfi(mask.fImage); |
| 30 | |
reed@google.com | 47ac84e | 2011-10-06 13:11:25 +0000 | [diff] [blame] | 31 | bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), |
| 32 | mask.fBounds.height(), mask.fRowBytes); |
| 33 | bm.setPixels(mask.fImage); |
| 34 | |
| 35 | SkPaint paint; |
| 36 | canvas->drawBitmap(bm, mask.fBounds.fLeft, mask.fBounds.fTop, &paint); |
| 37 | } |
| 38 | |
| 39 | static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip, |
| 40 | const SkPaint& paint) { |
| 41 | SkMask mask; |
| 42 | SkBitmap bm; |
| 43 | |
| 44 | clip.copyToMask(&mask); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame^] | 45 | SkAutoMaskFreeImage amfi(mask.fImage); |
| 46 | |
reed@google.com | 47ac84e | 2011-10-06 13:11:25 +0000 | [diff] [blame] | 47 | bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), |
| 48 | mask.fBounds.height(), mask.fRowBytes); |
| 49 | bm.setPixels(mask.fImage); |
| 50 | |
| 51 | canvas->drawBitmap(bm, mask.fBounds.fLeft, mask.fBounds.fTop, &paint); |
| 52 | } |
| 53 | |
| 54 | class AAClipView2 : public SampleView { |
| 55 | public: |
| 56 | AAClipView2() { |
| 57 | fBase.set(100, 100, 150, 150); |
| 58 | fRect = fBase; |
| 59 | fRect.inset(5, 5); |
| 60 | fRect.offset(25, 25); |
| 61 | this->setBGColor(0xFFDDDDDD); |
| 62 | } |
| 63 | |
reed@google.com | 1c04bf9 | 2011-10-10 12:57:12 +0000 | [diff] [blame] | 64 | static void setAAClip(SkAAClip* clip, const SkIRect& rect) { |
reed@google.com | 47ac84e | 2011-10-06 13:11:25 +0000 | [diff] [blame] | 65 | SkRect r; |
reed@google.com | 1c04bf9 | 2011-10-10 12:57:12 +0000 | [diff] [blame] | 66 | r.set(rect); |
reed@google.com | 47ac84e | 2011-10-06 13:11:25 +0000 | [diff] [blame] | 67 | SkPath path; |
reed@google.com | 1c04bf9 | 2011-10-10 12:57:12 +0000 | [diff] [blame] | 68 | path.addRoundRect(r, SkIntToScalar(5), SkIntToScalar(5)); |
| 69 | clip->setPath(path); |
| 70 | } |
| 71 | |
| 72 | void build_rgn(SkAAClip* clip, SkRegion::Op op) { |
| 73 | setAAClip(clip, fBase); |
| 74 | |
reed@google.com | 47ac84e | 2011-10-06 13:11:25 +0000 | [diff] [blame] | 75 | SkAAClip clip2; |
reed@google.com | 1c04bf9 | 2011-10-10 12:57:12 +0000 | [diff] [blame] | 76 | setAAClip(&clip2, fRect); |
reed@google.com | 47ac84e | 2011-10-06 13:11:25 +0000 | [diff] [blame] | 77 | clip->op(clip2, op); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | protected: |
| 82 | // overrides from SkEventSink |
| 83 | virtual bool onQuery(SkEvent* evt) { |
| 84 | if (SampleCode::TitleQ(*evt)) { |
| 85 | SampleCode::TitleR(evt, "AAClips"); |
| 86 | return true; |
| 87 | } |
| 88 | return this->INHERITED::onQuery(evt); |
| 89 | } |
| 90 | |
| 91 | void drawOrig(SkCanvas* canvas, bool bg) { |
| 92 | SkRect r; |
| 93 | SkPaint paint; |
| 94 | |
| 95 | paint.setStyle(SkPaint::kStroke_Style); |
| 96 | if (bg) |
| 97 | paint.setColor(0xFFBBBBBB); |
| 98 | |
| 99 | r.set(fBase); |
| 100 | canvas->drawRect(r, paint); |
| 101 | r.set(fRect); |
| 102 | canvas->drawRect(r, paint); |
| 103 | } |
| 104 | |
reed@google.com | 1c04bf9 | 2011-10-10 12:57:12 +0000 | [diff] [blame] | 105 | static void outer_frame(SkCanvas* canvas, const SkIRect& rect) { |
| 106 | SkRect r; |
| 107 | r.set(rect); |
| 108 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 109 | |
| 110 | SkPaint paint; |
| 111 | paint.setColor(SK_ColorGRAY); |
| 112 | paint.setStyle(SkPaint::kStroke_Style); |
| 113 | canvas->drawRect(r, paint); |
| 114 | } |
| 115 | |
reed@google.com | 47ac84e | 2011-10-06 13:11:25 +0000 | [diff] [blame] | 116 | void drawRgnOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) { |
| 117 | SkAAClip clip; |
| 118 | |
| 119 | this->build_rgn(&clip, op); |
| 120 | |
| 121 | this->drawOrig(canvas, true); |
| 122 | |
| 123 | SkPaint paint; |
| 124 | paint.setColor((color & ~(0xFF << 24)) | (0x44 << 24)); |
| 125 | paint_rgn(canvas, clip, paint); |
| 126 | |
| 127 | paint.setStyle(SkPaint::kStroke_Style); |
| 128 | paint.setColor(color); |
| 129 | paint_rgn(canvas, clip, paint); |
reed@google.com | 1c04bf9 | 2011-10-10 12:57:12 +0000 | [diff] [blame] | 130 | |
| 131 | SkAAClip clip2(clip); |
reed@google.com | 29ffc03 | 2011-10-13 15:18:49 +0000 | [diff] [blame] | 132 | clip2.translate(0, 80); |
reed@google.com | 1c04bf9 | 2011-10-10 12:57:12 +0000 | [diff] [blame] | 133 | outer_frame(canvas, clip2.getBounds()); |
| 134 | paint_rgn(canvas, clip2, paint); |
reed@google.com | 47ac84e | 2011-10-06 13:11:25 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | virtual void onDrawContent(SkCanvas* canvas) { |
| 138 | |
| 139 | static const struct { |
| 140 | SkColor fColor; |
| 141 | const char* fName; |
| 142 | SkRegion::Op fOp; |
| 143 | } gOps[] = { |
| 144 | { SK_ColorBLACK, "Difference", SkRegion::kDifference_Op }, |
| 145 | { SK_ColorRED, "Intersect", SkRegion::kIntersect_Op }, |
| 146 | { 0xFF008800, "Union", SkRegion::kUnion_Op }, |
| 147 | { SK_ColorBLUE, "XOR", SkRegion::kXOR_Op } |
| 148 | }; |
| 149 | |
| 150 | SkPaint textPaint; |
| 151 | textPaint.setAntiAlias(true); |
| 152 | textPaint.setTextSize(SK_Scalar1*24); |
| 153 | |
| 154 | this->drawOrig(canvas, false); |
reed@google.com | 47ac84e | 2011-10-06 13:11:25 +0000 | [diff] [blame] | 155 | |
| 156 | canvas->translate(0, SkIntToScalar(200)); |
| 157 | |
| 158 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) { |
| 159 | canvas->drawText(gOps[op].fName, strlen(gOps[op].fName), SkIntToScalar(75), SkIntToScalar(50), textPaint); |
| 160 | |
| 161 | this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 162 | |
| 163 | canvas->translate(SkIntToScalar(200), 0); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 168 | return fRect.contains(SkScalarRound(x), SkScalarRound(y)) ? new Click(this) : NULL; |
| 169 | } |
| 170 | |
| 171 | virtual bool onClick(Click* click) { |
| 172 | fRect.offset(click->fICurr.fX - click->fIPrev.fX, |
| 173 | click->fICurr.fY - click->fIPrev.fY); |
| 174 | this->inval(NULL); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | SkIRect fBase, fRect; |
| 180 | |
| 181 | typedef SampleView INHERITED; |
| 182 | }; |
| 183 | |
| 184 | ////////////////////////////////////////////////////////////////////////////// |
| 185 | |
| 186 | static SkView* MyFactory() { return new AAClipView2; } |
| 187 | static SkViewRegister reg(MyFactory); |
| 188 | |