blob: 7660129c386705d753de464973c665b82626b16b [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"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkCamera.h"
12#include "SkEmbossMaskFilter.h"
13#include "SkGradientShader.h"
14#include "SkPath.h"
15#include "SkRegion.h"
16#include "SkShader.h"
17#include "SkUtils.h"
18#include "SkRandom.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000019#include "SkImageDecoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000021class CameraView : public SampleView {
reed@android.comf2b98d62010-12-20 18:26:13 +000022 SkTDArray<SkShader*> fShaders;
23 int fShaderIndex;
24 bool fFrontFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +000025public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000026 CameraView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 fRX = fRY = fRZ = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +000028 fShaderIndex = 0;
29 fFrontFace = false;
30
31 for (int i = 0;; i++) {
32 SkString str;
33 str.printf("/skimages/elephant%d.jpeg", i);
34 SkBitmap bm;
35 if (SkImageDecoder::DecodeFile(str.c_str(), &bm)) {
reed@google.com261b8e22011-04-14 17:53:24 +000036 SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@android.comf2b98d62010-12-20 18:26:13 +000037 SkRect dst = { -150, -150, 150, 150 };
38 SkMatrix matrix;
39 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000040
41 SkShader* s = SkShader::CreateBitmapShader(bm,
42 SkShader::kClamp_TileMode,
43 SkShader::kClamp_TileMode,
44 &matrix);
reed@android.comf2b98d62010-12-20 18:26:13 +000045 *fShaders.append() = s;
46 } 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.comf2b98d62010-12-20 18:26:13 +000053 virtual ~CameraView() {
54 fShaders.unrefAll();
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 }
56
57protected:
58 // overrides from SkEventSink
reed@android.comf2b98d62010-12-20 18:26:13 +000059 virtual bool onQuery(SkEvent* evt) {
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000060 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 SampleCode::TitleR(evt, "Camera");
62 return true;
63 }
64 return this->INHERITED::onQuery(evt);
65 }
reed@android.comf2b98d62010-12-20 18:26:13 +000066
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000067 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 canvas->translate(this->width()/2, this->height()/2);
69
70 Sk3DView view;
reed@android.comf2b98d62010-12-20 18:26:13 +000071 view.rotateX(fRX);
72 view.rotateY(fRY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 view.applyToCanvas(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 SkPaint paint;
reed@android.comf2b98d62010-12-20 18:26:13 +000076 if (fShaders.count() > 0) {
77 bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0;
78 if (frontFace != fFrontFace) {
79 fFrontFace = frontFace;
80 fShaderIndex = (fShaderIndex + 1) % fShaders.count();
81 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
reed@android.comf2b98d62010-12-20 18:26:13 +000083 paint.setAntiAlias(true);
84 paint.setShader(fShaders[fShaderIndex]);
mike@reedtribe.orgd173b872014-01-23 02:02:45 +000085 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
reed@android.comf2b98d62010-12-20 18:26:13 +000086 SkRect r = { -150, -150, 150, 150 };
87 canvas->drawRoundRect(r, 30, 30, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
reed@android.comf2b98d62010-12-20 18:26:13 +000090 fRY += SampleCode::GetAnimSecondsDelta() * 90;
91 if (fRY >= SkIntToScalar(360)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 fRY = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +000093 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 this->inval(NULL);
95 }
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);