Merge remote branch 'origin/master' into pipe-video
[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 #include "intel_fbo.h"
39
40 #define FILE_DEBUG_FLAG DEBUG_PIXEL
41
42 static struct intel_region *
43 copypix_src_region(struct intel_context *intel, GLenum type)
44 {
45 struct intel_renderbuffer *depth;
46
47 depth = (struct intel_renderbuffer *)
48 &intel->ctx.DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
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
55 * have no stencil. */
56 if (depth && depth->region->cpp == 2)
57 return depth->region;
58 case GL_STENCIL:
59 /* Don't think this is really possible. */
60 break;
61 case GL_DEPTH_STENCIL_EXT:
62 /* Does it matter whether it is stencil/depth or depth/stencil?
63 */
64 return 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(struct gl_context * 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][0] ||
92 !ctx->Color.ColorMask[0][1] ||
93 !ctx->Color.ColorMask[0][2] ||
94 !ctx->Color.ColorMask[0][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(struct gl_context * 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;
112 struct intel_region *src;
113 struct gl_framebuffer *fb = ctx->DrawBuffer;
114 struct gl_framebuffer *read_fb = ctx->ReadBuffer;
115 GLint orig_dstx;
116 GLint orig_dsty;
117 GLint orig_srcx;
118 GLint orig_srcy;
119 GLboolean flip = GL_FALSE;
120
121 if (type == GL_DEPTH || type == GL_STENCIL) {
122 fallback_debug("glCopyPixels() fallback: GL_DEPTH || GL_STENCIL\n");
123 return GL_FALSE;
124 }
125
126 /* Update draw buffer bounds */
127 _mesa_update_state(ctx);
128
129 /* Copypixels can be more than a straight copy. Ensure all the
130 * extra operations are disabled:
131 */
132 if (!intel_check_copypixel_blit_fragment_ops(ctx) ||
133 ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F)
134 return GL_FALSE;
135
136 intel_prepare_render(intel);
137
138 dst = intel_drawbuf_region(intel);
139 src = copypix_src_region(intel, type);
140
141 if (!src || !dst)
142 return GL_FALSE;
143
144 intel_flush(&intel->ctx);
145
146 /* Clip to destination buffer. */
147 orig_dstx = dstx;
148 orig_dsty = dsty;
149 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
150 fb->_Xmax, fb->_Ymax,
151 &dstx, &dsty, &width, &height))
152 goto out;
153 /* Adjust src coords for our post-clipped destination origin */
154 srcx += dstx - orig_dstx;
155 srcy += dsty - orig_dsty;
156
157 /* Clip to source buffer. */
158 orig_srcx = srcx;
159 orig_srcy = srcy;
160 if (!_mesa_clip_to_region(0, 0,
161 read_fb->Width, read_fb->Height,
162 &srcx, &srcy, &width, &height))
163 goto out;
164 /* Adjust dst coords for our post-clipped source origin */
165 dstx += srcx - orig_srcx;
166 dsty += srcy - orig_srcy;
167
168 /* Flip dest Y if it's a window system framebuffer. */
169 if (fb->Name == 0) {
170 /* copypixels to a window system framebuffer */
171 dsty = fb->Height - dsty - height;
172 flip = !flip;
173 }
174
175 /* Flip source Y if it's a window system framebuffer. */
176 if (read_fb->Name == 0) {
177 srcy = read_fb->Height - srcy - height;
178 flip = !flip;
179 }
180
181 if (!intel_region_copy(intel,
182 dst, 0, dstx, dsty,
183 src, 0, srcx, srcy,
184 width, height, flip,
185 ctx->Color.ColorLogicOpEnabled ?
186 ctx->Color.LogicOp : GL_COPY)) {
187 DBG("%s: blit failure\n", __FUNCTION__);
188 return GL_FALSE;
189 }
190
191 out:
192 intel_check_front_buffer_rendering(intel);
193
194 DBG("%s: success\n", __FUNCTION__);
195 return GL_TRUE;
196 }
197
198
199 void
200 intelCopyPixels(struct gl_context * ctx,
201 GLint srcx, GLint srcy,
202 GLsizei width, GLsizei height,
203 GLint destx, GLint desty, GLenum type)
204 {
205 DBG("%s\n", __FUNCTION__);
206
207 if (do_blit_copypixels(ctx, srcx, srcy, width, height, destx, desty, type))
208 return;
209
210 /* this will use swrast if needed */
211 _mesa_meta_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type);
212 }