//-------------------------------------------------------------------------------------------------// void OSXWindow::swapCGLBuffers(void) { CGLFlushDrawable(mCGLContext); CGLContextObj curCtx = CGLGetCurrentContext(); if(curCtx != mCGLContext) { CGLSetCurrentContext(mCGLContext); #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 CGLSetFullScreenOnDisplay(mCGLContext, CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay)); #else CGLSetFullScreen(mCGLContext); #endif } }
void macosx_opengl_init(void) { CGLPixelFormatObj pixelFormatObj; GLint numPixelFormats; CGLPixelFormatAttribute attribs[] = { kCGLPFAFullScreen, kCGLPFADisplayMask, 0, 0 }; if (macosx_no_opengl) { return; } attribs[2] = CGDisplayIDToOpenGLDisplayMask(displayID); CGLChoosePixelFormat(attribs, &pixelFormatObj, &numPixelFormats); if (pixelFormatObj == NULL) { rfbLog("macosx_opengl_init: CGLChoosePixelFormat failed. Not using OpenGL.\n"); return; } CGLCreateContext(pixelFormatObj, NULL, &glContextObj); CGLDestroyPixelFormat(pixelFormatObj); if (glContextObj == NULL) { rfbLog("macosx_opengl_init: CGLCreateContext failed. Not using OpenGL.\n"); return; } CGLSetCurrentContext(glContextObj); CGLSetFullScreen(glContextObj); macosx_opengl_width = macosx_opengl_get_width(); macosx_opengl_height = macosx_opengl_get_height(); macosx_opengl_bpp = macosx_opengl_get_bpp(); glFinish(); glPixelStorei(GL_PACK_ALIGNMENT, 4); glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_SKIP_ROWS, 0); glPixelStorei(GL_PACK_SKIP_PIXELS, 0); rfbLog("macosx_opengl_init: Using OpenGL for screen capture.\n"); macosx_read_opengl = 1; }
/* Creates and returns a full-screen OpenGL graphics context (to be * released/destroyed by caller). * * To clean up the returned context use destroyFullScreenCGLContext(); */ static CGLContextObj createFullScreenCGLContext(CGOpenGLDisplayMask displayMask) { CGLContextObj glContext = NULL; CGLPixelFormatObj pix; GLint npix; CGLPixelFormatAttribute attribs[4]; attribs[0] = kCGLPFAFullScreen; attribs[1] = kCGLPFADisplayMask; attribs[2] = displayMask; attribs[3] = (CGLPixelFormatAttribute)0; CGLChoosePixelFormat(attribs, &pix, &npix); CGLCreateContext(pix, NULL, &glContext); /* The pixel format is no longer needed, so destroy it. */ CGLDestroyPixelFormat(pix); if (glContext == NULL) return NULL; /* Set our context as the current OpenGL context. */ CGLSetCurrentContext(glContext); /* Set full-screen mode. */ CGLSetFullScreen(glContext); //CGLSetFullScreenOnDisplay(glContext, displayMask); /* Select front buffer as our source for pixel data. */ glReadBuffer(GL_FRONT); /* Finish previous OpenGL commands before continuing. */ glFinish(); if (glGetError() != GL_NO_ERROR) return NULL; return glContext; }
int _glfwPlatformOpenWindow( int width, int height, int redbits, int greenbits, int bluebits, int alphabits, int depthbits, int stencilbits, int mode, _GLFWhints* hints ) { OSStatus error; ProcessSerialNumber psn; unsigned int windowAttributes; // TO DO: Refactor this function! _glfwWin.WindowFunctions = ( _glfwWin.Fullscreen ? &_glfwMacFSWindowFunctions : &_glfwMacDWWindowFunctions ); // Windowed or fullscreen; AGL or CGL? Quite the mess... // AGL appears to be the only choice for attaching OpenGL contexts to // Carbon windows, but it leaves the user no control over fullscreen // mode stretching. Solution: AGL for windowed, CGL for fullscreen. if( !_glfwWin.Fullscreen ) { // create AGL pixel format attribute list GLint AGLpixelFormatAttributes[256]; int numAGLAttrs = 0; AGLpixelFormatAttributes[numAGLAttrs++] = AGL_RGBA; AGLpixelFormatAttributes[numAGLAttrs++] = AGL_DOUBLEBUFFER; if( hints->Stereo ) { AGLpixelFormatAttributes[numAGLAttrs++] = AGL_STEREO; } _setAGLAttribute( AGL_AUX_BUFFERS, hints->AuxBuffers); _setAGLAttribute( AGL_RED_SIZE, redbits ); _setAGLAttribute( AGL_GREEN_SIZE, greenbits ); _setAGLAttribute( AGL_BLUE_SIZE, bluebits ); _setAGLAttribute( AGL_ALPHA_SIZE, alphabits ); _setAGLAttribute( AGL_DEPTH_SIZE, depthbits ); _setAGLAttribute( AGL_STENCIL_SIZE, stencilbits ); _setAGLAttribute( AGL_ACCUM_RED_SIZE, hints->AccumRedBits ); _setAGLAttribute( AGL_ACCUM_GREEN_SIZE, hints->AccumGreenBits ); _setAGLAttribute( AGL_ACCUM_BLUE_SIZE, hints->AccumBlueBits ); _setAGLAttribute( AGL_ACCUM_ALPHA_SIZE, hints->AccumAlphaBits ); if( hints->Samples > 1 ) { _setAGLAttribute( AGL_SAMPLE_BUFFERS_ARB, 1 ); _setAGLAttribute( AGL_SAMPLES_ARB, hints->Samples ); AGLpixelFormatAttributes[numAGLAttrs++] = AGL_NO_RECOVERY; } AGLpixelFormatAttributes[numAGLAttrs++] = AGL_NONE; // create pixel format descriptor AGLDevice mainMonitor = GetMainDevice(); AGLPixelFormat pixelFormat = aglChoosePixelFormat( &mainMonitor, 1, AGLpixelFormatAttributes ); if( pixelFormat == NULL ) { fprintf( stderr, "glfwOpenWindow failing because it can't create a pixel format\n" ); return GL_FALSE; } // store pixel format's values for _glfwPlatformGetWindowParam's use _getAGLAttribute( AGL_ACCELERATED, _glfwWin.Accelerated ); _getAGLAttribute( AGL_RED_SIZE, _glfwWin.RedBits ); _getAGLAttribute( AGL_GREEN_SIZE, _glfwWin.GreenBits ); _getAGLAttribute( AGL_BLUE_SIZE, _glfwWin.BlueBits ); _getAGLAttribute( AGL_ALPHA_SIZE, _glfwWin.AlphaBits ); _getAGLAttribute( AGL_DEPTH_SIZE, _glfwWin.DepthBits ); _getAGLAttribute( AGL_STENCIL_SIZE, _glfwWin.StencilBits ); _getAGLAttribute( AGL_ACCUM_RED_SIZE, _glfwWin.AccumRedBits ); _getAGLAttribute( AGL_ACCUM_GREEN_SIZE, _glfwWin.AccumGreenBits ); _getAGLAttribute( AGL_ACCUM_BLUE_SIZE, _glfwWin.AccumBlueBits ); _getAGLAttribute( AGL_ACCUM_ALPHA_SIZE, _glfwWin.AccumAlphaBits ); _getAGLAttribute( AGL_AUX_BUFFERS, _glfwWin.AuxBuffers ); _getAGLAttribute( AGL_STEREO, _glfwWin.Stereo ); _getAGLAttribute( AGL_SAMPLES_ARB, _glfwWin.Samples ); _glfwWin.RefreshRate = hints->RefreshRate; // create AGL context _glfwWin.AGLContext = aglCreateContext( pixelFormat, NULL ); aglDestroyPixelFormat( pixelFormat ); if( _glfwWin.AGLContext == NULL ) { fprintf( stderr, "glfwOpenWindow failing because it can't create an OpenGL context\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } if (_glfwLibrary.Unbundled) { if( GetCurrentProcess( &psn ) != noErr ) { fprintf( stderr, "glfwOpenWindow failing because it can't get its PSN\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } if( TransformProcessType( &psn, kProcessTransformToForegroundApplication ) != noErr ) { fprintf( stderr, "glfwOpenWindow failing because it can't become a foreground application\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } /* Keith Bauer 2007-07-12 - I don't believe this is desirable if( SetFrontProcess( &psn ) != noErr ) { fprintf( stderr, "glfwOpenWindow failing because it can't become the front process\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } */ } // create window Rect windowContentBounds; windowContentBounds.left = 0; windowContentBounds.top = 0; windowContentBounds.right = width; windowContentBounds.bottom = height; windowAttributes = ( kWindowCloseBoxAttribute \ | kWindowCollapseBoxAttribute \ | kWindowStandardHandlerAttribute ); if( hints->WindowNoResize ) { windowAttributes |= kWindowLiveResizeAttribute; } else { windowAttributes |= ( kWindowFullZoomAttribute | kWindowResizableAttribute ); } error = CreateNewWindow( kDocumentWindowClass, windowAttributes, &windowContentBounds, &( _glfwWin.MacWindow ) ); if( ( error != noErr ) || ( _glfwWin.MacWindow == NULL ) ) { fprintf( stderr, "glfwOpenWindow failing because it can't create a window\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } _glfwWin.WindowUPP = NewEventHandlerUPP( _glfwWindowEventHandler ); error = InstallWindowEventHandler( _glfwWin.MacWindow, _glfwWin.WindowUPP, GetEventTypeCount( GLFW_WINDOW_EVENT_TYPES ), GLFW_WINDOW_EVENT_TYPES, NULL, NULL ); if( error != noErr ) { fprintf( stderr, "glfwOpenWindow failing because it can't install window event handlers\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } // Don't care if we fail here (void)SetWindowTitleWithCFString( _glfwWin.MacWindow, CFSTR( "GLFW Window" ) ); (void)RepositionWindow( _glfwWin.MacWindow, NULL, kWindowCenterOnMainScreen ); if( !aglSetDrawable( _glfwWin.AGLContext, GetWindowPort( _glfwWin.MacWindow ) ) ) { fprintf( stderr, "glfwOpenWindow failing because it can't draw to the window\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } // Make OpenGL context current if( !aglSetCurrentContext( _glfwWin.AGLContext ) ) { fprintf( stderr, "glfwOpenWindow failing because it can't make the OpenGL context current\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } // show window ShowWindow( _glfwWin.MacWindow ); return GL_TRUE; } else { CGDisplayErr cgErr; CGLError cglErr; CFDictionaryRef optimalMode; CGLPixelFormatObj CGLpfObj; long numCGLvs = 0; CGLPixelFormatAttribute CGLpixelFormatAttributes[64]; int numCGLAttrs = 0; // variables for enumerating color depths GLint rgbColorDepth; GLint rgbaAccumDepth = 0; int rgbChannelDepth = 0; // CGL pixel format attributes _setCGLAttribute( kCGLPFADisplayMask, CGDisplayIDToOpenGLDisplayMask( kCGDirectMainDisplay ) ); if( hints->Stereo ) { CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFAStereo; } if( hints->Samples > 1 ) { _setCGLAttribute( kCGLPFASamples, (CGLPixelFormatAttribute)hints->Samples ); _setCGLAttribute( kCGLPFASampleBuffers, (CGLPixelFormatAttribute)1 ); CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFANoRecovery; } CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFAFullScreen; CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFADoubleBuffer; CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFAAccelerated; CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFANoRecovery; CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFAMinimumPolicy; _setCGLAttribute( kCGLPFAAccumSize, (CGLPixelFormatAttribute)( hints->AccumRedBits \ + hints->AccumGreenBits \ + hints->AccumBlueBits \ + hints->AccumAlphaBits ) ); _setCGLAttribute( kCGLPFAAlphaSize, (CGLPixelFormatAttribute)alphabits ); _setCGLAttribute( kCGLPFADepthSize, (CGLPixelFormatAttribute)depthbits ); _setCGLAttribute( kCGLPFAStencilSize, (CGLPixelFormatAttribute)stencilbits ); _setCGLAttribute( kCGLPFAAuxBuffers, (CGLPixelFormatAttribute)hints->AuxBuffers ); CGLpixelFormatAttributes[ numCGLAttrs++ ] = (CGLPixelFormatAttribute)NULL; // create a suitable pixel format with above attributes.. cglErr = CGLChoosePixelFormat( CGLpixelFormatAttributes, &CGLpfObj, &numCGLvs ); if( cglErr != kCGLNoError ) { return GL_FALSE; } // ..and create a rendering context using that pixel format cglErr = CGLCreateContext( CGLpfObj, NULL, &_glfwWin.CGLContext ); if( cglErr != kCGLNoError ) { return GL_FALSE; } // enumerate depth of RGB channels - unlike AGL, CGL works with // a single parameter reflecting the full depth of the frame buffer (void)CGLDescribePixelFormat( CGLpfObj, 0, kCGLPFAColorSize, &rgbColorDepth ); if( rgbColorDepth == 24 || rgbColorDepth == 32 ) { rgbChannelDepth = 8; } if( rgbColorDepth == 16 ) { rgbChannelDepth = 5; } // get pixel depth of accumulator - I haven't got the slightest idea // how this number conforms to any other channel depth than 8 bits, // so this might end up giving completely knackered results... (void)CGLDescribePixelFormat( CGLpfObj, 0, kCGLPFAAccumSize, &rgbaAccumDepth ); if( rgbaAccumDepth == 32 ) { rgbaAccumDepth = 8; } // store values of pixel format for _glfwPlatformGetWindowParam's use _getCGLAttribute( kCGLPFAAccelerated, _glfwWin.Accelerated ); _getCGLAttribute( rgbChannelDepth, _glfwWin.RedBits ); _getCGLAttribute( rgbChannelDepth, _glfwWin.GreenBits ); _getCGLAttribute( rgbChannelDepth, _glfwWin.BlueBits ); _getCGLAttribute( kCGLPFAAlphaSize, _glfwWin.AlphaBits ); _getCGLAttribute( kCGLPFADepthSize, _glfwWin.DepthBits ); _getCGLAttribute( kCGLPFAStencilSize, _glfwWin.StencilBits ); _getCGLAttribute( rgbaAccumDepth, _glfwWin.AccumRedBits ); _getCGLAttribute( rgbaAccumDepth, _glfwWin.AccumGreenBits ); _getCGLAttribute( rgbaAccumDepth, _glfwWin.AccumBlueBits ); _getCGLAttribute( rgbaAccumDepth, _glfwWin.AccumAlphaBits ); _getCGLAttribute( kCGLPFAAuxBuffers, _glfwWin.AuxBuffers ); _getCGLAttribute( kCGLPFAStereo, _glfwWin.Stereo ); _glfwWin.RefreshRate = hints->RefreshRate; // destroy our pixel format (void)CGLDestroyPixelFormat( CGLpfObj ); // capture the display for our application cgErr = CGCaptureAllDisplays(); if( cgErr != kCGErrorSuccess ) { return GL_FALSE; } // find closest matching NON-STRETCHED display mode.. optimalMode = CGDisplayBestModeForParametersAndRefreshRateWithProperty( kCGDirectMainDisplay, rgbColorDepth, width, /* Check further to the right -> */ height, hints->RefreshRate, NULL, NULL ); if( optimalMode == NULL ) { return GL_FALSE; } // ..and switch to that mode cgErr = CGDisplaySwitchToMode( kCGDirectMainDisplay, optimalMode ); if( cgErr != kCGErrorSuccess ) { return GL_FALSE; } // switch to our OpenGL context, and bring it up fullscreen cglErr = CGLSetCurrentContext( _glfwWin.CGLContext ); if( cglErr != kCGLNoError ) { return GL_FALSE; } cglErr = CGLSetFullScreen( _glfwWin.CGLContext ); if( cglErr != kCGLNoError ) { return GL_FALSE; } return GL_TRUE; } }
//-------------------------------------------------------------------------------------------------// void OSXWindow::createCGLFullscreen(unsigned int width, unsigned int height, unsigned int depth, unsigned int fsaa, CGLContextObj sharedContext) { // Find the best match to what was requested boolean_t exactMatch = 0; int reqWidth, reqHeight, reqDepth; #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 // Get a copy of the current display mode CGDisplayModeRef displayMode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay); // Loop through all display modes to determine the closest match. // CGDisplayBestModeForParameters is deprecated on 10.6 so we will emulate it's behavior // Try to find a mode with the requested depth and equal or greater dimensions first. // If no match is found, try to find a mode with greater depth and same or greater dimensions. // If still no match is found, just use the current mode. CFArrayRef allModes = CGDisplayCopyAllDisplayModes(kCGDirectMainDisplay, NULL); for(int i = 0; i < CFArrayGetCount(allModes); i++) { CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(allModes, i); String modeString = StringConverter::toString(CGDisplayModeGetWidth(mode)) + String(" x ") + StringConverter::toString(CGDisplayModeGetHeight(mode)) + String(" @ ") + StringConverter::toString(bitDepthFromDisplayMode(mode)) + "bpp."; LogManager::getSingleton().logMessage(modeString); if(bitDepthFromDisplayMode(mode) != depth) continue; if((CGDisplayModeGetWidth(mode) >= width) && (CGDisplayModeGetHeight(mode) >= height)) { displayMode = mode; exactMatch = 1; break; } } // No depth match was found if(!exactMatch) { for(int i = 0; i < CFArrayGetCount(allModes); i++) { CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(allModes, i); if(bitDepthFromDisplayMode(mode) >= depth) continue; if((CGDisplayModeGetWidth(mode) >= width) && (CGDisplayModeGetHeight(mode) >= height)) { displayMode = mode; exactMatch = 1; break; } } } reqWidth = CGDisplayModeGetWidth(displayMode); reqHeight = CGDisplayModeGetHeight(displayMode); reqDepth = bitDepthFromDisplayMode(displayMode); #else CFDictionaryRef displayMode = CGDisplayBestModeForParameters(kCGDirectMainDisplay, depth, width, height, &exactMatch); const void *value = NULL; value = CFDictionaryGetValue(displayMode, kCGDisplayWidth); CFNumberGetValue((CFNumberRef)value, kCFNumberSInt32Type, &reqWidth); value = CFDictionaryGetValue(displayMode, kCGDisplayHeight); CFNumberGetValue((CFNumberRef)value, kCFNumberSInt32Type, &reqHeight); value = CFDictionaryGetValue(displayMode, kCGDisplayBitsPerPixel); CFNumberGetValue((CFNumberRef)value, kCFNumberSInt32Type, &reqDepth); #endif if(!exactMatch) { // TODO: Report the size difference // That mode is not available, using the closest match String request = StringConverter::toString(width) + String(" x ") + StringConverter::toString(height) + String(" @ ") + StringConverter::toString(depth) + "bpp. "; String received = StringConverter::toString(reqWidth) + String(" x ") + StringConverter::toString(reqHeight) + String(" @ ") + StringConverter::toString(reqDepth) + "bpp. "; LogManager::getSingleton().logMessage(String("RenderSystem Warning: You requested a fullscreen mode of ") + request + String(" This mode is not available and you will receive the closest match. The best display mode for the parameters requested is: ") + received); } // Do the fancy display fading CGDisplayFadeReservationToken reservationToken; CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &reservationToken); CGDisplayFade(reservationToken, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, true); // Grab the main display and save it for later. // You could render to any display, but picking what display // to render to could be interesting. CGDisplayCapture(kCGDirectMainDisplay); // Switch to the correct resolution #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 CGDisplaySetDisplayMode(kCGDirectMainDisplay, displayMode, NULL); #else CGDisplaySwitchToMode(kCGDirectMainDisplay, displayMode); #endif // Get a pixel format that best matches what we are looking for CGLPixelFormatAttribute attribs[] = { kCGLPFADoubleBuffer, kCGLPFAAlphaSize, (CGLPixelFormatAttribute)8, kCGLPFADepthSize, (CGLPixelFormatAttribute)reqDepth, kCGLPFAStencilSize, (CGLPixelFormatAttribute)8, kCGLPFASampleBuffers, (CGLPixelFormatAttribute)0, kCGLPFASamples, (CGLPixelFormatAttribute)0, kCGLPFAFullScreen, kCGLPFASingleRenderer, kCGLPFAAccelerated, kCGLPFADisplayMask, (CGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay), (CGLPixelFormatAttribute)0 }; // Set up FSAA if it was requested if(fsaa > 1) { // turn on kCGLPFASampleBuffers attribs[8] = (CGLPixelFormatAttribute)1; // set the samples for kCGLPFASamples attribs[10] = (CGLPixelFormatAttribute)fsaa; } CGLError err; CGLPixelFormatObj pixelFormatObj; #if (MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_4) GLint numPixelFormats = 0; err = CGLChoosePixelFormat(attribs, &pixelFormatObj, &numPixelFormats); #else long numPixelFormats = 0; err = CGLChoosePixelFormat(attribs, &pixelFormatObj, &numPixelFormats); #endif if(err != 0) { CGReleaseAllDisplays(); OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, String("CGL Error: " + String(CGLErrorString(err))), "OSXWindow::createCGLFullscreen"); } // Create the CGLcontext from our pixel format, share it with the sharedContext passed in err = CGLCreateContext(pixelFormatObj, sharedContext, &mCGLContext); if(err != 0) { CGReleaseAllDisplays(); OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, String("CGL Error: " + String(CGLErrorString(err))), "OSXWindow::createCGLFullscreen"); } // Once we have the context we can destroy the pixel format // In order to share contexts you must keep a pointer to the context object around // Our context class will now manage the life of the pixelFormatObj //CGLDestroyPixelFormat(pixelFormatObj); // Set the context to full screen #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 CGLSetFullScreenOnDisplay(mCGLContext, CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay)); #else CGLSetFullScreen(mCGLContext); #endif // Set the context as current CGLSetCurrentContext(mCGLContext); // This synchronizes CGL with the vertical retrace // Apple docs suggest that OpenGL blocks rendering calls when waiting for // a vertical retrace anyhow. #if (MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_4) GLint swapInterval = 1; CGLSetParameter(mCGLContext, kCGLCPSwapInterval, &swapInterval); #else long swapInterval = 1; CGLSetParameter(mCGLContext, kCGLCPSwapInterval, &swapInterval); #endif // Give a copy of our context to the rendersystem mContext = new OSXCGLContext(mCGLContext, pixelFormatObj); // Let everyone know we are fullscreen now mIsFullScreen = true; // Set some other variables. Just in case we got a different value from CGDisplayBestModeForParameters than we requested mWidth = reqWidth; mHeight = reqHeight; mColourDepth = reqDepth; CGDisplayFade(reservationToken, 2.0, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, false); CGReleaseDisplayFadeReservation(reservationToken); }
screenshot* capture() { CGContextRef bitmap; CGImageRef image; void * data; long bytewidth; GLint width, height; long bytes; CGColorSpaceRef cSpace = CGColorSpaceCreateWithName (kCGColorSpaceGenericRGB); CGLContextObj glContextObj; CGLPixelFormatObj pixelFormatObj ; long numPixelFormats ; CGLPixelFormatAttribute attribs[] = { kCGLPFAFullScreen, kCGLPFADisplayMask, 0, /* Display mask bit goes here */ 0 } ; if ( display == kCGNullDirectDisplay ) display = CGMainDisplayID(); attribs[2] = CGDisplayIDToOpenGLDisplayMask(display); /* Build a full-screen GL context */ CGLChoosePixelFormat( attribs, &pixelFormatObj, &numPixelFormats ); if ( pixelFormatObj == NULL ) // No full screen context support return NULL; CGLCreateContext( pixelFormatObj, NULL, &glContextObj ) ; CGLDestroyPixelFormat( pixelFormatObj ) ; if ( glContextObj == NULL ) return NULL; CGLSetCurrentContext( glContextObj ) ; CGLSetFullScreen( glContextObj ) ; glReadBuffer(GL_FRONT); width = srcRect.size.width; height = srcRect.size.height; bytewidth = width * 4; // Assume 4 bytes/pixel for now bytewidth = (bytewidth + 3) & ~3; // Align to 4 bytes bytes = bytewidth * height; // width * height /* Build bitmap context */ data = malloc(height * bytewidth); if ( data == NULL ) { CGLSetCurrentContext( NULL ); CGLClearDrawable( glContextObj ); // disassociate from full screen CGLDestroyContext( glContextObj ); // and destroy the context return NULL; } bitmap = CGBitmapContextCreate(data, width, height, 8, bytewidth, cSpace, kCGImageAlphaNoneSkipFirst /* XRGB */); CFRelease(cSpace); /* Read framebuffer into our bitmap */ glFinish(); /* Finish all OpenGL commands */ glPixelStorei(GL_PACK_ALIGNMENT, 4); /* Force 4-byte alignment */ glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_SKIP_ROWS, 0); glPixelStorei(GL_PACK_SKIP_PIXELS, 0); /* * Fetch the data in XRGB format, matching the bitmap context. */ glReadPixels( (GLint) srcRect.origin.x, (GLint)srcRect.origin.y, width, height, GL_BGRA, #ifdef __BIG_ENDIAN__ GL_UNSIGNED_INT_8_8_8_8_REV, // for PPC #else GL_UNSIGNED_INT_8_8_8_8, // for Intel! http://lists.apple.com/archives/quartz-dev/2006/May/msg00100.html #endif data); /* * glReadPixels generates a quadrant I raster, with origin in the lower left * This isn't a problem for signal processing routines such as compressors, * as they can simply use a negative 'advance' to move between scanlines. * CGImageRef and CGBitmapContext assume a quadrant III raster, though, so we need to * invert it. Pixel reformatting can also be done here. */ swizzleBitmap(data, bytewidth, height); /* Make an image out of our bitmap; does a cheap vm_copy of the bitmap */ image = CGBitmapContextCreateImage(bitmap); /* Get rid of bitmap */ CFRelease(bitmap); free(data); /* Get rid of GL context */ CGLSetCurrentContext( NULL ); CGLClearDrawable( glContextObj ); // disassociate from full screen CGLDestroyContext( glContextObj ); // and destroy the context /* Returned image has a reference count of 1 */ return image; }
int screen_InitCapture( demux_t *p_demux ) { demux_sys_t *p_sys = p_demux->p_sys; screen_data_t *p_data; CGLError returnedError; unsigned int i; p_sys->p_data = p_data = ( screen_data_t * )calloc( 1, sizeof( screen_data_t ) ); p_data->display_id = kCGDirectMainDisplay; unsigned int displayCount; displayCount = 0; returnedError = CGGetOnlineDisplayList( 0, NULL, &displayCount ); if( !returnedError ) { CGDirectDisplayID *ids; ids = ( CGDirectDisplayID * )malloc( displayCount * sizeof( CGDirectDisplayID ) ); returnedError = CGGetOnlineDisplayList( displayCount, ids, &displayCount ); if( !returnedError ) { if ( p_sys->i_display_id > 0 ) { for( i = 0; i < displayCount; i ++ ) { if( p_sys->i_display_id == ids[i] ) { p_data->display_id = ids[i]; break; } } } else if ( p_sys->i_screen_index > 0 && p_sys->i_screen_index <= displayCount ) { p_data->display_id = ids[p_sys->i_screen_index - 1]; } } free( ids ); } /* CGImage Function * CGDisplayCreateImageForRect is available in Mac OS X v10.6 and later */ p_data->myCGDisplayCreateImageForRect = NULL; CFURLRef frameworkURL = NULL; CFStringRef path = CFSTR( "file://localhost/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework" ); frameworkURL = CFURLCreateWithString( kCFAllocatorDefault, path, NULL ); if( frameworkURL != NULL ) { p_data->bundle = CFBundleCreate( kCFAllocatorDefault, frameworkURL ); if( p_data->bundle != NULL ) { p_data->myCGDisplayCreateImageForRect = ( typeofCGDisplayCreateImageForRect )CFBundleGetFunctionPointerForName ( p_data->bundle, CFSTR( "CGDisplayCreateImageForRect" ) ); } CFRelease( frameworkURL ); } /* Screen Size */ CGRect rect = CGDisplayBounds( p_data->display_id ); p_data->screen_left = rect.origin.x; p_data->screen_top = rect.origin.y; p_data->screen_width = rect.size.width; p_data->screen_height = rect.size.height; p_data->width = p_sys->i_width; p_data->height = p_sys->i_height; if( p_data->width <= 0 || p_data->height <= 0 ) { p_data->width = p_data->screen_width; p_data->height = p_data->screen_height; } /* Screen Context */ if( p_data->myCGDisplayCreateImageForRect == NULL ) { returnedError = screen_CreateContext( &p_data->screen, kCGLPFAFullScreen, kCGLPFADisplayMask, ( CGLPixelFormatAttribute )CGDisplayIDToOpenGLDisplayMask( p_data->display_id ), ( CGLPixelFormatAttribute )0 ); if( returnedError ) goto errorHandling; returnedError = CGLSetCurrentContext( p_data->screen ); if( returnedError ) goto errorHandling; returnedError = CGLSetFullScreen( p_data->screen ); if( returnedError ) goto errorHandling; } /* Clipped Context */ returnedError = screen_CreateContext( &p_data->clipped, kCGLPFAOffScreen, kCGLPFAColorSize, ( CGLPixelFormatAttribute )32, ( CGLPixelFormatAttribute )0 ); if( returnedError ) goto errorHandling; returnedError = CGLSetCurrentContext( p_data->clipped ); if( returnedError ) goto errorHandling; /* Clipped Image */ p_data->clipped_image = ( char * )malloc( p_data->width * p_data->height * 4 ); returnedError = CGLSetOffScreen( p_data->clipped, p_data->width, p_data->height, p_data->width * 4, p_data->clipped_image ); if( returnedError ) goto errorHandling; /* Screen Image */ if( p_data->myCGDisplayCreateImageForRect != NULL ) { p_data->screen_image = ( char * )malloc( p_data->screen_width * p_data->screen_height * 4 ); } else { p_data->screen_image = ( char * )malloc( p_data->width * p_data->height * 4 ); } /* Cursor */ CGSNewConnection( NULL, &( p_data->connection ) ); p_data->cursor_need_update = 1; p_data->cursor_seed = 0; glGenTextures( 1, &( p_data->cursor_texture ) ); glBindTexture( GL_TEXTURE_2D, p_data->cursor_texture ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); /* System */ es_format_Init( &p_sys->fmt, VIDEO_ES, VLC_CODEC_RGB32 ); /* p_sys->fmt.video.i_* must set to screen size, not subscreen size */ p_sys->fmt.video.i_width = p_data->screen_width; p_sys->fmt.video.i_visible_width = p_data->screen_width; p_sys->fmt.video.i_height = p_data->screen_height; p_sys->fmt.video.i_bits_per_pixel = 32; return VLC_SUCCESS; errorHandling: msg_Err( p_demux, "Core OpenGL failure: %s", CGLErrorString( returnedError ) ); return VLC_EGENERIC; }
static int RLXAPI CreateSurface(int numberOfSparePages) { CGOpenGLDisplayMask displayMask = CGDisplayIDToOpenGLDisplayMask( g_cgDisplayID ) ; static CGLPixelFormatAttribute attribs[32]; CGLPixelFormatAttribute *pAttrib = attribs; CGLPixelFormatObj pixelFormatObj ; long numPixelFormats = 0; *pAttrib = kCGLPFAFullScreen; pAttrib++; *pAttrib = kCGLPFASingleRenderer; pAttrib++; *pAttrib = kCGLPFADisplayMask; pAttrib++; *pAttrib = (CGLPixelFormatAttribute)displayMask; pAttrib++; *pAttrib = kCGLPFADoubleBuffer; pAttrib++; *pAttrib = kCGLPFAColorSize; pAttrib++; *pAttrib = (CGLPixelFormatAttribute)gl_bpp; pAttrib++; if ((g_pRLX->pGX->View.Flags & GX_CAPS_MULTISAMPLING)) { *pAttrib = kCGLPFASampleBuffers; pAttrib++; *pAttrib = (CGLPixelFormatAttribute)1; pAttrib++; *pAttrib = kCGLPFASamples; pAttrib++; *pAttrib = (CGLPixelFormatAttribute)g_pRLX->pGX->View.Multisampling; pAttrib++; } if (SYS_CGLTRACE(CGLChoosePixelFormat( attribs, &pixelFormatObj, &numPixelFormats ))) return -1; if (!numPixelFormats) return -2; DEBUG_PIXEL_FORMAT(pixelFormatObj); if (SYS_CGLTRACE(CGLCreateContext( pixelFormatObj, NULL, &g_pCGLC ))) return -3; CGLDestroyPixelFormat( pixelFormatObj ) ; long swapInterval = !!(g_pRLX->pGX->View.Flags & GX_CAPS_VSYNC); SYS_CGLTRACE(CGLSetParameter( g_pCGLC, kCGLCPSwapInterval, &swapInterval)); SYS_CGLTRACE(CGLSetCurrentContext( g_pCGLC )); if (SYS_CGLTRACE(CGLSetFullScreen( g_pCGLC ))) { CGDisplaySwitchToMode(g_cgDisplayID, g_cgPrevDisplayMode); CGReleaseAllDisplays(); return -5; } HideMenuBar(); // Reset engine GL_InstallExtensions(); GL_ResetViewport(); g_pRLX->pGX->Surfaces.maxSurface = numberOfSparePages;; if (g_pRLX->pGX->View.Flags & GX_CAPS_MULTISAMPLING) { glEnable(GL_MULTISAMPLE_ARB); } return 0; }
int _glfwPlatformOpenWindow( int width, int height, const _GLFWwndconfig *wndconfig, const _GLFWfbconfig *fbconfig ) { OSStatus error; unsigned int windowAttributes; ProcessSerialNumber psn; // TODO: Break up this function! _glfwWin.windowUPP = NULL; _glfwWin.mouseUPP = NULL; _glfwWin.keyboardUPP = NULL; _glfwWin.commandUPP = NULL; _glfwWin.window = NULL; _glfwWin.aglContext = NULL; _glfwWin.aglPixelFormat = NULL; _glfwWin.cglContext = NULL; _glfwWin.cglPixelFormat = NULL; _glfwWin.refreshRate = wndconfig->refreshRate; // Fail if OpenGL 3.0 or above was requested if( wndconfig->glMajor > 2 ) { fprintf( stderr, "OpenGL 3.0+ is not yet supported on Mac OS X\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } if( _glfwLibrary.Unbundled ) { if( GetCurrentProcess( &psn ) != noErr ) { fprintf( stderr, "Failed to get the process serial number\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } if( TransformProcessType( &psn, kProcessTransformToForegroundApplication ) != noErr ) { fprintf( stderr, "Failed to become a foreground application\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } if( wndconfig->mode == GLFW_FULLSCREEN ) { if( SetFrontProcess( &psn ) != noErr ) { fprintf( stderr, "Failed to become the front process\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } } } if( !installEventHandlers() ) { fprintf( stderr, "Failed to install Carbon application event handlers\n" ); _glfwPlatformTerminate(); return GL_FALSE; } // Windowed or fullscreen; AGL or CGL? Quite the mess... // AGL appears to be the only choice for attaching OpenGL contexts to // Carbon windows, but it leaves the user no control over fullscreen // mode stretching. Solution: AGL for windowed, CGL for fullscreen. if( wndconfig->mode == GLFW_WINDOW ) { // create AGL pixel format attribute list GLint AGLpixelFormatAttributes[256]; int numAGLAttrs = 0; AGLpixelFormatAttributes[numAGLAttrs++] = AGL_RGBA; AGLpixelFormatAttributes[numAGLAttrs++] = AGL_DOUBLEBUFFER; AGLpixelFormatAttributes[numAGLAttrs++] = AGL_CLOSEST_POLICY; if( fbconfig->stereo ) { AGLpixelFormatAttributes[numAGLAttrs++] = AGL_STEREO; } _setAGLAttribute( AGL_AUX_BUFFERS, fbconfig->auxBuffers); _setAGLAttribute( AGL_RED_SIZE, fbconfig->redBits ); _setAGLAttribute( AGL_GREEN_SIZE, fbconfig->greenBits ); _setAGLAttribute( AGL_BLUE_SIZE, fbconfig->blueBits ); _setAGLAttribute( AGL_ALPHA_SIZE, fbconfig->alphaBits ); _setAGLAttribute( AGL_DEPTH_SIZE, fbconfig->depthBits ); _setAGLAttribute( AGL_STENCIL_SIZE, fbconfig->stencilBits ); _setAGLAttribute( AGL_ACCUM_RED_SIZE, fbconfig->accumRedBits ); _setAGLAttribute( AGL_ACCUM_GREEN_SIZE, fbconfig->accumGreenBits ); _setAGLAttribute( AGL_ACCUM_BLUE_SIZE, fbconfig->accumBlueBits ); _setAGLAttribute( AGL_ACCUM_ALPHA_SIZE, fbconfig->accumAlphaBits ); if( fbconfig->samples > 1 ) { _setAGLAttribute( AGL_SAMPLE_BUFFERS_ARB, 1 ); _setAGLAttribute( AGL_SAMPLES_ARB, fbconfig->samples ); AGLpixelFormatAttributes[numAGLAttrs++] = AGL_NO_RECOVERY; } AGLpixelFormatAttributes[numAGLAttrs++] = AGL_NONE; // create pixel format descriptor AGLDevice mainMonitor = GetMainDevice(); _glfwWin.aglPixelFormat = aglChoosePixelFormat( &mainMonitor, 1, AGLpixelFormatAttributes ); if( _glfwWin.aglPixelFormat == NULL ) { fprintf( stderr, "Failed to choose AGL pixel format: %s\n", aglErrorString( aglGetError() ) ); _glfwPlatformCloseWindow(); return GL_FALSE; } // create AGL context _glfwWin.aglContext = aglCreateContext( _glfwWin.aglPixelFormat, NULL ); if( _glfwWin.aglContext == NULL ) { fprintf( stderr, "Failed to create AGL context: %s\n", aglErrorString( aglGetError() ) ); _glfwPlatformCloseWindow(); return GL_FALSE; } // create window Rect windowContentBounds; windowContentBounds.left = 0; windowContentBounds.top = 0; windowContentBounds.right = width; windowContentBounds.bottom = height; windowAttributes = ( kWindowCloseBoxAttribute | kWindowCollapseBoxAttribute | kWindowStandardHandlerAttribute ); if( wndconfig->windowNoResize ) { windowAttributes |= kWindowLiveResizeAttribute; } else { windowAttributes |= ( kWindowFullZoomAttribute | kWindowResizableAttribute ); } error = CreateNewWindow( kDocumentWindowClass, windowAttributes, &windowContentBounds, &( _glfwWin.window ) ); if( ( error != noErr ) || ( _glfwWin.window == NULL ) ) { fprintf( stderr, "Failed to create Carbon window\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } _glfwWin.windowUPP = NewEventHandlerUPP( windowEventHandler ); error = InstallWindowEventHandler( _glfwWin.window, _glfwWin.windowUPP, GetEventTypeCount( GLFW_WINDOW_EVENT_TYPES ), GLFW_WINDOW_EVENT_TYPES, NULL, NULL ); if( error != noErr ) { fprintf( stderr, "Failed to install Carbon window event handler\n" ); _glfwPlatformCloseWindow(); return GL_FALSE; } // Don't care if we fail here (void)SetWindowTitleWithCFString( _glfwWin.window, CFSTR( "GLFW Window" ) ); (void)RepositionWindow( _glfwWin.window, NULL, kWindowCenterOnMainScreen ); if( !aglSetDrawable( _glfwWin.aglContext, GetWindowPort( _glfwWin.window ) ) ) { fprintf( stderr, "Failed to set the AGL context as the Carbon window drawable: %s\n", aglErrorString( aglGetError() ) ); _glfwPlatformCloseWindow(); return GL_FALSE; } // Make OpenGL context current if( !aglSetCurrentContext( _glfwWin.aglContext ) ) { fprintf( stderr, "Failed to make AGL context current: %s\n", aglErrorString( aglGetError() ) ); _glfwPlatformCloseWindow(); return GL_FALSE; } ShowWindow( _glfwWin.window ); } else { CGDisplayErr cgErr; CGLError cglErr; CFDictionaryRef optimalMode; GLint numCGLvs = 0; CGLPixelFormatAttribute CGLpixelFormatAttributes[64]; int numCGLAttrs = 0; // variables for enumerating color depths GLint rgbColorDepth; // CGL pixel format attributes _setCGLAttribute( kCGLPFADisplayMask, CGDisplayIDToOpenGLDisplayMask( kCGDirectMainDisplay ) ); if( fbconfig->stereo ) { CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFAStereo; } if( fbconfig->samples > 1 ) { _setCGLAttribute( kCGLPFASamples, (CGLPixelFormatAttribute)fbconfig->samples ); _setCGLAttribute( kCGLPFASampleBuffers, (CGLPixelFormatAttribute)1 ); CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFANoRecovery; } CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFAFullScreen; CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFADoubleBuffer; CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFAAccelerated; CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFANoRecovery; CGLpixelFormatAttributes[ numCGLAttrs++ ] = kCGLPFAMinimumPolicy; _setCGLAttribute( kCGLPFAAccumSize, (CGLPixelFormatAttribute)( fbconfig->accumRedBits \ + fbconfig->accumGreenBits \ + fbconfig->accumBlueBits \ + fbconfig->accumAlphaBits ) ); _setCGLAttribute( kCGLPFAAlphaSize, (CGLPixelFormatAttribute)fbconfig->alphaBits ); _setCGLAttribute( kCGLPFADepthSize, (CGLPixelFormatAttribute)fbconfig->depthBits ); _setCGLAttribute( kCGLPFAStencilSize, (CGLPixelFormatAttribute)fbconfig->stencilBits ); _setCGLAttribute( kCGLPFAAuxBuffers, (CGLPixelFormatAttribute)fbconfig->auxBuffers ); CGLpixelFormatAttributes[ numCGLAttrs++ ] = (CGLPixelFormatAttribute)NULL; // create a suitable pixel format with above attributes.. cglErr = CGLChoosePixelFormat( CGLpixelFormatAttributes, &_glfwWin.cglPixelFormat, &numCGLvs ); if( cglErr != kCGLNoError ) { fprintf( stderr, "Failed to choose CGL pixel format: %s\n", CGLErrorString( cglErr ) ); _glfwPlatformCloseWindow(); return GL_FALSE; } // ..and create a rendering context using that pixel format cglErr = CGLCreateContext( _glfwWin.cglPixelFormat, NULL, &_glfwWin.cglContext ); if( cglErr != kCGLNoError ) { fprintf( stderr, "Failed to create CGL context: %s\n", CGLErrorString( cglErr ) ); _glfwPlatformCloseWindow(); return GL_FALSE; } // enumerate depth of RGB channels - unlike AGL, CGL works with // a single parameter reflecting the full depth of the frame buffer (void)CGLDescribePixelFormat( _glfwWin.cglPixelFormat, 0, kCGLPFAColorSize, &rgbColorDepth ); // capture the display for our application cgErr = CGCaptureAllDisplays(); if( cgErr != kCGErrorSuccess ) { fprintf( stderr, "Failed to capture Core Graphics displays\n"); _glfwPlatformCloseWindow(); return GL_FALSE; } // find closest matching NON-STRETCHED display mode.. optimalMode = CGDisplayBestModeForParametersAndRefreshRateWithProperty( kCGDirectMainDisplay, rgbColorDepth, width, height, wndconfig->refreshRate, NULL, NULL ); if( optimalMode == NULL ) { fprintf( stderr, "Failed to retrieve Core Graphics display mode\n"); _glfwPlatformCloseWindow(); return GL_FALSE; } // ..and switch to that mode cgErr = CGDisplaySwitchToMode( kCGDirectMainDisplay, optimalMode ); if( cgErr != kCGErrorSuccess ) { fprintf( stderr, "Failed to switch to Core Graphics display mode\n"); _glfwPlatformCloseWindow(); return GL_FALSE; } // switch to our OpenGL context, and bring it up fullscreen cglErr = CGLSetCurrentContext( _glfwWin.cglContext ); if( cglErr != kCGLNoError ) { fprintf( stderr, "Failed to make CGL context current: %s\n", CGLErrorString( cglErr ) ); _glfwPlatformCloseWindow(); return GL_FALSE; } cglErr = CGLSetFullScreen( _glfwWin.cglContext ); if( cglErr != kCGLNoError ) { fprintf( stderr, "Failed to set CGL fullscreen mode: %s\n", CGLErrorString( cglErr ) ); _glfwPlatformCloseWindow(); return GL_FALSE; } } return GL_TRUE; }
i_img * imss_darwin(i_img_dim left, i_img_dim top, i_img_dim right, i_img_dim bottom) { CGDisplayCount count; CGDisplayErr err; CGRect rect; CGLPixelFormatObj pix; GLint npix; CGLContextObj ctx; i_img *im; CGDirectDisplayID disp; i_img_dim screen_width, screen_height; i_img_dim width, height; CGLPixelFormatAttribute pix_attrs[] = { kCGLPFADisplayMask, 0, /* filled in later */ kCGLPFAColorSize, 24, kCGLPFAAlphaSize, 0, kCGLPFAFullScreen, 0 }; i_clear_error(); disp = CGMainDisplayID(); if (!disp) { i_push_error(0, "No main display"); return NULL; } /* for now, only interested in the first display */ rect = CGDisplayBounds(disp); screen_width = rect.size.width; screen_height = rect.size.height; /* adjust negative/zero values to window size */ if (left < 0) left += screen_width; if (top < 0) top += screen_height; if (right <= 0) right += screen_width; if (bottom <= 0) bottom += screen_height; /* clamp */ if (left < 0) left = 0; if (right > screen_width) right = screen_width; if (top < 0) top = 0; if (bottom > screen_height) bottom = screen_height; /* validate */ if (right <= left || bottom <= top) { i_push_error(0, "image would be empty"); return NULL; } width = right - left; height = bottom - top; /* select a pixel format */ pix_attrs[1] = CGDisplayIDToOpenGLDisplayMask(disp); err = CGLChoosePixelFormat(pix_attrs, &pix, &npix); if (err) { i_push_errorf(err, "CGLChoosePixelFormat: %d", (int)err); return NULL; } if (!npix) { i_push_error(0, "No pixel format found - hidden display?"); return NULL; } /* make ourselves a context */ err = CGLCreateContext(pix, NULL, &ctx); CGLDestroyPixelFormat(pix); if (err) { i_push_errorf(err, "CGLCreateContext: %d", (int)err); return NULL; } err = CGLSetCurrentContext(ctx); if (err) { i_push_errorf(err, "CGLSetCurrentContext: %d", (int)err); return NULL; } err = CGLSetFullScreen(ctx); if (err) { i_push_errorf(err, "CGLSetFullScreen: %d", (int)err); return NULL; } /* capture */ im = i_img_8_new(width, height, 3); if (im) { size_t line_size = width * 4; size_t buf_size = line_size * height; unsigned char *buf = malloc(buf_size); i_img_dim y = height - 1; i_color *bufp = (i_color *)buf; /* hackish */ /* GL has the vertical axis going from bottom to top, so translate it */ glReadBuffer(GL_FRONT); glReadPixels(left, screen_height - top - height, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buf); /* transfer */ while (y >= 0) { i_plin(im, 0, width, y, bufp); bufp += width; --y; } free(buf); i_tags_setn(&im->tags, "ss_window_width", width); i_tags_setn(&im->tags, "ss_window_height", height); i_tags_set(&im->tags, "ss_type", "Darwin", 6); i_tags_set(&im->tags, "ss_variant", "<11", 3); i_tags_setn(&im->tags, "ss_left", left); i_tags_setn(&im->tags, "ss_top", top); } /* clean up */ CGLSetCurrentContext(NULL); CGLDestroyContext(ctx); return im; }
int doMain (int argc, char **argv) { int dummy; // OSG init OSG::osgInit(argc, argv); // create the graph // beacon for camera and light OSG::NodeUnrecPtr b1n = OSG::Node::create(); OSG::GroupUnrecPtr b1 = OSG::Group::create(); b1n->setCore( b1 ); // transformation OSG::NodeUnrecPtr t1n = OSG::Node::create(); OSG::TransformUnrecPtr t1 = OSG::Transform::create(); t1n->setCore( t1 ); t1n->addChild( b1n ); cam_trans = t1; // light OSG::NodeUnrecPtr dlight = OSG::Node::create(); OSG::DirectionalLightUnrecPtr dl = OSG::DirectionalLight::create(); dlight->setCore( dl ); dl->setAmbient( .3, .3, .3, 1 ); dl->setDiffuse( 1, 1, 1, 1 ); dl->setDirection(0,0,1); dl->setBeacon( b1n); // root root = OSG::Node::create(); OSG::GroupUnrecPtr gr1 = OSG::Group::create(); root->setCore( gr1 ); root->addChild( t1n ); root->addChild( dlight ); // Load the file OSG::NodeUnrecPtr file = NULL; if ( argc > 1 ) file = OSG::SceneFileHandler::the()->read(argv[1]); if ( file == NULL ) { std::cerr << "Couldn't load file, ignoring" << std::endl; file = OSG::makeTorus( .5, 2, 16, 16 ); } OSG::Thread::getCurrentChangeList()->commitChanges(); file->updateVolume(); OSG::Vec3f min,max; file->getVolume().getBounds( min, max ); std::cout << "Volume: from " << min << " to " << max << std::endl; dlight->addChild( file ); std::cerr << "Tree: " << std::endl; //root->dump(); // Camera cam = OSG::PerspectiveCamera::create(); cam->setBeacon( b1n ); cam->setFov( OSG::osgDegree2Rad( 90 ) ); cam->setNear( 0.1 ); cam->setFar( 100000 ); // Background OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create(); bkgnd->setColor(OSG::Color3f(0,0,1)); // Viewport vp = OSG::Viewport::create(); vp->setCamera( cam ); vp->setBackground( bkgnd ); vp->setRoot( root ); vp->setSize( 0,0, 1,1 ); // Action ract = OSG::RenderAction::create(); // tball OSG::Vec3f pos; pos.setValues(min[0] + ((max[0] - min[0]) * 0.5), min[1] + ((max[1] - min[1]) * 0.5), max[2] + ( max[2] - min[2] ) * 1.5 ); float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6; OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2, min[1] + (max[1] - min[1]) / 2, min[2] + (max[2] - min[2]) / 2); tball.setMode( OSG::Trackball::OSGObject ); tball.setStartPosition( pos, true ); tball.setSum( true ); tball.setTranslationMode( OSG::Trackball::OSGFree ); tball.setTranslationScale(scale); tball.setRotationCenter(tCenter); // CoreGL init // Install event handler EventHandlerUPP eventHandlerUPP = NewEventHandlerUPP(eventHandler); EventTypeSpec eventList[] = { { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent }, { kEventClassMouse, kEventMouseDown }, { kEventClassMouse, kEventMouseUp }, { kEventClassMouse, kEventMouseDragged } }; InstallApplicationEventHandler(eventHandlerUPP, GetEventTypeCount(eventList), eventList, 0, 0); CGDisplayCapture(kCGDirectMainDisplay); CGLPixelFormatAttribute attribs[] = { kCGLPFADoubleBuffer, kCGLPFAFullScreen, kCGLPFADepthSize, (CGLPixelFormatAttribute)16, kCGLPFADisplayMask, (CGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay), (CGLPixelFormatAttribute)0 }; CGLPixelFormatObj pixelFormatObj; GLint numPixelFormats; CGLChoosePixelFormat(attribs, &pixelFormatObj, &numPixelFormats); CGLContextObj contextObj; CGLCreateContext(pixelFormatObj, 0, &contextObj); CGLDestroyPixelFormat(pixelFormatObj); CGLSetCurrentContext(contextObj); CGLSetFullScreen(contextObj); // Create OpenSG window win = OSG::CoreGLWindow::create(); win->addPort( vp ); win->setContext ( contextObj ); win->init(); win->resize( CGDisplayPixelsWide(kCGDirectMainDisplay), CGDisplayPixelsHigh(kCGDirectMainDisplay) ); win->activate(); // do some OpenGL init. Will move into State Chunks later. glEnable( GL_DEPTH_TEST ); glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 ); redraw(); // Main loop ( event dispatching ) RunApplicationEventLoop(); // Cleanup CGLSetCurrentContext(0); CGLClearDrawable(contextObj); CGLDestroyContext(contextObj); CGReleaseAllDisplays(); DisposeEventHandlerUPP(eventHandlerUPP); ract = NULL; win = NULL; root = NULL; file = NULL; vp = NULL; cam_trans = NULL; cam = NULL; return 0; }
int screen_InitCapture( demux_t *p_demux ) { demux_sys_t *p_sys = p_demux->p_sys; screen_data_t *p_data; CGLPixelFormatAttribute attribs[4]; CGLPixelFormatObj pix; GLint npix; GLint viewport[4]; GLuint displayMask; CGLError returnedError; p_sys->p_data = p_data = ( screen_data_t * )malloc( sizeof( screen_data_t ) ); attribs[0] = kCGLPFAFullScreen; attribs[1] = kCGLPFADisplayMask; attribs[2] = CGDisplayIDToOpenGLDisplayMask( CGMainDisplayID() ); attribs[3] = 0; returnedError = CGLChoosePixelFormat( attribs, &pix, &npix ); if (returnedError) goto errorHandling; returnedError = CGLCreateContext( pix, NULL, &( p_data->screen ) ); if (returnedError) goto errorHandling; returnedError = CGLDestroyPixelFormat( pix ); if (returnedError) goto errorHandling; returnedError = CGLSetCurrentContext( p_data->screen ); if (returnedError) goto errorHandling; returnedError = CGLSetFullScreen( p_data->screen ); if (returnedError) goto errorHandling; glGetIntegerv( GL_VIEWPORT, viewport ); p_data->screen_width = viewport[2]; p_data->screen_height = viewport[3]; p_data->left = p_sys->i_left; p_data->top = p_sys->i_top; p_data->src_width = var_CreateGetInteger( p_demux, "screen-width" ); p_data->src_height = var_CreateGetInteger( p_demux, "screen-height" ); if (p_data->src_width <= 0 || p_data->src_height <= 0) { p_data->src_width = p_data->screen_width; p_data->src_height = p_data->screen_height; } p_data->dest_width = p_data->src_width; p_data->dest_height = p_data->src_height; attribs [0] = kCGLPFAOffScreen; attribs [1] = kCGLPFAColorSize; attribs [2] = 32; attribs [3] = 0; returnedError = CGLChoosePixelFormat( attribs, &pix, &npix ); if (returnedError) goto errorHandling; returnedError = CGLCreateContext( pix, NULL, &( p_data->scaled ) ); if (returnedError) goto errorHandling; returnedError = CGLDestroyPixelFormat( pix ); if (returnedError) goto errorHandling; returnedError = CGLSetCurrentContext( p_data->scaled ); if (returnedError) goto errorHandling; p_data->scaled_image = ( char * )malloc( p_data->dest_width * p_data->dest_height * 4 ); #warning FIXME: CGLSetOffScreen is no longer supported in the future! returnedError = CGLSetOffScreen( p_data->scaled, p_data->dest_width, p_data->dest_height, p_data->dest_width * 4, p_data->scaled_image ); if (returnedError) goto errorHandling; es_format_Init( &p_sys->fmt, VIDEO_ES, VLC_CODEC_RGB32 ); /* p_sys->fmt.video.i_* must set to screen size, not subscreen size */ p_sys->fmt.video.i_width = p_data->screen_width; p_sys->fmt.video.i_visible_width = p_data->screen_width; p_sys->fmt.video.i_height = p_data->screen_height; p_sys->fmt.video.i_bits_per_pixel = 32; glGenTextures( 1, &( p_data->texture ) ); glBindTexture( GL_TEXTURE_2D, p_data->texture ); p_data->texture_image = ( char * )malloc( p_data->src_width * p_data->src_height * 4 ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); glGenTextures( 1, &( p_data->cursor_texture ) ); glBindTexture( GL_TEXTURE_2D, p_data->cursor_texture ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); CGSNewConnection( NULL, &( p_data->connection ) ); return VLC_SUCCESS; errorHandling: msg_Err( p_demux, "Core OpenGL failure: %s", CGLErrorString( returnedError ) ); return VLC_EGENERIC; }