summaryrefslogtreecommitdiff
path: root/src/gallium/winsys/svga/drm/vmw_surface.c
blob: cf648b4dd93b7c0cb6469cf56d63cf67eb738ee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**********************************************************
 * Copyright 2009 VMware, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 **********************************************************/


#include "svga_cmd.h"
#include "util/u_debug.h"
#include "util/u_memory.h"
#include "pipe/p_defines.h"
#include "vmw_surface.h"
#include "vmw_screen.h"
#include "vmw_buffer.h"
#include "vmw_context.h"
#include "pipebuffer/pb_bufmgr.h"


void *
vmw_svga_winsys_surface_map(struct svga_winsys_context *swc,
                            struct svga_winsys_surface *srf,
                            unsigned flags, boolean *retry)
{
   struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
   void *data = NULL;
   struct pb_buffer *pb_buf;
   uint32_t pb_flags;
   struct vmw_winsys_screen *vws = vsrf->screen;
   
   *retry = FALSE;
   assert((flags & (PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE)) != 0);
   pipe_mutex_lock(vsrf->mutex);

   if (vsrf->mapcount) {
      /*
       * Only allow multiple readers to map.
       */
      if ((flags & PIPE_TRANSFER_WRITE) ||
          (vsrf->map_mode & PIPE_TRANSFER_WRITE))
         goto out_unlock;
      
      data = vsrf->data;
      goto out_mapped;
   }

   vsrf->rebind = FALSE;

   /*
    * If we intend to read, there's no point discarding the
    * data if busy.
    */
   if (flags & PIPE_TRANSFER_READ || vsrf->shared)
      flags &= ~PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;

   /*
    * Discard is a hint to a synchronized map.
    */
   if (flags & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE)
      flags &= ~PIPE_TRANSFER_UNSYNCHRONIZED;

   /*
    * The surface is allowed to be referenced on the command stream iff
    * we're mapping unsynchronized or discard. This is an early check.
    * We need to recheck after a failing discard map.
    */
   if (!(flags & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
                  PIPE_TRANSFER_UNSYNCHRONIZED)) &&
       p_atomic_read(&vsrf->validated)) {
      *retry = TRUE;
      goto out_unlock;
   }

   pb_flags = flags & (PIPE_TRANSFER_READ_WRITE | PIPE_TRANSFER_UNSYNCHRONIZED);

   if (flags & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
      struct pb_manager *provider;
      struct pb_desc desc;

      /*
       * First, if possible, try to map existing storage with DONTBLOCK.
       */
      if (!p_atomic_read(&vsrf->validated)) {
         data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf,
                                           PIPE_TRANSFER_DONTBLOCK | pb_flags);
         if (data)
            goto out_mapped;
      } 

      /*
       * Attempt to get a new buffer.
       */
      provider = vws->pools.mob_fenced;
      memset(&desc, 0, sizeof(desc));
      desc.alignment = 4096;
      pb_buf = provider->create_buffer(provider, vsrf->size, &desc);
      if (pb_buf != NULL) {
         struct svga_winsys_buffer *vbuf =
            vmw_svga_winsys_buffer_wrap(pb_buf);

         data = vmw_svga_winsys_buffer_map(&vws->base, vbuf, pb_flags);
         if (data) {
            vsrf->rebind = TRUE;
            /*
             * We've discarded data on this surface and thus
             * it's data is no longer consider referenced.
             */
            vmw_swc_surface_clear_reference(swc, vsrf);
            if (vsrf->buf)
               vmw_svga_winsys_buffer_destroy(&vws->base, vsrf->buf);
            vsrf->buf = vbuf;
            goto out_mapped;
         } else
            vmw_svga_winsys_buffer_destroy(&vws->base, vbuf);
      }
      /*
       * We couldn't get and map a new buffer for some reason.
       * Fall through to an ordinary map.
       * But tell pipe driver to flush now if already on validate list,
       * Otherwise we'll overwrite previous contents.
       */
      if (!(flags & PIPE_TRANSFER_UNSYNCHRONIZED) && 
          p_atomic_read(&vsrf->validated)) {
         *retry = TRUE;
         goto out_unlock;
      }
   }

   pb_flags |= (flags & PIPE_TRANSFER_DONTBLOCK);
   data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf, pb_flags);
   if (data == NULL)
      goto out_unlock;

out_mapped:
   ++vsrf->mapcount;
   vsrf->data = data;
   vsrf->map_mode = flags & (PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE);
out_unlock:
   pipe_mutex_unlock(vsrf->mutex);
   return data;
}


void
vmw_svga_winsys_surface_unmap(struct svga_winsys_context *swc,
                              struct svga_winsys_surface *srf,
                              boolean *rebind)
{
   struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
   pipe_mutex_lock(vsrf->mutex);
   if (--vsrf->mapcount == 0) {
      *rebind = vsrf->rebind;
      vsrf->rebind = FALSE;
      vmw_svga_winsys_buffer_unmap(&vsrf->screen->base, vsrf->buf);
   }
   pipe_mutex_unlock(vsrf->mutex);
}

void
vmw_svga_winsys_surface_reference(struct vmw_svga_winsys_surface **pdst,
                                  struct vmw_svga_winsys_surface *src)
{
   struct pipe_reference *src_ref;
   struct pipe_reference *dst_ref;
   struct vmw_svga_winsys_surface *dst;

   if(pdst == NULL || *pdst == src)
      return;

   dst = *pdst;

   src_ref = src ? &src->refcnt : NULL;
   dst_ref = dst ? &dst->refcnt : NULL;

   if (pipe_reference(dst_ref, src_ref)) {
      if (dst->buf)
         vmw_svga_winsys_buffer_destroy(&dst->screen->base, dst->buf);
      vmw_ioctl_surface_destroy(dst->screen, dst->sid);
#ifdef DEBUG
      /* to detect dangling pointers */
      assert(p_atomic_read(&dst->validated) == 0);
      dst->sid = SVGA3D_INVALID_ID;
#endif
      pipe_mutex_destroy(dst->mutex);
      FREE(dst);
   }

   *pdst = src;
}