blob: 3ad74550aaed9443005e0df2112aa79d246c3733 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkShader.h"
10#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/utils/SkCamera.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "samplecode/DecodeFile.h"
13#include "samplecode/Sample.h"
14#include "src/effects/SkEmbossMaskFilter.h"
Hal Canary455c41a2019-07-03 21:01:52 -040015#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "tools/timer/AnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
Hal Canary455c41a2019-07-03 21:01:52 -040018namespace {
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040019class CameraView : public Sample {
reedfe630452016-03-25 09:08:00 -070020 SkTArray<sk_sp<SkShader>> fShaders;
Hal Canary455c41a2019-07-03 21:01:52 -040021 int fShaderIndex = 0;
22 bool fFrontFace = false;
23 SkScalar fRX = 0;
24 SkScalar fRY = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +000025
Hal Canary455c41a2019-07-03 21:01:52 -040026 SkString name() override { return SkString("Camera"); }
27
28 void onOnceBeforeDraw() override {
29 for (const char* resource : {
30 "images/mandrill_512_q075.jpg",
31 "images/dog.jpg",
32 "images/gamut.png",
33 }) {
reed@android.comf2b98d62010-12-20 18:26:13 +000034 SkBitmap bm;
Hal Canary455c41a2019-07-03 21:01:52 -040035 if (GetResourceAsBitmap(resource, &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);
Mike Reed50acf8f2019-04-08 13:20:23 -040040 fShaders.push_back(bm.makeShader(&matrix));
reed@android.comf2b98d62010-12-20 18:26:13 +000041 }
42 }
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000043 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000045
mtkleinf0599002015-07-13 06:18:39 -070046 void onDrawContent(SkCanvas* canvas) override {
reed@android.comf2b98d62010-12-20 18:26:13 +000047 if (fShaders.count() > 0) {
Hal Canary455c41a2019-07-03 21:01:52 -040048 canvas->translate(this->width()/2, this->height()/2);
49
50 Sk3DView view;
51 view.rotateX(fRX);
52 view.rotateY(fRY);
53 view.applyToCanvas(canvas);
54
reed@android.comf2b98d62010-12-20 18:26:13 +000055 bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0;
56 if (frontFace != fFrontFace) {
57 fFrontFace = frontFace;
58 fShaderIndex = (fShaderIndex + 1) % fShaders.count();
59 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000060
Hal Canary455c41a2019-07-03 21:01:52 -040061 SkPaint paint;
reed@android.comf2b98d62010-12-20 18:26:13 +000062 paint.setAntiAlias(true);
63 paint.setShader(fShaders[fShaderIndex]);
reed93a12152015-03-16 10:08:34 -070064 paint.setFilterQuality(kLow_SkFilterQuality);
reed@android.comf2b98d62010-12-20 18:26:13 +000065 SkRect r = { -150, -150, 150, 150 };
66 canvas->drawRoundRect(r, 30, 30, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 }
reedd9adfe62015-02-01 19:01:04 -080068 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
Mike Kleincd5104e2019-03-20 11:55:08 -050070 bool onAnimate(const AnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -080071 if (timer.isStopped()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 fRY = 0;
reed76113a92015-02-02 12:55:02 -080073 } else {
74 fRY = timer.scaled(90, 360);
reed@android.comf2b98d62010-12-20 18:26:13 +000075 }
reedd9adfe62015-02-01 19:01:04 -080076 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000078};
Hal Canary455c41a2019-07-03 21:01:52 -040079} // namespace
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040080DEF_SAMPLE( return new CameraView(); )