gallium: drivers should reference vertex buffers
[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 "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, ~0, 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, ~0, NULL );
93 }
94 }
95
96 static unsigned int
97 failover_is_resource_referenced( struct pipe_context *_pipe,
98 struct pipe_resource *resource,
99 unsigned level, int layer)
100 {
101 struct failover_context *failover = failover_context( _pipe );
102 struct pipe_context *pipe = (failover->mode == FO_HW) ?
103 failover->hw : failover->sw;
104
105 return pipe->is_resource_referenced(pipe, resource, level, layer);
106 }
107
108 struct pipe_context *failover_create( struct pipe_context *hw,
109 struct pipe_context *sw )
110 {
111 struct failover_context *failover = CALLOC_STRUCT(failover_context);
112 if (failover == NULL)
113 return NULL;
114
115 failover->hw = hw;
116 failover->sw = sw;
117 failover->pipe.winsys = hw->winsys;
118 failover->pipe.screen = hw->screen;
119 failover->pipe.destroy = failover_destroy;
120 #if 0
121 failover->pipe.is_format_supported = hw->is_format_supported;
122 failover->pipe.get_name = hw->get_name;
123 failover->pipe.get_vendor = hw->get_vendor;
124 failover->pipe.get_param = hw->get_param;
125 failover->pipe.get_shader_param = hw->get_shader_param;
126 failover->pipe.get_paramf = hw->get_paramf;
127 #endif
128
129 failover->pipe.draw_vbo = failover_draw_vbo;
130 failover->pipe.clear = hw->clear;
131 failover->pipe.clear_render_target = hw->clear_render_target;
132 failover->pipe.clear_depth_stencil = hw->clear_depth_stencil;
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.resource_copy_region = hw->resource_copy_region;
144
145 #if 0
146 failover->pipe.resource_create = hw->resource_create;
147 failover->pipe.resource_destroy = hw->resource_destroy;
148 failover->pipe.create_surface = hw->create_surface;
149 failover->pipe.surface_destroy = hw->surface_destroy;
150 #endif
151
152 failover->pipe.flush = hw->flush;
153 failover->pipe.is_resource_referenced = failover_is_resource_referenced;
154
155 failover->dirty = 0;
156
157 return &failover->pipe;
158 }
159