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