summaryrefslogtreecommitdiff
path: root/src/glitz_glx_surface.c
blob: f603f1cf03a21c0066991b17a971a3e21992f2f1 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Copyright © 2004 David Reveman, Peter Nilsson
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the names of
 * David Reveman and Peter Nilsson not be used in advertising or
 * publicity pertaining to distribution of the software without
 * specific, written prior permission. David Reveman and Peter Nilsson
 * makes no representations about the suitability of this software for
 * any purpose. It is provided "as is" without express or implied warranty.
 *
 * DAVID REVEMAN AND PETER NILSSON DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DAVID REVEMAN AND
 * PETER NILSSON BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * Authors: David Reveman <c99drn@cs.umu.se>
 *          Peter Nilsson <c99pnn@cs.umu.se>
 */

#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif

#include "glitz_glxint.h"

static glitz_surface_t *
_glitz_glx_surface_create_similar (void *abstract_templ,
                                   glitz_format_t *format,
                                   int width,
                                   int height);

static void
_glitz_glx_surface_destroy (void *abstract_surface);

static glitz_texture_t *
_glitz_glx_surface_get_texture (void *abstract_surface);

static void
_glitz_glx_surface_update_size (void *abstract_surface);

static void
_glitz_glx_surface_swap_buffers (void *abstract_surface);

static glitz_bool_t
_glitz_glx_surface_push_current (void *abstract_surface,
                                 glitz_constraint_t constraint)
{
  glitz_glx_surface_t *surface = (glitz_glx_surface_t *) abstract_surface;
  glitz_bool_t success = 1;

  if (constraint == GLITZ_CN_SURFACE_DRAWABLE_CURRENT &&
      (!surface->drawable)) {
    constraint = GLITZ_CN_ANY_CONTEXT_CURRENT;
    success = 0;
  }
  
  surface = glitz_glx_context_push_current (surface, constraint);

  if (surface) {
    glitz_surface_setup_environment (&surface->base);
    return 1;
  }

  return success;
}

static void
_glitz_glx_surface_pop_current (void *abstract_surface)
{
  glitz_glx_surface_t *surface = (glitz_glx_surface_t *) abstract_surface;

  surface = glitz_glx_context_pop_current (surface);
  
  if (surface)
    glitz_surface_setup_environment (&surface->base);
}

static glitz_bool_t
_glitz_glx_surface_make_current_read (void *abstract_surface)
{
  /* This doesn't seem to work. 
  glitz_glx_surface_t *surface = (glitz_glx_surface_t *) abstract_surface;
  glitz_glx_static_proc_address_list_t *glx =
    &surface->screen_info->display_info->thread_info->glx;
    
  if (glx->make_context_current && surface->drawable) {
    GLXContext context = glXGetCurrentContext ();

    if (context == surface->context->context)
      return
        glx->make_context_current (surface->screen_info->display_info->display,
                                   glXGetCurrentDrawable (),
                                   surface->drawable,
                                   context);
  }
  */

  return 0;
}

static const struct glitz_surface_backend glitz_glx_surface_backend = {
  _glitz_glx_surface_create_similar,
  _glitz_glx_surface_destroy,
  _glitz_glx_surface_push_current,
  _glitz_glx_surface_pop_current,
  _glitz_glx_surface_get_texture,
  _glitz_glx_surface_update_size,
  _glitz_glx_surface_swap_buffers,
  _glitz_glx_surface_make_current_read
};

static glitz_bool_t
_glitz_glx_surface_update_size_for_window (Display *display,
                                           Window drawable,
                                           int *width,
                                           int *height)
{
  unsigned int uwidth, uheight, bwidth_ignore, depth_ignore;
  Window root_ignore;
  int x_ignore, y_ignore;
  
  if (!XGetGeometry (display, drawable, &root_ignore, &x_ignore, &y_ignore,
                     &uwidth, &uheight, &bwidth_ignore, &depth_ignore))
    return 0;
  
  *width = (int) uwidth;
  *height = (int) uheight;
  
  return 1;
}

static glitz_texture_t *
_glitz_glx_surface_get_texture (void *abstract_surface)
{
  glitz_glx_surface_t *surface = (glitz_glx_surface_t *) abstract_surface;
  
  if (surface->base.hint_mask & GLITZ_INT_HINT_DIRTY_MASK) {
    glitz_bounding_box_t copy_box;

    copy_box.x1 = copy_box.y1 = 0;
    copy_box.x2 = surface->base.width;
    copy_box.y2 = surface->base.height;
    glitz_intersect_bounding_box (&surface->base.dirty_box,
                                  &copy_box, &copy_box);
    glitz_texture_copy_surface (&surface->base.texture, &surface->base,
                                copy_box.x1,
                                copy_box.y1,
                                copy_box.x2 - copy_box.x1,
                                copy_box.y2 - copy_box.y1,
                                copy_box.x1,
                                copy_box.y1);
    surface->base.hint_mask &= ~GLITZ_INT_HINT_DIRTY_MASK;
  }
  
  if (surface->base.texture.allocated)
    return &surface->base.texture;
  else
    return NULL;
}

static void
_glitz_glx_set_features (glitz_glx_surface_t *surface)
{
  surface->base.feature_mask = surface->screen_info->feature_mask;

  surface->base.feature_mask &= ~GLITZ_FEATURE_ARB_MULTITEXTURE_MASK;
  surface->base.feature_mask &= ~GLITZ_FEATURE_ARB_VERTEX_PROGRAM_MASK;
  surface->base.feature_mask &= ~GLITZ_FEATURE_ARB_FRAGMENT_PROGRAM_MASK;
  surface->base.feature_mask &= ~GLITZ_FEATURE_CONVOLUTION_FILTER_MASK;
  surface->base.feature_mask &= ~GLITZ_FEATURE_PIXEL_BUFFER_OBJECT_MASK;

  if (surface->context->glx.need_lookup) {
    glitz_surface_push_current (&surface->base,
                                GLITZ_CN_SURFACE_CONTEXT_CURRENT);
    glitz_surface_pop_current (&surface->base);
  }

  if ((surface->screen_info->glx_feature_mask &
       GLITZ_GLX_FEATURE_ARB_RENDER_TEXTURE_MASK) &&
      surface->context->glx.bind_tex_image &&
      surface->context->glx.release_tex_image)
    surface->render_texture = 1;

  if (surface->context->gl.active_texture &&
      surface->context->gl.multi_tex_coord_2d) {
    surface->base.feature_mask |= GLITZ_FEATURE_ARB_MULTITEXTURE_MASK;

    if (surface->context->gl.gen_programs &&
        surface->context->gl.delete_programs &&
        surface->context->gl.program_string &&
        surface->context->gl.bind_program &&
        surface->context->gl.program_local_param_4d) {
      
      if (surface->screen_info->feature_mask &
          GLITZ_FEATURE_ARB_FRAGMENT_PROGRAM_MASK)
        surface->base.feature_mask |= GLITZ_FEATURE_ARB_FRAGMENT_PROGRAM_MASK;
      
      if (surface->screen_info->feature_mask &
          GLITZ_FEATURE_ARB_VERTEX_PROGRAM_MASK)
        surface->base.feature_mask |= GLITZ_FEATURE_ARB_VERTEX_PROGRAM_MASK;
    
      if ((surface->base.feature_mask &
           GLITZ_FEATURE_ARB_VERTEX_PROGRAM_MASK) &&
          (surface->base.feature_mask &
           GLITZ_FEATURE_ARB_FRAGMENT_PROGRAM_MASK) &&
          surface->context->texture_indirections >= 9) {
        /* Convolution filter programs require support for at least nine
           texture indirections. */
        surface->base.feature_mask |= GLITZ_FEATURE_CONVOLUTION_FILTER_MASK;
      }
    }
  }

  if (surface->context->gl.gen_buffers &&
      surface->context->gl.delete_buffers &&
      surface->context->gl.bind_buffer &&
      surface->context->gl.buffer_data &&
      surface->context->gl.map_buffer &&
      surface->context->gl.unmap_buffer)
    if (surface->screen_info->feature_mask &
        GLITZ_FEATURE_PIXEL_BUFFER_OBJECT_MASK)
      surface->base.feature_mask |= GLITZ_FEATURE_PIXEL_BUFFER_OBJECT_MASK;
}

static glitz_surface_t *
_glitz_glx_surface_create (glitz_glx_screen_info_t *screen_info,
                           glitz_format_t *format,
                           int width,
                           int height)
{
  glitz_glx_surface_t *surface;
  glitz_glx_context_t *context;

  context = glitz_glx_context_get (screen_info, format);
  if (!context)
    return NULL;
  
  surface = (glitz_glx_surface_t *) calloc (1, sizeof (glitz_glx_surface_t));
  if (surface == NULL)
    return NULL;

  glitz_surface_init (&surface->base,
                      &glitz_glx_surface_backend,
                      &context->gl,
                      format,
                      screen_info->formats,
                      screen_info->n_formats,
                      width,
                      height,
                      &screen_info->program_map,
                      screen_info->texture_mask);
  
  surface->screen_info = screen_info;
  surface->context = context;
  
  surface->base.hint_mask |= GLITZ_HINT_OFFSCREEN_MASK;

  if (surface->base.format->draw.offscreen) {
    glitz_surface_push_current (&surface->base, GLITZ_CN_ANY_CONTEXT_CURRENT);
    
    surface->drawable = surface->pbuffer =
      glitz_glx_pbuffer_create (screen_info->display_info,
                                surface->context->fbconfig,
                                &surface->base.texture);
    
    glitz_surface_pop_current (&surface->base);
  }

  _glitz_glx_set_features (surface);

  return &surface->base;
}

glitz_surface_t *
glitz_glx_surface_create (Display *display,
                          int screen,
                          glitz_format_t *format,
                          int width,
                          int height)
{

  return
    _glitz_glx_surface_create (glitz_glx_screen_info_get
                               (display, screen),
                               format, width, height);
}
slim_hidden_def(glitz_glx_surface_create);

glitz_surface_t *
glitz_glx_surface_create_for_window (Display *display,
                                     int screen,
                                     glitz_format_t *format,
                                     Window window)
{
  glitz_glx_surface_t *surface;
  glitz_glx_context_t *context;
  int width, height;
  glitz_glx_screen_info_t *screen_info =
    glitz_glx_screen_info_get (display, screen);
  
  context = glitz_glx_context_get (screen_info, format);
  if (!context)
    return NULL;

  if (!_glitz_glx_surface_update_size_for_window (display, window,
                                                  &width, &height))
    return NULL;

  surface = (glitz_glx_surface_t *) calloc (1, sizeof (glitz_glx_surface_t));
  if (surface == NULL)
    return NULL;

  glitz_surface_init (&surface->base,
                      &glitz_glx_surface_backend,
                      &context->gl,
                      format,
                      screen_info->formats,
                      screen_info->n_formats,
                      width,
                      height,
                      &screen_info->program_map,
                      screen_info->texture_mask);
  
  surface->screen_info = screen_info;
  surface->context = context;
  surface->drawable = window;

  _glitz_glx_set_features (surface);
  
  return &surface->base;
}
slim_hidden_def(glitz_glx_surface_create_for_window);

static glitz_surface_t *
_glitz_glx_surface_create_similar (void *abstract_templ,
                                   glitz_format_t *format,
                                   int width,
                                   int height)
{
  glitz_glx_surface_t *templ = (glitz_glx_surface_t *) abstract_templ;

  if (!format->read.offscreen)
    return NULL;
  
  return _glitz_glx_surface_create (templ->screen_info, format, width, height);
}

static void
_glitz_glx_surface_destroy (void *abstract_surface)
{
  glitz_glx_surface_t *surface = (glitz_glx_surface_t *) abstract_surface;

  glitz_surface_push_current (&surface->base, GLITZ_CN_ANY_CONTEXT_CURRENT);

  if (surface->pbuffer)
    glitz_glx_pbuffer_destroy (surface->screen_info->display_info,
                               surface->pbuffer);
  
  glitz_surface_pop_current (&surface->base);

  if (glXGetCurrentDrawable () == surface->drawable) {
    surface->drawable = None;
    glitz_glx_context_make_current (surface, 0);
  }
  
  glitz_surface_fini (&surface->base);
  
  free (surface);
}

static void
_glitz_glx_surface_update_size (void *abstract_surface)
{
  glitz_glx_surface_t *surface = (glitz_glx_surface_t *) abstract_surface;
  
  if ((!surface->pbuffer) && surface->drawable) {
    int width, height;
    
    _glitz_glx_surface_update_size_for_window
      (surface->screen_info->display_info->display, surface->drawable,
       &width, &height);

    if (width != surface->base.width || height != surface->base.height) {
      glitz_texture_t texture;

      glitz_texture_init (&texture,
                          width, height,
                          surface->base.texture.format,
                          surface->screen_info->texture_mask);
      
      if (texture.width != surface->base.texture.width ||
          texture.height != surface->base.texture.height ||
          texture.target != surface->base.texture.target) {
        texture.name = surface->base.texture.name;
        surface->base.texture = texture;
      }
      
      surface->base.width = width;
      surface->base.height = height;
    }
  }
}

static void
_glitz_glx_surface_swap_buffers (void *abstract_surface)
{
  glitz_glx_surface_t *surface = (glitz_glx_surface_t *) abstract_surface;

  if (surface->pbuffer || (!surface->drawable))
    return;

  glitz_glx_context_push_current (surface, GLITZ_CN_SURFACE_DRAWABLE_CURRENT);
  
  glXSwapBuffers (surface->screen_info->display_info->display,
                  surface->drawable);
  
  glitz_glx_context_pop_current (surface);
}