1 /*
   2  * Copyright (c) 2003, 2006, 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 <jni.h>
  29 #include <jlong.h>
  30 
  31 #include "SurfaceData.h"
  32 #include "OGLBlitLoops.h"
  33 #include "OGLRenderQueue.h"
  34 #include "OGLSurfaceData.h"
  35 #include "GraphicsPrimitiveMgr.h"
  36 
  37 extern OGLPixelFormat PixelFormats[];
  38 
  39 /**
  40  * Inner loop used for copying a source OpenGL "Surface" (window, pbuffer,
  41  * etc.) to a destination OpenGL "Surface".  Note that the same surface can
  42  * be used as both the source and destination, as is the case in a copyArea()
  43  * operation.  This method is invoked from OGLBlitLoops_IsoBlit() as well as
  44  * OGLBlitLoops_CopyArea().
  45  *
  46  * The standard glCopyPixels() mechanism is used to copy the source region
  47  * into the destination region.  If the regions have different dimensions,
  48  * the source will be scaled into the destination as appropriate (only
  49  * nearest neighbor filtering will be applied for simple scale operations).
  50  */
  51 static void
  52 OGLBlitSurfaceToSurface(OGLContext *oglc, OGLSDOps *srcOps, OGLSDOps *dstOps,
  53                         jint sx1, jint sy1, jint sx2, jint sy2,
  54                         jdouble dx1, jdouble dy1, jdouble dx2, jdouble dy2)
  55 {
  56     GLfloat scalex, scaley;
  57     jint srcw = sx2 - sx1;
  58     jint srch = sy2 - sy1;
  59 
  60     scalex = ((GLfloat)(dx2-dx1)) / srcw;
  61     scaley = ((GLfloat)(dy2-dy1)) / srch;
  62 
  63     // the following lines account for the fact that glCopyPixels() copies a
  64     // region whose lower-left corner is at (x,y), but the source parameters
  65     // (sx1,sy1) we are given here point to the upper-left corner of the
  66     // source region... so here we play with the sy1 and dy1 parameters so
  67     // that they point to the lower-left corners of the regions...
  68     sx1 = srcOps->xOffset + sx1;
  69     sy1 = srcOps->yOffset + srcOps->height - sy2;
  70     dy1 = dy2;
  71 
  72     if (oglc->extraAlpha != 1.0f) {
  73         OGLContext_SetExtraAlpha(oglc->extraAlpha);
  74     }
  75 
  76     // see OGLBlitSwToSurface() for more info on the following two lines
  77     j2d_glRasterPos2i(0, 0);
  78     j2d_glBitmap(0, 0, 0, 0, (GLfloat)dx1, (GLfloat)-dy1, NULL);
  79 
  80     if (scalex == 1.0f && scaley == 1.0f) {
  81         j2d_glCopyPixels(sx1, sy1, srcw, srch, GL_COLOR);
  82     } else {
  83         j2d_glPixelZoom(scalex, scaley);
  84         j2d_glCopyPixels(sx1, sy1, srcw, srch, GL_COLOR);
  85         j2d_glPixelZoom(1.0f, 1.0f);
  86     }
  87 
  88     if (oglc->extraAlpha != 1.0f) {
  89         OGLContext_SetExtraAlpha(1.0f);
  90     }
  91 }
  92 
  93 /**
  94  * Inner loop used for copying a source OpenGL "Texture" to a destination
  95  * OpenGL "Surface".  This method is invoked from OGLBlitLoops_IsoBlit().
  96  *
  97  * This method will copy, scale, or transform the source texture into the
  98  * destination depending on the transform state, as established in
  99  * and OGLContext_SetTransform().  If the source texture is
 100  * transformed in any way when rendered into the destination, the filtering
 101  * method applied is determined by the hint parameter (can be GL_NEAREST or
 102  * GL_LINEAR).
 103  */
 104 static void
 105 OGLBlitTextureToSurface(OGLContext *oglc,
 106                         OGLSDOps *srcOps, OGLSDOps *dstOps,
 107                         jboolean rtt, jint hint,
 108                         jint sx1, jint sy1, jint sx2, jint sy2,
 109                         jdouble dx1, jdouble dy1, jdouble dx2, jdouble dy2)
 110 {
 111     GLdouble tx1, ty1, tx2, ty2;
 112 
 113     if (rtt) {
 114         /*
 115          * The source is a render-to-texture surface.  These surfaces differ
 116          * from regular texture objects in that the bottom scanline (of
 117          * the actual image content) coincides with the top edge of the
 118          * texture object.  Therefore, we need to adjust the sy1/sy2
 119          * coordinates relative to the top scanline of the image content.
 120          *
 121          * In texture coordinates, the top-left corner of the image content
 122          * would be at:
 123          *     (0.0, (imgHeight/texHeight))
 124          * while the bottom-right corner corresponds to:
 125          *     ((imgWidth/texWidth), 0.0)
 126          */
 127         sy1 = srcOps->height - sy1;
 128         sy2 = srcOps->height - sy2;
 129     }
 130 
 131     if (srcOps->textureTarget == GL_TEXTURE_RECTANGLE_ARB) {
 132         // The GL_ARB_texture_rectangle extension requires that we specify
 133         // texture coordinates in the range [0,srcw] and [0,srch] instead of
 134         // [0,1] as we would normally do in the case of GL_TEXTURE_2D
 135         tx1 = (GLdouble)sx1;
 136         ty1 = (GLdouble)sy1;
 137         tx2 = (GLdouble)sx2;
 138         ty2 = (GLdouble)sy2;
 139     } else {
 140         // Otherwise we need to convert the source bounds into the range [0,1]
 141         tx1 = ((GLdouble)sx1) / srcOps->textureWidth;
 142         ty1 = ((GLdouble)sy1) / srcOps->textureHeight;
 143         tx2 = ((GLdouble)sx2) / srcOps->textureWidth;
 144         ty2 = ((GLdouble)sy2) / srcOps->textureHeight;
 145     }
 146 
 147     // Note that we call CHECK_PREVIOUS_OP(texTarget) in IsoBlit(), which
 148     // will call glEnable(texTarget) as necessary.
 149     j2d_glBindTexture(srcOps->textureTarget, srcOps->textureID);
 150     OGLC_UPDATE_TEXTURE_FUNCTION(oglc, GL_MODULATE);
 151     OGLSD_UPDATE_TEXTURE_FILTER(srcOps, hint);
 152 
 153     j2d_glBegin(GL_QUADS);
 154     j2d_glTexCoord2d(tx1, ty1); j2d_glVertex2d(dx1, dy1);
 155     j2d_glTexCoord2d(tx2, ty1); j2d_glVertex2d(dx2, dy1);
 156     j2d_glTexCoord2d(tx2, ty2); j2d_glVertex2d(dx2, dy2);
 157     j2d_glTexCoord2d(tx1, ty2); j2d_glVertex2d(dx1, dy2);
 158     j2d_glEnd();
 159 }
 160 
 161 /**
 162  * Inner loop used for copying a source system memory ("Sw") surface to a
 163  * destination OpenGL "Surface".  This method is invoked from
 164  * OGLBlitLoops_Blit().
 165  *
 166  * The standard glDrawPixels() mechanism is used to copy the source region
 167  * into the destination region.  If the regions have different
 168  * dimensions, the source will be scaled into the destination
 169  * as appropriate (only nearest neighbor filtering will be applied for simple
 170  * scale operations).
 171  */
 172 static void
 173 OGLBlitSwToSurface(OGLContext *oglc, SurfaceDataRasInfo *srcInfo,
 174                    OGLPixelFormat *pf,
 175                    jint sx1, jint sy1, jint sx2, jint sy2,
 176                    jdouble dx1, jdouble dy1, jdouble dx2, jdouble dy2)
 177 {
 178     GLfloat scalex, scaley;
 179 
 180     scalex = ((GLfloat)(dx2-dx1)) / (sx2-sx1);
 181     scaley = ((GLfloat)(dy2-dy1)) / (sy2-sy1);
 182 
 183     if (oglc->extraAlpha != 1.0f) {
 184         OGLContext_SetExtraAlpha(oglc->extraAlpha);
 185     }
 186     if (!pf->hasAlpha) {
 187         // if the source surface does not have an alpha channel,
 188         // we need to ensure that the alpha values are forced to
 189         // the current extra alpha value (see OGLContext_SetExtraAlpha()
 190         // for more information)
 191         j2d_glPixelTransferf(GL_ALPHA_SCALE, 0.0f);
 192         j2d_glPixelTransferf(GL_ALPHA_BIAS, oglc->extraAlpha);
 193     }
 194 
 195     // This is a rather intriguing (yet totally valid) hack... If we were to
 196     // specify a raster position that is outside the surface bounds, the raster
 197     // position would be invalid and nothing would be rendered.  However, we
 198     // can use a widely known trick to move the raster position outside the
 199     // surface bounds while maintaining its status as valid.  The following
 200     // call to glBitmap() renders a no-op bitmap, but offsets the current
 201     // raster position from (0,0) to the desired location of (dx1,-dy1)...
 202     j2d_glRasterPos2i(0, 0);
 203     j2d_glBitmap(0, 0, 0, 0, (GLfloat)dx1, (GLfloat)-dy1, NULL);
 204 
 205     j2d_glPixelZoom(scalex, -scaley);
 206 
 207     // in case pixel stride is not a multiple of scanline stride the copy
 208     // has to be done line by line (see 6207877)
 209     if (srcInfo->scanStride % srcInfo->pixelStride != 0) {
 210         jint width = sx2-sx1;
 211         jint height = sy2-sy1;
 212         GLvoid *pSrc = srcInfo->rasBase;
 213 
 214         while (height > 0) {
 215             j2d_glDrawPixels(width, 1, pf->format, pf->type, pSrc);
 216             j2d_glBitmap(0, 0, 0, 0, (GLfloat)0, (GLfloat)-1, NULL);
 217             pSrc = PtrAddBytes(pSrc, srcInfo->scanStride);
 218             height--;
 219         }
 220     } else {
 221         j2d_glDrawPixels(sx2-sx1, sy2-sy1, pf->format, pf->type, srcInfo->rasBase);
 222     }
 223 
 224     j2d_glPixelZoom(1.0, 1.0);
 225 
 226     if (oglc->extraAlpha != 1.0f) {
 227         OGLContext_SetExtraAlpha(1.0f);
 228     }
 229     if (!pf->hasAlpha) {
 230         // restore scale/bias to their original values
 231         j2d_glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
 232         j2d_glPixelTransferf(GL_ALPHA_BIAS, 0.0f);
 233     }
 234 }
 235 
 236 /**
 237  * Inner loop used for copying a source system memory ("Sw") surface or
 238  * OpenGL "Surface" to a destination OpenGL "Surface", using an OpenGL texture
 239  * tile as an intermediate surface.  This method is invoked from
 240  * OGLBlitLoops_Blit() for "Sw" surfaces and OGLBlitLoops_IsoBlit() for
 241  * "Surface" surfaces.
 242  *
 243  * This method is used to transform the source surface into the destination.
 244  * Pixel rectangles cannot be arbitrarily transformed (without the
 245  * GL_EXT_pixel_transform extension, which is not supported on most modern
 246  * hardware).  However, texture mapped quads do respect the GL_MODELVIEW
 247  * transform matrix, so we use textures here to perform the transform
 248  * operation.  This method uses a tile-based approach in which a small
 249  * subregion of the source surface is copied into a cached texture tile.  The
 250  * texture tile is then mapped into the appropriate location in the
 251  * destination surface.
 252  *
 253  * REMIND: this only works well using GL_NEAREST for the filtering mode
 254  *         (GL_LINEAR causes visible stitching problems between tiles,
 255  *         but this can be fixed by making use of texture borders)
 256  */
 257 static void
 258 OGLBlitToSurfaceViaTexture(OGLContext *oglc, SurfaceDataRasInfo *srcInfo,
 259                            OGLPixelFormat *pf, OGLSDOps *srcOps,
 260                            jboolean swsurface, jint hint,
 261                            jint sx1, jint sy1, jint sx2, jint sy2,
 262                            jdouble dx1, jdouble dy1, jdouble dx2, jdouble dy2)
 263 {
 264     GLdouble tx1, ty1, tx2, ty2;
 265     GLdouble dx, dy, dw, dh, cdw, cdh;
 266     jint tw, th;
 267     jint sx, sy, sw, sh;
 268     GLint glhint = (hint == OGLSD_XFORM_BILINEAR) ? GL_LINEAR : GL_NEAREST;
 269     jboolean adjustAlpha = (pf != NULL && !pf->hasAlpha);
 270     jboolean slowPath;
 271 
 272     if (oglc->blitTextureID == 0) {
 273         if (!OGLContext_InitBlitTileTexture(oglc)) {
 274             J2dRlsTraceLn(J2D_TRACE_ERROR,
 275                 "OGLBlitToSurfaceViaTexture: could not init blit tile");
 276             return;
 277         }
 278     }
 279 
 280     tx1 = 0.0f;
 281     ty1 = 0.0f;
 282     tw = OGLC_BLIT_TILE_SIZE;
 283     th = OGLC_BLIT_TILE_SIZE;
 284     cdw = (dx2-dx1) / (((GLdouble)(sx2-sx1)) / OGLC_BLIT_TILE_SIZE);
 285     cdh = (dy2-dy1) / (((GLdouble)(sy2-sy1)) / OGLC_BLIT_TILE_SIZE);
 286 
 287     j2d_glEnable(GL_TEXTURE_2D);
 288     j2d_glBindTexture(GL_TEXTURE_2D, oglc->blitTextureID);
 289     OGLC_UPDATE_TEXTURE_FUNCTION(oglc, GL_MODULATE);
 290     j2d_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glhint);
 291     j2d_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, glhint);
 292 
 293     if (adjustAlpha) {
 294         // if the source surface does not have an alpha channel,
 295         // we need to ensure that the alpha values are forced to 1.0f
 296         j2d_glPixelTransferf(GL_ALPHA_SCALE, 0.0f);
 297         j2d_glPixelTransferf(GL_ALPHA_BIAS, 1.0f);
 298     }
 299 
 300     // in case pixel stride is not a multiple of scanline stride the copy
 301     // has to be done line by line (see 6207877)
 302     slowPath = srcInfo->scanStride % srcInfo->pixelStride != 0;
 303 
 304     for (sy = sy1, dy = dy1; sy < sy2; sy += th, dy += cdh) {
 305         sh = ((sy + th) > sy2) ? (sy2 - sy) : th;
 306         dh = ((dy + cdh) > dy2) ? (dy2 - dy) : cdh;
 307 
 308         for (sx = sx1, dx = dx1; sx < sx2; sx += tw, dx += cdw) {
 309             sw = ((sx + tw) > sx2) ? (sx2 - sx) : tw;
 310             dw = ((dx + cdw) > dx2) ? (dx2 - dx) : cdw;
 311 
 312             tx2 = ((GLdouble)sw) / tw;
 313             ty2 = ((GLdouble)sh) / th;
 314 
 315             if (swsurface) {
 316                 if (slowPath) {
 317                     jint tmph = sh;
 318                     GLvoid *pSrc = PtrCoord(srcInfo->rasBase,
 319                                             sx, srcInfo->pixelStride,
 320                                             sy, srcInfo->scanStride);
 321 
 322                     while (tmph > 0) {
 323                         j2d_glTexSubImage2D(GL_TEXTURE_2D, 0,
 324                                             0, sh - tmph, sw, 1,
 325                                             pf->format, pf->type,
 326                                             pSrc);
 327                         pSrc = PtrAddBytes(pSrc, srcInfo->scanStride);
 328                         tmph--;
 329                     }
 330                 } else {
 331                     j2d_glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
 332                     j2d_glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
 333 
 334                     j2d_glTexSubImage2D(GL_TEXTURE_2D, 0,
 335                                         0, 0, sw, sh,
 336                                         pf->format, pf->type,
 337                                         srcInfo->rasBase);
 338                 }
 339 
 340                 // the texture image is "right side up", so we align the
 341                 // upper-left texture corner with the upper-left quad corner
 342                 j2d_glBegin(GL_QUADS);
 343                 j2d_glTexCoord2d(tx1, ty1); j2d_glVertex2d(dx, dy);
 344                 j2d_glTexCoord2d(tx2, ty1); j2d_glVertex2d(dx + dw, dy);
 345                 j2d_glTexCoord2d(tx2, ty2); j2d_glVertex2d(dx + dw, dy + dh);
 346                 j2d_glTexCoord2d(tx1, ty2); j2d_glVertex2d(dx, dy + dh);
 347                 j2d_glEnd();
 348             } else {
 349                 // this accounts for lower-left origin of the source region
 350                 jint newsx = srcOps->xOffset + sx;
 351                 jint newsy = srcOps->yOffset + srcOps->height - (sy + sh);
 352                 j2d_glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
 353                                         0, 0, newsx, newsy, sw, sh);
 354 
 355                 // the texture image is "upside down" after the last step, so
 356                 // we align the bottom-left texture corner with the upper-left
 357                 // quad corner (and vice versa) to effectively flip the
 358                 // texture image
 359                 j2d_glBegin(GL_QUADS);
 360                 j2d_glTexCoord2d(tx1, ty2); j2d_glVertex2d(dx, dy);
 361                 j2d_glTexCoord2d(tx2, ty2); j2d_glVertex2d(dx + dw, dy);
 362                 j2d_glTexCoord2d(tx2, ty1); j2d_glVertex2d(dx + dw, dy + dh);
 363                 j2d_glTexCoord2d(tx1, ty1); j2d_glVertex2d(dx, dy + dh);
 364                 j2d_glEnd();
 365             }
 366         }
 367     }
 368 
 369     if (adjustAlpha) {
 370         // restore scale/bias to their original values
 371         j2d_glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
 372         j2d_glPixelTransferf(GL_ALPHA_BIAS, 0.0f);
 373     }
 374 
 375     j2d_glDisable(GL_TEXTURE_2D);
 376 }
 377 
 378 /**
 379  * Inner loop used for copying a source system memory ("Sw") surface to a
 380  * destination OpenGL "Texture".  This method is invoked from
 381  * OGLBlitLoops_Blit().
 382  *
 383  * The source surface is effectively loaded into the OpenGL texture object,
 384  * which must have already been initialized by OGLSD_initTexture().  Note
 385  * that this method is only capable of copying the source surface into the
 386  * destination surface (i.e. no scaling or general transform is allowed).
 387  * This restriction should not be an issue as this method is only used
 388  * currently to cache a static system memory image into an OpenGL texture in
 389  * a hidden-acceleration situation.
 390  */
 391 static void
 392 OGLBlitSwToTexture(SurfaceDataRasInfo *srcInfo, OGLPixelFormat *pf,
 393                    OGLSDOps *dstOps,
 394                    jint dx1, jint dy1, jint dx2, jint dy2)
 395 {
 396     j2d_glBindTexture(dstOps->textureTarget, dstOps->textureID);
 397     // in case pixel stride is not a multiple of scanline stride the copy
 398     // has to be done line by line (see 6207877)
 399     if (srcInfo->scanStride % srcInfo->pixelStride != 0) {
 400         jint width = dx2 - dx1;
 401         jint height = dy2 - dy1;
 402         GLvoid *pSrc = srcInfo->rasBase;
 403 
 404         while (height > 0) {
 405             j2d_glTexSubImage2D(dstOps->textureTarget, 0,
 406                                 dx1, dy2 - height, width, 1,
 407                                 pf->format, pf->type, pSrc);
 408             pSrc = PtrAddBytes(pSrc, srcInfo->scanStride);
 409             height--;
 410         }
 411     } else {
 412         j2d_glTexSubImage2D(dstOps->textureTarget, 0,
 413                             dx1, dy1, dx2-dx1, dy2-dy1,
 414                             pf->format, pf->type, srcInfo->rasBase);
 415     }
 416 }
 417 
 418 /**
 419  * General blit method for copying a native OpenGL surface (of type "Surface"
 420  * or "Texture") to another OpenGL "Surface".  If texture is JNI_TRUE, this
 421  * method will invoke the Texture->Surface inner loop; otherwise, one of the
 422  * Surface->Surface inner loops will be invoked, depending on the transform
 423  * state.
 424  *
 425  * REMIND: we can trick these blit methods into doing XOR simply by passing
 426  *         in the (pixel ^ xorpixel) as the pixel value and preceding the
 427  *         blit with a fillrect...
 428  */
 429 void
 430 OGLBlitLoops_IsoBlit(JNIEnv *env,
 431                      OGLContext *oglc, jlong pSrcOps, jlong pDstOps,
 432                      jboolean xform, jint hint,
 433                      jboolean texture, jboolean rtt,
 434                      jint sx1, jint sy1, jint sx2, jint sy2,
 435                      jdouble dx1, jdouble dy1, jdouble dx2, jdouble dy2)
 436 {
 437     OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrcOps);
 438     OGLSDOps *dstOps = (OGLSDOps *)jlong_to_ptr(pDstOps);
 439     SurfaceDataRasInfo srcInfo;
 440     jint sw    = sx2 - sx1;
 441     jint sh    = sy2 - sy1;
 442     jdouble dw = dx2 - dx1;
 443     jdouble dh = dy2 - dy1;
 444 
 445     J2dTraceLn(J2D_TRACE_INFO, "OGLBlitLoops_IsoBlit");
 446 
 447     if (sw <= 0 || sh <= 0 || dw <= 0 || dh <= 0) {
 448         J2dTraceLn(J2D_TRACE_WARNING,
 449                    "OGLBlitLoops_IsoBlit: invalid dimensions");
 450         return;
 451     }
 452 
 453     RETURN_IF_NULL(srcOps);
 454     RETURN_IF_NULL(dstOps);
 455     RETURN_IF_NULL(oglc);
 456 
 457     srcInfo.bounds.x1 = sx1;
 458     srcInfo.bounds.y1 = sy1;
 459     srcInfo.bounds.x2 = sx2;
 460     srcInfo.bounds.y2 = sy2;
 461 
 462     SurfaceData_IntersectBoundsXYXY(&srcInfo.bounds,
 463                                     0, 0, srcOps->width, srcOps->height);
 464 
 465     if (srcInfo.bounds.x2 > srcInfo.bounds.x1 &&
 466         srcInfo.bounds.y2 > srcInfo.bounds.y1)
 467     {
 468         if (srcInfo.bounds.x1 != sx1) {
 469             dx1 += (srcInfo.bounds.x1 - sx1) * (dw / sw);
 470             sx1 = srcInfo.bounds.x1;
 471         }
 472         if (srcInfo.bounds.y1 != sy1) {
 473             dy1 += (srcInfo.bounds.y1 - sy1) * (dh / sh);
 474             sy1 = srcInfo.bounds.y1;
 475         }
 476         if (srcInfo.bounds.x2 != sx2) {
 477             dx2 += (srcInfo.bounds.x2 - sx2) * (dw / sw);
 478             sx2 = srcInfo.bounds.x2;
 479         }
 480         if (srcInfo.bounds.y2 != sy2) {
 481             dy2 += (srcInfo.bounds.y2 - sy2) * (dh / sh);
 482             sy2 = srcInfo.bounds.y2;
 483         }
 484 
 485         J2dTraceLn2(J2D_TRACE_VERBOSE, "  texture=%d hint=%d", texture, hint);
 486         J2dTraceLn4(J2D_TRACE_VERBOSE, "  sx1=%d sy1=%d sx2=%d sy2=%d",
 487                     sx1, sy1, sx2, sy2);
 488         J2dTraceLn4(J2D_TRACE_VERBOSE, "  dx1=%f dy1=%f dx2=%f dy2=%f",
 489                     dx1, dy1, dx2, dy2);
 490 
 491         if (texture) {
 492             GLint glhint = (hint == OGLSD_XFORM_BILINEAR) ? GL_LINEAR :
 493                                                             GL_NEAREST;
 494             CHECK_PREVIOUS_OP(srcOps->textureTarget);
 495             OGLBlitTextureToSurface(oglc, srcOps, dstOps, rtt, glhint,
 496                                     sx1, sy1, sx2, sy2,
 497                                     dx1, dy1, dx2, dy2);
 498         } else {
 499             jboolean viaTexture;
 500             if (xform) {
 501                 // we must use the via-texture codepath when there is a xform
 502                 viaTexture = JNI_TRUE;
 503             } else {
 504                 // look at the vendor to see which codepath is faster
 505                 // (this has been empirically determined; see 5020009)
 506                 switch (OGLC_GET_VENDOR(oglc)) {
 507                 case OGLC_VENDOR_NVIDIA:
 508                     // the via-texture codepath tends to be faster when
 509                     // there is either a simple scale OR an extra alpha
 510                     viaTexture =
 511                         (sx2-sx1) != (jint)(dx2-dx1) ||
 512                         (sy2-sy1) != (jint)(dy2-dy1) ||
 513                         oglc->extraAlpha != 1.0f;
 514                     break;
 515 
 516                 case OGLC_VENDOR_ATI:
 517                     // the via-texture codepath tends to be faster only when
 518                     // there is an extra alpha involved (scaling or not)
 519                     viaTexture = (oglc->extraAlpha != 1.0f);
 520                     break;
 521 
 522                 default:
 523                     // just use the glCopyPixels() codepath
 524                     viaTexture = JNI_FALSE;
 525                     break;
 526                 }
 527             }
 528 
 529             RESET_PREVIOUS_OP();
 530             if (viaTexture) {
 531                 OGLBlitToSurfaceViaTexture(oglc, &srcInfo, NULL, srcOps,
 532                                            JNI_FALSE, hint,
 533                                            sx1, sy1, sx2, sy2,
 534                                            dx1, dy1, dx2, dy2);
 535             } else {
 536                 OGLBlitSurfaceToSurface(oglc, srcOps, dstOps,
 537                                         sx1, sy1, sx2, sy2,
 538                                         dx1, dy1, dx2, dy2);
 539             }
 540         }
 541     }
 542 }
 543 
 544 /**
 545  * General blit method for copying a system memory ("Sw") surface to a native
 546  * OpenGL surface (of type "Surface" or "Texture").  If texture is JNI_TRUE,
 547  * this method will invoke the Sw->Texture inner loop; otherwise, one of the
 548  * Sw->Surface inner loops will be invoked, depending on the transform state.
 549  */
 550 void
 551 OGLBlitLoops_Blit(JNIEnv *env,
 552                   OGLContext *oglc, jlong pSrcOps, jlong pDstOps,
 553                   jboolean xform, jint hint,
 554                   jint srctype, jboolean texture,
 555                   jint sx1, jint sy1, jint sx2, jint sy2,
 556                   jdouble dx1, jdouble dy1, jdouble dx2, jdouble dy2)
 557 {
 558     SurfaceDataOps *srcOps = (SurfaceDataOps *)jlong_to_ptr(pSrcOps);
 559     OGLSDOps *dstOps = (OGLSDOps *)jlong_to_ptr(pDstOps);
 560     SurfaceDataRasInfo srcInfo;
 561     OGLPixelFormat pf = PixelFormats[srctype];
 562     jint sw    = sx2 - sx1;
 563     jint sh    = sy2 - sy1;
 564     jdouble dw = dx2 - dx1;
 565     jdouble dh = dy2 - dy1;
 566 
 567     J2dTraceLn(J2D_TRACE_INFO, "OGLBlitLoops_Blit");
 568 
 569     if (sw <= 0 || sh <= 0 || dw <= 0 || dh <= 0 || srctype < 0) {
 570         J2dTraceLn(J2D_TRACE_WARNING,
 571                    "OGLBlitLoops_Blit: invalid dimensions or srctype");
 572         return;
 573     }
 574 
 575     RETURN_IF_NULL(srcOps);
 576     RETURN_IF_NULL(dstOps);
 577     RETURN_IF_NULL(oglc);
 578     RESET_PREVIOUS_OP();
 579 
 580     srcInfo.bounds.x1 = sx1;
 581     srcInfo.bounds.y1 = sy1;
 582     srcInfo.bounds.x2 = sx2;
 583     srcInfo.bounds.y2 = sy2;
 584 
 585     if (srcOps->Lock(env, srcOps, &srcInfo, SD_LOCK_READ) != SD_SUCCESS) {
 586         J2dTraceLn(J2D_TRACE_WARNING,
 587                    "OGLBlitLoops_Blit: could not acquire lock");
 588         return;
 589     }
 590 
 591     if (srcInfo.bounds.x2 > srcInfo.bounds.x1 &&
 592         srcInfo.bounds.y2 > srcInfo.bounds.y1)
 593     {
 594         srcOps->GetRasInfo(env, srcOps, &srcInfo);
 595         if (srcInfo.rasBase) {
 596             if (srcInfo.bounds.x1 != sx1) {
 597                 dx1 += (srcInfo.bounds.x1 - sx1) * (dw / sw);
 598                 sx1 = srcInfo.bounds.x1;
 599             }
 600             if (srcInfo.bounds.y1 != sy1) {
 601                 dy1 += (srcInfo.bounds.y1 - sy1) * (dh / sh);
 602                 sy1 = srcInfo.bounds.y1;
 603             }
 604             if (srcInfo.bounds.x2 != sx2) {
 605                 dx2 += (srcInfo.bounds.x2 - sx2) * (dw / sw);
 606                 sx2 = srcInfo.bounds.x2;
 607             }
 608             if (srcInfo.bounds.y2 != sy2) {
 609                 dy2 += (srcInfo.bounds.y2 - sy2) * (dh / sh);
 610                 sy2 = srcInfo.bounds.y2;
 611             }
 612 
 613             J2dTraceLn3(J2D_TRACE_VERBOSE, "  texture=%d srctype=%d hint=%d",
 614                         texture, srctype, hint);
 615             J2dTraceLn4(J2D_TRACE_VERBOSE, "  sx1=%d sy1=%d sx2=%d sy2=%d",
 616                         sx1, sy1, sx2, sy2);
 617             J2dTraceLn4(J2D_TRACE_VERBOSE, "  dx1=%f dy1=%f dx2=%f dy2=%f",
 618                         dx1, dy1, dx2, dy2);
 619 
 620             j2d_glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx1);
 621             j2d_glPixelStorei(GL_UNPACK_SKIP_ROWS, sy1);
 622             j2d_glPixelStorei(GL_UNPACK_ROW_LENGTH,
 623                               srcInfo.scanStride / srcInfo.pixelStride);
 624             j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, pf.alignment);
 625 
 626             if (texture) {
 627                 // These coordinates will always be integers since we
 628                 // only ever do a straight copy from sw to texture.
 629                 // Thus these casts are "safe" - no loss of precision.
 630                 OGLBlitSwToTexture(&srcInfo, &pf, dstOps,
 631                                    (jint)dx1, (jint)dy1, (jint)dx2, (jint)dy2);
 632             } else {
 633                 jboolean viaTexture;
 634                 if (xform) {
 635                     // we must use the via-texture codepath when there
 636                     // is a xform
 637                     viaTexture = JNI_TRUE;
 638                 } else {
 639                     // look at the vendor to see which codepath is faster
 640                     // (this has been empirically determined; see 5020009)
 641                     switch (OGLC_GET_VENDOR(oglc)) {
 642                     case OGLC_VENDOR_NVIDIA:
 643                         // the via-texture codepath tends to be faster when
 644                         // there is either a simple scale OR an extra alpha
 645                         viaTexture =
 646                             (sx2-sx1) != (jint)(dx2-dx1) ||
 647                             (sy2-sy1) != (jint)(dy2-dy1) ||
 648                             oglc->extraAlpha != 1.0f;
 649                         break;
 650 
 651                     default:
 652                         // just use the glDrawPixels() codepath
 653                         viaTexture = JNI_FALSE;
 654                         break;
 655                     }
 656                 }
 657 
 658                 if (viaTexture) {
 659                     OGLBlitToSurfaceViaTexture(oglc, &srcInfo, &pf, NULL,
 660                                                JNI_TRUE, hint,
 661                                                sx1, sy1, sx2, sy2,
 662                                                dx1, dy1, dx2, dy2);
 663                 } else {
 664                     OGLBlitSwToSurface(oglc, &srcInfo, &pf,
 665                                        sx1, sy1, sx2, sy2,
 666                                        dx1, dy1, dx2, dy2);
 667                 }
 668             }
 669 
 670             j2d_glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
 671             j2d_glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
 672             j2d_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
 673             j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
 674         }
 675         SurfaceData_InvokeRelease(env, srcOps, &srcInfo);
 676     }
 677     SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
 678 }
 679 
 680 /**
 681  * Specialized blit method for copying a native OpenGL "Surface" (pbuffer,
 682  * window, etc.) to a system memory ("Sw") surface.
 683  */
 684 void
 685 OGLBlitLoops_SurfaceToSwBlit(JNIEnv *env, OGLContext *oglc,
 686                              jlong pSrcOps, jlong pDstOps, jint dsttype,
 687                              jint srcx, jint srcy, jint dstx, jint dsty,
 688                              jint width, jint height)
 689 {
 690     OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrcOps);
 691     SurfaceDataOps *dstOps = (SurfaceDataOps *)jlong_to_ptr(pDstOps);
 692     SurfaceDataRasInfo srcInfo, dstInfo;
 693     OGLPixelFormat pf = PixelFormats[dsttype];
 694 
 695     J2dTraceLn(J2D_TRACE_INFO, "OGLBlitLoops_SurfaceToSwBlit");
 696 
 697     if (width <= 0 || height <= 0) {
 698         J2dTraceLn(J2D_TRACE_WARNING,
 699             "OGLBlitLoops_SurfaceToSwBlit: dimensions are non-positive");
 700         return;
 701     }
 702 
 703     RETURN_IF_NULL(srcOps);
 704     RETURN_IF_NULL(dstOps);
 705     RETURN_IF_NULL(oglc);
 706     RESET_PREVIOUS_OP();
 707 
 708     srcInfo.bounds.x1 = srcx;
 709     srcInfo.bounds.y1 = srcy;
 710     srcInfo.bounds.x2 = srcx + width;
 711     srcInfo.bounds.y2 = srcy + height;
 712     dstInfo.bounds.x1 = dstx;
 713     dstInfo.bounds.y1 = dsty;
 714     dstInfo.bounds.x2 = dstx + width;
 715     dstInfo.bounds.y2 = dsty + height;
 716 
 717     if (dstOps->Lock(env, dstOps, &dstInfo, SD_LOCK_WRITE) != SD_SUCCESS) {
 718         J2dTraceLn(J2D_TRACE_WARNING,
 719             "OGLBlitLoops_SurfaceToSwBlit: could not acquire dst lock");
 720         return;
 721     }
 722 
 723     SurfaceData_IntersectBoundsXYXY(&srcInfo.bounds,
 724                                     0, 0, srcOps->width, srcOps->height);
 725     SurfaceData_IntersectBlitBounds(&dstInfo.bounds, &srcInfo.bounds,
 726                                     srcx - dstx, srcy - dsty);
 727 
 728     if (srcInfo.bounds.x2 > srcInfo.bounds.x1 &&
 729         srcInfo.bounds.y2 > srcInfo.bounds.y1)
 730     {
 731         dstOps->GetRasInfo(env, dstOps, &dstInfo);
 732         if (dstInfo.rasBase) {
 733             void *pDst = dstInfo.rasBase;
 734 
 735             srcx = srcInfo.bounds.x1;
 736             srcy = srcInfo.bounds.y1;
 737             dstx = dstInfo.bounds.x1;
 738             dsty = dstInfo.bounds.y1;
 739             width = srcInfo.bounds.x2 - srcInfo.bounds.x1;
 740             height = srcInfo.bounds.y2 - srcInfo.bounds.y1;
 741 
 742             j2d_glPixelStorei(GL_PACK_SKIP_PIXELS, dstx);
 743             j2d_glPixelStorei(GL_PACK_ROW_LENGTH,
 744                               dstInfo.scanStride / dstInfo.pixelStride);
 745             j2d_glPixelStorei(GL_PACK_ALIGNMENT, pf.alignment);
 746 #ifdef MACOSX
 747             if (srcOps->isOpaque) {
 748                 // For some reason Apple's OpenGL implementation will
 749                 // read back zero values from the alpha channel of an
 750                 // opaque surface when using glReadPixels(), so here we
 751                 // force the resulting pixels to be fully opaque.
 752                 j2d_glPixelTransferf(GL_ALPHA_BIAS, 1.0);
 753             }
 754 #endif
 755 
 756             J2dTraceLn4(J2D_TRACE_VERBOSE, "  sx=%d sy=%d w=%d h=%d",
 757                         srcx, srcy, width, height);
 758             J2dTraceLn2(J2D_TRACE_VERBOSE, "  dx=%d dy=%d",
 759                         dstx, dsty);
 760 
 761             // this accounts for lower-left origin of the source region
 762             srcx = srcOps->xOffset + srcx;
 763             srcy = srcOps->yOffset + srcOps->height - (srcy + 1);
 764 
 765             // we must read one scanline at a time because there is no way
 766             // to read starting at the top-left corner of the source region
 767             while (height > 0) {
 768                 j2d_glPixelStorei(GL_PACK_SKIP_ROWS, dsty);
 769                 j2d_glReadPixels(srcx, srcy, width, 1,
 770                                  pf.format, pf.type, pDst);
 771                 srcy--;
 772                 dsty++;
 773                 height--;
 774             }
 775 
 776 #ifdef MACOSX
 777             if (srcOps->isOpaque) {
 778                 j2d_glPixelTransferf(GL_ALPHA_BIAS, 0.0);
 779             }
 780 #endif
 781 
 782             j2d_glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
 783             j2d_glPixelStorei(GL_PACK_SKIP_ROWS, 0);
 784             j2d_glPixelStorei(GL_PACK_ROW_LENGTH, 0);
 785             j2d_glPixelStorei(GL_PACK_ALIGNMENT, 4);
 786         }
 787         SurfaceData_InvokeRelease(env, dstOps, &dstInfo);
 788     }
 789     SurfaceData_InvokeUnlock(env, dstOps, &dstInfo);
 790 }
 791 
 792 void
 793 OGLBlitLoops_CopyArea(JNIEnv *env,
 794                       OGLContext *oglc, OGLSDOps *dstOps,
 795                       jint x, jint y, jint width, jint height,
 796                       jint dx, jint dy)
 797 {
 798     SurfaceDataBounds srcBounds, dstBounds;
 799 
 800     J2dTraceLn(J2D_TRACE_INFO, "OGLBlitLoops_CopyArea");
 801 
 802     RETURN_IF_NULL(oglc);
 803     RETURN_IF_NULL(dstOps);
 804     RESET_PREVIOUS_OP();
 805 
 806     J2dTraceLn4(J2D_TRACE_VERBOSE, "  x=%d y=%d w=%d h=%d",
 807                 x, y, width, height);
 808     J2dTraceLn2(J2D_TRACE_VERBOSE, "  dx=%d dy=%d",
 809                 dx, dy);
 810 
 811     srcBounds.x1 = x;
 812     srcBounds.y1 = y;
 813     srcBounds.x2 = srcBounds.x1 + width;
 814     srcBounds.y2 = srcBounds.y1 + height;
 815     dstBounds.x1 = x + dx;
 816     dstBounds.y1 = y + dy;
 817     dstBounds.x2 = dstBounds.x1 + width;
 818     dstBounds.y2 = dstBounds.y1 + height;
 819 
 820     // 6430601: manually clip src/dst parameters to work around
 821     // some bugs in Sun's and Apple's OpenGL implementations
 822     // (it's a good idea to restrict the source parameters anyway, since
 823     // passing out of range parameters to glCopyPixels() will result in
 824     // an OpenGL error)
 825     SurfaceData_IntersectBoundsXYXY(&srcBounds,
 826                                     0, 0, dstOps->width, dstOps->height);
 827     SurfaceData_IntersectBoundsXYXY(&dstBounds,
 828                                     0, 0, dstOps->width, dstOps->height);
 829     SurfaceData_IntersectBlitBounds(&dstBounds, &srcBounds, -dx, -dy);
 830 
 831     if (dstBounds.x1 < dstBounds.x2 && dstBounds.y1 < dstBounds.y2) {
 832 #ifdef MACOSX
 833         if (dstOps->isOpaque) {
 834             // For some reason Apple's OpenGL implementation will fail
 835             // to render glCopyPixels() when the src/dst rectangles are
 836             // overlapping and glColorMask() has disabled writes to the
 837             // alpha channel.  The workaround is to temporarily re-enable
 838             // the alpha channel during the glCopyPixels() operation.
 839             j2d_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 840         }
 841 #endif
 842 
 843         OGLBlitSurfaceToSurface(oglc, dstOps, dstOps,
 844                                 srcBounds.x1, srcBounds.y1,
 845                                 srcBounds.x2, srcBounds.y2,
 846                                 dstBounds.x1, dstBounds.y1,
 847                                 dstBounds.x2, dstBounds.y2);
 848 #ifdef MACOSX
 849         if (dstOps->isOpaque) {
 850             j2d_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
 851         }
 852 #endif
 853     }
 854 }
 855 
 856 #endif /* !HEADLESS */