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