1 /*
   2  * Copyright (c) 2011, 2015, 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 public abstract class CGLSurfaceData extends OGLSurfaceData {
  42 
  43     protected final int scale;
  44     protected final int width;
  45     protected final int height;
  46     protected CPlatformView pView;
  47     private CGLGraphicsConfig graphicsConfig;
  48 
  49     native void validate(int xoff, int yoff, int width, int height, boolean isOpaque);
  50 
  51     private native void initOps(long pConfigInfo, long pPeerData, long layerPtr,
  52                                 int xoff, int yoff, boolean isOpaque);
  53 
  54     protected CGLSurfaceData(CGLGraphicsConfig gc, ColorModel cm, int type,
  55                              int width, int height) {
  56         super(gc, cm, type);
  57         // TEXTURE shouldn't be scaled, it is used for managed BufferedImages.
  58         scale = type == TEXTURE ? 1 : gc.getDevice().getScaleFactor();
  59         this.width = width * scale;
  60         this.height = height * scale;
  61     }
  62 
  63     protected CGLSurfaceData(CPlatformView pView, CGLGraphicsConfig gc,
  64                              ColorModel cm, int type,int width, int height)
  65     {
  66         this(gc, cm, type, width, height);
  67         this.pView = pView;
  68         this.graphicsConfig = gc;
  69 
  70         long pConfigInfo = gc.getNativeConfigInfo();
  71         long pPeerData = 0L;
  72         boolean isOpaque = true;
  73         if (pView != null) {
  74             pPeerData = pView.getAWTView();
  75             isOpaque = pView.isOpaque();
  76         }
  77         initOps(pConfigInfo, pPeerData, 0, 0, 0, isOpaque);
  78     }
  79 
  80     protected CGLSurfaceData(CGLLayer layer, CGLGraphicsConfig gc,
  81                              ColorModel cm, int type,int width, int height)
  82     {
  83         this(gc, cm, type, width, height);
  84         this.graphicsConfig = gc;
  85 
  86         long pConfigInfo = gc.getNativeConfigInfo();
  87         long layerPtr = 0L;
  88         boolean isOpaque = true;
  89         if (layer != null) {
  90             layerPtr = layer.getPointer();
  91             isOpaque = layer.isOpaque();
  92         }
  93         initOps(pConfigInfo, 0, layerPtr, 0, 0, isOpaque);
  94     }
  95 
  96     @Override //SurfaceData
  97     public GraphicsConfiguration getDeviceConfiguration() {
  98         return graphicsConfig;
  99     }
 100 
 101     /**
 102      * Creates a SurfaceData object representing the primary (front) buffer of
 103      * an on-screen Window.
 104      */
 105     public static CGLWindowSurfaceData createData(CPlatformView pView) {
 106         CGLGraphicsConfig gc = getGC(pView);
 107         return new CGLWindowSurfaceData(pView, gc);
 108     }
 109 
 110     /**
 111      * Creates a SurfaceData object representing the intermediate buffer
 112      * between the Java2D flusher thread and the AppKit thread.
 113      */
 114     public static CGLLayerSurfaceData createData(CGLLayer layer) {
 115         CGLGraphicsConfig gc = getGC(layer);
 116         Rectangle r = layer.getBounds();
 117         return new CGLLayerSurfaceData(layer, gc, r.width, r.height);
 118     }
 119 
 120     /**
 121      * Creates a SurfaceData object representing the back buffer of a
 122      * double-buffered on-screen Window.
 123      */
 124     public static CGLOffScreenSurfaceData createData(CPlatformView pView,
 125             Image image, int type) {
 126         CGLGraphicsConfig gc = getGC(pView);
 127         Rectangle r = pView.getBounds();
 128         if (type == FLIP_BACKBUFFER) {
 129             return new CGLOffScreenSurfaceData(pView, gc, r.width, r.height,
 130                     image, gc.getColorModel(), FLIP_BACKBUFFER);
 131         } else {
 132             return new CGLVSyncOffScreenSurfaceData(pView, gc, r.width,
 133                     r.height, image, gc.getColorModel(), type);
 134         }
 135     }
 136 
 137     /**
 138      * Creates a SurfaceData object representing an off-screen buffer (either a
 139      * FBO or Texture).
 140      */
 141     public static CGLOffScreenSurfaceData createData(CGLGraphicsConfig gc,
 142             int width, int height, ColorModel cm, Image image, int type) {
 143         return new CGLOffScreenSurfaceData(null, gc, width, height, image, cm,
 144                 type);
 145     }
 146 
 147     public static CGLGraphicsConfig getGC(CPlatformView pView) {
 148         if (pView != null) {
 149             return (CGLGraphicsConfig)pView.getGraphicsConfiguration();
 150         } else {
 151             // REMIND: this should rarely (never?) happen, but what if
 152             // default config is not CGL?
 153             GraphicsEnvironment env = GraphicsEnvironment
 154                 .getLocalGraphicsEnvironment();
 155             GraphicsDevice gd = env.getDefaultScreenDevice();
 156             return (CGLGraphicsConfig) gd.getDefaultConfiguration();
 157         }
 158     }
 159 
 160     public static CGLGraphicsConfig getGC(CGLLayer layer) {
 161         return (CGLGraphicsConfig)layer.getGraphicsConfiguration();
 162     }
 163 
 164     public void validate() {
 165         // Overridden in CGLWindowSurfaceData below
 166     }
 167 
 168     @Override
 169     public double getDefaultScaleX() {
 170         return scale;
 171     }
 172 
 173     @Override
 174     public double getDefaultScaleY() {
 175         return scale;
 176     }
 177 
 178     @Override
 179     public boolean copyArea(SunGraphics2D sg2d, int x, int y, int w, int h,
 180                             int dx, int dy) {
 181         final int state = sg2d.transformState;
 182         if (state > SunGraphics2D.TRANSFORM_TRANSLATESCALE
 183             || sg2d.compositeState >= SunGraphics2D.COMP_XOR) {
 184             return false;
 185         }
 186         if (state <= SunGraphics2D.TRANSFORM_ANY_TRANSLATE) {
 187             x += sg2d.transX;
 188             y += sg2d.transY;
 189         } else if (state == SunGraphics2D.TRANSFORM_TRANSLATESCALE) {
 190             final double[] coords = {x, y, x + w, y + h, x + dx, y + dy};
 191             sg2d.transform.transform(coords, 0, coords, 0, 3);
 192             x = (int) Math.ceil(coords[0] - 0.5);
 193             y = (int) Math.ceil(coords[1] - 0.5);
 194             w = ((int) Math.ceil(coords[2] - 0.5)) - x;
 195             h = ((int) Math.ceil(coords[3] - 0.5)) - y;
 196             dx = ((int) Math.ceil(coords[4] - 0.5)) - x;
 197             dy = ((int) Math.ceil(coords[5] - 0.5)) - y;
 198         }
 199         oglRenderPipe.copyArea(sg2d, x, y, w, h, dx, dy);
 200         return true;
 201     }
 202 
 203     protected native void clearWindow();
 204 
 205     public static class CGLWindowSurfaceData extends CGLSurfaceData {
 206 
 207         public CGLWindowSurfaceData(CPlatformView pView,
 208                 CGLGraphicsConfig gc) {
 209             super(pView, gc, gc.getColorModel(), WINDOW, 0, 0);
 210         }
 211 
 212         @Override
 213         public SurfaceData getReplacement() {
 214             return pView.getSurfaceData();
 215         }
 216 
 217         @Override
 218         public Rectangle getBounds() {
 219             Rectangle r = pView.getBounds();
 220             return new Rectangle(0, 0, r.width, r.height);
 221         }
 222 
 223         /**
 224          * Returns destination Component associated with this SurfaceData.
 225          */
 226         @Override
 227         public Object getDestination() {
 228             return pView.getDestination();
 229         }
 230 
 231         public void validate() {
 232             OGLRenderQueue rq = OGLRenderQueue.getInstance();
 233             rq.lock();
 234             try {
 235                 rq.flushAndInvokeNow(new Runnable() {
 236                     public void run() {
 237                         Rectangle peerBounds = pView.getBounds();
 238                         validate(0, 0, peerBounds.width, peerBounds.height, pView.isOpaque());
 239                     }
 240                 });
 241             } finally {
 242                 rq.unlock();
 243             }
 244         }
 245 
 246         @Override
 247         public void invalidate() {
 248             super.invalidate();
 249             clearWindow();
 250         }
 251     }
 252 
 253     /**
 254      * A surface which implements an intermediate buffer between
 255      * the Java2D flusher thread and the AppKit thread.
 256      *
 257      * This surface serves as a buffer attached to a CGLLayer and
 258      * the layer redirects all painting to the buffer's graphics.
 259      */
 260     public static class CGLLayerSurfaceData extends CGLSurfaceData {
 261 
 262         private CGLLayer layer;
 263 
 264         public CGLLayerSurfaceData(CGLLayer layer, CGLGraphicsConfig gc,
 265                                    int width, int height) {
 266             super(layer, gc, gc.getColorModel(), FBOBJECT, width, height);
 267             this.layer = layer;
 268             initSurface(this.width, this.height);
 269         }
 270 
 271         @Override
 272         public SurfaceData getReplacement() {
 273             return layer.getSurfaceData();
 274         }
 275 
 276         @Override
 277         boolean isOnScreen() {
 278             return true;
 279         }
 280 
 281         @Override
 282         public Rectangle getBounds() {
 283             return new Rectangle(width, height);
 284         }
 285 
 286         @Override
 287         public Object getDestination() {
 288             return layer.getDestination();
 289         }
 290 
 291         @Override
 292         public int getTransparency() {
 293             return layer.getTransparency();
 294         }
 295 
 296         @Override
 297         public void invalidate() {
 298             super.invalidate();
 299             clearWindow();
 300         }
 301     }
 302 
 303     /**
 304      * A surface which implements a v-synced flip back-buffer with COPIED
 305      * FlipContents.
 306      *
 307      * This surface serves as a back-buffer to the outside world, while it is
 308      * actually an offscreen surface. When the BufferStrategy this surface
 309      * belongs to is showed, it is first copied to the real private
 310      * FLIP_BACKBUFFER, which is then flipped.
 311      */
 312     public static class CGLVSyncOffScreenSurfaceData extends
 313             CGLOffScreenSurfaceData {
 314         private CGLOffScreenSurfaceData flipSurface;
 315 
 316         public CGLVSyncOffScreenSurfaceData(CPlatformView pView,
 317                 CGLGraphicsConfig gc, int width, int height, Image image,
 318                 ColorModel cm, int type) {
 319             super(pView, gc, width, height, image, cm, type);
 320             flipSurface = CGLSurfaceData.createData(pView, image,
 321                     FLIP_BACKBUFFER);
 322         }
 323 
 324         public SurfaceData getFlipSurface() {
 325             return flipSurface;
 326         }
 327 
 328         @Override
 329         public void flush() {
 330             flipSurface.flush();
 331             super.flush();
 332         }
 333     }
 334 
 335     public static class CGLOffScreenSurfaceData extends CGLSurfaceData {
 336         private Image offscreenImage;
 337 
 338         public CGLOffScreenSurfaceData(CPlatformView pView,
 339                                        CGLGraphicsConfig gc, int width, int height, Image image,
 340                                        ColorModel cm, int type) {
 341             super(pView, gc, cm, type, width, height);
 342             offscreenImage = image;
 343             initSurface(this.width, this.height);
 344         }
 345 
 346         @Override
 347         public SurfaceData getReplacement() {
 348             return restoreContents(offscreenImage);
 349         }
 350 
 351         @Override
 352         public Rectangle getBounds() {
 353             if (type == FLIP_BACKBUFFER) {
 354                 Rectangle r = pView.getBounds();
 355                 return new Rectangle(0, 0, r.width, r.height);
 356             } else {
 357                 return new Rectangle(width, height);
 358             }
 359         }
 360 
 361         /**
 362          * Returns destination Image associated with this SurfaceData.
 363          */
 364         @Override
 365         public Object getDestination() {
 366             return offscreenImage;
 367         }
 368     }
 369 
 370     // Mac OS X specific APIs for JOGL/Java2D bridge...
 371 
 372     // given a surface create and attach GL context, then return it
 373     private native static long createCGLContextOnSurface(CGLSurfaceData sd,
 374             long sharedContext);
 375 
 376     public static long createOGLContextOnSurface(Graphics g, long sharedContext) {
 377         SurfaceData sd = ((SunGraphics2D) g).surfaceData;
 378         if ((sd instanceof CGLSurfaceData) == true) {
 379             CGLSurfaceData cglsd = (CGLSurfaceData) sd;
 380             return createCGLContextOnSurface(cglsd, sharedContext);
 381         } else {
 382             return 0L;
 383         }
 384     }
 385 
 386     // returns whether or not the makeCurrent operation succeeded
 387     native static boolean makeCGLContextCurrentOnSurface(CGLSurfaceData sd,
 388             long ctx);
 389 
 390     public static boolean makeOGLContextCurrentOnSurface(Graphics g, long ctx) {
 391         SurfaceData sd = ((SunGraphics2D) g).surfaceData;
 392         if ((ctx != 0L) && ((sd instanceof CGLSurfaceData) == true)) {
 393             CGLSurfaceData cglsd = (CGLSurfaceData) sd;
 394             return makeCGLContextCurrentOnSurface(cglsd, ctx);
 395         } else {
 396             return false;
 397         }
 398     }
 399 
 400     // additional cleanup
 401     private native static void destroyCGLContext(long ctx);
 402 
 403     public static void destroyOGLContext(long ctx) {
 404         if (ctx != 0L) {
 405             destroyCGLContext(ctx);
 406         }
 407     }
 408 }
--- EOF ---