| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 * | 7 * |
| 8 */ | 8 */ |
| 9 #include <v8.h> | 9 #include <v8.h> |
| 10 | 10 |
| 11 using namespace v8; | 11 using namespace v8; |
| 12 | 12 |
| 13 #include "SkV8Example.h" | 13 #include "SkV8Example.h" |
| 14 #include "Global.h" | 14 #include "Global.h" |
| 15 #include "JsContext.h" |
| 16 #include "Path.h" |
| 15 | 17 |
| 16 #include "gl/GrGLUtil.h" | 18 #include "gl/GrGLUtil.h" |
| 17 #include "gl/GrGLDefines.h" | 19 #include "gl/GrGLDefines.h" |
| 18 #include "gl/GrGLInterface.h" | 20 #include "gl/GrGLInterface.h" |
| 19 #include "SkApplication.h" | 21 #include "SkApplication.h" |
| 20 #include "SkCommandLineFlags.h" | 22 #include "SkCommandLineFlags.h" |
| 21 #include "SkData.h" | 23 #include "SkData.h" |
| 22 #include "SkDraw.h" | 24 #include "SkDraw.h" |
| 23 #include "SkGpuDevice.h" | 25 #include "SkGpuDevice.h" |
| 24 #include "SkGraphics.h" | 26 #include "SkGraphics.h" |
| 25 #include "SkScalar.h" | 27 #include "SkScalar.h" |
| 26 | 28 |
| 27 | 29 |
| 28 DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n"); | 30 DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n"); |
| 29 | 31 |
| 30 void application_init() { | 32 void application_init() { |
| 31 SkGraphics::Init(); | 33 SkGraphics::Init(); |
| 32 SkEvent::Init(); | 34 SkEvent::Init(); |
| 33 } | 35 } |
| 34 | 36 |
| 35 void application_term() { | 37 void application_term() { |
| 36 SkEvent::Term(); | 38 SkEvent::Term(); |
| 37 SkGraphics::Term(); | 39 SkGraphics::Term(); |
| 38 } | 40 } |
| 39 | 41 |
| 40 // Extracts a C string from a V8 Utf8Value. | |
| 41 // TODO(jcgregrio) Currently dup'd in two files, fix. | |
| 42 static const char* to_cstring(const v8::String::Utf8Value& value) { | |
| 43 return *value ? *value : "<string conversion failed>"; | |
| 44 } | |
| 45 | 42 |
| 46 | 43 SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context) |
| 47 JsCanvas* 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 | |
| 53 void 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 | |
| 79 void 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 | |
| 90 void 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 | |
| 115 Persistent<ObjectTemplate> JsCanvas::fCanvasTemplate; | |
| 116 | |
| 117 Handle<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. | |
| 144 Handle<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 | |
| 174 void 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. | |
| 219 bool 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 | |
| 252 SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsCanvas* canvas) | |
| 253 : INHERITED(hwnd) | 44 : INHERITED(hwnd) |
| 254 , fJsCanvas(canvas) | 45 , fJsContext(context) |
| 255 { | 46 { |
| 256 this->setConfig(SkBitmap::kARGB_8888_Config); | 47 this->setConfig(SkBitmap::kARGB_8888_Config); |
| 257 this->setVisibleP(true); | 48 this->setVisibleP(true); |
| 258 this->setClipToBounds(false); | 49 this->setClipToBounds(false); |
| 259 } | 50 } |
| 260 | 51 |
| 261 void SkV8ExampleWindow::onDraw(SkCanvas* canvas) { | 52 void SkV8ExampleWindow::onDraw(SkCanvas* canvas) { |
| 262 | 53 |
| 263 canvas->save(); | 54 canvas->save(); |
| 264 canvas->drawColor(SK_ColorWHITE); | 55 canvas->drawColor(SK_ColorWHITE); |
| 265 | 56 |
| 266 // Now jump into JS and call the onDraw(canvas) method defined there. | 57 // Now jump into JS and call the onDraw(canvas) method defined there. |
| 267 fJsCanvas->onDraw(canvas); | 58 fJsContext->onDraw(canvas); |
| 268 | 59 |
| 269 canvas->restore(); | 60 canvas->restore(); |
| 270 | 61 |
| 271 INHERITED::onDraw(canvas); | 62 INHERITED::onDraw(canvas); |
| 272 } | 63 } |
| 273 | 64 |
| 274 | 65 |
| 275 #ifdef SK_BUILD_FOR_WIN | 66 #ifdef SK_BUILD_FOR_WIN |
| 276 void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) { | 67 void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) { |
| 277 RECT winRect; | 68 RECT winRect; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 301 | 92 |
| 302 SkAutoTUnref<SkData> data; | 93 SkAutoTUnref<SkData> data; |
| 303 if (FLAGS_infile.count()) { | 94 if (FLAGS_infile.count()) { |
| 304 data.reset(SkData::NewFromFileName(FLAGS_infile[0])); | 95 data.reset(SkData::NewFromFileName(FLAGS_infile[0])); |
| 305 script = static_cast<const char*>(data->data()); | 96 script = static_cast<const char*>(data->data()); |
| 306 } | 97 } |
| 307 if (NULL == script) { | 98 if (NULL == script) { |
| 308 printf("Could not load file: %s.\n", FLAGS_infile[0]); | 99 printf("Could not load file: %s.\n", FLAGS_infile[0]); |
| 309 exit(1); | 100 exit(1); |
| 310 } | 101 } |
| 102 Path::AddToGlobal(global); |
| 311 | 103 |
| 312 if (!global->parseScript(script)) { | 104 if (!global->parseScript(script)) { |
| 313 printf("Failed to parse file: %s.\n", FLAGS_infile[0]); | 105 printf("Failed to parse file: %s.\n", FLAGS_infile[0]); |
| 314 exit(1); | 106 exit(1); |
| 315 } | 107 } |
| 316 | 108 |
| 317 JsCanvas* jsCanvas = new JsCanvas(global); | 109 JsContext* jsContext = new JsContext(global); |
| 318 | 110 |
| 319 if (!jsCanvas->initialize()) { | 111 if (!jsContext->initialize()) { |
| 320 printf("Failed to initialize.\n"); | 112 printf("Failed to initialize.\n"); |
| 321 exit(1); | 113 exit(1); |
| 322 } | 114 } |
| 323 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsCanvas); | 115 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext); |
| 324 global->setWindow(win); | 116 global->setWindow(win); |
| 325 return win; | 117 return win; |
| 326 } | 118 } |
| OLD | NEW |