summaryrefslogtreecommitdiff
path: root/src/glitz_drawable.c
blob: 66dce22f879b1980cf351010f063e08404a87ce5 (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
/*
 * Copyright © 2004 David Reveman
 *
 * 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
 * David Reveman not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * David Reveman makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * DAVID REVEMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL DAVID REVEMAN 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"

void
_glitz_drawable_init (glitz_drawable_t	          *drawable,
		      glitz_int_drawable_format_t *format,
		      glitz_backend_t	          *backend,
		      int		          width,
		      int		          height)
{
    drawable->ref_count = 1;

    drawable->format  = format;
    drawable->backend = backend;

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

    drawable->front = NULL;
    drawable->back  = NULL;

    drawable->viewport.x = -32767;
    drawable->viewport.y = -32767;
    drawable->viewport.width = 65535;
    drawable->viewport.height = 65535;

    drawable->update_all = 1;
    drawable->flushed    = 0;
    drawable->finished   = 0;
}

void
_glitz_drawable_draw_buffer (void                  *abstract_drawable,
			     const glitz_gl_enum_t buffer)
{
    glitz_drawable_t *drawable = abstract_drawable;

    GLITZ_GL_DRAWABLE (drawable);

    gl->draw_buffer (buffer);
}

void
_glitz_drawable_read_buffer (void                  *abstract_drawable,
			     const glitz_gl_enum_t buffer)
{
    glitz_drawable_t *drawable = abstract_drawable;

    GLITZ_GL_DRAWABLE (drawable);

    gl->read_buffer (buffer);
}

static glitz_bool_t
_glitz_drawable_size_check (glitz_drawable_t *other,
			    unsigned int     width,
			    unsigned int     height)
{
    if (width == 0 || height == 0)
	return 0;

    if (width  > other->backend->max_viewport_dims[0] ||
	height > other->backend->max_viewport_dims[1])
	return 0;

    return 1;
}

glitz_drawable_t *
glitz_create_drawable (glitz_drawable_t        *other,
		       glitz_drawable_format_t *format,
		       unsigned int            width,
		       unsigned int            height)
{
    glitz_int_drawable_format_t *iformat;

    if (!_glitz_drawable_size_check (other, width, height))
	return NULL;

    if (format->id >= other->backend->n_drawable_formats)
	return NULL;

    iformat = &other->backend->drawable_formats[format->id];
    if (!(iformat->types & GLITZ_DRAWABLE_TYPE_FBO_MASK))
	return NULL;

    return _glitz_fbo_drawable_create (other, iformat, width, height);
}
slim_hidden_def(glitz_create_drawable);

glitz_drawable_t *
glitz_create_pbuffer_drawable (glitz_drawable_t        *other,
			       glitz_drawable_format_t *format,
			       unsigned int            width,
			       unsigned int            height)
{
    glitz_int_drawable_format_t *iformat;

    if (!_glitz_drawable_size_check (other, width, height))
	return NULL;

    if (format->id >= other->backend->n_drawable_formats)
	return NULL;

    iformat = &other->backend->drawable_formats[format->id];
    if (!(iformat->types & GLITZ_DRAWABLE_TYPE_PBUFFER_MASK))
	return NULL;

    return other->backend->create_pbuffer (other, format, width, height);
}
slim_hidden_def(glitz_create_pbuffer_drawable);

void
glitz_drawable_destroy (glitz_drawable_t *drawable)
{
    if (!drawable)
	return;

    drawable->ref_count--;
    if (drawable->ref_count)
	return;

    drawable->backend->destroy (drawable);
}

void
glitz_drawable_reference (glitz_drawable_t *drawable)
{
    if (!drawable)
	return;

    drawable->ref_count++;
}

void
glitz_drawable_update_size (glitz_drawable_t *drawable,
			    unsigned int     width,
			    unsigned int     height)
{
    drawable->width = (int) width;
    drawable->height = (int) height;

    drawable->viewport.x = -32767;
    drawable->viewport.y = -32767;
    drawable->viewport.width = 65535;
    drawable->viewport.height = 65535;

    drawable->update_all = 1;
}

unsigned int
glitz_drawable_get_width (glitz_drawable_t *drawable)
{
    return (unsigned int) drawable->width;
}
slim_hidden_def(glitz_drawable_get_width);

unsigned int
glitz_drawable_get_height (glitz_drawable_t *drawable)
{
    return (unsigned int) drawable->height;
}
slim_hidden_def(glitz_drawable_get_height);

void
glitz_drawable_swap_buffer_region (glitz_drawable_t *drawable,
				   int              x_origin,
				   int              y_origin,
				   glitz_box_t      *box,
				   int              n_box)
{
    glitz_box_t	    rect;
    glitz_surface_t *surface = NULL;
    int		    x_pos, y_pos;
    int		    x, y, w, h;

    GLITZ_GL_DRAWABLE (drawable);

    if (!drawable->format->d.doublebuffer || !n_box)
	return;

    /* try swap buffers (fastest) */
    if (n_box == 1)
    {
	rect.x1 = x_origin + box->x1;
	rect.y1 = y_origin + box->y1;
	rect.x2 = x_origin + box->x2;
	rect.y2 = y_origin + box->y2;

	if (rect.x1 <= 0	       &&
	    rect.y1 <= 0	       &&
	    rect.x2 >= drawable->width &&
	    rect.x2 >= drawable->height)
	{
	    if (drawable->backend->swap_buffers (drawable))
	    {
		if (drawable->front)
		{
		    REGION_EMPTY (&drawable->front->drawable_damage);
		    glitz_surface_damage (drawable->front, NULL,
					  GLITZ_DAMAGE_TEXTURE_MASK |
					  GLITZ_DAMAGE_SOLID_MASK);
		}
		return;
	    }
	}
    }

    /* try copy sub buffer (almost as fast) */
    while (n_box)
    {
	rect.x1 = x_origin + box->x1;
	rect.y1 = y_origin + box->y1;
	rect.x2 = x_origin + box->x2;
	rect.y2 = y_origin + box->y2;

	if (rect.x1 < rect.x2 && rect.y1 < rect.y2)
	{
	    x = rect.x1;
	    y = drawable->height - rect.y2;
	    w = rect.x2 - rect.x1;
	    h = rect.y2 - rect.y1;

	    if (!drawable->backend->copy_sub_buffer (drawable, x, y, w, h))
		break;

	    if (drawable->front)
		glitz_surface_damage (drawable->front, &rect,
				      GLITZ_DAMAGE_TEXTURE_MASK |
				      GLITZ_DAMAGE_SOLID_MASK);
	}

	n_box--;
	box++;
    }

    if (!n_box)
	return;

    /* do copy pixels (slow) */
    if (drawable->front)
    {
	if (glitz_surface_push_current (drawable->front,
					GLITZ_DRAWABLE_CURRENT))
	    surface = drawable->front;
    }
    if (!surface)
    {
	if (drawable->backend->push_current (drawable, NULL,
					     GLITZ_DRAWABLE_CURRENT, NULL))
	{
	    drawable->update_all = 1;

	    gl->viewport (0, 0, drawable->width, drawable->height);
	    gl->matrix_mode (GLITZ_GL_PROJECTION);
	    gl->load_identity ();
	    gl->ortho (0.0, drawable->width, 0.0,
		       drawable->height, -1.0, 1.0);
	    gl->matrix_mode (GLITZ_GL_MODELVIEW);
	    gl->load_identity ();
	    gl->scale_f (1.0f, -1.0f, 1.0f);
	    gl->translate_f (0.0f, -drawable->height, 0.0f);
	}
	else
	{
	    drawable->backend->pop_current (drawable);
	    return;
	}
    }

    gl->disable (GLITZ_GL_DITHER);

    drawable->backend->read_buffer (drawable, GLITZ_GL_BACK);
    drawable->backend->draw_buffer (drawable, GLITZ_GL_FRONT);

    glitz_set_operator (gl, GLITZ_OPERATOR_SRC);

    x_pos = 0;
    y_pos = 0;

    glitz_set_raster_pos (gl, x_pos, y_pos);

    while (n_box--)
    {
	rect.x1 = x_origin + box->x1;
	rect.y1 = y_origin + box->y1;
	rect.x2 = x_origin + box->x2;
	rect.y2 = y_origin + box->y2;

	if (rect.x1 < rect.x2 && rect.y1 < rect.y2)
	{
	    x = rect.x1;
	    y = drawable->height - rect.y2;
	    w = rect.x2 - rect.x1;
	    h = rect.y2 - rect.y1;

	    if (x != x_pos || y != y_pos)
	    {
		gl->bitmap (0, 0, 0, 0, x - x_pos, y - y_pos, NULL);

		x_pos = x;
		y_pos = y;
	    }

	    gl->scissor (x, y, w, h);
	    gl->copy_pixels (x, y, w, h, GLITZ_GL_COLOR);

	    if (surface)
		glitz_surface_damage (surface, &rect,
				      GLITZ_DAMAGE_TEXTURE_MASK |
				      GLITZ_DAMAGE_SOLID_MASK);

	    box++;
	}
    }

    drawable->backend->gl->flush ();
    drawable->flushed = 1;

    if (surface)
	glitz_surface_pop_current (surface);
    else
	drawable->backend->pop_current (drawable);
}

void
glitz_drawable_swap_buffers (glitz_drawable_t *drawable)
{
    glitz_box_t box;

    box.x1 = 0;
    box.y1 = 0;
    box.x2 = drawable->width;
    box.y2 = drawable->height;

    glitz_drawable_swap_buffer_region (drawable, 0, 0, &box, 1);
}
slim_hidden_def(glitz_drawable_swap_buffers);

void
glitz_drawable_flush (glitz_drawable_t *drawable)
{
    if (drawable->flushed)
	return;

    drawable->backend->push_current (drawable, NULL, GLITZ_DRAWABLE_CURRENT,
				     NULL);
    drawable->backend->gl->flush ();
    drawable->backend->pop_current (drawable);

    drawable->flushed = 1;
}
slim_hidden_def(glitz_drawable_flush);

void
glitz_drawable_finish (glitz_drawable_t *drawable)
{
    if (drawable->finished)
	return;

    drawable->backend->push_current (drawable, NULL, GLITZ_DRAWABLE_CURRENT,
				     NULL);
    drawable->backend->gl->finish ();
    drawable->backend->pop_current (drawable);

    drawable->finished = drawable->flushed = 1;
}
slim_hidden_def(glitz_drawable_finish);

unsigned long
glitz_drawable_get_features (glitz_drawable_t *drawable)
{
    return drawable->backend->feature_mask;
}
slim_hidden_def(glitz_drawable_get_features);

glitz_drawable_format_t *
glitz_drawable_get_format (glitz_drawable_t *drawable)
{
    return &drawable->format->d;
}
slim_hidden_def(glitz_drawable_get_format);

const char *
glitz_drawable_get_gl_string (glitz_drawable_t  *drawable,
			      glitz_gl_string_t name)
{
    const glitz_gl_ubyte_t *string;
    glitz_gl_enum_t	   gl_name;

    switch (name) {
    case GLITZ_GL_STRING_VENDOR:
	gl_name = GLITZ_GL_VENDOR;
	break;
    case GLITZ_GL_STRING_RENDERER:
	gl_name = GLITZ_GL_RENDERER;
	break;
    case GLITZ_GL_STRING_VERSION:
	gl_name = GLITZ_GL_VERSION;
	break;
    case GLITZ_GL_STRING_EXTENSIONS:
	gl_name = GLITZ_GL_EXTENSIONS;
	break;
    default:
	return NULL;
    }

    drawable->backend->push_current (drawable, NULL, GLITZ_CONTEXT_CURRENT,
				     NULL);
    string = drawable->backend->gl->get_string (gl_name);
    drawable->backend->pop_current (drawable);

    return (const char *) string;
}
slim_hidden_def(glitz_drawable_get_gl_string);