reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGradientShader.h" |
| 5 | #include "SkPath.h" |
| 6 | #include "SkRegion.h" |
| 7 | #include "SkShader.h" |
| 8 | #include "SkUtils.h" |
| 9 | #include "SkShaderExtras.h" |
| 10 | #include "Sk1DPathEffect.h" |
| 11 | #include "SkCornerPathEffect.h" |
| 12 | #include "SkPathMeasure.h" |
| 13 | #include "SkRandom.h" |
| 14 | #include "SkColorPriv.h" |
| 15 | #include "SkPixelXorXfermode.h" |
| 16 | |
| 17 | static void test_grow(SkPath* path) |
| 18 | { |
| 19 | for (int i = 0; i < 100000; i++) |
| 20 | { |
| 21 | path->lineTo(i, i); |
| 22 | path->lineTo(i, i*2); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | #define CORNER_RADIUS 12 |
| 27 | static SkScalar gPhase; |
| 28 | |
| 29 | static const int gXY[] = { |
| 30 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 31 | }; |
| 32 | |
| 33 | static SkPathEffect* make_pe(int flags) |
| 34 | { |
| 35 | if (flags == 1) |
| 36 | return new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS)); |
| 37 | |
| 38 | SkPath path; |
| 39 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 40 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 41 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 42 | path.close(); |
| 43 | path.offset(SkIntToScalar(-6), 0); |
| 44 | |
| 45 | SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kRotate_Style); |
| 46 | |
| 47 | if (flags == 2) |
| 48 | return outer; |
| 49 | |
| 50 | SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS)); |
| 51 | |
| 52 | SkPathEffect* pe = new SkComposePathEffect(outer, inner); |
| 53 | outer->unref(); |
| 54 | inner->unref(); |
| 55 | return pe; |
| 56 | } |
| 57 | |
| 58 | static SkPathEffect* make_warp_pe() |
| 59 | { |
| 60 | SkPath path; |
| 61 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 62 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 63 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 64 | path.close(); |
| 65 | path.offset(SkIntToScalar(-6), 0); |
| 66 | |
| 67 | SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style); |
| 68 | SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS)); |
| 69 | |
| 70 | SkPathEffect* pe = new SkComposePathEffect(outer, inner); |
| 71 | outer->unref(); |
| 72 | inner->unref(); |
| 73 | return pe; |
| 74 | } |
| 75 | |
| 76 | /////////////////////////////////////////////////////////// |
| 77 | |
| 78 | #include "SkColorFilter.h" |
| 79 | #include "SkPorterDuff.h" |
| 80 | #include "SkLayerRasterizer.h" |
| 81 | |
| 82 | class testrast : public SkLayerRasterizer { |
| 83 | public: |
| 84 | testrast() |
| 85 | { |
| 86 | SkPaint paint; |
| 87 | paint.setAntiAlias(true); |
| 88 | |
| 89 | #if 0 |
| 90 | paint.setStyle(SkPaint::kStroke_Style); |
| 91 | paint.setStrokeWidth(SK_Scalar1*4); |
| 92 | this->addLayer(paint); |
| 93 | |
| 94 | paint.setStrokeWidth(SK_Scalar1*1); |
| 95 | paint.setPorterDuffXfermode(SkPorterDuff::kClear_Mode); |
| 96 | this->addLayer(paint); |
| 97 | #else |
| 98 | paint.setAlpha(0x66); |
| 99 | this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4)); |
| 100 | |
| 101 | paint.setAlpha(0xFF); |
| 102 | this->addLayer(paint); |
| 103 | #endif |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | class PathEffectView : public SkView { |
| 108 | SkPath fPath; |
| 109 | SkPoint fClickPt; |
| 110 | public: |
| 111 | PathEffectView() |
| 112 | { |
| 113 | SkRandom rand; |
| 114 | int steps = 20; |
| 115 | SkScalar dist = SkIntToScalar(500); |
| 116 | SkScalar x = SkIntToScalar(20); |
| 117 | SkScalar y = SkIntToScalar(50); |
| 118 | |
| 119 | fPath.moveTo(x, y); |
| 120 | for (int i = 0; i < steps; i++) |
| 121 | { |
| 122 | x += dist/steps; |
| 123 | fPath.lineTo(x, y + SkIntToScalar(rand.nextS() % 25)); |
| 124 | } |
| 125 | |
| 126 | fClickPt.set(SkIntToScalar(200), SkIntToScalar(200)); |
| 127 | } |
| 128 | |
| 129 | protected: |
| 130 | // overrides from SkEventSink |
| 131 | virtual bool onQuery(SkEvent* evt) |
| 132 | { |
| 133 | if (SampleCode::TitleQ(*evt)) |
| 134 | { |
| 135 | SampleCode::TitleR(evt, "PathEffects"); |
| 136 | return true; |
| 137 | } |
| 138 | return this->INHERITED::onQuery(evt); |
| 139 | } |
| 140 | |
| 141 | void drawBG(SkCanvas* canvas) |
| 142 | { |
| 143 | canvas->drawColor(0xFFDDDDDD); |
| 144 | |
| 145 | #if 0 |
| 146 | SkPath path; |
| 147 | test_grow(&path); |
| 148 | SkPaint p; |
| 149 | |
| 150 | p.setAntiAlias(true); |
| 151 | p.setStyle(SkPaint::kStroke_Style); |
| 152 | p.setStrokeWidth(SK_Scalar1); |
| 153 | canvas->drawPath(path, p); |
| 154 | path.close(); |
| 155 | #endif |
| 156 | } |
| 157 | |
| 158 | virtual void onDraw(SkCanvas* canvas) |
| 159 | { |
| 160 | this->drawBG(canvas); |
| 161 | |
| 162 | if (true) |
| 163 | { |
| 164 | canvas->drawColor(SK_ColorWHITE); |
| 165 | |
| 166 | SkPixelXorXfermode mode(SK_ColorWHITE); |
| 167 | SkPaint paint; |
| 168 | |
| 169 | paint.setColor(SK_ColorRED); |
| 170 | paint.setXfermode(&mode); |
| 171 | paint.setStrokeWidth(SkIntToScalar(8)); |
| 172 | |
| 173 | canvas->drawLine(SkIntToScalar(100), SkIntToScalar(100), |
| 174 | SkIntToScalar(200), SkIntToScalar(200), paint); |
| 175 | canvas->drawLine(SkIntToScalar(100), SkIntToScalar(200), |
| 176 | SkIntToScalar(200), SkIntToScalar(100), paint); |
| 177 | // return; |
| 178 | } |
| 179 | |
| 180 | if (false) |
| 181 | { |
| 182 | SkPath path; |
| 183 | SkPoint pts[] = { SkIntToScalar(100), SkIntToScalar(100), |
| 184 | SkIntToScalar(200), SkIntToScalar(100), |
| 185 | SkIntToScalar(100), SkIntToScalar(200) |
| 186 | }; |
| 187 | SkPaint paint; |
| 188 | |
| 189 | pts[2] = fClickPt; |
| 190 | |
| 191 | paint.setAntiAlias(true); |
| 192 | paint.setStyle(SkPaint::kStroke_Style); |
| 193 | paint.setStrokeWidth(SkIntToScalar(5)); |
| 194 | |
| 195 | path.moveTo(pts[0]); |
| 196 | path.arcTo(pts[1], pts[2], SkIntToScalar(50)); |
| 197 | canvas->drawPath(path, paint); |
| 198 | |
| 199 | paint.setStrokeWidth(0); |
| 200 | paint.setColor(SK_ColorRED); |
| 201 | canvas->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint); |
| 202 | canvas->drawLine(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY, paint); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | gPhase -= SK_Scalar1; |
| 207 | this->inval(nil); |
| 208 | |
| 209 | SkPaint paint; |
| 210 | |
| 211 | paint.setAntiAlias(true); |
| 212 | paint.setStyle(SkPaint::kStroke_Style); |
| 213 | paint.setStrokeWidth(SkIntToScalar(5)); |
| 214 | canvas->drawPath(fPath, paint); |
| 215 | paint.setStrokeWidth(0); |
| 216 | |
| 217 | paint.setColor(SK_ColorRED); |
| 218 | paint.setPathEffect(make_pe(1))->unref(); |
| 219 | canvas->drawPath(fPath, paint); |
| 220 | |
| 221 | canvas->translate(0, SkIntToScalar(50)); |
| 222 | |
| 223 | paint.setColor(SK_ColorBLUE); |
| 224 | paint.setPathEffect(make_pe(2))->unref(); |
| 225 | canvas->drawPath(fPath, paint); |
| 226 | |
| 227 | canvas->translate(0, SkIntToScalar(50)); |
| 228 | |
| 229 | paint.setARGB(0xFF, 0, 0xBB, 0); |
| 230 | paint.setPathEffect(make_pe(3))->unref(); |
| 231 | canvas->drawPath(fPath, paint); |
| 232 | |
| 233 | canvas->translate(0, SkIntToScalar(50)); |
| 234 | |
| 235 | paint.setARGB(0xFF, 0, 0, 0); |
| 236 | paint.setPathEffect(make_warp_pe())->unref(); |
| 237 | paint.setRasterizer(new testrast)->unref(); |
| 238 | canvas->drawPath(fPath, paint); |
| 239 | |
| 240 | { |
| 241 | SkRect oval; |
| 242 | |
| 243 | oval.set(SkIntToScalar(50), SkIntToScalar(100), |
| 244 | SkIntToScalar(150), SkIntToScalar(150)); |
| 245 | canvas->drawRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8), paint); |
| 246 | } |
| 247 | |
| 248 | { |
| 249 | SkRect bounds; |
| 250 | SkPaint paint; |
| 251 | |
| 252 | paint.setAntiAlias(true); |
| 253 | paint.setAlpha(0x80); |
| 254 | paint.setColorFilter( |
| 255 | SkColorFilter::CreatePorterDuffFilter( |
| 256 | SkColorSetARGB(0x44, 0, 0xFF, 0), SkPorterDuff::kSrcATop_Mode))->unref(); |
| 257 | |
| 258 | bounds.set(SkIntToScalar(10), SkIntToScalar(10), SkIntToScalar(150), SkIntToScalar(70)); |
| 259 | canvas->saveLayer(&bounds, &paint, |
| 260 | (SkCanvas::SaveFlags)(SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag)); |
| 261 | |
| 262 | paint.setColorFilter(NULL); |
| 263 | paint.setColor(SK_ColorRED); |
| 264 | canvas->drawOval(bounds, paint); |
| 265 | |
| 266 | paint.setColor(SK_ColorBLUE); |
| 267 | paint.setAlpha(0x80); |
| 268 | bounds.inset(SkIntToScalar(10), SkIntToScalar(10)); |
| 269 | canvas->drawOval(bounds, paint); |
| 270 | |
| 271 | canvas->restore(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private: |
| 276 | typedef SkView INHERITED; |
| 277 | }; |
| 278 | |
| 279 | ////////////////////////////////////////////////////////////////////////////// |
| 280 | |
| 281 | static SkView* MyFactory() { return new PathEffectView; } |
| 282 | static SkViewRegister reg(MyFactory); |
| 283 | |