• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 
18 #pragma once
19 
20 #include "Thread.h"
21 
22 #include <thread>
23 #include <mutex>
24 
25 namespace swappy {
26 
27 class ChoreographerThread {
28 public:
29     enum class Type {
30         // choreographer ticks are provided by application
31         App,
32 
33         // register internally with choreographer
34         Swappy,
35     };
36 
37     using Callback = std::function<void()>;
38 
39     static std::unique_ptr<ChoreographerThread> createChoreographerThread(
40             Type type, JavaVM *vm, Callback onChoreographer);
41 
42     virtual ~ChoreographerThread() = 0;
43 
44     virtual void postFrameCallbacks();
45 
46 protected:
47     ChoreographerThread(Callback onChoreographer);
48     virtual void scheduleNextFrameCallback() REQUIRES(mWaitingMutex) = 0;
49     virtual void onChoreographer();
50 
51     std::mutex mWaitingMutex;
52     int mCallbacksBeforeIdle GUARDED_BY(mWaitingMutex) = 0;
53     Callback mCallback;
54 
55     static constexpr int MAX_CALLBACKS_BEFORE_IDLE = 10;
56 
57 private:
58     static int getSDKVersion(JavaVM *vm);
59     static bool isChoreographerCallbackClassLoaded(JavaVM *vm);
60 };
61 
62 } // namespace swappy
63