| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) | 2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) |
| 3 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved. | 3 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved. |
| 4 * Copyright (C) 2008-2009 Torch Mobile, Inc. | 4 * Copyright (C) 2008-2009 Torch Mobile, Inc. |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
| 8 * are met: | 8 * are met: |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 #include "platform/graphics/ImageSource.h" | 37 #include "platform/graphics/ImageSource.h" |
| 38 #include "platform/image-decoders/ImageAnimation.h" | 38 #include "platform/image-decoders/ImageAnimation.h" |
| 39 #include "wtf/Forward.h" | 39 #include "wtf/Forward.h" |
| 40 #include "wtf/OwnPtr.h" | 40 #include "wtf/OwnPtr.h" |
| 41 | 41 |
| 42 namespace blink { | 42 namespace blink { |
| 43 | 43 |
| 44 template <typename T> class Timer; | 44 template <typename T> class Timer; |
| 45 | 45 |
| 46 class PLATFORM_EXPORT BitmapImage final : public Image { | 46 class PLATFORM_EXPORT BitmapImage final : public Image { |
| 47 friend class BitmapImageTest; |
| 48 friend class CrossfadeGeneratedImage; |
| 47 friend class GeneratedImage; | 49 friend class GeneratedImage; |
| 48 friend class CrossfadeGeneratedImage; | |
| 49 friend class GradientGeneratedImage; | 50 friend class GradientGeneratedImage; |
| 50 friend class GraphicsContext; | 51 friend class GraphicsContext; |
| 51 public: | 52 public: |
| 52 static PassRefPtr<BitmapImage> create(ImageObserver* observer = 0) | 53 static PassRefPtr<BitmapImage> create(ImageObserver* observer = 0) |
| 53 { | 54 { |
| 54 return adoptRef(new BitmapImage(observer)); | 55 return adoptRef(new BitmapImage(observer)); |
| 55 } | 56 } |
| 56 | 57 |
| 57 ~BitmapImage() override; | 58 ~BitmapImage() override; |
| 58 | 59 |
| 59 bool isBitmapImage() const override { return true; } | 60 bool isBitmapImage() const override { return true; } |
| 60 | 61 |
| 61 bool currentFrameHasSingleSecurityOrigin() const override; | 62 bool currentFrameHasSingleSecurityOrigin() const override; |
| 62 | 63 |
| 63 IntSize size() const override; | 64 IntSize size() const override; |
| 64 IntSize sizeRespectingOrientation() const; | 65 IntSize sizeRespectingOrientation() const; |
| 65 bool getHotSpot(IntPoint&) const override; | 66 bool getHotSpot(IntPoint&) const override; |
| 66 String filenameExtension() const override; | 67 String filenameExtension() const override; |
| 67 bool dataChanged(bool allDataReceived) override; | 68 bool dataChanged(bool allDataReceived) override; |
| 68 | 69 |
| 69 bool isAllDataReceived() const { return m_allDataReceived; } | 70 bool isAllDataReceived() const { return m_allDataReceived; } |
| 70 bool hasColorProfile() const; | 71 bool hasColorProfile() const; |
| 71 | 72 |
| 72 // It may look unusual that there's no start animation call as public API. | |
| 73 // This because we start and stop animating lazily. Animation starts when | |
| 74 // the image is rendered, and automatically pauses once all observers no | |
| 75 // longer want to render the image. | |
| 76 void stopAnimation() override; | |
| 77 void resetAnimation() override; | 73 void resetAnimation() override; |
| 78 bool maybeAnimated() override; | 74 bool maybeAnimated() override; |
| 79 | 75 |
| 80 void setAnimationPolicy(ImageAnimationPolicy policy) override { m_animationP
olicy = policy; } | 76 void setAnimationPolicy(ImageAnimationPolicy policy) override { m_animationP
olicy = policy; } |
| 81 ImageAnimationPolicy animationPolicy() override { return m_animationPolicy;
} | 77 ImageAnimationPolicy animationPolicy() override { return m_animationPolicy;
} |
| 82 void advanceTime(double deltaTimeInSeconds) override; | 78 void advanceTime(double deltaTimeInSeconds) override; |
| 83 | 79 |
| 84 PassRefPtr<SkImage> imageForCurrentFrame() override; | 80 PassRefPtr<SkImage> imageForCurrentFrame() override; |
| 85 PassRefPtr<Image> imageForDefaultFrame() override; | 81 PassRefPtr<Image> imageForDefaultFrame() override; |
| 86 | 82 |
| 87 bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override
; | 83 bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override
; |
| 88 bool currentFrameIsComplete() override; | 84 bool currentFrameIsComplete() override; |
| 89 bool currentFrameIsLazyDecoded() override; | 85 bool currentFrameIsLazyDecoded() override; |
| 90 | 86 |
| 91 ImageOrientation currentFrameOrientation(); | 87 ImageOrientation currentFrameOrientation(); |
| 92 | 88 |
| 93 // Construct a BitmapImage with the given orientation. | 89 // Construct a BitmapImage with the given orientation. |
| 94 static PassRefPtr<BitmapImage> createWithOrientationForTesting(const SkBitma
p&, ImageOrientation); | 90 static PassRefPtr<BitmapImage> createWithOrientationForTesting(const SkBitma
p&, ImageOrientation); |
| 95 // Advance the image animation by one frame. | 91 // Advance the image animation by one frame. |
| 96 void advanceAnimationForTesting() override { internalAdvanceAnimation(false)
; } | 92 void advanceAnimationForTesting() override { internalAdvanceAnimation(false)
; } |
| 97 | 93 |
| 98 private: | 94 private: |
| 99 friend class BitmapImageTest; | |
| 100 | |
| 101 void updateSize() const; | |
| 102 | |
| 103 private: | |
| 104 enum RepetitionCountStatus { | 95 enum RepetitionCountStatus { |
| 105 Unknown, // We haven't checked the source's repetition count. | 96 Unknown, // We haven't checked the source's repetition count. |
| 106 Uncertain, // We have a repetition count, but it might be wrong (some GIF
s have a count after the image data, and will report "loop once" until all data
has been decoded). | 97 Uncertain, // We have a repetition count, but it might be wrong (some GIF
s 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. | 98 Certain // The repetition count is known to be correct. |
| 108 }; | 99 }; |
| 109 | 100 |
| 110 BitmapImage(const SkBitmap &, ImageObserver* = 0); | 101 BitmapImage(const SkBitmap &, ImageObserver* = 0); |
| 111 BitmapImage(ImageObserver* = 0); | 102 BitmapImage(ImageObserver* = 0); |
| 112 | 103 |
| 113 void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRe
ct& srcRect, RespectImageOrientationEnum, ImageClampingMode) override; | 104 void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRe
ct& srcRect, RespectImageOrientationEnum, ImageClampingMode) override; |
| 114 | 105 |
| 115 size_t currentFrame() const { return m_currentFrame; } | 106 size_t currentFrame() const { return m_currentFrame; } |
| 116 size_t frameCount(); | 107 size_t frameCount(); |
| 117 | 108 |
| 118 PassRefPtr<SkImage> frameAtIndex(size_t); | 109 PassRefPtr<SkImage> frameAtIndex(size_t); |
| 119 | 110 |
| 120 bool frameIsCompleteAtIndex(size_t); | 111 bool frameIsCompleteAtIndex(size_t); |
| 121 float frameDurationAtIndex(size_t); | 112 float frameDurationAtIndex(size_t); |
| 122 bool frameHasAlphaAtIndex(size_t); | 113 bool frameHasAlphaAtIndex(size_t); |
| 123 ImageOrientation frameOrientationAtIndex(size_t); | 114 ImageOrientation frameOrientationAtIndex(size_t); |
| 124 | 115 |
| 125 // Decodes and caches a frame. Never accessed except internally. | 116 PassRefPtr<SkImage> decodeAndCacheFrame(size_t index); |
| 126 void cacheFrame(size_t index); | 117 void updateSize() const; |
| 127 | |
| 128 // Called before accessing m_frames[index]. Returns false on index out of bo
unds. | |
| 129 bool ensureFrameIsCached(size_t index); | |
| 130 | 118 |
| 131 // Returns the total number of bytes allocated for all framebuffers, i.e. | 119 // Returns the total number of bytes allocated for all framebuffers, i.e. |
| 132 // the sum of m_source.frameBytesAtIndex(...) for all frames. | 120 // the sum of m_source.frameBytesAtIndex(...) for all frames. |
| 133 size_t totalFrameBytes(); | 121 size_t totalFrameBytes(); |
| 134 | 122 |
| 135 // Called to invalidate cached data. When |destroyAll| is true, we wipe out | 123 // Called to wipe out the entire frame buffer cache and tell the image |
| 136 // the entire frame buffer cache and tell the image source to destroy | 124 // source to destroy everything; this is used when e.g. we want to free |
| 137 // everything; this is used when e.g. we want to free some room in the image | 125 // some room in the image cache. |
| 138 // cache. If |destroyAll| is false, we delete frames except the current | 126 void destroyDecodedData() override; |
| 139 // frame; this is used while animating large images to keep memory footprint | |
| 140 // low; the decoder should preserve the current frame and may preserve some | |
| 141 // other frames to avoid redecoding the whole image on every frame. | |
| 142 void destroyDecodedData(bool destroyAll) override; | |
| 143 | |
| 144 // If the image is large enough, calls destroyDecodedData(). | |
| 145 void destroyDecodedDataIfNecessary(); | |
| 146 | 127 |
| 147 // Notifies observers that the memory footprint has changed. | 128 // Notifies observers that the memory footprint has changed. |
| 148 void notifyMemoryChanged(); | 129 void notifyMemoryChanged(); |
| 149 | 130 |
| 150 // Whether or not size is available yet. | 131 // Whether or not size is available yet. |
| 151 bool isSizeAvailable(); | 132 bool isSizeAvailable(); |
| 152 | 133 |
| 153 // Animation. | 134 // Animation. |
| 135 // We start and stop animating lazily. Animation starts when the image is |
| 136 // rendered, and automatically stops once no observer wants to render the |
| 137 // image. |
| 154 int repetitionCount(bool imageKnownToBeComplete); // |imageKnownToBeComplet
e| should be set if the caller knows the entire image has been decoded. | 138 int repetitionCount(bool imageKnownToBeComplete); // |imageKnownToBeComplet
e| should be set if the caller knows the entire image has been decoded. |
| 155 bool shouldAnimate(); | 139 bool shouldAnimate(); |
| 156 void startAnimation(CatchUpAnimation = CatchUp) override; | 140 void startAnimation(CatchUpAnimation = CatchUp) override; |
| 141 void stopAnimation(); |
| 157 void advanceAnimation(Timer<BitmapImage>*); | 142 void advanceAnimation(Timer<BitmapImage>*); |
| 158 | 143 |
| 159 // Function that does the real work of advancing the animation. When | 144 // Function that does the real work of advancing the animation. When |
| 160 // skippingFrames is true, we're in the middle of a loop trying to skip over | 145 // skippingFrames is true, we're in the middle of a loop trying to skip over |
| 161 // a bunch of animation frames, so we should not do things like decode each | 146 // a bunch of animation frames, so we should not do things like decode each |
| 162 // one or notify our observers. | 147 // one or notify our observers. |
| 163 // Returns whether the animation was advanced. | 148 // Returns whether the animation was advanced. |
| 164 bool internalAdvanceAnimation(bool skippingFrames); | 149 bool internalAdvanceAnimation(bool skippingFrames); |
| 165 | 150 |
| 166 ImageSource m_source; | 151 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). | 152 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; | 153 mutable IntSize m_sizeRespectingOrientation; |
| 169 | 154 |
| 170 size_t m_currentFrame; // The index of the current frame of animation. | 155 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 anima
tion. We have to ref frames to pin them in the cache. | 156 Vector<FrameData, 1> m_frames; // An array of the cached frames of the anima
tion. We have to ref frames to pin them in the cache. |
| 172 | 157 |
| 158 RefPtr<SkImage> m_cachedFrame; // A cached copy of the most recently-accesse
d frame. |
| 159 size_t m_cachedFrameIndex; // Index of the frame that is cached. |
| 160 |
| 173 OwnPtr<Timer<BitmapImage>> m_frameTimer; | 161 OwnPtr<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. | 162 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; | 163 RepetitionCountStatus m_repetitionCountStatus; |
| 176 int m_repetitionsComplete; // How many repetitions we've finished. | 164 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(). | 165 double m_desiredFrameStartTime; // The system time at which we hope to see
the next call to startAnimation(). |
| 178 | 166 |
| 179 size_t m_frameCount; | 167 size_t m_frameCount; |
| 180 | 168 |
| 181 ImageAnimationPolicy m_animationPolicy; // Whether or not we can play animat
ion. | 169 ImageAnimationPolicy m_animationPolicy; // Whether or not we can play animat
ion. |
| 182 | 170 |
| 183 bool m_animationFinished : 1; // Whether or not we've completed the entire a
nimation. | 171 bool m_animationFinished : 1; // Whether or not we've completed the entire a
nimation. |
| 184 | 172 |
| 185 bool m_allDataReceived : 1; // Whether or not we've received all our data. | 173 bool m_allDataReceived : 1; // Whether or not we've received all our data. |
| 186 mutable bool m_haveSize : 1; // Whether or not our |m_size| member variable
has the final overall image size yet. | 174 mutable bool m_haveSize : 1; // Whether or not our |m_size| member variable
has the final overall image size yet. |
| 187 bool m_sizeAvailable : 1; // Whether or not we can obtain the size of the fi
rst image frame yet from ImageIO. | 175 bool m_sizeAvailable : 1; // Whether or not we can obtain the size of the fi
rst image frame yet from ImageIO. |
| 188 mutable bool m_haveFrameCount : 1; | 176 mutable bool m_haveFrameCount : 1; |
| 189 }; | 177 }; |
| 190 | 178 |
| 191 DEFINE_IMAGE_TYPE_CASTS(BitmapImage); | 179 DEFINE_IMAGE_TYPE_CASTS(BitmapImage); |
| 192 | 180 |
| 193 } // namespace blink | 181 } // namespace blink |
| 194 | 182 |
| 195 #endif | 183 #endif |
| OLD | NEW |