make core compile with new js stuff and Task replacement class
[org.ibex.core.git] / src / org / ibex / plat / OpenGL.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 // Author: Brian Alliet
6
7 package org.ibex.plat;
8 import org.ibex.js.*;
9 import org.ibex.util.*;
10 import org.ibex.graphics.*;
11 import org.ibex.core.*;
12 import org.ibex.net.*;
13
14 abstract class OpenGL {
15     static final boolean pretendToBeACrappyVideoCard = false;
16     boolean rectangularTextures;
17     boolean hasRectangularTextures() { return rectangularTextures; }
18     int maxTexSize;
19     int maxRectTexSize;
20     float glVersion;
21     String version;
22     String renderer;
23     String vendor;
24     
25     private native void natInit();
26     
27     private static float parseVersion(String s) {
28         int end = s.indexOf(' ');
29         if(end < 0) end = s.length();
30         try {
31             return Float.parseFloat(s.substring(0,end));
32         } catch(NumberFormatException e) {
33             return 1.1f; // just a guess
34         }
35     }
36     
37     // This MUST be called after OpenGL is instansiated (and activateSharedContext is functioning)
38     public void init() throws NotSupportedException {
39         natInit();
40         glVersion = parseVersion(version);
41         if(glVersion < 1.1) throw new NotSupportedException("OpenGL 1.1 or greater is required. (you have: " + version +" - " + glVersion + ")");
42         if(pretendToBeACrappyVideoCard) {
43             maxTexSize = 512;
44             maxRectTexSize = 0;
45             rectangularTextures = false;
46         }
47         Log.diag(this,"Renderer: " + renderer);
48         Log.diag(this,"Version: " + version);
49         Log.diag(this,"Vendor: " + vendor);
50         Log.diag(this,"Rectangular textures: " + (rectangularTextures ? "supported" : "unsupported"));
51         Log.diag(this,"Max texture size: " + maxTexSize);
52         Log.diag(this,"Max rectangular texture size: " + maxRectTexSize);
53     }
54     
55     protected abstract void activateSharedContext();
56
57     public static class NotSupportedException extends Exception {
58         public NotSupportedException(String s) { super(s); }
59     }
60
61     public static abstract class GLPixelBuffer extends PixelBuffer {
62         protected int width;
63         protected int height;
64         public int getWidth() { return width; }
65         public int getHeight() { return height; }
66         
67         private boolean glScissorEnabled = false;
68         
69         public GLPixelBuffer(int width, int height) {
70             this.width = width;
71             this.height = height;
72         }
73         
74         // This should activate the drawing context and prepare the double buffer for drawing
75         protected abstract void activateContext();
76         
77         protected static native void drawableInit(int w, int h);
78         
79         private static native void setColor(int color);
80         
81         public native void setClip(int x, int y, int x2, int y2);
82         public native void resetClip();
83         public native void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
84             
85         public void drawString(String font, String text, int x, int y, int color) {
86             //System.out.println("drawString(): " + text);
87         }
88
89         public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
90             drawPicture_(((org.ibex.plat.Platform.DefaultGlyph)source).getPicture(), dx, dy, cx1, cy1, cx2, cy2, rgb);
91         }
92         public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
93             drawPicture_(source, dx, dy, cx1, cy1, cx2, cy2, 0xffffffff);
94         }
95
96         private void drawPicture_(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int color) {
97             activateContext();
98             setColor(color);
99             GLPicture p = getInnerPicture(source, gl);
100             p.draw(dx,dy,cx1,cy1,cx2,cy2);
101         }
102     }
103
104     // FIXME ugly
105     public static OpenGL gl = null;
106     public OpenGL() { gl = this; }
107     
108     public final static int roundToPowerOf2(int n) {
109         if(((n-1)&n)==0) return n;
110         for(int x=2;x!=0;x<<=1) if(n < x) return x;
111         return 0;
112     }
113     
114     private static GLPicture getInnerPicture(Picture p, OpenGL gl) {
115         OpenGLPicture oglp = (OpenGLPicture)p;
116         if (!oglp.isLoaded || oglp.realPicture != null) return oglp.realPicture;
117         if (gl.rectangularTextures && p.width <= gl.maxRectTexSize && p.height <= gl.maxRectTexSize) 
118             oglp.realPicture = new RectGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
119         else if (p.width <= gl.maxTexSize && p.height <= gl.maxTexSize)
120             oglp.realPicture = new SquareGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
121         else
122             oglp.realPicture = new MosaicGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
123         p.data = null;
124         return oglp.realPicture;
125     }
126     
127     public Picture _createPicture(JS r, boolean alphaOnly) { return new OpenGLPicture(r, false); }
128
129     public static class OpenGLPicture extends Picture {
130         public OpenGLPicture(JS r, boolean a) { super(r); alphaOnly = a; }
131         boolean alphaOnly;
132         GLPicture realPicture = null;
133     }
134
135     public Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new org.ibex.plat.Platform.DefaultGlyph(f, c); }
136     
137     private native void natDeleteTexture(int tex);
138     public void deleteTexture(final int tex) {
139         // CHECKME: Is this safe to do from finalize()?
140         // natDeleteTexture MUST be run from the message queue thread
141         Scheduler.add(new Callable() { public Object run(Object o) { natDeleteTexture(tex); return null; }});
142     }
143     
144     private static abstract class GLPicture {
145         protected int width;
146         protected int height;
147         
148         public final int getWidth() { return width; }
149         public final int getHeight() { return height; }
150                 
151         public GLPicture(int w, int h) {
152             this.width = w;
153             this.height = h;
154         }
155         
156         public abstract void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
157         protected abstract void finalize();
158     }
159     
160     private static class RectGLPicture extends GLPicture {
161         private OpenGL gl;
162         public int textureName;
163                 
164         public native void natInit(Object data, boolean alphaOnly);
165         
166         public RectGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
167             super(w,h);
168             this.gl = gl;
169             natInit(data,alphaOnly);
170         }
171     
172         public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);  
173         protected void finalize() { gl.deleteTexture(textureName); }
174     }
175     
176     private static class SquareGLPicture extends GLPicture {
177         private int texHeight;
178         private int texWidth;
179         private OpenGL gl;
180         public int textureName;
181                 
182         public native void natInit(Object data, boolean alphaOnly);
183                 
184         public SquareGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
185             super(w,h);
186             this.gl = gl;
187             if(w > 0x7fffffff || h > 0x7fffffff) throw new Error("insane texture size: " + w + "x" + h);
188             texHeight = roundToPowerOf2(h);
189             texWidth = roundToPowerOf2(w);
190             natInit(data,alphaOnly);
191         }
192         
193         public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);  
194         protected void finalize() { gl.deleteTexture(textureName); }
195     }
196     
197     private static class MosaicGLPicture extends GLPicture {
198         int psize;
199         GLPicture[][] pics;
200         
201         private static float wastedSpace(int w,int h,int size) {
202             int w2 = size * ((w+size-1)/size);
203             int h2 = size * ((h+size-1)/size);
204             return 1.0f-(float)(w*h)/(float)(w2*h2);
205         }
206         
207         private static Object subData(Object data, int x, int y, int w, int h, int rowSize) {
208             if(data instanceof byte[]) return subData((byte[])data,x,y,w,h,rowSize);
209             if(data instanceof int[]) return subData((int[])data,x,y,w,h,rowSize);
210             throw new Error("not reached");
211         }
212         
213         private static int[] subData(int[] data, int x, int y, int w, int h, int rowSize) {
214             int[] sub = new int[w*h];
215             int row = y;
216             for(int i=0;i<h;i++,row++)
217                 for(int j=0;j<w;j++)
218                     sub[i*w+j] = data[row*rowSize+j+x];
219             return sub;
220         }
221         
222         private static byte[] subData(byte[] data, int x, int y, int w, int h, int rowSize) {
223             byte[] sub = new byte[w*h];
224             int row = y;
225             for(int i=0;i<h;i++,row++)
226                 for(int j=0;j<w;j++)
227                     sub[i*w+j] = data[row*rowSize+j+x];
228             return sub;
229         }
230         
231         public MosaicGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
232             super(w,h);
233             psize = gl.maxTexSize;
234             while(wastedSpace(w,h,psize) > 0.40) psize/=2;
235             Log.info(this,"Using psize: " + psize + " for " + w + "x" + h + " image (wasted space: " + wastedSpace(w,h,psize));
236             
237             int rows = (h+psize-1)/psize;
238             int cols = (w+psize-1)/psize;
239             int tmp,tmp2;
240             
241             pics = new GLPicture[rows][cols];
242             for(int i=0;i<rows-1;i++)
243                 for(int j=0;j<cols-1;j++)
244                     pics[i][j] = new SquareGLPicture(subData(data,j*psize,i*psize,psize,psize,w),psize,psize,alphaOnly,gl);
245             tmp = (rows-1)*psize;
246             for(int i=0;i<cols-1;i++)
247                 pics[rows-1][i] = new SquareGLPicture(subData(data,i*psize,tmp,psize,h-tmp,w),psize,h-tmp,alphaOnly,gl);
248             tmp2 = (cols-1)*psize;
249             for(int i=0;i<rows-1;i++)
250                 pics[i][cols-1] = new SquareGLPicture(subData(data,tmp2,i*psize,w-tmp2,psize,w),w-tmp2,psize,alphaOnly,gl);
251             pics[rows-1][cols-1] = new SquareGLPicture(subData(data,tmp2,tmp,w-tmp2,h-tmp,w),w-tmp2,h-tmp,alphaOnly,gl);
252         }
253         protected void finalize() {  }
254         
255         private static final int max(int a, int b) { return a > b ? a : b; }
256         private static final int min(int a, int b) { return a < b ? a : b; }
257         
258         public void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
259             // *{x,y}{1,2} key: d=dest s=src, p=bounds of this picture, i=intersection of s and p, pd = dest of this pic
260             for(int i=0;i<pics.length;i++) {
261                 for(int j=0;j<pics[i].length;j++) {
262                     int px1 = j*psize + dx;
263                     int py1 = i*psize + dy;
264                     pics[i][j].draw(px1, py1, cx1, cy1, cx2, cy2);
265                 }
266             }
267         }
268     }
269 }