blob: 85b854eced17c69c2673b2349a40a227193c756f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
mike@reedtribe.orgd173b872014-01-23 02:02:45 +00007
msarettd15750c2016-03-18 15:48:49 -07008#include "DecodeFile.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -080010#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#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 "SkRegion.h"
18#include "SkShader.h"
19#include "SkUtils.h"
20#include "SkRandom.h"
21
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000022class CameraView : public SampleView {
reedfe630452016-03-25 09:08:00 -070023 SkTArray<sk_sp<SkShader>> fShaders;
reed@android.comf2b98d62010-12-20 18:26:13 +000024 int fShaderIndex;
25 bool fFrontFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +000026public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000027 CameraView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 fRX = fRY = fRZ = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +000029 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;
msarettd15750c2016-03-18 15:48:49 -070036 if (decode_file(str.c_str(), &bm)) {
reed@google.com261b8e22011-04-14 17:53:24 +000037 SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@android.comf2b98d62010-12-20 18:26:13 +000038 SkRect dst = { -150, -150, 150, 150 };
39 SkMatrix matrix;
40 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000041
reedfe630452016-03-25 09:08:00 -070042 fShaders.push_back(SkShader::MakeBitmapShader(bm,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000043 SkShader::kClamp_TileMode,
44 SkShader::kClamp_TileMode,
reedfe630452016-03-25 09:08:00 -070045 &matrix));
reed@android.comf2b98d62010-12-20 18:26:13 +000046 } else {
47 break;
48 }
49 }
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000050 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053protected:
54 // overrides from SkEventSink
mtkleinf0599002015-07-13 06:18:39 -070055 bool onQuery(SkEvent* evt) override {
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000056 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 SampleCode::TitleR(evt, "Camera");
58 return true;
59 }
60 return this->INHERITED::onQuery(evt);
61 }
reed@android.comf2b98d62010-12-20 18:26:13 +000062
mtkleinf0599002015-07-13 06:18:39 -070063 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 canvas->translate(this->width()/2, this->height()/2);
65
66 Sk3DView view;
reed@android.comf2b98d62010-12-20 18:26:13 +000067 view.rotateX(fRX);
68 view.rotateY(fRY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 view.applyToCanvas(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +000070
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 SkPaint paint;
reed@android.comf2b98d62010-12-20 18:26:13 +000072 if (fShaders.count() > 0) {
73 bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0;
74 if (frontFace != fFrontFace) {
75 fFrontFace = frontFace;
76 fShaderIndex = (fShaderIndex + 1) % fShaders.count();
77 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
reed@android.comf2b98d62010-12-20 18:26:13 +000079 paint.setAntiAlias(true);
80 paint.setShader(fShaders[fShaderIndex]);
reed93a12152015-03-16 10:08:34 -070081 paint.setFilterQuality(kLow_SkFilterQuality);
reed@android.comf2b98d62010-12-20 18:26:13 +000082 SkRect r = { -150, -150, 150, 150 };
83 canvas->drawRoundRect(r, 30, 30, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 }
reedd9adfe62015-02-01 19:01:04 -080085 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
mtklein36352bf2015-03-25 18:17:31 -070087 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -080088 if (timer.isStopped()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 fRY = 0;
reed76113a92015-02-02 12:55:02 -080090 } else {
91 fRY = timer.scaled(90, 360);
reed@android.comf2b98d62010-12-20 18:26:13 +000092 }
reedd9adfe62015-02-01 19:01:04 -080093 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 }
95
reed@android.com8a1c16f2008-12-17 15:59:43 +000096private:
reed@android.comf2b98d62010-12-20 18:26:13 +000097 SkScalar fRX, fRY, fRZ;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000098 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000099};
100
101//////////////////////////////////////////////////////////////////////////////
102
103static SkView* MyFactory() { return new CameraView; }
104static SkViewRegister reg(MyFactory);