1 /*
2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "config.h"
28 #include "JSHTMLCanvasElement.h"
29
30 #include "CanvasContextAttributes.h"
31 #include "HTMLCanvasElement.h"
32 #include "JSCanvasRenderingContext2D.h"
33 #if ENABLE(WEBGL)
34 #include "JSWebGLRenderingContext.h"
35 #include "WebGLContextAttributes.h"
36 #endif
37 #include <wtf/GetPtr.h>
38
39 using namespace JSC;
40
41 namespace WebCore {
42
markChildren(MarkStack & markStack)43 void JSHTMLCanvasElement::markChildren(MarkStack& markStack)
44 {
45 Base::markChildren(markStack);
46
47 HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(impl());
48 JSGlobalData& globalData = *Heap::heap(this)->globalData();
49
50 markDOMObjectWrapper(markStack, globalData, canvas->renderingContext());
51 }
52
getContext(ExecState * exec)53 JSValue JSHTMLCanvasElement::getContext(ExecState* exec)
54 {
55 HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(impl());
56 const UString& contextId = exec->argument(0).toString(exec);
57 RefPtr<CanvasContextAttributes> attrs;
58 #if ENABLE(WEBGL)
59 if (contextId == "experimental-webgl" || contextId == "webkit-3d") {
60 attrs = WebGLContextAttributes::create();
61 WebGLContextAttributes* webGLAttrs = static_cast<WebGLContextAttributes*>(attrs.get());
62 if (exec->argumentCount() > 1 && exec->argument(1).isObject()) {
63 JSObject* jsAttrs = exec->argument(1).getObject();
64 Identifier alpha(exec, "alpha");
65 if (jsAttrs->hasProperty(exec, alpha))
66 webGLAttrs->setAlpha(jsAttrs->get(exec, alpha).toBoolean(exec));
67 Identifier depth(exec, "depth");
68 if (jsAttrs->hasProperty(exec, depth))
69 webGLAttrs->setDepth(jsAttrs->get(exec, depth).toBoolean(exec));
70 Identifier stencil(exec, "stencil");
71 if (jsAttrs->hasProperty(exec, stencil))
72 webGLAttrs->setStencil(jsAttrs->get(exec, stencil).toBoolean(exec));
73 Identifier antialias(exec, "antialias");
74 if (jsAttrs->hasProperty(exec, antialias))
75 webGLAttrs->setAntialias(jsAttrs->get(exec, antialias).toBoolean(exec));
76 Identifier premultipliedAlpha(exec, "premultipliedAlpha");
77 if (jsAttrs->hasProperty(exec, premultipliedAlpha))
78 webGLAttrs->setPremultipliedAlpha(jsAttrs->get(exec, premultipliedAlpha).toBoolean(exec));
79 Identifier preserveDrawingBuffer(exec, "preserveDrawingBuffer");
80 if (jsAttrs->hasProperty(exec, preserveDrawingBuffer))
81 webGLAttrs->setPreserveDrawingBuffer(jsAttrs->get(exec, preserveDrawingBuffer).toBoolean(exec));
82 }
83 }
84 #endif
85 CanvasRenderingContext* context = canvas->getContext(ustringToString(contextId), attrs.get());
86 if (!context)
87 return jsNull();
88 return toJS(exec, globalObject(), WTF::getPtr(context));
89 }
90
toDataURL(ExecState * exec)91 JSValue JSHTMLCanvasElement::toDataURL(ExecState* exec)
92 {
93 const String& type = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(0));
94 HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(impl());
95 ExceptionCode ec = 0;
96
97 JSC::JSValue result;
98 double quality;
99 double* qualityPtr = 0;
100 if (exec->argumentCount() > 1) {
101 JSValue v = exec->argument(1);
102 if (v.isNumber()) {
103 quality = v.toNumber(exec);
104 qualityPtr = &quality;
105 }
106 }
107
108 result = jsString(exec, canvas->toDataURL(type, qualityPtr, ec));
109 setDOMException(exec, ec);
110 return result;
111 }
112
113 } // namespace WebCore
114