Merge branch 'gallium-0.1' into gallium-tex-surfaces
[mesa.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 "pipe/p_winsys.h"
31 #include "pipe/p_util.h"
32 #include "pipe/p_context.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
43 free( failover );
44 }
45
46
47
48 static boolean failover_draw_elements( struct pipe_context *pipe,
49 struct pipe_buffer *indexBuffer,
50 unsigned indexSize,
51 unsigned prim, unsigned start, unsigned count)
52 {
53 struct failover_context *failover = failover_context( pipe );
54
55 /* If there has been any statechange since last time, try hardware
56 * rendering again:
57 */
58 if (failover->dirty) {
59 failover->mode = FO_HW;
60 }
61
62 /* Try hardware:
63 */
64 if (failover->mode == FO_HW) {
65 if (!failover->hw->draw_elements( failover->hw,
66 indexBuffer,
67 indexSize,
68 prim,
69 start,
70 count )) {
71
72 failover->hw->flush( failover->hw, ~0, NULL );
73 failover->mode = FO_SW;
74 }
75 }
76
77 /* Possibly try software:
78 */
79 if (failover->mode == FO_SW) {
80
81 if (failover->dirty)
82 failover_state_emit( failover );
83
84 failover->sw->draw_elements( failover->sw,
85 indexBuffer,
86 indexSize,
87 prim,
88 start,
89 count );
90
91 /* Be ready to switch back to hardware rendering without an
92 * intervening flush. Unlikely to be much performance impact to
93 * this:
94 */
95 failover->sw->flush( failover->sw, ~0, NULL );
96 }
97
98 return TRUE;
99 }
100
101
102 static boolean failover_draw_arrays( struct pipe_context *pipe,
103 unsigned prim, unsigned start, unsigned count)
104 {
105 return failover_draw_elements(pipe, NULL, 0, prim, start, count);
106 }
107
108
109
110 struct pipe_context *failover_create( struct pipe_context *hw,
111 struct pipe_context *sw )
112 {
113 struct failover_context *failover = CALLOC_STRUCT(failover_context);
114 if (failover == NULL)
115 return NULL;
116
117 failover->hw = hw;
118 failover->sw = sw;
119 failover->pipe.winsys = hw->winsys;
120 failover->pipe.screen = hw->screen;
121 failover->pipe.destroy = failover_destroy;
122 #if 0
123 failover->pipe.is_format_supported = hw->is_format_supported;
124 failover->pipe.get_name = hw->get_name;
125 failover->pipe.get_vendor = hw->get_vendor;
126 failover->pipe.get_param = hw->get_param;
127 failover->pipe.get_paramf = hw->get_paramf;
128 #endif
129
130 failover->pipe.draw_arrays = failover_draw_arrays;
131 failover->pipe.draw_elements = failover_draw_elements;
132 failover->pipe.clear = hw->clear;
133
134 /* No software occlusion fallback (or other optional functionality)
135 * at this point - if the hardware doesn't support it, don't
136 * advertise it to the application.
137 */
138 failover->pipe.begin_query = hw->begin_query;
139 failover->pipe.end_query = hw->end_query;
140
141 failover_init_state_functions( failover );
142
143 failover->pipe.surface_copy = hw->surface_copy;
144 failover->pipe.surface_fill = hw->surface_fill;
145
146 #if 0
147 failover->pipe.texture_create = hw->texture_create;
148 failover->pipe.texture_release = hw->texture_release;
149 failover->pipe.get_tex_surface = hw->get_tex_surface;
150 failover->pipe.texture_update = hw->texture_update;
151 #endif
152
153 failover->pipe.flush = hw->flush;
154
155 failover->dirty = 0;
156
157 return &failover->pipe;
158 }
159