Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2006 Samuel Weinig (sam.weinig (at) gmail.com)
      3  * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.  All rights reserved.
      4  * Copyright (C) 2008-2009 Torch Mobile, Inc.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef BitmapImage_h
     29 #define BitmapImage_h
     30 
     31 #include "core/platform/graphics/Color.h"
     32 #include "core/platform/graphics/FrameData.h"
     33 #include "core/platform/graphics/Image.h"
     34 #include "core/platform/graphics/ImageOrientation.h"
     35 #include "core/platform/graphics/ImageSource.h"
     36 #include "core/platform/graphics/IntSize.h"
     37 #include "wtf/Forward.h"
     38 
     39 namespace WebCore {
     40 
     41 class NativeImageSkia;
     42 template <typename T> class Timer;
     43 
     44 class BitmapImage : public Image {
     45     friend class GeneratedImage;
     46     friend class CrossfadeGeneratedImage;
     47     friend class GeneratorGeneratedImage;
     48     friend class GraphicsContext;
     49 public:
     50     static PassRefPtr<BitmapImage> create(PassRefPtr<NativeImageSkia> nativeImage, ImageObserver* observer = 0)
     51     {
     52         return adoptRef(new BitmapImage(nativeImage, observer));
     53     }
     54     static PassRefPtr<BitmapImage> create(ImageObserver* observer = 0)
     55     {
     56         return adoptRef(new BitmapImage(observer));
     57     }
     58     virtual ~BitmapImage();
     59 
     60     virtual bool isBitmapImage() const OVERRIDE;
     61 
     62     virtual bool hasSingleSecurityOrigin() const OVERRIDE;
     63 
     64     virtual IntSize size() const OVERRIDE;
     65     IntSize sizeRespectingOrientation() const;
     66     IntSize currentFrameSize() const;
     67     virtual bool getHotSpot(IntPoint&) const OVERRIDE;
     68 
     69     virtual bool dataChanged(bool allDataReceived) OVERRIDE;
     70     virtual String filenameExtension() const OVERRIDE;
     71 
     72     // It may look unusual that there is no start animation call as public API.  This is because
     73     // we start and stop animating lazily.  Animation begins whenever someone draws the image.  It will
     74     // automatically pause once all observers no longer want to render the image anywhere.
     75     virtual void stopAnimation() OVERRIDE;
     76     virtual void resetAnimation() OVERRIDE;
     77 
     78     virtual unsigned decodedSize() const OVERRIDE;
     79 
     80     virtual PassRefPtr<NativeImageSkia> nativeImageForCurrentFrame() OVERRIDE;
     81     virtual bool currentFrameKnownToBeOpaque() OVERRIDE;
     82 
     83     ImageOrientation currentFrameOrientation();
     84 
     85 #if !ASSERT_DISABLED
     86     virtual bool notSolidColor() OVERRIDE;
     87 #endif
     88 
     89 private:
     90     friend class BitmapImageTest;
     91 
     92     void updateSize() const;
     93 
     94 protected:
     95     enum RepetitionCountStatus {
     96       Unknown,    // We haven't checked the source's repetition count.
     97       Uncertain,  // We have a repetition count, but it might be wrong (some GIFs have a count after the image data, and will report "loop once" until all data has been decoded).
     98       Certain     // The repetition count is known to be correct.
     99     };
    100 
    101     BitmapImage(PassRefPtr<NativeImageSkia>, ImageObserver* = 0);
    102     BitmapImage(ImageObserver* = 0);
    103 
    104     virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator, BlendMode) OVERRIDE;
    105     virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator, BlendMode, RespectImageOrientationEnum) OVERRIDE;
    106 
    107     size_t currentFrame() const { return m_currentFrame; }
    108     size_t frameCount();
    109     PassRefPtr<NativeImageSkia> frameAtIndex(size_t);
    110     bool frameIsCompleteAtIndex(size_t);
    111     float frameDurationAtIndex(size_t);
    112     bool frameHasAlphaAtIndex(size_t);
    113     ImageOrientation frameOrientationAtIndex(size_t);
    114 
    115     // Decodes and caches a frame. Never accessed except internally.
    116     void cacheFrame(size_t index);
    117     // Called before accessing m_frames[index]. Returns false on index out of bounds.
    118     bool ensureFrameIsCached(size_t index);
    119 
    120     // Called to invalidate cached data. When |destroyAll| is true, we wipe out
    121     // the entire frame buffer cache and tell the image source to destroy
    122     // everything; this is used when e.g. we want to free some room in the image
    123     // cache. If |destroyAll| is false, we delete frames except the current
    124     // frame; this is used while animating large images to keep memory footprint
    125     // low; the decoder should preserve the current frame and may preserve some
    126     // other frames to avoid redecoding the whole image on every frame.
    127     virtual void destroyDecodedData(bool destroyAll) OVERRIDE;
    128 
    129     // If the image is large enough, calls destroyDecodedData().
    130     void destroyDecodedDataIfNecessary();
    131 
    132     // Generally called by destroyDecodedData(), destroys whole-image metadata
    133     // and notifies observers that the memory footprint has (hopefully)
    134     // decreased by |frameBytesCleared|.
    135     void destroyMetadataAndNotify(size_t frameBytesCleared);
    136 
    137     // Whether or not size is available yet.
    138     bool isSizeAvailable();
    139 
    140     // Called after asking the source for any information that may require
    141     // decoding part of the image (e.g., the image size).  We need to report
    142     // the partially decoded data to our observer so it has an accurate
    143     // account of the BitmapImage's memory usage.
    144     void didDecodeProperties() const;
    145 
    146     // Animation.
    147     int repetitionCount(bool imageKnownToBeComplete);  // |imageKnownToBeComplete| should be set if the caller knows the entire image has been decoded.
    148     bool shouldAnimate();
    149     virtual void startAnimation(bool catchUpIfNecessary = true) OVERRIDE;
    150     void advanceAnimation(Timer<BitmapImage>*);
    151 
    152     // Function that does the real work of advancing the animation.  When
    153     // skippingFrames is true, we're in the middle of a loop trying to skip over
    154     // a bunch of animation frames, so we should not do things like decode each
    155     // one or notify our observers.
    156     // Returns whether the animation was advanced.
    157     bool internalAdvanceAnimation(bool skippingFrames);
    158 
    159     // Checks to see if the image is a 1x1 solid color.  We optimize these images and just do a fill rect instead.
    160     // This check should happen regardless whether m_checkedForSolidColor is already set, as the frame may have
    161     // changed.
    162     void checkForSolidColor();
    163 
    164     virtual bool mayFillWithSolidColor();
    165     virtual Color solidColor() const;
    166 
    167     ImageSource m_source;
    168     mutable IntSize m_size; // The size to use for the overall image (will just be the size of the first image).
    169     mutable IntSize m_sizeRespectingOrientation;
    170 
    171     size_t m_currentFrame; // The index of the current frame of animation.
    172     Vector<FrameData, 1> m_frames; // An array of the cached frames of the animation. We have to ref frames to pin them in the cache.
    173 
    174     Timer<BitmapImage>* m_frameTimer;
    175     int m_repetitionCount; // How many total animation loops we should do.  This will be cAnimationNone if this image type is incapable of animation.
    176     RepetitionCountStatus m_repetitionCountStatus;
    177     int m_repetitionsComplete;  // How many repetitions we've finished.
    178     double m_desiredFrameStartTime;  // The system time at which we hope to see the next call to startAnimation().
    179 
    180     Color m_solidColor;  // If we're a 1x1 solid color, this is the color to use to fill.
    181 
    182     unsigned m_decodedSize; // The current size of all decoded frames.
    183     mutable unsigned m_decodedPropertiesSize; // The size of data decoded by the source to determine image properties (e.g. size, frame count, etc).
    184     size_t m_frameCount;
    185 
    186     bool m_isSolidColor : 1; // Whether or not we are a 1x1 solid image.
    187     bool m_checkedForSolidColor : 1; // Whether we've checked the frame for solid color.
    188 
    189     bool m_animationFinished : 1; // Whether or not we've completed the entire animation.
    190 
    191     bool m_allDataReceived : 1; // Whether or not we've received all our data.
    192     mutable bool m_haveSize : 1; // Whether or not our |m_size| member variable has the final overall image size yet.
    193     bool m_sizeAvailable : 1; // Whether or not we can obtain the size of the first image frame yet from ImageIO.
    194     mutable bool m_hasUniformFrameSize : 1;
    195     mutable bool m_haveFrameCount : 1;
    196 };
    197 
    198 }
    199 
    200 #endif
    201