summaryrefslogtreecommitdiff
path: root/gdk/broadway/broadway.c
blob: b17d90bf46e1c7995c2b2c010b37d9f1a5afad24 (plain)
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <cairo.h>

#include "broadway.h"

/************************************************************************
 *                Base64 functions                                      *
 ************************************************************************/

static const char base64_alphabet[] =
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

#if 0 /* Unused for now */
static void
base64_uint8 (guint8 v, char *c)
{
  c[0] = base64_alphabet[(v >> 0) & 0x3f];
  c[1] = base64_alphabet[(v >> 6) & 0x3];
}
#endif

static void
base64_uint16 (guint32 v, char *c)
{
  c[0] = base64_alphabet[(v >> 0) & 0x3f];
  c[1] = base64_alphabet[(v >> 6) & 0x3f];
  c[2] = base64_alphabet[(v >> 12) & 0xf];
}

#if 0
static void
base64_uint24 (guint32 v, char *c)
{
  c[0] = base64_alphabet[(v >> 0) & 0x3f];
  c[1] = base64_alphabet[(v >> 6) & 0x3f];
  c[2] = base64_alphabet[(v >> 12) & 0x3f];
  c[3] = base64_alphabet[(v >> 18) & 0x3f];
}
#endif

static void
base64_uint32 (guint32 v, char *c)
{
  c[0] = base64_alphabet[(v >> 0) & 0x3f];
  c[1] = base64_alphabet[(v >> 6) & 0x3f];
  c[2] = base64_alphabet[(v >> 12) & 0x3f];
  c[3] = base64_alphabet[(v >> 18) & 0x3f];
  c[4] = base64_alphabet[(v >> 24) & 0x3f];
  c[5] = base64_alphabet[(v >> 30) & 0x2];
}

/***********************************************************
 *  conversion of raw image data to png data: uris         *
 ***********************************************************/

struct PngTarget {
  GString *url;
  int state;
  int save;
};

static cairo_status_t
write_png_url (void		  *closure,
	       const unsigned char *data,
	       unsigned int	   data_len)
{
  struct PngTarget *target = closure;
  gsize res, old_len;

  old_len = target->url->len;
  g_string_set_size (target->url,
		     old_len + (data_len / 3 + 1) * 4 + 4);

  res = g_base64_encode_step (data, data_len, FALSE,
			      target->url->str + old_len,
			      &target->state, &target->save);

  g_string_set_size (target->url,  old_len + res);

  return CAIRO_STATUS_SUCCESS;
}

static char *
to_png_rgb (int w, int h, int byte_stride, guint32 *data)
{
  cairo_surface_t *surface;
  struct PngTarget target;
  gsize res, old_len;

  target.url = g_string_new ("data:image/png;base64,");
  target.state = 0;
  target.save = 0;

  surface = cairo_image_surface_create_for_data ((guchar *)data,
						 CAIRO_FORMAT_RGB24, w, h, byte_stride);

  cairo_surface_write_to_png_stream (surface, write_png_url, &target);

  old_len = target.url->len;

  g_string_set_size (target.url, old_len + 4);
  res = g_base64_encode_close (FALSE,
			       target.url->str + old_len,
			       &target.state, &target.save);
  g_string_set_size (target.url, old_len + res);

  return g_string_free (target.url, FALSE);
}

static char *
to_png_rgba (int w, int h, int byte_stride, guint32 *data)
{
  cairo_surface_t *surface;
  struct PngTarget target;
  gsize res, old_len;

  target.url = g_string_new ("data:image/png;base64,");
  target.state = 0;
  target.save = 0;

  surface = cairo_image_surface_create_for_data ((guchar *)data,
						 CAIRO_FORMAT_ARGB32, w, h, byte_stride);

  cairo_surface_write_to_png_stream (surface, write_png_url, &target);

  old_len = target.url->len;

  g_string_set_size (target.url, old_len + 4);
  res = g_base64_encode_close (FALSE,
			       target.url->str + old_len,
			       &target.state, &target.save);
  g_string_set_size (target.url, old_len + res);

  return g_string_free (target.url, FALSE);
}

#if 0
static char *
to_png_a (int w, int h, int byte_stride, guint8 *data)
{
  cairo_surface_t *surface;
  struct PngTarget target;
  gsize res, old_len;

  target.url = g_string_new ("data:image/png;base64,");
  target.state = 0;
  target.save = 0;

  surface = cairo_image_surface_create_for_data (data,
						 CAIRO_FORMAT_A8, w, h, byte_stride);

  cairo_surface_write_to_png_stream (surface, write_png_url, &target);

  old_len = target.url->len;

  g_string_set_size (target.url, old_len + 4);
  res = g_base64_encode_close (FALSE,
			       target.url->str + old_len,
			       &target.state, &target.save);
  g_string_set_size (target.url, old_len + res);

  return g_string_free (target.url, FALSE);
}
#endif

/************************************************************************
 *                Basic I/O primitives                                  *
 ************************************************************************/

struct BroadwayOutput {
  GOutputStream *out;
  GString *buf;
  int error;
  guint32 serial;
  gboolean proto_v7_plus;
};

static void
broadway_output_send_cmd (BroadwayOutput *output,
			  gboolean fin, BroadwayWSOpCode code,
			  const void *buf, gsize count)
{
  gboolean mask = FALSE;
  guchar header[16];
  size_t p;

  gboolean mid_header = count > 125 && count <= 65535;
  gboolean long_header = count > 65535;

  /* NB. big-endian spec => bit 0 == MSB */
  header[0] = ( (fin ? 0x80 : 0) | (code & 0x0f) );
  header[1] = ( (mask ? 0x80 : 0) |
                (mid_header ? 126 : long_header ? 127 : count) );
  p = 2;
  if (mid_header)
    {
      *(guint16 *)(header + p) = GUINT16_TO_BE( (guint16)count );
      p += 2;
    }
  else if (long_header)
    {
      *(guint64 *)(header + p) = GUINT64_TO_BE( count );
      p += 8;
    }
  // FIXME: if we are paranoid we should 'mask' the data
  // FIXME: we should really emit these as a single write
  g_output_stream_write_all (output->out, header, p, NULL, NULL, NULL);
  g_output_stream_write_all (output->out, buf, count, NULL, NULL, NULL);
}

static void
broadway_output_send_cmd_pre_v7 (BroadwayOutput *output,
				 const void *buf, gsize count)
{
  g_output_stream_write_all (output->out, "\0", 1, NULL, NULL, NULL);
  g_output_stream_write_all (output->out, buf, count, NULL, NULL, NULL);
  g_output_stream_write_all (output->out, "\xff", 1, NULL, NULL, NULL);
}


static void
broadway_output_sendmsg (BroadwayOutput *output,
			 const void *buf, gsize count)
{
  g_string_append_len (output->buf, buf, count);
}

void broadway_output_pong (BroadwayOutput *output)
{
  if (output->proto_v7_plus)
    broadway_output_send_cmd (output, TRUE, BROADWAY_WS_CNX_PONG, NULL, 0);
}

int
broadway_output_flush (BroadwayOutput *output)
{
  if (output->buf->len == 0)
    return TRUE;

  if (!output->proto_v7_plus)
    broadway_output_send_cmd_pre_v7 (output, output->buf->str, output->buf->len);
  else
    broadway_output_send_cmd (output, TRUE, BROADWAY_WS_TEXT,
			      output->buf->str, output->buf->len);

  g_string_set_size (output->buf, 0);

  return !output->error;

}

BroadwayOutput *
broadway_output_new (GOutputStream *out, guint32 serial,
		     gboolean proto_v7_plus)
{
  BroadwayOutput *output;

  output = g_new0 (BroadwayOutput, 1);

  output->out = g_object_ref (out);
  output->buf = g_string_new ("");
  output->serial = serial;
  output->proto_v7_plus = proto_v7_plus;

  return output;
}

void
broadway_output_free (BroadwayOutput *output)
{
  g_object_unref (output->out);
  free (output);
}

guint32
broadway_output_get_next_serial (BroadwayOutput *output)
{
  return output->serial;
}


/************************************************************************
 *                     Core rendering operations                        *
 ************************************************************************/

#define HEADER_LEN (1+6)

static void
append_uint16 (guint32 v, char *buf, int *p)
{
  base64_uint16 (v, &buf[*p]);
  *p += 3;
}

static void
append_uint32 (guint32 v, char *buf, int *p)
{
  base64_uint32 (v, &buf[*p]);
  *p += 6;
}

static int
write_header(BroadwayOutput *output, char *buf, char op)
{
  int p;

  p = 0;
  buf[p++] = op;
  append_uint32 (output->serial++, buf, &p);

  return p;
}

void
broadway_output_copy_rectangles (BroadwayOutput *output,  int id,
				 BroadwayRect *rects, int n_rects,
				 int dx, int dy)
{
  char *buf;
  int len, i, p;

  len = HEADER_LEN + 3 + 3 + 3*4*n_rects + 3 + 3;

  buf = g_malloc (len);
  p = write_header (output, buf, 'b');
  append_uint16 (id, buf, &p);
  append_uint16 (n_rects, buf, &p);
  for (i = 0; i < n_rects; i++)
    {
      append_uint16 (rects[i].x, buf, &p);
      append_uint16 (rects[i].y, buf, &p);
      append_uint16 (rects[i].width, buf, &p);
      append_uint16 (rects[i].height, buf, &p);
    }
  append_uint16 (dx, buf, &p);
  append_uint16 (dy, buf, &p);

  assert (p == len);

  broadway_output_sendmsg (output, buf, len);
  free (buf);
}

void
broadway_output_grab_pointer (BroadwayOutput *output,
			      int id,
			      gboolean owner_event)
{
  char buf[HEADER_LEN + 3 + 1];
  int p;

  p = write_header (output, buf, 'g');
  append_uint16 (id, buf, &p);
  buf[p++] = owner_event ? '1': '0';

  assert (p == sizeof (buf));

  broadway_output_sendmsg (output, buf, sizeof (buf));
}

guint32
broadway_output_ungrab_pointer (BroadwayOutput *output)
{
  char buf[HEADER_LEN];
  guint32 serial;
  int p;

  serial = output->serial;
  p = write_header (output, buf, 'u');

  assert (p == sizeof (buf));

  broadway_output_sendmsg (output, buf, sizeof (buf));

  return serial;
}

void
broadway_output_new_surface(BroadwayOutput *output,
			    int id, int x, int y, int w, int h,
			    gboolean is_temp)
{
  char buf[HEADER_LEN + 16];
  int p;

  p = write_header (output, buf, 's');
  append_uint16 (id, buf, &p);
  append_uint16 (x, buf, &p);
  append_uint16 (y, buf, &p);
  append_uint16 (w, buf, &p);
  append_uint16 (h, buf, &p);
  buf[p++] = is_temp ? '1' : '0';

  assert (p == sizeof (buf));

  broadway_output_sendmsg (output, buf, sizeof (buf));
}

void
broadway_output_show_surface(BroadwayOutput *output,  int id)
{
  char buf[HEADER_LEN + 3];
  int p;

  p = write_header (output, buf, 'S');
  append_uint16 (id, buf, &p);

  assert (p == sizeof (buf));

  broadway_output_sendmsg (output, buf, sizeof (buf));
}

void
broadway_output_hide_surface(BroadwayOutput *output,  int id)
{
  char buf[HEADER_LEN + 3];
  int p;

  p = write_header (output, buf, 'H');
  append_uint16 (id, buf, &p);

  assert (p == sizeof (buf));

  broadway_output_sendmsg (output, buf, sizeof (buf));
}

void
broadway_output_destroy_surface(BroadwayOutput *output,  int id)
{
  char buf[HEADER_LEN + 3];
  int p;

  p = write_header (output, buf, 'd');
  append_uint16 (id, buf, &p);

  assert (p == sizeof (buf));

  broadway_output_sendmsg (output, buf, sizeof (buf));
}


void
broadway_output_move_resize_surface (BroadwayOutput *output,
				     int             id,
				     gboolean        has_pos,
				     int             x,
				     int             y,
				     gboolean        has_size,
				     int             w,
				     int             h)
{
  char buf[HEADER_LEN+3+1+6+6];
  int p;
  int val;

  if (!has_pos && !has_size)
    return;

  p = write_header (output, buf, 'm');

  val = (!!has_pos) | ((!!has_size) << 1);
  append_uint16 (id, buf, &p);
  buf[p++] = val + '0';
  if (has_pos)
    {
      append_uint16 (x, buf, &p);
      append_uint16 (y, buf, &p);
    }
  if (has_size)
    {
      append_uint16 (w, buf, &p);
      append_uint16 (h, buf, &p);
    }
  assert (p <= sizeof (buf));

  broadway_output_sendmsg (output, buf, p);
}

void
broadway_output_set_transient_for (BroadwayOutput *output,
				   int             id,
				   int             parent_id)
{
  char buf[HEADER_LEN + 6];
  int p;

  p = write_header (output, buf, 'p');

  append_uint16 (id, buf, &p);
  append_uint16 (parent_id, buf, &p);

  assert (p == sizeof (buf));

  broadway_output_sendmsg (output, buf, sizeof (buf));
}


void
broadway_output_put_rgb (BroadwayOutput *output,  int id, int x, int y,
			 int w, int h, int byte_stride, void *data)
{
  gsize buf_size;
  gsize url_len;
  char *url, *buf;
  int p;

  url = to_png_rgb (w, h, byte_stride, (guint32*)data);
  url_len = strlen (url);

  buf_size = HEADER_LEN + 15 + url_len;
  buf = g_malloc (buf_size);

  p = write_header (output, buf, 'i');

  append_uint16 (id, buf, &p);
  append_uint16 (x, buf, &p);
  append_uint16 (y, buf, &p);

  append_uint32 (url_len, buf, &p);

  g_assert (p == HEADER_LEN + 15);
  strncpy (buf + p, url, url_len);

  broadway_output_sendmsg (output, buf, buf_size);

  g_free (buf);
  free (url);
}

typedef struct  {
  int x1, y1;
  int x2, y2;
} BroadwayBox;

static int
is_any_x_set (unsigned char *data,
	      int box_x1, int box_x2,
	      int x1, int x2, int y, int *x_set,
	      int byte_stride)
{
  int w ;
  guint32 *ptr;

  if (x1 < box_x1)
    x1 = box_x1;

  if (x2 > box_x2)
    x2 = box_x2;

  w = x2 - x1;
  if (w > 0)
    {
      ptr = (guint32 *)(data + y * byte_stride + x1 * 4);
      while (w-- > 0)
	{
	  if (*ptr != 0)
	    {
	      if (x_set)
		*x_set = x1;
	      return 1;
	    }
	  ptr++;
	  x1++;
	}
    }
  return 0;
}


#define EXTEND_X_FUZZ 10
#define EXTEND_Y_FUZZ 10

static int
extend_x_range (unsigned char *data,
		int box_x1, int box_y1,
		int box_x2, int box_y2,
		int *x1, int *x2, int y,
		int byte_stride)
{
  int extended = 0;
  int new_x;

  while (is_any_x_set (data, box_x1, box_x2, *x1 - EXTEND_X_FUZZ, *x1, y, &new_x, byte_stride))
    {
      *x1 = new_x;
      extended = 1;
    }

  while (is_any_x_set (data, box_x1, box_x2, *x2, *x2 + EXTEND_X_FUZZ, y, &new_x, byte_stride))
    {
      *x2 = new_x + 1;
      extended = 1;
    }

  return extended;
}

static int
extend_y_range (unsigned char *data,
		int box_x1, int box_y1,
		int box_x2, int box_y2,
		int x1, int x2, int *y,
		int byte_stride)
{
  int extended = 0;
  int found_set;
  int yy, y2;

  while (*y < box_y2)
    {
      found_set = 0;

      y2 = *y + EXTEND_Y_FUZZ;
      if (y2 > box_y2)
	y2 = box_y2;

      for (yy = y2; yy > *y + 1; yy--)
	{
	  if (is_any_x_set (data, box_x1, box_x2, x1, x2, yy - 1, NULL, byte_stride))
	    {
	      found_set = 1;
	      break;
	    }
	}
      if (!found_set)
	break;
      *y = yy;
      extended = 1;
    }

  return extended;
}


static void
rgba_find_rects_extents (unsigned char *data,
			 int box_x1, int box_y1,
			 int box_x2, int box_y2,
			 int x, int y,
			 BroadwayBox *rect,
			 int byte_stride)
{
  int x1, x2, y1, y2, yy;
  int extended;

  x1 = x;
  x2 = x + 1;
  y1 = y;
  y2 = y + 1;

  do
    {
      /* Expand maximally for all known rows */
      do
	{
	  extended = 0;

	  for (yy = y1; yy < y2; yy++)
	    extended |= extend_x_range (data,
					box_x1, box_y1,
					box_x2, box_y2,
					&x1, &x2, yy,
					byte_stride);
	}
      while (extended);
    }
  while (extend_y_range(data,
			box_x1, box_y1,
			box_x2, box_y2,
			x1, x2, &y2,
			byte_stride));

  rect->x1 = x1;
  rect->x2 = x2;
  rect->y1 = y1;
  rect->y2 = y2;
}

static void
rgba_find_rects_sub (unsigned char *data,
		     int box_x1, int box_y1,
		     int box_x2, int box_y2,
		     int byte_stride,
		     BroadwayBox **rects,
		     int *n_rects, int *alloc_rects)
{
  guint32 *line;
  BroadwayBox rect;
  int x, y;

  if (box_x1 == box_x2 || box_y1 == box_y2)
    return;

  for (y = box_y1; y < box_y2; y++)
    {
      line = (guint32 *)(data + y * byte_stride + box_x1 * 4);

      for (x = box_x1; x < box_x2; x++)
	{
	  if (*line != 0)
	    {
	      rgba_find_rects_extents (data,
				       box_x1, box_y1, box_x2, box_y2,
				       x, y, &rect, byte_stride);
	      if (*n_rects == *alloc_rects)
		{
		  (*alloc_rects) *= 2;
		  *rects = g_renew (BroadwayBox, *rects, *alloc_rects);
		}
	      (*rects)[*n_rects] = rect;
	      (*n_rects)++;
	      rgba_find_rects_sub (data,
				   box_x1, rect.y1,
				   rect.x1, rect.y2,
				   byte_stride,
				   rects, n_rects, alloc_rects);
	      rgba_find_rects_sub (data,
				   rect.x2, rect.y1,
				   box_x2, rect.y2,
				   byte_stride,
				   rects, n_rects, alloc_rects);
	      rgba_find_rects_sub (data,
				   box_x1, rect.y2,
				   box_x2, box_y2,
				   byte_stride,
				   rects, n_rects, alloc_rects);
	      return;
	    }
	  line++;
	}
    }
}

static BroadwayBox *
rgba_find_rects (unsigned char *data,
		 int w, int h, int byte_stride,
		 int *n_rects)
{
  BroadwayBox *rects;
  int alloc_rects;

  alloc_rects = 20;
  rects = g_new (BroadwayBox, alloc_rects);

  *n_rects = 0;
  rgba_find_rects_sub (data,
		       0, 0, w, h, byte_stride,
		       &rects, n_rects, &alloc_rects);

  return rects;
}

void
broadway_output_put_rgba (BroadwayOutput *output,  int id, int x, int y,
			  int w, int h, int byte_stride, void *data)
{
  BroadwayBox *rects;
  int p, i, n_rects;

  rects = rgba_find_rects (data, w, h, byte_stride, &n_rects);

  for (i = 0; i < n_rects; i++)
    {
      gsize url_len, buf_size;
      char *buf, *url;
      guint8 *subdata;

      subdata = (guint8 *)data + rects[i].x1 * 4 + rects[i].y1 * byte_stride;
      url = to_png_rgba (rects[i].x2 - rects[i].x1,
			 rects[i].y2 - rects[i].y1,
			 byte_stride, (guint32*)subdata);

      url_len = strlen (url);
      buf_size = HEADER_LEN + 15 + url_len;
      buf = g_malloc (buf_size);


      p = write_header (output, buf, 'i');
      append_uint16 (id, buf, &p);
      append_uint16 (x + rects[i].x1, buf, &p);
      append_uint16 (y + rects[i].y1, buf, &p);

      append_uint32 (url_len, buf, &p);
      g_assert (p == HEADER_LEN + 15);
      strncpy (buf + p, url, url_len);

      broadway_output_sendmsg (output, buf, buf_size);

      free (url);
      g_free (buf);
    }

  free (rects);
}

void
broadway_output_surface_flush (BroadwayOutput *output,
			       int             id)
{
  char buf[HEADER_LEN + 3];
  int p;

  p = write_header (output, buf, 'f');
  append_uint16 (id, buf, &p);

  g_assert (p == sizeof (buf));

  broadway_output_sendmsg (output, buf, sizeof (buf));
}