blob: c952d79cae230c6e0bb51d453b33113bec009586 [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 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700146 native int rsnFileOpen(int con, byte[] name);
147 synchronized int nFileOpen(byte[] name) {
148 return rsnFileOpen(mContext, name);
149 }
Jason Samsfe08d992009-05-27 14:45:32 -0700150
Jason Sams2e1872f2010-08-17 16:25:41 -0700151 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
152 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
153 return rsnElementCreate(mContext, type, kind, norm, vecSize);
154 }
Jason Sams70d4e502010-09-02 17:35:23 -0700155 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
156 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
157 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700158 }
159 native void rsnElementGetNativeData(int con, int id, int[] elementData);
160 synchronized void nElementGetNativeData(int id, int[] elementData) {
161 rsnElementGetNativeData(mContext, id, elementData);
162 }
163 native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names);
164 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) {
165 rsnElementGetSubElements(mContext, id, IDs, names);
166 }
Jason Sams768bc022009-09-21 19:41:04 -0700167
Jason Sams2e1872f2010-08-17 16:25:41 -0700168 native void rsnTypeBegin(int con, int elementID);
169 synchronized void nTypeBegin(int elementID) {
170 rsnTypeBegin(mContext, elementID);
171 }
172 native void rsnTypeAdd(int con, int dim, int val);
173 synchronized void nTypeAdd(int dim, int val) {
174 rsnTypeAdd(mContext, dim, val);
175 }
176 native int rsnTypeCreate(int con);
177 synchronized int nTypeCreate() {
178 return rsnTypeCreate(mContext);
179 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700180 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
181 synchronized void nTypeGetNativeData(int id, int[] typeData) {
182 rsnTypeGetNativeData(mContext, id, typeData);
183 }
Jason Sams768bc022009-09-21 19:41:04 -0700184
Jason Sams2e1872f2010-08-17 16:25:41 -0700185 native int rsnAllocationCreateTyped(int con, int type);
186 synchronized int nAllocationCreateTyped(int type) {
187 return rsnAllocationCreateTyped(mContext, type);
188 }
189 native int rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
190 synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
191 return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
192 }
193 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
194 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
195 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
196 }
197 native int rsnAllocationCreateFromBitmapBoxed(int con, int dstFmt, boolean genMips, Bitmap bmp);
198 synchronized int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp) {
199 return rsnAllocationCreateFromBitmapBoxed(mContext, dstFmt, genMips, bmp);
200 }
201 native int rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
202 synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
203 return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
204 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700205
Jason Sams2e1872f2010-08-17 16:25:41 -0700206 native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
207 synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) {
208 rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel);
209 }
210 native void rsnAllocationUploadToBufferObject(int con, int alloc);
211 synchronized void nAllocationUploadToBufferObject(int alloc) {
212 rsnAllocationUploadToBufferObject(mContext, alloc);
213 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700214
Jason Sams2e1872f2010-08-17 16:25:41 -0700215 native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes);
216 synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) {
217 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
218 }
219 native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes);
220 synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) {
221 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
222 }
223 native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes);
224 synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) {
225 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
226 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700227 native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
228 synchronized void nAllocationSubElementData1D(int id, int xoff, int compIdx, byte[] d, int sizeBytes) {
229 rsnAllocationSubElementData1D(mContext, id, xoff, compIdx, d, sizeBytes);
230 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700231 native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes);
232 synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) {
233 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
234 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700235
Jason Sams2e1872f2010-08-17 16:25:41 -0700236 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
237 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) {
238 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
239 }
240 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
241 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) {
242 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
243 }
244 native void rsnAllocationRead(int con, int id, int[] d);
245 synchronized void nAllocationRead(int id, int[] d) {
246 rsnAllocationRead(mContext, id, d);
247 }
248 native void rsnAllocationRead(int con, int id, float[] d);
249 synchronized void nAllocationRead(int id, float[] d) {
250 rsnAllocationRead(mContext, id, d);
251 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700252 native int rsnAllocationGetType(int con, int id);
253 synchronized int nAllocationGetType(int id) {
254 return rsnAllocationGetType(mContext, id);
255 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700256
Jason Sams2e1872f2010-08-17 16:25:41 -0700257 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
258 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
259 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
260 }
261 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
262 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
263 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
264 }
265 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
266 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
267 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
268 }
269 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
270 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
271 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
272 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700273
Jason Sams2e1872f2010-08-17 16:25:41 -0700274 native int rsnFontCreateFromFile(int con, String fileName, int size, int dpi);
275 synchronized int nFontCreateFromFile(String fileName, int size, int dpi) {
276 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
277 }
Jason Sams22534172009-08-04 16:58:20 -0700278
Jason Sams2e1872f2010-08-17 16:25:41 -0700279 native void rsnAdapter1DBindAllocation(int con, int ad, int alloc);
280 synchronized void nAdapter1DBindAllocation(int ad, int alloc) {
281 rsnAdapter1DBindAllocation(mContext, ad, alloc);
282 }
283 native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value);
284 synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) {
285 rsnAdapter1DSetConstraint(mContext, ad, dim, value);
286 }
287 native void rsnAdapter1DData(int con, int ad, int[] d);
288 synchronized void nAdapter1DData(int ad, int[] d) {
289 rsnAdapter1DData(mContext, ad, d);
290 }
291 native void rsnAdapter1DData(int con, int ad, float[] d);
292 synchronized void nAdapter1DData(int ad, float[] d) {
293 rsnAdapter1DData(mContext, ad, d);
294 }
295 native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d);
296 synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) {
297 rsnAdapter1DSubData(mContext, ad, off, count, d);
298 }
299 native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d);
300 synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) {
301 rsnAdapter1DSubData(mContext, ad, off, count, d);
302 }
303 native int rsnAdapter1DCreate(int con);
304 synchronized int nAdapter1DCreate() {
305 return rsnAdapter1DCreate(mContext);
306 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700307
Jason Sams2e1872f2010-08-17 16:25:41 -0700308 native void rsnAdapter2DBindAllocation(int con, int ad, int alloc);
309 synchronized void nAdapter2DBindAllocation(int ad, int alloc) {
310 rsnAdapter2DBindAllocation(mContext, ad, alloc);
311 }
312 native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value);
313 synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) {
314 rsnAdapter2DSetConstraint(mContext, ad, dim, value);
315 }
316 native void rsnAdapter2DData(int con, int ad, int[] d);
317 synchronized void nAdapter2DData(int ad, int[] d) {
318 rsnAdapter2DData(mContext, ad, d);
319 }
320 native void rsnAdapter2DData(int con, int ad, float[] d);
321 synchronized void nAdapter2DData(int ad, float[] d) {
322 rsnAdapter2DData(mContext, ad, d);
323 }
324 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d);
325 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) {
326 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
327 }
328 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d);
329 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) {
330 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
331 }
332 native int rsnAdapter2DCreate(int con);
333 synchronized int nAdapter2DCreate() {
334 return rsnAdapter2DCreate(mContext);
335 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700336
Jason Sams2e1872f2010-08-17 16:25:41 -0700337 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
338 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
339 rsnScriptBindAllocation(mContext, script, alloc, slot);
340 }
341 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
342 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
343 rsnScriptSetTimeZone(mContext, script, timeZone);
344 }
345 native void rsnScriptInvoke(int con, int id, int slot);
346 synchronized void nScriptInvoke(int id, int slot) {
347 rsnScriptInvoke(mContext, id, slot);
348 }
349 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
350 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
351 rsnScriptInvokeV(mContext, id, slot, params);
352 }
353 native void rsnScriptSetVarI(int con, int id, int slot, int val);
354 synchronized void nScriptSetVarI(int id, int slot, int val) {
355 rsnScriptSetVarI(mContext, id, slot, val);
356 }
357 native void rsnScriptSetVarF(int con, int id, int slot, float val);
358 synchronized void nScriptSetVarF(int id, int slot, float val) {
359 rsnScriptSetVarF(mContext, id, slot, val);
360 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700361 native void rsnScriptSetVarD(int con, int id, int slot, double val);
362 synchronized void nScriptSetVarD(int id, int slot, double val) {
363 rsnScriptSetVarD(mContext, id, slot, val);
364 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700365 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
366 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
367 rsnScriptSetVarV(mContext, id, slot, val);
368 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700369
Jason Sams2e1872f2010-08-17 16:25:41 -0700370 native void rsnScriptCBegin(int con);
371 synchronized void nScriptCBegin() {
372 rsnScriptCBegin(mContext);
373 }
374 native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
375 synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
376 rsnScriptCSetScript(mContext, script, offset, length);
377 }
378 native int rsnScriptCCreate(int con);
379 synchronized int nScriptCCreate() {
380 return rsnScriptCCreate(mContext);
381 }
Jason Samsebfb4362009-09-23 13:57:02 -0700382
Jason Sams2e1872f2010-08-17 16:25:41 -0700383 native void rsnSamplerBegin(int con);
384 synchronized void nSamplerBegin() {
385 rsnSamplerBegin(mContext);
386 }
387 native void rsnSamplerSet(int con, int param, int value);
388 synchronized void nSamplerSet(int param, int value) {
389 rsnSamplerSet(mContext, param, value);
390 }
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700391 native void rsnSamplerSet2(int con, int param, float value);
392 synchronized void nSamplerSet2(int param, float value) {
393 rsnSamplerSet2(mContext, param, value);
394 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700395 native int rsnSamplerCreate(int con);
396 synchronized int nSamplerCreate() {
397 return rsnSamplerCreate(mContext);
398 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800399
Jason Sams2e1872f2010-08-17 16:25:41 -0700400 native void rsnProgramStoreBegin(int con, int in, int out);
401 synchronized void nProgramStoreBegin(int in, int out) {
402 rsnProgramStoreBegin(mContext, in, out);
403 }
404 native void rsnProgramStoreDepthFunc(int con, int func);
405 synchronized void nProgramStoreDepthFunc(int func) {
406 rsnProgramStoreDepthFunc(mContext, func);
407 }
408 native void rsnProgramStoreDepthMask(int con, boolean enable);
409 synchronized void nProgramStoreDepthMask(boolean enable) {
410 rsnProgramStoreDepthMask(mContext, enable);
411 }
412 native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
413 synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
414 rsnProgramStoreColorMask(mContext, r, g, b, a);
415 }
416 native void rsnProgramStoreBlendFunc(int con, int src, int dst);
417 synchronized void nProgramStoreBlendFunc(int src, int dst) {
418 rsnProgramStoreBlendFunc(mContext, src, dst);
419 }
420 native void rsnProgramStoreDither(int con, boolean enable);
421 synchronized void nProgramStoreDither(boolean enable) {
422 rsnProgramStoreDither(mContext, enable);
423 }
424 native int rsnProgramStoreCreate(int con);
425 synchronized int nProgramStoreCreate() {
426 return rsnProgramStoreCreate(mContext);
427 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700428
Jason Sams2e1872f2010-08-17 16:25:41 -0700429 native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
430 synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
431 return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
432 }
433 native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
434 synchronized void nProgramRasterSetLineWidth(int pr, float v) {
435 rsnProgramRasterSetLineWidth(mContext, pr, v);
436 }
437 native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
438 synchronized void nProgramRasterSetCullMode(int pr, int mode) {
439 rsnProgramRasterSetCullMode(mContext, pr, mode);
440 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700441
Jason Sams2e1872f2010-08-17 16:25:41 -0700442 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
443 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
444 rsnProgramBindConstants(mContext, pv, slot, mID);
445 }
446 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
447 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
448 rsnProgramBindTexture(mContext, vpf, slot, a);
449 }
450 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
451 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
452 rsnProgramBindSampler(mContext, vpf, slot, s);
453 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700454 native int rsnProgramFragmentCreate(int con, String shader, int[] params);
455 synchronized int nProgramFragmentCreate(String shader, int[] params) {
456 return rsnProgramFragmentCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700457 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700458 native int rsnProgramVertexCreate(int con, String shader, int[] params);
459 synchronized int nProgramVertexCreate(String shader, int[] params) {
460 return rsnProgramVertexCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700461 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700462
Jason Sams2e1872f2010-08-17 16:25:41 -0700463 native int rsnMeshCreate(int con, int vtxCount, int indexCount);
464 synchronized int nMeshCreate(int vtxCount, int indexCount) {
465 return rsnMeshCreate(mContext, vtxCount, indexCount);
466 }
467 native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
468 synchronized void nMeshBindVertex(int id, int alloc, int slot) {
469 rsnMeshBindVertex(mContext, id, alloc, slot);
470 }
471 native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
472 synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
473 rsnMeshBindIndex(mContext, id, alloc, prim, slot);
474 }
475 native int rsnMeshGetVertexBufferCount(int con, int id);
476 synchronized int nMeshGetVertexBufferCount(int id) {
477 return rsnMeshGetVertexBufferCount(mContext, id);
478 }
479 native int rsnMeshGetIndexCount(int con, int id);
480 synchronized int nMeshGetIndexCount(int id) {
481 return rsnMeshGetIndexCount(mContext, id);
482 }
483 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
484 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
485 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
486 }
487 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
488 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
489 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
490 }
491
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700492
Jason Sams704ff642010-02-09 16:05:07 -0800493 protected int mDev;
494 protected int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700495 @SuppressWarnings({"FieldCanBeLocal"})
Jason Sams704ff642010-02-09 16:05:07 -0800496 protected MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700497
Jason Sams8cb39de2010-06-01 15:47:01 -0700498 Element mElement_U8;
499 Element mElement_I8;
500 Element mElement_U16;
501 Element mElement_I16;
502 Element mElement_U32;
503 Element mElement_I32;
504 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700505 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700506 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700507
Jason Sams8cb39de2010-06-01 15:47:01 -0700508 Element mElement_ELEMENT;
509 Element mElement_TYPE;
510 Element mElement_ALLOCATION;
511 Element mElement_SAMPLER;
512 Element mElement_SCRIPT;
513 Element mElement_MESH;
514 Element mElement_PROGRAM_FRAGMENT;
515 Element mElement_PROGRAM_VERTEX;
516 Element mElement_PROGRAM_RASTER;
517 Element mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700518
Jason Sams3c0dfba2009-09-27 17:50:38 -0700519 Element mElement_A_8;
520 Element mElement_RGB_565;
521 Element mElement_RGB_888;
522 Element mElement_RGBA_5551;
523 Element mElement_RGBA_4444;
524 Element mElement_RGBA_8888;
525
Jason Sams8cb39de2010-06-01 15:47:01 -0700526 Element mElement_FLOAT_2;
527 Element mElement_FLOAT_3;
528 Element mElement_FLOAT_4;
529 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800530
Jason Sams1d45c472010-08-25 14:31:48 -0700531 Element mElement_MATRIX_4X4;
532 Element mElement_MATRIX_3X3;
533 Element mElement_MATRIX_2X2;
534
Jason Sams4d339932010-05-11 14:03:58 -0700535 Sampler mSampler_CLAMP_NEAREST;
536 Sampler mSampler_CLAMP_LINEAR;
537 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
538 Sampler mSampler_WRAP_NEAREST;
539 Sampler mSampler_WRAP_LINEAR;
540 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
541
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700542 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
543 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
544 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_TEST;
545 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_WRITE;
546 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
547 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
548 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_TEST;
549 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_WRITE;
550 ProgramStore mProgramStore_BLEND_ADD_DEPTH_TEST;
551 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_DEPTH;
552 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_TEST;
553 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_WRITE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700554
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700555 ProgramRaster mProgramRaster_CULL_BACK;
556 ProgramRaster mProgramRaster_CULL_FRONT;
557 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700558
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700559 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700560 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700561
Jason Sams516c3192009-10-06 13:58:47 -0700562 public static class RSMessage implements Runnable {
563 protected int[] mData;
564 protected int mID;
565 public void run() {
566 }
567 }
568 public RSMessage mMessageCallback = null;
569
Jason Sams7d787b42009-11-15 12:14:26 -0800570 public enum Priority {
571 LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5
572 NORMAL (-4); //ANDROID_PRIORITY_DISPLAY
573
574 int mID;
575 Priority(int id) {
576 mID = id;
577 }
578 }
579
Jason Sams771bebb2009-12-07 12:40:12 -0800580 void validate() {
581 if (mContext == 0) {
582 throw new IllegalStateException("Calling RS with no Context active.");
583 }
584 }
585
Jason Sams7d787b42009-11-15 12:14:26 -0800586 public void contextSetPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800587 validate();
Jason Sams7d787b42009-11-15 12:14:26 -0800588 nContextSetPriority(p.mID);
589 }
590
Jason Sams704ff642010-02-09 16:05:07 -0800591 protected static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -0700592 RenderScript mRS;
593 boolean mRun = true;
594
595 MessageThread(RenderScript rs) {
596 super("RSMessageThread");
597 mRS = rs;
598
599 }
600
601 public void run() {
602 // This function is a temporary solution. The final solution will
603 // used typed allocations where the message id is the type indicator.
604 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -0700605 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700606 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -0700607 rbuf[0] = 0;
Jason Sams2e1872f2010-08-17 16:25:41 -0700608 int msg = mRS.nContextGetMessage(mRS.mContext, rbuf, true);
Stephen Hinesab98bb62010-09-24 14:38:30 -0700609 if ((msg == 0)) {
Jason Sams1d45c472010-08-25 14:31:48 -0700610 // Can happen for two reasons
Stephen Hinesab98bb62010-09-24 14:38:30 -0700611 if (rbuf[0] > 0 && mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -0700612 // 1: Buffer needs to be enlarged.
613 rbuf = new int[rbuf[0] + 2];
614 } else {
615 // 2: teardown.
616 // But we want to avoid starving other threads during
617 // teardown by yielding until the next line in the destructor
618 // can execute to set mRun = false
619 try {
620 sleep(1, 0);
621 } catch(InterruptedException e) {
622 }
Jason Sams516c3192009-10-06 13:58:47 -0700623 }
Stephen Hinesab98bb62010-09-24 14:38:30 -0700624 continue;
Jason Sams516c3192009-10-06 13:58:47 -0700625 }
626 if(mRS.mMessageCallback != null) {
627 mRS.mMessageCallback.mData = rbuf;
628 mRS.mMessageCallback.mID = msg;
629 mRS.mMessageCallback.run();
630 }
Jason Sams516c3192009-10-06 13:58:47 -0700631 }
Jason Sams3bc47d42009-11-12 15:10:25 -0800632 Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -0700633 }
634 }
635
Jason Sams704ff642010-02-09 16:05:07 -0800636 protected RenderScript() {
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700637 }
638
Jason Sams704ff642010-02-09 16:05:07 -0800639 public static RenderScript create() {
640 RenderScript rs = new RenderScript();
641
642 rs.mDev = rs.nDeviceCreate();
643 rs.mContext = rs.nContextCreate(rs.mDev, 0);
644 rs.mMessageThread = new MessageThread(rs);
645 rs.mMessageThread.start();
646 Element.initPredefined(rs);
647 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800648 }
649
Jason Sams715333b2009-11-17 17:26:46 -0800650 public void contextDump(int bits) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800651 validate();
Jason Sams715333b2009-11-17 17:26:46 -0800652 nContextDump(bits);
653 }
654
Jason Sams96ed4cf2010-06-15 12:15:57 -0700655 public void finish() {
656 nContextFinish();
657 }
658
Jason Samsf5b45962009-08-25 14:49:07 -0700659 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -0800660 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700661 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700662 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -0700663 try {
664 mMessageThread.join();
665 } catch(InterruptedException e) {
666 }
Jason Sams516c3192009-10-06 13:58:47 -0700667
Jason Sams2e1872f2010-08-17 16:25:41 -0700668 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -0700669 mContext = 0;
670
671 nDeviceDestroy(mDev);
672 mDev = 0;
673 }
Jason Sams02fb2cb2009-05-28 15:37:57 -0700674
Jason Samsa9e7a052009-09-25 14:51:22 -0700675 boolean isAlive() {
676 return mContext != 0;
677 }
678
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700679 ///////////////////////////////////////////////////////////////////////////////////
680 // Root state
681
Jason Sams704ff642010-02-09 16:05:07 -0800682 protected int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -0700683 if(o != null) {
684 return o.mID;
Jason Samsd8e41612009-08-20 17:22:40 -0700685 }
Jason Sams6b9dec02009-09-23 16:38:37 -0700686 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700687 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700688}
689
Jason Sams36e612a2009-07-31 16:26:13 -0700690
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700691