1 /************************************************************************** 2 * 3 * Copyright (C) 2020 Chromium. 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 VIRGL_CONTEXT_H 26 #define VIRGL_CONTEXT_H 27 28 #include <stdbool.h> 29 #include <stddef.h> 30 #include <stdint.h> 31 32 #include "virglrenderer_hw.h" 33 #include "virgl_resource.h" 34 35 struct vrend_transfer_info; 36 struct pipe_resource; 37 38 struct virgl_context_blob { 39 /* valid fd or pipe resource */ 40 enum virgl_resource_fd_type type; 41 union { 42 int fd; 43 struct pipe_resource *pipe_resource; 44 } u; 45 46 uint32_t map_info; 47 48 void *renderer_data; 49 }; 50 51 struct virgl_context; 52 53 typedef void (*virgl_context_fence_retire)(struct virgl_context *ctx, 54 uint64_t queue_id, 55 void *fence_cookie); 56 57 /** 58 * Base class for renderer contexts. For example, vrend_decode_ctx is a 59 * subclass of virgl_context. 60 */ 61 struct virgl_context { 62 uint32_t ctx_id; 63 64 enum virgl_renderer_capset capset_id; 65 66 /* 67 * Each fence goes through submitted, signaled, and retired. This callback 68 * is called from virgl_context::retire_fences to retire signaled fences of 69 * each queue. When a queue has multiple signaled fences by the time 70 * virgl_context::retire_fences is called, this callback might not be called 71 * on all fences but only on the latest one, depending on the flags of the 72 * fences. 73 */ 74 virgl_context_fence_retire fence_retire; 75 76 void (*destroy)(struct virgl_context *ctx); 77 78 void (*attach_resource)(struct virgl_context *ctx, 79 struct virgl_resource *res); 80 void (*detach_resource)(struct virgl_context *ctx, 81 struct virgl_resource *res); 82 83 int (*transfer_3d)(struct virgl_context *ctx, 84 struct virgl_resource *res, 85 const struct vrend_transfer_info *info, 86 int transfer_mode); 87 88 /* These are used to create a virgl_resource from a context object. 89 * 90 * get_blob returns a virgl_context_blob from which a virgl_resource can be 91 * created. get_blob_done is optional and allows the context to associate 92 * the newly created resource with the context object. 93 * 94 * Note that get_blob is a one-time thing. The context object might be 95 * destroyed or reject subsequent get_blob calls. 96 */ 97 int (*get_blob)(struct virgl_context *ctx, 98 uint64_t blob_id, 99 uint32_t blob_flags, 100 struct virgl_context_blob *blob); 101 void (*get_blob_done)(struct virgl_context *ctx, 102 uint32_t res_id, 103 struct virgl_context_blob *blob); 104 105 int (*submit_cmd)(struct virgl_context *ctx, 106 const void *buffer, 107 size_t size); 108 109 /* 110 * Return an fd that is readable whenever there is any signaled fence in 111 * any queue, or -1 if not supported. 112 */ 113 int (*get_fencing_fd)(struct virgl_context *ctx); 114 115 /* retire signaled fences of all queues */ 116 void (*retire_fences)(struct virgl_context *ctx); 117 118 /* submit a fence to the queue identified by queue_id */ 119 int (*submit_fence)(struct virgl_context *ctx, 120 uint32_t flags, 121 uint64_t queue_id, 122 void *fence_cookie); 123 }; 124 125 struct virgl_context_foreach_args { 126 bool (*callback)(struct virgl_context *ctx, void *data); 127 void *data; 128 }; 129 130 int 131 virgl_context_table_init(void); 132 133 void 134 virgl_context_table_cleanup(void); 135 136 void 137 virgl_context_table_reset(void); 138 139 int 140 virgl_context_add(struct virgl_context *ctx); 141 142 void 143 virgl_context_remove(uint32_t ctx_id); 144 145 struct virgl_context * 146 virgl_context_lookup(uint32_t ctx_id); 147 148 void 149 virgl_context_foreach(const struct virgl_context_foreach_args *args); 150 151 #endif /* VIRGL_CONTEXT_H */ 152