summaryrefslogtreecommitdiff
path: root/src/glitz_framebuffer.c
blob: 04ef4161d59e51d6d418a68dd43271663133ef20 (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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * Copyright © 2005 Novell, Inc.
 *
 * 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 name of
 * Novell, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Novell, Inc. makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL NOVELL, INC. 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.
 *
 * Author: David Reveman <davidr@novell.com>
 */

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

#include "glitzint.h"

typedef struct _glitz_fbo_drawable {
    glitz_drawable_t base;
    glitz_drawable_t *other;
    int		     width;
    int		     height;
    glitz_gl_uint_t  fb;
    glitz_gl_uint_t  front;
    glitz_gl_uint_t  back;
    glitz_gl_uint_t  depth;
    glitz_gl_uint_t  stencil;
    glitz_gl_uint_t  front_texture;
    glitz_gl_uint_t  back_texture;
    glitz_gl_enum_t  internal_format;
} glitz_fbo_drawable_t;

static glitz_bool_t
_glitz_fbo_bind (glitz_fbo_drawable_t *drawable)
{
    glitz_bool_t    update = 0;
    glitz_gl_enum_t status;

    GLITZ_GL_DRAWABLE (drawable->other);

    if (!drawable->fb)
    {
	gl->gen_framebuffers (1, &drawable->fb);

	drawable->width  = drawable->base.width;
	drawable->height = drawable->base.height;
	update = 1;
    }
    else if (drawable->width  != drawable->base.width ||
	     drawable->height != drawable->base.height)
    {
	drawable->width  = drawable->base.width;
	drawable->height = drawable->base.height;
	update = 1;
    }

    gl->bind_framebuffer (GLITZ_GL_FRAMEBUFFER, drawable->fb);

    if (drawable->base.front &&
	drawable->front_texture != drawable->base.front->texture.name)
    {
	gl->framebuffer_texture_2d (GLITZ_GL_FRAMEBUFFER,
				    GLITZ_GL_COLOR_ATTACHMENT0,
				    drawable->base.front->texture.target,
				    drawable->base.front->texture.name,
				    0);

	drawable->front_texture = drawable->base.front->texture.name;

	if (drawable->front)
	{
	    gl->delete_renderbuffers (1, &drawable->front);
	    drawable->front = 0;
	}
    }

    if (!drawable->front_texture && !drawable->front)
    {
	gl->gen_renderbuffers (1, &drawable->front);
	gl->bind_renderbuffer (GLITZ_GL_RENDERBUFFER, drawable->front);
	gl->renderbuffer_storage (GLITZ_GL_RENDERBUFFER,
				  drawable->internal_format,
				  drawable->base.width,
				  drawable->base.height);
	gl->bind_renderbuffer (GLITZ_GL_RENDERBUFFER, 0);
	gl->framebuffer_renderbuffer (GLITZ_GL_FRAMEBUFFER,
				      GLITZ_GL_COLOR_ATTACHMENT0,
				      GLITZ_GL_RENDERBUFFER,
				      drawable->front);
    }

    if (drawable->base.format->d.doublebuffer)
    {
	if (drawable->base.back &&
	    drawable->back_texture != drawable->base.back->texture.name)
	{
	    gl->framebuffer_texture_2d (GLITZ_GL_FRAMEBUFFER,
					GLITZ_GL_COLOR_ATTACHMENT1,
					drawable->base.back->texture.target,
					drawable->base.back->texture.name,
					0);

	    drawable->back_texture = drawable->base.back->texture.name;

	    if (drawable->back)
	    {
		gl->delete_renderbuffers (1, &drawable->back);
		drawable->back = 0;
	    }
	}

	if (!drawable->back_texture && !drawable->back)
	{
	    gl->gen_renderbuffers (1, &drawable->back);
	    gl->bind_renderbuffer (GLITZ_GL_RENDERBUFFER,
				   drawable->back);
	    gl->renderbuffer_storage (GLITZ_GL_RENDERBUFFER,
				      drawable->internal_format,
				      drawable->base.width,
				      drawable->base.height);
	    gl->bind_renderbuffer (GLITZ_GL_RENDERBUFFER, 0);
	    gl->framebuffer_renderbuffer (GLITZ_GL_FRAMEBUFFER,
					  GLITZ_GL_COLOR_ATTACHMENT1,
					  GLITZ_GL_RENDERBUFFER,
					  drawable->back);
	}
    }

    if (update)
    {
	if (drawable->base.format->d.depth_size)
	{
	    if (!drawable->depth)
		gl->gen_renderbuffers (1, &drawable->depth);

	    gl->bind_renderbuffer (GLITZ_GL_RENDERBUFFER, drawable->depth);
	    gl->renderbuffer_storage (GLITZ_GL_RENDERBUFFER,
				      GLITZ_GL_DEPTH_COMPONENT,
				      drawable->base.width,
				      drawable->base.height);
	    gl->bind_renderbuffer (GLITZ_GL_RENDERBUFFER, 0);

	    gl->framebuffer_renderbuffer (GLITZ_GL_FRAMEBUFFER,
					  GLITZ_GL_DEPTH_ATTACHMENT,
					  GLITZ_GL_RENDERBUFFER,
					  drawable->depth);
	}

	if (drawable->base.format->d.stencil_size)
	{
	    if (!drawable->stencil)
		gl->gen_renderbuffers (1, &drawable->stencil);

	    gl->bind_renderbuffer (GLITZ_GL_RENDERBUFFER,
				   drawable->stencil);
	    gl->renderbuffer_storage (GLITZ_GL_RENDERBUFFER,
				      GLITZ_GL_STENCIL_INDEX,
				      drawable->base.width,
				      drawable->base.height);
	    gl->bind_renderbuffer (GLITZ_GL_RENDERBUFFER, 0);

	    gl->framebuffer_renderbuffer (GLITZ_GL_FRAMEBUFFER,
					  GLITZ_GL_STENCIL_ATTACHMENT,
					  GLITZ_GL_RENDERBUFFER,
					  drawable->stencil);
	}
    }

    status = gl->check_framebuffer_status (GLITZ_GL_FRAMEBUFFER);
    if (status == GLITZ_GL_FRAMEBUFFER_COMPLETE)
	return 1;

    return 0;
}

static void
_glitz_fbo_attach_notify (void            *abstract_drawable,
			  glitz_surface_t *surface)
{
    glitz_fbo_drawable_t *drawable = (glitz_fbo_drawable_t *)
	abstract_drawable;
    glitz_texture_t      *texture;

    GLITZ_GL_DRAWABLE (drawable->other);

    texture = &surface->texture;
    if (!TEXTURE_ALLOCATED (texture))
    {
	drawable->other->backend->push_current (drawable->other, NULL,
						GLITZ_ANY_CONTEXT_CURRENT,
						NULL);
	glitz_texture_allocate (gl, texture);
	drawable->other->backend->pop_current (drawable->other);

	if (!TEXTURE_ALLOCATED (texture))
	    return;
    }

    GLITZ_REGION_EMPTY (&surface->drawable_damage);
}

static void
_glitz_fbo_detach_notify (void            *abstract_drawable,
			  glitz_surface_t *surface)
{
    glitz_fbo_drawable_t *drawable = (glitz_fbo_drawable_t *)
	abstract_drawable;

    if (surface->texture.name == drawable->front_texture ||
	surface->texture.name == drawable->back_texture)
    {
	GLITZ_GL_DRAWABLE (drawable->other);

	drawable->other->backend->push_current (drawable->other, NULL,
						GLITZ_ANY_CONTEXT_CURRENT,
						NULL);

	gl->bind_framebuffer (GLITZ_GL_FRAMEBUFFER, drawable->fb);

	if (surface->texture.name == drawable->front_texture)
	{
	    gl->framebuffer_texture_2d (GLITZ_GL_FRAMEBUFFER,
					GLITZ_GL_COLOR_ATTACHMENT0,
					surface->texture.target,
					0, 0);
	    drawable->front_texture = 0;
	}

	if (surface->texture.name == drawable->back_texture)
	{
	    gl->framebuffer_texture_2d (GLITZ_GL_FRAMEBUFFER,
					GLITZ_GL_COLOR_ATTACHMENT1,
					surface->texture.target,
					0, 0);
	    drawable->back_texture = 0;
	}

	gl->bind_framebuffer (GLITZ_GL_FRAMEBUFFER, 0);

	surface->fb = 0;

	drawable->other->backend->pop_current (drawable->other);
    }
}

static glitz_bool_t
_glitz_fbo_push_current (void               *abstract_drawable,
			 glitz_surface_t    *surface,
			 glitz_constraint_t constraint,
			 glitz_bool_t       *restore_state)
{
    glitz_fbo_drawable_t *drawable = (glitz_fbo_drawable_t *)
	abstract_drawable;

    drawable->other->backend->push_current (drawable->other, surface,
					    constraint, restore_state);

    if (constraint == GLITZ_DRAWABLE_CURRENT)
    {
	if (_glitz_fbo_bind (drawable))
	{
	    drawable->base.update_all = drawable->other->update_all = 1;
	    surface->fb = drawable->fb;
	    return 1;
	}
    }

    return 0;
}

static glitz_surface_t *
_glitz_fbo_pop_current (void *abstract_drawable)
{
    glitz_fbo_drawable_t *drawable = (glitz_fbo_drawable_t *)
	abstract_drawable;

    GLITZ_GL_DRAWABLE (drawable->other);

    gl->bind_framebuffer (GLITZ_GL_FRAMEBUFFER, 0);

    return drawable->other->backend->pop_current (drawable->other);
}

static void
_glitz_fbo_make_current (void *abstract_drawable,
			 void *abstract_context)
{
    glitz_fbo_drawable_t *drawable = (glitz_fbo_drawable_t *)
	abstract_drawable;

    drawable->other->backend->make_current (drawable->other, abstract_context);

    _glitz_fbo_bind (drawable);
}

static glitz_bool_t
_glitz_fbo_swap_buffers (void *abstract_drawable)
{
    glitz_fbo_drawable_t *drawable = (glitz_fbo_drawable_t *)
	abstract_drawable;

    if (!drawable->fb)
	return 1;

    /* flip */
    if (drawable->base.back && drawable->base.front)
    {
	glitz_texture_t tmp;

	tmp = drawable->base.front->texture;

	drawable->base.front->texture = drawable->base.back->texture;
	drawable->base.back->texture  = tmp;

	return 1;
    }

    return 0;
}

static glitz_bool_t
_glitz_fbo_copy_sub_buffer (void *abstract_drawable,
			    int  x,
			    int  y,
			    int  width,
			    int  height)
{
    return 0;
}

static void
_glitz_fbo_destroy (void *abstract_drawable)
{
    glitz_fbo_drawable_t *drawable = (glitz_fbo_drawable_t *)
	abstract_drawable;

    if (drawable->fb)
    {
	GLITZ_GL_DRAWABLE (drawable->other);

	drawable->other->backend->push_current (drawable->other, NULL,
						GLITZ_ANY_CONTEXT_CURRENT,
						NULL);

	gl->delete_framebuffers (1, &drawable->fb);

	if (drawable->front)
	    gl->delete_renderbuffers (1, &drawable->front);

	if (drawable->back)
	    gl->delete_renderbuffers (1, &drawable->back);

	if (drawable->depth)
	    gl->delete_renderbuffers (1, &drawable->depth);

	if (drawable->stencil)
	    gl->delete_renderbuffers (1, &drawable->stencil);

	drawable->other->backend->pop_current (drawable->other);
    }

    glitz_drawable_destroy (drawable->other);

    free (drawable);
}

static void
_glitz_fbo_draw_buffer (void                  *abstract_drawable,
			const glitz_gl_enum_t buffer)
{
    glitz_fbo_drawable_t *drawable = (glitz_fbo_drawable_t *)
	abstract_drawable;

    GLITZ_GL_DRAWABLE (drawable->other);

    switch (buffer) {
    case GLITZ_GL_FRONT:
	gl->draw_buffer (GLITZ_GL_COLOR_ATTACHMENT0);
	break;
    case GLITZ_GL_BACK:
	gl->draw_buffer (GLITZ_GL_COLOR_ATTACHMENT1);
	break;
    /* TODO: use GL_ARB_draw_buffers
    case GLITZ_GL_FRONT_AND_BACK:
    */
    default:
	break;
    }
}

static void
_glitz_fbo_read_buffer (void                  *abstract_drawable,
			const glitz_gl_enum_t buffer)
{
    glitz_fbo_drawable_t *drawable = (glitz_fbo_drawable_t *)
	abstract_drawable;

    GLITZ_GL_DRAWABLE (drawable->other);

    switch (buffer) {
    case GLITZ_GL_FRONT:
	gl->read_buffer (GLITZ_GL_COLOR_ATTACHMENT0);
	break;
    case GLITZ_GL_BACK:
	gl->read_buffer (GLITZ_GL_COLOR_ATTACHMENT1);
    default:
	break;
    }
}

glitz_drawable_t *
_glitz_fbo_drawable_create (glitz_drawable_t	        *other,
			    glitz_int_drawable_format_t *format,
			    int	                        width,
			    int	                        height)
{
    glitz_fbo_drawable_t *drawable;
    glitz_backend_t	 *backend;

    drawable = malloc (sizeof (glitz_fbo_drawable_t) +
		       sizeof (glitz_backend_t));
    if (!drawable)
	return NULL;

    glitz_drawable_reference (other);
    drawable->other = other;
    backend = (glitz_backend_t *) (drawable + 1);
    *backend = *other->backend;

    backend->destroy         = _glitz_fbo_destroy;
    backend->push_current    = _glitz_fbo_push_current;
    backend->pop_current     = _glitz_fbo_pop_current;
    backend->attach_notify   = _glitz_fbo_attach_notify;
    backend->detach_notify   = _glitz_fbo_detach_notify;
    backend->swap_buffers    = _glitz_fbo_swap_buffers;
    backend->copy_sub_buffer = _glitz_fbo_copy_sub_buffer;
    backend->make_current    = _glitz_fbo_make_current;
    backend->draw_buffer     = _glitz_fbo_draw_buffer;
    backend->read_buffer     = _glitz_fbo_read_buffer;

    drawable->fb = 0;

    drawable->width  = 0;
    drawable->height = 0;

    drawable->front   = 0;
    drawable->back    = 0;
    drawable->depth   = 0;
    drawable->stencil = 0;

    drawable->front_texture = 0;
    drawable->back_texture  = 0;

    /* XXX: temporary solution until we have proper format validation */
    if (format->d.color.alpha_size)
	drawable->internal_format = GLITZ_GL_RGBA;
    else
	drawable->internal_format = GLITZ_GL_RGB;

    _glitz_drawable_init (&drawable->base, format, backend, width, height);

    return &drawable->base;
}