blob: 4de4766577c407e0fe363a9f246988de46797179 [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/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070036 * This class provides access to a RenderScript context, which controls RenderScript
37 * initialization, resource management, and teardown. An instance of the RenderScript
38 * class must be created before any other RS objects can be created.
Jason Sams27676fe2010-11-10 17:00:59 -080039 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080040 * <div class="special reference">
41 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070042 * <p>For more information about creating an application that uses RenderScript, read the
43 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080044 * </div>
Jason Samse29d4712009-07-23 15:19:03 -070045 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070046public class RenderScript {
Tim Murray6d7a53c2013-05-23 16:59:23 -070047 static final long TRACE_TAG = Trace.TRACE_TAG_RS;
48
Jason Sams3bc47d42009-11-12 15:10:25 -080049 static final String LOG_TAG = "RenderScript_jni";
Jason Samsbf6ef8d2010-12-06 15:59:59 -080050 static final boolean DEBUG = false;
Romain Guy650a3eb2009-08-31 14:06:43 -070051 @SuppressWarnings({"UnusedDeclaration", "deprecation"})
Joe Onorato43a17652011-04-06 19:22:23 -070052 static final boolean LOG_ENABLED = false;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070053
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080054 private Context mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070055
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080056 /*
Jack Palevich60aa3ea2009-05-26 13:45:08 -070057 * We use a class initializer to allow the native code to cache some
58 * field offsets.
59 */
Dan Morrille4d9a012013-03-28 18:10:43 -070060 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) // TODO: now used locally; remove?
Jason Samsbf6ef8d2010-12-06 15:59:59 -080061 static boolean sInitialized;
62 native static void _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070063
Jason Samsdba3ba52009-07-30 14:56:12 -070064
Jack Palevich60aa3ea2009-05-26 13:45:08 -070065 static {
66 sInitialized = false;
Dan Morrille4d9a012013-03-28 18:10:43 -070067 if (!SystemProperties.getBoolean("config.disable_renderscript", false)) {
68 try {
69 System.loadLibrary("rs_jni");
70 _nInit();
71 sInitialized = true;
72 } catch (UnsatisfiedLinkError e) {
73 Log.e(LOG_TAG, "Error loading RS jni library: " + e);
74 throw new RSRuntimeException("Error loading RS jni library: " + e);
75 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070076 }
77 }
78
Jason Sams2e1872f2010-08-17 16:25:41 -070079 // Non-threadsafe functions.
Jason Sams36e612a2009-07-31 16:26:13 -070080 native int nDeviceCreate();
81 native void nDeviceDestroy(int dev);
Jason Samsebfb4362009-09-23 13:57:02 -070082 native void nDeviceSetConfig(int dev, int param, int value);
Jason Samsedbfabd2011-05-17 15:01:29 -070083 native int nContextGetUserMessage(int con, int[] data);
Jason Sams1c415172010-11-08 17:06:46 -080084 native String nContextGetErrorMessage(int con);
Jason Samsedbfabd2011-05-17 15:01:29 -070085 native int nContextPeekMessage(int con, int[] subID);
Jason Sams2e1872f2010-08-17 16:25:41 -070086 native void nContextInitToClient(int con);
87 native void nContextDeinitToClient(int con);
Jason Sams3eaa3382009-06-10 15:04:38 -070088
Stephen Hines7d25a822013-04-09 23:51:56 -070089 static File mCacheDir;
Jason Samsa6f338c2012-02-24 16:22:16 -080090
91 /**
92 * Sets the directory to use as a persistent storage for the
93 * renderscript object file cache.
94 *
95 * @hide
96 * @param cacheDir A directory the current process can write to
97 */
Jason Samsa6f338c2012-02-24 16:22:16 -080098 public static void setupDiskCache(File cacheDir) {
Dan Morrille4d9a012013-03-28 18:10:43 -070099 if (!sInitialized) {
100 Log.e(LOG_TAG, "RenderScript.setupDiskCache() called when disabled");
101 return;
102 }
103
Stephen Hines7d25a822013-04-09 23:51:56 -0700104 // Defer creation of cache path to nScriptCCreate().
105 mCacheDir = cacheDir;
Jason Samsa6f338c2012-02-24 16:22:16 -0800106 }
107
Jason Sams02d56d92013-04-12 16:40:50 -0700108 /**
109 * ContextType specifies the specific type of context to be created.
110 *
111 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800112 public enum ContextType {
Jason Sams02d56d92013-04-12 16:40:50 -0700113 /**
114 * NORMAL context, this is the default and what shipping apps should
115 * use.
116 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800117 NORMAL (0),
Jason Sams02d56d92013-04-12 16:40:50 -0700118
119 /**
120 * DEBUG context, perform extra runtime checks to validate the
121 * kernels and APIs are being used as intended. Get and SetElementAt
122 * will be bounds checked in this mode.
123 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800124 DEBUG (1),
Jason Sams02d56d92013-04-12 16:40:50 -0700125
126 /**
127 * PROFILE context, Intended to be used once the first time an
128 * application is run on a new device. This mode allows the runtime to
129 * do additional testing and performance tuning.
130 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800131 PROFILE (2);
132
133 int mID;
134 ContextType(int id) {
135 mID = id;
136 }
137 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800138
Stephen Hines42028a82013-04-17 19:22:01 -0700139 ContextType mContextType;
140
Jason Sams2e1872f2010-08-17 16:25:41 -0700141 // Methods below are wrapped to protect the non-threadsafe
142 // lockless fifo.
Stephen Hines4382467a2011-08-01 15:02:34 -0700143 native int rsnContextCreateGL(int dev, int ver, int sdkVer,
Jason Sams11c8af92010-10-13 15:31:10 -0700144 int colorMin, int colorPref,
145 int alphaMin, int alphaPref,
146 int depthMin, int depthPref,
147 int stencilMin, int stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700148 int samplesMin, int samplesPref, float samplesQ, int dpi);
Stephen Hines4382467a2011-08-01 15:02:34 -0700149 synchronized int nContextCreateGL(int dev, int ver, int sdkVer,
Jason Sams11c8af92010-10-13 15:31:10 -0700150 int colorMin, int colorPref,
151 int alphaMin, int alphaPref,
152 int depthMin, int depthPref,
153 int stencilMin, int stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700154 int samplesMin, int samplesPref, float samplesQ, int dpi) {
Stephen Hines4382467a2011-08-01 15:02:34 -0700155 return rsnContextCreateGL(dev, ver, sdkVer, colorMin, colorPref,
Jason Sams11c8af92010-10-13 15:31:10 -0700156 alphaMin, alphaPref, depthMin, depthPref,
157 stencilMin, stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700158 samplesMin, samplesPref, samplesQ, dpi);
Jason Sams2e1872f2010-08-17 16:25:41 -0700159 }
Jason Samsadd26dc2013-02-22 18:43:45 -0800160 native int rsnContextCreate(int dev, int ver, int sdkVer, int contextType);
161 synchronized int nContextCreate(int dev, int ver, int sdkVer, int contextType) {
162 return rsnContextCreate(dev, ver, sdkVer, contextType);
Jason Sams2e1872f2010-08-17 16:25:41 -0700163 }
164 native void rsnContextDestroy(int con);
165 synchronized void nContextDestroy() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800166 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700167 rsnContextDestroy(mContext);
168 }
169 native void rsnContextSetSurface(int con, int w, int h, Surface sur);
170 synchronized void nContextSetSurface(int w, int h, Surface sur) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800171 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700172 rsnContextSetSurface(mContext, w, h, sur);
173 }
Jason Samsfaa32b32011-06-20 16:58:04 -0700174 native void rsnContextSetSurfaceTexture(int con, int w, int h, SurfaceTexture sur);
175 synchronized void nContextSetSurfaceTexture(int w, int h, SurfaceTexture sur) {
176 validate();
177 rsnContextSetSurfaceTexture(mContext, w, h, sur);
178 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700179 native void rsnContextSetPriority(int con, int p);
180 synchronized void nContextSetPriority(int p) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800181 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700182 rsnContextSetPriority(mContext, p);
183 }
184 native void rsnContextDump(int con, int bits);
185 synchronized void nContextDump(int bits) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800186 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700187 rsnContextDump(mContext, bits);
188 }
189 native void rsnContextFinish(int con);
190 synchronized void nContextFinish() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800191 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700192 rsnContextFinish(mContext);
193 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700194
Jason Sams455d6442013-02-05 19:20:18 -0800195 native void rsnContextSendMessage(int con, int id, int[] data);
196 synchronized void nContextSendMessage(int id, int[] data) {
197 validate();
198 rsnContextSendMessage(mContext, id, data);
199 }
200
Jason Sams2e1872f2010-08-17 16:25:41 -0700201 native void rsnContextBindRootScript(int con, int script);
202 synchronized void nContextBindRootScript(int script) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800203 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700204 rsnContextBindRootScript(mContext, script);
205 }
206 native void rsnContextBindSampler(int con, int sampler, int slot);
207 synchronized void nContextBindSampler(int sampler, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800208 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700209 rsnContextBindSampler(mContext, sampler, slot);
210 }
211 native void rsnContextBindProgramStore(int con, int pfs);
212 synchronized void nContextBindProgramStore(int pfs) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800213 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700214 rsnContextBindProgramStore(mContext, pfs);
215 }
216 native void rsnContextBindProgramFragment(int con, int pf);
217 synchronized void nContextBindProgramFragment(int pf) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800218 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700219 rsnContextBindProgramFragment(mContext, pf);
220 }
221 native void rsnContextBindProgramVertex(int con, int pv);
222 synchronized void nContextBindProgramVertex(int pv) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800223 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700224 rsnContextBindProgramVertex(mContext, pv);
225 }
226 native void rsnContextBindProgramRaster(int con, int pr);
227 synchronized void nContextBindProgramRaster(int pr) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800228 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700229 rsnContextBindProgramRaster(mContext, pr);
230 }
231 native void rsnContextPause(int con);
232 synchronized void nContextPause() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800233 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700234 rsnContextPause(mContext);
235 }
236 native void rsnContextResume(int con);
237 synchronized void nContextResume() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800238 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700239 rsnContextResume(mContext);
240 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700241
Jason Sams2e1872f2010-08-17 16:25:41 -0700242 native void rsnAssignName(int con, int obj, byte[] name);
243 synchronized void nAssignName(int obj, byte[] name) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800244 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700245 rsnAssignName(mContext, obj, name);
246 }
247 native String rsnGetName(int con, int obj);
248 synchronized String nGetName(int obj) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800249 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700250 return rsnGetName(mContext, obj);
251 }
252 native void rsnObjDestroy(int con, int id);
253 synchronized void nObjDestroy(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800254 // There is a race condition here. The calling code may be run
255 // by the gc while teardown is occuring. This protects againts
256 // deleting dead objects.
257 if (mContext != 0) {
258 rsnObjDestroy(mContext, id);
259 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700260 }
Jason Samsfe08d992009-05-27 14:45:32 -0700261
Jason Sams2e1872f2010-08-17 16:25:41 -0700262 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
263 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800264 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700265 return rsnElementCreate(mContext, type, kind, norm, vecSize);
266 }
Jason Sams70d4e502010-09-02 17:35:23 -0700267 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
268 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800269 validate();
Jason Sams70d4e502010-09-02 17:35:23 -0700270 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700271 }
272 native void rsnElementGetNativeData(int con, int id, int[] elementData);
273 synchronized void nElementGetNativeData(int id, int[] elementData) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800274 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700275 rsnElementGetNativeData(mContext, id, elementData);
276 }
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700277 native void rsnElementGetSubElements(int con, int id,
278 int[] IDs, String[] names, int[] arraySizes);
279 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names, int[] arraySizes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800280 validate();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700281 rsnElementGetSubElements(mContext, id, IDs, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700282 }
Jason Sams768bc022009-09-21 19:41:04 -0700283
Jason Samsb109cc72013-01-07 18:20:12 -0800284 native int rsnTypeCreate(int con, int eid, int x, int y, int z, boolean mips, boolean faces, int yuv);
285 synchronized int nTypeCreate(int eid, int x, int y, int z, boolean mips, boolean faces, int yuv) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800286 validate();
Jason Samsb109cc72013-01-07 18:20:12 -0800287 return rsnTypeCreate(mContext, eid, x, y, z, mips, faces, yuv);
Jason Sams2e1872f2010-08-17 16:25:41 -0700288 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700289 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
290 synchronized void nTypeGetNativeData(int id, int[] typeData) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800291 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700292 rsnTypeGetNativeData(mContext, id, typeData);
293 }
Jason Sams768bc022009-09-21 19:41:04 -0700294
Jason Sams857d0c72011-11-23 15:02:15 -0800295 native int rsnAllocationCreateTyped(int con, int type, int mip, int usage, int pointer);
296 synchronized int nAllocationCreateTyped(int type, int mip, int usage, int pointer) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800297 validate();
Jason Sams857d0c72011-11-23 15:02:15 -0800298 return rsnAllocationCreateTyped(mContext, type, mip, usage, pointer);
Jason Sams2e1872f2010-08-17 16:25:41 -0700299 }
Jason Sams5476b452010-12-08 16:14:36 -0800300 native int rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
301 synchronized int nAllocationCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800302 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800303 return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700304 }
Tim Murraya3145512012-12-04 17:59:29 -0800305
306 native int rsnAllocationCreateBitmapBackedAllocation(int con, int type, int mip, Bitmap bmp, int usage);
307 synchronized int nAllocationCreateBitmapBackedAllocation(int type, int mip, Bitmap bmp, int usage) {
308 validate();
309 return rsnAllocationCreateBitmapBackedAllocation(mContext, type, mip, bmp, usage);
310 }
311
312
Jason Sams5476b452010-12-08 16:14:36 -0800313 native int rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
314 synchronized int nAllocationCubeCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800315 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800316 return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800317 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700318 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
319 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800320 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700321 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
322 }
Jason Sams5476b452010-12-08 16:14:36 -0800323 native int rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage);
324 synchronized int nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800325 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800326 return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
327 }
328
Jason Sams4ef66502010-12-10 16:03:15 -0800329 native void rsnAllocationCopyToBitmap(int con, int alloc, Bitmap bmp);
330 synchronized void nAllocationCopyToBitmap(int alloc, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800331 validate();
Jason Sams4ef66502010-12-10 16:03:15 -0800332 rsnAllocationCopyToBitmap(mContext, alloc, bmp);
333 }
334
335
Jason Sams5476b452010-12-08 16:14:36 -0800336 native void rsnAllocationSyncAll(int con, int alloc, int src);
337 synchronized void nAllocationSyncAll(int alloc, int src) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800338 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800339 rsnAllocationSyncAll(mContext, alloc, src);
340 }
Jason Sams72226e02013-02-22 12:45:54 -0800341 native Surface rsnAllocationGetSurface(int con, int alloc);
342 synchronized Surface nAllocationGetSurface(int alloc) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800343 validate();
Jason Sams72226e02013-02-22 12:45:54 -0800344 return rsnAllocationGetSurface(mContext, alloc);
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700345 }
Jason Samsfb9aa9f2012-03-28 15:30:07 -0700346 native void rsnAllocationSetSurface(int con, int alloc, Surface sur);
347 synchronized void nAllocationSetSurface(int alloc, Surface sur) {
Jason Sams163766c2012-02-15 12:04:24 -0800348 validate();
Jason Samsfb9aa9f2012-03-28 15:30:07 -0700349 rsnAllocationSetSurface(mContext, alloc, sur);
Jason Sams163766c2012-02-15 12:04:24 -0800350 }
351 native void rsnAllocationIoSend(int con, int alloc);
352 synchronized void nAllocationIoSend(int alloc) {
353 validate();
354 rsnAllocationIoSend(mContext, alloc);
355 }
356 native void rsnAllocationIoReceive(int con, int alloc);
357 synchronized void nAllocationIoReceive(int alloc) {
358 validate();
359 rsnAllocationIoReceive(mContext, alloc);
360 }
361
Jason Sams615e7ce2012-01-13 14:01:20 -0800362
Jason Samsf7086092011-01-12 13:28:37 -0800363 native void rsnAllocationGenerateMipmaps(int con, int alloc);
364 synchronized void nAllocationGenerateMipmaps(int alloc) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800365 validate();
Jason Samsf7086092011-01-12 13:28:37 -0800366 rsnAllocationGenerateMipmaps(mContext, alloc);
367 }
Jason Sams4ef66502010-12-10 16:03:15 -0800368 native void rsnAllocationCopyFromBitmap(int con, int alloc, Bitmap bmp);
369 synchronized void nAllocationCopyFromBitmap(int alloc, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800370 validate();
Jason Sams4ef66502010-12-10 16:03:15 -0800371 rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
Jason Sams2e1872f2010-08-17 16:25:41 -0700372 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700373
Jason Sams49a05d72010-12-29 14:31:29 -0800374
375 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, int[] d, int sizeBytes);
376 synchronized void nAllocationData1D(int id, int off, int mip, int count, int[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800377 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800378 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700379 }
Jason Sams49a05d72010-12-29 14:31:29 -0800380 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, short[] d, int sizeBytes);
381 synchronized void nAllocationData1D(int id, int off, int mip, int count, short[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800382 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800383 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
384 }
385 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, byte[] d, int sizeBytes);
386 synchronized void nAllocationData1D(int id, int off, int mip, int count, byte[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800387 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800388 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
389 }
390 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, float[] d, int sizeBytes);
391 synchronized void nAllocationData1D(int id, int off, int mip, int count, float[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800392 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800393 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700394 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700395
Jason Sams49a05d72010-12-29 14:31:29 -0800396 native void rsnAllocationElementData1D(int con, int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes);
397 synchronized void nAllocationElementData1D(int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800398 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800399 rsnAllocationElementData1D(mContext, id, xoff, mip, compIdx, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700400 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700401
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700402 native void rsnAllocationData2D(int con,
403 int dstAlloc, int dstXoff, int dstYoff,
404 int dstMip, int dstFace,
405 int width, int height,
406 int srcAlloc, int srcXoff, int srcYoff,
407 int srcMip, int srcFace);
408 synchronized void nAllocationData2D(int dstAlloc, int dstXoff, int dstYoff,
409 int dstMip, int dstFace,
410 int width, int height,
411 int srcAlloc, int srcXoff, int srcYoff,
412 int srcMip, int srcFace) {
413 validate();
414 rsnAllocationData2D(mContext,
415 dstAlloc, dstXoff, dstYoff,
416 dstMip, dstFace,
417 width, height,
418 srcAlloc, srcXoff, srcYoff,
419 srcMip, srcFace);
420 }
421
Jason Samsfa445b92011-01-07 17:00:07 -0800422 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes);
423 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 -0800424 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800425 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
426 }
427 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes);
428 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 -0800429 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800430 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
431 }
Jason Sams49a05d72010-12-29 14:31:29 -0800432 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes);
433 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 -0800434 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800435 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700436 }
Jason Sams49a05d72010-12-29 14:31:29 -0800437 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes);
438 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 -0800439 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800440 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700441 }
Jason Samsfa445b92011-01-07 17:00:07 -0800442 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, Bitmap b);
443 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, Bitmap b) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800444 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800445 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, b);
446 }
Jason Sams49a05d72010-12-29 14:31:29 -0800447
Jason Samsb05d6892013-04-09 15:59:24 -0700448 native void rsnAllocationData3D(int con,
449 int dstAlloc, int dstXoff, int dstYoff, int dstZoff,
450 int dstMip,
451 int width, int height, int depth,
452 int srcAlloc, int srcXoff, int srcYoff, int srcZoff,
453 int srcMip);
454 synchronized void nAllocationData3D(int dstAlloc, int dstXoff, int dstYoff, int dstZoff,
455 int dstMip,
456 int width, int height, int depth,
457 int srcAlloc, int srcXoff, int srcYoff, int srcZoff,
458 int srcMip) {
459 validate();
460 rsnAllocationData3D(mContext,
461 dstAlloc, dstXoff, dstYoff, dstZoff,
462 dstMip, width, height, depth,
463 srcAlloc, srcXoff, srcYoff, srcZoff, srcMip);
464 }
465
466 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);
467 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, byte[] d, int sizeBytes) {
468 validate();
469 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
470 }
471 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);
472 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, short[] d, int sizeBytes) {
473 validate();
474 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
475 }
476 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);
477 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, int[] d, int sizeBytes) {
478 validate();
479 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
480 }
481 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);
482 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, float[] d, int sizeBytes) {
483 validate();
484 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
485 }
486
487
Jason Samsfa445b92011-01-07 17:00:07 -0800488 native void rsnAllocationRead(int con, int id, byte[] d);
489 synchronized void nAllocationRead(int id, byte[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800490 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800491 rsnAllocationRead(mContext, id, d);
492 }
493 native void rsnAllocationRead(int con, int id, short[] d);
494 synchronized void nAllocationRead(int id, short[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800495 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800496 rsnAllocationRead(mContext, id, d);
497 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700498 native void rsnAllocationRead(int con, int id, int[] d);
499 synchronized void nAllocationRead(int id, int[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800500 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700501 rsnAllocationRead(mContext, id, d);
502 }
503 native void rsnAllocationRead(int con, int id, float[] d);
504 synchronized void nAllocationRead(int id, float[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800505 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700506 rsnAllocationRead(mContext, id, d);
507 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700508 native int rsnAllocationGetType(int con, int id);
509 synchronized int nAllocationGetType(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800510 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700511 return rsnAllocationGetType(mContext, id);
512 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700513
Jason Sams5edc6082010-10-05 13:32:49 -0700514 native void rsnAllocationResize1D(int con, int id, int dimX);
515 synchronized void nAllocationResize1D(int id, int dimX) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800516 validate();
Jason Sams5edc6082010-10-05 13:32:49 -0700517 rsnAllocationResize1D(mContext, id, dimX);
518 }
Jason Sams5edc6082010-10-05 13:32:49 -0700519
Jason Sams2e1872f2010-08-17 16:25:41 -0700520 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
521 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800522 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700523 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
524 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800525 native int rsnFileA3DCreateFromFile(int con, String path);
526 synchronized int nFileA3DCreateFromFile(String path) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800527 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800528 return rsnFileA3DCreateFromFile(mContext, path);
529 }
530 native int rsnFileA3DCreateFromAsset(int con, AssetManager mgr, String path);
531 synchronized int nFileA3DCreateFromAsset(AssetManager mgr, String path) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800532 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800533 return rsnFileA3DCreateFromAsset(mContext, mgr, path);
534 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700535 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
536 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800537 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700538 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
539 }
540 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
541 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800542 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700543 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
544 }
545 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
546 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800547 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700548 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
549 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700550
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800551 native int rsnFontCreateFromFile(int con, String fileName, float size, int dpi);
552 synchronized int nFontCreateFromFile(String fileName, float size, int dpi) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800553 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700554 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
555 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800556 native int rsnFontCreateFromAssetStream(int con, String name, float size, int dpi, int assetStream);
557 synchronized int nFontCreateFromAssetStream(String name, float size, int dpi, int assetStream) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800558 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800559 return rsnFontCreateFromAssetStream(mContext, name, size, dpi, assetStream);
560 }
561 native int rsnFontCreateFromAsset(int con, AssetManager mgr, String path, float size, int dpi);
562 synchronized int nFontCreateFromAsset(AssetManager mgr, String path, float size, int dpi) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800563 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800564 return rsnFontCreateFromAsset(mContext, mgr, path, size, dpi);
565 }
Jason Sams22534172009-08-04 16:58:20 -0700566
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700567
Jason Sams2e1872f2010-08-17 16:25:41 -0700568 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
569 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800570 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700571 rsnScriptBindAllocation(mContext, script, alloc, slot);
572 }
573 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
574 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800575 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700576 rsnScriptSetTimeZone(mContext, script, timeZone);
577 }
578 native void rsnScriptInvoke(int con, int id, int slot);
579 synchronized void nScriptInvoke(int id, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800580 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700581 rsnScriptInvoke(mContext, id, slot);
582 }
Jason Sams6e494d32011-04-27 16:33:11 -0700583 native void rsnScriptForEach(int con, int id, int slot, int ain, int aout, byte[] params);
584 native void rsnScriptForEach(int con, int id, int slot, int ain, int aout);
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800585 native void rsnScriptForEachClipped(int con, int id, int slot, int ain, int aout, byte[] params,
586 int xstart, int xend, int ystart, int yend, int zstart, int zend);
Stephen Hinesdac6ed02013-02-13 00:09:02 -0800587 native void rsnScriptForEachClipped(int con, int id, int slot, int ain, int aout,
588 int xstart, int xend, int ystart, int yend, int zstart, int zend);
Jason Sams6e494d32011-04-27 16:33:11 -0700589 synchronized void nScriptForEach(int id, int slot, int ain, int aout, byte[] params) {
590 validate();
591 if (params == null) {
592 rsnScriptForEach(mContext, id, slot, ain, aout);
593 } else {
594 rsnScriptForEach(mContext, id, slot, ain, aout, params);
595 }
596 }
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800597
598 synchronized void nScriptForEachClipped(int id, int slot, int ain, int aout, byte[] params,
599 int xstart, int xend, int ystart, int yend, int zstart, int zend) {
600 validate();
Stephen Hinesdac6ed02013-02-13 00:09:02 -0800601 if (params == null) {
602 rsnScriptForEachClipped(mContext, id, slot, ain, aout, xstart, xend, ystart, yend, zstart, zend);
603 } else {
604 rsnScriptForEachClipped(mContext, id, slot, ain, aout, params, xstart, xend, ystart, yend, zstart, zend);
605 }
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800606 }
607
Jason Sams2e1872f2010-08-17 16:25:41 -0700608 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
609 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800610 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700611 rsnScriptInvokeV(mContext, id, slot, params);
612 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700613
Jason Sams2e1872f2010-08-17 16:25:41 -0700614 native void rsnScriptSetVarI(int con, int id, int slot, int val);
615 synchronized void nScriptSetVarI(int id, int slot, int val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800616 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700617 rsnScriptSetVarI(mContext, id, slot, val);
618 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700619 native int rsnScriptGetVarI(int con, int id, int slot);
620 synchronized int nScriptGetVarI(int id, int slot) {
621 validate();
622 return rsnScriptGetVarI(mContext, id, slot);
623 }
624
Stephen Hines031ec58c2010-10-11 10:54:21 -0700625 native void rsnScriptSetVarJ(int con, int id, int slot, long val);
626 synchronized void nScriptSetVarJ(int id, int slot, long val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800627 validate();
Stephen Hines031ec58c2010-10-11 10:54:21 -0700628 rsnScriptSetVarJ(mContext, id, slot, val);
629 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700630 native long rsnScriptGetVarJ(int con, int id, int slot);
631 synchronized long nScriptGetVarJ(int id, int slot) {
632 validate();
633 return rsnScriptGetVarJ(mContext, id, slot);
634 }
635
Jason Sams2e1872f2010-08-17 16:25:41 -0700636 native void rsnScriptSetVarF(int con, int id, int slot, float val);
637 synchronized void nScriptSetVarF(int id, int slot, float val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800638 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700639 rsnScriptSetVarF(mContext, id, slot, val);
640 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700641 native float rsnScriptGetVarF(int con, int id, int slot);
642 synchronized float nScriptGetVarF(int id, int slot) {
643 validate();
644 return rsnScriptGetVarF(mContext, id, slot);
645 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700646 native void rsnScriptSetVarD(int con, int id, int slot, double val);
647 synchronized void nScriptSetVarD(int id, int slot, double val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800648 validate();
Stephen Hinesca54ec32010-09-20 17:20:30 -0700649 rsnScriptSetVarD(mContext, id, slot, val);
650 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700651 native double rsnScriptGetVarD(int con, int id, int slot);
652 synchronized double nScriptGetVarD(int id, int slot) {
653 validate();
654 return rsnScriptGetVarD(mContext, id, slot);
655 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700656 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
657 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800658 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700659 rsnScriptSetVarV(mContext, id, slot, val);
660 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700661 native void rsnScriptGetVarV(int con, int id, int slot, byte[] val);
662 synchronized void nScriptGetVarV(int id, int slot, byte[] val) {
663 validate();
664 rsnScriptGetVarV(mContext, id, slot, val);
665 }
Stephen Hinesadeb8092012-04-20 14:26:06 -0700666 native void rsnScriptSetVarVE(int con, int id, int slot, byte[] val,
667 int e, int[] dims);
668 synchronized void nScriptSetVarVE(int id, int slot, byte[] val,
669 int e, int[] dims) {
670 validate();
671 rsnScriptSetVarVE(mContext, id, slot, val, e, dims);
672 }
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800673 native void rsnScriptSetVarObj(int con, int id, int slot, int val);
674 synchronized void nScriptSetVarObj(int id, int slot, int val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800675 validate();
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800676 rsnScriptSetVarObj(mContext, id, slot, val);
677 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700678
Jason Samse4a06c52011-03-16 16:29:28 -0700679 native int rsnScriptCCreate(int con, String resName, String cacheDir,
680 byte[] script, int length);
681 synchronized int nScriptCCreate(String resName, String cacheDir, byte[] script, int length) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800682 validate();
Jason Samse4a06c52011-03-16 16:29:28 -0700683 return rsnScriptCCreate(mContext, resName, cacheDir, script, length);
Jason Sams2e1872f2010-08-17 16:25:41 -0700684 }
Jason Samsebfb4362009-09-23 13:57:02 -0700685
Jason Sams6ab97682012-08-10 12:09:43 -0700686 native int rsnScriptIntrinsicCreate(int con, int id, int eid);
687 synchronized int nScriptIntrinsicCreate(int id, int eid) {
688 validate();
689 return rsnScriptIntrinsicCreate(mContext, id, eid);
690 }
691
Jason Sams08a81582012-09-18 12:32:10 -0700692 native int rsnScriptKernelIDCreate(int con, int sid, int slot, int sig);
693 synchronized int nScriptKernelIDCreate(int sid, int slot, int sig) {
694 validate();
695 return rsnScriptKernelIDCreate(mContext, sid, slot, sig);
696 }
697
698 native int rsnScriptFieldIDCreate(int con, int sid, int slot);
699 synchronized int nScriptFieldIDCreate(int sid, int slot) {
700 validate();
701 return rsnScriptFieldIDCreate(mContext, sid, slot);
702 }
703
704 native int rsnScriptGroupCreate(int con, int[] kernels, int[] src, int[] dstk, int[] dstf, int[] types);
705 synchronized int nScriptGroupCreate(int[] kernels, int[] src, int[] dstk, int[] dstf, int[] types) {
706 validate();
707 return rsnScriptGroupCreate(mContext, kernels, src, dstk, dstf, types);
708 }
709
710 native void rsnScriptGroupSetInput(int con, int group, int kernel, int alloc);
711 synchronized void nScriptGroupSetInput(int group, int kernel, int alloc) {
712 validate();
713 rsnScriptGroupSetInput(mContext, group, kernel, alloc);
714 }
715
716 native void rsnScriptGroupSetOutput(int con, int group, int kernel, int alloc);
717 synchronized void nScriptGroupSetOutput(int group, int kernel, int alloc) {
718 validate();
719 rsnScriptGroupSetOutput(mContext, group, kernel, alloc);
720 }
721
722 native void rsnScriptGroupExecute(int con, int group);
723 synchronized void nScriptGroupExecute(int group) {
724 validate();
725 rsnScriptGroupExecute(mContext, group);
726 }
727
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700728 native int rsnSamplerCreate(int con, int magFilter, int minFilter,
729 int wrapS, int wrapT, int wrapR, float aniso);
730 synchronized int nSamplerCreate(int magFilter, int minFilter,
731 int wrapS, int wrapT, int wrapR, float aniso) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800732 validate();
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700733 return rsnSamplerCreate(mContext, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
Jason Sams2e1872f2010-08-17 16:25:41 -0700734 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800735
Jason Sams331bf9b2011-04-06 11:23:54 -0700736 native int rsnProgramStoreCreate(int con, boolean r, boolean g, boolean b, boolean a,
737 boolean depthMask, boolean dither,
738 int srcMode, int dstMode, int depthFunc);
739 synchronized int nProgramStoreCreate(boolean r, boolean g, boolean b, boolean a,
740 boolean depthMask, boolean dither,
741 int srcMode, int dstMode, int depthFunc) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800742 validate();
Jason Samsbd184c52011-04-06 11:44:47 -0700743 return rsnProgramStoreCreate(mContext, r, g, b, a, depthMask, dither, srcMode,
744 dstMode, depthFunc);
Jason Sams2e1872f2010-08-17 16:25:41 -0700745 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700746
Jason Sams94aaed32011-09-23 14:18:53 -0700747 native int rsnProgramRasterCreate(int con, boolean pointSprite, int cullMode);
748 synchronized int nProgramRasterCreate(boolean pointSprite, int cullMode) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800749 validate();
Jason Sams94aaed32011-09-23 14:18:53 -0700750 return rsnProgramRasterCreate(mContext, pointSprite, cullMode);
Jason Sams2e1872f2010-08-17 16:25:41 -0700751 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700752
Jason Sams2e1872f2010-08-17 16:25:41 -0700753 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
754 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800755 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700756 rsnProgramBindConstants(mContext, pv, slot, mID);
757 }
758 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
759 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800760 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700761 rsnProgramBindTexture(mContext, vpf, slot, a);
762 }
763 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
764 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800765 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700766 rsnProgramBindSampler(mContext, vpf, slot, s);
767 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800768 native int rsnProgramFragmentCreate(int con, String shader, String[] texNames, int[] params);
769 synchronized int nProgramFragmentCreate(String shader, String[] texNames, int[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800770 validate();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800771 return rsnProgramFragmentCreate(mContext, shader, texNames, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700772 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800773 native int rsnProgramVertexCreate(int con, String shader, String[] texNames, int[] params);
774 synchronized int nProgramVertexCreate(String shader, String[] texNames, int[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800775 validate();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800776 return rsnProgramVertexCreate(mContext, shader, texNames, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700777 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700778
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700779 native int rsnMeshCreate(int con, int[] vtx, int[] idx, int[] prim);
780 synchronized int nMeshCreate(int[] vtx, int[] idx, int[] prim) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800781 validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700782 return rsnMeshCreate(mContext, vtx, idx, prim);
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800783 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700784 native int rsnMeshGetVertexBufferCount(int con, int id);
785 synchronized int nMeshGetVertexBufferCount(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800786 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700787 return rsnMeshGetVertexBufferCount(mContext, id);
788 }
789 native int rsnMeshGetIndexCount(int con, int id);
790 synchronized int nMeshGetIndexCount(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800791 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700792 return rsnMeshGetIndexCount(mContext, id);
793 }
794 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
795 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800796 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700797 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
798 }
799 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
800 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800801 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700802 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
803 }
804
Jason Samsf15ed012011-10-31 13:23:43 -0700805 native int rsnPathCreate(int con, int prim, boolean isStatic, int vtx, int loop, float q);
806 synchronized int nPathCreate(int prim, boolean isStatic, int vtx, int loop, float q) {
807 validate();
808 return rsnPathCreate(mContext, prim, isStatic, vtx, loop, q);
809 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700810
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800811 int mDev;
812 int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700813 @SuppressWarnings({"FieldCanBeLocal"})
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800814 MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700815
Jason Sams8cb39de2010-06-01 15:47:01 -0700816 Element mElement_U8;
817 Element mElement_I8;
818 Element mElement_U16;
819 Element mElement_I16;
820 Element mElement_U32;
821 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -0700822 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -0700823 Element mElement_I64;
Jason Sams8cb39de2010-06-01 15:47:01 -0700824 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700825 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700826 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700827
Jason Sams8cb39de2010-06-01 15:47:01 -0700828 Element mElement_ELEMENT;
829 Element mElement_TYPE;
830 Element mElement_ALLOCATION;
831 Element mElement_SAMPLER;
832 Element mElement_SCRIPT;
833 Element mElement_MESH;
834 Element mElement_PROGRAM_FRAGMENT;
835 Element mElement_PROGRAM_VERTEX;
836 Element mElement_PROGRAM_RASTER;
837 Element mElement_PROGRAM_STORE;
Stephen Hines3a291412012-04-11 17:27:29 -0700838 Element mElement_FONT;
Jason Samsa70f4162010-03-26 15:33:42 -0700839
Jason Sams3c0dfba2009-09-27 17:50:38 -0700840 Element mElement_A_8;
841 Element mElement_RGB_565;
842 Element mElement_RGB_888;
843 Element mElement_RGBA_5551;
844 Element mElement_RGBA_4444;
845 Element mElement_RGBA_8888;
846
Jason Sams8cb39de2010-06-01 15:47:01 -0700847 Element mElement_FLOAT_2;
848 Element mElement_FLOAT_3;
849 Element mElement_FLOAT_4;
Stephen Hines836c4a52011-06-01 14:38:10 -0700850
851 Element mElement_DOUBLE_2;
852 Element mElement_DOUBLE_3;
853 Element mElement_DOUBLE_4;
854
855 Element mElement_UCHAR_2;
856 Element mElement_UCHAR_3;
Jason Sams8cb39de2010-06-01 15:47:01 -0700857 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800858
Stephen Hines836c4a52011-06-01 14:38:10 -0700859 Element mElement_CHAR_2;
860 Element mElement_CHAR_3;
861 Element mElement_CHAR_4;
862
863 Element mElement_USHORT_2;
864 Element mElement_USHORT_3;
865 Element mElement_USHORT_4;
866
867 Element mElement_SHORT_2;
868 Element mElement_SHORT_3;
869 Element mElement_SHORT_4;
870
871 Element mElement_UINT_2;
872 Element mElement_UINT_3;
873 Element mElement_UINT_4;
874
875 Element mElement_INT_2;
876 Element mElement_INT_3;
877 Element mElement_INT_4;
878
879 Element mElement_ULONG_2;
880 Element mElement_ULONG_3;
881 Element mElement_ULONG_4;
882
883 Element mElement_LONG_2;
884 Element mElement_LONG_3;
885 Element mElement_LONG_4;
886
Jason Sams1d45c472010-08-25 14:31:48 -0700887 Element mElement_MATRIX_4X4;
888 Element mElement_MATRIX_3X3;
889 Element mElement_MATRIX_2X2;
890
Jason Sams4d339932010-05-11 14:03:58 -0700891 Sampler mSampler_CLAMP_NEAREST;
892 Sampler mSampler_CLAMP_LINEAR;
893 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
894 Sampler mSampler_WRAP_NEAREST;
895 Sampler mSampler_WRAP_LINEAR;
896 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800897 Sampler mSampler_MIRRORED_REPEAT_NEAREST;
898 Sampler mSampler_MIRRORED_REPEAT_LINEAR;
899 Sampler mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
Jason Sams4d339932010-05-11 14:03:58 -0700900
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700901 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
902 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700903 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
904 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700905
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700906 ProgramRaster mProgramRaster_CULL_BACK;
907 ProgramRaster mProgramRaster_CULL_FRONT;
908 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700909
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700910 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700911 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700912
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700913 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700914 * The base class from which an application should derive in order
915 * to receive RS messages from scripts. When a script calls {@code
916 * rsSendToClient}, the data fields will be filled, and the run
917 * method will be called on a separate thread. This will occur
918 * some time after {@code rsSendToClient} completes in the script,
919 * as {@code rsSendToClient} is asynchronous. Message handlers are
920 * not guaranteed to have completed when {@link
921 * android.renderscript.RenderScript#finish} returns.
Jason Sams27676fe2010-11-10 17:00:59 -0800922 *
923 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800924 public static class RSMessageHandler implements Runnable {
Jason Sams516c3192009-10-06 13:58:47 -0700925 protected int[] mData;
926 protected int mID;
Jason Sams1c415172010-11-08 17:06:46 -0800927 protected int mLength;
Jason Sams516c3192009-10-06 13:58:47 -0700928 public void run() {
929 }
930 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700931 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700932 * If an application is expecting messages, it should set this
933 * field to an instance of {@link RSMessageHandler}. This
934 * instance will receive all the user messages sent from {@code
935 * sendToClient} by scripts from this context.
Jason Sams27676fe2010-11-10 17:00:59 -0800936 *
937 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800938 RSMessageHandler mMessageCallback = null;
939
940 public void setMessageHandler(RSMessageHandler msg) {
941 mMessageCallback = msg;
942 }
943 public RSMessageHandler getMessageHandler() {
944 return mMessageCallback;
945 }
Jason Sams516c3192009-10-06 13:58:47 -0700946
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700947 /**
Jason Sams02d56d92013-04-12 16:40:50 -0700948 * Place a message into the message queue to be sent back to the message
949 * handler once all previous commands have been executed.
Jason Sams455d6442013-02-05 19:20:18 -0800950 *
951 * @param id
952 * @param data
953 */
954 public void sendMessage(int id, int[] data) {
955 nContextSendMessage(id, data);
956 }
957
958 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700959 * The runtime error handler base class. An application should derive from this class
960 * if it wishes to install an error handler. When errors occur at runtime,
961 * the fields in this class will be filled, and the run method will be called.
Jason Sams27676fe2010-11-10 17:00:59 -0800962 *
963 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800964 public static class RSErrorHandler implements Runnable {
Jason Sams1c415172010-11-08 17:06:46 -0800965 protected String mErrorMessage;
966 protected int mErrorNum;
967 public void run() {
968 }
969 }
Jason Sams27676fe2010-11-10 17:00:59 -0800970
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700971 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800972 * Application Error handler. All runtime errors will be dispatched to the
973 * instance of RSAsyncError set here. If this field is null a
Tim Murrayc11e25c2013-04-09 11:01:01 -0700974 * {@link RSRuntimeException} will instead be thrown with details about the error.
Jason Sams27676fe2010-11-10 17:00:59 -0800975 * This will cause program termaination.
976 *
977 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800978 RSErrorHandler mErrorCallback = null;
979
980 public void setErrorHandler(RSErrorHandler msg) {
981 mErrorCallback = msg;
982 }
983 public RSErrorHandler getErrorHandler() {
984 return mErrorCallback;
985 }
Jason Sams1c415172010-11-08 17:06:46 -0800986
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700987 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700988 * RenderScript worker thread priority enumeration. The default value is
989 * NORMAL. Applications wishing to do background processing should set
990 * their priority to LOW to avoid starving forground processes.
Jason Sams27676fe2010-11-10 17:00:59 -0800991 */
Jason Sams7d787b42009-11-15 12:14:26 -0800992 public enum Priority {
Glenn Kasten260c77a2011-06-01 17:25:54 -0700993 LOW (Process.THREAD_PRIORITY_BACKGROUND + (5 * Process.THREAD_PRIORITY_LESS_FAVORABLE)),
994 NORMAL (Process.THREAD_PRIORITY_DISPLAY);
Jason Sams7d787b42009-11-15 12:14:26 -0800995
996 int mID;
997 Priority(int id) {
998 mID = id;
999 }
1000 }
1001
Jason Sams771bebb2009-12-07 12:40:12 -08001002 void validate() {
1003 if (mContext == 0) {
Jason Samsc1d62102010-11-04 14:32:19 -07001004 throw new RSInvalidStateException("Calling RS with no Context active.");
Jason Sams771bebb2009-12-07 12:40:12 -08001005 }
1006 }
1007
Jason Sams27676fe2010-11-10 17:00:59 -08001008
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001009 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001010 * Change the priority of the worker threads for this context.
1011 *
1012 * @param p New priority to be set.
1013 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001014 public void setPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -08001015 validate();
Jason Sams7d787b42009-11-15 12:14:26 -08001016 nContextSetPriority(p.mID);
1017 }
1018
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001019 static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -07001020 RenderScript mRS;
1021 boolean mRun = true;
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001022 int[] mAuxData = new int[2];
Jason Sams1c415172010-11-08 17:06:46 -08001023
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001024 static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
1025 static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
1026 static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
1027 static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
1028 static final int RS_MESSAGE_TO_CLIENT_USER = 4;
Jason Sams739c8262013-04-11 18:07:52 -07001029 static final int RS_MESSAGE_TO_CLIENT_NEW_BUFFER = 5;
Jason Sams516c3192009-10-06 13:58:47 -07001030
Stephen Hines42028a82013-04-17 19:22:01 -07001031 static final int RS_ERROR_FATAL_DEBUG = 0x0800;
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001032 static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
Jason Samsadd9d962010-11-22 16:20:16 -08001033
Jason Sams516c3192009-10-06 13:58:47 -07001034 MessageThread(RenderScript rs) {
1035 super("RSMessageThread");
1036 mRS = rs;
1037
1038 }
1039
1040 public void run() {
1041 // This function is a temporary solution. The final solution will
1042 // used typed allocations where the message id is the type indicator.
1043 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -07001044 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -07001045 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -07001046 rbuf[0] = 0;
Jason Samsedbfabd2011-05-17 15:01:29 -07001047 int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData);
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001048 int size = mAuxData[1];
1049 int subID = mAuxData[0];
Jason Sams1c415172010-11-08 17:06:46 -08001050
1051 if (msg == RS_MESSAGE_TO_CLIENT_USER) {
1052 if ((size>>2) >= rbuf.length) {
1053 rbuf = new int[(size + 3) >> 2];
1054 }
Jason Samsedbfabd2011-05-17 15:01:29 -07001055 if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) !=
1056 RS_MESSAGE_TO_CLIENT_USER) {
Tim Murrayc11e25c2013-04-09 11:01:01 -07001057 throw new RSDriverException("Error processing message from RenderScript.");
Jason Samsedbfabd2011-05-17 15:01:29 -07001058 }
Jason Sams1c415172010-11-08 17:06:46 -08001059
1060 if(mRS.mMessageCallback != null) {
1061 mRS.mMessageCallback.mData = rbuf;
1062 mRS.mMessageCallback.mID = subID;
1063 mRS.mMessageCallback.mLength = size;
1064 mRS.mMessageCallback.run();
Jason Sams1d45c472010-08-25 14:31:48 -07001065 } else {
Jason Sams1c415172010-11-08 17:06:46 -08001066 throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
Jason Sams516c3192009-10-06 13:58:47 -07001067 }
Stephen Hinesab98bb62010-09-24 14:38:30 -07001068 continue;
Jason Sams516c3192009-10-06 13:58:47 -07001069 }
Jason Sams1c415172010-11-08 17:06:46 -08001070
1071 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
1072 String e = mRS.nContextGetErrorMessage(mRS.mContext);
1073
Stephen Hines42028a82013-04-17 19:22:01 -07001074 // Throw RSRuntimeException under the following conditions:
1075 //
1076 // 1) It is an unknown fatal error.
1077 // 2) It is a debug fatal error, and we are not in a
1078 // debug context.
1079 // 3) It is a debug fatal error, and we do not have an
1080 // error callback.
1081 if (subID >= RS_ERROR_FATAL_UNKNOWN ||
1082 (subID >= RS_ERROR_FATAL_DEBUG &&
1083 (mRS.mContextType != ContextType.DEBUG ||
1084 mRS.mErrorCallback == null))) {
Jason Samsadd9d962010-11-22 16:20:16 -08001085 throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
1086 }
1087
Jason Sams1c415172010-11-08 17:06:46 -08001088 if(mRS.mErrorCallback != null) {
1089 mRS.mErrorCallback.mErrorMessage = e;
1090 mRS.mErrorCallback.mErrorNum = subID;
1091 mRS.mErrorCallback.run();
1092 } else {
Jason Samsa4b7bc92013-02-05 15:05:39 -08001093 android.util.Log.e(LOG_TAG, "non fatal RS error, " + e);
Stephen Hinesbe74bdd2012-02-03 15:29:36 -08001094 // Do not throw here. In these cases, we do not have
1095 // a fatal error.
Jason Sams1c415172010-11-08 17:06:46 -08001096 }
1097 continue;
1098 }
1099
Jason Sams739c8262013-04-11 18:07:52 -07001100 if (msg == RS_MESSAGE_TO_CLIENT_NEW_BUFFER) {
1101 Allocation.sendBufferNotification(subID);
1102 continue;
1103 }
1104
Jason Sams1c415172010-11-08 17:06:46 -08001105 // 2: teardown.
1106 // But we want to avoid starving other threads during
1107 // teardown by yielding until the next line in the destructor
1108 // can execute to set mRun = false
1109 try {
1110 sleep(1, 0);
1111 } catch(InterruptedException e) {
Jason Sams516c3192009-10-06 13:58:47 -07001112 }
Jason Sams516c3192009-10-06 13:58:47 -07001113 }
Tim Murrayda67deb2013-05-09 12:02:50 -07001114 //Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -07001115 }
1116 }
1117
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001118 RenderScript(Context ctx) {
Stephen Hines42028a82013-04-17 19:22:01 -07001119 mContextType = ContextType.NORMAL;
Jason Sams1a4e1f32012-02-24 17:51:24 -08001120 if (ctx != null) {
1121 mApplicationContext = ctx.getApplicationContext();
1122 }
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001123 }
1124
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001125 /**
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001126 * Gets the application context associated with the RenderScript context.
1127 *
1128 * @return The application context.
1129 */
1130 public final Context getApplicationContext() {
1131 return mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001132 }
1133
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001134 /**
Jason Samsadd26dc2013-02-22 18:43:45 -08001135 * @hide
1136 */
1137 public static RenderScript create(Context ctx, int sdkVersion) {
1138 return create(ctx, sdkVersion, ContextType.NORMAL);
1139 }
1140
1141 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001142 * Create a RenderScript context.
Jason Sams27676fe2010-11-10 17:00:59 -08001143 *
Jason Sams1a4e1f32012-02-24 17:51:24 -08001144 * @hide
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001145 * @param ctx The context.
Jason Sams27676fe2010-11-10 17:00:59 -08001146 * @return RenderScript
1147 */
Jason Samsadd26dc2013-02-22 18:43:45 -08001148 public static RenderScript create(Context ctx, int sdkVersion, ContextType ct) {
Dan Morrille4d9a012013-03-28 18:10:43 -07001149 if (!sInitialized) {
1150 Log.e(LOG_TAG, "RenderScript.create() called when disabled; someone is likely to crash");
1151 return null;
1152 }
1153
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001154 RenderScript rs = new RenderScript(ctx);
Jason Sams704ff642010-02-09 16:05:07 -08001155
1156 rs.mDev = rs.nDeviceCreate();
Jason Samsadd26dc2013-02-22 18:43:45 -08001157 rs.mContext = rs.nContextCreate(rs.mDev, 0, sdkVersion, ct.mID);
Stephen Hines42028a82013-04-17 19:22:01 -07001158 rs.mContextType = ct;
Jason Sams26985362011-05-03 15:01:58 -07001159 if (rs.mContext == 0) {
1160 throw new RSDriverException("Failed to create RS context.");
1161 }
Jason Sams704ff642010-02-09 16:05:07 -08001162 rs.mMessageThread = new MessageThread(rs);
1163 rs.mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -08001164 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -08001165 }
1166
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001167 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001168 * Create a RenderScript context.
Jason Sams1a4e1f32012-02-24 17:51:24 -08001169 *
1170 * @param ctx The context.
1171 * @return RenderScript
1172 */
1173 public static RenderScript create(Context ctx) {
Jason Samsadd26dc2013-02-22 18:43:45 -08001174 return create(ctx, ContextType.NORMAL);
1175 }
1176
1177 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001178 * Create a RenderScript context.
Jason Samsadd26dc2013-02-22 18:43:45 -08001179 *
Jason Samsadd26dc2013-02-22 18:43:45 -08001180 *
1181 * @param ctx The context.
Jason Sams02d56d92013-04-12 16:40:50 -07001182 * @param ct The type of context to be created.
Jason Samsadd26dc2013-02-22 18:43:45 -08001183 * @return RenderScript
1184 */
1185 public static RenderScript create(Context ctx, ContextType ct) {
Jason Sams1a4e1f32012-02-24 17:51:24 -08001186 int v = ctx.getApplicationInfo().targetSdkVersion;
Jason Samsadd26dc2013-02-22 18:43:45 -08001187 return create(ctx, v, ct);
Jason Sams1a4e1f32012-02-24 17:51:24 -08001188 }
1189
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001190 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001191 * Print the currently available debugging information about the state of
1192 * the RS context to the log.
1193 *
Jason Sams27676fe2010-11-10 17:00:59 -08001194 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001195 public void contextDump() {
Jason Sams5dbfe932010-01-27 14:41:43 -08001196 validate();
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001197 nContextDump(0);
Jason Sams715333b2009-11-17 17:26:46 -08001198 }
1199
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001200 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001201 * Wait for any pending asynchronous opeations (such as copies to a RS
1202 * allocation or RS script executions) to complete.
Jason Sams27676fe2010-11-10 17:00:59 -08001203 *
1204 */
Jason Sams96ed4cf2010-06-15 12:15:57 -07001205 public void finish() {
1206 nContextFinish();
1207 }
1208
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001209 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001210 * Destroys this RenderScript context. Once this function is called,
1211 * using this context or any objects belonging to this context is
1212 * illegal.
Jason Sams27676fe2010-11-10 17:00:59 -08001213 *
1214 */
Jason Samsf5b45962009-08-25 14:49:07 -07001215 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -08001216 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -07001217 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -07001218 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -07001219 try {
1220 mMessageThread.join();
1221 } catch(InterruptedException e) {
1222 }
Jason Sams516c3192009-10-06 13:58:47 -07001223
Jason Sams2e1872f2010-08-17 16:25:41 -07001224 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -07001225 mContext = 0;
1226
1227 nDeviceDestroy(mDev);
1228 mDev = 0;
1229 }
Jason Sams02fb2cb2009-05-28 15:37:57 -07001230
Jason Samsa9e7a052009-09-25 14:51:22 -07001231 boolean isAlive() {
1232 return mContext != 0;
1233 }
1234
Jason Samsbf6ef8d2010-12-06 15:59:59 -08001235 int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -07001236 if(o != null) {
Jason Samse07694b2012-04-03 15:36:36 -07001237 return o.getID(this);
Jason Samsd8e41612009-08-20 17:22:40 -07001238 }
Jason Sams6b9dec02009-09-23 16:38:37 -07001239 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001240 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001241}