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