SkV8Example.cpp revision 0fc0dea333f313a579558beb7ba498db0711780e
1/* 2 * Copyright 2013 Google Inc. 3 * 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 * 8 */ 9#include <v8.h> 10 11using namespace v8; 12 13#include "SkV8Example.h" 14#include "Global.h" 15 16#include "gl/GrGLUtil.h" 17#include "gl/GrGLDefines.h" 18#include "gl/GrGLInterface.h" 19#include "SkApplication.h" 20#include "SkCommandLineFlags.h" 21#include "SkData.h" 22#include "SkDraw.h" 23#include "SkGpuDevice.h" 24#include "SkGraphics.h" 25#include "SkScalar.h" 26 27 28DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n"); 29 30void application_init() { 31 SkGraphics::Init(); 32 SkEvent::Init(); 33} 34 35void application_term() { 36 SkEvent::Term(); 37 SkGraphics::Term(); 38} 39 40// Extracts a C string from a V8 Utf8Value. 41// TODO(jcgregrio) Currently dup'd in two files, fix. 42static const char* to_cstring(const v8::String::Utf8Value& value) { 43 return *value ? *value : "<string conversion failed>"; 44} 45 46 47JsCanvas* JsCanvas::Unwrap(Handle<Object> obj) { 48 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0)); 49 void* ptr = field->Value(); 50 return static_cast<JsCanvas*>(ptr); 51} 52 53void JsCanvas::FillRect(const v8::FunctionCallbackInfo<Value>& args) { 54 JsCanvas* jsCanvas = Unwrap(args.This()); 55 SkCanvas* canvas = jsCanvas->fCanvas; 56 57 if (args.Length() != 4) { 58 args.GetIsolate()->ThrowException( 59 v8::String::NewFromUtf8( 60 args.GetIsolate(), "Error: 4 arguments required.")); 61 return; 62 } 63 // TODO(jcgregorio) Really figure out the conversion from JS numbers to 64 // SkScalars. Maybe test if int first? Not sure of the performance impact. 65 double x = args[0]->NumberValue(); 66 double y = args[1]->NumberValue(); 67 double w = args[2]->NumberValue(); 68 double h = args[3]->NumberValue(); 69 70 SkRect rect = { 71 SkDoubleToScalar(x), 72 SkDoubleToScalar(y), 73 SkDoubleToScalar(x) + SkDoubleToScalar(w), 74 SkDoubleToScalar(y) + SkDoubleToScalar(h) 75 }; 76 canvas->drawRect(rect, jsCanvas->fFillStyle); 77} 78 79void JsCanvas::GetFillStyle(Local<String> name, 80 const PropertyCallbackInfo<Value>& info) { 81 JsCanvas* jsCanvas = Unwrap(info.This()); 82 SkColor color = jsCanvas->fFillStyle.getColor(); 83 char buf[8]; 84 sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color), 85 SkColorGetB(color)); 86 87 info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf)); 88} 89 90void JsCanvas::SetFillStyle(Local<String> name, Local<Value> value, 91 const PropertyCallbackInfo<void>& info) { 92 JsCanvas* jsCanvas = Unwrap(info.This()); 93 Local<String> s = value->ToString(); 94 if (s->Length() != 7) { 95 info.GetIsolate()->ThrowException( 96 v8::String::NewFromUtf8( 97 info.GetIsolate(), "Invalid fill style format.")); 98 return; 99 } 100 char buf[8]; 101 s->WriteUtf8(buf, sizeof(buf)); 102 103 if (buf[0] != '#') { 104 info.GetIsolate()->ThrowException( 105 v8::String::NewFromUtf8( 106 info.GetIsolate(), "Invalid fill style format.")); 107 return; 108 } 109 110 long color = strtol(buf+1, NULL, 16); 111 jsCanvas->fFillStyle.setColor(SkColorSetA(SkColor(color), SK_AlphaOPAQUE)); 112} 113 114 115Persistent<ObjectTemplate> JsCanvas::fCanvasTemplate; 116 117Handle<ObjectTemplate> JsCanvas::makeCanvasTemplate() { 118 EscapableHandleScope handleScope(fGlobal->getIsolate()); 119 120 Local<ObjectTemplate> result = ObjectTemplate::New(); 121 122 // Add a field to store the pointer to a JsCanvas instance. 123 result->SetInternalFieldCount(1); 124 125 // Add accessors for each of the fields of the canvas object. 126 result->SetAccessor( 127 String::NewFromUtf8( 128 fGlobal->getIsolate(), "fillStyle", String::kInternalizedString), 129 GetFillStyle, SetFillStyle); 130 131 // Add methods. 132 result->Set( 133 String::NewFromUtf8( 134 fGlobal->getIsolate(), "fillRect", 135 String::kInternalizedString), 136 FunctionTemplate::New(FillRect)); 137 138 // Return the result through the current handle scope. 139 return handleScope.Escape(result); 140} 141 142 143// Wraps 'this' in a Javascript object. 144Handle<Object> JsCanvas::wrap() { 145 // Handle scope for temporary handles. 146 EscapableHandleScope handleScope(fGlobal->getIsolate()); 147 148 // Fetch the template for creating JavaScript JsCanvas wrappers. 149 // It only has to be created once, which we do on demand. 150 if (fCanvasTemplate.IsEmpty()) { 151 Handle<ObjectTemplate> raw_template = this->makeCanvasTemplate(); 152 fCanvasTemplate.Reset(fGlobal->getIsolate(), raw_template); 153 } 154 Handle<ObjectTemplate> templ = 155 Local<ObjectTemplate>::New(fGlobal->getIsolate(), fCanvasTemplate); 156 157 // Create an empty JsCanvas wrapper. 158 Local<Object> result = templ->NewInstance(); 159 160 // Wrap the raw C++ pointer in an External so it can be referenced 161 // from within JavaScript. 162 Handle<External> canvasPtr = External::New(fGlobal->getIsolate(), this); 163 164 // Store the canvas pointer in the JavaScript wrapper. 165 result->SetInternalField(0, canvasPtr); 166 167 // Return the result through the current handle scope. Since each 168 // of these handles will go away when the handle scope is deleted 169 // we need to call Close to let one, the result, escape into the 170 // outer handle scope. 171 return handleScope.Escape(result); 172} 173 174void JsCanvas::onDraw(SkCanvas* canvas) { 175 // Record canvas and window in this. 176 fCanvas = canvas; 177 178 // Create a handle scope to keep the temporary object references. 179 HandleScope handleScope(fGlobal->getIsolate()); 180 181 // Create a local context from our global context. 182 Local<Context> context = fGlobal->getContext(); 183 184 // Enter the context so all the remaining operations take place there. 185 Context::Scope contextScope(context); 186 187 // Wrap the C++ this pointer in a JavaScript wrapper. 188 Handle<Object> canvasObj = this->wrap(); 189 190 // Set up an exception handler before calling the Process function. 191 TryCatch tryCatch; 192 193 // Invoke the process function, giving the global object as 'this' 194 // and one argument, this JsCanvas. 195 const int argc = 1; 196 Handle<Value> argv[argc] = { canvasObj }; 197 Local<Function> onDraw = 198 Local<Function>::New(fGlobal->getIsolate(), fOnDraw); 199 Handle<Value> result = onDraw->Call(context->Global(), argc, argv); 200 201 // Handle any exceptions or output. 202 if (result.IsEmpty()) { 203 SkASSERT(tryCatch.HasCaught()); 204 // Print errors that happened during execution. 205 fGlobal->reportException(&tryCatch); 206 } else { 207 SkASSERT(!tryCatch.HasCaught()); 208 if (!result->IsUndefined()) { 209 // If all went well and the result wasn't undefined then print 210 // the returned value. 211 String::Utf8Value str(result); 212 const char* cstr = to_cstring(str); 213 printf("%s\n", cstr); 214 } 215 } 216} 217 218// Fetch the onDraw function from the global context. 219bool JsCanvas::initialize() { 220 221 // Create a stack-allocated handle scope. 222 HandleScope handleScope(fGlobal->getIsolate()); 223 224 // Create a local context from our global context. 225 Local<Context> context = fGlobal->getContext(); 226 227 // Enter the scope so all operations take place in the scope. 228 Context::Scope contextScope(context); 229 230 v8::TryCatch try_catch; 231 232 Handle<String> fn_name = String::NewFromUtf8( 233 fGlobal->getIsolate(), "onDraw"); 234 Handle<Value> fn_val = context->Global()->Get(fn_name); 235 236 if (!fn_val->IsFunction()) { 237 printf("Not a function.\n"); 238 return false; 239 } 240 241 // It is a function; cast it to a Function. 242 Handle<Function> fn_fun = Handle<Function>::Cast(fn_val); 243 244 // Store the function in a Persistent handle, since we also want that to 245 // remain after this call returns. 246 fOnDraw.Reset(fGlobal->getIsolate(), fn_fun); 247 248 return true; 249} 250 251 252SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsCanvas* canvas) 253 : INHERITED(hwnd) 254 , fJsCanvas(canvas) 255{ 256 this->setConfig(SkBitmap::kARGB_8888_Config); 257 this->setVisibleP(true); 258 this->setClipToBounds(false); 259} 260 261void SkV8ExampleWindow::onDraw(SkCanvas* canvas) { 262 263 canvas->save(); 264 canvas->drawColor(SK_ColorWHITE); 265 266 // Now jump into JS and call the onDraw(canvas) method defined there. 267 fJsCanvas->onDraw(canvas); 268 269 canvas->restore(); 270 271 INHERITED::onDraw(canvas); 272} 273 274 275#ifdef SK_BUILD_FOR_WIN 276void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) { 277 RECT winRect; 278 winRect.top = rect.top(); 279 winRect.bottom = rect.bottom(); 280 winRect.right = rect.right(); 281 winRect.left = rect.left(); 282 InvalidateRect((HWND)this->getHWND(), &winRect, false); 283} 284#endif 285 286SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { 287 printf("Started\n"); 288 289 SkCommandLineFlags::Parse(argc, argv); 290 291 // Get the default Isolate created at startup. 292 Isolate* isolate = Isolate::GetCurrent(); 293 Global* global = new Global(isolate); 294 295 const char* script = 296"function onDraw(canvas) { \n" 297" canvas.fillStyle = '#00FF00'; \n" 298" canvas.fillRect(20, 20, 100, 100); \n" 299" canvas.inval(); \n" 300"} \n"; 301 302 SkAutoTUnref<SkData> data; 303 if (FLAGS_infile.count()) { 304 data.reset(SkData::NewFromFileName(FLAGS_infile[0])); 305 script = static_cast<const char*>(data->data()); 306 } 307 if (NULL == script) { 308 printf("Could not load file: %s.\n", FLAGS_infile[0]); 309 exit(1); 310 } 311 312 if (!global->parseScript(script)) { 313 exit(1); 314 } 315 316 JsCanvas* jsCanvas = new JsCanvas(global); 317 318 if (!jsCanvas->initialize()) { 319 printf("Failed to initialize.\n"); 320 exit(1); 321 } 322 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsCanvas); 323 global->setWindow(win); 324 return win; 325} 326