Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/BitmapImage.h

Issue 1925533003: High CPU and increased memory usage fix for high-res (GIF, WEBP...) animations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: @scroggo, thanks a lot - this makes page [3] to use 250MB (was 750MB) Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // 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 // This because we start and stop animating lazily. Animation starts when
74 // the image is rendered, and automatically pauses once all observers no 75 // the image is rendered, and automatically pauses once all observers no
75 // longer want to render the image. 76 // longer want to render the image.
76 void stopAnimation() override; 77 void stopAnimation();
Peter Kasting 2016/05/04 03:01:07 It's confusing that we mix overrides and non-overr
aleksandar.stojiljkovic 2016/05/04 20:56:29 Done. They are all still used (for testing).
Peter Kasting 2016/05/07 01:58:29 Still need to move sizeRespectingOrientation(), cu
aleksandar.stojiljkovic 2016/05/07 19:50:52 Thanks, makes sense. I would like to do this toget
77 void resetAnimation() override; 78 void resetAnimation() override;
78 bool maybeAnimated() override; 79 bool maybeAnimated() override;
79 80
80 void setAnimationPolicy(ImageAnimationPolicy policy) override { m_animationP olicy = policy; } 81 void setAnimationPolicy(ImageAnimationPolicy policy) override { m_animationP olicy = policy; }
81 ImageAnimationPolicy animationPolicy() override { return m_animationPolicy; } 82 ImageAnimationPolicy animationPolicy() override { return m_animationPolicy; }
82 void advanceTime(double deltaTimeInSeconds) override; 83 void advanceTime(double deltaTimeInSeconds) override;
83 84
84 PassRefPtr<SkImage> imageForCurrentFrame() override; 85 PassRefPtr<SkImage> imageForCurrentFrame() override;
85 PassRefPtr<Image> imageForDefaultFrame() override; 86 PassRefPtr<Image> imageForDefaultFrame() override;
86 87
87 bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override ; 88 bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override ;
88 bool currentFrameIsComplete() override; 89 bool currentFrameIsComplete() override;
89 bool currentFrameIsLazyDecoded() override; 90 bool currentFrameIsLazyDecoded() override;
90 91
92 // Called to wipe out the entire frame buffer cache and tell the image
93 // source to destroy everything; this is used when e.g. we want to free
94 // some room in the image cache.
Peter Kasting 2016/05/04 03:01:06 Seems like this comment should be on the base clas
aleksandar.stojiljkovic 2016/05/04 20:56:29 Descriptio applies to this class functionality (fr
95 void destroyDecodedData() override;
96
91 ImageOrientation currentFrameOrientation(); 97 ImageOrientation currentFrameOrientation();
92 98
93 // Construct a BitmapImage with the given orientation. 99 // Construct a BitmapImage with the given orientation.
94 static PassRefPtr<BitmapImage> createWithOrientationForTesting(const SkBitma p&, ImageOrientation); 100 static PassRefPtr<BitmapImage> createWithOrientationForTesting(const SkBitma p&, ImageOrientation);
95 // Advance the image animation by one frame. 101 // Advance the image animation by one frame.
96 void advanceAnimationForTesting() override { internalAdvanceAnimation(false) ; } 102 void advanceAnimationForTesting() override { internalAdvanceAnimation(false) ; }
97 103
98 private: 104 private:
99 friend class BitmapImageTest;
100
101 void updateSize() const;
102
103 private:
104 enum RepetitionCountStatus { 105 enum RepetitionCountStatus {
105 Unknown, // We haven't checked the source's repetition count. 106 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). 107 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. 108 Certain // The repetition count is known to be correct.
108 }; 109 };
109 110
110 BitmapImage(const SkBitmap &, ImageObserver* = 0); 111 BitmapImage(const SkBitmap &, ImageObserver* = 0);
111 BitmapImage(ImageObserver* = 0); 112 BitmapImage(ImageObserver* = 0);
112 113
114 static void setAnimationCacheSizeForTesting(size_t maxCacheSize, size_t maxA nimationSizeInCache);
Peter Kasting 2016/05/04 03:01:06 Please add descriptive comments on new functions y
aleksandar.stojiljkovic 2016/05/04 20:56:29 Done. This one is removed.
115
113 void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRe ct& srcRect, RespectImageOrientationEnum, ImageClampingMode) override; 116 void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRe ct& srcRect, RespectImageOrientationEnum, ImageClampingMode) override;
114 117
118 void updateSize() const;
115 size_t currentFrame() const { return m_currentFrame; } 119 size_t currentFrame() const { return m_currentFrame; }
116 size_t frameCount(); 120 size_t frameCount();
117 121
118 PassRefPtr<SkImage> frameAtIndex(size_t); 122 PassRefPtr<SkImage> frameAtIndex(size_t);
119 123
120 bool frameIsCompleteAtIndex(size_t); 124 bool frameIsCompleteAtIndex(size_t);
121 float frameDurationAtIndex(size_t); 125 float frameDurationAtIndex(size_t);
122 bool frameHasAlphaAtIndex(size_t); 126 bool frameHasAlphaAtIndex(size_t);
123 ImageOrientation frameOrientationAtIndex(size_t); 127 ImageOrientation frameOrientationAtIndex(size_t);
124 128
125 // Decodes and caches a frame. Never accessed except internally. 129 // Decodes and caches a frame. Never accessed except internally.
Peter Kasting 2016/05/04 03:01:06 While here: Why is "Never accessed except internal
aleksandar.stojiljkovic 2016/05/04 20:56:29 Done. Removed.
126 void cacheFrame(size_t index); 130 PassRefPtr<SkImage> cacheFrame(size_t index);
127
128 // Called before accessing m_frames[index]. Returns false on index out of bo unds.
129 bool ensureFrameIsCached(size_t index);
130 131
131 // Returns the total number of bytes allocated for all framebuffers, i.e. 132 // Returns the total number of bytes allocated for all framebuffers, i.e.
132 // the sum of m_source.frameBytesAtIndex(...) for all frames. 133 // the sum of m_source.frameBytesAtIndex(...) for all frames.
133 size_t totalFrameBytes(); 134 size_t totalFrameBytes();
134 135
135 // Called to invalidate cached data. When |destroyAll| is true, we wipe out
136 // the entire frame buffer cache and tell the image source to destroy
137 // everything; this is used when e.g. we want to free some room in the image
138 // cache. If |destroyAll| is false, we delete frames except the current
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
147 // Notifies observers that the memory footprint has changed. 136 // Notifies observers that the memory footprint has changed.
148 void notifyMemoryChanged(); 137 void notifyMemoryChanged();
149 138
150 // Whether or not size is available yet. 139 // Whether or not size is available yet.
151 bool isSizeAvailable(); 140 bool isSizeAvailable();
152 141
153 // Animation. 142 // Animation.
154 int repetitionCount(bool imageKnownToBeComplete); // |imageKnownToBeComplet e| should be set if the caller knows the entire image has been decoded. 143 int repetitionCount(bool imageKnownToBeComplete); // |imageKnownToBeComplet e| should be set if the caller knows the entire image has been decoded.
155 bool shouldAnimate(); 144 bool shouldAnimate();
156 void startAnimation(CatchUpAnimation = CatchUp) override; 145 void startAnimation(CatchUpAnimation = CatchUp) override;
157 void advanceAnimation(Timer<BitmapImage>*); 146 void advanceAnimation(Timer<BitmapImage>*);
158 147
159 // Function that does the real work of advancing the animation. When 148 // 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 149 // 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 150 // a bunch of animation frames, so we should not do things like decode each
162 // one or notify our observers. 151 // one or notify our observers.
163 // Returns whether the animation was advanced. 152 // Returns whether the animation was advanced.
164 bool internalAdvanceAnimation(bool skippingFrames); 153 bool internalAdvanceAnimation(bool skippingFrames);
165 154
166 ImageSource m_source; 155 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). 156 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; 157 mutable IntSize m_sizeRespectingOrientation;
169 158
170 size_t m_currentFrame; // The index of the current frame of animation. 159 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. 160 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 161
162 RefPtr<SkImage> m_frame; // One frame is cached (the most recently accessed) also for animations.
Peter Kasting 2016/05/04 03:01:07 "A cached copy of the most recently-accessed frame
aleksandar.stojiljkovic 2016/05/04 20:56:29 Done.
163 size_t m_frameIndex; // Index of the frame that is cached.
164
173 OwnPtr<Timer<BitmapImage>> m_frameTimer; 165 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. 166 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; 167 RepetitionCountStatus m_repetitionCountStatus;
176 int m_repetitionsComplete; // How many repetitions we've finished. 168 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(). 169 double m_desiredFrameStartTime; // The system time at which we hope to see the next call to startAnimation().
178 170
179 size_t m_frameCount; 171 size_t m_frameCount;
180 172
181 ImageAnimationPolicy m_animationPolicy; // Whether or not we can play animat ion. 173 ImageAnimationPolicy m_animationPolicy; // Whether or not we can play animat ion.
182 174
183 bool m_animationFinished : 1; // Whether or not we've completed the entire a nimation. 175 bool m_animationFinished : 1; // Whether or not we've completed the entire a nimation.
184 176
185 bool m_allDataReceived : 1; // Whether or not we've received all our data. 177 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. 178 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. 179 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_hasUniformFrameSize : 1; 180 mutable bool m_hasUniformFrameSize : 1;
189 mutable bool m_haveFrameCount : 1; 181 mutable bool m_haveFrameCount : 1;
190 }; 182 };
191 183
192 DEFINE_IMAGE_TYPE_CASTS(BitmapImage); 184 DEFINE_IMAGE_TYPE_CASTS(BitmapImage);
193 185
194 } // namespace blink 186 } // namespace blink
195 187
196 #endif 188 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698