1 /*
   2  * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdlib.h>
  27 
  28 #include "sun_java2d_opengl_WGLSurfaceData.h"
  29 
  30 #include "jni.h"
  31 #include "jlong.h"
  32 #include "jni_util.h"
  33 #include "sizecalc.h"
  34 #include "OGLRenderQueue.h"
  35 #include "WGLGraphicsConfig.h"
  36 #include "WGLSurfaceData.h"
  37 
  38 /**
  39  * The methods in this file implement the native windowing system specific
  40  * layer (WGL) for the OpenGL-based Java 2D pipeline.
  41  */
  42 
  43 extern LockFunc                     OGLSD_Lock;
  44 extern GetRasInfoFunc               OGLSD_GetRasInfo;
  45 extern UnlockFunc                   OGLSD_Unlock;
  46 extern DisposeFunc                  OGLSD_Dispose;
  47 
  48 extern OGLPixelFormat PixelFormats[];
  49 extern void AwtWindow_UpdateWindow(JNIEnv *env, jobject peer,
  50                                    jint w, jint h, HBITMAP hBitmap);
  51 extern HBITMAP BitmapUtil_CreateBitmapFromARGBPre(int width, int height,
  52                                                   int srcStride,
  53                                                   int* imageData);
  54 extern void AwtComponent_GetInsets(JNIEnv *env, jobject peer, RECT *insets);
  55 
  56 extern void
  57     OGLSD_SetNativeDimensions(JNIEnv *env, OGLSDOps *oglsdo, jint w, jint h);
  58 
  59 JNIEXPORT void JNICALL
  60 Java_sun_java2d_opengl_WGLSurfaceData_initOps(JNIEnv *env, jobject wglsd,
  61                                               jlong pConfigInfo,
  62                                               jobject peer, jlong hwnd)
  63 {
  64     OGLSDOps *oglsdo = (OGLSDOps *)SurfaceData_InitOps(env, wglsd,
  65                                                        sizeof(OGLSDOps));
  66     WGLSDOps *wglsdo = (WGLSDOps *)malloc(sizeof(WGLSDOps));
  67 
  68     J2dTraceLn(J2D_TRACE_INFO, "WGLSurfaceData_initOps");
  69 
  70     if (wglsdo == NULL) {
  71         JNU_ThrowOutOfMemoryError(env, "creating native wgl ops");
  72         return;
  73     }
  74     if (oglsdo == NULL) {
  75         free(wglsdo);
  76         JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
  77         return;
  78     }
  79 
  80     oglsdo->privOps = wglsdo;
  81 
  82     oglsdo->sdOps.Lock               = OGLSD_Lock;
  83     oglsdo->sdOps.GetRasInfo         = OGLSD_GetRasInfo;
  84     oglsdo->sdOps.Unlock             = OGLSD_Unlock;
  85     oglsdo->sdOps.Dispose            = OGLSD_Dispose;
  86 
  87     oglsdo->drawableType = OGLSD_UNDEFINED;
  88     oglsdo->activeBuffer = GL_FRONT;
  89     oglsdo->needsInit = JNI_TRUE;
  90     if (peer != NULL) {
  91         RECT insets;
  92         AwtComponent_GetInsets(env, peer, &insets);
  93         oglsdo->xOffset = -insets.left;
  94         oglsdo->yOffset = -insets.bottom;
  95     } else {
  96         oglsdo->xOffset = 0;
  97         oglsdo->yOffset = 0;
  98     }
  99 
 100     wglsdo->window = (HWND)jlong_to_ptr(hwnd);
 101     wglsdo->configInfo = (WGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
 102     if (wglsdo->configInfo == NULL) {
 103         free(wglsdo);
 104         JNU_ThrowNullPointerException(env, "Config info is null in initOps");
 105     }
 106 }
 107 
 108 /**
 109  * This function disposes of any native windowing system resources associated
 110  * with this surface.
 111  */
 112 void
 113 OGLSD_DestroyOGLSurface(JNIEnv *env, OGLSDOps *oglsdo)
 114 {
 115     // Window is free'd later by AWT code...
 116 }
 117 
 118 /**
 119  * Makes the given context current to its associated "scratch" surface.  If
 120  * the operation is successful, this method will return JNI_TRUE; otherwise,
 121  * returns JNI_FALSE.
 122  */
 123 static jboolean
 124 WGLSD_MakeCurrentToScratch(JNIEnv *env, OGLContext *oglc)
 125 {
 126     WGLCtxInfo *ctxInfo;
 127 
 128     J2dTraceLn(J2D_TRACE_INFO, "WGLSD_MakeCurrentToScratch");
 129 
 130     if (oglc == NULL) {
 131         J2dRlsTraceLn(J2D_TRACE_ERROR,
 132                       "WGLSD_MakeCurrentToScratch: context is null");
 133         return JNI_FALSE;
 134     }
 135 
 136     ctxInfo = (WGLCtxInfo *)oglc->ctxInfo;
 137     if (!j2d_wglMakeCurrent(ctxInfo->scratchSurfaceDC, ctxInfo->context)) {
 138         J2dRlsTraceLn(J2D_TRACE_ERROR,
 139                       "WGLSD_MakeCurrentToScratch: could not make current");
 140         return JNI_FALSE;
 141     }
 142 
 143     return JNI_TRUE;
 144 }
 145 
 146 /**
 147  * Returns a pointer (as a jlong) to the native WGLGraphicsConfigInfo
 148  * associated with the given OGLSDOps.  This method can be called from
 149  * shared code to retrieve the native GraphicsConfig data in a platform-
 150  * independent manner.
 151  */
 152 jlong
 153 OGLSD_GetNativeConfigInfo(OGLSDOps *oglsdo)
 154 {
 155     WGLSDOps *wglsdo;
 156 
 157     if (oglsdo == NULL) {
 158         J2dRlsTraceLn(J2D_TRACE_ERROR,
 159                       "OGLSD_GetNativeConfigInfo: ops are null");
 160         return 0L;
 161     }
 162 
 163     wglsdo = (WGLSDOps *)oglsdo->privOps;
 164     if (wglsdo == NULL) {
 165         J2dRlsTraceLn(J2D_TRACE_ERROR,
 166                       "OGLSD_GetNativeConfigInfo: wgl ops are null");
 167         return 0L;
 168     }
 169 
 170     return ptr_to_jlong(wglsdo->configInfo);
 171 }
 172 
 173 /**
 174  * Makes the given GraphicsConfig's context current to its associated
 175  * "scratch" surface.  If there is a problem making the context current,
 176  * this method will return NULL; otherwise, returns a pointer to the
 177  * OGLContext that is associated with the given GraphicsConfig.
 178  */
 179 OGLContext *
 180 OGLSD_SetScratchSurface(JNIEnv *env, jlong pConfigInfo)
 181 {
 182     WGLGraphicsConfigInfo *wglInfo =
 183         (WGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
 184     OGLContext *oglc;
 185 
 186     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_SetScratchContext");
 187 
 188     if (wglInfo == NULL) {
 189         J2dRlsTraceLn(J2D_TRACE_ERROR,
 190                       "OGLSD_SetScratchContext: wgl config info is null");
 191         return NULL;
 192     }
 193 
 194     oglc = wglInfo->context;
 195     if (!WGLSD_MakeCurrentToScratch(env, oglc)) {
 196         return NULL;
 197     }
 198 
 199     if (OGLC_IS_CAP_PRESENT(oglc, CAPS_EXT_FBOBJECT)) {
 200         // the GL_EXT_framebuffer_object extension is present, so this call
 201         // will ensure that we are bound to the scratch pbuffer (and not
 202         // some other framebuffer object)
 203         j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
 204     }
 205 
 206     return oglc;
 207 }
 208 
 209 /**
 210  * Makes a context current to the given source and destination
 211  * surfaces.  If there is a problem making the context current, this method
 212  * will return NULL; otherwise, returns a pointer to the OGLContext that is
 213  * associated with the destination surface.
 214  */
 215 OGLContext *
 216 OGLSD_MakeOGLContextCurrent(JNIEnv *env, OGLSDOps *srcOps, OGLSDOps *dstOps)
 217 {
 218     WGLSDOps *srcWGLOps = (WGLSDOps *)srcOps->privOps;
 219     WGLSDOps *dstWGLOps = (WGLSDOps *)dstOps->privOps;
 220     OGLContext *oglc;
 221     WGLCtxInfo *ctxinfo;
 222     HDC srcHDC, dstHDC;
 223     BOOL success;
 224 
 225     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_MakeOGLContextCurrent");
 226 
 227     J2dTraceLn4(J2D_TRACE_VERBOSE, "  src: %d %p dst: %d %p",
 228                 srcOps->drawableType, srcOps,
 229                 dstOps->drawableType, dstOps);
 230 
 231     oglc = dstWGLOps->configInfo->context;
 232     if (oglc == NULL) {
 233         J2dRlsTraceLn(J2D_TRACE_ERROR,
 234                       "OGLSD_MakeOGLContextCurrent: context is null");
 235         return NULL;
 236     }
 237 
 238     if (dstOps->drawableType == OGLSD_FBOBJECT) {
 239         OGLContext *currentContext = OGLRenderQueue_GetCurrentContext();
 240 
 241         // first make sure we have a current context (if the context isn't
 242         // already current to some drawable, we will make it current to
 243         // its scratch surface)
 244         if (oglc != currentContext) {
 245             if (!WGLSD_MakeCurrentToScratch(env, oglc)) {
 246                 return NULL;
 247             }
 248         }
 249 
 250         // now bind to the fbobject associated with the destination surface;
 251         // this means that all rendering will go into the fbobject destination
 252         // (note that we unbind the currently bound texture first; this is
 253         // recommended procedure when binding an fbobject)
 254         j2d_glBindTexture(dstOps->textureTarget, 0);
 255         j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, dstOps->fbobjectID);
 256 
 257         return oglc;
 258     }
 259 
 260     ctxinfo = (WGLCtxInfo *)oglc->ctxInfo;
 261 
 262     // get the hdc for the destination surface
 263     dstHDC = GetDC(dstWGLOps->window);
 264 
 265     // get the hdc for the source surface
 266     // the source will always be equal to the destination in this case
 267     srcHDC = dstHDC;
 268 
 269     // REMIND: in theory we should be able to use wglMakeContextCurrentARB()
 270     // even when the src/dst surfaces are the same, but this causes problems
 271     // on ATI's drivers (see 6525997); for now we will only use it when the
 272     // surfaces are different, otherwise we will use the old
 273     // wglMakeCurrent() approach...
 274     if (srcHDC != dstHDC) {
 275         // use WGL_ARB_make_current_read extension to make context current
 276         success =
 277             j2d_wglMakeContextCurrentARB(dstHDC, srcHDC, ctxinfo->context);
 278     } else {
 279         // use the old approach for making current to the destination
 280         success = j2d_wglMakeCurrent(dstHDC, ctxinfo->context);
 281     }
 282     if (!success) {
 283         J2dRlsTraceLn(J2D_TRACE_ERROR,
 284                       "OGLSD_MakeOGLContextCurrent: could not make current");
 285         ReleaseDC(dstWGLOps->window, dstHDC);
 286         return NULL;
 287     }
 288 
 289     if (OGLC_IS_CAP_PRESENT(oglc, CAPS_EXT_FBOBJECT)) {
 290         // the GL_EXT_framebuffer_object extension is present, so we
 291         // must bind to the default (windowing system provided)
 292         // framebuffer
 293         j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
 294     }
 295 
 296     ReleaseDC(dstWGLOps->window, dstHDC);
 297 
 298     return oglc;
 299 }
 300 
 301 /**
 302  * This function initializes a native window surface and caches the window
 303  * bounds in the given OGLSDOps.  Returns JNI_TRUE if the operation was
 304  * successful; JNI_FALSE otherwise.
 305  */
 306 jboolean
 307 OGLSD_InitOGLWindow(JNIEnv *env, OGLSDOps *oglsdo)
 308 {
 309     PIXELFORMATDESCRIPTOR pfd;
 310     WGLSDOps *wglsdo;
 311     WGLGraphicsConfigInfo *wglInfo;
 312     HWND window;
 313     RECT wbounds;
 314     HDC hdc;
 315 
 316     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_InitOGLWindow");
 317 
 318     if (oglsdo == NULL) {
 319         J2dRlsTraceLn(J2D_TRACE_ERROR,
 320                       "OGLSD_InitOGLWindow: ops are null");
 321         return JNI_FALSE;
 322     }
 323 
 324     wglsdo = (WGLSDOps *)oglsdo->privOps;
 325     if (wglsdo == NULL) {
 326         J2dRlsTraceLn(J2D_TRACE_ERROR,
 327                       "OGLSD_InitOGLWindow: wgl ops are null");
 328         return JNI_FALSE;
 329     }
 330 
 331     wglInfo = wglsdo->configInfo;
 332     if (wglInfo == NULL) {
 333         J2dRlsTraceLn(J2D_TRACE_ERROR,
 334                       "OGLSD_InitOGLWindow: graphics config info is null");
 335         return JNI_FALSE;
 336     }
 337 
 338     window = wglsdo->window;
 339     if (!IsWindow(window)) {
 340         J2dRlsTraceLn(J2D_TRACE_ERROR,
 341                       "OGLSD_InitOGLWindow: disposed component");
 342         return JNI_FALSE;
 343     }
 344 
 345     GetWindowRect(window, &wbounds);
 346 
 347     hdc = GetDC(window);
 348     if (hdc == 0) {
 349         J2dRlsTraceLn(J2D_TRACE_ERROR,
 350                       "OGLSD_InitOGLWindow: invalid hdc");
 351         return JNI_FALSE;
 352     }
 353 
 354     if (!SetPixelFormat(hdc, wglInfo->pixfmt, &pfd)) {
 355         J2dRlsTraceLn(J2D_TRACE_ERROR,
 356                       "OGLSD_InitOGLWindow: error setting pixel format");
 357         ReleaseDC(window, hdc);
 358         return JNI_FALSE;
 359     }
 360 
 361     ReleaseDC(window, hdc);
 362 
 363     oglsdo->drawableType = OGLSD_WINDOW;
 364     oglsdo->isOpaque = JNI_TRUE;
 365     oglsdo->width = wbounds.right - wbounds.left;
 366     oglsdo->height = wbounds.bottom - wbounds.top;
 367     wglsdo->pbufferDC = 0;
 368 
 369     J2dTraceLn2(J2D_TRACE_VERBOSE, "  created window: w=%d h=%d",
 370                 oglsdo->width, oglsdo->height);
 371 
 372     return JNI_TRUE;
 373 }
 374 
 375 void
 376 OGLSD_SwapBuffers(JNIEnv *env, jlong pPeerData)
 377 {
 378     HWND window;
 379     HDC hdc;
 380 
 381     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_SwapBuffers");
 382 
 383     window = AwtComponent_GetHWnd(env, pPeerData);
 384     if (!IsWindow(window)) {
 385         J2dRlsTraceLn(J2D_TRACE_ERROR,
 386                       "OGLSD_SwapBuffers: disposed component");
 387         return;
 388     }
 389 
 390     hdc = GetDC(window);
 391     if (hdc == 0) {
 392         J2dRlsTraceLn(J2D_TRACE_ERROR,
 393                       "OGLSD_SwapBuffers: invalid hdc");
 394         return;
 395     }
 396 
 397     if (!SwapBuffers(hdc)) {
 398         J2dRlsTraceLn(J2D_TRACE_ERROR,
 399                       "OGLSD_SwapBuffers: error in SwapBuffers");
 400     }
 401 
 402     if (!ReleaseDC(window, hdc)) {
 403         J2dRlsTraceLn(J2D_TRACE_ERROR,
 404                       "OGLSD_SwapBuffers: error while releasing dc");
 405     }
 406 }
 407 
 408 // needed by Mac OS X port, no-op on other platforms
 409 void
 410 OGLSD_Flush(JNIEnv *env)
 411 {
 412 }
 413 
 414 /*
 415  * Class:     sun_java2d_opengl_WGLSurfaceData
 416  * Method:    updateWindowAccelImpl
 417  * Signature: (JJII)Z
 418  */
 419 JNIEXPORT jboolean JNICALL
 420     Java_sun_java2d_opengl_WGLSurfaceData_updateWindowAccelImpl
 421   (JNIEnv *env, jclass clazz, jlong pData, jobject peer, jint w, jint h)
 422 {
 423     OGLSDOps *oglsdo = (OGLSDOps *)jlong_to_ptr(pData);
 424     OGLPixelFormat pf = PixelFormats[0/*PF_INT_ARGB_PRE*/];
 425     HBITMAP hBitmap = NULL;
 426     void *pDst;
 427     jint srcx, srcy, dstx, dsty, width, height;
 428     jint pixelStride = 4;
 429     jint scanStride = pixelStride * w;
 430 
 431     J2dTraceLn(J2D_TRACE_INFO, "WGLSurfaceData_updateWindowAccelImpl");
 432 
 433     if (w <= 0 || h <= 0) {
 434         return JNI_TRUE;
 435     }
 436     if (oglsdo == NULL) {
 437         return JNI_FALSE;
 438     }
 439     RESET_PREVIOUS_OP();
 440 
 441     width = w;
 442     height = h;
 443     srcx = srcy = dstx = dsty = 0;
 444 
 445     pDst = SAFE_SIZE_ARRAY_ALLOC(malloc, height, scanStride);
 446     if (pDst == NULL) {
 447         return JNI_FALSE;
 448     }
 449     ZeroMemory(pDst, height * scanStride);
 450 
 451     // the code below is mostly copied from OGLBlitLoops_SurfaceToSwBlit
 452 
 453     j2d_glPixelStorei(GL_PACK_SKIP_PIXELS, dstx);
 454     j2d_glPixelStorei(GL_PACK_ROW_LENGTH, scanStride / pixelStride);
 455     j2d_glPixelStorei(GL_PACK_ALIGNMENT, pf.alignment);
 456 
 457     // this accounts for lower-left origin of the source region
 458     srcx = oglsdo->xOffset + srcx;
 459     srcy = oglsdo->yOffset + oglsdo->height - (srcy + 1);
 460     // we must read one scanline at a time because there is no way
 461     // to read starting at the top-left corner of the source region
 462     while (height > 0) {
 463         j2d_glPixelStorei(GL_PACK_SKIP_ROWS, dsty);
 464         j2d_glReadPixels(srcx, srcy, width, 1,
 465                          pf.format, pf.type, pDst);
 466         srcy--;
 467         dsty++;
 468         height--;
 469     }
 470 
 471     j2d_glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
 472     j2d_glPixelStorei(GL_PACK_SKIP_ROWS, 0);
 473     j2d_glPixelStorei(GL_PACK_ROW_LENGTH, 0);
 474     j2d_glPixelStorei(GL_PACK_ALIGNMENT, 4);
 475 
 476     // the pixels read from the surface are already premultiplied
 477     hBitmap = BitmapUtil_CreateBitmapFromARGBPre(w, h, scanStride,
 478                                                  (int*)pDst);
 479     free(pDst);
 480 
 481     if (hBitmap == NULL) {
 482         return JNI_FALSE;
 483     }
 484 
 485     AwtWindow_UpdateWindow(env, peer, w, h, hBitmap);
 486 
 487     // hBitmap is released in UpdateWindow
 488 
 489     return JNI_TRUE;
 490 }