summaryrefslogtreecommitdiff
path: root/glsnapshot.cpp
blob: f06818b5a61ad5d6abb704eddc43cb306243ee4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**************************************************************************
 *
 * Copyright 2011 Jose Fonseca
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 **************************************************************************/


#include <assert.h>
#include <stdint.h>

#include "image.hpp"
#include "glproc.hpp"
#include "glsize.hpp"


#if !defined(_WIN32) && !defined(__APPLE__)


#include <X11/Xproto.h>


static int
errorHandler(Display *display, XErrorEvent *event)
{
    if (event->error_code == BadDrawable &&
        event->request_code == X_GetGeometry) {
        return 0;
    }

    char error_text[512];
    XGetErrorText(display, event->error_code, error_text, sizeof error_text);
    fprintf(stderr, "X Error of failed request:  %s\n", error_text);

    return 0;
}


#endif /* !_WIN32 && !__APPLE__ */


namespace glsnapshot {


/**
 * Get the contents of the current drawable into an image.
 */
static Image::Image *
getDrawableImage(void) {
#if defined(_WIN32)

    HDC hDC = __wglGetCurrentDC();
    if (!hDC) {
        return false;
    }

    HWND hWnd = WindowFromDC(hDC);
    RECT rect;

    if (!GetClientRect(hWnd, &rect)) {
       return false;
    }

    int width  = rect.right  - rect.left;
    int height = rect.bottom - rect.top;

    // TODO: http://msdn.microsoft.com/en-us/library/dd183402

    return NULL;

#elif defined(__APPLE__)

    // TODO
    return NULL;

#else

    Display *display;
    Drawable drawable;
    Window root;
    int x, y;
    unsigned int w, h, bw, depth;

    __glFinish();
    __glXWaitGL();

    display = __glXGetCurrentDisplay();
    if (!display) {
        return false;
    }

    drawable = __glXGetCurrentDrawable();
    if (drawable == None) {
        return false;
    }

    /*
     * XXX: This does not work for drawables created with glXCreateWindow
     */

    int (*oldErrorHandler)(Display *, XErrorEvent *);
    Status status;

    oldErrorHandler = XSetErrorHandler(errorHandler);
    status = XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth);
    XSetErrorHandler(oldErrorHandler);
    if (!status) {
        return false;
    }

    XImage *ximage;

    ximage = XGetImage(display, drawable, 0, 0, w, h, AllPlanes, ZPixmap);
    if (!ximage) {
        return NULL;
    }

    Image::Image *image = NULL;

    if (ximage->depth          == 24 &&
        ximage->bits_per_pixel == 32 &&
        ximage->red_mask       == 0x00ff0000 &&
        ximage->green_mask     == 0x0000ff00 &&
        ximage->blue_mask      == 0x000000ff) {

        image = new Image::Image(w, h, 4);

        if (image) {
            const uint32_t *src = (const uint32_t *)ximage->data;
            uint32_t *dst = (uint32_t*) image->start();
            for (unsigned y = 0; y < h; ++y) {
                for (unsigned x = 0; x < w; ++x) {
                    uint32_t bgra = src[x];
                    uint32_t rgba = (bgra & 0xff00ff00)
                                  | ((bgra >> 16) & 0xff)
                                  | ((bgra & 0xff) << 16);
                    dst[x] = rgba;
                }

                src += ximage->bytes_per_line / sizeof *src;
                dst += image->stride() / sizeof *src;
            }
        }
    } else {
        OS::DebugMessage("apitrace: unexpected XImage: "
                         "bits_per_pixel = %i, "
                         "depth = %i, "
                         "red_mask = 0x%08lx, "
                         "green_mask = 0x%08lx, "
                         "blue_mask = 0x%08lx\n",
                         ximage->bits_per_pixel,
                         ximage->depth,
                         ximage->red_mask,
                         ximage->green_mask,
                         ximage->blue_mask);
    }

    XDestroyImage(ximage);

    return image;
#endif
}


// Prefix of the snapshot images to take, if not NULL.
static const char *snapshot_prefix = NULL;

// Maximum number of frames to trace.
static unsigned max_frames = ~0;

// Current frame number.
static unsigned frame_no = 0;


void snapshot(unsigned call_no) {

    if (frame_no == 0) {
        const char *max_frames_str = getenv("TRACE_FRAMES");
        if (max_frames_str) {
            max_frames = atoi(max_frames_str);
        }
        snapshot_prefix = getenv("TRACE_SNAPSHOT");
    }

    ++frame_no;

    if (snapshot_prefix) {
        Image::Image *src = getDrawableImage();
        if (src) {
            char filename[PATH_MAX];
            snprintf(filename, sizeof filename, "%s%010u.png", snapshot_prefix, call_no);
            if (src->writePNG(filename)) {
                OS::DebugMessage("apitrace: wrote %s\n", filename);
            }

            delete src;
        }
    }

    if (frame_no >= max_frames) {
        exit(0);
    }
}


} /* namespace glsnapshot */