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 | 14 |
15 #include "gl/GrGLUtil.h" | 15 #include "gl/GrGLUtil.h" |
16 #include "gl/GrGLDefines.h" | 16 #include "gl/GrGLDefines.h" |
17 #include "gl/GrGLInterface.h" | 17 #include "gl/GrGLInterface.h" |
18 #include "SkApplication.h" | 18 #include "SkApplication.h" |
19 #include "SkDraw.h" | 19 #include "SkDraw.h" |
20 #include "SkGpuDevice.h" | 20 #include "SkGpuDevice.h" |
21 #include "SkGraphics.h" | 21 #include "SkGraphics.h" |
22 #include "SkScalar.h" | |
22 | 23 |
23 | 24 |
24 void application_init() { | 25 void application_init() { |
25 SkGraphics::Init(); | 26 SkGraphics::Init(); |
26 SkEvent::Init(); | 27 SkEvent::Init(); |
27 } | 28 } |
28 | 29 |
29 void application_term() { | 30 void application_term() { |
30 SkEvent::Term(); | 31 SkEvent::Term(); |
31 SkGraphics::Term(); | 32 SkGraphics::Term(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 const char* stack_trace_string = ToCString(stack_trace); | 73 const char* stack_trace_string = ToCString(stack_trace); |
73 fprintf(stderr, "%s\n", stack_trace_string); | 74 fprintf(stderr, "%s\n", stack_trace_string); |
74 } | 75 } |
75 } | 76 } |
76 } | 77 } |
77 | 78 |
78 SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsCanvas* canvas) | 79 SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsCanvas* canvas) |
79 : INHERITED(hwnd) | 80 : INHERITED(hwnd) |
80 , fJsCanvas(canvas) | 81 , fJsCanvas(canvas) |
81 { | 82 { |
82 fRotationAngle = SkIntToScalar(0); | |
83 this->setConfig(SkBitmap::kARGB_8888_Config); | 83 this->setConfig(SkBitmap::kARGB_8888_Config); |
84 this->setVisibleP(true); | 84 this->setVisibleP(true); |
85 this->setClipToBounds(false); | 85 this->setClipToBounds(false); |
86 } | 86 } |
87 | 87 |
88 JsCanvas* JsCanvas::unwrap(Handle<Object> obj) { | 88 JsCanvas* JsCanvas::unwrap(Handle<Object> obj) { |
89 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0)); | 89 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0)); |
90 void* ptr = field->Value(); | 90 void* ptr = field->Value(); |
91 return static_cast<JsCanvas*>(ptr); | 91 return static_cast<JsCanvas*>(ptr); |
92 } | 92 } |
93 | 93 |
94 void JsCanvas::inval(const v8::FunctionCallbackInfo<Value>& args) { | 94 void JsCanvas::inval(const v8::FunctionCallbackInfo<Value>& args) { |
95 unwrap(args.This())->fWindow->inval(NULL); | 95 unwrap(args.This())->fWindow->inval(NULL); |
96 } | 96 } |
97 | 97 |
98 void JsCanvas::drawRect(const v8::FunctionCallbackInfo<Value>& args) { | 98 void JsCanvas::fillRect(const v8::FunctionCallbackInfo<Value>& args) { |
99 SkCanvas* canvas = unwrap(args.This())->fCanvas; | 99 JsCanvas* jsCanvas = unwrap(args.This()); |
100 SkCanvas* canvas = jsCanvas->fCanvas; | |
100 | 101 |
101 canvas->drawColor(SK_ColorWHITE); | 102 if (args.Length() != 4) { |
103 args.GetIsolate()->ThrowException( | |
104 v8::String::NewFromUtf8( | |
105 args.GetIsolate(), "Error: 4 arguments required.")); | |
106 return; | |
107 } | |
108 // TODO(jcgregorio) Really figure out the conversion from JS numbers to | |
109 // SkScalars. Maybe test if int first? Not sure of the performance impact. | |
robertphillips
2013/12/11 16:45:46
We shouldn't worry about fixed point.
jcgregorio
2013/12/11 17:23:47
Done.
| |
110 // Also conversion could depend on SkScalar being float or fixed-point. | |
111 // If fixed-point, check for overflow. | |
112 double x = args[0]->NumberValue(); | |
113 double y = args[1]->NumberValue(); | |
114 double w = args[2]->NumberValue(); | |
115 double h = args[3]->NumberValue(); | |
102 | 116 |
103 // Draw a rectangle with red paint. | |
104 SkPaint paint; | |
105 paint.setColor(SK_ColorRED); | |
106 SkRect rect = { | 117 SkRect rect = { |
107 SkIntToScalar(10), SkIntToScalar(10), | 118 SkDoubleToScalar(x), |
108 SkIntToScalar(128), SkIntToScalar(128) | 119 SkDoubleToScalar(y), |
120 SkDoubleToScalar(x) + SkDoubleToScalar(w), | |
121 SkDoubleToScalar(y) + SkDoubleToScalar(h) | |
109 }; | 122 }; |
110 canvas->drawRect(rect, paint); | 123 canvas->drawRect(rect, jsCanvas->fFillStyle); |
111 } | 124 } |
112 | 125 |
126 void JsCanvas::getFillStyle(Local<String> name, | |
robertphillips
2013/12/11 16:45:46
line this up with preceding line?
jcgregorio
2013/12/11 17:23:47
Done.
| |
127 const PropertyCallbackInfo<Value>& info) { | |
128 JsCanvas* jsCanvas = unwrap(info.This()); | |
129 SkColor color = jsCanvas->fFillStyle.getColor(); | |
130 char buf[8]; | |
robertphillips
2013/12/11 16:45:46
_snprintf?
jcgregorio
2013/12/11 17:23:47
Not done per our conversation.
| |
131 sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color), | |
132 SkColorGetB(color)); | |
133 | |
134 info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf)); | |
135 } | |
136 | |
robertphillips
2013/12/11 16:45:46
rm space in "( L")?
jcgregorio
2013/12/11 17:23:47
Done.
| |
137 void JsCanvas::setFillStyle( Local<String> name, Local<Value> value, | |
robertphillips
2013/12/11 16:45:46
line this up with preceding line?
jcgregorio
2013/12/11 17:23:47
Done.
| |
138 const PropertyCallbackInfo<void>& info) { | |
139 JsCanvas* jsCanvas = unwrap(info.This()); | |
140 Local<String> s = value->ToString(); | |
141 if (s->Length() != 7) { | |
142 info.GetIsolate()->ThrowException( | |
143 v8::String::NewFromUtf8( | |
robertphillips
2013/12/11 16:45:46
"Invalid fill style format"?
jcgregorio
2013/12/11 17:23:47
Done.
| |
144 info.GetIsolate(), "Invalid style format.")); | |
145 return; | |
146 } | |
147 char buf[8]; | |
148 s->WriteUtf8(buf, sizeof(buf)); | |
robertphillips
2013/12/11 16:45:46
Test if buf[0] == '#'?
jcgregorio
2013/12/11 17:23:47
Done.
| |
149 long color = strtol(buf+1, NULL, 16); | |
150 jsCanvas->fFillStyle.setColor(SkColorSetA(SkColor(color), SK_AlphaOPAQUE)); | |
151 } | |
152 | |
153 | |
113 Persistent<ObjectTemplate> JsCanvas::fCanvasTemplate; | 154 Persistent<ObjectTemplate> JsCanvas::fCanvasTemplate; |
114 | 155 |
115 Handle<ObjectTemplate> JsCanvas::makeCanvasTemplate() { | 156 Handle<ObjectTemplate> JsCanvas::makeCanvasTemplate() { |
116 EscapableHandleScope handleScope(fIsolate); | 157 EscapableHandleScope handleScope(fIsolate); |
117 | 158 |
118 Local<ObjectTemplate> result = ObjectTemplate::New(); | 159 Local<ObjectTemplate> result = ObjectTemplate::New(); |
119 | 160 |
120 // Add a field to store the pointer to a JsCanvas instance. | 161 // Add a field to store the pointer to a JsCanvas instance. |
121 result->SetInternalFieldCount(1); | 162 result->SetInternalFieldCount(1); |
122 | 163 |
123 // Add accessors for each of the fields of the canvas object. | 164 // Add accessors for each of the fields of the canvas object. |
165 result->SetAccessor( | |
166 String::NewFromUtf8(fIsolate, "fillStyle", String::kInternalizedString), | |
167 getFillStyle, setFillStyle); | |
168 | |
169 // Add methods. | |
124 result->Set( | 170 result->Set( |
125 String::NewFromUtf8( | 171 String::NewFromUtf8( |
126 fIsolate, "drawRect", String::kInternalizedString), | 172 fIsolate, "fillRect", String::kInternalizedString), |
127 FunctionTemplate::New(drawRect)); | 173 FunctionTemplate::New(fillRect)); |
128 result->Set( | 174 result->Set( |
129 String::NewFromUtf8( | 175 String::NewFromUtf8( |
130 fIsolate, "inval", String::kInternalizedString), | 176 fIsolate, "inval", String::kInternalizedString), |
131 FunctionTemplate::New(inval)); | 177 FunctionTemplate::New(inval)); |
132 | 178 |
133 // Return the result through the current handle scope. | 179 // Return the result through the current handle scope. |
134 return handleScope.Escape(result); | 180 return handleScope.Escape(result); |
135 } | 181 } |
136 | 182 |
137 | 183 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 String::Utf8Value str(result); | 253 String::Utf8Value str(result); |
208 const char* cstr = ToCString(str); | 254 const char* cstr = ToCString(str); |
209 printf("%s\n", cstr); | 255 printf("%s\n", cstr); |
210 } | 256 } |
211 } | 257 } |
212 } | 258 } |
213 | 259 |
214 void SkV8ExampleWindow::onDraw(SkCanvas* canvas) { | 260 void SkV8ExampleWindow::onDraw(SkCanvas* canvas) { |
215 | 261 |
216 canvas->save(); | 262 canvas->save(); |
217 | 263 canvas->drawColor(SK_ColorWHITE); |
218 fRotationAngle += SkDoubleToScalar(0.2); | |
219 if (fRotationAngle > SkDoubleToScalar(360.0)) { | |
220 fRotationAngle -= SkDoubleToScalar(360.0); | |
221 } | |
222 | |
223 canvas->rotate(fRotationAngle); | |
224 | 264 |
225 // Now jump into JS and call the onDraw(canvas) method defined there. | 265 // Now jump into JS and call the onDraw(canvas) method defined there. |
226 fJsCanvas->onDraw(canvas, this); | 266 fJsCanvas->onDraw(canvas, this); |
227 | 267 |
228 canvas->restore(); | 268 canvas->restore(); |
229 | 269 |
230 INHERITED::onDraw(canvas); | 270 INHERITED::onDraw(canvas); |
231 } | 271 } |
232 | 272 |
233 | 273 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 const char* cstr = ToCString(str); | 343 const char* cstr = ToCString(str); |
304 printf("%s\n", cstr); | 344 printf("%s\n", cstr); |
305 return false; | 345 return false; |
306 } | 346 } |
307 } | 347 } |
308 | 348 |
309 Handle<String> fn_name = String::NewFromUtf8(fIsolate, "onDraw"); | 349 Handle<String> fn_name = String::NewFromUtf8(fIsolate, "onDraw"); |
310 Handle<Value> fn_val = context->Global()->Get(fn_name); | 350 Handle<Value> fn_val = context->Global()->Get(fn_name); |
311 | 351 |
312 if (!fn_val->IsFunction()) { | 352 if (!fn_val->IsFunction()) { |
353 printf("Not a function.\n"); | |
313 return false; | 354 return false; |
314 } | 355 } |
315 | 356 |
316 // It is a function; cast it to a Function. | 357 // It is a function; cast it to a Function. |
317 Handle<Function> fn_fun = Handle<Function>::Cast(fn_val); | 358 Handle<Function> fn_fun = Handle<Function>::Cast(fn_val); |
318 | 359 |
319 // Store the function in a Persistent handle, since we also want that to | 360 // Store the function in a Persistent handle, since we also want that to |
320 // remain after this call returns. | 361 // remain after this call returns. |
321 fOnDraw.Reset(fIsolate, fn_fun); | 362 fOnDraw.Reset(fIsolate, fn_fun); |
322 | 363 |
323 // Also make the context persistent. | 364 // Also make the context persistent. |
324 fContext.Reset(fIsolate, context); | 365 fContext.Reset(fIsolate, context); |
325 return true; | 366 return true; |
326 } | 367 } |
327 | 368 |
328 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { | 369 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { |
329 printf("Started\n"); | 370 printf("Started\n"); |
330 | 371 |
331 // Get the default Isolate created at startup. | 372 // Get the default Isolate created at startup. |
332 Isolate* isolate = Isolate::GetCurrent(); | 373 Isolate* isolate = Isolate::GetCurrent(); |
333 | 374 |
334 JsCanvas* jsCanvas = new JsCanvas(isolate); | 375 JsCanvas* jsCanvas = new JsCanvas(isolate); |
335 const char* script = "function onDraw(canvas){" | 376 const char* script = |
robertphillips
2013/12/11 16:45:46
move var right a bit to line up with }?
jcgregorio
2013/12/11 17:23:47
Reformatted.
Eventually would like to add a comma
| |
336 "canvas.drawRect();" | 377 "var onDraw = function(){ \n" |
337 "canvas.inval();" | 378 " var tick = 0; \n" |
338 "};"; | 379 " function f(canvas) { \n" |
380 " tick += 0.001; \n" | |
381 " canvas.fillStyle = '#00FF00'; \n" | |
382 " canvas.fillRect(20, 20, 100, Math.abs(Math.cos(tick)*100)); \n" | |
383 " canvas.inval(); \n" | |
384 " }; \n" | |
385 " return f; \n" | |
386 " }(); \n"; | |
339 if (!jsCanvas->initialize(script)) { | 387 if (!jsCanvas->initialize(script)) { |
340 printf("Failed to initialize.\n"); | 388 printf("Failed to initialize.\n"); |
341 exit(1); | 389 exit(1); |
342 } | 390 } |
343 | 391 |
344 return new SkV8ExampleWindow(hwnd, jsCanvas); | 392 return new SkV8ExampleWindow(hwnd, jsCanvas); |
345 } | 393 } |
OLD | NEW |