| 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 "core/platform/graphics/DeferredImageDecoder.h" | 27 #include "core/platform/graphics/DeferredImageDecoder.h" |
| 28 | 28 |
| 29 #include "core/platform/graphics/DecodingImageGenerator.h" |
| 29 #include "core/platform/graphics/ImageFrameGenerator.h" | 30 #include "core/platform/graphics/ImageFrameGenerator.h" |
| 30 #include "core/platform/graphics/LazyDecodingPixelRef.h" | 31 #include "core/platform/graphics/LazyDecodingPixelRef.h" |
| 32 #include "third_party/skia/include/core/SkImageInfo.h" |
| 33 #include "third_party/skia/src/lazy/SkDiscardablePixelRef.h" |
| 31 #include "wtf/PassOwnPtr.h" | 34 #include "wtf/PassOwnPtr.h" |
| 32 | 35 |
| 33 namespace WebCore { | 36 namespace WebCore { |
| 34 | 37 |
| 35 namespace { | 38 namespace { |
| 36 | 39 |
| 37 // URI label for a lazily decoded SkPixelRef. | 40 // URI label for a lazily decoded SkPixelRef. |
| 38 const char labelLazyDecoded[] = "lazy"; | 41 const char labelLazyDecoded[] = "lazy"; |
| 39 | 42 |
| 40 } // namespace | 43 } // namespace |
| 41 | 44 |
| 42 bool DeferredImageDecoder::s_enabled = false; | 45 bool DeferredImageDecoder::s_enabled = false; |
| 46 bool DeferredImageDecoder::s_enabledSkiaDiscardableMemory = false; |
| 43 | 47 |
| 44 DeferredImageDecoder::DeferredImageDecoder(PassOwnPtr<ImageDecoder> actualDecode
r) | 48 DeferredImageDecoder::DeferredImageDecoder(PassOwnPtr<ImageDecoder> actualDecode
r) |
| 45 : m_allDataReceived(false) | 49 : m_allDataReceived(false) |
| 46 , m_actualDecoder(actualDecoder) | 50 , m_actualDecoder(actualDecoder) |
| 47 , m_orientation(DefaultImageOrientation) | 51 , m_orientation(DefaultImageOrientation) |
| 48 , m_repetitionCount(cAnimationNone) | 52 , m_repetitionCount(cAnimationNone) |
| 49 { | 53 { |
| 50 } | 54 } |
| 51 | 55 |
| 52 DeferredImageDecoder::~DeferredImageDecoder() | 56 DeferredImageDecoder::~DeferredImageDecoder() |
| (...skipping 16 matching lines...) Expand all Loading... |
| 69 return bitmap.pixelRef() | 73 return bitmap.pixelRef() |
| 70 && bitmap.pixelRef()->getURI() | 74 && bitmap.pixelRef()->getURI() |
| 71 && !memcmp(bitmap.pixelRef()->getURI(), labelLazyDecoded, sizeof(labelLa
zyDecoded)); | 75 && !memcmp(bitmap.pixelRef()->getURI(), labelLazyDecoded, sizeof(labelLa
zyDecoded)); |
| 72 } | 76 } |
| 73 | 77 |
| 74 void DeferredImageDecoder::setEnabled(bool enabled) | 78 void DeferredImageDecoder::setEnabled(bool enabled) |
| 75 { | 79 { |
| 76 s_enabled = enabled; | 80 s_enabled = enabled; |
| 77 } | 81 } |
| 78 | 82 |
| 83 void DeferredImageDecoder::setEnabledSkiaDiscardableMemory(bool enabled) |
| 84 { |
| 85 s_enabledSkiaDiscardableMemory = enabled; |
| 86 } |
| 87 |
| 79 String DeferredImageDecoder::filenameExtension() const | 88 String DeferredImageDecoder::filenameExtension() const |
| 80 { | 89 { |
| 81 return m_actualDecoder ? m_actualDecoder->filenameExtension() : m_filenameEx
tension; | 90 return m_actualDecoder ? m_actualDecoder->filenameExtension() : m_filenameEx
tension; |
| 82 } | 91 } |
| 83 | 92 |
| 84 ImageFrame* DeferredImageDecoder::frameBufferAtIndex(size_t index) | 93 ImageFrame* DeferredImageDecoder::frameBufferAtIndex(size_t index) |
| 85 { | 94 { |
| 86 prepareLazyDecodedFrames(); | 95 prepareLazyDecodedFrames(); |
| 87 if (index < m_lazyDecodedFrames.size()) { | 96 if (index < m_lazyDecodedFrames.size()) { |
| 88 // ImageFrameGenerator has the latest known alpha state. There will | 97 // 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() | 210 || !m_actualDecoder->isSizeAvailable() |
| 202 || m_actualDecoder->filenameExtension() == "ico") | 211 || m_actualDecoder->filenameExtension() == "ico") |
| 203 return; | 212 return; |
| 204 | 213 |
| 205 activateLazyDecoding(); | 214 activateLazyDecoding(); |
| 206 | 215 |
| 207 const size_t previousSize = m_lazyDecodedFrames.size(); | 216 const size_t previousSize = m_lazyDecodedFrames.size(); |
| 208 m_lazyDecodedFrames.resize(m_actualDecoder->frameCount()); | 217 m_lazyDecodedFrames.resize(m_actualDecoder->frameCount()); |
| 209 for (size_t i = previousSize; i < m_lazyDecodedFrames.size(); ++i) { | 218 for (size_t i = previousSize; i < m_lazyDecodedFrames.size(); ++i) { |
| 210 OwnPtr<ImageFrame> frame(adoptPtr(new ImageFrame())); | 219 OwnPtr<ImageFrame> frame(adoptPtr(new ImageFrame())); |
| 211 frame->setSkBitmap(createLazyDecodingBitmap(i)); | 220 frame->setSkBitmap(createBitmap(i)); |
| 212 frame->setDuration(m_actualDecoder->frameDurationAtIndex(i)); | 221 frame->setDuration(m_actualDecoder->frameDurationAtIndex(i)); |
| 213 frame->setStatus(m_actualDecoder->frameIsCompleteAtIndex(i) ? ImageFrame
::FrameComplete : ImageFrame::FramePartial); | 222 frame->setStatus(m_actualDecoder->frameIsCompleteAtIndex(i) ? ImageFrame
::FrameComplete : ImageFrame::FramePartial); |
| 214 m_lazyDecodedFrames[i] = frame.release(); | 223 m_lazyDecodedFrames[i] = frame.release(); |
| 215 } | 224 } |
| 216 | 225 |
| 217 // The last lazy decoded frame created from previous call might be | 226 // The last lazy decoded frame created from previous call might be |
| 218 // incomplete so update its state. | 227 // incomplete so update its state. |
| 219 if (previousSize) | 228 if (previousSize) |
| 220 m_lazyDecodedFrames[previousSize - 1]->setStatus(m_actualDecoder->frameI
sCompleteAtIndex(previousSize - 1) ? ImageFrame::FrameComplete : ImageFrame::Fra
mePartial); | 229 m_lazyDecodedFrames[previousSize - 1]->setStatus(m_actualDecoder->frameI
sCompleteAtIndex(previousSize - 1) ? ImageFrame::FrameComplete : ImageFrame::Fra
mePartial); |
| 221 | 230 |
| 222 if (m_allDataReceived) { | 231 if (m_allDataReceived) { |
| 223 m_repetitionCount = m_actualDecoder->repetitionCount(); | 232 m_repetitionCount = m_actualDecoder->repetitionCount(); |
| 224 m_actualDecoder.clear(); | 233 m_actualDecoder.clear(); |
| 225 m_data = nullptr; | 234 m_data = nullptr; |
| 226 } | 235 } |
| 227 } | 236 } |
| 228 | 237 |
| 238 // Creates either a SkBitmap backed by SkDiscardablePixelRef or a SkBitmap using
the |
| 239 // legacy LazyDecodingPixelRef. |
| 240 SkBitmap DeferredImageDecoder::createBitmap(size_t index) |
| 241 { |
| 242 // This code is temporary until the transition to SkDiscardablePixelRef is c
omplete. |
| 243 if (s_enabledSkiaDiscardableMemory) |
| 244 return createSkiaDiscardableBitmap(index); |
| 245 return createLazyDecodingBitmap(index); |
| 246 } |
| 247 |
| 248 // Creates a SkBitmap that is backed by SkDiscardablePixelRef. |
| 249 SkBitmap DeferredImageDecoder::createSkiaDiscardableBitmap(size_t index) |
| 250 { |
| 251 IntSize decodedSize = m_actualDecoder->decodedSize(); |
| 252 ASSERT(decodedSize.width() > 0); |
| 253 ASSERT(decodedSize.height() > 0); |
| 254 |
| 255 SkImageInfo info; |
| 256 info.fWidth = decodedSize.width(); |
| 257 info.fHeight = decodedSize.height(); |
| 258 info.fColorType = kBGRA_8888_SkColorType; |
| 259 info.fAlphaType = frameHasAlphaAtIndex(index) ? kPremul_SkAlphaType : kOpaqu
e_SkAlphaType; |
| 260 |
| 261 SkBitmap bitmap; |
| 262 bool installed = SkDiscardablePixelRef::Install(new DecodingImageGenerator(m
_frameGenerator, info, index), &bitmap); |
| 263 ASSERT_UNUSED(installed, installed); |
| 264 return bitmap; |
| 265 } |
| 266 |
| 229 SkBitmap DeferredImageDecoder::createLazyDecodingBitmap(size_t index) | 267 SkBitmap DeferredImageDecoder::createLazyDecodingBitmap(size_t index) |
| 230 { | 268 { |
| 231 IntSize decodedSize = m_actualDecoder->decodedSize(); | 269 IntSize decodedSize = m_actualDecoder->decodedSize(); |
| 232 ASSERT(decodedSize.width() > 0); | 270 ASSERT(decodedSize.width() > 0); |
| 233 ASSERT(decodedSize.height() > 0); | 271 ASSERT(decodedSize.height() > 0); |
| 234 | 272 |
| 235 // Creates a lazily decoded SkPixelRef that references the entire image with
out scaling. | 273 // Creates a lazily decoded SkPixelRef that references the entire image with
out scaling. |
| 236 SkBitmap bitmap; | 274 SkBitmap bitmap; |
| 237 bitmap.setConfig(SkBitmap::kARGB_8888_Config, decodedSize.width(), decodedSi
ze.height()); | 275 bitmap.setConfig(SkBitmap::kARGB_8888_Config, decodedSize.width(), decodedSi
ze.height()); |
| 238 bitmap.setPixelRef(new LazyDecodingPixelRef(m_frameGenerator, index))->unref
(); | 276 bitmap.setPixelRef(new LazyDecodingPixelRef(m_frameGenerator, index))->unref
(); |
| 239 | 277 |
| 240 // Use the URI to identify this as a lazily decoded SkPixelRef of type LazyD
ecodingPixelRef. | 278 // Use the URI to identify this as a lazily decoded SkPixelRef of type LazyD
ecodingPixelRef. |
| 241 // FIXME: It would be more useful to give the actual image URI. | 279 // FIXME: It would be more useful to give the actual image URI. |
| 242 bitmap.pixelRef()->setURI(labelLazyDecoded); | 280 bitmap.pixelRef()->setURI(labelLazyDecoded); |
| 243 | 281 |
| 244 // Inform the bitmap that we will never change the pixels. This is a perform
ance hint | 282 // Inform the bitmap that we will never change the pixels. This is a perform
ance hint |
| 245 // subsystems that may try to cache this bitmap (e.g. pictures, pipes, gpu,
pdf, etc.) | 283 // subsystems that may try to cache this bitmap (e.g. pictures, pipes, gpu,
pdf, etc.) |
| 246 bitmap.setImmutable(); | 284 bitmap.setImmutable(); |
| 247 | 285 |
| 248 return bitmap; | 286 return bitmap; |
| 249 } | 287 } |
| 250 | 288 |
| 251 bool DeferredImageDecoder::hotSpot(IntPoint& hotSpot) const | 289 bool DeferredImageDecoder::hotSpot(IntPoint& hotSpot) const |
| 252 { | 290 { |
| 253 // TODO: Implement. | 291 // TODO: Implement. |
| 254 return m_actualDecoder ? m_actualDecoder->hotSpot(hotSpot) : false; | 292 return m_actualDecoder ? m_actualDecoder->hotSpot(hotSpot) : false; |
| 255 } | 293 } |
| 256 | 294 |
| 257 } // namespace WebCore | 295 } // namespace WebCore |
| OLD | NEW |