1 | /* |
2 | * Copyright (C) 2006 Samuel Weinig ([email protected]) |
3 | * Copyright (C) 2004, 2005, 2006, 2013 Apple 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 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 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 | #pragma once |
29 | |
30 | #include "Image.h" |
31 | #include "Color.h" |
32 | #include "ImageObserver.h" |
33 | #include "ImageOrientation.h" |
34 | #include "ImageSource.h" |
35 | #include "IntSize.h" |
36 | |
37 | #if USE(CG) || USE(APPKIT) |
38 | #include <wtf/RetainPtr.h> |
39 | #endif |
40 | |
41 | #if USE(APPKIT) |
42 | OBJC_CLASS NSImage; |
43 | #endif |
44 | |
45 | #if PLATFORM(WIN) |
46 | typedef struct HBITMAP__ *HBITMAP; |
47 | #endif |
48 | |
49 | namespace WebCore { |
50 | |
51 | class Settings; |
52 | class Timer; |
53 | |
54 | class BitmapImage final : public Image { |
55 | public: |
56 | static Ref<BitmapImage> create(NativeImagePtr&& nativeImage, ImageObserver* observer = nullptr) |
57 | { |
58 | return adoptRef(*new BitmapImage(WTFMove(nativeImage), observer)); |
59 | } |
60 | static Ref<BitmapImage> create(ImageObserver* observer = nullptr) |
61 | { |
62 | return adoptRef(*new BitmapImage(observer)); |
63 | } |
64 | #if PLATFORM(WIN) |
65 | WEBCORE_EXPORT static RefPtr<BitmapImage> create(HBITMAP); |
66 | #endif |
67 | virtual ~BitmapImage(); |
68 | |
69 | void updateFromSettings(const Settings&); |
70 | |
71 | bool hasSingleSecurityOrigin() const override { return true; } |
72 | |
73 | EncodedDataStatus dataChanged(bool allDataReceived) override; |
74 | unsigned decodedSize() const { return m_source->decodedSize(); } |
75 | |
76 | EncodedDataStatus encodedDataStatus() const { return m_source->encodedDataStatus(); } |
77 | size_t frameCount() const { return m_source->frameCount(); } |
78 | RepetitionCount repetitionCount() const { return m_source->repetitionCount(); } |
79 | String uti() const override { return m_source->uti(); } |
80 | String filenameExtension() const override { return m_source->filenameExtension(); } |
81 | Optional<IntPoint> hotSpot() const override { return m_source->hotSpot(); } |
82 | |
83 | // FloatSize due to override. |
84 | FloatSize size() const override { return m_source->size(); } |
85 | IntSize sizeRespectingOrientation() const { return m_source->sizeRespectingOrientation(); } |
86 | Color singlePixelSolidColor() const override { return m_source->singlePixelSolidColor(); } |
87 | bool frameIsBeingDecodedAndIsCompatibleWithOptionsAtIndex(size_t index, const DecodingOptions& decodingOptions) const { return m_source->frameIsBeingDecodedAndIsCompatibleWithOptionsAtIndex(index, decodingOptions); } |
88 | DecodingStatus frameDecodingStatusAtIndex(size_t index) const { return m_source->frameDecodingStatusAtIndex(index); } |
89 | bool frameIsCompleteAtIndex(size_t index) const { return frameDecodingStatusAtIndex(index) == DecodingStatus::Complete; } |
90 | bool frameHasAlphaAtIndex(size_t index) const { return m_source->frameHasAlphaAtIndex(index); } |
91 | |
92 | bool frameHasFullSizeNativeImageAtIndex(size_t index, SubsamplingLevel subsamplingLevel) { return m_source->frameHasFullSizeNativeImageAtIndex(index, subsamplingLevel); } |
93 | bool frameHasDecodedNativeImageCompatibleWithOptionsAtIndex(size_t index, const Optional<SubsamplingLevel>& subsamplingLevel, const DecodingOptions& decodingOptions) { return m_source->frameHasDecodedNativeImageCompatibleWithOptionsAtIndex(index, subsamplingLevel, decodingOptions); } |
94 | |
95 | SubsamplingLevel frameSubsamplingLevelAtIndex(size_t index) const { return m_source->frameSubsamplingLevelAtIndex(index); } |
96 | |
97 | Seconds frameDurationAtIndex(size_t index) const { return m_source->frameDurationAtIndex(index); } |
98 | ImageOrientation frameOrientationAtIndex(size_t index) const { return m_source->frameOrientationAtIndex(index); } |
99 | |
100 | size_t currentFrame() const { return m_currentFrame; } |
101 | bool currentFrameKnownToBeOpaque() const override { return !frameHasAlphaAtIndex(currentFrame()); } |
102 | ImageOrientation orientationForCurrentFrame() const { return frameOrientationAtIndex(currentFrame()); } |
103 | bool canAnimate() const; |
104 | |
105 | bool shouldUseAsyncDecodingForTesting() const { return m_source->frameDecodingDurationForTesting() > 0_s; } |
106 | void setFrameDecodingDurationForTesting(Seconds duration) { m_source->setFrameDecodingDurationForTesting(duration); } |
107 | bool canUseAsyncDecodingForLargeImages() const; |
108 | bool shouldUseAsyncDecodingForAnimatedImages() const; |
109 | void setClearDecoderAfterAsyncFrameRequestForTesting(bool value) { m_clearDecoderAfterAsyncFrameRequestForTesting = value; } |
110 | void setLargeImageAsyncDecodingEnabledForTesting(bool enabled) { m_largeImageAsyncDecodingEnabledForTesting = enabled; } |
111 | bool isLargeImageAsyncDecodingEnabledForTesting() const { return m_largeImageAsyncDecodingEnabledForTesting; } |
112 | void stopAsyncDecodingQueue() { m_source->stopAsyncDecodingQueue(); } |
113 | |
114 | WEBCORE_EXPORT unsigned decodeCountForTesting() const; |
115 | |
116 | // Accessors for native image formats. |
117 | #if USE(APPKIT) |
118 | NSImage *nsImage() override; |
119 | RetainPtr<NSImage> snapshotNSImage() override; |
120 | #endif |
121 | |
122 | #if PLATFORM(COCOA) |
123 | CFDataRef tiffRepresentation() override; |
124 | #endif |
125 | |
126 | #if PLATFORM(WIN) |
127 | bool getHBITMAP(HBITMAP) override; |
128 | bool getHBITMAPOfSize(HBITMAP, const IntSize*) override; |
129 | #endif |
130 | |
131 | #if PLATFORM(GTK) |
132 | GdkPixbuf* getGdkPixbuf() override; |
133 | #endif |
134 | |
135 | WEBCORE_EXPORT NativeImagePtr nativeImage(const GraphicsContext* = nullptr) override; |
136 | NativeImagePtr nativeImageForCurrentFrame(const GraphicsContext* = nullptr) override; |
137 | #if USE(CG) |
138 | NativeImagePtr nativeImageOfSize(const IntSize&, const GraphicsContext* = nullptr) override; |
139 | Vector<NativeImagePtr> framesNativeImages(); |
140 | #endif |
141 | |
142 | void imageFrameAvailableAtIndex(size_t); |
143 | void decode(Function<void()>&&); |
144 | |
145 | protected: |
146 | WEBCORE_EXPORT BitmapImage(NativeImagePtr&&, ImageObserver* = nullptr); |
147 | WEBCORE_EXPORT BitmapImage(ImageObserver* = nullptr); |
148 | |
149 | NativeImagePtr frameImageAtIndex(size_t index) { return m_source->frameImageAtIndex(index); } |
150 | NativeImagePtr frameImageAtIndexCacheIfNeeded(size_t, SubsamplingLevel = SubsamplingLevel::Default, const GraphicsContext* = nullptr); |
151 | |
152 | // Called to invalidate cached data. When |destroyAll| is true, we wipe out |
153 | // the entire frame buffer cache and tell the image source to destroy |
154 | // everything; this is used when e.g. we want to free some room in the image |
155 | // cache. If |destroyAll| is false, we only delete frames up to the current |
156 | // one; this is used while animating large images to keep memory footprint |
157 | // low without redecoding the whole image on every frame. |
158 | void destroyDecodedData(bool destroyAll = true) override; |
159 | |
160 | // If the image is large enough, calls destroyDecodedData() and passes |
161 | // |destroyAll| along. |
162 | void destroyDecodedDataIfNecessary(bool destroyAll = true); |
163 | |
164 | ImageDrawResult draw(GraphicsContext&, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator, BlendMode, DecodingMode, ImageOrientationDescription) override; |
165 | void drawPattern(GraphicsContext&, const FloatRect& destRect, const FloatRect& srcRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator, BlendMode = BlendMode::Normal) override; |
166 | #if PLATFORM(WIN) |
167 | void drawFrameMatchingSourceSize(GraphicsContext&, const FloatRect& dstRect, const IntSize& srcSize, CompositeOperator) override; |
168 | #endif |
169 | |
170 | // Animation. |
171 | enum class StartAnimationStatus { CannotStart, IncompleteData, TimerActive, DecodingActive, Started }; |
172 | bool isAnimated() const override { return m_source->frameCount() > 1; } |
173 | bool shouldAnimate() const; |
174 | void startAnimation() override { internalStartAnimation(); } |
175 | StartAnimationStatus internalStartAnimation(); |
176 | void advanceAnimation(); |
177 | void internalAdvanceAnimation(); |
178 | bool isAnimating() const final; |
179 | |
180 | // It may look unusual that there is no start animation call as public API. This is because |
181 | // we start and stop animating lazily. Animation begins whenever someone draws the image. It will |
182 | // automatically pause once all observers no longer want to render the image anywhere. |
183 | void stopAnimation() override; |
184 | void resetAnimation() override; |
185 | |
186 | // Handle platform-specific data |
187 | void invalidatePlatformData(); |
188 | |
189 | #if !ASSERT_DISABLED |
190 | bool notSolidColor() override; |
191 | #endif |
192 | |
193 | #if PLATFORM(COCOA) |
194 | RetainPtr<CFDataRef> tiffRepresentation(const Vector<NativeImagePtr>&); |
195 | #endif |
196 | |
197 | private: |
198 | void clearTimer(); |
199 | void startTimer(Seconds delay); |
200 | SubsamplingLevel subsamplingLevelForScaleFactor(GraphicsContext&, const FloatSize& scaleFactor); |
201 | bool canDestroyDecodedData(); |
202 | void setCurrentFrameDecodingStatusIfNecessary(DecodingStatus); |
203 | bool isBitmapImage() const override { return true; } |
204 | void callDecodingCallbacks(); |
205 | void dump(WTF::TextStream&) const override; |
206 | |
207 | // Animated images over a certain size are considered large enough that we'll only hang on to one frame at a time. |
208 | static const unsigned LargeAnimationCutoff = 30 * 1024 * 1024; |
209 | |
210 | mutable Ref<ImageSource> m_source; |
211 | |
212 | size_t m_currentFrame { 0 }; // The index of the current frame of animation. |
213 | SubsamplingLevel m_currentSubsamplingLevel { SubsamplingLevel::Default }; |
214 | DecodingStatus m_currentFrameDecodingStatus { DecodingStatus::Invalid }; |
215 | std::unique_ptr<Timer> m_frameTimer; |
216 | RepetitionCount m_repetitionsComplete { RepetitionCountNone }; // How many repetitions we've finished. |
217 | MonotonicTime m_desiredFrameStartTime; // The system time at which we hope to see the next call to startAnimation(). |
218 | |
219 | std::unique_ptr<Vector<Function<void()>, 1>> m_decodingCallbacks; |
220 | |
221 | bool m_animationFinished { false }; |
222 | |
223 | // The default value of m_allowSubsampling should be the same as defaultImageSubsamplingEnabled in Settings.cpp |
224 | #if PLATFORM(IOS_FAMILY) |
225 | bool m_allowSubsampling { true }; |
226 | #else |
227 | bool m_allowSubsampling { false }; |
228 | #endif |
229 | bool m_allowAnimatedImageAsyncDecoding { false }; |
230 | bool m_showDebugBackground { false }; |
231 | |
232 | bool m_clearDecoderAfterAsyncFrameRequestForTesting { false }; |
233 | bool m_largeImageAsyncDecodingEnabledForTesting { false }; |
234 | |
235 | #if !LOG_DISABLED |
236 | size_t m_lateFrameCount { 0 }; |
237 | size_t m_earlyFrameCount { 0 }; |
238 | size_t m_cachedFrameCount { 0 }; |
239 | #endif |
240 | |
241 | unsigned m_decodeCountForTesting { 0 }; |
242 | |
243 | #if USE(APPKIT) |
244 | mutable RetainPtr<NSImage> m_nsImage; // A cached NSImage of all the frames. Only built lazily if someone actually queries for one. |
245 | #endif |
246 | #if USE(CG) |
247 | mutable RetainPtr<CFDataRef> m_tiffRep; // Cached TIFF rep for all the frames. Only built lazily if someone queries for one. |
248 | #endif |
249 | RefPtr<Image> m_cachedImage; |
250 | }; |
251 | |
252 | } // namespace WebCore |
253 | |
254 | SPECIALIZE_TYPE_TRAITS_IMAGE(BitmapImage) |
255 | |