OLD | NEW |
---|---|
(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 #include <jni.h> | |
5 #include <string.h> | |
6 #include <stdlib.h> | |
7 #include <stdio.h> | |
8 | |
9 #include <EGL/egl.h> | |
10 #include <GLES2/gl2.h> | |
11 #include <GLES2/gl2ext.h> | |
12 | |
13 #include <android/log.h> | |
14 | |
15 #include "include/dart_api.h" | |
16 #include "bin/log.h" | |
17 | |
18 Dart_NativeFunction ResolveName(Dart_Handle name, int argc); | |
19 | |
20 DART_EXPORT Dart_Handle android_extension_Init(Dart_Handle parent_library) { | |
21 if (Dart_IsError(parent_library)) { return parent_library; } | |
22 | |
23 Dart_Handle result_code = Dart_SetNativeResolver(parent_library, ResolveName); | |
24 if (Dart_IsError(result_code)) return result_code; | |
25 | |
26 return Dart_Null(); | |
27 } | |
28 | |
29 Dart_Handle HandleError(Dart_Handle handle) { | |
30 if (Dart_IsError(handle)) Dart_PropagateError(handle); | |
31 return handle; | |
32 } | |
33 | |
34 void CheckGLError(const char *function) { | |
35 int error = glGetError(); | |
36 if (error != GL_NO_ERROR) { | |
37 int x = 0; | |
38 int y = *(int*)x; | |
39 Log::PrintErr("ERROR!: %s returns %d", function, y); | |
40 } | |
41 } | |
42 | |
43 void Log(Dart_NativeArguments arguments) { | |
44 Dart_EnterScope(); | |
45 Dart_Handle whatHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
46 intptr_t length[1]; | |
47 HandleError(Dart_StringLength(whatHandle, length)); | |
48 uint8_t* str[1]; | |
49 //str[0] = reinterpret_cast<uint8_t*>(malloc(length[0]+1)); | |
vsm
2012/11/30 17:00:30
Delete comment.
gram
2012/11/30 18:01:31
Done.
| |
50 HandleError(Dart_StringToUTF8(whatHandle, &str[0], length)); | |
51 str[0][*length] = 0; | |
52 Log::Print(const_cast<const GLchar*>(reinterpret_cast<GLchar*>(str[0]))); | |
53 Dart_ExitScope(); | |
54 } | |
55 | |
56 void SystemRand(Dart_NativeArguments arguments) { | |
57 Dart_EnterScope(); | |
58 Dart_Handle result = HandleError(Dart_NewInteger(rand())); | |
59 Dart_SetReturnValue(arguments, result); | |
60 Dart_ExitScope(); | |
61 } | |
62 | |
63 void SystemSrand(Dart_NativeArguments arguments) { | |
64 Dart_EnterScope(); | |
65 bool success = false; | |
66 Dart_Handle seed_object = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
67 if (Dart_IsInteger(seed_object)) { | |
68 bool fits; | |
69 HandleError(Dart_IntegerFitsIntoInt64(seed_object, &fits)); | |
70 if (fits) { | |
71 int64_t seed; | |
72 HandleError(Dart_IntegerToInt64(seed_object, &seed)); | |
73 srand(static_cast<unsigned>(seed)); | |
74 success = true; | |
75 } | |
76 } | |
77 Dart_SetReturnValue(arguments, HandleError(Dart_NewBoolean(success))); | |
78 Dart_ExitScope(); | |
79 } | |
80 | |
81 void EGLSwapBuffers(Dart_NativeArguments arguments) { | |
82 Log::Print("GLSwapBuffers"); | |
83 Dart_EnterScope(); | |
84 | |
85 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); | |
86 EGLSurface surface = eglGetCurrentSurface(EGL_DRAW); | |
87 eglSwapBuffers(display, surface); | |
88 | |
89 CheckGLError("eglSwapBuffers"); | |
90 Dart_ExitScope(); | |
91 } | |
92 | |
93 void GLAttachShader(Dart_NativeArguments arguments) { | |
94 Log::Print("GLAttachShader"); | |
95 Dart_EnterScope(); | |
96 | |
97 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
98 int64_t program; | |
99 HandleError(Dart_IntegerToInt64(programHandle, &program)); | |
100 | |
101 Dart_Handle shaderHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
102 int64_t shader; | |
103 HandleError(Dart_IntegerToInt64(shaderHandle, &shader)); | |
104 | |
105 glAttachShader(program, shader); | |
106 CheckGLError("glAttachShader"); | |
107 Dart_ExitScope(); | |
108 } | |
109 | |
110 void GLBindBuffer(Dart_NativeArguments arguments) { | |
111 Log::Print("GLBindBuffer"); | |
112 Dart_EnterScope(); | |
113 | |
114 Dart_Handle targetHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
115 int64_t target; | |
116 HandleError(Dart_IntegerToInt64(targetHandle, &target)); | |
117 | |
118 Dart_Handle bufferHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
119 int64_t buffer; | |
120 HandleError(Dart_IntegerToInt64(bufferHandle, &buffer)); | |
121 | |
122 glBindBuffer(target, buffer); | |
123 CheckGLError("glBindBuffer"); | |
124 Dart_ExitScope(); | |
125 } | |
126 | |
127 void GLBufferData(Dart_NativeArguments arguments) { | |
128 Log::Print("GLBufferData"); | |
129 Dart_EnterScope(); | |
130 | |
131 Dart_Handle targetHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
132 int64_t target; | |
133 HandleError(Dart_IntegerToInt64(targetHandle, &target)); | |
134 | |
135 Dart_Handle dataHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
136 intptr_t size; | |
137 HandleError(Dart_ListLength(dataHandle, &size)); | |
138 | |
139 Log::Print("Size: %d", size); | |
140 | |
141 // TODO(vsm): No guarantee that this is a float! | |
142 float* data = reinterpret_cast<float*>(malloc(size * sizeof(float))); | |
143 for (int i = 0; i < size; i++) { | |
144 Dart_Handle elemHandle = HandleError(Dart_ListGetAt(dataHandle, i)); | |
145 double value; | |
146 Dart_DoubleValue(elemHandle, &value); | |
147 data[i] = static_cast<float>(value); | |
148 Log::Print("Value[%d]: %f", i, data[i]); | |
149 } | |
150 | |
151 Dart_Handle usageHandle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
152 int64_t usage; | |
153 HandleError(Dart_IntegerToInt64(usageHandle, &usage)); | |
154 | |
155 glBufferData(target, size * sizeof(float), data, usage); | |
156 CheckGLError("glBufferData"); | |
157 free(data); | |
158 Dart_ExitScope(); | |
159 } | |
160 | |
161 void GLCompileShader(Dart_NativeArguments arguments) { | |
162 Dart_EnterScope(); | |
163 | |
164 Dart_Handle shaderHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
165 int64_t shader; | |
166 HandleError(Dart_IntegerToInt64(shaderHandle, &shader)); | |
167 | |
168 Log::Print("GLCompileShader(%d)", (int)shader); | |
169 glCompileShader(shader); | |
170 CheckGLError("glCompileShader"); | |
171 Dart_ExitScope(); | |
172 } | |
173 | |
174 void GLCreateBuffer(Dart_NativeArguments arguments) { | |
175 Log::Print("GLCreateBuffer"); | |
176 Dart_EnterScope(); | |
177 GLuint buffer; | |
178 | |
179 glGenBuffers(1, &buffer); | |
180 CheckGLError("glGenBuffers"); | |
181 Dart_Handle result = HandleError(Dart_NewInteger(buffer)); | |
182 Dart_SetReturnValue(arguments, result); | |
183 Dart_ExitScope(); | |
184 } | |
185 | |
186 void GLCreateProgram(Dart_NativeArguments arguments) { | |
187 Log::Print("GLCreateProgram"); | |
188 Dart_EnterScope(); | |
189 | |
190 int64_t program = glCreateProgram(); | |
191 CheckGLError("glCreateProgram"); | |
192 Dart_Handle result = HandleError(Dart_NewInteger(program)); | |
193 Dart_SetReturnValue(arguments, result); | |
194 Dart_ExitScope(); | |
195 } | |
196 | |
197 void GLCreateShader(Dart_NativeArguments arguments) { | |
198 Dart_EnterScope(); | |
199 | |
200 Dart_Handle typeHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
201 int64_t type; | |
202 HandleError(Dart_IntegerToInt64(typeHandle, &type)); | |
203 | |
204 int64_t shader = glCreateShader((GLenum)type); | |
205 Log::Print("GLCreateShader(%d)=>%d", (int)type, (int)shader); | |
206 CheckGLError("glCreateShader"); | |
207 Dart_Handle result = HandleError(Dart_NewInteger(shader)); | |
208 Dart_SetReturnValue(arguments, result); | |
209 Dart_ExitScope(); | |
210 } | |
211 | |
212 void GLDrawArrays(Dart_NativeArguments arguments) { | |
213 Log::Print("GLDrawArrays"); | |
214 Dart_EnterScope(); | |
215 | |
216 Dart_Handle modeHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
217 int64_t mode; | |
218 HandleError(Dart_IntegerToInt64(modeHandle, &mode)); | |
219 | |
220 Dart_Handle firstHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
221 int64_t first; | |
222 HandleError(Dart_IntegerToInt64(firstHandle, &first)); | |
223 | |
224 Dart_Handle countHandle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
225 int64_t count; | |
226 HandleError(Dart_IntegerToInt64(countHandle, &count)); | |
227 | |
228 glDrawArrays(mode, first, count); | |
229 CheckGLError("glDrawArrays"); | |
230 Dart_ExitScope(); | |
231 } | |
232 | |
233 void GLEnableVertexAttribArray(Dart_NativeArguments arguments) { | |
234 Log::Print("GLEnableVertexAttribArray"); | |
235 Dart_EnterScope(); | |
236 | |
237 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
vsm
2012/11/30 17:00:30
Nit line length.
gram
2012/11/30 18:01:31
Done.
| |
238 int64_t location; | |
239 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
240 | |
241 glEnableVertexAttribArray(location); | |
242 CheckGLError("glEnableVertexAttribArray"); | |
243 Dart_ExitScope(); | |
244 } | |
245 | |
246 void GLGetAttribLocation(Dart_NativeArguments arguments) { | |
247 Log::Print("GLGetAttribLocation"); | |
248 Dart_EnterScope(); | |
249 | |
250 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
251 int64_t program; | |
252 HandleError(Dart_IntegerToInt64(programHandle, &program)); | |
253 | |
254 Dart_Handle nameHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
255 intptr_t length; | |
256 HandleError(Dart_StringLength(nameHandle, &length)); | |
257 uint8_t* str; | |
258 //str = reinterpret_cast<uint8_t*>(malloc(length+1)); | |
259 HandleError(Dart_StringToUTF8(nameHandle, &str, &length)); | |
260 str[length] = 0; | |
261 | |
262 int64_t location = glGetAttribLocation(program, const_cast<const GLchar*>(rein terpret_cast<GLchar*>(str))); | |
vsm
2012/11/30 17:00:30
Nit length.
gram
2012/11/30 18:01:31
Done.
| |
263 CheckGLError("glGetAttribLocation"); | |
264 Dart_Handle result = HandleError(Dart_NewInteger(location)); | |
265 Dart_SetReturnValue(arguments, result); | |
266 Dart_ExitScope(); | |
267 } | |
268 | |
269 void GLGetError(Dart_NativeArguments arguments) { | |
270 Log::Print("GLGetError"); | |
271 Dart_EnterScope(); | |
272 | |
273 int64_t error = glGetError(); | |
274 Dart_Handle result = HandleError(Dart_NewInteger(error)); | |
275 Dart_SetReturnValue(arguments, result); | |
276 Dart_ExitScope(); | |
277 } | |
278 | |
279 void GLGetProgramParameter(Dart_NativeArguments arguments) { | |
280 Log::Print("GLGetProgramParameter"); | |
281 Dart_EnterScope(); | |
282 | |
283 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
284 int64_t program; | |
285 HandleError(Dart_IntegerToInt64(programHandle, &program)); | |
286 | |
287 Dart_Handle paramHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
288 int64_t param; | |
289 HandleError(Dart_IntegerToInt64(paramHandle, ¶m)); | |
290 | |
291 GLint value = -1; | |
292 glGetProgramiv(program, param, &value); | |
293 CheckGLError("glGetProgramiv"); | |
294 | |
295 Dart_Handle result = HandleError(Dart_NewInteger(value)); | |
296 Dart_SetReturnValue(arguments, result); | |
297 Dart_ExitScope(); | |
298 } | |
299 | |
300 void GLGetShaderParameter(Dart_NativeArguments arguments) { | |
301 Log::Print("GLGetShaderParameter"); | |
302 Dart_EnterScope(); | |
303 | |
304 Dart_Handle shaderHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
305 int64_t shader; | |
306 HandleError(Dart_IntegerToInt64(shaderHandle, &shader)); | |
307 | |
308 Dart_Handle paramHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
309 int64_t param; | |
310 HandleError(Dart_IntegerToInt64(paramHandle, ¶m)); | |
311 | |
312 GLint value = -1; | |
313 glGetShaderiv((GLuint)shader, (GLenum)param, &value); | |
314 CheckGLError("glGetShaderiv"); | |
315 | |
316 Dart_Handle result = HandleError(Dart_NewInteger(value)); | |
317 Dart_SetReturnValue(arguments, result); | |
318 Dart_ExitScope(); | |
319 } | |
320 | |
321 void GLGetShaderInfoLog(Dart_NativeArguments arguments) { | |
322 Log::Print("GLGetShaderInfoLog"); | |
323 Dart_EnterScope(); | |
324 | |
325 Dart_Handle shaderHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
326 int64_t shader; | |
327 HandleError(Dart_IntegerToInt64(shaderHandle, &shader)); | |
328 | |
329 GLint infoLogLength = 0; | |
330 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); | |
331 | |
332 GLchar* strInfoLog = new GLchar[infoLogLength + 1]; | |
333 glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog); | |
334 strInfoLog[infoLogLength] = 0; | |
335 | |
336 Dart_Handle result = HandleError(Dart_NewStringFromCString(strInfoLog)); | |
337 Dart_SetReturnValue(arguments, result); | |
338 Dart_ExitScope(); | |
339 delete[] strInfoLog; | |
340 } | |
341 | |
342 void GLGetProgramInfoLog(Dart_NativeArguments arguments) { | |
343 Log::Print("GLGetProgramInfoLog"); | |
344 Dart_EnterScope(); | |
345 | |
346 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
347 int64_t program; | |
348 HandleError(Dart_IntegerToInt64(programHandle, &program)); | |
349 | |
350 GLint infoLogLength; | |
351 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength); | |
352 | |
353 GLchar* strInfoLog = new GLchar[infoLogLength + 1]; | |
354 glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog); | |
355 strInfoLog[infoLogLength] = 0; | |
356 | |
357 Dart_Handle result = HandleError(Dart_NewStringFromCString(strInfoLog)); | |
358 Dart_SetReturnValue(arguments, result); | |
359 Dart_ExitScope(); | |
360 delete[] strInfoLog; | |
361 } | |
362 void GLGetUniformLocation(Dart_NativeArguments arguments) { | |
363 Log::Print("GLGetUniformLocation"); | |
364 Dart_EnterScope(); | |
365 | |
366 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
367 int64_t program; | |
368 HandleError(Dart_IntegerToInt64(programHandle, &program)); | |
369 | |
370 Dart_Handle nameHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
371 intptr_t length; | |
372 HandleError(Dart_StringLength(nameHandle, &length)); | |
373 uint8_t* str; | |
374 //str = reinterpret_cast<uint8_t*>(malloc(length+1)); | |
375 HandleError(Dart_StringToUTF8(nameHandle, &str, &length)); | |
376 str[length] = 0; | |
377 | |
378 int64_t location = glGetUniformLocation(program, const_cast<const GLchar*>(rei nterpret_cast<GLchar*>(str))); | |
379 CheckGLError("glGetUniformLocation"); | |
380 Dart_Handle result = HandleError(Dart_NewInteger(location)); | |
381 Dart_SetReturnValue(arguments, result); | |
382 Dart_ExitScope(); | |
383 } | |
384 | |
385 void GLLinkProgram(Dart_NativeArguments arguments) { | |
386 Log::Print("GLLinkProgram"); | |
387 Dart_EnterScope(); | |
388 | |
389 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
390 int64_t program; | |
391 HandleError(Dart_IntegerToInt64(programHandle, &program)); | |
392 | |
393 glLinkProgram(program); | |
394 CheckGLError("glLinkProgram"); | |
395 Dart_ExitScope(); | |
396 } | |
397 | |
398 void GLShaderSource(Dart_NativeArguments arguments) { | |
399 Log::Print("GLShaderSource"); | |
400 Dart_EnterScope(); | |
401 | |
402 Dart_Handle shaderHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
403 int64_t shader; | |
404 HandleError(Dart_IntegerToInt64(shaderHandle, &shader)); | |
405 | |
406 Dart_Handle sourceHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
407 intptr_t length[1]; | |
408 HandleError(Dart_StringLength(sourceHandle, length)); | |
409 Log::Print("Source length is %d", length[0]); | |
410 uint8_t* str[1]; | |
411 //str[0] = reinterpret_cast<uint8_t*>(malloc(length[0]+1)); | |
412 HandleError(Dart_StringToUTF8(sourceHandle, &str[0], length)); | |
413 Log::Print("Converted length is %d", length[0]); | |
414 str[0][*length] = 0; | |
415 | |
416 const GLchar* source = const_cast<const GLchar*>(reinterpret_cast<GLchar*>(str [0])); | |
417 Log::Print("Source: %s", source); | |
418 glShaderSource(shader, 1, const_cast<const GLchar**>(reinterpret_cast<GLchar** >(str)), NULL); | |
419 CheckGLError("glShaderSource"); | |
420 Dart_ExitScope(); | |
421 } | |
422 | |
423 void GLUseProgram(Dart_NativeArguments arguments) { | |
424 Log::Print("GLUseProgram"); | |
425 Dart_EnterScope(); | |
426 | |
427 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
428 int64_t program; | |
429 HandleError(Dart_IntegerToInt64(programHandle, &program)); | |
430 | |
431 glUseProgram(program); | |
432 CheckGLError("glUseProgram"); | |
433 Dart_ExitScope(); | |
434 } | |
435 | |
436 void GLUniform1i(Dart_NativeArguments arguments) { | |
437 Log::Print("GLUniform1i"); | |
438 Dart_EnterScope(); | |
439 | |
440 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
441 int64_t location; | |
442 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
443 | |
444 Dart_Handle v0Handle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
445 int64_t v0; | |
446 HandleError(Dart_IntegerToInt64(v0Handle, &v0)); | |
447 | |
448 glUniform1i(location, v0); | |
449 CheckGLError("glUniform1i"); | |
450 Dart_ExitScope(); | |
451 } | |
452 | |
453 void GLUniform2i(Dart_NativeArguments arguments) { | |
454 Log::Print("GLUniform2i"); | |
455 Dart_EnterScope(); | |
456 | |
457 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
458 int64_t location; | |
459 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
460 | |
461 Dart_Handle v0Handle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
462 int64_t v0; | |
463 HandleError(Dart_IntegerToInt64(v0Handle, &v0)); | |
464 | |
465 Dart_Handle v1Handle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
466 int64_t v1; | |
467 HandleError(Dart_IntegerToInt64(v1Handle, &v1)); | |
468 | |
469 glUniform2i(location, v0, v1); | |
470 CheckGLError("glUniform2i"); | |
471 Dart_ExitScope(); | |
472 } | |
473 | |
474 void GLUniform3i(Dart_NativeArguments arguments) { | |
475 Log::Print("GLUniform3i"); | |
476 Dart_EnterScope(); | |
477 | |
478 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
479 int64_t location; | |
480 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
481 | |
482 Dart_Handle v0Handle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
483 int64_t v0; | |
484 HandleError(Dart_IntegerToInt64(v0Handle, &v0)); | |
485 | |
486 Dart_Handle v1Handle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
487 int64_t v1; | |
488 HandleError(Dart_IntegerToInt64(v1Handle, &v1)); | |
489 | |
490 Dart_Handle v2Handle = HandleError(Dart_GetNativeArgument(arguments, 3)); | |
491 int64_t v2; | |
492 HandleError(Dart_IntegerToInt64(v2Handle, &v2)); | |
493 | |
494 glUniform3i(location, v0, v1, v2); | |
495 CheckGLError("glUniform3i"); | |
496 Dart_ExitScope(); | |
497 } | |
498 | |
499 void GLUniform4i(Dart_NativeArguments arguments) { | |
500 Log::Print("GLUniform4i"); | |
501 Dart_EnterScope(); | |
502 | |
503 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
504 int64_t location; | |
505 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
506 | |
507 Dart_Handle v0Handle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
508 int64_t v0; | |
509 HandleError(Dart_IntegerToInt64(v0Handle, &v0)); | |
510 | |
511 Dart_Handle v1Handle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
512 int64_t v1; | |
513 HandleError(Dart_IntegerToInt64(v1Handle, &v1)); | |
514 | |
515 Dart_Handle v2Handle = HandleError(Dart_GetNativeArgument(arguments, 3)); | |
516 int64_t v2; | |
517 HandleError(Dart_IntegerToInt64(v2Handle, &v2)); | |
518 | |
519 Dart_Handle v3Handle = HandleError(Dart_GetNativeArgument(arguments, 4)); | |
520 int64_t v3; | |
521 HandleError(Dart_IntegerToInt64(v3Handle, &v3)); | |
522 | |
523 glUniform4i(location, v0, v1, v2, v3); | |
524 CheckGLError("glUniform4i"); | |
525 Dart_ExitScope(); | |
526 } | |
527 | |
528 void GLUniform1f(Dart_NativeArguments arguments) { | |
529 Log::Print("GLUniform1f"); | |
530 Dart_EnterScope(); | |
531 | |
532 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
533 int64_t location; | |
534 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
535 | |
536 Dart_Handle v0Handle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
537 double v0; | |
538 HandleError(Dart_DoubleValue(v0Handle, &v0)); | |
539 | |
540 glUniform1f(location, v0); | |
541 CheckGLError("glUniform1f"); | |
542 Dart_ExitScope(); | |
543 } | |
544 | |
545 void GLUniform2f(Dart_NativeArguments arguments) { | |
546 Log::Print("GLUniform2f"); | |
547 Dart_EnterScope(); | |
548 | |
549 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
550 int64_t location; | |
551 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
552 | |
553 Dart_Handle v0Handle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
554 double v0; | |
555 HandleError(Dart_DoubleValue(v0Handle, &v0)); | |
556 | |
557 Dart_Handle v1Handle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
558 double v1; | |
559 HandleError(Dart_DoubleValue(v1Handle, &v1)); | |
560 | |
561 glUniform2f(location, v0, v1); | |
562 CheckGLError("glUniform2f"); | |
563 Dart_ExitScope(); | |
564 } | |
565 | |
566 void GLUniform3f(Dart_NativeArguments arguments) { | |
567 Log::Print("GLUniform3f"); | |
568 Dart_EnterScope(); | |
569 | |
570 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
571 int64_t location; | |
572 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
573 | |
574 Dart_Handle v0Handle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
575 double v0; | |
576 HandleError(Dart_DoubleValue(v0Handle, &v0)); | |
577 | |
578 Dart_Handle v1Handle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
579 double v1; | |
580 HandleError(Dart_DoubleValue(v1Handle, &v1)); | |
581 | |
582 Dart_Handle v2Handle = HandleError(Dart_GetNativeArgument(arguments, 3)); | |
583 double v2; | |
584 HandleError(Dart_DoubleValue(v2Handle, &v2)); | |
585 | |
586 glUniform3f(location, v0, v1, v2); | |
587 CheckGLError("glUniform3f"); | |
588 Dart_ExitScope(); | |
589 } | |
590 | |
591 void GLUniform4f(Dart_NativeArguments arguments) { | |
592 Log::Print("GLUniform4f"); | |
593 Dart_EnterScope(); | |
594 | |
595 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
596 int64_t location; | |
597 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
598 | |
599 Dart_Handle v0Handle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
600 double v0; | |
601 HandleError(Dart_DoubleValue(v0Handle, &v0)); | |
602 | |
603 Dart_Handle v1Handle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
604 double v1; | |
605 HandleError(Dart_DoubleValue(v1Handle, &v1)); | |
606 | |
607 Dart_Handle v2Handle = HandleError(Dart_GetNativeArgument(arguments, 3)); | |
608 double v2; | |
609 HandleError(Dart_DoubleValue(v2Handle, &v2)); | |
610 | |
611 Dart_Handle v3Handle = HandleError(Dart_GetNativeArgument(arguments, 4)); | |
612 double v3; | |
613 HandleError(Dart_DoubleValue(v3Handle, &v3)); | |
614 | |
615 glUniform4f(location, v0, v1, v2, v3); | |
616 CheckGLError("glUniform4f"); | |
617 Dart_ExitScope(); | |
618 } | |
619 | |
620 void GLUniform1iv(Dart_NativeArguments arguments) { | |
621 Log::Print("GLUniform1iv"); | |
622 Dart_EnterScope(); | |
623 | |
624 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
625 int64_t location; | |
626 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
627 | |
628 Dart_Handle argHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
629 | |
630 if (Dart_IsList(argHandle)) { | |
631 int len; | |
632 HandleError(Dart_ListLength(argHandle, &len)); | |
633 GLint* list = new GLint[len]; | |
634 for (int i = 0; i < len; i++) { | |
635 Dart_Handle vHandle = Dart_ListGetAt(argHandle, i); | |
636 int64_t v; | |
637 HandleError(Dart_IntegerToInt64(vHandle, &v)); | |
638 list[i] = v; | |
639 } | |
640 glUniform1iv(location, len, list); | |
641 delete [] list; | |
642 CheckGLError("glUniform1iv"); | |
643 } | |
644 Dart_ExitScope(); | |
645 } | |
646 | |
647 void GLUniform2iv(Dart_NativeArguments arguments) { | |
648 Log::Print("GLUniform2iv"); | |
649 Dart_EnterScope(); | |
650 | |
651 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
652 int64_t location; | |
653 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
654 | |
655 Dart_Handle argHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
656 | |
657 if (Dart_IsList(argHandle)) { | |
658 int len; | |
659 HandleError(Dart_ListLength(argHandle, &len)); | |
660 GLint* list = new GLint[len]; | |
661 for (int i = 0; i < len; i++) { | |
662 Dart_Handle vHandle = Dart_ListGetAt(argHandle, i); | |
663 int64_t v; | |
664 HandleError(Dart_IntegerToInt64(vHandle, &v)); | |
665 list[i] = v; | |
666 } | |
667 glUniform2iv(location, len / 2, list); | |
668 delete [] list; | |
669 CheckGLError("glUniform2iv"); | |
670 } | |
671 Dart_ExitScope(); | |
672 } | |
673 | |
674 void GLUniform3iv(Dart_NativeArguments arguments) { | |
675 Log::Print("GLUniform3iv"); | |
676 Dart_EnterScope(); | |
677 | |
678 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
679 int64_t location; | |
680 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
681 | |
682 Dart_Handle argHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
683 | |
684 if (Dart_IsList(argHandle)) { | |
685 int len; | |
686 HandleError(Dart_ListLength(argHandle, &len)); | |
687 GLint* list = new GLint[len]; | |
688 for (int i = 0; i < len; i++) { | |
689 Dart_Handle vHandle = Dart_ListGetAt(argHandle, i); | |
690 int64_t v; | |
691 HandleError(Dart_IntegerToInt64(vHandle, &v)); | |
692 list[i] = v; | |
693 } | |
694 glUniform3iv(location, len / 3, list); | |
695 delete [] list; | |
696 CheckGLError("glUniform3iv"); | |
697 } | |
698 Dart_ExitScope(); | |
699 } | |
700 | |
701 void GLUniform4iv(Dart_NativeArguments arguments) { | |
702 Log::Print("GLUniform4iv"); | |
703 Dart_EnterScope(); | |
704 | |
705 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
706 int64_t location; | |
707 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
708 | |
709 Dart_Handle argHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
710 | |
711 if (Dart_IsList(argHandle)) { | |
712 int len; | |
713 HandleError(Dart_ListLength(argHandle, &len)); | |
714 GLint* list = new GLint[len]; | |
715 for (int i = 0; i < len; i++) { | |
716 Dart_Handle vHandle = Dart_ListGetAt(argHandle, i); | |
717 int64_t v; | |
718 HandleError(Dart_IntegerToInt64(vHandle, &v)); | |
719 list[i] = v; | |
720 } | |
721 glUniform1iv(location, len / 4, list); | |
722 delete [] list; | |
723 CheckGLError("glUniform4iv"); | |
724 } | |
725 Dart_ExitScope(); | |
726 } | |
727 | |
728 void GLUniform1fv(Dart_NativeArguments arguments) { | |
729 Log::Print("GLUniform1fv"); | |
730 Dart_EnterScope(); | |
731 | |
732 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
733 int64_t location; | |
734 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
735 | |
736 Dart_Handle argHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
737 | |
738 if (Dart_IsList(argHandle)) { | |
739 int len; | |
740 HandleError(Dart_ListLength(argHandle, &len)); | |
741 GLfloat* list = new GLfloat[len]; | |
742 for (int i = 0; i < len; i++) { | |
743 Dart_Handle vHandle = Dart_ListGetAt(argHandle, i); | |
744 double v; | |
745 HandleError(Dart_DoubleValue(vHandle, &v)); | |
746 list[i] = v; | |
747 } | |
748 glUniform1fv(location, len, list); | |
749 delete [] list; | |
750 CheckGLError("glUniform1fv"); | |
751 } | |
752 Dart_ExitScope(); | |
753 } | |
754 | |
755 void GLUniform2fv(Dart_NativeArguments arguments) { | |
756 Log::Print("GLUniform2fv"); | |
757 Dart_EnterScope(); | |
758 | |
759 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
760 int64_t location; | |
761 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
762 | |
763 Dart_Handle argHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
764 | |
765 if (Dart_IsList(argHandle)) { | |
766 int len; | |
767 HandleError(Dart_ListLength(argHandle, &len)); | |
768 GLfloat* list = new GLfloat[len]; | |
769 for (int i = 0; i < len; i++) { | |
770 Dart_Handle vHandle = Dart_ListGetAt(argHandle, i); | |
771 double v; | |
772 HandleError(Dart_DoubleValue(vHandle, &v)); | |
773 list[i] = v; | |
774 } | |
775 glUniform2fv(location, len / 2, list); | |
776 delete [] list; | |
777 CheckGLError("glUniform2fv"); | |
778 } | |
779 Dart_ExitScope(); | |
780 } | |
781 | |
782 void GLUniform3fv(Dart_NativeArguments arguments) { | |
783 Log::Print("GLUniform3fv"); | |
784 Dart_EnterScope(); | |
785 | |
786 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
787 int64_t location; | |
788 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
789 | |
790 Dart_Handle argHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
791 | |
792 if (Dart_IsList(argHandle)) { | |
793 int len; | |
794 HandleError(Dart_ListLength(argHandle, &len)); | |
795 GLfloat* list = new GLfloat[len]; | |
796 for (int i = 0; i < len; i++) { | |
797 Dart_Handle vHandle = Dart_ListGetAt(argHandle, i); | |
798 double v; | |
799 HandleError(Dart_DoubleValue(vHandle, &v)); | |
800 list[i] = v; | |
801 } | |
802 glUniform3fv(location, len / 3, list); | |
803 delete [] list; | |
804 CheckGLError("glUniform3fv"); | |
805 } | |
806 Dart_ExitScope(); | |
807 } | |
808 | |
809 void GLUniform4fv(Dart_NativeArguments arguments) { | |
810 Log::Print("In GLUniform4fv"); | |
811 Dart_EnterScope(); | |
812 | |
813 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ; | |
814 int64_t location; | |
815 HandleError(Dart_IntegerToInt64(locationHandle, &location)); | |
816 | |
817 Dart_Handle argHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
818 | |
819 if (Dart_IsList(argHandle)) { | |
820 int len; | |
821 HandleError(Dart_ListLength(argHandle, &len)); | |
822 GLfloat* list = new GLfloat[len]; | |
823 for (int i = 0; i < len; i++) { | |
824 Dart_Handle vHandle = Dart_ListGetAt(argHandle, i); | |
825 double v; | |
826 HandleError(Dart_DoubleValue(vHandle, &v)); | |
827 list[i] = v; | |
828 } | |
829 glUniform4fv(location, len / 4, list); | |
830 delete [] list; | |
831 CheckGLError("glUniform4fv"); | |
832 } | |
833 Dart_ExitScope(); | |
834 } | |
835 | |
836 void GLViewport(Dart_NativeArguments arguments) { | |
837 Log::Print("GLViewport"); | |
838 Dart_EnterScope(); | |
839 | |
840 Dart_Handle xHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
841 int64_t x; | |
842 HandleError(Dart_IntegerToInt64(xHandle, &x)); | |
843 | |
844 Dart_Handle yHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
845 int64_t y; | |
846 HandleError(Dart_IntegerToInt64(yHandle, &y)); | |
847 | |
848 Dart_Handle widthHandle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
849 int64_t width; | |
850 HandleError(Dart_IntegerToInt64(widthHandle, &width)); | |
851 | |
852 Dart_Handle heightHandle = HandleError(Dart_GetNativeArgument(arguments, 3)); | |
853 int64_t height; | |
854 HandleError(Dart_IntegerToInt64(heightHandle, &height)); | |
855 | |
856 Log::Print("Dimensions: [%d, %d, %d, %d]", (int) x, (int)y, (int) width, (int) height); | |
857 | |
858 glViewport((int) x, (int) y, (int) width, (int) height); | |
859 CheckGLError("glViewPort"); | |
860 Dart_ExitScope(); | |
861 } | |
862 | |
863 void GLVertexAttribPointer(Dart_NativeArguments arguments) { | |
864 Log::Print("GLVertexAttribPointer"); | |
865 Dart_EnterScope(); | |
866 | |
867 Dart_Handle indexHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
868 int64_t index; | |
869 HandleError(Dart_IntegerToInt64(indexHandle, &index)); | |
870 | |
871 Dart_Handle sizeHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
872 int64_t size; | |
873 HandleError(Dart_IntegerToInt64(sizeHandle, &size)); | |
874 | |
875 Dart_Handle typeHandle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
876 int64_t type; | |
877 HandleError(Dart_IntegerToInt64(typeHandle, &type)); | |
878 | |
879 Dart_Handle normalizedHandle = HandleError(Dart_GetNativeArgument(arguments, 3 )); | |
880 bool normalized; | |
881 HandleError(Dart_BooleanValue(normalizedHandle, &normalized)); | |
882 | |
883 Dart_Handle strideHandle = HandleError(Dart_GetNativeArgument(arguments, 4)); | |
884 int64_t stride; | |
885 HandleError(Dart_IntegerToInt64(strideHandle, &stride)); | |
886 | |
887 Dart_Handle pointerHandle = HandleError(Dart_GetNativeArgument(arguments, 5)); | |
888 int64_t pointerValue; | |
889 HandleError(Dart_IntegerToInt64(pointerHandle, &pointerValue)); | |
890 const void* pointer; | |
891 pointer = const_cast<const void*>(reinterpret_cast<void*>(pointerValue)); | |
892 | |
893 glVertexAttribPointer(index, (int) size, (int) type, normalized, (int) stride, pointer); | |
894 CheckGLError("glVertexAttribPointer"); | |
895 Dart_ExitScope(); | |
896 } | |
897 | |
898 void GLClearColor(Dart_NativeArguments arguments) { | |
899 Log::Print("GLClearColor"); | |
900 Dart_EnterScope(); | |
901 | |
902 Dart_Handle redHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
903 double red; | |
904 HandleError(Dart_DoubleValue(redHandle, &red)); | |
905 | |
906 Dart_Handle greenHandle = HandleError(Dart_GetNativeArgument(arguments, 1)); | |
907 double green; | |
908 HandleError(Dart_DoubleValue(greenHandle, &green)); | |
909 | |
910 Dart_Handle blueHandle = HandleError(Dart_GetNativeArgument(arguments, 2)); | |
911 double blue; | |
912 HandleError(Dart_DoubleValue(blueHandle, &blue)); | |
913 | |
914 Dart_Handle alphaHandle = HandleError(Dart_GetNativeArgument(arguments, 3)); | |
915 double alpha; | |
916 HandleError(Dart_DoubleValue(alphaHandle, &alpha)); | |
917 | |
918 glClearColor(red, green, blue, alpha); | |
919 CheckGLError("glClearColor"); | |
920 Dart_ExitScope(); | |
921 } | |
922 | |
923 void GLClearDepth(Dart_NativeArguments arguments) { | |
924 Log::Print("GLClearDepth"); | |
925 Dart_EnterScope(); | |
926 | |
927 Dart_Handle depthHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
928 double depth; | |
929 HandleError(Dart_DoubleValue(depthHandle, &depth)); | |
930 | |
931 glClearDepthf(depth); | |
932 CheckGLError("glClearDepthf"); | |
933 Dart_ExitScope(); | |
934 } | |
935 | |
936 void GLClear(Dart_NativeArguments arguments) { | |
937 Log::Print("GLClear"); | |
938 Dart_EnterScope(); | |
939 Dart_Handle maskHandle = HandleError(Dart_GetNativeArgument(arguments, 0)); | |
940 int64_t mask; | |
941 HandleError(Dart_IntegerToInt64(maskHandle, &mask)); | |
942 glClear(mask); | |
943 CheckGLError("glClear"); | |
944 Dart_ExitScope(); | |
945 } | |
946 | |
947 void GLArrayBuffer(Dart_NativeArguments arguments) { | |
948 Log::Print("GLArrayBuffer"); | |
949 Dart_EnterScope(); | |
950 Dart_Handle result = HandleError(Dart_NewInteger(GL_ARRAY_BUFFER)); | |
951 Dart_SetReturnValue(arguments, result); | |
952 Dart_ExitScope(); | |
953 } | |
954 | |
955 void GLColorBufferBit(Dart_NativeArguments arguments) { | |
956 Log::Print("GLColorBuffer"); | |
957 Dart_EnterScope(); | |
958 Dart_Handle result = HandleError(Dart_NewInteger(GL_COLOR_BUFFER_BIT)); | |
959 Dart_SetReturnValue(arguments, result); | |
960 Dart_ExitScope(); | |
961 } | |
962 | |
963 void GLCompileStatus(Dart_NativeArguments arguments) { | |
964 Log::Print("GLCompileStatus"); | |
965 Dart_EnterScope(); | |
966 Dart_Handle result = HandleError(Dart_NewInteger(GL_COMPILE_STATUS)); | |
967 Dart_SetReturnValue(arguments, result); | |
968 Dart_ExitScope(); | |
969 } | |
970 | |
971 void GLDepthBufferBit(Dart_NativeArguments arguments) { | |
972 Log::Print("GLDepthBufferBit"); | |
973 Dart_EnterScope(); | |
974 Dart_Handle result = HandleError(Dart_NewInteger(GL_DEPTH_BUFFER_BIT)); | |
975 Dart_SetReturnValue(arguments, result); | |
976 Dart_ExitScope(); | |
977 } | |
978 | |
979 void GLFloat(Dart_NativeArguments arguments) { | |
980 Dart_EnterScope(); | |
981 Dart_Handle result = HandleError(Dart_NewInteger(GL_FLOAT)); | |
982 Dart_SetReturnValue(arguments, result); | |
983 Dart_ExitScope(); | |
984 } | |
985 | |
986 void GLFragmentShader(Dart_NativeArguments arguments) { | |
987 Dart_EnterScope(); | |
988 Dart_Handle result = HandleError(Dart_NewInteger(GL_FRAGMENT_SHADER)); | |
989 Dart_SetReturnValue(arguments, result); | |
990 Dart_ExitScope(); | |
991 } | |
992 | |
993 void GLLinkStatus(Dart_NativeArguments arguments) { | |
994 Dart_EnterScope(); | |
995 Dart_Handle result = HandleError(Dart_NewInteger(GL_LINK_STATUS)); | |
996 Dart_SetReturnValue(arguments, result); | |
997 Dart_ExitScope(); | |
998 } | |
999 | |
1000 void GLStaticDraw(Dart_NativeArguments arguments) { | |
1001 Dart_EnterScope(); | |
1002 Dart_Handle result = HandleError(Dart_NewInteger(GL_STATIC_DRAW)); | |
1003 Dart_SetReturnValue(arguments, result); | |
1004 Dart_ExitScope(); | |
1005 } | |
1006 | |
1007 void GLTriangleStrip(Dart_NativeArguments arguments) { | |
1008 Dart_EnterScope(); | |
1009 Dart_Handle result = HandleError(Dart_NewInteger(GL_TRIANGLE_STRIP)); | |
1010 Dart_SetReturnValue(arguments, result); | |
1011 Dart_ExitScope(); | |
1012 } | |
1013 | |
1014 void GLTriangles(Dart_NativeArguments arguments) { | |
1015 Dart_EnterScope(); | |
1016 Dart_Handle result = HandleError(Dart_NewInteger(GL_TRIANGLES)); | |
1017 Dart_SetReturnValue(arguments, result); | |
1018 Dart_ExitScope(); | |
1019 } | |
1020 | |
1021 void GLTrue(Dart_NativeArguments arguments) { | |
1022 Dart_EnterScope(); | |
1023 Dart_Handle result = HandleError(Dart_NewInteger(GL_TRUE)); | |
1024 Dart_SetReturnValue(arguments, result); | |
1025 Dart_ExitScope(); | |
1026 } | |
1027 | |
1028 void GLVertexShader(Dart_NativeArguments arguments) { | |
1029 Dart_EnterScope(); | |
1030 Dart_Handle result = HandleError(Dart_NewInteger(GL_VERTEX_SHADER)); | |
1031 Dart_SetReturnValue(arguments, result); | |
1032 Dart_ExitScope(); | |
1033 } | |
1034 | |
1035 uint8_t* randomArray(int seed, int length) { | |
1036 if (length <= 0 || length > 10000000) return NULL; | |
1037 uint8_t* values = reinterpret_cast<uint8_t*>(malloc(length)); | |
1038 if (NULL == values) return NULL; | |
1039 srand(seed); | |
1040 for (int i = 0; i < length; ++i) { | |
1041 values[i] = rand() % 256; | |
1042 } | |
1043 return values; | |
1044 } | |
1045 | |
1046 void wrappedRandomArray(Dart_Port dest_port_id, | |
1047 Dart_Port reply_port_id, | |
1048 Dart_CObject* message) { | |
1049 if (message->type == Dart_CObject::kArray && | |
1050 2 == message->value.as_array.length) { | |
1051 // Use .as_array and .as_int32 to access the data in the Dart_CObject. | |
1052 Dart_CObject* param0 = message->value.as_array.values[0]; | |
1053 Dart_CObject* param1 = message->value.as_array.values[1]; | |
1054 if (param0->type == Dart_CObject::kInt32 && | |
1055 param1->type == Dart_CObject::kInt32) { | |
1056 int length = param0->value.as_int32; | |
1057 int seed = param1->value.as_int32; | |
1058 | |
1059 uint8_t* values = randomArray(seed, length); | |
1060 | |
1061 if (values != NULL) { | |
1062 Dart_CObject result; | |
1063 result.type = Dart_CObject::kUint8Array; | |
1064 result.value.as_byte_array.values = values; | |
1065 result.value.as_byte_array.length = length; | |
1066 Dart_PostCObject(reply_port_id, &result); | |
1067 free(values); | |
1068 // It is OK that result is destroyed when function exits. | |
1069 // Dart_PostCObject has copied its data. | |
1070 return; | |
1071 } | |
1072 } | |
1073 } | |
1074 Dart_CObject result; | |
1075 result.type = Dart_CObject::kNull; | |
1076 Dart_PostCObject(reply_port_id, &result); | |
1077 } | |
1078 | |
1079 void randomArrayServicePort(Dart_NativeArguments arguments) { | |
1080 Dart_EnterScope(); | |
1081 Dart_SetReturnValue(arguments, Dart_Null()); | |
1082 Dart_Port service_port = | |
1083 Dart_NewNativePort("RandomArrayService", wrappedRandomArray, true); | |
1084 if (service_port != ((Dart_Port)0)) { | |
1085 Dart_Handle send_port = HandleError(Dart_NewSendPort(service_port)); | |
1086 Dart_SetReturnValue(arguments, send_port); | |
1087 } | |
1088 Dart_ExitScope(); | |
1089 } | |
1090 | |
1091 | |
1092 struct FunctionLookup { | |
1093 const char* name; | |
1094 Dart_NativeFunction function; | |
1095 }; | |
1096 | |
1097 FunctionLookup function_list[] = { | |
1098 {"Log", Log}, | |
1099 {"SystemRand", SystemRand}, | |
1100 {"SystemSrand", SystemSrand}, | |
1101 {"EGLSwapBuffers", EGLSwapBuffers}, | |
1102 {"GLAttachShader", GLAttachShader}, | |
1103 {"GLBindBuffer", GLBindBuffer}, | |
1104 {"GLBufferData", GLBufferData}, | |
1105 {"GLClear", GLClear}, | |
1106 {"GLClearColor", GLClearColor}, | |
1107 {"GLClearDepth", GLClearDepth}, | |
1108 {"GLCompileShader", GLCompileShader}, | |
1109 {"GLCreateBuffer", GLCreateBuffer}, | |
1110 {"GLCreateProgram", GLCreateProgram}, | |
1111 {"GLCreateShader", GLCreateShader}, | |
1112 {"GLDrawArrays", GLDrawArrays}, | |
1113 {"GLEnableVertexAttribArray", GLEnableVertexAttribArray}, | |
1114 {"GLGetAttribLocation", GLGetAttribLocation}, | |
1115 {"GLGetError", GLGetError}, | |
1116 {"GLGetProgramParameter", GLGetProgramParameter}, | |
1117 {"GLGetShaderParameter", GLGetShaderParameter}, | |
1118 {"GLGetUniformLocation", GLGetUniformLocation}, | |
1119 {"GLLinkProgram", GLLinkProgram}, | |
1120 {"GLShaderSource", GLShaderSource}, | |
1121 {"GLUniform1f", GLUniform1f}, | |
1122 {"GLUniform2f", GLUniform2f}, | |
1123 {"GLUniform3f", GLUniform3f}, | |
1124 {"GLUniform4f", GLUniform4f}, | |
1125 {"GLUniform1i", GLUniform1i}, | |
1126 {"GLUniform2i", GLUniform2i}, | |
1127 {"GLUniform3i", GLUniform3i}, | |
1128 {"GLUniform4i", GLUniform4i}, | |
1129 {"GLUniform1fv", GLUniform1fv}, | |
1130 {"GLUniform2fv", GLUniform2fv}, | |
1131 {"GLUniform3fv", GLUniform3fv}, | |
1132 {"GLUniform4fv", GLUniform4fv}, | |
1133 {"GLUniform1iv", GLUniform1iv}, | |
1134 {"GLUniform2iv", GLUniform2iv}, | |
1135 {"GLUniform3iv", GLUniform3iv}, | |
1136 {"GLUniform4iv", GLUniform4iv}, | |
1137 {"GLUseProgram", GLUseProgram}, | |
1138 {"GLVertexAttribPointer", GLVertexAttribPointer}, | |
1139 {"GLViewport", GLViewport}, | |
1140 {"GLArrayBuffer", GLArrayBuffer}, | |
1141 {"GLColorBufferBit", GLColorBufferBit}, | |
1142 {"GLCompileStatus", GLCompileStatus}, | |
1143 {"GLDepthBufferBit", GLDepthBufferBit}, | |
1144 {"GLFloat", GLFloat}, | |
1145 {"GLFragmentShader", GLFragmentShader}, | |
1146 {"GLLinkStatus", GLLinkStatus}, | |
1147 {"GLTriangleStrip", GLTriangleStrip}, | |
1148 {"GLTriangles", GLTriangles}, | |
1149 {"GLTrue", GLTrue}, | |
1150 {"GLStaticDraw", GLStaticDraw}, | |
1151 {"GLVertexShader", GLVertexShader}, | |
1152 {"GLGetShaderInfoLog", GLGetShaderInfoLog}, | |
1153 {"GLGetProgramInfoLog", GLGetProgramInfoLog}, | |
1154 {"RandomArray_ServicePort", randomArrayServicePort}, | |
1155 {NULL, NULL}}; | |
1156 | |
1157 Dart_NativeFunction ResolveName(Dart_Handle name, int argc) { | |
1158 if (!Dart_IsString(name)) return NULL; | |
1159 Dart_NativeFunction result = NULL; | |
1160 Dart_EnterScope(); | |
1161 const char* cname; | |
1162 HandleError(Dart_StringToCString(name, &cname)); | |
1163 for (int i=0; function_list[i].name != NULL; ++i) { | |
1164 if (strcmp(function_list[i].name, cname) == 0) { | |
1165 result = function_list[i].function; | |
1166 break; | |
1167 } | |
1168 } | |
1169 Dart_ExitScope(); | |
1170 return result; | |
1171 } | |
OLD | NEW |