// Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL] // Author: Brian Alliet package org.ibex.plat; import org.ibex.js.*; import org.ibex.util.*; import org.ibex.graphics.*; import org.ibex.core.*; import org.ibex.net.*; abstract class OpenGL { static final boolean pretendToBeACrappyVideoCard = false; boolean rectangularTextures; boolean hasRectangularTextures() { return rectangularTextures; } int maxTexSize; int maxRectTexSize; float glVersion; String version; String renderer; String vendor; private native void natInit(); private static float parseVersion(String s) { int end = s.indexOf(' '); if(end < 0) end = s.length(); try { return Float.parseFloat(s.substring(0,end)); } catch(NumberFormatException e) { return 1.1f; // just a guess } } // This MUST be called after OpenGL is instansiated (and activateSharedContext is functioning) public void init() throws NotSupportedException { natInit(); glVersion = parseVersion(version); if(glVersion < 1.1) throw new NotSupportedException("OpenGL 1.1 or greater is required. (you have: " + version +" - " + glVersion + ")"); if(pretendToBeACrappyVideoCard) { maxTexSize = 512; maxRectTexSize = 0; rectangularTextures = false; } Log.diag(this,"Renderer: " + renderer); Log.diag(this,"Version: " + version); Log.diag(this,"Vendor: " + vendor); Log.diag(this,"Rectangular textures: " + (rectangularTextures ? "supported" : "unsupported")); Log.diag(this,"Max texture size: " + maxTexSize); Log.diag(this,"Max rectangular texture size: " + maxRectTexSize); } protected abstract void activateSharedContext(); public static class NotSupportedException extends Exception { public NotSupportedException(String s) { super(s); } } public static abstract class GLPixelBuffer implements PixelBuffer { protected int width; protected int height; public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argb) { throw new Error("not implemented"); } public void drawPicture(Picture p, Affine a, Mesh h) { throw new Error("drawPicture() not implemented"); } public void drawGlyph(Font.Glyph source,Affine a,Mesh h,int rgb,int bg) { throw new Error("drawGlyph() not implemented"); } public int getWidth() { return width; } public int getHeight() { return height; } private boolean glScissorEnabled = false; // FIXME: HUGE HACK! public GLPixelBuffer() { width = 500; height = 500; } public GLPixelBuffer(int width, int height) { this.width = width; this.height = height; } // This should activate the drawing context and prepare the double buffer for drawing protected abstract void activateContext(); protected static native void drawableInit(int w, int h); private static native void setColor(int color); public native void setClip(int x, int y, int x2, int y2); public native void resetClip(); public native void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color); public void drawLine(int x1, int y1, int x2, int y2, int color) { // FIXME fillTrapezoid(x1, x1, y1, x2, x2, y2, color); } public void stroke(Mesh p, int color) { /*p.stroke(this, color);*/ } public native void natFill(Mesh p, int color); public void fill(Mesh p, Paint paint) { natFill(p, ((Paint.SingleColorPaint)paint).color); } public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb, int pc) { drawPicture_(((org.ibex.plat.Platform.DefaultGlyph)source).getPicture(), dx, dy, cx1, cy1, cx2, cy2, rgb); } public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) { drawPicture_(source, dx, dy, cx1, cy1, cx2, cy2, 0xffffffff); } private void drawPicture_(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int color) { activateContext(); setColor(color); GLPicture p = getInnerPicture(source, gl); p.draw(dx,dy,cx1,cy1,cx2,cy2); } } // FIXME ugly public static OpenGL gl = null; public OpenGL() { gl = this; } public final static int roundToPowerOf2(int n) { if(((n-1)&n)==0) return n; for(int x=2;x!=0;x<<=1) if(n < x) return x; return 0; } private static GLPicture getInnerPicture(Picture p, OpenGL gl) { OpenGLPicture oglp = (OpenGLPicture)p; if (!oglp.isLoaded || oglp.realPicture != null) return oglp.realPicture; if (gl.rectangularTextures && p.width <= gl.maxRectTexSize && p.height <= gl.maxRectTexSize) oglp.realPicture = new RectGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl); else if (p.width <= gl.maxTexSize && p.height <= gl.maxTexSize) oglp.realPicture = new SquareGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl); else oglp.realPicture = new MosaicGLPicture(p.data,p.width,p.height,oglp.alphaOnly,gl); p.data = null; return oglp.realPicture; } public Picture _createPicture(JS r, boolean alphaOnly) { return new OpenGLPicture(r, false); } public static class OpenGLPicture extends Picture { public OpenGLPicture(JS r, boolean a) { super(r); alphaOnly = a; } boolean alphaOnly; GLPicture realPicture = null; } public Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new org.ibex.plat.Platform.DefaultGlyph(f, c); } private native void natDeleteTexture(int tex); public void deleteTexture(final int tex) { // CHECKME: Is this safe to do from finalize()? // natDeleteTexture MUST be run from the message queue thread Platform.Scheduler.add(new Callable() { public Object run(Object o) { natDeleteTexture(tex); return null; }}); } private static abstract class GLPicture { protected int width; protected int height; public final int getWidth() { return width; } public final int getHeight() { return height; } public GLPicture(int w, int h) { this.width = w; this.height = h; } public abstract void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2); protected abstract void finalize(); } private static class RectGLPicture extends GLPicture { private OpenGL gl; public int textureName; public native void natInit(Object data, boolean alphaOnly); public RectGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) { super(w,h); this.gl = gl; natInit(data,alphaOnly); } public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2); protected void finalize() { gl.deleteTexture(textureName); } } private static class SquareGLPicture extends GLPicture { private int texHeight; private int texWidth; private OpenGL gl; public int textureName; public native void natInit(Object data, boolean alphaOnly); public SquareGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) { super(w,h); this.gl = gl; if(w > 0x7fffffff || h > 0x7fffffff) throw new Error("insane texture size: " + w + "x" + h); texHeight = roundToPowerOf2(h); texWidth = roundToPowerOf2(w); natInit(data,alphaOnly); } public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2); protected void finalize() { gl.deleteTexture(textureName); } } private static class MosaicGLPicture extends GLPicture { int psize; GLPicture[][] pics; private static float wastedSpace(int w,int h,int size) { int w2 = size * ((w+size-1)/size); int h2 = size * ((h+size-1)/size); return 1.0f-(float)(w*h)/(float)(w2*h2); } private static Object subData(Object data, int x, int y, int w, int h, int rowSize) { if(data instanceof byte[]) return subData((byte[])data,x,y,w,h,rowSize); if(data instanceof int[]) return subData((int[])data,x,y,w,h,rowSize); throw new Error("not reached"); } private static int[] subData(int[] data, int x, int y, int w, int h, int rowSize) { int[] sub = new int[w*h]; int row = y; for(int i=0;i 0.40) psize/=2; Log.info(this,"Using psize: " + psize + " for " + w + "x" + h + " image (wasted space: " + wastedSpace(w,h,psize)); int rows = (h+psize-1)/psize; int cols = (w+psize-1)/psize; int tmp,tmp2; pics = new GLPicture[rows][cols]; for(int i=0;i b ? a : b; } private static final int min(int a, int b) { return a < b ? a : b; } public void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2) { // *{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 for(int i=0;i