OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 11 matching lines...) Expand all Loading... |
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | 28 |
29 #ifndef WEBPImageDecoder_h | 29 #ifndef WEBPImageDecoder_h |
30 #define WEBPImageDecoder_h | 30 #define WEBPImageDecoder_h |
31 | 31 |
| 32 #include <memory> |
32 #include "platform/image-decoders/ImageDecoder.h" | 33 #include "platform/image-decoders/ImageDecoder.h" |
33 #include "webp/decode.h" | 34 #include "platform/wtf/Noncopyable.h" |
34 #include "webp/demux.h" | 35 #include "platform/wtf/RefPtr.h" |
35 | 36 #include "third_party/skia/include/codec/SkCodec.h" |
36 class SkData; | |
37 | 37 |
38 namespace blink { | 38 namespace blink { |
39 | 39 |
| 40 class SegmentStream; |
| 41 |
40 class PLATFORM_EXPORT WEBPImageDecoder final : public ImageDecoder { | 42 class PLATFORM_EXPORT WEBPImageDecoder final : public ImageDecoder { |
41 WTF_MAKE_NONCOPYABLE(WEBPImageDecoder); | 43 WTF_MAKE_NONCOPYABLE(WEBPImageDecoder); |
42 | 44 |
43 public: | 45 public: |
44 WEBPImageDecoder(AlphaOption, const ColorBehavior&, size_t max_decoded_bytes); | 46 WEBPImageDecoder(AlphaOption, const ColorBehavior&, size_t max_decoded_bytes); |
| 47 WEBPImageDecoder(WEBPImageDecoder&&) = default; |
| 48 WEBPImageDecoder& operator=(WEBPImageDecoder&&) = default; |
45 ~WEBPImageDecoder() override; | 49 ~WEBPImageDecoder() override; |
46 | 50 |
47 // ImageDecoder: | 51 // ImageDecoder: |
48 String FilenameExtension() const override { return "webp"; } | 52 String FilenameExtension() const override { return "webp"; } |
49 void OnSetData(SegmentReader* data) override; | 53 void OnSetData(SegmentReader* data) override; |
50 int RepetitionCount() const override; | 54 int RepetitionCount() const override; |
51 bool FrameIsReceivedAtIndex(size_t) const override; | 55 bool FrameIsReceivedAtIndex(size_t) const override; |
52 float FrameDurationAtIndex(size_t) const override; | 56 float FrameDurationAtIndex(size_t) const override; |
| 57 // CAUTION: SetFailed() deletes |codec_|. Be careful to avoid |
| 58 // accessing deleted memory. |
| 59 bool SetFailed() override; |
| 60 |
| 61 size_t ClearCacheExceptFrame(size_t) override; |
53 | 62 |
54 private: | 63 private: |
55 // ImageDecoder: | 64 // ImageDecoder: |
56 virtual void DecodeSize() { UpdateDemuxer(); } | 65 void DecodeSize() override {} |
57 size_t DecodeFrameCount() override; | 66 size_t DecodeFrameCount() override; |
58 void InitializeNewFrame(size_t) override; | 67 void InitializeNewFrame(size_t) override; |
59 void Decode(size_t) override; | 68 void Decode(size_t) override; |
60 | 69 |
61 bool DecodeSingleFrame(const uint8_t* data_bytes, | |
62 size_t data_size, | |
63 size_t frame_index); | |
64 | |
65 // For WebP images, the frame status needs to be FrameComplete to decode | 70 // For WebP images, the frame status needs to be FrameComplete to decode |
66 // subsequent frames that depend on frame |index|. The reason for this is that | 71 // subsequent frames that depend on frame |index|. The reason for this is that |
67 // WebP uses the previous frame for alpha blending, in ApplyPostProcessing(). | 72 // WebP uses the previous frame for alpha blending. |
68 // | 73 // |
69 // Before calling this, verify that frame |index| exists by checking that | 74 // Before calling this, verify that frame |index| exists by checking that |
70 // |index| is smaller than |frame_buffer_cache_|.size(). | 75 // |index| is smaller than |frame_buffer_cache_|.size(). |
71 bool FrameStatusSufficientForSuccessors(size_t index) override { | 76 bool FrameStatusSufficientForSuccessors(size_t index) override { |
72 DCHECK(index < frame_buffer_cache_.size()); | 77 DCHECK(index < frame_buffer_cache_.size()); |
73 return frame_buffer_cache_[index].GetStatus() == ImageFrame::kFrameComplete; | 78 return frame_buffer_cache_[index].GetStatus() == ImageFrame::kFrameComplete; |
74 } | 79 } |
75 | 80 |
76 WebPIDecoder* decoder_; | 81 // When a frame depends on a previous frame's content, there is a list of |
77 WebPDecBuffer decoder_buffer_; | 82 // candidate reference frames. This function will find a previous frame from |
78 int format_flags_; | 83 // that list which satisfies the requirements of being a reference frame. |
79 bool frame_background_has_alpha_; | 84 size_t GetViableReferenceFrameIndex(size_t) const; |
80 | 85 |
81 void ReadColorProfile(); | 86 std::unique_ptr<SkCodec> codec_; |
82 bool UpdateDemuxer(); | 87 // |codec_| owns the SegmentStream, but we need access to it to append more |
83 | 88 // data as it arrives. |
84 // Set |frame_background_has_alpha_| based on this frame's characteristics. | 89 SegmentStream* segment_stream_; |
85 // Before calling this method, the caller must verify that the frame exists. | 90 mutable int repetition_count_ = kAnimationLoopOnce; |
86 void OnInitFrameBuffer(size_t frame_index) override; | |
87 | |
88 // When the blending method of this frame is BlendAtopPreviousFrame, the | |
89 // previous frame's buffer is necessary to decode this frame in | |
90 // ApplyPostProcessing, so we can't take over the data. Before calling this | |
91 // method, the caller must verify that the frame exists. | |
92 bool CanReusePreviousFrameBuffer(size_t frame_index) const override; | |
93 | |
94 void ApplyPostProcessing(size_t frame_index); | |
95 void ClearFrameBuffer(size_t frame_index) override; | |
96 | |
97 WebPDemuxer* demux_; | |
98 WebPDemuxState demux_state_; | |
99 bool have_already_parsed_this_data_; | |
100 int repetition_count_; | |
101 int decoded_height_; | |
102 | |
103 typedef void (*AlphaBlendFunction)(ImageFrame&, ImageFrame&, int, int, int); | |
104 AlphaBlendFunction blend_function_; | |
105 | |
106 void Clear(); | |
107 void ClearDecoder(); | |
108 | |
109 // FIXME: Update libwebp's API so it does not require copying the data on each | |
110 // update. | |
111 sk_sp<SkData> consolidated_data_; | |
112 }; | 91 }; |
113 | 92 |
114 } // namespace blink | 93 } // namespace blink |
115 | 94 |
116 #endif | 95 #endif |
OLD | NEW |