]> git.cworth.org Git - apitrace/blob - glsnapshot.cpp
Minor typo correction of code comment
[apitrace] / glsnapshot.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, 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 DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <assert.h>
28 #include <stdint.h>
29
30 #include "os_path.hpp"
31 #include "image.hpp"
32 #include "glproc.hpp"
33 #include "glsize.hpp"
34
35
36 #if !defined(_WIN32) && !defined(__APPLE__)
37
38
39 #include <X11/Xproto.h>
40
41
42 static int
43 errorHandler(Display *display, XErrorEvent *event)
44 {
45     if (event->error_code == BadDrawable &&
46         event->request_code == X_GetGeometry) {
47         return 0;
48     }
49
50     char error_text[512];
51     XGetErrorText(display, event->error_code, error_text, sizeof error_text);
52     fprintf(stderr, "X Error of failed request:  %s\n", error_text);
53
54     return 0;
55 }
56
57
58 #endif /* !_WIN32 && !__APPLE__ */
59
60
61 namespace glsnapshot {
62
63
64 /**
65  * Get the contents of the current drawable into an image.
66  */
67 static image::Image *
68 getDrawableImage(void) {
69 #if defined(_WIN32)
70
71     HDC hDC = __wglGetCurrentDC();
72     if (!hDC) {
73         return false;
74     }
75
76     HWND hWnd = WindowFromDC(hDC);
77     RECT rect;
78
79     if (!GetClientRect(hWnd, &rect)) {
80        return false;
81     }
82
83     int width  = rect.right  - rect.left;
84     int height = rect.bottom - rect.top;
85
86     // TODO: http://msdn.microsoft.com/en-us/library/dd183402
87
88     return NULL;
89
90 #elif defined(__APPLE__)
91
92     // TODO
93     return NULL;
94
95 #else
96
97     Display *display;
98     Drawable drawable;
99     Window root;
100     int x, y;
101     unsigned int w, h, bw, depth;
102
103     __glFinish();
104     __glXWaitGL();
105
106     display = __glXGetCurrentDisplay();
107     if (!display) {
108         return false;
109     }
110
111     drawable = __glXGetCurrentDrawable();
112     if (drawable == None) {
113         return false;
114     }
115
116     /*
117      * XXX: This does not work for drawables created with glXCreateWindow
118      */
119
120     int (*oldErrorHandler)(Display *, XErrorEvent *);
121     Status status;
122
123     oldErrorHandler = XSetErrorHandler(errorHandler);
124     status = XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth);
125     XSetErrorHandler(oldErrorHandler);
126     if (!status) {
127         return false;
128     }
129
130     XImage *ximage;
131
132     ximage = XGetImage(display, drawable, 0, 0, w, h, AllPlanes, ZPixmap);
133     if (!ximage) {
134         return NULL;
135     }
136
137     image::Image *image = NULL;
138
139     if (ximage->depth          == 24 &&
140         ximage->bits_per_pixel == 32 &&
141         ximage->red_mask       == 0x00ff0000 &&
142         ximage->green_mask     == 0x0000ff00 &&
143         ximage->blue_mask      == 0x000000ff) {
144
145         image = new image::Image(w, h, 4);
146
147         if (image) {
148             const uint32_t *src = (const uint32_t *)ximage->data;
149             uint32_t *dst = (uint32_t*) image->start();
150             for (unsigned y = 0; y < h; ++y) {
151                 for (unsigned x = 0; x < w; ++x) {
152                     uint32_t bgra = src[x];
153                     uint32_t rgba = (bgra & 0xff00ff00)
154                                   | ((bgra >> 16) & 0xff)
155                                   | ((bgra & 0xff) << 16);
156                     dst[x] = rgba;
157                 }
158
159                 src += ximage->bytes_per_line / sizeof *src;
160                 dst += image->stride() / sizeof *src;
161             }
162         }
163     } else {
164         os::log("apitrace: unexpected XImage: "
165                          "bits_per_pixel = %i, "
166                          "depth = %i, "
167                          "red_mask = 0x%08lx, "
168                          "green_mask = 0x%08lx, "
169                          "blue_mask = 0x%08lx\n",
170                          ximage->bits_per_pixel,
171                          ximage->depth,
172                          ximage->red_mask,
173                          ximage->green_mask,
174                          ximage->blue_mask);
175     }
176
177     XDestroyImage(ximage);
178
179     return image;
180 #endif
181 }
182
183
184 // Prefix of the snapshot images to take, if not NULL.
185 static const char *snapshot_prefix = NULL;
186
187 // Maximum number of frames to trace.
188 static unsigned max_frames = ~0;
189
190 // Current frame number.
191 static unsigned frame_no = 0;
192
193
194 void snapshot(unsigned call_no) {
195
196     if (frame_no == 0) {
197         const char *max_frames_str = getenv("TRACE_FRAMES");
198         if (max_frames_str) {
199             max_frames = atoi(max_frames_str);
200         }
201         snapshot_prefix = getenv("TRACE_SNAPSHOT");
202     }
203
204     ++frame_no;
205
206     if (snapshot_prefix) {
207         image::Image *src = getDrawableImage();
208         if (src) {
209             os::Path filename = os::Path::format("%s%010u.png", snapshot_prefix, call_no);
210             if (src->writePNG(filename)) {
211                 os::log("apitrace: wrote %s\n", filename.str());
212             }
213
214             delete src;
215         }
216     }
217
218     if (frame_no >= max_frames) {
219         exit(0);
220     }
221 }
222
223
224 } /* namespace glsnapshot */