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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 decoder_color_behavior_(color_behavior), | 110 decoder_color_behavior_(color_behavior), |
111 is_multi_frame_(is_multi_frame), | 111 is_multi_frame_(is_multi_frame), |
112 decode_failed_(false), | 112 decode_failed_(false), |
113 yuv_decoding_failed_(false), | 113 yuv_decoding_failed_(false), |
114 frame_count_(0) {} | 114 frame_count_(0) {} |
115 | 115 |
116 ImageFrameGenerator::~ImageFrameGenerator() { | 116 ImageFrameGenerator::~ImageFrameGenerator() { |
117 ImageDecodingStore::Instance().RemoveCacheIndexedByGenerator(this); | 117 ImageDecodingStore::Instance().RemoveCacheIndexedByGenerator(this); |
118 } | 118 } |
119 | 119 |
120 bool ImageFrameGenerator::DecodeAndScale(SegmentReader* data, | 120 bool ImageFrameGenerator::DecodeAndScale( |
121 bool all_data_received, | 121 SegmentReader* data, |
122 size_t index, | 122 bool all_data_received, |
123 const SkImageInfo& info, | 123 size_t index, |
124 void* pixels, | 124 const SkImageInfo& info, |
125 size_t row_bytes) { | 125 void* pixels, |
| 126 size_t row_bytes, |
| 127 ImageDecoder::AlphaOption alpha_option) { |
126 if (decode_failed_) | 128 if (decode_failed_) |
127 return false; | 129 return false; |
128 | 130 |
129 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", | 131 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", |
130 static_cast<int>(index)); | 132 static_cast<int>(index)); |
131 | 133 |
132 // This implementation does not support scaling so check the requested size. | 134 // This implementation does not support scaling so check the requested size. |
133 SkISize scaled_size = SkISize::Make(info.width(), info.height()); | 135 SkISize scaled_size = SkISize::Make(info.width(), info.height()); |
134 ASSERT(full_size_ == scaled_size); | 136 ASSERT(full_size_ == scaled_size); |
135 | 137 |
136 // It is okay to allocate ref-counted ExternalMemoryAllocator on the stack, | 138 // It is okay to allocate ref-counted ExternalMemoryAllocator on the stack, |
137 // because 1) it contains references to memory that will be invalid after | 139 // because 1) it contains references to memory that will be invalid after |
138 // returning (i.e. a pointer to |pixels|) and therefore 2) should not live | 140 // returning (i.e. a pointer to |pixels|) and therefore 2) should not live |
139 // longer than the call to the current method. | 141 // longer than the call to the current method. |
140 ExternalMemoryAllocator external_allocator(info, pixels, row_bytes); | 142 ExternalMemoryAllocator external_allocator(info, pixels, row_bytes); |
141 SkBitmap bitmap = TryToResumeDecode(data, all_data_received, index, | 143 SkBitmap bitmap = |
142 scaled_size, &external_allocator); | 144 TryToResumeDecode(data, all_data_received, index, scaled_size, |
| 145 &external_allocator, alpha_option); |
143 DCHECK(external_allocator.unique()); // Verify we have the only ref-count. | 146 DCHECK(external_allocator.unique()); // Verify we have the only ref-count. |
144 | 147 |
145 if (bitmap.isNull()) | 148 if (bitmap.isNull()) |
146 return false; | 149 return false; |
147 | 150 |
148 // Check to see if the decoder has written directly to the pixel memory | 151 // Check to see if the decoder has written directly to the pixel memory |
149 // provided. If not, make a copy. | 152 // provided. If not, make a copy. |
150 ASSERT(bitmap.width() == scaled_size.width()); | 153 ASSERT(bitmap.width() == scaled_size.width()); |
151 ASSERT(bitmap.height() == scaled_size.height()); | 154 ASSERT(bitmap.height() == scaled_size.height()); |
152 SkAutoLockPixels bitmap_lock(bitmap); | 155 SkAutoLockPixels bitmap_lock(bitmap); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 ASSERT(decoder->Failed()); | 197 ASSERT(decoder->Failed()); |
195 yuv_decoding_failed_ = true; | 198 yuv_decoding_failed_ = true; |
196 return false; | 199 return false; |
197 } | 200 } |
198 | 201 |
199 SkBitmap ImageFrameGenerator::TryToResumeDecode( | 202 SkBitmap ImageFrameGenerator::TryToResumeDecode( |
200 SegmentReader* data, | 203 SegmentReader* data, |
201 bool all_data_received, | 204 bool all_data_received, |
202 size_t index, | 205 size_t index, |
203 const SkISize& scaled_size, | 206 const SkISize& scaled_size, |
204 SkBitmap::Allocator* allocator) { | 207 SkBitmap::Allocator* allocator, |
| 208 ImageDecoder::AlphaOption alpha_option) { |
205 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index", | 209 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index", |
206 static_cast<int>(index)); | 210 static_cast<int>(index)); |
207 | 211 |
208 ImageDecoder* decoder = 0; | 212 ImageDecoder* decoder = 0; |
209 | 213 |
210 // Lock the mutex, so only one thread can use the decoder at once. | 214 // Lock the mutex, so only one thread can use the decoder at once. |
211 MutexLocker lock(decode_mutex_); | 215 MutexLocker lock(decode_mutex_); |
212 const bool resume_decoding = | 216 const bool resume_decoding = ImageDecodingStore::Instance().LockDecoder( |
213 ImageDecodingStore::Instance().LockDecoder(this, full_size_, &decoder); | 217 this, full_size_, alpha_option, &decoder); |
214 ASSERT(!resume_decoding || decoder); | 218 ASSERT(!resume_decoding || decoder); |
215 | 219 |
216 SkBitmap full_size_image; | 220 SkBitmap full_size_image; |
217 bool complete = Decode(data, all_data_received, index, &decoder, | 221 bool complete = Decode(data, all_data_received, index, &decoder, |
218 &full_size_image, allocator); | 222 &full_size_image, allocator, alpha_option); |
219 | 223 |
220 if (!decoder) | 224 if (!decoder) |
221 return SkBitmap(); | 225 return SkBitmap(); |
222 | 226 |
223 // If we are not resuming decoding that means the decoder is freshly | 227 // If we are not resuming decoding that means the decoder is freshly |
224 // created and we have ownership. If we are resuming decoding then | 228 // created and we have ownership. If we are resuming decoding then |
225 // the decoder is owned by ImageDecodingStore. | 229 // the decoder is owned by ImageDecodingStore. |
226 std::unique_ptr<ImageDecoder> decoder_container; | 230 std::unique_ptr<ImageDecoder> decoder_container; |
227 if (!resume_decoding) | 231 if (!resume_decoding) |
228 decoder_container = WTF::WrapUnique(decoder); | 232 decoder_container = WTF::WrapUnique(decoder); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 has_alpha_[i] = true; | 277 has_alpha_[i] = true; |
274 } | 278 } |
275 has_alpha_[index] = has_alpha; | 279 has_alpha_[index] = has_alpha; |
276 } | 280 } |
277 | 281 |
278 bool ImageFrameGenerator::Decode(SegmentReader* data, | 282 bool ImageFrameGenerator::Decode(SegmentReader* data, |
279 bool all_data_received, | 283 bool all_data_received, |
280 size_t index, | 284 size_t index, |
281 ImageDecoder** decoder, | 285 ImageDecoder** decoder, |
282 SkBitmap* bitmap, | 286 SkBitmap* bitmap, |
283 SkBitmap::Allocator* allocator) { | 287 SkBitmap::Allocator* allocator, |
| 288 ImageDecoder::AlphaOption alpha_option) { |
284 ASSERT(decode_mutex_.Locked()); | 289 ASSERT(decode_mutex_.Locked()); |
285 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", | 290 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", |
286 full_size_.width(), "height", full_size_.height()); | 291 full_size_.width(), "height", full_size_.height()); |
287 | 292 |
288 // Try to create an ImageDecoder if we are not given one. | 293 // Try to create an ImageDecoder if we are not given one. |
289 ASSERT(decoder); | 294 ASSERT(decoder); |
290 bool new_decoder = false; | 295 bool new_decoder = false; |
291 bool should_call_set_data = true; | 296 bool should_call_set_data = true; |
292 if (!*decoder) { | 297 if (!*decoder) { |
293 new_decoder = true; | 298 new_decoder = true; |
294 if (image_decoder_factory_) | 299 if (image_decoder_factory_) |
295 *decoder = image_decoder_factory_->Create().release(); | 300 *decoder = image_decoder_factory_->Create().release(); |
296 | 301 |
297 if (!*decoder) { | 302 if (!*decoder) { |
298 *decoder = ImageDecoder::Create(data, all_data_received, | 303 *decoder = ImageDecoder::Create(data, all_data_received, alpha_option, |
299 ImageDecoder::kAlphaPremultiplied, | |
300 decoder_color_behavior_) | 304 decoder_color_behavior_) |
301 .release(); | 305 .release(); |
302 // The newly created decoder just grabbed the data. No need to reset it. | 306 // The newly created decoder just grabbed the data. No need to reset it. |
303 should_call_set_data = false; | 307 should_call_set_data = false; |
304 } | 308 } |
305 | 309 |
306 if (!*decoder) | 310 if (!*decoder) |
307 return false; | 311 return false; |
308 } | 312 } |
309 | 313 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 // do YUV decoding. | 386 // do YUV decoding. |
383 std::unique_ptr<ImagePlanes> dummy_image_planes = | 387 std::unique_ptr<ImagePlanes> dummy_image_planes = |
384 WTF::WrapUnique(new ImagePlanes); | 388 WTF::WrapUnique(new ImagePlanes); |
385 decoder->SetImagePlanes(std::move(dummy_image_planes)); | 389 decoder->SetImagePlanes(std::move(dummy_image_planes)); |
386 | 390 |
387 return UpdateYUVComponentSizes(decoder.get(), size_info->fSizes, | 391 return UpdateYUVComponentSizes(decoder.get(), size_info->fSizes, |
388 size_info->fWidthBytes); | 392 size_info->fWidthBytes); |
389 } | 393 } |
390 | 394 |
391 } // namespace blink | 395 } // namespace blink |
OLD | NEW |