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