blob: 3d4f33335c6468702e5fb8304be62935b7cb8cbf [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
Jack Palevich43702d82009-05-28 13:38:16 -070019import java.io.IOException;
Jason Sams36e612a2009-07-31 16:26:13 -070020import java.io.InputStream;
21
22import android.content.res.Resources;
23import android.os.Bundle;
24import android.util.Config;
25import android.util.Log;
26import android.view.Surface;
Jack Palevich43702d82009-05-28 13:38:16 -070027
Jason Samsdba3ba52009-07-30 14:56:12 -070028import android.graphics.Bitmap;
29import android.graphics.BitmapFactory;
Jason Samsdba3ba52009-07-30 14:56:12 -070030import android.graphics.Color;
31
Jack Palevich60aa3ea2009-05-26 13:45:08 -070032
Jason Samse29d4712009-07-23 15:19:03 -070033/**
34 * @hide
35 *
36 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070037public class RenderScript {
Jason Samsf29ca502009-06-23 12:22:47 -070038 static final String LOG_TAG = "libRS_jni";
Jack Palevich60aa3ea2009-05-26 13:45:08 -070039 private static final boolean DEBUG = false;
40 private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
41
42
43
Jason Sams02fb2cb2009-05-28 15:37:57 -070044 /*
Jack Palevich60aa3ea2009-05-26 13:45:08 -070045 * We use a class initializer to allow the native code to cache some
46 * field offsets.
47 */
48 private static boolean sInitialized;
49 native private static void _nInit();
50
Jason Samsdba3ba52009-07-30 14:56:12 -070051 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
52
Jack Palevich60aa3ea2009-05-26 13:45:08 -070053 static {
54 sInitialized = false;
55 try {
Jason Samse29d4712009-07-23 15:19:03 -070056 System.loadLibrary("rs_jni");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070057 _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070058 sInitialized = true;
59 } catch (UnsatisfiedLinkError e) {
60 Log.d(LOG_TAG, "RenderScript JNI library not found!");
61 }
Jason Samsdba3ba52009-07-30 14:56:12 -070062 mBitmapOptions.inScaled = false;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070063 }
64
Jason Sams36e612a2009-07-31 16:26:13 -070065 native int nDeviceCreate();
66 native void nDeviceDestroy(int dev);
67 native int nContextCreate(int dev, Surface sur, int ver);
68 native void nContextDestroy(int con);
Jack Palevich60aa3ea2009-05-26 13:45:08 -070069
70 //void rsContextBindSampler (uint32_t slot, RsSampler sampler);
71 //void rsContextBindRootScript (RsScript sampler);
Jason Sams36e612a2009-07-31 16:26:13 -070072 native void nContextBindRootScript(int script);
73 native void nContextBindSampler(int sampler, int slot);
74 native void nContextBindProgramFragmentStore(int pfs);
75 native void nContextBindProgramFragment(int pf);
76 native void nContextBindProgramVertex(int pf);
Jack Palevich60aa3ea2009-05-26 13:45:08 -070077
Jason Sams36e612a2009-07-31 16:26:13 -070078 native void nAssignName(int obj, byte[] name);
79 native int nFileOpen(byte[] name);
Jason Sams3eaa3382009-06-10 15:04:38 -070080
Jason Sams36e612a2009-07-31 16:26:13 -070081 native void nElementBegin();
82 native void nElementAddPredefined(int predef);
83 native void nElementAdd(int kind, int type, int norm, int bits);
84 native int nElementCreate();
85 native int nElementGetPredefined(int predef);
86 native void nElementDestroy(int obj);
Jack Palevich60aa3ea2009-05-26 13:45:08 -070087
Jason Sams36e612a2009-07-31 16:26:13 -070088 native void nTypeBegin(int elementID);
89 native void nTypeAdd(int dim, int val);
90 native int nTypeCreate();
91 native void nTypeDestroy(int id);
Jack Palevich60aa3ea2009-05-26 13:45:08 -070092
Jason Sams36e612a2009-07-31 16:26:13 -070093 native int nAllocationCreateTyped(int type);
94 native int nAllocationCreatePredefSized(int predef, int count);
95 native int nAllocationCreateSized(int elem, int count);
96 native int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
97 native int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
Jason Samsfe08d992009-05-27 14:45:32 -070098
Jason Sams36e612a2009-07-31 16:26:13 -070099 native void nAllocationUploadToTexture(int alloc, int baseMioLevel);
100 native void nAllocationDestroy(int alloc);
101 native void nAllocationData(int id, int[] d);
102 native void nAllocationData(int id, float[] d);
103 native void nAllocationSubData1D(int id, int off, int count, int[] d);
104 native void nAllocationSubData1D(int id, int off, int count, float[] d);
105 native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d);
106 native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700107
Jason Sams36e612a2009-07-31 16:26:13 -0700108 native void nTriangleMeshDestroy(int id);
109 native void nTriangleMeshBegin(int vertex, int index);
110 native void nTriangleMeshAddVertex_XY (float x, float y);
111 native void nTriangleMeshAddVertex_XYZ (float x, float y, float z);
112 native void nTriangleMeshAddVertex_XY_ST (float x, float y, float s, float t);
113 native void nTriangleMeshAddVertex_XYZ_ST (float x, float y, float z, float s, float t);
114 native void nTriangleMeshAddVertex_XYZ_ST_NORM (float x, float y, float z, float s, float t, float nx, float ny, float nz);
115 native void nTriangleMeshAddTriangle(int i1, int i2, int i3);
116 native int nTriangleMeshCreate();
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700117
Jason Sams36e612a2009-07-31 16:26:13 -0700118 native void nAdapter1DDestroy(int id);
119 native void nAdapter1DBindAllocation(int ad, int alloc);
120 native void nAdapter1DSetConstraint(int ad, int dim, int value);
121 native void nAdapter1DData(int ad, int[] d);
122 native void nAdapter1DSubData(int ad, int off, int count, int[] d);
123 native void nAdapter1DData(int ad, float[] d);
124 native void nAdapter1DSubData(int ad, int off, int count, float[] d);
125 native int nAdapter1DCreate();
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700126
Jason Sams36e612a2009-07-31 16:26:13 -0700127 native void nScriptDestroy(int script);
128 native void nScriptBindAllocation(int vtm, int alloc, int slot);
129 native void nScriptCBegin();
130 native void nScriptCSetClearColor(float r, float g, float b, float a);
131 native void nScriptCSetClearDepth(float depth);
132 native void nScriptCSetClearStencil(int stencil);
133 native void nScriptCSetTimeZone(byte[] timeZone);
134 native void nScriptCAddType(int type);
135 native void nScriptCSetRoot(boolean isRoot);
136 native void nScriptCSetScript(byte[] script, int offset, int length);
137 native int nScriptCCreate();
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700138
Jason Sams36e612a2009-07-31 16:26:13 -0700139 native void nSamplerDestroy(int sampler);
140 native void nSamplerBegin();
141 native void nSamplerSet(int param, int value);
142 native int nSamplerCreate();
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700143
Jason Sams36e612a2009-07-31 16:26:13 -0700144 native void nProgramFragmentStoreBegin(int in, int out);
145 native void nProgramFragmentStoreDepthFunc(int func);
146 native void nProgramFragmentStoreDepthMask(boolean enable);
147 native void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a);
148 native void nProgramFragmentStoreBlendFunc(int src, int dst);
149 native void nProgramFragmentStoreDither(boolean enable);
150 native int nProgramFragmentStoreCreate();
151 native void nProgramFragmentStoreDestroy(int pgm);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700152
Jason Sams36e612a2009-07-31 16:26:13 -0700153 native void nProgramFragmentBegin(int in, int out);
154 native void nProgramFragmentBindTexture(int vpf, int slot, int a);
155 native void nProgramFragmentBindSampler(int vpf, int slot, int s);
156 native void nProgramFragmentSetType(int slot, int vt);
157 native void nProgramFragmentSetEnvMode(int slot, int env);
158 native void nProgramFragmentSetTexEnable(int slot, boolean enable);
159 native int nProgramFragmentCreate();
160 native void nProgramFragmentDestroy(int pgm);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700161
Jason Sams36e612a2009-07-31 16:26:13 -0700162 native void nProgramVertexDestroy(int pv);
163 native void nProgramVertexBindAllocation(int pv, int slot, int mID);
164 native void nProgramVertexBegin(int inID, int outID);
165 native void nProgramVertexSetType(int slot, int mID);
166 native void nProgramVertexSetTextureMatrixEnable(boolean enable);
167 native void nProgramVertexAddLight(int id);
168 native int nProgramVertexCreate();
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700169
Jason Sams36e612a2009-07-31 16:26:13 -0700170 native void nLightBegin();
171 native void nLightSetIsMono(boolean isMono);
172 native void nLightSetIsLocal(boolean isLocal);
173 native int nLightCreate();
174 native void nLightDestroy(int l);
175 native void nLightSetColor(int l, float r, float g, float b);
176 native void nLightSetPosition(int l, float x, float y, float z);
Jason Samsbba134c2009-06-22 15:49:21 -0700177
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700178
179 private int mDev;
180 private int mContext;
181 private Surface mSurface;
182
Jason Sams36e612a2009-07-31 16:26:13 -0700183 private static boolean mElementsInitialized = false;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700184
185 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700186 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700187
188 RenderScript(Surface sur) {
189 mSurface = sur;
190 mDev = nDeviceCreate();
191 mContext = nContextCreate(mDev, mSurface, 0);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700192
Jason Sams36e612a2009-07-31 16:26:13 -0700193 // TODO: This should be protected by a lock
194 if(!mElementsInitialized) {
195 Element.init(this);
196 mElementsInitialized = true;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700197 }
198 }
199
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700200 //////////////////////////////////////////////////////////////////////////////////
201 // Element
202
Jason Sams36e612a2009-07-31 16:26:13 -0700203 Element.Builder mElementBuilder = new Element.Builder(this);
204 public Element.Builder elementBuilderCreate() throws IllegalStateException {
205 mElementBuilder.begin();
206 return mElementBuilder;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700207 }
208
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700209
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700210
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700211
212 public enum DepthFunc {
213 ALWAYS (0),
214 LESS (1),
215 LEQUAL (2),
216 GREATER (3),
217 GEQUAL (4),
218 EQUAL (5),
219 NOTEQUAL (6);
220
221 int mID;
222 DepthFunc(int id) {
223 mID = id;
224 }
225 }
226
227 public enum BlendSrcFunc {
228 ZERO (0),
229 ONE (1),
230 DST_COLOR (2),
231 ONE_MINUS_DST_COLOR (3),
232 SRC_ALPHA (4),
233 ONE_MINUS_SRC_ALPHA (5),
234 DST_ALPHA (6),
235 ONE_MINUS_DST_ALPA (7),
236 SRC_ALPHA_SATURATE (8);
237
238 int mID;
239 BlendSrcFunc(int id) {
240 mID = id;
241 }
242 }
243
244 public enum BlendDstFunc {
245 ZERO (0),
246 ONE (1),
247 SRC_COLOR (2),
248 ONE_MINUS_SRC_COLOR (3),
249 SRC_ALPHA (4),
250 ONE_MINUS_SRC_ALPHA (5),
251 DST_ALPHA (6),
252 ONE_MINUS_DST_ALPA (7);
253
254 int mID;
255 BlendDstFunc(int id) {
256 mID = id;
257 }
258 }
259
260 public enum EnvMode {
261 REPLACE (0),
262 MODULATE (1),
263 DECAL (2);
264
265 int mID;
266 EnvMode(int id) {
267 mID = id;
268 }
269 }
270
Jason Sams02fb2cb2009-05-28 15:37:57 -0700271 public enum SamplerParam {
272 FILTER_MIN (0),
273 FILTER_MAG (1),
274 WRAP_MODE_S (2),
275 WRAP_MODE_T (3),
276 WRAP_MODE_R (4);
277
278 int mID;
279 SamplerParam(int id) {
280 mID = id;
281 }
282 }
283
284 public enum SamplerValue {
285 NEAREST (0),
286 LINEAR (1),
287 LINEAR_MIP_LINEAR (2),
288 WRAP (3),
289 CLAMP (4);
290
291 int mID;
292 SamplerValue(int id) {
293 mID = id;
294 }
295 }
296
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700297 //////////////////////////////////////////////////////////////////////////////////
298 // Type
299
300 public enum Dimension {
301 X (0),
302 Y (1),
303 Z (2),
304 LOD (3),
305 FACE (4),
306 ARRAY_0 (100);
307
308 int mID;
309 Dimension(int id) {
310 mID = id;
311 }
312 }
313
314 public class Type extends BaseObj {
315 Type(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700316 super(RenderScript.this);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700317 mID = id;
318 }
319
320 public void destroy() {
321 nTypeDestroy(mID);
322 mID = 0;
323 }
324 }
325
326 public void typeBegin(Element e) {
327 nTypeBegin(e.mID);
328 }
329
330 public void typeAdd(Dimension d, int value) {
331 nTypeAdd(d.mID, value);
332 }
333
334 public Type typeCreate() {
335 int id = nTypeCreate();
336 return new Type(id);
337 }
338
339
340 //////////////////////////////////////////////////////////////////////////////////
341 // Allocation
342
343 public class Allocation extends BaseObj {
344 Allocation(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700345 super(RenderScript.this);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700346 mID = id;
347 }
348
349 public void uploadToTexture(int baseMipLevel) {
350 nAllocationUploadToTexture(mID, baseMipLevel);
351 }
352
353 public void destroy() {
354 nAllocationDestroy(mID);
355 mID = 0;
356 }
357
358 public void data(int[] d) {
359 nAllocationData(mID, d);
360 }
361
362 public void data(float[] d) {
363 nAllocationData(mID, d);
364 }
365
366 public void subData1D(int off, int count, int[] d) {
367 nAllocationSubData1D(mID, off, count, d);
368 }
369
370 public void subData1D(int off, int count, float[] d) {
371 nAllocationSubData1D(mID, off, count, d);
372 }
373
374 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
375 nAllocationSubData2D(mID, xoff, yoff, w, h, d);
376 }
377
378 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
379 nAllocationSubData2D(mID, xoff, yoff, w, h, d);
380 }
381 }
382
383 public Allocation allocationCreateTyped(Type type) {
384 int id = nAllocationCreateTyped(type.mID);
385 return new Allocation(id);
386 }
387
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700388 public Allocation allocationCreateSized(Element e, int count) {
Jason Sams36e612a2009-07-31 16:26:13 -0700389 int id;
390 if(e.mIsPredefined) {
391 id = nAllocationCreatePredefSized(e.mPredefinedID, count);
392 } else {
393 id = nAllocationCreateSized(e.mID, count);
394 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700395 return new Allocation(id);
396 }
397
Jason Sams36e612a2009-07-31 16:26:13 -0700398 public Allocation allocationCreateFromBitmap(Bitmap b, Element dstFmt, boolean genMips)
399 throws IllegalArgumentException {
400 if(!dstFmt.mIsPredefined) {
401 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
402 }
403
404 int id = nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700405 return new Allocation(id);
406 }
407
Jason Sams36e612a2009-07-31 16:26:13 -0700408 public Allocation allocationCreateFromBitmapBoxed(Bitmap b, Element dstFmt, boolean genMips)
409 throws IllegalArgumentException {
410 if(!dstFmt.mIsPredefined) {
411 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
412 }
413
414 int id = nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b);
Jason Samsfe08d992009-05-27 14:45:32 -0700415 return new Allocation(id);
416 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700417
Jason Sams36e612a2009-07-31 16:26:13 -0700418 public Allocation allocationCreateFromBitmapResource(Resources res, int id, Element dstFmt, boolean genMips)
419 throws IllegalArgumentException {
420
Jason Samsdba3ba52009-07-30 14:56:12 -0700421 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
Jason Sams36e612a2009-07-31 16:26:13 -0700422 return allocationCreateFromBitmap(b, dstFmt, genMips);
Jason Samsdba3ba52009-07-30 14:56:12 -0700423 }
424
Jason Sams36e612a2009-07-31 16:26:13 -0700425 public Allocation allocationCreateFromBitmapResourceBoxed(Resources res, int id, Element dstFmt, boolean genMips)
426 throws IllegalArgumentException {
427
Jason Samsdba3ba52009-07-30 14:56:12 -0700428 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
Jason Sams36e612a2009-07-31 16:26:13 -0700429 return allocationCreateFromBitmapBoxed(b, dstFmt, genMips);
Jason Samsdba3ba52009-07-30 14:56:12 -0700430 }
431
432
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700433 //////////////////////////////////////////////////////////////////////////////////
434 // Adapter1D
435
436 public class Adapter1D extends BaseObj {
437 Adapter1D(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700438 super(RenderScript.this);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700439 mID = id;
440 }
441
442 public void destroy() {
443 nAdapter1DDestroy(mID);
444 mID = 0;
445 }
446
447 public void bindAllocation(Allocation a) {
448 nAdapter1DBindAllocation(mID, a.mID);
449 }
450
451 public void setConstraint(Dimension dim, int value) {
452 nAdapter1DSetConstraint(mID, dim.mID, value);
453 }
454
455 public void data(int[] d) {
456 nAdapter1DData(mID, d);
457 }
458
459 public void subData(int off, int count, int[] d) {
460 nAdapter1DSubData(mID, off, count, d);
461 }
462
463 public void data(float[] d) {
464 nAdapter1DData(mID, d);
465 }
466
467 public void subData(int off, int count, float[] d) {
468 nAdapter1DSubData(mID, off, count, d);
469 }
470 }
471
472 public Adapter1D adapter1DCreate() {
473 int id = nAdapter1DCreate();
474 return new Adapter1D(id);
475 }
476
477
478 //////////////////////////////////////////////////////////////////////////////////
479 // Triangle Mesh
480
481 public class TriangleMesh extends BaseObj {
482 TriangleMesh(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700483 super(RenderScript.this);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700484 mID = id;
485 }
486
487 public void destroy() {
488 nTriangleMeshDestroy(mID);
489 mID = 0;
490 }
491 }
492
493 public void triangleMeshBegin(Element vertex, Element index) {
Jason Sams36e612a2009-07-31 16:26:13 -0700494 Log.e("rs", "vtx " + vertex.toString() + " " + vertex.mID + " " + vertex.mPredefinedID);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700495 nTriangleMeshBegin(vertex.mID, index.mID);
496 }
497
498 public void triangleMeshAddVertex_XY(float x, float y) {
499 nTriangleMeshAddVertex_XY(x, y);
500 }
501
502 public void triangleMeshAddVertex_XYZ(float x, float y, float z) {
503 nTriangleMeshAddVertex_XYZ(x, y, z);
504 }
505
506 public void triangleMeshAddVertex_XY_ST(float x, float y, float s, float t) {
507 nTriangleMeshAddVertex_XY_ST(x, y, s, t);
508 }
509
510 public void triangleMeshAddVertex_XYZ_ST(float x, float y, float z, float s, float t) {
511 nTriangleMeshAddVertex_XYZ_ST(x, y, z, s, t);
512 }
513
Jason Sams0826a6f2009-06-15 19:04:56 -0700514 public void triangleMeshAddVertex_XYZ_ST_NORM(float x, float y, float z, float s, float t, float nx, float ny, float nz) {
515 nTriangleMeshAddVertex_XYZ_ST_NORM(x, y, z, s, t, nx, ny, nz);
516 }
517
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700518 public void triangleMeshAddTriangle(int i1, int i2, int i3) {
519 nTriangleMeshAddTriangle(i1, i2, i3);
520 }
521
522 public TriangleMesh triangleMeshCreate() {
523 int id = nTriangleMeshCreate();
524 return new TriangleMesh(id);
525 }
526
527 //////////////////////////////////////////////////////////////////////////////////
528 // Script
529
530 public class Script extends BaseObj {
531 Script(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700532 super(RenderScript.this);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700533 mID = id;
534 }
535
536 public void destroy() {
537 nScriptDestroy(mID);
538 mID = 0;
539 }
540
541 public void bindAllocation(Allocation va, int slot) {
542 nScriptBindAllocation(mID, va.mID, slot);
543 }
544 }
545
546 public void scriptCBegin() {
547 nScriptCBegin();
548 }
549
Romain Guy584a3752009-07-30 18:45:01 -0700550 public void scriptCSetTimeZone(String timeZone) {
551 try {
552 byte[] bytes = timeZone.getBytes("UTF-8");
Jason Sams36e612a2009-07-31 16:26:13 -0700553 nScriptCSetTimeZone(bytes);
Romain Guy584a3752009-07-30 18:45:01 -0700554 } catch (java.io.UnsupportedEncodingException e) {
555 throw new RuntimeException(e);
556 }
557 }
Jason Sams36e612a2009-07-31 16:26:13 -0700558
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700559 public void scriptCSetClearColor(float r, float g, float b, float a) {
560 nScriptCSetClearColor(r, g, b, a);
561 }
562
563 public void scriptCSetClearDepth(float d) {
564 nScriptCSetClearDepth(d);
565 }
566
567 public void scriptCSetClearStencil(int stencil) {
568 nScriptCSetClearStencil(stencil);
569 }
570
571 public void scriptCAddType(Type t) {
572 nScriptCAddType(t.mID);
573 }
574
575 public void scriptCSetRoot(boolean r) {
576 nScriptCSetRoot(r);
577 }
578
579 public void scriptCSetScript(String s) {
Jack Palevich43702d82009-05-28 13:38:16 -0700580 try {
Jack Palevich63975dd2009-05-28 15:34:15 -0700581 byte[] bytes = s.getBytes("UTF-8");
582 nScriptCSetScript(bytes, 0, bytes.length);
Jack Palevich43702d82009-05-28 13:38:16 -0700583 } catch (java.io.UnsupportedEncodingException e) {
584 throw new RuntimeException(e);
585 }
586 }
587
Jack Palevich43702d82009-05-28 13:38:16 -0700588 public void scriptCSetScript(Resources resources, int id) {
589 InputStream is = resources.openRawResource(id);
590 try {
591 try {
592 scriptCSetScript(is);
593 } finally {
594 is.close();
595 }
596 } catch(IOException e) {
597 throw new Resources.NotFoundException();
598 }
599 }
600
601 public void scriptCSetScript(InputStream is) throws IOException {
602 byte[] buf = new byte[1024];
603 int currentPos = 0;
604 while(true) {
605 int bytesLeft = buf.length - currentPos;
606 if (bytesLeft == 0) {
607 byte[] buf2 = new byte[buf.length * 2];
608 System.arraycopy(buf, 0, buf2, 0, buf.length);
609 buf = buf2;
610 bytesLeft = buf.length - currentPos;
611 }
612 int bytesRead = is.read(buf, currentPos, bytesLeft);
613 if (bytesRead <= 0) {
614 break;
615 }
616 currentPos += bytesRead;
617 }
618 nScriptCSetScript(buf, 0, currentPos);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700619 }
620
621 public Script scriptCCreate() {
622 int id = nScriptCCreate();
623 return new Script(id);
624 }
625
626 //////////////////////////////////////////////////////////////////////////////////
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700627 // ProgramVertex
628
629 public class ProgramVertex extends BaseObj {
630 ProgramVertex(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700631 super(RenderScript.this);
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700632 mID = id;
633 }
634
635 public void destroy() {
636 nProgramVertexDestroy(mID);
637 mID = 0;
638 }
639
640 public void bindAllocation(int slot, Allocation va) {
641 nProgramVertexBindAllocation(mID, slot, va.mID);
642 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700643 }
644
645 public void programVertexBegin(Element in, Element out) {
646 int inID = 0;
647 int outID = 0;
648 if (in != null) {
649 inID = in.mID;
650 }
651 if (out != null) {
652 outID = out.mID;
653 }
654 nProgramVertexBegin(inID, outID);
655 }
656
657 public void programVertexSetType(int slot, Type t) {
658 nProgramVertexSetType(slot, t.mID);
659 }
660
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700661 public void programVertexSetTextureMatrixEnable(boolean enable) {
662 nProgramVertexSetTextureMatrixEnable(enable);
663 }
664
Jason Samsee411122009-07-21 12:20:54 -0700665 public void programVertexAddLight(Light l) {
666 nProgramVertexAddLight(l.mID);
667 }
668
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700669 public ProgramVertex programVertexCreate() {
670 int id = nProgramVertexCreate();
671 return new ProgramVertex(id);
672 }
673
674
675 //////////////////////////////////////////////////////////////////////////////////
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700676 // ProgramFragmentStore
677
678 public class ProgramFragmentStore extends BaseObj {
679 ProgramFragmentStore(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700680 super(RenderScript.this);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700681 mID = id;
682 }
683
684 public void destroy() {
Jason Sams3eaa3382009-06-10 15:04:38 -0700685 nProgramFragmentStoreDestroy(mID);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700686 mID = 0;
687 }
688 }
689
690 public void programFragmentStoreBegin(Element in, Element out) {
691 int inID = 0;
692 int outID = 0;
693 if (in != null) {
694 inID = in.mID;
695 }
696 if (out != null) {
697 outID = out.mID;
698 }
699 nProgramFragmentStoreBegin(inID, outID);
700 }
701
702 public void programFragmentStoreDepthFunc(DepthFunc func) {
703 nProgramFragmentStoreDepthFunc(func.mID);
704 }
705
706 public void programFragmentStoreDepthMask(boolean enable) {
707 nProgramFragmentStoreDepthMask(enable);
708 }
709
710 public void programFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
711 nProgramFragmentStoreColorMask(r,g,b,a);
712 }
713
714 public void programFragmentStoreBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
715 nProgramFragmentStoreBlendFunc(src.mID, dst.mID);
716 }
717
718 public void programFragmentStoreDitherEnable(boolean enable) {
719 nProgramFragmentStoreDither(enable);
720 }
721
722 public ProgramFragmentStore programFragmentStoreCreate() {
723 int id = nProgramFragmentStoreCreate();
724 return new ProgramFragmentStore(id);
725 }
726
727 //////////////////////////////////////////////////////////////////////////////////
728 // ProgramFragment
729
730 public class ProgramFragment extends BaseObj {
731 ProgramFragment(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700732 super(RenderScript.this);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700733 mID = id;
734 }
735
736 public void destroy() {
Jason Sams3eaa3382009-06-10 15:04:38 -0700737 nProgramFragmentDestroy(mID);
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700738 mID = 0;
739 }
740
741 public void bindTexture(Allocation va, int slot) {
742 nProgramFragmentBindTexture(mID, slot, va.mID);
743 }
744
Jason Sams02fb2cb2009-05-28 15:37:57 -0700745 public void bindSampler(Sampler vs, int slot) {
746 nProgramFragmentBindSampler(mID, slot, vs.mID);
747 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700748 }
749
750 public void programFragmentBegin(Element in, Element out) {
751 int inID = 0;
752 int outID = 0;
753 if (in != null) {
754 inID = in.mID;
755 }
756 if (out != null) {
757 outID = out.mID;
758 }
759 nProgramFragmentBegin(inID, outID);
760 }
761
762 public void programFragmentSetType(int slot, Type t) {
763 nProgramFragmentSetType(slot, t.mID);
764 }
765
766 public void programFragmentSetType(int slot, EnvMode t) {
767 nProgramFragmentSetEnvMode(slot, t.mID);
768 }
769
770 public void programFragmentSetTexEnable(int slot, boolean enable) {
771 nProgramFragmentSetTexEnable(slot, enable);
772 }
773
Jason Samse6c8e9b2009-07-17 17:29:09 -0700774 public void programFragmentSetTexEnvMode(int slot, EnvMode env) {
775 nProgramFragmentSetEnvMode(slot, env.mID);
776 }
777
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700778 public ProgramFragment programFragmentCreate() {
779 int id = nProgramFragmentCreate();
780 return new ProgramFragment(id);
781 }
782
Jason Sams02fb2cb2009-05-28 15:37:57 -0700783 //////////////////////////////////////////////////////////////////////////////////
784 // Sampler
785
786 public class Sampler extends BaseObj {
787 Sampler(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700788 super(RenderScript.this);
Jason Sams02fb2cb2009-05-28 15:37:57 -0700789 mID = id;
790 }
791
792 public void destroy() {
793 nSamplerDestroy(mID);
794 mID = 0;
795 }
796 }
797
798 public void samplerBegin() {
799 nSamplerBegin();
800 }
801
802 public void samplerSet(SamplerParam p, SamplerValue v) {
803 nSamplerSet(p.mID, v.mID);
804 }
805
806 public Sampler samplerCreate() {
807 int id = nSamplerCreate();
808 return new Sampler(id);
809 }
810
Jason Samsbba134c2009-06-22 15:49:21 -0700811 //////////////////////////////////////////////////////////////////////////////////
812 // Light
813
814 public class Light extends BaseObj {
815 Light(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700816 super(RenderScript.this);
Jason Samsbba134c2009-06-22 15:49:21 -0700817 mID = id;
818 }
819
820 public void destroy() {
821 nLightDestroy(mID);
822 mID = 0;
823 }
824
825 public void setColor(float r, float g, float b) {
826 nLightSetColor(mID, r, g, b);
827 }
828
829 public void setPosition(float x, float y, float z) {
830 nLightSetPosition(mID, x, y, z);
831 }
832 }
833
834 public void lightBegin() {
835 nLightBegin();
836 }
837
838 public void lightSetIsMono(boolean isMono) {
839 nLightSetIsMono(isMono);
840 }
841
842 public void lightSetIsLocal(boolean isLocal) {
843 nLightSetIsLocal(isLocal);
844 }
845
846 public Light lightCreate() {
847 int id = nLightCreate();
848 return new Light(id);
849 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700850
Jason Sams64676f32009-07-08 18:01:53 -0700851 //////////////////////////////////////////////////////////////////////////////////
852 // File
853
854 public class File extends BaseObj {
855 File(int id) {
Jason Sams36e612a2009-07-31 16:26:13 -0700856 super(RenderScript.this);
Jason Sams64676f32009-07-08 18:01:53 -0700857 mID = id;
858 }
859
860 public void destroy() {
861 //nLightDestroy(mID);
862 mID = 0;
863 }
864 }
865
866 public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException
867 {
868 if(s.length() < 1) {
869 throw new IllegalArgumentException("fileOpen does not accept a zero length string.");
870 }
871
872 try {
873 byte[] bytes = s.getBytes("UTF-8");
874 int id = nFileOpen(bytes);
875 return new File(id);
876 } catch (java.io.UnsupportedEncodingException e) {
877 throw new RuntimeException(e);
878 }
879 }
880
881
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700882 ///////////////////////////////////////////////////////////////////////////////////
883 // Root state
884
885 public void contextBindRootScript(Script s) {
886 nContextBindRootScript(s.mID);
887 }
888
889 //public void contextBindSampler(Sampler s, int slot) {
890 //nContextBindSampler(s.mID);
891 //}
892
893 public void contextBindProgramFragmentStore(ProgramFragmentStore pfs) {
894 nContextBindProgramFragmentStore(pfs.mID);
895 }
896
897 public void contextBindProgramFragment(ProgramFragment pf) {
898 nContextBindProgramFragment(pf.mID);
899 }
900
Jason Sams0826a6f2009-06-15 19:04:56 -0700901 public void contextBindProgramVertex(ProgramVertex pf) {
902 nContextBindProgramVertex(pf.mID);
903 }
904
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700905/*
906 RsAdapter2D rsAdapter2DCreate ();
907 void rsAdapter2DBindAllocation (RsAdapter2D adapt, RsAllocation alloc);
908 void rsAdapter2DDestroy (RsAdapter2D adapter);
909 void rsAdapter2DSetConstraint (RsAdapter2D adapter, RsDimension dim, uint32_t value);
910 void rsAdapter2DData (RsAdapter2D adapter, const void * data);
911 void rsAdapter2DSubData (RsAdapter2D adapter, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void * data);
912 void rsSamplerBegin ();
913 void rsSamplerSet (RsSamplerParam p, RsSamplerValue value);
914 RsSampler rsSamplerCreate ();
915 void rsSamplerBind (RsSampler sampler, RsAllocation alloc);
916*/
917
918}
919
Jason Sams36e612a2009-07-31 16:26:13 -0700920