1/*
2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
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#ifndef BitmapImage_h
29#define BitmapImage_h
30
31#include "Image.h"
32#include "Color.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)
42OBJC_CLASS NSImage;
43#endif
44
45#if PLATFORM(WIN)
46typedef struct HBITMAP__ *HBITMAP;
47#endif
48
49namespace WebCore {
50    struct FrameData;
51}
52
53namespace WTF {
54    template<> struct VectorTraits<WebCore::FrameData> : public SimpleClassVectorTraits {
55        static const bool canInitializeWithMemset = false; // Not all FrameData members initialize to 0.
56    };
57}
58
59namespace WebCore {
60
61template <typename T> class Timer;
62
63// ================================================
64// FrameData Class
65// ================================================
66
67struct FrameData {
68    WTF_MAKE_NONCOPYABLE(FrameData);
69public:
70    FrameData()
71        : m_frame(0)
72        , m_orientation(DefaultImageOrientation)
73#if PLATFORM(IOS)
74        , m_subsamplingScale(0)
75        , m_haveInfo(false)
76#endif
77        , m_duration(0)
78        , m_haveMetadata(false)
79        , m_isComplete(false)
80        , m_hasAlpha(true)
81        , m_frameBytes(0)
82    {
83    }
84
85    ~FrameData()
86    {
87        clear(true);
88    }
89
90    // Clear the cached image data on the frame, and (optionally) the metadata.
91    // Returns whether there was cached image data to clear.
92    bool clear(bool clearMetadata);
93
94    NativeImagePtr m_frame;
95    ImageOrientation m_orientation;
96#if PLATFORM(IOS)
97    float m_subsamplingScale;
98    bool m_haveInfo;
99#endif
100    float m_duration;
101    bool m_haveMetadata : 1;
102    bool m_isComplete : 1;
103    bool m_hasAlpha : 1;
104    unsigned m_frameBytes;
105};
106
107// =================================================
108// BitmapImage Class
109// =================================================
110
111// FIXME: We should better integrate the iOS and non-iOS code in this class. Unlike other ports, the
112// iOS port caches the metadata for a frame without decoding the image.
113
114class BitmapImage final : public Image {
115    friend class GeneratedImage;
116    friend class CrossfadeGeneratedImage;
117    friend class GradientImage;
118    friend class GraphicsContext;
119public:
120    static PassRefPtr<BitmapImage> create(PassNativeImagePtr nativeImage, ImageObserver* observer = 0)
121    {
122        return adoptRef(new BitmapImage(nativeImage, observer));
123    }
124    static PassRefPtr<BitmapImage> create(ImageObserver* observer = 0)
125    {
126        return adoptRef(new BitmapImage(observer));
127    }
128#if PLATFORM(WIN)
129    static PassRefPtr<BitmapImage> create(HBITMAP);
130#endif
131    virtual ~BitmapImage();
132
133    virtual bool isBitmapImage() const override { return true; }
134
135    virtual bool hasSingleSecurityOrigin() const override;
136
137    // FloatSize due to override.
138    virtual FloatSize size() const override;
139    IntSize sizeRespectingOrientation(ImageOrientationDescription = ImageOrientationDescription()) const;
140#if PLATFORM(IOS)
141    virtual FloatSize originalSize() const override;
142    IntSize originalSizeRespectingOrientation() const;
143#endif
144    IntSize currentFrameSize() const;
145    virtual bool getHotSpot(IntPoint&) const override;
146
147    unsigned decodedSize() const { return m_decodedSize; }
148
149    virtual bool dataChanged(bool allDataReceived) override;
150    virtual String filenameExtension() const override;
151
152    // It may look unusual that there is no start animation call as public API.  This is because
153    // we start and stop animating lazily.  Animation begins whenever someone draws the image.  It will
154    // automatically pause once all observers no longer want to render the image anywhere.
155    virtual void stopAnimation() override;
156    virtual void resetAnimation() override;
157
158    virtual void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform,
159        const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator, const FloatRect& destRect, BlendMode = BlendModeNormal) override;
160
161    // Accessors for native image formats.
162
163#if USE(APPKIT)
164    virtual NSImage* getNSImage() override;
165#endif
166
167#if PLATFORM(COCOA)
168    virtual CFDataRef getTIFFRepresentation() override;
169#endif
170
171#if USE(CG)
172    virtual CGImageRef getCGImageRef() override;
173    virtual CGImageRef getFirstCGImageRefOfSize(const IntSize&) override;
174    virtual RetainPtr<CFArrayRef> getCGImageArray() override;
175#endif
176
177#if PLATFORM(WIN)
178    virtual bool getHBITMAP(HBITMAP) override;
179    virtual bool getHBITMAPOfSize(HBITMAP, const IntSize*) override;
180#endif
181
182#if PLATFORM(GTK)
183    virtual GdkPixbuf* getGdkPixbuf() override;
184#endif
185
186#if PLATFORM(EFL)
187    virtual Evas_Object* getEvasObject(Evas*) override;
188#endif
189
190    virtual PassNativeImagePtr nativeImageForCurrentFrame() override;
191    virtual ImageOrientation orientationForCurrentFrame() override { return frameOrientationAtIndex(currentFrame()); }
192
193    virtual bool currentFrameKnownToBeOpaque() override;
194
195    bool canAnimate();
196
197private:
198    void updateSize(ImageOrientationDescription = ImageOrientationDescription()) const;
199
200protected:
201    enum RepetitionCountStatus {
202      Unknown,    // We haven't checked the source's repetition count.
203      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).
204      Certain     // The repetition count is known to be correct.
205    };
206
207    BitmapImage(PassNativeImagePtr, ImageObserver* = 0);
208    BitmapImage(ImageObserver* = 0);
209
210#if PLATFORM(WIN)
211    virtual void drawFrameMatchingSourceSize(GraphicsContext*, const FloatRect& dstRect, const IntSize& srcSize, ColorSpace styleColorSpace, CompositeOperator) override;
212#endif
213    virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace styleColorSpace, CompositeOperator, BlendMode, ImageOrientationDescription) override;
214
215#if USE(WINGDI)
216    virtual void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform,
217        const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator, const FloatRect& destRect);
218#endif
219
220    size_t currentFrame() const { return m_currentFrame; }
221#if PLATFORM(IOS)
222    PassNativeImagePtr frameAtIndex(size_t, float scaleHint);
223    PassNativeImagePtr copyUnscaledFrameAtIndex(size_t);
224#endif
225    size_t frameCount();
226    PassNativeImagePtr frameAtIndex(size_t);
227    bool frameIsCompleteAtIndex(size_t);
228    float frameDurationAtIndex(size_t);
229    bool frameHasAlphaAtIndex(size_t);
230    ImageOrientation frameOrientationAtIndex(size_t);
231
232    // Decodes and caches a frame. Never accessed except internally.
233#if PLATFORM(IOS)
234    void cacheFrame(size_t index, float scaleHint);
235
236    // Cache frame metadata without decoding image.
237    void cacheFrameInfo(size_t index);
238    // Called before accessing m_frames[index] for info without decoding. Returns false on index out of bounds.
239    bool ensureFrameInfoIsCached(size_t index);
240#else
241    void cacheFrame(size_t index);
242    // Called before accessing m_frames[index]. Returns false on index out of bounds.
243    bool ensureFrameIsCached(size_t index);
244#endif
245
246    // Called to invalidate cached data.  When |destroyAll| is true, we wipe out
247    // the entire frame buffer cache and tell the image source to destroy
248    // everything; this is used when e.g. we want to free some room in the image
249    // cache.  If |destroyAll| is false, we only delete frames up to the current
250    // one; this is used while animating large images to keep memory footprint
251    // low without redecoding the whole image on every frame.
252    virtual void destroyDecodedData(bool destroyAll = true) override;
253
254    // If the image is large enough, calls destroyDecodedData() and passes
255    // |destroyAll| along.
256    void destroyDecodedDataIfNecessary(bool destroyAll);
257
258    // Generally called by destroyDecodedData(), destroys whole-image metadata
259    // and notifies observers that the memory footprint has (hopefully)
260    // decreased by |frameBytesCleared|.
261    void destroyMetadataAndNotify(unsigned frameBytesCleared);
262
263    // Whether or not size is available yet.
264    bool isSizeAvailable();
265
266    // Called after asking the source for any information that may require
267    // decoding part of the image (e.g., the image size).  We need to report
268    // the partially decoded data to our observer so it has an accurate
269    // account of the BitmapImage's memory usage.
270    void didDecodeProperties() const;
271
272    // Animation.
273    int repetitionCount(bool imageKnownToBeComplete);  // |imageKnownToBeComplete| should be set if the caller knows the entire image has been decoded.
274    bool shouldAnimate();
275    virtual void startAnimation(CatchUpAnimation = CatchUp) override;
276    void advanceAnimation(Timer<BitmapImage>&);
277
278    // Function that does the real work of advancing the animation.  When
279    // skippingFrames is true, we're in the middle of a loop trying to skip over
280    // a bunch of animation frames, so we should not do things like decode each
281    // one or notify our observers.
282    // Returns whether the animation was advanced.
283    bool internalAdvanceAnimation(bool skippingFrames);
284
285    // Handle platform-specific data
286    void invalidatePlatformData();
287
288    // Checks to see if the image is a 1x1 solid color.  We optimize these images and just do a fill rect instead.
289    // This check should happen regardless whether m_checkedForSolidColor is already set, as the frame may have
290    // changed.
291    void checkForSolidColor();
292
293    virtual bool mayFillWithSolidColor() override;
294    virtual Color solidColor() const override;
295
296#if !ASSERT_DISABLED
297    virtual bool notSolidColor() override;
298#endif
299
300private:
301    ImageSource m_source;
302    mutable IntSize m_size; // The size to use for the overall image (will just be the size of the first image).
303    mutable IntSize m_sizeRespectingOrientation;
304    mutable unsigned m_imageOrientation : 4; // ImageOrientationEnum
305    mutable unsigned m_shouldRespectImageOrientation : 1; // RespectImageOrientationEnum
306
307#if PLATFORM(IOS)
308    mutable IntSize m_originalSize; // The size of the unsubsampled image.
309    mutable IntSize m_originalSizeRespectingOrientation;
310#endif
311    size_t m_currentFrame; // The index of the current frame of animation.
312    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.
313
314    std::unique_ptr<Timer<BitmapImage>> m_frameTimer;
315    int m_repetitionCount; // How many total animation loops we should do.  This will be cAnimationNone if this image type is incapable of animation.
316    RepetitionCountStatus m_repetitionCountStatus;
317    int m_repetitionsComplete;  // How many repetitions we've finished.
318    double m_desiredFrameStartTime;  // The system time at which we hope to see the next call to startAnimation().
319
320#if USE(APPKIT)
321    mutable RetainPtr<NSImage> m_nsImage; // A cached NSImage of frame 0. Only built lazily if someone actually queries for one.
322#endif
323#if USE(CG)
324    mutable RetainPtr<CFDataRef> m_tiffRep; // Cached TIFF rep for frame 0.  Only built lazily if someone queries for one.
325#endif
326
327    Color m_solidColor;  // If we're a 1x1 solid color, this is the color to use to fill.
328
329    unsigned m_decodedSize; // The current size of all decoded frames.
330    mutable unsigned m_decodedPropertiesSize; // The size of data decoded by the source to determine image properties (e.g. size, frame count, etc).
331    size_t m_frameCount;
332
333#if PLATFORM(IOS)
334    // FIXME: We should expose a setting to enable/disable progressive loading remove the PLATFORM(IOS)-guard.
335    double m_progressiveLoadChunkTime;
336    uint16_t m_progressiveLoadChunkCount;
337#endif
338
339    bool m_isSolidColor : 1; // Whether or not we are a 1x1 solid image.
340    bool m_checkedForSolidColor : 1; // Whether we've checked the frame for solid color.
341
342    bool m_animationFinished : 1; // Whether or not we've completed the entire animation.
343
344    bool m_allDataReceived : 1; // Whether or not we've received all our data.
345    mutable bool m_haveSize : 1; // Whether or not our |m_size| member variable has the final overall image size yet.
346    bool m_sizeAvailable : 1; // Whether or not we can obtain the size of the first image frame yet from ImageIO.
347    mutable bool m_hasUniformFrameSize : 1;
348    mutable bool m_haveFrameCount : 1;
349
350    RefPtr<Image> m_cachedImage;
351};
352
353IMAGE_TYPE_CASTS(BitmapImage)
354
355}
356
357#endif
358