wp_presentation.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/exo/wayland/wp_presentation.h"
  5. #include <presentation-time-server-protocol.h>
  6. #include <memory>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/time/time.h"
  10. #include "components/exo/wayland/server_util.h"
  11. #include "ui/gfx/presentation_feedback.h"
  12. namespace exo {
  13. namespace wayland {
  14. namespace {
  15. ////////////////////////////////////////////////////////////////////////////////
  16. // presentation_interface:
  17. void HandleSurfacePresentationCallback(
  18. wl_resource* resource,
  19. const gfx::PresentationFeedback& feedback) {
  20. if (feedback.timestamp.is_null()) {
  21. wp_presentation_feedback_send_discarded(resource);
  22. } else {
  23. int64_t presentation_time_us = feedback.timestamp.ToInternalValue();
  24. int64_t seconds = presentation_time_us / base::Time::kMicrosecondsPerSecond;
  25. int64_t microseconds =
  26. presentation_time_us % base::Time::kMicrosecondsPerSecond;
  27. static_assert(
  28. static_cast<uint32_t>(gfx::PresentationFeedback::Flags::kVSync) ==
  29. static_cast<uint32_t>(WP_PRESENTATION_FEEDBACK_KIND_VSYNC),
  30. "gfx::PresentationFlags::VSync don't match!");
  31. static_assert(
  32. static_cast<uint32_t>(gfx::PresentationFeedback::Flags::kHWClock) ==
  33. static_cast<uint32_t>(WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK),
  34. "gfx::PresentationFlags::HWClock don't match!");
  35. static_assert(
  36. static_cast<uint32_t>(
  37. gfx::PresentationFeedback::Flags::kHWCompletion) ==
  38. static_cast<uint32_t>(WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION),
  39. "gfx::PresentationFlags::HWCompletion don't match!");
  40. static_assert(
  41. static_cast<uint32_t>(gfx::PresentationFeedback::Flags::kZeroCopy) ==
  42. static_cast<uint32_t>(WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY),
  43. "gfx::PresentationFlags::ZeroCopy don't match!");
  44. wp_presentation_feedback_send_presented(
  45. resource, seconds >> 32, seconds & 0xffffffff,
  46. microseconds * base::Time::kNanosecondsPerMicrosecond,
  47. feedback.interval.InMicroseconds() *
  48. base::Time::kNanosecondsPerMicrosecond,
  49. 0, 0, feedback.flags);
  50. }
  51. wl_client_flush(wl_resource_get_client(resource));
  52. }
  53. void presentation_destroy(wl_client* client, wl_resource* resource) {
  54. wl_resource_destroy(resource);
  55. }
  56. void presentation_feedback(wl_client* client,
  57. wl_resource* resource,
  58. wl_resource* surface_resource,
  59. uint32_t id) {
  60. wl_resource* presentation_feedback_resource =
  61. wl_resource_create(client, &wp_presentation_feedback_interface,
  62. wl_resource_get_version(resource), id);
  63. // base::Unretained is safe as the resource owns the callback.
  64. auto cancelable_callback = std::make_unique<base::CancelableRepeatingCallback<
  65. void(const gfx::PresentationFeedback&)>>(
  66. base::BindRepeating(&HandleSurfacePresentationCallback,
  67. base::Unretained(presentation_feedback_resource)));
  68. GetUserDataAs<Surface>(surface_resource)
  69. ->RequestPresentationCallback(cancelable_callback->callback());
  70. SetImplementation(presentation_feedback_resource, nullptr,
  71. std::move(cancelable_callback));
  72. }
  73. const struct wp_presentation_interface presentation_implementation = {
  74. presentation_destroy, presentation_feedback};
  75. } // namespace
  76. void bind_presentation(wl_client* client,
  77. void* data,
  78. uint32_t version,
  79. uint32_t id) {
  80. wl_resource* resource =
  81. wl_resource_create(client, &wp_presentation_interface, 1, id);
  82. wl_resource_set_implementation(resource, &presentation_implementation, data,
  83. nullptr);
  84. wp_presentation_send_clock_id(resource, CLOCK_MONOTONIC);
  85. }
  86. } // namespace wayland
  87. } // namespace exo