summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/r200/r200_pixel.c
blob: 2797cbb3dc0a7dbc8f79ca52caec25489beee541 (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
/*
Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.

The Weather Channel (TM) funded Tungsten Graphics to develop the
initial release of the Radeon 8500 driver under the XFree86 license.
This notice must be preserved.

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 (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 NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT OWNER(S) 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:
 *   Keith Whitwell <keith@tungstengraphics.com>
 */

#include "main/glheader.h"
#include "main/enums.h"
#include "main/mtypes.h"
#include "main/macros.h"
#include "swrast/swrast.h"

#include "r200_context.h"
#include "r200_ioctl.h"
#include "r200_pixel.h"
#include "r200_swtcl.h"

#include "drirenderbuffer.h"


static GLboolean
check_color( const GLcontext *ctx, GLenum type, GLenum format,
	     const struct gl_pixelstore_attrib *packing,
	     const void *pixels, GLint sz, GLint pitch )
{
   r200ContextPtr rmesa = R200_CONTEXT(ctx);
   GLuint cpp = rmesa->r200Screen->cpp;

   if (R200_DEBUG & DEBUG_PIXEL)
      fprintf(stderr, "%s\n", __FUNCTION__);

   if (	(pitch & 63) ||
	ctx->_ImageTransferState ||
	packing->SwapBytes ||
	packing->LsbFirst) {
      if (R200_DEBUG & DEBUG_PIXEL)
	 fprintf(stderr, "%s: failed 1\n", __FUNCTION__);
      return GL_FALSE;
   }

   if ( type == GL_UNSIGNED_INT_8_8_8_8_REV && 
	cpp == 4 && 
	format == GL_BGRA ) {
      if (R200_DEBUG & DEBUG_PIXEL)
	 fprintf(stderr, "%s: passed 2\n", __FUNCTION__);
      return GL_TRUE;
   }

   if (R200_DEBUG & DEBUG_PIXEL)
      fprintf(stderr, "%s: failed\n", __FUNCTION__);

   return GL_FALSE;
}

static GLboolean
check_color_per_fragment_ops( const GLcontext *ctx )
{
   int result;
   result = (!(     ctx->Color.AlphaEnabled || 
		    ctx->Depth.Test ||
		    ctx->Fog.Enabled ||
		    ctx->Scissor.Enabled ||
		    ctx->Stencil._Enabled ||
		    !ctx->Color.ColorMask[0] ||
		    !ctx->Color.ColorMask[1] ||
		    !ctx->Color.ColorMask[2] ||
		    !ctx->Color.ColorMask[3] ||
		    ctx->Color.ColorLogicOpEnabled ||
		    ctx->Texture._EnabledUnits
           ) &&
	   ctx->Current.RasterPosValid);
   
   return result;
}



static GLboolean
clip_pixelrect( const GLcontext *ctx,
		const GLframebuffer *buffer,
		GLint *x, GLint *y,
		GLsizei *width, GLsizei *height,
		GLint *size )
{
   r200ContextPtr rmesa = R200_CONTEXT(ctx);

   /* left clipping */
   if (*x < buffer->_Xmin) {
      *width -= (buffer->_Xmin - *x);
      *x = buffer->_Xmin;
   }

   /* right clipping */
   if (*x + *width > buffer->_Xmax)
      *width -= (*x + *width - buffer->_Xmax - 1);

   if (*width <= 0)
      return GL_FALSE;

   /* bottom clipping */
   if (*y < buffer->_Ymin) {
      *height -= (buffer->_Ymin - *y);
      *y = buffer->_Ymin;
   }

   /* top clipping */
   if (*y + *height > buffer->_Ymax)
      *height -= (*y + *height - buffer->_Ymax - 1);

   if (*height <= 0)
      return GL_FALSE;

   *size = ((*y + *height - 1) * rmesa->r200Screen->frontPitch +
	    (*x + *width - 1) * rmesa->r200Screen->cpp);

   return GL_TRUE;
}

static GLboolean
r200TryReadPixels( GLcontext *ctx,
		  GLint x, GLint y, GLsizei width, GLsizei height,
		  GLenum format, GLenum type,
		  const struct gl_pixelstore_attrib *pack,
		  GLvoid *pixels )
{
   r200ContextPtr rmesa = R200_CONTEXT(ctx);
   GLint pitch = pack->RowLength ? pack->RowLength : width;
   GLint blit_format;
   GLuint cpp = rmesa->r200Screen->cpp;
   GLint size = width * height * cpp;

   if (R200_DEBUG & DEBUG_PIXEL)
      fprintf(stderr, "%s\n", __FUNCTION__);

   /* Only accelerate reading to GART buffers.
    */
   if ( !r200IsGartMemory(rmesa, pixels, 
			 pitch * height * rmesa->r200Screen->cpp ) ) {
      if (R200_DEBUG & DEBUG_PIXEL)
	 fprintf(stderr, "%s: dest not GART\n", __FUNCTION__);
      return GL_FALSE;
   }

   /* Need GL_PACK_INVERT_MESA to cope with upsidedown results from
    * blitter:
    */
   if (!pack->Invert) {
      if (R200_DEBUG & DEBUG_PIXEL)
	 fprintf(stderr, "%s: MESA_PACK_INVERT not set\n", __FUNCTION__);
      return GL_FALSE;
   }

   if (!check_color(ctx, type, format, pack, pixels, size, pitch))
      return GL_FALSE;

   switch ( rmesa->r200Screen->cpp ) {
   case 4:
      blit_format = R200_CP_COLOR_FORMAT_ARGB8888;
      break;
   default:
      return GL_FALSE;
   }


   /* Although the blits go on the command buffer, need to do this and
    * fire with lock held to guarentee cliprects and drawOffset are
    * correct.
    *
    * This is an unusual situation however, as the code which flushes
    * a full command buffer expects to be called unlocked.  As a
    * workaround, immediately flush the buffer on aquiring the lock.
    */
   LOCK_HARDWARE( rmesa );

   if (rmesa->store.cmd_used)
      r200FlushCmdBufLocked( rmesa, __FUNCTION__ );

   if (!clip_pixelrect(ctx, ctx->ReadBuffer, &x, &y, &width, &height,
		       &size)) {
      UNLOCK_HARDWARE( rmesa );
      if (R200_DEBUG & DEBUG_PIXEL)
	 fprintf(stderr, "%s totally clipped -- nothing to do\n",
		 __FUNCTION__);
      return GL_TRUE;
   }

   {
      __DRIdrawablePrivate *dPriv = rmesa->dri.drawable;
      driRenderbuffer *drb = (driRenderbuffer *) ctx->ReadBuffer->_ColorReadBuffer;
      int nbox = dPriv->numClipRects;
      int src_offset = drb->offset
		     + rmesa->r200Screen->fbLocation;
      int src_pitch = drb->pitch * drb->cpp;
      int dst_offset = r200GartOffsetFromVirtual( rmesa, pixels );
      int dst_pitch = pitch * rmesa->r200Screen->cpp;
      drm_clip_rect_t *box = dPriv->pClipRects;
      int i;

      r200EmitWait( rmesa, RADEON_WAIT_3D ); 

      y = dPriv->h - y - height;
      x += dPriv->x;
      y += dPriv->y;


      if (R200_DEBUG & DEBUG_PIXEL)
	 fprintf(stderr, "readpixel blit src_pitch %d dst_pitch %d\n",
		 src_pitch, dst_pitch);

      for (i = 0 ; i < nbox ; i++)
      {
	 GLint bx = box[i].x1;
	 GLint by = box[i].y1;
	 GLint bw = box[i].x2 - bx;
	 GLint bh = box[i].y2 - by;
	 
	 if (bx < x) bw -= x - bx, bx = x;
	 if (by < y) bh -= y - by, by = y;
	 if (bx + bw > x + width) bw = x + width - bx;
	 if (by + bh > y + height) bh = y + height - by;
	 if (bw <= 0) continue;
	 if (bh <= 0) continue;

	 r200EmitBlit( rmesa,
		       blit_format,
		       src_pitch, src_offset,
		       dst_pitch, dst_offset,
		       bx, by,
		       bx - x, by - y,
		       bw, bh );
      }

      r200FlushCmdBufLocked( rmesa, __FUNCTION__ );
   }
   UNLOCK_HARDWARE( rmesa );

   r200Finish( ctx ); /* required by GL */

   return GL_TRUE;
}

static void
r200ReadPixels( GLcontext *ctx,
		 GLint x, GLint y, GLsizei width, GLsizei height,
		 GLenum format, GLenum type,
		 const struct gl_pixelstore_attrib *pack,
		 GLvoid *pixels )
{
   if (R200_DEBUG & DEBUG_PIXEL)
      fprintf(stderr, "%s\n", __FUNCTION__);

   if (!r200TryReadPixels( ctx, x, y, width, height, format, type, pack, 
			   pixels))
      _swrast_ReadPixels( ctx, x, y, width, height, format, type, pack, 
			  pixels);
}




static void do_draw_pix( GLcontext *ctx,
			 GLint x, GLint y, GLsizei width, GLsizei height,
			 GLint pitch,
			 const void *pixels,
			 GLuint planemask)
{
   r200ContextPtr rmesa = R200_CONTEXT(ctx);
   __DRIdrawablePrivate *dPriv = rmesa->dri.drawable;
   drm_clip_rect_t *box = dPriv->pClipRects;
   struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorDrawBuffers[0];
   driRenderbuffer *drb = (driRenderbuffer *) rb;
   int nbox = dPriv->numClipRects;
   int i;
   int blit_format;
   int size;
   int src_offset = r200GartOffsetFromVirtual( rmesa, pixels );
   int src_pitch = pitch * rmesa->r200Screen->cpp;

   if (R200_DEBUG & DEBUG_PIXEL)
      fprintf(stderr, "%s\n", __FUNCTION__);

   switch ( rmesa->r200Screen->cpp ) {
   case 2:
      blit_format = R200_CP_COLOR_FORMAT_RGB565;
      break;
   case 4:
      blit_format = R200_CP_COLOR_FORMAT_ARGB8888;
      break;
   default:
      return;
   }


   LOCK_HARDWARE( rmesa );

   if (rmesa->store.cmd_used)
      r200FlushCmdBufLocked( rmesa, __FUNCTION__ );

   y -= height;			/* cope with pixel zoom */
   
   if (!clip_pixelrect(ctx, ctx->DrawBuffer,
		       &x, &y, &width, &height,
		       &size)) {
      UNLOCK_HARDWARE( rmesa );
      return;
   }

   y = dPriv->h - y - height; 	/* convert from gl to hardware coords */
   x += dPriv->x;
   y += dPriv->y;


   r200EmitWait( rmesa, RADEON_WAIT_3D );

   for (i = 0 ; i < nbox ; i++ )
   {
      GLint bx = box[i].x1;
      GLint by = box[i].y1;
      GLint bw = box[i].x2 - bx;
      GLint bh = box[i].y2 - by;

      if (bx < x) bw -= x - bx, bx = x;
      if (by < y) bh -= y - by, by = y;
      if (bx + bw > x + width) bw = x + width - bx;
      if (by + bh > y + height) bh = y + height - by;
      if (bw <= 0) continue;
      if (bh <= 0) continue;

      r200EmitBlit( rmesa,
		    blit_format,
		    src_pitch, src_offset,
		    drb->pitch * drb->cpp,
		    drb->offset + rmesa->r200Screen->fbLocation,
		    bx - x, by - y,
		    bx, by,
		    bw, bh );
   }

   r200FlushCmdBufLocked( rmesa, __FUNCTION__ );
   r200WaitForIdleLocked( rmesa ); /* required by GL */
   UNLOCK_HARDWARE( rmesa );
}




static GLboolean
r200TryDrawPixels( GLcontext *ctx,
		  GLint x, GLint y, GLsizei width, GLsizei height,
		  GLenum format, GLenum type,
		  const struct gl_pixelstore_attrib *unpack,
		  const GLvoid *pixels )
{
   r200ContextPtr rmesa = R200_CONTEXT(ctx);
   GLint pitch = unpack->RowLength ? unpack->RowLength : width;
   GLuint planemask;
   GLuint cpp = rmesa->r200Screen->cpp;
   GLint size = height * pitch * cpp;

   if (R200_DEBUG & DEBUG_PIXEL)
      fprintf(stderr, "%s\n", __FUNCTION__);

   /* check that we're drawing to exactly one color buffer */
   if (ctx->DrawBuffer->_NumColorDrawBuffers != 1)
     return GL_FALSE;

   switch (format) {
   case GL_RGB:
   case GL_RGBA:
   case GL_BGRA:
      planemask = r200PackColor(cpp,
				ctx->Color.ColorMask[RCOMP],
				ctx->Color.ColorMask[GCOMP],
				ctx->Color.ColorMask[BCOMP],
				ctx->Color.ColorMask[ACOMP]);

      if (cpp == 2)
	 planemask |= planemask << 16;

      if (planemask != ~0)
	 return GL_FALSE;	/* fix me -- should be possible */

      /* Can't do conversions on GART reads/draws. 
       */
      if ( !r200IsGartMemory( rmesa, pixels, size ) ) {
	 if (R200_DEBUG & DEBUG_PIXEL)
	    fprintf(stderr, "%s: not GART memory\n", __FUNCTION__);
	 return GL_FALSE;
      }

      if (!check_color(ctx, type, format, unpack, pixels, size, pitch)) {
	 return GL_FALSE;
      }
      if (!check_color_per_fragment_ops(ctx)) {
	 return GL_FALSE;
      }

      if (ctx->Pixel.ZoomX != 1.0F ||
	  ctx->Pixel.ZoomY != -1.0F)
	 return GL_FALSE;
      break;

   default:
      return GL_FALSE;
   }

   if ( r200IsGartMemory(rmesa, pixels, size) )
   {
      do_draw_pix( ctx, x, y, width, height, pitch, pixels, planemask );
      return GL_TRUE;
   }
   else if (0)
   {
      /* Pixels is in regular memory -- get dma buffers and perform
       * upload through them.
       */
   }
   else
      return GL_FALSE;
}

static void
r200DrawPixels( GLcontext *ctx,
		 GLint x, GLint y, GLsizei width, GLsizei height,
		 GLenum format, GLenum type,
		 const struct gl_pixelstore_attrib *unpack,
		 const GLvoid *pixels )
{
   if (R200_DEBUG & DEBUG_PIXEL)
      fprintf(stderr, "%s\n", __FUNCTION__);

   if (!r200TryDrawPixels( ctx, x, y, width, height, format, type,
			  unpack, pixels ))
      _swrast_DrawPixels( ctx, x, y, width, height, format, type,
			  unpack, pixels );
}


static void
r200Bitmap( GLcontext *ctx, GLint px, GLint py,
		  GLsizei width, GLsizei height,
		  const struct gl_pixelstore_attrib *unpack,
		  const GLubyte *bitmap )
{
   r200ContextPtr rmesa = R200_CONTEXT(ctx);

   if (rmesa->Fallback)
      _swrast_Bitmap( ctx, px, py, width, height, unpack, bitmap );
   else
      r200PointsBitmap( ctx, px, py, width, height, unpack, bitmap );
}



void r200InitPixelFuncs( GLcontext *ctx )
{
   if (!getenv("R200_NO_BLITS")) {
      ctx->Driver.ReadPixels = r200ReadPixels;  
      ctx->Driver.DrawPixels = r200DrawPixels; 
      if (getenv("R200_HW_BITMAP")) 
	 ctx->Driver.Bitmap = r200Bitmap;
   }
}