Squashed commit of the following:
[profile/ivi/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
33 #include "fo_context.h"
34 #include "fo_winsys.h"
35
36
37
38 static void failover_destroy( struct pipe_context *pipe )
39 {
40    struct failover_context *failover = failover_context( pipe );
41
42    free( failover );
43 }
44
45
46 void failover_fail_over( struct failover_context *failover )
47 {
48    failover->dirty = TRUE;
49    failover->mode = FO_SW;
50 }
51
52
53 static void failover_draw_elements( struct pipe_context *pipe,
54                                     struct pipe_resource *indexResource,
55                                     unsigned indexSize,
56                                     unsigned prim, 
57                                     unsigned start, 
58                                     unsigned count)
59 {
60    struct failover_context *failover = failover_context( pipe );
61
62    /* If there has been any statechange since last time, try hardware
63     * rendering again:
64     */
65    if (failover->dirty) {
66       failover->mode = FO_HW;
67    }
68
69    /* Try hardware:
70     */
71    if (failover->mode == FO_HW) {
72       failover->hw->draw_elements( failover->hw, 
73                                    indexResource, 
74                                    indexSize, 
75                                    prim, 
76                                    start, 
77                                    count );
78    }
79
80    /* Possibly try software:
81     */
82    if (failover->mode == FO_SW) {
83
84       if (failover->dirty) {
85          failover->hw->flush( failover->hw, ~0, NULL );
86          failover_state_emit( failover );
87       }
88
89       failover->sw->draw_elements( failover->sw, 
90                                    indexResource, 
91                                    indexSize, 
92                                    prim, 
93                                    start, 
94                                    count );
95
96       /* Be ready to switch back to hardware rendering without an
97        * intervening flush.  Unlikely to be much performance impact to
98        * this:
99        */
100       failover->sw->flush( failover->sw, ~0, NULL );
101    }
102 }
103
104
105 static void failover_draw_arrays( struct pipe_context *pipe,
106                                      unsigned prim, unsigned start, unsigned count)
107 {
108    failover_draw_elements(pipe, NULL, 0, prim, start, count);
109 }
110
111 static unsigned int
112 failover_is_resource_referenced( struct pipe_context *_pipe,
113                                  struct pipe_resource *resource,
114                                  unsigned face, unsigned level)
115 {
116    struct failover_context *failover = failover_context( _pipe );
117    struct pipe_context *pipe = (failover->mode == FO_HW) ?
118       failover->hw : failover->sw;
119
120    return pipe->is_resource_referenced(pipe, resource, face, level);
121 }
122
123 struct pipe_context *failover_create( struct pipe_context *hw,
124                                       struct pipe_context *sw )
125 {
126    struct failover_context *failover = CALLOC_STRUCT(failover_context);
127    if (failover == NULL)
128       return NULL;
129
130    failover->hw = hw;
131    failover->sw = sw;
132    failover->pipe.winsys = hw->winsys;
133    failover->pipe.screen = hw->screen;
134    failover->pipe.destroy = failover_destroy;
135 #if 0
136    failover->pipe.is_format_supported = hw->is_format_supported;
137    failover->pipe.get_name = hw->get_name;
138    failover->pipe.get_vendor = hw->get_vendor;
139    failover->pipe.get_param = hw->get_param;
140    failover->pipe.get_paramf = hw->get_paramf;
141 #endif
142
143    failover->pipe.draw_arrays = failover_draw_arrays;
144    failover->pipe.draw_elements = failover_draw_elements;
145    failover->pipe.clear = hw->clear;
146
147    /* No software occlusion fallback (or other optional functionality)
148     * at this point - if the hardware doesn't support it, don't
149     * advertise it to the application.
150     */
151    failover->pipe.begin_query = hw->begin_query;
152    failover->pipe.end_query = hw->end_query;
153
154    failover_init_state_functions( failover );
155
156    failover->pipe.surface_copy = hw->surface_copy;
157    failover->pipe.surface_fill = hw->surface_fill;
158
159 #if 0
160    failover->pipe.texture_create = hw->texture_create;
161    failover->pipe.texture_destroy = hw->texture_destroy;
162    failover->pipe.get_tex_surface = hw->get_tex_surface;
163    failover->pipe.texture_update = hw->texture_update;
164 #endif
165
166    failover->pipe.flush = hw->flush;
167    failover->pipe.is_resource_referenced = failover_is_resource_referenced;
168
169    failover->dirty = 0;
170
171    return &failover->pipe;
172 }
173