blob: 8f1f2739ccca1aa707ad8322d47e7bfda7fd72ca [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 Sams2e1872f2010-08-17 16:25:41 -070065 native int nContextGetMessage(int con, int[] data, boolean wait);
66 native void nContextInitToClient(int con);
67 native void nContextDeinitToClient(int con);
Jason Sams3eaa338e2009-06-10 15:04:38 -070068
Jason Sams718cd1f2009-12-23 14:35:29 -080069
Jason Sams2e1872f2010-08-17 16:25:41 -070070 // Methods below are wrapped to protect the non-threadsafe
71 // lockless fifo.
Jason Sams11c8af92010-10-13 15:31:10 -070072 native int rsnContextCreateGL(int dev, int ver,
73 int colorMin, int colorPref,
74 int alphaMin, int alphaPref,
75 int depthMin, int depthPref,
76 int stencilMin, int stencilPref,
77 int samplesMin, int samplesPref, float samplesQ);
78 synchronized int nContextCreateGL(int dev, int ver,
79 int colorMin, int colorPref,
80 int alphaMin, int alphaPref,
81 int depthMin, int depthPref,
82 int stencilMin, int stencilPref,
83 int samplesMin, int samplesPref, float samplesQ) {
84 return rsnContextCreateGL(dev, ver, colorMin, colorPref,
85 alphaMin, alphaPref, depthMin, depthPref,
86 stencilMin, stencilPref,
87 samplesMin, samplesPref, samplesQ);
Jason Sams2e1872f2010-08-17 16:25:41 -070088 }
89 native int rsnContextCreate(int dev, int ver);
90 synchronized int nContextCreate(int dev, int ver) {
91 return rsnContextCreate(dev, ver);
92 }
93 native void rsnContextDestroy(int con);
94 synchronized void nContextDestroy() {
95 rsnContextDestroy(mContext);
96 }
97 native void rsnContextSetSurface(int con, int w, int h, Surface sur);
98 synchronized void nContextSetSurface(int w, int h, Surface sur) {
99 rsnContextSetSurface(mContext, w, h, sur);
100 }
101 native void rsnContextSetPriority(int con, int p);
102 synchronized void nContextSetPriority(int p) {
103 rsnContextSetPriority(mContext, p);
104 }
105 native void rsnContextDump(int con, int bits);
106 synchronized void nContextDump(int bits) {
107 rsnContextDump(mContext, bits);
108 }
109 native void rsnContextFinish(int con);
110 synchronized void nContextFinish() {
111 rsnContextFinish(mContext);
112 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700113
Jason Sams2e1872f2010-08-17 16:25:41 -0700114 native void rsnContextBindRootScript(int con, int script);
115 synchronized void nContextBindRootScript(int script) {
116 rsnContextBindRootScript(mContext, script);
117 }
118 native void rsnContextBindSampler(int con, int sampler, int slot);
119 synchronized void nContextBindSampler(int sampler, int slot) {
120 rsnContextBindSampler(mContext, sampler, slot);
121 }
122 native void rsnContextBindProgramStore(int con, int pfs);
123 synchronized void nContextBindProgramStore(int pfs) {
124 rsnContextBindProgramStore(mContext, pfs);
125 }
126 native void rsnContextBindProgramFragment(int con, int pf);
127 synchronized void nContextBindProgramFragment(int pf) {
128 rsnContextBindProgramFragment(mContext, pf);
129 }
130 native void rsnContextBindProgramVertex(int con, int pv);
131 synchronized void nContextBindProgramVertex(int pv) {
132 rsnContextBindProgramVertex(mContext, pv);
133 }
134 native void rsnContextBindProgramRaster(int con, int pr);
135 synchronized void nContextBindProgramRaster(int pr) {
136 rsnContextBindProgramRaster(mContext, pr);
137 }
138 native void rsnContextPause(int con);
139 synchronized void nContextPause() {
140 rsnContextPause(mContext);
141 }
142 native void rsnContextResume(int con);
143 synchronized void nContextResume() {
144 rsnContextResume(mContext);
145 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700146
Jason Sams2e1872f2010-08-17 16:25:41 -0700147 native void rsnAssignName(int con, int obj, byte[] name);
148 synchronized void nAssignName(int obj, byte[] name) {
149 rsnAssignName(mContext, obj, name);
150 }
151 native String rsnGetName(int con, int obj);
152 synchronized String nGetName(int obj) {
153 return rsnGetName(mContext, obj);
154 }
155 native void rsnObjDestroy(int con, int id);
156 synchronized void nObjDestroy(int id) {
157 rsnObjDestroy(mContext, id);
158 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700159 native int rsnFileOpen(int con, byte[] name);
160 synchronized int nFileOpen(byte[] name) {
161 return rsnFileOpen(mContext, name);
162 }
Jason Samsfe08d992009-05-27 14:45:32 -0700163
Jason Sams2e1872f2010-08-17 16:25:41 -0700164 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
165 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
166 return rsnElementCreate(mContext, type, kind, norm, vecSize);
167 }
Jason Sams70d4e502010-09-02 17:35:23 -0700168 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
169 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
170 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700171 }
172 native void rsnElementGetNativeData(int con, int id, int[] elementData);
173 synchronized void nElementGetNativeData(int id, int[] elementData) {
174 rsnElementGetNativeData(mContext, id, elementData);
175 }
176 native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names);
177 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) {
178 rsnElementGetSubElements(mContext, id, IDs, names);
179 }
Jason Sams768bc022009-09-21 19:41:04 -0700180
Jason Sams3b9c52a2010-10-14 17:48:46 -0700181 native int rsnTypeCreate(int con, int eid, int[] dims, int[] vals);
182 synchronized int nTypeCreate(int eid, int[] dims, int[] vals) {
183 return rsnTypeCreate(mContext, eid, dims, vals);
Jason Sams2e1872f2010-08-17 16:25:41 -0700184 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700185 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
186 synchronized void nTypeGetNativeData(int id, int[] typeData) {
187 rsnTypeGetNativeData(mContext, id, typeData);
188 }
Jason Sams768bc022009-09-21 19:41:04 -0700189
Jason Sams2e1872f2010-08-17 16:25:41 -0700190 native int rsnAllocationCreateTyped(int con, int type);
191 synchronized int nAllocationCreateTyped(int type) {
192 return rsnAllocationCreateTyped(mContext, type);
193 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700194 native void rsnAllocationUpdateFromBitmap(int con, int alloc, Bitmap bmp);
195 synchronized void nAllocationUpdateFromBitmap(int alloc, Bitmap bmp) {
196 rsnAllocationUpdateFromBitmap(mContext, alloc, bmp);
197 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700198 native int rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
199 synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
200 return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
201 }
202 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
203 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
204 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
205 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700206 native int rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
207 synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
208 return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
209 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700210
Jason Sams2e1872f2010-08-17 16:25:41 -0700211 native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
212 synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) {
213 rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel);
214 }
215 native void rsnAllocationUploadToBufferObject(int con, int alloc);
216 synchronized void nAllocationUploadToBufferObject(int alloc) {
217 rsnAllocationUploadToBufferObject(mContext, alloc);
218 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700219
Jason Sams2e1872f2010-08-17 16:25:41 -0700220 native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes);
221 synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) {
222 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
223 }
224 native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes);
225 synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) {
226 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
227 }
228 native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes);
229 synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) {
230 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
231 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700232 native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
233 synchronized void nAllocationSubElementData1D(int id, int xoff, int compIdx, byte[] d, int sizeBytes) {
234 rsnAllocationSubElementData1D(mContext, id, xoff, compIdx, d, sizeBytes);
235 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700236 native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes);
237 synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) {
238 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
239 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700240
Jason Sams2e1872f2010-08-17 16:25:41 -0700241 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
242 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) {
243 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
244 }
245 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
246 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) {
247 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
248 }
249 native void rsnAllocationRead(int con, int id, int[] d);
250 synchronized void nAllocationRead(int id, int[] d) {
251 rsnAllocationRead(mContext, id, d);
252 }
253 native void rsnAllocationRead(int con, int id, float[] d);
254 synchronized void nAllocationRead(int id, float[] d) {
255 rsnAllocationRead(mContext, id, d);
256 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700257 native int rsnAllocationGetType(int con, int id);
258 synchronized int nAllocationGetType(int id) {
259 return rsnAllocationGetType(mContext, id);
260 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700261
Jason Sams5edc6082010-10-05 13:32:49 -0700262 native void rsnAllocationResize1D(int con, int id, int dimX);
263 synchronized void nAllocationResize1D(int id, int dimX) {
264 rsnAllocationResize1D(mContext, id, dimX);
265 }
266 native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
267 synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
268 rsnAllocationResize2D(mContext, id, dimX, dimY);
269 }
270
Jason Sams2e1872f2010-08-17 16:25:41 -0700271 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
272 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
273 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
274 }
275 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
276 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
277 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
278 }
279 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
280 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
281 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
282 }
283 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
284 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
285 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
286 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700287
Jason Sams2e1872f2010-08-17 16:25:41 -0700288 native int rsnFontCreateFromFile(int con, String fileName, int size, int dpi);
289 synchronized int nFontCreateFromFile(String fileName, int size, int dpi) {
290 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
291 }
Jason Sams22534172009-08-04 16:58:20 -0700292
Jason Sams2e1872f2010-08-17 16:25:41 -0700293 native void rsnAdapter1DBindAllocation(int con, int ad, int alloc);
294 synchronized void nAdapter1DBindAllocation(int ad, int alloc) {
295 rsnAdapter1DBindAllocation(mContext, ad, alloc);
296 }
297 native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value);
298 synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) {
299 rsnAdapter1DSetConstraint(mContext, ad, dim, value);
300 }
301 native void rsnAdapter1DData(int con, int ad, int[] d);
302 synchronized void nAdapter1DData(int ad, int[] d) {
303 rsnAdapter1DData(mContext, ad, d);
304 }
305 native void rsnAdapter1DData(int con, int ad, float[] d);
306 synchronized void nAdapter1DData(int ad, float[] d) {
307 rsnAdapter1DData(mContext, ad, d);
308 }
309 native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d);
310 synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) {
311 rsnAdapter1DSubData(mContext, ad, off, count, d);
312 }
313 native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d);
314 synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) {
315 rsnAdapter1DSubData(mContext, ad, off, count, d);
316 }
317 native int rsnAdapter1DCreate(int con);
318 synchronized int nAdapter1DCreate() {
319 return rsnAdapter1DCreate(mContext);
320 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700321
Jason Sams2e1872f2010-08-17 16:25:41 -0700322 native void rsnAdapter2DBindAllocation(int con, int ad, int alloc);
323 synchronized void nAdapter2DBindAllocation(int ad, int alloc) {
324 rsnAdapter2DBindAllocation(mContext, ad, alloc);
325 }
326 native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value);
327 synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) {
328 rsnAdapter2DSetConstraint(mContext, ad, dim, value);
329 }
330 native void rsnAdapter2DData(int con, int ad, int[] d);
331 synchronized void nAdapter2DData(int ad, int[] d) {
332 rsnAdapter2DData(mContext, ad, d);
333 }
334 native void rsnAdapter2DData(int con, int ad, float[] d);
335 synchronized void nAdapter2DData(int ad, float[] d) {
336 rsnAdapter2DData(mContext, ad, d);
337 }
338 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d);
339 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) {
340 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
341 }
342 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d);
343 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) {
344 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
345 }
346 native int rsnAdapter2DCreate(int con);
347 synchronized int nAdapter2DCreate() {
348 return rsnAdapter2DCreate(mContext);
349 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700350
Jason Sams2e1872f2010-08-17 16:25:41 -0700351 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
352 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
353 rsnScriptBindAllocation(mContext, script, alloc, slot);
354 }
355 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
356 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
357 rsnScriptSetTimeZone(mContext, script, timeZone);
358 }
359 native void rsnScriptInvoke(int con, int id, int slot);
360 synchronized void nScriptInvoke(int id, int slot) {
361 rsnScriptInvoke(mContext, id, slot);
362 }
363 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
364 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
365 rsnScriptInvokeV(mContext, id, slot, params);
366 }
367 native void rsnScriptSetVarI(int con, int id, int slot, int val);
368 synchronized void nScriptSetVarI(int id, int slot, int val) {
369 rsnScriptSetVarI(mContext, id, slot, val);
370 }
Stephen Hines031ec58c2010-10-11 10:54:21 -0700371 native void rsnScriptSetVarJ(int con, int id, int slot, long val);
372 synchronized void nScriptSetVarJ(int id, int slot, long val) {
373 rsnScriptSetVarJ(mContext, id, slot, val);
374 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700375 native void rsnScriptSetVarF(int con, int id, int slot, float val);
376 synchronized void nScriptSetVarF(int id, int slot, float val) {
377 rsnScriptSetVarF(mContext, id, slot, val);
378 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700379 native void rsnScriptSetVarD(int con, int id, int slot, double val);
380 synchronized void nScriptSetVarD(int id, int slot, double val) {
381 rsnScriptSetVarD(mContext, id, slot, val);
382 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700383 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
384 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
385 rsnScriptSetVarV(mContext, id, slot, val);
386 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700387
Jason Sams2e1872f2010-08-17 16:25:41 -0700388 native void rsnScriptCBegin(int con);
389 synchronized void nScriptCBegin() {
390 rsnScriptCBegin(mContext);
391 }
392 native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
393 synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
394 rsnScriptCSetScript(mContext, script, offset, length);
395 }
396 native int rsnScriptCCreate(int con);
397 synchronized int nScriptCCreate() {
398 return rsnScriptCCreate(mContext);
399 }
Jason Samsebfb4362009-09-23 13:57:02 -0700400
Jason Sams2e1872f2010-08-17 16:25:41 -0700401 native void rsnSamplerBegin(int con);
402 synchronized void nSamplerBegin() {
403 rsnSamplerBegin(mContext);
404 }
405 native void rsnSamplerSet(int con, int param, int value);
406 synchronized void nSamplerSet(int param, int value) {
407 rsnSamplerSet(mContext, param, value);
408 }
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700409 native void rsnSamplerSet2(int con, int param, float value);
410 synchronized void nSamplerSet2(int param, float value) {
411 rsnSamplerSet2(mContext, param, value);
412 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700413 native int rsnSamplerCreate(int con);
414 synchronized int nSamplerCreate() {
415 return rsnSamplerCreate(mContext);
416 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800417
Jason Sams2e1872f2010-08-17 16:25:41 -0700418 native void rsnProgramStoreBegin(int con, int in, int out);
419 synchronized void nProgramStoreBegin(int in, int out) {
420 rsnProgramStoreBegin(mContext, in, out);
421 }
422 native void rsnProgramStoreDepthFunc(int con, int func);
423 synchronized void nProgramStoreDepthFunc(int func) {
424 rsnProgramStoreDepthFunc(mContext, func);
425 }
426 native void rsnProgramStoreDepthMask(int con, boolean enable);
427 synchronized void nProgramStoreDepthMask(boolean enable) {
428 rsnProgramStoreDepthMask(mContext, enable);
429 }
430 native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
431 synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
432 rsnProgramStoreColorMask(mContext, r, g, b, a);
433 }
434 native void rsnProgramStoreBlendFunc(int con, int src, int dst);
435 synchronized void nProgramStoreBlendFunc(int src, int dst) {
436 rsnProgramStoreBlendFunc(mContext, src, dst);
437 }
438 native void rsnProgramStoreDither(int con, boolean enable);
439 synchronized void nProgramStoreDither(boolean enable) {
440 rsnProgramStoreDither(mContext, enable);
441 }
442 native int rsnProgramStoreCreate(int con);
443 synchronized int nProgramStoreCreate() {
444 return rsnProgramStoreCreate(mContext);
445 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700446
Jason Sams2e1872f2010-08-17 16:25:41 -0700447 native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
448 synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
449 return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
450 }
451 native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
452 synchronized void nProgramRasterSetLineWidth(int pr, float v) {
453 rsnProgramRasterSetLineWidth(mContext, pr, v);
454 }
455 native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
456 synchronized void nProgramRasterSetCullMode(int pr, int mode) {
457 rsnProgramRasterSetCullMode(mContext, pr, mode);
458 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700459
Jason Sams2e1872f2010-08-17 16:25:41 -0700460 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
461 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
462 rsnProgramBindConstants(mContext, pv, slot, mID);
463 }
464 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
465 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
466 rsnProgramBindTexture(mContext, vpf, slot, a);
467 }
468 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
469 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
470 rsnProgramBindSampler(mContext, vpf, slot, s);
471 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700472 native int rsnProgramFragmentCreate(int con, String shader, int[] params);
473 synchronized int nProgramFragmentCreate(String shader, int[] params) {
474 return rsnProgramFragmentCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700475 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700476 native int rsnProgramVertexCreate(int con, String shader, int[] params);
477 synchronized int nProgramVertexCreate(String shader, int[] params) {
478 return rsnProgramVertexCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700479 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700480
Jason Sams2e1872f2010-08-17 16:25:41 -0700481 native int rsnMeshCreate(int con, int vtxCount, int indexCount);
482 synchronized int nMeshCreate(int vtxCount, int indexCount) {
483 return rsnMeshCreate(mContext, vtxCount, indexCount);
484 }
485 native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
486 synchronized void nMeshBindVertex(int id, int alloc, int slot) {
487 rsnMeshBindVertex(mContext, id, alloc, slot);
488 }
489 native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
490 synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
491 rsnMeshBindIndex(mContext, id, alloc, prim, slot);
492 }
493 native int rsnMeshGetVertexBufferCount(int con, int id);
494 synchronized int nMeshGetVertexBufferCount(int id) {
495 return rsnMeshGetVertexBufferCount(mContext, id);
496 }
497 native int rsnMeshGetIndexCount(int con, int id);
498 synchronized int nMeshGetIndexCount(int id) {
499 return rsnMeshGetIndexCount(mContext, id);
500 }
501 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
502 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
503 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
504 }
505 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
506 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
507 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
508 }
509
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700510
Jason Sams704ff642010-02-09 16:05:07 -0800511 protected int mDev;
512 protected int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700513 @SuppressWarnings({"FieldCanBeLocal"})
Jason Sams704ff642010-02-09 16:05:07 -0800514 protected MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700515
Jason Sams8cb39de2010-06-01 15:47:01 -0700516 Element mElement_U8;
517 Element mElement_I8;
518 Element mElement_U16;
519 Element mElement_I16;
520 Element mElement_U32;
521 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -0700522 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -0700523 Element mElement_I64;
Jason Sams8cb39de2010-06-01 15:47:01 -0700524 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700525 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700526 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700527
Jason Sams8cb39de2010-06-01 15:47:01 -0700528 Element mElement_ELEMENT;
529 Element mElement_TYPE;
530 Element mElement_ALLOCATION;
531 Element mElement_SAMPLER;
532 Element mElement_SCRIPT;
533 Element mElement_MESH;
534 Element mElement_PROGRAM_FRAGMENT;
535 Element mElement_PROGRAM_VERTEX;
536 Element mElement_PROGRAM_RASTER;
537 Element mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700538
Jason Sams3c0dfba2009-09-27 17:50:38 -0700539 Element mElement_A_8;
540 Element mElement_RGB_565;
541 Element mElement_RGB_888;
542 Element mElement_RGBA_5551;
543 Element mElement_RGBA_4444;
544 Element mElement_RGBA_8888;
545
Jason Sams8cb39de2010-06-01 15:47:01 -0700546 Element mElement_FLOAT_2;
547 Element mElement_FLOAT_3;
548 Element mElement_FLOAT_4;
549 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800550
Jason Sams1d45c472010-08-25 14:31:48 -0700551 Element mElement_MATRIX_4X4;
552 Element mElement_MATRIX_3X3;
553 Element mElement_MATRIX_2X2;
554
Jason Sams4d339932010-05-11 14:03:58 -0700555 Sampler mSampler_CLAMP_NEAREST;
556 Sampler mSampler_CLAMP_LINEAR;
557 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
558 Sampler mSampler_WRAP_NEAREST;
559 Sampler mSampler_WRAP_LINEAR;
560 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
561
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700562 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
563 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
564 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_TEST;
565 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_WRITE;
566 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
567 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
568 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_TEST;
569 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_WRITE;
570 ProgramStore mProgramStore_BLEND_ADD_DEPTH_TEST;
571 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_DEPTH;
572 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_TEST;
573 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_WRITE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700574
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700575 ProgramRaster mProgramRaster_CULL_BACK;
576 ProgramRaster mProgramRaster_CULL_FRONT;
577 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700578
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700579 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700580 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700581
Jason Sams516c3192009-10-06 13:58:47 -0700582 public static class RSMessage implements Runnable {
583 protected int[] mData;
584 protected int mID;
585 public void run() {
586 }
587 }
588 public RSMessage mMessageCallback = null;
589
Jason Sams7d787b42009-11-15 12:14:26 -0800590 public enum Priority {
591 LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5
592 NORMAL (-4); //ANDROID_PRIORITY_DISPLAY
593
594 int mID;
595 Priority(int id) {
596 mID = id;
597 }
598 }
599
Jason Sams771bebb2009-12-07 12:40:12 -0800600 void validate() {
601 if (mContext == 0) {
602 throw new IllegalStateException("Calling RS with no Context active.");
603 }
604 }
605
Jason Sams7d787b42009-11-15 12:14:26 -0800606 public void contextSetPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800607 validate();
Jason Sams7d787b42009-11-15 12:14:26 -0800608 nContextSetPriority(p.mID);
609 }
610
Jason Sams704ff642010-02-09 16:05:07 -0800611 protected static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -0700612 RenderScript mRS;
613 boolean mRun = true;
614
615 MessageThread(RenderScript rs) {
616 super("RSMessageThread");
617 mRS = rs;
618
619 }
620
621 public void run() {
622 // This function is a temporary solution. The final solution will
623 // used typed allocations where the message id is the type indicator.
624 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -0700625 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700626 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -0700627 rbuf[0] = 0;
Jason Sams2e1872f2010-08-17 16:25:41 -0700628 int msg = mRS.nContextGetMessage(mRS.mContext, rbuf, true);
Stephen Hinesab98bb62010-09-24 14:38:30 -0700629 if ((msg == 0)) {
Jason Sams1d45c472010-08-25 14:31:48 -0700630 // Can happen for two reasons
Stephen Hinesab98bb62010-09-24 14:38:30 -0700631 if (rbuf[0] > 0 && mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -0700632 // 1: Buffer needs to be enlarged.
633 rbuf = new int[rbuf[0] + 2];
634 } else {
635 // 2: teardown.
636 // But we want to avoid starving other threads during
637 // teardown by yielding until the next line in the destructor
638 // can execute to set mRun = false
639 try {
640 sleep(1, 0);
641 } catch(InterruptedException e) {
642 }
Jason Sams516c3192009-10-06 13:58:47 -0700643 }
Stephen Hinesab98bb62010-09-24 14:38:30 -0700644 continue;
Jason Sams516c3192009-10-06 13:58:47 -0700645 }
646 if(mRS.mMessageCallback != null) {
647 mRS.mMessageCallback.mData = rbuf;
648 mRS.mMessageCallback.mID = msg;
649 mRS.mMessageCallback.run();
650 }
Jason Sams516c3192009-10-06 13:58:47 -0700651 }
Jason Sams3bc47d42009-11-12 15:10:25 -0800652 Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -0700653 }
654 }
655
Jason Sams704ff642010-02-09 16:05:07 -0800656 protected RenderScript() {
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700657 }
658
Jason Sams704ff642010-02-09 16:05:07 -0800659 public static RenderScript create() {
660 RenderScript rs = new RenderScript();
661
662 rs.mDev = rs.nDeviceCreate();
663 rs.mContext = rs.nContextCreate(rs.mDev, 0);
664 rs.mMessageThread = new MessageThread(rs);
665 rs.mMessageThread.start();
666 Element.initPredefined(rs);
667 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800668 }
669
Jason Sams715333b2009-11-17 17:26:46 -0800670 public void contextDump(int bits) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800671 validate();
Jason Sams715333b2009-11-17 17:26:46 -0800672 nContextDump(bits);
673 }
674
Jason Sams96ed4cf2010-06-15 12:15:57 -0700675 public void finish() {
676 nContextFinish();
677 }
678
Jason Samsf5b45962009-08-25 14:49:07 -0700679 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -0800680 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700681 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700682 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -0700683 try {
684 mMessageThread.join();
685 } catch(InterruptedException e) {
686 }
Jason Sams516c3192009-10-06 13:58:47 -0700687
Jason Sams2e1872f2010-08-17 16:25:41 -0700688 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -0700689 mContext = 0;
690
691 nDeviceDestroy(mDev);
692 mDev = 0;
693 }
Jason Sams02fb2cb2009-05-28 15:37:57 -0700694
Jason Samsa9e7a052009-09-25 14:51:22 -0700695 boolean isAlive() {
696 return mContext != 0;
697 }
698
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700699 ///////////////////////////////////////////////////////////////////////////////////
700 // Root state
701
Jason Sams704ff642010-02-09 16:05:07 -0800702 protected int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -0700703 if(o != null) {
704 return o.mID;
Jason Samsd8e41612009-08-20 17:22:40 -0700705 }
Jason Sams6b9dec02009-09-23 16:38:37 -0700706 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700707 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700708}
709
Jason Sams36e612a2009-07-31 16:26:13 -0700710
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700711