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 #include "platform/graphics/DeferredImageDecoder.h" | 27 #include "platform/graphics/DeferredImageDecoder.h" |
28 | 28 |
29 #include "platform/graphics/DecodingImageGenerator.h" | |
29 #include "platform/graphics/LazyDecodingPixelRef.h" | 30 #include "platform/graphics/LazyDecodingPixelRef.h" |
30 | 31 #include "third_party/skia/include/core/SkImageInfo.h" |
31 #include "wtf/PassOwnPtr.h" | 32 #include "wtf/PassOwnPtr.h" |
32 | 33 |
33 namespace WebCore { | 34 namespace WebCore { |
34 | 35 |
35 namespace { | 36 namespace { |
36 | 37 |
37 // URI label for a lazily decoded SkPixelRef. | 38 // URI label for a lazily decoded SkPixelRef. |
38 const char labelLazyDecoded[] = "lazy"; | 39 const char labelLazyDecoded[] = "lazy"; |
39 | 40 |
41 // URI label for SkDiscardablePixelRef. | |
42 const char labelDiscardable[] = "discardable"; | |
43 | |
40 } // namespace | 44 } // namespace |
41 | 45 |
42 bool DeferredImageDecoder::s_enabled = false; | 46 bool DeferredImageDecoder::s_enabled = false; |
47 bool DeferredImageDecoder::s_skiaDiscardableMemoryEnabled = false; | |
reveman
2013/12/12 16:57:11
Can we just make this true if !defined(OS_ANDROID)
| |
43 | 48 |
44 DeferredImageDecoder::DeferredImageDecoder(PassOwnPtr<ImageDecoder> actualDecode r) | 49 DeferredImageDecoder::DeferredImageDecoder(PassOwnPtr<ImageDecoder> actualDecode r) |
45 : m_allDataReceived(false) | 50 : m_allDataReceived(false) |
46 , m_actualDecoder(actualDecoder) | 51 , m_actualDecoder(actualDecoder) |
47 , m_orientation(DefaultImageOrientation) | 52 , m_orientation(DefaultImageOrientation) |
48 , m_repetitionCount(cAnimationNone) | 53 , m_repetitionCount(cAnimationNone) |
49 { | 54 { |
50 } | 55 } |
51 | 56 |
52 DeferredImageDecoder::~DeferredImageDecoder() | 57 DeferredImageDecoder::~DeferredImageDecoder() |
53 { | 58 { |
54 } | 59 } |
55 | 60 |
56 PassOwnPtr<DeferredImageDecoder> DeferredImageDecoder::create(const SharedBuffer & data, ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileO ption gammaAndColorOption) | 61 PassOwnPtr<DeferredImageDecoder> DeferredImageDecoder::create(const SharedBuffer & data, ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileO ption gammaAndColorOption) |
57 { | 62 { |
58 OwnPtr<ImageDecoder> actualDecoder = ImageDecoder::create(data, alphaOption, gammaAndColorOption); | 63 OwnPtr<ImageDecoder> actualDecoder = ImageDecoder::create(data, alphaOption, gammaAndColorOption); |
59 return actualDecoder ? adoptPtr(new DeferredImageDecoder(actualDecoder.relea se())) : nullptr; | 64 return actualDecoder ? adoptPtr(new DeferredImageDecoder(actualDecoder.relea se())) : nullptr; |
60 } | 65 } |
61 | 66 |
62 PassOwnPtr<DeferredImageDecoder> DeferredImageDecoder::createForTesting(PassOwnP tr<ImageDecoder> decoder) | 67 PassOwnPtr<DeferredImageDecoder> DeferredImageDecoder::createForTesting(PassOwnP tr<ImageDecoder> decoder) |
63 { | 68 { |
64 return adoptPtr(new DeferredImageDecoder(decoder)); | 69 return adoptPtr(new DeferredImageDecoder(decoder)); |
65 } | 70 } |
66 | 71 |
67 bool DeferredImageDecoder::isLazyDecoded(const SkBitmap& bitmap) | 72 bool DeferredImageDecoder::isLazyDecoded(const SkBitmap& bitmap) |
68 { | 73 { |
69 return bitmap.pixelRef() | 74 return bitmap.pixelRef() |
70 && bitmap.pixelRef()->getURI() | 75 && bitmap.pixelRef()->getURI() |
71 && !memcmp(bitmap.pixelRef()->getURI(), labelLazyDecoded, sizeof(labelLa zyDecoded)); | 76 && (!memcmp(bitmap.pixelRef()->getURI(), labelLazyDecoded, sizeof(labelL azyDecoded)) |
77 || !memcmp(bitmap.pixelRef()->getURI(), labelDiscardable, sizeof(lab elDiscardable))); | |
72 } | 78 } |
73 | 79 |
74 void DeferredImageDecoder::setEnabled(bool enabled) | 80 void DeferredImageDecoder::setEnabled(bool enabled) |
75 { | 81 { |
76 s_enabled = enabled; | 82 s_enabled = enabled; |
77 } | 83 } |
78 | 84 |
85 void DeferredImageDecoder::setSkiaDiscardableMemoryEnabled(bool enabled) | |
86 { | |
87 s_skiaDiscardableMemoryEnabled = enabled; | |
88 } | |
89 | |
79 String DeferredImageDecoder::filenameExtension() const | 90 String DeferredImageDecoder::filenameExtension() const |
80 { | 91 { |
81 return m_actualDecoder ? m_actualDecoder->filenameExtension() : m_filenameEx tension; | 92 return m_actualDecoder ? m_actualDecoder->filenameExtension() : m_filenameEx tension; |
82 } | 93 } |
83 | 94 |
84 ImageFrame* DeferredImageDecoder::frameBufferAtIndex(size_t index) | 95 ImageFrame* DeferredImageDecoder::frameBufferAtIndex(size_t index) |
85 { | 96 { |
86 prepareLazyDecodedFrames(); | 97 prepareLazyDecodedFrames(); |
87 if (index < m_lazyDecodedFrames.size()) { | 98 if (index < m_lazyDecodedFrames.size()) { |
88 // ImageFrameGenerator has the latest known alpha state. There will | 99 // ImageFrameGenerator has the latest known alpha state. There will |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 || !m_actualDecoder->isSizeAvailable() | 212 || !m_actualDecoder->isSizeAvailable() |
202 || m_actualDecoder->filenameExtension() == "ico") | 213 || m_actualDecoder->filenameExtension() == "ico") |
203 return; | 214 return; |
204 | 215 |
205 activateLazyDecoding(); | 216 activateLazyDecoding(); |
206 | 217 |
207 const size_t previousSize = m_lazyDecodedFrames.size(); | 218 const size_t previousSize = m_lazyDecodedFrames.size(); |
208 m_lazyDecodedFrames.resize(m_actualDecoder->frameCount()); | 219 m_lazyDecodedFrames.resize(m_actualDecoder->frameCount()); |
209 for (size_t i = previousSize; i < m_lazyDecodedFrames.size(); ++i) { | 220 for (size_t i = previousSize; i < m_lazyDecodedFrames.size(); ++i) { |
210 OwnPtr<ImageFrame> frame(adoptPtr(new ImageFrame())); | 221 OwnPtr<ImageFrame> frame(adoptPtr(new ImageFrame())); |
211 frame->setSkBitmap(createLazyDecodingBitmap(i)); | 222 frame->setSkBitmap(createBitmap(i)); |
212 frame->setDuration(m_actualDecoder->frameDurationAtIndex(i)); | 223 frame->setDuration(m_actualDecoder->frameDurationAtIndex(i)); |
213 frame->setStatus(m_actualDecoder->frameIsCompleteAtIndex(i) ? ImageFrame ::FrameComplete : ImageFrame::FramePartial); | 224 frame->setStatus(m_actualDecoder->frameIsCompleteAtIndex(i) ? ImageFrame ::FrameComplete : ImageFrame::FramePartial); |
214 m_lazyDecodedFrames[i] = frame.release(); | 225 m_lazyDecodedFrames[i] = frame.release(); |
215 } | 226 } |
216 | 227 |
217 // The last lazy decoded frame created from previous call might be | 228 // The last lazy decoded frame created from previous call might be |
218 // incomplete so update its state. | 229 // incomplete so update its state. |
219 if (previousSize) | 230 if (previousSize) |
220 m_lazyDecodedFrames[previousSize - 1]->setStatus(m_actualDecoder->frameI sCompleteAtIndex(previousSize - 1) ? ImageFrame::FrameComplete : ImageFrame::Fra mePartial); | 231 m_lazyDecodedFrames[previousSize - 1]->setStatus(m_actualDecoder->frameI sCompleteAtIndex(previousSize - 1) ? ImageFrame::FrameComplete : ImageFrame::Fra mePartial); |
221 | 232 |
222 if (m_allDataReceived) { | 233 if (m_allDataReceived) { |
223 m_repetitionCount = m_actualDecoder->repetitionCount(); | 234 m_repetitionCount = m_actualDecoder->repetitionCount(); |
224 m_actualDecoder.clear(); | 235 m_actualDecoder.clear(); |
225 m_data = nullptr; | 236 m_data = nullptr; |
226 } | 237 } |
227 } | 238 } |
228 | 239 |
240 // Creates either a SkBitmap backed by SkDiscardablePixelRef or a SkBitmap using the | |
241 // legacy LazyDecodingPixelRef. | |
242 SkBitmap DeferredImageDecoder::createBitmap(size_t index) | |
243 { | |
244 // This code is temporary until the transition to SkDiscardablePixelRef is c omplete. | |
245 if (s_skiaDiscardableMemoryEnabled) | |
246 return createSkiaDiscardableBitmap(index); | |
247 return createLazyDecodingBitmap(index); | |
248 } | |
249 | |
250 // Creates a SkBitmap that is backed by SkDiscardablePixelRef. | |
251 SkBitmap DeferredImageDecoder::createSkiaDiscardableBitmap(size_t index) | |
252 { | |
253 IntSize decodedSize = m_actualDecoder->decodedSize(); | |
254 ASSERT(decodedSize.width() > 0); | |
255 ASSERT(decodedSize.height() > 0); | |
256 | |
257 SkImageInfo info; | |
258 info.fWidth = decodedSize.width(); | |
259 info.fHeight = decodedSize.height(); | |
260 info.fColorType = kBGRA_8888_SkColorType; | |
261 info.fAlphaType = frameHasAlphaAtIndex(index) ? kPremul_SkAlphaType : kOpaqu e_SkAlphaType; | |
262 | |
263 SkBitmap bitmap; | |
264 bool installed = SkInstallDiscardablePixelRef(new DecodingImageGenerator(m_f rameGenerator, info, index), &bitmap); | |
265 ASSERT_UNUSED(installed, installed); | |
266 bitmap.pixelRef()->setURI(labelDiscardable); | |
267 return bitmap; | |
268 } | |
269 | |
229 SkBitmap DeferredImageDecoder::createLazyDecodingBitmap(size_t index) | 270 SkBitmap DeferredImageDecoder::createLazyDecodingBitmap(size_t index) |
230 { | 271 { |
231 IntSize decodedSize = m_actualDecoder->decodedSize(); | 272 IntSize decodedSize = m_actualDecoder->decodedSize(); |
232 ASSERT(decodedSize.width() > 0); | 273 ASSERT(decodedSize.width() > 0); |
233 ASSERT(decodedSize.height() > 0); | 274 ASSERT(decodedSize.height() > 0); |
234 | 275 |
235 SkImageInfo info; | 276 SkImageInfo info; |
236 info.fWidth = decodedSize.width(); | 277 info.fWidth = decodedSize.width(); |
237 info.fHeight = decodedSize.height(); | 278 info.fHeight = decodedSize.height(); |
238 info.fColorType = kPMColor_SkColorType; | 279 info.fColorType = kPMColor_SkColorType; |
(...skipping 15 matching lines...) Expand all Loading... | |
254 return bitmap; | 295 return bitmap; |
255 } | 296 } |
256 | 297 |
257 bool DeferredImageDecoder::hotSpot(IntPoint& hotSpot) const | 298 bool DeferredImageDecoder::hotSpot(IntPoint& hotSpot) const |
258 { | 299 { |
259 // TODO: Implement. | 300 // TODO: Implement. |
260 return m_actualDecoder ? m_actualDecoder->hotSpot(hotSpot) : false; | 301 return m_actualDecoder ? m_actualDecoder->hotSpot(hotSpot) : false; |
261 } | 302 } |
262 | 303 |
263 } // namespace WebCore | 304 } // namespace WebCore |
OLD | NEW |