OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. |
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> | 3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> |
4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. | 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. |
5 * | 5 * |
6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
8 * are met: | 8 * are met: |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 13 matching lines...) Expand all Loading... |
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #include "config.h" | 28 #include "config.h" |
29 #include "core/html/HTMLCanvasElement.h" | 29 #include "core/html/HTMLCanvasElement.h" |
30 | 30 |
31 #include "bindings/core/v8/ExceptionMessages.h" | 31 #include "bindings/core/v8/ExceptionMessages.h" |
32 #include "bindings/core/v8/ExceptionState.h" | 32 #include "bindings/core/v8/ExceptionState.h" |
33 #include "bindings/core/v8/ScriptController.h" | 33 #include "bindings/core/v8/ScriptController.h" |
| 34 #include "bindings/core/v8/WrapCanvasContext.h" |
34 #include "core/HTMLNames.h" | 35 #include "core/HTMLNames.h" |
35 #include "core/dom/Document.h" | 36 #include "core/dom/Document.h" |
36 #include "core/dom/ExceptionCode.h" | 37 #include "core/dom/ExceptionCode.h" |
37 #include "core/frame/LocalFrame.h" | 38 #include "core/frame/LocalFrame.h" |
38 #include "core/frame/Settings.h" | 39 #include "core/frame/Settings.h" |
39 #include "core/html/ImageData.h" | 40 #include "core/html/ImageData.h" |
40 #include "core/html/canvas/Canvas2DContextAttributes.h" | 41 #include "core/html/canvas/Canvas2DContextAttributes.h" |
41 #include "core/html/canvas/CanvasRenderingContext2D.h" | 42 #include "core/html/canvas/CanvasRenderingContext2D.h" |
42 #include "core/html/canvas/WebGLContextAttributes.h" | 43 #include "core/html/canvas/WebGLContextAttributes.h" |
43 #include "core/html/canvas/WebGLContextEvent.h" | 44 #include "core/html/canvas/WebGLContextEvent.h" |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 void HTMLCanvasElement::setHeight(int value) | 164 void HTMLCanvasElement::setHeight(int value) |
164 { | 165 { |
165 setIntegralAttribute(heightAttr, value); | 166 setIntegralAttribute(heightAttr, value); |
166 } | 167 } |
167 | 168 |
168 void HTMLCanvasElement::setWidth(int value) | 169 void HTMLCanvasElement::setWidth(int value) |
169 { | 170 { |
170 setIntegralAttribute(widthAttr, value); | 171 setIntegralAttribute(widthAttr, value); |
171 } | 172 } |
172 | 173 |
173 CanvasRenderingContext* HTMLCanvasElement::getContext(const String& type, Canvas
ContextAttributes* attrs) | 174 ScriptValue HTMLCanvasElement::getContext(ScriptState* scriptState, const String
& type, const CanvasContextCreationAttributes& attributes) |
| 175 { |
| 176 CanvasRenderingContext2DOrWebGLRenderingContext result; |
| 177 getContext(type, attributes, result); |
| 178 if (result.isNull()) { |
| 179 return ScriptValue::createNull(scriptState); |
| 180 } |
| 181 |
| 182 if (result.isCanvasRenderingContext2D()) { |
| 183 return wrapCanvasContext(scriptState, this, result.getAsCanvasRenderingC
ontext2D()); |
| 184 } |
| 185 |
| 186 ASSERT(result.isWebGLRenderingContext()); |
| 187 return wrapCanvasContext(scriptState, this, result.getAsWebGLRenderingContex
t()); |
| 188 } |
| 189 |
| 190 void HTMLCanvasElement::getContext(const String& type, const CanvasContextCreati
onAttributes& attributes, CanvasRenderingContext2DOrWebGLRenderingContext& resul
t) |
174 { | 191 { |
175 // A Canvas can either be "2D" or "webgl" but never both. If you request a 2
D canvas and the existing | 192 // A Canvas can either be "2D" or "webgl" but never both. If you request a 2
D canvas and the existing |
176 // context is already 2D, just return that. If the existing context is WebGL
, then destroy it | 193 // context is already 2D, just return that. If the existing context is WebGL
, then destroy it |
177 // before creating a new 2D context. Vice versa when requesting a WebGL canv
as. Requesting a | 194 // before creating a new 2D context. Vice versa when requesting a WebGL canv
as. Requesting a |
178 // context with any other type string will destroy any existing context. | 195 // context with any other type string will destroy any existing context. |
179 enum ContextType { | 196 enum ContextType { |
180 // Do not change assigned numbers of existing items: add new features to
the end of the list. | 197 // Do not change assigned numbers of existing items: add new features to
the end of the list. |
181 Context2d = 0, | 198 Context2d = 0, |
182 ContextExperimentalWebgl = 2, | 199 ContextExperimentalWebgl = 2, |
183 ContextWebgl = 3, | 200 ContextWebgl = 3, |
184 ContextTypeCount, | 201 ContextTypeCount, |
185 }; | 202 }; |
186 | 203 |
187 // FIXME - The code depends on the context not going away once created, to p
revent JS from | 204 // FIXME - The code depends on the context not going away once created, to p
revent JS from |
188 // seeing a dangling pointer. So for now we will disallow the context from b
eing changed | 205 // seeing a dangling pointer. So for now we will disallow the context from b
eing changed |
189 // once it is created. | 206 // once it is created. |
190 if (type == "2d") { | 207 if (type == "2d") { |
191 if (m_context && !m_context->is2d()) | 208 if (m_context && !m_context->is2d()) |
192 return nullptr; | 209 return; |
193 if (!m_context) { | 210 if (!m_context) { |
194 blink::Platform::current()->histogramEnumeration("Canvas.ContextType
", Context2d, ContextTypeCount); | 211 blink::Platform::current()->histogramEnumeration("Canvas.ContextType
", Context2d, ContextTypeCount); |
195 | 212 |
196 m_context = CanvasRenderingContext2D::create(this, static_cast<Canva
s2DContextAttributes*>(attrs), document()); | 213 m_context = CanvasRenderingContext2D::create(this, attributes, docum
ent()); |
197 setNeedsCompositingUpdate(); | 214 setNeedsCompositingUpdate(); |
198 } | 215 } |
199 return m_context.get(); | 216 result.setCanvasRenderingContext2D(static_cast<CanvasRenderingContext2D*
>(m_context.get())); |
| 217 return; |
200 } | 218 } |
201 | 219 |
202 // Accept the the provisional "experimental-webgl" or official "webgl" conte
xt ID. | 220 // Accept the the provisional "experimental-webgl" or official "webgl" conte
xt ID. |
203 if (type == "webgl" || type == "experimental-webgl") { | 221 if (type == "webgl" || type == "experimental-webgl") { |
204 ContextType contextType = (type == "webgl") ? ContextWebgl : ContextExpe
rimentalWebgl; | 222 ContextType contextType = (type == "webgl") ? ContextWebgl : ContextExpe
rimentalWebgl; |
205 if (!m_context) { | 223 if (!m_context) { |
206 blink::Platform::current()->histogramEnumeration("Canvas.ContextType
", contextType, ContextTypeCount); | 224 blink::Platform::current()->histogramEnumeration("Canvas.ContextType
", contextType, ContextTypeCount); |
207 m_context = WebGLRenderingContext::create(this, static_cast<WebGLCon
textAttributes*>(attrs)); | 225 m_context = WebGLRenderingContext::create(this, attributes); |
208 setNeedsCompositingUpdate(); | 226 setNeedsCompositingUpdate(); |
209 updateExternallyAllocatedMemory(); | 227 updateExternallyAllocatedMemory(); |
210 } else if (!m_context->is3d()) { | 228 } else if (!m_context->is3d()) { |
211 dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontext
creationerror, false, true, "Canvas has an existing, non-WebGL context")); | 229 dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontext
creationerror, false, true, "Canvas has an existing, non-WebGL context")); |
212 return nullptr; | 230 return; |
213 } | 231 } |
214 return m_context.get(); | 232 result.setWebGLRenderingContext(static_cast<WebGLRenderingContext*>(m_co
ntext.get())); |
215 } | 233 } |
216 | 234 |
217 return nullptr; | 235 return; |
218 } | 236 } |
219 | 237 |
220 bool HTMLCanvasElement::isPaintable() const | 238 bool HTMLCanvasElement::isPaintable() const |
221 { | 239 { |
222 if (!m_context) | 240 if (!m_context) |
223 return canCreateImageBuffer(size()); | 241 return canCreateImageBuffer(size()); |
224 return buffer(); | 242 return buffer(); |
225 } | 243 } |
226 | 244 |
227 void HTMLCanvasElement::didDraw(const FloatRect& rect) | 245 void HTMLCanvasElement::didDraw(const FloatRect& rect) |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 { | 819 { |
802 return !originClean(); | 820 return !originClean(); |
803 } | 821 } |
804 | 822 |
805 FloatSize HTMLCanvasElement::sourceSize() const | 823 FloatSize HTMLCanvasElement::sourceSize() const |
806 { | 824 { |
807 return FloatSize(width(), height()); | 825 return FloatSize(width(), height()); |
808 } | 826 } |
809 | 827 |
810 } | 828 } |
OLD | NEW |