[6d5e378] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Petr Koupy
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup graph
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #ifndef GRAPH_GRAPH_H_
|
---|
| 37 | #define GRAPH_GRAPH_H_
|
---|
| 38 |
|
---|
| 39 | #include <sys/types.h>
|
---|
| 40 | #include <bool.h>
|
---|
| 41 | #include <loc.h>
|
---|
| 42 | #include <async.h>
|
---|
| 43 | #include <atomic.h>
|
---|
| 44 | #include <fibril_synch.h>
|
---|
| 45 | #include <adt/list.h>
|
---|
| 46 | #include <io/mode.h>
|
---|
| 47 | #include <io/pixelmap.h>
|
---|
| 48 | #include <ipc/graph.h>
|
---|
| 49 |
|
---|
| 50 | struct visualizer;
|
---|
| 51 | struct renderer;
|
---|
| 52 |
|
---|
| 53 | typedef struct {
|
---|
| 54 | /**
|
---|
| 55 | * Device driver shall allocate any necessary internal structures
|
---|
| 56 | * specific for a claimed visualizer. */
|
---|
| 57 | int (* claim)(struct visualizer *vs);
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Device driver shall deallocate any necessary internal structures
|
---|
| 61 | * specific for a claimed visualizer. Driver shall also check whether
|
---|
| 62 | * the mode is set and if so it shall change its internal state
|
---|
| 63 | * accordingly (e.g. deallocate frame buffers). */
|
---|
| 64 | int (* yield)(struct visualizer *vs);
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * Device driver shall first try to claim all resources required for
|
---|
| 68 | * a new mode (e.g. allocate new framebuffers) and only if successful
|
---|
| 69 | * it shall free resources for the old mode. Although such behaviour
|
---|
| 70 | * might not be always possible, it is preferable since libgraph tries to
|
---|
| 71 | * keep current mode functional if the new mode cannot be set (for any
|
---|
| 72 | * reason). If it is convenient for the device driver (e.g. for better
|
---|
| 73 | * optimization), the pointer to the handle_damage operation can be
|
---|
| 74 | * changed at this point. */
|
---|
| 75 | int (* change_mode)(struct visualizer *vs, vslmode_t new_mode);
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Device driver shall render the cells from damaged region into its
|
---|
| 79 | * internal framebuffer. In case the driver use multi-buffering, it
|
---|
| 80 | * shall also switch internal buffers (e.g. by pageflip). Offsets
|
---|
| 81 | * are intended to support basic vertical and horizontal scrolling on
|
---|
| 82 | * the shared backbuffer (i.e. when reading from backbuffer, the offsets
|
---|
| 83 | * shall be added to the coordinates and if necessary the result shall be
|
---|
| 84 | * wrapped around the edge of the backbuffer). */
|
---|
| 85 | int (* handle_damage)(struct visualizer *vs,
|
---|
| 86 | sysarg_t x, sysarg_t y, sysarg_t width, sysarg_t height,
|
---|
| 87 | sysarg_t x_offset, sysarg_t y_offset);
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * Upper layers of the graphic stack might report inactivity. In such
|
---|
| 91 | * case, device driver might enable power saving mode on the device
|
---|
| 92 | * corresponding to the visualizer. */
|
---|
| 93 | int (* suspend)(struct visualizer *vs);
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * When upper layers detect activity on suspended visualizer, device
|
---|
| 97 | * driver shall disable power saving mode on the corresponding device. */
|
---|
| 98 | int (* wakeup)(struct visualizer *vs);
|
---|
| 99 | } visualizer_ops_t;
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * Represents final output device (e.g. monitor connected to the port
|
---|
| 103 | * on the graphic adapter, serial console, local/remote virtual monitor).
|
---|
| 104 | */
|
---|
| 105 | typedef struct visualizer {
|
---|
| 106 | /**
|
---|
| 107 | * Link to the list of all visualizers belonging to the graphic device.
|
---|
| 108 | * Field is fully managed by libgraph. */
|
---|
| 109 | link_t link;
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * When reference count equals 1, visualizer is claimed by a client,
|
---|
| 113 | * when equals 0, visualizer is not claimed. At the time, visualizer
|
---|
| 114 | * can be claimed only by a single client.
|
---|
| 115 | * Field is fully managed by libgraph. */
|
---|
| 116 | atomic_t ref_cnt;
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * Visualizer ID assigned by some particular registration service
|
---|
| 120 | * in the system (either location service or device manager). Intended
|
---|
| 121 | * for cleanup duties (e.g. unregistering visualizer).
|
---|
| 122 | * If the visualizer is registered through libgraph functions, then the
|
---|
| 123 | * field is fully managed by libgraph. Otherwise, it is a driver
|
---|
| 124 | * responsibility to set and update this field. */
|
---|
| 125 | sysarg_t reg_svc_handle;
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Visualizer ID in the client context. When client gets notified by
|
---|
| 129 | * libgraph about some event, it can use this identification to lookup
|
---|
| 130 | * data structures corresponding to a particular visualizer (e.g. viewports
|
---|
| 131 | * in the compositor).
|
---|
| 132 | * Field is fully managed by libgraph. It is assigned when the visualizer
|
---|
| 133 | * gets claimed and is valid until it is yielded. */
|
---|
| 134 | sysarg_t client_side_handle;
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Callback session to the client. Established by libgraph during initial
|
---|
| 138 | * phase of client connection. Can be used to notify client about
|
---|
| 139 | * external asynchronous changes to the output device state
|
---|
| 140 | * (e.g. monitor gets disconnected from the graphic adapter, virtual
|
---|
| 141 | * monitor is terminated by the user, pivot monitor is rotated by 90
|
---|
| 142 | * degrees, virtual monitor is resized by the user).
|
---|
| 143 | * Field is fully managed by libgraph. Device driver can use it indirectly
|
---|
| 144 | * through notification functions. */
|
---|
| 145 | async_sess_t *notif_sess;
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * Mutex protecting the mode list and default mode index. This is required
|
---|
| 149 | * for the case when device driver might asynchronously update these
|
---|
| 150 | * upon the request from the final output device (e.g. to change mode
|
---|
| 151 | * dimensions when virtual monitor is resized).
|
---|
| 152 | * Both device driver and libgraph must lock on this mutex when accessing
|
---|
| 153 | * modes list or default mode index. */
|
---|
| 154 | fibril_mutex_t mode_mtx;
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * List of all modes that can be set by this visualizer. List is populated
|
---|
| 158 | * by device driver when creating a new visualizer or when handling the
|
---|
| 159 | * request from the final output device to change the available modes.
|
---|
| 160 | * When this happens, the device driver is expected to increment version
|
---|
| 161 | * numbers in the modified modes. Modes in the list typically represent
|
---|
| 162 | * the intersection of modes supported by device driver (graphic adapter)
|
---|
| 163 | * and final output device (e.g. monitor).
|
---|
| 164 | * Field is fully managed by device driver, libgraph reads it with locked
|
---|
| 165 | * mutex. */
|
---|
| 166 | list_t modes;
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
| 169 | * Index of the default mode. Might come in handy to the clients that are
|
---|
| 170 | * not able to enumerate modes and present the choice to the user
|
---|
| 171 | * (either the client does not have such functionality or the client is
|
---|
| 172 | * bootstraping without any preliminary knowledge). Device driver shall
|
---|
| 173 | * maintain this field at the same time when it is doing any changes to the
|
---|
| 174 | * mode list.
|
---|
| 175 | * Field is fully managed by device driver, libgraph reads it with locked
|
---|
| 176 | * mutex. */
|
---|
| 177 | sysarg_t def_mode_idx;
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | * Copy of the currently established mode. It is read by both libgraph and
|
---|
| 181 | * device driver when deallocating resources for the current mode. Device
|
---|
| 182 | * driver can also read it to properly interpret the cell type and its
|
---|
| 183 | * internal structures when handling the damage.
|
---|
| 184 | * Field is fully managed by libgraph, can be read by device driver. */
|
---|
| 185 | vslmode_t cur_mode;
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
| 188 | * Determines whether the visualizer is currently set to some mode or not,
|
---|
| 189 | * that is whether cur_mode field contains up-to-date data.
|
---|
| 190 | * Field is fully managed by libgraph, can be read by device driver. */
|
---|
| 191 | bool mode_set;
|
---|
| 192 |
|
---|
| 193 | /**
|
---|
| 194 | * Device driver function pointers.
|
---|
| 195 | * Field is fully managed by device driver, libgraph invokes driver's
|
---|
| 196 | * functions through it. */
|
---|
| 197 | visualizer_ops_t ops;
|
---|
| 198 |
|
---|
| 199 | /**
|
---|
| 200 | * Backbuffer shared with the client. Sharing is established by libgraph.
|
---|
| 201 | * Device driver reads the cells when handling damage. Cells shall be
|
---|
| 202 | * interpreted according to the currently set mode.
|
---|
| 203 | * Field is fully managed by libgraph, can be read by device driver. */
|
---|
| 204 | pixelmap_t cells;
|
---|
| 205 |
|
---|
| 206 | /**
|
---|
| 207 | * Device driver context, completely opaque to the libgraph. Intended to
|
---|
| 208 | * contain pointers to frontbuffers or information representing the
|
---|
| 209 | * final output device (e.g. hardware port for physical monitor).
|
---|
| 210 | * Field is fully managed by device driver. */
|
---|
| 211 | void *dev_ctx;
|
---|
| 212 | } visualizer_t;
|
---|
| 213 |
|
---|
| 214 | typedef struct {
|
---|
| 215 | // TODO
|
---|
| 216 | int dummy;
|
---|
| 217 | } renderer_ops_t;
|
---|
| 218 |
|
---|
| 219 | /**
|
---|
| 220 | * NOTE: Following description is just a result of brainstorming on how the
|
---|
| 221 | * driver of real physical graphic accelerator could be supported by
|
---|
| 222 | * libgraph.
|
---|
| 223 | *
|
---|
| 224 | * Renderer represents the hardware graphic accelerator. If the device driver
|
---|
| 225 | * handles more than one physical accelerator (e.g. graphic cards connected in
|
---|
| 226 | * SLI mode or single graphic card with two GPUs), it is up to the driver
|
---|
| 227 | * whether the load balancing will be exposed to the clients (that is the
|
---|
| 228 | * driver will provide multiple renderers) or not (just a single renderer
|
---|
| 229 | * handling the load balancing internally).
|
---|
| 230 | *
|
---|
| 231 | * At runtime, renderer is represented by scheduling thread and multiple
|
---|
| 232 | * connection fibrils handling client requests. For each client, there is
|
---|
| 233 | * command queue, condition variable and device context. Connection fibril puts
|
---|
| 234 | * the command into the command queue and blocks on the condition variable.
|
---|
| 235 | * Scheduling thread decides what client to serve, switches corresponding device
|
---|
| 236 | * context into the accelerator, consumes the command from the command queue and
|
---|
| 237 | * executes it on the accelerator. When the command execution is finished, the
|
---|
| 238 | * condition variable si signalled and the connection fibril answers the client.
|
---|
| 239 | *
|
---|
| 240 | * Operations that are not implemented in the hardware of the accelerator might
|
---|
| 241 | * be carried out by worker threads managed by scheduling thread. If the
|
---|
| 242 | * accelerator physical memory is mapped to the address space of the driver, it
|
---|
| 243 | * could be extended by allowing scheduling thread to page out the memory and
|
---|
| 244 | * to handle the page faults.
|
---|
| 245 | *
|
---|
| 246 | * NOTE: It is not entirely clear which parts should be implemented in libgraph
|
---|
| 247 | * and which in the device driver. As a rough sketch, the connection
|
---|
| 248 | * fibril routine, command queue and memory paging should be handled
|
---|
| 249 | * by libgraph. The scheduling thread and device context should be
|
---|
| 250 | * provided by device driver.
|
---|
| 251 | */
|
---|
| 252 | typedef struct renderer {
|
---|
| 253 | // TODO
|
---|
| 254 | link_t link;
|
---|
| 255 |
|
---|
| 256 | atomic_t ref_cnt;
|
---|
| 257 |
|
---|
| 258 | sysarg_t reg_svc_handle;
|
---|
| 259 |
|
---|
| 260 | renderer_ops_t ops;
|
---|
| 261 | } renderer_t;
|
---|
| 262 |
|
---|
| 263 | /*----------------------------------------------------------------------------*/
|
---|
| 264 |
|
---|
| 265 | /**
|
---|
| 266 | * Fill in the basic visualizer structure. The device driver shall take the
|
---|
| 267 | * created torso and to complete it by adding its specific structures
|
---|
| 268 | * (device context, modes). */
|
---|
| 269 | extern void graph_init_visualizer(visualizer_t *);
|
---|
| 270 |
|
---|
| 271 | extern void graph_init_renderer(renderer_t *);
|
---|
| 272 |
|
---|
| 273 | /*----------------------------------------------------------------------------*/
|
---|
| 274 |
|
---|
| 275 | /*
|
---|
| 276 | * NOTE: All functions in this section are intended to be used only by various
|
---|
| 277 | * driver emulators (e.g. compositor client containing emulated driver
|
---|
| 278 | * to render the framebuffer of other compositor or console server).
|
---|
| 279 | * Driver of the physical hardware shall instead use similar functions
|
---|
| 280 | * from libdrv.
|
---|
| 281 | */
|
---|
| 282 |
|
---|
| 283 | /**
|
---|
| 284 | * Allocate the visualizer so it can be initialized. */
|
---|
| 285 | extern visualizer_t *graph_alloc_visualizer(void);
|
---|
| 286 |
|
---|
| 287 | /**
|
---|
| 288 | * Register the completely prepared visualizer to the location service and
|
---|
| 289 | * add it to the driver visualizer list. After the registration, the visualizer
|
---|
| 290 | * is considered ready to handle client connection. Since visualizer
|
---|
| 291 | * list is guarded by the mutex, visualizers might be added even after the
|
---|
| 292 | * initialialization of the device driver. */
|
---|
| 293 | extern int graph_register_visualizer(visualizer_t *);
|
---|
| 294 |
|
---|
| 295 | /**
|
---|
| 296 | * Retrieve the visualizer from the visualizer list according to its
|
---|
| 297 | * service ID. */
|
---|
| 298 | extern visualizer_t *graph_get_visualizer(sysarg_t);
|
---|
| 299 |
|
---|
| 300 | /**
|
---|
| 301 | * Unregister the visualizer from the location service and remove it
|
---|
| 302 | * from the driver visualizer list. Function shall be called by device driver
|
---|
| 303 | * before deallocating the resources for the visualizer. */
|
---|
| 304 | extern int graph_unregister_visualizer(visualizer_t *);
|
---|
| 305 |
|
---|
| 306 | /**
|
---|
| 307 | * Destroy the rest of the visualizer. Device driver shall call this function
|
---|
| 308 | * only after it has unregistered the visualizer and deallocated all the
|
---|
| 309 | * resources for which the driver is responsible. */
|
---|
| 310 | extern void graph_destroy_visualizer(visualizer_t *);
|
---|
| 311 |
|
---|
| 312 | extern renderer_t *graph_alloc_renderer(void);
|
---|
| 313 | extern int graph_register_renderer(renderer_t *);
|
---|
| 314 | extern renderer_t *graph_get_renderer(sysarg_t);
|
---|
| 315 | extern int graph_unregister_renderer(renderer_t *);
|
---|
| 316 | extern void graph_destroy_renderer(renderer_t *);
|
---|
| 317 |
|
---|
| 318 | /*----------------------------------------------------------------------------*/
|
---|
| 319 |
|
---|
| 320 | /**
|
---|
| 321 | * Device driver can call this function to notify the client through the
|
---|
| 322 | * callback connection that the visualizer with a specified service ID should
|
---|
| 323 | * be switched to the mode with the given index. */
|
---|
| 324 | extern int graph_notify_mode_change(async_sess_t *, sysarg_t, sysarg_t);
|
---|
| 325 |
|
---|
| 326 | /**
|
---|
| 327 | * Device driver can call this function to notify the client through the
|
---|
| 328 | * callback connection that the visualizer with a specified service ID has
|
---|
| 329 | * lost its output device (e.g. virtual monitor was closed by a user). */
|
---|
| 330 | extern int graph_notify_disconnect(async_sess_t *, sysarg_t);
|
---|
| 331 |
|
---|
| 332 | /*----------------------------------------------------------------------------*/
|
---|
| 333 |
|
---|
| 334 | /** Shall be registered to libdrv by physical device driver. */
|
---|
| 335 | extern void graph_visualizer_connection(visualizer_t *, ipc_callid_t, ipc_call_t *, void *);
|
---|
| 336 |
|
---|
| 337 | /** Shall be registered to libdrv by physical device driver. */
|
---|
| 338 | extern void graph_renderer_connection(renderer_t *, ipc_callid_t, ipc_call_t *, void *);
|
---|
| 339 |
|
---|
| 340 | /** Shall be registered to location service by emulated device driver. */
|
---|
| 341 | extern void graph_client_connection(ipc_callid_t, ipc_call_t *, void *);
|
---|
| 342 |
|
---|
| 343 | #endif
|
---|
| 344 |
|
---|
| 345 | /** @}
|
---|
| 346 | */
|
---|