xref: /aosp_15_r20/external/oboe/src/common/AdpfWrapper.h (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright 2021 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 SYNTHMARK_ADPF_WRAPPER_H
18 #define SYNTHMARK_ADPF_WRAPPER_H
19 
20 #include <algorithm>
21 #include <functional>
22 #include <stdint.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <mutex>
26 
27 struct APerformanceHintManager;
28 struct APerformanceHintSession;
29 
30 typedef struct APerformanceHintManager APerformanceHintManager;
31 typedef struct APerformanceHintSession APerformanceHintSession;
32 
33 class AdpfWrapper {
34 public:
35      /**
36       * Create an ADPF session that can be used to boost performance.
37       * @param threadId
38       * @param targetDurationNanos - nominal period of isochronous task
39       * @return zero or negative error
40       */
41     int open(pid_t threadId,
42              int64_t targetDurationNanos);
43 
isOpen()44     bool isOpen() const {
45         return (mHintSession != nullptr);
46     }
47 
48     void close();
49 
50     /**
51      * Call this at the beginning of the callback that you are measuring.
52      */
53     void onBeginCallback();
54 
55     /**
56      * Call this at the end of the callback that you are measuring.
57      * It is OK to skip this if you have a short callback.
58      */
59     void onEndCallback(double durationScaler);
60 
61     /**
62      * For internal use only!
63      * This is a hack for communicating with experimental versions of ADPF.
64      * @param enabled
65      */
setUseAlternative(bool enabled)66     static void setUseAlternative(bool enabled) {
67         sUseAlternativeHack = enabled;
68     }
69 
70     /**
71      * Report the measured duration of a callback.
72      * This is normally called by onEndCallback().
73      * You may want to call this directly in order to give an advance hint of a jump in workload.
74      * @param actualDurationNanos
75      */
76     void reportActualDuration(int64_t actualDurationNanos);
77 
78 private:
79     std::mutex               mLock;
80     APerformanceHintSession* mHintSession = nullptr;
81     int64_t                  mBeginCallbackNanos = 0;
82     static bool              sUseAlternativeHack;
83 };
84 
85 #endif //SYNTHMARK_ADPF_WRAPPER_H
86