aboutsummaryrefslogtreecommitdiff
path: root/src/macosx/native/sun/java2d/opengl/CGLSurfaceData.m
blob: 84c1fb12c5b53aced6804d0232e1a776c2ca45fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

#import <stdlib.h>
#import <JavaNativeFoundation/JavaNativeFoundation.h>

#import "sun_java2d_opengl_CGLSurfaceData.h"

#import "jni.h"
#import "jni_util.h"
#import "OGLRenderQueue.h"
#import "CGLGraphicsConfig.h"
#import "CGLSurfaceData.h"
#import "CGLLayer.h"
#import "ThreadUtilities.h"

/* JDK's glext.h is already included and will prevent the Apple glext.h
 * being included, so define the externs directly
 */
extern void glBindFramebufferEXT(GLenum target, GLuint framebuffer);
extern CGLError CGLTexImageIOSurface2D(
        CGLContextObj ctx, GLenum target, GLenum internal_format,
        GLsizei width, GLsizei height, GLenum format, GLenum type,
        IOSurfaceRef ioSurface, GLuint plane);

/**
 * The methods in this file implement the native windowing system specific
 * layer (CGL) for the OpenGL-based Java 2D pipeline.
 */

#pragma mark -
#pragma mark "--- Mac OS X specific methods for GL pipeline ---"

// TODO: hack that's called from OGLRenderQueue to test out unlockFocus behavior
#if 0
void
OGLSD_UnlockFocus(OGLContext *oglc, OGLSDOps *dstOps)
{
    CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
    CGLSDOps *cglsdo = (CGLSDOps *)dstOps->privOps;
    fprintf(stderr, "about to unlock focus: %p %p\n",
            cglsdo->peerData, ctxinfo->context);

    NSOpenGLView *nsView = cglsdo->peerData;
    if (nsView != NULL) {
JNF_COCOA_ENTER(env);
        [nsView unlockFocus];
JNF_COCOA_EXIT(env);
    }
}
#endif

/**
 * Makes the given context current to its associated "scratch" surface.  If
 * the operation is successful, this method will return JNI_TRUE; otherwise,
 * returns JNI_FALSE.
 */
static jboolean
CGLSD_MakeCurrentToScratch(JNIEnv *env, OGLContext *oglc)
{
    J2dTraceLn(J2D_TRACE_INFO, "CGLSD_MakeCurrentToScratch");

    if (oglc == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR,
                      "CGLSD_MakeCurrentToScratch: context is null");
        return JNI_FALSE;
    }

JNF_COCOA_ENTER(env);

    CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
#if USE_NSVIEW_FOR_SCRATCH
    [ctxinfo->context makeCurrentContext];
    [ctxinfo->context setView: ctxinfo->scratchSurface];
#else
    [ctxinfo->context clearDrawable];
    [ctxinfo->context makeCurrentContext];
    [ctxinfo->context setPixelBuffer: ctxinfo->scratchSurface
            cubeMapFace: 0
            mipMapLevel: 0
            currentVirtualScreen: [ctxinfo->context currentVirtualScreen]];
#endif

JNF_COCOA_EXIT(env);

    return JNI_TRUE;
}

/**
 * This function disposes of any native windowing system resources associated
 * with this surface.  For instance, if the given OGLSDOps is of type
 * OGLSD_PBUFFER, this method implementation will destroy the actual pbuffer
 * surface.
 */
void
OGLSD_DestroyOGLSurface(JNIEnv *env, OGLSDOps *oglsdo)
{
    J2dTraceLn(J2D_TRACE_INFO, "OGLSD_DestroyOGLSurface");

JNF_COCOA_ENTER(env);

    CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
    if (oglsdo->drawableType == OGLSD_PBUFFER) {
        if (oglsdo->textureID != 0) {
            j2d_glDeleteTextures(1, &oglsdo->textureID);
            oglsdo->textureID = 0;
        }
        if (cglsdo->pbuffer != NULL) {
            [cglsdo->pbuffer release];
            cglsdo->pbuffer = NULL;
        }
    } else if (oglsdo->drawableType == OGLSD_WINDOW) {
        // detach the NSView from the NSOpenGLContext
        CGLGraphicsConfigInfo *cglInfo = cglsdo->configInfo;
        OGLContext *oglc = cglInfo->context;
        CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
        [ctxinfo->context clearDrawable];
    }

    oglsdo->drawableType = OGLSD_UNDEFINED;

JNF_COCOA_EXIT(env);
}

/**
 * Returns a pointer (as a jlong) to the native CGLGraphicsConfigInfo
 * associated with the given OGLSDOps.  This method can be called from
 * shared code to retrieve the native GraphicsConfig data in a platform-
 * independent manner.
 */
jlong
OGLSD_GetNativeConfigInfo(OGLSDOps *oglsdo)
{
    J2dTraceLn(J2D_TRACE_INFO, "OGLSD_GetNativeConfigInfo");

    if (oglsdo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_GetNativeConfigInfo: ops are null");
        return 0L;
    }

    CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
    if (cglsdo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_GetNativeConfigInfo: cgl ops are null");
        return 0L;
    }

    return ptr_to_jlong(cglsdo->configInfo);
}

/**
 * Makes the given GraphicsConfig's context current to its associated
 * "scratch" surface.  If there is a problem making the context current,
 * this method will return NULL; otherwise, returns a pointer to the
 * OGLContext that is associated with the given GraphicsConfig.
 */
OGLContext *
OGLSD_SetScratchSurface(JNIEnv *env, jlong pConfigInfo)
{
    J2dTraceLn(J2D_TRACE_INFO, "OGLSD_SetScratchContext");

    CGLGraphicsConfigInfo *cglInfo = (CGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
    if (cglInfo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_SetScratchContext: cgl config info is null");
        return NULL;
    }

    OGLContext *oglc = cglInfo->context;
    CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;

JNF_COCOA_ENTER(env);

    // avoid changing the context's target view whenever possible, since
    // calling setView causes flickering; as long as our context is current
    // to some view, it's not necessary to switch to the scratch surface
    if ([ctxinfo->context view] == nil) {
        // it seems to be necessary to explicitly flush between context changes
        OGLContext *currentContext = OGLRenderQueue_GetCurrentContext();
        if (currentContext != NULL) {
            j2d_glFlush();
        }

        if (!CGLSD_MakeCurrentToScratch(env, oglc)) {
            return NULL;
        }
    // make sure our context is current
    } else if ([NSOpenGLContext currentContext] != ctxinfo->context) {
        [ctxinfo->context makeCurrentContext];
    }

    if (OGLC_IS_CAP_PRESENT(oglc, CAPS_EXT_FBOBJECT)) {
        // the GL_EXT_framebuffer_object extension is present, so this call
        // will ensure that we are bound to the scratch surface (and not
        // some other framebuffer object)
        j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    }

JNF_COCOA_EXIT(env);

    return oglc;
}

/**
 * Makes a context current to the given source and destination
 * surfaces.  If there is a problem making the context current, this method
 * will return NULL; otherwise, returns a pointer to the OGLContext that is
 * associated with the destination surface.
 */
OGLContext *
OGLSD_MakeOGLContextCurrent(JNIEnv *env, OGLSDOps *srcOps, OGLSDOps *dstOps)
{
    J2dTraceLn(J2D_TRACE_INFO, "OGLSD_MakeOGLContextCurrent");

    CGLSDOps *dstCGLOps = (CGLSDOps *)dstOps->privOps;

    J2dTraceLn4(J2D_TRACE_VERBOSE, "  src: %d %p dst: %d %p", srcOps->drawableType, srcOps, dstOps->drawableType, dstOps);

    OGLContext *oglc = dstCGLOps->configInfo->context;
    if (oglc == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_MakeOGLContextCurrent: context is null");
        return NULL;
    }

    CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;

    // it seems to be necessary to explicitly flush between context changes
    OGLContext *currentContext = OGLRenderQueue_GetCurrentContext();
    if (currentContext != NULL) {
        j2d_glFlush();
    }

    if (dstOps->drawableType == OGLSD_FBOBJECT) {
        // first make sure we have a current context (if the context isn't
        // already current to some drawable, we will make it current to
        // its scratch surface)
        if (oglc != currentContext) {
            if (!CGLSD_MakeCurrentToScratch(env, oglc)) {
                return NULL;
            }
        }

        // now bind to the fbobject associated with the destination surface;
        // this means that all rendering will go into the fbobject destination
        // (note that we unbind the currently bound texture first; this is
        // recommended procedure when binding an fbobject)
        j2d_glBindTexture(GL_TEXTURE_2D, 0);
        j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, dstOps->fbobjectID);

        return oglc;
    }

JNF_COCOA_ENTER(env);

    // set the current surface
    if (dstOps->drawableType == OGLSD_PBUFFER) {
        // REMIND: pbuffers are not fully tested yet...
        [ctxinfo->context clearDrawable];
        [ctxinfo->context makeCurrentContext];
        [ctxinfo->context setPixelBuffer: dstCGLOps->pbuffer
                cubeMapFace: 0
                mipMapLevel: 0
                currentVirtualScreen: [ctxinfo->context currentVirtualScreen]];
    } else {
        CGLSDOps *cglsdo = (CGLSDOps *)dstOps->privOps;
        NSView *nsView = (NSView *)cglsdo->peerData;

        if ([ctxinfo->context view] != nsView) {
            [ctxinfo->context makeCurrentContext];
            [ctxinfo->context setView: nsView];
        }
    }

    if (OGLC_IS_CAP_PRESENT(oglc, CAPS_EXT_FBOBJECT)) {
        // the GL_EXT_framebuffer_object extension is present, so we
        // must bind to the default (windowing system provided)
        // framebuffer
        j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    }

    if ((srcOps != dstOps) && (srcOps->drawableType == OGLSD_PBUFFER)) {
        // bind pbuffer to the render texture object (since we are preparing
        // to copy from the pbuffer)
        CGLSDOps *srcCGLOps = (CGLSDOps *)srcOps->privOps;
        j2d_glBindTexture(GL_TEXTURE_2D, srcOps->textureID);
        [ctxinfo->context
                setTextureImageToPixelBuffer: srcCGLOps->pbuffer
                colorBuffer: GL_FRONT];
    }

JNF_COCOA_EXIT(env);

    return oglc;
}

/**
 * This function initializes a native window surface and caches the window
 * bounds in the given OGLSDOps.  Returns JNI_TRUE if the operation was
 * successful; JNI_FALSE otherwise.
 */
jboolean
OGLSD_InitOGLWindow(JNIEnv *env, OGLSDOps *oglsdo)
{
    J2dTraceLn(J2D_TRACE_INFO, "OGLSD_InitOGLWindow");

    if (oglsdo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_InitOGLWindow: ops are null");
        return JNI_FALSE;
    }

    CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
    if (cglsdo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_InitOGLWindow: cgl ops are null");
        return JNI_FALSE;
    }

    AWTView *v = cglsdo->peerData;
    if (v == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_InitOGLWindow: view is invalid");
        return JNI_FALSE;
    }

JNF_COCOA_ENTER(env);
    NSRect surfaceBounds = [v bounds];
    oglsdo->drawableType = OGLSD_WINDOW;
    oglsdo->isOpaque = JNI_TRUE;
    oglsdo->width = surfaceBounds.size.width;
    oglsdo->height = surfaceBounds.size.height;
JNF_COCOA_EXIT(env);

    J2dTraceLn2(J2D_TRACE_VERBOSE, "  created window: w=%d h=%d", oglsdo->width, oglsdo->height);

    return JNI_TRUE;
}

void
OGLSD_SwapBuffers(JNIEnv *env, jlong pPeerData)
{
    J2dTraceLn(J2D_TRACE_INFO, "OGLSD_SwapBuffers");

JNF_COCOA_ENTER(env);
    [[NSOpenGLContext currentContext] flushBuffer];
JNF_COCOA_EXIT(env);
}

void
OGLSD_Flush(JNIEnv *env)
{
    OGLSDOps *dstOps = OGLRenderQueue_GetCurrentDestination();
    if (dstOps != NULL) {
        CGLSDOps *dstCGLOps = (CGLSDOps *)dstOps->privOps;
        CGLLayer *layer = (CGLLayer*)dstCGLOps->layer;
        if (layer != NULL) {
            [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
                AWT_ASSERT_APPKIT_THREAD;
                [layer setNeedsDisplay];

#ifdef REMOTELAYER
                /* If there's a remote layer (being used for testing)
                 * then we want to have that also receive the texture.
                 * First sync. up its dimensions with that of the layer
                 * we have attached to the local window and tell it that
                 * it also needs to copy the texture.
                 */
                if (layer.remoteLayer != nil) {
                    CGLLayer* remoteLayer = layer.remoteLayer;
                    remoteLayer.target = GL_TEXTURE_2D;
                    remoteLayer.textureID = layer.textureID;
                    remoteLayer.textureWidth = layer.textureWidth;
                    remoteLayer.textureHeight = layer.textureHeight;
                    [remoteLayer setNeedsDisplay];
                }
#endif /* REMOTELAYER */
            }];
        }
    }
}

#pragma mark -
#pragma mark "--- CGLSurfaceData methods ---"

extern LockFunc        OGLSD_Lock;
extern GetRasInfoFunc  OGLSD_GetRasInfo;
extern UnlockFunc      OGLSD_Unlock;
extern DisposeFunc     OGLSD_Dispose;

JNIEXPORT void JNICALL
Java_sun_java2d_opengl_CGLSurfaceData_initOps
    (JNIEnv *env, jobject cglsd,
     jlong pConfigInfo, jlong pPeerData, jlong layerPtr,
     jint xoff, jint yoff, jboolean isOpaque)
{
    J2dTraceLn(J2D_TRACE_INFO, "CGLSurfaceData_initOps");
    J2dTraceLn1(J2D_TRACE_INFO, "  pPeerData=%p", jlong_to_ptr(pPeerData));
    J2dTraceLn2(J2D_TRACE_INFO, "  xoff=%d, yoff=%d", (int)xoff, (int)yoff);

    OGLSDOps *oglsdo = (OGLSDOps *)
        SurfaceData_InitOps(env, cglsd, sizeof(OGLSDOps));
    CGLSDOps *cglsdo = (CGLSDOps *)malloc(sizeof(CGLSDOps));
    if (cglsdo == NULL) {
        JNU_ThrowOutOfMemoryError(env, "creating native cgl ops");
        return;
    }

    oglsdo->privOps = cglsdo;

    oglsdo->sdOps.Lock               = OGLSD_Lock;
    oglsdo->sdOps.GetRasInfo         = OGLSD_GetRasInfo;
    oglsdo->sdOps.Unlock             = OGLSD_Unlock;
    oglsdo->sdOps.Dispose            = OGLSD_Dispose;

    oglsdo->drawableType = OGLSD_UNDEFINED;
    oglsdo->activeBuffer = GL_FRONT;
    oglsdo->needsInit = JNI_TRUE;
    oglsdo->xOffset = xoff;
    oglsdo->yOffset = yoff;
    oglsdo->isOpaque = isOpaque;

    cglsdo->peerData = (AWTView *)jlong_to_ptr(pPeerData);
    cglsdo->layer = (CGLLayer *)jlong_to_ptr(layerPtr);
    cglsdo->configInfo = (CGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);

    if (cglsdo->configInfo == NULL) {
        free(cglsdo);
        JNU_ThrowNullPointerException(env, "Config info is null in initOps");
    }
}

JNIEXPORT void JNICALL
Java_sun_java2d_opengl_CGLSurfaceData_clearWindow
(JNIEnv *env, jobject cglsd)
{
    J2dTraceLn(J2D_TRACE_INFO, "CGLSurfaceData_clearWindow");

    OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, cglsd);
    CGLSDOps *cglsdo = (CGLSDOps*) oglsdo->privOps;

    cglsdo->peerData = NULL;
    cglsdo->layer = NULL;
}

JNIEXPORT jboolean JNICALL
Java_sun_java2d_opengl_CGLSurfaceData_initPbuffer
    (JNIEnv *env, jobject cglsd,
     jlong pData, jlong pConfigInfo, jboolean isOpaque,
     jint width, jint height)
{
    J2dTraceLn3(J2D_TRACE_INFO, "CGLSurfaceData_initPbuffer: w=%d h=%d opq=%d", width, height, isOpaque);

    OGLSDOps *oglsdo = (OGLSDOps *)jlong_to_ptr(pData);
    if (oglsdo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: ops are null");
        return JNI_FALSE;
    }

    CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
    if (cglsdo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: cgl ops are null");
        return JNI_FALSE;
    }

    CGLGraphicsConfigInfo *cglInfo = (CGLGraphicsConfigInfo *)
        jlong_to_ptr(pConfigInfo);
    if (cglInfo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: cgl config info is null");
        return JNI_FALSE;
    }

    // find the maximum allowable texture dimensions (this value ultimately
    // determines our maximum pbuffer size)
    int pbMax = 0;
    j2d_glGetIntegerv(GL_MAX_TEXTURE_SIZE, &pbMax);

    int pbWidth = 0;
    int pbHeight = 0;
    if (OGLC_IS_CAP_PRESENT(cglInfo->context, CAPS_TEXNONPOW2)) {
        // use non-power-of-two dimensions directly
        pbWidth = (width <= pbMax) ? width : 0;
        pbHeight = (height <= pbMax) ? height : 0;
    } else {
        // find the appropriate power-of-two dimensions
        pbWidth = OGLSD_NextPowerOfTwo(width, pbMax);
        pbHeight = OGLSD_NextPowerOfTwo(height, pbMax);
    }

    J2dTraceLn3(J2D_TRACE_VERBOSE, "  desired pbuffer dimensions: w=%d h=%d max=%d", pbWidth, pbHeight, pbMax);

    // if either dimension is 0, we cannot allocate a pbuffer/texture with the
    // requested dimensions
    if (pbWidth == 0 || pbHeight == 0) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: dimensions too large");
        return JNI_FALSE;
    }

    int format = isOpaque ? GL_RGB : GL_RGBA;

JNF_COCOA_ENTER(env);

    cglsdo->pbuffer =
        [[NSOpenGLPixelBuffer alloc]
            initWithTextureTarget: GL_TEXTURE_2D
            textureInternalFormat: format
            textureMaxMipMapLevel: 0
            pixelsWide: pbWidth
            pixelsHigh: pbHeight];
    if (cglsdo->pbuffer == nil) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: could not create pbuffer");
        return JNI_FALSE;
    }

    // make sure the actual dimensions match those that we requested
    GLsizei actualWidth  = [cglsdo->pbuffer pixelsWide];
    GLsizei actualHeight = [cglsdo->pbuffer pixelsHigh];
    if (actualWidth != pbWidth || actualHeight != pbHeight) {
        J2dRlsTraceLn2(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: actual (w=%d h=%d) != requested", actualWidth, actualHeight);
        [cglsdo->pbuffer release];
        return JNI_FALSE;
    }

    GLuint texID = 0;
    j2d_glGenTextures(1, &texID);
    j2d_glBindTexture(GL_TEXTURE_2D, texID);

    oglsdo->drawableType = OGLSD_PBUFFER;
    oglsdo->isOpaque = isOpaque;
    oglsdo->width = width;
    oglsdo->height = height;
    oglsdo->textureID = texID;
    oglsdo->textureWidth = pbWidth;
    oglsdo->textureHeight = pbHeight;
    oglsdo->activeBuffer = GL_FRONT;
    oglsdo->needsInit = JNI_TRUE;

    OGLSD_INIT_TEXTURE_FILTER(oglsdo, GL_NEAREST);

JNF_COCOA_EXIT(env);

    return JNI_TRUE;
}

#pragma mark -
#pragma mark "--- CGLSurfaceData methods - Mac OS X specific ---"

// Must be called on the QFT...
JNIEXPORT void JNICALL
Java_sun_java2d_opengl_CGLSurfaceData_validate
    (JNIEnv *env, jobject jsurfacedata,
     jint xoff, jint yoff, jint width, jint height, jboolean isOpaque)
{
    J2dTraceLn2(J2D_TRACE_INFO, "CGLSurfaceData_validate: w=%d h=%d", width, height);

    OGLSDOps *oglsdo = (OGLSDOps*)SurfaceData_GetOps(env, jsurfacedata);
    oglsdo->needsInit = JNI_TRUE;
    oglsdo->xOffset = xoff;
    oglsdo->yOffset = yoff;

    oglsdo->width = width;
    oglsdo->height = height;
    oglsdo->isOpaque = isOpaque;

    if (oglsdo->drawableType == OGLSD_WINDOW) {
        OGLContext_SetSurfaces(env, ptr_to_jlong(oglsdo), ptr_to_jlong(oglsdo));

        // we have to explicitly tell the NSOpenGLContext that its target
        // drawable has changed size
        CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
        OGLContext *oglc = cglsdo->configInfo->context;
        CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;

JNF_COCOA_ENTER(env);
        [ctxinfo->context update];
JNF_COCOA_EXIT(env);
    }
}