blob: d951d8508b528961c9444405835294ac6d541123 [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
reed@android.com47ca0362009-09-02 02:07:45 +000026 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.orge36ddf02011-07-15 14:28:16 +000052class BlurView : public SampleView {
reed@android.com47ca0362009-09-02 02:07:45 +000053 SkBitmap fBM;
54public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000055 BlurView() {
caryclark@google.com02939ce2012-06-06 12:09:51 +000056 if (false) { // avoid bit rot, suppress warning
57 fBM = make_bitmap();
58 }
reed@android.com47ca0362009-09-02 02:07:45 +000059 }
reed@google.com0ecc2b792011-02-16 13:06:15 +000060
reed@android.com47ca0362009-09-02 02:07:45 +000061protected:
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.com0ecc2b792011-02-16 13:06:15 +000070
reed@android.com47ca0362009-09-02 02:07:45 +000071 void drawBG(SkCanvas* canvas) {
72 canvas->drawColor(0xFFDDDDDD);
73 }
reed@google.com0ecc2b792011-02-16 13:06:15 +000074
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000075 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com47ca0362009-09-02 02:07:45 +000076 drawBG(canvas);
reed@google.com0ecc2b792011-02-16 13:06:15 +000077
reed@android.comda449a32009-09-18 20:57:05 +000078 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.com47ca0362009-09-02 02:07:45 +000089
reed@android.com47ca0362009-09-02 02:07:45 +000090 SkPaint paint;
91 paint.setAntiAlias(true);
reed@google.com0ecc2b792011-02-16 13:06:15 +000092 paint.setTextSize(25);
93 canvas->translate(-40, 0);
reed@android.comda449a32009-09-18 20:57:05 +000094
reed@google.com0ecc2b792011-02-16 13:06:15 +000095 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.comb7061172013-09-06 14:16:12 +0000101 SkMaskFilter* mf = SkBlurMaskFilter::Create(gRecs[i].fStyle,
102 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)),
103 flags);
reed@google.com0ecc2b792011-02-16 13:06:15 +0000104 paint.setMaskFilter(mf)->unref();
105 } else {
106 paint.setMaskFilter(NULL);
107 }
reed@google.com261b8e22011-04-14 17:53:24 +0000108 canvas->drawCircle(200 + gRecs[i].fCx*100.f,
109 200 + gRecs[i].fCy*100.f, 50, paint);
reed@android.comda449a32009-09-18 20:57:05 +0000110 }
reed@google.com0ecc2b792011-02-16 13:06:15 +0000111 // draw text
112 {
robertphillips@google.comb7061172013-09-06 14:16:12 +0000113 SkMaskFilter* mf = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
114 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)),
115 flags);
reed@google.com0ecc2b792011-02-16 13:06:15 +0000116 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.com11a5ff32011-02-18 20:20:51 +0000129 flags = SkBlurMaskFilter::kHighQuality_BlurFlag;
reed@google.com0ecc2b792011-02-16 13:06:15 +0000130 canvas->translate(350, 0);
reed@android.comda449a32009-09-18 20:57:05 +0000131 }
reed@android.com47ca0362009-09-02 02:07:45 +0000132 }
reed@google.com0ecc2b792011-02-16 13:06:15 +0000133
reed@android.com47ca0362009-09-02 02:07:45 +0000134private:
135 typedef SkView INHERITED;
136};
137
138//////////////////////////////////////////////////////////////////////////////
139
140static SkView* MyFactory() { return new BlurView; }
141static SkViewRegister reg(MyFactory);