1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [LGPL]
2 // Author: Brian Alliet
8 abstract class OpenGL {
9 static final boolean pretendToBeACrappyVideoCard = false;
10 boolean rectangularTextures;
11 boolean hasRectangularTextures() { return rectangularTextures; }
19 private native void natInit();
21 private static float parseVersion(String s) {
22 int end = s.indexOf(' ');
23 if(end < 0) end = s.length();
25 return Float.parseFloat(s.substring(0,end));
26 } catch(NumberFormatException e) {
27 return 1.1f; // just a guess
31 // This MUST be called after OpenGL is instansiated (and activateSharedJSContext is functioning)
32 public void init() throws NotSupportedException {
34 glVersion = parseVersion(version);
35 if(glVersion < 1.1) throw new NotSupportedException("OpenGL 1.1 or greater is required. (you have: " + version +" - " + glVersion + ")");
36 if(pretendToBeACrappyVideoCard) {
39 rectangularTextures = false;
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);
49 protected abstract void activateSharedJSContext();
51 public static class NotSupportedException extends Exception {
52 public NotSupportedException(String s) { super(s); }
55 public static abstract class GLPixelBuffer extends PixelBuffer {
58 public int getWidth() { return width; }
59 public int getHeight() { return height; }
61 private boolean glScissorEnabled = false;
63 public GLPixelBuffer(int width, int height) {
68 // This should activate the drawing context and prepare the double buffer for drawing
69 protected abstract void activateJSContext();
71 protected static native void drawableInit(int w, int h);
73 private static native void setColor(int color);
75 public native void setClip(int x, int y, int x2, int y2);
76 public native void resetClip();
77 public native void fillJSTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
79 public void drawString(String font, String text, int x, int y, int color) {
80 //System.out.println("drawString(): " + text);
83 public void drawPictureAlphaOnly(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
84 drawPicture_(source, dx, dy, cx1, cy1, cx2, cy2, rgb);
86 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
87 drawPicture_(source, dx, dy, cx1, cy1, cx2, cy2, 0xffffffff);
90 private void drawPicture_(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int color) {
93 GLPicture p = (GLPicture) source;
94 p.draw(dx,dy,cx1,cy1,cx2,cy2);
98 public final static int roundToPowerOf2(int n) {
99 if(((n-1)&n)==0) return n;
100 for(int x=2;x!=0;x<<=1) if(n < x) return x;
104 private Picture _createPicture(Object data, int w, int h, boolean alphaOnly) {
105 if(rectangularTextures && w <= maxRectTexSize && h <= maxRectTexSize) new RectGLPicture(data,w,h,alphaOnly,this);
106 if(w <= maxTexSize && h <= maxTexSize) return new SquareGLPicture(data,w,h,alphaOnly,this);
107 return new MosaicGLPicture(data,w,h,alphaOnly,this);
110 public Picture createPicture(int[] data, int w, int h) {
111 if(w*h > data.length) throw new Error("should never happen");
112 return _createPicture(data,w,h,false);
114 public Picture createAlphaOnlyPicture(byte[] data, int w, int h) {
115 if(w*h > data.length) throw new Error("should never happen");
116 return _createPicture(data,w,h,true);
119 private native void natDeleteTexture(int tex);
120 public void deleteTexture(final int tex) {
121 // CHECKME: Is this safe to do from finalize()?
122 // natDeleteTexture MUST be run from the message queue thread
123 Scheduler.add(new Scheduler.Task() { public void perform() { natDeleteTexture(tex); }});
126 private static abstract class GLPicture extends Picture {
128 protected int height;
130 public final int getWidth() { return width; }
131 public final int getHeight() { return height; }
133 public GLPicture(int w, int h) {
138 public abstract void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
139 protected abstract void finalize();
142 private static class RectGLPicture extends GLPicture {
144 public int textureName;
146 public native void natInit(Object data, boolean alphaOnly);
148 public RectGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
151 natInit(data,alphaOnly);
154 public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
155 protected void finalize() { gl.deleteTexture(textureName); }
158 private static class SquareGLPicture extends GLPicture {
159 private int texHeight;
160 private int texWidth;
162 public int textureName;
164 public native void natInit(Object data, boolean alphaOnly);
166 public SquareGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
169 if(w > 0x7fffffff || h > 0x7fffffff) throw new Error("insane texture size: " + w + "x" + h);
170 texHeight = roundToPowerOf2(h);
171 texWidth = roundToPowerOf2(w);
172 natInit(data,alphaOnly);
175 public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
176 protected void finalize() { gl.deleteTexture(textureName); }
179 private static class MosaicGLPicture extends GLPicture {
180 private static final int overlap = 8;
185 private static float wastedSpace(int w,int h,int size) {
186 int w2 = size * ((w+size-1)/size);
187 int h2 = size * ((h+size-1)/size);
188 return 1.0f-(float)(w*h)/(float)(w2*h2);
191 private static Object subData(Object data, int x, int y, int w, int h, int rowSize) {
192 if(data instanceof byte[]) return subData((byte[])data,x,y,w,h,rowSize);
193 if(data instanceof int[]) return subData((int[])data,x,y,w,h,rowSize);
194 throw new Error("not reached");
197 private static int[] subData(int[] data, int x, int y, int w, int h, int rowSize) {
198 int[] sub = new int[w*h];
200 for(int i=0;i<h;i++,row++)
202 sub[i*w+j] = data[row*rowSize+j+x];
206 private static byte[] subData(byte[] data, int x, int y, int w, int h, int rowSize) {
207 byte[] sub = new byte[w*h];
209 for(int i=0;i<h;i++,row++)
211 sub[i*w+j] = data[row*rowSize+j+x];
215 public MosaicGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
217 psize = gl.maxTexSize;
218 while(wastedSpace(w,h,psize) > 0.40) psize/=2;
219 Log.log(this,"Using psize: " + psize + " for " + w + "x" + h + " image (wasted space: " + wastedSpace(w,h,psize));
221 int rows = (h+psize-1)/psize;
222 int cols = (w+psize-1)/psize;
225 pics = new GLPicture[rows][cols];
226 for(int i=0;i<rows-1;i++)
227 for(int j=0;j<cols-1;j++)
228 pics[i][j] = new SquareGLPicture(subData(data,j*psize,i*psize,psize,psize,w),psize,psize,alphaOnly,gl);
229 tmp = (rows-1)*psize;
230 for(int i=0;i<cols-1;i++)
231 pics[rows-1][i] = new SquareGLPicture(subData(data,i*psize,tmp,psize,h-tmp,w),psize,h-tmp,alphaOnly,gl);
232 tmp2 = (cols-1)*psize;
233 for(int i=0;i<rows-1;i++)
234 pics[i][cols-1] = new SquareGLPicture(subData(data,tmp2,i*psize,w-tmp2,psize,w),w-tmp2,psize,alphaOnly,gl);
235 pics[rows-1][cols-1] = new SquareGLPicture(subData(data,tmp2,tmp,w-tmp2,h-tmp,w),w-tmp2,h-tmp,alphaOnly,gl);
237 protected void finalize() { }
239 private static final int max(int a, int b) { return a > b ? a : b; }
240 private static final int min(int a, int b) { return a < b ? a : b; }
242 public void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
243 int totalWidth = width;
244 int totalHeight = height;
245 // *{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
246 for(int i=0;i<pics.length;i++) {
247 for(int j=0;j<pics[i].length;j++) {
248 int px1 = j*psize + dx;
249 int py1 = i*psize + dy;
250 pics[i][j].draw(px1, py1, cx1, cy1, cx2, cy2);