| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 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. | |
| 24 */ | |
| 25 | |
| 26 #include "config.h" | |
| 27 #include "core/platform/graphics/chromium/DeferredImageDecoder.h" | |
| 28 | |
| 29 #include "core/platform/graphics/chromium/ImageFrameGenerator.h" | |
| 30 #include "core/platform/graphics/chromium/LazyDecodingPixelRef.h" | |
| 31 #include "wtf/OwnPtr.h" | |
| 32 #include "wtf/PassOwnPtr.h" | |
| 33 | |
| 34 namespace WebCore { | |
| 35 | |
| 36 namespace { | |
| 37 | |
| 38 // URI label for a lazily decoded SkPixelRef. | |
| 39 const char labelLazyDecoded[] = "lazy"; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 bool DeferredImageDecoder::s_enabled = false; | |
| 44 | |
| 45 DeferredImageDecoder::DeferredImageDecoder(PassOwnPtr<ImageDecoder> actualDecode
r) | |
| 46 : m_allDataReceived(false) | |
| 47 , m_actualDecoder(actualDecoder) | |
| 48 , m_orientation(DefaultImageOrientation) | |
| 49 , m_repetitionCount(cAnimationNone) | |
| 50 { | |
| 51 } | |
| 52 | |
| 53 DeferredImageDecoder::~DeferredImageDecoder() | |
| 54 { | |
| 55 } | |
| 56 | |
| 57 PassOwnPtr<DeferredImageDecoder> DeferredImageDecoder::create(const SharedBuffer
& data, ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileO
ption gammaAndColorOption) | |
| 58 { | |
| 59 OwnPtr<ImageDecoder> actualDecoder = ImageDecoder::create(data, alphaOption,
gammaAndColorOption); | |
| 60 return actualDecoder ? adoptPtr(new DeferredImageDecoder(actualDecoder.relea
se())) : nullptr; | |
| 61 } | |
| 62 | |
| 63 PassOwnPtr<DeferredImageDecoder> DeferredImageDecoder::createForTesting(PassOwnP
tr<ImageDecoder> decoder) | |
| 64 { | |
| 65 return adoptPtr(new DeferredImageDecoder(decoder)); | |
| 66 } | |
| 67 | |
| 68 bool DeferredImageDecoder::isLazyDecoded(const SkBitmap& bitmap) | |
| 69 { | |
| 70 return bitmap.pixelRef() | |
| 71 && bitmap.pixelRef()->getURI() | |
| 72 && !memcmp(bitmap.pixelRef()->getURI(), labelLazyDecoded, sizeof(labelLa
zyDecoded)); | |
| 73 } | |
| 74 | |
| 75 void DeferredImageDecoder::setEnabled(bool enabled) | |
| 76 { | |
| 77 s_enabled = enabled; | |
| 78 } | |
| 79 | |
| 80 String DeferredImageDecoder::filenameExtension() const | |
| 81 { | |
| 82 return m_actualDecoder ? m_actualDecoder->filenameExtension() : m_filenameEx
tension; | |
| 83 } | |
| 84 | |
| 85 ImageFrame* DeferredImageDecoder::frameBufferAtIndex(size_t index) | |
| 86 { | |
| 87 prepareLazyDecodedFrames(); | |
| 88 if (index < m_lazyDecodedFrames.size()) { | |
| 89 // ImageFrameGenerator has the latest known alpha state. There will | |
| 90 // be a performance boost if this frame is opaque. | |
| 91 m_lazyDecodedFrames[index]->setHasAlpha(m_frameGenerator->hasAlpha(index
)); | |
| 92 return m_lazyDecodedFrames[index].get(); | |
| 93 } | |
| 94 if (m_actualDecoder) | |
| 95 return m_actualDecoder->frameBufferAtIndex(index); | |
| 96 return 0; | |
| 97 } | |
| 98 | |
| 99 void DeferredImageDecoder::setData(SharedBuffer* data, bool allDataReceived) | |
| 100 { | |
| 101 if (m_actualDecoder) { | |
| 102 m_data = data; | |
| 103 m_allDataReceived = allDataReceived; | |
| 104 m_actualDecoder->setData(data, allDataReceived); | |
| 105 prepareLazyDecodedFrames(); | |
| 106 } | |
| 107 | |
| 108 if (m_frameGenerator) | |
| 109 m_frameGenerator->setData(data, allDataReceived); | |
| 110 } | |
| 111 | |
| 112 bool DeferredImageDecoder::isSizeAvailable() | |
| 113 { | |
| 114 // m_actualDecoder is 0 only if image decoding is deferred and that | |
| 115 // means image header decoded successfully and size is available. | |
| 116 return m_actualDecoder ? m_actualDecoder->isSizeAvailable() : true; | |
| 117 } | |
| 118 | |
| 119 IntSize DeferredImageDecoder::size() const | |
| 120 { | |
| 121 return m_actualDecoder ? m_actualDecoder->size() : m_size; | |
| 122 } | |
| 123 | |
| 124 IntSize DeferredImageDecoder::frameSizeAtIndex(size_t index) const | |
| 125 { | |
| 126 // FIXME: Frame size is assumed to be uniform. This might not be true for | |
| 127 // future supported codecs. | |
| 128 return m_actualDecoder ? m_actualDecoder->frameSizeAtIndex(index) : m_size; | |
| 129 } | |
| 130 | |
| 131 size_t DeferredImageDecoder::frameCount() | |
| 132 { | |
| 133 return m_actualDecoder ? m_actualDecoder->frameCount() : m_lazyDecodedFrames
.size(); | |
| 134 } | |
| 135 | |
| 136 int DeferredImageDecoder::repetitionCount() const | |
| 137 { | |
| 138 return m_actualDecoder ? m_actualDecoder->repetitionCount() : m_repetitionCo
unt; | |
| 139 } | |
| 140 | |
| 141 size_t DeferredImageDecoder::clearCacheExceptFrame(size_t clearExceptFrame) | |
| 142 { | |
| 143 // If image decoding is deferred then frame buffer cache is managed by | |
| 144 // the compositor and this call is ignored. | |
| 145 return m_actualDecoder ? m_actualDecoder->clearCacheExceptFrame(clearExceptF
rame) : 0; | |
| 146 } | |
| 147 | |
| 148 bool DeferredImageDecoder::frameHasAlphaAtIndex(size_t index) const | |
| 149 { | |
| 150 if (m_actualDecoder) | |
| 151 return m_actualDecoder->frameHasAlphaAtIndex(index); | |
| 152 if (!m_frameGenerator->isMultiFrame()) | |
| 153 return m_frameGenerator->hasAlpha(index); | |
| 154 return true; | |
| 155 } | |
| 156 | |
| 157 bool DeferredImageDecoder::frameIsCompleteAtIndex(size_t index) const | |
| 158 { | |
| 159 if (m_actualDecoder) | |
| 160 return m_actualDecoder->frameIsCompleteAtIndex(index); | |
| 161 if (index < m_lazyDecodedFrames.size()) | |
| 162 return m_lazyDecodedFrames[index]->status() == ImageFrame::FrameComplete
; | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 float DeferredImageDecoder::frameDurationAtIndex(size_t index) const | |
| 167 { | |
| 168 if (m_actualDecoder) | |
| 169 return m_actualDecoder->frameDurationAtIndex(index); | |
| 170 if (index < m_lazyDecodedFrames.size()) | |
| 171 return m_lazyDecodedFrames[index]->duration(); | |
| 172 return 0; | |
| 173 } | |
| 174 | |
| 175 unsigned DeferredImageDecoder::frameBytesAtIndex(size_t index) const | |
| 176 { | |
| 177 // If frame decoding is deferred then it is not managed by MemoryCache | |
| 178 // so return 0 here. | |
| 179 return m_frameGenerator ? 0 : m_actualDecoder->frameBytesAtIndex(index); | |
| 180 } | |
| 181 | |
| 182 ImageOrientation DeferredImageDecoder::orientation() const | |
| 183 { | |
| 184 return m_actualDecoder ? m_actualDecoder->orientation() : m_orientation; | |
| 185 } | |
| 186 | |
| 187 void DeferredImageDecoder::activateLazyDecoding() | |
| 188 { | |
| 189 if (m_frameGenerator) | |
| 190 return; | |
| 191 m_size = m_actualDecoder->size(); | |
| 192 m_orientation = m_actualDecoder->orientation(); | |
| 193 m_filenameExtension = m_actualDecoder->filenameExtension(); | |
| 194 const bool isSingleFrame = m_actualDecoder->repetitionCount() == cAnimationN
one || (m_allDataReceived && m_actualDecoder->frameCount() == 1u); | |
| 195 m_frameGenerator = ImageFrameGenerator::create(SkISize::Make(m_actualDecoder
->decodedSize().width(), m_actualDecoder->decodedSize().height()), m_data, m_all
DataReceived, !isSingleFrame); | |
| 196 } | |
| 197 | |
| 198 void DeferredImageDecoder::prepareLazyDecodedFrames() | |
| 199 { | |
| 200 if (!s_enabled | |
| 201 || !m_actualDecoder | |
| 202 || !m_actualDecoder->isSizeAvailable() | |
| 203 || m_actualDecoder->filenameExtension() == "ico") | |
| 204 return; | |
| 205 | |
| 206 activateLazyDecoding(); | |
| 207 | |
| 208 const size_t previousSize = m_lazyDecodedFrames.size(); | |
| 209 m_lazyDecodedFrames.resize(m_actualDecoder->frameCount()); | |
| 210 for (size_t i = previousSize; i < m_lazyDecodedFrames.size(); ++i) { | |
| 211 OwnPtr<ImageFrame> frame(adoptPtr(new ImageFrame())); | |
| 212 frame->setSkBitmap(createLazyDecodingBitmap(i)); | |
| 213 frame->setDuration(m_actualDecoder->frameDurationAtIndex(i)); | |
| 214 frame->setStatus(m_actualDecoder->frameIsCompleteAtIndex(i) ? ImageFrame
::FrameComplete : ImageFrame::FramePartial); | |
| 215 m_lazyDecodedFrames[i] = frame.release(); | |
| 216 } | |
| 217 | |
| 218 // The last lazy decoded frame created from previous call might be | |
| 219 // incomplete so update its state. | |
| 220 if (previousSize) | |
| 221 m_lazyDecodedFrames[previousSize - 1]->setStatus(m_actualDecoder->frameI
sCompleteAtIndex(previousSize - 1) ? ImageFrame::FrameComplete : ImageFrame::Fra
mePartial); | |
| 222 | |
| 223 if (m_allDataReceived) { | |
| 224 m_repetitionCount = m_actualDecoder->repetitionCount(); | |
| 225 m_actualDecoder.clear(); | |
| 226 m_data = nullptr; | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 SkBitmap DeferredImageDecoder::createLazyDecodingBitmap(size_t index) | |
| 231 { | |
| 232 IntSize decodedSize = m_actualDecoder->decodedSize(); | |
| 233 ASSERT(decodedSize.width() > 0); | |
| 234 ASSERT(decodedSize.height() > 0); | |
| 235 | |
| 236 // Creates a lazily decoded SkPixelRef that references the entire image with
out scaling. | |
| 237 SkBitmap bitmap; | |
| 238 bitmap.setConfig(SkBitmap::kARGB_8888_Config, decodedSize.width(), decodedSi
ze.height()); | |
| 239 bitmap.setPixelRef(new LazyDecodingPixelRef(m_frameGenerator, index))->unref
(); | |
| 240 | |
| 241 // Use the URI to identify this as a lazily decoded SkPixelRef of type LazyD
ecodingPixelRef. | |
| 242 // FIXME: It would be more useful to give the actual image URI. | |
| 243 bitmap.pixelRef()->setURI(labelLazyDecoded); | |
| 244 | |
| 245 // Inform the bitmap that we will never change the pixels. This is a perform
ance hint | |
| 246 // subsystems that may try to cache this bitmap (e.g. pictures, pipes, gpu,
pdf, etc.) | |
| 247 bitmap.setImmutable(); | |
| 248 | |
| 249 return bitmap; | |
| 250 } | |
| 251 | |
| 252 bool DeferredImageDecoder::hotSpot(IntPoint& hotSpot) const | |
| 253 { | |
| 254 // TODO: Implement. | |
| 255 return m_actualDecoder ? m_actualDecoder->hotSpot(hotSpot) : false; | |
| 256 } | |
| 257 | |
| 258 } // namespace WebCore | |
| OLD | NEW |