intel_pixel_draw.c revision f75843a517bd188639e6866db2a7b04de3524e16
1/**************************************************************************
2 *
3 * Copyright 2006 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 portionsalloc
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 "image.h"
31#include "mtypes.h"
32#include "macros.h"
33#include "bufferobj.h"
34#include "swrast/swrast.h"
35
36#include "intel_screen.h"
37#include "intel_context.h"
38#include "intel_ioctl.h"
39#include "intel_batchbuffer.h"
40#include "intel_blit.h"
41#include "intel_buffers.h"
42#include "intel_regions.h"
43#include "intel_pixel.h"
44#include "intel_buffer_objects.h"
45#include "intel_tris.h"
46
47
48
49static GLboolean
50do_texture_drawpixels(GLcontext * ctx,
51                      GLint x, GLint y,
52                      GLsizei width, GLsizei height,
53                      GLenum format, GLenum type,
54                      const struct gl_pixelstore_attrib *unpack,
55                      const GLvoid * pixels)
56{
57   struct intel_context *intel = intel_context(ctx);
58   struct intel_region *dst = intel_drawbuf_region(intel);
59   struct intel_buffer_object *src = intel_buffer_object(unpack->BufferObj);
60   GLuint rowLength = unpack->RowLength ? unpack->RowLength : width;
61   GLuint src_offset;
62
63   if (INTEL_DEBUG & DEBUG_PIXEL)
64      fprintf(stderr, "%s\n", __FUNCTION__);
65
66   intelFlush(&intel->ctx);
67
68   if (!dst)
69      return GL_FALSE;
70
71   intel->vtbl.render_start(intel);
72   intel->vtbl.emit_state(intel);
73
74   if (src) {
75      if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
76                                     format, type, pixels)) {
77         _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels");
78         return GL_TRUE;
79      }
80   }
81   else {
82      /* PBO only for now:
83       */
84      if (INTEL_DEBUG & DEBUG_PIXEL)
85	 _mesa_printf("%s - not PBO\n", __FUNCTION__);
86      return GL_FALSE;
87   }
88
89   /* There are a couple of things we can't do yet, one of which is
90    * set the correct state for pixel operations when GL texturing is
91    * enabled.  That's a pretty rare state and probably not worth the
92    * effort.  A completely device-independent version of this may do
93    * more.
94    *
95    * Similarly, we make no attempt to merge metaops processing with
96    * an enabled fragment program, though it would certainly be
97    * possible.
98    */
99   if (!intel_check_meta_tex_fragment_ops(ctx)) {
100      if (INTEL_DEBUG & DEBUG_PIXEL)
101         _mesa_printf("%s - bad GL fragment state for metaops texture\n",
102                      __FUNCTION__);
103      return GL_FALSE;
104   }
105
106   intel->vtbl.install_meta_state(intel);
107
108
109   /* Is this true?  Also will need to turn depth testing on according
110    * to state:
111    */
112   intel->vtbl.meta_no_stencil_write(intel);
113   intel->vtbl.meta_no_depth_write(intel);
114
115   /* Set the 3d engine to draw into the destination region:
116    */
117   intel->vtbl.meta_draw_region(intel, dst, intel->depth_region);
118
119   intel->vtbl.meta_import_pixel_state(intel);
120
121   src_offset = (GLuint) _mesa_image_address(2, unpack, pixels, width, height,
122                                             format, type, 0, 0, 0);
123
124
125   /* Setup the pbo up as a rectangular texture, if possible.
126    *
127    * TODO: This is almost always possible if the i915 fragment
128    * program is adjusted to correctly swizzle the sampled colors.
129    * The major exception is any 24bit texture, like RGB888, for which
130    * there is no hardware support.
131    */
132   if (!intel->vtbl.meta_tex_rect_source(intel, src->buffer, src_offset,
133                                         rowLength, height, format, type)) {
134      intel->vtbl.leave_meta_state(intel);
135      return GL_FALSE;
136   }
137
138   intel->vtbl.meta_texture_blend_replace(intel);
139
140
141   LOCK_HARDWARE(intel);
142
143   if (intel->driDrawable->numClipRects) {
144      __DRIdrawablePrivate *dPriv = intel->driDrawable;
145      GLint srcx, srcy;
146      GLint dstx, dsty;
147
148      dstx = x;
149      dsty = dPriv->h - (y + height);
150
151      srcx = 0;                 /* skiprows/pixels already done */
152      srcy = 0;
153
154      if (0) {
155         const GLint orig_x = dstx;
156         const GLint orig_y = dsty;
157
158         if (!_mesa_clip_to_region(0, 0, dst->pitch, dst->height,
159                                   &dstx, &dsty, &width, &height))
160            goto out;
161
162         srcx += dstx - orig_x;
163         srcy += dsty - orig_y;
164      }
165
166
167      if (INTEL_DEBUG & DEBUG_PIXEL)
168         _mesa_printf("draw %d,%d %dx%d\n", dstx, dsty, width, height);
169
170      /* Must use the regular cliprect mechanism in order to get the
171       * drawing origin set correctly.  Otherwise scissor state is in
172       * incorrect coordinate space.  Does this even need to hold the
173       * lock???
174       */
175      intel->vtbl.meta_draw_quad(intel,
176				 dstx, dstx + width * ctx->Pixel.ZoomX,
177				 dPriv->h - (y + height * ctx->Pixel.ZoomY),
178				 dPriv->h - (y),
179				 -ctx->Current.RasterPos[2] * .5,
180				 0x00ff00ff,
181				 srcx, srcx + width, srcy + height, srcy);
182    out:
183      intel->vtbl.leave_meta_state(intel);
184      intel_batchbuffer_emit_mi_flush(intel->batch);
185   }
186   UNLOCK_HARDWARE(intel);
187   return GL_TRUE;
188}
189
190
191
192
193
194/* Pros:
195 *   - no waiting for idle before updating framebuffer.
196 *
197 * Cons:
198 *   - if upload is by memcpy, this may actually be slower than fallback path.
199 *   - uploads the whole image even if destination is clipped
200 *
201 * Need to benchmark.
202 *
203 * Given the questions about performance, implement for pbo's only.
204 * This path is definitely a win if the pbo is already in agp.  If it
205 * turns out otherwise, we can add the code necessary to upload client
206 * data to agp space before performing the blit.  (Though it may turn
207 * out to be better/simpler just to use the texture engine).
208 */
209static GLboolean
210do_blit_drawpixels(GLcontext * ctx,
211                   GLint x, GLint y,
212                   GLsizei width, GLsizei height,
213                   GLenum format, GLenum type,
214                   const struct gl_pixelstore_attrib *unpack,
215                   const GLvoid * pixels)
216{
217   struct intel_context *intel = intel_context(ctx);
218   struct intel_region *dest = intel_drawbuf_region(intel);
219   struct intel_buffer_object *src = intel_buffer_object(unpack->BufferObj);
220   GLuint src_offset;
221   GLuint rowLength;
222
223   if (INTEL_DEBUG & DEBUG_PIXEL)
224      _mesa_printf("%s\n", __FUNCTION__);
225
226
227   if (!dest) {
228      if (INTEL_DEBUG & DEBUG_PIXEL)
229         _mesa_printf("%s - no dest\n", __FUNCTION__);
230      return GL_FALSE;
231   }
232
233   if (src) {
234      /* This validation should be done by core mesa:
235       */
236      if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
237                                     format, type, pixels)) {
238         _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels");
239         return GL_TRUE;
240      }
241   }
242   else {
243      /* PBO only for now:
244       */
245      if (INTEL_DEBUG & DEBUG_PIXEL)
246         _mesa_printf("%s - not PBO\n", __FUNCTION__);
247      return GL_FALSE;
248   }
249
250   if (!intel_check_blit_format(dest, format, type)) {
251      if (INTEL_DEBUG & DEBUG_PIXEL)
252         _mesa_printf("%s - bad format for blit\n", __FUNCTION__);
253      return GL_FALSE;
254   }
255
256   if (!intel_check_blit_fragment_ops(ctx, GL_FALSE)) {
257      if (INTEL_DEBUG & DEBUG_PIXEL)
258         _mesa_printf("%s - bad GL fragment state for blitter\n",
259                      __FUNCTION__);
260      return GL_FALSE;
261   }
262
263   if (ctx->Pixel.ZoomX != 1.0F) {
264      if (INTEL_DEBUG & DEBUG_PIXEL)
265         _mesa_printf("%s - bad PixelZoomX for blit\n", __FUNCTION__);
266      return GL_FALSE;
267   }
268
269
270   if (unpack->RowLength > 0)
271      rowLength = unpack->RowLength;
272   else
273      rowLength = width;
274
275   if (ctx->Pixel.ZoomY == -1.0F) {
276      if (INTEL_DEBUG & DEBUG_PIXEL)
277         _mesa_printf("%s - bad PixelZoomY for blit\n", __FUNCTION__);
278      return GL_FALSE;          /* later */
279      y -= height;
280   }
281   else if (ctx->Pixel.ZoomY == 1.0F) {
282      rowLength = -rowLength;
283   }
284   else {
285      if (INTEL_DEBUG & DEBUG_PIXEL)
286         _mesa_printf("%s - bad PixelZoomY for blit\n", __FUNCTION__);
287      return GL_FALSE;
288   }
289
290   src_offset = (GLuint) _mesa_image_address(2, unpack, pixels, width, height,
291                                             format, type, 0, 0, 0);
292
293   intelFlush(&intel->ctx);
294   LOCK_HARDWARE(intel);
295
296   if (intel->driDrawable->numClipRects) {
297      __DRIdrawablePrivate *dPriv = intel->driDrawable;
298      int nbox = dPriv->numClipRects;
299      drm_clip_rect_t *box = dPriv->pClipRects;
300      drm_clip_rect_t rect;
301      drm_clip_rect_t dest_rect;
302      dri_bo *src_buffer = intel_bufferobj_buffer(intel, src, INTEL_READ);
303      int i;
304
305      dest_rect.x1 = dPriv->x + x;
306      dest_rect.y1 = dPriv->y + dPriv->h - (y + height);
307      dest_rect.x2 = dest_rect.x1 + width;
308      dest_rect.y2 = dest_rect.y1 + height;
309
310      for (i = 0; i < nbox; i++) {
311         if (!intel_intersect_cliprects(&rect, &dest_rect, &box[i]))
312            continue;
313
314         intelEmitCopyBlit(intel,
315                           dest->cpp,
316                           rowLength, src_buffer, src_offset, GL_FALSE,
317                           dest->pitch, dest->buffer, 0, dest->tiling,
318                           rect.x1 - dest_rect.x1,
319                           rect.y2 - dest_rect.y2,
320                           rect.x1,
321                           rect.y1, rect.x2 - rect.x1, rect.y2 - rect.y1,
322			   ctx->Color.ColorLogicOpEnabled ?
323			   ctx->Color.LogicOp : GL_COPY);
324      }
325   }
326   UNLOCK_HARDWARE(intel);
327
328   if (INTEL_DEBUG & DEBUG_PIXEL)
329      _mesa_printf("%s - DONE\n", __FUNCTION__);
330
331   return GL_TRUE;
332}
333
334
335
336void
337intelDrawPixels(GLcontext * ctx,
338                GLint x, GLint y,
339                GLsizei width, GLsizei height,
340                GLenum format,
341                GLenum type,
342                const struct gl_pixelstore_attrib *unpack,
343                const GLvoid * pixels)
344{
345   if (do_blit_drawpixels(ctx, x, y, width, height, format, type,
346                          unpack, pixels))
347      return;
348
349   if (do_texture_drawpixels(ctx, x, y, width, height, format, type,
350                             unpack, pixels))
351      return;
352
353
354   if (INTEL_DEBUG & DEBUG_PIXEL)
355      _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
356
357   if (ctx->FragmentProgram._Current == ctx->FragmentProgram._TexEnvProgram) {
358      /*
359       * We don't want the i915 texenv program to be applied to DrawPixels.
360       * This is really just a performance optimization (mesa will other-
361       * wise happily run the fragment program on each pixel in the image).
362       */
363      struct gl_fragment_program *fpSave = ctx->FragmentProgram._Current;
364   /* can't just set current frag prog to 0 here as on buffer resize
365      we'll get new state checks which will segfault. Remains a hack. */
366      ctx->FragmentProgram._Current = NULL;
367      ctx->FragmentProgram._UseTexEnvProgram = GL_FALSE;
368      ctx->FragmentProgram._Active = GL_FALSE;
369      _swrast_DrawPixels( ctx, x, y, width, height, format, type,
370                          unpack, pixels );
371      ctx->FragmentProgram._Current = fpSave;
372      ctx->FragmentProgram._UseTexEnvProgram = GL_TRUE;
373      ctx->FragmentProgram._Active = GL_TRUE;
374      _swrast_InvalidateState(ctx, _NEW_PROGRAM);
375   }
376   else {
377      _swrast_DrawPixels( ctx, x, y, width, height, format, type,
378                          unpack, pixels );
379   }
380}
381