blob: fc9e6838a65b19eba89fd45d0e1c93a5f18fabbc [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
11#include "SkCodec.h"
msarettad8bcfe2016-03-07 07:09:03 -080012#include "SkColorSpace.h"
Hal Canarydb683012016-11-23 08:55:18 -070013#include "SkEncodedImageFormat.h"
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040014#include "SkFrameHolder.h"
scroggo6f5e6192015-06-18 12:53:43 -070015#include "SkImageInfo.h"
16#include "SkTypes.h"
17
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
scroggodb30be22015-12-08 18:54:13 -080026static const size_t WEBP_VP8_HEADER_SIZE = 30;
27
scroggo6f5e6192015-06-18 12:53:43 -070028class SkWebpCodec final : public SkCodec {
29public:
30 // Assumes IsWebp was called and returned true.
31 static SkCodec* NewFromStream(SkStream*);
scroggodb30be22015-12-08 18:54:13 -080032 static bool IsWebp(const void*, size_t);
scroggo6f5e6192015-06-18 12:53:43 -070033protected:
msarette6dd0042015-10-09 11:07:34 -070034 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*, int*)
scroggo6f5e6192015-06-18 12:53:43 -070035 override;
Hal Canarydb683012016-11-23 08:55:18 -070036 SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kWEBP; }
scroggo6f5e6192015-06-18 12:53:43 -070037
scroggo6f5e6192015-06-18 12:53:43 -070038 SkISize onGetScaledDimensions(float desiredScale) const override;
scroggob636b452015-07-22 07:16:20 -070039
scroggoe7fc14b2015-10-02 13:14:46 -070040 bool onDimensionsSupported(const SkISize&) override;
41
scroggob636b452015-07-22 07:16:20 -070042 bool onGetValidSubset(SkIRect* /* desiredSubset */) const override;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040043
44 int onGetFrameCount() override;
45 bool onGetFrameInfo(int, FrameInfo*) const override;
46 int onGetRepetitionCount() override;
47
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -040048 const SkFrameHolder* getFrameHolder() const override {
49 return &fFrameHolder;
50 }
51
scroggo6f5e6192015-06-18 12:53:43 -070052private:
msarettff2a6c82016-09-07 11:23:28 -070053 SkWebpCodec(int width, int height, const SkEncodedInfo&, sk_sp<SkColorSpace>, SkStream*,
54 WebPDemuxer*, sk_sp<SkData>);
55
56 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> fDemux;
57
58 // fDemux has a pointer into this data.
59 // This should not be freed until the decode is completed.
60 sk_sp<SkData> fData;
scroggo6f5e6192015-06-18 12:53:43 -070061
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040062 class Frame : public SkFrame {
63 public:
64 Frame(int i, bool alpha)
65 : INHERITED(i)
66 , fReportsAlpha(alpha)
67 {}
68 Frame(Frame&& other)
69 : INHERITED(other.frameId())
70 , fReportsAlpha(other.fReportsAlpha)
71 {}
72
73 protected:
74 bool onReportsAlpha() const override {
75 return fReportsAlpha;
76 }
77
78 private:
79 const bool fReportsAlpha;
80
81 typedef SkFrame INHERITED;
82 };
83
84 class FrameHolder : public SkFrameHolder {
85 public:
86 ~FrameHolder() override {}
87 void setScreenSize(int w, int h) {
88 fScreenWidth = w;
89 fScreenHeight = h;
90 }
91 Frame* appendNewFrame(bool hasAlpha);
92 const Frame* frame(int i) const;
93 int size() const {
94 return static_cast<int>(fFrames.size());
95 }
96 void reserve(int size) {
97 fFrames.reserve(size);
98 }
99
100 protected:
101 const SkFrame* onGetFrame(int i) const override;
102
103 private:
104 std::vector<Frame> fFrames;
105 };
106
107 FrameHolder fFrameHolder;
108 // Set to true if WebPDemuxGetFrame fails. This only means
109 // that we will cap the frame count to the frames that
110 // succeed.
111 bool fFailed;
112
scroggo6f5e6192015-06-18 12:53:43 -0700113 typedef SkCodec INHERITED;
114};
115#endif // SkWebpCodec_DEFINED