blob: 20949a4ed587d0aca2e7d8a0504ccc7004f50878 [file] [log] [blame]
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jason Sams94d8e90a2009-06-10 16:09:05 -070017package android.renderscript;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070018
Jason Sams43ee06852009-08-12 17:54:11 -070019import java.lang.reflect.Field;
Jason Sams36e612a2009-07-31 16:26:13 -070020
Jason Samsb8c5a842009-07-31 20:40:47 -070021import android.graphics.Bitmap;
Romain Guy650a3eb2009-08-31 14:06:43 -070022import android.graphics.BitmapFactory;
Jason Sams36e612a2009-07-31 16:26:13 -070023import android.util.Config;
24import android.util.Log;
25import android.view.Surface;
Jack Palevich43702d82009-05-28 13:38:16 -070026
Jack Palevich60aa3ea2009-05-26 13:45:08 -070027
Jason Samse29d4712009-07-23 15:19:03 -070028/**
29 * @hide
30 *
Jason Sams27676fe2010-11-10 17:00:59 -080031 * RenderScript base master class. An instance of this class creates native
32 * worker threads for processing commands from this object. This base class
33 * does not provide any extended capabilities beyond simple data processing.
34 * For extended capabilities use derived classes such as RenderScriptGL.
35 *
36 *
37 *
Jason Samse29d4712009-07-23 15:19:03 -070038 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070039public class RenderScript {
Jason Sams3bc47d42009-11-12 15:10:25 -080040 static final String LOG_TAG = "RenderScript_jni";
Jason Sams704ff642010-02-09 16:05:07 -080041 protected static final boolean DEBUG = false;
Romain Guy650a3eb2009-08-31 14:06:43 -070042 @SuppressWarnings({"UnusedDeclaration", "deprecation"})
Jason Sams704ff642010-02-09 16:05:07 -080043 protected static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070044
45
46
Jason Sams02fb2cb2009-05-28 15:37:57 -070047 /*
Jack Palevich60aa3ea2009-05-26 13:45:08 -070048 * We use a class initializer to allow the native code to cache some
49 * field offsets.
50 */
Romain Guy650a3eb2009-08-31 14:06:43 -070051 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Jason Sams704ff642010-02-09 16:05:07 -080052 protected static boolean sInitialized;
53 native protected static void _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070054
Jason Samsdba3ba52009-07-30 14:56:12 -070055
Jack Palevich60aa3ea2009-05-26 13:45:08 -070056 static {
57 sInitialized = false;
58 try {
Jason Samse29d4712009-07-23 15:19:03 -070059 System.loadLibrary("rs_jni");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070060 _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070061 sInitialized = true;
62 } catch (UnsatisfiedLinkError e) {
63 Log.d(LOG_TAG, "RenderScript JNI library not found!");
64 }
65 }
66
Jason Sams2e1872f2010-08-17 16:25:41 -070067 // Non-threadsafe functions.
Jason Samsea84a7c2009-09-04 14:42:41 -070068 native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565);
Jason Sams36e612a2009-07-31 16:26:13 -070069 native int nDeviceCreate();
70 native void nDeviceDestroy(int dev);
Jason Samsebfb4362009-09-23 13:57:02 -070071 native void nDeviceSetConfig(int dev, int param, int value);
Jason Sams1c415172010-11-08 17:06:46 -080072 native void nContextGetUserMessage(int con, int[] data);
73 native String nContextGetErrorMessage(int con);
74 native int nContextPeekMessage(int con, int[] subID, boolean wait);
Jason Sams2e1872f2010-08-17 16:25:41 -070075 native void nContextInitToClient(int con);
76 native void nContextDeinitToClient(int con);
Jason Sams3eaa3382009-06-10 15:04:38 -070077
Jason Sams718cd1f2009-12-23 14:35:29 -080078
Jason Sams2e1872f2010-08-17 16:25:41 -070079 // Methods below are wrapped to protect the non-threadsafe
80 // lockless fifo.
Jason Sams11c8af92010-10-13 15:31:10 -070081 native int rsnContextCreateGL(int dev, int ver,
82 int colorMin, int colorPref,
83 int alphaMin, int alphaPref,
84 int depthMin, int depthPref,
85 int stencilMin, int stencilPref,
86 int samplesMin, int samplesPref, float samplesQ);
87 synchronized int nContextCreateGL(int dev, int ver,
88 int colorMin, int colorPref,
89 int alphaMin, int alphaPref,
90 int depthMin, int depthPref,
91 int stencilMin, int stencilPref,
92 int samplesMin, int samplesPref, float samplesQ) {
93 return rsnContextCreateGL(dev, ver, colorMin, colorPref,
94 alphaMin, alphaPref, depthMin, depthPref,
95 stencilMin, stencilPref,
96 samplesMin, samplesPref, samplesQ);
Jason Sams2e1872f2010-08-17 16:25:41 -070097 }
98 native int rsnContextCreate(int dev, int ver);
99 synchronized int nContextCreate(int dev, int ver) {
100 return rsnContextCreate(dev, ver);
101 }
102 native void rsnContextDestroy(int con);
103 synchronized void nContextDestroy() {
104 rsnContextDestroy(mContext);
105 }
106 native void rsnContextSetSurface(int con, int w, int h, Surface sur);
107 synchronized void nContextSetSurface(int w, int h, Surface sur) {
108 rsnContextSetSurface(mContext, w, h, sur);
109 }
110 native void rsnContextSetPriority(int con, int p);
111 synchronized void nContextSetPriority(int p) {
112 rsnContextSetPriority(mContext, p);
113 }
114 native void rsnContextDump(int con, int bits);
115 synchronized void nContextDump(int bits) {
116 rsnContextDump(mContext, bits);
117 }
118 native void rsnContextFinish(int con);
119 synchronized void nContextFinish() {
120 rsnContextFinish(mContext);
121 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700122
Jason Sams2e1872f2010-08-17 16:25:41 -0700123 native void rsnContextBindRootScript(int con, int script);
124 synchronized void nContextBindRootScript(int script) {
125 rsnContextBindRootScript(mContext, script);
126 }
127 native void rsnContextBindSampler(int con, int sampler, int slot);
128 synchronized void nContextBindSampler(int sampler, int slot) {
129 rsnContextBindSampler(mContext, sampler, slot);
130 }
131 native void rsnContextBindProgramStore(int con, int pfs);
132 synchronized void nContextBindProgramStore(int pfs) {
133 rsnContextBindProgramStore(mContext, pfs);
134 }
135 native void rsnContextBindProgramFragment(int con, int pf);
136 synchronized void nContextBindProgramFragment(int pf) {
137 rsnContextBindProgramFragment(mContext, pf);
138 }
139 native void rsnContextBindProgramVertex(int con, int pv);
140 synchronized void nContextBindProgramVertex(int pv) {
141 rsnContextBindProgramVertex(mContext, pv);
142 }
143 native void rsnContextBindProgramRaster(int con, int pr);
144 synchronized void nContextBindProgramRaster(int pr) {
145 rsnContextBindProgramRaster(mContext, pr);
146 }
147 native void rsnContextPause(int con);
148 synchronized void nContextPause() {
149 rsnContextPause(mContext);
150 }
151 native void rsnContextResume(int con);
152 synchronized void nContextResume() {
153 rsnContextResume(mContext);
154 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700155
Jason Sams2e1872f2010-08-17 16:25:41 -0700156 native void rsnAssignName(int con, int obj, byte[] name);
157 synchronized void nAssignName(int obj, byte[] name) {
158 rsnAssignName(mContext, obj, name);
159 }
160 native String rsnGetName(int con, int obj);
161 synchronized String nGetName(int obj) {
162 return rsnGetName(mContext, obj);
163 }
164 native void rsnObjDestroy(int con, int id);
165 synchronized void nObjDestroy(int id) {
166 rsnObjDestroy(mContext, id);
167 }
Jason Samsfe08d992009-05-27 14:45:32 -0700168
Jason Sams2e1872f2010-08-17 16:25:41 -0700169 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
170 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
171 return rsnElementCreate(mContext, type, kind, norm, vecSize);
172 }
Jason Sams70d4e502010-09-02 17:35:23 -0700173 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
174 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
175 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700176 }
177 native void rsnElementGetNativeData(int con, int id, int[] elementData);
178 synchronized void nElementGetNativeData(int id, int[] elementData) {
179 rsnElementGetNativeData(mContext, id, elementData);
180 }
181 native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names);
182 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) {
183 rsnElementGetSubElements(mContext, id, IDs, names);
184 }
Jason Sams768bc022009-09-21 19:41:04 -0700185
Jason Sams3b9c52a2010-10-14 17:48:46 -0700186 native int rsnTypeCreate(int con, int eid, int[] dims, int[] vals);
187 synchronized int nTypeCreate(int eid, int[] dims, int[] vals) {
188 return rsnTypeCreate(mContext, eid, dims, vals);
Jason Sams2e1872f2010-08-17 16:25:41 -0700189 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700190 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
191 synchronized void nTypeGetNativeData(int id, int[] typeData) {
192 rsnTypeGetNativeData(mContext, id, typeData);
193 }
Jason Sams768bc022009-09-21 19:41:04 -0700194
Jason Sams2e1872f2010-08-17 16:25:41 -0700195 native int rsnAllocationCreateTyped(int con, int type);
196 synchronized int nAllocationCreateTyped(int type) {
197 return rsnAllocationCreateTyped(mContext, type);
198 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700199 native void rsnAllocationUpdateFromBitmap(int con, int alloc, Bitmap bmp);
200 synchronized void nAllocationUpdateFromBitmap(int alloc, Bitmap bmp) {
201 rsnAllocationUpdateFromBitmap(mContext, alloc, bmp);
202 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700203 native int rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
204 synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
205 return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
206 }
207 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
208 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
209 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
210 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700211 native int rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
212 synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
213 return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
214 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700215
Jason Sams2e1872f2010-08-17 16:25:41 -0700216 native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
217 synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) {
218 rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel);
219 }
220 native void rsnAllocationUploadToBufferObject(int con, int alloc);
221 synchronized void nAllocationUploadToBufferObject(int alloc) {
222 rsnAllocationUploadToBufferObject(mContext, alloc);
223 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700224
Jason Sams2e1872f2010-08-17 16:25:41 -0700225 native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes);
226 synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) {
227 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
228 }
229 native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes);
230 synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) {
231 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
232 }
233 native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes);
234 synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) {
235 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
236 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700237 native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
238 synchronized void nAllocationSubElementData1D(int id, int xoff, int compIdx, byte[] d, int sizeBytes) {
239 rsnAllocationSubElementData1D(mContext, id, xoff, compIdx, d, sizeBytes);
240 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700241 native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes);
242 synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) {
243 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
244 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700245
Jason Sams2e1872f2010-08-17 16:25:41 -0700246 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
247 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) {
248 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
249 }
250 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
251 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) {
252 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
253 }
254 native void rsnAllocationRead(int con, int id, int[] d);
255 synchronized void nAllocationRead(int id, int[] d) {
256 rsnAllocationRead(mContext, id, d);
257 }
258 native void rsnAllocationRead(int con, int id, float[] d);
259 synchronized void nAllocationRead(int id, float[] d) {
260 rsnAllocationRead(mContext, id, d);
261 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700262 native int rsnAllocationGetType(int con, int id);
263 synchronized int nAllocationGetType(int id) {
264 return rsnAllocationGetType(mContext, id);
265 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700266
Jason Sams5edc6082010-10-05 13:32:49 -0700267 native void rsnAllocationResize1D(int con, int id, int dimX);
268 synchronized void nAllocationResize1D(int id, int dimX) {
269 rsnAllocationResize1D(mContext, id, dimX);
270 }
271 native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
272 synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
273 rsnAllocationResize2D(mContext, id, dimX, dimY);
274 }
275
Jason Sams2e1872f2010-08-17 16:25:41 -0700276 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
277 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
278 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
279 }
280 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
281 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
282 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
283 }
284 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
285 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
286 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
287 }
288 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
289 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
290 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
291 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700292
Jason Sams2e1872f2010-08-17 16:25:41 -0700293 native int rsnFontCreateFromFile(int con, String fileName, int size, int dpi);
294 synchronized int nFontCreateFromFile(String fileName, int size, int dpi) {
295 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
296 }
Jason Sams22534172009-08-04 16:58:20 -0700297
Jason Sams2e1872f2010-08-17 16:25:41 -0700298 native void rsnAdapter1DBindAllocation(int con, int ad, int alloc);
299 synchronized void nAdapter1DBindAllocation(int ad, int alloc) {
300 rsnAdapter1DBindAllocation(mContext, ad, alloc);
301 }
302 native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value);
303 synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) {
304 rsnAdapter1DSetConstraint(mContext, ad, dim, value);
305 }
306 native void rsnAdapter1DData(int con, int ad, int[] d);
307 synchronized void nAdapter1DData(int ad, int[] d) {
308 rsnAdapter1DData(mContext, ad, d);
309 }
310 native void rsnAdapter1DData(int con, int ad, float[] d);
311 synchronized void nAdapter1DData(int ad, float[] d) {
312 rsnAdapter1DData(mContext, ad, d);
313 }
314 native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d);
315 synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) {
316 rsnAdapter1DSubData(mContext, ad, off, count, d);
317 }
318 native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d);
319 synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) {
320 rsnAdapter1DSubData(mContext, ad, off, count, d);
321 }
322 native int rsnAdapter1DCreate(int con);
323 synchronized int nAdapter1DCreate() {
324 return rsnAdapter1DCreate(mContext);
325 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700326
Jason Sams2e1872f2010-08-17 16:25:41 -0700327 native void rsnAdapter2DBindAllocation(int con, int ad, int alloc);
328 synchronized void nAdapter2DBindAllocation(int ad, int alloc) {
329 rsnAdapter2DBindAllocation(mContext, ad, alloc);
330 }
331 native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value);
332 synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) {
333 rsnAdapter2DSetConstraint(mContext, ad, dim, value);
334 }
335 native void rsnAdapter2DData(int con, int ad, int[] d);
336 synchronized void nAdapter2DData(int ad, int[] d) {
337 rsnAdapter2DData(mContext, ad, d);
338 }
339 native void rsnAdapter2DData(int con, int ad, float[] d);
340 synchronized void nAdapter2DData(int ad, float[] d) {
341 rsnAdapter2DData(mContext, ad, d);
342 }
343 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d);
344 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) {
345 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
346 }
347 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d);
348 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) {
349 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
350 }
351 native int rsnAdapter2DCreate(int con);
352 synchronized int nAdapter2DCreate() {
353 return rsnAdapter2DCreate(mContext);
354 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700355
Jason Sams2e1872f2010-08-17 16:25:41 -0700356 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
357 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
358 rsnScriptBindAllocation(mContext, script, alloc, slot);
359 }
360 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
361 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
362 rsnScriptSetTimeZone(mContext, script, timeZone);
363 }
364 native void rsnScriptInvoke(int con, int id, int slot);
365 synchronized void nScriptInvoke(int id, int slot) {
366 rsnScriptInvoke(mContext, id, slot);
367 }
368 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
369 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
370 rsnScriptInvokeV(mContext, id, slot, params);
371 }
372 native void rsnScriptSetVarI(int con, int id, int slot, int val);
373 synchronized void nScriptSetVarI(int id, int slot, int val) {
374 rsnScriptSetVarI(mContext, id, slot, val);
375 }
Stephen Hines031ec58c2010-10-11 10:54:21 -0700376 native void rsnScriptSetVarJ(int con, int id, int slot, long val);
377 synchronized void nScriptSetVarJ(int id, int slot, long val) {
378 rsnScriptSetVarJ(mContext, id, slot, val);
379 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700380 native void rsnScriptSetVarF(int con, int id, int slot, float val);
381 synchronized void nScriptSetVarF(int id, int slot, float val) {
382 rsnScriptSetVarF(mContext, id, slot, val);
383 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700384 native void rsnScriptSetVarD(int con, int id, int slot, double val);
385 synchronized void nScriptSetVarD(int id, int slot, double val) {
386 rsnScriptSetVarD(mContext, id, slot, val);
387 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700388 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
389 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
390 rsnScriptSetVarV(mContext, id, slot, val);
391 }
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800392 native void rsnScriptSetVarObj(int con, int id, int slot, int val);
393 synchronized void nScriptSetVarObj(int id, int slot, int val) {
394 rsnScriptSetVarObj(mContext, id, slot, val);
395 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700396
Jason Sams2e1872f2010-08-17 16:25:41 -0700397 native void rsnScriptCBegin(int con);
398 synchronized void nScriptCBegin() {
399 rsnScriptCBegin(mContext);
400 }
401 native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
402 synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
403 rsnScriptCSetScript(mContext, script, offset, length);
404 }
405 native int rsnScriptCCreate(int con);
406 synchronized int nScriptCCreate() {
407 return rsnScriptCCreate(mContext);
408 }
Jason Samsebfb4362009-09-23 13:57:02 -0700409
Jason Sams2e1872f2010-08-17 16:25:41 -0700410 native void rsnSamplerBegin(int con);
411 synchronized void nSamplerBegin() {
412 rsnSamplerBegin(mContext);
413 }
414 native void rsnSamplerSet(int con, int param, int value);
415 synchronized void nSamplerSet(int param, int value) {
416 rsnSamplerSet(mContext, param, value);
417 }
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700418 native void rsnSamplerSet2(int con, int param, float value);
419 synchronized void nSamplerSet2(int param, float value) {
420 rsnSamplerSet2(mContext, param, value);
421 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700422 native int rsnSamplerCreate(int con);
423 synchronized int nSamplerCreate() {
424 return rsnSamplerCreate(mContext);
425 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800426
Jason Sams2e1872f2010-08-17 16:25:41 -0700427 native void rsnProgramStoreBegin(int con, int in, int out);
428 synchronized void nProgramStoreBegin(int in, int out) {
429 rsnProgramStoreBegin(mContext, in, out);
430 }
431 native void rsnProgramStoreDepthFunc(int con, int func);
432 synchronized void nProgramStoreDepthFunc(int func) {
433 rsnProgramStoreDepthFunc(mContext, func);
434 }
435 native void rsnProgramStoreDepthMask(int con, boolean enable);
436 synchronized void nProgramStoreDepthMask(boolean enable) {
437 rsnProgramStoreDepthMask(mContext, enable);
438 }
439 native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
440 synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
441 rsnProgramStoreColorMask(mContext, r, g, b, a);
442 }
443 native void rsnProgramStoreBlendFunc(int con, int src, int dst);
444 synchronized void nProgramStoreBlendFunc(int src, int dst) {
445 rsnProgramStoreBlendFunc(mContext, src, dst);
446 }
447 native void rsnProgramStoreDither(int con, boolean enable);
448 synchronized void nProgramStoreDither(boolean enable) {
449 rsnProgramStoreDither(mContext, enable);
450 }
451 native int rsnProgramStoreCreate(int con);
452 synchronized int nProgramStoreCreate() {
453 return rsnProgramStoreCreate(mContext);
454 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700455
Jason Sams2e1872f2010-08-17 16:25:41 -0700456 native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
457 synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
458 return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
459 }
460 native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
461 synchronized void nProgramRasterSetLineWidth(int pr, float v) {
462 rsnProgramRasterSetLineWidth(mContext, pr, v);
463 }
464 native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
465 synchronized void nProgramRasterSetCullMode(int pr, int mode) {
466 rsnProgramRasterSetCullMode(mContext, pr, mode);
467 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700468
Jason Sams2e1872f2010-08-17 16:25:41 -0700469 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
470 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
471 rsnProgramBindConstants(mContext, pv, slot, mID);
472 }
473 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
474 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
475 rsnProgramBindTexture(mContext, vpf, slot, a);
476 }
477 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
478 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
479 rsnProgramBindSampler(mContext, vpf, slot, s);
480 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700481 native int rsnProgramFragmentCreate(int con, String shader, int[] params);
482 synchronized int nProgramFragmentCreate(String shader, int[] params) {
483 return rsnProgramFragmentCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700484 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700485 native int rsnProgramVertexCreate(int con, String shader, int[] params);
486 synchronized int nProgramVertexCreate(String shader, int[] params) {
487 return rsnProgramVertexCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700488 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700489
Jason Sams2e1872f2010-08-17 16:25:41 -0700490 native int rsnMeshCreate(int con, int vtxCount, int indexCount);
491 synchronized int nMeshCreate(int vtxCount, int indexCount) {
492 return rsnMeshCreate(mContext, vtxCount, indexCount);
493 }
494 native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
495 synchronized void nMeshBindVertex(int id, int alloc, int slot) {
496 rsnMeshBindVertex(mContext, id, alloc, slot);
497 }
498 native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
499 synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
500 rsnMeshBindIndex(mContext, id, alloc, prim, slot);
501 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800502 native void rsnMeshInitVertexAttribs(int con, int id);
503 synchronized void nMeshInitVertexAttribs(int id) {
504 rsnMeshInitVertexAttribs(mContext, id);
505 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700506 native int rsnMeshGetVertexBufferCount(int con, int id);
507 synchronized int nMeshGetVertexBufferCount(int id) {
508 return rsnMeshGetVertexBufferCount(mContext, id);
509 }
510 native int rsnMeshGetIndexCount(int con, int id);
511 synchronized int nMeshGetIndexCount(int id) {
512 return rsnMeshGetIndexCount(mContext, id);
513 }
514 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
515 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
516 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
517 }
518 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
519 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
520 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
521 }
522
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700523
Jason Sams704ff642010-02-09 16:05:07 -0800524 protected int mDev;
525 protected int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700526 @SuppressWarnings({"FieldCanBeLocal"})
Jason Sams704ff642010-02-09 16:05:07 -0800527 protected MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700528
Jason Sams8cb39de2010-06-01 15:47:01 -0700529 Element mElement_U8;
530 Element mElement_I8;
531 Element mElement_U16;
532 Element mElement_I16;
533 Element mElement_U32;
534 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -0700535 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -0700536 Element mElement_I64;
Jason Sams8cb39de2010-06-01 15:47:01 -0700537 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700538 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700539 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700540
Jason Sams8cb39de2010-06-01 15:47:01 -0700541 Element mElement_ELEMENT;
542 Element mElement_TYPE;
543 Element mElement_ALLOCATION;
544 Element mElement_SAMPLER;
545 Element mElement_SCRIPT;
546 Element mElement_MESH;
547 Element mElement_PROGRAM_FRAGMENT;
548 Element mElement_PROGRAM_VERTEX;
549 Element mElement_PROGRAM_RASTER;
550 Element mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700551
Jason Sams3c0dfba2009-09-27 17:50:38 -0700552 Element mElement_A_8;
553 Element mElement_RGB_565;
554 Element mElement_RGB_888;
555 Element mElement_RGBA_5551;
556 Element mElement_RGBA_4444;
557 Element mElement_RGBA_8888;
558
Jason Sams8cb39de2010-06-01 15:47:01 -0700559 Element mElement_FLOAT_2;
560 Element mElement_FLOAT_3;
561 Element mElement_FLOAT_4;
562 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800563
Jason Sams1d45c472010-08-25 14:31:48 -0700564 Element mElement_MATRIX_4X4;
565 Element mElement_MATRIX_3X3;
566 Element mElement_MATRIX_2X2;
567
Jason Sams4d339932010-05-11 14:03:58 -0700568 Sampler mSampler_CLAMP_NEAREST;
569 Sampler mSampler_CLAMP_LINEAR;
570 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
571 Sampler mSampler_WRAP_NEAREST;
572 Sampler mSampler_WRAP_LINEAR;
573 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
574
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700575 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
576 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
577 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_TEST;
578 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_WRITE;
579 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
580 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
581 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_TEST;
582 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_WRITE;
583 ProgramStore mProgramStore_BLEND_ADD_DEPTH_TEST;
584 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_DEPTH;
585 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_TEST;
586 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_WRITE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700587
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700588 ProgramRaster mProgramRaster_CULL_BACK;
589 ProgramRaster mProgramRaster_CULL_FRONT;
590 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700591
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700592 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700593 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700594
Jason Sams27676fe2010-11-10 17:00:59 -0800595 /**
596 * Base class application should derive from for handling RS messages
597 * comming from their scripts. When a script calls sendToClient the data
598 * fields will be filled in and then the run method called by a message
599 * handling thread. This will occur some time after sendToClient completes
600 * in the script.
601 *
602 */
Jason Sams516c3192009-10-06 13:58:47 -0700603 public static class RSMessage implements Runnable {
604 protected int[] mData;
605 protected int mID;
Jason Sams1c415172010-11-08 17:06:46 -0800606 protected int mLength;
Jason Sams516c3192009-10-06 13:58:47 -0700607 public void run() {
608 }
609 }
Jason Sams27676fe2010-11-10 17:00:59 -0800610 /**
611 * If an application is expecting messages it should set this field to an
612 * instance of RSMessage. This instance will receive all the user messages
613 * sent from sendToClient by scripts from this context.
614 *
615 */
Jason Sams516c3192009-10-06 13:58:47 -0700616 public RSMessage mMessageCallback = null;
617
Jason Sams27676fe2010-11-10 17:00:59 -0800618 /**
619 * Runtime error base class. An application should derive from this class
620 * if it wishes to install an error handler. When errors occur at runtime
621 * the fields in this class will be filled and the run method called.
622 *
623 */
Jason Sams1c415172010-11-08 17:06:46 -0800624 public static class RSAsyncError implements Runnable {
625 protected String mErrorMessage;
626 protected int mErrorNum;
627 public void run() {
628 }
629 }
Jason Sams27676fe2010-11-10 17:00:59 -0800630
631 /**
632 * Application Error handler. All runtime errors will be dispatched to the
633 * instance of RSAsyncError set here. If this field is null a
634 * RSRuntimeException will instead be thrown with details about the error.
635 * This will cause program termaination.
636 *
637 */
Jason Sams1c415172010-11-08 17:06:46 -0800638 public RSAsyncError mErrorCallback = null;
639
Jason Sams27676fe2010-11-10 17:00:59 -0800640 /**
641 * RenderScript worker threads priority enumeration. The default value is
642 * NORMAL. Applications wishing to do background processing such as
643 * wallpapers should set their priority to LOW to avoid starving forground
644 * processes.
645 */
Jason Sams7d787b42009-11-15 12:14:26 -0800646 public enum Priority {
647 LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5
648 NORMAL (-4); //ANDROID_PRIORITY_DISPLAY
649
650 int mID;
651 Priority(int id) {
652 mID = id;
653 }
654 }
655
Jason Sams771bebb2009-12-07 12:40:12 -0800656 void validate() {
657 if (mContext == 0) {
Jason Samsc1d62102010-11-04 14:32:19 -0700658 throw new RSInvalidStateException("Calling RS with no Context active.");
Jason Sams771bebb2009-12-07 12:40:12 -0800659 }
660 }
661
Jason Sams27676fe2010-11-10 17:00:59 -0800662
663 /**
664 * Change the priority of the worker threads for this context.
665 *
666 * @param p New priority to be set.
667 */
Jason Sams7d787b42009-11-15 12:14:26 -0800668 public void contextSetPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800669 validate();
Jason Sams7d787b42009-11-15 12:14:26 -0800670 nContextSetPriority(p.mID);
671 }
672
Jason Sams704ff642010-02-09 16:05:07 -0800673 protected static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -0700674 RenderScript mRS;
675 boolean mRun = true;
Jason Sams1c415172010-11-08 17:06:46 -0800676 int[] auxData = new int[2];
677
678 public static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
679 public static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
680 public static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
681 public static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
682 public static final int RS_MESSAGE_TO_CLIENT_USER = 4;
Jason Sams516c3192009-10-06 13:58:47 -0700683
684 MessageThread(RenderScript rs) {
685 super("RSMessageThread");
686 mRS = rs;
687
688 }
689
690 public void run() {
691 // This function is a temporary solution. The final solution will
692 // used typed allocations where the message id is the type indicator.
693 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -0700694 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700695 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -0700696 rbuf[0] = 0;
Jason Sams1c415172010-11-08 17:06:46 -0800697 int msg = mRS.nContextPeekMessage(mRS.mContext, auxData, true);
698 int size = auxData[1];
699 int subID = auxData[0];
700
701 if (msg == RS_MESSAGE_TO_CLIENT_USER) {
702 if ((size>>2) >= rbuf.length) {
703 rbuf = new int[(size + 3) >> 2];
704 }
705 mRS.nContextGetUserMessage(mRS.mContext, rbuf);
706
707 if(mRS.mMessageCallback != null) {
708 mRS.mMessageCallback.mData = rbuf;
709 mRS.mMessageCallback.mID = subID;
710 mRS.mMessageCallback.mLength = size;
711 mRS.mMessageCallback.run();
Jason Sams1d45c472010-08-25 14:31:48 -0700712 } else {
Jason Sams1c415172010-11-08 17:06:46 -0800713 throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
Jason Sams516c3192009-10-06 13:58:47 -0700714 }
Stephen Hinesab98bb62010-09-24 14:38:30 -0700715 continue;
Jason Sams516c3192009-10-06 13:58:47 -0700716 }
Jason Sams1c415172010-11-08 17:06:46 -0800717
718 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
719 String e = mRS.nContextGetErrorMessage(mRS.mContext);
720
721 if(mRS.mErrorCallback != null) {
722 mRS.mErrorCallback.mErrorMessage = e;
723 mRS.mErrorCallback.mErrorNum = subID;
724 mRS.mErrorCallback.run();
725 } else {
726 //throw new RSRuntimeException("Received error num " + subID + ", details: " + e);
727 }
728 continue;
729 }
730
731 // 2: teardown.
732 // But we want to avoid starving other threads during
733 // teardown by yielding until the next line in the destructor
734 // can execute to set mRun = false
735 try {
736 sleep(1, 0);
737 } catch(InterruptedException e) {
Jason Sams516c3192009-10-06 13:58:47 -0700738 }
Jason Sams516c3192009-10-06 13:58:47 -0700739 }
Jason Sams3bc47d42009-11-12 15:10:25 -0800740 Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -0700741 }
742 }
743
Jason Sams27676fe2010-11-10 17:00:59 -0800744 RenderScript() {
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700745 }
746
Jason Sams27676fe2010-11-10 17:00:59 -0800747 /**
748 * Create a basic RenderScript context.
749 *
750 *
751 * @return RenderScript
752 */
Jason Sams704ff642010-02-09 16:05:07 -0800753 public static RenderScript create() {
754 RenderScript rs = new RenderScript();
755
756 rs.mDev = rs.nDeviceCreate();
757 rs.mContext = rs.nContextCreate(rs.mDev, 0);
758 rs.mMessageThread = new MessageThread(rs);
759 rs.mMessageThread.start();
760 Element.initPredefined(rs);
761 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800762 }
763
Jason Sams27676fe2010-11-10 17:00:59 -0800764 /**
765 * Print the currently available debugging information about the state of
766 * the RS context to the log.
767 *
768 *
769 * @param bits Currently ignored.
770 */
Jason Sams715333b2009-11-17 17:26:46 -0800771 public void contextDump(int bits) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800772 validate();
Jason Sams715333b2009-11-17 17:26:46 -0800773 nContextDump(bits);
774 }
775
Jason Sams27676fe2010-11-10 17:00:59 -0800776 /**
777 * Wait for any commands in the fifo between the java bindings and native to
778 * be processed.
779 *
780 */
Jason Sams96ed4cf2010-06-15 12:15:57 -0700781 public void finish() {
782 nContextFinish();
783 }
784
Jason Sams27676fe2010-11-10 17:00:59 -0800785 /**
786 * Destroy this renderscript context. Once this function is called its no
787 * longer legal to use this or any objects created by this context.
788 *
789 */
Jason Samsf5b45962009-08-25 14:49:07 -0700790 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -0800791 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700792 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700793 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -0700794 try {
795 mMessageThread.join();
796 } catch(InterruptedException e) {
797 }
Jason Sams516c3192009-10-06 13:58:47 -0700798
Jason Sams2e1872f2010-08-17 16:25:41 -0700799 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -0700800 mContext = 0;
801
802 nDeviceDestroy(mDev);
803 mDev = 0;
804 }
Jason Sams02fb2cb2009-05-28 15:37:57 -0700805
Jason Samsa9e7a052009-09-25 14:51:22 -0700806 boolean isAlive() {
807 return mContext != 0;
808 }
809
Jason Sams704ff642010-02-09 16:05:07 -0800810 protected int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -0700811 if(o != null) {
Jason Sams06d69de2010-11-09 17:11:40 -0800812 return o.getID();
Jason Samsd8e41612009-08-20 17:22:40 -0700813 }
Jason Sams6b9dec02009-09-23 16:38:37 -0700814 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700815 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700816}
817
Jason Sams36e612a2009-07-31 16:26:13 -0700818
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700819