Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLCanvasElement.h
1 /*
2  * Copyright (C) 2004, 2006, 2009, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
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 COMPUTER, 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 COMPUTER, 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 HTMLCanvasElement_h
29 #define HTMLCanvasElement_h
30
31 #include "core/dom/Document.h"
32 #include "core/html/HTMLElement.h"
33 #include "core/html/canvas/CanvasImageSource.h"
34 #include "platform/geometry/FloatRect.h"
35 #include "platform/geometry/IntSize.h"
36 #include "platform/graphics/Canvas2DLayerBridge.h"
37 #include "platform/graphics/GraphicsTypes.h"
38 #include "platform/graphics/ImageBufferClient.h"
39 #include "platform/heap/Handle.h"
40 #include "public/platform/WebThread.h"
41 #include "wtf/Forward.h"
42
43 #define CanvasDefaultInterpolationQuality InterpolationLow
44
45 namespace blink {
46
47 class AffineTransform;
48 class CanvasContextAttributes;
49 class CanvasRenderingContext;
50 class GraphicsContext;
51 class GraphicsContextStateSaver;
52 class HTMLCanvasElement;
53 class Image;
54 class ImageData;
55 class ImageBuffer;
56 class ImageBufferSurface;
57 class IntSize;
58 class RecordingImageBufferFallbackSurfaceFactory;
59
60 class CanvasObserver : public WillBeGarbageCollectedMixin {
61     DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(CanvasObserver);
62 public:
63     virtual void canvasChanged(HTMLCanvasElement*, const FloatRect& changedRect) = 0;
64     virtual void canvasResized(HTMLCanvasElement*) = 0;
65 #if !ENABLE(OILPAN)
66     virtual void canvasDestroyed(HTMLCanvasElement*) = 0;
67 #endif
68
69     virtual void trace(Visitor*) { }
70 };
71
72 class HTMLCanvasElement final : public HTMLElement, public DocumentVisibilityObserver, public CanvasImageSource, public ImageBufferClient, public blink::WebThread::TaskObserver {
73     DEFINE_WRAPPERTYPEINFO();
74     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(HTMLCanvasElement);
75 public:
76     DECLARE_NODE_FACTORY(HTMLCanvasElement);
77     virtual ~HTMLCanvasElement();
78
79     void addObserver(CanvasObserver*);
80     void removeObserver(CanvasObserver*);
81
82     // Attributes and functions exposed to script
83     int width() const { return size().width(); }
84     int height() const { return size().height(); }
85
86     const IntSize& size() const { return m_size; }
87
88     void setWidth(int);
89     void setHeight(int);
90     void setAccelerationDisabled(bool accelerationDisabled) { m_accelerationDisabled = accelerationDisabled; }
91     bool accelerationDisabled() const { return m_accelerationDisabled; }
92
93     void setSize(const IntSize& newSize)
94     {
95         if (newSize == size())
96             return;
97         m_ignoreReset = true;
98         setWidth(newSize.width());
99         setHeight(newSize.height());
100         m_ignoreReset = false;
101         reset();
102     }
103
104     CanvasRenderingContext* getContext(const String&, CanvasContextAttributes* attributes = 0);
105
106     static String toEncodingMimeType(const String& mimeType);
107     String toDataURL(const String& mimeType, const double* quality, ExceptionState&) const;
108     String toDataURL(const String& mimeType, ExceptionState& exceptionState) const { return toDataURL(mimeType, 0, exceptionState); }
109
110     // Used for rendering
111     void didDraw(const FloatRect&);
112     void notifyObserversCanvasChanged(const FloatRect&);
113
114     void paint(GraphicsContext*, const LayoutRect&);
115
116     GraphicsContext* drawingContext() const;
117     GraphicsContext* existingDrawingContext() const;
118
119     CanvasRenderingContext* renderingContext() const { return m_context.get(); }
120
121     void ensureUnacceleratedImageBuffer();
122     ImageBuffer* buffer() const;
123     Image* copiedImage() const;
124     void clearCopiedImage();
125     PassRefPtrWillBeRawPtr<ImageData> getImageData() const;
126     void makePresentationCopy();
127     void clearPresentationCopy();
128
129     SecurityOrigin* securityOrigin() const;
130     bool originClean() const { return m_originClean; }
131     void setOriginTainted() { m_originClean = false; }
132
133     AffineTransform baseTransform() const;
134
135     bool is3D() const;
136
137     bool hasImageBuffer() const { return m_imageBuffer; }
138     bool hasValidImageBuffer() const;
139     void discardImageBuffer();
140
141     bool shouldAccelerate(const IntSize&) const;
142
143     virtual const AtomicString imageSourceURL() const override;
144
145     virtual InsertionNotificationRequest insertedInto(ContainerNode*) override;
146
147     // DocumentVisibilityObserver implementation
148     virtual void didChangeVisibilityState(PageVisibilityState) override;
149
150     // CanvasImageSource implementation
151     virtual PassRefPtr<Image> getSourceImageForCanvas(SourceImageMode, SourceImageStatus*) const override;
152     virtual bool wouldTaintOrigin(SecurityOrigin*) const override;
153     virtual FloatSize sourceSize() const override;
154
155     // ImageBufferClient implementation
156     virtual void notifySurfaceInvalid() override;
157     virtual bool isDirty() override { return !m_dirtyRect.isEmpty(); }
158     virtual void didFinalizeFrame() override;
159
160     // Implementation of WebThread::TaskObserver methods
161     virtual void willProcessTask() override;
162     virtual void didProcessTask() override;
163
164     virtual void trace(Visitor*) override;
165
166 protected:
167     virtual void didMoveToNewDocument(Document& oldDocument) override;
168
169 private:
170     explicit HTMLCanvasElement(Document&);
171
172     virtual void parseAttribute(const QualifiedName&, const AtomicString&) override;
173     virtual RenderObject* createRenderer(RenderStyle*) override;
174     virtual bool areAuthorShadowsAllowed() const override { return false; }
175
176     void reset();
177
178     PassOwnPtr<RecordingImageBufferFallbackSurfaceFactory> createSurfaceFactory(const IntSize& deviceSize, int* msaaSampleCount) const;
179     PassOwnPtr<ImageBufferSurface> createImageBufferSurface(const IntSize& deviceSize, int* msaaSampleCount);
180     void createImageBuffer();
181     void createImageBufferInternal();
182     void clearImageBuffer();
183     bool shouldUseDisplayList(const IntSize& deviceSize);
184
185     void resetDirtyRect();
186
187     void setSurfaceSize(const IntSize&);
188
189     bool paintsIntoCanvasBuffer() const;
190
191     void updateExternallyAllocatedMemory() const;
192
193     String toDataURLInternal(const String& mimeType, const double* quality, bool isSaving = false) const;
194
195     WillBeHeapHashSet<RawPtrWillBeWeakMember<CanvasObserver>> m_observers;
196
197     IntSize m_size;
198
199     OwnPtrWillBeMember<CanvasRenderingContext> m_context;
200
201     bool m_ignoreReset;
202     bool m_accelerationDisabled;
203     FloatRect m_dirtyRect;
204
205     mutable intptr_t m_externallyAllocatedMemory;
206
207     bool m_originClean;
208
209     // It prevents HTMLCanvasElement::buffer() from continuously re-attempting to allocate an imageBuffer
210     // after the first attempt failed.
211     mutable bool m_didFailToCreateImageBuffer;
212     mutable bool m_didClearImageBuffer;
213     OwnPtr<ImageBuffer> m_imageBuffer;
214     mutable OwnPtr<GraphicsContextStateSaver> m_contextStateSaver;
215
216     mutable RefPtr<Image> m_presentedImage;
217     mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue).
218 };
219
220 } // namespace blink
221
222 #endif // HTMLCanvasElement_h