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