blob: e42f9f66ee9fc28f32325d40359ff86cd9a40b2b [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.
72 native int rsnContextCreateGL(int dev, int ver, boolean useDepth);
73 synchronized int nContextCreateGL(int dev, int ver, boolean useDepth) {
74 return rsnContextCreateGL(dev, ver, useDepth);
75 }
76 native int rsnContextCreate(int dev, int ver);
77 synchronized int nContextCreate(int dev, int ver) {
78 return rsnContextCreate(dev, ver);
79 }
80 native void rsnContextDestroy(int con);
81 synchronized void nContextDestroy() {
82 rsnContextDestroy(mContext);
83 }
84 native void rsnContextSetSurface(int con, int w, int h, Surface sur);
85 synchronized void nContextSetSurface(int w, int h, Surface sur) {
86 rsnContextSetSurface(mContext, w, h, sur);
87 }
88 native void rsnContextSetPriority(int con, int p);
89 synchronized void nContextSetPriority(int p) {
90 rsnContextSetPriority(mContext, p);
91 }
92 native void rsnContextDump(int con, int bits);
93 synchronized void nContextDump(int bits) {
94 rsnContextDump(mContext, bits);
95 }
96 native void rsnContextFinish(int con);
97 synchronized void nContextFinish() {
98 rsnContextFinish(mContext);
99 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700100
Jason Sams2e1872f2010-08-17 16:25:41 -0700101 native void rsnContextBindRootScript(int con, int script);
102 synchronized void nContextBindRootScript(int script) {
103 rsnContextBindRootScript(mContext, script);
104 }
105 native void rsnContextBindSampler(int con, int sampler, int slot);
106 synchronized void nContextBindSampler(int sampler, int slot) {
107 rsnContextBindSampler(mContext, sampler, slot);
108 }
109 native void rsnContextBindProgramStore(int con, int pfs);
110 synchronized void nContextBindProgramStore(int pfs) {
111 rsnContextBindProgramStore(mContext, pfs);
112 }
113 native void rsnContextBindProgramFragment(int con, int pf);
114 synchronized void nContextBindProgramFragment(int pf) {
115 rsnContextBindProgramFragment(mContext, pf);
116 }
117 native void rsnContextBindProgramVertex(int con, int pv);
118 synchronized void nContextBindProgramVertex(int pv) {
119 rsnContextBindProgramVertex(mContext, pv);
120 }
121 native void rsnContextBindProgramRaster(int con, int pr);
122 synchronized void nContextBindProgramRaster(int pr) {
123 rsnContextBindProgramRaster(mContext, pr);
124 }
125 native void rsnContextPause(int con);
126 synchronized void nContextPause() {
127 rsnContextPause(mContext);
128 }
129 native void rsnContextResume(int con);
130 synchronized void nContextResume() {
131 rsnContextResume(mContext);
132 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700133
Jason Sams2e1872f2010-08-17 16:25:41 -0700134 native void rsnAssignName(int con, int obj, byte[] name);
135 synchronized void nAssignName(int obj, byte[] name) {
136 rsnAssignName(mContext, obj, name);
137 }
138 native String rsnGetName(int con, int obj);
139 synchronized String nGetName(int obj) {
140 return rsnGetName(mContext, obj);
141 }
142 native void rsnObjDestroy(int con, int id);
143 synchronized void nObjDestroy(int id) {
144 rsnObjDestroy(mContext, id);
145 }
146 native void rsnObjDestroyOOB(int con, int id);
147 synchronized void nObjDestroyOOB(int id) {
148 rsnObjDestroyOOB(mContext, id);
149 }
150 native int rsnFileOpen(int con, byte[] name);
151 synchronized int nFileOpen(byte[] name) {
152 return rsnFileOpen(mContext, name);
153 }
Jason Samsfe08d992009-05-27 14:45:32 -0700154
Jason Sams2e1872f2010-08-17 16:25:41 -0700155 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
156 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
157 return rsnElementCreate(mContext, type, kind, norm, vecSize);
158 }
159 native int rsnElementCreate2(int con, int[] elements, String[] names);
160 synchronized int nElementCreate2(int[] elements, String[] names) {
161 return rsnElementCreate2(mContext, elements, names);
162 }
163 native void rsnElementGetNativeData(int con, int id, int[] elementData);
164 synchronized void nElementGetNativeData(int id, int[] elementData) {
165 rsnElementGetNativeData(mContext, id, elementData);
166 }
167 native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names);
168 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) {
169 rsnElementGetSubElements(mContext, id, IDs, names);
170 }
Jason Sams768bc022009-09-21 19:41:04 -0700171
Jason Sams2e1872f2010-08-17 16:25:41 -0700172 native void rsnTypeBegin(int con, int elementID);
173 synchronized void nTypeBegin(int elementID) {
174 rsnTypeBegin(mContext, elementID);
175 }
176 native void rsnTypeAdd(int con, int dim, int val);
177 synchronized void nTypeAdd(int dim, int val) {
178 rsnTypeAdd(mContext, dim, val);
179 }
180 native int rsnTypeCreate(int con);
181 synchronized int nTypeCreate() {
182 return rsnTypeCreate(mContext);
183 }
184 native void rsnTypeFinalDestroy(int con, Type t);
185 synchronized void nTypeFinalDestroy(Type t) {
186 rsnTypeFinalDestroy(mContext, t);
187 }
188 native void rsnTypeSetupFields(int con, Type t, int[] types, int[] bits, Field[] IDs);
189 synchronized void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs) {
190 rsnTypeSetupFields(mContext, t, types, bits, IDs);
191 }
192 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
193 synchronized void nTypeGetNativeData(int id, int[] typeData) {
194 rsnTypeGetNativeData(mContext, id, typeData);
195 }
Jason Sams768bc022009-09-21 19:41:04 -0700196
Jason Sams2e1872f2010-08-17 16:25:41 -0700197 native int rsnAllocationCreateTyped(int con, int type);
198 synchronized int nAllocationCreateTyped(int type) {
199 return rsnAllocationCreateTyped(mContext, type);
200 }
201 native int rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
202 synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
203 return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
204 }
205 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
206 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
207 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
208 }
209 native int rsnAllocationCreateFromBitmapBoxed(int con, int dstFmt, boolean genMips, Bitmap bmp);
210 synchronized int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp) {
211 return rsnAllocationCreateFromBitmapBoxed(mContext, dstFmt, genMips, bmp);
212 }
213 native int rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
214 synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
215 return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
216 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700217
Jason Sams2e1872f2010-08-17 16:25:41 -0700218 native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
219 synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) {
220 rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel);
221 }
222 native void rsnAllocationUploadToBufferObject(int con, int alloc);
223 synchronized void nAllocationUploadToBufferObject(int alloc) {
224 rsnAllocationUploadToBufferObject(mContext, alloc);
225 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700226
Jason Sams2e1872f2010-08-17 16:25:41 -0700227 native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes);
228 synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) {
229 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
230 }
231 native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes);
232 synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) {
233 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
234 }
235 native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes);
236 synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) {
237 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
238 }
239 native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes);
240 synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) {
241 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
242 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700243
Jason Sams2e1872f2010-08-17 16:25:41 -0700244 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
245 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) {
246 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
247 }
248 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
249 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) {
250 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
251 }
252 native void rsnAllocationRead(int con, int id, int[] d);
253 synchronized void nAllocationRead(int id, int[] d) {
254 rsnAllocationRead(mContext, id, d);
255 }
256 native void rsnAllocationRead(int con, int id, float[] d);
257 synchronized void nAllocationRead(int id, float[] d) {
258 rsnAllocationRead(mContext, id, d);
259 }
260 native void rsnAllocationSubDataFromObject(int con, int id, Type t, int offset, Object o);
261 synchronized void nAllocationSubDataFromObject(int id, Type t, int offset, Object o) {
262 rsnAllocationSubDataFromObject(mContext, id, t, offset, o);
263 }
264 native void rsnAllocationSubReadFromObject(int con, int id, Type t, int offset, Object o);
265 synchronized void nAllocationSubReadFromObject(int id, Type t, int offset, Object o) {
266 rsnAllocationSubReadFromObject(mContext, id, t, offset, o);
267 }
268 native int rsnAllocationGetType(int con, int id);
269 synchronized int nAllocationGetType(int id) {
270 return rsnAllocationGetType(mContext, id);
271 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700272
Jason Sams2e1872f2010-08-17 16:25:41 -0700273 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
274 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
275 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
276 }
277 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
278 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
279 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
280 }
281 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
282 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
283 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
284 }
285 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
286 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
287 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
288 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700289
Jason Sams2e1872f2010-08-17 16:25:41 -0700290 native int rsnFontCreateFromFile(int con, String fileName, int size, int dpi);
291 synchronized int nFontCreateFromFile(String fileName, int size, int dpi) {
292 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
293 }
Jason Sams22534172009-08-04 16:58:20 -0700294
Jason Sams2e1872f2010-08-17 16:25:41 -0700295 native void rsnAdapter1DBindAllocation(int con, int ad, int alloc);
296 synchronized void nAdapter1DBindAllocation(int ad, int alloc) {
297 rsnAdapter1DBindAllocation(mContext, ad, alloc);
298 }
299 native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value);
300 synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) {
301 rsnAdapter1DSetConstraint(mContext, ad, dim, value);
302 }
303 native void rsnAdapter1DData(int con, int ad, int[] d);
304 synchronized void nAdapter1DData(int ad, int[] d) {
305 rsnAdapter1DData(mContext, ad, d);
306 }
307 native void rsnAdapter1DData(int con, int ad, float[] d);
308 synchronized void nAdapter1DData(int ad, float[] d) {
309 rsnAdapter1DData(mContext, ad, d);
310 }
311 native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d);
312 synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) {
313 rsnAdapter1DSubData(mContext, ad, off, count, d);
314 }
315 native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d);
316 synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) {
317 rsnAdapter1DSubData(mContext, ad, off, count, d);
318 }
319 native int rsnAdapter1DCreate(int con);
320 synchronized int nAdapter1DCreate() {
321 return rsnAdapter1DCreate(mContext);
322 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700323
Jason Sams2e1872f2010-08-17 16:25:41 -0700324 native void rsnAdapter2DBindAllocation(int con, int ad, int alloc);
325 synchronized void nAdapter2DBindAllocation(int ad, int alloc) {
326 rsnAdapter2DBindAllocation(mContext, ad, alloc);
327 }
328 native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value);
329 synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) {
330 rsnAdapter2DSetConstraint(mContext, ad, dim, value);
331 }
332 native void rsnAdapter2DData(int con, int ad, int[] d);
333 synchronized void nAdapter2DData(int ad, int[] d) {
334 rsnAdapter2DData(mContext, ad, d);
335 }
336 native void rsnAdapter2DData(int con, int ad, float[] d);
337 synchronized void nAdapter2DData(int ad, float[] d) {
338 rsnAdapter2DData(mContext, ad, d);
339 }
340 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d);
341 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) {
342 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
343 }
344 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d);
345 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) {
346 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
347 }
348 native int rsnAdapter2DCreate(int con);
349 synchronized int nAdapter2DCreate() {
350 return rsnAdapter2DCreate(mContext);
351 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700352
Jason Sams2e1872f2010-08-17 16:25:41 -0700353 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
354 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
355 rsnScriptBindAllocation(mContext, script, alloc, slot);
356 }
357 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
358 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
359 rsnScriptSetTimeZone(mContext, script, timeZone);
360 }
361 native void rsnScriptInvoke(int con, int id, int slot);
362 synchronized void nScriptInvoke(int id, int slot) {
363 rsnScriptInvoke(mContext, id, slot);
364 }
365 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
366 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
367 rsnScriptInvokeV(mContext, id, slot, params);
368 }
369 native void rsnScriptSetVarI(int con, int id, int slot, int val);
370 synchronized void nScriptSetVarI(int id, int slot, int val) {
371 rsnScriptSetVarI(mContext, id, slot, val);
372 }
373 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 }
377 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
378 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
379 rsnScriptSetVarV(mContext, id, slot, val);
380 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700381
Jason Sams2e1872f2010-08-17 16:25:41 -0700382 native void rsnScriptCBegin(int con);
383 synchronized void nScriptCBegin() {
384 rsnScriptCBegin(mContext);
385 }
386 native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
387 synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
388 rsnScriptCSetScript(mContext, script, offset, length);
389 }
390 native int rsnScriptCCreate(int con);
391 synchronized int nScriptCCreate() {
392 return rsnScriptCCreate(mContext);
393 }
Jason Samsebfb4362009-09-23 13:57:02 -0700394
Jason Sams2e1872f2010-08-17 16:25:41 -0700395 native void rsnSamplerBegin(int con);
396 synchronized void nSamplerBegin() {
397 rsnSamplerBegin(mContext);
398 }
399 native void rsnSamplerSet(int con, int param, int value);
400 synchronized void nSamplerSet(int param, int value) {
401 rsnSamplerSet(mContext, param, value);
402 }
403 native int rsnSamplerCreate(int con);
404 synchronized int nSamplerCreate() {
405 return rsnSamplerCreate(mContext);
406 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800407
Jason Sams2e1872f2010-08-17 16:25:41 -0700408 native void rsnProgramStoreBegin(int con, int in, int out);
409 synchronized void nProgramStoreBegin(int in, int out) {
410 rsnProgramStoreBegin(mContext, in, out);
411 }
412 native void rsnProgramStoreDepthFunc(int con, int func);
413 synchronized void nProgramStoreDepthFunc(int func) {
414 rsnProgramStoreDepthFunc(mContext, func);
415 }
416 native void rsnProgramStoreDepthMask(int con, boolean enable);
417 synchronized void nProgramStoreDepthMask(boolean enable) {
418 rsnProgramStoreDepthMask(mContext, enable);
419 }
420 native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
421 synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
422 rsnProgramStoreColorMask(mContext, r, g, b, a);
423 }
424 native void rsnProgramStoreBlendFunc(int con, int src, int dst);
425 synchronized void nProgramStoreBlendFunc(int src, int dst) {
426 rsnProgramStoreBlendFunc(mContext, src, dst);
427 }
428 native void rsnProgramStoreDither(int con, boolean enable);
429 synchronized void nProgramStoreDither(boolean enable) {
430 rsnProgramStoreDither(mContext, enable);
431 }
432 native int rsnProgramStoreCreate(int con);
433 synchronized int nProgramStoreCreate() {
434 return rsnProgramStoreCreate(mContext);
435 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700436
Jason Sams2e1872f2010-08-17 16:25:41 -0700437 native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
438 synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
439 return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
440 }
441 native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
442 synchronized void nProgramRasterSetLineWidth(int pr, float v) {
443 rsnProgramRasterSetLineWidth(mContext, pr, v);
444 }
445 native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
446 synchronized void nProgramRasterSetCullMode(int pr, int mode) {
447 rsnProgramRasterSetCullMode(mContext, pr, mode);
448 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700449
Jason Sams2e1872f2010-08-17 16:25:41 -0700450 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
451 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
452 rsnProgramBindConstants(mContext, pv, slot, mID);
453 }
454 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
455 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
456 rsnProgramBindTexture(mContext, vpf, slot, a);
457 }
458 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
459 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
460 rsnProgramBindSampler(mContext, vpf, slot, s);
461 }
Jason Samsbba134c2009-06-22 15:49:21 -0700462
Jason Sams2e1872f2010-08-17 16:25:41 -0700463 native int rsnProgramFragmentCreate(int con, int[] params);
464 synchronized int nProgramFragmentCreate(int[] params) {
465 return rsnProgramFragmentCreate(mContext, params);
466 }
467 native int rsnProgramFragmentCreate2(int con, String shader, int[] params);
468 synchronized int nProgramFragmentCreate2(String shader, int[] params) {
469 return rsnProgramFragmentCreate2(mContext, shader, params);
470 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700471
Jason Sams2e1872f2010-08-17 16:25:41 -0700472 native int rsnProgramVertexCreate(int con, boolean texMat);
473 synchronized int nProgramVertexCreate(boolean texMat) {
474 return rsnProgramVertexCreate(mContext, texMat);
475 }
476 native int rsnProgramVertexCreate2(int con, String shader, int[] params);
477 synchronized int nProgramVertexCreate2(String shader, int[] params) {
478 return rsnProgramVertexCreate2(mContext, shader, params);
479 }
480
481
482 native int rsnMeshCreate(int con, int vtxCount, int indexCount);
483 synchronized int nMeshCreate(int vtxCount, int indexCount) {
484 return rsnMeshCreate(mContext, vtxCount, indexCount);
485 }
486 native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
487 synchronized void nMeshBindVertex(int id, int alloc, int slot) {
488 rsnMeshBindVertex(mContext, id, alloc, slot);
489 }
490 native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
491 synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
492 rsnMeshBindIndex(mContext, id, alloc, prim, slot);
493 }
494 native int rsnMeshGetVertexBufferCount(int con, int id);
495 synchronized int nMeshGetVertexBufferCount(int id) {
496 return rsnMeshGetVertexBufferCount(mContext, id);
497 }
498 native int rsnMeshGetIndexCount(int con, int id);
499 synchronized int nMeshGetIndexCount(int id) {
500 return rsnMeshGetIndexCount(mContext, id);
501 }
502 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
503 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
504 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
505 }
506 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
507 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
508 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
509 }
510
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700511
Jason Sams704ff642010-02-09 16:05:07 -0800512 protected int mDev;
513 protected int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700514 @SuppressWarnings({"FieldCanBeLocal"})
Jason Sams704ff642010-02-09 16:05:07 -0800515 protected MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700516
Jason Sams8cb39de2010-06-01 15:47:01 -0700517 Element mElement_U8;
518 Element mElement_I8;
519 Element mElement_U16;
520 Element mElement_I16;
521 Element mElement_U32;
522 Element mElement_I32;
523 Element mElement_F32;
Jason Samsf110d4b2010-06-21 17:42:41 -0700524 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700525
Jason Sams8cb39de2010-06-01 15:47:01 -0700526 Element mElement_ELEMENT;
527 Element mElement_TYPE;
528 Element mElement_ALLOCATION;
529 Element mElement_SAMPLER;
530 Element mElement_SCRIPT;
531 Element mElement_MESH;
532 Element mElement_PROGRAM_FRAGMENT;
533 Element mElement_PROGRAM_VERTEX;
534 Element mElement_PROGRAM_RASTER;
535 Element mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700536
Jason Sams3c0dfba2009-09-27 17:50:38 -0700537 Element mElement_A_8;
538 Element mElement_RGB_565;
539 Element mElement_RGB_888;
540 Element mElement_RGBA_5551;
541 Element mElement_RGBA_4444;
542 Element mElement_RGBA_8888;
543
Jason Sams8cb39de2010-06-01 15:47:01 -0700544 Element mElement_FLOAT_2;
545 Element mElement_FLOAT_3;
546 Element mElement_FLOAT_4;
547 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800548
Jason Sams4d339932010-05-11 14:03:58 -0700549 Sampler mSampler_CLAMP_NEAREST;
550 Sampler mSampler_CLAMP_LINEAR;
551 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
552 Sampler mSampler_WRAP_NEAREST;
553 Sampler mSampler_WRAP_LINEAR;
554 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
555
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700556 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700557 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700558
Jason Sams516c3192009-10-06 13:58:47 -0700559 public static class RSMessage implements Runnable {
560 protected int[] mData;
561 protected int mID;
562 public void run() {
563 }
564 }
565 public RSMessage mMessageCallback = null;
566
Jason Sams7d787b42009-11-15 12:14:26 -0800567 public enum Priority {
568 LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5
569 NORMAL (-4); //ANDROID_PRIORITY_DISPLAY
570
571 int mID;
572 Priority(int id) {
573 mID = id;
574 }
575 }
576
Jason Sams771bebb2009-12-07 12:40:12 -0800577 void validate() {
578 if (mContext == 0) {
579 throw new IllegalStateException("Calling RS with no Context active.");
580 }
581 }
582
Jason Sams7d787b42009-11-15 12:14:26 -0800583 public void contextSetPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800584 validate();
Jason Sams7d787b42009-11-15 12:14:26 -0800585 nContextSetPriority(p.mID);
586 }
587
Jason Sams704ff642010-02-09 16:05:07 -0800588 protected static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -0700589 RenderScript mRS;
590 boolean mRun = true;
591
592 MessageThread(RenderScript rs) {
593 super("RSMessageThread");
594 mRS = rs;
595
596 }
597
598 public void run() {
599 // This function is a temporary solution. The final solution will
600 // used typed allocations where the message id is the type indicator.
601 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -0700602 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700603 while(mRun) {
Jason Sams2e1872f2010-08-17 16:25:41 -0700604 int msg = mRS.nContextGetMessage(mRS.mContext, rbuf, true);
Jason Sams516c3192009-10-06 13:58:47 -0700605 if (msg == 0) {
606 // Should only happen during teardown.
607 // But we want to avoid starving other threads during
608 // teardown by yielding until the next line in the destructor
609 // can execute to set mRun = false
610 try {
611 sleep(1, 0);
612 } catch(InterruptedException e) {
613 }
614 }
615 if(mRS.mMessageCallback != null) {
616 mRS.mMessageCallback.mData = rbuf;
617 mRS.mMessageCallback.mID = msg;
618 mRS.mMessageCallback.run();
619 }
Jason Sams516c3192009-10-06 13:58:47 -0700620 }
Jason Sams3bc47d42009-11-12 15:10:25 -0800621 Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -0700622 }
623 }
624
Jason Sams704ff642010-02-09 16:05:07 -0800625 protected RenderScript() {
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700626 }
627
Jason Sams704ff642010-02-09 16:05:07 -0800628 public static RenderScript create() {
629 RenderScript rs = new RenderScript();
630
631 rs.mDev = rs.nDeviceCreate();
632 rs.mContext = rs.nContextCreate(rs.mDev, 0);
633 rs.mMessageThread = new MessageThread(rs);
634 rs.mMessageThread.start();
635 Element.initPredefined(rs);
636 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800637 }
638
Jason Sams715333b2009-11-17 17:26:46 -0800639 public void contextDump(int bits) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800640 validate();
Jason Sams715333b2009-11-17 17:26:46 -0800641 nContextDump(bits);
642 }
643
Jason Sams96ed4cf2010-06-15 12:15:57 -0700644 public void finish() {
645 nContextFinish();
646 }
647
Jason Samsf5b45962009-08-25 14:49:07 -0700648 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -0800649 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700650 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700651 mMessageThread.mRun = false;
652
Jason Sams2e1872f2010-08-17 16:25:41 -0700653 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -0700654 mContext = 0;
655
656 nDeviceDestroy(mDev);
657 mDev = 0;
658 }
Jason Sams02fb2cb2009-05-28 15:37:57 -0700659
Jason Samsa9e7a052009-09-25 14:51:22 -0700660 boolean isAlive() {
661 return mContext != 0;
662 }
663
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700664 ///////////////////////////////////////////////////////////////////////////////////
665 // Root state
666
Jason Sams704ff642010-02-09 16:05:07 -0800667 protected int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -0700668 if(o != null) {
669 return o.mID;
Jason Samsd8e41612009-08-20 17:22:40 -0700670 }
Jason Sams6b9dec02009-09-23 16:38:37 -0700671 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700672 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700673}
674
Jason Sams36e612a2009-07-31 16:26:13 -0700675
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700676