EventThread.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_SURFACE_FLINGER_EVENT_THREAD_H
  17. #define ANDROID_SURFACE_FLINGER_EVENT_THREAD_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <gui/DisplayEventReceiver.h>
  21. #include <gui/IDisplayEventConnection.h>
  22. #include <utils/Errors.h>
  23. #include <utils/threads.h>
  24. #include <utils/SortedVector.h>
  25. #include "DisplayDevice.h"
  26. #include "DisplayHardware/PowerHAL.h"
  27. // ---------------------------------------------------------------------------
  28. namespace android {
  29. // ---------------------------------------------------------------------------
  30. class SurfaceFlinger;
  31. class String8;
  32. // ---------------------------------------------------------------------------
  33. class VSyncSource : public virtual RefBase {
  34. public:
  35. class Callback: public virtual RefBase {
  36. public:
  37. virtual ~Callback() {}
  38. virtual void onVSyncEvent(nsecs_t when) = 0;
  39. };
  40. virtual ~VSyncSource() {}
  41. virtual void setVSyncEnabled(bool enable) = 0;
  42. virtual void setCallback(const sp<Callback>& callback) = 0;
  43. virtual void setPhaseOffset(nsecs_t phaseOffset) = 0;
  44. };
  45. class EventThread : public Thread, private VSyncSource::Callback {
  46. class Connection : public BnDisplayEventConnection {
  47. public:
  48. Connection(const sp<EventThread>& eventThread);
  49. status_t postEvent(const DisplayEventReceiver::Event& event);
  50. // count >= 1 : continuous event. count is the vsync rate
  51. // count == 0 : one-shot event that has not fired
  52. // count ==-1 : one-shot event that fired this round / disabled
  53. int32_t count;
  54. private:
  55. virtual ~Connection();
  56. virtual void onFirstRef();
  57. virtual sp<BitTube> getDataChannel() const;
  58. virtual void setVsyncRate(uint32_t count);
  59. virtual void requestNextVsync(); // asynchronous
  60. sp<EventThread> const mEventThread;
  61. sp<BitTube> const mChannel;
  62. };
  63. public:
  64. EventThread(const sp<VSyncSource>& src);
  65. sp<Connection> createEventConnection() const;
  66. status_t registerDisplayEventConnection(const sp<Connection>& connection);
  67. void setVsyncRate(uint32_t count, const sp<Connection>& connection);
  68. void requestNextVsync(const sp<Connection>& connection);
  69. // called before the screen is turned off from main thread
  70. void onScreenReleased();
  71. // called after the screen is turned on from main thread
  72. void onScreenAcquired();
  73. // called when receiving a hotplug event
  74. void onHotplugReceived(int type, bool connected);
  75. Vector< sp<EventThread::Connection> > waitForEvent(
  76. DisplayEventReceiver::Event* event);
  77. void dump(String8& result) const;
  78. void sendVsyncHintOff();
  79. void setPhaseOffset(nsecs_t phaseOffset);
  80. private:
  81. virtual bool threadLoop();
  82. virtual void onFirstRef();
  83. virtual void onVSyncEvent(nsecs_t timestamp);
  84. void removeDisplayEventConnection(const wp<Connection>& connection);
  85. void enableVSyncLocked();
  86. void disableVSyncLocked();
  87. void sendVsyncHintOnLocked();
  88. // constants
  89. sp<VSyncSource> mVSyncSource;
  90. PowerHAL mPowerHAL;
  91. mutable Mutex mLock;
  92. mutable Condition mCondition;
  93. // protected by mLock
  94. SortedVector< wp<Connection> > mDisplayEventConnections;
  95. Vector< DisplayEventReceiver::Event > mPendingEvents;
  96. DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
  97. bool mUseSoftwareVSync;
  98. bool mVsyncEnabled;
  99. // for debugging
  100. bool mDebugVsyncEnabled;
  101. bool mVsyncHintSent;
  102. timer_t mTimerId;
  103. };
  104. // ---------------------------------------------------------------------------
  105. }; // namespace android
  106. // ---------------------------------------------------------------------------
  107. #endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */