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