blob: 4af58a67d2b6ecfa92e912d8202ac2de7ba6dfbc [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <stdio.h>
19#include <fcntl.h>
20#include <unistd.h>
21#include <math.h>
22
23#include <utils/misc.h>
24#include <utils/Log.h>
25
26#include <ui/EGLNativeWindowSurface.h>
27#include <ui/Surface.h>
28
29#include "jni.h"
30#include "JNIHelp.h"
31#include "android_runtime/AndroidRuntime.h"
32
33#include "../RenderScript.h"
34#include "../RenderScriptEnv.h"
35
Jack Palevichfd19dc92009-05-26 18:58:04 -070036#include "acc/acc.h"
Jack Palevichfd19dc92009-05-26 18:58:04 -070037
Jason Sams326e0dd2009-05-22 14:03:28 -070038//#define LOG_API LOGE
39#define LOG_API(...)
40
41using namespace android;
42
Jason Sams326e0dd2009-05-22 14:03:28 -070043// ---------------------------------------------------------------------------
44
45static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
46{
47 jclass npeClazz = env->FindClass(exc);
48 env->ThrowNew(npeClazz, msg);
49}
50
51static jfieldID gContextId;
52
53static void _nInit(JNIEnv *_env, jclass _this)
54{
55 LOGE("_nInit");
56 gContextId = _env->GetFieldID(_this, "mContext", "I");
57}
58
59
60// ---------------------------------------------------------------------------
61
62static jint
63nDeviceCreate(JNIEnv *_env, jobject _this)
64{
65 LOG_API("nDeviceCreate");
66 return (jint)rsDeviceCreate();
67}
68
69static void
70nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev)
71{
72 LOG_API("nDeviceDestroy");
73 return rsDeviceDestroy((RsDevice)dev);
74}
75
76static jint
77nContextCreate(JNIEnv *_env, jobject _this, jint dev, jobject wnd, jint ver)
78{
79 LOG_API("nContextCreate");
80
81 if (wnd == NULL) {
82 not_valid_surface:
83 doThrow(_env, "java/lang/IllegalArgumentException",
84 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
85 return 0;
86 }
87 jclass surface_class = _env->FindClass("android/view/Surface");
88 jfieldID surfaceFieldID = _env->GetFieldID(surface_class, "mSurface", "I");
89 Surface * window = (Surface*)_env->GetIntField(wnd, surfaceFieldID);
90 if (window == NULL)
91 goto not_valid_surface;
92
93 LOGE("nContextCreate 5");
94 return (jint)rsContextCreate((RsDevice)dev, window, ver);
95}
96
97static void
98nContextDestroy(JNIEnv *_env, jobject _this, jint con)
99{
100 LOG_API("nContextDestroy, con(%p)", (RsContext)con);
101 return rsContextDestroy((RsContext)con);
102}
103
104
105static void
106nElementBegin(JNIEnv *_env, jobject _this)
107{
108 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
109 LOG_API("nElementBegin, con(%p)", con);
110 rsElementBegin();
111}
112
113static void
114nElementAddPredefined(JNIEnv *_env, jobject _this, jint predef)
115{
116 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
117 LOG_API("nElementAddPredefined, con(%p), predef(%i)", con, predef);
118 rsElementAddPredefined((RsElementPredefined)predef);
119}
120
121static void
122nElementAdd(JNIEnv *_env, jobject _this, jint kind, jint type, jint norm, jint bits)
123{
124 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
125 LOG_API("nElementAdd, con(%p), kind(%i), type(%i), norm(%i), bits(%i)", con, kind, type, norm, bits);
126 rsElementAdd((RsDataKind)kind, (RsDataType)type, norm != 0, (size_t)bits);
127}
128
129static jint
130nElementCreate(JNIEnv *_env, jobject _this)
131{
132 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
133 LOG_API("nElementCreate, con(%p)", con);
134 return (jint)rsElementCreate();
135}
136
137static jint
138nElementGetPredefined(JNIEnv *_env, jobject _this, jint predef)
139{
140 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
141 LOG_API("nElementGetPredefined, con(%p) predef(%i)", con, predef);
142 return (jint)rsElementGetPredefined((RsElementPredefined)predef);
143}
144
145static void
146nElementDestroy(JNIEnv *_env, jobject _this, jint e)
147{
148 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
149 LOG_API("nElementDestroy, con(%p) e(%p)", con, (RsElement)e);
150 rsElementDestroy((RsElement)e);
151}
152
153// -----------------------------------
154
155static void
156nTypeBegin(JNIEnv *_env, jobject _this, jint eID)
157{
158 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
159 LOG_API("nTypeBegin, con(%p) e(%p)", con, (RsElement)eID);
160 rsTypeBegin((RsElement)eID);
161}
162
163static void
164nTypeAdd(JNIEnv *_env, jobject _this, jint dim, jint val)
165{
166 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
167 LOG_API("nTypeAdd, con(%p) dim(%i), val(%i)", con, dim, val);
168 rsTypeAdd((RsDimension)dim, val);
169}
170
171static jint
172nTypeCreate(JNIEnv *_env, jobject _this)
173{
174 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
175 LOG_API("nTypeCreate, con(%p)", con);
176 return (jint)rsTypeCreate();
177}
178
179static void
180nTypeDestroy(JNIEnv *_env, jobject _this, jint eID)
181{
182 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
183 LOG_API("nTypeDestroy, con(%p), t(%p)", con, (RsType)eID);
184 rsTypeDestroy((RsType)eID);
185}
186
187// -----------------------------------
188
189static jint
190nAllocationCreateTyped(JNIEnv *_env, jobject _this, jint e)
191{
192 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
193 LOG_API("nAllocationCreateTyped, con(%p), e(%p)", con, (RsElement)e);
194 return (jint) rsAllocationCreateTyped((RsElement)e);
195}
196
197static jint
198nAllocationCreatePredefSized(JNIEnv *_env, jobject _this, jint predef, jint count)
199{
200 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
201 LOG_API("nAllocationCreatePredefSized, con(%p), predef(%i), count(%i)", con, predef, count);
202 return (jint) rsAllocationCreatePredefSized((RsElementPredefined)predef, count);
203}
204
205static jint
206nAllocationCreateSized(JNIEnv *_env, jobject _this, jint e, jint count)
207{
208 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
209 LOG_API("nAllocationCreateSized, con(%p), e(%p), count(%i)", con, (RsElement)e, count);
210 return (jint) rsAllocationCreateSized((RsElement)e, count);
211}
212
213static void
214nAllocationUploadToTexture(JNIEnv *_env, jobject _this, jint a, jint mip)
215{
216 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
217 LOG_API("nAllocationUploadToTexture, con(%p), a(%p), mip(%i)", con, (RsAllocation)a, mip);
218 rsAllocationUploadToTexture((RsAllocation)a, mip);
219}
220
Jason Sams6678e9b2009-05-27 14:45:32 -0700221static int
222nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jint w, jint h, jint dstFmt, jint srcFmt, jboolean genMips, jintArray data)
223{
224 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
225 jint len = _env->GetArrayLength(data);
226 LOG_API("nAllocationCreateFromBitmap, con(%p), w(%i), h(%i), dstFmt(%i), srcFmt(%i), mip(%i), len(%i)", con, w, h, dstFmt, srcFmt, genMips, len);
227
228 jint *ptr = _env->GetIntArrayElements(data, NULL);
229 jint id = (jint)rsAllocationCreateFromBitmap(w, h, (RsElementPredefined)dstFmt, (RsElementPredefined)srcFmt, genMips, ptr);
230 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
231 return id;
232}
233
234
235
Jason Sams326e0dd2009-05-22 14:03:28 -0700236static void
237nAllocationDestroy(JNIEnv *_env, jobject _this, jint a)
238{
239 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
240 LOG_API("nAllocationDestroy, con(%p), a(%p)", con, (RsAllocation)a);
241 rsAllocationDestroy((RsAllocation)a);
242}
243
244static void
245nAllocationData_i(JNIEnv *_env, jobject _this, jint alloc, jintArray data)
246{
247 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
248 jint len = _env->GetArrayLength(data);
249 LOG_API("nAllocationData_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
250 jint *ptr = _env->GetIntArrayElements(data, NULL);
251 rsAllocationData((RsAllocation)alloc, ptr);
252 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
253}
254
255static void
256nAllocationData_f(JNIEnv *_env, jobject _this, jint alloc, jfloatArray data)
257{
258 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
259 jint len = _env->GetArrayLength(data);
260 LOG_API("nAllocationData_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
261 jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
262 rsAllocationData((RsAllocation)alloc, ptr);
263 _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
264}
265
266static void
267nAllocationSubData1D_i(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jintArray data)
268{
269 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
270 jint len = _env->GetArrayLength(data);
271 LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAllocation)alloc, offset, count, len);
272 jint *ptr = _env->GetIntArrayElements(data, NULL);
273 rsAllocation1DSubData((RsAllocation)alloc, offset, count, ptr);
274 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
275}
276
277static void
278nAllocationSubData1D_f(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jfloatArray data)
279{
280 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
281 jint len = _env->GetArrayLength(data);
282 LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAllocation)alloc, offset, count, len);
283 jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
284 rsAllocation1DSubData((RsAllocation)alloc, offset, count, ptr);
285 _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
286}
287
288static void
289nAllocationSubData2D_i(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jintArray data)
290{
291 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
292 jint len = _env->GetArrayLength(data);
293 LOG_API("nAllocation2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len);
294 jint *ptr = _env->GetIntArrayElements(data, NULL);
295 rsAllocation2DSubData((RsAllocation)alloc, xoff, yoff, w, h, ptr);
296 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
297}
298
299static void
300nAllocationSubData2D_f(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jfloatArray data)
301{
302 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
303 jint len = _env->GetArrayLength(data);
304 LOG_API("nAllocation2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len);
305 jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
306 rsAllocation2DSubData((RsAllocation)alloc, xoff, yoff, w, h, ptr);
307 _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
308}
309
310
311
312// -----------------------------------
313
314static void
315nTriangleMeshDestroy(JNIEnv *_env, jobject _this, jint tm)
316{
317 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
318 LOG_API("nTriangleMeshDestroy, con(%p), tm(%p)", con, (RsAllocation)tm);
319 rsTriangleMeshDestroy((RsTriangleMesh)tm);
320}
321
322static void
323nTriangleMeshBegin(JNIEnv *_env, jobject _this, jint v, jint i)
324{
325 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
326 LOG_API("nTriangleMeshBegin, con(%p), vertex(%p), index(%p)", con, (RsElement)v, (RsElement)i);
327 rsTriangleMeshBegin((RsElement)v, (RsElement)i);
328}
329
330static void
331nTriangleMeshAddVertex_XY(JNIEnv *_env, jobject _this, jfloat x, jfloat y)
332{
333 float v[] = {x, y};
334 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
335 LOG_API("nTriangleMeshAddVertex_XY, con(%p), x(%f), y(%f)", con, x, y);
336 rsTriangleMeshAddVertex(v);
337}
338
339static void
340nTriangleMeshAddVertex_XYZ(JNIEnv *_env, jobject _this, jfloat x, jfloat y, jfloat z)
341{
342 float v[] = {x, y, z};
343 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
344 LOG_API("nTriangleMeshAddVertex_XYZ, con(%p), x(%f), y(%f), z(%f)", con, x, y, z);
345 rsTriangleMeshAddVertex(v);
346}
347
348static void
349nTriangleMeshAddVertex_XY_ST(JNIEnv *_env, jobject _this, jfloat x, jfloat y, jfloat s, jfloat t)
350{
351 float v[] = {s, t, x, y};
352 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
353 LOG_API("nTriangleMeshAddVertex_XY_ST, con(%p), x(%f), y(%f), s(%f), t(%f)", con, x, y, s, t);
354 rsTriangleMeshAddVertex(v);
355}
356
357static void
358nTriangleMeshAddVertex_XYZ_ST(JNIEnv *_env, jobject _this, jfloat x, jfloat y, jfloat z, jfloat s, jfloat t)
359{
360 float v[] = {s, t, x, y, z};
361 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
362 LOG_API("nTriangleMeshAddVertex_XYZ_ST, con(%p), x(%f), y(%f), z(%f), s(%f), t(%f)", con, x, y, z, s, t);
363 rsTriangleMeshAddVertex(v);
364}
365
366static void
367nTriangleMeshAddTriangle(JNIEnv *_env, jobject _this, jint i1, jint i2, jint i3)
368{
369 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
370 LOG_API("nTriangleMeshAddTriangle, con(%p), i1(%i), i2(%i), i3(%i)", con, i1, i2, i3);
371 rsTriangleMeshAddTriangle(i1, i2, i3);
372}
373
374static jint
375nTriangleMeshCreate(JNIEnv *_env, jobject _this)
376{
377 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
378 LOG_API("nTriangleMeshCreate, con(%p)", con);
379 return (jint) rsTriangleMeshCreate();
380}
381
382// -----------------------------------
383
384static void
385nAdapter1DDestroy(JNIEnv *_env, jobject _this, jint adapter)
386{
387 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
388 LOG_API("nAdapter1DDestroy, con(%p), adapter(%p)", con, (RsAdapter1D)adapter);
389 rsAdapter1DDestroy((RsAdapter1D)adapter);
390}
391
392static void
393nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, jint adapter, jint alloc)
394{
395 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
396 LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc);
397 rsAdapter1DBindAllocation((RsAdapter1D)adapter, (RsAllocation)alloc);
398}
399
400static void
401nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, jint adapter, jint dim, jint value)
402{
403 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
404 LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value);
405 rsAdapter1DSetConstraint((RsAdapter1D)adapter, (RsDimension)dim, value);
406}
407
408static void
409nAdapter1DData_i(JNIEnv *_env, jobject _this, jint adapter, jintArray data)
410{
411 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
412 jint len = _env->GetArrayLength(data);
413 LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
414 jint *ptr = _env->GetIntArrayElements(data, NULL);
415 rsAdapter1DData((RsAdapter1D)adapter, ptr);
416 _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
417}
418
419static void
420nAdapter1DSubData_i(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jintArray data)
421{
422 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
423 jint len = _env->GetArrayLength(data);
424 LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
425 jint *ptr = _env->GetIntArrayElements(data, NULL);
426 rsAdapter1DSubData((RsAdapter1D)adapter, offset, count, ptr);
427 _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
428}
429
430static void
431nAdapter1DData_f(JNIEnv *_env, jobject _this, jint adapter, jfloatArray data)
432{
433 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
434 jint len = _env->GetArrayLength(data);
435 LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
436 jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
437 rsAdapter1DData((RsAdapter1D)adapter, ptr);
438 _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
439}
440
441static void
442nAdapter1DSubData_f(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jfloatArray data)
443{
444 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
445 jint len = _env->GetArrayLength(data);
446 LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
447 jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
448 rsAdapter1DSubData((RsAdapter1D)adapter, offset, count, ptr);
449 _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
450}
451
452static jint
453nAdapter1DCreate(JNIEnv *_env, jobject _this)
454{
455 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
456 LOG_API("nAdapter1DCreate, con(%p)", con);
457 return (jint)rsAdapter1DCreate();
458}
459
460// -----------------------------------
461
462static void
463nScriptDestroy(JNIEnv *_env, jobject _this, jint script)
464{
465 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
466 LOG_API("nScriptDestroy, con(%p), script(%p)", con, (RsScript)script);
467 rsScriptDestroy((RsScript)script);
468}
469
470static void
471nScriptBindAllocation(JNIEnv *_env, jobject _this, jint script, jint alloc, jint slot)
472{
473 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
474 LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
475 rsScriptBindAllocation((RsScript)script, (RsAllocation)alloc, slot);
476}
477
478static void
479nScriptCBegin(JNIEnv *_env, jobject _this)
480{
481 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
482 LOG_API("nScriptCBegin, con(%p)", con);
483 rsScriptCBegin();
484}
485
486static void
487nScriptCSetClearColor(JNIEnv *_env, jobject _this, jfloat r, jfloat g, jfloat b, jfloat a)
488{
489 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
490 LOG_API("nScriptCSetClearColor, con(%p), r(%f), g(%f), b(%f), a(%f)", con, r, g, b, a);
491 rsScriptCSetClearColor(r, g, b, a);
492}
493
494static void
495nScriptCSetClearDepth(JNIEnv *_env, jobject _this, jfloat d)
496{
497 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
498 LOG_API("nScriptCSetClearColor, con(%p), depth(%f)", con, d);
499 rsScriptCSetClearDepth(d);
500}
501
502static void
503nScriptCSetClearStencil(JNIEnv *_env, jobject _this, jint stencil)
504{
505 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
506 LOG_API("nScriptCSetClearStencil, con(%p), stencil(%i)", con, stencil);
507 rsScriptCSetClearStencil(stencil);
508}
509
510static void
511nScriptCAddType(JNIEnv *_env, jobject _this, jint type)
512{
513 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
514 LOG_API("nScriptCAddType, con(%p), type(%p)", con, (RsType)type);
515 rsScriptCAddType((RsType)type);
516}
517
518static void
519nScriptCSetRoot(JNIEnv *_env, jobject _this, jboolean isRoot)
520{
521 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
522 LOG_API("nScriptCSetRoot, con(%p), isRoot(%i)", con, isRoot);
523 rsScriptCSetRoot(isRoot);
524}
525
526static void
Jack Paleviched44df22009-05-28 13:38:16 -0700527nScriptCSetScript(JNIEnv *_env, jobject _this, jbyteArray scriptRef,
528 jint offset, jint length)
Jason Sams326e0dd2009-05-22 14:03:28 -0700529{
530 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
531 LOG_API("!!! nScriptCSetScript, con(%p)", con);
Jack Paleviched44df22009-05-28 13:38:16 -0700532 jint _exception = 0;
533 jint remaining;
534 jbyte* script_base = 0;
535 jbyte* script_ptr;
Jack Palevich1ef8b802009-05-28 15:53:04 -0700536 ACCscript* script = 0;
Jack Paleviched44df22009-05-28 13:38:16 -0700537 void* scriptEntry = 0;
538 if (!scriptRef) {
539 _exception = 1;
540 //_env->ThrowNew(IAEClass, "script == null");
541 goto exit;
542 }
543 if (offset < 0) {
544 _exception = 1;
545 //_env->ThrowNew(IAEClass, "offset < 0");
546 goto exit;
547 }
548 if (length < 0) {
549 _exception = 1;
550 //_env->ThrowNew(IAEClass, "length < 0");
551 goto exit;
552 }
553 remaining = _env->GetArrayLength(scriptRef) - offset;
554 if (remaining < length) {
555 _exception = 1;
556 //_env->ThrowNew(IAEClass, "length > script.length - offset");
557 goto exit;
558 }
559 script_base = (jbyte *)
560 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
561 script_ptr = script_base + offset;
562
563 {
Jack Palevich1ef8b802009-05-28 15:53:04 -0700564 script = accCreateScript();
Jack Paleviched44df22009-05-28 13:38:16 -0700565 const char* scriptSource[] = {(const char*) script_ptr};
566 int scriptLength[] = {length} ;
567 accScriptSource(script, 1, scriptSource, scriptLength);
568 accCompileScript(script);
569 accGetScriptLabel(script, "main", (ACCvoid**) &scriptEntry);
Jack Paleviched44df22009-05-28 13:38:16 -0700570 }
571 if (scriptEntry) {
Jack Palevich1ef8b802009-05-28 15:53:04 -0700572 rsScriptCSetScript((void*) script, (void *)scriptEntry);
573 script = 0;
Jack Paleviched44df22009-05-28 13:38:16 -0700574 }
575exit:
Jack Palevich1ef8b802009-05-28 15:53:04 -0700576 if (script) {
577 accDeleteScript(script);
578 }
Jack Paleviched44df22009-05-28 13:38:16 -0700579 if (script_base) {
580 _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
581 _exception ? JNI_ABORT: 0);
582 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700583}
584
585static jint
586nScriptCCreate(JNIEnv *_env, jobject _this)
587{
588 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
589 LOG_API("nScriptCCreate, con(%p)", con);
590 return (jint)rsScriptCCreate();
591}
592
593// ---------------------------------------------------------------------------
594
595static void
596nProgramFragmentStoreBegin(JNIEnv *_env, jobject _this, jint in, jint out)
597{
598 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
599 LOG_API("nProgramFragmentStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
600 rsProgramFragmentStoreBegin((RsElement)in, (RsElement)out);
601}
602
603static void
604nProgramFragmentStoreDepthFunc(JNIEnv *_env, jobject _this, jint func)
605{
606 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
607 LOG_API("nProgramFragmentStoreDepthFunc, con(%p), func(%i)", con, func);
608 rsProgramFragmentStoreDepthFunc((RsDepthFunc)func);
609}
610
611static void
612nProgramFragmentStoreDepthMask(JNIEnv *_env, jobject _this, jboolean enable)
613{
614 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
615 LOG_API("nProgramFragmentStoreDepthMask, con(%p), enable(%i)", con, enable);
616 rsProgramFragmentStoreDepthMask(enable);
617}
618
619static void
620nProgramFragmentStoreColorMask(JNIEnv *_env, jobject _this, jboolean r, jboolean g, jboolean b, jboolean a)
621{
622 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
623 LOG_API("nProgramFragmentStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
624 rsProgramFragmentStoreColorMask(r, g, b, a);
625}
626
627static void
628nProgramFragmentStoreBlendFunc(JNIEnv *_env, jobject _this, int src, int dst)
629{
630 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
631 LOG_API("nProgramFragmentStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
632 rsProgramFragmentStoreBlendFunc((RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
633}
634
635static void
636nProgramFragmentStoreDither(JNIEnv *_env, jobject _this, jboolean enable)
637{
638 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
639 LOG_API("nProgramFragmentStoreDither, con(%p), enable(%i)", con, enable);
640 rsProgramFragmentStoreDither(enable);
641}
642
643static jint
644nProgramFragmentStoreCreate(JNIEnv *_env, jobject _this)
645{
646 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
647 LOG_API("nProgramFragmentStoreCreate, con(%p)", con);
648 return (jint)rsProgramFragmentStoreCreate();
649}
650
651// ---------------------------------------------------------------------------
652
653static void
654nProgramFragmentBegin(JNIEnv *_env, jobject _this, jint in, jint out)
655{
656 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
657 LOG_API("nProgramFragmentBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
658 rsProgramFragmentBegin((RsElement)in, (RsElement)out);
659}
660
661static void
662nProgramFragmentBindTexture(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
663{
664 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
665 LOG_API("nProgramFragmentBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
666 rsProgramFragmentBindTexture((RsProgramFragment)vpf, slot, (RsAllocation)a);
667}
668
669static void
670nProgramFragmentBindSampler(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
671{
672 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
673 LOG_API("nProgramFragmentBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
674 rsProgramFragmentBindSampler((RsProgramFragment)vpf, slot, (RsSampler)a);
675}
676
677static void
678nProgramFragmentSetType(JNIEnv *_env, jobject _this, jint slot, jint vt)
679{
680 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
681 LOG_API("nProgramFragmentSetType, con(%p), slot(%i), vt(%p)", con, slot, (RsType)vt);
682 rsProgramFragmentSetType(slot, (RsType)vt);
683}
684
685static void
686nProgramFragmentSetEnvMode(JNIEnv *_env, jobject _this, jint slot, jint env)
687{
688 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
689 LOG_API("nProgramFragmentSetEnvMode, con(%p), slot(%i), vt(%i)", con, slot, env);
690 rsProgramFragmentSetEnvMode(slot, (RsTexEnvMode)env);
691}
692
693static void
694nProgramFragmentSetTexEnable(JNIEnv *_env, jobject _this, jint slot, jboolean enable)
695{
696 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
697 LOG_API("nProgramFragmentSetTexEnable, con(%p), slot(%i), enable(%i)", con, slot, enable);
698 rsProgramFragmentSetTexEnable(slot, enable);
699}
700
701static jint
702nProgramFragmentCreate(JNIEnv *_env, jobject _this, jint slot, jboolean enable)
703{
704 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
705 LOG_API("nProgramFragmentCreate, con(%p)", con);
706 return (jint)rsProgramFragmentCreate();
707}
708
709
710// ---------------------------------------------------------------------------
711
712static void
713nContextBindRootScript(JNIEnv *_env, jobject _this, jint script)
714{
715 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
716 LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
717 rsContextBindRootScript((RsScript)script);
718}
719
720static void
Jason Sams326e0dd2009-05-22 14:03:28 -0700721nContextBindProgramFragmentStore(JNIEnv *_env, jobject _this, jint pfs)
722{
723 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
724 LOG_API("nContextBindProgramFragmentStore, con(%p), pfs(%p)", con, (RsProgramFragmentStore)pfs);
725 rsContextBindProgramFragmentStore((RsProgramFragmentStore)pfs);
726}
727
728static void
729nContextBindProgramFragment(JNIEnv *_env, jobject _this, jint pf)
730{
731 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
732 LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
733 rsContextBindProgramFragment((RsProgramFragment)pf);
734}
735
Jason Sams39c8bc72009-05-28 15:37:57 -0700736// ---------------------------------------------------------------------------
737
738static void
739nSamplerDestroy(JNIEnv *_env, jobject _this, jint s)
740{
741 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
742 LOG_API("nSamplerDestroy, con(%p), sampler(%p)", con, (RsSampler)s);
743 rsSamplerDestroy((RsSampler)s);
744}
745
746static void
747nSamplerBegin(JNIEnv *_env, jobject _this)
748{
749 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
750 LOG_API("nSamplerBegin, con(%p)", con);
751 rsSamplerBegin();
752}
753
754static void
755nSamplerSet(JNIEnv *_env, jobject _this, jint p, jint v)
756{
757 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
758 LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
759 rsSamplerSet((RsSamplerParam)p, (RsSamplerValue)v);
760}
761
762static jint
763nSamplerCreate(JNIEnv *_env, jobject _this)
764{
765 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
766 LOG_API("nSamplerCreate, con(%p), script(%p)", con, (RsScript)script);
767 return (jint)rsSamplerCreate();
768}
769
Jason Sams326e0dd2009-05-22 14:03:28 -0700770
771// ---------------------------------------------------------------------------
772
773
Jack Palevich7f144fe2009-05-27 17:00:45 -0700774static const char *classPathName = "com/android/fountain/RenderScript";
Jason Sams326e0dd2009-05-22 14:03:28 -0700775
776static JNINativeMethod methods[] = {
777{"_nInit", "()V", (void*)_nInit },
778{"nDeviceCreate", "()I", (void*)nDeviceCreate },
779{"nDeviceDestroy", "(I)V", (void*)nDeviceDestroy },
780{"nContextCreate", "(ILandroid/view/Surface;I)I", (void*)nContextCreate },
781{"nContextDestroy", "(I)V", (void*)nContextDestroy },
782
783{"nElementBegin", "()V", (void*)nElementBegin },
784{"nElementAddPredefined", "(I)V", (void*)nElementAddPredefined },
785{"nElementAdd", "(IIII)V", (void*)nElementAdd },
786{"nElementCreate", "()I", (void*)nElementCreate },
787{"nElementGetPredefined", "(I)I", (void*)nElementGetPredefined },
788{"nElementDestroy", "(I)V", (void*)nElementDestroy },
789
790{"nTypeBegin", "(I)V", (void*)nTypeBegin },
791{"nTypeAdd", "(II)V", (void*)nTypeAdd },
792{"nTypeCreate", "()I", (void*)nTypeCreate },
793{"nTypeDestroy", "(I)V", (void*)nTypeDestroy },
794
795{"nAllocationCreateTyped", "(I)I", (void*)nAllocationCreateTyped },
796{"nAllocationCreatePredefSized", "(II)I", (void*)nAllocationCreatePredefSized },
797{"nAllocationCreateSized", "(II)I", (void*)nAllocationCreateSized },
Jason Sams6678e9b2009-05-27 14:45:32 -0700798{"nAllocationCreateFromBitmap", "(IIIIZ[I)I", (void*)nAllocationCreateFromBitmap },
Jason Sams326e0dd2009-05-22 14:03:28 -0700799{"nAllocationUploadToTexture", "(II)V", (void*)nAllocationUploadToTexture },
800{"nAllocationDestroy", "(I)V", (void*)nAllocationDestroy },
801{"nAllocationData", "(I[I)V", (void*)nAllocationData_i },
802{"nAllocationData", "(I[F)V", (void*)nAllocationData_f },
803{"nAllocationSubData1D", "(III[I)V", (void*)nAllocationSubData1D_i },
804{"nAllocationSubData1D", "(III[F)V", (void*)nAllocationSubData1D_f },
805{"nAllocationSubData2D", "(IIIII[I)V", (void*)nAllocationSubData2D_i },
806{"nAllocationSubData2D", "(IIIII[F)V", (void*)nAllocationSubData2D_f },
807
808{"nTriangleMeshDestroy", "(I)V", (void*)nTriangleMeshDestroy },
809{"nTriangleMeshBegin", "(II)V", (void*)nTriangleMeshBegin },
810{"nTriangleMeshAddVertex_XY", "(FF)V", (void*)nTriangleMeshAddVertex_XY },
811{"nTriangleMeshAddVertex_XYZ", "(FFF)V", (void*)nTriangleMeshAddVertex_XYZ },
812{"nTriangleMeshAddVertex_XY_ST", "(FFFF)V", (void*)nTriangleMeshAddVertex_XY_ST },
813{"nTriangleMeshAddVertex_XYZ_ST", "(FFFFF)V", (void*)nTriangleMeshAddVertex_XYZ_ST },
814{"nTriangleMeshAddTriangle", "(III)V", (void*)nTriangleMeshAddTriangle },
815{"nTriangleMeshCreate", "()I", (void*)nTriangleMeshCreate },
816
817{"nAdapter1DDestroy", "(I)V", (void*)nAdapter1DDestroy },
818{"nAdapter1DBindAllocation", "(II)V", (void*)nAdapter1DBindAllocation },
819{"nAdapter1DSetConstraint", "(III)V", (void*)nAdapter1DSetConstraint },
820{"nAdapter1DData", "(I[I)V", (void*)nAdapter1DData_i },
821{"nAdapter1DSubData", "(III[I)V", (void*)nAdapter1DSubData_i },
822{"nAdapter1DData", "(I[F)V", (void*)nAdapter1DData_f },
823{"nAdapter1DSubData", "(III[F)V", (void*)nAdapter1DSubData_f },
824{"nAdapter1DCreate", "()I", (void*)nAdapter1DCreate },
825
826{"nScriptDestroy", "(I)V", (void*)nScriptDestroy },
827{"nScriptBindAllocation", "(III)V", (void*)nScriptBindAllocation },
828{"nScriptCBegin", "()V", (void*)nScriptCBegin },
829{"nScriptCSetClearColor", "(FFFF)V", (void*)nScriptCSetClearColor },
830{"nScriptCSetClearDepth", "(F)V", (void*)nScriptCSetClearDepth },
831{"nScriptCSetClearStencil", "(I)V", (void*)nScriptCSetClearStencil },
832{"nScriptCAddType", "(I)V", (void*)nScriptCAddType },
833{"nScriptCSetRoot", "(Z)V", (void*)nScriptCSetRoot },
Jack Paleviched44df22009-05-28 13:38:16 -0700834{"nScriptCSetScript", "([BII)V", (void*)nScriptCSetScript },
Jason Sams326e0dd2009-05-22 14:03:28 -0700835{"nScriptCCreate", "()I", (void*)nScriptCCreate },
836
837{"nProgramFragmentStoreBegin", "(II)V", (void*)nProgramFragmentStoreBegin },
838{"nProgramFragmentStoreDepthFunc", "(I)V", (void*)nProgramFragmentStoreDepthFunc },
839{"nProgramFragmentStoreDepthMask", "(Z)V", (void*)nProgramFragmentStoreDepthMask },
840{"nProgramFragmentStoreColorMask", "(ZZZZ)V", (void*)nProgramFragmentStoreColorMask },
841{"nProgramFragmentStoreBlendFunc", "(II)V", (void*)nProgramFragmentStoreBlendFunc },
842{"nProgramFragmentStoreDither", "(Z)V", (void*)nProgramFragmentStoreDither },
843{"nProgramFragmentStoreCreate", "()I", (void*)nProgramFragmentStoreCreate },
844
845{"nProgramFragmentBegin", "(II)V", (void*)nProgramFragmentBegin },
846{"nProgramFragmentBindTexture", "(III)V", (void*)nProgramFragmentBindTexture },
847{"nProgramFragmentBindSampler", "(III)V", (void*)nProgramFragmentBindSampler },
848{"nProgramFragmentSetType", "(II)V", (void*)nProgramFragmentSetType },
849{"nProgramFragmentSetEnvMode", "(II)V", (void*)nProgramFragmentSetEnvMode },
850{"nProgramFragmentSetTexEnable", "(IZ)V", (void*)nProgramFragmentSetTexEnable },
851{"nProgramFragmentCreate", "()I", (void*)nProgramFragmentCreate },
852
853{"nContextBindRootScript", "(I)V", (void*)nContextBindRootScript },
Jason Sams326e0dd2009-05-22 14:03:28 -0700854{"nContextBindProgramFragmentStore","(I)V", (void*)nContextBindProgramFragmentStore },
855{"nContextBindProgramFragment", "(I)V", (void*)nContextBindProgramFragment },
856
Jason Sams39c8bc72009-05-28 15:37:57 -0700857{"nSamplerDestroy", "(I)V", (void*)nSamplerDestroy },
858{"nSamplerBegin", "()V", (void*)nSamplerBegin },
859{"nSamplerSet", "(II)V", (void*)nSamplerSet },
860{"nSamplerCreate", "()I", (void*)nSamplerCreate },
861
Jason Sams326e0dd2009-05-22 14:03:28 -0700862};
863
864static int registerFuncs(JNIEnv *_env)
865{
866 return android::AndroidRuntime::registerNativeMethods(
867 _env, classPathName, methods, NELEM(methods));
868}
869
870// ---------------------------------------------------------------------------
871
872jint JNI_OnLoad(JavaVM* vm, void* reserved)
873{
874 JNIEnv* env = NULL;
875 jint result = -1;
876
877 LOGE("****************************************************\n");
878
879 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
880 LOGE("ERROR: GetEnv failed\n");
881 goto bail;
882 }
883 assert(env != NULL);
884
885 if (registerFuncs(env) < 0) {
886 LOGE("ERROR: MediaPlayer native registration failed\n");
887 goto bail;
888 }
889
890 /* success -- return valid version number */
891 result = JNI_VERSION_1_4;
892
893bail:
894 return result;
895}