summaryrefslogtreecommitdiff
path: root/tests/OffscreenContext.cc
blob: 06799627167057ab872984b450006bd0b8294535 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*

Create an OpenGL context without creating an OpenGL Window. for Linux.

See Also

 glxgears.c by Brian Paul from mesa-demos (mesa3d.org)
 http://cgit.freedesktop.org/mesa/demos/tree/src/xdemos?id=mesa-demos-8.0.1
 http://www.opengl.org/sdk/docs/man/xhtml/glXIntro.xml
 http://www.mesa3d.org/brianp/sig97/offscrn.htm
 http://glprogramming.com/blue/ch07.html
 OffscreenContext.mm (Mac OSX version)

*/

#include "OffscreenContext.h"
#include "printutils.h"
#include "imageutils.h"
#include "fbo.h"

#include <GL/gl.h>
#include <GL/glx.h>

using namespace std;

struct OffscreenContext
{
  GLXContext openGLContext;
  Display *xdisplay;
  Window xwindow;
  int width;
  int height;
  fbo_t *fbo;
};

void offscreen_context_init(OffscreenContext &ctx, int width, int height)
{
  ctx.width = width;
  ctx.height = height;
  ctx.openGLContext = NULL;
  ctx.xdisplay = NULL;
  ctx.xwindow = (Window)NULL;
  ctx.fbo = NULL;
}

static XErrorHandler original_xlib_handler = (XErrorHandler) NULL;
static bool XCreateWindow_failed = false;
static int XCreateWindow_error(Display *dpy, XErrorEvent *event) 
{
  cerr << "XCreateWindow failed: XID: " << event->resourceid 
        << " request: " << (int)event->request_code 
        << " minor: " << (int)event->minor_code << "\n";
  char description[1024];
  XGetErrorText( dpy, event->error_code, description, 1023 );
  cerr << " error message: " << description << "\n";
  XCreateWindow_failed = true;
  return 0;
}

bool create_glx_dummy_window(OffscreenContext &ctx) 
{
  /*
  create a dummy X window without showing it. (without 'mapping' it)
  and save information to the ctx.

  This purposely does not use glxCreateWindow, to avoid crashes,
  "failed to create drawable" errors, and Mesa "WARNING: Application calling 
    GLX 1.3 function when GLX 1.3 is not supported! This is an application bug!"

  This function will alter ctx.openGLContext and ctx.xwindow if successfull
  */

  int attributes[] = {
    GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
    GLX_RENDER_TYPE,   GLX_RGBA_BIT,
    GLX_RED_SIZE, 1,
    GLX_GREEN_SIZE, 1,
    GLX_BLUE_SIZE, 1,
    None
  };

  Display *dpy = ctx.xdisplay;

  int num_returned = 0;
  GLXFBConfig *fbconfigs = glXChooseFBConfig( dpy, DefaultScreen(dpy), attributes, &num_returned );
  if ( fbconfigs == NULL ) {
    cerr << "glXChooseFBConfig failed\n";
    return false;
  }

  XVisualInfo *visinfo = glXGetVisualFromFBConfig( dpy, fbconfigs[0] );
  if ( visinfo == NULL ) {
    cerr << "glXGetVisualFromFBConfig failed\n";
    XFree( fbconfigs );
    return false;
  }

  // can't depend on xWin==NULL at failure. use a custom Xlib error handler instead.
  original_xlib_handler = XSetErrorHandler( XCreateWindow_error );
  Window xWin = XCreateSimpleWindow( dpy, DefaultRootWindow(dpy), 0,0,10,10, 0,0,0 );
  XSync( dpy, false ); 
  if ( XCreateWindow_failed ) { 
    XFree( visinfo );
    XFree( fbconfigs );
    return false;    
  }
  XSetErrorHandler( original_xlib_handler );

  // Most programs would call XMapWindow here. But we don't, to keep the window hidden

  GLXContext context = glXCreateNewContext( dpy, fbconfigs[0], GLX_RGBA_TYPE, NULL, True );
  if ( context == NULL ) {
    cerr << "glXCreateNewContext failed\n";
    XDestroyWindow( dpy, xWin );
    XFree( visinfo );
    XFree( fbconfigs );
    return false;
  }  

  if (!glXMakeContextCurrent( dpy, xWin, xWin, context )) {
    cerr << "glXMakeContextCurrent failed\n";
    glXDestroyContext( dpy, context );
    XDestroyWindow( dpy, xWin );
    XFree( visinfo );
    XFree( fbconfigs );
    return false;
  }

  ctx.openGLContext = context;
  ctx.xwindow = xWin;

  XFree( visinfo );
  XFree( fbconfigs );

  return true;
}


Bool create_glx_dummy_context(OffscreenContext &ctx)
{
  // This will alter ctx.openGLContext and ctx.xdisplay and ctx.xwindow if successfull
  int major;
  int minor;
  Bool result = False;

  ctx.xdisplay = XOpenDisplay( NULL );
  if ( ctx.xdisplay == NULL ) {
    cerr << "Unable to open a connection to the X server\n";
    return False;
  }

  // glxQueryVersion is not always reliable. Use it, but then
  // also check to see if GLX 1.3 functions exist

  glXQueryVersion(ctx.xdisplay, &major, &minor);

  if ( major==1 && minor<=2 && glXGetVisualFromFBConfig==NULL ) {
    cerr << "Error: GLX version 1.3 functions missing. "
        << "Your GLX version: " << major << "." << minor << endl;
  } else {
    result = create_glx_dummy_window(ctx);
  }

  if (!result) XCloseDisplay( ctx.xdisplay );
  return result;
}

void glewCheck() {
#ifdef DEBUG
  cerr << "GLEW version " << glewGetString(GLEW_VERSION) << "\n";
  cerr << (const char *)glGetString(GL_RENDERER) << "(" << (const char *)glGetString(GL_VENDOR) << ")\n"
       << "OpenGL version " << (const char *)glGetString(GL_VERSION) << "\n";
  cerr  << "Extensions: " << (const char *)glGetString(GL_EXTENSIONS) << "\n";

  if (GLEW_ARB_framebuffer_object) {
    cerr << "ARB_FBO supported\n";
  }
  if (GLEW_EXT_framebuffer_object) {
    cerr << "EXT_FBO supported\n";
  }
  if (GLEW_EXT_packed_depth_stencil) {
    cerr << "EXT_packed_depth_stencil\n";
  }
#endif
}

OffscreenContext *create_offscreen_context(int w, int h)
{
  OffscreenContext *ctx = new OffscreenContext;
  offscreen_context_init( *ctx, w, h );

  // before an FBO can be setup, a GLX context must be created
  // this call alters ctx->xDisplay and ctx->openGLContext 
  //  and ctx->xwindow if successfull
  if (!create_glx_dummy_context( *ctx )) {
    return NULL;
  }

  // glewInit must come after Context creation and before FBO calls.
  GLenum err = glewInit();
  if (GLEW_OK != err) {
    fprintf(stderr, "Unable to init GLEW: %s\n", glewGetErrorString(err));
    exit(1);
  }
  glewCheck();

  ctx->fbo = fbo_new();
  if (!fbo_init(ctx->fbo, w, h)) {
    return NULL;
  }

  return ctx;
}

bool teardown_offscreen_context(OffscreenContext *ctx)
{
  if (ctx) {
    fbo_unbind(ctx->fbo);
    fbo_delete(ctx->fbo);
    XDestroyWindow( ctx->xdisplay, ctx->xwindow );
    glXDestroyContext( ctx->xdisplay, ctx->openGLContext );
    XCloseDisplay( ctx->xdisplay );
    return true;
  }
  return false;
}

/*!
  Capture framebuffer from OpenGL and write it to the given filename as PNG.
*/
bool save_framebuffer(OffscreenContext *ctx, const char *filename)
{
  if (!ctx || !filename) return false;
  int samplesPerPixel = 4; // R, G, B and A
  GLubyte pixels[ctx->width * ctx->height * samplesPerPixel];
  glReadPixels(0, 0, ctx->width, ctx->height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

  // Flip it vertically - images read from OpenGL buffers are upside-down
  int rowBytes = samplesPerPixel * ctx->width;
  unsigned char *flippedBuffer = (unsigned char *)malloc(rowBytes * ctx->height);
  if (!flippedBuffer) {
    std::cerr << "Unable to allocate flipped buffer for corrected image.";
    return 1;
  }
  flip_image(pixels, flippedBuffer, samplesPerPixel, ctx->width, ctx->height);

  bool writeok = write_png(filename, flippedBuffer, ctx->width, ctx->height);

  free(flippedBuffer);

  return writeok;
}

void bind_offscreen_context(OffscreenContext *ctx)
{
  if (ctx) fbo_bind(ctx->fbo);
}
contact: Jan Huwald // Impressum