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 "config.h"
41 #include <rfb/rfb.h>
42 #if HAVE_MACOSX_OPENGL_H
43 #include <OpenGL/OpenGL.h>
44 #include <OpenGL/gl.h>
45 #endif
46 
47 extern int macosx_no_opengl, macosx_read_opengl;
48 extern CGDirectDisplayID displayID;
49 
50 static CGLContextObj glContextObj;
51 
52 int macosx_opengl_width = 0;
53 int macosx_opengl_height = 0;
54 int macosx_opengl_bpp = 0;
55 
macosx_opengl_get_width(void)56 int macosx_opengl_get_width(void) {
57 	GLint viewport[4];
58 
59 	glGetIntegerv(GL_VIEWPORT, viewport);
60 	return (int) viewport[2];
61 }
62 
macosx_opengl_get_height(void)63 int macosx_opengl_get_height(void) {
64 	GLint viewport[4];
65 
66 	glGetIntegerv(GL_VIEWPORT, viewport);
67 	return (int) viewport[3];
68 }
69 
macosx_opengl_get_bpp(void)70 int macosx_opengl_get_bpp(void) {
71 	return 32;
72 }
73 
macosx_opengl_get_bps(void)74 int macosx_opengl_get_bps(void) {
75 	return 8;
76 }
77 
macosx_opengl_get_spp(void)78 int macosx_opengl_get_spp(void) {
79 	return 3;
80 }
81 
macosx_opengl_init(void)82 void macosx_opengl_init(void) {
83 	CGLPixelFormatObj pixelFormatObj;
84 	GLint numPixelFormats;
85 	CGLPixelFormatAttribute attribs[] = {
86 		kCGLPFAFullScreen,
87 		kCGLPFADisplayMask,
88 		0,
89 		0
90 	};
91 
92 	if (macosx_no_opengl) {
93 		return;
94 	}
95 
96 	attribs[2] = CGDisplayIDToOpenGLDisplayMask(displayID);
97 
98 	CGLChoosePixelFormat(attribs, &pixelFormatObj, &numPixelFormats);
99 	if (pixelFormatObj == NULL) {
100 		rfbLog("macosx_opengl_init: CGLChoosePixelFormat failed. Not using OpenGL.\n");
101 		return;
102 	}
103 
104 	CGLCreateContext(pixelFormatObj, NULL, &glContextObj);
105 	CGLDestroyPixelFormat(pixelFormatObj);
106 
107 	if (glContextObj == NULL) {
108 		rfbLog("macosx_opengl_init: CGLCreateContext failed. Not using OpenGL.\n");
109 		return;
110 	}
111 
112 	CGLSetCurrentContext(glContextObj);
113 	CGLSetFullScreen(glContextObj);
114 
115 	macosx_opengl_width  = macosx_opengl_get_width();
116 	macosx_opengl_height = macosx_opengl_get_height();
117 
118 	macosx_opengl_bpp = macosx_opengl_get_bpp();
119 
120 	glFinish();
121 
122 	glPixelStorei(GL_PACK_ALIGNMENT, 4);
123 	glPixelStorei(GL_PACK_ROW_LENGTH, 0);
124 	glPixelStorei(GL_PACK_SKIP_ROWS, 0);
125 	glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
126 
127 	rfbLog("macosx_opengl_init: Using OpenGL for screen capture.\n");
128 	macosx_read_opengl = 1;
129 }
130 
macosx_opengl_fini(void)131 void macosx_opengl_fini(void) {
132 	if (!macosx_read_opengl) {
133 		return;
134 	}
135 	CGLSetCurrentContext(NULL);
136 	CGLClearDrawable(glContextObj);
137 	CGLDestroyContext(glContextObj);
138 }
139 
macosx_copy_opengl(char * dest,int x,int y,unsigned int w,unsigned int h)140 void macosx_copy_opengl(char *dest, int x, int y, unsigned int w, unsigned int h) {
141 	int yflip = macosx_opengl_height - y - h;
142 
143 	CGLSetCurrentContext(glContextObj);
144 
145 	glReadPixels((GLint) x, (GLint) yflip, (int) w, (int) h,
146 	    GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, dest);
147 
148 	if (h > 1) {
149 		static char *pbuf = NULL;
150 		static int buflen = 0;
151 		int top = 0, bot = h - 1, rowlen = w * macosx_opengl_bpp/8;
152 		char *ptop, *pbot;
153 
154 		if (rowlen > buflen || buflen == 0)  {
155 			buflen = rowlen + 128;
156 			if (pbuf) {
157 				free(pbuf);
158 			}
159 			pbuf = (char *) malloc(buflen);
160 		}
161 		while (top < bot) {
162 			ptop = dest + top * rowlen;
163 			pbot = dest + bot * rowlen;
164 			memcpy(pbuf, ptop, rowlen);
165 			memcpy(ptop, pbot, rowlen);
166 			memcpy(pbot, pbuf, rowlen);
167 			top++;
168 			bot--;
169 		}
170 	}
171 }
172 
173 
174 #else
175 
176 #endif	/* __APPLE__ */
177 
178