1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL]
2 // Author: Brian Alliet
6 import org.ibex.util.*;
7 import org.ibex.graphics.*;
8 import org.ibex.core.*;
11 abstract class OpenGL {
12 static final boolean pretendToBeACrappyVideoCard = false;
13 boolean rectangularTextures;
14 boolean hasRectangularTextures() { return rectangularTextures; }
22 private native void natInit();
24 private static float parseVersion(String s) {
25 int end = s.indexOf(' ');
26 if(end < 0) end = s.length();
28 return Float.parseFloat(s.substring(0,end));
29 } catch(NumberFormatException e) {
30 return 1.1f; // just a guess
34 // This MUST be called after OpenGL is instansiated (and activateSharedContext is functioning)
35 public void init() throws NotSupportedException {
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) {
42 rectangularTextures = false;
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);
52 protected abstract void activateSharedContext();
54 public static class NotSupportedException extends Exception {
55 public NotSupportedException(String s) { super(s); }
58 public static abstract class GLPixelBuffer extends PixelBuffer {
61 public int getWidth() { return width; }
62 public int getHeight() { return height; }
64 private boolean glScissorEnabled = false;
66 public GLPixelBuffer(int width, int height) {
71 // This should activate the drawing context and prepare the double buffer for drawing
72 protected abstract void activateContext();
74 protected static native void drawableInit(int w, int h);
76 private static native void setColor(int color);
78 public native void setClip(int x, int y, int x2, int y2);
79 public native void resetClip();
80 public native void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
82 public void drawString(String font, String text, int x, int y, int color) {
83 //System.out.println("drawString(): " + text);
86 public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
87 drawPicture_(((org.ibex.plat.Platform.DefaultGlyph)source).getPicture(), dx, dy, cx1, cy1, cx2, cy2, rgb);
89 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
90 drawPicture_(source, dx, dy, cx1, cy1, cx2, cy2, 0xffffffff);
93 private void drawPicture_(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int color) {
96 GLPicture p = getInnerPicture(source, gl);
97 p.draw(dx,dy,cx1,cy1,cx2,cy2);
102 public static OpenGL gl = null;
103 public OpenGL() { gl = this; }
105 public final static int roundToPowerOf2(int n) {
106 if(((n-1)&n)==0) return n;
107 for(int x=2;x!=0;x<<=1) if(n < x) return x;
111 private static GLPicture getInnerPicture(Picture p, OpenGL gl) {
112 OpenGLPicture oglp = (OpenGLPicture)p;
113 if (!oglp.isLoaded || oglp.realPicture != null) return oglp.realPicture;
114 if (gl.rectangularTextures && p.width <= gl.maxRectTexSize && p.height <= gl.maxRectTexSize)
115 oglp.realPicture = new RectGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
116 else if (p.width <= gl.maxTexSize && p.height <= gl.maxTexSize)
117 oglp.realPicture = new SquareGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
119 oglp.realPicture = new MosaicGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl);
121 return oglp.realPicture;
124 public Picture _createPicture(JS r, boolean alphaOnly) { return new OpenGLPicture(r, false); }
126 public static class OpenGLPicture extends Picture {
127 public OpenGLPicture(JS r, boolean a) { super(r); alphaOnly = a; }
129 GLPicture realPicture = null;
132 public Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new org.ibex.plat.Platform.DefaultGlyph(f, c); }
134 private native void natDeleteTexture(int tex);
135 public void deleteTexture(final int tex) {
136 // CHECKME: Is this safe to do from finalize()?
137 // natDeleteTexture MUST be run from the message queue thread
138 Scheduler.add(new Task() { public void perform() { natDeleteTexture(tex); }});
141 private static abstract class GLPicture {
143 protected int height;
145 public final int getWidth() { return width; }
146 public final int getHeight() { return height; }
148 public GLPicture(int w, int h) {
153 public abstract void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
154 protected abstract void finalize();
157 private static class RectGLPicture extends GLPicture {
159 public int textureName;
161 public native void natInit(Object data, boolean alphaOnly);
163 public RectGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
166 natInit(data,alphaOnly);
169 public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
170 protected void finalize() { gl.deleteTexture(textureName); }
173 private static class SquareGLPicture extends GLPicture {
174 private int texHeight;
175 private int texWidth;
177 public int textureName;
179 public native void natInit(Object data, boolean alphaOnly);
181 public SquareGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
184 if(w > 0x7fffffff || h > 0x7fffffff) throw new Error("insane texture size: " + w + "x" + h);
185 texHeight = roundToPowerOf2(h);
186 texWidth = roundToPowerOf2(w);
187 natInit(data,alphaOnly);
190 public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
191 protected void finalize() { gl.deleteTexture(textureName); }
194 private static class MosaicGLPicture extends GLPicture {
198 private static float wastedSpace(int w,int h,int size) {
199 int w2 = size * ((w+size-1)/size);
200 int h2 = size * ((h+size-1)/size);
201 return 1.0f-(float)(w*h)/(float)(w2*h2);
204 private static Object subData(Object data, int x, int y, int w, int h, int rowSize) {
205 if(data instanceof byte[]) return subData((byte[])data,x,y,w,h,rowSize);
206 if(data instanceof int[]) return subData((int[])data,x,y,w,h,rowSize);
207 throw new Error("not reached");
210 private static int[] subData(int[] data, int x, int y, int w, int h, int rowSize) {
211 int[] sub = new int[w*h];
213 for(int i=0;i<h;i++,row++)
215 sub[i*w+j] = data[row*rowSize+j+x];
219 private static byte[] subData(byte[] data, int x, int y, int w, int h, int rowSize) {
220 byte[] sub = new byte[w*h];
222 for(int i=0;i<h;i++,row++)
224 sub[i*w+j] = data[row*rowSize+j+x];
228 public MosaicGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
230 psize = gl.maxTexSize;
231 while(wastedSpace(w,h,psize) > 0.40) psize/=2;
232 Log.info(this,"Using psize: " + psize + " for " + w + "x" + h + " image (wasted space: " + wastedSpace(w,h,psize));
234 int rows = (h+psize-1)/psize;
235 int cols = (w+psize-1)/psize;
238 pics = new GLPicture[rows][cols];
239 for(int i=0;i<rows-1;i++)
240 for(int j=0;j<cols-1;j++)
241 pics[i][j] = new SquareGLPicture(subData(data,j*psize,i*psize,psize,psize,w),psize,psize,alphaOnly,gl);
242 tmp = (rows-1)*psize;
243 for(int i=0;i<cols-1;i++)
244 pics[rows-1][i] = new SquareGLPicture(subData(data,i*psize,tmp,psize,h-tmp,w),psize,h-tmp,alphaOnly,gl);
245 tmp2 = (cols-1)*psize;
246 for(int i=0;i<rows-1;i++)
247 pics[i][cols-1] = new SquareGLPicture(subData(data,tmp2,i*psize,w-tmp2,psize,w),w-tmp2,psize,alphaOnly,gl);
248 pics[rows-1][cols-1] = new SquareGLPicture(subData(data,tmp2,tmp,w-tmp2,h-tmp,w),w-tmp2,h-tmp,alphaOnly,gl);
250 protected void finalize() { }
252 private static final int max(int a, int b) { return a > b ? a : b; }
253 private static final int min(int a, int b) { return a < b ? a : b; }
255 public void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
256 // *{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
257 for(int i=0;i<pics.length;i++) {
258 for(int j=0;j<pics[i].length;j++) {
259 int px1 = j*psize + dx;
260 int py1 = i*psize + dy;
261 pics[i][j].draw(px1, py1, cx1, cy1, cx2, cy2);