Merge branch '7.8'
[mesa.git] / src / gallium / winsys / sw / dri / dri_sw_winsys.c
1 /**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
4 * All Rights Reserved.
5 * Copyright 2010 George Sapountzis <gsapountzis@gmail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "pipe/p_compiler.h"
30 #include "pipe/p_format.h"
31 #include "util/u_inlines.h"
32 #include "util/u_format.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35
36 #include "state_tracker/sw_winsys.h"
37 #include "dri_sw_winsys.h"
38
39
40 struct xm_displaytarget
41 {
42 void *data;
43 void *mapped;
44 };
45
46
47 /** Cast wrapper */
48 static INLINE struct xm_displaytarget *
49 xm_displaytarget( struct sw_displaytarget *dt )
50 {
51 return (struct xm_displaytarget *)dt;
52 }
53
54
55 /* pipe_screen::is_format_supported */
56 static boolean
57 xm_is_displaytarget_format_supported( struct sw_winsys *ws,
58 unsigned tex_usage,
59 enum pipe_format format )
60 {
61 /* TODO: check visuals or other sensible thing here */
62 return TRUE;
63 }
64
65 static INLINE int
66 bytes_per_line(unsigned stride, unsigned mul)
67 {
68 unsigned mask = mul - 1;
69
70 return ((stride * 8 + mask) & ~mask) / 8;
71 }
72
73 /* pipe_screen::texture_create DISPLAY_TARGET / SCANOUT / SHARED */
74 static struct sw_displaytarget *
75 xm_displaytarget_create(struct sw_winsys *winsys,
76 unsigned tex_usage,
77 enum pipe_format format,
78 unsigned width, unsigned height,
79 unsigned alignment,
80 unsigned *stride)
81 {
82 struct xm_displaytarget *xm_dt;
83 unsigned nblocksy, size, xm_stride, loader_stride, format_stride;
84
85 xm_dt = CALLOC_STRUCT(xm_displaytarget);
86 if(!xm_dt)
87 goto no_xm_dt;
88
89 format_stride = util_format_get_stride(format, width);
90 xm_stride = align(format_stride, alignment);
91 loader_stride = bytes_per_line(format_stride, 32);
92
93 nblocksy = util_format_get_nblocksy(format, height);
94 size = xm_stride * nblocksy;
95
96 #ifdef DEBUG
97 debug_printf("swrast format stride: %8d\n", format_stride);
98 debug_printf("swrast pipe stride : %8d\n", xm_stride);
99 debug_printf("swrast loader stride: %8d\n", loader_stride);
100 #endif
101
102 /*
103 * Allocate with the aligned stride required by the pipe but set the stride
104 * to the one hardcoded in the loaders XXX
105 */
106
107 xm_dt->data = align_malloc(size, alignment);
108 if(!xm_dt->data)
109 goto no_data;
110
111 *stride = loader_stride;
112 return (struct sw_displaytarget *)xm_dt;
113
114 no_data:
115 FREE(xm_dt);
116 no_xm_dt:
117 return NULL;
118 }
119
120 /* pipe_screen::texture_destroy */
121 static void
122 xm_displaytarget_destroy(struct sw_winsys *ws,
123 struct sw_displaytarget *dt)
124 {
125 struct xm_displaytarget *xm_dt = xm_displaytarget(dt);
126
127 if (xm_dt->data) {
128 FREE(xm_dt->data);
129 }
130
131 FREE(xm_dt);
132 }
133
134 /* pipe_context::transfer_map */
135 static void *
136 xm_displaytarget_map(struct sw_winsys *ws,
137 struct sw_displaytarget *dt,
138 unsigned flags)
139 {
140 struct xm_displaytarget *xm_dt = xm_displaytarget(dt);
141 xm_dt->mapped = xm_dt->data;
142 return xm_dt->mapped;
143 }
144
145 /* pipe_context::transfer_unmap */
146 static void
147 xm_displaytarget_unmap(struct sw_winsys *ws,
148 struct sw_displaytarget *dt)
149 {
150 struct xm_displaytarget *xm_dt = xm_displaytarget(dt);
151 xm_dt->mapped = NULL;
152 }
153
154 /* pipe_screen::texture_from_handle */
155 static struct sw_displaytarget *
156 xm_displaytarget_from_handle(struct sw_winsys *winsys,
157 const struct pipe_texture *templ,
158 struct winsys_handle *whandle,
159 unsigned *stride)
160 {
161 assert(0);
162 return NULL;
163 }
164
165 /* pipe_screen::texture_get_handle */
166 static boolean
167 xm_displaytarget_get_handle(struct sw_winsys *winsys,
168 struct sw_displaytarget *dt,
169 struct winsys_handle *whandle)
170 {
171 assert(0);
172 return FALSE;
173 }
174
175 /* pipe_screen::flush_frontbuffer */
176 static void
177 xm_displaytarget_display(struct sw_winsys *ws,
178 struct sw_displaytarget *dt,
179 void *context_private)
180 {
181 assert(0);
182 }
183
184
185 static void
186 dri_destroy_sw_winsys(struct sw_winsys *winsys)
187 {
188 FREE(winsys);
189 }
190
191 struct sw_winsys *
192 dri_create_sw_winsys(void)
193 {
194 struct sw_winsys *ws;
195
196 ws = CALLOC_STRUCT(sw_winsys);
197 if (!ws)
198 return NULL;
199
200 ws->destroy = dri_destroy_sw_winsys;
201
202 ws->is_displaytarget_format_supported = xm_is_displaytarget_format_supported;
203
204 /* screen texture functions */
205 ws->displaytarget_create = xm_displaytarget_create;
206 ws->displaytarget_destroy = xm_displaytarget_destroy;
207 ws->displaytarget_from_handle = xm_displaytarget_from_handle;
208 ws->displaytarget_get_handle = xm_displaytarget_get_handle;
209
210 /* texture functions */
211 ws->displaytarget_map = xm_displaytarget_map;
212 ws->displaytarget_unmap = xm_displaytarget_unmap;
213
214 ws->displaytarget_display = xm_displaytarget_display;
215
216 return ws;
217 }
218
219 /* vim: set sw=3 ts=8 sts=3 expandtab: */