• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #define LOG_TAG "libRS_jni"
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <math.h>
24 #include <utils/misc.h>
25 
26 #include <surfaceflinger/Surface.h>
27 
28 #include <core/SkBitmap.h>
29 #include <core/SkPixelRef.h>
30 #include <core/SkStream.h>
31 #include <core/SkTemplates.h>
32 #include <images/SkImageDecoder.h>
33 
34 #include <utils/Asset.h>
35 #include <utils/ResourceTypes.h>
36 
37 #include "jni.h"
38 #include "JNIHelp.h"
39 #include "android_runtime/AndroidRuntime.h"
40 
41 #include <RenderScript.h>
42 #include <RenderScriptEnv.h>
43 
44 //#define LOG_API LOGE
45 #define LOG_API(...)
46 
47 using namespace android;
48 
49 // ---------------------------------------------------------------------------
50 
doThrow(JNIEnv * env,const char * exc,const char * msg=NULL)51 static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
52 {
53     jclass npeClazz = env->FindClass(exc);
54     env->ThrowNew(npeClazz, msg);
55 }
56 
57 static jfieldID gContextId = 0;
58 static jfieldID gNativeBitmapID = 0;
59 static jfieldID gTypeNativeCache = 0;
60 
61 static RsElement g_A_8 = NULL;
62 static RsElement g_RGBA_4444 = NULL;
63 static RsElement g_RGBA_8888 = NULL;
64 static RsElement g_RGB_565 = NULL;
65 
_nInit(JNIEnv * _env,jclass _this)66 static void _nInit(JNIEnv *_env, jclass _this)
67 {
68     gContextId             = _env->GetFieldID(_this, "mContext", "I");
69 
70     jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
71     gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I");
72 
73     jclass typeClass = _env->FindClass("android/renderscript/Type");
74     gTypeNativeCache = _env->GetFieldID(typeClass, "mNativeCache", "I");
75 }
76 
nInitElements(JNIEnv * _env,jobject _this,jint a8,jint rgba4444,jint rgba8888,jint rgb565)77 static void nInitElements(JNIEnv *_env, jobject _this, jint a8, jint rgba4444, jint rgba8888, jint rgb565)
78 {
79     g_A_8 = reinterpret_cast<RsElement>(a8);
80     g_RGBA_4444 = reinterpret_cast<RsElement>(rgba4444);
81     g_RGBA_8888 = reinterpret_cast<RsElement>(rgba8888);
82     g_RGB_565 = reinterpret_cast<RsElement>(rgb565);
83 }
84 
85 // ---------------------------------------------------------------------------
86 
87 static void
nAssignName(JNIEnv * _env,jobject _this,jint obj,jbyteArray str)88 nAssignName(JNIEnv *_env, jobject _this, jint obj, jbyteArray str)
89 {
90     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
91     LOG_API("nAssignName, con(%p), obj(%p)", con, (void *)obj);
92 
93     jint len = _env->GetArrayLength(str);
94     jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
95     rsAssignName(con, (void *)obj, (const char *)cptr, len);
96     _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
97 }
98 
99 static void
nObjDestroy(JNIEnv * _env,jobject _this,jint obj)100 nObjDestroy(JNIEnv *_env, jobject _this, jint obj)
101 {
102     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
103     LOG_API("nObjDestroy, con(%p) obj(%p)", con, (void *)obj);
104     rsObjDestroy(con, (void *)obj);
105 }
106 
107 static void
nObjDestroyOOB(JNIEnv * _env,jobject _this,jint obj)108 nObjDestroyOOB(JNIEnv *_env, jobject _this, jint obj)
109 {
110     // This function only differs from nObjDestroy in that it calls the
111     // special Out Of Band version of ObjDestroy which is thread safe.
112     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
113     LOG_API("nObjDestroyOOB, con(%p) obj(%p)", con, (void *)obj);
114     rsObjDestroyOOB(con, (void *)obj);
115 }
116 
117 static jint
nFileOpen(JNIEnv * _env,jobject _this,jbyteArray str)118 nFileOpen(JNIEnv *_env, jobject _this, jbyteArray str)
119 {
120     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
121     LOG_API("nFileOpen, con(%p)", con);
122 
123     jint len = _env->GetArrayLength(str);
124     jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
125     jint ret = (jint)rsFileOpen(con, (const char *)cptr, len);
126     _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
127     return ret;
128 }
129 
130 // ---------------------------------------------------------------------------
131 
132 static jint
nDeviceCreate(JNIEnv * _env,jobject _this)133 nDeviceCreate(JNIEnv *_env, jobject _this)
134 {
135     LOG_API("nDeviceCreate");
136     return (jint)rsDeviceCreate();
137 }
138 
139 static void
nDeviceDestroy(JNIEnv * _env,jobject _this,jint dev)140 nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev)
141 {
142     LOG_API("nDeviceDestroy");
143     return rsDeviceDestroy((RsDevice)dev);
144 }
145 
146 static void
nDeviceSetConfig(JNIEnv * _env,jobject _this,jint dev,jint p,jint value)147 nDeviceSetConfig(JNIEnv *_env, jobject _this, jint dev, jint p, jint value)
148 {
149     LOG_API("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
150     return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
151 }
152 
153 static jint
nContextCreate(JNIEnv * _env,jobject _this,jint dev,jint ver)154 nContextCreate(JNIEnv *_env, jobject _this, jint dev, jint ver)
155 {
156     LOG_API("nContextCreate");
157     return (jint)rsContextCreate((RsDevice)dev, ver);
158 }
159 
160 static jint
nContextCreateGL(JNIEnv * _env,jobject _this,jint dev,jint ver,jboolean useDepth)161 nContextCreateGL(JNIEnv *_env, jobject _this, jint dev, jint ver, jboolean useDepth)
162 {
163     LOG_API("nContextCreateGL");
164     return (jint)rsContextCreateGL((RsDevice)dev, ver, useDepth);
165 }
166 
167 static void
nContextSetPriority(JNIEnv * _env,jobject _this,jint p)168 nContextSetPriority(JNIEnv *_env, jobject _this, jint p)
169 {
170     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
171     LOG_API("ContextSetPriority, con(%p), priority(%i)", con, p);
172     rsContextSetPriority(con, p);
173 }
174 
175 
176 
177 static void
nContextSetSurface(JNIEnv * _env,jobject _this,jint width,jint height,jobject wnd)178 nContextSetSurface(JNIEnv *_env, jobject _this, jint width, jint height, jobject wnd)
179 {
180     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
181     LOG_API("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", con, width, height, (Surface *)wnd);
182 
183     Surface * window = NULL;
184     if (wnd == NULL) {
185 
186     } else {
187         jclass surface_class = _env->FindClass("android/view/Surface");
188         jfieldID surfaceFieldID = _env->GetFieldID(surface_class, ANDROID_VIEW_SURFACE_JNI_ID, "I");
189         window = (Surface*)_env->GetIntField(wnd, surfaceFieldID);
190     }
191 
192     rsContextSetSurface(con, width, height, window);
193 }
194 
195 static void
nContextDestroy(JNIEnv * _env,jobject _this,jint con)196 nContextDestroy(JNIEnv *_env, jobject _this, jint con)
197 {
198     LOG_API("nContextDestroy, con(%p)", (RsContext)con);
199     rsContextDestroy((RsContext)con);
200 }
201 
202 static void
nContextDump(JNIEnv * _env,jobject _this,jint bits)203 nContextDump(JNIEnv *_env, jobject _this, jint bits)
204 {
205     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
206     LOG_API("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
207     rsContextDump((RsContext)con, bits);
208 }
209 
210 static void
nContextPause(JNIEnv * _env,jobject _this)211 nContextPause(JNIEnv *_env, jobject _this)
212 {
213     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
214     LOG_API("nContextPause, con(%p)", con);
215     rsContextPause(con);
216 }
217 
218 static void
nContextResume(JNIEnv * _env,jobject _this)219 nContextResume(JNIEnv *_env, jobject _this)
220 {
221     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
222     LOG_API("nContextResume, con(%p)", con);
223     rsContextResume(con);
224 }
225 
226 static jint
nContextGetMessage(JNIEnv * _env,jobject _this,jintArray data,jboolean wait)227 nContextGetMessage(JNIEnv *_env, jobject _this, jintArray data, jboolean wait)
228 {
229     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
230     jint len = _env->GetArrayLength(data);
231     LOG_API("nContextGetMessage, con(%p), len(%i)", con, len);
232     jint *ptr = _env->GetIntArrayElements(data, NULL);
233     size_t receiveLen;
234     int id = rsContextGetMessage(con, ptr, &receiveLen, len * 4, wait);
235     if (!id && receiveLen) {
236         LOGE("message receive buffer too small.  %i", receiveLen);
237     }
238     _env->ReleaseIntArrayElements(data, ptr, 0);
239     return id;
240 }
241 
nContextInitToClient(JNIEnv * _env,jobject _this)242 static void nContextInitToClient(JNIEnv *_env, jobject _this)
243 {
244     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
245     LOG_API("nContextInitToClient, con(%p)", con);
246     rsContextInitToClient(con);
247 }
248 
nContextDeinitToClient(JNIEnv * _env,jobject _this)249 static void nContextDeinitToClient(JNIEnv *_env, jobject _this)
250 {
251     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
252     LOG_API("nContextDeinitToClient, con(%p)", con);
253     rsContextDeinitToClient(con);
254 }
255 
256 
257 static jint
nElementCreate(JNIEnv * _env,jobject _this,jint type,jint kind,jboolean norm,jint size)258 nElementCreate(JNIEnv *_env, jobject _this, jint type, jint kind, jboolean norm, jint size)
259 {
260     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
261     LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", con, type, kind, norm, size);
262     return (jint)rsElementCreate(con, (RsDataType)type, (RsDataKind)kind, norm, size);
263 }
264 
265 static jint
nElementCreate2(JNIEnv * _env,jobject _this,jintArray _ids,jobjectArray _names)266 nElementCreate2(JNIEnv *_env, jobject _this, jintArray _ids, jobjectArray _names)
267 {
268     int fieldCount = _env->GetArrayLength(_ids);
269     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
270     LOG_API("nElementCreate2, con(%p)", con);
271 
272     jint *ids = _env->GetIntArrayElements(_ids, NULL);
273     const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
274     size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
275 
276     for (int ct=0; ct < fieldCount; ct++) {
277         jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
278         nameArray[ct] = _env->GetStringUTFChars(s, NULL);
279         sizeArray[ct] = _env->GetStringUTFLength(s);
280     }
281     jint id = (jint)rsElementCreate2(con, fieldCount, (RsElement *)ids, nameArray, sizeArray);
282     for (int ct=0; ct < fieldCount; ct++) {
283         jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
284         _env->ReleaseStringUTFChars(s, nameArray[ct]);
285     }
286     _env->ReleaseIntArrayElements(_ids, ids, JNI_ABORT);
287     free(nameArray);
288     free(sizeArray);
289     return (jint)id;
290 }
291 
292 // -----------------------------------
293 
294 static void
nTypeBegin(JNIEnv * _env,jobject _this,jint eID)295 nTypeBegin(JNIEnv *_env, jobject _this, jint eID)
296 {
297     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
298     LOG_API("nTypeBegin, con(%p) e(%p)", con, (RsElement)eID);
299     rsTypeBegin(con, (RsElement)eID);
300 }
301 
302 static void
nTypeAdd(JNIEnv * _env,jobject _this,jint dim,jint val)303 nTypeAdd(JNIEnv *_env, jobject _this, jint dim, jint val)
304 {
305     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
306     LOG_API("nTypeAdd, con(%p) dim(%i), val(%i)", con, dim, val);
307     rsTypeAdd(con, (RsDimension)dim, val);
308 }
309 
310 static jint
nTypeCreate(JNIEnv * _env,jobject _this)311 nTypeCreate(JNIEnv *_env, jobject _this)
312 {
313     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
314     LOG_API("nTypeCreate, con(%p)", con);
315     return (jint)rsTypeCreate(con);
316 }
317 
SF_LoadInt(JNIEnv * _env,jobject _obj,jfieldID _field,void * buffer)318 static void * SF_LoadInt(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
319 {
320     ((int32_t *)buffer)[0] = _env->GetIntField(_obj, _field);
321     return ((uint8_t *)buffer) + 4;
322 }
323 
SF_LoadShort(JNIEnv * _env,jobject _obj,jfieldID _field,void * buffer)324 static void * SF_LoadShort(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
325 {
326     ((int16_t *)buffer)[0] = _env->GetShortField(_obj, _field);
327     return ((uint8_t *)buffer) + 2;
328 }
329 
SF_LoadByte(JNIEnv * _env,jobject _obj,jfieldID _field,void * buffer)330 static void * SF_LoadByte(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
331 {
332     ((int8_t *)buffer)[0] = _env->GetByteField(_obj, _field);
333     return ((uint8_t *)buffer) + 1;
334 }
335 
SF_LoadFloat(JNIEnv * _env,jobject _obj,jfieldID _field,void * buffer)336 static void * SF_LoadFloat(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
337 {
338     ((float *)buffer)[0] = _env->GetFloatField(_obj, _field);
339     return ((uint8_t *)buffer) + 4;
340 }
341 
SF_SaveInt(JNIEnv * _env,jobject _obj,jfieldID _field,void * buffer)342 static void * SF_SaveInt(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
343 {
344     _env->SetIntField(_obj, _field, ((int32_t *)buffer)[0]);
345     return ((uint8_t *)buffer) + 4;
346 }
347 
SF_SaveShort(JNIEnv * _env,jobject _obj,jfieldID _field,void * buffer)348 static void * SF_SaveShort(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
349 {
350     _env->SetShortField(_obj, _field, ((int16_t *)buffer)[0]);
351     return ((uint8_t *)buffer) + 2;
352 }
353 
SF_SaveByte(JNIEnv * _env,jobject _obj,jfieldID _field,void * buffer)354 static void * SF_SaveByte(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
355 {
356     _env->SetByteField(_obj, _field, ((int8_t *)buffer)[0]);
357     return ((uint8_t *)buffer) + 1;
358 }
359 
SF_SaveFloat(JNIEnv * _env,jobject _obj,jfieldID _field,void * buffer)360 static void * SF_SaveFloat(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
361 {
362     _env->SetFloatField(_obj, _field, ((float *)buffer)[0]);
363     return ((uint8_t *)buffer) + 4;
364 }
365 
366 struct TypeFieldCache {
367     jfieldID field;
368     int bits;
369     void * (*ptr)(JNIEnv *, jobject, jfieldID, void *buffer);
370     void * (*readPtr)(JNIEnv *, jobject, jfieldID, void *buffer);
371 };
372 
373 struct TypeCache {
374     int fieldCount;
375     int size;
376     TypeFieldCache fields[1];
377 };
378 
379 //{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
380 static void
nTypeFinalDestroy(JNIEnv * _env,jobject _this,jobject _type)381 nTypeFinalDestroy(JNIEnv *_env, jobject _this, jobject _type)
382 {
383     TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
384     free(tc);
385 }
386 
387 // native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
388 static void
nTypeSetupFields(JNIEnv * _env,jobject _this,jobject _type,jintArray _types,jintArray _bits,jobjectArray _IDs)389 nTypeSetupFields(JNIEnv *_env, jobject _this, jobject _type, jintArray _types, jintArray _bits, jobjectArray _IDs)
390 {
391     int fieldCount = _env->GetArrayLength(_types);
392     size_t structSize = sizeof(TypeCache) + (sizeof(TypeFieldCache) * (fieldCount-1));
393     TypeCache *tc = (TypeCache *)malloc(structSize);
394     memset(tc, 0, structSize);
395 
396     TypeFieldCache *tfc = &tc->fields[0];
397     tc->fieldCount = fieldCount;
398     _env->SetIntField(_type, gTypeNativeCache, (jint)tc);
399 
400     jint *fType = _env->GetIntArrayElements(_types, NULL);
401     jint *fBits = _env->GetIntArrayElements(_bits, NULL);
402     for (int ct=0; ct < fieldCount; ct++) {
403         jobject field = _env->GetObjectArrayElement(_IDs, ct);
404         tfc[ct].field = _env->FromReflectedField(field);
405         tfc[ct].bits = fBits[ct];
406 
407         switch(fType[ct]) {
408         case RS_TYPE_FLOAT_32:
409             tfc[ct].ptr = SF_LoadFloat;
410             tfc[ct].readPtr = SF_SaveFloat;
411             break;
412         case RS_TYPE_UNSIGNED_32:
413         case RS_TYPE_SIGNED_32:
414             tfc[ct].ptr = SF_LoadInt;
415             tfc[ct].readPtr = SF_SaveInt;
416             break;
417         case RS_TYPE_UNSIGNED_16:
418         case RS_TYPE_SIGNED_16:
419             tfc[ct].ptr = SF_LoadShort;
420             tfc[ct].readPtr = SF_SaveShort;
421             break;
422         case RS_TYPE_UNSIGNED_8:
423         case RS_TYPE_SIGNED_8:
424             tfc[ct].ptr = SF_LoadByte;
425             tfc[ct].readPtr = SF_SaveByte;
426             break;
427         }
428         tc->size += 4;
429     }
430 
431     _env->ReleaseIntArrayElements(_types, fType, JNI_ABORT);
432     _env->ReleaseIntArrayElements(_bits, fBits, JNI_ABORT);
433 }
434 
435 
436 // -----------------------------------
437 
438 static jint
nAllocationCreateTyped(JNIEnv * _env,jobject _this,jint e)439 nAllocationCreateTyped(JNIEnv *_env, jobject _this, jint e)
440 {
441     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
442     LOG_API("nAllocationCreateTyped, con(%p), e(%p)", con, (RsElement)e);
443     return (jint) rsAllocationCreateTyped(con, (RsElement)e);
444 }
445 
446 static void
nAllocationUploadToTexture(JNIEnv * _env,jobject _this,jint a,jboolean genMip,jint mip)447 nAllocationUploadToTexture(JNIEnv *_env, jobject _this, jint a, jboolean genMip, jint mip)
448 {
449     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
450     LOG_API("nAllocationUploadToTexture, con(%p), a(%p), genMip(%i), mip(%i)", con, (RsAllocation)a, genMip, mip);
451     rsAllocationUploadToTexture(con, (RsAllocation)a, genMip, mip);
452 }
453 
454 static void
nAllocationUploadToBufferObject(JNIEnv * _env,jobject _this,jint a)455 nAllocationUploadToBufferObject(JNIEnv *_env, jobject _this, jint a)
456 {
457     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
458     LOG_API("nAllocationUploadToBufferObject, con(%p), a(%p)", con, (RsAllocation)a);
459     rsAllocationUploadToBufferObject(con, (RsAllocation)a);
460 }
461 
SkBitmapToPredefined(SkBitmap::Config cfg)462 static RsElement SkBitmapToPredefined(SkBitmap::Config cfg)
463 {
464     switch (cfg) {
465     case SkBitmap::kA8_Config:
466         return g_A_8;
467     case SkBitmap::kARGB_4444_Config:
468         return g_RGBA_4444;
469     case SkBitmap::kARGB_8888_Config:
470         return g_RGBA_8888;
471     case SkBitmap::kRGB_565_Config:
472         return g_RGB_565;
473 
474     default:
475         break;
476     }
477     // If we don't have a conversion mark it as a user type.
478     LOGE("Unsupported bitmap type");
479     return NULL;
480 }
481 
482 static int
nAllocationCreateFromBitmap(JNIEnv * _env,jobject _this,jint dstFmt,jboolean genMips,jobject jbitmap)483 nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
484 {
485     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
486     SkBitmap const * nativeBitmap =
487             (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
488     const SkBitmap& bitmap(*nativeBitmap);
489     SkBitmap::Config config = bitmap.getConfig();
490 
491     RsElement e = SkBitmapToPredefined(config);
492     if (e) {
493         bitmap.lockPixels();
494         const int w = bitmap.width();
495         const int h = bitmap.height();
496         const void* ptr = bitmap.getPixels();
497         jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
498         bitmap.unlockPixels();
499         return id;
500     }
501     return 0;
502 }
503 
ReleaseBitmapCallback(void * bmp)504 static void ReleaseBitmapCallback(void *bmp)
505 {
506     SkBitmap const * nativeBitmap = (SkBitmap const *)bmp;
507     nativeBitmap->unlockPixels();
508 }
509 
510 static int
nAllocationCreateBitmapRef(JNIEnv * _env,jobject _this,jint type,jobject jbitmap)511 nAllocationCreateBitmapRef(JNIEnv *_env, jobject _this, jint type, jobject jbitmap)
512 {
513     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
514     SkBitmap * nativeBitmap =
515             (SkBitmap *)_env->GetIntField(jbitmap, gNativeBitmapID);
516 
517 
518     nativeBitmap->lockPixels();
519     void* ptr = nativeBitmap->getPixels();
520     jint id = (jint)rsAllocationCreateBitmapRef(con, (RsType)type, ptr, nativeBitmap, ReleaseBitmapCallback);
521     return id;
522 }
523 
524 static int
nAllocationCreateFromAssetStream(JNIEnv * _env,jobject _this,jint dstFmt,jboolean genMips,jint native_asset)525 nAllocationCreateFromAssetStream(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jint native_asset)
526 {
527     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
528 
529     Asset* asset = reinterpret_cast<Asset*>(native_asset);
530     SkBitmap bitmap;
531     SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
532             &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
533 
534     SkBitmap::Config config = bitmap.getConfig();
535 
536     RsElement e = SkBitmapToPredefined(config);
537 
538     if (e) {
539         bitmap.lockPixels();
540         const int w = bitmap.width();
541         const int h = bitmap.height();
542         const void* ptr = bitmap.getPixels();
543         jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
544         bitmap.unlockPixels();
545         return id;
546     }
547     return 0;
548 }
549 
550 static int
nAllocationCreateFromBitmapBoxed(JNIEnv * _env,jobject _this,jint dstFmt,jboolean genMips,jobject jbitmap)551 nAllocationCreateFromBitmapBoxed(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
552 {
553     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
554     SkBitmap const * nativeBitmap =
555             (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
556     const SkBitmap& bitmap(*nativeBitmap);
557     SkBitmap::Config config = bitmap.getConfig();
558 
559     RsElement e = SkBitmapToPredefined(config);
560 
561     if (e) {
562         bitmap.lockPixels();
563         const int w = bitmap.width();
564         const int h = bitmap.height();
565         const void* ptr = bitmap.getPixels();
566         jint id = (jint)rsAllocationCreateFromBitmapBoxed(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
567         bitmap.unlockPixels();
568         return id;
569     }
570     return 0;
571 }
572 
573 
574 static void
nAllocationSubData1D_i(JNIEnv * _env,jobject _this,jint alloc,jint offset,jint count,jintArray data,int sizeBytes)575 nAllocationSubData1D_i(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jintArray data, int sizeBytes)
576 {
577     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
578     jint len = _env->GetArrayLength(data);
579     LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
580     jint *ptr = _env->GetIntArrayElements(data, NULL);
581     rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
582     _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
583 }
584 
585 static void
nAllocationSubData1D_s(JNIEnv * _env,jobject _this,jint alloc,jint offset,jint count,jshortArray data,int sizeBytes)586 nAllocationSubData1D_s(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jshortArray data, int sizeBytes)
587 {
588     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
589     jint len = _env->GetArrayLength(data);
590     LOG_API("nAllocation1DSubData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
591     jshort *ptr = _env->GetShortArrayElements(data, NULL);
592     rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
593     _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
594 }
595 
596 static void
nAllocationSubData1D_b(JNIEnv * _env,jobject _this,jint alloc,jint offset,jint count,jbyteArray data,int sizeBytes)597 nAllocationSubData1D_b(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jbyteArray data, int sizeBytes)
598 {
599     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
600     jint len = _env->GetArrayLength(data);
601     LOG_API("nAllocation1DSubData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
602     jbyte *ptr = _env->GetByteArrayElements(data, NULL);
603     rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
604     _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
605 }
606 
607 static void
nAllocationSubData1D_f(JNIEnv * _env,jobject _this,jint alloc,jint offset,jint count,jfloatArray data,int sizeBytes)608 nAllocationSubData1D_f(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jfloatArray data, int sizeBytes)
609 {
610     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
611     jint len = _env->GetArrayLength(data);
612     LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
613     jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
614     rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
615     _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
616 }
617 
618 static void
nAllocationSubData2D_i(JNIEnv * _env,jobject _this,jint alloc,jint xoff,jint yoff,jint w,jint h,jintArray data,int sizeBytes)619 nAllocationSubData2D_i(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jintArray data, int sizeBytes)
620 {
621     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
622     jint len = _env->GetArrayLength(data);
623     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);
624     jint *ptr = _env->GetIntArrayElements(data, NULL);
625     rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
626     _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
627 }
628 
629 static void
nAllocationSubData2D_f(JNIEnv * _env,jobject _this,jint alloc,jint xoff,jint yoff,jint w,jint h,jfloatArray data,int sizeBytes)630 nAllocationSubData2D_f(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jfloatArray data, int sizeBytes)
631 {
632     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
633     jint len = _env->GetArrayLength(data);
634     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);
635     jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
636     rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
637     _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
638 }
639 
640 static void
nAllocationRead_i(JNIEnv * _env,jobject _this,jint alloc,jintArray data)641 nAllocationRead_i(JNIEnv *_env, jobject _this, jint alloc, jintArray data)
642 {
643     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
644     jint len = _env->GetArrayLength(data);
645     LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
646     jint *ptr = _env->GetIntArrayElements(data, NULL);
647     rsAllocationRead(con, (RsAllocation)alloc, ptr);
648     _env->ReleaseIntArrayElements(data, ptr, 0);
649 }
650 
651 static void
nAllocationRead_f(JNIEnv * _env,jobject _this,jint alloc,jfloatArray data)652 nAllocationRead_f(JNIEnv *_env, jobject _this, jint alloc, jfloatArray data)
653 {
654     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
655     jint len = _env->GetArrayLength(data);
656     LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
657     jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
658     rsAllocationRead(con, (RsAllocation)alloc, ptr);
659     _env->ReleaseFloatArrayElements(data, ptr, 0);
660 }
661 
662 
663 //{"nAllocationDataFromObject",      "(ILandroid/renderscript/Type;Ljava/lang/Object;)V",   (void*)nAllocationDataFromObject },
664 static void
nAllocationSubDataFromObject(JNIEnv * _env,jobject _this,jint alloc,jobject _type,jint offset,jobject _o)665 nAllocationSubDataFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _type, jint offset, jobject _o)
666 {
667     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
668     LOG_API("nAllocationDataFromObject con(%p), alloc(%p)", con, (RsAllocation)alloc);
669 
670     const TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
671 
672     void * bufAlloc = malloc(tc->size);
673     void * buf = bufAlloc;
674     for (int ct=0; ct < tc->fieldCount; ct++) {
675         const TypeFieldCache *tfc = &tc->fields[ct];
676         buf = tfc->ptr(_env, _o, tfc->field, buf);
677     }
678     rsAllocation1DSubData(con, (RsAllocation)alloc, offset, 1, bufAlloc, tc->size);
679     free(bufAlloc);
680 }
681 
682 static void
nAllocationSubReadFromObject(JNIEnv * _env,jobject _this,jint alloc,jobject _type,jint offset,jobject _o)683 nAllocationSubReadFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _type, jint offset, jobject _o)
684 {
685     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
686     LOG_API("nAllocationReadFromObject con(%p), alloc(%p)", con, (RsAllocation)alloc);
687 
688     assert(offset == 0);
689 
690     const TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
691 
692     void * bufAlloc = malloc(tc->size);
693     void * buf = bufAlloc;
694     rsAllocationRead(con, (RsAllocation)alloc, bufAlloc);
695 
696     for (int ct=0; ct < tc->fieldCount; ct++) {
697         const TypeFieldCache *tfc = &tc->fields[ct];
698         buf = tfc->readPtr(_env, _o, tfc->field, buf);
699     }
700     free(bufAlloc);
701 }
702 
703 
704 // -----------------------------------
705 
706 static void
nAdapter1DBindAllocation(JNIEnv * _env,jobject _this,jint adapter,jint alloc)707 nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, jint adapter, jint alloc)
708 {
709     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
710     LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc);
711     rsAdapter1DBindAllocation(con, (RsAdapter1D)adapter, (RsAllocation)alloc);
712 }
713 
714 static void
nAdapter1DSetConstraint(JNIEnv * _env,jobject _this,jint adapter,jint dim,jint value)715 nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, jint adapter, jint dim, jint value)
716 {
717     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
718     LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value);
719     rsAdapter1DSetConstraint(con, (RsAdapter1D)adapter, (RsDimension)dim, value);
720 }
721 
722 static void
nAdapter1DData_i(JNIEnv * _env,jobject _this,jint adapter,jintArray data)723 nAdapter1DData_i(JNIEnv *_env, jobject _this, jint adapter, jintArray data)
724 {
725     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
726     jint len = _env->GetArrayLength(data);
727     LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
728     jint *ptr = _env->GetIntArrayElements(data, NULL);
729     rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
730     _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
731 }
732 
733 static void
nAdapter1DSubData_i(JNIEnv * _env,jobject _this,jint adapter,jint offset,jint count,jintArray data)734 nAdapter1DSubData_i(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jintArray data)
735 {
736     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
737     jint len = _env->GetArrayLength(data);
738     LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
739     jint *ptr = _env->GetIntArrayElements(data, NULL);
740     rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
741     _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
742 }
743 
744 static void
nAdapter1DData_f(JNIEnv * _env,jobject _this,jint adapter,jfloatArray data)745 nAdapter1DData_f(JNIEnv *_env, jobject _this, jint adapter, jfloatArray data)
746 {
747     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
748     jint len = _env->GetArrayLength(data);
749     LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
750     jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
751     rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
752     _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
753 }
754 
755 static void
nAdapter1DSubData_f(JNIEnv * _env,jobject _this,jint adapter,jint offset,jint count,jfloatArray data)756 nAdapter1DSubData_f(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jfloatArray data)
757 {
758     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
759     jint len = _env->GetArrayLength(data);
760     LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
761     jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
762     rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
763     _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
764 }
765 
766 static jint
nAdapter1DCreate(JNIEnv * _env,jobject _this)767 nAdapter1DCreate(JNIEnv *_env, jobject _this)
768 {
769     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
770     LOG_API("nAdapter1DCreate, con(%p)", con);
771     return (jint)rsAdapter1DCreate(con);
772 }
773 
774 // -----------------------------------
775 
776 static void
nAdapter2DBindAllocation(JNIEnv * _env,jobject _this,jint adapter,jint alloc)777 nAdapter2DBindAllocation(JNIEnv *_env, jobject _this, jint adapter, jint alloc)
778 {
779     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
780     LOG_API("nAdapter2DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter2D)adapter, (RsAllocation)alloc);
781     rsAdapter2DBindAllocation(con, (RsAdapter2D)adapter, (RsAllocation)alloc);
782 }
783 
784 static void
nAdapter2DSetConstraint(JNIEnv * _env,jobject _this,jint adapter,jint dim,jint value)785 nAdapter2DSetConstraint(JNIEnv *_env, jobject _this, jint adapter, jint dim, jint value)
786 {
787     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
788     LOG_API("nAdapter2DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter2D)adapter, dim, value);
789     rsAdapter2DSetConstraint(con, (RsAdapter2D)adapter, (RsDimension)dim, value);
790 }
791 
792 static void
nAdapter2DData_i(JNIEnv * _env,jobject _this,jint adapter,jintArray data)793 nAdapter2DData_i(JNIEnv *_env, jobject _this, jint adapter, jintArray data)
794 {
795     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
796     jint len = _env->GetArrayLength(data);
797     LOG_API("nAdapter2DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
798     jint *ptr = _env->GetIntArrayElements(data, NULL);
799     rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
800     _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
801 }
802 
803 static void
nAdapter2DData_f(JNIEnv * _env,jobject _this,jint adapter,jfloatArray data)804 nAdapter2DData_f(JNIEnv *_env, jobject _this, jint adapter, jfloatArray data)
805 {
806     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
807     jint len = _env->GetArrayLength(data);
808     LOG_API("nAdapter2DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
809     jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
810     rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
811     _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
812 }
813 
814 static void
nAdapter2DSubData_i(JNIEnv * _env,jobject _this,jint adapter,jint xoff,jint yoff,jint w,jint h,jintArray data)815 nAdapter2DSubData_i(JNIEnv *_env, jobject _this, jint adapter, jint xoff, jint yoff, jint w, jint h, jintArray data)
816 {
817     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
818     jint len = _env->GetArrayLength(data);
819     LOG_API("nAdapter2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
820             con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
821     jint *ptr = _env->GetIntArrayElements(data, NULL);
822     rsAdapter2DSubData(con, (RsAdapter2D)adapter, xoff, yoff, w, h, ptr);
823     _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
824 }
825 
826 static void
nAdapter2DSubData_f(JNIEnv * _env,jobject _this,jint adapter,jint xoff,jint yoff,jint w,jint h,jfloatArray data)827 nAdapter2DSubData_f(JNIEnv *_env, jobject _this, jint adapter, jint xoff, jint yoff, jint w, jint h, jfloatArray data)
828 {
829     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
830     jint len = _env->GetArrayLength(data);
831     LOG_API("nAdapter2DSubData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
832             con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
833     jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
834     rsAdapter2DSubData(con, (RsAdapter1D)adapter, xoff, yoff, w, h, ptr);
835     _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
836 }
837 
838 static jint
nAdapter2DCreate(JNIEnv * _env,jobject _this)839 nAdapter2DCreate(JNIEnv *_env, jobject _this)
840 {
841     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
842     LOG_API("nAdapter2DCreate, con(%p)", con);
843     return (jint)rsAdapter2DCreate(con);
844 }
845 
846 // -----------------------------------
847 
848 static void
nScriptBindAllocation(JNIEnv * _env,jobject _this,jint script,jint alloc,jint slot)849 nScriptBindAllocation(JNIEnv *_env, jobject _this, jint script, jint alloc, jint slot)
850 {
851     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
852     LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
853     rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
854 }
855 
856 static void
nScriptSetClearColor(JNIEnv * _env,jobject _this,jint script,jfloat r,jfloat g,jfloat b,jfloat a)857 nScriptSetClearColor(JNIEnv *_env, jobject _this, jint script, jfloat r, jfloat g, jfloat b, jfloat a)
858 {
859     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
860     LOG_API("nScriptSetClearColor, con(%p), s(%p), r(%f), g(%f), b(%f), a(%f)", con, (void *)script, r, g, b, a);
861     rsScriptSetClearColor(con, (RsScript)script, r, g, b, a);
862 }
863 
864 static void
nScriptSetClearDepth(JNIEnv * _env,jobject _this,jint script,jfloat d)865 nScriptSetClearDepth(JNIEnv *_env, jobject _this, jint script, jfloat d)
866 {
867     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
868     LOG_API("nScriptCSetClearDepth, con(%p), s(%p), depth(%f)", con, (void *)script, d);
869     rsScriptSetClearDepth(con, (RsScript)script, d);
870 }
871 
872 static void
nScriptSetClearStencil(JNIEnv * _env,jobject _this,jint script,jint stencil)873 nScriptSetClearStencil(JNIEnv *_env, jobject _this, jint script, jint stencil)
874 {
875     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
876     LOG_API("nScriptCSetClearStencil, con(%p), s(%p), stencil(%i)", con, (void *)script, stencil);
877     rsScriptSetClearStencil(con, (RsScript)script, stencil);
878 }
879 
880 static void
nScriptSetTimeZone(JNIEnv * _env,jobject _this,jint script,jbyteArray timeZone)881 nScriptSetTimeZone(JNIEnv *_env, jobject _this, jint script, jbyteArray timeZone)
882 {
883     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
884     LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
885 
886     jint length = _env->GetArrayLength(timeZone);
887     jbyte* timeZone_ptr;
888     timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
889 
890     rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
891 
892     if (timeZone_ptr) {
893         _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
894     }
895 }
896 
897 static void
nScriptSetType(JNIEnv * _env,jobject _this,jint type,jboolean writable,jstring _str,jint slot)898 nScriptSetType(JNIEnv *_env, jobject _this, jint type, jboolean writable, jstring _str, jint slot)
899 {
900     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
901     LOG_API("nScriptCAddType, con(%p), type(%p), writable(%i), slot(%i)", con, (RsType)type, writable, slot);
902     const char* n = NULL;
903     if (_str) {
904         n = _env->GetStringUTFChars(_str, NULL);
905     }
906     rsScriptSetType(con, (RsType)type, slot, writable, n);
907     if (n) {
908         _env->ReleaseStringUTFChars(_str, n);
909     }
910 }
911 
912 static void
nScriptSetInvoke(JNIEnv * _env,jobject _this,jstring _str,jint slot)913 nScriptSetInvoke(JNIEnv *_env, jobject _this, jstring _str, jint slot)
914 {
915     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
916     LOG_API("nScriptSetInvoke, con(%p)", con);
917     const char* n = NULL;
918     if (_str) {
919         n = _env->GetStringUTFChars(_str, NULL);
920     }
921     rsScriptSetInvoke(con, n, slot);
922     if (n) {
923         _env->ReleaseStringUTFChars(_str, n);
924     }
925 }
926 
927 static void
nScriptInvoke(JNIEnv * _env,jobject _this,jint obj,jint slot)928 nScriptInvoke(JNIEnv *_env, jobject _this, jint obj, jint slot)
929 {
930     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
931     LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
932     rsScriptInvoke(con, (RsScript)obj, slot);
933 }
934 
935 static void
nScriptSetRoot(JNIEnv * _env,jobject _this,jboolean isRoot)936 nScriptSetRoot(JNIEnv *_env, jobject _this, jboolean isRoot)
937 {
938     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
939     LOG_API("nScriptCSetRoot, con(%p), isRoot(%i)", con, isRoot);
940     rsScriptSetRoot(con, isRoot);
941 }
942 
943 // -----------------------------------
944 
945 static void
nScriptCBegin(JNIEnv * _env,jobject _this)946 nScriptCBegin(JNIEnv *_env, jobject _this)
947 {
948     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
949     LOG_API("nScriptCBegin, con(%p)", con);
950     rsScriptCBegin(con);
951 }
952 
953 static void
nScriptCSetScript(JNIEnv * _env,jobject _this,jbyteArray scriptRef,jint offset,jint length)954 nScriptCSetScript(JNIEnv *_env, jobject _this, jbyteArray scriptRef,
955                   jint offset, jint length)
956 {
957     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
958     LOG_API("!!! nScriptCSetScript, con(%p)", con);
959     jint _exception = 0;
960     jint remaining;
961     jbyte* script_base = 0;
962     jbyte* script_ptr;
963     if (!scriptRef) {
964         _exception = 1;
965         //_env->ThrowNew(IAEClass, "script == null");
966         goto exit;
967     }
968     if (offset < 0) {
969         _exception = 1;
970         //_env->ThrowNew(IAEClass, "offset < 0");
971         goto exit;
972     }
973     if (length < 0) {
974         _exception = 1;
975         //_env->ThrowNew(IAEClass, "length < 0");
976         goto exit;
977     }
978     remaining = _env->GetArrayLength(scriptRef) - offset;
979     if (remaining < length) {
980         _exception = 1;
981         //_env->ThrowNew(IAEClass, "length > script.length - offset");
982         goto exit;
983     }
984     script_base = (jbyte *)
985         _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
986     script_ptr = script_base + offset;
987 
988     rsScriptCSetText(con, (const char *)script_ptr, length);
989 
990 exit:
991     if (script_base) {
992         _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
993                 _exception ? JNI_ABORT: 0);
994     }
995 }
996 
997 static jint
nScriptCCreate(JNIEnv * _env,jobject _this)998 nScriptCCreate(JNIEnv *_env, jobject _this)
999 {
1000     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1001     LOG_API("nScriptCCreate, con(%p)", con);
1002     return (jint)rsScriptCCreate(con);
1003 }
1004 
1005 static void
nScriptCAddDefineI32(JNIEnv * _env,jobject _this,jstring name,jint value)1006 nScriptCAddDefineI32(JNIEnv *_env, jobject _this, jstring name, jint value)
1007 {
1008     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1009     const char* n = _env->GetStringUTFChars(name, NULL);
1010     LOG_API("nScriptCAddDefineI32, con(%p) name(%s) value(%d)", con, n, value);
1011     rsScriptCSetDefineI32(con, n, value);
1012     _env->ReleaseStringUTFChars(name, n);
1013 }
1014 
1015 static void
nScriptCAddDefineF(JNIEnv * _env,jobject _this,jstring name,jfloat value)1016 nScriptCAddDefineF(JNIEnv *_env, jobject _this, jstring name, jfloat value)
1017 {
1018     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1019     const char* n = _env->GetStringUTFChars(name, NULL);
1020     LOG_API("nScriptCAddDefineF, con(%p) name(%s) value(%f)", con, n, value);
1021     rsScriptCSetDefineF(con, n, value);
1022     _env->ReleaseStringUTFChars(name, n);
1023 }
1024 
1025 // ---------------------------------------------------------------------------
1026 
1027 static void
nProgramFragmentStoreBegin(JNIEnv * _env,jobject _this,jint in,jint out)1028 nProgramFragmentStoreBegin(JNIEnv *_env, jobject _this, jint in, jint out)
1029 {
1030     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1031     LOG_API("nProgramFragmentStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
1032     rsProgramFragmentStoreBegin(con, (RsElement)in, (RsElement)out);
1033 }
1034 
1035 static void
nProgramFragmentStoreDepthFunc(JNIEnv * _env,jobject _this,jint func)1036 nProgramFragmentStoreDepthFunc(JNIEnv *_env, jobject _this, jint func)
1037 {
1038     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1039     LOG_API("nProgramFragmentStoreDepthFunc, con(%p), func(%i)", con, func);
1040     rsProgramFragmentStoreDepthFunc(con, (RsDepthFunc)func);
1041 }
1042 
1043 static void
nProgramFragmentStoreDepthMask(JNIEnv * _env,jobject _this,jboolean enable)1044 nProgramFragmentStoreDepthMask(JNIEnv *_env, jobject _this, jboolean enable)
1045 {
1046     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1047     LOG_API("nProgramFragmentStoreDepthMask, con(%p), enable(%i)", con, enable);
1048     rsProgramFragmentStoreDepthMask(con, enable);
1049 }
1050 
1051 static void
nProgramFragmentStoreColorMask(JNIEnv * _env,jobject _this,jboolean r,jboolean g,jboolean b,jboolean a)1052 nProgramFragmentStoreColorMask(JNIEnv *_env, jobject _this, jboolean r, jboolean g, jboolean b, jboolean a)
1053 {
1054     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1055     LOG_API("nProgramFragmentStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
1056     rsProgramFragmentStoreColorMask(con, r, g, b, a);
1057 }
1058 
1059 static void
nProgramFragmentStoreBlendFunc(JNIEnv * _env,jobject _this,int src,int dst)1060 nProgramFragmentStoreBlendFunc(JNIEnv *_env, jobject _this, int src, int dst)
1061 {
1062     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1063     LOG_API("nProgramFragmentStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
1064     rsProgramFragmentStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
1065 }
1066 
1067 static void
nProgramFragmentStoreDither(JNIEnv * _env,jobject _this,jboolean enable)1068 nProgramFragmentStoreDither(JNIEnv *_env, jobject _this, jboolean enable)
1069 {
1070     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1071     LOG_API("nProgramFragmentStoreDither, con(%p), enable(%i)", con, enable);
1072     rsProgramFragmentStoreDither(con, enable);
1073 }
1074 
1075 static jint
nProgramFragmentStoreCreate(JNIEnv * _env,jobject _this)1076 nProgramFragmentStoreCreate(JNIEnv *_env, jobject _this)
1077 {
1078     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1079     LOG_API("nProgramFragmentStoreCreate, con(%p)", con);
1080 
1081     return (jint)rsProgramFragmentStoreCreate(con);
1082 }
1083 
1084 // ---------------------------------------------------------------------------
1085 
1086 static void
nProgramBindConstants(JNIEnv * _env,jobject _this,jint vpv,jint slot,jint a)1087 nProgramBindConstants(JNIEnv *_env, jobject _this, jint vpv, jint slot, jint a)
1088 {
1089     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1090     LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
1091     rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
1092 }
1093 
1094 static void
nProgramBindTexture(JNIEnv * _env,jobject _this,jint vpf,jint slot,jint a)1095 nProgramBindTexture(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
1096 {
1097     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1098     LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1099     rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1100 }
1101 
1102 static void
nProgramBindSampler(JNIEnv * _env,jobject _this,jint vpf,jint slot,jint a)1103 nProgramBindSampler(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
1104 {
1105     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1106     LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1107     rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1108 }
1109 
1110 // ---------------------------------------------------------------------------
1111 
1112 static jint
nProgramFragmentCreate(JNIEnv * _env,jobject _this,jintArray params)1113 nProgramFragmentCreate(JNIEnv *_env, jobject _this, jintArray params)
1114 {
1115     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1116     jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1117     jint paramLen = _env->GetArrayLength(params);
1118 
1119     LOG_API("nProgramFragmentCreate, con(%p), paramLen(%i)", con, paramLen);
1120 
1121     jint ret = (jint)rsProgramFragmentCreate(con, (uint32_t *)paramPtr, paramLen);
1122     _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1123     return ret;
1124 }
1125 
1126 static jint
nProgramFragmentCreate2(JNIEnv * _env,jobject _this,jstring shader,jintArray params)1127 nProgramFragmentCreate2(JNIEnv *_env, jobject _this, jstring shader, jintArray params)
1128 {
1129     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1130     const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1131     jint shaderLen = _env->GetStringUTFLength(shader);
1132     jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1133     jint paramLen = _env->GetArrayLength(params);
1134 
1135     LOG_API("nProgramFragmentCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1136 
1137     jint ret = (jint)rsProgramFragmentCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1138     _env->ReleaseStringUTFChars(shader, shaderUTF);
1139     _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1140     return ret;
1141 }
1142 
1143 
1144 // ---------------------------------------------------------------------------
1145 
1146 static jint
nProgramVertexCreate(JNIEnv * _env,jobject _this,jboolean texMat)1147 nProgramVertexCreate(JNIEnv *_env, jobject _this, jboolean texMat)
1148 {
1149     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1150     LOG_API("nProgramVertexCreate, con(%p), texMat(%i)", con, texMat);
1151     return (jint)rsProgramVertexCreate(con, texMat);
1152 }
1153 
1154 static jint
nProgramVertexCreate2(JNIEnv * _env,jobject _this,jstring shader,jintArray params)1155 nProgramVertexCreate2(JNIEnv *_env, jobject _this, jstring shader, jintArray params)
1156 {
1157     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1158     const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1159     jint shaderLen = _env->GetStringUTFLength(shader);
1160     jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1161     jint paramLen = _env->GetArrayLength(params);
1162 
1163     LOG_API("nProgramVertexCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1164 
1165     jint ret = (jint)rsProgramVertexCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1166     _env->ReleaseStringUTFChars(shader, shaderUTF);
1167     _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1168     return ret;
1169 }
1170 
1171 // ---------------------------------------------------------------------------
1172 
1173 static jint
nProgramRasterCreate(JNIEnv * _env,jobject _this,jint in,jint out,jboolean pointSmooth,jboolean lineSmooth,jboolean pointSprite)1174 nProgramRasterCreate(JNIEnv *_env, jobject _this, jint in, jint out,
1175                      jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1176 {
1177     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1178     LOG_API("nProgramRasterCreate, con(%p), in(%p), out(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1179             con, (RsElement)in, (RsElement)out, pointSmooth, lineSmooth, pointSprite);
1180     return (jint)rsProgramRasterCreate(con, (RsElement)in, (RsElement)out, pointSmooth, lineSmooth, pointSprite);
1181 }
1182 
1183 static void
nProgramRasterSetPointSize(JNIEnv * _env,jobject _this,jint vpr,jfloat v)1184 nProgramRasterSetPointSize(JNIEnv *_env, jobject _this, jint vpr, jfloat v)
1185 {
1186     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1187     LOG_API("nProgramRasterSetPointSize, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1188     rsProgramRasterSetPointSize(con, (RsProgramFragment)vpr, v);
1189 }
1190 
1191 static void
nProgramRasterSetLineWidth(JNIEnv * _env,jobject _this,jint vpr,jfloat v)1192 nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, jint vpr, jfloat v)
1193 {
1194     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1195     LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1196     rsProgramRasterSetLineWidth(con, (RsProgramFragment)vpr, v);
1197 }
1198 
1199 
1200 // ---------------------------------------------------------------------------
1201 
1202 static void
nContextBindRootScript(JNIEnv * _env,jobject _this,jint script)1203 nContextBindRootScript(JNIEnv *_env, jobject _this, jint script)
1204 {
1205     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1206     LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1207     rsContextBindRootScript(con, (RsScript)script);
1208 }
1209 
1210 static void
nContextBindProgramFragmentStore(JNIEnv * _env,jobject _this,jint pfs)1211 nContextBindProgramFragmentStore(JNIEnv *_env, jobject _this, jint pfs)
1212 {
1213     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1214     LOG_API("nContextBindProgramFragmentStore, con(%p), pfs(%p)", con, (RsProgramFragmentStore)pfs);
1215     rsContextBindProgramFragmentStore(con, (RsProgramFragmentStore)pfs);
1216 }
1217 
1218 static void
nContextBindProgramFragment(JNIEnv * _env,jobject _this,jint pf)1219 nContextBindProgramFragment(JNIEnv *_env, jobject _this, jint pf)
1220 {
1221     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1222     LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1223     rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1224 }
1225 
1226 static void
nContextBindProgramVertex(JNIEnv * _env,jobject _this,jint pf)1227 nContextBindProgramVertex(JNIEnv *_env, jobject _this, jint pf)
1228 {
1229     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1230     LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1231     rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1232 }
1233 
1234 static void
nContextBindProgramRaster(JNIEnv * _env,jobject _this,jint pf)1235 nContextBindProgramRaster(JNIEnv *_env, jobject _this, jint pf)
1236 {
1237     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1238     LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1239     rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1240 }
1241 
1242 
1243 // ---------------------------------------------------------------------------
1244 
1245 static void
nSamplerBegin(JNIEnv * _env,jobject _this)1246 nSamplerBegin(JNIEnv *_env, jobject _this)
1247 {
1248     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1249     LOG_API("nSamplerBegin, con(%p)", con);
1250     rsSamplerBegin(con);
1251 }
1252 
1253 static void
nSamplerSet(JNIEnv * _env,jobject _this,jint p,jint v)1254 nSamplerSet(JNIEnv *_env, jobject _this, jint p, jint v)
1255 {
1256     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1257     LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1258     rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1259 }
1260 
1261 static jint
nSamplerCreate(JNIEnv * _env,jobject _this)1262 nSamplerCreate(JNIEnv *_env, jobject _this)
1263 {
1264     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1265     LOG_API("nSamplerCreate, con(%p)", con);
1266     return (jint)rsSamplerCreate(con);
1267 }
1268 
1269 // ---------------------------------------------------------------------------
1270 
1271 static void
nLightBegin(JNIEnv * _env,jobject _this)1272 nLightBegin(JNIEnv *_env, jobject _this)
1273 {
1274     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1275     LOG_API("nLightBegin, con(%p)", con);
1276     rsLightBegin(con);
1277 }
1278 
1279 static void
nLightSetIsMono(JNIEnv * _env,jobject _this,jboolean isMono)1280 nLightSetIsMono(JNIEnv *_env, jobject _this, jboolean isMono)
1281 {
1282     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1283     LOG_API("nLightSetIsMono, con(%p), isMono(%i)", con, isMono);
1284     rsLightSetMonochromatic(con, isMono);
1285 }
1286 
1287 static void
nLightSetIsLocal(JNIEnv * _env,jobject _this,jboolean isLocal)1288 nLightSetIsLocal(JNIEnv *_env, jobject _this, jboolean isLocal)
1289 {
1290     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1291     LOG_API("nLightSetIsLocal, con(%p), isLocal(%i)", con, isLocal);
1292     rsLightSetLocal(con, isLocal);
1293 }
1294 
1295 static jint
nLightCreate(JNIEnv * _env,jobject _this)1296 nLightCreate(JNIEnv *_env, jobject _this)
1297 {
1298     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1299     LOG_API("nLightCreate, con(%p)", con);
1300     return (jint)rsLightCreate(con);
1301 }
1302 
1303 static void
nLightSetColor(JNIEnv * _env,jobject _this,jint light,float r,float g,float b)1304 nLightSetColor(JNIEnv *_env, jobject _this, jint light, float r, float g, float b)
1305 {
1306     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1307     LOG_API("nLightSetColor, con(%p), light(%p), r(%f), g(%f), b(%f)", con, (RsLight)light, r, g, b);
1308     rsLightSetColor(con, (RsLight)light, r, g, b);
1309 }
1310 
1311 static void
nLightSetPosition(JNIEnv * _env,jobject _this,jint light,float x,float y,float z)1312 nLightSetPosition(JNIEnv *_env, jobject _this, jint light, float x, float y, float z)
1313 {
1314     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1315     LOG_API("nLightSetPosition, con(%p), light(%p), x(%f), y(%f), z(%f)", con, (RsLight)light, x, y, z);
1316     rsLightSetPosition(con, (RsLight)light, x, y, z);
1317 }
1318 
1319 // ---------------------------------------------------------------------------
1320 
1321 static jint
nSimpleMeshCreate(JNIEnv * _env,jobject _this,jint batchID,jint indexID,jintArray vtxIDs,jint primID)1322 nSimpleMeshCreate(JNIEnv *_env, jobject _this, jint batchID, jint indexID, jintArray vtxIDs, jint primID)
1323 {
1324     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1325     jint len = _env->GetArrayLength(vtxIDs);
1326     LOG_API("nSimpleMeshCreate, con(%p), batchID(%i), indexID(%i), vtxIDs.len(%i), primID(%i)",
1327             con, batchID, indexID, len, primID);
1328     jint *ptr = _env->GetIntArrayElements(vtxIDs, NULL);
1329     int id = (int)rsSimpleMeshCreate(con, (void *)batchID, (void *)indexID, (void **)ptr, len, primID);
1330     _env->ReleaseIntArrayElements(vtxIDs, ptr, 0/*JNI_ABORT*/);
1331     return id;
1332 }
1333 
1334 static void
nSimpleMeshBindVertex(JNIEnv * _env,jobject _this,jint s,jint alloc,jint slot)1335 nSimpleMeshBindVertex(JNIEnv *_env, jobject _this, jint s, jint alloc, jint slot)
1336 {
1337     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1338     LOG_API("nSimpleMeshBindVertex, con(%p), SimpleMesh(%p), Alloc(%p), slot(%i)", con, (RsSimpleMesh)s, (RsAllocation)alloc, slot);
1339     rsSimpleMeshBindVertex(con, (RsSimpleMesh)s, (RsAllocation)alloc, slot);
1340 }
1341 
1342 static void
nSimpleMeshBindIndex(JNIEnv * _env,jobject _this,jint s,jint alloc)1343 nSimpleMeshBindIndex(JNIEnv *_env, jobject _this, jint s, jint alloc)
1344 {
1345     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1346     LOG_API("nSimpleMeshBindIndex, con(%p), SimpleMesh(%p), Alloc(%p)", con, (RsSimpleMesh)s, (RsAllocation)alloc);
1347     rsSimpleMeshBindIndex(con, (RsSimpleMesh)s, (RsAllocation)alloc);
1348 }
1349 
1350 // ---------------------------------------------------------------------------
1351 
1352 
1353 static const char *classPathName = "android/renderscript/RenderScript";
1354 
1355 static JNINativeMethod methods[] = {
1356 {"_nInit",                         "()V",                                  (void*)_nInit },
1357 {"nInitElements",                  "(IIII)V",                              (void*)nInitElements },
1358 
1359 {"nDeviceCreate",                  "()I",                                  (void*)nDeviceCreate },
1360 {"nDeviceDestroy",                 "(I)V",                                 (void*)nDeviceDestroy },
1361 {"nDeviceSetConfig",               "(III)V",                               (void*)nDeviceSetConfig },
1362 {"nContextCreate",                 "(II)I",                                (void*)nContextCreate },
1363 {"nContextCreateGL",               "(IIZ)I",                               (void*)nContextCreateGL },
1364 {"nContextSetPriority",            "(I)V",                                 (void*)nContextSetPriority },
1365 {"nContextSetSurface",             "(IILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1366 {"nContextDestroy",                "(I)V",                                 (void*)nContextDestroy },
1367 {"nContextDump",                   "(I)V",                                 (void*)nContextDump },
1368 {"nContextPause",                  "()V",                                  (void*)nContextPause },
1369 {"nContextResume",                 "()V",                                  (void*)nContextResume },
1370 {"nAssignName",                    "(I[B)V",                               (void*)nAssignName },
1371 {"nObjDestroy",                    "(I)V",                                 (void*)nObjDestroy },
1372 {"nObjDestroyOOB",                 "(I)V",                                 (void*)nObjDestroyOOB },
1373 {"nContextGetMessage",             "([IZ)I",                               (void*)nContextGetMessage },
1374 {"nContextInitToClient",           "()V",                                  (void*)nContextInitToClient },
1375 {"nContextDeinitToClient",         "()V",                                  (void*)nContextDeinitToClient },
1376 
1377 {"nFileOpen",                      "([B)I",                                (void*)nFileOpen },
1378 
1379 {"nElementCreate",                 "(IIZI)I",                              (void*)nElementCreate },
1380 {"nElementCreate2",                "([I[Ljava/lang/String;)I",             (void*)nElementCreate2 },
1381 
1382 {"nTypeBegin",                     "(I)V",                                 (void*)nTypeBegin },
1383 {"nTypeAdd",                       "(II)V",                                (void*)nTypeAdd },
1384 {"nTypeCreate",                    "()I",                                  (void*)nTypeCreate },
1385 {"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
1386 {"nTypeSetupFields",               "(Landroid/renderscript/Type;[I[I[Ljava/lang/reflect/Field;)V", (void*)nTypeSetupFields },
1387 
1388 {"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
1389 {"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
1390 {"nAllocationCreateBitmapRef",     "(ILandroid/graphics/Bitmap;)I",        (void*)nAllocationCreateBitmapRef },
1391 {"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
1392 {"nAllocationCreateFromAssetStream","(IZI)I",                              (void*)nAllocationCreateFromAssetStream },
1393 {"nAllocationUploadToTexture",     "(IZI)V",                               (void*)nAllocationUploadToTexture },
1394 {"nAllocationUploadToBufferObject","(I)V",                                 (void*)nAllocationUploadToBufferObject },
1395 {"nAllocationSubData1D",           "(III[II)V",                            (void*)nAllocationSubData1D_i },
1396 {"nAllocationSubData1D",           "(III[SI)V",                            (void*)nAllocationSubData1D_s },
1397 {"nAllocationSubData1D",           "(III[BI)V",                            (void*)nAllocationSubData1D_b },
1398 {"nAllocationSubData1D",           "(III[FI)V",                            (void*)nAllocationSubData1D_f },
1399 {"nAllocationSubData2D",           "(IIIII[II)V",                          (void*)nAllocationSubData2D_i },
1400 {"nAllocationSubData2D",           "(IIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1401 {"nAllocationRead",                "(I[I)V",                               (void*)nAllocationRead_i },
1402 {"nAllocationRead",                "(I[F)V",                               (void*)nAllocationRead_f },
1403 {"nAllocationSubDataFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubDataFromObject },
1404 {"nAllocationSubReadFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubReadFromObject },
1405 
1406 {"nAdapter1DBindAllocation",       "(II)V",                                (void*)nAdapter1DBindAllocation },
1407 {"nAdapter1DSetConstraint",        "(III)V",                               (void*)nAdapter1DSetConstraint },
1408 {"nAdapter1DData",                 "(I[I)V",                               (void*)nAdapter1DData_i },
1409 {"nAdapter1DData",                 "(I[F)V",                               (void*)nAdapter1DData_f },
1410 {"nAdapter1DSubData",              "(III[I)V",                             (void*)nAdapter1DSubData_i },
1411 {"nAdapter1DSubData",              "(III[F)V",                             (void*)nAdapter1DSubData_f },
1412 {"nAdapter1DCreate",               "()I",                                  (void*)nAdapter1DCreate },
1413 
1414 {"nAdapter2DBindAllocation",       "(II)V",                                (void*)nAdapter2DBindAllocation },
1415 {"nAdapter2DSetConstraint",        "(III)V",                               (void*)nAdapter2DSetConstraint },
1416 {"nAdapter2DData",                 "(I[I)V",                               (void*)nAdapter2DData_i },
1417 {"nAdapter2DData",                 "(I[F)V",                               (void*)nAdapter2DData_f },
1418 {"nAdapter2DSubData",              "(IIIII[I)V",                           (void*)nAdapter2DSubData_i },
1419 {"nAdapter2DSubData",              "(IIIII[F)V",                           (void*)nAdapter2DSubData_f },
1420 {"nAdapter2DCreate",               "()I",                                  (void*)nAdapter2DCreate },
1421 
1422 {"nScriptBindAllocation",          "(III)V",                               (void*)nScriptBindAllocation },
1423 {"nScriptSetClearColor",           "(IFFFF)V",                             (void*)nScriptSetClearColor },
1424 {"nScriptSetClearDepth",           "(IF)V",                                (void*)nScriptSetClearDepth },
1425 {"nScriptSetClearStencil",         "(II)V",                                (void*)nScriptSetClearStencil },
1426 {"nScriptSetTimeZone",             "(I[B)V",                               (void*)nScriptSetTimeZone },
1427 {"nScriptSetType",                 "(IZLjava/lang/String;I)V",             (void*)nScriptSetType },
1428 {"nScriptSetRoot",                 "(Z)V",                                 (void*)nScriptSetRoot },
1429 {"nScriptSetInvokable",            "(Ljava/lang/String;I)V",               (void*)nScriptSetInvoke },
1430 {"nScriptInvoke",                  "(II)V",                                (void*)nScriptInvoke },
1431 
1432 {"nScriptCBegin",                  "()V",                                  (void*)nScriptCBegin },
1433 {"nScriptCSetScript",              "([BII)V",                              (void*)nScriptCSetScript },
1434 {"nScriptCCreate",                 "()I",                                  (void*)nScriptCCreate },
1435 {"nScriptCAddDefineI32",           "(Ljava/lang/String;I)V",               (void*)nScriptCAddDefineI32 },
1436 {"nScriptCAddDefineF",             "(Ljava/lang/String;F)V",               (void*)nScriptCAddDefineF },
1437 
1438 {"nProgramFragmentStoreBegin",     "(II)V",                                (void*)nProgramFragmentStoreBegin },
1439 {"nProgramFragmentStoreDepthFunc", "(I)V",                                 (void*)nProgramFragmentStoreDepthFunc },
1440 {"nProgramFragmentStoreDepthMask", "(Z)V",                                 (void*)nProgramFragmentStoreDepthMask },
1441 {"nProgramFragmentStoreColorMask", "(ZZZZ)V",                              (void*)nProgramFragmentStoreColorMask },
1442 {"nProgramFragmentStoreBlendFunc", "(II)V",                                (void*)nProgramFragmentStoreBlendFunc },
1443 {"nProgramFragmentStoreDither",    "(Z)V",                                 (void*)nProgramFragmentStoreDither },
1444 {"nProgramFragmentStoreCreate",    "()I",                                  (void*)nProgramFragmentStoreCreate },
1445 
1446 {"nProgramBindConstants",          "(III)V",                               (void*)nProgramBindConstants },
1447 {"nProgramBindTexture",            "(III)V",                               (void*)nProgramBindTexture },
1448 {"nProgramBindSampler",            "(III)V",                               (void*)nProgramBindSampler },
1449 
1450 {"nProgramFragmentCreate",         "([I)I",                                (void*)nProgramFragmentCreate },
1451 {"nProgramFragmentCreate2",        "(Ljava/lang/String;[I)I",              (void*)nProgramFragmentCreate2 },
1452 
1453 {"nProgramRasterCreate",           "(IIZZZ)I",                             (void*)nProgramRasterCreate },
1454 {"nProgramRasterSetPointSize",     "(IF)V",                                (void*)nProgramRasterSetPointSize },
1455 {"nProgramRasterSetLineWidth",     "(IF)V",                                (void*)nProgramRasterSetLineWidth },
1456 
1457 {"nProgramVertexCreate",           "(Z)I",                                 (void*)nProgramVertexCreate },
1458 {"nProgramVertexCreate2",          "(Ljava/lang/String;[I)I",              (void*)nProgramVertexCreate2 },
1459 
1460 {"nLightBegin",                    "()V",                                  (void*)nLightBegin },
1461 {"nLightSetIsMono",                "(Z)V",                                 (void*)nLightSetIsMono },
1462 {"nLightSetIsLocal",               "(Z)V",                                 (void*)nLightSetIsLocal },
1463 {"nLightCreate",                   "()I",                                  (void*)nLightCreate },
1464 {"nLightSetColor",                 "(IFFF)V",                              (void*)nLightSetColor },
1465 {"nLightSetPosition",              "(IFFF)V",                              (void*)nLightSetPosition },
1466 
1467 {"nContextBindRootScript",         "(I)V",                                 (void*)nContextBindRootScript },
1468 {"nContextBindProgramFragmentStore","(I)V",                                (void*)nContextBindProgramFragmentStore },
1469 {"nContextBindProgramFragment",    "(I)V",                                 (void*)nContextBindProgramFragment },
1470 {"nContextBindProgramVertex",      "(I)V",                                 (void*)nContextBindProgramVertex },
1471 {"nContextBindProgramRaster",      "(I)V",                                 (void*)nContextBindProgramRaster },
1472 
1473 {"nSamplerBegin",                  "()V",                                  (void*)nSamplerBegin },
1474 {"nSamplerSet",                    "(II)V",                                (void*)nSamplerSet },
1475 {"nSamplerCreate",                 "()I",                                  (void*)nSamplerCreate },
1476 
1477 {"nSimpleMeshCreate",              "(II[II)I",                             (void*)nSimpleMeshCreate },
1478 {"nSimpleMeshBindVertex",          "(III)V",                               (void*)nSimpleMeshBindVertex },
1479 {"nSimpleMeshBindIndex",           "(II)V",                                (void*)nSimpleMeshBindIndex },
1480 
1481 };
1482 
registerFuncs(JNIEnv * _env)1483 static int registerFuncs(JNIEnv *_env)
1484 {
1485     return android::AndroidRuntime::registerNativeMethods(
1486             _env, classPathName, methods, NELEM(methods));
1487 }
1488 
1489 // ---------------------------------------------------------------------------
1490 
JNI_OnLoad(JavaVM * vm,void * reserved)1491 jint JNI_OnLoad(JavaVM* vm, void* reserved)
1492 {
1493     JNIEnv* env = NULL;
1494     jint result = -1;
1495 
1496     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1497         LOGE("ERROR: GetEnv failed\n");
1498         goto bail;
1499     }
1500     assert(env != NULL);
1501 
1502     if (registerFuncs(env) < 0) {
1503         LOGE("ERROR: MediaPlayer native registration failed\n");
1504         goto bail;
1505     }
1506 
1507     /* success -- return valid version number */
1508     result = JNI_VERSION_1_4;
1509 
1510 bail:
1511     return result;
1512 }
1513