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 **************************************************************************/
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"
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"
44 #define FILE_DEBUG_FLAG DEBUG_PIXEL
46 static struct intel_region
*
47 copypix_src_region(struct intel_context
*intel
, GLenum type
)
51 return intel_readbuf_region(intel
);
53 /* Don't think this is really possible execpt at 16bpp, when we have no stencil.
55 if (intel
->depth_region
&& intel
->depth_region
->cpp
== 2)
56 return intel
->depth_region
;
58 /* Don't think this is really possible.
61 case GL_DEPTH_STENCIL_EXT
:
62 /* Does it matter whether it is stencil/depth or depth/stencil?
64 return intel
->depth_region
;
74 * Check if any fragment operations are in effect which might effect
75 * glCopyPixels. Differs from intel_check_blit_fragment_ops in that
79 intel_check_copypixel_blit_fragment_ops(GLcontext
* ctx
)
82 _mesa_update_state(ctx
);
84 /* Could do logicop with the blitter:
86 return !(ctx
->_ImageTransferState
||
87 ctx
->Color
.AlphaEnabled
||
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
);
101 /* Doesn't work for overlapping regions. Could do a double copy or
105 do_texture_copypixels(GLcontext
* ctx
,
106 GLint srcx
, GLint srcy
,
107 GLsizei width
, GLsizei height
,
108 GLint dstx
, GLint dsty
, GLenum type
)
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
);
116 DBG("%s %d,%d %dx%d --> %d,%d\n", __FUNCTION__
,
117 srcx
, srcy
, width
, height
, dstx
, dsty
);
119 if (!src
|| !dst
|| type
!= GL_COLOR
)
122 /* Can't handle overlapping regions. Don't have sufficient control
123 * over rasterization to pull it off in-place. Punt on these for
126 * XXX: do a copy to a temporary.
128 if (src
->buffer
== dst
->buffer
) {
129 drm_clip_rect_t srcbox
;
130 drm_clip_rect_t dstbox
;
135 srcbox
.x2
= srcx
+ width
;
136 srcbox
.y2
= srcy
+ height
;
138 if (ctx
->Pixel
.ZoomX
> 0) {
140 dstbox
.x2
= dstx
+ width
* ctx
->Pixel
.ZoomX
;
142 dstbox
.x1
= dstx
+ width
* ctx
->Pixel
.ZoomX
;
145 if (ctx
->Pixel
.ZoomY
> 0) {
147 dstbox
.y2
= dsty
+ height
* ctx
->Pixel
.ZoomY
;
149 dstbox
.y1
= dsty
+ height
* ctx
->Pixel
.ZoomY
;
153 DBG("src %d,%d %d,%d\n", srcbox
.x1
, srcbox
.y1
, srcbox
.x2
, srcbox
.y2
);
154 DBG("dst %d,%d %d,%d (%dx%d) (%f,%f)\n", dstbox
.x1
, dstbox
.y1
, dstbox
.x2
, dstbox
.y2
,
155 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
157 if (intel_intersect_cliprects(&tmp
, &srcbox
, &dstbox
)) {
158 DBG("%s: regions overlap\n", __FUNCTION__
);
163 intelFlush(&intel
->ctx
);
165 intel
->vtbl
.install_meta_state(intel
);
167 /* Is this true? Also will need to turn depth testing on according
170 intel
->vtbl
.meta_no_stencil_write(intel
);
171 intel
->vtbl
.meta_no_depth_write(intel
);
173 /* Set the 3d engine to draw into the destination region:
175 intel
->vtbl
.meta_draw_region(intel
, dst
, intel
->depth_region
);
177 intel
->vtbl
.meta_import_pixel_state(intel
);
181 src_type
= GL_UNSIGNED_SHORT_5_6_5
;
184 src_format
= GL_BGRA
;
185 src_type
= GL_UNSIGNED_BYTE
;
188 /* Set the frontbuffer up as a large rectangular texture.
190 if (!intel
->vtbl
.meta_tex_rect_source(intel
, src
->buffer
, 0,
192 src
->height
, src_format
, src_type
)) {
193 intel
->vtbl
.leave_meta_state(intel
);
198 intel
->vtbl
.meta_texture_blend_replace(intel
);
200 LOCK_HARDWARE(intel
);
202 if (intel
->driDrawable
->numClipRects
) {
203 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
206 srcy
= dPriv
->h
- srcy
- height
; /* convert from gl to hardware coords */
211 /* Clip against the source region. This is the only source
212 * clipping we do. XXX: Just set the texcord wrap mode to clamp
220 if (!_mesa_clip_to_region(0, 0, src
->pitch
, src
->height
,
221 &srcx
, &srcy
, &width
, &height
))
224 dstx
+= srcx
- orig_x
;
225 dsty
+= (srcy
- orig_y
) * ctx
->Pixel
.ZoomY
;
228 /* Just use the regular cliprect mechanism... Does this need to
229 * even hold the lock???
231 intel
->vtbl
.meta_draw_quad(intel
,
233 dstx
+ width
* ctx
->Pixel
.ZoomX
,
234 dPriv
->h
- (dsty
+ height
* ctx
->Pixel
.ZoomY
),
235 dPriv
->h
- (dsty
), 0, /* XXX: what z value? */
237 srcx
, srcx
+ width
, srcy
, srcy
+ height
);
240 intel
->vtbl
.leave_meta_state(intel
);
241 intel_batchbuffer_emit_mi_flush(intel
->batch
);
243 UNLOCK_HARDWARE(intel
);
245 DBG("%s: success\n", __FUNCTION__
);
252 * CopyPixels with the blitter. Don't support zooming, pixel transfer, etc.
255 do_blit_copypixels(GLcontext
* ctx
,
256 GLint srcx
, GLint srcy
,
257 GLsizei width
, GLsizei height
,
258 GLint dstx
, GLint dsty
, GLenum type
)
260 struct intel_context
*intel
= intel_context(ctx
);
261 struct intel_region
*dst
= intel_drawbuf_region(intel
);
262 struct intel_region
*src
= copypix_src_region(intel
, type
);
264 /* Copypixels can be more than a straight copy. Ensure all the
265 * extra operations are disabled:
267 if (!intel_check_copypixel_blit_fragment_ops(ctx
) ||
268 ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
)
276 intelFlush(&intel
->ctx
);
278 LOCK_HARDWARE(intel
);
280 if (intel
->driDrawable
->numClipRects
) {
281 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
282 __DRIdrawablePrivate
*dReadPriv
= intel
->driReadDrawable
;
283 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
284 GLint nbox
= dPriv
->numClipRects
;
289 /* Do scissoring in GL coordinates:
291 if (ctx
->Scissor
.Enabled
)
293 GLint x
= ctx
->Scissor
.X
;
294 GLint y
= ctx
->Scissor
.Y
;
295 GLuint w
= ctx
->Scissor
.Width
;
296 GLuint h
= ctx
->Scissor
.Height
;
297 GLint dx
= dstx
- srcx
;
298 GLint dy
= dsty
- srcy
;
300 if (!_mesa_clip_to_region(x
, y
, x
+w
-1, y
+h
-1, &dstx
, &dsty
, &width
, &height
))
307 /* Convert from GL to hardware coordinates:
309 dsty
= dPriv
->h
- dsty
- height
;
310 srcy
= dPriv
->h
- srcy
- height
;
313 srcx
+= dReadPriv
->x
;
314 srcy
+= dReadPriv
->y
;
316 /* Clip against the source region. This is the only source
317 * clipping we do. Dst is clipped with cliprects below.
320 delta_x
= srcx
- dstx
;
321 delta_y
= srcy
- dsty
;
323 if (!_mesa_clip_to_region(0, 0, src
->pitch
, src
->height
,
324 &srcx
, &srcy
, &width
, &height
))
327 dstx
= srcx
- delta_x
;
328 dsty
= srcy
- delta_y
;
331 /* Could do slightly more clipping: Eg, take the intersection of
332 * the existing set of cliprects and those cliprects translated
333 * by delta_x, delta_y:
335 * This code will not overwrite other windows, but will
336 * introduce garbage when copying from obscured window regions.
338 for (i
= 0; i
< nbox
; i
++) {
341 GLint clip_w
= width
;
342 GLint clip_h
= height
;
344 if (!_mesa_clip_to_region(box
[i
].x1
, box
[i
].y1
, box
[i
].x2
, box
[i
].y2
,
345 &clip_x
, &clip_y
, &clip_w
, &clip_h
))
348 intelEmitCopyBlit(intel
, dst
->cpp
,
349 src
->pitch
, src
->buffer
, 0, src
->tiling
,
350 dst
->pitch
, dst
->buffer
, 0, dst
->tiling
,
351 clip_x
+ delta_x
, clip_y
+ delta_y
, /* srcx, srcy */
352 clip_x
, clip_y
, /* dstx, dsty */
354 ctx
->Color
.ColorLogicOpEnabled
?
355 ctx
->Color
.LogicOp
: GL_COPY
);
359 UNLOCK_HARDWARE(intel
);
361 DBG("%s: success\n", __FUNCTION__
);
367 intelCopyPixels(GLcontext
* ctx
,
368 GLint srcx
, GLint srcy
,
369 GLsizei width
, GLsizei height
,
370 GLint destx
, GLint desty
, GLenum type
)
372 if (INTEL_DEBUG
& DEBUG_PIXEL
)
373 fprintf(stderr
, "%s\n", __FUNCTION__
);
375 if (do_blit_copypixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
))
379 if (do_texture_copypixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
))
383 DBG("fallback to _swrast_CopyPixels\n");
385 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);