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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (C) 2009-2015 Red Hat, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DISPLAY_CHANNEL_H_
# define DISPLAY_CHANNEL_H_
#include <setjmp.h>
#include "common/rect.h"
#include "red_worker.h"
#include "reds_stream.h"
#include "cache-item.h"
#include "pixmap-cache.h"
#ifdef USE_OPENGL
#include "common/ogl_ctx.h"
#include "reds_gl_canvas.h"
#endif /* USE_OPENGL */
#include "reds_sw_canvas.h"
#include "glz_encoder_dictionary.h"
#include "glz_encoder.h"
#include "stat.h"
#include "reds.h"
#include "mjpeg_encoder.h"
#include "red_memslots.h"
#include "red_parse_qxl.h"
#include "red_record_qxl.h"
#include "jpeg_encoder.h"
#ifdef USE_LZ4
#include "lz4_encoder.h"
#endif
#include "demarshallers.h"
#include "zlib_encoder.h"
#include "red_channel.h"
#include "red_dispatcher.h"
#include "dispatcher.h"
#include "main_channel.h"
#include "migration_protocol.h"
#include "main_dispatcher.h"
#include "spice_server_utils.h"
#include "spice_bitmap_utils.h"
#include "spice_image_cache.h"
#include "utils.h"
#include "tree.h"
#include "stream.h"
#define PALETTE_CACHE_HASH_SHIFT 8
#define PALETTE_CACHE_HASH_SIZE (1 << PALETTE_CACHE_HASH_SHIFT)
#define PALETTE_CACHE_HASH_MASK (PALETTE_CACHE_HASH_SIZE - 1)
#define PALETTE_CACHE_HASH_KEY(id) ((id) & PALETTE_CACHE_HASH_MASK)
#define CLIENT_PALETTE_CACHE_SIZE 128
/* Each drawable can refer to at most 3 images: src, brush and mask */
#define MAX_DRAWABLE_PIXMAP_CACHE_ITEMS 3
#define NUM_STREAMS 50
#define NUM_SURFACES 10000
typedef struct RedCompressBuf RedCompressBuf;
struct RedCompressBuf {
uint8_t buf[64 * 1024];
RedCompressBuf *next;
RedCompressBuf *send_next;
};
typedef struct WaitForChannels {
SpiceMsgWaitForChannels header;
SpiceWaitForChannel buf[MAX_CACHE_CLIENTS];
} WaitForChannels;
typedef struct FreeList {
int res_size;
SpiceResourceList *res;
uint64_t sync[MAX_CACHE_CLIENTS];
WaitForChannels wait;
} FreeList;
typedef struct GlzSharedDictionary {
RingItem base;
GlzEncDictContext *dict;
uint32_t refs;
uint8_t id;
pthread_rwlock_t encode_lock;
int migrate_freeze;
RedClient *client; // channel clients of the same client share the dict
} GlzSharedDictionary;
typedef struct {
DisplayChannelClient *dcc;
RedCompressBuf *bufs_head;
RedCompressBuf *bufs_tail;
jmp_buf jmp_env;
union {
struct {
SpiceChunks *chunks;
int next;
int stride;
int reverse;
} lines_data;
struct {
RedCompressBuf* next;
int size_left;
} compressed_data; // for encoding data that was already compressed by another method
} u;
char message_buf[512];
} EncoderData;
typedef struct {
GlzEncoderUsrContext usr;
EncoderData data;
} GlzData;
typedef struct DependItem {
Drawable *drawable;
RingItem ring_item;
} DependItem;
struct Drawable {
uint8_t refs;
RingItem surface_list_link;
RingItem list_link;
DrawItem tree_item;
Ring pipes;
PipeItem *pipe_item_rest;
uint32_t size_pipe_item_rest;
RedDrawable *red_drawable;
Ring glz_ring;
red_time_t creation_time;
int frames_count;
int gradual_frames_count;
int last_gradual_frame;
Stream *stream;
Stream *sized_stream;
int streamable;
BitmapGradualType copy_bitmap_graduality;
uint32_t group_id;
DependItem depend_items[3];
int surface_id;
int surface_deps[3];
uint32_t process_commands_generation;
};
struct DisplayChannelClient {
CommonChannelClient common;
SpiceImageCompression image_compression;
spice_wan_compression_t jpeg_state;
spice_wan_compression_t zlib_glz_state;
int expect_init;
PixmapCache *pixmap_cache;
uint32_t pixmap_cache_generation;
int pending_pixmaps_sync;
CacheItem *palette_cache[PALETTE_CACHE_HASH_SIZE];
Ring palette_cache_lru;
long palette_cache_available;
uint32_t palette_cache_items;
struct {
uint32_t stream_outbuf_size;
uint8_t *stream_outbuf; // caution stream buffer is also used as compress bufs!!!
RedCompressBuf *used_compress_bufs;
FreeList free_list;
uint64_t pixmap_cache_items[MAX_DRAWABLE_PIXMAP_CACHE_ITEMS];
int num_pixmap_cache_items;
} send_data;
/* global lz encoding entities */
GlzSharedDictionary *glz_dict;
GlzEncoderContext *glz;
GlzData glz_data;
Ring glz_drawables; // all the living lz drawable, ordered by encoding time
Ring glz_drawables_inst_to_free; // list of instances to be freed
pthread_mutex_t glz_drawables_inst_to_free_lock;
uint8_t surface_client_created[NUM_SURFACES];
QRegion surface_client_lossy_region[NUM_SURFACES];
StreamAgent stream_agents[NUM_STREAMS];
int use_mjpeg_encoder_rate_control;
uint32_t streams_max_latency;
uint64_t streams_max_bit_rate;
};
#define DCC_TO_WORKER(dcc) \
(SPICE_CONTAINEROF((dcc)->common.base.channel, CommonChannel, base)->worker)
#define DCC_TO_DC(dcc) \
SPICE_CONTAINEROF((dcc)->common.base.channel, DisplayChannel, common.base)
#define RCC_TO_DCC(rcc) SPICE_CONTAINEROF((rcc), DisplayChannelClient, common.base)
enum {
PIPE_ITEM_TYPE_DRAW = PIPE_ITEM_TYPE_COMMON_LAST,
PIPE_ITEM_TYPE_IMAGE,
PIPE_ITEM_TYPE_STREAM_CREATE,
PIPE_ITEM_TYPE_STREAM_CLIP,
PIPE_ITEM_TYPE_STREAM_DESTROY,
PIPE_ITEM_TYPE_UPGRADE,
PIPE_ITEM_TYPE_MIGRATE_DATA,
PIPE_ITEM_TYPE_PIXMAP_SYNC,
PIPE_ITEM_TYPE_PIXMAP_RESET,
PIPE_ITEM_TYPE_INVAL_PALETTE_CACHE,
PIPE_ITEM_TYPE_CREATE_SURFACE,
PIPE_ITEM_TYPE_DESTROY_SURFACE,
PIPE_ITEM_TYPE_MONITORS_CONFIG,
PIPE_ITEM_TYPE_STREAM_ACTIVATE_REPORT,
};
DisplayChannelClient* dcc_new (DisplayChannel *display,
RedClient *client,
RedsStream *stream,
int mig_target,
uint32_t *common_caps,
int num_common_caps,
uint32_t *caps,
int num_caps,
SpiceImageCompression image_compression,
spice_wan_compression_t jpeg_state,
spice_wan_compression_t zlib_glz_state);
void dcc_push_monitors_config (DisplayChannelClient *dcc);
void dcc_push_destroy_surface (DisplayChannelClient *dcc,
uint32_t surface_id);
typedef struct DrawablePipeItem {
RingItem base; /* link for a list of pipe items held by Drawable */
PipeItem dpi_pipe_item; /* link for the client's pipe itself */
Drawable *drawable;
DisplayChannelClient *dcc;
uint8_t refs;
} DrawablePipeItem;
DrawablePipeItem* drawable_pipe_item_new (DisplayChannelClient *dcc,
Drawable *drawable);
void drawable_pipe_item_unref (DrawablePipeItem *dpi);
DrawablePipeItem* drawable_pipe_item_ref (DrawablePipeItem *dpi);
typedef struct MonitorsConfig {
int refs;
int count;
int max_allowed;
QXLHead heads[0];
} MonitorsConfig;
typedef struct MonitorsConfigItem {
PipeItem pipe_item;
MonitorsConfig *monitors_config;
} MonitorsConfigItem;
MonitorsConfig* monitors_config_new (QXLHead *heads, ssize_t nheads,
ssize_t max);
MonitorsConfig * monitors_config_ref (MonitorsConfig *config);
void monitors_config_unref (MonitorsConfig *config);
#define TRACE_ITEMS_SHIFT 3
#define NUM_TRACE_ITEMS (1 << TRACE_ITEMS_SHIFT)
#define ITEMS_TRACE_MASK (NUM_TRACE_ITEMS - 1)
typedef struct DrawContext {
SpiceCanvas *canvas;
int canvas_draws_on_surface;
int top_down;
uint32_t width;
uint32_t height;
int32_t stride;
uint32_t format;
void *line_0;
} DrawContext;
typedef struct RedSurface {
uint32_t refs;
Ring current;
Ring current_list;
DrawContext context;
Ring depend_on_me;
QRegion draw_dirty_region;
//fix me - better handling here
QXLReleaseInfoExt create, destroy;
} RedSurface;
#define NUM_DRAWABLES 1000
typedef struct _Drawable _Drawable;
struct _Drawable {
union {
Drawable drawable;
_Drawable *next;
} u;
};
struct DisplayChannel {
CommonChannel common; // Must be the first thing
MonitorsConfig *monitors_config;
uint32_t num_renderers;
uint32_t renderers[RED_RENDERER_LAST];
uint32_t renderer;
int enable_jpeg;
int jpeg_quality;
int enable_zlib_glz_wrap;
int zlib_level;
Ring current_list; // of TreeItem
uint32_t current_size;
uint32_t drawable_count;
_Drawable drawables[NUM_DRAWABLES];
_Drawable *free_drawables;
int stream_video;
uint32_t stream_count;
Stream streams_buf[NUM_STREAMS];
Stream *free_streams;
Ring streams;
ItemTrace items_trace[NUM_TRACE_ITEMS];
uint32_t next_item_trace;
uint64_t streams_size_total;
RedSurface surfaces[NUM_SURFACES];
uint32_t n_surfaces;
SpiceImageSurfaces image_surfaces;
ImageCache image_cache;
RedCompressBuf *free_compress_bufs;
/* TODO: some day unify this, make it more runtime.. */
#ifdef RED_WORKER_STAT
stat_info_t add_stat;
stat_info_t exclude_stat;
stat_info_t __exclude_stat;
uint32_t add_count;
uint32_t add_with_shadow_count;
#endif
#ifdef RED_STATISTICS
uint64_t *cache_hits_counter;
uint64_t *add_to_cache_counter;
uint64_t *non_cache_counter;
#endif
#ifdef COMPRESS_STAT
stat_info_t lz_stat;
stat_info_t glz_stat;
stat_info_t quic_stat;
stat_info_t jpeg_stat;
stat_info_t zlib_glz_stat;
stat_info_t jpeg_alpha_stat;
stat_info_t lz4_stat;
#endif
};
#define LINK_TO_DCC(ptr) SPICE_CONTAINEROF(ptr, DisplayChannelClient, \
common.base.channel_link)
#define DCC_FOREACH_SAFE(link, next, dcc, channel) \
SAFE_FOREACH(link, next, channel, &(channel)->clients, dcc, LINK_TO_DCC(link))
#define FOREACH_DCC(display_channel, link, next, dcc) \
DCC_FOREACH_SAFE(link, next, dcc, RED_CHANNEL(display_channel))
static inline int get_stream_id(DisplayChannel *display, Stream *stream)
{
return (int)(stream - display->streams_buf);
}
typedef struct SurfaceDestroyItem {
SpiceMsgSurfaceDestroy surface_destroy;
PipeItem pipe_item;
} SurfaceDestroyItem;
typedef struct SurfaceCreateItem {
SpiceMsgSurfaceCreate surface_create;
PipeItem pipe_item;
} SurfaceCreateItem;
void display_channel_set_stream_video (DisplayChannel *display,
int stream_video);
void display_channel_attach_stream (DisplayChannel *display,
Drawable *drawable,
Stream *stream);
int display_channel_get_streams_timeout (DisplayChannel *display);
void display_channel_compress_stats_print (const DisplayChannel *display);
void display_channel_compress_stats_reset (DisplayChannel *display);
void display_channel_drawable_unref (DisplayChannel *display, Drawable *drawable);
void display_channel_surface_unref (DisplayChannel *display,
uint32_t surface_id);
bool display_channel_surface_has_canvas (DisplayChannel *display,
uint32_t surface_id);
void display_channel_show_tree (DisplayChannel *display);
static inline int is_equal_path(SpicePath *path1, SpicePath *path2)
{
SpicePathSeg *seg1, *seg2;
int i, j;
if (path1->num_segments != path2->num_segments)
return FALSE;
for (i = 0; i < path1->num_segments; i++) {
seg1 = path1->segments[i];
seg2 = path2->segments[i];
if (seg1->flags != seg2->flags ||
seg1->count != seg2->count) {
return FALSE;
}
for (j = 0; j < seg1->count; j++) {
if (seg1->points[j].x != seg2->points[j].x ||
seg1->points[j].y != seg2->points[j].y) {
return FALSE;
}
}
}
return TRUE;
}
// partial imp
static inline int is_equal_brush(SpiceBrush *b1, SpiceBrush *b2)
{
return b1->type == b2->type &&
b1->type == SPICE_BRUSH_TYPE_SOLID &&
b1->u.color == b2->u.color;
}
// partial imp
static inline int is_equal_line_attr(SpiceLineAttr *a1, SpiceLineAttr *a2)
{
return a1->flags == a2->flags &&
a1->style_nseg == a2->style_nseg &&
a1->style_nseg == 0;
}
// partial imp
static inline int is_same_geometry(Drawable *d1, Drawable *d2)
{
if (d1->red_drawable->type != d2->red_drawable->type) {
return FALSE;
}
switch (d1->red_drawable->type) {
case QXL_DRAW_STROKE:
return is_equal_line_attr(&d1->red_drawable->u.stroke.attr,
&d2->red_drawable->u.stroke.attr) &&
is_equal_path(d1->red_drawable->u.stroke.path,
d2->red_drawable->u.stroke.path);
case QXL_DRAW_FILL:
return rect_is_equal(&d1->red_drawable->bbox, &d2->red_drawable->bbox);
default:
return FALSE;
}
}
static inline int is_same_drawable(Drawable *d1, Drawable *d2)
{
if (!is_same_geometry(d1, d2)) {
return FALSE;
}
switch (d1->red_drawable->type) {
case QXL_DRAW_STROKE:
return is_equal_brush(&d1->red_drawable->u.stroke.brush,
&d2->red_drawable->u.stroke.brush);
case QXL_DRAW_FILL:
return is_equal_brush(&d1->red_drawable->u.fill.brush,
&d2->red_drawable->u.fill.brush);
default:
return FALSE;
}
}
static inline int is_primary_surface(DisplayChannel *display, uint32_t surface_id)
{
if (surface_id == 0) {
return TRUE;
}
return FALSE;
}
#endif /* DISPLAY_CHANNEL_H_ */
|