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