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