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