jdk/src/java.desktop/macosx/native/libawt_lwawt/java2d/opengl/CGLSurfaceData.m
changeset 26751 70bac69b37c9
parent 25859 3317bb8137f4
child 27510 573e93cd3123
equal deleted inserted replaced
26750:d0d6c64a2e2b 26751:70bac69b37c9
       
     1 /*
       
     2  * Copyright (c) 2011, 2012, 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 #import <stdlib.h>
       
    27 #import <JavaNativeFoundation/JavaNativeFoundation.h>
       
    28 
       
    29 #import "sun_java2d_opengl_CGLSurfaceData.h"
       
    30 
       
    31 #import "jni.h"
       
    32 #import "jni_util.h"
       
    33 #import "OGLRenderQueue.h"
       
    34 #import "CGLGraphicsConfig.h"
       
    35 #import "CGLSurfaceData.h"
       
    36 #import "CGLLayer.h"
       
    37 #import "ThreadUtilities.h"
       
    38 
       
    39 /* JDK's glext.h is already included and will prevent the Apple glext.h
       
    40  * being included, so define the externs directly
       
    41  */
       
    42 extern void glBindFramebufferEXT(GLenum target, GLuint framebuffer);
       
    43 extern CGLError CGLTexImageIOSurface2D(
       
    44         CGLContextObj ctx, GLenum target, GLenum internal_format,
       
    45         GLsizei width, GLsizei height, GLenum format, GLenum type,
       
    46         IOSurfaceRef ioSurface, GLuint plane);
       
    47 
       
    48 /**
       
    49  * The methods in this file implement the native windowing system specific
       
    50  * layer (CGL) for the OpenGL-based Java 2D pipeline.
       
    51  */
       
    52 
       
    53 #pragma mark -
       
    54 #pragma mark "--- Mac OS X specific methods for GL pipeline ---"
       
    55 
       
    56 // TODO: hack that's called from OGLRenderQueue to test out unlockFocus behavior
       
    57 #if 0
       
    58 void
       
    59 OGLSD_UnlockFocus(OGLContext *oglc, OGLSDOps *dstOps)
       
    60 {
       
    61     CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
       
    62     CGLSDOps *cglsdo = (CGLSDOps *)dstOps->privOps;
       
    63     fprintf(stderr, "about to unlock focus: %p %p\n",
       
    64             cglsdo->peerData, ctxinfo->context);
       
    65 
       
    66     NSOpenGLView *nsView = cglsdo->peerData;
       
    67     if (nsView != NULL) {
       
    68 JNF_COCOA_ENTER(env);
       
    69         [nsView unlockFocus];
       
    70 JNF_COCOA_EXIT(env);
       
    71     }
       
    72 }
       
    73 #endif
       
    74 
       
    75 /**
       
    76  * Makes the given context current to its associated "scratch" surface.  If
       
    77  * the operation is successful, this method will return JNI_TRUE; otherwise,
       
    78  * returns JNI_FALSE.
       
    79  */
       
    80 static jboolean
       
    81 CGLSD_MakeCurrentToScratch(JNIEnv *env, OGLContext *oglc)
       
    82 {
       
    83     J2dTraceLn(J2D_TRACE_INFO, "CGLSD_MakeCurrentToScratch");
       
    84 
       
    85     if (oglc == NULL) {
       
    86         J2dRlsTraceLn(J2D_TRACE_ERROR,
       
    87                       "CGLSD_MakeCurrentToScratch: context is null");
       
    88         return JNI_FALSE;
       
    89     }
       
    90 
       
    91 JNF_COCOA_ENTER(env);
       
    92 
       
    93     CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
       
    94 #if USE_NSVIEW_FOR_SCRATCH
       
    95     [ctxinfo->context makeCurrentContext];
       
    96     [ctxinfo->context setView: ctxinfo->scratchSurface];
       
    97 #else
       
    98     [ctxinfo->context clearDrawable];
       
    99     [ctxinfo->context makeCurrentContext];
       
   100     [ctxinfo->context setPixelBuffer: ctxinfo->scratchSurface
       
   101             cubeMapFace: 0
       
   102             mipMapLevel: 0
       
   103             currentVirtualScreen: [ctxinfo->context currentVirtualScreen]];
       
   104 #endif
       
   105 
       
   106 JNF_COCOA_EXIT(env);
       
   107 
       
   108     return JNI_TRUE;
       
   109 }
       
   110 
       
   111 /**
       
   112  * This function disposes of any native windowing system resources associated
       
   113  * with this surface.  For instance, if the given OGLSDOps is of type
       
   114  * OGLSD_PBUFFER, this method implementation will destroy the actual pbuffer
       
   115  * surface.
       
   116  */
       
   117 void
       
   118 OGLSD_DestroyOGLSurface(JNIEnv *env, OGLSDOps *oglsdo)
       
   119 {
       
   120     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_DestroyOGLSurface");
       
   121 
       
   122 JNF_COCOA_ENTER(env);
       
   123 
       
   124     CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
       
   125     if (oglsdo->drawableType == OGLSD_PBUFFER) {
       
   126         if (oglsdo->textureID != 0) {
       
   127             j2d_glDeleteTextures(1, &oglsdo->textureID);
       
   128             oglsdo->textureID = 0;
       
   129         }
       
   130         if (cglsdo->pbuffer != NULL) {
       
   131             [cglsdo->pbuffer release];
       
   132             cglsdo->pbuffer = NULL;
       
   133         }
       
   134     } else if (oglsdo->drawableType == OGLSD_WINDOW) {
       
   135         // detach the NSView from the NSOpenGLContext
       
   136         CGLGraphicsConfigInfo *cglInfo = cglsdo->configInfo;
       
   137         OGLContext *oglc = cglInfo->context;
       
   138         CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
       
   139         [ctxinfo->context clearDrawable];
       
   140     }
       
   141 
       
   142     oglsdo->drawableType = OGLSD_UNDEFINED;
       
   143 
       
   144 JNF_COCOA_EXIT(env);
       
   145 }
       
   146 
       
   147 /**
       
   148  * Returns a pointer (as a jlong) to the native CGLGraphicsConfigInfo
       
   149  * associated with the given OGLSDOps.  This method can be called from
       
   150  * shared code to retrieve the native GraphicsConfig data in a platform-
       
   151  * independent manner.
       
   152  */
       
   153 jlong
       
   154 OGLSD_GetNativeConfigInfo(OGLSDOps *oglsdo)
       
   155 {
       
   156     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_GetNativeConfigInfo");
       
   157 
       
   158     if (oglsdo == NULL) {
       
   159         J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_GetNativeConfigInfo: ops are null");
       
   160         return 0L;
       
   161     }
       
   162 
       
   163     CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
       
   164     if (cglsdo == NULL) {
       
   165         J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_GetNativeConfigInfo: cgl ops are null");
       
   166         return 0L;
       
   167     }
       
   168 
       
   169     return ptr_to_jlong(cglsdo->configInfo);
       
   170 }
       
   171 
       
   172 /**
       
   173  * Makes the given GraphicsConfig's context current to its associated
       
   174  * "scratch" surface.  If there is a problem making the context current,
       
   175  * this method will return NULL; otherwise, returns a pointer to the
       
   176  * OGLContext that is associated with the given GraphicsConfig.
       
   177  */
       
   178 OGLContext *
       
   179 OGLSD_SetScratchSurface(JNIEnv *env, jlong pConfigInfo)
       
   180 {
       
   181     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_SetScratchContext");
       
   182 
       
   183     CGLGraphicsConfigInfo *cglInfo = (CGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
       
   184     if (cglInfo == NULL) {
       
   185         J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_SetScratchContext: cgl config info is null");
       
   186         return NULL;
       
   187     }
       
   188 
       
   189     OGLContext *oglc = cglInfo->context;
       
   190     CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
       
   191 
       
   192 JNF_COCOA_ENTER(env);
       
   193 
       
   194     // avoid changing the context's target view whenever possible, since
       
   195     // calling setView causes flickering; as long as our context is current
       
   196     // to some view, it's not necessary to switch to the scratch surface
       
   197     if ([ctxinfo->context view] == nil) {
       
   198         // it seems to be necessary to explicitly flush between context changes
       
   199         OGLContext *currentContext = OGLRenderQueue_GetCurrentContext();
       
   200         if (currentContext != NULL) {
       
   201             j2d_glFlush();
       
   202         }
       
   203 
       
   204         if (!CGLSD_MakeCurrentToScratch(env, oglc)) {
       
   205             return NULL;
       
   206         }
       
   207     // make sure our context is current
       
   208     } else if ([NSOpenGLContext currentContext] != ctxinfo->context) {
       
   209         [ctxinfo->context makeCurrentContext];
       
   210     }
       
   211 
       
   212     if (OGLC_IS_CAP_PRESENT(oglc, CAPS_EXT_FBOBJECT)) {
       
   213         // the GL_EXT_framebuffer_object extension is present, so this call
       
   214         // will ensure that we are bound to the scratch surface (and not
       
   215         // some other framebuffer object)
       
   216         j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
       
   217     }
       
   218 
       
   219 JNF_COCOA_EXIT(env);
       
   220 
       
   221     return oglc;
       
   222 }
       
   223 
       
   224 /**
       
   225  * Makes a context current to the given source and destination
       
   226  * surfaces.  If there is a problem making the context current, this method
       
   227  * will return NULL; otherwise, returns a pointer to the OGLContext that is
       
   228  * associated with the destination surface.
       
   229  */
       
   230 OGLContext *
       
   231 OGLSD_MakeOGLContextCurrent(JNIEnv *env, OGLSDOps *srcOps, OGLSDOps *dstOps)
       
   232 {
       
   233     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_MakeOGLContextCurrent");
       
   234 
       
   235     CGLSDOps *dstCGLOps = (CGLSDOps *)dstOps->privOps;
       
   236 
       
   237     J2dTraceLn4(J2D_TRACE_VERBOSE, "  src: %d %p dst: %d %p", srcOps->drawableType, srcOps, dstOps->drawableType, dstOps);
       
   238 
       
   239     OGLContext *oglc = dstCGLOps->configInfo->context;
       
   240     if (oglc == NULL) {
       
   241         J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_MakeOGLContextCurrent: context is null");
       
   242         return NULL;
       
   243     }
       
   244 
       
   245     CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
       
   246 
       
   247     // it seems to be necessary to explicitly flush between context changes
       
   248     OGLContext *currentContext = OGLRenderQueue_GetCurrentContext();
       
   249     if (currentContext != NULL) {
       
   250         j2d_glFlush();
       
   251     }
       
   252 
       
   253     if (dstOps->drawableType == OGLSD_FBOBJECT) {
       
   254         // first make sure we have a current context (if the context isn't
       
   255         // already current to some drawable, we will make it current to
       
   256         // its scratch surface)
       
   257         if (oglc != currentContext) {
       
   258             if (!CGLSD_MakeCurrentToScratch(env, oglc)) {
       
   259                 return NULL;
       
   260             }
       
   261         }
       
   262 
       
   263         // now bind to the fbobject associated with the destination surface;
       
   264         // this means that all rendering will go into the fbobject destination
       
   265         // (note that we unbind the currently bound texture first; this is
       
   266         // recommended procedure when binding an fbobject)
       
   267         j2d_glBindTexture(GL_TEXTURE_2D, 0);
       
   268         j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, dstOps->fbobjectID);
       
   269 
       
   270         return oglc;
       
   271     }
       
   272 
       
   273 JNF_COCOA_ENTER(env);
       
   274 
       
   275     // set the current surface
       
   276     if (dstOps->drawableType == OGLSD_PBUFFER) {
       
   277         // REMIND: pbuffers are not fully tested yet...
       
   278         [ctxinfo->context clearDrawable];
       
   279         [ctxinfo->context makeCurrentContext];
       
   280         [ctxinfo->context setPixelBuffer: dstCGLOps->pbuffer
       
   281                 cubeMapFace: 0
       
   282                 mipMapLevel: 0
       
   283                 currentVirtualScreen: [ctxinfo->context currentVirtualScreen]];
       
   284     } else {
       
   285         CGLSDOps *cglsdo = (CGLSDOps *)dstOps->privOps;
       
   286         NSView *nsView = (NSView *)cglsdo->peerData;
       
   287 
       
   288         if ([ctxinfo->context view] != nsView) {
       
   289             [ctxinfo->context makeCurrentContext];
       
   290             [ctxinfo->context setView: nsView];
       
   291         }
       
   292     }
       
   293 
       
   294     if (OGLC_IS_CAP_PRESENT(oglc, CAPS_EXT_FBOBJECT)) {
       
   295         // the GL_EXT_framebuffer_object extension is present, so we
       
   296         // must bind to the default (windowing system provided)
       
   297         // framebuffer
       
   298         j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
       
   299     }
       
   300 
       
   301     if ((srcOps != dstOps) && (srcOps->drawableType == OGLSD_PBUFFER)) {
       
   302         // bind pbuffer to the render texture object (since we are preparing
       
   303         // to copy from the pbuffer)
       
   304         CGLSDOps *srcCGLOps = (CGLSDOps *)srcOps->privOps;
       
   305         j2d_glBindTexture(GL_TEXTURE_2D, srcOps->textureID);
       
   306         [ctxinfo->context
       
   307                 setTextureImageToPixelBuffer: srcCGLOps->pbuffer
       
   308                 colorBuffer: GL_FRONT];
       
   309     }
       
   310 
       
   311 JNF_COCOA_EXIT(env);
       
   312 
       
   313     return oglc;
       
   314 }
       
   315 
       
   316 /**
       
   317  * This function initializes a native window surface and caches the window
       
   318  * bounds in the given OGLSDOps.  Returns JNI_TRUE if the operation was
       
   319  * successful; JNI_FALSE otherwise.
       
   320  */
       
   321 jboolean
       
   322 OGLSD_InitOGLWindow(JNIEnv *env, OGLSDOps *oglsdo)
       
   323 {
       
   324     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_InitOGLWindow");
       
   325 
       
   326     if (oglsdo == NULL) {
       
   327         J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_InitOGLWindow: ops are null");
       
   328         return JNI_FALSE;
       
   329     }
       
   330 
       
   331     CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
       
   332     if (cglsdo == NULL) {
       
   333         J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_InitOGLWindow: cgl ops are null");
       
   334         return JNI_FALSE;
       
   335     }
       
   336 
       
   337     AWTView *v = cglsdo->peerData;
       
   338     if (v == NULL) {
       
   339         J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_InitOGLWindow: view is invalid");
       
   340         return JNI_FALSE;
       
   341     }
       
   342 
       
   343 JNF_COCOA_ENTER(env);
       
   344     NSRect surfaceBounds = [v bounds];
       
   345     oglsdo->drawableType = OGLSD_WINDOW;
       
   346     oglsdo->isOpaque = JNI_TRUE;
       
   347     oglsdo->width = surfaceBounds.size.width;
       
   348     oglsdo->height = surfaceBounds.size.height;
       
   349 JNF_COCOA_EXIT(env);
       
   350 
       
   351     J2dTraceLn2(J2D_TRACE_VERBOSE, "  created window: w=%d h=%d", oglsdo->width, oglsdo->height);
       
   352 
       
   353     return JNI_TRUE;
       
   354 }
       
   355 
       
   356 void
       
   357 OGLSD_SwapBuffers(JNIEnv *env, jlong pPeerData)
       
   358 {
       
   359     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_SwapBuffers");
       
   360 
       
   361 JNF_COCOA_ENTER(env);
       
   362     [[NSOpenGLContext currentContext] flushBuffer];
       
   363 JNF_COCOA_EXIT(env);
       
   364 }
       
   365 
       
   366 void
       
   367 OGLSD_Flush(JNIEnv *env)
       
   368 {
       
   369     OGLSDOps *dstOps = OGLRenderQueue_GetCurrentDestination();
       
   370     if (dstOps != NULL) {
       
   371         CGLSDOps *dstCGLOps = (CGLSDOps *)dstOps->privOps;
       
   372         CGLLayer *layer = (CGLLayer*)dstCGLOps->layer;
       
   373         if (layer != NULL) {
       
   374             [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
       
   375                 AWT_ASSERT_APPKIT_THREAD;
       
   376                 [layer setNeedsDisplay];
       
   377 
       
   378 #ifdef REMOTELAYER
       
   379                 /* If there's a remote layer (being used for testing)
       
   380                  * then we want to have that also receive the texture.
       
   381                  * First sync. up its dimensions with that of the layer
       
   382                  * we have attached to the local window and tell it that
       
   383                  * it also needs to copy the texture.
       
   384                  */
       
   385                 if (layer.remoteLayer != nil) {
       
   386                     CGLLayer* remoteLayer = layer.remoteLayer;
       
   387                     remoteLayer.target = GL_TEXTURE_2D;
       
   388                     remoteLayer.textureID = layer.textureID;
       
   389                     remoteLayer.textureWidth = layer.textureWidth;
       
   390                     remoteLayer.textureHeight = layer.textureHeight;
       
   391                     [remoteLayer setNeedsDisplay];
       
   392                 }
       
   393 #endif /* REMOTELAYER */
       
   394             }];
       
   395         }
       
   396     }
       
   397 }
       
   398 
       
   399 #pragma mark -
       
   400 #pragma mark "--- CGLSurfaceData methods ---"
       
   401 
       
   402 extern LockFunc        OGLSD_Lock;
       
   403 extern GetRasInfoFunc  OGLSD_GetRasInfo;
       
   404 extern UnlockFunc      OGLSD_Unlock;
       
   405 extern DisposeFunc     OGLSD_Dispose;
       
   406 
       
   407 JNIEXPORT void JNICALL
       
   408 Java_sun_java2d_opengl_CGLSurfaceData_initOps
       
   409     (JNIEnv *env, jobject cglsd,
       
   410      jlong pConfigInfo, jlong pPeerData, jlong layerPtr,
       
   411      jint xoff, jint yoff, jboolean isOpaque)
       
   412 {
       
   413     J2dTraceLn(J2D_TRACE_INFO, "CGLSurfaceData_initOps");
       
   414     J2dTraceLn1(J2D_TRACE_INFO, "  pPeerData=%p", jlong_to_ptr(pPeerData));
       
   415     J2dTraceLn2(J2D_TRACE_INFO, "  xoff=%d, yoff=%d", (int)xoff, (int)yoff);
       
   416 
       
   417     OGLSDOps *oglsdo = (OGLSDOps *)
       
   418         SurfaceData_InitOps(env, cglsd, sizeof(OGLSDOps));
       
   419     CGLSDOps *cglsdo = (CGLSDOps *)malloc(sizeof(CGLSDOps));
       
   420     if (cglsdo == NULL) {
       
   421         JNU_ThrowOutOfMemoryError(env, "creating native cgl ops");
       
   422         return;
       
   423     }
       
   424 
       
   425     oglsdo->privOps = cglsdo;
       
   426 
       
   427     oglsdo->sdOps.Lock               = OGLSD_Lock;
       
   428     oglsdo->sdOps.GetRasInfo         = OGLSD_GetRasInfo;
       
   429     oglsdo->sdOps.Unlock             = OGLSD_Unlock;
       
   430     oglsdo->sdOps.Dispose            = OGLSD_Dispose;
       
   431 
       
   432     oglsdo->drawableType = OGLSD_UNDEFINED;
       
   433     oglsdo->activeBuffer = GL_FRONT;
       
   434     oglsdo->needsInit = JNI_TRUE;
       
   435     oglsdo->xOffset = xoff;
       
   436     oglsdo->yOffset = yoff;
       
   437     oglsdo->isOpaque = isOpaque;
       
   438 
       
   439     cglsdo->peerData = (AWTView *)jlong_to_ptr(pPeerData);
       
   440     cglsdo->layer = (CGLLayer *)jlong_to_ptr(layerPtr);
       
   441     cglsdo->configInfo = (CGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
       
   442 
       
   443     if (cglsdo->configInfo == NULL) {
       
   444         free(cglsdo);
       
   445         JNU_ThrowNullPointerException(env, "Config info is null in initOps");
       
   446     }
       
   447 }
       
   448 
       
   449 JNIEXPORT void JNICALL
       
   450 Java_sun_java2d_opengl_CGLSurfaceData_clearWindow
       
   451 (JNIEnv *env, jobject cglsd)
       
   452 {
       
   453     J2dTraceLn(J2D_TRACE_INFO, "CGLSurfaceData_clearWindow");
       
   454 
       
   455     OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, cglsd);
       
   456     CGLSDOps *cglsdo = (CGLSDOps*) oglsdo->privOps;
       
   457 
       
   458     cglsdo->peerData = NULL;
       
   459     cglsdo->layer = NULL;
       
   460 }
       
   461 
       
   462 JNIEXPORT jboolean JNICALL
       
   463 Java_sun_java2d_opengl_CGLSurfaceData_initPbuffer
       
   464     (JNIEnv *env, jobject cglsd,
       
   465      jlong pData, jlong pConfigInfo, jboolean isOpaque,
       
   466      jint width, jint height)
       
   467 {
       
   468     J2dTraceLn3(J2D_TRACE_INFO, "CGLSurfaceData_initPbuffer: w=%d h=%d opq=%d", width, height, isOpaque);
       
   469 
       
   470     OGLSDOps *oglsdo = (OGLSDOps *)jlong_to_ptr(pData);
       
   471     if (oglsdo == NULL) {
       
   472         J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: ops are null");
       
   473         return JNI_FALSE;
       
   474     }
       
   475 
       
   476     CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
       
   477     if (cglsdo == NULL) {
       
   478         J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: cgl ops are null");
       
   479         return JNI_FALSE;
       
   480     }
       
   481 
       
   482     CGLGraphicsConfigInfo *cglInfo = (CGLGraphicsConfigInfo *)
       
   483         jlong_to_ptr(pConfigInfo);
       
   484     if (cglInfo == NULL) {
       
   485         J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: cgl config info is null");
       
   486         return JNI_FALSE;
       
   487     }
       
   488 
       
   489     // find the maximum allowable texture dimensions (this value ultimately
       
   490     // determines our maximum pbuffer size)
       
   491     int pbMax = 0;
       
   492     j2d_glGetIntegerv(GL_MAX_TEXTURE_SIZE, &pbMax);
       
   493 
       
   494     int pbWidth = 0;
       
   495     int pbHeight = 0;
       
   496     if (OGLC_IS_CAP_PRESENT(cglInfo->context, CAPS_TEXNONPOW2)) {
       
   497         // use non-power-of-two dimensions directly
       
   498         pbWidth = (width <= pbMax) ? width : 0;
       
   499         pbHeight = (height <= pbMax) ? height : 0;
       
   500     } else {
       
   501         // find the appropriate power-of-two dimensions
       
   502         pbWidth = OGLSD_NextPowerOfTwo(width, pbMax);
       
   503         pbHeight = OGLSD_NextPowerOfTwo(height, pbMax);
       
   504     }
       
   505 
       
   506     J2dTraceLn3(J2D_TRACE_VERBOSE, "  desired pbuffer dimensions: w=%d h=%d max=%d", pbWidth, pbHeight, pbMax);
       
   507 
       
   508     // if either dimension is 0, we cannot allocate a pbuffer/texture with the
       
   509     // requested dimensions
       
   510     if (pbWidth == 0 || pbHeight == 0) {
       
   511         J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: dimensions too large");
       
   512         return JNI_FALSE;
       
   513     }
       
   514 
       
   515     int format = isOpaque ? GL_RGB : GL_RGBA;
       
   516 
       
   517 JNF_COCOA_ENTER(env);
       
   518 
       
   519     cglsdo->pbuffer =
       
   520         [[NSOpenGLPixelBuffer alloc]
       
   521             initWithTextureTarget: GL_TEXTURE_2D
       
   522             textureInternalFormat: format
       
   523             textureMaxMipMapLevel: 0
       
   524             pixelsWide: pbWidth
       
   525             pixelsHigh: pbHeight];
       
   526     if (cglsdo->pbuffer == nil) {
       
   527         J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: could not create pbuffer");
       
   528         return JNI_FALSE;
       
   529     }
       
   530 
       
   531     // make sure the actual dimensions match those that we requested
       
   532     GLsizei actualWidth  = [cglsdo->pbuffer pixelsWide];
       
   533     GLsizei actualHeight = [cglsdo->pbuffer pixelsHigh];
       
   534     if (actualWidth != pbWidth || actualHeight != pbHeight) {
       
   535         J2dRlsTraceLn2(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: actual (w=%d h=%d) != requested", actualWidth, actualHeight);
       
   536         [cglsdo->pbuffer release];
       
   537         return JNI_FALSE;
       
   538     }
       
   539 
       
   540     GLuint texID = 0;
       
   541     j2d_glGenTextures(1, &texID);
       
   542     j2d_glBindTexture(GL_TEXTURE_2D, texID);
       
   543 
       
   544     oglsdo->drawableType = OGLSD_PBUFFER;
       
   545     oglsdo->isOpaque = isOpaque;
       
   546     oglsdo->width = width;
       
   547     oglsdo->height = height;
       
   548     oglsdo->textureID = texID;
       
   549     oglsdo->textureWidth = pbWidth;
       
   550     oglsdo->textureHeight = pbHeight;
       
   551     oglsdo->activeBuffer = GL_FRONT;
       
   552     oglsdo->needsInit = JNI_TRUE;
       
   553 
       
   554     OGLSD_INIT_TEXTURE_FILTER(oglsdo, GL_NEAREST);
       
   555 
       
   556 JNF_COCOA_EXIT(env);
       
   557 
       
   558     return JNI_TRUE;
       
   559 }
       
   560 
       
   561 #pragma mark -
       
   562 #pragma mark "--- CGLSurfaceData methods - Mac OS X specific ---"
       
   563 
       
   564 // Must be called on the QFT...
       
   565 JNIEXPORT void JNICALL
       
   566 Java_sun_java2d_opengl_CGLSurfaceData_validate
       
   567     (JNIEnv *env, jobject jsurfacedata,
       
   568      jint xoff, jint yoff, jint width, jint height, jboolean isOpaque)
       
   569 {
       
   570     J2dTraceLn2(J2D_TRACE_INFO, "CGLSurfaceData_validate: w=%d h=%d", width, height);
       
   571 
       
   572     OGLSDOps *oglsdo = (OGLSDOps*)SurfaceData_GetOps(env, jsurfacedata);
       
   573     oglsdo->needsInit = JNI_TRUE;
       
   574     oglsdo->xOffset = xoff;
       
   575     oglsdo->yOffset = yoff;
       
   576 
       
   577     oglsdo->width = width;
       
   578     oglsdo->height = height;
       
   579     oglsdo->isOpaque = isOpaque;
       
   580 
       
   581     if (oglsdo->drawableType == OGLSD_WINDOW) {
       
   582         OGLContext_SetSurfaces(env, ptr_to_jlong(oglsdo), ptr_to_jlong(oglsdo));
       
   583 
       
   584         // we have to explicitly tell the NSOpenGLContext that its target
       
   585         // drawable has changed size
       
   586         CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
       
   587         OGLContext *oglc = cglsdo->configInfo->context;
       
   588         CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
       
   589 
       
   590 JNF_COCOA_ENTER(env);
       
   591         [ctxinfo->context update];
       
   592 JNF_COCOA_EXIT(env);
       
   593     }
       
   594 }