Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / svga / svga_resource_buffer.c
1 /**********************************************************
2  * Copyright 2008-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 #include "svga_cmd.h"
27
28 #include "pipe/p_state.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_inlines.h"
31 #include "os/os_thread.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34
35 #include "svga_context.h"
36 #include "svga_screen.h"
37 #include "svga_resource_buffer.h"
38 #include "svga_resource_buffer_upload.h"
39 #include "svga_winsys.h"
40 #include "svga_debug.h"
41
42
43 /**
44  * Vertex and index buffers need hardware backing.  Constant buffers
45  * do not.  No other types of buffers currently supported.
46  */
47 static INLINE boolean
48 svga_buffer_needs_hw_storage(unsigned usage)
49 {
50    return usage & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER);
51 }
52
53
54 /**
55  * Create a buffer transfer.
56  *
57  * Unlike texture DMAs (which are written immediately to the command buffer and
58  * therefore inherently serialized with other context operations), for buffers
59  * we try to coalesce multiple range mappings (i.e, multiple calls to this
60  * function) into a single DMA command, for better efficiency in command
61  * processing.  This means we need to exercise extra care here to ensure that
62  * the end result is exactly the same as if one DMA was used for every mapped
63  * range.
64  */
65 static struct pipe_transfer *
66 svga_buffer_get_transfer(struct pipe_context *pipe,
67                          struct pipe_resource *resource,
68                          unsigned level,
69                          unsigned usage,
70                          const struct pipe_box *box)
71 {
72    struct svga_context *svga = svga_context(pipe);
73    struct svga_screen *ss = svga_screen(pipe->screen);
74    struct svga_buffer *sbuf = svga_buffer(resource);
75    struct pipe_transfer *transfer;
76
77    transfer = CALLOC_STRUCT(pipe_transfer);
78    if (transfer == NULL) {
79       return NULL;
80    }
81
82    transfer->resource = resource;
83    transfer->level = level;
84    transfer->usage = usage;
85    transfer->box = *box;
86
87    if (usage & PIPE_TRANSFER_WRITE) {
88       if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
89          /*
90           * Finish writing any pending DMA commands, and tell the host to discard
91           * the buffer contents on the next DMA operation.
92           */
93
94          if (sbuf->dma.pending) {
95             svga_buffer_upload_flush(svga, sbuf);
96
97             /*
98              * Instead of flushing the context command buffer, simply discard
99              * the current hwbuf, and start a new one.
100              */
101
102             svga_buffer_destroy_hw_storage(ss, sbuf);
103          }
104
105          sbuf->map.num_ranges = 0;
106          sbuf->dma.flags.discard = TRUE;
107       }
108
109       if (usage & PIPE_TRANSFER_UNSYNCHRONIZED) {
110          if (!sbuf->map.num_ranges) {
111             /*
112              * No pending ranges to upload so far, so we can tell the host to
113              * not synchronize on the next DMA command.
114              */
115
116             sbuf->dma.flags.unsynchronized = TRUE;
117          }
118       } else {
119          /*
120           * Synchronizing, so finish writing any pending DMA command, and
121           * ensure the next DMA will be done in order.
122           */
123
124          if (sbuf->dma.pending) {
125             svga_buffer_upload_flush(svga, sbuf);
126
127             if (sbuf->hwbuf) {
128                /*
129                 * We have a pending DMA upload from a hardware buffer, therefore
130                 * we need to ensure that the host finishes processing that DMA
131                 * command before the state tracker can start overwriting the
132                 * hardware buffer.
133                 *
134                 * XXX: This could be avoided by tying the hardware buffer to
135                 * the transfer (just as done with textures), which would allow
136                 * overlapping DMAs commands to be queued on the same context
137                 * buffer. However, due to the likelihood of software vertex
138                 * processing, it is more convenient to hold on to the hardware
139                 * buffer, allowing to quickly access the contents from the CPU
140                 * without having to do a DMA download from the host.
141                 */
142
143                if (usage & PIPE_TRANSFER_DONTBLOCK) {
144                   /*
145                    * Flushing the command buffer here will most likely cause
146                    * the map of the hwbuf below to block, so preemptively
147                    * return NULL here if DONTBLOCK is set to prevent unnecessary
148                    * command buffer flushes.
149                    */
150
151                   FREE(transfer);
152                   return NULL;
153                }
154
155                svga_context_flush(svga, NULL);
156             }
157          }
158
159          sbuf->dma.flags.unsynchronized = FALSE;
160       }
161    }
162
163    if (!sbuf->swbuf && !sbuf->hwbuf) {
164       if (svga_buffer_create_hw_storage(ss, sbuf) != PIPE_OK) {
165          /*
166           * We can't create a hardware buffer big enough, so create a malloc
167           * buffer instead.
168           */
169          if (0) {
170             debug_printf("%s: failed to allocate %u KB of DMA, "
171                          "splitting DMA transfers\n",
172                          __FUNCTION__,
173                          (sbuf->b.b.width0 + 1023)/1024);
174          }
175
176          sbuf->swbuf = align_malloc(sbuf->b.b.width0, 16);
177          if (!sbuf->swbuf) {
178             FREE(transfer);
179             return NULL;
180          }
181       }
182    }
183
184    return transfer;
185 }
186
187
188 /**
189  * Map a range of a buffer.
190  */
191 static void *
192 svga_buffer_transfer_map( struct pipe_context *pipe,
193                           struct pipe_transfer *transfer )
194 {
195    struct svga_buffer *sbuf = svga_buffer(transfer->resource);
196
197    uint8_t *map;
198
199    if (sbuf->swbuf) {
200       /* User/malloc buffer */
201       map = sbuf->swbuf;
202    }
203    else if (sbuf->hwbuf) {
204       struct svga_screen *ss = svga_screen(pipe->screen);
205       struct svga_winsys_screen *sws = ss->sws;
206
207       map = sws->buffer_map(sws, sbuf->hwbuf, transfer->usage);
208    }
209    else {
210       map = NULL;
211    }
212
213    if (map) {
214       ++sbuf->map.count;
215       map += transfer->box.x;
216    }
217    
218    return map;
219 }
220
221
222 static void
223 svga_buffer_transfer_flush_region( struct pipe_context *pipe,
224                                    struct pipe_transfer *transfer,
225                                    const struct pipe_box *box)
226 {
227    struct svga_screen *ss = svga_screen(pipe->screen);
228    struct svga_buffer *sbuf = svga_buffer(transfer->resource);
229
230    unsigned offset = transfer->box.x + box->x;
231    unsigned length = box->width;
232
233    assert(transfer->usage & PIPE_TRANSFER_WRITE);
234    assert(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT);
235
236    pipe_mutex_lock(ss->swc_mutex);
237    svga_buffer_add_range(sbuf, offset, offset + length);
238    pipe_mutex_unlock(ss->swc_mutex);
239 }
240
241
242 static void
243 svga_buffer_transfer_unmap( struct pipe_context *pipe,
244                             struct pipe_transfer *transfer )
245 {
246    struct svga_screen *ss = svga_screen(pipe->screen);
247    struct svga_winsys_screen *sws = ss->sws;
248    struct svga_buffer *sbuf = svga_buffer(transfer->resource);
249    
250    pipe_mutex_lock(ss->swc_mutex);
251    
252    assert(sbuf->map.count);
253    if (sbuf->map.count) {
254       --sbuf->map.count;
255    }
256
257    if (sbuf->hwbuf) {
258       sws->buffer_unmap(sws, sbuf->hwbuf);
259    }
260
261    if (transfer->usage & PIPE_TRANSFER_WRITE) {
262       if (!(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
263          /*
264           * Mapped range not flushed explicitly, so flush the whole buffer,
265           * and tell the host to discard the contents when processing the DMA
266           * command.
267           */
268
269          SVGA_DBG(DEBUG_DMA, "flushing the whole buffer\n");
270    
271          sbuf->dma.flags.discard = TRUE;
272
273          svga_buffer_add_range(sbuf, 0, sbuf->b.b.width0);
274       }
275    }
276
277    pipe_mutex_unlock(ss->swc_mutex);
278 }
279
280
281 /**
282  * Destroy transfer
283  */
284 static void
285 svga_buffer_transfer_destroy(struct pipe_context *pipe,
286                              struct pipe_transfer *transfer)
287 {
288    FREE(transfer);
289 }
290
291
292 static void
293 svga_buffer_destroy( struct pipe_screen *screen,
294                      struct pipe_resource *buf )
295 {
296    struct svga_screen *ss = svga_screen(screen); 
297    struct svga_buffer *sbuf = svga_buffer( buf );
298
299    assert(!p_atomic_read(&buf->reference.count));
300    
301    assert(!sbuf->dma.pending);
302
303    if(sbuf->handle)
304       svga_buffer_destroy_host_surface(ss, sbuf);
305    
306    if(sbuf->uploaded.buffer)
307       pipe_resource_reference(&sbuf->uploaded.buffer, NULL);
308
309    if(sbuf->hwbuf)
310       svga_buffer_destroy_hw_storage(ss, sbuf);
311    
312    if(sbuf->swbuf && !sbuf->user)
313       align_free(sbuf->swbuf);
314    
315    FREE(sbuf);
316 }
317
318
319 struct u_resource_vtbl svga_buffer_vtbl = 
320 {
321    u_default_resource_get_handle,      /* get_handle */
322    svga_buffer_destroy,              /* resource_destroy */
323    svga_buffer_get_transfer,         /* get_transfer */
324    svga_buffer_transfer_destroy,     /* transfer_destroy */
325    svga_buffer_transfer_map,         /* transfer_map */
326    svga_buffer_transfer_flush_region,  /* transfer_flush_region */
327    svga_buffer_transfer_unmap,       /* transfer_unmap */
328    u_default_transfer_inline_write   /* transfer_inline_write */
329 };
330
331
332
333 struct pipe_resource *
334 svga_buffer_create(struct pipe_screen *screen,
335                    const struct pipe_resource *template)
336 {
337    struct svga_screen *ss = svga_screen(screen);
338    struct svga_buffer *sbuf;
339    
340    sbuf = CALLOC_STRUCT(svga_buffer);
341    if(!sbuf)
342       goto error1;
343    
344    sbuf->b.b = *template;
345    sbuf->b.vtbl = &svga_buffer_vtbl;
346    pipe_reference_init(&sbuf->b.b.reference, 1);
347    sbuf->b.b.screen = screen;
348
349    if(svga_buffer_needs_hw_storage(template->bind)) {
350       if(svga_buffer_create_host_surface(ss, sbuf) != PIPE_OK)
351          goto error2;
352    }
353    else {
354       sbuf->swbuf = align_malloc(template->width0, 64);
355       if(!sbuf->swbuf)
356          goto error2;
357    }
358       
359    debug_reference(&sbuf->b.b.reference,
360                    (debug_reference_descriptor)debug_describe_resource, 0);
361
362    return &sbuf->b.b; 
363
364 error2:
365    FREE(sbuf);
366 error1:
367    return NULL;
368 }
369
370 struct pipe_resource *
371 svga_user_buffer_create(struct pipe_screen *screen,
372                         void *ptr,
373                         unsigned bytes,
374                         unsigned bind)
375 {
376    struct svga_buffer *sbuf;
377    
378    sbuf = CALLOC_STRUCT(svga_buffer);
379    if(!sbuf)
380       goto no_sbuf;
381       
382    pipe_reference_init(&sbuf->b.b.reference, 1);
383    sbuf->b.vtbl = &svga_buffer_vtbl;
384    sbuf->b.b.screen = screen;
385    sbuf->b.b.format = PIPE_FORMAT_R8_UNORM; /* ?? */
386    sbuf->b.b.usage = PIPE_USAGE_IMMUTABLE;
387    sbuf->b.b.bind = bind;
388    sbuf->b.b.width0 = bytes;
389    sbuf->b.b.height0 = 1;
390    sbuf->b.b.depth0 = 1;
391    sbuf->b.b.array_size = 1;
392
393    sbuf->swbuf = ptr;
394    sbuf->user = TRUE;
395
396    debug_reference(&sbuf->b.b.reference,
397                    (debug_reference_descriptor)debug_describe_resource, 0);
398    
399    return &sbuf->b.b; 
400
401 no_sbuf:
402    return NULL;
403 }
404
405
406