1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.opengl;
  27 
  28 import java.awt.Graphics;
  29 import java.awt.GraphicsConfiguration;
  30 import java.awt.GraphicsDevice;
  31 import java.awt.GraphicsEnvironment;
  32 import java.awt.Image;
  33 import java.awt.Rectangle;
  34 import java.awt.image.ColorModel;
  35 
  36 import sun.java2d.SunGraphics2D;
  37 import sun.java2d.SurfaceData;
  38 
  39 import sun.lwawt.macosx.CPlatformView;
  40 
  41 import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_EXT_LCD_SHADER;
  42 
  43 public abstract class CGLSurfaceData extends OGLSurfaceData {
  44 
  45     protected final int scale;
  46     protected final int width;
  47     protected final int height;
  48     protected CPlatformView pView;
  49     private CGLGraphicsConfig graphicsConfig;
  50 
  51     native void validate(int xoff, int yoff, int width, int height, boolean isOpaque);
  52 
  53     private native void initOps(long pConfigInfo, long pPeerData, long layerPtr,
  54                                 int xoff, int yoff, boolean isOpaque);
  55 
  56     protected native boolean initPbuffer(long pData, long pConfigInfo,
  57             boolean isOpaque, int width, int height);
  58 
  59     protected CGLSurfaceData(CGLGraphicsConfig gc, ColorModel cm, int type,
  60                              int width, int height) {
  61         super(gc, cm, type);
  62         // TEXTURE shouldn't be scaled, it is used for managed BufferedImages.
  63         scale = type == TEXTURE ? 1 : gc.getDevice().getScaleFactor();
  64         this.width = width * scale;
  65         this.height = height * scale;
  66     }
  67 
  68     protected CGLSurfaceData(CPlatformView pView, CGLGraphicsConfig gc,
  69                              ColorModel cm, int type,int width, int height)
  70     {
  71         this(gc, cm, type, width, height);
  72         this.pView = pView;
  73         this.graphicsConfig = gc;
  74 
  75         long pConfigInfo = gc.getNativeConfigInfo();
  76         long pPeerData = 0L;
  77         boolean isOpaque = true;
  78         if (pView != null) {
  79             pPeerData = pView.getAWTView();
  80             isOpaque = pView.isOpaque();
  81         }
  82         initOps(pConfigInfo, pPeerData, 0, 0, 0, isOpaque);
  83     }
  84 
  85     protected CGLSurfaceData(CGLLayer layer, CGLGraphicsConfig gc,
  86                              ColorModel cm, int type,int width, int height)
  87     {
  88         this(gc, cm, type, width, height);
  89         this.graphicsConfig = gc;
  90 
  91         long pConfigInfo = gc.getNativeConfigInfo();
  92         long layerPtr = 0L;
  93         boolean isOpaque = true;
  94         if (layer != null) {
  95             layerPtr = layer.getPointer();
  96             isOpaque = layer.isOpaque();
  97         }
  98         initOps(pConfigInfo, 0, layerPtr, 0, 0, isOpaque);
  99     }
 100 
 101     @Override //SurfaceData
 102     public GraphicsConfiguration getDeviceConfiguration() {
 103         return graphicsConfig;
 104     }
 105 
 106     @Override
 107     public boolean canRenderLCDText(SunGraphics2D sg2d) {
 108         // relax the condition to render the lcd text
 109         // by removal the transparency check, because
 110         // we have to create translucent buffers due to
 111         // shaped windows support on macosx.
 112         return getOGLGraphicsConfig().isCapPresent(CAPS_EXT_LCD_SHADER) &&
 113                sg2d.compositeState <= SunGraphics2D.COMP_ISCOPY &&
 114                sg2d.paintState <= SunGraphics2D.PAINT_OPAQUECOLOR;
 115     }
 116 
 117 
 118     /**
 119      * Creates a SurfaceData object representing the primary (front) buffer of
 120      * an on-screen Window.
 121      */
 122     public static CGLWindowSurfaceData createData(CPlatformView pView) {
 123         CGLGraphicsConfig gc = getGC(pView);
 124         return new CGLWindowSurfaceData(pView, gc);
 125     }
 126 
 127     /**
 128      * Creates a SurfaceData object representing the intermediate buffer
 129      * between the Java2D flusher thread and the AppKit thread.
 130      */
 131     public static CGLLayerSurfaceData createData(CGLLayer layer) {
 132         CGLGraphicsConfig gc = getGC(layer);
 133         Rectangle r = layer.getBounds();
 134         return new CGLLayerSurfaceData(layer, gc, r.width, r.height);
 135     }
 136 
 137     /**
 138      * Creates a SurfaceData object representing the back buffer of a
 139      * double-buffered on-screen Window.
 140      */
 141     public static CGLOffScreenSurfaceData createData(CPlatformView pView,
 142             Image image, int type) {
 143         CGLGraphicsConfig gc = getGC(pView);
 144         Rectangle r = pView.getBounds();
 145         if (type == FLIP_BACKBUFFER) {
 146             return new CGLOffScreenSurfaceData(pView, gc, r.width, r.height,
 147                     image, gc.getColorModel(), FLIP_BACKBUFFER);
 148         } else {
 149             return new CGLVSyncOffScreenSurfaceData(pView, gc, r.width,
 150                     r.height, image, gc.getColorModel(), type);
 151         }
 152     }
 153 
 154     /**
 155      * Creates a SurfaceData object representing an off-screen buffer (either a
 156      * Pbuffer or Texture).
 157      */
 158     public static CGLOffScreenSurfaceData createData(CGLGraphicsConfig gc,
 159             int width, int height, ColorModel cm, Image image, int type) {
 160         return new CGLOffScreenSurfaceData(null, gc, width, height, image, cm,
 161                 type);
 162     }
 163 
 164     public static CGLGraphicsConfig getGC(CPlatformView pView) {
 165         if (pView != null) {
 166             return (CGLGraphicsConfig)pView.getGraphicsConfiguration();
 167         } else {
 168             // REMIND: this should rarely (never?) happen, but what if
 169             // default config is not CGL?
 170             GraphicsEnvironment env = GraphicsEnvironment
 171                 .getLocalGraphicsEnvironment();
 172             GraphicsDevice gd = env.getDefaultScreenDevice();
 173             return (CGLGraphicsConfig) gd.getDefaultConfiguration();
 174         }
 175     }
 176 
 177     public static CGLGraphicsConfig getGC(CGLLayer layer) {
 178         return (CGLGraphicsConfig)layer.getGraphicsConfiguration();
 179     }
 180 
 181     public void validate() {
 182         // Overridden in CGLWindowSurfaceData below
 183     }
 184 
 185     @Override
 186     public int getDefaultScale() {
 187         return scale;
 188     }
 189 
 190     @Override
 191     public boolean copyArea(SunGraphics2D sg2d, int x, int y, int w, int h,
 192                             int dx, int dy) {
 193         final int state = sg2d.transformState;
 194         if (state > SunGraphics2D.TRANSFORM_TRANSLATESCALE
 195             || sg2d.compositeState >= SunGraphics2D.COMP_XOR) {
 196             return false;
 197         }
 198         if (state <= SunGraphics2D.TRANSFORM_ANY_TRANSLATE) {
 199             x += sg2d.transX;
 200             y += sg2d.transY;
 201         } else if (state == SunGraphics2D.TRANSFORM_TRANSLATESCALE) {
 202             final double[] coords = {x, y, x + w, y + h, x + dx, y + dy};
 203             sg2d.transform.transform(coords, 0, coords, 0, 3);
 204             x = (int) Math.ceil(coords[0] - 0.5);
 205             y = (int) Math.ceil(coords[1] - 0.5);
 206             w = ((int) Math.ceil(coords[2] - 0.5)) - x;
 207             h = ((int) Math.ceil(coords[3] - 0.5)) - y;
 208             dx = ((int) Math.ceil(coords[4] - 0.5)) - x;
 209             dy = ((int) Math.ceil(coords[5] - 0.5)) - y;
 210         }
 211         oglRenderPipe.copyArea(sg2d, x, y, w, h, dx, dy);
 212         return true;
 213     }
 214 
 215     protected native void clearWindow();
 216 
 217     public static class CGLWindowSurfaceData extends CGLSurfaceData {
 218 
 219         public CGLWindowSurfaceData(CPlatformView pView,
 220                 CGLGraphicsConfig gc) {
 221             super(pView, gc, gc.getColorModel(), WINDOW, 0, 0);
 222         }
 223 
 224         @Override
 225         public SurfaceData getReplacement() {
 226             return pView.getSurfaceData();
 227         }
 228 
 229         @Override
 230         public Rectangle getBounds() {
 231             Rectangle r = pView.getBounds();
 232             return new Rectangle(0, 0, r.width, r.height);
 233         }
 234 
 235         /**
 236          * Returns destination Component associated with this SurfaceData.
 237          */
 238         @Override
 239         public Object getDestination() {
 240             return pView.getDestination();
 241         }
 242 
 243         public void validate() {
 244             OGLRenderQueue rq = OGLRenderQueue.getInstance();
 245             rq.lock();
 246             try {
 247                 rq.flushAndInvokeNow(new Runnable() {
 248                     public void run() {
 249                         Rectangle peerBounds = pView.getBounds();
 250                         validate(0, 0, peerBounds.width, peerBounds.height, pView.isOpaque());
 251                     }
 252                 });
 253             } finally {
 254                 rq.unlock();
 255             }
 256         }
 257 
 258         @Override
 259         public void invalidate() {
 260             super.invalidate();
 261             clearWindow();
 262         }
 263     }
 264 
 265     /**
 266      * A surface which implements an intermediate buffer between
 267      * the Java2D flusher thread and the AppKit thread.
 268      *
 269      * This surface serves as a buffer attached to a CGLLayer and
 270      * the layer redirects all painting to the buffer's graphics.
 271      */
 272     public static class CGLLayerSurfaceData extends CGLSurfaceData {
 273 
 274         private CGLLayer layer;
 275 
 276         public CGLLayerSurfaceData(CGLLayer layer, CGLGraphicsConfig gc,
 277                                    int width, int height) {
 278             super(layer, gc, gc.getColorModel(), FBOBJECT, width, height);
 279             this.layer = layer;
 280             initSurface(this.width, this.height);
 281         }
 282 
 283         @Override
 284         public SurfaceData getReplacement() {
 285             return layer.getSurfaceData();
 286         }
 287 
 288         @Override
 289         boolean isOnScreen() {
 290             return true;
 291         }
 292 
 293         @Override
 294         public Rectangle getBounds() {
 295             return new Rectangle(width, height);
 296         }
 297 
 298         @Override
 299         public Object getDestination() {
 300             return layer.getDestination();
 301         }
 302 
 303         @Override
 304         public int getTransparency() {
 305             return layer.getTransparency();
 306         }
 307 
 308         @Override
 309         public void invalidate() {
 310             super.invalidate();
 311             clearWindow();
 312         }
 313     }
 314 
 315     /**
 316      * A surface which implements a v-synced flip back-buffer with COPIED
 317      * FlipContents.
 318      *
 319      * This surface serves as a back-buffer to the outside world, while it is
 320      * actually an offscreen surface. When the BufferStrategy this surface
 321      * belongs to is showed, it is first copied to the real private
 322      * FLIP_BACKBUFFER, which is then flipped.
 323      */
 324     public static class CGLVSyncOffScreenSurfaceData extends
 325             CGLOffScreenSurfaceData {
 326         private CGLOffScreenSurfaceData flipSurface;
 327 
 328         public CGLVSyncOffScreenSurfaceData(CPlatformView pView,
 329                 CGLGraphicsConfig gc, int width, int height, Image image,
 330                 ColorModel cm, int type) {
 331             super(pView, gc, width, height, image, cm, type);
 332             flipSurface = CGLSurfaceData.createData(pView, image,
 333                     FLIP_BACKBUFFER);
 334         }
 335 
 336         public SurfaceData getFlipSurface() {
 337             return flipSurface;
 338         }
 339 
 340         @Override
 341         public void flush() {
 342             flipSurface.flush();
 343             super.flush();
 344         }
 345     }
 346 
 347     public static class CGLOffScreenSurfaceData extends CGLSurfaceData {
 348         private Image offscreenImage;
 349 
 350         public CGLOffScreenSurfaceData(CPlatformView pView,
 351                                        CGLGraphicsConfig gc, int width, int height, Image image,
 352                                        ColorModel cm, int type) {
 353             super(pView, gc, cm, type, width, height);
 354             offscreenImage = image;
 355             initSurface(this.width, this.height);
 356         }
 357 
 358         @Override
 359         public SurfaceData getReplacement() {
 360             return restoreContents(offscreenImage);
 361         }
 362 
 363         @Override
 364         public Rectangle getBounds() {
 365             if (type == FLIP_BACKBUFFER) {
 366                 Rectangle r = pView.getBounds();
 367                 return new Rectangle(0, 0, r.width, r.height);
 368             } else {
 369                 return new Rectangle(width, height);
 370             }
 371         }
 372 
 373         /**
 374          * Returns destination Image associated with this SurfaceData.
 375          */
 376         @Override
 377         public Object getDestination() {
 378             return offscreenImage;
 379         }
 380     }
 381 
 382     // Mac OS X specific APIs for JOGL/Java2D bridge...
 383 
 384     // given a surface create and attach GL context, then return it
 385     private native static long createCGLContextOnSurface(CGLSurfaceData sd,
 386             long sharedContext);
 387 
 388     public static long createOGLContextOnSurface(Graphics g, long sharedContext) {
 389         SurfaceData sd = ((SunGraphics2D) g).surfaceData;
 390         if ((sd instanceof CGLSurfaceData) == true) {
 391             CGLSurfaceData cglsd = (CGLSurfaceData) sd;
 392             return createCGLContextOnSurface(cglsd, sharedContext);
 393         } else {
 394             return 0L;
 395         }
 396     }
 397 
 398     // returns whether or not the makeCurrent operation succeeded
 399     native static boolean makeCGLContextCurrentOnSurface(CGLSurfaceData sd,
 400             long ctx);
 401 
 402     public static boolean makeOGLContextCurrentOnSurface(Graphics g, long ctx) {
 403         SurfaceData sd = ((SunGraphics2D) g).surfaceData;
 404         if ((ctx != 0L) && ((sd instanceof CGLSurfaceData) == true)) {
 405             CGLSurfaceData cglsd = (CGLSurfaceData) sd;
 406             return makeCGLContextCurrentOnSurface(cglsd, ctx);
 407         } else {
 408             return false;
 409         }
 410     }
 411 
 412     // additional cleanup
 413     private native static void destroyCGLContext(long ctx);
 414 
 415     public static void destroyOGLContext(long ctx) {
 416         if (ctx != 0L) {
 417             destroyCGLContext(ctx);
 418         }
 419     }
 420 }