1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
30 #include "os_path.hpp"
36 #if !defined(_WIN32) && !defined(__APPLE__)
39 #include <X11/Xproto.h>
43 errorHandler(Display *display, XErrorEvent *event)
45 if (event->error_code == BadDrawable &&
46 event->request_code == X_GetGeometry) {
51 XGetErrorText(display, event->error_code, error_text, sizeof error_text);
52 fprintf(stderr, "X Error of failed request: %s\n", error_text);
58 #endif /* !_WIN32 && !__APPLE__ */
61 namespace glsnapshot {
65 * Get the contents of the current drawable into an image.
68 getDrawableImage(void) {
71 HDC hDC = __wglGetCurrentDC();
76 HWND hWnd = WindowFromDC(hDC);
79 if (!GetClientRect(hWnd, &rect)) {
83 int width = rect.right - rect.left;
84 int height = rect.bottom - rect.top;
86 // TODO: http://msdn.microsoft.com/en-us/library/dd183402
90 #elif defined(__APPLE__)
101 unsigned int w, h, bw, depth;
106 display = __glXGetCurrentDisplay();
111 drawable = __glXGetCurrentDrawable();
112 if (drawable == None) {
117 * XXX: This does not work for drawables created with glXCreateWindow
120 int (*oldErrorHandler)(Display *, XErrorEvent *);
123 oldErrorHandler = XSetErrorHandler(errorHandler);
124 status = XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth);
125 XSetErrorHandler(oldErrorHandler);
132 ximage = XGetImage(display, drawable, 0, 0, w, h, AllPlanes, ZPixmap);
137 image::Image *image = NULL;
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) {
145 image = new image::Image(w, h, 4);
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);
159 src += ximage->bytes_per_line / sizeof *src;
160 dst += image->stride() / sizeof *src;
164 os::log("apitrace: unexpected XImage: "
165 "bits_per_pixel = %i, "
167 "red_mask = 0x%08lx, "
168 "green_mask = 0x%08lx, "
169 "blue_mask = 0x%08lx\n",
170 ximage->bits_per_pixel,
177 XDestroyImage(ximage);
184 // Prefix of the snapshot images to take, if not NULL.
185 static const char *snapshot_prefix = NULL;
187 // Maximum number of frames to trace.
188 static unsigned max_frames = ~0;
190 // Current frame number.
191 static unsigned frame_no = 0;
194 void snapshot(unsigned call_no) {
197 const char *max_frames_str = getenv("TRACE_FRAMES");
198 if (max_frames_str) {
199 max_frames = atoi(max_frames_str);
201 snapshot_prefix = getenv("TRACE_SNAPSHOT");
206 if (snapshot_prefix) {
207 image::Image *src = getDrawableImage();
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());
218 if (frame_no >= max_frames) {
224 } /* namespace glsnapshot */