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