Merge commit 'origin/gallium-0.2' into gallium-xlib-rework
[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 "main/glheader.h"
29 #include "main/enums.h"
30 #include "main/image.h"
31 #include "main/state.h"
32 #include "main/mtypes.h"
33 #include "main/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 if (ctx->Pixel.ZoomX > 0) {
139 dstbox.x1 = dstx;
140 dstbox.x2 = dstx + width * ctx->Pixel.ZoomX;
141 } else {
142 dstbox.x1 = dstx + width * ctx->Pixel.ZoomX;
143 dstbox.x2 = dstx;
144 }
145 if (ctx->Pixel.ZoomY > 0) {
146 dstbox.y1 = dsty;
147 dstbox.y2 = dsty + height * ctx->Pixel.ZoomY;
148 } else {
149 dstbox.y1 = dsty + height * ctx->Pixel.ZoomY;
150 dstbox.y2 = dsty;
151 }
152
153 DBG("src %d,%d %d,%d\n", srcbox.x1, srcbox.y1, srcbox.x2, srcbox.y2);
154 DBG("dst %d,%d %d,%d (%dx%d) (%f,%f)\n", dstbox.x1, dstbox.y1, dstbox.x2, dstbox.y2,
155 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
156
157 if (intel_intersect_cliprects(&tmp, &srcbox, &dstbox)) {
158 DBG("%s: regions overlap\n", __FUNCTION__);
159 return GL_FALSE;
160 }
161 }
162
163 intelFlush(&intel->ctx);
164
165 intel->vtbl.install_meta_state(intel);
166
167 /* Is this true? Also will need to turn depth testing on according
168 * to state:
169 */
170 intel->vtbl.meta_no_stencil_write(intel);
171 intel->vtbl.meta_no_depth_write(intel);
172
173 /* Set the 3d engine to draw into the destination region:
174 */
175 intel->vtbl.meta_draw_region(intel, dst, intel->depth_region);
176
177 intel->vtbl.meta_import_pixel_state(intel);
178
179 if (src->cpp == 2) {
180 src_format = GL_RGB;
181 src_type = GL_UNSIGNED_SHORT_5_6_5;
182 }
183 else {
184 src_format = GL_BGRA;
185 src_type = GL_UNSIGNED_BYTE;
186 }
187
188 /* Set the frontbuffer up as a large rectangular texture.
189 */
190 if (!intel->vtbl.meta_tex_rect_source(intel, src->buffer, 0,
191 src->pitch,
192 src->height, src_format, src_type)) {
193 intel->vtbl.leave_meta_state(intel);
194 return GL_FALSE;
195 }
196
197
198 intel->vtbl.meta_texture_blend_replace(intel);
199
200 LOCK_HARDWARE(intel);
201
202 if (intel->driDrawable->numClipRects) {
203 __DRIdrawablePrivate *dPriv = intel->driDrawable;
204
205
206 srcy = dPriv->h - srcy - height; /* convert from gl to hardware coords */
207
208 srcx += dPriv->x;
209 srcy += dPriv->y;
210
211 /* Clip against the source region. This is the only source
212 * clipping we do. XXX: Just set the texcord wrap mode to clamp
213 * or similar.
214 *
215 */
216 if (0) {
217 GLint orig_x = srcx;
218 GLint orig_y = srcy;
219
220 if (!_mesa_clip_to_region(0, 0, src->pitch, src->height,
221 &srcx, &srcy, &width, &height))
222 goto out;
223
224 dstx += srcx - orig_x;
225 dsty += (srcy - orig_y) * ctx->Pixel.ZoomY;
226 }
227
228 /* Just use the regular cliprect mechanism... Does this need to
229 * even hold the lock???
230 */
231 intel->vtbl.meta_draw_quad(intel,
232 dstx,
233 dstx + width * ctx->Pixel.ZoomX,
234 dPriv->h - (dsty + height * ctx->Pixel.ZoomY),
235 dPriv->h - (dsty), 0, /* XXX: what z value? */
236 0x00ff00ff,
237 srcx, srcx + width, srcy, srcy + height);
238
239 out:
240 intel->vtbl.leave_meta_state(intel);
241 intel_batchbuffer_emit_mi_flush(intel->batch);
242 }
243 UNLOCK_HARDWARE(intel);
244
245 DBG("%s: success\n", __FUNCTION__);
246 return GL_TRUE;
247 }
248 #endif /* I915 */
249
250
251 /**
252 * CopyPixels with the blitter. Don't support zooming, pixel transfer, etc.
253 */
254 static GLboolean
255 do_blit_copypixels(GLcontext * ctx,
256 GLint srcx, GLint srcy,
257 GLsizei width, GLsizei height,
258 GLint dstx, GLint dsty, GLenum type)
259 {
260 struct intel_context *intel = intel_context(ctx);
261 struct intel_region *dst = intel_drawbuf_region(intel);
262 struct intel_region *src = copypix_src_region(intel, type);
263 struct gl_framebuffer *fb = ctx->DrawBuffer;
264 struct gl_framebuffer *read_fb = ctx->ReadBuffer;
265 unsigned int num_cliprects;
266 drm_clip_rect_t *cliprects;
267 int x_off, y_off;
268
269 /* Update draw buffer bounds */
270 _mesa_update_state(ctx);
271
272 /* Copypixels can be more than a straight copy. Ensure all the
273 * extra operations are disabled:
274 */
275 if (!intel_check_copypixel_blit_fragment_ops(ctx) ||
276 ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F)
277 return GL_FALSE;
278
279 if (!src || !dst)
280 return GL_FALSE;
281
282
283
284 intelFlush(&intel->ctx);
285
286 LOCK_HARDWARE(intel);
287
288 intel_get_cliprects(intel, &cliprects, &num_cliprects, &x_off, &y_off);
289 if (num_cliprects != 0) {
290 GLint delta_x;
291 GLint delta_y;
292 GLint orig_dstx;
293 GLint orig_dsty;
294 GLint orig_srcx;
295 GLint orig_srcy;
296 GLuint i;
297
298 /* XXX: We fail to handle different inversion between read and draw framebuffer. */
299
300 /* Clip to destination buffer. */
301 orig_dstx = dstx;
302 orig_dsty = dsty;
303 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
304 fb->_Xmax, fb->_Ymax,
305 &dstx, &dsty, &width, &height))
306 goto out;
307 /* Adjust src coords for our post-clipped destination origin */
308 srcx += dstx - orig_dstx;
309 srcy += dsty - orig_dsty;
310
311 /* Clip to source buffer. */
312 orig_srcx = srcx;
313 orig_srcy = srcy;
314 if (!_mesa_clip_to_region(0, 0,
315 read_fb->Width, read_fb->Height,
316 &srcx, &srcy, &width, &height))
317 goto out;
318 /* Adjust dst coords for our post-clipped source origin */
319 dstx += srcx - orig_srcx;
320 dsty += srcy - orig_srcy;
321
322 /* Convert from GL to hardware coordinates:
323 */
324 if (fb->Name == 0) {
325 /* copypixels to a system framebuffer */
326 dstx = x_off + dstx;
327 dsty = y_off + (fb->Height - dsty - height);
328 } else {
329 /* copypixels to a user framebuffer object */
330 dstx = x_off + dstx;
331 dsty = y_off + dsty;
332 }
333
334 /* Flip source Y if it's a system framebuffer. */
335 if (read_fb->Name == 0) {
336 srcx = intel->driReadDrawable->x + srcx;
337 srcy = intel->driReadDrawable->y + (fb->Height - srcy - height);
338 }
339
340 delta_x = srcx - dstx;
341 delta_y = srcy - dsty;
342 /* Could do slightly more clipping: Eg, take the intersection of
343 * the destination cliprects and the read drawable cliprects
344 *
345 * This code will not overwrite other windows, but will
346 * introduce garbage when copying from obscured window regions.
347 */
348 for (i = 0; i < num_cliprects; i++) {
349 GLint clip_x = dstx;
350 GLint clip_y = dsty;
351 GLint clip_w = width;
352 GLint clip_h = height;
353
354 if (!_mesa_clip_to_region(cliprects[i].x1, cliprects[i].y1,
355 cliprects[i].x2, cliprects[i].y2,
356 &clip_x, &clip_y, &clip_w, &clip_h))
357 continue;
358
359 intelEmitCopyBlit(intel, dst->cpp,
360 src->pitch, src->buffer, 0, src->tiling,
361 dst->pitch, dst->buffer, 0, dst->tiling,
362 clip_x + delta_x, clip_y + delta_y, /* srcx, srcy */
363 clip_x, clip_y, /* dstx, dsty */
364 clip_w, clip_h,
365 ctx->Color.ColorLogicOpEnabled ?
366 ctx->Color.LogicOp : GL_COPY);
367 }
368 }
369 out:
370 UNLOCK_HARDWARE(intel);
371
372 DBG("%s: success\n", __FUNCTION__);
373 return GL_TRUE;
374 }
375
376
377 void
378 intelCopyPixels(GLcontext * ctx,
379 GLint srcx, GLint srcy,
380 GLsizei width, GLsizei height,
381 GLint destx, GLint desty, GLenum type)
382 {
383 if (INTEL_DEBUG & DEBUG_PIXEL)
384 fprintf(stderr, "%s\n", __FUNCTION__);
385
386 if (do_blit_copypixels(ctx, srcx, srcy, width, height, destx, desty, type))
387 return;
388
389 #ifdef I915
390 if (do_texture_copypixels(ctx, srcx, srcy, width, height, destx, desty, type))
391 return;
392 #endif
393
394 DBG("fallback to _swrast_CopyPixels\n");
395
396 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type);
397 }