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); |
| 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; |
128 } | 176 } |
129 | 177 |
130 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index,
void* pixels, size_t rowBytes) | 178 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index,
void* pixels, size_t rowBytes) |
131 { | 179 { |
132 // This method is called to populate a discardable memory owned by Skia. | 180 // This method is called to populate a discardable memory owned by Skia. |
133 | 181 |
134 // Prevents concurrent decode or scale operations on the same image data. | 182 // Prevents concurrent decode or scale operations on the same image data. |
135 MutexLocker lock(m_decodeMutex); | 183 MutexLocker lock(m_decodeMutex); |
136 | 184 |
137 // This implementation does not support scaling so check the requested size. | 185 // 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 | 225 |
178 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeToYUV", "generator", this,
"decodeCount", static_cast<int>(m_decodeCount)); | 226 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeToYUV", "generator", this,
"decodeCount", static_cast<int>(m_decodeCount)); |
179 | 227 |
180 if (!planes || !planes[0] || !planes[1] || !planes[2] | 228 if (!planes || !planes[0] || !planes[1] || !planes[2] |
181 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { | 229 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { |
182 return false; | 230 return false; |
183 } | 231 } |
184 | 232 |
185 SharedBuffer* data = 0; | 233 SharedBuffer* data = 0; |
186 bool allDataReceived = false; | 234 bool allDataReceived = false; |
187 m_data.data(&data, &allDataReceived); | 235 m_data->data(&data, &allDataReceived); |
188 | 236 |
189 // FIXME: YUV decoding does not currently support progressive decoding. | 237 // FIXME: YUV decoding does not currently support progressive decoding. |
190 ASSERT(allDataReceived); | 238 ASSERT(allDataReceived); |
191 | 239 |
192 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp
haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | 240 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp
haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
193 if (!decoder) | 241 if (!decoder) |
194 return false; | 242 return false; |
195 | 243 |
196 decoder->setData(data, allDataReceived); | 244 decoder->setData(data, allDataReceived); |
197 | 245 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 } | 331 } |
284 | 332 |
285 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap*
bitmap) | 333 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap*
bitmap) |
286 { | 334 { |
287 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid
th(), "height", m_fullSize.height()); | 335 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid
th(), "height", m_fullSize.height()); |
288 | 336 |
289 ASSERT(decoder); | 337 ASSERT(decoder); |
290 SharedBuffer* data = 0; | 338 SharedBuffer* data = 0; |
291 bool allDataReceived = false; | 339 bool allDataReceived = false; |
292 bool newDecoder = false; | 340 bool newDecoder = false; |
293 m_data.data(&data, &allDataReceived); | 341 m_data->data(&data, &allDataReceived); |
294 | 342 |
295 // Try to create an ImageDecoder if we are not given one. | 343 // Try to create an ImageDecoder if we are not given one. |
296 if (!*decoder) { | 344 if (!*decoder) { |
297 newDecoder = true; | 345 newDecoder = true; |
298 if (m_imageDecoderFactory) | 346 if (m_imageDecoderFactory) |
299 *decoder = m_imageDecoderFactory->create().leakPtr(); | 347 *decoder = m_imageDecoderFactory->create().leakPtr(); |
300 | 348 |
301 if (!*decoder) | 349 if (!*decoder) |
302 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl
ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); | 350 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl
ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); |
303 | 351 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 } | 399 } |
352 | 400 |
353 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) | 401 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) |
354 { | 402 { |
355 ASSERT(componentSizes); | 403 ASSERT(componentSizes); |
356 | 404 |
357 TRACE_EVENT2("blink", "ImageFrameGenerator::getYUVComponentSizes", "width",
m_fullSize.width(), "height", m_fullSize.height()); | 405 TRACE_EVENT2("blink", "ImageFrameGenerator::getYUVComponentSizes", "width",
m_fullSize.width(), "height", m_fullSize.height()); |
358 | 406 |
359 SharedBuffer* data = 0; | 407 SharedBuffer* data = 0; |
360 bool allDataReceived = false; | 408 bool allDataReceived = false; |
361 m_data.data(&data, &allDataReceived); | 409 m_data->data(&data, &allDataReceived); |
362 | 410 |
363 // FIXME: YUV decoding does not currently support progressive decoding. | 411 // FIXME: YUV decoding does not currently support progressive decoding. |
364 if (!allDataReceived) | 412 if (!allDataReceived) |
365 return false; | 413 return false; |
366 | 414 |
367 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp
haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | 415 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp
haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
368 if (!decoder) | 416 if (!decoder) |
369 return false; | 417 return false; |
370 | 418 |
371 // Setting a dummy ImagePlanes object signals to the decoder that we want to
do YUV decoding. | 419 // Setting a dummy ImagePlanes object signals to the decoder that we want to
do YUV decoding. |
372 decoder->setData(data, allDataReceived); | 420 decoder->setData(data, allDataReceived); |
373 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); | 421 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); |
374 decoder->setImagePlanes(dummyImagePlanes.release()); | 422 decoder->setImagePlanes(dummyImagePlanes.release()); |
375 | 423 |
376 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder::
SizeForMemoryAllocation); | 424 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder::
SizeForMemoryAllocation); |
377 } | 425 } |
378 | 426 |
379 } // namespace blink | 427 } // namespace blink |
OLD | NEW |