]> git.saurik.com Git - wxWidgets.git/blame - src/osx/core/glgrab.cpp
adding more infos, links
[wxWidgets.git] / src / osx / core / glgrab.cpp
CommitLineData
4fcb208a
SC
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.
03647350 6
4fcb208a
SC
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.
03647350 19
4fcb208a
SC
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.
03647350 24
4fcb208a
SC
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
afd5d91c
SC
33#if wxOSX_USE_COCOA_OR_CARBON
34
4fcb208a
SC
35#import <CoreFoundation/CoreFoundation.h>
36#import <ApplicationServices/ApplicationServices.h>
37#import <OpenGL/OpenGL.h>
38#import <OpenGL/gl.h>
39
40#include "wx/osx/private/glgrab.h"
41
42extern CGColorSpaceRef wxMacGetGenericRGBColorSpace();
43
44/*
45 * perform an in-place swap from Quadrant 1 to Quadrant III format
46 * (upside-down PostScript/GL to right side up QD/CG raster format)
47 * We do this in-place, which requires more copying, but will touch
48 * only half the pages. (Display grabs are BIG!)
49 *
50 * Pixel reformatting may optionally be done here if needed.
51 */
52static void swizzleBitmap(void * data, int rowBytes, int height)
53{
54 int top, bottom;
55 void * buffer;
56 void * topP;
57 void * bottomP;
58 void * base;
03647350
VZ
59
60
4fcb208a
SC
61 top = 0;
62 bottom = height - 1;
63 base = data;
64 buffer = malloc(rowBytes);
03647350
VZ
65
66
4fcb208a
SC
67 while ( top < bottom )
68 {
69 topP = (void *)((top * rowBytes) + (intptr_t)base);
70 bottomP = (void *)((bottom * rowBytes) + (intptr_t)base);
03647350
VZ
71
72
4fcb208a
SC
73 /*
74 * Save and swap scanlines.
75 *
76 * This code does a simple in-place exchange with a temp buffer.
77 * If you need to reformat the pixels, replace the first two bcopy()
78 * calls with your own custom pixel reformatter.
79 */
80 bcopy( topP, buffer, rowBytes );
81 bcopy( bottomP, topP, rowBytes );
82 bcopy( buffer, bottomP, rowBytes );
03647350 83
4fcb208a
SC
84 ++top;
85 --bottom;
86 }
87 free( buffer );
88}
89
90
91/*
92 * Given a display ID and a rectangle on that display, generate a CGImageRef
93 * containing the display contents.
94 *
95 * srcRect is display-origin relative.
96 *
97 * This function uses a full screen OpenGL read-only context.
98 * By using OpenGL, we can read the screen using a DMA transfer
99 * when it's in millions of colors mode, and we can correctly read
100 * a microtiled full screen OpenGL context, such as a game or full
101 * screen video display.
102 *
103 * Returns a CGImageRef. When you are done with the CGImageRef, release it
104 * using CFRelease().
105 * Returns NULL on an error.
106 */
03647350 107
4fcb208a
SC
108CGImageRef grabViaOpenGL(CGDirectDisplayID display, CGRect srcRect)
109{
110 CGContextRef bitmap;
111 CGImageRef image;
112 void * data;
113 long bytewidth;
114 GLint width, height;
115 long bytes;
03647350 116
4fcb208a
SC
117 CGLContextObj glContextObj;
118 CGLPixelFormatObj pixelFormatObj ;
119 GLint numPixelFormats ;
120 CGLPixelFormatAttribute attribs[] =
121 {
122 kCGLPFAFullScreen,
123 kCGLPFADisplayMask,
124 (CGLPixelFormatAttribute) 0, /* Display mask bit goes here */
125 (CGLPixelFormatAttribute) 0
126 } ;
03647350
VZ
127
128
4fcb208a
SC
129 if ( display == kCGNullDirectDisplay )
130 display = CGMainDisplayID();
131 attribs[2] = (CGLPixelFormatAttribute) CGDisplayIDToOpenGLDisplayMask(display);
03647350
VZ
132
133
4fcb208a
SC
134 /* Build a full-screen GL context */
135 CGLChoosePixelFormat( attribs, &pixelFormatObj, &numPixelFormats );
136 if ( pixelFormatObj == NULL ) // No full screen context support
137 return NULL;
138 CGLCreateContext( pixelFormatObj, NULL, &glContextObj ) ;
139 CGLDestroyPixelFormat( pixelFormatObj ) ;
140 if ( glContextObj == NULL )
141 return NULL;
03647350
VZ
142
143
4fcb208a
SC
144 CGLSetCurrentContext( glContextObj ) ;
145 CGLSetFullScreen( glContextObj ) ;
03647350
VZ
146
147
4fcb208a 148 glReadBuffer(GL_FRONT);
03647350
VZ
149
150
c22ace4d
VZ
151 width = (GLint)srcRect.size.width;
152 height = (GLint)srcRect.size.height;
03647350
VZ
153
154
4fcb208a
SC
155 bytewidth = width * 4; // Assume 4 bytes/pixel for now
156 bytewidth = (bytewidth + 3) & ~3; // Align to 4 bytes
157 bytes = bytewidth * height; // width * height
03647350 158
4fcb208a
SC
159 /* Build bitmap context */
160 data = malloc(height * bytewidth);
161 if ( data == NULL )
162 {
163 CGLSetCurrentContext( NULL );
164 CGLClearDrawable( glContextObj ); // disassociate from full screen
165 CGLDestroyContext( glContextObj ); // and destroy the context
166 return NULL;
167 }
168 bitmap = CGBitmapContextCreate(data, width, height, 8, bytewidth,
169 wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst /* XRGB */);
03647350
VZ
170
171
4fcb208a
SC
172 /* Read framebuffer into our bitmap */
173 glFinish(); /* Finish all OpenGL commands */
174 glPixelStorei(GL_PACK_ALIGNMENT, 4); /* Force 4-byte alignment */
175 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
176 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
177 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
03647350 178
4fcb208a
SC
179 /*
180 * Fetch the data in XRGB format, matching the bitmap context.
181 */
182 glReadPixels((GLint)srcRect.origin.x, (GLint)srcRect.origin.y, width, height,
183 GL_BGRA,
184#ifdef __BIG_ENDIAN__
185 GL_UNSIGNED_INT_8_8_8_8_REV, // for PPC
186#else
187 GL_UNSIGNED_INT_8_8_8_8, // for Intel! http://lists.apple.com/archives/quartz-dev/2006/May/msg00100.html
188#endif
189 data);
190 /*
191 * glReadPixels generates a quadrant I raster, with origin in the lower left
192 * This isn't a problem for signal processing routines such as compressors,
193 * as they can simply use a negative 'advance' to move between scanlines.
194 * CGImageRef and CGBitmapContext assume a quadrant III raster, though, so we need to
195 * invert it. Pixel reformatting can also be done here.
196 */
197 swizzleBitmap(data, bytewidth, height);
03647350
VZ
198
199
4fcb208a
SC
200 /* Make an image out of our bitmap; does a cheap vm_copy of the bitmap */
201 image = CGBitmapContextCreateImage(bitmap);
03647350 202
4fcb208a
SC
203 /* Get rid of bitmap */
204 CFRelease(bitmap);
205 free(data);
03647350
VZ
206
207
4fcb208a
SC
208 /* Get rid of GL context */
209 CGLSetCurrentContext( NULL );
210 CGLClearDrawable( glContextObj ); // disassociate from full screen
211 CGLDestroyContext( glContextObj ); // and destroy the context
03647350 212
4fcb208a
SC
213 /* Returned image has a reference count of 1 */
214 return image;
215}
216
afd5d91c 217#endif