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 16 matching lines...) Expand all Loading... |
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 Loading... |
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 updateAggressivePurging(index); |
| 306 |
304 Vector<size_t> framesToDecode; | 307 Vector<size_t> framesToDecode; |
305 size_t frameToDecode = index; | 308 size_t frameToDecode = index; |
306 do { | 309 do { |
307 framesToDecode.append(frameToDecode); | 310 framesToDecode.append(frameToDecode); |
308 frameToDecode = m_frameBufferCache[frameToDecode].requiredPreviousFrameI
ndex(); | 311 frameToDecode = m_frameBufferCache[frameToDecode].requiredPreviousFrameI
ndex(); |
309 } while (frameToDecode != kNotFound && m_frameBufferCache[frameToDecode].get
Status() != ImageFrame::FrameComplete); | 312 } while (frameToDecode != kNotFound && m_frameBufferCache[frameToDecode].get
Status() != ImageFrame::FrameComplete); |
310 | 313 |
311 for (auto i = framesToDecode.rbegin(); i != framesToDecode.rend(); ++i) { | 314 for (auto i = framesToDecode.rbegin(); i != framesToDecode.rend(); ++i) { |
312 if (!m_reader->decode(*i)) { | 315 if (!m_reader->decode(*i)) { |
313 setFailed(); | 316 setFailed(); |
314 return; | 317 return; |
315 } | 318 } |
316 | 319 |
| 320 if (m_purgeAggressively) |
| 321 clearCacheExceptFrame(*i); |
| 322 |
317 // We need more data to continue decoding. | 323 // We need more data to continue decoding. |
318 if (m_frameBufferCache[*i].getStatus() != ImageFrame::FrameComplete) | 324 if (m_frameBufferCache[*i].getStatus() != ImageFrame::FrameComplete) |
319 break; | 325 break; |
320 } | 326 } |
321 | 327 |
322 // It is also a fatal error if all data is received and we have decoded all | 328 // It is also a fatal error if all data is received and we have decoded all |
323 // frames available but the file is truncated. | 329 // frames available but the file is truncated. |
324 if (index >= m_frameBufferCache.size() - 1 && isAllDataReceived() && m_reade
r && !m_reader->parseCompleted()) | 330 if (index >= m_frameBufferCache.size() - 1 && isAllDataReceived() && m_reade
r && !m_reader->parseCompleted()) |
325 setFailed(); | 331 setFailed(); |
326 } | 332 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 } | 373 } |
368 | 374 |
369 // Update our status to be partially complete. | 375 // Update our status to be partially complete. |
370 buffer->setStatus(ImageFrame::FramePartial); | 376 buffer->setStatus(ImageFrame::FramePartial); |
371 | 377 |
372 // Reset the alpha pixel tracker for this frame. | 378 // Reset the alpha pixel tracker for this frame. |
373 m_currentBufferSawAlpha = false; | 379 m_currentBufferSawAlpha = false; |
374 return true; | 380 return true; |
375 } | 381 } |
376 | 382 |
| 383 void GIFImageDecoder::updateAggressivePurging(size_t index) |
| 384 { |
| 385 if (m_purgeAggressively) |
| 386 return; |
| 387 |
| 388 // We don't want to cache so much that we cause a memory issue. |
| 389 // |
| 390 // If we used a LRU cache we would fill it and then on next animation loop |
| 391 // we would need to decode all the frames again -- the LRU would give no |
| 392 // benefit and would consume more memory. |
| 393 // So instead, simply purge unused frames if caching all of the frames of |
| 394 // the image would use more memory than the image decoder is allowed |
| 395 // (m_maxDecodedBytes) or would overflow 32 bits.. |
| 396 // |
| 397 // As we decode we will learn the total number of frames, and thus total |
| 398 // possible image memory used. |
| 399 |
| 400 const uint64_t frameArea = decodedSize().area(); |
| 401 // We are about to multiply by 4, which may require an extra bit of storage |
| 402 bool wouldOverflow = frameArea > (UINT64_C(1) << 62); |
| 403 if (wouldOverflow) { |
| 404 m_purgeAggressively = true; |
| 405 return; |
| 406 } |
| 407 |
| 408 const uint64_t frameMemoryUsage = frameArea * 4; // 4 bytes per pixel |
| 409 // We are about to multiply by a size_t, which does not have a fixed |
| 410 // size. |
| 411 // To simplify things, let's make sure our per-frame memory usage and |
| 412 // index can be stored in 32 bits and store the multiplicand in a 64-bit |
| 413 // number. |
| 414 wouldOverflow = (frameMemoryUsage > (UINT32_C(1) << 31)) |
| 415 || (index > (UINT32_C(1) << 31)); |
| 416 if (wouldOverflow) { |
| 417 m_purgeAggressively = true; |
| 418 return; |
| 419 } |
| 420 |
| 421 const uint64_t totalMemoryUsage = frameMemoryUsage * index; |
| 422 if (totalMemoryUsage > m_maxDecodedBytes) { |
| 423 m_purgeAggressively = true; |
| 424 } |
| 425 } |
377 } // namespace blink | 426 } // namespace blink |
OLD | NEW |