1 /*
2 * Copyright 2011 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 "DecodeFile.h"
9 #include "SampleCode.h"
10 #include "SkAnimTimer.h"
11 #include "SkView.h"
12 #include "SkCanvas.h"
13 #include "SkCamera.h"
14 #include "SkEmbossMaskFilter.h"
15 #include "SkGradientShader.h"
16 #include "SkPath.h"
17 #include "SkRandom.h"
18 #include "SkRegion.h"
19 #include "SkShader.h"
20 #include "SkString.h"
21 #include "SkUtils.h"
22
23 class CameraView : public SampleView {
24 SkTArray<sk_sp<SkShader>> fShaders;
25 int fShaderIndex;
26 bool fFrontFace;
27 public:
CameraView()28 CameraView() {
29 fRX = fRY = fRZ = 0;
30 fShaderIndex = 0;
31 fFrontFace = false;
32
33 for (int i = 0;; i++) {
34 SkString str;
35 str.printf("/skimages/elephant%d.jpeg", i);
36 SkBitmap bm;
37 if (decode_file(str.c_str(), &bm)) {
38 SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
39 SkRect dst = { -150, -150, 150, 150 };
40 SkMatrix matrix;
41 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
42
43 fShaders.push_back(SkShader::MakeBitmapShader(bm,
44 SkShader::kClamp_TileMode,
45 SkShader::kClamp_TileMode,
46 &matrix));
47 } else {
48 break;
49 }
50 }
51 this->setBGColor(0xFFDDDDDD);
52 }
53
54 protected:
55 // overrides from SkEventSink
onQuery(SkEvent * evt)56 bool onQuery(SkEvent* evt) override {
57 if (SampleCode::TitleQ(*evt)) {
58 SampleCode::TitleR(evt, "Camera");
59 return true;
60 }
61 return this->INHERITED::onQuery(evt);
62 }
63
onDrawContent(SkCanvas * canvas)64 void onDrawContent(SkCanvas* canvas) override {
65 canvas->translate(this->width()/2, this->height()/2);
66
67 Sk3DView view;
68 view.rotateX(fRX);
69 view.rotateY(fRY);
70 view.applyToCanvas(canvas);
71
72 SkPaint paint;
73 if (fShaders.count() > 0) {
74 bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0;
75 if (frontFace != fFrontFace) {
76 fFrontFace = frontFace;
77 fShaderIndex = (fShaderIndex + 1) % fShaders.count();
78 }
79
80 paint.setAntiAlias(true);
81 paint.setShader(fShaders[fShaderIndex]);
82 paint.setFilterQuality(kLow_SkFilterQuality);
83 SkRect r = { -150, -150, 150, 150 };
84 canvas->drawRoundRect(r, 30, 30, paint);
85 }
86 }
87
onAnimate(const SkAnimTimer & timer)88 bool onAnimate(const SkAnimTimer& timer) override {
89 if (timer.isStopped()) {
90 fRY = 0;
91 } else {
92 fRY = timer.scaled(90, 360);
93 }
94 return true;
95 }
96
97 private:
98 SkScalar fRX, fRY, fRZ;
99 typedef SampleView INHERITED;
100 };
101
102 //////////////////////////////////////////////////////////////////////////////
103
MyFactory()104 static SkView* MyFactory() { return new CameraView; }
105 static SkViewRegister reg(MyFactory);
106