blob: 98a3560fe9e9f76ed012ca629642123b8ee5408a [file] [log] [blame]
bsalomon@google.com85003222012-03-28 14:44:37 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed76113a92015-02-02 12:55:02 -08007
bsalomon@google.com85003222012-03-28 14:44:37 +00008#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
bsalomon@google.com85003222012-03-28 14:44:37 +000010#include "SkBlurMaskFilter.h"
11#include "SkColorPriv.h"
12#include "SkCanvas.h"
13#include "SkRandom.h"
14
reed76113a92015-02-02 12:55:02 -080015SkScalar get_anim_sin(double secs, SkScalar amplitude, SkScalar periodInSec, SkScalar phaseInSec) {
reedd9adfe62015-02-01 19:01:04 -080016 if (!periodInSec) {
17 return 0;
18 }
reed76113a92015-02-02 12:55:02 -080019 double t = secs + phaseInSec;
reedd9adfe62015-02-01 19:01:04 -080020 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
21 amplitude = SK_ScalarHalf * amplitude;
22 return amplitude * SkDoubleToScalar(sin(t)) + amplitude;
23}
24
bsalomon@google.com85003222012-03-28 14:44:37 +000025class AnimBlurView : public SampleView {
26public:
reedd9adfe62015-02-01 19:01:04 -080027 AnimBlurView() : fBlurSigma(0), fCircleRadius(100) {}
bsalomon@google.com85003222012-03-28 14:44:37 +000028
29protected:
30 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070031 bool onQuery(SkEvent* evt) override {
bsalomon@google.com85003222012-03-28 14:44:37 +000032 if (SampleCode::TitleQ(*evt)) {
33 SampleCode::TitleR(evt, "AnimBlur");
34 return true;
35 }
36 return this->INHERITED::onQuery(evt);
37 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 void onDrawContent(SkCanvas* canvas) override {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000040 static const SkBlurStyle gStyles[] = {
41 kNormal_SkBlurStyle,
42 kInner_SkBlurStyle,
43 kSolid_SkBlurStyle,
44 kOuter_SkBlurStyle,
bsalomon@google.com85003222012-03-28 14:44:37 +000045 };
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000046 SkRandom random;
bsalomon@google.com85003222012-03-28 14:44:37 +000047
robertphillips@google.com7460b372012-04-25 16:54:51 +000048 for (size_t i = 0; i < SK_ARRAY_COUNT(gStyles); ++i) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000049 SkMaskFilter* mf = SkBlurMaskFilter::Create(
bsalomon@google.com85003222012-03-28 14:44:37 +000050 gStyles[i],
reedd9adfe62015-02-01 19:01:04 -080051 fBlurSigma,
bsalomon@google.com85003222012-03-28 14:44:37 +000052 SkBlurMaskFilter::kHighQuality_BlurFlag);
53 SkPaint paint;
reed@google.com59ed2c22012-04-09 17:12:30 +000054 SkSafeUnref(paint.setMaskFilter(mf));
bsalomon@google.com85003222012-03-28 14:44:37 +000055 paint.setColor(random.nextU() | 0xff000000);
56 canvas->drawCircle(200 * SK_Scalar1 + 400 * (i % 2) * SK_Scalar1,
57 200 * SK_Scalar1 + i / 2 * 400 * SK_Scalar1,
reedd9adfe62015-02-01 19:01:04 -080058 fCircleRadius, paint);
bsalomon@google.com85003222012-03-28 14:44:37 +000059 }
reedd9adfe62015-02-01 19:01:04 -080060 }
61
mtklein36352bf2015-03-25 18:17:31 -070062 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -080063 fBlurSigma = get_anim_sin(timer.secs(), 100, 4, 5);
64 fCircleRadius = 3 + get_anim_sin(timer.secs(), 150, 25, 3);
reedd9adfe62015-02-01 19:01:04 -080065 return true;
bsalomon@google.com85003222012-03-28 14:44:37 +000066 }
67
68private:
reedd9adfe62015-02-01 19:01:04 -080069 SkScalar fBlurSigma, fCircleRadius;
70
71 typedef SampleView INHERITED;
bsalomon@google.com85003222012-03-28 14:44:37 +000072};
73
74//////////////////////////////////////////////////////////////////////////////
75
76static SkView* MyFactory() { return new AnimBlurView; }
77static SkViewRegister reg(MyFactory);