aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/capture/kms.c
blob: 31d4b17dcd0ff64bd8d1f8019c399b26992b2a2a (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
#include "../../include/capture/kms.h"
#include "../../include/utils.h"
#include "../../include/color_conversion.h"
#include "../../include/cursor.h"
#include "../../include/kde_night_light.h"
#include "../../include/window/window.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm_fourcc.h>

#include <libavutil/mastering_display_metadata.h>

#define FIND_CRTC_BY_NAME_TIMEOUT_SECONDS 2.0

#define HDMI_STATIC_METADATA_TYPE1 0
#define HDMI_EOTF_SMPTE_ST2084 2
#define HDR_PEAK_LUMINANCE_FALLBACK 1000.0f
#define SDR_WHITE_LUMINANCE_REFERENCE 203.0f

#define MAX_CONNECTOR_IDS 32

typedef struct {
    uint32_t connector_ids[MAX_CONNECTOR_IDS];
    int num_connector_ids;
} MonitorId;

typedef struct {
    gsr_capture_kms_params params;

    vec2i capture_pos;
    vec2i capture_size;
    MonitorId monitor_id;

    gsr_monitor_rotation display_server_monitor_rotation;
    gsr_monitor_rotation final_monitor_rotation;

    unsigned int input_texture_id;
    unsigned int external_input_texture_id;
    unsigned int cursor_texture_id;

    bool no_modifiers_fallback;
    bool external_texture_fallback;

    struct hdr_output_metadata hdr_metadata;
    bool hdr_metadata_set;
    bool tone_mapping_message_shown;

    int drm_card_fd;
    uint32_t gamma_lut_connector_id;
    uint32_t gamma_lut_crtc_id;
    uint32_t gamma_lut_property_id;
    uint64_t gamma_lut_blob_id;

    bool night_light_message_shown;
    bool hdr_luminance_message_shown;
    float hdr_video_max_luminance;

    bool is_x11;

    //int drm_fd;
    //uint64_t prev_sequence;
    //bool damaged;

    vec2i prev_target_pos;
    vec2i prev_plane_size;

    double last_time_monitor_check;

    bool capture_is_combined_plane;
    gsr_kms_response_item *drm_fd;
    vec2i output_size;
    vec2i target_pos;
} gsr_capture_kms;

static void gsr_capture_kms_stop(gsr_capture_kms *self) {
    if(self->input_texture_id) {
        self->params.egl->glDeleteTextures(1, &self->input_texture_id);
        self->input_texture_id = 0;
    }

    if(self->external_input_texture_id) {
        self->params.egl->glDeleteTextures(1, &self->external_input_texture_id);
        self->external_input_texture_id = 0;
    }

    if(self->cursor_texture_id) {
        self->params.egl->glDeleteTextures(1, &self->cursor_texture_id);
        self->cursor_texture_id = 0;
    }

    if(self->drm_card_fd > 0) {
        close(self->drm_card_fd);
        self->drm_card_fd = -1;
    }

    // if(self->drm_fd > 0) {
    //     close(self->drm_fd);
    //     self->drm_fd = -1;
    // }
}

static int max_int(int a, int b) {
    return a > b ? a : b;
}

static void gsr_capture_kms_create_input_texture_ids(gsr_capture_kms *self) {
    self->params.egl->glGenTextures(1, &self->input_texture_id);
    self->params.egl->glBindTexture(GL_TEXTURE_2D, self->input_texture_id);
    self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    self->params.egl->glBindTexture(GL_TEXTURE_2D, 0);

    self->params.egl->glGenTextures(1, &self->external_input_texture_id);
    self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, self->external_input_texture_id);
    self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);

    const bool cursor_texture_id_is_external = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA;
    const int cursor_texture_id_target = cursor_texture_id_is_external ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;

    self->params.egl->glGenTextures(1, &self->cursor_texture_id);
    self->params.egl->glBindTexture(cursor_texture_id_target, self->cursor_texture_id);
    self->params.egl->glTexParameteri(cursor_texture_id_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    self->params.egl->glTexParameteri(cursor_texture_id_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    self->params.egl->glBindTexture(cursor_texture_id_target, 0);
}

/* TODO: On monitor reconfiguration, find monitor x, y, width and height again. Do the same for nvfbc. */

typedef struct {
    MonitorId *monitor_id;
    const char *monitor_to_capture;
    int monitor_to_capture_len;
    int num_monitors;
} MonitorCallbackUserdata;

static void monitor_callback(const gsr_monitor *monitor, void *userdata) {
    MonitorCallbackUserdata *monitor_callback_userdata = userdata;
    ++monitor_callback_userdata->num_monitors;

    if(monitor_callback_userdata->monitor_to_capture_len != monitor->name_len || memcmp(monitor_callback_userdata->monitor_to_capture, monitor->name, monitor->name_len) != 0)
        return;

    if(monitor_callback_userdata->monitor_id->num_connector_ids < MAX_CONNECTOR_IDS) {
        monitor_callback_userdata->monitor_id->connector_ids[monitor_callback_userdata->monitor_id->num_connector_ids] = monitor->connector_id;
        ++monitor_callback_userdata->monitor_id->num_connector_ids;
    }

    if(monitor_callback_userdata->monitor_id->num_connector_ids == MAX_CONNECTOR_IDS)
        fprintf(stderr, "gsr warning: reached max connector ids\n");
}

static vec2i rotate_capture_size_if_rotated(gsr_capture_kms *self, vec2i capture_size, gsr_monitor_rotation rotation) {
    if(rotation == GSR_MONITOR_ROT_90 || rotation == GSR_MONITOR_ROT_270) {
        int tmp_x = capture_size.x;
        capture_size.x = capture_size.y;
        capture_size.y = tmp_x;
    }
    return capture_size;
}

static int gsr_capture_kms_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) {
    gsr_capture_kms *self = cap->priv;

    gsr_capture_kms_create_input_texture_ids(self);
    self->drm_card_fd = open(self->params.egl->card_path, O_RDONLY);

    gsr_monitor monitor;
    self->monitor_id.num_connector_ids = 0;

    self->is_x11 = gsr_window_get_display_server(self->params.egl->window) == GSR_DISPLAY_SERVER_X11;
    const gsr_connection_type connection_type = self->is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;

    MonitorCallbackUserdata monitor_callback_userdata = {
        &self->monitor_id,
        self->params.display_to_capture, strlen(self->params.display_to_capture),
        0,
    };
    for_each_active_monitor_output(self->params.egl->window, self->params.egl->card_path, connection_type, monitor_callback, &monitor_callback_userdata);

    if(!get_monitor_by_name(self->params.egl, connection_type, self->params.display_to_capture, &monitor)) {
        fprintf(stderr, "gsr error: gsr_capture_kms_start: failed to find monitor by name \"%s\"\n", self->params.display_to_capture);
        gsr_capture_kms_stop(self);
        return -1;
    }

    monitor.name = self->params.display_to_capture;
    vec2i monitor_position = {0, 0};
    drm_monitor_get_display_server_data(self->params.egl->window, &monitor, &self->display_server_monitor_rotation, &monitor_position);

    self->capture_pos = monitor.pos;
    /* Monitor size is already rotated on x11 when the monitor is rotated, no need to apply it ourselves */
    if(self->is_x11)
        self->capture_size = monitor.size;
    else
        self->capture_size = rotate_capture_size_if_rotated(self, monitor.size, self->display_server_monitor_rotation);

    vec2i capture_size = self->capture_size;
    if(self->params.region_size.x > 0 && self->params.region_size.y > 0)
        capture_size = self->params.region_size;

    if(self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0) {
        self->params.output_resolution = scale_keep_aspect_ratio(capture_size, self->params.output_resolution);
        capture_metadata->video_size = self->params.output_resolution;
    } else {
        capture_metadata->video_size = capture_size;
    }

    self->last_time_monitor_check = clock_get_monotonic_seconds();
    return 0;
}

// TODO: This is disabled for now because we want to be able to record at a framerate higher than the monitor framerate
// static void gsr_capture_kms_tick(gsr_capture *cap) {
//     gsr_capture_kms *self = cap->priv;

//     if(self->drm_fd <= 0)
//         self->drm_fd = open(self->params.egl->card_path, O_RDONLY);

//     if(self->drm_fd <= 0)
//         return;

//     uint64_t sequence = 0;
//     uint64_t ns = 0;
//     if(drmCrtcGetSequence(self->drm_fd, 79, &sequence, &ns) != 0)
//         return;

//     if(sequence != self->prev_sequence) {
//         self->prev_sequence = sequence;
//         self->damaged = true;
//     }
// }

static gsr_kms_response_item* find_drm_by_connector_id(gsr_kms_response *kms_response, uint32_t connector_id) {
    for(int i = 0; i < kms_response->num_items; ++i) {
        if(kms_response->items[i].connector_id == connector_id && kms_response->items[i].plane_type == KMS_PLANE_TYPE_PRIMARY)
            return &kms_response->items[i];
    }
    return NULL;
}

static gsr_kms_response_item* find_largest_drm(gsr_kms_response *kms_response) {
    if(kms_response->num_items == 0)
        return NULL;

    int64_t largest_size = 0;
    gsr_kms_response_item *largest_drm = &kms_response->items[0];
    for(int i = 0; i < kms_response->num_items; ++i) {
        const int64_t size = (int64_t)kms_response->items[i].width * (int64_t)kms_response->items[i].height;
        if(size > largest_size && kms_response->items[i].plane_type == KMS_PLANE_TYPE_PRIMARY) {
            largest_size = size;
            largest_drm = &kms_response->items[i];
        }
    }
    return largest_drm;
}

static gsr_kms_response_item* find_cursor_drm(gsr_kms_response *kms_response, uint32_t connector_id) {
    gsr_kms_response_item *cursor_drm = NULL;
    for(int i = 0; i < kms_response->num_items; ++i) {
        if(kms_response->items[i].plane_type == KMS_PLANE_TYPE_CURSOR) {
            cursor_drm = &kms_response->items[i];
            if(kms_response->items[i].connector_id == connector_id)
                break;
        }
    }
    return cursor_drm;
}

static bool hdr_metadata_is_supported_format(const struct hdr_output_metadata *hdr_metadata) {
    return hdr_metadata->metadata_type == HDMI_STATIC_METADATA_TYPE1 &&
        hdr_metadata->hdmi_metadata_type1.metadata_type == HDMI_STATIC_METADATA_TYPE1 &&
        hdr_metadata->hdmi_metadata_type1.eotf == HDMI_EOTF_SMPTE_ST2084;
}

static float hdr_metadata_get_max_luminance(const struct hdr_output_metadata *hdr_metadata) {
    float max_luminance = hdr_metadata->hdmi_metadata_type1.max_cll;
    if(max_luminance <= 0.0f)
        max_luminance = hdr_metadata->hdmi_metadata_type1.max_display_mastering_luminance;
    return max_luminance;
}

static bool drm_plane_is_hdr(const gsr_kms_response_item *drm_fd) {
    return drm_fd->has_hdr_metadata && hdr_metadata_is_supported_format(&drm_fd->hdr_metadata);
}

static void gsr_capture_kms_resolve_gamma_lut_property(gsr_capture_kms *self, uint32_t connector_id) {
    self->gamma_lut_connector_id = connector_id;
    self->gamma_lut_crtc_id = 0;
    self->gamma_lut_property_id = 0;

    drmModeConnector *connector = drmModeGetConnectorCurrent(self->drm_card_fd, connector_id);
    if(!connector)
        return;

    if(connector->encoder_id) {
        drmModeEncoder *encoder = drmModeGetEncoder(self->drm_card_fd, connector->encoder_id);
        if(encoder) {
            self->gamma_lut_crtc_id = encoder->crtc_id;
            drmModeFreeEncoder(encoder);
        }
    }
    drmModeFreeConnector(connector);

    if(self->gamma_lut_crtc_id == 0)
        return;

    drmModeObjectProperties *properties = drmModeObjectGetProperties(self->drm_card_fd, self->gamma_lut_crtc_id, DRM_MODE_OBJECT_CRTC);
    if(!properties)
        return;

    for(uint32_t i = 0; i < properties->count_props; ++i) {
        drmModePropertyRes *property = drmModeGetProperty(self->drm_card_fd, properties->props[i]);
        if(!property)
            continue;

        if(strcmp(property->name, "GAMMA_LUT") == 0)
            self->gamma_lut_property_id = property->prop_id;

        drmModeFreeProperty(property);
        if(self->gamma_lut_property_id)
            break;
    }
    drmModeFreeObjectProperties(properties);
}

static uint64_t gsr_capture_kms_get_gamma_lut_blob_id(gsr_capture_kms *self) {
    if(self->gamma_lut_crtc_id == 0 || self->gamma_lut_property_id == 0)
        return 0;

    uint64_t blob_id = 0;
    drmModeObjectProperties *properties = drmModeObjectGetProperties(self->drm_card_fd, self->gamma_lut_crtc_id, DRM_MODE_OBJECT_CRTC);
    if(!properties)
        return 0;

    for(uint32_t i = 0; i < properties->count_props; ++i) {
        if(properties->props[i] == self->gamma_lut_property_id) {
            blob_id = properties->prop_values[i];
            break;
        }
    }
    drmModeFreeObjectProperties(properties);
    return blob_id;
}

static void gsr_capture_kms_update_gamma_lut(gsr_capture_kms *self, gsr_color_conversion *color_conversion) {
    if(self->drm_card_fd <= 0)
        return;

    if(self->drm_fd->connector_id != self->gamma_lut_connector_id)
        gsr_capture_kms_resolve_gamma_lut_property(self, self->drm_fd->connector_id);

    const uint64_t blob_id = gsr_capture_kms_get_gamma_lut_blob_id(self);
    if(blob_id == self->gamma_lut_blob_id)
        return;

    self->gamma_lut_blob_id = blob_id;
    if(blob_id == 0) {
        gsr_color_conversion_set_gamma_lut(color_conversion, NULL, 0);
        return;
    }

    drmModePropertyBlobRes *blob = drmModeGetPropertyBlob(self->drm_card_fd, blob_id);
    if(!blob) {
        self->gamma_lut_blob_id = 0;
        gsr_color_conversion_set_gamma_lut(color_conversion, NULL, 0);
        return;
    }

    const int num_entries = blob->length / sizeof(struct drm_color_lut);
    const struct drm_color_lut *lut = blob->data;
    float *rgb_values = malloc(num_entries * 3 * sizeof(float));
    if(rgb_values && num_entries > 0) {
        for(int i = 0; i < num_entries; ++i) {
            rgb_values[i*3 + 0] = (float)lut[i].red / 65535.0f;
            rgb_values[i*3 + 1] = (float)lut[i].green / 65535.0f;
            rgb_values[i*3 + 2] = (float)lut[i].blue / 65535.0f;
        }
        gsr_color_conversion_set_gamma_lut(color_conversion, rgb_values, num_entries);
    } else {
        self->gamma_lut_blob_id = 0;
        gsr_color_conversion_set_gamma_lut(color_conversion, NULL, 0);
    }

    free(rgb_values);
    drmModeFreePropertyBlob(blob);
}

static void gsr_capture_kms_get_monitor_luminances(gsr_capture_kms *self, const gsr_kms_response_item *drm_fd, float *sdr_white_luminance, float *hdr_peak_luminance) {
    *sdr_white_luminance = 0.0f;
    *hdr_peak_luminance = hdr_metadata_get_max_luminance(&drm_fd->hdr_metadata);

    gsr_monitor_hdr_info monitor_hdr_info;
    if(gsr_window_get_monitor_hdr_info(self->params.egl->window, self->params.display_to_capture, &monitor_hdr_info)) {
        if(monitor_hdr_info.sdr_white_luminance > 0.0f)
            *sdr_white_luminance = monitor_hdr_info.sdr_white_luminance;
        if(monitor_hdr_info.max_peak_luminance > 0.0f)
            *hdr_peak_luminance = monitor_hdr_info.max_peak_luminance;
    }

    if(*hdr_peak_luminance <= 0.0f)
        *hdr_peak_luminance = HDR_PEAK_LUMINANCE_FALLBACK;

    if(*sdr_white_luminance <= 0.0f)
        *sdr_white_luminance = SDR_WHITE_LUMINANCE_REFERENCE;

    if(*sdr_white_luminance > *hdr_peak_luminance)
        *sdr_white_luminance = *hdr_peak_luminance;
}

static void gsr_capture_kms_update_hdr_color_transforms(gsr_capture_kms *self, gsr_color_conversion *color_conversion, const gsr_kms_response_item *drm_fd) {
    const bool plane_is_hdr = drm_plane_is_hdr(drm_fd);
    gsr_color_conversion_enable_gamma_lut(color_conversion, plane_is_hdr && self->gamma_lut_blob_id != 0);

    float night_light_matrix[9];
    const bool night_light = self->params.kde_night_light && gsr_kde_night_light_get_inverse_matrix(self->params.kde_night_light, night_light_matrix);
    if(night_light && !self->night_light_message_shown) {
        self->night_light_message_shown = true;
        fprintf(stderr, "gsr info: gsr_capture_kms_update_hdr_color_transforms: night light is active, removing the night light tint from the capture\n");
    }

    float sdr_white_luminance = 0.0f;
    float hdr_peak_luminance = 0.0f;
    gsr_capture_kms_get_monitor_luminances(self, drm_fd, &sdr_white_luminance, &hdr_peak_luminance);

    /* Hdr passthrough: the compositor composites sdr white at the monitors sdr white luminance while the video should have
       sdr white at the reference luminance (203 nits), so the luminance of the video is scaled to match that */
    float luminance_scale = 1.0f;
    if(self->params.hdr && plane_is_hdr) {
        luminance_scale = SDR_WHITE_LUMINANCE_REFERENCE / sdr_white_luminance;
        if(luminance_scale != 1.0f && !self->hdr_luminance_message_shown) {
            self->hdr_luminance_message_shown = true;
            fprintf(stderr, "gsr info: gsr_capture_kms_update_hdr_color_transforms: scaling the luminance of the hdr video from sdr white at %d nits to sdr white at %d nits (hdr peak luminance: %d nits)\n",
                (int)sdr_white_luminance, (int)SDR_WHITE_LUMINANCE_REFERENCE, (int)(hdr_peak_luminance * luminance_scale));
        }
    }

    const bool convert_sdr_to_hdr = self->params.hdr && !plane_is_hdr;
    if(convert_sdr_to_hdr && !self->hdr_luminance_message_shown) {
        self->hdr_luminance_message_shown = true;
        fprintf(stderr, "gsr info: gsr_capture_kms_update_hdr_color_transforms: the monitor is in sdr mode, converting the captured sdr image to hdr (BT.709 to BT.2020 with sdr white at %d nits)\n",
            (int)SDR_WHITE_LUMINANCE_REFERENCE);
    }

    const bool tone_map_hdr_to_sdr = !self->params.hdr && plane_is_hdr;
    gsr_color_conversion_set_hdr_to_sdr_tone_mapping(color_conversion, tone_map_hdr_to_sdr, hdr_peak_luminance, sdr_white_luminance);
    if(tone_map_hdr_to_sdr && !self->tone_mapping_message_shown) {
        self->tone_mapping_message_shown = true;
        fprintf(stderr, "gsr info: gsr_capture_kms_update_hdr_color_transforms: the monitor is in hdr mode, tone mapping the hdr content to sdr (sdr white luminance: %d nits, hdr peak luminance: %d nits). Record with -k hevc_hdr or -k av1_hdr video codec option to record hdr instead\n",
            (int)sdr_white_luminance, (int)hdr_peak_luminance);
    }

    if(convert_sdr_to_hdr) {
        /* ITU-R BT.2087 */
        static const float BT709_TO_BT2020[9] = {
            0.627404f, 0.329283f, 0.043313f,
            0.069097f, 0.919540f, 0.011362f,
            0.016391f, 0.088013f, 0.895595f
        };
        float color_matrix[9];
        for(int i = 0; i < 9; ++i) {
            color_matrix[i] = BT709_TO_BT2020[i] * (SDR_WHITE_LUMINANCE_REFERENCE / 10000.0f);
            if(night_light)
                color_matrix[i] *= night_light_matrix[(i % 3) * 4];
        }
        gsr_color_conversion_set_color_matrix(color_conversion, color_matrix, GSR_COLOR_MATRIX_TRANSFER_GAMMA22_TO_PQ);
    } else if(night_light || luminance_scale != 1.0f) {
        float color_matrix[9] = {
            luminance_scale, 0.0f, 0.0f,
            0.0f, luminance_scale, 0.0f,
            0.0f, 0.0f, luminance_scale
        };
        if(night_light) {
            for(int i = 0; i < 9; ++i) {
                color_matrix[i] = night_light_matrix[i] * luminance_scale;
            }
        }
        gsr_color_conversion_set_color_matrix(color_conversion, color_matrix, plane_is_hdr ? GSR_COLOR_MATRIX_TRANSFER_PQ : GSR_COLOR_MATRIX_TRANSFER_GAMMA22);
    } else {
        gsr_color_conversion_set_color_matrix(color_conversion, NULL, GSR_COLOR_MATRIX_TRANSFER_PQ);
    }
}

static void gsr_kms_set_sdr_in_hdr_metadata(gsr_capture_kms *self) {
    if(self->hdr_metadata_set)
        return;

    self->hdr_metadata_set = true;
    memset(&self->hdr_metadata, 0, sizeof(self->hdr_metadata));
    self->hdr_metadata.metadata_type = HDMI_STATIC_METADATA_TYPE1;
    self->hdr_metadata.hdmi_metadata_type1.metadata_type = HDMI_STATIC_METADATA_TYPE1;
    self->hdr_metadata.hdmi_metadata_type1.eotf = HDMI_EOTF_SMPTE_ST2084;
    self->hdr_metadata.hdmi_metadata_type1.display_primaries[0].x = 0.64 * 50000;
    self->hdr_metadata.hdmi_metadata_type1.display_primaries[0].y = 0.33 * 50000;
    self->hdr_metadata.hdmi_metadata_type1.display_primaries[1].x = 0.30 * 50000;
    self->hdr_metadata.hdmi_metadata_type1.display_primaries[1].y = 0.60 * 50000;
    self->hdr_metadata.hdmi_metadata_type1.display_primaries[2].x = 0.15 * 50000;
    self->hdr_metadata.hdmi_metadata_type1.display_primaries[2].y = 0.06 * 50000;
    self->hdr_metadata.hdmi_metadata_type1.white_point.x = 0.3127 * 50000;
    self->hdr_metadata.hdmi_metadata_type1.white_point.y = 0.3290 * 50000;
    self->hdr_metadata.hdmi_metadata_type1.max_display_mastering_luminance = SDR_WHITE_LUMINANCE_REFERENCE;
    self->hdr_metadata.hdmi_metadata_type1.max_cll = SDR_WHITE_LUMINANCE_REFERENCE;
    self->hdr_video_max_luminance = SDR_WHITE_LUMINANCE_REFERENCE;
}

// TODO: Check if this hdr data can be changed after the call to av_packet_side_data_add
static void gsr_kms_set_hdr_metadata(gsr_capture_kms *self, const gsr_kms_response_item *drm_fd) {
    if(self->hdr_metadata_set)
        return;

    self->hdr_metadata_set = true;
    self->hdr_metadata = drm_fd->hdr_metadata;

    float sdr_white_luminance = 0.0f;
    float hdr_peak_luminance = 0.0f;
    gsr_capture_kms_get_monitor_luminances(self, drm_fd, &sdr_white_luminance, &hdr_peak_luminance);
    self->hdr_video_max_luminance = hdr_peak_luminance * (SDR_WHITE_LUMINANCE_REFERENCE / sdr_white_luminance);
}

static vec2i swap_vec2i(vec2i value) {
    int tmp = value.x;
    value.x = value.y;
    value.y = tmp;
    return value;
}

static EGLImage gsr_capture_kms_create_egl_image(gsr_capture_kms *self, const gsr_kms_response_item *drm_fd, const int *fds, const uint32_t *offsets, const uint32_t *pitches, const uint64_t *modifiers, bool use_modifiers) {
    intptr_t img_attr[44];
    setup_dma_buf_attrs(img_attr, drm_fd->pixel_format, drm_fd->width, drm_fd->height, fds, offsets, pitches, modifiers, drm_fd->num_dma_bufs, use_modifiers);
    while(self->params.egl->eglGetError() != EGL_SUCCESS){}
    EGLImage image = self->params.egl->eglCreateImage(self->params.egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr);
    if(!image || self->params.egl->eglGetError() != EGL_SUCCESS) {
        if(image)
            self->params.egl->eglDestroyImage(self->params.egl->egl_display, image);
        return NULL;
    }
    return image;
}

static EGLImage gsr_capture_kms_create_egl_image_with_fallback(gsr_capture_kms *self, const gsr_kms_response_item *drm_fd) {
    // TODO: This causes a crash sometimes on steam deck, why? is it a driver bug? a vaapi pure version doesn't cause a crash.
    // Even ffmpeg kmsgrab causes this crash. The error is:
    // amdgpu: Failed to allocate a buffer:
    // amdgpu:    size      : 28508160 bytes
    // amdgpu:    alignment : 2097152 bytes
    // amdgpu:    domains   : 4
    // amdgpu:    flags   : 4
    // amdgpu: Failed to allocate a buffer:
    // amdgpu:    size      : 28508160 bytes
    // amdgpu:    alignment : 2097152 bytes
    // amdgpu:    domains   : 4
    // amdgpu:    flags   : 4
    // EE ../jupiter-mesa/src/gallium/drivers/radeonsi/radeon_vcn_enc.c:516 radeon_create_encoder UVD - Can't create CPB buffer.
    // [hevc_vaapi @ 0x55ea72b09840] Failed to upload encode parameters: 2 (resource allocation failed).
    // [hevc_vaapi @ 0x55ea72b09840] Encode failed: -5.
    // Error: avcodec_send_frame failed, error: Input/output error
    // Assertion pic->display_order == pic->encode_order failed at libavcodec/vaapi_encode_h265.c:765
    // kms server info: kms client shutdown, shutting down the server

    int fds[GSR_KMS_MAX_DMA_BUFS];
    uint32_t offsets[GSR_KMS_MAX_DMA_BUFS];
    uint32_t pitches[GSR_KMS_MAX_DMA_BUFS];
    uint64_t modifiers[GSR_KMS_MAX_DMA_BUFS];

    for(int i = 0; i < drm_fd->num_dma_bufs; ++i) {
        fds[i] = drm_fd->dma_buf[i].fd;
        offsets[i] = drm_fd->dma_buf[i].offset;
        pitches[i] = drm_fd->dma_buf[i].pitch;
        modifiers[i] = drm_fd->modifier;
    }

    EGLImage image = NULL;
    if(self->no_modifiers_fallback) {
        image = gsr_capture_kms_create_egl_image(self, drm_fd, fds, offsets, pitches, modifiers, false);
    } else {
        image = gsr_capture_kms_create_egl_image(self, drm_fd, fds, offsets, pitches, modifiers, true);
        if(!image) {
            fprintf(stderr, "gsr error: gsr_capture_kms_create_egl_image_with_fallback: failed to create egl image with modifiers, trying without modifiers\n");
            self->no_modifiers_fallback = true;
            image = gsr_capture_kms_create_egl_image(self, drm_fd, fds, offsets, pitches, modifiers, false);
        }
    }
    return image;
}

static bool gsr_capture_kms_bind_image_to_texture(gsr_capture_kms *self, EGLImage image, unsigned int texture_id, bool external_texture) {
    const int texture_target = external_texture ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
    while(self->params.egl->glGetError() != 0){}
    self->params.egl->glBindTexture(texture_target, texture_id);
    self->params.egl->glEGLImageTargetTexture2DOES(texture_target, image);
    const bool success = self->params.egl->glGetError() == 0;
    self->params.egl->glBindTexture(texture_target, 0);
    return success;
}

static void gsr_capture_kms_bind_image_to_input_texture_with_fallback(gsr_capture_kms *self, EGLImage image) {
    if(self->external_texture_fallback) {
        gsr_capture_kms_bind_image_to_texture(self, image, self->external_input_texture_id, true);
    } else {
        if(!gsr_capture_kms_bind_image_to_texture(self, image, self->input_texture_id, false)) {
            fprintf(stderr, "gsr error: gsr_capture_kms_capture: failed to bind image to texture, trying with external texture\n");
            self->external_texture_fallback = true;
            gsr_capture_kms_bind_image_to_texture(self, image, self->external_input_texture_id, true);
        }
    }
}

static gsr_kms_response_item* find_monitor_drm(gsr_capture_kms *self, bool *capture_is_combined_plane) {
    *capture_is_combined_plane = false;
    gsr_kms_response_item *drm_fd = NULL;

    for(int i = 0; i < self->monitor_id.num_connector_ids; ++i) {
        drm_fd = find_drm_by_connector_id(self->params.kms_response, self->monitor_id.connector_ids[i]);
        if(drm_fd)
            break;
    }

    // Will never happen on wayland unless the target monitor has been disconnected
    if(!drm_fd && self->is_x11) {
        drm_fd = find_largest_drm(self->params.kms_response);
        *capture_is_combined_plane = true;
    }

    return drm_fd;
}

static gsr_kms_response_item* find_cursor_drm_if_on_monitor(gsr_capture_kms *self, uint32_t monitor_connector_id, bool capture_is_combined_plane) {
    gsr_kms_response_item *cursor_drm_fd = find_cursor_drm(self->params.kms_response, monitor_connector_id);
    if(!capture_is_combined_plane && cursor_drm_fd && cursor_drm_fd->connector_id != monitor_connector_id)
        cursor_drm_fd = NULL;
    return cursor_drm_fd;
}

static gsr_monitor_rotation kms_rotation_to_gsr_monitor_rotation(gsr_kms_rotation rotation) {
    // Right now both enums have the same values
    return (gsr_monitor_rotation)rotation;
}

static int remainder_int(int a, int b) {
    return a - (a / b) * b;
}

static gsr_monitor_rotation sub_rotations(gsr_monitor_rotation rot1, gsr_monitor_rotation rot2) {
    return remainder_int(rot1 - rot2, 4);
}

static void render_drm_cursor(gsr_capture_kms *self, gsr_color_conversion *color_conversion, gsr_capture_metadata *capture_metadata, const gsr_kms_response_item *cursor_drm_fd, vec2i target_pos, vec2i output_size, vec2i framebuffer_size) {
    const vec2d scale = {
        self->capture_size.x == 0 ? 0 : (double)output_size.x / (double)self->capture_size.x,
        self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y
    };

    const bool cursor_texture_id_is_external = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA;
    const vec2i cursor_size = {cursor_drm_fd->width, cursor_drm_fd->height};

    const gsr_monitor_rotation cursor_plane_rotation = kms_rotation_to_gsr_monitor_rotation(cursor_drm_fd->rotation);
    const gsr_monitor_rotation rotation = sub_rotations(self->display_server_monitor_rotation, cursor_plane_rotation);

    vec2i cursor_pos = {cursor_drm_fd->dst_x, cursor_drm_fd->dst_y};
    switch(rotation) {
        case GSR_MONITOR_ROT_0:
            break;
        case GSR_MONITOR_ROT_90:
            cursor_pos = swap_vec2i(cursor_pos);
            cursor_pos.x = framebuffer_size.x - cursor_pos.x;
            // TODO: Remove this horrible hack
            cursor_pos.x -= cursor_size.x;
            break;
        case GSR_MONITOR_ROT_180:
            cursor_pos.x = framebuffer_size.x - cursor_pos.x;
            cursor_pos.y = framebuffer_size.y - cursor_pos.y;
            // TODO: Remove this horrible hack
            cursor_pos.x -= cursor_size.x;
            cursor_pos.y -= cursor_size.y;
            break;
        case GSR_MONITOR_ROT_270:
            cursor_pos = swap_vec2i(cursor_pos);
            cursor_pos.y = framebuffer_size.y - cursor_pos.y;
            // TODO: Remove this horrible hack
            cursor_pos.y -= cursor_size.y;
            break;
    }

    cursor_pos.x -= self->params.region_position.x;
    cursor_pos.y -= self->params.region_position.y;

    cursor_pos.x *= scale.x;
    cursor_pos.y *= scale.y;

    cursor_pos.x += target_pos.x;
    cursor_pos.y += target_pos.y;

    int fds[GSR_KMS_MAX_DMA_BUFS];
    uint32_t offsets[GSR_KMS_MAX_DMA_BUFS];
    uint32_t pitches[GSR_KMS_MAX_DMA_BUFS];
    uint64_t modifiers[GSR_KMS_MAX_DMA_BUFS];

    for(int i = 0; i < cursor_drm_fd->num_dma_bufs; ++i) {
        fds[i] = cursor_drm_fd->dma_buf[i].fd;
        offsets[i] = cursor_drm_fd->dma_buf[i].offset;
        pitches[i] = cursor_drm_fd->dma_buf[i].pitch;
        modifiers[i] = cursor_drm_fd->modifier;
    }

    intptr_t img_attr_cursor[44];
    setup_dma_buf_attrs(img_attr_cursor, cursor_drm_fd->pixel_format, cursor_drm_fd->width, cursor_drm_fd->height,
        fds, offsets, pitches, modifiers, cursor_drm_fd->num_dma_bufs, true);

    EGLImage cursor_image = self->params.egl->eglCreateImage(self->params.egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr_cursor);
    const int target = cursor_texture_id_is_external ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
    self->params.egl->glBindTexture(target, self->cursor_texture_id);
    self->params.egl->glEGLImageTargetTexture2DOES(target, cursor_image);
    self->params.egl->glBindTexture(target, 0);

    if(cursor_image)
        self->params.egl->eglDestroyImage(self->params.egl->egl_display, cursor_image);

    gsr_capture_kms_update_hdr_color_transforms(self, color_conversion, cursor_drm_fd);

    self->params.egl->glEnable(GL_SCISSOR_TEST);
    self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y);

    gsr_color_conversion_draw(color_conversion, self->cursor_texture_id,
        cursor_pos, (vec2i){cursor_size.x * scale.x, cursor_size.y * scale.y},
        (vec2i){0, 0}, cursor_size, cursor_size,
        gsr_monitor_rotation_to_rotation(rotation), capture_metadata->flip, GSR_SOURCE_COLOR_RGB, cursor_texture_id_is_external);

    self->params.egl->glDisable(GL_SCISSOR_TEST);
}

static void render_x11_cursor(gsr_capture_kms *self, gsr_color_conversion *color_conversion, gsr_capture_metadata *capture_metadata, vec2i capture_pos, vec2i target_pos, vec2i output_size) {
    if(!self->params.x11_cursor->visible)
        return;

    const vec2d scale = {
        self->capture_size.x == 0 ? 0 : (double)output_size.x / (double)self->capture_size.x,
        self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y
    };

    const vec2i cursor_pos = {
        target_pos.x + (self->params.x11_cursor->position.x - self->params.x11_cursor->hotspot.x - capture_pos.x) * scale.x,
        target_pos.y + (self->params.x11_cursor->position.y - self->params.x11_cursor->hotspot.y - capture_pos.y) * scale.y
    };

    self->params.egl->glEnable(GL_SCISSOR_TEST);
    self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y);

    gsr_color_conversion_draw(color_conversion, self->params.x11_cursor->texture_id,
        cursor_pos, (vec2i){self->params.x11_cursor->size.x * scale.x, self->params.x11_cursor->size.y * scale.y},
        (vec2i){0, 0}, self->params.x11_cursor->size, self->params.x11_cursor->size,
        GSR_ROT_0, capture_metadata->flip, GSR_SOURCE_COLOR_RGB, false);

    self->params.egl->glDisable(GL_SCISSOR_TEST);
}

static void gsr_capture_kms_update_capture_size_change(gsr_capture_kms *self, gsr_color_conversion *color_conversion, vec2i target_pos, const gsr_kms_response_item *drm_fd) {
    if(target_pos.x != self->prev_target_pos.x || target_pos.y != self->prev_target_pos.y || drm_fd->src_w != self->prev_plane_size.x || drm_fd->src_h != self->prev_plane_size.y) {
        self->prev_target_pos = target_pos;
        self->prev_plane_size = self->capture_size;
        color_conversion->schedule_clear = true;
    }
}

static void gsr_capture_kms_update_connector_ids(gsr_capture_kms *self) {
    const double now = clock_get_monotonic_seconds();
    if(now - self->last_time_monitor_check < FIND_CRTC_BY_NAME_TIMEOUT_SECONDS)
        return;

    self->last_time_monitor_check = now;
    /* TODO: Assume for now that there is only 1 framebuffer for all monitors and it doesn't change */
    if(self->is_x11)
        return;

    self->gamma_lut_connector_id = 0;

    self->monitor_id.num_connector_ids = 0;
    const gsr_connection_type connection_type = self->is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;
    // MonitorCallbackUserdata monitor_callback_userdata = {
    //     &self->monitor_id,
    //     self->params.display_to_capture, strlen(self->params.display_to_capture),
    //     0,
    // };
    // for_each_active_monitor_output(self->params.egl->window, self->params.egl->card_path, connection_type, monitor_callback, &monitor_callback_userdata);

    gsr_monitor monitor;
    if(!get_monitor_by_name(self->params.egl, connection_type, self->params.display_to_capture, &monitor)) {
        fprintf(stderr, "gsr error: gsr_capture_kms_update_connector_ids: failed to find monitor by name \"%s\"\n", self->params.display_to_capture);
        return;
    }

    self->monitor_id.num_connector_ids = 1;
    self->monitor_id.connector_ids[0] = monitor.connector_id;

    monitor.name = self->params.display_to_capture;
    vec2i monitor_position = {0, 0};
    // TODO: This is cached. We need it updated.
    drm_monitor_get_display_server_data(self->params.egl->window, &monitor, &self->display_server_monitor_rotation, &monitor_position);

    self->capture_pos = monitor.pos;
    /* Monitor size is already rotated on x11 when the monitor is rotated, no need to apply it ourselves */
    if(self->is_x11)
        self->capture_size = monitor.size;
    else
        self->capture_size = rotate_capture_size_if_rotated(self, monitor.size, self->display_server_monitor_rotation);
}

static void gsr_capture_kms_pre_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
    gsr_capture_kms *self = cap->priv;

    if(self->params.kms_response->num_items == 0) {
        static bool error_shown = false;
        if(!error_shown) {
            error_shown = true;
            fprintf(stderr, "gsr error: gsr_capture_kms_pre_capture: no drm found, capture will fail\n");
        }
        return;
    }

    gsr_capture_kms_update_connector_ids(self);

    self->capture_is_combined_plane = false;
    self->drm_fd = find_monitor_drm(self, &self->capture_is_combined_plane);
    if(!self->drm_fd)
        return;

    if(drm_plane_is_hdr(self->drm_fd))
        gsr_capture_kms_update_gamma_lut(self, color_conversion);

    if(self->params.hdr) {
        if(drm_plane_is_hdr(self->drm_fd))
            gsr_kms_set_hdr_metadata(self, self->drm_fd);
        else
            gsr_kms_set_sdr_in_hdr_metadata(self);
    }

    const gsr_monitor_rotation plane_rotation = kms_rotation_to_gsr_monitor_rotation(self->drm_fd->rotation);
    self->final_monitor_rotation = self->capture_is_combined_plane ? GSR_MONITOR_ROT_0 : sub_rotations(self->display_server_monitor_rotation, plane_rotation);

    self->capture_size = rotate_capture_size_if_rotated(self, (vec2i){ self->drm_fd->src_w, self->drm_fd->src_h }, self->final_monitor_rotation);
    if(self->params.region_size.x > 0 && self->params.region_size.y > 0)
        self->capture_size = self->params.region_size;

    self->output_size = scale_keep_aspect_ratio(self->capture_size, capture_metadata->recording_size);
    self->target_pos = gsr_capture_get_target_position(self->output_size, capture_metadata);
    gsr_capture_kms_update_capture_size_change(self, color_conversion, self->target_pos, self->drm_fd);
}

static void render_monitor_plane(gsr_capture_kms *self, gsr_color_conversion *color_conversion, gsr_capture_metadata *capture_metadata) {
    vec2i capture_pos = self->capture_pos;
    if(!self->capture_is_combined_plane)
        capture_pos = (vec2i){self->drm_fd->src_x, self->drm_fd->src_y};

    capture_pos.x += self->params.region_position.x;
    capture_pos.y += self->params.region_position.y;

    EGLImage image = gsr_capture_kms_create_egl_image_with_fallback(self, self->drm_fd);
    if(image) {
        gsr_capture_kms_bind_image_to_input_texture_with_fallback(self, image);
        self->params.egl->eglDestroyImage(self->params.egl->egl_display, image);
    }

    gsr_capture_kms_update_hdr_color_transforms(self, color_conversion, self->drm_fd);

    gsr_color_conversion_draw(color_conversion, self->external_texture_fallback ? self->external_input_texture_id : self->input_texture_id,
        self->target_pos, self->output_size,
        capture_pos, self->capture_size, (vec2i){ self->drm_fd->width, self->drm_fd->height },
        gsr_monitor_rotation_to_rotation(self->final_monitor_rotation), capture_metadata->flip, GSR_SOURCE_COLOR_RGB, self->external_texture_fallback);
}

/* Renders an overlay plane on top of (or below, depending on render order) the monitor plane, scaled to the output */
static void render_drm_plane(gsr_capture_kms *self, gsr_color_conversion *color_conversion, gsr_capture_metadata *capture_metadata, const gsr_kms_response_item *plane_drm_fd, vec2i target_pos, vec2i output_size, vec2i framebuffer_size) {
    const vec2d scale = {
        self->capture_size.x == 0 ? 0 : (double)output_size.x / (double)self->capture_size.x,
        self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y
    };

    const gsr_monitor_rotation plane_rotation = kms_rotation_to_gsr_monitor_rotation(plane_drm_fd->rotation);
    const gsr_monitor_rotation rotation = sub_rotations(self->display_server_monitor_rotation, plane_rotation);

    const vec2i plane_size = {plane_drm_fd->dst_w, plane_drm_fd->dst_h};
    vec2i plane_pos = {plane_drm_fd->dst_x, plane_drm_fd->dst_y};
    switch(rotation) {
        case GSR_MONITOR_ROT_0:
            break;
        case GSR_MONITOR_ROT_90:
            plane_pos = swap_vec2i(plane_pos);
            plane_pos.x = framebuffer_size.x - plane_pos.x;
            // TODO: Remove this horrible hack
            plane_pos.x -= plane_size.x;
            break;
        case GSR_MONITOR_ROT_180:
            plane_pos.x = framebuffer_size.x - plane_pos.x;
            plane_pos.y = framebuffer_size.y - plane_pos.y;
            // TODO: Remove this horrible hack
            plane_pos.x -= plane_size.x;
            plane_pos.y -= plane_size.y;
            break;
        case GSR_MONITOR_ROT_270:
            plane_pos = swap_vec2i(plane_pos);
            plane_pos.y = framebuffer_size.y - plane_pos.y;
            // TODO: Remove this horrible hack
            plane_pos.y -= plane_size.y;
            break;
    }

    plane_pos.x -= self->params.region_position.x;
    plane_pos.y -= self->params.region_position.y;

    plane_pos.x *= scale.x;
    plane_pos.y *= scale.y;

    plane_pos.x += target_pos.x;
    plane_pos.y += target_pos.y;

    EGLImage image = gsr_capture_kms_create_egl_image_with_fallback(self, plane_drm_fd);
    if(!image)
        return;

    gsr_capture_kms_bind_image_to_input_texture_with_fallback(self, image);
    self->params.egl->eglDestroyImage(self->params.egl->egl_display, image);

    gsr_capture_kms_update_hdr_color_transforms(self, color_conversion, plane_drm_fd);

    self->params.egl->glEnable(GL_SCISSOR_TEST);
    self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y);

    gsr_color_conversion_draw(color_conversion, self->external_texture_fallback ? self->external_input_texture_id : self->input_texture_id,
        plane_pos, (vec2i){plane_size.x * scale.x, plane_size.y * scale.y},
        (vec2i){plane_drm_fd->src_x, plane_drm_fd->src_y}, (vec2i){plane_drm_fd->src_w, plane_drm_fd->src_h}, (vec2i){plane_drm_fd->width, plane_drm_fd->height},
        gsr_monitor_rotation_to_rotation(rotation), capture_metadata->flip, GSR_SOURCE_COLOR_RGB, self->external_texture_fallback);

    self->params.egl->glDisable(GL_SCISSOR_TEST);
}

static int gsr_capture_kms_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) {
    gsr_capture_kms *self = cap->priv;

    if(!self->drm_fd || self->params.kms_response->num_items == 0)
        return -1;

    const vec2i framebuffer_size = rotate_capture_size_if_rotated(self, (vec2i){ self->drm_fd->src_w, self->drm_fd->src_h }, self->final_monitor_rotation);

    //self->params.egl->glFlush();
    //self->params.egl->glFinish();

    /* Gather all planes that are displayed on the captured monitor. Overlay planes are not used on x11 (combined plane) */
    const gsr_kms_response_item *planes[GSR_KMS_MAX_ITEMS];
    int num_planes = 0;
    planes[num_planes++] = self->drm_fd;
    if(!self->capture_is_combined_plane) {
        for(int i = 0; i < self->params.kms_response->num_items && num_planes < GSR_KMS_MAX_ITEMS; ++i) {
            const gsr_kms_response_item *item = &self->params.kms_response->items[i];
            if(item->plane_type == KMS_PLANE_TYPE_OVERLAY && item->connector_id == self->drm_fd->connector_id)
                planes[num_planes++] = item;
        }
    }

    /* Sort the planes by zpos, from bottom to top. Insertion sort to keep planes with the same zpos in the order the drm driver returned them (stable) */
    for(int i = 1; i < num_planes; ++i) {
        const gsr_kms_response_item *plane = planes[i];
        int j = i - 1;
        for(; j >= 0 && planes[j]->zpos > plane->zpos; --j) {
            planes[j + 1] = planes[j];
        }
        planes[j + 1] = plane;
    }

    for(int i = 0; i < num_planes; ++i) {
        if(planes[i] == self->drm_fd)
            render_monitor_plane(self, color_conversion, capture_metadata);
        else
            render_drm_plane(self, color_conversion, capture_metadata, planes[i], self->target_pos, self->output_size, framebuffer_size);
    }

    if(self->params.record_cursor) {
        gsr_kms_response_item *cursor_drm_fd = find_cursor_drm_if_on_monitor(self, self->drm_fd->connector_id, self->capture_is_combined_plane);
        // The cursor is handled by x11 on x11 instead of using the cursor drm plane because on prime systems with a dedicated nvidia gpu
        // the cursor plane is not available when the cursor is on the monitor controlled by the nvidia device.
        // TODO: This doesn't work properly with software cursor on x11 since it will draw the x11 cursor on top of the cursor already in the framebuffer.
        // Detect if software cursor is used on x11 somehow.
        if(self->is_x11) {
            vec2i cursor_monitor_offset = self->capture_pos;
            cursor_monitor_offset.x += self->params.region_position.x;
            cursor_monitor_offset.y += self->params.region_position.y;
            render_x11_cursor(self, color_conversion, capture_metadata, cursor_monitor_offset, self->target_pos, self->output_size);
        } else if(cursor_drm_fd) {
            render_drm_cursor(self, color_conversion, capture_metadata, cursor_drm_fd, self->target_pos, self->output_size, framebuffer_size);
        }
    }

    gsr_color_conversion_set_hdr_to_sdr_tone_mapping(color_conversion, false, 0.0f, 0.0f);
    gsr_color_conversion_enable_gamma_lut(color_conversion, false);
    gsr_color_conversion_set_color_matrix(color_conversion, NULL, GSR_COLOR_MATRIX_TRANSFER_PQ);

    //self->params.egl->glFlush();
    //self->params.egl->glFinish();

    return 0;
}

static bool gsr_capture_kms_should_stop(gsr_capture *cap, bool *err) {
    (void)cap;
    if(err)
        *err = false;
    return false;
}

static bool gsr_capture_kms_uses_external_image(gsr_capture *cap) {
    (void)cap;
    return true;
}

static bool gsr_capture_kms_set_hdr_metadata(gsr_capture *cap, AVMasteringDisplayMetadata *mastering_display_metadata, AVContentLightMetadata *light_metadata) {
    gsr_capture_kms *self = cap->priv;

    if(!self->hdr_metadata_set)
        return false;

    light_metadata->MaxCLL = self->hdr_metadata.hdmi_metadata_type1.max_cll;
    light_metadata->MaxFALL = self->hdr_metadata.hdmi_metadata_type1.max_fall;

    for(int i = 0; i < 3; ++i) {
        mastering_display_metadata->display_primaries[i][0] = av_make_q(self->hdr_metadata.hdmi_metadata_type1.display_primaries[i].x, 50000);
        mastering_display_metadata->display_primaries[i][1] = av_make_q(self->hdr_metadata.hdmi_metadata_type1.display_primaries[i].y, 50000);
    }

    mastering_display_metadata->white_point[0] = av_make_q(self->hdr_metadata.hdmi_metadata_type1.white_point.x, 50000);
    mastering_display_metadata->white_point[1] = av_make_q(self->hdr_metadata.hdmi_metadata_type1.white_point.y, 50000);

    mastering_display_metadata->min_luminance = av_make_q(self->hdr_metadata.hdmi_metadata_type1.min_display_mastering_luminance, 10000);
    mastering_display_metadata->max_luminance = av_make_q(self->hdr_metadata.hdmi_metadata_type1.max_display_mastering_luminance, 1);

    if(self->hdr_video_max_luminance > 0.0f) {
        light_metadata->MaxCLL = (unsigned int)(self->hdr_video_max_luminance + 0.5f);
        mastering_display_metadata->max_luminance = av_make_q((int)(self->hdr_video_max_luminance + 0.5f), 1);
    }

    mastering_display_metadata->has_primaries = true;
    mastering_display_metadata->has_luminance = true;

    return true;
}

// static bool gsr_capture_kms_is_damaged(gsr_capture *cap) {
//     gsr_capture_kms *self = cap->priv;
//     return self->damaged;
// }

// static void gsr_capture_kms_clear_damage(gsr_capture *cap) {
//     gsr_capture_kms *self = cap->priv;
//     self->damaged = false;
// }

static void gsr_capture_kms_destroy(gsr_capture *cap) {
    gsr_capture_kms *self = cap->priv;
    if(cap->priv) {
        gsr_capture_kms_stop(self);
        free((void*)self->params.display_to_capture);
        self->params.display_to_capture = NULL;
        free(cap->priv);
        cap->priv = NULL;
    }
    free(cap);
}

gsr_capture* gsr_capture_kms_create(const gsr_capture_kms_params *params) {
    if(!params) {
        fprintf(stderr, "gsr error: gsr_capture_kms_create params is NULL\n");
        return NULL;
    }

    gsr_capture *cap = calloc(1, sizeof(gsr_capture));
    if(!cap)
        return NULL;

    gsr_capture_kms *cap_kms = calloc(1, sizeof(gsr_capture_kms));
    if(!cap_kms) {
        free(cap);
        return NULL;
    }

    const char *display_to_capture = strdup(params->display_to_capture);
    if(!display_to_capture) {
        free(cap);
        free(cap_kms);
        return NULL;
    }

    cap_kms->params = *params;
    cap_kms->params.display_to_capture = display_to_capture;
    
    *cap = (gsr_capture) {
        .start = gsr_capture_kms_start,
        //.tick = gsr_capture_kms_tick,
        .should_stop = gsr_capture_kms_should_stop,
        .pre_capture = gsr_capture_kms_pre_capture,
        .capture = gsr_capture_kms_capture,
        .uses_external_image = gsr_capture_kms_uses_external_image,
        .set_hdr_metadata = gsr_capture_kms_set_hdr_metadata,
        //.is_damaged = gsr_capture_kms_is_damaged,
        //.clear_damage = gsr_capture_kms_clear_damage,
        .destroy = gsr_capture_kms_destroy,
        .priv = cap_kms
    };

    return cap;
}