Flutter Engine
The Flutter Engine
AnimatedImageSlide.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 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
11#include "include/core/SkFont.h"
14#include "include/core/SkRect.h"
17#include "tools/Resources.h"
20#include "tools/viewer/Slide.h"
21
22static constexpr char kPauseKey = 'p';
23static constexpr char kResetKey = 'r';
24
25class AnimatedImageSlide : public Slide {
27 sk_sp<SkDrawable> fDrawable;
28 SkScalar fYOffset = 0;
29 bool fRunning = false;
30 double fCurrentTime = 0.0;
31 double fLastWallTime = 0.0;
32 double fTimeToShowNextFrame = 0.0;
33
34public:
35 AnimatedImageSlide() { fName = "AnimatedImage"; }
36
37 void draw(SkCanvas* canvas) override {
39 font.setSize(20);
40
41 SkString str = SkStringPrintf("Press '%c' to start/pause; '%c' to reset.",
43 const char* text = str.c_str();
45 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
46 fYOffset = bounds.height();
47
48 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 5, fYOffset, font, SkPaint());
49 fYOffset *= 2;
50
51 if (!fImage) {
52 return;
53 }
54
55 canvas->translate(0, fYOffset);
56
57 canvas->drawDrawable(fImage.get());
58 canvas->drawDrawable(fDrawable.get(), fImage->getBounds().width(), 0);
59 }
60
61 bool animate(double nanos) override {
62 if (!fImage) {
63 return false;
64 }
65
66 const double lastWallTime = fLastWallTime;
67 fLastWallTime = TimeUtils::NanosToMSec(nanos);
68
69 if (fRunning) {
70 fCurrentTime += fLastWallTime - lastWallTime;
71 if (fCurrentTime > fTimeToShowNextFrame) {
72 fTimeToShowNextFrame += fImage->decodeNextFrame();
73 if (fImage->isFinished()) {
74 fRunning = false;
75 }
76 }
77 }
78
79 return true;
80 }
81
82 void load(SkScalar w, SkScalar h) override {
83 sk_sp<SkData> file(GetResourceAsData("images/alphabetAnim.gif"));
84 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(file));
85 if (!codec) {
86 return;
87 }
88
89 fImage = SkAnimatedImage::Make(SkAndroidCodec::MakeFromCodec(std::move(codec)));
90 if (!fImage) {
91 return;
92 }
93
94 fTimeToShowNextFrame = fImage->currentFrameDuration();
95 SkPictureRecorder recorder;
96 auto canvas = recorder.beginRecording(fImage->getBounds());
97 canvas->drawDrawable(fImage.get());
98 fDrawable = recorder.finishRecordingAsDrawable();
99 }
100
101 bool onChar(SkUnichar uni) override {
102 if (fImage) {
103 switch (uni) {
104 case kPauseKey:
105 fRunning = !fRunning;
106 if (!fImage->isFinished()) {
107 return true;
108 }
109 [[fallthrough]];
110 case kResetKey:
111 fImage->reset();
112 fCurrentTime = fLastWallTime;
113 fTimeToShowNextFrame = fCurrentTime + fImage->currentFrameDuration();
114 return true;
115 default:
116 break;
117 }
118 }
119 return false;
120 }
121};
122
123DEF_SLIDE( return new AnimatedImageSlide(); )
static constexpr char kPauseKey
static constexpr char kResetKey
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition: Resources.cpp:42
@ kUTF8
uses bytes to represent UTF-8 or ASCII
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
int32_t SkUnichar
Definition: SkTypes.h:175
#define DEF_SLIDE(code)
Definition: Slide.h:25
void draw(SkCanvas *canvas) override
bool onChar(SkUnichar uni) override
void load(SkScalar w, SkScalar h) override
bool animate(double nanos) override
static std::unique_ptr< SkAndroidCodec > MakeFromCodec(std::unique_ptr< SkCodec >)
static sk_sp< SkAnimatedImage > Make(std::unique_ptr< SkAndroidCodec >, const SkImageInfo &info, SkIRect cropRect, sk_sp< SkPicture > postProcess)
bool isFinished() const
int currentFrameDuration()
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawDrawable(SkDrawable *drawable, const SkMatrix *matrix=nullptr)
Definition: SkCanvas.cpp:2574
static std::unique_ptr< SkCodec > MakeFromData(sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
Definition: SkCodec.cpp:241
SkRect getBounds()
Definition: SkDrawable.cpp:71
Definition: SkFont.h:35
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkDrawable > finishRecordingAsDrawable()
const char * c_str() const
Definition: SkString.h:133
Definition: Slide.h:29
SkString fName
Definition: Slide.h:54
T * get() const
Definition: SkRefCnt.h:303
float SkScalar
Definition: extension.cpp:12
std::u16string text
Optional< SkRect > bounds
Definition: SkRecords.h:189
static SkMSec NanosToMSec(double nanos)
Definition: TimeUtils.h:16
SkFont DefaultFont()
font
Font Metadata and Metrics.
SkScalar w
SkScalar h
constexpr float width() const
Definition: SkRect.h:762