1 /*
2  * Copyright 2020 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 #define LOG_TAG "StatsPullAtomService"
18 
19 #include <jni.h>
20 #include <log/log.h>
21 #include <nativehelper/JNIHelp.h>
22 #include <stats_event.h>
23 #include <stats_pull_atom_callback.h>
24 #include <statslog.h>
25 
26 #include "stats/SurfaceFlingerPuller.h"
27 
28 namespace android {
29 
30 static server::stats::SurfaceFlingerPuller gSurfaceFlingerPuller;
31 
onSurfaceFlingerPullCallback(int32_t atom_tag,AStatsEventList * data,void * cookie)32 static AStatsManager_PullAtomCallbackReturn onSurfaceFlingerPullCallback(int32_t atom_tag,
33                                                                          AStatsEventList* data,
34                                                                          void* cookie) {
35     return gSurfaceFlingerPuller.pull(atom_tag, data);
36 }
37 
initializeNativePullers(JNIEnv * env,jobject javaObject)38 static void initializeNativePullers(JNIEnv* env, jobject javaObject) {
39     // Surface flinger layer & global info.
40     gSurfaceFlingerPuller = server::stats::SurfaceFlingerPuller();
41     AStatsManager_setPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
42                                       /* metadata= */ nullptr, onSurfaceFlingerPullCallback,
43                                       /* cookie= */ nullptr);
44     AStatsManager_setPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
45                                       /* metadata= */ nullptr, onSurfaceFlingerPullCallback,
46                                       /* cookie= */ nullptr);
47 }
48 
49 static const JNINativeMethod sMethods[] = {
50         {"initializeNativePullers", "()V", (void*)initializeNativePullers}};
51 
register_android_server_stats_pull_StatsPullAtomService(JNIEnv * env)52 int register_android_server_stats_pull_StatsPullAtomService(JNIEnv* env) {
53     int res = jniRegisterNativeMethods(env, "com/android/server/stats/pull/StatsPullAtomService",
54                                        sMethods, NELEM(sMethods));
55     if (res < 0) {
56         ALOGE("failed to register native methods");
57     }
58     return res;
59 }
60 
61 } // namespace android