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 | |
| 11 | #include "SkCodec.h" |
msarett | ad8bcfe | 2016-03-07 07:09:03 -0800 | [diff] [blame] | 12 | #include "SkColorSpace.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 13 | #include "SkEncodedImageFormat.h" |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 14 | #include "SkFrameHolder.h" |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 15 | #include "SkImageInfo.h" |
| 16 | #include "SkTypes.h" |
| 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 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 26 | static const size_t WEBP_VP8_HEADER_SIZE = 30; |
| 27 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 28 | class SkWebpCodec final : public SkCodec { |
| 29 | public: |
| 30 | // Assumes IsWebp was called and returned true. |
| 31 | static SkCodec* NewFromStream(SkStream*); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 32 | static bool IsWebp(const void*, size_t); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 33 | protected: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 34 | Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*, int*) |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 35 | override; |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 36 | SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kWEBP; } |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 37 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 38 | SkISize onGetScaledDimensions(float desiredScale) const override; |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 39 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 40 | bool onDimensionsSupported(const SkISize&) override; |
| 41 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 42 | bool onGetValidSubset(SkIRect* /* desiredSubset */) const override; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 43 | |
| 44 | int onGetFrameCount() override; |
| 45 | bool onGetFrameInfo(int, FrameInfo*) const override; |
| 46 | int onGetRepetitionCount() override; |
| 47 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame^] | 48 | const SkFrameHolder* getFrameHolder() const override { |
| 49 | return &fFrameHolder; |
| 50 | } |
| 51 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 52 | private: |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 53 | 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; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 61 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 62 | 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 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 113 | typedef SkCodec INHERITED; |
| 114 | }; |
| 115 | #endif // SkWebpCodec_DEFINED |