1 /*
2 Copyright (C) 2002-2010 Karl J. Runge <runge@karlrunge.com>
3 All rights reserved.
4
5 This file is part of x11vnc.
6
7 x11vnc is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at
10 your option) any later version.
11
12 x11vnc is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with x11vnc; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
20 or see <http://www.gnu.org/licenses/>.
21
22 In addition, as a special exception, Karl J. Runge
23 gives permission to link the code of its release of x11vnc with the
24 OpenSSL project's "OpenSSL" library (or with modified versions of it
25 that use the same license as the "OpenSSL" library), and distribute
26 the linked executables. You must obey the GNU General Public License
27 in all respects for all of the code used other than "OpenSSL". If you
28 modify this file, you may extend this exception to your version of the
29 file, but you are not obligated to do so. If you do not wish to do
30 so, delete this exception statement from your version.
31 */
32
33 /* -- macosx_opengl.c -- */
34
35 #if (defined(__MACH__) && defined(__APPLE__))
36
37 #include <CoreFoundation/CoreFoundation.h>
38 #include <ApplicationServices/ApplicationServices.h>
39
40 #include <rfb/rfb.h>
41 #if LIBVNCSERVER_HAVE_MACOSX_OPENGL_H
42 #include <OpenGL/OpenGL.h>
43 #include <OpenGL/gl.h>
44 #endif
45
46 extern int macosx_no_opengl, macosx_read_opengl;
47 extern CGDirectDisplayID displayID;
48
49 static CGLContextObj glContextObj;
50
51 int macosx_opengl_width = 0;
52 int macosx_opengl_height = 0;
53 int macosx_opengl_bpp = 0;
54
macosx_opengl_get_width(void)55 int macosx_opengl_get_width(void) {
56 GLint viewport[4];
57
58 glGetIntegerv(GL_VIEWPORT, viewport);
59 return (int) viewport[2];
60 }
61
macosx_opengl_get_height(void)62 int macosx_opengl_get_height(void) {
63 GLint viewport[4];
64
65 glGetIntegerv(GL_VIEWPORT, viewport);
66 return (int) viewport[3];
67 }
68
macosx_opengl_get_bpp(void)69 int macosx_opengl_get_bpp(void) {
70 return 32;
71 }
72
macosx_opengl_get_bps(void)73 int macosx_opengl_get_bps(void) {
74 return 8;
75 }
76
macosx_opengl_get_spp(void)77 int macosx_opengl_get_spp(void) {
78 return 3;
79 }
80
macosx_opengl_init(void)81 void macosx_opengl_init(void) {
82 CGLPixelFormatObj pixelFormatObj;
83 GLint numPixelFormats;
84 CGLPixelFormatAttribute attribs[] = {
85 kCGLPFAFullScreen,
86 kCGLPFADisplayMask,
87 0,
88 0
89 };
90
91 if (macosx_no_opengl) {
92 return;
93 }
94
95 attribs[2] = CGDisplayIDToOpenGLDisplayMask(displayID);
96
97 CGLChoosePixelFormat(attribs, &pixelFormatObj, &numPixelFormats);
98 if (pixelFormatObj == NULL) {
99 rfbLog("macosx_opengl_init: CGLChoosePixelFormat failed. Not using OpenGL.\n");
100 return;
101 }
102
103 CGLCreateContext(pixelFormatObj, NULL, &glContextObj);
104 CGLDestroyPixelFormat(pixelFormatObj);
105
106 if (glContextObj == NULL) {
107 rfbLog("macosx_opengl_init: CGLCreateContext failed. Not using OpenGL.\n");
108 return;
109 }
110
111 CGLSetCurrentContext(glContextObj);
112 CGLSetFullScreen(glContextObj);
113
114 macosx_opengl_width = macosx_opengl_get_width();
115 macosx_opengl_height = macosx_opengl_get_height();
116
117 macosx_opengl_bpp = macosx_opengl_get_bpp();
118
119 glFinish();
120
121 glPixelStorei(GL_PACK_ALIGNMENT, 4);
122 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
123 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
124 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
125
126 rfbLog("macosx_opengl_init: Using OpenGL for screen capture.\n");
127 macosx_read_opengl = 1;
128 }
129
macosx_opengl_fini(void)130 void macosx_opengl_fini(void) {
131 if (!macosx_read_opengl) {
132 return;
133 }
134 CGLSetCurrentContext(NULL);
135 CGLClearDrawable(glContextObj);
136 CGLDestroyContext(glContextObj);
137 }
138
macosx_copy_opengl(char * dest,int x,int y,unsigned int w,unsigned int h)139 void macosx_copy_opengl(char *dest, int x, int y, unsigned int w, unsigned int h) {
140 int yflip = macosx_opengl_height - y - h;
141
142 CGLSetCurrentContext(glContextObj);
143
144 glReadPixels((GLint) x, (GLint) yflip, (int) w, (int) h,
145 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, dest);
146
147 if (h > 1) {
148 static char *pbuf = NULL;
149 static int buflen = 0;
150 int top = 0, bot = h - 1, rowlen = w * macosx_opengl_bpp/8;
151 char *ptop, *pbot;
152
153 if (rowlen > buflen || buflen == 0) {
154 buflen = rowlen + 128;
155 if (pbuf) {
156 free(pbuf);
157 }
158 pbuf = (char *) malloc(buflen);
159 }
160 while (top < bot) {
161 ptop = dest + top * rowlen;
162 pbot = dest + bot * rowlen;
163 memcpy(pbuf, ptop, rowlen);
164 memcpy(ptop, pbot, rowlen);
165 memcpy(pbot, pbuf, rowlen);
166 top++;
167 bot--;
168 }
169 }
170 }
171
172
173 #else
174
175 #endif /* __APPLE__ */
176
177