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 28 matching lines...) Expand all Loading... | |
| 94 componentWidthBytes[0] = decoder->decodedYUVWidthBytes(0); | 93 componentWidthBytes[0] = decoder->decodedYUVWidthBytes(0); |
| 95 size = decoder->decodedYUVSize(1); | 94 size = decoder->decodedYUVSize(1); |
| 96 componentSizes[1].set(size.width(), size.height()); | 95 componentSizes[1].set(size.width(), size.height()); |
| 97 componentWidthBytes[1] = decoder->decodedYUVWidthBytes(1); | 96 componentWidthBytes[1] = decoder->decodedYUVWidthBytes(1); |
| 98 size = decoder->decodedYUVSize(2); | 97 size = decoder->decodedYUVSize(2); |
| 99 componentSizes[2].set(size.width(), size.height()); | 98 componentSizes[2].set(size.width(), size.height()); |
| 100 componentWidthBytes[2] = decoder->decodedYUVWidthBytes(2); | 99 componentWidthBytes[2] = decoder->decodedYUVWidthBytes(2); |
| 101 return true; | 100 return true; |
| 102 } | 101 } |
| 103 | 102 |
| 104 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha redBuffer> data, bool allDataReceived, bool isMultiFrame) | 103 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, bool isMultiFr ame) |
| 105 : m_fullSize(fullSize) | 104 : m_fullSize(fullSize) |
| 106 , m_data(adoptRef(new ThreadSafeDataTransport())) | |
| 107 , m_isMultiFrame(isMultiFrame) | 105 , m_isMultiFrame(isMultiFrame) |
| 108 , m_decodeFailed(false) | 106 , m_decodeFailed(false) |
| 109 , m_frameCount(0) | 107 , m_frameCount(0) |
| 110 , m_encodedData(nullptr) | |
| 111 { | 108 { |
| 112 setData(data.get(), allDataReceived); | |
| 113 } | 109 } |
| 114 | 110 |
| 115 ImageFrameGenerator::~ImageFrameGenerator() | 111 ImageFrameGenerator::~ImageFrameGenerator() |
| 116 { | 112 { |
| 117 if (m_encodedData) | |
| 118 m_encodedData->unref(); | |
| 119 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); | 113 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); |
| 120 } | 114 } |
| 121 | 115 |
| 122 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec eived) | 116 bool ImageFrameGenerator::decodeAndScale(SegmentReader* data, bool allDataReceiv ed, size_t index, const SkImageInfo& info, void* pixels, size_t rowBytes) |
| 123 { | 117 { |
| 124 m_data->setData(data.get(), allDataReceived); | |
| 125 } | |
| 126 | |
| 127 static void sharedSkDataReleaseCallback(const void* address, void* context) | |
| 128 { | |
| 129 // This gets called when m_encodedData reference count becomes 0 - and it co uld happen in | |
| 130 // ImageFrameGenerator destructor or later when m_encodedData gets dereferen ced. | |
| 131 // In this method, we deref ThreadSafeDataTransport, as ThreadSafeDataTransp ort is the owner | |
| 132 // of data returned via refEncodedData. | |
| 133 | |
| 134 ThreadSafeDataTransport* dataTransport = static_cast<ThreadSafeDataTransport *>(context); | |
| 135 #if ENABLE(ASSERT) | |
| 136 ASSERT(dataTransport); | |
| 137 SharedBuffer* buffer = 0; | |
| 138 bool allDataReceived = false; | |
| 139 dataTransport->data(&buffer, &allDataReceived); | |
| 140 ASSERT(allDataReceived && buffer && buffer->data() == address); | |
| 141 #endif | |
| 142 // Dereference m_data now. | |
| 143 dataTransport->deref(); | |
| 144 } | |
| 145 | |
| 146 SkData* ImageFrameGenerator::refEncodedData() | |
| 147 { | |
| 148 // SkData is returned only when full image (encoded) data is received. This is important | |
| 149 // since DeferredImageDecoder::setData is called only once with allDataRecei ved set to true, | |
| 150 // and after that m_data->m_readBuffer.data() is not changed. See also RELEA SE_ASSERT used in | |
| 151 // ThreadSafeDataTransport::data(). | |
| 152 SharedBuffer* buffer = 0; | |
| 153 bool allDataReceived = false; | |
| 154 m_data->data(&buffer, &allDataReceived); | |
| 155 if (!allDataReceived) | |
| 156 return nullptr; | |
| 157 | |
| 158 { | |
| 159 // Prevents concurrent access to m_encodedData creation. | |
| 160 MutexLocker lock(m_decodeMutex); | |
| 161 if (m_encodedData) { | |
| 162 m_encodedData->ref(); | |
| 163 return m_encodedData; | |
| 164 } | |
| 165 // m_encodedData is created with initial reference count == 1. ImageFram eGenerator always holds one | |
| 166 // reference to m_encodedData, as it prevents write access in SkData::wr itable_data. | |
| 167 m_encodedData = SkData::NewWithProc(buffer->data(), buffer->size(), shar edSkDataReleaseCallback, m_data.get()); | |
| 168 // While m_encodedData is referenced, prevent disposing m_data and its c ontent. | |
| 169 // it is dereferenced in sharedSkDataReleaseCallback, called when m_enco dedData gets dereferenced. | |
| 170 m_data->ref(); | |
| 171 } | |
| 172 // Increase the reference, caller must decrease it. One reference is always kept by ImageFrameGenerator and released | |
| 173 // in destructor. | |
| 174 m_encodedData->ref(); | |
| 175 return m_encodedData; | |
| 176 } | |
| 177 | |
| 178 bool ImageFrameGenerator::decodeAndScale(size_t index, const SkImageInfo& info, void* pixels, size_t rowBytes) | |
| 179 { | |
| 180 // Prevent concurrent decode or scale operations on the same image data. | |
| 181 MutexLocker lock(m_decodeMutex); | |
| 182 | |
| 183 if (m_decodeFailed) | 118 if (m_decodeFailed) |
| 184 return false; | 119 return false; |
| 185 | 120 |
| 186 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", static_cast<int>(index)); | 121 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", static_cast<int>(index)); |
| 187 | 122 |
| 188 m_externalAllocator = adoptPtr(new ExternalMemoryAllocator(info, pixels, row Bytes)); | 123 RefPtr<ExternalMemoryAllocator> externalAllocator = adoptRef(new ExternalMem oryAllocator(info, pixels, rowBytes)); |
| 189 | 124 |
| 190 // This implementation does not support scaling so check the requested size. | 125 // This implementation does not support scaling so check the requested size. |
| 191 SkISize scaledSize = SkISize::Make(info.width(), info.height()); | 126 SkISize scaledSize = SkISize::Make(info.width(), info.height()); |
| 192 ASSERT(m_fullSize == scaledSize); | 127 ASSERT(m_fullSize == scaledSize); |
| 193 | 128 |
| 194 SkBitmap bitmap = tryToResumeDecode(index, scaledSize); | 129 // TODO (scroggo): Convert tryToResumeDecode() and decode() to take a |
| 130 // PassRefPtr<SkBitmap::Allocator> instead of a bare pointer. | |
| 131 SkBitmap bitmap = tryToResumeDecode(data, allDataReceived, index, scaledSize , externalAllocator.get()); | |
| 195 if (bitmap.isNull()) | 132 if (bitmap.isNull()) |
| 196 return false; | 133 return false; |
| 197 | 134 |
| 198 // Don't keep the allocator because it contains a pointer to memory | 135 externalAllocator.clear(); |
|
f(malita)
2016/03/25 18:47:53
Nit: since externalAllocator is now local, I think
scroggo_chromium
2016/03/25 18:56:21
Done.
| |
| 199 // that we do not own. | |
| 200 m_externalAllocator.clear(); | |
| 201 | 136 |
| 202 // Check to see if the decoder has written directly to the pixel memory | 137 // Check to see if the decoder has written directly to the pixel memory |
| 203 // provided. If not, make a copy. | 138 // provided. If not, make a copy. |
| 204 ASSERT(bitmap.width() == scaledSize.width()); | 139 ASSERT(bitmap.width() == scaledSize.width()); |
| 205 ASSERT(bitmap.height() == scaledSize.height()); | 140 ASSERT(bitmap.height() == scaledSize.height()); |
| 206 SkAutoLockPixels bitmapLock(bitmap); | 141 SkAutoLockPixels bitmapLock(bitmap); |
| 207 if (bitmap.getPixels() != pixels) | 142 if (bitmap.getPixels() != pixels) |
| 208 return bitmap.copyPixelsTo(pixels, rowBytes * info.height(), rowBytes); | 143 return bitmap.copyPixelsTo(pixels, rowBytes * info.height(), rowBytes); |
| 209 return true; | 144 return true; |
| 210 } | 145 } |
| 211 | 146 |
| 212 bool ImageFrameGenerator::decodeToYUV(size_t index, const SkISize componentSizes [3], void* planes[3], const size_t rowBytes[3]) | 147 bool ImageFrameGenerator::decodeToYUV(SegmentReader* data, size_t index, const S kISize componentSizes[3], void* planes[3], const size_t rowBytes[3]) |
| 213 { | 148 { |
| 214 // Prevent concurrent decode or scale operations on the same image data. | 149 // TODO (scroggo): The only interesting thing this uses from the ImageFrameG enerator is m_decodeFailed. |
| 215 MutexLocker lock(m_decodeMutex); | 150 // Move this into DecodingImageGenerator, which is the only class that calls it. |
| 216 | |
| 217 if (m_decodeFailed) | 151 if (m_decodeFailed) |
| 218 return false; | 152 return false; |
| 219 | 153 |
| 220 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeToYUV", "frame index", sta tic_cast<int>(index)); | 154 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeToYUV", "frame index", sta tic_cast<int>(index)); |
| 221 | 155 |
| 222 if (!planes || !planes[0] || !planes[1] || !planes[2] | 156 if (!planes || !planes[0] || !planes[1] || !planes[2] |
| 223 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { | 157 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { |
| 224 return false; | 158 return false; |
| 225 } | 159 } |
| 226 | 160 |
| 227 SharedBuffer* data = 0; | 161 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
| 228 bool allDataReceived = false; | 162 // getYUVComponentSizes was already called and was successful, so ImageDecod er::create must succeed. |
| 229 m_data->data(&data, &allDataReceived); | 163 ASSERT(decoder); |
| 230 | 164 |
| 231 // FIXME: YUV decoding does not currently support progressive decoding. | 165 decoder->setData(data, true); |
| 232 ASSERT(allDataReceived); | |
| 233 | |
| 234 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | |
| 235 if (!decoder) | |
| 236 return false; | |
| 237 | |
| 238 decoder->setData(data, allDataReceived); | |
| 239 | 166 |
| 240 OwnPtr<ImagePlanes> imagePlanes = adoptPtr(new ImagePlanes(planes, rowBytes) ); | 167 OwnPtr<ImagePlanes> imagePlanes = adoptPtr(new ImagePlanes(planes, rowBytes) ); |
| 241 decoder->setImagePlanes(imagePlanes.release()); | 168 decoder->setImagePlanes(imagePlanes.release()); |
| 242 | 169 |
| 243 ASSERT(decoder->canDecodeToYUV()); | 170 ASSERT(decoder->canDecodeToYUV()); |
| 244 | 171 |
| 245 if (decoder->decodeToYUV()) { | 172 if (decoder->decodeToYUV()) { |
| 246 setHasAlpha(0, false); // YUV is always opaque | 173 setHasAlpha(0, false); // YUV is always opaque |
| 247 return true; | 174 return true; |
| 248 } | 175 } |
| 249 | 176 |
| 250 ASSERT(decoder->failed()); | 177 ASSERT(decoder->failed()); |
| 251 m_decodeFailed = true; | 178 m_decodeFailed = true; |
| 252 return false; | 179 return false; |
| 253 } | 180 } |
| 254 | 181 |
| 255 SkBitmap ImageFrameGenerator::tryToResumeDecode(size_t index, const SkISize& sca ledSize) | 182 SkBitmap ImageFrameGenerator::tryToResumeDecode(SegmentReader* data, bool allDat aReceived, size_t index, const SkISize& scaledSize, SkBitmap::Allocator* allocat or) |
| 256 { | 183 { |
| 257 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index ", static_cast<int>(index)); | 184 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index ", static_cast<int>(index)); |
| 258 | 185 |
| 259 ImageDecoder* decoder = 0; | 186 ImageDecoder* decoder = 0; |
| 187 | |
| 188 // Lock the mutex, so only one thread can use the decoder at once. | |
| 189 MutexLocker lock(m_decodeMutex); | |
| 260 const bool resumeDecoding = ImageDecodingStore::instance().lockDecoder(this, m_fullSize, &decoder); | 190 const bool resumeDecoding = ImageDecodingStore::instance().lockDecoder(this, m_fullSize, &decoder); |
| 261 ASSERT(!resumeDecoding || decoder); | 191 ASSERT(!resumeDecoding || decoder); |
| 262 | 192 |
| 263 SkBitmap fullSizeImage; | 193 SkBitmap fullSizeImage; |
| 264 bool complete = decode(index, &decoder, &fullSizeImage); | 194 bool complete = decode(data, allDataReceived, index, &decoder, &fullSizeImag e, allocator); |
| 265 | 195 |
| 266 if (!decoder) | 196 if (!decoder) |
| 267 return SkBitmap(); | 197 return SkBitmap(); |
| 268 | 198 |
| 269 // If we are not resuming decoding that means the decoder is freshly | 199 // If we are not resuming decoding that means the decoder is freshly |
| 270 // created and we have ownership. If we are resuming decoding then | 200 // created and we have ownership. If we are resuming decoding then |
| 271 // the decoder is owned by ImageDecodingStore. | 201 // the decoder is owned by ImageDecodingStore. |
| 272 OwnPtr<ImageDecoder> decoderContainer; | 202 OwnPtr<ImageDecoder> decoderContainer; |
| 273 if (!resumeDecoding) | 203 if (!resumeDecoding) |
| 274 decoderContainer = adoptPtr(decoder); | 204 decoderContainer = adoptPtr(decoder); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 MutexLocker lock(m_alphaMutex); | 244 MutexLocker lock(m_alphaMutex); |
| 315 if (index >= m_hasAlpha.size()) { | 245 if (index >= m_hasAlpha.size()) { |
| 316 const size_t oldSize = m_hasAlpha.size(); | 246 const size_t oldSize = m_hasAlpha.size(); |
| 317 m_hasAlpha.resize(index + 1); | 247 m_hasAlpha.resize(index + 1); |
| 318 for (size_t i = oldSize; i < m_hasAlpha.size(); ++i) | 248 for (size_t i = oldSize; i < m_hasAlpha.size(); ++i) |
| 319 m_hasAlpha[i] = true; | 249 m_hasAlpha[i] = true; |
| 320 } | 250 } |
| 321 m_hasAlpha[index] = hasAlpha; | 251 m_hasAlpha[index] = hasAlpha; |
| 322 } | 252 } |
| 323 | 253 |
| 324 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap) | 254 bool ImageFrameGenerator::decode(SegmentReader* data, bool allDataReceived, size _t index, ImageDecoder** decoder, SkBitmap* bitmap, SkBitmap::Allocator* allocat or) |
| 325 { | 255 { |
| 256 ASSERT(m_decodeMutex.locked()); | |
| 326 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); | 257 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); |
| 327 | 258 |
| 328 SharedBuffer* data = 0; | |
| 329 bool allDataReceived = false; | |
| 330 m_data->data(&data, &allDataReceived); | |
| 331 | |
| 332 // Try to create an ImageDecoder if we are not given one. | 259 // Try to create an ImageDecoder if we are not given one. |
| 333 ASSERT(decoder); | 260 ASSERT(decoder); |
| 334 bool newDecoder = false; | 261 bool newDecoder = false; |
| 335 if (!*decoder) { | 262 if (!*decoder) { |
| 336 newDecoder = true; | 263 newDecoder = true; |
| 337 if (m_imageDecoderFactory) | 264 if (m_imageDecoderFactory) |
| 338 *decoder = m_imageDecoderFactory->create().leakPtr(); | 265 *decoder = m_imageDecoderFactory->create().leakPtr(); |
| 339 | 266 |
| 340 if (!*decoder) | 267 if (!*decoder) |
| 341 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); | 268 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); |
| 342 | 269 |
| 343 if (!*decoder) | 270 if (!*decoder) |
| 344 return false; | 271 return false; |
| 345 } | 272 } |
| 346 | 273 |
| 347 if (!m_isMultiFrame && newDecoder && allDataReceived) { | 274 if (!m_isMultiFrame && newDecoder && allDataReceived) { |
| 348 // If we're using an external memory allocator that means we're decoding | 275 // If we're using an external memory allocator that means we're decoding |
| 349 // directly into the output memory and we can save one memcpy. | 276 // directly into the output memory and we can save one memcpy. |
| 350 ASSERT(m_externalAllocator.get()); | 277 ASSERT(allocator); |
| 351 (*decoder)->setMemoryAllocator(m_externalAllocator.get()); | 278 (*decoder)->setMemoryAllocator(allocator); |
| 352 } | 279 } |
| 353 | 280 |
| 354 (*decoder)->setData(data, allDataReceived); | 281 (*decoder)->setData(data, allDataReceived); |
| 355 ImageFrame* frame = (*decoder)->frameBufferAtIndex(index); | 282 ImageFrame* frame = (*decoder)->frameBufferAtIndex(index); |
| 356 | 283 |
| 357 // For multi-frame image decoders, we need to know how many frames are | 284 // For multi-frame image decoders, we need to know how many frames are |
| 358 // in that image in order to release the decoder when all frames are | 285 // in that image in order to release the decoder when all frames are |
| 359 // decoded. frameCount() is reliable only if all data is received and set in | 286 // decoded. frameCount() is reliable only if all data is received and set in |
| 360 // decoder, particularly with GIF. | 287 // decoder, particularly with GIF. |
| 361 if (allDataReceived) | 288 if (allDataReceived) |
| 362 m_frameCount = (*decoder)->frameCount(); | 289 m_frameCount = (*decoder)->frameCount(); |
| 363 | 290 |
| 364 (*decoder)->setData(0, false); // Unref SharedBuffer from ImageDecoder. | 291 (*decoder)->setData(PassRefPtr<SegmentReader>(nullptr), false); // Unref Seg mentReader from ImageDecoder. |
|
f(malita)
2016/03/25 18:47:53
nit: would straight nullptr not work here? (we hav
scroggo_chromium
2016/03/25 18:56:21
It would, except that this method is overloaded -
| |
| 365 (*decoder)->clearCacheExceptFrame(index); | 292 (*decoder)->clearCacheExceptFrame(index); |
| 366 (*decoder)->setMemoryAllocator(0); | 293 (*decoder)->setMemoryAllocator(0); |
| 367 | 294 |
| 368 if (!frame || frame->getStatus() == ImageFrame::FrameEmpty) | 295 if (!frame || frame->getStatus() == ImageFrame::FrameEmpty) |
| 369 return false; | 296 return false; |
| 370 | 297 |
| 371 // A cache object is considered complete if we can decode a complete frame. | 298 // A cache object is considered complete if we can decode a complete frame. |
| 372 // Or we have received all data. The image might not be fully decoded in | 299 // Or we have received all data. The image might not be fully decoded in |
| 373 // the latter case. | 300 // the latter case. |
| 374 const bool isDecodeComplete = frame->getStatus() == ImageFrame::FrameComplet e || allDataReceived; | 301 const bool isDecodeComplete = frame->getStatus() == ImageFrame::FrameComplet e || allDataReceived; |
| 375 | 302 |
| 376 SkBitmap fullSizeBitmap = frame->getSkBitmap(); | 303 SkBitmap fullSizeBitmap = frame->getSkBitmap(); |
| 377 if (!fullSizeBitmap.isNull()) { | 304 if (!fullSizeBitmap.isNull()) { |
| 378 ASSERT(fullSizeBitmap.width() == m_fullSize.width() && fullSizeBitmap.he ight() == m_fullSize.height()); | 305 ASSERT(fullSizeBitmap.width() == m_fullSize.width() && fullSizeBitmap.he ight() == m_fullSize.height()); |
| 379 setHasAlpha(index, !fullSizeBitmap.isOpaque()); | 306 setHasAlpha(index, !fullSizeBitmap.isOpaque()); |
| 380 } | 307 } |
| 381 | 308 |
| 382 *bitmap = fullSizeBitmap; | 309 *bitmap = fullSizeBitmap; |
| 383 return isDecodeComplete; | 310 return isDecodeComplete; |
| 384 } | 311 } |
| 385 | 312 |
| 386 bool ImageFrameGenerator::hasAlpha(size_t index) | 313 bool ImageFrameGenerator::hasAlpha(size_t index) |
| 387 { | 314 { |
| 388 MutexLocker lock(m_alphaMutex); | 315 MutexLocker lock(m_alphaMutex); |
| 389 if (index < m_hasAlpha.size()) | 316 if (index < m_hasAlpha.size()) |
| 390 return m_hasAlpha[index]; | 317 return m_hasAlpha[index]; |
| 391 return true; | 318 return true; |
| 392 } | 319 } |
| 393 | 320 |
| 394 bool ImageFrameGenerator::getYUVComponentSizes(SkYUVSizeInfo* sizeInfo) | 321 bool ImageFrameGenerator::getYUVComponentSizes(SegmentReader* data, SkYUVSizeInf o* sizeInfo) |
| 395 { | 322 { |
| 396 TRACE_EVENT2("blink", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); | 323 TRACE_EVENT2("blink", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); |
| 397 | 324 |
| 398 SharedBuffer* data = 0; | |
| 399 bool allDataReceived = false; | |
| 400 m_data->data(&data, &allDataReceived); | |
| 401 | |
| 402 // FIXME: YUV decoding does not currently support progressive decoding. | |
| 403 if (!allDataReceived) | |
| 404 return false; | |
| 405 | |
| 406 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | 325 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
| 407 if (!decoder) | 326 if (!decoder) |
| 408 return false; | 327 return false; |
| 409 | 328 |
| 410 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding. | 329 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding. |
| 411 decoder->setData(data, allDataReceived); | 330 decoder->setData(data, true); |
| 412 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); | 331 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); |
| 413 decoder->setImagePlanes(dummyImagePlanes.release()); | 332 decoder->setImagePlanes(dummyImagePlanes.release()); |
| 414 | 333 |
| 415 return updateYUVComponentSizes(decoder.get(), sizeInfo->fSizes, sizeInfo->fW idthBytes); | 334 return updateYUVComponentSizes(decoder.get(), sizeInfo->fSizes, sizeInfo->fW idthBytes); |
| 416 } | 335 } |
| 417 | 336 |
| 418 } // namespace blink | 337 } // namespace blink |
| OLD | NEW |