1/**************************************************************************
2 *
3 * Copyright © 2009 Jakob Bornecrantz
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 **************************************************************************/
25
26#ifndef I915_WINSYS_H
27#define I915_WINSYS_H
28
29#include "pipe/p_compiler.h"
30
31struct i915_winsys;
32struct i915_winsys_buffer;
33struct i915_winsys_batchbuffer;
34struct pipe_resource;
35struct pipe_fence_handle;
36struct winsys_handle;
37
38enum i915_winsys_buffer_usage {
39   /* use on textures */
40   I915_USAGE_RENDER = 0x01,
41   I915_USAGE_SAMPLER = 0x02,
42   I915_USAGE_2D_TARGET = 0x04,
43   I915_USAGE_2D_SOURCE = 0x08,
44   /* use on vertex */
45   I915_USAGE_VERTEX = 0x10
46};
47
48enum i915_winsys_buffer_type {
49   I915_NEW_TEXTURE,
50   I915_NEW_SCANOUT, /**< a texture used for scanning out from */
51   I915_NEW_VERTEX
52};
53
54/* These need to be in sync with the definitions of libdrm-intel! */
55enum i915_winsys_buffer_tile { I915_TILE_NONE, I915_TILE_X, I915_TILE_Y };
56
57enum i915_winsys_flush_flags {
58   I915_FLUSH_ASYNC = 0,
59   I915_FLUSH_END_OF_FRAME = 1
60};
61
62struct i915_winsys_batchbuffer {
63
64   struct i915_winsys *iws;
65
66   /**
67    * Values exported to speed up the writing the batchbuffer,
68    * instead of having to go trough a accesor function for
69    * each dword written.
70    */
71   /*{@*/
72   uint8_t *map;
73   uint8_t *ptr;
74   size_t size;
75
76   size_t relocs;
77   /*@}*/
78};
79
80struct i915_winsys {
81
82   unsigned pci_id; /**< PCI ID for the device */
83
84   /**
85    * Batchbuffer functions.
86    */
87   /*@{*/
88   /**
89    * Create a new batchbuffer.
90    */
91   struct i915_winsys_batchbuffer *(*batchbuffer_create)(
92      struct i915_winsys *iws);
93
94   /**
95    * Validate buffers for usage in this batchbuffer.
96    * Does space-checking and asorted other book-keeping.
97    *
98    * @batch
99    * @buffers array to buffers to validate
100    * @num_of_buffers size of the passed array
101    */
102   bool (*validate_buffers)(struct i915_winsys_batchbuffer *batch,
103                            struct i915_winsys_buffer **buffers,
104                            int num_of_buffers);
105
106   /**
107    * Emit a relocation to a buffer.
108    * Target position in batchbuffer is the same as ptr.
109    *
110    * @batch
111    * @reloc buffer address to be inserted into target.
112    * @usage how is the hardware going to use the buffer.
113    * @offset add this to the reloc buffers address
114    * @target buffer where to write the address, null for batchbuffer.
115    * @fenced relocation needs a fence.
116    */
117   int (*batchbuffer_reloc)(struct i915_winsys_batchbuffer *batch,
118                            struct i915_winsys_buffer *reloc,
119                            enum i915_winsys_buffer_usage usage,
120                            unsigned offset, bool fenced);
121
122   /**
123    * Flush a bufferbatch.
124    */
125   void (*batchbuffer_flush)(struct i915_winsys_batchbuffer *batch,
126                             struct pipe_fence_handle **fence,
127                             enum i915_winsys_flush_flags flags);
128
129   /**
130    * Destroy a batchbuffer.
131    */
132   void (*batchbuffer_destroy)(struct i915_winsys_batchbuffer *batch);
133   /*@}*/
134
135   /**
136    * Buffer functions.
137    */
138   /*@{*/
139   /**
140    * Create a buffer.
141    */
142   struct i915_winsys_buffer *(*buffer_create)(
143      struct i915_winsys *iws, unsigned size,
144      enum i915_winsys_buffer_type type);
145
146   /**
147    * Create a tiled buffer.
148    *
149    * *stride, height are in bytes. The winsys tries to allocate the buffer with
150    * the tiling mode provide in *tiling. If tiling is no possible, *tiling will
151    * be set to I915_TILE_NONE. The calculated stride (incorporateing hw/kernel
152    * requirements) is always returned in *stride.
153    */
154   struct i915_winsys_buffer *(*buffer_create_tiled)(
155      struct i915_winsys *iws, unsigned *stride, unsigned height,
156      enum i915_winsys_buffer_tile *tiling, enum i915_winsys_buffer_type type);
157
158   /**
159    * Creates a buffer from a handle.
160    * Used to implement pipe_screen::resource_from_handle.
161    * Also provides the stride information needed for the
162    * texture via the stride argument.
163    */
164   struct i915_winsys_buffer *(*buffer_from_handle)(
165      struct i915_winsys *iws, struct winsys_handle *whandle, unsigned height,
166      enum i915_winsys_buffer_tile *tiling, unsigned *stride);
167
168   /**
169    * Used to implement pipe_screen::resource_get_handle.
170    * The winsys might need the stride information.
171    */
172   bool (*buffer_get_handle)(struct i915_winsys *iws,
173                             struct i915_winsys_buffer *buffer,
174                             struct winsys_handle *whandle, unsigned stride);
175
176   /**
177    * Map a buffer.
178    */
179   void *(*buffer_map)(struct i915_winsys *iws,
180                       struct i915_winsys_buffer *buffer, bool write);
181
182   /**
183    * Unmap a buffer.
184    */
185   void (*buffer_unmap)(struct i915_winsys *iws,
186                        struct i915_winsys_buffer *buffer);
187
188   /**
189    * Write to a buffer.
190    *
191    * Arguments follows pipe_buffer_write.
192    */
193   int (*buffer_write)(struct i915_winsys *iws, struct i915_winsys_buffer *dst,
194                       size_t offset, size_t size, const void *data);
195
196   void (*buffer_destroy)(struct i915_winsys *iws,
197                          struct i915_winsys_buffer *buffer);
198
199   /**
200    * Check if a buffer is busy.
201    */
202   bool (*buffer_is_busy)(struct i915_winsys *iws,
203                          struct i915_winsys_buffer *buffer);
204   /*@}*/
205
206   /**
207    * Fence functions.
208    */
209   /*@{*/
210   /**
211    * Reference fence and set ptr to fence.
212    */
213   void (*fence_reference)(struct i915_winsys *iws,
214                           struct pipe_fence_handle **ptr,
215                           struct pipe_fence_handle *fence);
216
217   /**
218    * Check if a fence has finished.
219    */
220   int (*fence_signalled)(struct i915_winsys *iws,
221                          struct pipe_fence_handle *fence);
222
223   /**
224    * Wait on a fence to finish.
225    */
226   int (*fence_finish)(struct i915_winsys *iws,
227                       struct pipe_fence_handle *fence);
228   /*@}*/
229
230   /**
231    * Retrieve the aperture size (in MiB) of the device.
232    */
233   int (*aperture_size)(struct i915_winsys *iws);
234
235   /**
236    * Destroy the winsys.
237    */
238   void (*destroy)(struct i915_winsys *iws);
239};
240
241#endif
242