RetroArch/gfx/drivers_context/cgl_ctx.c

386 lines
9.5 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2016-01-10 03:41:52 +00:00
* Copyright (C) 2011-2016 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
2015-04-26 01:48:35 +00:00
/* Apple CGL context.
Based on http://fernlightning.com/doku.php?id=randd:xopengl.
*/
2015-09-16 03:53:34 +00:00
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
2015-06-02 06:48:07 +00:00
#include <ApplicationServices/ApplicationServices.h>
2015-04-26 01:59:55 +00:00
#include <OpenGL/CGLTypes.h>
#include <OpenGL/CGLCurrent.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
2016-09-05 16:31:32 +00:00
#include "../../configuration.h"
2015-04-27 14:15:08 +00:00
#include "../../runloop.h"
#include "../../configuration.h"
#include "../video_context_driver.h"
typedef int CGSConnectionID;
typedef int CGSWindowID;
typedef int CGSSurfaceID;
typedef uint32_t _CGWindowID;
#ifdef __cplusplus
extern "C" {
#endif
/* Undocumented CGS */
extern CGSConnectionID CGSMainConnectionID(void);
extern CGError CGSAddSurface(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID *sid);
extern CGError CGSSetSurfaceBounds(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID sid, CGRect rect);
extern CGError CGSOrderSurface(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID sid, int a, int b);
/* Undocumented CGL */
2015-04-26 01:48:35 +00:00
extern CGLError CGLSetSurface(CGLContextObj gl, CGSConnectionID cid, CGSWindowID wid, CGSSurfaceID sid);
#ifdef __cplusplus
}
#endif
typedef struct gfx_ctx_cgl_data
{
CGLContextObj glCtx;
CGDirectDisplayID displayID;
int width, height;
} gfx_ctx_cgl_data_t;
static void gfx_ctx_cgl_swap_interval(void *data, unsigned interval)
{
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)data;
GLint params = interval;
CGLSetParameter(cgl->glCtx, kCGLCPSwapInterval, &params);
}
static void gfx_ctx_cgl_get_video_size(void *data, unsigned *width, unsigned *height)
{
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)data;
*width = cgl->width;
*height = cgl->height;
}
static void gfx_ctx_cgl_check_window(void *data, bool *quit,
bool *resize, unsigned *width, unsigned *height, unsigned frame_count)
{
2015-04-26 02:26:57 +00:00
unsigned new_width, new_height;
(void)frame_count;
*quit = false;
gfx_ctx_cgl_get_video_size(data, &new_width, &new_height);
if (new_width != *width || new_height != *height)
{
*width = new_width;
*height = new_height;
*resize = true;
}
}
static void gfx_ctx_cgl_swap_buffers(void *data)
{
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)data;
CGLFlushDrawable(cgl->glCtx);
}
static bool gfx_ctx_cgl_set_resize(void *data, unsigned width, unsigned height)
{
(void)data;
(void)width;
(void)height;
return false;
}
static void gfx_ctx_cgl_update_window_title(void *data)
{
2015-06-13 00:10:06 +00:00
char buf[128] = {0};
char buf_fps[128] = {0};
2015-04-27 14:15:08 +00:00
settings_t *settings = config_get_ptr();
(void)data;
2015-04-27 14:15:08 +00:00
video_monitor_get_fps(buf, sizeof(buf),
buf_fps, sizeof(buf_fps));
if (settings->fps_show)
2015-12-07 14:32:14 +00:00
runloop_msg_queue_push(buf_fps, 1, 1, false);
}
static bool gfx_ctx_cgl_set_video_mode(void *data,
unsigned width, unsigned height,
bool fullscreen)
{
(void)data;
(void)width;
(void)height;
(void)fullscreen;
return true;
}
static void gfx_ctx_cgl_destroy(void *data)
{
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)data;
if (cgl->glCtx)
{
CGLSetCurrentContext(NULL);
CGLDestroyContext(cgl->glCtx);
}
if (cgl->displayID)
CGDisplayRelease(cgl->displayID);
if (cgl)
free(cgl);
}
static void gfx_ctx_cgl_input_driver(void *data, const input_driver_t **input, void **input_data)
{
(void)data;
(void)input;
(void)input_data;
}
static gfx_ctx_proc_t gfx_ctx_cgl_get_proc_address(const char *symbol_name)
{
CFURLRef bundle_url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
CFSTR
("/System/Library/Frameworks/OpenGL.framework"),
kCFURLPOSIXPathStyle, true);
CFBundleRef opengl_bundle_ref = CFBundleCreate(kCFAllocatorDefault, bundle_url);
CFStringRef function = CFStringCreateWithCString(kCFAllocatorDefault, symbol_name,
kCFStringEncodingASCII);
gfx_ctx_proc_t ret = (gfx_ctx_proc_t)CFBundleGetFunctionPointerForName(
opengl_bundle_ref, function);
2015-04-27 13:52:37 +00:00
CFRelease(bundle_url);
CFRelease(function);
2015-04-27 13:52:37 +00:00
CFRelease(opengl_bundle_ref);
return ret;
}
static bool gfx_ctx_cgl_has_focus(void *data)
{
(void)data;
return true;
}
static bool gfx_ctx_cgl_suppress_screensaver(void *data, bool enable)
{
(void)data;
(void)enable;
return false;
}
static bool gfx_ctx_cgl_has_windowed(void *data)
{
(void)data;
2015-04-27 14:28:52 +00:00
return false;
}
2015-04-26 01:48:35 +00:00
static bool gfx_ctx_cgl_bind_api(void *data, enum gfx_ctx_api api,
unsigned major, unsigned minor)
{
(void)data;
(void)api;
(void)major;
(void)minor;
2015-04-26 02:38:35 +00:00
return api == GFX_CTX_OPENGL_API;
}
static void gfx_ctx_cgl_show_mouse(void *data, bool state)
{
(void)data;
(void)state;
}
static void gfx_ctx_cgl_bind_hw_render(void *data, bool enable)
{
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)data;
(void)enable;
CGLSetCurrentContext(cgl->glCtx);
/* TODO - needs to handle HW render context too */
}
static CGLContextObj gfx_ctx_cgl_init_create(void)
{
2015-04-27 14:28:52 +00:00
GLint num, params = 1;
CGLPixelFormatObj pix;
CGLContextObj glCtx = NULL;
CGLPixelFormatAttribute attributes[] = {
kCGLPFAAccelerated,
2015-04-26 01:48:35 +00:00
kCGLPFADoubleBuffer,
(CGLPixelFormatAttribute)0
};
CGLChoosePixelFormat(attributes, &pix, &num);
CGLCreateContext(pix, NULL, &glCtx);
CGLDestroyPixelFormat(pix);
2015-04-27 14:28:52 +00:00
CGLSetParameter(glCtx, kCGLCPSwapInterval, &params);
return glCtx;
}
2015-04-26 01:48:35 +00:00
static CGSSurfaceID attach_gl_context_to_window(CGLContextObj glCtx,
CGSWindowID wid, int *width, int *height)
{
CFArrayRef wins;
CFDictionaryRef win, bnd;
GLint params = 0;
Float64 w = 0, h = 0;
CGSSurfaceID sid = 0;
CGSConnectionID cid = CGSMainConnectionID();
printf("cid:%d wid:%d\n", cid, wid);
/* determine window size */
2015-06-02 17:11:01 +00:00
/* FIXME/TODO - CGWindowListCopyWindowInfo was introduced on OSX 10.5,
* find alternative for lower versions. */
2015-04-26 01:48:35 +00:00
wins = CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, wid); /* expect one result only */
2015-06-25 15:40:09 +00:00
win = (CFDictionaryRef)CFArrayGetValueAtIndex(wins, 0);
bnd = (CFDictionaryRef)CFDictionaryGetValue(win, kCGWindowBounds);
2015-06-25 15:44:11 +00:00
CFNumberGetValue((CFNumberRef)CFDictionaryGetValue((CFDictionaryRef)bnd, CFSTR("Width")),
2015-04-26 01:48:35 +00:00
kCFNumberFloat64Type, &w);
2015-06-25 15:44:11 +00:00
CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(bnd, CFSTR("Height")),
2015-04-26 01:48:35 +00:00
kCFNumberFloat64Type, &h);
CFRelease(wins);
/* create a surface. */
if(CGSAddSurface(cid, wid, &sid) != kCGErrorSuccess)
{
printf("ERR: no surface\n");
}
printf("sid:%d\n", sid);
/* set surface size, and order it frontmost */
if(CGSSetSurfaceBounds(cid, wid, sid, CGRectMake(0, 0, w, h)) != kCGErrorSuccess)
printf("ERR: cant set bounds\n");
if(CGSOrderSurface(cid, wid, sid, 1, 0) != kCGErrorSuccess)
printf("ERR: cant order front\n");
/* attach context to the surface */
if(CGLSetSurface(glCtx, cid, wid, sid) != kCGErrorSuccess)
{
printf("ERR: cant set surface\n");
}
/* check drawable */
CGLGetParameter(glCtx, kCGLCPHasDrawable, &params);
if(params != 1)
{
printf("ERR: no drawable\n");
}
*width = (int)w;
*height = (int)h;
return sid;
}
static void *gfx_ctx_cgl_init(void *video_driver)
{
CGError err;
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)calloc(1, sizeof(gfx_ctx_cgl_data_t));
if (!cgl)
2015-04-26 01:48:35 +00:00
goto error;
cgl->displayID = CGMainDisplayID();
err = CGDisplayCapture(cgl->displayID);
if (err != kCGErrorSuccess)
goto error;
cgl->glCtx = gfx_ctx_cgl_init_create();
if (!cgl->glCtx)
goto error;
2015-04-26 01:48:35 +00:00
attach_gl_context_to_window(cgl->glCtx,
CGShieldingWindowID(cgl->displayID), &cgl->width, &cgl->height);
2015-04-27 14:28:52 +00:00
printf("size:%dx%d\n", cgl->width, cgl->height);
2015-04-27 14:15:08 +00:00
CGLSetCurrentContext(cgl->glCtx);
return cgl;
error:
gfx_ctx_cgl_destroy(cgl);
return NULL;
}
static uint32_t gfx_ctx_cgl_get_flags(void *data)
2016-05-05 15:35:28 +00:00
{
uint32_t flags = 0;
BIT32_SET(flags, GFX_CTX_FLAGS_NONE);
return flags;
}
static void gfx_ctx_cgl_set_flags(void *data, uint32_t flags)
{
(void)data;
}
const gfx_ctx_driver_t gfx_ctx_cgl = {
gfx_ctx_cgl_init,
gfx_ctx_cgl_destroy,
gfx_ctx_cgl_bind_api,
gfx_ctx_cgl_swap_interval,
gfx_ctx_cgl_set_video_mode,
gfx_ctx_cgl_get_video_size,
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
NULL, /* get_metrics */
NULL,
gfx_ctx_cgl_update_window_title,
gfx_ctx_cgl_check_window,
gfx_ctx_cgl_set_resize,
gfx_ctx_cgl_has_focus,
gfx_ctx_cgl_suppress_screensaver,
gfx_ctx_cgl_has_windowed,
gfx_ctx_cgl_swap_buffers,
gfx_ctx_cgl_input_driver,
gfx_ctx_cgl_get_proc_address,
NULL,
NULL,
gfx_ctx_cgl_show_mouse,
"cgl",
gfx_ctx_cgl_get_flags,
2016-05-05 15:35:28 +00:00
gfx_ctx_cgl_set_flags,
2016-08-31 13:02:07 +00:00
gfx_ctx_cgl_bind_hw_render,
NULL,
NULL
};