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

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/ImageDecoder.cpp

Issue 1812273003: Eliminate copies of encoded image data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Convert to PassRefPtr in ImageDecoder, helper for setData Created 4 years, 9 months 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) Research In Motion Limited 2009-2010. All rights reserved. 2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 14 matching lines...) Expand all
25 #include "platform/image-decoders/bmp/BMPImageDecoder.h" 25 #include "platform/image-decoders/bmp/BMPImageDecoder.h"
26 #include "platform/image-decoders/gif/GIFImageDecoder.h" 26 #include "platform/image-decoders/gif/GIFImageDecoder.h"
27 #include "platform/image-decoders/ico/ICOImageDecoder.h" 27 #include "platform/image-decoders/ico/ICOImageDecoder.h"
28 #include "platform/image-decoders/jpeg/JPEGImageDecoder.h" 28 #include "platform/image-decoders/jpeg/JPEGImageDecoder.h"
29 #include "platform/image-decoders/png/PNGImageDecoder.h" 29 #include "platform/image-decoders/png/PNGImageDecoder.h"
30 #include "platform/image-decoders/webp/WEBPImageDecoder.h" 30 #include "platform/image-decoders/webp/WEBPImageDecoder.h"
31 #include "wtf/PassOwnPtr.h" 31 #include "wtf/PassOwnPtr.h"
32 32
33 namespace blink { 33 namespace blink {
34 34
35 static unsigned copyFromSharedBuffer(char* buffer, unsigned bufferLength, const SharedBuffer& sharedBuffer, unsigned offset) 35 inline bool matchesJPEGSignature(const char* contents)
36 {
37 unsigned bytesExtracted = 0;
38 const char* moreData;
39 while (unsigned moreDataLength = sharedBuffer.getSomeData(moreData, offset)) {
40 unsigned bytesToCopy = std::min(bufferLength - bytesExtracted, moreDataL ength);
41 memcpy(buffer + bytesExtracted, moreData, bytesToCopy);
42 bytesExtracted += bytesToCopy;
43 if (bytesExtracted == bufferLength)
44 break;
45 offset += bytesToCopy;
46 }
47 return bytesExtracted;
48 }
49
50 inline bool matchesJPEGSignature(char* contents)
51 { 36 {
52 return !memcmp(contents, "\xFF\xD8\xFF", 3); 37 return !memcmp(contents, "\xFF\xD8\xFF", 3);
53 } 38 }
54 39
55 inline bool matchesPNGSignature(char* contents) 40 inline bool matchesPNGSignature(const char* contents)
56 { 41 {
57 return !memcmp(contents, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8); 42 return !memcmp(contents, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8);
58 } 43 }
59 44
60 inline bool matchesGIFSignature(char* contents) 45 inline bool matchesGIFSignature(const char* contents)
61 { 46 {
62 return !memcmp(contents, "GIF87a", 6) || !memcmp(contents, "GIF89a", 6); 47 return !memcmp(contents, "GIF87a", 6) || !memcmp(contents, "GIF89a", 6);
63 } 48 }
64 49
65 inline bool matchesWebPSignature(char* contents) 50 inline bool matchesWebPSignature(const char* contents)
66 { 51 {
67 return !memcmp(contents, "RIFF", 4) && !memcmp(contents + 8, "WEBPVP", 6); 52 return !memcmp(contents, "RIFF", 4) && !memcmp(contents + 8, "WEBPVP", 6);
68 } 53 }
69 54
70 inline bool matchesICOSignature(char* contents) 55 inline bool matchesICOSignature(const char* contents)
71 { 56 {
72 return !memcmp(contents, "\x00\x00\x01\x00", 4); 57 return !memcmp(contents, "\x00\x00\x01\x00", 4);
73 } 58 }
74 59
75 inline bool matchesCURSignature(char* contents) 60 inline bool matchesCURSignature(const char* contents)
76 { 61 {
77 return !memcmp(contents, "\x00\x00\x02\x00", 4); 62 return !memcmp(contents, "\x00\x00\x02\x00", 4);
78 } 63 }
79 64
80 inline bool matchesBMPSignature(char* contents) 65 inline bool matchesBMPSignature(const char* contents)
81 { 66 {
82 return !memcmp(contents, "BM", 2); 67 return !memcmp(contents, "BM", 2);
83 } 68 }
84 69
85 PassOwnPtr<ImageDecoder> ImageDecoder::create(const SharedBuffer& data, AlphaOpt ion alphaOption, GammaAndColorProfileOption colorOptions) 70 PassOwnPtr<ImageDecoder> ImageDecoder::create(const char* contents, size_t lengt h, AlphaOption alphaOption, GammaAndColorProfileOption colorOptions)
86 { 71 {
87 const unsigned longestSignatureLength = sizeof("RIFF????WEBPVP") - 1; 72 const unsigned longestSignatureLength = sizeof("RIFF????WEBPVP") - 1;
88 ASSERT(longestSignatureLength == 14); 73 ASSERT(longestSignatureLength == 14);
89 74
75 if (length < longestSignatureLength)
76 return nullptr;
77
90 size_t maxDecodedBytes = Platform::current() ? Platform::current()->maxDecod edImageBytes() : noDecodedImageByteLimit; 78 size_t maxDecodedBytes = Platform::current() ? Platform::current()->maxDecod edImageBytes() : noDecodedImageByteLimit;
91 79
92 char contents[longestSignatureLength];
93 if (copyFromSharedBuffer(contents, longestSignatureLength, data, 0) < longes tSignatureLength)
94 return nullptr;
95
96 if (matchesJPEGSignature(contents)) 80 if (matchesJPEGSignature(contents))
97 return adoptPtr(new JPEGImageDecoder(alphaOption, colorOptions, maxDecod edBytes)); 81 return adoptPtr(new JPEGImageDecoder(alphaOption, colorOptions, maxDecod edBytes));
98 82
99 if (matchesPNGSignature(contents)) 83 if (matchesPNGSignature(contents))
100 return adoptPtr(new PNGImageDecoder(alphaOption, colorOptions, maxDecode dBytes)); 84 return adoptPtr(new PNGImageDecoder(alphaOption, colorOptions, maxDecode dBytes));
101 85
102 if (matchesGIFSignature(contents)) 86 if (matchesGIFSignature(contents))
103 return adoptPtr(new GIFImageDecoder(alphaOption, colorOptions, maxDecode dBytes)); 87 return adoptPtr(new GIFImageDecoder(alphaOption, colorOptions, maxDecode dBytes));
104 88
105 if (matchesWebPSignature(contents)) 89 if (matchesWebPSignature(contents))
106 return adoptPtr(new WEBPImageDecoder(alphaOption, colorOptions, maxDecod edBytes)); 90 return adoptPtr(new WEBPImageDecoder(alphaOption, colorOptions, maxDecod edBytes));
107 91
108 if (matchesICOSignature(contents) || matchesCURSignature(contents)) 92 if (matchesICOSignature(contents) || matchesCURSignature(contents))
109 return adoptPtr(new ICOImageDecoder(alphaOption, colorOptions, maxDecode dBytes)); 93 return adoptPtr(new ICOImageDecoder(alphaOption, colorOptions, maxDecode dBytes));
110 94
111 if (matchesBMPSignature(contents)) 95 if (matchesBMPSignature(contents))
112 return adoptPtr(new BMPImageDecoder(alphaOption, colorOptions, maxDecode dBytes)); 96 return adoptPtr(new BMPImageDecoder(alphaOption, colorOptions, maxDecode dBytes));
113 97
114 return nullptr; 98 return nullptr;
115 } 99 }
116 100
101 PassOwnPtr<ImageDecoder> ImageDecoder::create(const SharedBuffer& data, AlphaOpt ion alphaOption, GammaAndColorProfileOption colorOptions)
102 {
103 // SharedBuffer::getSomeData() will not return less than 14 bytes from a rea d from the beginning, unless there is no more data.
104 const char* contents;
105 const size_t length = data.getSomeData(contents);
106 return create(contents, length, alphaOption, colorOptions);
107 }
108
109 PassOwnPtr<ImageDecoder> ImageDecoder::create(const SegmentReader& data, AlphaOp tion alphaOption, GammaAndColorProfileOption colorOptions)
110 {
111 // SegmentReader::getSomeData() will not return less than 14 bytes from a re ad from the beginning, unless there is no more data.
112 const char* contents;
113 const size_t length = data.getSomeData(contents, 0);
114 return create(contents, length, alphaOption, colorOptions);
115 }
116
117 size_t ImageDecoder::frameCount() 117 size_t ImageDecoder::frameCount()
118 { 118 {
119 const size_t oldSize = m_frameBufferCache.size(); 119 const size_t oldSize = m_frameBufferCache.size();
120 const size_t newSize = decodeFrameCount(); 120 const size_t newSize = decodeFrameCount();
121 if (oldSize != newSize) { 121 if (oldSize != newSize) {
122 m_frameBufferCache.resize(newSize); 122 m_frameBufferCache.resize(newSize);
123 for (size_t i = oldSize; i < newSize; ++i) { 123 for (size_t i = oldSize; i < newSize; ++i) {
124 m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha); 124 m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha);
125 initializeNewFrame(i); 125 initializeNewFrame(i);
126 } 126 }
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 return m_planes[i]; 265 return m_planes[i];
266 } 266 }
267 267
268 size_t ImagePlanes::rowBytes(int i) const 268 size_t ImagePlanes::rowBytes(int i) const
269 { 269 {
270 ASSERT((i >= 0) && i < 3); 270 ASSERT((i >= 0) && i < 3);
271 return m_rowBytes[i]; 271 return m_rowBytes[i];
272 } 272 }
273 273
274 } // namespace blink 274 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698