blob: 8071073a1ac7813f20c8c468c860f357939c561c [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;
Jason Sams36e612a2009-07-31 16:26:13 -070021
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080022import android.content.Context;
Stephen Hines4382467a2011-08-01 15:02:34 -070023import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080025import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070026import android.graphics.Bitmap;
Romain Guy650a3eb2009-08-31 14:06:43 -070027import android.graphics.BitmapFactory;
Jason Samsfaa32b32011-06-20 16:58:04 -070028import android.graphics.SurfaceTexture;
Glenn Kasten260c77a2011-06-01 17:25:54 -070029import android.os.Process;
Jason Sams36e612a2009-07-31 16:26:13 -070030import android.util.Log;
31import android.view.Surface;
Dan Morrille4d9a012013-03-28 18:10:43 -070032import android.os.SystemProperties;
Tim Murray6d7a53c2013-05-23 16:59:23 -070033import android.os.Trace;
Stephen Hines4382467a2011-08-01 15:02:34 -070034
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070035/**
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080036 * Renderscript base master class. An instance of this class creates native
Jason Sams27676fe2010-11-10 17:00:59 -080037 * worker threads for processing commands from this object. This base class
38 * does not provide any extended capabilities beyond simple data processing.
39 * For extended capabilities use derived classes such as RenderScriptGL.
40 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080041 * <div class="special reference">
42 * <h3>Developer Guides</h3>
43 * <p>For more information about creating an application that uses Renderscript, read the
Scott Mainb47fa162013-02-05 14:23:13 -080044 * <a href="{@docRoot}guide/topics/renderscript/index.html">Renderscript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080045 * </div>
Jason Samse29d4712009-07-23 15:19:03 -070046 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070047public class RenderScript {
Tim Murray6d7a53c2013-05-23 16:59:23 -070048 static final long TRACE_TAG = Trace.TRACE_TAG_RS;
49
Jason Sams3bc47d42009-11-12 15:10:25 -080050 static final String LOG_TAG = "RenderScript_jni";
Jason Samsbf6ef8d2010-12-06 15:59:59 -080051 static final boolean DEBUG = false;
Romain Guy650a3eb2009-08-31 14:06:43 -070052 @SuppressWarnings({"UnusedDeclaration", "deprecation"})
Joe Onorato43a17652011-04-06 19:22:23 -070053 static final boolean LOG_ENABLED = false;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070054
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080055 private Context mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070056
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080057 /*
Jack Palevich60aa3ea2009-05-26 13:45:08 -070058 * We use a class initializer to allow the native code to cache some
59 * field offsets.
60 */
Dan Morrille4d9a012013-03-28 18:10:43 -070061 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) // TODO: now used locally; remove?
Jason Samsbf6ef8d2010-12-06 15:59:59 -080062 static boolean sInitialized;
63 native static void _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070064
Jason Samsdba3ba52009-07-30 14:56:12 -070065
Jack Palevich60aa3ea2009-05-26 13:45:08 -070066 static {
67 sInitialized = false;
Dan Morrille4d9a012013-03-28 18:10:43 -070068 if (!SystemProperties.getBoolean("config.disable_renderscript", false)) {
69 try {
70 System.loadLibrary("rs_jni");
71 _nInit();
72 sInitialized = true;
73 } catch (UnsatisfiedLinkError e) {
74 Log.e(LOG_TAG, "Error loading RS jni library: " + e);
75 throw new RSRuntimeException("Error loading RS jni library: " + e);
76 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070077 }
78 }
79
Jason Sams2e1872f2010-08-17 16:25:41 -070080 // Non-threadsafe functions.
Jason Sams36e612a2009-07-31 16:26:13 -070081 native int nDeviceCreate();
82 native void nDeviceDestroy(int dev);
Jason Samsebfb4362009-09-23 13:57:02 -070083 native void nDeviceSetConfig(int dev, int param, int value);
Jason Samsedbfabd2011-05-17 15:01:29 -070084 native int nContextGetUserMessage(int con, int[] data);
Jason Sams1c415172010-11-08 17:06:46 -080085 native String nContextGetErrorMessage(int con);
Jason Samsedbfabd2011-05-17 15:01:29 -070086 native int nContextPeekMessage(int con, int[] subID);
Jason Sams2e1872f2010-08-17 16:25:41 -070087 native void nContextInitToClient(int con);
88 native void nContextDeinitToClient(int con);
Jason Sams3eaa3382009-06-10 15:04:38 -070089
Stephen Hines7d25a822013-04-09 23:51:56 -070090 static File mCacheDir;
Jason Samsa6f338c2012-02-24 16:22:16 -080091
92 /**
93 * Sets the directory to use as a persistent storage for the
94 * renderscript object file cache.
95 *
96 * @hide
97 * @param cacheDir A directory the current process can write to
98 */
Jason Samsa6f338c2012-02-24 16:22:16 -080099 public static void setupDiskCache(File cacheDir) {
Dan Morrille4d9a012013-03-28 18:10:43 -0700100 if (!sInitialized) {
101 Log.e(LOG_TAG, "RenderScript.setupDiskCache() called when disabled");
102 return;
103 }
104
Stephen Hines7d25a822013-04-09 23:51:56 -0700105 // Defer creation of cache path to nScriptCCreate().
106 mCacheDir = cacheDir;
Jason Samsa6f338c2012-02-24 16:22:16 -0800107 }
108
Jason Sams02d56d92013-04-12 16:40:50 -0700109 /**
110 * ContextType specifies the specific type of context to be created.
111 *
112 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800113 public enum ContextType {
Jason Sams02d56d92013-04-12 16:40:50 -0700114 /**
115 * NORMAL context, this is the default and what shipping apps should
116 * use.
117 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800118 NORMAL (0),
Jason Sams02d56d92013-04-12 16:40:50 -0700119
120 /**
121 * DEBUG context, perform extra runtime checks to validate the
122 * kernels and APIs are being used as intended. Get and SetElementAt
123 * will be bounds checked in this mode.
124 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800125 DEBUG (1),
Jason Sams02d56d92013-04-12 16:40:50 -0700126
127 /**
128 * PROFILE context, Intended to be used once the first time an
129 * application is run on a new device. This mode allows the runtime to
130 * do additional testing and performance tuning.
131 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800132 PROFILE (2);
133
134 int mID;
135 ContextType(int id) {
136 mID = id;
137 }
138 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800139
Stephen Hines42028a82013-04-17 19:22:01 -0700140 ContextType mContextType;
141
Jason Sams2e1872f2010-08-17 16:25:41 -0700142 // Methods below are wrapped to protect the non-threadsafe
143 // lockless fifo.
Stephen Hines4382467a2011-08-01 15:02:34 -0700144 native int rsnContextCreateGL(int dev, int ver, int sdkVer,
Jason Sams11c8af92010-10-13 15:31:10 -0700145 int colorMin, int colorPref,
146 int alphaMin, int alphaPref,
147 int depthMin, int depthPref,
148 int stencilMin, int stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700149 int samplesMin, int samplesPref, float samplesQ, int dpi);
Stephen Hines4382467a2011-08-01 15:02:34 -0700150 synchronized int nContextCreateGL(int dev, int ver, int sdkVer,
Jason Sams11c8af92010-10-13 15:31:10 -0700151 int colorMin, int colorPref,
152 int alphaMin, int alphaPref,
153 int depthMin, int depthPref,
154 int stencilMin, int stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700155 int samplesMin, int samplesPref, float samplesQ, int dpi) {
Stephen Hines4382467a2011-08-01 15:02:34 -0700156 return rsnContextCreateGL(dev, ver, sdkVer, colorMin, colorPref,
Jason Sams11c8af92010-10-13 15:31:10 -0700157 alphaMin, alphaPref, depthMin, depthPref,
158 stencilMin, stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700159 samplesMin, samplesPref, samplesQ, dpi);
Jason Sams2e1872f2010-08-17 16:25:41 -0700160 }
Jason Samsadd26dc2013-02-22 18:43:45 -0800161 native int rsnContextCreate(int dev, int ver, int sdkVer, int contextType);
162 synchronized int nContextCreate(int dev, int ver, int sdkVer, int contextType) {
163 return rsnContextCreate(dev, ver, sdkVer, contextType);
Jason Sams2e1872f2010-08-17 16:25:41 -0700164 }
165 native void rsnContextDestroy(int con);
166 synchronized void nContextDestroy() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800167 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700168 rsnContextDestroy(mContext);
169 }
170 native void rsnContextSetSurface(int con, int w, int h, Surface sur);
171 synchronized void nContextSetSurface(int w, int h, Surface sur) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800172 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700173 rsnContextSetSurface(mContext, w, h, sur);
174 }
Jason Samsfaa32b32011-06-20 16:58:04 -0700175 native void rsnContextSetSurfaceTexture(int con, int w, int h, SurfaceTexture sur);
176 synchronized void nContextSetSurfaceTexture(int w, int h, SurfaceTexture sur) {
177 validate();
178 rsnContextSetSurfaceTexture(mContext, w, h, sur);
179 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700180 native void rsnContextSetPriority(int con, int p);
181 synchronized void nContextSetPriority(int p) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800182 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700183 rsnContextSetPriority(mContext, p);
184 }
185 native void rsnContextDump(int con, int bits);
186 synchronized void nContextDump(int bits) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800187 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700188 rsnContextDump(mContext, bits);
189 }
190 native void rsnContextFinish(int con);
191 synchronized void nContextFinish() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800192 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700193 rsnContextFinish(mContext);
194 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700195
Jason Sams455d6442013-02-05 19:20:18 -0800196 native void rsnContextSendMessage(int con, int id, int[] data);
197 synchronized void nContextSendMessage(int id, int[] data) {
198 validate();
199 rsnContextSendMessage(mContext, id, data);
200 }
201
Jason Sams2e1872f2010-08-17 16:25:41 -0700202 native void rsnContextBindRootScript(int con, int script);
203 synchronized void nContextBindRootScript(int script) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800204 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700205 rsnContextBindRootScript(mContext, script);
206 }
207 native void rsnContextBindSampler(int con, int sampler, int slot);
208 synchronized void nContextBindSampler(int sampler, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800209 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700210 rsnContextBindSampler(mContext, sampler, slot);
211 }
212 native void rsnContextBindProgramStore(int con, int pfs);
213 synchronized void nContextBindProgramStore(int pfs) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800214 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700215 rsnContextBindProgramStore(mContext, pfs);
216 }
217 native void rsnContextBindProgramFragment(int con, int pf);
218 synchronized void nContextBindProgramFragment(int pf) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800219 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700220 rsnContextBindProgramFragment(mContext, pf);
221 }
222 native void rsnContextBindProgramVertex(int con, int pv);
223 synchronized void nContextBindProgramVertex(int pv) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800224 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700225 rsnContextBindProgramVertex(mContext, pv);
226 }
227 native void rsnContextBindProgramRaster(int con, int pr);
228 synchronized void nContextBindProgramRaster(int pr) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800229 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700230 rsnContextBindProgramRaster(mContext, pr);
231 }
232 native void rsnContextPause(int con);
233 synchronized void nContextPause() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800234 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700235 rsnContextPause(mContext);
236 }
237 native void rsnContextResume(int con);
238 synchronized void nContextResume() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800239 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700240 rsnContextResume(mContext);
241 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700242
Jason Sams2e1872f2010-08-17 16:25:41 -0700243 native void rsnAssignName(int con, int obj, byte[] name);
244 synchronized void nAssignName(int obj, byte[] name) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800245 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700246 rsnAssignName(mContext, obj, name);
247 }
248 native String rsnGetName(int con, int obj);
249 synchronized String nGetName(int obj) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800250 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700251 return rsnGetName(mContext, obj);
252 }
253 native void rsnObjDestroy(int con, int id);
254 synchronized void nObjDestroy(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800255 // There is a race condition here. The calling code may be run
256 // by the gc while teardown is occuring. This protects againts
257 // deleting dead objects.
258 if (mContext != 0) {
259 rsnObjDestroy(mContext, id);
260 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700261 }
Jason Samsfe08d992009-05-27 14:45:32 -0700262
Jason Sams2e1872f2010-08-17 16:25:41 -0700263 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
264 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800265 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700266 return rsnElementCreate(mContext, type, kind, norm, vecSize);
267 }
Jason Sams70d4e502010-09-02 17:35:23 -0700268 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
269 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800270 validate();
Jason Sams70d4e502010-09-02 17:35:23 -0700271 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700272 }
273 native void rsnElementGetNativeData(int con, int id, int[] elementData);
274 synchronized void nElementGetNativeData(int id, int[] elementData) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800275 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700276 rsnElementGetNativeData(mContext, id, elementData);
277 }
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700278 native void rsnElementGetSubElements(int con, int id,
279 int[] IDs, String[] names, int[] arraySizes);
280 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names, int[] arraySizes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800281 validate();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700282 rsnElementGetSubElements(mContext, id, IDs, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700283 }
Jason Sams768bc022009-09-21 19:41:04 -0700284
Jason Samsb109cc72013-01-07 18:20:12 -0800285 native int rsnTypeCreate(int con, int eid, int x, int y, int z, boolean mips, boolean faces, int yuv);
286 synchronized int nTypeCreate(int eid, int x, int y, int z, boolean mips, boolean faces, int yuv) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800287 validate();
Jason Samsb109cc72013-01-07 18:20:12 -0800288 return rsnTypeCreate(mContext, eid, x, y, z, mips, faces, yuv);
Jason Sams2e1872f2010-08-17 16:25:41 -0700289 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700290 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
291 synchronized void nTypeGetNativeData(int id, int[] typeData) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800292 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700293 rsnTypeGetNativeData(mContext, id, typeData);
294 }
Jason Sams768bc022009-09-21 19:41:04 -0700295
Jason Sams857d0c72011-11-23 15:02:15 -0800296 native int rsnAllocationCreateTyped(int con, int type, int mip, int usage, int pointer);
297 synchronized int nAllocationCreateTyped(int type, int mip, int usage, int pointer) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800298 validate();
Jason Sams857d0c72011-11-23 15:02:15 -0800299 return rsnAllocationCreateTyped(mContext, type, mip, usage, pointer);
Jason Sams2e1872f2010-08-17 16:25:41 -0700300 }
Jason Sams5476b452010-12-08 16:14:36 -0800301 native int rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
302 synchronized int nAllocationCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800303 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800304 return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700305 }
Tim Murraya3145512012-12-04 17:59:29 -0800306
307 native int rsnAllocationCreateBitmapBackedAllocation(int con, int type, int mip, Bitmap bmp, int usage);
308 synchronized int nAllocationCreateBitmapBackedAllocation(int type, int mip, Bitmap bmp, int usage) {
309 validate();
310 return rsnAllocationCreateBitmapBackedAllocation(mContext, type, mip, bmp, usage);
311 }
312
313
Jason Sams5476b452010-12-08 16:14:36 -0800314 native int rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
315 synchronized int nAllocationCubeCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800316 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800317 return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800318 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700319 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
320 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800321 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700322 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
323 }
Jason Sams5476b452010-12-08 16:14:36 -0800324 native int rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage);
325 synchronized int nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800326 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800327 return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
328 }
329
Jason Sams4ef66502010-12-10 16:03:15 -0800330 native void rsnAllocationCopyToBitmap(int con, int alloc, Bitmap bmp);
331 synchronized void nAllocationCopyToBitmap(int alloc, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800332 validate();
Jason Sams4ef66502010-12-10 16:03:15 -0800333 rsnAllocationCopyToBitmap(mContext, alloc, bmp);
334 }
335
336
Jason Sams5476b452010-12-08 16:14:36 -0800337 native void rsnAllocationSyncAll(int con, int alloc, int src);
338 synchronized void nAllocationSyncAll(int alloc, int src) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800339 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800340 rsnAllocationSyncAll(mContext, alloc, src);
341 }
Jason Sams72226e02013-02-22 12:45:54 -0800342 native Surface rsnAllocationGetSurface(int con, int alloc);
343 synchronized Surface nAllocationGetSurface(int alloc) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800344 validate();
Jason Sams72226e02013-02-22 12:45:54 -0800345 return rsnAllocationGetSurface(mContext, alloc);
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700346 }
Jason Samsfb9aa9f2012-03-28 15:30:07 -0700347 native void rsnAllocationSetSurface(int con, int alloc, Surface sur);
348 synchronized void nAllocationSetSurface(int alloc, Surface sur) {
Jason Sams163766c2012-02-15 12:04:24 -0800349 validate();
Jason Samsfb9aa9f2012-03-28 15:30:07 -0700350 rsnAllocationSetSurface(mContext, alloc, sur);
Jason Sams163766c2012-02-15 12:04:24 -0800351 }
352 native void rsnAllocationIoSend(int con, int alloc);
353 synchronized void nAllocationIoSend(int alloc) {
354 validate();
355 rsnAllocationIoSend(mContext, alloc);
356 }
357 native void rsnAllocationIoReceive(int con, int alloc);
358 synchronized void nAllocationIoReceive(int alloc) {
359 validate();
360 rsnAllocationIoReceive(mContext, alloc);
361 }
362
Jason Sams615e7ce2012-01-13 14:01:20 -0800363
Jason Samsf7086092011-01-12 13:28:37 -0800364 native void rsnAllocationGenerateMipmaps(int con, int alloc);
365 synchronized void nAllocationGenerateMipmaps(int alloc) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800366 validate();
Jason Samsf7086092011-01-12 13:28:37 -0800367 rsnAllocationGenerateMipmaps(mContext, alloc);
368 }
Jason Sams4ef66502010-12-10 16:03:15 -0800369 native void rsnAllocationCopyFromBitmap(int con, int alloc, Bitmap bmp);
370 synchronized void nAllocationCopyFromBitmap(int alloc, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800371 validate();
Jason Sams4ef66502010-12-10 16:03:15 -0800372 rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
Jason Sams2e1872f2010-08-17 16:25:41 -0700373 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700374
Jason Sams49a05d72010-12-29 14:31:29 -0800375
376 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, int[] d, int sizeBytes);
377 synchronized void nAllocationData1D(int id, int off, int mip, int count, int[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800378 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800379 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700380 }
Jason Sams49a05d72010-12-29 14:31:29 -0800381 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, short[] d, int sizeBytes);
382 synchronized void nAllocationData1D(int id, int off, int mip, int count, short[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800383 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800384 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
385 }
386 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, byte[] d, int sizeBytes);
387 synchronized void nAllocationData1D(int id, int off, int mip, int count, byte[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800388 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800389 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
390 }
391 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, float[] d, int sizeBytes);
392 synchronized void nAllocationData1D(int id, int off, int mip, int count, float[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800393 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800394 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700395 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700396
Jason Sams49a05d72010-12-29 14:31:29 -0800397 native void rsnAllocationElementData1D(int con, int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes);
398 synchronized void nAllocationElementData1D(int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800399 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800400 rsnAllocationElementData1D(mContext, id, xoff, mip, compIdx, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700401 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700402
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700403 native void rsnAllocationData2D(int con,
404 int dstAlloc, int dstXoff, int dstYoff,
405 int dstMip, int dstFace,
406 int width, int height,
407 int srcAlloc, int srcXoff, int srcYoff,
408 int srcMip, int srcFace);
409 synchronized void nAllocationData2D(int dstAlloc, int dstXoff, int dstYoff,
410 int dstMip, int dstFace,
411 int width, int height,
412 int srcAlloc, int srcXoff, int srcYoff,
413 int srcMip, int srcFace) {
414 validate();
415 rsnAllocationData2D(mContext,
416 dstAlloc, dstXoff, dstYoff,
417 dstMip, dstFace,
418 width, height,
419 srcAlloc, srcXoff, srcYoff,
420 srcMip, srcFace);
421 }
422
Jason Samsfa445b92011-01-07 17:00:07 -0800423 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes);
424 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800425 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800426 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
427 }
428 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes);
429 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800430 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800431 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
432 }
Jason Sams49a05d72010-12-29 14:31:29 -0800433 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes);
434 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800435 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800436 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700437 }
Jason Sams49a05d72010-12-29 14:31:29 -0800438 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes);
439 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800440 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800441 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700442 }
Jason Samsfa445b92011-01-07 17:00:07 -0800443 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, Bitmap b);
444 synchronized void nAllocationData2D(int 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
Jason Samsb05d6892013-04-09 15:59:24 -0700449 native void rsnAllocationData3D(int con,
450 int dstAlloc, int dstXoff, int dstYoff, int dstZoff,
451 int dstMip,
452 int width, int height, int depth,
453 int srcAlloc, int srcXoff, int srcYoff, int srcZoff,
454 int srcMip);
455 synchronized void nAllocationData3D(int dstAlloc, int dstXoff, int dstYoff, int dstZoff,
456 int dstMip,
457 int width, int height, int depth,
458 int srcAlloc, int srcXoff, int srcYoff, int srcZoff,
459 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
467 native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, byte[] d, int sizeBytes);
468 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, byte[] d, int sizeBytes) {
469 validate();
470 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
471 }
472 native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, short[] d, int sizeBytes);
473 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, short[] d, int sizeBytes) {
474 validate();
475 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
476 }
477 native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, int[] d, int sizeBytes);
478 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, int[] d, int sizeBytes) {
479 validate();
480 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
481 }
482 native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, float[] d, int sizeBytes);
483 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, float[] d, int sizeBytes) {
484 validate();
485 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
486 }
487
488
Jason Samsfa445b92011-01-07 17:00:07 -0800489 native void rsnAllocationRead(int con, int id, byte[] d);
490 synchronized void nAllocationRead(int id, byte[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800491 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800492 rsnAllocationRead(mContext, id, d);
493 }
494 native void rsnAllocationRead(int con, int id, short[] d);
495 synchronized void nAllocationRead(int id, short[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800496 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800497 rsnAllocationRead(mContext, id, d);
498 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700499 native void rsnAllocationRead(int con, int id, int[] d);
500 synchronized void nAllocationRead(int id, int[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800501 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700502 rsnAllocationRead(mContext, id, d);
503 }
504 native void rsnAllocationRead(int con, int id, float[] d);
505 synchronized void nAllocationRead(int id, float[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800506 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700507 rsnAllocationRead(mContext, id, d);
508 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700509 native int rsnAllocationGetType(int con, int id);
510 synchronized int nAllocationGetType(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800511 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700512 return rsnAllocationGetType(mContext, id);
513 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700514
Jason Sams5edc6082010-10-05 13:32:49 -0700515 native void rsnAllocationResize1D(int con, int id, int dimX);
516 synchronized void nAllocationResize1D(int id, int dimX) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800517 validate();
Jason Sams5edc6082010-10-05 13:32:49 -0700518 rsnAllocationResize1D(mContext, id, dimX);
519 }
Jason Sams5edc6082010-10-05 13:32:49 -0700520
Jason Sams2e1872f2010-08-17 16:25:41 -0700521 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
522 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800523 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700524 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
525 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800526 native int rsnFileA3DCreateFromFile(int con, String path);
527 synchronized int nFileA3DCreateFromFile(String path) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800528 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800529 return rsnFileA3DCreateFromFile(mContext, path);
530 }
531 native int rsnFileA3DCreateFromAsset(int con, AssetManager mgr, String path);
532 synchronized int nFileA3DCreateFromAsset(AssetManager mgr, String path) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800533 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800534 return rsnFileA3DCreateFromAsset(mContext, mgr, path);
535 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700536 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
537 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800538 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700539 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
540 }
541 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
542 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800543 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700544 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
545 }
546 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
547 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800548 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700549 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
550 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700551
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800552 native int rsnFontCreateFromFile(int con, String fileName, float size, int dpi);
553 synchronized int nFontCreateFromFile(String fileName, float size, int dpi) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800554 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700555 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
556 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800557 native int rsnFontCreateFromAssetStream(int con, String name, float size, int dpi, int assetStream);
558 synchronized int nFontCreateFromAssetStream(String name, float size, int dpi, int assetStream) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800559 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800560 return rsnFontCreateFromAssetStream(mContext, name, size, dpi, assetStream);
561 }
562 native int rsnFontCreateFromAsset(int con, AssetManager mgr, String path, float size, int dpi);
563 synchronized int nFontCreateFromAsset(AssetManager mgr, String path, float size, int dpi) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800564 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800565 return rsnFontCreateFromAsset(mContext, mgr, path, size, dpi);
566 }
Jason Sams22534172009-08-04 16:58:20 -0700567
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700568
Jason Sams2e1872f2010-08-17 16:25:41 -0700569 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
570 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800571 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700572 rsnScriptBindAllocation(mContext, script, alloc, slot);
573 }
574 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
575 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800576 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700577 rsnScriptSetTimeZone(mContext, script, timeZone);
578 }
579 native void rsnScriptInvoke(int con, int id, int slot);
580 synchronized void nScriptInvoke(int id, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800581 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700582 rsnScriptInvoke(mContext, id, slot);
583 }
Jason Sams6e494d32011-04-27 16:33:11 -0700584 native void rsnScriptForEach(int con, int id, int slot, int ain, int aout, byte[] params);
585 native void rsnScriptForEach(int con, int id, int slot, int ain, int aout);
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800586 native void rsnScriptForEachClipped(int con, int id, int slot, int ain, int aout, byte[] params,
587 int xstart, int xend, int ystart, int yend, int zstart, int zend);
Stephen Hinesdac6ed02013-02-13 00:09:02 -0800588 native void rsnScriptForEachClipped(int con, int id, int slot, int ain, int aout,
589 int xstart, int xend, int ystart, int yend, int zstart, int zend);
Jason Sams6e494d32011-04-27 16:33:11 -0700590 synchronized void nScriptForEach(int id, int slot, int ain, int aout, byte[] params) {
591 validate();
592 if (params == null) {
593 rsnScriptForEach(mContext, id, slot, ain, aout);
594 } else {
595 rsnScriptForEach(mContext, id, slot, ain, aout, params);
596 }
597 }
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800598
599 synchronized void nScriptForEachClipped(int id, int slot, int ain, int aout, byte[] params,
600 int xstart, int xend, int ystart, int yend, int zstart, int zend) {
601 validate();
Stephen Hinesdac6ed02013-02-13 00:09:02 -0800602 if (params == null) {
603 rsnScriptForEachClipped(mContext, id, slot, ain, aout, xstart, xend, ystart, yend, zstart, zend);
604 } else {
605 rsnScriptForEachClipped(mContext, id, slot, ain, aout, params, xstart, xend, ystart, yend, zstart, zend);
606 }
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800607 }
608
Jason Sams2e1872f2010-08-17 16:25:41 -0700609 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
610 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800611 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700612 rsnScriptInvokeV(mContext, id, slot, params);
613 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700614
Jason Sams2e1872f2010-08-17 16:25:41 -0700615 native void rsnScriptSetVarI(int con, int id, int slot, int val);
616 synchronized void nScriptSetVarI(int id, int slot, int val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800617 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700618 rsnScriptSetVarI(mContext, id, slot, val);
619 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700620 native int rsnScriptGetVarI(int con, int id, int slot);
621 synchronized int nScriptGetVarI(int id, int slot) {
622 validate();
623 return rsnScriptGetVarI(mContext, id, slot);
624 }
625
Stephen Hines031ec58c2010-10-11 10:54:21 -0700626 native void rsnScriptSetVarJ(int con, int id, int slot, long val);
627 synchronized void nScriptSetVarJ(int id, int slot, long val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800628 validate();
Stephen Hines031ec58c2010-10-11 10:54:21 -0700629 rsnScriptSetVarJ(mContext, id, slot, val);
630 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700631 native long rsnScriptGetVarJ(int con, int id, int slot);
632 synchronized long nScriptGetVarJ(int id, int slot) {
633 validate();
634 return rsnScriptGetVarJ(mContext, id, slot);
635 }
636
Jason Sams2e1872f2010-08-17 16:25:41 -0700637 native void rsnScriptSetVarF(int con, int id, int slot, float val);
638 synchronized void nScriptSetVarF(int id, int slot, float val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800639 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700640 rsnScriptSetVarF(mContext, id, slot, val);
641 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700642 native float rsnScriptGetVarF(int con, int id, int slot);
643 synchronized float nScriptGetVarF(int id, int slot) {
644 validate();
645 return rsnScriptGetVarF(mContext, id, slot);
646 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700647 native void rsnScriptSetVarD(int con, int id, int slot, double val);
648 synchronized void nScriptSetVarD(int id, int slot, double val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800649 validate();
Stephen Hinesca54ec32010-09-20 17:20:30 -0700650 rsnScriptSetVarD(mContext, id, slot, val);
651 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700652 native double rsnScriptGetVarD(int con, int id, int slot);
653 synchronized double nScriptGetVarD(int id, int slot) {
654 validate();
655 return rsnScriptGetVarD(mContext, id, slot);
656 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700657 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
658 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800659 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700660 rsnScriptSetVarV(mContext, id, slot, val);
661 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700662 native void rsnScriptGetVarV(int con, int id, int slot, byte[] val);
663 synchronized void nScriptGetVarV(int id, int slot, byte[] val) {
664 validate();
665 rsnScriptGetVarV(mContext, id, slot, val);
666 }
Stephen Hinesadeb8092012-04-20 14:26:06 -0700667 native void rsnScriptSetVarVE(int con, int id, int slot, byte[] val,
668 int e, int[] dims);
669 synchronized void nScriptSetVarVE(int id, int slot, byte[] val,
670 int e, int[] dims) {
671 validate();
672 rsnScriptSetVarVE(mContext, id, slot, val, e, dims);
673 }
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800674 native void rsnScriptSetVarObj(int con, int id, int slot, int val);
675 synchronized void nScriptSetVarObj(int id, int slot, int val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800676 validate();
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800677 rsnScriptSetVarObj(mContext, id, slot, val);
678 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700679
Jason Samse4a06c52011-03-16 16:29:28 -0700680 native int rsnScriptCCreate(int con, String resName, String cacheDir,
681 byte[] script, int length);
682 synchronized int nScriptCCreate(String resName, String cacheDir, byte[] script, int length) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800683 validate();
Jason Samse4a06c52011-03-16 16:29:28 -0700684 return rsnScriptCCreate(mContext, resName, cacheDir, script, length);
Jason Sams2e1872f2010-08-17 16:25:41 -0700685 }
Jason Samsebfb4362009-09-23 13:57:02 -0700686
Jason Sams6ab97682012-08-10 12:09:43 -0700687 native int rsnScriptIntrinsicCreate(int con, int id, int eid);
688 synchronized int nScriptIntrinsicCreate(int id, int eid) {
689 validate();
690 return rsnScriptIntrinsicCreate(mContext, id, eid);
691 }
692
Jason Sams08a81582012-09-18 12:32:10 -0700693 native int rsnScriptKernelIDCreate(int con, int sid, int slot, int sig);
694 synchronized int nScriptKernelIDCreate(int sid, int slot, int sig) {
695 validate();
696 return rsnScriptKernelIDCreate(mContext, sid, slot, sig);
697 }
698
699 native int rsnScriptFieldIDCreate(int con, int sid, int slot);
700 synchronized int nScriptFieldIDCreate(int sid, int slot) {
701 validate();
702 return rsnScriptFieldIDCreate(mContext, sid, slot);
703 }
704
705 native int rsnScriptGroupCreate(int con, int[] kernels, int[] src, int[] dstk, int[] dstf, int[] types);
706 synchronized int nScriptGroupCreate(int[] kernels, int[] src, int[] dstk, int[] dstf, int[] types) {
707 validate();
708 return rsnScriptGroupCreate(mContext, kernels, src, dstk, dstf, types);
709 }
710
711 native void rsnScriptGroupSetInput(int con, int group, int kernel, int alloc);
712 synchronized void nScriptGroupSetInput(int group, int kernel, int alloc) {
713 validate();
714 rsnScriptGroupSetInput(mContext, group, kernel, alloc);
715 }
716
717 native void rsnScriptGroupSetOutput(int con, int group, int kernel, int alloc);
718 synchronized void nScriptGroupSetOutput(int group, int kernel, int alloc) {
719 validate();
720 rsnScriptGroupSetOutput(mContext, group, kernel, alloc);
721 }
722
723 native void rsnScriptGroupExecute(int con, int group);
724 synchronized void nScriptGroupExecute(int group) {
725 validate();
726 rsnScriptGroupExecute(mContext, group);
727 }
728
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700729 native int rsnSamplerCreate(int con, int magFilter, int minFilter,
730 int wrapS, int wrapT, int wrapR, float aniso);
731 synchronized int nSamplerCreate(int magFilter, int minFilter,
732 int wrapS, int wrapT, int wrapR, float aniso) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800733 validate();
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700734 return rsnSamplerCreate(mContext, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
Jason Sams2e1872f2010-08-17 16:25:41 -0700735 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800736
Jason Sams331bf9b2011-04-06 11:23:54 -0700737 native int rsnProgramStoreCreate(int con, boolean r, boolean g, boolean b, boolean a,
738 boolean depthMask, boolean dither,
739 int srcMode, int dstMode, int depthFunc);
740 synchronized int nProgramStoreCreate(boolean r, boolean g, boolean b, boolean a,
741 boolean depthMask, boolean dither,
742 int srcMode, int dstMode, int depthFunc) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800743 validate();
Jason Samsbd184c52011-04-06 11:44:47 -0700744 return rsnProgramStoreCreate(mContext, r, g, b, a, depthMask, dither, srcMode,
745 dstMode, depthFunc);
Jason Sams2e1872f2010-08-17 16:25:41 -0700746 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700747
Jason Sams94aaed32011-09-23 14:18:53 -0700748 native int rsnProgramRasterCreate(int con, boolean pointSprite, int cullMode);
749 synchronized int nProgramRasterCreate(boolean pointSprite, int cullMode) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800750 validate();
Jason Sams94aaed32011-09-23 14:18:53 -0700751 return rsnProgramRasterCreate(mContext, pointSprite, cullMode);
Jason Sams2e1872f2010-08-17 16:25:41 -0700752 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700753
Jason Sams2e1872f2010-08-17 16:25:41 -0700754 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
755 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800756 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700757 rsnProgramBindConstants(mContext, pv, slot, mID);
758 }
759 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
760 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800761 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700762 rsnProgramBindTexture(mContext, vpf, slot, a);
763 }
764 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
765 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800766 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700767 rsnProgramBindSampler(mContext, vpf, slot, s);
768 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800769 native int rsnProgramFragmentCreate(int con, String shader, String[] texNames, int[] params);
770 synchronized int nProgramFragmentCreate(String shader, String[] texNames, int[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800771 validate();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800772 return rsnProgramFragmentCreate(mContext, shader, texNames, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700773 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800774 native int rsnProgramVertexCreate(int con, String shader, String[] texNames, int[] params);
775 synchronized int nProgramVertexCreate(String shader, String[] texNames, int[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800776 validate();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800777 return rsnProgramVertexCreate(mContext, shader, texNames, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700778 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700779
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700780 native int rsnMeshCreate(int con, int[] vtx, int[] idx, int[] prim);
781 synchronized int nMeshCreate(int[] vtx, int[] idx, int[] prim) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800782 validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700783 return rsnMeshCreate(mContext, vtx, idx, prim);
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800784 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700785 native int rsnMeshGetVertexBufferCount(int con, int id);
786 synchronized int nMeshGetVertexBufferCount(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800787 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700788 return rsnMeshGetVertexBufferCount(mContext, id);
789 }
790 native int rsnMeshGetIndexCount(int con, int id);
791 synchronized int nMeshGetIndexCount(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800792 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700793 return rsnMeshGetIndexCount(mContext, id);
794 }
795 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
796 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800797 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700798 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
799 }
800 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
801 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800802 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700803 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
804 }
805
Jason Samsf15ed012011-10-31 13:23:43 -0700806 native int rsnPathCreate(int con, int prim, boolean isStatic, int vtx, int loop, float q);
807 synchronized int nPathCreate(int prim, boolean isStatic, int vtx, int loop, float q) {
808 validate();
809 return rsnPathCreate(mContext, prim, isStatic, vtx, loop, q);
810 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700811
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800812 int mDev;
813 int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700814 @SuppressWarnings({"FieldCanBeLocal"})
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800815 MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700816
Jason Sams8cb39de2010-06-01 15:47:01 -0700817 Element mElement_U8;
818 Element mElement_I8;
819 Element mElement_U16;
820 Element mElement_I16;
821 Element mElement_U32;
822 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -0700823 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -0700824 Element mElement_I64;
Jason Sams8cb39de2010-06-01 15:47:01 -0700825 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700826 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700827 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700828
Jason Sams8cb39de2010-06-01 15:47:01 -0700829 Element mElement_ELEMENT;
830 Element mElement_TYPE;
831 Element mElement_ALLOCATION;
832 Element mElement_SAMPLER;
833 Element mElement_SCRIPT;
834 Element mElement_MESH;
835 Element mElement_PROGRAM_FRAGMENT;
836 Element mElement_PROGRAM_VERTEX;
837 Element mElement_PROGRAM_RASTER;
838 Element mElement_PROGRAM_STORE;
Stephen Hines3a291412012-04-11 17:27:29 -0700839 Element mElement_FONT;
Jason Samsa70f4162010-03-26 15:33:42 -0700840
Jason Sams3c0dfba2009-09-27 17:50:38 -0700841 Element mElement_A_8;
842 Element mElement_RGB_565;
843 Element mElement_RGB_888;
844 Element mElement_RGBA_5551;
845 Element mElement_RGBA_4444;
846 Element mElement_RGBA_8888;
847
Jason Sams8cb39de2010-06-01 15:47:01 -0700848 Element mElement_FLOAT_2;
849 Element mElement_FLOAT_3;
850 Element mElement_FLOAT_4;
Stephen Hines836c4a52011-06-01 14:38:10 -0700851
852 Element mElement_DOUBLE_2;
853 Element mElement_DOUBLE_3;
854 Element mElement_DOUBLE_4;
855
856 Element mElement_UCHAR_2;
857 Element mElement_UCHAR_3;
Jason Sams8cb39de2010-06-01 15:47:01 -0700858 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800859
Stephen Hines836c4a52011-06-01 14:38:10 -0700860 Element mElement_CHAR_2;
861 Element mElement_CHAR_3;
862 Element mElement_CHAR_4;
863
864 Element mElement_USHORT_2;
865 Element mElement_USHORT_3;
866 Element mElement_USHORT_4;
867
868 Element mElement_SHORT_2;
869 Element mElement_SHORT_3;
870 Element mElement_SHORT_4;
871
872 Element mElement_UINT_2;
873 Element mElement_UINT_3;
874 Element mElement_UINT_4;
875
876 Element mElement_INT_2;
877 Element mElement_INT_3;
878 Element mElement_INT_4;
879
880 Element mElement_ULONG_2;
881 Element mElement_ULONG_3;
882 Element mElement_ULONG_4;
883
884 Element mElement_LONG_2;
885 Element mElement_LONG_3;
886 Element mElement_LONG_4;
887
Jason Sams1d45c472010-08-25 14:31:48 -0700888 Element mElement_MATRIX_4X4;
889 Element mElement_MATRIX_3X3;
890 Element mElement_MATRIX_2X2;
891
Jason Sams4d339932010-05-11 14:03:58 -0700892 Sampler mSampler_CLAMP_NEAREST;
893 Sampler mSampler_CLAMP_LINEAR;
894 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
895 Sampler mSampler_WRAP_NEAREST;
896 Sampler mSampler_WRAP_LINEAR;
897 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800898 Sampler mSampler_MIRRORED_REPEAT_NEAREST;
899 Sampler mSampler_MIRRORED_REPEAT_LINEAR;
900 Sampler mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
Jason Sams4d339932010-05-11 14:03:58 -0700901
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700902 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
903 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700904 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
905 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700906
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700907 ProgramRaster mProgramRaster_CULL_BACK;
908 ProgramRaster mProgramRaster_CULL_FRONT;
909 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700910
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700911 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700912 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700913
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700914 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800915 * Base class application should derive from for handling RS messages
Stephen Hines8cecbb52011-02-28 18:20:34 -0800916 * coming from their scripts. When a script calls sendToClient the data
Jason Sams27676fe2010-11-10 17:00:59 -0800917 * fields will be filled in and then the run method called by a message
918 * handling thread. This will occur some time after sendToClient completes
919 * in the script.
920 *
921 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800922 public static class RSMessageHandler implements Runnable {
Jason Sams516c3192009-10-06 13:58:47 -0700923 protected int[] mData;
924 protected int mID;
Jason Sams1c415172010-11-08 17:06:46 -0800925 protected int mLength;
Jason Sams516c3192009-10-06 13:58:47 -0700926 public void run() {
927 }
928 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700929 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800930 * If an application is expecting messages it should set this field to an
931 * instance of RSMessage. This instance will receive all the user messages
932 * sent from sendToClient by scripts from this context.
933 *
934 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800935 RSMessageHandler mMessageCallback = null;
936
937 public void setMessageHandler(RSMessageHandler msg) {
938 mMessageCallback = msg;
939 }
940 public RSMessageHandler getMessageHandler() {
941 return mMessageCallback;
942 }
Jason Sams516c3192009-10-06 13:58:47 -0700943
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700944 /**
Jason Sams02d56d92013-04-12 16:40:50 -0700945 * Place a message into the message queue to be sent back to the message
946 * handler once all previous commands have been executed.
Jason Sams455d6442013-02-05 19:20:18 -0800947 *
948 * @param id
949 * @param data
950 */
951 public void sendMessage(int id, int[] data) {
952 nContextSendMessage(id, data);
953 }
954
955 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800956 * Runtime error base class. An application should derive from this class
957 * if it wishes to install an error handler. When errors occur at runtime
958 * the fields in this class will be filled and the run method called.
959 *
960 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800961 public static class RSErrorHandler implements Runnable {
Jason Sams1c415172010-11-08 17:06:46 -0800962 protected String mErrorMessage;
963 protected int mErrorNum;
964 public void run() {
965 }
966 }
Jason Sams27676fe2010-11-10 17:00:59 -0800967
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700968 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800969 * Application Error handler. All runtime errors will be dispatched to the
970 * instance of RSAsyncError set here. If this field is null a
971 * RSRuntimeException will instead be thrown with details about the error.
972 * This will cause program termaination.
973 *
974 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800975 RSErrorHandler mErrorCallback = null;
976
977 public void setErrorHandler(RSErrorHandler msg) {
978 mErrorCallback = msg;
979 }
980 public RSErrorHandler getErrorHandler() {
981 return mErrorCallback;
982 }
Jason Sams1c415172010-11-08 17:06:46 -0800983
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700984 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800985 * RenderScript worker threads priority enumeration. The default value is
986 * NORMAL. Applications wishing to do background processing such as
987 * wallpapers should set their priority to LOW to avoid starving forground
988 * processes.
989 */
Jason Sams7d787b42009-11-15 12:14:26 -0800990 public enum Priority {
Glenn Kasten260c77a2011-06-01 17:25:54 -0700991 LOW (Process.THREAD_PRIORITY_BACKGROUND + (5 * Process.THREAD_PRIORITY_LESS_FAVORABLE)),
992 NORMAL (Process.THREAD_PRIORITY_DISPLAY);
Jason Sams7d787b42009-11-15 12:14:26 -0800993
994 int mID;
995 Priority(int id) {
996 mID = id;
997 }
998 }
999
Jason Sams771bebb2009-12-07 12:40:12 -08001000 void validate() {
1001 if (mContext == 0) {
Jason Samsc1d62102010-11-04 14:32:19 -07001002 throw new RSInvalidStateException("Calling RS with no Context active.");
Jason Sams771bebb2009-12-07 12:40:12 -08001003 }
1004 }
1005
Jason Sams27676fe2010-11-10 17:00:59 -08001006
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001007 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001008 * Change the priority of the worker threads for this context.
1009 *
1010 * @param p New priority to be set.
1011 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001012 public void setPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -08001013 validate();
Jason Sams7d787b42009-11-15 12:14:26 -08001014 nContextSetPriority(p.mID);
1015 }
1016
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001017 static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -07001018 RenderScript mRS;
1019 boolean mRun = true;
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001020 int[] mAuxData = new int[2];
Jason Sams1c415172010-11-08 17:06:46 -08001021
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001022 static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
1023 static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
1024 static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
1025 static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
1026 static final int RS_MESSAGE_TO_CLIENT_USER = 4;
Jason Sams739c8262013-04-11 18:07:52 -07001027 static final int RS_MESSAGE_TO_CLIENT_NEW_BUFFER = 5;
Jason Sams516c3192009-10-06 13:58:47 -07001028
Stephen Hines42028a82013-04-17 19:22:01 -07001029 static final int RS_ERROR_FATAL_DEBUG = 0x0800;
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001030 static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
Jason Samsadd9d962010-11-22 16:20:16 -08001031
Jason Sams516c3192009-10-06 13:58:47 -07001032 MessageThread(RenderScript rs) {
1033 super("RSMessageThread");
1034 mRS = rs;
1035
1036 }
1037
1038 public void run() {
1039 // This function is a temporary solution. The final solution will
1040 // used typed allocations where the message id is the type indicator.
1041 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -07001042 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -07001043 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -07001044 rbuf[0] = 0;
Jason Samsedbfabd2011-05-17 15:01:29 -07001045 int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001046 int size = mAuxData[1];
1047 int subID = mAuxData[0];
Jason Sams1c415172010-11-08 17:06:46 -08001048
1049 if (msg == RS_MESSAGE_TO_CLIENT_USER) {
1050 if ((size>>2) >= rbuf.length) {
1051 rbuf = new int[(size + 3) >> 2];
1052 }
Jason Samsedbfabd2011-05-17 15:01:29 -07001053 if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) !=
1054 RS_MESSAGE_TO_CLIENT_USER) {
1055 throw new RSDriverException("Error processing message from Renderscript.");
1056 }
Jason Sams1c415172010-11-08 17:06:46 -08001057
1058 if(mRS.mMessageCallback != null) {
1059 mRS.mMessageCallback.mData = rbuf;
1060 mRS.mMessageCallback.mID = subID;
1061 mRS.mMessageCallback.mLength = size;
1062 mRS.mMessageCallback.run();
Jason Sams1d45c472010-08-25 14:31:48 -07001063 } else {
Jason Sams1c415172010-11-08 17:06:46 -08001064 throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
Jason Sams516c3192009-10-06 13:58:47 -07001065 }
Stephen Hinesab98bb62010-09-24 14:38:30 -07001066 continue;
Jason Sams516c3192009-10-06 13:58:47 -07001067 }
Jason Sams1c415172010-11-08 17:06:46 -08001068
1069 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
1070 String e = mRS.nContextGetErrorMessage(mRS.mContext);
1071
Stephen Hines42028a82013-04-17 19:22:01 -07001072 // Throw RSRuntimeException under the following conditions:
1073 //
1074 // 1) It is an unknown fatal error.
1075 // 2) It is a debug fatal error, and we are not in a
1076 // debug context.
1077 // 3) It is a debug fatal error, and we do not have an
1078 // error callback.
1079 if (subID >= RS_ERROR_FATAL_UNKNOWN ||
1080 (subID >= RS_ERROR_FATAL_DEBUG &&
1081 (mRS.mContextType != ContextType.DEBUG ||
1082 mRS.mErrorCallback == null))) {
Jason Samsadd9d962010-11-22 16:20:16 -08001083 throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
1084 }
1085
Jason Sams1c415172010-11-08 17:06:46 -08001086 if(mRS.mErrorCallback != null) {
1087 mRS.mErrorCallback.mErrorMessage = e;
1088 mRS.mErrorCallback.mErrorNum = subID;
1089 mRS.mErrorCallback.run();
1090 } else {
Jason Samsa4b7bc92013-02-05 15:05:39 -08001091 android.util.Log.e(LOG_TAG, "non fatal RS error, " + e);
Stephen Hinesbe74bdd2012-02-03 15:29:36 -08001092 // Do not throw here. In these cases, we do not have
1093 // a fatal error.
Jason Sams1c415172010-11-08 17:06:46 -08001094 }
1095 continue;
1096 }
1097
Jason Sams739c8262013-04-11 18:07:52 -07001098 if (msg == RS_MESSAGE_TO_CLIENT_NEW_BUFFER) {
1099 Allocation.sendBufferNotification(subID);
1100 continue;
1101 }
1102
Jason Sams1c415172010-11-08 17:06:46 -08001103 // 2: teardown.
1104 // But we want to avoid starving other threads during
1105 // teardown by yielding until the next line in the destructor
1106 // can execute to set mRun = false
1107 try {
1108 sleep(1, 0);
1109 } catch(InterruptedException e) {
Jason Sams516c3192009-10-06 13:58:47 -07001110 }
Jason Sams516c3192009-10-06 13:58:47 -07001111 }
Tim Murrayda67deb2013-05-09 12:02:50 -07001112 //Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -07001113 }
1114 }
1115
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001116 RenderScript(Context ctx) {
Stephen Hines42028a82013-04-17 19:22:01 -07001117 mContextType = ContextType.NORMAL;
Jason Sams1a4e1f32012-02-24 17:51:24 -08001118 if (ctx != null) {
1119 mApplicationContext = ctx.getApplicationContext();
1120 }
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001121 }
1122
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001123 /**
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001124 * Gets the application context associated with the RenderScript context.
1125 *
1126 * @return The application context.
1127 */
1128 public final Context getApplicationContext() {
1129 return mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001130 }
1131
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001132 /**
Jason Samsadd26dc2013-02-22 18:43:45 -08001133 * @hide
1134 */
1135 public static RenderScript create(Context ctx, int sdkVersion) {
1136 return create(ctx, sdkVersion, ContextType.NORMAL);
1137 }
1138
1139 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001140 * Create a basic RenderScript context.
1141 *
Jason Sams1a4e1f32012-02-24 17:51:24 -08001142 * @hide
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001143 * @param ctx The context.
Jason Sams27676fe2010-11-10 17:00:59 -08001144 * @return RenderScript
1145 */
Jason Samsadd26dc2013-02-22 18:43:45 -08001146 public static RenderScript create(Context ctx, int sdkVersion, ContextType ct) {
Dan Morrille4d9a012013-03-28 18:10:43 -07001147 if (!sInitialized) {
1148 Log.e(LOG_TAG, "RenderScript.create() called when disabled; someone is likely to crash");
1149 return null;
1150 }
1151
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001152 RenderScript rs = new RenderScript(ctx);
Jason Sams704ff642010-02-09 16:05:07 -08001153
1154 rs.mDev = rs.nDeviceCreate();
Jason Samsadd26dc2013-02-22 18:43:45 -08001155 rs.mContext = rs.nContextCreate(rs.mDev, 0, sdkVersion, ct.mID);
Stephen Hines42028a82013-04-17 19:22:01 -07001156 rs.mContextType = ct;
Jason Sams26985362011-05-03 15:01:58 -07001157 if (rs.mContext == 0) {
1158 throw new RSDriverException("Failed to create RS context.");
1159 }
Jason Sams704ff642010-02-09 16:05:07 -08001160 rs.mMessageThread = new MessageThread(rs);
1161 rs.mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -08001162 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -08001163 }
1164
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001165 /**
Jason Sams1a4e1f32012-02-24 17:51:24 -08001166 * Create a basic RenderScript context.
1167 *
1168 * @param ctx The context.
1169 * @return RenderScript
1170 */
1171 public static RenderScript create(Context ctx) {
Jason Samsadd26dc2013-02-22 18:43:45 -08001172 return create(ctx, ContextType.NORMAL);
1173 }
1174
1175 /**
1176 * Create a basic RenderScript context.
1177 *
Jason Samsadd26dc2013-02-22 18:43:45 -08001178 *
1179 * @param ctx The context.
Jason Sams02d56d92013-04-12 16:40:50 -07001180 * @param ct The type of context to be created.
Jason Samsadd26dc2013-02-22 18:43:45 -08001181 * @return RenderScript
1182 */
1183 public static RenderScript create(Context ctx, ContextType ct) {
Jason Sams1a4e1f32012-02-24 17:51:24 -08001184 int v = ctx.getApplicationInfo().targetSdkVersion;
Jason Samsadd26dc2013-02-22 18:43:45 -08001185 return create(ctx, v, ct);
Jason Sams1a4e1f32012-02-24 17:51:24 -08001186 }
1187
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001188 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001189 * Print the currently available debugging information about the state of
1190 * the RS context to the log.
1191 *
Jason Sams27676fe2010-11-10 17:00:59 -08001192 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001193 public void contextDump() {
Jason Sams5dbfe932010-01-27 14:41:43 -08001194 validate();
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001195 nContextDump(0);
Jason Sams715333b2009-11-17 17:26:46 -08001196 }
1197
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001198 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001199 * Wait for any commands in the fifo between the java bindings and native to
1200 * be processed.
1201 *
1202 */
Jason Sams96ed4cf2010-06-15 12:15:57 -07001203 public void finish() {
1204 nContextFinish();
1205 }
1206
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001207 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001208 * Destroy this renderscript context. Once this function is called its no
1209 * longer legal to use this or any objects created by this context.
1210 *
1211 */
Jason Samsf5b45962009-08-25 14:49:07 -07001212 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -08001213 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -07001214 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -07001215 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -07001216 try {
1217 mMessageThread.join();
1218 } catch(InterruptedException e) {
1219 }
Jason Sams516c3192009-10-06 13:58:47 -07001220
Jason Sams2e1872f2010-08-17 16:25:41 -07001221 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -07001222 mContext = 0;
1223
1224 nDeviceDestroy(mDev);
1225 mDev = 0;
1226 }
Jason Sams02fb2cb2009-05-28 15:37:57 -07001227
Jason Samsa9e7a052009-09-25 14:51:22 -07001228 boolean isAlive() {
1229 return mContext != 0;
1230 }
1231
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001232 int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -07001233 if(o != null) {
Jason Samse07694b2012-04-03 15:36:36 -07001234 return o.getID(this);
Jason Samsd8e41612009-08-20 17:22:40 -07001235 }
Jason Sams6b9dec02009-09-23 16:38:37 -07001236 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001237 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001238}