blob: c9bb1c96aa87a12bda52ef338b3a07f2daf32e21 [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 Reede78f8ce2020-12-19 15:14:06 -05009#include "include/core/SkImage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkShader.h"
11#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/utils/SkCamera.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "samplecode/DecodeFile.h"
14#include "samplecode/Sample.h"
15#include "src/effects/SkEmbossMaskFilter.h"
Hal Canary455c41a2019-07-03 21:01:52 -040016#include "tools/Resources.h"
Hal Canary41248072019-07-11 16:32:53 -040017#include "tools/timer/TimeUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
Hal Canary455c41a2019-07-03 21:01:52 -040019namespace {
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040020class CameraView : public Sample {
reedfe630452016-03-25 09:08:00 -070021 SkTArray<sk_sp<SkShader>> fShaders;
Hal Canary455c41a2019-07-03 21:01:52 -040022 int fShaderIndex = 0;
23 bool fFrontFace = false;
24 SkScalar fRX = 0;
25 SkScalar fRY = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +000026
Hal Canary455c41a2019-07-03 21:01:52 -040027 SkString name() override { return SkString("Camera"); }
28
29 void onOnceBeforeDraw() override {
30 for (const char* resource : {
31 "images/mandrill_512_q075.jpg",
32 "images/dog.jpg",
33 "images/gamut.png",
34 }) {
reed@android.comf2b98d62010-12-20 18:26:13 +000035 SkBitmap bm;
Hal Canary455c41a2019-07-03 21:01:52 -040036 if (GetResourceAsBitmap(resource, &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);
Mike Reed82abece2020-12-12 09:51:11 -050041 fShaders.push_back(bm.makeShader(SkSamplingOptions(SkFilterMode::kLinear,
42 SkMipmapMode::kNone),
43 matrix));
reed@android.comf2b98d62010-12-20 18:26:13 +000044 }
45 }
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000046 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000048
mtkleinf0599002015-07-13 06:18:39 -070049 void onDrawContent(SkCanvas* canvas) override {
reed@android.comf2b98d62010-12-20 18:26:13 +000050 if (fShaders.count() > 0) {
Hal Canary455c41a2019-07-03 21:01:52 -040051 canvas->translate(this->width()/2, this->height()/2);
52
53 Sk3DView view;
54 view.rotateX(fRX);
55 view.rotateY(fRY);
56 view.applyToCanvas(canvas);
57
reed@android.comf2b98d62010-12-20 18:26:13 +000058 bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0;
59 if (frontFace != fFrontFace) {
60 fFrontFace = frontFace;
61 fShaderIndex = (fShaderIndex + 1) % fShaders.count();
62 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000063
Hal Canary455c41a2019-07-03 21:01:52 -040064 SkPaint paint;
reed@android.comf2b98d62010-12-20 18:26:13 +000065 paint.setAntiAlias(true);
66 paint.setShader(fShaders[fShaderIndex]);
reed@android.comf2b98d62010-12-20 18:26:13 +000067 SkRect r = { -150, -150, 150, 150 };
68 canvas->drawRoundRect(r, 30, 30, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 }
reedd9adfe62015-02-01 19:01:04 -080070 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
Hal Canary41248072019-07-11 16:32:53 -040072 bool onAnimate(double nanos) override {
73 fRY = nanos ? TimeUtils::Scaled(1e-9 * nanos, 90, 360) : 0;
reedd9adfe62015-02-01 19:01:04 -080074 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000076};
Hal Canary455c41a2019-07-03 21:01:52 -040077} // namespace
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040078DEF_SAMPLE( return new CameraView(); )