blob: b3774d406e68857bf3680b3ab8b752cdff9e5407 [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 *
31 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070032public class RenderScript {
Jason Sams3bc47d42009-11-12 15:10:25 -080033 static final String LOG_TAG = "RenderScript_jni";
Jason Sams704ff642010-02-09 16:05:07 -080034 protected static final boolean DEBUG = false;
Romain Guy650a3eb2009-08-31 14:06:43 -070035 @SuppressWarnings({"UnusedDeclaration", "deprecation"})
Jason Sams704ff642010-02-09 16:05:07 -080036 protected static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070037
38
39
Jason Sams02fb2cb2009-05-28 15:37:57 -070040 /*
Jack Palevich60aa3ea2009-05-26 13:45:08 -070041 * We use a class initializer to allow the native code to cache some
42 * field offsets.
43 */
Romain Guy650a3eb2009-08-31 14:06:43 -070044 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Jason Sams704ff642010-02-09 16:05:07 -080045 protected static boolean sInitialized;
46 native protected static void _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070047
Jason Samsdba3ba52009-07-30 14:56:12 -070048
Jack Palevich60aa3ea2009-05-26 13:45:08 -070049 static {
50 sInitialized = false;
51 try {
Jason Samse29d4712009-07-23 15:19:03 -070052 System.loadLibrary("rs_jni");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070053 _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070054 sInitialized = true;
55 } catch (UnsatisfiedLinkError e) {
56 Log.d(LOG_TAG, "RenderScript JNI library not found!");
57 }
58 }
59
Jason Sams2e1872f2010-08-17 16:25:41 -070060 // Non-threadsafe functions.
Jason Samsea84a7c2009-09-04 14:42:41 -070061 native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565);
Jason Sams36e612a2009-07-31 16:26:13 -070062 native int nDeviceCreate();
63 native void nDeviceDestroy(int dev);
Jason Samsebfb4362009-09-23 13:57:02 -070064 native void nDeviceSetConfig(int dev, int param, int value);
Jason Sams1c415172010-11-08 17:06:46 -080065 native void nContextGetUserMessage(int con, int[] data);
66 native String nContextGetErrorMessage(int con);
67 native int nContextPeekMessage(int con, int[] subID, boolean wait);
Jason Sams2e1872f2010-08-17 16:25:41 -070068 native void nContextInitToClient(int con);
69 native void nContextDeinitToClient(int con);
Jason Sams3eaa3382009-06-10 15:04:38 -070070
Jason Sams718cd1f2009-12-23 14:35:29 -080071
Jason Sams2e1872f2010-08-17 16:25:41 -070072 // Methods below are wrapped to protect the non-threadsafe
73 // lockless fifo.
Jason Sams11c8af92010-10-13 15:31:10 -070074 native int rsnContextCreateGL(int dev, int ver,
75 int colorMin, int colorPref,
76 int alphaMin, int alphaPref,
77 int depthMin, int depthPref,
78 int stencilMin, int stencilPref,
79 int samplesMin, int samplesPref, float samplesQ);
80 synchronized int nContextCreateGL(int dev, int ver,
81 int colorMin, int colorPref,
82 int alphaMin, int alphaPref,
83 int depthMin, int depthPref,
84 int stencilMin, int stencilPref,
85 int samplesMin, int samplesPref, float samplesQ) {
86 return rsnContextCreateGL(dev, ver, colorMin, colorPref,
87 alphaMin, alphaPref, depthMin, depthPref,
88 stencilMin, stencilPref,
89 samplesMin, samplesPref, samplesQ);
Jason Sams2e1872f2010-08-17 16:25:41 -070090 }
91 native int rsnContextCreate(int dev, int ver);
92 synchronized int nContextCreate(int dev, int ver) {
93 return rsnContextCreate(dev, ver);
94 }
95 native void rsnContextDestroy(int con);
96 synchronized void nContextDestroy() {
97 rsnContextDestroy(mContext);
98 }
99 native void rsnContextSetSurface(int con, int w, int h, Surface sur);
100 synchronized void nContextSetSurface(int w, int h, Surface sur) {
101 rsnContextSetSurface(mContext, w, h, sur);
102 }
103 native void rsnContextSetPriority(int con, int p);
104 synchronized void nContextSetPriority(int p) {
105 rsnContextSetPriority(mContext, p);
106 }
107 native void rsnContextDump(int con, int bits);
108 synchronized void nContextDump(int bits) {
109 rsnContextDump(mContext, bits);
110 }
111 native void rsnContextFinish(int con);
112 synchronized void nContextFinish() {
113 rsnContextFinish(mContext);
114 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700115
Jason Sams2e1872f2010-08-17 16:25:41 -0700116 native void rsnContextBindRootScript(int con, int script);
117 synchronized void nContextBindRootScript(int script) {
118 rsnContextBindRootScript(mContext, script);
119 }
120 native void rsnContextBindSampler(int con, int sampler, int slot);
121 synchronized void nContextBindSampler(int sampler, int slot) {
122 rsnContextBindSampler(mContext, sampler, slot);
123 }
124 native void rsnContextBindProgramStore(int con, int pfs);
125 synchronized void nContextBindProgramStore(int pfs) {
126 rsnContextBindProgramStore(mContext, pfs);
127 }
128 native void rsnContextBindProgramFragment(int con, int pf);
129 synchronized void nContextBindProgramFragment(int pf) {
130 rsnContextBindProgramFragment(mContext, pf);
131 }
132 native void rsnContextBindProgramVertex(int con, int pv);
133 synchronized void nContextBindProgramVertex(int pv) {
134 rsnContextBindProgramVertex(mContext, pv);
135 }
136 native void rsnContextBindProgramRaster(int con, int pr);
137 synchronized void nContextBindProgramRaster(int pr) {
138 rsnContextBindProgramRaster(mContext, pr);
139 }
140 native void rsnContextPause(int con);
141 synchronized void nContextPause() {
142 rsnContextPause(mContext);
143 }
144 native void rsnContextResume(int con);
145 synchronized void nContextResume() {
146 rsnContextResume(mContext);
147 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700148
Jason Sams2e1872f2010-08-17 16:25:41 -0700149 native void rsnAssignName(int con, int obj, byte[] name);
150 synchronized void nAssignName(int obj, byte[] name) {
151 rsnAssignName(mContext, obj, name);
152 }
153 native String rsnGetName(int con, int obj);
154 synchronized String nGetName(int obj) {
155 return rsnGetName(mContext, obj);
156 }
157 native void rsnObjDestroy(int con, int id);
158 synchronized void nObjDestroy(int id) {
159 rsnObjDestroy(mContext, id);
160 }
Jason Samsfe08d992009-05-27 14:45:32 -0700161
Jason Sams2e1872f2010-08-17 16:25:41 -0700162 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
163 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
164 return rsnElementCreate(mContext, type, kind, norm, vecSize);
165 }
Jason Sams70d4e502010-09-02 17:35:23 -0700166 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
167 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
168 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700169 }
170 native void rsnElementGetNativeData(int con, int id, int[] elementData);
171 synchronized void nElementGetNativeData(int id, int[] elementData) {
172 rsnElementGetNativeData(mContext, id, elementData);
173 }
174 native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names);
175 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) {
176 rsnElementGetSubElements(mContext, id, IDs, names);
177 }
Jason Sams768bc022009-09-21 19:41:04 -0700178
Jason Sams3b9c52a2010-10-14 17:48:46 -0700179 native int rsnTypeCreate(int con, int eid, int[] dims, int[] vals);
180 synchronized int nTypeCreate(int eid, int[] dims, int[] vals) {
181 return rsnTypeCreate(mContext, eid, dims, vals);
Jason Sams2e1872f2010-08-17 16:25:41 -0700182 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700183 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
184 synchronized void nTypeGetNativeData(int id, int[] typeData) {
185 rsnTypeGetNativeData(mContext, id, typeData);
186 }
Jason Sams768bc022009-09-21 19:41:04 -0700187
Jason Sams2e1872f2010-08-17 16:25:41 -0700188 native int rsnAllocationCreateTyped(int con, int type);
189 synchronized int nAllocationCreateTyped(int type) {
190 return rsnAllocationCreateTyped(mContext, type);
191 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700192 native void rsnAllocationUpdateFromBitmap(int con, int alloc, Bitmap bmp);
193 synchronized void nAllocationUpdateFromBitmap(int alloc, Bitmap bmp) {
194 rsnAllocationUpdateFromBitmap(mContext, alloc, bmp);
195 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700196 native int rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
197 synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
198 return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
199 }
200 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
201 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
202 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
203 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700204 native int rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
205 synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
206 return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
207 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700208
Jason Sams2e1872f2010-08-17 16:25:41 -0700209 native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
210 synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) {
211 rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel);
212 }
213 native void rsnAllocationUploadToBufferObject(int con, int alloc);
214 synchronized void nAllocationUploadToBufferObject(int alloc) {
215 rsnAllocationUploadToBufferObject(mContext, alloc);
216 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700217
Jason Sams2e1872f2010-08-17 16:25:41 -0700218 native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes);
219 synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) {
220 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
221 }
222 native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes);
223 synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) {
224 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
225 }
226 native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes);
227 synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) {
228 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
229 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700230 native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
231 synchronized void nAllocationSubElementData1D(int id, int xoff, int compIdx, byte[] d, int sizeBytes) {
232 rsnAllocationSubElementData1D(mContext, id, xoff, compIdx, d, sizeBytes);
233 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700234 native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes);
235 synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) {
236 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
237 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700238
Jason Sams2e1872f2010-08-17 16:25:41 -0700239 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
240 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) {
241 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
242 }
243 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
244 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) {
245 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
246 }
247 native void rsnAllocationRead(int con, int id, int[] d);
248 synchronized void nAllocationRead(int id, int[] d) {
249 rsnAllocationRead(mContext, id, d);
250 }
251 native void rsnAllocationRead(int con, int id, float[] d);
252 synchronized void nAllocationRead(int id, float[] d) {
253 rsnAllocationRead(mContext, id, d);
254 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700255 native int rsnAllocationGetType(int con, int id);
256 synchronized int nAllocationGetType(int id) {
257 return rsnAllocationGetType(mContext, id);
258 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700259
Jason Sams5edc6082010-10-05 13:32:49 -0700260 native void rsnAllocationResize1D(int con, int id, int dimX);
261 synchronized void nAllocationResize1D(int id, int dimX) {
262 rsnAllocationResize1D(mContext, id, dimX);
263 }
264 native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
265 synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
266 rsnAllocationResize2D(mContext, id, dimX, dimY);
267 }
268
Jason Sams2e1872f2010-08-17 16:25:41 -0700269 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
270 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
271 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
272 }
273 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
274 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
275 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
276 }
277 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
278 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
279 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
280 }
281 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
282 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
283 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
284 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700285
Jason Sams2e1872f2010-08-17 16:25:41 -0700286 native int rsnFontCreateFromFile(int con, String fileName, int size, int dpi);
287 synchronized int nFontCreateFromFile(String fileName, int size, int dpi) {
288 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
289 }
Jason Sams22534172009-08-04 16:58:20 -0700290
Jason Sams2e1872f2010-08-17 16:25:41 -0700291 native void rsnAdapter1DBindAllocation(int con, int ad, int alloc);
292 synchronized void nAdapter1DBindAllocation(int ad, int alloc) {
293 rsnAdapter1DBindAllocation(mContext, ad, alloc);
294 }
295 native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value);
296 synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) {
297 rsnAdapter1DSetConstraint(mContext, ad, dim, value);
298 }
299 native void rsnAdapter1DData(int con, int ad, int[] d);
300 synchronized void nAdapter1DData(int ad, int[] d) {
301 rsnAdapter1DData(mContext, ad, d);
302 }
303 native void rsnAdapter1DData(int con, int ad, float[] d);
304 synchronized void nAdapter1DData(int ad, float[] d) {
305 rsnAdapter1DData(mContext, ad, d);
306 }
307 native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d);
308 synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) {
309 rsnAdapter1DSubData(mContext, ad, off, count, d);
310 }
311 native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d);
312 synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) {
313 rsnAdapter1DSubData(mContext, ad, off, count, d);
314 }
315 native int rsnAdapter1DCreate(int con);
316 synchronized int nAdapter1DCreate() {
317 return rsnAdapter1DCreate(mContext);
318 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700319
Jason Sams2e1872f2010-08-17 16:25:41 -0700320 native void rsnAdapter2DBindAllocation(int con, int ad, int alloc);
321 synchronized void nAdapter2DBindAllocation(int ad, int alloc) {
322 rsnAdapter2DBindAllocation(mContext, ad, alloc);
323 }
324 native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value);
325 synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) {
326 rsnAdapter2DSetConstraint(mContext, ad, dim, value);
327 }
328 native void rsnAdapter2DData(int con, int ad, int[] d);
329 synchronized void nAdapter2DData(int ad, int[] d) {
330 rsnAdapter2DData(mContext, ad, d);
331 }
332 native void rsnAdapter2DData(int con, int ad, float[] d);
333 synchronized void nAdapter2DData(int ad, float[] d) {
334 rsnAdapter2DData(mContext, ad, d);
335 }
336 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d);
337 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) {
338 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
339 }
340 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d);
341 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) {
342 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
343 }
344 native int rsnAdapter2DCreate(int con);
345 synchronized int nAdapter2DCreate() {
346 return rsnAdapter2DCreate(mContext);
347 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700348
Jason Sams2e1872f2010-08-17 16:25:41 -0700349 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
350 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
351 rsnScriptBindAllocation(mContext, script, alloc, slot);
352 }
353 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
354 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
355 rsnScriptSetTimeZone(mContext, script, timeZone);
356 }
357 native void rsnScriptInvoke(int con, int id, int slot);
358 synchronized void nScriptInvoke(int id, int slot) {
359 rsnScriptInvoke(mContext, id, slot);
360 }
361 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
362 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
363 rsnScriptInvokeV(mContext, id, slot, params);
364 }
365 native void rsnScriptSetVarI(int con, int id, int slot, int val);
366 synchronized void nScriptSetVarI(int id, int slot, int val) {
367 rsnScriptSetVarI(mContext, id, slot, val);
368 }
Stephen Hines031ec58c2010-10-11 10:54:21 -0700369 native void rsnScriptSetVarJ(int con, int id, int slot, long val);
370 synchronized void nScriptSetVarJ(int id, int slot, long val) {
371 rsnScriptSetVarJ(mContext, id, slot, val);
372 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700373 native void rsnScriptSetVarF(int con, int id, int slot, float val);
374 synchronized void nScriptSetVarF(int id, int slot, float val) {
375 rsnScriptSetVarF(mContext, id, slot, val);
376 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700377 native void rsnScriptSetVarD(int con, int id, int slot, double val);
378 synchronized void nScriptSetVarD(int id, int slot, double val) {
379 rsnScriptSetVarD(mContext, id, slot, val);
380 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700381 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
382 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
383 rsnScriptSetVarV(mContext, id, slot, val);
384 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700385
Jason Sams2e1872f2010-08-17 16:25:41 -0700386 native void rsnScriptCBegin(int con);
387 synchronized void nScriptCBegin() {
388 rsnScriptCBegin(mContext);
389 }
390 native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
391 synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
392 rsnScriptCSetScript(mContext, script, offset, length);
393 }
394 native int rsnScriptCCreate(int con);
395 synchronized int nScriptCCreate() {
396 return rsnScriptCCreate(mContext);
397 }
Jason Samsebfb4362009-09-23 13:57:02 -0700398
Jason Sams2e1872f2010-08-17 16:25:41 -0700399 native void rsnSamplerBegin(int con);
400 synchronized void nSamplerBegin() {
401 rsnSamplerBegin(mContext);
402 }
403 native void rsnSamplerSet(int con, int param, int value);
404 synchronized void nSamplerSet(int param, int value) {
405 rsnSamplerSet(mContext, param, value);
406 }
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700407 native void rsnSamplerSet2(int con, int param, float value);
408 synchronized void nSamplerSet2(int param, float value) {
409 rsnSamplerSet2(mContext, param, value);
410 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700411 native int rsnSamplerCreate(int con);
412 synchronized int nSamplerCreate() {
413 return rsnSamplerCreate(mContext);
414 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800415
Jason Sams2e1872f2010-08-17 16:25:41 -0700416 native void rsnProgramStoreBegin(int con, int in, int out);
417 synchronized void nProgramStoreBegin(int in, int out) {
418 rsnProgramStoreBegin(mContext, in, out);
419 }
420 native void rsnProgramStoreDepthFunc(int con, int func);
421 synchronized void nProgramStoreDepthFunc(int func) {
422 rsnProgramStoreDepthFunc(mContext, func);
423 }
424 native void rsnProgramStoreDepthMask(int con, boolean enable);
425 synchronized void nProgramStoreDepthMask(boolean enable) {
426 rsnProgramStoreDepthMask(mContext, enable);
427 }
428 native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
429 synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
430 rsnProgramStoreColorMask(mContext, r, g, b, a);
431 }
432 native void rsnProgramStoreBlendFunc(int con, int src, int dst);
433 synchronized void nProgramStoreBlendFunc(int src, int dst) {
434 rsnProgramStoreBlendFunc(mContext, src, dst);
435 }
436 native void rsnProgramStoreDither(int con, boolean enable);
437 synchronized void nProgramStoreDither(boolean enable) {
438 rsnProgramStoreDither(mContext, enable);
439 }
440 native int rsnProgramStoreCreate(int con);
441 synchronized int nProgramStoreCreate() {
442 return rsnProgramStoreCreate(mContext);
443 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700444
Jason Sams2e1872f2010-08-17 16:25:41 -0700445 native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
446 synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
447 return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
448 }
449 native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
450 synchronized void nProgramRasterSetLineWidth(int pr, float v) {
451 rsnProgramRasterSetLineWidth(mContext, pr, v);
452 }
453 native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
454 synchronized void nProgramRasterSetCullMode(int pr, int mode) {
455 rsnProgramRasterSetCullMode(mContext, pr, mode);
456 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700457
Jason Sams2e1872f2010-08-17 16:25:41 -0700458 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
459 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
460 rsnProgramBindConstants(mContext, pv, slot, mID);
461 }
462 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
463 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
464 rsnProgramBindTexture(mContext, vpf, slot, a);
465 }
466 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
467 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
468 rsnProgramBindSampler(mContext, vpf, slot, s);
469 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700470 native int rsnProgramFragmentCreate(int con, String shader, int[] params);
471 synchronized int nProgramFragmentCreate(String shader, int[] params) {
472 return rsnProgramFragmentCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700473 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700474 native int rsnProgramVertexCreate(int con, String shader, int[] params);
475 synchronized int nProgramVertexCreate(String shader, int[] params) {
476 return rsnProgramVertexCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700477 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700478
Jason Sams2e1872f2010-08-17 16:25:41 -0700479 native int rsnMeshCreate(int con, int vtxCount, int indexCount);
480 synchronized int nMeshCreate(int vtxCount, int indexCount) {
481 return rsnMeshCreate(mContext, vtxCount, indexCount);
482 }
483 native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
484 synchronized void nMeshBindVertex(int id, int alloc, int slot) {
485 rsnMeshBindVertex(mContext, id, alloc, slot);
486 }
487 native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
488 synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
489 rsnMeshBindIndex(mContext, id, alloc, prim, slot);
490 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800491 native void rsnMeshInitVertexAttribs(int con, int id);
492 synchronized void nMeshInitVertexAttribs(int id) {
493 rsnMeshInitVertexAttribs(mContext, id);
494 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700495 native int rsnMeshGetVertexBufferCount(int con, int id);
496 synchronized int nMeshGetVertexBufferCount(int id) {
497 return rsnMeshGetVertexBufferCount(mContext, id);
498 }
499 native int rsnMeshGetIndexCount(int con, int id);
500 synchronized int nMeshGetIndexCount(int id) {
501 return rsnMeshGetIndexCount(mContext, id);
502 }
503 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
504 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
505 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
506 }
507 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
508 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
509 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
510 }
511
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700512
Jason Sams704ff642010-02-09 16:05:07 -0800513 protected int mDev;
514 protected int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700515 @SuppressWarnings({"FieldCanBeLocal"})
Jason Sams704ff642010-02-09 16:05:07 -0800516 protected MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700517
Jason Sams8cb39de2010-06-01 15:47:01 -0700518 Element mElement_U8;
519 Element mElement_I8;
520 Element mElement_U16;
521 Element mElement_I16;
522 Element mElement_U32;
523 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -0700524 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -0700525 Element mElement_I64;
Jason Sams8cb39de2010-06-01 15:47:01 -0700526 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700527 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700528 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700529
Jason Sams8cb39de2010-06-01 15:47:01 -0700530 Element mElement_ELEMENT;
531 Element mElement_TYPE;
532 Element mElement_ALLOCATION;
533 Element mElement_SAMPLER;
534 Element mElement_SCRIPT;
535 Element mElement_MESH;
536 Element mElement_PROGRAM_FRAGMENT;
537 Element mElement_PROGRAM_VERTEX;
538 Element mElement_PROGRAM_RASTER;
539 Element mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700540
Jason Sams3c0dfba2009-09-27 17:50:38 -0700541 Element mElement_A_8;
542 Element mElement_RGB_565;
543 Element mElement_RGB_888;
544 Element mElement_RGBA_5551;
545 Element mElement_RGBA_4444;
546 Element mElement_RGBA_8888;
547
Jason Sams8cb39de2010-06-01 15:47:01 -0700548 Element mElement_FLOAT_2;
549 Element mElement_FLOAT_3;
550 Element mElement_FLOAT_4;
551 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800552
Jason Sams1d45c472010-08-25 14:31:48 -0700553 Element mElement_MATRIX_4X4;
554 Element mElement_MATRIX_3X3;
555 Element mElement_MATRIX_2X2;
556
Jason Sams4d339932010-05-11 14:03:58 -0700557 Sampler mSampler_CLAMP_NEAREST;
558 Sampler mSampler_CLAMP_LINEAR;
559 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
560 Sampler mSampler_WRAP_NEAREST;
561 Sampler mSampler_WRAP_LINEAR;
562 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
563
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700564 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
565 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
566 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_TEST;
567 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_WRITE;
568 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
569 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
570 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_TEST;
571 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_WRITE;
572 ProgramStore mProgramStore_BLEND_ADD_DEPTH_TEST;
573 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_DEPTH;
574 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_TEST;
575 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_WRITE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700576
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700577 ProgramRaster mProgramRaster_CULL_BACK;
578 ProgramRaster mProgramRaster_CULL_FRONT;
579 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700580
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700581 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700582 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700583
Jason Sams516c3192009-10-06 13:58:47 -0700584 public static class RSMessage implements Runnable {
585 protected int[] mData;
586 protected int mID;
Jason Sams1c415172010-11-08 17:06:46 -0800587 protected int mLength;
Jason Sams516c3192009-10-06 13:58:47 -0700588 public void run() {
589 }
590 }
591 public RSMessage mMessageCallback = null;
592
Jason Sams1c415172010-11-08 17:06:46 -0800593 public static class RSAsyncError implements Runnable {
594 protected String mErrorMessage;
595 protected int mErrorNum;
596 public void run() {
597 }
598 }
599 public RSAsyncError mErrorCallback = null;
600
Jason Sams7d787b42009-11-15 12:14:26 -0800601 public enum Priority {
602 LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5
603 NORMAL (-4); //ANDROID_PRIORITY_DISPLAY
604
605 int mID;
606 Priority(int id) {
607 mID = id;
608 }
609 }
610
Jason Sams771bebb2009-12-07 12:40:12 -0800611 void validate() {
612 if (mContext == 0) {
Jason Samsc1d62102010-11-04 14:32:19 -0700613 throw new RSInvalidStateException("Calling RS with no Context active.");
Jason Sams771bebb2009-12-07 12:40:12 -0800614 }
615 }
616
Jason Sams7d787b42009-11-15 12:14:26 -0800617 public void contextSetPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800618 validate();
Jason Sams7d787b42009-11-15 12:14:26 -0800619 nContextSetPriority(p.mID);
620 }
621
Jason Sams704ff642010-02-09 16:05:07 -0800622 protected static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -0700623 RenderScript mRS;
624 boolean mRun = true;
Jason Sams1c415172010-11-08 17:06:46 -0800625 int[] auxData = new int[2];
626
627 public static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
628 public static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
629 public static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
630 public static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
631 public static final int RS_MESSAGE_TO_CLIENT_USER = 4;
Jason Sams516c3192009-10-06 13:58:47 -0700632
633 MessageThread(RenderScript rs) {
634 super("RSMessageThread");
635 mRS = rs;
636
637 }
638
639 public void run() {
640 // This function is a temporary solution. The final solution will
641 // used typed allocations where the message id is the type indicator.
642 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -0700643 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700644 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -0700645 rbuf[0] = 0;
Jason Sams1c415172010-11-08 17:06:46 -0800646 int msg = mRS.nContextPeekMessage(mRS.mContext, auxData, true);
647 int size = auxData[1];
648 int subID = auxData[0];
649
650 if (msg == RS_MESSAGE_TO_CLIENT_USER) {
651 if ((size>>2) >= rbuf.length) {
652 rbuf = new int[(size + 3) >> 2];
653 }
654 mRS.nContextGetUserMessage(mRS.mContext, rbuf);
655
656 if(mRS.mMessageCallback != null) {
657 mRS.mMessageCallback.mData = rbuf;
658 mRS.mMessageCallback.mID = subID;
659 mRS.mMessageCallback.mLength = size;
660 mRS.mMessageCallback.run();
Jason Sams1d45c472010-08-25 14:31:48 -0700661 } else {
Jason Sams1c415172010-11-08 17:06:46 -0800662 throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
Jason Sams516c3192009-10-06 13:58:47 -0700663 }
Stephen Hinesab98bb62010-09-24 14:38:30 -0700664 continue;
Jason Sams516c3192009-10-06 13:58:47 -0700665 }
Jason Sams1c415172010-11-08 17:06:46 -0800666
667 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
668 String e = mRS.nContextGetErrorMessage(mRS.mContext);
669
670 if(mRS.mErrorCallback != null) {
671 mRS.mErrorCallback.mErrorMessage = e;
672 mRS.mErrorCallback.mErrorNum = subID;
673 mRS.mErrorCallback.run();
674 } else {
675 //throw new RSRuntimeException("Received error num " + subID + ", details: " + e);
676 }
677 continue;
678 }
679
680 // 2: teardown.
681 // But we want to avoid starving other threads during
682 // teardown by yielding until the next line in the destructor
683 // can execute to set mRun = false
684 try {
685 sleep(1, 0);
686 } catch(InterruptedException e) {
Jason Sams516c3192009-10-06 13:58:47 -0700687 }
Jason Sams516c3192009-10-06 13:58:47 -0700688 }
Jason Sams3bc47d42009-11-12 15:10:25 -0800689 Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -0700690 }
691 }
692
Jason Sams704ff642010-02-09 16:05:07 -0800693 protected RenderScript() {
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700694 }
695
Jason Sams704ff642010-02-09 16:05:07 -0800696 public static RenderScript create() {
697 RenderScript rs = new RenderScript();
698
699 rs.mDev = rs.nDeviceCreate();
700 rs.mContext = rs.nContextCreate(rs.mDev, 0);
701 rs.mMessageThread = new MessageThread(rs);
702 rs.mMessageThread.start();
703 Element.initPredefined(rs);
704 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800705 }
706
Jason Sams715333b2009-11-17 17:26:46 -0800707 public void contextDump(int bits) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800708 validate();
Jason Sams715333b2009-11-17 17:26:46 -0800709 nContextDump(bits);
710 }
711
Jason Sams96ed4cf2010-06-15 12:15:57 -0700712 public void finish() {
713 nContextFinish();
714 }
715
Jason Samsf5b45962009-08-25 14:49:07 -0700716 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -0800717 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700718 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700719 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -0700720 try {
721 mMessageThread.join();
722 } catch(InterruptedException e) {
723 }
Jason Sams516c3192009-10-06 13:58:47 -0700724
Jason Sams2e1872f2010-08-17 16:25:41 -0700725 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -0700726 mContext = 0;
727
728 nDeviceDestroy(mDev);
729 mDev = 0;
730 }
Jason Sams02fb2cb2009-05-28 15:37:57 -0700731
Jason Samsa9e7a052009-09-25 14:51:22 -0700732 boolean isAlive() {
733 return mContext != 0;
734 }
735
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700736 ///////////////////////////////////////////////////////////////////////////////////
737 // Root state
738
Jason Sams704ff642010-02-09 16:05:07 -0800739 protected int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -0700740 if(o != null) {
741 return o.mID;
Jason Samsd8e41612009-08-20 17:22:40 -0700742 }
Jason Sams6b9dec02009-09-23 16:38:37 -0700743 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700744 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700745}
746
Jason Sams36e612a2009-07-31 16:26:13 -0700747
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700748