3c0be8376848909129bd29c1d2e6c8b6ffeec847
[platform/upstream/libSkiaSharp.git] / samplecode / SampleAnimBlur.cpp
1 /*
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  */
7
8 #include "SampleCode.h"
9 #include "SkAnimTimer.h"
10 #include "SkBlurMaskFilter.h"
11 #include "SkColorPriv.h"
12 #include "SkCanvas.h"
13 #include "SkRandom.h"
14
15 SkScalar get_anim_sin(double secs, SkScalar amplitude, SkScalar periodInSec, SkScalar phaseInSec) {
16     if (!periodInSec) {
17         return 0;
18     }
19     double t = secs + phaseInSec;
20     t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
21     amplitude = SK_ScalarHalf * amplitude;
22     return amplitude * SkDoubleToScalar(sin(t)) + amplitude;
23 }
24
25 class AnimBlurView : public SampleView {
26 public:
27     AnimBlurView() : fBlurSigma(0), fCircleRadius(100) {}
28
29 protected:
30     // overrides from SkEventSink
31     bool onQuery(SkEvent* evt) SK_OVERRIDE {
32         if (SampleCode::TitleQ(*evt)) {
33             SampleCode::TitleR(evt, "AnimBlur");
34             return true;
35         }
36         return this->INHERITED::onQuery(evt);
37     }
38
39     void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
40         static const SkBlurStyle gStyles[] = {
41             kNormal_SkBlurStyle,
42             kInner_SkBlurStyle,
43             kSolid_SkBlurStyle,
44             kOuter_SkBlurStyle,
45         };
46         SkRandom random;
47
48         for (size_t i = 0; i < SK_ARRAY_COUNT(gStyles); ++i) {
49             SkMaskFilter* mf = SkBlurMaskFilter::Create(
50                                        gStyles[i],
51                                        fBlurSigma,
52                                        SkBlurMaskFilter::kHighQuality_BlurFlag);
53             SkPaint paint;
54             SkSafeUnref(paint.setMaskFilter(mf));
55             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,
58                                fCircleRadius, paint);
59         }
60     }
61
62     bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
63         fBlurSigma = get_anim_sin(timer.secs(), 100, 4, 5);
64         fCircleRadius = 3 + get_anim_sin(timer.secs(), 150, 25, 3);
65         return true;
66     }
67
68 private:
69     SkScalar fBlurSigma, fCircleRadius;
70
71     typedef SampleView INHERITED;
72 };
73
74 //////////////////////////////////////////////////////////////////////////////
75
76 static SkView* MyFactory() { return new AnimBlurView; }
77 static SkViewRegister reg(MyFactory);