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