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 #ifndef ImageFrameGenerator_h
     27 #define ImageFrameGenerator_h
     28 
     29 #include "SkBitmap.h"
     30 #include "SkSize.h"
     31 #include "SkTypes.h"
     32 #include "platform/PlatformExport.h"
     33 #include "platform/graphics/ThreadSafeDataTransport.h"
     34 #include "wtf/PassOwnPtr.h"
     35 #include "wtf/PassRefPtr.h"
     36 #include "wtf/RefCounted.h"
     37 #include "wtf/RefPtr.h"
     38 #include "wtf/ThreadingPrimitives.h"
     39 #include "wtf/ThreadSafeRefCounted.h"
     40 #include "wtf/Vector.h"
     41 
     42 namespace WebCore {
     43 
     44 class ImageDecoder;
     45 class ScaledImageFragment;
     46 class SharedBuffer;
     47 
     48 class PLATFORM_EXPORT ImageDecoderFactory {
     49     WTF_MAKE_NONCOPYABLE(ImageDecoderFactory);
     50 public:
     51     ImageDecoderFactory() {}
     52     virtual ~ImageDecoderFactory() { }
     53     virtual PassOwnPtr<ImageDecoder> create() = 0;
     54 };
     55 
     56 class PLATFORM_EXPORT ImageFrameGenerator : public ThreadSafeRefCounted<ImageFrameGenerator> {
     57     WTF_MAKE_NONCOPYABLE(ImageFrameGenerator);
     58 public:
     59     static PassRefPtr<ImageFrameGenerator> create(const SkISize& fullSize, PassRefPtr<SharedBuffer> data, bool allDataReceived, bool isMultiFrame = false)
     60     {
     61         return adoptRef(new ImageFrameGenerator(fullSize, data, allDataReceived, isMultiFrame));
     62     }
     63 
     64     ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<SharedBuffer>, bool allDataReceived, bool isMultiFrame);
     65     ~ImageFrameGenerator();
     66 
     67     const ScaledImageFragment* decodeAndScale(const SkISize& scaledSize, size_t index = 0);
     68 
     69     // Decodes and scales the specified frame indicated by |index|. Dimensions
     70     // and output format are specified in |info|. Decoded pixels are written
     71     // into |pixels| with a stride of |rowBytes|.
     72     //
     73     // Returns true if decoding was successful.
     74     bool decodeAndScale(const SkImageInfo&, size_t index, void* pixels, size_t rowBytes);
     75 
     76     void setData(PassRefPtr<SharedBuffer>, bool allDataReceived);
     77 
     78     // Creates a new SharedBuffer containing the data received so far.
     79     void copyData(RefPtr<SharedBuffer>*, bool* allDataReceived);
     80 
     81     SkISize getFullSize() const { return m_fullSize; }
     82 
     83     bool isMultiFrame() const { return m_isMultiFrame; }
     84 
     85     // FIXME: Return alpha state for each frame.
     86     bool hasAlpha(size_t);
     87 
     88 private:
     89     friend class ImageFrameGeneratorTest;
     90     friend class DeferredImageDecoderTest;
     91     // For testing. |factory| will overwrite the default ImageDecoder creation logic if |factory->create()| returns non-zero.
     92     void setImageDecoderFactory(PassOwnPtr<ImageDecoderFactory> factory) { m_imageDecoderFactory = factory; }
     93     // For testing.
     94     SkBitmap::Allocator* allocator() const { return m_allocator.get(); }
     95     void setAllocator(PassOwnPtr<SkBitmap::Allocator> allocator) { m_allocator = allocator; }
     96 
     97     // These methods are called while m_decodeMutex is locked.
     98     const ScaledImageFragment* tryToLockCompleteCache(const SkISize& scaledSize, size_t index);
     99     const ScaledImageFragment* tryToScale(const ScaledImageFragment* fullSizeImage, const SkISize& scaledSize, size_t index);
    100     const ScaledImageFragment* tryToResumeDecodeAndScale(const SkISize& scaledSize, size_t index);
    101 
    102     // Use the given decoder to decode. If a decoder is not given then try to create one.
    103     PassOwnPtr<ScaledImageFragment> decode(size_t index, ImageDecoder**);
    104 
    105     // Return the next generation ID of a new image object. This is used
    106     // to identify images of the same frame from different stages of
    107     // progressive decode.
    108     size_t nextGenerationId() { return m_decodeCount++; }
    109 
    110     SkISize m_fullSize;
    111     ThreadSafeDataTransport m_data;
    112     bool m_isMultiFrame;
    113     bool m_decodeFailedAndEmpty;
    114     Vector<bool> m_hasAlpha;
    115     size_t m_decodeCount;
    116     OwnPtr<SkBitmap::Allocator> m_allocator;
    117 
    118     OwnPtr<ImageDecoderFactory> m_imageDecoderFactory;
    119 
    120     // Prevents multiple decode operations on the same data.
    121     Mutex m_decodeMutex;
    122 
    123     // Protect concurrent access to m_hasAlpha.
    124     Mutex m_alphaMutex;
    125 };
    126 
    127 } // namespace WebCore
    128 
    129 #endif
    130