Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / failover / fo_context.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
29 #include "pipe/p_defines.h"
30 #include "util/u_memory.h"
31 #include "pipe/p_context.h"
32 #include "util/u_inlines.h"
33
34 #include "fo_context.h"
35 #include "fo_winsys.h"
36
37
38
39 static void failover_destroy( struct pipe_context *pipe )
40 {
41    struct failover_context *failover = failover_context( pipe );
42    unsigned i;
43
44    for (i = 0; i < failover->num_vertex_buffers; i++) {
45       pipe_resource_reference(&failover->vertex_buffers[i].buffer, NULL);
46    }
47
48    FREE( failover );
49 }
50
51
52 void failover_fail_over( struct failover_context *failover )
53 {
54    failover->dirty = TRUE;
55    failover->mode = FO_SW;
56 }
57
58
59 static void failover_draw_vbo( struct pipe_context *pipe,
60                                const struct pipe_draw_info *info)
61 {
62    struct failover_context *failover = failover_context( pipe );
63
64    /* If there has been any statechange since last time, try hardware
65     * rendering again:
66     */
67    if (failover->dirty) {
68       failover->mode = FO_HW;
69    }
70
71    /* Try hardware:
72     */
73    if (failover->mode == FO_HW) {
74       failover->hw->draw_vbo( failover->hw, info );
75    }
76
77    /* Possibly try software:
78     */
79    if (failover->mode == FO_SW) {
80
81       if (failover->dirty) {
82          failover->hw->flush( failover->hw, NULL );
83          failover_state_emit( failover );
84       }
85
86       failover->sw->draw_vbo( failover->sw, info );
87
88       /* Be ready to switch back to hardware rendering without an
89        * intervening flush.  Unlikely to be much performance impact to
90        * this:
91        */
92       failover->sw->flush( failover->sw, NULL );
93    }
94 }
95
96 struct pipe_context *failover_create( struct pipe_context *hw,
97                                       struct pipe_context *sw )
98 {
99    struct failover_context *failover = CALLOC_STRUCT(failover_context);
100    if (failover == NULL)
101       return NULL;
102
103    failover->hw = hw;
104    failover->sw = sw;
105    failover->pipe.winsys = hw->winsys;
106    failover->pipe.screen = hw->screen;
107    failover->pipe.destroy = failover_destroy;
108 #if 0
109    failover->pipe.is_format_supported = hw->is_format_supported;
110    failover->pipe.get_name = hw->get_name;
111    failover->pipe.get_vendor = hw->get_vendor;
112    failover->pipe.get_param = hw->get_param;
113    failover->pipe.get_shader_param = hw->get_shader_param;
114    failover->pipe.get_paramf = hw->get_paramf;
115 #endif
116
117    failover->pipe.draw_vbo = failover_draw_vbo;
118    failover->pipe.clear = hw->clear;
119    failover->pipe.clear_render_target = hw->clear_render_target;
120    failover->pipe.clear_depth_stencil = hw->clear_depth_stencil;
121
122    /* No software occlusion fallback (or other optional functionality)
123     * at this point - if the hardware doesn't support it, don't
124     * advertise it to the application.
125     */
126    failover->pipe.begin_query = hw->begin_query;
127    failover->pipe.end_query = hw->end_query;
128
129    failover_init_state_functions( failover );
130
131    failover->pipe.resource_copy_region = hw->resource_copy_region;
132
133 #if 0
134    failover->pipe.resource_create = hw->resource_create;
135    failover->pipe.resource_destroy = hw->resource_destroy;
136    failover->pipe.create_surface = hw->create_surface;
137    failover->pipe.surface_destroy = hw->surface_destroy;
138 #endif
139
140    failover->pipe.flush = hw->flush;
141
142    failover->dirty = 0;
143
144    return &failover->pipe;
145 }
146