summaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/i810/i810ioctl.c
blob: 623d50c5cbe69990f78e13da3abf5060cb5c827e (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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519

#include <unistd.h> /* for usleep() */

#include "main/glheader.h"
#include "main/mtypes.h"
#include "main/macros.h"
#include "main/dd.h"
#include "swrast/swrast.h"
#include "main/mm.h"

#include "i810screen.h"
#include "i810_dri.h"

#include "main/context.h"
#include "i810ioctl.h"
#include "i810state.h"

static drmBufPtr i810_get_buffer_ioctl( i810ContextPtr imesa )
{
   drmI810DMA dma;
   drmBufPtr buf;
   int retcode, i = 0;
   
   while (1) {
      retcode = drmCommandWriteRead(imesa->driFd, DRM_I810_GETBUF,
                                    &dma, sizeof(drmI810DMA));

      if (dma.granted == 1 && retcode == 0) 
	 break;
      
      if (++i > 1000) {
	 drmCommandNone(imesa->driFd, DRM_I810_FLUSH);
	 i = 0;
      }
   }

   buf = &(imesa->i810Screen->bufs->list[dma.request_idx]);
   buf->idx = dma.request_idx;
   buf->used = 0;
   buf->total = dma.request_size;
   buf->address = (drmAddress)dma.virtual;

   return buf;
}



#define DEPTH_SCALE ((1<<16)-1)

static void i810Clear( GLcontext *ctx, GLbitfield mask )
{
   i810ContextPtr imesa = I810_CONTEXT( ctx );
   __DRIdrawable *dPriv = imesa->driDrawable;
   const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
   drmI810Clear clear;
   unsigned int i;

   clear.flags = 0;
   clear.clear_color = imesa->ClearColor;
   clear.clear_depth = (GLuint) (ctx->Depth.Clear * DEPTH_SCALE);

   I810_FIREVERTICES( imesa );
	
   if ((mask & BUFFER_BIT_FRONT_LEFT) && colorMask == ~0U) {
      clear.flags |= I810_FRONT;
      mask &= ~BUFFER_BIT_FRONT_LEFT;
   }

   if ((mask & BUFFER_BIT_BACK_LEFT) && colorMask == ~0U) {
      clear.flags |= I810_BACK;
      mask &= ~BUFFER_BIT_BACK_LEFT;
   }

   if (mask & BUFFER_BIT_DEPTH) {
      if (ctx->Depth.Mask) 
	 clear.flags |= I810_DEPTH;
      mask &= ~BUFFER_BIT_DEPTH;
   }

   if (clear.flags) {
      GLint cx, cy, cw, ch;

      LOCK_HARDWARE( imesa );

      /* compute region after locking: */
      cx = ctx->DrawBuffer->_Xmin;
      cy = ctx->DrawBuffer->_Ymin;
      cw = ctx->DrawBuffer->_Xmax - cx;
      ch = ctx->DrawBuffer->_Ymax - cy;

      /* flip top to bottom */
      cy = dPriv->h-cy-ch;
      cx += imesa->drawX;
      cy += imesa->drawY;

      for (i = 0 ; i < imesa->numClipRects ; ) 
      { 	 
	 unsigned int nr = MIN2(i + I810_NR_SAREA_CLIPRECTS, imesa->numClipRects);
	 drm_clip_rect_t *box = imesa->pClipRects;	 
	 drm_clip_rect_t *b = (drm_clip_rect_t *)imesa->sarea->boxes;
	 int n = 0;

	 if (cw != dPriv->w || ch != dPriv->h) {
            /* clear sub region */
	    for ( ; i < nr ; i++) {
	       GLint x = box[i].x1;
	       GLint y = box[i].y1;
	       GLint w = box[i].x2 - x;
	       GLint h = box[i].y2 - y;

	       if (x < cx) w -= cx - x, x = cx; 
	       if (y < cy) h -= cy - y, y = cy;
	       if (x + w > cx + cw) w = cx + cw - x;
	       if (y + h > cy + ch) h = cy + ch - y;
	       if (w <= 0) continue;
	       if (h <= 0) continue;

	       b->x1 = x;
	       b->y1 = y;
	       b->x2 = x + w;
	       b->y2 = y + h;
	       b++;
	       n++;
	    }
	 } else {
            /* clear whole buffer */
	    for ( ; i < nr ; i++) {
	       *b++ = box[i];
	       n++;
	    }
	 }

	 imesa->sarea->nbox = n;
         drmCommandWrite(imesa->driFd, DRM_I810_CLEAR,
                         &clear, sizeof(drmI810Clear));
      }

      UNLOCK_HARDWARE( imesa );
      imesa->upload_cliprects = GL_TRUE;
   }

   if (mask) 
      _swrast_Clear( ctx, mask );
}




/*
 * Copy the back buffer to the front buffer. 
 */
void i810CopyBuffer( const __DRIdrawable *dPriv ) 
{
   i810ContextPtr imesa;
   drm_clip_rect_t *pbox;
   int nbox, i, tmp;

   assert(dPriv);
   assert(dPriv->driContextPriv);
   assert(dPriv->driContextPriv->driverPrivate);

   imesa = (i810ContextPtr) dPriv->driContextPriv->driverPrivate;

   I810_FIREVERTICES( imesa );
   LOCK_HARDWARE( imesa );
   
   pbox = (drm_clip_rect_t *)dPriv->pClipRects;
   nbox = dPriv->numClipRects;

   for (i = 0 ; i < nbox ; )
   {
      int nr = MIN2(i + I810_NR_SAREA_CLIPRECTS, dPriv->numClipRects);
      drm_clip_rect_t *b = (drm_clip_rect_t *)imesa->sarea->boxes;

      imesa->sarea->nbox = nr - i;

      for ( ; i < nr ; i++) 
	 *b++ = pbox[i];

      drmCommandNone(imesa->driFd, DRM_I810_SWAP);
   }

   tmp = GET_ENQUEUE_AGE(imesa);
   UNLOCK_HARDWARE( imesa );

   /* multiarb will suck the life out of the server without this throttle:
    */
   if (GET_DISPATCH_AGE(imesa) < imesa->lastSwap) {
      i810WaitAge(imesa, imesa->lastSwap);
   }

   imesa->lastSwap = tmp;
   imesa->upload_cliprects = GL_TRUE;
}


/*
 * XXX implement when full-screen extension is done.
 */
void i810PageFlip( const __DRIdrawable *dPriv ) 
{
  i810ContextPtr imesa;
  int tmp, ret;

  assert(dPriv);
  assert(dPriv->driContextPriv);
  assert(dPriv->driContextPriv->driverPrivate);
    
  imesa = (i810ContextPtr) dPriv->driContextPriv->driverPrivate;

  I810_FIREVERTICES( imesa );
  LOCK_HARDWARE( imesa );
  
  if (dPriv->pClipRects) {
     memcpy(&(imesa->sarea->boxes[0]), &(dPriv->pClipRects[0]),
            sizeof(drm_clip_rect_t));
     imesa->sarea->nbox = 1;
  }
  ret = drmCommandNone(imesa->driFd, DRM_I810_FLIP);
  if (ret) {
    fprintf(stderr, "%s: %d\n", __FUNCTION__, ret);
    UNLOCK_HARDWARE( imesa );
    exit(1);
  }

  tmp = GET_ENQUEUE_AGE(imesa);
  UNLOCK_HARDWARE( imesa );
  
   /* multiarb will suck the life out of the server without this throttle:
    */
  if (GET_DISPATCH_AGE(imesa) < imesa->lastSwap) {
    i810WaitAge(imesa, imesa->lastSwap);
   }

  /*  i810SetDrawBuffer( imesa->glCtx, imesa->glCtx->Color.DriverDrawBuffer );*/
  i810DrawBuffer( imesa->glCtx, imesa->glCtx->Color.DrawBuffer[0] );
  imesa->upload_cliprects = GL_TRUE;
  imesa->lastSwap = tmp;
  return;
}


/* This waits for *everybody* to finish rendering -- overkill.
 */
void i810DmaFinish( i810ContextPtr imesa  ) 
{
   I810_FIREVERTICES( imesa );

   LOCK_HARDWARE( imesa );
   i810RegetLockQuiescent( imesa );
   UNLOCK_HARDWARE( imesa );
}


void i810RegetLockQuiescent( i810ContextPtr imesa  ) 
{
   drmUnlock(imesa->driFd, imesa->hHWContext);
   i810GetLock( imesa, DRM_LOCK_QUIESCENT ); 
}

void i810WaitAgeLocked( i810ContextPtr imesa, int age  ) 
{
   int i = 0, j;

   while (++i < 5000) {
      drmCommandNone(imesa->driFd, DRM_I810_GETAGE);
      if (GET_DISPATCH_AGE(imesa) >= age)
	 return;
      for (j = 0 ; j < 1000 ; j++)
	 ;
   }

   drmCommandNone(imesa->driFd, DRM_I810_FLUSH);
}


void i810WaitAge( i810ContextPtr imesa, int age  ) 
{
   int i = 0, j;

   while (++i < 5000) {
      drmCommandNone(imesa->driFd, DRM_I810_GETAGE);
      if (GET_DISPATCH_AGE(imesa) >= age)
	 return;
      for (j = 0 ; j < 1000 ; j++)
	 ;
   }

   i = 0;
   while (++i < 1000) {
      drmCommandNone(imesa->driFd, DRM_I810_GETAGE);
      if (GET_DISPATCH_AGE(imesa) >= age)
	 return;
      usleep(1000);
   }

   LOCK_HARDWARE(imesa);
   drmCommandNone(imesa->driFd, DRM_I810_FLUSH);
   UNLOCK_HARDWARE(imesa);
}




static int intersect_rect( drm_clip_rect_t *out,
                           drm_clip_rect_t *a,
                           drm_clip_rect_t *b )
{
   *out = *a;
   if (b->x1 > out->x1) out->x1 = b->x1;
   if (b->x2 < out->x2) out->x2 = b->x2;
   if (out->x1 >= out->x2) return 0;

   if (b->y1 > out->y1) out->y1 = b->y1;
   if (b->y2 < out->y2) out->y2 = b->y2;
   if (out->y1 >= out->y2) return 0;
   return 1;
}


static void emit_state( i810ContextPtr imesa )
{
   GLuint dirty = imesa->dirty;   
   I810SAREAPtr sarea = imesa->sarea;

   if (dirty & I810_UPLOAD_BUFFERS) {
      memcpy( sarea->BufferState, imesa->BufferSetup, 
	      sizeof(imesa->BufferSetup) );
   }	 

   if (dirty & I810_UPLOAD_CTX) {
      memcpy( sarea->ContextState, imesa->Setup, 
	      sizeof(imesa->Setup) );
   }

   if (dirty & I810_UPLOAD_TEX0) {
      memcpy(sarea->TexState[0], 
	     imesa->CurrentTexObj[0]->Setup,
	     sizeof(imesa->CurrentTexObj[0]->Setup));
   }

   if (dirty & I810_UPLOAD_TEX1) {
      GLuint *setup = sarea->TexState[1];

      memcpy( setup,
	      imesa->CurrentTexObj[1]->Setup,
	      sizeof(imesa->CurrentTexObj[1]->Setup));

      /* Need this for the case where both units are bound to the same
       * texobj.  
       */
      setup[I810_TEXREG_MI1] ^= (MI1_MAP_0 ^ MI1_MAP_1);
      setup[I810_TEXREG_MLC] ^= (MLC_MAP_0 ^ MLC_MAP_1);
      setup[I810_TEXREG_MLL] ^= (MLL_MAP_0 ^ MLL_MAP_1);
      setup[I810_TEXREG_MCS] ^= (MCS_COORD_0 ^ MCS_COORD_1);
      setup[I810_TEXREG_MF]  ^= (MF_MAP_0 ^ MF_MAP_1);
   }
    
   sarea->dirty = dirty;
   imesa->dirty = 0;
}


static void age_imesa( i810ContextPtr imesa, int age )
{
   if (imesa->CurrentTexObj[0]) imesa->CurrentTexObj[0]->base.timestamp = age;
   if (imesa->CurrentTexObj[1]) imesa->CurrentTexObj[1]->base.timestamp = age;
}


void i810FlushPrimsLocked( i810ContextPtr imesa )
{
   drm_clip_rect_t *pbox = imesa->pClipRects;
   int nbox = imesa->numClipRects;
   drmBufPtr buffer = imesa->vertex_buffer;
   I810SAREAPtr sarea = imesa->sarea;
   drmI810Vertex vertex;
   int i;
	  
   if (I810_DEBUG & DEBUG_STATE)
      i810PrintDirty( __FUNCTION__, imesa->dirty );
   
   if (imesa->dirty)
      emit_state( imesa );

   vertex.idx = buffer->idx;
   vertex.used = imesa->vertex_low;
   vertex.discard = 0;
   sarea->vertex_prim = imesa->hw_primitive;

   if (!nbox) {
      vertex.used = 0;
   }
   else if (nbox > I810_NR_SAREA_CLIPRECTS) {      
      imesa->upload_cliprects = GL_TRUE;
   }

   if (!nbox || !imesa->upload_cliprects) 
   {
      if (nbox == 1) 
	 sarea->nbox = 0;
      else
	 sarea->nbox = nbox;

      vertex.discard = 1;	
      drmCommandWrite(imesa->driFd, DRM_I810_VERTEX,
                      &vertex, sizeof(drmI810Vertex));
      age_imesa(imesa, sarea->last_enqueue);
   }  
   else 
   {
      for (i = 0 ; i < nbox ; )
      {
	 int nr = MIN2(i + I810_NR_SAREA_CLIPRECTS, nbox);
	 drm_clip_rect_t *b = (drm_clip_rect_t *)sarea->boxes;

	 if (imesa->scissor) {
	    sarea->nbox = 0;
	 
	    for ( ; i < nr ; i++) {
	       b->x1 = pbox[i].x1 - imesa->drawX;
	       b->y1 = pbox[i].y1 - imesa->drawY;
	       b->x2 = pbox[i].x2 - imesa->drawX;
	       b->y2 = pbox[i].y2 - imesa->drawY;

	       if (intersect_rect(b, b, &imesa->scissor_rect)) {
		  sarea->nbox++;
		  b++;
	       }
	    }

	    /* Culled?
	     */
	    if (!sarea->nbox) {
	       if (nr < nbox) continue;
	       vertex.used = 0;
	    }
	 } else {
	    sarea->nbox = nr - i;
	    for ( ; i < nr ; i++, b++) {
	       b->x1 = pbox[i].x1 - imesa->drawX;
	       b->y1 = pbox[i].y1 - imesa->drawY;
	       b->x2 = pbox[i].x2 - imesa->drawX;
	       b->y2 = pbox[i].y2 - imesa->drawY;
	    }
	 }
	 
	 /* Finished with the buffer?
	  */
	 if (nr == nbox) 
	    vertex.discard = 1;

	 drmCommandWrite(imesa->driFd, DRM_I810_VERTEX,
                         &vertex, sizeof(drmI810Vertex));
	 age_imesa(imesa, imesa->sarea->last_enqueue);
      }
   }

   /* Reset imesa vars:
    */
   imesa->vertex_buffer = 0;
   imesa->vertex_addr = 0;
   imesa->vertex_low = 0;
   imesa->vertex_high = 0;
   imesa->vertex_last_prim = 0;
   imesa->dirty = 0;
   imesa->upload_cliprects = GL_FALSE;
}

void i810FlushPrimsGetBuffer( i810ContextPtr imesa )
{
   LOCK_HARDWARE(imesa);

   if (imesa->vertex_buffer) 
      i810FlushPrimsLocked( imesa );      

   imesa->vertex_buffer = i810_get_buffer_ioctl( imesa );
   imesa->vertex_high = imesa->vertex_buffer->total;
   imesa->vertex_addr = (char *)imesa->vertex_buffer->address;
   imesa->vertex_low = 4;	/* leave room for instruction header */
   imesa->vertex_last_prim = imesa->vertex_low;
   UNLOCK_HARDWARE(imesa);
}


void i810FlushPrims( i810ContextPtr imesa ) 
{
   if (imesa->vertex_buffer) {
      LOCK_HARDWARE( imesa );
      i810FlushPrimsLocked( imesa );
      UNLOCK_HARDWARE( imesa );
   }
}



int i810_check_copy(int fd)
{
   return(drmCommandNone(fd, DRM_I810_DOCOPY));
}

static void i810Flush( GLcontext *ctx )
{
   i810ContextPtr imesa = I810_CONTEXT( ctx );
   I810_FIREVERTICES( imesa );
}

static void i810Finish( GLcontext *ctx  ) 
{
   i810ContextPtr imesa = I810_CONTEXT( ctx );
   i810DmaFinish( imesa );
}

void i810InitIoctlFuncs( struct dd_function_table *functions )
{
   functions->Flush = i810Flush;
   functions->Clear = i810Clear;
   functions->Finish = i810Finish;
}