2013-12-05 13:45:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <v8.h>
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
|
|
|
#include "SkV8Example.h"
|
2013-12-18 04:45:37 +00:00
|
|
|
#include "Global.h"
|
2013-12-05 13:45:19 +00:00
|
|
|
|
|
|
|
#include "gl/GrGLUtil.h"
|
|
|
|
#include "gl/GrGLDefines.h"
|
|
|
|
#include "gl/GrGLInterface.h"
|
|
|
|
#include "SkApplication.h"
|
2013-12-16 18:24:51 +00:00
|
|
|
#include "SkCommandLineFlags.h"
|
|
|
|
#include "SkData.h"
|
2013-12-05 13:45:19 +00:00
|
|
|
#include "SkDraw.h"
|
|
|
|
#include "SkGpuDevice.h"
|
|
|
|
#include "SkGraphics.h"
|
2013-12-11 17:34:58 +00:00
|
|
|
#include "SkScalar.h"
|
2013-12-05 13:45:19 +00:00
|
|
|
|
|
|
|
|
2013-12-16 18:24:51 +00:00
|
|
|
DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n");
|
|
|
|
|
2013-12-05 13:45:19 +00:00
|
|
|
void application_init() {
|
|
|
|
SkGraphics::Init();
|
|
|
|
SkEvent::Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void application_term() {
|
|
|
|
SkEvent::Term();
|
|
|
|
SkGraphics::Term();
|
|
|
|
}
|
|
|
|
|
2013-12-06 15:24:52 +00:00
|
|
|
// Extracts a C string from a V8 Utf8Value.
|
2013-12-18 04:45:37 +00:00
|
|
|
// TODO(jcgregrio) Currently dup'd in two files, fix.
|
|
|
|
static const char* to_cstring(const v8::String::Utf8Value& value) {
|
2013-12-10 14:13:03 +00:00
|
|
|
return *value ? *value : "<string conversion failed>";
|
2013-12-06 15:24:52 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 13:45:19 +00:00
|
|
|
|
2013-12-11 17:34:58 +00:00
|
|
|
JsCanvas* JsCanvas::Unwrap(Handle<Object> obj) {
|
2013-12-10 14:13:03 +00:00
|
|
|
Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
|
|
|
|
void* ptr = field->Value();
|
|
|
|
return static_cast<JsCanvas*>(ptr);
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:34:58 +00:00
|
|
|
void JsCanvas::FillRect(const v8::FunctionCallbackInfo<Value>& args) {
|
|
|
|
JsCanvas* jsCanvas = Unwrap(args.This());
|
|
|
|
SkCanvas* canvas = jsCanvas->fCanvas;
|
2013-12-10 14:13:03 +00:00
|
|
|
|
2013-12-11 17:34:58 +00:00
|
|
|
if (args.Length() != 4) {
|
|
|
|
args.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
args.GetIsolate(), "Error: 4 arguments required."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO(jcgregorio) Really figure out the conversion from JS numbers to
|
|
|
|
// SkScalars. Maybe test if int first? Not sure of the performance impact.
|
|
|
|
double x = args[0]->NumberValue();
|
|
|
|
double y = args[1]->NumberValue();
|
|
|
|
double w = args[2]->NumberValue();
|
|
|
|
double h = args[3]->NumberValue();
|
2013-12-10 14:13:03 +00:00
|
|
|
|
|
|
|
SkRect rect = {
|
2013-12-11 17:34:58 +00:00
|
|
|
SkDoubleToScalar(x),
|
|
|
|
SkDoubleToScalar(y),
|
|
|
|
SkDoubleToScalar(x) + SkDoubleToScalar(w),
|
|
|
|
SkDoubleToScalar(y) + SkDoubleToScalar(h)
|
2013-12-10 14:13:03 +00:00
|
|
|
};
|
2013-12-11 17:34:58 +00:00
|
|
|
canvas->drawRect(rect, jsCanvas->fFillStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsCanvas::GetFillStyle(Local<String> name,
|
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
|
|
|
JsCanvas* jsCanvas = Unwrap(info.This());
|
|
|
|
SkColor color = jsCanvas->fFillStyle.getColor();
|
|
|
|
char buf[8];
|
|
|
|
sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color),
|
|
|
|
SkColorGetB(color));
|
|
|
|
|
|
|
|
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsCanvas::SetFillStyle(Local<String> name, Local<Value> value,
|
|
|
|
const PropertyCallbackInfo<void>& info) {
|
|
|
|
JsCanvas* jsCanvas = Unwrap(info.This());
|
|
|
|
Local<String> s = value->ToString();
|
|
|
|
if (s->Length() != 7) {
|
|
|
|
info.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
info.GetIsolate(), "Invalid fill style format."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char buf[8];
|
|
|
|
s->WriteUtf8(buf, sizeof(buf));
|
|
|
|
|
|
|
|
if (buf[0] != '#') {
|
|
|
|
info.GetIsolate()->ThrowException(
|
|
|
|
v8::String::NewFromUtf8(
|
|
|
|
info.GetIsolate(), "Invalid fill style format."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long color = strtol(buf+1, NULL, 16);
|
|
|
|
jsCanvas->fFillStyle.setColor(SkColorSetA(SkColor(color), SK_AlphaOPAQUE));
|
2013-12-10 14:13:03 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 17:34:58 +00:00
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
Persistent<ObjectTemplate> JsCanvas::fCanvasTemplate;
|
|
|
|
|
|
|
|
Handle<ObjectTemplate> JsCanvas::makeCanvasTemplate() {
|
2013-12-18 04:45:37 +00:00
|
|
|
EscapableHandleScope handleScope(fGlobal->getIsolate());
|
2013-12-10 14:13:03 +00:00
|
|
|
|
|
|
|
Local<ObjectTemplate> result = ObjectTemplate::New();
|
|
|
|
|
|
|
|
// Add a field to store the pointer to a JsCanvas instance.
|
|
|
|
result->SetInternalFieldCount(1);
|
|
|
|
|
|
|
|
// Add accessors for each of the fields of the canvas object.
|
2013-12-11 17:34:58 +00:00
|
|
|
result->SetAccessor(
|
2013-12-18 04:45:37 +00:00
|
|
|
String::NewFromUtf8(
|
|
|
|
fGlobal->getIsolate(), "fillStyle", String::kInternalizedString),
|
2013-12-11 17:34:58 +00:00
|
|
|
GetFillStyle, SetFillStyle);
|
|
|
|
|
|
|
|
// Add methods.
|
2013-12-10 14:13:03 +00:00
|
|
|
result->Set(
|
|
|
|
String::NewFromUtf8(
|
2013-12-18 04:45:37 +00:00
|
|
|
fGlobal->getIsolate(), "fillRect",
|
|
|
|
String::kInternalizedString),
|
2013-12-11 17:34:58 +00:00
|
|
|
FunctionTemplate::New(FillRect));
|
2013-12-10 14:13:03 +00:00
|
|
|
|
|
|
|
// Return the result through the current handle scope.
|
|
|
|
return handleScope.Escape(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Wraps 'this' in a Javascript object.
|
|
|
|
Handle<Object> JsCanvas::wrap() {
|
|
|
|
// Handle scope for temporary handles.
|
2013-12-18 04:45:37 +00:00
|
|
|
EscapableHandleScope handleScope(fGlobal->getIsolate());
|
2013-12-10 14:13:03 +00:00
|
|
|
|
|
|
|
// Fetch the template for creating JavaScript JsCanvas wrappers.
|
|
|
|
// It only has to be created once, which we do on demand.
|
|
|
|
if (fCanvasTemplate.IsEmpty()) {
|
|
|
|
Handle<ObjectTemplate> raw_template = this->makeCanvasTemplate();
|
2013-12-18 04:45:37 +00:00
|
|
|
fCanvasTemplate.Reset(fGlobal->getIsolate(), raw_template);
|
2013-12-10 14:13:03 +00:00
|
|
|
}
|
|
|
|
Handle<ObjectTemplate> templ =
|
2013-12-18 04:45:37 +00:00
|
|
|
Local<ObjectTemplate>::New(fGlobal->getIsolate(), fCanvasTemplate);
|
2013-12-05 13:45:19 +00:00
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
// Create an empty JsCanvas wrapper.
|
|
|
|
Local<Object> result = templ->NewInstance();
|
2013-12-05 13:45:19 +00:00
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
// Wrap the raw C++ pointer in an External so it can be referenced
|
|
|
|
// from within JavaScript.
|
2013-12-18 04:45:37 +00:00
|
|
|
Handle<External> canvasPtr = External::New(fGlobal->getIsolate(), this);
|
2013-12-06 15:24:52 +00:00
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
// Store the canvas pointer in the JavaScript wrapper.
|
|
|
|
result->SetInternalField(0, canvasPtr);
|
2013-12-05 13:45:19 +00:00
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
// Return the result through the current handle scope. Since each
|
|
|
|
// of these handles will go away when the handle scope is deleted
|
|
|
|
// we need to call Close to let one, the result, escape into the
|
|
|
|
// outer handle scope.
|
|
|
|
return handleScope.Escape(result);
|
|
|
|
}
|
|
|
|
|
2013-12-18 04:45:37 +00:00
|
|
|
void JsCanvas::onDraw(SkCanvas* canvas) {
|
2013-12-10 14:13:03 +00:00
|
|
|
// Record canvas and window in this.
|
|
|
|
fCanvas = canvas;
|
|
|
|
|
|
|
|
// Create a handle scope to keep the temporary object references.
|
2013-12-18 04:45:37 +00:00
|
|
|
HandleScope handleScope(fGlobal->getIsolate());
|
2013-12-10 14:13:03 +00:00
|
|
|
|
|
|
|
// Create a local context from our global context.
|
2013-12-18 04:45:37 +00:00
|
|
|
Local<Context> context = fGlobal->getContext();
|
2013-12-10 14:13:03 +00:00
|
|
|
|
|
|
|
// Enter the context so all the remaining operations take place there.
|
|
|
|
Context::Scope contextScope(context);
|
|
|
|
|
|
|
|
// Wrap the C++ this pointer in a JavaScript wrapper.
|
|
|
|
Handle<Object> canvasObj = this->wrap();
|
|
|
|
|
|
|
|
// Set up an exception handler before calling the Process function.
|
|
|
|
TryCatch tryCatch;
|
|
|
|
|
|
|
|
// Invoke the process function, giving the global object as 'this'
|
|
|
|
// and one argument, this JsCanvas.
|
|
|
|
const int argc = 1;
|
|
|
|
Handle<Value> argv[argc] = { canvasObj };
|
|
|
|
Local<Function> onDraw =
|
2013-12-18 04:45:37 +00:00
|
|
|
Local<Function>::New(fGlobal->getIsolate(), fOnDraw);
|
2013-12-10 14:13:03 +00:00
|
|
|
Handle<Value> result = onDraw->Call(context->Global(), argc, argv);
|
|
|
|
|
|
|
|
// Handle any exceptions or output.
|
|
|
|
if (result.IsEmpty()) {
|
|
|
|
SkASSERT(tryCatch.HasCaught());
|
|
|
|
// Print errors that happened during execution.
|
2013-12-18 04:45:37 +00:00
|
|
|
fGlobal->reportException(&tryCatch);
|
2013-12-10 14:13:03 +00:00
|
|
|
} else {
|
|
|
|
SkASSERT(!tryCatch.HasCaught());
|
|
|
|
if (!result->IsUndefined()) {
|
|
|
|
// If all went well and the result wasn't undefined then print
|
|
|
|
// the returned value.
|
|
|
|
String::Utf8Value str(result);
|
2013-12-18 04:45:37 +00:00
|
|
|
const char* cstr = to_cstring(str);
|
2013-12-10 14:13:03 +00:00
|
|
|
printf("%s\n", cstr);
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 15:24:52 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 04:45:37 +00:00
|
|
|
// Fetch the onDraw function from the global context.
|
|
|
|
bool JsCanvas::initialize() {
|
2013-12-10 14:13:03 +00:00
|
|
|
|
|
|
|
// Create a stack-allocated handle scope.
|
2013-12-18 04:45:37 +00:00
|
|
|
HandleScope handleScope(fGlobal->getIsolate());
|
2013-12-10 14:13:03 +00:00
|
|
|
|
2013-12-18 04:45:37 +00:00
|
|
|
// Create a local context from our global context.
|
|
|
|
Local<Context> context = fGlobal->getContext();
|
2013-12-05 13:45:19 +00:00
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
// Enter the scope so all operations take place in the scope.
|
|
|
|
Context::Scope contextScope(context);
|
2013-12-05 13:45:19 +00:00
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
v8::TryCatch try_catch;
|
2013-12-05 13:45:19 +00:00
|
|
|
|
2013-12-18 04:45:37 +00:00
|
|
|
Handle<String> fn_name = String::NewFromUtf8(
|
|
|
|
fGlobal->getIsolate(), "onDraw");
|
2013-12-10 14:13:03 +00:00
|
|
|
Handle<Value> fn_val = context->Global()->Get(fn_name);
|
|
|
|
|
|
|
|
if (!fn_val->IsFunction()) {
|
2013-12-11 17:34:58 +00:00
|
|
|
printf("Not a function.\n");
|
2013-12-10 14:13:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-05 13:45:19 +00:00
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
// It is a function; cast it to a Function.
|
|
|
|
Handle<Function> fn_fun = Handle<Function>::Cast(fn_val);
|
2013-12-05 13:45:19 +00:00
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
// Store the function in a Persistent handle, since we also want that to
|
|
|
|
// remain after this call returns.
|
2013-12-18 04:45:37 +00:00
|
|
|
fOnDraw.Reset(fGlobal->getIsolate(), fn_fun);
|
2013-12-10 14:13:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-18 04:45:37 +00:00
|
|
|
|
|
|
|
SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsCanvas* canvas)
|
|
|
|
: INHERITED(hwnd)
|
|
|
|
, fJsCanvas(canvas)
|
|
|
|
{
|
|
|
|
this->setConfig(SkBitmap::kARGB_8888_Config);
|
|
|
|
this->setVisibleP(true);
|
|
|
|
this->setClipToBounds(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
// Now jump into JS and call the onDraw(canvas) method defined there.
|
|
|
|
fJsCanvas->onDraw(canvas);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
INHERITED::onDraw(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
|
|
|
|
RECT winRect;
|
|
|
|
winRect.top = rect.top();
|
|
|
|
winRect.bottom = rect.bottom();
|
|
|
|
winRect.right = rect.right();
|
|
|
|
winRect.left = rect.left();
|
|
|
|
InvalidateRect((HWND)this->getHWND(), &winRect, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
|
|
|
|
printf("Started\n");
|
|
|
|
|
2013-12-16 18:24:51 +00:00
|
|
|
SkCommandLineFlags::Parse(argc, argv);
|
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
// Get the default Isolate created at startup.
|
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-12-18 04:45:37 +00:00
|
|
|
Global* global = new Global(isolate);
|
2013-12-16 18:24:51 +00:00
|
|
|
|
2013-12-11 17:34:58 +00:00
|
|
|
const char* script =
|
2013-12-16 18:24:51 +00:00
|
|
|
"function onDraw(canvas) { \n"
|
|
|
|
" canvas.fillStyle = '#00FF00'; \n"
|
|
|
|
" canvas.fillRect(20, 20, 100, 100); \n"
|
|
|
|
" canvas.inval(); \n"
|
|
|
|
"} \n";
|
|
|
|
|
|
|
|
SkAutoTUnref<SkData> data;
|
|
|
|
if (FLAGS_infile.count()) {
|
|
|
|
data.reset(SkData::NewFromFileName(FLAGS_infile[0]));
|
|
|
|
script = static_cast<const char*>(data->data());
|
|
|
|
}
|
|
|
|
if (NULL == script) {
|
|
|
|
printf("Could not load file: %s.\n", FLAGS_infile[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-12-18 04:45:37 +00:00
|
|
|
|
|
|
|
if (!global->parseScript(script)) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsCanvas* jsCanvas = new JsCanvas(global);
|
|
|
|
|
|
|
|
if (!jsCanvas->initialize()) {
|
2013-12-10 14:13:03 +00:00
|
|
|
printf("Failed to initialize.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-12-18 04:45:37 +00:00
|
|
|
SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsCanvas);
|
|
|
|
global->setWindow(win);
|
|
|
|
return win;
|
2013-12-05 13:45:19 +00:00
|
|
|
}
|