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 jboolean adjustAlpha = (pf != NULL && !pf->hasAlpha); 397 j2d_glBindTexture(dstOps->textureTarget, dstOps->textureID); 398 399 if (adjustAlpha) { 400 // if the source surface does not have an alpha channel, 401 // we need to ensure that the alpha values are forced to 1.0f 402 j2d_glPixelTransferf(GL_ALPHA_SCALE, 0.0f); 403 j2d_glPixelTransferf(GL_ALPHA_BIAS, 1.0f); 404 } 405 406 // in case pixel stride is not a multiple of scanline stride the copy 407 // has to be done line by line (see 6207877) 408 if (srcInfo->scanStride % srcInfo->pixelStride != 0) { 409 jint width = dx2 - dx1; 410 jint height = dy2 - dy1; 411 GLvoid *pSrc = srcInfo->rasBase; 412 413 while (height > 0) { 414 j2d_glTexSubImage2D(dstOps->textureTarget, 0, 415 dx1, dy2 - height, width, 1, 416 pf->format, pf->type, pSrc); 417 pSrc = PtrAddBytes(pSrc, srcInfo->scanStride); 418 height--; 419 } 420 } else { 421 j2d_glTexSubImage2D(dstOps->textureTarget, 0, 422 dx1, dy1, dx2-dx1, dy2-dy1, 423 pf->format, pf->type, srcInfo->rasBase); 424 } 425 if (adjustAlpha) { 426 // restore scale/bias to their original values 427 j2d_glPixelTransferf(GL_ALPHA_SCALE, 1.0f); 428 j2d_glPixelTransferf(GL_ALPHA_BIAS, 0.0f); 429 } 430 } 431 432 /** 433 * General blit method for copying a native OpenGL surface (of type "Surface" 434 * or "Texture") to another OpenGL "Surface". If texture is JNI_TRUE, this 435 * method will invoke the Texture->Surface inner loop; otherwise, one of the 436 * Surface->Surface inner loops will be invoked, depending on the transform 437 * state. 438 * 439 * REMIND: we can trick these blit methods into doing XOR simply by passing 440 * in the (pixel ^ xorpixel) as the pixel value and preceding the 441 * blit with a fillrect... 442 */ 443 void 444 OGLBlitLoops_IsoBlit(JNIEnv *env, 445 OGLContext *oglc, jlong pSrcOps, jlong pDstOps, 446 jboolean xform, jint hint, 447 jboolean texture, jboolean rtt, 448 jint sx1, jint sy1, jint sx2, jint sy2, 449 jdouble dx1, jdouble dy1, jdouble dx2, jdouble dy2) 450 { 451 OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrcOps); 452 OGLSDOps *dstOps = (OGLSDOps *)jlong_to_ptr(pDstOps); 453 SurfaceDataRasInfo srcInfo; 454 jint sw = sx2 - sx1; 455 jint sh = sy2 - sy1; 456 jdouble dw = dx2 - dx1; 457 jdouble dh = dy2 - dy1; 458 459 J2dTraceLn(J2D_TRACE_INFO, "OGLBlitLoops_IsoBlit"); 460 461 if (sw <= 0 || sh <= 0 || dw <= 0 || dh <= 0) { 462 J2dTraceLn(J2D_TRACE_WARNING, 463 "OGLBlitLoops_IsoBlit: invalid dimensions"); 464 return; 465 } 466 467 RETURN_IF_NULL(srcOps); 468 RETURN_IF_NULL(dstOps); 469 RETURN_IF_NULL(oglc); 470 471 srcInfo.bounds.x1 = sx1; 472 srcInfo.bounds.y1 = sy1; 473 srcInfo.bounds.x2 = sx2; 474 srcInfo.bounds.y2 = sy2; 475 476 SurfaceData_IntersectBoundsXYXY(&srcInfo.bounds, 477 0, 0, srcOps->width, srcOps->height); 478 479 if (srcInfo.bounds.x2 > srcInfo.bounds.x1 && 480 srcInfo.bounds.y2 > srcInfo.bounds.y1) 481 { 482 if (srcInfo.bounds.x1 != sx1) { 483 dx1 += (srcInfo.bounds.x1 - sx1) * (dw / sw); 484 sx1 = srcInfo.bounds.x1; 485 } 486 if (srcInfo.bounds.y1 != sy1) { 487 dy1 += (srcInfo.bounds.y1 - sy1) * (dh / sh); 488 sy1 = srcInfo.bounds.y1; 489 } 490 if (srcInfo.bounds.x2 != sx2) { 491 dx2 += (srcInfo.bounds.x2 - sx2) * (dw / sw); 492 sx2 = srcInfo.bounds.x2; 493 } 494 if (srcInfo.bounds.y2 != sy2) { 495 dy2 += (srcInfo.bounds.y2 - sy2) * (dh / sh); 496 sy2 = srcInfo.bounds.y2; 497 } 498 499 J2dTraceLn2(J2D_TRACE_VERBOSE, " texture=%d hint=%d", texture, hint); 500 J2dTraceLn4(J2D_TRACE_VERBOSE, " sx1=%d sy1=%d sx2=%d sy2=%d", 501 sx1, sy1, sx2, sy2); 502 J2dTraceLn4(J2D_TRACE_VERBOSE, " dx1=%f dy1=%f dx2=%f dy2=%f", 503 dx1, dy1, dx2, dy2); 504 505 if (texture) { 506 GLint glhint = (hint == OGLSD_XFORM_BILINEAR) ? GL_LINEAR : 507 GL_NEAREST; 508 CHECK_PREVIOUS_OP(srcOps->textureTarget); 509 OGLBlitTextureToSurface(oglc, srcOps, dstOps, rtt, glhint, 510 sx1, sy1, sx2, sy2, 511 dx1, dy1, dx2, dy2); 512 } else { 513 jboolean viaTexture; 514 if (xform) { 515 // we must use the via-texture codepath when there is a xform 516 viaTexture = JNI_TRUE; 517 } else { 518 // look at the vendor to see which codepath is faster 519 // (this has been empirically determined; see 5020009) 520 switch (OGLC_GET_VENDOR(oglc)) { 521 case OGLC_VENDOR_NVIDIA: 522 // the via-texture codepath tends to be faster when 523 // there is either a simple scale OR an extra alpha 524 viaTexture = 525 (sx2-sx1) != (jint)(dx2-dx1) || 526 (sy2-sy1) != (jint)(dy2-dy1) || 527 oglc->extraAlpha != 1.0f; 528 break; 529 530 case OGLC_VENDOR_ATI: 531 // the via-texture codepath tends to be faster only when 532 // there is an extra alpha involved (scaling or not) 533 viaTexture = (oglc->extraAlpha != 1.0f); 534 break; 535 536 default: 537 // just use the glCopyPixels() codepath 538 viaTexture = JNI_FALSE; 539 break; 540 } 541 } 542 543 RESET_PREVIOUS_OP(); 544 if (viaTexture) { 545 OGLBlitToSurfaceViaTexture(oglc, &srcInfo, NULL, srcOps, 546 JNI_FALSE, hint, 547 sx1, sy1, sx2, sy2, 548 dx1, dy1, dx2, dy2); 549 } else { 550 OGLBlitSurfaceToSurface(oglc, srcOps, dstOps, 551 sx1, sy1, sx2, sy2, 552 dx1, dy1, dx2, dy2); 553 } 554 } 555 } 556 } 557 558 /** 559 * General blit method for copying a system memory ("Sw") surface to a native 560 * OpenGL surface (of type "Surface" or "Texture"). If texture is JNI_TRUE, 561 * this method will invoke the Sw->Texture inner loop; otherwise, one of the 562 * Sw->Surface inner loops will be invoked, depending on the transform state. 563 */ 564 void 565 OGLBlitLoops_Blit(JNIEnv *env, 566 OGLContext *oglc, jlong pSrcOps, jlong pDstOps, 567 jboolean xform, jint hint, 568 jint srctype, jboolean texture, 569 jint sx1, jint sy1, jint sx2, jint sy2, 570 jdouble dx1, jdouble dy1, jdouble dx2, jdouble dy2) 571 { 572 SurfaceDataOps *srcOps = (SurfaceDataOps *)jlong_to_ptr(pSrcOps); 573 OGLSDOps *dstOps = (OGLSDOps *)jlong_to_ptr(pDstOps); 574 SurfaceDataRasInfo srcInfo; 575 OGLPixelFormat pf = PixelFormats[srctype]; 576 jint sw = sx2 - sx1; 577 jint sh = sy2 - sy1; 578 jdouble dw = dx2 - dx1; 579 jdouble dh = dy2 - dy1; 580 581 J2dTraceLn(J2D_TRACE_INFO, "OGLBlitLoops_Blit"); 582 583 if (sw <= 0 || sh <= 0 || dw <= 0 || dh <= 0 || srctype < 0) { 584 J2dTraceLn(J2D_TRACE_WARNING, 585 "OGLBlitLoops_Blit: invalid dimensions or srctype"); 586 return; 587 } 588 589 RETURN_IF_NULL(srcOps); 590 RETURN_IF_NULL(dstOps); 591 RETURN_IF_NULL(oglc); 592 RESET_PREVIOUS_OP(); 593 594 srcInfo.bounds.x1 = sx1; 595 srcInfo.bounds.y1 = sy1; 596 srcInfo.bounds.x2 = sx2; 597 srcInfo.bounds.y2 = sy2; 598 599 if (srcOps->Lock(env, srcOps, &srcInfo, SD_LOCK_READ) != SD_SUCCESS) { 600 J2dTraceLn(J2D_TRACE_WARNING, 601 "OGLBlitLoops_Blit: could not acquire lock"); 602 return; 603 } 604 605 if (srcInfo.bounds.x2 > srcInfo.bounds.x1 && 606 srcInfo.bounds.y2 > srcInfo.bounds.y1) 607 { 608 srcOps->GetRasInfo(env, srcOps, &srcInfo); 609 if (srcInfo.rasBase) { 610 if (srcInfo.bounds.x1 != sx1) { 611 dx1 += (srcInfo.bounds.x1 - sx1) * (dw / sw); 612 sx1 = srcInfo.bounds.x1; 613 } 614 if (srcInfo.bounds.y1 != sy1) { 615 dy1 += (srcInfo.bounds.y1 - sy1) * (dh / sh); 616 sy1 = srcInfo.bounds.y1; 617 } 618 if (srcInfo.bounds.x2 != sx2) { 619 dx2 += (srcInfo.bounds.x2 - sx2) * (dw / sw); 620 sx2 = srcInfo.bounds.x2; 621 } 622 if (srcInfo.bounds.y2 != sy2) { 623 dy2 += (srcInfo.bounds.y2 - sy2) * (dh / sh); 624 sy2 = srcInfo.bounds.y2; 625 } 626 627 J2dTraceLn3(J2D_TRACE_VERBOSE, " texture=%d srctype=%d hint=%d", 628 texture, srctype, hint); 629 J2dTraceLn4(J2D_TRACE_VERBOSE, " sx1=%d sy1=%d sx2=%d sy2=%d", 630 sx1, sy1, sx2, sy2); 631 J2dTraceLn4(J2D_TRACE_VERBOSE, " dx1=%f dy1=%f dx2=%f dy2=%f", 632 dx1, dy1, dx2, dy2); 633 634 j2d_glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx1); 635 j2d_glPixelStorei(GL_UNPACK_SKIP_ROWS, sy1); 636 j2d_glPixelStorei(GL_UNPACK_ROW_LENGTH, 637 srcInfo.scanStride / srcInfo.pixelStride); 638 j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, pf.alignment); 639 640 if (texture) { 641 // These coordinates will always be integers since we 642 // only ever do a straight copy from sw to texture. 643 // Thus these casts are "safe" - no loss of precision. 644 OGLBlitSwToTexture(&srcInfo, &pf, dstOps, 645 (jint)dx1, (jint)dy1, (jint)dx2, (jint)dy2); 646 } else { 647 jboolean viaTexture; 648 if (xform) { 649 // we must use the via-texture codepath when there 650 // is a xform 651 viaTexture = JNI_TRUE; 652 } else { 653 // look at the vendor to see which codepath is faster 654 // (this has been empirically determined; see 5020009) 655 switch (OGLC_GET_VENDOR(oglc)) { 656 case OGLC_VENDOR_NVIDIA: 657 // the via-texture codepath tends to be faster when 658 // there is either a simple scale OR an extra alpha 659 viaTexture = 660 (sx2-sx1) != (jint)(dx2-dx1) || 661 (sy2-sy1) != (jint)(dy2-dy1) || 662 oglc->extraAlpha != 1.0f; 663 break; 664 665 default: 666 // just use the glDrawPixels() codepath 667 viaTexture = JNI_FALSE; 668 break; 669 } 670 } 671 672 if (viaTexture) { 673 OGLBlitToSurfaceViaTexture(oglc, &srcInfo, &pf, NULL, 674 JNI_TRUE, hint, 675 sx1, sy1, sx2, sy2, 676 dx1, dy1, dx2, dy2); 677 } else { 678 OGLBlitSwToSurface(oglc, &srcInfo, &pf, 679 sx1, sy1, sx2, sy2, 680 dx1, dy1, dx2, dy2); 681 } 682 } 683 684 j2d_glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); 685 j2d_glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); 686 j2d_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); 687 j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 688 } 689 SurfaceData_InvokeRelease(env, srcOps, &srcInfo); 690 } 691 SurfaceData_InvokeUnlock(env, srcOps, &srcInfo); 692 } 693 694 /** 695 * Specialized blit method for copying a native OpenGL "Surface" (pbuffer, 696 * window, etc.) to a system memory ("Sw") surface. 697 */ 698 void 699 OGLBlitLoops_SurfaceToSwBlit(JNIEnv *env, OGLContext *oglc, 700 jlong pSrcOps, jlong pDstOps, jint dsttype, 701 jint srcx, jint srcy, jint dstx, jint dsty, 702 jint width, jint height) 703 { 704 OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrcOps); 705 SurfaceDataOps *dstOps = (SurfaceDataOps *)jlong_to_ptr(pDstOps); 706 SurfaceDataRasInfo srcInfo, dstInfo; 707 OGLPixelFormat pf = PixelFormats[dsttype]; 708 709 J2dTraceLn(J2D_TRACE_INFO, "OGLBlitLoops_SurfaceToSwBlit"); 710 711 if (width <= 0 || height <= 0) { 712 J2dTraceLn(J2D_TRACE_WARNING, 713 "OGLBlitLoops_SurfaceToSwBlit: dimensions are non-positive"); 714 return; 715 } 716 717 RETURN_IF_NULL(srcOps); 718 RETURN_IF_NULL(dstOps); 719 RETURN_IF_NULL(oglc); 720 RESET_PREVIOUS_OP(); 721 722 srcInfo.bounds.x1 = srcx; 723 srcInfo.bounds.y1 = srcy; 724 srcInfo.bounds.x2 = srcx + width; 725 srcInfo.bounds.y2 = srcy + height; 726 dstInfo.bounds.x1 = dstx; 727 dstInfo.bounds.y1 = dsty; 728 dstInfo.bounds.x2 = dstx + width; 729 dstInfo.bounds.y2 = dsty + height; 730 731 if (dstOps->Lock(env, dstOps, &dstInfo, SD_LOCK_WRITE) != SD_SUCCESS) { 732 J2dTraceLn(J2D_TRACE_WARNING, 733 "OGLBlitLoops_SurfaceToSwBlit: could not acquire dst lock"); 734 return; 735 } 736 737 SurfaceData_IntersectBoundsXYXY(&srcInfo.bounds, 738 0, 0, srcOps->width, srcOps->height); 739 SurfaceData_IntersectBlitBounds(&dstInfo.bounds, &srcInfo.bounds, 740 srcx - dstx, srcy - dsty); 741 742 if (srcInfo.bounds.x2 > srcInfo.bounds.x1 && 743 srcInfo.bounds.y2 > srcInfo.bounds.y1) 744 { 745 dstOps->GetRasInfo(env, dstOps, &dstInfo); 746 if (dstInfo.rasBase) { 747 void *pDst = dstInfo.rasBase; 748 749 srcx = srcInfo.bounds.x1; 750 srcy = srcInfo.bounds.y1; 751 dstx = dstInfo.bounds.x1; 752 dsty = dstInfo.bounds.y1; 753 width = srcInfo.bounds.x2 - srcInfo.bounds.x1; 754 height = srcInfo.bounds.y2 - srcInfo.bounds.y1; 755 756 j2d_glPixelStorei(GL_PACK_SKIP_PIXELS, dstx); 757 j2d_glPixelStorei(GL_PACK_ROW_LENGTH, 758 dstInfo.scanStride / dstInfo.pixelStride); 759 j2d_glPixelStorei(GL_PACK_ALIGNMENT, pf.alignment); 760 #ifdef MACOSX 761 if (srcOps->isOpaque) { 762 // For some reason Apple's OpenGL implementation will 763 // read back zero values from the alpha channel of an 764 // opaque surface when using glReadPixels(), so here we 765 // force the resulting pixels to be fully opaque. 766 j2d_glPixelTransferf(GL_ALPHA_BIAS, 1.0); 767 } 768 #endif 769 770 J2dTraceLn4(J2D_TRACE_VERBOSE, " sx=%d sy=%d w=%d h=%d", 771 srcx, srcy, width, height); 772 J2dTraceLn2(J2D_TRACE_VERBOSE, " dx=%d dy=%d", 773 dstx, dsty); 774 775 // this accounts for lower-left origin of the source region 776 srcx = srcOps->xOffset + srcx; 777 srcy = srcOps->yOffset + srcOps->height - (srcy + 1); 778 779 // we must read one scanline at a time because there is no way 780 // to read starting at the top-left corner of the source region 781 while (height > 0) { 782 j2d_glPixelStorei(GL_PACK_SKIP_ROWS, dsty); 783 j2d_glReadPixels(srcx, srcy, width, 1, 784 pf.format, pf.type, pDst); 785 srcy--; 786 dsty++; 787 height--; 788 } 789 790 #ifdef MACOSX 791 if (srcOps->isOpaque) { 792 j2d_glPixelTransferf(GL_ALPHA_BIAS, 0.0); 793 } 794 #endif 795 796 j2d_glPixelStorei(GL_PACK_SKIP_PIXELS, 0); 797 j2d_glPixelStorei(GL_PACK_SKIP_ROWS, 0); 798 j2d_glPixelStorei(GL_PACK_ROW_LENGTH, 0); 799 j2d_glPixelStorei(GL_PACK_ALIGNMENT, 4); 800 } 801 SurfaceData_InvokeRelease(env, dstOps, &dstInfo); 802 } 803 SurfaceData_InvokeUnlock(env, dstOps, &dstInfo); 804 } 805 806 void 807 OGLBlitLoops_CopyArea(JNIEnv *env, 808 OGLContext *oglc, OGLSDOps *dstOps, 809 jint x, jint y, jint width, jint height, 810 jint dx, jint dy) 811 { 812 SurfaceDataBounds srcBounds, dstBounds; 813 814 J2dTraceLn(J2D_TRACE_INFO, "OGLBlitLoops_CopyArea"); 815 816 RETURN_IF_NULL(oglc); 817 RETURN_IF_NULL(dstOps); 818 RESET_PREVIOUS_OP(); 819 820 J2dTraceLn4(J2D_TRACE_VERBOSE, " x=%d y=%d w=%d h=%d", 821 x, y, width, height); 822 J2dTraceLn2(J2D_TRACE_VERBOSE, " dx=%d dy=%d", 823 dx, dy); 824 825 srcBounds.x1 = x; 826 srcBounds.y1 = y; 827 srcBounds.x2 = srcBounds.x1 + width; 828 srcBounds.y2 = srcBounds.y1 + height; 829 dstBounds.x1 = x + dx; 830 dstBounds.y1 = y + dy; 831 dstBounds.x2 = dstBounds.x1 + width; 832 dstBounds.y2 = dstBounds.y1 + height; 833 834 // 6430601: manually clip src/dst parameters to work around 835 // some bugs in Sun's and Apple's OpenGL implementations 836 // (it's a good idea to restrict the source parameters anyway, since 837 // passing out of range parameters to glCopyPixels() will result in 838 // an OpenGL error) 839 SurfaceData_IntersectBoundsXYXY(&srcBounds, 840 0, 0, dstOps->width, dstOps->height); 841 SurfaceData_IntersectBoundsXYXY(&dstBounds, 842 0, 0, dstOps->width, dstOps->height); 843 SurfaceData_IntersectBlitBounds(&dstBounds, &srcBounds, -dx, -dy); 844 845 if (dstBounds.x1 < dstBounds.x2 && dstBounds.y1 < dstBounds.y2) { 846 #ifdef MACOSX 847 if (dstOps->isOpaque) { 848 // For some reason Apple's OpenGL implementation will fail 849 // to render glCopyPixels() when the src/dst rectangles are 850 // overlapping and glColorMask() has disabled writes to the 851 // alpha channel. The workaround is to temporarily re-enable 852 // the alpha channel during the glCopyPixels() operation. 853 j2d_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 854 } 855 #endif 856 857 OGLBlitSurfaceToSurface(oglc, dstOps, dstOps, 858 srcBounds.x1, srcBounds.y1, 859 srcBounds.x2, srcBounds.y2, 860 dstBounds.x1, dstBounds.y1, 861 dstBounds.x2, dstBounds.y2); 862 #ifdef MACOSX 863 if (dstOps->isOpaque) { 864 j2d_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE); 865 } 866 #endif 867 } 868 } 869 870 #endif /* !HEADLESS */