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