blob: 0c9a4ed2fd7fc4f9df715f9258df92efaac8fd17 [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 Sams3eaa3382009-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 Samsfe08d992009-05-27 14:45:32 -0700159
Jason Sams2e1872f2010-08-17 16:25:41 -0700160 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
161 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
162 return rsnElementCreate(mContext, type, kind, norm, vecSize);
163 }
Jason Sams70d4e502010-09-02 17:35:23 -0700164 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
165 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
166 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700167 }
168 native void rsnElementGetNativeData(int con, int id, int[] elementData);
169 synchronized void nElementGetNativeData(int id, int[] elementData) {
170 rsnElementGetNativeData(mContext, id, elementData);
171 }
172 native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names);
173 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) {
174 rsnElementGetSubElements(mContext, id, IDs, names);
175 }
Jason Sams768bc022009-09-21 19:41:04 -0700176
Jason Sams3b9c52a2010-10-14 17:48:46 -0700177 native int rsnTypeCreate(int con, int eid, int[] dims, int[] vals);
178 synchronized int nTypeCreate(int eid, int[] dims, int[] vals) {
179 return rsnTypeCreate(mContext, eid, dims, vals);
Jason Sams2e1872f2010-08-17 16:25:41 -0700180 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700181 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
182 synchronized void nTypeGetNativeData(int id, int[] typeData) {
183 rsnTypeGetNativeData(mContext, id, typeData);
184 }
Jason Sams768bc022009-09-21 19:41:04 -0700185
Jason Sams2e1872f2010-08-17 16:25:41 -0700186 native int rsnAllocationCreateTyped(int con, int type);
187 synchronized int nAllocationCreateTyped(int type) {
188 return rsnAllocationCreateTyped(mContext, type);
189 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700190 native void rsnAllocationUpdateFromBitmap(int con, int alloc, Bitmap bmp);
191 synchronized void nAllocationUpdateFromBitmap(int alloc, Bitmap bmp) {
192 rsnAllocationUpdateFromBitmap(mContext, alloc, bmp);
193 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700194 native int rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
195 synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
196 return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
197 }
198 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
199 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
200 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
201 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700202 native int rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
203 synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
204 return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
205 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700206
Jason Sams2e1872f2010-08-17 16:25:41 -0700207 native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
208 synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) {
209 rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel);
210 }
211 native void rsnAllocationUploadToBufferObject(int con, int alloc);
212 synchronized void nAllocationUploadToBufferObject(int alloc) {
213 rsnAllocationUploadToBufferObject(mContext, alloc);
214 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700215
Jason Sams2e1872f2010-08-17 16:25:41 -0700216 native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes);
217 synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) {
218 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
219 }
220 native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes);
221 synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) {
222 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
223 }
224 native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes);
225 synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) {
226 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
227 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700228 native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
229 synchronized void nAllocationSubElementData1D(int id, int xoff, int compIdx, byte[] d, int sizeBytes) {
230 rsnAllocationSubElementData1D(mContext, id, xoff, compIdx, d, sizeBytes);
231 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700232 native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes);
233 synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) {
234 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
235 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700236
Jason Sams2e1872f2010-08-17 16:25:41 -0700237 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
238 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) {
239 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
240 }
241 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
242 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) {
243 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
244 }
245 native void rsnAllocationRead(int con, int id, int[] d);
246 synchronized void nAllocationRead(int id, int[] d) {
247 rsnAllocationRead(mContext, id, d);
248 }
249 native void rsnAllocationRead(int con, int id, float[] d);
250 synchronized void nAllocationRead(int id, float[] d) {
251 rsnAllocationRead(mContext, id, d);
252 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700253 native int rsnAllocationGetType(int con, int id);
254 synchronized int nAllocationGetType(int id) {
255 return rsnAllocationGetType(mContext, id);
256 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700257
Jason Sams5edc6082010-10-05 13:32:49 -0700258 native void rsnAllocationResize1D(int con, int id, int dimX);
259 synchronized void nAllocationResize1D(int id, int dimX) {
260 rsnAllocationResize1D(mContext, id, dimX);
261 }
262 native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
263 synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
264 rsnAllocationResize2D(mContext, id, dimX, dimY);
265 }
266
Jason Sams2e1872f2010-08-17 16:25:41 -0700267 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
268 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
269 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
270 }
271 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
272 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
273 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
274 }
275 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
276 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
277 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
278 }
279 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
280 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
281 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
282 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700283
Jason Sams2e1872f2010-08-17 16:25:41 -0700284 native int rsnFontCreateFromFile(int con, String fileName, int size, int dpi);
285 synchronized int nFontCreateFromFile(String fileName, int size, int dpi) {
286 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
287 }
Jason Sams22534172009-08-04 16:58:20 -0700288
Jason Sams2e1872f2010-08-17 16:25:41 -0700289 native void rsnAdapter1DBindAllocation(int con, int ad, int alloc);
290 synchronized void nAdapter1DBindAllocation(int ad, int alloc) {
291 rsnAdapter1DBindAllocation(mContext, ad, alloc);
292 }
293 native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value);
294 synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) {
295 rsnAdapter1DSetConstraint(mContext, ad, dim, value);
296 }
297 native void rsnAdapter1DData(int con, int ad, int[] d);
298 synchronized void nAdapter1DData(int ad, int[] d) {
299 rsnAdapter1DData(mContext, ad, d);
300 }
301 native void rsnAdapter1DData(int con, int ad, float[] d);
302 synchronized void nAdapter1DData(int ad, float[] d) {
303 rsnAdapter1DData(mContext, ad, d);
304 }
305 native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d);
306 synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) {
307 rsnAdapter1DSubData(mContext, ad, off, count, d);
308 }
309 native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d);
310 synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) {
311 rsnAdapter1DSubData(mContext, ad, off, count, d);
312 }
313 native int rsnAdapter1DCreate(int con);
314 synchronized int nAdapter1DCreate() {
315 return rsnAdapter1DCreate(mContext);
316 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700317
Jason Sams2e1872f2010-08-17 16:25:41 -0700318 native void rsnAdapter2DBindAllocation(int con, int ad, int alloc);
319 synchronized void nAdapter2DBindAllocation(int ad, int alloc) {
320 rsnAdapter2DBindAllocation(mContext, ad, alloc);
321 }
322 native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value);
323 synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) {
324 rsnAdapter2DSetConstraint(mContext, ad, dim, value);
325 }
326 native void rsnAdapter2DData(int con, int ad, int[] d);
327 synchronized void nAdapter2DData(int ad, int[] d) {
328 rsnAdapter2DData(mContext, ad, d);
329 }
330 native void rsnAdapter2DData(int con, int ad, float[] d);
331 synchronized void nAdapter2DData(int ad, float[] d) {
332 rsnAdapter2DData(mContext, ad, d);
333 }
334 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d);
335 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) {
336 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
337 }
338 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d);
339 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) {
340 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
341 }
342 native int rsnAdapter2DCreate(int con);
343 synchronized int nAdapter2DCreate() {
344 return rsnAdapter2DCreate(mContext);
345 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700346
Jason Sams2e1872f2010-08-17 16:25:41 -0700347 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
348 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
349 rsnScriptBindAllocation(mContext, script, alloc, slot);
350 }
351 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
352 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
353 rsnScriptSetTimeZone(mContext, script, timeZone);
354 }
355 native void rsnScriptInvoke(int con, int id, int slot);
356 synchronized void nScriptInvoke(int id, int slot) {
357 rsnScriptInvoke(mContext, id, slot);
358 }
359 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
360 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
361 rsnScriptInvokeV(mContext, id, slot, params);
362 }
363 native void rsnScriptSetVarI(int con, int id, int slot, int val);
364 synchronized void nScriptSetVarI(int id, int slot, int val) {
365 rsnScriptSetVarI(mContext, id, slot, val);
366 }
Stephen Hines031ec58c2010-10-11 10:54:21 -0700367 native void rsnScriptSetVarJ(int con, int id, int slot, long val);
368 synchronized void nScriptSetVarJ(int id, int slot, long val) {
369 rsnScriptSetVarJ(mContext, id, slot, val);
370 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700371 native void rsnScriptSetVarF(int con, int id, int slot, float val);
372 synchronized void nScriptSetVarF(int id, int slot, float val) {
373 rsnScriptSetVarF(mContext, id, slot, val);
374 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700375 native void rsnScriptSetVarD(int con, int id, int slot, double val);
376 synchronized void nScriptSetVarD(int id, int slot, double val) {
377 rsnScriptSetVarD(mContext, id, slot, val);
378 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700379 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
380 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
381 rsnScriptSetVarV(mContext, id, slot, val);
382 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700383
Jason Sams2e1872f2010-08-17 16:25:41 -0700384 native void rsnScriptCBegin(int con);
385 synchronized void nScriptCBegin() {
386 rsnScriptCBegin(mContext);
387 }
388 native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
389 synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
390 rsnScriptCSetScript(mContext, script, offset, length);
391 }
392 native int rsnScriptCCreate(int con);
393 synchronized int nScriptCCreate() {
394 return rsnScriptCCreate(mContext);
395 }
Jason Samsebfb4362009-09-23 13:57:02 -0700396
Jason Sams2e1872f2010-08-17 16:25:41 -0700397 native void rsnSamplerBegin(int con);
398 synchronized void nSamplerBegin() {
399 rsnSamplerBegin(mContext);
400 }
401 native void rsnSamplerSet(int con, int param, int value);
402 synchronized void nSamplerSet(int param, int value) {
403 rsnSamplerSet(mContext, param, value);
404 }
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700405 native void rsnSamplerSet2(int con, int param, float value);
406 synchronized void nSamplerSet2(int param, float value) {
407 rsnSamplerSet2(mContext, param, value);
408 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700409 native int rsnSamplerCreate(int con);
410 synchronized int nSamplerCreate() {
411 return rsnSamplerCreate(mContext);
412 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800413
Jason Sams2e1872f2010-08-17 16:25:41 -0700414 native void rsnProgramStoreBegin(int con, int in, int out);
415 synchronized void nProgramStoreBegin(int in, int out) {
416 rsnProgramStoreBegin(mContext, in, out);
417 }
418 native void rsnProgramStoreDepthFunc(int con, int func);
419 synchronized void nProgramStoreDepthFunc(int func) {
420 rsnProgramStoreDepthFunc(mContext, func);
421 }
422 native void rsnProgramStoreDepthMask(int con, boolean enable);
423 synchronized void nProgramStoreDepthMask(boolean enable) {
424 rsnProgramStoreDepthMask(mContext, enable);
425 }
426 native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
427 synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
428 rsnProgramStoreColorMask(mContext, r, g, b, a);
429 }
430 native void rsnProgramStoreBlendFunc(int con, int src, int dst);
431 synchronized void nProgramStoreBlendFunc(int src, int dst) {
432 rsnProgramStoreBlendFunc(mContext, src, dst);
433 }
434 native void rsnProgramStoreDither(int con, boolean enable);
435 synchronized void nProgramStoreDither(boolean enable) {
436 rsnProgramStoreDither(mContext, enable);
437 }
438 native int rsnProgramStoreCreate(int con);
439 synchronized int nProgramStoreCreate() {
440 return rsnProgramStoreCreate(mContext);
441 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700442
Jason Sams2e1872f2010-08-17 16:25:41 -0700443 native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
444 synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
445 return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
446 }
447 native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
448 synchronized void nProgramRasterSetLineWidth(int pr, float v) {
449 rsnProgramRasterSetLineWidth(mContext, pr, v);
450 }
451 native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
452 synchronized void nProgramRasterSetCullMode(int pr, int mode) {
453 rsnProgramRasterSetCullMode(mContext, pr, mode);
454 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700455
Jason Sams2e1872f2010-08-17 16:25:41 -0700456 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
457 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
458 rsnProgramBindConstants(mContext, pv, slot, mID);
459 }
460 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
461 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
462 rsnProgramBindTexture(mContext, vpf, slot, a);
463 }
464 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
465 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
466 rsnProgramBindSampler(mContext, vpf, slot, s);
467 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700468 native int rsnProgramFragmentCreate(int con, String shader, int[] params);
469 synchronized int nProgramFragmentCreate(String shader, int[] params) {
470 return rsnProgramFragmentCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700471 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700472 native int rsnProgramVertexCreate(int con, String shader, int[] params);
473 synchronized int nProgramVertexCreate(String shader, int[] params) {
474 return rsnProgramVertexCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700475 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700476
Jason Sams2e1872f2010-08-17 16:25:41 -0700477 native int rsnMeshCreate(int con, int vtxCount, int indexCount);
478 synchronized int nMeshCreate(int vtxCount, int indexCount) {
479 return rsnMeshCreate(mContext, vtxCount, indexCount);
480 }
481 native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
482 synchronized void nMeshBindVertex(int id, int alloc, int slot) {
483 rsnMeshBindVertex(mContext, id, alloc, slot);
484 }
485 native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
486 synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
487 rsnMeshBindIndex(mContext, id, alloc, prim, slot);
488 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800489 native void rsnMeshInitVertexAttribs(int con, int id);
490 synchronized void nMeshInitVertexAttribs(int id) {
491 rsnMeshInitVertexAttribs(mContext, id);
492 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700493 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) {
Jason Samsc1d62102010-11-04 14:32:19 -0700602 throw new RSInvalidStateException("Calling RS with no Context active.");
Jason Sams771bebb2009-12-07 12:40:12 -0800603 }
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