From 745fb6aad79e10eb8c402b8fa6a591769c0fcdfb Mon Sep 17 00:00:00 2001 From: brian Date: Fri, 30 Jan 2004 07:39:22 +0000 Subject: [PATCH] 2003/10/16 05:39:20 darcs-hash:20040130073922-aa32f-35006afe917a7e568875735055c78ca1fde74765.gz --- src/org/xwt/Platform.java | 7 ++ src/org/xwt/plat/Darwin.cc | 11 +-- src/org/xwt/plat/Darwin.java | 7 ++ src/org/xwt/plat/OpenGL.cc | 148 +++++++++++++++++---------------- src/org/xwt/plat/OpenGL.java | 82 +++++++++++------- src/org/xwt/translators/Freetype.java | 10 ++- 6 files changed, 153 insertions(+), 112 deletions(-) diff --git a/src/org/xwt/Platform.java b/src/org/xwt/Platform.java index 0ca55b9..aa8be35 100644 --- a/src/org/xwt/Platform.java +++ b/src/org/xwt/Platform.java @@ -124,7 +124,14 @@ public class Platform { /** creates and returns a picture */ public static Picture createPicture(int[] data, int w, int h) { return platform._createPicture(data, w, h); } + public static Picture createAlphaOnlyPicture(byte[] data, int w, int h) { return platform._createAlphaOnlyPicture(data, w, h); } + protected Picture _createPicture(int[] b, int w, int h) { return null; } + protected Picture _createAlphaOnlyPicture(byte[] b, int w, int h) { + int[] b2 = new int[b.length]; + for(int i=0;iactivateContext(); glFlush(); checkGLError(); - + WC(aglSetCurrentContext)(ctx); checkGLError(); diff --git a/src/org/xwt/plat/Darwin.java b/src/org/xwt/plat/Darwin.java index 8da005c..a3e4141 100644 --- a/src/org/xwt/plat/Darwin.java +++ b/src/org/xwt/plat/Darwin.java @@ -275,6 +275,7 @@ public class Darwin extends POSIX { private native void reshape(int w, int h); // blit_lock is assumed to be held public void needsReshape() { needsReshape = true; } + public native void natDispose(); } @@ -321,6 +322,12 @@ public class Darwin extends POSIX { else return /*new QZCarbonPicture(data,w,h);*/ null; } + protected Picture _createAlphaOnlyPicture(byte[] data, int w, int h) { + if(openGL != null) + return openGL.createAlphaOnlyPicture(data,w,h); + else + return super.createAlphaOnlyPicture(data,w,h); + } /* A message that is sent through the carbon event queue */ private static abstract class CarbonMessage { diff --git a/src/org/xwt/plat/OpenGL.cc b/src/org/xwt/plat/OpenGL.cc index 71870c7..5540c2d 100644 --- a/src/org/xwt/plat/OpenGL.cc +++ b/src/org/xwt/plat/OpenGL.cc @@ -126,20 +126,26 @@ void OpenGL$GLPixelBuffer::resetClip() { } } -void OpenGL$RectGLPicture::natInit(JArray *data_) { +// FEATURE: Eliminate duplicate code in RectGLPicture and SquareGLPicture +void OpenGL$RectGLPicture::natInit(Object *data_, jboolean alphaOnly) { unsigned char *buf; int i,j; int size = width*height; - jint *data = elements(data_); - buf = new unsigned char[size*4]; GLuint tex; - for(i=0,j=0;i> 16) & 0xff; - buf[j+1] = (pixel >> 8) & 0xff; - buf[j+2] = (pixel >> 0) & 0xff; - buf[j+3] = (pixel >> 24) & 0xff; + if(alphaOnly) { + jbyte *data = elements((JArray *)data_); + buf = (unsigned char *) data; + } else { + jint *data = elements((JArray *)data_); + buf = new unsigned char[size*4]; + for(i=0,j=0;i> 16) & 0xff; + buf[j+1] = (pixel >> 8) & 0xff; + buf[j+2] = (pixel >> 0) & 0xff; + buf[j+3] = (pixel >> 24) & 0xff; + } } gl->activateSharedContext(); @@ -147,18 +153,23 @@ void OpenGL$RectGLPicture::natInit(JArray *data_) { glGenTextures(1,&tex); glBindTexture(GL_TEXTURE_RECTANGLE_EXT,tex); glPixelStorei(GL_PACK_ALIGNMENT,1); - glTexImage2D(GL_TEXTURE_RECTANGLE_EXT,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,buf); - delete buf; + glTexImage2D( + GL_TEXTURE_RECTANGLE_EXT,0, + alphaOnly ? GL_ALPHA : GL_RGBA,width,height,0, + alphaOnly ? GL_ALPHA : GL_RGBA,GL_UNSIGNED_BYTE,buf); + if(!alphaOnly) + delete buf; - // FIXME: enable linear filtering for opengl 1.2 - glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_MIN_FILTER,GL_NEAREST); - glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_MAG_FILTER,GL_NEAREST); + if(gl->glVersion >= 1.2) { + glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameterf(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } else { + glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_MIN_FILTER,GL_NEAREST); + glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_MAG_FILTER,GL_NEAREST); + } - /*glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_MIN_FILTER,GL_LINEAR); - glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_MAG_FILTER,GL_LINEAR); - glTexParameterf(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameterf(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);*/ - glDisable(GL_TEXTURE_RECTANGLE_EXT); checkGLError(); @@ -166,8 +177,6 @@ void OpenGL$RectGLPicture::natInit(JArray *data_) { } void OpenGL$RectGLPicture::draw(jint dx1, jint dy1, jint dx2, jint dy2,jint sx1, jint sy1, jint sx2, jint sy2) { - if (gl->hasRectangularTextures()) { - glColor4f(1.0f,1.0f,1.0f,1.0f); glEnable(GL_TEXTURE_RECTANGLE_EXT); glBindTexture(GL_TEXTURE_RECTANGLE_EXT, textureName); glBegin(GL_QUADS); @@ -182,30 +191,6 @@ void OpenGL$RectGLPicture::draw(jint dx1, jint dy1, jint dx2, jint dy2,jint sx1, glEnd(); glDisable(GL_TEXTURE_RECTANGLE_EXT); checkGLError(); - } else { - float tx1,ty1,tx2,ty2; // normalized texture coords - tx1 = (float) sx1 / (float) width; - ty1 = (float) sy1 / (float) height; - tx2 = (float) sx2 / (float) width; - ty2 = (float) sy2 / (float) height; - - glColor4f(1.0f,1.0f,1.0f,1.0f); - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, textureName); - checkGLError(); - glBegin(GL_QUADS); - glTexCoord2f (tx1, ty1 ); - glVertex3i (dx1, dy1, 0); - glTexCoord2f (tx2, ty1 ); - glVertex3i (dx2, dy1, 0); - glTexCoord2f (tx2, ty2 ); - glVertex3i (dx2, dy2, 0); - glTexCoord2f (tx1, ty2 ); - glVertex3i (dx1, dy2, 0); - glEnd(); - glDisable(GL_TEXTURE_2D); - checkGLError(); - } } void OpenGL::natDeleteTexture(jint tex_) { @@ -214,45 +199,67 @@ void OpenGL::natDeleteTexture(jint tex_) { glDeleteTextures(1,&tex); } -void OpenGL$SquareGLPicture::natInit(JArray *data_) { +void OpenGL$SquareGLPicture::natInit(Object *data_, jboolean alphaOnly) { unsigned char *buf; int row,col,p; - jint *data = elements(data_); - buf = new unsigned char[texWidth*texHeight*4]; GLuint tex; - p=0; - for(row=0;row> 16) & 0xff; - buf[p+1] = (pixel >> 8) & 0xff; - buf[p+2] = (pixel >> 0) & 0xff; - buf[p+3] = (pixel >> 24) & 0xff; - p+=4; + if(alphaOnly) { + jbyte *data = elements((JArray*) data_); + if(texWidth == width && texHeight == height) { + buf = (unsigned char*) data; + } else { + buf = new unsigned char[texWidth*texHeight]; + p=0; + for(row=0;row*) data_); + buf = new unsigned char[texWidth*texHeight*4]; + p=0; + for(row=0;row> 16) & 0xff; + buf[p+1] = (pixel >> 8) & 0xff; + buf[p+2] = (pixel >> 0) & 0xff; + buf[p+3] = (pixel >> 24) & 0xff; + p+=4; + } + for(;col < texWidth;col++,p+=4) + buf[p+0] = buf[p+1] = buf[p+2] = buf[p+3] = 0; } - for(;col < texWidth;col++,p+=4) - buf[p+0] = buf[p+1] = buf[p+2] = buf[p+3] = 0; + for(;row < texHeight;row++) + for(col=0;colactivateSharedContext(); glEnable(GL_TEXTURE_2D); glGenTextures(1,&tex); glBindTexture(GL_TEXTURE_2D,tex); glPixelStorei(GL_PACK_ALIGNMENT,1); - glTexImage2D(GL_TEXTURE_2D,0,4,texWidth,texHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,buf); + glTexImage2D(GL_TEXTURE_2D,0,alphaOnly ? GL_ALPHA : GL_RGBA,texWidth,texHeight,0,alphaOnly ? GL_ALPHA : GL_RGBA,GL_UNSIGNED_BYTE,buf); checkGLError(); - delete buf; + if(!alphaOnly || width != texWidth || height != texHeight) delete buf; - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); - /*glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);*/ + if(gl->glVersion >= 1.2) { + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } else { + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); + } glDisable(GL_TEXTURE_2D); checkGLError(); @@ -267,7 +274,6 @@ void OpenGL$SquareGLPicture::draw(jint dx1, jint dy1, jint dx2, jint dy2,jint sx tx2 = (float) sx2 / (float) texWidth; ty2 = (float) sy2 / (float) texHeight; - glColor4f(1.0f,1.0f,1.0f,1.0f); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, textureName); checkGLError(); diff --git a/src/org/xwt/plat/OpenGL.java b/src/org/xwt/plat/OpenGL.java index 5367cb6..284fb62 100644 --- a/src/org/xwt/plat/OpenGL.java +++ b/src/org/xwt/plat/OpenGL.java @@ -11,6 +11,7 @@ abstract class OpenGL { boolean hasRectangularTextures() { return rectangularTextures; } int maxTexSize; int maxRectTexSize; + float glVersion; String version; String renderer; String vendor; @@ -26,13 +27,12 @@ abstract class OpenGL { return 1.1f; // just a guess } } + // This MUST be called after OpenGL is instansiated (and activateSharedContext is functioning) public void init() throws NotSupportedException { natInit(); - float v = parseVersion(version); - // FEATURE: enable linear filtering for OpenGL >= 1.2 - // If we disable linear filtering (and therefor GL_CLAMP_TO_EDGE) we could probably get by with less - if(v < 1.1) throw new NotSupportedException("OpenGL 1.1 or greater is required. (you have: " + version +" - " + v + ")"); + 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; @@ -82,16 +82,16 @@ abstract class OpenGL { //public native void drawPictureAlphaOnly(Picture source, int dx1, int dy1, int dx2, int dy2, //int sx1, int sy1, int sx2, int sy2, int rgb); - public void drawPictureAlphaOnly(Picture source, int dx1, int dy1, int dx2, int dy2, - int sx1, int sy1, int sx2, int sy2, int rgb) { } - - public void drawPicture(org.xwt.Picture source, int x, int y) { - activateContext(); - GLPicture p = (GLPicture) source; - p.draw(x,y); + public void drawPictureAlphaOnly(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, int rgb) { + drawPicture_(source,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,rgb); + } + public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2) { + drawPicture_(source,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,0xffffffff); } - public void drawPicture(org.xwt.Picture source, int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2) { + + private void drawPicture_(Picture source, int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2, int color) { activateContext(); + setColor(color); GLPicture p = (GLPicture) source; p.draw(dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2); } @@ -103,10 +103,19 @@ abstract class OpenGL { return 0; } + private Picture _createPicture(Object data, int w, int h, boolean alphaOnly) { + if(rectangularTextures && w <= maxRectTexSize && h <= maxRectTexSize) new RectGLPicture(data,w,h,alphaOnly,this); + if(w <= maxTexSize && h <= maxTexSize) return new SquareGLPicture(data,w,h,alphaOnly,this); + return new MosaicGLPicture(data,w,h,alphaOnly,this); + } + public Picture createPicture(int[] data, int w, int h) { - if(rectangularTextures && w <= maxRectTexSize && h <= maxRectTexSize) new RectGLPicture(data,w,h,this); - if(w <= maxTexSize && h <= maxTexSize) return new SquareGLPicture(data,w,h,this); - return new MosaicGLPicture(data,w,h,this); + if(w*h > data.length) throw new Error("should never happen"); + return _createPicture(data,w,h,false); + } + public Picture createAlphaOnlyPicture(byte[] data, int w, int h) { + if(w*h > data.length) throw new Error("should never happen"); + return _createPicture(data,w,h,true); } private native void natDeleteTexture(int tex); @@ -130,7 +139,6 @@ abstract class OpenGL { this.height = h; } - public void draw(int x, int y) { draw(x,y,x+width,y+height,0,0,width,height); } public abstract void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2); protected abstract void finalize(); } @@ -139,12 +147,12 @@ abstract class OpenGL { private OpenGL gl; public int textureName; - public native void natInit(int[] data); + public native void natInit(Object data, boolean alphaOnly); - public RectGLPicture(int[] data,int w, int h, OpenGL gl) { + public RectGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) { super(w,h); this.gl = gl; - natInit(data); + natInit(data,alphaOnly); } public native void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2); @@ -157,15 +165,15 @@ abstract class OpenGL { private OpenGL gl; public int textureName; - public native void natInit(int[] data); + public native void natInit(Object data, boolean alphaOnly); - public SquareGLPicture(int[] data,int w, int h, OpenGL gl) { + 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); + natInit(data,alphaOnly); } public native void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2); @@ -184,21 +192,31 @@ abstract class OpenGL { 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; - try { for(int i=0;i 0.40) psize/=2; @@ -211,14 +229,14 @@ abstract class OpenGL { pics = new GLPicture[rows][cols]; for(int i=0;i= 0; k--) { if (i + k < width * height) data[i + k] = (val & 0xff) << 24; val >>>= 8; } - } + }*/ - glyph.p = Platform.createPicture(data, width, height); + glyph.p = Platform.createAlphaOnlyPicture(data, width, height); glyphCache.put(res, new Integer((g << 16) | pointsize), glyph); } } catch (Exception e) { -- 1.7.10.4