blob: 97ce7dcfc4685bf7b479ae3876d3304dd3badcc9 [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 Sams2e1872f2010-08-17 16:25:41 -0700159 native int rsnFileOpen(int con, byte[] name);
160 synchronized int nFileOpen(byte[] name) {
161 return rsnFileOpen(mContext, name);
162 }
Jason Samsfe08d992009-05-27 14:45:32 -0700163
Jason Sams2e1872f2010-08-17 16:25:41 -0700164 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
165 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
166 return rsnElementCreate(mContext, type, kind, norm, vecSize);
167 }
Jason Sams70d4e502010-09-02 17:35:23 -0700168 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
169 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
170 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700171 }
172 native void rsnElementGetNativeData(int con, int id, int[] elementData);
173 synchronized void nElementGetNativeData(int id, int[] elementData) {
174 rsnElementGetNativeData(mContext, id, elementData);
175 }
176 native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names);
177 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) {
178 rsnElementGetSubElements(mContext, id, IDs, names);
179 }
Jason Sams768bc022009-09-21 19:41:04 -0700180
Jason Sams2e1872f2010-08-17 16:25:41 -0700181 native void rsnTypeBegin(int con, int elementID);
182 synchronized void nTypeBegin(int elementID) {
183 rsnTypeBegin(mContext, elementID);
184 }
185 native void rsnTypeAdd(int con, int dim, int val);
186 synchronized void nTypeAdd(int dim, int val) {
187 rsnTypeAdd(mContext, dim, val);
188 }
189 native int rsnTypeCreate(int con);
190 synchronized int nTypeCreate() {
191 return rsnTypeCreate(mContext);
192 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700193 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
194 synchronized void nTypeGetNativeData(int id, int[] typeData) {
195 rsnTypeGetNativeData(mContext, id, typeData);
196 }
Jason Sams768bc022009-09-21 19:41:04 -0700197
Jason Sams2e1872f2010-08-17 16:25:41 -0700198 native int rsnAllocationCreateTyped(int con, int type);
199 synchronized int nAllocationCreateTyped(int type) {
200 return rsnAllocationCreateTyped(mContext, type);
201 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700202 native void rsnAllocationUpdateFromBitmap(int con, int alloc, Bitmap bmp);
203 synchronized void nAllocationUpdateFromBitmap(int alloc, Bitmap bmp) {
204 rsnAllocationUpdateFromBitmap(mContext, alloc, bmp);
205 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700206 native int rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
207 synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
208 return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
209 }
210 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
211 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
212 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
213 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700214 native int rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
215 synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
216 return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
217 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700218
Jason Sams2e1872f2010-08-17 16:25:41 -0700219 native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
220 synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) {
221 rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel);
222 }
223 native void rsnAllocationUploadToBufferObject(int con, int alloc);
224 synchronized void nAllocationUploadToBufferObject(int alloc) {
225 rsnAllocationUploadToBufferObject(mContext, alloc);
226 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700227
Jason Sams2e1872f2010-08-17 16:25:41 -0700228 native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes);
229 synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) {
230 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
231 }
232 native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes);
233 synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) {
234 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
235 }
236 native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes);
237 synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) {
238 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
239 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700240 native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
241 synchronized void nAllocationSubElementData1D(int id, int xoff, int compIdx, byte[] d, int sizeBytes) {
242 rsnAllocationSubElementData1D(mContext, id, xoff, compIdx, d, sizeBytes);
243 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700244 native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes);
245 synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) {
246 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
247 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700248
Jason Sams2e1872f2010-08-17 16:25:41 -0700249 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
250 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) {
251 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
252 }
253 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
254 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) {
255 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
256 }
257 native void rsnAllocationRead(int con, int id, int[] d);
258 synchronized void nAllocationRead(int id, int[] d) {
259 rsnAllocationRead(mContext, id, d);
260 }
261 native void rsnAllocationRead(int con, int id, float[] d);
262 synchronized void nAllocationRead(int id, float[] d) {
263 rsnAllocationRead(mContext, id, d);
264 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700265 native int rsnAllocationGetType(int con, int id);
266 synchronized int nAllocationGetType(int id) {
267 return rsnAllocationGetType(mContext, id);
268 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700269
Jason Sams5edc6082010-10-05 13:32:49 -0700270 native void rsnAllocationResize1D(int con, int id, int dimX);
271 synchronized void nAllocationResize1D(int id, int dimX) {
272 rsnAllocationResize1D(mContext, id, dimX);
273 }
274 native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
275 synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
276 rsnAllocationResize2D(mContext, id, dimX, dimY);
277 }
278
Jason Sams2e1872f2010-08-17 16:25:41 -0700279 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
280 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
281 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
282 }
283 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
284 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
285 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
286 }
287 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
288 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
289 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
290 }
291 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
292 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
293 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
294 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700295
Jason Sams2e1872f2010-08-17 16:25:41 -0700296 native int rsnFontCreateFromFile(int con, String fileName, int size, int dpi);
297 synchronized int nFontCreateFromFile(String fileName, int size, int dpi) {
298 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
299 }
Jason Sams22534172009-08-04 16:58:20 -0700300
Jason Sams2e1872f2010-08-17 16:25:41 -0700301 native void rsnAdapter1DBindAllocation(int con, int ad, int alloc);
302 synchronized void nAdapter1DBindAllocation(int ad, int alloc) {
303 rsnAdapter1DBindAllocation(mContext, ad, alloc);
304 }
305 native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value);
306 synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) {
307 rsnAdapter1DSetConstraint(mContext, ad, dim, value);
308 }
309 native void rsnAdapter1DData(int con, int ad, int[] d);
310 synchronized void nAdapter1DData(int ad, int[] d) {
311 rsnAdapter1DData(mContext, ad, d);
312 }
313 native void rsnAdapter1DData(int con, int ad, float[] d);
314 synchronized void nAdapter1DData(int ad, float[] d) {
315 rsnAdapter1DData(mContext, ad, d);
316 }
317 native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d);
318 synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) {
319 rsnAdapter1DSubData(mContext, ad, off, count, d);
320 }
321 native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d);
322 synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) {
323 rsnAdapter1DSubData(mContext, ad, off, count, d);
324 }
325 native int rsnAdapter1DCreate(int con);
326 synchronized int nAdapter1DCreate() {
327 return rsnAdapter1DCreate(mContext);
328 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700329
Jason Sams2e1872f2010-08-17 16:25:41 -0700330 native void rsnAdapter2DBindAllocation(int con, int ad, int alloc);
331 synchronized void nAdapter2DBindAllocation(int ad, int alloc) {
332 rsnAdapter2DBindAllocation(mContext, ad, alloc);
333 }
334 native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value);
335 synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) {
336 rsnAdapter2DSetConstraint(mContext, ad, dim, value);
337 }
338 native void rsnAdapter2DData(int con, int ad, int[] d);
339 synchronized void nAdapter2DData(int ad, int[] d) {
340 rsnAdapter2DData(mContext, ad, d);
341 }
342 native void rsnAdapter2DData(int con, int ad, float[] d);
343 synchronized void nAdapter2DData(int ad, float[] d) {
344 rsnAdapter2DData(mContext, ad, d);
345 }
346 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d);
347 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) {
348 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
349 }
350 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d);
351 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) {
352 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
353 }
354 native int rsnAdapter2DCreate(int con);
355 synchronized int nAdapter2DCreate() {
356 return rsnAdapter2DCreate(mContext);
357 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700358
Jason Sams2e1872f2010-08-17 16:25:41 -0700359 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
360 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
361 rsnScriptBindAllocation(mContext, script, alloc, slot);
362 }
363 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
364 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
365 rsnScriptSetTimeZone(mContext, script, timeZone);
366 }
367 native void rsnScriptInvoke(int con, int id, int slot);
368 synchronized void nScriptInvoke(int id, int slot) {
369 rsnScriptInvoke(mContext, id, slot);
370 }
371 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
372 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
373 rsnScriptInvokeV(mContext, id, slot, params);
374 }
375 native void rsnScriptSetVarI(int con, int id, int slot, int val);
376 synchronized void nScriptSetVarI(int id, int slot, int val) {
377 rsnScriptSetVarI(mContext, id, slot, val);
378 }
Stephen Hines031ec58c2010-10-11 10:54:21 -0700379 native void rsnScriptSetVarJ(int con, int id, int slot, long val);
380 synchronized void nScriptSetVarJ(int id, int slot, long val) {
381 rsnScriptSetVarJ(mContext, id, slot, val);
382 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700383 native void rsnScriptSetVarF(int con, int id, int slot, float val);
384 synchronized void nScriptSetVarF(int id, int slot, float val) {
385 rsnScriptSetVarF(mContext, id, slot, val);
386 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700387 native void rsnScriptSetVarD(int con, int id, int slot, double val);
388 synchronized void nScriptSetVarD(int id, int slot, double val) {
389 rsnScriptSetVarD(mContext, id, slot, val);
390 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700391 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
392 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
393 rsnScriptSetVarV(mContext, id, slot, val);
394 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700395
Jason Sams2e1872f2010-08-17 16:25:41 -0700396 native void rsnScriptCBegin(int con);
397 synchronized void nScriptCBegin() {
398 rsnScriptCBegin(mContext);
399 }
400 native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
401 synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
402 rsnScriptCSetScript(mContext, script, offset, length);
403 }
404 native int rsnScriptCCreate(int con);
405 synchronized int nScriptCCreate() {
406 return rsnScriptCCreate(mContext);
407 }
Jason Samsebfb4362009-09-23 13:57:02 -0700408
Jason Sams2e1872f2010-08-17 16:25:41 -0700409 native void rsnSamplerBegin(int con);
410 synchronized void nSamplerBegin() {
411 rsnSamplerBegin(mContext);
412 }
413 native void rsnSamplerSet(int con, int param, int value);
414 synchronized void nSamplerSet(int param, int value) {
415 rsnSamplerSet(mContext, param, value);
416 }
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700417 native void rsnSamplerSet2(int con, int param, float value);
418 synchronized void nSamplerSet2(int param, float value) {
419 rsnSamplerSet2(mContext, param, value);
420 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700421 native int rsnSamplerCreate(int con);
422 synchronized int nSamplerCreate() {
423 return rsnSamplerCreate(mContext);
424 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800425
Jason Sams2e1872f2010-08-17 16:25:41 -0700426 native void rsnProgramStoreBegin(int con, int in, int out);
427 synchronized void nProgramStoreBegin(int in, int out) {
428 rsnProgramStoreBegin(mContext, in, out);
429 }
430 native void rsnProgramStoreDepthFunc(int con, int func);
431 synchronized void nProgramStoreDepthFunc(int func) {
432 rsnProgramStoreDepthFunc(mContext, func);
433 }
434 native void rsnProgramStoreDepthMask(int con, boolean enable);
435 synchronized void nProgramStoreDepthMask(boolean enable) {
436 rsnProgramStoreDepthMask(mContext, enable);
437 }
438 native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
439 synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
440 rsnProgramStoreColorMask(mContext, r, g, b, a);
441 }
442 native void rsnProgramStoreBlendFunc(int con, int src, int dst);
443 synchronized void nProgramStoreBlendFunc(int src, int dst) {
444 rsnProgramStoreBlendFunc(mContext, src, dst);
445 }
446 native void rsnProgramStoreDither(int con, boolean enable);
447 synchronized void nProgramStoreDither(boolean enable) {
448 rsnProgramStoreDither(mContext, enable);
449 }
450 native int rsnProgramStoreCreate(int con);
451 synchronized int nProgramStoreCreate() {
452 return rsnProgramStoreCreate(mContext);
453 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700454
Jason Sams2e1872f2010-08-17 16:25:41 -0700455 native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
456 synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
457 return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
458 }
459 native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
460 synchronized void nProgramRasterSetLineWidth(int pr, float v) {
461 rsnProgramRasterSetLineWidth(mContext, pr, v);
462 }
463 native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
464 synchronized void nProgramRasterSetCullMode(int pr, int mode) {
465 rsnProgramRasterSetCullMode(mContext, pr, mode);
466 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700467
Jason Sams2e1872f2010-08-17 16:25:41 -0700468 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
469 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
470 rsnProgramBindConstants(mContext, pv, slot, mID);
471 }
472 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
473 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
474 rsnProgramBindTexture(mContext, vpf, slot, a);
475 }
476 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
477 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
478 rsnProgramBindSampler(mContext, vpf, slot, s);
479 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700480 native int rsnProgramFragmentCreate(int con, String shader, int[] params);
481 synchronized int nProgramFragmentCreate(String shader, int[] params) {
482 return rsnProgramFragmentCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700483 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700484 native int rsnProgramVertexCreate(int con, String shader, int[] params);
485 synchronized int nProgramVertexCreate(String shader, int[] params) {
486 return rsnProgramVertexCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700487 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700488
Jason Sams2e1872f2010-08-17 16:25:41 -0700489 native int rsnMeshCreate(int con, int vtxCount, int indexCount);
490 synchronized int nMeshCreate(int vtxCount, int indexCount) {
491 return rsnMeshCreate(mContext, vtxCount, indexCount);
492 }
493 native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
494 synchronized void nMeshBindVertex(int id, int alloc, int slot) {
495 rsnMeshBindVertex(mContext, id, alloc, slot);
496 }
497 native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
498 synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
499 rsnMeshBindIndex(mContext, id, alloc, prim, slot);
500 }
501 native int rsnMeshGetVertexBufferCount(int con, int id);
502 synchronized int nMeshGetVertexBufferCount(int id) {
503 return rsnMeshGetVertexBufferCount(mContext, id);
504 }
505 native int rsnMeshGetIndexCount(int con, int id);
506 synchronized int nMeshGetIndexCount(int id) {
507 return rsnMeshGetIndexCount(mContext, id);
508 }
509 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
510 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
511 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
512 }
513 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
514 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
515 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
516 }
517
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700518
Jason Sams704ff642010-02-09 16:05:07 -0800519 protected int mDev;
520 protected int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700521 @SuppressWarnings({"FieldCanBeLocal"})
Jason Sams704ff642010-02-09 16:05:07 -0800522 protected MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700523
Jason Sams8cb39de2010-06-01 15:47:01 -0700524 Element mElement_U8;
525 Element mElement_I8;
526 Element mElement_U16;
527 Element mElement_I16;
528 Element mElement_U32;
529 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -0700530 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -0700531 Element mElement_I64;
Jason Sams8cb39de2010-06-01 15:47:01 -0700532 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700533 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700534 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700535
Jason Sams8cb39de2010-06-01 15:47:01 -0700536 Element mElement_ELEMENT;
537 Element mElement_TYPE;
538 Element mElement_ALLOCATION;
539 Element mElement_SAMPLER;
540 Element mElement_SCRIPT;
541 Element mElement_MESH;
542 Element mElement_PROGRAM_FRAGMENT;
543 Element mElement_PROGRAM_VERTEX;
544 Element mElement_PROGRAM_RASTER;
545 Element mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700546
Jason Sams3c0dfba2009-09-27 17:50:38 -0700547 Element mElement_A_8;
548 Element mElement_RGB_565;
549 Element mElement_RGB_888;
550 Element mElement_RGBA_5551;
551 Element mElement_RGBA_4444;
552 Element mElement_RGBA_8888;
553
Jason Sams8cb39de2010-06-01 15:47:01 -0700554 Element mElement_FLOAT_2;
555 Element mElement_FLOAT_3;
556 Element mElement_FLOAT_4;
557 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800558
Jason Sams1d45c472010-08-25 14:31:48 -0700559 Element mElement_MATRIX_4X4;
560 Element mElement_MATRIX_3X3;
561 Element mElement_MATRIX_2X2;
562
Jason Sams4d339932010-05-11 14:03:58 -0700563 Sampler mSampler_CLAMP_NEAREST;
564 Sampler mSampler_CLAMP_LINEAR;
565 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
566 Sampler mSampler_WRAP_NEAREST;
567 Sampler mSampler_WRAP_LINEAR;
568 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
569
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700570 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
571 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
572 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_TEST;
573 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_WRITE;
574 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
575 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
576 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_TEST;
577 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_WRITE;
578 ProgramStore mProgramStore_BLEND_ADD_DEPTH_TEST;
579 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_DEPTH;
580 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_TEST;
581 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_WRITE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700582
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700583 ProgramRaster mProgramRaster_CULL_BACK;
584 ProgramRaster mProgramRaster_CULL_FRONT;
585 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700586
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700587 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700588 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700589
Jason Sams516c3192009-10-06 13:58:47 -0700590 public static class RSMessage implements Runnable {
591 protected int[] mData;
592 protected int mID;
593 public void run() {
594 }
595 }
596 public RSMessage mMessageCallback = null;
597
Jason Sams7d787b42009-11-15 12:14:26 -0800598 public enum Priority {
599 LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5
600 NORMAL (-4); //ANDROID_PRIORITY_DISPLAY
601
602 int mID;
603 Priority(int id) {
604 mID = id;
605 }
606 }
607
Jason Sams771bebb2009-12-07 12:40:12 -0800608 void validate() {
609 if (mContext == 0) {
610 throw new IllegalStateException("Calling RS with no Context active.");
611 }
612 }
613
Jason Sams7d787b42009-11-15 12:14:26 -0800614 public void contextSetPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800615 validate();
Jason Sams7d787b42009-11-15 12:14:26 -0800616 nContextSetPriority(p.mID);
617 }
618
Jason Sams704ff642010-02-09 16:05:07 -0800619 protected static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -0700620 RenderScript mRS;
621 boolean mRun = true;
622
623 MessageThread(RenderScript rs) {
624 super("RSMessageThread");
625 mRS = rs;
626
627 }
628
629 public void run() {
630 // This function is a temporary solution. The final solution will
631 // used typed allocations where the message id is the type indicator.
632 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -0700633 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700634 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -0700635 rbuf[0] = 0;
Jason Sams2e1872f2010-08-17 16:25:41 -0700636 int msg = mRS.nContextGetMessage(mRS.mContext, rbuf, true);
Stephen Hinesab98bb62010-09-24 14:38:30 -0700637 if ((msg == 0)) {
Jason Sams1d45c472010-08-25 14:31:48 -0700638 // Can happen for two reasons
Stephen Hinesab98bb62010-09-24 14:38:30 -0700639 if (rbuf[0] > 0 && mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -0700640 // 1: Buffer needs to be enlarged.
641 rbuf = new int[rbuf[0] + 2];
642 } else {
643 // 2: teardown.
644 // But we want to avoid starving other threads during
645 // teardown by yielding until the next line in the destructor
646 // can execute to set mRun = false
647 try {
648 sleep(1, 0);
649 } catch(InterruptedException e) {
650 }
Jason Sams516c3192009-10-06 13:58:47 -0700651 }
Stephen Hinesab98bb62010-09-24 14:38:30 -0700652 continue;
Jason Sams516c3192009-10-06 13:58:47 -0700653 }
654 if(mRS.mMessageCallback != null) {
655 mRS.mMessageCallback.mData = rbuf;
656 mRS.mMessageCallback.mID = msg;
657 mRS.mMessageCallback.run();
658 }
Jason Sams516c3192009-10-06 13:58:47 -0700659 }
Jason Sams3bc47d42009-11-12 15:10:25 -0800660 Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -0700661 }
662 }
663
Jason Sams704ff642010-02-09 16:05:07 -0800664 protected RenderScript() {
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700665 }
666
Jason Sams704ff642010-02-09 16:05:07 -0800667 public static RenderScript create() {
668 RenderScript rs = new RenderScript();
669
670 rs.mDev = rs.nDeviceCreate();
671 rs.mContext = rs.nContextCreate(rs.mDev, 0);
672 rs.mMessageThread = new MessageThread(rs);
673 rs.mMessageThread.start();
674 Element.initPredefined(rs);
675 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800676 }
677
Jason Sams715333b2009-11-17 17:26:46 -0800678 public void contextDump(int bits) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800679 validate();
Jason Sams715333b2009-11-17 17:26:46 -0800680 nContextDump(bits);
681 }
682
Jason Sams96ed4cf2010-06-15 12:15:57 -0700683 public void finish() {
684 nContextFinish();
685 }
686
Jason Samsf5b45962009-08-25 14:49:07 -0700687 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -0800688 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700689 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700690 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -0700691 try {
692 mMessageThread.join();
693 } catch(InterruptedException e) {
694 }
Jason Sams516c3192009-10-06 13:58:47 -0700695
Jason Sams2e1872f2010-08-17 16:25:41 -0700696 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -0700697 mContext = 0;
698
699 nDeviceDestroy(mDev);
700 mDev = 0;
701 }
Jason Sams02fb2cb2009-05-28 15:37:57 -0700702
Jason Samsa9e7a052009-09-25 14:51:22 -0700703 boolean isAlive() {
704 return mContext != 0;
705 }
706
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700707 ///////////////////////////////////////////////////////////////////////////////////
708 // Root state
709
Jason Sams704ff642010-02-09 16:05:07 -0800710 protected int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -0700711 if(o != null) {
712 return o.mID;
Jason Samsd8e41612009-08-20 17:22:40 -0700713 }
Jason Sams6b9dec02009-09-23 16:38:37 -0700714 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700715 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700716}
717
Jason Sams36e612a2009-07-31 16:26:13 -0700718
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700719