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