Flutter Engine
The Flutter Engine
CameraSlide.cpp
Go to the documentation of this file.
1/*
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 */
7
16#include "tools/DecodeUtils.h"
17#include "tools/Resources.h"
19#include "tools/viewer/Slide.h"
20
21using namespace skia_private;
22
23namespace {
24class CameraSlide : public Slide {
25 TArray<sk_sp<SkShader>> fShaders;
26 int fShaderIndex = 0;
27 bool fFrontFace = false;
28 SkScalar fRX = 0;
29 SkScalar fRY = 0;
30 SkSize fSize;
31
32public:
33 CameraSlide() { fName = "Camera"; }
34
35 void load(SkScalar w, SkScalar h) override {
36 fSize = {w, h};
37 for (const char* resource : {
38 "images/mandrill_512_q075.jpg",
39 "images/dog.jpg",
40 "images/gamut.png",
41 }) {
42 SkBitmap bm;
44 SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
45 SkRect dst = { -150, -150, 150, 150 };
48 }
49 }
50 }
51
52 void draw(SkCanvas* canvas) override {
53 canvas->clear(0xFFDDDDDD);
54 if (fShaders.size() > 0) {
55 canvas->translate(fSize.width()/2, fSize.height()/2);
56
57 Sk3DView view;
58 view.rotateX(fRX);
59 view.rotateY(fRY);
60 view.applyToCanvas(canvas);
61
62 bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0;
63 if (frontFace != fFrontFace) {
64 fFrontFace = frontFace;
65 fShaderIndex = (fShaderIndex + 1) % fShaders.size();
66 }
67
69 paint.setAntiAlias(true);
70 paint.setShader(fShaders[fShaderIndex]);
71 SkRect r = { -150, -150, 150, 150 };
72 canvas->drawRoundRect(r, 30, 30, paint);
73 }
74 }
75
76 void resize(SkScalar w, SkScalar h) override { fSize = {w, h}; }
77
78 bool animate(double nanos) override {
79 fRY = nanos ? TimeUtils::Scaled(1e-9 * nanos, 90, 360) : 0;
80 return true;
81 }
82};
83} // namespace
84DEF_SLIDE( return new CameraSlide(); )
const char * fName
static SkString resource(SkPDFResourceType type, int index)
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define DEF_SLIDE(code)
Definition: Slide.h:25
SI T load(const P *ptr)
Definition: Transform_inl.h:98
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
void applyToCanvas(SkCanvas *) const
Definition: SkCamera.cpp:234
SkScalar dotWithNormal(SkScalar dx, SkScalar dy, SkScalar dz) const
Definition: SkCamera.cpp:218
void rotateX(SkScalar deg)
Definition: SkCamera.cpp:206
void rotateY(SkScalar deg)
Definition: SkCamera.cpp:210
int width() const
Definition: SkBitmap.h:149
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkBitmap.cpp:669
int height() const
Definition: SkBitmap.h:158
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawRoundRect(const SkRect &rect, SkScalar rx, SkScalar ry, const SkPaint &paint)
Definition: SkCanvas.cpp:2717
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition: SkMatrix.h:157
Definition: Slide.h:29
virtual void resize(SkScalar winWidth, SkScalar winHeight)
Definition: Slide.h:41
virtual bool animate(double nanos)
Definition: Slide.h:39
int size() const
Definition: SkTArray.h:421
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
static float Scaled(float time, float speed, float period=0)
Definition: TimeUtils.h:27
bool GetResourceAsBitmap(const char *resource, SkBitmap *dst)
Definition: DecodeUtils.h:21
dst
Definition: cp.py:12
SkScalar w
SkScalar h
Definition: SkSize.h:52