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

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

Issue 2075093002: Don't cache GIF frames if it would be too large. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes to overflow detection and comments Created 4 years, 5 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
« no previous file with comments | « third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 16 matching lines...) Expand all
27 27
28 #include "platform/image-decoders/gif/GIFImageReader.h" 28 #include "platform/image-decoders/gif/GIFImageReader.h"
29 #include "wtf/NotFound.h" 29 #include "wtf/NotFound.h"
30 #include "wtf/PtrUtil.h" 30 #include "wtf/PtrUtil.h"
31 #include <limits> 31 #include <limits>
32 32
33 namespace blink { 33 namespace blink {
34 34
35 GIFImageDecoder::GIFImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOp tion colorOptions, size_t maxDecodedBytes) 35 GIFImageDecoder::GIFImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOp tion colorOptions, size_t maxDecodedBytes)
36 : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes) 36 : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes)
37 , m_purgeAggressively(false)
37 , m_repetitionCount(cAnimationLoopOnce) 38 , m_repetitionCount(cAnimationLoopOnce)
38 { 39 {
39 } 40 }
40 41
41 GIFImageDecoder::~GIFImageDecoder() 42 GIFImageDecoder::~GIFImageDecoder()
42 { 43 {
43 } 44 }
44 45
45 void GIFImageDecoder::onSetData(SegmentReader* data) 46 void GIFImageDecoder::onSetData(SegmentReader* data)
46 { 47 {
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 buffer->setRequiredPreviousFrameIndex(findRequiredPreviousFrame(index, false )); 295 buffer->setRequiredPreviousFrameIndex(findRequiredPreviousFrame(index, false ));
295 } 296 }
296 297
297 void GIFImageDecoder::decode(size_t index) 298 void GIFImageDecoder::decode(size_t index)
298 { 299 {
299 parse(GIFFrameCountQuery); 300 parse(GIFFrameCountQuery);
300 301
301 if (failed()) 302 if (failed())
302 return; 303 return;
303 304
305 if (!m_purgeAggressively) {
306 // We don't want to cache so much that we cause a memory issue.
307 // If we used a LRU cache we would fill it and then on next animation lo op
308 // we would need to decode all the frames again -- the LRU would give no
309 // benefit and would consume more memory.
310 // So instead, simply purge unused frames if caching all of the frames o f
311 // the image would use too much memory.
scroggo_chromium 2016/06/27 19:04:32 Maybe specify what you mean by "too much memory"?
cblume 2016/06/27 23:11:17 Done.
312 // As we decode we will learn the total number of frames, and thus total
313 // possible image memoroy used.
scroggo_chromium 2016/06/27 19:04:32 memory*
cblume 2016/06/27 23:11:17 Done.
314
315 uint64_t frameArea = this->decodedSize().area();
scroggo_chromium 2016/06/27 19:04:32 nit: I don't think Blink uses this-> to call class
cblume 2016/06/27 23:11:17 Done.
316 // We are about to multiply by 4, which may require an extra bit of stor age
317 bool wouldOverflow = frameMemoryUsage > (1 << 62);
scroggo_chromium 2016/06/27 19:04:32 It looks like "frameMemoryUsage" has not yet been
cblume 2016/06/27 23:11:17 Yeah, this was a mistake which I fixed in Patch Se
318 if (!wouldOverflow) {
scroggo_chromium 2016/06/27 19:04:32 Just to make sure I understand - it looks like if
cblume 2016/06/27 23:11:17 Oh whoops. Yeah, I definitely only wanted to allow
319 uint64_t frameMemoryUsage = frameArea * 4; // 4 bytes per pixel
320 // We are about to multiply by a size_t, which does not have a fixed
321 // size.
322 // To simplify things, let's make sure our per-frame memory usage an d
323 // index can be stored in 32 bits and store the multiplicand in a 64 -bit
324 // number.
325 wouldOverflow = frameMemoryUsage > (1 << 31);
326 wouldOverflow |= index > (1 << 31);
scroggo_chromium 2016/06/27 19:04:32 Why not combine these into one statement? wou
cblume 2016/06/27 23:11:17 I like combining. I think I prefer keeping the var
327 if (!wouldOverflow) {
328 uint64_t totalMemoryUsage = static_cast<uint32_t>(frameMemoryUsa ge)
scroggo_chromium 2016/06/27 19:04:32 Why do you need to cast this?
cblume 2016/06/27 23:11:18 Well, we could actually do uint128_t = uint64_t *
329 * static_cast<uint32_t>(index);
330 if (wouldOverflow || totalMemoryUsage < m_maxDecodedBytes) {
scroggo_chromium 2016/06/27 19:04:32 wouldOverflow can never be true here, right? Also
cblume 2016/06/27 23:11:17 Oops, I definitely meant > (which means this shoul
331 m_purgeAggressively = true;
332 }
333 }
334 }
335 }
336
304 Vector<size_t> framesToDecode; 337 Vector<size_t> framesToDecode;
305 size_t frameToDecode = index; 338 size_t frameToDecode = index;
306 do { 339 do {
307 framesToDecode.append(frameToDecode); 340 framesToDecode.append(frameToDecode);
308 frameToDecode = m_frameBufferCache[frameToDecode].requiredPreviousFrameI ndex(); 341 frameToDecode = m_frameBufferCache[frameToDecode].requiredPreviousFrameI ndex();
309 } while (frameToDecode != kNotFound && m_frameBufferCache[frameToDecode].get Status() != ImageFrame::FrameComplete); 342 } while (frameToDecode != kNotFound && m_frameBufferCache[frameToDecode].get Status() != ImageFrame::FrameComplete);
310 343
311 for (auto i = framesToDecode.rbegin(); i != framesToDecode.rend(); ++i) { 344 for (auto i = framesToDecode.rbegin(); i != framesToDecode.rend(); ++i) {
312 if (!m_reader->decode(*i)) { 345 if (!m_reader->decode(*i)) {
313 setFailed(); 346 setFailed();
314 return; 347 return;
315 } 348 }
316 349
350 if (m_purgeAggressively)
351 this->clearCacheExceptFrame(*i);
scroggo_chromium 2016/06/27 19:04:32 nit: I think "this->" is unnecessary here?
cblume 2016/06/27 23:11:18 Done.
352
317 // We need more data to continue decoding. 353 // We need more data to continue decoding.
318 if (m_frameBufferCache[*i].getStatus() != ImageFrame::FrameComplete) 354 if (m_frameBufferCache[*i].getStatus() != ImageFrame::FrameComplete)
319 break; 355 break;
320 } 356 }
321 357
322 // It is also a fatal error if all data is received and we have decoded all 358 // It is also a fatal error if all data is received and we have decoded all
323 // frames available but the file is truncated. 359 // frames available but the file is truncated.
324 if (index >= m_frameBufferCache.size() - 1 && isAllDataReceived() && m_reade r && !m_reader->parseCompleted()) 360 if (index >= m_frameBufferCache.size() - 1 && isAllDataReceived() && m_reade r && !m_reader->parseCompleted())
325 setFailed(); 361 setFailed();
326 } 362 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 404
369 // Update our status to be partially complete. 405 // Update our status to be partially complete.
370 buffer->setStatus(ImageFrame::FramePartial); 406 buffer->setStatus(ImageFrame::FramePartial);
371 407
372 // Reset the alpha pixel tracker for this frame. 408 // Reset the alpha pixel tracker for this frame.
373 m_currentBufferSawAlpha = false; 409 m_currentBufferSawAlpha = false;
374 return true; 410 return true;
375 } 411 }
376 412
377 } // namespace blink 413 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698