| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 m_decoderColorBehavior(colorBehavior), | 110 m_decoderColorBehavior(colorBehavior), |
| 111 m_isMultiFrame(isMultiFrame), | 111 m_isMultiFrame(isMultiFrame), |
| 112 m_decodeFailed(false), | 112 m_decodeFailed(false), |
| 113 m_yuvDecodingFailed(false), | 113 m_yuvDecodingFailed(false), |
| 114 m_frameCount(0) {} | 114 m_frameCount(0) {} |
| 115 | 115 |
| 116 ImageFrameGenerator::~ImageFrameGenerator() { | 116 ImageFrameGenerator::~ImageFrameGenerator() { |
| 117 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); | 117 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); |
| 118 } | 118 } |
| 119 | 119 |
| 120 bool ImageFrameGenerator::decodeAndScale(SegmentReader* data, | 120 bool ImageFrameGenerator::decodeAndScale( |
| 121 bool allDataReceived, | 121 SegmentReader* data, |
| 122 size_t index, | 122 bool allDataReceived, |
| 123 const SkImageInfo& info, | 123 size_t index, |
| 124 void* pixels, | 124 const SkImageInfo& info, |
| 125 size_t rowBytes) { | 125 void* pixels, |
| 126 size_t rowBytes, |
| 127 ImageDecoder::AlphaOption alphaOption) { |
| 126 if (m_decodeFailed) | 128 if (m_decodeFailed) |
| 127 return false; | 129 return false; |
| 128 | 130 |
| 129 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", | 131 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", |
| 130 static_cast<int>(index)); | 132 static_cast<int>(index)); |
| 131 | 133 |
| 132 // This implementation does not support scaling so check the requested size. | 134 // This implementation does not support scaling so check the requested size. |
| 133 SkISize scaledSize = SkISize::Make(info.width(), info.height()); | 135 SkISize scaledSize = SkISize::Make(info.width(), info.height()); |
| 134 ASSERT(m_fullSize == scaledSize); | 136 ASSERT(m_fullSize == scaledSize); |
| 135 | 137 |
| 136 // It is okay to allocate ref-counted ExternalMemoryAllocator on the stack, | 138 // It is okay to allocate ref-counted ExternalMemoryAllocator on the stack, |
| 137 // because 1) it contains references to memory that will be invalid after | 139 // because 1) it contains references to memory that will be invalid after |
| 138 // returning (i.e. a pointer to |pixels|) and therefore 2) should not live | 140 // returning (i.e. a pointer to |pixels|) and therefore 2) should not live |
| 139 // longer than the call to the current method. | 141 // longer than the call to the current method. |
| 140 ExternalMemoryAllocator externalAllocator(info, pixels, rowBytes); | 142 ExternalMemoryAllocator externalAllocator(info, pixels, rowBytes); |
| 141 SkBitmap bitmap = tryToResumeDecode(data, allDataReceived, index, scaledSize, | 143 SkBitmap bitmap = tryToResumeDecode(data, allDataReceived, index, scaledSize, |
| 142 &externalAllocator); | 144 &externalAllocator, alphaOption); |
| 143 DCHECK(externalAllocator.unique()); // Verify we have the only ref-count. | 145 DCHECK(externalAllocator.unique()); // Verify we have the only ref-count. |
| 144 | 146 |
| 145 if (bitmap.isNull()) | 147 if (bitmap.isNull()) |
| 146 return false; | 148 return false; |
| 147 | 149 |
| 148 // Check to see if the decoder has written directly to the pixel memory | 150 // Check to see if the decoder has written directly to the pixel memory |
| 149 // provided. If not, make a copy. | 151 // provided. If not, make a copy. |
| 150 ASSERT(bitmap.width() == scaledSize.width()); | 152 ASSERT(bitmap.width() == scaledSize.width()); |
| 151 ASSERT(bitmap.height() == scaledSize.height()); | 153 ASSERT(bitmap.height() == scaledSize.height()); |
| 152 SkAutoLockPixels bitmapLock(bitmap); | 154 SkAutoLockPixels bitmapLock(bitmap); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 ASSERT(decoder->failed()); | 196 ASSERT(decoder->failed()); |
| 195 m_yuvDecodingFailed = true; | 197 m_yuvDecodingFailed = true; |
| 196 return false; | 198 return false; |
| 197 } | 199 } |
| 198 | 200 |
| 199 SkBitmap ImageFrameGenerator::tryToResumeDecode( | 201 SkBitmap ImageFrameGenerator::tryToResumeDecode( |
| 200 SegmentReader* data, | 202 SegmentReader* data, |
| 201 bool allDataReceived, | 203 bool allDataReceived, |
| 202 size_t index, | 204 size_t index, |
| 203 const SkISize& scaledSize, | 205 const SkISize& scaledSize, |
| 204 SkBitmap::Allocator* allocator) { | 206 SkBitmap::Allocator* allocator, |
| 207 ImageDecoder::AlphaOption alphaOption) { |
| 205 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index", | 208 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index", |
| 206 static_cast<int>(index)); | 209 static_cast<int>(index)); |
| 207 | 210 |
| 208 ImageDecoder* decoder = 0; | 211 ImageDecoder* decoder = 0; |
| 209 | 212 |
| 210 // Lock the mutex, so only one thread can use the decoder at once. | 213 // Lock the mutex, so only one thread can use the decoder at once. |
| 211 MutexLocker lock(m_decodeMutex); | 214 MutexLocker lock(m_decodeMutex); |
| 212 const bool resumeDecoding = | 215 const bool resumeDecoding = ImageDecodingStore::instance().lockDecoder( |
| 213 ImageDecodingStore::instance().lockDecoder(this, m_fullSize, &decoder); | 216 this, m_fullSize, alphaOption, &decoder); |
| 214 ASSERT(!resumeDecoding || decoder); | 217 ASSERT(!resumeDecoding || decoder); |
| 215 | 218 |
| 216 SkBitmap fullSizeImage; | 219 SkBitmap fullSizeImage; |
| 217 bool complete = | 220 bool complete = decode(data, allDataReceived, index, &decoder, &fullSizeImage, |
| 218 decode(data, allDataReceived, index, &decoder, &fullSizeImage, allocator); | 221 allocator, alphaOption); |
| 219 | 222 |
| 220 if (!decoder) | 223 if (!decoder) |
| 221 return SkBitmap(); | 224 return SkBitmap(); |
| 222 | 225 |
| 223 // If we are not resuming decoding that means the decoder is freshly | 226 // If we are not resuming decoding that means the decoder is freshly |
| 224 // created and we have ownership. If we are resuming decoding then | 227 // created and we have ownership. If we are resuming decoding then |
| 225 // the decoder is owned by ImageDecodingStore. | 228 // the decoder is owned by ImageDecodingStore. |
| 226 std::unique_ptr<ImageDecoder> decoderContainer; | 229 std::unique_ptr<ImageDecoder> decoderContainer; |
| 227 if (!resumeDecoding) | 230 if (!resumeDecoding) |
| 228 decoderContainer = WTF::wrapUnique(decoder); | 231 decoderContainer = WTF::wrapUnique(decoder); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 m_hasAlpha[i] = true; | 276 m_hasAlpha[i] = true; |
| 274 } | 277 } |
| 275 m_hasAlpha[index] = hasAlpha; | 278 m_hasAlpha[index] = hasAlpha; |
| 276 } | 279 } |
| 277 | 280 |
| 278 bool ImageFrameGenerator::decode(SegmentReader* data, | 281 bool ImageFrameGenerator::decode(SegmentReader* data, |
| 279 bool allDataReceived, | 282 bool allDataReceived, |
| 280 size_t index, | 283 size_t index, |
| 281 ImageDecoder** decoder, | 284 ImageDecoder** decoder, |
| 282 SkBitmap* bitmap, | 285 SkBitmap* bitmap, |
| 283 SkBitmap::Allocator* allocator) { | 286 SkBitmap::Allocator* allocator, |
| 287 ImageDecoder::AlphaOption alphaOption) { |
| 284 ASSERT(m_decodeMutex.locked()); | 288 ASSERT(m_decodeMutex.locked()); |
| 285 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", | 289 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", |
| 286 m_fullSize.width(), "height", m_fullSize.height()); | 290 m_fullSize.width(), "height", m_fullSize.height()); |
| 287 | 291 |
| 288 // Try to create an ImageDecoder if we are not given one. | 292 // Try to create an ImageDecoder if we are not given one. |
| 289 ASSERT(decoder); | 293 ASSERT(decoder); |
| 290 bool newDecoder = false; | 294 bool newDecoder = false; |
| 291 bool shouldCallSetData = true; | 295 bool shouldCallSetData = true; |
| 292 if (!*decoder) { | 296 if (!*decoder) { |
| 293 newDecoder = true; | 297 newDecoder = true; |
| 294 if (m_imageDecoderFactory) | 298 if (m_imageDecoderFactory) |
| 295 *decoder = m_imageDecoderFactory->create().release(); | 299 *decoder = m_imageDecoderFactory->create().release(); |
| 296 | 300 |
| 297 if (!*decoder) { | 301 if (!*decoder) { |
| 298 *decoder = ImageDecoder::create(data, allDataReceived, | 302 *decoder = ImageDecoder::create(data, allDataReceived, alphaOption, |
| 299 ImageDecoder::AlphaPremultiplied, | |
| 300 m_decoderColorBehavior) | 303 m_decoderColorBehavior) |
| 301 .release(); | 304 .release(); |
| 302 // The newly created decoder just grabbed the data. No need to reset it. | 305 // The newly created decoder just grabbed the data. No need to reset it. |
| 303 shouldCallSetData = false; | 306 shouldCallSetData = false; |
| 304 } | 307 } |
| 305 | 308 |
| 306 if (!*decoder) | 309 if (!*decoder) |
| 307 return false; | 310 return false; |
| 308 } | 311 } |
| 309 | 312 |
| 310 | |
| 311 if (shouldCallSetData) | 313 if (shouldCallSetData) |
| 312 (*decoder)->setData(data, allDataReceived); | 314 (*decoder)->setData(data, allDataReceived); |
| 313 | 315 |
| 314 bool usingExternalAllocator = false; | 316 bool usingExternalAllocator = false; |
| 315 | 317 |
| 316 // For multi-frame image decoders, we need to know how many frames are | 318 // For multi-frame image decoders, we need to know how many frames are |
| 317 // in that image in order to release the decoder when all frames are | 319 // in that image in order to release the decoder when all frames are |
| 318 // decoded. frameCount() is reliable only if all data is received and set in | 320 // decoded. frameCount() is reliable only if all data is received and set in |
| 319 // decoder, particularly with GIF. | 321 // decoder, particularly with GIF. |
| 320 if (allDataReceived) { | 322 if (allDataReceived) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 // do YUV decoding. | 385 // do YUV decoding. |
| 384 std::unique_ptr<ImagePlanes> dummyImagePlanes = | 386 std::unique_ptr<ImagePlanes> dummyImagePlanes = |
| 385 WTF::wrapUnique(new ImagePlanes); | 387 WTF::wrapUnique(new ImagePlanes); |
| 386 decoder->setImagePlanes(std::move(dummyImagePlanes)); | 388 decoder->setImagePlanes(std::move(dummyImagePlanes)); |
| 387 | 389 |
| 388 return updateYUVComponentSizes(decoder.get(), sizeInfo->fSizes, | 390 return updateYUVComponentSizes(decoder.get(), sizeInfo->fSizes, |
| 389 sizeInfo->fWidthBytes); | 391 sizeInfo->fWidthBytes); |
| 390 } | 392 } |
| 391 | 393 |
| 392 } // namespace blink | 394 } // namespace blink |
| OLD | NEW |