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