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