blob: 61866b7482e56c322157b3d71e9a3bbb5dac2ac0 [file] [log] [blame]
Jason Samsd19f10d2009-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
Jason Samsffe9f482009-06-01 17:45:53 -070029#include <core/SkBitmap.h>
30
Jason Samsd19f10d2009-05-22 14:03:28 -070031#include "jni.h"
32#include "JNIHelp.h"
33#include "android_runtime/AndroidRuntime.h"
34
35#include "../RenderScript.h"
36#include "../RenderScriptEnv.h"
37
Jack Palevich55d45222009-05-26 18:58:04 -070038#include "acc/acc.h"
Jack Palevich55d45222009-05-26 18:58:04 -070039
Jason Samsd19f10d2009-05-22 14:03:28 -070040//#define LOG_API LOGE
41#define LOG_API(...)
42
43using namespace android;
44
Jason Samsd19f10d2009-05-22 14:03:28 -070045// ---------------------------------------------------------------------------
46
47static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
48{
49 jclass npeClazz = env->FindClass(exc);
50 env->ThrowNew(npeClazz, msg);
51}
52
Jason Samsffe9f482009-06-01 17:45:53 -070053static jfieldID gContextId = 0;
54static jfieldID gNativeBitmapID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070055
56static void _nInit(JNIEnv *_env, jclass _this)
57{
Jason Samsd19f10d2009-05-22 14:03:28 -070058 gContextId = _env->GetFieldID(_this, "mContext", "I");
Jason Samsffe9f482009-06-01 17:45:53 -070059
60 jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
61 gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I");
Jason Samsd19f10d2009-05-22 14:03:28 -070062}
63
64
65// ---------------------------------------------------------------------------
66
67static jint
68nDeviceCreate(JNIEnv *_env, jobject _this)
69{
70 LOG_API("nDeviceCreate");
71 return (jint)rsDeviceCreate();
72}
73
74static void
75nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev)
76{
77 LOG_API("nDeviceDestroy");
78 return rsDeviceDestroy((RsDevice)dev);
79}
80
81static jint
82nContextCreate(JNIEnv *_env, jobject _this, jint dev, jobject wnd, jint ver)
83{
84 LOG_API("nContextCreate");
85
86 if (wnd == NULL) {
87 not_valid_surface:
88 doThrow(_env, "java/lang/IllegalArgumentException",
89 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
90 return 0;
91 }
92 jclass surface_class = _env->FindClass("android/view/Surface");
93 jfieldID surfaceFieldID = _env->GetFieldID(surface_class, "mSurface", "I");
94 Surface * window = (Surface*)_env->GetIntField(wnd, surfaceFieldID);
95 if (window == NULL)
96 goto not_valid_surface;
97
98 LOGE("nContextCreate 5");
99 return (jint)rsContextCreate((RsDevice)dev, window, ver);
100}
101
102static void
103nContextDestroy(JNIEnv *_env, jobject _this, jint con)
104{
105 LOG_API("nContextDestroy, con(%p)", (RsContext)con);
106 return rsContextDestroy((RsContext)con);
107}
108
109
110static void
111nElementBegin(JNIEnv *_env, jobject _this)
112{
113 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
114 LOG_API("nElementBegin, con(%p)", con);
115 rsElementBegin();
116}
117
118static void
119nElementAddPredefined(JNIEnv *_env, jobject _this, jint predef)
120{
121 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
122 LOG_API("nElementAddPredefined, con(%p), predef(%i)", con, predef);
123 rsElementAddPredefined((RsElementPredefined)predef);
124}
125
126static void
127nElementAdd(JNIEnv *_env, jobject _this, jint kind, jint type, jint norm, jint bits)
128{
129 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
130 LOG_API("nElementAdd, con(%p), kind(%i), type(%i), norm(%i), bits(%i)", con, kind, type, norm, bits);
131 rsElementAdd((RsDataKind)kind, (RsDataType)type, norm != 0, (size_t)bits);
132}
133
134static jint
135nElementCreate(JNIEnv *_env, jobject _this)
136{
137 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
138 LOG_API("nElementCreate, con(%p)", con);
139 return (jint)rsElementCreate();
140}
141
142static jint
143nElementGetPredefined(JNIEnv *_env, jobject _this, jint predef)
144{
145 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
146 LOG_API("nElementGetPredefined, con(%p) predef(%i)", con, predef);
147 return (jint)rsElementGetPredefined((RsElementPredefined)predef);
148}
149
150static void
151nElementDestroy(JNIEnv *_env, jobject _this, jint e)
152{
153 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
154 LOG_API("nElementDestroy, con(%p) e(%p)", con, (RsElement)e);
155 rsElementDestroy((RsElement)e);
156}
157
158// -----------------------------------
159
160static void
161nTypeBegin(JNIEnv *_env, jobject _this, jint eID)
162{
163 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
164 LOG_API("nTypeBegin, con(%p) e(%p)", con, (RsElement)eID);
165 rsTypeBegin((RsElement)eID);
166}
167
168static void
169nTypeAdd(JNIEnv *_env, jobject _this, jint dim, jint val)
170{
171 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
172 LOG_API("nTypeAdd, con(%p) dim(%i), val(%i)", con, dim, val);
173 rsTypeAdd((RsDimension)dim, val);
174}
175
176static jint
177nTypeCreate(JNIEnv *_env, jobject _this)
178{
179 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
180 LOG_API("nTypeCreate, con(%p)", con);
181 return (jint)rsTypeCreate();
182}
183
184static void
185nTypeDestroy(JNIEnv *_env, jobject _this, jint eID)
186{
187 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
188 LOG_API("nTypeDestroy, con(%p), t(%p)", con, (RsType)eID);
189 rsTypeDestroy((RsType)eID);
190}
191
192// -----------------------------------
193
194static jint
195nAllocationCreateTyped(JNIEnv *_env, jobject _this, jint e)
196{
197 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
198 LOG_API("nAllocationCreateTyped, con(%p), e(%p)", con, (RsElement)e);
199 return (jint) rsAllocationCreateTyped((RsElement)e);
200}
201
202static jint
203nAllocationCreatePredefSized(JNIEnv *_env, jobject _this, jint predef, jint count)
204{
205 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
206 LOG_API("nAllocationCreatePredefSized, con(%p), predef(%i), count(%i)", con, predef, count);
207 return (jint) rsAllocationCreatePredefSized((RsElementPredefined)predef, count);
208}
209
210static jint
211nAllocationCreateSized(JNIEnv *_env, jobject _this, jint e, jint count)
212{
213 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
214 LOG_API("nAllocationCreateSized, con(%p), e(%p), count(%i)", con, (RsElement)e, count);
215 return (jint) rsAllocationCreateSized((RsElement)e, count);
216}
217
218static void
219nAllocationUploadToTexture(JNIEnv *_env, jobject _this, jint a, jint mip)
220{
221 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
222 LOG_API("nAllocationUploadToTexture, con(%p), a(%p), mip(%i)", con, (RsAllocation)a, mip);
223 rsAllocationUploadToTexture((RsAllocation)a, mip);
224}
225
Jason Samsffe9f482009-06-01 17:45:53 -0700226static RsElementPredefined SkBitmapToPredefined(SkBitmap::Config cfg)
Jason Samsfe08d992009-05-27 14:45:32 -0700227{
Jason Samsffe9f482009-06-01 17:45:53 -0700228 switch (cfg) {
229 case SkBitmap::kA8_Config:
230 return RS_ELEMENT_A_8;
231 case SkBitmap::kARGB_4444_Config:
232 return RS_ELEMENT_RGBA_4444;
233 case SkBitmap::kARGB_8888_Config:
234 return RS_ELEMENT_RGBA_8888;
235 case SkBitmap::kRGB_565_Config:
236 return RS_ELEMENT_RGB_565;
Jason Samsfe08d992009-05-27 14:45:32 -0700237
Jason Samsffe9f482009-06-01 17:45:53 -0700238 default:
239 break;
240 }
241 // If we don't have a conversion mark it as a user type.
242 LOGE("Unsupported bitmap type");
243 return RS_ELEMENT_USER_U8;
Jason Samsfe08d992009-05-27 14:45:32 -0700244}
245
Jason Samsffe9f482009-06-01 17:45:53 -0700246static int
247nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
248{
249 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
250 SkBitmap const * nativeBitmap =
251 (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
252 const SkBitmap& bitmap(*nativeBitmap);
253 SkBitmap::Config config = bitmap.getConfig();
254
255 RsElementPredefined e = SkBitmapToPredefined(config);
256
257 if (e != RS_ELEMENT_USER_U8) {
258 bitmap.lockPixels();
259 const int w = bitmap.width();
260 const int h = bitmap.height();
261 const void* ptr = bitmap.getPixels();
262 jint id = (jint)rsAllocationCreateFromBitmap(w, h, (RsElementPredefined)dstFmt, e, genMips, ptr);
263 bitmap.unlockPixels();
264 return id;
265 }
266 return 0;
267}
Jason Samsfe08d992009-05-27 14:45:32 -0700268
269
Jason Samsd19f10d2009-05-22 14:03:28 -0700270static void
271nAllocationDestroy(JNIEnv *_env, jobject _this, jint a)
272{
273 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
274 LOG_API("nAllocationDestroy, con(%p), a(%p)", con, (RsAllocation)a);
275 rsAllocationDestroy((RsAllocation)a);
276}
277
278static void
279nAllocationData_i(JNIEnv *_env, jobject _this, jint alloc, jintArray data)
280{
281 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
282 jint len = _env->GetArrayLength(data);
283 LOG_API("nAllocationData_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
284 jint *ptr = _env->GetIntArrayElements(data, NULL);
285 rsAllocationData((RsAllocation)alloc, ptr);
286 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
287}
288
289static void
290nAllocationData_f(JNIEnv *_env, jobject _this, jint alloc, jfloatArray data)
291{
292 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
293 jint len = _env->GetArrayLength(data);
294 LOG_API("nAllocationData_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
295 jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
296 rsAllocationData((RsAllocation)alloc, ptr);
297 _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
298}
299
300static void
301nAllocationSubData1D_i(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jintArray data)
302{
303 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
304 jint len = _env->GetArrayLength(data);
305 LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAllocation)alloc, offset, count, len);
306 jint *ptr = _env->GetIntArrayElements(data, NULL);
307 rsAllocation1DSubData((RsAllocation)alloc, offset, count, ptr);
308 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
309}
310
311static void
312nAllocationSubData1D_f(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jfloatArray data)
313{
314 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
315 jint len = _env->GetArrayLength(data);
316 LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAllocation)alloc, offset, count, len);
317 jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
318 rsAllocation1DSubData((RsAllocation)alloc, offset, count, ptr);
319 _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
320}
321
322static void
323nAllocationSubData2D_i(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jintArray data)
324{
325 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
326 jint len = _env->GetArrayLength(data);
327 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);
328 jint *ptr = _env->GetIntArrayElements(data, NULL);
329 rsAllocation2DSubData((RsAllocation)alloc, xoff, yoff, w, h, ptr);
330 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
331}
332
333static void
334nAllocationSubData2D_f(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jfloatArray data)
335{
336 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
337 jint len = _env->GetArrayLength(data);
338 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);
339 jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
340 rsAllocation2DSubData((RsAllocation)alloc, xoff, yoff, w, h, ptr);
341 _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
342}
343
344
345
346// -----------------------------------
347
348static void
349nTriangleMeshDestroy(JNIEnv *_env, jobject _this, jint tm)
350{
351 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
352 LOG_API("nTriangleMeshDestroy, con(%p), tm(%p)", con, (RsAllocation)tm);
353 rsTriangleMeshDestroy((RsTriangleMesh)tm);
354}
355
356static void
357nTriangleMeshBegin(JNIEnv *_env, jobject _this, jint v, jint i)
358{
359 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
360 LOG_API("nTriangleMeshBegin, con(%p), vertex(%p), index(%p)", con, (RsElement)v, (RsElement)i);
361 rsTriangleMeshBegin((RsElement)v, (RsElement)i);
362}
363
364static void
365nTriangleMeshAddVertex_XY(JNIEnv *_env, jobject _this, jfloat x, jfloat y)
366{
367 float v[] = {x, y};
368 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
369 LOG_API("nTriangleMeshAddVertex_XY, con(%p), x(%f), y(%f)", con, x, y);
370 rsTriangleMeshAddVertex(v);
371}
372
373static void
374nTriangleMeshAddVertex_XYZ(JNIEnv *_env, jobject _this, jfloat x, jfloat y, jfloat z)
375{
376 float v[] = {x, y, z};
377 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
378 LOG_API("nTriangleMeshAddVertex_XYZ, con(%p), x(%f), y(%f), z(%f)", con, x, y, z);
379 rsTriangleMeshAddVertex(v);
380}
381
382static void
383nTriangleMeshAddVertex_XY_ST(JNIEnv *_env, jobject _this, jfloat x, jfloat y, jfloat s, jfloat t)
384{
385 float v[] = {s, t, x, y};
386 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
387 LOG_API("nTriangleMeshAddVertex_XY_ST, con(%p), x(%f), y(%f), s(%f), t(%f)", con, x, y, s, t);
388 rsTriangleMeshAddVertex(v);
389}
390
391static void
392nTriangleMeshAddVertex_XYZ_ST(JNIEnv *_env, jobject _this, jfloat x, jfloat y, jfloat z, jfloat s, jfloat t)
393{
394 float v[] = {s, t, x, y, z};
395 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
396 LOG_API("nTriangleMeshAddVertex_XYZ_ST, con(%p), x(%f), y(%f), z(%f), s(%f), t(%f)", con, x, y, z, s, t);
397 rsTriangleMeshAddVertex(v);
398}
399
400static void
401nTriangleMeshAddTriangle(JNIEnv *_env, jobject _this, jint i1, jint i2, jint i3)
402{
403 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
404 LOG_API("nTriangleMeshAddTriangle, con(%p), i1(%i), i2(%i), i3(%i)", con, i1, i2, i3);
405 rsTriangleMeshAddTriangle(i1, i2, i3);
406}
407
408static jint
409nTriangleMeshCreate(JNIEnv *_env, jobject _this)
410{
411 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
412 LOG_API("nTriangleMeshCreate, con(%p)", con);
413 return (jint) rsTriangleMeshCreate();
414}
415
416// -----------------------------------
417
418static void
419nAdapter1DDestroy(JNIEnv *_env, jobject _this, jint adapter)
420{
421 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
422 LOG_API("nAdapter1DDestroy, con(%p), adapter(%p)", con, (RsAdapter1D)adapter);
423 rsAdapter1DDestroy((RsAdapter1D)adapter);
424}
425
426static void
427nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, jint adapter, jint alloc)
428{
429 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
430 LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc);
431 rsAdapter1DBindAllocation((RsAdapter1D)adapter, (RsAllocation)alloc);
432}
433
434static void
435nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, jint adapter, jint dim, jint value)
436{
437 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
438 LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value);
439 rsAdapter1DSetConstraint((RsAdapter1D)adapter, (RsDimension)dim, value);
440}
441
442static void
443nAdapter1DData_i(JNIEnv *_env, jobject _this, jint adapter, jintArray data)
444{
445 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
446 jint len = _env->GetArrayLength(data);
447 LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
448 jint *ptr = _env->GetIntArrayElements(data, NULL);
449 rsAdapter1DData((RsAdapter1D)adapter, ptr);
450 _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
451}
452
453static void
454nAdapter1DSubData_i(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jintArray data)
455{
456 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
457 jint len = _env->GetArrayLength(data);
458 LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
459 jint *ptr = _env->GetIntArrayElements(data, NULL);
460 rsAdapter1DSubData((RsAdapter1D)adapter, offset, count, ptr);
461 _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
462}
463
464static void
465nAdapter1DData_f(JNIEnv *_env, jobject _this, jint adapter, jfloatArray data)
466{
467 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
468 jint len = _env->GetArrayLength(data);
469 LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
470 jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
471 rsAdapter1DData((RsAdapter1D)adapter, ptr);
472 _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
473}
474
475static void
476nAdapter1DSubData_f(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jfloatArray data)
477{
478 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
479 jint len = _env->GetArrayLength(data);
480 LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
481 jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
482 rsAdapter1DSubData((RsAdapter1D)adapter, offset, count, ptr);
483 _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
484}
485
486static jint
487nAdapter1DCreate(JNIEnv *_env, jobject _this)
488{
489 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
490 LOG_API("nAdapter1DCreate, con(%p)", con);
491 return (jint)rsAdapter1DCreate();
492}
493
494// -----------------------------------
495
496static void
497nScriptDestroy(JNIEnv *_env, jobject _this, jint script)
498{
499 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
500 LOG_API("nScriptDestroy, con(%p), script(%p)", con, (RsScript)script);
501 rsScriptDestroy((RsScript)script);
502}
503
504static void
505nScriptBindAllocation(JNIEnv *_env, jobject _this, jint script, jint alloc, jint slot)
506{
507 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
508 LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
509 rsScriptBindAllocation((RsScript)script, (RsAllocation)alloc, slot);
510}
511
512static void
513nScriptCBegin(JNIEnv *_env, jobject _this)
514{
515 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
516 LOG_API("nScriptCBegin, con(%p)", con);
517 rsScriptCBegin();
518}
519
520static void
521nScriptCSetClearColor(JNIEnv *_env, jobject _this, jfloat r, jfloat g, jfloat b, jfloat a)
522{
523 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
524 LOG_API("nScriptCSetClearColor, con(%p), r(%f), g(%f), b(%f), a(%f)", con, r, g, b, a);
525 rsScriptCSetClearColor(r, g, b, a);
526}
527
528static void
529nScriptCSetClearDepth(JNIEnv *_env, jobject _this, jfloat d)
530{
531 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
532 LOG_API("nScriptCSetClearColor, con(%p), depth(%f)", con, d);
533 rsScriptCSetClearDepth(d);
534}
535
536static void
537nScriptCSetClearStencil(JNIEnv *_env, jobject _this, jint stencil)
538{
539 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
540 LOG_API("nScriptCSetClearStencil, con(%p), stencil(%i)", con, stencil);
541 rsScriptCSetClearStencil(stencil);
542}
543
544static void
545nScriptCAddType(JNIEnv *_env, jobject _this, jint type)
546{
547 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
548 LOG_API("nScriptCAddType, con(%p), type(%p)", con, (RsType)type);
549 rsScriptCAddType((RsType)type);
550}
551
552static void
553nScriptCSetRoot(JNIEnv *_env, jobject _this, jboolean isRoot)
554{
555 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
556 LOG_API("nScriptCSetRoot, con(%p), isRoot(%i)", con, isRoot);
557 rsScriptCSetRoot(isRoot);
558}
559
560static void
Jack Palevich43702d82009-05-28 13:38:16 -0700561nScriptCSetScript(JNIEnv *_env, jobject _this, jbyteArray scriptRef,
562 jint offset, jint length)
Jason Samsd19f10d2009-05-22 14:03:28 -0700563{
564 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
565 LOG_API("!!! nScriptCSetScript, con(%p)", con);
Jack Palevich43702d82009-05-28 13:38:16 -0700566 jint _exception = 0;
567 jint remaining;
568 jbyte* script_base = 0;
569 jbyte* script_ptr;
Jack Palevichec5a20b2009-05-28 15:53:04 -0700570 ACCscript* script = 0;
Jack Palevich43702d82009-05-28 13:38:16 -0700571 void* scriptEntry = 0;
572 if (!scriptRef) {
573 _exception = 1;
574 //_env->ThrowNew(IAEClass, "script == null");
575 goto exit;
576 }
577 if (offset < 0) {
578 _exception = 1;
579 //_env->ThrowNew(IAEClass, "offset < 0");
580 goto exit;
581 }
582 if (length < 0) {
583 _exception = 1;
584 //_env->ThrowNew(IAEClass, "length < 0");
585 goto exit;
586 }
587 remaining = _env->GetArrayLength(scriptRef) - offset;
588 if (remaining < length) {
589 _exception = 1;
590 //_env->ThrowNew(IAEClass, "length > script.length - offset");
591 goto exit;
592 }
593 script_base = (jbyte *)
594 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
595 script_ptr = script_base + offset;
596
597 {
Jack Palevichec5a20b2009-05-28 15:53:04 -0700598 script = accCreateScript();
Jack Palevich43702d82009-05-28 13:38:16 -0700599 const char* scriptSource[] = {(const char*) script_ptr};
600 int scriptLength[] = {length} ;
601 accScriptSource(script, 1, scriptSource, scriptLength);
602 accCompileScript(script);
603 accGetScriptLabel(script, "main", (ACCvoid**) &scriptEntry);
Jack Palevich43702d82009-05-28 13:38:16 -0700604 }
605 if (scriptEntry) {
Jack Palevichec5a20b2009-05-28 15:53:04 -0700606 rsScriptCSetScript((void*) script, (void *)scriptEntry);
607 script = 0;
Jack Palevich43702d82009-05-28 13:38:16 -0700608 }
609exit:
Jack Palevichec5a20b2009-05-28 15:53:04 -0700610 if (script) {
611 accDeleteScript(script);
612 }
Jack Palevich43702d82009-05-28 13:38:16 -0700613 if (script_base) {
614 _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
615 _exception ? JNI_ABORT: 0);
616 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700617}
618
619static jint
620nScriptCCreate(JNIEnv *_env, jobject _this)
621{
622 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
623 LOG_API("nScriptCCreate, con(%p)", con);
624 return (jint)rsScriptCCreate();
625}
626
627// ---------------------------------------------------------------------------
628
629static void
630nProgramFragmentStoreBegin(JNIEnv *_env, jobject _this, jint in, jint out)
631{
632 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
633 LOG_API("nProgramFragmentStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
634 rsProgramFragmentStoreBegin((RsElement)in, (RsElement)out);
635}
636
637static void
638nProgramFragmentStoreDepthFunc(JNIEnv *_env, jobject _this, jint func)
639{
640 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
641 LOG_API("nProgramFragmentStoreDepthFunc, con(%p), func(%i)", con, func);
642 rsProgramFragmentStoreDepthFunc((RsDepthFunc)func);
643}
644
645static void
646nProgramFragmentStoreDepthMask(JNIEnv *_env, jobject _this, jboolean enable)
647{
648 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
649 LOG_API("nProgramFragmentStoreDepthMask, con(%p), enable(%i)", con, enable);
650 rsProgramFragmentStoreDepthMask(enable);
651}
652
653static void
654nProgramFragmentStoreColorMask(JNIEnv *_env, jobject _this, jboolean r, jboolean g, jboolean b, jboolean a)
655{
656 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
657 LOG_API("nProgramFragmentStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
658 rsProgramFragmentStoreColorMask(r, g, b, a);
659}
660
661static void
662nProgramFragmentStoreBlendFunc(JNIEnv *_env, jobject _this, int src, int dst)
663{
664 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
665 LOG_API("nProgramFragmentStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
666 rsProgramFragmentStoreBlendFunc((RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
667}
668
669static void
670nProgramFragmentStoreDither(JNIEnv *_env, jobject _this, jboolean enable)
671{
672 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
673 LOG_API("nProgramFragmentStoreDither, con(%p), enable(%i)", con, enable);
674 rsProgramFragmentStoreDither(enable);
675}
676
677static jint
678nProgramFragmentStoreCreate(JNIEnv *_env, jobject _this)
679{
680 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
681 LOG_API("nProgramFragmentStoreCreate, con(%p)", con);
682 return (jint)rsProgramFragmentStoreCreate();
683}
684
685// ---------------------------------------------------------------------------
686
687static void
688nProgramFragmentBegin(JNIEnv *_env, jobject _this, jint in, jint out)
689{
690 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
691 LOG_API("nProgramFragmentBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
692 rsProgramFragmentBegin((RsElement)in, (RsElement)out);
693}
694
695static void
696nProgramFragmentBindTexture(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
697{
698 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
699 LOG_API("nProgramFragmentBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
700 rsProgramFragmentBindTexture((RsProgramFragment)vpf, slot, (RsAllocation)a);
701}
702
703static void
704nProgramFragmentBindSampler(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
705{
706 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
707 LOG_API("nProgramFragmentBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
708 rsProgramFragmentBindSampler((RsProgramFragment)vpf, slot, (RsSampler)a);
709}
710
711static void
712nProgramFragmentSetType(JNIEnv *_env, jobject _this, jint slot, jint vt)
713{
714 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
715 LOG_API("nProgramFragmentSetType, con(%p), slot(%i), vt(%p)", con, slot, (RsType)vt);
716 rsProgramFragmentSetType(slot, (RsType)vt);
717}
718
719static void
720nProgramFragmentSetEnvMode(JNIEnv *_env, jobject _this, jint slot, jint env)
721{
722 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
723 LOG_API("nProgramFragmentSetEnvMode, con(%p), slot(%i), vt(%i)", con, slot, env);
724 rsProgramFragmentSetEnvMode(slot, (RsTexEnvMode)env);
725}
726
727static void
728nProgramFragmentSetTexEnable(JNIEnv *_env, jobject _this, jint slot, jboolean enable)
729{
730 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
731 LOG_API("nProgramFragmentSetTexEnable, con(%p), slot(%i), enable(%i)", con, slot, enable);
732 rsProgramFragmentSetTexEnable(slot, enable);
733}
734
735static jint
736nProgramFragmentCreate(JNIEnv *_env, jobject _this, jint slot, jboolean enable)
737{
738 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
739 LOG_API("nProgramFragmentCreate, con(%p)", con);
740 return (jint)rsProgramFragmentCreate();
741}
742
743
744// ---------------------------------------------------------------------------
745
746static void
747nContextBindRootScript(JNIEnv *_env, jobject _this, jint script)
748{
749 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
750 LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
751 rsContextBindRootScript((RsScript)script);
752}
753
754static void
Jason Samsd19f10d2009-05-22 14:03:28 -0700755nContextBindProgramFragmentStore(JNIEnv *_env, jobject _this, jint pfs)
756{
757 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
758 LOG_API("nContextBindProgramFragmentStore, con(%p), pfs(%p)", con, (RsProgramFragmentStore)pfs);
759 rsContextBindProgramFragmentStore((RsProgramFragmentStore)pfs);
760}
761
762static void
763nContextBindProgramFragment(JNIEnv *_env, jobject _this, jint pf)
764{
765 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
766 LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
767 rsContextBindProgramFragment((RsProgramFragment)pf);
768}
769
Jason Sams02fb2cb2009-05-28 15:37:57 -0700770// ---------------------------------------------------------------------------
771
772static void
773nSamplerDestroy(JNIEnv *_env, jobject _this, jint s)
774{
775 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
776 LOG_API("nSamplerDestroy, con(%p), sampler(%p)", con, (RsSampler)s);
777 rsSamplerDestroy((RsSampler)s);
778}
779
780static void
781nSamplerBegin(JNIEnv *_env, jobject _this)
782{
783 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
784 LOG_API("nSamplerBegin, con(%p)", con);
785 rsSamplerBegin();
786}
787
788static void
789nSamplerSet(JNIEnv *_env, jobject _this, jint p, jint v)
790{
791 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
792 LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
793 rsSamplerSet((RsSamplerParam)p, (RsSamplerValue)v);
794}
795
796static jint
797nSamplerCreate(JNIEnv *_env, jobject _this)
798{
799 RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
800 LOG_API("nSamplerCreate, con(%p), script(%p)", con, (RsScript)script);
801 return (jint)rsSamplerCreate();
802}
803
Jason Samsd19f10d2009-05-22 14:03:28 -0700804
805// ---------------------------------------------------------------------------
806
807
Jack Palevichdf988512009-05-27 17:00:45 -0700808static const char *classPathName = "com/android/fountain/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -0700809
810static JNINativeMethod methods[] = {
811{"_nInit", "()V", (void*)_nInit },
812{"nDeviceCreate", "()I", (void*)nDeviceCreate },
813{"nDeviceDestroy", "(I)V", (void*)nDeviceDestroy },
814{"nContextCreate", "(ILandroid/view/Surface;I)I", (void*)nContextCreate },
815{"nContextDestroy", "(I)V", (void*)nContextDestroy },
816
817{"nElementBegin", "()V", (void*)nElementBegin },
818{"nElementAddPredefined", "(I)V", (void*)nElementAddPredefined },
819{"nElementAdd", "(IIII)V", (void*)nElementAdd },
820{"nElementCreate", "()I", (void*)nElementCreate },
821{"nElementGetPredefined", "(I)I", (void*)nElementGetPredefined },
822{"nElementDestroy", "(I)V", (void*)nElementDestroy },
823
824{"nTypeBegin", "(I)V", (void*)nTypeBegin },
825{"nTypeAdd", "(II)V", (void*)nTypeAdd },
826{"nTypeCreate", "()I", (void*)nTypeCreate },
827{"nTypeDestroy", "(I)V", (void*)nTypeDestroy },
828
829{"nAllocationCreateTyped", "(I)I", (void*)nAllocationCreateTyped },
830{"nAllocationCreatePredefSized", "(II)I", (void*)nAllocationCreatePredefSized },
831{"nAllocationCreateSized", "(II)I", (void*)nAllocationCreateSized },
Jason Samsffe9f482009-06-01 17:45:53 -0700832{"nAllocationCreateFromBitmap", "(IZLandroid/graphics/Bitmap;)I", (void*)nAllocationCreateFromBitmap },
Jason Samsd19f10d2009-05-22 14:03:28 -0700833{"nAllocationUploadToTexture", "(II)V", (void*)nAllocationUploadToTexture },
834{"nAllocationDestroy", "(I)V", (void*)nAllocationDestroy },
835{"nAllocationData", "(I[I)V", (void*)nAllocationData_i },
836{"nAllocationData", "(I[F)V", (void*)nAllocationData_f },
837{"nAllocationSubData1D", "(III[I)V", (void*)nAllocationSubData1D_i },
838{"nAllocationSubData1D", "(III[F)V", (void*)nAllocationSubData1D_f },
839{"nAllocationSubData2D", "(IIIII[I)V", (void*)nAllocationSubData2D_i },
840{"nAllocationSubData2D", "(IIIII[F)V", (void*)nAllocationSubData2D_f },
841
842{"nTriangleMeshDestroy", "(I)V", (void*)nTriangleMeshDestroy },
843{"nTriangleMeshBegin", "(II)V", (void*)nTriangleMeshBegin },
844{"nTriangleMeshAddVertex_XY", "(FF)V", (void*)nTriangleMeshAddVertex_XY },
845{"nTriangleMeshAddVertex_XYZ", "(FFF)V", (void*)nTriangleMeshAddVertex_XYZ },
846{"nTriangleMeshAddVertex_XY_ST", "(FFFF)V", (void*)nTriangleMeshAddVertex_XY_ST },
847{"nTriangleMeshAddVertex_XYZ_ST", "(FFFFF)V", (void*)nTriangleMeshAddVertex_XYZ_ST },
848{"nTriangleMeshAddTriangle", "(III)V", (void*)nTriangleMeshAddTriangle },
849{"nTriangleMeshCreate", "()I", (void*)nTriangleMeshCreate },
850
851{"nAdapter1DDestroy", "(I)V", (void*)nAdapter1DDestroy },
852{"nAdapter1DBindAllocation", "(II)V", (void*)nAdapter1DBindAllocation },
853{"nAdapter1DSetConstraint", "(III)V", (void*)nAdapter1DSetConstraint },
854{"nAdapter1DData", "(I[I)V", (void*)nAdapter1DData_i },
855{"nAdapter1DSubData", "(III[I)V", (void*)nAdapter1DSubData_i },
856{"nAdapter1DData", "(I[F)V", (void*)nAdapter1DData_f },
857{"nAdapter1DSubData", "(III[F)V", (void*)nAdapter1DSubData_f },
858{"nAdapter1DCreate", "()I", (void*)nAdapter1DCreate },
859
860{"nScriptDestroy", "(I)V", (void*)nScriptDestroy },
861{"nScriptBindAllocation", "(III)V", (void*)nScriptBindAllocation },
862{"nScriptCBegin", "()V", (void*)nScriptCBegin },
863{"nScriptCSetClearColor", "(FFFF)V", (void*)nScriptCSetClearColor },
864{"nScriptCSetClearDepth", "(F)V", (void*)nScriptCSetClearDepth },
865{"nScriptCSetClearStencil", "(I)V", (void*)nScriptCSetClearStencil },
866{"nScriptCAddType", "(I)V", (void*)nScriptCAddType },
867{"nScriptCSetRoot", "(Z)V", (void*)nScriptCSetRoot },
Jack Palevich43702d82009-05-28 13:38:16 -0700868{"nScriptCSetScript", "([BII)V", (void*)nScriptCSetScript },
Jason Samsd19f10d2009-05-22 14:03:28 -0700869{"nScriptCCreate", "()I", (void*)nScriptCCreate },
870
871{"nProgramFragmentStoreBegin", "(II)V", (void*)nProgramFragmentStoreBegin },
872{"nProgramFragmentStoreDepthFunc", "(I)V", (void*)nProgramFragmentStoreDepthFunc },
873{"nProgramFragmentStoreDepthMask", "(Z)V", (void*)nProgramFragmentStoreDepthMask },
874{"nProgramFragmentStoreColorMask", "(ZZZZ)V", (void*)nProgramFragmentStoreColorMask },
875{"nProgramFragmentStoreBlendFunc", "(II)V", (void*)nProgramFragmentStoreBlendFunc },
876{"nProgramFragmentStoreDither", "(Z)V", (void*)nProgramFragmentStoreDither },
877{"nProgramFragmentStoreCreate", "()I", (void*)nProgramFragmentStoreCreate },
878
879{"nProgramFragmentBegin", "(II)V", (void*)nProgramFragmentBegin },
880{"nProgramFragmentBindTexture", "(III)V", (void*)nProgramFragmentBindTexture },
881{"nProgramFragmentBindSampler", "(III)V", (void*)nProgramFragmentBindSampler },
882{"nProgramFragmentSetType", "(II)V", (void*)nProgramFragmentSetType },
883{"nProgramFragmentSetEnvMode", "(II)V", (void*)nProgramFragmentSetEnvMode },
884{"nProgramFragmentSetTexEnable", "(IZ)V", (void*)nProgramFragmentSetTexEnable },
885{"nProgramFragmentCreate", "()I", (void*)nProgramFragmentCreate },
886
887{"nContextBindRootScript", "(I)V", (void*)nContextBindRootScript },
Jason Samsd19f10d2009-05-22 14:03:28 -0700888{"nContextBindProgramFragmentStore","(I)V", (void*)nContextBindProgramFragmentStore },
889{"nContextBindProgramFragment", "(I)V", (void*)nContextBindProgramFragment },
890
Jason Sams02fb2cb2009-05-28 15:37:57 -0700891{"nSamplerDestroy", "(I)V", (void*)nSamplerDestroy },
892{"nSamplerBegin", "()V", (void*)nSamplerBegin },
893{"nSamplerSet", "(II)V", (void*)nSamplerSet },
894{"nSamplerCreate", "()I", (void*)nSamplerCreate },
895
Jason Samsd19f10d2009-05-22 14:03:28 -0700896};
897
898static int registerFuncs(JNIEnv *_env)
899{
900 return android::AndroidRuntime::registerNativeMethods(
901 _env, classPathName, methods, NELEM(methods));
902}
903
904// ---------------------------------------------------------------------------
905
906jint JNI_OnLoad(JavaVM* vm, void* reserved)
907{
908 JNIEnv* env = NULL;
909 jint result = -1;
910
911 LOGE("****************************************************\n");
912
913 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
914 LOGE("ERROR: GetEnv failed\n");
915 goto bail;
916 }
917 assert(env != NULL);
918
919 if (registerFuncs(env) < 0) {
920 LOGE("ERROR: MediaPlayer native registration failed\n");
921 goto bail;
922 }
923
924 /* success -- return valid version number */
925 result = JNI_VERSION_1_4;
926
927bail:
928 return result;
929}