1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
34 #include "swrast/swrast.h"
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"
45 #define FILE_DEBUG_FLAG DEBUG_PIXEL
47 static struct intel_region
*
48 copypix_src_region(struct intel_context
*intel
, GLenum type
)
52 return intel_readbuf_region(intel
);
54 /* Don't think this is really possible execpt at 16bpp, when we have no stencil.
56 if (intel
->depth_region
&& intel
->depth_region
->cpp
== 2)
57 return intel
->depth_region
;
59 /* Don't think this is really possible.
62 case GL_DEPTH_STENCIL_EXT
:
63 /* Does it matter whether it is stencil/depth or depth/stencil?
65 return intel
->depth_region
;
75 * Check if any fragment operations are in effect which might effect
76 * glCopyPixels. Differs from intel_check_blit_fragment_ops in that
80 intel_check_copypixel_blit_fragment_ops(GLcontext
* ctx
)
83 _mesa_update_state(ctx
);
85 /* Could do logicop with the blitter:
87 return !(ctx
->_ImageTransferState
||
88 ctx
->Color
.AlphaEnabled
||
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
);
102 /* Doesn't work for overlapping regions. Could do a double copy or
106 do_texture_copypixels(GLcontext
* ctx
,
107 GLint srcx
, GLint srcy
,
108 GLsizei width
, GLsizei height
,
109 GLint dstx
, GLint dsty
, GLenum type
)
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
);
117 DBG("%s %d,%d %dx%d --> %d,%d\n", __FUNCTION__
,
118 srcx
, srcy
, width
, height
, dstx
, dsty
);
120 if (!src
|| !dst
|| type
!= GL_COLOR
)
123 /* Can't handle overlapping regions. Don't have sufficient control
124 * over rasterization to pull it off in-place. Punt on these for
127 * XXX: do a copy to a temporary.
129 if (src
->buffer
== dst
->buffer
) {
130 drm_clip_rect_t srcbox
;
131 drm_clip_rect_t dstbox
;
136 srcbox
.x2
= srcx
+ width
;
137 srcbox
.y2
= srcy
+ height
;
141 dstbox
.x2
= dstx
+ width
* ctx
->Pixel
.ZoomX
;
142 dstbox
.y2
= dsty
+ height
* ctx
->Pixel
.ZoomY
;
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
);
148 if (intel_intersect_cliprects(&tmp
, &srcbox
, &dstbox
)) {
149 DBG("%s: regions overlap\n", __FUNCTION__
);
154 intelFlush(&intel
->ctx
);
156 intel
->vtbl
.install_meta_state(intel
);
158 /* Is this true? Also will need to turn depth testing on according
161 intel
->vtbl
.meta_no_stencil_write(intel
);
162 intel
->vtbl
.meta_no_depth_write(intel
);
164 /* Set the 3d engine to draw into the destination region:
166 intel
->vtbl
.meta_draw_region(intel
, dst
, intel
->depth_region
);
168 intel
->vtbl
.meta_import_pixel_state(intel
);
172 src_type
= GL_UNSIGNED_SHORT_5_6_5
;
175 src_format
= GL_BGRA
;
176 src_type
= GL_UNSIGNED_BYTE
;
179 /* Set the frontbuffer up as a large rectangular texture.
181 if (!intel
->vtbl
.meta_tex_rect_source(intel
, src
->buffer
, 0,
183 src
->height
, src_format
, src_type
)) {
184 intel
->vtbl
.leave_meta_state(intel
);
189 intel
->vtbl
.meta_texture_blend_replace(intel
);
191 LOCK_HARDWARE(intel
);
193 if (intel
->driDrawable
->numClipRects
) {
194 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
197 srcy
= dPriv
->h
- srcy
- height
; /* convert from gl to hardware coords */
202 /* Clip against the source region. This is the only source
203 * clipping we do. XXX: Just set the texcord wrap mode to clamp
211 if (!_mesa_clip_to_region(0, 0, src
->pitch
, src
->height
,
212 &srcx
, &srcy
, &width
, &height
))
215 dstx
+= srcx
- orig_x
;
216 dsty
+= (srcy
- orig_y
) * ctx
->Pixel
.ZoomY
;
219 /* Just use the regular cliprect mechanism... Does this need to
220 * even hold the lock???
222 intel
->vtbl
.meta_draw_quad(intel
,
224 dstx
+ width
* ctx
->Pixel
.ZoomX
,
225 dPriv
->h
- (dsty
+ height
* ctx
->Pixel
.ZoomY
),
226 dPriv
->h
- (dsty
), 0, /* XXX: what z value? */
228 srcx
, srcx
+ width
, srcy
, srcy
+ height
);
231 intel
->vtbl
.leave_meta_state(intel
);
232 intel_batchbuffer_emit_mi_flush(intel
->batch
);
234 UNLOCK_HARDWARE(intel
);
236 DBG("%s: success\n", __FUNCTION__
);
243 * CopyPixels with the blitter. Don't support zooming, pixel transfer, etc.
246 do_blit_copypixels(GLcontext
* ctx
,
247 GLint srcx
, GLint srcy
,
248 GLsizei width
, GLsizei height
,
249 GLint dstx
, GLint dsty
, GLenum type
)
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
);
255 /* Copypixels can be more than a straight copy. Ensure all the
256 * extra operations are disabled:
258 if (!intel_check_copypixel_blit_fragment_ops(ctx
) ||
259 ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
)
267 intelFlush(&intel
->ctx
);
269 LOCK_HARDWARE(intel
);
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
;
280 /* Do scissoring in GL coordinates:
282 if (ctx
->Scissor
.Enabled
)
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
;
291 if (!_mesa_clip_to_region(x
, y
, x
+w
-1, y
+h
-1, &dstx
, &dsty
, &width
, &height
))
298 /* Convert from GL to hardware coordinates:
300 dsty
= dPriv
->h
- dsty
- height
;
301 srcy
= dPriv
->h
- srcy
- height
;
304 srcx
+= dReadPriv
->x
;
305 srcy
+= dReadPriv
->y
;
307 /* Clip against the source region. This is the only source
308 * clipping we do. Dst is clipped with cliprects below.
311 delta_x
= srcx
- dstx
;
312 delta_y
= srcy
- dsty
;
314 if (!_mesa_clip_to_region(0, 0, src
->pitch
, src
->height
,
315 &srcx
, &srcy
, &width
, &height
))
318 dstx
= srcx
- delta_x
;
319 dsty
= srcy
- delta_y
;
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:
326 * This code will not overwrite other windows, but will
327 * introduce garbage when copying from obscured window regions.
329 for (i
= 0; i
< nbox
; i
++) {
332 GLint clip_w
= width
;
333 GLint clip_h
= height
;
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
))
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 */
345 ctx
->Color
.ColorLogicOpEnabled
?
346 ctx
->Color
.LogicOp
: GL_COPY
);
350 UNLOCK_HARDWARE(intel
);
352 DBG("%s: success\n", __FUNCTION__
);
358 intelCopyPixels(GLcontext
* ctx
,
359 GLint srcx
, GLint srcy
,
360 GLsizei width
, GLsizei height
,
361 GLint destx
, GLint desty
, GLenum type
)
363 if (INTEL_DEBUG
& DEBUG_PIXEL
)
364 fprintf(stderr
, "%s\n", __FUNCTION__
);
366 if (do_blit_copypixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
))
370 if (do_texture_copypixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
))
374 DBG("fallback to _swrast_CopyPixels\n");
376 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);