1 | /* |
2 | * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) |
3 | * Copyright (C) 2004, 2005, 2006, 2013 Apple 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 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 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 | #pragma once |
28 | |
29 | #include "Color.h" |
30 | #include "DecodingOptions.h" |
31 | #include "FloatRect.h" |
32 | #include "FloatSize.h" |
33 | #include "GraphicsTypes.h" |
34 | #include "ImageOrientation.h" |
35 | #include "ImageTypes.h" |
36 | #include "NativeImage.h" |
37 | #include "Timer.h" |
38 | #include <wtf/Optional.h> |
39 | #include <wtf/RefCounted.h> |
40 | #include <wtf/RefPtr.h> |
41 | #include <wtf/RetainPtr.h> |
42 | #include <wtf/TypeCasts.h> |
43 | #include <wtf/text/WTFString.h> |
44 | |
45 | #if USE(APPKIT) |
46 | OBJC_CLASS NSImage; |
47 | #endif |
48 | |
49 | #if USE(CG) |
50 | struct CGContext; |
51 | #endif |
52 | |
53 | #if PLATFORM(WIN) |
54 | typedef struct tagSIZE SIZE; |
55 | typedef SIZE* LPSIZE; |
56 | typedef struct HBITMAP__ *HBITMAP; |
57 | #endif |
58 | |
59 | #if PLATFORM(GTK) |
60 | typedef struct _GdkPixbuf GdkPixbuf; |
61 | #endif |
62 | |
63 | namespace WebCore { |
64 | |
65 | class AffineTransform; |
66 | class FloatPoint; |
67 | class FloatSize; |
68 | class GraphicsContext; |
69 | class GraphicsContextImpl; |
70 | class SharedBuffer; |
71 | struct Length; |
72 | |
73 | // This class gets notified when an image creates or destroys decoded frames and when it advances animation frames. |
74 | class ImageObserver; |
75 | |
76 | class Image : public RefCounted<Image> { |
77 | friend class GraphicsContext; |
78 | friend class GraphicsContextImpl; |
79 | public: |
80 | virtual ~Image(); |
81 | |
82 | WEBCORE_EXPORT static Ref<Image> loadPlatformResource(const char* name); |
83 | WEBCORE_EXPORT static RefPtr<Image> create(ImageObserver&); |
84 | WEBCORE_EXPORT static bool supportsType(const String&); |
85 | static bool isPDFResource(const String& mimeType, const URL&); |
86 | static bool isPostScriptResource(const String& mimeType, const URL&); |
87 | |
88 | virtual bool isBitmapImage() const { return false; } |
89 | virtual bool isGeneratedImage() const { return false; } |
90 | virtual bool isCrossfadeGeneratedImage() const { return false; } |
91 | virtual bool isNamedImageGeneratedImage() const { return false; } |
92 | virtual bool isGradientImage() const { return false; } |
93 | virtual bool isSVGImage() const { return false; } |
94 | virtual bool isPDFDocumentImage() const { return false; } |
95 | virtual bool isCustomPaintImage() const { return false; } |
96 | |
97 | virtual bool currentFrameKnownToBeOpaque() const = 0; |
98 | virtual bool isAnimated() const { return false; } |
99 | |
100 | // Derived classes should override this if they can assure that |
101 | // the image contains only resources from its own security origin. |
102 | virtual bool hasSingleSecurityOrigin() const { return false; } |
103 | |
104 | WEBCORE_EXPORT static Image& nullImage(); |
105 | bool isNull() const { return size().isEmpty(); } |
106 | |
107 | virtual void setContainerSize(const FloatSize&) { } |
108 | virtual bool usesContainerSize() const { return false; } |
109 | virtual bool hasRelativeWidth() const { return false; } |
110 | virtual bool hasRelativeHeight() const { return false; } |
111 | virtual void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio); |
112 | |
113 | virtual FloatSize size() const = 0; |
114 | FloatRect rect() const { return FloatRect(FloatPoint(), size()); } |
115 | float width() const { return size().width(); } |
116 | float height() const { return size().height(); } |
117 | virtual Optional<IntPoint> hotSpot() const { return WTF::nullopt; } |
118 | |
119 | #if PLATFORM(IOS_FAMILY) |
120 | virtual FloatSize originalSize() const { return size(); } |
121 | #endif |
122 | |
123 | WEBCORE_EXPORT EncodedDataStatus setData(RefPtr<SharedBuffer>&& data, bool allDataReceived); |
124 | virtual EncodedDataStatus dataChanged(bool /*allDataReceived*/) { return EncodedDataStatus::Unknown; } |
125 | |
126 | virtual String uti() const { return String(); } // null string if unknown |
127 | virtual String filenameExtension() const { return String(); } // null string if unknown |
128 | |
129 | virtual void destroyDecodedData(bool destroyAll = true) = 0; |
130 | |
131 | SharedBuffer* data() { return m_encodedImageData.get(); } |
132 | const SharedBuffer* data() const { return m_encodedImageData.get(); } |
133 | |
134 | // Animation begins whenever someone draws the image, so startAnimation() is not normally called. |
135 | // It will automatically pause once all observers no longer want to render the image anywhere. |
136 | virtual void startAnimation() { } |
137 | void startAnimationAsynchronously(); |
138 | virtual void stopAnimation() {} |
139 | virtual void resetAnimation() {} |
140 | virtual bool isAnimating() const { return false; } |
141 | bool animationPending() const { return m_animationStartTimer && m_animationStartTimer->isActive(); } |
142 | |
143 | // Typically the CachedImage that owns us. |
144 | ImageObserver* imageObserver() const { return m_imageObserver; } |
145 | void setImageObserver(ImageObserver* observer) { m_imageObserver = observer; } |
146 | URL sourceURL() const; |
147 | String mimeType() const; |
148 | long long expectedContentLength() const; |
149 | |
150 | enum TileRule { StretchTile, RoundTile, SpaceTile, RepeatTile }; |
151 | |
152 | virtual NativeImagePtr nativeImage(const GraphicsContext* = nullptr) { return nullptr; } |
153 | virtual NativeImagePtr nativeImageOfSize(const IntSize&, const GraphicsContext* = nullptr) { return nullptr; } |
154 | virtual NativeImagePtr nativeImageForCurrentFrame(const GraphicsContext* = nullptr) { return nullptr; } |
155 | |
156 | // Accessors for native image formats. |
157 | |
158 | #if USE(APPKIT) |
159 | virtual NSImage *nsImage() { return nullptr; } |
160 | virtual RetainPtr<NSImage> snapshotNSImage() { return nullptr; } |
161 | #endif |
162 | |
163 | #if PLATFORM(COCOA) |
164 | virtual CFDataRef tiffRepresentation() { return nullptr; } |
165 | #endif |
166 | |
167 | #if PLATFORM(WIN) |
168 | virtual bool getHBITMAP(HBITMAP) { return false; } |
169 | virtual bool getHBITMAPOfSize(HBITMAP, const IntSize*) { return false; } |
170 | #endif |
171 | |
172 | #if PLATFORM(GTK) |
173 | virtual GdkPixbuf* getGdkPixbuf() { return nullptr; } |
174 | #endif |
175 | |
176 | virtual void drawPattern(GraphicsContext&, const FloatRect& destRect, const FloatRect& srcRect, const AffineTransform& patternTransform, |
177 | const FloatPoint& phase, const FloatSize& spacing, CompositeOperator, BlendMode = BlendMode::Normal); |
178 | |
179 | #if !ASSERT_DISABLED |
180 | virtual bool notSolidColor() { return true; } |
181 | #endif |
182 | |
183 | virtual void dump(WTF::TextStream&) const; |
184 | |
185 | protected: |
186 | Image(ImageObserver* = nullptr); |
187 | |
188 | static void fillWithSolidColor(GraphicsContext&, const FloatRect& dstRect, const Color&, CompositeOperator); |
189 | |
190 | #if PLATFORM(WIN) |
191 | virtual void drawFrameMatchingSourceSize(GraphicsContext&, const FloatRect& dstRect, const IntSize& srcSize, CompositeOperator) { } |
192 | #endif |
193 | virtual ImageDrawResult draw(GraphicsContext&, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator, BlendMode, DecodingMode, ImageOrientationDescription) = 0; |
194 | ImageDrawResult drawTiled(GraphicsContext&, const FloatRect& dstRect, const FloatPoint& srcPoint, const FloatSize& tileSize, const FloatSize& spacing, CompositeOperator, BlendMode, DecodingMode); |
195 | ImageDrawResult drawTiled(GraphicsContext&, const FloatRect& dstRect, const FloatRect& srcRect, const FloatSize& tileScaleFactor, TileRule hRule, TileRule vRule, CompositeOperator); |
196 | |
197 | // Supporting tiled drawing |
198 | virtual Color singlePixelSolidColor() const { return Color(); } |
199 | |
200 | private: |
201 | RefPtr<SharedBuffer> m_encodedImageData; |
202 | ImageObserver* m_imageObserver; |
203 | std::unique_ptr<Timer> m_animationStartTimer; |
204 | }; |
205 | |
206 | WTF::TextStream& operator<<(WTF::TextStream&, const Image&); |
207 | |
208 | } // namespace WebCore |
209 | |
210 | #define SPECIALIZE_TYPE_TRAITS_IMAGE(ToClassName) \ |
211 | SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToClassName) \ |
212 | static bool isType(const WebCore::Image& image) { return image.is##ToClassName(); } \ |
213 | SPECIALIZE_TYPE_TRAITS_END() |
214 | |
215 | |