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())) | |
aleksandar.stojiljkovic
2015/12/04 15:36:59
haraken@
| |
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) |
108 { | 110 { |
109 setData(data.get(), allDataReceived); | 111 setData(data.get(), allDataReceived); |
110 } | 112 } |
111 | 113 |
112 ImageFrameGenerator::~ImageFrameGenerator() | 114 ImageFrameGenerator::~ImageFrameGenerator() |
113 { | 115 { |
114 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); | 116 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); |
115 } | 117 } |
116 | 118 |
117 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec eived) | 119 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec eived) |
118 { | 120 { |
119 m_data.setData(data.get(), allDataReceived); | 121 m_data->setData(data.get(), allDataReceived); |
120 } | 122 } |
121 | 123 |
122 void ImageFrameGenerator::copyData(RefPtr<SharedBuffer>* data, bool* allDataRece ived) | 124 static void sharedSkDataReleaseCallback(const void* address, void* context) |
123 { | 125 { |
126 ThreadSafeDataTransport* dataTransport = static_cast<ThreadSafeDataTransport *>(context); | |
127 #if ENABLE(ASSERT) | |
128 ASSERT(dataTransport); | |
124 SharedBuffer* buffer = 0; | 129 SharedBuffer* buffer = 0; |
125 m_data.data(&buffer, allDataReceived); | 130 bool allDataReceived = false; |
126 if (buffer) | 131 dataTransport->data(&buffer, &allDataReceived); |
127 *data = buffer->copy(); | 132 ASSERT(allDataReceived && buffer && buffer->data() == address); |
133 #endif | |
134 // Deref m_data now. | |
135 dataTransport->deref(); | |
136 } | |
137 | |
138 SkData* ImageFrameGenerator::refEncodedData() const | |
139 { | |
140 // Every SkData instance and SharedBuffer need to hold reference to buffer-> data(). | |
141 // Both SkData and m_data->m_readBuffer are having separate reference counti ng | |
142 // implementations. | |
143 // | |
144 // SkData's SkData::NewWithProc is designed with similar use case in mind, | |
145 // unmapping wrapped shmem data once all SkData instances are disposed. | |
146 // | |
147 // In this case, sharedSkDataReleaseCallback would get called when SkData is disposed. | |
148 // In sharedSkDataReleaseCallback, we deref ThreadSafeDataTransport (and Ima geFrameGenerator | |
149 // might still hold a reference to ThreadSafeDataTransport). | |
150 // | |
151 // Multithreading concerns: | |
152 // ThreadSafeDataTransport disposal needs to be thread safe - SkData could g et disposed | |
153 // in any thread, and SkData would still need to hold the reference to Share dBuffer when | |
154 // ImageFrameGenerator is deleted. This is why m_data (ThreadSafeRefCounted) is passed to | |
155 // client - to hold the reference to SharedBuffer (accompanying returned SkD ata). | |
156 // | |
157 // A note about allDataReceived: | |
158 // SkData is returned only when full image (encoded) data is downloaded. Thi s is important | |
159 // since DeferredImageDecoder::setData is called only once with allDataRecei ved set to true, | |
160 // and after that m_data->m_readBuffer.data() is not changed. See also RELEA SE_ASSERT used in | |
161 // ThreadSafeDataTransport::data(). | |
162 SharedBuffer* buffer = 0; | |
163 bool allDataReceived = false; | |
164 m_data->data(&buffer, &allDataReceived); | |
165 if (!allDataReceived) | |
166 return nullptr; | |
167 | |
168 // While SkData is holding reference to underlying data, prevent disposing m _data and its content. | |
169 m_data->ref(); | |
170 return SkData::NewWithProc(buffer->data(), buffer->size(), sharedSkDataRelea seCallback, m_data.get()); | |
128 } | 171 } |
129 | 172 |
130 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes) | 173 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes) |
131 { | 174 { |
132 // This method is called to populate a discardable memory owned by Skia. | 175 // This method is called to populate a discardable memory owned by Skia. |
133 | 176 |
134 // Prevents concurrent decode or scale operations on the same image data. | 177 // Prevents concurrent decode or scale operations on the same image data. |
135 MutexLocker lock(m_decodeMutex); | 178 MutexLocker lock(m_decodeMutex); |
136 | 179 |
137 // This implementation does not support scaling so check the requested size. | 180 // 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 | 220 |
178 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeToYUV", "generator", this, "decodeCount", static_cast<int>(m_decodeCount)); | 221 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeToYUV", "generator", this, "decodeCount", static_cast<int>(m_decodeCount)); |
179 | 222 |
180 if (!planes || !planes[0] || !planes[1] || !planes[2] | 223 if (!planes || !planes[0] || !planes[1] || !planes[2] |
181 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { | 224 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { |
182 return false; | 225 return false; |
183 } | 226 } |
184 | 227 |
185 SharedBuffer* data = 0; | 228 SharedBuffer* data = 0; |
186 bool allDataReceived = false; | 229 bool allDataReceived = false; |
187 m_data.data(&data, &allDataReceived); | 230 m_data->data(&data, &allDataReceived); |
188 | 231 |
189 // FIXME: YUV decoding does not currently support progressive decoding. | 232 // FIXME: YUV decoding does not currently support progressive decoding. |
190 ASSERT(allDataReceived); | 233 ASSERT(allDataReceived); |
191 | 234 |
192 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | 235 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
193 if (!decoder) | 236 if (!decoder) |
194 return false; | 237 return false; |
195 | 238 |
196 decoder->setData(data, allDataReceived); | 239 decoder->setData(data, allDataReceived); |
197 | 240 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 } | 326 } |
284 | 327 |
285 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap) | 328 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap) |
286 { | 329 { |
287 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); | 330 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); |
288 | 331 |
289 ASSERT(decoder); | 332 ASSERT(decoder); |
290 SharedBuffer* data = 0; | 333 SharedBuffer* data = 0; |
291 bool allDataReceived = false; | 334 bool allDataReceived = false; |
292 bool newDecoder = false; | 335 bool newDecoder = false; |
293 m_data.data(&data, &allDataReceived); | 336 m_data->data(&data, &allDataReceived); |
294 | 337 |
295 // Try to create an ImageDecoder if we are not given one. | 338 // Try to create an ImageDecoder if we are not given one. |
296 if (!*decoder) { | 339 if (!*decoder) { |
297 newDecoder = true; | 340 newDecoder = true; |
298 if (m_imageDecoderFactory) | 341 if (m_imageDecoderFactory) |
299 *decoder = m_imageDecoderFactory->create().leakPtr(); | 342 *decoder = m_imageDecoderFactory->create().leakPtr(); |
300 | 343 |
301 if (!*decoder) | 344 if (!*decoder) |
302 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); | 345 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); |
303 | 346 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 } | 394 } |
352 | 395 |
353 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) | 396 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) |
354 { | 397 { |
355 ASSERT(componentSizes); | 398 ASSERT(componentSizes); |
356 | 399 |
357 TRACE_EVENT2("webkit", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); | 400 TRACE_EVENT2("webkit", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); |
358 | 401 |
359 SharedBuffer* data = 0; | 402 SharedBuffer* data = 0; |
360 bool allDataReceived = false; | 403 bool allDataReceived = false; |
361 m_data.data(&data, &allDataReceived); | 404 m_data->data(&data, &allDataReceived); |
362 | 405 |
363 // FIXME: YUV decoding does not currently support progressive decoding. | 406 // FIXME: YUV decoding does not currently support progressive decoding. |
364 if (!allDataReceived) | 407 if (!allDataReceived) |
365 return false; | 408 return false; |
366 | 409 |
367 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | 410 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
368 if (!decoder) | 411 if (!decoder) |
369 return false; | 412 return false; |
370 | 413 |
371 // JPEG images support YUV decoding: other decoders do not. So don't pump da ta into decoders | 414 // 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. | 415 // that always return false to updateYUVComponentSizes() requests. |
373 if (decoder->filenameExtension() != "jpg") | 416 if (decoder->filenameExtension() != "jpg") |
374 return false; | 417 return false; |
375 | 418 |
376 // 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. |
377 decoder->setData(data, allDataReceived); | 420 decoder->setData(data, allDataReceived); |
378 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); | 421 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); |
379 decoder->setImagePlanes(dummyImagePlanes.release()); | 422 decoder->setImagePlanes(dummyImagePlanes.release()); |
380 | 423 |
381 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder:: SizeForMemoryAllocation); | 424 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder:: SizeForMemoryAllocation); |
382 } | 425 } |
383 | 426 |
384 } // namespace blink | 427 } // namespace blink |
OLD | NEW |