Chromium Code Reviews| 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 if (!m_purgeAggressively) { | |
|
scroggo_chromium
2016/06/28 15:55:28
How do you feel about making this a private method
cblume
2016/06/28 16:53:33
I like it. Done.
| |
| 306 // We don't want to cache so much that we cause a memory issue. | |
| 307 // | |
| 308 // If we used a LRU cache we would fill it and then on next animation lo op | |
| 309 // we would need to decode all the frames again -- the LRU would give no | |
| 310 // benefit and would consume more memory. | |
| 311 // So instead, simply purge unused frames if caching all of the frames o f | |
| 312 // the image would use more memory than the image decoder is allowed | |
| 313 // (m_maxDecodedBytes). | |
|
scroggo_chromium
2016/06/28 15:55:28
or overflow.
cblume
2016/06/28 16:53:33
Done.
| |
| 314 // | |
| 315 // As we decode we will learn the total number of frames, and thus total | |
| 316 // possible image memory used. | |
| 317 | |
| 318 const uint64_t frameArea = decodedSize().area(); | |
| 319 // We are about to multiply by 4, which may require an extra bit of stor age | |
| 320 bool wouldOverflow = frameArea > (UINT64_C(1) << 62); | |
| 321 if (!wouldOverflow) { | |
|
scroggo_chromium
2016/06/28 15:55:28
I think you want
if (wouldOverflow)
? (No "!
cblume
2016/06/28 16:53:33
Done.
| |
| 322 m_purgeAggressively = true; | |
| 323 } else { | |
| 324 const uint64_t frameMemoryUsage = frameArea * 4; // 4 bytes per pixe l | |
| 325 // We are about to multiply by a size_t, which does not have a fixed | |
| 326 // size. | |
| 327 // To simplify things, let's make sure our per-frame memory usage an d | |
| 328 // index can be stored in 32 bits and store the multiplicand in a 64 -bit | |
| 329 // number. | |
| 330 wouldOverflow = (frameMemoryUsage > (UINT32_C(1) << 31)) | |
| 331 || (index > (UINT32_C(1) << 31)); | |
| 332 if (wouldOverflow) { | |
| 333 m_purgeAggressively = true; | |
| 334 } else { | |
| 335 const uint64_t totalMemoryUsage = frameMemoryUsage * index; | |
| 336 if (wouldOverflow || totalMemoryUsage > m_maxDecodedBytes) { | |
|
scroggo_chromium
2016/06/28 15:55:28
No need to check wouldOverflow here. It is guarant
cblume
2016/06/28 16:53:33
Done.
| |
| 337 m_purgeAggressively = true; | |
| 338 } | |
| 339 } | |
| 340 } | |
| 341 } | |
| 342 | |
| 304 Vector<size_t> framesToDecode; | 343 Vector<size_t> framesToDecode; |
| 305 size_t frameToDecode = index; | 344 size_t frameToDecode = index; |
| 306 do { | 345 do { |
| 307 framesToDecode.append(frameToDecode); | 346 framesToDecode.append(frameToDecode); |
| 308 frameToDecode = m_frameBufferCache[frameToDecode].requiredPreviousFrameI ndex(); | 347 frameToDecode = m_frameBufferCache[frameToDecode].requiredPreviousFrameI ndex(); |
| 309 } while (frameToDecode != kNotFound && m_frameBufferCache[frameToDecode].get Status() != ImageFrame::FrameComplete); | 348 } while (frameToDecode != kNotFound && m_frameBufferCache[frameToDecode].get Status() != ImageFrame::FrameComplete); |
| 310 | 349 |
| 311 for (auto i = framesToDecode.rbegin(); i != framesToDecode.rend(); ++i) { | 350 for (auto i = framesToDecode.rbegin(); i != framesToDecode.rend(); ++i) { |
| 312 if (!m_reader->decode(*i)) { | 351 if (!m_reader->decode(*i)) { |
| 313 setFailed(); | 352 setFailed(); |
| 314 return; | 353 return; |
| 315 } | 354 } |
| 316 | 355 |
| 356 if (m_purgeAggressively) | |
| 357 clearCacheExceptFrame(*i); | |
| 358 | |
| 317 // We need more data to continue decoding. | 359 // We need more data to continue decoding. |
| 318 if (m_frameBufferCache[*i].getStatus() != ImageFrame::FrameComplete) | 360 if (m_frameBufferCache[*i].getStatus() != ImageFrame::FrameComplete) |
| 319 break; | 361 break; |
| 320 } | 362 } |
| 321 | 363 |
| 322 // It is also a fatal error if all data is received and we have decoded all | 364 // It is also a fatal error if all data is received and we have decoded all |
| 323 // frames available but the file is truncated. | 365 // frames available but the file is truncated. |
| 324 if (index >= m_frameBufferCache.size() - 1 && isAllDataReceived() && m_reade r && !m_reader->parseCompleted()) | 366 if (index >= m_frameBufferCache.size() - 1 && isAllDataReceived() && m_reade r && !m_reader->parseCompleted()) |
| 325 setFailed(); | 367 setFailed(); |
| 326 } | 368 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 368 | 410 |
| 369 // Update our status to be partially complete. | 411 // Update our status to be partially complete. |
| 370 buffer->setStatus(ImageFrame::FramePartial); | 412 buffer->setStatus(ImageFrame::FramePartial); |
| 371 | 413 |
| 372 // Reset the alpha pixel tracker for this frame. | 414 // Reset the alpha pixel tracker for this frame. |
| 373 m_currentBufferSawAlpha = false; | 415 m_currentBufferSawAlpha = false; |
| 374 return true; | 416 return true; |
| 375 } | 417 } |
| 376 | 418 |
| 377 } // namespace blink | 419 } // namespace blink |
| OLD | NEW |