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
|
2018-01-30 00:35:55 +00:00
|
|
|
* decode the first frame.
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the animation to the beginning.
|
|
|
|
*/
|
|
|
|
void reset();
|
|
|
|
|
2018-01-21 20:50:12 +00:00
|
|
|
/**
|
|
|
|
* Whether the animation completed.
|
|
|
|
*
|
|
|
|
* Returns true after all repetitions are complete, or an error stops the
|
|
|
|
* animation. Gets reset to false if the animation is restarted.
|
|
|
|
*/
|
|
|
|
bool isFinished() const { return fFinished; }
|
|
|
|
|
2018-01-22 17:31:21 +00:00
|
|
|
/**
|
2018-01-30 00:35:55 +00:00
|
|
|
* Returned by decodeNextFrame and currentFrameDuration if the animation
|
|
|
|
* is not running.
|
2018-01-22 17:31:21 +00:00
|
|
|
*/
|
2018-01-30 00:35:55 +00:00
|
|
|
static constexpr int kFinished = -1;
|
2018-01-22 17:31:21 +00:00
|
|
|
|
2018-01-12 16:24:30 +00:00
|
|
|
/**
|
2018-01-30 00:35:55 +00:00
|
|
|
* Decode the next frame.
|
2018-01-12 16:24:30 +00:00
|
|
|
*
|
2018-01-30 00:35:55 +00:00
|
|
|
* If the animation is on the last frame or has hit an error, returns
|
|
|
|
* kFinished.
|
2018-01-12 16:24:30 +00:00
|
|
|
*/
|
2018-01-30 00:35:55 +00:00
|
|
|
int decodeNextFrame();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How long to display the current frame.
|
|
|
|
*
|
|
|
|
* Useful for the first frame, for which decodeNextFrame is called
|
|
|
|
* internally.
|
|
|
|
*/
|
|
|
|
int currentFrameDuration() {
|
|
|
|
return fCurrentFrameDuration;
|
|
|
|
}
|
2018-01-12 16:24:30 +00:00
|
|
|
|
2018-01-20 15:33:24 +00:00
|
|
|
/**
|
|
|
|
* Change the repetition count.
|
|
|
|
*
|
|
|
|
* By default, the image will repeat the number of times indicated in the
|
|
|
|
* encoded data.
|
|
|
|
*
|
|
|
|
* Use SkCodec::kRepetitionCountInfinite for infinite, and 0 to show all
|
|
|
|
* frames once and then stop.
|
|
|
|
*/
|
|
|
|
void setRepetitionCount(int count);
|
|
|
|
|
2018-01-26 16:06:12 +00:00
|
|
|
/**
|
|
|
|
* Return the currently set repetition count.
|
|
|
|
*/
|
|
|
|
int getRepetitionCount() const {
|
|
|
|
return fRepetitionCount;
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:24:30 +00:00
|
|
|
protected:
|
|
|
|
SkRect onGetBounds() override;
|
|
|
|
void onDraw(SkCanvas*) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Frame {
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
int fIndex;
|
|
|
|
SkCodecAnimation::DisposalMethod fDisposalMethod;
|
|
|
|
|
2018-05-23 20:15:09 +00:00
|
|
|
// init() may have to create a new SkPixelRef, if the
|
|
|
|
// current one is already in use by another owner (e.g.
|
|
|
|
// an SkPicture). This determines whether to copy the
|
|
|
|
// existing one to the new one.
|
|
|
|
enum class OnInit {
|
|
|
|
// Restore the image from the old SkPixelRef to the
|
|
|
|
// new one.
|
|
|
|
kRestoreIfNecessary,
|
|
|
|
// No need to restore.
|
|
|
|
kNoRestore,
|
|
|
|
};
|
|
|
|
|
2018-01-12 16:24:30 +00:00
|
|
|
Frame();
|
2018-05-23 20:15:09 +00:00
|
|
|
bool init(const SkImageInfo& info, OnInit);
|
2018-01-12 16:24:30 +00:00
|
|
|
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;
|
2018-01-20 15:33:24 +00:00
|
|
|
const int fFrameCount;
|
2018-01-16 20:26:35 +00:00
|
|
|
const bool fSimple; // no crop, scale, or postprocess
|
|
|
|
SkMatrix fMatrix; // used only if !fSimple
|
|
|
|
|
2018-01-14 19:46:51 +00:00
|
|
|
bool fFinished;
|
2018-01-30 00:35:55 +00:00
|
|
|
int fCurrentFrameDuration;
|
2018-05-23 20:15:09 +00:00
|
|
|
Frame fDisplayFrame;
|
|
|
|
Frame fDecodingFrame;
|
2018-01-14 19:46:51 +00:00
|
|
|
Frame fRestoreFrame;
|
2018-01-20 15:33:24 +00:00
|
|
|
int fRepetitionCount;
|
|
|
|
int fRepetitionsCompleted;
|
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-20 15:33:24 +00:00
|
|
|
SkAnimatedImage(std::unique_ptr<SkAndroidCodec>);
|
|
|
|
|
|
|
|
int computeNextFrame(int current, bool* animationEnded);
|
|
|
|
double finish();
|
2018-01-12 16:24:30 +00:00
|
|
|
|
|
|
|
typedef SkDrawable INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SkAnimatedImage_DEFINED
|