Merge branch 'mesa_7_6_branch'
[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 "main/glheader.h"
29 #include "main/image.h"
30 #include "main/state.h"
31 #include "main/mtypes.h"
32 #include "drivers/common/meta.h"
33
34 #include "intel_context.h"
35 #include "intel_buffers.h"
36 #include "intel_regions.h"
37 #include "intel_pixel.h"
38
39 #define FILE_DEBUG_FLAG DEBUG_PIXEL
40
41 static struct intel_region *
42 copypix_src_region(struct intel_context *intel, GLenum type)
43 {
44 switch (type) {
45 case GL_COLOR:
46 return intel_readbuf_region(intel);
47 case GL_DEPTH:
48 /* Don't think this is really possible execpt at 16bpp, when we have no stencil.
49 */
50 if (intel->depth_region && intel->depth_region->cpp == 2)
51 return intel->depth_region;
52 case GL_STENCIL:
53 /* Don't think this is really possible.
54 */
55 break;
56 case GL_DEPTH_STENCIL_EXT:
57 /* Does it matter whether it is stencil/depth or depth/stencil?
58 */
59 return intel->depth_region;
60 default:
61 break;
62 }
63
64 return NULL;
65 }
66
67
68 /**
69 * Check if any fragment operations are in effect which might effect
70 * glCopyPixels. Differs from intel_check_blit_fragment_ops in that
71 * we allow Scissor.
72 */
73 static GLboolean
74 intel_check_copypixel_blit_fragment_ops(GLcontext * ctx)
75 {
76 if (ctx->NewState)
77 _mesa_update_state(ctx);
78
79 /* Could do logicop with the blitter:
80 */
81 return !(ctx->_ImageTransferState ||
82 ctx->Color.AlphaEnabled ||
83 ctx->Depth.Test ||
84 ctx->Fog.Enabled ||
85 ctx->Stencil._Enabled ||
86 !ctx->Color.ColorMask[0] ||
87 !ctx->Color.ColorMask[1] ||
88 !ctx->Color.ColorMask[2] ||
89 !ctx->Color.ColorMask[3] ||
90 ctx->Texture._EnabledUnits ||
91 ctx->FragmentProgram._Enabled ||
92 ctx->Color.BlendEnabled);
93 }
94
95
96 /**
97 * CopyPixels with the blitter. Don't support zooming, pixel transfer, etc.
98 */
99 static GLboolean
100 do_blit_copypixels(GLcontext * ctx,
101 GLint srcx, GLint srcy,
102 GLsizei width, GLsizei height,
103 GLint dstx, GLint dsty, GLenum type)
104 {
105 struct intel_context *intel = intel_context(ctx);
106 struct intel_region *dst = intel_drawbuf_region(intel);
107 struct intel_region *src = copypix_src_region(intel, type);
108 struct gl_framebuffer *fb = ctx->DrawBuffer;
109 struct gl_framebuffer *read_fb = ctx->ReadBuffer;
110 unsigned int num_cliprects;
111 drm_clip_rect_t *cliprects;
112 int x_off, y_off;
113
114 if (type == GL_DEPTH || type == GL_STENCIL) {
115 if (INTEL_DEBUG & DEBUG_FALLBACKS)
116 fprintf(stderr, "glCopyPixels() fallback: GL_DEPTH || GL_STENCIL\n");
117 return GL_FALSE;
118 }
119
120 /* Update draw buffer bounds */
121 _mesa_update_state(ctx);
122
123 /* Copypixels can be more than a straight copy. Ensure all the
124 * extra operations are disabled:
125 */
126 if (!intel_check_copypixel_blit_fragment_ops(ctx) ||
127 ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F)
128 return GL_FALSE;
129
130 if (!src || !dst)
131 return GL_FALSE;
132
133
134
135 intelFlush(&intel->ctx);
136
137 LOCK_HARDWARE(intel);
138
139 intel_get_cliprects(intel, &cliprects, &num_cliprects, &x_off, &y_off);
140 if (num_cliprects != 0) {
141 GLint delta_x;
142 GLint delta_y;
143 GLint orig_dstx;
144 GLint orig_dsty;
145 GLint orig_srcx;
146 GLint orig_srcy;
147 GLuint i;
148
149 /* XXX: We fail to handle different inversion between read and draw framebuffer. */
150
151 /* Clip to destination buffer. */
152 orig_dstx = dstx;
153 orig_dsty = dsty;
154 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
155 fb->_Xmax, fb->_Ymax,
156 &dstx, &dsty, &width, &height))
157 goto out;
158 /* Adjust src coords for our post-clipped destination origin */
159 srcx += dstx - orig_dstx;
160 srcy += dsty - orig_dsty;
161
162 /* Clip to source buffer. */
163 orig_srcx = srcx;
164 orig_srcy = srcy;
165 if (!_mesa_clip_to_region(0, 0,
166 read_fb->Width, read_fb->Height,
167 &srcx, &srcy, &width, &height))
168 goto out;
169 /* Adjust dst coords for our post-clipped source origin */
170 dstx += srcx - orig_srcx;
171 dsty += srcy - orig_srcy;
172
173 /* Convert from GL to hardware coordinates:
174 */
175 if (fb->Name == 0) {
176 /* copypixels to a system framebuffer */
177 dstx = x_off + dstx;
178 dsty = y_off + (fb->Height - dsty - height);
179 } else {
180 /* copypixels to a user framebuffer object */
181 dstx = x_off + dstx;
182 dsty = y_off + dsty;
183 }
184
185 /* Flip source Y if it's a system framebuffer. */
186 if (read_fb->Name == 0) {
187 srcx = intel->driReadDrawable->x + srcx;
188 srcy = intel->driReadDrawable->y + (fb->Height - srcy - height);
189 }
190
191 delta_x = srcx - dstx;
192 delta_y = srcy - dsty;
193 /* Could do slightly more clipping: Eg, take the intersection of
194 * the destination cliprects and the read drawable cliprects
195 *
196 * This code will not overwrite other windows, but will
197 * introduce garbage when copying from obscured window regions.
198 */
199 for (i = 0; i < num_cliprects; i++) {
200 GLint clip_x = dstx;
201 GLint clip_y = dsty;
202 GLint clip_w = width;
203 GLint clip_h = height;
204
205 if (!_mesa_clip_to_region(cliprects[i].x1, cliprects[i].y1,
206 cliprects[i].x2, cliprects[i].y2,
207 &clip_x, &clip_y, &clip_w, &clip_h))
208 continue;
209
210 if (!intel_region_copy(intel,
211 dst, 0, clip_x, clip_y,
212 src, 0, clip_x + delta_x, clip_y + delta_y,
213 clip_w, clip_h,
214 ctx->Color.ColorLogicOpEnabled ?
215 ctx->Color.LogicOp : GL_COPY)) {
216 DBG("%s: blit failure\n", __FUNCTION__);
217 UNLOCK_HARDWARE(intel);
218 return GL_FALSE;
219 }
220 }
221 }
222 out:
223 UNLOCK_HARDWARE(intel);
224
225 DBG("%s: success\n", __FUNCTION__);
226 return GL_TRUE;
227 }
228
229
230 void
231 intelCopyPixels(GLcontext * ctx,
232 GLint srcx, GLint srcy,
233 GLsizei width, GLsizei height,
234 GLint destx, GLint desty, GLenum type)
235 {
236 if (INTEL_DEBUG & DEBUG_PIXEL)
237 fprintf(stderr, "%s\n", __FUNCTION__);
238
239 if (do_blit_copypixels(ctx, srcx, srcy, width, height, destx, desty, type))
240 return;
241
242 /* this will use swrast if needed */
243 _mesa_meta_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type);
244 }