]> git.saurik.com Git - wxWidgets.git/blob - src/osx/core/glgrab.cpp
using statically allocated font objects for speedup
[wxWidgets.git] / src / osx / core / glgrab.cpp
1 /*
2 IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
3 consideration of your agreement to the following terms, and your use, installation or modification
4 of this Apple software constitutes acceptance of these terms. If you do not agree with these terms,
5 please do not use, install or modify this Apple software.
6
7 In consideration of your agreement to abide by the following terms, and subject to these
8 terms, Apple grants you a personal, non-exclusive license, under Apple\xd5s copyrights in
9 this original Apple software (the "Apple Software"), to use and modify the Apple Software,
10 with or without modifications, in source and/or binary forms; provided that if you redistribute
11 the Apple Software in its entirety and without modifications, you must retain this notice and
12 the following text and disclaimers in all such redistributions of the Apple Software.
13 Neither the name, trademarks, service marks or logos of Apple Computer, Inc. may be used
14 to endorse or promote products derived from the Apple Software without specific prior
15 written permission from Apple. Except as expressly tated in this notice, no other rights or
16 licenses, express or implied, are granted by Apple herein, including but not limited
17 to any patent rights that may be infringed by your derivative works or by other works
18 in which the Apple Software may be incorporated.
19
20 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES,
21 EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
22 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
23 USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
24
25 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
28 REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
29 WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
30 OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #import <CoreFoundation/CoreFoundation.h>
34 #import <ApplicationServices/ApplicationServices.h>
35 #import <OpenGL/OpenGL.h>
36 #import <OpenGL/gl.h>
37
38 #include "wx/osx/private/glgrab.h"
39
40 extern CGColorSpaceRef wxMacGetGenericRGBColorSpace();
41
42 /*
43 * perform an in-place swap from Quadrant 1 to Quadrant III format
44 * (upside-down PostScript/GL to right side up QD/CG raster format)
45 * We do this in-place, which requires more copying, but will touch
46 * only half the pages. (Display grabs are BIG!)
47 *
48 * Pixel reformatting may optionally be done here if needed.
49 */
50 static void swizzleBitmap(void * data, int rowBytes, int height)
51 {
52 int top, bottom;
53 void * buffer;
54 void * topP;
55 void * bottomP;
56 void * base;
57
58
59 top = 0;
60 bottom = height - 1;
61 base = data;
62 buffer = malloc(rowBytes);
63
64
65 while ( top < bottom )
66 {
67 topP = (void *)((top * rowBytes) + (intptr_t)base);
68 bottomP = (void *)((bottom * rowBytes) + (intptr_t)base);
69
70
71 /*
72 * Save and swap scanlines.
73 *
74 * This code does a simple in-place exchange with a temp buffer.
75 * If you need to reformat the pixels, replace the first two bcopy()
76 * calls with your own custom pixel reformatter.
77 */
78 bcopy( topP, buffer, rowBytes );
79 bcopy( bottomP, topP, rowBytes );
80 bcopy( buffer, bottomP, rowBytes );
81
82 ++top;
83 --bottom;
84 }
85 free( buffer );
86 }
87
88
89 /*
90 * Given a display ID and a rectangle on that display, generate a CGImageRef
91 * containing the display contents.
92 *
93 * srcRect is display-origin relative.
94 *
95 * This function uses a full screen OpenGL read-only context.
96 * By using OpenGL, we can read the screen using a DMA transfer
97 * when it's in millions of colors mode, and we can correctly read
98 * a microtiled full screen OpenGL context, such as a game or full
99 * screen video display.
100 *
101 * Returns a CGImageRef. When you are done with the CGImageRef, release it
102 * using CFRelease().
103 * Returns NULL on an error.
104 */
105
106 CGImageRef grabViaOpenGL(CGDirectDisplayID display, CGRect srcRect)
107 {
108 CGContextRef bitmap;
109 CGImageRef image;
110 void * data;
111 long bytewidth;
112 GLint width, height;
113 long bytes;
114
115 CGLContextObj glContextObj;
116 CGLPixelFormatObj pixelFormatObj ;
117 GLint numPixelFormats ;
118 CGLPixelFormatAttribute attribs[] =
119 {
120 kCGLPFAFullScreen,
121 kCGLPFADisplayMask,
122 (CGLPixelFormatAttribute) 0, /* Display mask bit goes here */
123 (CGLPixelFormatAttribute) 0
124 } ;
125
126
127 if ( display == kCGNullDirectDisplay )
128 display = CGMainDisplayID();
129 attribs[2] = (CGLPixelFormatAttribute) CGDisplayIDToOpenGLDisplayMask(display);
130
131
132 /* Build a full-screen GL context */
133 CGLChoosePixelFormat( attribs, &pixelFormatObj, &numPixelFormats );
134 if ( pixelFormatObj == NULL ) // No full screen context support
135 return NULL;
136 CGLCreateContext( pixelFormatObj, NULL, &glContextObj ) ;
137 CGLDestroyPixelFormat( pixelFormatObj ) ;
138 if ( glContextObj == NULL )
139 return NULL;
140
141
142 CGLSetCurrentContext( glContextObj ) ;
143 CGLSetFullScreen( glContextObj ) ;
144
145
146 glReadBuffer(GL_FRONT);
147
148
149 width = (GLint)srcRect.size.width;
150 height = (GLint)srcRect.size.height;
151
152
153 bytewidth = width * 4; // Assume 4 bytes/pixel for now
154 bytewidth = (bytewidth + 3) & ~3; // Align to 4 bytes
155 bytes = bytewidth * height; // width * height
156
157 /* Build bitmap context */
158 data = malloc(height * bytewidth);
159 if ( data == NULL )
160 {
161 CGLSetCurrentContext( NULL );
162 CGLClearDrawable( glContextObj ); // disassociate from full screen
163 CGLDestroyContext( glContextObj ); // and destroy the context
164 return NULL;
165 }
166 bitmap = CGBitmapContextCreate(data, width, height, 8, bytewidth,
167 wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst /* XRGB */);
168
169
170 /* Read framebuffer into our bitmap */
171 glFinish(); /* Finish all OpenGL commands */
172 glPixelStorei(GL_PACK_ALIGNMENT, 4); /* Force 4-byte alignment */
173 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
174 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
175 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
176
177 /*
178 * Fetch the data in XRGB format, matching the bitmap context.
179 */
180 glReadPixels((GLint)srcRect.origin.x, (GLint)srcRect.origin.y, width, height,
181 GL_BGRA,
182 #ifdef __BIG_ENDIAN__
183 GL_UNSIGNED_INT_8_8_8_8_REV, // for PPC
184 #else
185 GL_UNSIGNED_INT_8_8_8_8, // for Intel! http://lists.apple.com/archives/quartz-dev/2006/May/msg00100.html
186 #endif
187 data);
188 /*
189 * glReadPixels generates a quadrant I raster, with origin in the lower left
190 * This isn't a problem for signal processing routines such as compressors,
191 * as they can simply use a negative 'advance' to move between scanlines.
192 * CGImageRef and CGBitmapContext assume a quadrant III raster, though, so we need to
193 * invert it. Pixel reformatting can also be done here.
194 */
195 swizzleBitmap(data, bytewidth, height);
196
197
198 /* Make an image out of our bitmap; does a cheap vm_copy of the bitmap */
199 image = CGBitmapContextCreateImage(bitmap);
200
201 /* Get rid of bitmap */
202 CFRelease(bitmap);
203 free(data);
204
205
206 /* Get rid of GL context */
207 CGLSetCurrentContext( NULL );
208 CGLClearDrawable( glContextObj ); // disassociate from full screen
209 CGLDestroyContext( glContextObj ); // and destroy the context
210
211 /* Returned image has a reference count of 1 */
212 return image;
213 }
214