intel_pixel_copy.c revision 9e68e191ac9d32f2f93e840a66127e724b442756
19e68e191ac9d32f2f93e840a66127e724b442756Eric Anholt/**************************************************************************
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 "image.h"
31#include "state.h"
32#include "mtypes.h"
33#include "macros.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_buffers.h"
41#include "intel_blit.h"
42#include "intel_regions.h"
43#include "intel_tris.h"
44#include "intel_pixel.h"
45
46#define FILE_DEBUG_FLAG DEBUG_PIXEL
47
48static struct intel_region *
49copypix_src_region(struct intel_context *intel, GLenum type)
50{
51   switch (type) {
52   case GL_COLOR:
53      return intel_readbuf_region(intel);
54   case GL_DEPTH:
55      /* Don't think this is really possible execpt at 16bpp, when we have no stencil.
56       */
57      if (intel->depth_region && intel->depth_region->cpp == 2)
58         return intel->depth_region;
59   case GL_STENCIL:
60      /* Don't think this is really possible.
61       */
62      break;
63   case GL_DEPTH_STENCIL_EXT:
64      /* Does it matter whether it is stencil/depth or depth/stencil?
65       */
66      return intel->depth_region;
67   default:
68      break;
69   }
70
71   return NULL;
72}
73
74
75/**
76 * Check if any fragment operations are in effect which might effect
77 * glCopyPixels.  Differs from intel_check_blit_fragment_ops in that
78 * we allow Scissor.
79 */
80static GLboolean
81intel_check_copypixel_blit_fragment_ops(GLcontext * ctx)
82{
83   if (ctx->NewState)
84      _mesa_update_state(ctx);
85
86   /* Could do logicop with the blitter:
87    */
88   return !(ctx->_ImageTransferState ||
89            ctx->Color.AlphaEnabled ||
90            ctx->Depth.Test ||
91            ctx->Fog.Enabled ||
92            ctx->Stencil.Enabled ||
93            !ctx->Color.ColorMask[0] ||
94            !ctx->Color.ColorMask[1] ||
95            !ctx->Color.ColorMask[2] ||
96            !ctx->Color.ColorMask[3] ||
97            ctx->Texture._EnabledUnits ||
98	    ctx->FragmentProgram._Enabled ||
99	    ctx->Color.BlendEnabled);
100}
101
102/* Doesn't work for overlapping regions.  Could do a double copy or
103 * just fallback.
104 */
105static GLboolean
106do_texture_copypixels(GLcontext * ctx,
107                      GLint srcx, GLint srcy,
108                      GLsizei width, GLsizei height,
109                      GLint dstx, GLint dsty, GLenum type)
110{
111   struct intel_context *intel = intel_context(ctx);
112   struct intel_region *dst = intel_drawbuf_region(intel);
113   struct intel_region *src = copypix_src_region(intel, type);
114   GLenum src_format;
115   GLenum src_type;
116
117   DBG("%s %d,%d %dx%d --> %d,%d\n", __FUNCTION__,
118       srcx, srcy, width, height, dstx, dsty);
119
120   if (!src || !dst || type != GL_COLOR)
121      return GL_FALSE;
122
123   /* Can't handle overlapping regions.  Don't have sufficient control
124    * over rasterization to pull it off in-place.  Punt on these for
125    * now.
126    *
127    * XXX: do a copy to a temporary.
128    */
129   if (src->buffer == dst->buffer) {
130      drm_clip_rect_t srcbox;
131      drm_clip_rect_t dstbox;
132      drm_clip_rect_t tmp;
133
134      srcbox.x1 = srcx;
135      srcbox.y1 = srcy;
136      srcbox.x2 = srcx + width;
137      srcbox.y2 = srcy + height;
138
139      dstbox.x1 = dstx;
140      dstbox.y1 = dsty;
141      dstbox.x2 = dstx + width * ctx->Pixel.ZoomX;
142      dstbox.y2 = dsty + height * ctx->Pixel.ZoomY;
143
144      DBG("src %d,%d %d,%d\n", srcbox.x1, srcbox.y1, srcbox.x2, srcbox.y2);
145      DBG("dst %d,%d %d,%d (%dx%d) (%f,%f)\n", dstbox.x1, dstbox.y1, dstbox.x2, dstbox.y2,
146	  width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
147
148      if (intel_intersect_cliprects(&tmp, &srcbox, &dstbox)) {
149         DBG("%s: regions overlap\n", __FUNCTION__);
150         return GL_FALSE;
151      }
152   }
153
154   intelFlush(&intel->ctx);
155
156   intel->vtbl.install_meta_state(intel);
157
158   /* Is this true?  Also will need to turn depth testing on according
159    * to state:
160    */
161   intel->vtbl.meta_no_stencil_write(intel);
162   intel->vtbl.meta_no_depth_write(intel);
163
164   /* Set the 3d engine to draw into the destination region:
165    */
166   intel->vtbl.meta_draw_region(intel, dst, intel->depth_region);
167
168   intel->vtbl.meta_import_pixel_state(intel);
169
170   if (src->cpp == 2) {
171      src_format = GL_RGB;
172      src_type = GL_UNSIGNED_SHORT_5_6_5;
173   }
174   else {
175      src_format = GL_BGRA;
176      src_type = GL_UNSIGNED_BYTE;
177   }
178
179   /* Set the frontbuffer up as a large rectangular texture.
180    */
181   if (!intel->vtbl.meta_tex_rect_source(intel, src->buffer, 0,
182                                         src->pitch,
183                                         src->height, src_format, src_type)) {
184      intel->vtbl.leave_meta_state(intel);
185      return GL_FALSE;
186   }
187
188
189   intel->vtbl.meta_texture_blend_replace(intel);
190
191   LOCK_HARDWARE(intel);
192
193   if (intel->driDrawable->numClipRects) {
194      __DRIdrawablePrivate *dPriv = intel->driDrawable;
195
196
197      srcy = dPriv->h - srcy - height;  /* convert from gl to hardware coords */
198
199      srcx += dPriv->x;
200      srcy += dPriv->y;
201
202      /* Clip against the source region.  This is the only source
203       * clipping we do.  XXX: Just set the texcord wrap mode to clamp
204       * or similar.
205       *
206       */
207      if (0) {
208         GLint orig_x = srcx;
209         GLint orig_y = srcy;
210
211         if (!_mesa_clip_to_region(0, 0, src->pitch, src->height,
212                                   &srcx, &srcy, &width, &height))
213            goto out;
214
215         dstx += srcx - orig_x;
216         dsty += (srcy - orig_y) * ctx->Pixel.ZoomY;
217      }
218
219      /* Just use the regular cliprect mechanism...  Does this need to
220       * even hold the lock???
221       */
222      intel->vtbl.meta_draw_quad(intel,
223				 dstx,
224				 dstx + width * ctx->Pixel.ZoomX,
225				 dPriv->h - (dsty + height * ctx->Pixel.ZoomY),
226				 dPriv->h - (dsty), 0, /* XXX: what z value? */
227				 0x00ff00ff,
228				 srcx, srcx + width, srcy, srcy + height);
229
230    out:
231      intel->vtbl.leave_meta_state(intel);
232      intel_batchbuffer_flush(intel->batch);
233   }
234   UNLOCK_HARDWARE(intel);
235
236   DBG("%s: success\n", __FUNCTION__);
237   return GL_TRUE;
238}
239
240
241
242
243
244/**
245 * CopyPixels with the blitter.  Don't support zooming, pixel transfer, etc.
246 */
247static GLboolean
248do_blit_copypixels(GLcontext * ctx,
249                   GLint srcx, GLint srcy,
250                   GLsizei width, GLsizei height,
251                   GLint dstx, GLint dsty, GLenum type)
252{
253   struct intel_context *intel = intel_context(ctx);
254   struct intel_region *dst = intel_drawbuf_region(intel);
255   struct intel_region *src = copypix_src_region(intel, type);
256
257   /* Copypixels can be more than a straight copy.  Ensure all the
258    * extra operations are disabled:
259    */
260   if (!intel_check_copypixel_blit_fragment_ops(ctx) ||
261       ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F)
262      return GL_FALSE;
263
264   if (!src || !dst)
265      return GL_FALSE;
266
267
268
269   intelFlush(&intel->ctx);
270
271   LOCK_HARDWARE(intel);
272
273   if (intel->driDrawable->numClipRects) {
274      __DRIdrawablePrivate *dPriv = intel->driDrawable;
275      drm_clip_rect_t *box = dPriv->pClipRects;
276      drm_clip_rect_t dest_rect;
277      GLint nbox = dPriv->numClipRects;
278      GLint delta_x = 0;
279      GLint delta_y = 0;
280      GLuint i;
281
282      /* Do scissoring in GL coordinates:
283       */
284      if (ctx->Scissor.Enabled)
285      {
286	 GLint x = ctx->Scissor.X;
287	 GLint y = ctx->Scissor.Y;
288	 GLuint w = ctx->Scissor.Width;
289	 GLuint h = ctx->Scissor.Height;
290	 GLint dx = dstx - srcx;
291         GLint dy = dsty - srcy;
292
293         if (!_mesa_clip_to_region(x, y, x+w-1, y+h-1, &dstx, &dsty, &width, &height))
294            goto out;
295
296         srcx = dstx - dx;
297         srcy = dsty - dy;
298      }
299
300      /* Convert from GL to hardware coordinates:
301       */
302      dsty = dPriv->h - dsty - height;
303      srcy = dPriv->h - srcy - height;
304      dstx += dPriv->x;
305      dsty += dPriv->y;
306      srcx += dPriv->x;
307      srcy += dPriv->y;
308
309      /* Clip against the source region.  This is the only source
310       * clipping we do.  Dst is clipped with cliprects below.
311       */
312      {
313         delta_x = srcx - dstx;
314         delta_y = srcy - dsty;
315
316         if (!_mesa_clip_to_region(0, 0, src->pitch, src->height,
317                                   &srcx, &srcy, &width, &height))
318            goto out;
319
320         dstx = srcx - delta_x;
321         dsty = srcy - delta_y;
322      }
323
324      dest_rect.x1 = dstx;
325      dest_rect.y1 = dsty;
326      dest_rect.x2 = dstx + width;
327      dest_rect.y2 = dsty + height;
328
329      /* Could do slightly more clipping: Eg, take the intersection of
330       * the existing set of cliprects and those cliprects translated
331       * by delta_x, delta_y:
332       *
333       * This code will not overwrite other windows, but will
334       * introduce garbage when copying from obscured window regions.
335       */
336      for (i = 0; i < nbox; i++) {
337         drm_clip_rect_t rect;
338
339         if (!intel_intersect_cliprects(&rect, &dest_rect, &box[i]))
340            continue;
341
342
343         intelEmitCopyBlit(intel, dst->cpp,
344			   src->pitch, src->buffer, 0, src->tiled,
345			   dst->pitch, dst->buffer, 0, dst->tiled,
346			   rect.x1 + delta_x,
347			   rect.y1 + delta_y,       /* srcx, srcy */
348                           rect.x1, rect.y1,    /* dstx, dsty */
349                           rect.x2 - rect.x1, rect.y2 - rect.y1,
350			   ctx->Color.ColorLogicOpEnabled ?
351			   ctx->Color.LogicOp : GL_COPY);
352      }
353
354    out:
355      intel_batchbuffer_flush(intel->batch);
356   }
357   UNLOCK_HARDWARE(intel);
358
359   DBG("%s: success\n", __FUNCTION__);
360   return GL_TRUE;
361}
362
363
364void
365intelCopyPixels(GLcontext * ctx,
366                GLint srcx, GLint srcy,
367                GLsizei width, GLsizei height,
368                GLint destx, GLint desty, GLenum type)
369{
370   if (INTEL_DEBUG & DEBUG_PIXEL)
371      fprintf(stderr, "%s\n", __FUNCTION__);
372
373   if (do_blit_copypixels(ctx, srcx, srcy, width, height, destx, desty, type))
374      return;
375
376   if (do_texture_copypixels(ctx, srcx, srcy, width, height, destx, desty, type))
377      return;
378
379   DBG("fallback to _swrast_CopyPixels\n");
380
381   _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type);
382}
383