1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
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 VMWARE 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  /*
29   * Authors:
30   *   Brian Paul
31   */
32 
33 
34 #include "main/image.h"
35 #include "main/macros.h"
36 
37 #include "st_context.h"
38 #include "st_texture.h"
39 #include "st_cb_bitmap.h"
40 #include "st_cb_blit.h"
41 #include "st_cb_fbo.h"
42 #include "st_cb_texture.h"
43 #include "st_manager.h"
44 #include "st_scissor.h"
45 #include "st_util.h"
46 
47 #include "util/format/u_format.h"
48 
49 static void
st_BlitFramebuffer(struct gl_context * ctx,struct gl_framebuffer * readFB,struct gl_framebuffer * drawFB,GLint srcX0,GLint srcY0,GLint srcX1,GLint srcY1,GLint dstX0,GLint dstY0,GLint dstX1,GLint dstY1,GLbitfield mask,GLenum filter)50 st_BlitFramebuffer(struct gl_context *ctx,
51                    struct gl_framebuffer *readFB,
52                    struct gl_framebuffer *drawFB,
53                    GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
54                    GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
55                    GLbitfield mask, GLenum filter)
56 {
57    const GLbitfield depthStencil = (GL_DEPTH_BUFFER_BIT |
58                                     GL_STENCIL_BUFFER_BIT);
59    struct st_context *st = st_context(ctx);
60    const uint pFilter = ((filter == GL_NEAREST)
61                          ? PIPE_TEX_FILTER_NEAREST
62                          : PIPE_TEX_FILTER_LINEAR);
63    struct {
64       GLint srcX0, srcY0, srcX1, srcY1;
65       GLint dstX0, dstY0, dstX1, dstY1;
66    } clip;
67    struct pipe_blit_info blit;
68 
69    st_manager_validate_framebuffers(st);
70 
71    /* Make sure bitmap rendering has landed in the framebuffers */
72    st_flush_bitmap_cache(st);
73    st_invalidate_readpix_cache(st);
74 
75    clip.srcX0 = srcX0;
76    clip.srcY0 = srcY0;
77    clip.srcX1 = srcX1;
78    clip.srcY1 = srcY1;
79    clip.dstX0 = dstX0;
80    clip.dstY0 = dstY0;
81    clip.dstX1 = dstX1;
82    clip.dstY1 = dstY1;
83 
84    /* NOTE: If the src and dst dimensions don't match, we cannot simply adjust
85     * the integer coordinates to account for clipping (or scissors) because that
86     * would make us cut off fractional parts, affecting the result of the blit.
87     *
88     * XXX: This should depend on mask !
89     */
90    if (!_mesa_clip_blit(ctx, readFB, drawFB,
91                         &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
92                         &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
93       return; /* nothing to draw/blit */
94    }
95    memset(&blit, 0, sizeof(struct pipe_blit_info));
96    blit.scissor_enable =
97       (dstX0 != clip.dstX0) ||
98       (dstY0 != clip.dstY0) ||
99       (dstX1 != clip.dstX1) ||
100       (dstY1 != clip.dstY1);
101 
102    if (st_fb_orientation(drawFB) == Y_0_TOP) {
103       /* invert Y for dest */
104       dstY0 = drawFB->Height - dstY0;
105       dstY1 = drawFB->Height - dstY1;
106       /* invert Y for clip */
107       clip.dstY0 = drawFB->Height - clip.dstY0;
108       clip.dstY1 = drawFB->Height - clip.dstY1;
109    }
110    if (blit.scissor_enable) {
111       blit.scissor.minx = MIN2(clip.dstX0, clip.dstX1);
112       blit.scissor.miny = MIN2(clip.dstY0, clip.dstY1);
113       blit.scissor.maxx = MAX2(clip.dstX0, clip.dstX1);
114       blit.scissor.maxy = MAX2(clip.dstY0, clip.dstY1);
115 #if 0
116       debug_printf("scissor = (%i,%i)-(%i,%i)\n",
117                    blit.scissor.minx,blit.scissor.miny,
118                    blit.scissor.maxx,blit.scissor.maxy);
119 #endif
120    }
121 
122    if (st_fb_orientation(readFB) == Y_0_TOP) {
123       /* invert Y for src */
124       srcY0 = readFB->Height - srcY0;
125       srcY1 = readFB->Height - srcY1;
126    }
127 
128    if (srcY0 > srcY1 && dstY0 > dstY1) {
129       /* Both src and dst are upside down.  Swap Y to make it
130        * right-side up to increase odds of using a fast path.
131        * Recall that all Gallium raster coords have Y=0=top.
132        */
133       GLint tmp;
134       tmp = srcY0;
135       srcY0 = srcY1;
136       srcY1 = tmp;
137       tmp = dstY0;
138       dstY0 = dstY1;
139       dstY1 = tmp;
140    }
141 
142    blit.src.box.depth = 1;
143    blit.dst.box.depth = 1;
144 
145    /* Destination dimensions have to be positive: */
146    if (dstX0 < dstX1) {
147       blit.dst.box.x = dstX0;
148       blit.src.box.x = srcX0;
149       blit.dst.box.width = dstX1 - dstX0;
150       blit.src.box.width = srcX1 - srcX0;
151    } else {
152       blit.dst.box.x = dstX1;
153       blit.src.box.x = srcX1;
154       blit.dst.box.width = dstX0 - dstX1;
155       blit.src.box.width = srcX0 - srcX1;
156    }
157    if (dstY0 < dstY1) {
158       blit.dst.box.y = dstY0;
159       blit.src.box.y = srcY0;
160       blit.dst.box.height = dstY1 - dstY0;
161       blit.src.box.height = srcY1 - srcY0;
162    } else {
163       blit.dst.box.y = dstY1;
164       blit.src.box.y = srcY1;
165       blit.dst.box.height = dstY0 - dstY1;
166       blit.src.box.height = srcY0 - srcY1;
167    }
168 
169    if (drawFB != ctx->WinSysDrawBuffer)
170       st_window_rectangles_to_blit(ctx, &blit);
171 
172    blit.filter = pFilter;
173    blit.render_condition_enable = st->has_conditional_render;
174    blit.alpha_blend = FALSE;
175 
176    if (mask & GL_COLOR_BUFFER_BIT) {
177       struct gl_renderbuffer_attachment *srcAtt =
178          &readFB->Attachment[readFB->_ColorReadBufferIndex];
179       GLuint i;
180 
181       blit.mask = PIPE_MASK_RGBA;
182 
183       if (srcAtt->Type == GL_TEXTURE) {
184          /* Make sure that the st_texture_object->pt is the current storage for
185           * our miplevel.  The finalize would happen at some point anyway, might
186           * as well be now.
187           */
188          st_finalize_texture(ctx, st->pipe, srcAtt->Texture, srcAtt->CubeMapFace);
189 
190          struct st_texture_object *srcObj = st_texture_object(srcAtt->Texture);
191 
192          if (!srcObj || !srcObj->pt) {
193             return;
194          }
195 
196          blit.src.resource = srcObj->pt;
197          blit.src.level = srcAtt->TextureLevel;
198          blit.src.box.z = srcAtt->Zoffset + srcAtt->CubeMapFace;
199          blit.src.format = srcObj->surface_based ? srcObj->surface_format : srcObj->pt->format;
200 
201          if (!ctx->Color.sRGBEnabled)
202             blit.src.format = util_format_linear(blit.src.format);
203       }
204       else {
205          struct st_renderbuffer *srcRb =
206             st_renderbuffer(readFB->_ColorReadBuffer);
207          struct pipe_surface *srcSurf;
208 
209          if (!srcRb)
210             return;
211 
212          st_update_renderbuffer_surface(st, srcRb);
213 
214          if (!srcRb->surface)
215             return;
216 
217          srcSurf = srcRb->surface;
218 
219          blit.src.resource = srcSurf->texture;
220          blit.src.level = srcSurf->u.tex.level;
221          blit.src.box.z = srcSurf->u.tex.first_layer;
222          blit.src.format = srcSurf->format;
223       }
224 
225       for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
226          struct st_renderbuffer *dstRb =
227             st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
228 
229          if (dstRb) {
230             struct pipe_surface *dstSurf;
231 
232             st_update_renderbuffer_surface(st, dstRb);
233 
234             dstSurf = dstRb->surface;
235 
236             if (dstSurf) {
237                blit.dst.resource = dstSurf->texture;
238                blit.dst.level = dstSurf->u.tex.level;
239                blit.dst.box.z = dstSurf->u.tex.first_layer;
240                blit.dst.format = dstSurf->format;
241 
242                st->pipe->blit(st->pipe, &blit);
243                dstRb->defined = true; /* front buffer tracking */
244             }
245          }
246       }
247    }
248 
249    if (mask & depthStencil) {
250       /* depth and/or stencil blit */
251 
252       /* get src/dst depth surfaces */
253       struct st_renderbuffer *srcDepthRb =
254          st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
255       struct st_renderbuffer *dstDepthRb =
256          st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
257       struct pipe_surface *dstDepthSurf =
258          dstDepthRb ? dstDepthRb->surface : NULL;
259 
260       struct st_renderbuffer *srcStencilRb =
261          st_renderbuffer(readFB->Attachment[BUFFER_STENCIL].Renderbuffer);
262       struct st_renderbuffer *dstStencilRb =
263          st_renderbuffer(drawFB->Attachment[BUFFER_STENCIL].Renderbuffer);
264       struct pipe_surface *dstStencilSurf =
265          dstStencilRb ? dstStencilRb->surface : NULL;
266 
267       if (_mesa_has_depthstencil_combined(readFB) &&
268           _mesa_has_depthstencil_combined(drawFB)) {
269          blit.mask = 0;
270          if (mask & GL_DEPTH_BUFFER_BIT)
271             blit.mask |= PIPE_MASK_Z;
272          if (mask & GL_STENCIL_BUFFER_BIT)
273             blit.mask |= PIPE_MASK_S;
274 
275          blit.dst.resource = dstDepthSurf->texture;
276          blit.dst.level = dstDepthSurf->u.tex.level;
277          blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
278          blit.dst.format = dstDepthSurf->format;
279 
280          blit.src.resource = srcDepthRb->texture;
281          blit.src.level = srcDepthRb->surface->u.tex.level;
282          blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
283          blit.src.format = srcDepthRb->surface->format;
284 
285          st->pipe->blit(st->pipe, &blit);
286       }
287       else {
288          /* blitting depth and stencil separately */
289 
290          if (mask & GL_DEPTH_BUFFER_BIT) {
291             blit.mask = PIPE_MASK_Z;
292 
293             blit.dst.resource = dstDepthSurf->texture;
294             blit.dst.level = dstDepthSurf->u.tex.level;
295             blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
296             blit.dst.format = dstDepthSurf->format;
297 
298             blit.src.resource = srcDepthRb->texture;
299             blit.src.level = srcDepthRb->surface->u.tex.level;
300             blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
301             blit.src.format = srcDepthRb->surface->format;
302 
303             st->pipe->blit(st->pipe, &blit);
304          }
305 
306          if (mask & GL_STENCIL_BUFFER_BIT) {
307             blit.mask = PIPE_MASK_S;
308 
309             blit.dst.resource = dstStencilSurf->texture;
310             blit.dst.level = dstStencilSurf->u.tex.level;
311             blit.dst.box.z = dstStencilSurf->u.tex.first_layer;
312             blit.dst.format = dstStencilSurf->format;
313 
314             blit.src.resource = srcStencilRb->texture;
315             blit.src.level = srcStencilRb->surface->u.tex.level;
316             blit.src.box.z = srcStencilRb->surface->u.tex.first_layer;
317             blit.src.format = srcStencilRb->surface->format;
318 
319             st->pipe->blit(st->pipe, &blit);
320          }
321       }
322    }
323 }
324 
325 
326 void
st_init_blit_functions(struct dd_function_table * functions)327 st_init_blit_functions(struct dd_function_table *functions)
328 {
329    functions->BlitFramebuffer = st_BlitFramebuffer;
330 }
331