Chromium Code Reviews| 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 |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "platform/graphics/ImageFrameGenerator.h" | 26 #include "platform/graphics/ImageFrameGenerator.h" |
| 27 | 27 |
| 28 #include "SkData.h" | 28 #include "SkData.h" |
| 29 #include "platform/SharedBuffer.h" | |
| 30 #include "platform/TraceEvent.h" | 29 #include "platform/TraceEvent.h" |
| 31 #include "platform/graphics/ImageDecodingStore.h" | 30 #include "platform/graphics/ImageDecodingStore.h" |
| 32 #include "platform/image-decoders/ImageDecoder.h" | 31 #include "platform/image-decoders/ImageDecoder.h" |
| 33 | 32 |
| 34 namespace blink { | 33 namespace blink { |
| 35 | 34 |
| 36 static bool compatibleInfo(const SkImageInfo& src, const SkImageInfo& dst) | 35 static bool compatibleInfo(const SkImageInfo& src, const SkImageInfo& dst) |
| 37 { | 36 { |
| 38 if (src == dst) | 37 if (src == dst) |
| 39 return true; | 38 return true; |
| 40 | 39 |
| 41 // It is legal to write kOpaque_SkAlphaType pixels into a kPremul_SkAlphaTyp e buffer. | 40 // It is legal to write kOpaque_SkAlphaType pixels into a kPremul_SkAlphaTyp e buffer. |
| 42 // This can happen when DeferredImageDecoder allocates an kOpaque_SkAlphaTyp e image | 41 // This can happen when DeferredImageDecoder allocates an kOpaque_SkAlphaTyp e image |
| 43 // generator based on cached frame info, while the ImageFrame-allocated dest bitmap | 42 // generator based on cached frame info, while the ImageFrame-allocated dest bitmap |
| 44 // stays kPremul_SkAlphaType. | 43 // stays kPremul_SkAlphaType. |
| 45 if (src.alphaType() == kOpaque_SkAlphaType && dst.alphaType() == kPremul_SkA lphaType) { | 44 if (src.alphaType() == kOpaque_SkAlphaType && dst.alphaType() == kPremul_SkA lphaType) { |
| 46 const SkImageInfo& tmp = src.makeAlphaType(kPremul_SkAlphaType); | 45 const SkImageInfo& tmp = src.makeAlphaType(kPremul_SkAlphaType); |
| 47 return tmp == dst; | 46 return tmp == dst; |
| 48 } | 47 } |
| 49 | 48 |
| 50 return false; | 49 return false; |
| 51 } | 50 } |
| 52 | 51 |
| 53 // Creates a SkPixelRef such that the memory for pixels is given by an external body. | 52 // Creates a SkPixelRef such that the memory for pixels is given by an external body. |
| 54 // This is used to write directly to the memory given by Skia during decoding. | 53 // This is used to write directly to the memory given by Skia during decoding. |
| 55 class ImageFrameGenerator::ExternalMemoryAllocator final : public SkBitmap::Allo cator { | 54 class ExternalMemoryAllocator final : public SkBitmap::Allocator { |
| 56 USING_FAST_MALLOC(ExternalMemoryAllocator); | 55 USING_FAST_MALLOC(ExternalMemoryAllocator); |
| 57 WTF_MAKE_NONCOPYABLE(ExternalMemoryAllocator); | 56 WTF_MAKE_NONCOPYABLE(ExternalMemoryAllocator); |
| 58 public: | 57 public: |
| 59 ExternalMemoryAllocator(const SkImageInfo& info, void* pixels, size_t rowByt es) | 58 ExternalMemoryAllocator(const SkImageInfo& info, void* pixels, size_t rowByt es) |
| 60 : m_info(info) | 59 : m_info(info) |
| 61 , m_pixels(pixels) | 60 , m_pixels(pixels) |
| 62 , m_rowBytes(rowBytes) | 61 , m_rowBytes(rowBytes) |
| 63 { | 62 { |
| 64 } | 63 } |
| 65 | 64 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 91 | 90 |
| 92 IntSize size = decoder->decodedYUVSize(0, sizeType); | 91 IntSize size = decoder->decodedYUVSize(0, sizeType); |
| 93 componentSizes[0].set(size.width(), size.height()); | 92 componentSizes[0].set(size.width(), size.height()); |
| 94 size = decoder->decodedYUVSize(1, sizeType); | 93 size = decoder->decodedYUVSize(1, sizeType); |
| 95 componentSizes[1].set(size.width(), size.height()); | 94 componentSizes[1].set(size.width(), size.height()); |
| 96 size = decoder->decodedYUVSize(2, sizeType); | 95 size = decoder->decodedYUVSize(2, sizeType); |
| 97 componentSizes[2].set(size.width(), size.height()); | 96 componentSizes[2].set(size.width(), size.height()); |
| 98 return true; | 97 return true; |
| 99 } | 98 } |
| 100 | 99 |
| 101 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha redBuffer> data, bool allDataReceived, bool isMultiFrame) | 100 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, bool isMultiFr ame) |
| 102 : m_fullSize(fullSize) | 101 : m_fullSize(fullSize) |
| 103 , m_data(adoptRef(new ThreadSafeDataTransport())) | |
| 104 , m_isMultiFrame(isMultiFrame) | 102 , m_isMultiFrame(isMultiFrame) |
| 105 , m_decodeFailed(false) | 103 , m_decodeFailed(false) |
| 106 , m_frameCount(0) | 104 , m_frameCount(0) |
| 107 , m_encodedData(nullptr) | |
| 108 { | 105 { |
| 109 setData(data.get(), allDataReceived); | |
| 110 } | 106 } |
| 111 | 107 |
| 112 ImageFrameGenerator::~ImageFrameGenerator() | 108 ImageFrameGenerator::~ImageFrameGenerator() |
| 113 { | 109 { |
| 114 if (m_encodedData) | |
| 115 m_encodedData->unref(); | |
| 116 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); | 110 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); |
| 117 } | 111 } |
| 118 | 112 |
| 119 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec eived) | 113 bool ImageFrameGenerator::decodeAndScale(SegmentReader* data, bool allDataReceiv ed, size_t index, const SkImageInfo& info, void* pixels, size_t rowBytes) |
| 120 { | 114 { |
| 121 m_data->setData(data.get(), allDataReceived); | |
| 122 } | |
| 123 | |
| 124 static void sharedSkDataReleaseCallback(const void* address, void* context) | |
| 125 { | |
| 126 // This gets called when m_encodedData reference count becomes 0 - and it co uld happen in | |
| 127 // ImageFrameGenerator destructor or later when m_encodedData gets dereferen ced. | |
| 128 // In this method, we deref ThreadSafeDataTransport, as ThreadSafeDataTransp ort is the owner | |
| 129 // of data returned via refEncodedData. | |
| 130 | |
| 131 ThreadSafeDataTransport* dataTransport = static_cast<ThreadSafeDataTransport *>(context); | |
| 132 #if ENABLE(ASSERT) | |
| 133 ASSERT(dataTransport); | |
| 134 SharedBuffer* buffer = 0; | |
| 135 bool allDataReceived = false; | |
| 136 dataTransport->data(&buffer, &allDataReceived); | |
| 137 ASSERT(allDataReceived && buffer && buffer->data() == address); | |
| 138 #endif | |
| 139 // Dereference m_data now. | |
| 140 dataTransport->deref(); | |
| 141 } | |
| 142 | |
| 143 SkData* ImageFrameGenerator::refEncodedData() | |
| 144 { | |
| 145 // SkData is returned only when full image (encoded) data is received. This is important | |
| 146 // since DeferredImageDecoder::setData is called only once with allDataRecei ved set to true, | |
| 147 // and after that m_data->m_readBuffer.data() is not changed. See also RELEA SE_ASSERT used in | |
| 148 // ThreadSafeDataTransport::data(). | |
| 149 SharedBuffer* buffer = 0; | |
| 150 bool allDataReceived = false; | |
| 151 m_data->data(&buffer, &allDataReceived); | |
| 152 if (!allDataReceived) | |
| 153 return nullptr; | |
| 154 | |
| 155 { | |
| 156 // Prevents concurrent access to m_encodedData creation. | |
| 157 MutexLocker lock(m_decodeMutex); | |
| 158 if (m_encodedData) { | |
| 159 m_encodedData->ref(); | |
| 160 return m_encodedData; | |
| 161 } | |
| 162 // m_encodedData is created with initial reference count == 1. ImageFram eGenerator always holds one | |
| 163 // reference to m_encodedData, as it prevents write access in SkData::wr itable_data. | |
| 164 m_encodedData = SkData::NewWithProc(buffer->data(), buffer->size(), shar edSkDataReleaseCallback, m_data.get()); | |
| 165 // While m_encodedData is referenced, prevent disposing m_data and its c ontent. | |
| 166 // it is dereferenced in sharedSkDataReleaseCallback, called when m_enco dedData gets dereferenced. | |
| 167 m_data->ref(); | |
| 168 } | |
| 169 // Increase the reference, caller must decrease it. One reference is always kept by ImageFrameGenerator and released | |
| 170 // in destructor. | |
| 171 m_encodedData->ref(); | |
| 172 return m_encodedData; | |
| 173 } | |
| 174 | |
| 175 bool ImageFrameGenerator::decodeAndScale(size_t index, const SkImageInfo& info, void* pixels, size_t rowBytes) | |
| 176 { | |
| 177 // Prevent concurrent decode or scale operations on the same image data. | |
| 178 MutexLocker lock(m_decodeMutex); | |
| 179 | |
| 180 if (m_decodeFailed) | 115 if (m_decodeFailed) |
| 181 return false; | 116 return false; |
| 182 | 117 |
| 183 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", static_cast<int>(index)); | 118 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", static_cast<int>(index)); |
| 184 | 119 |
| 185 m_externalAllocator = adoptPtr(new ExternalMemoryAllocator(info, pixels, row Bytes)); | 120 ExternalMemoryAllocator externalAllocator(info, pixels, rowBytes); |
|
f(malita)
2016/03/24 16:48:38
I think this needs to stay heap-allocated: it appe
scroggo_chromium
2016/03/24 19:04:33
Done.
| |
| 186 | 121 |
| 187 // This implementation does not support scaling so check the requested size. | 122 // This implementation does not support scaling so check the requested size. |
| 188 SkISize scaledSize = SkISize::Make(info.width(), info.height()); | 123 SkISize scaledSize = SkISize::Make(info.width(), info.height()); |
| 189 ASSERT(m_fullSize == scaledSize); | 124 ASSERT(m_fullSize == scaledSize); |
| 190 | 125 |
| 191 SkBitmap bitmap = tryToResumeDecode(index, scaledSize); | 126 SkBitmap bitmap = tryToResumeDecode(data, allDataReceived, index, scaledSize , &externalAllocator); |
| 192 if (bitmap.isNull()) | 127 if (bitmap.isNull()) |
| 193 return false; | 128 return false; |
| 194 | 129 |
| 195 // Don't keep the allocator because it contains a pointer to memory | |
| 196 // that we do not own. | |
| 197 m_externalAllocator.clear(); | |
| 198 | |
| 199 // Check to see if the decoder has written directly to the pixel memory | 130 // Check to see if the decoder has written directly to the pixel memory |
| 200 // provided. If not, make a copy. | 131 // provided. If not, make a copy. |
| 201 ASSERT(bitmap.width() == scaledSize.width()); | 132 ASSERT(bitmap.width() == scaledSize.width()); |
| 202 ASSERT(bitmap.height() == scaledSize.height()); | 133 ASSERT(bitmap.height() == scaledSize.height()); |
| 203 SkAutoLockPixels bitmapLock(bitmap); | 134 SkAutoLockPixels bitmapLock(bitmap); |
| 204 if (bitmap.getPixels() != pixels) | 135 if (bitmap.getPixels() != pixels) |
| 205 return bitmap.copyPixelsTo(pixels, rowBytes * info.height(), rowBytes); | 136 return bitmap.copyPixelsTo(pixels, rowBytes * info.height(), rowBytes); |
| 206 return true; | 137 return true; |
| 207 } | 138 } |
| 208 | 139 |
| 209 bool ImageFrameGenerator::decodeToYUV(size_t index, SkISize componentSizes[3], v oid* planes[3], size_t rowBytes[3]) | 140 bool ImageFrameGenerator::decodeToYUV(SegmentReader* data, size_t index, SkISize componentSizes[3], void* planes[3], size_t rowBytes[3]) |
| 210 { | 141 { |
| 211 // Prevent concurrent decode or scale operations on the same image data. | 142 // TODO (scroggo): The only interesting thing this uses from the ImageFrameG enerator is m_decodeFailed. |
| 212 MutexLocker lock(m_decodeMutex); | 143 // Move this into DecodingImageGenerator, which is the only class that calls it. |
| 213 | |
| 214 if (m_decodeFailed) | 144 if (m_decodeFailed) |
| 215 return false; | 145 return false; |
| 216 | 146 |
| 217 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeToYUV", "frame index", sta tic_cast<int>(index)); | 147 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeToYUV", "frame index", sta tic_cast<int>(index)); |
| 218 | 148 |
| 219 if (!planes || !planes[0] || !planes[1] || !planes[2] | 149 if (!planes || !planes[0] || !planes[1] || !planes[2] |
| 220 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { | 150 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { |
| 221 return false; | 151 return false; |
| 222 } | 152 } |
| 223 | 153 |
| 224 SharedBuffer* data = 0; | 154 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
| 225 bool allDataReceived = false; | 155 // getYUVComponentSizes was already called and was successful, so ImageDecod er::create must succeed. |
| 226 m_data->data(&data, &allDataReceived); | 156 ASSERT(decoder); |
| 227 | 157 |
| 228 // FIXME: YUV decoding does not currently support progressive decoding. | 158 decoder->setData(data, true); |
| 229 ASSERT(allDataReceived); | |
| 230 | |
| 231 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | |
| 232 if (!decoder) | |
| 233 return false; | |
| 234 | |
| 235 decoder->setData(data, allDataReceived); | |
| 236 | 159 |
| 237 OwnPtr<ImagePlanes> imagePlanes = adoptPtr(new ImagePlanes(planes, rowBytes) ); | 160 OwnPtr<ImagePlanes> imagePlanes = adoptPtr(new ImagePlanes(planes, rowBytes) ); |
| 238 decoder->setImagePlanes(imagePlanes.release()); | 161 decoder->setImagePlanes(imagePlanes.release()); |
| 239 | 162 |
| 240 bool sizeUpdated = updateYUVComponentSizes(decoder.get(), componentSizes, Im ageDecoder::ActualSize); | 163 bool sizeUpdated = updateYUVComponentSizes(decoder.get(), componentSizes, Im ageDecoder::ActualSize); |
| 241 RELEASE_ASSERT(sizeUpdated); | 164 RELEASE_ASSERT(sizeUpdated); |
| 242 | 165 |
| 243 if (decoder->decodeToYUV()) { | 166 if (decoder->decodeToYUV()) { |
| 244 setHasAlpha(0, false); // YUV is always opaque | 167 setHasAlpha(0, false); // YUV is always opaque |
| 245 return true; | 168 return true; |
| 246 } | 169 } |
| 247 | 170 |
| 248 ASSERT(decoder->failed()); | 171 ASSERT(decoder->failed()); |
| 249 m_decodeFailed = true; | 172 m_decodeFailed = true; |
| 250 return false; | 173 return false; |
| 251 } | 174 } |
| 252 | 175 |
| 253 SkBitmap ImageFrameGenerator::tryToResumeDecode(size_t index, const SkISize& sca ledSize) | 176 SkBitmap ImageFrameGenerator::tryToResumeDecode(SegmentReader* data, bool allDat aReceived, size_t index, const SkISize& scaledSize, SkBitmap::Allocator* allocat or) |
| 254 { | 177 { |
| 255 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index ", static_cast<int>(index)); | 178 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index ", static_cast<int>(index)); |
| 256 | 179 |
| 257 ImageDecoder* decoder = 0; | 180 ImageDecoder* decoder = 0; |
| 181 | |
| 182 // Lock the mutex, so only one thread can use the decoder at once. | |
| 183 MutexLocker lock(m_decodeMutex); | |
| 258 const bool resumeDecoding = ImageDecodingStore::instance().lockDecoder(this, m_fullSize, &decoder); | 184 const bool resumeDecoding = ImageDecodingStore::instance().lockDecoder(this, m_fullSize, &decoder); |
| 259 ASSERT(!resumeDecoding || decoder); | 185 ASSERT(!resumeDecoding || decoder); |
| 260 | 186 |
| 261 SkBitmap fullSizeImage; | 187 SkBitmap fullSizeImage; |
| 262 bool complete = decode(index, &decoder, &fullSizeImage); | 188 bool complete = decode(data, allDataReceived, index, &decoder, &fullSizeImag e, allocator); |
| 263 | 189 |
| 264 if (!decoder) | 190 if (!decoder) |
| 265 return SkBitmap(); | 191 return SkBitmap(); |
| 266 if (index >= m_frameComplete.size()) | 192 if (index >= m_frameComplete.size()) |
| 267 m_frameComplete.resize(index + 1); | 193 m_frameComplete.resize(index + 1); |
| 268 m_frameComplete[index] = complete; | 194 m_frameComplete[index] = complete; |
| 269 | 195 |
| 270 // If we are not resuming decoding that means the decoder is freshly | 196 // If we are not resuming decoding that means the decoder is freshly |
| 271 // created and we have ownership. If we are resuming decoding then | 197 // created and we have ownership. If we are resuming decoding then |
| 272 // the decoder is owned by ImageDecodingStore. | 198 // the decoder is owned by ImageDecodingStore. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 MutexLocker lock(m_alphaMutex); | 243 MutexLocker lock(m_alphaMutex); |
| 318 if (index >= m_hasAlpha.size()) { | 244 if (index >= m_hasAlpha.size()) { |
| 319 const size_t oldSize = m_hasAlpha.size(); | 245 const size_t oldSize = m_hasAlpha.size(); |
| 320 m_hasAlpha.resize(index + 1); | 246 m_hasAlpha.resize(index + 1); |
| 321 for (size_t i = oldSize; i < m_hasAlpha.size(); ++i) | 247 for (size_t i = oldSize; i < m_hasAlpha.size(); ++i) |
| 322 m_hasAlpha[i] = true; | 248 m_hasAlpha[i] = true; |
| 323 } | 249 } |
| 324 m_hasAlpha[index] = hasAlpha; | 250 m_hasAlpha[index] = hasAlpha; |
| 325 } | 251 } |
| 326 | 252 |
| 327 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap) | 253 bool ImageFrameGenerator::decode(SegmentReader* data, bool allDataReceived, size _t index, ImageDecoder** decoder, SkBitmap* bitmap, SkBitmap::Allocator* allocat or) |
| 328 { | 254 { |
| 329 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); | 255 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); |
| 330 | 256 |
| 331 SharedBuffer* data = 0; | |
| 332 bool allDataReceived = false; | |
| 333 m_data->data(&data, &allDataReceived); | |
| 334 | |
| 335 // Try to create an ImageDecoder if we are not given one. | 257 // Try to create an ImageDecoder if we are not given one. |
| 336 ASSERT(decoder); | 258 ASSERT(decoder); |
| 337 bool newDecoder = false; | 259 bool newDecoder = false; |
| 338 if (!*decoder) { | 260 if (!*decoder) { |
| 339 newDecoder = true; | 261 newDecoder = true; |
| 340 if (m_imageDecoderFactory) | 262 if (m_imageDecoderFactory) |
| 341 *decoder = m_imageDecoderFactory->create().leakPtr(); | 263 *decoder = m_imageDecoderFactory->create().leakPtr(); |
| 342 | 264 |
| 343 if (!*decoder) | 265 if (!*decoder) |
| 344 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); | 266 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); |
| 345 | 267 |
| 346 if (!*decoder) | 268 if (!*decoder) |
| 347 return false; | 269 return false; |
| 348 } | 270 } |
| 349 | 271 |
| 350 if (!m_isMultiFrame && newDecoder && allDataReceived) { | 272 if (!m_isMultiFrame && newDecoder && allDataReceived) { |
| 351 // If we're using an external memory allocator that means we're decoding | 273 // If we're using an external memory allocator that means we're decoding |
| 352 // directly into the output memory and we can save one memcpy. | 274 // directly into the output memory and we can save one memcpy. |
| 353 ASSERT(m_externalAllocator.get()); | 275 ASSERT(allocator); |
| 354 (*decoder)->setMemoryAllocator(m_externalAllocator.get()); | 276 (*decoder)->setMemoryAllocator(allocator); |
| 355 } | 277 } |
| 356 | 278 |
| 357 (*decoder)->setData(data, allDataReceived); | 279 (*decoder)->setData(data, allDataReceived); |
| 358 ImageFrame* frame = (*decoder)->frameBufferAtIndex(index); | 280 ImageFrame* frame = (*decoder)->frameBufferAtIndex(index); |
| 359 | 281 |
| 360 // For multi-frame image decoders, we need to know how many frames are | 282 // For multi-frame image decoders, we need to know how many frames are |
| 361 // in that image in order to release the decoder when all frames are | 283 // in that image in order to release the decoder when all frames are |
| 362 // decoded. frameCount() is reliable only if all data is received and set in | 284 // decoded. frameCount() is reliable only if all data is received and set in |
| 363 // decoder, particularly with GIF. | 285 // decoder, particularly with GIF. |
| 364 if (allDataReceived) | 286 if (allDataReceived) |
| 365 m_frameCount = (*decoder)->frameCount(); | 287 m_frameCount = (*decoder)->frameCount(); |
| 366 | 288 |
| 367 (*decoder)->setData(0, false); // Unref SharedBuffer from ImageDecoder. | 289 (*decoder)->setData(PassRefPtr<SegmentReader>(nullptr), false); // Unref Seg mentReader from ImageDecoder. |
| 368 (*decoder)->clearCacheExceptFrame(index); | 290 (*decoder)->clearCacheExceptFrame(index); |
| 369 (*decoder)->setMemoryAllocator(0); | 291 (*decoder)->setMemoryAllocator(0); |
| 370 | 292 |
| 371 if (!frame || frame->status() == ImageFrame::FrameEmpty) | 293 if (!frame || frame->status() == ImageFrame::FrameEmpty) |
| 372 return false; | 294 return false; |
| 373 | 295 |
| 374 // A cache object is considered complete if we can decode a complete frame. | 296 // A cache object is considered complete if we can decode a complete frame. |
| 375 // Or we have received all data. The image might not be fully decoded in | 297 // Or we have received all data. The image might not be fully decoded in |
| 376 // the latter case. | 298 // the latter case. |
| 377 const bool isDecodeComplete = frame->status() == ImageFrame::FrameComplete | | allDataReceived; | 299 const bool isDecodeComplete = frame->status() == ImageFrame::FrameComplete | | allDataReceived; |
| 378 | 300 |
| 379 SkBitmap fullSizeBitmap = frame->getSkBitmap(); | 301 SkBitmap fullSizeBitmap = frame->getSkBitmap(); |
| 380 if (!fullSizeBitmap.isNull()) { | 302 if (!fullSizeBitmap.isNull()) { |
| 381 ASSERT(fullSizeBitmap.width() == m_fullSize.width() && fullSizeBitmap.he ight() == m_fullSize.height()); | 303 ASSERT(fullSizeBitmap.width() == m_fullSize.width() && fullSizeBitmap.he ight() == m_fullSize.height()); |
| 382 setHasAlpha(index, !fullSizeBitmap.isOpaque()); | 304 setHasAlpha(index, !fullSizeBitmap.isOpaque()); |
| 383 } | 305 } |
| 384 | 306 |
| 385 *bitmap = fullSizeBitmap; | 307 *bitmap = fullSizeBitmap; |
| 386 return isDecodeComplete; | 308 return isDecodeComplete; |
| 387 } | 309 } |
| 388 | 310 |
| 389 bool ImageFrameGenerator::hasAlpha(size_t index) | 311 bool ImageFrameGenerator::hasAlpha(size_t index) |
| 390 { | 312 { |
| 391 MutexLocker lock(m_alphaMutex); | 313 MutexLocker lock(m_alphaMutex); |
| 392 if (index < m_hasAlpha.size()) | 314 if (index < m_hasAlpha.size()) |
| 393 return m_hasAlpha[index]; | 315 return m_hasAlpha[index]; |
| 394 return true; | 316 return true; |
| 395 } | 317 } |
| 396 | 318 |
| 397 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) | 319 bool ImageFrameGenerator::getYUVComponentSizes(SegmentReader* data, SkISize comp onentSizes[3]) |
| 398 { | 320 { |
| 399 TRACE_EVENT2("blink", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); | 321 TRACE_EVENT2("blink", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); |
| 400 | 322 |
| 401 SharedBuffer* data = 0; | |
| 402 bool allDataReceived = false; | |
| 403 m_data->data(&data, &allDataReceived); | |
| 404 | |
| 405 // FIXME: YUV decoding does not currently support progressive decoding. | |
| 406 if (!allDataReceived) | |
| 407 return false; | |
| 408 | |
| 409 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | 323 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
| 410 if (!decoder) | 324 if (!decoder) |
| 411 return false; | 325 return false; |
| 412 | 326 |
| 413 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding. | 327 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding. |
| 414 decoder->setData(data, allDataReceived); | 328 decoder->setData(data, true); |
| 415 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); | 329 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); |
| 416 decoder->setImagePlanes(dummyImagePlanes.release()); | 330 decoder->setImagePlanes(dummyImagePlanes.release()); |
| 417 | 331 |
| 418 ASSERT(componentSizes); | 332 ASSERT(componentSizes); |
| 419 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder:: SizeForMemoryAllocation); | 333 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder:: SizeForMemoryAllocation); |
| 420 } | 334 } |
| 421 | 335 |
| 422 } // namespace blink | 336 } // namespace blink |
| OLD | NEW |