Squashed commit of the following:
[profile/ivi/mesa.git] / src / gallium / state_trackers / dri / common / dri1_helper.c
1 /**************************************************************************
2  *
3  * Copyright 2009, 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  * Management of pipe objects (surface / pipe / fences) used by DRI1 and DRISW.
29  *
30  * Author: Keith Whitwell <keithw@vmware.com>
31  * Author: Jakob Bornecrantz <wallbraker@gmail.com>
32  */
33
34 #include "util/u_inlines.h"
35 #include "pipe/p_context.h"
36
37 #include "dri_screen.h"
38 #include "dri_context.h"
39 #include "dri_drawable.h"
40 #include "dri1_helper.h"
41
42 struct pipe_fence_handle *
43 dri1_swap_fences_pop_front(struct dri_drawable *draw)
44 {
45    struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
46    struct pipe_fence_handle *fence = NULL;
47
48    if (draw->cur_fences >= draw->desired_fences) {
49       screen->fence_reference(screen, &fence, draw->swap_fences[draw->tail]);
50       screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
51       --draw->cur_fences;
52       draw->tail &= DRI_SWAP_FENCES_MASK;
53    }
54    return fence;
55 }
56
57 void
58 dri1_swap_fences_push_back(struct dri_drawable *draw,
59                            struct pipe_fence_handle *fence)
60 {
61    struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
62
63    if (!fence)
64       return;
65
66    if (draw->cur_fences < DRI_SWAP_FENCES_MAX) {
67       draw->cur_fences++;
68       screen->fence_reference(screen, &draw->swap_fences[draw->head++],
69                               fence);
70       draw->head &= DRI_SWAP_FENCES_MASK;
71    }
72 }
73
74 void
75 dri1_swap_fences_clear(struct dri_drawable *drawable)
76 {
77    struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
78    struct pipe_fence_handle *fence;
79
80    while (drawable->cur_fences) {
81       fence = dri1_swap_fences_pop_front(drawable);
82       screen->fence_reference(screen, &fence, NULL);
83    }
84 }
85
86 struct pipe_surface *
87 dri1_get_pipe_surface(struct dri_drawable *drawable, struct pipe_resource *ptex)
88 {
89    struct pipe_screen *pipe_screen = dri_screen(drawable->sPriv)->pipe_screen;
90    struct pipe_surface *psurf = drawable->dri1_surface;
91
92    if (!psurf || psurf->texture != ptex) {
93       pipe_surface_reference(&drawable->dri1_surface, NULL);
94
95       drawable->dri1_surface = pipe_screen->get_tex_surface(pipe_screen,
96             ptex, 0, 0, 0, PIPE_BIND_BLIT_SOURCE);
97
98       psurf = drawable->dri1_surface;
99    }
100
101    return psurf;
102 }
103
104 void
105 dri1_destroy_pipe_surface(struct dri_drawable *drawable)
106 {
107    pipe_surface_reference(&drawable->dri1_surface, NULL);
108 }
109
110 struct pipe_context *
111 dri1_get_pipe_context(struct dri_screen *screen)
112 {
113    struct pipe_context *pipe = screen->dri1_pipe;
114
115    if (!pipe) {
116       screen->dri1_pipe =
117          screen->pipe_screen->context_create(screen->pipe_screen, NULL);
118       pipe = screen->dri1_pipe;
119    }
120
121    return pipe;
122 }
123
124 void
125 dri1_destroy_pipe_context(struct dri_screen *screen)
126 {
127    if (screen->dri1_pipe)
128       screen->dri1_pipe->destroy(screen->dri1_pipe);
129 }