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