DisplayEventReceiver.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_GUI_DISPLAY_EVENT_H
  17. #define ANDROID_GUI_DISPLAY_EVENT_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <utils/Errors.h>
  21. #include <utils/RefBase.h>
  22. #include <utils/Timers.h>
  23. #include <binder/IInterface.h>
  24. // ----------------------------------------------------------------------------
  25. namespace android {
  26. // ----------------------------------------------------------------------------
  27. class BitTube;
  28. class IDisplayEventConnection;
  29. // ----------------------------------------------------------------------------
  30. class DisplayEventReceiver {
  31. public:
  32. enum {
  33. DISPLAY_EVENT_VSYNC = 'vsyn',
  34. DISPLAY_EVENT_HOTPLUG = 'plug'
  35. };
  36. struct Event {
  37. struct Header {
  38. uint32_t type;
  39. uint32_t id;
  40. nsecs_t timestamp __attribute__((aligned(8)));
  41. };
  42. struct VSync {
  43. uint32_t count;
  44. };
  45. struct Hotplug {
  46. bool connected;
  47. };
  48. Header header;
  49. union {
  50. VSync vsync;
  51. Hotplug hotplug;
  52. };
  53. };
  54. public:
  55. /*
  56. * DisplayEventReceiver creates and registers an event connection with
  57. * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
  58. * or requestNextVsync to receive them.
  59. * Other events start being delivered immediately.
  60. */
  61. DisplayEventReceiver();
  62. /*
  63. * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
  64. * stop being delivered immediately. Note that the queue could have
  65. * some events pending. These will be delivered.
  66. */
  67. ~DisplayEventReceiver();
  68. /*
  69. * initCheck returns the state of DisplayEventReceiver after construction.
  70. */
  71. status_t initCheck() const;
  72. /*
  73. * getFd returns the file descriptor to use to receive events.
  74. * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
  75. * file-descriptor.
  76. */
  77. int getFd() const;
  78. /*
  79. * getEvents reads events from the queue and returns how many events were
  80. * read. Returns 0 if there are no more events or a negative error code.
  81. * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
  82. * should be destroyed and getEvents() shouldn't be called again.
  83. */
  84. ssize_t getEvents(Event* events, size_t count);
  85. static ssize_t getEvents(const sp<BitTube>& dataChannel,
  86. Event* events, size_t count);
  87. /*
  88. * sendEvents write events to the queue and returns how many events were
  89. * written.
  90. */
  91. static ssize_t sendEvents(const sp<BitTube>& dataChannel,
  92. Event const* events, size_t count);
  93. /*
  94. * setVsyncRate() sets the Event::VSync delivery rate. A value of
  95. * 1 returns every Event::VSync. A value of 2 returns every other event,
  96. * etc... a value of 0 returns no event unless requestNextVsync() has
  97. * been called.
  98. */
  99. status_t setVsyncRate(uint32_t count);
  100. /*
  101. * requestNextVsync() schedules the next Event::VSync. It has no effect
  102. * if the vsync rate is > 0.
  103. */
  104. status_t requestNextVsync();
  105. private:
  106. sp<IDisplayEventConnection> mEventConnection;
  107. sp<BitTube> mDataChannel;
  108. };
  109. // ----------------------------------------------------------------------------
  110. }; // namespace android
  111. #endif // ANDROID_GUI_DISPLAY_EVENT_H