Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.h

Issue 2565323003: Move gif image decoder to SkCodec (Closed)
Patch Set: Removing impossible branch and old comment. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 2 * Copyright (C) 2006 Apple Computer, 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 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #ifndef GIFImageDecoder_h 26 #ifndef GIFImageDecoder_h
27 #define GIFImageDecoder_h 27 #define GIFImageDecoder_h
28 28
29 #include "platform/image-decoders/ImageDecoder.h" 29 #include "platform/image-decoders/ImageDecoder.h"
30 #include "third_party/skia/include/codec/SkCodec.h"
31 #include "third_party/skia/include/core/SkStream.h"
30 #include "wtf/Noncopyable.h" 32 #include "wtf/Noncopyable.h"
33 #include "wtf/RefPtr.h"
34 #include <algorithm>
31 #include <memory> 35 #include <memory>
32 36
33 class GIFImageReader;
34
35 typedef Vector<unsigned char> GIFRow;
36
37 namespace blink { 37 namespace blink {
38 38
39 // This class decodes the GIF image format. 39 // This class decodes the GIF image format.
40 class PLATFORM_EXPORT GIFImageDecoder final : public ImageDecoder { 40 class PLATFORM_EXPORT GIFImageDecoder final : public ImageDecoder {
41 WTF_MAKE_NONCOPYABLE(GIFImageDecoder); 41 WTF_MAKE_NONCOPYABLE(GIFImageDecoder);
42 42
43 public: 43 public:
44 GIFImageDecoder(AlphaOption, 44 GIFImageDecoder(AlphaOption,
45 ColorSpaceOption, 45 ColorSpaceOption,
46 sk_sp<SkColorSpace>, 46 sk_sp<SkColorSpace>,
47 size_t maxDecodedBytes); 47 size_t maxDecodedBytes);
48 ~GIFImageDecoder() override; 48 ~GIFImageDecoder() override;
49 49
50 enum GIFParseQuery { GIFSizeQuery, GIFFrameCountQuery };
51
52 // ImageDecoder: 50 // ImageDecoder:
53 String filenameExtension() const override { return "gif"; } 51 String filenameExtension() const override { return "gif"; }
54 void onSetData(SegmentReader* data) override; 52 void onSetData(SegmentReader* data) override;
55 int repetitionCount() const override; 53 int repetitionCount() const override;
56 bool frameIsCompleteAtIndex(size_t) const override; 54 bool frameIsCompleteAtIndex(size_t) const override;
57 float frameDurationAtIndex(size_t) const override; 55 float frameDurationAtIndex(size_t) const override;
58 // CAUTION: setFailed() deletes |m_reader|. Be careful to avoid
59 // accessing deleted memory, especially when calling this from inside
60 // GIFImageReader!
61 bool setFailed() override;
62
63 // Callbacks from the GIF reader.
64 bool haveDecodedRow(size_t frameIndex,
65 GIFRow::const_iterator rowBegin,
66 size_t width,
67 size_t rowNumber,
68 unsigned repeatCount,
69 bool writeTransparentPixels);
70 bool frameComplete(size_t frameIndex);
71
72 // For testing.
73 bool parseCompleted() const;
74 56
75 private: 57 private:
76 // ImageDecoder: 58 // ImageDecoder:
77 void clearFrameBuffer(size_t frameIndex) override; 59 void decodeSize() override;
78 virtual void decodeSize() { parse(GIFSizeQuery); }
79 size_t decodeFrameCount() override; 60 size_t decodeFrameCount() override;
80 void initializeNewFrame(size_t) override; 61 void initializeNewFrame(size_t) override;
81 void decode(size_t) override; 62 void decode(size_t) override;
82 63
83 // Parses as much as is needed to answer the query, ignoring bitmap
84 // data. If parsing fails, sets the "decode failure" flag.
85 void parse(GIFParseQuery);
86
87 // Reset the alpha tracker for this frame. Before calling this method, the
88 // caller must verify that the frame exists.
89 void onInitFrameBuffer(size_t) override;
90
91 // When the disposal method of the frame is DisposeOverWritePrevious, the 64 // When the disposal method of the frame is DisposeOverWritePrevious, the
92 // next frame will use the previous frame's buffer as its starting state, so 65 // next frame will use the previous frame's buffer as its starting state, so
93 // we can't take over the data in that case. Before calling this method, the 66 // we can't take over the data in that case. Before calling this method, the
94 // caller must verify that the frame exists. 67 // caller must verify that the frame exists.
95 bool canReusePreviousFrameBuffer(size_t) const override; 68 bool canReusePreviousFrameBuffer(size_t) const override;
96 69
97 bool m_currentBufferSawAlpha; 70 class SegmentStream : public SkStream {
98 mutable int m_repetitionCount; 71 public:
99 std::unique_ptr<GIFImageReader> m_reader; 72 SegmentStream()
73 : m_reader(),
74 m_position(0),
75 m_hasReadAllContents(true) {}
76
77 void setReader(SegmentReader* reader, bool allContentsReceived) {
78 m_reader = reader;
79 m_position = 0;
scroggo_chromium 2016/12/14 17:49:23 Why does this reset the position? You'll be receiv
cblume 2016/12/16 02:57:10 Yeah, that was a mistake. I removed it.
80 m_hasReadAllContents = false;
scroggo_chromium 2016/12/14 17:49:23 This assumes you will never be given a SegmentRead
cblume 2016/12/16 02:57:10 We might be thinking of different things. Perhaps
scroggo_chromium 2016/12/16 15:03:48 Yes, but that would be determined by m_position, w
cblume 2016/12/16 17:43:41 Done.
81 }
82
83 size_t read(void* dst, size_t len) {
84 len = std::min(len, m_reader->size());
85
86 size_t bytesAdvanced = 0;
87 if (!dst) { // skipping, not reading
88 bytesAdvanced = len;
89 } else {
90 size_t totalBytesRead = 0;
91 intptr_t destAsInt = reinterpret_cast<intptr_t>(dst);
scroggo_chromium 2016/12/14 17:49:23 I haven't seen this pattern before, and do not see
cblume 2016/12/16 02:57:10 Hrmmm you are right, it is optional. We had used t
92 while (len) {
93 const char* segment = nullptr;
94 size_t bytesRead = m_reader->getSomeData(segment, len);
scroggo_chromium 2016/12/14 17:49:23 This came up while I was reviewing the first patch
cblume 2016/12/16 02:57:10 Done.
95 if (!bytesRead) {
96 break;
97 }
98
99 memcpy(reinterpret_cast<void*>(destAsInt), segment, bytesRead);
100 destAsInt += bytesRead;
101 len -= bytesRead;
102 totalBytesRead += bytesRead;
103 }
104
105 bytesAdvanced = totalBytesRead;
106 }
107
108 m_position += bytesAdvanced;
109 m_hasReadAllContents = m_position == m_reader->size();
110
111 return bytesAdvanced;
112 }
113
114 bool isAtEnd() const override { return m_hasReadAllContents; }
115
116 private:
117 WTF::RefPtr<SegmentReader> m_reader;
118 size_t m_position;
119 bool m_hasReadAllContents;
120 };
121
122 std::unique_ptr<SkCodec> m_codec;
123 SegmentStream* m_segmentStream;
124 // m_segmentStream is a raw pointer because it passes ownership to
125 // m_codec when m_codec is created. However, we still need the
126 // reference so we can append more data as it arrives.
100 }; 127 };
101 128
102 } // namespace blink 129 } // namespace blink
103 130
104 #endif 131 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698