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

Side by Side Diff: samples/android/android_extension.cc

Issue 11362103: Rotating spheres sample (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: More cleanup Created 8 years 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
« no previous file with comments | « samples/android/android.gyp ('k') | samples/android/ant.properties » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include <string.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #include <GLES2/gl2.h>
9 #include <GLES2/gl2ext.h>
10
11 #include <android/log.h>
12
13 #include "dart_api.h"
14
15 #define LOGX(LOG_LEVEL, ...) do { \
16 __android_log_print(LOG_LEVEL, "DartExt", __VA_ARGS__); \
17 } while (0)
18 #define LOGI(...) LOGX(ANDROID_LOG_INFO, __VA_ARGS__)
19 #define LOGW(...) LOGX(ANDROID_LOG_WARN, __VA_ARGS__)
20 #define LOGE(...) LOGX(ANDROID_LOG_ERROR, __VA_ARGS__)
21
22
23 Dart_NativeFunction ResolveName(Dart_Handle name, int argc);
24
25 DART_EXPORT Dart_Handle android_extension_Init(Dart_Handle parent_library) {
26 if (Dart_IsError(parent_library)) { return parent_library; }
27
28 Dart_Handle result_code = Dart_SetNativeResolver(parent_library, ResolveName);
29 if (Dart_IsError(result_code)) return result_code;
30
31 return Dart_Null();
32 }
33
34 Dart_Handle HandleError(Dart_Handle handle) {
35 if (Dart_IsError(handle)) Dart_PropagateError(handle);
36 return handle;
37 }
38
39 void CheckGLError() {
40 int error = glGetError();
41 if (error != GL_NO_ERROR) {
42 LOGI("ERROR!");
43 int x = 0;
44 int y = *(int*)x;
45 LOGI("ERROR!: %d", y);
46 }
47 }
48
49 void SystemRand(Dart_NativeArguments arguments) {
50 Dart_EnterScope();
51 Dart_Handle result = HandleError(Dart_NewInteger(rand()));
52 Dart_SetReturnValue(arguments, result);
53 Dart_ExitScope();
54 }
55
56 void SystemSrand(Dart_NativeArguments arguments) {
57 Dart_EnterScope();
58 bool success = false;
59 Dart_Handle seed_object = HandleError(Dart_GetNativeArgument(arguments, 0));
60 if (Dart_IsInteger(seed_object)) {
61 bool fits;
62 HandleError(Dart_IntegerFitsIntoInt64(seed_object, &fits));
63 if (fits) {
64 int64_t seed;
65 HandleError(Dart_IntegerToInt64(seed_object, &seed));
66 srand(static_cast<unsigned>(seed));
67 success = true;
68 }
69 }
70 Dart_SetReturnValue(arguments, HandleError(Dart_NewBoolean(success)));
71 Dart_ExitScope();
72 }
73
74 void GLAttachShader(Dart_NativeArguments arguments) {
75 LOGI("GLAttachShader");
76 Dart_EnterScope();
77
78 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
79 int64_t program;
80 HandleError(Dart_IntegerToInt64(programHandle, &program));
81
82 Dart_Handle shaderHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
83 int64_t shader;
84 HandleError(Dart_IntegerToInt64(shaderHandle, &shader));
85
86 glAttachShader(program, shader);
87 CheckGLError();
88 Dart_ExitScope();
89 }
90
91 void GLBindBuffer(Dart_NativeArguments arguments) {
92 LOGI("GLBindBuffer");
93 Dart_EnterScope();
94
95 Dart_Handle targetHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
96 int64_t target;
97 HandleError(Dart_IntegerToInt64(targetHandle, &target));
98
99 Dart_Handle bufferHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
100 int64_t buffer;
101 HandleError(Dart_IntegerToInt64(bufferHandle, &buffer));
102
103 glBindBuffer(target, buffer);
104 CheckGLError();
105 Dart_ExitScope();
106 }
107
108 void GLBufferData(Dart_NativeArguments arguments) {
109 LOGI("GLBufferData");
110 Dart_EnterScope();
111
112 Dart_Handle targetHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
113 int64_t target;
114 HandleError(Dart_IntegerToInt64(targetHandle, &target));
115
116 Dart_Handle dataHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
117 intptr_t size;
118 HandleError(Dart_ListLength(dataHandle, &size));
119
120 LOGI("Size: %d", size);
121
122 // TODO(vsm): No guarantee that this is a float!
123 float* data = reinterpret_cast<float*>(malloc(size * sizeof(float)));
124 for (int i = 0; i < size; i++) {
125 Dart_Handle elemHandle = HandleError(Dart_ListGetAt(dataHandle, i));
126 double value;
127 Dart_DoubleValue(elemHandle, &value);
128 data[i] = static_cast<float>(value);
129 LOGI("Value[%d]: %f", i, data[i]);
130 }
131
132 Dart_Handle usageHandle = HandleError(Dart_GetNativeArgument(arguments, 2));
133 int64_t usage;
134 HandleError(Dart_IntegerToInt64(usageHandle, &usage));
135
136 glBufferData(target, size*sizeof(float), data, usage);
137 CheckGLError();
138 free(data);
139 Dart_ExitScope();
140 }
141
142 void GLCompileShader(Dart_NativeArguments arguments) {
143 LOGI("GLCompileShader");
144 Dart_EnterScope();
145
146 Dart_Handle shaderHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
147 int64_t shader;
148 HandleError(Dart_IntegerToInt64(shaderHandle, &shader));
149
150 glCompileShader(shader);
151 CheckGLError();
152 Dart_ExitScope();
153 }
154
155 void GLCreateBuffer(Dart_NativeArguments arguments) {
156 LOGI("GLCreateBuffer");
157 Dart_EnterScope();
158 GLuint buffer;
159
160 glGenBuffers(1, &buffer);
161 CheckGLError();
162 Dart_Handle result = HandleError(Dart_NewInteger(buffer));
163 Dart_SetReturnValue(arguments, result);
164 Dart_ExitScope();
165 }
166
167 void GLCreateProgram(Dart_NativeArguments arguments) {
168 LOGI("GLCreateProgram");
169 Dart_EnterScope();
170
171 int64_t program = glCreateProgram();
172 CheckGLError();
173 Dart_Handle result = HandleError(Dart_NewInteger(program));
174 Dart_SetReturnValue(arguments, result);
175 Dart_ExitScope();
176 }
177
178 void GLCreateShader(Dart_NativeArguments arguments) {
179 LOGI("GLCreateShader");
180 Dart_EnterScope();
181
182 Dart_Handle typeHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
183 int64_t type;
184 HandleError(Dart_IntegerToInt64(typeHandle, &type));
185
186 int64_t shader = glCreateShader(type);
187 CheckGLError();
188 Dart_Handle result = HandleError(Dart_NewInteger(shader));
189 Dart_SetReturnValue(arguments, result);
190 Dart_ExitScope();
191 }
192
193 void GLDrawArrays(Dart_NativeArguments arguments) {
194 LOGI("GLDrawArrays");
195 Dart_EnterScope();
196
197 Dart_Handle modeHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
198 int64_t mode;
199 HandleError(Dart_IntegerToInt64(modeHandle, &mode));
200
201 Dart_Handle firstHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
202 int64_t first;
203 HandleError(Dart_IntegerToInt64(firstHandle, &first));
204
205 Dart_Handle countHandle = HandleError(Dart_GetNativeArgument(arguments, 2));
206 int64_t count;
207 HandleError(Dart_IntegerToInt64(countHandle, &count));
208
209 glDrawArrays(mode, first, count);
210 CheckGLError();
211 Dart_ExitScope();
212 }
213
214 void GLEnableVertexAttribArray(Dart_NativeArguments arguments) {
215 LOGI("GLEnableVertexAttribArray");
216 Dart_EnterScope();
217
218 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ;
219 int64_t location;
220 HandleError(Dart_IntegerToInt64(locationHandle, &location));
221
222 glEnableVertexAttribArray(location);
223 CheckGLError();
224 Dart_ExitScope();
225 }
226
227 void GLGetAttribLocation(Dart_NativeArguments arguments) {
228 LOGI("GLGetAttribLocation");
229 Dart_EnterScope();
230
231 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
232 int64_t program;
233 HandleError(Dart_IntegerToInt64(programHandle, &program));
234
235 Dart_Handle nameHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
236 intptr_t length;
237 HandleError(Dart_StringLength(nameHandle, &length));
238 uint8_t* str;
239 str = reinterpret_cast<uint8_t*>(malloc(length+1));
240 HandleError(Dart_StringGet8(nameHandle, str, &length));
241 str[length] = 0;
242
243 int64_t location = glGetAttribLocation(program, const_cast<const GLchar*>(rein terpret_cast<GLchar*>(str)));
244 CheckGLError();
245 Dart_Handle result = HandleError(Dart_NewInteger(location));
246 Dart_SetReturnValue(arguments, result);
247 Dart_ExitScope();
248 }
249
250 void GLGetError(Dart_NativeArguments arguments) {
251 LOGI("GLGetError");
252 Dart_EnterScope();
253
254 int64_t error = glGetError();
255 Dart_Handle result = HandleError(Dart_NewInteger(error));
256 Dart_SetReturnValue(arguments, result);
257 Dart_ExitScope();
258 }
259
260 void GLGetProgramParameter(Dart_NativeArguments arguments) {
261 LOGI("GLGetProgramParameter");
262 Dart_EnterScope();
263
264 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
265 int64_t program;
266 HandleError(Dart_IntegerToInt64(programHandle, &program));
267
268 Dart_Handle paramHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
269 int64_t param;
270 HandleError(Dart_IntegerToInt64(paramHandle, &param));
271
272 GLint value;
273 glGetProgramiv(program, param, &value);
274 CheckGLError();
275
276 Dart_Handle result = HandleError(Dart_NewInteger(value));
277 Dart_SetReturnValue(arguments, result);
278 Dart_ExitScope();
279 }
280
281 void GLGetShaderParameter(Dart_NativeArguments arguments) {
282 LOGI("GLGetShaderParameter");
283 Dart_EnterScope();
284
285 Dart_Handle shaderHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
286 int64_t shader;
287 HandleError(Dart_IntegerToInt64(shaderHandle, &shader));
288
289 Dart_Handle paramHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
290 int64_t param;
291 HandleError(Dart_IntegerToInt64(paramHandle, &param));
292
293 GLint value;
294 glGetShaderiv(shader, param, &value);
295 CheckGLError();
296
297 Dart_Handle result = HandleError(Dart_NewInteger(value));
298 Dart_SetReturnValue(arguments, result);
299 Dart_ExitScope();
300 }
301
302 void GLGetUniformLocation(Dart_NativeArguments arguments) {
303 LOGI("GLGetUniformLocation");
304 Dart_EnterScope();
305
306 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
307 int64_t program;
308 HandleError(Dart_IntegerToInt64(programHandle, &program));
309
310 Dart_Handle nameHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
311 intptr_t length;
312 HandleError(Dart_StringLength(nameHandle, &length));
313 uint8_t* str;
314 str = reinterpret_cast<uint8_t*>(malloc(length+1));
315 HandleError(Dart_StringGet8(nameHandle, str, &length));
316 str[length] = 0;
317
318 int64_t location = glGetUniformLocation(program, const_cast<const GLchar*>(rei nterpret_cast<GLchar*>(str)));
319 CheckGLError();
320 Dart_Handle result = HandleError(Dart_NewInteger(location));
321 Dart_SetReturnValue(arguments, result);
322 Dart_ExitScope();
323 }
324
325 void GLLinkProgram(Dart_NativeArguments arguments) {
326 LOGI("GLLinkProgram");
327 Dart_EnterScope();
328
329 Dart_Handle programHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
330 int64_t program;
331 HandleError(Dart_IntegerToInt64(programHandle, &program));
332
333 glLinkProgram(program);
334 CheckGLError();
335 Dart_ExitScope();
336 }
337
338 void GLShaderSource(Dart_NativeArguments arguments) {
339 LOGI("GLShaderSource");
340 Dart_EnterScope();
341
342 Dart_Handle shaderHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
343 int64_t shader;
344 HandleError(Dart_IntegerToInt64(shaderHandle, &shader));
345
346 Dart_Handle sourceHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
347 intptr_t length[1];
348 HandleError(Dart_StringLength(sourceHandle, length));
349 LOGI("Source length is %d", length[0]);
350 uint8_t* str[1];
351 str[0] = reinterpret_cast<uint8_t*>(malloc(length[0]+1));
352 HandleError(Dart_StringGet8(sourceHandle, str[0], length));
353 LOGI("Converted length is %d", length[0]);
354 str[0][*length] = 0;
355
356 LOGI("Source: %s", const_cast<const GLchar*>(reinterpret_cast<GLchar*>(str[0]) ));
357 glShaderSource(shader, 1, const_cast<const GLchar**>(reinterpret_cast<GLchar** >(str)), NULL);
358 CheckGLError();
359 Dart_ExitScope();
360 }
361
362 void GLUseProgram(Dart_NativeArguments arguments) {
363 LOGI("GLUseProgram");
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 glUseProgram(program);
371 CheckGLError();
372 Dart_ExitScope();
373 }
374
375 void GLUniform3f(Dart_NativeArguments arguments) {
376 LOGI("GLUniform3f");
377 Dart_EnterScope();
378
379 Dart_Handle locationHandle = HandleError(Dart_GetNativeArgument(arguments, 0)) ;
380 int64_t location;
381 HandleError(Dart_IntegerToInt64(locationHandle, &location));
382
383 Dart_Handle v0Handle = HandleError(Dart_GetNativeArgument(arguments, 1));
384 double v0;
385 HandleError(Dart_DoubleValue(v0Handle, &v0));
386
387 Dart_Handle v1Handle = HandleError(Dart_GetNativeArgument(arguments, 2));
388 double v1;
389 HandleError(Dart_DoubleValue(v1Handle, &v1));
390
391 Dart_Handle v2Handle = HandleError(Dart_GetNativeArgument(arguments, 3));
392 double v2;
393 HandleError(Dart_DoubleValue(v2Handle, &v2));
394
395 glUniform3f(location, v0, v1, v2);
396 CheckGLError();
397 Dart_ExitScope();
398 }
399
400 void GLViewport(Dart_NativeArguments arguments) {
401 LOGI("GLViewport");
402 Dart_EnterScope();
403
404 Dart_Handle xHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
405 int64_t x;
406 HandleError(Dart_IntegerToInt64(xHandle, &x));
407
408 Dart_Handle yHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
409 int64_t y;
410 HandleError(Dart_IntegerToInt64(yHandle, &y));
411
412 Dart_Handle widthHandle = HandleError(Dart_GetNativeArgument(arguments, 2));
413 int64_t width;
414 HandleError(Dart_IntegerToInt64(widthHandle, &width));
415
416 Dart_Handle heightHandle = HandleError(Dart_GetNativeArgument(arguments, 3));
417 int64_t height;
418 HandleError(Dart_IntegerToInt64(heightHandle, &height));
419
420 LOGI("Dimensions: [%d, %d, %d, %d]", (int) x, (int)y, (int) width, (int) heigh t);
421
422 glViewport((int) x, (int) y, (int) width, (int) height);
423 CheckGLError();
424 Dart_ExitScope();
425 }
426
427 void GLVertexAttribPointer(Dart_NativeArguments arguments) {
428 LOGI("GLVertexAttribPointer");
429 Dart_EnterScope();
430
431 Dart_Handle indexHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
432 int64_t index;
433 HandleError(Dart_IntegerToInt64(indexHandle, &index));
434
435 Dart_Handle sizeHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
436 int64_t size;
437 HandleError(Dart_IntegerToInt64(sizeHandle, &size));
438
439 Dart_Handle typeHandle = HandleError(Dart_GetNativeArgument(arguments, 2));
440 int64_t type;
441 HandleError(Dart_IntegerToInt64(typeHandle, &type));
442
443 Dart_Handle normalizedHandle = HandleError(Dart_GetNativeArgument(arguments, 3 ));
444 bool normalized;
445 HandleError(Dart_BooleanValue(normalizedHandle, &normalized));
446
447 Dart_Handle strideHandle = HandleError(Dart_GetNativeArgument(arguments, 4));
448 int64_t stride;
449 HandleError(Dart_IntegerToInt64(strideHandle, &stride));
450
451 Dart_Handle pointerHandle = HandleError(Dart_GetNativeArgument(arguments, 5));
452 int64_t pointerValue;
453 HandleError(Dart_IntegerToInt64(pointerHandle, &pointerValue));
454 const void* pointer;
455 pointer = const_cast<const void*>(reinterpret_cast<void*>(pointerValue));
456
457 glVertexAttribPointer(index, (int) size, (int) type, normalized, (int) stride, pointer);
458 CheckGLError();
459 Dart_ExitScope();
460 }
461
462 void GLClearColor(Dart_NativeArguments arguments) {
463 Dart_EnterScope();
464
465 Dart_Handle redHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
466 double red;
467 HandleError(Dart_DoubleValue(redHandle, &red));
468
469 Dart_Handle greenHandle = HandleError(Dart_GetNativeArgument(arguments, 1));
470 double green;
471 HandleError(Dart_DoubleValue(greenHandle, &green));
472
473 Dart_Handle blueHandle = HandleError(Dart_GetNativeArgument(arguments, 2));
474 double blue;
475 HandleError(Dart_DoubleValue(blueHandle, &blue));
476
477 Dart_Handle alphaHandle = HandleError(Dart_GetNativeArgument(arguments, 3));
478 double alpha;
479 HandleError(Dart_DoubleValue(alphaHandle, &alpha));
480
481 glClearColor(red, green, blue, alpha);
482 CheckGLError();
483 Dart_ExitScope();
484 }
485
486 void GLClearDepth(Dart_NativeArguments arguments) {
487 Dart_EnterScope();
488
489 Dart_Handle depthHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
490 double depth;
491 HandleError(Dart_DoubleValue(depthHandle, &depth));
492
493 glClearDepthf(depth);
494 CheckGLError();
495 Dart_ExitScope();
496 }
497
498 void GLClear(Dart_NativeArguments arguments) {
499 Dart_EnterScope();
500 Dart_Handle maskHandle = HandleError(Dart_GetNativeArgument(arguments, 0));
501 int64_t mask;
502 HandleError(Dart_IntegerToInt64(maskHandle, &mask));
503 glClear(mask);
504 CheckGLError();
505 Dart_ExitScope();
506 }
507
508 void GLArrayBuffer(Dart_NativeArguments arguments) {
509 Dart_EnterScope();
510 Dart_Handle result = HandleError(Dart_NewInteger(GL_ARRAY_BUFFER));
511 Dart_SetReturnValue(arguments, result);
512 Dart_ExitScope();
513 }
514
515 void GLColorBufferBit(Dart_NativeArguments arguments) {
516 Dart_EnterScope();
517 Dart_Handle result = HandleError(Dart_NewInteger(GL_COLOR_BUFFER_BIT));
518 Dart_SetReturnValue(arguments, result);
519 Dart_ExitScope();
520 }
521
522 void GLCompileStatus(Dart_NativeArguments arguments) {
523 Dart_EnterScope();
524 Dart_Handle result = HandleError(Dart_NewInteger(GL_COMPILE_STATUS));
525 Dart_SetReturnValue(arguments, result);
526 Dart_ExitScope();
527 }
528
529 void GLDepthBufferBit(Dart_NativeArguments arguments) {
530 Dart_EnterScope();
531 Dart_Handle result = HandleError(Dart_NewInteger(GL_DEPTH_BUFFER_BIT));
532 Dart_SetReturnValue(arguments, result);
533 Dart_ExitScope();
534 }
535
536 void GLFloat(Dart_NativeArguments arguments) {
537 Dart_EnterScope();
538 Dart_Handle result = HandleError(Dart_NewInteger(GL_FLOAT));
539 Dart_SetReturnValue(arguments, result);
540 Dart_ExitScope();
541 }
542
543 void GLFragmentShader(Dart_NativeArguments arguments) {
544 Dart_EnterScope();
545 Dart_Handle result = HandleError(Dart_NewInteger(GL_FRAGMENT_SHADER));
546 Dart_SetReturnValue(arguments, result);
547 Dart_ExitScope();
548 }
549
550 void GLLinkStatus(Dart_NativeArguments arguments) {
551 Dart_EnterScope();
552 Dart_Handle result = HandleError(Dart_NewInteger(GL_LINK_STATUS));
553 Dart_SetReturnValue(arguments, result);
554 Dart_ExitScope();
555 }
556
557 void GLStaticDraw(Dart_NativeArguments arguments) {
558 Dart_EnterScope();
559 Dart_Handle result = HandleError(Dart_NewInteger(GL_STATIC_DRAW));
560 Dart_SetReturnValue(arguments, result);
561 Dart_ExitScope();
562 }
563
564 void GLTriangleStrip(Dart_NativeArguments arguments) {
565 Dart_EnterScope();
566 Dart_Handle result = HandleError(Dart_NewInteger(GL_TRIANGLE_STRIP));
567 Dart_SetReturnValue(arguments, result);
568 Dart_ExitScope();
569 }
570
571 void GLTrue(Dart_NativeArguments arguments) {
572 Dart_EnterScope();
573 Dart_Handle result = HandleError(Dart_NewInteger(GL_TRUE));
574 Dart_SetReturnValue(arguments, result);
575 Dart_ExitScope();
576 }
577
578 void GLVertexShader(Dart_NativeArguments arguments) {
579 Dart_EnterScope();
580 Dart_Handle result = HandleError(Dart_NewInteger(GL_VERTEX_SHADER));
581 Dart_SetReturnValue(arguments, result);
582 Dart_ExitScope();
583 }
584
585 uint8_t* randomArray(int seed, int length) {
586 if (length <= 0 || length > 10000000) return NULL;
587 uint8_t* values = reinterpret_cast<uint8_t*>(malloc(length));
588 if (NULL == values) return NULL;
589 srand(seed);
590 for (int i = 0; i < length; ++i) {
591 values[i] = rand() % 256;
592 }
593 return values;
594 }
595
596 void wrappedRandomArray(Dart_Port dest_port_id,
597 Dart_Port reply_port_id,
598 Dart_CObject* message) {
599 if (message->type == Dart_CObject::kArray &&
600 2 == message->value.as_array.length) {
601 // Use .as_array and .as_int32 to access the data in the Dart_CObject.
602 Dart_CObject* param0 = message->value.as_array.values[0];
603 Dart_CObject* param1 = message->value.as_array.values[1];
604 if (param0->type == Dart_CObject::kInt32 &&
605 param1->type == Dart_CObject::kInt32) {
606 int length = param0->value.as_int32;
607 int seed = param1->value.as_int32;
608
609 uint8_t* values = randomArray(seed, length);
610
611 if (values != NULL) {
612 Dart_CObject result;
613 result.type = Dart_CObject::kUint8Array;
614 result.value.as_byte_array.values = values;
615 result.value.as_byte_array.length = length;
616 Dart_PostCObject(reply_port_id, &result);
617 free(values);
618 // It is OK that result is destroyed when function exits.
619 // Dart_PostCObject has copied its data.
620 return;
621 }
622 }
623 }
624 Dart_CObject result;
625 result.type = Dart_CObject::kNull;
626 Dart_PostCObject(reply_port_id, &result);
627 }
628
629 void randomArrayServicePort(Dart_NativeArguments arguments) {
630 Dart_EnterScope();
631 Dart_SetReturnValue(arguments, Dart_Null());
632 Dart_Port service_port =
633 Dart_NewNativePort("RandomArrayService", wrappedRandomArray, true);
634 if (service_port != ILLEGAL_PORT) {
635 Dart_Handle send_port = HandleError(Dart_NewSendPort(service_port));
636 Dart_SetReturnValue(arguments, send_port);
637 }
638 Dart_ExitScope();
639 }
640
641
642 struct FunctionLookup {
643 const char* name;
644 Dart_NativeFunction function;
645 };
646
647 FunctionLookup function_list[] = {
648 {"SystemRand", SystemRand},
649 {"SystemSrand", SystemSrand},
650 {"GLAttachShader", GLAttachShader},
651 {"GLBindBuffer", GLBindBuffer},
652 {"GLBufferData", GLBufferData},
653 {"GLClear", GLClear},
654 {"GLClearColor", GLClearColor},
655 {"GLClearDepth", GLClearDepth},
656 {"GLCompileShader", GLCompileShader},
657 {"GLCreateBuffer", GLCreateBuffer},
658 {"GLCreateProgram", GLCreateProgram},
659 {"GLCreateShader", GLCreateShader},
660 {"GLDrawArrays", GLDrawArrays},
661 {"GLEnableVertexAttribArray", GLEnableVertexAttribArray},
662 {"GLGetAttribLocation", GLGetAttribLocation},
663 {"GLGetError", GLGetError},
664 {"GLGetProgramParameter", GLGetProgramParameter},
665 {"GLGetShaderParameter", GLGetShaderParameter},
666 {"GLGetUniformLocation", GLGetUniformLocation},
667 {"GLLinkProgram", GLLinkProgram},
668 {"GLShaderSource", GLShaderSource},
669 {"GLUniform3f", GLUniform3f},
670 {"GLUseProgram", GLUseProgram},
671 {"GLVertexAttribPointer", GLVertexAttribPointer},
672 {"GLViewport", GLViewport},
673 {"GLArrayBuffer", GLArrayBuffer},
674 {"GLColorBufferBit", GLColorBufferBit},
675 {"GLCompileStatus", GLCompileStatus},
676 {"GLDepthBufferBit", GLDepthBufferBit},
677 {"GLFloat", GLFloat},
678 {"GLFragmentShader", GLFragmentShader},
679 {"GLLinkStatus", GLLinkStatus},
680 {"GLTriangleStrip", GLTriangleStrip},
681 {"GLTrue", GLTrue},
682 {"GLStaticDraw", GLStaticDraw},
683 {"GLVertexShader", GLVertexShader},
684 {"RandomArray_ServicePort", randomArrayServicePort},
685 {NULL, NULL}};
686
687 Dart_NativeFunction ResolveName(Dart_Handle name, int argc) {
688 if (!Dart_IsString8(name)) return NULL;
689 Dart_NativeFunction result = NULL;
690 Dart_EnterScope();
691 const char* cname;
692 HandleError(Dart_StringToCString(name, &cname));
693 for (int i=0; function_list[i].name != NULL; ++i) {
694 if (strcmp(function_list[i].name, cname) == 0) {
695 result = function_list[i].function;
696 break;
697 }
698 }
699 Dart_ExitScope();
700 return result;
701 }
OLDNEW
« no previous file with comments | « samples/android/android.gyp ('k') | samples/android/ant.properties » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698