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