OLD | NEW |
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 18 matching lines...) Expand all Loading... |
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, | 35 GIFImageDecoder::GIFImageDecoder(AlphaOption alphaOption, |
36 GammaAndColorProfileOption colorOptions, | 36 GammaAndColorProfileOption colorOptions, |
37 size_t maxDecodedBytes) | 37 size_t maxDecodedBytes) |
38 : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes), | 38 : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes), |
39 m_purgeAggressively(false), | |
40 m_repetitionCount(cAnimationLoopOnce) {} | 39 m_repetitionCount(cAnimationLoopOnce) {} |
41 | 40 |
42 GIFImageDecoder::~GIFImageDecoder() {} | 41 GIFImageDecoder::~GIFImageDecoder() {} |
43 | 42 |
44 void GIFImageDecoder::onSetData(SegmentReader* data) { | 43 void GIFImageDecoder::onSetData(SegmentReader* data) { |
45 if (m_reader) | 44 if (m_reader) |
46 m_reader->setData(data); | 45 m_reader->setData(data); |
47 } | 46 } |
48 | 47 |
49 int GIFImageDecoder::repetitionCount() const { | 48 int GIFImageDecoder::repetitionCount() const { |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 } | 395 } |
397 } | 396 } |
398 | 397 |
399 // Update our status to be partially complete. | 398 // Update our status to be partially complete. |
400 buffer->setStatus(ImageFrame::FramePartial); | 399 buffer->setStatus(ImageFrame::FramePartial); |
401 | 400 |
402 // Reset the alpha pixel tracker for this frame. | 401 // Reset the alpha pixel tracker for this frame. |
403 m_currentBufferSawAlpha = false; | 402 m_currentBufferSawAlpha = false; |
404 return true; | 403 return true; |
405 } | 404 } |
406 | |
407 void GIFImageDecoder::updateAggressivePurging(size_t index) { | |
408 if (m_purgeAggressively) | |
409 return; | |
410 | |
411 // We don't want to cache so much that we cause a memory issue. | |
412 // | |
413 // If we used a LRU cache we would fill it and then on next animation loop | |
414 // we would need to decode all the frames again -- the LRU would give no | |
415 // benefit and would consume more memory. | |
416 // So instead, simply purge unused frames if caching all of the frames of | |
417 // the image would use more memory than the image decoder is allowed | |
418 // (m_maxDecodedBytes) or would overflow 32 bits.. | |
419 // | |
420 // As we decode we will learn the total number of frames, and thus total | |
421 // possible image memory used. | |
422 | |
423 const uint64_t frameArea = decodedSize().area(); | |
424 // We are about to multiply by 4, which may require an extra bit of storage | |
425 bool wouldOverflow = frameArea > (UINT64_C(1) << 62); | |
426 if (wouldOverflow) { | |
427 m_purgeAggressively = true; | |
428 return; | |
429 } | |
430 | |
431 const uint64_t frameMemoryUsage = frameArea * 4; // 4 bytes per pixel | |
432 // We are about to multiply by a size_t, which does not have a fixed | |
433 // size. | |
434 // To simplify things, let's make sure our per-frame memory usage and | |
435 // index can be stored in 32 bits and store the multiplicand in a 64-bit | |
436 // number. | |
437 wouldOverflow = | |
438 (frameMemoryUsage > (UINT32_C(1) << 31)) || (index > (UINT32_C(1) << 31)); | |
439 if (wouldOverflow) { | |
440 m_purgeAggressively = true; | |
441 return; | |
442 } | |
443 | |
444 const uint64_t totalMemoryUsage = frameMemoryUsage * index; | |
445 if (totalMemoryUsage > m_maxDecodedBytes) { | |
446 m_purgeAggressively = true; | |
447 } | |
448 } | |
449 } // namespace blink | 405 } // namespace blink |
OLD | NEW |