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