File: buffer_checker_test.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (664 lines) | stat: -rw-r--r-- 24,102 bytes parent folder | download | duplicates (6)
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
// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <drm_fourcc.h>
#include <gbm.h>
#include <sys/mman.h>

#include <algorithm>
#include <cstdint>
#include <iterator>
#include <vector>

#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/containers/queue.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/test/task_environment.h"
#include "components/exo/wayland/clients/client_base.h"
#include "components/exo/wayland/clients/client_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/buffer_usage_util.h"
#include "ui/gfx/linux/drm_util_linux.h"
#include "ui/gfx/linux/gbm_util.h"

namespace {

std::string DrmCodeToString(uint32_t drm_format) {
  return std::string{static_cast<char>(drm_format),
                     static_cast<char>(drm_format >> 8),
                     static_cast<char>(drm_format >> 16),
                     static_cast<char>(drm_format >> 24), 0};
}

std::string DrmCodeToBufferFormatString(int32_t drm_format) {
  return gfx::BufferFormatToString(
      ui::GetBufferFormatFromFourCCFormat(drm_format));
}

std::string DrmModifiersToString(std::vector<uint64_t> drm_modifiers) {
  std::stringstream ss;
  for (uint64_t drm_modifier : drm_modifiers)
    ss << "0x" << std::hex << drm_modifier << " ";
  return ss.str();
}

}  // namespace

namespace exo {
namespace wayland {
namespace test {

namespace {

#define WL_ARRAY_FOR_EACH(pos, array, type)                             \
  for (pos = (type)(array)->data;                                       \
       (const char*)pos < ((const char*)(array)->data + (array)->size); \
       (pos)++)

class BufferCheckerTestClient : public ::exo::wayland::clients::ClientBase {
 public:
  explicit BufferCheckerTestClient() = default;
  ~BufferCheckerTestClient() override = default;

  int GetNumSupportedUsages(uint32_t format) {
    std::vector<gfx::BufferUsage> supported_usages;
    bool callback_pending = false;
    std::unique_ptr<wl_callback> frame_callback;
    wl_callback_listener frame_listener = {
        [](void* data, struct wl_callback*, uint32_t) {
          *(static_cast<bool*>(data)) = false;
        }};

    base::queue<gfx::BufferUsage> usages_to_test;
    for (int i = 0; i < static_cast<int>(gfx::BufferUsage::LAST); i++)
      usages_to_test.push(static_cast<gfx::BufferUsage>(i));

    gfx::BufferUsage current_usage;
    std::unique_ptr<Buffer> current_buffer;
    do {
      if (callback_pending)
        continue;

      if (current_buffer) {
        supported_usages.push_back(current_usage);
      }

      if (wl_display_get_error(display_.get())) {
        LOG(ERROR) << "Wayland error encountered";
        return -1;
      }

      // Buffers may fail to be created, so loop until we get one or return
      // early if we run out. We can't loop in the outer loop because, without
      // doing the rest of the code, we won't be dispatched to again.
      do {
        if (usages_to_test.size() == 0) {
          std::vector<std::string> supported_usage_strings;
          std::ranges::transform(supported_usages,
                                 std::back_inserter(supported_usage_strings),
                                 gfx::BufferUsageToString);
          LOG(INFO) << "Successfully used buffer with format drm: "
                    << DrmCodeToString(format) << " gfx::BufferFormat: "
                    << DrmCodeToBufferFormatString(format)
                    << " gfx::BufferUsages: ["
                    << base::JoinString(supported_usage_strings, ", ") << "]";
          return supported_usages.size();
        }

        current_usage = usages_to_test.front();
        usages_to_test.pop();

        current_buffer = CreateDrmBuffer(
            gfx::Size(surface_size_.width(), surface_size_.height()), format,
            nullptr, 0, ui::BufferUsageToGbmFlags(current_usage),
            /*y_invert=*/false);
        if (!current_buffer) {
          LOG(ERROR) << "Unable to create buffer for drm: "
                     << DrmCodeToString(format) << " gfx::BufferFormat: "
                     << DrmCodeToBufferFormatString(format)
                     << " gfx::BufferUsage "
                     << gfx::BufferUsageToString(current_usage);
        }
      } while (current_buffer == nullptr);

      LOG(INFO) << "Attempting to use buffer with format drm: "
                << DrmCodeToString(format)
                << " gfx::BufferFormat: " << DrmCodeToBufferFormatString(format)
                << " gfx::BufferUsage "
                << gfx::BufferUsageToString(current_usage);

      wl_surface_damage(surface_.get(), 0, 0, surface_size_.width(),
                        surface_size_.height());
      wl_surface_attach(surface_.get(), current_buffer->buffer.get(), 0, 0);

      frame_callback.reset(wl_surface_frame(surface_.get()));
      wl_callback_add_listener(frame_callback.get(), &frame_listener,
                               &callback_pending);
      callback_pending = true;
      wl_surface_commit(surface_.get());

      wl_display_flush(display_.get());
    } while (wl_display_dispatch(display_.get()) != -1);

    LOG(ERROR)
        << "Expected to return from inside the loop. Wayland disconnected?";
    return -1;
  }

  int GetNumSupportedFormatsAndModifier(uint32_t format,
                                        std::vector<uint64_t> modifiers) {
    std::vector<gfx::BufferUsage> supported_usages;
    bool callback_pending = false;
    std::unique_ptr<wl_callback> frame_callback;
    wl_callback_listener frame_listener = {
        [](void* data, struct wl_callback*, uint32_t) {
          *(static_cast<bool*>(data)) = false;
        }};

    std::unique_ptr<Buffer> current_buffer;
    do {
      if (callback_pending)
        continue;

      if (current_buffer) {
        LOG(INFO) << "Successfully used buffer with drm format: "
                  << DrmCodeToString(format)
                  << " drm modifiers: " << DrmModifiersToString(modifiers)
                  << " gfx::BufferFormat: "
                  << DrmCodeToBufferFormatString(format);
        return 1;
      }

      if (wl_display_get_error(display_.get())) {
        LOG(ERROR) << "Wayland error encountered";
        return -1;
      }

      if (modifiers.size() == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
        current_buffer = CreateDrmBuffer(
            gfx::Size(surface_size_.width(), surface_size_.height()), format,
            nullptr, 0, ui::BufferUsageToGbmFlags(gfx::BufferUsage::GPU_READ),
            /*y_invert=*/false);
      } else {
        current_buffer = CreateDrmBuffer(
            gfx::Size(surface_size_.width(), surface_size_.height()), format,
            modifiers.data(), modifiers.size(),
            ui::BufferUsageToGbmFlags(gfx::BufferUsage::GPU_READ),
            /*y_invert=*/false);
      }
      if (!current_buffer) {
        LOG(ERROR) << "Unable to create buffer for drm format: "
                   << DrmCodeToString(format)
                   << " drm modifiers: " << DrmModifiersToString(modifiers)
                   << " gfx::BufferFormat: "
                   << DrmCodeToBufferFormatString(format);
        return 0;
      }

      LOG(INFO) << "Attempting to use buffer with format drm format: "
                << DrmCodeToString(format)
                << " drm modifiers: " << DrmModifiersToString(modifiers)
                << " gfx::BufferFormat: "
                << DrmCodeToBufferFormatString(format);

      wl_surface_damage(surface_.get(), 0, 0, surface_size_.width(),
                        surface_size_.height());
      wl_surface_attach(surface_.get(), current_buffer->buffer.get(), 0, 0);

      frame_callback.reset(wl_surface_frame(surface_.get()));
      wl_callback_add_listener(frame_callback.get(), &frame_listener,
                               &callback_pending);
      callback_pending = true;
      wl_surface_commit(surface_.get());

      wl_display_flush(display_.get());
    } while (wl_display_dispatch(display_.get()) != -1);

    LOG(ERROR)
        << "Expected to return from inside the loop. Wayland disconnected?";
    return -1;
  }

  std::vector<uint32_t> reported_formats;
  base::flat_map<uint32_t, std::vector<uint64_t>> reported_format_modifier_map;

  struct WaylandDmabufFeedbackFormat {
    uint32_t format;
    uint32_t padding;
    uint64_t modifier;
  };

  struct DmabufFeedbackTranche {
    dev_t target_device;
    uint32_t flags;
    base::flat_map<uint32_t, std::vector<uint64_t>> format_modifier_map;
  };

  struct DmabufFeedback {
    dev_t main_device;
    std::vector<WaylandDmabufFeedbackFormat> format_table;
    std::vector<DmabufFeedbackTranche> tranches;
    DmabufFeedbackTranche pending_tranche;
  };

  DmabufFeedback current_feedback_;
  DmabufFeedback pending_feedback_;

  void HandleFeedbackDone(zwp_linux_dmabuf_feedback_v1* dmabuf_feedback) {
    current_feedback_ = pending_feedback_;
    pending_feedback_ = {};

    reported_formats.clear();
    reported_format_modifier_map.clear();
    for (DmabufFeedbackTranche tranche : current_feedback_.tranches) {
      for (const auto& [format, modifiers] : tranche.format_modifier_map) {
        if (!base::Contains(reported_formats, format)) {
          reported_formats.push_back(format);
        }
        if (!reported_format_modifier_map.contains(format)) {
          reported_format_modifier_map[format] = std::vector<uint64_t>();
        }

        for (uint64_t modifier : modifiers) {
          if (!base::Contains(reported_format_modifier_map[format], modifier)) {
            reported_format_modifier_map[format].push_back(modifier);
          }
        }
      }
    }
  }

  void HandleFeedbackFormatTable(
      zwp_linux_dmabuf_feedback_v1* zwp_linux_dmabuf_feedback_v1,
      int32_t fd,
      uint32_t size) {
    ASSERT_TRUE(pending_feedback_.format_table.empty());

    WaylandDmabufFeedbackFormat* format_table =
        static_cast<WaylandDmabufFeedbackFormat*>(
            mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0));
    uint32_t table_size = size / sizeof(WaylandDmabufFeedbackFormat);
    for (uint32_t i = 0; i < table_size; i++) {
      pending_feedback_.format_table.push_back(format_table[i]);
    }
    munmap(format_table, size);
    close(fd);
  }

  void HandleFeedbackMainDevice(zwp_linux_dmabuf_feedback_v1* dmabuf_feedback,
                                wl_array* dev) {
    memcpy(&pending_feedback_.main_device, dev->data, sizeof(dev));
  }

  void HandleFeedbackTrancheDone(
      zwp_linux_dmabuf_feedback_v1* dmabuf_feedback) {
    pending_feedback_.tranches.push_back(pending_feedback_.pending_tranche);
    pending_feedback_.pending_tranche = {};
  }

  void HandleFeedbackTrancheTargetDevice(
      zwp_linux_dmabuf_feedback_v1* dmabuf_feedback,
      wl_array* dev) {
    memcpy(&pending_feedback_.pending_tranche.target_device, dev->data,
           sizeof(dev));
  }

  void HandleFeedbackTrancheFormats(
      zwp_linux_dmabuf_feedback_v1* dmabuf_feedback,
      wl_array* indices) {
    std::vector<WaylandDmabufFeedbackFormat>* format_table =
        &pending_feedback_.format_table;
    if (format_table == nullptr)
      format_table = &current_feedback_.format_table;
    ASSERT_TRUE(format_table != nullptr);

    uint16_t* index;
    WL_ARRAY_FOR_EACH(index, indices, uint16_t*) {
      uint32_t format = format_table->at(*index).format;
      uint64_t modifier = format_table->at(*index).modifier;

      if (!pending_feedback_.pending_tranche.format_modifier_map.contains(
              format))
        pending_feedback_.pending_tranche.format_modifier_map[format] =
            std::vector<uint64_t>();

      pending_feedback_.pending_tranche.format_modifier_map[format].push_back(
          modifier);
    }
  }

  void HandleFeedbackTrancheFlags(zwp_linux_dmabuf_feedback_v1* dmabuf_feedback,
                                  uint32_t flags) {
    pending_feedback_.pending_tranche.flags = flags;
  }

  void AddFeedbackListener(zwp_linux_dmabuf_feedback_v1* dmabuf_feedback_obj) {
    static struct zwp_linux_dmabuf_feedback_v1_listener kLinuxFeedbackListener =
        {
            .done =
                [](void* data,
                   struct zwp_linux_dmabuf_feedback_v1* dmabuf_feedback) {
                  static_cast<BufferCheckerTestClient*>(data)
                      ->HandleFeedbackDone(dmabuf_feedback);
                },
            .format_table =
                [](void* data, zwp_linux_dmabuf_feedback_v1* dmabuf_feedback,
                   int32_t fd, uint32_t size) {
                  static_cast<BufferCheckerTestClient*>(data)
                      ->HandleFeedbackFormatTable(dmabuf_feedback, fd, size);
                },
            .main_device =
                [](void* data,
                   struct zwp_linux_dmabuf_feedback_v1* dmabuf_feedback,
                   struct wl_array* dev) {
                  static_cast<BufferCheckerTestClient*>(data)
                      ->HandleFeedbackMainDevice(dmabuf_feedback, dev);
                },
            .tranche_done =
                [](void* data,
                   struct zwp_linux_dmabuf_feedback_v1* dmabuf_feedback) {
                  static_cast<BufferCheckerTestClient*>(data)
                      ->HandleFeedbackTrancheDone(dmabuf_feedback);
                },
            .tranche_target_device =
                [](void* data,
                   struct zwp_linux_dmabuf_feedback_v1* dmabuf_feedback,
                   struct wl_array* dev) {
                  static_cast<BufferCheckerTestClient*>(data)
                      ->HandleFeedbackTrancheTargetDevice(dmabuf_feedback, dev);
                },
            .tranche_formats =
                [](void* data,
                   struct zwp_linux_dmabuf_feedback_v1* dmabuf_feedback,
                   struct wl_array* indices) {
                  static_cast<BufferCheckerTestClient*>(data)
                      ->HandleFeedbackTrancheFormats(dmabuf_feedback, indices);
                },
            .tranche_flags =
                [](void* data,
                   struct zwp_linux_dmabuf_feedback_v1* dmabuf_feedback,
                   uint32_t flags) {
                  static_cast<BufferCheckerTestClient*>(data)
                      ->HandleFeedbackTrancheFlags(dmabuf_feedback, flags);
                },
        };

    zwp_linux_dmabuf_feedback_v1_add_listener(dmabuf_feedback_obj,
                                              &kLinuxFeedbackListener, this);
    wl_display_roundtrip(display_.get());
  }

  void GetDefaultFeedback() {
    zwp_linux_dmabuf_feedback_v1* dmabuf_feedback_obj =
        zwp_linux_dmabuf_v1_get_default_feedback(globals_.linux_dmabuf.get());

    AddFeedbackListener(dmabuf_feedback_obj);
  }

  void GetSurfaceFeedback() {
    zwp_linux_dmabuf_feedback_v1* dmabuf_feedback_obj =
        zwp_linux_dmabuf_v1_get_surface_feedback(globals_.linux_dmabuf.get(),
                                                 surface_.get());

    AddFeedbackListener(dmabuf_feedback_obj);
  }

 protected:
  void HandleDmabufFormat(void* data,
                          struct zwp_linux_dmabuf_v1* zwp_linux_dmabuf_v1,
                          uint32_t format) override {
    reported_formats.push_back(format);
  }

  void HandleDmabufModifier(void* data,
                            struct zwp_linux_dmabuf_v1* zwp_linux_dmabuf_v1,
                            uint32_t format,
                            uint32_t modifier_hi,
                            uint32_t modifier_lo) override {
    if (!reported_format_modifier_map.contains(format)) {
      reported_format_modifier_map[format] = std::vector<uint64_t>();
    }

    uint64_t modifier = static_cast<uint64_t>(modifier_hi) << 32 | modifier_lo;
    reported_format_modifier_map[format].push_back(modifier);
  }
};

}  // namespace
}  // namespace test
}  // namespace wayland
}  // namespace exo

class BufferCheckerClientTest : public testing::Test {
 protected:
  void SetUp() override {
    base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
    CHECK(base_params_.FromCommandLine(*command_line));
    CHECK(base_params_.use_drm) << "Missing --use-drm parameter which is "
                                   "required for gbm buffer allocation";
  }

  base::test::SingleThreadTaskEnvironment task_environment_{
      base::test::TaskEnvironment::MainThreadType::UI};

  exo::wayland::clients::ClientBase::InitParams base_params_;
};

void PrintReportedFormats(std::vector<uint32_t>& formats) {
  std::vector<std::string> drm_names;
  std::vector<std::string> buffer_names;
  for (auto format : formats) {
    drm_names.push_back(DrmCodeToString(format));
    buffer_names.push_back(DrmCodeToBufferFormatString(format));
  }
  LOG(INFO) << "zwp_linux_dmabuf_v1 reported supported DRM formats: "
             << base::JoinString(drm_names, ", ");
  LOG(INFO) << "zwp_linux_dmabuf_v1 reported supported gfx::BufferFormats: "
             << base::JoinString(buffer_names, ", ");
}

TEST_F(BufferCheckerClientTest, CanUseAnyReportedBufferFormatsLegacy) {
  exo::wayland::test::BufferCheckerTestClient client;
  auto params = base_params_;
  // Initialize no buffers when we start, wait until we've gotten the list
  params.num_buffers = 0;
  params.linux_dmabuf_version =
      ZWP_LINUX_BUFFER_PARAMS_V1_CREATE_IMMED_SINCE_VERSION;
  ASSERT_TRUE(client.Init(params));
  EXPECT_TRUE(!client.reported_formats.empty());

  PrintReportedFormats(client.reported_formats);
  bool has_any_supported_formats = false;
  for (auto format : client.reported_formats) {
    int res = client.GetNumSupportedUsages(format);
    EXPECT_TRUE(res != -1);
    if (res > 0) {
      has_any_supported_formats = true;
    }
  }
  EXPECT_TRUE(has_any_supported_formats);
}

TEST_F(BufferCheckerClientTest, CanUseAnyReportedBufferModifiersLegacy) {
  exo::wayland::test::BufferCheckerTestClient client;
  auto params = base_params_;
  // Initialize no buffers when we start, wait until we've gotten the list
  params.num_buffers = 0;
  params.linux_dmabuf_version = ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION;
  ASSERT_TRUE(client.Init(params));
  EXPECT_TRUE(!client.reported_format_modifier_map.empty());

  bool has_any_supported_formats = false;
  for (const auto& [format, modifiers] : client.reported_format_modifier_map) {
    std::vector<uint64_t> valid_modifiers;
    for (uint64_t modifier : modifiers) {
      if (modifier != DRM_FORMAT_MOD_INVALID)
        valid_modifiers.push_back(modifier);
    }

    if (!valid_modifiers.empty()) {
      int res =
          client.GetNumSupportedFormatsAndModifier(format, valid_modifiers);
      EXPECT_TRUE(res != -1);
      if (res > 0) {
        has_any_supported_formats = true;
      }
    }

    if (base::Contains(modifiers, DRM_FORMAT_MOD_INVALID)) {
      int res = client.GetNumSupportedFormatsAndModifier(
          format, std::vector<uint64_t>({DRM_FORMAT_MOD_INVALID}));
      EXPECT_TRUE(res != -1);
      if (res > 0) {
        has_any_supported_formats = true;
      }
    }
  }
  EXPECT_TRUE(has_any_supported_formats);
}

TEST_F(BufferCheckerClientTest, CanUseAnyReportedBufferFormatsDefaultFeedback) {
  exo::wayland::test::BufferCheckerTestClient client;
  auto params = base_params_;
  // Initialize no buffers when we start, wait until we've gotten the list
  params.num_buffers = 0;
  params.linux_dmabuf_version =
      ZWP_LINUX_DMABUF_V1_GET_DEFAULT_FEEDBACK_SINCE_VERSION;
  ASSERT_TRUE(client.Init(params));

  EXPECT_TRUE(client.reported_format_modifier_map.empty());
  client.GetDefaultFeedback();
  EXPECT_TRUE(!client.reported_format_modifier_map.empty());

  PrintReportedFormats(client.reported_formats);
  bool has_any_supported_formats = false;
  for (auto format : client.reported_formats) {
    int res = client.GetNumSupportedUsages(format);
    EXPECT_TRUE(res != -1);
    if (res > 0) {
      has_any_supported_formats = true;
    }
  }
  EXPECT_TRUE(has_any_supported_formats);
}

TEST_F(BufferCheckerClientTest,
       CanUseAnyReportedBufferModifiersDefaultFeedback) {
  exo::wayland::test::BufferCheckerTestClient client;
  auto params = base_params_;
  // Initialize no buffers when we start, wait until we've gotten the list
  params.num_buffers = 0;
  params.linux_dmabuf_version =
      ZWP_LINUX_DMABUF_V1_GET_DEFAULT_FEEDBACK_SINCE_VERSION;
  ASSERT_TRUE(client.Init(params));

  EXPECT_TRUE(client.reported_format_modifier_map.empty());
  client.GetDefaultFeedback();
  EXPECT_TRUE(!client.reported_format_modifier_map.empty());

  bool has_any_supported_formats = false;
  for (const auto& [format, modifiers] : client.reported_format_modifier_map) {
    std::vector<uint64_t> valid_modifiers;
    for (uint64_t modifier : modifiers) {
      if (modifier != DRM_FORMAT_MOD_INVALID)
        valid_modifiers.push_back(modifier);
    }

    if (!valid_modifiers.empty()) {
      int res =
          client.GetNumSupportedFormatsAndModifier(format, valid_modifiers);
      EXPECT_TRUE(res != -1);
      if (res > 0) {
        has_any_supported_formats = true;
      }
    }

    if (base::Contains(modifiers, DRM_FORMAT_MOD_INVALID)) {
      int res = client.GetNumSupportedFormatsAndModifier(
          format, std::vector<uint64_t>({DRM_FORMAT_MOD_INVALID}));
      EXPECT_TRUE(res != -1);
      if (res > 0) {
        has_any_supported_formats = true;
      }
    }
  }
  EXPECT_TRUE(has_any_supported_formats);
}

TEST_F(BufferCheckerClientTest, CanUseAnyReportedBufferFormatsSurfaceFeedback) {
  exo::wayland::test::BufferCheckerTestClient client;
  auto params = base_params_;
  // Initialize no buffers when we start, wait until we've gotten the list
  params.num_buffers = 0;
  params.linux_dmabuf_version =
      ZWP_LINUX_DMABUF_V1_GET_SURFACE_FEEDBACK_SINCE_VERSION;
  ASSERT_TRUE(client.Init(params));

  EXPECT_TRUE(client.reported_format_modifier_map.empty());
  client.GetSurfaceFeedback();
  EXPECT_TRUE(!client.reported_format_modifier_map.empty());

  PrintReportedFormats(client.reported_formats);
  bool has_any_supported_formats = false;
  for (auto format : client.reported_formats) {
    int res = client.GetNumSupportedUsages(format);
    EXPECT_TRUE(res != -1);
    if (res > 0) {
      has_any_supported_formats = true;
    }
  }
  EXPECT_TRUE(has_any_supported_formats);
}

TEST_F(BufferCheckerClientTest,
       CanUseAnyReportedBufferModifiersSurfaceFeedback) {
  exo::wayland::test::BufferCheckerTestClient client;
  auto params = base_params_;
  // Initialize no buffers when we start, wait until we've gotten the list
  params.num_buffers = 0;
  params.linux_dmabuf_version =
      ZWP_LINUX_DMABUF_V1_GET_SURFACE_FEEDBACK_SINCE_VERSION;
  ASSERT_TRUE(client.Init(params));

  EXPECT_TRUE(client.reported_format_modifier_map.empty());
  client.GetSurfaceFeedback();
  EXPECT_TRUE(!client.reported_format_modifier_map.empty());

  bool has_any_supported_formats = false;
  for (const auto& [format, modifiers] : client.reported_format_modifier_map) {
    std::vector<uint64_t> valid_modifiers;
    for (uint64_t modifier : modifiers) {
      if (modifier != DRM_FORMAT_MOD_INVALID)
        valid_modifiers.push_back(modifier);
    }

    if (!valid_modifiers.empty()) {
      int res =
          client.GetNumSupportedFormatsAndModifier(format, valid_modifiers);
      EXPECT_TRUE(res != -1);
      if (res > 0) {
        has_any_supported_formats = true;
      }
    }

    if (base::Contains(modifiers, DRM_FORMAT_MOD_INVALID)) {
      int res = client.GetNumSupportedFormatsAndModifier(
          format, std::vector<uint64_t>({DRM_FORMAT_MOD_INVALID}));
      EXPECT_TRUE(res != -1);
      if (res > 0) {
        has_any_supported_formats = true;
      }
    }
  }
  EXPECT_TRUE(has_any_supported_formats);
}