Search in sources :

Example 1 with Display

use of com.sun.jna.platform.unix.X11.Display in project jna by java-native-access.

the class AlphaMaskDemo method updateX11.

private void updateX11(boolean a, boolean i) {
    X11 x11 = X11.INSTANCE;
    X11.Window win = X11.Window.None;
    Display dpy = x11.XOpenDisplay(null);
    try {
        if (!alphaWindow.isDisplayable()) {
            alphaWindow.pack();
            if (System.getProperty("java.version").matches("^1\\.4\\..*"))
                alphaWindow.setVisible(true);
            win = new X11.Window((int) Native.getWindowID(alphaWindow));
            Window parent = alphaWindow.getOwner();
            Point where = parent.getLocationOnScreen();
            where.translate(parent.getWidth(), 0);
            alphaWindow.removeAll();
            alphaWindow.setLocation(where);
            alphaWindow.setBackground(new Color(0, 0, 0, 0));
        } else {
            win = new X11.Window((int) Native.getWindowID(alphaWindow));
        }
        if (i) {
            int w = image.getWidth(null);
            int h = image.getHeight(null);
            alphaWindow.setSize(w, h);
            if (buffer == null || buffer.size() != w * h * 4) {
                buffer = new com.sun.jna.Memory(w * h * 4);
                pixels = new int[w * h];
            }
            BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics g = buf.getGraphics();
            g.drawImage(image, 0, 0, w, h, null);
            long start = System.currentTimeMillis();
            long blitTime, putImageTime, write;
            GC gc = x11.XCreateGC(dpy, win, new NativeLong(0), null);
            long gcTime = System.currentTimeMillis();
            try {
                Raster raster = buf.getData();
                int[] pixel = new int[4];
                for (int y = 0; y < h; y++) {
                    for (int x = 0; x < w; x++) {
                        raster.getPixel(x, y, pixel);
                        int alpha = (pixel[3] & 0xFF) << 24;
                        int red = (pixel[2] & 0xFF);
                        int green = (pixel[1] & 0xFF) << 8;
                        int blue = (pixel[0] & 0xFF) << 16;
                        pixels[y * w + x] = alpha | red | green | blue;
                    }
                }
                blitTime = System.currentTimeMillis();
                X11.XWindowAttributes xwa = new X11.XWindowAttributes();
                x11.XGetWindowAttributes(dpy, win, xwa);
                X11.XImage image = x11.XCreateImage(dpy, xwa.visual, 32, X11.ZPixmap, 0, buffer, w, h, 32, w * 4);
                buffer.write(0, pixels, 0, pixels.length);
                write = System.currentTimeMillis();
                x11.XPutImage(dpy, win, gc, image, 0, 0, 0, 0, w, h);
                x11.XFree(image.getPointer());
                putImageTime = System.currentTimeMillis();
            } finally {
                if (gc != null)
                    x11.XFreeGC(dpy, gc);
            }
            long end = System.currentTimeMillis();
        //System.out.println("gc: " + (gcTime-start) + "ms");
        //System.out.println("blit: " + (blitTime-gcTime) + "ms");
        //System.out.println("write: " + (write-blitTime) + "ms");
        //System.out.println("put image: " + (putImageTime-write) + "ms");
        //System.out.println("total: " + (end-start) + "ms");
        }
    } finally {
        if (dpy != null)
            x11.XCloseDisplay(dpy);
    }
    if (a)
        WindowUtils.setWindowAlpha(alphaWindow, alpha);
    if (!alphaWindow.isVisible()) {
        alphaWindow.setVisible(true);
        // hack for initial refresh (X11)
        update(true, true);
    }
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) Color(java.awt.Color) NativeLong(com.sun.jna.NativeLong) Raster(java.awt.image.Raster) Point(java.awt.Point) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics(java.awt.Graphics) X11(com.sun.jna.platform.unix.X11) GC(com.sun.jna.platform.unix.X11.GC) Display(com.sun.jna.platform.unix.X11.Display)

Aggregations

NativeLong (com.sun.jna.NativeLong)1 X11 (com.sun.jna.platform.unix.X11)1 Display (com.sun.jna.platform.unix.X11.Display)1 GC (com.sun.jna.platform.unix.X11.GC)1 Color (java.awt.Color)1 Graphics (java.awt.Graphics)1 Point (java.awt.Point)1 Window (java.awt.Window)1 BufferedImage (java.awt.image.BufferedImage)1 Raster (java.awt.image.Raster)1 JWindow (javax.swing.JWindow)1