1 /*
   2  * Copyright (c) 2004, 2008, 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 #ifndef HEADLESS
  27 
  28 #include <stdlib.h>
  29 #include <string.h>
  30 
  31 #include "sun_java2d_SunGraphics2D.h"
  32 
  33 #include "jlong.h"
  34 #include "jni_util.h"
  35 #include "OGLContext.h"
  36 #include "OGLRenderQueue.h"
  37 #include "OGLSurfaceData.h"
  38 #include "GraphicsPrimitiveMgr.h"
  39 #include "Region.h"
  40 
  41 /**
  42  * The following methods are implemented in the windowing system (i.e. GLX
  43  * and WGL) source files.
  44  */
  45 extern jboolean OGLSD_InitOGLWindow(JNIEnv *env, OGLSDOps *oglsdo);
  46 extern OGLContext *OGLSD_MakeOGLContextCurrent(JNIEnv *env,
  47                                                OGLSDOps *srcOps,
  48                                                OGLSDOps *dstOps);
  49 
  50 /**
  51  * This table contains the standard blending rules (or Porter-Duff compositing
  52  * factors) used in glBlendFunc(), indexed by the rule constants from the
  53  * AlphaComposite class.
  54  */
  55 OGLBlendRule StdBlendRules[] = {
  56     { GL_ZERO,                GL_ZERO                }, /* 0 - Nothing      */
  57     { GL_ZERO,                GL_ZERO                }, /* 1 - RULE_Clear   */
  58     { GL_ONE,                 GL_ZERO                }, /* 2 - RULE_Src     */
  59     { GL_ONE,                 GL_ONE_MINUS_SRC_ALPHA }, /* 3 - RULE_SrcOver */
  60     { GL_ONE_MINUS_DST_ALPHA, GL_ONE                 }, /* 4 - RULE_DstOver */
  61     { GL_DST_ALPHA,           GL_ZERO                }, /* 5 - RULE_SrcIn   */
  62     { GL_ZERO,                GL_SRC_ALPHA           }, /* 6 - RULE_DstIn   */
  63     { GL_ONE_MINUS_DST_ALPHA, GL_ZERO                }, /* 7 - RULE_SrcOut  */
  64     { GL_ZERO,                GL_ONE_MINUS_SRC_ALPHA }, /* 8 - RULE_DstOut  */
  65     { GL_ZERO,                GL_ONE                 }, /* 9 - RULE_Dst     */
  66     { GL_DST_ALPHA,           GL_ONE_MINUS_SRC_ALPHA }, /*10 - RULE_SrcAtop */
  67     { GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA           }, /*11 - RULE_DstAtop */
  68     { GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, /*12 - RULE_AlphaXor*/
  69 };
  70 
  71 /** Evaluates to "front" or "back", depending on the value of buf. */
  72 #define OGLC_ACTIVE_BUFFER_NAME(buf) \
  73     (buf == GL_FRONT || buf == GL_COLOR_ATTACHMENT0_EXT) ? "front" : "back"
  74 
  75 /**
  76  * Initializes the viewport and projection matrix, effectively positioning
  77  * the origin at the top-left corner of the surface.  This allows Java 2D
  78  * coordinates to be passed directly to OpenGL, which is typically based on
  79  * a bottom-right coordinate system.  This method also sets the appropriate
  80  * read and draw buffers.
  81  */
  82 static void
  83 OGLContext_SetViewport(OGLSDOps *srcOps, OGLSDOps *dstOps)
  84 {
  85     jint width = dstOps->width;
  86     jint height = dstOps->height;
  87 
  88     J2dTraceLn4(J2D_TRACE_INFO,
  89                 "OGLContext_SetViewport: w=%d h=%d read=%s draw=%s",
  90                 width, height,
  91                 OGLC_ACTIVE_BUFFER_NAME(srcOps->activeBuffer),
  92                 OGLC_ACTIVE_BUFFER_NAME(dstOps->activeBuffer));
  93 
  94     // set the viewport and projection matrix
  95     j2d_glViewport(dstOps->xOffset, dstOps->yOffset,
  96                    (GLsizei)width, (GLsizei)height);
  97     j2d_glMatrixMode(GL_PROJECTION);
  98     j2d_glLoadIdentity();
  99     j2d_glOrtho(0.0, (GLdouble)width, (GLdouble)height, 0.0, -1.0, 1.0);
 100 
 101     // set the active read and draw buffers
 102     j2d_glReadBuffer(srcOps->activeBuffer);
 103     j2d_glDrawBuffer(dstOps->activeBuffer);
 104 
 105     // set the color mask to enable alpha channel only when necessary
 106     j2d_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, (GLboolean)!dstOps->isOpaque);
 107 }
 108 
 109 /**
 110  * Initializes the alpha channel of the current surface so that it contains
 111  * fully opaque alpha values.
 112  */
 113 static void
 114 OGLContext_InitAlphaChannel()
 115 {
 116     GLboolean scissorEnabled;
 117 
 118     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_InitAlphaChannel");
 119 
 120     // it is possible for the scissor test to be enabled at this point;
 121     // if it is, disable it temporarily since it can affect the glClear() op
 122     scissorEnabled = j2d_glIsEnabled(GL_SCISSOR_TEST);
 123     if (scissorEnabled) {
 124         j2d_glDisable(GL_SCISSOR_TEST);
 125     }
 126 
 127     // set the color mask so that we only affect the alpha channel
 128     j2d_glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
 129 
 130     // clear the color buffer so that the alpha channel is fully opaque
 131     j2d_glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 132     j2d_glClear(GL_COLOR_BUFFER_BIT);
 133 
 134     // restore the color mask (as it was set in OGLContext_SetViewport())
 135     j2d_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
 136 
 137     // re-enable scissor test, only if it was enabled earlier
 138     if (scissorEnabled) {
 139         j2d_glEnable(GL_SCISSOR_TEST);
 140     }
 141 }
 142 
 143 /**
 144  * Fetches the OGLContext associated with the given destination surface,
 145  * makes the context current for those surfaces, updates the destination
 146  * viewport, and then returns a pointer to the OGLContext.
 147  */
 148 OGLContext *
 149 OGLContext_SetSurfaces(JNIEnv *env, jlong pSrc, jlong pDst)
 150 {
 151     OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrc);
 152     OGLSDOps *dstOps = (OGLSDOps *)jlong_to_ptr(pDst);
 153     OGLContext *oglc = NULL;
 154 
 155     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_SetSurfaces");
 156 
 157     if (srcOps == NULL || dstOps == NULL) {
 158         J2dRlsTraceLn(J2D_TRACE_ERROR,
 159             "OGLContext_SetSurfaces: ops are null");
 160         return NULL;
 161     }
 162 
 163     J2dTraceLn2(J2D_TRACE_VERBOSE, "  srctype=%d dsttype=%d",
 164                 srcOps->drawableType, dstOps->drawableType);
 165 
 166     if (dstOps->drawableType == OGLSD_TEXTURE) {
 167         J2dRlsTraceLn(J2D_TRACE_ERROR,
 168             "OGLContext_SetSurfaces: texture cannot be used as destination");
 169         return NULL;
 170     }
 171 
 172     if (dstOps->drawableType == OGLSD_UNDEFINED) {
 173         // initialize the surface as an OGLSD_WINDOW
 174         if (!OGLSD_InitOGLWindow(env, dstOps)) {
 175             J2dRlsTraceLn(J2D_TRACE_ERROR,
 176                 "OGLContext_SetSurfaces: could not init OGL window");
 177             return NULL;
 178         }
 179     }
 180 
 181     // make the context current
 182     oglc = OGLSD_MakeOGLContextCurrent(env, srcOps, dstOps);
 183     if (oglc == NULL) {
 184         J2dRlsTraceLn(J2D_TRACE_ERROR,
 185             "OGLContext_SetSurfaces: could not make context current");
 186         return NULL;
 187     }
 188 
 189     // update the viewport
 190     OGLContext_SetViewport(srcOps, dstOps);
 191 
 192     // perform additional one-time initialization, if necessary
 193     if (dstOps->needsInit) {
 194         if (dstOps->isOpaque) {
 195             // in this case we are treating the destination as opaque, but
 196             // to do so, first we need to ensure that the alpha channel
 197             // is filled with fully opaque values (see 6319663)
 198             OGLContext_InitAlphaChannel();
 199         }
 200         dstOps->needsInit = JNI_FALSE;
 201     }
 202 
 203     return oglc;
 204 }
 205 
 206 /**
 207  * Resets the current clip state (disables both scissor and depth tests).
 208  */
 209 void
 210 OGLContext_ResetClip(OGLContext *oglc)
 211 {
 212     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_ResetClip");
 213 
 214     RETURN_IF_NULL(oglc);
 215     CHECK_PREVIOUS_OP(OGL_STATE_CHANGE);
 216 
 217     j2d_glDisable(GL_SCISSOR_TEST);
 218     j2d_glDisable(GL_DEPTH_TEST);
 219 }
 220 
 221 /**
 222  * Sets the OpenGL scissor bounds to the provided rectangular clip bounds.
 223  */
 224 void
 225 OGLContext_SetRectClip(OGLContext *oglc, OGLSDOps *dstOps,
 226                        jint x1, jint y1, jint x2, jint y2)
 227 {
 228     jint width = x2 - x1;
 229     jint height = y2 - y1;
 230 
 231     J2dTraceLn4(J2D_TRACE_INFO,
 232                 "OGLContext_SetRectClip: x=%d y=%d w=%d h=%d",
 233                 x1, y1, width, height);
 234 
 235     RETURN_IF_NULL(dstOps);
 236     RETURN_IF_NULL(oglc);
 237     CHECK_PREVIOUS_OP(OGL_STATE_CHANGE);
 238 
 239     if ((width < 0) || (height < 0)) {
 240         // use an empty scissor rectangle when the region is empty
 241         width = 0;
 242         height = 0;
 243     }
 244 
 245     j2d_glDisable(GL_DEPTH_TEST);
 246     j2d_glEnable(GL_SCISSOR_TEST);
 247 
 248     // the scissor rectangle is specified using the lower-left
 249     // origin of the clip region (in the framebuffer's coordinate
 250     // space), so we must account for the x/y offsets of the
 251     // destination surface
 252     j2d_glScissor(dstOps->xOffset + x1,
 253                   dstOps->yOffset + dstOps->height - (y1 + height),
 254                   width, height);
 255 }
 256 
 257 /**
 258  * Sets up a complex (shape) clip using the OpenGL depth buffer.  This
 259  * method prepares the depth buffer so that the clip Region spans can
 260  * be "rendered" into it.  The depth buffer is first cleared, then the
 261  * depth func is setup so that when we render the clip spans,
 262  * nothing is rendered into the color buffer, but for each pixel that would
 263  * be rendered, a non-zero value is placed into that location in the depth
 264  * buffer.  With depth test enabled, pixels will only be rendered into the
 265  * color buffer if the corresponding value at that (x,y) location in the
 266  * depth buffer differs from the incoming depth value.
 267  */
 268 void
 269 OGLContext_BeginShapeClip(OGLContext *oglc)
 270 {
 271     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_BeginShapeClip");
 272 
 273     RETURN_IF_NULL(oglc);
 274     RESET_PREVIOUS_OP();
 275 
 276     j2d_glDisable(GL_SCISSOR_TEST);
 277 
 278     // enable depth test and clear depth buffer so that depth values are at
 279     // their maximum; also set the depth func to GL_ALWAYS so that the
 280     // depth values of the clip spans are forced into the depth buffer
 281     j2d_glEnable(GL_DEPTH_TEST);
 282     j2d_glClearDepth(1.0);
 283     j2d_glClear(GL_DEPTH_BUFFER_BIT);
 284     j2d_glDepthFunc(GL_ALWAYS);
 285 
 286     // disable writes into the color buffer while we set up the clip
 287     j2d_glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 288 
 289     // save current transform
 290     j2d_glMatrixMode(GL_MODELVIEW);
 291     j2d_glPushMatrix();
 292 
 293     // use identity transform plus slight translation in the z-axis when
 294     // setting the clip spans; this will push the clip spans (which would
 295     // normally be at z=0) to the z=1 plane to give them some depth
 296     j2d_glLoadIdentity();
 297     j2d_glTranslatef(0.0f, 0.0f, 1.0f);
 298 }
 299 
 300 /**
 301  * Finishes setting up the shape clip by resetting the depth func
 302  * so that future rendering operations will once again be written into the
 303  * color buffer (while respecting the clip set up in the depth buffer).
 304  */
 305 void
 306 OGLContext_EndShapeClip(OGLContext *oglc, OGLSDOps *dstOps)
 307 {
 308     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_EndShapeClip");
 309 
 310     RETURN_IF_NULL(dstOps);
 311     RETURN_IF_NULL(oglc);
 312     RESET_PREVIOUS_OP();
 313 
 314     // restore transform
 315     j2d_glPopMatrix();
 316 
 317     // re-enable writes into the color buffer
 318     j2d_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, (GLboolean)!dstOps->isOpaque);
 319 
 320     // enable the depth test so that only fragments within the clip region
 321     // (i.e. those fragments whose z-values are >= the values currently
 322     // stored in the depth buffer) are rendered
 323     j2d_glDepthFunc(GL_GEQUAL);
 324 }
 325 
 326 /**
 327  * Initializes the OpenGL state responsible for applying extra alpha.  This
 328  * step is only necessary for any operation that uses glDrawPixels() or
 329  * glCopyPixels() with a non-1.0f extra alpha value.  Since the source is
 330  * always premultiplied, we apply the extra alpha value to both alpha and
 331  * color components using GL_*_SCALE.
 332  */
 333 void
 334 OGLContext_SetExtraAlpha(jfloat ea)
 335 {
 336     J2dTraceLn1(J2D_TRACE_INFO, "OGLContext_SetExtraAlpha: ea=%f", ea);
 337 
 338     j2d_glPixelTransferf(GL_ALPHA_SCALE, ea);
 339     j2d_glPixelTransferf(GL_RED_SCALE, ea);
 340     j2d_glPixelTransferf(GL_GREEN_SCALE, ea);
 341     j2d_glPixelTransferf(GL_BLUE_SCALE, ea);
 342 }
 343 
 344 /**
 345  * Resets all OpenGL compositing state (disables blending and logic
 346  * operations).
 347  */
 348 void
 349 OGLContext_ResetComposite(OGLContext *oglc)
 350 {
 351     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_ResetComposite");
 352 
 353     RETURN_IF_NULL(oglc);
 354     CHECK_PREVIOUS_OP(OGL_STATE_CHANGE);
 355 
 356     // disable blending and XOR mode
 357     if (oglc->compState == sun_java2d_SunGraphics2D_COMP_ALPHA) {
 358         j2d_glDisable(GL_BLEND);
 359     } else if (oglc->compState == sun_java2d_SunGraphics2D_COMP_XOR) {
 360         j2d_glDisable(GL_COLOR_LOGIC_OP);
 361         j2d_glDisable(GL_ALPHA_TEST);
 362     }
 363 
 364     // set state to default values
 365     oglc->compState = sun_java2d_SunGraphics2D_COMP_ISCOPY;
 366     oglc->extraAlpha = 1.0f;
 367 }
 368 
 369 /**
 370  * Initializes the OpenGL blending state.  XOR mode is disabled and the
 371  * appropriate blend functions are setup based on the AlphaComposite rule
 372  * constant.
 373  */
 374 void
 375 OGLContext_SetAlphaComposite(OGLContext *oglc,
 376                              jint rule, jfloat extraAlpha, jint flags)
 377 {
 378     J2dTraceLn1(J2D_TRACE_INFO,
 379                 "OGLContext_SetAlphaComposite: flags=%d", flags);
 380 
 381     RETURN_IF_NULL(oglc);
 382     CHECK_PREVIOUS_OP(OGL_STATE_CHANGE);
 383 
 384     // disable XOR mode
 385     if (oglc->compState == sun_java2d_SunGraphics2D_COMP_XOR) {
 386         j2d_glDisable(GL_COLOR_LOGIC_OP);
 387         j2d_glDisable(GL_ALPHA_TEST);
 388     }
 389 
 390     // we can safely disable blending when:
 391     //   - comp is SrcNoEa or SrcOverNoEa, and
 392     //   - the source is opaque
 393     // (turning off blending can have a large positive impact on
 394     // performance)
 395     if ((rule == RULE_Src || rule == RULE_SrcOver) &&
 396         (extraAlpha == 1.0f) &&
 397         (flags & OGLC_SRC_IS_OPAQUE))
 398     {
 399         J2dTraceLn1(J2D_TRACE_VERBOSE,
 400                     "  disabling alpha comp: rule=%d ea=1.0 src=opq", rule);
 401         j2d_glDisable(GL_BLEND);
 402     } else {
 403         J2dTraceLn2(J2D_TRACE_VERBOSE,
 404                     "  enabling alpha comp: rule=%d ea=%f", rule, extraAlpha);
 405         j2d_glEnable(GL_BLEND);
 406         j2d_glBlendFunc(StdBlendRules[rule].src, StdBlendRules[rule].dst);
 407     }
 408 
 409     // update state
 410     oglc->compState = sun_java2d_SunGraphics2D_COMP_ALPHA;
 411     oglc->extraAlpha = extraAlpha;
 412 }
 413 
 414 /**
 415  * Initializes the OpenGL logic op state to XOR mode.  Blending is disabled
 416  * before enabling logic op mode.  The XOR pixel value will be applied
 417  * later in the OGLContext_SetColor() method.
 418  */
 419 void
 420 OGLContext_SetXorComposite(OGLContext *oglc, jint xorPixel)
 421 {
 422     J2dTraceLn1(J2D_TRACE_INFO,
 423                 "OGLContext_SetXorComposite: xorPixel=%08x", xorPixel);
 424 
 425     RETURN_IF_NULL(oglc);
 426     CHECK_PREVIOUS_OP(OGL_STATE_CHANGE);
 427 
 428     // disable blending mode
 429     if (oglc->compState == sun_java2d_SunGraphics2D_COMP_ALPHA) {
 430         j2d_glDisable(GL_BLEND);
 431     }
 432 
 433     // enable XOR mode
 434     j2d_glEnable(GL_COLOR_LOGIC_OP);
 435     j2d_glLogicOp(GL_XOR);
 436 
 437     // set up the alpha test so that we discard transparent fragments (this
 438     // is primarily useful for rendering text in XOR mode)
 439     j2d_glEnable(GL_ALPHA_TEST);
 440     j2d_glAlphaFunc(GL_NOTEQUAL, 0.0f);
 441 
 442     // update state
 443     oglc->compState = sun_java2d_SunGraphics2D_COMP_XOR;
 444     oglc->xorPixel = xorPixel;
 445     oglc->extraAlpha = 1.0f;
 446 }
 447 
 448 /**
 449  * Resets the OpenGL transform state back to the identity matrix.
 450  */
 451 void
 452 OGLContext_ResetTransform(OGLContext *oglc)
 453 {
 454     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_ResetTransform");
 455 
 456     RETURN_IF_NULL(oglc);
 457     CHECK_PREVIOUS_OP(OGL_STATE_CHANGE);
 458 
 459     j2d_glMatrixMode(GL_MODELVIEW);
 460     j2d_glLoadIdentity();
 461 }
 462 
 463 /**
 464  * Initializes the OpenGL transform state by setting the modelview transform
 465  * using the given matrix parameters.
 466  *
 467  * REMIND: it may be worthwhile to add serial id to AffineTransform, so we
 468  *         could do a quick check to see if the xform has changed since
 469  *         last time... a simple object compare won't suffice...
 470  */
 471 void
 472 OGLContext_SetTransform(OGLContext *oglc,
 473                         jdouble m00, jdouble m10,
 474                         jdouble m01, jdouble m11,
 475                         jdouble m02, jdouble m12)
 476 {
 477     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_SetTransform");
 478 
 479     RETURN_IF_NULL(oglc);
 480     CHECK_PREVIOUS_OP(OGL_STATE_CHANGE);
 481 
 482     if (oglc->xformMatrix == NULL) {
 483         size_t arrsize = 16 * sizeof(GLdouble);
 484         oglc->xformMatrix = (GLdouble *)malloc(arrsize);
 485         memset(oglc->xformMatrix, 0, arrsize);
 486         oglc->xformMatrix[10] = 1.0;
 487         oglc->xformMatrix[15] = 1.0;
 488     }
 489 
 490     // copy values from AffineTransform object into native matrix array
 491     oglc->xformMatrix[0] = m00;
 492     oglc->xformMatrix[1] = m10;
 493     oglc->xformMatrix[4] = m01;
 494     oglc->xformMatrix[5] = m11;
 495     oglc->xformMatrix[12] = m02;
 496     oglc->xformMatrix[13] = m12;
 497 
 498     J2dTraceLn3(J2D_TRACE_VERBOSE, "  [%lf %lf %lf]",
 499                 oglc->xformMatrix[0], oglc->xformMatrix[4],
 500                 oglc->xformMatrix[12]);
 501     J2dTraceLn3(J2D_TRACE_VERBOSE, "  [%lf %lf %lf]",
 502                 oglc->xformMatrix[1], oglc->xformMatrix[5],
 503                 oglc->xformMatrix[13]);
 504 
 505     j2d_glMatrixMode(GL_MODELVIEW);
 506     j2d_glLoadMatrixd(oglc->xformMatrix);
 507 }
 508 
 509 /**
 510  * Creates a 2D texture of the given format and dimensions and returns the
 511  * texture object identifier.  This method is typically used to create a
 512  * temporary texture for intermediate work, such as in the
 513  * OGLContext_InitBlitTileTexture() method below.
 514  */
 515 GLuint
 516 OGLContext_CreateBlitTexture(GLenum internalFormat, GLenum pixelFormat,
 517                              GLuint width, GLuint height)
 518 {
 519     GLuint texID;
 520     GLint sp, sr, rl, align;
 521     GLclampf priority = 1.0f;
 522 
 523     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_CreateBlitTexture");
 524 
 525     j2d_glGenTextures(1, &texID);
 526     j2d_glBindTexture(GL_TEXTURE_2D, texID);
 527     j2d_glPrioritizeTextures(1, &texID, &priority);
 528     j2d_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 529     j2d_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 530     OGLSD_RESET_TEXTURE_WRAP(GL_TEXTURE_2D);
 531 
 532     // save pixel store parameters (since this method could be invoked after
 533     // the caller has already set up its pixel store parameters)
 534     j2d_glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &sp);
 535     j2d_glGetIntegerv(GL_UNPACK_SKIP_ROWS, &sr);
 536     j2d_glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rl);
 537     j2d_glGetIntegerv(GL_UNPACK_ALIGNMENT, &align);
 538 
 539     // set pixel store parameters to default values
 540     j2d_glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
 541     j2d_glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
 542     j2d_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
 543     j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 544 
 545     j2d_glTexImage2D(GL_TEXTURE_2D, 0, internalFormat,
 546                      width, height, 0,
 547                      pixelFormat, GL_UNSIGNED_BYTE, NULL);
 548 
 549     // restore pixel store parameters
 550     j2d_glPixelStorei(GL_UNPACK_SKIP_PIXELS, sp);
 551     j2d_glPixelStorei(GL_UNPACK_SKIP_ROWS, sr);
 552     j2d_glPixelStorei(GL_UNPACK_ROW_LENGTH, rl);
 553     j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, align);
 554 
 555     return texID;
 556 }
 557 
 558 /**
 559  * Initializes a small texture tile for use with tiled blit operations (see
 560  * OGLBlitLoops.c and OGLMaskBlit.c for usage examples).  The texture ID for
 561  * the tile is stored in the given OGLContext.  The tile is initially filled
 562  * with garbage values, but the tile is updated as needed (via
 563  * glTexSubImage2D()) with real RGBA values used in tiled blit situations.
 564  * The internal format for the texture is GL_RGBA8, which should be sufficient
 565  * for storing system memory surfaces of any known format (see PixelFormats
 566  * for a list of compatible surface formats).
 567  */
 568 jboolean
 569 OGLContext_InitBlitTileTexture(OGLContext *oglc)
 570 {
 571     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_InitBlitTileTexture");
 572 
 573     oglc->blitTextureID =
 574         OGLContext_CreateBlitTexture(GL_RGBA8, GL_RGBA,
 575                                      OGLC_BLIT_TILE_SIZE,
 576                                      OGLC_BLIT_TILE_SIZE);
 577 
 578     return JNI_TRUE;
 579 }
 580 
 581 /**
 582  * Destroys the OpenGL resources associated with the given OGLContext.
 583  * It is required that the native context associated with the OGLContext
 584  * be made current prior to calling this method.
 585  */
 586 void
 587 OGLContext_DestroyContextResources(OGLContext *oglc)
 588 {
 589     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_DestroyContextResources");
 590 
 591     if (oglc->xformMatrix != NULL) {
 592         free(oglc->xformMatrix);
 593     }
 594 
 595     if (oglc->blitTextureID != 0) {
 596         j2d_glDeleteTextures(1, &oglc->blitTextureID);
 597     }
 598 }
 599 
 600 /**
 601  * Returns JNI_TRUE if the given extension name is available for the current
 602  * GraphicsConfig; JNI_FALSE otherwise.  An extension is considered available
 603  * if its identifier string is found amongst the space-delimited GL_EXTENSIONS
 604  * string.
 605  *
 606  * Adapted from the OpenGL Red Book, pg. 506.
 607  */
 608 jboolean
 609 OGLContext_IsExtensionAvailable(const char *extString, char *extName)
 610 {
 611     jboolean ret = JNI_FALSE;
 612     char *p = (char *)extString;
 613     char *end;
 614 
 615     if (extString == NULL) {
 616         J2dTraceLn(J2D_TRACE_INFO, "OGLContext_IsExtensionAvailable");
 617         J2dRlsTraceLn(J2D_TRACE_ERROR,
 618             "OGLContext_IsExtensionAvailable: extension string is null");
 619         return JNI_FALSE;
 620     }
 621 
 622     end = p + strlen(p);
 623 
 624     while (p < end) {
 625         size_t n = strcspn(p, " ");
 626 
 627         if ((strlen(extName) == n) && (strncmp(extName, p, n) == 0)) {
 628             ret = JNI_TRUE;
 629             break;
 630         }
 631 
 632         p += (n + 1);
 633     }
 634 
 635     J2dRlsTraceLn2(J2D_TRACE_INFO,
 636                    "OGLContext_IsExtensionAvailable: %s=%s",
 637                    extName, ret ? "true" : "false");
 638 
 639     return ret;
 640 }
 641 
 642 /**
 643  * Returns JNI_TRUE only if all of the following conditions are met:
 644  *   - the GL_EXT_framebuffer_object extension is available
 645  *   - FBO support has been enabled via the system property
 646  *   - we can successfully create an FBO with depth capabilities
 647  */
 648 static jboolean
 649 OGLContext_IsFBObjectExtensionAvailable(JNIEnv *env,
 650                                         const char *extString)
 651 {
 652     jboolean isFBObjectEnabled = JNI_FALSE;
 653     GLuint fbobjectID, textureID, depthID;
 654     jint width = 1, height = 1;
 655 
 656     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_IsFBObjectExtensionAvailable");
 657 
 658     // first see if the fbobject extension is available
 659     if (!OGLContext_IsExtensionAvailable(extString,
 660                                          "GL_EXT_framebuffer_object"))
 661     {
 662         return JNI_FALSE;
 663     }
 664 
 665     // next see if the fbobject system property has been enabled
 666     isFBObjectEnabled =
 667         JNU_GetStaticFieldByName(env, NULL,
 668                                  "sun/java2d/opengl/OGLSurfaceData",
 669                                  "isFBObjectEnabled", "Z").z;
 670     if (!isFBObjectEnabled) {
 671         J2dRlsTraceLn(J2D_TRACE_INFO,
 672             "OGLContext_IsFBObjectExtensionAvailable: disabled via flag");
 673         return JNI_FALSE;
 674     }
 675 
 676     // finally, create a dummy fbobject with depth capabilities to see
 677     // if this configuration is supported by the drivers/hardware
 678     // (first we initialize a color texture object that will be used to
 679     // construct the dummy fbobject)
 680     j2d_glGenTextures(1, &textureID);
 681     j2d_glBindTexture(GL_TEXTURE_2D, textureID);
 682     j2d_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
 683                      width, height, 0,
 684                      GL_RGB, GL_UNSIGNED_BYTE, NULL);
 685     j2d_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 686     j2d_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 687 
 688     // initialize framebuffer object using color texture created above
 689     if (!OGLSD_InitFBObject(&fbobjectID, &depthID,
 690                             textureID, GL_TEXTURE_2D,
 691                             width, height))
 692     {
 693         J2dRlsTraceLn(J2D_TRACE_INFO,
 694             "OGLContext_IsFBObjectExtensionAvailable: fbobject unsupported");
 695         j2d_glDeleteTextures(1, &textureID);
 696         return JNI_FALSE;
 697     }
 698 
 699     // delete the temporary resources
 700     j2d_glDeleteTextures(1, &textureID);
 701     j2d_glDeleteRenderbuffersEXT(1, &depthID);
 702     j2d_glDeleteFramebuffersEXT(1, &fbobjectID);
 703 
 704     J2dRlsTraceLn(J2D_TRACE_INFO,
 705         "OGLContext_IsFBObjectExtensionAvailable: fbobject supported");
 706 
 707     return JNI_TRUE;
 708 }
 709 
 710 /**
 711  * Returns JNI_TRUE only if all of the following conditions are met:
 712  *   - the GL_ARB_fragment_shader extension is available
 713  *   - the LCD text shader codepath has been enabled via the system property
 714  *   - the hardware supports the minimum number of texture units
 715  */
 716 static jboolean
 717 OGLContext_IsLCDShaderSupportAvailable(JNIEnv *env,
 718                                        jboolean fragShaderAvailable)
 719 {
 720     jboolean isLCDShaderEnabled = JNI_FALSE;
 721     GLint maxTexUnits;
 722 
 723     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_IsLCDShaderSupportAvailable");
 724 
 725     // first see if the fragment shader extension is available
 726     if (!fragShaderAvailable) {
 727         return JNI_FALSE;
 728     }
 729 
 730     // next see if the lcdshader system property has been enabled
 731     isLCDShaderEnabled =
 732         JNU_GetStaticFieldByName(env, NULL,
 733                                  "sun/java2d/opengl/OGLSurfaceData",
 734                                  "isLCDShaderEnabled", "Z").z;
 735     if (!isLCDShaderEnabled) {
 736         J2dRlsTraceLn(J2D_TRACE_INFO,
 737             "OGLContext_IsLCDShaderSupportAvailable: disabled via flag");
 738         return JNI_FALSE;
 739     }
 740 
 741     // finally, check to see if the hardware supports the required number
 742     // of texture units
 743     j2d_glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &maxTexUnits);
 744     if (maxTexUnits < 4) {
 745         J2dRlsTraceLn1(J2D_TRACE_INFO,
 746           "OGLContext_IsLCDShaderSupportAvailable: not enough tex units (%d)",
 747           maxTexUnits);
 748     }
 749 
 750     J2dRlsTraceLn(J2D_TRACE_INFO,
 751         "OGLContext_IsLCDShaderSupportAvailable: LCD text shader supported");
 752 
 753     return JNI_TRUE;
 754 }
 755 
 756 /**
 757  * Returns JNI_TRUE only if all of the following conditions are met:
 758  *   - the GL_ARB_fragment_shader extension is available
 759  *   - the BufferedImageOp shader codepath has been enabled via the
 760  *     system property
 761  */
 762 static jboolean
 763 OGLContext_IsBIOpShaderSupportAvailable(JNIEnv *env,
 764                                         jboolean fragShaderAvailable)
 765 {
 766     jboolean isBIOpShaderEnabled = JNI_FALSE;
 767 
 768     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_IsBIOpShaderSupportAvailable");
 769 
 770     // first see if the fragment shader extension is available
 771     if (!fragShaderAvailable) {
 772         return JNI_FALSE;
 773     }
 774 
 775     // next see if the biopshader system property has been enabled
 776     isBIOpShaderEnabled =
 777         JNU_GetStaticFieldByName(env, NULL,
 778                                  "sun/java2d/opengl/OGLSurfaceData",
 779                                  "isBIOpShaderEnabled", "Z").z;
 780     if (!isBIOpShaderEnabled) {
 781         J2dRlsTraceLn(J2D_TRACE_INFO,
 782             "OGLContext_IsBIOpShaderSupportAvailable: disabled via flag");
 783         return JNI_FALSE;
 784     }
 785 
 786     /*
 787      * Note: In theory we should probably do some other checks here, like
 788      * linking a sample shader to see if the hardware truly supports our
 789      * shader programs.  However, our current BufferedImageOp shaders were
 790      * designed to support first-generation shader-level hardware, so the
 791      * assumption is that if our shaders work on those GPUs, then they'll
 792      * work on newer ones as well.  Also, linking a fragment program can
 793      * cost valuable CPU cycles, which is another reason to avoid these
 794      * checks at startup.
 795      */
 796 
 797     J2dRlsTraceLn(J2D_TRACE_INFO,
 798         "OGLContext_IsBIOpShaderSupportAvailable: BufferedImageOp shader supported");
 799 
 800     return JNI_TRUE;
 801 }
 802 
 803 /**
 804  * Returns JNI_TRUE only if all of the following conditions are met:
 805  *   - the GL_ARB_fragment_shader extension is available
 806  *   - the Linear/RadialGradientPaint shader codepath has been enabled via the
 807  *     system property
 808  */
 809 static jboolean
 810 OGLContext_IsGradShaderSupportAvailable(JNIEnv *env,
 811                                         jboolean fragShaderAvailable)
 812 {
 813     jboolean isGradShaderEnabled = JNI_FALSE;
 814 
 815     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_IsGradShaderSupportAvailable");
 816 
 817     // first see if the fragment shader extension is available
 818     if (!fragShaderAvailable) {
 819         return JNI_FALSE;
 820     }
 821 
 822     // next see if the gradshader system property has been enabled
 823     isGradShaderEnabled =
 824         JNU_GetStaticFieldByName(env, NULL,
 825                                  "sun/java2d/opengl/OGLSurfaceData",
 826                                  "isGradShaderEnabled", "Z").z;
 827     if (!isGradShaderEnabled) {
 828         J2dRlsTraceLn(J2D_TRACE_INFO,
 829             "OGLContext_IsGradShaderSupportAvailable: disabled via flag");
 830         return JNI_FALSE;
 831     }
 832 
 833     J2dRlsTraceLn(J2D_TRACE_INFO,
 834         "OGLContext_IsGradShaderSupportAvailable: Linear/RadialGradientPaint shader supported");
 835 
 836     return JNI_TRUE;
 837 }
 838 
 839 /**
 840  * Checks for the presence of the optional extensions used by
 841  * the Java 2D OpenGL pipeline.  The given caps bitfield is updated
 842  * to reflect the availability of these extensions.
 843  */
 844 void
 845 OGLContext_GetExtensionInfo(JNIEnv *env, jint *caps)
 846 {
 847     jint vcap = OGLC_VENDOR_OTHER;
 848     const char *vendor = (char *)j2d_glGetString(GL_VENDOR);
 849     const char *e = (char *)j2d_glGetString(GL_EXTENSIONS);
 850     jboolean fragShaderAvail =
 851         OGLContext_IsExtensionAvailable(e, "GL_ARB_fragment_shader");
 852 
 853     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_GetExtensionInfo");
 854 
 855     *caps |= CAPS_TEXNONSQUARE;
 856     if (OGLContext_IsExtensionAvailable(e, "GL_ARB_multitexture")) {
 857         *caps |= CAPS_MULTITEXTURE;
 858     }
 859     if (OGLContext_IsExtensionAvailable(e, "GL_ARB_texture_non_power_of_two")){
 860         *caps |= CAPS_TEXNONPOW2;
 861     }
 862     // 6656574: Use of the GL_ARB_texture_rectangle extension by Java 2D
 863     // complicates any third-party libraries that try to interact with
 864     // the OGL pipeline (and we've run into driver bugs in the past related
 865     // to this extension), so for now we will disable its use by default (unless
 866     // forced). We will still make use of the GL_ARB_texture_non_power_of_two
 867     // extension when available, which is the better choice going forward
 868     // anyway.
 869     if (OGLContext_IsExtensionAvailable(e, "GL_ARB_texture_rectangle") &&
 870         getenv("J2D_OGL_TEXRECT") != NULL)
 871     {
 872         *caps |= CAPS_EXT_TEXRECT;
 873     }
 874     if (OGLContext_IsFBObjectExtensionAvailable(env, e)) {
 875         *caps |= CAPS_EXT_FBOBJECT;
 876     }
 877     if (OGLContext_IsLCDShaderSupportAvailable(env, fragShaderAvail)) {
 878         *caps |= CAPS_EXT_LCD_SHADER | CAPS_PS20;
 879     }
 880     if (OGLContext_IsBIOpShaderSupportAvailable(env, fragShaderAvail)) {
 881         *caps |= CAPS_EXT_BIOP_SHADER | CAPS_PS20;
 882     }
 883     if (OGLContext_IsGradShaderSupportAvailable(env, fragShaderAvail)) {
 884         *caps |= CAPS_EXT_GRAD_SHADER | CAPS_PS20;
 885     }
 886     if (OGLContext_IsExtensionAvailable(e, "GL_NV_fragment_program")) {
 887         // this is an Nvidia board, at least PS 2.0, but we can't
 888         // use the "max instructions" heuristic since GeForce FX
 889         // boards report 1024 even though they're only PS 2.0,
 890         // so we'll check the following, which does imply PS 3.0
 891         if (OGLContext_IsExtensionAvailable(e, "GL_NV_fragment_program2")) {
 892             *caps |= CAPS_PS30;
 893         }
 894     } else {
 895         // for all other boards, we look at the "max instructions"
 896         // count reported by the GL_ARB_fragment_program extension
 897         // as a heuristic for detecting PS 3.0 compatible hardware
 898         if (OGLContext_IsExtensionAvailable(e, "GL_ARB_fragment_program")) {
 899             GLint instr;
 900             j2d_glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB,
 901                                   GL_MAX_PROGRAM_INSTRUCTIONS_ARB, &instr);
 902             if (instr > 512) {
 903                 *caps |= CAPS_PS30;
 904             }
 905         }
 906     }
 907     // stuff vendor descriptor in the upper bits of the caps
 908     if (vendor != NULL) {
 909         if (strncmp(vendor, "ATI", 3) == 0) {
 910             vcap = OGLC_VENDOR_ATI;
 911         } else if (strncmp(vendor, "NVIDIA", 6) == 0) {
 912             vcap = OGLC_VENDOR_NVIDIA;
 913         } else if (strncmp(vendor, "Sun", 3) == 0) {
 914             vcap = OGLC_VENDOR_SUN;
 915         }
 916         // REMIND: new in 7 - check if needs fixing
 917         *caps |= ((vcap & OGLC_VCAP_MASK) << OGLC_VCAP_OFFSET);
 918     }
 919 
 920 }
 921 
 922 /**
 923  * Returns JNI_TRUE if the given GL_VERSION string meets the minimum
 924  * requirements (>= 1.2); JNI_FALSE otherwise.
 925  */
 926 jboolean
 927 OGLContext_IsVersionSupported(const unsigned char *versionstr)
 928 {
 929     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_IsVersionSupported");
 930 
 931     if (versionstr == NULL) {
 932         J2dRlsTraceLn(J2D_TRACE_ERROR,
 933             "OGLContext_IsVersionSupported: version string is null");
 934         return JNI_FALSE;
 935     }
 936 
 937     // note that this check allows for OpenGL 2.x
 938     return ((versionstr[0] == '1' && versionstr[2] >= '2') ||
 939             (versionstr[0] >= '2'));
 940 }
 941 
 942 /**
 943  * Compiles and links the given fragment shader program.  If
 944  * successful, this function returns a handle to the newly created shader
 945  * program; otherwise returns 0.
 946  */
 947 GLhandleARB
 948 OGLContext_CreateFragmentProgram(const char *fragmentShaderSource)
 949 {
 950     GLhandleARB fragmentShader, fragmentProgram;
 951     GLint success;
 952     int infoLogLength = 0;
 953 
 954     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_CreateFragmentProgram");
 955 
 956     // create the shader object and compile the shader source code
 957     fragmentShader = j2d_glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
 958     j2d_glShaderSourceARB(fragmentShader, 1, &fragmentShaderSource, NULL);
 959     j2d_glCompileShaderARB(fragmentShader);
 960     j2d_glGetObjectParameterivARB(fragmentShader,
 961                                   GL_OBJECT_COMPILE_STATUS_ARB,
 962                                   &success);
 963 
 964     // print the compiler messages, if necessary
 965     j2d_glGetObjectParameterivARB(fragmentShader,
 966                                   GL_OBJECT_INFO_LOG_LENGTH_ARB,
 967                                   &infoLogLength);
 968     if (infoLogLength > 1) {
 969         char infoLog[1024];
 970         j2d_glGetInfoLogARB(fragmentShader, 1024, NULL, infoLog);
 971         J2dRlsTraceLn2(J2D_TRACE_WARNING,
 972             "OGLContext_CreateFragmentProgram: compiler msg (%d):\n%s",
 973                        infoLogLength, infoLog);
 974     }
 975 
 976     if (!success) {
 977         J2dRlsTraceLn(J2D_TRACE_ERROR,
 978             "OGLContext_CreateFragmentProgram: error compiling shader");
 979         j2d_glDeleteObjectARB(fragmentShader);
 980         return 0;
 981     }
 982 
 983     // create the program object and attach it to the shader
 984     fragmentProgram = j2d_glCreateProgramObjectARB();
 985     j2d_glAttachObjectARB(fragmentProgram, fragmentShader);
 986 
 987     // it is now safe to delete the shader object
 988     j2d_glDeleteObjectARB(fragmentShader);
 989 
 990     // link the program
 991     j2d_glLinkProgramARB(fragmentProgram);
 992     j2d_glGetObjectParameterivARB(fragmentProgram,
 993                                   GL_OBJECT_LINK_STATUS_ARB,
 994                                   &success);
 995 
 996     // print the linker messages, if necessary
 997     j2d_glGetObjectParameterivARB(fragmentProgram,
 998                                   GL_OBJECT_INFO_LOG_LENGTH_ARB,
 999                                   &infoLogLength);
1000     if (infoLogLength > 1) {
1001         char infoLog[1024];
1002         j2d_glGetInfoLogARB(fragmentProgram, 1024, NULL, infoLog);
1003         J2dRlsTraceLn2(J2D_TRACE_WARNING,
1004             "OGLContext_CreateFragmentProgram: linker msg (%d):\n%s",
1005                        infoLogLength, infoLog);
1006     }
1007 
1008     if (!success) {
1009         J2dRlsTraceLn(J2D_TRACE_ERROR,
1010             "OGLContext_CreateFragmentProgram: error linking shader");
1011         j2d_glDeleteObjectARB(fragmentProgram);
1012         return 0;
1013     }
1014 
1015     return fragmentProgram;
1016 }
1017 
1018 /*
1019  * Class:     sun_java2d_opengl_OGLContext
1020  * Method:    getOGLIdString
1021  * Signature: ()Ljava/lang/String;
1022  */
1023 JNIEXPORT jstring JNICALL Java_sun_java2d_opengl_OGLContext_getOGLIdString
1024   (JNIEnv *env, jclass oglcc)
1025 {
1026     char *vendor, *renderer, *version;
1027     char *pAdapterId;
1028     jobject ret = NULL;
1029     int len;
1030 
1031     J2dTraceLn(J2D_TRACE_INFO, "OGLContext_getOGLIdString");
1032 
1033     vendor = (char*)j2d_glGetString(GL_VENDOR);
1034     if (vendor == NULL) {
1035         vendor = "Unknown Vendor";
1036     }
1037     renderer = (char*)j2d_glGetString(GL_RENDERER);
1038     if (renderer == NULL) {
1039         renderer = "Unknown Renderer";
1040     }
1041     version = (char*)j2d_glGetString(GL_VERSION);
1042     if (version == NULL) {
1043         version = "unknown version";
1044     }
1045 
1046     // 'vendor renderer (version)0'
1047     len = strlen(vendor) + 1 + strlen(renderer) + 1 + 1+strlen(version)+1 + 1;
1048     pAdapterId = malloc(len);
1049     if (pAdapterId != NULL) {
1050 
1051         jio_snprintf(pAdapterId, len, "%s %s (%s)", vendor, renderer, version);
1052 
1053         J2dTraceLn1(J2D_TRACE_VERBOSE, "  id=%s", pAdapterId);
1054 
1055         ret = JNU_NewStringPlatform(env, pAdapterId);
1056 
1057         free(pAdapterId);
1058     }
1059 
1060     return ret;
1061 }
1062 
1063 #endif /* !HEADLESS */