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