Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | #pragma once |
| 17 | |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 18 | #include <mutex> |
| 19 | #include <string> |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 20 | |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 21 | #include "EventThread.h" |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 22 | #include "TracedOrdinal.h" |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 23 | #include "VSyncDispatch.h" |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 24 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 25 | namespace android::scheduler { |
| 26 | class CallbackRepeater; |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 27 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 28 | class DispSyncSource final : public VSyncSource { |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 29 | public: |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 30 | DispSyncSource(VSyncDispatch& vSyncDispatch, std::chrono::nanoseconds workDuration, |
| 31 | std::chrono::nanoseconds readyDuration, bool traceVsync, const char* name); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 32 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 33 | ~DispSyncSource() override; |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 34 | |
| 35 | // The following methods are implementation of VSyncSource. |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 36 | const char* getName() const override { return mName; } |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 37 | void setVSyncEnabled(bool enable) override; |
| 38 | void setCallback(VSyncSource::Callback* callback) override; |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 39 | void setDuration(std::chrono::nanoseconds workDuration, |
| 40 | std::chrono::nanoseconds readyDuration) override; |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 41 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 42 | void dump(std::string&) const override; |
| 43 | |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 44 | private: |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 45 | void onVsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime); |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 46 | |
| 47 | const char* const mName; |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 48 | TracedOrdinal<int> mValue; |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 49 | |
| 50 | const bool mTraceVsync; |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 51 | const std::string mVsyncOnLabel; |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 52 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 53 | std::unique_ptr<CallbackRepeater> mCallbackRepeater; |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 54 | |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 55 | std::mutex mCallbackMutex; |
| 56 | VSyncSource::Callback* mCallback GUARDED_BY(mCallbackMutex) = nullptr; |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 57 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 58 | mutable std::mutex mVsyncMutex; |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 59 | TracedOrdinal<std::chrono::nanoseconds> mWorkDuration GUARDED_BY(mVsyncMutex); |
| 60 | std::chrono::nanoseconds mReadyDuration GUARDED_BY(mVsyncMutex); |
Ana Krulec | 072233f | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 61 | bool mEnabled GUARDED_BY(mVsyncMutex) = false; |
Ana Krulec | 241cf83 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 64 | } // namespace android::scheduler |