blob: 28bbb1d2b1a5cba3ef1617f02e846f54e0cb015a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkPath.h"
10#include "include/core/SkRegion.h"
11#include "include/core/SkShader.h"
12#include "include/core/SkString.h"
13#include "include/effects/SkGradientShader.h"
14#include "include/utils/SkCamera.h"
15#include "include/utils/SkRandom.h"
16#include "samplecode/DecodeFile.h"
17#include "samplecode/Sample.h"
18#include "src/effects/SkEmbossMaskFilter.h"
19#include "src/utils/SkUTF.h"
20#include "tools/timer/AnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040022class CameraView : public Sample {
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
Mike Reed50acf8f2019-04-08 13:20:23 -040042 fShaders.push_back(bm.makeShader(&matrix));
reed@android.comf2b98d62010-12-20 18:26:13 +000043 } else {
44 break;
45 }
46 }
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000047 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040051 bool onQuery(Sample::Event* evt) override {
52 if (Sample::TitleQ(*evt)) {
53 Sample::TitleR(evt, "Camera");
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 return true;
55 }
56 return this->INHERITED::onQuery(evt);
57 }
reed@android.comf2b98d62010-12-20 18:26:13 +000058
mtkleinf0599002015-07-13 06:18:39 -070059 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 canvas->translate(this->width()/2, this->height()/2);
61
62 Sk3DView view;
reed@android.comf2b98d62010-12-20 18:26:13 +000063 view.rotateX(fRX);
64 view.rotateY(fRY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 view.applyToCanvas(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +000066
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 SkPaint paint;
reed@android.comf2b98d62010-12-20 18:26:13 +000068 if (fShaders.count() > 0) {
69 bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0;
70 if (frontFace != fFrontFace) {
71 fFrontFace = frontFace;
72 fShaderIndex = (fShaderIndex + 1) % fShaders.count();
73 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
reed@android.comf2b98d62010-12-20 18:26:13 +000075 paint.setAntiAlias(true);
76 paint.setShader(fShaders[fShaderIndex]);
reed93a12152015-03-16 10:08:34 -070077 paint.setFilterQuality(kLow_SkFilterQuality);
reed@android.comf2b98d62010-12-20 18:26:13 +000078 SkRect r = { -150, -150, 150, 150 };
79 canvas->drawRoundRect(r, 30, 30, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 }
reedd9adfe62015-02-01 19:01:04 -080081 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
Mike Kleincd5104e2019-03-20 11:55:08 -050083 bool onAnimate(const AnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -080084 if (timer.isStopped()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 fRY = 0;
reed76113a92015-02-02 12:55:02 -080086 } else {
87 fRY = timer.scaled(90, 360);
reed@android.comf2b98d62010-12-20 18:26:13 +000088 }
reedd9adfe62015-02-01 19:01:04 -080089 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 }
91
reed@android.com8a1c16f2008-12-17 15:59:43 +000092private:
reed@android.comf2b98d62010-12-20 18:26:13 +000093 SkScalar fRX, fRY, fRZ;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040094 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000095};
96
97//////////////////////////////////////////////////////////////////////////////
98
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040099DEF_SAMPLE( return new CameraView(); )