Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: runtime/embedders/openglui/common/vm_glue.cc

Issue 25081002: Remove openglui embedder code (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #include <math.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12
13 #include "embedders/openglui/common/extension.h"
14 #include "embedders/openglui/common/log.h"
15 #include "embedders/openglui/common/vm_glue.h"
16 #include "include/dart_api.h"
17
18 char* VMGlue::extension_script_ = NULL;
19 bool VMGlue::initialized_vm_ = false;
20
21 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise
22 // it is initialized to NULL.
23
24 VMGlue::VMGlue(ISized* surface,
25 const char* script_path,
26 const char* extension_script,
27 const char* main_script,
28 int setup_flag)
29 : surface_(surface),
30 isolate_(NULL),
31 initialized_script_(false),
32 x_(0.0),
33 y_(0.0),
34 z_(0.0),
35 accelerometer_changed_(false),
36 setup_flag_(setup_flag) {
37 LOGI("Creating VMGlue");
38 if (main_script == NULL) {
39 main_script = "main.dart";
40 }
41 if (extension_script == NULL) {
42 extension_script = "gl.dart";
43 }
44 size_t len = strlen(script_path) + strlen(main_script) + 2;
45 main_script_ = new char[len];
46 snprintf(main_script_, len, "%s/%s", script_path, main_script);
47 len = strlen(script_path) + strlen(extension_script) + 2;
48 extension_script_ = new char[len];
49 snprintf(extension_script_, len, "%s/%s", script_path, extension_script);
50 }
51
52 Dart_Handle VMGlue::CheckError(Dart_Handle handle) {
53 if (Dart_IsError(handle)) {
54 LOGE("Unexpected Error Handle: %s", Dart_GetError(handle));
55 Dart_PropagateError(handle);
56 }
57 return handle;
58 }
59
60 #define CHECK_RESULT(result) \
61 if (Dart_IsError(result)) { \
62 *error = strdup(Dart_GetError(result)); \
63 LOGE("%s", *error); \
64 Dart_ExitScope(); \
65 Dart_ShutdownIsolate(); \
66 return false; \
67 }
68
69 Dart_Handle VMGlue::LibraryTagHandler(Dart_LibraryTag tag,
70 Dart_Handle library,
71 Dart_Handle urlHandle) {
72 const char* url;
73 Dart_StringToCString(urlHandle, &url);
74 if (tag == kCanonicalizeUrl) {
75 return urlHandle;
76 }
77 // TODO(vsm): Split this up into separate libraries for 3D, 2D,
78 // Touch, Audio, etc. All builtin libraries should be handled here
79 // (or moved into a snapshot).
80 if (strcmp(url, "gl.dart") == 0) {
81 Dart_Handle source =
82 VMGlue::LoadSourceFromFile(extension_script_);
83 Dart_Handle library = CheckError(Dart_LoadLibrary(urlHandle, source));
84 CheckError(Dart_SetNativeResolver(library, ResolveName));
85 return library;
86 }
87 LOGE("UNIMPLEMENTED: load library %s\n", url);
88 return NULL;
89 }
90
91 // Returns true on success, false on failure.
92 Dart_Isolate VMGlue::CreateIsolateAndSetupHelper(const char* script_uri,
93 const char* main,
94 void* data,
95 char** error) {
96 LOGI("Creating isolate %s, %s", script_uri, main);
97 Dart_Isolate isolate =
98 Dart_CreateIsolate(script_uri, main, NULL, data, error);
99 if (isolate == NULL) {
100 LOGE("Couldn't create isolate: %s", *error);
101 return NULL;
102 }
103
104 LOGI("Entering scope");
105 Dart_EnterScope();
106
107 // Set up the library tag handler for this isolate.
108 LOGI("Setting up library tag handler");
109 Dart_Handle result = CheckError(Dart_SetLibraryTagHandler(LibraryTagHandler));
110 CHECK_RESULT(result);
111
112 Dart_ExitScope();
113 return isolate;
114 }
115
116 Dart_Isolate VMGlue::CreateIsolateAndSetup(const char* script_uri,
117 const char* main,
118 void* data, char** error) {
119 return CreateIsolateAndSetupHelper(script_uri,
120 main,
121 data,
122 error);
123 }
124
125 const char* VM_FLAGS[] = {
126 "--enable_type_checks", // TODO(gram): This should be an option!
127 // "--trace_isolates",
128 // "--trace_natives",
129 // "--trace_compiler",
130 };
131
132 static void* openFileCallback(const char* name, bool write) {
133 return fopen(name, write ? "w" : "r");
134 }
135
136 static void readFileCallback(const uint8_t** data, intptr_t* fileLength,
137 void* stream) {
138 if (!stream) {
139 *data = 0;
140 *fileLength = 0;
141 } else {
142 FILE* file = reinterpret_cast<FILE*>(stream);
143
144 // Get the file size.
145 fseek(file, 0, SEEK_END);
146 *fileLength = ftell(file);
147 rewind(file);
148
149 // Allocate data buffer.
150 *data = new uint8_t[*fileLength];
151 *fileLength = fread(const_cast<uint8_t*>(*data), 1, *fileLength, file);
152 }
153 }
154
155 static void writeFileCallback(const void* data, intptr_t length, void* file) {
156 fwrite(data, 1, length, reinterpret_cast<FILE*>(file));
157 }
158
159 static void closeFileCallback(void* file) {
160 fclose(reinterpret_cast<FILE*>(file));
161 }
162
163 int VMGlue::InitializeVM() {
164 // We need the next call to get Dart_Initialize not to bail early.
165 LOGI("Setting VM Options");
166 Dart_SetVMFlags(sizeof(VM_FLAGS) / sizeof(VM_FLAGS[0]), VM_FLAGS);
167
168 // Initialize the Dart VM, providing the callbacks to use for
169 // creating and shutting down isolates.
170 LOGI("Initializing Dart");
171 if (!Dart_Initialize(CreateIsolateAndSetup,
172 0,
173 0,
174 0,
175 openFileCallback,
176 readFileCallback,
177 writeFileCallback,
178 closeFileCallback)) {
179 LOGE("VM initialization failed\n");
180 return -1;
181 }
182 initialized_vm_ = true;
183
184 return 0;
185 }
186
187 Dart_Handle VMGlue::LoadSourceFromFile(const char* url) {
188 FILE* file = fopen(url, "r");
189 if (file == NULL) {
190 LOGE("Main script not found at: %s\n", url);
191 return NULL;
192 }
193
194 struct stat sb;
195 int fd = fileno(file);
196 fstat(fd, &sb);
197 int length = sb.st_size;
198 LOGI("Entry file %s is %d bytes.\n", url, length);
199
200 char* buffer = new char[length+1];
201 if (read(fd, buffer, length) < 0) {
202 LOGE("Could not read script %s.\n", url);
203 return NULL;
204 }
205 buffer[length] = 0;
206 fclose(file);
207
208 Dart_Handle contents = CheckError(Dart_NewStringFromCString(buffer));
209 delete[] buffer;
210 return contents;
211 }
212
213 int VMGlue::StartMainIsolate() {
214 if (!initialized_vm_) {
215 int rtn = InitializeVM();
216 if (rtn != 0) return rtn;
217 }
218
219 // Create an isolate and loads up the application script.
220 char* error = NULL;
221 if (!CreateIsolateAndSetup(main_script_, "main", NULL, &error)) {
222 LOGE("CreateIsolateAndSetup: %s\n", error);
223 free(error);
224 return -1;
225 }
226 LOGI("Created isolate");
227 isolate_ = Dart_CurrentIsolate();
228 Dart_EnterScope();
229
230 Dart_Handle url = CheckError(Dart_NewStringFromCString(main_script_));
231 Dart_Handle source = LoadSourceFromFile(main_script_);
232 CheckError(Dart_LoadScript(url, source, 0, 0));
233
234 Dart_ExitScope();
235 Dart_ExitIsolate();
236 return 0;
237 }
238
239 int VMGlue::CallSetup(bool force) {
240 // TODO(gram): See if we actually need this flag guard here, or if
241 // we can eliminate it along with the need for the force parameter.
242 if (!initialized_script_ || force) {
243 initialized_script_ = true;
244 LOGI("Invoking setup(null, %d,%d,%d)",
245 surface_->width(), surface_->height(), setup_flag_);
246 Dart_EnterIsolate(isolate_);
247 Dart_EnterScope();
248 Dart_Handle args[4];
249 args[0] = CheckError(Dart_Null());
250 args[1] = CheckError(Dart_NewInteger(surface_->width()));
251 args[2] = CheckError(Dart_NewInteger(surface_->height()));
252 args[3] = CheckError(Dart_NewInteger(setup_flag_));
253 int rtn = Invoke("setup", 4, args);
254
255 if (rtn == 0) {
256 // Plug in the print handler. It would be nice if we could do this
257 // before calling setup, but the call to GetField blows up if we
258 // haven't run anything yet.
259 Dart_Handle library = CheckError(Dart_LookupLibrary(
260 Dart_NewStringFromCString("gl.dart")));
261 Dart_Handle print = CheckError(
262 Dart_GetField(library, Dart_NewStringFromCString("_printClosure")));
263 Dart_Handle corelib = CheckError(Dart_LookupLibrary(
264 Dart_NewStringFromCString("dart:core")));
265 CheckError(Dart_SetField(corelib,
266 Dart_NewStringFromCString("_printClosure"), print));
267 }
268 Dart_ExitScope();
269 Dart_ExitIsolate();
270 LOGI("Done setup");
271 return rtn;
272 }
273 return 0;
274 }
275
276 int VMGlue::CallUpdate() {
277 if (initialized_script_) {
278 // If the accelerometer has changed, first do that
279 // event.
280 Dart_EnterIsolate(isolate_);
281 if (accelerometer_changed_) {
282 Dart_Handle args[3];
283 LOGI("Invoking onAccelerometer(%f,%f,%f)", x_, y_, z_);
284 Dart_EnterScope();
285 args[0] = CheckError(Dart_NewDouble(x_));
286 args[1] = CheckError(Dart_NewDouble(y_));
287 args[2] = CheckError(Dart_NewDouble(z_));
288 Invoke("onAccelerometer", 3, args, false);
289 Dart_ExitScope();
290 accelerometer_changed_ = false;
291 }
292 Dart_EnterScope();
293 int rtn = Invoke("update_", 0, 0);
294 Dart_ExitScope();
295 Dart_ExitIsolate();
296 LOGI("Invoke update_ returns %d", rtn);
297 return rtn;
298 }
299 return -1;
300 }
301
302 int VMGlue::CallShutdown() {
303 if (initialized_script_) {
304 Dart_EnterIsolate(isolate_);
305 Dart_EnterScope();
306 int rtn = Invoke("shutdown", 0, 0);
307 Dart_ExitScope();
308 Dart_ExitIsolate();
309 return rtn;
310 }
311 return -1;
312 }
313
314 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen,
315 float pMoveX, float pMoveY) {
316 if (initialized_script_) {
317 LOGI("Invoking %s", pFunction);
318 Dart_EnterIsolate(isolate_);
319 Dart_EnterScope();
320 Dart_Handle args[3];
321 args[0] = CheckError(Dart_NewInteger(pWhen));
322 args[1] = CheckError(Dart_NewDouble(pMoveX));
323 args[2] = CheckError(Dart_NewDouble(pMoveY));
324 int rtn = Invoke(pFunction, 3, args, false);
325 Dart_ExitScope();
326 Dart_ExitIsolate();
327 LOGI("Done %s", pFunction);
328 return rtn;
329 }
330 return -1;
331 }
332
333 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t key_code,
334 bool isAltKeyDown, bool isCtrlKeyDown,
335 bool isShiftKeyDown, int32_t repeat) {
336 if (initialized_script_) {
337 LOGI("Invoking %s(_,%d,...)", function, key_code);
338 Dart_EnterIsolate(isolate_);
339 Dart_EnterScope();
340 Dart_Handle args[6];
341 args[0] = CheckError(Dart_NewInteger(when));
342 args[1] = CheckError(Dart_NewInteger(key_code));
343 args[2] = CheckError(Dart_NewBoolean(isAltKeyDown));
344 args[3] = CheckError(Dart_NewBoolean(isCtrlKeyDown));
345 args[4] = CheckError(Dart_NewBoolean(isShiftKeyDown));
346 args[5] = CheckError(Dart_NewInteger(repeat));
347 int rtn = Invoke(function, 6, args, false);
348 Dart_ExitScope();
349 Dart_ExitIsolate();
350 LOGI("Done %s", function);
351 return rtn;
352 }
353 return -1;
354 }
355
356 int VMGlue::Invoke(const char* function,
357 int argc,
358 Dart_Handle* args,
359 bool failIfNotDefined) {
360 // Lookup the library of the root script.
361 Dart_Handle library = Dart_RootLibrary();
362 if (Dart_IsNull(library)) {
363 LOGE("Unable to find root library\n");
364 return -1;
365 }
366
367 Dart_Handle nameHandle = Dart_NewStringFromCString(function);
368
369 Dart_Handle result = Dart_Invoke(library, nameHandle, argc, args);
370
371 if (Dart_IsError(result)) {
372 const char* error = Dart_GetError(result);
373 LOGE("Invoke %s failed: %s", function, error);
374 if (failIfNotDefined) {
375 return -1;
376 }
377 }
378
379 // TODO(vsm): I don't think we need this.
380 // Keep handling messages until the last active receive port is closed.
381 result = Dart_RunLoop();
382 if (Dart_IsError(result)) {
383 LOGE("Dart_RunLoop: %s\n", Dart_GetError(result));
384 return -1;
385 }
386
387 return 0;
388 }
389
390 void VMGlue::FinishMainIsolate() {
391 LOGI("Finish main isolate");
392 Dart_EnterIsolate(isolate_);
393 // Shutdown the isolate.
394 Dart_ShutdownIsolate();
395 isolate_ = NULL;
396 initialized_script_ = false;
397 }
398
OLDNEW
« no previous file with comments | « runtime/embedders/openglui/common/vm_glue.h ('k') | runtime/embedders/openglui/emulator/emulator_embedder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698