scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #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" |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 17 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 20 | class SkStream; |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 21 | extern "C" { |
| 22 | struct WebPDemuxer; |
| 23 | void WebPDemuxDelete(WebPDemuxer* dmux); |
| 24 | } |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 25 | |
Leon Scroggins III | 70d8f4f | 2019-04-01 12:57:46 -0400 | [diff] [blame] | 26 | class SkWebpCodec final : public SkScalingCodec { |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 27 | public: |
| 28 | // Assumes IsWebp was called and returned true. |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 29 | static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 30 | static bool IsWebp(const void*, size_t); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 31 | protected: |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 32 | Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override; |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 33 | SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kWEBP; } |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 34 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 35 | bool onGetValidSubset(SkIRect* /* desiredSubset */) const override; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 36 | |
| 37 | int onGetFrameCount() override; |
| 38 | bool onGetFrameInfo(int, FrameInfo*) const override; |
| 39 | int onGetRepetitionCount() override; |
| 40 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 41 | const SkFrameHolder* getFrameHolder() const override { |
| 42 | return &fFrameHolder; |
| 43 | } |
| 44 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 45 | private: |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 46 | SkWebpCodec(SkEncodedInfo&&, std::unique_ptr<SkStream>, WebPDemuxer*, sk_sp<SkData>, |
| 47 | SkEncodedOrigin); |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 48 | |
| 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; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 54 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 55 | class Frame : public SkFrame { |
| 56 | public: |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 57 | Frame(int i, SkEncodedInfo::Alpha alpha) |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 58 | : INHERITED(i) |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 59 | , fReportedAlpha(alpha) |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 60 | {} |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 61 | |
| 62 | protected: |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 63 | SkEncodedInfo::Alpha onReportedAlpha() const override { |
| 64 | return fReportedAlpha; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private: |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 68 | const SkEncodedInfo::Alpha fReportedAlpha; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 69 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 70 | using INHERITED = SkFrame; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 71 | }; |
| 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 Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 102 | using INHERITED = SkScalingCodec; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 103 | }; |
| 104 | #endif // SkWebpCodec_DEFINED |