1c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org/*
2c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org * Copyright 2014 Google Inc.
3c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org *
4c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org *
5c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
6c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org * found in the LICENSE file.
7c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org *
8c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org */
9c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
10c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org#ifndef SkV8Example_JsContext_DEFINED
11c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org#define SkV8Example_JsContext_DEFINED
12c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
13c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org#include <v8.h>
14c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
15c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org#include "SkPaint.h"
1624e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.org#include "BaseContext.h"
17c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
18c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.orgusing namespace v8;
19c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
20c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.orgclass SkCanvas;
21c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.orgclass Global;
22c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
23c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org// Provides the canvas context implementation in JS, and the OnDraw() method in
24c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org// C++ that's used to bridge from C++ to JS. Should be used in JS as:
25c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org//
26c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org//  function onDraw(context) {
27c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org//    context.fillStyle="#FF0000";
28c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org//    context.fillRect(x, y, w, h);
29c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org//  }
3024e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.orgclass JsContext : public BaseContext {
31c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.orgpublic:
32c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    JsContext(Global* global)
3324e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.org            : INHERITED(global)
34c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org            , fCanvas(NULL)
35c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    {
36c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    }
3724e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.org    virtual ~JsContext() {}
38c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
39c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    // Parse the script.
40c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    bool initialize();
41c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
42c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    // Call this with the SkCanvas you want onDraw to draw on.
43c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    void onDraw(SkCanvas* canvas);
44c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
4524e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.org    virtual SkCanvas* getCanvas() { return fCanvas; };
4624e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.org
47c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.orgprivate:
48c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
49c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap.
50c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    Handle<Object> wrap();
51c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
52c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    // A handle to the onDraw function defined in the script.
53c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    Persistent<Function> fOnDraw;
54c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
55c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    // The template for what a canvas context object looks like. The canvas
56c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    // context object is what's passed into the JS onDraw() function.
57c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org    static Persistent<ObjectTemplate> gContextTemplate;
5824e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.org
5924e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.org    // Only valid when inside OnDraw().
6024e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.org    SkCanvas* fCanvas;
6124e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.org
6224e0496e8d58e779487307fbe0ae20d5fea4cfc0commit-bot@chromium.org    typedef BaseContext INHERITED;
63c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org};
64c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org
65c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org#endif
66