2003/10/28 10:10:18
[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 activateSharedContext 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.log(this,"Renderer: " + renderer);
42         Log.log(this,"Version: " + version);
43         Log.log(this,"Vendor: " + vendor);
44         Log.log(this,"Rectangular textures: " + (rectangularTextures ? "supported" : "unsupported"));
45         Log.log(this,"Max texture size: " + maxTexSize);
46         Log.log(this,"Max rectangular texture size: " + maxRectTexSize);
47     }
48     
49     protected abstract void activateSharedContext();
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 activateContext();
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 drawPictureAlphaOnly(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
84             drawPicture_(source, 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             activateContext();
92             setColor(color);
93             GLPicture p = (GLPicture) source;
94             p.draw(dx,dy,cx1,cy1,cx2,cy2);
95         }
96     }
97     
98     public final static int roundToPowerOf2(int n) {
99         if(((n-1)&n)==0) return n;
100         for(int x=2;x!=0;x<<=1) if(n < x) return x;
101         return 0;
102     }
103         
104     private Picture _createPicture(Object data, int w, int h, boolean alphaOnly) {
105         if(rectangularTextures && w <= maxRectTexSize && h <= maxRectTexSize) new RectGLPicture(data,w,h,alphaOnly,this);
106         if(w <= maxTexSize && h <= maxTexSize) return new SquareGLPicture(data,w,h,alphaOnly,this);
107         return new MosaicGLPicture(data,w,h,alphaOnly,this);
108     }
109     
110     public Picture createPicture(int[] data, int w, int h) {
111         if(w*h > data.length) throw new Error("should never happen");
112         return _createPicture(data,w,h,false);
113     }
114     public Picture createAlphaOnlyPicture(byte[] data, int w, int h) {
115         if(w*h > data.length) throw new Error("should never happen");
116         return _createPicture(data,w,h,true);
117     }
118     
119     private native void natDeleteTexture(int tex);
120     public void deleteTexture(final int tex) {
121         // CHECKME: Is this safe to do from finalize()?
122         // natDeleteTexture MUST be run from the message queue thread
123         Message.Q.add(new Message() { public void perform() {
124             natDeleteTexture(tex);
125         }});
126     }
127     
128     private static abstract class GLPicture extends Picture {
129         protected int width;
130         protected int height;
131         
132         public final int getWidth() { return width; }
133         public final int getHeight() { return height; }
134                 
135         public GLPicture(int w, int h) {
136             this.width = w;
137             this.height = h;
138         }
139         
140         public abstract void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
141         protected abstract void finalize();
142     }
143     
144     private static class RectGLPicture extends GLPicture {
145         private OpenGL gl;
146         public int textureName;
147                 
148         public native void natInit(Object data, boolean alphaOnly);
149         
150         public RectGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
151             super(w,h);
152             this.gl = gl;
153             natInit(data,alphaOnly);
154         }
155     
156         public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);  
157         protected void finalize() { gl.deleteTexture(textureName); }
158     }
159     
160     private static class SquareGLPicture extends GLPicture {
161         private int texHeight;
162         private int texWidth;
163         private OpenGL gl;
164         public int textureName;
165                 
166         public native void natInit(Object data, boolean alphaOnly);
167                 
168         public SquareGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
169             super(w,h);
170             this.gl = gl;
171             if(w > 0x7fffffff || h > 0x7fffffff) throw new Error("insane texture size: " + w + "x" + h);
172             texHeight = roundToPowerOf2(h);
173             texWidth = roundToPowerOf2(w);
174             natInit(data,alphaOnly);
175         }
176         
177         public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);  
178         protected void finalize() { gl.deleteTexture(textureName); }
179     }
180     
181     private static class MosaicGLPicture extends GLPicture {
182         private static final int overlap = 8;
183         
184         int psize;
185         GLPicture[][] pics;
186         
187         private static float wastedSpace(int w,int h,int size) {
188             int w2 = size * ((w+size-1)/size);
189             int h2 = size * ((h+size-1)/size);
190             return 1.0f-(float)(w*h)/(float)(w2*h2);
191         }
192         
193         private static Object subData(Object data, int x, int y, int w, int h, int rowSize) {
194             if(data instanceof byte[]) return subData((byte[])data,x,y,w,h,rowSize);
195             if(data instanceof int[]) return subData((int[])data,x,y,w,h,rowSize);
196             throw new Error("not reached");
197         }
198         
199         private static int[] subData(int[] data, int x, int y, int w, int h, int rowSize) {
200             int[] sub = new int[w*h];
201             int row = y;
202             for(int i=0;i<h;i++,row++)
203                 for(int j=0;j<w;j++)
204                     sub[i*w+j] = data[row*rowSize+j+x];
205             return sub;
206         }
207         
208         private static byte[] subData(byte[] data, int x, int y, int w, int h, int rowSize) {
209             byte[] sub = new byte[w*h];
210             int row = y;
211             for(int i=0;i<h;i++,row++)
212                 for(int j=0;j<w;j++)
213                     sub[i*w+j] = data[row*rowSize+j+x];
214             return sub;
215         }
216         
217         public MosaicGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
218             super(w,h);
219             psize = gl.maxTexSize;
220             while(wastedSpace(w,h,psize) > 0.40) psize/=2;
221             Log.log(this,"Using psize: " + psize + " for " + w + "x" + h + " image (wasted space: " + wastedSpace(w,h,psize));
222             
223             int rows = (h+psize-1)/psize;
224             int cols = (w+psize-1)/psize;
225             int tmp,tmp2;
226             
227             pics = new GLPicture[rows][cols];
228             for(int i=0;i<rows-1;i++)
229                 for(int j=0;j<cols-1;j++)
230                     pics[i][j] = new SquareGLPicture(subData(data,j*psize,i*psize,psize,psize,w),psize,psize,alphaOnly,gl);
231             tmp = (rows-1)*psize;
232             for(int i=0;i<cols-1;i++)
233                 pics[rows-1][i] = new SquareGLPicture(subData(data,i*psize,tmp,psize,h-tmp,w),psize,h-tmp,alphaOnly,gl);
234             tmp2 = (cols-1)*psize;
235             for(int i=0;i<rows-1;i++)
236                 pics[i][cols-1] = new SquareGLPicture(subData(data,tmp2,i*psize,w-tmp2,psize,w),w-tmp2,psize,alphaOnly,gl);
237             pics[rows-1][cols-1] = new SquareGLPicture(subData(data,tmp2,tmp,w-tmp2,h-tmp,w),w-tmp2,h-tmp,alphaOnly,gl);
238         }
239         protected void finalize() {  }
240         
241         private static final int max(int a, int b) { return a > b ? a : b; }
242         private static final int min(int a, int b) { return a < b ? a : b; }
243         
244         public void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
245             int totalWidth = width;
246             int totalHeight = height;
247             // *{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
248             for(int i=0;i<pics.length;i++) {
249                 for(int j=0;j<pics[i].length;j++) {
250                     int px1 = j*psize;
251                     int py1 = i*psize;
252                     int px2 = min(px1+psize,totalWidth);
253                     int py2 = min(py1+psize,totalHeight);
254                     int ix1 = max(px1,cx1);
255                     int iy1 = max(py1,cy1);
256                     int ix2 = min(px2,cx2);
257                     int iy2 = min(py2,cy2);
258                     if (ix1 >= ix2 || iy1 >= iy2) continue; // no intersection
259                     pics[i][j].draw(px1, py1, ix1, iy1, ix2, iy2);
260                 }
261             }
262         }
263     }
264 }