Home | History | Annotate | Download | only in custom
      1 /*
      2  * Copyright (C) 2007-2009 Google 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 are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "config.h"
     33 #include "V8HTMLCanvasElement.h"
     34 
     35 #include "CanvasContextAttributes.h"
     36 #include "CanvasRenderingContext.h"
     37 #include "HTMLCanvasElement.h"
     38 #include "PlatformString.h"
     39 #include "WebGLContextAttributes.h"
     40 #include "V8Binding.h"
     41 #include "V8CanvasRenderingContext2D.h"
     42 #include "V8Node.h"
     43 #include "V8Proxy.h"
     44 #if ENABLE(WEBGL)
     45 #include "V8WebGLRenderingContext.h"
     46 #endif
     47 #include <wtf/MathExtras.h>
     48 
     49 namespace WebCore {
     50 
     51 v8::Handle<v8::Value> V8HTMLCanvasElement::getContextCallback(const v8::Arguments& args)
     52 {
     53     INC_STATS("DOM.HTMLCanvasElement.context");
     54     v8::Handle<v8::Object> holder = args.Holder();
     55     HTMLCanvasElement* imp = V8HTMLCanvasElement::toNative(holder);
     56     String contextId = toWebCoreString(args[0]);
     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 (args.Length() > 1 && args[1]->IsObject()) {
     63             v8::Handle<v8::Object> jsAttrs = args[1]->ToObject();
     64             v8::Handle<v8::String> alpha = v8::String::New("alpha");
     65             if (jsAttrs->Has(alpha))
     66                 webGLAttrs->setAlpha(jsAttrs->Get(alpha)->BooleanValue());
     67             v8::Handle<v8::String> depth = v8::String::New("depth");
     68             if (jsAttrs->Has(depth))
     69                 webGLAttrs->setDepth(jsAttrs->Get(depth)->BooleanValue());
     70             v8::Handle<v8::String> stencil = v8::String::New("stencil");
     71             if (jsAttrs->Has(stencil))
     72                 webGLAttrs->setStencil(jsAttrs->Get(stencil)->BooleanValue());
     73             v8::Handle<v8::String> antialias = v8::String::New("antialias");
     74             if (jsAttrs->Has(antialias))
     75                 webGLAttrs->setAntialias(jsAttrs->Get(antialias)->BooleanValue());
     76             v8::Handle<v8::String> premultipliedAlpha = v8::String::New("premultipliedAlpha");
     77             if (jsAttrs->Has(premultipliedAlpha))
     78                 webGLAttrs->setPremultipliedAlpha(jsAttrs->Get(premultipliedAlpha)->BooleanValue());
     79             v8::Handle<v8::String> preserveDrawingBuffer = v8::String::New("preserveDrawingBuffer");
     80             if (jsAttrs->Has(preserveDrawingBuffer))
     81                 webGLAttrs->setPreserveDrawingBuffer(jsAttrs->Get(preserveDrawingBuffer)->BooleanValue());
     82         }
     83     }
     84 #endif
     85     CanvasRenderingContext* result = imp->getContext(contextId, attrs.get());
     86     if (!result)
     87         return v8::Null();
     88     if (result->is2d())
     89         return toV8(static_cast<CanvasRenderingContext2D*>(result));
     90 #if ENABLE(WEBGL)
     91     else if (result->is3d())
     92         return toV8(static_cast<WebGLRenderingContext*>(result));
     93 #endif
     94     ASSERT_NOT_REACHED();
     95     return v8::Null();
     96 }
     97 
     98 v8::Handle<v8::Value> V8HTMLCanvasElement::toDataURLCallback(const v8::Arguments& args)
     99 {
    100     v8::Handle<v8::Object> holder = args.Holder();
    101     HTMLCanvasElement* canvas = V8HTMLCanvasElement::toNative(holder);
    102     ExceptionCode ec = 0;
    103 
    104     String type = toWebCoreString(args[0]);
    105     double quality;
    106     double* qualityPtr = 0;
    107     if (args.Length() > 1 && args[1]->IsNumber()) {
    108         quality = args[1]->NumberValue();
    109         qualityPtr = &quality;
    110     }
    111 
    112     String result = canvas->toDataURL(type, qualityPtr, ec);
    113     V8Proxy::setDOMException(ec);
    114     return v8StringOrUndefined(result);
    115 }
    116 
    117 } // namespace WebCore
    118