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 "SkColorFilter.h" |
| 15 | #include "SkDither.h" |
| 16 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame^] | 17 | static void make_bm(SkBitmap* bm) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | const SkColor colors[] = { |
| 19 | SK_ColorRED, SK_ColorGREEN, |
| 20 | SK_ColorBLUE, SK_ColorWHITE |
| 21 | }; |
| 22 | SkColorTable* ctable = new SkColorTable(colors, 4); |
| 23 | bm->setConfig(SkBitmap::kIndex8_Config, 2, 2); |
| 24 | bm->allocPixels(ctable); |
| 25 | ctable->unref(); |
| 26 | |
| 27 | *bm->getAddr8(0, 0) = 0; |
| 28 | *bm->getAddr8(1, 0) = 1; |
| 29 | *bm->getAddr8(0, 1) = 2; |
| 30 | *bm->getAddr8(1, 1) = 3; |
| 31 | } |
| 32 | |
| 33 | static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm, |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame^] | 34 | SkScalar x, SkScalar y, SkPaint* paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | #if 1 |
| 36 | canvas->drawBitmap(bm, x, y, paint); |
| 37 | return SkIntToScalar(bm.width()) * 5/4; |
| 38 | #else |
| 39 | SkRect r; |
| 40 | |
| 41 | r.set(x, y, |
| 42 | x + SkIntToScalar(bm.width() * 2), |
| 43 | y + SkIntToScalar(bm.height() * 2)); |
| 44 | SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, |
| 45 | SkShader::kRepeat_TileMode); |
| 46 | paint->setShader(s)->unref(); |
| 47 | canvas->drawRect(r, *paint); |
| 48 | paint->setShader(NULL); |
| 49 | return r.width() * 5/4; |
| 50 | #endif |
| 51 | } |
| 52 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame^] | 53 | static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x, SkPaint* p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | x += draw_bm(c, bm, x, 0, p); |
| 55 | p->setFilterBitmap(true); |
| 56 | x += draw_bm(c, bm, x, 0, p); |
| 57 | p->setDither(true); |
| 58 | return x + draw_bm(c, bm, x, 0, p); |
| 59 | } |
| 60 | |
| 61 | static const char* gConfigNames[] = { |
| 62 | "unknown config", |
| 63 | "A1", |
| 64 | "A8", |
| 65 | "Index8", |
| 66 | "565", |
| 67 | "4444", |
| 68 | "8888" |
| 69 | }; |
| 70 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame^] | 71 | static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | SkAutoCanvasRestore acr(canvas, true); |
| 73 | |
| 74 | SkPaint paint; |
| 75 | SkScalar x = 0; |
| 76 | const int scale = 32; |
| 77 | |
| 78 | paint.setAntiAlias(true); |
| 79 | const char* name = gConfigNames[bm.config()]; |
| 80 | canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8, |
| 81 | paint); |
| 82 | canvas->translate(SkIntToScalar(48), 0); |
| 83 | |
| 84 | canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
| 85 | |
| 86 | x += draw_set(canvas, bm, 0, &paint); |
| 87 | paint.reset(); |
| 88 | paint.setAlpha(0x80); |
| 89 | draw_set(canvas, bm, x, &paint); |
| 90 | return x * scale / 3; |
| 91 | } |
| 92 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame^] | 93 | class FilterView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | public: |
| 95 | SkBitmap fBM8, fBM4444, fBM16, fBM32; |
| 96 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame^] | 97 | FilterView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | make_bm(&fBM8); |
| 99 | fBM8.copyTo(&fBM4444, SkBitmap::kARGB_4444_Config); |
| 100 | fBM8.copyTo(&fBM16, SkBitmap::kRGB_565_Config); |
| 101 | fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config); |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame^] | 102 | |
| 103 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | protected: |
| 107 | // overrides from SkEventSink |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame^] | 108 | virtual bool onQuery(SkEvent* evt) { |
| 109 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | SampleCode::TitleR(evt, "Filter"); |
| 111 | return true; |
| 112 | } |
| 113 | return this->INHERITED::onQuery(evt); |
| 114 | } |
| 115 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame^] | 116 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | SkScalar x = SkIntToScalar(10); |
| 118 | SkScalar y = SkIntToScalar(10); |
| 119 | |
| 120 | canvas->translate(x, y); |
| 121 | y = draw_row(canvas, fBM8); |
| 122 | canvas->translate(0, y); |
| 123 | y = draw_row(canvas, fBM4444); |
| 124 | canvas->translate(0, y); |
| 125 | y = draw_row(canvas, fBM16); |
| 126 | canvas->translate(0, y); |
| 127 | draw_row(canvas, fBM32); |
| 128 | } |
| 129 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | private: |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame^] | 131 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | ////////////////////////////////////////////////////////////////////////////// |
| 135 | |
| 136 | static SkView* MyFactory() { return new FilterView; } |
| 137 | static SkViewRegister reg(MyFactory); |
| 138 | |