xref: /aosp_15_r20/external/oboe/src/common/AdpfWrapper.h (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1*05767d91SRobert Wu /*
2*05767d91SRobert Wu  * Copyright 2021 The Android Open Source Project
3*05767d91SRobert Wu  *
4*05767d91SRobert Wu  * Licensed under the Apache License, Version 2.0 (the "License");
5*05767d91SRobert Wu  * you may not use this file except in compliance with the License.
6*05767d91SRobert Wu  * You may obtain a copy of the License at
7*05767d91SRobert Wu  *
8*05767d91SRobert Wu  *      http://www.apache.org/licenses/LICENSE-2.0
9*05767d91SRobert Wu  *
10*05767d91SRobert Wu  * Unless required by applicable law or agreed to in writing, software
11*05767d91SRobert Wu  * distributed under the License is distributed on an "AS IS" BASIS,
12*05767d91SRobert Wu  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*05767d91SRobert Wu  * See the License for the specific language governing permissions and
14*05767d91SRobert Wu  * limitations under the License.
15*05767d91SRobert Wu  */
16*05767d91SRobert Wu 
17*05767d91SRobert Wu #ifndef SYNTHMARK_ADPF_WRAPPER_H
18*05767d91SRobert Wu #define SYNTHMARK_ADPF_WRAPPER_H
19*05767d91SRobert Wu 
20*05767d91SRobert Wu #include <algorithm>
21*05767d91SRobert Wu #include <functional>
22*05767d91SRobert Wu #include <stdint.h>
23*05767d91SRobert Wu #include <sys/types.h>
24*05767d91SRobert Wu #include <unistd.h>
25*05767d91SRobert Wu #include <mutex>
26*05767d91SRobert Wu 
27*05767d91SRobert Wu struct APerformanceHintManager;
28*05767d91SRobert Wu struct APerformanceHintSession;
29*05767d91SRobert Wu 
30*05767d91SRobert Wu typedef struct APerformanceHintManager APerformanceHintManager;
31*05767d91SRobert Wu typedef struct APerformanceHintSession APerformanceHintSession;
32*05767d91SRobert Wu 
33*05767d91SRobert Wu class AdpfWrapper {
34*05767d91SRobert Wu public:
35*05767d91SRobert Wu      /**
36*05767d91SRobert Wu       * Create an ADPF session that can be used to boost performance.
37*05767d91SRobert Wu       * @param threadId
38*05767d91SRobert Wu       * @param targetDurationNanos - nominal period of isochronous task
39*05767d91SRobert Wu       * @return zero or negative error
40*05767d91SRobert Wu       */
41*05767d91SRobert Wu     int open(pid_t threadId,
42*05767d91SRobert Wu              int64_t targetDurationNanos);
43*05767d91SRobert Wu 
isOpen()44*05767d91SRobert Wu     bool isOpen() const {
45*05767d91SRobert Wu         return (mHintSession != nullptr);
46*05767d91SRobert Wu     }
47*05767d91SRobert Wu 
48*05767d91SRobert Wu     void close();
49*05767d91SRobert Wu 
50*05767d91SRobert Wu     /**
51*05767d91SRobert Wu      * Call this at the beginning of the callback that you are measuring.
52*05767d91SRobert Wu      */
53*05767d91SRobert Wu     void onBeginCallback();
54*05767d91SRobert Wu 
55*05767d91SRobert Wu     /**
56*05767d91SRobert Wu      * Call this at the end of the callback that you are measuring.
57*05767d91SRobert Wu      * It is OK to skip this if you have a short callback.
58*05767d91SRobert Wu      */
59*05767d91SRobert Wu     void onEndCallback(double durationScaler);
60*05767d91SRobert Wu 
61*05767d91SRobert Wu     /**
62*05767d91SRobert Wu      * For internal use only!
63*05767d91SRobert Wu      * This is a hack for communicating with experimental versions of ADPF.
64*05767d91SRobert Wu      * @param enabled
65*05767d91SRobert Wu      */
setUseAlternative(bool enabled)66*05767d91SRobert Wu     static void setUseAlternative(bool enabled) {
67*05767d91SRobert Wu         sUseAlternativeHack = enabled;
68*05767d91SRobert Wu     }
69*05767d91SRobert Wu 
70*05767d91SRobert Wu     /**
71*05767d91SRobert Wu      * Report the measured duration of a callback.
72*05767d91SRobert Wu      * This is normally called by onEndCallback().
73*05767d91SRobert Wu      * You may want to call this directly in order to give an advance hint of a jump in workload.
74*05767d91SRobert Wu      * @param actualDurationNanos
75*05767d91SRobert Wu      */
76*05767d91SRobert Wu     void reportActualDuration(int64_t actualDurationNanos);
77*05767d91SRobert Wu 
78*05767d91SRobert Wu private:
79*05767d91SRobert Wu     std::mutex               mLock;
80*05767d91SRobert Wu     APerformanceHintSession* mHintSession = nullptr;
81*05767d91SRobert Wu     int64_t                  mBeginCallbackNanos = 0;
82*05767d91SRobert Wu     static bool              sUseAlternativeHack;
83*05767d91SRobert Wu };
84*05767d91SRobert Wu 
85*05767d91SRobert Wu #endif //SYNTHMARK_ADPF_WRAPPER_H
86