i965: Change brw_format_for_mesa_format to a non-static function.
[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 "main/condrender.h"
33 #include "drivers/common/meta.h"
34
35 #include "intel_context.h"
36 #include "intel_buffers.h"
37 #include "intel_regions.h"
38 #include "intel_pixel.h"
39 #include "intel_fbo.h"
40
41 #define FILE_DEBUG_FLAG DEBUG_PIXEL
42
43 static struct intel_region *
44 copypix_src_region(struct intel_context *intel, GLenum type)
45 {
46 struct intel_renderbuffer *depth;
47
48 depth = (struct intel_renderbuffer *)
49 &intel->ctx.DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
50
51 switch (type) {
52 case GL_COLOR:
53 return intel_readbuf_region(intel);
54 case GL_DEPTH:
55 /* Don't think this is really possible execpt at 16bpp, when we
56 * have no stencil. */
57 if (depth && depth->region->cpp == 2)
58 return depth->region;
59 case GL_STENCIL:
60 /* Don't think this is really possible. */
61 break;
62 case GL_DEPTH_STENCIL_EXT:
63 /* Does it matter whether it is stencil/depth or depth/stencil?
64 */
65 return depth->region;
66 default:
67 break;
68 }
69
70 return NULL;
71 }
72
73
74 /**
75 * Check if any fragment operations are in effect which might effect
76 * glCopyPixels. Differs from intel_check_blit_fragment_ops in that
77 * we allow Scissor.
78 */
79 static GLboolean
80 intel_check_copypixel_blit_fragment_ops(struct gl_context * ctx)
81 {
82 if (ctx->NewState)
83 _mesa_update_state(ctx);
84
85 /* Could do logicop with the blitter:
86 */
87 return !(ctx->_ImageTransferState ||
88 ctx->Color.AlphaEnabled ||
89 ctx->Depth.Test ||
90 ctx->Fog.Enabled ||
91 ctx->Stencil._Enabled ||
92 !ctx->Color.ColorMask[0][0] ||
93 !ctx->Color.ColorMask[0][1] ||
94 !ctx->Color.ColorMask[0][2] ||
95 !ctx->Color.ColorMask[0][3] ||
96 ctx->Texture._EnabledUnits ||
97 ctx->FragmentProgram._Enabled ||
98 ctx->Color.BlendEnabled);
99 }
100
101
102 /**
103 * CopyPixels with the blitter. Don't support zooming, pixel transfer, etc.
104 */
105 static GLboolean
106 do_blit_copypixels(struct gl_context * ctx,
107 GLint srcx, GLint srcy,
108 GLsizei width, GLsizei height,
109 GLint dstx, GLint dsty, GLenum type)
110 {
111 struct intel_context *intel = intel_context(ctx);
112 struct intel_region *dst;
113 struct intel_region *src;
114 struct gl_framebuffer *fb = ctx->DrawBuffer;
115 struct gl_framebuffer *read_fb = ctx->ReadBuffer;
116 GLint orig_dstx;
117 GLint orig_dsty;
118 GLint orig_srcx;
119 GLint orig_srcy;
120 GLboolean flip = GL_FALSE;
121
122 if (type == GL_DEPTH || type == GL_STENCIL) {
123 fallback_debug("glCopyPixels() fallback: GL_DEPTH || GL_STENCIL\n");
124 return GL_FALSE;
125 }
126
127 /* Update draw buffer bounds */
128 _mesa_update_state(ctx);
129
130 /* Copypixels can be more than a straight copy. Ensure all the
131 * extra operations are disabled:
132 */
133 if (!intel_check_copypixel_blit_fragment_ops(ctx) ||
134 ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F)
135 return GL_FALSE;
136
137 intel_prepare_render(intel);
138
139 dst = intel_drawbuf_region(intel);
140 src = copypix_src_region(intel, type);
141
142 if (!src || !dst)
143 return GL_FALSE;
144
145 intel_flush(&intel->ctx);
146
147 /* Clip to destination buffer. */
148 orig_dstx = dstx;
149 orig_dsty = dsty;
150 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
151 fb->_Xmax, fb->_Ymax,
152 &dstx, &dsty, &width, &height))
153 goto out;
154 /* Adjust src coords for our post-clipped destination origin */
155 srcx += dstx - orig_dstx;
156 srcy += dsty - orig_dsty;
157
158 /* Clip to source buffer. */
159 orig_srcx = srcx;
160 orig_srcy = srcy;
161 if (!_mesa_clip_to_region(0, 0,
162 read_fb->Width, read_fb->Height,
163 &srcx, &srcy, &width, &height))
164 goto out;
165 /* Adjust dst coords for our post-clipped source origin */
166 dstx += srcx - orig_srcx;
167 dsty += srcy - orig_srcy;
168
169 /* Flip dest Y if it's a window system framebuffer. */
170 if (fb->Name == 0) {
171 /* copypixels to a window system framebuffer */
172 dsty = fb->Height - dsty - height;
173 flip = !flip;
174 }
175
176 /* Flip source Y if it's a window system framebuffer. */
177 if (read_fb->Name == 0) {
178 srcy = read_fb->Height - srcy - height;
179 flip = !flip;
180 }
181
182 if (!intel_region_copy(intel,
183 dst, 0, dstx, dsty,
184 src, 0, srcx, srcy,
185 width, height, flip,
186 ctx->Color.ColorLogicOpEnabled ?
187 ctx->Color.LogicOp : GL_COPY)) {
188 DBG("%s: blit failure\n", __FUNCTION__);
189 return GL_FALSE;
190 }
191
192 out:
193 intel_check_front_buffer_rendering(intel);
194
195 DBG("%s: success\n", __FUNCTION__);
196 return GL_TRUE;
197 }
198
199
200 void
201 intelCopyPixels(struct gl_context * ctx,
202 GLint srcx, GLint srcy,
203 GLsizei width, GLsizei height,
204 GLint destx, GLint desty, GLenum type)
205 {
206 DBG("%s\n", __FUNCTION__);
207
208 if (!_mesa_check_conditional_render(ctx))
209 return;
210
211 if (do_blit_copypixels(ctx, srcx, srcy, width, height, destx, desty, type))
212 return;
213
214 /* this will use swrast if needed */
215 _mesa_meta_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type);
216 }