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