1 /*
2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_IMAGE_DECODERS_GIF_GIF_IMAGE_DECODER_H_
27 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_IMAGE_DECODERS_GIF_GIF_IMAGE_DECODER_H_
28 
29 #include <memory>
30 
31 #include "base/macros.h"
32 #include "third_party/blink/renderer/platform/image-decoders/image_decoder.h"
33 
34 #include "third_party/skia/include/codec/SkCodec.h"
35 
36 namespace blink {
37 
38 class SegmentStream;
39 
40 // This class decodes the GIF image format.
41 class PLATFORM_EXPORT GIFImageDecoder final : public ImageDecoder {
42  public:
43   GIFImageDecoder(AlphaOption, const ColorBehavior&, size_t max_decoded_bytes);
44   ~GIFImageDecoder() override;
45 
46   // ImageDecoder:
FilenameExtension()47   String FilenameExtension() const override { return "gif"; }
48   void OnSetData(SegmentReader* data) override;
49   int RepetitionCount() const override;
50   bool FrameIsReceivedAtIndex(size_t) const override;
51   base::TimeDelta FrameDurationAtIndex(size_t) const override;
52   // CAUTION: SetFailed() deletes |codec_|.  Be careful to avoid
53   // accessing deleted memory.
54   bool SetFailed() override;
55 
56   size_t ClearCacheExceptFrame(size_t) override;
57 
58  private:
59   // ImageDecoder:
DecodeSize()60   void DecodeSize() override {}
61   size_t DecodeFrameCount() override;
62   void InitializeNewFrame(size_t) override;
63   void Decode(size_t) override;
64   // When the disposal method of the frame is DisposeOverWritePrevious, the
65   // next frame will use a previous frame's buffer as its starting state, so
66   // we can't take over the data in that case. Before calling this method, the
67   // caller must verify that the frame exists.
68   bool CanReusePreviousFrameBuffer(size_t) const override;
69 
70   // When a frame depends on a previous frame's content, there is a list of
71   // candidate reference frames. This function will find a previous frame from
72   // that list which satisfies the requirements of being a reference frame
73   // (kFrameComplete, not kDisposeOverwritePrevious).
74   // If no frame is found, it returns kNotFound.
75   size_t GetViableReferenceFrameIndex(size_t) const;
76 
77   std::unique_ptr<SkCodec> codec_;
78   // |codec_| owns the SegmentStream, but we need access to it to append more
79   // data as it arrives.
80   SegmentStream* segment_stream_ = nullptr;
81   mutable int repetition_count_ = kAnimationLoopOnce;
82   int prior_frame_ = SkCodec::kNoFrame;
83 
84   DISALLOW_COPY_AND_ASSIGN(GIFImageDecoder);
85 };
86 
87 }  // namespace blink
88 
89 #endif
90