4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A#import <stdlib.h>
4632N/A#import <JavaNativeFoundation/JavaNativeFoundation.h>
4632N/A
4632N/A#import "sun_java2d_opengl_CGLSurfaceData.h"
4632N/A
4632N/A#import "jni.h"
4632N/A#import "jni_util.h"
4632N/A#import "OGLRenderQueue.h"
4632N/A#import "CGLGraphicsConfig.h"
4632N/A#import "CGLSurfaceData.h"
4632N/A#import "CGLLayer.h"
4632N/A#import "ThreadUtilities.h"
4632N/A
4632N/A/* JDK's glext.h is already included and will prevent the Apple glext.h
4632N/A * being included, so define the externs directly
4632N/A */
4632N/Aextern void glBindFramebufferEXT(GLenum target, GLuint framebuffer);
4632N/Aextern CGLError CGLTexImageIOSurface2D(
4632N/A CGLContextObj ctx, GLenum target, GLenum internal_format,
4632N/A GLsizei width, GLsizei height, GLenum format, GLenum type,
4632N/A IOSurfaceRef ioSurface, GLuint plane);
4632N/A
4632N/A/**
4632N/A * The methods in this file implement the native windowing system specific
4632N/A * layer (CGL) for the OpenGL-based Java 2D pipeline.
4632N/A */
4632N/A
4632N/A#pragma mark -
4632N/A#pragma mark "--- Mac OS X specific methods for GL pipeline ---"
4632N/A
4632N/A// TODO: hack that's called from OGLRenderQueue to test out unlockFocus behavior
4632N/A#if 0
4632N/Avoid
4632N/AOGLSD_UnlockFocus(OGLContext *oglc, OGLSDOps *dstOps)
4632N/A{
4632N/A CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
4632N/A CGLSDOps *cglsdo = (CGLSDOps *)dstOps->privOps;
4632N/A fprintf(stderr, "about to unlock focus: %p %p\n",
4632N/A cglsdo->peerData, ctxinfo->context);
4632N/A
4632N/A NSOpenGLView *nsView = cglsdo->peerData;
4632N/A if (nsView != NULL) {
4632N/AJNF_COCOA_ENTER(env);
4632N/A [nsView unlockFocus];
4632N/AJNF_COCOA_EXIT(env);
4632N/A }
4632N/A}
4632N/A#endif
4632N/A
4632N/A/**
4632N/A * Makes the given context current to its associated "scratch" surface. If
4632N/A * the operation is successful, this method will return JNI_TRUE; otherwise,
4632N/A * returns JNI_FALSE.
4632N/A */
4632N/Astatic jboolean
4632N/ACGLSD_MakeCurrentToScratch(JNIEnv *env, OGLContext *oglc)
4632N/A{
4632N/A J2dTraceLn(J2D_TRACE_INFO, "CGLSD_MakeCurrentToScratch");
4632N/A
4632N/A if (oglc == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR,
4632N/A "CGLSD_MakeCurrentToScratch: context is null");
4632N/A return JNI_FALSE;
4632N/A }
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A
4632N/A CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
4632N/A#if USE_NSVIEW_FOR_SCRATCH
4632N/A [ctxinfo->context makeCurrentContext];
4632N/A [ctxinfo->context setView: ctxinfo->scratchSurface];
4632N/A#else
4632N/A [ctxinfo->context clearDrawable];
4632N/A [ctxinfo->context makeCurrentContext];
4632N/A [ctxinfo->context setPixelBuffer: ctxinfo->scratchSurface
4632N/A cubeMapFace: 0
4632N/A mipMapLevel: 0
4632N/A currentVirtualScreen: [ctxinfo->context currentVirtualScreen]];
4632N/A#endif
4632N/A
4632N/AJNF_COCOA_EXIT(env);
4632N/A
4632N/A return JNI_TRUE;
4632N/A}
4632N/A
4632N/A/**
4632N/A * This function disposes of any native windowing system resources associated
4632N/A * with this surface. For instance, if the given OGLSDOps is of type
4632N/A * OGLSD_PBUFFER, this method implementation will destroy the actual pbuffer
4632N/A * surface.
4632N/A */
4632N/Avoid
4632N/AOGLSD_DestroyOGLSurface(JNIEnv *env, OGLSDOps *oglsdo)
4632N/A{
4632N/A J2dTraceLn(J2D_TRACE_INFO, "OGLSD_DestroyOGLSurface");
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A
4632N/A CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
4632N/A if (oglsdo->drawableType == OGLSD_PBUFFER) {
4632N/A if (oglsdo->textureID != 0) {
4632N/A j2d_glDeleteTextures(1, &oglsdo->textureID);
4632N/A oglsdo->textureID = 0;
4632N/A }
4632N/A if (cglsdo->pbuffer != NULL) {
4632N/A [cglsdo->pbuffer release];
4632N/A cglsdo->pbuffer = NULL;
4632N/A }
4632N/A } else if (oglsdo->drawableType == OGLSD_WINDOW) {
4632N/A // detach the NSView from the NSOpenGLContext
4632N/A CGLGraphicsConfigInfo *cglInfo = cglsdo->configInfo;
4632N/A OGLContext *oglc = cglInfo->context;
4632N/A CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
4632N/A [ctxinfo->context clearDrawable];
4632N/A }
4632N/A
4632N/A oglsdo->drawableType = OGLSD_UNDEFINED;
4632N/A
4632N/AJNF_COCOA_EXIT(env);
4632N/A}
4632N/A
4632N/A/**
4632N/A * Returns a pointer (as a jlong) to the native CGLGraphicsConfigInfo
4632N/A * associated with the given OGLSDOps. This method can be called from
4632N/A * shared code to retrieve the native GraphicsConfig data in a platform-
4632N/A * independent manner.
4632N/A */
4632N/Ajlong
4632N/AOGLSD_GetNativeConfigInfo(OGLSDOps *oglsdo)
4632N/A{
4632N/A J2dTraceLn(J2D_TRACE_INFO, "OGLSD_GetNativeConfigInfo");
4632N/A
4632N/A if (oglsdo == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_GetNativeConfigInfo: ops are null");
4632N/A return 0L;
4632N/A }
4632N/A
4632N/A CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
4632N/A if (cglsdo == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_GetNativeConfigInfo: cgl ops are null");
4632N/A return 0L;
4632N/A }
4632N/A
4632N/A return ptr_to_jlong(cglsdo->configInfo);
4632N/A}
4632N/A
4632N/A/**
4632N/A * Makes the given GraphicsConfig's context current to its associated
4632N/A * "scratch" surface. If there is a problem making the context current,
4632N/A * this method will return NULL; otherwise, returns a pointer to the
4632N/A * OGLContext that is associated with the given GraphicsConfig.
4632N/A */
4632N/AOGLContext *
4632N/AOGLSD_SetScratchSurface(JNIEnv *env, jlong pConfigInfo)
4632N/A{
4632N/A J2dTraceLn(J2D_TRACE_INFO, "OGLSD_SetScratchContext");
4632N/A
4632N/A CGLGraphicsConfigInfo *cglInfo = (CGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
4632N/A if (cglInfo == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_SetScratchContext: cgl config info is null");
4632N/A return NULL;
4632N/A }
4632N/A
4632N/A OGLContext *oglc = cglInfo->context;
4632N/A CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A
4632N/A // avoid changing the context's target view whenever possible, since
4632N/A // calling setView causes flickering; as long as our context is current
4632N/A // to some view, it's not necessary to switch to the scratch surface
4632N/A if ([ctxinfo->context view] == nil) {
4632N/A // it seems to be necessary to explicitly flush between context changes
4632N/A OGLContext *currentContext = OGLRenderQueue_GetCurrentContext();
4632N/A if (currentContext != NULL) {
4632N/A j2d_glFlush();
4632N/A }
4632N/A
4632N/A if (!CGLSD_MakeCurrentToScratch(env, oglc)) {
4632N/A return NULL;
4632N/A }
5040N/A // make sure our context is current
5040N/A } else if ([NSOpenGLContext currentContext] != ctxinfo->context) {
4632N/A [ctxinfo->context makeCurrentContext];
4632N/A }
4632N/A
4632N/A if (OGLC_IS_CAP_PRESENT(oglc, CAPS_EXT_FBOBJECT)) {
4632N/A // the GL_EXT_framebuffer_object extension is present, so this call
4632N/A // will ensure that we are bound to the scratch surface (and not
4632N/A // some other framebuffer object)
4632N/A j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
4632N/A }
4632N/A
4632N/AJNF_COCOA_EXIT(env);
4632N/A
4632N/A return oglc;
4632N/A}
4632N/A
4632N/A/**
4632N/A * Makes a context current to the given source and destination
4632N/A * surfaces. If there is a problem making the context current, this method
4632N/A * will return NULL; otherwise, returns a pointer to the OGLContext that is
4632N/A * associated with the destination surface.
4632N/A */
4632N/AOGLContext *
4632N/AOGLSD_MakeOGLContextCurrent(JNIEnv *env, OGLSDOps *srcOps, OGLSDOps *dstOps)
4632N/A{
4632N/A J2dTraceLn(J2D_TRACE_INFO, "OGLSD_MakeOGLContextCurrent");
4632N/A
4632N/A CGLSDOps *dstCGLOps = (CGLSDOps *)dstOps->privOps;
4632N/A
4632N/A J2dTraceLn4(J2D_TRACE_VERBOSE, " src: %d %p dst: %d %p", srcOps->drawableType, srcOps, dstOps->drawableType, dstOps);
4632N/A
4632N/A OGLContext *oglc = dstCGLOps->configInfo->context;
4632N/A if (oglc == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_MakeOGLContextCurrent: context is null");
4632N/A return NULL;
4632N/A }
4632N/A
4632N/A CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
4632N/A
4632N/A // it seems to be necessary to explicitly flush between context changes
4632N/A OGLContext *currentContext = OGLRenderQueue_GetCurrentContext();
4632N/A if (currentContext != NULL) {
4632N/A j2d_glFlush();
4632N/A }
4632N/A
4632N/A if (dstOps->drawableType == OGLSD_FBOBJECT) {
4632N/A // first make sure we have a current context (if the context isn't
4632N/A // already current to some drawable, we will make it current to
4632N/A // its scratch surface)
4632N/A if (oglc != currentContext) {
4632N/A if (!CGLSD_MakeCurrentToScratch(env, oglc)) {
4632N/A return NULL;
4632N/A }
4632N/A }
4632N/A
4632N/A // now bind to the fbobject associated with the destination surface;
4632N/A // this means that all rendering will go into the fbobject destination
4632N/A // (note that we unbind the currently bound texture first; this is
4632N/A // recommended procedure when binding an fbobject)
4632N/A j2d_glBindTexture(GL_TEXTURE_2D, 0);
4632N/A j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, dstOps->fbobjectID);
4632N/A
4632N/A return oglc;
4632N/A }
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A
4632N/A // set the current surface
4632N/A if (dstOps->drawableType == OGLSD_PBUFFER) {
4632N/A // REMIND: pbuffers are not fully tested yet...
4632N/A [ctxinfo->context clearDrawable];
4632N/A [ctxinfo->context makeCurrentContext];
4632N/A [ctxinfo->context setPixelBuffer: dstCGLOps->pbuffer
4632N/A cubeMapFace: 0
4632N/A mipMapLevel: 0
4632N/A currentVirtualScreen: [ctxinfo->context currentVirtualScreen]];
4632N/A } else {
4632N/A CGLSDOps *cglsdo = (CGLSDOps *)dstOps->privOps;
4632N/A NSView *nsView = (NSView *)cglsdo->peerData;
4632N/A
4632N/A if ([ctxinfo->context view] != nsView) {
4632N/A [ctxinfo->context makeCurrentContext];
4632N/A [ctxinfo->context setView: nsView];
4632N/A }
4632N/A }
4632N/A
4632N/A if (OGLC_IS_CAP_PRESENT(oglc, CAPS_EXT_FBOBJECT)) {
4632N/A // the GL_EXT_framebuffer_object extension is present, so we
4632N/A // must bind to the default (windowing system provided)
4632N/A // framebuffer
4632N/A j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
4632N/A }
4632N/A
4632N/A if ((srcOps != dstOps) && (srcOps->drawableType == OGLSD_PBUFFER)) {
4632N/A // bind pbuffer to the render texture object (since we are preparing
4632N/A // to copy from the pbuffer)
4632N/A CGLSDOps *srcCGLOps = (CGLSDOps *)srcOps->privOps;
4632N/A j2d_glBindTexture(GL_TEXTURE_2D, srcOps->textureID);
4632N/A [ctxinfo->context
4632N/A setTextureImageToPixelBuffer: srcCGLOps->pbuffer
4632N/A colorBuffer: GL_FRONT];
4632N/A }
4632N/A
4632N/AJNF_COCOA_EXIT(env);
4632N/A
4632N/A return oglc;
4632N/A}
4632N/A
4632N/A/**
4632N/A * This function initializes a native window surface and caches the window
4632N/A * bounds in the given OGLSDOps. Returns JNI_TRUE if the operation was
4632N/A * successful; JNI_FALSE otherwise.
4632N/A */
4632N/Ajboolean
4632N/AOGLSD_InitOGLWindow(JNIEnv *env, OGLSDOps *oglsdo)
4632N/A{
4632N/A J2dTraceLn(J2D_TRACE_INFO, "OGLSD_InitOGLWindow");
4632N/A
4632N/A if (oglsdo == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_InitOGLWindow: ops are null");
4632N/A return JNI_FALSE;
4632N/A }
4632N/A
4632N/A CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
4632N/A if (cglsdo == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_InitOGLWindow: cgl ops are null");
4632N/A return JNI_FALSE;
4632N/A }
4632N/A
4632N/A AWTView *v = cglsdo->peerData;
4632N/A if (v == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "OGLSD_InitOGLWindow: view is invalid");
4632N/A return JNI_FALSE;
4632N/A }
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A NSRect surfaceBounds = [v bounds];
4632N/A oglsdo->drawableType = OGLSD_WINDOW;
4632N/A oglsdo->isOpaque = JNI_TRUE;
4632N/A oglsdo->width = surfaceBounds.size.width;
4632N/A oglsdo->height = surfaceBounds.size.height;
4632N/AJNF_COCOA_EXIT(env);
4632N/A
4632N/A J2dTraceLn2(J2D_TRACE_VERBOSE, " created window: w=%d h=%d", oglsdo->width, oglsdo->height);
4730N/A
4721N/A return JNI_TRUE;
4632N/A}
4632N/A
4632N/Avoid
4632N/AOGLSD_SwapBuffers(JNIEnv *env, jlong pPeerData)
4632N/A{
4632N/A J2dTraceLn(J2D_TRACE_INFO, "OGLSD_SwapBuffers");
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A [[NSOpenGLContext currentContext] flushBuffer];
4632N/AJNF_COCOA_EXIT(env);
4632N/A}
4632N/A
4632N/Avoid
4632N/AOGLSD_Flush(JNIEnv *env)
4632N/A{
4639N/A OGLSDOps *dstOps = OGLRenderQueue_GetCurrentDestination();
4639N/A if (dstOps != NULL) {
4639N/A CGLSDOps *dstCGLOps = (CGLSDOps *)dstOps->privOps;
4639N/A CGLLayer *layer = (CGLLayer*)dstCGLOps->layer;
4639N/A if (layer != NULL) {
4639N/A [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
4639N/A AWT_ASSERT_APPKIT_THREAD;
4639N/A [layer setNeedsDisplay];
4721N/A
4721N/A#ifdef REMOTELAYER
4721N/A /* If there's a remote layer (being used for testing)
4721N/A * then we want to have that also receive the texture.
4721N/A * First sync. up its dimensions with that of the layer
4721N/A * we have attached to the local window and tell it that
4721N/A * it also needs to copy the texture.
4721N/A */
4721N/A if (layer.remoteLayer != nil) {
4721N/A CGLLayer* remoteLayer = layer.remoteLayer;
4721N/A remoteLayer.target = GL_TEXTURE_2D;
4721N/A remoteLayer.textureID = layer.textureID;
4721N/A remoteLayer.textureWidth = layer.textureWidth;
4721N/A remoteLayer.textureHeight = layer.textureHeight;
4721N/A [remoteLayer setNeedsDisplay];
4721N/A }
4730N/A#endif /* REMOTELAYER */
4639N/A }];
4639N/A }
4639N/A }
4632N/A}
4632N/A
4632N/A#pragma mark -
4632N/A#pragma mark "--- CGLSurfaceData methods ---"
4632N/A
4632N/Aextern LockFunc OGLSD_Lock;
4632N/Aextern GetRasInfoFunc OGLSD_GetRasInfo;
4632N/Aextern UnlockFunc OGLSD_Unlock;
4632N/Aextern DisposeFunc OGLSD_Dispose;
4632N/A
4632N/AJNIEXPORT void JNICALL
4632N/AJava_sun_java2d_opengl_CGLSurfaceData_initOps
4632N/A (JNIEnv *env, jobject cglsd,
4639N/A jlong pConfigInfo, jlong pPeerData, jlong layerPtr,
4639N/A jint xoff, jint yoff, jboolean isOpaque)
4632N/A{
4632N/A J2dTraceLn(J2D_TRACE_INFO, "CGLSurfaceData_initOps");
4632N/A J2dTraceLn1(J2D_TRACE_INFO, " pPeerData=%p", jlong_to_ptr(pPeerData));
4632N/A J2dTraceLn2(J2D_TRACE_INFO, " xoff=%d, yoff=%d", (int)xoff, (int)yoff);
4632N/A
4632N/A OGLSDOps *oglsdo = (OGLSDOps *)
4632N/A SurfaceData_InitOps(env, cglsd, sizeof(OGLSDOps));
4632N/A CGLSDOps *cglsdo = (CGLSDOps *)malloc(sizeof(CGLSDOps));
4632N/A if (cglsdo == NULL) {
4632N/A JNU_ThrowOutOfMemoryError(env, "creating native cgl ops");
4632N/A return;
4632N/A }
4632N/A
4632N/A oglsdo->privOps = cglsdo;
4632N/A
4632N/A oglsdo->sdOps.Lock = OGLSD_Lock;
4632N/A oglsdo->sdOps.GetRasInfo = OGLSD_GetRasInfo;
4632N/A oglsdo->sdOps.Unlock = OGLSD_Unlock;
4632N/A oglsdo->sdOps.Dispose = OGLSD_Dispose;
4632N/A
4632N/A oglsdo->drawableType = OGLSD_UNDEFINED;
4632N/A oglsdo->activeBuffer = GL_FRONT;
4632N/A oglsdo->needsInit = JNI_TRUE;
4632N/A oglsdo->xOffset = xoff;
4632N/A oglsdo->yOffset = yoff;
4639N/A oglsdo->isOpaque = isOpaque;
4632N/A
4632N/A cglsdo->peerData = (AWTView *)jlong_to_ptr(pPeerData);
4730N/A cglsdo->layer = (CGLLayer *)jlong_to_ptr(layerPtr);
4632N/A cglsdo->configInfo = (CGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
4730N/A
4632N/A if (cglsdo->configInfo == NULL) {
4632N/A free(cglsdo);
4632N/A JNU_ThrowNullPointerException(env, "Config info is null in initOps");
4632N/A }
4632N/A}
4632N/A
4632N/AJNIEXPORT void JNICALL
4632N/AJava_sun_java2d_opengl_CGLSurfaceData_clearWindow
4632N/A(JNIEnv *env, jobject cglsd)
4632N/A{
4632N/A J2dTraceLn(J2D_TRACE_INFO, "CGLSurfaceData_clearWindow");
4632N/A
4632N/A OGLSDOps *oglsdo = (OGLSDOps*) SurfaceData_GetOps(env, cglsd);
4632N/A CGLSDOps *cglsdo = (CGLSDOps*) oglsdo->privOps;
4632N/A
4632N/A cglsdo->peerData = NULL;
4639N/A cglsdo->layer = NULL;
4632N/A}
4632N/A
4632N/AJNIEXPORT jboolean JNICALL
4632N/AJava_sun_java2d_opengl_CGLSurfaceData_initPbuffer
4632N/A (JNIEnv *env, jobject cglsd,
4632N/A jlong pData, jlong pConfigInfo, jboolean isOpaque,
4632N/A jint width, jint height)
4632N/A{
4632N/A J2dTraceLn3(J2D_TRACE_INFO, "CGLSurfaceData_initPbuffer: w=%d h=%d opq=%d", width, height, isOpaque);
4632N/A
4632N/A OGLSDOps *oglsdo = (OGLSDOps *)jlong_to_ptr(pData);
4632N/A if (oglsdo == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: ops are null");
4632N/A return JNI_FALSE;
4632N/A }
4632N/A
4632N/A CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
4632N/A if (cglsdo == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: cgl ops are null");
4632N/A return JNI_FALSE;
4632N/A }
4632N/A
4632N/A CGLGraphicsConfigInfo *cglInfo = (CGLGraphicsConfigInfo *)
4632N/A jlong_to_ptr(pConfigInfo);
4632N/A if (cglInfo == NULL) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: cgl config info is null");
4632N/A return JNI_FALSE;
4632N/A }
4632N/A
4632N/A // find the maximum allowable texture dimensions (this value ultimately
4632N/A // determines our maximum pbuffer size)
4632N/A int pbMax = 0;
4632N/A j2d_glGetIntegerv(GL_MAX_TEXTURE_SIZE, &pbMax);
4632N/A
4632N/A int pbWidth = 0;
4632N/A int pbHeight = 0;
4632N/A if (OGLC_IS_CAP_PRESENT(cglInfo->context, CAPS_TEXNONPOW2)) {
4632N/A // use non-power-of-two dimensions directly
4632N/A pbWidth = (width <= pbMax) ? width : 0;
4632N/A pbHeight = (height <= pbMax) ? height : 0;
4632N/A } else {
4632N/A // find the appropriate power-of-two dimensions
4632N/A pbWidth = OGLSD_NextPowerOfTwo(width, pbMax);
4632N/A pbHeight = OGLSD_NextPowerOfTwo(height, pbMax);
4632N/A }
4632N/A
4632N/A J2dTraceLn3(J2D_TRACE_VERBOSE, " desired pbuffer dimensions: w=%d h=%d max=%d", pbWidth, pbHeight, pbMax);
4632N/A
4632N/A // if either dimension is 0, we cannot allocate a pbuffer/texture with the
4632N/A // requested dimensions
4632N/A if (pbWidth == 0 || pbHeight == 0) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: dimensions too large");
4632N/A return JNI_FALSE;
4632N/A }
4632N/A
4632N/A int format = isOpaque ? GL_RGB : GL_RGBA;
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A
4632N/A cglsdo->pbuffer =
4632N/A [[NSOpenGLPixelBuffer alloc]
4632N/A initWithTextureTarget: GL_TEXTURE_2D
4632N/A textureInternalFormat: format
4632N/A textureMaxMipMapLevel: 0
4632N/A pixelsWide: pbWidth
4632N/A pixelsHigh: pbHeight];
4632N/A if (cglsdo->pbuffer == nil) {
4632N/A J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: could not create pbuffer");
4632N/A return JNI_FALSE;
4632N/A }
4632N/A
4632N/A // make sure the actual dimensions match those that we requested
4632N/A GLsizei actualWidth = [cglsdo->pbuffer pixelsWide];
4632N/A GLsizei actualHeight = [cglsdo->pbuffer pixelsHigh];
4632N/A if (actualWidth != pbWidth || actualHeight != pbHeight) {
4632N/A J2dRlsTraceLn2(J2D_TRACE_ERROR, "CGLSurfaceData_initPbuffer: actual (w=%d h=%d) != requested", actualWidth, actualHeight);
4632N/A [cglsdo->pbuffer release];
4632N/A return JNI_FALSE;
4632N/A }
4632N/A
4632N/A GLuint texID = 0;
4632N/A j2d_glGenTextures(1, &texID);
4632N/A j2d_glBindTexture(GL_TEXTURE_2D, texID);
4632N/A
4632N/A oglsdo->drawableType = OGLSD_PBUFFER;
4632N/A oglsdo->isOpaque = isOpaque;
4632N/A oglsdo->width = width;
4632N/A oglsdo->height = height;
4632N/A oglsdo->textureID = texID;
4632N/A oglsdo->textureWidth = pbWidth;
4632N/A oglsdo->textureHeight = pbHeight;
4632N/A oglsdo->activeBuffer = GL_FRONT;
4632N/A oglsdo->needsInit = JNI_TRUE;
4632N/A
4632N/A OGLSD_INIT_TEXTURE_FILTER(oglsdo, GL_NEAREST);
4632N/A
4632N/AJNF_COCOA_EXIT(env);
4632N/A
4632N/A return JNI_TRUE;
4632N/A}
4632N/A
4632N/A#pragma mark -
4632N/A#pragma mark "--- CGLSurfaceData methods - Mac OS X specific ---"
4632N/A
4632N/A// Must be called on the QFT...
4632N/AJNIEXPORT void JNICALL
4639N/AJava_sun_java2d_opengl_CGLSurfaceData_validate
4632N/A (JNIEnv *env, jobject jsurfacedata,
4639N/A jint xoff, jint yoff, jint width, jint height, jboolean isOpaque)
4632N/A{
4639N/A J2dTraceLn2(J2D_TRACE_INFO, "CGLSurfaceData_validate: w=%d h=%d", width, height);
4632N/A
4632N/A OGLSDOps *oglsdo = (OGLSDOps*)SurfaceData_GetOps(env, jsurfacedata);
4632N/A oglsdo->needsInit = JNI_TRUE;
4632N/A oglsdo->xOffset = xoff;
4632N/A oglsdo->yOffset = yoff;
4632N/A
4632N/A oglsdo->width = width;
4632N/A oglsdo->height = height;
4639N/A oglsdo->isOpaque = isOpaque;
4632N/A
4632N/A if (oglsdo->drawableType == OGLSD_WINDOW) {
4632N/A OGLContext_SetSurfaces(env, ptr_to_jlong(oglsdo), ptr_to_jlong(oglsdo));
4632N/A
4632N/A // we have to explicitly tell the NSOpenGLContext that its target
4632N/A // drawable has changed size
4632N/A CGLSDOps *cglsdo = (CGLSDOps *)oglsdo->privOps;
4632N/A OGLContext *oglc = cglsdo->configInfo->context;
4632N/A CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
4632N/A
4632N/AJNF_COCOA_ENTER(env);
4632N/A [ctxinfo->context update];
4632N/AJNF_COCOA_EXIT(env);
4632N/A }
4632N/A}