| 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 | |
| 28 #include "core/platform/graphics/ImageFrameGenerator.h" | |
| 29 | |
| 30 #include "core/platform/graphics/DiscardablePixelRef.h" | |
| 31 #include "core/platform/graphics/ImageDecodingStore.h" | |
| 32 #include "core/platform/image-decoders/ImageDecoder.h" | |
| 33 #include "platform/SharedBuffer.h" | |
| 34 #include "platform/TraceEvent.h" | |
| 35 #include "platform/graphics/ScaledImageFragment.h" | |
| 36 | |
| 37 #include "skia/ext/image_operations.h" | |
| 38 | |
| 39 namespace WebCore { | |
| 40 | |
| 41 namespace { | |
| 42 | |
| 43 skia::ImageOperations::ResizeMethod resizeMethod() | |
| 44 { | |
| 45 return skia::ImageOperations::RESIZE_LANCZOS3; | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha
redBuffer> data, bool allDataReceived, bool isMultiFrame) | |
| 51 : m_fullSize(fullSize) | |
| 52 , m_isMultiFrame(isMultiFrame) | |
| 53 , m_decodeFailedAndEmpty(false) | |
| 54 , m_decodeCount(ScaledImageFragment::FirstPartialImage) | |
| 55 , m_allocator(adoptPtr(new DiscardablePixelRefAllocator())) | |
| 56 { | |
| 57 setData(data.get(), allDataReceived); | |
| 58 } | |
| 59 | |
| 60 ImageFrameGenerator::~ImageFrameGenerator() | |
| 61 { | |
| 62 // FIXME: This check is not really thread-safe. This should be changed to: | |
| 63 // ImageDecodingStore::removeCacheFromInstance(this); | |
| 64 // Which uses a lock internally. | |
| 65 if (ImageDecodingStore::instance()) | |
| 66 ImageDecodingStore::instance()->removeCacheIndexedByGenerator(this); | |
| 67 } | |
| 68 | |
| 69 void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataRec
eived) | |
| 70 { | |
| 71 m_data.setData(data.get(), allDataReceived); | |
| 72 } | |
| 73 | |
| 74 void ImageFrameGenerator::copyData(RefPtr<SharedBuffer>* data, bool* allDataRece
ived) | |
| 75 { | |
| 76 SharedBuffer* buffer = 0; | |
| 77 m_data.data(&buffer, allDataReceived); | |
| 78 if (buffer) | |
| 79 *data = buffer->copy(); | |
| 80 } | |
| 81 | |
| 82 const ScaledImageFragment* ImageFrameGenerator::decodeAndScale(const SkISize& sc
aledSize, size_t index) | |
| 83 { | |
| 84 // Prevents concurrent decode or scale operations on the same image data. | |
| 85 // Multiple LazyDecodingPixelRefs can call this method at the same time. | |
| 86 MutexLocker lock(m_decodeMutex); | |
| 87 if (m_decodeFailedAndEmpty) | |
| 88 return 0; | |
| 89 | |
| 90 const ScaledImageFragment* cachedImage = 0; | |
| 91 | |
| 92 cachedImage = tryToLockCompleteCache(scaledSize, index); | |
| 93 if (cachedImage) | |
| 94 return cachedImage; | |
| 95 | |
| 96 TRACE_EVENT2("webkit", "ImageFrameGenerator::decodeAndScale", "generator", t
his, "decodeCount", static_cast<int>(m_decodeCount)); | |
| 97 | |
| 98 cachedImage = tryToScale(0, scaledSize, index); | |
| 99 if (cachedImage) | |
| 100 return cachedImage; | |
| 101 | |
| 102 cachedImage = tryToResumeDecodeAndScale(scaledSize, index); | |
| 103 if (cachedImage) | |
| 104 return cachedImage; | |
| 105 return 0; | |
| 106 } | |
| 107 | |
| 108 const ScaledImageFragment* ImageFrameGenerator::tryToLockCompleteCache(const SkI
Size& scaledSize, size_t index) | |
| 109 { | |
| 110 const ScaledImageFragment* cachedImage = 0; | |
| 111 if (ImageDecodingStore::instance()->lockCache(this, scaledSize, index, &cach
edImage)) | |
| 112 return cachedImage; | |
| 113 return 0; | |
| 114 } | |
| 115 | |
| 116 const ScaledImageFragment* ImageFrameGenerator::tryToScale(const ScaledImageFrag
ment* fullSizeImage, const SkISize& scaledSize, size_t index) | |
| 117 { | |
| 118 TRACE_EVENT0("webkit", "ImageFrameGenerator::tryToScale"); | |
| 119 | |
| 120 // If the requested scaled size is the same as the full size then exit | |
| 121 // early. This saves a cache lookup. | |
| 122 if (scaledSize == m_fullSize) | |
| 123 return 0; | |
| 124 | |
| 125 if (!fullSizeImage && !ImageDecodingStore::instance()->lockCache(this, m_ful
lSize, index, &fullSizeImage)) | |
| 126 return 0; | |
| 127 | |
| 128 // This call allocates the DiscardablePixelRef and lock/unlocks it | |
| 129 // afterwards. So the memory allocated to the scaledBitmap can be | |
| 130 // discarded after this call. Need to lock the scaledBitmap and | |
| 131 // check the pixels before using it next time. | |
| 132 SkBitmap scaledBitmap = skia::ImageOperations::Resize(fullSizeImage->bitmap(
), resizeMethod(), scaledSize.width(), scaledSize.height(), m_allocator.get()); | |
| 133 | |
| 134 OwnPtr<ScaledImageFragment> scaledImage; | |
| 135 if (fullSizeImage->isComplete()) | |
| 136 scaledImage = ScaledImageFragment::createComplete(scaledSize, fullSizeIm
age->index(), scaledBitmap); | |
| 137 else | |
| 138 scaledImage = ScaledImageFragment::createPartial(scaledSize, fullSizeIma
ge->index(), nextGenerationId(), scaledBitmap); | |
| 139 ImageDecodingStore::instance()->unlockCache(this, fullSizeImage); | |
| 140 return ImageDecodingStore::instance()->insertAndLockCache(this, scaledImage.
release()); | |
| 141 } | |
| 142 | |
| 143 const ScaledImageFragment* ImageFrameGenerator::tryToResumeDecodeAndScale(const
SkISize& scaledSize, size_t index) | |
| 144 { | |
| 145 TRACE_EVENT1("webkit", "ImageFrameGenerator::tryToResumeDecodeAndScale", "in
dex", static_cast<int>(index)); | |
| 146 | |
| 147 ImageDecoder* decoder = 0; | |
| 148 const bool resumeDecoding = ImageDecodingStore::instance()->lockDecoder(this
, m_fullSize, &decoder); | |
| 149 ASSERT(!resumeDecoding || decoder); | |
| 150 | |
| 151 OwnPtr<ScaledImageFragment> fullSizeImage = decode(index, &decoder); | |
| 152 | |
| 153 if (!decoder) | |
| 154 return 0; | |
| 155 | |
| 156 // If we are not resuming decoding that means the decoder is freshly | |
| 157 // created and we have ownership. If we are resuming decoding then | |
| 158 // the decoder is owned by ImageDecodingStore. | |
| 159 OwnPtr<ImageDecoder> decoderContainer; | |
| 160 if (!resumeDecoding) | |
| 161 decoderContainer = adoptPtr(decoder); | |
| 162 | |
| 163 if (!fullSizeImage) { | |
| 164 // If decode has failed and resulted an empty image we can save work | |
| 165 // in the future by returning early. | |
| 166 m_decodeFailedAndEmpty = !m_isMultiFrame && decoder->failed(); | |
| 167 | |
| 168 if (resumeDecoding) | |
| 169 ImageDecodingStore::instance()->unlockDecoder(this, decoder); | |
| 170 return 0; | |
| 171 } | |
| 172 | |
| 173 const ScaledImageFragment* cachedImage = ImageDecodingStore::instance()->ins
ertAndLockCache(this, fullSizeImage.release()); | |
| 174 | |
| 175 // If the image generated is complete then there is no need to keep | |
| 176 // the decoder. The exception is multi-frame decoder which can generate | |
| 177 // multiple complete frames. | |
| 178 const bool removeDecoder = cachedImage->isComplete() && !m_isMultiFrame; | |
| 179 | |
| 180 if (resumeDecoding) { | |
| 181 if (removeDecoder) | |
| 182 ImageDecodingStore::instance()->removeDecoder(this, decoder); | |
| 183 else | |
| 184 ImageDecodingStore::instance()->unlockDecoder(this, decoder); | |
| 185 } else if (!removeDecoder) { | |
| 186 ImageDecodingStore::instance()->insertDecoder(this, decoderContainer.rel
ease(), DiscardablePixelRef::isDiscardable(cachedImage->bitmap().pixelRef())); | |
| 187 } | |
| 188 | |
| 189 if (m_fullSize == scaledSize) | |
| 190 return cachedImage; | |
| 191 return tryToScale(cachedImage, scaledSize, index); | |
| 192 } | |
| 193 | |
| 194 PassOwnPtr<ScaledImageFragment> ImageFrameGenerator::decode(size_t index, ImageD
ecoder** decoder) | |
| 195 { | |
| 196 TRACE_EVENT2("webkit", "ImageFrameGenerator::decode", "width", m_fullSize.wi
dth(), "height", m_fullSize.height()); | |
| 197 | |
| 198 ASSERT(decoder); | |
| 199 SharedBuffer* data = 0; | |
| 200 bool allDataReceived = false; | |
| 201 m_data.data(&data, &allDataReceived); | |
| 202 | |
| 203 // Try to create an ImageDecoder if we are not given one. | |
| 204 if (!*decoder) { | |
| 205 if (m_imageDecoderFactory) | |
| 206 *decoder = m_imageDecoderFactory->create().leakPtr(); | |
| 207 | |
| 208 if (!*decoder) | |
| 209 *decoder = ImageDecoder::create(*data, ImageSource::AlphaPremultipli
ed, ImageSource::GammaAndColorProfileApplied).leakPtr(); | |
| 210 | |
| 211 if (!*decoder) | |
| 212 return nullptr; | |
| 213 } | |
| 214 | |
| 215 // TODO: this is very ugly. We need to refactor the way how we can pass a | |
| 216 // memory allocator to image decoders. | |
| 217 if (!m_isMultiFrame) | |
| 218 (*decoder)->setMemoryAllocator(m_allocator.get()); | |
| 219 (*decoder)->setData(data, allDataReceived); | |
| 220 // If this call returns a newly allocated DiscardablePixelRef, then | |
| 221 // ImageFrame::m_bitmap and the contained DiscardablePixelRef are locked. | |
| 222 // They will be unlocked when ImageDecoder is destroyed since ImageDecoder | |
| 223 // owns the ImageFrame. Partially decoded SkBitmap is thus inserted into the | |
| 224 // ImageDecodingStore while locked. | |
| 225 ImageFrame* frame = (*decoder)->frameBufferAtIndex(index); | |
| 226 (*decoder)->setData(0, false); // Unref SharedBuffer from ImageDecoder. | |
| 227 (*decoder)->clearCacheExceptFrame(index); | |
| 228 | |
| 229 if (!frame || frame->status() == ImageFrame::FrameEmpty) | |
| 230 return nullptr; | |
| 231 | |
| 232 const bool isComplete = frame->status() == ImageFrame::FrameComplete; | |
| 233 SkBitmap fullSizeBitmap = frame->getSkBitmap(); | |
| 234 if (fullSizeBitmap.isNull()) | |
| 235 return nullptr; | |
| 236 | |
| 237 { | |
| 238 MutexLocker lock(m_alphaMutex); | |
| 239 if (index >= m_hasAlpha.size()) { | |
| 240 const size_t oldSize = m_hasAlpha.size(); | |
| 241 m_hasAlpha.resize(index + 1); | |
| 242 for (size_t i = oldSize; i < m_hasAlpha.size(); ++i) | |
| 243 m_hasAlpha[i] = true; | |
| 244 } | |
| 245 m_hasAlpha[index] = !fullSizeBitmap.isOpaque(); | |
| 246 } | |
| 247 ASSERT(fullSizeBitmap.width() == m_fullSize.width() && fullSizeBitmap.height
() == m_fullSize.height()); | |
| 248 | |
| 249 if (isComplete) | |
| 250 return ScaledImageFragment::createComplete(m_fullSize, index, fullSizeBi
tmap); | |
| 251 | |
| 252 // If the image is partial we need to return a copy. This is to avoid future | |
| 253 // decode operations writing to the same bitmap. | |
| 254 SkBitmap copyBitmap; | |
| 255 return fullSizeBitmap.copyTo(©Bitmap, fullSizeBitmap.config(), m_allocat
or.get()) ? | |
| 256 ScaledImageFragment::createPartial(m_fullSize, index, nextGenerationId()
, copyBitmap) : nullptr; | |
| 257 } | |
| 258 | |
| 259 bool ImageFrameGenerator::hasAlpha(size_t index) | |
| 260 { | |
| 261 MutexLocker lock(m_alphaMutex); | |
| 262 if (index < m_hasAlpha.size()) | |
| 263 return m_hasAlpha[index]; | |
| 264 return true; | |
| 265 } | |
| 266 | |
| 267 } // namespace | |
| OLD | NEW |