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