• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.window;
18 
19 import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
20 import static android.view.Surface.FRAME_RATE_CATEGORY_DEFAULT;
21 import static android.view.Surface.FRAME_RATE_CATEGORY_HIGH;
22 import static android.view.SurfaceControl.FRAME_RATE_SELECTION_STRATEGY_OVERRIDE_CHILDREN;
23 import static android.view.SurfaceControl.FRAME_RATE_SELECTION_STRATEGY_PROPAGATE;
24 
25 import android.annotation.IntDef;
26 import android.annotation.NonNull;
27 import android.annotation.Nullable;
28 import android.content.Context;
29 import android.os.PerformanceHintManager;
30 import android.os.Trace;
31 import android.util.Log;
32 import android.view.Display;
33 import android.view.SurfaceControl;
34 
35 import java.io.PrintWriter;
36 import java.util.ArrayList;
37 import java.util.function.Supplier;
38 
39 /**
40  * A helper class to manage performance related hints for a process.  This helper is used for both
41  * long-lived and transient hints.
42  *
43  * @hide
44  */
45 public class SystemPerformanceHinter {
46     private static final String TAG = "SystemPerformanceHinter";
47 
48     private static final int HINT_NO_OP = 0;
49     // Change app and SF wakeup times to allow sf more time to composite a frame
50     public static final int HINT_SF_EARLY_WAKEUP = 1 << 0;
51     // Force max refresh rate
52     public static final int HINT_SF_FRAME_RATE = 1 << 1;
53     // Boost CPU & GPU clocks
54     public static final int HINT_ADPF = 1 << 2;
55     // Convenience constant for SF only flags
56     public static final int HINT_SF = HINT_SF_EARLY_WAKEUP | HINT_SF_FRAME_RATE;
57     // Convenience constant for all the flags
58     public static final int HINT_ALL = HINT_SF_EARLY_WAKEUP | HINT_SF_FRAME_RATE | HINT_ADPF;
59 
60     // Hints that are applied per-display and require a display root surface
61     private static final int HINT_PER_DISPLAY = HINT_SF_FRAME_RATE;
62     // Hints that are global (not per-display)
63     private static final int HINT_GLOBAL = HINT_SF_EARLY_WAKEUP | HINT_ADPF;
64 
65     @IntDef(prefix = {"HINT_"}, value = {
66             HINT_SF_EARLY_WAKEUP,
67             HINT_SF_FRAME_RATE,
68             HINT_ADPF,
69     })
70     private @interface HintFlags {}
71 
72     /**
73      * A provider for the root to apply SurfaceControl hints which will be inherited by all children
74      * of that root.
75      * @hide
76      */
77     public interface DisplayRootProvider {
78         /**
79          * @return the SurfaceControl to apply hints for the given displayId.
80          */
getRootForDisplay(int displayId)81         @Nullable SurfaceControl getRootForDisplay(int displayId);
82     }
83 
84     /**
85      * A session where high performance is needed.
86      * @hide
87      */
88     public class HighPerfSession implements AutoCloseable {
89         private final @HintFlags int hintFlags;
90         private final String reason;
91         private final int displayId;
92         private String mTraceName;
93 
HighPerfSession(@intFlags int hintFlags, int displayId, @NonNull String reason)94         protected HighPerfSession(@HintFlags int hintFlags, int displayId, @NonNull String reason) {
95             this.hintFlags = hintFlags;
96             this.reason = reason;
97             this.displayId = displayId;
98         }
99 
100         /** Makes this session active. It is no-op if this session is already active. */
start()101         public void start() {
102             if (!mActiveSessions.contains(this)) {
103                 startSession(this);
104             }
105         }
106 
107         /**
108          * Closes this session.
109          */
110         @Override
close()111         public void close() {
112             endSession(this);
113         }
114 
115         @Override
finalize()116         public void finalize() {
117             close();
118         }
119 
asyncTraceBegin()120         boolean asyncTraceBegin() {
121             if (!Trace.isTagEnabled(mTraceTag)) {
122                 mTraceName = null;
123                 return false;
124             }
125             if (mTraceName == null) {
126                 mTraceName = "PerfSession-d" + displayId + "-" + reason;
127             }
128             Trace.asyncTraceForTrackBegin(mTraceTag, TAG, mTraceName,
129                     System.identityHashCode(this));
130             return true;
131         }
132 
asyncTraceEnd()133         boolean asyncTraceEnd() {
134             if (mTraceName == null) {
135                 return false;
136             }
137             Trace.asyncTraceForTrackEnd(mTraceTag, TAG, System.identityHashCode(this));
138             return true;
139         }
140     }
141 
142     /**
143      * A no-op implementation of a session.
144      */
145     private class NoOpHighPerfSession extends HighPerfSession {
NoOpHighPerfSession()146         public NoOpHighPerfSession() {
147             super(HINT_NO_OP, Display.INVALID_DISPLAY, "");
148         }
149 
150         @Override
start()151         public void start() {
152         }
153 
154         @Override
close()155         public void close() {
156             // Do nothing
157         }
158     }
159 
160     /** The tag category of trace. */
161     public long mTraceTag = Trace.TRACE_TAG_APP;
162 
163     // The active sessions
164     private final ArrayList<HighPerfSession> mActiveSessions = new ArrayList<>();
165     private final SurfaceControl.Transaction mTransaction;
166     private @Nullable PerformanceHintManager.Session mAdpfSession;
167     private @Nullable DisplayRootProvider mDisplayRootProvider;
168 
169     /**
170      * Constructor for the hinter.
171      * @hide
172      */
SystemPerformanceHinter(@onNull Context context, @Nullable DisplayRootProvider displayRootProvider)173     public SystemPerformanceHinter(@NonNull Context context,
174             @Nullable DisplayRootProvider displayRootProvider) {
175         this(context, displayRootProvider, null /* transactionSupplier */);
176     }
177 
178     /**
179      * Constructor for the hinter.
180      * @hide
181      */
SystemPerformanceHinter(@onNull Context context, @Nullable DisplayRootProvider displayRootProvider, @Nullable Supplier<SurfaceControl.Transaction> transactionSupplier)182     public SystemPerformanceHinter(@NonNull Context context,
183             @Nullable DisplayRootProvider displayRootProvider,
184             @Nullable Supplier<SurfaceControl.Transaction> transactionSupplier) {
185         mDisplayRootProvider = displayRootProvider;
186         mTransaction = transactionSupplier != null
187                 ? transactionSupplier.get()
188                 : new SurfaceControl.Transaction();
189     }
190 
191     /**
192      * Sets the current ADPF session, required if you are using HINT_ADPF.  It is the responsibility
193      * of the caller to manage up the ADPF session.
194      * @hide
195      */
setAdpfSession(PerformanceHintManager.Session adpfSession)196     public void setAdpfSession(PerformanceHintManager.Session adpfSession) {
197         mAdpfSession = adpfSession;
198     }
199 
200     /** Creates a session that requires high performance. */
createSession(@intFlags int hintFlags, int displayId, @NonNull String reason)201     public HighPerfSession createSession(@HintFlags int hintFlags, int displayId,
202             @NonNull String reason) {
203         if (hintFlags == HINT_NO_OP) {
204             throw new IllegalArgumentException("Not allow empty hint flags");
205         }
206         if (mDisplayRootProvider == null && (hintFlags & HINT_SF_FRAME_RATE) != 0) {
207             throw new IllegalArgumentException(
208                     "Using SF frame rate hints requires a valid display root provider");
209         }
210         if (mAdpfSession == null && (hintFlags & HINT_ADPF) != 0) {
211             throw new IllegalArgumentException("Using ADPF hints requires an ADPF session");
212         }
213         if ((hintFlags & HINT_PER_DISPLAY) != 0) {
214             if (mDisplayRootProvider.getRootForDisplay(displayId) == null) {
215                 // Just log an error and return early if there is no root as there could be races
216                 // between when a display root is removed and when a hint session is requested
217                 Log.v(TAG, "No display root for displayId=" + displayId);
218                 Trace.instant(TRACE_TAG_WINDOW_MANAGER, "PerfHint-NoDisplayRoot: " + displayId);
219                 return new NoOpHighPerfSession();
220             }
221         }
222         return new HighPerfSession(hintFlags, displayId, reason);
223     }
224 
225     /**
226      * Starts a new session that requires high performance.
227      */
startSession(@intFlags int hintFlags, int displayId, @NonNull String reason)228     public HighPerfSession startSession(@HintFlags int hintFlags, int displayId,
229             @NonNull String reason) {
230         final HighPerfSession session = createSession(hintFlags, displayId, reason);
231         if (session.hintFlags != HINT_NO_OP) {
232             startSession(session);
233         }
234         return session;
235     }
236 
237     /** Starts the session that requires high performance. */
startSession(HighPerfSession session)238     private void startSession(HighPerfSession session) {
239         final boolean isTraceEnabled = session.asyncTraceBegin();
240         int oldGlobalFlags = calculateActiveHintFlags(HINT_GLOBAL);
241         int oldPerDisplayFlags = calculateActiveHintFlagsForDisplay(HINT_PER_DISPLAY,
242                 session.displayId);
243         mActiveSessions.add(session);
244         int newGlobalFlags = calculateActiveHintFlags(HINT_GLOBAL);
245         int newPerDisplayFlags = calculateActiveHintFlagsForDisplay(HINT_PER_DISPLAY,
246                 session.displayId);
247 
248         boolean transactionChanged = false;
249         // Per-display flags
250         if (nowEnabled(oldPerDisplayFlags, newPerDisplayFlags, HINT_SF_FRAME_RATE)) {
251             SurfaceControl displaySurfaceControl = mDisplayRootProvider.getRootForDisplay(
252                     session.displayId);
253             mTransaction.setFrameRateSelectionStrategy(displaySurfaceControl,
254                     FRAME_RATE_SELECTION_STRATEGY_OVERRIDE_CHILDREN);
255             // smoothSwitchOnly is false to request a higher framerate, even if it means switching
256             // the display mode will cause would jank on non-VRR devices because keeping a lower
257             // refresh rate would mean a poorer user experience.
258             mTransaction.setFrameRateCategory(
259                     displaySurfaceControl, FRAME_RATE_CATEGORY_HIGH, /* smoothSwitchOnly= */ false);
260             transactionChanged = true;
261             if (isTraceEnabled) {
262                 asyncTraceBegin(HINT_SF_FRAME_RATE, session.displayId);
263             }
264         }
265 
266         // Global flags
267         if (nowEnabled(oldGlobalFlags, newGlobalFlags, HINT_SF_EARLY_WAKEUP)) {
268             mTransaction.setEarlyWakeupStart();
269             transactionChanged = true;
270             if (isTraceEnabled) {
271                 asyncTraceBegin(HINT_SF_EARLY_WAKEUP, Display.INVALID_DISPLAY);
272             }
273         }
274         if (mAdpfSession != null && nowEnabled(oldGlobalFlags, newGlobalFlags, HINT_ADPF)) {
275             mAdpfSession.sendHint(PerformanceHintManager.Session.CPU_LOAD_UP);
276             if (isTraceEnabled) {
277                 asyncTraceBegin(HINT_ADPF, Display.INVALID_DISPLAY);
278             }
279         }
280         if (transactionChanged) {
281             mTransaction.applyAsyncUnsafe();
282         }
283     }
284 
285     /**
286      * Ends a session that requires high performance.
287      */
endSession(HighPerfSession session)288     private void endSession(HighPerfSession session) {
289         final boolean isTraceEnabled = session.asyncTraceEnd();
290         int oldGlobalFlags = calculateActiveHintFlags(HINT_GLOBAL);
291         int oldPerDisplayFlags = calculateActiveHintFlagsForDisplay(HINT_PER_DISPLAY,
292                 session.displayId);
293         mActiveSessions.remove(session);
294         int newGlobalFlags = calculateActiveHintFlags(HINT_GLOBAL);
295         int newPerDisplayFlags = calculateActiveHintFlagsForDisplay(HINT_PER_DISPLAY,
296                 session.displayId);
297 
298         boolean transactionChanged = false;
299         // Per-display flags
300         if (nowDisabled(oldPerDisplayFlags, newPerDisplayFlags, HINT_SF_FRAME_RATE)) {
301             SurfaceControl displaySurfaceControl = mDisplayRootProvider.getRootForDisplay(
302                     session.displayId);
303             mTransaction.setFrameRateSelectionStrategy(displaySurfaceControl,
304                     FRAME_RATE_SELECTION_STRATEGY_PROPAGATE);
305             // smoothSwitchOnly is false to request a higher framerate, even if it means switching
306             // the display mode will cause would jank on non-VRR devices because keeping a lower
307             // refresh rate would mean a poorer user experience.
308             mTransaction.setFrameRateCategory(displaySurfaceControl, FRAME_RATE_CATEGORY_DEFAULT,
309                     /* smoothSwitchOnly= */ false);
310             transactionChanged = true;
311             if (isTraceEnabled) {
312                 asyncTraceEnd(HINT_SF_FRAME_RATE);
313             }
314         }
315 
316         // Global flags
317         if (nowDisabled(oldGlobalFlags, newGlobalFlags, HINT_SF_EARLY_WAKEUP)) {
318             mTransaction.setEarlyWakeupEnd();
319             transactionChanged = true;
320             if (isTraceEnabled) {
321                 asyncTraceEnd(HINT_SF_EARLY_WAKEUP);
322             }
323         }
324         if (mAdpfSession != null && nowDisabled(oldGlobalFlags, newGlobalFlags, HINT_ADPF)) {
325             mAdpfSession.sendHint(PerformanceHintManager.Session.CPU_LOAD_RESET);
326             if (isTraceEnabled) {
327                 asyncTraceEnd(HINT_ADPF);
328             }
329         }
330         if (transactionChanged) {
331             mTransaction.applyAsyncUnsafe();
332         }
333     }
334 
335     /**
336      * Checks if checkFlags was previously not set and is now set.
337      */
nowEnabled(@intFlags int oldFlags, @HintFlags int newFlags, @HintFlags int checkFlags)338     private boolean nowEnabled(@HintFlags int oldFlags, @HintFlags int newFlags,
339                                @HintFlags int checkFlags) {
340         return (oldFlags & checkFlags) == 0 && (newFlags & checkFlags) != 0;
341     }
342 
343     /**
344      * Checks if checkFlags was previously set and is now not set.
345      */
nowDisabled(@intFlags int oldFlags, @HintFlags int newFlags, @HintFlags int checkFlags)346     private boolean nowDisabled(@HintFlags int oldFlags, @HintFlags int newFlags,
347                                 @HintFlags int checkFlags) {
348         return (oldFlags & checkFlags) != 0 && (newFlags & checkFlags) == 0;
349     }
350 
351     /**
352      * @return the combined hint flags for all active sessions, filtered by {@param filterFlags}.
353      */
calculateActiveHintFlags(@intFlags int filterFlags)354     private @HintFlags int calculateActiveHintFlags(@HintFlags int filterFlags) {
355         int flags = 0;
356         for (int i = 0; i < mActiveSessions.size(); i++) {
357             flags |= mActiveSessions.get(i).hintFlags & filterFlags;
358         }
359         return flags;
360     }
361 
362     /**
363      * @return the combined hint flags for all active sessions for a given display, filtered by
364      *         {@param filterFlags}.
365      */
calculateActiveHintFlagsForDisplay(@intFlags int filterFlags, int displayId)366     private @HintFlags int calculateActiveHintFlagsForDisplay(@HintFlags int filterFlags,
367             int displayId) {
368         int flags = 0;
369         for (int i = 0; i < mActiveSessions.size(); i++) {
370             final HighPerfSession session = mActiveSessions.get(i);
371             if (session.displayId == displayId) {
372                 flags |= mActiveSessions.get(i).hintFlags & filterFlags;
373             }
374         }
375         return flags;
376     }
377 
asyncTraceBegin(@intFlags int flag, int displayId)378     private void asyncTraceBegin(@HintFlags int flag, int displayId) {
379         final String prefix = switch (flag) {
380             case HINT_SF_EARLY_WAKEUP -> "PerfHint-early_wakeup";
381             case HINT_SF_FRAME_RATE -> "PerfHint-framerate";
382             case HINT_ADPF -> "PerfHint-adpf";
383             default -> "PerfHint-" + flag;
384         };
385         final String name = displayId != Display.INVALID_DISPLAY
386                 ? (prefix + "-d" + displayId) : prefix;
387         Trace.asyncTraceForTrackBegin(mTraceTag, TAG, name,
388                 flag ^ System.identityHashCode(this));
389     }
390 
asyncTraceEnd(@intFlags int flag)391     private void asyncTraceEnd(@HintFlags int flag) {
392         Trace.asyncTraceForTrackEnd(mTraceTag, TAG, flag ^ System.identityHashCode(this));
393     }
394 
395     /**
396      * Dumps the existing sessions.
397      */
dump(PrintWriter pw, String prefix)398     public void dump(PrintWriter pw, String prefix) {
399         final String innerPrefix = prefix + "  ";
400         pw.println(prefix + TAG + ":");
401         pw.println(innerPrefix + "Active sessions (" + mActiveSessions.size() + "):");
402         for (int i = 0; i < mActiveSessions.size(); i++) {
403             final HighPerfSession s = mActiveSessions.get(i);
404             pw.println(innerPrefix + "  reason=" + s.reason
405                     + " flags=" + s.hintFlags
406                     + " display=" + s.displayId);
407         }
408     }
409 }
410