svga: Performance fixes
[mesa.git] / src / gallium / winsys / svga / drm / vmw_surface.c
1 /**********************************************************
2 * Copyright 2009-2015 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 #include "svga_cmd.h"
28 #include "util/u_debug.h"
29 #include "util/u_memory.h"
30 #include "pipe/p_defines.h"
31 #include "vmw_surface.h"
32 #include "vmw_screen.h"
33 #include "vmw_buffer.h"
34 #include "vmw_context.h"
35 #include "pipebuffer/pb_bufmgr.h"
36
37 void
38 vmw_svga_winsys_surface_init(struct svga_winsys_screen *sws,
39 struct svga_winsys_surface *srf,
40 unsigned surf_size, SVGA3dSurfaceAllFlags flags)
41 {
42 struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
43 void *data = NULL;
44 struct pb_buffer *pb_buf;
45 uint32_t pb_flags;
46 struct vmw_winsys_screen *vws = vsrf->screen;
47 pb_flags = PIPE_TRANSFER_READ_WRITE | PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
48
49 struct pb_manager *provider;
50 struct pb_desc desc;
51
52 data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf,
53 PIPE_TRANSFER_DONTBLOCK | pb_flags);
54 if (data)
55 goto out_unlock;
56
57 provider = vws->pools.mob_fenced;
58 memset(&desc, 0, sizeof(desc));
59 desc.alignment = 4096;
60 pb_buf = provider->create_buffer(provider, vsrf->size, &desc);
61 if (pb_buf != NULL) {
62 struct svga_winsys_buffer *vbuf =
63 vmw_svga_winsys_buffer_wrap(pb_buf);
64
65 data = vmw_svga_winsys_buffer_map(&vws->base, vbuf, pb_flags);
66 if (data) {
67 if (vsrf->buf) {
68 vmw_svga_winsys_buffer_destroy(&vws->base, vsrf->buf);
69 vsrf->buf = vbuf;
70 goto out_unlock;
71 } else
72 vmw_svga_winsys_buffer_destroy(&vws->base, vbuf);
73 }
74 }
75
76 data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf, pb_flags);
77 if (data == NULL)
78 goto out_unlock;
79
80 out_unlock:
81 mtx_unlock(&vsrf->mutex);
82
83 if (data)
84 {
85 if (flags & SVGA3D_SURFACE_BIND_STREAM_OUTPUT) {
86 memset(data, 0, surf_size + sizeof(SVGA3dDXSOState));
87 }
88 else {
89 memset(data, 0, surf_size);
90 }
91 }
92 mtx_lock(&vsrf->mutex);
93 vmw_svga_winsys_buffer_unmap(&vsrf->screen->base, vsrf->buf);
94 mtx_unlock(&vsrf->mutex);
95 }
96
97
98
99 void *
100 vmw_svga_winsys_surface_map(struct svga_winsys_context *swc,
101 struct svga_winsys_surface *srf,
102 unsigned flags, boolean *retry,
103 boolean *rebind)
104 {
105 struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
106 void *data = NULL;
107 struct pb_buffer *pb_buf;
108 uint32_t pb_flags;
109 struct vmw_winsys_screen *vws = vsrf->screen;
110
111 *retry = FALSE;
112 *rebind = FALSE;
113 assert((flags & (PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE)) != 0);
114 mtx_lock(&vsrf->mutex);
115
116 if (vsrf->mapcount) {
117 /* Other mappers will get confused if we discard. */
118 flags &= ~PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
119 }
120
121 vsrf->rebind = FALSE;
122
123 /*
124 * If we intend to read, there's no point discarding the
125 * data if busy.
126 */
127 if (flags & PIPE_TRANSFER_READ || vsrf->shared)
128 flags &= ~PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
129
130 /*
131 * Discard is a hint to a synchronized map.
132 */
133 if (flags & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE)
134 flags &= ~PIPE_TRANSFER_UNSYNCHRONIZED;
135
136 /*
137 * The surface is allowed to be referenced on the command stream iff
138 * we're mapping unsynchronized or discard. This is an early check.
139 * We need to recheck after a failing discard map.
140 */
141 if (!(flags & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
142 PIPE_TRANSFER_UNSYNCHRONIZED)) &&
143 p_atomic_read(&vsrf->validated)) {
144 *retry = TRUE;
145 goto out_unlock;
146 }
147
148 pb_flags = flags & (PIPE_TRANSFER_READ_WRITE | PIPE_TRANSFER_UNSYNCHRONIZED |
149 PIPE_TRANSFER_PERSISTENT);
150
151 if (flags & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
152 struct pb_manager *provider;
153 struct pb_desc desc;
154
155 /*
156 * First, if possible, try to map existing storage with DONTBLOCK.
157 */
158 if (!p_atomic_read(&vsrf->validated)) {
159 data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf,
160 PIPE_TRANSFER_DONTBLOCK | pb_flags);
161 if (data)
162 goto out_mapped;
163 }
164
165 /*
166 * Attempt to get a new buffer.
167 */
168 provider = vws->pools.mob_fenced;
169 memset(&desc, 0, sizeof(desc));
170 desc.alignment = 4096;
171 pb_buf = provider->create_buffer(provider, vsrf->size, &desc);
172 if (pb_buf != NULL) {
173 struct svga_winsys_buffer *vbuf =
174 vmw_svga_winsys_buffer_wrap(pb_buf);
175
176 data = vmw_svga_winsys_buffer_map(&vws->base, vbuf, pb_flags);
177 if (data) {
178 vsrf->rebind = TRUE;
179 /*
180 * We've discarded data on this surface and thus
181 * it's data is no longer consider referenced.
182 */
183 vmw_swc_surface_clear_reference(swc, vsrf);
184 if (vsrf->buf)
185 vmw_svga_winsys_buffer_destroy(&vws->base, vsrf->buf);
186 vsrf->buf = vbuf;
187
188 /* Rebind persistent maps immediately */
189 if (flags & PIPE_TRANSFER_PERSISTENT) {
190 *rebind = TRUE;
191 vsrf->rebind = FALSE;
192 }
193 goto out_mapped;
194 } else
195 vmw_svga_winsys_buffer_destroy(&vws->base, vbuf);
196 }
197 /*
198 * We couldn't get and map a new buffer for some reason.
199 * Fall through to an ordinary map.
200 * But tell pipe driver to flush now if already on validate list,
201 * Otherwise we'll overwrite previous contents.
202 */
203 if (!(flags & PIPE_TRANSFER_UNSYNCHRONIZED) &&
204 p_atomic_read(&vsrf->validated)) {
205 *retry = TRUE;
206 goto out_unlock;
207 }
208 }
209
210 pb_flags |= (flags & PIPE_TRANSFER_DONTBLOCK);
211 data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf, pb_flags);
212 if (data == NULL)
213 goto out_unlock;
214
215 out_mapped:
216 ++vsrf->mapcount;
217 vsrf->data = data;
218 vsrf->map_mode = flags & (PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE);
219 out_unlock:
220 mtx_unlock(&vsrf->mutex);
221 return data;
222 }
223
224
225 void
226 vmw_svga_winsys_surface_unmap(struct svga_winsys_context *swc,
227 struct svga_winsys_surface *srf,
228 boolean *rebind)
229 {
230 struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
231 mtx_lock(&vsrf->mutex);
232 if (--vsrf->mapcount == 0) {
233 *rebind = vsrf->rebind;
234 vsrf->rebind = FALSE;
235 } else {
236 *rebind = FALSE;
237 }
238 vmw_svga_winsys_buffer_unmap(&vsrf->screen->base, vsrf->buf);
239 mtx_unlock(&vsrf->mutex);
240 }
241
242 void
243 vmw_svga_winsys_surface_reference(struct vmw_svga_winsys_surface **pdst,
244 struct vmw_svga_winsys_surface *src)
245 {
246 struct pipe_reference *src_ref;
247 struct pipe_reference *dst_ref;
248 struct vmw_svga_winsys_surface *dst;
249
250 if(pdst == NULL || *pdst == src)
251 return;
252
253 dst = *pdst;
254
255 src_ref = src ? &src->refcnt : NULL;
256 dst_ref = dst ? &dst->refcnt : NULL;
257
258 if (pipe_reference(dst_ref, src_ref)) {
259 if (dst->buf)
260 vmw_svga_winsys_buffer_destroy(&dst->screen->base, dst->buf);
261 vmw_ioctl_surface_destroy(dst->screen, dst->sid);
262 #ifdef DEBUG
263 /* to detect dangling pointers */
264 assert(p_atomic_read(&dst->validated) == 0);
265 dst->sid = SVGA3D_INVALID_ID;
266 #endif
267 mtx_destroy(&dst->mutex);
268 FREE(dst);
269 }
270
271 *pdst = src;
272 }