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 | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 10 | #include "SkBlurMaskFilter.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 12 | #include "SkColorPriv.h" |
| 13 | #include "SkGradientShader.h" |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 14 | #include "SkUtils.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 15 | #include "SkView.h" |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 16 | |
| 17 | static SkBitmap make_bitmap() { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 18 | SkPMColor c[256]; |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 19 | for (int i = 0; i < 256; i++) { |
| 20 | c[i] = SkPackARGB32(255 - i, 0, 0, 0); |
| 21 | } |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 22 | |
| 23 | SkBitmap bm; |
| 24 | SkColorTable* ctable = new SkColorTable(c, 256); |
| 25 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 26 | bm.allocPixels(SkImageInfo::Make(256, 256, kIndex_8_SkColorType, |
| 27 | kPremul_SkAlphaType), |
| 28 | NULL, ctable); |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 29 | ctable->unref(); |
| 30 | |
| 31 | bm.lockPixels(); |
| 32 | const float cx = bm.width() * 0.5f; |
| 33 | const float cy = bm.height() * 0.5f; |
| 34 | for (int y = 0; y < bm.height(); y++) { |
| 35 | float dy = y - cy; |
| 36 | dy *= dy; |
| 37 | uint8_t* p = bm.getAddr8(0, y); |
| 38 | for (int x = 0; x < 256; x++) { |
| 39 | float dx = x - cx; |
| 40 | dx *= dx; |
| 41 | float d = (dx + dy) / (cx/2); |
| 42 | int id = (int)d; |
| 43 | if (id > 255) { |
| 44 | id = 255; |
| 45 | } |
| 46 | p[x] = id; |
| 47 | } |
| 48 | } |
| 49 | bm.unlockPixels(); |
| 50 | return bm; |
| 51 | } |
| 52 | |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 53 | class BlurView : public SampleView { |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 54 | SkBitmap fBM; |
| 55 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 56 | BlurView() { |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 57 | if (false) { // avoid bit rot, suppress warning |
| 58 | fBM = make_bitmap(); |
| 59 | } |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 60 | } |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 61 | |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 62 | protected: |
| 63 | // overrides from SkEventSink |
| 64 | virtual bool onQuery(SkEvent* evt) { |
| 65 | if (SampleCode::TitleQ(*evt)) { |
| 66 | SampleCode::TitleR(evt, "Blur"); |
| 67 | return true; |
| 68 | } |
| 69 | return this->INHERITED::onQuery(evt); |
| 70 | } |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 71 | |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 72 | void drawBG(SkCanvas* canvas) { |
| 73 | canvas->drawColor(0xFFDDDDDD); |
| 74 | } |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 75 | |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 76 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 77 | drawBG(canvas); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 78 | |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 79 | SkBlurStyle NONE = SkBlurStyle(-999); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 80 | static const struct { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 81 | SkBlurStyle fStyle; |
| 82 | int fCx, fCy; |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 83 | } gRecs[] = { |
| 84 | { NONE, 0, 0 }, |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 85 | { kInner_SkBlurStyle, -1, 0 }, |
| 86 | { kNormal_SkBlurStyle, 0, 1 }, |
| 87 | { kSolid_SkBlurStyle, 0, -1 }, |
| 88 | { kOuter_SkBlurStyle, 1, 0 }, |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 89 | }; |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 90 | |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 91 | SkPaint paint; |
| 92 | paint.setAntiAlias(true); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 93 | paint.setTextSize(25); |
| 94 | canvas->translate(-40, 0); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 95 | |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 96 | SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag; |
| 97 | for (int j = 0; j < 2; j++) { |
| 98 | canvas->save(); |
| 99 | paint.setColor(SK_ColorBLUE); |
| 100 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) { |
| 101 | if (gRecs[i].fStyle != NONE) { |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 102 | SkMaskFilter* mf = SkBlurMaskFilter::Create(gRecs[i].fStyle, |
| 103 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)), |
| 104 | flags); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 105 | paint.setMaskFilter(mf)->unref(); |
| 106 | } else { |
| 107 | paint.setMaskFilter(NULL); |
| 108 | } |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 109 | canvas->drawCircle(200 + gRecs[i].fCx*100.f, |
| 110 | 200 + gRecs[i].fCy*100.f, 50, paint); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 111 | } |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 112 | // draw text |
| 113 | { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 114 | SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 115 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)), |
| 116 | flags); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 117 | paint.setMaskFilter(mf)->unref(); |
| 118 | SkScalar x = SkIntToScalar(70); |
| 119 | SkScalar y = SkIntToScalar(400); |
| 120 | paint.setColor(SK_ColorBLACK); |
| 121 | canvas->drawText("Hamburgefons Style", 18, x, y, paint); |
| 122 | canvas->drawText("Hamburgefons Style", 18, x, y + SkIntToScalar(50), paint); |
| 123 | paint.setMaskFilter(NULL); |
| 124 | paint.setColor(SK_ColorWHITE); |
| 125 | x -= SkIntToScalar(2); |
| 126 | y -= SkIntToScalar(2); |
| 127 | canvas->drawText("Hamburgefons Style", 18, x, y, paint); |
| 128 | } |
| 129 | canvas->restore(); |
reed@google.com | 11a5ff3 | 2011-02-18 20:20:51 +0000 | [diff] [blame] | 130 | flags = SkBlurMaskFilter::kHighQuality_BlurFlag; |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 131 | canvas->translate(350, 0); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 132 | } |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 133 | } |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 134 | |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 135 | private: |
| 136 | typedef SkView INHERITED; |
| 137 | }; |
| 138 | |
| 139 | ////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | static SkView* MyFactory() { return new BlurView; } |
| 142 | static SkViewRegister reg(MyFactory); |