blob: bab966618cffd7af80f9991ca7392afa389cb479 [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
17package com.android.calc;
18
19import android.os.Bundle;
20import android.util.Log;
21import android.util.Config;
22import android.view.Menu;
23import android.view.MenuItem;
24import android.view.Window;
25import android.view.View;
26import android.view.Surface;
27
28public class RenderScript {
29 private static final String LOG_TAG = "libRS_jni";
30 private static final boolean DEBUG = false;
31 private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
32
33
34
35 /*
36 * We use a class initializer to allow the native code to cache some
37 * field offsets.
38 */
39 private static boolean sInitialized;
40 native private static void _nInit();
41
42 static {
43 sInitialized = false;
44 try {
45 System.loadLibrary("RS_jni");
46 Log.e(LOG_TAG, "*** Renderscript INIT");
47 _nInit();
48 Log.e(LOG_TAG, "*** Renderscript INIT 3");
49 sInitialized = true;
50 } catch (UnsatisfiedLinkError e) {
51 Log.d(LOG_TAG, "RenderScript JNI library not found!");
52 }
53 }
54
55 native private int nDeviceCreate();
56 native private void nDeviceDestroy(int dev);
57 native private int nContextCreate(int dev, Surface sur, int ver);
58 native private void nContextDestroy(int con);
59
60 //void rsContextBindSampler (uint32_t slot, RsSampler sampler);
61 //void rsContextBindRootScript (RsScript sampler);
62 native private void nContextBindRootScript(int script);
63 native private void nContextBindSampler(int sampler, int slot);
64 native private void nContextBindProgramFragmentStore(int pfs);
65 native private void nContextBindProgramFragment(int pf);
66
67 native private void nElementBegin();
68 native private void nElementAddPredefined(int predef);
69 native private void nElementAdd(int kind, int type, int norm, int bits);
70 native private int nElementCreate();
71 native private int nElementGetPredefined(int predef);
72 native private void nElementDestroy(int obj);
73
74 native private void nTypeBegin(int elementID);
75 native private void nTypeAdd(int dim, int val);
76 native private int nTypeCreate();
77 native private void nTypeDestroy(int id);
78
79 native private int nAllocationCreateTyped(int type);
80 native private int nAllocationCreatePredefSized(int predef, int count);
81 native private int nAllocationCreateSized(int elem, int count);
82 //native private int nAllocationCreateFromBitmap(type.mID);
83 native private void nAllocationUploadToTexture(int alloc, int baseMioLevel);
84 native private void nAllocationDestroy(int alloc);
85 native private void nAllocationData(int id, int[] d);
86 native private void nAllocationData(int id, float[] d);
87 native private void nAllocationSubData1D(int id, int off, int count, int[] d);
88 native private void nAllocationSubData1D(int id, int off, int count, float[] d);
89 native private void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d);
90 native private void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d);
91
92 native private void nTriangleMeshDestroy(int id);
93 native private void nTriangleMeshBegin(int vertex, int index);
94 native private void nTriangleMeshAddVertex_XY (float x, float y);
95 native private void nTriangleMeshAddVertex_XYZ (float x, float y, float z);
96 native private void nTriangleMeshAddVertex_XY_ST (float x, float y, float s, float t);
97 native private void nTriangleMeshAddVertex_XYZ_ST (float x, float y, float z, float s, float t);
98 native private void nTriangleMeshAddTriangle(int i1, int i2, int i3);
99 native private int nTriangleMeshCreate();
100
101 native private void nAdapter1DDestroy(int id);
102 native private void nAdapter1DBindAllocation(int ad, int alloc);
103 native private void nAdapter1DSetConstraint(int ad, int dim, int value);
104 native private void nAdapter1DData(int ad, int[] d);
105 native private void nAdapter1DSubData(int ad, int off, int count, int[] d);
106 native private void nAdapter1DData(int ad, float[] d);
107 native private void nAdapter1DSubData(int ad, int off, int count, float[] d);
108 native private int nAdapter1DCreate();
109
110 native private void nScriptDestroy(int script);
111 native private void nScriptBindAllocation(int vtm, int alloc, int slot);
112 native private void nScriptCBegin();
113 native private void nScriptCSetClearColor(float r, float g, float b, float a);
114 native private void nScriptCSetClearDepth(float depth);
115 native private void nScriptCSetClearStencil(int stencil);
116 native private void nScriptCAddType(int type);
117 native private void nScriptCSetRoot(boolean isRoot);
118 native private void nScriptCSetScript(String s);
119 native private int nScriptCCreate();
120
121
122 native private void nProgramFragmentStoreBegin(int in, int out);
123 native private void nProgramFragmentStoreDepthFunc(int func);
124 native private void nProgramFragmentStoreDepthMask(boolean enable);
125 native private void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a);
126 native private void nProgramFragmentStoreBlendFunc(int src, int dst);
127 native private void nProgramFragmentStoreDither(boolean enable);
128 native private int nProgramFragmentStoreCreate();
129
130 native private void nProgramFragmentBegin(int in, int out);
131 native private void nProgramFragmentBindTexture(int vpf, int slot, int a);
132 native private void nProgramFragmentBindSampler(int vpf, int slot, int s);
133 native private void nProgramFragmentSetType(int slot, int vt);
134 native private void nProgramFragmentSetEnvMode(int slot, int env);
135 native private void nProgramFragmentSetTexEnable(int slot, boolean enable);
136 native private int nProgramFragmentCreate();
137
138
139 private int mDev;
140 private int mContext;
141 private Surface mSurface;
142
143
144
145 ///////////////////////////////////////////////////////////////////////////////////
146 //
147
148 RenderScript(Surface sur) {
149 mSurface = sur;
150 mDev = nDeviceCreate();
151 mContext = nContextCreate(mDev, mSurface, 0);
152 }
153
154 private class BaseObj {
155 BaseObj() {
156 mID = 0;
157 }
158
159 int mID;
160 protected void finalize() throws Throwable
161 {
162 if (mID != 0) {
163 Log.v(LOG_TAG,
164 "Element finalized without having released the RS reference.");
165 }
166 super.finalize();
167 }
168 }
169
170
171 //////////////////////////////////////////////////////////////////////////////////
172 // Element
173
174 public enum ElementPredefined {
175 USER_U8 (0),
176 USER_I8 (1),
177 USER_U16 (2),
178 USER_I16 (3),
179 USER_U32 (4),
180 USER_I32 (5),
181 USER_FLOAT (6),
182
183 RGB_565 (7),
184 RGBA_5551 (8),
185 RGBA_4444 (9),
186 RGB_888 (10),
187 RGBA_8888 (11),
188
189 INDEX_16 (12),
190 INDEX_32 (13),
191 XY_F32 (14),
192 XYZ_F32 (15),
193 ST_XY_F32 (16),
194 ST_XYZ_F32 (17),
195 NORM_XYZ_F32 (18),
196 NORM_ST_XYZ_F32 (19);
197
198 int mID;
199 ElementPredefined(int id) {
200 mID = id;
201 }
202 }
203
204 public enum DataType {
205 FLOAT (0),
206 UNSIGNED (1),
207 SIGNED (2);
208
209 int mID;
210 DataType(int id) {
211 mID = id;
212 }
213 }
214
215 public enum DataKind {
216 USER (0),
217 RED (1),
218 GREEN (2),
219 BLUE (3),
220 ALPHA (4),
221 LUMINANCE (5),
222 INTENSITY (6),
223 X (7),
224 Y (8),
225 Z (9),
226 W (10),
227 S (11),
228 T (12),
229 Q (13),
230 R (14),
231 NX (15),
232 NY (16),
233 NZ (17),
234 INDEX (18);
235
236 int mID;
237 DataKind(int id) {
238 mID = id;
239 }
240 }
241
242 public enum DepthFunc {
243 ALWAYS (0),
244 LESS (1),
245 LEQUAL (2),
246 GREATER (3),
247 GEQUAL (4),
248 EQUAL (5),
249 NOTEQUAL (6);
250
251 int mID;
252 DepthFunc(int id) {
253 mID = id;
254 }
255 }
256
257 public enum BlendSrcFunc {
258 ZERO (0),
259 ONE (1),
260 DST_COLOR (2),
261 ONE_MINUS_DST_COLOR (3),
262 SRC_ALPHA (4),
263 ONE_MINUS_SRC_ALPHA (5),
264 DST_ALPHA (6),
265 ONE_MINUS_DST_ALPA (7),
266 SRC_ALPHA_SATURATE (8);
267
268 int mID;
269 BlendSrcFunc(int id) {
270 mID = id;
271 }
272 }
273
274 public enum BlendDstFunc {
275 ZERO (0),
276 ONE (1),
277 SRC_COLOR (2),
278 ONE_MINUS_SRC_COLOR (3),
279 SRC_ALPHA (4),
280 ONE_MINUS_SRC_ALPHA (5),
281 DST_ALPHA (6),
282 ONE_MINUS_DST_ALPA (7);
283
284 int mID;
285 BlendDstFunc(int id) {
286 mID = id;
287 }
288 }
289
290 public enum EnvMode {
291 REPLACE (0),
292 MODULATE (1),
293 DECAL (2);
294
295 int mID;
296 EnvMode(int id) {
297 mID = id;
298 }
299 }
300
301 public class Element extends BaseObj {
302 Element(int id) {
303 mID = id;
304 }
305
306 public void estroy() {
307 nElementDestroy(mID);
308 mID = 0;
309 }
310 }
311
312 public void elementBegin() {
313 nElementBegin();
314 }
315
316 public void elementAddPredefined(ElementPredefined e) {
317 nElementAddPredefined(e.mID);
318 }
319
320 public void elementAdd(DataType dt, DataKind dk, boolean isNormalized, int bits) {
321 int norm = 0;
322 if (isNormalized) {
323 norm = 1;
324 }
325 nElementAdd(dt.mID, dk.mID, norm, bits);
326 }
327
328 public Element elementCreate() {
329 int id = nElementCreate();
330 return new Element(id);
331 }
332
333 public Element elementGetPredefined(ElementPredefined predef) {
334 int id = nElementGetPredefined(predef.mID);
335 return new Element(id);
336 }
337
338
339 //////////////////////////////////////////////////////////////////////////////////
340 // Type
341
342 public enum Dimension {
343 X (0),
344 Y (1),
345 Z (2),
346 LOD (3),
347 FACE (4),
348 ARRAY_0 (100);
349
350 int mID;
351 Dimension(int id) {
352 mID = id;
353 }
354 }
355
356 public class Type extends BaseObj {
357 Type(int id) {
358 mID = id;
359 }
360
361 public void destroy() {
362 nTypeDestroy(mID);
363 mID = 0;
364 }
365 }
366
367 public void typeBegin(Element e) {
368 nTypeBegin(e.mID);
369 }
370
371 public void typeAdd(Dimension d, int value) {
372 nTypeAdd(d.mID, value);
373 }
374
375 public Type typeCreate() {
376 int id = nTypeCreate();
377 return new Type(id);
378 }
379
380
381 //////////////////////////////////////////////////////////////////////////////////
382 // Allocation
383
384 public class Allocation extends BaseObj {
385 Allocation(int id) {
386 mID = id;
387 }
388
389 public void uploadToTexture(int baseMipLevel) {
390 nAllocationUploadToTexture(mID, baseMipLevel);
391 }
392
393 public void destroy() {
394 nAllocationDestroy(mID);
395 mID = 0;
396 }
397
398 public void data(int[] d) {
399 nAllocationData(mID, d);
400 }
401
402 public void data(float[] d) {
403 nAllocationData(mID, d);
404 }
405
406 public void subData1D(int off, int count, int[] d) {
407 nAllocationSubData1D(mID, off, count, d);
408 }
409
410 public void subData1D(int off, int count, float[] d) {
411 nAllocationSubData1D(mID, off, count, d);
412 }
413
414 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
415 nAllocationSubData2D(mID, xoff, yoff, w, h, d);
416 }
417
418 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
419 nAllocationSubData2D(mID, xoff, yoff, w, h, d);
420 }
421 }
422
423 public Allocation allocationCreateTyped(Type type) {
424 int id = nAllocationCreateTyped(type.mID);
425 return new Allocation(id);
426 }
427
428 public Allocation allocationCreatePredefSized(ElementPredefined e, int count) {
429 int id = nAllocationCreatePredefSized(e.mID, count);
430 return new Allocation(id);
431 }
432
433 public Allocation allocationCreateSized(Element e, int count) {
434 int id = nAllocationCreateSized(e.mID, count);
435 return new Allocation(id);
436 }
437
438 //public Allocation allocationCreateFromBitmap(string file, boolean genMips) {
439 //int id = nAllocationCreateTyped(type.mID);
440 //return new Allocation(id);
441 //}
442
443 //////////////////////////////////////////////////////////////////////////////////
444 // Adapter1D
445
446 public class Adapter1D extends BaseObj {
447 Adapter1D(int id) {
448 mID = id;
449 }
450
451 public void destroy() {
452 nAdapter1DDestroy(mID);
453 mID = 0;
454 }
455
456 public void bindAllocation(Allocation a) {
457 nAdapter1DBindAllocation(mID, a.mID);
458 }
459
460 public void setConstraint(Dimension dim, int value) {
461 nAdapter1DSetConstraint(mID, dim.mID, value);
462 }
463
464 public void data(int[] d) {
465 nAdapter1DData(mID, d);
466 }
467
468 public void subData(int off, int count, int[] d) {
469 nAdapter1DSubData(mID, off, count, d);
470 }
471
472 public void data(float[] d) {
473 nAdapter1DData(mID, d);
474 }
475
476 public void subData(int off, int count, float[] d) {
477 nAdapter1DSubData(mID, off, count, d);
478 }
479 }
480
481 public Adapter1D adapter1DCreate() {
482 int id = nAdapter1DCreate();
483 return new Adapter1D(id);
484 }
485
486
487 //////////////////////////////////////////////////////////////////////////////////
488 // Triangle Mesh
489
490 public class TriangleMesh extends BaseObj {
491 TriangleMesh(int id) {
492 mID = id;
493 }
494
495 public void destroy() {
496 nTriangleMeshDestroy(mID);
497 mID = 0;
498 }
499 }
500
501 public void triangleMeshBegin(Element vertex, Element index) {
502 nTriangleMeshBegin(vertex.mID, index.mID);
503 }
504
505 public void triangleMeshAddVertex_XY(float x, float y) {
506 nTriangleMeshAddVertex_XY(x, y);
507 }
508
509 public void triangleMeshAddVertex_XYZ(float x, float y, float z) {
510 nTriangleMeshAddVertex_XYZ(x, y, z);
511 }
512
513 public void triangleMeshAddVertex_XY_ST(float x, float y, float s, float t) {
514 nTriangleMeshAddVertex_XY_ST(x, y, s, t);
515 }
516
517 public void triangleMeshAddVertex_XYZ_ST(float x, float y, float z, float s, float t) {
518 nTriangleMeshAddVertex_XYZ_ST(x, y, z, s, t);
519 }
520
521 public void triangleMeshAddTriangle(int i1, int i2, int i3) {
522 nTriangleMeshAddTriangle(i1, i2, i3);
523 }
524
525 public TriangleMesh triangleMeshCreate() {
526 int id = nTriangleMeshCreate();
527 return new TriangleMesh(id);
528 }
529
530 //////////////////////////////////////////////////////////////////////////////////
531 // Script
532
533 public class Script extends BaseObj {
534 Script(int id) {
535 mID = id;
536 }
537
538 public void destroy() {
539 nScriptDestroy(mID);
540 mID = 0;
541 }
542
543 public void bindAllocation(Allocation va, int slot) {
544 nScriptBindAllocation(mID, va.mID, slot);
545 }
546 }
547
548 public void scriptCBegin() {
549 nScriptCBegin();
550 }
551
552 public void scriptCSetClearColor(float r, float g, float b, float a) {
553 nScriptCSetClearColor(r, g, b, a);
554 }
555
556 public void scriptCSetClearDepth(float d) {
557 nScriptCSetClearDepth(d);
558 }
559
560 public void scriptCSetClearStencil(int stencil) {
561 nScriptCSetClearStencil(stencil);
562 }
563
564 public void scriptCAddType(Type t) {
565 nScriptCAddType(t.mID);
566 }
567
568 public void scriptCSetRoot(boolean r) {
569 nScriptCSetRoot(r);
570 }
571
572 public void scriptCSetScript(String s) {
573 nScriptCSetScript(s);
574 }
575
576 public Script scriptCCreate() {
577 int id = nScriptCCreate();
578 return new Script(id);
579 }
580
581 //////////////////////////////////////////////////////////////////////////////////
582 // ProgramFragmentStore
583
584 public class ProgramFragmentStore extends BaseObj {
585 ProgramFragmentStore(int id) {
586 mID = id;
587 }
588
589 public void destroy() {
590 nScriptDestroy(mID);
591 mID = 0;
592 }
593 }
594
595 public void programFragmentStoreBegin(Element in, Element out) {
596 int inID = 0;
597 int outID = 0;
598 if (in != null) {
599 inID = in.mID;
600 }
601 if (out != null) {
602 outID = out.mID;
603 }
604 nProgramFragmentStoreBegin(inID, outID);
605 }
606
607 public void programFragmentStoreDepthFunc(DepthFunc func) {
608 nProgramFragmentStoreDepthFunc(func.mID);
609 }
610
611 public void programFragmentStoreDepthMask(boolean enable) {
612 nProgramFragmentStoreDepthMask(enable);
613 }
614
615 public void programFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
616 nProgramFragmentStoreColorMask(r,g,b,a);
617 }
618
619 public void programFragmentStoreBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
620 nProgramFragmentStoreBlendFunc(src.mID, dst.mID);
621 }
622
623 public void programFragmentStoreDitherEnable(boolean enable) {
624 nProgramFragmentStoreDither(enable);
625 }
626
627 public ProgramFragmentStore programFragmentStoreCreate() {
628 int id = nProgramFragmentStoreCreate();
629 return new ProgramFragmentStore(id);
630 }
631
632 //////////////////////////////////////////////////////////////////////////////////
633 // ProgramFragment
634
635 public class ProgramFragment extends BaseObj {
636 ProgramFragment(int id) {
637 mID = id;
638 }
639
640 public void destroy() {
641 nScriptDestroy(mID);
642 mID = 0;
643 }
644
645 public void bindTexture(Allocation va, int slot) {
646 nProgramFragmentBindTexture(mID, slot, va.mID);
647 }
648
649 //public void bindSampler(Sampler vs, int slot) {
650 //nProgramFragmentBindSampler(mID, slot, vs.mID);
651 //}
652 }
653
654 public void programFragmentBegin(Element in, Element out) {
655 int inID = 0;
656 int outID = 0;
657 if (in != null) {
658 inID = in.mID;
659 }
660 if (out != null) {
661 outID = out.mID;
662 }
663 nProgramFragmentBegin(inID, outID);
664 }
665
666 public void programFragmentSetType(int slot, Type t) {
667 nProgramFragmentSetType(slot, t.mID);
668 }
669
670 public void programFragmentSetType(int slot, EnvMode t) {
671 nProgramFragmentSetEnvMode(slot, t.mID);
672 }
673
674 public void programFragmentSetTexEnable(int slot, boolean enable) {
675 nProgramFragmentSetTexEnable(slot, enable);
676 }
677
678 public ProgramFragment programFragmentCreate() {
679 int id = nProgramFragmentCreate();
680 return new ProgramFragment(id);
681 }
682
683
684 ///////////////////////////////////////////////////////////////////////////////////
685 // Root state
686
687 public void contextBindRootScript(Script s) {
688 nContextBindRootScript(s.mID);
689 }
690
691 //public void contextBindSampler(Sampler s, int slot) {
692 //nContextBindSampler(s.mID);
693 //}
694
695 public void contextBindProgramFragmentStore(ProgramFragmentStore pfs) {
696 nContextBindProgramFragmentStore(pfs.mID);
697 }
698
699 public void contextBindProgramFragment(ProgramFragment pf) {
700 nContextBindProgramFragment(pf.mID);
701 }
702
703/*
704 RsAdapter2D rsAdapter2DCreate ();
705 void rsAdapter2DBindAllocation (RsAdapter2D adapt, RsAllocation alloc);
706 void rsAdapter2DDestroy (RsAdapter2D adapter);
707 void rsAdapter2DSetConstraint (RsAdapter2D adapter, RsDimension dim, uint32_t value);
708 void rsAdapter2DData (RsAdapter2D adapter, const void * data);
709 void rsAdapter2DSubData (RsAdapter2D adapter, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void * data);
710 void rsSamplerBegin ();
711 void rsSamplerSet (RsSamplerParam p, RsSamplerValue value);
712 RsSampler rsSamplerCreate ();
713 void rsSamplerBind (RsSampler sampler, RsAllocation alloc);
714*/
715
716}
717