summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/glamo/glamo_context.c
blob: 5f435028b791dc937c91932455705c3cb60a6ed1 (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
/*
 * Direct Rendering Support for SMedia Glamo 336x/337x
 *
 * (c) 2009 Thomas White <taw@bitwiz.org.uk>
 * Roughly based on sis_context.c (c) 2003 Eric Anholt
 *              and radeon_common_context.c
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#include "dri_util.h"
#include "drirenderbuffer.h"
#include "utils.h"

#include "swrast/swrast.h"
#include "swrast_setup/swrast_setup.h"
#include "drivers/common/driverfuncs.h"
#include "vbo/vbo.h"
#include "tnl/tnl.h"
#include "tnl/t_pipeline.h"
#include "main/state.h"

#include "glamo_context.h"
#include "glamo_screen.h"
#include "glamo_state.h"
#include "glamo_fbo.h"
#include "glamo_tris.h"
#include "glamo_render.h"
#include "glamo_cmdq.h"

#include <glamo_bo.h>
#include <glamo_bo_gem.h>
#include <glamo_drm.h>


#define DRIVER_DATE  "20090913"


static inline struct glamo_renderbuffer *glamo_get_renderbuffer(
                                                      struct gl_framebuffer *fb,
                                                      int att_index)
{
	if ( att_index >= 0 ) {
		struct glamo_renderbuffer *gr;
		gr = glamo_renderbuffer(fb->Attachment[att_index].Renderbuffer);
		return gr;
	} else {
		return NULL;
	}
}


static const GLubyte *glamoGetString(GLcontext *ctx, GLenum name)
{
	static char buffer[128];

	switch (name) {
	case GL_VENDOR:
		return (GLubyte *)"Thomas White";
	case GL_RENDERER: {
		driGetRendererString(buffer, "Glamo", DRIVER_DATE, 0);
		return (GLubyte *) buffer;
	}
	default:
		return 0;
	}
}


/* Called when Mesa needs to know the size of the framebuffer */
static void glamoBufferSize(GLframebuffer *buffer,
                            GLuint *width, GLuint *height)
{
	GET_CURRENT_CONTEXT(ctx);
	glamoContextPtr glamo = GLAMO_CONTEXT(ctx);

	*width = glamo->driDrawable->w;
	*height = glamo->driDrawable->h;
}


GLboolean glamoCreateContext(const __GLcontextModes *glVisual,
                             __DRIcontext *driContextPriv,
                             void *sharedContextPrivate)
{
	GLcontext *ctx, *shareCtx;
	__DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
	glamoContextPtr context;
	glamoScreenPtr glamoScreen;
	struct dd_function_table functions;

	context = (glamoContextPtr)CALLOC(sizeof(*context));
	if ( context == NULL ) return GL_FALSE;

	_mesa_init_driver_functions(&functions);

	/* Allocate the Mesa context */
	if ( sharedContextPrivate )
		shareCtx = ((glamoContextPtr)sharedContextPrivate)->glCtx;
	else
		shareCtx = NULL;
	context->glCtx = _mesa_create_context(glVisual, shareCtx,
		                        &functions, (void *)context);
	if ( context->glCtx == NULL ) {
		FREE(context);
		return GL_FALSE;
	}
	driContextPriv->driverPrivate = context;
	ctx = context->glCtx;

	glamoScreen = context->glamoScreen = (glamoScreenPtr)sPriv->private;

	ctx->Driver.GetString = glamoGetString;
	ctx->Driver.GetBufferSize = glamoBufferSize;

	context->driContext = driContextPriv;
	context->driScreen = sPriv;
	context->driDrawable = NULL;
	context->drm_fd = sPriv->fd;

	/* Initialize the software rasterizer and helper modules. */
	_swrast_CreateContext(ctx);
	_vbo_CreateContext(ctx);
	_tnl_CreateContext(ctx);
	_swsetup_CreateContext(ctx);

	/* Install our pipeline (see glamo_render.c) */
	_tnl_install_pipeline(ctx, glamo_pipeline);

	_swrast_allow_pixel_fog(ctx, GL_TRUE);
	_swrast_allow_vertex_fog(ctx, GL_FALSE);
	_tnl_allow_pixel_fog(ctx, GL_TRUE);
	_tnl_allow_vertex_fog(ctx, GL_FALSE);

	glamoInitCmdqCache(context);
	glamoInitStateFuncs(ctx);
	glamoInitTriFuncs(ctx);

	return GL_TRUE;
}


void glamoDestroyContext(__DRIcontext *driContextPriv)
{
	glamoContextPtr context;

	context = (glamoContextPtr)driContextPriv->driverPrivate;
	assert(context != NULL);

	if ( context != NULL ) {

		_swsetup_DestroyContext(context->glCtx);
		_tnl_DestroyContext(context->glCtx);
		_vbo_DestroyContext(context->glCtx);
		_swrast_DestroyContext(context->glCtx);

		_mesa_destroy_context(context->glCtx);

	}

	FREE(context);
}


void glamo_update_renderbuffers(__DRIcontext *context,
                                __DRIdrawable *drawable)
{
	unsigned int attachments[10];
	__DRIbuffer *buffers;
	__DRIscreen *screen;
	int i, count;
	struct glamo_framebuffer *draw;
	glamoContextPtr glamo;
	struct glamo_bo *bo;

	draw = drawable->driverPrivate;
	screen = context->driScreenPriv;
	glamo = (glamoContextPtr)context->driverPrivate;
	i = 0;
	if ( draw->color_rb[0] ) {
		attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
	}
	if ( draw->color_rb[1] ) {
		attachments[i++] = __DRI_BUFFER_BACK_LEFT;
	}

	buffers = screen->dri2.loader->getBuffers(drawable,
	                                          &drawable->w,
	                                          &drawable->h,
	                                          attachments, i,
	                                          &count,
	                                          drawable->loaderPrivate);
	if ( buffers == NULL ) return;

	/* Set one cliprect to cover the whole drawable */
	drawable->x = 0;
	drawable->y = 0;
	drawable->backX = 0;
	drawable->backY = 0;
	drawable->numClipRects = 1;
	drawable->pClipRects[0].x1 = 0;
	drawable->pClipRects[0].y1 = 0;
	drawable->pClipRects[0].x2 = drawable->w;
	drawable->pClipRects[0].y2 = drawable->h;
	drawable->numBackClipRects = 1;
	drawable->pBackClipRects[0].x1 = 0;
	drawable->pBackClipRects[0].y1 = 0;
	drawable->pBackClipRects[0].x2 = drawable->w;
	drawable->pBackClipRects[0].y2 = drawable->h;

	/* For each attachment */
	for ( i=0; i<count; i++ ) {

		struct glamo_renderbuffer *grb;

		switch ( buffers[i].attachment ) {
		case __DRI_BUFFER_FRONT_LEFT:
			grb = draw->color_rb[0];
			break;
		case __DRI_BUFFER_BACK_LEFT:
			grb = draw->color_rb[1];
			break;
		case __DRI_BUFFER_DEPTH:
			grb = glamo_get_renderbuffer(&draw->base, BUFFER_DEPTH);
			break;
		case __DRI_BUFFER_STENCIL:
			grb = glamo_get_renderbuffer(&draw->base,
			                                        BUFFER_STENCIL);
			break;
		case __DRI_BUFFER_FAKE_FRONT_LEFT:
			grb = draw->color_rb[0];
			break;
		case __DRI_BUFFER_ACCUM:
		default:
			fprintf(stderr,
				"Unhandled buffer attach event,"
				" attachment type %d\n", buffers[i].attachment);
			return;
		}

		if ( grb == NULL ) {
			/* Don't know how to handle this type of buffer */
			continue;
		}

		if ( grb->bo ) {
			uint32_t name = glamo_gem_get_name(grb->bo);
			if ( name == buffers[i].name ) {
				/* Buffer already attached.  No action needed */
				continue;
			}
		}

		grb->cpp = buffers[i].cpp;
		grb->pitch = buffers[i].pitch;
		grb->width = drawable->w;
		grb->height = drawable->h;

		bo = glamo_bo_open(glamo->glamoScreen->bom, buffers[i].name,
		                   0, 0, GLAMO_GEM_DOMAIN_VRAM,
		                   buffers[i].flags);
		if ( bo == NULL ) {
			fprintf(stderr, "Failed to attach buffer %d\n",
				buffers[i].name);
		}

		glamo_renderbuffer_set_bo(grb, bo);
		glamo_bo_unref(bo);

	}

	driUpdateFramebufferSize(glamo->glCtx, drawable);
}


GLboolean glamoMakeCurrent(__DRIcontext *driContextPriv,
                           __DRIdrawable *driDrawPriv,
                           __DRIdrawable *driReadPriv)
{
	struct glamo_framebuffer *draw_fb;
	struct gl_framebuffer *read_fb;
	glamoContextPtr glamo;

	if ( driContextPriv == NULL ) {
		_mesa_make_current(NULL, NULL, NULL);
		return GL_TRUE;
	}

	/* The Glamo context we're switching to */
	glamo = (glamoContextPtr)driContextPriv->driverPrivate;

	glamo->driDrawable = driDrawPriv;

	/* These two will probably be the same */
	draw_fb = (struct glamo_framebuffer *)driDrawPriv->driverPrivate;
	read_fb = (struct gl_framebuffer *)driReadPriv->driverPrivate;

	glamo_update_renderbuffers(driContextPriv, driDrawPriv);
	if (driDrawPriv != driReadPriv)
		glamo_update_renderbuffers(driContextPriv, driReadPriv);

	_mesa_make_current(glamo->glCtx, &draw_fb->base, read_fb);
	_mesa_update_state(glamo->glCtx);

	return GL_TRUE;
}


GLboolean glamoUnbindContext(__DRIcontext *driContextPriv)
{
	return GL_TRUE;
}