blob: 213ff894c3ba67d95c9fe44e4e64e548e2c08c71 [file] [log] [blame]
scroggo6f5e6192015-06-18 12:53:43 -07001/*
2 * Copyright 2015 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
8#ifndef SkWebpCodec_DEFINED
9#define SkWebpCodec_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/codec/SkCodec.h"
12#include "include/core/SkEncodedImageFormat.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkTypes.h"
15#include "src/codec/SkFrameHolder.h"
16#include "src/codec/SkScalingCodec.h"
scroggo6f5e6192015-06-18 12:53:43 -070017
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040018#include <vector>
19
scroggo6f5e6192015-06-18 12:53:43 -070020class SkStream;
msarettff2a6c82016-09-07 11:23:28 -070021extern "C" {
22 struct WebPDemuxer;
23 void WebPDemuxDelete(WebPDemuxer* dmux);
24}
scroggo6f5e6192015-06-18 12:53:43 -070025
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040026class SkWebpCodec final : public SkScalingCodec {
scroggo6f5e6192015-06-18 12:53:43 -070027public:
28 // Assumes IsWebp was called and returned true.
Mike Reedede7bac2017-07-23 15:30:02 -040029 static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
scroggodb30be22015-12-08 18:54:13 -080030 static bool IsWebp(const void*, size_t);
scroggo6f5e6192015-06-18 12:53:43 -070031protected:
Leon Scroggins571b30f2017-07-11 17:35:31 +000032 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
Hal Canarydb683012016-11-23 08:55:18 -070033 SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kWEBP; }
scroggo6f5e6192015-06-18 12:53:43 -070034
scroggob636b452015-07-22 07:16:20 -070035 bool onGetValidSubset(SkIRect* /* desiredSubset */) const override;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040036
37 int onGetFrameCount() override;
38 bool onGetFrameInfo(int, FrameInfo*) const override;
39 int onGetRepetitionCount() override;
40
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -040041 const SkFrameHolder* getFrameHolder() const override {
42 return &fFrameHolder;
43 }
44
scroggo6f5e6192015-06-18 12:53:43 -070045private:
Leon Scroggins III36f7e322018-08-27 11:55:46 -040046 SkWebpCodec(SkEncodedInfo&&, std::unique_ptr<SkStream>, WebPDemuxer*, sk_sp<SkData>,
47 SkEncodedOrigin);
msarettff2a6c82016-09-07 11:23:28 -070048
49 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> fDemux;
50
51 // fDemux has a pointer into this data.
52 // This should not be freed until the decode is completed.
53 sk_sp<SkData> fData;
scroggo6f5e6192015-06-18 12:53:43 -070054
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040055 class Frame : public SkFrame {
56 public:
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050057 Frame(int i, SkEncodedInfo::Alpha alpha)
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040058 : INHERITED(i)
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050059 , fReportedAlpha(alpha)
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040060 {}
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040061
62 protected:
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050063 SkEncodedInfo::Alpha onReportedAlpha() const override {
64 return fReportedAlpha;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040065 }
66
67 private:
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -050068 const SkEncodedInfo::Alpha fReportedAlpha;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040069
John Stiles7571f9e2020-09-02 22:42:33 -040070 using INHERITED = SkFrame;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040071 };
72
73 class FrameHolder : public SkFrameHolder {
74 public:
75 ~FrameHolder() override {}
76 void setScreenSize(int w, int h) {
77 fScreenWidth = w;
78 fScreenHeight = h;
79 }
80 Frame* appendNewFrame(bool hasAlpha);
81 const Frame* frame(int i) const;
82 int size() const {
83 return static_cast<int>(fFrames.size());
84 }
85 void reserve(int size) {
86 fFrames.reserve(size);
87 }
88
89 protected:
90 const SkFrame* onGetFrame(int i) const override;
91
92 private:
93 std::vector<Frame> fFrames;
94 };
95
96 FrameHolder fFrameHolder;
97 // Set to true if WebPDemuxGetFrame fails. This only means
98 // that we will cap the frame count to the frames that
99 // succeed.
100 bool fFailed;
101
John Stiles7571f9e2020-09-02 22:42:33 -0400102 using INHERITED = SkScalingCodec;
scroggo6f5e6192015-06-18 12:53:43 -0700103};
104#endif // SkWebpCodec_DEFINED