Add Intel 945GM support
[mesa.git] / src / mesa / drivers / dri / i915 / intel_pixel.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "glheader.h"
29 #include "enums.h"
30 #include "mtypes.h"
31 #include "macros.h"
32 #include "swrast/swrast.h"
33
34 #include "intel_screen.h"
35 #include "intel_context.h"
36 #include "intel_ioctl.h"
37 #include "intel_batchbuffer.h"
38
39
40
41 static GLboolean
42 check_color( const GLcontext *ctx, GLenum type, GLenum format,
43 const struct gl_pixelstore_attrib *packing,
44 const void *pixels, GLint sz, GLint pitch )
45 {
46 intelContextPtr intel = INTEL_CONTEXT(ctx);
47 GLuint cpp = intel->intelScreen->cpp;
48
49 if (INTEL_DEBUG & DEBUG_PIXEL)
50 fprintf(stderr, "%s\n", __FUNCTION__);
51
52 if ( (pitch & 63) ||
53 ctx->_ImageTransferState ||
54 packing->SwapBytes ||
55 packing->LsbFirst) {
56 if (INTEL_DEBUG & DEBUG_PIXEL)
57 fprintf(stderr, "%s: failed 1\n", __FUNCTION__);
58 return GL_FALSE;
59 }
60
61 if ( type == GL_UNSIGNED_INT_8_8_8_8_REV &&
62 cpp == 4 &&
63 format == GL_BGRA ) {
64 if (INTEL_DEBUG & DEBUG_PIXEL)
65 fprintf(stderr, "%s: passed 2\n", __FUNCTION__);
66 return GL_TRUE;
67 }
68
69 if (INTEL_DEBUG & DEBUG_PIXEL)
70 fprintf(stderr, "%s: failed\n", __FUNCTION__);
71
72 return GL_FALSE;
73 }
74
75 static GLboolean
76 check_color_per_fragment_ops( const GLcontext *ctx )
77 {
78 int result;
79 result = (!( ctx->Color.AlphaEnabled ||
80 ctx->Depth.Test ||
81 ctx->Fog.Enabled ||
82 ctx->Scissor.Enabled ||
83 ctx->Stencil.Enabled ||
84 !ctx->Color.ColorMask[0] ||
85 !ctx->Color.ColorMask[1] ||
86 !ctx->Color.ColorMask[2] ||
87 !ctx->Color.ColorMask[3] ||
88 ctx->Color.ColorLogicOpEnabled ||
89 ctx->Texture._EnabledUnits
90 ) &&
91 ctx->Current.RasterPosValid);
92
93 return result;
94 }
95
96
97 /**
98 * Clip the given rectangle against the buffer's bounds (including scissor).
99 * \param size returns the
100 * \return GL_TRUE if any pixels remain, GL_FALSE if totally clipped.
101 *
102 * XXX Replace this with _mesa_clip_drawpixels() and _mesa_clip_readpixels()
103 * from Mesa 6.4. We shouldn't apply scissor for ReadPixels.
104 */
105 static GLboolean
106 clip_pixelrect( const GLcontext *ctx,
107 const GLframebuffer *buffer,
108 GLint *x, GLint *y,
109 GLsizei *width, GLsizei *height)
110 {
111 /* left clipping */
112 if (*x < buffer->_Xmin) {
113 *width -= (buffer->_Xmin - *x);
114 *x = buffer->_Xmin;
115 }
116
117 /* right clipping */
118 if (*x + *width > buffer->_Xmax)
119 *width -= (*x + *width - buffer->_Xmax - 1);
120
121 if (*width <= 0)
122 return GL_FALSE;
123
124 /* bottom clipping */
125 if (*y < buffer->_Ymin) {
126 *height -= (buffer->_Ymin - *y);
127 *y = buffer->_Ymin;
128 }
129
130 /* top clipping */
131 if (*y + *height > buffer->_Ymax)
132 *height -= (*y + *height - buffer->_Ymax - 1);
133
134 if (*height <= 0)
135 return GL_FALSE;
136
137 return GL_TRUE;
138 }
139
140
141 /**
142 * Compute intersection of a clipping rectangle and pixel rectangle,
143 * returning results in x/y/w/hOut vars.
144 * \return GL_TRUE if there's intersection, GL_FALSE if disjoint.
145 */
146 static INLINE GLboolean
147 intersect_region(const drm_clip_rect_t *box,
148 GLint x, GLint y, GLsizei width, GLsizei height,
149 GLint *xOut, GLint *yOut, GLint *wOut, GLint *hOut)
150 {
151 GLint bx = box->x1;
152 GLint by = box->y1;
153 GLint bw = box->x2 - bx;
154 GLint bh = box->y2 - by;
155
156 if (bx < x) bw -= x - bx, bx = x;
157 if (by < y) bh -= y - by, by = y;
158 if (bx + bw > x + width) bw = x + width - bx;
159 if (by + bh > y + height) bh = y + height - by;
160 if (bw <= 0) return GL_FALSE;
161 if (bh <= 0) return GL_FALSE;
162
163 *xOut = bx;
164 *yOut = by;
165 *wOut = bw;
166 *hOut = bh;
167 return GL_TRUE;
168 }
169
170
171
172 static GLboolean
173 intelTryReadPixels( GLcontext *ctx,
174 GLint x, GLint y, GLsizei width, GLsizei height,
175 GLenum format, GLenum type,
176 const struct gl_pixelstore_attrib *pack,
177 GLvoid *pixels )
178 {
179 intelContextPtr intel = INTEL_CONTEXT(ctx);
180 GLint size = 0; /* not really used */
181 GLint pitch = pack->RowLength ? pack->RowLength : width;
182
183 if (INTEL_DEBUG & DEBUG_PIXEL)
184 fprintf(stderr, "%s\n", __FUNCTION__);
185
186 /* Only accelerate reading to agp buffers.
187 */
188 if ( !intelIsAgpMemory(intel, pixels,
189 pitch * height * intel->intelScreen->cpp ) ) {
190 if (INTEL_DEBUG & DEBUG_PIXEL)
191 fprintf(stderr, "%s: dest not agp\n", __FUNCTION__);
192 return GL_FALSE;
193 }
194
195 /* Need GL_PACK_INVERT_MESA to cope with upsidedown results from
196 * blitter:
197 */
198 if (!pack->Invert) {
199 if (INTEL_DEBUG & DEBUG_PIXEL)
200 fprintf(stderr, "%s: MESA_PACK_INVERT not set\n", __FUNCTION__);
201 return GL_FALSE;
202 }
203
204 if (!check_color(ctx, type, format, pack, pixels, size, pitch))
205 return GL_FALSE;
206
207 switch ( intel->intelScreen->cpp ) {
208 case 4:
209 break;
210 default:
211 return GL_FALSE;
212 }
213
214
215 /* Although the blits go on the command buffer, need to do this and
216 * fire with lock held to guarentee cliprects and drawing offset are
217 * correct.
218 *
219 * This is an unusual situation however, as the code which flushes
220 * a full command buffer expects to be called unlocked. As a
221 * workaround, immediately flush the buffer on aquiring the lock.
222 */
223 intelFlush( &intel->ctx );
224 LOCK_HARDWARE( intel );
225 {
226 __DRIdrawablePrivate *dPriv = intel->driDrawable;
227 int nbox = dPriv->numClipRects;
228 int src_offset = intel->readRegion->offset;
229 int src_pitch = intel->intelScreen->front.pitch;
230 int dst_offset = intelAgpOffsetFromVirtual( intel, pixels);
231 drm_clip_rect_t *box = dPriv->pClipRects;
232 int i;
233
234 assert(dst_offset != ~0); /* should have been caught above */
235
236 if (!clip_pixelrect(ctx, ctx->ReadBuffer, &x, &y, &width, &height)) {
237 UNLOCK_HARDWARE( intel );
238 if (INTEL_DEBUG & DEBUG_PIXEL)
239 fprintf(stderr, "%s totally clipped -- nothing to do\n",
240 __FUNCTION__);
241 return GL_TRUE;
242 }
243
244 /* convert to screen coords (y=0=top) */
245 y = dPriv->h - y - height;
246 x += dPriv->x;
247 y += dPriv->y;
248
249 if (INTEL_DEBUG & DEBUG_PIXEL)
250 fprintf(stderr, "readpixel blit src_pitch %d dst_pitch %d\n",
251 src_pitch, pitch);
252
253 /* We don't really have to do window clipping for readpixels.
254 * The OpenGL spec says that pixels read from outside the
255 * visible window region (pixel ownership) have undefined value.
256 */
257 for (i = 0 ; i < nbox ; i++)
258 {
259 GLint bx, by, bw, bh;
260 if (intersect_region(box+i, x, y, width, height,
261 &bx, &by, &bw, &bh)) {
262 intelEmitCopyBlitLocked( intel,
263 intel->intelScreen->cpp,
264 src_pitch, src_offset,
265 pitch, dst_offset,
266 bx, by,
267 bx - x, by - y,
268 bw, bh );
269 }
270 }
271 }
272 UNLOCK_HARDWARE( intel );
273 intelFinish( &intel->ctx );
274
275 return GL_TRUE;
276 }
277
278 static void
279 intelReadPixels( GLcontext *ctx,
280 GLint x, GLint y, GLsizei width, GLsizei height,
281 GLenum format, GLenum type,
282 const struct gl_pixelstore_attrib *pack,
283 GLvoid *pixels )
284 {
285 if (INTEL_DEBUG & DEBUG_PIXEL)
286 fprintf(stderr, "%s\n", __FUNCTION__);
287
288 if (!intelTryReadPixels( ctx, x, y, width, height, format, type, pack,
289 pixels))
290 _swrast_ReadPixels( ctx, x, y, width, height, format, type, pack,
291 pixels);
292 }
293
294
295
296
297 static void do_draw_pix( GLcontext *ctx,
298 GLint x, GLint y, GLsizei width, GLsizei height,
299 GLint pitch,
300 const void *pixels,
301 GLuint dest )
302 {
303 intelContextPtr intel = INTEL_CONTEXT(ctx);
304 __DRIdrawablePrivate *dPriv = intel->driDrawable;
305 drm_clip_rect_t *box = dPriv->pClipRects;
306 int nbox = dPriv->numClipRects;
307 int i;
308 int src_offset = intelAgpOffsetFromVirtual( intel, pixels);
309 int src_pitch = pitch;
310
311 assert(src_offset != ~0); /* should be caught earlier */
312
313 if (INTEL_DEBUG & DEBUG_PIXEL)
314 fprintf(stderr, "%s\n", __FUNCTION__);
315
316 intelFlush( &intel->ctx );
317 LOCK_HARDWARE( intel );
318 if (ctx->DrawBuffer)
319 {
320 y -= height; /* cope with pixel zoom */
321
322 if (!clip_pixelrect(ctx, ctx->DrawBuffer,
323 &x, &y, &width, &height)) {
324 UNLOCK_HARDWARE( intel );
325 return;
326 }
327
328 y = dPriv->h - y - height; /* convert from gl to hardware coords */
329 x += dPriv->x;
330 y += dPriv->y;
331
332 for (i = 0 ; i < nbox ; i++ )
333 {
334 GLint bx, by, bw, bh;
335 if (intersect_region(box + i, x, y, width, height,
336 &bx, &by, &bw, &bh)) {
337 intelEmitCopyBlitLocked( intel,
338 intel->intelScreen->cpp,
339 src_pitch, src_offset,
340 intel->intelScreen->front.pitch,
341 intel->drawRegion->offset,
342 bx - x, by - y,
343 bx, by,
344 bw, bh );
345 }
346 }
347 }
348 UNLOCK_HARDWARE( intel );
349 intelFinish( &intel->ctx );
350 }
351
352
353
354 static GLboolean
355 intelTryDrawPixels( GLcontext *ctx,
356 GLint x, GLint y, GLsizei width, GLsizei height,
357 GLenum format, GLenum type,
358 const struct gl_pixelstore_attrib *unpack,
359 const GLvoid *pixels )
360 {
361 intelContextPtr intel = INTEL_CONTEXT(ctx);
362 GLint pitch = unpack->RowLength ? unpack->RowLength : width;
363 GLuint dest;
364 GLuint cpp = intel->intelScreen->cpp;
365 GLint size = width * pitch * cpp;
366
367 if (INTEL_DEBUG & DEBUG_PIXEL)
368 fprintf(stderr, "%s\n", __FUNCTION__);
369
370 switch (format) {
371 case GL_RGB:
372 case GL_RGBA:
373 case GL_BGRA:
374 dest = intel->drawRegion->offset;
375
376 /* Planemask doesn't have full support in blits.
377 */
378 if (!ctx->Color.ColorMask[RCOMP] ||
379 !ctx->Color.ColorMask[GCOMP] ||
380 !ctx->Color.ColorMask[BCOMP] ||
381 !ctx->Color.ColorMask[ACOMP]) {
382 if (INTEL_DEBUG & DEBUG_PIXEL)
383 fprintf(stderr, "%s: planemask\n", __FUNCTION__);
384 return GL_FALSE;
385 }
386
387 /* Can't do conversions on agp reads/draws.
388 */
389 if ( !intelIsAgpMemory( intel, pixels, size ) ) {
390 if (INTEL_DEBUG & DEBUG_PIXEL)
391 fprintf(stderr, "%s: not agp memory\n", __FUNCTION__);
392 return GL_FALSE;
393 }
394
395 if (!check_color(ctx, type, format, unpack, pixels, size, pitch)) {
396 return GL_FALSE;
397 }
398 if (!check_color_per_fragment_ops(ctx)) {
399 return GL_FALSE;
400 }
401
402 if (ctx->Pixel.ZoomX != 1.0F ||
403 ctx->Pixel.ZoomY != -1.0F)
404 return GL_FALSE;
405 break;
406
407 default:
408 return GL_FALSE;
409 }
410
411 if ( intelIsAgpMemory(intel, pixels, size) )
412 {
413 do_draw_pix( ctx, x, y, width, height, pitch, pixels, dest );
414 return GL_TRUE;
415 }
416 else if (0)
417 {
418 /* Pixels is in regular memory -- get dma buffers and perform
419 * upload through them. No point doing this for regular uploads
420 * but once we remove some of the restrictions above (colormask,
421 * pixelformat conversion, zoom?, etc), this could be a win.
422 */
423 }
424 else
425 return GL_FALSE;
426 }
427
428 static void
429 intelDrawPixels( GLcontext *ctx,
430 GLint x, GLint y, GLsizei width, GLsizei height,
431 GLenum format, GLenum type,
432 const struct gl_pixelstore_attrib *unpack,
433 const GLvoid *pixels )
434 {
435 if (INTEL_DEBUG & DEBUG_PIXEL)
436 fprintf(stderr, "%s\n", __FUNCTION__);
437
438 if (!intelTryDrawPixels( ctx, x, y, width, height, format, type,
439 unpack, pixels ))
440 _swrast_DrawPixels( ctx, x, y, width, height, format, type,
441 unpack, pixels );
442 }
443
444
445
446
447 /**
448 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
449 * for the color buffer. Don't support zooming, pixel transfer, etc.
450 * We do support copying from one window to another, ala glXMakeCurrentRead.
451 */
452 static void
453 intelCopyPixels( GLcontext *ctx,
454 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
455 GLint destx, GLint desty, GLenum type )
456 {
457 #if 0
458 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
459 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
460 XMesaDisplay *dpy = xmesa->xm_visual->display;
461 const XMesaDrawable drawBuffer = xmesa->xm_draw_buffer->buffer;
462 const XMesaDrawable readBuffer = xmesa->xm_read_buffer->buffer;
463 const XMesaGC gc = xmesa->xm_draw_buffer->gc;
464
465 ASSERT(dpy);
466 ASSERT(gc);
467
468 if (drawBuffer && /* buffer != 0 means it's a Window or Pixmap */
469 readBuffer &&
470 type == GL_COLOR &&
471 (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
472 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
473 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
474 ctx->Pixel.ZoomY == 1.0) {
475 /* Note: we don't do any special clipping work here. We could,
476 * but X will do it for us.
477 */
478 srcy = FLIP(xmesa->xm_read_buffer, srcy) - height + 1;
479 desty = FLIP(xmesa->xm_draw_buffer, desty) - height + 1;
480 XCopyArea(dpy, readBuffer, drawBuffer, gc,
481 srcx, srcy, width, height, destx, desty);
482 }
483 #else
484 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type );
485 #endif
486 }
487
488
489
490
491 void intelInitPixelFuncs( struct dd_function_table *functions )
492 {
493 /* Pixel path fallbacks.
494 */
495 functions->Accum = _swrast_Accum;
496 functions->Bitmap = _swrast_Bitmap;
497 functions->CopyPixels = intelCopyPixels;
498
499 if (!getenv("INTEL_NO_BLITS")) {
500 functions->ReadPixels = intelReadPixels;
501 functions->DrawPixels = intelDrawPixels;
502 }
503 else {
504 functions->ReadPixels = _swrast_ReadPixels;
505 functions->DrawPixels = _swrast_DrawPixels;
506 }
507 }