blob: bd5ff475d793671a5e0f108bb91245dccdb3a9a5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com47ca0362009-09-02 02:07:45 +00008#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
reed@android.com47ca0362009-09-02 02:07:45 +000010#include "SkBlurMaskFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000011#include "SkCanvas.h"
reed@android.com47ca0362009-09-02 02:07:45 +000012#include "SkColorPriv.h"
13#include "SkGradientShader.h"
reed@android.com47ca0362009-09-02 02:07:45 +000014#include "SkUtils.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000015#include "SkView.h"
reed@android.com47ca0362009-09-02 02:07:45 +000016
17static SkBitmap make_bitmap() {
reed@google.com0a6151d2013-10-10 14:44:56 +000018 SkPMColor c[256];
reed@android.com47ca0362009-09-02 02:07:45 +000019 for (int i = 0; i < 256; i++) {
20 c[i] = SkPackARGB32(255 - i, 0, 0, 0);
21 }
reed@google.com0a6151d2013-10-10 14:44:56 +000022
23 SkBitmap bm;
24 SkColorTable* ctable = new SkColorTable(c, 256);
25
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000026 bm.allocPixels(SkImageInfo::Make(256, 256, kIndex_8_SkColorType,
27 kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -070028 nullptr, ctable);
reed@android.com47ca0362009-09-02 02:07:45 +000029 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.orge36ddf02011-07-15 14:28:16 +000053class BlurView : public SampleView {
reed@android.com47ca0362009-09-02 02:07:45 +000054 SkBitmap fBM;
55public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000056 BlurView() {
caryclark@google.com02939ce2012-06-06 12:09:51 +000057 if (false) { // avoid bit rot, suppress warning
58 fBM = make_bitmap();
59 }
reed@android.com47ca0362009-09-02 02:07:45 +000060 }
reed@google.com0ecc2b792011-02-16 13:06:15 +000061
reed@android.com47ca0362009-09-02 02:07:45 +000062protected:
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.com0ecc2b792011-02-16 13:06:15 +000071
reed@android.com47ca0362009-09-02 02:07:45 +000072 void drawBG(SkCanvas* canvas) {
73 canvas->drawColor(0xFFDDDDDD);
74 }
reed@google.com0ecc2b792011-02-16 13:06:15 +000075
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000076 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com47ca0362009-09-02 02:07:45 +000077 drawBG(canvas);
reed@google.com0ecc2b792011-02-16 13:06:15 +000078
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000079 SkBlurStyle NONE = SkBlurStyle(-999);
reed@android.comda449a32009-09-18 20:57:05 +000080 static const struct {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000081 SkBlurStyle fStyle;
82 int fCx, fCy;
reed@android.comda449a32009-09-18 20:57:05 +000083 } gRecs[] = {
84 { NONE, 0, 0 },
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000085 { kInner_SkBlurStyle, -1, 0 },
86 { kNormal_SkBlurStyle, 0, 1 },
87 { kSolid_SkBlurStyle, 0, -1 },
88 { kOuter_SkBlurStyle, 1, 0 },
reed@android.comda449a32009-09-18 20:57:05 +000089 };
reed@android.com47ca0362009-09-02 02:07:45 +000090
reed@android.com47ca0362009-09-02 02:07:45 +000091 SkPaint paint;
92 paint.setAntiAlias(true);
reed@google.com0ecc2b792011-02-16 13:06:15 +000093 paint.setTextSize(25);
94 canvas->translate(-40, 0);
reed@android.comda449a32009-09-18 20:57:05 +000095
reed@google.com0ecc2b792011-02-16 13:06:15 +000096 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.comb7061172013-09-06 14:16:12 +0000102 SkMaskFilter* mf = SkBlurMaskFilter::Create(gRecs[i].fStyle,
103 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)),
104 flags);
reed@google.com0ecc2b792011-02-16 13:06:15 +0000105 paint.setMaskFilter(mf)->unref();
106 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700107 paint.setMaskFilter(nullptr);
reed@google.com0ecc2b792011-02-16 13:06:15 +0000108 }
reed@google.com261b8e22011-04-14 17:53:24 +0000109 canvas->drawCircle(200 + gRecs[i].fCx*100.f,
110 200 + gRecs[i].fCy*100.f, 50, paint);
reed@android.comda449a32009-09-18 20:57:05 +0000111 }
reed@google.com0ecc2b792011-02-16 13:06:15 +0000112 // draw text
113 {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000114 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000115 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)),
116 flags);
reed@google.com0ecc2b792011-02-16 13:06:15 +0000117 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);
halcanary96fcdcc2015-08-27 07:41:13 -0700123 paint.setMaskFilter(nullptr);
reed@google.com0ecc2b792011-02-16 13:06:15 +0000124 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.com11a5ff32011-02-18 20:20:51 +0000130 flags = SkBlurMaskFilter::kHighQuality_BlurFlag;
reed@google.com0ecc2b792011-02-16 13:06:15 +0000131 canvas->translate(350, 0);
reed@android.comda449a32009-09-18 20:57:05 +0000132 }
reed@android.com47ca0362009-09-02 02:07:45 +0000133 }
reed@google.com0ecc2b792011-02-16 13:06:15 +0000134
reed@android.com47ca0362009-09-02 02:07:45 +0000135private:
136 typedef SkView INHERITED;
137};
138
139//////////////////////////////////////////////////////////////////////////////
140
141static SkView* MyFactory() { return new BlurView; }
142static SkViewRegister reg(MyFactory);