1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
5 // Author: Brian Alliet
9 import org.ibex.util.*;
10 import org.ibex.graphics.*;
11 import org.ibex.core.*;
12 import org.ibex.net.*;
14 abstract class OpenGL {
15 static final boolean pretendToBeACrappyVideoCard = false;
16 boolean rectangularTextures;
17 boolean hasRectangularTextures() { return rectangularTextures; }
25 private native void natInit();
27 private static float parseVersion(String s) {
28 int end = s.indexOf(' ');
29 if(end < 0) end = s.length();
31 return Float.parseFloat(s.substring(0,end));
32 } catch(NumberFormatException e) {
33 return 1.1f; // just a guess
37 // This MUST be called after OpenGL is instansiated (and activateSharedContext is functioning)
38 public void init() throws NotSupportedException {
40 glVersion = parseVersion(version);
41 if(glVersion < 1.1) throw new NotSupportedException("OpenGL 1.1 or greater is required. (you have: " + version +" - " + glVersion + ")");
42 if(pretendToBeACrappyVideoCard) {
45 rectangularTextures = false;
47 Log.diag(this,"Renderer: " + renderer);
48 Log.diag(this,"Version: " + version);
49 Log.diag(this,"Vendor: " + vendor);
50 Log.diag(this,"Rectangular textures: " + (rectangularTextures ? "supported" : "unsupported"));
51 Log.diag(this,"Max texture size: " + maxTexSize);
52 Log.diag(this,"Max rectangular texture size: " + maxRectTexSize);
55 protected abstract void activateSharedContext();
57 public static class NotSupportedException extends Exception {
58 public NotSupportedException(String s) { super(s); }
61 public static abstract class GLPixelBuffer extends PixelBuffer {
64 public int getWidth() { return width; }
65 public int getHeight() { return height; }
67 private boolean glScissorEnabled = false;
69 public GLPixelBuffer(int width, int height) {
74 // This should activate the drawing context and prepare the double buffer for drawing
75 protected abstract void activateContext();
77 protected static native void drawableInit(int w, int h);
79 private static native void setColor(int color);
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);
85 public void drawString(String font, String text, int x, int y, int color) {
86 //System.out.println("drawString(): " + text);
89 public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
90 drawPicture_(((org.ibex.plat.Platform.DefaultGlyph)source).getPicture(), dx, dy, cx1, cy1, cx2, cy2, rgb);
92 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
93 drawPicture_(source, dx, dy, cx1, cy1, cx2, cy2, 0xffffffff);
96 private void drawPicture_(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int color) {
99 GLPicture p = getInnerPicture(source, gl);
100 p.draw(dx,dy,cx1,cy1,cx2,cy2);
105 public static OpenGL gl = null;
106 public OpenGL() { gl = this; }
108 public final static int roundToPowerOf2(int n) {
109 if(((n-1)&n)==0) return n;
110 for(int x=2;x!=0;x<<=1) if(n < x) return x;
114 private static GLPicture getInnerPicture(Picture p, OpenGL gl) {
115 OpenGLPicture oglp = (OpenGLPicture)p;
116 if (!oglp.isLoaded || oglp.realPicture != null) return oglp.realPicture;
117 if (gl.rectangularTextures && p.width <= gl.maxRectTexSize && p.height <= gl.maxRectTexSize)
118 oglp.realPicture = new RectGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
119 else if (p.width <= gl.maxTexSize && p.height <= gl.maxTexSize)
120 oglp.realPicture = new SquareGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
122 oglp.realPicture = new MosaicGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
124 return oglp.realPicture;
127 public Picture _createPicture(JS r, boolean alphaOnly) { return new OpenGLPicture(r, false); }
129 public static class OpenGLPicture extends Picture {
130 public OpenGLPicture(JS r, boolean a) { super(r); alphaOnly = a; }
132 GLPicture realPicture = null;
135 public Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new org.ibex.plat.Platform.DefaultGlyph(f, c); }
137 private native void natDeleteTexture(int tex);
138 public void deleteTexture(final int tex) {
139 // CHECKME: Is this safe to do from finalize()?
140 // natDeleteTexture MUST be run from the message queue thread
141 Scheduler.add(new Callable() { public Object run(Object o) { natDeleteTexture(tex); return null; }});
144 private static abstract class GLPicture {
146 protected int height;
148 public final int getWidth() { return width; }
149 public final int getHeight() { return height; }
151 public GLPicture(int w, int h) {
156 public abstract void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
157 protected abstract void finalize();
160 private static class RectGLPicture extends GLPicture {
162 public int textureName;
164 public native void natInit(Object data, boolean alphaOnly);
166 public RectGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
169 natInit(data,alphaOnly);
172 public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
173 protected void finalize() { gl.deleteTexture(textureName); }
176 private static class SquareGLPicture extends GLPicture {
177 private int texHeight;
178 private int texWidth;
180 public int textureName;
182 public native void natInit(Object data, boolean alphaOnly);
184 public SquareGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
187 if(w > 0x7fffffff || h > 0x7fffffff) throw new Error("insane texture size: " + w + "x" + h);
188 texHeight = roundToPowerOf2(h);
189 texWidth = roundToPowerOf2(w);
190 natInit(data,alphaOnly);
193 public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
194 protected void finalize() { gl.deleteTexture(textureName); }
197 private static class MosaicGLPicture extends GLPicture {
201 private static float wastedSpace(int w,int h,int size) {
202 int w2 = size * ((w+size-1)/size);
203 int h2 = size * ((h+size-1)/size);
204 return 1.0f-(float)(w*h)/(float)(w2*h2);
207 private static Object subData(Object data, int x, int y, int w, int h, int rowSize) {
208 if(data instanceof byte[]) return subData((byte[])data,x,y,w,h,rowSize);
209 if(data instanceof int[]) return subData((int[])data,x,y,w,h,rowSize);
210 throw new Error("not reached");
213 private static int[] subData(int[] data, int x, int y, int w, int h, int rowSize) {
214 int[] sub = new int[w*h];
216 for(int i=0;i<h;i++,row++)
218 sub[i*w+j] = data[row*rowSize+j+x];
222 private static byte[] subData(byte[] data, int x, int y, int w, int h, int rowSize) {
223 byte[] sub = new byte[w*h];
225 for(int i=0;i<h;i++,row++)
227 sub[i*w+j] = data[row*rowSize+j+x];
231 public MosaicGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
233 psize = gl.maxTexSize;
234 while(wastedSpace(w,h,psize) > 0.40) psize/=2;
235 Log.info(this,"Using psize: " + psize + " for " + w + "x" + h + " image (wasted space: " + wastedSpace(w,h,psize));
237 int rows = (h+psize-1)/psize;
238 int cols = (w+psize-1)/psize;
241 pics = new GLPicture[rows][cols];
242 for(int i=0;i<rows-1;i++)
243 for(int j=0;j<cols-1;j++)
244 pics[i][j] = new SquareGLPicture(subData(data,j*psize,i*psize,psize,psize,w),psize,psize,alphaOnly,gl);
245 tmp = (rows-1)*psize;
246 for(int i=0;i<cols-1;i++)
247 pics[rows-1][i] = new SquareGLPicture(subData(data,i*psize,tmp,psize,h-tmp,w),psize,h-tmp,alphaOnly,gl);
248 tmp2 = (cols-1)*psize;
249 for(int i=0;i<rows-1;i++)
250 pics[i][cols-1] = new SquareGLPicture(subData(data,tmp2,i*psize,w-tmp2,psize,w),w-tmp2,psize,alphaOnly,gl);
251 pics[rows-1][cols-1] = new SquareGLPicture(subData(data,tmp2,tmp,w-tmp2,h-tmp,w),w-tmp2,h-tmp,alphaOnly,gl);
253 protected void finalize() { }
255 private static final int max(int a, int b) { return a > b ? a : b; }
256 private static final int min(int a, int b) { return a < b ? a : b; }
258 public void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
259 // *{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
260 for(int i=0;i<pics.length;i++) {
261 for(int j=0;j<pics[i].length;j++) {
262 int px1 = j*psize + dx;
263 int py1 = i*psize + dy;
264 pics[i][j].draw(px1, py1, cx1, cy1, cx2, cy2);