1d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian/*
2d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * Copyright (C) 2011 The Android Open Source Project
3d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian *
4d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * you may not use this file except in compliance with the License.
6d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * You may obtain a copy of the License at
7d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian *
8d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian *
10d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * See the License for the specific language governing permissions and
14d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian * limitations under the License.
15d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian */
16d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
17d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#ifndef ANDROID_SURFACE_FLINGER_EVENT_THREAD_H
18d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#define ANDROID_SURFACE_FLINGER_EVENT_THREAD_H
19d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
20d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <stdint.h>
21d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <sys/types.h>
22d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
23cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian#include <gui/DisplayEventReceiver.h>
24d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <gui/IDisplayEventConnection.h>
25d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
26d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <utils/Errors.h>
27d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include <utils/threads.h>
28cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian#include <utils/SortedVector.h>
29d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
303eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include "DisplayHardware/DisplayHardware.h"
313eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
32d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian// ---------------------------------------------------------------------------
33d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
34d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopiannamespace android {
35d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
36d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian// ---------------------------------------------------------------------------
37d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
38d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopianclass SurfaceFlinger;
39d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
40d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian// ---------------------------------------------------------------------------
41d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
423eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopianclass EventThread : public Thread, public DisplayHardware::VSyncHandler {
43cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    class Connection : public BnDisplayEventConnection {
44cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    public:
45cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        Connection(const sp<EventThread>& eventThread);
46cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        status_t postEvent(const DisplayEventReceiver::Event& event);
47d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
48cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        // count >= 1 : continuous event. count is the vsync rate
49cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        // count == 0 : one-shot event that has not fired
50cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        // count ==-1 : one-shot event that fired this round / disabled
51cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        // count ==-2 : one-shot event that fired the round before
52cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        int32_t count;
53d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
54cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    private:
55cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        virtual ~Connection();
56cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        virtual void onFirstRef();
57cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        virtual sp<BitTube> getDataChannel() const;
58cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        virtual void setVsyncRate(uint32_t count);
59cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        virtual void requestNextVsync();    // asynchronous
60cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        sp<EventThread> const mEventThread;
61cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian        sp<BitTube> const mChannel;
62cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    };
638aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian
64cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopianpublic:
65d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
66cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    EventThread(const sp<SurfaceFlinger>& flinger);
67d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
68cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    sp<Connection> createEventConnection() const;
69cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    status_t registerDisplayEventConnection(const sp<Connection>& connection);
70cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    status_t unregisterDisplayEventConnection(const wp<Connection>& connection);
71478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian
72cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    void setVsyncRate(uint32_t count, const sp<Connection>& connection);
73cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    void requestNextVsync(const sp<Connection>& connection);
74478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian
7522ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian    // called before the screen is turned off from main thread
7622ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian    void onScreenReleased();
7722ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian
7822ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian    // called after the screen is turned on from main thread
7922ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian    void onScreenAcquired();
808aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian
81d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    void dump(String8& result, char* buffer, size_t SIZE) const;
82d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
83d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopianprivate:
84d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    virtual bool        threadLoop();
85d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    virtual status_t    readyToRun();
86d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    virtual void        onFirstRef();
87e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    virtual void        onVSyncReceived(int, nsecs_t timestamp);
88d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
89cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    void removeDisplayEventConnection(const wp<Connection>& connection);
9022ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian    void enableVSyncLocked();
9122ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian    void disableVSyncLocked();
9223748668d33ac850e64d87e25ac4cc78679c9384Mathias Agopian
93d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    // constants
94d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    sp<SurfaceFlinger> mFlinger;
953eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    DisplayHardware& mHw;
96d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
97d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    mutable Mutex mLock;
98d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    mutable Condition mCondition;
99d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
100d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    // protected by mLock
101cb9732a951d20cacb7ebe2dab132b5738226b1b6Mathias Agopian    SortedVector< wp<Connection> > mDisplayEventConnections;
1028aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    nsecs_t mLastVSyncTimestamp;
1033eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    nsecs_t mVSyncTimestamp;
10422ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian    bool mUseSoftwareVSync;
105478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian
106478ae5eb5a0047e1b2988c896cff6363b455ee50Mathias Agopian    // main thread only
107d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    size_t mDeliveredEvents;
108e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
109e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    // for debugging
110e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    bool mDebugVsyncEnabled;
111d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian};
112d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
113d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian// ---------------------------------------------------------------------------
114d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
115d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian}; // namespace android
116d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
117d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian// ---------------------------------------------------------------------------
118d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
119d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#endif /* ANDROID_SURFACE_FLINGER_EVENT_THREAD_H */
120