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