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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 componentSizes[0].set(size.width(), size.height()); | 93 componentSizes[0].set(size.width(), size.height()); |
93 size = decoder->decodedYUVSize(1, sizeType); | 94 size = decoder->decodedYUVSize(1, sizeType); |
94 componentSizes[1].set(size.width(), size.height()); | 95 componentSizes[1].set(size.width(), size.height()); |
95 size = decoder->decodedYUVSize(2, sizeType); | 96 size = decoder->decodedYUVSize(2, sizeType); |
96 componentSizes[2].set(size.width(), size.height()); | 97 componentSizes[2].set(size.width(), size.height()); |
97 return true; | 98 return true; |
98 } | 99 } |
99 | 100 |
100 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha redBuffer> data, bool allDataReceived, bool isMultiFrame) | 101 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha redBuffer> data, bool allDataReceived, bool isMultiFrame) |
101 : m_fullSize(fullSize) | 102 : m_fullSize(fullSize) |
103 , m_data(new ThreadSafeDataTransport()) | |
102 , m_isMultiFrame(isMultiFrame) | 104 , m_isMultiFrame(isMultiFrame) |
103 , m_decodeFailedAndEmpty(false) | 105 , m_decodeFailedAndEmpty(false) |
104 , m_decodeCount(0) | 106 , m_decodeCount(0) |
105 , m_frameCount(0) | 107 , m_frameCount(0) |
106 { | 108 { |
107 setData(data.get(), allDataReceived); | 109 setData(data.get(), allDataReceived); |
108 } | 110 } |
109 | 111 |
110 ImageFrameGenerator::~ImageFrameGenerator() | 112 ImageFrameGenerator::~ImageFrameGenerator() |
111 { | 113 { |
112 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); | 114 ImageDecodingStore::instance().removeCacheIndexedByGenerator(this); |
113 } | 115 } |
114 | 116 |
115 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec eived) | 117 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec eived) |
116 { | 118 { |
117 m_data.setData(data.get(), allDataReceived); | 119 m_data->setData(data.get(), allDataReceived); |
118 } | 120 } |
119 | 121 |
120 void ImageFrameGenerator::copyData(RefPtr<SharedBuffer>* data, bool* allDataRece ived) | 122 static void sharedBufSkDataReleaseProc(const void* addr, void* ctx) |
121 { | 123 { |
124 ThreadSafeDataTransport* someonesMData = static_cast<ThreadSafeDataTransport *>(ctx); | |
125 ASSERT(someonesMData); | |
126 // Deref m_data now. | |
127 someonesMData->deref(); | |
128 } | |
129 | |
130 SkData* ImageFrameGenerator::refEncodedData() | |
131 { | |
132 // Every SkData instance and SharedBuffer need to keep buffer->data() refcou nted. | |
133 // Both SkData and m_data.m_readBuffer are having separate ref counting impl ementations. | |
134 // | |
135 // SkData's SkData::NewWithProc is designed with similar use case in mind, | |
136 // unmapping wrapped shmem data once all SkData instances are disposed. | |
137 // in this case, sharedBufSkDataReleaseProc would just need to deref (since m_data.m_readBuffer | |
138 // might still hold a reference) and that is happening in sharedBufSkDataRel easeProc. | |
139 // | |
140 // Preventing m_data disposal if ImageFrameGenerator is thread safe - SkData could get disposed | |
141 // in any thread, and SkData would still need to hold the ref to SharedBuffe r if ImageFrameGenerator | |
aleksandar.stojiljkovic
2015/12/03 18:04:14
I tried to cover it here, please help on reformula
chrishtr
2015/12/03 18:32:00
Oh I see now, thanks. I didn't notice the change t
| |
142 // is deleted. This is why full m_data (ThreadSafeRefCounted) is passed to c lient - to hold the | |
143 // reference to SharedBuffer (accompanying returned SkData). By contract in Skia's SkData, | |
144 // client needs to call SkData unref that would end up in sharedBufSkDataRel easeProc | |
145 // releasing the ref to ThreadSafeDataTransport (in thread safe way) and und erlying SharedBuffer. | |
146 // | |
147 // A note about allDataReceived: | |
148 // Client side use case is valid only for full image (encoded) data download ed, | |
149 // but, incidentally, this is in line with current Chromium implementation, where | |
150 // DeferredImageDecoder::setData is called only once with allDataReceived an d after | |
151 // that m_data.m_readBuffer.data() is not changed. For the sake of not leavi ng loose ends, | |
152 // ThreadSafeDataTransport::data is checking if there is new data added afte r AllDataReceived | |
153 // was set to true. | |
122 SharedBuffer* buffer = 0; | 154 SharedBuffer* buffer = 0; |
123 m_data.data(&buffer, allDataReceived); | 155 bool allDataReceived = false; |
124 if (buffer) | 156 m_data->data(&buffer, &allDataReceived); |
125 *data = buffer->copy(); | 157 if (!allDataReceived) |
158 return nullptr; | |
159 | |
160 // While SkData is holding reference to underlying data, prevent disposing m _data and its content. | |
161 m_data->ref(); | |
162 return SkData::NewWithProc(buffer->data(), buffer->size(), sharedBufSkDataRe leaseProc, m_data.get()); | |
126 } | 163 } |
127 | 164 |
128 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes) | 165 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes) |
129 { | 166 { |
130 // This method is called to populate a discardable memory owned by Skia. | 167 // This method is called to populate a discardable memory owned by Skia. |
131 | 168 |
132 // Prevents concurrent decode or scale operations on the same image data. | 169 // Prevents concurrent decode or scale operations on the same image data. |
133 MutexLocker lock(m_decodeMutex); | 170 MutexLocker lock(m_decodeMutex); |
134 | 171 |
135 // This implementation does not support scaling so check the requested size. | 172 // This implementation does not support scaling so check the requested size. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 | 212 |
176 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeToYUV", "generator", this, "decodeCount", static_cast<int>(m_decodeCount)); | 213 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeToYUV", "generator", this, "decodeCount", static_cast<int>(m_decodeCount)); |
177 | 214 |
178 if (!planes || !planes[0] || !planes[1] || !planes[2] | 215 if (!planes || !planes[0] || !planes[1] || !planes[2] |
179 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { | 216 || !rowBytes || !rowBytes[0] || !rowBytes[1] || !rowBytes[2]) { |
180 return false; | 217 return false; |
181 } | 218 } |
182 | 219 |
183 SharedBuffer* data = 0; | 220 SharedBuffer* data = 0; |
184 bool allDataReceived = false; | 221 bool allDataReceived = false; |
185 m_data.data(&data, &allDataReceived); | 222 m_data->data(&data, &allDataReceived); |
186 | 223 |
187 // FIXME: YUV decoding does not currently support progressive decoding. | 224 // FIXME: YUV decoding does not currently support progressive decoding. |
188 ASSERT(allDataReceived); | 225 ASSERT(allDataReceived); |
189 | 226 |
190 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | 227 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
191 if (!decoder) | 228 if (!decoder) |
192 return false; | 229 return false; |
193 | 230 |
194 decoder->setData(data, allDataReceived); | 231 decoder->setData(data, allDataReceived); |
195 | 232 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 } | 318 } |
282 | 319 |
283 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap) | 320 bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap) |
284 { | 321 { |
285 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); | 322 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.wid th(), "height", m_fullSize.height()); |
286 | 323 |
287 ASSERT(decoder); | 324 ASSERT(decoder); |
288 SharedBuffer* data = 0; | 325 SharedBuffer* data = 0; |
289 bool allDataReceived = false; | 326 bool allDataReceived = false; |
290 bool newDecoder = false; | 327 bool newDecoder = false; |
291 m_data.data(&data, &allDataReceived); | 328 m_data->data(&data, &allDataReceived); |
292 | 329 |
293 // Try to create an ImageDecoder if we are not given one. | 330 // Try to create an ImageDecoder if we are not given one. |
294 if (!*decoder) { | 331 if (!*decoder) { |
295 newDecoder = true; | 332 newDecoder = true; |
296 if (m_imageDecoderFactory) | 333 if (m_imageDecoderFactory) |
297 *decoder = m_imageDecoderFactory->create().leakPtr(); | 334 *decoder = m_imageDecoderFactory->create().leakPtr(); |
298 | 335 |
299 if (!*decoder) | 336 if (!*decoder) |
300 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); | 337 *decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultipl ied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); |
301 | 338 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
349 } | 386 } |
350 | 387 |
351 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) | 388 bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) |
352 { | 389 { |
353 ASSERT(componentSizes); | 390 ASSERT(componentSizes); |
354 | 391 |
355 TRACE_EVENT2("webkit", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); | 392 TRACE_EVENT2("webkit", "ImageFrameGenerator::getYUVComponentSizes", "width", m_fullSize.width(), "height", m_fullSize.height()); |
356 | 393 |
357 SharedBuffer* data = 0; | 394 SharedBuffer* data = 0; |
358 bool allDataReceived = false; | 395 bool allDataReceived = false; |
359 m_data.data(&data, &allDataReceived); | 396 m_data->data(&data, &allDataReceived); |
360 | 397 |
361 // FIXME: YUV decoding does not currently support progressive decoding. | 398 // FIXME: YUV decoding does not currently support progressive decoding. |
362 if (!allDataReceived) | 399 if (!allDataReceived) |
363 return false; | 400 return false; |
364 | 401 |
365 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); | 402 OwnPtr<ImageDecoder> decoder = ImageDecoder::create(*data, ImageDecoder::Alp haPremultiplied, ImageDecoder::GammaAndColorProfileApplied); |
366 if (!decoder) | 403 if (!decoder) |
367 return false; | 404 return false; |
368 | 405 |
369 // JPEG images support YUV decoding: other decoders do not. So don't pump da ta into decoders | 406 // JPEG images support YUV decoding: other decoders do not. So don't pump da ta into decoders |
370 // that always return false to updateYUVComponentSizes() requests. | 407 // that always return false to updateYUVComponentSizes() requests. |
371 if (decoder->filenameExtension() != "jpg") | 408 if (decoder->filenameExtension() != "jpg") |
372 return false; | 409 return false; |
373 | 410 |
374 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding. | 411 // Setting a dummy ImagePlanes object signals to the decoder that we want to do YUV decoding. |
375 decoder->setData(data, allDataReceived); | 412 decoder->setData(data, allDataReceived); |
376 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); | 413 OwnPtr<ImagePlanes> dummyImagePlanes = adoptPtr(new ImagePlanes); |
377 decoder->setImagePlanes(dummyImagePlanes.release()); | 414 decoder->setImagePlanes(dummyImagePlanes.release()); |
378 | 415 |
379 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder:: SizeForMemoryAllocation); | 416 return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder:: SizeForMemoryAllocation); |
380 } | 417 } |
381 | 418 |
382 } // namespace blink | 419 } // namespace blink |
OLD | NEW |