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