Merge {i915,i965}/intel_context.h as intel/intel_context.h
[mesa.git] / src / mesa / drivers / dri / i965 / 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 "mtypes.h"
32 #include "macros.h"
33 #include "state.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_blit.h"
41 #include "intel_regions.h"
42 #include "intel_buffers.h"
43 #include "intel_pixel.h"
44
45 #define FILE_DEBUG_FLAG DEBUG_PIXEL
46
47 static struct intel_region *
48 copypix_src_region(struct intel_context *intel, GLenum type)
49 {
50 switch (type) {
51 case GL_COLOR:
52 return intel_readbuf_region(intel);
53 case GL_DEPTH:
54 /* Don't think this is really possible execpt at 16bpp, when we have no stencil.
55 */
56 if (intel->depth_region && intel->depth_region->cpp == 2)
57 return intel->depth_region;
58 case GL_STENCIL:
59 /* Don't think this is really possible.
60 */
61 break;
62 case GL_DEPTH_STENCIL_EXT:
63 /* Does it matter whether it is stencil/depth or depth/stencil?
64 */
65 return intel->depth_region;
66 default:
67 break;
68 }
69
70 return NULL;
71 }
72
73
74
75
76 /**
77 * Check if any fragment operations are in effect which might effect
78 * glDraw/CopyPixels.
79 */
80 GLboolean
81 intel_check_blit_fragment_ops(GLcontext * ctx)
82 {
83 if (ctx->NewState)
84 _mesa_update_state(ctx);
85
86 return !(ctx->_ImageTransferState ||
87 ctx->RenderMode != GL_RENDER ||
88 ctx->Color.AlphaEnabled ||
89 ctx->Depth.Test ||
90 ctx->Fog.Enabled ||
91 ctx->Stencil.Enabled ||
92 !ctx->Color.ColorMask[0] ||
93 !ctx->Color.ColorMask[1] ||
94 !ctx->Color.ColorMask[2] ||
95 !ctx->Color.ColorMask[3] || /* can do this! */
96 ctx->Texture._EnabledUnits ||
97 ctx->FragmentProgram._Enabled ||
98 ctx->Color.BlendEnabled);
99 }
100
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 ctx->_ImageTransferState ||
121 ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F ||
122 ctx->RenderMode != GL_RENDER ||
123 ctx->Texture._EnabledUnits ||
124 ctx->FragmentProgram._Enabled ||
125 src != dst )
126 return GL_FALSE;
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 - 1;
142 srcbox.y2 = srcy + height - 1;
143
144 dstbox.x1 = dstx;
145 dstbox.y1 = dsty;
146 dstbox.x2 = dstx + width - 1;
147 dstbox.y2 = dsty + height - 1;
148
149 DBG("src %d,%d %d,%d\n", srcbox.x1, srcbox.y1, srcbox.x2, srcbox.y2);
150 DBG("dst %d,%d %d,%d (%dx%d) (%f,%f)\n", dstbox.x1, dstbox.y1, dstbox.x2, dstbox.y2,
151 width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
152
153 if (intel_intersect_cliprects(&tmp, &srcbox, &dstbox)) {
154 DBG("%s: regions overlap\n", __FUNCTION__);
155 return GL_FALSE;
156 }
157 }
158
159 intelFlush(&intel->ctx);
160
161 intel->vtbl.install_meta_state(intel);
162
163 /* Is this true? Also will need to turn depth testing on according
164 * to state:
165 */
166 intel->vtbl.meta_no_stencil_write(intel);
167 intel->vtbl.meta_no_depth_write(intel);
168
169 /* Set the 3d engine to draw into the destination region:
170 */
171 intel->vtbl.meta_draw_region(intel, dst, intel->depth_region);
172
173 intel->vtbl.meta_import_pixel_state(intel);
174
175 if (src->cpp == 2) {
176 src_format = GL_RGB;
177 src_type = GL_UNSIGNED_SHORT_5_6_5;
178 }
179 else {
180 src_format = GL_BGRA;
181 src_type = GL_UNSIGNED_BYTE;
182 }
183
184 /* Set the frontbuffer up as a large rectangular texture.
185 */
186 intel->vtbl.meta_frame_buffer_texture( intel, srcx - dstx, srcy - dsty );
187
188 intel->vtbl.meta_texture_blend_replace(intel);
189
190 if (intel->driDrawable->numClipRects)
191 intel->vtbl.meta_draw_quad( intel,
192 dstx, dstx + width,
193 dsty, dsty + height,
194 ctx->Current.RasterPos[ 2 ],
195 0, 0.0, 0.0, 0.0, 0.0 );
196
197 intel->vtbl.leave_meta_state( intel );
198
199 DBG("%s: success\n", __FUNCTION__);
200 return GL_TRUE;
201 }
202
203 /**
204 * CopyPixels with the blitter. Don't support zooming, pixel transfer, etc.
205 */
206 static GLboolean
207 do_blit_copypixels(GLcontext * ctx,
208 GLint srcx, GLint srcy,
209 GLsizei width, GLsizei height,
210 GLint dstx, GLint dsty, GLenum type)
211 {
212 struct intel_context *intel = intel_context(ctx);
213 struct intel_region *dst = intel_drawbuf_region(intel);
214 struct intel_region *src = copypix_src_region(intel, type);
215
216 /* Copypixels can be more than a straight copy. Ensure all the
217 * extra operations are disabled:
218 */
219 if (!intel_check_blit_fragment_ops(ctx) ||
220 ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F)
221 return GL_FALSE;
222
223 if (!src || !dst)
224 return GL_FALSE;
225
226
227
228 intelFlush(&intel->ctx);
229
230 /* intel->vtbl.render_start(intel); */
231 /* intel->vtbl.emit_state(intel); */
232
233 LOCK_HARDWARE(intel);
234
235 if (intel->driDrawable->numClipRects) {
236 __DRIdrawablePrivate *dPriv = intel->driDrawable;
237 __DRIdrawablePrivate *dReadPriv = intel->driReadDrawable;
238 drm_clip_rect_t *box = dPriv->pClipRects;
239 drm_clip_rect_t dest_rect;
240 GLint nbox = dPriv->numClipRects;
241 GLint delta_x = 0;
242 GLint delta_y = 0;
243 GLuint i;
244
245 /* Do scissoring in GL coordinates:
246 */
247 if (ctx->Scissor.Enabled)
248 {
249 GLint x = ctx->Scissor.X;
250 GLint y = ctx->Scissor.Y;
251 GLuint w = ctx->Scissor.Width;
252 GLuint h = ctx->Scissor.Height;
253 GLint dx = dstx - srcx;
254 GLint dy = dsty - srcy;
255
256 if (!_mesa_clip_to_region(x, y, x+w-1, y+h-1, &dstx, &dsty, &width, &height))
257 goto out;
258
259 srcx = dstx - dx;
260 srcy = dsty - dy;
261 }
262
263 /* Convert from GL to hardware coordinates:
264 */
265 dsty = dPriv->h - dsty - height;
266 srcy = dPriv->h - srcy - height;
267 dstx += dPriv->x;
268 dsty += dPriv->y;
269 srcx += dReadPriv->x;
270 srcy += dReadPriv->y;
271
272 /* Clip against the source region. This is the only source
273 * clipping we do. Dst is clipped with cliprects below.
274 */
275 {
276 delta_x = srcx - dstx;
277 delta_y = srcy - dsty;
278
279 if (!_mesa_clip_to_region(0, 0, src->pitch, src->height,
280 &srcx, &srcy, &width, &height))
281 goto out;
282
283 dstx = srcx - delta_x;
284 dsty = srcy - delta_y;
285 }
286
287 dest_rect.x1 = dstx;
288 dest_rect.y1 = dsty;
289 dest_rect.x2 = dstx + width;
290 dest_rect.y2 = dsty + height;
291
292 /* intel->vtbl.emit_flush(intel, 0); */
293
294 /* Could do slightly more clipping: Eg, take the intersection of
295 * the existing set of cliprects and those cliprects translated
296 * by delta_x, delta_y:
297 *
298 * This code will not overwrite other windows, but will
299 * introduce garbage when copying from obscured window regions.
300 */
301 for (i = 0; i < nbox; i++) {
302 drm_clip_rect_t rect;
303
304 if (!intel_intersect_cliprects(&rect, &dest_rect, &box[i]))
305 continue;
306
307
308 intelEmitCopyBlit(intel,
309 dst->cpp,
310 src->pitch, src->buffer, 0, src->tiled,
311 dst->pitch, dst->buffer, 0, dst->tiled,
312 rect.x1 + delta_x,
313 rect.y1 + delta_y, /* srcx, srcy */
314 rect.x1, rect.y1, /* dstx, dsty */
315 rect.x2 - rect.x1, rect.y2 - rect.y1,
316 ctx->Color.ColorLogicOpEnabled ?
317 ctx->Color.LogicOp : GL_COPY);
318 }
319
320 out:
321 intel_batchbuffer_flush(intel->batch);
322 }
323 UNLOCK_HARDWARE(intel);
324 return GL_TRUE;
325 }
326
327 void
328 intelCopyPixels(GLcontext * ctx,
329 GLint srcx, GLint srcy,
330 GLsizei width, GLsizei height,
331 GLint destx, GLint desty, GLenum type)
332 {
333 if (INTEL_DEBUG & DEBUG_PIXEL)
334 fprintf(stderr, "%s\n", __FUNCTION__);
335
336 if (do_blit_copypixels(ctx, srcx, srcy, width, height, destx, desty, type))
337 return;
338
339 if (do_texture_copypixels(ctx, srcx, srcy, width, height, destx, desty, type))
340 return;
341
342 if (INTEL_DEBUG & DEBUG_PIXEL)
343 _mesa_printf("fallback to _swrast_CopyPixels\n");
344
345 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type);
346 }