Merge branch 'gallium-noconstbuf'
[mesa.git] / src / gallium / drivers / svga / svga_screen_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 "pipe/p_inlines.h"
31 #include "pipe/p_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_screen_buffer.h"
38 #include "svga_winsys.h"
39 #include "svga_debug.h"
40
41
42 /**
43 * Vertex and index buffers have to be treated slightly differently from
44 * regular guest memory regions because the SVGA device sees them as
45 * surfaces, and the state tracker can create/destroy without the pipe
46 * driver, therefore we must do the uploads from the vws.
47 */
48 static INLINE boolean
49 svga_buffer_needs_hw_storage(unsigned usage)
50 {
51 return usage & (PIPE_BUFFER_USAGE_VERTEX | PIPE_BUFFER_USAGE_INDEX);
52 }
53
54
55 static INLINE enum pipe_error
56 svga_buffer_create_host_surface(struct svga_screen *ss,
57 struct svga_buffer *sbuf)
58 {
59 if(!sbuf->handle) {
60 sbuf->key.flags = 0;
61
62 sbuf->key.format = SVGA3D_BUFFER;
63 if(sbuf->base.usage & PIPE_BUFFER_USAGE_VERTEX)
64 sbuf->key.flags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
65 if(sbuf->base.usage & PIPE_BUFFER_USAGE_INDEX)
66 sbuf->key.flags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
67
68 sbuf->key.size.width = sbuf->base.size;
69 sbuf->key.size.height = 1;
70 sbuf->key.size.depth = 1;
71
72 sbuf->key.numFaces = 1;
73 sbuf->key.numMipLevels = 1;
74 sbuf->key.cachable = 1;
75
76 SVGA_DBG(DEBUG_DMA, "surface_create for buffer sz %d\n", sbuf->base.size);
77
78 sbuf->handle = svga_screen_surface_create(ss, &sbuf->key);
79 if(!sbuf->handle)
80 return PIPE_ERROR_OUT_OF_MEMORY;
81
82 /* Always set the discard flag on the first time the buffer is written
83 * as svga_screen_surface_create might have passed a recycled host
84 * buffer.
85 */
86 sbuf->hw.flags.discard = TRUE;
87
88 SVGA_DBG(DEBUG_DMA, " --> got sid %p sz %d (buffer)\n", sbuf->handle, sbuf->base.size);
89 }
90
91 return PIPE_OK;
92 }
93
94
95 static INLINE void
96 svga_buffer_destroy_host_surface(struct svga_screen *ss,
97 struct svga_buffer *sbuf)
98 {
99 if(sbuf->handle) {
100 SVGA_DBG(DEBUG_DMA, " ungrab sid %p sz %d\n", sbuf->handle, sbuf->base.size);
101 svga_screen_surface_destroy(ss, &sbuf->key, &sbuf->handle);
102 }
103 }
104
105
106 static INLINE void
107 svga_buffer_destroy_hw_storage(struct svga_screen *ss, struct svga_buffer *sbuf)
108 {
109 struct svga_winsys_screen *sws = ss->sws;
110
111 assert(!sbuf->map.count);
112 assert(sbuf->hw.buf);
113 if(sbuf->hw.buf) {
114 sws->buffer_destroy(sws, sbuf->hw.buf);
115 sbuf->hw.buf = NULL;
116 assert(sbuf->head.prev && sbuf->head.next);
117 LIST_DEL(&sbuf->head);
118 #ifdef DEBUG
119 sbuf->head.next = sbuf->head.prev = NULL;
120 #endif
121 }
122 }
123
124 static INLINE enum pipe_error
125 svga_buffer_backup(struct svga_screen *ss, struct svga_buffer *sbuf)
126 {
127 if (sbuf->hw.buf && sbuf->hw.num_ranges) {
128 void *src;
129
130 if (!sbuf->swbuf)
131 sbuf->swbuf = align_malloc(sbuf->base.size, sbuf->base.alignment);
132 if (!sbuf->swbuf)
133 return PIPE_ERROR_OUT_OF_MEMORY;
134
135 src = ss->sws->buffer_map(ss->sws, sbuf->hw.buf,
136 PIPE_BUFFER_USAGE_CPU_READ);
137 if (!src)
138 return PIPE_ERROR;
139
140 memcpy(sbuf->swbuf, src, sbuf->base.size);
141 ss->sws->buffer_unmap(ss->sws, sbuf->hw.buf);
142 }
143
144 return PIPE_OK;
145 }
146
147 /**
148 * Try to make GMR space available by freeing the hardware storage of
149 * unmapped
150 */
151 boolean
152 svga_buffer_free_cached_hw_storage(struct svga_screen *ss)
153 {
154 struct list_head *curr;
155 struct svga_buffer *sbuf;
156 enum pipe_error ret = PIPE_OK;
157
158 curr = ss->cached_buffers.prev;
159
160 /* free the least recently used buffer's hw storage which is not mapped */
161 do {
162 if(curr == &ss->cached_buffers)
163 return FALSE;
164
165 sbuf = LIST_ENTRY(struct svga_buffer, curr, head);
166
167 curr = curr->prev;
168 if (sbuf->map.count == 0)
169 ret = svga_buffer_backup(ss, sbuf);
170
171 } while(sbuf->map.count != 0 || ret != PIPE_OK);
172
173 svga_buffer_destroy_hw_storage(ss, sbuf);
174
175 return TRUE;
176 }
177
178 struct svga_winsys_buffer *
179 svga_winsys_buffer_create( struct svga_screen *ss,
180 unsigned alignment,
181 unsigned usage,
182 unsigned size )
183 {
184 struct svga_winsys_screen *sws = ss->sws;
185 struct svga_winsys_buffer *buf;
186
187 /* Just try */
188 buf = sws->buffer_create(sws, alignment, usage, size);
189 if(!buf) {
190
191 SVGA_DBG(DEBUG_DMA|DEBUG_PERF, "flushing screen to find %d bytes GMR\n",
192 size);
193
194 /* Try flushing all pending DMAs */
195 svga_screen_flush(ss, NULL);
196 buf = sws->buffer_create(sws, alignment, usage, size);
197
198 SVGA_DBG(DEBUG_DMA|DEBUG_PERF, "evicting buffers to find %d bytes GMR\n",
199 size);
200
201 /* Try evicing all buffer storage */
202 while(!buf && svga_buffer_free_cached_hw_storage(ss))
203 buf = sws->buffer_create(sws, alignment, usage, size);
204 }
205
206 return buf;
207 }
208
209
210 /**
211 * Allocate DMA'ble storage for the buffer.
212 *
213 * Called before mapping a buffer.
214 */
215 static INLINE enum pipe_error
216 svga_buffer_create_hw_storage(struct svga_screen *ss,
217 struct svga_buffer *sbuf)
218 {
219 if(!sbuf->hw.buf) {
220 unsigned alignment = sbuf->base.alignment;
221 unsigned usage = 0;
222 unsigned size = sbuf->base.size;
223
224 sbuf->hw.buf = svga_winsys_buffer_create(ss, alignment, usage, size);
225 if(!sbuf->hw.buf)
226 return PIPE_ERROR_OUT_OF_MEMORY;
227
228 assert(!sbuf->needs_flush);
229 assert(!sbuf->head.prev && !sbuf->head.next);
230 LIST_ADD(&sbuf->head, &ss->cached_buffers);
231 }
232
233 return PIPE_OK;
234 }
235
236
237 /**
238 * Variant of SVGA3D_BufferDMA which leaves the copy box temporarily in blank.
239 */
240 static enum pipe_error
241 svga_buffer_upload_command(struct svga_context *svga,
242 struct svga_buffer *sbuf)
243 {
244 struct svga_winsys_context *swc = svga->swc;
245 struct svga_winsys_buffer *guest = sbuf->hw.buf;
246 struct svga_winsys_surface *host = sbuf->handle;
247 SVGA3dTransferType transfer = SVGA3D_WRITE_HOST_VRAM;
248 SVGA3dSurfaceDMAFlags flags = sbuf->hw.flags;
249 SVGA3dCmdSurfaceDMA *cmd;
250 uint32 numBoxes = sbuf->hw.num_ranges;
251 SVGA3dCopyBox *boxes;
252 SVGA3dCmdSurfaceDMASuffix *pSuffix;
253 unsigned region_flags;
254 unsigned surface_flags;
255 struct pipe_buffer *dummy;
256
257 if(transfer == SVGA3D_WRITE_HOST_VRAM) {
258 region_flags = PIPE_BUFFER_USAGE_GPU_READ;
259 surface_flags = PIPE_BUFFER_USAGE_GPU_WRITE;
260 }
261 else if(transfer == SVGA3D_READ_HOST_VRAM) {
262 region_flags = PIPE_BUFFER_USAGE_GPU_WRITE;
263 surface_flags = PIPE_BUFFER_USAGE_GPU_READ;
264 }
265 else {
266 assert(0);
267 return PIPE_ERROR_BAD_INPUT;
268 }
269
270 assert(numBoxes);
271
272 cmd = SVGA3D_FIFOReserve(swc,
273 SVGA_3D_CMD_SURFACE_DMA,
274 sizeof *cmd + numBoxes * sizeof *boxes + sizeof *pSuffix,
275 2);
276 if(!cmd)
277 return PIPE_ERROR_OUT_OF_MEMORY;
278
279 swc->region_relocation(swc, &cmd->guest.ptr, guest, 0, region_flags);
280 cmd->guest.pitch = 0;
281
282 swc->surface_relocation(swc, &cmd->host.sid, host, surface_flags);
283 cmd->host.face = 0;
284 cmd->host.mipmap = 0;
285
286 cmd->transfer = transfer;
287
288 sbuf->hw.boxes = (SVGA3dCopyBox *)&cmd[1];
289 sbuf->hw.svga = svga;
290
291 /* Increment reference count */
292 dummy = NULL;
293 pipe_buffer_reference(&dummy, &sbuf->base);
294
295 pSuffix = (SVGA3dCmdSurfaceDMASuffix *)((uint8_t*)cmd + sizeof *cmd + numBoxes * sizeof *boxes);
296 pSuffix->suffixSize = sizeof *pSuffix;
297 pSuffix->maximumOffset = sbuf->base.size;
298 pSuffix->flags = flags;
299
300 swc->commit(swc);
301
302 return PIPE_OK;
303 }
304
305
306 /**
307 * Patch up the upload DMA command reserved by svga_buffer_upload_command
308 * with the final ranges.
309 */
310 static void
311 svga_buffer_upload_flush(struct svga_context *svga,
312 struct svga_buffer *sbuf)
313 {
314 struct svga_screen *ss = svga_screen(svga->pipe.screen);
315 SVGA3dCopyBox *boxes;
316 unsigned i;
317
318 assert(sbuf->handle);
319 assert(sbuf->hw.buf);
320 assert(sbuf->hw.num_ranges);
321 assert(sbuf->hw.svga == svga);
322 assert(sbuf->hw.boxes);
323
324 /*
325 * Patch the DMA command with the final copy box.
326 */
327
328 SVGA_DBG(DEBUG_DMA, "dma to sid %p\n", sbuf->handle);
329
330 boxes = sbuf->hw.boxes;
331 for(i = 0; i < sbuf->hw.num_ranges; ++i) {
332 SVGA_DBG(DEBUG_DMA, " bytes %u - %u\n",
333 sbuf->hw.ranges[i].start, sbuf->hw.ranges[i].end);
334
335 boxes[i].x = sbuf->hw.ranges[i].start;
336 boxes[i].y = 0;
337 boxes[i].z = 0;
338 boxes[i].w = sbuf->hw.ranges[i].end - sbuf->hw.ranges[i].start;
339 boxes[i].h = 1;
340 boxes[i].d = 1;
341 boxes[i].srcx = sbuf->hw.ranges[i].start;
342 boxes[i].srcy = 0;
343 boxes[i].srcz = 0;
344 }
345
346 sbuf->hw.num_ranges = 0;
347 memset(&sbuf->hw.flags, 0, sizeof sbuf->hw.flags);
348
349 assert(sbuf->head.prev && sbuf->head.next);
350 LIST_DEL(&sbuf->head);
351 sbuf->needs_flush = FALSE;
352 /* XXX: do we care about cached_buffers any more ?*/
353 LIST_ADD(&sbuf->head, &ss->cached_buffers);
354
355 sbuf->hw.svga = NULL;
356 sbuf->hw.boxes = NULL;
357
358 /* Decrement reference count */
359 pipe_reference(&(sbuf->base.reference), NULL);
360 sbuf = NULL;
361 }
362
363
364 /**
365 * Queue a DMA upload of a range of this buffer to the host.
366 *
367 * This function only notes the range down. It doesn't actually emit a DMA
368 * upload command. That only happens when a context tries to refer to this
369 * buffer, and the DMA upload command is added to that context's command buffer.
370 *
371 * We try to lump as many contiguous DMA transfers together as possible.
372 */
373 static void
374 svga_buffer_upload_queue(struct svga_buffer *sbuf,
375 unsigned start,
376 unsigned end)
377 {
378 unsigned i;
379
380 assert(sbuf->hw.buf);
381 assert(end > start);
382
383 /*
384 * Try to grow one of the ranges.
385 *
386 * Note that it is not this function task to care about overlapping ranges,
387 * as the GMR was already given so it is too late to do anything. Situations
388 * where overlapping ranges may pose a problem should be detected via
389 * pipe_context::is_buffer_referenced and the context that refers to the
390 * buffer should be flushed.
391 */
392
393 for(i = 0; i < sbuf->hw.num_ranges; ++i) {
394 if(start <= sbuf->hw.ranges[i].end && sbuf->hw.ranges[i].start <= end) {
395 sbuf->hw.ranges[i].start = MIN2(sbuf->hw.ranges[i].start, start);
396 sbuf->hw.ranges[i].end = MAX2(sbuf->hw.ranges[i].end, end);
397 return;
398 }
399 }
400
401 /*
402 * We cannot add a new range to an existing DMA command, so patch-up the
403 * pending DMA upload and start clean.
404 */
405
406 if(sbuf->needs_flush)
407 svga_buffer_upload_flush(sbuf->hw.svga, sbuf);
408
409 assert(!sbuf->needs_flush);
410 assert(!sbuf->hw.svga);
411 assert(!sbuf->hw.boxes);
412
413 /*
414 * Add a new range.
415 */
416
417 sbuf->hw.ranges[sbuf->hw.num_ranges].start = start;
418 sbuf->hw.ranges[sbuf->hw.num_ranges].end = end;
419 ++sbuf->hw.num_ranges;
420 }
421
422
423 static void *
424 svga_buffer_map_range( struct pipe_screen *screen,
425 struct pipe_buffer *buf,
426 unsigned offset, unsigned length,
427 unsigned usage )
428 {
429 struct svga_screen *ss = svga_screen(screen);
430 struct svga_winsys_screen *sws = ss->sws;
431 struct svga_buffer *sbuf = svga_buffer( buf );
432 void *map;
433
434 if(sbuf->swbuf) {
435 /* User/malloc buffer */
436 map = sbuf->swbuf;
437 }
438 else {
439 if(!sbuf->hw.buf) {
440 struct svga_winsys_surface *handle = sbuf->handle;
441
442 if(svga_buffer_create_hw_storage(ss, sbuf) != PIPE_OK)
443 return NULL;
444
445 /* Populate the hardware storage if the host surface pre-existed */
446 if((usage & PIPE_BUFFER_USAGE_CPU_READ) && handle) {
447 SVGA3dSurfaceDMAFlags flags;
448 enum pipe_error ret;
449 struct pipe_fence_handle *fence = NULL;
450
451 SVGA_DBG(DEBUG_DMA|DEBUG_PERF, "dma from sid %p (buffer), bytes %u - %u\n",
452 sbuf->handle, 0, sbuf->base.size);
453
454 memset(&flags, 0, sizeof flags);
455
456 ret = SVGA3D_BufferDMA(ss->swc,
457 sbuf->hw.buf,
458 sbuf->handle,
459 SVGA3D_READ_HOST_VRAM,
460 sbuf->base.size,
461 0,
462 flags);
463 if(ret != PIPE_OK) {
464 ss->swc->flush(ss->swc, NULL);
465
466 ret = SVGA3D_BufferDMA(ss->swc,
467 sbuf->hw.buf,
468 sbuf->handle,
469 SVGA3D_READ_HOST_VRAM,
470 sbuf->base.size,
471 0,
472 flags);
473 assert(ret == PIPE_OK);
474 }
475
476 ss->swc->flush(ss->swc, &fence);
477 sws->fence_finish(sws, fence, 0);
478 sws->fence_reference(sws, &fence, NULL);
479 }
480 }
481 else {
482 if((usage & PIPE_BUFFER_USAGE_CPU_READ) && !sbuf->needs_flush) {
483 /* We already had the hardware storage but we would have to issue
484 * a download if we hadn't, so move the buffer to the begginning
485 * of the LRU list.
486 */
487 assert(sbuf->head.prev && sbuf->head.next);
488 LIST_DEL(&sbuf->head);
489 LIST_ADD(&sbuf->head, &ss->cached_buffers);
490 }
491 }
492
493 map = sws->buffer_map(sws, sbuf->hw.buf, usage);
494 }
495
496 if(map) {
497 pipe_mutex_lock(ss->swc_mutex);
498
499 ++sbuf->map.count;
500
501 if (usage & PIPE_BUFFER_USAGE_CPU_WRITE) {
502 assert(sbuf->map.count <= 1);
503 sbuf->map.writing = TRUE;
504 if (usage & PIPE_BUFFER_USAGE_FLUSH_EXPLICIT)
505 sbuf->map.flush_explicit = TRUE;
506 }
507
508 pipe_mutex_unlock(ss->swc_mutex);
509 }
510
511 return map;
512 }
513
514 static void
515 svga_buffer_flush_mapped_range( struct pipe_screen *screen,
516 struct pipe_buffer *buf,
517 unsigned offset, unsigned length)
518 {
519 struct svga_buffer *sbuf = svga_buffer( buf );
520 struct svga_screen *ss = svga_screen(screen);
521
522 pipe_mutex_lock(ss->swc_mutex);
523 assert(sbuf->map.writing);
524 if(sbuf->map.writing) {
525 assert(sbuf->map.flush_explicit);
526 if(sbuf->hw.buf)
527 svga_buffer_upload_queue(sbuf, offset, offset + length);
528 }
529 pipe_mutex_unlock(ss->swc_mutex);
530 }
531
532 static void
533 svga_buffer_unmap( struct pipe_screen *screen,
534 struct pipe_buffer *buf)
535 {
536 struct svga_screen *ss = svga_screen(screen);
537 struct svga_winsys_screen *sws = ss->sws;
538 struct svga_buffer *sbuf = svga_buffer( buf );
539
540 pipe_mutex_lock(ss->swc_mutex);
541
542 assert(sbuf->map.count);
543 if(sbuf->map.count)
544 --sbuf->map.count;
545
546 if(sbuf->hw.buf)
547 sws->buffer_unmap(sws, sbuf->hw.buf);
548
549 if(sbuf->map.writing) {
550 if(!sbuf->map.flush_explicit) {
551 /* No mapped range was flushed -- flush the whole buffer */
552 SVGA_DBG(DEBUG_DMA, "flushing the whole buffer\n");
553
554 if(sbuf->hw.buf)
555 svga_buffer_upload_queue(sbuf, 0, sbuf->base.size);
556 }
557
558 sbuf->map.writing = FALSE;
559 sbuf->map.flush_explicit = FALSE;
560 }
561
562 pipe_mutex_unlock(ss->swc_mutex);
563 }
564
565 static void
566 svga_buffer_destroy( struct pipe_buffer *buf )
567 {
568 struct svga_screen *ss = svga_screen(buf->screen);
569 struct svga_buffer *sbuf = svga_buffer( buf );
570
571 assert(!p_atomic_read(&buf->reference.count));
572
573 assert(!sbuf->needs_flush);
574
575 if(sbuf->handle) {
576 SVGA_DBG(DEBUG_DMA, "release sid %p sz %d\n", sbuf->handle, sbuf->base.size);
577 svga_screen_surface_destroy(ss, &sbuf->key, &sbuf->handle);
578 }
579
580 if(sbuf->hw.buf)
581 svga_buffer_destroy_hw_storage(ss, sbuf);
582
583 if(sbuf->swbuf && !sbuf->user)
584 align_free(sbuf->swbuf);
585
586 FREE(sbuf);
587 }
588
589 static struct pipe_buffer *
590 svga_buffer_create(struct pipe_screen *screen,
591 unsigned alignment,
592 unsigned usage,
593 unsigned size)
594 {
595 struct svga_screen *ss = svga_screen(screen);
596 struct svga_buffer *sbuf;
597
598 sbuf = CALLOC_STRUCT(svga_buffer);
599 if(!sbuf)
600 goto error1;
601
602 sbuf->magic = SVGA_BUFFER_MAGIC;
603
604 pipe_reference_init(&sbuf->base.reference, 1);
605 sbuf->base.screen = screen;
606 sbuf->base.alignment = alignment;
607 sbuf->base.usage = usage;
608 sbuf->base.size = size;
609
610 if(svga_buffer_needs_hw_storage(usage)) {
611 if(svga_buffer_create_host_surface(ss, sbuf) != PIPE_OK)
612 goto error2;
613 }
614 else {
615 if(alignment < sizeof(void*))
616 alignment = sizeof(void*);
617
618 usage |= PIPE_BUFFER_USAGE_CPU_READ_WRITE;
619
620 sbuf->swbuf = align_malloc(size, alignment);
621 if(!sbuf->swbuf)
622 goto error2;
623 }
624
625 return &sbuf->base;
626
627 error2:
628 FREE(sbuf);
629 error1:
630 return NULL;
631 }
632
633 static struct pipe_buffer *
634 svga_user_buffer_create(struct pipe_screen *screen,
635 void *ptr,
636 unsigned bytes)
637 {
638 struct svga_buffer *sbuf;
639
640 sbuf = CALLOC_STRUCT(svga_buffer);
641 if(!sbuf)
642 goto no_sbuf;
643
644 sbuf->magic = SVGA_BUFFER_MAGIC;
645
646 sbuf->swbuf = ptr;
647 sbuf->user = TRUE;
648
649 pipe_reference_init(&sbuf->base.reference, 1);
650 sbuf->base.screen = screen;
651 sbuf->base.alignment = 1;
652 sbuf->base.usage = 0;
653 sbuf->base.size = bytes;
654
655 return &sbuf->base;
656
657 no_sbuf:
658 return NULL;
659 }
660
661
662 void
663 svga_screen_init_buffer_functions(struct pipe_screen *screen)
664 {
665 screen->buffer_create = svga_buffer_create;
666 screen->user_buffer_create = svga_user_buffer_create;
667 screen->buffer_map_range = svga_buffer_map_range;
668 screen->buffer_flush_mapped_range = svga_buffer_flush_mapped_range;
669 screen->buffer_unmap = svga_buffer_unmap;
670 screen->buffer_destroy = svga_buffer_destroy;
671 }
672
673
674 /**
675 * Copy the contents of the user buffer / malloc buffer to a hardware buffer.
676 */
677 static INLINE enum pipe_error
678 svga_buffer_update_hw(struct svga_screen *ss, struct svga_buffer *sbuf)
679 {
680 if(!sbuf->hw.buf) {
681 enum pipe_error ret;
682 void *map;
683
684 assert(sbuf->swbuf);
685 if(!sbuf->swbuf)
686 return PIPE_ERROR;
687
688 ret = svga_buffer_create_hw_storage(ss, sbuf);
689 assert(ret == PIPE_OK);
690 if(ret != PIPE_OK)
691 return ret;
692
693 pipe_mutex_lock(ss->swc_mutex);
694 map = ss->sws->buffer_map(ss->sws, sbuf->hw.buf, PIPE_BUFFER_USAGE_CPU_WRITE);
695 assert(map);
696 if(!map) {
697 pipe_mutex_unlock(ss->swc_mutex);
698 return PIPE_ERROR_OUT_OF_MEMORY;
699 }
700
701 memcpy(map, sbuf->swbuf, sbuf->base.size);
702 ss->sws->buffer_unmap(ss->sws, sbuf->hw.buf);
703
704 /* This user/malloc buffer is now indistinguishable from a gpu buffer */
705 assert(!sbuf->map.count);
706 if(!sbuf->map.count) {
707 if(sbuf->user)
708 sbuf->user = FALSE;
709 else
710 align_free(sbuf->swbuf);
711 sbuf->swbuf = NULL;
712 }
713
714 svga_buffer_upload_queue(sbuf, 0, sbuf->base.size);
715 }
716
717 pipe_mutex_unlock(ss->swc_mutex);
718 return PIPE_OK;
719 }
720
721
722 struct svga_winsys_surface *
723 svga_buffer_handle(struct svga_context *svga,
724 struct pipe_buffer *buf)
725 {
726 struct pipe_screen *screen = svga->pipe.screen;
727 struct svga_screen *ss = svga_screen(screen);
728 struct svga_buffer *sbuf;
729 enum pipe_error ret;
730
731 if(!buf)
732 return NULL;
733
734 sbuf = svga_buffer(buf);
735
736 assert(!sbuf->map.count);
737
738 if(!sbuf->handle) {
739 ret = svga_buffer_create_host_surface(ss, sbuf);
740 if(ret != PIPE_OK)
741 return NULL;
742
743 ret = svga_buffer_update_hw(ss, sbuf);
744 if(ret != PIPE_OK)
745 return NULL;
746 }
747
748 if(!sbuf->needs_flush && sbuf->hw.num_ranges) {
749 /* Queue the buffer for flushing */
750 ret = svga_buffer_upload_command(svga, sbuf);
751 if(ret != PIPE_OK)
752 /* XXX: Should probably have a richer return value */
753 return NULL;
754
755 assert(sbuf->hw.svga == svga);
756
757 sbuf->needs_flush = TRUE;
758 assert(sbuf->head.prev && sbuf->head.next);
759 LIST_DEL(&sbuf->head);
760 LIST_ADDTAIL(&sbuf->head, &svga->dirty_buffers);
761 }
762
763 return sbuf->handle;
764 }
765
766 struct pipe_buffer *
767 svga_screen_buffer_wrap_surface(struct pipe_screen *screen,
768 enum SVGA3dSurfaceFormat format,
769 struct svga_winsys_surface *srf)
770 {
771 struct pipe_buffer *buf;
772 struct svga_buffer *sbuf;
773 struct svga_winsys_screen *sws = svga_winsys_screen(screen);
774
775 buf = svga_buffer_create(screen, 0, SVGA_BUFFER_USAGE_WRAPPED, 0);
776 if (!buf)
777 return NULL;
778
779 sbuf = svga_buffer(buf);
780
781 /*
782 * We are not the creator of this surface and therefore we must not
783 * cache it for reuse. Set the cacheable flag to zero in the key to
784 * prevent this.
785 */
786 sbuf->key.format = format;
787 sbuf->key.cachable = 0;
788 sws->surface_reference(sws, &sbuf->handle, srf);
789
790 return buf;
791 }
792
793
794 struct svga_winsys_surface *
795 svga_screen_buffer_get_winsys_surface(struct pipe_buffer *buffer)
796 {
797 struct svga_winsys_screen *sws = svga_winsys_screen(buffer->screen);
798 struct svga_winsys_surface *vsurf = NULL;
799
800 assert(svga_buffer(buffer)->key.cachable == 0);
801 svga_buffer(buffer)->key.cachable = 0;
802 sws->surface_reference(sws, &vsurf, svga_buffer(buffer)->handle);
803 return vsurf;
804 }
805
806 void
807 svga_context_flush_buffers(struct svga_context *svga)
808 {
809 struct list_head *curr, *next;
810 struct svga_buffer *sbuf;
811
812 curr = svga->dirty_buffers.next;
813 next = curr->next;
814 while(curr != &svga->dirty_buffers) {
815 sbuf = LIST_ENTRY(struct svga_buffer, curr, head);
816
817 assert(p_atomic_read(&sbuf->base.reference.count) != 0);
818 assert(sbuf->needs_flush);
819
820 svga_buffer_upload_flush(svga, sbuf);
821
822 curr = next;
823 next = curr->next;
824 }
825 }