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#ifndef SVGA_BUFFER_H
27#define SVGA_BUFFER_H
28
29
30#include "pipe/p_compiler.h"
31#include "pipe/p_state.h"
32#include "util/u_transfer.h"
33
34#include "util/u_double_list.h"
35
36#include "svga_screen_cache.h"
37
38
39/**
40 * Maximum number of discontiguous ranges
41 */
42#define SVGA_BUFFER_MAX_RANGES 32
43
44
45struct svga_context;
46struct svga_winsys_buffer;
47struct svga_winsys_surface;
48
49
50extern struct u_resource_vtbl svga_buffer_vtbl;
51
52struct svga_buffer_range
53{
54   unsigned start;
55   unsigned end;
56};
57
58
59/**
60 * SVGA pipe buffer.
61 */
62struct svga_buffer
63{
64   struct u_resource b;
65
66   /**
67    * Regular (non DMA'able) memory.
68    *
69    * Used for user buffers or for buffers which we know before hand that can
70    * never be used by the virtual hardware directly, such as constant buffers.
71    */
72   void *swbuf;
73
74   /**
75    * Whether swbuf was created by the user or not.
76    */
77   boolean user;
78
79   /**
80    * Creation key for the host surface handle.
81    *
82    * This structure describes all the host surface characteristics so that it
83    * can be looked up in cache, since creating a host surface is often a slow
84    * operation.
85    */
86   struct svga_host_surface_cache_key key;
87
88   /**
89    * Host surface handle.
90    *
91    * This is a platform independent abstraction for host SID. We create when
92    * trying to bind.
93    *
94    * Only set for non-user buffers.
95    */
96   struct svga_winsys_surface *handle;
97
98   /**
99    * Information about ongoing and past map operations.
100    */
101   struct {
102      /**
103       * Number of concurrent mappings.
104       */
105      unsigned count;
106
107      /**
108       * Dirty ranges.
109       *
110       * Ranges that were touched by the application and need to be uploaded to
111       * the host.
112       *
113       * This information will be copied into dma.boxes, when emiting the
114       * SVGA3dCmdSurfaceDMA command.
115       */
116      struct svga_buffer_range ranges[SVGA_BUFFER_MAX_RANGES];
117      unsigned num_ranges;
118   } map;
119
120   /**
121    * Information about uploaded version of user buffers.
122    */
123   struct {
124      struct pipe_resource *buffer;
125
126      /**
127       * We combine multiple user buffers into the same hardware buffer. This
128       * is the relative offset within that buffer.
129       */
130      unsigned offset;
131
132      /**
133       * Range of user buffer that is uploaded in @buffer at @offset.
134       */
135      unsigned start;
136      unsigned end;
137   } uploaded;
138
139   /**
140    * DMA'ble memory.
141    *
142    * A piece of GMR memory, with the same size of the buffer. It is created
143    * when mapping the buffer, and will be used to upload vertex data to the
144    * host.
145    *
146    * Only set for non-user buffers.
147    */
148   struct svga_winsys_buffer *hwbuf;
149
150   /**
151    * Information about pending DMA uploads.
152    *
153    */
154   struct {
155      /**
156       * Whether this buffer has an unfinished DMA upload command.
157       *
158       * If not set then the rest of the information is null.
159       */
160      boolean pending;
161
162      SVGA3dSurfaceDMAFlags flags;
163
164      /**
165       * Pointer to the DMA copy box *inside* the command buffer.
166       */
167      SVGA3dCopyBox *boxes;
168
169      /**
170       * Context that has the pending DMA to this buffer.
171       */
172      struct svga_context *svga;
173   } dma;
174
175   /**
176    * Linked list head, used to gather all buffers with pending dma uploads on
177    * a context. It is only valid if the dma.pending is set above.
178    */
179   struct list_head head;
180};
181
182
183static INLINE struct svga_buffer *
184svga_buffer(struct pipe_resource *buffer)
185{
186   if (buffer) {
187      assert(((struct svga_buffer *)buffer)->b.vtbl == &svga_buffer_vtbl);
188      return (struct svga_buffer *)buffer;
189   }
190   return NULL;
191}
192
193
194/**
195 * Returns TRUE for user buffers.  We may
196 * decide to use an alternate upload path for these buffers.
197 */
198static INLINE boolean
199svga_buffer_is_user_buffer( struct pipe_resource *buffer )
200{
201   if (buffer) {
202      return svga_buffer(buffer)->user;
203   } else {
204      return FALSE;
205   }
206}
207
208
209
210
211struct pipe_resource *
212svga_user_buffer_create(struct pipe_screen *screen,
213                        void *ptr,
214                        unsigned bytes,
215			unsigned usage);
216
217struct pipe_resource *
218svga_buffer_create(struct pipe_screen *screen,
219		   const struct pipe_resource *template);
220
221
222
223/**
224 * Get the host surface handle for this buffer.
225 *
226 * This will ensure the host surface is updated, issuing DMAs as needed.
227 *
228 * NOTE: This may insert new commands in the context, so it *must* be called
229 * before reserving command buffer space. And, in order to insert commands
230 * it may need to call svga_context_flush().
231 */
232struct svga_winsys_surface *
233svga_buffer_handle(struct svga_context *svga,
234                   struct pipe_resource *buf);
235
236void
237svga_context_flush_buffers(struct svga_context *svga);
238
239struct svga_winsys_buffer *
240svga_winsys_buffer_create(struct svga_context *svga,
241                          unsigned alignment,
242                          unsigned usage,
243                          unsigned size);
244
245#endif /* SVGA_BUFFER_H */
246