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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 private: | 68 private: |
69 SkImageInfo m_info; | 69 SkImageInfo m_info; |
70 void* m_pixels; | 70 void* m_pixels; |
71 size_t m_rowBytes; | 71 size_t m_rowBytes; |
72 }; | 72 }; |
73 | 73 |
74 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha redBuffer> data, bool allDataReceived, bool isMultiFrame) | 74 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha redBuffer> data, bool allDataReceived, bool isMultiFrame) |
75 : m_fullSize(fullSize) | 75 : m_fullSize(fullSize) |
76 , m_isMultiFrame(isMultiFrame) | 76 , m_isMultiFrame(isMultiFrame) |
77 , m_decodeFailedAndEmpty(false) | 77 , m_decodeFailedAndEmpty(false) |
78 , m_acceleratedYUVDecoding(false) | |
78 , m_decodeCount(ScaledImageFragment::FirstPartialImage) | 79 , m_decodeCount(ScaledImageFragment::FirstPartialImage) |
79 , m_discardableAllocator(adoptPtr(new DiscardablePixelRefAllocator())) | 80 , m_discardableAllocator(adoptPtr(new DiscardablePixelRefAllocator())) |
80 { | 81 { |
81 setData(data.get(), allDataReceived); | 82 setData(data.get(), allDataReceived); |
82 } | 83 } |
83 | 84 |
84 ImageFrameGenerator::~ImageFrameGenerator() | 85 ImageFrameGenerator::~ImageFrameGenerator() |
85 { | 86 { |
86 ImageDecodingStore::instance()->removeCacheIndexedByGenerator(this); | 87 ImageDecodingStore::instance()->removeCacheIndexedByGenerator(this); |
87 } | 88 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 // This method is called to populate a discardable memory owned by Skia. | 127 // This method is called to populate a discardable memory owned by Skia. |
127 | 128 |
128 // Prevents concurrent decode or scale operations on the same image data. | 129 // Prevents concurrent decode or scale operations on the same image data. |
129 MutexLocker lock(m_decodeMutex); | 130 MutexLocker lock(m_decodeMutex); |
130 | 131 |
131 // This implementation does not support scaling so check the requested size. | 132 // This implementation does not support scaling so check the requested size. |
132 SkISize scaledSize = SkISize::Make(info.fWidth, info.fHeight); | 133 SkISize scaledSize = SkISize::Make(info.fWidth, info.fHeight); |
133 ASSERT(m_fullSize == scaledSize); | 134 ASSERT(m_fullSize == scaledSize); |
134 | 135 |
135 if (m_decodeFailedAndEmpty) | 136 if (m_decodeFailedAndEmpty) |
136 return 0; | 137 return false; |
137 | 138 |
138 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeAndScale", "generator", th is, "decodeCount", static_cast<int>(m_decodeCount)); | 139 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeAndScale", "generator", th is, "decodeCount", static_cast<int>(m_decodeCount)); |
139 | 140 |
140 // Don't use discardable memory for decoding if Skia is providing output | 141 // Don't use discardable memory for decoding if Skia is providing output |
141 // memory. Instead use ExternalMemoryAllocator such that we can | 142 // memory. Instead use ExternalMemoryAllocator such that we can |
142 // write directly to the memory given by Skia. | 143 // write directly to the memory given by Skia. |
143 // | 144 // |
144 // TODO: | 145 // TODO: |
145 // This is not pretty because this class is used in two different code | 146 // This is not pretty because this class is used in two different code |
146 // paths: discardable memory decoding on Android and discardable memory | 147 // paths: discardable memory decoding on Android and discardable memory |
(...skipping 15 matching lines...) Expand all Loading... | |
162 | 163 |
163 bool result = true; | 164 bool result = true; |
164 // Check to see if decoder has written directly to the memory provided | 165 // Check to see if decoder has written directly to the memory provided |
165 // by Skia. If not make a copy. | 166 // by Skia. If not make a copy. |
166 if (cachedImage->bitmap().getPixels() != pixels) | 167 if (cachedImage->bitmap().getPixels() != pixels) |
167 result = cachedImage->bitmap().copyPixelsTo(pixels, rowBytes * info.fHei ght, rowBytes); | 168 result = cachedImage->bitmap().copyPixelsTo(pixels, rowBytes * info.fHei ght, rowBytes); |
168 ImageDecodingStore::instance()->unlockCache(this, cachedImage); | 169 ImageDecodingStore::instance()->unlockCache(this, cachedImage); |
169 return result; | 170 return result; |
170 } | 171 } |
171 | 172 |
173 bool ImageFrameGenerator::decodeAndScale(void* planes[3], size_t rowBytes[3]) | |
174 { | |
175 // This method is called to populate a discardable memory owned by Skia. | |
176 | |
177 // Prevents concurrent decode or scale operations on the same image data. | |
178 MutexLocker lock(m_decodeMutex); | |
179 | |
180 if (m_decodeFailedAndEmpty) | |
181 return false; | |
182 | |
183 TRACE_EVENT2("blink", "ImageFrameGenerator::decodeAndScale", "generator", th is, "decodeCount", static_cast<int>(m_decodeCount)); | |
184 | |
185 m_acceleratedYUVDecoding = (0 != planes) && (0 != planes[0]) && (0 != planes [1]) && (0 != planes[2]) && (0 != rowBytes) && (0 != rowBytes[0]) && (0 != rowBy tes[1]) && (0 != rowBytes[2]); | |
186 if (!m_acceleratedYUVDecoding) { | |
187 return false; | |
188 } | |
189 | |
190 DecodingBuffers* decodingBuffers = new DecodingBuffers; | |
191 decodingBuffers->set(planes, rowBytes); | |
192 m_decodingBuffers = adoptPtr(decodingBuffers); | |
193 | |
194 ImageDecoder* decoder = 0; | |
195 const bool isCached = ImageDecodingStore::instance()->lockDecoder(this, m_fu llSize, &decoder); | |
196 ASSERT(!isCached || decoder); | |
197 | |
198 decode(0, &decoder); | |
199 | |
200 if (isCached) { | |
201 ImageDecodingStore::instance()->unlockDecoder(this, decoder); | |
202 } | |
203 | |
204 return m_acceleratedYUVDecoding; | |
205 } | |
206 | |
172 const ScaledImageFragment* ImageFrameGenerator::tryToLockCompleteCache(const SkI Size& scaledSize, size_t index) | 207 const ScaledImageFragment* ImageFrameGenerator::tryToLockCompleteCache(const SkI Size& scaledSize, size_t index) |
173 { | 208 { |
174 const ScaledImageFragment* cachedImage = 0; | 209 const ScaledImageFragment* cachedImage = 0; |
175 if (ImageDecodingStore::instance()->lockCache(this, scaledSize, index, &cach edImage)) | 210 if (ImageDecodingStore::instance()->lockCache(this, scaledSize, index, &cach edImage)) |
176 return cachedImage; | 211 return cachedImage; |
177 return 0; | 212 return 0; |
178 } | 213 } |
179 | 214 |
180 const ScaledImageFragment* ImageFrameGenerator::tryToResumeDecode(const SkISize& scaledSize, size_t index) | 215 const ScaledImageFragment* ImageFrameGenerator::tryToResumeDecode(const SkISize& scaledSize, size_t index) |
181 { | 216 { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 if (!m_isMultiFrame && newDecoder && allDataReceived) { | 290 if (!m_isMultiFrame && newDecoder && allDataReceived) { |
256 // If we're using an external memory allocator that means we're decoding | 291 // If we're using an external memory allocator that means we're decoding |
257 // directly into the output memory and we can save one memcpy. | 292 // directly into the output memory and we can save one memcpy. |
258 canSkipBitmapCopy = true; | 293 canSkipBitmapCopy = true; |
259 if (m_externalAllocator) | 294 if (m_externalAllocator) |
260 (*decoder)->setMemoryAllocator(m_externalAllocator.get()); | 295 (*decoder)->setMemoryAllocator(m_externalAllocator.get()); |
261 else | 296 else |
262 (*decoder)->setMemoryAllocator(m_discardableAllocator.get()); | 297 (*decoder)->setMemoryAllocator(m_discardableAllocator.get()); |
263 } | 298 } |
264 (*decoder)->setData(data, allDataReceived); | 299 (*decoder)->setData(data, allDataReceived); |
300 if (m_acceleratedYUVDecoding) { | |
301 (*decoder)->setDecodingBuffers(m_decodingBuffers); | |
302 m_acceleratedYUVDecoding = (*decoder)->doAcceleratedYUVDecoding(); | |
303 return nullptr; | |
304 } | |
265 // If this call returns a newly allocated DiscardablePixelRef, then | 305 // If this call returns a newly allocated DiscardablePixelRef, then |
266 // ImageFrame::m_bitmap and the contained DiscardablePixelRef are locked. | 306 // ImageFrame::m_bitmap and the contained DiscardablePixelRef are locked. |
267 // They will be unlocked when ImageDecoder is destroyed since ImageDecoder | 307 // They will be unlocked when ImageDecoder is destroyed since ImageDecoder |
268 // owns the ImageFrame. Partially decoded SkBitmap is thus inserted into the | 308 // owns the ImageFrame. Partially decoded SkBitmap is thus inserted into the |
269 // ImageDecodingStore while locked. | 309 // ImageDecodingStore while locked. |
270 ImageFrame* frame = (*decoder)->frameBufferAtIndex(index); | 310 ImageFrame* frame = (*decoder)->frameBufferAtIndex(index); |
271 (*decoder)->setData(0, false); // Unref SharedBuffer from ImageDecoder. | 311 (*decoder)->setData(0, false); // Unref SharedBuffer from ImageDecoder. |
272 (*decoder)->clearCacheExceptFrame(index); | 312 (*decoder)->clearCacheExceptFrame(index); |
273 (*decoder)->setMemoryAllocator(0); | 313 (*decoder)->setMemoryAllocator(0); |
274 | 314 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 } | 357 } |
318 | 358 |
319 bool ImageFrameGenerator::hasAlpha(size_t index) | 359 bool ImageFrameGenerator::hasAlpha(size_t index) |
320 { | 360 { |
321 MutexLocker lock(m_alphaMutex); | 361 MutexLocker lock(m_alphaMutex); |
322 if (index < m_hasAlpha.size()) | 362 if (index < m_hasAlpha.size()) |
323 return m_hasAlpha[index]; | 363 return m_hasAlpha[index]; |
324 return true; | 364 return true; |
325 } | 365 } |
326 | 366 |
367 bool ImageFrameGenerator::getComponentSizes(SkISize componentSizes[3]) | |
368 { | |
369 if (!componentSizes) { | |
370 return false; | |
371 } | |
372 | |
373 TRACE_EVENT2("webkit", "ImageFrameGenerator::getImageFormat", "width", m_ful lSize.width(), "height", m_fullSize.height()); | |
374 | |
375 ImageDecoder* decoder = 0; | |
Peter Kasting
2014/07/24 18:08:14
Nit: Seems like this block should move down below
| |
376 const bool isCached = ImageDecodingStore::instance()->lockDecoder(this, m_fu llSize, &decoder); | |
377 ASSERT(!isCached || decoder); | |
378 | |
379 SharedBuffer* data = 0; | |
380 bool allDataReceived = false; | |
381 m_data.data(&data, &allDataReceived); | |
382 | |
383 // If the decoder isn't cached, take ownership of it here | |
384 OwnPtr<ImageDecoder> decoderContainer; | |
385 if (!isCached) | |
386 decoderContainer = adoptPtr(decoder); | |
387 | |
388 if (!decoder) { | |
389 if (m_imageDecoderFactory) | |
390 decoderContainer = adoptPtr(m_imageDecoderFactory->create().leakPtr( )); | |
Peter Kasting
2014/07/24 18:08:14
Do we actually have to call leakPtr() explicitly h
| |
391 | |
392 if (!decoderContainer.get()) | |
393 decoderContainer = adoptPtr(ImageDecoder::create(*data, ImageSource: :AlphaPremultiplied, ImageSource::GammaAndColorProfileApplied).leakPtr()); | |
394 | |
395 decoder = decoderContainer.get(); | |
396 | |
397 if (!decoder) | |
398 return false; | |
399 } | |
400 | |
401 decoder->setData(data, allDataReceived); | |
402 decoder->setAcceleratedYUVDecoding(true); | |
403 bool canComputeSize = decoder->acceleratedYUVDecoding() && decoder->isSizeAv ailable(); | |
404 if (canComputeSize) { | |
405 IntSize size = decoder->decodedSize(0); | |
406 componentSizes[0].set(size.width(), size.height()); | |
407 size = decoder->decodedSize(1); | |
408 componentSizes[1].set(size.width(), size.height()); | |
409 size = decoder->decodedSize(2); | |
410 componentSizes[2].set(size.width(), size.height()); | |
411 } | |
412 decoder->setData(0, false); // Unref SharedBuffer from ImageDecoder. | |
413 if (isCached) | |
414 ImageDecodingStore::instance()->unlockDecoder(this, decoder); | |
415 return canComputeSize; | |
416 } | |
417 | |
327 } // namespace | 418 } // namespace |
OLD | NEW |