apple_cgl.c revision 0594cf70883b64692ba617d85f4f9b4e636e5c2b
1/*
2 Copyright (c) 2008 Apple Inc.
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19 HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23
24 Except as contained in this notice, the name(s) of the above
25 copyright holders shall not be used in advertising or otherwise to
26 promote the sale, use or other dealings in this Software without
27 prior written authorization.
28*/
29
30#include <stdbool.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <dlfcn.h>
34
35#include "apple_cgl.h"
36#include "apple_glx.h"
37
38#ifndef OPENGL_FRAMEWORK_PATH
39#define OPENGL_FRAMEWORK_PATH "/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL"
40#endif
41
42static void *dl_handle = NULL;
43
44struct apple_cgl_api apple_cgl;
45
46static bool initialized = false;
47
48static void *
49sym(void *h, const char *name)
50{
51   void *s;
52
53   s = dlsym(h, name);
54
55   if (NULL == s) {
56      fprintf(stderr, "error: %s\n", dlerror());
57      abort();
58   }
59
60   return s;
61}
62
63void
64apple_cgl_init(void)
65{
66   void *h;
67   GLint major = 0, minor = 0;
68   const char *opengl_framework_path;
69
70   if (initialized)
71      return;
72
73   opengl_framework_path = getenv("OPENGL_FRAMEWORK_PATH");
74   if (!opengl_framework_path) {
75      opengl_framework_path = OPENGL_FRAMEWORK_PATH;
76   }
77
78   (void) dlerror();            /*drain dlerror */
79   h = dlopen(opengl_framework_path, RTLD_NOW);
80
81   if (NULL == h) {
82      fprintf(stderr, "error: unable to dlopen %s : %s\n",
83              opengl_framework_path, dlerror());
84      abort();
85   }
86
87   dl_handle = h;
88
89   apple_cgl.get_version = sym(h, "CGLGetVersion");
90
91   apple_cgl.get_version(&major, &minor);
92
93   apple_glx_diagnostic("CGL major %d minor %d\n", major, minor);
94
95   if (1 != major) {
96      fprintf(stderr, "WARNING: the CGL major version has changed!\n"
97              "libGL may be incompatible!\n");
98   }
99
100   apple_cgl.choose_pixel_format = sym(h, "CGLChoosePixelFormat");
101   apple_cgl.destroy_pixel_format = sym(h, "CGLDestroyPixelFormat");
102
103   apple_cgl.clear_drawable = sym(h, "CGLClearDrawable");
104   apple_cgl.flush_drawable = sym(h, "CGLFlushDrawable");
105
106   apple_cgl.create_context = sym(h, "CGLCreateContext");
107   apple_cgl.destroy_context = sym(h, "CGLDestroyContext");
108
109   apple_cgl.set_current_context = sym(h, "CGLSetCurrentContext");
110   apple_cgl.get_current_context = sym(h, "CGLGetCurrentContext");
111   apple_cgl.error_string = sym(h, "CGLErrorString");
112
113   apple_cgl.set_off_screen = sym(h, "CGLSetOffScreen");
114
115   apple_cgl.copy_context = sym(h, "CGLCopyContext");
116
117   apple_cgl.create_pbuffer = sym(h, "CGLCreatePBuffer");
118   apple_cgl.destroy_pbuffer = sym(h, "CGLDestroyPBuffer");
119   apple_cgl.set_pbuffer = sym(h, "CGLSetPBuffer");
120
121   initialized = true;
122}
123
124void *
125apple_cgl_get_dl_handle(void)
126{
127   return dl_handle;
128}
129