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