1 /*
2  * Copyright (C) 2010 Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 or
7  * (at your option) version 3 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef UI_SPICE_DISPLAY_H
19 #define UI_SPICE_DISPLAY_H
20 
21 #include <spice.h>
22 #include <spice/ipc_ring.h>
23 #include <spice/enums.h>
24 #include <spice/qxl_dev.h>
25 
26 #include "qemu/thread.h"
27 #include "ui/qemu-pixman.h"
28 #include "ui/console.h"
29 
30 #if defined(CONFIG_OPENGL) && defined(CONFIG_GBM)
31 # if SPICE_SERVER_VERSION >= 0x000d01 /* release 0.13.1 */
32 #  define HAVE_SPICE_GL 1
33 #  include "ui/egl-helpers.h"
34 #  include "ui/egl-context.h"
35 # endif
36 #endif
37 
38 #define NUM_MEMSLOTS 8
39 #define MEMSLOT_GENERATION_BITS 8
40 #define MEMSLOT_SLOT_BITS 8
41 
42 #define MEMSLOT_GROUP_HOST  0
43 #define MEMSLOT_GROUP_GUEST 1
44 #define NUM_MEMSLOTS_GROUPS 2
45 
46 /*
47  * Internal enum to differenciate between options for
48  * io calls that have a sync (old) version and an _async (new)
49  * version:
50  *  QXL_SYNC: use the old version
51  *  QXL_ASYNC: use the new version and make sure there are no two
52  *   happening at the same time. This is used for guest initiated
53  *   calls
54  */
55 typedef enum qxl_async_io {
56     QXL_SYNC,
57     QXL_ASYNC,
58 } qxl_async_io;
59 
60 enum {
61     QXL_COOKIE_TYPE_IO,
62     QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
63     QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG,
64     QXL_COOKIE_TYPE_GL_DRAW_DONE,
65 };
66 
67 typedef struct QXLCookie {
68     int      type;
69     uint64_t io;
70     union {
71         uint32_t surface_id;
72         QXLRect area;
73         struct {
74             QXLRect area;
75             int redraw;
76         } render;
77         void *data;
78     } u;
79 } QXLCookie;
80 
81 QXLCookie *qxl_cookie_new(int type, uint64_t io);
82 
83 typedef struct SimpleSpiceDisplay SimpleSpiceDisplay;
84 typedef struct SimpleSpiceUpdate SimpleSpiceUpdate;
85 typedef struct SimpleSpiceCursor SimpleSpiceCursor;
86 
87 struct SimpleSpiceDisplay {
88     DisplaySurface *ds;
89     DisplayChangeListener dcl;
90     void *buf;
91     int bufsize;
92     QXLInstance qxl;
93     uint32_t unique;
94     pixman_image_t *surface;
95     pixman_image_t *mirror;
96     int32_t num_surfaces;
97 
98     QXLRect dirty;
99     int notify;
100 
101     /*
102      * All struct members below this comment can be accessed from
103      * both spice server and qemu (iothread) context and any access
104      * to them must be protected by the lock.
105      */
106     QemuMutex lock;
107     QTAILQ_HEAD(, SimpleSpiceUpdate) updates;
108 
109     /* cursor (without qxl): displaychangelistener -> spice server */
110     SimpleSpiceCursor *ptr_define;
111     SimpleSpiceCursor *ptr_move;
112     int16_t ptr_x, ptr_y;
113     int16_t hot_x, hot_y;
114 
115     /* cursor (with qxl): qxl local renderer -> displaychangelistener */
116     QEMUCursor *cursor;
117     int mouse_x, mouse_y;
118     QEMUBH *cursor_bh;
119 
120 #ifdef HAVE_SPICE_GL
121     /* opengl rendering */
122     QEMUBH *gl_unblock_bh;
123     QEMUTimer *gl_unblock_timer;
124     QemuGLShader *gls;
125     int gl_updates;
126     bool have_scanout;
127     bool have_surface;
128 
129     QemuDmaBuf *guest_dmabuf;
130     bool guest_dmabuf_refresh;
131     bool render_cursor;
132 
133     egl_fb guest_fb;
134     egl_fb blit_fb;
135     egl_fb cursor_fb;
136     bool have_hot;
137 #endif
138 };
139 
140 struct SimpleSpiceUpdate {
141     QXLDrawable drawable;
142     QXLImage image;
143     QXLCommandExt ext;
144     uint8_t *bitmap;
145     QTAILQ_ENTRY(SimpleSpiceUpdate) next;
146 };
147 
148 struct SimpleSpiceCursor {
149     QXLCursorCmd cmd;
150     QXLCommandExt ext;
151     QXLCursor cursor;
152 };
153 
154 extern bool spice_opengl;
155 
156 int qemu_spice_rect_is_empty(const QXLRect* r);
157 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r);
158 
159 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update);
160 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd);
161 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd);
162 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd);
163 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd);
164 
165 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
166                                int x, int y, int w, int h);
167 void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
168                                DisplaySurface *surface);
169 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd);
170 void qemu_spice_cursor_refresh_bh(void *opaque);
171 
172 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
173                             qxl_async_io async);
174 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid,
175                             uint32_t sid);
176 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
177                                        QXLDevSurfaceCreate *surface,
178                                        qxl_async_io async);
179 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
180                                         uint32_t id, qxl_async_io async);
181 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd);
182 void qemu_spice_display_start(void);
183 void qemu_spice_display_stop(void);
184 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd);
185 
186 bool qemu_spice_fill_device_address(QemuConsole *con,
187                                     char *device_address,
188                                     size_t size);
189 
190 #endif
191