Repository navigation
Expand file tree
/
Copy pathevdi_source.c
More file actions
1705 lines (1597 loc) · 59.2 KB
/
Copy pathevdi_source.c
File metadata and controls
1705 lines (1597 loc) · 59.2 KB
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "evdi_source.h"
#include "log.h"
#include "protocol.h"
#include "time.h"
#include "transport.h"
#include "usb.h"
#include <errno.h>
#include <evdi_lib.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <time.h>
#include <unistd.h>
#if (defined(__x86_64__) || defined(__i386__)) && defined(__GNUC__)
#include <immintrin.h>
#define SMID_HAVE_X86_TARGET_INTRINSICS 1
#endif
enum {
SMID_EVDI_MAX_STREAMS = 2,
SMID_EVDI_BUFFER_ID = 1,
SMID_EVDI_MAX_RECTS = 64,
SMID_CURSOR_IMAGE_LEN = 0x4060u,
SMID_CURSOR_IMAGE_BYTES = 0x4000u,
SMID_CURSOR_IMAGE_OFF = 0x60u,
SMID_CURSOR_SIDE = 64u,
SMID_CURSOR_STRIDE = 0x100u,
SMID_CURSOR_CACHE_MAX = 0x190u,
SMID_CURSOR_CACHE_REWIND = 0x64u,
};
typedef struct smid_frame_hash (*smid_frame_hash_fn)(const uint8_t *data, size_t len);
struct smid_frame_hash_impl {
smid_frame_hash_fn fn;
};
struct smid_cursor_cache_entry {
bool valid;
uint32_t shape;
uint32_t hash;
uint32_t width;
uint32_t height;
uint32_t hot_x;
uint32_t hot_y;
uint32_t pixel_format;
uint32_t stride;
uint8_t image[SMID_CURSOR_IMAGE_BYTES];
};
struct smid_evdi_stream {
struct smid_evdi_source *source;
int index;
evdi_handle handle;
pthread_t thread;
pthread_rwlock_t buffer_lock;
bool buffer_lock_initialized;
bool thread_started;
atomic_bool stop;
int wake_pipe[2];
struct evdi_event_context events;
struct evdi_rect rects[SMID_EVDI_MAX_RECTS];
atomic_bool dpms_on;
atomic_bool mode_seen;
atomic_bool requested;
int width;
int height;
int stride;
size_t buffer_size;
uint8_t *buffer;
struct smid_frame_hash last_band_hash[2];
bool have_band_hash[2];
uint8_t cursor_image_pkt[SMID_CURSOR_IMAGE_LEN];
uint8_t cursor_select_pkt[48];
uint8_t cursor_state_pkt[44];
struct smid_tx_result cursor_upload_result;
uint32_t cursor_shape_slot;
uint32_t cursor_upload_shape;
uint64_t cursor_upload_code_before;
uint64_t cursor_upload_start_us;
int32_t cursor_x;
int32_t cursor_y;
bool cursor_visible;
bool cursor_upload_result_ready;
atomic_bool cursor_upload_inflight;
bool cursor_upload_tx_done;
bool pending_cursor_set;
bool pending_cursor_enabled;
uint32_t pending_cursor_width;
uint32_t pending_cursor_height;
int32_t pending_cursor_hot_x;
int32_t pending_cursor_hot_y;
uint32_t pending_cursor_pixel_format;
uint32_t pending_cursor_stride;
uint8_t pending_cursor_image[SMID_CURSOR_IMAGE_BYTES];
bool pending_cursor_move;
int32_t pending_cursor_x;
int32_t pending_cursor_y;
atomic_uchar dirty_tiles;
atomic_uchar blank_tiles;
atomic_uint_fast64_t stat_dirty;
};
struct smid_evdi_source {
int streams;
bool cursor_events;
smid_frame_hash_fn frame_hash_fn;
struct smid_usb *usb;
pthread_mutex_t usb_lock;
pthread_mutex_t damage_lock;
pthread_cond_t damage_cond;
pthread_mutex_t cursor_lock;
pthread_mutex_t cursor_event_lock;
pthread_cond_t cursor_event_cond;
pthread_t cursor_thread;
bool cursor_thread_started;
atomic_bool cursor_stop;
atomic_bool device_power_on;
uint64_t damage_generation;
int cursor_selected_stream;
uint32_t cursor_selected_shape[SMID_EVDI_MAX_STREAMS];
uint32_t cursor_next_shape;
struct smid_cursor_cache_entry cursor_cache[SMID_CURSOR_CACHE_MAX + 1u];
struct smid_evdi_stream stream[SMID_EVDI_MAX_STREAMS];
};
static bool evdi_request_next(struct smid_evdi_stream *s);
static void stream_stop(struct smid_evdi_stream *s);
static void signal_damage_locked(struct smid_evdi_source *src) {
src->damage_generation++;
pthread_cond_broadcast(&src->damage_cond);
}
static void signal_damage(struct smid_evdi_source *src) {
pthread_mutex_lock(&src->damage_lock);
signal_damage_locked(src);
pthread_mutex_unlock(&src->damage_lock);
}
static struct smid_usb *source_usb_locked(struct smid_evdi_source *src) {
pthread_mutex_lock(&src->usb_lock);
struct smid_usb *usb = src->usb;
pthread_mutex_unlock(&src->usb_lock);
return usb;
}
static void make_device_power_packet(uint8_t packet[44], bool on) {
memset(packet, 0, 44);
memcpy(packet, SMID_MAGIC, 12);
smid_put32(packet + 12, 44);
smid_put32(packet + 16, 6);
packet[20] = 0x47;
packet[21] = on ? 1u : 0u;
}
static int submit_device_power(struct smid_evdi_source *src, bool on) {
struct smid_usb *usb = source_usb_locked(src);
if (!usb) {
return 0;
}
uint8_t packet[44];
make_device_power_packet(packet, on);
struct smid_tx_item item = {
.priority = SMID_TX_PRIO_CONTROL,
.endpoint = SMID_EP_BULK_OUT,
.xfer = SMID_USB_XFER_BULK,
.timeout_ms = 1000,
.data = packet,
.len = sizeof(packet),
.name = on ? "display-power-on" : "display-power-off",
};
return smid_transport_submit(&usb->tx, &item);
}
static void update_device_power(struct smid_evdi_source *src) {
bool any_on = false;
for (int i = 0; i < src->streams; i++) {
any_on = any_on ||
atomic_load_explicit(&src->stream[i].dpms_on, memory_order_acquire);
}
bool old = atomic_exchange_explicit(&src->device_power_on, any_on,
memory_order_acq_rel);
if (old != any_on && submit_device_power(src, any_on)) {
smid_logf("evdi: display power %s enqueue failed\n", any_on ? "on" : "off");
}
}
void smid_evdi_source_set_usb(struct smid_evdi_source *src, struct smid_usb *usb) {
if (!src) {
return;
}
pthread_mutex_lock(&src->usb_lock);
src->usb = usb;
pthread_mutex_unlock(&src->usb_lock);
if (usb) {
atomic_store_explicit(&src->device_power_on, true, memory_order_release);
update_device_power(src);
}
pthread_mutex_lock(&src->cursor_event_lock);
pthread_cond_broadcast(&src->cursor_event_cond);
pthread_mutex_unlock(&src->cursor_event_lock);
}
static uint64_t atomic_take_u64(atomic_uint_fast64_t *v) {
return atomic_exchange_explicit(v, 0, memory_order_relaxed);
}
static uint32_t fnv1a32_update(uint32_t h, const void *data, size_t len) {
const uint8_t *p = data;
for (size_t i = 0; i < len; i++) {
h ^= p[i];
h *= 16777619u;
}
return h;
}
static uint64_t rotl64(uint64_t v, unsigned n) {
return (v << n) | (v >> (64u - n));
}
static uint64_t mix64(uint64_t v) {
v ^= v >> 33;
v *= 0xff51afd7ed558ccdull;
v ^= v >> 33;
v *= 0xc4ceb9fe1a85ec53ull;
v ^= v >> 33;
return v;
}
static struct smid_frame_hash frame_hash_scalar(const uint8_t *data, size_t len) {
uint64_t sum = 0x9e3779b97f4a7c15ull ^ (uint64_t)len;
uint64_t sum2 = 0x632be59bd9b4e019ull + ((uint64_t)len << 1);
uint64_t x = 0x94d049bb133111ebull ^ ((uint64_t)len << 32);
size_t i = 0;
for (; i + sizeof(uint64_t) <= len; i += sizeof(uint64_t)) {
uint64_t v = 0;
memcpy(&v, data + i, sizeof(v));
sum += v;
sum2 += sum;
x ^= rotl64(v, 17);
}
uint64_t tail = 0;
unsigned shift = 0;
for (; i < len; i++, shift += 8) {
tail |= (uint64_t)data[i] << shift;
}
sum += tail;
sum2 += sum;
x ^= rotl64(tail, 17);
struct smid_frame_hash out = {
.a = mix64(sum ^ rotl64(sum2, 23) ^ x),
.b = mix64(sum2 ^ rotl64(x, 41) ^ ((uint64_t)len * 0x9e3779b185ebca87ull)),
};
return out;
}
#if SMID_HAVE_X86_TARGET_INTRINSICS
__attribute__((target("avx2"), always_inline))
static inline __m256i loadu256_once(const void *p) {
__m256i v = _mm256_loadu_si256((const __m256i *)p);
__asm__("" : "+x"(v));
return v;
}
__attribute__((target("avx2"), always_inline))
static inline uint64_t hsum256_epi64(__m256i v) {
__m128i lo = _mm256_castsi256_si128(v);
__m128i hi = _mm256_extracti128_si256(v, 1);
__m128i s = _mm_add_epi64(lo, hi);
s = _mm_add_epi64(s, _mm_unpackhi_epi64(s, s));
return (uint64_t)_mm_cvtsi128_si64(s);
}
__attribute__((target("avx2"), always_inline))
static inline uint64_t hxor256_epi64(__m256i v) {
__m128i lo = _mm256_castsi256_si128(v);
__m128i hi = _mm256_extracti128_si256(v, 1);
__m128i x = _mm_xor_si128(lo, hi);
x = _mm_xor_si128(x, _mm_unpackhi_epi64(x, x));
return (uint64_t)_mm_cvtsi128_si64(x);
}
__attribute__((target("avx2")))
static struct smid_frame_hash frame_hash_avx2(const uint8_t *data, size_t len) {
const __m256i c0 = _mm256_set_epi64x(0x94d049bb133111ebull, 0x632be59bd9b4e019ull,
0xbf58476d1ce4e5b9ull, 0x9e3779b97f4a7c15ull);
const __m256i c1 = _mm256_set_epi64x(0x8ebc6af09c88c6e3ull, 0x589965cc75374cc3ull,
0x1d8e4e27c47d124full, 0xc2b2ae3d27d4eb4full);
const __m256i c2 = _mm256_set_epi64x(0x165667b19e3779f9ull, 0xd6e8feb86659fd93ull,
0xa5a3564e27f886c7ull, 0x85ebca77c2b2ae63ull);
const __m256i c3 = _mm256_set_epi64x(0x27d4eb2f165667c5ull, 0x9e3779b185ebca87ull,
0xc4ceb9fe1a85ec53ull, 0xff51afd7ed558ccdull);
__m256i sum0 = _mm256_xor_si256(c0, _mm256_set1_epi64x((long long)len));
__m256i sum1 = _mm256_xor_si256(c1, _mm256_set1_epi64x((long long)((uint64_t)len << 1)));
__m256i sum2 = _mm256_xor_si256(c2, _mm256_set1_epi64x((long long)((uint64_t)len << 2)));
__m256i sum3 = _mm256_xor_si256(c3, _mm256_set1_epi64x((long long)((uint64_t)len << 3)));
__m256i xor0 = c3;
__m256i xor1 = c2;
__m256i xor2 = c1;
__m256i xor3 = c0;
xor0 = _mm256_xor_si256(xor0, _mm256_set1_epi64x((long long)((uint64_t)len << 32)));
size_t i = 0;
for (; i + 256u <= len; i += 256u) {
__m256i v0 = loadu256_once(data + i + 0u);
__m256i v1 = loadu256_once(data + i + 32u);
__m256i v2 = loadu256_once(data + i + 64u);
__m256i v3 = loadu256_once(data + i + 96u);
__m256i v4 = loadu256_once(data + i + 128u);
__m256i v5 = loadu256_once(data + i + 160u);
__m256i v6 = loadu256_once(data + i + 192u);
__m256i v7 = loadu256_once(data + i + 224u);
sum0 = _mm256_add_epi64(sum0, v0);
sum1 = _mm256_add_epi64(sum1, v1);
sum2 = _mm256_add_epi64(sum2, v2);
sum3 = _mm256_add_epi64(sum3, v3);
sum0 = _mm256_add_epi64(sum0, v4);
sum1 = _mm256_add_epi64(sum1, v5);
sum2 = _mm256_add_epi64(sum2, v6);
sum3 = _mm256_add_epi64(sum3, v7);
xor0 = _mm256_xor_si256(xor0, v0);
xor1 = _mm256_xor_si256(xor1, v1);
xor2 = _mm256_xor_si256(xor2, v2);
xor3 = _mm256_xor_si256(xor3, v3);
xor0 = _mm256_xor_si256(xor0, v4);
xor1 = _mm256_xor_si256(xor1, v5);
xor2 = _mm256_xor_si256(xor2, v6);
xor3 = _mm256_xor_si256(xor3, v7);
}
for (; i + 32u <= len; i += 32u) {
__m256i v = loadu256_once(data + i);
sum0 = _mm256_add_epi64(sum0, v);
xor0 = _mm256_xor_si256(xor0, v);
}
__m256i sum01 = _mm256_add_epi64(sum0, sum1);
__m256i sum23 = _mm256_add_epi64(sum2, sum3);
__m256i xor01 = _mm256_xor_si256(xor0, xor1);
__m256i xor23 = _mm256_xor_si256(xor2, xor3);
uint64_t vec_sum = hsum256_epi64(_mm256_add_epi64(sum01, sum23));
uint64_t vec_x = hxor256_epi64(_mm256_xor_si256(xor01, xor23));
uint64_t a = mix64(vec_sum ^ rotl64(vec_x, 17) ^
((uint64_t)len * 0x9e3779b185ebca87ull));
uint64_t b = mix64(vec_x ^ rotl64(vec_sum, 29) ^
((uint64_t)len * 0xc2b2ae3d27d4eb4full));
uint64_t tail_sum = 0;
uint64_t tail_sum2 = 0;
uint64_t tail_x = 0;
for (; i + sizeof(uint64_t) <= len; i += sizeof(uint64_t)) {
uint64_t v = 0;
memcpy(&v, data + i, sizeof(v));
tail_sum += v;
tail_sum2 += tail_sum;
tail_x ^= rotl64(v, 17);
}
uint64_t tail = 0;
unsigned shift = 0;
for (; i < len; i++, shift += 8) {
tail |= (uint64_t)data[i] << shift;
}
tail_sum += tail;
tail_sum2 += tail_sum;
tail_x ^= rotl64(tail, 17);
struct smid_frame_hash out = {
.a = mix64(a ^ tail_sum ^ rotl64(tail_sum2, 23) ^ tail_x),
.b = mix64(b ^ tail_sum2 ^ rotl64(tail_x, 41)),
};
return out;
}
#endif
static struct smid_frame_hash_impl frame_hash_resolve(void) {
#if SMID_HAVE_X86_TARGET_INTRINSICS
__builtin_cpu_init();
if (__builtin_cpu_supports("avx2")) {
return (struct smid_frame_hash_impl){
.fn = frame_hash_avx2,
};
}
#endif
return (struct smid_frame_hash_impl){
.fn = frame_hash_scalar,
};
}
static bool frame_hash_equal(struct smid_frame_hash a, struct smid_frame_hash b) {
return a.a == b.a && a.b == b.b;
}
static uint32_t cursor_image_hash(const struct evdi_cursor_set *cursor_set,
const uint8_t *image) {
uint32_t h = 2166136261u;
h = fnv1a32_update(h, &cursor_set->width, sizeof(cursor_set->width));
h = fnv1a32_update(h, &cursor_set->height, sizeof(cursor_set->height));
h = fnv1a32_update(h, &cursor_set->hot_x, sizeof(cursor_set->hot_x));
h = fnv1a32_update(h, &cursor_set->hot_y, sizeof(cursor_set->hot_y));
h = fnv1a32_update(h, &cursor_set->pixel_format, sizeof(cursor_set->pixel_format));
h = fnv1a32_update(h, &cursor_set->stride, sizeof(cursor_set->stride));
return fnv1a32_update(h, image, SMID_CURSOR_IMAGE_BYTES);
}
static bool cursor_cache_matches(const struct smid_cursor_cache_entry *e,
const struct evdi_cursor_set *cursor_set,
const uint8_t *image, uint32_t hash) {
return e->valid &&
e->hash == hash &&
e->width == cursor_set->width &&
e->height == cursor_set->height &&
e->hot_x == (uint32_t)cursor_set->hot_x &&
e->hot_y == (uint32_t)cursor_set->hot_y &&
e->pixel_format == cursor_set->pixel_format &&
e->stride == cursor_set->stride &&
!memcmp(e->image, image, SMID_CURSOR_IMAGE_BYTES);
}
static bool cursor_cache_matches_source(const struct smid_cursor_cache_entry *e,
const struct evdi_cursor_set *cursor_set) {
if (!e->valid ||
e->width != cursor_set->width ||
e->height != cursor_set->height ||
e->hot_x != (uint32_t)cursor_set->hot_x ||
e->hot_y != (uint32_t)cursor_set->hot_y ||
e->pixel_format != cursor_set->pixel_format ||
e->stride != cursor_set->stride ||
!cursor_set->buffer) {
return false;
}
const uint8_t *src = (const uint8_t *)cursor_set->buffer;
uint32_t src_stride = cursor_set->stride ? cursor_set->stride : cursor_set->width * 4u;
uint32_t copy_row = cursor_set->width * 4u;
if (copy_row > src_stride) {
copy_row = src_stride;
}
if (copy_row > SMID_CURSOR_STRIDE) {
copy_row = SMID_CURSOR_STRIDE;
}
for (uint32_t y = 0; y < cursor_set->height; y++) {
if (memcmp(e->image + (size_t)y * SMID_CURSOR_STRIDE,
src + (size_t)y * src_stride, copy_row)) {
return false;
}
}
return true;
}
static void make_1080p_edid(uint8_t edid[128], int index) {
static const uint8_t tmpl[128] = {
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
0x4f, 0x53, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00,
0x01, 0x24, 0x01, 0x03, 0x80, 0x34, 0x1d, 0x78,
0x0a, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26,
0x0f, 0x50, 0x54, 0x21, 0x08, 0x00, 0x81, 0x80,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x3a,
0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
0x45, 0x00, 0x09, 0x25, 0x21, 0x00, 0x00, 0x1e,
0x00, 0x00, 0x00, 0xfd, 0x00, 0x32, 0x4b, 0x1e,
0x53, 0x11, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x53,
0x4d, 0x49, 0x20, 0x56, 0x69, 0x72, 0x74, 0x20,
0x41, 0x0a, 0x20, 0x20, 0x00, 0x00, 0x00, 0xff,
0x00, 0x53, 0x4d, 0x49, 0x45, 0x56, 0x44, 0x49,
0x30, 0x30, 0x31, 0x0a, 0x20, 0x20, 0x00, 0x00,
};
memcpy(edid, tmpl, sizeof(tmpl));
edid[12] = (uint8_t)(1 + index);
edid[104] = (uint8_t)('A' + index);
edid[122] = (uint8_t)('1' + index);
edid[127] = 0;
uint8_t sum = 0;
for (int i = 0; i < 127; i++) {
sum = (uint8_t)(sum + edid[i]);
}
edid[127] = (uint8_t)(0u - sum);
}
static const uint8_t *band_data_locked(struct smid_evdi_stream *s, int tile,
size_t *bytes_out) {
int y0 = tile ? 0 : SMID_LOWER_TILE_Y0;
if (bytes_out) {
*bytes_out = 0;
}
if (!s->buffer || s->stride <= 0 || y0 < 0 || y0 >= s->height) {
return NULL;
}
int rows = SMID_TILE_H;
if (y0 + rows > s->height) {
rows = s->height - y0;
}
if (rows <= 0) {
return NULL;
}
if (bytes_out) {
*bytes_out = (size_t)rows * (size_t)s->stride;
}
return s->buffer + (size_t)y0 * (size_t)s->stride;
}
static uint8_t screen_dirty_mask_locked(struct smid_evdi_stream *s) {
if (!s->buffer || s->width <= 0 || s->height <= 0) {
return 0;
}
uint8_t dirty = 0;
for (int tile = 0; tile < 2; tile++) {
size_t band_bytes = 0;
const uint8_t *band = band_data_locked(s, tile, &band_bytes);
if (!band || !band_bytes) {
continue;
}
struct smid_frame_hash h = s->source->frame_hash_fn(band, band_bytes);
if (!s->have_band_hash[tile] ||
!frame_hash_equal(h, s->last_band_hash[tile])) {
dirty |= (uint8_t)(1u << tile);
}
s->last_band_hash[tile] = h;
s->have_band_hash[tile] = true;
}
return dirty;
}
static int evdi_alloc_buffer(struct smid_evdi_stream *s, struct evdi_mode mode) {
int stride = ((mode.width + 63) & ~63) * 4;
size_t buffer_size = (size_t)stride * (size_t)mode.height;
uint8_t *buffer = NULL;
if (posix_memalign((void **)&buffer, 4096, buffer_size)) {
buffer = NULL;
}
if (buffer) {
memset(buffer, 0, buffer_size);
}
if (!buffer) {
free(buffer);
smid_logf("evdi%d: framebuffer allocation failed\n", s->index);
return -1;
}
pthread_rwlock_wrlock(&s->buffer_lock);
if (s->buffer) {
evdi_unregister_buffer(s->handle, SMID_EVDI_BUFFER_ID);
free(s->buffer);
}
s->width = mode.width;
s->height = mode.height;
s->stride = stride;
s->buffer_size = buffer_size;
s->buffer = buffer;
s->last_band_hash[0] = (struct smid_frame_hash){0, 0};
s->last_band_hash[1] = (struct smid_frame_hash){0, 0};
s->have_band_hash[0] = false;
s->have_band_hash[1] = false;
struct evdi_buffer b = {
.id = SMID_EVDI_BUFFER_ID,
.buffer = s->buffer,
.width = mode.width,
.height = mode.height,
.stride = s->stride,
.rects = s->rects,
.rect_count = SMID_EVDI_MAX_RECTS,
};
evdi_register_buffer(s->handle, b);
atomic_store_explicit(&s->requested, false, memory_order_release);
atomic_store_explicit(&s->dirty_tiles, 0x3, memory_order_release);
atomic_store_explicit(&s->mode_seen, true, memory_order_release);
pthread_rwlock_unlock(&s->buffer_lock);
smid_logf("evdi%d: mode %dx%d@%d bpp=%d fmt=0x%x stride=%d\n",
s->index, mode.width, mode.height, mode.refresh_rate,
mode.bits_per_pixel, mode.pixel_format, s->stride);
return 0;
}
static void evdi_mode_handler(struct evdi_mode mode, void *user_data) {
(void)evdi_alloc_buffer((struct smid_evdi_stream *)user_data, mode);
}
static void cursor_init_stream(struct smid_evdi_stream *s) {
uint8_t selector = s->index ? 0x10 : 0x00;
memset(s->cursor_image_pkt, 0, sizeof(s->cursor_image_pkt));
memcpy(s->cursor_image_pkt, SMID_MAGIC, 12);
smid_put32(s->cursor_image_pkt + 12, SMID_CURSOR_IMAGE_LEN);
smid_put32(s->cursor_image_pkt + 16, 6u | ((uint32_t)selector << 24));
s->cursor_image_pkt[20] = 0x42;
s->cursor_image_pkt[21] = 0x30;
s->cursor_image_pkt[22] = 0x40;
smid_put32(s->cursor_image_pkt + 48, 2);
smid_put32(s->cursor_image_pkt + 52, SMID_CURSOR_SIDE);
smid_put32(s->cursor_image_pkt + 56, SMID_CURSOR_SIDE);
smid_put32(s->cursor_image_pkt + 60, SMID_CURSOR_STRIDE);
smid_put32(s->cursor_image_pkt + 88, SMID_CURSOR_IMAGE_BYTES);
memset(s->cursor_select_pkt, 0, sizeof(s->cursor_select_pkt));
memcpy(s->cursor_select_pkt, SMID_MAGIC, 12);
smid_put32(s->cursor_select_pkt + 12, sizeof(s->cursor_select_pkt));
smid_put32(s->cursor_select_pkt + 16, 0x00400039u);
s->cursor_select_pkt[21] = (uint8_t)(s->index & 1);
s->cursor_select_pkt[26] = 1;
memset(s->cursor_state_pkt, 0, sizeof(s->cursor_state_pkt));
memcpy(s->cursor_state_pkt, SMID_MAGIC, 12);
smid_put32(s->cursor_state_pkt + 12, sizeof(s->cursor_state_pkt));
s->cursor_state_pkt[16] = 0x3a;
smid_put32(s->cursor_state_pkt + 17, 0x0c);
s->cursor_state_pkt[21] = 0;
s->cursor_shape_slot = UINT32_MAX;
s->cursor_upload_shape = UINT32_MAX;
atomic_init(&s->cursor_upload_inflight, false);
smid_tx_result_init(&s->cursor_upload_result);
s->cursor_upload_result_ready = true;
}
static int cursor_submit_inline(struct smid_evdi_source *src, const uint8_t *data, uint32_t len,
enum smid_tx_priority priority, uint8_t endpoint,
enum smid_usb_xfer xfer, uint32_t timeout_ms,
const char *name) {
if (len > SMID_TX_INLINE_CAP) {
return -1;
}
pthread_mutex_lock(&src->usb_lock);
struct smid_usb *usb = src->usb;
if (!usb) {
pthread_mutex_unlock(&src->usb_lock);
return -1;
}
struct smid_tx_item item = {
.priority = priority,
.endpoint = endpoint,
.xfer = xfer,
.timeout_ms = timeout_ms,
.data = data,
.len = len,
.result = NULL,
.name = name,
};
int rc = smid_transport_submit(&usb->tx, &item);
pthread_mutex_unlock(&src->usb_lock);
return rc;
}
static int cursor_select_locked(struct smid_evdi_stream *s) {
struct smid_evdi_source *src = s->source;
int target = s->index;
uint32_t shape = s->cursor_shape_slot;
if (shape == UINT32_MAX) {
return 0;
}
if (atomic_load_explicit(&s->cursor_upload_inflight, memory_order_acquire) &&
s->cursor_upload_shape == shape) {
return 0;
}
if (src->cursor_selected_stream == target &&
src->cursor_selected_shape[target & 1] == shape) {
return 0;
}
s->cursor_select_pkt[21] = (uint8_t)(target & 1);
smid_put32(s->cursor_select_pkt + 22, shape);
s->cursor_select_pkt[26] = 1;
int rc = cursor_submit_inline(src, s->cursor_select_pkt, sizeof(s->cursor_select_pkt),
SMID_TX_PRIO_CURSOR, SMID_EP_INTR_OUT, SMID_USB_XFER_INTR,
250, "cursor-select");
if (!rc) {
src->cursor_selected_stream = target;
src->cursor_selected_shape[target & 1] = shape;
}
return rc;
}
static int cursor_submit_state_locked(struct smid_evdi_stream *s, int32_t x, int32_t y,
bool enabled, bool force) {
struct smid_evdi_source *src = s->source;
bool same_state = s->cursor_x == x && s->cursor_y == y && s->cursor_visible == enabled;
int rc = 0;
if (enabled) {
rc = cursor_select_locked(s);
}
if (!rc && (force || !same_state)) {
int target = s->index;
s->cursor_x = x;
s->cursor_y = y;
s->cursor_visible = enabled;
s->cursor_state_pkt[21] = (uint8_t)(target & 1);
s->cursor_state_pkt[32] = enabled ? 1 : 0;
smid_put32(s->cursor_state_pkt + 36, (uint32_t)x);
smid_put32(s->cursor_state_pkt + 40, (uint32_t)y);
rc = cursor_submit_inline(src, s->cursor_state_pkt, sizeof(s->cursor_state_pkt),
SMID_TX_PRIO_CURSOR, SMID_EP_INTR_OUT, SMID_USB_XFER_INTR,
250, "cursor-state");
}
return rc;
}
static int cursor_send_state(struct smid_evdi_stream *s, int32_t x, int32_t y, bool enabled) {
struct smid_evdi_source *src = s->source;
pthread_mutex_lock(&src->cursor_lock);
int rc = cursor_submit_state_locked(s, x, y, enabled, false);
pthread_mutex_unlock(&src->cursor_lock);
return rc;
}
static int cursor_send_state_force(struct smid_evdi_stream *s, int32_t x, int32_t y, bool enabled) {
struct smid_evdi_source *src = s->source;
pthread_mutex_lock(&src->cursor_lock);
int rc = cursor_submit_state_locked(s, x, y, enabled, true);
pthread_mutex_unlock(&src->cursor_lock);
return rc;
}
static void cursor_upload_fail_locked(struct smid_evdi_stream *s, const char *why) {
if (s->cursor_upload_shape <= SMID_CURSOR_CACHE_MAX) {
s->source->cursor_cache[s->cursor_upload_shape].valid = false;
}
smid_logf("warn: cursor image upload failed stream=%d shape=%u %s\n",
s->index, s->cursor_upload_shape, why);
atomic_store_explicit(&s->cursor_upload_inflight, false, memory_order_release);
s->cursor_upload_tx_done = false;
s->cursor_upload_shape = UINT32_MAX;
}
static void cursor_pump_locked(struct smid_evdi_stream *s) {
if (!atomic_load_explicit(&s->cursor_upload_inflight, memory_order_acquire)) {
return;
}
if (!s->cursor_upload_tx_done) {
int rc = 0;
int transferred = 0;
if (!smid_tx_result_poll(&s->cursor_upload_result, &rc, &transferred)) {
return;
}
if (rc || transferred != (int)SMID_CURSOR_IMAGE_LEN) {
cursor_upload_fail_locked(s, "tx");
return;
}
s->cursor_upload_tx_done = true;
}
uint8_t status = 0xff;
pthread_mutex_lock(&s->source->usb_lock);
struct smid_usb *usb = s->source->usb;
bool seen = usb && smid_usb_code_seen_after(usb, 0x42, s->cursor_upload_code_before, &status);
pthread_mutex_unlock(&s->source->usb_lock);
if (!seen) {
if (smid_mono_us() - s->cursor_upload_start_us > 1000000u) {
cursor_upload_fail_locked(s, "response-timeout");
}
return;
}
if (status) {
cursor_upload_fail_locked(s, "status");
return;
}
uint32_t shape = s->cursor_upload_shape;
atomic_store_explicit(&s->cursor_upload_inflight, false, memory_order_release);
s->cursor_upload_tx_done = false;
s->cursor_upload_shape = UINT32_MAX;
if (s->cursor_shape_slot == shape) {
int rc = cursor_select_locked(s);
if (!rc && s->cursor_visible) {
(void)cursor_submit_state_locked(s, s->cursor_x, s->cursor_y, true, true);
}
}
}
static void cursor_pump_stream(struct smid_evdi_stream *s) {
struct smid_evdi_source *src = s->source;
if (!source_usb_locked(src) ||
!atomic_load_explicit(&s->cursor_upload_inflight, memory_order_acquire)) {
return;
}
pthread_mutex_lock(&src->cursor_lock);
cursor_pump_locked(s);
pthread_mutex_unlock(&src->cursor_lock);
}
static int cursor_send_image_ex(struct smid_evdi_stream *s, struct evdi_cursor_set *cursor_set,
bool force_upload) {
struct smid_evdi_source *src = s->source;
if (!cursor_set->enabled || !cursor_set->buffer ||
cursor_set->width == 0 || cursor_set->height == 0) {
return cursor_send_state(s, 0, 0, false);
}
if (cursor_set->width > SMID_CURSOR_SIDE || cursor_set->height > SMID_CURSOR_SIDE) {
smid_logf("cursor stream=%d unsupported size %ux%u\n",
s->index, cursor_set->width, cursor_set->height);
return -1;
}
pthread_mutex_lock(&src->cursor_lock);
cursor_pump_locked(s);
int target = s->index;
if (atomic_load_explicit(&s->cursor_upload_inflight, memory_order_acquire)) {
pthread_mutex_unlock(&src->cursor_lock);
return 0;
}
if (!force_upload &&
s->cursor_shape_slot <= SMID_CURSOR_CACHE_MAX &&
cursor_cache_matches_source(&src->cursor_cache[s->cursor_shape_slot], cursor_set)) {
int rc = cursor_select_locked(s);
pthread_mutex_unlock(&src->cursor_lock);
return rc;
}
uint8_t *p = s->cursor_image_pkt;
p[19] = target ? 0x10 : 0x00;
p[24] = 0;
smid_put32(p + 64, (uint32_t)cursor_set->hot_x);
smid_put32(p + 68, (uint32_t)cursor_set->hot_y);
smid_put32(p + 72, cursor_set->width);
smid_put32(p + 76, cursor_set->height);
smid_put32(p + 80, cursor_set->pixel_format);
smid_put32(p + 84, cursor_set->stride);
memset(p + SMID_CURSOR_IMAGE_OFF, 0, SMID_CURSOR_IMAGE_BYTES);
const uint8_t *src_pixels = (const uint8_t *)cursor_set->buffer;
uint32_t src_stride = cursor_set->stride ? cursor_set->stride : cursor_set->width * 4u;
uint32_t copy_row = cursor_set->width * 4u;
if (copy_row > src_stride) {
copy_row = src_stride;
}
if (copy_row > SMID_CURSOR_STRIDE) {
copy_row = SMID_CURSOR_STRIDE;
}
for (uint32_t y = 0; y < cursor_set->height; y++) {
memcpy(p + SMID_CURSOR_IMAGE_OFF + (size_t)y * SMID_CURSOR_STRIDE,
src_pixels + (size_t)y * src_stride, copy_row);
}
uint8_t *image = p + SMID_CURSOR_IMAGE_OFF;
uint32_t hash = cursor_image_hash(cursor_set, image);
uint32_t shape = UINT32_MAX;
bool cached = false;
if (!force_upload) {
for (uint32_t i = 0; i <= SMID_CURSOR_CACHE_MAX; i++) {
if (cursor_cache_matches(&src->cursor_cache[i], cursor_set, image, hash)) {
shape = i;
cached = true;
break;
}
}
}
if (!cached) {
if (src->cursor_next_shape > SMID_CURSOR_CACHE_MAX) {
src->cursor_next_shape -= SMID_CURSOR_CACHE_REWIND;
}
shape = src->cursor_next_shape++;
if (shape > SMID_CURSOR_CACHE_MAX) {
shape = SMID_CURSOR_CACHE_MAX;
}
struct smid_cursor_cache_entry *e = &src->cursor_cache[shape];
e->valid = true;
e->shape = shape;
e->hash = hash;
e->width = cursor_set->width;
e->height = cursor_set->height;
e->hot_x = (uint32_t)cursor_set->hot_x;
e->hot_y = (uint32_t)cursor_set->hot_y;
e->pixel_format = cursor_set->pixel_format;
e->stride = cursor_set->stride;
memcpy(e->image, image, SMID_CURSOR_IMAGE_BYTES);
}
s->cursor_shape_slot = shape;
smid_put32(p + 25, shape);
s->cursor_select_pkt[21] = (uint8_t)(target & 1);
smid_put32(s->cursor_select_pkt + 22, shape);
s->cursor_select_pkt[26] = 1;
int rc = 0;
if (!cached) {
uint64_t rx_before = 0;
uint64_t code_before = 0;
pthread_mutex_lock(&src->usb_lock);
struct smid_usb *usb = src->usb;
if (!usb) {
pthread_mutex_unlock(&src->usb_lock);
pthread_mutex_unlock(&src->cursor_lock);
return -1;
}
smid_usb_rx_snapshot(usb, 0x42, &rx_before, &code_before);
(void)rx_before;
smid_tx_result_reset(&s->cursor_upload_result);
struct smid_tx_item item = {
.priority = SMID_TX_PRIO_CURSOR,
.endpoint = SMID_EP_BULK_OUT,
.xfer = SMID_USB_XFER_BULK,
.timeout_ms = 1000,
.data = s->cursor_image_pkt,
.len = SMID_CURSOR_IMAGE_LEN,
.result = &s->cursor_upload_result,
.name = "cursor-image",
};
rc = smid_transport_submit(&usb->tx, &item);
pthread_mutex_unlock(&src->usb_lock);
if (rc) {
pthread_mutex_unlock(&src->cursor_lock);
return rc;
}
s->cursor_upload_tx_done = false;
s->cursor_upload_shape = shape;
s->cursor_upload_code_before = code_before;
s->cursor_upload_start_us = smid_mono_us();
atomic_store_explicit(&s->cursor_upload_inflight, true, memory_order_release);
pthread_mutex_unlock(&src->cursor_lock);
return 0;
}
rc = cursor_select_locked(s);
pthread_mutex_unlock(&src->cursor_lock);
return rc;
}
static int cursor_send_image(struct smid_evdi_stream *s, struct evdi_cursor_set *cursor_set) {
return cursor_send_image_ex(s, cursor_set, false);
}
static void evdi_update_handler(int buffer_id, void *user_data) {
struct smid_evdi_stream *s = user_data;
(void)buffer_id;
if (!atomic_load_explicit(&s->dpms_on, memory_order_acquire)) {
atomic_store_explicit(&s->requested, false, memory_order_release);
return;
}
int n = SMID_EVDI_MAX_RECTS;
uint8_t dirty = 0;
pthread_rwlock_wrlock(&s->buffer_lock);
evdi_grab_pixels(s->handle, s->rects, &n);
dirty = screen_dirty_mask_locked(s);
pthread_rwlock_unlock(&s->buffer_lock);
if (dirty) {
atomic_fetch_or_explicit(&s->dirty_tiles, dirty, memory_order_release);
atomic_fetch_add_explicit(&s->stat_dirty, 1, memory_order_relaxed);
signal_damage(s->source);
}
atomic_store_explicit(&s->requested, false, memory_order_release);
}
static void evdi_dpms_handler(int dpms_mode, void *user_data) {
struct smid_evdi_stream *s = user_data;
bool on = dpms_mode == 0;
bool old = atomic_exchange_explicit(&s->dpms_on, on, memory_order_acq_rel);
if (on) {
atomic_store_explicit(&s->requested, false, memory_order_release);
atomic_store_explicit(&s->blank_tiles, 0, memory_order_release);
atomic_fetch_or_explicit(&s->dirty_tiles, 0x3, memory_order_release);
signal_damage(s->source);
} else {
atomic_store_explicit(&s->requested, false, memory_order_release);
atomic_store_explicit(&s->dirty_tiles, 0, memory_order_release);
if (old) {
atomic_store_explicit(&s->blank_tiles, 0x3, memory_order_release);
signal_damage(s->source);
}
}
if (old != on) {
smid_logf("evdi%d: dpms %s\n", s->index, on ? "on" : "off");
update_device_power(s->source);
}
}
static void evdi_cursor_set_handler(struct evdi_cursor_set cursor_set, void *user_data) {
struct smid_evdi_stream *s = user_data;
struct smid_evdi_source *src = s->source;
if (src->cursor_events && source_usb_locked(src)) {
pthread_mutex_lock(&src->cursor_event_lock);
s->pending_cursor_enabled = cursor_set.enabled && cursor_set.buffer &&
cursor_set.width > 0 && cursor_set.height > 0 &&
cursor_set.width <= SMID_CURSOR_SIDE &&
cursor_set.height <= SMID_CURSOR_SIDE;
s->pending_cursor_width = cursor_set.width;
s->pending_cursor_height = cursor_set.height;
s->pending_cursor_hot_x = cursor_set.hot_x;
s->pending_cursor_hot_y = cursor_set.hot_y;
s->pending_cursor_pixel_format = cursor_set.pixel_format;
s->pending_cursor_stride = cursor_set.stride;
memset(s->pending_cursor_image, 0, sizeof(s->pending_cursor_image));
if (s->pending_cursor_enabled) {
const uint8_t *src_pixels = (const uint8_t *)cursor_set.buffer;
uint32_t src_stride = cursor_set.stride ? cursor_set.stride : cursor_set.width * 4u;
uint32_t copy_row = cursor_set.width * 4u;
if (copy_row > src_stride) {
copy_row = src_stride;
}
if (copy_row > SMID_CURSOR_STRIDE) {