1/*
2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef Image_h
28#define Image_h
29
30#include "platform/PlatformExport.h"
31#include "platform/geometry/IntRect.h"
32#include "platform/graphics/Color.h"
33#include "platform/graphics/GraphicsTypes.h"
34#include "platform/graphics/ImageOrientation.h"
35#include "platform/graphics/skia/NativeImageSkia.h"
36#include "third_party/skia/include/core/SkXfermode.h"
37#include "wtf/Assertions.h"
38#include "wtf/PassRefPtr.h"
39#include "wtf/RefCounted.h"
40#include "wtf/RefPtr.h"
41#include "wtf/RetainPtr.h"
42#include "wtf/text/WTFString.h"
43
44namespace blink {
45
46class FloatPoint;
47class FloatRect;
48class FloatSize;
49class GraphicsContext;
50class Length;
51class SharedBuffer;
52
53// This class gets notified when an image creates or destroys decoded frames and when it advances animation frames.
54class ImageObserver;
55
56class PLATFORM_EXPORT Image : public RefCounted<Image> {
57    friend class GeneratedImage;
58    friend class CrossfadeGeneratedImage;
59    friend class GradientGeneratedImage;
60    friend class GraphicsContext;
61
62public:
63    virtual ~Image();
64
65    static PassRefPtr<Image> loadPlatformResource(const char* name);
66    static bool supportsType(const String&);
67
68    virtual bool isSVGImage() const { return false; }
69    virtual bool isBitmapImage() const { return false; }
70    virtual bool currentFrameKnownToBeOpaque() = 0;
71
72    // Derived classes should override this if they can assure that the current
73    // image frame contains only resources from its own security origin.
74    virtual bool currentFrameHasSingleSecurityOrigin() const { return false; }
75
76    static Image* nullImage();
77    bool isNull() const { return size().isEmpty(); }
78
79    virtual void setContainerSize(const IntSize&) { }
80    virtual bool usesContainerSize() const { return false; }
81    virtual bool hasRelativeWidth() const { return false; }
82    virtual bool hasRelativeHeight() const { return false; }
83    virtual void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio);
84
85    virtual IntSize size() const = 0;
86    IntRect rect() const { return IntRect(IntPoint(), size()); }
87    int width() const { return size().width(); }
88    int height() const { return size().height(); }
89    virtual bool getHotSpot(IntPoint&) const { return false; }
90
91    bool setData(PassRefPtr<SharedBuffer> data, bool allDataReceived);
92    virtual bool dataChanged(bool /*allDataReceived*/) { return false; }
93
94    virtual String filenameExtension() const { return String(); } // null string if unknown
95
96    virtual void destroyDecodedData(bool destroyAll) = 0;
97
98    SharedBuffer* data() { return m_encodedImageData.get(); }
99
100    // Animation begins whenever someone draws the image, so startAnimation() is not normally called.
101    // It will automatically pause once all observers no longer want to render the image anywhere.
102    enum CatchUpAnimation { DoNotCatchUp, CatchUp };
103    virtual void startAnimation(CatchUpAnimation = CatchUp) { }
104    virtual void stopAnimation() {}
105    virtual void resetAnimation() {}
106
107    // True if this image can potentially animate.
108    virtual bool maybeAnimated() { return false; }
109
110    // Typically the ImageResource that owns us.
111    ImageObserver* imageObserver() const { return m_imageObserver; }
112    void setImageObserver(ImageObserver* observer) { m_imageObserver = observer; }
113
114    enum TileRule { StretchTile, RoundTile, SpaceTile, RepeatTile };
115
116    virtual PassRefPtr<NativeImageSkia> nativeImageForCurrentFrame() { return nullptr; }
117
118    virtual PassRefPtr<Image> imageForDefaultFrame();
119
120    virtual void drawPattern(GraphicsContext*, const FloatRect&,
121        const FloatSize&, const FloatPoint& phase, CompositeOperator,
122        const FloatRect&, WebBlendMode = WebBlendModeNormal, const IntSize& repeatSpacing = IntSize());
123
124#if ENABLE(ASSERT)
125    virtual bool notSolidColor() { return true; }
126#endif
127
128protected:
129    Image(ImageObserver* = 0);
130
131    static void fillWithSolidColor(GraphicsContext*, const FloatRect& dstRect, const Color&, CompositeOperator);
132    static FloatRect adjustForNegativeSize(const FloatRect&); // A helper method for translating negative width and height values.
133
134    virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator, WebBlendMode) = 0;
135    virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator, WebBlendMode, RespectImageOrientationEnum);
136    void drawTiled(GraphicsContext*, const FloatRect& dstRect, const FloatPoint& srcPoint, const FloatSize& tileSize,
137        CompositeOperator, WebBlendMode, const IntSize& repeatSpacing);
138    void drawTiled(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, const FloatSize& tileScaleFactor, TileRule hRule, TileRule vRule, CompositeOperator);
139
140    // Supporting tiled drawing
141    virtual bool mayFillWithSolidColor() { return false; }
142    virtual Color solidColor() const { return Color(); }
143
144private:
145    RefPtr<SharedBuffer> m_encodedImageData;
146    ImageObserver* m_imageObserver;
147};
148
149#define DEFINE_IMAGE_TYPE_CASTS(typeName) \
150    DEFINE_TYPE_CASTS(typeName, Image, image, image->is##typeName(), image.is##typeName())
151
152} // namespace blink
153
154#endif
155