blob: 991013a850251e830bb0e68e9e708d8a34a016a8 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#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"
reed@android.comf2b98d62010-12-20 18:26:13 +000020#include "SkImageDecoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000022class CameraView : public SampleView {
reed@android.comf2b98d62010-12-20 18:26:13 +000023 SkTDArray<SkShader*> fShaders;
24 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;
36 if (SkImageDecoder::DecodeFile(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
42 SkShader* s = SkShader::CreateBitmapShader(bm,
43 SkShader::kClamp_TileMode,
44 SkShader::kClamp_TileMode,
45 &matrix);
reed@android.comf2b98d62010-12-20 18:26:13 +000046 *fShaders.append() = s;
47 } 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.comf2b98d62010-12-20 18:26:13 +000054 virtual ~CameraView() {
55 fShaders.unrefAll();
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 }
57
58protected:
59 // overrides from SkEventSink
mtkleinf0599002015-07-13 06:18:39 -070060 bool onQuery(SkEvent* evt) override {
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000061 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 SampleCode::TitleR(evt, "Camera");
63 return true;
64 }
65 return this->INHERITED::onQuery(evt);
66 }
reed@android.comf2b98d62010-12-20 18:26:13 +000067
mtkleinf0599002015-07-13 06:18:39 -070068 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 canvas->translate(this->width()/2, this->height()/2);
70
71 Sk3DView view;
reed@android.comf2b98d62010-12-20 18:26:13 +000072 view.rotateX(fRX);
73 view.rotateY(fRY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 view.applyToCanvas(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 SkPaint paint;
reed@android.comf2b98d62010-12-20 18:26:13 +000077 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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000083
reed@android.comf2b98d62010-12-20 18:26:13 +000084 paint.setAntiAlias(true);
85 paint.setShader(fShaders[fShaderIndex]);
reed93a12152015-03-16 10:08:34 -070086 paint.setFilterQuality(kLow_SkFilterQuality);
reed@android.comf2b98d62010-12-20 18:26:13 +000087 SkRect r = { -150, -150, 150, 150 };
88 canvas->drawRoundRect(r, 30, 30, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 }
reedd9adfe62015-02-01 19:01:04 -080090 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
mtklein36352bf2015-03-25 18:17:31 -070092 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -080093 if (timer.isStopped()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 fRY = 0;
reed76113a92015-02-02 12:55:02 -080095 } else {
96 fRY = timer.scaled(90, 360);
reed@android.comf2b98d62010-12-20 18:26:13 +000097 }
reedd9adfe62015-02-01 19:01:04 -080098 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 }
100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101private:
reed@android.comf2b98d62010-12-20 18:26:13 +0000102 SkScalar fRX, fRY, fRZ;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000103 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104};
105
106//////////////////////////////////////////////////////////////////////////////
107
108static SkView* MyFactory() { return new CameraView; }
109static SkViewRegister reg(MyFactory);