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 USE(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 ImageData;
51 class ImageBuffer;
52 class IntSize;
53 
54 class CanvasObserver {
55 public:
~CanvasObserver()56     virtual ~CanvasObserver() { }
57 
58     virtual void canvasChanged(HTMLCanvasElement*, const FloatRect& changedRect) = 0;
59     virtual void canvasResized(HTMLCanvasElement*) = 0;
60     virtual void canvasDestroyed(HTMLCanvasElement*) = 0;
61 };
62 
63 class HTMLCanvasElement : public HTMLElement {
64 public:
65     static PassRefPtr<HTMLCanvasElement> create(Document*);
66     static PassRefPtr<HTMLCanvasElement> create(const QualifiedName&, Document*);
67     virtual ~HTMLCanvasElement();
68 
69     void addObserver(CanvasObserver*);
70     void removeObserver(CanvasObserver*);
71 
72     // Attributes and functions exposed to script
width()73     int width() const { return size().width(); }
height()74     int height() const { return size().height(); }
75 
size()76     const IntSize& size() const { return m_size; }
77 
78     void setWidth(int);
79     void setHeight(int);
80 
setSize(const IntSize & newSize)81     void setSize(const IntSize& newSize)
82     {
83         if (newSize == size())
84             return;
85         m_ignoreReset = true;
86         setWidth(newSize.width());
87         setHeight(newSize.height());
88         m_ignoreReset = false;
89         reset();
90     }
91 
92     CanvasRenderingContext* getContext(const String&, CanvasContextAttributes* attributes = 0);
93 
94     String toDataURL(const String& mimeType, const double* quality, ExceptionCode&);
toDataURL(const String & mimeType,ExceptionCode & ec)95     String toDataURL(const String& mimeType, ExceptionCode& ec) { return toDataURL(mimeType, 0, ec); }
96 
97     // Used for rendering
98     void didDraw(const FloatRect&);
99 
100     void paint(GraphicsContext*, const IntRect&);
101 
102     GraphicsContext* drawingContext() const;
103     GraphicsContext* existingDrawingContext() const;
104 
renderingContext()105     CanvasRenderingContext* renderingContext() const { return m_context.get(); }
106 
107     ImageBuffer* buffer() const;
108     Image* copiedImage() const;
109     void clearCopiedImage();
110     PassRefPtr<ImageData> getImageData();
111     void makePresentationCopy();
112     void clearPresentationCopy();
113 
114     IntRect convertLogicalToDevice(const FloatRect&) const;
115     IntSize convertLogicalToDevice(const FloatSize&) const;
116     IntSize convertToValidDeviceSize(float width, float height) const;
117 
118     const SecurityOrigin& securityOrigin() const;
setOriginTainted()119     void setOriginTainted() { m_originClean = false; }
originClean()120     bool originClean() const { return m_originClean; }
121 
122     CSSStyleSelector* styleSelector();
123 
124     AffineTransform baseTransform() const;
125 
126 #if ENABLE(WEBGL)
127     bool is3D() const;
128 #endif
129 
130     void makeRenderingResultsAvailable();
131 
132 private:
133     HTMLCanvasElement(const QualifiedName&, Document*);
134 
135     virtual void parseMappedAttribute(Attribute*);
136     virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
137 
138     void reset();
139 
140     void createImageBuffer() const;
141 
142     void setSurfaceSize(const IntSize&);
hasCreatedImageBuffer()143     bool hasCreatedImageBuffer() const { return m_hasCreatedImageBuffer; }
144 
145     HashSet<CanvasObserver*> m_observers;
146 
147     IntSize m_size;
148 
149     OwnPtr<CanvasRenderingContext> m_context;
150 
151     bool m_rendererIsCanvas;
152 
153     bool m_ignoreReset;
154     FloatRect m_dirtyRect;
155 
156     float m_pageScaleFactor;
157     bool m_originClean;
158 
159     // m_createdImageBuffer means we tried to malloc the buffer.  We didn't necessarily get it.
160     mutable bool m_hasCreatedImageBuffer;
161     mutable OwnPtr<ImageBuffer> m_imageBuffer;
162 
163     mutable RefPtr<Image> m_presentedImage;
164     mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue).
165 };
166 
167 } //namespace
168 
169 #endif
170