2003/10/01 21:44:33
[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     String version;
15     String renderer;
16     String vendor;
17     
18     private native void natInit();
19     
20     private static float parseVersion(String s) {
21         int end = s.indexOf(' ');
22         if(end < 0) end = s.length();
23         try {
24             return Float.parseFloat(s.substring(0,end));
25         } catch(NumberFormatException e) {
26             return 1.1f; // just a guess
27         }
28     }
29     // This MUST be called after OpenGL is instansiated (and activateSharedContext is functioning)
30     public void init() throws NotSupportedException {
31         natInit();
32         float v = parseVersion(version);
33         // FEATURE: enable linear filtering for OpenGL >= 1.2
34         // If we disable linear filtering (and therefor GL_CLAMP_TO_EDGE) we could probably get by with less
35         if(v < 1.1) throw new NotSupportedException("OpenGL 1.1 or greater is required. (you have: " + version +" - " + v + ")");
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,
86             int sx1, int sy1, int sx2, int sy2, int rgb) { }
87
88         public void drawPicture(org.xwt.Picture source, int x, int y) {
89             activateContext();
90             GLPicture p = (GLPicture) source;
91             p.draw(x,y);
92         }
93         public void drawPicture(org.xwt.Picture source, int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2) {
94             activateContext();
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     public Picture createPicture(int[] data, int w, int h) {
107         if(rectangularTextures && w <= maxRectTexSize && h <= maxRectTexSize) new RectGLPicture(data,w,h,this);
108         if(w <= maxTexSize && h <= maxTexSize) return new SquareGLPicture(data,w,h,this);
109         return new MosaicGLPicture(data,w,h,this);
110     }
111     
112     private native void natDeleteTexture(int tex);
113     public void deleteTexture(final int tex) {
114         // CHECKME: Is this safe to do from finalize()?
115         // natDeleteTexture MUST be run from the message queue thread
116         Message.Q.add(new Message() { public void perform() {
117             natDeleteTexture(tex);
118         }});
119     }
120     
121     private static abstract class GLPicture extends Picture {
122         protected int width;
123         protected int height;
124         
125         public final int getWidth() { return width; }
126         public final int getHeight() { return height; }
127                 
128         public GLPicture(int w, int h) {
129             this.width = w;
130             this.height = h;
131         }
132         
133         public void draw(int x, int y) { draw(x,y,x+width,y+height,0,0,width,height); }
134         public abstract void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2);
135         protected abstract void finalize();
136     }
137     
138     private static class RectGLPicture extends GLPicture {
139         private OpenGL gl;
140         public int textureName;
141                 
142         public native void natInit(int[] data);
143         
144         public RectGLPicture(int[] data,int w, int h, OpenGL gl) {
145             super(w,h);
146             this.gl = gl;
147             natInit(data);
148         }
149     
150         public native void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2);  
151         protected void finalize() { gl.deleteTexture(textureName); }
152     }
153     
154     private static class SquareGLPicture extends GLPicture {
155         private int texHeight;
156         private int texWidth;
157         private OpenGL gl;
158         public int textureName;
159                 
160         public native void natInit(int[] data);
161                 
162         public SquareGLPicture(int[] data,int w, int h, OpenGL gl) {
163             super(w,h);
164             this.gl = gl;
165             if(w > 0x7fffffff || h > 0x7fffffff) throw new Error("insane texture size: " + w + "x" + h);
166             texHeight = roundToPowerOf2(h);
167             texWidth = roundToPowerOf2(w);
168             natInit(data);
169         }
170         
171         public native void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2); 
172         protected void finalize() { gl.deleteTexture(textureName); }
173     }
174     
175     private static class MosaicGLPicture extends GLPicture {
176         private static final int overlap = 8;
177         
178         int psize;
179         GLPicture[][] pics;
180         
181         private static float wastedSpace(int w,int h,int size) {
182             int w2 = size * ((w+size-1)/size);
183             int h2 = size * ((h+size-1)/size);
184             return 1.0f-(float)(w*h)/(float)(w2*h2);
185         }
186         
187         private static int[] subData(int[] data, int x, int y, int w, int h, int rowSize) {
188             int[] sub = new int[w*h];
189             int row = y;
190             try {
191             for(int i=0;i<h;i++,row++)
192                 for(int j=0;j<w;j++)
193                     sub[i*w+j] = data[row*rowSize+j+x];
194             } catch(ArrayIndexOutOfBoundsException e) {
195                 Log.log(MosaicGLPicture.class,"subData(data("+data.length+"),"+x+","+y+","+w+","+h+","+rowSize);
196                 throw e;
197             }
198             return sub;
199         }
200         
201         public MosaicGLPicture(int[] data,int w, int h, OpenGL gl) {
202             super(w,h);
203             psize = gl.maxTexSize;
204             while(wastedSpace(w,h,psize) > 0.40) psize/=2;
205             Log.log(this,"Using psize: " + psize + " for " + w + "x" + h + " image (wasted space: " + wastedSpace(w,h,psize));
206             
207             int rows = (h+psize-1)/psize;
208             int cols = (w+psize-1)/psize;
209             int tmp,tmp2;
210             
211             pics = new GLPicture[rows][cols];
212             for(int i=0;i<rows-1;i++)
213                 for(int j=0;j<cols-1;j++)
214                     pics[i][j] = new SquareGLPicture(subData(data,j*psize,i*psize,psize,psize,w),psize,psize,gl);
215             tmp = (rows-1)*psize;
216             for(int i=0;i<cols-1;i++)
217                 pics[rows-1][i] = new SquareGLPicture(subData(data,i*psize,tmp,psize,h-tmp,w),psize,h-tmp,gl);
218             tmp2 = (cols-1)*psize;
219             for(int i=0;i<rows-1;i++)
220                 pics[i][cols-1] = new SquareGLPicture(subData(data,tmp2,i*psize,w-tmp2,psize,w),w-tmp2,psize,gl);
221             pics[rows-1][cols-1] = new SquareGLPicture(subData(data,tmp2,tmp,w-tmp2,h-tmp,w),w-tmp2,h-tmp,gl);
222         }
223         protected void finalize() {  }
224         
225         private static final int max(int a, int b) { return a > b ? a : b; }
226         private static final int min(int a, int b) { return a < b ? a : b; }
227         
228         public void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2) {
229             double xscale = (double)(dx2-dx1)/(double)(sx2-sx1);
230             double yscale = (double)(dy2-dy1)/(double)(sy2-sy1);
231             int totalWidth = width;
232             int totalHeight = height;
233             // *{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
234             //System.out.println("Starting draw..." + sx1 + "," + sy1 + " " + sx2 + "," + sy2 + " to " + dx1 +"," + dy1 + " " + dx2 + "," + dy2);
235             for(int i=0;i<pics.length;i++) {
236                 for(int j=0;j<pics[i].length;j++) {
237                     int px1 = j*psize;
238                     int py1 = i*psize;
239                     int px2 = min(px1+psize,totalWidth);
240                     int py2 = min(py1+psize,totalHeight);
241                     int ix1 = max(px1,sx1);
242                     int iy1 = max(py1,sy1);
243                     int ix2 = min(px2,sx2);
244                     int iy2 = min(py2,sy2);
245                     if(ix1 >= ix2 || iy1 >= iy2) continue; // no intersection
246                     
247                     int pdx1 = dx1 + (int) (xscale*(ix1-sx1));
248                     int pdy1 = dy1 + (int) (yscale*(iy1-sy1));
249                     int pdx2 = dx2 - (int) (xscale*(sx2-ix2));
250                     int pdy2 = dy2 - (int) (yscale*(sy2-iy2));
251                     
252                     //System.out.println("" + i + "," + j + " is good... drawing from " + (ix1-px1) + "," + (iy1-py1) + " " + (ix2-px1) + "," + (iy2-py1) + " to " + pdx1 + "," + pdy1 + " to " + pdx2 + "," + pdy2);
253                     
254                     pics[i][j].draw(pdx1,pdy1,pdx2,pdy2,ix1-px1,iy1-py1,ix2-px1,iy2-py1);
255                 }
256             }
257         }
258     }
259 }