Home | History | Annotate | Download | only in graphics
      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 "platform/graphics/ImageFrameGenerator.h"
     29 
     30 #include "platform/SharedBuffer.h"
     31 #include "platform/TraceEvent.h"
     32 #include "platform/graphics/DiscardablePixelRef.h"
     33 #include "platform/graphics/ImageDecodingStore.h"
     34 #include "platform/graphics/ScaledImageFragment.h"
     35 #include "platform/image-decoders/ImageDecoder.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<SharedBuffer> 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 allDataReceived)
     70 {
     71     m_data.setData(data.get(), allDataReceived);
     72 }
     73 
     74 void ImageFrameGenerator::copyData(RefPtr<SharedBuffer>* data, bool* allDataReceived)
     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& scaledSize, 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", this, "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 bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes)
    109 {
    110     // This method is called to populate a discardable memory owned by Skia.
    111     // Ideally we want the decoder to write directly to |pixels| but this
    112     // simple implementation copies from a decoded bitmap.
    113 
    114     // This implementation does not support scaling so check the requested size.
    115     ASSERT(m_fullSize.width() == info.fWidth);
    116     ASSERT(m_fullSize.height() == info.fHeight);
    117 
    118     // Don't use discardable memory for decoding if Skia is providing output
    119     // memory. By clearing the memory allocator decoding will use heap memory.
    120     //
    121     // TODO:
    122     // This is not pretty because this class is used in two different code
    123     // paths: discardable memory decoding on Android and discardable memory
    124     // in Skia. Once the transition to caching in Skia is complete we can get
    125     // rid of the logic that handles discardable memory.
    126     m_allocator.clear();
    127 
    128     const ScaledImageFragment* cachedImage = decodeAndScale(SkISize::Make(info.fWidth, info.fHeight), index);
    129     if (!cachedImage)
    130         return false;
    131 
    132     ASSERT(cachedImage->bitmap().width() == info.fWidth);
    133     ASSERT(cachedImage->bitmap().height() == info.fHeight);
    134 
    135     bool copied = cachedImage->bitmap().copyPixelsTo(pixels, rowBytes * info.fHeight, rowBytes);
    136     ImageDecodingStore::instance()->unlockCache(this, cachedImage);
    137     return copied;
    138 }
    139 
    140 const ScaledImageFragment* ImageFrameGenerator::tryToLockCompleteCache(const SkISize& scaledSize, size_t index)
    141 {
    142     const ScaledImageFragment* cachedImage = 0;
    143     if (ImageDecodingStore::instance()->lockCache(this, scaledSize, index, &cachedImage))
    144         return cachedImage;
    145     return 0;
    146 }
    147 
    148 const ScaledImageFragment* ImageFrameGenerator::tryToScale(const ScaledImageFragment* fullSizeImage, const SkISize& scaledSize, size_t index)
    149 {
    150     TRACE_EVENT0("webkit", "ImageFrameGenerator::tryToScale");
    151 
    152     // If the requested scaled size is the same as the full size then exit
    153     // early. This saves a cache lookup.
    154     if (scaledSize == m_fullSize)
    155         return 0;
    156 
    157     if (!fullSizeImage && !ImageDecodingStore::instance()->lockCache(this, m_fullSize, index, &fullSizeImage))
    158         return 0;
    159 
    160     // This call allocates the DiscardablePixelRef and lock/unlocks it
    161     // afterwards. So the memory allocated to the scaledBitmap can be
    162     // discarded after this call. Need to lock the scaledBitmap and
    163     // check the pixels before using it next time.
    164     SkBitmap scaledBitmap = skia::ImageOperations::Resize(fullSizeImage->bitmap(), resizeMethod(), scaledSize.width(), scaledSize.height(), m_allocator.get());
    165 
    166     OwnPtr<ScaledImageFragment> scaledImage;
    167     if (fullSizeImage->isComplete())
    168         scaledImage = ScaledImageFragment::createComplete(scaledSize, fullSizeImage->index(), scaledBitmap);
    169     else
    170         scaledImage = ScaledImageFragment::createPartial(scaledSize, fullSizeImage->index(), nextGenerationId(), scaledBitmap);
    171     ImageDecodingStore::instance()->unlockCache(this, fullSizeImage);
    172     return ImageDecodingStore::instance()->insertAndLockCache(this, scaledImage.release());
    173 }
    174 
    175 const ScaledImageFragment* ImageFrameGenerator::tryToResumeDecodeAndScale(const SkISize& scaledSize, size_t index)
    176 {
    177     TRACE_EVENT1("webkit", "ImageFrameGenerator::tryToResumeDecodeAndScale", "index", static_cast<int>(index));
    178 
    179     ImageDecoder* decoder = 0;
    180     const bool resumeDecoding = ImageDecodingStore::instance()->lockDecoder(this, m_fullSize, &decoder);
    181     ASSERT(!resumeDecoding || decoder);
    182 
    183     OwnPtr<ScaledImageFragment> fullSizeImage = decode(index, &decoder);
    184 
    185     if (!decoder)
    186         return 0;
    187 
    188     // If we are not resuming decoding that means the decoder is freshly
    189     // created and we have ownership. If we are resuming decoding then
    190     // the decoder is owned by ImageDecodingStore.
    191     OwnPtr<ImageDecoder> decoderContainer;
    192     if (!resumeDecoding)
    193         decoderContainer = adoptPtr(decoder);
    194 
    195     if (!fullSizeImage) {
    196         // If decode has failed and resulted an empty image we can save work
    197         // in the future by returning early.
    198         m_decodeFailedAndEmpty = !m_isMultiFrame && decoder->failed();
    199 
    200         if (resumeDecoding)
    201             ImageDecodingStore::instance()->unlockDecoder(this, decoder);
    202         return 0;
    203     }
    204 
    205     const ScaledImageFragment* cachedImage = ImageDecodingStore::instance()->insertAndLockCache(this, fullSizeImage.release());
    206 
    207     // If the image generated is complete then there is no need to keep
    208     // the decoder. The exception is multi-frame decoder which can generate
    209     // multiple complete frames.
    210     const bool removeDecoder = cachedImage->isComplete() && !m_isMultiFrame;
    211 
    212     if (resumeDecoding) {
    213         if (removeDecoder)
    214             ImageDecodingStore::instance()->removeDecoder(this, decoder);
    215         else
    216             ImageDecodingStore::instance()->unlockDecoder(this, decoder);
    217     } else if (!removeDecoder) {
    218         ImageDecodingStore::instance()->insertDecoder(this, decoderContainer.release(), DiscardablePixelRef::isDiscardable(cachedImage->bitmap().pixelRef()));
    219     }
    220 
    221     if (m_fullSize == scaledSize)
    222         return cachedImage;
    223     return tryToScale(cachedImage, scaledSize, index);
    224 }
    225 
    226 PassOwnPtr<ScaledImageFragment> ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder)
    227 {
    228     TRACE_EVENT2("webkit", "ImageFrameGenerator::decode", "width", m_fullSize.width(), "height", m_fullSize.height());
    229 
    230     ASSERT(decoder);
    231     SharedBuffer* data = 0;
    232     bool allDataReceived = false;
    233     m_data.data(&data, &allDataReceived);
    234 
    235     // Try to create an ImageDecoder if we are not given one.
    236     if (!*decoder) {
    237         if (m_imageDecoderFactory)
    238             *decoder = m_imageDecoderFactory->create().leakPtr();
    239 
    240         if (!*decoder)
    241             *decoder = ImageDecoder::create(*data, ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileApplied).leakPtr();
    242 
    243         if (!*decoder)
    244             return nullptr;
    245     }
    246 
    247     // TODO: this is very ugly. We need to refactor the way how we can pass a
    248     // memory allocator to image decoders.
    249     if (!m_isMultiFrame)
    250         (*decoder)->setMemoryAllocator(m_allocator.get());
    251     (*decoder)->setData(data, allDataReceived);
    252     // If this call returns a newly allocated DiscardablePixelRef, then
    253     // ImageFrame::m_bitmap and the contained DiscardablePixelRef are locked.
    254     // They will be unlocked when ImageDecoder is destroyed since ImageDecoder
    255     // owns the ImageFrame. Partially decoded SkBitmap is thus inserted into the
    256     // ImageDecodingStore while locked.
    257     ImageFrame* frame = (*decoder)->frameBufferAtIndex(index);
    258     (*decoder)->setData(0, false); // Unref SharedBuffer from ImageDecoder.
    259     (*decoder)->clearCacheExceptFrame(index);
    260 
    261     if (!frame || frame->status() == ImageFrame::FrameEmpty)
    262         return nullptr;
    263 
    264     const bool isComplete = frame->status() == ImageFrame::FrameComplete;
    265     SkBitmap fullSizeBitmap = frame->getSkBitmap();
    266     if (fullSizeBitmap.isNull())
    267         return nullptr;
    268 
    269     {
    270         MutexLocker lock(m_alphaMutex);
    271         if (index >= m_hasAlpha.size()) {
    272             const size_t oldSize = m_hasAlpha.size();
    273             m_hasAlpha.resize(index + 1);
    274             for (size_t i = oldSize; i < m_hasAlpha.size(); ++i)
    275                 m_hasAlpha[i] = true;
    276         }
    277         m_hasAlpha[index] = !fullSizeBitmap.isOpaque();
    278     }
    279     ASSERT(fullSizeBitmap.width() == m_fullSize.width() && fullSizeBitmap.height() == m_fullSize.height());
    280 
    281     if (isComplete)
    282         return ScaledImageFragment::createComplete(m_fullSize, index, fullSizeBitmap);
    283 
    284     // If the image is partial we need to return a copy. This is to avoid future
    285     // decode operations writing to the same bitmap.
    286     SkBitmap copyBitmap;
    287     return fullSizeBitmap.copyTo(&copyBitmap, fullSizeBitmap.config(), m_allocator.get()) ?
    288         ScaledImageFragment::createPartial(m_fullSize, index, nextGenerationId(), copyBitmap) : nullptr;
    289 }
    290 
    291 bool ImageFrameGenerator::hasAlpha(size_t index)
    292 {
    293     MutexLocker lock(m_alphaMutex);
    294     if (index < m_hasAlpha.size())
    295         return m_hasAlpha[index];
    296     return true;
    297 }
    298 
    299 } // namespace
    300