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