2018-01-12 16:24:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkAnimatedImage_DEFINED
|
|
|
|
#define SkAnimatedImage_DEFINED
|
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCodecAnimation.h"
|
|
|
|
#include "SkDrawable.h"
|
2018-01-16 20:26:35 +00:00
|
|
|
#include "SkMatrix.h"
|
|
|
|
#include "SkRect.h"
|
2018-01-12 16:24:30 +00:00
|
|
|
|
2018-01-14 19:46:51 +00:00
|
|
|
class SkAndroidCodec;
|
2018-01-16 20:26:35 +00:00
|
|
|
class SkPicture;
|
2018-01-12 16:24:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Thread unsafe drawable for drawing animated images (e.g. GIF).
|
|
|
|
*/
|
|
|
|
class SK_API SkAnimatedImage : public SkDrawable {
|
|
|
|
public:
|
|
|
|
/**
|
2018-01-14 19:46:51 +00:00
|
|
|
* Create an SkAnimatedImage from the SkAndroidCodec.
|
2018-01-12 16:24:30 +00:00
|
|
|
*
|
|
|
|
* Returns null on failure to allocate pixels. On success, this will
|
|
|
|
* decode the first frame. It will not animate until start() is called.
|
2018-01-16 20:26:35 +00:00
|
|
|
*
|
|
|
|
* @param scaledSize Size to draw the image, possibly requiring scaling.
|
|
|
|
* @param cropRect Rectangle to crop to after scaling.
|
|
|
|
* @param postProcess Picture to apply after scaling and cropping.
|
|
|
|
*/
|
|
|
|
static sk_sp<SkAnimatedImage> Make(std::unique_ptr<SkAndroidCodec>,
|
|
|
|
SkISize scaledSize, SkIRect cropRect, sk_sp<SkPicture> postProcess);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simpler version that uses the default size, no cropping, and no postProcess.
|
2018-01-12 16:24:30 +00:00
|
|
|
*/
|
2018-01-14 19:46:51 +00:00
|
|
|
static sk_sp<SkAnimatedImage> Make(std::unique_ptr<SkAndroidCodec>);
|
2018-01-12 16:24:30 +00:00
|
|
|
|
|
|
|
~SkAnimatedImage() override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start or resume the animation. update() must be called to advance the
|
|
|
|
* time.
|
|
|
|
*/
|
|
|
|
void start();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the animation. update() has no effect while the animation is
|
|
|
|
* stopped.
|
|
|
|
*/
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the animation to the beginning.
|
|
|
|
*/
|
|
|
|
void reset();
|
|
|
|
|
2018-01-16 20:26:35 +00:00
|
|
|
/**
|
|
|
|
* Whether the animation is active.
|
|
|
|
*
|
|
|
|
* If true, update() can be called to animate.
|
|
|
|
*/
|
|
|
|
bool isRunning() const { return fRunning && !fFinished; }
|
|
|
|
|
2018-01-12 16:24:30 +00:00
|
|
|
/**
|
|
|
|
* Update the current time. If the image is animating, this may decode
|
|
|
|
* a new frame.
|
|
|
|
*
|
|
|
|
* @return the time to show the next frame.
|
|
|
|
* Returns numeric_limits<double>::max() if there is no max frame to
|
|
|
|
* show, and -1.0 if the animation is not running.
|
|
|
|
*/
|
|
|
|
double update(double msecs);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkRect onGetBounds() override;
|
|
|
|
void onDraw(SkCanvas*) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Frame {
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
int fIndex;
|
|
|
|
SkCodecAnimation::DisposalMethod fDisposalMethod;
|
|
|
|
|
|
|
|
Frame();
|
|
|
|
bool copyTo(Frame*) const;
|
|
|
|
};
|
|
|
|
|
2018-01-14 19:46:51 +00:00
|
|
|
std::unique_ptr<SkAndroidCodec> fCodec;
|
2018-01-16 20:26:35 +00:00
|
|
|
const SkISize fScaledSize;
|
|
|
|
const SkImageInfo fDecodeInfo;
|
|
|
|
const SkIRect fCropRect;
|
|
|
|
const sk_sp<SkPicture> fPostProcess;
|
|
|
|
const bool fSimple; // no crop, scale, or postprocess
|
|
|
|
SkMatrix fMatrix; // used only if !fSimple
|
|
|
|
|
2018-01-14 19:46:51 +00:00
|
|
|
bool fFinished;
|
|
|
|
bool fRunning;
|
|
|
|
double fNowMS;
|
|
|
|
double fRemainingMS;
|
|
|
|
Frame fActiveFrame;
|
|
|
|
Frame fRestoreFrame;
|
2018-01-12 16:24:30 +00:00
|
|
|
|
2018-01-16 20:26:35 +00:00
|
|
|
SkAnimatedImage(std::unique_ptr<SkAndroidCodec>, SkISize scaledSize,
|
|
|
|
SkImageInfo decodeInfo, SkIRect cropRect, sk_sp<SkPicture> postProcess);
|
2018-01-12 16:24:30 +00:00
|
|
|
|
|
|
|
typedef SkDrawable INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SkAnimatedImage_DEFINED
|