1 /*
   2  * Copyright (c) 2003, 2014, 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.*;
  29 import java.awt.image.ColorModel;
  30 import java.awt.image.Raster;
  31 import sun.awt.SunHints;
  32 import sun.awt.image.PixelConverter;
  33 import sun.java2d.pipe.hw.AccelSurface;
  34 import sun.java2d.SunGraphics2D;
  35 import sun.java2d.SurfaceData;
  36 import sun.java2d.SurfaceDataProxy;
  37 import sun.java2d.loops.CompositeType;
  38 import sun.java2d.loops.GraphicsPrimitive;
  39 import sun.java2d.loops.MaskFill;
  40 import sun.java2d.loops.SurfaceType;
  41 import sun.java2d.pipe.ParallelogramPipe;
  42 import sun.java2d.pipe.PixelToParallelogramConverter;
  43 import sun.java2d.pipe.RenderBuffer;
  44 import sun.java2d.pipe.TextPipe;
  45 import static sun.java2d.pipe.BufferedOpCodes.*;
  46 import static sun.java2d.opengl.OGLContext.OGLContextCaps.*;
  47 
  48 /**
  49  * This class describes an OpenGL "surface", that is, a region of pixels
  50  * managed via OpenGL.  An OGLSurfaceData can be tagged with one of three
  51  * different SurfaceType objects for the purpose of registering loops, etc.
  52  * This diagram shows the hierarchy of OGL SurfaceTypes:
  53  *
  54  *                               Any
  55  *                             /     \
  56  *                 OpenGLSurface     OpenGLTexture
  57  *                      |
  58  *               OpenGLSurfaceRTT
  59  *
  60  * OpenGLSurface
  61  * This kind of surface can be rendered to using OpenGL APIs.  It is also
  62  * possible to copy an OpenGLSurface to another OpenGLSurface (or to itself).
  63  * This is typically accomplished by calling MakeContextCurrent(dstSD, srcSD)
  64  * and then calling glCopyPixels() (although there are other techniques to
  65  * achieve the same goal).
  66  *
  67  * OpenGLTexture
  68  * This kind of surface cannot be rendered to using OpenGL (in the same sense
  69  * as in OpenGLSurface).  However, it is possible to upload a region of pixels
  70  * to an OpenGLTexture object via glTexSubImage2D().  One can also copy a
  71  * surface of type OpenGLTexture to an OpenGLSurface by binding the texture
  72  * to a quad and then rendering it to the destination surface (this process
  73  * is known as "texture mapping").
  74  *
  75  * OpenGLSurfaceRTT
  76  * This kind of surface can be thought of as a sort of hybrid between
  77  * OpenGLSurface and OpenGLTexture, in that one can render to this kind of
  78  * surface as if it were of type OpenGLSurface, but the process of copying
  79  * this kind of surface to another is more like an OpenGLTexture.  (Note that
  80  * "RTT" stands for "render-to-texture".)
  81  *
  82  * In addition to these SurfaceType variants, we have also defined some
  83  * constants that describe in more detail the type of underlying OpenGL
  84  * surface.  This table helps explain the relationships between those
  85  * "type" constants and their corresponding SurfaceType:
  86  *
  87  * OGL Type          Corresponding SurfaceType
  88  * --------          -------------------------
  89  * WINDOW            OpenGLSurface
  90  * PBUFFER           OpenGLSurface
  91  * TEXTURE           OpenGLTexture
  92  * FLIP_BACKBUFFER   OpenGLSurface
  93  * FBOBJECT          OpenGLSurfaceRTT
  94  */
  95 public abstract class OGLSurfaceData extends SurfaceData
  96     implements AccelSurface {
  97 
  98     /**
  99      * OGL-specific surface types
 100      *
 101      * @see sun.java2d.pipe.hw.AccelSurface
 102      */
 103     public static final int PBUFFER         = RT_PLAIN;
 104     public static final int FBOBJECT        = RT_TEXTURE;
 105 
 106     /**
 107      * Pixel formats
 108      */
 109     public static final int PF_INT_ARGB        = 0;
 110     public static final int PF_INT_ARGB_PRE    = 1;
 111     public static final int PF_INT_RGB         = 2;
 112     public static final int PF_INT_RGBX        = 3;
 113     public static final int PF_INT_BGR         = 4;
 114     public static final int PF_INT_BGRX        = 5;
 115     public static final int PF_USHORT_565_RGB  = 6;
 116     public static final int PF_USHORT_555_RGB  = 7;
 117     public static final int PF_USHORT_555_RGBX = 8;
 118     public static final int PF_BYTE_GRAY       = 9;
 119     public static final int PF_USHORT_GRAY     = 10;
 120     public static final int PF_3BYTE_BGR       = 11;
 121 
 122     /**
 123      * SurfaceTypes
 124      */
 125     private static final String DESC_OPENGL_SURFACE = "OpenGL Surface";
 126     private static final String DESC_OPENGL_SURFACE_RTT =
 127         "OpenGL Surface (render-to-texture)";
 128     private static final String DESC_OPENGL_TEXTURE = "OpenGL Texture";
 129 
 130     static final SurfaceType OpenGLSurface =
 131         SurfaceType.Any.deriveSubType(DESC_OPENGL_SURFACE,
 132                                       PixelConverter.ArgbPre.instance);
 133     static final SurfaceType OpenGLSurfaceRTT =
 134         OpenGLSurface.deriveSubType(DESC_OPENGL_SURFACE_RTT);
 135     static final SurfaceType OpenGLTexture =
 136         SurfaceType.Any.deriveSubType(DESC_OPENGL_TEXTURE);
 137 
 138     /** This will be true if the fbobject system property has been enabled. */
 139     private static boolean isFBObjectEnabled;
 140 
 141     /** This will be true if the lcdshader system property has been enabled.*/
 142     private static boolean isLCDShaderEnabled;
 143 
 144     /** This will be true if the biopshader system property has been enabled.*/
 145     private static boolean isBIOpShaderEnabled;
 146 
 147     /** This will be true if the gradshader system property has been enabled.*/
 148     private static boolean isGradShaderEnabled;
 149 
 150     private OGLGraphicsConfig graphicsConfig;
 151     protected int type;
 152     // these fields are set from the native code when the surface is
 153     // initialized
 154     private int nativeWidth, nativeHeight;
 155 
 156     protected static OGLRenderer oglRenderPipe;
 157     protected static PixelToParallelogramConverter oglTxRenderPipe;
 158     protected static ParallelogramPipe oglAAPgramPipe;
 159     protected static OGLTextRenderer oglTextPipe;
 160     protected static OGLDrawImage oglImagePipe;
 161 
 162     protected native boolean initTexture(long pData,
 163                                          boolean isOpaque, boolean texNonPow2,
 164                                          boolean texRect,
 165                                          int width, int height);
 166     protected native boolean initFBObject(long pData,
 167                                           boolean isOpaque, boolean texNonPow2,
 168                                           boolean texRect,
 169                                           int width, int height);
 170     protected native boolean initFlipBackbuffer(long pData);
 171     protected abstract boolean initPbuffer(long pData, long pConfigInfo,
 172                                            boolean isOpaque,
 173                                            int width, int height);
 174 
 175     private native int getTextureTarget(long pData);
 176     private native int getTextureID(long pData);
 177 
 178     static {
 179         if (!GraphicsEnvironment.isHeadless()) {
 180             // fbobject currently enabled by default; use "false" to disable
 181             String fbo = java.security.AccessController.doPrivileged(
 182                 new sun.security.action.GetPropertyAction(
 183                     "sun.java2d.opengl.fbobject"));
 184             isFBObjectEnabled = !"false".equals(fbo);
 185 
 186             // lcdshader currently enabled by default; use "false" to disable
 187             String lcd = java.security.AccessController.doPrivileged(
 188                 new sun.security.action.GetPropertyAction(
 189                     "sun.java2d.opengl.lcdshader"));
 190             isLCDShaderEnabled = !"false".equals(lcd);
 191 
 192             // biopshader currently enabled by default; use "false" to disable
 193             String biop = java.security.AccessController.doPrivileged(
 194                 new sun.security.action.GetPropertyAction(
 195                     "sun.java2d.opengl.biopshader"));
 196             isBIOpShaderEnabled = !"false".equals(biop);
 197 
 198             // gradshader currently enabled by default; use "false" to disable
 199             String grad = java.security.AccessController.doPrivileged(
 200                 new sun.security.action.GetPropertyAction(
 201                     "sun.java2d.opengl.gradshader"));
 202             isGradShaderEnabled = !"false".equals(grad);
 203 
 204             OGLRenderQueue rq = OGLRenderQueue.getInstance();
 205             oglImagePipe = new OGLDrawImage();
 206             oglTextPipe = new OGLTextRenderer(rq);
 207             oglRenderPipe = new OGLRenderer(rq);
 208             if (GraphicsPrimitive.tracingEnabled()) {
 209                 oglTextPipe = oglTextPipe.traceWrap();
 210                 //The wrapped oglRenderPipe will wrap the AA pipe as well...
 211                 //oglAAPgramPipe = oglRenderPipe.traceWrap();
 212             }
 213             oglAAPgramPipe = oglRenderPipe.getAAParallelogramPipe();
 214             oglTxRenderPipe =
 215                 new PixelToParallelogramConverter(oglRenderPipe,
 216                                                   oglRenderPipe,
 217                                                   1.0, 0.25, true);
 218 
 219             OGLBlitLoops.register();
 220             OGLMaskFill.register();
 221             OGLMaskBlit.register();
 222         }
 223     }
 224 
 225     protected OGLSurfaceData(OGLGraphicsConfig gc,
 226                              ColorModel cm, int type)
 227     {
 228         super(getCustomSurfaceType(type), cm);
 229         this.graphicsConfig = gc;
 230         this.type = type;
 231         setBlitProxyKey(gc.getProxyKey());
 232     }
 233 
 234     @Override
 235     public SurfaceDataProxy makeProxyFor(SurfaceData srcData) {
 236         return OGLSurfaceDataProxy.createProxy(srcData, graphicsConfig);
 237     }
 238 
 239     /**
 240      * Returns the appropriate SurfaceType corresponding to the given OpenGL
 241      * surface type constant (e.g. TEXTURE -> OpenGLTexture).
 242      */
 243     private static SurfaceType getCustomSurfaceType(int oglType) {
 244         switch (oglType) {
 245         case TEXTURE:
 246             return OpenGLTexture;
 247         case FBOBJECT:
 248             return OpenGLSurfaceRTT;
 249         case PBUFFER:
 250         default:
 251             return OpenGLSurface;
 252         }
 253     }
 254 
 255     /**
 256      * Note: This should only be called from the QFT under the AWT lock.
 257      * This method is kept separate from the initSurface() method below just
 258      * to keep the code a bit cleaner.
 259      */
 260     private void initSurfaceNow(int width, int height) {
 261         boolean isOpaque = (getTransparency() == Transparency.OPAQUE);
 262         boolean success = false;
 263 
 264         switch (type) {
 265         case PBUFFER:
 266             success = initPbuffer(getNativeOps(),
 267                                   graphicsConfig.getNativeConfigInfo(),
 268                                   isOpaque,
 269                                   width, height);
 270             break;
 271 
 272         case TEXTURE:
 273             success = initTexture(getNativeOps(),
 274                                   isOpaque, isTexNonPow2Available(),
 275                                   isTexRectAvailable(),
 276                                   width, height);
 277             break;
 278 
 279         case FBOBJECT:
 280             success = initFBObject(getNativeOps(),
 281                                    isOpaque, isTexNonPow2Available(),
 282                                    isTexRectAvailable(),
 283                                    width, height);
 284             break;
 285 
 286         case FLIP_BACKBUFFER:
 287             success = initFlipBackbuffer(getNativeOps());
 288             break;
 289 
 290         default:
 291             break;
 292         }
 293 
 294         if (!success) {
 295             throw new OutOfMemoryError("can't create offscreen surface");
 296         }
 297     }
 298 
 299     /**
 300      * Initializes the appropriate OpenGL offscreen surface based on the value
 301      * of the type parameter.  If the surface creation fails for any reason,
 302      * an OutOfMemoryError will be thrown.
 303      */
 304     protected void initSurface(final int width, final int height) {
 305         OGLRenderQueue rq = OGLRenderQueue.getInstance();
 306         rq.lock();
 307         try {
 308             switch (type) {
 309             case TEXTURE:
 310             case PBUFFER:
 311             case FBOBJECT:
 312                 // need to make sure the context is current before
 313                 // creating the texture (or pbuffer, or fbobject)
 314                 OGLContext.setScratchSurface(graphicsConfig);
 315                 break;
 316             default:
 317                 break;
 318             }
 319             rq.flushAndInvokeNow(new Runnable() {
 320                 public void run() {
 321                     initSurfaceNow(width, height);
 322                 }
 323             });
 324         } finally {
 325             rq.unlock();
 326         }
 327     }
 328 
 329     /**
 330      * Returns the OGLContext for the GraphicsConfig associated with this
 331      * surface.
 332      */
 333     public final OGLContext getContext() {
 334         return graphicsConfig.getContext();
 335     }
 336 
 337     /**
 338      * Returns the OGLGraphicsConfig associated with this surface.
 339      */
 340     final OGLGraphicsConfig getOGLGraphicsConfig() {
 341         return graphicsConfig;
 342     }
 343 
 344     /**
 345      * Returns one of the surface type constants defined above.
 346      */
 347     public final int getType() {
 348         return type;
 349     }
 350 
 351     /**
 352      * If this surface is backed by a texture object, returns the target
 353      * for that texture (either GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE_ARB).
 354      * Otherwise, this method will return zero.
 355      */
 356     public final int getTextureTarget() {
 357         return getTextureTarget(getNativeOps());
 358     }
 359 
 360     /**
 361      * If this surface is backed by a texture object, returns the texture ID
 362      * for that texture.
 363      * Otherwise, this method will return zero.
 364      */
 365     public final int getTextureID() {
 366         return getTextureID(getNativeOps());
 367     }
 368 
 369     /**
 370      * Returns native resource of specified {@code resType} associated with
 371      * this surface.
 372      *
 373      * Specifically, for {@code OGLSurfaceData} this method returns the
 374      * the following:
 375      * <pre>
 376      * TEXTURE              - texture id
 377      * </pre>
 378      *
 379      * Note: the resource returned by this method is only valid on the rendering
 380      * thread.
 381      *
 382      * @return native resource of specified type or 0L if
 383      * such resource doesn't exist or can not be retrieved.
 384      * @see sun.java2d.pipe.hw.AccelSurface#getNativeResource
 385      */
 386     public long getNativeResource(int resType) {
 387         if (resType == TEXTURE) {
 388             return getTextureID();
 389         }
 390         return 0L;
 391     }
 392 
 393     public Raster getRaster(int x, int y, int w, int h) {
 394         throw new InternalError("not implemented yet");
 395     }
 396 
 397     /**
 398      * For now, we can only render LCD text if:
 399      *   - the fragment shader extension is available, and
 400      *   - the source color is opaque, and
 401      *   - blending is SrcOverNoEa or disabled
 402      *
 403      * Eventually, we could enhance the native OGL text rendering code
 404      * and remove the above restrictions, but that would require significantly
 405      * more code just to support a few uncommon cases.
 406      */
 407     public boolean canRenderLCDText(SunGraphics2D sg2d) {
 408         return
 409             graphicsConfig.isCapPresent(CAPS_EXT_LCD_SHADER) &&
 410             sg2d.surfaceData.getTransparency() == Transparency.OPAQUE &&
 411             sg2d.paintState <= SunGraphics2D.PAINT_OPAQUECOLOR &&
 412             (sg2d.compositeState <= SunGraphics2D.COMP_ISCOPY ||
 413              (sg2d.compositeState <= SunGraphics2D.COMP_ALPHA && canHandleComposite(sg2d.composite)));
 414     }
 415 
 416     private boolean canHandleComposite(Composite c) {
 417         if (c instanceof AlphaComposite) {
 418             AlphaComposite ac = (AlphaComposite)c;
 419 
 420             return ac.getRule() == AlphaComposite.SRC_OVER && ac.getAlpha() >= 1f;
 421         }
 422         return false;
 423     }
 424 
 425     public void validatePipe(SunGraphics2D sg2d) {
 426         TextPipe textpipe;
 427         boolean validated = false;
 428 
 429         // OGLTextRenderer handles both AA and non-AA text, but
 430         // only works with the following modes:
 431         // (Note: For LCD text we only enter this code path if
 432         // canRenderLCDText() has already validated that the mode is
 433         // CompositeType.SrcNoEa (opaque color), which will be subsumed
 434         // by the CompositeType.SrcNoEa (any color) test below.)
 435 
 436         if (/* CompositeType.SrcNoEa (any color) */
 437             (sg2d.compositeState <= SunGraphics2D.COMP_ISCOPY &&
 438              sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR)         ||
 439 
 440             /* CompositeType.SrcOver (any color) */
 441             (sg2d.compositeState == SunGraphics2D.COMP_ALPHA   &&
 442              sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR &&
 443              (((AlphaComposite)sg2d.composite).getRule() ==
 444               AlphaComposite.SRC_OVER))                                 ||
 445 
 446             /* CompositeType.Xor (any color) */
 447             (sg2d.compositeState == SunGraphics2D.COMP_XOR &&
 448              sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR))
 449         {
 450             textpipe = oglTextPipe;
 451         } else {
 452             // do this to initialize textpipe correctly; we will attempt
 453             // to override the non-text pipes below
 454             super.validatePipe(sg2d);
 455             textpipe = sg2d.textpipe;
 456             validated = true;
 457         }
 458 
 459         PixelToParallelogramConverter txPipe = null;
 460         OGLRenderer nonTxPipe = null;
 461 
 462         if (sg2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {
 463             if (sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR) {
 464                 if (sg2d.compositeState <= SunGraphics2D.COMP_XOR) {
 465                     txPipe = oglTxRenderPipe;
 466                     nonTxPipe = oglRenderPipe;
 467                 }
 468             } else if (sg2d.compositeState <= SunGraphics2D.COMP_ALPHA) {
 469                 if (OGLPaints.isValid(sg2d)) {
 470                     txPipe = oglTxRenderPipe;
 471                     nonTxPipe = oglRenderPipe;
 472                 }
 473                 // custom paints handled by super.validatePipe() below
 474             }
 475         } else {
 476             if (sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR) {
 477                 if (graphicsConfig.isCapPresent(CAPS_PS30) &&
 478                     (sg2d.imageComp == CompositeType.SrcOverNoEa ||
 479                      sg2d.imageComp == CompositeType.SrcOver))
 480                 {
 481                     if (!validated) {
 482                         super.validatePipe(sg2d);
 483                         validated = true;
 484                     }
 485                     PixelToParallelogramConverter aaConverter =
 486                         new PixelToParallelogramConverter(sg2d.shapepipe,
 487                                                           oglAAPgramPipe,
 488                                                           1.0/8.0, 0.499,
 489                                                           false);
 490                     sg2d.drawpipe = aaConverter;
 491                     sg2d.fillpipe = aaConverter;
 492                     sg2d.shapepipe = aaConverter;
 493                 } else if (sg2d.compositeState == SunGraphics2D.COMP_XOR) {
 494                     // install the solid pipes when AA and XOR are both enabled
 495                     txPipe = oglTxRenderPipe;
 496                     nonTxPipe = oglRenderPipe;
 497                 }
 498             }
 499             // other cases handled by super.validatePipe() below
 500         }
 501 
 502         if (txPipe != null) {
 503             if (sg2d.transformState >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) {
 504                 sg2d.drawpipe = txPipe;
 505                 sg2d.fillpipe = txPipe;
 506             } else if (sg2d.strokeState != SunGraphics2D.STROKE_THIN) {
 507                 sg2d.drawpipe = txPipe;
 508                 sg2d.fillpipe = nonTxPipe;
 509             } else {
 510                 sg2d.drawpipe = nonTxPipe;
 511                 sg2d.fillpipe = nonTxPipe;
 512             }
 513             // Note that we use the transforming pipe here because it
 514             // will examine the shape and possibly perform an optimized
 515             // operation if it can be simplified.  The simplifications
 516             // will be valid for all STROKE and TRANSFORM types.
 517             sg2d.shapepipe = txPipe;
 518         } else {
 519             if (!validated) {
 520                 super.validatePipe(sg2d);
 521             }
 522         }
 523 
 524         // install the text pipe based on our earlier decision
 525         sg2d.textpipe = textpipe;
 526 
 527         // always override the image pipe with the specialized OGL pipe
 528         sg2d.imagepipe = oglImagePipe;
 529     }
 530 
 531     @Override
 532     protected MaskFill getMaskFill(SunGraphics2D sg2d) {
 533         if (sg2d.paintState > SunGraphics2D.PAINT_ALPHACOLOR) {
 534             /*
 535              * We can only accelerate non-Color MaskFill operations if
 536              * all of the following conditions hold true:
 537              *   - there is an implementation for the given paintState
 538              *   - the current Paint can be accelerated for this destination
 539              *   - multitexturing is available (since we need to modulate
 540              *     the alpha mask texture with the paint texture)
 541              *
 542              * In all other cases, we return null, in which case the
 543              * validation code will choose a more general software-based loop.
 544              */
 545             if (!OGLPaints.isValid(sg2d) ||
 546                 !graphicsConfig.isCapPresent(CAPS_MULTITEXTURE))
 547             {
 548                 return null;
 549             }
 550         }
 551         return super.getMaskFill(sg2d);
 552     }
 553 
 554     public boolean copyArea(SunGraphics2D sg2d,
 555                             int x, int y, int w, int h, int dx, int dy)
 556     {
 557         if (sg2d.transformState < SunGraphics2D.TRANSFORM_TRANSLATESCALE &&
 558             sg2d.compositeState < SunGraphics2D.COMP_XOR)
 559         {
 560             x += sg2d.transX;
 561             y += sg2d.transY;
 562 
 563             oglRenderPipe.copyArea(sg2d, x, y, w, h, dx, dy);
 564 
 565             return true;
 566         }
 567         return false;
 568     }
 569 
 570     public void flush() {
 571         invalidate();
 572         OGLRenderQueue rq = OGLRenderQueue.getInstance();
 573         rq.lock();
 574         try {
 575             // make sure we have a current context before
 576             // disposing the native resources (e.g. texture object)
 577             OGLContext.setScratchSurface(graphicsConfig);
 578 
 579             RenderBuffer buf = rq.getBuffer();
 580             rq.ensureCapacityAndAlignment(12, 4);
 581             buf.putInt(FLUSH_SURFACE);
 582             buf.putLong(getNativeOps());
 583 
 584             // this call is expected to complete synchronously, so flush now
 585             rq.flushNow();
 586         } finally {
 587             rq.unlock();
 588         }
 589     }
 590 
 591     /**
 592      * Disposes the native resources associated with the given OGLSurfaceData
 593      * (referenced by the pData parameter).  This method is invoked from
 594      * the native Dispose() method from the Disposer thread when the
 595      * Java-level OGLSurfaceData object is about to go away.  Note that we
 596      * also pass a reference to the native GLX/WGLGraphicsConfigInfo
 597      * (pConfigInfo) for the purposes of making a context current.
 598      */
 599     static void dispose(long pData, long pConfigInfo) {
 600         OGLRenderQueue rq = OGLRenderQueue.getInstance();
 601         rq.lock();
 602         try {
 603             // make sure we have a current context before
 604             // disposing the native resources (e.g. texture object)
 605             OGLContext.setScratchSurface(pConfigInfo);
 606 
 607             RenderBuffer buf = rq.getBuffer();
 608             rq.ensureCapacityAndAlignment(12, 4);
 609             buf.putInt(DISPOSE_SURFACE);
 610             buf.putLong(pData);
 611 
 612             // this call is expected to complete synchronously, so flush now
 613             rq.flushNow();
 614         } finally {
 615             rq.unlock();
 616         }
 617     }
 618 
 619     static void swapBuffers(long window) {
 620         OGLRenderQueue rq = OGLRenderQueue.getInstance();
 621         rq.lock();
 622         try {
 623             RenderBuffer buf = rq.getBuffer();
 624             rq.ensureCapacityAndAlignment(12, 4);
 625             buf.putInt(SWAP_BUFFERS);
 626             buf.putLong(window);
 627             rq.flushNow();
 628         } finally {
 629             rq.unlock();
 630         }
 631     }
 632 
 633     /**
 634      * Returns true if OpenGL textures can have non-power-of-two dimensions
 635      * when using the basic GL_TEXTURE_2D target.
 636      */
 637     boolean isTexNonPow2Available() {
 638         return graphicsConfig.isCapPresent(CAPS_TEXNONPOW2);
 639     }
 640 
 641     /**
 642      * Returns true if OpenGL textures can have non-power-of-two dimensions
 643      * when using the GL_TEXTURE_RECTANGLE_ARB target (only available when the
 644      * GL_ARB_texture_rectangle extension is present).
 645      */
 646     boolean isTexRectAvailable() {
 647         return graphicsConfig.isCapPresent(CAPS_EXT_TEXRECT);
 648     }
 649 
 650     public Rectangle getNativeBounds() {
 651         OGLRenderQueue rq = OGLRenderQueue.getInstance();
 652         rq.lock();
 653         try {
 654             return new Rectangle(nativeWidth, nativeHeight);
 655         } finally {
 656             rq.unlock();
 657         }
 658     }
 659 
 660     /**
 661      * Returns true if the surface is an on-screen window surface or
 662      * a FBO texture attached to an on-screen CALayer.
 663      *
 664      * Needed by Mac OS X port.
 665      */
 666     boolean isOnScreen() {
 667         return getType() == WINDOW;
 668     }
 669 }