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