summaryrefslogtreecommitdiff
path: root/stream-servers/virglrenderer.h
blob: a2d2ebf62fe004e0e01fde1316811cea70ae35df (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**************************************************************************
 *
 * Copyright (C) 2014 Red Hat Inc.
 *
 * 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.
 *
 **************************************************************************/

/* library interface from QEMU to virglrenderer */

#ifndef VIRGLRENDERER_H
#define VIRGLRENDERER_H

#include <stdint.h>
#include <stdbool.h>

struct virgl_box;
struct iovec;

#define VIRGL_EXPORT  __attribute__((visibility("default")))

typedef void *virgl_renderer_gl_context;

struct virgl_renderer_gl_ctx_param {
   int version;
   bool shared;
   int major_ver;
   int minor_ver;
};

struct virgl_renderer_callbacks {
   int version;
   void (*write_fence)(void *cookie, uint32_t fence);

   /* interact with GL implementation */
   virgl_renderer_gl_context (*create_gl_context)(void *cookie, int scanout_idx, struct virgl_renderer_gl_ctx_param *param);
   void (*destroy_gl_context)(void *cookie, virgl_renderer_gl_context ctx);
   int (*make_current)(void *cookie, int scanout_idx, virgl_renderer_gl_context ctx);
};

/* virtio-gpu compatible interface */
#define VIRGL_RENDERER_USE_EGL 1
/*
 * Wait for sync objects in thread rather than polling
 * need to use virgl_renderer_get_poll_fd to know if this feature is in effect.
 */
#define VIRGL_RENDERER_THREAD_SYNC 2
#define VIRGL_RENDERER_USE_GLX (1 << 2)

VIRGL_EXPORT int virgl_renderer_init(void *cookie, int flags, struct virgl_renderer_callbacks *cb);
VIRGL_EXPORT void virgl_renderer_poll(void); /* force fences */

/* we need to give qemu the cursor resource contents */
VIRGL_EXPORT void *virgl_renderer_get_cursor_data(uint32_t resource_id, uint32_t *width, uint32_t *height);

VIRGL_EXPORT void virgl_renderer_get_rect(int resource_id, struct iovec *iov, unsigned int num_iovs,
                                          uint32_t offset, int x, int y, int width, int height);

VIRGL_EXPORT int virgl_renderer_get_fd_for_texture(uint32_t tex_id, int *fd);

/* virgl bind flags - these are compatible with mesa 10.5 gallium.
   but are fixed, no other should be passed to virgl either. */
#define VIRGL_RES_BIND_DEPTH_STENCIL (1 << 0)
#define VIRGL_RES_BIND_RENDER_TARGET (1 << 1)
#define VIRGL_RES_BIND_SAMPLER_VIEW  (1 << 3)
#define VIRGL_RES_BIND_VERTEX_BUFFER (1 << 4)
#define VIRGL_RES_BIND_INDEX_BUFFER  (1 << 5)
#define VIRGL_RES_BIND_CONSTANT_BUFFER (1 << 6)
#define VIRGL_RES_BIND_STREAM_OUTPUT (1 << 11)
#define VIRGL_RES_BIND_CURSOR        (1 << 16)
#define VIRGL_RES_BIND_CUSTOM        (1 << 17)

struct virgl_renderer_resource_create_args {
   uint32_t handle;
   uint32_t target;
   uint32_t format;
   uint32_t bind;
   uint32_t width;
   uint32_t height;
   uint32_t depth;
   uint32_t array_size;
   uint32_t last_level;
   uint32_t nr_samples;
   uint32_t flags;
};

/* new API */

VIRGL_EXPORT int virgl_renderer_resource_create(struct virgl_renderer_resource_create_args *args, struct iovec *iov, uint32_t num_iovs);
VIRGL_EXPORT void virgl_renderer_resource_unref(uint32_t res_handle);

VIRGL_EXPORT int virgl_renderer_context_create(uint32_t handle, uint32_t nlen, const char *name);
VIRGL_EXPORT void virgl_renderer_context_destroy(uint32_t handle);

VIRGL_EXPORT int virgl_renderer_submit_cmd(void *buffer,
                                           int ctx_id,
                                           int ndw);

VIRGL_EXPORT int virgl_renderer_transfer_read_iov(uint32_t handle, uint32_t ctx_id,
                                                  uint32_t level, uint32_t stride,
                                                  uint32_t layer_stride,
                                                  struct virgl_box *box,
                                                  uint64_t offset, struct iovec *iov,
                                                  int iovec_cnt);

VIRGL_EXPORT int virgl_renderer_transfer_write_iov(uint32_t handle,
                                                   uint32_t ctx_id,
                                                   int level,
                                                   uint32_t stride,
                                                   uint32_t layer_stride,
                                                   struct virgl_box *box,
                                                   uint64_t offset,
                                                   struct iovec *iovec,
                                                   unsigned int iovec_cnt);

VIRGL_EXPORT void virgl_renderer_get_cap_set(uint32_t set, uint32_t *max_ver,
                                             uint32_t *max_size);

VIRGL_EXPORT void virgl_renderer_fill_caps(uint32_t set, uint32_t version,
                                           void *caps);

VIRGL_EXPORT int virgl_renderer_resource_attach_iov(int res_handle, struct iovec *iov,
                                                    int num_iovs);
VIRGL_EXPORT void virgl_renderer_resource_detach_iov(int res_handle, struct iovec **iov, int *num_iovs);

VIRGL_EXPORT int virgl_renderer_create_fence(int client_fence_id, uint32_t ctx_id);

VIRGL_EXPORT void virgl_renderer_force_ctx_0(void);

VIRGL_EXPORT void virgl_renderer_ctx_attach_resource(int ctx_id, int res_handle);
VIRGL_EXPORT void virgl_renderer_ctx_detach_resource(int ctx_id, int res_handle);

/* return information about a resource */

struct virgl_renderer_resource_info {
   uint32_t handle;
   uint32_t virgl_format;
   uint32_t width;
   uint32_t height;
   uint32_t depth;
   uint32_t flags;
   uint32_t tex_id;
   uint32_t stride;
   int drm_fourcc;
};

VIRGL_EXPORT int virgl_renderer_resource_get_info(int res_handle,
                                                  struct virgl_renderer_resource_info *info);

VIRGL_EXPORT int virgl_renderer_resource_create_v2(unsigned int res_handle, uint64_t hvaId);
VIRGL_EXPORT int virgl_renderer_resource_map(unsigned int res_handle, void** hvaOut, uint64_t* sizeOut);
VIRGL_EXPORT int virgl_renderer_resource_unmap(unsigned int res_handle);

VIRGL_EXPORT void virgl_renderer_cleanup(void *cookie);

/* reset the rendererer - destroy all contexts and resource */
VIRGL_EXPORT void virgl_renderer_reset(void);

VIRGL_EXPORT int virgl_renderer_get_poll_fd(void);

/* Struct of virglrenderer API calls in order to be able to switch between
 * different virgl API impls, or different devices altogether that use
 * the virglrenderer API (e.g., goldfish pipe) */

typedef int (*virgl_renderer_init_t)(
    void *cookie, int flags, struct virgl_renderer_callbacks *cb);
typedef void (*virgl_renderer_poll_t)(void); /* force fences */
typedef void* (*virgl_renderer_get_cursor_data_t)(
    uint32_t resource_id, uint32_t *width, uint32_t *height);
typedef int (*virgl_renderer_resource_create_t)(
    struct virgl_renderer_resource_create_args *args,
    struct iovec *iov, uint32_t num_iovs);
typedef void (*virgl_renderer_resource_unref_t)(uint32_t res_handle);
typedef int (*virgl_renderer_context_create_t)(
    uint32_t handle, uint32_t nlen, const char *name);
typedef void (*virgl_renderer_context_destroy_t)(uint32_t handle);
typedef int (*virgl_renderer_submit_cmd_t)(void *buffer,
                                           int ctx_id,
                                           int ndw);
typedef int (*virgl_renderer_transfer_read_iov_t)(
    uint32_t handle, uint32_t ctx_id,
    uint32_t level, uint32_t stride,
    uint32_t layer_stride,
    struct virgl_box *box,
    uint64_t offset, struct iovec *iov,
    int iovec_cnt);
typedef int (*virgl_renderer_transfer_write_iov_t)(
    uint32_t handle,
    uint32_t ctx_id,
    int level,
    uint32_t stride,
    uint32_t layer_stride,
    struct virgl_box *box,
    uint64_t offset,
    struct iovec *iovec,
    unsigned int iovec_cnt);
typedef void (*virgl_renderer_get_cap_set_t)(uint32_t set, uint32_t *max_ver,
                                             uint32_t *max_size);

typedef void (*virgl_renderer_fill_caps_t)(uint32_t set, uint32_t version,
                                           void *caps);

typedef int (*virgl_renderer_resource_attach_iov_t)(
    int res_handle, struct iovec *iov,
    int num_iovs);
typedef void (*virgl_renderer_resource_detach_iov_t)(
    int res_handle, struct iovec **iov, int *num_iovs);

typedef int (*virgl_renderer_create_fence_t)(
    int client_fence_id, uint32_t ctx_id);

typedef void (*virgl_renderer_force_ctx_0_t)(void);

typedef void (*virgl_renderer_ctx_attach_resource_t)(
    int ctx_id, int res_handle);
typedef void (*virgl_renderer_ctx_detach_resource_t)(
    int ctx_id, int res_handle);
typedef int (*virgl_renderer_resource_get_info_t)(
    int res_handle,
    struct virgl_renderer_resource_info *info);
typedef int (*virgl_renderer_resource_create_v2_t)(unsigned int res_handle, uint64_t hvaId);
typedef int (*virgl_renderer_resource_map_t)(unsigned int res_handle, void** hvaOut, uint64_t* sizeOut);
typedef int (*virgl_renderer_resource_unmap_t)(unsigned int res_handle);

#define LIST_VIRGLRENDERER_API(f) \
f(virgl_renderer_init) \
f(virgl_renderer_poll) \
f(virgl_renderer_get_cursor_data) \
f(virgl_renderer_resource_create) \
f(virgl_renderer_resource_unref) \
f(virgl_renderer_context_create) \
f(virgl_renderer_context_destroy) \
f(virgl_renderer_submit_cmd) \
f(virgl_renderer_transfer_read_iov) \
f(virgl_renderer_transfer_write_iov) \
f(virgl_renderer_get_cap_set) \
f(virgl_renderer_fill_caps) \
f(virgl_renderer_resource_attach_iov) \
f(virgl_renderer_resource_detach_iov) \
f(virgl_renderer_create_fence) \
f(virgl_renderer_force_ctx_0) \
f(virgl_renderer_ctx_attach_resource) \
f(virgl_renderer_ctx_detach_resource) \
f(virgl_renderer_resource_get_info) \
f(virgl_renderer_resource_create_v2) \
f(virgl_renderer_resource_map) \
f(virgl_renderer_resource_unmap) \

#define VIRGLRENDERER_API_DEFINE_STRUCT_FIELD(api) \
    api##_t api;

struct virgl_renderer_virtio_interface {
    LIST_VIRGLRENDERER_API(VIRGLRENDERER_API_DEFINE_STRUCT_FIELD)
};

struct virgl_renderer_virtio_interface* get_default_virtio_interface();

#endif