1/* 2 * Copyright (C) 2017 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 "GraphicsStatsService" 18 19#include <jni.h> 20#include <log/log.h> 21#include <nativehelper/JNIHelp.h> 22#include <ScopedPrimitiveArray.h> 23#include <ScopedUtfChars.h> 24#include <JankTracker.h> 25#include <service/GraphicsStatsService.h> 26 27namespace android { 28 29using namespace android::uirenderer; 30 31static jint getAshmemSize(JNIEnv*, jobject) { 32 return sizeof(ProfileData); 33} 34 35static jlong createDump(JNIEnv*, jobject, jint fd, jboolean isProto) { 36 GraphicsStatsService::Dump* dump = GraphicsStatsService::createDump(fd, isProto 37 ? GraphicsStatsService::DumpType::Protobuf : GraphicsStatsService::DumpType::Text); 38 return reinterpret_cast<jlong>(dump); 39} 40 41static void addToDump(JNIEnv* env, jobject, jlong dumpPtr, jstring jpath, jstring jpackage, 42 jint versionCode, jlong startTime, jlong endTime, jbyteArray jdata) { 43 std::string path; 44 const ProfileData* data = nullptr; 45 LOG_ALWAYS_FATAL_IF(jdata == nullptr && jpath == nullptr, "Path and data can't both be null"); 46 if (jdata != nullptr) { 47 ScopedByteArrayRO buffer(env, jdata); 48 LOG_ALWAYS_FATAL_IF(buffer.size() != sizeof(ProfileData), 49 "Buffer size %zu doesn't match expected %zu!", buffer.size(), sizeof(ProfileData)); 50 data = reinterpret_cast<const ProfileData*>(buffer.get()); 51 } 52 if (jpath != nullptr) { 53 ScopedUtfChars pathChars(env, jpath); 54 LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), "Failed to get path chars"); 55 path.assign(pathChars.c_str(), pathChars.size()); 56 } 57 ScopedUtfChars packageChars(env, jpackage); 58 LOG_ALWAYS_FATAL_IF(packageChars.size() <= 0 || !packageChars.c_str(), "Failed to get path chars"); 59 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr); 60 LOG_ALWAYS_FATAL_IF(!dump, "null passed for dump pointer"); 61 62 const std::string package(packageChars.c_str(), packageChars.size()); 63 GraphicsStatsService::addToDump(dump, path, package, versionCode, startTime, endTime, data); 64} 65 66static void addFileToDump(JNIEnv* env, jobject, jlong dumpPtr, jstring jpath) { 67 ScopedUtfChars pathChars(env, jpath); 68 LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), "Failed to get path chars"); 69 const std::string path(pathChars.c_str(), pathChars.size()); 70 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr); 71 GraphicsStatsService::addToDump(dump, path); 72} 73 74static void finishDump(JNIEnv*, jobject, jlong dumpPtr) { 75 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr); 76 GraphicsStatsService::finishDump(dump); 77} 78 79static void saveBuffer(JNIEnv* env, jobject clazz, jstring jpath, jstring jpackage, 80 jint versionCode, jlong startTime, jlong endTime, jbyteArray jdata) { 81 ScopedByteArrayRO buffer(env, jdata); 82 LOG_ALWAYS_FATAL_IF(buffer.size() != sizeof(ProfileData), 83 "Buffer size %zu doesn't match expected %zu!", buffer.size(), sizeof(ProfileData)); 84 ScopedUtfChars pathChars(env, jpath); 85 LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), "Failed to get path chars"); 86 ScopedUtfChars packageChars(env, jpackage); 87 LOG_ALWAYS_FATAL_IF(packageChars.size() <= 0 || !packageChars.c_str(), "Failed to get path chars"); 88 89 const std::string path(pathChars.c_str(), pathChars.size()); 90 const std::string package(packageChars.c_str(), packageChars.size()); 91 const ProfileData* data = reinterpret_cast<const ProfileData*>(buffer.get()); 92 GraphicsStatsService::saveBuffer(path, package, versionCode, startTime, endTime, data); 93} 94 95static const JNINativeMethod sMethods[] = { 96 { "nGetAshmemSize", "()I", (void*) getAshmemSize }, 97 { "nCreateDump", "(IZ)J", (void*) createDump }, 98 { "nAddToDump", "(JLjava/lang/String;Ljava/lang/String;IJJ[B)V", (void*) addToDump }, 99 { "nAddToDump", "(JLjava/lang/String;)V", (void*) addFileToDump }, 100 { "nFinishDump", "(J)V", (void*) finishDump }, 101 { "nSaveBuffer", "(Ljava/lang/String;Ljava/lang/String;IJJ[B)V", (void*) saveBuffer }, 102}; 103 104int register_android_server_GraphicsStatsService(JNIEnv* env) 105{ 106 return jniRegisterNativeMethods(env, "com/android/server/GraphicsStatsService", 107 sMethods, NELEM(sMethods)); 108} 109 110} // namespace android