1 /* 2 * Copyright (C) 2023 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 package com.android.wm.shell.performance 17 18 import android.content.Context 19 import android.os.PerformanceHintManager 20 import android.os.Process 21 import android.window.SystemPerformanceHinter 22 import com.android.wm.shell.RootTaskDisplayAreaOrganizer 23 import com.android.wm.shell.sysui.ShellCommandHandler 24 import com.android.wm.shell.sysui.ShellInit 25 import java.io.PrintWriter 26 import java.util.concurrent.TimeUnit 27 28 /** 29 * Manages the performance hints to the system. 30 */ 31 class PerfHintController(private val mContext: Context, 32 shellInit: ShellInit, 33 private val mShellCommandHandler: ShellCommandHandler, 34 rootTdaOrganizer: RootTaskDisplayAreaOrganizer) { 35 36 // The system perf hinter 37 val hinter: SystemPerformanceHinter 38 39 init { 40 hinter = SystemPerformanceHinter(mContext, 41 rootTdaOrganizer.performanceRootProvider) 42 shellInit.addInitCallback(this::onInit, this) 43 } 44 onInitnull45 private fun onInit() { 46 mShellCommandHandler.addDumpCallback(this::dump, this) 47 val perfHintMgr = mContext.getSystemService(PerformanceHintManager::class.java) 48 if (perfHintMgr != null) { 49 val adpfSession = perfHintMgr.createHintSession( 50 intArrayOf(Process.myTid()), 51 TimeUnit.SECONDS.toNanos(1) 52 ) 53 if (adpfSession != null) { 54 hinter.setAdpfSession(adpfSession) 55 } 56 } 57 } 58 dumpnull59 fun dump(pw: PrintWriter, prefix: String?) { 60 hinter.dump(pw, prefix) 61 } 62 } 63