propose-patch
[org.ibex.core.git] / src / org / xwt / plat / OpenGL.java
index 284fb62..c18d09c 100644 (file)
@@ -1,8 +1,9 @@
-// Copyright 2003 Adam Megacz, see the COPYING file for licensing [LGPL]
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL]
 // Author: Brian Alliet
 
 package org.xwt.plat;
 import org.xwt.*;
+import org.xwt.js.*;
 import org.xwt.util.*;
 
 abstract class OpenGL {
@@ -38,12 +39,12 @@ abstract class OpenGL {
             maxRectTexSize = 0;
             rectangularTextures = false;
         }
-        Log.log(this,"Renderer: " + renderer);
-        Log.log(this,"Version: " + version);
-        Log.log(this,"Vendor: " + vendor);
-        Log.log(this,"Rectangular textures: " + (rectangularTextures ? "supported" : "unsupported"));
-        Log.log(this,"Max texture size: " + maxTexSize);
-        Log.log(this,"Max rectangular texture size: " + maxRectTexSize);
+        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();
@@ -79,55 +80,63 @@ abstract class OpenGL {
         public void drawString(String font, String text, int x, int y, int color) {
             //System.out.println("drawString(): " + text);
         }
-        
-        //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) {
-            drawPicture_(source,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,rgb);
+
+        public void drawGlyph(org.xwt.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
+            drawPicture_(((org.xwt.Platform.DefaultGlyph)source).getPicture(), dx, dy, cx1, cy1, cx2, cy2, 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(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 dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2, int color) {
+        private void drawPicture_(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int color) {
             activateContext();
             setColor(color);
-            GLPicture p = (GLPicture) source;
-            p.draw(dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2);
+            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 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(w*h > data.length) throw new Error("should never happen");
-        return _createPicture(data,w,h,false);
+    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 createAlphaOnlyPicture(byte[] data, int w, int h) {
-        if(w*h > data.length) throw new Error("should never happen");
-        return _createPicture(data,w,h,true);
+    
+    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.xwt.Font f, char c) { return new org.xwt.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
-        Message.Q.add(new Message() { public void perform() {
-            natDeleteTexture(tex);
-        }});
+        Scheduler.add(new Scheduler.Task() { public void perform() { natDeleteTexture(tex); }});
     }
     
-    private static abstract class GLPicture extends Picture {
+    private static abstract class GLPicture {
         protected int width;
         protected int height;
         
@@ -139,7 +148,7 @@ abstract class OpenGL {
             this.height = h;
         }
         
-        public abstract void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2);
+        public abstract void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);
         protected abstract void finalize();
     }
     
@@ -155,7 +164,7 @@ abstract class OpenGL {
             natInit(data,alphaOnly);
         }
     
-        public native void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2);  
+        public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);  
         protected void finalize() { gl.deleteTexture(textureName); }
     }
     
@@ -176,7 +185,7 @@ abstract class OpenGL {
             natInit(data,alphaOnly);
         }
         
-        public native void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2); 
+        public native void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2);  
         protected void finalize() { gl.deleteTexture(textureName); }
     }
     
@@ -220,7 +229,7 @@ abstract class OpenGL {
             super(w,h);
             psize = gl.maxTexSize;
             while(wastedSpace(w,h,psize) > 0.40) psize/=2;
-            Log.log(this,"Using psize: " + psize + " for " + w + "x" + h + " image (wasted space: " + wastedSpace(w,h,psize));
+            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;
@@ -243,33 +252,15 @@ abstract class OpenGL {
         private static final int max(int a, int b) { return a > b ? a : b; }
         private static final int min(int a, int b) { return a < b ? a : b; }
         
-        public void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2) {
-            double xscale = (double)(dx2-dx1)/(double)(sx2-sx1);
-            double yscale = (double)(dy2-dy1)/(double)(sy2-sy1);
+        public void draw(int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
             int totalWidth = width;
             int totalHeight = height;
             // *{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
-            //System.out.println("Starting draw..." + sx1 + "," + sy1 + " " + sx2 + "," + sy2 + " to " + dx1 +"," + dy1 + " " + dx2 + "," + dy2);
             for(int i=0;i<pics.length;i++) {
                 for(int j=0;j<pics[i].length;j++) {
-                    int px1 = j*psize;
-                    int py1 = i*psize;
-                    int px2 = min(px1+psize,totalWidth);
-                    int py2 = min(py1+psize,totalHeight);
-                    int ix1 = max(px1,sx1);
-                    int iy1 = max(py1,sy1);
-                    int ix2 = min(px2,sx2);
-                    int iy2 = min(py2,sy2);
-                    if(ix1 >= ix2 || iy1 >= iy2) continue; // no intersection
-                    
-                    int pdx1 = dx1 + (int) (xscale*(ix1-sx1));
-                    int pdy1 = dy1 + (int) (yscale*(iy1-sy1));
-                    int pdx2 = dx2 - (int) (xscale*(sx2-ix2));
-                    int pdy2 = dy2 - (int) (yscale*(sy2-iy2));
-                    
-                    //System.out.println("" + i + "," + j + " is good... drawing from " + (ix1-px1) + "," + (iy1-py1) + " " + (ix2-px1) + "," + (iy2-py1) + " to " + pdx1 + "," + pdy1 + " to " + pdx2 + "," + pdy2);
-                    
-                    pics[i][j].draw(pdx1,pdy1,pdx2,pdy2,ix1-px1,iy1-py1,ix2-px1,iy2-py1);
+                    int px1 = j*psize + dx;
+                    int py1 = i*psize + dy;
+                    pics[i][j].draw(px1, py1, cx1, cy1, cx2, cy2);
                 }
             }
         }