OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.git] / Source / WebCore / 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 "FloatRect.h"
32 #include "HTMLElement.h"
33 #include "IntSize.h"
34
35 #if PLATFORM(CHROMIUM) || PLATFORM(QT)
36 #define DefaultInterpolationQuality InterpolationMedium
37 #elif PLATFORM(CG)
38 #define DefaultInterpolationQuality InterpolationLow
39 #else
40 #define DefaultInterpolationQuality InterpolationDefault
41 #endif
42
43 namespace WebCore {
44
45 class CanvasContextAttributes;
46 class CanvasRenderingContext;
47 class GraphicsContext;
48 class HTMLCanvasElement;
49 class Image;
50 class ImageBuffer;
51 class IntSize;
52
53 class CanvasObserver {
54 public:
55     virtual ~CanvasObserver() { }
56
57     virtual void canvasChanged(HTMLCanvasElement*, const FloatRect& changedRect) = 0;
58     virtual void canvasResized(HTMLCanvasElement*) = 0;
59     virtual void canvasDestroyed(HTMLCanvasElement*) = 0;
60 };
61
62 class HTMLCanvasElement : public HTMLElement {
63 public:
64     static PassRefPtr<HTMLCanvasElement> create(Document*);
65     static PassRefPtr<HTMLCanvasElement> create(const QualifiedName&, Document*);
66     virtual ~HTMLCanvasElement();
67
68     void addObserver(CanvasObserver*);
69     void removeObserver(CanvasObserver*);
70
71     // Attributes and functions exposed to script
72     int width() const { return size().width(); }
73     int height() const { return size().height(); }
74
75     const IntSize& size() const { return m_size; }
76
77     void setWidth(int);
78     void setHeight(int);
79
80     void setSize(const IntSize& newSize)
81     { 
82         if (newSize == size())
83             return;
84         m_ignoreReset = true; 
85         setWidth(newSize.width());
86         setHeight(newSize.height());
87         m_ignoreReset = false;
88         reset();
89     }
90
91     CanvasRenderingContext* getContext(const String&, CanvasContextAttributes* attributes = 0);
92
93     String toDataURL(const String& mimeType, const double* quality, ExceptionCode&);
94     String toDataURL(const String& mimeType, ExceptionCode& ec) { return toDataURL(mimeType, 0, ec); }
95
96     // Used for rendering
97     void didDraw(const FloatRect&);
98
99     void paint(GraphicsContext*, const IntRect&);
100
101     GraphicsContext* drawingContext() const;
102
103     CanvasRenderingContext* renderingContext() const { return m_context.get(); }
104
105     ImageBuffer* buffer() const;
106     Image* copiedImage() const;
107     void clearCopiedImage();
108
109     IntRect convertLogicalToDevice(const FloatRect&) const;
110     IntSize convertLogicalToDevice(const FloatSize&) const;
111     IntSize convertToValidDeviceSize(float width, float height) const;
112
113     const SecurityOrigin& securityOrigin() const;
114     void setOriginTainted() { m_originClean = false; }
115     bool originClean() const { return m_originClean; }
116
117     CSSStyleSelector* styleSelector();
118
119     AffineTransform baseTransform() const;
120
121 #if ENABLE(WEBGL)    
122     bool is3D() const;
123 #endif
124
125     void makeRenderingResultsAvailable();
126
127 #ifdef ANDROID_INSTRUMENT
128     void* operator new(size_t size) {
129         return HTMLElement::operator new(size);
130     }
131     void* operator new[](size_t size) {
132         return HTMLElement::operator new[](size);
133     }
134
135     void operator delete(void* p, size_t size) {
136         HTMLElement::operator delete(p, size);
137     }
138     void operator delete[](void* p, size_t size) {
139         HTMLElement::operator delete[](p, size);
140     }
141 #endif
142
143 private:
144     HTMLCanvasElement(const QualifiedName&, Document*);
145
146     virtual void parseMappedAttribute(Attribute*);
147     virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
148
149     virtual void attach();
150
151     virtual void recalcStyle(StyleChange);
152
153     void reset();
154
155     void createImageBuffer() const;
156
157     void setSurfaceSize(const IntSize&);
158     bool hasCreatedImageBuffer() const { return m_hasCreatedImageBuffer; }
159
160     HashSet<CanvasObserver*> m_observers;
161
162     IntSize m_size;
163
164     OwnPtr<CanvasRenderingContext> m_context;
165
166     bool m_rendererIsCanvas;
167
168     bool m_ignoreReset;
169     FloatRect m_dirtyRect;
170
171     float m_pageScaleFactor;
172     bool m_originClean;
173
174     // m_createdImageBuffer means we tried to malloc the buffer.  We didn't necessarily get it.
175     mutable bool m_hasCreatedImageBuffer;
176     mutable OwnPtr<ImageBuffer> m_imageBuffer;
177     
178     mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue).
179 };
180
181 } //namespace
182
183 #endif