| 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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 decoder_color_behavior_(color_behavior), | 123 decoder_color_behavior_(color_behavior), |
| 124 is_multi_frame_(is_multi_frame), | 124 is_multi_frame_(is_multi_frame), |
| 125 decode_failed_(false), | 125 decode_failed_(false), |
| 126 yuv_decoding_failed_(false), | 126 yuv_decoding_failed_(false), |
| 127 frame_count_(0) {} | 127 frame_count_(0) {} |
| 128 | 128 |
| 129 ImageFrameGenerator::~ImageFrameGenerator() { | 129 ImageFrameGenerator::~ImageFrameGenerator() { |
| 130 ImageDecodingStore::Instance().RemoveCacheIndexedByGenerator(this); | 130 ImageDecodingStore::Instance().RemoveCacheIndexedByGenerator(this); |
| 131 } | 131 } |
| 132 | 132 |
| 133 bool ImageFrameGenerator::DecodeAndScale(SegmentReader* data, | 133 bool ImageFrameGenerator::DecodeAndScale( |
| 134 bool all_data_received, | 134 SegmentReader* data, |
| 135 size_t index, | 135 bool all_data_received, |
| 136 const SkImageInfo& info, | 136 size_t index, |
| 137 void* pixels, | 137 const SkImageInfo& info, |
| 138 size_t row_bytes) { | 138 void* pixels, |
| 139 size_t row_bytes, |
| 140 ImageDecoder::AlphaOption alpha_option) { |
| 139 if (decode_failed_) | 141 if (decode_failed_) |
| 140 return false; | 142 return false; |
| 141 | 143 |
| 142 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", | 144 TRACE_EVENT1("blink", "ImageFrameGenerator::decodeAndScale", "frame index", |
| 143 static_cast<int>(index)); | 145 static_cast<int>(index)); |
| 144 | 146 |
| 145 // This implementation does not support scaling so check the requested size. | 147 // This implementation does not support scaling so check the requested size. |
| 146 SkISize scaled_size = SkISize::Make(info.width(), info.height()); | 148 SkISize scaled_size = SkISize::Make(info.width(), info.height()); |
| 147 DCHECK(full_size_ == scaled_size); | 149 DCHECK(full_size_ == scaled_size); |
| 148 | 150 |
| 149 // It is okay to allocate ref-counted ExternalMemoryAllocator on the stack, | 151 // It is okay to allocate ref-counted ExternalMemoryAllocator on the stack, |
| 150 // because 1) it contains references to memory that will be invalid after | 152 // because 1) it contains references to memory that will be invalid after |
| 151 // returning (i.e. a pointer to |pixels|) and therefore 2) should not live | 153 // returning (i.e. a pointer to |pixels|) and therefore 2) should not live |
| 152 // longer than the call to the current method. | 154 // longer than the call to the current method. |
| 153 ExternalMemoryAllocator external_allocator(info, pixels, row_bytes); | 155 ExternalMemoryAllocator external_allocator(info, pixels, row_bytes); |
| 154 SkBitmap bitmap = TryToResumeDecode(data, all_data_received, index, | 156 SkBitmap bitmap = |
| 155 scaled_size, &external_allocator); | 157 TryToResumeDecode(data, all_data_received, index, scaled_size, |
| 158 &external_allocator, alpha_option); |
| 156 DCHECK(external_allocator.unique()); // Verify we have the only ref-count. | 159 DCHECK(external_allocator.unique()); // Verify we have the only ref-count. |
| 157 | 160 |
| 158 if (bitmap.isNull()) | 161 if (bitmap.isNull()) |
| 159 return false; | 162 return false; |
| 160 | 163 |
| 161 // Check to see if the decoder has written directly to the pixel memory | 164 // Check to see if the decoder has written directly to the pixel memory |
| 162 // provided. If not, make a copy. | 165 // provided. If not, make a copy. |
| 163 DCHECK_EQ(bitmap.width(), scaled_size.width()); | 166 DCHECK_EQ(bitmap.width(), scaled_size.width()); |
| 164 DCHECK_EQ(bitmap.height(), scaled_size.height()); | 167 DCHECK_EQ(bitmap.height(), scaled_size.height()); |
| 165 SkAutoLockPixels bitmap_lock(bitmap); | 168 SkAutoLockPixels bitmap_lock(bitmap); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 DCHECK(decoder->Failed()); | 210 DCHECK(decoder->Failed()); |
| 208 yuv_decoding_failed_ = true; | 211 yuv_decoding_failed_ = true; |
| 209 return false; | 212 return false; |
| 210 } | 213 } |
| 211 | 214 |
| 212 SkBitmap ImageFrameGenerator::TryToResumeDecode( | 215 SkBitmap ImageFrameGenerator::TryToResumeDecode( |
| 213 SegmentReader* data, | 216 SegmentReader* data, |
| 214 bool all_data_received, | 217 bool all_data_received, |
| 215 size_t index, | 218 size_t index, |
| 216 const SkISize& scaled_size, | 219 const SkISize& scaled_size, |
| 217 SkBitmap::Allocator* allocator) { | 220 SkBitmap::Allocator* allocator, |
| 221 ImageDecoder::AlphaOption alpha_option) { |
| 218 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index", | 222 TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecode", "frame index", |
| 219 static_cast<int>(index)); | 223 static_cast<int>(index)); |
| 220 | 224 |
| 221 ImageDecoder* decoder = 0; | 225 ImageDecoder* decoder = 0; |
| 222 | 226 |
| 223 // Lock the mutex, so only one thread can use the decoder at once. | 227 // Lock the mutex, so only one thread can use the decoder at once. |
| 224 MutexLocker lock(decode_mutex_); | 228 MutexLocker lock(decode_mutex_); |
| 225 const bool resume_decoding = | 229 const bool resume_decoding = ImageDecodingStore::Instance().LockDecoder( |
| 226 ImageDecodingStore::Instance().LockDecoder(this, full_size_, &decoder); | 230 this, full_size_, alpha_option, &decoder); |
| 227 DCHECK(!resume_decoding || decoder); | 231 DCHECK(!resume_decoding || decoder); |
| 228 | 232 |
| 229 SkBitmap full_size_image; | 233 SkBitmap full_size_image; |
| 230 bool complete = Decode(data, all_data_received, index, &decoder, | 234 bool complete = Decode(data, all_data_received, index, &decoder, |
| 231 &full_size_image, allocator); | 235 &full_size_image, allocator, alpha_option); |
| 232 | 236 |
| 233 if (!decoder) | 237 if (!decoder) |
| 234 return SkBitmap(); | 238 return SkBitmap(); |
| 235 | 239 |
| 236 // If we are not resuming decoding that means the decoder is freshly | 240 // If we are not resuming decoding that means the decoder is freshly |
| 237 // created and we have ownership. If we are resuming decoding then | 241 // created and we have ownership. If we are resuming decoding then |
| 238 // the decoder is owned by ImageDecodingStore. | 242 // the decoder is owned by ImageDecodingStore. |
| 239 std::unique_ptr<ImageDecoder> decoder_container; | 243 std::unique_ptr<ImageDecoder> decoder_container; |
| 240 if (!resume_decoding) | 244 if (!resume_decoding) |
| 241 decoder_container = WTF::WrapUnique(decoder); | 245 decoder_container = WTF::WrapUnique(decoder); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 has_alpha_[i] = true; | 290 has_alpha_[i] = true; |
| 287 } | 291 } |
| 288 has_alpha_[index] = has_alpha; | 292 has_alpha_[index] = has_alpha; |
| 289 } | 293 } |
| 290 | 294 |
| 291 bool ImageFrameGenerator::Decode(SegmentReader* data, | 295 bool ImageFrameGenerator::Decode(SegmentReader* data, |
| 292 bool all_data_received, | 296 bool all_data_received, |
| 293 size_t index, | 297 size_t index, |
| 294 ImageDecoder** decoder, | 298 ImageDecoder** decoder, |
| 295 SkBitmap* bitmap, | 299 SkBitmap* bitmap, |
| 296 SkBitmap::Allocator* allocator) { | 300 SkBitmap::Allocator* allocator, |
| 301 ImageDecoder::AlphaOption alpha_option) { |
| 297 #if DCHECK_IS_ON() | 302 #if DCHECK_IS_ON() |
| 298 DCHECK(decode_mutex_.Locked()); | 303 DCHECK(decode_mutex_.Locked()); |
| 299 #endif | 304 #endif |
| 300 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", | 305 TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", |
| 301 full_size_.width(), "height", full_size_.height()); | 306 full_size_.width(), "height", full_size_.height()); |
| 302 | 307 |
| 303 // Try to create an ImageDecoder if we are not given one. | 308 // Try to create an ImageDecoder if we are not given one. |
| 304 DCHECK(decoder); | 309 DCHECK(decoder); |
| 305 bool new_decoder = false; | 310 bool new_decoder = false; |
| 306 bool should_call_set_data = true; | 311 bool should_call_set_data = true; |
| 307 if (!*decoder) { | 312 if (!*decoder) { |
| 308 new_decoder = true; | 313 new_decoder = true; |
| 309 if (image_decoder_factory_) | 314 if (image_decoder_factory_) |
| 310 *decoder = image_decoder_factory_->Create().release(); | 315 *decoder = image_decoder_factory_->Create().release(); |
| 311 | 316 |
| 312 if (!*decoder) { | 317 if (!*decoder) { |
| 313 *decoder = ImageDecoder::Create(data, all_data_received, | 318 *decoder = ImageDecoder::Create(data, all_data_received, alpha_option, |
| 314 ImageDecoder::kAlphaPremultiplied, | |
| 315 decoder_color_behavior_) | 319 decoder_color_behavior_) |
| 316 .release(); | 320 .release(); |
| 317 // The newly created decoder just grabbed the data. No need to reset it. | 321 // The newly created decoder just grabbed the data. No need to reset it. |
| 318 should_call_set_data = false; | 322 should_call_set_data = false; |
| 319 } | 323 } |
| 320 | 324 |
| 321 if (!*decoder) | 325 if (!*decoder) |
| 322 return false; | 326 return false; |
| 323 } | 327 } |
| 324 | 328 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 // do YUV decoding. | 401 // do YUV decoding. |
| 398 std::unique_ptr<ImagePlanes> dummy_image_planes = | 402 std::unique_ptr<ImagePlanes> dummy_image_planes = |
| 399 WTF::WrapUnique(new ImagePlanes); | 403 WTF::WrapUnique(new ImagePlanes); |
| 400 decoder->SetImagePlanes(std::move(dummy_image_planes)); | 404 decoder->SetImagePlanes(std::move(dummy_image_planes)); |
| 401 | 405 |
| 402 return UpdateYUVComponentSizes(decoder.get(), size_info->fSizes, | 406 return UpdateYUVComponentSizes(decoder.get(), size_info->fSizes, |
| 403 size_info->fWidthBytes); | 407 size_info->fWidthBytes); |
| 404 } | 408 } |
| 405 | 409 |
| 406 } // namespace blink | 410 } // namespace blink |
| OLD | NEW |