1 /*
2  * Copyright (C) 2015 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_EVENT_WORKER_H_
18 #define ANDROID_EVENT_WORKER_H_
19 
20 #include "drmdevice.h"
21 #include "worker.h"
22 
23 #include <stdint.h>
24 #include <map>
25 
26 #include <hardware/hardware.h>
27 #include <hardware/hwcomposer.h>
28 
29 namespace android {
30 
31 class VsyncCallback {
32  public:
~VsyncCallback()33      virtual ~VsyncCallback() {}
34      virtual void Callback(int display, int64_t timestamp) = 0;
35 };
36 
37 class VSyncWorker : public Worker {
38  public:
39      VSyncWorker();
40      ~VSyncWorker() override;
41 
42      int Init(DrmDevice *drm, int display);
43      void RegisterCallback(std::shared_ptr<VsyncCallback> callback);
44 
45      void VSyncControl(bool enabled);
46 
47  protected:
48      void Routine() override;
49 
50  private:
51      int GetPhasedVSync(int64_t frame_ns, int64_t &expect);
52      int SyntheticWaitVBlank(int64_t &timestamp);
53 
54      DrmDevice *drm_;
55 
56      // shared_ptr since we need to use this outside of the thread lock (to
57      // actually call the hook) and we don't want the memory freed until we're
58      // done
59      std::shared_ptr<VsyncCallback> callback_ = NULL;
60 
61      int display_;
62      std::atomic_bool enabled_;
63      int64_t last_timestamp_;
64 };
65 }  // namespace android
66 
67 #endif
68