Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / winsys / svga / drm / vmw_screen_svga.c
1 /**********************************************************
2  * Copyright 2009 VMware, Inc.  All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  **********************************************************/
25
26 /**
27  * @file
28  * This file implements the SVGA interface into this winsys, defined
29  * in drivers/svga/svga_winsys.h.
30  *
31  * @author Keith Whitwell
32  * @author Jose Fonseca
33  */
34
35
36 #include "svga_cmd.h"
37 #include "svga3d_caps.h"
38
39 #include "util/u_inlines.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "pipebuffer/pb_buffer.h"
43 #include "pipebuffer/pb_bufmgr.h"
44 #include "svga_winsys.h"
45 #include "vmw_context.h"
46 #include "vmw_screen.h"
47 #include "vmw_surface.h"
48 #include "vmw_buffer.h"
49 #include "vmw_fence.h"
50
51
52 static struct svga_winsys_buffer *
53 vmw_svga_winsys_buffer_create(struct svga_winsys_screen *sws,
54                               unsigned alignment,
55                               unsigned usage,
56                               unsigned size)
57 {
58    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
59    struct pb_desc desc;
60    struct pb_manager *provider;
61    struct pb_buffer *buffer;
62
63    memset(&desc, 0, sizeof desc);
64    desc.alignment = alignment;
65    desc.usage = usage;
66
67    provider = vws->pools.gmr_fenced;
68
69    assert(provider);
70    buffer = provider->create_buffer(provider, size, &desc);
71    if(!buffer)
72       return NULL;
73
74    return vmw_svga_winsys_buffer(buffer);
75 }
76
77
78 static void *
79 vmw_svga_winsys_buffer_map(struct svga_winsys_screen *sws,
80                            struct svga_winsys_buffer *buf,
81                            unsigned flags)
82 {
83    (void)sws;
84    return pb_map(vmw_pb_buffer(buf), flags, NULL);
85 }
86
87
88 static void
89 vmw_svga_winsys_buffer_unmap(struct svga_winsys_screen *sws,
90                              struct svga_winsys_buffer *buf)
91 {
92    (void)sws;
93    pb_unmap(vmw_pb_buffer(buf));
94 }
95
96
97 static void
98 vmw_svga_winsys_buffer_destroy(struct svga_winsys_screen *sws,
99                                struct svga_winsys_buffer *buf)
100 {
101    struct pb_buffer *pbuf = vmw_pb_buffer(buf);
102    (void)sws;
103    pb_reference(&pbuf, NULL);
104 }
105
106
107 static void
108 vmw_svga_winsys_fence_reference(struct svga_winsys_screen *sws,
109                                 struct pipe_fence_handle **pdst,
110                                 struct pipe_fence_handle *src)
111 {
112    (void)sws;
113    *pdst = src;
114 }
115
116
117 static int
118 vmw_svga_winsys_fence_signalled(struct svga_winsys_screen *sws,
119                                 struct pipe_fence_handle *fence,
120                                 unsigned flag)
121 {
122    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
123    (void)flag;
124    return vmw_ioctl_fence_signalled(vws, vmw_fence(fence));
125 }
126
127
128 static int
129 vmw_svga_winsys_fence_finish(struct svga_winsys_screen *sws,
130                              struct pipe_fence_handle *fence,
131                              unsigned flag)
132 {
133    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
134    (void)flag;
135    return vmw_ioctl_fence_finish(vws, vmw_fence(fence));
136 }
137
138
139
140 static struct svga_winsys_surface *
141 vmw_svga_winsys_surface_create(struct svga_winsys_screen *sws,
142                                SVGA3dSurfaceFlags flags,
143                                SVGA3dSurfaceFormat format,
144                                SVGA3dSize size,
145                                uint32 numFaces,
146                                uint32 numMipLevels)
147 {
148    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
149    struct vmw_svga_winsys_surface *surface;
150
151    surface = CALLOC_STRUCT(vmw_svga_winsys_surface);
152    if(!surface)
153       goto no_surface;
154
155    pipe_reference_init(&surface->refcnt, 1);
156    p_atomic_set(&surface->validated, 0);
157    surface->screen = vws;
158    surface->sid = vmw_ioctl_surface_create(vws,
159                                            flags, format, size,
160                                            numFaces, numMipLevels);
161    if(surface->sid == SVGA3D_INVALID_ID)
162       goto no_sid;
163
164    return svga_winsys_surface(surface);
165
166 no_sid:
167    FREE(surface);
168 no_surface:
169    return NULL;
170 }
171
172
173 static boolean
174 vmw_svga_winsys_surface_is_flushed(struct svga_winsys_screen *sws,
175                                    struct svga_winsys_surface *surface)
176 {
177    struct vmw_svga_winsys_surface *vsurf = vmw_svga_winsys_surface(surface);
178    return (p_atomic_read(&vsurf->validated) == 0);
179 }
180
181
182 static void
183 vmw_svga_winsys_surface_ref(struct svga_winsys_screen *sws,
184                             struct svga_winsys_surface **pDst,
185                             struct svga_winsys_surface *src)
186 {
187    struct vmw_svga_winsys_surface *d_vsurf = vmw_svga_winsys_surface(*pDst);
188    struct vmw_svga_winsys_surface *s_vsurf = vmw_svga_winsys_surface(src);
189
190    vmw_svga_winsys_surface_reference(&d_vsurf, s_vsurf);
191    *pDst = svga_winsys_surface(d_vsurf);
192 }
193
194
195 static void
196 vmw_svga_winsys_destroy(struct svga_winsys_screen *sws)
197 {
198    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
199
200    vmw_winsys_destroy(vws);
201 }
202
203
204 static SVGA3dHardwareVersion
205 vmw_svga_winsys_get_hw_version(struct svga_winsys_screen *sws)
206 {
207    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
208
209    if (!vws->ioctl.fifo_map) {
210       return 0;
211    }
212
213    return vws->ioctl.fifo_map[SVGA_FIFO_3D_HWVERSION];
214 }
215
216
217 static boolean
218 vmw_svga_winsys_get_cap(struct svga_winsys_screen *sws,
219                         SVGA3dDevCapIndex index,
220                         SVGA3dDevCapResult *result)
221 {
222    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
223    const uint32 *capsBlock;
224    const SVGA3dCapsRecord *capsRecord = NULL;
225    uint32 offset;
226    const SVGA3dCapPair *capArray;
227    int numCaps, first, last;
228
229    if(!vws->ioctl.fifo_map)
230       return FALSE;
231
232    if(vws->ioctl.fifo_map[SVGA_FIFO_3D_HWVERSION] < SVGA3D_HWVERSION_WS6_B1)
233       return FALSE;
234
235    /*
236     * Search linearly through the caps block records for the specified type.
237     */
238    capsBlock = (const uint32 *)&vws->ioctl.fifo_map[SVGA_FIFO_3D_CAPS];
239    for (offset = 0; capsBlock[offset] != 0; offset += capsBlock[offset]) {
240       const SVGA3dCapsRecord *record;
241       assert(offset < SVGA_FIFO_3D_CAPS_SIZE);
242       record = (const SVGA3dCapsRecord *) (capsBlock + offset);
243       if ((record->header.type >= SVGA3DCAPS_RECORD_DEVCAPS_MIN) &&
244           (record->header.type <= SVGA3DCAPS_RECORD_DEVCAPS_MAX) &&
245           (!capsRecord || (record->header.type > capsRecord->header.type))) {
246          capsRecord = record;
247       }
248    }
249
250    if(!capsRecord)
251       return FALSE;
252
253    /*
254     * Calculate the number of caps from the size of the record.
255     */
256    capArray = (const SVGA3dCapPair *) capsRecord->data;
257    numCaps = (int) ((capsRecord->header.length * sizeof(uint32) -
258                      sizeof capsRecord->header) / (2 * sizeof(uint32)));
259
260    /*
261     * Binary-search for the cap with the specified index.
262     */
263    for (first = 0, last = numCaps - 1; first <= last; ) {
264       int mid = (first + last) / 2;
265
266       if ((SVGA3dDevCapIndex) capArray[mid][0] == index) {
267          /*
268           * Found it.
269           */
270          result->u = capArray[mid][1];
271          return TRUE;
272       }
273
274       /*
275        * Divide and conquer.
276        */
277       if ((SVGA3dDevCapIndex) capArray[mid][0] > index) {
278          last = mid - 1;
279       } else {
280          first = mid + 1;
281       }
282    }
283
284    return FALSE;
285 }
286
287
288 boolean
289 vmw_winsys_screen_init_svga(struct vmw_winsys_screen *vws)
290 {
291    vws->base.destroy = vmw_svga_winsys_destroy;
292    vws->base.get_hw_version = vmw_svga_winsys_get_hw_version;
293    vws->base.get_cap = vmw_svga_winsys_get_cap;
294    vws->base.context_create = vmw_svga_winsys_context_create;
295    vws->base.surface_create = vmw_svga_winsys_surface_create;
296    vws->base.surface_is_flushed = vmw_svga_winsys_surface_is_flushed;
297    vws->base.surface_reference = vmw_svga_winsys_surface_ref;
298    vws->base.buffer_create = vmw_svga_winsys_buffer_create;
299    vws->base.buffer_map = vmw_svga_winsys_buffer_map;
300    vws->base.buffer_unmap = vmw_svga_winsys_buffer_unmap;
301    vws->base.buffer_destroy = vmw_svga_winsys_buffer_destroy;
302    vws->base.fence_reference = vmw_svga_winsys_fence_reference;
303    vws->base.fence_signalled = vmw_svga_winsys_fence_signalled;
304    vws->base.fence_finish = vmw_svga_winsys_fence_finish;
305
306    return TRUE;
307 }
308
309