blob: fdba40375a7c073d4d36b0a4a84b0b1154d53387 [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"
Brian Osman8ceee432017-12-01 10:52:28 -050017#include "SkRandom.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkRegion.h"
19#include "SkShader.h"
Brian Osman8ceee432017-12-01 10:52:28 -050020#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000023class CameraView : public SampleView {
reedfe630452016-03-25 09:08:00 -070024 SkTArray<sk_sp<SkShader>> fShaders;
reed@android.comf2b98d62010-12-20 18:26:13 +000025 int fShaderIndex;
26 bool fFrontFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +000027public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000028 CameraView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 fRX = fRY = fRZ = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +000030 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;
msarettd15750c2016-03-18 15:48:49 -070037 if (decode_file(str.c_str(), &bm)) {
reed@google.com261b8e22011-04-14 17:53:24 +000038 SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@android.comf2b98d62010-12-20 18:26:13 +000039 SkRect dst = { -150, -150, 150, 150 };
40 SkMatrix matrix;
41 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000042
reedfe630452016-03-25 09:08:00 -070043 fShaders.push_back(SkShader::MakeBitmapShader(bm,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000044 SkShader::kClamp_TileMode,
45 SkShader::kClamp_TileMode,
reedfe630452016-03-25 09:08:00 -070046 &matrix));
reed@android.comf2b98d62010-12-20 18:26:13 +000047 } else {
48 break;
49 }
50 }
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000051 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000053
reed@android.com8a1c16f2008-12-17 15:59:43 +000054protected:
55 // overrides from SkEventSink
mtkleinf0599002015-07-13 06:18:39 -070056 bool onQuery(SkEvent* evt) override {
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000057 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 SampleCode::TitleR(evt, "Camera");
59 return true;
60 }
61 return this->INHERITED::onQuery(evt);
62 }
reed@android.comf2b98d62010-12-20 18:26:13 +000063
mtkleinf0599002015-07-13 06:18:39 -070064 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 canvas->translate(this->width()/2, this->height()/2);
66
67 Sk3DView view;
reed@android.comf2b98d62010-12-20 18:26:13 +000068 view.rotateX(fRX);
69 view.rotateY(fRY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 view.applyToCanvas(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 SkPaint paint;
reed@android.comf2b98d62010-12-20 18:26:13 +000073 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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
reed@android.comf2b98d62010-12-20 18:26:13 +000080 paint.setAntiAlias(true);
81 paint.setShader(fShaders[fShaderIndex]);
reed93a12152015-03-16 10:08:34 -070082 paint.setFilterQuality(kLow_SkFilterQuality);
reed@android.comf2b98d62010-12-20 18:26:13 +000083 SkRect r = { -150, -150, 150, 150 };
84 canvas->drawRoundRect(r, 30, 30, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 }
reedd9adfe62015-02-01 19:01:04 -080086 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
mtklein36352bf2015-03-25 18:17:31 -070088 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -080089 if (timer.isStopped()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 fRY = 0;
reed76113a92015-02-02 12:55:02 -080091 } else {
92 fRY = timer.scaled(90, 360);
reed@android.comf2b98d62010-12-20 18:26:13 +000093 }
reedd9adfe62015-02-01 19:01:04 -080094 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 }
96
reed@android.com8a1c16f2008-12-17 15:59:43 +000097private:
reed@android.comf2b98d62010-12-20 18:26:13 +000098 SkScalar fRX, fRY, fRZ;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000099 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100};
101
102//////////////////////////////////////////////////////////////////////////////
103
104static SkView* MyFactory() { return new CameraView; }
105static SkViewRegister reg(MyFactory);