File: dri3.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (956 lines) | stat: -rw-r--r-- 21,522 bytes parent folder | download | duplicates (8)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file was automatically generated with:
// ../../ui/gfx/x/gen_xproto.py \
//    ../../third_party/xcbproto/src \
//    gen/ui/gfx/x \
//    bigreq \
//    dri3 \
//    glx \
//    randr \
//    render \
//    screensaver \
//    shape \
//    shm \
//    sync \
//    xfixes \
//    xinput \
//    xkb \
//    xproto \
//    xtest

#include "dri3.h"

#include <unistd.h>
#include <xcb/xcb.h>
#include <xcb/xcbext.h>

#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/xproto_internal.h"

namespace x11 {

Dri3::Dri3(Connection* connection, const x11::QueryExtensionReply& info)
    : connection_(connection), info_(info) {}

Future<Dri3::QueryVersionReply> Dri3::QueryVersion(
    const Dri3::QueryVersionRequest& request) {
  if (!connection_->Ready() || !present())
    return {};

  WriteBuffer buf;

  auto& major_version = request.major_version;
  auto& minor_version = request.minor_version;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 0;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // major_version
  buf.Write(&major_version);

  // minor_version
  buf.Write(&minor_version);

  Align(&buf, 4);

  return connection_->SendRequest<Dri3::QueryVersionReply>(
      &buf, "Dri3::QueryVersion", false);
}

Future<Dri3::QueryVersionReply> Dri3::QueryVersion(
    const uint32_t& major_version,
    const uint32_t& minor_version) {
  return Dri3::QueryVersion(
      Dri3::QueryVersionRequest{major_version, minor_version});
}

template <>
COMPONENT_EXPORT(X11)
std::unique_ptr<Dri3::QueryVersionReply> detail::ReadReply<
    Dri3::QueryVersionReply>(ReadBuffer* buffer) {
  auto& buf = *buffer;
  auto reply = std::make_unique<Dri3::QueryVersionReply>();

  auto& sequence = (*reply).sequence;
  auto& major_version = (*reply).major_version;
  auto& minor_version = (*reply).minor_version;

  // response_type
  uint8_t response_type;
  Read(&response_type, &buf);

  // pad0
  Pad(&buf, 1);

  // sequence
  Read(&sequence, &buf);

  // length
  uint32_t length;
  Read(&length, &buf);

  // major_version
  Read(&major_version, &buf);

  // minor_version
  Read(&minor_version, &buf);

  Align(&buf, 4);
  CHECK_EQ(buf.offset < 32 ? 0 : buf.offset - 32, 4 * length);

  return reply;
}

Future<Dri3::OpenReply> Dri3::Open(const Dri3::OpenRequest& request) {
  if (!connection_->Ready() || !present())
    return {};

  WriteBuffer buf;

  auto& drawable = request.drawable;
  auto& provider = request.provider;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 1;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // drawable
  buf.Write(&drawable);

  // provider
  buf.Write(&provider);

  Align(&buf, 4);

  return connection_->SendRequest<Dri3::OpenReply>(&buf, "Dri3::Open", true);
}

Future<Dri3::OpenReply> Dri3::Open(const Drawable& drawable,
                                   const uint32_t& provider) {
  return Dri3::Open(Dri3::OpenRequest{drawable, provider});
}

template <>
COMPONENT_EXPORT(X11)
std::unique_ptr<Dri3::OpenReply> detail::ReadReply<Dri3::OpenReply>(
    ReadBuffer* buffer) {
  auto& buf = *buffer;
  auto reply = std::make_unique<Dri3::OpenReply>();

  auto& nfd = (*reply).nfd;
  auto& sequence = (*reply).sequence;
  auto& device_fd = (*reply).device_fd;

  // response_type
  uint8_t response_type;
  Read(&response_type, &buf);

  // nfd
  Read(&nfd, &buf);

  // sequence
  Read(&sequence, &buf);

  // length
  uint32_t length;
  Read(&length, &buf);

  // device_fd
  device_fd = RefCountedFD(buf.TakeFd());

  // pad0
  Pad(&buf, 24);

  Align(&buf, 4);
  CHECK_EQ(buf.offset < 32 ? 0 : buf.offset - 32, 4 * length);

  return reply;
}

Future<void> Dri3::PixmapFromBuffer(
    const Dri3::PixmapFromBufferRequest& request) {
  if (!connection_->Ready() || !present())
    return {};

  WriteBuffer buf;

  auto& pixmap = request.pixmap;
  auto& drawable = request.drawable;
  auto& size = request.size;
  auto& width = request.width;
  auto& height = request.height;
  auto& stride = request.stride;
  auto& depth = request.depth;
  auto& bpp = request.bpp;
  auto& pixmap_fd = request.pixmap_fd;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 2;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // pixmap
  buf.Write(&pixmap);

  // drawable
  buf.Write(&drawable);

  // size
  buf.Write(&size);

  // width
  buf.Write(&width);

  // height
  buf.Write(&height);

  // stride
  buf.Write(&stride);

  // depth
  buf.Write(&depth);

  // bpp
  buf.Write(&bpp);

  // pixmap_fd
  buf.fds().push_back(HANDLE_EINTR(dup(pixmap_fd.get())));

  Align(&buf, 4);

  return connection_->SendRequest<void>(&buf, "Dri3::PixmapFromBuffer", false);
}

Future<void> Dri3::PixmapFromBuffer(const Pixmap& pixmap,
                                    const Drawable& drawable,
                                    const uint32_t& size,
                                    const uint16_t& width,
                                    const uint16_t& height,
                                    const uint16_t& stride,
                                    const uint8_t& depth,
                                    const uint8_t& bpp,
                                    const RefCountedFD& pixmap_fd) {
  return Dri3::PixmapFromBuffer(Dri3::PixmapFromBufferRequest{
      pixmap, drawable, size, width, height, stride, depth, bpp, pixmap_fd});
}

Future<Dri3::BufferFromPixmapReply> Dri3::BufferFromPixmap(
    const Dri3::BufferFromPixmapRequest& request) {
  if (!connection_->Ready() || !present())
    return {};

  WriteBuffer buf;

  auto& pixmap = request.pixmap;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 3;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // pixmap
  buf.Write(&pixmap);

  Align(&buf, 4);

  return connection_->SendRequest<Dri3::BufferFromPixmapReply>(
      &buf, "Dri3::BufferFromPixmap", true);
}

Future<Dri3::BufferFromPixmapReply> Dri3::BufferFromPixmap(
    const Pixmap& pixmap) {
  return Dri3::BufferFromPixmap(Dri3::BufferFromPixmapRequest{pixmap});
}

template <>
COMPONENT_EXPORT(X11)
std::unique_ptr<Dri3::BufferFromPixmapReply> detail::ReadReply<
    Dri3::BufferFromPixmapReply>(ReadBuffer* buffer) {
  auto& buf = *buffer;
  auto reply = std::make_unique<Dri3::BufferFromPixmapReply>();

  auto& nfd = (*reply).nfd;
  auto& sequence = (*reply).sequence;
  auto& size = (*reply).size;
  auto& width = (*reply).width;
  auto& height = (*reply).height;
  auto& stride = (*reply).stride;
  auto& depth = (*reply).depth;
  auto& bpp = (*reply).bpp;
  auto& pixmap_fd = (*reply).pixmap_fd;

  // response_type
  uint8_t response_type;
  Read(&response_type, &buf);

  // nfd
  Read(&nfd, &buf);

  // sequence
  Read(&sequence, &buf);

  // length
  uint32_t length;
  Read(&length, &buf);

  // size
  Read(&size, &buf);

  // width
  Read(&width, &buf);

  // height
  Read(&height, &buf);

  // stride
  Read(&stride, &buf);

  // depth
  Read(&depth, &buf);

  // bpp
  Read(&bpp, &buf);

  // pixmap_fd
  pixmap_fd = RefCountedFD(buf.TakeFd());

  // pad0
  Pad(&buf, 12);

  Align(&buf, 4);
  CHECK_EQ(buf.offset < 32 ? 0 : buf.offset - 32, 4 * length);

  return reply;
}

Future<void> Dri3::FenceFromFD(const Dri3::FenceFromFDRequest& request) {
  if (!connection_->Ready() || !present())
    return {};

  WriteBuffer buf;

  auto& drawable = request.drawable;
  auto& fence = request.fence;
  auto& initially_triggered = request.initially_triggered;
  auto& fence_fd = request.fence_fd;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 4;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // drawable
  buf.Write(&drawable);

  // fence
  buf.Write(&fence);

  // initially_triggered
  buf.Write(&initially_triggered);

  // pad0
  Pad(&buf, 3);

  // fence_fd
  buf.fds().push_back(HANDLE_EINTR(dup(fence_fd.get())));

  Align(&buf, 4);

  return connection_->SendRequest<void>(&buf, "Dri3::FenceFromFD", false);
}

Future<void> Dri3::FenceFromFD(const Drawable& drawable,
                               const uint32_t& fence,
                               const uint8_t& initially_triggered,
                               const RefCountedFD& fence_fd) {
  return Dri3::FenceFromFD(
      Dri3::FenceFromFDRequest{drawable, fence, initially_triggered, fence_fd});
}

Future<Dri3::FDFromFenceReply> Dri3::FDFromFence(
    const Dri3::FDFromFenceRequest& request) {
  if (!connection_->Ready() || !present())
    return {};

  WriteBuffer buf;

  auto& drawable = request.drawable;
  auto& fence = request.fence;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 5;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // drawable
  buf.Write(&drawable);

  // fence
  buf.Write(&fence);

  Align(&buf, 4);

  return connection_->SendRequest<Dri3::FDFromFenceReply>(
      &buf, "Dri3::FDFromFence", true);
}

Future<Dri3::FDFromFenceReply> Dri3::FDFromFence(const Drawable& drawable,
                                                 const uint32_t& fence) {
  return Dri3::FDFromFence(Dri3::FDFromFenceRequest{drawable, fence});
}

template <>
COMPONENT_EXPORT(X11)
std::unique_ptr<Dri3::FDFromFenceReply> detail::ReadReply<
    Dri3::FDFromFenceReply>(ReadBuffer* buffer) {
  auto& buf = *buffer;
  auto reply = std::make_unique<Dri3::FDFromFenceReply>();

  auto& nfd = (*reply).nfd;
  auto& sequence = (*reply).sequence;
  auto& fence_fd = (*reply).fence_fd;

  // response_type
  uint8_t response_type;
  Read(&response_type, &buf);

  // nfd
  Read(&nfd, &buf);

  // sequence
  Read(&sequence, &buf);

  // length
  uint32_t length;
  Read(&length, &buf);

  // fence_fd
  fence_fd = RefCountedFD(buf.TakeFd());

  // pad0
  Pad(&buf, 24);

  Align(&buf, 4);
  CHECK_EQ(buf.offset < 32 ? 0 : buf.offset - 32, 4 * length);

  return reply;
}

Future<Dri3::GetSupportedModifiersReply> Dri3::GetSupportedModifiers(
    const Dri3::GetSupportedModifiersRequest& request) {
  if (!connection_->Ready() || !present())
    return {};

  WriteBuffer buf;

  auto& window = request.window;
  auto& depth = request.depth;
  auto& bpp = request.bpp;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 6;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // window
  buf.Write(&window);

  // depth
  buf.Write(&depth);

  // bpp
  buf.Write(&bpp);

  // pad0
  Pad(&buf, 2);

  Align(&buf, 4);

  return connection_->SendRequest<Dri3::GetSupportedModifiersReply>(
      &buf, "Dri3::GetSupportedModifiers", false);
}

Future<Dri3::GetSupportedModifiersReply> Dri3::GetSupportedModifiers(
    const uint32_t& window,
    const uint8_t& depth,
    const uint8_t& bpp) {
  return Dri3::GetSupportedModifiers(
      Dri3::GetSupportedModifiersRequest{window, depth, bpp});
}

template <>
COMPONENT_EXPORT(X11)
std::unique_ptr<Dri3::GetSupportedModifiersReply> detail::ReadReply<
    Dri3::GetSupportedModifiersReply>(ReadBuffer* buffer) {
  auto& buf = *buffer;
  auto reply = std::make_unique<Dri3::GetSupportedModifiersReply>();

  auto& sequence = (*reply).sequence;
  uint32_t num_window_modifiers{};
  uint32_t num_screen_modifiers{};
  auto& window_modifiers = (*reply).window_modifiers;
  auto& screen_modifiers = (*reply).screen_modifiers;

  // response_type
  uint8_t response_type;
  Read(&response_type, &buf);

  // pad0
  Pad(&buf, 1);

  // sequence
  Read(&sequence, &buf);

  // length
  uint32_t length;
  Read(&length, &buf);

  // num_window_modifiers
  Read(&num_window_modifiers, &buf);

  // num_screen_modifiers
  Read(&num_screen_modifiers, &buf);

  // pad1
  Pad(&buf, 16);

  // window_modifiers
  window_modifiers.resize(num_window_modifiers);
  for (auto& window_modifiers_elem : window_modifiers) {
    // window_modifiers_elem
    Read(&window_modifiers_elem, &buf);
  }

  // screen_modifiers
  screen_modifiers.resize(num_screen_modifiers);
  for (auto& screen_modifiers_elem : screen_modifiers) {
    // screen_modifiers_elem
    Read(&screen_modifiers_elem, &buf);
  }

  Align(&buf, 4);
  CHECK_EQ(buf.offset < 32 ? 0 : buf.offset - 32, 4 * length);

  return reply;
}

Future<void> Dri3::PixmapFromBuffers(
    const Dri3::PixmapFromBuffersRequest& request) {
  if (!connection_->Ready() || !present())
    return {};

  WriteBuffer buf;

  auto& pixmap = request.pixmap;
  auto& window = request.window;
  uint8_t num_buffers{};
  auto& width = request.width;
  auto& height = request.height;
  auto& stride0 = request.stride0;
  auto& offset0 = request.offset0;
  auto& stride1 = request.stride1;
  auto& offset1 = request.offset1;
  auto& stride2 = request.stride2;
  auto& offset2 = request.offset2;
  auto& stride3 = request.stride3;
  auto& offset3 = request.offset3;
  auto& depth = request.depth;
  auto& bpp = request.bpp;
  auto& modifier = request.modifier;
  auto& buffers = request.buffers;
  size_t buffers_len = buffers.size();

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 7;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // pixmap
  buf.Write(&pixmap);

  // window
  buf.Write(&window);

  // num_buffers
  num_buffers = buffers.size();
  buf.Write(&num_buffers);

  // pad0
  Pad(&buf, 3);

  // width
  buf.Write(&width);

  // height
  buf.Write(&height);

  // stride0
  buf.Write(&stride0);

  // offset0
  buf.Write(&offset0);

  // stride1
  buf.Write(&stride1);

  // offset1
  buf.Write(&offset1);

  // stride2
  buf.Write(&stride2);

  // offset2
  buf.Write(&offset2);

  // stride3
  buf.Write(&stride3);

  // offset3
  buf.Write(&offset3);

  // depth
  buf.Write(&depth);

  // bpp
  buf.Write(&bpp);

  // pad1
  Pad(&buf, 2);

  // modifier
  buf.Write(&modifier);

  // buffers
  CHECK_EQ(static_cast<size_t>(num_buffers), buffers.size());
  for (auto& buffers_elem : buffers) {
    // buffers_elem
    buf.fds().push_back(HANDLE_EINTR(dup(buffers_elem.get())));
  }

  Align(&buf, 4);

  return connection_->SendRequest<void>(&buf, "Dri3::PixmapFromBuffers", false);
}

Future<void> Dri3::PixmapFromBuffers(const Pixmap& pixmap,
                                     const Window& window,
                                     const uint16_t& width,
                                     const uint16_t& height,
                                     const uint32_t& stride0,
                                     const uint32_t& offset0,
                                     const uint32_t& stride1,
                                     const uint32_t& offset1,
                                     const uint32_t& stride2,
                                     const uint32_t& offset2,
                                     const uint32_t& stride3,
                                     const uint32_t& offset3,
                                     const uint8_t& depth,
                                     const uint8_t& bpp,
                                     const uint64_t& modifier,
                                     const std::vector<RefCountedFD>& buffers) {
  return Dri3::PixmapFromBuffers(Dri3::PixmapFromBuffersRequest{
      pixmap, window, width, height, stride0, offset0, stride1, offset1,
      stride2, offset2, stride3, offset3, depth, bpp, modifier, buffers});
}

Future<Dri3::BuffersFromPixmapReply> Dri3::BuffersFromPixmap(
    const Dri3::BuffersFromPixmapRequest& request) {
  if (!connection_->Ready() || !present())
    return {};

  WriteBuffer buf;

  auto& pixmap = request.pixmap;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 8;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // pixmap
  buf.Write(&pixmap);

  Align(&buf, 4);

  return connection_->SendRequest<Dri3::BuffersFromPixmapReply>(
      &buf, "Dri3::BuffersFromPixmap", true);
}

Future<Dri3::BuffersFromPixmapReply> Dri3::BuffersFromPixmap(
    const Pixmap& pixmap) {
  return Dri3::BuffersFromPixmap(Dri3::BuffersFromPixmapRequest{pixmap});
}

template <>
COMPONENT_EXPORT(X11)
std::unique_ptr<Dri3::BuffersFromPixmapReply> detail::ReadReply<
    Dri3::BuffersFromPixmapReply>(ReadBuffer* buffer) {
  auto& buf = *buffer;
  auto reply = std::make_unique<Dri3::BuffersFromPixmapReply>();

  uint8_t nfd{};
  auto& sequence = (*reply).sequence;
  auto& width = (*reply).width;
  auto& height = (*reply).height;
  auto& modifier = (*reply).modifier;
  auto& depth = (*reply).depth;
  auto& bpp = (*reply).bpp;
  auto& strides = (*reply).strides;
  auto& offsets = (*reply).offsets;
  auto& buffers = (*reply).buffers;

  // response_type
  uint8_t response_type;
  Read(&response_type, &buf);

  // nfd
  Read(&nfd, &buf);

  // sequence
  Read(&sequence, &buf);

  // length
  uint32_t length;
  Read(&length, &buf);

  // width
  Read(&width, &buf);

  // height
  Read(&height, &buf);

  // pad0
  Pad(&buf, 4);

  // modifier
  Read(&modifier, &buf);

  // depth
  Read(&depth, &buf);

  // bpp
  Read(&bpp, &buf);

  // pad1
  Pad(&buf, 6);

  // strides
  strides.resize(nfd);
  for (auto& strides_elem : strides) {
    // strides_elem
    Read(&strides_elem, &buf);
  }

  // offsets
  offsets.resize(nfd);
  for (auto& offsets_elem : offsets) {
    // offsets_elem
    Read(&offsets_elem, &buf);
  }

  // buffers
  buffers.resize(nfd);
  for (auto& buffers_elem : buffers) {
    // buffers_elem
    buffers_elem = RefCountedFD(buf.TakeFd());
  }

  Align(&buf, 4);
  CHECK_EQ(buf.offset < 32 ? 0 : buf.offset - 32, 4 * length);

  return reply;
}

Future<void> Dri3::SetDRMDeviceInUse(
    const Dri3::SetDRMDeviceInUseRequest& request) {
  if (!connection_->Ready() || !present())
    return {};

  WriteBuffer buf;

  auto& window = request.window;
  auto& drmMajor = request.drmMajor;
  auto& drmMinor = request.drmMinor;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 9;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // window
  buf.Write(&window);

  // drmMajor
  buf.Write(&drmMajor);

  // drmMinor
  buf.Write(&drmMinor);

  Align(&buf, 4);

  return connection_->SendRequest<void>(&buf, "Dri3::SetDRMDeviceInUse", false);
}

Future<void> Dri3::SetDRMDeviceInUse(const Window& window,
                                     const uint32_t& drmMajor,
                                     const uint32_t& drmMinor) {
  return Dri3::SetDRMDeviceInUse(
      Dri3::SetDRMDeviceInUseRequest{window, drmMajor, drmMinor});
}

Future<void> Dri3::ImportSyncobj(const Dri3::ImportSyncobjRequest& request) {
  if (!connection_->Ready() || !present()) {
    return {};
  }

  WriteBuffer buf;

  auto& syncobj = request.syncobj;
  auto& drawable = request.drawable;
  auto& syncobj_fd = request.syncobj_fd;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 10;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // syncobj
  buf.Write(&syncobj);

  // drawable
  buf.Write(&drawable);

  // syncobj_fd
  buf.fds().push_back(HANDLE_EINTR(dup(syncobj_fd.get())));

  Align(&buf, 4);

  return connection_->SendRequest<void>(&buf, "Dri3::ImportSyncobj", false);
}

Future<void> Dri3::ImportSyncobj(const Syncobj& syncobj,
                                 const Drawable& drawable,
                                 const RefCountedFD& syncobj_fd) {
  return Dri3::ImportSyncobj(
      Dri3::ImportSyncobjRequest{syncobj, drawable, syncobj_fd});
}

Future<void> Dri3::FreeSyncobj(const Dri3::FreeSyncobjRequest& request) {
  if (!connection_->Ready() || !present()) {
    return {};
  }

  WriteBuffer buf;

  auto& syncobj = request.syncobj;

  // major_opcode
  uint8_t major_opcode = info_.major_opcode;
  buf.Write(&major_opcode);

  // minor_opcode
  uint8_t minor_opcode = 11;
  buf.Write(&minor_opcode);

  // length
  // Caller fills in length for writes.
  Pad(&buf, sizeof(uint16_t));

  // syncobj
  buf.Write(&syncobj);

  Align(&buf, 4);

  return connection_->SendRequest<void>(&buf, "Dri3::FreeSyncobj", false);
}

Future<void> Dri3::FreeSyncobj(const Syncobj& syncobj) {
  return Dri3::FreeSyncobj(Dri3::FreeSyncobjRequest{syncobj});
}

}  // namespace x11