1 /*
2  * Copyright (C) 2019 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 #include "jni.h"
18 
19 #include <FrameInfo.h>
20 #include <FrameMetricsObserver.h>
21 
22 namespace android {
23 
24 /*
25  * Implements JNI layer for hwui frame metrics reporting.
26  */
27 class HardwareRendererObserver : public uirenderer::FrameMetricsObserver {
28 public:
29     HardwareRendererObserver(JavaVM* vm, jobject observer, bool waitForPresentTime);
30     ~HardwareRendererObserver();
31 
32     /**
33      * Retrieves frame metrics for the oldest frame that the renderer has retained. The renderer
34      * will retain a buffer until it has been retrieved, via this method, or its internal storage
35      * is exhausted at which point it informs the caller of how many frames it has failed to store
36      * since the last time this method was invoked.
37      * @param env java env required to populate the provided buffer array
38      * @param metrics output parameter that represents the buffer of metrics that is to be filled
39      * @param dropCount output parameter that is updated to reflect the number of buffers that were
40                         discarded since the last successful invocation of this method.
41      * @return true if there was data to populate the array and false otherwise. If false then
42      *         neither the metrics buffer or dropCount will be modified.
43      */
44     bool getNextBuffer(JNIEnv* env, jlongArray metrics, int* dropCount);
45 
46     void notify(const int64_t* stats) override;
47 
48 private:
49     static constexpr int kBufferSize = static_cast<int>(uirenderer::FrameInfoIndex::NumIndexes);
50     static constexpr int kRingSize = 3;
51 
52     class FrameMetricsNotification {
53     public:
FrameMetricsNotification()54         FrameMetricsNotification() {}
55 
56         std::atomic_bool hasData = false;
57         int64_t buffer[kBufferSize];
58         int dropCount = 0;
59     private:
60         // non-copyable
61         FrameMetricsNotification(const FrameMetricsNotification&) = delete;
62         FrameMetricsNotification& operator=(const FrameMetricsNotification& ) = delete;
63     };
64 
65     JavaVM* const mVm;
66     jobject mObserver;
67     bool mKeepListening = true;
68 
69     int mNextFree = 0;
70     int mNextInQueue = 0;
71     FrameMetricsNotification mRingBuffer[kRingSize];
72 
73     int mDroppedReports = 0;
74 };
75 
76 } // namespace android
77