blob: 9c8775a47276454d366ad7d1fd37d353e4d16023 [file] [log] [blame]
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001/*
Stephen Hinesbe74bdd2012-02-03 15:29:36 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jack Palevich60aa3ea2009-05-26 13:45:08 -07003 *
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
Jason Sams94d8e90a2009-06-10 16:09:05 -070017package android.renderscript;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070018
Jason Samsa6f338c2012-02-24 16:22:16 -080019import java.io.File;
Jason Samsfe1d5ff2012-03-23 11:47:26 -070020import java.lang.reflect.Field;
Tim Murray2f2472c2013-08-22 14:55:26 -070021import java.lang.reflect.Method;
Tim Murray504abb32014-01-07 11:13:56 -080022import java.util.concurrent.locks.ReentrantReadWriteLock;
Jason Sams36e612a2009-07-31 16:26:13 -070023
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080024import android.content.Context;
Stephen Hines4382467a2011-08-01 15:02:34 -070025import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageManager;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080027import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070028import android.graphics.Bitmap;
Romain Guy650a3eb2009-08-31 14:06:43 -070029import android.graphics.BitmapFactory;
Jason Samsfaa32b32011-06-20 16:58:04 -070030import android.graphics.SurfaceTexture;
Glenn Kasten260c77a2011-06-01 17:25:54 -070031import android.os.Process;
Jason Sams36e612a2009-07-31 16:26:13 -070032import android.util.Log;
33import android.view.Surface;
Dan Morrille4d9a012013-03-28 18:10:43 -070034import android.os.SystemProperties;
Tim Murray6d7a53c2013-05-23 16:59:23 -070035import android.os.Trace;
Stephen Hines4382467a2011-08-01 15:02:34 -070036
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070037/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070038 * This class provides access to a RenderScript context, which controls RenderScript
39 * initialization, resource management, and teardown. An instance of the RenderScript
40 * class must be created before any other RS objects can be created.
Jason Sams27676fe2010-11-10 17:00:59 -080041 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080042 * <div class="special reference">
43 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070044 * <p>For more information about creating an application that uses RenderScript, read the
45 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080046 * </div>
Jason Samse29d4712009-07-23 15:19:03 -070047 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070048public class RenderScript {
Tim Murray6d7a53c2013-05-23 16:59:23 -070049 static final long TRACE_TAG = Trace.TRACE_TAG_RS;
50
Jason Sams3bc47d42009-11-12 15:10:25 -080051 static final String LOG_TAG = "RenderScript_jni";
Jason Samsbf6ef8d2010-12-06 15:59:59 -080052 static final boolean DEBUG = false;
Romain Guy650a3eb2009-08-31 14:06:43 -070053 @SuppressWarnings({"UnusedDeclaration", "deprecation"})
Joe Onorato43a17652011-04-06 19:22:23 -070054 static final boolean LOG_ENABLED = false;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070055
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080056 private Context mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070057
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080058 /*
Jack Palevich60aa3ea2009-05-26 13:45:08 -070059 * We use a class initializer to allow the native code to cache some
60 * field offsets.
61 */
Dan Morrille4d9a012013-03-28 18:10:43 -070062 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) // TODO: now used locally; remove?
Jason Samsbf6ef8d2010-12-06 15:59:59 -080063 static boolean sInitialized;
64 native static void _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070065
Tim Murray2f2472c2013-08-22 14:55:26 -070066 static Object sRuntime;
67 static Method registerNativeAllocation;
68 static Method registerNativeFree;
Jason Samsdba3ba52009-07-30 14:56:12 -070069
Jack Palevich60aa3ea2009-05-26 13:45:08 -070070 static {
71 sInitialized = false;
Dan Morrille4d9a012013-03-28 18:10:43 -070072 if (!SystemProperties.getBoolean("config.disable_renderscript", false)) {
73 try {
Tim Murray2f2472c2013-08-22 14:55:26 -070074 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
75 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
76 sRuntime = get_runtime.invoke(null);
77 registerNativeAllocation = vm_runtime.getDeclaredMethod("registerNativeAllocation", Integer.TYPE);
78 registerNativeFree = vm_runtime.getDeclaredMethod("registerNativeFree", Integer.TYPE);
79 } catch (Exception e) {
80 Log.e(LOG_TAG, "Error loading GC methods: " + e);
81 throw new RSRuntimeException("Error loading GC methods: " + e);
82 }
83 try {
Dan Morrille4d9a012013-03-28 18:10:43 -070084 System.loadLibrary("rs_jni");
85 _nInit();
86 sInitialized = true;
87 } catch (UnsatisfiedLinkError e) {
88 Log.e(LOG_TAG, "Error loading RS jni library: " + e);
89 throw new RSRuntimeException("Error loading RS jni library: " + e);
90 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070091 }
92 }
93
Jason Sams2e1872f2010-08-17 16:25:41 -070094 // Non-threadsafe functions.
Tim Murraya78e9ad2013-11-15 13:08:30 -080095 native long nDeviceCreate();
96 native void nDeviceDestroy(long dev);
97 native void nDeviceSetConfig(long dev, int param, int value);
98 native int nContextGetUserMessage(long con, int[] data);
99 native String nContextGetErrorMessage(long con);
100 native int nContextPeekMessage(long con, int[] subID);
101 native void nContextInitToClient(long con);
102 native void nContextDeinitToClient(long con);
Jason Sams3eaa3382009-06-10 15:04:38 -0700103
Stephen Hines7d25a822013-04-09 23:51:56 -0700104 static File mCacheDir;
Jason Samsa6f338c2012-02-24 16:22:16 -0800105
106 /**
107 * Sets the directory to use as a persistent storage for the
108 * renderscript object file cache.
109 *
110 * @hide
111 * @param cacheDir A directory the current process can write to
112 */
Jason Samsa6f338c2012-02-24 16:22:16 -0800113 public static void setupDiskCache(File cacheDir) {
Dan Morrille4d9a012013-03-28 18:10:43 -0700114 if (!sInitialized) {
115 Log.e(LOG_TAG, "RenderScript.setupDiskCache() called when disabled");
116 return;
117 }
118
Stephen Hines7d25a822013-04-09 23:51:56 -0700119 // Defer creation of cache path to nScriptCCreate().
120 mCacheDir = cacheDir;
Jason Samsa6f338c2012-02-24 16:22:16 -0800121 }
122
Jason Sams02d56d92013-04-12 16:40:50 -0700123 /**
124 * ContextType specifies the specific type of context to be created.
125 *
126 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800127 public enum ContextType {
Jason Sams02d56d92013-04-12 16:40:50 -0700128 /**
129 * NORMAL context, this is the default and what shipping apps should
130 * use.
131 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800132 NORMAL (0),
Jason Sams02d56d92013-04-12 16:40:50 -0700133
134 /**
135 * DEBUG context, perform extra runtime checks to validate the
136 * kernels and APIs are being used as intended. Get and SetElementAt
137 * will be bounds checked in this mode.
138 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800139 DEBUG (1),
Jason Sams02d56d92013-04-12 16:40:50 -0700140
141 /**
142 * PROFILE context, Intended to be used once the first time an
143 * application is run on a new device. This mode allows the runtime to
144 * do additional testing and performance tuning.
145 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800146 PROFILE (2);
147
148 int mID;
149 ContextType(int id) {
150 mID = id;
151 }
152 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800153
Stephen Hines42028a82013-04-17 19:22:01 -0700154 ContextType mContextType;
Tim Murray504abb32014-01-07 11:13:56 -0800155 ReentrantReadWriteLock mRWLock;
Stephen Hines42028a82013-04-17 19:22:01 -0700156
Jason Sams2e1872f2010-08-17 16:25:41 -0700157 // Methods below are wrapped to protect the non-threadsafe
158 // lockless fifo.
Tim Murraya78e9ad2013-11-15 13:08:30 -0800159 native long rsnContextCreateGL(long dev, int ver, int sdkVer,
Jason Sams11c8af92010-10-13 15:31:10 -0700160 int colorMin, int colorPref,
161 int alphaMin, int alphaPref,
162 int depthMin, int depthPref,
163 int stencilMin, int stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700164 int samplesMin, int samplesPref, float samplesQ, int dpi);
Tim Murraya78e9ad2013-11-15 13:08:30 -0800165 synchronized long nContextCreateGL(long dev, int ver, int sdkVer,
Jason Sams11c8af92010-10-13 15:31:10 -0700166 int colorMin, int colorPref,
167 int alphaMin, int alphaPref,
168 int depthMin, int depthPref,
169 int stencilMin, int stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700170 int samplesMin, int samplesPref, float samplesQ, int dpi) {
Stephen Hines4382467a2011-08-01 15:02:34 -0700171 return rsnContextCreateGL(dev, ver, sdkVer, colorMin, colorPref,
Jason Sams11c8af92010-10-13 15:31:10 -0700172 alphaMin, alphaPref, depthMin, depthPref,
173 stencilMin, stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700174 samplesMin, samplesPref, samplesQ, dpi);
Jason Sams2e1872f2010-08-17 16:25:41 -0700175 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800176 native long rsnContextCreate(long dev, int ver, int sdkVer, int contextType);
177 synchronized long nContextCreate(long dev, int ver, int sdkVer, int contextType) {
Jason Samsadd26dc2013-02-22 18:43:45 -0800178 return rsnContextCreate(dev, ver, sdkVer, contextType);
Jason Sams2e1872f2010-08-17 16:25:41 -0700179 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800180 native void rsnContextDestroy(long con);
Jason Sams2e1872f2010-08-17 16:25:41 -0700181 synchronized void nContextDestroy() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800182 validate();
Tim Murray504abb32014-01-07 11:13:56 -0800183
184 // take teardown lock
185 // teardown lock can only be taken when no objects are being destroyed
186 ReentrantReadWriteLock.WriteLock wlock = mRWLock.writeLock();
187 wlock.lock();
188
189 long curCon = mContext;
190 // context is considered dead as of this point
191 mContext = 0;
192
193 wlock.unlock();
194 rsnContextDestroy(curCon);
Jason Sams2e1872f2010-08-17 16:25:41 -0700195 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800196 native void rsnContextSetSurface(long con, int w, int h, Surface sur);
Jason Sams2e1872f2010-08-17 16:25:41 -0700197 synchronized void nContextSetSurface(int w, int h, Surface sur) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800198 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700199 rsnContextSetSurface(mContext, w, h, sur);
200 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800201 native void rsnContextSetSurfaceTexture(long con, int w, int h, SurfaceTexture sur);
Jason Samsfaa32b32011-06-20 16:58:04 -0700202 synchronized void nContextSetSurfaceTexture(int w, int h, SurfaceTexture sur) {
203 validate();
204 rsnContextSetSurfaceTexture(mContext, w, h, sur);
205 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800206 native void rsnContextSetPriority(long con, int p);
Jason Sams2e1872f2010-08-17 16:25:41 -0700207 synchronized void nContextSetPriority(int p) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800208 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700209 rsnContextSetPriority(mContext, p);
210 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800211 native void rsnContextDump(long con, int bits);
Jason Sams2e1872f2010-08-17 16:25:41 -0700212 synchronized void nContextDump(int bits) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800213 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700214 rsnContextDump(mContext, bits);
215 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800216 native void rsnContextFinish(long con);
Jason Sams2e1872f2010-08-17 16:25:41 -0700217 synchronized void nContextFinish() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800218 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700219 rsnContextFinish(mContext);
220 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700221
Tim Murraya78e9ad2013-11-15 13:08:30 -0800222 native void rsnContextSendMessage(long con, int id, int[] data);
Jason Sams455d6442013-02-05 19:20:18 -0800223 synchronized void nContextSendMessage(int id, int[] data) {
224 validate();
225 rsnContextSendMessage(mContext, id, data);
226 }
227
Tim Murraya78e9ad2013-11-15 13:08:30 -0800228 native void rsnContextBindRootScript(long con, int script);
Jason Sams2e1872f2010-08-17 16:25:41 -0700229 synchronized void nContextBindRootScript(int script) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800230 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700231 rsnContextBindRootScript(mContext, script);
232 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800233 native void rsnContextBindSampler(long con, int sampler, int slot);
Jason Sams2e1872f2010-08-17 16:25:41 -0700234 synchronized void nContextBindSampler(int sampler, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800235 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700236 rsnContextBindSampler(mContext, sampler, slot);
237 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800238 native void rsnContextBindProgramStore(long con, int pfs);
Jason Sams2e1872f2010-08-17 16:25:41 -0700239 synchronized void nContextBindProgramStore(int pfs) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800240 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700241 rsnContextBindProgramStore(mContext, pfs);
242 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800243 native void rsnContextBindProgramFragment(long con, int pf);
Jason Sams2e1872f2010-08-17 16:25:41 -0700244 synchronized void nContextBindProgramFragment(int pf) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800245 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700246 rsnContextBindProgramFragment(mContext, pf);
247 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800248 native void rsnContextBindProgramVertex(long con, int pv);
Jason Sams2e1872f2010-08-17 16:25:41 -0700249 synchronized void nContextBindProgramVertex(int pv) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800250 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700251 rsnContextBindProgramVertex(mContext, pv);
252 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800253 native void rsnContextBindProgramRaster(long con, int pr);
Jason Sams2e1872f2010-08-17 16:25:41 -0700254 synchronized void nContextBindProgramRaster(int pr) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800255 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700256 rsnContextBindProgramRaster(mContext, pr);
257 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800258 native void rsnContextPause(long con);
Jason Sams2e1872f2010-08-17 16:25:41 -0700259 synchronized void nContextPause() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800260 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700261 rsnContextPause(mContext);
262 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800263 native void rsnContextResume(long con);
Jason Sams2e1872f2010-08-17 16:25:41 -0700264 synchronized void nContextResume() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800265 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700266 rsnContextResume(mContext);
267 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700268
Tim Murray7a629fa2013-11-19 12:45:54 -0800269 native void rsnAssignName(long con, long obj, byte[] name);
270 synchronized void nAssignName(long obj, byte[] name) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800271 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700272 rsnAssignName(mContext, obj, name);
273 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800274 native String rsnGetName(long con, long obj);
275 synchronized String nGetName(long obj) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800276 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700277 return rsnGetName(mContext, obj);
278 }
Tim Murray504abb32014-01-07 11:13:56 -0800279
280 // nObjDestroy is explicitly _not_ synchronous to prevent crashes in finalizers
Tim Murray7a629fa2013-11-19 12:45:54 -0800281 native void rsnObjDestroy(long con, long id);
Tim Murray504abb32014-01-07 11:13:56 -0800282 void nObjDestroy(long id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800283 // There is a race condition here. The calling code may be run
284 // by the gc while teardown is occuring. This protects againts
285 // deleting dead objects.
286 if (mContext != 0) {
287 rsnObjDestroy(mContext, id);
288 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700289 }
Jason Samsfe08d992009-05-27 14:45:32 -0700290
Tim Murray7a629fa2013-11-19 12:45:54 -0800291 native long rsnElementCreate(long con, long type, int kind, boolean norm, int vecSize);
292 synchronized long nElementCreate(long type, int kind, boolean norm, int vecSize) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800293 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700294 return rsnElementCreate(mContext, type, kind, norm, vecSize);
295 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800296 native long rsnElementCreate2(long con, int[]elements, String[] names, int[] arraySizes);
297 synchronized long nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800298 validate();
Jason Sams70d4e502010-09-02 17:35:23 -0700299 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700300 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800301 native void rsnElementGetNativeData(long con, long id, int[] elementData);
302 synchronized void nElementGetNativeData(long id, int[] elementData) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800303 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700304 rsnElementGetNativeData(mContext, id, elementData);
305 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800306 native void rsnElementGetSubElements(long con, long id,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700307 int[] IDs, String[] names, int[] arraySizes);
Tim Murray7a629fa2013-11-19 12:45:54 -0800308 synchronized void nElementGetSubElements(long id, int[] IDs, String[] names, int[] arraySizes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800309 validate();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700310 rsnElementGetSubElements(mContext, id, IDs, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700311 }
Jason Sams768bc022009-09-21 19:41:04 -0700312
Tim Murray7a629fa2013-11-19 12:45:54 -0800313 native long rsnTypeCreate(long con, long eid, int x, int y, int z, boolean mips, boolean faces, int yuv);
314 synchronized long nTypeCreate(long eid, int x, int y, int z, boolean mips, boolean faces, int yuv) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800315 validate();
Jason Samsb109cc72013-01-07 18:20:12 -0800316 return rsnTypeCreate(mContext, eid, x, y, z, mips, faces, yuv);
Jason Sams2e1872f2010-08-17 16:25:41 -0700317 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800318 native void rsnTypeGetNativeData(long con, long id, int[] typeData);
319 synchronized void nTypeGetNativeData(long id, int[] typeData) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800320 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700321 rsnTypeGetNativeData(mContext, id, typeData);
322 }
Jason Sams768bc022009-09-21 19:41:04 -0700323
Tim Murray7a629fa2013-11-19 12:45:54 -0800324 native long rsnAllocationCreateTyped(long con, long type, int mip, int usage, int pointer);
325 synchronized long nAllocationCreateTyped(long type, int mip, int usage, int pointer) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800326 validate();
Jason Sams857d0c72011-11-23 15:02:15 -0800327 return rsnAllocationCreateTyped(mContext, type, mip, usage, pointer);
Jason Sams2e1872f2010-08-17 16:25:41 -0700328 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800329 native long rsnAllocationCreateFromBitmap(long con, long type, int mip, Bitmap bmp, int usage);
330 synchronized long nAllocationCreateFromBitmap(long type, int mip, Bitmap bmp, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800331 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800332 return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700333 }
Tim Murraya3145512012-12-04 17:59:29 -0800334
Tim Murray7a629fa2013-11-19 12:45:54 -0800335 native long rsnAllocationCreateBitmapBackedAllocation(long con, long type, int mip, Bitmap bmp, int usage);
336 synchronized long nAllocationCreateBitmapBackedAllocation(long type, int mip, Bitmap bmp, int usage) {
Tim Murraya3145512012-12-04 17:59:29 -0800337 validate();
338 return rsnAllocationCreateBitmapBackedAllocation(mContext, type, mip, bmp, usage);
339 }
340
Tim Murray7a629fa2013-11-19 12:45:54 -0800341 native long rsnAllocationCubeCreateFromBitmap(long con, long type, int mip, Bitmap bmp, int usage);
342 synchronized long nAllocationCubeCreateFromBitmap(long type, int mip, Bitmap bmp, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800343 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800344 return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800345 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800346 native long rsnAllocationCreateBitmapRef(long con, long type, Bitmap bmp);
347 synchronized long nAllocationCreateBitmapRef(long type, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800348 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700349 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
350 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800351 native long rsnAllocationCreateFromAssetStream(long con, int mips, int assetStream, int usage);
352 synchronized long nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800353 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800354 return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
355 }
356
Tim Murray7a629fa2013-11-19 12:45:54 -0800357 native void rsnAllocationCopyToBitmap(long con, long alloc, Bitmap bmp);
358 synchronized void nAllocationCopyToBitmap(long alloc, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800359 validate();
Jason Sams4ef66502010-12-10 16:03:15 -0800360 rsnAllocationCopyToBitmap(mContext, alloc, bmp);
361 }
362
363
Tim Murray7a629fa2013-11-19 12:45:54 -0800364 native void rsnAllocationSyncAll(long con, long alloc, int src);
365 synchronized void nAllocationSyncAll(long alloc, int src) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800366 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800367 rsnAllocationSyncAll(mContext, alloc, src);
368 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800369 native Surface rsnAllocationGetSurface(long con, long alloc);
370 synchronized Surface nAllocationGetSurface(long alloc) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800371 validate();
Jason Sams72226e02013-02-22 12:45:54 -0800372 return rsnAllocationGetSurface(mContext, alloc);
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700373 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800374 native void rsnAllocationSetSurface(long con, long alloc, Surface sur);
375 synchronized void nAllocationSetSurface(long alloc, Surface sur) {
Jason Sams163766c2012-02-15 12:04:24 -0800376 validate();
Jason Samsfb9aa9f2012-03-28 15:30:07 -0700377 rsnAllocationSetSurface(mContext, alloc, sur);
Jason Sams163766c2012-02-15 12:04:24 -0800378 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800379 native void rsnAllocationIoSend(long con, long alloc);
380 synchronized void nAllocationIoSend(long alloc) {
Jason Sams163766c2012-02-15 12:04:24 -0800381 validate();
382 rsnAllocationIoSend(mContext, alloc);
383 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800384 native void rsnAllocationIoReceive(long con, long alloc);
385 synchronized void nAllocationIoReceive(long alloc) {
Jason Sams163766c2012-02-15 12:04:24 -0800386 validate();
387 rsnAllocationIoReceive(mContext, alloc);
388 }
389
Jason Sams615e7ce2012-01-13 14:01:20 -0800390
Tim Murray7a629fa2013-11-19 12:45:54 -0800391 native void rsnAllocationGenerateMipmaps(long con, long alloc);
392 synchronized void nAllocationGenerateMipmaps(long alloc) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800393 validate();
Jason Samsf7086092011-01-12 13:28:37 -0800394 rsnAllocationGenerateMipmaps(mContext, alloc);
395 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800396 native void rsnAllocationCopyFromBitmap(long con, long alloc, Bitmap bmp);
397 synchronized void nAllocationCopyFromBitmap(long alloc, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800398 validate();
Jason Sams4ef66502010-12-10 16:03:15 -0800399 rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
Jason Sams2e1872f2010-08-17 16:25:41 -0700400 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700401
Jason Sams49a05d72010-12-29 14:31:29 -0800402
Tim Murray7a629fa2013-11-19 12:45:54 -0800403 native void rsnAllocationData1D(long con, long id, int off, int mip, int count, Object d, int sizeBytes, int dt);
404 synchronized void nAllocationData1D(long id, int off, int mip, int count, Object d, int sizeBytes, Element.DataType dt) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800405 validate();
Jason Sams6fcf2e12013-11-06 11:22:02 -0800406 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes, dt.mID);
Jason Sams2e1872f2010-08-17 16:25:41 -0700407 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700408
Tim Murray7a629fa2013-11-19 12:45:54 -0800409 native void rsnAllocationElementData1D(long con,long id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes);
410 synchronized void nAllocationElementData1D(long id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800411 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800412 rsnAllocationElementData1D(mContext, id, xoff, mip, compIdx, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700413 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700414
Tim Murraya78e9ad2013-11-15 13:08:30 -0800415 native void rsnAllocationData2D(long con,
Tim Murray7a629fa2013-11-19 12:45:54 -0800416 long dstAlloc, int dstXoff, int dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700417 int dstMip, int dstFace,
418 int width, int height,
Tim Murray7a629fa2013-11-19 12:45:54 -0800419 long srcAlloc, int srcXoff, int srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700420 int srcMip, int srcFace);
Tim Murray7a629fa2013-11-19 12:45:54 -0800421 synchronized void nAllocationData2D(long dstAlloc, int dstXoff, int dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700422 int dstMip, int dstFace,
423 int width, int height,
Tim Murray7a629fa2013-11-19 12:45:54 -0800424 long srcAlloc, int srcXoff, int srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700425 int srcMip, int srcFace) {
426 validate();
427 rsnAllocationData2D(mContext,
428 dstAlloc, dstXoff, dstYoff,
429 dstMip, dstFace,
430 width, height,
431 srcAlloc, srcXoff, srcYoff,
432 srcMip, srcFace);
433 }
434
Tim Murray7a629fa2013-11-19 12:45:54 -0800435 native void rsnAllocationData2D(long con, long id, int xoff, int yoff, int mip, int face,
Jason Sams6fcf2e12013-11-06 11:22:02 -0800436 int w, int h, Object d, int sizeBytes, int dt);
Tim Murray7a629fa2013-11-19 12:45:54 -0800437 synchronized void nAllocationData2D(long id, int xoff, int yoff, int mip, int face,
Jason Sams6fcf2e12013-11-06 11:22:02 -0800438 int w, int h, Object d, int sizeBytes, Element.DataType dt) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800439 validate();
Jason Sams6fcf2e12013-11-06 11:22:02 -0800440 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes, dt.mID);
Jason Sams2e1872f2010-08-17 16:25:41 -0700441 }
Jason Sams29868df2013-11-06 15:08:07 -0800442
Tim Murray7a629fa2013-11-19 12:45:54 -0800443 native void rsnAllocationData2D(long con, long id, int xoff, int yoff, int mip, int face, Bitmap b);
444 synchronized void nAllocationData2D(long id, int xoff, int yoff, int mip, int face, Bitmap b) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800445 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800446 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, b);
447 }
Jason Sams49a05d72010-12-29 14:31:29 -0800448
Tim Murraya78e9ad2013-11-15 13:08:30 -0800449 native void rsnAllocationData3D(long con,
Tim Murray7a629fa2013-11-19 12:45:54 -0800450 long dstAlloc, int dstXoff, int dstYoff, int dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -0700451 int dstMip,
452 int width, int height, int depth,
Tim Murray7a629fa2013-11-19 12:45:54 -0800453 long srcAlloc, int srcXoff, int srcYoff, int srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -0700454 int srcMip);
Tim Murray7a629fa2013-11-19 12:45:54 -0800455 synchronized void nAllocationData3D(long dstAlloc, int dstXoff, int dstYoff, int dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -0700456 int dstMip,
457 int width, int height, int depth,
Tim Murray7a629fa2013-11-19 12:45:54 -0800458 long srcAlloc, int srcXoff, int srcYoff, int srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -0700459 int srcMip) {
460 validate();
461 rsnAllocationData3D(mContext,
462 dstAlloc, dstXoff, dstYoff, dstZoff,
463 dstMip, width, height, depth,
464 srcAlloc, srcXoff, srcYoff, srcZoff, srcMip);
465 }
466
Tim Murray7a629fa2013-11-19 12:45:54 -0800467 native void rsnAllocationData3D(long con, long id, int xoff, int yoff, int zoff, int mip,
Jason Sams6fcf2e12013-11-06 11:22:02 -0800468 int w, int h, int depth, Object d, int sizeBytes, int dt);
Tim Murray7a629fa2013-11-19 12:45:54 -0800469 synchronized void nAllocationData3D(long id, int xoff, int yoff, int zoff, int mip,
Jason Sams6fcf2e12013-11-06 11:22:02 -0800470 int w, int h, int depth, Object d, int sizeBytes, Element.DataType dt) {
Jason Samsb05d6892013-04-09 15:59:24 -0700471 validate();
Jason Sams6fcf2e12013-11-06 11:22:02 -0800472 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes, dt.mID);
Jason Samsb05d6892013-04-09 15:59:24 -0700473 }
Jason Samsb05d6892013-04-09 15:59:24 -0700474
Tim Murray7a629fa2013-11-19 12:45:54 -0800475 native void rsnAllocationRead(long con, long id, Object d, int dt);
476 synchronized void nAllocationRead(long id, Object d, Element.DataType dt) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800477 validate();
Jason Sams29868df2013-11-06 15:08:07 -0800478 rsnAllocationRead(mContext, id, d, dt.mID);
Jason Samsfa445b92011-01-07 17:00:07 -0800479 }
Jason Sams29868df2013-11-06 15:08:07 -0800480
Tim Murray7a629fa2013-11-19 12:45:54 -0800481 native void rsnAllocationRead1D(long con, long id, int off, int mip, int count, Object d,
Jason Sams29868df2013-11-06 15:08:07 -0800482 int sizeBytes, int dt);
Tim Murray7a629fa2013-11-19 12:45:54 -0800483 synchronized void nAllocationRead1D(long id, int off, int mip, int count, Object d,
Jason Sams29868df2013-11-06 15:08:07 -0800484 int sizeBytes, Element.DataType dt) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800485 validate();
Jason Sams29868df2013-11-06 15:08:07 -0800486 rsnAllocationRead1D(mContext, id, off, mip, count, d, sizeBytes, dt.mID);
Jason Samsfa445b92011-01-07 17:00:07 -0800487 }
Jason Sams29868df2013-11-06 15:08:07 -0800488
Tim Murray7a629fa2013-11-19 12:45:54 -0800489 native void rsnAllocationRead2D(long con, long id, int xoff, int yoff, int mip, int face,
Jason Sams29868df2013-11-06 15:08:07 -0800490 int w, int h, Object d, int sizeBytes, int dt);
Tim Murray7a629fa2013-11-19 12:45:54 -0800491 synchronized void nAllocationRead2D(long id, int xoff, int yoff, int mip, int face,
Jason Sams29868df2013-11-06 15:08:07 -0800492 int w, int h, Object d, int sizeBytes, Element.DataType dt) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800493 validate();
Jason Sams29868df2013-11-06 15:08:07 -0800494 rsnAllocationRead2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes, dt.mID);
Jason Sams2e1872f2010-08-17 16:25:41 -0700495 }
Jason Sams29868df2013-11-06 15:08:07 -0800496
Tim Murray7a629fa2013-11-19 12:45:54 -0800497 native long rsnAllocationGetType(long con, long id);
498 synchronized long nAllocationGetType(long id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800499 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700500 return rsnAllocationGetType(mContext, id);
501 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700502
Tim Murray7a629fa2013-11-19 12:45:54 -0800503 native void rsnAllocationResize1D(long con, long id, int dimX);
504 synchronized void nAllocationResize1D(long id, int dimX) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800505 validate();
Jason Sams5edc6082010-10-05 13:32:49 -0700506 rsnAllocationResize1D(mContext, id, dimX);
507 }
Jason Sams5edc6082010-10-05 13:32:49 -0700508
Tim Murray7a629fa2013-11-19 12:45:54 -0800509 native long rsnFileA3DCreateFromAssetStream(long con, int assetStream);
510 synchronized long nFileA3DCreateFromAssetStream(int assetStream) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800511 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700512 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
513 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800514 native long rsnFileA3DCreateFromFile(long con, String path);
515 synchronized long nFileA3DCreateFromFile(String path) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800516 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800517 return rsnFileA3DCreateFromFile(mContext, path);
518 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800519 native long rsnFileA3DCreateFromAsset(long con, AssetManager mgr, String path);
520 synchronized long nFileA3DCreateFromAsset(AssetManager mgr, String path) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800521 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800522 return rsnFileA3DCreateFromAsset(mContext, mgr, path);
523 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800524 native int rsnFileA3DGetNumIndexEntries(long con, long fileA3D);
525 synchronized int nFileA3DGetNumIndexEntries(long fileA3D) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800526 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700527 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
528 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800529 native void rsnFileA3DGetIndexEntries(long con, long fileA3D, int numEntries, int[] IDs, String[] names);
530 synchronized void nFileA3DGetIndexEntries(long fileA3D, int numEntries, int[] IDs, String[] names) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800531 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700532 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
533 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800534 native int rsnFileA3DGetEntryByIndex(long con, long fileA3D, int index);
535 synchronized int nFileA3DGetEntryByIndex(long fileA3D, int index) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800536 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700537 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
538 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700539
Tim Murraya78e9ad2013-11-15 13:08:30 -0800540 native int rsnFontCreateFromFile(long con, String fileName, float size, int dpi);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800541 synchronized int nFontCreateFromFile(String fileName, float size, int dpi) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800542 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700543 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
544 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800545 native int rsnFontCreateFromAssetStream(long con, String name, float size, int dpi, int assetStream);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800546 synchronized int nFontCreateFromAssetStream(String name, float size, int dpi, int assetStream) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800547 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800548 return rsnFontCreateFromAssetStream(mContext, name, size, dpi, assetStream);
549 }
Tim Murraya78e9ad2013-11-15 13:08:30 -0800550 native int rsnFontCreateFromAsset(long con, AssetManager mgr, String path, float size, int dpi);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800551 synchronized int nFontCreateFromAsset(AssetManager mgr, String path, float size, int dpi) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800552 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800553 return rsnFontCreateFromAsset(mContext, mgr, path, size, dpi);
554 }
Jason Sams22534172009-08-04 16:58:20 -0700555
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700556
Tim Murray7a629fa2013-11-19 12:45:54 -0800557 native void rsnScriptBindAllocation(long con, long script, long alloc, int slot);
558 synchronized void nScriptBindAllocation(long script, long alloc, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800559 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700560 rsnScriptBindAllocation(mContext, script, alloc, slot);
561 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800562 native void rsnScriptSetTimeZone(long con, long script, byte[] timeZone);
563 synchronized void nScriptSetTimeZone(long script, byte[] timeZone) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800564 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700565 rsnScriptSetTimeZone(mContext, script, timeZone);
566 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800567 native void rsnScriptInvoke(long con, long id, int slot);
568 synchronized void nScriptInvoke(long id, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800569 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700570 rsnScriptInvoke(mContext, id, slot);
571 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800572 native void rsnScriptForEach(long con, long id, int slot, long ain, long aout, byte[] params);
573 native void rsnScriptForEach(long con, long id, int slot, long ain, long aout);
574 native void rsnScriptForEachClipped(long con, long id, int slot, long ain, long aout, byte[] params,
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800575 int xstart, int xend, int ystart, int yend, int zstart, int zend);
Tim Murray7a629fa2013-11-19 12:45:54 -0800576 native void rsnScriptForEachClipped(long con, long id, int slot, long ain, long aout,
Stephen Hinesdac6ed02013-02-13 00:09:02 -0800577 int xstart, int xend, int ystart, int yend, int zstart, int zend);
Tim Murray7a629fa2013-11-19 12:45:54 -0800578 synchronized void nScriptForEach(long id, int slot, long ain, long aout, byte[] params) {
Jason Sams6e494d32011-04-27 16:33:11 -0700579 validate();
580 if (params == null) {
581 rsnScriptForEach(mContext, id, slot, ain, aout);
582 } else {
583 rsnScriptForEach(mContext, id, slot, ain, aout, params);
584 }
585 }
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800586
Tim Murray7a629fa2013-11-19 12:45:54 -0800587 synchronized void nScriptForEachClipped(long id, int slot, long ain, long aout, byte[] params,
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800588 int xstart, int xend, int ystart, int yend, int zstart, int zend) {
589 validate();
Stephen Hinesdac6ed02013-02-13 00:09:02 -0800590 if (params == null) {
591 rsnScriptForEachClipped(mContext, id, slot, ain, aout, xstart, xend, ystart, yend, zstart, zend);
592 } else {
593 rsnScriptForEachClipped(mContext, id, slot, ain, aout, params, xstart, xend, ystart, yend, zstart, zend);
594 }
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800595 }
596
Tim Murray7a629fa2013-11-19 12:45:54 -0800597 native void rsnScriptInvokeV(long con, long id, int slot, byte[] params);
598 synchronized void nScriptInvokeV(long id, int slot, byte[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800599 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700600 rsnScriptInvokeV(mContext, id, slot, params);
601 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700602
Tim Murray7a629fa2013-11-19 12:45:54 -0800603 native void rsnScriptSetVarI(long con, long id, int slot, int val);
604 synchronized void nScriptSetVarI(long id, int slot, int val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800605 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700606 rsnScriptSetVarI(mContext, id, slot, val);
607 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800608 native int rsnScriptGetVarI(long con, long id, int slot);
609 synchronized int nScriptGetVarI(long id, int slot) {
Tim Murray7c4caad2013-04-10 16:21:40 -0700610 validate();
611 return rsnScriptGetVarI(mContext, id, slot);
612 }
613
Tim Murray7a629fa2013-11-19 12:45:54 -0800614 native void rsnScriptSetVarJ(long con, long id, int slot, long val);
615 synchronized void nScriptSetVarJ(long id, int slot, long val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800616 validate();
Stephen Hines031ec58c2010-10-11 10:54:21 -0700617 rsnScriptSetVarJ(mContext, id, slot, val);
618 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800619 native long rsnScriptGetVarJ(long con, long id, int slot);
620 synchronized long nScriptGetVarJ(long id, int slot) {
Tim Murray7c4caad2013-04-10 16:21:40 -0700621 validate();
622 return rsnScriptGetVarJ(mContext, id, slot);
623 }
624
Tim Murray7a629fa2013-11-19 12:45:54 -0800625 native void rsnScriptSetVarF(long con, long id, int slot, float val);
626 synchronized void nScriptSetVarF(long id, int slot, float val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800627 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700628 rsnScriptSetVarF(mContext, id, slot, val);
629 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800630 native float rsnScriptGetVarF(long con, long id, int slot);
631 synchronized float nScriptGetVarF(long id, int slot) {
Tim Murray7c4caad2013-04-10 16:21:40 -0700632 validate();
633 return rsnScriptGetVarF(mContext, id, slot);
634 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800635 native void rsnScriptSetVarD(long con, long id, int slot, double val);
636 synchronized void nScriptSetVarD(long id, int slot, double val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800637 validate();
Stephen Hinesca54ec32010-09-20 17:20:30 -0700638 rsnScriptSetVarD(mContext, id, slot, val);
639 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800640 native double rsnScriptGetVarD(long con, long id, int slot);
641 synchronized double nScriptGetVarD(long id, int slot) {
Tim Murray7c4caad2013-04-10 16:21:40 -0700642 validate();
643 return rsnScriptGetVarD(mContext, id, slot);
644 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800645 native void rsnScriptSetVarV(long con, long id, int slot, byte[] val);
646 synchronized void nScriptSetVarV(long id, int slot, byte[] val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800647 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700648 rsnScriptSetVarV(mContext, id, slot, val);
649 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800650 native void rsnScriptGetVarV(long con, long id, int slot, byte[] val);
651 synchronized void nScriptGetVarV(long id, int slot, byte[] val) {
Tim Murray7c4caad2013-04-10 16:21:40 -0700652 validate();
653 rsnScriptGetVarV(mContext, id, slot, val);
654 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800655 native void rsnScriptSetVarVE(long con, long id, int slot, byte[] val,
656 long e, int[] dims);
657 synchronized void nScriptSetVarVE(long id, int slot, byte[] val,
658 long e, int[] dims) {
Stephen Hinesadeb8092012-04-20 14:26:06 -0700659 validate();
660 rsnScriptSetVarVE(mContext, id, slot, val, e, dims);
661 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800662 native void rsnScriptSetVarObj(long con, long id, int slot, long val);
663 synchronized void nScriptSetVarObj(long id, int slot, long val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800664 validate();
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800665 rsnScriptSetVarObj(mContext, id, slot, val);
666 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700667
Tim Murraya78e9ad2013-11-15 13:08:30 -0800668 native int rsnScriptCCreate(long con, String resName, String cacheDir,
Jason Samse4a06c52011-03-16 16:29:28 -0700669 byte[] script, int length);
670 synchronized int nScriptCCreate(String resName, String cacheDir, byte[] script, int length) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800671 validate();
Jason Samse4a06c52011-03-16 16:29:28 -0700672 return rsnScriptCCreate(mContext, resName, cacheDir, script, length);
Jason Sams2e1872f2010-08-17 16:25:41 -0700673 }
Jason Samsebfb4362009-09-23 13:57:02 -0700674
Tim Murray7a629fa2013-11-19 12:45:54 -0800675 native long rsnScriptIntrinsicCreate(long con, int id, long eid);
676 synchronized long nScriptIntrinsicCreate(int id, long eid) {
Jason Sams6ab97682012-08-10 12:09:43 -0700677 validate();
678 return rsnScriptIntrinsicCreate(mContext, id, eid);
679 }
680
Tim Murray7a629fa2013-11-19 12:45:54 -0800681 native long rsnScriptKernelIDCreate(long con, long sid, int slot, int sig);
682 synchronized long nScriptKernelIDCreate(long sid, int slot, int sig) {
Jason Sams08a81582012-09-18 12:32:10 -0700683 validate();
684 return rsnScriptKernelIDCreate(mContext, sid, slot, sig);
685 }
686
Tim Murray7a629fa2013-11-19 12:45:54 -0800687 native long rsnScriptFieldIDCreate(long con, long sid, int slot);
688 synchronized long nScriptFieldIDCreate(long sid, int slot) {
Jason Sams08a81582012-09-18 12:32:10 -0700689 validate();
690 return rsnScriptFieldIDCreate(mContext, sid, slot);
691 }
692
Tim Murray7a629fa2013-11-19 12:45:54 -0800693 native long rsnScriptGroupCreate(long con, int[] kernels, int[] src, int[] dstk, int[] dstf, int[] types);
694 synchronized long nScriptGroupCreate(int[] kernels, int[] src, int[] dstk, int[] dstf, int[] types) {
Jason Sams08a81582012-09-18 12:32:10 -0700695 validate();
696 return rsnScriptGroupCreate(mContext, kernels, src, dstk, dstf, types);
697 }
698
Tim Murray7a629fa2013-11-19 12:45:54 -0800699 native void rsnScriptGroupSetInput(long con, long group, long kernel, long alloc);
700 synchronized void nScriptGroupSetInput(long group, long kernel, long alloc) {
Jason Sams08a81582012-09-18 12:32:10 -0700701 validate();
702 rsnScriptGroupSetInput(mContext, group, kernel, alloc);
703 }
704
Tim Murray7a629fa2013-11-19 12:45:54 -0800705 native void rsnScriptGroupSetOutput(long con, long group, long kernel, long alloc);
706 synchronized void nScriptGroupSetOutput(long group, long kernel, long alloc) {
Jason Sams08a81582012-09-18 12:32:10 -0700707 validate();
708 rsnScriptGroupSetOutput(mContext, group, kernel, alloc);
709 }
710
Tim Murray7a629fa2013-11-19 12:45:54 -0800711 native void rsnScriptGroupExecute(long con, long group);
712 synchronized void nScriptGroupExecute(long group) {
Jason Sams08a81582012-09-18 12:32:10 -0700713 validate();
714 rsnScriptGroupExecute(mContext, group);
715 }
716
Tim Murraya78e9ad2013-11-15 13:08:30 -0800717 native int rsnSamplerCreate(long con, int magFilter, int minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700718 int wrapS, int wrapT, int wrapR, float aniso);
719 synchronized int nSamplerCreate(int magFilter, int minFilter,
720 int wrapS, int wrapT, int wrapR, float aniso) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800721 validate();
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700722 return rsnSamplerCreate(mContext, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
Jason Sams2e1872f2010-08-17 16:25:41 -0700723 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800724
Tim Murraya78e9ad2013-11-15 13:08:30 -0800725 native int rsnProgramStoreCreate(long con, boolean r, boolean g, boolean b, boolean a,
Jason Sams331bf9b2011-04-06 11:23:54 -0700726 boolean depthMask, boolean dither,
727 int srcMode, int dstMode, int depthFunc);
728 synchronized int nProgramStoreCreate(boolean r, boolean g, boolean b, boolean a,
729 boolean depthMask, boolean dither,
730 int srcMode, int dstMode, int depthFunc) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800731 validate();
Jason Samsbd184c52011-04-06 11:44:47 -0700732 return rsnProgramStoreCreate(mContext, r, g, b, a, depthMask, dither, srcMode,
733 dstMode, depthFunc);
Jason Sams2e1872f2010-08-17 16:25:41 -0700734 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700735
Tim Murray7a629fa2013-11-19 12:45:54 -0800736 native long rsnProgramRasterCreate(long con, boolean pointSprite, int cullMode);
737 synchronized long nProgramRasterCreate(boolean pointSprite, int cullMode) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800738 validate();
Jason Sams94aaed32011-09-23 14:18:53 -0700739 return rsnProgramRasterCreate(mContext, pointSprite, cullMode);
Jason Sams2e1872f2010-08-17 16:25:41 -0700740 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700741
Tim Murray7a629fa2013-11-19 12:45:54 -0800742 native void rsnProgramBindConstants(long con, long pv, int slot, long mID);
743 synchronized void nProgramBindConstants(long pv, int slot, long mID) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800744 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700745 rsnProgramBindConstants(mContext, pv, slot, mID);
746 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800747 native void rsnProgramBindTexture(long con, long vpf, int slot, long a);
748 synchronized void nProgramBindTexture(long vpf, int slot, long a) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800749 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700750 rsnProgramBindTexture(mContext, vpf, slot, a);
751 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800752 native void rsnProgramBindSampler(long con, long vpf, int slot, long s);
753 synchronized void nProgramBindSampler(long vpf, int slot, long s) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800754 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700755 rsnProgramBindSampler(mContext, vpf, slot, s);
756 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800757 native long rsnProgramFragmentCreate(long con, String shader, String[] texNames, int[] params);
758 synchronized long nProgramFragmentCreate(String shader, String[] texNames, int[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800759 validate();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800760 return rsnProgramFragmentCreate(mContext, shader, texNames, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700761 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800762 native long rsnProgramVertexCreate(long con, String shader, String[] texNames, int[] params);
763 synchronized long nProgramVertexCreate(String shader, String[] texNames, int[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800764 validate();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800765 return rsnProgramVertexCreate(mContext, shader, texNames, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700766 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700767
Tim Murray7a629fa2013-11-19 12:45:54 -0800768 native long rsnMeshCreate(long con, int[] vtx, int[] idx, int[] prim);
769 synchronized long nMeshCreate(int[] vtx, int[] idx, int[] prim) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800770 validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700771 return rsnMeshCreate(mContext, vtx, idx, prim);
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800772 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800773 native int rsnMeshGetVertexBufferCount(long con, long id);
774 synchronized int nMeshGetVertexBufferCount(long id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800775 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700776 return rsnMeshGetVertexBufferCount(mContext, id);
777 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800778 native int rsnMeshGetIndexCount(long con, long id);
779 synchronized int nMeshGetIndexCount(long id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800780 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700781 return rsnMeshGetIndexCount(mContext, id);
782 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800783 native void rsnMeshGetVertices(long con, long id, int[] vtxIds, int vtxIdCount);
784 synchronized void nMeshGetVertices(long id, int[] vtxIds, int vtxIdCount) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800785 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700786 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
787 }
Tim Murray7a629fa2013-11-19 12:45:54 -0800788 native void rsnMeshGetIndices(long con, long id, int[] idxIds, int[] primitives, int vtxIdCount);
789 synchronized void nMeshGetIndices(long id, int[] idxIds, int[] primitives, int vtxIdCount) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800790 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700791 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
792 }
793
Tim Murray7a629fa2013-11-19 12:45:54 -0800794 native long rsnPathCreate(long con, int prim, boolean isStatic, long vtx, int loop, float q);
795 synchronized long nPathCreate(int prim, boolean isStatic, long vtx, int loop, float q) {
Jason Samsf15ed012011-10-31 13:23:43 -0700796 validate();
797 return rsnPathCreate(mContext, prim, isStatic, vtx, loop, q);
798 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700799
Tim Murraya78e9ad2013-11-15 13:08:30 -0800800 long mDev;
801 long mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700802 @SuppressWarnings({"FieldCanBeLocal"})
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800803 MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700804
Jason Sams8cb39de2010-06-01 15:47:01 -0700805 Element mElement_U8;
806 Element mElement_I8;
807 Element mElement_U16;
808 Element mElement_I16;
809 Element mElement_U32;
810 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -0700811 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -0700812 Element mElement_I64;
Jason Sams8cb39de2010-06-01 15:47:01 -0700813 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700814 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700815 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700816
Jason Sams8cb39de2010-06-01 15:47:01 -0700817 Element mElement_ELEMENT;
818 Element mElement_TYPE;
819 Element mElement_ALLOCATION;
820 Element mElement_SAMPLER;
821 Element mElement_SCRIPT;
822 Element mElement_MESH;
823 Element mElement_PROGRAM_FRAGMENT;
824 Element mElement_PROGRAM_VERTEX;
825 Element mElement_PROGRAM_RASTER;
826 Element mElement_PROGRAM_STORE;
Stephen Hines3a291412012-04-11 17:27:29 -0700827 Element mElement_FONT;
Jason Samsa70f4162010-03-26 15:33:42 -0700828
Jason Sams3c0dfba2009-09-27 17:50:38 -0700829 Element mElement_A_8;
830 Element mElement_RGB_565;
831 Element mElement_RGB_888;
832 Element mElement_RGBA_5551;
833 Element mElement_RGBA_4444;
834 Element mElement_RGBA_8888;
835
Jason Sams8cb39de2010-06-01 15:47:01 -0700836 Element mElement_FLOAT_2;
837 Element mElement_FLOAT_3;
838 Element mElement_FLOAT_4;
Stephen Hines836c4a52011-06-01 14:38:10 -0700839
840 Element mElement_DOUBLE_2;
841 Element mElement_DOUBLE_3;
842 Element mElement_DOUBLE_4;
843
844 Element mElement_UCHAR_2;
845 Element mElement_UCHAR_3;
Jason Sams8cb39de2010-06-01 15:47:01 -0700846 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800847
Stephen Hines836c4a52011-06-01 14:38:10 -0700848 Element mElement_CHAR_2;
849 Element mElement_CHAR_3;
850 Element mElement_CHAR_4;
851
852 Element mElement_USHORT_2;
853 Element mElement_USHORT_3;
854 Element mElement_USHORT_4;
855
856 Element mElement_SHORT_2;
857 Element mElement_SHORT_3;
858 Element mElement_SHORT_4;
859
860 Element mElement_UINT_2;
861 Element mElement_UINT_3;
862 Element mElement_UINT_4;
863
864 Element mElement_INT_2;
865 Element mElement_INT_3;
866 Element mElement_INT_4;
867
868 Element mElement_ULONG_2;
869 Element mElement_ULONG_3;
870 Element mElement_ULONG_4;
871
872 Element mElement_LONG_2;
873 Element mElement_LONG_3;
874 Element mElement_LONG_4;
875
Tim Murray932e78e2013-09-03 11:42:26 -0700876 Element mElement_YUV;
877
Jason Sams1d45c472010-08-25 14:31:48 -0700878 Element mElement_MATRIX_4X4;
879 Element mElement_MATRIX_3X3;
880 Element mElement_MATRIX_2X2;
881
Jason Sams4d339932010-05-11 14:03:58 -0700882 Sampler mSampler_CLAMP_NEAREST;
883 Sampler mSampler_CLAMP_LINEAR;
884 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
885 Sampler mSampler_WRAP_NEAREST;
886 Sampler mSampler_WRAP_LINEAR;
887 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800888 Sampler mSampler_MIRRORED_REPEAT_NEAREST;
889 Sampler mSampler_MIRRORED_REPEAT_LINEAR;
890 Sampler mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
Jason Sams4d339932010-05-11 14:03:58 -0700891
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700892 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
893 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700894 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
895 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700896
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700897 ProgramRaster mProgramRaster_CULL_BACK;
898 ProgramRaster mProgramRaster_CULL_FRONT;
899 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700900
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700901 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700902 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700903
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700904 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700905 * The base class from which an application should derive in order
906 * to receive RS messages from scripts. When a script calls {@code
907 * rsSendToClient}, the data fields will be filled, and the run
908 * method will be called on a separate thread. This will occur
909 * some time after {@code rsSendToClient} completes in the script,
910 * as {@code rsSendToClient} is asynchronous. Message handlers are
911 * not guaranteed to have completed when {@link
912 * android.renderscript.RenderScript#finish} returns.
Jason Sams27676fe2010-11-10 17:00:59 -0800913 *
914 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800915 public static class RSMessageHandler implements Runnable {
Jason Sams516c3192009-10-06 13:58:47 -0700916 protected int[] mData;
917 protected int mID;
Jason Sams1c415172010-11-08 17:06:46 -0800918 protected int mLength;
Jason Sams516c3192009-10-06 13:58:47 -0700919 public void run() {
920 }
921 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700922 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700923 * If an application is expecting messages, it should set this
924 * field to an instance of {@link RSMessageHandler}. This
925 * instance will receive all the user messages sent from {@code
926 * sendToClient} by scripts from this context.
Jason Sams27676fe2010-11-10 17:00:59 -0800927 *
928 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800929 RSMessageHandler mMessageCallback = null;
930
931 public void setMessageHandler(RSMessageHandler msg) {
932 mMessageCallback = msg;
933 }
934 public RSMessageHandler getMessageHandler() {
935 return mMessageCallback;
936 }
Jason Sams516c3192009-10-06 13:58:47 -0700937
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700938 /**
Jason Sams02d56d92013-04-12 16:40:50 -0700939 * Place a message into the message queue to be sent back to the message
940 * handler once all previous commands have been executed.
Jason Sams455d6442013-02-05 19:20:18 -0800941 *
942 * @param id
943 * @param data
944 */
945 public void sendMessage(int id, int[] data) {
946 nContextSendMessage(id, data);
947 }
948
949 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700950 * The runtime error handler base class. An application should derive from this class
951 * if it wishes to install an error handler. When errors occur at runtime,
952 * the fields in this class will be filled, and the run method will be called.
Jason Sams27676fe2010-11-10 17:00:59 -0800953 *
954 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800955 public static class RSErrorHandler implements Runnable {
Jason Sams1c415172010-11-08 17:06:46 -0800956 protected String mErrorMessage;
957 protected int mErrorNum;
958 public void run() {
959 }
960 }
Jason Sams27676fe2010-11-10 17:00:59 -0800961
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700962 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800963 * Application Error handler. All runtime errors will be dispatched to the
964 * instance of RSAsyncError set here. If this field is null a
Tim Murrayc11e25c2013-04-09 11:01:01 -0700965 * {@link RSRuntimeException} will instead be thrown with details about the error.
Jason Sams27676fe2010-11-10 17:00:59 -0800966 * This will cause program termaination.
967 *
968 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800969 RSErrorHandler mErrorCallback = null;
970
971 public void setErrorHandler(RSErrorHandler msg) {
972 mErrorCallback = msg;
973 }
974 public RSErrorHandler getErrorHandler() {
975 return mErrorCallback;
976 }
Jason Sams1c415172010-11-08 17:06:46 -0800977
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700978 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700979 * RenderScript worker thread priority enumeration. The default value is
980 * NORMAL. Applications wishing to do background processing should set
981 * their priority to LOW to avoid starving forground processes.
Jason Sams27676fe2010-11-10 17:00:59 -0800982 */
Jason Sams7d787b42009-11-15 12:14:26 -0800983 public enum Priority {
Glenn Kasten260c77a2011-06-01 17:25:54 -0700984 LOW (Process.THREAD_PRIORITY_BACKGROUND + (5 * Process.THREAD_PRIORITY_LESS_FAVORABLE)),
985 NORMAL (Process.THREAD_PRIORITY_DISPLAY);
Jason Sams7d787b42009-11-15 12:14:26 -0800986
987 int mID;
988 Priority(int id) {
989 mID = id;
990 }
991 }
992
Jason Sams771bebb2009-12-07 12:40:12 -0800993 void validate() {
994 if (mContext == 0) {
Jason Samsc1d62102010-11-04 14:32:19 -0700995 throw new RSInvalidStateException("Calling RS with no Context active.");
Jason Sams771bebb2009-12-07 12:40:12 -0800996 }
997 }
998
Jason Sams27676fe2010-11-10 17:00:59 -0800999
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001000 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001001 * Change the priority of the worker threads for this context.
1002 *
1003 * @param p New priority to be set.
1004 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001005 public void setPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -08001006 validate();
Jason Sams7d787b42009-11-15 12:14:26 -08001007 nContextSetPriority(p.mID);
1008 }
1009
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001010 static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -07001011 RenderScript mRS;
1012 boolean mRun = true;
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001013 int[] mAuxData = new int[2];
Jason Sams1c415172010-11-08 17:06:46 -08001014
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001015 static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
1016 static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
1017 static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
1018 static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
1019 static final int RS_MESSAGE_TO_CLIENT_USER = 4;
Jason Sams739c8262013-04-11 18:07:52 -07001020 static final int RS_MESSAGE_TO_CLIENT_NEW_BUFFER = 5;
Jason Sams516c3192009-10-06 13:58:47 -07001021
Stephen Hines42028a82013-04-17 19:22:01 -07001022 static final int RS_ERROR_FATAL_DEBUG = 0x0800;
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001023 static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
Jason Samsadd9d962010-11-22 16:20:16 -08001024
Jason Sams516c3192009-10-06 13:58:47 -07001025 MessageThread(RenderScript rs) {
1026 super("RSMessageThread");
1027 mRS = rs;
1028
1029 }
1030
1031 public void run() {
1032 // This function is a temporary solution. The final solution will
1033 // used typed allocations where the message id is the type indicator.
1034 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -07001035 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -07001036 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -07001037 rbuf[0] = 0;
Jason Samsedbfabd2011-05-17 15:01:29 -07001038 int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001039 int size = mAuxData[1];
1040 int subID = mAuxData[0];
Jason Sams1c415172010-11-08 17:06:46 -08001041
1042 if (msg == RS_MESSAGE_TO_CLIENT_USER) {
1043 if ((size>>2) >= rbuf.length) {
1044 rbuf = new int[(size + 3) >> 2];
1045 }
Jason Samsedbfabd2011-05-17 15:01:29 -07001046 if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) !=
1047 RS_MESSAGE_TO_CLIENT_USER) {
Tim Murrayc11e25c2013-04-09 11:01:01 -07001048 throw new RSDriverException("Error processing message from RenderScript.");
Jason Samsedbfabd2011-05-17 15:01:29 -07001049 }
Jason Sams1c415172010-11-08 17:06:46 -08001050
1051 if(mRS.mMessageCallback != null) {
1052 mRS.mMessageCallback.mData = rbuf;
1053 mRS.mMessageCallback.mID = subID;
1054 mRS.mMessageCallback.mLength = size;
1055 mRS.mMessageCallback.run();
Jason Sams1d45c472010-08-25 14:31:48 -07001056 } else {
Jason Sams1c415172010-11-08 17:06:46 -08001057 throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
Jason Sams516c3192009-10-06 13:58:47 -07001058 }
Stephen Hinesab98bb62010-09-24 14:38:30 -07001059 continue;
Jason Sams516c3192009-10-06 13:58:47 -07001060 }
Jason Sams1c415172010-11-08 17:06:46 -08001061
1062 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
1063 String e = mRS.nContextGetErrorMessage(mRS.mContext);
1064
Stephen Hines42028a82013-04-17 19:22:01 -07001065 // Throw RSRuntimeException under the following conditions:
1066 //
1067 // 1) It is an unknown fatal error.
1068 // 2) It is a debug fatal error, and we are not in a
1069 // debug context.
1070 // 3) It is a debug fatal error, and we do not have an
1071 // error callback.
1072 if (subID >= RS_ERROR_FATAL_UNKNOWN ||
1073 (subID >= RS_ERROR_FATAL_DEBUG &&
1074 (mRS.mContextType != ContextType.DEBUG ||
1075 mRS.mErrorCallback == null))) {
Jason Samsadd9d962010-11-22 16:20:16 -08001076 throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
1077 }
1078
Jason Sams1c415172010-11-08 17:06:46 -08001079 if(mRS.mErrorCallback != null) {
1080 mRS.mErrorCallback.mErrorMessage = e;
1081 mRS.mErrorCallback.mErrorNum = subID;
1082 mRS.mErrorCallback.run();
1083 } else {
Jason Samsa4b7bc92013-02-05 15:05:39 -08001084 android.util.Log.e(LOG_TAG, "non fatal RS error, " + e);
Stephen Hinesbe74bdd2012-02-03 15:29:36 -08001085 // Do not throw here. In these cases, we do not have
1086 // a fatal error.
Jason Sams1c415172010-11-08 17:06:46 -08001087 }
1088 continue;
1089 }
1090
Jason Sams739c8262013-04-11 18:07:52 -07001091 if (msg == RS_MESSAGE_TO_CLIENT_NEW_BUFFER) {
1092 Allocation.sendBufferNotification(subID);
1093 continue;
1094 }
1095
Jason Sams1c415172010-11-08 17:06:46 -08001096 // 2: teardown.
1097 // But we want to avoid starving other threads during
1098 // teardown by yielding until the next line in the destructor
1099 // can execute to set mRun = false
1100 try {
1101 sleep(1, 0);
1102 } catch(InterruptedException e) {
Jason Sams516c3192009-10-06 13:58:47 -07001103 }
Jason Sams516c3192009-10-06 13:58:47 -07001104 }
Tim Murrayda67deb2013-05-09 12:02:50 -07001105 //Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -07001106 }
1107 }
1108
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001109 RenderScript(Context ctx) {
Stephen Hines42028a82013-04-17 19:22:01 -07001110 mContextType = ContextType.NORMAL;
Jason Sams1a4e1f32012-02-24 17:51:24 -08001111 if (ctx != null) {
1112 mApplicationContext = ctx.getApplicationContext();
1113 }
Tim Murray504abb32014-01-07 11:13:56 -08001114 mRWLock = new ReentrantReadWriteLock();
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001115 }
1116
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001117 /**
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001118 * Gets the application context associated with the RenderScript context.
1119 *
1120 * @return The application context.
1121 */
1122 public final Context getApplicationContext() {
1123 return mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001124 }
1125
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001126 /**
Jason Samsadd26dc2013-02-22 18:43:45 -08001127 * @hide
1128 */
1129 public static RenderScript create(Context ctx, int sdkVersion) {
1130 return create(ctx, sdkVersion, ContextType.NORMAL);
1131 }
1132
1133 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001134 * Create a RenderScript context.
Jason Sams27676fe2010-11-10 17:00:59 -08001135 *
Jason Sams1a4e1f32012-02-24 17:51:24 -08001136 * @hide
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001137 * @param ctx The context.
Jason Sams27676fe2010-11-10 17:00:59 -08001138 * @return RenderScript
1139 */
Jason Samsadd26dc2013-02-22 18:43:45 -08001140 public static RenderScript create(Context ctx, int sdkVersion, ContextType ct) {
Dan Morrille4d9a012013-03-28 18:10:43 -07001141 if (!sInitialized) {
1142 Log.e(LOG_TAG, "RenderScript.create() called when disabled; someone is likely to crash");
1143 return null;
1144 }
1145
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001146 RenderScript rs = new RenderScript(ctx);
Jason Sams704ff642010-02-09 16:05:07 -08001147
1148 rs.mDev = rs.nDeviceCreate();
Jason Samsadd26dc2013-02-22 18:43:45 -08001149 rs.mContext = rs.nContextCreate(rs.mDev, 0, sdkVersion, ct.mID);
Stephen Hines42028a82013-04-17 19:22:01 -07001150 rs.mContextType = ct;
Jason Sams26985362011-05-03 15:01:58 -07001151 if (rs.mContext == 0) {
1152 throw new RSDriverException("Failed to create RS context.");
1153 }
Jason Sams704ff642010-02-09 16:05:07 -08001154 rs.mMessageThread = new MessageThread(rs);
1155 rs.mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -08001156 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -08001157 }
1158
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001159 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001160 * Create a RenderScript context.
Jason Sams1a4e1f32012-02-24 17:51:24 -08001161 *
1162 * @param ctx The context.
1163 * @return RenderScript
1164 */
1165 public static RenderScript create(Context ctx) {
Jason Samsadd26dc2013-02-22 18:43:45 -08001166 return create(ctx, ContextType.NORMAL);
1167 }
1168
1169 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001170 * Create a RenderScript context.
Jason Samsadd26dc2013-02-22 18:43:45 -08001171 *
Jason Samsadd26dc2013-02-22 18:43:45 -08001172 *
1173 * @param ctx The context.
Jason Sams02d56d92013-04-12 16:40:50 -07001174 * @param ct The type of context to be created.
Jason Samsadd26dc2013-02-22 18:43:45 -08001175 * @return RenderScript
1176 */
1177 public static RenderScript create(Context ctx, ContextType ct) {
Jason Sams1a4e1f32012-02-24 17:51:24 -08001178 int v = ctx.getApplicationInfo().targetSdkVersion;
Jason Samsadd26dc2013-02-22 18:43:45 -08001179 return create(ctx, v, ct);
Jason Sams1a4e1f32012-02-24 17:51:24 -08001180 }
1181
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001182 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001183 * Print the currently available debugging information about the state of
1184 * the RS context to the log.
1185 *
Jason Sams27676fe2010-11-10 17:00:59 -08001186 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001187 public void contextDump() {
Jason Sams5dbfe932010-01-27 14:41:43 -08001188 validate();
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001189 nContextDump(0);
Jason Sams715333b2009-11-17 17:26:46 -08001190 }
1191
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001192 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001193 * Wait for any pending asynchronous opeations (such as copies to a RS
1194 * allocation or RS script executions) to complete.
Jason Sams27676fe2010-11-10 17:00:59 -08001195 *
1196 */
Jason Sams96ed4cf2010-06-15 12:15:57 -07001197 public void finish() {
1198 nContextFinish();
1199 }
1200
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001201 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001202 * Destroys this RenderScript context. Once this function is called,
1203 * using this context or any objects belonging to this context is
1204 * illegal.
Jason Sams27676fe2010-11-10 17:00:59 -08001205 *
1206 */
Jason Samsf5b45962009-08-25 14:49:07 -07001207 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -08001208 validate();
Tim Murray504abb32014-01-07 11:13:56 -08001209 nContextFinish();
1210
Jason Sams2e1872f2010-08-17 16:25:41 -07001211 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -07001212 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -07001213 try {
1214 mMessageThread.join();
1215 } catch(InterruptedException e) {
1216 }
Jason Sams516c3192009-10-06 13:58:47 -07001217
Jason Sams2e1872f2010-08-17 16:25:41 -07001218 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -07001219
1220 nDeviceDestroy(mDev);
1221 mDev = 0;
1222 }
Jason Sams02fb2cb2009-05-28 15:37:57 -07001223
Jason Samsa9e7a052009-09-25 14:51:22 -07001224 boolean isAlive() {
1225 return mContext != 0;
1226 }
1227
Tim Murray7a629fa2013-11-19 12:45:54 -08001228 long safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -07001229 if(o != null) {
Jason Samse07694b2012-04-03 15:36:36 -07001230 return o.getID(this);
Jason Samsd8e41612009-08-20 17:22:40 -07001231 }
Jason Sams6b9dec02009-09-23 16:38:37 -07001232 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001233 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001234}