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