1 /**************************************************************************
2  *
3  * Copyright (C) 2014 Red Hat Inc.
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 shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  **************************************************************************/
24 
25 #ifndef VREND_RENDERER_H
26 #define VREND_RENDERER_H
27 
28 #include "pipe/p_state.h"
29 #include "util/u_inlines.h"
30 #include "virgl_protocol.h"
31 #include "vrend_debug.h"
32 #include "vrend_tweaks.h"
33 #include "vrend_iov.h"
34 #include "virgl_gbm.h"
35 #include "virgl_hw.h"
36 #include <epoxy/gl.h>
37 
38 typedef void *virgl_gl_context;
39 typedef void *virgl_gl_drawable;
40 
41 struct virgl_gl_ctx_param {
42    int major_ver;
43    int minor_ver;
44    bool shared;
45 };
46 
47 struct vrend_context;
48 
49 /* Number of mipmap levels for which to keep the backing iov offsets.
50  * Value mirrored from mesa/virgl
51  */
52 #define VR_MAX_TEXTURE_2D_LEVELS 15
53 
54 #define VREND_STORAGE_GUEST_MEMORY       BIT(0)
55 #define VREND_STORAGE_GL_TEXTURE         BIT(1)
56 #define VREND_STORAGE_GL_BUFFER          BIT(2)
57 #define VREND_STORAGE_EGL_IMAGE          BIT(3)
58 #define VREND_STORAGE_GBM_BUFFER         BIT(4)
59 #define VREND_STORAGE_HOST_SYSTEM_MEMORY BIT(5)
60 #define VREND_STORAGE_GL_IMMUTABLE       BIT(6)
61 
62 enum {
63    CONTEXT_NONE,
64    CONTEXT_EGL,
65    CONTEXT_GLX
66 };
67 
68 extern int use_context;
69 #ifdef HAVE_EPOXY_EGL_H
70 extern struct virgl_egl *egl;
71 extern struct virgl_gbm *gbm;
72 #endif
73 
74 
75 struct vrend_resource {
76    struct pipe_resource base;
77    uint32_t storage_bits;
78 
79    GLuint id;
80    GLenum target;
81 
82    /* fb id if we need to readback this resource */
83    GLuint readback_fb_id;
84    GLuint readback_fb_level;
85    GLuint readback_fb_z;
86 
87    GLuint tbo_tex_id;/* tbos have two ids to track */
88    bool y_0_top;
89 
90    GLuint handle;
91 
92    void *priv;
93    /* Pointer to system memory storage for this resource. Only valid for
94     * VREND_RESOURCE_STORAGE_GUEST_ELSE_SYSTEM buffer storage.
95     */
96    char *ptr;
97    /* IOV pointing to shared guest memory storage for this resource. */
98    struct iovec *iov;
99    uint32_t num_iovs;
100    uint64_t mipmap_offsets[VR_MAX_TEXTURE_2D_LEVELS];
101    void *gbm_bo, *egl_image;
102    void *aux_plane_egl_image[VIRGL_GBM_MAX_PLANES - 1];
103 };
104 
105 #define VIRGL_TEXTURE_NEED_SWIZZLE        (1 << 0)
106 #define VIRGL_TEXTURE_CAN_TEXTURE_STORAGE (1 << 1)
107 #define VIRGL_TEXTURE_CAN_READBACK        (1 << 2)
108 
109 struct vrend_format_table {
110    enum virgl_formats format;
111    GLenum internalformat;
112    GLenum glformat;
113    GLenum gltype;
114    uint8_t swizzle[4];
115    uint32_t bindings;
116    uint32_t flags;
117 };
118 
119 struct vrend_if_cbs {
120    void (*write_fence)(unsigned fence_id);
121 
122    virgl_gl_context (*create_gl_context)(int scanout, struct virgl_gl_ctx_param *params);
123    void (*destroy_gl_context)(virgl_gl_context ctx);
124    int (*make_current)(virgl_gl_context ctx);
125 };
126 
127 #define VREND_USE_THREAD_SYNC 1
128 
129 int vrend_renderer_init(struct vrend_if_cbs *cbs, uint32_t flags);
130 
131 void vrend_insert_format(struct vrend_format_table *entry, uint32_t bindings, uint32_t flags);
132 bool vrend_check_framebuffer_mixed_color_attachements(void);
133 
134 void vrend_insert_format_swizzle(int override_format, struct vrend_format_table *entry,
135                                  uint32_t bindings, uint8_t swizzle[4], uint32_t flags);
136 const struct vrend_format_table *vrend_get_format_table_entry(enum virgl_formats format);
137 const struct vrend_format_table *vrend_get_format_table_entry_with_emulation(uint32_t bind, enum virgl_formats format);
138 
139 int vrend_create_shader(struct vrend_context *ctx,
140                         uint32_t handle,
141                         const struct pipe_stream_output_info *stream_output,
142                         uint32_t req_local_mem,
143                         const char *shd_text, uint32_t offlen, uint32_t num_tokens,
144                         uint32_t type, uint32_t pkt_length);
145 
146 void vrend_bind_shader(struct vrend_context *ctx,
147                        uint32_t type,
148                        uint32_t handle);
149 
150 void vrend_bind_vs_so(struct vrend_context *ctx,
151                       uint32_t handle);
152 void vrend_clear(struct vrend_context *ctx,
153                  unsigned buffers,
154                  const union pipe_color_union *color,
155                  double depth, unsigned stencil);
156 
157 int vrend_draw_vbo(struct vrend_context *ctx,
158                    const struct pipe_draw_info *info,
159                    uint32_t cso, uint32_t indirect_handle, uint32_t indirect_draw_count_handle);
160 
161 void vrend_set_framebuffer_state(struct vrend_context *ctx,
162                                  uint32_t nr_cbufs, uint32_t surf_handle[PIPE_MAX_COLOR_BUFS],
163                                  uint32_t zsurf_handle);
164 
165 struct vrend_context *vrend_create_context(int id, uint32_t nlen, const char *debug_name);
166 bool vrend_destroy_context(struct vrend_context *ctx);
167 int vrend_renderer_context_create(uint32_t handle, uint32_t nlen, const char *name);
168 void vrend_renderer_context_create_internal(uint32_t handle, uint32_t nlen, const char *name);
169 void vrend_renderer_context_destroy(uint32_t handle);
170 
171 struct vrend_renderer_resource_create_args {
172    uint32_t handle;
173    enum pipe_texture_target target;
174    uint32_t format;
175    uint32_t bind;
176    uint32_t width;
177    uint32_t height;
178    uint32_t depth;
179    uint32_t array_size;
180    uint32_t last_level;
181    uint32_t nr_samples;
182    uint32_t flags;
183 };
184 
185 int vrend_renderer_resource_create(struct vrend_renderer_resource_create_args *args, struct iovec *iov, uint32_t num_iovs, void *image_eos);
186 
187 void vrend_renderer_resource_unref(uint32_t handle);
188 void vrend_renderer_resource_set_priv(uint32_t res_handle, void *priv);
189 void *vrend_renderer_resource_get_priv(uint32_t res_handle);
190 
191 int vrend_create_surface(struct vrend_context *ctx,
192                          uint32_t handle,
193                          uint32_t res_handle, uint32_t format,
194                          uint32_t val0, uint32_t val1);
195 int vrend_create_sampler_view(struct vrend_context *ctx,
196                               uint32_t handle,
197                               uint32_t res_handle, uint32_t format,
198                               uint32_t val0, uint32_t val1, uint32_t swizzle_packed);
199 
200 int vrend_create_sampler_state(struct vrend_context *ctx,
201                                uint32_t handle,
202                                struct pipe_sampler_state *templ);
203 
204 int vrend_create_so_target(struct vrend_context *ctx,
205                            uint32_t handle,
206                            uint32_t res_handle,
207                            uint32_t buffer_offset,
208                            uint32_t buffer_size);
209 
210 void vrend_set_streamout_targets(struct vrend_context *ctx,
211                                  uint32_t append_bitmask,
212                                  uint32_t num_targets,
213                                  uint32_t *handles);
214 
215 int vrend_create_vertex_elements_state(struct vrend_context *ctx,
216                                        uint32_t handle,
217                                        unsigned num_elements,
218                                        const struct pipe_vertex_element *elements);
219 void vrend_bind_vertex_elements_state(struct vrend_context *ctx,
220                                       uint32_t handle);
221 
222 void vrend_set_single_vbo(struct vrend_context *ctx,
223                           uint32_t index,
224                           uint32_t stride,
225                           uint32_t buffer_offset,
226                           uint32_t res_handle);
227 void vrend_set_num_vbo(struct vrend_context *ctx,
228                        int num_vbo);
229 
230 int vrend_transfer_inline_write(struct vrend_context *ctx,
231                                 struct vrend_transfer_info *info);
232 
233 int vrend_renderer_copy_transfer3d(struct vrend_context *ctx,
234                                    struct vrend_transfer_info *info,
235                                    uint32_t src_handle);
236 
237 void vrend_set_viewport_states(struct vrend_context *ctx,
238                                uint32_t start_slot, uint32_t num_viewports,
239                                const struct pipe_viewport_state *state);
240 void vrend_set_num_sampler_views(struct vrend_context *ctx,
241                                  uint32_t shader_type,
242                                  uint32_t start_slot,
243                                  uint32_t num_sampler_views);
244 void vrend_set_single_sampler_view(struct vrend_context *ctx,
245                                    uint32_t shader_type,
246                                    uint32_t index,
247                                    uint32_t res_handle);
248 
249 void vrend_object_bind_blend(struct vrend_context *ctx,
250                              uint32_t handle);
251 void vrend_object_bind_dsa(struct vrend_context *ctx,
252                            uint32_t handle);
253 void vrend_object_bind_rasterizer(struct vrend_context *ctx,
254                                   uint32_t handle);
255 
256 void vrend_bind_sampler_states(struct vrend_context *ctx,
257                                uint32_t shader_type,
258                                uint32_t start_slot,
259                                uint32_t num_states,
260                                uint32_t *handles);
261 void vrend_set_index_buffer(struct vrend_context *ctx,
262                             uint32_t res_handle,
263                             uint32_t index_size,
264                             uint32_t offset);
265 void vrend_set_single_image_view(struct vrend_context *ctx,
266                                  uint32_t shader_type,
267                                  uint32_t index,
268                                  uint32_t format, uint32_t access,
269                                  uint32_t layer_offset, uint32_t level_size,
270                                  uint32_t handle);
271 void vrend_set_single_ssbo(struct vrend_context *ctx,
272                            uint32_t shader_type,
273                            uint32_t index,
274                            uint32_t offset, uint32_t length,
275                            uint32_t handle);
276 void vrend_set_single_abo(struct vrend_context *ctx,
277                           uint32_t index,
278                           uint32_t offset, uint32_t length,
279                           uint32_t handle);
280 void vrend_memory_barrier(struct vrend_context *ctx,
281                           unsigned flags);
282 void vrend_launch_grid(struct vrend_context *ctx,
283                        uint32_t *block,
284                        uint32_t *grid,
285                        uint32_t indirect_handle,
286                        uint32_t indirect_offset);
287 void vrend_set_framebuffer_state_no_attach(struct vrend_context *ctx,
288                                            uint32_t width, uint32_t height,
289                                            uint32_t layers, uint32_t samples);
290 void vrend_texture_barrier(struct vrend_context *ctx,
291                            unsigned flags);
292 int vrend_renderer_transfer_iov(const struct vrend_transfer_info *info, int transfer_mode);
293 
294 void vrend_renderer_resource_copy_region(struct vrend_context *ctx,
295                                          uint32_t dst_handle, uint32_t dst_level,
296                                          uint32_t dstx, uint32_t dsty, uint32_t dstz,
297                                          uint32_t src_handle, uint32_t src_level,
298                                          const struct pipe_box *src_box);
299 
300 void vrend_renderer_blit(struct vrend_context *ctx,
301                          uint32_t dst_handle, uint32_t src_handle,
302                          const struct pipe_blit_info *info);
303 
304 void vrend_set_stencil_ref(struct vrend_context *ctx, struct pipe_stencil_ref *ref);
305 void vrend_set_blend_color(struct vrend_context *ctx, struct pipe_blend_color *color);
306 void vrend_set_scissor_state(struct vrend_context *ctx,
307                              uint32_t start_slot,
308                              uint32_t num_scissor,
309                              struct pipe_scissor_state *ss);
310 
311 void vrend_set_polygon_stipple(struct vrend_context *ctx, struct pipe_poly_stipple *ps);
312 
313 void vrend_set_clip_state(struct vrend_context *ctx, struct pipe_clip_state *ucp);
314 void vrend_set_sample_mask(struct vrend_context *ctx, unsigned sample_mask);
315 void vrend_set_min_samples(struct vrend_context *ctx, unsigned min_samples);
316 
317 void vrend_set_constants(struct vrend_context *ctx,
318                          uint32_t shader,
319                          uint32_t index,
320                          uint32_t num_constant,
321                          float *data);
322 
323 void vrend_set_uniform_buffer(struct vrend_context *ctx, uint32_t shader,
324                               uint32_t index, uint32_t offset, uint32_t length,
325                               uint32_t res_handle);
326 
327 void vrend_fb_bind_texture_id(struct vrend_resource *res,
328                               int id,
329                               int idx,
330                               uint32_t level, uint32_t layer);
331 
332 void vrend_set_tess_state(struct vrend_context *ctx, const float tess_factors[6]);
333 
334 void vrend_renderer_fini(void);
335 
336 int vrend_decode_block(uint32_t ctx_id, uint32_t *block, int ndw);
337 struct vrend_context *vrend_lookup_renderer_ctx(uint32_t ctx_id);
338 
339 int vrend_renderer_create_fence(int client_fence_id, uint32_t ctx_id);
340 
341 void vrend_renderer_check_fences(void);
342 
343 bool vrend_hw_switch_context(struct vrend_context *ctx, bool now);
344 uint32_t vrend_renderer_object_insert(struct vrend_context *ctx, void *data,
345                                       uint32_t size, uint32_t handle, enum virgl_object_type type);
346 void vrend_renderer_object_destroy(struct vrend_context *ctx, uint32_t handle);
347 
348 int vrend_create_query(struct vrend_context *ctx, uint32_t handle,
349                        uint32_t query_type, uint32_t query_index,
350                        uint32_t res_handle, uint32_t offset);
351 
352 int vrend_begin_query(struct vrend_context *ctx, uint32_t handle);
353 int vrend_end_query(struct vrend_context *ctx, uint32_t handle);
354 void vrend_get_query_result(struct vrend_context *ctx, uint32_t handle,
355                             uint32_t wait);
356 void vrend_get_query_result_qbo(struct vrend_context *ctx, uint32_t handle,
357                                 uint32_t qbo_handle,
358                                 uint32_t wait, uint32_t result_type, uint32_t offset,
359                                 int32_t index);
360 void vrend_render_condition(struct vrend_context *ctx,
361                             uint32_t handle,
362                             bool condtion,
363                             uint mode);
364 void *vrend_renderer_get_cursor_contents(uint32_t res_handle, uint32_t *width, uint32_t *height);
365 
366 void vrend_renderer_fill_caps(uint32_t set, uint32_t version,
367                               union virgl_caps *caps);
368 
369 GLint64 vrend_renderer_get_timestamp(void);
370 
371 void vrend_build_format_list_common(void);
372 void vrend_build_format_list_gl(void);
373 void vrend_build_format_list_gles(void);
374 void vrend_build_emulated_format_list_gles(void);
375 void vrend_check_texture_storage(struct vrend_format_table *table);
376 
377 int vrend_renderer_resource_attach_iov(int res_handle, struct iovec *iov,
378                                        int num_iovs);
379 void vrend_renderer_resource_detach_iov(int res_handle,
380                                         struct iovec **iov_p,
381                                         int *num_iovs_p);
382 void vrend_renderer_resource_destroy(struct vrend_resource *res);
383 
384 static inline void
vrend_resource_reference(struct vrend_resource ** ptr,struct vrend_resource * tex)385 vrend_resource_reference(struct vrend_resource **ptr, struct vrend_resource *tex)
386 {
387    struct vrend_resource *old_tex = *ptr;
388 
389    if (pipe_reference(&(*ptr)->base.reference, &tex->base.reference))
390       vrend_renderer_resource_destroy(old_tex);
391    *ptr = tex;
392 }
393 
394 void vrend_renderer_force_ctx_0(void);
395 
396 void vrend_renderer_get_rect(int resource_id, struct iovec *iov, unsigned int num_iovs,
397                              uint32_t offset, int x, int y, int width, int height);
398 void vrend_renderer_attach_res_ctx(int ctx_id, int resource_id);
399 void vrend_renderer_detach_res_ctx(int ctx_id, int resource_id);
400 
401 struct vrend_context_tweaks *vrend_get_context_tweaks(struct vrend_context *ctx);
402 
403 struct vrend_renderer_resource_info {
404    uint32_t handle;
405    uint32_t format;
406    uint32_t width;
407    uint32_t height;
408    uint32_t depth;
409    uint32_t flags;
410    uint32_t tex_id;
411    uint32_t stride;
412 };
413 
414 int vrend_renderer_resource_get_info(int res_handle,
415                                      struct vrend_renderer_resource_info *info);
416 
417 #define VREND_CAP_SET 1
418 #define VREND_CAP_SET2 2
419 
420 void vrend_renderer_get_cap_set(uint32_t cap_set, uint32_t *max_ver,
421                                 uint32_t *max_size);
422 
423 void vrend_renderer_create_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
424 void vrend_renderer_destroy_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
425 void vrend_renderer_set_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
426 void vrend_report_buffer_error(struct vrend_context *ctx, int cmd);
427 
428 void vrend_fb_bind_texture(struct vrend_resource *res,
429                            int idx,
430                            uint32_t level, uint32_t layer);
431 bool vrend_format_is_emulated_alpha(enum virgl_formats format);
432 boolean format_is_copy_compatible(enum virgl_formats src, enum virgl_formats dst,
433                                   boolean allow_compressed);
434 
435 /* blitter interface */
436 void vrend_renderer_blit_gl(struct vrend_context *ctx,
437                             struct vrend_resource *src_res,
438                             struct vrend_resource *dst_res,
439                             GLenum blit_views[2],
440                             const struct pipe_blit_info *info,
441                             bool has_texture_srgb_decode,
442                             bool has_srgb_write_control,
443                             bool skip_dest_swizzle);
444 void vrend_blitter_fini(void);
445 
446 void vrend_renderer_reset(void);
447 int vrend_renderer_get_poll_fd(void);
448 void vrend_decode_reset(bool ctx_0_only);
449 
450 unsigned vrend_context_has_debug_flag(struct vrend_context *ctx,
451                                       enum virgl_debug_flags flag);
452 
453 unsigned vrend_renderer_query_multisample_caps(unsigned max_samples,
454                                                struct virgl_caps_v2 *caps);
455 
456 int virgl_has_gl_colorspace(void);
457 
458 struct gl_version {
459    uint32_t major;
460    uint32_t minor;
461 };
462 
463 static const struct gl_version gl_versions[] = { {4,5}, {4,4}, {4,3}, {4,2}, {4,1}, {4,0},
464                                                  {3,3}, {3,2}, {3,1}, {3,0} };
465 
466 extern struct vrend_if_cbs *vrend_clicbs;
467 
468 int vrend_renderer_execute(void *execute_args, uint32_t execute_size);
469 
470 void vrend_sync_make_current(virgl_gl_context);
471 
472 #endif
473