summaryrefslogtreecommitdiff
path: root/src/glitz_context.c
blob: f07e755e836e89c7a0a741f69c32e87d0d8ca676 (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
/*
 * 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"

void
_glitz_context_init (glitz_context_t  *context,
		     glitz_drawable_t *drawable)
{
    glitz_drawable_reference (drawable);

    context->ref_count    = 1;
    context->drawable     = drawable;
    context->closure      = NULL;
    context->lose_current = NULL;
}

void
_glitz_context_fini (glitz_context_t *context)
{
    glitz_drawable_destroy (context->drawable);
}

glitz_context_t *
glitz_context_create (glitz_drawable_t        *drawable,
		      glitz_drawable_format_t *format)
{
    return drawable->backend->create_context (drawable, format);
}
slim_hidden_def(glitz_context_create);

void
glitz_context_destroy (glitz_context_t *context)
{
    if (!context)
	return;

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

    context->drawable->backend->destroy_context (context);
}
slim_hidden_def(glitz_context_destroy);

void
glitz_context_reference (glitz_context_t *context)
{
    if (!context)
	return;

    context->ref_count++;
}
slim_hidden_def(glitz_context_reference);

void
glitz_context_copy (glitz_context_t *src,
		    glitz_context_t *dst,
		    unsigned long   mask)
{
    src->drawable->backend->copy_context (src, dst, mask);
}
slim_hidden_def(glitz_context_copy);

void
glitz_context_set_user_data (glitz_context_t               *context,
			     void                          *closure,
			     glitz_lose_current_function_t lose_current)
{
    context->closure = closure;
    context->lose_current = lose_current;
}
slim_hidden_def(glitz_context_set_user_data);

glitz_function_pointer_t
glitz_context_get_proc_address (glitz_context_t *context,
				const char      *name)
{
    return context->drawable->backend->get_proc_address (context, name);
}
slim_hidden_def(glitz_context_get_proc_address);

void
glitz_context_make_current (glitz_context_t  *context,
			    glitz_drawable_t *drawable)
{
    glitz_lose_current_function_t lose_current;

    lose_current = context->lose_current;
    context->lose_current = 0;

    if (drawable != context->drawable)
    {
	glitz_drawable_reference (drawable);
	glitz_drawable_destroy (context->drawable);
	context->drawable = drawable;
    }

    if (drawable->front)
    {
	if (GLITZ_REGION_NOTEMPTY (&drawable->front->drawable_damage))
	{
	    glitz_surface_push_current (drawable->front,
					GLITZ_DRAWABLE_CURRENT);
	    glitz_surface_pop_current (drawable->front);
	}

	glitz_surface_damage (drawable->front, NULL,
			      GLITZ_DAMAGE_TEXTURE_MASK |
			      GLITZ_DAMAGE_SOLID_MASK);
    }

    if (drawable->back)
    {
	if (GLITZ_REGION_NOTEMPTY (&drawable->back->drawable_damage))
	{
	    glitz_surface_push_current (drawable->back,
					GLITZ_DRAWABLE_CURRENT);
	    glitz_surface_pop_current (drawable->back);
	}

	glitz_surface_damage (drawable->back, NULL,
			      GLITZ_DAMAGE_TEXTURE_MASK |
			      GLITZ_DAMAGE_SOLID_MASK);
    }

    context->lose_current = lose_current;

    drawable->backend->make_current (drawable, context);
}
slim_hidden_def(glitz_context_make_current);

void
glitz_context_bind_texture (glitz_context_t	   *context,
			    glitz_texture_object_t *texture)
{
    glitz_gl_proc_address_list_t *gl = context->drawable->backend->gl;

    if (GLITZ_REGION_NOTEMPTY (&texture->surface->texture_damage))
    {
	glitz_lose_current_function_t lose_current;

	lose_current = context->lose_current;
	context->lose_current = 0;

	glitz_surface_push_current (texture->surface, GLITZ_CONTEXT_CURRENT);
	_glitz_surface_sync_texture (texture->surface);
	glitz_surface_pop_current (texture->surface);

	context->lose_current = lose_current;

	glitz_context_make_current (context, context->drawable);
    }

    gl->bind_texture (texture->surface->texture.target,
		      texture->surface->texture.name);

    glitz_texture_ensure_parameters (gl,
				     &texture->surface->texture,
				     &texture->param);
}
slim_hidden_def(glitz_context_bind_texture);

void
glitz_context_draw_buffers (glitz_context_t	          *context,
			    const glitz_drawable_buffer_t *buffers,
			    int				  n)
{
    unsigned int mask = 0;

#define FRONT_BIT (1 << 0)
#define BACK_BIT  (1 << 1)

    while (n--)
    {
	switch (*buffers++) {
	case GLITZ_DRAWABLE_BUFFER_FRONT_COLOR:
	    mask |= FRONT_BIT;
	    break;
	case GLITZ_DRAWABLE_BUFFER_BACK_COLOR:
	    mask |= BACK_BIT;
	default:
	    break;
	}
    }

    if (mask)
    {
	static const glitz_gl_enum_t mode[] = {
	    GLITZ_GL_FRONT,
	    GLITZ_GL_BACK,
	    GLITZ_GL_FRONT_AND_BACK
	};

	context->drawable->backend->draw_buffer (context->drawable,
						 mode[mask - 1]);
    }

#undef FRONT_BIT
#undef BACK_BIT

}
slim_hidden_def(glitz_context_draw_buffers);

void
glitz_context_read_buffer (glitz_context_t		 *context,
			   const glitz_drawable_buffer_t buffer)
{
    switch (buffer) {
    case GLITZ_DRAWABLE_BUFFER_FRONT_COLOR:
	context->drawable->backend->read_buffer (context->drawable,
						 GLITZ_GL_FRONT);
	break;
    case GLITZ_DRAWABLE_BUFFER_BACK_COLOR:
	context->drawable->backend->read_buffer (context->drawable,
						 GLITZ_GL_BACK);
    default:
	break;
    }
}
slim_hidden_def(glitz_context_read_buffer);