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 "config.h" | 26 #include "config.h" |
| 27 | 27 |
| 28 #include "platform/graphics/ImageFrameGenerator.h" | 28 #include "platform/graphics/ImageFrameGenerator.h" |
| 29 | 29 |
| 30 #include "SkData.h" | |
| 30 #include "platform/SharedBuffer.h" | 31 #include "platform/SharedBuffer.h" |
| 31 #include "platform/TraceEvent.h" | 32 #include "platform/TraceEvent.h" |
| 32 #include "platform/graphics/ImageDecodingStore.h" | 33 #include "platform/graphics/ImageDecodingStore.h" |
| 33 #include "platform/image-decoders/ImageDecoder.h" | 34 #include "platform/image-decoders/ImageDecoder.h" |
| 34 | 35 |
| 35 namespace blink { | 36 namespace blink { |
| 36 | 37 |
| 37 static bool compatibleInfo(const SkImageInfo& src, const SkImageInfo& dst) | 38 static bool compatibleInfo(const SkImageInfo& src, const SkImageInfo& dst) |
| 38 { | 39 { |
| 39 if (src == dst) | 40 if (src == dst) |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 componentSizes[0].set(size.width(), size.height()); | 95 componentSizes[0].set(size.width(), size.height()); |
| 95 size = decoder->decodedYUVSize(1, sizeType); | 96 size = decoder->decodedYUVSize(1, sizeType); |
| 96 componentSizes[1].set(size.width(), size.height()); | 97 componentSizes[1].set(size.width(), size.height()); |
| 97 size = decoder->decodedYUVSize(2, sizeType); | 98 size = decoder->decodedYUVSize(2, sizeType); |
| 98 componentSizes[2].set(size.width(), size.height()); | 99 componentSizes[2].set(size.width(), size.height()); |
| 99 return true; | 100 return true; |
| 100 } | 101 } |
| 101 | 102 |
| 102 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha redBuffer> data, bool allDataReceived, bool isMultiFrame) | 103 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha redBuffer> data, bool allDataReceived, bool isMultiFrame) |
| 103 : m_fullSize(fullSize) | 104 : m_fullSize(fullSize) |
| 105 , m_data(adoptRef(new ThreadSafeDataTransport())) | |
| 104 , m_isMultiFrame(isMultiFrame) | 106 , m_isMultiFrame(isMultiFrame) |
| 105 , m_decodeFailedAndEmpty(false) | 107 , m_decodeFailedAndEmpty(false) |
| 106 , m_decodeCount(0) | 108 , m_decodeCount(0) |
| 107 , m_frameCount(0) | 109 , m_frameCount(0) |
| 110 , m_encodedData(nullptr) | |
| 108 { | 111 { |
| 109 setData(data.get(), allDataReceived); | 112 setData(data.get(), allDataReceived); |
| 110 } | 113 } |
| 111 | 114 |
| 112 ImageFrameGenerator::~ImageFrameGenerator() | 115 ImageFrameGenerator::~ImageFrameGenerator() |
| 113 { | 116 { |
| 117 if (m_encodedData) | |
| 118 m_encodedData->unref(); | |
| 114 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); | 119 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); |
| 115 } | 120 } |
| 116 | 121 |
| 117 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec eived) | 122 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec eived) |
| 118 { | 123 { |
| 119 m_data.setData(data.get(), allDataReceived); | 124 m_data->setData(data.get(), allDataReceived); |
| 120 } | 125 } |
| 121 | 126 |
| 122 void ImageFrameGenerator::copyData(RefPtr<SharedBuffer>* data, bool* allDataRece ived) | 127 static void sharedSkDataReleaseCallback(const void* address, void* context) |
| 123 { | 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); | |
| 124 SharedBuffer* buffer = 0; | 137 SharedBuffer* buffer = 0; |
| 125 m_data.data(&buffer, allDataReceived); | 138 bool allDataReceived = false; |
| 126 if (buffer) | 139 dataTransport->data(&buffer, &allDataReceived); |
| 127 *data = buffer->copy(); | 140 ASSERT(allDataReceived && buffer && buffer->data() == address); |
|
Noel Gordon
2015/12/08 15:41:58
Interesting, a little too much on looking, sorry f
aleksandar.stojiljkovic
2015/12/08 20:28:13
Acknowledged. Prefer to keep it since it is very u
| |
| 141 #endif | |
| 142 // Dereference m_data now. | |
| 143 dataTransport->deref(); | |
| 144 } | |
| 145 | |
| 146 SkData* ImageFrameGenerator::refEncodedData() | |
| 147 { | |
| 148 // A note about allDataReceived: | |
|
Noel Gordon
2015/12/08 15:41:59
Remove this line.
aleksandar.stojiljkovic
2015/12/08 20:28:13
Done.
| |
| 149 // SkData is returned only when full image (encoded) data is downloaded. Thi s is important | |
|
Noel Gordon
2015/12/08 15:41:59
downloaded -> received
aleksandar.stojiljkovic
2015/12/08 20:28:13
Done.
| |
| 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 | |
|
Noel Gordon
2015/12/08 15:41:59
Prefer the early return: do this case first
i
aleksandar.stojiljkovic
2015/12/08 20:28:13
Done.
| |
| 159 { | |
| 160 // Prevents concurrent access to m_encodedData creation. | |
| 161 MutexLocker lock(m_decodeMutex); | |
|
Noel Gordon
2015/12/08 15:41:59
Hmmm ... none of your other patches had this lock.
aleksandar_stojiljkovic
2015/12/08 16:04:00
The comment two lines above - for multithreaded ra
| |
| 162 if (!m_encodedData) { | |
| 163 // m_encodedData is created with initial reference count == 1. Image FrameGenerator always holds one | |
| 164 // reference to m_encodedData, as it prevents write access in SkData ::writable_data. | |
| 165 m_encodedData = SkData::NewWithProc(buffer->data(), buffer->size(), sharedSkDataReleaseCallback, m_data.get()); | |
| 166 // While m_encodedData is referenced, prevent disposing m_data and i ts content. | |
| 167 // it is dereferenced in sharedSkDataReleaseCallback, called when m_ encodedData gets dereferenced. | |
| 168 m_data->ref(); | |
| 169 } | |
| 170 } | |
| 171 // Increase the reference, caller must decrease it. One reference is always kept by ImageFrameGenerator and released | |
| 172 // in destructor. | |
| 173 m_encodedData->ref(); | |
| 174 return m_encodedData; | |
| 128 } | 175 } |
| 129 | 176 |
| 130 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes) | 177 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes) |
| 131 { | 178 { |
| 132 // This method is called to populate a discardable memory owned by Skia. | 179 // This method is called to populate a discardable memory owned by Skia. |
| 133 | 180 |
| 134 // Prevents concurrent decode or scale operations on the same image data. | 181 // Prevents concurrent decode or scale operations on the same image data. |
| 135 MutexLocker lock(m_decodeMutex); | 182 MutexLocker lock(m_decodeMutex); |
| 136 | 183 |
| 137 // This implementation does not support scaling so check the requested size. | 184 // This implementation does not support scaling so check the requested size. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 | 224 |
| 178 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeToYUV", "generator", this, "decodeCount", static_cast<int>(m_decodeCount)); | 225 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeToYUV", "generator", this, "decodeCount", static_cast<int>(m_decodeCount)); |
| 179 | 226 |
| 180 if (!planes || !planes[0] || !planes[1] || !planes[2] | 227 if (!planes || !planes[0] || !planes[1] || !planes[2] |
| 181 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { | 228 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { |
| 182 return false; | 229 return false; |
| 183 } | 230 } |
| 184 | 231 |
| 185 SharedBuffer* data = 0; | 232 SharedBuffer* data = 0; |
| 186 bool allDataReceived = false; | 233 bool allDataReceived = false; |
| 187 m_data.data(&data, &allDataReceived); | 234 m_data->data(&data, &allDataReceived); |
| 188 | 235 |
| 189 // FIXME: YUV decoding does not currently support progressive decoding. | 236 // FIXME: YUV decoding does not currently support progressive decoding. |
| 190 ASSERT(allDataReceived); | 237 ASSERT(allDataReceived); |
| 191 | 238 |
| 192 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | 239 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
| 193 if (!decoder) | 240 if (!decoder) |
| 194 return false; | 241 return false; |
| 195 | 242 |
| 196 decoder->setData(data, allDataReceived); | 243 decoder->setData(data, allDataReceived); |
| 197 | 244 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 } | 330 } |
| 284 | 331 |
| 285 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap) | 332 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap) |
| 286 { | 333 { |
| 287 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); | 334 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); |
| 288 | 335 |
| 289 ASSERT(decoder); | 336 ASSERT(decoder); |
| 290 SharedBuffer* data = 0; | 337 SharedBuffer* data = 0; |
| 291 bool allDataReceived = false; | 338 bool allDataReceived = false; |
| 292 bool newDecoder = false; | 339 bool newDecoder = false; |
| 293 m_data.data(&data, &allDataReceived); | 340 m_data->data(&data, &allDataReceived); |
| 294 | 341 |
| 295 // Try to create an ImageDecoder if we are not given one. | 342 // Try to create an ImageDecoder if we are not given one. |
| 296 if (!*decoder) { | 343 if (!*decoder) { |
| 297 newDecoder = true; | 344 newDecoder = true; |
| 298 if (m_imageDecoderFactory) | 345 if (m_imageDecoderFactory) |
| 299 *decoder = m_imageDecoderFactory->create().leakPtr(); | 346 *decoder = m_imageDecoderFactory->create().leakPtr(); |
| 300 | 347 |
| 301 if (!*decoder) | 348 if (!*decoder) |
| 302 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); | 349 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); |
| 303 | 350 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 } | 398 } |
| 352 | 399 |
| 353 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) | 400 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) |
| 354 { | 401 { |
| 355 ASSERT(componentSizes); | 402 ASSERT(componentSizes); |
| 356 | 403 |
| 357 TRACE_EVENT2("webkit", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); | 404 TRACE_EVENT2("webkit", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); |
| 358 | 405 |
| 359 SharedBuffer* data = 0; | 406 SharedBuffer* data = 0; |
| 360 bool allDataReceived = false; | 407 bool allDataReceived = false; |
| 361 m_data.data(&data, &allDataReceived); | 408 m_data->data(&data, &allDataReceived); |
| 362 | 409 |
| 363 // FIXME: YUV decoding does not currently support progressive decoding. | 410 // FIXME: YUV decoding does not currently support progressive decoding. |
| 364 if (!allDataReceived) | 411 if (!allDataReceived) |
| 365 return false; | 412 return false; |
| 366 | 413 |
| 367 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | 414 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
| 368 if (!decoder) | 415 if (!decoder) |
| 369 return false; | 416 return false; |
| 370 | 417 |
| 371 // JPEG images support YUV decoding: other decoders do not. So don't pump da ta into decoders | 418 // JPEG images support YUV decoding: other decoders do not. So don't pump da ta into decoders |
| 372 // that always return false to updateYUVComponentSizes() requests. | 419 // that always return false to updateYUVComponentSizes() requests. |
| 373 if (decoder->filenameExtension() != "jpg") | 420 if (decoder->filenameExtension() != "jpg") |
| 374 return false; | 421 return false; |
| 375 | 422 |
| 376 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding. | 423 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding. |
| 377 decoder->setData(data, allDataReceived); | 424 decoder->setData(data, allDataReceived); |
| 378 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); | 425 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); |
| 379 decoder->setImagePlanes(dummyImagePlanes.release()); | 426 decoder->setImagePlanes(dummyImagePlanes.release()); |
| 380 | 427 |
| 381 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder:: SizeForMemoryAllocation); | 428 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder:: SizeForMemoryAllocation); |
| 382 } | 429 } |
| 383 | 430 |
| 384 } // namespace blink | 431 } // namespace blink |
| OLD | NEW |