Tizen 2.0 Release
[profile/ivi/osmesa.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/mfeatures.h"
37
38 #include "st_context.h"
39 #include "st_texture.h"
40 #include "st_cb_blit.h"
41 #include "st_cb_fbo.h"
42 #include "st_atom.h"
43
44 #include "util/u_blit.h"
45
46
47 void
48 st_init_blit(struct st_context *st)
49 {
50    st->blit = util_create_blit(st->pipe, st->cso_context);
51 }
52
53
54 void
55 st_destroy_blit(struct st_context *st)
56 {
57    util_destroy_blit(st->blit);
58    st->blit = NULL;
59 }
60
61
62 #if FEATURE_EXT_framebuffer_blit
63
64 static void
65 st_BlitFramebuffer(struct gl_context *ctx,
66                    GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
67                    GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
68                    GLbitfield mask, GLenum filter)
69 {
70    const GLbitfield depthStencil = (GL_DEPTH_BUFFER_BIT |
71                                     GL_STENCIL_BUFFER_BIT);
72    struct st_context *st = st_context(ctx);
73    const uint pFilter = ((filter == GL_NEAREST)
74                          ? PIPE_TEX_MIPFILTER_NEAREST
75                          : PIPE_TEX_MIPFILTER_LINEAR);
76    struct gl_framebuffer *readFB = ctx->ReadBuffer;
77    struct gl_framebuffer *drawFB = ctx->DrawBuffer;
78
79    st_validate_state(st);
80
81    if (!_mesa_clip_blit(ctx, &srcX0, &srcY0, &srcX1, &srcY1,
82                         &dstX0, &dstY0, &dstX1, &dstY1)) {
83       return; /* nothing to draw/blit */
84    }
85
86    if (st_fb_orientation(drawFB) == Y_0_TOP) {
87       /* invert Y for dest */
88       dstY0 = drawFB->Height - dstY0;
89       dstY1 = drawFB->Height - dstY1;
90    }
91
92    if (st_fb_orientation(readFB) == Y_0_TOP) {
93       /* invert Y for src */
94       srcY0 = readFB->Height - srcY0;
95       srcY1 = readFB->Height - srcY1;
96    }
97
98    if (srcY0 > srcY1 && dstY0 > dstY1) {
99       /* Both src and dst are upside down.  Swap Y to make it
100        * right-side up to increase odds of using a fast path.
101        * Recall that all Gallium raster coords have Y=0=top.
102        */
103       GLint tmp;
104       tmp = srcY0;
105       srcY0 = srcY1;
106       srcY1 = tmp;
107       tmp = dstY0;
108       dstY0 = dstY1;
109       dstY1 = tmp;
110    }
111
112    /* Disable conditional rendering. */
113    if (st->render_condition) {
114       st->pipe->render_condition(st->pipe, NULL, 0);
115    }
116
117    if (mask & GL_COLOR_BUFFER_BIT) {
118       struct gl_renderbuffer_attachment *srcAtt =
119          &readFB->Attachment[readFB->_ColorReadBufferIndex];
120
121       if(srcAtt->Type == GL_TEXTURE) {
122          struct st_texture_object *srcObj =
123             st_texture_object(srcAtt->Texture);
124          struct st_renderbuffer *dstRb =
125             st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
126          struct pipe_surface *dstSurf = dstRb->surface;
127
128          if (!srcObj->pt)
129             goto done;
130
131          util_blit_pixels(st->blit, srcObj->pt, srcAtt->TextureLevel,
132                           srcX0, srcY0, srcX1, srcY1,
133                           srcAtt->Zoffset + srcAtt->CubeMapFace,
134                           dstSurf, dstX0, dstY0, dstX1, dstY1,
135                           0.0, pFilter);
136       }
137       else {
138          struct st_renderbuffer *srcRb =
139             st_renderbuffer(readFB->_ColorReadBuffer);
140          struct st_renderbuffer *dstRb =
141             st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
142          struct pipe_surface *srcSurf = srcRb->surface;
143          struct pipe_surface *dstSurf = dstRb->surface;
144
145          util_blit_pixels(st->blit,
146                           srcRb->texture, srcSurf->u.tex.level,
147                           srcX0, srcY0, srcX1, srcY1,
148                           srcSurf->u.tex.first_layer,
149                           dstSurf, dstX0, dstY0, dstX1, dstY1,
150                           0.0, pFilter);
151       }
152    }
153
154    if (mask & depthStencil) {
155       /* depth and/or stencil blit */
156
157       /* get src/dst depth surfaces */
158       struct gl_renderbuffer_attachment *srcDepth =
159          &readFB->Attachment[BUFFER_DEPTH];
160       struct gl_renderbuffer_attachment *dstDepth =
161          &drawFB->Attachment[BUFFER_DEPTH];
162       struct gl_renderbuffer_attachment *srcStencil =
163          &readFB->Attachment[BUFFER_STENCIL];
164       struct gl_renderbuffer_attachment *dstStencil =
165          &drawFB->Attachment[BUFFER_STENCIL];
166
167       struct st_renderbuffer *srcDepthRb =
168          st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
169       struct st_renderbuffer *dstDepthRb = 
170          st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
171       struct pipe_surface *dstDepthSurf =
172          dstDepthRb ? dstDepthRb->surface : NULL;
173
174       if ((mask & depthStencil) == depthStencil &&
175           st_is_depth_stencil_combined(srcDepth, srcStencil) &&
176           st_is_depth_stencil_combined(dstDepth, dstStencil)) {
177
178          /* Blitting depth and stencil values between combined
179           * depth/stencil buffers.  This is the ideal case for such buffers.
180           */
181          util_blit_pixels(st->blit,
182                           srcDepthRb->texture,
183                           srcDepthRb->surface->u.tex.level,
184                           srcX0, srcY0, srcX1, srcY1,
185                           srcDepthRb->surface->u.tex.first_layer,
186                           dstDepthSurf, dstX0, dstY0, dstX1, dstY1,
187                           0.0, pFilter);
188       }
189       else {
190          /* blitting depth and stencil separately */
191
192          if (mask & GL_DEPTH_BUFFER_BIT) {
193             util_blit_pixels(st->blit, srcDepthRb->texture,
194                              srcDepthRb->surface->u.tex.level,
195                              srcX0, srcY0, srcX1, srcY1,
196                              srcDepthRb->surface->u.tex.first_layer,
197                              dstDepthSurf, dstX0, dstY0, dstX1, dstY1,
198                              0.0, pFilter);
199          }
200
201          if (mask & GL_STENCIL_BUFFER_BIT) {
202             /* blit stencil only */
203             _mesa_problem(ctx, "st_BlitFramebuffer(STENCIL) not completed");
204          }
205       }
206    }
207
208 done:
209    /* Restore conditional rendering state. */
210    if (st->render_condition) {
211       st->pipe->render_condition(st->pipe, st->render_condition,
212                                  st->condition_mode);
213    }
214 }
215
216
217 void
218 st_init_blit_functions(struct dd_function_table *functions)
219 {
220    functions->BlitFramebuffer = st_BlitFramebuffer;
221 }
222
223 #endif /* FEATURE_EXT_framebuffer_blit */