st/mesa: fix Y inversion and optimize st_BlitFramebuffer()
[mesa.git] / src / mesa / state_tracker / st_cb_blit.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 /*
29 * Authors:
30 * Brian Paul
31 */
32
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36 #include "main/texformat.h"
37 #include "shader/program.h"
38 #include "shader/prog_parameter.h"
39 #include "shader/prog_print.h"
40
41 #include "st_context.h"
42 #include "st_program.h"
43 #include "st_cb_blit.h"
44 #include "st_cb_fbo.h"
45
46 #include "util/u_blit.h"
47
48 #include "cso_cache/cso_context.h"
49
50
51 void
52 st_init_blit(struct st_context *st)
53 {
54 st->blit = util_create_blit(st->pipe, st->cso_context);
55 }
56
57
58 void
59 st_destroy_blit(struct st_context *st)
60 {
61 util_destroy_blit(st->blit);
62 st->blit = NULL;
63 }
64
65
66 static void
67 st_BlitFramebuffer(GLcontext *ctx,
68 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
69 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
70 GLbitfield mask, GLenum filter)
71 {
72 struct st_context *st = ctx->st;
73
74 const uint pFilter = ((filter == GL_NEAREST)
75 ? PIPE_TEX_MIPFILTER_NEAREST
76 : PIPE_TEX_MIPFILTER_LINEAR);
77
78 if (!_mesa_clip_blit(ctx, &srcX0, &srcY0, &srcX1, &srcY1,
79 &dstX0, &dstY0, &dstX1, &dstY1)) {
80 return; /* nothing to draw/blit */
81 }
82
83 if (mask & GL_COLOR_BUFFER_BIT) {
84 struct st_renderbuffer *srcRb =
85 st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
86 struct st_renderbuffer *dstRb =
87 st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
88 struct pipe_surface *srcSurf = srcRb->surface;
89 struct pipe_surface *dstSurf = dstRb->surface;
90
91 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
92 /* invert Y for dest */
93 dstY0 = dstRb->Base.Height - dstY0;
94 dstY1 = dstRb->Base.Height - dstY1;
95 }
96
97 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
98 /* invert Y for src */
99 srcY0 = srcRb->Base.Height - srcY0;
100 srcY1 = srcRb->Base.Height - srcY1;
101 }
102
103 if (srcY0 > srcY1 && dstY0 > dstY1) {
104 /* Both src and dst are upside down. Swap Y to make it
105 * right-side up to increase odds of using a fast path.
106 * Recall that all Gallium raster coords have Y=0=top.
107 */
108 GLint tmp;
109 tmp = srcY0;
110 srcY0 = srcY1;
111 srcY1 = tmp;
112 tmp = dstY0;
113 dstY0 = dstY1;
114 dstY1 = tmp;
115 }
116
117 util_blit_pixels(st->blit,
118 srcSurf, srcX0, srcY0, srcX1, srcY1,
119 dstSurf, dstX0, dstY0, dstX1, dstY1,
120 0.0, pFilter);
121
122 }
123 }
124
125
126
127 void
128 st_init_blit_functions(struct dd_function_table *functions)
129 {
130 #if FEATURE_EXT_framebuffer_blit
131 functions->BlitFramebuffer = st_BlitFramebuffer;
132 #endif
133 }