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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #ifndef HTMLCanvasElement_h
29 #define HTMLCanvasElement_h
31 #include "bindings/core/v8/ScriptValue.h"
32 #include "bindings/core/v8/UnionTypesCore.h"
33 #include "core/CoreExport.h"
34 #include "core/dom/Document.h"
35 #include "core/dom/DocumentVisibilityObserver.h"
36 #include "core/fileapi/FileCallback.h"
37 #include "core/html/HTMLElement.h"
38 #include "core/html/canvas/CanvasImageSource.h"
39 #include "platform/geometry/FloatRect.h"
40 #include "platform/geometry/IntSize.h"
41 #include "platform/graphics/GraphicsTypes.h"
42 #include "platform/graphics/GraphicsTypes3D.h"
43 #include "platform/graphics/ImageBufferClient.h"
44 #include "platform/heap/Handle.h"
46 #define CanvasDefaultInterpolationQuality InterpolationLow
50 class AffineTransform
;
51 class CanvasContextCreationAttributes
;
52 class CanvasRenderingContext
;
53 class CanvasRenderingContextFactory
;
54 class GraphicsContext
;
55 class HTMLCanvasElement
;
58 class ImageBufferSurface
;
62 class CORE_EXPORT CanvasObserver
: public WillBeGarbageCollectedMixin
{
63 DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(CanvasObserver
);
65 virtual void canvasChanged(HTMLCanvasElement
*, const FloatRect
& changedRect
) = 0;
66 virtual void canvasResized(HTMLCanvasElement
*) = 0;
68 virtual void canvasDestroyed(HTMLCanvasElement
*) = 0;
71 DEFINE_INLINE_VIRTUAL_TRACE() { }
74 class CORE_EXPORT HTMLCanvasElement final
: public HTMLElement
, public DocumentVisibilityObserver
, public CanvasImageSource
, public ImageBufferClient
{
75 DEFINE_WRAPPERTYPEINFO();
76 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(HTMLCanvasElement
);
78 DECLARE_NODE_FACTORY(HTMLCanvasElement
);
79 ~HTMLCanvasElement() override
;
81 void addObserver(CanvasObserver
*);
82 void removeObserver(CanvasObserver
*);
84 // Attributes and functions exposed to script
85 int width() const { return size().width(); }
86 int height() const { return size().height(); }
88 const IntSize
& size() const { return m_size
; }
92 void setAccelerationDisabled(bool accelerationDisabled
) { m_accelerationDisabled
= accelerationDisabled
; }
93 bool accelerationDisabled() const { return m_accelerationDisabled
; }
95 void setSize(const IntSize
& newSize
)
97 if (newSize
== size())
100 setWidth(newSize
.width());
101 setHeight(newSize
.height());
102 m_ignoreReset
= false;
106 // Called by HTMLCanvasElement's V8 bindings.
107 ScriptValue
getContext(ScriptState
*, const String
&, const CanvasContextCreationAttributes
&);
108 // Called by Document::getCSSCanvasContext as well as above getContext().
109 CanvasRenderingContext
* getCanvasRenderingContext(const String
&, const CanvasContextCreationAttributes
&);
111 bool isPaintable() const;
113 static String
toEncodingMimeType(const String
& mimeType
);
114 String
toDataURL(const String
& mimeType
, const ScriptValue
& qualityArgument
, ExceptionState
&) const;
115 String
toDataURL(const String
& mimeType
, ExceptionState
& exceptionState
) const { return toDataURL(mimeType
, ScriptValue(), exceptionState
); }
117 void toBlob(FileCallback
*, const String
& mimeType
, const ScriptValue
& qualityArgument
, ExceptionState
&) const;
118 void toBlob(FileCallback
* callback
, const String
& mimeType
, ExceptionState
& exceptionState
) { return toBlob(callback
, mimeType
, ScriptValue(), exceptionState
); }
120 // Used for rendering
121 void didDraw(const FloatRect
&);
122 void notifyObserversCanvasChanged(const FloatRect
&);
124 void paint(GraphicsContext
*, const LayoutRect
&);
126 SkCanvas
* drawingCanvas() const;
127 void disableDeferral() const;
128 SkCanvas
* existingDrawingCanvas() const;
130 void setRenderingContext(PassOwnPtrWillBeRawPtr
<CanvasRenderingContext
>);
131 CanvasRenderingContext
* renderingContext() const { return m_context
.get(); }
133 void ensureUnacceleratedImageBuffer();
134 ImageBuffer
* buffer() const;
135 PassRefPtr
<Image
> copiedImage(SourceDrawingBuffer
) const;
136 void clearCopiedImage();
138 SecurityOrigin
* securityOrigin() const;
139 bool originClean() const;
140 void setOriginTainted() { m_originClean
= false; }
142 AffineTransform
baseTransform() const;
145 bool isAnimated2D() const;
147 bool hasImageBuffer() const { return m_imageBuffer
; }
148 void discardImageBuffer();
150 bool shouldAccelerate(const IntSize
&) const;
152 bool shouldBeDirectComposited() const;
154 const AtomicString
imageSourceURL() const override
;
156 InsertionNotificationRequest
insertedInto(ContainerNode
*) override
;
158 // DocumentVisibilityObserver implementation
159 void didChangeVisibilityState(PageVisibilityState
) override
;
161 // CanvasImageSource implementation
162 PassRefPtr
<Image
> getSourceImageForCanvas(SourceImageStatus
*) const override
;
163 bool wouldTaintOrigin(SecurityOrigin
*) const override
;
164 FloatSize
elementSize() const override
;
165 bool isCanvasElement() const override
{ return true; }
166 bool isOpaque() const override
;
168 // ImageBufferClient implementation
169 void notifySurfaceInvalid() override
;
170 bool isDirty() override
{ return !m_dirtyRect
.isEmpty(); }
171 void didFinalizeFrame() override
;
172 void restoreCanvasMatrixClipStack(SkCanvas
*) const override
;
174 void doDeferredPaintInvalidation();
176 DECLARE_VIRTUAL_TRACE();
178 void createImageBufferUsingSurfaceForTesting(PassOwnPtr
<ImageBufferSurface
>);
180 static void registerRenderingContextFactory(PassOwnPtr
<CanvasRenderingContextFactory
>);
181 void updateExternallyAllocatedMemory() const;
183 void styleDidChange(const ComputedStyle
* oldStyle
, const ComputedStyle
& newStyle
);
186 void didMoveToNewDocument(Document
& oldDocument
) override
;
189 explicit HTMLCanvasElement(Document
&);
191 using ContextFactoryVector
= Vector
<OwnPtr
<CanvasRenderingContextFactory
>>;
192 static ContextFactoryVector
& renderingContextFactories();
193 static CanvasRenderingContextFactory
* getRenderingContextFactory(int);
195 void parseAttribute(const QualifiedName
&, const AtomicString
&) override
;
196 LayoutObject
* createLayoutObject(const ComputedStyle
&) override
;
197 void didRecalcStyle(StyleRecalcChange
) override
;
198 bool areAuthorShadowsAllowed() const override
{ return false; }
202 PassOwnPtr
<ImageBufferSurface
> createImageBufferSurface(const IntSize
& deviceSize
, int* msaaSampleCount
);
203 void createImageBuffer();
204 void createImageBufferInternal(PassOwnPtr
<ImageBufferSurface
> externalSurface
);
205 bool shouldUseDisplayList(const IntSize
& deviceSize
);
207 void setSurfaceSize(const IntSize
&);
209 bool paintsIntoCanvasBuffer() const;
211 ImageData
* toImageData(SourceDrawingBuffer
) const;
212 String
toDataURLInternal(const String
& mimeType
, const double* quality
, SourceDrawingBuffer
) const;
214 WillBeHeapHashSet
<RawPtrWillBeWeakMember
<CanvasObserver
>> m_observers
;
218 OwnPtrWillBeMember
<CanvasRenderingContext
> m_context
;
221 bool m_accelerationDisabled
;
222 FloatRect m_dirtyRect
;
224 mutable intptr_t m_externallyAllocatedMemory
;
228 // It prevents HTMLCanvasElement::buffer() from continuously re-attempting to allocate an imageBuffer
229 // after the first attempt failed.
230 mutable bool m_didFailToCreateImageBuffer
;
231 bool m_imageBufferIsClear
;
232 OwnPtr
<ImageBuffer
> m_imageBuffer
;
234 mutable RefPtr
<Image
> m_copiedImage
; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue).
239 #endif // HTMLCanvasElement_h