2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
4 * Copyright (C) 2008-2009 Torch Mobile, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
31 #include "platform/geometry/IntSize.h"
32 #include "platform/graphics/Color.h"
33 #include "platform/graphics/FrameData.h"
34 #include "platform/graphics/Image.h"
35 #include "platform/graphics/ImageAnimationPolicy.h"
36 #include "platform/graphics/ImageOrientation.h"
37 #include "platform/graphics/ImageSource.h"
38 #include "platform/image-decoders/ImageAnimation.h"
39 #include "wtf/Forward.h"
40 #include "wtf/OwnPtr.h"
44 template <typename T
> class Timer
;
46 class PLATFORM_EXPORT BitmapImage final
: public Image
{
47 friend class GeneratedImage
;
48 friend class CrossfadeGeneratedImage
;
49 friend class GradientGeneratedImage
;
50 friend class GraphicsContext
;
52 static PassRefPtr
<BitmapImage
> create(ImageObserver
* observer
= 0)
54 return adoptRef(new BitmapImage(observer
));
57 // This allows constructing a BitmapImage with a forced non-default orientation.
58 static PassRefPtr
<BitmapImage
> createWithOrientationForTesting(const SkBitmap
&, ImageOrientation
);
60 ~BitmapImage() override
;
62 bool isBitmapImage() const override
;
64 bool currentFrameHasSingleSecurityOrigin() const override
;
66 IntSize
size() const override
;
67 IntSize
sizeRespectingOrientation() const;
68 bool getHotSpot(IntPoint
&) const override
;
69 String
filenameExtension() const override
;
70 bool dataChanged(bool allDataReceived
) override
;
72 bool isAllDataReceived() const { return m_allDataReceived
; }
73 bool hasColorProfile() const;
75 // It may look unusual that there's no start animation call as public API.
76 // This because we start and stop animating lazily. Animation starts when
77 // the image is rendered, and automatically pauses once all observers no
78 // longer want to render the image.
79 void stopAnimation() override
;
80 void resetAnimation() override
;
81 bool maybeAnimated() override
;
83 void setAnimationPolicy(ImageAnimationPolicy policy
) override
{ m_animationPolicy
= policy
; }
84 ImageAnimationPolicy
animationPolicy() override
{ return m_animationPolicy
; }
85 void advanceTime(double deltaTimeInSeconds
) override
;
87 // Advance the image animation by one frame.
88 void advanceAnimationForTesting() override
{ internalAdvanceAnimation(false); }
90 PassRefPtr
<SkImage
> imageForCurrentFrame() override
;
91 PassRefPtr
<Image
> imageForDefaultFrame() override
;
92 bool currentFrameKnownToBeOpaque() override
;
93 bool currentFrameIsComplete() override
;
94 bool currentFrameIsLazyDecoded() override
;
96 ImageOrientation
currentFrameOrientation();
99 friend class BitmapImageTest
;
101 void updateSize() const;
104 enum RepetitionCountStatus
{
105 Unknown
, // We haven't checked the source's repetition count.
106 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).
107 Certain
// The repetition count is known to be correct.
110 BitmapImage(const SkBitmap
&, ImageObserver
* = 0);
111 BitmapImage(ImageObserver
* = 0);
113 void draw(SkCanvas
*, const SkPaint
&, const FloatRect
& dstRect
, const FloatRect
& srcRect
, RespectImageOrientationEnum
, ImageClampingMode
) override
;
115 size_t currentFrame() const { return m_currentFrame
; }
118 PassRefPtr
<SkImage
> frameAtIndex(size_t);
120 bool frameIsCompleteAtIndex(size_t);
121 float frameDurationAtIndex(size_t);
122 bool frameHasAlphaAtIndex(size_t);
123 ImageOrientation
frameOrientationAtIndex(size_t);
125 // Decodes and caches a frame. Never accessed except internally.
126 void cacheFrame(size_t index
);
128 // Called before accessing m_frames[index]. Returns false on index out of bounds.
129 bool ensureFrameIsCached(size_t index
);
131 // Returns the total number of bytes allocated for all framebuffers, i.e.
132 // the sum of m_source.frameBytesAtIndex(...) for all frames. This is
133 // returned as an int for caller convenience, to allow safely subtracting
134 // the values from successive calls as signed expressions.
135 int totalFrameBytes();
137 // Called to invalidate cached data. When |destroyAll| is true, we wipe out
138 // the entire frame buffer cache and tell the image source to destroy
139 // everything; this is used when e.g. we want to free some room in the image
140 // cache. If |destroyAll| is false, we delete frames except the current
141 // frame; this is used while animating large images to keep memory footprint
142 // low; the decoder should preserve the current frame and may preserve some
143 // other frames to avoid redecoding the whole image on every frame.
144 void destroyDecodedData(bool destroyAll
) override
;
146 // If the image is large enough, calls destroyDecodedData().
147 void destroyDecodedDataIfNecessary();
149 // Generally called by destroyDecodedData(), destroys whole-image metadata
150 // and notifies observers that the memory footprint has (hopefully)
151 // decreased by |frameBytesCleared|.
152 void destroyMetadataAndNotify(size_t frameBytesCleared
);
154 // Whether or not size is available yet.
155 bool isSizeAvailable();
158 int repetitionCount(bool imageKnownToBeComplete
); // |imageKnownToBeComplete| should be set if the caller knows the entire image has been decoded.
159 bool shouldAnimate();
160 void startAnimation(CatchUpAnimation
= CatchUp
) override
;
161 void advanceAnimation(Timer
<BitmapImage
>*);
163 // Function that does the real work of advancing the animation. When
164 // skippingFrames is true, we're in the middle of a loop trying to skip over
165 // a bunch of animation frames, so we should not do things like decode each
166 // one or notify our observers.
167 // Returns whether the animation was advanced.
168 bool internalAdvanceAnimation(bool skippingFrames
);
170 ImageSource m_source
;
171 mutable IntSize m_size
; // The size to use for the overall image (will just be the size of the first image).
172 mutable IntSize m_sizeRespectingOrientation
;
174 size_t m_currentFrame
; // The index of the current frame of animation.
175 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.
177 OwnPtr
<Timer
<BitmapImage
>> m_frameTimer
;
178 int m_repetitionCount
; // How many total animation loops we should do. This will be cAnimationNone if this image type is incapable of animation.
179 RepetitionCountStatus m_repetitionCountStatus
;
180 int m_repetitionsComplete
; // How many repetitions we've finished.
181 double m_desiredFrameStartTime
; // The system time at which we hope to see the next call to startAnimation().
185 ImageAnimationPolicy m_animationPolicy
; // Whether or not we can play animation.
187 bool m_animationFinished
: 1; // Whether or not we've completed the entire animation.
189 bool m_allDataReceived
: 1; // Whether or not we've received all our data.
190 mutable bool m_haveSize
: 1; // Whether or not our |m_size| member variable has the final overall image size yet.
191 bool m_sizeAvailable
: 1; // Whether or not we can obtain the size of the first image frame yet from ImageIO.
192 mutable bool m_hasUniformFrameSize
: 1;
193 mutable bool m_haveFrameCount
: 1;
196 DEFINE_IMAGE_TYPE_CASTS(BitmapImage
);