aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/SkV8Example/SkV8Example.h
blob: 878e1f0f1f8db13a1ce9a5d863711af648637576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright 2013 Google Inc.
 *
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 */

#ifndef SkV8Example_DEFINED
#define SkV8Example_DEFINED

#include <v8.h>

#include "SkWindow.h"

using namespace v8;

class SkCanvas;
class JsCanvas;

class SkV8ExampleWindow : public SkOSWindow {
public:
    SkV8ExampleWindow(void* hwnd, JsCanvas* canvas);

protected:
    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE;

#ifdef SK_BUILD_FOR_WIN
    virtual void onHandleInval(const SkIRect&) SK_OVERRIDE;
#endif

private:
    typedef SkOSWindow INHERITED;
    SkScalar fRotationAngle;
    JsCanvas* fJsCanvas;
};


// Provides the canvas implementation in JS, and the OnDraw() method in C++
// that's used to bridge from C++ to JS. Should be used in JS as:
//
//  function onDraw(canvas) {
//   canvas.drawRect();
//   canvas.inval();
//  }
//
class JsCanvas {
public:
    JsCanvas(Isolate* isolate)
            : fIsolate(isolate)
            , fCanvas(NULL)
            , fWindow(NULL)
    {}
    ~JsCanvas();

    // Parse the script.
    bool initialize(const char script[]);

    // Call this with the SkCanvas you want onDraw to draw on.
    void onDraw(SkCanvas* canvas, SkOSWindow* window);

private:
    // Implementation of the canvas.drawRect() JS function.
    static void drawRect(const v8::FunctionCallbackInfo<Value>& args);

    // Implementation of the canvas.inval() JS function.
    static void inval(const v8::FunctionCallbackInfo<Value>& args);

    // Get the pointer out of obj.
    static JsCanvas* unwrap(Handle<Object> obj);

    // Create a template for JS object associated with JsCanvas, called lazily
    // by Wrap() and the results are stored in fCanvasTemplate;
    Handle<ObjectTemplate> makeCanvasTemplate();

    // Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap.
    Handle<Object> wrap();

    Isolate* fIsolate;

    // Only valid when inside OnDraw().
    SkCanvas* fCanvas;

    // Only valid when inside OnDraw().
    SkOSWindow* fWindow;

    // The context that the script will be parsed into.
    Persistent<Context> fContext;

    // A handle to the onDraw function defined in the script.
    Persistent<Function> fOnDraw;

    // The template for what a canvas object looks like. The canvas object is
    // what's passed into the JS onDraw() function.
    static Persistent<ObjectTemplate> fCanvasTemplate;
};

#endif