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 17 #pragma once 18 19 #include <stdint.h> 20 #include <sys/types.h> 21 #include <condition_variable> 22 #include <mutex> 23 #include <thread> 24 25 #include <android-base/thread_annotations.h> 26 27 #include <gui/DisplayEventReceiver.h> 28 #include <gui/IDisplayEventConnection.h> 29 #include <private/gui/BitTube.h> 30 31 #include <utils/Errors.h> 32 #include <utils/SortedVector.h> 33 34 #include "DisplayDevice.h" 35 36 // --------------------------------------------------------------------------- 37 namespace android { 38 // --------------------------------------------------------------------------- 39 40 class EventThreadTest; 41 class SurfaceFlinger; 42 class String8; 43 44 // --------------------------------------------------------------------------- 45 46 class VSyncSource { 47 public: 48 class Callback { 49 public: ~Callback()50 virtual ~Callback() {} 51 virtual void onVSyncEvent(nsecs_t when) = 0; 52 }; 53 ~VSyncSource()54 virtual ~VSyncSource() {} 55 virtual void setVSyncEnabled(bool enable) = 0; 56 virtual void setCallback(Callback* callback) = 0; 57 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; 58 }; 59 60 class EventThread { 61 public: 62 virtual ~EventThread(); 63 64 virtual sp<BnDisplayEventConnection> createEventConnection() const = 0; 65 66 // called before the screen is turned off from main thread 67 virtual void onScreenReleased() = 0; 68 69 // called after the screen is turned on from main thread 70 virtual void onScreenAcquired() = 0; 71 72 // called when receiving a hotplug event 73 virtual void onHotplugReceived(int type, bool connected) = 0; 74 75 virtual void dump(String8& result) const = 0; 76 77 virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; 78 }; 79 80 namespace impl { 81 82 class EventThread : public android::EventThread, private VSyncSource::Callback { 83 class Connection : public BnDisplayEventConnection { 84 public: 85 explicit Connection(EventThread* eventThread); 86 virtual ~Connection(); 87 88 virtual status_t postEvent(const DisplayEventReceiver::Event& event); 89 90 // count >= 1 : continuous event. count is the vsync rate 91 // count == 0 : one-shot event that has not fired 92 // count ==-1 : one-shot event that fired this round / disabled 93 int32_t count; 94 95 private: 96 virtual void onFirstRef(); 97 status_t stealReceiveChannel(gui::BitTube* outChannel) override; 98 status_t setVsyncRate(uint32_t count) override; 99 void requestNextVsync() override; // asynchronous 100 EventThread* const mEventThread; 101 gui::BitTube mChannel; 102 }; 103 104 public: 105 using ResyncWithRateLimitCallback = std::function<void()>; 106 using InterceptVSyncsCallback = std::function<void(nsecs_t)>; 107 108 EventThread(VSyncSource* src, ResyncWithRateLimitCallback resyncWithRateLimitCallback, 109 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName); 110 ~EventThread(); 111 112 sp<BnDisplayEventConnection> createEventConnection() const override; 113 status_t registerDisplayEventConnection(const sp<Connection>& connection); 114 115 void setVsyncRate(uint32_t count, const sp<Connection>& connection); 116 void requestNextVsync(const sp<Connection>& connection); 117 118 // called before the screen is turned off from main thread 119 void onScreenReleased() override; 120 121 // called after the screen is turned on from main thread 122 void onScreenAcquired() override; 123 124 // called when receiving a hotplug event 125 void onHotplugReceived(int type, bool connected) override; 126 127 void dump(String8& result) const override; 128 129 void setPhaseOffset(nsecs_t phaseOffset) override; 130 131 private: 132 friend EventThreadTest; 133 134 void threadMain(); 135 Vector<sp<EventThread::Connection>> waitForEventLocked(std::unique_lock<std::mutex>* lock, 136 DisplayEventReceiver::Event* event) 137 REQUIRES(mMutex); 138 139 void removeDisplayEventConnectionLocked(const wp<Connection>& connection) REQUIRES(mMutex); 140 void enableVSyncLocked() REQUIRES(mMutex); 141 void disableVSyncLocked() REQUIRES(mMutex); 142 143 // Implements VSyncSource::Callback 144 void onVSyncEvent(nsecs_t timestamp) override; 145 146 // constants 147 VSyncSource* const mVSyncSource GUARDED_BY(mMutex) = nullptr; 148 const ResyncWithRateLimitCallback mResyncWithRateLimitCallback; 149 const InterceptVSyncsCallback mInterceptVSyncsCallback; 150 151 std::thread mThread; 152 mutable std::mutex mMutex; 153 mutable std::condition_variable mCondition; 154 155 // protected by mLock 156 SortedVector<wp<Connection>> mDisplayEventConnections GUARDED_BY(mMutex); 157 Vector<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex); 158 DisplayEventReceiver::Event mVSyncEvent[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES] GUARDED_BY( 159 mMutex); 160 bool mUseSoftwareVSync GUARDED_BY(mMutex) = false; 161 bool mVsyncEnabled GUARDED_BY(mMutex) = false; 162 bool mKeepRunning GUARDED_BY(mMutex) = true; 163 164 // for debugging 165 bool mDebugVsyncEnabled GUARDED_BY(mMutex) = false; 166 }; 167 168 // --------------------------------------------------------------------------- 169 170 } // namespace impl 171 } // namespace android 172