summaryrefslogtreecommitdiff
path: root/xc/lib/GL/mesa/src/drv/gamma/gamma_xmesa.c
blob: 3c1fe64fc1c6b8db65f6cc7abae80cc70bd8c408 (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* $XFree86: xc/lib/GL/mesa/src/drv/gamma/gamma_xmesa.c,v 1.4 2000/03/02 16:07:36 martin Exp $ */
/**************************************************************************

Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
All Rights Reserved.

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, sub license, 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 (including the
next paragraph) 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 NON-INFRINGEMENT.
IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.

**************************************************************************/

/*
 * Authors:
 *   Kevin E. Martin <kevin@precisioninsight.com>
 *   Brian Paul <brian@precisioninsight.com>
 *   Alan Hourihane <Alan.Hourihane@btinternet.com>
 */

#ifdef GLX_DIRECT_RENDERING

#include <X11/Xlibint.h>
#include "gamma_init.h"
#include "gamma_gl.h"
#include "glapi.h"
#include "glint_dri.h"
#include "context.h"
#include "mmath.h"


__DRIcontextPrivate *nullCC  = NULL;
__DRIcontextPrivate *gCC = NULL;
gammaContextPrivate *gCCPriv = NULL;

static struct _glapi_table *Dispatch = NULL;


GLboolean XMesaInitDriver(__DRIscreenPrivate *driScrnPriv)
{
    gammaScreenPrivate *gsp;

    /* Allocate the private area */
    gsp = (gammaScreenPrivate *)Xmalloc(sizeof(gammaScreenPrivate));
    if (!gsp) {
	return GL_FALSE;
    }
    gsp->driScrnPriv = driScrnPriv;

    driScrnPriv->private = (void *)gsp;

    if (!gammaMapAllRegions(driScrnPriv)) {
	Xfree(driScrnPriv->private);
	return GL_FALSE;
    }

    return GL_TRUE;
}

void XMesaResetDriver(__DRIscreenPrivate *driScrnPriv)
{
    gammaUnmapAllRegions(driScrnPriv);
    Xfree(driScrnPriv->private);
}

GLvisual *XMesaCreateVisual(Display *dpy,
                            __DRIscreenPrivate *driScrnPriv,
                            const XVisualInfo *visinfo,
                            const __GLXvisualConfig *config)
{
   /* Drivers may change the args to _mesa_create_visual() in order to
    * setup special visuals.
    */
   return _mesa_create_visual( config->rgba,
                               config->doubleBuffer,
                               config->stereo,
                               _mesa_bitcount(visinfo->red_mask),
                               _mesa_bitcount(visinfo->green_mask),
                               _mesa_bitcount(visinfo->blue_mask),
                               config->alphaSize,
                               0, /* index bits */
                               config->depthSize,
                               config->stencilSize,
                               config->accumRedSize,
                               config->accumGreenSize,
                               config->accumBlueSize,
                               config->accumAlphaSize,
                               0 /* num samples */ );
}


GLboolean XMesaCreateContext( Display *dpy,
                              GLvisual *mesaVis,
                              __DRIcontextPrivate *driContextPriv )
{
    int i;
    gammaContextPrivate *cPriv;
    __DRIscreenPrivate *driScrnPriv = driContextPriv->driScreenPriv;
    gammaScreenPrivate *gPriv = (gammaScreenPrivate *)driScrnPriv->private;
    GLINTDRIPtr         gDRIPriv = (GLINTDRIPtr)driScrnPriv->pDevPriv;

    if (!Dispatch) {
       GLuint size = _glapi_get_dispatch_table_size() * sizeof(GLvoid *);
       Dispatch = (struct _glapi_table *) malloc(size);
       _gamma_init_dispatch(Dispatch);
    }

    cPriv = (gammaContextPrivate *)Xmalloc(sizeof(gammaContextPrivate));
    if (!cPriv) {
	return GL_FALSE;
    }

    cPriv->hHWContext = driContextPriv->hHWContext;
    GET_FIRST_DMA(driScrnPriv->fd, cPriv->hHWContext,
		  1, &cPriv->bufIndex, &cPriv->bufSize,
		  &cPriv->buf, &cPriv->bufCount, gPriv);

#ifdef DO_VALIDATE
    GET_FIRST_DMA(driScrnPriv->fd, cPriv->hHWContext,
		  1, &cPriv->WCbufIndex, &cPriv->WCbufSize,
		  &cPriv->WCbuf, &cPriv->WCbufCount, gPriv);
#endif

    cPriv->ClearColor[0] = 0.0;
    cPriv->ClearColor[1] = 0.0;
    cPriv->ClearColor[2] = 0.0;
    cPriv->ClearColor[3] = 1.0;
    cPriv->ClearDepth = 1.0;
    cPriv->x = 0;
    cPriv->y = 0;
    cPriv->w = 0;
    cPriv->h = 0;
    cPriv->FrameCount = 0;
    cPriv->MatrixMode = GL_MODELVIEW;
    cPriv->ModelViewCount = 0;
    cPriv->ProjCount = 0;
    cPriv->TextureCount = 0;

    for (i = 0; i < 16; i++)
	if (i % 5 == 0)
	    cPriv->ModelView[i] =
		cPriv->Proj[i] =
		cPriv->ModelViewProj[i] =
		cPriv->Texture[i] = 1.0;
	else
	    cPriv->ModelView[i] =
		cPriv->Proj[i] =
		cPriv->ModelViewProj[i] =
		cPriv->Texture[i] = 0.0;

    cPriv->LBReadMode = (LBReadSrcDisable |
			 LBReadDstDisable |
			 LBDataTypeDefault |
			 LBWindowOriginBot |
			 gDRIPriv->pprod);
    cPriv->FBReadMode = (FBReadSrcDisable |
			 FBReadDstDisable |
			 FBDataTypeDefault |
			 FBWindowOriginBot |
			 gDRIPriv->pprod);
 
    if (gDRIPriv->numMXDevices == 2) {
	cPriv->LBReadMode |= LBScanLineInt2;
	cPriv->FBReadMode |= FBScanLineInt2;
    	cPriv->FBWindowBase =driScrnPriv->fbWidth*(driScrnPriv->fbHeight/2 - 1);
    	cPriv->LBWindowBase =driScrnPriv->fbWidth*(driScrnPriv->fbHeight/2 - 1);
    } else {
    	cPriv->FBWindowBase = driScrnPriv->fbWidth * driScrnPriv->fbHeight;
    	cPriv->LBWindowBase = driScrnPriv->fbWidth * driScrnPriv->fbHeight;
    }

    cPriv->Begin = (B_AreaStippleDisable |
		    B_LineStippleDisable |
		    B_AntiAliasDisable |
		    B_TextureDisable |
		    B_FogDisable |
		    B_SubPixelCorrectEnable |
		    B_PrimType_Null);

    cPriv->ColorDDAMode = (ColorDDAEnable |
			   ColorDDAGouraud);

#ifdef CULL_ALL_PRIMS
    cPriv->GeometryMode = (GM_TextureDisable |
			   GM_FogDisable |
			   GM_FogExp |
			   GM_FrontPolyFill |
			   GM_BackPolyFill |
			   GM_FrontFaceCCW |
			   GM_PolyCullDisable |
			   GM_PolyCullBoth |
			   GM_ClipShortLinesDisable |
			   GM_ClipSmallTrisDisable |
			   GM_RenderMode |
			   GM_Feedback2D |
			   GM_CullFaceNormDisable |
			   GM_AutoFaceNormDisable |
			   GM_GouraudShading |
			   GM_UserClipNone |
			   GM_PolyOffsetPointDisable |
			   GM_PolyOffsetLineDisable |
			   GM_PolyOffsetFillDisable |
			   GM_InvertFaceNormCullDisable);
#else
    cPriv->GeometryMode = (GM_TextureDisable |
			   GM_FogDisable |
			   GM_FogExp |
			   GM_FrontPolyFill |
			   GM_BackPolyFill |
			   GM_FrontFaceCCW |
			   GM_PolyCullDisable |
			   GM_PolyCullBack |
			   GM_ClipShortLinesDisable |
			   GM_ClipSmallTrisDisable |
			   GM_RenderMode |
			   GM_Feedback2D |
			   GM_CullFaceNormDisable |
			   GM_AutoFaceNormDisable |
			   GM_GouraudShading |
			   GM_UserClipNone |
			   GM_PolyOffsetPointDisable |
			   GM_PolyOffsetLineDisable |
			   GM_PolyOffsetFillDisable |
			   GM_InvertFaceNormCullDisable);
#endif

    cPriv->AlphaTestMode = (AlphaTestModeDisable |
			    AT_Always);

    cPriv->AlphaBlendMode = (AlphaBlendModeDisable |
			     AB_Src_One |
			     AB_Dst_Zero |
			     AB_ColorFmt_8888 |
			     AB_NoAlphaBufferPresent |
			     AB_ColorOrder_RGB |
			     AB_OpenGLType |
			     AB_AlphaDst_FBData |
			     AB_ColorConversionScale |
			     AB_AlphaConversionScale);

    cPriv->AB_FBReadMode_Save = cPriv->AB_FBReadMode = 0;

    cPriv->Window = (WindowEnable  | /* For GID testing */
		     W_PassIfEqual |
		     (0 << 5)); /* GID part is set from draw priv (below) */

    cPriv->NotClipped = GL_FALSE;
    cPriv->WindowChanged = GL_TRUE;

    /*
    ** NOT_DONE:
    ** 1. These values should be calculated from the registers.
    ** 2. Only one client can use texture memory at this time.
    ** 3. A two-tiered texture allocation routine is needed to properly
    **    handle texture management.
    */
    cPriv->tmm = driTMMCreate(0x00080000,
			      0x00800000 - 0x00080000,
			      4, 1,
			      gammaTOLoad,
			      gammaTOLoadSub);

    cPriv->curTexObj = gammaTOFind(0);
    cPriv->curTexObj1D = cPriv->curTexObj;
    cPriv->curTexObj2D = cPriv->curTexObj;
    cPriv->Texture1DEnabled = GL_FALSE;
    cPriv->Texture2DEnabled = GL_FALSE;

#ifdef FORCE_DEPTH32
    cPriv->DepthSize = 32;
#else
    cPriv->DepthSize = mesaVis->DepthBits;
#endif
    cPriv->zNear = 0.0;
    cPriv->zFar  = 1.0;

    cPriv->Flags  = GAMMA_FRONT_BUFFER;
    cPriv->Flags |= (mesaVis->DBflag ? GAMMA_BACK_BUFFER  : 0);
    cPriv->Flags |= (cPriv->DepthSize > 0 ? GAMMA_DEPTH_BUFFER : 0);

    cPriv->EnabledFlags = GAMMA_FRONT_BUFFER;
    cPriv->EnabledFlags |= (mesaVis->DBflag ? GAMMA_BACK_BUFFER  : 0);

    cPriv->DepthMode = (DepthModeDisable |
			DM_WriteMask |
			DM_Less);

    cPriv->DeltaMode = (DM_SubPixlCorrectionEnable |
			DM_SmoothShadingEnable |
			DM_Target500TXMX);

    switch (cPriv->DepthSize) {
    case 16:
	cPriv->DeltaMode |= DM_Depth16;
	break;
    case 24:
	cPriv->DeltaMode |= DM_Depth24;
	break;
    case 32:
	cPriv->DeltaMode |= DM_Depth32;
	break;
    default:
	break;
    }

    cPriv->gammaScrnPriv = gPriv;

    cPriv->LightingMode = LightingModeDisable;
    cPriv->Light0Mode = LNM_Off;
    cPriv->Light1Mode = LNM_Off;

    cPriv->MaterialMode = MaterialModeDisable;

    cPriv->ScissorMode = UserScissorDisable | ScreenScissorDisable;

    driContextPriv->driverPrivate = cPriv;

    /* Initialize the HW to a known state */
    gammaInitHW(cPriv);

    return GL_TRUE;
}

void XMesaDestroyContext(__DRIcontextPrivate *driContextPriv)
{
    gammaContextPrivate *cPriv;
    cPriv = (gammaContextPrivate *) driContextPriv->driverPrivate;
    if (cPriv) {
       /* XXX free driver context data? */
    }
}


GLframebuffer *XMesaCreateWindowBuffer( Display *dpy,
                                        __DRIscreenPrivate *driScrnPriv,
                                        __DRIdrawablePrivate *driDrawPriv,
                                        GLvisual *mesaVis)
{
   return gl_create_framebuffer(mesaVis,
                                GL_FALSE,  /* software depth buffer? */
                                mesaVis->StencilBits > 0,
                                mesaVis->AccumRedBits > 0,
                                mesaVis->AlphaBits > 0
                                );
}


GLframebuffer *XMesaCreatePixmapBuffer( Display *dpy,
                                        __DRIscreenPrivate *driScrnPriv,
                                        __DRIdrawablePrivate *driDrawPriv,
                                        GLvisual *mesaVis)
{
#if 0
   /* Different drivers may have different combinations of hardware and
    * software ancillary buffers.
    */
   return gl_create_framebuffer(mesaVis,
                                GL_FALSE,  /* software depth buffer? */
                                mesaVis->StencilBits > 0,
                                mesaVis->AccumRedBits > 0,
                                mesaVis->AlphaBits > 0
                                );
#else
   return NULL;  /* not implemented yet */
#endif
}


void XMesaSwapBuffers(__DRIdrawablePrivate *driDrawPriv)
{
    /*
    ** NOT_DONE: This assumes buffer is currently bound to a context.
    ** This needs to be able to swap buffers when not currently bound.
    */
    if (gCC == NULL || gCCPriv == NULL)
	return;

    VALIDATE_DRAWABLE_INFO(gCC,gCCPriv);

    /* Flush any partially filled buffers */
    FLUSH_DMA_BUFFER(gCC, gCCPriv);

    if (gCCPriv->EnabledFlags & GAMMA_BACK_BUFFER) {
	int src, dst, x0, y0, x1, h;
	int i;
	int nRect = driDrawPriv->numClipRects;
	XF86DRIClipRectPtr pRect = driDrawPriv->pClipRects;
	__DRIscreenPrivate *driScrnPriv = gCC->driScreenPriv;

#ifdef DO_VALIDATE
	DRM_SPINLOCK(&driScrnPriv->pSAREA->drawable_lock,
		     driScrnPriv->drawLockID);
	VALIDATE_DRAWABLE_INFO_NO_LOCK(gCC,gCCPriv);
#endif

	CHECK_DMA_BUFFER(nullCC, gCCPriv, 2);
	WRITE(gCCPriv->buf, FBReadMode, (gCCPriv->FBReadMode |
					 FBReadSrcEnable));
	WRITE(gCCPriv->buf, LBWriteMode, LBWriteModeDisable);

	for (i = 0; i < nRect; i++, pRect++) {
	    x0 = pRect->x1;
	    x1 = pRect->x2;
	    h  = pRect->y2 - pRect->y1;
	    y0 = driScrnPriv->fbHeight - (pRect->y1+h);

	    src = (y0/2)*driScrnPriv->fbWidth+x0;
	    y0 += driScrnPriv->fbHeight;
	    dst = (y0/2)*driScrnPriv->fbWidth+x0;

	    CHECK_DMA_BUFFER(nullCC, gCCPriv, 9);
	    WRITE(gCCPriv->buf, StartXDom,       x0<<16);   /* X0dest */
	    WRITE(gCCPriv->buf, StartY,          y0<<16);   /* Y0dest */
	    WRITE(gCCPriv->buf, StartXSub,       x1<<16);   /* X1dest */
	    WRITE(gCCPriv->buf, GLINTCount,      h);        /* H */
	    WRITE(gCCPriv->buf, dY,              1<<16);    /* ydir */
	    WRITE(gCCPriv->buf, dXDom,           0<<16);
	    WRITE(gCCPriv->buf, dXSub,           0<<16);
	    WRITE(gCCPriv->buf, FBSourceOffset, (dst-src));
	    WRITE(gCCPriv->buf, Render,          0x00040048); /* NOT_DONE */
	}

	/*
	** NOTE: FBSourceOffset (above) is backwards from what is
	** described in the manual (i.e., dst-src instead of src-dst)
	** due to our using the bottom-left window origin instead of the
	** top-left window origin.
	*/

	/* Restore FBReadMode */
	CHECK_DMA_BUFFER(nullCC, gCCPriv, 2);
	WRITE(gCCPriv->buf, FBReadMode, (gCCPriv->FBReadMode |
					 gCCPriv->AB_FBReadMode));
	WRITE(gCCPriv->buf, LBWriteMode, LBWriteModeEnable);

#ifdef DO_VALIDATE
	PROCESS_DMA_BUFFER_TOP_HALF(gCCPriv);

	DRM_SPINUNLOCK(&driScrnPriv->pSAREA->drawable_lock,
		       driScrnPriv->drawLockID);
	VALIDATE_DRAWABLE_INFO_NO_LOCK_POST(gCC,gCCPriv);

	PROCESS_DMA_BUFFER_BOTTOM_HALF(gCCPriv);
#else
	FLUSH_DMA_BUFFER(gCC,gCCPriv);
#endif

    }
}

GLboolean XMesaMakeCurrent(__DRIcontextPrivate *driContextPriv,
                           __DRIdrawablePrivate *driDrawPriv,
                           __DRIdrawablePrivate *driReadPriv)
{
    if (driContextPriv) {
	gCC     = driContextPriv;
	gCCPriv = (gammaContextPrivate *) driContextPriv->driverPrivate;

	gCCPriv->Window &= ~W_GIDMask;
	gCCPriv->Window |= (driDrawPriv->index << 5);

	CHECK_DMA_BUFFER(gCC, gCCPriv, 1);
	WRITE(gCCPriv->buf, GLINTWindow, gCCPriv->Window);

        _glapi_set_dispatch(Dispatch);

	_gamma_Viewport(0, 0, driDrawPriv->w, driDrawPriv->h);
    } else {
	gCC     = NULL;
	gCCPriv = NULL;
    }
    return GL_TRUE;
}


GLboolean XMesaUnbindContext( __DRIcontextPrivate *driContextPriv )
{
   /* XXX not 100% sure what's supposed to be done here */
   return GL_TRUE;
}


void __driRegisterExtensions(void)
{
   /* No extensions */
}


#endif