reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkCanvas.h" |
| 3 | #include "SkView.h" |
| 4 | #include "Sk1DPathEffect.h" |
| 5 | #include "Sk2DPathEffect.h" |
| 6 | #include "SkAvoidXfermode.h" |
| 7 | #include "SkBlurMaskFilter.h" |
| 8 | #include "SkColorFilter.h" |
| 9 | #include "SkColorPriv.h" |
| 10 | #include "SkCornerPathEffect.h" |
| 11 | #include "SkDashPathEffect.h" |
| 12 | #include "SkDiscretePathEffect.h" |
| 13 | #include "SkEmbossMaskFilter.h" |
| 14 | #include "SkGradientShader.h" |
| 15 | #include "SkImageDecoder.h" |
| 16 | #include "SkLayerRasterizer.h" |
| 17 | #include "SkMath.h" |
| 18 | #include "SkPath.h" |
| 19 | #include "SkRegion.h" |
| 20 | #include "SkShader.h" |
reed@android.com | aa5a7db | 2009-05-27 01:20:10 +0000 | [diff] [blame] | 21 | #include "SkComposeShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkCornerPathEffect.h" |
| 23 | #include "SkPathMeasure.h" |
| 24 | #include "SkPicture.h" |
| 25 | #include "SkRandom.h" |
| 26 | #include "SkTransparentShader.h" |
| 27 | #include "SkTypeface.h" |
| 28 | #include "SkUnitMappers.h" |
| 29 | #include "SkUtils.h" |
| 30 | #include "SkXfermode.h" |
| 31 | |
| 32 | #include <math.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | |
| 34 | static inline SkPMColor rgb2gray(SkPMColor c) |
| 35 | { |
| 36 | unsigned r = SkGetPackedR32(c); |
| 37 | unsigned g = SkGetPackedG32(c); |
| 38 | unsigned b = SkGetPackedB32(c); |
| 39 | |
| 40 | unsigned x = r * 5 + g * 7 + b * 4 >> 4; |
| 41 | |
| 42 | return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT)); |
| 43 | } |
| 44 | |
| 45 | class SkGrayScaleColorFilter : public SkColorFilter { |
| 46 | public: |
| 47 | virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) |
| 48 | { |
| 49 | for (int i = 0; i < count; i++) |
| 50 | result[i] = rgb2gray(src[i]); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | class SkChannelMaskColorFilter : public SkColorFilter { |
| 55 | public: |
| 56 | SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) |
| 57 | { |
| 58 | fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask); |
| 59 | } |
| 60 | |
| 61 | virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) |
| 62 | { |
| 63 | SkPMColor mask = fMask; |
| 64 | for (int i = 0; i < count; i++) |
| 65 | result[i] = src[i] & mask; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | SkPMColor fMask; |
| 70 | }; |
| 71 | |
| 72 | /////////////////////////////////////////////////////////// |
| 73 | |
| 74 | static void r0(SkLayerRasterizer* rast, SkPaint& p) |
| 75 | { |
| 76 | p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3), |
| 77 | SkBlurMaskFilter::kNormal_BlurStyle))->unref(); |
| 78 | rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
| 79 | |
| 80 | p.setMaskFilter(NULL); |
| 81 | p.setStyle(SkPaint::kStroke_Style); |
| 82 | p.setStrokeWidth(SK_Scalar1); |
| 83 | rast->addLayer(p); |
| 84 | |
| 85 | p.setAlpha(0x11); |
| 86 | p.setStyle(SkPaint::kFill_Style); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 87 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | rast->addLayer(p); |
| 89 | } |
| 90 | |
| 91 | static void r1(SkLayerRasterizer* rast, SkPaint& p) |
| 92 | { |
| 93 | rast->addLayer(p); |
| 94 | |
| 95 | p.setAlpha(0x40); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 96 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | p.setStyle(SkPaint::kStroke_Style); |
| 98 | p.setStrokeWidth(SK_Scalar1*2); |
| 99 | rast->addLayer(p); |
| 100 | } |
| 101 | |
| 102 | static void r2(SkLayerRasterizer* rast, SkPaint& p) |
| 103 | { |
| 104 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 105 | p.setStrokeWidth(SK_Scalar1*4); |
| 106 | rast->addLayer(p); |
| 107 | |
| 108 | p.setStyle(SkPaint::kStroke_Style); |
| 109 | p.setStrokeWidth(SK_Scalar1*3/2); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 110 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | rast->addLayer(p); |
| 112 | } |
| 113 | |
| 114 | static void r3(SkLayerRasterizer* rast, SkPaint& p) |
| 115 | { |
| 116 | p.setStyle(SkPaint::kStroke_Style); |
| 117 | p.setStrokeWidth(SK_Scalar1*3); |
| 118 | rast->addLayer(p); |
| 119 | |
| 120 | p.setAlpha(0x20); |
| 121 | p.setStyle(SkPaint::kFill_Style); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 122 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | rast->addLayer(p); |
| 124 | } |
| 125 | |
| 126 | static void r4(SkLayerRasterizer* rast, SkPaint& p) |
| 127 | { |
| 128 | p.setAlpha(0x60); |
| 129 | rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
| 130 | |
| 131 | p.setAlpha(0xFF); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 132 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2); |
| 134 | |
| 135 | p.setXfermode(NULL); |
| 136 | rast->addLayer(p); |
| 137 | } |
| 138 | |
| 139 | static void r5(SkLayerRasterizer* rast, SkPaint& p) |
| 140 | { |
| 141 | rast->addLayer(p); |
| 142 | |
| 143 | p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 144 | p.setXfermodeMode(SkXfermode::kSrcOut_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | rast->addLayer(p); |
| 146 | } |
| 147 | |
| 148 | static void r6(SkLayerRasterizer* rast, SkPaint& p) |
| 149 | { |
| 150 | rast->addLayer(p); |
| 151 | |
| 152 | p.setAntiAlias(false); |
| 153 | SkLayerRasterizer* rast2 = new SkLayerRasterizer; |
| 154 | r5(rast2, p); |
| 155 | p.setRasterizer(rast2)->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 156 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | rast->addLayer(p); |
| 158 | } |
| 159 | |
| 160 | class Dot2DPathEffect : public Sk2DPathEffect { |
| 161 | public: |
| 162 | Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix) |
| 163 | : Sk2DPathEffect(matrix), fRadius(radius) {} |
| 164 | |
| 165 | virtual void flatten(SkFlattenableWriteBuffer& buffer) |
| 166 | { |
| 167 | this->INHERITED::flatten(buffer); |
| 168 | |
| 169 | buffer.writeScalar(fRadius); |
| 170 | } |
| 171 | virtual Factory getFactory() { return CreateProc; } |
| 172 | |
| 173 | protected: |
| 174 | virtual void next(const SkPoint& loc, int u, int v, SkPath* dst) |
| 175 | { |
| 176 | dst->addCircle(loc.fX, loc.fY, fRadius); |
| 177 | } |
| 178 | |
| 179 | Dot2DPathEffect(SkFlattenableReadBuffer& buffer) : Sk2DPathEffect(buffer) |
| 180 | { |
| 181 | fRadius = buffer.readScalar(); |
| 182 | } |
| 183 | private: |
| 184 | SkScalar fRadius; |
| 185 | |
| 186 | static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) |
| 187 | { |
| 188 | return new Dot2DPathEffect(buffer); |
| 189 | } |
| 190 | |
| 191 | typedef Sk2DPathEffect INHERITED; |
| 192 | }; |
| 193 | |
| 194 | static void r7(SkLayerRasterizer* rast, SkPaint& p) |
| 195 | { |
| 196 | SkMatrix lattice; |
| 197 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 198 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
| 199 | p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*4, lattice))->unref(); |
| 200 | rast->addLayer(p); |
| 201 | } |
| 202 | |
| 203 | static void r8(SkLayerRasterizer* rast, SkPaint& p) |
| 204 | { |
| 205 | rast->addLayer(p); |
| 206 | |
| 207 | SkMatrix lattice; |
| 208 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 209 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
| 210 | p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*2, lattice))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 211 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | rast->addLayer(p); |
| 213 | |
| 214 | p.setPathEffect(NULL); |
| 215 | p.setXfermode(NULL); |
| 216 | p.setStyle(SkPaint::kStroke_Style); |
| 217 | p.setStrokeWidth(SK_Scalar1); |
| 218 | rast->addLayer(p); |
| 219 | } |
| 220 | |
| 221 | class Line2DPathEffect : public Sk2DPathEffect { |
| 222 | public: |
| 223 | Line2DPathEffect(SkScalar width, const SkMatrix& matrix) |
| 224 | : Sk2DPathEffect(matrix), fWidth(width) {} |
| 225 | |
| 226 | virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width) |
| 227 | { |
| 228 | if (this->INHERITED::filterPath(dst, src, width)) |
| 229 | { |
| 230 | *width = fWidth; |
| 231 | return true; |
| 232 | } |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | virtual Factory getFactory() { return CreateProc; } |
| 237 | virtual void flatten(SkFlattenableWriteBuffer& buffer) |
| 238 | { |
| 239 | this->INHERITED::flatten(buffer); |
| 240 | buffer.writeScalar(fWidth); |
| 241 | } |
| 242 | protected: |
| 243 | virtual void nextSpan(int u, int v, int ucount, SkPath* dst) |
| 244 | { |
| 245 | if (ucount > 1) |
| 246 | { |
| 247 | SkPoint src[2], dstP[2]; |
| 248 | |
| 249 | src[0].set(SkIntToScalar(u) + SK_ScalarHalf, |
| 250 | SkIntToScalar(v) + SK_ScalarHalf); |
| 251 | src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf, |
| 252 | SkIntToScalar(v) + SK_ScalarHalf); |
| 253 | this->getMatrix().mapPoints(dstP, src, 2); |
| 254 | |
| 255 | dst->moveTo(dstP[0]); |
| 256 | dst->lineTo(dstP[1]); |
| 257 | } |
| 258 | } |
| 259 | |
deanm@chromium.org | 1220e1c | 2009-06-11 12:26:47 +0000 | [diff] [blame] | 260 | Line2DPathEffect(SkFlattenableReadBuffer& buffer) : Sk2DPathEffect(buffer) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 261 | { |
| 262 | fWidth = buffer.readScalar(); |
| 263 | } |
| 264 | |
| 265 | private: |
| 266 | SkScalar fWidth; |
| 267 | |
| 268 | static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { return new Line2DPathEffect(buffer); } |
| 269 | |
| 270 | typedef Sk2DPathEffect INHERITED; |
| 271 | }; |
| 272 | |
| 273 | static void r9(SkLayerRasterizer* rast, SkPaint& p) |
| 274 | { |
| 275 | rast->addLayer(p); |
| 276 | |
| 277 | SkMatrix lattice; |
| 278 | lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0); |
| 279 | lattice.postRotate(SkIntToScalar(30), 0, 0); |
| 280 | p.setPathEffect(new Line2DPathEffect(SK_Scalar1*2, lattice))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 281 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 282 | rast->addLayer(p); |
| 283 | |
| 284 | p.setPathEffect(NULL); |
| 285 | p.setXfermode(NULL); |
| 286 | p.setStyle(SkPaint::kStroke_Style); |
| 287 | p.setStrokeWidth(SK_Scalar1); |
| 288 | rast->addLayer(p); |
| 289 | } |
| 290 | |
| 291 | typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&); |
| 292 | |
| 293 | static const raster_proc gRastProcs[] = { |
| 294 | r0, r1, r2, r3, r4, r5, r6, r7, r8, r9 |
| 295 | }; |
| 296 | |
| 297 | static const struct { |
| 298 | SkColor fMul, fAdd; |
| 299 | } gLightingColors[] = { |
| 300 | { 0x808080, 0x800000 }, // general case |
| 301 | { 0x707070, 0x707070 }, // no-pin case |
| 302 | { 0xFFFFFF, 0x800000 }, // just-add case |
| 303 | { 0x808080, 0x000000 }, // just-mul case |
| 304 | { 0xFFFFFF, 0x000000 } // identity case |
| 305 | }; |
| 306 | |
| 307 | static unsigned color_dist16(uint16_t a, uint16_t b) |
| 308 | { |
| 309 | unsigned dr = SkAbs32(SkPacked16ToR32(a) - SkPacked16ToR32(b)); |
| 310 | unsigned dg = SkAbs32(SkPacked16ToG32(a) - SkPacked16ToG32(b)); |
| 311 | unsigned db = SkAbs32(SkPacked16ToB32(a) - SkPacked16ToB32(b)); |
| 312 | |
| 313 | return SkMax32(dr, SkMax32(dg, db)); |
| 314 | } |
| 315 | |
| 316 | static unsigned scale_dist(unsigned dist, unsigned scale) |
| 317 | { |
| 318 | dist >>= 6; |
| 319 | dist = (dist << 2) | dist; |
| 320 | dist = (dist << 4) | dist; |
| 321 | return dist; |
| 322 | |
| 323 | // return SkAlphaMul(dist, scale); |
| 324 | } |
| 325 | |
| 326 | static void apply_shader(SkPaint* paint, int index) |
| 327 | { |
| 328 | raster_proc proc = gRastProcs[index]; |
| 329 | if (proc) |
| 330 | { |
| 331 | SkPaint p; |
| 332 | SkLayerRasterizer* rast = new SkLayerRasterizer; |
| 333 | |
| 334 | p.setAntiAlias(true); |
| 335 | proc(rast, p); |
| 336 | paint->setRasterizer(rast)->unref(); |
| 337 | } |
| 338 | |
| 339 | #if 1 |
| 340 | SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 }; |
| 341 | paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref(); |
| 342 | paint->setColor(SK_ColorBLUE); |
| 343 | #endif |
| 344 | } |
| 345 | |
| 346 | static void test_math() |
| 347 | { |
| 348 | float x; |
| 349 | const float PI = 3.141593; |
| 350 | |
| 351 | for (x = 0; x < 1; x += 0.05f) |
| 352 | printf("atan(%g) = %g\n", x, atanf(x) * 180/PI); |
| 353 | for (x = 1; x < 10000000; x *= 2) |
| 354 | printf("atan(%g) = %g\n", x, atanf(x) * 180/PI); |
| 355 | } |
| 356 | |
| 357 | class DemoView : public SkView { |
| 358 | public: |
| 359 | DemoView() |
| 360 | { |
| 361 | test_math(); |
| 362 | } |
| 363 | |
| 364 | protected: |
| 365 | // overrides from SkEventSink |
| 366 | virtual bool onQuery(SkEvent* evt) |
| 367 | { |
| 368 | if (SampleCode::TitleQ(*evt)) |
| 369 | { |
| 370 | SampleCode::TitleR(evt, "Demo"); |
| 371 | return true; |
| 372 | } |
| 373 | return this->INHERITED::onQuery(evt); |
| 374 | } |
| 375 | |
| 376 | virtual bool onClick(Click* click) |
| 377 | { |
| 378 | return this->INHERITED::onClick(click); |
| 379 | } |
| 380 | |
| 381 | void makePath(SkPath& path) |
| 382 | { |
| 383 | path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20), |
| 384 | SkPath::kCCW_Direction); |
| 385 | for (int index = 0; index < 10; index++) { |
| 386 | SkScalar x = SkFloatToScalar(cos(index / 10.0f * 2 * 3.1415925358f)); |
| 387 | SkScalar y = SkFloatToScalar(sin(index / 10.0f * 2 * 3.1415925358f)); |
| 388 | x *= index & 1 ? 7 : 14; |
| 389 | y *= index & 1 ? 7 : 14; |
| 390 | x += SkIntToScalar(20); |
| 391 | y += SkIntToScalar(20); |
| 392 | if (index == 0) |
| 393 | path.moveTo(x, y); |
| 394 | else |
| 395 | path.lineTo(x, y); |
| 396 | } |
| 397 | path.close(); |
| 398 | } |
| 399 | |
| 400 | virtual void onDraw(SkCanvas* canvas) |
| 401 | { |
| 402 | canvas->drawColor(SK_ColorWHITE); |
| 403 | canvas->save(); |
| 404 | drawPicture(canvas, 0); |
| 405 | canvas->restore(); |
| 406 | |
| 407 | { |
| 408 | SkPicture picture; |
| 409 | SkCanvas* record = picture.beginRecording(320, 480); |
| 410 | drawPicture(record, 120); |
| 411 | canvas->translate(0, SkIntToScalar(120)); |
| 412 | |
| 413 | SkRect clip; |
| 414 | clip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160)); |
| 415 | do { |
| 416 | canvas->save(); |
| 417 | canvas->clipRect(clip); |
| 418 | picture.draw(canvas); |
| 419 | canvas->restore(); |
| 420 | if (clip.fRight < SkIntToScalar(320)) |
| 421 | clip.offset(SkIntToScalar(160), 0); |
| 422 | else if (clip.fBottom < SkIntToScalar(480)) |
| 423 | clip.offset(-SkIntToScalar(320), SkIntToScalar(160)); |
| 424 | else |
| 425 | break; |
| 426 | } while (true); |
| 427 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void drawPicture(SkCanvas* canvas, int spriteOffset) |
| 431 | { |
| 432 | SkMatrix matrix; matrix.reset(); |
| 433 | SkPaint paint; |
| 434 | SkPath path; |
| 435 | SkPoint start = {0, 0}; |
| 436 | SkPoint stop = { SkIntToScalar(40), SkIntToScalar(40) }; |
| 437 | SkRect rect = {0, 0, SkIntToScalar(40), SkIntToScalar(40) }; |
| 438 | SkRect rect2 = {0, 0, SkIntToScalar(65), SkIntToScalar(20) }; |
| 439 | SkScalar left = 0, top = 0, x = 0, y = 0; |
| 440 | int index; |
| 441 | |
| 442 | char ascii[] = "ascii..."; |
| 443 | size_t asciiLength = sizeof(ascii) - 1; |
| 444 | char utf8[] = "utf8" "\xe2\x80\xa6"; |
| 445 | short utf16[] = {'u', 't', 'f', '1', '6', 0x2026 }; |
| 446 | short utf16simple[] = {'u', 't', 'f', '1', '6', '!' }; |
| 447 | |
| 448 | makePath(path); |
| 449 | SkTDArray<SkPoint>(pos); |
| 450 | pos.setCount(asciiLength); |
| 451 | for (index = 0; index < asciiLength; index++) |
| 452 | pos[index].set(SkIntToScalar(index * 10), SkIntToScalar(index * 2)); |
| 453 | SkTDArray<SkPoint>(pos2); |
| 454 | pos2.setCount(asciiLength); |
| 455 | for (index = 0; index < asciiLength; index++) |
| 456 | pos2[index].set(SkIntToScalar(index * 10), SkIntToScalar(20)); |
| 457 | |
| 458 | // shaders |
| 459 | SkPoint linearPoints[] = { 0, 0, SkIntToScalar(40), SkIntToScalar(40) }; |
| 460 | SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE }; |
| 461 | SkScalar* linearPos = NULL; |
| 462 | int linearCount = 2; |
| 463 | SkShader::TileMode linearMode = SkShader::kMirror_TileMode; |
| 464 | SkUnitMapper* linearMapper = new SkDiscreteMapper(3); |
| 465 | SkAutoUnref unmapLinearMapper(linearMapper); |
| 466 | SkShader* linear = SkGradientShader::CreateLinear(linearPoints, |
| 467 | linearColors, linearPos, linearCount, linearMode, linearMapper); |
| 468 | |
| 469 | SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) }; |
| 470 | SkScalar radialRadius = SkIntToScalar(25); |
| 471 | SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED }; |
| 472 | SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)}; |
| 473 | int radialCount = 3; |
| 474 | SkShader::TileMode radialMode = SkShader::kRepeat_TileMode; |
| 475 | SkUnitMapper* radialMapper = new SkCosineMapper(); |
| 476 | SkAutoUnref unmapRadialMapper(radialMapper); |
| 477 | SkShader* radial = SkGradientShader::CreateRadial(radialCenter, |
| 478 | radialRadius, radialColors, radialPos, radialCount, |
| 479 | radialMode, radialMapper); |
| 480 | |
| 481 | SkTransparentShader* transparentShader = new SkTransparentShader(); |
| 482 | SkEmbossMaskFilter::Light light; |
| 483 | light.fDirection[0] = SK_Scalar1/2; |
| 484 | light.fDirection[1] = SK_Scalar1/2; |
| 485 | light.fDirection[2] = SK_Scalar1/3; |
| 486 | light.fAmbient = 0x48; |
| 487 | light.fSpecular = 0x80; |
| 488 | SkScalar radius = SkIntToScalar(12)/5; |
| 489 | SkEmbossMaskFilter* embossFilter = new SkEmbossMaskFilter(light, |
| 490 | radius); |
| 491 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 492 | SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 493 | SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter( |
| 494 | 0xff89bc45, 0xff112233); |
| 495 | |
| 496 | canvas->save(); |
| 497 | canvas->translate(SkIntToScalar(0), SkIntToScalar(5)); |
| 498 | paint.setFlags(SkPaint::kAntiAlias_Flag | SkPaint::kFilterBitmap_Flag); |
| 499 | // !!! draw through a clip |
| 500 | paint.setColor(SK_ColorLTGRAY); |
| 501 | paint.setStyle(SkPaint::kFill_Style); |
| 502 | SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)}; |
| 503 | canvas->clipRect(clip); |
| 504 | paint.setShader(SkShader::CreateBitmapShader(fTx, |
| 505 | SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode))->unref(); |
| 506 | canvas->drawPaint(paint); |
| 507 | canvas->save(); |
| 508 | |
| 509 | // line (exercises xfermode, colorShader, colorFilter, filterShader) |
| 510 | paint.setColor(SK_ColorGREEN); |
| 511 | paint.setStrokeWidth(SkIntToScalar(10)); |
| 512 | paint.setStyle(SkPaint::kStroke_Style); |
| 513 | paint.setXfermode(xfermode)->unref(); |
| 514 | paint.setColorFilter(lightingFilter)->unref(); |
| 515 | canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green |
| 516 | paint.setXfermode(NULL); |
| 517 | paint.setColorFilter(NULL); |
| 518 | |
| 519 | // rectangle |
| 520 | paint.setStyle(SkPaint::kFill_Style); |
| 521 | canvas->translate(SkIntToScalar(50), 0); |
| 522 | paint.setColor(SK_ColorYELLOW); |
| 523 | paint.setShader(linear)->unref(); |
| 524 | paint.setPathEffect(pathEffectTest())->unref(); |
| 525 | canvas->drawRect(rect, paint); |
| 526 | paint.setPathEffect(NULL); |
| 527 | |
| 528 | // circle w/ emboss & transparent (exercises 3dshader) |
| 529 | canvas->translate(SkIntToScalar(50), 0); |
| 530 | paint.setMaskFilter(embossFilter)->unref(); |
| 531 | canvas->drawOval(rect, paint); |
| 532 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 533 | paint.setShader(transparentShader)->unref(); |
| 534 | canvas->drawOval(rect, paint); |
| 535 | canvas->translate(0, SkIntToScalar(-10)); |
| 536 | |
| 537 | // path |
| 538 | canvas->translate(SkIntToScalar(50), 0); |
| 539 | paint.setColor(SK_ColorRED); |
| 540 | paint.setStyle(SkPaint::kStroke_Style); |
| 541 | paint.setStrokeWidth(SkIntToScalar(5)); |
| 542 | paint.setShader(radial)->unref(); |
| 543 | paint.setMaskFilter(NULL); |
| 544 | canvas->drawPath(path, paint); |
| 545 | |
| 546 | paint.setShader(NULL); |
| 547 | // bitmap, sprite |
| 548 | canvas->translate(SkIntToScalar(50), 0); |
| 549 | paint.setStyle(SkPaint::kFill_Style); |
| 550 | canvas->drawBitmap(fBug, left, top, &paint); |
| 551 | canvas->translate(SkIntToScalar(30), 0); |
| 552 | canvas->drawSprite(fTb, |
| 553 | SkScalarRound(canvas->getTotalMatrix().getTranslateX()), |
| 554 | spriteOffset + 10, &paint); |
| 555 | |
| 556 | canvas->translate(-SkIntToScalar(30), SkIntToScalar(30)); |
| 557 | paint.setShader(shaderTest())->unref(); // test compose shader |
| 558 | canvas->drawRect(rect2, paint); |
| 559 | paint.setShader(NULL); |
| 560 | |
| 561 | canvas->restore(); |
| 562 | // text |
| 563 | canvas->translate(0, SkIntToScalar(60)); |
| 564 | canvas->save(); |
| 565 | paint.setColor(SK_ColorGRAY); |
| 566 | canvas->drawPosText(ascii, asciiLength, pos.begin(), paint); |
| 567 | canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint); |
| 568 | |
| 569 | canvas->translate(SkIntToScalar(50), 0); |
| 570 | paint.setColor(SK_ColorCYAN); |
| 571 | canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint); |
| 572 | |
| 573 | canvas->translate(SkIntToScalar(30), 0); |
| 574 | paint.setColor(SK_ColorMAGENTA); |
| 575 | paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
| 576 | matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10)); |
| 577 | canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint); |
| 578 | canvas->translate(0, SkIntToScalar(20)); |
| 579 | canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint); |
| 580 | canvas->restore(); |
| 581 | |
| 582 | canvas->translate(0, SkIntToScalar(60)); |
| 583 | paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 584 | canvas->restore(); |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | ./SkColorFilter.h:25:class SkColorFilter : public SkFlattenable { -- abstract |
| 589 | static SkColorFilter* CreatXfermodeFilter() *** untested *** |
| 590 | static SkColorFilter* CreatePorterDuffFilter() *** untested *** |
| 591 | static SkColorFilter* CreateLightingFilter() -- tested |
| 592 | ./SkDrawLooper.h:9:class SkDrawLooper : public SkFlattenable { -- virtually abstract |
| 593 | ./SkBlurDrawLooper.h:9:class SkBlurDrawLooper : public SkDrawLooper { *** untested *** |
| 594 | ./SkMaskFilter.h:41:class SkMaskFilter : public SkFlattenable { -- abstract chmod +w .h |
| 595 | ./SkEmbossMaskFilter.h:27:class SkEmbossMaskFilter : public SkMaskFilter { -- tested |
| 596 | ./SkPathEffect.h:33:class SkPathEffect : public SkFlattenable { -- abstract |
| 597 | ./Sk1DPathEffect.h:27:class Sk1DPathEffect : public SkPathEffect { -- abstract |
| 598 | ./Sk1DPathEffect.h:48:class SkPath1DPathEffect : public Sk1DPathEffect { -- tested |
| 599 | ./Sk2DPathEffect.h:25:class Sk2DPathEffect : public SkPathEffect { *** untested *** |
| 600 | ./SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect { *** untested *** |
| 601 | ./SkDashPathEffect.h:27:class SkDashPathEffect : public SkPathEffect { |
| 602 | ./SkDiscretePathEffect.h:27:class SkDiscretePathEffect : public SkPathEffect { |
| 603 | ./SkPaint.h:760:class SkStrokePathEffect : public SkPathEffect { |
| 604 | ./SkPathEffect.h:58:class SkPairPathEffect : public SkPathEffect { |
| 605 | ./SkPathEffect.h:78:class SkComposePathEffect : public SkPairPathEffect { |
| 606 | ./SkPathEffect.h:114:class SkSumPathEffect : public SkPairPathEffect { |
| 607 | ./SkRasterizer.h:29:class SkRasterizer : public SkFlattenable { |
| 608 | ./SkLayerRasterizer.h:27:class SkLayerRasterizer : public SkRasterizer { |
| 609 | ./SkShader.h:36:class SkShader : public SkFlattenable { |
| 610 | ./SkColorFilter.h:59:class SkFilterShader : public SkShader { |
| 611 | ./SkColorShader.h:26:class SkColorShader : public SkShader { |
| 612 | ./SkShaderExtras.h:31:class SkComposeShader : public SkShader { |
| 613 | ./SkTransparentShader.h:23:class SkTransparentShader : public SkShader { |
| 614 | ./SkUnitMapper.h:24:class SkUnitMapper : public SkFlattenable { |
| 615 | ./SkUnitMapper.h:33:class SkDiscreteMapper : public SkUnitMapper { |
| 616 | ./SkUnitMapper.h:51:class SkFlipCosineMapper : public SkUnitMapper { |
| 617 | ./SkXfermode.h:32:class SkXfermode : public SkFlattenable { |
| 618 | ./SkAvoidXfermode.h:28:class SkAvoidXfermode : public SkXfermode { *** not done *** chmod +w .h .cpp |
| 619 | ./SkXfermode.h:54:class SkProcXfermode : public SkXfermode { |
| 620 | */ |
| 621 | |
| 622 | /* |
| 623 | ./SkBlurMaskFilter.h:25:class SkBlurMaskFilter { |
| 624 | chmod +w SkBlurMaskFilter.cpp |
| 625 | ./SkGradientShader.h:30:class SkGradientShader { |
| 626 | */ |
| 627 | // save layer, bounder, looper |
| 628 | // matrix |
| 629 | // clip /path/region |
| 630 | // bitmap proc shader ? |
| 631 | |
| 632 | /* untested: |
| 633 | SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect { |
| 634 | */ |
| 635 | |
| 636 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) |
| 637 | { |
| 638 | fClickPt.set(x, y); |
| 639 | this->inval(NULL); |
| 640 | return this->INHERITED::onFindClickHandler(x, y); |
| 641 | } |
| 642 | |
| 643 | SkPathEffect* pathEffectTest() |
| 644 | { |
| 645 | static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 }; |
| 646 | SkScalar gPhase = 0; |
| 647 | SkPath path; |
| 648 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 649 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 650 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 651 | path.close(); |
| 652 | path.offset(SkIntToScalar(-6), 0); |
| 653 | SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), |
| 654 | gPhase, SkPath1DPathEffect::kRotate_Style); |
| 655 | SkPathEffect* inner = new SkDiscretePathEffect(SkIntToScalar(2), |
| 656 | SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2)); |
| 657 | SkPathEffect* result = new SkComposePathEffect(outer, inner); |
| 658 | outer->unref(); |
| 659 | inner->unref(); |
| 660 | return result; |
| 661 | } |
| 662 | |
| 663 | SkPathEffect* pathEffectTest2() // unsure this works (has no visible effect) |
| 664 | { |
| 665 | SkPathEffect* outer = new SkStrokePathEffect(SkIntToScalar(4), |
| 666 | SkPaint::kStroke_Style, SkPaint::kMiter_Join, SkPaint::kButt_Cap); |
| 667 | static const SkScalar intervals[] = {SkIntToScalar(1), SkIntToScalar(2), |
| 668 | SkIntToScalar(2), SkIntToScalar(1)}; |
| 669 | SkPathEffect* inner = new SkDashPathEffect(intervals, |
| 670 | sizeof(intervals) / sizeof(intervals[0]), 0); |
| 671 | SkPathEffect* result = new SkSumPathEffect(outer, inner); |
| 672 | outer->unref(); |
| 673 | inner->unref(); |
| 674 | return result; |
| 675 | } |
| 676 | |
| 677 | SkShader* shaderTest() |
| 678 | { |
| 679 | SkPoint pts[] = {0, 0, SkIntToScalar(100), 0 }; |
| 680 | SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; |
| 681 | SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, |
| 682 | 2, SkShader::kClamp_TileMode); |
| 683 | pts[1].set(0, SkIntToScalar(100)); |
| 684 | SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)}; |
| 685 | SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors2, NULL, |
| 686 | 2, SkShader::kClamp_TileMode); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 687 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 688 | SkShader* result = new SkComposeShader(shaderA, shaderB, mode); |
| 689 | shaderA->unref(); |
| 690 | shaderB->unref(); |
| 691 | mode->unref(); |
| 692 | return result; |
| 693 | } |
| 694 | |
| 695 | virtual void startTest() { |
| 696 | SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/bugcirc.gif", &fBug); |
| 697 | SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/tbcirc.gif", &fTb); |
| 698 | SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/05psp04.gif", &fTx); |
| 699 | } |
| 700 | |
| 701 | void drawRaster(SkCanvas* canvas) |
| 702 | { |
| 703 | for (int index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++) |
| 704 | drawOneRaster(canvas); |
| 705 | } |
| 706 | |
| 707 | void drawOneRaster(SkCanvas* canvas) |
| 708 | { |
| 709 | canvas->save(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 710 | |
| 711 | SkScalar x = SkIntToScalar(20); |
| 712 | SkScalar y = SkIntToScalar(40); |
| 713 | SkPaint paint; |
| 714 | |
| 715 | paint.setAntiAlias(true); |
| 716 | paint.setTextSize(SkIntToScalar(48)); |
reed@android.com | aa5a7db | 2009-05-27 01:20:10 +0000 | [diff] [blame] | 717 | paint.setTypeface(SkTypeface::CreateFromName("sans-serif", |
| 718 | SkTypeface::kBold)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 719 | |
| 720 | SkString str("GOOGLE"); |
| 721 | |
| 722 | for (int i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) |
| 723 | { |
| 724 | apply_shader(&paint, i); |
| 725 | |
| 726 | // paint.setMaskFilter(NULL); |
| 727 | // paint.setColor(SK_ColorBLACK); |
| 728 | |
| 729 | #if 01 |
| 730 | int index = i % SK_ARRAY_COUNT(gLightingColors); |
| 731 | paint.setColorFilter(SkColorFilter::CreateLightingFilter( |
| 732 | gLightingColors[index].fMul, |
| 733 | gLightingColors[index].fAdd))->unref(); |
| 734 | #endif |
| 735 | |
| 736 | canvas->drawText(str.c_str(), str.size(), x, y, paint); |
| 737 | SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y }; |
| 738 | paint.setStyle(SkPaint::kStroke_Style); |
| 739 | canvas->drawOval(oval, paint); |
| 740 | paint.setStyle(SkPaint::kFill_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 741 | |
| 742 | y += paint.getFontSpacing(); |
| 743 | } |
| 744 | |
| 745 | canvas->restore(); |
| 746 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 747 | if (1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 748 | SkAvoidXfermode mode(SK_ColorWHITE, 0xFF, |
| 749 | SkAvoidXfermode::kTargetColor_Mode); |
| 750 | SkPaint paint; |
| 751 | x += SkIntToScalar(20); |
| 752 | SkRect r = { x, 0, x + SkIntToScalar(360), SkIntToScalar(700) }; |
| 753 | paint.setXfermode(&mode); |
| 754 | paint.setColor(SK_ColorGREEN); |
| 755 | paint.setAntiAlias(true); |
| 756 | canvas->drawOval(r, paint); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | private: |
| 761 | SkPoint fClickPt; |
| 762 | SkBitmap fBug, fTb, fTx; |
| 763 | typedef SkView INHERITED; |
| 764 | }; |
| 765 | |
| 766 | ////////////////////////////////////////////////////////////////////////////// |
| 767 | |
| 768 | static SkView* MyFactory() { return new DemoView; } |
| 769 | static SkViewRegister reg(MyFactory); |
| 770 | |