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