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