The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 20 | #pragma clang diagnostic ignored "-Wextra" |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 21 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 22 | //#define LOG_NDEBUG 0 |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 23 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 24 | |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 25 | #include "SurfaceFlinger.h" |
Dominik Laskowski | 83b8821 | 2018-12-11 13:34:06 -0800 | [diff] [blame] | 26 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 27 | #include <aidl/android/hardware/power/Boost.h> |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 28 | #include <android-base/parseint.h> |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 29 | #include <android-base/properties.h> |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
| 31 | #include <android-base/strings.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 32 | #include <android/configuration.h> |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 33 | #include <android/gui/IDisplayEventConnection.h> |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 34 | #include <android/gui/StaticDisplayInfo.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 35 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| 36 | #include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h> |
| 37 | #include <android/hardware/configstore/1.1/types.h> |
Steven Thomas | 62a4cf8 | 2020-01-31 12:04:03 -0800 | [diff] [blame] | 38 | #include <android/native_window.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 39 | #include <android/os/IInputFlinger.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 40 | #include <binder/IPCThreadState.h> |
| 41 | #include <binder/IServiceManager.h> |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 42 | #include <binder/PermissionCache.h> |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 43 | #include <com_android_graphics_surfaceflinger_flags.h> |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 44 | #include <common/FlagManager.h> |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 45 | #include <common/trace.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 46 | #include <compositionengine/CompositionEngine.h> |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 47 | #include <compositionengine/CompositionRefreshArgs.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 48 | #include <compositionengine/Display.h> |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 49 | #include <compositionengine/DisplayColorProfile.h> |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 50 | #include <compositionengine/DisplayColorProfileCreationArgs.h> |
Lloyd Pique | 9370a48 | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 51 | #include <compositionengine/DisplayCreationArgs.h> |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 52 | #include <compositionengine/LayerFECompositionState.h> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 53 | #include <compositionengine/OutputLayer.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 54 | #include <compositionengine/RenderSurface.h> |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 55 | #include <compositionengine/impl/DisplayColorProfile.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 56 | #include <compositionengine/impl/OutputCompositionState.h> |
John Reck | db1a16b | 2021-07-09 13:05:52 -0400 | [diff] [blame] | 57 | #include <compositionengine/impl/OutputLayerCompositionState.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 58 | #include <configstore/Utils.h> |
| 59 | #include <cutils/compiler.h> |
| 60 | #include <cutils/properties.h> |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 61 | #include <fmt/format.h> |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 62 | #include <ftl/algorithm.h> |
Dominik Laskowski | 5d7de5f | 2022-11-03 12:38:32 -0400 | [diff] [blame] | 63 | #include <ftl/concat.h> |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 64 | #include <ftl/fake_guard.h> |
Dominik Laskowski | 4e2b71f | 2020-11-10 15:05:32 -0800 | [diff] [blame] | 65 | #include <ftl/future.h> |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 66 | #include <ftl/unit.h> |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 67 | #include <gui/AidlStatusUtil.h> |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 68 | #include <gui/BufferQueue.h> |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 69 | #include <gui/DebugEGLImageTracker.h> |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 70 | #include <gui/IProducerListener.h> |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 71 | #include <gui/LayerMetadata.h> |
Steven Thomas | 62a4cf8 | 2020-01-31 12:04:03 -0800 | [diff] [blame] | 72 | #include <gui/LayerState.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 73 | #include <gui/Surface.h> |
Brian Johnson | 51d8d5d | 2023-07-28 10:18:47 -0700 | [diff] [blame] | 74 | #include <gui/SurfaceComposerClient.h> |
Steven Moreland | 7c8af94 | 2020-07-08 18:35:51 +0000 | [diff] [blame] | 75 | #include <hidl/ServiceManagement.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 76 | #include <layerproto/LayerProtoParser.h> |
Elliott Hughes | e3ffd13 | 2023-06-28 19:45:15 +0000 | [diff] [blame] | 77 | #include <linux/sched/types.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 78 | #include <log/log.h> |
| 79 | #include <private/android_filesystem_config.h> |
| 80 | #include <private/gui/SyncFeatures.h> |
Wei Wang | 976a645 | 2021-06-11 15:26:00 -0700 | [diff] [blame] | 81 | #include <processgroup/processgroup.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 82 | #include <renderengine/RenderEngine.h> |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 83 | #include <renderengine/impl/ExternalTexture.h> |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 84 | #include <scheduler/FrameTargeter.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 85 | #include <sys/types.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 86 | #include <ui/ColorSpace.h> |
| 87 | #include <ui/DebugUtils.h> |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 88 | #include <ui/DisplayId.h> |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 89 | #include <ui/DisplayMode.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 90 | #include <ui/DisplayStatInfo.h> |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 91 | #include <ui/DisplayState.h> |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 92 | #include <ui/DynamicDisplayInfo.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 93 | #include <ui/GraphicBufferAllocator.h> |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 94 | #include <ui/HdrRenderTypeUtils.h> |
Vishnu Nair | afd527a | 2022-10-18 08:56:10 -0700 | [diff] [blame] | 95 | #include <ui/LayerStack.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 96 | #include <ui/PixelFormat.h> |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 97 | #include <ui/StaticDisplayInfo.h> |
Brian Johnson | 51d8d5d | 2023-07-28 10:18:47 -0700 | [diff] [blame] | 98 | #include <unistd.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | #include <utils/StopWatch.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 100 | #include <utils/String16.h> |
| 101 | #include <utils/String8.h> |
Yusuke Sato | 0a688f5 | 2015-07-30 23:05:39 -0700 | [diff] [blame] | 102 | #include <utils/Timers.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 103 | #include <utils/misc.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 104 | #include <algorithm> |
Dominik Laskowski | 4e2b71f | 2020-11-10 15:05:32 -0800 | [diff] [blame] | 105 | #include <cerrno> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 106 | #include <cinttypes> |
| 107 | #include <cmath> |
| 108 | #include <cstdint> |
Patrick Williams | 8b731c1 | 2023-10-24 09:56:24 -0500 | [diff] [blame] | 109 | #include <filesystem> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 110 | #include <functional> |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 111 | #include <memory> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 112 | #include <mutex> |
| 113 | #include <optional> |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 114 | #include <string> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 115 | #include <type_traits> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 116 | #include <unordered_map> |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 117 | #include <vector> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | |
Alec Mouri | 9b133ca | 2023-11-14 19:00:01 +0000 | [diff] [blame] | 119 | #include <common/FlagManager.h> |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 120 | #include <gui/LayerStatePermissions.h> |
Ady Abraham | 07d03c4 | 2023-09-27 19:15:08 -0700 | [diff] [blame] | 121 | #include <gui/SchedulingPolicy.h> |
Chavi Weingarten | f6fb445 | 2024-01-23 21:10:30 +0000 | [diff] [blame] | 122 | #include <gui/SyncScreenCaptureListener.h> |
Alec Mouri | ff79387 | 2022-01-13 17:45:06 -0800 | [diff] [blame] | 123 | #include <ui/DisplayIdentification.h> |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 124 | #include "BackgroundExecutor.h" |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 125 | #include "Client.h" |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 126 | #include "ClientCache.h" |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 127 | #include "Colorizer.h" |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 128 | #include "DisplayDevice.h" |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 129 | #include "DisplayHardware/ComposerHal.h" |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 130 | #include "DisplayHardware/FramebufferSurface.h" |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 131 | #include "DisplayHardware/HWComposer.h" |
Marin Shalamanov | d3b5c5d | 2021-02-11 18:26:14 +0100 | [diff] [blame] | 132 | #include "DisplayHardware/Hal.h" |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 133 | #include "DisplayHardware/PowerAdvisor.h" |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 134 | #include "DisplayHardware/VirtualDisplaySurface.h" |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 135 | #include "DisplayRenderArea.h" |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 136 | #include "Effects/Daltonizer.h" |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 137 | #include "FpsReporter.h" |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 138 | #include "FrameTimeline/FrameTimeline.h" |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 139 | #include "FrameTracer/FrameTracer.h" |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 140 | #include "FrontEnd/LayerCreationArgs.h" |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 141 | #include "FrontEnd/LayerHandle.h" |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 142 | #include "FrontEnd/LayerLifecycleManager.h" |
Vishnu Nair | 854ce1c | 2023-08-19 15:00:13 -0700 | [diff] [blame] | 143 | #include "FrontEnd/LayerLog.h" |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 144 | #include "FrontEnd/LayerSnapshot.h" |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 145 | #include "HdrLayerInfoReporter.h" |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 146 | #include "Jank/JankTracker.h" |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 147 | #include "Layer.h" |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 148 | #include "LayerProtoHelper.h" |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 149 | #include "LayerRenderArea.h" |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 150 | #include "LayerVector.h" |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 151 | #include "MutexUtils.h" |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 152 | #include "NativeWindowSurface.h" |
Dominik Laskowski | 9dab343 | 2019-03-27 13:21:10 -0700 | [diff] [blame] | 153 | #include "RegionSamplingThread.h" |
Melody Hsu | 41ade20 | 2024-05-03 01:25:50 +0000 | [diff] [blame] | 154 | #include "RenderAreaBuilder.h" |
Ana Krulec | fefcb58 | 2018-08-07 14:22:37 -0700 | [diff] [blame] | 155 | #include "Scheduler/EventThread.h" |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 156 | #include "Scheduler/LayerHistory.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 157 | #include "Scheduler/Scheduler.h" |
Ady Abraham | c581d3c | 2020-08-06 17:34:27 -0700 | [diff] [blame] | 158 | #include "Scheduler/VsyncConfiguration.h" |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 159 | #include "Scheduler/VsyncModulator.h" |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 160 | #include "ScreenCaptureOutput.h" |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 161 | #include "SurfaceFlingerProperties.h" |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 162 | #include "TimeStats/TimeStats.h" |
Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 163 | #include "TunnelModeEnabledReporter.h" |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame] | 164 | #include "Utils/Dumper.h" |
chaviw | 60c9d3e | 2021-06-04 12:52:17 -0500 | [diff] [blame] | 165 | #include "WindowInfosListenerInvoker.h" |
David Sodman | 7e4ae11 | 2018-02-09 15:02:28 -0800 | [diff] [blame] | 166 | |
Leon Scroggins III | e7c51c6 | 2022-02-01 15:53:54 -0500 | [diff] [blame] | 167 | #include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h> |
Leon Scroggins III | 5967aec | 2021-12-29 11:14:22 -0500 | [diff] [blame] | 168 | #include <aidl/android/hardware/graphics/composer3/DisplayCapability.h> |
Alec Mouri | fcedb9c | 2022-04-11 20:02:17 +0000 | [diff] [blame] | 169 | #include <aidl/android/hardware/graphics/composer3/RenderIntent.h> |
Leon Scroggins III | 5967aec | 2021-12-29 11:14:22 -0500 | [diff] [blame] | 170 | |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 171 | #undef NO_THREAD_SAFETY_ANALYSIS |
| 172 | #define NO_THREAD_SAFETY_ANALYSIS \ |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 173 | _Pragma("GCC error \"Prefer <ftl/fake_guard.h> or MutexUtils.h helpers.\"") |
Leon Scroggins III | 5967aec | 2021-12-29 11:14:22 -0500 | [diff] [blame] | 174 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | namespace android { |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 176 | using namespace std::chrono_literals; |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 177 | using namespace std::string_literals; |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 178 | using namespace std::string_view_literals; |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 179 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 180 | using namespace hardware::configstore; |
| 181 | using namespace hardware::configstore::V1_0; |
| 182 | using namespace sysprop; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 183 | using ftl::Flags; |
| 184 | using namespace ftl::flag_operators; |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 185 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 186 | using aidl::android::hardware::graphics::common::DisplayDecorationSupport; |
| 187 | using aidl::android::hardware::graphics::composer3::Capability; |
| 188 | using aidl::android::hardware::graphics::composer3::DisplayCapability; |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 189 | using CompositionStrategyPredictionState = android::compositionengine::impl:: |
| 190 | OutputCompositionState::CompositionStrategyPredictionState; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 191 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 192 | using base::StringAppendF; |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 193 | using display::PhysicalDisplay; |
| 194 | using display::PhysicalDisplays; |
Vishnu Nair | af6d297 | 2022-11-18 06:26:38 +0000 | [diff] [blame] | 195 | using frontend::TransactionHandler; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 196 | using gui::DisplayInfo; |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 197 | using gui::GameMode; |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 198 | using gui::IDisplayEventConnection; |
chaviw | 60c9d3e | 2021-06-04 12:52:17 -0500 | [diff] [blame] | 199 | using gui::IWindowInfosListener; |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 200 | using gui::LayerMetadata; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 201 | using gui::WindowInfo; |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 202 | using gui::aidl_utils::binderStatusFromStatusT; |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 203 | using scheduler::VsyncModulator; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 204 | using ui::Dataspace; |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 205 | using ui::DisplayPrimaries; |
Peiyong Lin | 0e7a791 | 2018-04-05 14:36:36 -0700 | [diff] [blame] | 206 | using ui::RenderIntent; |
Jiyong Park | 4b20c2e | 2017-01-14 19:45:11 +0900 | [diff] [blame] | 207 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 208 | using KernelIdleTimerController = scheduler::RefreshRateSelector::KernelIdleTimerController; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 209 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 210 | namespace hal = android::hardware::graphics::composer::hal; |
| 211 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 212 | namespace { |
Peiyong Lin | fca547f | 2018-07-09 13:03:33 -0700 | [diff] [blame] | 213 | |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 214 | static constexpr int FOUR_K_WIDTH = 3840; |
| 215 | static constexpr int FOUR_K_HEIGHT = 2160; |
| 216 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 217 | // TODO(b/141333600): Consolidate with DisplayMode::Builder::getDefaultDensity. |
Dominik Laskowski | d125d0e | 2020-05-08 12:36:39 -0700 | [diff] [blame] | 218 | constexpr float FALLBACK_DENSITY = ACONFIGURATION_DENSITY_TV; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 219 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 220 | float getDensityFromProperty(const char* property, bool required) { |
| 221 | char value[PROPERTY_VALUE_MAX]; |
| 222 | const float density = property_get(property, value, nullptr) > 0 ? std::atof(value) : 0.f; |
| 223 | if (!density && required) { |
| 224 | ALOGE("%s must be defined as a build property", property); |
| 225 | return FALLBACK_DENSITY; |
| 226 | } |
| 227 | return density; |
| 228 | } |
| 229 | |
Peiyong Lin | 9d846a5 | 2018-11-05 13:18:20 -0800 | [diff] [blame] | 230 | // Currently we only support V0_SRGB and DISPLAY_P3 as composition preference. |
| 231 | bool validateCompositionDataspace(Dataspace dataspace) { |
| 232 | return dataspace == Dataspace::V0_SRGB || dataspace == Dataspace::DISPLAY_P3; |
| 233 | } |
| 234 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 235 | std::chrono::milliseconds getIdleTimerTimeout(PhysicalDisplayId displayId) { |
Dominik Laskowski | dd374e1 | 2024-02-21 10:28:15 -0500 | [diff] [blame] | 236 | if (const int32_t displayIdleTimerMs = |
| 237 | base::GetIntProperty("debug.sf.set_idle_timer_ms_"s + |
| 238 | std::to_string(displayId.value), |
| 239 | 0); |
| 240 | displayIdleTimerMs > 0) { |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 241 | return std::chrono::milliseconds(displayIdleTimerMs); |
| 242 | } |
| 243 | |
Dominik Laskowski | dd374e1 | 2024-02-21 10:28:15 -0500 | [diff] [blame] | 244 | const int32_t setIdleTimerMs = base::GetIntProperty("debug.sf.set_idle_timer_ms"s, 0); |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 245 | const int32_t millis = setIdleTimerMs ? setIdleTimerMs : sysprop::set_idle_timer_ms(0); |
| 246 | return std::chrono::milliseconds(millis); |
| 247 | } |
| 248 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 249 | bool getKernelIdleTimerSyspropConfig(PhysicalDisplayId displayId) { |
Dominik Laskowski | dd374e1 | 2024-02-21 10:28:15 -0500 | [diff] [blame] | 250 | const bool displaySupportKernelIdleTimer = |
| 251 | base::GetBoolProperty("debug.sf.support_kernel_idle_timer_"s + |
| 252 | std::to_string(displayId.value), |
| 253 | false); |
Ady Abraham | 6d88593 | 2021-09-03 18:05:48 -0700 | [diff] [blame] | 254 | |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 255 | return displaySupportKernelIdleTimer || sysprop::support_kernel_idle_timer(false); |
Ady Abraham | 6d88593 | 2021-09-03 18:05:48 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 258 | bool isAbove4k30(const ui::DisplayMode& outMode) { |
| 259 | using fps_approx_ops::operator>; |
Alec Mouri | 55e3103 | 2023-10-02 20:34:18 +0000 | [diff] [blame] | 260 | Fps refreshRate = Fps::fromValue(outMode.peakRefreshRate); |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 261 | return outMode.resolution.getWidth() >= FOUR_K_WIDTH && |
| 262 | outMode.resolution.getHeight() >= FOUR_K_HEIGHT && refreshRate > 30_Hz; |
| 263 | } |
| 264 | |
| 265 | void excludeDolbyVisionIf4k30Present(const std::vector<ui::Hdr>& displayHdrTypes, |
| 266 | ui::DisplayMode& outMode) { |
| 267 | if (isAbove4k30(outMode) && |
| 268 | std::any_of(displayHdrTypes.begin(), displayHdrTypes.end(), |
| 269 | [](ui::Hdr type) { return type == ui::Hdr::DOLBY_VISION_4K30; })) { |
| 270 | for (ui::Hdr type : displayHdrTypes) { |
| 271 | if (type != ui::Hdr::DOLBY_VISION_4K30 && type != ui::Hdr::DOLBY_VISION) { |
| 272 | outMode.supportedHdrTypes.push_back(type); |
| 273 | } |
| 274 | } |
| 275 | } else { |
| 276 | for (ui::Hdr type : displayHdrTypes) { |
| 277 | if (type != ui::Hdr::DOLBY_VISION_4K30) { |
| 278 | outMode.supportedHdrTypes.push_back(type); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | HdrCapabilities filterOut4k30(const HdrCapabilities& displayHdrCapabilities) { |
| 285 | std::vector<ui::Hdr> hdrTypes; |
| 286 | for (ui::Hdr type : displayHdrCapabilities.getSupportedHdrTypes()) { |
| 287 | if (type != ui::Hdr::DOLBY_VISION_4K30) { |
| 288 | hdrTypes.push_back(type); |
| 289 | } |
| 290 | } |
| 291 | return {hdrTypes, displayHdrCapabilities.getDesiredMaxLuminance(), |
| 292 | displayHdrCapabilities.getDesiredMaxAverageLuminance(), |
| 293 | displayHdrCapabilities.getDesiredMinLuminance()}; |
| 294 | } |
| 295 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 296 | uint32_t getLayerIdFromSurfaceControl(sp<SurfaceControl> surfaceControl) { |
| 297 | if (!surfaceControl) { |
| 298 | return UNASSIGNED_LAYER_ID; |
| 299 | } |
| 300 | return LayerHandle::getLayerId(surfaceControl->getHandle()); |
| 301 | } |
| 302 | |
Patrick Williams | 8b731c1 | 2023-10-24 09:56:24 -0500 | [diff] [blame] | 303 | /** |
| 304 | * Returns true if the file at path exists and is newer than duration. |
| 305 | */ |
| 306 | bool fileNewerThan(const std::string& path, std::chrono::minutes duration) { |
| 307 | using Clock = std::filesystem::file_time_type::clock; |
| 308 | std::error_code error; |
| 309 | std::filesystem::file_time_type updateTime = std::filesystem::last_write_time(path, error); |
| 310 | if (error) { |
| 311 | return false; |
| 312 | } |
| 313 | return duration > (Clock::now() - updateTime); |
| 314 | } |
| 315 | |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 316 | bool isFrameIntervalOnCadence(TimePoint expectedPresentTime, TimePoint lastExpectedPresentTimestamp, |
| 317 | Fps lastFrameInterval, Period timeout, Duration threshold) { |
| 318 | if (lastFrameInterval.getPeriodNsecs() == 0) { |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | const auto expectedPresentTimeDeltaNs = |
| 323 | expectedPresentTime.ns() - lastExpectedPresentTimestamp.ns(); |
| 324 | |
| 325 | if (expectedPresentTimeDeltaNs > timeout.ns()) { |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | const auto expectedPresentPeriods = static_cast<nsecs_t>( |
| 330 | std::round(static_cast<float>(expectedPresentTimeDeltaNs) / |
| 331 | static_cast<float>(lastFrameInterval.getPeriodNsecs()))); |
| 332 | const auto calculatedPeriodsOutNs = lastFrameInterval.getPeriodNsecs() * expectedPresentPeriods; |
| 333 | const auto calculatedExpectedPresentTimeNs = |
| 334 | lastExpectedPresentTimestamp.ns() + calculatedPeriodsOutNs; |
| 335 | const auto presentTimeDelta = |
| 336 | std::abs(expectedPresentTime.ns() - calculatedExpectedPresentTimeNs); |
| 337 | return presentTimeDelta < threshold.ns(); |
| 338 | } |
| 339 | |
| 340 | bool isExpectedPresentWithinTimeout(TimePoint expectedPresentTime, |
| 341 | TimePoint lastExpectedPresentTimestamp, |
| 342 | std::optional<Period> timeoutOpt, Duration threshold) { |
| 343 | if (!timeoutOpt) { |
| 344 | // Always within timeout if timeoutOpt is absent and don't send hint |
| 345 | // for the timeout |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | if (timeoutOpt->ns() == 0) { |
| 350 | // Always outside timeout if timeoutOpt is 0 and always send |
| 351 | // the hint for the timeout. |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | if (expectedPresentTime.ns() < lastExpectedPresentTimestamp.ns() + timeoutOpt->ns()) { |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | // Check if within the threshold as it can be just outside the timeout |
| 360 | return std::abs(expectedPresentTime.ns() - |
| 361 | (lastExpectedPresentTimestamp.ns() + timeoutOpt->ns())) < threshold.ns(); |
| 362 | } |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 363 | } // namespace anonymous |
| 364 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | // --------------------------------------------------------------------------- |
| 366 | |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 367 | const String16 sHardwareTest("android.permission.HARDWARE_TEST"); |
| 368 | const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"); |
Hongwei Wang | 46213ea | 2020-12-04 17:17:31 -0800 | [diff] [blame] | 369 | const String16 sRotateSurfaceFlinger("android.permission.ROTATE_SURFACE_FLINGER"); |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 370 | const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER"); |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 371 | const String16 sControlDisplayBrightness("android.permission.CONTROL_DISPLAY_BRIGHTNESS"); |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 372 | const String16 sDump("android.permission.DUMP"); |
chaviw | f5bb97b | 2021-04-28 15:35:37 -0500 | [diff] [blame] | 373 | const String16 sCaptureBlackoutContent("android.permission.CAPTURE_BLACKOUT_CONTENT"); |
Leon Scroggins | 9a20f72 | 2021-12-28 14:43:12 +0000 | [diff] [blame] | 374 | const String16 sInternalSystemWindow("android.permission.INTERNAL_SYSTEM_WINDOW"); |
Vishnu Nair | 884549f | 2022-12-01 17:20:59 -0800 | [diff] [blame] | 375 | const String16 sWakeupSurfaceFlinger("android.permission.WAKEUP_SURFACE_FLINGER"); |
chaviw | f5bb97b | 2021-04-28 15:35:37 -0500 | [diff] [blame] | 376 | |
Amy Hsu | c8cdfef | 2020-05-07 13:06:25 +0800 | [diff] [blame] | 377 | const char* KERNEL_IDLE_TIMER_PROP = "graphics.display.kernel_idle_timer.enabled"; |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 378 | |
| 379 | // --------------------------------------------------------------------------- |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 380 | int64_t SurfaceFlinger::dispSyncPresentTimeOffset; |
| 381 | bool SurfaceFlinger::useHwcForRgbToYuv; |
| 382 | bool SurfaceFlinger::hasSyncFramework; |
| 383 | int64_t SurfaceFlinger::maxFrameBufferAcquiredBuffers; |
| 384 | int64_t SurfaceFlinger::minAcquiredBuffers = 1; |
| 385 | uint32_t SurfaceFlinger::maxGraphicsWidth; |
| 386 | uint32_t SurfaceFlinger::maxGraphicsHeight; |
| 387 | bool SurfaceFlinger::useContextPriority; |
| 388 | Dataspace SurfaceFlinger::defaultCompositionDataspace = Dataspace::V0_SRGB; |
| 389 | ui::PixelFormat SurfaceFlinger::defaultCompositionPixelFormat = ui::PixelFormat::RGBA_8888; |
| 390 | Dataspace SurfaceFlinger::wideColorGamutCompositionDataspace = Dataspace::V0_SRGB; |
| 391 | ui::PixelFormat SurfaceFlinger::wideColorGamutCompositionPixelFormat = ui::PixelFormat::RGBA_8888; |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 392 | LatchUnsignaledConfig SurfaceFlinger::enableLatchUnsignaledConfig; |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 393 | |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 394 | std::string decodeDisplayColorSetting(DisplayColorSetting displayColorSetting) { |
| 395 | switch(displayColorSetting) { |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 396 | case DisplayColorSetting::kManaged: |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 397 | return std::string("Managed"); |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 398 | case DisplayColorSetting::kUnmanaged: |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 399 | return std::string("Unmanaged"); |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 400 | case DisplayColorSetting::kEnhanced: |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 401 | return std::string("Enhanced"); |
| 402 | default: |
| 403 | return std::string("Unknown ") + |
| 404 | std::to_string(static_cast<int>(displayColorSetting)); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 405 | } |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Vishnu Nair | 884549f | 2022-12-01 17:20:59 -0800 | [diff] [blame] | 408 | bool callingThreadHasPermission(const String16& permission) { |
Hongwei Wang | 46213ea | 2020-12-04 17:17:31 -0800 | [diff] [blame] | 409 | IPCThreadState* ipc = IPCThreadState::self(); |
| 410 | const int pid = ipc->getCallingPid(); |
| 411 | const int uid = ipc->getCallingUid(); |
| 412 | return uid == AID_GRAPHICS || uid == AID_SYSTEM || |
Vishnu Nair | 884549f | 2022-12-01 17:20:59 -0800 | [diff] [blame] | 413 | PermissionCache::checkPermission(permission, pid, uid); |
Leon Scroggins | 9a20f72 | 2021-12-28 14:43:12 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Leon Scroggins III | 85d4b22 | 2023-05-09 13:58:18 -0400 | [diff] [blame] | 416 | ui::Transform::RotationFlags SurfaceFlinger::sActiveDisplayRotationFlags = ui::Transform::ROT_0; |
| 417 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 418 | SurfaceFlinger::SurfaceFlinger(Factory& factory, SkipInitializationTag) |
| 419 | : mFactory(factory), |
| 420 | mPid(getpid()), |
Yiwei Zhang | f0229a7 | 2019-09-09 19:28:02 -0700 | [diff] [blame] | 421 | mTimeStats(std::make_shared<impl::TimeStats>()), |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 422 | mFrameTracer(mFactory.createFrameTracer()), |
| 423 | mFrameTimeline(mFactory.createFrameTimeline(mTimeStats, mPid)), |
| 424 | mCompositionEngine(mFactory.createCompositionEngine()), |
| 425 | mHwcServiceName(base::GetProperty("debug.sf.hwc_service_name"s, "default"s)), |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 426 | mTunnelModeEnabledReporter(sp<TunnelModeEnabledReporter>::make()), |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 427 | mEmulatedDisplayDensity(getDensityFromProperty("qemu.sf.lcd_density", false)), |
| 428 | mInternalDisplayDensity( |
| 429 | getDensityFromProperty("ro.sf.lcd_density", !mEmulatedDisplayDensity)), |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 430 | mPowerAdvisor(std::make_unique<Hwc2::impl::PowerAdvisor>(*this)), |
Robert Horvath | dce110a | 2022-09-16 11:08:47 +0200 | [diff] [blame] | 431 | mWindowInfosListenerInvoker(sp<WindowInfosListenerInvoker>::make()), |
| 432 | mSkipPowerOnForQuiescent(base::GetBoolProperty("ro.boot.quiescent"s, false)) { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 433 | ALOGI("Using HWComposer service: %s", mHwcServiceName.c_str()); |
| 434 | } |
Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 435 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 436 | SurfaceFlinger::SurfaceFlinger(Factory& factory) : SurfaceFlinger(factory, SkipInitialization) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 437 | SFTRACE_CALL(); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 438 | ALOGI("SurfaceFlinger is starting"); |
| 439 | |
| 440 | hasSyncFramework = running_without_sync_framework(true); |
| 441 | |
| 442 | dispSyncPresentTimeOffset = present_time_offset_from_vsync_ns(0); |
| 443 | |
| 444 | useHwcForRgbToYuv = force_hwc_copy_for_virtual_displays(false); |
| 445 | |
| 446 | maxFrameBufferAcquiredBuffers = max_frame_buffer_acquired_buffers(2); |
| 447 | minAcquiredBuffers = |
| 448 | SurfaceFlingerProperties::min_acquired_buffers().value_or(minAcquiredBuffers); |
| 449 | |
| 450 | maxGraphicsWidth = std::max(max_graphics_width(0), 0); |
| 451 | maxGraphicsHeight = std::max(max_graphics_height(0), 0); |
| 452 | |
| 453 | mSupportsWideColor = has_wide_color_display(false); |
| 454 | mDefaultCompositionDataspace = |
| 455 | static_cast<ui::Dataspace>(default_composition_dataspace(Dataspace::V0_SRGB)); |
| 456 | mWideColorGamutCompositionDataspace = static_cast<ui::Dataspace>(wcg_composition_dataspace( |
| 457 | mSupportsWideColor ? Dataspace::DISPLAY_P3 : Dataspace::V0_SRGB)); |
| 458 | defaultCompositionDataspace = mDefaultCompositionDataspace; |
| 459 | wideColorGamutCompositionDataspace = mWideColorGamutCompositionDataspace; |
| 460 | defaultCompositionPixelFormat = static_cast<ui::PixelFormat>( |
| 461 | default_composition_pixel_format(ui::PixelFormat::RGBA_8888)); |
| 462 | wideColorGamutCompositionPixelFormat = |
| 463 | static_cast<ui::PixelFormat>(wcg_composition_pixel_format(ui::PixelFormat::RGBA_8888)); |
| 464 | |
Dominik Laskowski | dd374e1 | 2024-02-21 10:28:15 -0500 | [diff] [blame] | 465 | mLayerCachingEnabled = |
| 466 | base::GetBoolProperty("debug.sf.enable_layer_caching"s, |
| 467 | sysprop::SurfaceFlingerProperties::enable_layer_caching() |
| 468 | .value_or(false)); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 469 | |
| 470 | useContextPriority = use_context_priority(true); |
| 471 | |
| 472 | mInternalDisplayPrimaries = sysprop::getDisplayNativePrimaries(); |
| 473 | |
| 474 | // debugging stuff... |
| 475 | char value[PROPERTY_VALUE_MAX]; |
| 476 | |
| 477 | property_get("ro.build.type", value, "user"); |
| 478 | mIsUserBuild = strcmp(value, "user") == 0; |
| 479 | |
| 480 | mDebugFlashDelay = base::GetUintProperty("debug.sf.showupdates"s, 0u); |
| 481 | |
| 482 | mBackpressureGpuComposition = base::GetBoolProperty("debug.sf.enable_gl_backpressure"s, true); |
| 483 | ALOGI_IF(mBackpressureGpuComposition, "Enabling backpressure for GPU composition"); |
| 484 | |
| 485 | property_get("ro.surface_flinger.supports_background_blur", value, "0"); |
| 486 | bool supportsBlurs = atoi(value); |
| 487 | mSupportsBlur = supportsBlurs; |
| 488 | ALOGI_IF(!mSupportsBlur, "Disabling blur effects, they are not supported."); |
| 489 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 490 | property_get("debug.sf.luma_sampling", value, "1"); |
| 491 | mLumaSampling = atoi(value); |
| 492 | |
| 493 | property_get("debug.sf.disable_client_composition_cache", value, "0"); |
| 494 | mDisableClientCompositionCache = atoi(value); |
| 495 | |
| 496 | property_get("debug.sf.predict_hwc_composition_strategy", value, "1"); |
| 497 | mPredictCompositionStrategy = atoi(value); |
| 498 | |
| 499 | property_get("debug.sf.treat_170m_as_sRGB", value, "0"); |
| 500 | mTreat170mAsSrgb = atoi(value); |
| 501 | |
Alec Mouri | 9af38ba | 2023-08-16 22:41:14 +0000 | [diff] [blame] | 502 | property_get("debug.sf.dim_in_gamma_in_enhanced_screenshots", value, 0); |
| 503 | mDimInGammaSpaceForEnhancedScreenshots = atoi(value); |
| 504 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 505 | mIgnoreHwcPhysicalDisplayOrientation = |
| 506 | base::GetBoolProperty("debug.sf.ignore_hwc_physical_display_orientation"s, false); |
| 507 | |
| 508 | // We should be reading 'persist.sys.sf.color_saturation' here |
| 509 | // but since /data may be encrypted, we need to wait until after vold |
| 510 | // comes online to attempt to read the property. The property is |
| 511 | // instead read after the boot animation |
| 512 | |
| 513 | if (base::GetBoolProperty("debug.sf.treble_testing_override"s, false)) { |
Kalle Raita | a099a24 | 2017-01-11 11:17:29 -0800 | [diff] [blame] | 514 | // Without the override SurfaceFlinger cannot connect to HIDL |
| 515 | // services that are not listed in the manifests. Considered |
| 516 | // deriving the setting from the set service name, but it |
| 517 | // would be brittle if the name that's not 'default' is used |
| 518 | // for production purposes later on. |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 519 | ALOGI("Enabling Treble testing override"); |
Steven Moreland | 7c8af94 | 2020-07-08 18:35:51 +0000 | [diff] [blame] | 520 | android::hardware::details::setTrebleTestingOverride(true); |
Kalle Raita | a099a24 | 2017-01-11 11:17:29 -0800 | [diff] [blame] | 521 | } |
Ana Krulec | 3803b8d | 2020-02-03 16:35:46 -0800 | [diff] [blame] | 522 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 523 | // TODO (b/270966065) Update the HWC based refresh rate overlay to support spinner |
| 524 | mRefreshRateOverlaySpinner = property_get_bool("debug.sf.show_refresh_rate_overlay_spinner", 0); |
| 525 | mRefreshRateOverlayRenderRate = |
| 526 | property_get_bool("debug.sf.show_refresh_rate_overlay_render_rate", 0); |
| 527 | mRefreshRateOverlayShowInMiddle = |
| 528 | property_get_bool("debug.sf.show_refresh_rate_overlay_in_middle", 0); |
| 529 | |
| 530 | if (!mIsUserBuild && base::GetBoolProperty("debug.sf.enable_transaction_tracing"s, true)) { |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 531 | mTransactionTracing.emplace(); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 532 | mLayerTracing.setTransactionTracing(*mTransactionTracing); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 533 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 534 | |
| 535 | mIgnoreHdrCameraLayers = ignore_hdr_camera_layers(false); |
| 536 | |
| 537 | mLayerLifecycleManagerEnabled = |
Vishnu Nair | f42d9bd | 2023-08-23 21:35:33 +0000 | [diff] [blame] | 538 | base::GetBoolProperty("persist.debug.sf.enable_layer_lifecycle_manager"s, true); |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 539 | |
| 540 | // These are set by the HWC implementation to indicate that they will use the workarounds. |
| 541 | mIsHotplugErrViaNegVsync = |
| 542 | base::GetBoolProperty("debug.sf.hwc_hotplug_error_via_neg_vsync"s, false); |
| 543 | |
| 544 | mIsHdcpViaNegVsync = base::GetBoolProperty("debug.sf.hwc_hdcp_via_neg_vsync"s, false); |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | LatchUnsignaledConfig SurfaceFlinger::getLatchUnsignaledConfig() { |
Ady Abraham | d9e8d1b | 2022-02-25 00:36:10 +0000 | [diff] [blame] | 548 | if (base::GetBoolProperty("debug.sf.auto_latch_unsignaled"s, true)) { |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 549 | return LatchUnsignaledConfig::AutoSingleLayer; |
| 550 | } |
| 551 | |
| 552 | return LatchUnsignaledConfig::Disabled; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 555 | SurfaceFlinger::~SurfaceFlinger() = default; |
| 556 | |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 557 | void SurfaceFlinger::binderDied(const wp<IBinder>&) { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 558 | // the window manager died on us. prepare its eulogy. |
Rob Carr | 2aa78cb | 2020-03-10 14:27:49 -0700 | [diff] [blame] | 559 | mBootFinished = false; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 560 | |
Dominik Laskowski | b5f40e5 | 2022-09-29 13:25:05 -0400 | [diff] [blame] | 561 | static_cast<void>(mScheduler->schedule([this]() FTL_FAKE_GUARD(kMainThreadContext) { |
| 562 | // Sever the link to inputflinger since it's gone as well. |
| 563 | mInputFlinger.clear(); |
Vishnu Nair | b9df470 | 2021-03-01 11:53:15 -0800 | [diff] [blame] | 564 | |
Dominik Laskowski | b5f40e5 | 2022-09-29 13:25:05 -0400 | [diff] [blame] | 565 | initializeDisplays(); |
| 566 | })); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 567 | |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 568 | mInitBootPropsFuture.callOnce([this] { |
| 569 | return std::async(std::launch::async, &SurfaceFlinger::initBootProperties, this); |
| 570 | }); |
Dominik Laskowski | efb131e | 2024-02-16 10:57:40 -0500 | [diff] [blame] | 571 | |
| 572 | mInitBootPropsFuture.wait(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 575 | void SurfaceFlinger::run() { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 576 | mScheduler->run(); |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 579 | sp<IBinder> SurfaceFlinger::createVirtualDisplay(const std::string& displayName, bool isSecure, |
| 580 | const std::string& uniqueId, |
| 581 | float requestedRefreshRate) { |
Patrick Williams | e58a92b | 2024-02-29 14:52:25 -0600 | [diff] [blame] | 582 | // SurfaceComposerAIDL checks for some permissions, but adding an additional check here. |
| 583 | // This is to ensure that only root, system, and graphics can request to create a secure |
chaviw | d4a6164 | 2020-09-01 14:53:46 -0700 | [diff] [blame] | 584 | // display. Secure displays can show secure content so we add an additional restriction on it. |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 585 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 586 | if (isSecure && uid != AID_ROOT && uid != AID_GRAPHICS && uid != AID_SYSTEM) { |
chaviw | d4a6164 | 2020-09-01 14:53:46 -0700 | [diff] [blame] | 587 | ALOGE("Only privileged processes can create a secure display"); |
| 588 | return nullptr; |
| 589 | } |
| 590 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 591 | class DisplayToken : public BBinder { |
| 592 | sp<SurfaceFlinger> flinger; |
| 593 | virtual ~DisplayToken() { |
| 594 | // no more references, this display must be terminated |
| 595 | Mutex::Autolock _l(flinger->mStateLock); |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 596 | flinger->mCurrentState.displays.removeItem(wp<IBinder>::fromExisting(this)); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 597 | flinger->setTransactionFlags(eDisplayTransactionNeeded); |
| 598 | } |
| 599 | public: |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 600 | explicit DisplayToken(const sp<SurfaceFlinger>& flinger) |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 601 | : flinger(flinger) { |
| 602 | } |
| 603 | }; |
| 604 | |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 605 | sp<BBinder> token = sp<DisplayToken>::make(sp<SurfaceFlinger>::fromExisting(this)); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 606 | |
| 607 | Mutex::Autolock _l(mStateLock); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 608 | // Display ID is assigned when virtual display is allocated by HWC. |
| 609 | DisplayDeviceState state; |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 610 | state.isSecure = isSecure; |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 611 | // Set display as protected when marked as secure to ensure no behavior change |
| 612 | // TODO (b/314820005): separate as a different arg when creating the display. |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 613 | state.isProtected = isSecure; |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 614 | state.displayName = displayName; |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 615 | state.uniqueId = uniqueId; |
Huihong Luo | 1768cb0 | 2022-10-11 11:10:34 -0700 | [diff] [blame] | 616 | state.requestedRefreshRate = Fps::fromValue(requestedRefreshRate); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 617 | mCurrentState.displays.add(token, state); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 618 | return token; |
| 619 | } |
| 620 | |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 621 | status_t SurfaceFlinger::destroyVirtualDisplay(const sp<IBinder>& displayToken) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 622 | Mutex::Autolock lock(mStateLock); |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 623 | |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 624 | const ssize_t index = mCurrentState.displays.indexOfKey(displayToken); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 625 | if (index < 0) { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 626 | ALOGE("%s: Invalid display token %p", __func__, displayToken.get()); |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 627 | return NAME_NOT_FOUND; |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 630 | const DisplayDeviceState& state = mCurrentState.displays.valueAt(index); |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 631 | if (state.physical) { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 632 | ALOGE("%s: Invalid operation on physical display", __func__); |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 633 | return INVALID_OPERATION; |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 634 | } |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 635 | mCurrentState.displays.removeItemsAt(index); |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 636 | setTransactionFlags(eDisplayTransactionNeeded); |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 637 | return NO_ERROR; |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 640 | void SurfaceFlinger::enableHalVirtualDisplays(bool enable) { |
| 641 | auto& generator = mVirtualDisplayIdGenerators.hal; |
| 642 | if (!generator && enable) { |
| 643 | ALOGI("Enabling HAL virtual displays"); |
| 644 | generator.emplace(getHwComposer().getMaxVirtualDisplayCount()); |
| 645 | } else if (generator && !enable) { |
| 646 | ALOGW_IF(generator->inUse(), "Disabling HAL virtual displays while in use"); |
| 647 | generator.reset(); |
| 648 | } |
| 649 | } |
| 650 | |
Dominik Laskowski | 263eec4 | 2021-07-21 23:13:24 -0700 | [diff] [blame] | 651 | VirtualDisplayId SurfaceFlinger::acquireVirtualDisplay(ui::Size resolution, |
| 652 | ui::PixelFormat format) { |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 653 | if (auto& generator = mVirtualDisplayIdGenerators.hal) { |
| 654 | if (const auto id = generator->generateId()) { |
Dominik Laskowski | 263eec4 | 2021-07-21 23:13:24 -0700 | [diff] [blame] | 655 | if (getHwComposer().allocateVirtualDisplay(*id, resolution, &format)) { |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 656 | return *id; |
| 657 | } |
| 658 | |
| 659 | generator->releaseId(*id); |
| 660 | } else { |
| 661 | ALOGW("%s: Exhausted HAL virtual displays", __func__); |
| 662 | } |
| 663 | |
| 664 | ALOGW("%s: Falling back to GPU virtual display", __func__); |
| 665 | } |
| 666 | |
| 667 | const auto id = mVirtualDisplayIdGenerators.gpu.generateId(); |
| 668 | LOG_ALWAYS_FATAL_IF(!id, "Failed to generate ID for GPU virtual display"); |
| 669 | return *id; |
| 670 | } |
| 671 | |
| 672 | void SurfaceFlinger::releaseVirtualDisplay(VirtualDisplayId displayId) { |
| 673 | if (const auto id = HalVirtualDisplayId::tryCast(displayId)) { |
| 674 | if (auto& generator = mVirtualDisplayIdGenerators.hal) { |
| 675 | generator->releaseId(*id); |
| 676 | } |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | const auto id = GpuVirtualDisplayId::tryCast(displayId); |
| 681 | LOG_ALWAYS_FATAL_IF(!id); |
| 682 | mVirtualDisplayIdGenerators.gpu.releaseId(*id); |
| 683 | } |
| 684 | |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 685 | std::vector<PhysicalDisplayId> SurfaceFlinger::getPhysicalDisplayIdsLocked() const { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 686 | std::vector<PhysicalDisplayId> displayIds; |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 687 | displayIds.reserve(mPhysicalDisplays.size()); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 688 | |
Dominik Laskowski | 3c36324 | 2022-04-07 10:44:12 -0700 | [diff] [blame] | 689 | const auto defaultDisplayId = getDefaultDisplayDeviceLocked()->getPhysicalId(); |
Ady Abraham | 2a0066d | 2021-07-15 20:16:58 -0700 | [diff] [blame] | 690 | displayIds.push_back(defaultDisplayId); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 691 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 692 | for (const auto& [id, display] : mPhysicalDisplays) { |
Ady Abraham | 2a0066d | 2021-07-15 20:16:58 -0700 | [diff] [blame] | 693 | if (id != defaultDisplayId) { |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 694 | displayIds.push_back(id); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 695 | } |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 696 | } |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 697 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 698 | return displayIds; |
| 699 | } |
| 700 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 701 | std::optional<PhysicalDisplayId> SurfaceFlinger::getPhysicalDisplayIdLocked( |
| 702 | const sp<display::DisplayToken>& displayToken) const { |
| 703 | return ftl::find_if(mPhysicalDisplays, PhysicalDisplay::hasToken(displayToken)) |
| 704 | .transform(&ftl::to_key<PhysicalDisplays>); |
| 705 | } |
| 706 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 707 | sp<IBinder> SurfaceFlinger::getPhysicalDisplayToken(PhysicalDisplayId displayId) const { |
| 708 | Mutex::Autolock lock(mStateLock); |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 709 | return getPhysicalDisplayTokenLocked(displayId); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 712 | HWComposer& SurfaceFlinger::getHwComposer() const { |
| 713 | return mCompositionEngine->getHwComposer(); |
| 714 | } |
| 715 | |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 716 | renderengine::RenderEngine& SurfaceFlinger::getRenderEngine() const { |
Patrick Williams | 0a525a4 | 2022-10-26 20:20:50 +0000 | [diff] [blame] | 717 | return *mRenderEngine; |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 720 | compositionengine::CompositionEngine& SurfaceFlinger::getCompositionEngine() const { |
| 721 | return *mCompositionEngine.get(); |
| 722 | } |
| 723 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 724 | void SurfaceFlinger::bootFinished() { |
Rob Carr | 2aa78cb | 2020-03-10 14:27:49 -0700 | [diff] [blame] | 725 | if (mBootFinished == true) { |
| 726 | ALOGE("Extra call to bootFinished"); |
| 727 | return; |
| 728 | } |
| 729 | mBootFinished = true; |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 730 | FlagManager::getMutableInstance().markBootCompleted(); |
Dominik Laskowski | efb131e | 2024-02-16 10:57:40 -0500 | [diff] [blame] | 731 | |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 732 | mInitBootPropsFuture.wait(); |
| 733 | mRenderEnginePrimeCacheFuture.wait(); |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 734 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | const nsecs_t now = systemTime(); |
| 736 | const nsecs_t duration = now - mBootTime; |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 737 | ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) ); |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 738 | |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 739 | mFrameTracer->initialize(); |
Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 740 | mFrameTimeline->onBootFinished(); |
Ady Abraham | 53b0c49 | 2023-10-23 11:47:14 -0700 | [diff] [blame] | 741 | getRenderEngine().setEnableTracing(FlagManager::getInstance().use_skia_tracing()); |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 742 | |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 743 | // wait patiently for the window manager death |
| 744 | const String16 name("window"); |
Cody Heiner | bcfa7fa | 2023-04-17 18:34:30 -0700 | [diff] [blame] | 745 | mWindowManager = defaultServiceManager()->waitForService(name); |
Steven Moreland | a904bb9 | 2019-07-02 17:37:23 -0700 | [diff] [blame] | 746 | if (mWindowManager != 0) { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 747 | mWindowManager->linkToDeath(sp<IBinder::DeathRecipient>::fromExisting(this)); |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // stop boot animation |
Mathias Agopian | a67e418 | 2012-06-19 17:26:12 -0700 | [diff] [blame] | 751 | // formerly we would just kill the process, but we now ask it to exit so it |
| 752 | // can choose where to stop the animation. |
| 753 | property_set("service.bootanim.exit", "1"); |
Yusuke Sato | 0a688f5 | 2015-07-30 23:05:39 -0700 | [diff] [blame] | 754 | |
| 755 | const int LOGTAG_SF_STOP_BOOTANIM = 60110; |
| 756 | LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM, |
| 757 | ns2ms(systemTime(SYSTEM_TIME_MONOTONIC))); |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 758 | |
Cody Heiner | bcfa7fa | 2023-04-17 18:34:30 -0700 | [diff] [blame] | 759 | sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger"))); |
Denis Hsu | e70cc6c | 2020-06-17 10:17:30 +0800 | [diff] [blame] | 760 | |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 761 | static_cast<void>(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(kMainThreadContext) { |
Denis Hsu | e70cc6c | 2020-06-17 10:17:30 +0800 | [diff] [blame] | 762 | if (input == nullptr) { |
| 763 | ALOGE("Failed to link to input service"); |
| 764 | } else { |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 765 | mInputFlinger = interface_cast<os::IInputFlinger>(input); |
Denis Hsu | e70cc6c | 2020-06-17 10:17:30 +0800 | [diff] [blame] | 766 | } |
| 767 | |
Chia-I Wu | 14c9c7b | 2018-06-26 10:18:18 +0800 | [diff] [blame] | 768 | readPersistentProperties(); |
Ady Abraham | 53b0c49 | 2023-10-23 11:47:14 -0700 | [diff] [blame] | 769 | const bool hintSessionEnabled = FlagManager::getInstance().use_adpf_cpu_hint(); |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 770 | mPowerAdvisor->enablePowerHintSession(hintSessionEnabled); |
| 771 | const bool hintSessionUsed = mPowerAdvisor->usePowerHintSession(); |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 772 | // Ordering is important here, as onBootFinished signals to PowerAdvisor that concurrency |
| 773 | // is safe because its variables are initialized. |
| 774 | mPowerAdvisor->onBootFinished(); |
Matt Buckley | 618c43f | 2022-08-12 18:09:48 +0000 | [diff] [blame] | 775 | ALOGD("Power hint is %s", |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 776 | hintSessionUsed ? "supported" : (hintSessionEnabled ? "unsupported" : "disabled")); |
| 777 | if (hintSessionUsed) { |
Matt Buckley | 618c43f | 2022-08-12 18:09:48 +0000 | [diff] [blame] | 778 | std::optional<pid_t> renderEngineTid = getRenderEngine().getRenderEngineTid(); |
| 779 | std::vector<int32_t> tidList; |
| 780 | tidList.emplace_back(gettid()); |
| 781 | if (renderEngineTid.has_value()) { |
| 782 | tidList.emplace_back(*renderEngineTid); |
| 783 | } |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 784 | if (!mPowerAdvisor->startPowerHintSession(std::move(tidList))) { |
Matt Buckley | 618c43f | 2022-08-12 18:09:48 +0000 | [diff] [blame] | 785 | ALOGW("Cannot start power hint session"); |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 786 | } |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Chia-I Wu | 14c9c7b | 2018-06-26 10:18:18 +0800 | [diff] [blame] | 789 | mBootStage = BootStage::FINISHED; |
Ana Krulec | bd6654b | 2019-02-15 15:18:15 -0800 | [diff] [blame] | 790 | |
Dominik Laskowski | 02bb207 | 2022-08-26 15:02:52 -0700 | [diff] [blame] | 791 | if (base::GetBoolProperty("sf.debug.show_refresh_rate_overlay"s, false)) { |
| 792 | ftl::FakeGuard guard(mStateLock); |
| 793 | enableRefreshRateOverlay(true); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 794 | } |
Chia-I Wu | 14c9c7b | 2018-06-26 10:18:18 +0800 | [diff] [blame] | 795 | })); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 798 | void chooseRenderEngineType(renderengine::RenderEngineCreationArgs::Builder& builder) { |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 799 | char prop[PROPERTY_VALUE_MAX]; |
Leon Scroggins III | f840ae5 | 2024-01-25 15:10:12 -0500 | [diff] [blame] | 800 | property_get(PROPERTY_DEBUG_RENDERENGINE_BACKEND, prop, ""); |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 801 | |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 802 | // TODO: b/293371537 - Once GraphiteVk is deemed relatively stable, log a warning that |
| 803 | // PROPERTY_DEBUG_RENDERENGINE_BACKEND is deprecated |
Alec Mouri | e2b61c6 | 2023-08-15 19:04:54 +0000 | [diff] [blame] | 804 | if (strcmp(prop, "skiagl") == 0) { |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 805 | builder.setThreaded(renderengine::RenderEngine::Threaded::NO) |
| 806 | .setGraphicsApi(renderengine::RenderEngine::GraphicsApi::GL); |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 807 | } else if (strcmp(prop, "skiaglthreaded") == 0) { |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 808 | builder.setThreaded(renderengine::RenderEngine::Threaded::YES) |
| 809 | .setGraphicsApi(renderengine::RenderEngine::GraphicsApi::GL); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 810 | } else if (strcmp(prop, "skiavk") == 0) { |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 811 | builder.setThreaded(renderengine::RenderEngine::Threaded::NO) |
| 812 | .setGraphicsApi(renderengine::RenderEngine::GraphicsApi::VK); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 813 | } else if (strcmp(prop, "skiavkthreaded") == 0) { |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 814 | builder.setThreaded(renderengine::RenderEngine::Threaded::YES) |
| 815 | .setGraphicsApi(renderengine::RenderEngine::GraphicsApi::VK); |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 816 | } else { |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 817 | const auto kVulkan = renderengine::RenderEngine::GraphicsApi::VK; |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 818 | // TODO: b/341728634 - Clean up conditional compilation. |
| 819 | // Note: this guard in particular must check e.g. |
| 820 | // COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS_GRAPHITE_RENDERENGINE directly (instead of calling e.g. |
| 821 | // COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS(GRAPHITE_RENDERENGINE)) because that macro is undefined |
| 822 | // in the libsurfaceflingerflags_test variant of com_android_graphics_surfaceflinger_flags.h, which |
| 823 | // is used by layertracegenerator (which also needs SurfaceFlinger.cpp). :) |
| 824 | #if COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS_GRAPHITE_RENDERENGINE || \ |
| 825 | COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS_FORCE_COMPILE_GRAPHITE_RENDERENGINE |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 826 | const bool useGraphite = FlagManager::getInstance().graphite_renderengine() && |
| 827 | renderengine::RenderEngine::canSupport(kVulkan); |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 828 | #else |
| 829 | const bool useGraphite = false; |
| 830 | if (FlagManager::getInstance().graphite_renderengine()) { |
| 831 | ALOGE("RenderEngine's Graphite Skia backend was requested with the " |
| 832 | "debug.renderengine.graphite system property, but it is not compiled in this " |
| 833 | "build! Falling back to Ganesh backend selection logic."); |
| 834 | } |
| 835 | #endif |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 836 | const bool useVulkan = useGraphite || |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 837 | (FlagManager::getInstance().vulkan_renderengine() && |
| 838 | renderengine::RenderEngine::canSupport(kVulkan)); |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 839 | |
| 840 | builder.setSkiaBackend(useGraphite ? renderengine::RenderEngine::SkiaBackend::GRAPHITE |
| 841 | : renderengine::RenderEngine::SkiaBackend::GANESH); |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 842 | builder.setGraphicsApi(useVulkan ? kVulkan : renderengine::RenderEngine::GraphicsApi::GL); |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Robin Lee | 7338bd9 | 2024-04-04 14:05:07 +0000 | [diff] [blame] | 846 | /** |
| 847 | * Choose a suggested blurring algorithm if supportsBlur is true. By default Kawase will be |
| 848 | * suggested as it's faster than a full Gaussian blur and looks close enough. |
| 849 | */ |
| 850 | renderengine::RenderEngine::BlurAlgorithm chooseBlurAlgorithm(bool supportsBlur) { |
| 851 | if (!supportsBlur) { |
| 852 | return renderengine::RenderEngine::BlurAlgorithm::NONE; |
| 853 | } |
| 854 | |
| 855 | auto const algorithm = base::GetProperty(PROPERTY_DEBUG_RENDERENGINE_BLUR_ALGORITHM, ""); |
| 856 | if (algorithm == "gaussian") { |
| 857 | return renderengine::RenderEngine::BlurAlgorithm::GAUSSIAN; |
| 858 | } else { |
| 859 | return renderengine::RenderEngine::BlurAlgorithm::KAWASE; |
| 860 | } |
| 861 | } |
| 862 | |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 863 | void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 864 | SFTRACE_CALL(); |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 865 | ALOGI( "SurfaceFlinger's main thread ready to run. " |
| 866 | "Initializing graphics H/W..."); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 867 | addTransactionReadyFilters(); |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 868 | Mutex::Autolock lock(mStateLock); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 869 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 870 | // Get a RenderEngine for the given display / config (can't fail) |
Peiyong Lin | 0256f72 | 2018-08-31 15:45:10 -0700 | [diff] [blame] | 871 | // TODO(b/77156734): We need to stop casting and use HAL types when possible. |
Alec Mouri | da4cf3b | 2019-02-12 15:33:01 -0800 | [diff] [blame] | 872 | // Sending maxFrameBufferAcquiredBuffers as the cache size is tightly tuned to single-display. |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 873 | auto builder = renderengine::RenderEngineCreationArgs::Builder() |
| 874 | .setPixelFormat(static_cast<int32_t>(defaultCompositionPixelFormat)) |
| 875 | .setImageCacheSize(maxFrameBufferAcquiredBuffers) |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 876 | .setEnableProtectedContext(enable_protected_contents(false)) |
| 877 | .setPrecacheToneMapperShaderOnly(false) |
Robin Lee | 7338bd9 | 2024-04-04 14:05:07 +0000 | [diff] [blame] | 878 | .setBlurAlgorithm(chooseBlurAlgorithm(mSupportsBlur)) |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 879 | .setContextPriority( |
| 880 | useContextPriority |
| 881 | ? renderengine::RenderEngine::ContextPriority::REALTIME |
| 882 | : renderengine::RenderEngine::ContextPriority::MEDIUM); |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 883 | chooseRenderEngineType(builder); |
Patrick Williams | 0a525a4 | 2022-10-26 20:20:50 +0000 | [diff] [blame] | 884 | mRenderEngine = renderengine::RenderEngine::create(builder.build()); |
| 885 | mCompositionEngine->setRenderEngine(mRenderEngine.get()); |
Garfield Tan | 9c9c191 | 2021-07-19 12:02:16 -0700 | [diff] [blame] | 886 | mMaxRenderTargetSize = |
| 887 | std::min(getRenderEngine().getMaxTextureSize(), getRenderEngine().getMaxViewportDims()); |
Wei Wang | 976a645 | 2021-06-11 15:26:00 -0700 | [diff] [blame] | 888 | |
| 889 | // Set SF main policy after initializing RenderEngine which has its own policy. |
| 890 | if (!SetTaskProfiles(0, {"SFMainPolicy"})) { |
| 891 | ALOGW("Failed to set main task profile"); |
| 892 | } |
| 893 | |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 894 | mCompositionEngine->setTimeStats(mTimeStats); |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 895 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 896 | mCompositionEngine->setHwComposer(getFactory().createHWComposer(mHwcServiceName)); |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 897 | auto& composer = mCompositionEngine->getHwComposer(); |
| 898 | composer.setCallback(*this); |
| 899 | mDisplayModeController.setHwComposer(&composer); |
| 900 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 901 | ClientCache::getInstance().setRenderEngine(&getRenderEngine()); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 902 | |
Dominik Laskowski | e62606d | 2024-02-22 11:42:13 -0500 | [diff] [blame] | 903 | mHasReliablePresentFences = |
| 904 | !getHwComposer().hasCapability(Capability::PRESENT_FENCE_IS_NOT_RELIABLE); |
| 905 | |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 906 | enableLatchUnsignaledConfig = getLatchUnsignaledConfig(); |
| 907 | |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 908 | if (base::GetBoolProperty("debug.sf.enable_hwc_vds"s, false)) { |
| 909 | enableHalVirtualDisplays(true); |
| 910 | } |
| 911 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 912 | // Process hotplug for displays connected at boot. |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 913 | LOG_ALWAYS_FATAL_IF(!configureLocked(), |
| 914 | "Initial display configuration failed: HWC did not hotplug"); |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 915 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 916 | // Commit primary display. |
| 917 | sp<const DisplayDevice> display; |
| 918 | if (const auto indexOpt = mCurrentState.getDisplayIndex(getPrimaryDisplayIdLocked())) { |
| 919 | const auto& displays = mCurrentState.displays; |
| 920 | |
| 921 | const auto& token = displays.keyAt(*indexOpt); |
| 922 | const auto& state = displays.valueAt(*indexOpt); |
| 923 | |
| 924 | processDisplayAdded(token, state); |
| 925 | mDrawingState.displays.add(token, state); |
| 926 | |
| 927 | display = getDefaultDisplayDeviceLocked(); |
| 928 | } |
| 929 | |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 930 | LOG_ALWAYS_FATAL_IF(!display, "Failed to configure the primary display"); |
| 931 | LOG_ALWAYS_FATAL_IF(!getHwComposer().isConnected(display->getPhysicalId()), |
| 932 | "Primary display is disconnected"); |
Jesse Hall | 692c723 | 2012-11-08 15:41:56 -0800 | [diff] [blame] | 933 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 934 | // TODO(b/241285876): The Scheduler needlessly depends on creating the CompositionEngine part of |
| 935 | // the DisplayDevice, hence the above commit of the primary display. Remove that special case by |
| 936 | // initializing the Scheduler after configureLocked, once decoupled from DisplayDevice. |
| 937 | initScheduler(display); |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 938 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 939 | // Start listening after creating the Scheduler, since the listener calls into it. |
| 940 | mDisplayModeController.setActiveModeListener( |
| 941 | display::DisplayModeController::ActiveModeListener::make( |
| 942 | [this](PhysicalDisplayId displayId, Fps vsyncRate, Fps renderRate) { |
| 943 | // This callback cannot lock mStateLock, as some callers already lock it. |
| 944 | // Instead, switch context to the main thread. |
| 945 | static_cast<void>(mScheduler->schedule([=, |
| 946 | this]() FTL_FAKE_GUARD(mStateLock) { |
| 947 | if (const auto display = getDisplayDeviceLocked(displayId)) { |
| 948 | display->updateRefreshRateOverlayRate(vsyncRate, renderRate); |
| 949 | } |
| 950 | })); |
| 951 | })); |
| 952 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 953 | mLayerTracing.setTakeLayersSnapshotProtoFunction([&](uint32_t traceFlags) { |
| 954 | auto snapshot = perfetto::protos::LayersSnapshotProto{}; |
| 955 | mScheduler |
| 956 | ->schedule([&]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD(kMainThreadContext) { |
| 957 | snapshot = takeLayersSnapshotProto(traceFlags, TimePoint::now(), |
| 958 | mLastCommittedVsyncId, true); |
| 959 | }) |
| 960 | .wait(); |
| 961 | return snapshot; |
| 962 | }); |
| 963 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 964 | // Commit secondary display(s). |
| 965 | processDisplayChangesLocked(); |
| 966 | |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 967 | // initialize our drawing state |
| 968 | mDrawingState = mCurrentState; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 969 | |
Dominik Laskowski | a39e32f | 2023-02-08 16:40:13 -0500 | [diff] [blame] | 970 | onActiveDisplayChangedLocked(nullptr, *display); |
Dominik Laskowski | b5f40e5 | 2022-09-29 13:25:05 -0400 | [diff] [blame] | 971 | |
| 972 | static_cast<void>(mScheduler->schedule( |
| 973 | [this]() FTL_FAKE_GUARD(kMainThreadContext) { initializeDisplays(); })); |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 974 | |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 975 | mPowerAdvisor->init(); |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 976 | |
Bruno BELANYI | fd02878 | 2023-10-13 14:39:53 +0000 | [diff] [blame] | 977 | if (base::GetBoolProperty("service.sf.prime_shader_cache"s, true)) { |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 978 | if (setSchedFifo(false) != NO_ERROR) { |
| 979 | ALOGW("Can't set SCHED_OTHER for primeCache"); |
| 980 | } |
| 981 | |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 982 | mRenderEnginePrimeCacheFuture.callOnce([this] { |
Russell Myers | 3348c74 | 2024-04-29 20:22:42 +0000 | [diff] [blame] | 983 | renderengine::PrimeCacheConfig config; |
| 984 | config.cacheHolePunchLayer = |
| 985 | base::GetBoolProperty("debug.sf.prime_shader_cache.hole_punch"s, true); |
| 986 | config.cacheSolidLayers = |
| 987 | base::GetBoolProperty("debug.sf.prime_shader_cache.solid_layers"s, true); |
| 988 | config.cacheSolidDimmedLayers = |
| 989 | base::GetBoolProperty("debug.sf.prime_shader_cache.solid_dimmed_layers"s, true); |
| 990 | config.cacheImageLayers = |
| 991 | base::GetBoolProperty("debug.sf.prime_shader_cache.image_layers"s, true); |
| 992 | config.cacheImageDimmedLayers = |
| 993 | base::GetBoolProperty("debug.sf.prime_shader_cache.image_dimmed_layers"s, true); |
| 994 | config.cacheClippedLayers = |
| 995 | base::GetBoolProperty("debug.sf.prime_shader_cache.clipped_layers"s, true); |
| 996 | config.cacheShadowLayers = |
| 997 | base::GetBoolProperty("debug.sf.prime_shader_cache.shadow_layers"s, true); |
| 998 | config.cachePIPImageLayers = |
| 999 | base::GetBoolProperty("debug.sf.prime_shader_cache.pip_image_layers"s, true); |
| 1000 | config.cacheTransparentImageDimmedLayers = base:: |
| 1001 | GetBoolProperty("debug.sf.prime_shader_cache.transparent_image_dimmed_layers"s, |
| 1002 | true); |
| 1003 | config.cacheClippedDimmedImageLayers = base:: |
| 1004 | GetBoolProperty("debug.sf.prime_shader_cache.clipped_dimmed_image_layers"s, |
| 1005 | true); |
| 1006 | // ro.surface_flinger.prime_chader_cache.ultrahdr exists as a previous ro property |
| 1007 | // which we maintain for backwards compatibility. |
| 1008 | config.cacheUltraHDR = |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 1009 | base::GetBoolProperty("ro.surface_flinger.prime_shader_cache.ultrahdr"s, false); |
Russell Myers | 3348c74 | 2024-04-29 20:22:42 +0000 | [diff] [blame] | 1010 | return getRenderEngine().primeCache(config); |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 1011 | }); |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 1012 | |
| 1013 | if (setSchedFifo(true) != NO_ERROR) { |
linkai | d696a4f | 2023-11-14 10:13:36 +0800 | [diff] [blame] | 1014 | ALOGW("Can't set SCHED_FIFO after primeCache"); |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 1015 | } |
Steven Thomas | 137d4bc | 2019-07-25 16:55:14 -0700 | [diff] [blame] | 1016 | } |
Dan Stoza | 4e63777 | 2016-07-28 13:31:51 -0700 | [diff] [blame] | 1017 | |
Dominik Laskowski | efb131e | 2024-02-16 10:57:40 -0500 | [diff] [blame] | 1018 | // Avoid blocking the main thread on `init` to set properties. |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 1019 | mInitBootPropsFuture.callOnce([this] { |
| 1020 | return std::async(std::launch::async, &SurfaceFlinger::initBootProperties, this); |
| 1021 | }); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 1023 | initTransactionTraceWriter(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1024 | ALOGV("Done initializing"); |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Dominik Laskowski | efb131e | 2024-02-16 10:57:40 -0500 | [diff] [blame] | 1027 | // During boot, offload `initBootProperties` to another thread. `property_set` depends on |
| 1028 | // `property_service`, which may be delayed by slow operations like `mount_all --late` in |
| 1029 | // the `init` process. See b/34499826 and b/63844978. |
| 1030 | void SurfaceFlinger::initBootProperties() { |
| 1031 | property_set("service.sf.present_timestamp", mHasReliablePresentFences ? "1" : "0"); |
| 1032 | |
Dominik Laskowski | b0fdfe7 | 2024-02-21 10:42:23 -0500 | [diff] [blame] | 1033 | if (base::GetBoolProperty("debug.sf.boot_animation"s, true)) { |
| 1034 | // Reset and (if needed) start BootAnimation. |
| 1035 | property_set("service.bootanim.exit", "0"); |
| 1036 | property_set("service.bootanim.progress", "0"); |
| 1037 | property_set("ctl.start", "bootanim"); |
| 1038 | } |
Dominik Laskowski | efb131e | 2024-02-16 10:57:40 -0500 | [diff] [blame] | 1039 | } |
| 1040 | |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 1041 | void SurfaceFlinger::initTransactionTraceWriter() { |
| 1042 | if (!mTransactionTracing) { |
| 1043 | return; |
| 1044 | } |
| 1045 | TransactionTraceWriter::getInstance().setWriterFunction( |
| 1046 | [&](const std::string& filename, bool overwrite) { |
| 1047 | auto writeFn = [&]() { |
Patrick Williams | 8b731c1 | 2023-10-24 09:56:24 -0500 | [diff] [blame] | 1048 | if (!overwrite && fileNewerThan(filename, std::chrono::minutes{10})) { |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 1049 | ALOGD("TransactionTraceWriter: file=%s already exists", filename.c_str()); |
| 1050 | return; |
| 1051 | } |
Wenhui Yang | eafc18a | 2024-05-14 17:15:52 +0000 | [diff] [blame] | 1052 | ALOGD("TransactionTraceWriter: writing file=%s", filename.c_str()); |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 1053 | mTransactionTracing->writeToFile(filename); |
Wenhui Yang | eafc18a | 2024-05-14 17:15:52 +0000 | [diff] [blame] | 1054 | mTransactionTracing->flush(); |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 1055 | }; |
| 1056 | if (std::this_thread::get_id() == mMainThreadId) { |
| 1057 | writeFn(); |
| 1058 | } else { |
| 1059 | mScheduler->schedule(writeFn).get(); |
| 1060 | } |
| 1061 | }); |
| 1062 | } |
| 1063 | |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 1064 | void SurfaceFlinger::readPersistentProperties() { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 1065 | Mutex::Autolock _l(mStateLock); |
| 1066 | |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 1067 | char value[PROPERTY_VALUE_MAX]; |
| 1068 | |
| 1069 | property_get("persist.sys.sf.color_saturation", value, "1.0"); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1070 | mGlobalSaturationFactor = atof(value); |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 1071 | updateColorMatrixLocked(); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1072 | ALOGV("Saturation is set to %.2f", mGlobalSaturationFactor); |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 1073 | |
| 1074 | property_get("persist.sys.sf.native_mode", value, "0"); |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 1075 | mDisplayColorSetting = static_cast<DisplayColorSetting>(atoi(value)); |
Peiyong Lin | a3ea559 | 2019-02-10 14:45:00 -0800 | [diff] [blame] | 1076 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1077 | mForceColorMode = |
| 1078 | static_cast<ui::ColorMode>(base::GetIntProperty("persist.sys.sf.color_mode"s, 0)); |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 1081 | status_t SurfaceFlinger::getSupportedFrameTimestamps( |
| 1082 | std::vector<FrameEvent>* outSupported) const { |
| 1083 | *outSupported = { |
| 1084 | FrameEvent::REQUESTED_PRESENT, |
| 1085 | FrameEvent::ACQUIRE, |
| 1086 | FrameEvent::LATCH, |
| 1087 | FrameEvent::FIRST_REFRESH_START, |
| 1088 | FrameEvent::LAST_REFRESH_START, |
| 1089 | FrameEvent::GPU_COMPOSITION_DONE, |
| 1090 | FrameEvent::DEQUEUE_READY, |
| 1091 | FrameEvent::RELEASE, |
| 1092 | }; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 1093 | |
Dominik Laskowski | e62606d | 2024-02-22 11:42:13 -0500 | [diff] [blame] | 1094 | if (mHasReliablePresentFences) { |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 1095 | outSupported->push_back(FrameEvent::DISPLAY_PRESENT); |
| 1096 | } |
| 1097 | return NO_ERROR; |
| 1098 | } |
| 1099 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1100 | status_t SurfaceFlinger::getDisplayState(const sp<IBinder>& displayToken, ui::DisplayState* state) { |
| 1101 | if (!displayToken || !state) { |
| 1102 | return BAD_VALUE; |
| 1103 | } |
| 1104 | |
| 1105 | Mutex::Autolock lock(mStateLock); |
| 1106 | |
| 1107 | const auto display = getDisplayDeviceLocked(displayToken); |
| 1108 | if (!display) { |
| 1109 | return NAME_NOT_FOUND; |
| 1110 | } |
| 1111 | |
| 1112 | state->layerStack = display->getLayerStack(); |
| 1113 | state->orientation = display->getOrientation(); |
| 1114 | |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 1115 | const Rect layerStackRect = display->getLayerStackSpaceRect(); |
| 1116 | state->layerStackSpaceRect = |
| 1117 | layerStackRect.isValid() ? layerStackRect.getSize() : display->getSize(); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1118 | |
| 1119 | return NO_ERROR; |
| 1120 | } |
| 1121 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 1122 | status_t SurfaceFlinger::getStaticDisplayInfo(int64_t displayId, ui::StaticDisplayInfo* info) { |
| 1123 | if (!info) { |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1124 | return BAD_VALUE; |
| 1125 | } |
| 1126 | |
| 1127 | Mutex::Autolock lock(mStateLock); |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 1128 | const auto id = DisplayId::fromValue<PhysicalDisplayId>(static_cast<uint64_t>(displayId)); |
| 1129 | const auto displayOpt = mPhysicalDisplays.get(*id).and_then(getDisplayDeviceAndSnapshot()); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1130 | |
| 1131 | if (!displayOpt) { |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1132 | return NAME_NOT_FOUND; |
| 1133 | } |
| 1134 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1135 | const auto& [display, snapshotRef] = *displayOpt; |
| 1136 | const auto& snapshot = snapshotRef.get(); |
| 1137 | |
| 1138 | info->connectionType = snapshot.connectionType(); |
| 1139 | info->deviceProductInfo = snapshot.deviceProductInfo(); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1140 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1141 | if (mEmulatedDisplayDensity) { |
| 1142 | info->density = mEmulatedDisplayDensity; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1143 | } else { |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1144 | info->density = info->connectionType == ui::DisplayConnectionType::Internal |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1145 | ? mInternalDisplayDensity |
Dominik Laskowski | 55c8540 | 2020-01-21 16:25:47 -0800 | [diff] [blame] | 1146 | : FALLBACK_DENSITY; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1147 | } |
Dominik Laskowski | d125d0e | 2020-05-08 12:36:39 -0700 | [diff] [blame] | 1148 | info->density /= ACONFIGURATION_DENSITY_MEDIUM; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1149 | |
| 1150 | info->secure = display->isSecure(); |
ramindani | 06e518e | 2022-03-14 18:47:53 +0000 | [diff] [blame] | 1151 | info->installOrientation = display->getPhysicalOrientation(); |
Vishnu Nair | d0a8965 | 2022-01-13 12:05:54 -0800 | [diff] [blame] | 1152 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1153 | return NO_ERROR; |
| 1154 | } |
| 1155 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 1156 | void SurfaceFlinger::getDynamicDisplayInfoInternal(ui::DynamicDisplayInfo*& info, |
| 1157 | const sp<DisplayDevice>& display, |
| 1158 | const display::DisplaySnapshot& snapshot) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1159 | const auto& displayModes = snapshot.displayModes(); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1160 | info->supportedDisplayModes.clear(); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1161 | info->supportedDisplayModes.reserve(displayModes.size()); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1162 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1163 | for (const auto& [id, mode] : displayModes) { |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1164 | ui::DisplayMode outMode; |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1165 | outMode.id = ftl::to_underlying(id); |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 1166 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1167 | auto [width, height] = mode->getResolution(); |
| 1168 | auto [xDpi, yDpi] = mode->getDpi(); |
Yiwei Zhang | 27de5df | 2018-08-23 17:04:51 -0700 | [diff] [blame] | 1169 | |
ramindani | 06e518e | 2022-03-14 18:47:53 +0000 | [diff] [blame] | 1170 | if (const auto physicalOrientation = display->getPhysicalOrientation(); |
| 1171 | physicalOrientation == ui::ROTATION_90 || physicalOrientation == ui::ROTATION_270) { |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1172 | std::swap(width, height); |
| 1173 | std::swap(xDpi, yDpi); |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1176 | outMode.resolution = ui::Size(width, height); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 1177 | |
Huan Song | f7eeb10 | 2022-03-28 11:02:38 -0700 | [diff] [blame] | 1178 | outMode.xDpi = xDpi; |
| 1179 | outMode.yDpi = yDpi; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1180 | |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 1181 | const auto peakFps = mode->getPeakFps(); |
Alec Mouri | 55e3103 | 2023-10-02 20:34:18 +0000 | [diff] [blame] | 1182 | outMode.peakRefreshRate = peakFps.getValue(); |
Alec Mouri | d122a1c | 2023-09-30 01:36:33 +0000 | [diff] [blame] | 1183 | outMode.vsyncRate = mode->getVsyncRate().getValue(); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1184 | |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 1185 | const auto vsyncConfigSet = mScheduler->getVsyncConfiguration().getConfigsForRefreshRate( |
Alec Mouri | 55e3103 | 2023-10-02 20:34:18 +0000 | [diff] [blame] | 1186 | Fps::fromValue(outMode.peakRefreshRate)); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1187 | outMode.appVsyncOffset = vsyncConfigSet.late.appOffset; |
| 1188 | outMode.sfVsyncOffset = vsyncConfigSet.late.sfOffset; |
| 1189 | outMode.group = mode->getGroup(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1190 | |
Andy McFadden | 91b2ca8 | 2014-06-13 14:04:23 -0700 | [diff] [blame] | 1191 | // This is how far in advance a buffer must be queued for |
| 1192 | // presentation at a given time. If you want a buffer to appear |
| 1193 | // on the screen at time N, you must submit the buffer before |
| 1194 | // (N - presentationDeadline). |
| 1195 | // |
| 1196 | // Normally it's one full refresh period (to give SF a chance to |
| 1197 | // latch the buffer), but this can be reduced by configuring a |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 1198 | // VsyncController offset. Any additional delays introduced by the hardware |
Andy McFadden | 91b2ca8 | 2014-06-13 14:04:23 -0700 | [diff] [blame] | 1199 | // composer or panel must be accounted for here. |
| 1200 | // |
| 1201 | // We add an additional 1ms to allow for processing time and |
| 1202 | // differences between the ideal and actual refresh rate. |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 1203 | outMode.presentationDeadline = peakFps.getPeriodNsecs() - outMode.sfVsyncOffset + 1000000; |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 1204 | excludeDolbyVisionIf4k30Present(display->getHdrCapabilities().getSupportedHdrTypes(), |
| 1205 | outMode); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1206 | info->supportedDisplayModes.push_back(outMode); |
Mathias Agopian | 8b736f1 | 2012-08-13 17:54:26 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1209 | info->supportedColorModes = snapshot.filterColorModes(mSupportsWideColor); |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1210 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1211 | const PhysicalDisplayId displayId = snapshot.displayId(); |
| 1212 | |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 1213 | const auto mode = display->refreshRateSelector().getActiveMode(); |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1214 | info->activeDisplayModeId = ftl::to_underlying(mode.modePtr->getId()); |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 1215 | info->renderFrameRate = mode.fps.getValue(); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1216 | info->activeColorMode = display->getCompositionDisplay()->getState().colorMode; |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 1217 | info->hdrCapabilities = filterOut4k30(display->getHdrCapabilities()); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1218 | |
Marin Shalamanov | b173f75 | 2021-02-16 19:38:36 +0100 | [diff] [blame] | 1219 | info->autoLowLatencyModeSupported = |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1220 | getHwComposer().hasDisplayCapability(displayId, |
Leon Scroggins III | 5967aec | 2021-12-29 11:14:22 -0500 | [diff] [blame] | 1221 | DisplayCapability::AUTO_LOW_LATENCY_MODE); |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1222 | info->gameContentTypeSupported = |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1223 | getHwComposer().supportsContentType(displayId, hal::ContentType::GAME); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1224 | |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1225 | info->preferredBootDisplayMode = static_cast<ui::DisplayModeId>(-1); |
Kriti Dang | ac0a74d | 2022-03-16 11:52:35 +0100 | [diff] [blame] | 1226 | |
| 1227 | if (getHwComposer().hasCapability(Capability::BOOT_DISPLAY_CONFIG)) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1228 | if (const auto hwcId = getHwComposer().getPreferredBootDisplayMode(displayId)) { |
| 1229 | if (const auto modeId = snapshot.translateModeId(*hwcId)) { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1230 | info->preferredBootDisplayMode = ftl::to_underlying(*modeId); |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | } |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 1234 | } |
Kriti Dang | 646f8ec | 2022-01-18 14:35:02 +0100 | [diff] [blame] | 1235 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 1236 | status_t SurfaceFlinger::getDynamicDisplayInfoFromId(int64_t physicalDisplayId, |
| 1237 | ui::DynamicDisplayInfo* info) { |
| 1238 | if (!info) { |
| 1239 | return BAD_VALUE; |
| 1240 | } |
| 1241 | |
| 1242 | Mutex::Autolock lock(mStateLock); |
| 1243 | |
| 1244 | const auto id_ = |
| 1245 | DisplayId::fromValue<PhysicalDisplayId>(static_cast<uint64_t>(physicalDisplayId)); |
| 1246 | const auto displayOpt = mPhysicalDisplays.get(*id_).and_then(getDisplayDeviceAndSnapshot()); |
| 1247 | |
| 1248 | if (!displayOpt) { |
| 1249 | return NAME_NOT_FOUND; |
| 1250 | } |
| 1251 | |
| 1252 | const auto& [display, snapshotRef] = *displayOpt; |
| 1253 | getDynamicDisplayInfoInternal(info, display, snapshotRef.get()); |
| 1254 | return NO_ERROR; |
| 1255 | } |
| 1256 | |
| 1257 | status_t SurfaceFlinger::getDynamicDisplayInfoFromToken(const sp<IBinder>& displayToken, |
| 1258 | ui::DynamicDisplayInfo* info) { |
| 1259 | if (!displayToken || !info) { |
| 1260 | return BAD_VALUE; |
| 1261 | } |
| 1262 | |
| 1263 | Mutex::Autolock lock(mStateLock); |
| 1264 | |
| 1265 | const auto displayOpt = ftl::find_if(mPhysicalDisplays, PhysicalDisplay::hasToken(displayToken)) |
| 1266 | .transform(&ftl::to_mapped_ref<PhysicalDisplays>) |
| 1267 | .and_then(getDisplayDeviceAndSnapshot()); |
| 1268 | |
| 1269 | if (!displayOpt) { |
| 1270 | return NAME_NOT_FOUND; |
| 1271 | } |
| 1272 | |
| 1273 | const auto& [display, snapshotRef] = *displayOpt; |
| 1274 | getDynamicDisplayInfoInternal(info, display, snapshotRef.get()); |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 1275 | return NO_ERROR; |
| 1276 | } |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 1277 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1278 | status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& displayToken, |
| 1279 | DisplayStatInfo* outStats) { |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 1280 | if (!outStats) { |
Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 1281 | return BAD_VALUE; |
| 1282 | } |
| 1283 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1284 | std::optional<PhysicalDisplayId> displayIdOpt; |
| 1285 | { |
| 1286 | Mutex::Autolock lock(mStateLock); |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 1287 | if (displayToken) { |
| 1288 | displayIdOpt = getPhysicalDisplayIdLocked(displayToken); |
| 1289 | if (!displayIdOpt) { |
| 1290 | ALOGW("%s: Invalid physical display token %p", __func__, displayToken.get()); |
| 1291 | return NAME_NOT_FOUND; |
| 1292 | } |
| 1293 | } else { |
| 1294 | // TODO (b/277364366): Clients should be updated to pass in the display they |
| 1295 | // want, rather than us picking an arbitrary one (the active display, in this |
| 1296 | // case). |
| 1297 | displayIdOpt = mActiveDisplayId; |
| 1298 | } |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1299 | } |
| 1300 | |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 1301 | const auto schedule = mScheduler->getVsyncSchedule(displayIdOpt); |
| 1302 | if (!schedule) { |
| 1303 | ALOGE("%s: Missing VSYNC schedule for display %s!", __func__, |
| 1304 | to_string(*displayIdOpt).c_str()); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1305 | return NAME_NOT_FOUND; |
| 1306 | } |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1307 | outStats->vsyncTime = schedule->vsyncDeadlineAfter(TimePoint::now()).ns(); |
| 1308 | outStats->vsyncPeriod = schedule->period().ns(); |
Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 1309 | return NO_ERROR; |
| 1310 | } |
| 1311 | |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 1312 | void SurfaceFlinger::setDesiredMode(display::DisplayModeRequest&& desiredMode) { |
| 1313 | const auto mode = desiredMode.mode; |
| 1314 | const auto displayId = mode.modePtr->getPhysicalDisplayId(); |
| 1315 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 1316 | SFTRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str()); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1317 | |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 1318 | const bool emitEvent = desiredMode.emitEvent; |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 1319 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1320 | using DesiredModeAction = display::DisplayModeController::DesiredModeAction; |
| 1321 | |
| 1322 | switch (mDisplayModeController.setDesiredMode(displayId, std::move(desiredMode))) { |
| 1323 | case DesiredModeAction::InitiateDisplayModeSwitch: { |
| 1324 | const auto selectorPtr = mDisplayModeController.selectorPtrFor(displayId); |
| 1325 | if (!selectorPtr) break; |
| 1326 | |
| 1327 | const Fps renderRate = selectorPtr->getActiveMode().fps; |
| 1328 | |
| 1329 | // DisplayModeController::setDesiredMode updated the render rate, so inform Scheduler. |
| 1330 | mScheduler->setRenderRate(displayId, renderRate, true /* applyImmediately */); |
Ady Abraham | 95a37e2 | 2023-01-18 22:57:28 -0800 | [diff] [blame] | 1331 | |
| 1332 | // Schedule a new frame to initiate the display mode switch. |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1333 | scheduleComposite(FrameHint::kNone); |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 1334 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1335 | // Start receiving vsync samples now, so that we can detect a period |
| 1336 | // switch. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1337 | mScheduler->resyncToHardwareVsync(displayId, true /* allowToEnable */, |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 1338 | mode.modePtr.get()); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1339 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1340 | // As we called to set period, we will call to onRefreshRateChangeCompleted once |
| 1341 | // VsyncController model is locked. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1342 | mScheduler->modulateVsync(displayId, &VsyncModulator::onRefreshRateChangeInitiated); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1343 | |
| 1344 | if (displayId == mActiveDisplayId) { |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 1345 | mScheduler->updatePhaseConfiguration(mode.fps); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1346 | } |
| 1347 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1348 | mScheduler->setModeChangePending(true); |
| 1349 | break; |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1350 | } |
| 1351 | case DesiredModeAction::InitiateRenderRateSwitch: |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 1352 | mScheduler->setRenderRate(displayId, mode.fps, /*applyImmediately*/ false); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1353 | |
| 1354 | if (displayId == mActiveDisplayId) { |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 1355 | mScheduler->updatePhaseConfiguration(mode.fps); |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 1356 | } |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1357 | |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1358 | if (emitEvent) { |
| 1359 | dispatchDisplayModeChangeEvent(displayId, mode); |
| 1360 | } |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1361 | break; |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1362 | case DesiredModeAction::None: |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1363 | break; |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1364 | } |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1367 | status_t SurfaceFlinger::setActiveModeFromBackdoor(const sp<display::DisplayToken>& displayToken, |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 1368 | DisplayModeId modeId, Fps minFps, Fps maxFps) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 1369 | SFTRACE_CALL(); |
Ady Abraham | 838de06 | 2019-02-04 10:24:03 -0800 | [diff] [blame] | 1370 | |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 1371 | if (!displayToken) { |
| 1372 | return BAD_VALUE; |
| 1373 | } |
Ady Abraham | 838de06 | 2019-02-04 10:24:03 -0800 | [diff] [blame] | 1374 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1375 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1376 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(kMainThreadContext) -> status_t { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1377 | const auto displayOpt = |
| 1378 | FTL_FAKE_GUARD(mStateLock, |
| 1379 | ftl::find_if(mPhysicalDisplays, |
| 1380 | PhysicalDisplay::hasToken(displayToken)) |
| 1381 | .transform(&ftl::to_mapped_ref<PhysicalDisplays>) |
| 1382 | .and_then(getDisplayDeviceAndSnapshot())); |
| 1383 | if (!displayOpt) { |
| 1384 | ALOGE("%s: Invalid physical display token %p", whence, displayToken.get()); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 1385 | return NAME_NOT_FOUND; |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1388 | const auto& [display, snapshotRef] = *displayOpt; |
| 1389 | const auto& snapshot = snapshotRef.get(); |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1390 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1391 | const auto fpsOpt = snapshot.displayModes().get(modeId).transform( |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 1392 | [](const DisplayModePtr& mode) { return mode->getPeakFps(); }); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1393 | |
| 1394 | if (!fpsOpt) { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1395 | ALOGE("%s: Invalid mode %d for display %s", whence, ftl::to_underlying(modeId), |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1396 | to_string(snapshot.displayId()).c_str()); |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1397 | return BAD_VALUE; |
| 1398 | } |
| 1399 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1400 | const Fps fps = *fpsOpt; |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 1401 | const FpsRange physical = {fps, fps}; |
| 1402 | const FpsRange render = {minFps.isValid() ? minFps : fps, maxFps.isValid() ? maxFps : fps}; |
| 1403 | const FpsRanges ranges = {physical, render}; |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 1404 | |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1405 | // Keep the old switching type. |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1406 | const bool allowGroupSwitching = |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 1407 | display->refreshRateSelector().getCurrentPolicy().allowGroupSwitching; |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1408 | |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 1409 | const scheduler::RefreshRateSelector::DisplayManagerPolicy policy{modeId, ranges, ranges, |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 1410 | allowGroupSwitching}; |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 1411 | |
| 1412 | return setDesiredDisplayModeSpecsInternal(display, policy); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 1413 | }); |
| 1414 | |
| 1415 | return future.get(); |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1416 | } |
| 1417 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1418 | void SurfaceFlinger::finalizeDisplayModeChange(PhysicalDisplayId displayId) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 1419 | SFTRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str()); |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1420 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1421 | const auto pendingModeOpt = mDisplayModeController.getPendingMode(displayId); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1422 | if (!pendingModeOpt) { |
| 1423 | // There is no pending mode change. This can happen if the active |
| 1424 | // display changed and the mode change happened on a different display. |
| 1425 | return; |
| 1426 | } |
Ady Abraham | faac6da | 2021-07-15 10:04:40 -0700 | [diff] [blame] | 1427 | |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1428 | const auto& activeMode = pendingModeOpt->mode; |
Dominik Laskowski | 1ab20c5 | 2023-11-27 11:45:57 -0500 | [diff] [blame] | 1429 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1430 | if (const auto oldResolution = |
| 1431 | mDisplayModeController.getActiveMode(displayId).modePtr->getResolution(); |
| 1432 | oldResolution != activeMode.modePtr->getResolution()) { |
| 1433 | Mutex::Autolock lock(mStateLock); |
| 1434 | |
| 1435 | auto& state = mCurrentState.displays.editValueFor(getPhysicalDisplayTokenLocked(displayId)); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1436 | // We need to generate new sequenceId in order to recreate the display (and this |
| 1437 | // way the framebuffer). |
| 1438 | state.sequenceId = DisplayDeviceState{}.sequenceId; |
| 1439 | state.physical->activeMode = activeMode.modePtr.get(); |
| 1440 | processDisplayChangesLocked(); |
Marin Shalamanov | 993ddf4 | 2021-05-26 16:54:40 +0200 | [diff] [blame] | 1441 | |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1442 | // processDisplayChangesLocked will update all necessary components so we're done here. |
| 1443 | return; |
| 1444 | } |
Alec Mouri | 8de697e | 2020-03-19 10:52:01 -0700 | [diff] [blame] | 1445 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1446 | mDisplayModeController.finalizeModeChange(displayId, activeMode.modePtr->getId(), |
| 1447 | activeMode.modePtr->getVsyncRate(), activeMode.fps); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1448 | |
| 1449 | if (displayId == mActiveDisplayId) { |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 1450 | mScheduler->updatePhaseConfiguration(activeMode.fps); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | if (pendingModeOpt->emitEvent) { |
| 1454 | dispatchDisplayModeChangeEvent(displayId, activeMode); |
| 1455 | } |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1456 | } |
| 1457 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1458 | void SurfaceFlinger::dropModeRequest(PhysicalDisplayId displayId) { |
| 1459 | mDisplayModeController.clearDesiredMode(displayId); |
| 1460 | if (displayId == mActiveDisplayId) { |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1461 | // TODO(b/255635711): Check for pending mode changes on other displays. |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1462 | mScheduler->setModeChangePending(false); |
| 1463 | } |
Ady Abraham | 53852a5 | 2019-05-28 18:07:44 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1466 | void SurfaceFlinger::applyActiveMode(PhysicalDisplayId displayId) { |
| 1467 | const auto activeModeOpt = mDisplayModeController.getDesiredMode(displayId); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1468 | auto activeModePtr = activeModeOpt->mode.modePtr; |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1469 | const auto renderFps = activeModeOpt->mode.fps; |
Dominik Laskowski | 1ab20c5 | 2023-11-27 11:45:57 -0500 | [diff] [blame] | 1470 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1471 | dropModeRequest(displayId); |
Dominik Laskowski | 1ab20c5 | 2023-11-27 11:45:57 -0500 | [diff] [blame] | 1472 | |
| 1473 | constexpr bool kAllowToEnable = true; |
| 1474 | mScheduler->resyncToHardwareVsync(displayId, kAllowToEnable, std::move(activeModePtr).take()); |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 1475 | mScheduler->setRenderRate(displayId, renderFps, /*applyImmediately*/ true); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1476 | |
| 1477 | if (displayId == mActiveDisplayId) { |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 1478 | mScheduler->updatePhaseConfiguration(renderFps); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1479 | } |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1480 | } |
| 1481 | |
Ady Abraham | 0b10360 | 2024-05-21 20:37:49 +0000 | [diff] [blame] | 1482 | void SurfaceFlinger::initiateDisplayModeChanges() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 1483 | SFTRACE_CALL(); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1484 | |
Marin Shalamanov | dd59431 | 2021-11-09 16:37:37 +0100 | [diff] [blame] | 1485 | std::optional<PhysicalDisplayId> displayToUpdateImmediately; |
| 1486 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1487 | for (const auto& [displayId, physical] : FTL_FAKE_GUARD(mStateLock, mPhysicalDisplays)) { |
| 1488 | auto desiredModeOpt = mDisplayModeController.getDesiredMode(displayId); |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 1489 | if (!desiredModeOpt) { |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1490 | continue; |
| 1491 | } |
| 1492 | |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1493 | const auto desiredModeId = desiredModeOpt->mode.modePtr->getId(); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1494 | const auto displayModePtrOpt = physical.snapshot().displayModes().get(desiredModeId); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1495 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1496 | if (!displayModePtrOpt) { |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1497 | ALOGW("Desired display mode is no longer supported. Mode ID = %d", |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1498 | ftl::to_underlying(desiredModeId)); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1499 | continue; |
| 1500 | } |
| 1501 | |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1502 | ALOGV("%s changing active mode to %d(%s) for display %s", __func__, |
| 1503 | ftl::to_underlying(desiredModeId), |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1504 | to_string(displayModePtrOpt->get()->getVsyncRate()).c_str(), |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1505 | to_string(displayId).c_str()); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1506 | |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 1507 | if ((!FlagManager::getInstance().connected_display() || !desiredModeOpt->force) && |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1508 | mDisplayModeController.getActiveMode(displayId) == desiredModeOpt->mode) { |
| 1509 | applyActiveMode(displayId); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1510 | continue; |
| 1511 | } |
| 1512 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1513 | const auto selectorPtr = mDisplayModeController.selectorPtrFor(displayId); |
| 1514 | |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1515 | // Desired active mode was set, it is different than the mode currently in use, however |
| 1516 | // allowed modes might have changed by the time we process the refresh. |
| 1517 | // Make sure the desired mode is still allowed |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1518 | if (!selectorPtr->isModeAllowed(desiredModeOpt->mode)) { |
| 1519 | dropModeRequest(displayId); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1520 | continue; |
| 1521 | } |
| 1522 | |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1523 | // TODO(b/142753666) use constrains |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1524 | hal::VsyncPeriodChangeConstraints constraints; |
| 1525 | constraints.desiredTimeNanos = systemTime(); |
| 1526 | constraints.seamlessRequired = false; |
| 1527 | hal::VsyncPeriodChangeTimeline outTimeline; |
| 1528 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1529 | if (!mDisplayModeController.initiateModeChange(displayId, std::move(*desiredModeOpt), |
| 1530 | constraints, outTimeline)) { |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1531 | continue; |
| 1532 | } |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 1533 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1534 | selectorPtr->onModeChangeInitiated(); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1535 | mScheduler->onNewVsyncPeriodChangeTimeline(outTimeline); |
| 1536 | |
Marin Shalamanov | dd59431 | 2021-11-09 16:37:37 +0100 | [diff] [blame] | 1537 | if (outTimeline.refreshRequired) { |
Ady Abraham | 0b10360 | 2024-05-21 20:37:49 +0000 | [diff] [blame] | 1538 | scheduleComposite(FrameHint::kNone); |
Marin Shalamanov | dd59431 | 2021-11-09 16:37:37 +0100 | [diff] [blame] | 1539 | } else { |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1540 | // TODO(b/255635711): Remove `displayToUpdateImmediately` to `finalizeDisplayModeChange` |
| 1541 | // for all displays. This was only needed when the loop iterated over `mDisplays` rather |
| 1542 | // than `mPhysicalDisplays`. |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1543 | displayToUpdateImmediately = displayId; |
Marin Shalamanov | dd59431 | 2021-11-09 16:37:37 +0100 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | if (displayToUpdateImmediately) { |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1548 | const auto displayId = *displayToUpdateImmediately; |
| 1549 | finalizeDisplayModeChange(displayId); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1550 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1551 | const auto desiredModeOpt = mDisplayModeController.getDesiredMode(displayId); |
| 1552 | if (desiredModeOpt && |
| 1553 | mDisplayModeController.getActiveMode(displayId) == desiredModeOpt->mode) { |
| 1554 | applyActiveMode(displayId); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1555 | } |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1556 | } |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 1557 | } |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 1558 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 1559 | void SurfaceFlinger::disableExpensiveRendering() { |
Dominik Laskowski | 4d5052d | 2022-03-23 10:35:47 -0700 | [diff] [blame] | 1560 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1561 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 1562 | SFTRACE_NAME(whence); |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 1563 | if (mPowerAdvisor->isUsingExpensiveRendering()) { |
Dominik Laskowski | bc6c860 | 2022-01-11 08:53:24 -0800 | [diff] [blame] | 1564 | for (const auto& [_, display] : mDisplays) { |
| 1565 | constexpr bool kDisable = false; |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 1566 | mPowerAdvisor->setExpensiveRenderingExpected(display->getId(), kDisable); |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 1567 | } |
| 1568 | } |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1569 | }); |
| 1570 | |
| 1571 | future.wait(); |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 1574 | status_t SurfaceFlinger::getDisplayNativePrimaries(const sp<IBinder>& displayToken, |
Dominik Laskowski | 3c36324 | 2022-04-07 10:44:12 -0700 | [diff] [blame] | 1575 | ui::DisplayPrimaries& primaries) { |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 1576 | if (!displayToken) { |
| 1577 | return BAD_VALUE; |
| 1578 | } |
| 1579 | |
Dominik Laskowski | 3c36324 | 2022-04-07 10:44:12 -0700 | [diff] [blame] | 1580 | Mutex::Autolock lock(mStateLock); |
| 1581 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1582 | const auto display = ftl::find_if(mPhysicalDisplays, PhysicalDisplay::hasToken(displayToken)) |
| 1583 | .transform(&ftl::to_mapped_ref<PhysicalDisplays>); |
Dominik Laskowski | 3c36324 | 2022-04-07 10:44:12 -0700 | [diff] [blame] | 1584 | if (!display) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1585 | return NAME_NOT_FOUND; |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 1586 | } |
| 1587 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1588 | if (!display.transform(&PhysicalDisplay::isInternal).value()) { |
Dominik Laskowski | 3c36324 | 2022-04-07 10:44:12 -0700 | [diff] [blame] | 1589 | return INVALID_OPERATION; |
| 1590 | } |
| 1591 | |
| 1592 | // TODO(b/229846990): For now, assume that all internal displays have the same primaries. |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1593 | primaries = mInternalDisplayPrimaries; |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 1594 | return NO_ERROR; |
| 1595 | } |
| 1596 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1597 | status_t SurfaceFlinger::setActiveColorMode(const sp<IBinder>& displayToken, ui::ColorMode mode) { |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1598 | if (!displayToken) { |
| 1599 | return BAD_VALUE; |
| 1600 | } |
| 1601 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1602 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1603 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) -> status_t { |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1604 | const auto displayOpt = |
| 1605 | ftl::find_if(mPhysicalDisplays, PhysicalDisplay::hasToken(displayToken)) |
| 1606 | .transform(&ftl::to_mapped_ref<PhysicalDisplays>) |
| 1607 | .and_then(getDisplayDeviceAndSnapshot()); |
| 1608 | |
| 1609 | if (!displayOpt) { |
| 1610 | ALOGE("%s: Invalid physical display token %p", whence, displayToken.get()); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1611 | return NAME_NOT_FOUND; |
| 1612 | } |
| 1613 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1614 | const auto& [display, snapshotRef] = *displayOpt; |
| 1615 | const auto& snapshot = snapshotRef.get(); |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 1616 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1617 | const auto modes = snapshot.filterColorModes(mSupportsWideColor); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1618 | const bool exists = std::find(modes.begin(), modes.end(), mode) != modes.end(); |
| 1619 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1620 | if (mode < ui::ColorMode::NATIVE || !exists) { |
| 1621 | ALOGE("%s: Invalid color mode %s (%d) for display %s", whence, |
| 1622 | decodeColorMode(mode).c_str(), mode, to_string(snapshot.displayId()).c_str()); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1623 | return BAD_VALUE; |
| 1624 | } |
| 1625 | |
| 1626 | display->getCompositionDisplay()->setColorProfile( |
Alec Mouri | 88790f3 | 2023-07-21 01:25:14 +0000 | [diff] [blame] | 1627 | {mode, Dataspace::UNKNOWN, RenderIntent::COLORIMETRIC}); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1628 | |
| 1629 | return NO_ERROR; |
| 1630 | }); |
| 1631 | |
Dominik Laskowski | 0a5f921 | 2021-08-05 17:00:04 -0700 | [diff] [blame] | 1632 | // TODO(b/195698395): Propagate error. |
| 1633 | future.wait(); |
| 1634 | return NO_ERROR; |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1635 | } |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 1636 | |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1637 | status_t SurfaceFlinger::getBootDisplayModeSupport(bool* outSupport) const { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 1638 | auto future = mScheduler->schedule( |
| 1639 | [this] { return getHwComposer().hasCapability(Capability::BOOT_DISPLAY_CONFIG); }); |
| 1640 | |
| 1641 | *outSupport = future.get(); |
| 1642 | return NO_ERROR; |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1643 | } |
| 1644 | |
Sally Qi | bb866c1 | 2022-10-17 11:31:20 -0700 | [diff] [blame] | 1645 | status_t SurfaceFlinger::getOverlaySupport(gui::OverlayProperties* outProperties) const { |
| 1646 | const auto& aidlProperties = getHwComposer().getOverlaySupport(); |
| 1647 | // convert aidl OverlayProperties to gui::OverlayProperties |
| 1648 | outProperties->combinations.reserve(aidlProperties.combinations.size()); |
| 1649 | for (const auto& combination : aidlProperties.combinations) { |
| 1650 | std::vector<int32_t> pixelFormats; |
| 1651 | pixelFormats.reserve(combination.pixelFormats.size()); |
| 1652 | std::transform(combination.pixelFormats.cbegin(), combination.pixelFormats.cend(), |
| 1653 | std::back_inserter(pixelFormats), |
| 1654 | [](const auto& val) { return static_cast<int32_t>(val); }); |
Sally Qi | 4119e22 | 2023-01-13 14:52:18 -0800 | [diff] [blame] | 1655 | std::vector<int32_t> standards; |
| 1656 | standards.reserve(combination.standards.size()); |
| 1657 | std::transform(combination.standards.cbegin(), combination.standards.cend(), |
| 1658 | std::back_inserter(standards), |
| 1659 | [](const auto& val) { return static_cast<int32_t>(val); }); |
| 1660 | std::vector<int32_t> transfers; |
| 1661 | transfers.reserve(combination.transfers.size()); |
| 1662 | std::transform(combination.transfers.cbegin(), combination.transfers.cend(), |
| 1663 | std::back_inserter(transfers), |
| 1664 | [](const auto& val) { return static_cast<int32_t>(val); }); |
| 1665 | std::vector<int32_t> ranges; |
| 1666 | ranges.reserve(combination.ranges.size()); |
| 1667 | std::transform(combination.ranges.cbegin(), combination.ranges.cend(), |
| 1668 | std::back_inserter(ranges), |
Sally Qi | bb866c1 | 2022-10-17 11:31:20 -0700 | [diff] [blame] | 1669 | [](const auto& val) { return static_cast<int32_t>(val); }); |
| 1670 | gui::OverlayProperties::SupportedBufferCombinations outCombination; |
| 1671 | outCombination.pixelFormats = std::move(pixelFormats); |
Sally Qi | 4119e22 | 2023-01-13 14:52:18 -0800 | [diff] [blame] | 1672 | outCombination.standards = std::move(standards); |
| 1673 | outCombination.transfers = std::move(transfers); |
| 1674 | outCombination.ranges = std::move(ranges); |
Sally Qi | bb866c1 | 2022-10-17 11:31:20 -0700 | [diff] [blame] | 1675 | outProperties->combinations.emplace_back(outCombination); |
| 1676 | } |
Sally Qi | 8b4cb55 | 2022-12-14 16:24:31 -0800 | [diff] [blame] | 1677 | outProperties->supportMixedColorSpaces = aidlProperties.supportMixedColorSpaces; |
Sally Qi | 0cbd08b | 2022-08-17 12:12:28 -0700 | [diff] [blame] | 1678 | return NO_ERROR; |
| 1679 | } |
| 1680 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1681 | status_t SurfaceFlinger::setBootDisplayMode(const sp<display::DisplayToken>& displayToken, |
| 1682 | DisplayModeId modeId) { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1683 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1684 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) -> status_t { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1685 | const auto snapshotOpt = |
| 1686 | ftl::find_if(mPhysicalDisplays, PhysicalDisplay::hasToken(displayToken)) |
| 1687 | .transform(&ftl::to_mapped_ref<PhysicalDisplays>) |
| 1688 | .transform(&PhysicalDisplay::snapshotRef); |
| 1689 | |
| 1690 | if (!snapshotOpt) { |
| 1691 | ALOGE("%s: Invalid physical display token %p", whence, displayToken.get()); |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1692 | return NAME_NOT_FOUND; |
| 1693 | } |
Kriti Dang | f50d677 | 2022-02-18 15:09:12 +0100 | [diff] [blame] | 1694 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1695 | const auto& snapshot = snapshotOpt->get(); |
| 1696 | const auto hwcIdOpt = snapshot.displayModes().get(modeId).transform( |
| 1697 | [](const DisplayModePtr& mode) { return mode->getHwcId(); }); |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1698 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1699 | if (!hwcIdOpt) { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1700 | ALOGE("%s: Invalid mode %d for display %s", whence, ftl::to_underlying(modeId), |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1701 | to_string(snapshot.displayId()).c_str()); |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1702 | return BAD_VALUE; |
| 1703 | } |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1704 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1705 | return getHwComposer().setBootDisplayMode(snapshot.displayId(), *hwcIdOpt); |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1706 | }); |
| 1707 | return future.get(); |
| 1708 | } |
| 1709 | |
| 1710 | status_t SurfaceFlinger::clearBootDisplayMode(const sp<IBinder>& displayToken) { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1711 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1712 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) -> status_t { |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1713 | if (const auto displayId = getPhysicalDisplayIdLocked(displayToken)) { |
| 1714 | return getHwComposer().clearBootDisplayMode(*displayId); |
| 1715 | } else { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1716 | ALOGE("%s: Invalid display token %p", whence, displayToken.get()); |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1717 | return BAD_VALUE; |
| 1718 | } |
| 1719 | }); |
| 1720 | return future.get(); |
| 1721 | } |
| 1722 | |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1723 | status_t SurfaceFlinger::getHdrConversionCapabilities( |
| 1724 | std::vector<gui::HdrConversionCapability>* hdrConversionCapabilities) const { |
| 1725 | bool hdrOutputConversionSupport; |
| 1726 | getHdrOutputConversionSupport(&hdrOutputConversionSupport); |
| 1727 | if (hdrOutputConversionSupport == false) { |
| 1728 | ALOGE("hdrOutputConversion is not supported by this device."); |
| 1729 | return INVALID_OPERATION; |
| 1730 | } |
| 1731 | const auto aidlConversionCapability = getHwComposer().getHdrConversionCapabilities(); |
| 1732 | for (auto capability : aidlConversionCapability) { |
| 1733 | gui::HdrConversionCapability tempCapability; |
Kriti Dang | 3c243dc | 2023-03-07 15:00:13 +0100 | [diff] [blame] | 1734 | tempCapability.sourceType = static_cast<int>(capability.sourceType); |
| 1735 | tempCapability.outputType = static_cast<int>(capability.outputType); |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1736 | tempCapability.addsLatency = capability.addsLatency; |
| 1737 | hdrConversionCapabilities->push_back(tempCapability); |
| 1738 | } |
| 1739 | return NO_ERROR; |
| 1740 | } |
| 1741 | |
| 1742 | status_t SurfaceFlinger::setHdrConversionStrategy( |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 1743 | const gui::HdrConversionStrategy& hdrConversionStrategy, |
| 1744 | int32_t* outPreferredHdrOutputType) { |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1745 | bool hdrOutputConversionSupport; |
| 1746 | getHdrOutputConversionSupport(&hdrOutputConversionSupport); |
| 1747 | if (hdrOutputConversionSupport == false) { |
| 1748 | ALOGE("hdrOutputConversion is not supported by this device."); |
| 1749 | return INVALID_OPERATION; |
| 1750 | } |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1751 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) mutable -> status_t { |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1752 | using AidlHdrConversionStrategy = |
| 1753 | aidl::android::hardware::graphics::common::HdrConversionStrategy; |
| 1754 | using GuiHdrConversionStrategyTag = gui::HdrConversionStrategy::Tag; |
| 1755 | AidlHdrConversionStrategy aidlConversionStrategy; |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 1756 | status_t status; |
| 1757 | aidl::android::hardware::graphics::common::Hdr aidlPreferredHdrOutputType; |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1758 | switch (hdrConversionStrategy.getTag()) { |
| 1759 | case GuiHdrConversionStrategyTag::passthrough: { |
| 1760 | aidlConversionStrategy.set<AidlHdrConversionStrategy::Tag::passthrough>( |
| 1761 | hdrConversionStrategy.get<GuiHdrConversionStrategyTag::passthrough>()); |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 1762 | status = getHwComposer().setHdrConversionStrategy(aidlConversionStrategy, |
| 1763 | &aidlPreferredHdrOutputType); |
| 1764 | *outPreferredHdrOutputType = static_cast<int32_t>(aidlPreferredHdrOutputType); |
| 1765 | return status; |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1766 | } |
| 1767 | case GuiHdrConversionStrategyTag::autoAllowedHdrTypes: { |
| 1768 | auto autoHdrTypes = |
| 1769 | hdrConversionStrategy |
| 1770 | .get<GuiHdrConversionStrategyTag::autoAllowedHdrTypes>(); |
| 1771 | std::vector<aidl::android::hardware::graphics::common::Hdr> aidlAutoHdrTypes; |
| 1772 | for (auto type : autoHdrTypes) { |
| 1773 | aidlAutoHdrTypes.push_back( |
| 1774 | static_cast<aidl::android::hardware::graphics::common::Hdr>(type)); |
| 1775 | } |
| 1776 | aidlConversionStrategy.set<AidlHdrConversionStrategy::Tag::autoAllowedHdrTypes>( |
| 1777 | aidlAutoHdrTypes); |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 1778 | status = getHwComposer().setHdrConversionStrategy(aidlConversionStrategy, |
| 1779 | &aidlPreferredHdrOutputType); |
| 1780 | *outPreferredHdrOutputType = static_cast<int32_t>(aidlPreferredHdrOutputType); |
| 1781 | return status; |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1782 | } |
| 1783 | case GuiHdrConversionStrategyTag::forceHdrConversion: { |
| 1784 | auto forceHdrConversion = |
| 1785 | hdrConversionStrategy |
| 1786 | .get<GuiHdrConversionStrategyTag::forceHdrConversion>(); |
| 1787 | aidlConversionStrategy.set<AidlHdrConversionStrategy::Tag::forceHdrConversion>( |
| 1788 | static_cast<aidl::android::hardware::graphics::common::Hdr>( |
| 1789 | forceHdrConversion)); |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 1790 | status = getHwComposer().setHdrConversionStrategy(aidlConversionStrategy, |
| 1791 | &aidlPreferredHdrOutputType); |
| 1792 | *outPreferredHdrOutputType = static_cast<int32_t>(aidlPreferredHdrOutputType); |
| 1793 | return status; |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | }); |
| 1797 | return future.get(); |
| 1798 | } |
| 1799 | |
| 1800 | status_t SurfaceFlinger::getHdrOutputConversionSupport(bool* outSupport) const { |
| 1801 | auto future = mScheduler->schedule([this] { |
| 1802 | return getHwComposer().hasCapability(Capability::HDR_OUTPUT_CONVERSION_CONFIG); |
| 1803 | }); |
| 1804 | |
| 1805 | *outSupport = future.get(); |
| 1806 | return NO_ERROR; |
| 1807 | } |
| 1808 | |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 1809 | void SurfaceFlinger::setAutoLowLatencyMode(const sp<IBinder>& displayToken, bool on) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1810 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1811 | static_cast<void>(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1812 | if (const auto displayId = getPhysicalDisplayIdLocked(displayToken)) { |
| 1813 | getHwComposer().setAutoLowLatencyMode(*displayId, on); |
| 1814 | } else { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1815 | ALOGE("%s: Invalid display token %p", whence, displayToken.get()); |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1816 | } |
| 1817 | })); |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 1818 | } |
| 1819 | |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 1820 | void SurfaceFlinger::setGameContentType(const sp<IBinder>& displayToken, bool on) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1821 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1822 | static_cast<void>(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1823 | if (const auto displayId = getPhysicalDisplayIdLocked(displayToken)) { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 1824 | const auto type = on ? hal::ContentType::GAME : hal::ContentType::NONE; |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1825 | getHwComposer().setContentType(*displayId, type); |
| 1826 | } else { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1827 | ALOGE("%s: Invalid display token %p", whence, displayToken.get()); |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1828 | } |
| 1829 | })); |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 1830 | } |
| 1831 | |
Kriti Dang | 49ad413 | 2021-01-08 11:49:56 +0100 | [diff] [blame] | 1832 | status_t SurfaceFlinger::overrideHdrTypes(const sp<IBinder>& displayToken, |
| 1833 | const std::vector<ui::Hdr>& hdrTypes) { |
| 1834 | Mutex::Autolock lock(mStateLock); |
| 1835 | |
| 1836 | auto display = getDisplayDeviceLocked(displayToken); |
| 1837 | if (!display) { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1838 | ALOGE("%s: Invalid display token %p", __func__, displayToken.get()); |
Kriti Dang | 49ad413 | 2021-01-08 11:49:56 +0100 | [diff] [blame] | 1839 | return NAME_NOT_FOUND; |
| 1840 | } |
| 1841 | |
| 1842 | display->overrideHdrTypes(hdrTypes); |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 1843 | mScheduler->dispatchHotplug(display->getPhysicalId(), scheduler::Scheduler::Hotplug::Connected); |
Kriti Dang | 49ad413 | 2021-01-08 11:49:56 +0100 | [diff] [blame] | 1844 | return NO_ERROR; |
| 1845 | } |
| 1846 | |
Huihong Luo | 30aa437 | 2022-10-03 14:54:12 -0700 | [diff] [blame] | 1847 | status_t SurfaceFlinger::onPullAtom(const int32_t atomId, std::vector<uint8_t>* pulledData, |
| 1848 | bool* success) { |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 1849 | *success = mTimeStats->onPullAtom(atomId, pulledData); |
| 1850 | return NO_ERROR; |
| 1851 | } |
| 1852 | |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 1853 | status_t SurfaceFlinger::getDisplayedContentSamplingAttributes(const sp<IBinder>& displayToken, |
| 1854 | ui::PixelFormat* outFormat, |
| 1855 | ui::Dataspace* outDataspace, |
| 1856 | uint8_t* outComponentMask) const { |
| 1857 | if (!outFormat || !outDataspace || !outComponentMask) { |
| 1858 | return BAD_VALUE; |
| 1859 | } |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1860 | |
| 1861 | Mutex::Autolock lock(mStateLock); |
| 1862 | |
| 1863 | const auto displayId = getPhysicalDisplayIdLocked(displayToken); |
| 1864 | if (!displayId) { |
| 1865 | return NAME_NOT_FOUND; |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 1866 | } |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1867 | |
| 1868 | return getHwComposer().getDisplayedContentSamplingAttributes(*displayId, outFormat, |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 1869 | outDataspace, outComponentMask); |
| 1870 | } |
| 1871 | |
Kevin DuBois | 74e5377 | 2018-11-19 10:52:38 -0800 | [diff] [blame] | 1872 | status_t SurfaceFlinger::setDisplayContentSamplingEnabled(const sp<IBinder>& displayToken, |
| 1873 | bool enable, uint8_t componentMask, |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1874 | uint64_t maxFrames) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1875 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1876 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) -> status_t { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1877 | if (const auto displayId = getPhysicalDisplayIdLocked(displayToken)) { |
| 1878 | return getHwComposer().setDisplayContentSamplingEnabled(*displayId, enable, |
| 1879 | componentMask, maxFrames); |
| 1880 | } else { |
| 1881 | ALOGE("%s: Invalid display token %p", whence, displayToken.get()); |
| 1882 | return NAME_NOT_FOUND; |
| 1883 | } |
| 1884 | }); |
| 1885 | |
| 1886 | return future.get(); |
Kevin DuBois | 74e5377 | 2018-11-19 10:52:38 -0800 | [diff] [blame] | 1887 | } |
| 1888 | |
Kevin DuBois | 1d4249a | 2018-08-29 10:45:14 -0700 | [diff] [blame] | 1889 | status_t SurfaceFlinger::getDisplayedContentSample(const sp<IBinder>& displayToken, |
| 1890 | uint64_t maxFrames, uint64_t timestamp, |
| 1891 | DisplayedFrameStats* outStats) const { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1892 | Mutex::Autolock lock(mStateLock); |
| 1893 | |
| 1894 | const auto displayId = getPhysicalDisplayIdLocked(displayToken); |
| 1895 | if (!displayId) { |
| 1896 | return NAME_NOT_FOUND; |
Kevin DuBois | 1d4249a | 2018-08-29 10:45:14 -0700 | [diff] [blame] | 1897 | } |
| 1898 | |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1899 | return getHwComposer().getDisplayedContentSample(*displayId, maxFrames, timestamp, outStats); |
Kevin DuBois | 1d4249a | 2018-08-29 10:45:14 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Peiyong Lin | 3c2791e | 2019-01-14 17:05:18 -0800 | [diff] [blame] | 1902 | status_t SurfaceFlinger::getProtectedContentSupport(bool* outSupported) const { |
| 1903 | if (!outSupported) { |
| 1904 | return BAD_VALUE; |
| 1905 | } |
| 1906 | *outSupported = getRenderEngine().supportsProtectedContent(); |
| 1907 | return NO_ERROR; |
| 1908 | } |
| 1909 | |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 1910 | status_t SurfaceFlinger::isWideColorDisplay(const sp<IBinder>& displayToken, |
| 1911 | bool* outIsWideColorDisplay) const { |
| 1912 | if (!displayToken || !outIsWideColorDisplay) { |
| 1913 | return BAD_VALUE; |
| 1914 | } |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1915 | |
| 1916 | Mutex::Autolock lock(mStateLock); |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 1917 | const auto display = getDisplayDeviceLocked(displayToken); |
| 1918 | if (!display) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1919 | return NAME_NOT_FOUND; |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 1920 | } |
Peiyong Lin | ff84a15 | 2019-05-17 18:36:19 -0700 | [diff] [blame] | 1921 | |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1922 | *outIsWideColorDisplay = |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1923 | display->isPrimary() ? mSupportsWideColor : display->hasWideColorGamut(); |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 1924 | return NO_ERROR; |
| 1925 | } |
| 1926 | |
Peiyong Lin | c678097 | 2018-10-28 15:24:08 -0700 | [diff] [blame] | 1927 | status_t SurfaceFlinger::getCompositionPreference( |
| 1928 | Dataspace* outDataspace, ui::PixelFormat* outPixelFormat, |
| 1929 | Dataspace* outWideColorGamutDataspace, |
| 1930 | ui::PixelFormat* outWideColorGamutPixelFormat) const { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1931 | *outDataspace = mDefaultCompositionDataspace; |
| 1932 | *outPixelFormat = defaultCompositionPixelFormat; |
| 1933 | *outWideColorGamutDataspace = mWideColorGamutCompositionDataspace; |
| 1934 | *outWideColorGamutPixelFormat = wideColorGamutCompositionPixelFormat; |
Peiyong Lin | 0256f72 | 2018-08-31 15:45:10 -0700 | [diff] [blame] | 1935 | return NO_ERROR; |
| 1936 | } |
| 1937 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1938 | status_t SurfaceFlinger::addRegionSamplingListener(const Rect& samplingArea, |
| 1939 | const sp<IBinder>& stopLayerHandle, |
| 1940 | const sp<IRegionSamplingListener>& listener) { |
Leon Scroggins III | ae16b80 | 2022-01-12 11:42:05 -0500 | [diff] [blame] | 1941 | if (!listener || samplingArea == Rect::INVALID_RECT || samplingArea.isEmpty()) { |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1942 | return BAD_VALUE; |
| 1943 | } |
Alec Mouri | 9a02eda | 2020-04-21 17:39:34 -0700 | [diff] [blame] | 1944 | |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 1945 | // LayerHandle::getLayer promotes the layer object in a binder thread but we will not destroy |
| 1946 | // the layer here since the caller has a strong ref to the layer's handle. |
Vishnu Nair | 9d77a5c | 2022-10-28 06:31:21 +0000 | [diff] [blame] | 1947 | const sp<Layer> stopLayer = LayerHandle::getLayer(stopLayerHandle); |
| 1948 | mRegionSamplingThread->addListener(samplingArea, |
| 1949 | stopLayer ? stopLayer->getSequence() : UNASSIGNED_LAYER_ID, |
| 1950 | listener); |
Dan Stoza | 84ab937 | 2018-12-17 15:27:57 -0800 | [diff] [blame] | 1951 | return NO_ERROR; |
| 1952 | } |
| 1953 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1954 | status_t SurfaceFlinger::removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener) { |
tangrobin | af45f01 | 2019-02-26 18:10:10 +0800 | [diff] [blame] | 1955 | if (!listener) { |
| 1956 | return BAD_VALUE; |
| 1957 | } |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1958 | mRegionSamplingThread->removeListener(listener); |
Dan Stoza | 84ab937 | 2018-12-17 15:27:57 -0800 | [diff] [blame] | 1959 | return NO_ERROR; |
| 1960 | } |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 1961 | |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 1962 | status_t SurfaceFlinger::addFpsListener(int32_t taskId, const sp<gui::IFpsListener>& listener) { |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 1963 | if (!listener) { |
| 1964 | return BAD_VALUE; |
| 1965 | } |
| 1966 | |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 1967 | mFpsReporter->addListener(listener, taskId); |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 1968 | return NO_ERROR; |
| 1969 | } |
| 1970 | |
| 1971 | status_t SurfaceFlinger::removeFpsListener(const sp<gui::IFpsListener>& listener) { |
| 1972 | if (!listener) { |
| 1973 | return BAD_VALUE; |
| 1974 | } |
| 1975 | mFpsReporter->removeListener(listener); |
| 1976 | return NO_ERROR; |
| 1977 | } |
| 1978 | |
Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 1979 | status_t SurfaceFlinger::addTunnelModeEnabledListener( |
| 1980 | const sp<gui::ITunnelModeEnabledListener>& listener) { |
| 1981 | if (!listener) { |
| 1982 | return BAD_VALUE; |
| 1983 | } |
| 1984 | |
| 1985 | mTunnelModeEnabledReporter->addListener(listener); |
| 1986 | return NO_ERROR; |
| 1987 | } |
| 1988 | |
| 1989 | status_t SurfaceFlinger::removeTunnelModeEnabledListener( |
| 1990 | const sp<gui::ITunnelModeEnabledListener>& listener) { |
| 1991 | if (!listener) { |
| 1992 | return BAD_VALUE; |
| 1993 | } |
| 1994 | |
| 1995 | mTunnelModeEnabledReporter->removeListener(listener); |
| 1996 | return NO_ERROR; |
| 1997 | } |
| 1998 | |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 1999 | status_t SurfaceFlinger::getDisplayBrightnessSupport(const sp<IBinder>& displayToken, |
| 2000 | bool* outSupport) const { |
| 2001 | if (!displayToken || !outSupport) { |
| 2002 | return BAD_VALUE; |
| 2003 | } |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 2004 | |
| 2005 | Mutex::Autolock lock(mStateLock); |
| 2006 | |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 2007 | const auto displayId = getPhysicalDisplayIdLocked(displayToken); |
| 2008 | if (!displayId) { |
| 2009 | return NAME_NOT_FOUND; |
| 2010 | } |
Leon Scroggins III | 5967aec | 2021-12-29 11:14:22 -0500 | [diff] [blame] | 2011 | *outSupport = getHwComposer().hasDisplayCapability(*displayId, DisplayCapability::BRIGHTNESS); |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 2012 | return NO_ERROR; |
| 2013 | } |
| 2014 | |
John Reck | 22be696 | 2021-03-10 12:59:54 -0500 | [diff] [blame] | 2015 | status_t SurfaceFlinger::setDisplayBrightness(const sp<IBinder>& displayToken, |
| 2016 | const gui::DisplayBrightness& brightness) { |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 2017 | if (!displayToken) { |
| 2018 | return BAD_VALUE; |
| 2019 | } |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 2020 | |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 2021 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 2022 | return ftl::Future(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) { |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 2023 | // TODO(b/241285876): Validate that the display is physical instead of failing later. |
John Reck | ac09e45 | 2021-04-07 16:35:37 -0400 | [diff] [blame] | 2024 | if (const auto display = getDisplayDeviceLocked(displayToken)) { |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 2025 | const bool supportsDisplayBrightnessCommand = |
| 2026 | getHwComposer().getComposer()->isSupported( |
| 2027 | Hwc2::Composer::OptionalFeature::DisplayBrightnessCommand); |
| 2028 | // If we support applying display brightness as a command, then we also support |
| 2029 | // dimming SDR layers. |
Alec Mouri | 90a1927 | 2021-12-30 14:11:38 -0800 | [diff] [blame] | 2030 | if (supportsDisplayBrightnessCommand) { |
Alec Mouri | 6da0e27 | 2022-02-07 12:45:57 -0800 | [diff] [blame] | 2031 | auto compositionDisplay = display->getCompositionDisplay(); |
| 2032 | float currentDimmingRatio = |
| 2033 | compositionDisplay->editState().sdrWhitePointNits / |
| 2034 | compositionDisplay->editState().displayBrightnessNits; |
Sally Qi | 0ea47b5 | 2023-06-22 13:27:20 -0700 | [diff] [blame] | 2035 | static constexpr float kDimmingThreshold = 0.02f; |
| 2036 | if (brightness.sdrWhitePointNits == 0.f || |
| 2037 | abs(brightness.sdrWhitePointNits - brightness.displayBrightnessNits) / |
| 2038 | brightness.sdrWhitePointNits >= |
| 2039 | kDimmingThreshold) { |
| 2040 | // to optimize, skip brightness setter if the brightness difference ratio |
| 2041 | // is lower than threshold |
| 2042 | compositionDisplay |
| 2043 | ->setDisplayBrightness(brightness.sdrWhitePointNits, |
| 2044 | brightness.displayBrightnessNits); |
| 2045 | } else { |
| 2046 | compositionDisplay->setDisplayBrightness(brightness.sdrWhitePointNits, |
| 2047 | brightness.sdrWhitePointNits); |
| 2048 | } |
| 2049 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 2050 | FTL_FAKE_GUARD(kMainThreadContext, |
| 2051 | display->stageBrightness(brightness.displayBrightness)); |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 2052 | float currentHdrSdrRatio = |
| 2053 | compositionDisplay->editState().displayBrightnessNits / |
| 2054 | compositionDisplay->editState().sdrWhitePointNits; |
| 2055 | FTL_FAKE_GUARD(kMainThreadContext, |
| 2056 | display->updateHdrSdrRatioOverlayRatio(currentHdrSdrRatio)); |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 2057 | |
Alec Mouri | 6da0e27 | 2022-02-07 12:45:57 -0800 | [diff] [blame] | 2058 | if (brightness.sdrWhitePointNits / brightness.displayBrightnessNits != |
| 2059 | currentDimmingRatio) { |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 2060 | scheduleComposite(FrameHint::kNone); |
| 2061 | } else { |
| 2062 | scheduleCommit(FrameHint::kNone); |
| 2063 | } |
| 2064 | return ftl::yield<status_t>(OK); |
| 2065 | } else { |
Alec Mouri | 90a1927 | 2021-12-30 14:11:38 -0800 | [diff] [blame] | 2066 | return getHwComposer() |
| 2067 | .setDisplayBrightness(display->getPhysicalId(), |
| 2068 | brightness.displayBrightness, |
Alec Mouri | 4d8a05d | 2022-03-23 18:14:26 +0000 | [diff] [blame] | 2069 | brightness.displayBrightnessNits, |
Alec Mouri | 90a1927 | 2021-12-30 14:11:38 -0800 | [diff] [blame] | 2070 | Hwc2::Composer::DisplayBrightnessOptions{ |
| 2071 | .applyImmediately = true}); |
John Reck | ac09e45 | 2021-04-07 16:35:37 -0400 | [diff] [blame] | 2072 | } |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 2073 | } else { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 2074 | ALOGE("%s: Invalid display token %p", whence, displayToken.get()); |
Dominik Laskowski | 4e2b71f | 2020-11-10 15:05:32 -0800 | [diff] [blame] | 2075 | return ftl::yield<status_t>(NAME_NOT_FOUND); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 2076 | } |
Dominik Laskowski | 5690bde | 2020-04-23 19:04:22 -0700 | [diff] [blame] | 2077 | })) |
Dominik Laskowski | b17c621 | 2022-05-09 09:36:19 -0700 | [diff] [blame] | 2078 | .then([](ftl::Future<status_t> task) { return task; }) |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 2079 | .get(); |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 2082 | status_t SurfaceFlinger::addHdrLayerInfoListener(const sp<IBinder>& displayToken, |
| 2083 | const sp<gui::IHdrLayerInfoListener>& listener) { |
| 2084 | if (!displayToken) { |
| 2085 | return BAD_VALUE; |
| 2086 | } |
| 2087 | |
| 2088 | Mutex::Autolock lock(mStateLock); |
| 2089 | |
| 2090 | const auto display = getDisplayDeviceLocked(displayToken); |
| 2091 | if (!display) { |
| 2092 | return NAME_NOT_FOUND; |
| 2093 | } |
| 2094 | const auto displayId = display->getId(); |
| 2095 | sp<HdrLayerInfoReporter>& hdrInfoReporter = mHdrLayerInfoListeners[displayId]; |
| 2096 | if (!hdrInfoReporter) { |
| 2097 | hdrInfoReporter = sp<HdrLayerInfoReporter>::make(); |
| 2098 | } |
| 2099 | hdrInfoReporter->addListener(listener); |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 2100 | |
| 2101 | |
| 2102 | mAddingHDRLayerInfoListener = true; |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 2103 | return OK; |
| 2104 | } |
| 2105 | |
| 2106 | status_t SurfaceFlinger::removeHdrLayerInfoListener( |
| 2107 | const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) { |
| 2108 | if (!displayToken) { |
| 2109 | return BAD_VALUE; |
| 2110 | } |
| 2111 | |
| 2112 | Mutex::Autolock lock(mStateLock); |
| 2113 | |
| 2114 | const auto display = getDisplayDeviceLocked(displayToken); |
| 2115 | if (!display) { |
| 2116 | return NAME_NOT_FOUND; |
| 2117 | } |
| 2118 | const auto displayId = display->getId(); |
| 2119 | sp<HdrLayerInfoReporter>& hdrInfoReporter = mHdrLayerInfoListeners[displayId]; |
| 2120 | if (hdrInfoReporter) { |
| 2121 | hdrInfoReporter->removeListener(listener); |
| 2122 | } |
| 2123 | return OK; |
| 2124 | } |
| 2125 | |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 2126 | status_t SurfaceFlinger::notifyPowerBoost(int32_t boostId) { |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 2127 | using aidl::android::hardware::power::Boost; |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 2128 | Boost powerBoost = static_cast<Boost>(boostId); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 2129 | |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 2130 | if (powerBoost == Boost::INTERACTION) { |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2131 | mScheduler->onTouchHint(); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | return NO_ERROR; |
| 2135 | } |
| 2136 | |
Leon Scroggins III | e7c51c6 | 2022-02-01 15:53:54 -0500 | [diff] [blame] | 2137 | status_t SurfaceFlinger::getDisplayDecorationSupport( |
| 2138 | const sp<IBinder>& displayToken, |
| 2139 | std::optional<DisplayDecorationSupport>* outSupport) const { |
Leon Scroggins III | e5cff63 | 2021-12-29 11:53:36 -0500 | [diff] [blame] | 2140 | if (!displayToken || !outSupport) { |
| 2141 | return BAD_VALUE; |
| 2142 | } |
| 2143 | |
| 2144 | Mutex::Autolock lock(mStateLock); |
| 2145 | |
| 2146 | const auto displayId = getPhysicalDisplayIdLocked(displayToken); |
| 2147 | if (!displayId) { |
| 2148 | return NAME_NOT_FOUND; |
| 2149 | } |
Leon Scroggins III | e7c51c6 | 2022-02-01 15:53:54 -0500 | [diff] [blame] | 2150 | getHwComposer().getDisplayDecorationSupport(*displayId, outSupport); |
Leon Scroggins III | e5cff63 | 2021-12-29 11:53:36 -0500 | [diff] [blame] | 2151 | return NO_ERROR; |
| 2152 | } |
| 2153 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 2154 | // ---------------------------------------------------------------------------- |
| 2155 | |
Jorim Jaggi | b1e2f8d | 2017-06-08 15:43:59 -0700 | [diff] [blame] | 2156 | sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection( |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 2157 | gui::ISurfaceComposer::VsyncSource vsyncSource, EventRegistrationFlags eventRegistration, |
| 2158 | const sp<IBinder>& layerHandle) { |
Ady Abraham | b334758 | 2024-04-03 01:01:51 +0000 | [diff] [blame] | 2159 | const auto cycle = [&] { |
| 2160 | if (FlagManager::getInstance().deprecate_vsync_sf()) { |
| 2161 | ALOGW_IF(vsyncSource == gui::ISurfaceComposer::VsyncSource::eVsyncSourceSurfaceFlinger, |
| 2162 | "requested unsupported config eVsyncSourceSurfaceFlinger"); |
| 2163 | return scheduler::Cycle::Render; |
| 2164 | } |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 2165 | |
Ady Abraham | b334758 | 2024-04-03 01:01:51 +0000 | [diff] [blame] | 2166 | return vsyncSource == gui::ISurfaceComposer::VsyncSource::eVsyncSourceSurfaceFlinger |
| 2167 | ? scheduler::Cycle::LastComposite |
| 2168 | : scheduler::Cycle::Render; |
| 2169 | }(); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 2170 | return mScheduler->createDisplayEventConnection(cycle, eventRegistration, layerHandle); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2173 | void SurfaceFlinger::scheduleCommit(FrameHint hint) { |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2174 | if (hint == FrameHint::kActive) { |
| 2175 | mScheduler->resetIdleTimer(); |
| 2176 | } |
Matt Buckley | 15ecd1c | 2022-11-01 21:57:16 +0000 | [diff] [blame] | 2177 | mPowerAdvisor->notifyDisplayUpdateImminentAndCpuReset(); |
Dominik Laskowski | 46f3e3b | 2021-08-10 11:44:24 -0700 | [diff] [blame] | 2178 | mScheduler->scheduleFrame(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 2179 | } |
| 2180 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2181 | void SurfaceFlinger::scheduleComposite(FrameHint hint) { |
| 2182 | mMustComposite = true; |
| 2183 | scheduleCommit(hint); |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | void SurfaceFlinger::scheduleRepaint() { |
| 2187 | mGeometryDirty = true; |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2188 | scheduleComposite(FrameHint::kActive); |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2191 | void SurfaceFlinger::scheduleSample() { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 2192 | static_cast<void>(mScheduler->schedule([this] { sample(); })); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 2193 | } |
| 2194 | |
Ady Abraham | 2492a02 | 2020-07-24 11:09:55 -0700 | [diff] [blame] | 2195 | nsecs_t SurfaceFlinger::getVsyncPeriodFromHWC() const { |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 2196 | if (const auto display = getDefaultDisplayDeviceLocked()) { |
| 2197 | return display->getVsyncPeriodFromHWC(); |
Dominik Laskowski | 83b8821 | 2018-12-11 13:34:06 -0800 | [diff] [blame] | 2198 | } |
| 2199 | |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 2200 | return 0; |
Dominik Laskowski | 83b8821 | 2018-12-11 13:34:06 -0800 | [diff] [blame] | 2201 | } |
| 2202 | |
Dominik Laskowski | 0deb06e | 2021-04-16 23:18:31 -0700 | [diff] [blame] | 2203 | void SurfaceFlinger::onComposerHalVsync(hal::HWDisplayId hwcDisplayId, int64_t timestamp, |
| 2204 | std::optional<hal::VsyncPeriodNanos> vsyncPeriod) { |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 2205 | if (FlagManager::getInstance().connected_display() && timestamp < 0 && |
| 2206 | vsyncPeriod.has_value()) { |
Huihong Luo | aac3a77 | 2023-09-19 20:51:59 -0700 | [diff] [blame] | 2207 | // use ~0 instead of -1 as AidlComposerHal.cpp passes the param as unsigned int32 |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 2208 | if (mIsHotplugErrViaNegVsync && vsyncPeriod.value() == ~0) { |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 2209 | const auto errorCode = static_cast<int32_t>(-timestamp); |
| 2210 | ALOGD("%s: Hotplug error %d for display %" PRIu64, __func__, errorCode, hwcDisplayId); |
| 2211 | mScheduler->dispatchHotplugError(errorCode); |
Huihong Luo | aac3a77 | 2023-09-19 20:51:59 -0700 | [diff] [blame] | 2212 | return; |
| 2213 | } |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 2214 | |
| 2215 | if (mIsHdcpViaNegVsync && vsyncPeriod.value() == ~1) { |
| 2216 | const int32_t value = static_cast<int32_t>(-timestamp); |
| 2217 | // one byte is good enough to encode android.hardware.drm.HdcpLevel |
| 2218 | const int32_t maxLevel = (value >> 8) & 0xFF; |
| 2219 | const int32_t connectedLevel = value & 0xFF; |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 2220 | ALOGD("%s: HDCP levels changed (connected=%d, max=%d) for display %" PRIu64, __func__, |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 2221 | connectedLevel, maxLevel, hwcDisplayId); |
| 2222 | updateHdcpLevels(hwcDisplayId, connectedLevel, maxLevel); |
| 2223 | return; |
| 2224 | } |
Huihong Luo | aac3a77 | 2023-09-19 20:51:59 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2227 | SFTRACE_NAME(vsyncPeriod |
| 2228 | ? ftl::Concat(__func__, ' ', hwcDisplayId, ' ', *vsyncPeriod, "ns").c_str() |
| 2229 | : ftl::Concat(__func__, ' ', hwcDisplayId).c_str()); |
Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 2230 | |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 2231 | Mutex::Autolock lock(mStateLock); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 2232 | if (const auto displayIdOpt = getHwComposer().onVsync(hwcDisplayId, timestamp)) { |
| 2233 | if (mScheduler->addResyncSample(*displayIdOpt, timestamp, vsyncPeriod)) { |
| 2234 | // period flushed |
| 2235 | mScheduler->modulateVsync(displayIdOpt, &VsyncModulator::onRefreshRateChangeCompleted); |
| 2236 | } |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 2237 | } |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
Huihong Luo | 791bef9 | 2023-09-19 21:18:45 -0700 | [diff] [blame] | 2240 | void SurfaceFlinger::onComposerHalHotplugEvent(hal::HWDisplayId hwcDisplayId, |
| 2241 | DisplayHotplugEvent event) { |
| 2242 | if (event == DisplayHotplugEvent::CONNECTED || event == DisplayHotplugEvent::DISCONNECTED) { |
| 2243 | hal::Connection connection = (event == DisplayHotplugEvent::CONNECTED) |
| 2244 | ? hal::Connection::CONNECTED |
| 2245 | : hal::Connection::DISCONNECTED; |
| 2246 | { |
| 2247 | std::lock_guard<std::mutex> lock(mHotplugMutex); |
| 2248 | mPendingHotplugEvents.push_back(HotplugEvent{hwcDisplayId, connection}); |
| 2249 | } |
| 2250 | |
| 2251 | if (mScheduler) { |
| 2252 | mScheduler->scheduleConfigure(); |
| 2253 | } |
| 2254 | |
| 2255 | return; |
Jiwen 'Steve' Cai | ccfd682 | 2018-02-21 16:15:58 -0800 | [diff] [blame] | 2256 | } |
| 2257 | |
Huihong Luo | 791bef9 | 2023-09-19 21:18:45 -0700 | [diff] [blame] | 2258 | if (FlagManager::getInstance().hotplug2()) { |
Huihong Luo | 791bef9 | 2023-09-19 21:18:45 -0700 | [diff] [blame] | 2259 | // TODO(b/311403559): use enum type instead of int |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 2260 | const auto errorCode = static_cast<int32_t>(event); |
| 2261 | ALOGD("%s: Hotplug error %d for display %" PRIu64, __func__, errorCode, hwcDisplayId); |
| 2262 | mScheduler->dispatchHotplugError(errorCode); |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 2263 | } |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 2264 | } |
| 2265 | |
Dominik Laskowski | 0deb06e | 2021-04-16 23:18:31 -0700 | [diff] [blame] | 2266 | void SurfaceFlinger::onComposerHalVsyncPeriodTimingChanged( |
| 2267 | hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline& timeline) { |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 2268 | Mutex::Autolock lock(mStateLock); |
Dominik Laskowski | 0deb06e | 2021-04-16 23:18:31 -0700 | [diff] [blame] | 2269 | mScheduler->onNewVsyncPeriodChangeTimeline(timeline); |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame] | 2270 | |
| 2271 | if (timeline.refreshRequired) { |
| 2272 | scheduleComposite(FrameHint::kNone); |
| 2273 | } |
Ady Abraham | 7159f57 | 2019-10-11 11:10:18 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
Dominik Laskowski | 0deb06e | 2021-04-16 23:18:31 -0700 | [diff] [blame] | 2276 | void SurfaceFlinger::onComposerHalSeamlessPossible(hal::HWDisplayId) { |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 2277 | // TODO(b/142753666): use constraints when calling to setActiveModeWithConstraints and |
Ady Abraham | b0433bc | 2020-01-08 17:31:06 -0800 | [diff] [blame] | 2278 | // use this callback to know when to retry in case of SEAMLESS_NOT_POSSIBLE. |
| 2279 | } |
| 2280 | |
Dominik Laskowski | 0deb06e | 2021-04-16 23:18:31 -0700 | [diff] [blame] | 2281 | void SurfaceFlinger::onComposerHalRefresh(hal::HWDisplayId) { |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 2282 | Mutex::Autolock lock(mStateLock); |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2283 | scheduleComposite(FrameHint::kNone); |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 2284 | } |
| 2285 | |
Yichi Chen | 3401b56 | 2022-01-17 15:42:35 +0800 | [diff] [blame] | 2286 | void SurfaceFlinger::onComposerHalVsyncIdle(hal::HWDisplayId) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2287 | SFTRACE_CALL(); |
Ady Abraham | e9befd7 | 2022-02-24 17:24:44 -0800 | [diff] [blame] | 2288 | mScheduler->forceNextResync(); |
Yichi Chen | 3401b56 | 2022-01-17 15:42:35 +0800 | [diff] [blame] | 2289 | } |
| 2290 | |
ramindani | b2158ee | 2023-02-13 20:29:59 -0800 | [diff] [blame] | 2291 | void SurfaceFlinger::onRefreshRateChangedDebug(const RefreshRateChangedDebugData& data) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2292 | SFTRACE_CALL(); |
Dominik Laskowski | 7372a60 | 2024-04-29 16:40:42 -0400 | [diff] [blame] | 2293 | const char* const whence = __func__; |
| 2294 | static_cast<void>(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD( |
| 2295 | kMainThreadContext) { |
| 2296 | if (const auto displayIdOpt = getHwComposer().toPhysicalDisplayId(data.display)) { |
| 2297 | if (const auto display = getDisplayDeviceLocked(*displayIdOpt)) { |
Ying Wei | 22d59cc | 2024-05-11 02:41:08 +0000 | [diff] [blame] | 2298 | const Fps refreshRate = Fps::fromPeriodNsecs( |
| 2299 | getHwComposer().getComposer()->isVrrSupported() ? data.refreshPeriodNanos |
| 2300 | : data.vsyncPeriodNanos); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2301 | SFTRACE_FORMAT("%s refresh rate = %d", whence, refreshRate.getIntValue()); |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 2302 | |
| 2303 | const auto renderRate = mDisplayModeController.getActiveMode(*displayIdOpt).fps; |
| 2304 | constexpr bool kSetByHwc = true; |
| 2305 | display->updateRefreshRateOverlayRate(refreshRate, renderRate, kSetByHwc); |
Dominik Laskowski | 7372a60 | 2024-04-29 16:40:42 -0400 | [diff] [blame] | 2306 | } |
| 2307 | } |
| 2308 | })); |
ramindani | 12bfe6b | 2023-02-03 13:29:19 -0800 | [diff] [blame] | 2309 | } |
| 2310 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2311 | void SurfaceFlinger::configure() { |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 2312 | Mutex::Autolock lock(mStateLock); |
| 2313 | if (configureLocked()) { |
| 2314 | setTransactionFlags(eDisplayTransactionNeeded); |
| 2315 | } |
| 2316 | } |
| 2317 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2318 | bool SurfaceFlinger::updateLayerSnapshotsLegacy(VsyncId vsyncId, nsecs_t frameTimeNs, |
| 2319 | bool flushTransactions, |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2320 | bool& outTransactionsAreEmpty) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2321 | SFTRACE_CALL(); |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2322 | frontend::Update update; |
| 2323 | if (flushTransactions) { |
| 2324 | update = flushLifecycleUpdates(); |
| 2325 | if (mTransactionTracing) { |
| 2326 | mTransactionTracing->addCommittedTransactions(ftl::to_underlying(vsyncId), frameTimeNs, |
| 2327 | update, mFrontEndDisplayInfos, |
| 2328 | mFrontEndDisplayInfosChanged); |
| 2329 | } |
| 2330 | } |
| 2331 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2332 | bool needsTraversal = false; |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2333 | if (flushTransactions) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2334 | needsTraversal |= commitMirrorDisplays(vsyncId); |
| 2335 | needsTraversal |= commitCreatedLayers(vsyncId, update.layerCreatedStates); |
| 2336 | needsTraversal |= applyTransactions(update.transactions, vsyncId); |
| 2337 | } |
| 2338 | outTransactionsAreEmpty = !needsTraversal; |
| 2339 | const bool shouldCommit = (getTransactionFlags() & ~eTransactionFlushNeeded) || needsTraversal; |
| 2340 | if (shouldCommit) { |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 2341 | commitTransactionsLegacy(); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | bool mustComposite = latchBuffers() || shouldCommit; |
| 2345 | updateLayerGeometry(); |
| 2346 | return mustComposite; |
| 2347 | } |
| 2348 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2349 | void SurfaceFlinger::updateLayerHistory(nsecs_t now) { |
| 2350 | for (const auto& snapshot : mLayerSnapshotBuilder.getSnapshots()) { |
| 2351 | using Changes = frontend::RequestedLayerState::Changes; |
| 2352 | if (snapshot->path.isClone()) { |
| 2353 | continue; |
| 2354 | } |
Vishnu Nair | 80e8cfe | 2023-09-29 17:03:45 -0700 | [diff] [blame] | 2355 | |
Jerry Chang | 3667800 | 2023-11-29 16:56:17 +0000 | [diff] [blame] | 2356 | const bool updateSmallDirty = FlagManager::getInstance().enable_small_area_detection() && |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 2357 | ((snapshot->clientChanges & layer_state_t::eSurfaceDamageRegionChanged) || |
| 2358 | snapshot->changes.any(Changes::Geometry)); |
| 2359 | |
| 2360 | const bool hasChanges = |
Vishnu Nair | 41376b6 | 2023-11-08 05:08:58 -0800 | [diff] [blame] | 2361 | snapshot->changes.any(Changes::FrameRate | Changes::Buffer | Changes::Animation | |
| 2362 | Changes::Geometry | Changes::Visibility) || |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 2363 | (snapshot->clientChanges & layer_state_t::eDefaultFrameRateCompatibilityChanged) != |
| 2364 | 0; |
| 2365 | |
| 2366 | if (!updateSmallDirty && !hasChanges) { |
| 2367 | continue; |
| 2368 | } |
| 2369 | |
| 2370 | auto it = mLegacyLayers.find(snapshot->sequence); |
Vishnu Nair | 5943bf6 | 2023-12-08 22:04:20 +0000 | [diff] [blame] | 2371 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 2372 | "Couldn't find layer object for %s", |
| 2373 | snapshot->getDebugString().c_str()); |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 2374 | |
| 2375 | if (updateSmallDirty) { |
| 2376 | // Update small dirty flag while surface damage region or geometry changed |
| 2377 | it->second->setIsSmallDirty(snapshot.get()); |
| 2378 | } |
| 2379 | |
| 2380 | if (!hasChanges) { |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2381 | continue; |
| 2382 | } |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 2383 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2384 | const auto layerProps = scheduler::LayerProps{ |
| 2385 | .visible = snapshot->isVisible, |
| 2386 | .bounds = snapshot->geomLayerBounds, |
| 2387 | .transform = snapshot->geomLayerTransform, |
| 2388 | .setFrameRateVote = snapshot->frameRate, |
| 2389 | .frameRateSelectionPriority = snapshot->frameRateSelectionPriority, |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 2390 | .isSmallDirty = snapshot->isSmallDirty, |
Alec Mouri | 89f5d4e | 2023-10-20 17:12:49 +0000 | [diff] [blame] | 2391 | .isFrontBuffered = snapshot->isFrontBuffered(), |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2392 | }; |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 2393 | |
Vishnu Nair | 41376b6 | 2023-11-08 05:08:58 -0800 | [diff] [blame] | 2394 | if (snapshot->changes.any(Changes::Geometry | Changes::Visibility)) { |
| 2395 | mScheduler->setLayerProperties(snapshot->sequence, layerProps); |
| 2396 | } |
| 2397 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2398 | if (snapshot->clientChanges & layer_state_t::eDefaultFrameRateCompatibilityChanged) { |
| 2399 | mScheduler->setDefaultFrameRateCompatibility(snapshot->sequence, |
| 2400 | snapshot->defaultFrameRateCompatibility); |
| 2401 | } |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 2402 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2403 | if (snapshot->changes.test(Changes::Animation)) { |
| 2404 | it->second->recordLayerHistoryAnimationTx(layerProps, now); |
| 2405 | } |
Vishnu Nair | 80e8cfe | 2023-09-29 17:03:45 -0700 | [diff] [blame] | 2406 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2407 | if (snapshot->changes.test(Changes::FrameRate)) { |
| 2408 | it->second->setFrameRateForLayerTree(snapshot->frameRate, layerProps, now); |
| 2409 | } |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 2410 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2411 | if (snapshot->changes.test(Changes::Buffer)) { |
| 2412 | it->second->recordLayerHistoryBufferUpdate(layerProps, now); |
| 2413 | } |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2417 | bool SurfaceFlinger::updateLayerSnapshots(VsyncId vsyncId, nsecs_t frameTimeNs, |
| 2418 | bool flushTransactions, bool& outTransactionsAreEmpty) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2419 | using Changes = frontend::RequestedLayerState::Changes; |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2420 | SFTRACE_CALL(); |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2421 | frontend::Update update; |
| 2422 | if (flushTransactions) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2423 | SFTRACE_NAME("TransactionHandler:flushTransactions"); |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2424 | // Locking: |
| 2425 | // 1. to prevent onHandleDestroyed from being called while the state lock is held, |
| 2426 | // we must keep a copy of the transactions (specifically the composer |
| 2427 | // states) around outside the scope of the lock. |
| 2428 | // 2. Transactions and created layers do not share a lock. To prevent applying |
| 2429 | // transactions with layers still in the createdLayer queue, collect the transactions |
| 2430 | // before committing the created layers. |
| 2431 | // 3. Transactions can only be flushed after adding layers, since the layer can be a newly |
| 2432 | // created one |
| 2433 | mTransactionHandler.collectTransactions(); |
| 2434 | { |
| 2435 | // TODO(b/238781169) lockless queue this and keep order. |
| 2436 | std::scoped_lock<std::mutex> lock(mCreatedLayersLock); |
| 2437 | update.layerCreatedStates = std::move(mCreatedLayers); |
| 2438 | mCreatedLayers.clear(); |
| 2439 | update.newLayers = std::move(mNewLayers); |
| 2440 | mNewLayers.clear(); |
| 2441 | update.layerCreationArgs = std::move(mNewLayerArgs); |
| 2442 | mNewLayerArgs.clear(); |
| 2443 | update.destroyedHandles = std::move(mDestroyedHandles); |
| 2444 | mDestroyedHandles.clear(); |
| 2445 | } |
| 2446 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2447 | mLayerLifecycleManager.addLayers(std::move(update.newLayers)); |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2448 | update.transactions = mTransactionHandler.flushTransactions(); |
| 2449 | if (mTransactionTracing) { |
| 2450 | mTransactionTracing->addCommittedTransactions(ftl::to_underlying(vsyncId), frameTimeNs, |
| 2451 | update, mFrontEndDisplayInfos, |
| 2452 | mFrontEndDisplayInfosChanged); |
| 2453 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2454 | mLayerLifecycleManager.applyTransactions(update.transactions); |
| 2455 | mLayerLifecycleManager.onHandlesDestroyed(update.destroyedHandles); |
| 2456 | for (auto& legacyLayer : update.layerCreatedStates) { |
| 2457 | sp<Layer> layer = legacyLayer.layer.promote(); |
| 2458 | if (layer) { |
| 2459 | mLegacyLayers[layer->sequence] = layer; |
| 2460 | } |
| 2461 | } |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 2462 | mLayerHierarchyBuilder.update(mLayerLifecycleManager); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 2465 | // Keep a copy of the drawing state (that is going to be overwritten |
| 2466 | // by commitTransactionsLocked) outside of mStateLock so that the side |
| 2467 | // effects of the State assignment don't happen with mStateLock held, |
| 2468 | // which can cause deadlocks. |
| 2469 | State drawingState(mDrawingState); |
| 2470 | Mutex::Autolock lock(mStateLock); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 2471 | bool mustComposite = false; |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 2472 | mustComposite |= applyAndCommitDisplayTransactionStatesLocked(update.transactions); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2473 | |
| 2474 | { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2475 | SFTRACE_NAME("LayerSnapshotBuilder:update"); |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 2476 | frontend::LayerSnapshotBuilder::Args |
| 2477 | args{.root = mLayerHierarchyBuilder.getHierarchy(), |
| 2478 | .layerLifecycleManager = mLayerLifecycleManager, |
Melody Hsu | 63e3dcc | 2024-04-03 23:22:20 +0000 | [diff] [blame] | 2479 | .includeMetadata = mCompositionEngine->getFeatureFlags().test( |
| 2480 | compositionengine::Feature::kSnapshotLayerMetadata), |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 2481 | .displays = mFrontEndDisplayInfos, |
| 2482 | .displayChanges = mFrontEndDisplayInfosChanged, |
| 2483 | .globalShadowSettings = mDrawingState.globalShadowSettings, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 2484 | .supportsBlur = mSupportsBlur, |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 2485 | .forceFullDamage = mForceFullDamage, |
| 2486 | .supportedLayerGenericMetadata = |
| 2487 | getHwComposer().getSupportedLayerGenericMetadata(), |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 2488 | .genericLayerMetadataKeyMap = getGenericLayerMetadataKeyMap(), |
| 2489 | .skipRoundCornersWhenProtected = |
| 2490 | !getRenderEngine().supportsProtectedContent()}; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2491 | mLayerSnapshotBuilder.update(args); |
| 2492 | } |
| 2493 | |
| 2494 | if (mLayerLifecycleManager.getGlobalChanges().any(Changes::Geometry | Changes::Input | |
Vishnu Nair | 40d0228 | 2023-02-28 21:11:40 +0000 | [diff] [blame] | 2495 | Changes::Hierarchy | Changes::Visibility)) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2496 | mUpdateInputInfo = true; |
| 2497 | } |
| 2498 | if (mLayerLifecycleManager.getGlobalChanges().any(Changes::VisibleRegion | Changes::Hierarchy | |
Vishnu Nair | 3996ee3 | 2023-08-14 04:32:31 +0000 | [diff] [blame] | 2499 | Changes::Visibility | Changes::Geometry)) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2500 | mVisibleRegionsDirty = true; |
| 2501 | } |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 2502 | if (mLayerLifecycleManager.getGlobalChanges().any(Changes::Hierarchy | Changes::FrameRate)) { |
| 2503 | // The frame rate of attached choreographers can only change as a result of a |
| 2504 | // FrameRate change (including when Hierarchy changes). |
| 2505 | mUpdateAttachedChoreographer = true; |
| 2506 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2507 | outTransactionsAreEmpty = mLayerLifecycleManager.getGlobalChanges().get() == 0; |
Vishnu Nair | df59f47 | 2024-05-17 16:51:33 +0000 | [diff] [blame] | 2508 | if (FlagManager::getInstance().vrr_bugfix_24q4()) { |
Ady Abraham | 6846ade | 2024-05-20 21:58:27 +0000 | [diff] [blame] | 2509 | mustComposite |= mLayerLifecycleManager.getGlobalChanges().any( |
| 2510 | frontend::RequestedLayerState::kMustComposite); |
Vishnu Nair | df59f47 | 2024-05-17 16:51:33 +0000 | [diff] [blame] | 2511 | } else { |
| 2512 | mustComposite |= mLayerLifecycleManager.getGlobalChanges().get() != 0; |
| 2513 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2514 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 2515 | bool newDataLatched = false; |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2516 | SFTRACE_NAME("DisplayCallbackAndStatsUpdates"); |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2517 | mustComposite |= applyTransactionsLocked(update.transactions, vsyncId); |
| 2518 | traverseLegacyLayers([&](Layer* layer) { layer->commitTransaction(); }); |
| 2519 | const nsecs_t latchTime = systemTime(); |
| 2520 | bool unused = false; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2521 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2522 | for (auto& layer : mLayerLifecycleManager.getLayers()) { |
| 2523 | if (layer->changes.test(frontend::RequestedLayerState::Changes::Created) && |
| 2524 | layer->bgColorLayer) { |
| 2525 | sp<Layer> bgColorLayer = getFactory().createEffectLayer( |
| 2526 | LayerCreationArgs(this, nullptr, layer->name, |
| 2527 | ISurfaceComposerClient::eFXSurfaceEffect, LayerMetadata(), |
| 2528 | std::make_optional(layer->id), true)); |
| 2529 | mLegacyLayers[bgColorLayer->sequence] = bgColorLayer; |
| 2530 | } |
| 2531 | const bool willReleaseBufferOnLatch = layer->willReleaseBufferOnLatch(); |
Matthew DeVore | ce6bde8 | 2024-04-28 16:58:06 +0000 | [diff] [blame] | 2532 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2533 | auto it = mLegacyLayers.find(layer->id); |
| 2534 | if (it == mLegacyLayers.end() && |
| 2535 | layer->changes.test(frontend::RequestedLayerState::Changes::Destroyed)) { |
| 2536 | // Layer handle was created and immediately destroyed. It was destroyed before it |
| 2537 | // was added to the map. |
| 2538 | continue; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2541 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 2542 | "Couldnt find layer object for %s", |
| 2543 | layer->getDebugString().c_str()); |
| 2544 | if (!layer->hasReadyFrame() && !willReleaseBufferOnLatch) { |
| 2545 | if (!it->second->hasBuffer()) { |
| 2546 | // The last latch time is used to classify a missed frame as buffer stuffing |
| 2547 | // instead of a missed frame. This is used to identify scenarios where we |
| 2548 | // could not latch a buffer or apply a transaction due to backpressure. |
| 2549 | // We only update the latch time for buffer less layers here, the latch time |
| 2550 | // is updated for buffer layers when the buffer is latched. |
| 2551 | it->second->updateLastLatchTime(latchTime); |
| 2552 | } |
| 2553 | continue; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2556 | const bool bgColorOnly = |
| 2557 | !layer->externalTexture && (layer->bgColorLayerId != UNASSIGNED_LAYER_ID); |
| 2558 | if (willReleaseBufferOnLatch) { |
| 2559 | mLayersWithBuffersRemoved.emplace(it->second); |
Matthew DeVore | ce6bde8 | 2024-04-28 16:58:06 +0000 | [diff] [blame] | 2560 | } |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2561 | it->second->latchBufferImpl(unused, latchTime, bgColorOnly); |
| 2562 | newDataLatched = true; |
Matthew DeVore | ce6bde8 | 2024-04-28 16:58:06 +0000 | [diff] [blame] | 2563 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2564 | mLayersWithQueuedFrames.emplace(it->second); |
| 2565 | mLayersIdsWithQueuedFrames.emplace(it->second->sequence); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2566 | } |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2567 | |
| 2568 | updateLayerHistory(latchTime); |
| 2569 | mLayerSnapshotBuilder.forEachVisibleSnapshot([&](const frontend::LayerSnapshot& snapshot) { |
| 2570 | if (mLayersIdsWithQueuedFrames.find(snapshot.path.id) == mLayersIdsWithQueuedFrames.end()) |
| 2571 | return; |
| 2572 | Region visibleReg; |
| 2573 | visibleReg.set(snapshot.transformedBoundsWithoutTransparentRegion); |
| 2574 | invalidateLayerStack(snapshot.outputFilter, visibleReg); |
| 2575 | }); |
| 2576 | |
| 2577 | for (auto& destroyedLayer : mLayerLifecycleManager.getDestroyedLayers()) { |
| 2578 | mLegacyLayers.erase(destroyedLayer->id); |
| 2579 | } |
| 2580 | |
| 2581 | { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2582 | SFTRACE_NAME("LLM:commitChanges"); |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2583 | mLayerLifecycleManager.commitChanges(); |
| 2584 | } |
| 2585 | |
| 2586 | // enter boot animation on first buffer latch |
| 2587 | if (CC_UNLIKELY(mBootStage == BootStage::BOOTLOADER && newDataLatched)) { |
| 2588 | ALOGI("Enter boot animation"); |
| 2589 | mBootStage = BootStage::BOOTANIMATION; |
| 2590 | } |
| 2591 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 2592 | mustComposite |= (getTransactionFlags() & ~eTransactionFlushNeeded) || newDataLatched; |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2593 | if (mustComposite) { |
Vishnu Nair | 8134c1a | 2023-10-17 22:55:49 -0700 | [diff] [blame] | 2594 | commitTransactions(); |
| 2595 | } |
| 2596 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2597 | return mustComposite; |
| 2598 | } |
| 2599 | |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 2600 | bool SurfaceFlinger::commit(PhysicalDisplayId pacesetterId, |
| 2601 | const scheduler::FrameTargets& frameTargets) { |
| 2602 | const scheduler::FrameTarget& pacesetterFrameTarget = *frameTargets.get(pacesetterId)->get(); |
| 2603 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2604 | const VsyncId vsyncId = pacesetterFrameTarget.vsyncId(); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2605 | SFTRACE_NAME(ftl::Concat(__func__, ' ', ftl::to_underlying(vsyncId)).c_str()); |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2606 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2607 | if (pacesetterFrameTarget.didMissFrame()) { |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2608 | mTimeStats->incrementMissedFrames(); |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2609 | } |
| 2610 | |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 2611 | // If a mode set is pending and the fence hasn't fired yet, wait for the next commit. |
| 2612 | if (std::any_of(frameTargets.begin(), frameTargets.end(), |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 2613 | [this](const auto& pair) FTL_FAKE_GUARD(kMainThreadContext) { |
| 2614 | const auto [displayId, target] = pair; |
| 2615 | return target->isFramePending() && |
| 2616 | mDisplayModeController.isModeSetPending(displayId); |
| 2617 | })) { |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 2618 | mScheduler->scheduleFrame(); |
| 2619 | return false; |
| 2620 | } |
| 2621 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 2622 | for (const auto [displayId, _] : frameTargets) { |
| 2623 | if (mDisplayModeController.isModeSetPending(displayId)) { |
| 2624 | finalizeDisplayModeChange(displayId); |
yuhui.zhang | 087d374 | 2021-12-11 15:23:52 +0800 | [diff] [blame] | 2625 | } |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2628 | if (pacesetterFrameTarget.isFramePending()) { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 2629 | if (mBackpressureGpuComposition || pacesetterFrameTarget.didMissHwcFrame()) { |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 2630 | if (FlagManager::getInstance().vrr_config()) { |
| 2631 | mScheduler->getVsyncSchedule()->getTracker().onFrameMissed( |
| 2632 | pacesetterFrameTarget.expectedPresentTime()); |
| 2633 | } |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2634 | scheduleCommit(FrameHint::kNone); |
| 2635 | return false; |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2636 | } |
| 2637 | } |
| 2638 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2639 | const Period vsyncPeriod = mScheduler->getVsyncSchedule()->period(); |
| 2640 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2641 | // Save this once per commit + composite to ensure consistency |
Matt Buckley | ba8d15a | 2022-08-02 20:10:15 +0000 | [diff] [blame] | 2642 | // TODO (b/240619471): consider removing active display check once AOD is fixed |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 2643 | const auto activeDisplay = FTL_FAKE_GUARD(mStateLock, getDisplayDeviceLocked(mActiveDisplayId)); |
Matt Buckley | ba8d15a | 2022-08-02 20:10:15 +0000 | [diff] [blame] | 2644 | mPowerHintSessionEnabled = mPowerAdvisor->usePowerHintSession() && activeDisplay && |
| 2645 | activeDisplay->getPowerMode() == hal::PowerMode::ON; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2646 | if (mPowerHintSessionEnabled) { |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2647 | mPowerAdvisor->setCommitStart(pacesetterFrameTarget.frameBeginTime()); |
| 2648 | mPowerAdvisor->setExpectedPresentTime(pacesetterFrameTarget.expectedPresentTime()); |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 2649 | |
| 2650 | // Frame delay is how long we should have minus how long we actually have. |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 2651 | const Duration idealSfWorkDuration = |
| 2652 | mScheduler->vsyncModulator().getVsyncConfig().sfWorkDuration; |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2653 | const Duration frameDelay = |
| 2654 | idealSfWorkDuration - pacesetterFrameTarget.expectedFrameDuration(); |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 2655 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 2656 | mPowerAdvisor->setFrameDelay(frameDelay); |
| 2657 | mPowerAdvisor->setTotalFrameTargetWorkDuration(idealSfWorkDuration); |
Matt Buckley | 676e439 | 2023-05-25 22:09:26 +0000 | [diff] [blame] | 2658 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 2659 | const Period idealVsyncPeriod = |
| 2660 | mDisplayModeController.getActiveMode(pacesetterId).fps.getPeriod(); |
Matt Buckley | 676e439 | 2023-05-25 22:09:26 +0000 | [diff] [blame] | 2661 | mPowerAdvisor->updateTargetWorkDuration(idealVsyncPeriod); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 2664 | if (mRefreshRateOverlaySpinner || mHdrSdrRatioOverlay) { |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 2665 | Mutex::Autolock lock(mStateLock); |
| 2666 | if (const auto display = getDefaultDisplayDeviceLocked()) { |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 2667 | display->animateOverlay(); |
Ady Abraham | 29d0da3 | 2020-07-16 18:39:33 -0700 | [diff] [blame] | 2668 | } |
| 2669 | } |
| 2670 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2671 | // Composite if transactions were committed, or if requested by HWC. |
| 2672 | bool mustComposite = mMustComposite.exchange(false); |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2673 | { |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2674 | mFrameTimeline->setSfWakeUp(ftl::to_underlying(vsyncId), |
| 2675 | pacesetterFrameTarget.frameBeginTime().ns(), |
Pascal Muetschard | ac7bcd9 | 2023-10-03 15:05:36 +0200 | [diff] [blame] | 2676 | Fps::fromPeriodNsecs(vsyncPeriod.ns()), |
| 2677 | mScheduler->getPacesetterRefreshRate()); |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 2678 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2679 | const bool flushTransactions = clearTransactionFlags(eTransactionFlushNeeded); |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2680 | bool transactionsAreEmpty = false; |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 2681 | if (mLayerLifecycleManagerEnabled) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2682 | mustComposite |= |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2683 | updateLayerSnapshots(vsyncId, pacesetterFrameTarget.frameBeginTime().ns(), |
| 2684 | flushTransactions, transactionsAreEmpty); |
chaviw | 6b9ffea | 2021-11-08 09:25:48 -0600 | [diff] [blame] | 2685 | } |
| 2686 | |
Ady Abraham | aa7ebd6 | 2024-03-26 02:27:57 +0000 | [diff] [blame] | 2687 | // Tell VsyncTracker that we are going to present this frame before scheduling |
| 2688 | // setTransactionFlags which will schedule another SF frame. This was if the tracker |
| 2689 | // needs to adjust the vsync timeline, it will be done before the next frame. |
| 2690 | if (FlagManager::getInstance().vrr_config() && mustComposite) { |
| 2691 | mScheduler->getVsyncSchedule()->getTracker().onFrameBegin( |
| 2692 | pacesetterFrameTarget.expectedPresentTime(), |
| 2693 | pacesetterFrameTarget.lastSignaledFrameTime()); |
| 2694 | } |
chaviw | 6b9ffea | 2021-11-08 09:25:48 -0600 | [diff] [blame] | 2695 | if (transactionFlushNeeded()) { |
| 2696 | setTransactionFlags(eTransactionFlushNeeded); |
| 2697 | } |
| 2698 | |
chaviw | 6b9ffea | 2021-11-08 09:25:48 -0600 | [diff] [blame] | 2699 | // This has to be called after latchBuffers because we want to include the layers that have |
| 2700 | // been latched in the commit callback |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2701 | if (transactionsAreEmpty) { |
chaviw | 6b9ffea | 2021-11-08 09:25:48 -0600 | [diff] [blame] | 2702 | // Invoke empty transaction callbacks early. |
| 2703 | mTransactionCallbackInvoker.sendCallbacks(false /* onCommitOnly */); |
| 2704 | } else { |
| 2705 | // Invoke OnCommit callbacks. |
| 2706 | mTransactionCallbackInvoker.sendCallbacks(true /* onCommitOnly */); |
| 2707 | } |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | // Layers need to get updated (in the previous line) before we can use them for |
| 2711 | // choosing the refresh rate. |
| 2712 | // Hold mStateLock as chooseRefreshRateForContent promotes wp<Layer> to sp<Layer> |
| 2713 | // and may eventually call to ~Layer() if it holds the last reference |
| 2714 | { |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 2715 | bool updateAttachedChoreographer = mUpdateAttachedChoreographer; |
| 2716 | mUpdateAttachedChoreographer = false; |
| 2717 | |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 2718 | Mutex::Autolock lock(mStateLock); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 2719 | mScheduler->chooseRefreshRateForContent(mLayerLifecycleManagerEnabled |
| 2720 | ? &mLayerHierarchyBuilder.getHierarchy() |
| 2721 | : nullptr, |
| 2722 | updateAttachedChoreographer); |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2723 | } |
| 2724 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 2725 | initiateDisplayModeChanges(); |
| 2726 | |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2727 | updateCursorAsync(); |
Vishnu Nair | d2bfbb3 | 2023-07-18 00:45:45 +0000 | [diff] [blame] | 2728 | if (!mustComposite) { |
| 2729 | updateInputFlinger(vsyncId, pacesetterFrameTarget.frameBeginTime()); |
| 2730 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 2731 | doActiveLayersTracingIfNeeded(false, mVisibleRegionsDirty, |
| 2732 | pacesetterFrameTarget.frameBeginTime(), vsyncId); |
Vishnu Nair | 1fe1117 | 2023-04-03 18:10:09 +0000 | [diff] [blame] | 2733 | |
Pablo Gamito | 6be8244 | 2022-06-13 11:04:34 +0000 | [diff] [blame] | 2734 | mLastCommittedVsyncId = vsyncId; |
Vishnu Nair | b64a3b4 | 2022-01-13 15:29:32 -0800 | [diff] [blame] | 2735 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 2736 | persistDisplayBrightness(mustComposite); |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 2737 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2738 | return mustComposite && CC_LIKELY(mBootStage != BootStage::BOOTLOADER); |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2739 | } |
| 2740 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2741 | CompositeResultsPerDisplay SurfaceFlinger::composite( |
| 2742 | PhysicalDisplayId pacesetterId, const scheduler::FrameTargeters& frameTargeters) { |
| 2743 | const scheduler::FrameTarget& pacesetterTarget = |
| 2744 | frameTargeters.get(pacesetterId)->get()->target(); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2745 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2746 | const VsyncId vsyncId = pacesetterTarget.vsyncId(); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2747 | SFTRACE_NAME(ftl::Concat(__func__, ' ', ftl::to_underlying(vsyncId)).c_str()); |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 2748 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 2749 | compositionengine::CompositionRefreshArgs refreshArgs; |
jimmyshiu | 4e21177 | 2023-06-15 15:18:38 +0000 | [diff] [blame] | 2750 | refreshArgs.powerCallback = this; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 2751 | const auto& displays = FTL_FAKE_GUARD(mStateLock, mDisplays); |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 2752 | refreshArgs.outputs.reserve(displays.size()); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2753 | |
| 2754 | // Add outputs for physical displays. |
| 2755 | for (const auto& [id, targeter] : frameTargeters) { |
| 2756 | ftl::FakeGuard guard(mStateLock); |
| 2757 | |
| 2758 | if (const auto display = getCompositionDisplayLocked(id)) { |
| 2759 | refreshArgs.outputs.push_back(display); |
| 2760 | } |
Leon Scroggins III | 370b8b5 | 2022-12-08 13:20:45 -0500 | [diff] [blame] | 2761 | |
| 2762 | refreshArgs.frameTargets.try_emplace(id, &targeter->target()); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2763 | } |
| 2764 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2765 | std::vector<DisplayId> displayIds; |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 2766 | for (const auto& [_, display] : displays) { |
Dominik Laskowski | e85d7fa | 2023-05-15 10:50:22 -0400 | [diff] [blame] | 2767 | displayIds.push_back(display->getId()); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2768 | display->tracePowerMode(); |
| 2769 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2770 | // Add outputs for virtual displays. |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2771 | if (display->isVirtual()) { |
| 2772 | const Fps refreshRate = display->getAdjustedRefreshRate(); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2773 | |
| 2774 | if (!refreshRate.isValid() || |
| 2775 | mScheduler->isVsyncInPhase(pacesetterTarget.frameBeginTime(), refreshRate)) { |
| 2776 | refreshArgs.outputs.push_back(display->getCompositionDisplay()); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2777 | } |
| 2778 | } |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 2779 | } |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2780 | mPowerAdvisor->setDisplays(displayIds); |
Tianhao Yao | 67dd712 | 2022-02-22 17:48:33 +0000 | [diff] [blame] | 2781 | |
Vishnu Nair | 0a4fb00 | 2022-08-08 02:40:42 +0000 | [diff] [blame] | 2782 | const bool updateTaskMetadata = mCompositionEngine->getFeatureFlags().test( |
| 2783 | compositionengine::Feature::kSnapshotLayerMetadata); |
| 2784 | if (updateTaskMetadata && (mVisibleRegionsDirty || mLayerMetadataSnapshotNeeded)) { |
| 2785 | updateLayerMetadataSnapshot(); |
| 2786 | mLayerMetadataSnapshotNeeded = false; |
| 2787 | } |
| 2788 | |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 2789 | refreshArgs.bufferIdsToUncache = std::move(mBufferIdsToUncache); |
| 2790 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2791 | if (!FlagManager::getInstance().ce_fence_promise()) { |
| 2792 | refreshArgs.layersWithQueuedFrames.reserve(mLayersWithQueuedFrames.size()); |
| 2793 | for (auto& layer : mLayersWithQueuedFrames) { |
| 2794 | if (const auto& layerFE = layer->getCompositionEngineLayerFE()) |
| 2795 | refreshArgs.layersWithQueuedFrames.push_back(layerFE); |
| 2796 | } |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 2797 | } |
| 2798 | |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 2799 | refreshArgs.outputColorSetting = mDisplayColorSetting; |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 2800 | refreshArgs.forceOutputColorMode = mForceColorMode; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 2801 | |
| 2802 | refreshArgs.updatingOutputGeometryThisFrame = mVisibleRegionsDirty; |
Vishnu Nair | 995f4cf | 2024-03-07 19:40:03 -0800 | [diff] [blame] | 2803 | refreshArgs.updatingGeometryThisFrame = mGeometryDirty.exchange(false) || |
| 2804 | mVisibleRegionsDirty || mDrawingState.colorMatrixChanged; |
Leon Scroggins III | 85d4b22 | 2023-05-09 13:58:18 -0400 | [diff] [blame] | 2805 | refreshArgs.internalDisplayRotationFlags = getActiveDisplayRotationFlags(); |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 2806 | |
| 2807 | if (CC_UNLIKELY(mDrawingState.colorMatrixChanged)) { |
| 2808 | refreshArgs.colorTransformMatrix = mDrawingState.colorMatrix; |
| 2809 | mDrawingState.colorMatrixChanged = false; |
| 2810 | } |
| 2811 | |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2812 | refreshArgs.devOptForceClientComposition = mDebugDisableHWC; |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 2813 | |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2814 | if (mDebugFlashDelay != 0) { |
| 2815 | refreshArgs.devOptForceClientComposition = true; |
| 2816 | refreshArgs.devOptFlashDirtyRegionsDelay = std::chrono::milliseconds(mDebugFlashDelay); |
Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 2817 | } |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 2818 | |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 2819 | // TODO(b/255601557) Update frameInterval per display |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 2820 | refreshArgs.frameInterval = |
| 2821 | mScheduler->getNextFrameInterval(pacesetterId, pacesetterTarget.expectedPresentTime()); |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 2822 | const auto scheduledFrameResultOpt = mScheduler->getScheduledFrameResult(); |
| 2823 | const auto scheduledFrameTimeOpt = scheduledFrameResultOpt |
| 2824 | ? std::optional{scheduledFrameResultOpt->callbackTime} |
| 2825 | : std::nullopt; |
| 2826 | refreshArgs.scheduledFrameTime = scheduledFrameTimeOpt; |
Chavi Weingarten | 545da0e | 2023-02-09 14:55:57 +0000 | [diff] [blame] | 2827 | refreshArgs.hasTrustedPresentationListener = mNumTrustedPresentationListeners > 0; |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 2828 | // Store the present time just before calling to the composition engine so we could notify |
| 2829 | // the scheduler. |
| 2830 | const auto presentTime = systemTime(); |
| 2831 | |
Dominik Laskowski | 16a345b | 2023-04-27 17:07:33 -0400 | [diff] [blame] | 2832 | constexpr bool kCursorOnly = false; |
| 2833 | const auto layers = moveSnapshotsToCompositionArgs(refreshArgs, kCursorOnly); |
| 2834 | |
Vishnu Nair | 0109d3d | 2023-08-22 20:23:32 -0700 | [diff] [blame] | 2835 | if (mLayerLifecycleManagerEnabled && !mVisibleRegionsDirty) { |
Vishnu Nair | 854ce1c | 2023-08-19 15:00:13 -0700 | [diff] [blame] | 2836 | for (const auto& [token, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
| 2837 | auto compositionDisplay = display->getCompositionDisplay(); |
| 2838 | if (!compositionDisplay->getState().isEnabled) continue; |
| 2839 | for (auto outputLayer : compositionDisplay->getOutputLayersOrderedByZ()) { |
Vishnu Nair | 0109d3d | 2023-08-22 20:23:32 -0700 | [diff] [blame] | 2840 | if (outputLayer->getLayerFE().getCompositionState() == nullptr) { |
| 2841 | // This is unexpected but instead of crashing, capture traces to disk |
| 2842 | // and recover gracefully by forcing CE to rebuild layer stack. |
| 2843 | ALOGE("Output layer %s for display %s %" PRIu64 " has a null " |
| 2844 | "snapshot. Forcing mVisibleRegionsDirty", |
| 2845 | outputLayer->getLayerFE().getDebugName(), |
| 2846 | compositionDisplay->getName().c_str(), compositionDisplay->getId().value); |
| 2847 | |
| 2848 | TransactionTraceWriter::getInstance().invoke(__func__, /* overwrite= */ false); |
| 2849 | mVisibleRegionsDirty = true; |
| 2850 | refreshArgs.updatingOutputGeometryThisFrame = mVisibleRegionsDirty; |
| 2851 | refreshArgs.updatingGeometryThisFrame = mVisibleRegionsDirty; |
| 2852 | } |
Vishnu Nair | 854ce1c | 2023-08-19 15:00:13 -0700 | [diff] [blame] | 2853 | } |
| 2854 | } |
| 2855 | } |
| 2856 | |
Melody Hsu | c949cde | 2024-03-12 01:43:34 +0000 | [diff] [blame] | 2857 | refreshArgs.refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2858 | for (auto& [layer, layerFE] : layers) { |
Melody Hsu | c949cde | 2024-03-12 01:43:34 +0000 | [diff] [blame] | 2859 | layer->onPreComposition(refreshArgs.refreshStartTime); |
| 2860 | } |
| 2861 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2862 | if (FlagManager::getInstance().ce_fence_promise()) { |
| 2863 | for (auto& [layer, layerFE] : layers) { |
| 2864 | attachReleaseFenceFutureToLayer(layer, layerFE, |
| 2865 | layerFE->mSnapshot->outputFilter.layerStack); |
Vishnu Nair | e14c6b3 | 2022-08-06 04:20:15 +0000 | [diff] [blame] | 2866 | } |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2867 | |
| 2868 | refreshArgs.layersWithQueuedFrames.reserve(mLayersWithQueuedFrames.size()); |
| 2869 | for (auto& layer : mLayersWithQueuedFrames) { |
| 2870 | if (const auto& layerFE = layer->getCompositionEngineLayerFE()) { |
| 2871 | refreshArgs.layersWithQueuedFrames.push_back(layerFE); |
| 2872 | // Some layers are not displayed and do not yet have a future release fence |
| 2873 | if (layerFE->getReleaseFencePromiseStatus() == |
| 2874 | LayerFE::ReleaseFencePromiseStatus::UNINITIALIZED || |
| 2875 | layerFE->getReleaseFencePromiseStatus() == |
| 2876 | LayerFE::ReleaseFencePromiseStatus::FULFILLED) { |
| 2877 | // layerStack is invalid because layer is not on a display |
| 2878 | attachReleaseFenceFutureToLayer(layer.get(), layerFE.get(), |
| 2879 | ui::INVALID_LAYER_STACK); |
| 2880 | } |
| 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | mCompositionEngine->present(refreshArgs); |
| 2885 | moveSnapshotsFromCompositionArgs(refreshArgs, layers); |
| 2886 | |
| 2887 | for (auto& [layer, layerFE] : layers) { |
| 2888 | CompositionResult compositionResult{layerFE->stealCompositionResult()}; |
| 2889 | if (compositionResult.lastClientCompositionFence) { |
| 2890 | layer->setWasClientComposed(compositionResult.lastClientCompositionFence); |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | } else { |
| 2895 | mCompositionEngine->present(refreshArgs); |
| 2896 | moveSnapshotsFromCompositionArgs(refreshArgs, layers); |
| 2897 | |
| 2898 | for (auto [layer, layerFE] : layers) { |
| 2899 | CompositionResult compositionResult{layerFE->stealCompositionResult()}; |
| 2900 | for (auto& [releaseFence, layerStack] : compositionResult.releaseFences) { |
| 2901 | Layer* clonedFrom = layer->getClonedFrom().get(); |
| 2902 | auto owningLayer = clonedFrom ? clonedFrom : layer; |
| 2903 | owningLayer->onLayerDisplayed(std::move(releaseFence), layerStack); |
| 2904 | } |
| 2905 | if (compositionResult.lastClientCompositionFence) { |
| 2906 | layer->setWasClientComposed(compositionResult.lastClientCompositionFence); |
| 2907 | } |
Vishnu Nair | e14c6b3 | 2022-08-06 04:20:15 +0000 | [diff] [blame] | 2908 | } |
| 2909 | } |
| 2910 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 2911 | SFTRACE_NAME("postComposition"); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2912 | mTimeStats->recordFrameDuration(pacesetterTarget.frameBeginTime().ns(), systemTime()); |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 2913 | |
Dominik Laskowski | 61dfca4 | 2023-05-19 14:42:24 -0400 | [diff] [blame] | 2914 | // Send a power hint after presentation is finished. |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2915 | if (mPowerHintSessionEnabled) { |
Dominik Laskowski | 61dfca4 | 2023-05-19 14:42:24 -0400 | [diff] [blame] | 2916 | // Now that the current frame has been presented above, PowerAdvisor needs the present time |
| 2917 | // of the previous frame (whose fence is signaled by now) to determine how long the HWC had |
| 2918 | // waited on that fence to retire before presenting. |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2919 | const auto& previousPresentFence = pacesetterTarget.presentFenceForPreviousFrame(); |
Dominik Laskowski | e85d7fa | 2023-05-15 10:50:22 -0400 | [diff] [blame] | 2920 | |
Dominik Laskowski | 61dfca4 | 2023-05-19 14:42:24 -0400 | [diff] [blame] | 2921 | mPowerAdvisor->setSfPresentTiming(TimePoint::fromNs(previousPresentFence->getSignalTime()), |
| 2922 | TimePoint::now()); |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 2923 | mPowerAdvisor->reportActualWorkDuration(); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Leon Scroggins III | 5b58149 | 2023-10-31 14:29:41 -0400 | [diff] [blame] | 2926 | if (mScheduler->onCompositionPresented(presentTime)) { |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame] | 2927 | scheduleComposite(FrameHint::kNone); |
| 2928 | } |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 2929 | |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 2930 | mNotifyExpectedPresentMap[pacesetterId].hintStatus = NotifyExpectedPresentHintStatus::Start; |
Leon Scroggins III | 5b58149 | 2023-10-31 14:29:41 -0400 | [diff] [blame] | 2931 | onCompositionPresented(pacesetterId, frameTargeters, presentTime); |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 2932 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2933 | const bool hadGpuComposited = |
| 2934 | multiDisplayUnion(mCompositionCoverage).test(CompositionCoverage::Gpu); |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2935 | mCompositionCoverage.clear(); |
Alec Mouri | 8f7a010 | 2020-04-15 12:11:10 -0700 | [diff] [blame] | 2936 | |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 2937 | TimeStats::ClientCompositionRecord clientCompositionRecord; |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2938 | |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 2939 | for (const auto& [_, display] : displays) { |
| 2940 | const auto& state = display->getCompositionDisplay()->getState(); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2941 | CompositionCoverageFlags& flags = |
| 2942 | mCompositionCoverage.try_emplace(display->getId()).first->second; |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2943 | |
| 2944 | if (state.usesDeviceComposition) { |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2945 | flags |= CompositionCoverage::Hwc; |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | if (state.reusedClientComposition) { |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2949 | flags |= CompositionCoverage::GpuReuse; |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2950 | } else if (state.usesClientComposition) { |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2951 | flags |= CompositionCoverage::Gpu; |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2952 | } |
| 2953 | |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 2954 | clientCompositionRecord.predicted |= |
| 2955 | (state.strategyPrediction != CompositionStrategyPredictionState::DISABLED); |
| 2956 | clientCompositionRecord.predictionSucceeded |= |
| 2957 | (state.strategyPrediction == CompositionStrategyPredictionState::SUCCESS); |
Alec Mouri | 8f7a010 | 2020-04-15 12:11:10 -0700 | [diff] [blame] | 2958 | } |
| 2959 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2960 | const auto coverage = multiDisplayUnion(mCompositionCoverage); |
| 2961 | const bool hasGpuComposited = coverage.test(CompositionCoverage::Gpu); |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2962 | |
| 2963 | clientCompositionRecord.hadClientComposition = hasGpuComposited; |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2964 | clientCompositionRecord.reused = coverage.test(CompositionCoverage::GpuReuse); |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2965 | clientCompositionRecord.changed = hadGpuComposited != hasGpuComposited; |
| 2966 | |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 2967 | mTimeStats->pushCompositionStrategyState(clientCompositionRecord); |
| 2968 | |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2969 | using namespace ftl::flag_operators; |
| 2970 | |
| 2971 | // TODO(b/160583065): Enable skip validation when SF caches all client composition layers. |
| 2972 | const bool hasGpuUseOrReuse = |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2973 | coverage.any(CompositionCoverage::Gpu | CompositionCoverage::GpuReuse); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 2974 | mScheduler->modulateVsync({}, &VsyncModulator::onDisplayRefresh, hasGpuUseOrReuse); |
Dan Stoza | 14cd37c | 2015-07-09 12:43:33 -0700 | [diff] [blame] | 2975 | |
David Sodman | 7e4ae11 | 2018-02-09 15:02:28 -0800 | [diff] [blame] | 2976 | mLayersWithQueuedFrames.clear(); |
Vishnu Nair | 6f209e2 | 2023-08-04 16:33:23 +0000 | [diff] [blame] | 2977 | mLayersIdsWithQueuedFrames.clear(); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 2978 | doActiveLayersTracingIfNeeded(true, mVisibleRegionsDirty, pacesetterTarget.frameBeginTime(), |
| 2979 | vsyncId); |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 2980 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2981 | updateInputFlinger(vsyncId, pacesetterTarget.frameBeginTime()); |
Vishnu Nair | d2bfbb3 | 2023-07-18 00:45:45 +0000 | [diff] [blame] | 2982 | |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 2983 | if (mVisibleRegionsDirty) mHdrLayerInfoChanged = true; |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 2984 | mVisibleRegionsDirty = false; |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 2985 | |
| 2986 | if (mCompositionEngine->needsAnotherUpdate()) { |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2987 | scheduleCommit(FrameHint::kNone); |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 2988 | } |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 2989 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2990 | if (mPowerHintSessionEnabled) { |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 2991 | mPowerAdvisor->setCompositeEnd(TimePoint::now()); |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 2992 | } |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2993 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2994 | CompositeResultsPerDisplay resultsPerDisplay; |
| 2995 | |
| 2996 | // Filter out virtual displays. |
| 2997 | for (const auto& [id, coverage] : mCompositionCoverage) { |
| 2998 | if (const auto idOpt = PhysicalDisplayId::tryCast(id)) { |
| 2999 | resultsPerDisplay.try_emplace(*idOpt, CompositeResult{coverage}); |
| 3000 | } |
| 3001 | } |
| 3002 | |
| 3003 | return resultsPerDisplay; |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 3004 | } |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 3005 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 3006 | void SurfaceFlinger::updateLayerGeometry() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 3007 | SFTRACE_CALL(); |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 3008 | |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 3009 | if (mVisibleRegionsDirty) { |
| 3010 | computeLayerBounds(); |
| 3011 | } |
| 3012 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 3013 | for (auto& layer : mLayersPendingRefresh) { |
| 3014 | Region visibleReg; |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 3015 | visibleReg.set(layer->getScreenBounds()); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 3016 | invalidateLayerStack(layer->getOutputFilter(), visibleReg); |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 3017 | } |
| 3018 | mLayersPendingRefresh.clear(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 3019 | } |
| 3020 | |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 3021 | bool SurfaceFlinger::isHdrLayer(const frontend::LayerSnapshot& snapshot) const { |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 3022 | // Even though the camera layer may be using an HDR transfer function or otherwise be "HDR" |
| 3023 | // the device may need to avoid boosting the brightness as a result of these layers to |
| 3024 | // reduce power consumption during camera recording |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3025 | if (mIgnoreHdrCameraLayers) { |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 3026 | if (snapshot.externalTexture && |
| 3027 | (snapshot.externalTexture->getUsage() & GRALLOC_USAGE_HW_CAMERA_WRITE) != 0) { |
John Reck | 49d9ad3 | 2022-02-23 19:03:31 -0500 | [diff] [blame] | 3028 | return false; |
| 3029 | } |
| 3030 | } |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 3031 | // RANGE_EXTENDED layer may identify themselves as being "HDR" |
| 3032 | // via a desired hdr/sdr ratio |
| 3033 | auto pixelFormat = snapshot.buffer |
| 3034 | ? std::make_optional(static_cast<ui::PixelFormat>(snapshot.buffer->getPixelFormat())) |
| 3035 | : std::nullopt; |
| 3036 | |
| 3037 | if (getHdrRenderType(snapshot.dataspace, pixelFormat, snapshot.desiredHdrSdrRatio) != |
| 3038 | HdrRenderType::SDR) { |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 3039 | return true; |
| 3040 | } |
| 3041 | // If the layer is not allowed to be dimmed, treat it as HDR. WindowManager may disable |
| 3042 | // dimming in order to keep animations invoking SDR screenshots of HDR layers seamless. |
| 3043 | // Treat such tagged layers as HDR so that DisplayManagerService does not try to change |
| 3044 | // the screen brightness |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 3045 | if (!snapshot.dimmingEnabled) { |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 3046 | return true; |
| 3047 | } |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 3048 | return false; |
John Reck | 49d9ad3 | 2022-02-23 19:03:31 -0500 | [diff] [blame] | 3049 | } |
| 3050 | |
ramindani | 06e518e | 2022-03-14 18:47:53 +0000 | [diff] [blame] | 3051 | ui::Rotation SurfaceFlinger::getPhysicalDisplayOrientation(DisplayId displayId, |
| 3052 | bool isPrimary) const { |
| 3053 | const auto id = PhysicalDisplayId::tryCast(displayId); |
| 3054 | if (!id) { |
| 3055 | return ui::ROTATION_0; |
| 3056 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3057 | if (!mIgnoreHwcPhysicalDisplayOrientation && |
Neha Jain | 7b31cca | 2023-01-09 23:10:42 +0000 | [diff] [blame] | 3058 | getHwComposer().getComposer()->isSupported( |
ramindani | 06e518e | 2022-03-14 18:47:53 +0000 | [diff] [blame] | 3059 | Hwc2::Composer::OptionalFeature::PhysicalDisplayOrientation)) { |
| 3060 | switch (getHwComposer().getPhysicalDisplayOrientation(*id)) { |
| 3061 | case Hwc2::AidlTransform::ROT_90: |
| 3062 | return ui::ROTATION_90; |
| 3063 | case Hwc2::AidlTransform::ROT_180: |
| 3064 | return ui::ROTATION_180; |
| 3065 | case Hwc2::AidlTransform::ROT_270: |
| 3066 | return ui::ROTATION_270; |
| 3067 | default: |
| 3068 | return ui::ROTATION_0; |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | if (isPrimary) { |
| 3073 | using Values = SurfaceFlingerProperties::primary_display_orientation_values; |
| 3074 | switch (primary_display_orientation(Values::ORIENTATION_0)) { |
| 3075 | case Values::ORIENTATION_90: |
| 3076 | return ui::ROTATION_90; |
| 3077 | case Values::ORIENTATION_180: |
| 3078 | return ui::ROTATION_180; |
| 3079 | case Values::ORIENTATION_270: |
| 3080 | return ui::ROTATION_270; |
| 3081 | default: |
| 3082 | break; |
| 3083 | } |
| 3084 | } |
| 3085 | return ui::ROTATION_0; |
| 3086 | } |
| 3087 | |
Leon Scroggins III | 5b58149 | 2023-10-31 14:29:41 -0400 | [diff] [blame] | 3088 | void SurfaceFlinger::onCompositionPresented(PhysicalDisplayId pacesetterId, |
| 3089 | const scheduler::FrameTargeters& frameTargeters, |
| 3090 | nsecs_t presentStartTime) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 3091 | SFTRACE_CALL(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3092 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3093 | ui::PhysicalDisplayMap<PhysicalDisplayId, std::shared_ptr<FenceTime>> presentFences; |
| 3094 | ui::PhysicalDisplayMap<PhysicalDisplayId, const sp<Fence>> gpuCompositionDoneFences; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 3095 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3096 | for (const auto& [id, targeter] : frameTargeters) { |
| 3097 | auto presentFence = getHwComposer().getPresentFence(id); |
| 3098 | |
| 3099 | if (id == pacesetterId) { |
| 3100 | mTransactionCallbackInvoker.addPresentFence(presentFence); |
| 3101 | } |
| 3102 | |
| 3103 | if (auto fenceTime = targeter->setPresentFence(std::move(presentFence)); |
| 3104 | fenceTime->isValid()) { |
| 3105 | presentFences.try_emplace(id, std::move(fenceTime)); |
| 3106 | } |
| 3107 | |
| 3108 | ftl::FakeGuard guard(mStateLock); |
| 3109 | if (const auto display = getCompositionDisplayLocked(id); |
| 3110 | display && display->getState().usesClientComposition) { |
| 3111 | gpuCompositionDoneFences |
| 3112 | .try_emplace(id, display->getRenderSurface()->getClientTargetAcquireFence()); |
| 3113 | } |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 3114 | } |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 3115 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3116 | const auto pacesetterDisplay = FTL_FAKE_GUARD(mStateLock, getDisplayDeviceLocked(pacesetterId)); |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 3117 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3118 | std::shared_ptr<FenceTime> pacesetterPresentFenceTime = |
| 3119 | presentFences.get(pacesetterId) |
| 3120 | .transform([](const FenceTimePtr& ptr) { return ptr; }) |
| 3121 | .value_or(FenceTime::NO_FENCE); |
| 3122 | |
| 3123 | std::shared_ptr<FenceTime> pacesetterGpuCompositionDoneFenceTime = |
| 3124 | gpuCompositionDoneFences.get(pacesetterId) |
| 3125 | .transform([](sp<Fence> fence) { |
| 3126 | return std::make_shared<FenceTime>(std::move(fence)); |
| 3127 | }) |
| 3128 | .value_or(FenceTime::NO_FENCE); |
| 3129 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 3130 | const TimePoint presentTime = TimePoint::now(); |
Vishnu Nair | 9a69a04 | 2021-06-18 13:19:49 -0700 | [diff] [blame] | 3131 | |
Jorim Jaggi | 8d34c90 | 2020-12-16 21:43:07 +0100 | [diff] [blame] | 3132 | // Set presentation information before calling Layer::releasePendingBuffer, such that jank |
| 3133 | // information from previous' frame classification is already available when sending jank info |
| 3134 | // to clients, so they get jank classification as early as possible. |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3135 | mFrameTimeline->setSfPresent(presentTime.ns(), pacesetterPresentFenceTime, |
| 3136 | pacesetterGpuCompositionDoneFenceTime); |
Jorim Jaggi | 8d34c90 | 2020-12-16 21:43:07 +0100 | [diff] [blame] | 3137 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 3138 | // We use the CompositionEngine::getLastFrameRefreshTimestamp() which might |
| 3139 | // be sampled a little later than when we started doing work for this frame, |
Dominik Laskowski | 5a5e01e | 2022-07-08 07:52:44 -0700 | [diff] [blame] | 3140 | // but that should be okay since CompositorTiming has snapping logic. |
Dominik Laskowski | 80c7727 | 2022-07-08 12:47:29 -0700 | [diff] [blame] | 3141 | const TimePoint compositeTime = |
| 3142 | TimePoint::fromNs(mCompositionEngine->getLastFrameRefreshTimestamp()); |
Dominik Laskowski | e62606d | 2024-02-22 11:42:13 -0500 | [diff] [blame] | 3143 | const Duration presentLatency = mHasReliablePresentFences |
| 3144 | ? mPresentLatencyTracker.trackPendingFrame(compositeTime, pacesetterPresentFenceTime) |
| 3145 | : Duration::zero(); |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 3146 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 3147 | const auto schedule = mScheduler->getVsyncSchedule(); |
| 3148 | const TimePoint vsyncDeadline = schedule->vsyncDeadlineAfter(presentTime); |
| 3149 | const Period vsyncPeriod = schedule->period(); |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 3150 | const nsecs_t vsyncPhase = |
| 3151 | mScheduler->getVsyncConfiguration().getCurrentConfigs().late.sfOffset; |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 3152 | |
Dominik Laskowski | 5a5e01e | 2022-07-08 07:52:44 -0700 | [diff] [blame] | 3153 | const CompositorTiming compositorTiming(vsyncDeadline.ns(), vsyncPeriod.ns(), vsyncPhase, |
Dominik Laskowski | 80c7727 | 2022-07-08 12:47:29 -0700 | [diff] [blame] | 3154 | presentLatency.ns()); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 3155 | |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 3156 | ui::DisplayMap<ui::LayerStack, const DisplayDevice*> layerStackToDisplay; |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 3157 | { |
| 3158 | if (!mLayersWithBuffersRemoved.empty() || mNumTrustedPresentationListeners > 0) { |
| 3159 | Mutex::Autolock lock(mStateLock); |
| 3160 | for (const auto& [token, display] : mDisplays) { |
| 3161 | layerStackToDisplay.emplace_or_replace(display->getLayerStack(), display.get()); |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | for (auto layer : mLayersWithBuffersRemoved) { |
Vishnu Nair | f10a87b | 2023-05-01 14:02:51 -0700 | [diff] [blame] | 3167 | std::vector<ui::LayerStack> previouslyPresentedLayerStacks = |
| 3168 | std::move(layer->mPreviouslyPresentedLayerStacks); |
| 3169 | layer->mPreviouslyPresentedLayerStacks.clear(); |
| 3170 | for (auto layerStack : previouslyPresentedLayerStacks) { |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 3171 | auto optDisplay = layerStackToDisplay.get(layerStack); |
| 3172 | if (optDisplay && !optDisplay->get()->isVirtual()) { |
| 3173 | auto fence = getHwComposer().getPresentFence(optDisplay->get()->getPhysicalId()); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 3174 | if (FlagManager::getInstance().ce_fence_promise()) { |
| 3175 | layer->prepareReleaseCallbacks(ftl::yield<FenceResult>(fence), |
| 3176 | ui::INVALID_LAYER_STACK); |
| 3177 | } else { |
| 3178 | layer->onLayerDisplayed(ftl::yield<FenceResult>(fence).share(), |
| 3179 | ui::INVALID_LAYER_STACK); |
| 3180 | } |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 3181 | } |
| 3182 | } |
| 3183 | layer->releasePendingBuffer(presentTime.ns()); |
| 3184 | } |
| 3185 | mLayersWithBuffersRemoved.clear(); |
| 3186 | |
Robert Carr | c747ad0 | 2021-06-11 14:01:24 -0700 | [diff] [blame] | 3187 | for (const auto& layer: mLayersWithQueuedFrames) { |
Leon Scroggins III | 5b58149 | 2023-10-31 14:29:41 -0400 | [diff] [blame] | 3188 | layer->onCompositionPresented(pacesetterDisplay.get(), |
| 3189 | pacesetterGpuCompositionDoneFenceTime, |
| 3190 | pacesetterPresentFenceTime, compositorTiming); |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 3191 | layer->releasePendingBuffer(presentTime.ns()); |
Robert Carr | c747ad0 | 2021-06-11 14:01:24 -0700 | [diff] [blame] | 3192 | } |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 3193 | |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 3194 | std::vector<std::pair<std::shared_ptr<compositionengine::Display>, sp<HdrLayerInfoReporter>>> |
| 3195 | hdrInfoListeners; |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3196 | bool haveNewListeners = false; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 3197 | { |
| 3198 | Mutex::Autolock lock(mStateLock); |
| 3199 | if (mFpsReporter) { |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 3200 | mFpsReporter->dispatchLayerFps(mLayerHierarchyBuilder.getHierarchy()); |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 3201 | } |
Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 3202 | |
| 3203 | if (mTunnelModeEnabledReporter) { |
| 3204 | mTunnelModeEnabledReporter->updateTunnelModeStatus(); |
| 3205 | } |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 3206 | hdrInfoListeners.reserve(mHdrLayerInfoListeners.size()); |
Dominik Laskowski | 7b9bf4c | 2021-03-18 12:21:29 -0700 | [diff] [blame] | 3207 | for (const auto& [displayId, reporter] : mHdrLayerInfoListeners) { |
| 3208 | if (reporter && reporter->hasListeners()) { |
| 3209 | if (const auto display = getDisplayDeviceLocked(displayId)) { |
| 3210 | hdrInfoListeners.emplace_back(display->getCompositionDisplay(), reporter); |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 3211 | } |
| 3212 | } |
| 3213 | } |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3214 | haveNewListeners = mAddingHDRLayerInfoListener; // grab this with state lock |
| 3215 | mAddingHDRLayerInfoListener = false; |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 3216 | } |
| 3217 | |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 3218 | if (haveNewListeners || mHdrLayerInfoChanged) { |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3219 | for (auto& [compositionDisplay, listener] : hdrInfoListeners) { |
| 3220 | HdrLayerInfoReporter::HdrLayerInfo info; |
| 3221 | int32_t maxArea = 0; |
Alec Mouri | 1b0d4e1 | 2024-02-12 22:27:19 +0000 | [diff] [blame] | 3222 | |
Vishnu Nair | 9a5463a | 2023-06-17 12:18:36 -0700 | [diff] [blame] | 3223 | auto updateInfoFn = |
| 3224 | [&](const std::shared_ptr<compositionengine::Display>& compositionDisplay, |
| 3225 | const frontend::LayerSnapshot& snapshot, const sp<LayerFE>& layerFe) { |
| 3226 | if (snapshot.isVisible && |
| 3227 | compositionDisplay->includesLayer(snapshot.outputFilter)) { |
| 3228 | if (isHdrLayer(snapshot)) { |
| 3229 | const auto* outputLayer = |
| 3230 | compositionDisplay->getOutputLayerForLayer(layerFe); |
| 3231 | if (outputLayer) { |
| 3232 | const float desiredHdrSdrRatio = |
Alec Mouri | 1b0d4e1 | 2024-02-12 22:27:19 +0000 | [diff] [blame] | 3233 | snapshot.desiredHdrSdrRatio < 1.f |
Vishnu Nair | 9a5463a | 2023-06-17 12:18:36 -0700 | [diff] [blame] | 3234 | ? std::numeric_limits<float>::infinity() |
| 3235 | : snapshot.desiredHdrSdrRatio; |
| 3236 | info.mergeDesiredRatio(desiredHdrSdrRatio); |
| 3237 | info.numberOfHdrLayers++; |
| 3238 | const auto displayFrame = outputLayer->getState().displayFrame; |
| 3239 | const int32_t area = |
| 3240 | displayFrame.width() * displayFrame.height(); |
| 3241 | if (area > maxArea) { |
| 3242 | maxArea = area; |
| 3243 | info.maxW = displayFrame.width(); |
| 3244 | info.maxH = displayFrame.height(); |
| 3245 | } |
| 3246 | } |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3247 | } |
John Reck | db1a16b | 2021-07-09 13:05:52 -0400 | [diff] [blame] | 3248 | } |
Vishnu Nair | 9a5463a | 2023-06-17 12:18:36 -0700 | [diff] [blame] | 3249 | }; |
| 3250 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3251 | if (mLayerLifecycleManagerEnabled) { |
Vishnu Nair | 9a5463a | 2023-06-17 12:18:36 -0700 | [diff] [blame] | 3252 | mLayerSnapshotBuilder.forEachVisibleSnapshot( |
| 3253 | [&, compositionDisplay = compositionDisplay]( |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 3254 | std::unique_ptr<frontend::LayerSnapshot>& |
| 3255 | snapshot) FTL_FAKE_GUARD(kMainThreadContext) { |
Vishnu Nair | 9a5463a | 2023-06-17 12:18:36 -0700 | [diff] [blame] | 3256 | auto it = mLegacyLayers.find(snapshot->sequence); |
Vishnu Nair | 5943bf6 | 2023-12-08 22:04:20 +0000 | [diff] [blame] | 3257 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 3258 | "Couldnt find layer object for %s", |
| 3259 | snapshot->getDebugString().c_str()); |
Vishnu Nair | 9a5463a | 2023-06-17 12:18:36 -0700 | [diff] [blame] | 3260 | auto& legacyLayer = it->second; |
| 3261 | sp<LayerFE> layerFe = |
| 3262 | legacyLayer->getCompositionEngineLayerFE(snapshot->path); |
| 3263 | |
| 3264 | updateInfoFn(compositionDisplay, *snapshot, layerFe); |
| 3265 | }); |
| 3266 | } else { |
| 3267 | mDrawingState.traverse([&, compositionDisplay = compositionDisplay](Layer* layer) { |
| 3268 | const auto layerFe = layer->getCompositionEngineLayerFE(); |
| 3269 | const frontend::LayerSnapshot& snapshot = *layer->getLayerSnapshot(); |
| 3270 | updateInfoFn(compositionDisplay, snapshot, layerFe); |
| 3271 | }); |
| 3272 | } |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3273 | listener->dispatchHdrLayerInfo(info); |
| 3274 | } |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 3275 | } |
| 3276 | |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 3277 | mHdrLayerInfoChanged = false; |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3278 | |
Vishnu Nair | 1fe1117 | 2023-04-03 18:10:09 +0000 | [diff] [blame] | 3279 | mTransactionCallbackInvoker.sendCallbacks(false /* onCommitOnly */); |
| 3280 | mTransactionCallbackInvoker.clearCompletedTransactions(); |
| 3281 | |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 3282 | mTimeStats->incrementTotalFrames(); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3283 | mTimeStats->setPresentFenceGlobal(pacesetterPresentFenceTime); |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 3284 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3285 | for (auto&& [id, presentFence] : presentFences) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 3286 | ftl::FakeGuard guard(mStateLock); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3287 | const bool isInternalDisplay = |
| 3288 | mPhysicalDisplays.get(id).transform(&PhysicalDisplay::isInternal).value_or(false); |
| 3289 | |
| 3290 | if (isInternalDisplay) { |
| 3291 | mScheduler->addPresentFence(id, std::move(presentFence)); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 3292 | } |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 3293 | } |
| 3294 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3295 | const bool hasPacesetterDisplay = |
| 3296 | pacesetterDisplay && getHwComposer().isConnected(pacesetterId); |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 3297 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3298 | if (!hasSyncFramework) { |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3299 | if (hasPacesetterDisplay && pacesetterDisplay->isPoweredOn()) { |
| 3300 | mScheduler->enableHardwareVsync(pacesetterId); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 3301 | } |
| 3302 | } |
| 3303 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3304 | if (hasPacesetterDisplay && !pacesetterDisplay->isPoweredOn()) { |
Lars Svensson | d9e5469 | 2021-12-21 07:41:57 +0100 | [diff] [blame] | 3305 | getRenderEngine().cleanupPostRender(); |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 3306 | return; |
| 3307 | } |
| 3308 | |
Lingfeng Yang | 2e4fef6 | 2020-04-24 14:48:43 +0000 | [diff] [blame] | 3309 | // Cleanup any outstanding resources due to rendering a prior frame. |
| 3310 | getRenderEngine().cleanupPostRender(); |
| 3311 | |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 3312 | if (mNumTrustedPresentationListeners > 0) { |
| 3313 | // We avoid any reverse traversal upwards so this shouldn't be too expensive |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 3314 | traverseLegacyLayers([&](Layer* layer) FTL_FAKE_GUARD(kMainThreadContext) { |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 3315 | if (!layer->hasTrustedPresentationListener()) { |
| 3316 | return; |
| 3317 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3318 | const frontend::LayerSnapshot* snapshot = mLayerLifecycleManagerEnabled |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 3319 | ? mLayerSnapshotBuilder.getSnapshot(layer->sequence) |
| 3320 | : layer->getLayerSnapshot(); |
| 3321 | std::optional<const DisplayDevice*> displayOpt = std::nullopt; |
| 3322 | if (snapshot) { |
| 3323 | displayOpt = layerStackToDisplay.get(snapshot->outputFilter.layerStack); |
| 3324 | } |
Vishnu Nair | fad494c | 2023-02-01 19:52:54 +0000 | [diff] [blame] | 3325 | const DisplayDevice* display = displayOpt.value_or(nullptr); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 3326 | layer->updateTrustedPresentationState(display, snapshot, |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 3327 | nanoseconds_to_milliseconds(presentStartTime), |
| 3328 | false); |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 3329 | }); |
| 3330 | } |
| 3331 | |
Dan Stoza | 45de5ba | 2019-04-25 14:12:09 -0700 | [diff] [blame] | 3332 | // Even though ATRACE_INT64 already checks if tracing is enabled, it doesn't prevent the |
| 3333 | // side-effect of getTotalSize(), so we check that again here |
| 3334 | if (ATRACE_ENABLED()) { |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 3335 | // getTotalSize returns the total number of buffers that were allocated by SurfaceFlinger |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 3336 | SFTRACE_INT64("Total Buffer Size", GraphicBufferAllocator::get().getTotalSize()); |
Dan Stoza | 45de5ba | 2019-04-25 14:12:09 -0700 | [diff] [blame] | 3337 | } |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 3338 | |
| 3339 | logFrameStats(presentTime); |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 3340 | } |
| 3341 | |
chaviw | 79468ab | 2021-10-27 11:11:24 -0500 | [diff] [blame] | 3342 | FloatRect SurfaceFlinger::getMaxDisplayBounds() { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 3343 | const ui::Size maxSize = [this] { |
| 3344 | ftl::FakeGuard guard(mStateLock); |
Vishnu Nair | f6f5695 | 2022-03-01 20:29:46 -0800 | [diff] [blame] | 3345 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 3346 | // The LayerTraceGenerator tool runs without displays. |
| 3347 | if (mDisplays.empty()) return ui::Size{5000, 5000}; |
Vishnu Nair | f6f5695 | 2022-03-01 20:29:46 -0800 | [diff] [blame] | 3348 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 3349 | return std::accumulate(mDisplays.begin(), mDisplays.end(), ui::kEmptySize, |
| 3350 | [](ui::Size size, const auto& pair) -> ui::Size { |
| 3351 | const auto& display = pair.second; |
| 3352 | return {std::max(size.getWidth(), display->getWidth()), |
| 3353 | std::max(size.getHeight(), display->getHeight())}; |
| 3354 | }); |
| 3355 | }(); |
chaviw | b09c655 | 2021-08-12 17:20:43 -0500 | [diff] [blame] | 3356 | |
| 3357 | // Ignore display bounds for now since they will be computed later. Use a large Rect bound |
| 3358 | // to ensure it's bigger than an actual display will be. |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 3359 | const float xMax = maxSize.getWidth() * 10.f; |
| 3360 | const float yMax = maxSize.getHeight() * 10.f; |
| 3361 | |
| 3362 | return {-xMax, -yMax, xMax, yMax}; |
chaviw | 79468ab | 2021-10-27 11:11:24 -0500 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | void SurfaceFlinger::computeLayerBounds() { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 3366 | const FloatRect maxBounds = getMaxDisplayBounds(); |
chaviw | b09c655 | 2021-08-12 17:20:43 -0500 | [diff] [blame] | 3367 | for (const auto& layer : mDrawingState.layersSortedByZ) { |
| 3368 | layer->computeBounds(maxBounds, ui::Transform(), 0.f /* shadowRadius */); |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 3369 | } |
| 3370 | } |
| 3371 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 3372 | void SurfaceFlinger::commitTransactions() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 3373 | SFTRACE_CALL(); |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 3374 | mDebugInTransaction = systemTime(); |
| 3375 | |
| 3376 | // Here we're guaranteed that some transaction flags are set |
| 3377 | // so we can call commitTransactionsLocked unconditionally. |
| 3378 | // We clear the flags with mStateLock held to guarantee that |
| 3379 | // mCurrentState won't change until the transaction is committed. |
| 3380 | mScheduler->modulateVsync({}, &VsyncModulator::onTransactionCommit); |
| 3381 | commitTransactionsLocked(clearTransactionFlags(eTransactionMask)); |
| 3382 | mDebugInTransaction = 0; |
| 3383 | } |
| 3384 | |
| 3385 | void SurfaceFlinger::commitTransactionsLegacy() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 3386 | SFTRACE_CALL(); |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 3387 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 3388 | // Keep a copy of the drawing state (that is going to be overwritten |
| 3389 | // by commitTransactionsLocked) outside of mStateLock so that the side |
| 3390 | // effects of the State assignment don't happen with mStateLock held, |
| 3391 | // which can cause deadlocks. |
Mathias Agopian | 7cc6df5 | 2013-06-05 14:30:54 -0700 | [diff] [blame] | 3392 | State drawingState(mDrawingState); |
| 3393 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 3394 | Mutex::Autolock lock(mStateLock); |
Dominik Laskowski | 9dab343 | 2019-03-27 13:21:10 -0700 | [diff] [blame] | 3395 | mDebugInTransaction = systemTime(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3396 | |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 3397 | // Here we're guaranteed that some transaction flags are set |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 3398 | // so we can call commitTransactionsLocked unconditionally. |
| 3399 | // We clear the flags with mStateLock held to guarantee that |
| 3400 | // mCurrentState won't change until the transaction is committed. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 3401 | mScheduler->modulateVsync({}, &VsyncModulator::onTransactionCommit); |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 3402 | commitTransactionsLocked(clearTransactionFlags(eTransactionMask)); |
Mathias Agopian | dea20b1 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 3403 | |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 3404 | mDebugInTransaction = 0; |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 3405 | } |
| 3406 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3407 | std::pair<DisplayModes, DisplayModePtr> SurfaceFlinger::loadDisplayModes( |
| 3408 | PhysicalDisplayId displayId) const { |
Marin Shalamanov | d3b5c5d | 2021-02-11 18:26:14 +0100 | [diff] [blame] | 3409 | std::vector<HWComposer::HWCDisplayMode> hwcModes; |
Dominik Laskowski | d940a01 | 2024-01-28 13:25:44 -0500 | [diff] [blame] | 3410 | std::optional<hal::HWConfigId> activeModeHwcIdOpt; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3411 | |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 3412 | const bool isExternalDisplay = FlagManager::getInstance().connected_display() && |
| 3413 | getHwComposer().getDisplayConnectionType(displayId) == |
| 3414 | ui::DisplayConnectionType::External; |
| 3415 | |
Marin Shalamanov | d3b5c5d | 2021-02-11 18:26:14 +0100 | [diff] [blame] | 3416 | int attempt = 0; |
| 3417 | constexpr int kMaxAttempts = 3; |
| 3418 | do { |
ramindani | 263a3f1 | 2023-07-18 20:44:49 -0700 | [diff] [blame] | 3419 | hwcModes = getHwComposer().getModes(displayId, |
| 3420 | scheduler::RefreshRateSelector::kMinSupportedFrameRate |
| 3421 | .getPeriodNsecs()); |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 3422 | const auto activeModeHwcIdExp = getHwComposer().getActiveMode(displayId); |
| 3423 | activeModeHwcIdOpt = activeModeHwcIdExp.value_opt(); |
| 3424 | |
| 3425 | if (isExternalDisplay && |
| 3426 | activeModeHwcIdExp.has_error([](status_t error) { return error == NO_INIT; })) { |
| 3427 | constexpr nsecs_t k59HzVsyncPeriod = 16949153; |
| 3428 | constexpr nsecs_t k60HzVsyncPeriod = 16666667; |
| 3429 | |
| 3430 | // DM sets the initial mode for an external display to 1080p@60, but |
| 3431 | // this comes after SF creates its own state (including the |
| 3432 | // DisplayDevice). For now, pick the same mode in order to avoid |
| 3433 | // inconsistent state and unnecessary mode switching. |
| 3434 | // TODO (b/318534874): Let DM decide the initial mode. |
| 3435 | // |
| 3436 | // Try to find 1920x1080 @ 60 Hz |
| 3437 | if (const auto iter = std::find_if(hwcModes.begin(), hwcModes.end(), |
| 3438 | [](const auto& mode) { |
| 3439 | return mode.width == 1920 && |
| 3440 | mode.height == 1080 && |
| 3441 | mode.vsyncPeriod == k60HzVsyncPeriod; |
| 3442 | }); |
| 3443 | iter != hwcModes.end()) { |
| 3444 | activeModeHwcIdOpt = iter->hwcId; |
| 3445 | break; |
| 3446 | } |
| 3447 | |
| 3448 | // Try to find 1920x1080 @ 59-60 Hz |
| 3449 | if (const auto iter = std::find_if(hwcModes.begin(), hwcModes.end(), |
| 3450 | [](const auto& mode) { |
| 3451 | return mode.width == 1920 && |
| 3452 | mode.height == 1080 && |
| 3453 | mode.vsyncPeriod >= k60HzVsyncPeriod && |
| 3454 | mode.vsyncPeriod <= k59HzVsyncPeriod; |
| 3455 | }); |
| 3456 | iter != hwcModes.end()) { |
| 3457 | activeModeHwcIdOpt = iter->hwcId; |
| 3458 | break; |
| 3459 | } |
| 3460 | |
| 3461 | // The display does not support 1080p@60, and this is the last attempt to pick a display |
| 3462 | // mode. Prefer 60 Hz if available, with the closest resolution to 1080p. |
| 3463 | if (attempt + 1 == kMaxAttempts) { |
| 3464 | std::vector<HWComposer::HWCDisplayMode> hwcModeOpts; |
| 3465 | |
| 3466 | for (const auto& mode : hwcModes) { |
| 3467 | if (mode.width <= 1920 && mode.height <= 1080 && |
| 3468 | mode.vsyncPeriod >= k60HzVsyncPeriod && |
| 3469 | mode.vsyncPeriod <= k59HzVsyncPeriod) { |
| 3470 | hwcModeOpts.push_back(mode); |
| 3471 | } |
| 3472 | } |
| 3473 | |
| 3474 | if (const auto iter = std::max_element(hwcModeOpts.begin(), hwcModeOpts.end(), |
| 3475 | [](const auto& a, const auto& b) { |
| 3476 | const auto aSize = a.width * a.height; |
| 3477 | const auto bSize = b.width * b.height; |
| 3478 | if (aSize < bSize) |
| 3479 | return true; |
| 3480 | else if (aSize == bSize) |
| 3481 | return a.vsyncPeriod > b.vsyncPeriod; |
| 3482 | else |
| 3483 | return false; |
| 3484 | }); |
| 3485 | iter != hwcModeOpts.end()) { |
| 3486 | activeModeHwcIdOpt = iter->hwcId; |
| 3487 | break; |
| 3488 | } |
| 3489 | |
| 3490 | // hwcModeOpts was empty, use hwcModes[0] as the last resort |
| 3491 | activeModeHwcIdOpt = hwcModes[0].hwcId; |
| 3492 | } |
| 3493 | } |
Marin Shalamanov | d3b5c5d | 2021-02-11 18:26:14 +0100 | [diff] [blame] | 3494 | |
Dominik Laskowski | d940a01 | 2024-01-28 13:25:44 -0500 | [diff] [blame] | 3495 | const auto isActiveMode = [activeModeHwcIdOpt](const HWComposer::HWCDisplayMode& mode) { |
| 3496 | return mode.hwcId == activeModeHwcIdOpt; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3497 | }; |
| 3498 | |
| 3499 | if (std::any_of(hwcModes.begin(), hwcModes.end(), isActiveMode)) { |
| 3500 | break; |
| 3501 | } |
| 3502 | } while (++attempt < kMaxAttempts); |
| 3503 | |
Dominik Laskowski | bab5128 | 2022-08-12 09:28:55 -0700 | [diff] [blame] | 3504 | if (attempt == kMaxAttempts) { |
| 3505 | const std::string activeMode = |
Dominik Laskowski | d940a01 | 2024-01-28 13:25:44 -0500 | [diff] [blame] | 3506 | activeModeHwcIdOpt ? std::to_string(*activeModeHwcIdOpt) : "unknown"s; |
Dominik Laskowski | bab5128 | 2022-08-12 09:28:55 -0700 | [diff] [blame] | 3507 | ALOGE("HWC failed to report an active mode that is supported: activeModeHwcId=%s, " |
| 3508 | "hwcModes={%s}", |
| 3509 | activeMode.c_str(), base::Join(hwcModes, ", ").c_str()); |
| 3510 | return {}; |
| 3511 | } |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3512 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3513 | const DisplayModes oldModes = mPhysicalDisplays.get(displayId) |
| 3514 | .transform([](const PhysicalDisplay& display) { |
| 3515 | return display.snapshot().displayModes(); |
| 3516 | }) |
| 3517 | .value_or(DisplayModes{}); |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3518 | |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 3519 | DisplayModeId nextModeId = std::accumulate(oldModes.begin(), oldModes.end(), DisplayModeId(-1), |
| 3520 | [](DisplayModeId max, const auto& pair) { |
| 3521 | return std::max(max, pair.first); |
| 3522 | }); |
| 3523 | ++nextModeId; |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3524 | |
| 3525 | DisplayModes newModes; |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3526 | for (const auto& hwcMode : hwcModes) { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 3527 | const auto id = nextModeId++; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3528 | newModes.try_emplace(id, |
| 3529 | DisplayMode::Builder(hwcMode.hwcId) |
| 3530 | .setId(id) |
| 3531 | .setPhysicalDisplayId(displayId) |
| 3532 | .setResolution({hwcMode.width, hwcMode.height}) |
| 3533 | .setVsyncPeriod(hwcMode.vsyncPeriod) |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 3534 | .setVrrConfig(hwcMode.vrrConfig) |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3535 | .setDpiX(hwcMode.dpiX) |
| 3536 | .setDpiY(hwcMode.dpiY) |
| 3537 | .setGroup(hwcMode.configGroup) |
| 3538 | .build()); |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3539 | } |
| 3540 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3541 | const bool sameModes = |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3542 | std::equal(newModes.begin(), newModes.end(), oldModes.begin(), oldModes.end(), |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3543 | [](const auto& lhs, const auto& rhs) { |
| 3544 | return equalsExceptDisplayModeId(*lhs.second, *rhs.second); |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3545 | }); |
| 3546 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3547 | // Keep IDs if modes have not changed. |
| 3548 | const auto& modes = sameModes ? oldModes : newModes; |
| 3549 | const DisplayModePtr activeMode = |
Dominik Laskowski | d940a01 | 2024-01-28 13:25:44 -0500 | [diff] [blame] | 3550 | std::find_if(modes.begin(), modes.end(), [activeModeHwcIdOpt](const auto& pair) { |
| 3551 | return pair.second->getHwcId() == activeModeHwcIdOpt; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3552 | })->second; |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3553 | |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 3554 | if (isExternalDisplay) { |
| 3555 | ALOGI("External display %s initial mode: {%s}", to_string(displayId).c_str(), |
| 3556 | to_string(*activeMode).c_str()); |
| 3557 | } |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3558 | return {modes, activeMode}; |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 3559 | } |
| 3560 | |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 3561 | bool SurfaceFlinger::configureLocked() { |
| 3562 | std::vector<HotplugEvent> events; |
| 3563 | { |
| 3564 | std::lock_guard<std::mutex> lock(mHotplugMutex); |
| 3565 | events = std::move(mPendingHotplugEvents); |
| 3566 | } |
| 3567 | |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3568 | for (const auto [hwcDisplayId, connection] : events) { |
| 3569 | if (auto info = getHwComposer().onHotplug(hwcDisplayId, connection)) { |
| 3570 | const auto displayId = info->id; |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3571 | const ftl::Concat displayString("display ", displayId.value, "(HAL ID ", hwcDisplayId, |
| 3572 | ')'); |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 3573 | |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3574 | if (connection == hal::Connection::CONNECTED) { |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3575 | const auto activeModeIdOpt = |
| 3576 | processHotplugConnect(displayId, hwcDisplayId, std::move(*info), |
| 3577 | displayString.c_str()); |
| 3578 | if (!activeModeIdOpt) { |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3579 | if (FlagManager::getInstance().hotplug2()) { |
| 3580 | mScheduler->dispatchHotplugError( |
| 3581 | static_cast<int32_t>(DisplayHotplugEvent::ERROR_UNKNOWN)); |
| 3582 | } |
| 3583 | getHwComposer().disconnectDisplay(displayId); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3584 | continue; |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3585 | } |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3586 | |
| 3587 | const auto [kernelIdleTimerController, idleTimerTimeoutMs] = |
| 3588 | getKernelIdleTimerProperties(displayId); |
| 3589 | |
| 3590 | using Config = scheduler::RefreshRateSelector::Config; |
| 3591 | const Config config = |
| 3592 | {.enableFrameRateOverride = sysprop::enable_frame_rate_override(true) |
| 3593 | ? Config::FrameRateOverride::Enabled |
| 3594 | : Config::FrameRateOverride::Disabled, |
| 3595 | .frameRateMultipleThreshold = |
| 3596 | base::GetIntProperty("debug.sf.frame_rate_multiple_threshold"s, 0), |
| 3597 | .legacyIdleTimerTimeout = idleTimerTimeoutMs, |
| 3598 | .kernelIdleTimerController = kernelIdleTimerController}; |
| 3599 | |
| 3600 | const auto snapshotOpt = |
| 3601 | mPhysicalDisplays.get(displayId).transform(&PhysicalDisplay::snapshotRef); |
| 3602 | LOG_ALWAYS_FATAL_IF(!snapshotOpt); |
| 3603 | |
| 3604 | mDisplayModeController.registerDisplay(*snapshotOpt, *activeModeIdOpt, config); |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3605 | } else { |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3606 | // Unregister before destroying the DisplaySnapshot below. |
| 3607 | mDisplayModeController.unregisterDisplay(displayId); |
| 3608 | |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3609 | processHotplugDisconnect(displayId, displayString.c_str()); |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3610 | } |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 3611 | } |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 3612 | } |
| 3613 | |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 3614 | return !events.empty(); |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 3615 | } |
| 3616 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3617 | std::optional<DisplayModeId> SurfaceFlinger::processHotplugConnect(PhysicalDisplayId displayId, |
| 3618 | hal::HWDisplayId hwcDisplayId, |
| 3619 | DisplayIdentificationInfo&& info, |
| 3620 | const char* displayString) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3621 | auto [displayModes, activeMode] = loadDisplayModes(displayId); |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3622 | if (!activeMode) { |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3623 | ALOGE("Failed to hotplug %s", displayString); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3624 | return std::nullopt; |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3625 | } |
| 3626 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3627 | const DisplayModeId activeModeId = activeMode->getId(); |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 3628 | ui::ColorModes colorModes = getHwComposer().getColorModes(displayId); |
| 3629 | |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3630 | if (const auto displayOpt = mPhysicalDisplays.get(displayId)) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3631 | const auto& display = displayOpt->get(); |
| 3632 | const auto& snapshot = display.snapshot(); |
| 3633 | |
| 3634 | std::optional<DeviceProductInfo> deviceProductInfo; |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3635 | if (getHwComposer().updatesDeviceProductInfoOnHotplugReconnect()) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3636 | deviceProductInfo = std::move(info.deviceProductInfo); |
| 3637 | } else { |
| 3638 | deviceProductInfo = snapshot.deviceProductInfo(); |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3639 | } |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3640 | |
| 3641 | const auto it = |
| 3642 | mPhysicalDisplays.try_replace(displayId, display.token(), displayId, |
| 3643 | snapshot.connectionType(), std::move(displayModes), |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 3644 | std::move(colorModes), std::move(deviceProductInfo)); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3645 | |
| 3646 | auto& state = mCurrentState.displays.editValueFor(it->second.token()); |
| 3647 | state.sequenceId = DisplayDeviceState{}.sequenceId; // Generate new sequenceId. |
| 3648 | state.physical->activeMode = std::move(activeMode); |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3649 | ALOGI("Reconnecting %s", displayString); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3650 | return activeModeId; |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3651 | } |
| 3652 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3653 | const sp<IBinder> token = sp<BBinder>::make(); |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 3654 | const ui::DisplayConnectionType connectionType = |
| 3655 | getHwComposer().getDisplayConnectionType(displayId); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3656 | |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 3657 | mPhysicalDisplays.try_emplace(displayId, token, displayId, connectionType, |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 3658 | std::move(displayModes), std::move(colorModes), |
| 3659 | std::move(info.deviceProductInfo)); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3660 | |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3661 | DisplayDeviceState state; |
| 3662 | state.physical = {.id = displayId, |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3663 | .hwcDisplayId = hwcDisplayId, |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3664 | .activeMode = std::move(activeMode)}; |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 3665 | state.isSecure = connectionType == ui::DisplayConnectionType::Internal; |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 3666 | state.isProtected = true; |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3667 | state.displayName = std::move(info.name); |
| 3668 | |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3669 | mCurrentState.displays.add(token, state); |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3670 | ALOGI("Connecting %s", displayString); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3671 | return activeModeId; |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | void SurfaceFlinger::processHotplugDisconnect(PhysicalDisplayId displayId, |
| 3675 | const char* displayString) { |
| 3676 | ALOGI("Disconnecting %s", displayString); |
| 3677 | |
| 3678 | const auto displayOpt = mPhysicalDisplays.get(displayId); |
| 3679 | LOG_ALWAYS_FATAL_IF(!displayOpt); |
| 3680 | const auto& display = displayOpt->get(); |
| 3681 | |
| 3682 | if (const ssize_t index = mCurrentState.displays.indexOfKey(display.token()); index >= 0) { |
| 3683 | mCurrentState.displays.removeItemsAt(index); |
| 3684 | } |
| 3685 | |
| 3686 | mPhysicalDisplays.erase(displayId); |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3687 | } |
| 3688 | |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 3689 | void SurfaceFlinger::dispatchDisplayModeChangeEvent(PhysicalDisplayId displayId, |
| 3690 | const scheduler::FrameRateMode& mode) { |
| 3691 | // TODO(b/255635821): Merge code paths and move to Scheduler. |
| 3692 | const auto onDisplayModeChanged = displayId == mActiveDisplayId |
| 3693 | ? &scheduler::Scheduler::onPrimaryDisplayModeChanged |
| 3694 | : &scheduler::Scheduler::onNonPrimaryDisplayModeChanged; |
| 3695 | |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 3696 | ((*mScheduler).*onDisplayModeChanged)(scheduler::Cycle::Render, mode); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 3697 | } |
| 3698 | |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3699 | sp<DisplayDevice> SurfaceFlinger::setupNewDisplayDeviceInternal( |
Lloyd Pique | 9370a48 | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 3700 | const wp<IBinder>& displayToken, |
| 3701 | std::shared_ptr<compositionengine::Display> compositionDisplay, |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3702 | const DisplayDeviceState& state, |
| 3703 | const sp<compositionengine::DisplaySurface>& displaySurface, |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 3704 | const sp<IGraphicBufferProducer>& producer) { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 3705 | DisplayDeviceCreationArgs creationArgs(sp<SurfaceFlinger>::fromExisting(this), getHwComposer(), |
| 3706 | displayToken, compositionDisplay); |
Dominik Laskowski | e977409 | 2018-12-11 10:04:24 -0800 | [diff] [blame] | 3707 | creationArgs.sequenceId = state.sequenceId; |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 3708 | creationArgs.isSecure = state.isSecure; |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 3709 | creationArgs.isProtected = state.isProtected; |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3710 | creationArgs.displaySurface = displaySurface; |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 3711 | creationArgs.hasWideColorGamut = false; |
| 3712 | creationArgs.supportedPerFrameMetadata = 0; |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame] | 3713 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3714 | if (const auto physicalIdOpt = PhysicalDisplayId::tryCast(compositionDisplay->getId())) { |
| 3715 | const auto physicalId = *physicalIdOpt; |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 3716 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3717 | creationArgs.isPrimary = physicalId == getPrimaryDisplayIdLocked(); |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 3718 | creationArgs.refreshRateSelector = |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3719 | FTL_FAKE_GUARD(kMainThreadContext, |
| 3720 | mDisplayModeController.selectorPtrFor(physicalId)); |
Dominik Laskowski | 55c8540 | 2020-01-21 16:25:47 -0800 | [diff] [blame] | 3721 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3722 | mPhysicalDisplays.get(physicalId) |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 3723 | .transform(&PhysicalDisplay::snapshotRef) |
| 3724 | .transform(ftl::unit_fn([&](const display::DisplaySnapshot& snapshot) { |
| 3725 | for (const auto mode : snapshot.colorModes()) { |
| 3726 | creationArgs.hasWideColorGamut |= ui::isWideColorMode(mode); |
| 3727 | creationArgs.hwcColorModes |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3728 | .emplace(mode, getHwComposer().getRenderIntents(physicalId, mode)); |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 3729 | } |
| 3730 | })); |
tangrobin | 6753a02 | 2018-08-10 10:58:54 +0800 | [diff] [blame] | 3731 | } |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3732 | |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 3733 | if (const auto id = HalDisplayId::tryCast(compositionDisplay->getId())) { |
| 3734 | getHwComposer().getHdrCapabilities(*id, &creationArgs.hdrCapabilities); |
| 3735 | creationArgs.supportedPerFrameMetadata = getHwComposer().getSupportedPerFrameMetadata(*id); |
Chia-I Wu | 0f509fb | 2018-06-21 15:52:50 +0800 | [diff] [blame] | 3736 | } |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3737 | |
Lloyd Pique | 90c115d | 2018-09-18 21:39:42 -0700 | [diff] [blame] | 3738 | auto nativeWindowSurface = getFactory().createNativeWindowSurface(producer); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3739 | auto nativeWindow = nativeWindowSurface->getNativeWindow(); |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 3740 | creationArgs.nativeWindow = nativeWindow; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3741 | |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3742 | // Make sure that composition can never be stalled by a virtual display |
| 3743 | // consumer that isn't processing buffers fast enough. We have to do this |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 3744 | // here, in case the display is composed entirely by HWC. |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 3745 | if (state.isVirtual()) { |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3746 | nativeWindow->setSwapInterval(nativeWindow.get(), 0); |
| 3747 | } |
| 3748 | |
Dominik Laskowski | 718f960 | 2019-11-09 20:01:35 -0800 | [diff] [blame] | 3749 | creationArgs.physicalOrientation = |
ramindani | 06e518e | 2022-03-14 18:47:53 +0000 | [diff] [blame] | 3750 | getPhysicalDisplayOrientation(compositionDisplay->getId(), creationArgs.isPrimary); |
| 3751 | ALOGV("Display Orientation: %s", toCString(creationArgs.physicalOrientation)); |
Chia-I Wu | a02871c | 2018-08-27 14:38:23 -0700 | [diff] [blame] | 3752 | |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 3753 | creationArgs.initialPowerMode = state.isVirtual() ? hal::PowerMode::ON : hal::PowerMode::OFF; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3754 | |
Huihong Luo | 1768cb0 | 2022-10-11 11:10:34 -0700 | [diff] [blame] | 3755 | creationArgs.requestedRefreshRate = state.requestedRefreshRate; |
| 3756 | |
Lloyd Pique | 9370a48 | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 3757 | sp<DisplayDevice> display = getFactory().createDisplayDevice(creationArgs); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3758 | |
Ady Abraham | 8565ad2 | 2021-06-22 17:45:44 -0700 | [diff] [blame] | 3759 | nativeWindowSurface->preallocateBuffers(); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3760 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 3761 | ui::ColorMode defaultColorMode = ui::ColorMode::NATIVE; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 3762 | Dataspace defaultDataSpace = Dataspace::UNKNOWN; |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 3763 | if (display->hasWideColorGamut()) { |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 3764 | defaultColorMode = ui::ColorMode::SRGB; |
Peiyong Lin | 14724e6 | 2018-12-05 07:27:30 -0800 | [diff] [blame] | 3765 | defaultDataSpace = Dataspace::V0_SRGB; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3766 | } |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 3767 | display->getCompositionDisplay()->setColorProfile( |
| 3768 | compositionengine::Output::ColorProfile{defaultColorMode, defaultDataSpace, |
Alec Mouri | 88790f3 | 2023-07-21 01:25:14 +0000 | [diff] [blame] | 3769 | RenderIntent::COLORIMETRIC}); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3770 | |
| 3771 | if (const auto& physical = state.physical) { |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 3772 | const auto& mode = *physical->activeMode; |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 3773 | mDisplayModeController.setActiveMode(physical->id, mode.getId(), mode.getVsyncRate(), |
| 3774 | mode.getVsyncRate()); |
Lloyd Pique | 3c085a0 | 2018-05-09 19:38:32 -0700 | [diff] [blame] | 3775 | } |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 3776 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3777 | display->setLayerFilter(makeLayerFilterForDisplay(display->getId(), state.layerStack)); |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 3778 | display->setProjection(state.orientation, state.layerStackSpaceRect, |
| 3779 | state.orientedDisplaySpaceRect); |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 3780 | display->setDisplayName(state.displayName); |
Vishnu Nair | a119aaa | 2021-09-24 07:19:35 -0700 | [diff] [blame] | 3781 | display->setFlags(state.flags); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3782 | |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 3783 | return display; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3784 | } |
| 3785 | |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3786 | void SurfaceFlinger::processDisplayAdded(const wp<IBinder>& displayToken, |
| 3787 | const DisplayDeviceState& state) { |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 3788 | ui::Size resolution(0, 0); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3789 | ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(PIXEL_FORMAT_UNKNOWN); |
| 3790 | if (state.physical) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3791 | resolution = state.physical->activeMode->getResolution(); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3792 | pixelFormat = static_cast<ui::PixelFormat>(PIXEL_FORMAT_RGBA_8888); |
| 3793 | } else if (state.surface != nullptr) { |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 3794 | int status = state.surface->query(NATIVE_WINDOW_WIDTH, &resolution.width); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3795 | ALOGE_IF(status != NO_ERROR, "Unable to query width (%d)", status); |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 3796 | status = state.surface->query(NATIVE_WINDOW_HEIGHT, &resolution.height); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3797 | ALOGE_IF(status != NO_ERROR, "Unable to query height (%d)", status); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3798 | int format; |
| 3799 | status = state.surface->query(NATIVE_WINDOW_FORMAT, &format); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3800 | ALOGE_IF(status != NO_ERROR, "Unable to query format (%d)", status); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3801 | pixelFormat = static_cast<ui::PixelFormat>(format); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3802 | } else { |
| 3803 | // Virtual displays without a surface are dormant: |
| 3804 | // they have external state (layer stack, projection, |
| 3805 | // etc.) but no internal state (i.e. a DisplayDevice). |
| 3806 | return; |
| 3807 | } |
| 3808 | |
| 3809 | compositionengine::DisplayCreationArgsBuilder builder; |
| 3810 | if (const auto& physical = state.physical) { |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3811 | builder.setId(physical->id); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3812 | } else { |
Dominik Laskowski | 263eec4 | 2021-07-21 23:13:24 -0700 | [diff] [blame] | 3813 | builder.setId(acquireVirtualDisplay(resolution, pixelFormat)); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3814 | } |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3815 | |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 3816 | builder.setPixels(resolution); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3817 | builder.setIsSecure(state.isSecure); |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 3818 | builder.setIsProtected(state.isProtected); |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 3819 | builder.setPowerAdvisor(mPowerAdvisor.get()); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3820 | builder.setName(state.displayName); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3821 | auto compositionDisplay = getCompositionEngine().createDisplay(builder.build()); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3822 | compositionDisplay->setLayerCachingEnabled(mLayerCachingEnabled); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3823 | |
| 3824 | sp<compositionengine::DisplaySurface> displaySurface; |
| 3825 | sp<IGraphicBufferProducer> producer; |
| 3826 | sp<IGraphicBufferProducer> bqProducer; |
| 3827 | sp<IGraphicBufferConsumer> bqConsumer; |
| 3828 | getFactory().createBufferQueue(&bqProducer, &bqConsumer, /*consumerIsSurfaceFlinger =*/false); |
| 3829 | |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3830 | if (state.isVirtual()) { |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3831 | const auto displayId = VirtualDisplayId::tryCast(compositionDisplay->getId()); |
| 3832 | LOG_FATAL_IF(!displayId); |
| 3833 | auto surface = sp<VirtualDisplaySurface>::make(getHwComposer(), *displayId, state.surface, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3834 | bqProducer, bqConsumer, state.displayName); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3835 | displaySurface = surface; |
| 3836 | producer = std::move(surface); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3837 | } else { |
| 3838 | ALOGE_IF(state.surface != nullptr, |
| 3839 | "adding a supported display, but rendering " |
| 3840 | "surface is provided (%p), ignoring it", |
| 3841 | state.surface.get()); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3842 | const auto displayId = PhysicalDisplayId::tryCast(compositionDisplay->getId()); |
| 3843 | LOG_FATAL_IF(!displayId); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3844 | displaySurface = |
| 3845 | sp<FramebufferSurface>::make(getHwComposer(), *displayId, bqConsumer, |
| 3846 | state.physical->activeMode->getResolution(), |
| 3847 | ui::Size(maxGraphicsWidth, maxGraphicsHeight)); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3848 | producer = bqProducer; |
| 3849 | } |
| 3850 | |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3851 | LOG_FATAL_IF(!displaySurface); |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 3852 | auto display = setupNewDisplayDeviceInternal(displayToken, std::move(compositionDisplay), state, |
| 3853 | displaySurface, producer); |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 3854 | |
| 3855 | if (mScheduler && !display->isVirtual()) { |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 3856 | // TODO(b/241285876): Annotate `processDisplayAdded` instead. |
| 3857 | ftl::FakeGuard guard(kMainThreadContext); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 3858 | |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 3859 | // For hotplug reconnect, renew the registration since display modes have been reloaded. |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 3860 | mScheduler->registerDisplay(display->getPhysicalId(), display->holdRefreshRateSelector(), |
| 3861 | mActiveDisplayId); |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3862 | } |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 3863 | |
Huihong Luo | 1768cb0 | 2022-10-11 11:10:34 -0700 | [diff] [blame] | 3864 | if (display->isVirtual()) { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 3865 | display->adjustRefreshRate(mScheduler->getPacesetterRefreshRate()); |
Huihong Luo | 1768cb0 | 2022-10-11 11:10:34 -0700 | [diff] [blame] | 3866 | } |
| 3867 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 3868 | mDisplays.try_emplace(displayToken, std::move(display)); |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 3869 | |
| 3870 | // For an external display, loadDisplayModes already attempted to select the same mode |
| 3871 | // as DM, but SF still needs to be updated to match. |
| 3872 | // TODO (b/318534874): Let DM decide the initial mode. |
| 3873 | if (const auto& physical = state.physical; |
| 3874 | mScheduler && physical && FlagManager::getInstance().connected_display()) { |
| 3875 | const bool isInternalDisplay = mPhysicalDisplays.get(physical->id) |
| 3876 | .transform(&PhysicalDisplay::isInternal) |
| 3877 | .value_or(false); |
| 3878 | |
| 3879 | if (!isInternalDisplay) { |
| 3880 | auto activeModePtr = physical->activeMode; |
| 3881 | const auto fps = activeModePtr->getPeakFps(); |
| 3882 | |
| 3883 | setDesiredMode( |
| 3884 | {.mode = scheduler::FrameRateMode{fps, |
| 3885 | ftl::as_non_null(std::move(activeModePtr))}, |
| 3886 | .emitEvent = false, |
| 3887 | .force = true}); |
| 3888 | } |
| 3889 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | void SurfaceFlinger::processDisplayRemoved(const wp<IBinder>& displayToken) { |
Vishnu Nair | f78589c | 2020-12-02 18:36:03 -0800 | [diff] [blame] | 3893 | auto display = getDisplayDeviceLocked(displayToken); |
| 3894 | if (display) { |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3895 | display->disconnect(); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3896 | |
| 3897 | if (display->isVirtual()) { |
| 3898 | releaseVirtualDisplay(display->getVirtualId()); |
| 3899 | } else { |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 3900 | mScheduler->unregisterDisplay(display->getPhysicalId(), mActiveDisplayId); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | mDisplays.erase(displayToken); |
Vishnu Nair | f78589c | 2020-12-02 18:36:03 -0800 | [diff] [blame] | 3905 | |
| 3906 | if (display && display->isVirtual()) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 3907 | static_cast<void>(mScheduler->schedule([display = std::move(display)] { |
Vishnu Nair | f78589c | 2020-12-02 18:36:03 -0800 | [diff] [blame] | 3908 | // Destroy the display without holding the mStateLock. |
| 3909 | // This is a temporary solution until we can manage transaction queues without |
| 3910 | // holding the mStateLock. |
| 3911 | // With blast, the IGBP that is passed to the VirtualDisplaySurface is owned by the |
| 3912 | // client. When the IGBP is disconnected, its buffer cache in SF will be cleared |
| 3913 | // via SurfaceComposerClient::doUncacheBufferTransaction. This call from the client |
| 3914 | // ends up running on the main thread causing a deadlock since setTransactionstate |
| 3915 | // will try to acquire the mStateLock. Instead we extend the lifetime of |
| 3916 | // DisplayDevice and destroy it in the main thread without holding the mStateLock. |
| 3917 | // The display will be disconnected and removed from the mDisplays list so it will |
| 3918 | // not be accessible. |
| 3919 | })); |
| 3920 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | void SurfaceFlinger::processDisplayChanged(const wp<IBinder>& displayToken, |
| 3924 | const DisplayDeviceState& currentState, |
| 3925 | const DisplayDeviceState& drawingState) { |
| 3926 | const sp<IBinder> currentBinder = IInterface::asBinder(currentState.surface); |
| 3927 | const sp<IBinder> drawingBinder = IInterface::asBinder(drawingState.surface); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3928 | |
| 3929 | // Recreate the DisplayDevice if the surface or sequence ID changed. |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3930 | if (currentBinder != drawingBinder || currentState.sequenceId != drawingState.sequenceId) { |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3931 | if (const auto display = getDisplayDeviceLocked(displayToken)) { |
| 3932 | display->disconnect(); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3933 | if (display->isVirtual()) { |
| 3934 | releaseVirtualDisplay(display->getVirtualId()); |
| 3935 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3936 | } |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3937 | |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3938 | mDisplays.erase(displayToken); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3939 | |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3940 | if (const auto& physical = currentState.physical) { |
| 3941 | getHwComposer().allocatePhysicalDisplay(physical->hwcDisplayId, physical->id); |
| 3942 | } |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3943 | |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3944 | processDisplayAdded(displayToken, currentState); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3945 | |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3946 | if (currentState.physical) { |
Marin Shalamanov | 4c5e301 | 2020-06-04 21:41:42 +0200 | [diff] [blame] | 3947 | const auto display = getDisplayDeviceLocked(displayToken); |
Robert Horvath | dce110a | 2022-09-16 11:08:47 +0200 | [diff] [blame] | 3948 | if (!mSkipPowerOnForQuiescent) { |
| 3949 | setPowerModeInternal(display, hal::PowerMode::ON); |
| 3950 | } |
Marin Shalamanov | f7f6b3c | 2020-12-09 13:19:38 +0100 | [diff] [blame] | 3951 | |
| 3952 | // TODO(b/175678251) Call a listener instead. |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 3953 | if (currentState.physical->hwcDisplayId == getHwComposer().getPrimaryHwcDisplayId()) { |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 3954 | const Fps refreshRate = |
| 3955 | mDisplayModeController.getActiveMode(display->getPhysicalId()).fps; |
| 3956 | mScheduler->resetPhaseConfiguration(refreshRate); |
Marin Shalamanov | f7f6b3c | 2020-12-09 13:19:38 +0100 | [diff] [blame] | 3957 | } |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3958 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3959 | return; |
| 3960 | } |
| 3961 | |
| 3962 | if (const auto display = getDisplayDeviceLocked(displayToken)) { |
| 3963 | if (currentState.layerStack != drawingState.layerStack) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3964 | display->setLayerFilter( |
| 3965 | makeLayerFilterForDisplay(display->getId(), currentState.layerStack)); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3966 | } |
Evan Rosky | 2239b37 | 2021-05-20 13:43:47 -0700 | [diff] [blame] | 3967 | if (currentState.flags != drawingState.flags) { |
| 3968 | display->setFlags(currentState.flags); |
| 3969 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3970 | if ((currentState.orientation != drawingState.orientation) || |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 3971 | (currentState.layerStackSpaceRect != drawingState.layerStackSpaceRect) || |
| 3972 | (currentState.orientedDisplaySpaceRect != drawingState.orientedDisplaySpaceRect)) { |
| 3973 | display->setProjection(currentState.orientation, currentState.layerStackSpaceRect, |
| 3974 | currentState.orientedDisplaySpaceRect); |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 3975 | if (display->getId() == mActiveDisplayId) { |
Ady Abraham | 1273ac1 | 2021-08-26 17:31:53 -0700 | [diff] [blame] | 3976 | mActiveDisplayTransformHint = display->getTransformHint(); |
Leon Scroggins III | 85d4b22 | 2023-05-09 13:58:18 -0400 | [diff] [blame] | 3977 | sActiveDisplayRotationFlags = |
| 3978 | ui::Transform::toRotationFlags(display->getOrientation()); |
Arthur Hung | b6aa9a0 | 2021-06-09 14:23:01 +0800 | [diff] [blame] | 3979 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3980 | } |
| 3981 | if (currentState.width != drawingState.width || |
| 3982 | currentState.height != drawingState.height) { |
| 3983 | display->setDisplaySize(currentState.width, currentState.height); |
Dominik Laskowski | 2013464 | 2020-04-20 22:36:44 -0700 | [diff] [blame] | 3984 | |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 3985 | if (display->getId() == mActiveDisplayId) { |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 3986 | onActiveDisplaySizeChanged(*display); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3987 | } |
| 3988 | } |
| 3989 | } |
| 3990 | } |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 3991 | |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 3992 | void SurfaceFlinger::processDisplayChangesLocked() { |
| 3993 | // here we take advantage of Vector's copy-on-write semantics to |
| 3994 | // improve performance by skipping the transaction entirely when |
| 3995 | // know that the lists are identical |
| 3996 | const KeyedVector<wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays); |
| 3997 | const KeyedVector<wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays); |
| 3998 | if (!curr.isIdenticalTo(draw)) { |
| 3999 | mVisibleRegionsDirty = true; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 4000 | mUpdateInputInfo = true; |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 4001 | |
Dominik Laskowski | 01f191c | 2024-01-29 11:51:23 -0500 | [diff] [blame] | 4002 | // Apply the current color matrix to any added or changed display. |
| 4003 | mCurrentState.colorMatrixChanged = true; |
| 4004 | |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 4005 | // find the displays that were removed |
| 4006 | // (ie: in drawing state but not in current state) |
| 4007 | // also handle displays that changed |
| 4008 | // (ie: displays that are in both lists) |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 4009 | for (size_t i = 0; i < draw.size(); i++) { |
| 4010 | const wp<IBinder>& displayToken = draw.keyAt(i); |
| 4011 | const ssize_t j = curr.indexOfKey(displayToken); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 4012 | if (j < 0) { |
| 4013 | // in drawing state but not in current state |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 4014 | processDisplayRemoved(displayToken); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 4015 | } else { |
| 4016 | // this display is in both lists. see if something changed. |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 4017 | const DisplayDeviceState& currentState = curr[j]; |
| 4018 | const DisplayDeviceState& drawingState = draw[i]; |
| 4019 | processDisplayChanged(displayToken, currentState, drawingState); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 4020 | } |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 4021 | } |
| 4022 | |
| 4023 | // find displays that were added |
| 4024 | // (ie: in current state but not in drawing state) |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 4025 | for (size_t i = 0; i < curr.size(); i++) { |
| 4026 | const wp<IBinder>& displayToken = curr.keyAt(i); |
| 4027 | if (draw.indexOfKey(displayToken) < 0) { |
| 4028 | processDisplayAdded(displayToken, curr[i]); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | } |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 4032 | |
| 4033 | mDrawingState.displays = mCurrentState.displays; |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 4034 | } |
| 4035 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4036 | void SurfaceFlinger::commitTransactionsLocked(uint32_t transactionFlags) { |
| 4037 | // Commit display transactions. |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 4038 | const bool displayTransactionNeeded = transactionFlags & eDisplayTransactionNeeded; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4039 | mFrontEndDisplayInfosChanged = displayTransactionNeeded; |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 4040 | if (displayTransactionNeeded && !mLayerLifecycleManagerEnabled) { |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 4041 | processDisplayChangesLocked(); |
Vishnu Nair | 67b431c | 2022-11-16 01:54:05 +0000 | [diff] [blame] | 4042 | mFrontEndDisplayInfos.clear(); |
| 4043 | for (const auto& [_, display] : mDisplays) { |
| 4044 | mFrontEndDisplayInfos.try_emplace(display->getLayerStack(), display->getFrontEndInfo()); |
| 4045 | } |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 4046 | } |
Robert Carr | b552ff5 | 2021-06-11 13:35:54 -0700 | [diff] [blame] | 4047 | mForceTransactionDisplayChange = displayTransactionNeeded; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4048 | |
Robert Carr | e450fb5 | 2021-06-11 13:21:09 -0700 | [diff] [blame] | 4049 | if (mSomeChildrenChanged) { |
| 4050 | mVisibleRegionsDirty = true; |
| 4051 | mSomeChildrenChanged = false; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 4052 | mUpdateInputInfo = true; |
Robert Carr | e450fb5 | 2021-06-11 13:21:09 -0700 | [diff] [blame] | 4053 | } |
| 4054 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4055 | // Update transform hint. |
Vishnu Nair | 6213bd9 | 2020-05-08 17:42:25 -0700 | [diff] [blame] | 4056 | if (transactionFlags & (eTransformHintUpdateNeeded | eDisplayTransactionNeeded)) { |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 4057 | // Layers and/or displays have changed, so update the transform hint for each layer. |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 4058 | // |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 4059 | // NOTE: we do this here, rather than when presenting the display so that |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 4060 | // the hint is set before we acquire a buffer from the surface texture. |
| 4061 | // |
| 4062 | // NOTE: layer transactions have taken place already, so we use their |
| 4063 | // drawing state. However, SurfaceFlinger's own transaction has not |
| 4064 | // happened yet, so we must use the current state layer list |
| 4065 | // (soon to become the drawing state list). |
| 4066 | // |
Dominik Laskowski | 9fae102 | 2018-05-29 13:17:40 -0700 | [diff] [blame] | 4067 | sp<const DisplayDevice> hintDisplay; |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 4068 | ui::LayerStack layerStack; |
| 4069 | |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 4070 | mCurrentState.traverse([&](Layer* layer) REQUIRES(mStateLock) { |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 4071 | // NOTE: we rely on the fact that layers are sorted by |
| 4072 | // layerStack first (so we don't have to traverse the list |
| 4073 | // of displays for every layer). |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 4074 | if (const auto filter = layer->getOutputFilter(); layerStack != filter.layerStack) { |
| 4075 | layerStack = filter.layerStack; |
Dominik Laskowski | 9fae102 | 2018-05-29 13:17:40 -0700 | [diff] [blame] | 4076 | hintDisplay = nullptr; |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 4077 | |
| 4078 | // Find the display that includes the layer. |
Dominik Laskowski | 9fae102 | 2018-05-29 13:17:40 -0700 | [diff] [blame] | 4079 | for (const auto& [token, display] : mDisplays) { |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 4080 | if (!display->getCompositionDisplay()->includesLayer(filter)) { |
| 4081 | continue; |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 4082 | } |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 4083 | |
| 4084 | // Pick the primary display if another display mirrors the layer. |
| 4085 | if (hintDisplay) { |
| 4086 | hintDisplay = nullptr; |
| 4087 | break; |
| 4088 | } |
| 4089 | |
| 4090 | hintDisplay = display; |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 4091 | } |
| 4092 | } |
Chet Haase | 91d2593 | 2013-04-11 15:24:55 -0700 | [diff] [blame] | 4093 | |
Melody Hsu | 9af3070 | 2024-04-17 18:23:43 +0000 | [diff] [blame] | 4094 | if (hintDisplay) { |
Vishnu Nair | 71fcf91 | 2022-10-18 09:14:20 -0700 | [diff] [blame] | 4095 | layer->updateTransformHint(hintDisplay->getTransformHint()); |
Vishnu Nair | e3066de | 2022-06-08 12:16:25 -0700 | [diff] [blame] | 4096 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4097 | }); |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 4098 | } |
| 4099 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 4100 | if (mLayersAdded) { |
| 4101 | mLayersAdded = false; |
| 4102 | // Layers have been added. |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 4103 | mVisibleRegionsDirty = true; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 4104 | mUpdateInputInfo = true; |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | // some layers might have been removed, so |
| 4108 | // we need to update the regions they're exposing. |
| 4109 | if (mLayersRemoved) { |
| 4110 | mLayersRemoved = false; |
| 4111 | mVisibleRegionsDirty = true; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 4112 | mUpdateInputInfo = true; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4113 | mDrawingState.traverseInZOrder([&](Layer* layer) { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 4114 | if (mLayersPendingRemoval.indexOf(sp<Layer>::fromExisting(layer)) >= 0) { |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 4115 | // this layer is not visible anymore |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 4116 | Region visibleReg; |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 4117 | visibleReg.set(layer->getScreenBounds()); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4118 | invalidateLayerStack(layer->getOutputFilter(), visibleReg); |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 4119 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4120 | }); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4121 | } |
| 4122 | |
Patrick Williams | af06b79 | 2023-02-16 17:46:22 -0600 | [diff] [blame] | 4123 | if (transactionFlags & eInputInfoUpdateNeeded) { |
| 4124 | mUpdateInputInfo = true; |
| 4125 | } |
| 4126 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4127 | doCommitTransactions(); |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 4128 | } |
| 4129 | |
Patrick Williams | 6681c02 | 2023-05-15 13:38:16 -0500 | [diff] [blame] | 4130 | void SurfaceFlinger::updateInputFlinger(VsyncId vsyncId, TimePoint frameTime) { |
Patrick Williams | d86bd8c | 2023-04-21 11:54:11 -0500 | [diff] [blame] | 4131 | if (!mInputFlinger || (!mUpdateInputInfo && mInputWindowCommands.empty())) { |
Vishnu Nair | 42a27b5 | 2021-11-18 15:35:22 -0800 | [diff] [blame] | 4132 | return; |
| 4133 | } |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4134 | SFTRACE_CALL(); |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 4135 | |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 4136 | std::vector<WindowInfo> windowInfos; |
| 4137 | std::vector<DisplayInfo> displayInfos; |
| 4138 | bool updateWindowInfo = false; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 4139 | if (mUpdateInputInfo) { |
| 4140 | mUpdateInputInfo = false; |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 4141 | updateWindowInfo = true; |
| 4142 | buildWindowInfos(windowInfos, displayInfos); |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 4143 | } |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 4144 | |
Patrick Williams | d86bd8c | 2023-04-21 11:54:11 -0500 | [diff] [blame] | 4145 | std::unordered_set<int32_t> visibleWindowIds; |
| 4146 | for (WindowInfo& windowInfo : windowInfos) { |
| 4147 | if (!windowInfo.inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
| 4148 | visibleWindowIds.insert(windowInfo.id); |
Patrick Williams | 1caf3b7 | 2023-03-31 10:59:10 -0500 | [diff] [blame] | 4149 | } |
Patrick Williams | d86bd8c | 2023-04-21 11:54:11 -0500 | [diff] [blame] | 4150 | } |
| 4151 | bool visibleWindowsChanged = false; |
| 4152 | if (visibleWindowIds != mVisibleWindowIds) { |
| 4153 | visibleWindowsChanged = true; |
| 4154 | mVisibleWindowIds = std::move(visibleWindowIds); |
Patrick Williams | 1caf3b7 | 2023-03-31 10:59:10 -0500 | [diff] [blame] | 4155 | } |
| 4156 | |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 4157 | BackgroundExecutor::getInstance().sendCallbacks({[updateWindowInfo, |
| 4158 | windowInfos = std::move(windowInfos), |
| 4159 | displayInfos = std::move(displayInfos), |
| 4160 | inputWindowCommands = |
| 4161 | std::move(mInputWindowCommands), |
Patrick Williams | 1caf3b7 | 2023-03-31 10:59:10 -0500 | [diff] [blame] | 4162 | inputFlinger = mInputFlinger, this, |
Patrick Williams | 6681c02 | 2023-05-15 13:38:16 -0500 | [diff] [blame] | 4163 | visibleWindowsChanged, vsyncId, frameTime]() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4164 | SFTRACE_NAME("BackgroundExecutor::updateInputFlinger"); |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 4165 | if (updateWindowInfo) { |
Patrick Williams | 641f7f2 | 2022-06-22 19:25:35 +0000 | [diff] [blame] | 4166 | mWindowInfosListenerInvoker |
Patrick Williams | 6681c02 | 2023-05-15 13:38:16 -0500 | [diff] [blame] | 4167 | ->windowInfosChanged(gui::WindowInfosUpdate{std::move(windowInfos), |
| 4168 | std::move(displayInfos), |
| 4169 | ftl::to_underlying(vsyncId), |
| 4170 | frameTime.ns()}, |
Patrick Williams | 1caf3b7 | 2023-03-31 10:59:10 -0500 | [diff] [blame] | 4171 | std::move( |
| 4172 | inputWindowCommands.windowInfosReportedListeners), |
Patrick Williams | d86bd8c | 2023-04-21 11:54:11 -0500 | [diff] [blame] | 4173 | /* forceImmediateCall= */ visibleWindowsChanged || |
Patrick Williams | 6681c02 | 2023-05-15 13:38:16 -0500 | [diff] [blame] | 4174 | !inputWindowCommands.focusRequests.empty()); |
Patrick Williams | 641f7f2 | 2022-06-22 19:25:35 +0000 | [diff] [blame] | 4175 | } else { |
| 4176 | // If there are listeners but no changes to input windows, call the listeners |
| 4177 | // immediately. |
| 4178 | for (const auto& listener : inputWindowCommands.windowInfosReportedListeners) { |
| 4179 | if (IInterface::asBinder(listener)->isBinderAlive()) { |
| 4180 | listener->onWindowInfosReported(); |
| 4181 | } |
| 4182 | } |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 4183 | } |
| 4184 | for (const auto& focusRequest : inputWindowCommands.focusRequests) { |
| 4185 | inputFlinger->setFocusedWindow(focusRequest); |
| 4186 | } |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 4187 | }}); |
Jaineel Mehta | ac331c5 | 2021-11-29 21:38:10 +0000 | [diff] [blame] | 4188 | |
Arthur Hung | 6cbb975 | 2019-09-05 16:38:18 +0800 | [diff] [blame] | 4189 | mInputWindowCommands.clear(); |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 4190 | } |
| 4191 | |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 4192 | void SurfaceFlinger::persistDisplayBrightness(bool needsComposite) { |
| 4193 | const bool supportsDisplayBrightnessCommand = getHwComposer().getComposer()->isSupported( |
| 4194 | Hwc2::Composer::OptionalFeature::DisplayBrightnessCommand); |
| 4195 | if (!supportsDisplayBrightnessCommand) { |
| 4196 | return; |
| 4197 | } |
| 4198 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 4199 | for (const auto& [_, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 4200 | if (const auto brightness = display->getStagedBrightness(); brightness) { |
| 4201 | if (!needsComposite) { |
| 4202 | const status_t error = |
| 4203 | getHwComposer() |
| 4204 | .setDisplayBrightness(display->getPhysicalId(), *brightness, |
Alec Mouri | 4d8a05d | 2022-03-23 18:14:26 +0000 | [diff] [blame] | 4205 | display->getCompositionDisplay() |
| 4206 | ->getState() |
| 4207 | .displayBrightnessNits, |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 4208 | Hwc2::Composer::DisplayBrightnessOptions{ |
| 4209 | .applyImmediately = true}) |
| 4210 | .get(); |
| 4211 | |
| 4212 | ALOGE_IF(error != NO_ERROR, |
| 4213 | "Error setting display brightness for display %s: %d (%s)", |
Dominik Laskowski | 5d7de5f | 2022-11-03 12:38:32 -0400 | [diff] [blame] | 4214 | to_string(display->getId()).c_str(), error, strerror(error)); |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 4215 | } |
| 4216 | display->persistBrightness(needsComposite); |
| 4217 | } |
| 4218 | } |
| 4219 | } |
| 4220 | |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 4221 | void SurfaceFlinger::buildWindowInfos(std::vector<WindowInfo>& outWindowInfos, |
| 4222 | std::vector<DisplayInfo>& outDisplayInfos) { |
Dominik Laskowski | 9f410f0 | 2022-01-08 16:22:46 -0800 | [diff] [blame] | 4223 | static size_t sNumWindowInfos = 0; |
| 4224 | outWindowInfos.reserve(sNumWindowInfos); |
| 4225 | sNumWindowInfos = 0; |
| 4226 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 4227 | if (mLayerLifecycleManagerEnabled) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4228 | mLayerSnapshotBuilder.forEachInputSnapshot( |
| 4229 | [&outWindowInfos](const frontend::LayerSnapshot& snapshot) { |
| 4230 | outWindowInfos.push_back(snapshot.inputInfo); |
| 4231 | }); |
| 4232 | } else { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4233 | mDrawingState.traverseInReverseZOrder([&](Layer* layer) FTL_FAKE_GUARD(kMainThreadContext) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4234 | if (!layer->needsInputInfo()) return; |
| 4235 | const auto opt = |
| 4236 | mFrontEndDisplayInfos.get(layer->getLayerStack()) |
| 4237 | .transform([](const frontend::DisplayInfo& info) { |
| 4238 | return Layer::InputDisplayArgs{&info.transform, info.isSecure}; |
| 4239 | }); |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 4240 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4241 | outWindowInfos.push_back(layer->fillInputInfo(opt.value_or(Layer::InputDisplayArgs{}))); |
| 4242 | }); |
| 4243 | } |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 4244 | |
Dominik Laskowski | 9f410f0 | 2022-01-08 16:22:46 -0800 | [diff] [blame] | 4245 | sNumWindowInfos = outWindowInfos.size(); |
| 4246 | |
Vishnu Nair | 67b431c | 2022-11-16 01:54:05 +0000 | [diff] [blame] | 4247 | outDisplayInfos.reserve(mFrontEndDisplayInfos.size()); |
| 4248 | for (const auto& [_, info] : mFrontEndDisplayInfos) { |
Dominik Laskowski | 9f410f0 | 2022-01-08 16:22:46 -0800 | [diff] [blame] | 4249 | outDisplayInfos.push_back(info.info); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 4250 | } |
Robert Carr | 720e506 | 2018-07-30 17:45:14 -0700 | [diff] [blame] | 4251 | } |
| 4252 | |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 4253 | void SurfaceFlinger::updateCursorAsync() { |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 4254 | compositionengine::CompositionRefreshArgs refreshArgs; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 4255 | for (const auto& [_, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 4256 | if (HalDisplayId::tryCast(display->getId())) { |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 4257 | refreshArgs.outputs.push_back(display->getCompositionDisplay()); |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 4258 | } |
| 4259 | } |
Dominik Laskowski | 16a345b | 2023-04-27 17:07:33 -0400 | [diff] [blame] | 4260 | |
| 4261 | constexpr bool kCursorOnly = true; |
| 4262 | const auto layers = moveSnapshotsToCompositionArgs(refreshArgs, kCursorOnly); |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 4263 | mCompositionEngine->updateCursorAsync(refreshArgs); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4264 | moveSnapshotsFromCompositionArgs(refreshArgs, layers); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 4265 | } |
| 4266 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 4267 | void SurfaceFlinger::requestHardwareVsync(PhysicalDisplayId displayId, bool enable) { |
| 4268 | getHwComposer().setVsyncEnabled(displayId, enable ? hal::Vsync::ENABLE : hal::Vsync::DISABLE); |
| 4269 | } |
| 4270 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4271 | void SurfaceFlinger::requestDisplayModes(std::vector<display::DisplayModeRequest> modeRequests) { |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 4272 | if (mBootStage != BootStage::FINISHED) { |
| 4273 | ALOGV("Currently in the boot stage, skipping display mode changes"); |
| 4274 | return; |
| 4275 | } |
| 4276 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4277 | SFTRACE_CALL(); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4278 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 4279 | // If this is called from the main thread mStateLock must be locked before |
| 4280 | // Currently the only way to call this function from the main thread is from |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 4281 | // Scheduler::chooseRefreshRateForContent |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 4282 | |
| 4283 | ConditionalLock lock(mStateLock, std::this_thread::get_id() != mMainThreadId); |
Marin Shalamanov | a63f9ae | 2021-11-03 15:05:16 +0100 | [diff] [blame] | 4284 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4285 | for (auto& request : modeRequests) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 4286 | const auto& modePtr = request.mode.modePtr; |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 4287 | |
| 4288 | const auto displayId = modePtr->getPhysicalDisplayId(); |
| 4289 | const auto display = getDisplayDeviceLocked(displayId); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4290 | |
| 4291 | if (!display) continue; |
| 4292 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 4293 | if (display->refreshRateSelector().isModeAllowed(request.mode)) { |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 4294 | setDesiredMode(std::move(request)); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4295 | } else { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 4296 | ALOGV("%s: Mode %d is disallowed for display %s", __func__, |
| 4297 | ftl::to_underlying(modePtr->getId()), to_string(displayId).c_str()); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4298 | } |
| 4299 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 4300 | } |
| 4301 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 4302 | void SurfaceFlinger::triggerOnFrameRateOverridesChanged() { |
| 4303 | PhysicalDisplayId displayId = [&]() { |
| 4304 | ConditionalLock lock(mStateLock, std::this_thread::get_id() != mMainThreadId); |
| 4305 | return getDefaultDisplayDeviceLocked()->getPhysicalId(); |
| 4306 | }(); |
| 4307 | |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 4308 | mScheduler->onFrameRateOverridesChanged(scheduler::Cycle::Render, displayId); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 4309 | } |
| 4310 | |
jimmyshiu | 4e21177 | 2023-06-15 15:18:38 +0000 | [diff] [blame] | 4311 | void SurfaceFlinger::notifyCpuLoadUp() { |
| 4312 | mPowerAdvisor->notifyCpuLoadUp(); |
| 4313 | } |
| 4314 | |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 4315 | void SurfaceFlinger::onChoreographerAttached() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4316 | SFTRACE_CALL(); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 4317 | if (mLayerLifecycleManagerEnabled) { |
| 4318 | mUpdateAttachedChoreographer = true; |
| 4319 | scheduleCommit(FrameHint::kNone); |
| 4320 | } |
| 4321 | } |
| 4322 | |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4323 | void SurfaceFlinger::onExpectedPresentTimePosted(TimePoint expectedPresentTime, |
| 4324 | ftl::NonNull<DisplayModePtr> modePtr, |
| 4325 | Fps renderRate) { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 4326 | const auto vsyncPeriod = modePtr->getVsyncRate().getPeriod(); |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4327 | const auto timeoutOpt = [&]() -> std::optional<Period> { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 4328 | const auto vrrConfig = modePtr->getVrrConfig(); |
| 4329 | if (!vrrConfig) return std::nullopt; |
| 4330 | |
| 4331 | const auto notifyExpectedPresentConfig = |
| 4332 | modePtr->getVrrConfig()->notifyExpectedPresentConfig; |
| 4333 | if (!notifyExpectedPresentConfig) return std::nullopt; |
ramindani | cbd7a6d | 2023-12-19 16:00:30 -0800 | [diff] [blame] | 4334 | return Period::fromNs(notifyExpectedPresentConfig->timeoutNs); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 4335 | }(); |
| 4336 | |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4337 | notifyExpectedPresentIfRequired(modePtr->getPhysicalDisplayId(), vsyncPeriod, |
| 4338 | expectedPresentTime, renderRate, timeoutOpt); |
| 4339 | } |
| 4340 | |
| 4341 | void SurfaceFlinger::notifyExpectedPresentIfRequired(PhysicalDisplayId displayId, |
| 4342 | Period vsyncPeriod, |
| 4343 | TimePoint expectedPresentTime, |
| 4344 | Fps frameInterval, |
| 4345 | std::optional<Period> timeoutOpt) { |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4346 | auto& data = mNotifyExpectedPresentMap[displayId]; |
| 4347 | const auto lastExpectedPresentTimestamp = data.lastExpectedPresentTimestamp; |
| 4348 | const auto lastFrameInterval = data.lastFrameInterval; |
| 4349 | data.lastFrameInterval = frameInterval; |
| 4350 | data.lastExpectedPresentTimestamp = expectedPresentTime; |
| 4351 | const auto threshold = Duration::fromNs(vsyncPeriod.ns() / 2); |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4352 | |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4353 | const constexpr nsecs_t kOneSecondNs = |
| 4354 | std::chrono::duration_cast<std::chrono::nanoseconds>(1s).count(); |
| 4355 | const auto timeout = |
| 4356 | Period::fromNs(timeoutOpt && timeoutOpt->ns() > 0 ? timeoutOpt->ns() : kOneSecondNs); |
| 4357 | const bool frameIntervalIsOnCadence = |
| 4358 | isFrameIntervalOnCadence(expectedPresentTime, lastExpectedPresentTimestamp, |
| 4359 | lastFrameInterval, timeout, threshold); |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4360 | |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4361 | const bool expectedPresentWithinTimeout = |
| 4362 | isExpectedPresentWithinTimeout(expectedPresentTime, lastExpectedPresentTimestamp, |
| 4363 | timeoutOpt, threshold); |
| 4364 | if (expectedPresentWithinTimeout && frameIntervalIsOnCadence) { |
| 4365 | return; |
| 4366 | } |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4367 | |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4368 | auto hintStatus = data.hintStatus.load(); |
| 4369 | if (!expectedPresentWithinTimeout) { |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4370 | if ((hintStatus != NotifyExpectedPresentHintStatus::Sent && |
| 4371 | hintStatus != NotifyExpectedPresentHintStatus::ScheduleOnTx) || |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4372 | (timeoutOpt && timeoutOpt->ns() == 0)) { |
| 4373 | // Send the hint immediately if timeout, as the hint gets |
| 4374 | // delayed otherwise, as the frame is scheduled close |
| 4375 | // to the actual present. |
| 4376 | if (data.hintStatus |
| 4377 | .compare_exchange_strong(hintStatus, |
| 4378 | NotifyExpectedPresentHintStatus::ScheduleOnTx)) { |
| 4379 | scheduleNotifyExpectedPresentHint(displayId); |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4380 | return; |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4381 | } |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4382 | } |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4383 | } |
| 4384 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4385 | if (hintStatus == NotifyExpectedPresentHintStatus::Sent && |
| 4386 | data.hintStatus.compare_exchange_strong(hintStatus, |
| 4387 | NotifyExpectedPresentHintStatus::ScheduleOnTx)) { |
| 4388 | return; |
| 4389 | } |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4390 | if (hintStatus != NotifyExpectedPresentHintStatus::Start) { |
| 4391 | return; |
| 4392 | } |
| 4393 | data.hintStatus.store(NotifyExpectedPresentHintStatus::ScheduleOnPresent); |
| 4394 | mScheduler->scheduleFrame(); |
| 4395 | } |
| 4396 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4397 | void SurfaceFlinger::scheduleNotifyExpectedPresentHint(PhysicalDisplayId displayId, |
| 4398 | VsyncId vsyncId) { |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4399 | auto itr = mNotifyExpectedPresentMap.find(displayId); |
| 4400 | if (itr == mNotifyExpectedPresentMap.end()) { |
| 4401 | return; |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 4402 | } |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4403 | |
| 4404 | const char* const whence = __func__; |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4405 | const auto sendHint = [=, this]() { |
| 4406 | auto& data = mNotifyExpectedPresentMap.at(displayId); |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4407 | TimePoint expectedPresentTime = data.lastExpectedPresentTimestamp; |
| 4408 | if (ftl::to_underlying(vsyncId) != FrameTimelineInfo::INVALID_VSYNC_ID) { |
| 4409 | const auto predictionOpt = mFrameTimeline->getTokenManager()->getPredictionsForToken( |
| 4410 | ftl::to_underlying(vsyncId)); |
| 4411 | const auto expectedPresentTimeOnPredictor = TimePoint::fromNs( |
| 4412 | predictionOpt ? predictionOpt->presentTime : expectedPresentTime.ns()); |
| 4413 | const auto scheduledFrameResultOpt = mScheduler->getScheduledFrameResult(); |
| 4414 | const auto expectedPresentTimeOnScheduler = scheduledFrameResultOpt.has_value() |
| 4415 | ? scheduledFrameResultOpt->vsyncTime |
| 4416 | : TimePoint::fromNs(0); |
| 4417 | expectedPresentTime = |
| 4418 | std::max(expectedPresentTimeOnPredictor, expectedPresentTimeOnScheduler); |
| 4419 | } |
| 4420 | |
| 4421 | if (expectedPresentTime < TimePoint::now()) { |
| 4422 | expectedPresentTime = |
| 4423 | mScheduler->getVsyncSchedule()->vsyncDeadlineAfter(TimePoint::now()); |
| 4424 | if (mScheduler->vsyncModulator().getVsyncConfig().sfWorkDuration > |
| 4425 | mScheduler->getVsyncSchedule(displayId)->period()) { |
| 4426 | expectedPresentTime += mScheduler->getVsyncSchedule(displayId)->period(); |
| 4427 | } |
| 4428 | } |
| 4429 | const auto status = getHwComposer().notifyExpectedPresent(displayId, expectedPresentTime, |
| 4430 | data.lastFrameInterval); |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4431 | if (status != NO_ERROR) { |
| 4432 | ALOGE("%s failed to notifyExpectedPresentHint for display %" PRId64, whence, |
| 4433 | displayId.value); |
| 4434 | } |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4435 | }; |
| 4436 | |
| 4437 | if (itr->second.hintStatus == NotifyExpectedPresentHintStatus::ScheduleOnTx) { |
| 4438 | return static_cast<void>(mScheduler->schedule([=, |
| 4439 | this]() FTL_FAKE_GUARD(kMainThreadContext) { |
| 4440 | auto& data = mNotifyExpectedPresentMap.at(displayId); |
| 4441 | auto scheduleHintOnTx = NotifyExpectedPresentHintStatus::ScheduleOnTx; |
| 4442 | if (data.hintStatus.compare_exchange_strong(scheduleHintOnTx, |
| 4443 | NotifyExpectedPresentHintStatus::Sent)) { |
| 4444 | sendHint(); |
| 4445 | } |
| 4446 | })); |
| 4447 | } |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4448 | auto scheduleHintOnPresent = NotifyExpectedPresentHintStatus::ScheduleOnPresent; |
| 4449 | if (itr->second.hintStatus.compare_exchange_strong(scheduleHintOnPresent, |
| 4450 | NotifyExpectedPresentHintStatus::Sent)) { |
| 4451 | sendHint(); |
| 4452 | } |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | void SurfaceFlinger::sendNotifyExpectedPresentHint(PhysicalDisplayId displayId) { |
| 4456 | if (auto itr = mNotifyExpectedPresentMap.find(displayId); |
| 4457 | itr == mNotifyExpectedPresentMap.end() || |
| 4458 | itr->second.hintStatus != NotifyExpectedPresentHintStatus::ScheduleOnPresent) { |
| 4459 | return; |
| 4460 | } |
| 4461 | scheduleNotifyExpectedPresentHint(displayId); |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 4462 | } |
| 4463 | |
Ady Abraham | 14beed7 | 2024-05-15 17:16:45 -0700 | [diff] [blame] | 4464 | void SurfaceFlinger::onCommitNotComposited(PhysicalDisplayId pacesetterDisplayId) { |
| 4465 | if (FlagManager::getInstance().commit_not_composited()) { |
| 4466 | mFrameTimeline->onCommitNotComposited(); |
| 4467 | } |
| 4468 | } |
| 4469 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 4470 | void SurfaceFlinger::initScheduler(const sp<const DisplayDevice>& display) { |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 4471 | using namespace scheduler; |
| 4472 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 4473 | LOG_ALWAYS_FATAL_IF(mScheduler); |
| 4474 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 4475 | const auto activeMode = display->refreshRateSelector().getActiveMode(); |
| 4476 | const Fps activeRefreshRate = activeMode.fps; |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 4477 | |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 4478 | FeatureFlags features; |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 4479 | |
Ady Abraham | 3f84c50 | 2023-11-30 18:18:06 -0800 | [diff] [blame] | 4480 | const auto defaultContentDetectionValue = |
| 4481 | FlagManager::getInstance().enable_fro_dependent_features() && |
| 4482 | sysprop::enable_frame_rate_override(true); |
| 4483 | if (sysprop::use_content_detection_for_refresh_rate(defaultContentDetectionValue)) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 4484 | features |= Feature::kContentDetection; |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 4485 | if (FlagManager::getInstance().enable_small_area_detection()) { |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 4486 | features |= Feature::kSmallDirtyContentDetection; |
| 4487 | } |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 4488 | } |
| 4489 | if (base::GetBoolProperty("debug.sf.show_predicted_vsync"s, false)) { |
| 4490 | features |= Feature::kTracePredictedVsync; |
| 4491 | } |
| 4492 | if (!base::GetBoolProperty("debug.sf.vsync_reactor_ignore_present_fences"s, false) && |
Dominik Laskowski | e62606d | 2024-02-22 11:42:13 -0500 | [diff] [blame] | 4493 | mHasReliablePresentFences) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 4494 | features |= Feature::kPresentFences; |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 4495 | } |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 4496 | if (display->refreshRateSelector().kernelIdleTimerController()) { |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 4497 | features |= Feature::kKernelIdleTimer; |
| 4498 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 4499 | if (mBackpressureGpuComposition) { |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 4500 | features |= Feature::kBackpressureGpuComposition; |
| 4501 | } |
Leon Scroggins III | 0bd0d4c | 2022-12-08 13:20:45 -0500 | [diff] [blame] | 4502 | if (getHwComposer().getComposer()->isSupported( |
| 4503 | Hwc2::Composer::OptionalFeature::ExpectedPresentTime)) { |
| 4504 | features |= Feature::kExpectedPresentTime; |
| 4505 | } |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 4506 | |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 4507 | mScheduler = std::make_unique<Scheduler>(static_cast<ICompositor&>(*this), |
| 4508 | static_cast<ISchedulerCallback&>(*this), features, |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4509 | getFactory(), activeRefreshRate, *mTimeStats); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 4510 | |
| 4511 | // The pacesetter must be registered before EventThread creation below. |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 4512 | mScheduler->registerDisplay(display->getPhysicalId(), display->holdRefreshRateSelector(), |
| 4513 | mActiveDisplayId); |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 4514 | if (FlagManager::getInstance().vrr_config()) { |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 4515 | mScheduler->setRenderRate(display->getPhysicalId(), activeMode.fps, |
| 4516 | /*applyImmediately*/ true); |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 4517 | } |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 4518 | |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 4519 | const auto configs = mScheduler->getVsyncConfiguration().getCurrentConfigs(); |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 4520 | |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 4521 | mScheduler->createEventThread(scheduler::Cycle::Render, mFrameTimeline->getTokenManager(), |
| 4522 | /* workDuration */ configs.late.appWorkDuration, |
| 4523 | /* readyDuration */ configs.late.sfWorkDuration); |
| 4524 | mScheduler->createEventThread(scheduler::Cycle::LastComposite, |
| 4525 | mFrameTimeline->getTokenManager(), |
| 4526 | /* workDuration */ activeRefreshRate.getPeriod(), |
| 4527 | /* readyDuration */ configs.late.sfWorkDuration); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 4528 | |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 4529 | // Dispatch after EventThread creation, since registerDisplay above skipped dispatch. |
| 4530 | mScheduler->dispatchHotplug(display->getPhysicalId(), scheduler::Scheduler::Hotplug::Connected); |
| 4531 | |
Leon Scroggins III | a7be94e | 2024-01-23 12:24:30 -0500 | [diff] [blame] | 4532 | mScheduler->initVsync(*mFrameTimeline->getTokenManager(), configs.late.sfWorkDuration); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 4533 | |
| 4534 | mRegionSamplingThread = |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 4535 | sp<RegionSamplingThread>::make(*this, |
| 4536 | RegionSamplingThread::EnvironmentTimingTunables()); |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 4537 | mFpsReporter = sp<FpsReporter>::make(*mFrameTimeline); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 4538 | |
| 4539 | // Timer callbacks may fire, so do this last. |
| 4540 | mScheduler->startTimers(); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 4541 | } |
| 4542 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4543 | void SurfaceFlinger::doCommitTransactions() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4544 | SFTRACE_CALL(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4545 | |
Lloyd Pique | 58e24a3 | 2019-08-01 15:50:14 -0700 | [diff] [blame] | 4546 | if (!mLayersPendingRemoval.isEmpty()) { |
| 4547 | // Notify removed layers now that they can't be drawn from |
| 4548 | for (const auto& l : mLayersPendingRemoval) { |
Lloyd Pique | 58e24a3 | 2019-08-01 15:50:14 -0700 | [diff] [blame] | 4549 | // Ensure any buffers set to display on any children are released. |
| 4550 | if (l->isRemovedFromCurrentState()) { |
| 4551 | l->latchAndReleaseBuffer(); |
| 4552 | } |
| 4553 | |
chaviw | 6852bff | 2022-04-19 17:35:11 -0500 | [diff] [blame] | 4554 | // If a layer has a parent, we allow it to out-live it's handle |
| 4555 | // with the idea that the parent holds a reference and will eventually |
| 4556 | // be cleaned up. However no one cleans up the top-level so we do so |
| 4557 | // here. |
| 4558 | if (l->isAtRoot()) { |
| 4559 | l->setIsAtRoot(false); |
| 4560 | mCurrentState.layersSortedByZ.remove(l); |
| 4561 | } |
| 4562 | |
Lloyd Pique | 58e24a3 | 2019-08-01 15:50:14 -0700 | [diff] [blame] | 4563 | // If the layer has been removed and has no parent, then it will not be reachable |
| 4564 | // when traversing layers on screen. Add the layer to the offscreenLayers set to |
| 4565 | // ensure we can copy its current to drawing state. |
| 4566 | if (!l->getParent()) { |
| 4567 | mOffscreenLayers.emplace(l.get()); |
| 4568 | } |
| 4569 | } |
| 4570 | mLayersPendingRemoval.clear(); |
| 4571 | } |
| 4572 | |
Lloyd Pique | 58e24a3 | 2019-08-01 15:50:14 -0700 | [diff] [blame] | 4573 | mDrawingState = mCurrentState; |
Lloyd Pique | 58e24a3 | 2019-08-01 15:50:14 -0700 | [diff] [blame] | 4574 | mCurrentState.colorMatrixChanged = false; |
| 4575 | |
Robert Carr | a70e91c | 2021-06-11 13:59:52 -0700 | [diff] [blame] | 4576 | if (mVisibleRegionsDirty) { |
| 4577 | for (const auto& rootLayer : mDrawingState.layersSortedByZ) { |
| 4578 | rootLayer->commitChildList(); |
| 4579 | } |
Andrei Stanciu | dadac5a | 2020-08-05 17:02:34 +0300 | [diff] [blame] | 4580 | } |
Robert Carr | a70e91c | 2021-06-11 13:59:52 -0700 | [diff] [blame] | 4581 | |
Lloyd Pique | 58e24a3 | 2019-08-01 15:50:14 -0700 | [diff] [blame] | 4582 | commitOffscreenLayers(); |
Vishnu Nair | 3be6190 | 2023-04-26 19:47:29 -0700 | [diff] [blame] | 4583 | if (mLayerMirrorRoots.size() > 0) { |
| 4584 | std::deque<Layer*> pendingUpdates; |
| 4585 | pendingUpdates.insert(pendingUpdates.end(), mLayerMirrorRoots.begin(), |
| 4586 | mLayerMirrorRoots.end()); |
| 4587 | std::vector<Layer*> needsUpdating; |
| 4588 | for (Layer* cloneRoot : mLayerMirrorRoots) { |
| 4589 | pendingUpdates.pop_front(); |
| 4590 | if (cloneRoot->isRemovedFromCurrentState()) { |
| 4591 | continue; |
| 4592 | } |
| 4593 | if (cloneRoot->updateMirrorInfo(pendingUpdates)) { |
| 4594 | } else { |
| 4595 | needsUpdating.push_back(cloneRoot); |
| 4596 | } |
| 4597 | } |
| 4598 | for (Layer* cloneRoot : needsUpdating) { |
| 4599 | cloneRoot->updateMirrorInfo({}); |
| 4600 | } |
Robert Carr | 6a0382d | 2021-07-01 15:57:17 -0700 | [diff] [blame] | 4601 | } |
Lloyd Pique | 58e24a3 | 2019-08-01 15:50:14 -0700 | [diff] [blame] | 4602 | } |
| 4603 | |
chaviw | 74d90ad | 2019-04-26 14:45:26 -0700 | [diff] [blame] | 4604 | void SurfaceFlinger::commitOffscreenLayers() { |
| 4605 | for (Layer* offscreenLayer : mOffscreenLayers) { |
Edgar Arriaga | 844fa67 | 2020-01-16 14:21:42 -0800 | [diff] [blame] | 4606 | offscreenLayer->traverse(LayerVector::StateSet::Drawing, [](Layer* layer) { |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4607 | if (layer->clearTransactionFlags(eTransactionNeeded)) { |
Vishnu Nair | a156f48 | 2023-02-22 00:23:38 +0000 | [diff] [blame] | 4608 | layer->doTransaction(0); |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4609 | layer->commitChildList(); |
| 4610 | } |
chaviw | 74d90ad | 2019-04-26 14:45:26 -0700 | [diff] [blame] | 4611 | }); |
| 4612 | } |
| 4613 | } |
| 4614 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4615 | void SurfaceFlinger::invalidateLayerStack(const ui::LayerFilter& layerFilter, const Region& dirty) { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 4616 | for (const auto& [token, displayDevice] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 4617 | auto display = displayDevice->getCompositionDisplay(); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4618 | if (display->includesLayer(layerFilter)) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 4619 | display->editState().dirtyRegion.orSelf(dirty); |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 4620 | } |
| 4621 | } |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 4622 | } |
| 4623 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 4624 | bool SurfaceFlinger::latchBuffers() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4625 | SFTRACE_CALL(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 4626 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 4627 | const nsecs_t latchTime = systemTime(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4628 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 4629 | bool visibleRegions = false; |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 4630 | bool frameQueued = false; |
Mike Stroyan | 0cd7619 | 2017-04-20 12:10:48 -0600 | [diff] [blame] | 4631 | bool newDataLatched = false; |
Eric Penner | 51c59cd | 2014-07-28 19:51:58 -0700 | [diff] [blame] | 4632 | |
| 4633 | // Store the set of layers that need updates. This set must not change as |
| 4634 | // buffers are being latched, as this could result in a deadlock. |
| 4635 | // Example: Two producers share the same command stream and: |
| 4636 | // 1.) Layer 0 is latched |
| 4637 | // 2.) Layer 0 gets a new frame |
| 4638 | // 2.) Layer 1 gets a new frame |
| 4639 | // 3.) Layer 1 is latched. |
| 4640 | // Display is now waiting on Layer 1's frame, which is behind layer 0's |
| 4641 | // second frame. But layer 0's second frame could be waiting on display. |
Edgar Arriaga | 844fa67 | 2020-01-16 14:21:42 -0800 | [diff] [blame] | 4642 | mDrawingState.traverse([&](Layer* layer) { |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4643 | if (layer->clearTransactionFlags(eTransactionNeeded) || mForceTransactionDisplayChange) { |
Vishnu Nair | a156f48 | 2023-02-22 00:23:38 +0000 | [diff] [blame] | 4644 | const uint32_t flags = layer->doTransaction(0); |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4645 | if (flags & Layer::eVisibleRegion) { |
| 4646 | mVisibleRegionsDirty = true; |
| 4647 | } |
| 4648 | } |
Robert Carr | b552ff5 | 2021-06-11 13:35:54 -0700 | [diff] [blame] | 4649 | |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 4650 | if (layer->hasReadyFrame() || layer->willReleaseBufferOnLatch()) { |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 4651 | frameQueued = true; |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 4652 | mLayersWithQueuedFrames.emplace(sp<Layer>::fromExisting(layer)); |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4653 | } else { |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 4654 | layer->useEmptyDamage(); |
Vishnu Nair | c09c023 | 2023-03-02 03:22:35 +0000 | [diff] [blame] | 4655 | if (!layer->hasBuffer()) { |
| 4656 | // The last latch time is used to classify a missed frame as buffer stuffing |
| 4657 | // instead of a missed frame. This is used to identify scenarios where we |
| 4658 | // could not latch a buffer or apply a transaction due to backpressure. |
| 4659 | // We only update the latch time for buffer less layers here, the latch time |
| 4660 | // is updated for buffer layers when the buffer is latched. |
| 4661 | layer->updateLastLatchTime(latchTime); |
| 4662 | } |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 4663 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4664 | }); |
Robert Carr | b552ff5 | 2021-06-11 13:35:54 -0700 | [diff] [blame] | 4665 | mForceTransactionDisplayChange = false; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4666 | |
chaviw | 49a108c | 2019-08-12 11:23:06 -0700 | [diff] [blame] | 4667 | // The client can continue submitting buffers for offscreen layers, but they will not |
| 4668 | // be shown on screen. Therefore, we need to latch and release buffers of offscreen |
| 4669 | // layers to ensure dequeueBuffer doesn't block indefinitely. |
| 4670 | for (Layer* offscreenLayer : mOffscreenLayers) { |
Edgar Arriaga | 844fa67 | 2020-01-16 14:21:42 -0800 | [diff] [blame] | 4671 | offscreenLayer->traverse(LayerVector::StateSet::Drawing, |
chaviw | 49a108c | 2019-08-12 11:23:06 -0700 | [diff] [blame] | 4672 | [&](Layer* l) { l->latchAndReleaseBuffer(); }); |
| 4673 | } |
| 4674 | |
Lloyd Pique | f2c7939 | 2018-12-20 16:23:33 -0800 | [diff] [blame] | 4675 | if (!mLayersWithQueuedFrames.empty()) { |
| 4676 | // mStateLock is needed for latchBuffer as LayerRejecter::reject() |
| 4677 | // writes to Layer current state. See also b/119481871 |
| 4678 | Mutex::Autolock lock(mStateLock); |
| 4679 | |
Alec Mouri | 2f7d9ae | 2020-11-30 19:32:33 -0800 | [diff] [blame] | 4680 | for (const auto& layer : mLayersWithQueuedFrames) { |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 4681 | if (layer->willReleaseBufferOnLatch()) { |
| 4682 | mLayersWithBuffersRemoved.emplace(layer); |
| 4683 | } |
Vishnu Nair | 397a0e3 | 2022-07-26 00:01:48 +0000 | [diff] [blame] | 4684 | if (layer->latchBuffer(visibleRegions, latchTime)) { |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 4685 | mLayersPendingRefresh.push_back(layer); |
Lloyd Pique | f2c7939 | 2018-12-20 16:23:33 -0800 | [diff] [blame] | 4686 | newDataLatched = true; |
| 4687 | } |
Vishnu Nair | 1b70019 | 2022-02-04 10:09:47 -0800 | [diff] [blame] | 4688 | layer->useSurfaceDamage(); |
Mike Stroyan | 0cd7619 | 2017-04-20 12:10:48 -0600 | [diff] [blame] | 4689 | } |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 4690 | } |
Mathias Agopian | 4da7519 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 4691 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 4692 | mVisibleRegionsDirty |= visibleRegions; |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 4693 | |
| 4694 | // If we will need to wake up at some time in the future to deal with a |
| 4695 | // queued frame that shouldn't be displayed during this vsync period, wake |
| 4696 | // up during the next vsync period to check again. |
Chia-I Wu | a36bf92 | 2017-06-30 12:51:05 -0700 | [diff] [blame] | 4697 | if (frameQueued && (mLayersWithQueuedFrames.empty() || !newDataLatched)) { |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 4698 | scheduleCommit(FrameHint::kNone); |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 4699 | } |
| 4700 | |
Chia-I Wu | 14c9c7b | 2018-06-26 10:18:18 +0800 | [diff] [blame] | 4701 | // enter boot animation on first buffer latch |
| 4702 | if (CC_UNLIKELY(mBootStage == BootStage::BOOTLOADER && newDataLatched)) { |
| 4703 | ALOGI("Enter boot animation"); |
| 4704 | mBootStage = BootStage::BOOTANIMATION; |
| 4705 | } |
| 4706 | |
Vishnu Nair | 3be6190 | 2023-04-26 19:47:29 -0700 | [diff] [blame] | 4707 | if (mLayerMirrorRoots.size() > 0) { |
Robert Carr | 6a0382d | 2021-07-01 15:57:17 -0700 | [diff] [blame] | 4708 | mDrawingState.traverse([&](Layer* layer) { layer->updateCloneBufferInfo(); }); |
| 4709 | } |
chaviw | 74b0317 | 2019-08-19 11:09:03 -0700 | [diff] [blame] | 4710 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 4711 | // Only continue with the refresh if there is actually new work to do |
Mike Stroyan | 0cd7619 | 2017-04-20 12:10:48 -0600 | [diff] [blame] | 4712 | return !mLayersWithQueuedFrames.empty() && newDataLatched; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4713 | } |
| 4714 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 4715 | status_t SurfaceFlinger::addClientLayer(LayerCreationArgs& args, const sp<IBinder>& handle, |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 4716 | const sp<Layer>& layer, const wp<Layer>& parent, |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 4717 | uint32_t* outTransformHint) { |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 4718 | if (mNumLayers >= MAX_LAYERS) { |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 4719 | ALOGE("AddClientLayer failed, mNumLayers (%zu) >= MAX_LAYERS (%zu)", mNumLayers.load(), |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 4720 | MAX_LAYERS); |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 4721 | static_cast<void>(mScheduler->schedule([=, this] { |
Vishnu Nair | 787aa78 | 2023-03-17 13:46:46 -0700 | [diff] [blame] | 4722 | ALOGE("Dumping layer keeping > 20 children alive:"); |
| 4723 | bool leakingParentLayerFound = false; |
| 4724 | mDrawingState.traverse([&](Layer* layer) { |
| 4725 | if (leakingParentLayerFound) { |
| 4726 | return; |
| 4727 | } |
| 4728 | if (layer->getChildrenCount() > 20) { |
| 4729 | leakingParentLayerFound = true; |
| 4730 | sp<Layer> parent = sp<Layer>::fromExisting(layer); |
| 4731 | while (parent) { |
Vishnu Nair | 3be6190 | 2023-04-26 19:47:29 -0700 | [diff] [blame] | 4732 | ALOGE("Parent Layer: %s%s", parent->getName().c_str(), |
| 4733 | (parent->isHandleAlive() ? "handleAlive" : "")); |
Vishnu Nair | 787aa78 | 2023-03-17 13:46:46 -0700 | [diff] [blame] | 4734 | parent = parent->getParent(); |
| 4735 | } |
| 4736 | // Sample up to 100 layers |
| 4737 | ALOGE("Dumping random sampling of child layers total(%zu): ", |
| 4738 | layer->getChildrenCount()); |
| 4739 | int sampleSize = (layer->getChildrenCount() / 100) + 1; |
| 4740 | layer->traverseChildren([&](Layer* layer) { |
| 4741 | if (rand() % sampleSize == 0) { |
jiayongqiang | c476a34 | 2023-12-06 17:04:54 +0800 | [diff] [blame] | 4742 | ALOGE("Child Layer: %s%s", layer->getName().c_str(), |
| 4743 | (layer->isHandleAlive() ? "handleAlive" : "")); |
Vishnu Nair | 787aa78 | 2023-03-17 13:46:46 -0700 | [diff] [blame] | 4744 | } |
| 4745 | }); |
| 4746 | } |
| 4747 | }); |
| 4748 | |
Vishnu Nair | 3be6190 | 2023-04-26 19:47:29 -0700 | [diff] [blame] | 4749 | int numLayers = 0; |
| 4750 | mDrawingState.traverse([&](Layer* layer) { numLayers++; }); |
| 4751 | |
| 4752 | ALOGE("Dumping random sampling of on-screen layers total(%u):", numLayers); |
Vishnu Nair | 787aa78 | 2023-03-17 13:46:46 -0700 | [diff] [blame] | 4753 | mDrawingState.traverse([&](Layer* layer) { |
Robert Carr | 26b98f4 | 2022-04-11 15:54:31 -0700 | [diff] [blame] | 4754 | // Aim to dump about 200 layers to avoid totally trashing |
| 4755 | // logcat. On the other hand, if there really are 4096 layers |
| 4756 | // something has gone totally wrong its probably the most |
| 4757 | // useful information in logcat. |
| 4758 | if (rand() % 20 == 13) { |
Vishnu Nair | 3be6190 | 2023-04-26 19:47:29 -0700 | [diff] [blame] | 4759 | ALOGE("Layer: %s%s", layer->getName().c_str(), |
| 4760 | (layer->isHandleAlive() ? "handleAlive" : "")); |
| 4761 | std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
Robert Carr | 26b98f4 | 2022-04-11 15:54:31 -0700 | [diff] [blame] | 4762 | } |
| 4763 | }); |
Vishnu Nair | 787aa78 | 2023-03-17 13:46:46 -0700 | [diff] [blame] | 4764 | ALOGE("Dumping random sampling of off-screen layers total(%zu): ", |
| 4765 | mOffscreenLayers.size()); |
Robert Carr | 26b98f4 | 2022-04-11 15:54:31 -0700 | [diff] [blame] | 4766 | for (Layer* offscreenLayer : mOffscreenLayers) { |
| 4767 | if (rand() % 20 == 13) { |
Vishnu Nair | 3be6190 | 2023-04-26 19:47:29 -0700 | [diff] [blame] | 4768 | ALOGE("Offscreen-layer: %s%s", offscreenLayer->getName().c_str(), |
| 4769 | (offscreenLayer->isHandleAlive() ? "handleAlive" : "")); |
| 4770 | std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
Robert Carr | 26b98f4 | 2022-04-11 15:54:31 -0700 | [diff] [blame] | 4771 | } |
| 4772 | } |
| 4773 | })); |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 4774 | return NO_MEMORY; |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 4775 | } |
| 4776 | |
Vishnu Nair | 86e1190 | 2022-09-06 16:03:27 +0000 | [diff] [blame] | 4777 | layer->updateTransformHint(mActiveDisplayTransformHint); |
| 4778 | if (outTransformHint) { |
| 4779 | *outTransformHint = mActiveDisplayTransformHint; |
| 4780 | } |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 4781 | args.parentId = LayerHandle::getLayerId(args.parentHandle.promote()); |
| 4782 | args.layerIdToMirror = LayerHandle::getLayerId(args.mirrorLayerHandle.promote()); |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 4783 | { |
| 4784 | std::scoped_lock<std::mutex> lock(mCreatedLayersLock); |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 4785 | mCreatedLayers.emplace_back(layer, parent, args.addToRoot); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4786 | mNewLayers.emplace_back(std::make_unique<frontend::RequestedLayerState>(args)); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 4787 | args.mirrorLayerHandle.clear(); |
| 4788 | args.parentHandle.clear(); |
| 4789 | mNewLayerArgs.emplace_back(std::move(args)); |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 4790 | } |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 4791 | |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 4792 | setTransactionFlags(eTransactionNeeded); |
| 4793 | return NO_ERROR; |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 4794 | } |
| 4795 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4796 | uint32_t SurfaceFlinger::getTransactionFlags() const { |
Lloyd Pique | f1c675b | 2018-09-12 20:45:39 -0700 | [diff] [blame] | 4797 | return mTransactionFlags; |
Mathias Agopian | dea20b1 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 4798 | } |
| 4799 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4800 | uint32_t SurfaceFlinger::clearTransactionFlags(uint32_t mask) { |
Vishnu Nair | e4f00c5 | 2023-01-31 20:20:18 -0800 | [diff] [blame] | 4801 | uint32_t transactionFlags = mTransactionFlags.fetch_and(~mask); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4802 | SFTRACE_INT("mTransactionFlags", transactionFlags); |
Vishnu Nair | e4f00c5 | 2023-01-31 20:20:18 -0800 | [diff] [blame] | 4803 | return transactionFlags & mask; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4804 | } |
| 4805 | |
Dominik Laskowski | 94000c4 | 2022-03-17 12:55:14 -0700 | [diff] [blame] | 4806 | void SurfaceFlinger::setTransactionFlags(uint32_t mask, TransactionSchedule schedule, |
Dominik Laskowski | 1f6fc70 | 2022-03-21 08:34:50 -0700 | [diff] [blame] | 4807 | const sp<IBinder>& applyToken, FrameHint frameHint) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 4808 | mScheduler->modulateVsync({}, &VsyncModulator::setTransactionSchedule, schedule, applyToken); |
Vishnu Nair | e4f00c5 | 2023-01-31 20:20:18 -0800 | [diff] [blame] | 4809 | uint32_t transactionFlags = mTransactionFlags.fetch_or(mask); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4810 | SFTRACE_INT("mTransactionFlags", transactionFlags); |
Dominik Laskowski | 94000c4 | 2022-03-17 12:55:14 -0700 | [diff] [blame] | 4811 | |
Vishnu Nair | e4f00c5 | 2023-01-31 20:20:18 -0800 | [diff] [blame] | 4812 | if (const bool scheduled = transactionFlags & mask; !scheduled) { |
Dominik Laskowski | 1f6fc70 | 2022-03-21 08:34:50 -0700 | [diff] [blame] | 4813 | scheduleCommit(frameHint); |
Ady Abraham | 27bcb55 | 2023-01-21 04:34:22 +0000 | [diff] [blame] | 4814 | } else if (frameHint == FrameHint::kActive) { |
| 4815 | // Even if the next frame is already scheduled, we should reset the idle timer |
| 4816 | // as a new activity just happened. |
| 4817 | mScheduler->resetIdleTimer(); |
Dominik Laskowski | 94000c4 | 2022-03-17 12:55:14 -0700 | [diff] [blame] | 4818 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4819 | } |
| 4820 | |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4821 | TransactionHandler::TransactionReadiness SurfaceFlinger::transactionReadyTimelineCheck( |
| 4822 | const TransactionHandler::TransactionFlushState& flushState) { |
Dominik Laskowski | e85d7fa | 2023-05-15 10:50:22 -0400 | [diff] [blame] | 4823 | const auto& transaction = *flushState.transaction; |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 4824 | |
| 4825 | const TimePoint desiredPresentTime = TimePoint::fromNs(transaction.desiredPresentTime); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 4826 | const TimePoint expectedPresentTime = mScheduler->expectedPresentTimeForPacesetter(); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 4827 | |
| 4828 | using TransactionReadiness = TransactionHandler::TransactionReadiness; |
| 4829 | |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4830 | // Do not present if the desiredPresentTime has not passed unless it is more than |
| 4831 | // one second in the future. We ignore timestamps more than 1 second in the future |
| 4832 | // for stability reasons. |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 4833 | if (!transaction.isAutoTimestamp && desiredPresentTime >= expectedPresentTime && |
| 4834 | desiredPresentTime < expectedPresentTime + 1s) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4835 | SFTRACE_FORMAT("not current desiredPresentTime: %" PRId64 " expectedPresentTime: %" PRId64, |
| 4836 | desiredPresentTime, expectedPresentTime); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4837 | return TransactionReadiness::NotReady; |
Ady Abraham | e1bfaac | 2022-02-22 21:32:08 -0800 | [diff] [blame] | 4838 | } |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4839 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 4840 | const auto vsyncId = VsyncId{transaction.frameTimelineInfo.vsyncId}; |
| 4841 | |
| 4842 | // Transactions with VsyncId are already throttled by the vsyncId (i.e. Choreographer issued |
| 4843 | // the vsyncId according to the frame rate override cadence) so we shouldn't throttle again |
| 4844 | // when applying the transaction. Otherwise we might throttle older transactions |
| 4845 | // incorrectly as the frame rate of SF changed before it drained the older transactions. |
| 4846 | if (ftl::to_underlying(vsyncId) == FrameTimelineInfo::INVALID_VSYNC_ID && |
| 4847 | !mScheduler->isVsyncValid(expectedPresentTime, transaction.originUid)) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4848 | SFTRACE_FORMAT("!isVsyncValid expectedPresentTime: %" PRId64 " uid: %d", |
| 4849 | expectedPresentTime, transaction.originUid); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4850 | return TransactionReadiness::NotReady; |
| 4851 | } |
| 4852 | |
| 4853 | // If the client didn't specify desiredPresentTime, use the vsyncId to determine the |
| 4854 | // expected present time of this transaction. |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 4855 | if (transaction.isAutoTimestamp && frameIsEarly(expectedPresentTime, vsyncId)) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4856 | SFTRACE_FORMAT("frameIsEarly vsyncId: %" PRId64 " expectedPresentTime: %" PRId64, |
| 4857 | transaction.frameTimelineInfo.vsyncId, expectedPresentTime); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4858 | return TransactionReadiness::NotReady; |
| 4859 | } |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 4860 | |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4861 | return TransactionReadiness::Ready; |
| 4862 | } |
| 4863 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4864 | TransactionHandler::TransactionReadiness SurfaceFlinger::transactionReadyBufferCheckLegacy( |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4865 | const TransactionHandler::TransactionFlushState& flushState) { |
| 4866 | using TransactionReadiness = TransactionHandler::TransactionReadiness; |
| 4867 | auto ready = TransactionReadiness::Ready; |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4868 | flushState.transaction->traverseStatesWithBuffersWhileTrue([&](const ResolvedComposerState& |
| 4869 | resolvedState) -> bool { |
| 4870 | sp<Layer> layer = LayerHandle::getLayer(resolvedState.state.surface); |
| 4871 | |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4872 | const auto& transaction = *flushState.transaction; |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4873 | const auto& s = resolvedState.state; |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4874 | // check for barrier frames |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 4875 | if (s.bufferData->hasBarrier) { |
| 4876 | // The current producerId is already a newer producer than the buffer that has a |
| 4877 | // barrier. This means the incoming buffer is older and we can release it here. We |
| 4878 | // don't wait on the barrier since we know that's stale information. |
Vishnu Nair | 6322121 | 2023-04-06 15:17:37 -0700 | [diff] [blame] | 4879 | if (layer->getDrawingState().barrierProducerId > s.bufferData->producerId) { |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 4880 | layer->callReleaseBufferCallback(s.bufferData->releaseBufferListener, |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4881 | resolvedState.externalTexture->getBuffer(), |
Chavi Weingarten | c8bcb68 | 2023-03-06 22:20:42 +0000 | [diff] [blame] | 4882 | s.bufferData->frameNumber, |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 4883 | s.bufferData->acquireFence); |
| 4884 | // Delete the entire state at this point and not just release the buffer because |
| 4885 | // everything associated with the Layer in this Transaction is now out of date. |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4886 | SFTRACE_FORMAT("DeleteStaleBuffer %s barrierProducerId:%d > %d", |
| 4887 | layer->getDebugName(), layer->getDrawingState().barrierProducerId, |
| 4888 | s.bufferData->producerId); |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 4889 | return TraverseBuffersReturnValues::DELETE_AND_CONTINUE_TRAVERSAL; |
| 4890 | } |
| 4891 | |
Vishnu Nair | 6322121 | 2023-04-06 15:17:37 -0700 | [diff] [blame] | 4892 | if (layer->getDrawingState().barrierFrameNumber < s.bufferData->barrierFrameNumber) { |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 4893 | const bool willApplyBarrierFrame = |
| 4894 | flushState.bufferLayersReadyToPresent.contains(s.surface.get()) && |
| 4895 | ((flushState.bufferLayersReadyToPresent.get(s.surface.get()) >= |
| 4896 | s.bufferData->barrierFrameNumber)); |
| 4897 | if (!willApplyBarrierFrame) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4898 | SFTRACE_FORMAT("NotReadyBarrier %s barrierFrameNumber:%" PRId64 " > %" PRId64, |
| 4899 | layer->getDebugName(), |
| 4900 | layer->getDrawingState().barrierFrameNumber, |
| 4901 | s.bufferData->barrierFrameNumber); |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 4902 | ready = TransactionReadiness::NotReadyBarrier; |
| 4903 | return TraverseBuffersReturnValues::STOP_TRAVERSAL; |
| 4904 | } |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4905 | } |
| 4906 | } |
| 4907 | |
| 4908 | // If backpressure is enabled and we already have a buffer to commit, keep |
| 4909 | // the transaction in the queue. |
| 4910 | const bool hasPendingBuffer = |
| 4911 | flushState.bufferLayersReadyToPresent.contains(s.surface.get()); |
| 4912 | if (layer->backpressureEnabled() && hasPendingBuffer && transaction.isAutoTimestamp) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4913 | SFTRACE_FORMAT("hasPendingBuffer %s", layer->getDebugName()); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4914 | ready = TransactionReadiness::NotReady; |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 4915 | return TraverseBuffersReturnValues::STOP_TRAVERSAL; |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4916 | } |
| 4917 | |
Vishnu Nair | f01a6f1 | 2023-04-03 22:34:17 +0000 | [diff] [blame] | 4918 | const bool acquireFenceAvailable = s.bufferData && |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4919 | s.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged) && |
| 4920 | s.bufferData->acquireFence; |
Ying Wei | 1fd9bfc | 2023-06-29 16:47:00 +0000 | [diff] [blame] | 4921 | const bool fenceSignaled = !acquireFenceAvailable || |
Vishnu Nair | f01a6f1 | 2023-04-03 22:34:17 +0000 | [diff] [blame] | 4922 | s.bufferData->acquireFence->getStatus() != Fence::Status::Unsignaled; |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4923 | if (!fenceSignaled) { |
Vishnu Nair | f01a6f1 | 2023-04-03 22:34:17 +0000 | [diff] [blame] | 4924 | // check fence status |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4925 | const bool allowLatchUnsignaled = shouldLatchUnsignaled(s, transaction.states.size(), |
| 4926 | flushState.firstTransaction) && |
| 4927 | layer->isSimpleBufferUpdate(s); |
| 4928 | |
Vishnu Nair | f01a6f1 | 2023-04-03 22:34:17 +0000 | [diff] [blame] | 4929 | if (allowLatchUnsignaled) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4930 | SFTRACE_FORMAT("fence unsignaled try allowLatchUnsignaled %s", |
| 4931 | layer->getDebugName()); |
Vishnu Nair | f01a6f1 | 2023-04-03 22:34:17 +0000 | [diff] [blame] | 4932 | ready = TransactionReadiness::NotReadyUnsignaled; |
| 4933 | } else { |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4934 | ready = TransactionReadiness::NotReady; |
| 4935 | auto& listener = s.bufferData->releaseBufferListener; |
| 4936 | if (listener && |
| 4937 | (flushState.queueProcessTime - transaction.postTime) > |
| 4938 | std::chrono::nanoseconds(4s).count()) { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4939 | // Used to add a stalled transaction which uses an internal lock. |
| 4940 | ftl::FakeGuard guard(kMainThreadContext); |
Patrick Williams | f1e5df1 | 2022-10-17 21:37:42 +0000 | [diff] [blame] | 4941 | mTransactionHandler |
Patrick Williams | 090ad06 | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 4942 | .onTransactionQueueStalled(transaction.id, |
| 4943 | {.pid = layer->getOwnerPid(), |
| 4944 | .layerId = static_cast<uint32_t>( |
| 4945 | layer->getSequence()), |
| 4946 | .layerName = layer->getDebugName(), |
| 4947 | .bufferId = s.bufferData->getId(), |
| 4948 | .frameNumber = s.bufferData->frameNumber}); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4949 | } |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4950 | SFTRACE_FORMAT("fence unsignaled %s", layer->getDebugName()); |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 4951 | return TraverseBuffersReturnValues::STOP_TRAVERSAL; |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4952 | } |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4953 | } |
liulijun | eb489f6 | 2022-10-17 22:02:14 +0800 | [diff] [blame] | 4954 | return TraverseBuffersReturnValues::CONTINUE_TRAVERSAL; |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4955 | }); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4956 | return ready; |
| 4957 | } |
| 4958 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4959 | TransactionHandler::TransactionReadiness SurfaceFlinger::transactionReadyBufferCheck( |
| 4960 | const TransactionHandler::TransactionFlushState& flushState) { |
| 4961 | using TransactionReadiness = TransactionHandler::TransactionReadiness; |
| 4962 | auto ready = TransactionReadiness::Ready; |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4963 | flushState.transaction->traverseStatesWithBuffersWhileTrue( |
| 4964 | [&](const ResolvedComposerState& resolvedState) FTL_FAKE_GUARD( |
| 4965 | kMainThreadContext) -> bool { |
| 4966 | const frontend::RequestedLayerState* layer = |
| 4967 | mLayerLifecycleManager.getLayerFromId(resolvedState.layerId); |
| 4968 | const auto& transaction = *flushState.transaction; |
| 4969 | const auto& s = resolvedState.state; |
| 4970 | // check for barrier frames |
| 4971 | if (s.bufferData->hasBarrier) { |
| 4972 | // The current producerId is already a newer producer than the buffer that has a |
| 4973 | // barrier. This means the incoming buffer is older and we can release it here. |
| 4974 | // We don't wait on the barrier since we know that's stale information. |
| 4975 | if (layer->barrierProducerId > s.bufferData->producerId) { |
| 4976 | if (s.bufferData->releaseBufferListener) { |
| 4977 | uint32_t currentMaxAcquiredBufferCount = |
| 4978 | getMaxAcquiredBufferCountForCurrentRefreshRate( |
| 4979 | layer->ownerUid.val()); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4980 | SFTRACE_FORMAT_INSTANT("callReleaseBufferCallback %s - %" PRIu64, |
| 4981 | layer->name.c_str(), s.bufferData->frameNumber); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4982 | s.bufferData->releaseBufferListener |
| 4983 | ->onReleaseBuffer({resolvedState.externalTexture->getBuffer() |
| 4984 | ->getId(), |
| 4985 | s.bufferData->frameNumber}, |
| 4986 | s.bufferData->acquireFence |
| 4987 | ? s.bufferData->acquireFence |
| 4988 | : Fence::NO_FENCE, |
| 4989 | currentMaxAcquiredBufferCount); |
| 4990 | } |
| 4991 | |
| 4992 | // Delete the entire state at this point and not just release the buffer |
| 4993 | // because everything associated with the Layer in this Transaction is now |
| 4994 | // out of date. |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 4995 | SFTRACE_FORMAT("DeleteStaleBuffer %s barrierProducerId:%d > %d", |
| 4996 | layer->name.c_str(), layer->barrierProducerId, |
| 4997 | s.bufferData->producerId); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4998 | return TraverseBuffersReturnValues::DELETE_AND_CONTINUE_TRAVERSAL; |
| 4999 | } |
| 5000 | |
| 5001 | if (layer->barrierFrameNumber < s.bufferData->barrierFrameNumber) { |
| 5002 | const bool willApplyBarrierFrame = |
| 5003 | flushState.bufferLayersReadyToPresent.contains(s.surface.get()) && |
| 5004 | ((flushState.bufferLayersReadyToPresent.get(s.surface.get()) >= |
| 5005 | s.bufferData->barrierFrameNumber)); |
| 5006 | if (!willApplyBarrierFrame) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 5007 | SFTRACE_FORMAT("NotReadyBarrier %s barrierFrameNumber:%" PRId64 |
| 5008 | " > %" PRId64, |
| 5009 | layer->name.c_str(), layer->barrierFrameNumber, |
| 5010 | s.bufferData->barrierFrameNumber); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5011 | ready = TransactionReadiness::NotReadyBarrier; |
| 5012 | return TraverseBuffersReturnValues::STOP_TRAVERSAL; |
| 5013 | } |
| 5014 | } |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5017 | // If backpressure is enabled and we already have a buffer to commit, keep |
| 5018 | // the transaction in the queue. |
| 5019 | const bool hasPendingBuffer = |
| 5020 | flushState.bufferLayersReadyToPresent.contains(s.surface.get()); |
| 5021 | if (layer->backpressureEnabled() && hasPendingBuffer && |
| 5022 | transaction.isAutoTimestamp) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 5023 | SFTRACE_FORMAT("hasPendingBuffer %s", layer->name.c_str()); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5024 | ready = TransactionReadiness::NotReady; |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 5025 | return TraverseBuffersReturnValues::STOP_TRAVERSAL; |
| 5026 | } |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 5027 | |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5028 | const bool acquireFenceAvailable = s.bufferData && |
| 5029 | s.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged) && |
| 5030 | s.bufferData->acquireFence; |
| 5031 | const bool fenceSignaled = !acquireFenceAvailable || |
| 5032 | s.bufferData->acquireFence->getStatus() != Fence::Status::Unsignaled; |
| 5033 | if (!fenceSignaled) { |
| 5034 | // check fence status |
| 5035 | const bool allowLatchUnsignaled = |
| 5036 | shouldLatchUnsignaled(s, transaction.states.size(), |
| 5037 | flushState.firstTransaction) && |
| 5038 | layer->isSimpleBufferUpdate(s); |
| 5039 | if (allowLatchUnsignaled) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 5040 | SFTRACE_FORMAT("fence unsignaled try allowLatchUnsignaled %s", |
| 5041 | layer->name.c_str()); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5042 | ready = TransactionReadiness::NotReadyUnsignaled; |
| 5043 | } else { |
| 5044 | ready = TransactionReadiness::NotReady; |
| 5045 | auto& listener = s.bufferData->releaseBufferListener; |
| 5046 | if (listener && |
| 5047 | (flushState.queueProcessTime - transaction.postTime) > |
| 5048 | std::chrono::nanoseconds(4s).count()) { |
| 5049 | mTransactionHandler |
| 5050 | .onTransactionQueueStalled(transaction.id, |
| 5051 | {.pid = layer->ownerPid.val(), |
| 5052 | .layerId = layer->id, |
| 5053 | .layerName = layer->name, |
| 5054 | .bufferId = s.bufferData->getId(), |
| 5055 | .frameNumber = |
| 5056 | s.bufferData->frameNumber}); |
| 5057 | } |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 5058 | SFTRACE_FORMAT("fence unsignaled %s", layer->name.c_str()); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5059 | return TraverseBuffersReturnValues::STOP_TRAVERSAL; |
| 5060 | } |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 5061 | } |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5062 | return TraverseBuffersReturnValues::CONTINUE_TRAVERSAL; |
| 5063 | }); |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 5064 | return ready; |
| 5065 | } |
| 5066 | |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 5067 | void SurfaceFlinger::addTransactionReadyFilters() { |
| 5068 | mTransactionHandler.addTransactionReadyFilter( |
| 5069 | std::bind(&SurfaceFlinger::transactionReadyTimelineCheck, this, std::placeholders::_1)); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 5070 | if (mLayerLifecycleManagerEnabled) { |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 5071 | mTransactionHandler.addTransactionReadyFilter( |
| 5072 | std::bind(&SurfaceFlinger::transactionReadyBufferCheck, this, |
| 5073 | std::placeholders::_1)); |
| 5074 | } else { |
| 5075 | mTransactionHandler.addTransactionReadyFilter( |
| 5076 | std::bind(&SurfaceFlinger::transactionReadyBufferCheckLegacy, this, |
| 5077 | std::placeholders::_1)); |
| 5078 | } |
Ady Abraham | e1bfaac | 2022-02-22 21:32:08 -0800 | [diff] [blame] | 5079 | } |
| 5080 | |
Vishnu Nair | 42ad319 | 2023-01-24 19:17:59 -0800 | [diff] [blame] | 5081 | // For tests only |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 5082 | bool SurfaceFlinger::flushTransactionQueues(VsyncId vsyncId) { |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 5083 | mTransactionHandler.collectTransactions(); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 5084 | std::vector<TransactionState> transactions = mTransactionHandler.flushTransactions(); |
Vishnu Nair | 42ad319 | 2023-01-24 19:17:59 -0800 | [diff] [blame] | 5085 | return applyTransactions(transactions, vsyncId); |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 5088 | bool SurfaceFlinger::applyTransactions(std::vector<TransactionState>& transactions, |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 5089 | VsyncId vsyncId) { |
Dominik Laskowski | 1686374 | 2022-09-29 11:57:40 -0400 | [diff] [blame] | 5090 | Mutex::Autolock lock(mStateLock); |
Vishnu Nair | 42ad319 | 2023-01-24 19:17:59 -0800 | [diff] [blame] | 5091 | return applyTransactionsLocked(transactions, vsyncId); |
| 5092 | } |
| 5093 | |
| 5094 | bool SurfaceFlinger::applyTransactionsLocked(std::vector<TransactionState>& transactions, |
| 5095 | VsyncId vsyncId) { |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 5096 | bool needsTraversal = false; |
| 5097 | // Now apply all transactions. |
Robert Carr | ff7663b | 2022-02-08 12:46:49 -0800 | [diff] [blame] | 5098 | for (auto& transaction : transactions) { |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 5099 | needsTraversal |= |
| 5100 | applyTransactionState(transaction.frameTimelineInfo, transaction.states, |
| 5101 | transaction.displays, transaction.flags, |
| 5102 | transaction.inputWindowCommands, |
| 5103 | transaction.desiredPresentTime, transaction.isAutoTimestamp, |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 5104 | std::move(transaction.uncacheBufferIds), transaction.postTime, |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 5105 | transaction.hasListenerCallbacks, |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 5106 | transaction.listenerCallbacks, transaction.originPid, |
| 5107 | transaction.originUid, transaction.id); |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 5108 | } |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 5109 | return needsTraversal; |
Marissa Wall | e6e3c0d | 2019-03-29 10:28:30 -0700 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | bool SurfaceFlinger::transactionFlushNeeded() { |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 5113 | return mTransactionHandler.hasPendingTransactions(); |
Marissa Wall | 713b63f | 2018-10-17 15:42:43 -0700 | [diff] [blame] | 5114 | } |
| 5115 | |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 5116 | bool SurfaceFlinger::frameIsEarly(TimePoint expectedPresentTime, VsyncId vsyncId) const { |
| 5117 | const auto prediction = |
Dominik Laskowski | 16a345b | 2023-04-27 17:07:33 -0400 | [diff] [blame] | 5118 | mFrameTimeline->getTokenManager()->getPredictionsForToken(ftl::to_underlying(vsyncId)); |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 5119 | if (!prediction) { |
Ady Abraham | 8db1010 | 2021-03-15 17:19:23 -0700 | [diff] [blame] | 5120 | return false; |
| 5121 | } |
| 5122 | |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 5123 | const auto predictedPresentTime = TimePoint::fromNs(prediction->presentTime); |
| 5124 | |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 5125 | if (std::chrono::abs(predictedPresentTime - expectedPresentTime) >= |
| 5126 | scheduler::VsyncConfig::kEarlyLatchMaxThreshold) { |
Ady Abraham | 8db1010 | 2021-03-15 17:19:23 -0700 | [diff] [blame] | 5127 | return false; |
| 5128 | } |
| 5129 | |
Ady Abraham | 3db8a3c | 2023-11-20 17:53:47 -0800 | [diff] [blame] | 5130 | const Duration earlyLatchVsyncThreshold = mScheduler->getVsyncSchedule()->minFramePeriod() / 2; |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 5131 | |
| 5132 | return predictedPresentTime >= expectedPresentTime && |
| 5133 | predictedPresentTime - expectedPresentTime >= earlyLatchVsyncThreshold; |
Ady Abraham | 8db1010 | 2021-03-15 17:19:23 -0700 | [diff] [blame] | 5134 | } |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 5135 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 5136 | bool SurfaceFlinger::shouldLatchUnsignaled(const layer_state_t& state, size_t numStates, |
| 5137 | bool firstTransaction) const { |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 5138 | if (enableLatchUnsignaledConfig == LatchUnsignaledConfig::Disabled) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 5139 | SFTRACE_FORMAT_INSTANT("%s: false (LatchUnsignaledConfig::Disabled)", __func__); |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 5140 | return false; |
| 5141 | } |
Ady Abraham | 8db1010 | 2021-03-15 17:19:23 -0700 | [diff] [blame] | 5142 | |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 5143 | // We only want to latch unsignaled when a single layer is updated in this |
| 5144 | // transaction (i.e. not a blast sync transaction). |
| 5145 | if (numStates != 1) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 5146 | SFTRACE_FORMAT_INSTANT("%s: false (numStates=%zu)", __func__, numStates); |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 5147 | return false; |
| 5148 | } |
| 5149 | |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 5150 | if (enableLatchUnsignaledConfig == LatchUnsignaledConfig::AutoSingleLayer) { |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 5151 | if (!firstTransaction) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 5152 | SFTRACE_FORMAT_INSTANT("%s: false (LatchUnsignaledConfig::AutoSingleLayer; not first " |
| 5153 | "transaction)", |
| 5154 | __func__); |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 5155 | return false; |
| 5156 | } |
| 5157 | |
| 5158 | // We don't want to latch unsignaled if are in early / client composition |
| 5159 | // as it leads to jank due to RenderEngine waiting for unsignaled buffer |
| 5160 | // or window animations being slow. |
Dominik Laskowski | 14956dc | 2023-02-22 13:43:57 -0500 | [diff] [blame] | 5161 | if (mScheduler->vsyncModulator().isVsyncConfigEarly()) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 5162 | SFTRACE_FORMAT_INSTANT("%s: false (LatchUnsignaledConfig::AutoSingleLayer; " |
| 5163 | "isVsyncConfigEarly)", |
| 5164 | __func__); |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 5165 | return false; |
| 5166 | } |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 5167 | } |
| 5168 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 5169 | return true; |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 5170 | } |
| 5171 | |
chaviw | 308ddba | 2020-08-11 16:23:51 -0700 | [diff] [blame] | 5172 | status_t SurfaceFlinger::setTransactionState( |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 5173 | const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& states, |
Patrick Williams | b6ddf52 | 2024-06-24 18:56:08 -0500 | [diff] [blame] | 5174 | Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken, |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 5175 | InputWindowCommands inputWindowCommands, int64_t desiredPresentTime, bool isAutoTimestamp, |
| 5176 | const std::vector<client_cache_t>& uncacheBuffers, bool hasListenerCallbacks, |
Pablo Gamito | 23780be | 2023-04-18 08:30:00 +0000 | [diff] [blame] | 5177 | const std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId, |
| 5178 | const std::vector<uint64_t>& mergedTransactionIds) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 5179 | SFTRACE_CALL(); |
Robert Carr | 14167e0 | 2019-02-13 13:50:55 -0800 | [diff] [blame] | 5180 | |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 5181 | IPCThreadState* ipc = IPCThreadState::self(); |
| 5182 | const int originPid = ipc->getCallingPid(); |
| 5183 | const int originUid = ipc->getCallingUid(); |
| 5184 | uint32_t permissions = LayerStatePermissions::getTransactionPermissions(originPid, originUid); |
Patrick Williams | 04e4176 | 2024-04-23 19:05:38 -0500 | [diff] [blame] | 5185 | for (auto& composerState : states) { |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 5186 | composerState.state.sanitize(permissions); |
Vishnu Nair | 24e3bba | 2021-02-23 14:19:36 -0800 | [diff] [blame] | 5187 | } |
| 5188 | |
Patrick Williams | b6ddf52 | 2024-06-24 18:56:08 -0500 | [diff] [blame] | 5189 | for (DisplayState& display : displays) { |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 5190 | display.sanitize(permissions); |
| 5191 | } |
| 5192 | |
| 5193 | if (!inputWindowCommands.empty() && |
| 5194 | (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) == 0) { |
| 5195 | ALOGE("Only privileged callers are allowed to send input commands."); |
| 5196 | inputWindowCommands.clear(); |
Leon Scroggins | 9a20f72 | 2021-12-28 14:43:12 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
Vishnu Nair | 884549f | 2022-12-01 17:20:59 -0800 | [diff] [blame] | 5199 | if (flags & (eEarlyWakeupStart | eEarlyWakeupEnd)) { |
| 5200 | const bool hasPermission = |
| 5201 | (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) || |
| 5202 | callingThreadHasPermission(sWakeupSurfaceFlinger); |
| 5203 | if (!hasPermission) { |
| 5204 | ALOGE("Caller needs permission android.permission.WAKEUP_SURFACE_FLINGER to use " |
| 5205 | "eEarlyWakeup[Start|End] flags"); |
| 5206 | flags &= ~(eEarlyWakeupStart | eEarlyWakeupEnd); |
| 5207 | } |
arthurhung | f1b7c7b | 2021-03-03 17:42:23 +0800 | [diff] [blame] | 5208 | } |
| 5209 | |
Vishnu Nair | 24e3bba | 2021-02-23 14:19:36 -0800 | [diff] [blame] | 5210 | const int64_t postTime = systemTime(); |
| 5211 | |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 5212 | std::vector<uint64_t> uncacheBufferIds; |
| 5213 | uncacheBufferIds.reserve(uncacheBuffers.size()); |
| 5214 | for (const auto& uncacheBuffer : uncacheBuffers) { |
| 5215 | sp<GraphicBuffer> buffer = ClientCache::getInstance().erase(uncacheBuffer); |
| 5216 | if (buffer != nullptr) { |
| 5217 | uncacheBufferIds.push_back(buffer->getId()); |
| 5218 | } |
| 5219 | } |
| 5220 | |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 5221 | std::vector<ResolvedComposerState> resolvedStates; |
| 5222 | resolvedStates.reserve(states.size()); |
| 5223 | for (auto& state : states) { |
| 5224 | resolvedStates.emplace_back(std::move(state)); |
| 5225 | auto& resolvedState = resolvedStates.back(); |
| 5226 | if (resolvedState.state.hasBufferChanges() && resolvedState.state.hasValidBuffer() && |
| 5227 | resolvedState.state.surface) { |
Vishnu Nair | c6086dd | 2022-11-04 04:39:35 +0000 | [diff] [blame] | 5228 | sp<Layer> layer = LayerHandle::getLayer(resolvedState.state.surface); |
| 5229 | std::string layerName = (layer) ? |
| 5230 | layer->getDebugName() : std::to_string(resolvedState.state.layerId); |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 5231 | resolvedState.externalTexture = |
| 5232 | getExternalTextureFromBufferData(*resolvedState.state.bufferData, |
Vishnu Nair | c6086dd | 2022-11-04 04:39:35 +0000 | [diff] [blame] | 5233 | layerName.c_str(), transactionId); |
Patrick Williams | 38d920e | 2023-08-31 17:31:18 -0500 | [diff] [blame] | 5234 | if (resolvedState.externalTexture) { |
| 5235 | resolvedState.state.bufferData->buffer = resolvedState.externalTexture->getBuffer(); |
| 5236 | } |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 5237 | mBufferCountTracker.increment(resolvedState.state.surface->localBinder()); |
| 5238 | } |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 5239 | resolvedState.layerId = LayerHandle::getLayerId(resolvedState.state.surface); |
| 5240 | if (resolvedState.state.what & layer_state_t::eReparent) { |
| 5241 | resolvedState.parentId = |
| 5242 | getLayerIdFromSurfaceControl(resolvedState.state.parentSurfaceControlForChild); |
| 5243 | } |
| 5244 | if (resolvedState.state.what & layer_state_t::eRelativeLayerChanged) { |
| 5245 | resolvedState.relativeParentId = |
| 5246 | getLayerIdFromSurfaceControl(resolvedState.state.relativeLayerSurfaceControl); |
| 5247 | } |
| 5248 | if (resolvedState.state.what & layer_state_t::eInputInfoChanged) { |
| 5249 | wp<IBinder>& touchableRegionCropHandle = |
| 5250 | resolvedState.state.windowInfoHandle->editInfo()->touchableRegionCropHandle; |
| 5251 | resolvedState.touchCropId = |
| 5252 | LayerHandle::getLayerId(touchableRegionCropHandle.promote()); |
| 5253 | } |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 5256 | TransactionState state{frameTimelineInfo, |
| 5257 | resolvedStates, |
| 5258 | displays, |
| 5259 | flags, |
| 5260 | applyToken, |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 5261 | std::move(inputWindowCommands), |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 5262 | desiredPresentTime, |
| 5263 | isAutoTimestamp, |
| 5264 | std::move(uncacheBufferIds), |
| 5265 | postTime, |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 5266 | hasListenerCallbacks, |
| 5267 | listenerCallbacks, |
| 5268 | originPid, |
| 5269 | originUid, |
Pablo Gamito | 23780be | 2023-04-18 08:30:00 +0000 | [diff] [blame] | 5270 | transactionId, |
| 5271 | mergedTransactionIds}; |
Vishnu Nair | 83df034 | 2021-03-30 11:50:44 -0700 | [diff] [blame] | 5272 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 5273 | if (mTransactionTracing) { |
| 5274 | mTransactionTracing->addQueuedTransaction(state); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 5275 | } |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 5276 | |
| 5277 | const auto schedule = [](uint32_t flags) { |
| 5278 | if (flags & eEarlyWakeupEnd) return TransactionSchedule::EarlyEnd; |
| 5279 | if (flags & eEarlyWakeupStart) return TransactionSchedule::EarlyStart; |
| 5280 | return TransactionSchedule::Late; |
| 5281 | }(state.flags); |
| 5282 | |
| 5283 | const auto frameHint = state.isFrameActive() ? FrameHint::kActive : FrameHint::kNone; |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5284 | { |
| 5285 | // Transactions are added via a lockless queue and does not need to be added from the main |
| 5286 | // thread. |
| 5287 | ftl::FakeGuard guard(kMainThreadContext); |
| 5288 | mTransactionHandler.queueTransaction(std::move(state)); |
| 5289 | } |
| 5290 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 5291 | for (const auto& [displayId, data] : mNotifyExpectedPresentMap) { |
| 5292 | if (data.hintStatus.load() == NotifyExpectedPresentHintStatus::ScheduleOnTx) { |
| 5293 | scheduleNotifyExpectedPresentHint(displayId, VsyncId{frameTimelineInfo.vsyncId}); |
| 5294 | } |
| 5295 | } |
Vishnu Nair | e4f00c5 | 2023-01-31 20:20:18 -0800 | [diff] [blame] | 5296 | setTransactionFlags(eTransactionFlushNeeded, schedule, applyToken, frameHint); |
Zhuoyao Zhang | 3d3540d | 2021-01-14 05:14:54 +0000 | [diff] [blame] | 5297 | return NO_ERROR; |
| 5298 | } |
Marissa Wall | 713b63f | 2018-10-17 15:42:43 -0700 | [diff] [blame] | 5299 | |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 5300 | bool SurfaceFlinger::applyTransactionState(const FrameTimelineInfo& frameTimelineInfo, |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 5301 | std::vector<ResolvedComposerState>& states, |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 5302 | Vector<DisplayState>& displays, uint32_t flags, |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 5303 | const InputWindowCommands& inputWindowCommands, |
| 5304 | const int64_t desiredPresentTime, bool isAutoTimestamp, |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 5305 | const std::vector<uint64_t>& uncacheBufferIds, |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 5306 | const int64_t postTime, bool hasListenerCallbacks, |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 5307 | const std::vector<ListenerCallbacks>& listenerCallbacks, |
| 5308 | int originPid, int originUid, uint64_t transactionId) { |
Zhuoyao Zhang | 3d3540d | 2021-01-14 05:14:54 +0000 | [diff] [blame] | 5309 | uint32_t transactionFlags = 0; |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 5310 | if (!mLayerLifecycleManagerEnabled) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5311 | for (DisplayState& display : displays) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5312 | transactionFlags |= setDisplayStateLocked(display); |
| 5313 | } |
Jamie Gennis | b8d69a5 | 2011-10-10 15:48:06 -0700 | [diff] [blame] | 5314 | } |
| 5315 | |
Jiakai Zhang | a5505cb | 2021-11-09 11:46:30 +0000 | [diff] [blame] | 5316 | // start and end registration for listeners w/ no surface so they can get their callback. Note |
| 5317 | // that listeners with SurfaceControls will start registration during setClientStateLocked |
| 5318 | // below. |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 5319 | for (const auto& listener : listenerCallbacks) { |
Jiakai Zhang | a5505cb | 2021-11-09 11:46:30 +0000 | [diff] [blame] | 5320 | mTransactionCallbackInvoker.addEmptyTransaction(listener); |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 5321 | } |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 5322 | nsecs_t now = systemTime(); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 5323 | uint32_t clientStateFlags = 0; |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 5324 | for (auto& resolvedState : states) { |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 5325 | clientStateFlags |= |
| 5326 | updateLayerCallbacksAndStats(frameTimelineInfo, resolvedState, desiredPresentTime, |
| 5327 | isAutoTimestamp, postTime, transactionId); |
Melody Hsu | 2606ec0 | 2024-04-04 22:31:34 +0000 | [diff] [blame] | 5328 | if (!mLayerLifecycleManagerEnabled) { |
| 5329 | if ((flags & eAnimation) && resolvedState.state.surface) { |
| 5330 | if (const auto layer = LayerHandle::getLayer(resolvedState.state.surface)) { |
| 5331 | const auto layerProps = scheduler::LayerProps{ |
| 5332 | .visible = layer->isVisible(), |
| 5333 | .bounds = layer->getBounds(), |
| 5334 | .transform = layer->getTransform(), |
| 5335 | .setFrameRateVote = layer->getFrameRateForLayerTree(), |
| 5336 | .frameRateSelectionPriority = layer->getFrameRateSelectionPriority(), |
| 5337 | .isFrontBuffered = layer->isFrontBuffered(), |
| 5338 | }; |
| 5339 | layer->recordLayerHistoryAnimationTx(layerProps, now); |
| 5340 | } |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 5341 | } |
| 5342 | } |
Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 5343 | } |
| 5344 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 5345 | transactionFlags |= clientStateFlags; |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 5346 | transactionFlags |= addInputWindowCommands(inputWindowCommands); |
chaviw | 273171b | 2018-12-26 11:46:30 -0800 | [diff] [blame] | 5347 | |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 5348 | for (uint64_t uncacheBufferId : uncacheBufferIds) { |
| 5349 | mBufferIdsToUncache.push_back(uncacheBufferId); |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame] | 5350 | } |
| 5351 | |
Jorim Jaggi | bdcf09c | 2017-08-08 15:22:08 +0200 | [diff] [blame] | 5352 | // If a synchronous transaction is explicitly requested without any changes, force a transaction |
| 5353 | // anyway. This can be used as a flush mechanism for previous async transactions. |
| 5354 | // Empty animation transaction can be used to simulate back-pressure, so also force a |
| 5355 | // transaction for empty animation transactions. |
Vishnu Nair | 1523dad | 2022-09-29 16:05:18 -0700 | [diff] [blame] | 5356 | if (transactionFlags == 0 && (flags & eAnimation)) { |
Robert Carr | 2a7dbb4 | 2016-05-24 11:41:28 -0700 | [diff] [blame] | 5357 | transactionFlags = eTransactionNeeded; |
| 5358 | } |
| 5359 | |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 5360 | bool needsTraversal = false; |
Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 5361 | if (transactionFlags) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5362 | // We are on the main thread, we are about to perform a traversal. Clear the traversal bit |
| 5363 | // so we don't have to wake up again next frame to perform an unnecessary traversal. |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 5364 | if (transactionFlags & eTraversalNeeded) { |
| 5365 | transactionFlags = transactionFlags & (~eTraversalNeeded); |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 5366 | needsTraversal = true; |
Arthur Hung | 1bedccb | 2020-09-24 10:09:25 +0000 | [diff] [blame] | 5367 | } |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 5368 | if (transactionFlags) { |
| 5369 | setTransactionFlags(transactionFlags); |
Arthur Hung | 1bedccb | 2020-09-24 10:09:25 +0000 | [diff] [blame] | 5370 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5371 | } |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 5372 | |
| 5373 | return needsTraversal; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5374 | } |
| 5375 | |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 5376 | bool SurfaceFlinger::applyAndCommitDisplayTransactionStatesLocked( |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5377 | std::vector<TransactionState>& transactions) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5378 | bool needsTraversal = false; |
| 5379 | uint32_t transactionFlags = 0; |
| 5380 | for (auto& transaction : transactions) { |
| 5381 | for (DisplayState& display : transaction.displays) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5382 | transactionFlags |= setDisplayStateLocked(display); |
| 5383 | } |
| 5384 | } |
| 5385 | |
| 5386 | if (transactionFlags) { |
| 5387 | // We are on the main thread, we are about to perform a traversal. Clear the traversal bit |
| 5388 | // so we don't have to wake up again next frame to perform an unnecessary traversal. |
| 5389 | if (transactionFlags & eTraversalNeeded) { |
| 5390 | transactionFlags = transactionFlags & (~eTraversalNeeded); |
| 5391 | needsTraversal = true; |
| 5392 | } |
| 5393 | if (transactionFlags) { |
| 5394 | setTransactionFlags(transactionFlags); |
| 5395 | } |
| 5396 | } |
| 5397 | |
| 5398 | mFrontEndDisplayInfosChanged = mTransactionFlags & eDisplayTransactionNeeded; |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 5399 | if (mFrontEndDisplayInfosChanged) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5400 | processDisplayChangesLocked(); |
| 5401 | mFrontEndDisplayInfos.clear(); |
| 5402 | for (const auto& [_, display] : mDisplays) { |
| 5403 | mFrontEndDisplayInfos.try_emplace(display->getLayerStack(), display->getFrontEndInfo()); |
| 5404 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 5405 | needsTraversal = true; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
| 5408 | return needsTraversal; |
| 5409 | } |
| 5410 | |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 5411 | uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s) { |
| 5412 | const ssize_t index = mCurrentState.displays.indexOfKey(s.token); |
| 5413 | if (index < 0) return 0; |
Jesse Hall | 9a14392 | 2012-10-04 16:29:19 -0700 | [diff] [blame] | 5414 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 5415 | uint32_t flags = 0; |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 5416 | DisplayDeviceState& state = mCurrentState.displays.editValueAt(index); |
| 5417 | |
| 5418 | const uint32_t what = s.what; |
| 5419 | if (what & DisplayState::eSurfaceChanged) { |
| 5420 | if (IInterface::asBinder(state.surface) != IInterface::asBinder(s.surface)) { |
| 5421 | state.surface = s.surface; |
| 5422 | flags |= eDisplayTransactionNeeded; |
Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 5423 | } |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 5424 | } |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 5425 | if (what & DisplayState::eLayerStackChanged) { |
| 5426 | if (state.layerStack != s.layerStack) { |
| 5427 | state.layerStack = s.layerStack; |
| 5428 | flags |= eDisplayTransactionNeeded; |
| 5429 | } |
| 5430 | } |
Evan Rosky | 2239b37 | 2021-05-20 13:43:47 -0700 | [diff] [blame] | 5431 | if (what & DisplayState::eFlagsChanged) { |
| 5432 | if (state.flags != s.flags) { |
| 5433 | state.flags = s.flags; |
| 5434 | flags |= eDisplayTransactionNeeded; |
| 5435 | } |
| 5436 | } |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 5437 | if (what & DisplayState::eDisplayProjectionChanged) { |
| 5438 | if (state.orientation != s.orientation) { |
| 5439 | state.orientation = s.orientation; |
| 5440 | flags |= eDisplayTransactionNeeded; |
| 5441 | } |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 5442 | if (state.orientedDisplaySpaceRect != s.orientedDisplaySpaceRect) { |
| 5443 | state.orientedDisplaySpaceRect = s.orientedDisplaySpaceRect; |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 5444 | flags |= eDisplayTransactionNeeded; |
| 5445 | } |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 5446 | if (state.layerStackSpaceRect != s.layerStackSpaceRect) { |
| 5447 | state.layerStackSpaceRect = s.layerStackSpaceRect; |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 5448 | flags |= eDisplayTransactionNeeded; |
| 5449 | } |
| 5450 | } |
| 5451 | if (what & DisplayState::eDisplaySizeChanged) { |
| 5452 | if (state.width != s.width) { |
| 5453 | state.width = s.width; |
| 5454 | flags |= eDisplayTransactionNeeded; |
| 5455 | } |
| 5456 | if (state.height != s.height) { |
| 5457 | state.height = s.height; |
| 5458 | flags |= eDisplayTransactionNeeded; |
| 5459 | } |
| 5460 | } |
| 5461 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 5462 | return flags; |
| 5463 | } |
| 5464 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 5465 | bool SurfaceFlinger::callingThreadHasUnscopedSurfaceFlingerAccess(bool usePermissionCache) { |
Robert Carr | d4ae7f3 | 2018-06-07 16:10:57 -0700 | [diff] [blame] | 5466 | IPCThreadState* ipc = IPCThreadState::self(); |
| 5467 | const int pid = ipc->getCallingPid(); |
| 5468 | const int uid = ipc->getCallingUid(); |
Robert Carr | d4ae7f3 | 2018-06-07 16:10:57 -0700 | [diff] [blame] | 5469 | if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) && |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 5470 | (usePermissionCache ? !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid) |
| 5471 | : !checkPermission(sAccessSurfaceFlinger, pid, uid))) { |
Robert Carr | d4ae7f3 | 2018-06-07 16:10:57 -0700 | [diff] [blame] | 5472 | return false; |
| 5473 | } |
| 5474 | return true; |
| 5475 | } |
| 5476 | |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 5477 | uint32_t SurfaceFlinger::setClientStateLocked(const FrameTimelineInfo& frameTimelineInfo, |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 5478 | ResolvedComposerState& composerState, |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 5479 | int64_t desiredPresentTime, bool isAutoTimestamp, |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 5480 | int64_t postTime, uint64_t transactionId) { |
Robert Carr | de6d7b4 | 2022-01-07 18:23:06 -0800 | [diff] [blame] | 5481 | layer_state_t& s = composerState.state; |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 5482 | |
| 5483 | std::vector<ListenerCallbacks> filteredListeners; |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 5484 | for (auto& listener : s.listeners) { |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 5485 | // Starts a registration but separates the callback ids according to callback type. This |
| 5486 | // allows the callback invoker to send on latch callbacks earlier. |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 5487 | // note that startRegistration will not re-register if the listener has |
| 5488 | // already be registered for a prior surface control |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 5489 | |
| 5490 | ListenerCallbacks onCommitCallbacks = listener.filter(CallbackId::Type::ON_COMMIT); |
| 5491 | if (!onCommitCallbacks.callbackIds.empty()) { |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 5492 | filteredListeners.push_back(onCommitCallbacks); |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | ListenerCallbacks onCompleteCallbacks = listener.filter(CallbackId::Type::ON_COMPLETE); |
| 5496 | if (!onCompleteCallbacks.callbackIds.empty()) { |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 5497 | filteredListeners.push_back(onCompleteCallbacks); |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 5498 | } |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 5499 | } |
| 5500 | |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 5501 | const uint64_t what = s.what; |
| 5502 | uint32_t flags = 0; |
Vishnu Nair | dc6f5b9 | 2019-12-03 07:33:37 -0800 | [diff] [blame] | 5503 | sp<Layer> layer = nullptr; |
| 5504 | if (s.surface) { |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 5505 | layer = LayerHandle::getLayer(s.surface); |
Vishnu Nair | dc6f5b9 | 2019-12-03 07:33:37 -0800 | [diff] [blame] | 5506 | } else { |
| 5507 | // The client may provide us a null handle. Treat it as if the layer was removed. |
| 5508 | ALOGW("Attempt to set client state with a null layer handle"); |
| 5509 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5510 | if (layer == nullptr) { |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 5511 | for (auto& [listener, callbackIds] : s.listeners) { |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 5512 | mTransactionCallbackInvoker.addCallbackHandle( |
| 5513 | sp<CallbackHandle>::make(listener, callbackIds, s.surface)); |
Marissa Wall | 88e2048 | 2019-06-24 10:49:42 -0700 | [diff] [blame] | 5514 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5515 | return 0; |
| 5516 | } |
Josh Gao | 194ff39 | 2022-09-08 16:19:29 -0700 | [diff] [blame] | 5517 | MUTEX_ALIAS(mStateLock, layer->mFlinger->mStateLock); |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5518 | |
Vishnu Nair | 73908d1 | 2022-10-24 21:46:42 -0700 | [diff] [blame] | 5519 | ui::LayerStack oldLayerStack = layer->getLayerStack(LayerVector::StateSet::Current); |
Vishnu Nair | afd527a | 2022-10-18 08:56:10 -0700 | [diff] [blame] | 5520 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 5521 | // Only set by BLAST adapter layers |
| 5522 | if (what & layer_state_t::eProducerDisconnect) { |
| 5523 | layer->onDisconnect(); |
| 5524 | } |
| 5525 | |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5526 | if (what & layer_state_t::ePositionChanged) { |
chaviw | 214c89d | 2019-09-04 16:03:53 -0700 | [diff] [blame] | 5527 | if (layer->setPosition(s.x, s.y)) { |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5528 | flags |= eTraversalNeeded; |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 5529 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5530 | } |
| 5531 | if (what & layer_state_t::eLayerChanged) { |
| 5532 | // NOTE: index needs to be calculated before we update the state |
Rob Carr | c6d2d2b | 2021-10-25 16:51:49 +0000 | [diff] [blame] | 5533 | const auto& p = layer->getParent(); |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5534 | if (p == nullptr) { |
chaviw | 64f7b42 | 2017-07-12 10:31:58 -0700 | [diff] [blame] | 5535 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5536 | if (layer->setLayer(s.z) && idx >= 0) { |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 5537 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 5538 | mCurrentState.layersSortedByZ.add(layer); |
| 5539 | // we need traversal (state changed) |
| 5540 | // AND transaction (list changed) |
| 5541 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 5542 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5543 | } else { |
| 5544 | if (p->setChildLayer(layer, s.z)) { |
chaviw | 0617894 | 2017-07-27 10:25:59 -0700 | [diff] [blame] | 5545 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 5546 | } |
| 5547 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5548 | } |
| 5549 | if (what & layer_state_t::eRelativeLayerChanged) { |
Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 5550 | // NOTE: index needs to be calculated before we update the state |
Rob Carr | c6d2d2b | 2021-10-25 16:51:49 +0000 | [diff] [blame] | 5551 | const auto& p = layer->getParent(); |
Robert Carr | bc38496 | 2021-01-27 15:44:50 -0800 | [diff] [blame] | 5552 | const auto& relativeHandle = s.relativeLayerSurfaceControl ? |
| 5553 | s.relativeLayerSurfaceControl->getHandle() : nullptr; |
Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 5554 | if (p == nullptr) { |
| 5555 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); |
Robert Carr | bc38496 | 2021-01-27 15:44:50 -0800 | [diff] [blame] | 5556 | if (layer->setRelativeLayer(relativeHandle, s.z) && |
Pablo Gamito | 11dcc22 | 2020-09-12 15:49:39 +0000 | [diff] [blame] | 5557 | idx >= 0) { |
Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 5558 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 5559 | mCurrentState.layersSortedByZ.add(layer); |
| 5560 | // we need traversal (state changed) |
| 5561 | // AND transaction (list changed) |
| 5562 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 5563 | } |
| 5564 | } else { |
Robert Carr | bc38496 | 2021-01-27 15:44:50 -0800 | [diff] [blame] | 5565 | if (p->setChildRelativeLayer(layer, relativeHandle, s.z)) { |
Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 5566 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 5567 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5568 | } |
| 5569 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5570 | if (what & layer_state_t::eAlphaChanged) { |
Vishnu Nair | bbceb46 | 2022-10-10 04:52:13 +0000 | [diff] [blame] | 5571 | if (layer->setAlpha(s.color.a)) flags |= eTraversalNeeded; |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5572 | } |
| 5573 | if (what & layer_state_t::eColorChanged) { |
Vishnu Nair | bbceb46 | 2022-10-10 04:52:13 +0000 | [diff] [blame] | 5574 | if (layer->setColor(s.color.rgb)) flags |= eTraversalNeeded; |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5575 | } |
Peiyong Lin | d378863 | 2018-09-18 16:01:31 -0700 | [diff] [blame] | 5576 | if (what & layer_state_t::eColorTransformChanged) { |
| 5577 | if (layer->setColorTransform(s.colorTransform)) { |
| 5578 | flags |= eTraversalNeeded; |
| 5579 | } |
| 5580 | } |
Valerie Hau | dd0b757 | 2019-01-29 14:59:27 -0800 | [diff] [blame] | 5581 | if (what & layer_state_t::eBackgroundColorChanged) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 5582 | if (layer->setBackgroundColor(s.bgColor.rgb, s.bgColor.a, s.bgColorDataspace)) { |
Valerie Hau | dd0b757 | 2019-01-29 14:59:27 -0800 | [diff] [blame] | 5583 | flags |= eTraversalNeeded; |
| 5584 | } |
| 5585 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5586 | if (what & layer_state_t::eMatrixChanged) { |
Robert Carr | de6d7b4 | 2022-01-07 18:23:06 -0800 | [diff] [blame] | 5587 | if (layer->setMatrix(s.matrix)) flags |= eTraversalNeeded; |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5588 | } |
| 5589 | if (what & layer_state_t::eTransparentRegionChanged) { |
| 5590 | if (layer->setTransparentRegionHint(s.transparentRegion)) |
| 5591 | flags |= eTraversalNeeded; |
| 5592 | } |
| 5593 | if (what & layer_state_t::eFlagsChanged) { |
Robert Carr | de6d7b4 | 2022-01-07 18:23:06 -0800 | [diff] [blame] | 5594 | if (layer->setFlags(s.flags, s.mask)) flags |= eTraversalNeeded; |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5595 | } |
Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 5596 | if (what & layer_state_t::eCornerRadiusChanged) { |
| 5597 | if (layer->setCornerRadius(s.cornerRadius)) |
| 5598 | flags |= eTraversalNeeded; |
| 5599 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 5600 | if (what & layer_state_t::eBackgroundBlurRadiusChanged && mSupportsBlur) { |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 5601 | if (layer->setBackgroundBlurRadius(s.backgroundBlurRadius)) flags |= eTraversalNeeded; |
| 5602 | } |
Galia Peycheva | e11d575 | 2021-01-22 13:50:16 +0000 | [diff] [blame] | 5603 | if (what & layer_state_t::eBlurRegionsChanged) { |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 5604 | if (layer->setBlurRegions(s.blurRegions)) flags |= eTraversalNeeded; |
| 5605 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5606 | if (what & layer_state_t::eLayerStackChanged) { |
| 5607 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); |
| 5608 | // We only allow setting layer stacks for top level layers, |
| 5609 | // everything else inherits layer stack from its parent. |
| 5610 | if (layer->hasParent()) { |
| 5611 | ALOGE("Attempt to set layer stack on layer with parent (%s) is invalid", |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 5612 | layer->getDebugName()); |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5613 | } else if (idx < 0) { |
| 5614 | ALOGE("Attempt to set layer stack on layer without parent (%s) that " |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 5615 | "that also does not appear in the top level layer list. Something" |
| 5616 | " has gone wrong.", |
| 5617 | layer->getDebugName()); |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5618 | } else if (layer->setLayerStack(s.layerStack)) { |
| 5619 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 5620 | mCurrentState.layersSortedByZ.add(layer); |
| 5621 | // we need traversal (state changed) |
| 5622 | // AND transaction (list changed) |
Vishnu Nair | 6213bd9 | 2020-05-08 17:42:25 -0700 | [diff] [blame] | 5623 | flags |= eTransactionNeeded | eTraversalNeeded | eTransformHintUpdateNeeded; |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 5624 | } |
| 5625 | } |
Vishnu Nair | bbceb46 | 2022-10-10 04:52:13 +0000 | [diff] [blame] | 5626 | if (what & layer_state_t::eBufferTransformChanged) { |
| 5627 | if (layer->setTransform(s.bufferTransform)) flags |= eTraversalNeeded; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 5628 | } |
| 5629 | if (what & layer_state_t::eTransformToDisplayInverseChanged) { |
| 5630 | if (layer->setTransformToDisplayInverse(s.transformToDisplayInverse)) |
| 5631 | flags |= eTraversalNeeded; |
| 5632 | } |
| 5633 | if (what & layer_state_t::eCropChanged) { |
| 5634 | if (layer->setCrop(s.crop)) flags |= eTraversalNeeded; |
| 5635 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 5636 | if (what & layer_state_t::eDataspaceChanged) { |
| 5637 | if (layer->setDataspace(s.dataspace)) flags |= eTraversalNeeded; |
| 5638 | } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 5639 | if (what & layer_state_t::eSurfaceDamageRegionChanged) { |
| 5640 | if (layer->setSurfaceDamageRegion(s.surfaceDamageRegion)) flags |= eTraversalNeeded; |
| 5641 | } |
| 5642 | if (what & layer_state_t::eApiChanged) { |
| 5643 | if (layer->setApi(s.api)) flags |= eTraversalNeeded; |
| 5644 | } |
| 5645 | if (what & layer_state_t::eSidebandStreamChanged) { |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 5646 | if (layer->setSidebandStream(s.sidebandStream, frameTimelineInfo, postTime)) |
| 5647 | flags |= eTraversalNeeded; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 5648 | } |
Robert Carr | 720e506 | 2018-07-30 17:45:14 -0700 | [diff] [blame] | 5649 | if (what & layer_state_t::eInputInfoChanged) { |
Robert Carr | de6d7b4 | 2022-01-07 18:23:06 -0800 | [diff] [blame] | 5650 | layer->setInputInfo(*s.windowInfoHandle->getInfo()); |
| 5651 | flags |= eTraversalNeeded; |
Robert Carr | 720e506 | 2018-07-30 17:45:14 -0700 | [diff] [blame] | 5652 | } |
Evan Rosky | 1f6d6d5 | 2018-12-06 10:47:26 -0800 | [diff] [blame] | 5653 | if (what & layer_state_t::eMetadataChanged) { |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 5654 | if (const int32_t gameMode = s.metadata.getInt32(gui::METADATA_GAME_MODE, -1); |
| 5655 | gameMode != -1) { |
Adithya Srinivasan | ac977e6 | 2021-05-21 22:50:56 +0000 | [diff] [blame] | 5656 | // The transaction will be received on the Task layer and needs to be applied to all |
| 5657 | // child layers. Child layers that are added at a later point will obtain the game mode |
| 5658 | // info through addChild(). |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 5659 | layer->setGameModeForTree(static_cast<GameMode>(gameMode)); |
Adithya Srinivasan | ac977e6 | 2021-05-21 22:50:56 +0000 | [diff] [blame] | 5660 | } |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 5661 | |
Vishnu Nair | 0a4fb00 | 2022-08-08 02:40:42 +0000 | [diff] [blame] | 5662 | if (layer->setMetadata(s.metadata)) { |
| 5663 | flags |= eTraversalNeeded; |
| 5664 | mLayerMetadataSnapshotNeeded = true; |
| 5665 | } |
Evan Rosky | 1f6d6d5 | 2018-12-06 10:47:26 -0800 | [diff] [blame] | 5666 | } |
Peiyong Lin | c502cb7 | 2019-03-01 15:00:23 -0800 | [diff] [blame] | 5667 | if (what & layer_state_t::eColorSpaceAgnosticChanged) { |
| 5668 | if (layer->setColorSpaceAgnostic(s.colorSpaceAgnostic)) { |
| 5669 | flags |= eTraversalNeeded; |
| 5670 | } |
| 5671 | } |
Vishnu Nair | c97b8db | 2019-10-29 18:19:35 -0700 | [diff] [blame] | 5672 | if (what & layer_state_t::eShadowRadiusChanged) { |
| 5673 | if (layer->setShadowRadius(s.shadowRadius)) flags |= eTraversalNeeded; |
| 5674 | } |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 5675 | if (what & layer_state_t::eDefaultFrameRateCompatibilityChanged) { |
| 5676 | const auto compatibility = |
| 5677 | Layer::FrameRate::convertCompatibility(s.defaultFrameRateCompatibility); |
| 5678 | |
| 5679 | if (layer->setDefaultFrameRateCompatibility(compatibility)) { |
| 5680 | flags |= eTraversalNeeded; |
| 5681 | } |
| 5682 | } |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 5683 | if (what & layer_state_t::eFrameRateSelectionPriority) { |
Robert Carr | de6d7b4 | 2022-01-07 18:23:06 -0800 | [diff] [blame] | 5684 | if (layer->setFrameRateSelectionPriority(s.frameRateSelectionPriority)) { |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 5685 | flags |= eTraversalNeeded; |
| 5686 | } |
| 5687 | } |
Steven Thomas | 3172e20 | 2020-01-06 19:25:30 -0800 | [diff] [blame] | 5688 | if (what & layer_state_t::eFrameRateChanged) { |
Robert Carr | de6d7b4 | 2022-01-07 18:23:06 -0800 | [diff] [blame] | 5689 | const auto compatibility = |
| 5690 | Layer::FrameRate::convertCompatibility(s.frameRateCompatibility); |
| 5691 | const auto strategy = |
| 5692 | Layer::FrameRate::convertChangeFrameRateStrategy(s.changeFrameRateStrategy); |
Marin Shalamanov | c598677 | 2021-03-16 16:09:49 +0100 | [diff] [blame] | 5693 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 5694 | if (layer->setFrameRate(Layer::FrameRate::FrameRateVote(Fps::fromValue(s.frameRate), |
| 5695 | compatibility, strategy))) { |
| 5696 | flags |= eTraversalNeeded; |
| 5697 | } |
| 5698 | } |
| 5699 | if (what & layer_state_t::eFrameRateCategoryChanged) { |
| 5700 | const FrameRateCategory category = Layer::FrameRate::convertCategory(s.frameRateCategory); |
Rachel Lee | 67afbea | 2023-09-28 15:35:07 -0700 | [diff] [blame] | 5701 | if (layer->setFrameRateCategory(category, s.frameRateCategorySmoothSwitchOnly)) { |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 5702 | flags |= eTraversalNeeded; |
Steven Thomas | 62a4cf8 | 2020-01-31 12:04:03 -0800 | [diff] [blame] | 5703 | } |
Steven Thomas | 3172e20 | 2020-01-06 19:25:30 -0800 | [diff] [blame] | 5704 | } |
Rachel Lee | 58cc90d | 2023-09-05 18:50:20 -0700 | [diff] [blame] | 5705 | if (what & layer_state_t::eFrameRateSelectionStrategyChanged) { |
| 5706 | const scheduler::LayerInfo::FrameRateSelectionStrategy strategy = |
| 5707 | scheduler::LayerInfo::convertFrameRateSelectionStrategy( |
| 5708 | s.frameRateSelectionStrategy); |
| 5709 | if (layer->setFrameRateSelectionStrategy(strategy)) { |
| 5710 | flags |= eTraversalNeeded; |
| 5711 | } |
| 5712 | } |
Vishnu Nair | 6213bd9 | 2020-05-08 17:42:25 -0700 | [diff] [blame] | 5713 | if (what & layer_state_t::eFixedTransformHintChanged) { |
| 5714 | if (layer->setFixedTransformHint(s.fixedTransformHint)) { |
| 5715 | flags |= eTraversalNeeded | eTransformHintUpdateNeeded; |
| 5716 | } |
| 5717 | } |
Vishnu Nair | cf26a0a | 2020-11-13 12:56:20 -0800 | [diff] [blame] | 5718 | if (what & layer_state_t::eAutoRefreshChanged) { |
| 5719 | layer->setAutoRefresh(s.autoRefresh); |
| 5720 | } |
Sally Qi | 421ffb0 | 2022-03-21 19:41:33 -0700 | [diff] [blame] | 5721 | if (what & layer_state_t::eDimmingEnabledChanged) { |
| 5722 | if (layer->setDimmingEnabled(s.dimmingEnabled)) flags |= eTraversalNeeded; |
| 5723 | } |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 5724 | if (what & layer_state_t::eExtendedRangeBrightnessChanged) { |
Sally Qi | 963049b | 2023-03-23 14:06:21 -0700 | [diff] [blame] | 5725 | if (layer->setExtendedRangeBrightness(s.currentHdrSdrRatio, s.desiredHdrSdrRatio)) { |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 5726 | flags |= eTraversalNeeded; |
| 5727 | } |
| 5728 | } |
Alec Mouri | 1b0d4e1 | 2024-02-12 22:27:19 +0000 | [diff] [blame] | 5729 | if (what & layer_state_t::eDesiredHdrHeadroomChanged) { |
| 5730 | if (layer->setDesiredHdrHeadroom(s.desiredHdrSdrRatio)) { |
| 5731 | flags |= eTraversalNeeded; |
| 5732 | } |
| 5733 | } |
Alec Mouri | f4af03e | 2023-02-11 00:25:24 +0000 | [diff] [blame] | 5734 | if (what & layer_state_t::eCachingHintChanged) { |
| 5735 | if (layer->setCachingHint(s.cachingHint)) { |
| 5736 | flags |= eTraversalNeeded; |
| 5737 | } |
| 5738 | } |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 5739 | if (what & layer_state_t::eHdrMetadataChanged) { |
| 5740 | if (layer->setHdrMetadata(s.hdrMetadata)) flags |= eTraversalNeeded; |
| 5741 | } |
Winson Chung | a30f7c9 | 2021-06-29 15:42:56 -0700 | [diff] [blame] | 5742 | if (what & layer_state_t::eTrustedOverlayChanged) { |
Vishnu Nair | 9e0017e | 2024-05-22 19:02:44 +0000 | [diff] [blame] | 5743 | if (layer->setTrustedOverlay(s.trustedOverlay == gui::TrustedOverlay::ENABLED)) { |
Robert Carr | de6d7b4 | 2022-01-07 18:23:06 -0800 | [diff] [blame] | 5744 | flags |= eTraversalNeeded; |
Winson Chung | a30f7c9 | 2021-06-29 15:42:56 -0700 | [diff] [blame] | 5745 | } |
| 5746 | } |
John Reck | cdb4ed7 | 2021-02-04 13:39:33 -0500 | [diff] [blame] | 5747 | if (what & layer_state_t::eStretchChanged) { |
| 5748 | if (layer->setStretchEffect(s.stretchEffect)) { |
| 5749 | flags |= eTraversalNeeded; |
| 5750 | } |
| 5751 | } |
chaviw | f3f40fe | 2021-04-27 15:54:02 -0500 | [diff] [blame] | 5752 | if (what & layer_state_t::eBufferCropChanged) { |
| 5753 | if (layer->setBufferCrop(s.bufferCrop)) { |
| 5754 | flags |= eTraversalNeeded; |
| 5755 | } |
| 5756 | } |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 5757 | if (what & layer_state_t::eDestinationFrameChanged) { |
| 5758 | if (layer->setDestinationFrame(s.destinationFrame)) { |
| 5759 | flags |= eTraversalNeeded; |
| 5760 | } |
| 5761 | } |
Vishnu Nair | 9cf4a4d | 2021-09-17 12:16:08 -0700 | [diff] [blame] | 5762 | if (what & layer_state_t::eDropInputModeChanged) { |
Robert Carr | de6d7b4 | 2022-01-07 18:23:06 -0800 | [diff] [blame] | 5763 | if (layer->setDropInputMode(s.dropInputMode)) { |
| 5764 | flags |= eTraversalNeeded; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 5765 | mUpdateInputInfo = true; |
Vishnu Nair | 9cf4a4d | 2021-09-17 12:16:08 -0700 | [diff] [blame] | 5766 | } |
| 5767 | } |
Vishnu Nair | 14d7692 | 2019-08-05 08:41:20 -0700 | [diff] [blame] | 5768 | // This has to happen after we reparent children because when we reparent to null we remove |
| 5769 | // child layers from current state and remove its relative z. If the children are reparented in |
| 5770 | // the same transaction, then we have to make sure we reparent the children first so we do not |
| 5771 | // lose its relative z order. |
| 5772 | if (what & layer_state_t::eReparent) { |
| 5773 | bool hadParent = layer->hasParent(); |
Pablo Gamito | 11dcc22 | 2020-09-12 15:49:39 +0000 | [diff] [blame] | 5774 | auto parentHandle = (s.parentSurfaceControlForChild) |
| 5775 | ? s.parentSurfaceControlForChild->getHandle() |
| 5776 | : nullptr; |
| 5777 | if (layer->reparent(parentHandle)) { |
Vishnu Nair | 14d7692 | 2019-08-05 08:41:20 -0700 | [diff] [blame] | 5778 | if (!hadParent) { |
Vishnu Nair | 14d218b | 2021-07-13 13:57:39 -0700 | [diff] [blame] | 5779 | layer->setIsAtRoot(false); |
Vishnu Nair | 14d7692 | 2019-08-05 08:41:20 -0700 | [diff] [blame] | 5780 | mCurrentState.layersSortedByZ.remove(layer); |
| 5781 | } |
| 5782 | flags |= eTransactionNeeded | eTraversalNeeded; |
| 5783 | } |
| 5784 | } |
Jiakai Zhang | a5505cb | 2021-11-09 11:46:30 +0000 | [diff] [blame] | 5785 | std::vector<sp<CallbackHandle>> callbackHandles; |
| 5786 | if ((what & layer_state_t::eHasListenerCallbacksChanged) && (!filteredListeners.empty())) { |
| 5787 | for (auto& [listener, callbackIds] : filteredListeners) { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 5788 | callbackHandles.emplace_back( |
| 5789 | sp<CallbackHandle>::make(listener, callbackIds, s.surface)); |
Jiakai Zhang | a5505cb | 2021-11-09 11:46:30 +0000 | [diff] [blame] | 5790 | } |
| 5791 | } |
Vishnu Nair | 6b7c5c9 | 2020-09-29 17:27:05 -0700 | [diff] [blame] | 5792 | |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 5793 | if (what & layer_state_t::eBufferChanged) { |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 5794 | if (layer->setBuffer(composerState.externalTexture, *s.bufferData, postTime, |
Nergi Rahardi | 39f510f | 2024-05-23 15:16:54 +0900 | [diff] [blame] | 5795 | desiredPresentTime, isAutoTimestamp, frameTimelineInfo)) { |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 5796 | flags |= eTraversalNeeded; |
| 5797 | } |
Ady Abraham | 8db1010 | 2021-03-15 17:19:23 -0700 | [diff] [blame] | 5798 | } else if (frameTimelineInfo.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) { |
| 5799 | layer->setFrameTimelineVsyncForBufferlessTransaction(frameTimelineInfo, postTime); |
Marissa Wall | ebc2c05 | 2019-01-16 19:16:55 -0800 | [diff] [blame] | 5800 | } |
Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 5801 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 5802 | if ((what & layer_state_t::eBufferChanged) == 0) { |
| 5803 | layer->setDesiredPresentTime(desiredPresentTime, isAutoTimestamp); |
| 5804 | } |
| 5805 | |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 5806 | if (what & layer_state_t::eTrustedPresentationInfoChanged) { |
Chavi Weingarten | 328a831 | 2023-01-26 21:17:52 +0000 | [diff] [blame] | 5807 | if (layer->setTrustedPresentationInfo(s.trustedPresentationThresholds, |
| 5808 | s.trustedPresentationListener)) { |
| 5809 | flags |= eTraversalNeeded; |
| 5810 | } |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 5811 | } |
| 5812 | |
Pascal Muetschard | b39918f | 2023-01-27 11:36:11 +0100 | [diff] [blame] | 5813 | if (what & layer_state_t::eFlushJankData) { |
| 5814 | // Do nothing. Processing the transaction completed listeners currently cause the flush. |
| 5815 | } |
| 5816 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5817 | if (layer->setTransactionCompletedListeners(callbackHandles, |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 5818 | layer->willPresentCurrentTransaction() || |
| 5819 | layer->willReleaseBufferOnLatch())) { |
Vishnu Nair | d691322 | 2023-02-01 22:56:21 +0000 | [diff] [blame] | 5820 | flags |= eTraversalNeeded; |
| 5821 | } |
| 5822 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 5823 | // Do not put anything that updates layer state or modifies flags after |
| 5824 | // setTransactionCompletedListener |
Vishnu Nair | afd527a | 2022-10-18 08:56:10 -0700 | [diff] [blame] | 5825 | |
| 5826 | // if the layer has been parented on to a new display, update its transform hint. |
Vishnu Nair | 73908d1 | 2022-10-24 21:46:42 -0700 | [diff] [blame] | 5827 | if (((flags & eTransformHintUpdateNeeded) == 0) && |
| 5828 | oldLayerStack != layer->getLayerStack(LayerVector::StateSet::Current)) { |
Vishnu Nair | afd527a | 2022-10-18 08:56:10 -0700 | [diff] [blame] | 5829 | flags |= eTransformHintUpdateNeeded; |
| 5830 | } |
| 5831 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 5832 | return flags; |
| 5833 | } |
| 5834 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5835 | uint32_t SurfaceFlinger::updateLayerCallbacksAndStats(const FrameTimelineInfo& frameTimelineInfo, |
| 5836 | ResolvedComposerState& composerState, |
| 5837 | int64_t desiredPresentTime, |
| 5838 | bool isAutoTimestamp, int64_t postTime, |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5839 | uint64_t transactionId) { |
| 5840 | layer_state_t& s = composerState.state; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5841 | |
| 5842 | std::vector<ListenerCallbacks> filteredListeners; |
| 5843 | for (auto& listener : s.listeners) { |
| 5844 | // Starts a registration but separates the callback ids according to callback type. This |
| 5845 | // allows the callback invoker to send on latch callbacks earlier. |
| 5846 | // note that startRegistration will not re-register if the listener has |
| 5847 | // already be registered for a prior surface control |
| 5848 | |
| 5849 | ListenerCallbacks onCommitCallbacks = listener.filter(CallbackId::Type::ON_COMMIT); |
| 5850 | if (!onCommitCallbacks.callbackIds.empty()) { |
| 5851 | filteredListeners.push_back(onCommitCallbacks); |
| 5852 | } |
| 5853 | |
| 5854 | ListenerCallbacks onCompleteCallbacks = listener.filter(CallbackId::Type::ON_COMPLETE); |
| 5855 | if (!onCompleteCallbacks.callbackIds.empty()) { |
| 5856 | filteredListeners.push_back(onCompleteCallbacks); |
| 5857 | } |
| 5858 | } |
| 5859 | |
| 5860 | const uint64_t what = s.what; |
| 5861 | uint32_t flags = 0; |
| 5862 | sp<Layer> layer = nullptr; |
| 5863 | if (s.surface) { |
| 5864 | layer = LayerHandle::getLayer(s.surface); |
| 5865 | } else { |
| 5866 | // The client may provide us a null handle. Treat it as if the layer was removed. |
| 5867 | ALOGW("Attempt to set client state with a null layer handle"); |
| 5868 | } |
| 5869 | if (layer == nullptr) { |
| 5870 | for (auto& [listener, callbackIds] : s.listeners) { |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 5871 | mTransactionCallbackInvoker.addCallbackHandle( |
| 5872 | sp<CallbackHandle>::make(listener, callbackIds, s.surface)); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5873 | } |
| 5874 | return 0; |
| 5875 | } |
| 5876 | if (what & layer_state_t::eProducerDisconnect) { |
| 5877 | layer->onDisconnect(); |
| 5878 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5879 | |
| 5880 | std::vector<sp<CallbackHandle>> callbackHandles; |
| 5881 | if ((what & layer_state_t::eHasListenerCallbacksChanged) && (!filteredListeners.empty())) { |
| 5882 | for (auto& [listener, callbackIds] : filteredListeners) { |
| 5883 | callbackHandles.emplace_back( |
| 5884 | sp<CallbackHandle>::make(listener, callbackIds, s.surface)); |
| 5885 | } |
| 5886 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 5887 | // TODO(b/238781169) remove after screenshot refactor, currently screenshots |
| 5888 | // requires to read drawing state from binder thread. So we need to fix that |
| 5889 | // before removing this. |
Vishnu Nair | aeaf9d7 | 2023-09-12 15:47:41 -0700 | [diff] [blame] | 5890 | if (what & layer_state_t::eBufferTransformChanged) { |
| 5891 | if (layer->setTransform(s.bufferTransform)) flags |= eTraversalNeeded; |
| 5892 | } |
| 5893 | if (what & layer_state_t::eTransformToDisplayInverseChanged) { |
| 5894 | if (layer->setTransformToDisplayInverse(s.transformToDisplayInverse)) |
| 5895 | flags |= eTraversalNeeded; |
| 5896 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 5897 | if (what & layer_state_t::eCropChanged) { |
| 5898 | if (layer->setCrop(s.crop)) flags |= eTraversalNeeded; |
| 5899 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5900 | if (what & layer_state_t::eSidebandStreamChanged) { |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 5901 | if (layer->setSidebandStream(s.sidebandStream, frameTimelineInfo, postTime)) |
| 5902 | flags |= eTraversalNeeded; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5903 | } |
Vishnu Nair | 3996ee3 | 2023-08-14 04:32:31 +0000 | [diff] [blame] | 5904 | if (what & layer_state_t::eDataspaceChanged) { |
| 5905 | if (layer->setDataspace(s.dataspace)) flags |= eTraversalNeeded; |
| 5906 | } |
Vishnu Nair | c5e3416 | 2023-08-22 13:21:20 -0700 | [diff] [blame] | 5907 | if (what & layer_state_t::eExtendedRangeBrightnessChanged) { |
| 5908 | if (layer->setExtendedRangeBrightness(s.currentHdrSdrRatio, s.desiredHdrSdrRatio)) { |
| 5909 | flags |= eTraversalNeeded; |
| 5910 | } |
| 5911 | } |
Alec Mouri | 1b0d4e1 | 2024-02-12 22:27:19 +0000 | [diff] [blame] | 5912 | if (what & layer_state_t::eDesiredHdrHeadroomChanged) { |
| 5913 | if (layer->setDesiredHdrHeadroom(s.desiredHdrSdrRatio)) { |
| 5914 | flags |= eTraversalNeeded; |
| 5915 | } |
| 5916 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5917 | if (what & layer_state_t::eBufferChanged) { |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 5918 | std::optional<ui::Transform::RotationFlags> transformHint = std::nullopt; |
| 5919 | frontend::LayerSnapshot* snapshot = mLayerSnapshotBuilder.getSnapshot(layer->sequence); |
| 5920 | if (snapshot) { |
| 5921 | transformHint = snapshot->transformHint; |
| 5922 | } |
| 5923 | layer->setTransformHint(transformHint); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5924 | if (layer->setBuffer(composerState.externalTexture, *s.bufferData, postTime, |
Nergi Rahardi | 39f510f | 2024-05-23 15:16:54 +0900 | [diff] [blame] | 5925 | desiredPresentTime, isAutoTimestamp, frameTimelineInfo)) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5926 | flags |= eTraversalNeeded; |
| 5927 | } |
| 5928 | mLayersWithQueuedFrames.emplace(layer); |
| 5929 | } else if (frameTimelineInfo.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) { |
| 5930 | layer->setFrameTimelineVsyncForBufferlessTransaction(frameTimelineInfo, postTime); |
| 5931 | } |
| 5932 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 5933 | if ((what & layer_state_t::eBufferChanged) == 0) { |
| 5934 | layer->setDesiredPresentTime(desiredPresentTime, isAutoTimestamp); |
| 5935 | } |
| 5936 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5937 | if (what & layer_state_t::eTrustedPresentationInfoChanged) { |
Chavi Weingarten | 328a831 | 2023-01-26 21:17:52 +0000 | [diff] [blame] | 5938 | if (layer->setTrustedPresentationInfo(s.trustedPresentationThresholds, |
| 5939 | s.trustedPresentationListener)) { |
| 5940 | flags |= eTraversalNeeded; |
| 5941 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5942 | } |
| 5943 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 5944 | const auto& requestedLayerState = mLayerLifecycleManager.getLayerFromId(layer->getSequence()); |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 5945 | bool willPresentCurrentTransaction = requestedLayerState && |
| 5946 | (requestedLayerState->hasReadyFrame() || |
| 5947 | requestedLayerState->willReleaseBufferOnLatch()); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5948 | if (layer->setTransactionCompletedListeners(callbackHandles, willPresentCurrentTransaction)) |
| 5949 | flags |= eTraversalNeeded; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 5950 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5951 | return flags; |
| 5952 | } |
| 5953 | |
chaviw | 273171b | 2018-12-26 11:46:30 -0800 | [diff] [blame] | 5954 | uint32_t SurfaceFlinger::addInputWindowCommands(const InputWindowCommands& inputWindowCommands) { |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 5955 | bool hasChanges = mInputWindowCommands.merge(inputWindowCommands); |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5956 | return hasChanges ? eTraversalNeeded : 0; |
chaviw | 273171b | 2018-12-26 11:46:30 -0800 | [diff] [blame] | 5957 | } |
| 5958 | |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 5959 | status_t SurfaceFlinger::mirrorLayer(const LayerCreationArgs& args, |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5960 | const sp<IBinder>& mirrorFromHandle, |
| 5961 | gui::CreateSurfaceResult& outResult) { |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5962 | if (!mirrorFromHandle) { |
| 5963 | return NAME_NOT_FOUND; |
| 5964 | } |
| 5965 | |
| 5966 | sp<Layer> mirrorLayer; |
| 5967 | sp<Layer> mirrorFrom; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 5968 | LayerCreationArgs mirrorArgs = LayerCreationArgs::fromOtherArgs(args); |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5969 | { |
| 5970 | Mutex::Autolock _l(mStateLock); |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 5971 | mirrorFrom = LayerHandle::getLayer(mirrorFromHandle); |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5972 | if (!mirrorFrom) { |
| 5973 | return NAME_NOT_FOUND; |
| 5974 | } |
Vishnu Nair | b21272e | 2022-07-01 22:47:33 +0000 | [diff] [blame] | 5975 | mirrorArgs.flags |= ISurfaceComposerClient::eNoColorFill; |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 5976 | mirrorArgs.mirrorLayerHandle = mirrorFromHandle; |
| 5977 | mirrorArgs.addToRoot = false; |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5978 | status_t result = createEffectLayer(mirrorArgs, &outResult.handle, &mirrorLayer); |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5979 | if (result != NO_ERROR) { |
| 5980 | return result; |
| 5981 | } |
| 5982 | |
Melody Hsu | 12ecd08 | 2024-04-17 19:03:13 +0000 | [diff] [blame] | 5983 | mirrorLayer->setClonedChild(mirrorFrom->createClone()); |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5984 | } |
| 5985 | |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5986 | outResult.layerId = mirrorLayer->sequence; |
| 5987 | outResult.layerName = String16(mirrorLayer->getDebugName()); |
Vishnu Nair | 8319e36 | 2022-10-24 18:59:20 +0000 | [diff] [blame] | 5988 | return addClientLayer(mirrorArgs, outResult.handle, mirrorLayer /* layer */, |
| 5989 | nullptr /* parent */, nullptr /* outTransformHint */); |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5990 | } |
| 5991 | |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 5992 | status_t SurfaceFlinger::mirrorDisplay(DisplayId displayId, const LayerCreationArgs& args, |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5993 | gui::CreateSurfaceResult& outResult) { |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 5994 | IPCThreadState* ipc = IPCThreadState::self(); |
| 5995 | const int uid = ipc->getCallingUid(); |
| 5996 | if (uid != AID_ROOT && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != AID_SHELL) { |
| 5997 | ALOGE("Permission denied when trying to mirror display"); |
| 5998 | return PERMISSION_DENIED; |
| 5999 | } |
| 6000 | |
| 6001 | ui::LayerStack layerStack; |
| 6002 | sp<Layer> rootMirrorLayer; |
| 6003 | status_t result = 0; |
| 6004 | |
| 6005 | { |
| 6006 | Mutex::Autolock lock(mStateLock); |
| 6007 | |
| 6008 | const auto display = getDisplayDeviceLocked(displayId); |
| 6009 | if (!display) { |
| 6010 | return NAME_NOT_FOUND; |
| 6011 | } |
| 6012 | |
| 6013 | layerStack = display->getLayerStack(); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 6014 | LayerCreationArgs mirrorArgs = LayerCreationArgs::fromOtherArgs(args); |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 6015 | mirrorArgs.flags |= ISurfaceComposerClient::eNoColorFill; |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 6016 | mirrorArgs.addToRoot = true; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 6017 | mirrorArgs.layerStackToMirror = layerStack; |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 6018 | result = createEffectLayer(mirrorArgs, &outResult.handle, &rootMirrorLayer); |
| 6019 | outResult.layerId = rootMirrorLayer->sequence; |
| 6020 | outResult.layerName = String16(rootMirrorLayer->getDebugName()); |
Vishnu Nair | 8319e36 | 2022-10-24 18:59:20 +0000 | [diff] [blame] | 6021 | result |= addClientLayer(mirrorArgs, outResult.handle, rootMirrorLayer /* layer */, |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 6022 | nullptr /* parent */, nullptr /* outTransformHint */); |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 6023 | } |
| 6024 | |
| 6025 | if (result != NO_ERROR) { |
| 6026 | return result; |
| 6027 | } |
| 6028 | |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 6029 | setTransactionFlags(eTransactionFlushNeeded); |
| 6030 | return NO_ERROR; |
| 6031 | } |
| 6032 | |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 6033 | status_t SurfaceFlinger::createLayer(LayerCreationArgs& args, gui::CreateSurfaceResult& outResult) { |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 6034 | status_t result = NO_ERROR; |
| 6035 | |
| 6036 | sp<Layer> layer; |
| 6037 | |
Vishnu Nair | 7fb9e5a | 2021-11-08 12:44:05 -0800 | [diff] [blame] | 6038 | switch (args.flags & ISurfaceComposerClient::eFXSurfaceMask) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 6039 | case ISurfaceComposerClient::eFXSurfaceBufferQueue: |
Vishnu Nair | bc4ee5c | 2022-08-16 03:19:37 +0000 | [diff] [blame] | 6040 | case ISurfaceComposerClient::eFXSurfaceContainer: |
| 6041 | case ISurfaceComposerClient::eFXSurfaceBufferState: |
| 6042 | args.flags |= ISurfaceComposerClient::eNoColorFill; |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 6043 | [[fallthrough]]; |
Vishnu Nair | bc4ee5c | 2022-08-16 03:19:37 +0000 | [diff] [blame] | 6044 | case ISurfaceComposerClient::eFXSurfaceEffect: { |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 6045 | result = createBufferStateLayer(args, &outResult.handle, &layer); |
Vishnu Nair | 8eda69e | 2021-02-26 10:42:10 -0800 | [diff] [blame] | 6046 | std::atomic<int32_t>* pendingBufferCounter = layer->getPendingBufferCounter(); |
| 6047 | if (pendingBufferCounter) { |
| 6048 | std::string counterName = layer->getPendingBufferCounterName(); |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 6049 | mBufferCountTracker.add(outResult.handle->localBinder(), counterName, |
Vishnu Nair | 8eda69e | 2021-02-26 10:42:10 -0800 | [diff] [blame] | 6050 | pendingBufferCounter); |
| 6051 | } |
| 6052 | } break; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 6053 | default: |
| 6054 | result = BAD_VALUE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6055 | break; |
| 6056 | } |
| 6057 | |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 6058 | if (result != NO_ERROR) { |
| 6059 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6060 | } |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 6061 | |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 6062 | args.addToRoot = args.addToRoot && callingThreadHasUnscopedSurfaceFlingerAccess(); |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 6063 | // We can safely promote the parent layer in binder thread because we have a strong reference |
| 6064 | // to the layer's handle inside this scope. |
| 6065 | sp<Layer> parent = LayerHandle::getLayer(args.parentHandle.promote()); |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 6066 | if (args.parentHandle != nullptr && parent == nullptr) { |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 6067 | ALOGE("Invalid parent handle %p", args.parentHandle.promote().get()); |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 6068 | args.addToRoot = false; |
Garfield Tan | dbc93d7 | 2021-10-26 18:28:57 -0700 | [diff] [blame] | 6069 | } |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 6070 | |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 6071 | uint32_t outTransformHint; |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 6072 | result = addClientLayer(args, outResult.handle, layer, parent, &outTransformHint); |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 6073 | if (result != NO_ERROR) { |
| 6074 | return result; |
| 6075 | } |
| 6076 | |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 6077 | outResult.transformHint = static_cast<int32_t>(outTransformHint); |
| 6078 | outResult.layerId = layer->sequence; |
| 6079 | outResult.layerName = String16(layer->getDebugName()); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 6080 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6081 | } |
| 6082 | |
Vishnu Nair | 7fb9e5a | 2021-11-08 12:44:05 -0800 | [diff] [blame] | 6083 | status_t SurfaceFlinger::createBufferStateLayer(LayerCreationArgs& args, sp<IBinder>* handle, |
Vishnu Nair | 6213bd9 | 2020-05-08 17:42:25 -0700 | [diff] [blame] | 6084 | sp<Layer>* outLayer) { |
Vishnu Nair | 7fb9e5a | 2021-11-08 12:44:05 -0800 | [diff] [blame] | 6085 | *outLayer = getFactory().createBufferStateLayer(args); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 6086 | *handle = (*outLayer)->getHandle(); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 6087 | return NO_ERROR; |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 6088 | } |
| 6089 | |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 6090 | status_t SurfaceFlinger::createEffectLayer(const LayerCreationArgs& args, sp<IBinder>* handle, |
Vishnu Nair | 7fb9e5a | 2021-11-08 12:44:05 -0800 | [diff] [blame] | 6091 | sp<Layer>* outLayer) { |
| 6092 | *outLayer = getFactory().createEffectLayer(args); |
| 6093 | *handle = (*outLayer)->getHandle(); |
| 6094 | return NO_ERROR; |
| 6095 | } |
| 6096 | |
Robert Carr | 6fb1a7e | 2018-12-11 12:07:25 -0800 | [diff] [blame] | 6097 | void SurfaceFlinger::markLayerPendingRemovalLocked(const sp<Layer>& layer) { |
Robert Carr | 2e102c9 | 2018-10-23 12:11:15 -0700 | [diff] [blame] | 6098 | mLayersPendingRemoval.add(layer); |
| 6099 | mLayersRemoved = true; |
| 6100 | setTransactionFlags(eTransactionNeeded); |
| 6101 | } |
| 6102 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 6103 | void SurfaceFlinger::onHandleDestroyed(BBinder* handle, sp<Layer>& layer, uint32_t layerId) { |
| 6104 | { |
| 6105 | std::scoped_lock<std::mutex> lock(mCreatedLayersLock); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 6106 | mDestroyedHandles.emplace_back(layerId, layer->getDebugName()); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 6107 | } |
| 6108 | |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 6109 | { |
| 6110 | // Used to remove stalled transactions which uses an internal lock. |
| 6111 | ftl::FakeGuard guard(kMainThreadContext); |
| 6112 | mTransactionHandler.onLayerDestroyed(layerId); |
| 6113 | } |
Patrick Williams | 090ad06 | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 6114 | |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 6115 | JankTracker::flushJankData(layerId); |
| 6116 | |
Robert Carr | 2e102c9 | 2018-10-23 12:11:15 -0700 | [diff] [blame] | 6117 | Mutex::Autolock lock(mStateLock); |
Robert Carr | 6fb1a7e | 2018-12-11 12:07:25 -0800 | [diff] [blame] | 6118 | markLayerPendingRemovalLocked(layer); |
Vishnu Nair | 787aa78 | 2023-03-17 13:46:46 -0700 | [diff] [blame] | 6119 | layer->onHandleDestroyed(); |
Vishnu Nair | f909665 | 2021-07-20 18:49:42 -0700 | [diff] [blame] | 6120 | mBufferCountTracker.remove(handle); |
Robert Carr | 695d528 | 2018-12-18 15:27:58 -0800 | [diff] [blame] | 6121 | layer.clear(); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 6122 | |
| 6123 | setTransactionFlags(eTransactionFlushNeeded); |
Rob Carr | 4bba370 | 2018-10-08 21:53:30 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
Dominik Laskowski | b5f40e5 | 2022-09-29 13:25:05 -0400 | [diff] [blame] | 6126 | void SurfaceFlinger::initializeDisplays() { |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 6127 | TransactionState state; |
| 6128 | state.inputWindowCommands = mInputWindowCommands; |
Dominik Laskowski | 1686374 | 2022-09-29 11:57:40 -0400 | [diff] [blame] | 6129 | const nsecs_t now = systemTime(); |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 6130 | state.desiredPresentTime = now; |
| 6131 | state.postTime = now; |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6132 | state.originPid = mPid; |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 6133 | state.originUid = static_cast<int>(getuid()); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6134 | const uint64_t transactionId = (static_cast<uint64_t>(mPid) << 32) | mUniqueTransactionId++; |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 6135 | state.id = transactionId; |
| 6136 | |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 6137 | auto layerStack = ui::DEFAULT_LAYER_STACK.id; |
| 6138 | for (const auto& [id, display] : FTL_FAKE_GUARD(mStateLock, mPhysicalDisplays)) { |
| 6139 | state.displays.push(DisplayState(display.token(), ui::LayerStack::fromValue(layerStack++))); |
| 6140 | } |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 6141 | |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 6142 | std::vector<TransactionState> transactions; |
| 6143 | transactions.emplace_back(state); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 6144 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 6145 | { |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 6146 | Mutex::Autolock lock(mStateLock); |
| 6147 | applyAndCommitDisplayTransactionStatesLocked(transactions); |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 6148 | } |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 6149 | |
Dominik Laskowski | 1686374 | 2022-09-29 11:57:40 -0400 | [diff] [blame] | 6150 | { |
| 6151 | ftl::FakeGuard guard(mStateLock); |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 6152 | |
| 6153 | // In case of a restart, ensure all displays are off. |
| 6154 | for (const auto& [id, display] : mPhysicalDisplays) { |
| 6155 | setPowerModeInternal(getDisplayDeviceLocked(id), hal::PowerMode::OFF); |
| 6156 | } |
| 6157 | |
| 6158 | // Power on all displays. The primary display is first, so becomes the active display. Also, |
| 6159 | // the DisplayCapability set of a display is populated on its first powering on. Do this now |
| 6160 | // before responding to any Binder query from DisplayManager about display capabilities. |
Robert Horvath | dce110a | 2022-09-16 11:08:47 +0200 | [diff] [blame] | 6161 | // Additionally, do not turn on displays if the boot should be quiescent. |
| 6162 | if (!mSkipPowerOnForQuiescent) { |
| 6163 | for (const auto& [id, display] : mPhysicalDisplays) { |
| 6164 | setPowerModeInternal(getDisplayDeviceLocked(id), hal::PowerMode::ON); |
| 6165 | } |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 6166 | } |
Dominik Laskowski | 1686374 | 2022-09-29 11:57:40 -0400 | [diff] [blame] | 6167 | } |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 6168 | } |
| 6169 | |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 6170 | void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& display, hal::PowerMode mode) { |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 6171 | if (display->isVirtual()) { |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 6172 | // TODO(b/241285876): This code path should not be reachable, so enforce this at compile |
| 6173 | // time. |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 6174 | ALOGE("%s: Invalid operation on virtual display", __func__); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 6175 | return; |
| 6176 | } |
| 6177 | |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 6178 | const auto displayId = display->getPhysicalId(); |
| 6179 | ALOGD("Setting power mode %d on display %s", mode, to_string(displayId).c_str()); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 6180 | |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 6181 | const auto currentMode = display->getPowerMode(); |
| 6182 | if (currentMode == mode) { |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 6183 | return; |
| 6184 | } |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 6185 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 6186 | const bool isInternalDisplay = mPhysicalDisplays.get(displayId) |
| 6187 | .transform(&PhysicalDisplay::isInternal) |
| 6188 | .value_or(false); |
| 6189 | |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 6190 | const auto activeDisplay = getDisplayDeviceLocked(mActiveDisplayId); |
Dominik Laskowski | c132a15 | 2022-11-30 15:44:52 -0500 | [diff] [blame] | 6191 | |
Dominik Laskowski | 31afdda | 2022-12-08 19:15:15 -0500 | [diff] [blame] | 6192 | ALOGW_IF(display != activeDisplay && isInternalDisplay && activeDisplay && |
| 6193 | activeDisplay->isPoweredOn(), |
Dominik Laskowski | c132a15 | 2022-11-30 15:44:52 -0500 | [diff] [blame] | 6194 | "Trying to change power mode on inactive display without powering off active display"); |
Ady Abraham | 4f960d1 | 2021-10-13 16:59:49 -0700 | [diff] [blame] | 6195 | |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 6196 | display->setPowerMode(mode); |
| 6197 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 6198 | const auto activeMode = display->refreshRateSelector().getActiveMode().modePtr; |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 6199 | if (currentMode == hal::PowerMode::OFF) { |
Ady Abraham | 4f960d1 | 2021-10-13 16:59:49 -0700 | [diff] [blame] | 6200 | // Turn on the display |
Dominik Laskowski | 31afdda | 2022-12-08 19:15:15 -0500 | [diff] [blame] | 6201 | |
Dominik Laskowski | a39e32f | 2023-02-08 16:40:13 -0500 | [diff] [blame] | 6202 | // Activate the display (which involves a modeset to the active mode) when the inner or |
| 6203 | // outer display of a foldable is powered on. This condition relies on the above |
| 6204 | // DisplayDevice::setPowerMode. If `display` and `activeDisplay` are the same display, |
| 6205 | // then the `activeDisplay->isPoweredOn()` below is true, such that the display is not |
| 6206 | // activated every time it is powered on. |
Dominik Laskowski | 31afdda | 2022-12-08 19:15:15 -0500 | [diff] [blame] | 6207 | // |
| 6208 | // TODO(b/255635821): Remove the concept of active display. |
Dominik Laskowski | a39e32f | 2023-02-08 16:40:13 -0500 | [diff] [blame] | 6209 | if (isInternalDisplay && (!activeDisplay || !activeDisplay->isPoweredOn())) { |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 6210 | onActiveDisplayChangedLocked(activeDisplay.get(), *display); |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 6211 | } |
Dominik Laskowski | 31afdda | 2022-12-08 19:15:15 -0500 | [diff] [blame] | 6212 | |
Dominik Laskowski | cf0bb43 | 2023-05-09 11:49:16 -0400 | [diff] [blame] | 6213 | if (displayId == mActiveDisplayId) { |
| 6214 | // TODO(b/281692563): Merge the syscalls. For now, keep uclamp in a separate syscall and |
| 6215 | // set it before SCHED_FIFO due to b/190237315. |
| 6216 | if (setSchedAttr(true) != NO_ERROR) { |
| 6217 | ALOGW("Failed to set uclamp.min after powering on active display: %s", |
| 6218 | strerror(errno)); |
| 6219 | } |
| 6220 | if (setSchedFifo(true) != NO_ERROR) { |
| 6221 | ALOGW("Failed to set SCHED_FIFO after powering on active display: %s", |
| 6222 | strerror(errno)); |
| 6223 | } |
Wei Wang | 23aa5a6 | 2021-06-04 15:56:57 -0700 | [diff] [blame] | 6224 | } |
Dominik Laskowski | cf0bb43 | 2023-05-09 11:49:16 -0400 | [diff] [blame] | 6225 | |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 6226 | getHwComposer().setPowerMode(displayId, mode); |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 6227 | if (mode != hal::PowerMode::DOZE_SUSPEND && |
| 6228 | (displayId == mActiveDisplayId || FlagManager::getInstance().multithreaded_present())) { |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 6229 | const bool enable = |
| 6230 | mScheduler->getVsyncSchedule(displayId)->getPendingHardwareVsyncState(); |
| 6231 | requestHardwareVsync(displayId, enable); |
| 6232 | |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 6233 | if (displayId == mActiveDisplayId) { |
| 6234 | mScheduler->enableSyntheticVsync(false); |
| 6235 | } |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 6236 | |
| 6237 | constexpr bool kAllowToEnable = true; |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 6238 | mScheduler->resyncToHardwareVsync(displayId, kAllowToEnable, activeMode.get()); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 6239 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6240 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 6241 | mVisibleRegionsDirty = true; |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 6242 | scheduleComposite(FrameHint::kActive); |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 6243 | } else if (mode == hal::PowerMode::OFF) { |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 6244 | const bool currentModeNotDozeSuspend = (currentMode != hal::PowerMode::DOZE_SUSPEND); |
Tim Murray | f9d4e44 | 2016-08-02 15:43:59 -0700 | [diff] [blame] | 6245 | // Turn off the display |
Dominik Laskowski | cf0bb43 | 2023-05-09 11:49:16 -0400 | [diff] [blame] | 6246 | if (displayId == mActiveDisplayId) { |
Dominik Laskowski | 3de6727 | 2023-08-09 13:15:31 -0400 | [diff] [blame] | 6247 | if (const auto display = getActivatableDisplay()) { |
| 6248 | onActiveDisplayChangedLocked(activeDisplay.get(), *display); |
| 6249 | } else { |
| 6250 | if (setSchedFifo(false) != NO_ERROR) { |
| 6251 | ALOGW("Failed to set SCHED_OTHER after powering off active display: %s", |
| 6252 | strerror(errno)); |
| 6253 | } |
| 6254 | if (setSchedAttr(false) != NO_ERROR) { |
| 6255 | ALOGW("Failed set uclamp.min after powering off active display: %s", |
| 6256 | strerror(errno)); |
| 6257 | } |
Dominik Laskowski | cf0bb43 | 2023-05-09 11:49:16 -0400 | [diff] [blame] | 6258 | |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 6259 | if (currentModeNotDozeSuspend) { |
| 6260 | if (!FlagManager::getInstance().multithreaded_present()) { |
| 6261 | mScheduler->disableHardwareVsync(displayId, true); |
| 6262 | } |
Dominik Laskowski | 3de6727 | 2023-08-09 13:15:31 -0400 | [diff] [blame] | 6263 | mScheduler->enableSyntheticVsync(); |
| 6264 | } |
Dominik Laskowski | cf0bb43 | 2023-05-09 11:49:16 -0400 | [diff] [blame] | 6265 | } |
Mathias Agopian | cde87a3 | 2012-09-13 14:09:01 -0700 | [diff] [blame] | 6266 | } |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 6267 | if (currentModeNotDozeSuspend && FlagManager::getInstance().multithreaded_present()) { |
| 6268 | constexpr bool kDisallow = true; |
| 6269 | mScheduler->disableHardwareVsync(displayId, kDisallow); |
| 6270 | } |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 6271 | |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 6272 | // We must disable VSYNC *before* turning off the display. The call to |
| 6273 | // disableHardwareVsync, above, schedules a task to turn it off after |
| 6274 | // this method returns. But by that point, the display is OFF, so the |
| 6275 | // call just updates the pending state, without actually disabling |
| 6276 | // VSYNC. |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 6277 | requestHardwareVsync(displayId, false); |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 6278 | getHwComposer().setPowerMode(displayId, mode); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 6279 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 6280 | mVisibleRegionsDirty = true; |
| 6281 | // from this point on, SF will stop drawing on this display |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 6282 | } else if (mode == hal::PowerMode::DOZE || mode == hal::PowerMode::ON) { |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 6283 | // Update display while dozing |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 6284 | getHwComposer().setPowerMode(displayId, mode); |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 6285 | if (currentMode == hal::PowerMode::DOZE_SUSPEND && |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 6286 | (displayId == mActiveDisplayId || FlagManager::getInstance().multithreaded_present())) { |
| 6287 | if (displayId == mActiveDisplayId) { |
| 6288 | ALOGI("Force repainting for DOZE_SUSPEND -> DOZE or ON."); |
| 6289 | mVisibleRegionsDirty = true; |
| 6290 | scheduleRepaint(); |
| 6291 | mScheduler->enableSyntheticVsync(false); |
| 6292 | } |
| 6293 | constexpr bool kAllowToEnable = true; |
| 6294 | mScheduler->resyncToHardwareVsync(displayId, kAllowToEnable, activeMode.get()); |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 6295 | } |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 6296 | } else if (mode == hal::PowerMode::DOZE_SUSPEND) { |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 6297 | // Leave display going to doze |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 6298 | if (displayId == mActiveDisplayId || FlagManager::getInstance().multithreaded_present()) { |
| 6299 | constexpr bool kDisallow = true; |
| 6300 | mScheduler->disableHardwareVsync(displayId, kDisallow); |
| 6301 | } |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 6302 | if (displayId == mActiveDisplayId) { |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 6303 | mScheduler->enableSyntheticVsync(); |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 6304 | } |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 6305 | getHwComposer().setPowerMode(displayId, mode); |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 6306 | } else { |
Matthew Bouyack | 38d4961 | 2017-05-12 12:49:32 -0700 | [diff] [blame] | 6307 | ALOGE("Attempting to set unknown power mode: %d\n", mode); |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 6308 | getHwComposer().setPowerMode(displayId, mode); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 6309 | } |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 6310 | |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 6311 | if (displayId == mActiveDisplayId) { |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 6312 | mTimeStats->setPowerMode(mode); |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 6313 | mScheduler->setActiveDisplayPowerModeForRefreshRateStats(mode); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 6314 | } |
| 6315 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 6316 | mScheduler->setDisplayPowerMode(displayId, mode); |
| 6317 | |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 6318 | ALOGD("Finished setting power mode %d on display %s", mode, to_string(displayId).c_str()); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 6319 | } |
| 6320 | |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 6321 | void SurfaceFlinger::setPowerMode(const sp<IBinder>& displayToken, int mode) { |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 6322 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD( |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 6323 | kMainThreadContext) { |
Robert Horvath | dce110a | 2022-09-16 11:08:47 +0200 | [diff] [blame] | 6324 | mSkipPowerOnForQuiescent = false; |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 6325 | const auto display = getDisplayDeviceLocked(displayToken); |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 6326 | if (!display) { |
| 6327 | ALOGE("Attempt to set power mode %d for invalid display token %p", mode, |
| 6328 | displayToken.get()); |
| 6329 | } else if (display->isVirtual()) { |
| 6330 | ALOGW("Attempt to set power mode %d for virtual display", mode); |
| 6331 | } else { |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 6332 | setPowerModeInternal(display, static_cast<hal::PowerMode>(mode)); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 6333 | } |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6334 | }); |
| 6335 | |
| 6336 | future.wait(); |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 6337 | } |
| 6338 | |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 6339 | status_t SurfaceFlinger::doDump(int fd, const DumpArgs& args, bool asProto) { |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6340 | std::string result; |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 6341 | |
Mathias Agopian | bd11533 | 2013-04-18 16:41:04 -0700 | [diff] [blame] | 6342 | IPCThreadState* ipc = IPCThreadState::self(); |
| 6343 | const int pid = ipc->getCallingPid(); |
| 6344 | const int uid = ipc->getCallingUid(); |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 6345 | |
Mathias Agopian | bd11533 | 2013-04-18 16:41:04 -0700 | [diff] [blame] | 6346 | if ((uid != AID_SHELL) && |
| 6347 | !PermissionCache::checkPermission(sDump, pid, uid)) { |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6348 | StringAppendF(&result, "Permission Denial: can't dump SurfaceFlinger from pid=%d, uid=%d\n", |
| 6349 | pid, uid); |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6350 | write(fd, result.c_str(), result.size()); |
| 6351 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6352 | } |
Ady Abraham | 5ffd98a | 2023-08-24 17:37:01 -0700 | [diff] [blame] | 6353 | |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6354 | if (asProto && args.empty()) { |
| 6355 | perfetto::protos::LayersTraceFileProto traceFileProto = |
| 6356 | mLayerTracing.createTraceFileProto(); |
| 6357 | perfetto::protos::LayersSnapshotProto* layersTrace = traceFileProto.add_entry(); |
| 6358 | perfetto::protos::LayersProto layersProto = dumpProtoFromMainThread(); |
| 6359 | layersTrace->mutable_layers()->Swap(&layersProto); |
| 6360 | auto displayProtos = dumpDisplayProto(); |
| 6361 | layersTrace->mutable_displays()->Swap(&displayProtos); |
| 6362 | result.append(traceFileProto.SerializeAsString()); |
| 6363 | write(fd, result.c_str(), result.size()); |
| 6364 | return NO_ERROR; |
| 6365 | } |
| 6366 | |
| 6367 | static const std::unordered_map<std::string, Dumper> dumpers = { |
| 6368 | {"--comp-displays"s, dumper(&SurfaceFlinger::dumpCompositionDisplays)}, |
| 6369 | {"--display-id"s, dumper(&SurfaceFlinger::dumpDisplayIdentificationData)}, |
| 6370 | {"--displays"s, dumper(&SurfaceFlinger::dumpDisplays)}, |
| 6371 | {"--edid"s, argsDumper(&SurfaceFlinger::dumpRawDisplayIdentificationData)}, |
| 6372 | {"--events"s, dumper(&SurfaceFlinger::dumpEvents)}, |
| 6373 | {"--frametimeline"s, argsDumper(&SurfaceFlinger::dumpFrameTimeline)}, |
| 6374 | {"--frontend"s, mainThreadDumper(&SurfaceFlinger::dumpFrontEnd)}, |
| 6375 | {"--hdrinfo"s, dumper(&SurfaceFlinger::dumpHdrInfo)}, |
| 6376 | {"--hwclayers"s, mainThreadDumper(&SurfaceFlinger::dumpHwcLayersMinidump)}, |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 6377 | {"--latency"s, argsMainThreadDumper(&SurfaceFlinger::dumpStats)}, |
| 6378 | {"--latency-clear"s, argsMainThreadDumper(&SurfaceFlinger::clearStats)}, |
| 6379 | {"--list"s, mainThreadDumper(&SurfaceFlinger::listLayers)}, |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6380 | {"--planner"s, argsDumper(&SurfaceFlinger::dumpPlannerInfo)}, |
| 6381 | {"--scheduler"s, dumper(&SurfaceFlinger::dumpScheduler)}, |
| 6382 | {"--timestats"s, protoDumper(&SurfaceFlinger::dumpTimeStats)}, |
| 6383 | {"--vsync"s, dumper(&SurfaceFlinger::dumpVsync)}, |
| 6384 | {"--wide-color"s, dumper(&SurfaceFlinger::dumpWideColorInfo)}, |
| 6385 | }; |
| 6386 | |
| 6387 | const auto flag = args.empty() ? ""s : std::string(String8(args[0])); |
| 6388 | if (const auto it = dumpers.find(flag); it != dumpers.end()) { |
| 6389 | (it->second)(args, asProto, result); |
| 6390 | write(fd, result.c_str(), result.size()); |
| 6391 | return NO_ERROR; |
| 6392 | } |
| 6393 | |
Vishnu Nair | 11db986 | 2024-06-21 22:06:14 +0000 | [diff] [blame] | 6394 | // Collect debug data from main thread |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6395 | std::string compositionLayers; |
| 6396 | mScheduler |
| 6397 | ->schedule([&]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD(kMainThreadContext) { |
| 6398 | dumpVisibleFrontEnd(compositionLayers); |
| 6399 | }) |
| 6400 | .get(); |
Vishnu Nair | 11db986 | 2024-06-21 22:06:14 +0000 | [diff] [blame] | 6401 | // get window info listener data without the state lock |
| 6402 | auto windowInfosDebug = mWindowInfosListenerInvoker->getDebugInfo(); |
| 6403 | compositionLayers.append("Window Infos:\n"); |
| 6404 | StringAppendF(&compositionLayers, " max send vsync id: %" PRId64 "\n", |
| 6405 | ftl::to_underlying(windowInfosDebug.maxSendDelayVsyncId)); |
| 6406 | StringAppendF(&compositionLayers, " max send delay (ns): %" PRId64 " ns\n", |
| 6407 | windowInfosDebug.maxSendDelayDuration); |
| 6408 | StringAppendF(&compositionLayers, " unsent messages: %zu\n", |
| 6409 | windowInfosDebug.pendingMessageCount); |
| 6410 | compositionLayers.append("\n"); |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6411 | dumpAll(args, compositionLayers, result); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6412 | write(fd, result.c_str(), result.size()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6413 | return NO_ERROR; |
| 6414 | } |
| 6415 | |
Nataniel Borges | 8e7dc72 | 2019-02-28 15:10:28 -0800 | [diff] [blame] | 6416 | status_t SurfaceFlinger::dumpCritical(int fd, const DumpArgs&, bool asProto) { |
Nataniel Borges | 8e7dc72 | 2019-02-28 15:10:28 -0800 | [diff] [blame] | 6417 | return doDump(fd, DumpArgs(), asProto); |
| 6418 | } |
| 6419 | |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 6420 | void SurfaceFlinger::listLayers(std::string& result) const { |
| 6421 | for (const auto& layer : mLayerLifecycleManager.getLayers()) { |
| 6422 | StringAppendF(&result, "%s\n", layer->getDebugString().c_str()); |
| 6423 | } |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 6424 | } |
| 6425 | |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 6426 | void SurfaceFlinger::dumpStats(const DumpArgs& args, std::string& result) const { |
Ady Abraham | 2492a02 | 2020-07-24 11:09:55 -0700 | [diff] [blame] | 6427 | StringAppendF(&result, "%" PRId64 "\n", getVsyncPeriodFromHWC()); |
Dominik Laskowski | 66296b2 | 2022-07-12 08:20:36 -0700 | [diff] [blame] | 6428 | if (args.size() < 2) return; |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 6429 | |
Dominik Laskowski | 66296b2 | 2022-07-12 08:20:36 -0700 | [diff] [blame] | 6430 | const auto name = String8(args[1]); |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 6431 | traverseLegacyLayers([&](Layer* layer) { |
Tomasz Wasilczyk | b19fe0c | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 6432 | if (layer->getName() == name.c_str()) { |
Dominik Laskowski | 66296b2 | 2022-07-12 08:20:36 -0700 | [diff] [blame] | 6433 | layer->dumpFrameStats(result); |
| 6434 | } |
| 6435 | }); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6436 | } |
| 6437 | |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 6438 | void SurfaceFlinger::clearStats(const DumpArgs& args, std::string&) { |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 6439 | const bool clearAll = args.size() < 2; |
| 6440 | const auto name = clearAll ? String8() : String8(args[1]); |
| 6441 | |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 6442 | traverseLegacyLayers([&](Layer* layer) { |
Tomasz Wasilczyk | b19fe0c | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 6443 | if (clearAll || layer->getName() == name.c_str()) { |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 6444 | layer->clearFrameStats(); |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 6445 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 6446 | }); |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 6447 | } |
| 6448 | |
Dominik Laskowski | c286714 | 2019-01-21 11:33:38 -0800 | [diff] [blame] | 6449 | void SurfaceFlinger::dumpTimeStats(const DumpArgs& args, bool asProto, std::string& result) const { |
| 6450 | mTimeStats->parseArgs(asProto, args, result); |
| 6451 | } |
| 6452 | |
Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 6453 | void SurfaceFlinger::dumpFrameTimeline(const DumpArgs& args, std::string& result) const { |
| 6454 | mFrameTimeline->parseArgs(args, result); |
| 6455 | } |
| 6456 | |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 6457 | void SurfaceFlinger::logFrameStats(TimePoint now) { |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 6458 | static TimePoint sTimestamp = now; |
| 6459 | if (now - sTimestamp < 30min) return; |
| 6460 | sTimestamp = now; |
| 6461 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 6462 | SFTRACE_CALL(); |
Dominik Laskowski | 66296b2 | 2022-07-12 08:20:36 -0700 | [diff] [blame] | 6463 | mDrawingState.traverse([&](Layer* layer) { layer->logFrameStats(); }); |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 6464 | } |
| 6465 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6466 | void SurfaceFlinger::appendSfConfigString(std::string& result) const { |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 6467 | result.append(" [sf"); |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 6468 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6469 | StringAppendF(&result, " PRESENT_TIME_OFFSET=%" PRId64, dispSyncPresentTimeOffset); |
| 6470 | StringAppendF(&result, " FORCE_HWC_FOR_RBG_TO_YUV=%d", useHwcForRgbToYuv); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 6471 | StringAppendF(&result, " MAX_VIRT_DISPLAY_DIM=%zu", |
| 6472 | getHwComposer().getMaxVirtualDisplayDimension()); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6473 | StringAppendF(&result, " RUNNING_WITHOUT_SYNC_FRAMEWORK=%d", !hasSyncFramework); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6474 | StringAppendF(&result, " NUM_FRAMEBUFFER_SURFACE_BUFFERS=%" PRId64, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6475 | maxFrameBufferAcquiredBuffers); |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 6476 | result.append("]"); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 6477 | } |
| 6478 | |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 6479 | void SurfaceFlinger::dumpScheduler(std::string& result) const { |
| 6480 | utils::Dumper dumper{result}; |
| 6481 | |
| 6482 | mScheduler->dump(dumper); |
| 6483 | |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 6484 | // TODO(b/241285876): Move to DisplayModeController. |
| 6485 | dumper.dump("debugDisplayModeSetByBackdoor"sv, mDebugDisplayModeSetByBackdoor); |
| 6486 | dumper.eol(); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 6487 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 6488 | StringAppendF(&result, |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 6489 | " present offset: %9" PRId64 " ns\t VSYNC period: %9" PRId64 |
| 6490 | " ns\n\n", |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6491 | dispSyncPresentTimeOffset, getVsyncPeriodFromHWC()); |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 6492 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 6493 | |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 6494 | void SurfaceFlinger::dumpEvents(std::string& result) const { |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 6495 | mScheduler->dump(scheduler::Cycle::Render, result); |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 6496 | } |
| 6497 | |
| 6498 | void SurfaceFlinger::dumpVsync(std::string& result) const { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 6499 | mScheduler->dumpVsync(result); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 6500 | } |
| 6501 | |
Dan Stoza | 269dc4d | 2021-01-15 15:07:43 -0800 | [diff] [blame] | 6502 | void SurfaceFlinger::dumpPlannerInfo(const DumpArgs& args, std::string& result) const { |
| 6503 | for (const auto& [token, display] : mDisplays) { |
| 6504 | const auto compositionDisplay = display->getCompositionDisplay(); |
| 6505 | compositionDisplay->dumpPlannerInfo(args, result); |
| 6506 | } |
| 6507 | } |
| 6508 | |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 6509 | void SurfaceFlinger::dumpCompositionDisplays(std::string& result) const { |
| 6510 | for (const auto& [token, display] : mDisplays) { |
| 6511 | display->getCompositionDisplay()->dump(result); |
| 6512 | result += '\n'; |
| 6513 | } |
| 6514 | } |
| 6515 | |
| 6516 | void SurfaceFlinger::dumpDisplays(std::string& result) const { |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame] | 6517 | utils::Dumper dumper{result}; |
| 6518 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 6519 | for (const auto& [id, display] : mPhysicalDisplays) { |
Dominik Laskowski | 5d7de5f | 2022-11-03 12:38:32 -0400 | [diff] [blame] | 6520 | utils::Dumper::Section section(dumper, ftl::Concat("Display ", id.value).str()); |
| 6521 | |
| 6522 | display.snapshot().dump(dumper); |
| 6523 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 6524 | if (const auto device = getDisplayDeviceLocked(id)) { |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame] | 6525 | device->dump(dumper); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 6526 | } |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 6527 | } |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 6528 | |
| 6529 | for (const auto& [token, display] : mDisplays) { |
| 6530 | if (display->isVirtual()) { |
Dominik Laskowski | 5d7de5f | 2022-11-03 12:38:32 -0400 | [diff] [blame] | 6531 | const auto displayId = display->getId(); |
| 6532 | utils::Dumper::Section section(dumper, |
| 6533 | ftl::Concat("Virtual Display ", displayId.value).str()); |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame] | 6534 | display->dump(dumper); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 6535 | } |
| 6536 | } |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 6537 | } |
| 6538 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6539 | void SurfaceFlinger::dumpDisplayIdentificationData(std::string& result) const { |
Dominik Laskowski | 9fae102 | 2018-05-29 13:17:40 -0700 | [diff] [blame] | 6540 | for (const auto& [token, display] : mDisplays) { |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 6541 | const auto displayId = PhysicalDisplayId::tryCast(display->getId()); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 6542 | if (!displayId) { |
| 6543 | continue; |
| 6544 | } |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 6545 | const auto hwcDisplayId = getHwComposer().fromPhysicalDisplayId(*displayId); |
Dominik Laskowski | 7e04546 | 2018-05-30 13:02:02 -0700 | [diff] [blame] | 6546 | if (!hwcDisplayId) { |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 6547 | continue; |
| 6548 | } |
| 6549 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6550 | StringAppendF(&result, |
| 6551 | "Display %s (HWC display %" PRIu64 "): ", to_string(*displayId).c_str(), |
| 6552 | *hwcDisplayId); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 6553 | uint8_t port; |
| 6554 | DisplayIdentificationData data; |
Dominik Laskowski | 7e04546 | 2018-05-30 13:02:02 -0700 | [diff] [blame] | 6555 | if (!getHwComposer().getDisplayIdentificationData(*hwcDisplayId, &port, &data)) { |
Brian Lindahl | 8a96ef9 | 2024-05-24 14:46:29 +0000 | [diff] [blame] | 6556 | result.append("no display identification data\n"); |
| 6557 | continue; |
| 6558 | } |
| 6559 | |
| 6560 | if (data.empty()) { |
| 6561 | result.append("empty display identification data\n"); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 6562 | continue; |
| 6563 | } |
| 6564 | |
| 6565 | if (!isEdid(data)) { |
Brian Lindahl | 8a96ef9 | 2024-05-24 14:46:29 +0000 | [diff] [blame] | 6566 | result.append("unknown format for display identification data\n"); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 6567 | continue; |
| 6568 | } |
| 6569 | |
| 6570 | const auto edid = parseEdid(data); |
| 6571 | if (!edid) { |
Dominik Laskowski | b6e5437 | 2019-09-04 14:06:28 -0700 | [diff] [blame] | 6572 | result.append("invalid EDID\n"); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 6573 | continue; |
| 6574 | } |
| 6575 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6576 | StringAppendF(&result, "port=%u pnpId=%s displayName=\"", port, edid->pnpId.data()); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 6577 | result.append(edid->displayName.data(), edid->displayName.length()); |
| 6578 | result.append("\"\n"); |
| 6579 | } |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 6580 | } |
| 6581 | |
Dominik Laskowski | b6e5437 | 2019-09-04 14:06:28 -0700 | [diff] [blame] | 6582 | void SurfaceFlinger::dumpRawDisplayIdentificationData(const DumpArgs& args, |
| 6583 | std::string& result) const { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 6584 | hal::HWDisplayId hwcDisplayId; |
Dominik Laskowski | b6e5437 | 2019-09-04 14:06:28 -0700 | [diff] [blame] | 6585 | uint8_t port; |
| 6586 | DisplayIdentificationData data; |
| 6587 | |
| 6588 | if (args.size() > 1 && base::ParseUint(String8(args[1]), &hwcDisplayId) && |
| 6589 | getHwComposer().getDisplayIdentificationData(hwcDisplayId, &port, &data)) { |
| 6590 | result.append(reinterpret_cast<const char*>(data.data()), data.size()); |
| 6591 | } |
| 6592 | } |
| 6593 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6594 | void SurfaceFlinger::dumpWideColorInfo(std::string& result) const { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6595 | StringAppendF(&result, "Device supports wide color: %d\n", mSupportsWideColor); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6596 | StringAppendF(&result, "DisplayColorSetting: %s\n", |
| 6597 | decodeDisplayColorSetting(mDisplayColorSetting).c_str()); |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 6598 | |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 6599 | // TODO: print out if wide-color mode is active or not. |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 6600 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 6601 | for (const auto& [id, display] : mPhysicalDisplays) { |
| 6602 | StringAppendF(&result, "Display %s color modes:\n", to_string(id).c_str()); |
| 6603 | for (const auto mode : display.snapshot().colorModes()) { |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 6604 | StringAppendF(&result, " %s (%d)\n", decodeColorMode(mode).c_str(), |
| 6605 | fmt::underlying(mode)); |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 6606 | } |
| 6607 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 6608 | if (const auto display = getDisplayDeviceLocked(id)) { |
| 6609 | ui::ColorMode currentMode = display->getCompositionDisplay()->getState().colorMode; |
| 6610 | StringAppendF(&result, " Current color mode: %s (%d)\n", |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 6611 | decodeColorMode(currentMode).c_str(), fmt::underlying(currentMode)); |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 6612 | } |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 6613 | } |
| 6614 | result.append("\n"); |
| 6615 | } |
| 6616 | |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 6617 | void SurfaceFlinger::dumpHdrInfo(std::string& result) const { |
| 6618 | for (const auto& [displayId, listener] : mHdrLayerInfoListeners) { |
| 6619 | StringAppendF(&result, "HDR events for display %" PRIu64 "\n", displayId.value); |
| 6620 | listener->dump(result); |
| 6621 | result.append("\n"); |
| 6622 | } |
| 6623 | } |
| 6624 | |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 6625 | void SurfaceFlinger::dumpFrontEnd(std::string& result) { |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6626 | std::ostringstream out; |
| 6627 | out << "\nComposition list\n"; |
| 6628 | ui::LayerStack lastPrintedLayerStackHeader = ui::INVALID_LAYER_STACK; |
| 6629 | for (const auto& snapshot : mLayerSnapshotBuilder.getSnapshots()) { |
| 6630 | if (lastPrintedLayerStackHeader != snapshot->outputFilter.layerStack) { |
| 6631 | lastPrintedLayerStackHeader = snapshot->outputFilter.layerStack; |
| 6632 | out << "LayerStack=" << lastPrintedLayerStackHeader.id << "\n"; |
| 6633 | } |
| 6634 | out << " " << *snapshot << "\n"; |
| 6635 | } |
| 6636 | |
| 6637 | out << "\nInput list\n"; |
| 6638 | lastPrintedLayerStackHeader = ui::INVALID_LAYER_STACK; |
| 6639 | mLayerSnapshotBuilder.forEachInputSnapshot([&](const frontend::LayerSnapshot& snapshot) { |
| 6640 | if (lastPrintedLayerStackHeader != snapshot.outputFilter.layerStack) { |
| 6641 | lastPrintedLayerStackHeader = snapshot.outputFilter.layerStack; |
| 6642 | out << "LayerStack=" << lastPrintedLayerStackHeader.id << "\n"; |
| 6643 | } |
| 6644 | out << " " << snapshot << "\n"; |
| 6645 | }); |
| 6646 | |
| 6647 | out << "\nLayer Hierarchy\n" |
| 6648 | << mLayerHierarchyBuilder.getHierarchy().dump() << "\nOffscreen Hierarchy\n" |
| 6649 | << mLayerHierarchyBuilder.getOffscreenHierarchy().dump() << "\n\n"; |
| 6650 | result.append(out.str()); |
| 6651 | } |
| 6652 | |
| 6653 | void SurfaceFlinger::dumpVisibleFrontEnd(std::string& result) { |
| 6654 | if (!mLayerLifecycleManagerEnabled) { |
| 6655 | StringAppendF(&result, "Composition layers\n"); |
| 6656 | mDrawingState.traverseInZOrder([&](Layer* layer) { |
| 6657 | auto* compositionState = layer->getCompositionState(); |
| 6658 | if (!compositionState || !compositionState->isVisible) return; |
| 6659 | android::base::StringAppendF(&result, "* Layer %p (%s)\n", layer, |
| 6660 | layer->getDebugName() ? layer->getDebugName() |
| 6661 | : "<unknown>"); |
| 6662 | compositionState->dump(result); |
| 6663 | }); |
| 6664 | |
| 6665 | StringAppendF(&result, "Offscreen Layers\n"); |
| 6666 | for (Layer* offscreenLayer : mOffscreenLayers) { |
| 6667 | offscreenLayer->traverse(LayerVector::StateSet::Drawing, |
| 6668 | [&](Layer* layer) { layer->dumpOffscreenDebugInfo(result); }); |
| 6669 | } |
| 6670 | } else { |
| 6671 | std::ostringstream out; |
| 6672 | out << "\nComposition list\n"; |
| 6673 | ui::LayerStack lastPrintedLayerStackHeader = ui::INVALID_LAYER_STACK; |
| 6674 | mLayerSnapshotBuilder.forEachVisibleSnapshot( |
| 6675 | [&](std::unique_ptr<frontend::LayerSnapshot>& snapshot) { |
| 6676 | if (snapshot->hasSomethingToDraw()) { |
| 6677 | if (lastPrintedLayerStackHeader != snapshot->outputFilter.layerStack) { |
| 6678 | lastPrintedLayerStackHeader = snapshot->outputFilter.layerStack; |
| 6679 | out << "LayerStack=" << lastPrintedLayerStackHeader.id << "\n"; |
| 6680 | } |
| 6681 | out << " " << *snapshot << "\n"; |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 6682 | } |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6683 | }); |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 6684 | |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6685 | out << "\nInput list\n"; |
| 6686 | lastPrintedLayerStackHeader = ui::INVALID_LAYER_STACK; |
| 6687 | mLayerSnapshotBuilder.forEachInputSnapshot([&](const frontend::LayerSnapshot& snapshot) { |
| 6688 | if (lastPrintedLayerStackHeader != snapshot.outputFilter.layerStack) { |
| 6689 | lastPrintedLayerStackHeader = snapshot.outputFilter.layerStack; |
| 6690 | out << "LayerStack=" << lastPrintedLayerStackHeader.id << "\n"; |
| 6691 | } |
| 6692 | out << " " << snapshot << "\n"; |
| 6693 | }); |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 6694 | |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6695 | out << "\nLayer Hierarchy\n" |
| 6696 | << mLayerHierarchyBuilder.getHierarchy() << "\nOffscreen Hierarchy\n" |
| 6697 | << mLayerHierarchyBuilder.getOffscreenHierarchy() << "\n\n"; |
| 6698 | result = out.str(); |
| 6699 | dumpHwcLayersMinidump(result); |
| 6700 | } |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 6701 | } |
| 6702 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 6703 | perfetto::protos::LayersProto SurfaceFlinger::dumpDrawingStateProto(uint32_t traceFlags) const { |
Nataniel Borges | 63b3c87 | 2022-11-03 16:22:24 +0000 | [diff] [blame] | 6704 | std::unordered_set<uint64_t> stackIdsToSkip; |
| 6705 | |
| 6706 | // Determine if virtual layers display should be skipped |
| 6707 | if ((traceFlags & LayerTracing::TRACE_VIRTUAL_DISPLAYS) == 0) { |
| 6708 | for (const auto& [_, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
| 6709 | if (display->isVirtual()) { |
| 6710 | stackIdsToSkip.insert(display->getLayerStack().id); |
| 6711 | } |
| 6712 | } |
| 6713 | } |
| 6714 | |
Vishnu Nair | 22be349 | 2023-04-20 10:08:21 -0700 | [diff] [blame] | 6715 | return LayerProtoFromSnapshotGenerator(mLayerSnapshotBuilder, mFrontEndDisplayInfos, |
| 6716 | mLegacyLayers, traceFlags) |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 6717 | .generate(mLayerHierarchyBuilder.getHierarchy()); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 6718 | } |
| 6719 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 6720 | google::protobuf::RepeatedPtrField<perfetto::protos::DisplayProto> |
| 6721 | SurfaceFlinger::dumpDisplayProto() const { |
| 6722 | google::protobuf::RepeatedPtrField<perfetto::protos::DisplayProto> displays; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 6723 | for (const auto& [_, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 6724 | perfetto::protos::DisplayProto* displayProto = displays.Add(); |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 6725 | displayProto->set_id(display->getId().value); |
| 6726 | displayProto->set_name(display->getDisplayName()); |
| 6727 | displayProto->set_layer_stack(display->getLayerStack().id); |
DoÄŸancan Emek | fc4b6ed | 2023-07-26 15:24:04 +0000 | [diff] [blame] | 6728 | |
| 6729 | if (!display->isVirtual()) { |
| 6730 | const auto dpi = display->refreshRateSelector().getActiveMode().modePtr->getDpi(); |
| 6731 | displayProto->set_dpi_x(dpi.x); |
| 6732 | displayProto->set_dpi_y(dpi.y); |
| 6733 | } |
| 6734 | |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 6735 | LayerProtoHelper::writeSizeToProto(display->getWidth(), display->getHeight(), |
| 6736 | [&]() { return displayProto->mutable_size(); }); |
| 6737 | LayerProtoHelper::writeToProto(display->getLayerStackSpaceRect(), [&]() { |
| 6738 | return displayProto->mutable_layer_stack_space_rect(); |
| 6739 | }); |
| 6740 | LayerProtoHelper::writeTransformToProto(display->getTransform(), |
| 6741 | displayProto->mutable_transform()); |
Vishnu Nair | 791d48a | 2021-12-02 16:55:13 -0800 | [diff] [blame] | 6742 | displayProto->set_is_virtual(display->isVirtual()); |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 6743 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 6744 | return displays; |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 6745 | } |
| 6746 | |
Alec Mouri | 6b9e991 | 2020-01-21 10:50:24 -0800 | [diff] [blame] | 6747 | void SurfaceFlinger::dumpHwc(std::string& result) const { |
| 6748 | getHwComposer().dump(result); |
| 6749 | } |
| 6750 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 6751 | void SurfaceFlinger::dumpOffscreenLayersProto(perfetto::protos::LayersProto& layersProto, |
| 6752 | uint32_t traceFlags) const { |
Vishnu Nair | 0f085c6 | 2019-08-30 08:49:12 -0700 | [diff] [blame] | 6753 | // Add a fake invisible root layer to the proto output and parent all the offscreen layers to |
| 6754 | // it. |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 6755 | perfetto::protos::LayerProto* rootProto = layersProto.add_layers(); |
Vishnu Nair | 0f085c6 | 2019-08-30 08:49:12 -0700 | [diff] [blame] | 6756 | const int32_t offscreenRootLayerId = INT32_MAX - 2; |
| 6757 | rootProto->set_id(offscreenRootLayerId); |
| 6758 | rootProto->set_name("Offscreen Root"); |
Vishnu Nair | d5aeb61 | 2019-09-20 14:39:39 -0700 | [diff] [blame] | 6759 | rootProto->set_parent(-1); |
Vishnu Nair | 0f085c6 | 2019-08-30 08:49:12 -0700 | [diff] [blame] | 6760 | |
| 6761 | for (Layer* offscreenLayer : mOffscreenLayers) { |
| 6762 | // Add layer as child of the fake root |
| 6763 | rootProto->add_children(offscreenLayer->sequence); |
| 6764 | |
| 6765 | // Add layer |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 6766 | auto* layerProto = offscreenLayer->writeToProto(layersProto, traceFlags); |
Vishnu Nair | 0f085c6 | 2019-08-30 08:49:12 -0700 | [diff] [blame] | 6767 | layerProto->set_parent(offscreenRootLayerId); |
Vishnu Nair | 0f085c6 | 2019-08-30 08:49:12 -0700 | [diff] [blame] | 6768 | } |
| 6769 | } |
| 6770 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 6771 | perfetto::protos::LayersProto SurfaceFlinger::dumpProtoFromMainThread(uint32_t traceFlags) { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 6772 | return mScheduler |
| 6773 | ->schedule([=, this]() FTL_FAKE_GUARD(kMainThreadContext) { |
| 6774 | return dumpDrawingStateProto(traceFlags); |
| 6775 | }) |
| 6776 | .get(); |
Vishnu Nair | 8406fd7 | 2019-07-30 11:29:31 -0700 | [diff] [blame] | 6777 | } |
| 6778 | |
Vishnu Nair | 0f085c6 | 2019-08-30 08:49:12 -0700 | [diff] [blame] | 6779 | void SurfaceFlinger::dumpOffscreenLayers(std::string& result) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6780 | auto future = mScheduler->schedule([this] { |
| 6781 | std::string result; |
| 6782 | for (Layer* offscreenLayer : mOffscreenLayers) { |
| 6783 | offscreenLayer->traverse(LayerVector::StateSet::Drawing, |
Vishnu Nair | 76554eb | 2022-12-09 03:38:36 +0000 | [diff] [blame] | 6784 | [&](Layer* layer) { layer->dumpOffscreenDebugInfo(result); }); |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6785 | } |
| 6786 | return result; |
| 6787 | }); |
| 6788 | |
Vishnu Nair | 0f085c6 | 2019-08-30 08:49:12 -0700 | [diff] [blame] | 6789 | result.append("Offscreen Layers:\n"); |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6790 | result.append(future.get()); |
Vishnu Nair | 0f085c6 | 2019-08-30 08:49:12 -0700 | [diff] [blame] | 6791 | } |
| 6792 | |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 6793 | void SurfaceFlinger::dumpHwcLayersMinidumpLockedLegacy(std::string& result) const { |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6794 | for (const auto& [token, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
Ady Abraham | 5ad28d6 | 2022-08-11 11:26:57 -0700 | [diff] [blame] | 6795 | const auto displayId = HalDisplayId::tryCast(display->getId()); |
| 6796 | if (!displayId) { |
| 6797 | continue; |
| 6798 | } |
| 6799 | |
| 6800 | StringAppendF(&result, "Display %s (%s) HWC layers:\n", to_string(*displayId).c_str(), |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 6801 | displayId == mActiveDisplayId ? "active" : "inactive"); |
Ady Abraham | 5ad28d6 | 2022-08-11 11:26:57 -0700 | [diff] [blame] | 6802 | Layer::miniDumpHeader(result); |
| 6803 | |
| 6804 | const DisplayDevice& ref = *display; |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 6805 | mDrawingState.traverseInZOrder([&](Layer* layer) { layer->miniDumpLegacy(result, ref); }); |
| 6806 | result.append("\n"); |
| 6807 | } |
| 6808 | } |
| 6809 | |
| 6810 | void SurfaceFlinger::dumpHwcLayersMinidump(std::string& result) const { |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6811 | if (!mLayerLifecycleManagerEnabled) { |
| 6812 | return dumpHwcLayersMinidumpLockedLegacy(result); |
| 6813 | } |
| 6814 | for (const auto& [token, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 6815 | const auto displayId = HalDisplayId::tryCast(display->getId()); |
| 6816 | if (!displayId) { |
| 6817 | continue; |
| 6818 | } |
| 6819 | |
| 6820 | StringAppendF(&result, "Display %s (%s) HWC layers:\n", to_string(*displayId).c_str(), |
| 6821 | displayId == mActiveDisplayId ? "active" : "inactive"); |
| 6822 | Layer::miniDumpHeader(result); |
| 6823 | |
| 6824 | const DisplayDevice& ref = *display; |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 6825 | mLayerSnapshotBuilder.forEachVisibleSnapshot( |
| 6826 | [&](const frontend::LayerSnapshot& snapshot) FTL_FAKE_GUARD(kMainThreadContext) { |
| 6827 | if (!snapshot.hasSomethingToDraw() || |
| 6828 | ref.getLayerStack() != snapshot.outputFilter.layerStack) { |
| 6829 | return; |
| 6830 | } |
| 6831 | auto it = mLegacyLayers.find(snapshot.sequence); |
| 6832 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 6833 | "Couldnt find layer object for %s", |
| 6834 | snapshot.getDebugString().c_str()); |
| 6835 | it->second->miniDump(result, snapshot, ref); |
| 6836 | }); |
Ady Abraham | 5ad28d6 | 2022-08-11 11:26:57 -0700 | [diff] [blame] | 6837 | result.append("\n"); |
| 6838 | } |
| 6839 | } |
| 6840 | |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 6841 | void SurfaceFlinger::dumpAll(const DumpArgs& args, const std::string& compositionLayers, |
| 6842 | std::string& result) const { |
| 6843 | TimedLock lock(mStateLock, s2ns(1), __func__); |
| 6844 | if (!lock.locked()) { |
| 6845 | StringAppendF(&result, "Dumping without lock after timeout: %s (%d)\n", |
| 6846 | strerror(-lock.status), lock.status); |
| 6847 | } |
| 6848 | |
Dominik Laskowski | c286714 | 2019-01-21 11:33:38 -0800 | [diff] [blame] | 6849 | const bool colorize = !args.empty() && args[0] == String16("--color"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6850 | Colorizer colorizer(colorize); |
| 6851 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6852 | // figure out if we're stuck somewhere |
| 6853 | const nsecs_t now = systemTime(); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6854 | const nsecs_t inTransaction(mDebugInTransaction); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6855 | nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0; |
| 6856 | |
| 6857 | /* |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 6858 | * Dump library configuration. |
| 6859 | */ |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6860 | |
| 6861 | colorizer.bold(result); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 6862 | result.append("Build configuration:"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6863 | colorizer.reset(result); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 6864 | appendSfConfigString(result); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 6865 | result.append("\n"); |
| 6866 | |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 6867 | result.append("\nDisplay identification data:\n"); |
| 6868 | dumpDisplayIdentificationData(result); |
| 6869 | |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 6870 | result.append("\nWide-Color information:\n"); |
| 6871 | dumpWideColorInfo(result); |
| 6872 | |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 6873 | dumpHdrInfo(result); |
| 6874 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6875 | colorizer.bold(result); |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 6876 | result.append("Sync configuration: "); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6877 | colorizer.reset(result); |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 6878 | result.append(SyncFeatures::getInstance().toString()); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 6879 | result.append("\n\n"); |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 6880 | |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 6881 | colorizer.bold(result); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 6882 | result.append("Scheduler:\n"); |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 6883 | colorizer.reset(result); |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 6884 | dumpScheduler(result); |
| 6885 | dumpEvents(result); |
| 6886 | dumpVsync(result); |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 6887 | result.append("\n"); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 6888 | |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 6889 | /* |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6890 | * Dump the visible layer list |
| 6891 | */ |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6892 | colorizer.bold(result); |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 6893 | StringAppendF(&result, "SurfaceFlinger New Frontend Enabled:%s\n", |
| 6894 | mLayerLifecycleManagerEnabled ? "true" : "false"); |
| 6895 | StringAppendF(&result, "Active Layers - layers with client handles (count = %zu)\n", |
| 6896 | mNumLayers.load()); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6897 | colorizer.reset(result); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 6898 | |
Alec Mouri | 6761733 | 2022-09-26 21:37:01 +0000 | [diff] [blame] | 6899 | result.append(compositionLayers); |
Lloyd Pique | 207def9 | 2019-02-28 16:09:52 -0800 | [diff] [blame] | 6900 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6901 | colorizer.bold(result); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6902 | StringAppendF(&result, "Displays (%zu entries)\n", mDisplays.size()); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6903 | colorizer.reset(result); |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 6904 | dumpDisplays(result); |
| 6905 | dumpCompositionDisplays(result); |
| 6906 | result.push_back('\n'); |
Lloyd Pique | c3cb729 | 2019-05-17 15:25:14 -0700 | [diff] [blame] | 6907 | |
| 6908 | mCompositionEngine->dump(result); |
| 6909 | |
| 6910 | /* |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6911 | * Dump SurfaceFlinger global state |
| 6912 | */ |
| 6913 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6914 | colorizer.bold(result); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 6915 | result.append("SurfaceFlinger global state:\n"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6916 | colorizer.reset(result); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6917 | |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 6918 | getRenderEngine().dump(result); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6919 | |
Robert Carr | beba6f0 | 2021-02-10 21:06:27 -0800 | [diff] [blame] | 6920 | result.append("ClientCache state:\n"); |
| 6921 | ClientCache::getInstance().dump(result); |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 6922 | DebugEGLImageTracker::getInstance()->dump(result); |
| 6923 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 6924 | if (const auto display = getDefaultDisplayDeviceLocked()) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 6925 | display->getCompositionDisplay()->getState().undefinedRegion.dump(result, |
| 6926 | "undefinedRegion"); |
Dominik Laskowski | 718f960 | 2019-11-09 20:01:35 -0800 | [diff] [blame] | 6927 | StringAppendF(&result, " orientation=%s, isPoweredOn=%d\n", |
| 6928 | toCString(display->getOrientation()), display->isPoweredOn()); |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 6929 | } |
Carlos Martinez Romero | 779dc1f | 2022-10-27 23:23:30 +0000 | [diff] [blame] | 6930 | StringAppendF(&result, " transaction-flags : %08x\n", mTransactionFlags.load()); |
Dominik Laskowski | 45de9bd | 2018-06-11 17:44:10 -0700 | [diff] [blame] | 6931 | |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 6932 | if (const auto display = getDefaultDisplayDeviceLocked()) { |
Ying Wei | 784a8e7 | 2024-04-20 08:46:42 +0000 | [diff] [blame] | 6933 | std::string peakFps, xDpi, yDpi; |
| 6934 | const auto activeMode = display->refreshRateSelector().getActiveMode(); |
| 6935 | if (const auto activeModePtr = activeMode.modePtr.get()) { |
| 6936 | peakFps = to_string(activeMode.modePtr->getPeakFps()); |
Dominik Laskowski | 02bb207 | 2022-08-26 15:02:52 -0700 | [diff] [blame] | 6937 | const auto dpi = activeModePtr->getDpi(); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 6938 | xDpi = base::StringPrintf("%.2f", dpi.x); |
| 6939 | yDpi = base::StringPrintf("%.2f", dpi.y); |
Marin Shalamanov | 9afcbd8 | 2020-08-19 12:34:59 +0200 | [diff] [blame] | 6940 | } else { |
Ying Wei | 784a8e7 | 2024-04-20 08:46:42 +0000 | [diff] [blame] | 6941 | peakFps = "unknown"; |
Marin Shalamanov | 9afcbd8 | 2020-08-19 12:34:59 +0200 | [diff] [blame] | 6942 | xDpi = "unknown"; |
| 6943 | yDpi = "unknown"; |
| 6944 | } |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6945 | StringAppendF(&result, |
Ying Wei | 784a8e7 | 2024-04-20 08:46:42 +0000 | [diff] [blame] | 6946 | " peak-refresh-rate : %s\n" |
Marin Shalamanov | 9afcbd8 | 2020-08-19 12:34:59 +0200 | [diff] [blame] | 6947 | " x-dpi : %s\n" |
| 6948 | " y-dpi : %s\n", |
Ying Wei | 784a8e7 | 2024-04-20 08:46:42 +0000 | [diff] [blame] | 6949 | peakFps.c_str(), xDpi.c_str(), yDpi.c_str()); |
Dominik Laskowski | 45de9bd | 2018-06-11 17:44:10 -0700 | [diff] [blame] | 6950 | } |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6951 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6952 | StringAppendF(&result, " transaction time: %f us\n", inTransactionDuration / 1000.0); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6953 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 6954 | result.append("\nTransaction tracing: "); |
| 6955 | if (mTransactionTracing) { |
| 6956 | result.append("enabled\n"); |
| 6957 | mTransactionTracing->dump(result); |
| 6958 | } else { |
| 6959 | result.append("disabled\n"); |
| 6960 | } |
| 6961 | result.push_back('\n'); |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 6962 | |
Ady Abraham | 2790e9f | 2021-04-28 13:14:20 -0700 | [diff] [blame] | 6963 | { |
| 6964 | DumpArgs plannerArgs; |
| 6965 | plannerArgs.add(); // first argument is ignored |
| 6966 | plannerArgs.add(String16("--layers")); |
| 6967 | dumpPlannerInfo(plannerArgs, result); |
| 6968 | } |
| 6969 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6970 | /* |
| 6971 | * Dump HWComposer state |
| 6972 | */ |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6973 | colorizer.bold(result); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 6974 | result.append("h/w composer state:\n"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6975 | colorizer.reset(result); |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6976 | const bool hwcDisabled = mDebugDisableHWC || mDebugFlashDelay; |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6977 | StringAppendF(&result, " h/w composer %s\n", hwcDisabled ? "disabled" : "enabled"); |
Ady Abraham | c4acf51 | 2022-02-18 17:11:59 -0800 | [diff] [blame] | 6978 | dumpHwc(result); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6979 | |
| 6980 | /* |
| 6981 | * Dump gralloc state |
| 6982 | */ |
| 6983 | const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
| 6984 | alloc.dump(result); |
Karthik Ravi Shankar | 171155a | 2017-06-28 15:40:24 -0700 | [diff] [blame] | 6985 | |
rnlee | a2ecd83 | 2021-07-29 13:58:30 -0700 | [diff] [blame] | 6986 | /* |
| 6987 | * Dump flag/property manager state |
| 6988 | */ |
Ady Abraham | 53b0c49 | 2023-10-23 11:47:14 -0700 | [diff] [blame] | 6989 | FlagManager::getInstance().dump(result); |
rnlee | a2ecd83 | 2021-07-29 13:58:30 -0700 | [diff] [blame] | 6990 | |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 6991 | result.append(mTimeStats->miniDump()); |
| 6992 | result.append("\n"); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6993 | } |
| 6994 | |
chaviw | 17ac24b | 2021-01-28 18:50:05 -0800 | [diff] [blame] | 6995 | mat4 SurfaceFlinger::calculateColorMatrix(float saturation) { |
| 6996 | if (saturation == 1) { |
| 6997 | return mat4(); |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6998 | } |
| 6999 | |
chaviw | 17ac24b | 2021-01-28 18:50:05 -0800 | [diff] [blame] | 7000 | float3 luminance{0.213f, 0.715f, 0.072f}; |
| 7001 | luminance *= 1.0f - saturation; |
| 7002 | mat4 saturationMatrix = mat4(vec4{luminance.r + saturation, luminance.r, luminance.r, 0.0f}, |
| 7003 | vec4{luminance.g, luminance.g + saturation, luminance.g, 0.0f}, |
| 7004 | vec4{luminance.b, luminance.b, luminance.b + saturation, 0.0f}, |
| 7005 | vec4{0.0f, 0.0f, 0.0f, 1.0f}); |
| 7006 | return saturationMatrix; |
| 7007 | } |
| 7008 | |
| 7009 | void SurfaceFlinger::updateColorMatrixLocked() { |
| 7010 | mat4 colorMatrix = |
| 7011 | mClientColorMatrix * calculateColorMatrix(mGlobalSaturationFactor) * mDaltonizer(); |
| 7012 | |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 7013 | if (mCurrentState.colorMatrix != colorMatrix) { |
| 7014 | mCurrentState.colorMatrix = colorMatrix; |
| 7015 | mCurrentState.colorMatrixChanged = true; |
| 7016 | setTransactionFlags(eTransactionNeeded); |
| 7017 | } |
| 7018 | } |
| 7019 | |
Fabien Sanglard | 6e8e98a | 2016-10-27 20:04:11 -0700 | [diff] [blame] | 7020 | status_t SurfaceFlinger::CheckTransactCodeCredentials(uint32_t code) { |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 7021 | #pragma clang diagnostic push |
| 7022 | #pragma clang diagnostic error "-Wswitch-enum" |
| 7023 | switch (static_cast<ISurfaceComposerTag>(code)) { |
| 7024 | // These methods should at minimum make sure that the client requested |
| 7025 | // access to SF. |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 7026 | case GET_HDR_CAPABILITIES: |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 7027 | case GET_AUTO_LOW_LATENCY_MODE_SUPPORT: |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 7028 | case GET_GAME_CONTENT_TYPE_SUPPORT: |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 7029 | case ACQUIRE_FRAME_RATE_FLEXIBILITY_TOKEN: { |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 7030 | // OVERRIDE_HDR_TYPES is used by CTS tests, which acquire the necessary |
| 7031 | // permission dynamically. Don't use the permission cache for this check. |
| 7032 | bool usePermissionCache = code != OVERRIDE_HDR_TYPES; |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 7033 | if (!callingThreadHasUnscopedSurfaceFlingerAccess(usePermissionCache)) { |
Robert Carr | d4ae7f3 | 2018-06-07 16:10:57 -0700 | [diff] [blame] | 7034 | IPCThreadState* ipc = IPCThreadState::self(); |
| 7035 | ALOGE("Permission Denial: can't access SurfaceFlinger pid=%d, uid=%d", |
| 7036 | ipc->getCallingPid(), ipc->getCallingUid()); |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 7037 | return PERMISSION_DENIED; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7038 | } |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 7039 | return OK; |
| 7040 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 7041 | // The following calls are currently used by clients that do not |
| 7042 | // request necessary permissions. However, they do not expose any secret |
| 7043 | // information, so it is OK to pass them. |
Peiyong Lin | d1fedb4 | 2019-03-11 17:48:41 -0700 | [diff] [blame] | 7044 | case GET_ACTIVE_COLOR_MODE: |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 7045 | case GET_ACTIVE_DISPLAY_MODE: |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 7046 | case GET_DISPLAY_COLOR_MODES: |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 7047 | case GET_DISPLAY_MODES: |
Ady Abraham | 07d03c4 | 2023-09-27 19:15:08 -0700 | [diff] [blame] | 7048 | case GET_SCHEDULING_POLICY: |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 7049 | // Calling setTransactionState is safe, because you need to have been |
| 7050 | // granted a reference to Client* and Handle* to do anything with it. |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 7051 | case SET_TRANSACTION_STATE: { |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 7052 | // This is not sensitive information, so should not require permission control. |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 7053 | return OK; |
| 7054 | } |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 7055 | case BOOT_FINISHED: |
| 7056 | // Used by apps to hook Choreographer to SurfaceFlinger. |
| 7057 | case CREATE_DISPLAY_EVENT_CONNECTION: |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 7058 | case CREATE_CONNECTION: |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 7059 | case CREATE_VIRTUAL_DISPLAY: |
| 7060 | case DESTROY_VIRTUAL_DISPLAY: |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 7061 | case GET_PRIMARY_PHYSICAL_DISPLAY_ID: |
| 7062 | case GET_PHYSICAL_DISPLAY_IDS: |
| 7063 | case GET_PHYSICAL_DISPLAY_TOKEN: |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 7064 | case AUTHENTICATE_SURFACE: |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 7065 | case SET_POWER_MODE: |
Huihong Luo | 0a81aa3 | 2022-02-22 16:02:36 -0800 | [diff] [blame] | 7066 | case GET_SUPPORTED_FRAME_TIMESTAMPS: |
Huihong Luo | aa7fc2e | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 7067 | case GET_DISPLAY_STATE: |
| 7068 | case GET_DISPLAY_STATS: |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 7069 | case GET_STATIC_DISPLAY_INFO: |
Huihong Luo | 38603fd | 2022-02-21 14:32:54 -0800 | [diff] [blame] | 7070 | case GET_DYNAMIC_DISPLAY_INFO: |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 7071 | case GET_DISPLAY_NATIVE_PRIMARIES: |
| 7072 | case SET_ACTIVE_COLOR_MODE: |
| 7073 | case SET_BOOT_DISPLAY_MODE: |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 7074 | case CLEAR_BOOT_DISPLAY_MODE: |
| 7075 | case GET_BOOT_DISPLAY_MODE_SUPPORT: |
| 7076 | case SET_AUTO_LOW_LATENCY_MODE: |
| 7077 | case SET_GAME_CONTENT_TYPE: |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 7078 | case CAPTURE_LAYERS: |
| 7079 | case CAPTURE_DISPLAY: |
| 7080 | case CAPTURE_DISPLAY_BY_ID: |
Huihong Luo | 4ed1c91 | 2022-02-22 14:30:01 -0800 | [diff] [blame] | 7081 | case CLEAR_ANIMATION_FRAME_STATS: |
| 7082 | case GET_ANIMATION_FRAME_STATS: |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 7083 | case OVERRIDE_HDR_TYPES: |
| 7084 | case ON_PULL_ATOM: |
| 7085 | case ENABLE_VSYNC_INJECTIONS: |
| 7086 | case INJECT_VSYNC: |
| 7087 | case GET_LAYER_DEBUG_INFO: |
| 7088 | case GET_COLOR_MANAGEMENT: |
| 7089 | case GET_COMPOSITION_PREFERENCE: |
| 7090 | case GET_DISPLAYED_CONTENT_SAMPLING_ATTRIBUTES: |
| 7091 | case SET_DISPLAY_CONTENT_SAMPLING_ENABLED: |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 7092 | case GET_DISPLAYED_CONTENT_SAMPLE: |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 7093 | case GET_PROTECTED_CONTENT_SUPPORT: |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 7094 | case IS_WIDE_COLOR_DISPLAY: |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 7095 | case ADD_REGION_SAMPLING_LISTENER: |
| 7096 | case REMOVE_REGION_SAMPLING_LISTENER: |
| 7097 | case ADD_FPS_LISTENER: |
| 7098 | case REMOVE_FPS_LISTENER: |
| 7099 | case ADD_TUNNEL_MODE_ENABLED_LISTENER: |
| 7100 | case REMOVE_TUNNEL_MODE_ENABLED_LISTENER: |
| 7101 | case ADD_WINDOW_INFOS_LISTENER: |
| 7102 | case REMOVE_WINDOW_INFOS_LISTENER: |
| 7103 | case SET_DESIRED_DISPLAY_MODE_SPECS: |
| 7104 | case GET_DESIRED_DISPLAY_MODE_SPECS: |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 7105 | case GET_DISPLAY_BRIGHTNESS_SUPPORT: |
| 7106 | case SET_DISPLAY_BRIGHTNESS: |
| 7107 | case ADD_HDR_LAYER_INFO_LISTENER: |
| 7108 | case REMOVE_HDR_LAYER_INFO_LISTENER: |
| 7109 | case NOTIFY_POWER_BOOST: |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 7110 | case SET_GLOBAL_SHADOW_SETTINGS: |
| 7111 | case GET_DISPLAY_DECORATION_SUPPORT: |
| 7112 | case SET_FRAME_RATE: |
| 7113 | case SET_OVERRIDE_FRAME_RATE: |
| 7114 | case SET_FRAME_TIMELINE_INFO: |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 7115 | case ADD_TRANSACTION_TRACE_LISTENER: |
| 7116 | case GET_GPU_CONTEXT_PRIORITY: |
| 7117 | case GET_MAX_ACQUIRED_BUFFER_COUNT: |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 7118 | LOG_FATAL("Deprecated opcode: %d, migrated to AIDL", code); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 7119 | return PERMISSION_DENIED; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7120 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 7121 | |
| 7122 | // These codes are used for the IBinder protocol to either interrogate the recipient |
| 7123 | // side of the transaction for its canonical interface descriptor or to dump its state. |
| 7124 | // We let them pass by default. |
| 7125 | if (code == IBinder::INTERFACE_TRANSACTION || code == IBinder::DUMP_TRANSACTION || |
| 7126 | code == IBinder::PING_TRANSACTION || code == IBinder::SHELL_COMMAND_TRANSACTION || |
| 7127 | code == IBinder::SYSPROPS_TRANSACTION) { |
| 7128 | return OK; |
| 7129 | } |
Alec Mouri | 1c7938e | 2023-09-22 04:17:23 +0000 | [diff] [blame] | 7130 | // Numbers from 1000 to 1045 are currently used for backdoors. The code |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 7131 | // in onTransact verifies that the user is root, and has access to use SF. |
Alec Mouri | 1c7938e | 2023-09-22 04:17:23 +0000 | [diff] [blame] | 7132 | if (code >= 1000 && code <= 1045) { |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 7133 | ALOGV("Accessing SurfaceFlinger through backdoor code: %u", code); |
| 7134 | return OK; |
| 7135 | } |
| 7136 | ALOGE("Permission Denial: SurfaceFlinger did not recognize request code: %u", code); |
| 7137 | return PERMISSION_DENIED; |
| 7138 | #pragma clang diagnostic pop |
Fabien Sanglard | 6e8e98a | 2016-10-27 20:04:11 -0700 | [diff] [blame] | 7139 | } |
| 7140 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 7141 | status_t SurfaceFlinger::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 7142 | uint32_t flags) { |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 7143 | if (const status_t error = CheckTransactCodeCredentials(code); error != OK) { |
| 7144 | return error; |
Fabien Sanglard | 6e8e98a | 2016-10-27 20:04:11 -0700 | [diff] [blame] | 7145 | } |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 7146 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7147 | status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); |
| 7148 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 7149 | CHECK_INTERFACE(ISurfaceComposer, data, reply); |
Romain Guy | 8c6bbd6 | 2017-06-05 13:51:43 -0700 | [diff] [blame] | 7150 | IPCThreadState* ipc = IPCThreadState::self(); |
| 7151 | const int uid = ipc->getCallingUid(); |
| 7152 | if (CC_UNLIKELY(uid != AID_SYSTEM |
| 7153 | && !PermissionCache::checkCallingPermission(sHardwareTest))) { |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 7154 | const int pid = ipc->getCallingPid(); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 7155 | ALOGE("Permission Denial: " |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 7156 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7157 | return PERMISSION_DENIED; |
| 7158 | } |
| 7159 | int n; |
| 7160 | switch (code) { |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 7161 | case 1000: // Unused. |
| 7162 | case 1001: |
| 7163 | return NAME_NOT_FOUND; |
| 7164 | case 1002: // Toggle flashing on surface damage. |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 7165 | sfdo_setDebugFlash(data.readInt32()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7166 | return NO_ERROR; |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 7167 | case 1004: // Force composite ahead of next VSYNC. |
Dominik Laskowski | 46f3e3b | 2021-08-10 11:44:24 -0700 | [diff] [blame] | 7168 | case 1006: |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 7169 | sfdo_scheduleComposite(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7170 | return NO_ERROR; |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 7171 | case 1005: { // Force commit ahead of next VSYNC. |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 7172 | sfdo_scheduleCommit(); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 7173 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7174 | } |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 7175 | case 1007: // Unused. |
| 7176 | return NAME_NOT_FOUND; |
| 7177 | case 1008: // Toggle forced GPU composition. |
Carlos Martinez Romero | b4ea9e6 | 2023-10-25 21:48:56 +0000 | [diff] [blame] | 7178 | sfdo_forceClientComposition(data.readInt32() != 0); |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 7179 | return NO_ERROR; |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 7180 | case 1009: // Toggle use of transform hint. |
| 7181 | mDebugDisableTransformHint = data.readInt32() != 0; |
| 7182 | scheduleRepaint(); |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 7183 | return NO_ERROR; |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 7184 | case 1010: // Interrogate. |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 7185 | reply->writeInt32(0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7186 | reply->writeInt32(0); |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 7187 | reply->writeInt32(mDebugFlashDelay); |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 7188 | reply->writeInt32(0); |
Dianne Hackborn | 12839be | 2012-02-06 21:21:05 -0800 | [diff] [blame] | 7189 | reply->writeInt32(mDebugDisableHWC); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7190 | return NO_ERROR; |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 7191 | case 1013: // Unused. |
| 7192 | return NAME_NOT_FOUND; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 7193 | case 1014: { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 7194 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 7195 | // daltonize |
| 7196 | n = data.readInt32(); |
Isaac Chai | 1ec2d9a | 2024-04-11 20:10:34 +0000 | [diff] [blame] | 7197 | mDaltonizer.setLevel(data.readInt32()); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 7198 | switch (n % 10) { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 7199 | case 1: |
| 7200 | mDaltonizer.setType(ColorBlindnessType::Protanomaly); |
| 7201 | break; |
| 7202 | case 2: |
| 7203 | mDaltonizer.setType(ColorBlindnessType::Deuteranomaly); |
| 7204 | break; |
| 7205 | case 3: |
| 7206 | mDaltonizer.setType(ColorBlindnessType::Tritanomaly); |
| 7207 | break; |
| 7208 | default: |
| 7209 | mDaltonizer.setType(ColorBlindnessType::None); |
| 7210 | break; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 7211 | } |
| 7212 | if (n >= 10) { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 7213 | mDaltonizer.setMode(ColorBlindnessMode::Correction); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 7214 | } else { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 7215 | mDaltonizer.setMode(ColorBlindnessMode::Simulation); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 7216 | } |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 7217 | |
| 7218 | updateColorMatrixLocked(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 7219 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7220 | } |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 7221 | case 1015: { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 7222 | Mutex::Autolock _l(mStateLock); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 7223 | // apply a color matrix |
| 7224 | n = data.readInt32(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 7225 | if (n) { |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 7226 | // color matrix is sent as a column-major mat4 matrix |
Alan Viverette | 794c5ba | 2013-10-03 16:40:52 -0700 | [diff] [blame] | 7227 | for (size_t i = 0 ; i < 4; i++) { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 7228 | for (size_t j = 0; j < 4; j++) { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 7229 | mClientColorMatrix[i][j] = data.readFloat(); |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 7230 | } |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 7231 | } |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 7232 | } else { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 7233 | mClientColorMatrix = mat4(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 7234 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 7235 | |
| 7236 | // Check that supplied matrix's last row is {0,0,0,1} so we can avoid |
| 7237 | // the division by w in the fragment shader |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 7238 | float4 lastRow(transpose(mClientColorMatrix)[3]); |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 7239 | if (any(greaterThan(abs(lastRow - float4{0, 0, 0, 1}), float4{1e-4f}))) { |
| 7240 | ALOGE("The color transform's last row must be (0, 0, 0, 1)"); |
| 7241 | } |
| 7242 | |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 7243 | updateColorMatrixLocked(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 7244 | return NO_ERROR; |
| 7245 | } |
Dominik Laskowski | 8d32ddc | 2019-08-07 11:25:35 -0700 | [diff] [blame] | 7246 | case 1016: { // Unused. |
| 7247 | return NAME_NOT_FOUND; |
Ruchi Kandoi | f52b3c8 | 2014-04-24 16:42:35 -0700 | [diff] [blame] | 7248 | } |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 7249 | case 1017: { |
| 7250 | n = data.readInt32(); |
Dominik Laskowski | 9dab343 | 2019-03-27 13:21:10 -0700 | [diff] [blame] | 7251 | mForceFullDamage = n != 0; |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 7252 | return NO_ERROR; |
| 7253 | } |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 7254 | case 1018: { // Set the render deadline as a duration until VSYNC. |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 7255 | n = data.readInt32(); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 7256 | mScheduler->setDuration(scheduler::Cycle::Render, std::chrono::nanoseconds(n), 0ns); |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 7257 | return NO_ERROR; |
| 7258 | } |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 7259 | case 1019: { // Set the deadline of the last composite as a duration until VSYNC. |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 7260 | n = data.readInt32(); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 7261 | mScheduler->setDuration(scheduler::Cycle::LastComposite, |
| 7262 | std::chrono::nanoseconds(n), 0ns); |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 7263 | return NO_ERROR; |
| 7264 | } |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 7265 | case 1020: { // Unused |
| 7266 | return NAME_NOT_FOUND; |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 7267 | } |
Dan Stoza | 8cf150a | 2016-08-02 10:27:31 -0700 | [diff] [blame] | 7268 | case 1021: { // Disable HWC virtual displays |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 7269 | const bool enable = data.readInt32() != 0; |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7270 | static_cast<void>( |
| 7271 | mScheduler->schedule([this, enable] { enableHalVirtualDisplays(enable); })); |
Dan Stoza | 8cf150a | 2016-08-02 10:27:31 -0700 | [diff] [blame] | 7272 | return NO_ERROR; |
| 7273 | } |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 7274 | case 1022: { // Set saturation boost |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 7275 | Mutex::Autolock _l(mStateLock); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 7276 | mGlobalSaturationFactor = std::max(0.0f, std::min(data.readFloat(), 2.0f)); |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 7277 | |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 7278 | updateColorMatrixLocked(); |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 7279 | return NO_ERROR; |
| 7280 | } |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 7281 | case 1023: { // Set color mode. |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 7282 | mDisplayColorSetting = static_cast<DisplayColorSetting>(data.readInt32()); |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 7283 | |
| 7284 | if (int32_t colorMode; data.readInt32(&colorMode) == NO_ERROR) { |
| 7285 | mForceColorMode = static_cast<ui::ColorMode>(colorMode); |
Daniel Solomon | 7c7ede2 | 2019-07-11 16:35:40 -0700 | [diff] [blame] | 7286 | } |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 7287 | scheduleRepaint(); |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 7288 | return NO_ERROR; |
| 7289 | } |
Peiyong Lin | 4bac91c | 2018-10-25 09:48:33 -0700 | [diff] [blame] | 7290 | // Deprecate, use 1030 to check whether the device is color managed. |
| 7291 | case 1024: { |
| 7292 | return NAME_NOT_FOUND; |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 7293 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 7294 | // Deprecated, use perfetto to start/stop the layer tracing |
| 7295 | case 1025: { |
| 7296 | return NAME_NOT_FOUND; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 7297 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 7298 | // Deprecated, execute "adb shell perfetto --query" to see the ongoing tracing sessions |
| 7299 | case 1026: { |
| 7300 | return NAME_NOT_FOUND; |
Vishnu Nair | 87704c9 | 2018-01-08 15:32:57 -0800 | [diff] [blame] | 7301 | } |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 7302 | // Is a DisplayColorSetting supported? |
| 7303 | case 1027: { |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 7304 | const auto display = getDefaultDisplayDevice(); |
| 7305 | if (!display) { |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 7306 | return NAME_NOT_FOUND; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 7307 | } |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 7308 | |
| 7309 | DisplayColorSetting setting = static_cast<DisplayColorSetting>(data.readInt32()); |
| 7310 | switch (setting) { |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 7311 | case DisplayColorSetting::kManaged: |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 7312 | case DisplayColorSetting::kUnmanaged: |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 7313 | reply->writeBool(true); |
| 7314 | break; |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 7315 | case DisplayColorSetting::kEnhanced: |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 7316 | reply->writeBool(display->hasRenderIntent(RenderIntent::ENHANCE)); |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 7317 | break; |
| 7318 | default: // vendor display color setting |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 7319 | reply->writeBool( |
| 7320 | display->hasRenderIntent(static_cast<RenderIntent>(setting))); |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 7321 | break; |
| 7322 | } |
| 7323 | return NO_ERROR; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 7324 | } |
Alec Mouri | 9c58bcd | 2020-09-09 18:57:07 -0700 | [diff] [blame] | 7325 | case 1028: { // Unused. |
| 7326 | return NAME_NOT_FOUND; |
Steven Thomas | 97f1f4c | 2018-06-01 12:04:16 -0700 | [diff] [blame] | 7327 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 7328 | // Deprecated, use perfetto to set the active layer tracing buffer size |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 7329 | case 1029: { |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 7330 | return NAME_NOT_FOUND; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 7331 | } |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 7332 | // Is device color managed? |
| 7333 | case 1030: { |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 7334 | // ColorDisplayManager stil calls this |
| 7335 | reply->writeBool(true); |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 7336 | return NO_ERROR; |
| 7337 | } |
Peiyong Lin | 9d846a5 | 2018-11-05 13:18:20 -0800 | [diff] [blame] | 7338 | // Override default composition data space |
| 7339 | // adb shell service call SurfaceFlinger 1031 i32 1 DATASPACE_NUMBER DATASPACE_NUMBER \ |
| 7340 | // && adb shell stop zygote && adb shell start zygote |
| 7341 | // to restore: adb shell service call SurfaceFlinger 1031 i32 0 && \ |
| 7342 | // adb shell stop zygote && adb shell start zygote |
| 7343 | case 1031: { |
| 7344 | Mutex::Autolock _l(mStateLock); |
| 7345 | n = data.readInt32(); |
| 7346 | if (n) { |
| 7347 | n = data.readInt32(); |
| 7348 | if (n) { |
| 7349 | Dataspace dataspace = static_cast<Dataspace>(n); |
| 7350 | if (!validateCompositionDataspace(dataspace)) { |
| 7351 | return BAD_VALUE; |
| 7352 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 7353 | mDefaultCompositionDataspace = dataspace; |
Peiyong Lin | 9d846a5 | 2018-11-05 13:18:20 -0800 | [diff] [blame] | 7354 | } |
| 7355 | n = data.readInt32(); |
| 7356 | if (n) { |
| 7357 | Dataspace dataspace = static_cast<Dataspace>(n); |
| 7358 | if (!validateCompositionDataspace(dataspace)) { |
| 7359 | return BAD_VALUE; |
| 7360 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 7361 | mWideColorGamutCompositionDataspace = dataspace; |
Peiyong Lin | 9d846a5 | 2018-11-05 13:18:20 -0800 | [diff] [blame] | 7362 | } |
| 7363 | } else { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 7364 | // restore composition data space. |
| 7365 | mDefaultCompositionDataspace = defaultCompositionDataspace; |
| 7366 | mWideColorGamutCompositionDataspace = wideColorGamutCompositionDataspace; |
Peiyong Lin | 9d846a5 | 2018-11-05 13:18:20 -0800 | [diff] [blame] | 7367 | } |
| 7368 | return NO_ERROR; |
| 7369 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 7370 | // Deprecated, use perfetto to set layer trace flags |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 7371 | case 1033: { |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 7372 | return NAME_NOT_FOUND; |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 7373 | } |
Ady Abraham | 03b02dd | 2019-03-21 15:40:11 -0700 | [diff] [blame] | 7374 | case 1034: { |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 7375 | n = data.readInt32(); |
| 7376 | if (n == 0 || n == 1) { |
| 7377 | sfdo_enableRefreshRateOverlay(static_cast<bool>(n)); |
| 7378 | } else { |
| 7379 | Mutex::Autolock lock(mStateLock); |
| 7380 | reply->writeBool(isRefreshRateOverlayEnabled()); |
| 7381 | } |
Ady Abraham | 03b02dd | 2019-03-21 15:40:11 -0700 | [diff] [blame] | 7382 | return NO_ERROR; |
| 7383 | } |
Ady Abraham | 34392f7 | 2019-04-10 11:29:27 -0700 | [diff] [blame] | 7384 | case 1035: { |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 7385 | // Parameters: |
| 7386 | // - (required) i32 mode id. |
| 7387 | // - (optional) i64 display id. Using default display if not provided. |
| 7388 | // - (optional) f min render rate. Using mode's fps is not provided. |
| 7389 | // - (optional) f max render rate. Using mode's fps is not provided. |
| 7390 | |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 7391 | const int modeId = data.readInt32(); |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 7392 | |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 7393 | const auto display = [&]() -> sp<IBinder> { |
| 7394 | uint64_t value; |
| 7395 | if (data.readUint64(&value) != NO_ERROR) { |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 7396 | return getDefaultDisplayDevice()->getDisplayToken().promote(); |
Ady Abraham | fb85366 | 2021-04-09 11:46:40 -0700 | [diff] [blame] | 7397 | } |
| 7398 | |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 7399 | if (const auto id = DisplayId::fromValue<PhysicalDisplayId>(value)) { |
| 7400 | return getPhysicalDisplayToken(*id); |
| 7401 | } |
| 7402 | |
| 7403 | ALOGE("Invalid physical display ID"); |
| 7404 | return nullptr; |
Ady Abraham | fb85366 | 2021-04-09 11:46:40 -0700 | [diff] [blame] | 7405 | }(); |
| 7406 | |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 7407 | const auto getFps = [&] { |
| 7408 | float value; |
| 7409 | if (data.readFloat(&value) == NO_ERROR) { |
| 7410 | return Fps::fromValue(value); |
| 7411 | } |
| 7412 | |
| 7413 | return Fps(); |
| 7414 | }; |
| 7415 | |
| 7416 | const auto minFps = getFps(); |
| 7417 | const auto maxFps = getFps(); |
| 7418 | |
Shiyong Li | 2bcece9 | 2021-08-30 20:38:50 +0000 | [diff] [blame] | 7419 | mDebugDisplayModeSetByBackdoor = false; |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 7420 | const status_t result = |
| 7421 | setActiveModeFromBackdoor(display, DisplayModeId{modeId}, minFps, maxFps); |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 7422 | mDebugDisplayModeSetByBackdoor = result == NO_ERROR; |
| 7423 | return result; |
Ady Abraham | 34392f7 | 2019-04-10 11:29:27 -0700 | [diff] [blame] | 7424 | } |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 7425 | // Turn on/off frame rate flexibility mode. When turned on it overrides the display |
| 7426 | // manager frame rate policy a new policy which allows switching between all refresh |
| 7427 | // rates. |
Ady Abraham | 07e5470 | 2020-04-16 15:05:37 -0700 | [diff] [blame] | 7428 | case 1036: { |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 7429 | if (data.readInt32() > 0) { // turn on |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7430 | return mScheduler |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 7431 | ->schedule([this]() FTL_FAKE_GUARD(kMainThreadContext) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7432 | const auto display = |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 7433 | FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked()); |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 7434 | |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7435 | // This is a little racy, but not in a way that hurts anything. As |
| 7436 | // we grab the defaultMode from the display manager policy, we could |
| 7437 | // be setting a new display manager policy, leaving us using a stale |
| 7438 | // defaultMode. The defaultMode doesn't matter for the override |
| 7439 | // policy though, since we set allowGroupSwitching to true, so it's |
| 7440 | // not a problem. |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7441 | scheduler::RefreshRateSelector::OverridePolicy overridePolicy; |
| 7442 | overridePolicy.defaultMode = display->refreshRateSelector() |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7443 | .getDisplayManagerPolicy() |
| 7444 | .defaultMode; |
| 7445 | overridePolicy.allowGroupSwitching = true; |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 7446 | return setDesiredDisplayModeSpecsInternal(display, overridePolicy); |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7447 | }) |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 7448 | .get(); |
| 7449 | } else { // turn off |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7450 | return mScheduler |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 7451 | ->schedule([this]() FTL_FAKE_GUARD(kMainThreadContext) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7452 | const auto display = |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 7453 | FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked()); |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 7454 | return setDesiredDisplayModeSpecsInternal( |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7455 | display, |
| 7456 | scheduler::RefreshRateSelector::NoOverridePolicy{}); |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7457 | }) |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 7458 | .get(); |
Ady Abraham | 07e5470 | 2020-04-16 15:05:37 -0700 | [diff] [blame] | 7459 | } |
Ady Abraham | 07e5470 | 2020-04-16 15:05:37 -0700 | [diff] [blame] | 7460 | } |
Marin Shalamanov | e6332b0 | 2020-09-03 11:30:37 +0200 | [diff] [blame] | 7461 | // Inject a hotplug connected event for the primary display. This will deallocate and |
| 7462 | // reallocate the display state including framebuffers. |
| 7463 | case 1037: { |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 7464 | const hal::HWDisplayId hwcId = |
| 7465 | (Mutex::Autolock(mStateLock), getHwComposer().getPrimaryHwcDisplayId()); |
| 7466 | |
Huihong Luo | 791bef9 | 2023-09-19 21:18:45 -0700 | [diff] [blame] | 7467 | onComposerHalHotplugEvent(hwcId, DisplayHotplugEvent::CONNECTED); |
Marin Shalamanov | e6332b0 | 2020-09-03 11:30:37 +0200 | [diff] [blame] | 7468 | return NO_ERROR; |
| 7469 | } |
Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 7470 | // Modify the max number of display frames stored within FrameTimeline |
| 7471 | case 1038: { |
| 7472 | n = data.readInt32(); |
| 7473 | if (n < 0 || n > MAX_ALLOWED_DISPLAY_FRAMES) { |
| 7474 | ALOGW("Invalid max size. Maximum allowed is %d", MAX_ALLOWED_DISPLAY_FRAMES); |
| 7475 | return BAD_VALUE; |
| 7476 | } |
| 7477 | if (n == 0) { |
| 7478 | // restore to default |
| 7479 | mFrameTimeline->reset(); |
| 7480 | return NO_ERROR; |
| 7481 | } |
| 7482 | mFrameTimeline->setMaxDisplayFrames(n); |
| 7483 | return NO_ERROR; |
| 7484 | } |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 7485 | case 1039: { |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 7486 | PhysicalDisplayId displayId = [&]() { |
| 7487 | Mutex::Autolock lock(mStateLock); |
| 7488 | return getDefaultDisplayDeviceLocked()->getPhysicalId(); |
| 7489 | }(); |
| 7490 | |
| 7491 | auto inUid = static_cast<uid_t>(data.readInt32()); |
| 7492 | const auto refreshRate = data.readFloat(); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 7493 | mScheduler->setPreferredRefreshRateForUid(FrameRateOverride{inUid, refreshRate}); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 7494 | mScheduler->onFrameRateOverridesChanged(scheduler::Cycle::Render, displayId); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 7495 | return NO_ERROR; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 7496 | } |
Alec Mouri | dd8bf2d | 2021-05-08 16:36:33 -0700 | [diff] [blame] | 7497 | // Toggle caching feature |
| 7498 | // First argument is an int32 - nonzero enables caching and zero disables caching |
| 7499 | // Second argument is an optional uint64 - if present, then limits enabling/disabling |
| 7500 | // caching to a particular physical display |
| 7501 | case 1040: { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7502 | auto future = mScheduler->schedule([&] { |
| 7503 | n = data.readInt32(); |
| 7504 | std::optional<PhysicalDisplayId> inputId = std::nullopt; |
| 7505 | if (uint64_t inputDisplayId; data.readUint64(&inputDisplayId) == NO_ERROR) { |
| 7506 | inputId = DisplayId::fromValue<PhysicalDisplayId>(inputDisplayId); |
| 7507 | if (!inputId || getPhysicalDisplayToken(*inputId)) { |
| 7508 | ALOGE("No display with id: %" PRIu64, inputDisplayId); |
| 7509 | return NAME_NOT_FOUND; |
| 7510 | } |
| 7511 | } |
| 7512 | { |
| 7513 | Mutex::Autolock lock(mStateLock); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 7514 | mLayerCachingEnabled = n != 0; |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7515 | for (const auto& [_, display] : mDisplays) { |
| 7516 | if (!inputId || *inputId == display->getPhysicalId()) { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 7517 | display->enableLayerCaching(mLayerCachingEnabled); |
Alec Mouri | cdd722b | 2021-05-19 14:29:05 -0700 | [diff] [blame] | 7518 | } |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7519 | } |
| 7520 | } |
| 7521 | return OK; |
| 7522 | }); |
Alec Mouri | cdd722b | 2021-05-19 14:29:05 -0700 | [diff] [blame] | 7523 | |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 7524 | if (const status_t error = future.get(); error != OK) { |
Alec Mouri | cdd722b | 2021-05-19 14:29:05 -0700 | [diff] [blame] | 7525 | return error; |
Alec Mouri | dd8bf2d | 2021-05-08 16:36:33 -0700 | [diff] [blame] | 7526 | } |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 7527 | scheduleRepaint(); |
Alec Mouri | dd8bf2d | 2021-05-08 16:36:33 -0700 | [diff] [blame] | 7528 | return NO_ERROR; |
| 7529 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 7530 | case 1041: { // Transaction tracing |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 7531 | if (mTransactionTracing) { |
Vishnu Nair | 640261c | 2023-09-21 21:22:51 -0700 | [diff] [blame] | 7532 | int arg = data.readInt32(); |
| 7533 | if (arg == -1) { |
Vishnu Nair | 50858e6 | 2023-10-13 23:28:24 +0000 | [diff] [blame] | 7534 | mScheduler->schedule([&]() { mTransactionTracing.reset(); }).get(); |
Vishnu Nair | 640261c | 2023-09-21 21:22:51 -0700 | [diff] [blame] | 7535 | } else if (arg > 0) { |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 7536 | // Transaction tracing is always running but allow the user to temporarily |
| 7537 | // increase the buffer when actively debugging. |
| 7538 | mTransactionTracing->setBufferSize( |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 7539 | TransactionTracing::LEGACY_ACTIVE_TRACING_BUFFER_SIZE); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 7540 | } else { |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 7541 | TransactionTraceWriter::getInstance().invoke("", /* overwrite= */ true); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 7542 | mTransactionTracing->setBufferSize( |
| 7543 | TransactionTracing::CONTINUOUS_TRACING_BUFFER_SIZE); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 7544 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 7545 | } |
| 7546 | reply->writeInt32(NO_ERROR); |
| 7547 | return NO_ERROR; |
| 7548 | } |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 7549 | case 1042: { // Write transaction trace to file |
Nataniel Borges | 533c2f8 | 2022-01-21 12:07:02 +0100 | [diff] [blame] | 7550 | if (mTransactionTracing) { |
| 7551 | mTransactionTracing->writeToFile(); |
| 7552 | } |
Nataniel Borges | 533c2f8 | 2022-01-21 12:07:02 +0100 | [diff] [blame] | 7553 | reply->writeInt32(NO_ERROR); |
| 7554 | return NO_ERROR; |
| 7555 | } |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 7556 | // hdr sdr ratio overlay |
| 7557 | case 1043: { |
| 7558 | auto future = mScheduler->schedule( |
| 7559 | [&]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD(kMainThreadContext) { |
| 7560 | n = data.readInt32(); |
Sally Qi | 0cd9e86 | 2024-05-15 18:37:02 -0700 | [diff] [blame] | 7561 | if (n == 0 || n == 1) { |
| 7562 | mHdrSdrRatioOverlay = n != 0; |
| 7563 | enableHdrSdrRatioOverlay(mHdrSdrRatioOverlay); |
| 7564 | } else { |
| 7565 | reply->writeBool(isHdrSdrRatioOverlayEnabled()); |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 7566 | } |
| 7567 | }); |
| 7568 | future.wait(); |
| 7569 | return NO_ERROR; |
| 7570 | } |
Brian Johnson | 51d8d5d | 2023-07-28 10:18:47 -0700 | [diff] [blame] | 7571 | |
| 7572 | case 1044: { // Enable/Disable mirroring from one display to another |
| 7573 | /* |
| 7574 | * Mirror one display onto another. |
| 7575 | * Ensure the source and destination displays are on. |
| 7576 | * Commands: |
| 7577 | * 0: Mirror one display to another |
| 7578 | * 1: Disable mirroring to a previously mirrored display |
| 7579 | * 2: Disable mirroring on previously mirrored displays |
| 7580 | * |
| 7581 | * Ex: |
| 7582 | * Get the display ids: |
| 7583 | * adb shell dumpsys SurfaceFlinger --display-id |
| 7584 | * Mirror first display to the second: |
| 7585 | * adb shell service call SurfaceFlinger 1044 i64 0 i64 4619827677550801152 i64 |
| 7586 | * 4619827677550801153 |
| 7587 | * Stop mirroring: |
| 7588 | * adb shell service call SurfaceFlinger 1044 i64 1 |
| 7589 | */ |
| 7590 | |
| 7591 | int64_t arg0 = data.readInt64(); |
| 7592 | |
| 7593 | switch (arg0) { |
| 7594 | case 0: { |
| 7595 | // Mirror arg1 to arg2 |
| 7596 | int64_t arg1 = data.readInt64(); |
| 7597 | int64_t arg2 = data.readInt64(); |
| 7598 | // Enable mirroring for one display |
| 7599 | const auto display1id = DisplayId::fromValue(arg1); |
| 7600 | auto mirrorRoot = SurfaceComposerClient::getDefault()->mirrorDisplay( |
| 7601 | display1id.value()); |
| 7602 | auto id2 = DisplayId::fromValue<PhysicalDisplayId>(arg2); |
| 7603 | const auto token2 = getPhysicalDisplayToken(*id2); |
| 7604 | ui::LayerStack layerStack; |
| 7605 | { |
| 7606 | Mutex::Autolock lock(mStateLock); |
| 7607 | sp<DisplayDevice> display = getDisplayDeviceLocked(token2); |
| 7608 | layerStack = display->getLayerStack(); |
| 7609 | } |
| 7610 | SurfaceComposerClient::Transaction t; |
| 7611 | t.setDisplayLayerStack(token2, layerStack); |
| 7612 | t.setLayer(mirrorRoot, INT_MAX); // Top-most layer |
| 7613 | t.setLayerStack(mirrorRoot, layerStack); |
| 7614 | t.apply(); |
| 7615 | |
| 7616 | mMirrorMapForDebug.emplace_or_replace(arg2, mirrorRoot); |
| 7617 | break; |
| 7618 | } |
| 7619 | |
| 7620 | case 1: { |
| 7621 | // Disable mirroring for arg1 |
| 7622 | int64_t arg1 = data.readInt64(); |
| 7623 | mMirrorMapForDebug.erase(arg1); |
| 7624 | break; |
| 7625 | } |
| 7626 | |
| 7627 | case 2: { |
| 7628 | // Disable mirroring for all displays |
| 7629 | mMirrorMapForDebug.clear(); |
| 7630 | break; |
| 7631 | } |
| 7632 | |
| 7633 | default: |
| 7634 | return BAD_VALUE; |
| 7635 | } |
| 7636 | return NO_ERROR; |
| 7637 | } |
Alec Mouri | 1c7938e | 2023-09-22 04:17:23 +0000 | [diff] [blame] | 7638 | // Inject jank |
| 7639 | // First argument is a float that describes the fraction of frame duration to jank by. |
| 7640 | // Second argument is a delay in ms for triggering the jank. This is useful for working |
| 7641 | // with tools that steal the adb connection. This argument is optional. |
| 7642 | case 1045: { |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 7643 | if (FlagManager::getInstance().vrr_config()) { |
Alec Mouri | 1c7938e | 2023-09-22 04:17:23 +0000 | [diff] [blame] | 7644 | float jankAmount = data.readFloat(); |
| 7645 | int32_t jankDelayMs = 0; |
| 7646 | if (data.readInt32(&jankDelayMs) != NO_ERROR) { |
| 7647 | jankDelayMs = 0; |
| 7648 | } |
| 7649 | |
| 7650 | const auto jankDelayDuration = Duration(std::chrono::milliseconds(jankDelayMs)); |
| 7651 | |
| 7652 | const bool jankAmountValid = jankAmount > 0.0 && jankAmount < 100.0; |
| 7653 | |
| 7654 | if (!jankAmountValid) { |
| 7655 | ALOGD("Ignoring invalid jank amount: %f", jankAmount); |
| 7656 | reply->writeInt32(BAD_VALUE); |
| 7657 | return BAD_VALUE; |
| 7658 | } |
| 7659 | |
| 7660 | (void)mScheduler->scheduleDelayed( |
| 7661 | [&, jankAmount]() FTL_FAKE_GUARD(kMainThreadContext) { |
| 7662 | mScheduler->injectPacesetterDelay(jankAmount); |
| 7663 | scheduleComposite(FrameHint::kActive); |
| 7664 | }, |
| 7665 | jankDelayDuration.ns()); |
| 7666 | reply->writeInt32(NO_ERROR); |
| 7667 | return NO_ERROR; |
| 7668 | } |
| 7669 | return err; |
| 7670 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7671 | } |
| 7672 | } |
| 7673 | return err; |
| 7674 | } |
| 7675 | |
Ady Abraham | a09852a | 2020-02-20 14:23:42 -0800 | [diff] [blame] | 7676 | void SurfaceFlinger::kernelTimerChanged(bool expired) { |
| 7677 | static bool updateOverlay = |
| 7678 | property_get_bool("debug.sf.kernel_idle_timer_update_overlay", true); |
Dominik Laskowski | 2013464 | 2020-04-20 22:36:44 -0700 | [diff] [blame] | 7679 | if (!updateOverlay) return; |
Ady Abraham | a09852a | 2020-02-20 14:23:42 -0800 | [diff] [blame] | 7680 | |
| 7681 | // Update the overlay on the main thread to avoid race conditions with |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 7682 | // RefreshRateSelector::getActiveMode |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 7683 | static_cast<void>(mScheduler->schedule([=, this] { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 7684 | const auto display = FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked()); |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7685 | if (!display) { |
| 7686 | ALOGW("%s: default display is null", __func__); |
| 7687 | return; |
| 7688 | } |
Adrian Salido | a942af8 | 2022-02-17 19:22:31 -0800 | [diff] [blame] | 7689 | if (!display->isRefreshRateOverlayEnabled()) return; |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 7690 | |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 7691 | const auto desiredModeIdOpt = |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 7692 | mDisplayModeController.getDesiredMode(display->getPhysicalId()) |
| 7693 | .transform([](const display::DisplayModeRequest& request) { |
| 7694 | return request.mode.modePtr->getId(); |
| 7695 | }); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 7696 | |
| 7697 | const bool timerExpired = mKernelIdleTimerEnabled && expired; |
| 7698 | |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 7699 | if (display->onKernelTimerChanged(desiredModeIdOpt, timerExpired)) { |
Dominik Laskowski | 46f3e3b | 2021-08-10 11:44:24 -0700 | [diff] [blame] | 7700 | mScheduler->scheduleFrame(); |
Ady Abraham | a09852a | 2020-02-20 14:23:42 -0800 | [diff] [blame] | 7701 | } |
| 7702 | })); |
| 7703 | } |
| 7704 | |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 7705 | std::pair<std::optional<KernelIdleTimerController>, std::chrono::milliseconds> |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 7706 | SurfaceFlinger::getKernelIdleTimerProperties(PhysicalDisplayId displayId) { |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 7707 | const bool isKernelIdleTimerHwcSupported = getHwComposer().getComposer()->isSupported( |
| 7708 | android::Hwc2::Composer::OptionalFeature::KernelIdleTimer); |
| 7709 | const auto timeout = getIdleTimerTimeout(displayId); |
| 7710 | if (isKernelIdleTimerHwcSupported) { |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 7711 | if (getHwComposer().hasDisplayIdleTimerCapability(displayId)) { |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 7712 | // In order to decide if we can use the HWC api for idle timer |
| 7713 | // we query DisplayCapability::DISPLAY_IDLE_TIMER directly on the composer |
| 7714 | // without relying on hasDisplayCapability. |
| 7715 | // hasDisplayCapability relies on DisplayCapabilities |
| 7716 | // which are updated after we set the PowerMode::ON. |
| 7717 | // DISPLAY_IDLE_TIMER is a display driver property |
| 7718 | // and is available before the PowerMode::ON |
| 7719 | return {KernelIdleTimerController::HwcApi, timeout}; |
| 7720 | } |
| 7721 | return {std::nullopt, timeout}; |
| 7722 | } |
| 7723 | if (getKernelIdleTimerSyspropConfig(displayId)) { |
| 7724 | return {KernelIdleTimerController::Sysprop, timeout}; |
| 7725 | } |
| 7726 | |
| 7727 | return {std::nullopt, timeout}; |
| 7728 | } |
| 7729 | |
| 7730 | void SurfaceFlinger::updateKernelIdleTimer(std::chrono::milliseconds timeout, |
| 7731 | KernelIdleTimerController controller, |
| 7732 | PhysicalDisplayId displayId) { |
| 7733 | switch (controller) { |
| 7734 | case KernelIdleTimerController::HwcApi: { |
| 7735 | getHwComposer().setIdleTimerEnabled(displayId, timeout); |
| 7736 | break; |
| 7737 | } |
| 7738 | case KernelIdleTimerController::Sysprop: { |
| 7739 | base::SetProperty(KERNEL_IDLE_TIMER_PROP, timeout > 0ms ? "true" : "false"); |
| 7740 | break; |
| 7741 | } |
| 7742 | } |
| 7743 | } |
| 7744 | |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 7745 | void SurfaceFlinger::toggleKernelIdleTimer() { |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7746 | using KernelIdleTimerAction = scheduler::RefreshRateSelector::KernelIdleTimerAction; |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 7747 | |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7748 | const auto display = getDefaultDisplayDeviceLocked(); |
| 7749 | if (!display) { |
| 7750 | ALOGW("%s: default display is null", __func__); |
| 7751 | return; |
| 7752 | } |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 7753 | |
Ady Abraham | e9316b7 | 2021-09-24 17:04:36 -0700 | [diff] [blame] | 7754 | // If the support for kernel idle timer is disabled for the active display, |
| 7755 | // don't do anything. |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 7756 | const std::optional<KernelIdleTimerController> kernelIdleTimerController = |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7757 | display->refreshRateSelector().kernelIdleTimerController(); |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 7758 | if (!kernelIdleTimerController.has_value()) { |
Ady Abraham | e9316b7 | 2021-09-24 17:04:36 -0700 | [diff] [blame] | 7759 | return; |
| 7760 | } |
| 7761 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7762 | const KernelIdleTimerAction action = display->refreshRateSelector().getIdleTimerAction(); |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 7763 | |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 7764 | switch (action) { |
| 7765 | case KernelIdleTimerAction::TurnOff: |
| 7766 | if (mKernelIdleTimerEnabled) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 7767 | SFTRACE_INT("KernelIdleTimer", 0); |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 7768 | std::chrono::milliseconds constexpr kTimerDisabledTimeout = 0ms; |
| 7769 | updateKernelIdleTimer(kTimerDisabledTimeout, kernelIdleTimerController.value(), |
| 7770 | display->getPhysicalId()); |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 7771 | mKernelIdleTimerEnabled = false; |
| 7772 | } |
| 7773 | break; |
| 7774 | case KernelIdleTimerAction::TurnOn: |
| 7775 | if (!mKernelIdleTimerEnabled) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 7776 | SFTRACE_INT("KernelIdleTimer", 1); |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 7777 | const std::chrono::milliseconds timeout = |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7778 | display->refreshRateSelector().getIdleTimerTimeout(); |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 7779 | updateKernelIdleTimer(timeout, kernelIdleTimerController.value(), |
| 7780 | display->getPhysicalId()); |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 7781 | mKernelIdleTimerEnabled = true; |
| 7782 | } |
| 7783 | break; |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 7784 | } |
| 7785 | } |
| 7786 | |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 7787 | // A simple RAII class to disconnect from an ANativeWindow* when it goes out of scope |
| 7788 | class WindowDisconnector { |
Mathias Agopian | 2ed0fe5 | 2013-07-08 17:09:41 -0700 | [diff] [blame] | 7789 | public: |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 7790 | WindowDisconnector(ANativeWindow* window, int api) : mWindow(window), mApi(api) {} |
| 7791 | ~WindowDisconnector() { |
| 7792 | native_window_api_disconnect(mWindow, mApi); |
Mathias Agopian | 2ed0fe5 | 2013-07-08 17:09:41 -0700 | [diff] [blame] | 7793 | } |
| 7794 | |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 7795 | private: |
| 7796 | ANativeWindow* mWindow; |
| 7797 | const int mApi; |
Mathias Agopian | 2ed0fe5 | 2013-07-08 17:09:41 -0700 | [diff] [blame] | 7798 | }; |
| 7799 | |
chaviw | f5bb97b | 2021-04-28 15:35:37 -0500 | [diff] [blame] | 7800 | static bool hasCaptureBlackoutContentPermission() { |
| 7801 | IPCThreadState* ipc = IPCThreadState::self(); |
| 7802 | const int pid = ipc->getCallingPid(); |
| 7803 | const int uid = ipc->getCallingUid(); |
| 7804 | return uid == AID_GRAPHICS || uid == AID_SYSTEM || |
| 7805 | PermissionCache::checkPermission(sCaptureBlackoutContent, pid, uid); |
| 7806 | } |
| 7807 | |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 7808 | static status_t validateScreenshotPermissions(const CaptureArgs& captureArgs) { |
| 7809 | IPCThreadState* ipc = IPCThreadState::self(); |
| 7810 | const int pid = ipc->getCallingPid(); |
| 7811 | const int uid = ipc->getCallingUid(); |
| 7812 | if (uid == AID_GRAPHICS || PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) { |
| 7813 | return OK; |
| 7814 | } |
| 7815 | |
| 7816 | // If the caller doesn't have the correct permissions but is only attempting to screenshot |
| 7817 | // itself, we allow it to continue. |
| 7818 | if (captureArgs.uid == uid) { |
| 7819 | return OK; |
| 7820 | } |
| 7821 | |
| 7822 | ALOGE("Permission Denial: can't take screenshot pid=%d, uid=%d", pid, uid); |
| 7823 | return PERMISSION_DENIED; |
| 7824 | } |
| 7825 | |
Peiyong Lin | 05cc011 | 2020-10-14 16:16:37 -0700 | [diff] [blame] | 7826 | status_t SurfaceFlinger::setSchedFifo(bool enabled) { |
| 7827 | static constexpr int kFifoPriority = 2; |
| 7828 | static constexpr int kOtherPriority = 0; |
| 7829 | |
| 7830 | struct sched_param param = {0}; |
| 7831 | int sched_policy; |
| 7832 | if (enabled) { |
| 7833 | sched_policy = SCHED_FIFO; |
| 7834 | param.sched_priority = kFifoPriority; |
| 7835 | } else { |
| 7836 | sched_policy = SCHED_OTHER; |
| 7837 | param.sched_priority = kOtherPriority; |
| 7838 | } |
| 7839 | |
| 7840 | if (sched_setscheduler(0, sched_policy, ¶m) != 0) { |
| 7841 | return -errno; |
| 7842 | } |
Wei Wang | 23aa5a6 | 2021-06-04 15:56:57 -0700 | [diff] [blame] | 7843 | |
| 7844 | return NO_ERROR; |
| 7845 | } |
| 7846 | |
| 7847 | status_t SurfaceFlinger::setSchedAttr(bool enabled) { |
| 7848 | static const unsigned int kUclampMin = |
Dominik Laskowski | dd374e1 | 2024-02-21 10:28:15 -0500 | [diff] [blame] | 7849 | base::GetUintProperty<unsigned int>("ro.surface_flinger.uclamp.min"s, 0U); |
Wei Wang | 23aa5a6 | 2021-06-04 15:56:57 -0700 | [diff] [blame] | 7850 | |
| 7851 | if (!kUclampMin) { |
| 7852 | // uclamp.min set to 0 (default), skip setting |
| 7853 | return NO_ERROR; |
| 7854 | } |
| 7855 | |
Wei Wang | 23aa5a6 | 2021-06-04 15:56:57 -0700 | [diff] [blame] | 7856 | sched_attr attr = {}; |
| 7857 | attr.size = sizeof(attr); |
| 7858 | |
| 7859 | attr.sched_flags = (SCHED_FLAG_KEEP_ALL | SCHED_FLAG_UTIL_CLAMP); |
| 7860 | attr.sched_util_min = enabled ? kUclampMin : 0; |
| 7861 | attr.sched_util_max = 1024; |
| 7862 | |
| 7863 | if (syscall(__NR_sched_setattr, 0, &attr, 0)) { |
| 7864 | return -errno; |
| 7865 | } |
| 7866 | |
Peiyong Lin | 05cc011 | 2020-10-14 16:16:37 -0700 | [diff] [blame] | 7867 | return NO_ERROR; |
| 7868 | } |
| 7869 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7870 | namespace { |
| 7871 | |
Melody Hsu | 683fe04 | 2024-06-04 17:22:04 +0000 | [diff] [blame] | 7872 | ui::Dataspace pickBestDataspace(ui::Dataspace requestedDataspace, |
| 7873 | const compositionengine::impl::OutputCompositionState& state, |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7874 | bool capturingHdrLayers, bool hintForSeamlessTransition) { |
Melody Hsu | 683fe04 | 2024-06-04 17:22:04 +0000 | [diff] [blame] | 7875 | if (requestedDataspace != ui::Dataspace::UNKNOWN) { |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7876 | return requestedDataspace; |
| 7877 | } |
| 7878 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7879 | const auto dataspaceForColorMode = ui::pickDataspaceFor(state.colorMode); |
| 7880 | |
Alec Mouri | 2964072 | 2023-05-16 23:22:41 +0000 | [diff] [blame] | 7881 | // TODO: Enable once HDR screenshots are ready. |
| 7882 | if constexpr (/* DISABLES CODE */ (false)) { |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7883 | // For now since we only support 8-bit screenshots, just use HLG and |
| 7884 | // assume that 1.0 >= display max luminance. This isn't quite as future |
| 7885 | // proof as PQ is, but is good enough. |
| 7886 | // Consider using PQ once we support 16-bit screenshots and we're able |
| 7887 | // to consistently supply metadata to image encoders. |
| 7888 | return ui::Dataspace::BT2020_HLG; |
| 7889 | } |
| 7890 | |
| 7891 | return dataspaceForColorMode; |
| 7892 | } |
| 7893 | |
| 7894 | } // namespace |
| 7895 | |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7896 | static void invokeScreenCaptureError(const status_t status, |
| 7897 | const sp<IScreenCaptureListener>& captureListener) { |
| 7898 | ScreenCaptureResults captureResults; |
| 7899 | captureResults.fenceResult = base::unexpected(status); |
| 7900 | captureListener->onScreenCaptureCompleted(captureResults); |
| 7901 | } |
| 7902 | |
| 7903 | void SurfaceFlinger::captureDisplay(const DisplayCaptureArgs& args, |
| 7904 | const sp<IScreenCaptureListener>& captureListener) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 7905 | SFTRACE_CALL(); |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 7906 | |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 7907 | status_t validate = validateScreenshotPermissions(args); |
| 7908 | if (validate != OK) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7909 | ALOGD("Permission denied to captureDisplay"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7910 | invokeScreenCaptureError(validate, captureListener); |
| 7911 | return; |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 7912 | } |
| 7913 | |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7914 | if (!args.displayToken) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7915 | ALOGD("Invalid display token to captureDisplay"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7916 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 7917 | return; |
| 7918 | } |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 7919 | |
Chavi Weingarten | f5fb5b0 | 2023-11-30 17:27:52 +0000 | [diff] [blame] | 7920 | if (args.captureSecureLayers && !hasCaptureBlackoutContentPermission()) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7921 | ALOGD("Attempting to capture secure layers without CAPTURE_BLACKOUT_CONTENT"); |
Chavi Weingarten | f5fb5b0 | 2023-11-30 17:27:52 +0000 | [diff] [blame] | 7922 | invokeScreenCaptureError(PERMISSION_DENIED, captureListener); |
| 7923 | return; |
| 7924 | } |
| 7925 | |
Dominik Laskowski | 7b9bf4c | 2021-03-18 12:21:29 -0700 | [diff] [blame] | 7926 | wp<const DisplayDevice> displayWeak; |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 7927 | ui::LayerStack layerStack; |
| 7928 | ui::Size reqSize(args.width, args.height); |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 7929 | std::unordered_set<uint32_t> excludeLayerIds; |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 7930 | { |
| 7931 | Mutex::Autolock lock(mStateLock); |
| 7932 | sp<DisplayDevice> display = getDisplayDeviceLocked(args.displayToken); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7933 | if (!display) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7934 | ALOGD("Unable to find display device for captureDisplay"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7935 | invokeScreenCaptureError(NAME_NOT_FOUND, captureListener); |
| 7936 | return; |
| 7937 | } |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 7938 | displayWeak = display; |
| 7939 | layerStack = display->getLayerStack(); |
| 7940 | |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 7941 | // set the requested width/height to the logical display layer stack rect size by default |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 7942 | if (args.width == 0 || args.height == 0) { |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 7943 | reqSize = display->getLayerStackSpaceRect().getSize(); |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 7944 | } |
| 7945 | |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 7946 | for (const auto& handle : args.excludeHandles) { |
| 7947 | uint32_t excludeLayer = LayerHandle::getLayerId(handle); |
| 7948 | if (excludeLayer != UNASSIGNED_LAYER_ID) { |
| 7949 | excludeLayerIds.emplace(excludeLayer); |
| 7950 | } else { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7951 | ALOGD("Invalid layer handle passed as excludeLayer to captureDisplay"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7952 | invokeScreenCaptureError(NAME_NOT_FOUND, captureListener); |
| 7953 | return; |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 7954 | } |
| 7955 | } |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 7956 | } |
| 7957 | |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 7958 | GetLayerSnapshotsFunction getLayerSnapshotsFn = |
| 7959 | getLayerSnapshotsForScreenshots(layerStack, args.uid, std::move(excludeLayerIds)); |
chaviw | 0390077 | 2020-08-18 12:34:51 -0700 | [diff] [blame] | 7960 | |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 7961 | ftl::Flags<RenderArea::Options> options; |
| 7962 | if (args.captureSecureLayers) options |= RenderArea::Options::CAPTURE_SECURE_LAYERS; |
| 7963 | if (args.hintForSeamlessTransition) |
| 7964 | options |= RenderArea::Options::HINT_FOR_SEAMLESS_TRANSITION; |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7965 | captureScreenCommon(RenderAreaBuilderVariant(std::in_place_type<DisplayRenderAreaBuilder>, |
| 7966 | args.sourceCrop, reqSize, args.dataspace, |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 7967 | displayWeak, options), |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 7968 | getLayerSnapshotsFn, reqSize, args.pixelFormat, args.allowProtected, |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7969 | args.grayscale, captureListener); |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 7970 | } |
| 7971 | |
John Reck | 7e4c487 | 2023-11-14 18:31:03 -0500 | [diff] [blame] | 7972 | void SurfaceFlinger::captureDisplay(DisplayId displayId, const CaptureArgs& args, |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7973 | const sp<IScreenCaptureListener>& captureListener) { |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 7974 | ui::LayerStack layerStack; |
Dominik Laskowski | 7b9bf4c | 2021-03-18 12:21:29 -0700 | [diff] [blame] | 7975 | wp<const DisplayDevice> displayWeak; |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 7976 | ui::Size size; |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 7977 | { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 7978 | Mutex::Autolock lock(mStateLock); |
Dominik Laskowski | 7b9bf4c | 2021-03-18 12:21:29 -0700 | [diff] [blame] | 7979 | |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 7980 | const auto display = getDisplayDeviceLocked(displayId); |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 7981 | if (!display) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7982 | ALOGD("Unable to find display device for captureDisplay"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7983 | invokeScreenCaptureError(NAME_NOT_FOUND, captureListener); |
| 7984 | return; |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 7985 | } |
| 7986 | |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 7987 | displayWeak = display; |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 7988 | layerStack = display->getLayerStack(); |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 7989 | size = display->getLayerStackSpaceRect().getSize(); |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 7990 | } |
| 7991 | |
John Reck | 7e4c487 | 2023-11-14 18:31:03 -0500 | [diff] [blame] | 7992 | size.width *= args.frameScaleX; |
| 7993 | size.height *= args.frameScaleY; |
| 7994 | |
| 7995 | // We could query a real value for this but it'll be a long, long time until we support |
| 7996 | // displays that need upwards of 1GB per buffer so... |
| 7997 | constexpr auto kMaxTextureSize = 16384; |
| 7998 | if (size.width <= 0 || size.height <= 0 || size.width >= kMaxTextureSize || |
| 7999 | size.height >= kMaxTextureSize) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 8000 | ALOGD("captureDisplay resolved to invalid size %d x %d", size.width, size.height); |
John Reck | 7e4c487 | 2023-11-14 18:31:03 -0500 | [diff] [blame] | 8001 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 8002 | return; |
| 8003 | } |
| 8004 | |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 8005 | GetLayerSnapshotsFunction getLayerSnapshotsFn = |
| 8006 | getLayerSnapshotsForScreenshots(layerStack, CaptureArgs::UNSET_UID, |
| 8007 | /*snapshotFilterFn=*/nullptr); |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 8008 | |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 8009 | if (captureListener == nullptr) { |
| 8010 | ALOGE("capture screen must provide a capture listener callback"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8011 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 8012 | return; |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 8013 | } |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 8014 | |
| 8015 | constexpr bool kAllowProtected = false; |
| 8016 | constexpr bool kGrayscale = false; |
| 8017 | |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 8018 | ftl::Flags<RenderArea::Options> options; |
| 8019 | if (args.hintForSeamlessTransition) |
| 8020 | options |= RenderArea::Options::HINT_FOR_SEAMLESS_TRANSITION; |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8021 | captureScreenCommon(RenderAreaBuilderVariant(std::in_place_type<DisplayRenderAreaBuilder>, |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 8022 | Rect(), size, args.dataspace, displayWeak, |
| 8023 | options), |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 8024 | getLayerSnapshotsFn, size, args.pixelFormat, kAllowProtected, kGrayscale, |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8025 | captureListener); |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 8026 | } |
| 8027 | |
Chavi Weingarten | f6fb445 | 2024-01-23 21:10:30 +0000 | [diff] [blame] | 8028 | ScreenCaptureResults SurfaceFlinger::captureLayersSync(const LayerCaptureArgs& args) { |
| 8029 | sp<SyncScreenCaptureListener> captureListener = sp<SyncScreenCaptureListener>::make(); |
| 8030 | captureLayers(args, captureListener); |
| 8031 | return captureListener->waitForResults(); |
| 8032 | } |
| 8033 | |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8034 | void SurfaceFlinger::captureLayers(const LayerCaptureArgs& args, |
| 8035 | const sp<IScreenCaptureListener>& captureListener) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 8036 | SFTRACE_CALL(); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 8037 | |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 8038 | status_t validate = validateScreenshotPermissions(args); |
| 8039 | if (validate != OK) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 8040 | ALOGD("Permission denied to captureLayers"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8041 | invokeScreenCaptureError(validate, captureListener); |
| 8042 | return; |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 8043 | } |
| 8044 | |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 8045 | ui::Size reqSize; |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 8046 | sp<Layer> parent; |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 8047 | Rect crop(args.sourceCrop); |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 8048 | std::unordered_set<uint32_t> excludeLayerIds; |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 8049 | ui::Dataspace dataspace = args.dataspace; |
chaviw | f5bb97b | 2021-04-28 15:35:37 -0500 | [diff] [blame] | 8050 | |
Chavi Weingarten | f5fb5b0 | 2023-11-30 17:27:52 +0000 | [diff] [blame] | 8051 | if (args.captureSecureLayers && !hasCaptureBlackoutContentPermission()) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 8052 | ALOGD("Attempting to capture secure layers without CAPTURE_BLACKOUT_CONTENT"); |
Chavi Weingarten | f5fb5b0 | 2023-11-30 17:27:52 +0000 | [diff] [blame] | 8053 | invokeScreenCaptureError(PERMISSION_DENIED, captureListener); |
| 8054 | return; |
| 8055 | } |
chaviw | f5bb97b | 2021-04-28 15:35:37 -0500 | [diff] [blame] | 8056 | |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 8057 | { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 8058 | Mutex::Autolock lock(mStateLock); |
chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 8059 | |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 8060 | parent = LayerHandle::getLayer(args.layerHandle); |
chaviw | 79468ab | 2021-10-27 11:11:24 -0500 | [diff] [blame] | 8061 | if (parent == nullptr) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 8062 | ALOGD("captureLayers called with an invalid or removed parent"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8063 | invokeScreenCaptureError(NAME_NOT_FOUND, captureListener); |
| 8064 | return; |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 8065 | } |
| 8066 | |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 8067 | Rect parentSourceBounds = parent->getCroppedBufferSize(parent->getDrawingState()); |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 8068 | if (args.sourceCrop.width() <= 0) { |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 8069 | crop.left = 0; |
Vishnu Nair | efc42e2 | 2019-12-03 17:36:12 -0800 | [diff] [blame] | 8070 | crop.right = parentSourceBounds.getWidth(); |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 8071 | } |
| 8072 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 8073 | if (args.sourceCrop.height() <= 0) { |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 8074 | crop.top = 0; |
Vishnu Nair | efc42e2 | 2019-12-03 17:36:12 -0800 | [diff] [blame] | 8075 | crop.bottom = parentSourceBounds.getHeight(); |
| 8076 | } |
| 8077 | |
chaviw | 17ac24b | 2021-01-28 18:50:05 -0800 | [diff] [blame] | 8078 | if (crop.isEmpty() || args.frameScaleX <= 0.0f || args.frameScaleY <= 0.0f) { |
Vishnu Nair | efc42e2 | 2019-12-03 17:36:12 -0800 | [diff] [blame] | 8079 | // Error out if the layer has no source bounds (i.e. they are boundless) and a source |
| 8080 | // crop was not specified, or an invalid frame scale was provided. |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 8081 | ALOGD("Boundless layer, unspecified crop, or invalid frame scale to captureLayers"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8082 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 8083 | return; |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 8084 | } |
chaviw | 17ac24b | 2021-01-28 18:50:05 -0800 | [diff] [blame] | 8085 | reqSize = ui::Size(crop.width() * args.frameScaleX, crop.height() * args.frameScaleY); |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 8086 | |
chaviw | 3efadb1 | 2020-07-27 10:07:15 -0700 | [diff] [blame] | 8087 | for (const auto& handle : args.excludeHandles) { |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 8088 | uint32_t excludeLayer = LayerHandle::getLayerId(handle); |
| 8089 | if (excludeLayer != UNASSIGNED_LAYER_ID) { |
| 8090 | excludeLayerIds.emplace(excludeLayer); |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 8091 | } else { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 8092 | ALOGD("Invalid layer handle passed as excludeLayer to captureLayers"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8093 | invokeScreenCaptureError(NAME_NOT_FOUND, captureListener); |
| 8094 | return; |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 8095 | } |
| 8096 | } |
| 8097 | } // mStateLock |
chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 8098 | |
Chia-I Wu | 20261cb | 2018-08-23 12:55:44 -0700 | [diff] [blame] | 8099 | // really small crop or frameScale |
chaviw | 79468ab | 2021-10-27 11:11:24 -0500 | [diff] [blame] | 8100 | if (reqSize.width <= 0 || reqSize.height <= 0) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 8101 | ALOGD("Failed to captureLayers: crop or scale too small"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8102 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 8103 | return; |
Chia-I Wu | 20261cb | 2018-08-23 12:55:44 -0700 | [diff] [blame] | 8104 | } |
| 8105 | |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 8106 | std::optional<FloatRect> parentCrop = std::nullopt; |
| 8107 | if (args.childrenOnly) { |
| 8108 | parentCrop = crop.isEmpty() ? FloatRect(0, 0, reqSize.width, reqSize.height) |
| 8109 | : crop.toFloatRect(); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8110 | } |
Robert Carr | 108b2c7 | 2019-04-02 16:32:58 -0700 | [diff] [blame] | 8111 | |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 8112 | GetLayerSnapshotsFunction getLayerSnapshotsFn = |
| 8113 | getLayerSnapshotsForScreenshots(parent->sequence, args.uid, std::move(excludeLayerIds), |
| 8114 | args.childrenOnly, parentCrop); |
| 8115 | |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 8116 | if (captureListener == nullptr) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 8117 | ALOGD("capture screen must provide a capture listener callback"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8118 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 8119 | return; |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 8120 | } |
| 8121 | |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 8122 | ftl::Flags<RenderArea::Options> options; |
| 8123 | if (args.captureSecureLayers) options |= RenderArea::Options::CAPTURE_SECURE_LAYERS; |
| 8124 | if (args.hintForSeamlessTransition) |
| 8125 | options |= RenderArea::Options::HINT_FOR_SEAMLESS_TRANSITION; |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8126 | captureScreenCommon(RenderAreaBuilderVariant(std::in_place_type<LayerRenderAreaBuilder>, crop, |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 8127 | reqSize, dataspace, parent, args.childrenOnly, |
| 8128 | options), |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 8129 | getLayerSnapshotsFn, reqSize, args.pixelFormat, args.allowProtected, |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8130 | args.grayscale, captureListener); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 8131 | } |
| 8132 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 8133 | // Creates a Future release fence for a layer and keeps track of it in a list to |
| 8134 | // release the buffer when the Future is complete. Calls from composittion |
| 8135 | // involve needing to refresh the composition start time for stats. |
| 8136 | void SurfaceFlinger::attachReleaseFenceFutureToLayer(Layer* layer, LayerFE* layerFE, |
| 8137 | ui::LayerStack layerStack) { |
| 8138 | ftl::Future<FenceResult> futureFence = layerFE->createReleaseFenceFuture(); |
| 8139 | Layer* clonedFrom = layer->getClonedFrom().get(); |
| 8140 | auto owningLayer = clonedFrom ? clonedFrom : layer; |
| 8141 | owningLayer->prepareReleaseCallbacks(std::move(futureFence), layerStack); |
| 8142 | } |
| 8143 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8144 | // Loop over all visible layers to see whether there's any protected layer. A protected layer is |
| 8145 | // typically a layer with DRM contents, or have the GRALLOC_USAGE_PROTECTED set on the buffer. |
| 8146 | // A protected layer has no implication on whether it's secure, which is explicitly set by |
| 8147 | // application to avoid being screenshot or drawn via unsecure display. |
| 8148 | bool SurfaceFlinger::layersHasProtectedLayer(const std::vector<sp<LayerFE>>& layers) const { |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 8149 | bool protectedLayerFound = false; |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8150 | for (auto& layerFE : layers) { |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 8151 | protectedLayerFound |= |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8152 | (layerFE->mSnapshot->isVisible && layerFE->mSnapshot->hasProtectedContent); |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 8153 | if (protectedLayerFound) { |
| 8154 | break; |
| 8155 | } |
| 8156 | } |
| 8157 | return protectedLayerFound; |
| 8158 | } |
| 8159 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8160 | // Getting layer snapshots and display should take place on main thread. |
| 8161 | // Accessing display requires mStateLock, and contention for this lock |
| 8162 | // is reduced when grabbed from the main thread, thus also reducing |
| 8163 | // risk of deadlocks. |
Vishnu Nair | 5a93ccb | 2024-06-26 00:38:32 +0000 | [diff] [blame] | 8164 | std::optional<SurfaceFlinger::OutputCompositionState> SurfaceFlinger::getSnapshotsFromMainThread( |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8165 | RenderAreaBuilderVariant& renderAreaBuilder, GetLayerSnapshotsFunction getLayerSnapshotsFn, |
| 8166 | std::vector<sp<LayerFE>>& layerFEs) { |
| 8167 | return mScheduler |
| 8168 | ->schedule([=, this, &renderAreaBuilder, &layerFEs]() REQUIRES(kMainThreadContext) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 8169 | SFTRACE_NAME("getSnapshotsFromMainThread"); |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8170 | auto layers = getLayerSnapshotsFn(); |
| 8171 | for (auto& [layer, layerFE] : layers) { |
| 8172 | attachReleaseFenceFutureToLayer(layer, layerFE.get(), ui::INVALID_LAYER_STACK); |
| 8173 | } |
| 8174 | layerFEs = extractLayerFEs(layers); |
| 8175 | return getDisplayStateFromRenderAreaBuilder(renderAreaBuilder); |
| 8176 | }) |
| 8177 | .get(); |
| 8178 | } |
| 8179 | |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8180 | void SurfaceFlinger::captureScreenCommon(RenderAreaBuilderVariant renderAreaBuilder, |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 8181 | GetLayerSnapshotsFunction getLayerSnapshotsFn, |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8182 | ui::Size bufferSize, ui::PixelFormat reqPixelFormat, |
| 8183 | bool allowProtected, bool grayscale, |
| 8184 | const sp<IScreenCaptureListener>& captureListener) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 8185 | SFTRACE_CALL(); |
Mathias Agopian | 2a9fc49 | 2013-03-01 13:42:57 -0800 | [diff] [blame] | 8186 | |
Garfield Tan | 9c9c191 | 2021-07-19 12:02:16 -0700 | [diff] [blame] | 8187 | if (exceedsMaxRenderTargetSize(bufferSize.getWidth(), bufferSize.getHeight())) { |
| 8188 | ALOGE("Attempted to capture screen with size (%" PRId32 ", %" PRId32 |
| 8189 | ") that exceeds render target size limit.", |
| 8190 | bufferSize.getWidth(), bufferSize.getHeight()); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8191 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 8192 | return; |
Garfield Tan | 9c9c191 | 2021-07-19 12:02:16 -0700 | [diff] [blame] | 8193 | } |
| 8194 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8195 | if (FlagManager::getInstance().single_hop_screenshot() && |
| 8196 | FlagManager::getInstance().ce_fence_promise()) { |
| 8197 | std::vector<sp<LayerFE>> layerFEs; |
| 8198 | auto displayState = |
Vishnu Nair | 5a93ccb | 2024-06-26 00:38:32 +0000 | [diff] [blame] | 8199 | getSnapshotsFromMainThread(renderAreaBuilder, getLayerSnapshotsFn, layerFEs); |
Alec Mouri | 7013b6f | 2021-02-12 11:16:54 -0800 | [diff] [blame] | 8200 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8201 | const bool supportsProtected = getRenderEngine().supportsProtectedContent(); |
| 8202 | bool hasProtectedLayer = false; |
| 8203 | if (allowProtected && supportsProtected) { |
| 8204 | hasProtectedLayer = layersHasProtectedLayer(layerFEs); |
| 8205 | } |
| 8206 | const bool isProtected = hasProtectedLayer && allowProtected && supportsProtected; |
| 8207 | const uint32_t usage = GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER | |
| 8208 | GRALLOC_USAGE_HW_TEXTURE | |
| 8209 | (isProtected ? GRALLOC_USAGE_PROTECTED |
| 8210 | : GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 8211 | sp<GraphicBuffer> buffer = |
| 8212 | getFactory().createGraphicBuffer(bufferSize.getWidth(), bufferSize.getHeight(), |
| 8213 | static_cast<android_pixel_format>(reqPixelFormat), |
| 8214 | 1 /* layerCount */, usage, "screenshot"); |
| 8215 | |
| 8216 | const status_t bufferStatus = buffer->initCheck(); |
| 8217 | if (bufferStatus != OK) { |
| 8218 | // Animations may end up being really janky, but don't crash here. |
| 8219 | // Otherwise an irreponsible process may cause an SF crash by allocating |
| 8220 | // too much. |
| 8221 | ALOGE("%s: Buffer failed to allocate: %d", __func__, bufferStatus); |
| 8222 | invokeScreenCaptureError(bufferStatus, captureListener); |
| 8223 | return; |
| 8224 | } |
| 8225 | const std::shared_ptr<renderengine::ExternalTexture> texture = std::make_shared< |
| 8226 | renderengine::impl::ExternalTexture>(buffer, getRenderEngine(), |
| 8227 | renderengine::impl::ExternalTexture::Usage:: |
| 8228 | WRITEABLE); |
| 8229 | auto futureFence = |
| 8230 | captureScreenshot(renderAreaBuilder, texture, false /* regionSampling */, grayscale, |
| 8231 | isProtected, captureListener, displayState, layerFEs); |
| 8232 | futureFence.get(); |
| 8233 | |
| 8234 | } else { |
| 8235 | const bool supportsProtected = getRenderEngine().supportsProtectedContent(); |
| 8236 | bool hasProtectedLayer = false; |
| 8237 | if (allowProtected && supportsProtected) { |
| 8238 | auto layers = mScheduler->schedule([=]() { return getLayerSnapshotsFn(); }).get(); |
| 8239 | hasProtectedLayer = layersHasProtectedLayer(extractLayerFEs(layers)); |
| 8240 | } |
| 8241 | const bool isProtected = hasProtectedLayer && allowProtected && supportsProtected; |
| 8242 | const uint32_t usage = GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER | |
| 8243 | GRALLOC_USAGE_HW_TEXTURE | |
| 8244 | (isProtected ? GRALLOC_USAGE_PROTECTED |
| 8245 | : GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 8246 | sp<GraphicBuffer> buffer = |
| 8247 | getFactory().createGraphicBuffer(bufferSize.getWidth(), bufferSize.getHeight(), |
| 8248 | static_cast<android_pixel_format>(reqPixelFormat), |
| 8249 | 1 /* layerCount */, usage, "screenshot"); |
| 8250 | |
| 8251 | const status_t bufferStatus = buffer->initCheck(); |
| 8252 | if (bufferStatus != OK) { |
| 8253 | // Animations may end up being really janky, but don't crash here. |
| 8254 | // Otherwise an irreponsible process may cause an SF crash by allocating |
| 8255 | // too much. |
| 8256 | ALOGE("%s: Buffer failed to allocate: %d", __func__, bufferStatus); |
| 8257 | invokeScreenCaptureError(bufferStatus, captureListener); |
| 8258 | return; |
| 8259 | } |
| 8260 | const std::shared_ptr<renderengine::ExternalTexture> texture = std::make_shared< |
| 8261 | renderengine::impl::ExternalTexture>(buffer, getRenderEngine(), |
| 8262 | renderengine::impl::ExternalTexture::Usage:: |
| 8263 | WRITEABLE); |
| 8264 | auto futureFence = captureScreenshotLegacy(renderAreaBuilder, getLayerSnapshotsFn, texture, |
| 8265 | false /* regionSampling */, grayscale, |
| 8266 | isProtected, captureListener); |
| 8267 | futureFence.get(); |
Alec Mouri | d8f17c5 | 2022-07-21 18:43:10 +0000 | [diff] [blame] | 8268 | } |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 8269 | } |
| 8270 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8271 | std::optional<SurfaceFlinger::OutputCompositionState> |
| 8272 | SurfaceFlinger::getDisplayStateFromRenderAreaBuilder(RenderAreaBuilderVariant& renderAreaBuilder) { |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8273 | sp<const DisplayDevice> display = nullptr; |
| 8274 | { |
| 8275 | Mutex::Autolock lock(mStateLock); |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8276 | if (auto* layerRenderAreaBuilder = |
| 8277 | std::get_if<LayerRenderAreaBuilder>(&renderAreaBuilder)) { |
| 8278 | // LayerSnapshotBuilder should only be accessed from the main thread. |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8279 | const frontend::LayerSnapshot* snapshot = |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8280 | mLayerSnapshotBuilder.getSnapshot(layerRenderAreaBuilder->layer->getSequence()); |
| 8281 | if (!snapshot) { |
| 8282 | ALOGW("Couldn't find layer snapshot for %d", |
| 8283 | layerRenderAreaBuilder->layer->getSequence()); |
| 8284 | } else { |
Melody Hsu | 50c151a | 2024-05-14 06:11:12 +0000 | [diff] [blame] | 8285 | layerRenderAreaBuilder->setLayerSnapshot(*snapshot); |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8286 | display = findDisplay( |
| 8287 | [layerStack = snapshot->outputFilter.layerStack](const auto& display) { |
| 8288 | return display.getLayerStack() == layerStack; |
| 8289 | }); |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8290 | } |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8291 | } else if (auto* displayRenderAreaBuilder = |
| 8292 | std::get_if<DisplayRenderAreaBuilder>(&renderAreaBuilder)) { |
| 8293 | display = displayRenderAreaBuilder->displayWeak.promote(); |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8294 | } |
| 8295 | |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8296 | if (display == nullptr) { |
| 8297 | display = getDefaultDisplayDeviceLocked(); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 8298 | } |
| 8299 | |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8300 | if (display != nullptr) { |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8301 | return std::optional{display->getCompositionDisplay()->getState()}; |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8302 | } |
| 8303 | } |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8304 | return std::nullopt; |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8305 | } |
| 8306 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8307 | std::vector<sp<LayerFE>> SurfaceFlinger::extractLayerFEs( |
| 8308 | const std::vector<std::pair<Layer*, sp<LayerFE>>>& layers) const { |
| 8309 | std::vector<sp<LayerFE>> layerFEs; |
| 8310 | layerFEs.reserve(layers.size()); |
| 8311 | for (const auto& [_, layerFE] : layers) { |
| 8312 | layerFEs.push_back(layerFE); |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8313 | } |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8314 | return layerFEs; |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8315 | } |
| 8316 | |
| 8317 | ftl::SharedFuture<FenceResult> SurfaceFlinger::captureScreenshot( |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8318 | const RenderAreaBuilderVariant& renderAreaBuilder, |
| 8319 | const std::shared_ptr<renderengine::ExternalTexture>& buffer, bool regionSampling, |
| 8320 | bool grayscale, bool isProtected, const sp<IScreenCaptureListener>& captureListener, |
| 8321 | std::optional<OutputCompositionState>& displayState, std::vector<sp<LayerFE>>& layerFEs) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 8322 | SFTRACE_CALL(); |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8323 | |
| 8324 | ScreenCaptureResults captureResults; |
| 8325 | std::unique_ptr<const RenderArea> renderArea = |
| 8326 | std::visit([](auto&& arg) -> std::unique_ptr<RenderArea> { return arg.build(); }, |
| 8327 | renderAreaBuilder); |
| 8328 | |
| 8329 | if (!renderArea) { |
| 8330 | ALOGW("Skipping screen capture because of invalid render area."); |
| 8331 | if (captureListener) { |
| 8332 | captureResults.fenceResult = base::unexpected(NO_MEMORY); |
| 8333 | captureListener->onScreenCaptureCompleted(captureResults); |
| 8334 | } |
| 8335 | return ftl::yield<FenceResult>(base::unexpected(NO_ERROR)).share(); |
| 8336 | } |
| 8337 | |
| 8338 | // Empty vector needed to pass into renderScreenImpl for legacy path |
| 8339 | std::vector<std::pair<Layer*, sp<android::LayerFE>>> layers; |
| 8340 | ftl::SharedFuture<FenceResult> renderFuture = |
| 8341 | renderScreenImpl(std::move(renderArea), buffer, regionSampling, grayscale, isProtected, |
| 8342 | captureResults, displayState, layers, layerFEs); |
| 8343 | |
| 8344 | if (captureListener) { |
| 8345 | // Defer blocking on renderFuture back to the Binder thread. |
| 8346 | return ftl::Future(std::move(renderFuture)) |
| 8347 | .then([captureListener, captureResults = std::move(captureResults)]( |
| 8348 | FenceResult fenceResult) mutable -> FenceResult { |
| 8349 | captureResults.fenceResult = std::move(fenceResult); |
| 8350 | captureListener->onScreenCaptureCompleted(captureResults); |
| 8351 | return base::unexpected(NO_ERROR); |
| 8352 | }) |
| 8353 | .share(); |
| 8354 | } |
| 8355 | return renderFuture; |
| 8356 | } |
| 8357 | |
| 8358 | ftl::SharedFuture<FenceResult> SurfaceFlinger::captureScreenshotLegacy( |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8359 | RenderAreaBuilderVariant renderAreaBuilder, GetLayerSnapshotsFunction getLayerSnapshotsFn, |
| 8360 | const std::shared_ptr<renderengine::ExternalTexture>& buffer, bool regionSampling, |
| 8361 | bool grayscale, bool isProtected, const sp<IScreenCaptureListener>& captureListener) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 8362 | SFTRACE_CALL(); |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8363 | |
| 8364 | auto takeScreenshotFn = [=, this, renderAreaBuilder = std::move(renderAreaBuilder)]() REQUIRES( |
| 8365 | kMainThreadContext) mutable -> ftl::SharedFuture<FenceResult> { |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8366 | auto layers = getLayerSnapshotsFn(); |
| 8367 | if (FlagManager::getInstance().ce_fence_promise()) { |
| 8368 | for (auto& [layer, layerFE] : layers) { |
| 8369 | attachReleaseFenceFutureToLayer(layer, layerFE.get(), ui::INVALID_LAYER_STACK); |
| 8370 | } |
| 8371 | } |
| 8372 | auto displayState = getDisplayStateFromRenderAreaBuilder(renderAreaBuilder); |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8373 | |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 8374 | ScreenCaptureResults captureResults; |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8375 | std::unique_ptr<const RenderArea> renderArea = |
| 8376 | std::visit([](auto&& arg) -> std::unique_ptr<RenderArea> { return arg.build(); }, |
| 8377 | renderAreaBuilder); |
| 8378 | |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 8379 | if (!renderArea) { |
| 8380 | ALOGW("Skipping screen capture because of invalid render area."); |
| 8381 | if (captureListener) { |
| 8382 | captureResults.fenceResult = base::unexpected(NO_MEMORY); |
| 8383 | captureListener->onScreenCaptureCompleted(captureResults); |
| 8384 | } |
| 8385 | return ftl::yield<FenceResult>(base::unexpected(NO_ERROR)).share(); |
| 8386 | } |
| 8387 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8388 | auto layerFEs = extractLayerFEs(layers); |
Melody Hsu | 9db32ff | 2024-04-17 19:52:55 +0000 | [diff] [blame] | 8389 | ftl::SharedFuture<FenceResult> renderFuture = |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8390 | renderScreenImpl(std::move(renderArea), buffer, regionSampling, grayscale, |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8391 | isProtected, captureResults, displayState, layers, layerFEs); |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 8392 | |
| 8393 | if (captureListener) { |
| 8394 | // Defer blocking on renderFuture back to the Binder thread. |
| 8395 | return ftl::Future(std::move(renderFuture)) |
| 8396 | .then([captureListener, captureResults = std::move(captureResults)]( |
| 8397 | FenceResult fenceResult) mutable -> FenceResult { |
| 8398 | captureResults.fenceResult = std::move(fenceResult); |
Nolan Scobie | 1d2deb2 | 2022-11-18 12:57:47 -0500 | [diff] [blame] | 8399 | captureListener->onScreenCaptureCompleted(captureResults); |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 8400 | return base::unexpected(NO_ERROR); |
| 8401 | }) |
| 8402 | .share(); |
| 8403 | } |
| 8404 | return renderFuture; |
| 8405 | }; |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 8406 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 8407 | // TODO(b/294936197): Run takeScreenshotsFn() in a binder thread to reduce the number |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8408 | // of calls on the main thread. |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 8409 | auto future = |
| 8410 | mScheduler->schedule(FTL_FAKE_GUARD(kMainThreadContext, std::move(takeScreenshotFn))); |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 8411 | |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 8412 | // Flatten nested futures. |
Dominik Laskowski | b17c621 | 2022-05-09 09:36:19 -0700 | [diff] [blame] | 8413 | auto chain = ftl::Future(std::move(future)).then([](ftl::SharedFuture<FenceResult> future) { |
| 8414 | return future; |
| 8415 | }); |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 8416 | |
| 8417 | return chain.share(); |
Mathias Agopian | 2a9fc49 | 2013-03-01 13:42:57 -0800 | [diff] [blame] | 8418 | } |
| 8419 | |
Dominik Laskowski | b17c621 | 2022-05-09 09:36:19 -0700 | [diff] [blame] | 8420 | ftl::SharedFuture<FenceResult> SurfaceFlinger::renderScreenImpl( |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 8421 | std::unique_ptr<const RenderArea> renderArea, |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 8422 | const std::shared_ptr<renderengine::ExternalTexture>& buffer, bool regionSampling, |
| 8423 | bool grayscale, bool isProtected, ScreenCaptureResults& captureResults, |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8424 | std::optional<OutputCompositionState>& displayState, |
| 8425 | std::vector<std::pair<Layer*, sp<LayerFE>>>& layers, std::vector<sp<LayerFE>>& layerFEs) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 8426 | SFTRACE_CALL(); |
Alec Mouri | b4814de | 2023-09-11 22:09:03 +0000 | [diff] [blame] | 8427 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8428 | for (auto& layerFE : layerFEs) { |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 8429 | frontend::LayerSnapshot* snapshot = layerFE->mSnapshot.get(); |
| 8430 | captureResults.capturedSecureLayers |= (snapshot->isVisible && snapshot->isSecure); |
| 8431 | captureResults.capturedHdrLayers |= isHdrLayer(*snapshot); |
| 8432 | layerFE->mSnapshot->geomLayerTransform = |
| 8433 | renderArea->getTransform() * layerFE->mSnapshot->geomLayerTransform; |
Patrick Williams | c6e0cce | 2023-02-28 23:04:45 -0600 | [diff] [blame] | 8434 | layerFE->mSnapshot->geomInverseLayerTransform = |
| 8435 | layerFE->mSnapshot->geomLayerTransform.inverse(); |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 8436 | } |
chaviw | 426b5c0 | 2020-07-27 11:20:15 -0700 | [diff] [blame] | 8437 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 8438 | auto capturedBuffer = buffer; |
| 8439 | |
| 8440 | auto requestedDataspace = renderArea->getReqDataSpace(); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8441 | auto parent = renderArea->getParentLayer(); |
Alec Mouri | fcedb9c | 2022-04-11 20:02:17 +0000 | [diff] [blame] | 8442 | auto renderIntent = RenderIntent::TONE_MAP_COLORIMETRIC; |
Sally Qi | 6c903ca | 2022-04-25 11:24:20 -0700 | [diff] [blame] | 8443 | auto sdrWhitePointNits = DisplayDevice::sDefaultMaxLumiance; |
| 8444 | auto displayBrightnessNits = DisplayDevice::sDefaultMaxLumiance; |
| 8445 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 8446 | captureResults.capturedDataspace = requestedDataspace; |
| 8447 | |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 8448 | const bool enableLocalTonemapping = FlagManager::getInstance().local_tonemap_screenshots() && |
| 8449 | !renderArea->getHintForSeamlessTransition(); |
| 8450 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8451 | if (displayState) { |
| 8452 | const auto& state = displayState.value(); |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8453 | captureResults.capturedDataspace = |
Melody Hsu | 683fe04 | 2024-06-04 17:22:04 +0000 | [diff] [blame] | 8454 | pickBestDataspace(requestedDataspace, state, captureResults.capturedHdrLayers, |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8455 | renderArea->getHintForSeamlessTransition()); |
| 8456 | sdrWhitePointNits = state.sdrWhitePointNits; |
Robert Carr | 12a3b9b | 2022-03-10 09:55:29 -0800 | [diff] [blame] | 8457 | |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8458 | if (!captureResults.capturedHdrLayers) { |
| 8459 | displayBrightnessNits = sdrWhitePointNits; |
| 8460 | } else { |
| 8461 | displayBrightnessNits = state.displayBrightnessNits; |
| 8462 | if (!enableLocalTonemapping) { |
| 8463 | // Only clamp the display brightness if this is not a seamless transition. |
| 8464 | // Otherwise for seamless transitions it's important to match the current |
| 8465 | // display state as the buffer will be shown under these same conditions, and we |
| 8466 | // want to avoid any flickers |
| 8467 | if (sdrWhitePointNits > 1.0f && !renderArea->getHintForSeamlessTransition()) { |
| 8468 | // Restrict the amount of HDR "headroom" in the screenshot to avoid |
| 8469 | // over-dimming the SDR portion. 2.0 chosen by experimentation |
| 8470 | constexpr float kMaxScreenshotHeadroom = 2.0f; |
| 8471 | displayBrightnessNits = std::min(sdrWhitePointNits * kMaxScreenshotHeadroom, |
| 8472 | displayBrightnessNits); |
Alec Mouri | b4814de | 2023-09-11 22:09:03 +0000 | [diff] [blame] | 8473 | } |
Alec Mouri | 5e488f8 | 2023-09-15 01:27:49 +0000 | [diff] [blame] | 8474 | } |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8475 | } |
Alec Mouri | ffb3221 | 2023-09-16 19:29:15 +0000 | [diff] [blame] | 8476 | |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 8477 | // Screenshots leaving the device should be colorimetric |
| 8478 | if (requestedDataspace == ui::Dataspace::UNKNOWN && |
| 8479 | renderArea->getHintForSeamlessTransition()) { |
| 8480 | renderIntent = state.renderIntent; |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 8481 | } |
Robert Carr | 12a3b9b | 2022-03-10 09:55:29 -0800 | [diff] [blame] | 8482 | } |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 8483 | |
| 8484 | captureResults.buffer = capturedBuffer->getBuffer(); |
chaviw | 426b5c0 | 2020-07-27 11:20:15 -0700 | [diff] [blame] | 8485 | |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8486 | ui::LayerStack layerStack{ui::DEFAULT_LAYER_STACK}; |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8487 | if (!layerFEs.empty()) { |
| 8488 | const sp<LayerFE>& layerFE = layerFEs.back(); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8489 | layerStack = layerFE->getCompositionState()->outputFilter.layerStack; |
Yichi Chen | 40773d9 | 2020-04-01 10:10:18 +0800 | [diff] [blame] | 8490 | } |
Ady Abraham | 9959994 | 2021-01-27 20:27:23 -0800 | [diff] [blame] | 8491 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8492 | auto copyLayerFEs = [&layerFEs]() { |
| 8493 | std::vector<sp<compositionengine::LayerFE>> ceLayerFEs; |
| 8494 | ceLayerFEs.reserve(layerFEs.size()); |
| 8495 | for (const auto& layerFE : layerFEs) { |
| 8496 | ceLayerFEs.push_back(layerFE); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8497 | } |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8498 | return ceLayerFEs; |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8499 | }; |
| 8500 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 8501 | auto present = [this, buffer = capturedBuffer, dataspace = captureResults.capturedDataspace, |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 8502 | sdrWhitePointNits, displayBrightnessNits, grayscale, isProtected, |
| 8503 | layerFEs = copyLayerFEs(), layerStack, regionSampling, |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 8504 | renderArea = std::move(renderArea), renderIntent, |
| 8505 | enableLocalTonemapping]() -> FenceResult { |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8506 | std::unique_ptr<compositionengine::CompositionEngine> compositionEngine = |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 8507 | mFactory.createCompositionEngine(); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8508 | compositionEngine->setRenderEngine(mRenderEngine.get()); |
| 8509 | |
| 8510 | compositionengine::Output::ColorProfile colorProfile{.dataspace = dataspace, |
| 8511 | .renderIntent = renderIntent}; |
| 8512 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 8513 | float targetBrightness = 1.0f; |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 8514 | if (enableLocalTonemapping) { |
| 8515 | // Boost the whole scene so that SDR white is at 1.0 while still communicating the hdr |
| 8516 | // sdr ratio via display brightness / sdrWhite nits. |
| 8517 | targetBrightness = sdrWhitePointNits / displayBrightnessNits; |
| 8518 | } else if (dataspace == ui::Dataspace::BT2020_HLG) { |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 8519 | const float maxBrightnessNits = displayBrightnessNits / sdrWhitePointNits * 203; |
| 8520 | // With a low dimming ratio, don't fit the entire curve. Otherwise mixed content |
| 8521 | // will appear way too bright. |
| 8522 | if (maxBrightnessNits < 1000.f) { |
| 8523 | targetBrightness = 1000.f / maxBrightnessNits; |
| 8524 | } |
| 8525 | } |
| 8526 | |
Alec Mouri | ffb3221 | 2023-09-16 19:29:15 +0000 | [diff] [blame] | 8527 | // Screenshots leaving the device must not dim in gamma space. |
| 8528 | const bool dimInGammaSpaceForEnhancedScreenshots = mDimInGammaSpaceForEnhancedScreenshots && |
| 8529 | renderArea->getHintForSeamlessTransition(); |
| 8530 | |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8531 | std::shared_ptr<ScreenCaptureOutput> output = createScreenCaptureOutput( |
| 8532 | ScreenCaptureOutputArgs{.compositionEngine = *compositionEngine, |
| 8533 | .colorProfile = colorProfile, |
| 8534 | .renderArea = *renderArea, |
| 8535 | .layerStack = layerStack, |
| 8536 | .buffer = std::move(buffer), |
| 8537 | .sdrWhitePointNits = sdrWhitePointNits, |
| 8538 | .displayBrightnessNits = displayBrightnessNits, |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 8539 | .targetBrightness = targetBrightness, |
Alec Mouri | 7dae9c1 | 2023-07-13 19:40:53 +0000 | [diff] [blame] | 8540 | .regionSampling = regionSampling, |
Alec Mouri | 9af38ba | 2023-08-16 22:41:14 +0000 | [diff] [blame] | 8541 | .treat170mAsSrgb = mTreat170mAsSrgb, |
| 8542 | .dimInGammaSpaceForEnhancedScreenshots = |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 8543 | dimInGammaSpaceForEnhancedScreenshots, |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 8544 | .isProtected = isProtected, |
| 8545 | .enableLocalTonemapping = enableLocalTonemapping}); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8546 | |
| 8547 | const float colorSaturation = grayscale ? 0 : 1; |
| 8548 | compositionengine::CompositionRefreshArgs refreshArgs{ |
| 8549 | .outputs = {output}, |
| 8550 | .layers = std::move(layerFEs), |
| 8551 | .updatingOutputGeometryThisFrame = true, |
| 8552 | .updatingGeometryThisFrame = true, |
| 8553 | .colorTransformMatrix = calculateColorMatrix(colorSaturation), |
| 8554 | }; |
| 8555 | compositionEngine->present(refreshArgs); |
| 8556 | |
| 8557 | return output->getRenderSurface()->getClientTargetAcquireFence(); |
| 8558 | }; |
| 8559 | |
| 8560 | // If RenderEngine is threaded, we can safely call CompositionEngine::present off the main |
| 8561 | // thread as the RenderEngine::drawLayers call will run on RenderEngine's thread. Otherwise, |
| 8562 | // we need RenderEngine to run on the main thread so we call CompositionEngine::present |
| 8563 | // immediately. |
| 8564 | // |
| 8565 | // TODO(b/196334700) Once we use RenderEngineThreaded everywhere we can always defer the call |
| 8566 | // to CompositionEngine::present. |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 8567 | ftl::SharedFuture<FenceResult> presentFuture; |
| 8568 | if (FlagManager::getInstance().single_hop_screenshot() && |
| 8569 | FlagManager::getInstance().ce_fence_promise()) { |
| 8570 | presentFuture = mRenderEngine->isThreaded() |
| 8571 | ? ftl::yield(present()).share() |
| 8572 | : mScheduler->schedule(std::move(present)).share(); |
| 8573 | } else { |
| 8574 | presentFuture = mRenderEngine->isThreaded() ? ftl::defer(std::move(present)).share() |
| 8575 | : ftl::yield(present()).share(); |
| 8576 | } |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8577 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 8578 | if (!FlagManager::getInstance().ce_fence_promise()) { |
| 8579 | for (auto& [layer, layerFE] : layers) { |
| 8580 | layer->onLayerDisplayed(presentFuture, ui::INVALID_LAYER_STACK, |
| 8581 | [layerFE = std::move(layerFE)](FenceResult) { |
| 8582 | if (FlagManager::getInstance() |
| 8583 | .screenshot_fence_preservation()) { |
| 8584 | const auto compositionResult = |
| 8585 | layerFE->stealCompositionResult(); |
| 8586 | const auto& fences = compositionResult.releaseFences; |
| 8587 | // CompositionEngine may choose to cull layers that |
| 8588 | // aren't visible, so pass a non-fence. |
| 8589 | return fences.empty() ? Fence::NO_FENCE |
| 8590 | : fences.back().first.get(); |
| 8591 | } else { |
| 8592 | return layerFE->stealCompositionResult() |
| 8593 | .releaseFences.back() |
| 8594 | .first.get(); |
| 8595 | } |
| 8596 | }); |
| 8597 | } |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 8598 | } |
| 8599 | |
| 8600 | return presentFuture; |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 8601 | } |
| 8602 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8603 | void SurfaceFlinger::traverseLegacyLayers(const LayerVector::Visitor& visitor) const { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 8604 | if (mLayerLifecycleManagerEnabled) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8605 | for (auto& layer : mLegacyLayers) { |
| 8606 | visitor(layer.second.get()); |
| 8607 | } |
| 8608 | } else { |
| 8609 | mDrawingState.traverse(visitor); |
| 8610 | } |
| 8611 | } |
| 8612 | |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 8613 | // --------------------------------------------------------------------------- |
| 8614 | |
Edgar Arriaga | 844fa67 | 2020-01-16 14:21:42 -0800 | [diff] [blame] | 8615 | void SurfaceFlinger::State::traverse(const LayerVector::Visitor& visitor) const { |
| 8616 | layersSortedByZ.traverse(visitor); |
| 8617 | } |
| 8618 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 8619 | void SurfaceFlinger::State::traverseInZOrder(const LayerVector::Visitor& visitor) const { |
| 8620 | layersSortedByZ.traverseInZOrder(stateSet, visitor); |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 8621 | } |
| 8622 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 8623 | void SurfaceFlinger::State::traverseInReverseZOrder(const LayerVector::Visitor& visitor) const { |
| 8624 | layersSortedByZ.traverseInReverseZOrder(stateSet, visitor); |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 8625 | } |
| 8626 | |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 8627 | void SurfaceFlinger::traverseLayersInLayerStack(ui::LayerStack layerStack, const int32_t uid, |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 8628 | std::unordered_set<uint32_t> excludeLayerIds, |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 8629 | const LayerVector::Visitor& visitor) { |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 8630 | // We loop through the first level of layers without traversing, |
chaviw | 0e3479f | 2018-09-10 16:49:30 -0700 | [diff] [blame] | 8631 | // as we need to determine which layers belong to the requested display. |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 8632 | for (const auto& layer : mDrawingState.layersSortedByZ) { |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 8633 | if (layer->getLayerStack() != layerStack) { |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 8634 | continue; |
| 8635 | } |
Chia-I Wu | ec2d985 | 2017-11-21 09:21:01 -0800 | [diff] [blame] | 8636 | // relative layers are traversed in Layer::traverseInZOrder |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 8637 | layer->traverseInZOrder(LayerVector::StateSet::Drawing, [&](Layer* layer) { |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 8638 | if (layer->isInternalDisplayOverlay()) { |
Adrian Roos | 8acf5a0 | 2018-01-17 21:28:19 +0100 | [diff] [blame] | 8639 | return; |
| 8640 | } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 8641 | if (!layer->isVisible()) { |
| 8642 | return; |
| 8643 | } |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 8644 | if (uid != CaptureArgs::UNSET_UID && layer->getOwnerUid() != uid) { |
| 8645 | return; |
| 8646 | } |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 8647 | |
| 8648 | if (!excludeLayerIds.empty()) { |
| 8649 | auto p = sp<Layer>::fromExisting(layer); |
| 8650 | while (p != nullptr) { |
| 8651 | if (excludeLayerIds.count(p->sequence) != 0) { |
| 8652 | return; |
| 8653 | } |
| 8654 | p = p->getParent(); |
| 8655 | } |
| 8656 | } |
| 8657 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 8658 | visitor(layer); |
| 8659 | }); |
| 8660 | } |
| 8661 | } |
| 8662 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 8663 | ftl::Optional<scheduler::FrameRateMode> SurfaceFlinger::getPreferredDisplayMode( |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 8664 | PhysicalDisplayId displayId, DisplayModeId defaultModeId) const { |
| 8665 | if (const auto schedulerMode = mScheduler->getPreferredDisplayMode(); |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 8666 | schedulerMode.modePtr->getPhysicalDisplayId() == displayId) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 8667 | return schedulerMode; |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 8668 | } |
| 8669 | |
| 8670 | return mPhysicalDisplays.get(displayId) |
| 8671 | .transform(&PhysicalDisplay::snapshotRef) |
| 8672 | .and_then([&](const display::DisplaySnapshot& snapshot) { |
| 8673 | return snapshot.displayModes().get(defaultModeId); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 8674 | }) |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 8675 | .transform([](const DisplayModePtr& modePtr) { |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 8676 | return scheduler::FrameRateMode{modePtr->getPeakFps(), ftl::as_non_null(modePtr)}; |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 8677 | }); |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 8678 | } |
| 8679 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 8680 | status_t SurfaceFlinger::setDesiredDisplayModeSpecsInternal( |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 8681 | const sp<DisplayDevice>& display, |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 8682 | const scheduler::RefreshRateSelector::PolicyVariant& policy) { |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 8683 | const auto displayId = display->getPhysicalId(); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 8684 | SFTRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str()); |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 8685 | |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 8686 | Mutex::Autolock lock(mStateLock); |
| 8687 | |
Ady Abraham | fb85366 | 2021-04-09 11:46:40 -0700 | [diff] [blame] | 8688 | if (mDebugDisplayModeSetByBackdoor) { |
| 8689 | // ignore this request as mode is overridden by backdoor |
| 8690 | return NO_ERROR; |
| 8691 | } |
| 8692 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 8693 | auto& selector = display->refreshRateSelector(); |
| 8694 | using SetPolicyResult = scheduler::RefreshRateSelector::SetPolicyResult; |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 8695 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 8696 | switch (selector.setPolicy(policy)) { |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 8697 | case SetPolicyResult::Invalid: |
| 8698 | return BAD_VALUE; |
| 8699 | case SetPolicyResult::Unchanged: |
| 8700 | return NO_ERROR; |
| 8701 | case SetPolicyResult::Changed: |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 8702 | break; |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 8703 | } |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 8704 | |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 8705 | return applyRefreshRateSelectorPolicy(displayId, selector); |
Dominik Laskowski | 3f72640 | 2022-11-21 17:21:22 -0500 | [diff] [blame] | 8706 | } |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 8707 | |
Dominik Laskowski | 3f72640 | 2022-11-21 17:21:22 -0500 | [diff] [blame] | 8708 | status_t SurfaceFlinger::applyRefreshRateSelectorPolicy( |
Dominik Laskowski | 1746afd | 2024-04-10 19:21:37 -0400 | [diff] [blame] | 8709 | PhysicalDisplayId displayId, const scheduler::RefreshRateSelector& selector) { |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 8710 | const scheduler::RefreshRateSelector::Policy currentPolicy = selector.getCurrentPolicy(); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 8711 | ALOGV("Setting desired display mode specs: %s", currentPolicy.toString().c_str()); |
Adrian Salido | ab6ef6c | 2019-08-28 10:02:49 -0700 | [diff] [blame] | 8712 | |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 8713 | // TODO(b/140204874): Leave the event in until we do proper testing with all apps that might |
| 8714 | // be depending in this callback. |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 8715 | if (const auto activeMode = selector.getActiveMode(); displayId == mActiveDisplayId) { |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 8716 | mScheduler->onPrimaryDisplayModeChanged(scheduler::Cycle::Render, activeMode); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 8717 | toggleKernelIdleTimer(); |
| 8718 | } else { |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 8719 | mScheduler->onNonPrimaryDisplayModeChanged(scheduler::Cycle::Render, activeMode); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 8720 | } |
Ady Abraham | 838de06 | 2019-02-04 10:24:03 -0800 | [diff] [blame] | 8721 | |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 8722 | auto preferredModeOpt = getPreferredDisplayMode(displayId, currentPolicy.defaultMode); |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 8723 | if (!preferredModeOpt) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 8724 | ALOGE("%s: Preferred mode is unknown", __func__); |
| 8725 | return NAME_NOT_FOUND; |
| 8726 | } |
| 8727 | |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 8728 | auto preferredMode = std::move(*preferredModeOpt); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 8729 | const auto preferredModeId = preferredMode.modePtr->getId(); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 8730 | |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 8731 | const Fps preferredFps = preferredMode.fps; |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 8732 | ALOGV("Switching to Scheduler preferred mode %d (%s)", ftl::to_underlying(preferredModeId), |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 8733 | to_string(preferredFps).c_str()); |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 8734 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 8735 | if (!selector.isModeAllowed(preferredMode)) { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 8736 | ALOGE("%s: Preferred mode %d is disallowed", __func__, ftl::to_underlying(preferredModeId)); |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 8737 | return INVALID_OPERATION; |
Dominik Laskowski | 4f1dd8c | 2019-04-17 15:54:30 -0700 | [diff] [blame] | 8738 | } |
| 8739 | |
Dominik Laskowski | 1746afd | 2024-04-10 19:21:37 -0400 | [diff] [blame] | 8740 | setDesiredMode({std::move(preferredMode), .emitEvent = true}); |
Ady Abraham | 33a386b | 2023-07-18 15:37:11 -0700 | [diff] [blame] | 8741 | |
| 8742 | // Update the frameRateOverride list as the display render rate might have changed |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 8743 | if (mScheduler->updateFrameRateOverrides(scheduler::GlobalSignals{}, preferredFps)) { |
Ady Abraham | 33a386b | 2023-07-18 15:37:11 -0700 | [diff] [blame] | 8744 | triggerOnFrameRateOverridesChanged(); |
| 8745 | } |
| 8746 | |
Ady Abraham | d9b3ea6 | 2019-02-26 14:08:03 -0800 | [diff] [blame] | 8747 | return NO_ERROR; |
| 8748 | } |
| 8749 | |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 8750 | namespace { |
| 8751 | FpsRange translate(const gui::DisplayModeSpecs::RefreshRateRanges::RefreshRateRange& aidlRange) { |
| 8752 | return FpsRange{Fps::fromValue(aidlRange.min), Fps::fromValue(aidlRange.max)}; |
| 8753 | } |
| 8754 | |
| 8755 | FpsRanges translate(const gui::DisplayModeSpecs::RefreshRateRanges& aidlRanges) { |
| 8756 | return FpsRanges{translate(aidlRanges.physical), translate(aidlRanges.render)}; |
| 8757 | } |
| 8758 | |
| 8759 | gui::DisplayModeSpecs::RefreshRateRanges::RefreshRateRange translate(const FpsRange& range) { |
| 8760 | gui::DisplayModeSpecs::RefreshRateRanges::RefreshRateRange aidlRange; |
| 8761 | aidlRange.min = range.min.getValue(); |
| 8762 | aidlRange.max = range.max.getValue(); |
| 8763 | return aidlRange; |
| 8764 | } |
| 8765 | |
| 8766 | gui::DisplayModeSpecs::RefreshRateRanges translate(const FpsRanges& ranges) { |
| 8767 | gui::DisplayModeSpecs::RefreshRateRanges aidlRanges; |
| 8768 | aidlRanges.physical = translate(ranges.physical); |
| 8769 | aidlRanges.render = translate(ranges.render); |
| 8770 | return aidlRanges; |
| 8771 | } |
| 8772 | |
| 8773 | } // namespace |
| 8774 | |
| 8775 | status_t SurfaceFlinger::setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, |
| 8776 | const gui::DisplayModeSpecs& specs) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 8777 | SFTRACE_CALL(); |
Ana Krulec | 0782b88 | 2019-10-15 17:34:54 -0700 | [diff] [blame] | 8778 | |
| 8779 | if (!displayToken) { |
| 8780 | return BAD_VALUE; |
| 8781 | } |
| 8782 | |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 8783 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(kMainThreadContext) -> status_t { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 8784 | const auto display = FTL_FAKE_GUARD(mStateLock, getDisplayDeviceLocked(displayToken)); |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 8785 | if (!display) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 8786 | ALOGE("Attempt to set desired display modes for invalid display token %p", |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 8787 | displayToken.get()); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 8788 | return NAME_NOT_FOUND; |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 8789 | } else if (display->isVirtual()) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 8790 | ALOGW("Attempt to set desired display modes for virtual display"); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 8791 | return INVALID_OPERATION; |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 8792 | } else { |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 8793 | using Policy = scheduler::RefreshRateSelector::DisplayManagerPolicy; |
Ady Abraham | 6723172 | 2024-03-21 18:06:21 -0700 | [diff] [blame] | 8794 | const auto idleScreenConfigOpt = |
| 8795 | FlagManager::getInstance().idle_screen_refresh_rate_timeout() |
| 8796 | ? specs.idleScreenRefreshRateConfig |
| 8797 | : std::nullopt; |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 8798 | const Policy policy{DisplayModeId(specs.defaultMode), translate(specs.primaryRanges), |
Ady Abraham | 6723172 | 2024-03-21 18:06:21 -0700 | [diff] [blame] | 8799 | translate(specs.appRequestRanges), specs.allowGroupSwitching, |
| 8800 | idleScreenConfigOpt}; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 8801 | |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 8802 | return setDesiredDisplayModeSpecsInternal(display, policy); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 8803 | } |
| 8804 | }); |
| 8805 | |
| 8806 | return future.get(); |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 8807 | } |
| 8808 | |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 8809 | status_t SurfaceFlinger::getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 8810 | gui::DisplayModeSpecs* outSpecs) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 8811 | SFTRACE_CALL(); |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 8812 | |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 8813 | if (!displayToken || !outSpecs) { |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 8814 | return BAD_VALUE; |
| 8815 | } |
| 8816 | |
| 8817 | Mutex::Autolock lock(mStateLock); |
| 8818 | const auto display = getDisplayDeviceLocked(displayToken); |
| 8819 | if (!display) { |
| 8820 | return NAME_NOT_FOUND; |
| 8821 | } |
| 8822 | |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 8823 | if (display->isVirtual()) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 8824 | return INVALID_OPERATION; |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 8825 | } |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 8826 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 8827 | scheduler::RefreshRateSelector::Policy policy = |
| 8828 | display->refreshRateSelector().getDisplayManagerPolicy(); |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 8829 | outSpecs->defaultMode = ftl::to_underlying(policy.defaultMode); |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 8830 | outSpecs->allowGroupSwitching = policy.allowGroupSwitching; |
| 8831 | outSpecs->primaryRanges = translate(policy.primaryRanges); |
| 8832 | outSpecs->appRequestRanges = translate(policy.appRequestRanges); |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 8833 | return NO_ERROR; |
Ana Krulec | 0782b88 | 2019-10-15 17:34:54 -0700 | [diff] [blame] | 8834 | } |
| 8835 | |
Dominik Laskowski | 7584836 | 2019-11-11 17:57:20 -0800 | [diff] [blame] | 8836 | void SurfaceFlinger::onLayerFirstRef(Layer* layer) { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 8837 | mNumLayers++; |
Ady Abraham | be09aad | 2021-05-03 18:59:38 -0700 | [diff] [blame] | 8838 | if (!layer->isRemovedFromCurrentState()) { |
| 8839 | mScheduler->registerLayer(layer); |
| 8840 | } |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 8841 | } |
| 8842 | |
| 8843 | void SurfaceFlinger::onLayerDestroyed(Layer* layer) { |
| 8844 | mNumLayers--; |
Robert Carr | 867be37 | 2021-06-29 17:36:02 -0700 | [diff] [blame] | 8845 | removeHierarchyFromOffscreenLayers(layer); |
Ady Abraham | be09aad | 2021-05-03 18:59:38 -0700 | [diff] [blame] | 8846 | if (!layer->isRemovedFromCurrentState()) { |
| 8847 | mScheduler->deregisterLayer(layer); |
| 8848 | } |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 8849 | if (mTransactionTracing) { |
| 8850 | mTransactionTracing->onLayerRemoved(layer->getSequence()); |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 8851 | } |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 8852 | mScheduler->onLayerDestroyed(layer); |
Valerie Hau | c568680 | 2019-11-22 14:18:09 -0800 | [diff] [blame] | 8853 | } |
| 8854 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 8855 | void SurfaceFlinger::onLayerUpdate() { |
| 8856 | scheduleCommit(FrameHint::kActive); |
| 8857 | } |
| 8858 | |
Valerie Hau | c568680 | 2019-11-22 14:18:09 -0800 | [diff] [blame] | 8859 | // WARNING: ONLY CALL THIS FROM LAYER DTOR |
| 8860 | // Here we add children in the current state to offscreen layers and remove the |
| 8861 | // layer itself from the offscreen layer list. Since |
| 8862 | // this is the dtor, it is safe to access the current state. This keeps us |
| 8863 | // from dangling children layers such that they are not reachable from the |
| 8864 | // Drawing state nor the offscreen layer list |
| 8865 | // See b/141111965 |
Robert Carr | 867be37 | 2021-06-29 17:36:02 -0700 | [diff] [blame] | 8866 | void SurfaceFlinger::removeHierarchyFromOffscreenLayers(Layer* layer) { |
Valerie Hau | c568680 | 2019-11-22 14:18:09 -0800 | [diff] [blame] | 8867 | for (auto& child : layer->getCurrentChildren()) { |
| 8868 | mOffscreenLayers.emplace(child.get()); |
| 8869 | } |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 8870 | mOffscreenLayers.erase(layer); |
| 8871 | } |
| 8872 | |
Robert Carr | 867be37 | 2021-06-29 17:36:02 -0700 | [diff] [blame] | 8873 | void SurfaceFlinger::removeFromOffscreenLayers(Layer* layer) { |
| 8874 | mOffscreenLayers.erase(layer); |
| 8875 | } |
| 8876 | |
Vishnu Nair | 08f6eae | 2019-11-26 14:01:39 -0800 | [diff] [blame] | 8877 | status_t SurfaceFlinger::setGlobalShadowSettings(const half4& ambientColor, const half4& spotColor, |
| 8878 | float lightPosY, float lightPosZ, |
| 8879 | float lightRadius) { |
| 8880 | Mutex::Autolock _l(mStateLock); |
| 8881 | mCurrentState.globalShadowSettings.ambientColor = vec4(ambientColor); |
| 8882 | mCurrentState.globalShadowSettings.spotColor = vec4(spotColor); |
| 8883 | mCurrentState.globalShadowSettings.lightPos.y = lightPosY; |
| 8884 | mCurrentState.globalShadowSettings.lightPos.z = lightPosZ; |
| 8885 | mCurrentState.globalShadowSettings.lightRadius = lightRadius; |
| 8886 | |
| 8887 | // these values are overridden when calculating the shadow settings for a layer. |
| 8888 | mCurrentState.globalShadowSettings.lightPos.x = 0.f; |
| 8889 | mCurrentState.globalShadowSettings.length = 0.f; |
Vishnu Nair | b13bb95 | 2019-11-15 10:24:08 -0800 | [diff] [blame] | 8890 | return NO_ERROR; |
| 8891 | } |
| 8892 | |
Lloyd Pique | 8d9f836 | 2020-02-11 19:13:09 -0800 | [diff] [blame] | 8893 | const std::unordered_map<std::string, uint32_t>& SurfaceFlinger::getGenericLayerMetadataKeyMap() |
| 8894 | const { |
| 8895 | // TODO(b/149500060): Remove this fixed/static mapping. Please prefer taking |
| 8896 | // on the work to remove the table in that bug rather than adding more to |
| 8897 | // it. |
| 8898 | static const std::unordered_map<std::string, uint32_t> genericLayerMetadataKeyMap{ |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 8899 | {"org.chromium.arc.V1_0.TaskId", gui::METADATA_TASK_ID}, |
| 8900 | {"org.chromium.arc.V1_0.CursorInfo", gui::METADATA_MOUSE_CURSOR}, |
Lloyd Pique | 8d9f836 | 2020-02-11 19:13:09 -0800 | [diff] [blame] | 8901 | }; |
| 8902 | return genericLayerMetadataKeyMap; |
| 8903 | } |
| 8904 | |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 8905 | status_t SurfaceFlinger::setGameModeFrameRateOverride(uid_t uid, float frameRate) { |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 8906 | PhysicalDisplayId displayId = [&]() { |
| 8907 | Mutex::Autolock lock(mStateLock); |
| 8908 | return getDefaultDisplayDeviceLocked()->getPhysicalId(); |
| 8909 | }(); |
| 8910 | |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 8911 | mScheduler->setGameModeFrameRateForUid(FrameRateOverride{static_cast<uid_t>(uid), frameRate}); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 8912 | mScheduler->onFrameRateOverridesChanged(scheduler::Cycle::Render, displayId); |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 8913 | return NO_ERROR; |
| 8914 | } |
| 8915 | |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 8916 | status_t SurfaceFlinger::setGameDefaultFrameRateOverride(uid_t uid, float frameRate) { |
| 8917 | if (FlagManager::getInstance().game_default_frame_rate()) { |
| 8918 | mScheduler->setGameDefaultFrameRateForUid( |
| 8919 | FrameRateOverride{static_cast<uid_t>(uid), frameRate}); |
| 8920 | } |
| 8921 | return NO_ERROR; |
| 8922 | } |
| 8923 | |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 8924 | status_t SurfaceFlinger::updateSmallAreaDetection( |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 8925 | std::vector<std::pair<int32_t, float>>& appIdThresholdMappings) { |
| 8926 | mScheduler->updateSmallAreaDetection(appIdThresholdMappings); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 8927 | return NO_ERROR; |
| 8928 | } |
| 8929 | |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 8930 | status_t SurfaceFlinger::setSmallAreaDetectionThreshold(int32_t appId, float threshold) { |
| 8931 | mScheduler->setSmallAreaDetectionThreshold(appId, threshold); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 8932 | return NO_ERROR; |
| 8933 | } |
| 8934 | |
Dominik Laskowski | 2013464 | 2020-04-20 22:36:44 -0700 | [diff] [blame] | 8935 | void SurfaceFlinger::enableRefreshRateOverlay(bool enable) { |
ramindani | b2158ee | 2023-02-13 20:29:59 -0800 | [diff] [blame] | 8936 | bool setByHwc = getHwComposer().hasCapability(Capability::REFRESH_RATE_CHANGED_CALLBACK_DEBUG); |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 8937 | for (const auto& [displayId, physical] : mPhysicalDisplays) { |
| 8938 | if (physical.snapshot().connectionType() == ui::DisplayConnectionType::Internal || |
Sally Qi | de329f2 | 2023-09-26 16:21:39 -0700 | [diff] [blame] | 8939 | FlagManager::getInstance().refresh_rate_overlay_on_external_display()) { |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 8940 | if (const auto display = getDisplayDeviceLocked(displayId)) { |
| 8941 | const auto enableOverlay = [&](bool setByHwc) FTL_FAKE_GUARD(kMainThreadContext) { |
| 8942 | const auto activeMode = mDisplayModeController.getActiveMode(displayId); |
| 8943 | const Fps refreshRate = activeMode.modePtr->getVsyncRate(); |
| 8944 | const Fps renderFps = activeMode.fps; |
| 8945 | |
| 8946 | display->enableRefreshRateOverlay(enable, setByHwc, refreshRate, renderFps, |
| 8947 | mRefreshRateOverlaySpinner, |
| 8948 | mRefreshRateOverlayRenderRate, |
| 8949 | mRefreshRateOverlayShowInMiddle); |
ramindani | 3c722da | 2023-07-12 20:37:59 -0700 | [diff] [blame] | 8950 | }; |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 8951 | |
ramindani | 3c722da | 2023-07-12 20:37:59 -0700 | [diff] [blame] | 8952 | enableOverlay(setByHwc); |
| 8953 | if (setByHwc) { |
| 8954 | const auto status = |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 8955 | getHwComposer().setRefreshRateChangedCallbackDebugEnabled(displayId, |
| 8956 | enable); |
ramindani | 3c722da | 2023-07-12 20:37:59 -0700 | [diff] [blame] | 8957 | if (status != NO_ERROR) { |
Ying Wei | 22d59cc | 2024-05-11 02:41:08 +0000 | [diff] [blame] | 8958 | ALOGE("Error %s refresh rate changed callback debug", |
| 8959 | enable ? "enabling" : "disabling"); |
ramindani | 3c722da | 2023-07-12 20:37:59 -0700 | [diff] [blame] | 8960 | enableOverlay(/*setByHwc*/ false); |
| 8961 | } |
| 8962 | } |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 8963 | } |
Dominik Laskowski | 2013464 | 2020-04-20 22:36:44 -0700 | [diff] [blame] | 8964 | } |
Ady Abraham | 1b11bc6 | 2021-06-03 19:51:19 -0700 | [diff] [blame] | 8965 | } |
Dominik Laskowski | 2013464 | 2020-04-20 22:36:44 -0700 | [diff] [blame] | 8966 | } |
| 8967 | |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 8968 | void SurfaceFlinger::enableHdrSdrRatioOverlay(bool enable) { |
| 8969 | for (const auto& [id, display] : mPhysicalDisplays) { |
| 8970 | if (display.snapshot().connectionType() == ui::DisplayConnectionType::Internal) { |
| 8971 | if (const auto device = getDisplayDeviceLocked(id)) { |
| 8972 | device->enableHdrSdrRatioOverlay(enable); |
| 8973 | } |
| 8974 | } |
| 8975 | } |
| 8976 | } |
| 8977 | |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8978 | int SurfaceFlinger::getGpuContextPriority() { |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 8979 | return getRenderEngine().getContextPriority(); |
Ana Krulec | 31f2b3c | 2020-12-14 14:30:09 -0800 | [diff] [blame] | 8980 | } |
| 8981 | |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 8982 | int SurfaceFlinger::calculateMaxAcquiredBufferCount(Fps refreshRate, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 8983 | std::chrono::nanoseconds presentLatency) { |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 8984 | auto pipelineDepth = presentLatency.count() / refreshRate.getPeriodNsecs(); |
| 8985 | if (presentLatency.count() % refreshRate.getPeriodNsecs()) { |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 8986 | pipelineDepth++; |
| 8987 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 8988 | return std::max(minAcquiredBuffers, static_cast<int64_t>(pipelineDepth - 1)); |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 8989 | } |
| 8990 | |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 8991 | status_t SurfaceFlinger::getMaxAcquiredBufferCount(int* buffers) const { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 8992 | Fps maxRefreshRate = 60_Hz; |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 8993 | |
| 8994 | if (!getHwComposer().isHeadless()) { |
| 8995 | if (const auto display = getDefaultDisplayDevice()) { |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 8996 | maxRefreshRate = display->refreshRateSelector().getSupportedRefreshRateRange().max; |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 8997 | } |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 8998 | } |
| 8999 | |
| 9000 | *buffers = getMaxAcquiredBufferCountForRefreshRate(maxRefreshRate); |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 9001 | return NO_ERROR; |
| 9002 | } |
| 9003 | |
rnlee | ed20fa4 | 2021-08-10 18:00:03 -0700 | [diff] [blame] | 9004 | uint32_t SurfaceFlinger::getMaxAcquiredBufferCountForCurrentRefreshRate(uid_t uid) const { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 9005 | Fps refreshRate = 60_Hz; |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 9006 | |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 9007 | if (const auto frameRateOverride = mScheduler->getFrameRateOverride(uid)) { |
| 9008 | refreshRate = *frameRateOverride; |
| 9009 | } else if (!getHwComposer().isHeadless()) { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 9010 | if (const auto display = FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked())) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 9011 | refreshRate = display->refreshRateSelector().getActiveMode().fps; |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 9012 | } |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 9013 | } |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 9014 | |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 9015 | return getMaxAcquiredBufferCountForRefreshRate(refreshRate); |
| 9016 | } |
| 9017 | |
| 9018 | int SurfaceFlinger::getMaxAcquiredBufferCountForRefreshRate(Fps refreshRate) const { |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 9019 | const auto vsyncConfig = |
| 9020 | mScheduler->getVsyncConfiguration().getConfigsForRefreshRate(refreshRate).late; |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 9021 | const auto presentLatency = vsyncConfig.appWorkDuration + vsyncConfig.sfWorkDuration; |
| 9022 | return calculateMaxAcquiredBufferCount(refreshRate, presentLatency); |
| 9023 | } |
| 9024 | |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 9025 | void SurfaceFlinger::handleLayerCreatedLocked(const LayerCreatedState& state, VsyncId vsyncId) { |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 9026 | sp<Layer> layer = state.layer.promote(); |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 9027 | if (!layer) { |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 9028 | ALOGD("Layer was destroyed soon after creation %p", state.layer.unsafe_get()); |
| 9029 | return; |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 9030 | } |
Josh Gao | 194ff39 | 2022-09-08 16:19:29 -0700 | [diff] [blame] | 9031 | MUTEX_ALIAS(mStateLock, layer->mFlinger->mStateLock); |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 9032 | |
| 9033 | sp<Layer> parent; |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 9034 | bool addToRoot = state.addToRoot; |
| 9035 | if (state.initialParent != nullptr) { |
| 9036 | parent = state.initialParent.promote(); |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 9037 | if (parent == nullptr) { |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 9038 | ALOGD("Parent was destroyed soon after creation %p", state.initialParent.unsafe_get()); |
Garfield Tan | d712ad3 | 2021-10-27 13:44:22 -0700 | [diff] [blame] | 9039 | addToRoot = false; |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 9040 | } |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 9041 | } |
| 9042 | |
Garfield Tan | d712ad3 | 2021-10-27 13:44:22 -0700 | [diff] [blame] | 9043 | if (parent == nullptr && addToRoot) { |
Vishnu Nair | 14d218b | 2021-07-13 13:57:39 -0700 | [diff] [blame] | 9044 | layer->setIsAtRoot(true); |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 9045 | mCurrentState.layersSortedByZ.add(layer); |
| 9046 | } else if (parent == nullptr) { |
| 9047 | layer->onRemovedFromCurrentState(); |
| 9048 | } else if (parent->isRemovedFromCurrentState()) { |
| 9049 | parent->addChild(layer); |
| 9050 | layer->onRemovedFromCurrentState(); |
| 9051 | } else { |
| 9052 | parent->addChild(layer); |
| 9053 | } |
| 9054 | |
Vishnu Nair | 73908d1 | 2022-10-24 21:46:42 -0700 | [diff] [blame] | 9055 | ui::LayerStack layerStack = layer->getLayerStack(LayerVector::StateSet::Current); |
Vishnu Nair | afd527a | 2022-10-18 08:56:10 -0700 | [diff] [blame] | 9056 | sp<const DisplayDevice> hintDisplay; |
| 9057 | // Find the display that includes the layer. |
| 9058 | for (const auto& [token, display] : mDisplays) { |
| 9059 | if (display->getLayerStack() == layerStack) { |
| 9060 | hintDisplay = display; |
| 9061 | break; |
| 9062 | } |
| 9063 | } |
| 9064 | |
| 9065 | if (hintDisplay) { |
| 9066 | layer->updateTransformHint(hintDisplay->getTransformHint()); |
| 9067 | } |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 9068 | } |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 9069 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 9070 | void SurfaceFlinger::sample() { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 9071 | if (!mLumaSampling || !mRegionSamplingThread) { |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 9072 | return; |
| 9073 | } |
| 9074 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 9075 | const auto scheduledFrameResultOpt = mScheduler->getScheduledFrameResult(); |
| 9076 | const auto scheduleFrameTimeOpt = scheduledFrameResultOpt |
| 9077 | ? std::optional{scheduledFrameResultOpt->callbackTime} |
| 9078 | : std::nullopt; |
| 9079 | mRegionSamplingThread->onCompositionComplete(scheduleFrameTimeOpt); |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 9080 | } |
| 9081 | |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 9082 | void SurfaceFlinger::onActiveDisplaySizeChanged(const DisplayDevice& activeDisplay) { |
| 9083 | mScheduler->onActiveDisplayAreaChanged(activeDisplay.getWidth() * activeDisplay.getHeight()); |
| 9084 | getRenderEngine().onActiveDisplaySizeChanged(activeDisplay.getSize()); |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 9085 | } |
| 9086 | |
Dominik Laskowski | 3de6727 | 2023-08-09 13:15:31 -0400 | [diff] [blame] | 9087 | sp<DisplayDevice> SurfaceFlinger::getActivatableDisplay() const { |
| 9088 | if (mPhysicalDisplays.size() == 1) return nullptr; |
| 9089 | |
| 9090 | // TODO(b/255635821): Choose the pacesetter display, considering both internal and external |
| 9091 | // displays. For now, pick the other internal display, assuming a dual-display foldable. |
| 9092 | return findDisplay([this](const DisplayDevice& display) REQUIRES(mStateLock) { |
| 9093 | const auto idOpt = PhysicalDisplayId::tryCast(display.getId()); |
| 9094 | return idOpt && *idOpt != mActiveDisplayId && display.isPoweredOn() && |
| 9095 | mPhysicalDisplays.get(*idOpt) |
| 9096 | .transform(&PhysicalDisplay::isInternal) |
| 9097 | .value_or(false); |
| 9098 | }); |
| 9099 | } |
| 9100 | |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 9101 | void SurfaceFlinger::onActiveDisplayChangedLocked(const DisplayDevice* inactiveDisplayPtr, |
| 9102 | const DisplayDevice& activeDisplay) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 9103 | SFTRACE_CALL(); |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 9104 | |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 9105 | if (inactiveDisplayPtr) { |
| 9106 | inactiveDisplayPtr->getCompositionDisplay()->setLayerCachingTexturePoolEnabled(false); |
Ady Abraham | db036a8 | 2021-07-16 14:18:34 -0700 | [diff] [blame] | 9107 | } |
| 9108 | |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 9109 | mActiveDisplayId = activeDisplay.getPhysicalId(); |
| 9110 | activeDisplay.getCompositionDisplay()->setLayerCachingTexturePoolEnabled(true); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 9111 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 9112 | mScheduler->resetPhaseConfiguration(mDisplayModeController.getActiveMode(mActiveDisplayId).fps); |
Dominik Laskowski | 14956dc | 2023-02-22 13:43:57 -0500 | [diff] [blame] | 9113 | |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 9114 | // TODO(b/255635711): Check for pending mode changes on other displays. |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 9115 | mScheduler->setModeChangePending(false); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 9116 | |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 9117 | mScheduler->setPacesetterDisplay(mActiveDisplayId); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 9118 | |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 9119 | onActiveDisplaySizeChanged(activeDisplay); |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 9120 | mActiveDisplayTransformHint = activeDisplay.getTransformHint(); |
Leon Scroggins III | 85d4b22 | 2023-05-09 13:58:18 -0400 | [diff] [blame] | 9121 | sActiveDisplayRotationFlags = ui::Transform::toRotationFlags(activeDisplay.getOrientation()); |
Ady Abraham | 85d692d | 2021-09-28 14:08:54 -0700 | [diff] [blame] | 9122 | |
Dominik Laskowski | 1746afd | 2024-04-10 19:21:37 -0400 | [diff] [blame] | 9123 | // Whether or not the policy of the new active/pacesetter display changed while it was inactive |
| 9124 | // (in which case its preferred mode has already been propagated to HWC via setDesiredMode), the |
| 9125 | // Scheduler's cachedModeChangedParams must be initialized to the newly active mode, and the |
| 9126 | // kernel idle timer of the newly active display must be toggled. |
| 9127 | applyRefreshRateSelectorPolicy(mActiveDisplayId, activeDisplay.refreshRateSelector()); |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 9128 | } |
| 9129 | |
Patrick Williams | acd2258 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 9130 | status_t SurfaceFlinger::addWindowInfosListener(const sp<IWindowInfosListener>& windowInfosListener, |
| 9131 | gui::WindowInfosListenerInfo* outInfo) { |
| 9132 | mWindowInfosListenerInvoker->addWindowInfosListener(windowInfosListener, outInfo); |
Patrick Williams | af06b79 | 2023-02-16 17:46:22 -0600 | [diff] [blame] | 9133 | setTransactionFlags(eInputInfoUpdateNeeded); |
chaviw | 60c9d3e | 2021-06-04 12:52:17 -0500 | [diff] [blame] | 9134 | return NO_ERROR; |
| 9135 | } |
| 9136 | |
| 9137 | status_t SurfaceFlinger::removeWindowInfosListener( |
| 9138 | const sp<IWindowInfosListener>& windowInfosListener) const { |
| 9139 | mWindowInfosListenerInvoker->removeWindowInfosListener(windowInfosListener); |
| 9140 | return NO_ERROR; |
| 9141 | } |
| 9142 | |
Patrick Williams | 090ad06 | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 9143 | status_t SurfaceFlinger::getStalledTransactionInfo( |
| 9144 | int pid, std::optional<TransactionHandler::StalledTransactionInfo>& result) { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 9145 | // Used to add a stalled transaction which uses an internal lock. |
| 9146 | ftl::FakeGuard guard(kMainThreadContext); |
Patrick Williams | 090ad06 | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 9147 | result = mTransactionHandler.getStalledTransactionInfo(pid); |
| 9148 | return NO_ERROR; |
| 9149 | } |
| 9150 | |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 9151 | void SurfaceFlinger::updateHdcpLevels(hal::HWDisplayId hwcDisplayId, int32_t connectedLevel, |
| 9152 | int32_t maxLevel) { |
| 9153 | if (!FlagManager::getInstance().connected_display()) { |
| 9154 | return; |
| 9155 | } |
| 9156 | |
| 9157 | Mutex::Autolock lock(mStateLock); |
| 9158 | |
| 9159 | const auto idOpt = getHwComposer().toPhysicalDisplayId(hwcDisplayId); |
| 9160 | if (!idOpt) { |
| 9161 | ALOGE("No display found for HDCP level changed event: connected=%d, max=%d for " |
| 9162 | "display=%" PRIu64, |
| 9163 | connectedLevel, maxLevel, hwcDisplayId); |
| 9164 | return; |
| 9165 | } |
| 9166 | |
| 9167 | const bool isInternalDisplay = |
| 9168 | mPhysicalDisplays.get(*idOpt).transform(&PhysicalDisplay::isInternal).value_or(false); |
| 9169 | if (isInternalDisplay) { |
| 9170 | ALOGW("Unexpected HDCP level changed for internal display: connected=%d, max=%d for " |
| 9171 | "display=%" PRIu64, |
| 9172 | connectedLevel, maxLevel, hwcDisplayId); |
| 9173 | return; |
| 9174 | } |
| 9175 | |
| 9176 | static_cast<void>(mScheduler->schedule([this, displayId = *idOpt, connectedLevel, maxLevel]() { |
| 9177 | if (const auto display = FTL_FAKE_GUARD(mStateLock, getDisplayDeviceLocked(displayId))) { |
| 9178 | Mutex::Autolock lock(mStateLock); |
| 9179 | display->setSecure(connectedLevel >= 2 /* HDCP_V1 */); |
| 9180 | } |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 9181 | mScheduler->onHdcpLevelsChanged(scheduler::Cycle::Render, displayId, connectedLevel, |
| 9182 | maxLevel); |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 9183 | })); |
| 9184 | } |
| 9185 | |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 9186 | std::shared_ptr<renderengine::ExternalTexture> SurfaceFlinger::getExternalTextureFromBufferData( |
Patrick Williams | f1e5df1 | 2022-10-17 21:37:42 +0000 | [diff] [blame] | 9187 | BufferData& bufferData, const char* layerName, uint64_t transactionId) { |
| 9188 | if (bufferData.buffer && |
| 9189 | exceedsMaxRenderTargetSize(bufferData.buffer->getWidth(), bufferData.buffer->getHeight())) { |
Patrick Williams | ae9ab53 | 2022-11-14 18:41:38 +0000 | [diff] [blame] | 9190 | std::string errorMessage = |
| 9191 | base::StringPrintf("Attempted to create an ExternalTexture with size (%u, %u) for " |
| 9192 | "layer %s that exceeds render target size limit of %u.", |
| 9193 | bufferData.buffer->getWidth(), bufferData.buffer->getHeight(), |
| 9194 | layerName, static_cast<uint32_t>(mMaxRenderTargetSize)); |
| 9195 | ALOGD("%s", errorMessage.c_str()); |
| 9196 | if (bufferData.releaseBufferListener) { |
Huihong Luo | ffee3bc | 2023-01-17 16:14:35 +0000 | [diff] [blame] | 9197 | bufferData.releaseBufferListener->onTransactionQueueStalled( |
| 9198 | String8(errorMessage.c_str())); |
Patrick Williams | ae9ab53 | 2022-11-14 18:41:38 +0000 | [diff] [blame] | 9199 | } |
Patrick Williams | f1e5df1 | 2022-10-17 21:37:42 +0000 | [diff] [blame] | 9200 | return nullptr; |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 9201 | } |
Patrick Williams | f1e5df1 | 2022-10-17 21:37:42 +0000 | [diff] [blame] | 9202 | |
| 9203 | bool cachedBufferChanged = |
| 9204 | bufferData.flags.test(BufferData::BufferDataChange::cachedBufferChanged); |
| 9205 | if (cachedBufferChanged && bufferData.buffer) { |
| 9206 | auto result = ClientCache::getInstance().add(bufferData.cachedBuffer, bufferData.buffer); |
| 9207 | if (result.ok()) { |
| 9208 | return result.value(); |
| 9209 | } |
| 9210 | |
| 9211 | if (result.error() == ClientCache::AddError::CacheFull) { |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 9212 | ALOGE("Attempted to create an ExternalTexture for layer %s but CacheFull", layerName); |
| 9213 | |
| 9214 | if (bufferData.releaseBufferListener) { |
| 9215 | bufferData.releaseBufferListener->onTransactionQueueStalled( |
Huihong Luo | ffee3bc | 2023-01-17 16:14:35 +0000 | [diff] [blame] | 9216 | String8("Buffer processing hung due to full buffer cache")); |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 9217 | } |
Patrick Williams | f1e5df1 | 2022-10-17 21:37:42 +0000 | [diff] [blame] | 9218 | } |
| 9219 | |
| 9220 | return nullptr; |
| 9221 | } |
| 9222 | |
| 9223 | if (cachedBufferChanged) { |
| 9224 | return ClientCache::getInstance().get(bufferData.cachedBuffer); |
| 9225 | } |
| 9226 | |
| 9227 | if (bufferData.buffer) { |
| 9228 | return std::make_shared< |
| 9229 | renderengine::impl::ExternalTexture>(bufferData.buffer, getRenderEngine(), |
| 9230 | renderengine::impl::ExternalTexture::Usage:: |
| 9231 | READABLE); |
| 9232 | } |
| 9233 | |
| 9234 | return nullptr; |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 9235 | } |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 9236 | |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 9237 | bool SurfaceFlinger::commitMirrorDisplays(VsyncId vsyncId) { |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 9238 | std::vector<MirrorDisplayState> mirrorDisplays; |
| 9239 | { |
| 9240 | std::scoped_lock<std::mutex> lock(mMirrorDisplayLock); |
| 9241 | mirrorDisplays = std::move(mMirrorDisplays); |
| 9242 | mMirrorDisplays.clear(); |
| 9243 | if (mirrorDisplays.size() == 0) { |
| 9244 | return false; |
| 9245 | } |
| 9246 | } |
| 9247 | |
| 9248 | sp<IBinder> unused; |
| 9249 | for (const auto& mirrorDisplay : mirrorDisplays) { |
| 9250 | // Set mirror layer's default layer stack to -1 so it doesn't end up rendered on a display |
| 9251 | // accidentally. |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 9252 | sp<Layer> rootMirrorLayer = LayerHandle::getLayer(mirrorDisplay.rootHandle); |
Qi Jing | b8e4c13 | 2023-08-25 23:01:47 +0000 | [diff] [blame] | 9253 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(rootMirrorLayer); |
| 9254 | bool ret = rootMirrorLayer->setLayerStack(ui::LayerStack::fromValue(-1)); |
| 9255 | if (idx >= 0 && ret) { |
| 9256 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 9257 | mCurrentState.layersSortedByZ.add(rootMirrorLayer); |
| 9258 | } |
| 9259 | |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 9260 | for (const auto& layer : mDrawingState.layersSortedByZ) { |
| 9261 | if (layer->getLayerStack() != mirrorDisplay.layerStack || |
| 9262 | layer->isInternalDisplayOverlay()) { |
| 9263 | continue; |
| 9264 | } |
| 9265 | |
| 9266 | LayerCreationArgs mirrorArgs(this, mirrorDisplay.client, "MirrorLayerParent", |
| 9267 | ISurfaceComposerClient::eNoColorFill, |
| 9268 | gui::LayerMetadata()); |
| 9269 | sp<Layer> childMirror; |
Ying Wei | 2cf827f | 2023-03-22 22:25:28 +0000 | [diff] [blame] | 9270 | { |
| 9271 | Mutex::Autolock lock(mStateLock); |
| 9272 | createEffectLayer(mirrorArgs, &unused, &childMirror); |
Josh Gao | 194ff39 | 2022-09-08 16:19:29 -0700 | [diff] [blame] | 9273 | MUTEX_ALIAS(mStateLock, childMirror->mFlinger->mStateLock); |
Melody Hsu | 12ecd08 | 2024-04-17 19:03:13 +0000 | [diff] [blame] | 9274 | childMirror->setClonedChild(layer->createClone()); |
Ying Wei | 2cf827f | 2023-03-22 22:25:28 +0000 | [diff] [blame] | 9275 | childMirror->reparent(mirrorDisplay.rootHandle); |
| 9276 | } |
| 9277 | // lock on mStateLock needs to be released before binder handle gets destroyed |
| 9278 | unused.clear(); |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 9279 | } |
| 9280 | } |
| 9281 | return true; |
| 9282 | } |
| 9283 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9284 | bool SurfaceFlinger::commitCreatedLayers(VsyncId vsyncId, |
| 9285 | std::vector<LayerCreatedState>& createdLayers) { |
| 9286 | if (createdLayers.size() == 0) { |
| 9287 | return false; |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 9288 | } |
| 9289 | |
| 9290 | Mutex::Autolock _l(mStateLock); |
| 9291 | for (const auto& createdLayer : createdLayers) { |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 9292 | handleLayerCreatedLocked(createdLayer, vsyncId); |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 9293 | } |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 9294 | mLayersAdded = true; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9295 | return mLayersAdded; |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 9296 | } |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 9297 | |
Vishnu Nair | 0a4fb00 | 2022-08-08 02:40:42 +0000 | [diff] [blame] | 9298 | void SurfaceFlinger::updateLayerMetadataSnapshot() { |
| 9299 | LayerMetadata parentMetadata; |
| 9300 | for (const auto& layer : mDrawingState.layersSortedByZ) { |
| 9301 | layer->updateMetadataSnapshot(parentMetadata); |
| 9302 | } |
| 9303 | |
| 9304 | std::unordered_set<Layer*> visited; |
| 9305 | mDrawingState.traverse([&visited](Layer* layer) { |
| 9306 | if (visited.find(layer) != visited.end()) { |
| 9307 | return; |
| 9308 | } |
| 9309 | |
| 9310 | // If the layer isRelativeOf, then either it's relative metadata will be set |
| 9311 | // recursively when updateRelativeMetadataSnapshot is called on its relative parent or |
| 9312 | // it's relative parent has been deleted. Clear the layer's relativeLayerMetadata to ensure |
| 9313 | // that layers with deleted relative parents don't hold stale relativeLayerMetadata. |
| 9314 | if (layer->getDrawingState().isRelativeOf) { |
| 9315 | layer->editLayerSnapshot()->relativeLayerMetadata = {}; |
| 9316 | return; |
| 9317 | } |
| 9318 | |
| 9319 | layer->updateRelativeMetadataSnapshot({}, visited); |
| 9320 | }); |
| 9321 | } |
| 9322 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9323 | void SurfaceFlinger::moveSnapshotsFromCompositionArgs( |
| 9324 | compositionengine::CompositionRefreshArgs& refreshArgs, |
Dominik Laskowski | 16a345b | 2023-04-27 17:07:33 -0400 | [diff] [blame] | 9325 | const std::vector<std::pair<Layer*, LayerFE*>>& layers) { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 9326 | if (mLayerLifecycleManagerEnabled) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9327 | std::vector<std::unique_ptr<frontend::LayerSnapshot>>& snapshots = |
| 9328 | mLayerSnapshotBuilder.getSnapshots(); |
| 9329 | for (auto [_, layerFE] : layers) { |
| 9330 | auto i = layerFE->mSnapshot->globalZ; |
| 9331 | snapshots[i] = std::move(layerFE->mSnapshot); |
| 9332 | } |
| 9333 | } |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 9334 | if (!mLayerLifecycleManagerEnabled) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9335 | for (auto [layer, layerFE] : layers) { |
| 9336 | layer->updateLayerSnapshot(std::move(layerFE->mSnapshot)); |
| 9337 | } |
| 9338 | } |
| 9339 | } |
| 9340 | |
| 9341 | std::vector<std::pair<Layer*, LayerFE*>> SurfaceFlinger::moveSnapshotsToCompositionArgs( |
Dominik Laskowski | 16a345b | 2023-04-27 17:07:33 -0400 | [diff] [blame] | 9342 | compositionengine::CompositionRefreshArgs& refreshArgs, bool cursorOnly) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9343 | std::vector<std::pair<Layer*, LayerFE*>> layers; |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 9344 | if (mLayerLifecycleManagerEnabled) { |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 9345 | nsecs_t currentTime = systemTime(); |
Nergi Rahardi | 0dfc096 | 2024-05-23 06:57:36 +0000 | [diff] [blame] | 9346 | const bool needsMetadata = mCompositionEngine->getFeatureFlags().test( |
| 9347 | compositionengine::Feature::kSnapshotLayerMetadata); |
| 9348 | mLayerSnapshotBuilder.forEachSnapshot( |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 9349 | [&](std::unique_ptr<frontend::LayerSnapshot>& snapshot) FTL_FAKE_GUARD( |
| 9350 | kMainThreadContext) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9351 | if (cursorOnly && |
| 9352 | snapshot->compositionType != |
| 9353 | aidl::android::hardware::graphics::composer3::Composition::CURSOR) { |
| 9354 | return; |
| 9355 | } |
| 9356 | |
| 9357 | if (!snapshot->hasSomethingToDraw()) { |
| 9358 | return; |
| 9359 | } |
| 9360 | |
| 9361 | auto it = mLegacyLayers.find(snapshot->sequence); |
Vishnu Nair | 5943bf6 | 2023-12-08 22:04:20 +0000 | [diff] [blame] | 9362 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 9363 | "Couldnt find layer object for %s", |
| 9364 | snapshot->getDebugString().c_str()); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9365 | auto& legacyLayer = it->second; |
| 9366 | sp<LayerFE> layerFE = legacyLayer->getCompositionEngineLayerFE(snapshot->path); |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 9367 | snapshot->fps = getLayerFramerate(currentTime, snapshot->sequence); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9368 | layerFE->mSnapshot = std::move(snapshot); |
| 9369 | refreshArgs.layers.push_back(layerFE); |
| 9370 | layers.emplace_back(legacyLayer.get(), layerFE.get()); |
Nergi Rahardi | 0dfc096 | 2024-05-23 06:57:36 +0000 | [diff] [blame] | 9371 | }, |
| 9372 | [needsMetadata](const frontend::LayerSnapshot& snapshot) { |
| 9373 | return snapshot.isVisible || |
| 9374 | (needsMetadata && |
| 9375 | snapshot.changes.test( |
| 9376 | frontend::RequestedLayerState::Changes::Metadata)); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9377 | }); |
| 9378 | } |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 9379 | if (!mLayerLifecycleManagerEnabled) { |
Vishnu Nair | 802ac4f | 2023-05-10 13:55:22 -0700 | [diff] [blame] | 9380 | auto moveSnapshots = [&layers, &refreshArgs, cursorOnly](Layer* layer) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9381 | if (const auto& layerFE = layer->getCompositionEngineLayerFE()) { |
| 9382 | if (cursorOnly && |
| 9383 | layer->getLayerSnapshot()->compositionType != |
| 9384 | aidl::android::hardware::graphics::composer3::Composition::CURSOR) |
| 9385 | return; |
| 9386 | layer->updateSnapshot(refreshArgs.updatingGeometryThisFrame); |
| 9387 | layerFE->mSnapshot = layer->stealLayerSnapshot(); |
| 9388 | refreshArgs.layers.push_back(layerFE); |
| 9389 | layers.emplace_back(layer, layerFE.get()); |
| 9390 | } |
Vishnu Nair | 802ac4f | 2023-05-10 13:55:22 -0700 | [diff] [blame] | 9391 | }; |
| 9392 | |
| 9393 | if (cursorOnly || !mVisibleRegionsDirty) { |
| 9394 | // for hot path avoid traversals by walking though the previous composition list |
| 9395 | for (sp<Layer> layer : mPreviouslyComposedLayers) { |
| 9396 | moveSnapshots(layer.get()); |
| 9397 | } |
| 9398 | } else { |
| 9399 | mPreviouslyComposedLayers.clear(); |
| 9400 | mDrawingState.traverseInZOrder( |
| 9401 | [&moveSnapshots](Layer* layer) { moveSnapshots(layer); }); |
| 9402 | mPreviouslyComposedLayers.reserve(layers.size()); |
| 9403 | for (auto [layer, _] : layers) { |
| 9404 | mPreviouslyComposedLayers.push_back(sp<Layer>::fromExisting(layer)); |
| 9405 | } |
| 9406 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9407 | } |
| 9408 | |
| 9409 | return layers; |
| 9410 | } |
| 9411 | |
| 9412 | std::function<std::vector<std::pair<Layer*, sp<LayerFE>>>()> |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 9413 | SurfaceFlinger::getLayerSnapshotsForScreenshots( |
| 9414 | std::optional<ui::LayerStack> layerStack, uint32_t uid, |
| 9415 | std::function<bool(const frontend::LayerSnapshot&, bool& outStopTraversal)> |
| 9416 | snapshotFilterFn) { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 9417 | return [&, layerStack, uid]() FTL_FAKE_GUARD(kMainThreadContext) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9418 | std::vector<std::pair<Layer*, sp<LayerFE>>> layers; |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 9419 | bool stopTraversal = false; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 9420 | mLayerSnapshotBuilder.forEachVisibleSnapshot( |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 9421 | [&](std::unique_ptr<frontend::LayerSnapshot>& snapshot) FTL_FAKE_GUARD( |
| 9422 | kMainThreadContext) { |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 9423 | if (stopTraversal) { |
| 9424 | return; |
| 9425 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 9426 | if (layerStack && snapshot->outputFilter.layerStack != *layerStack) { |
| 9427 | return; |
| 9428 | } |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 9429 | if (uid != CaptureArgs::UNSET_UID && snapshot->uid != gui::Uid(uid)) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 9430 | return; |
| 9431 | } |
| 9432 | if (!snapshot->hasSomethingToDraw()) { |
| 9433 | return; |
| 9434 | } |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 9435 | if (snapshotFilterFn && !snapshotFilterFn(*snapshot, stopTraversal)) { |
| 9436 | return; |
| 9437 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9438 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 9439 | auto it = mLegacyLayers.find(snapshot->sequence); |
Vishnu Nair | 5943bf6 | 2023-12-08 22:04:20 +0000 | [diff] [blame] | 9440 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 9441 | "Couldnt find layer object for %s", |
| 9442 | snapshot->getDebugString().c_str()); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 9443 | Layer* legacyLayer = (it == mLegacyLayers.end()) ? nullptr : it->second.get(); |
Lloyd Pique | 70ddc67 | 2024-04-30 18:20:40 -0700 | [diff] [blame] | 9444 | sp<LayerFE> layerFE = getFactory().createLayerFE(snapshot->name, legacyLayer); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 9445 | layerFE->mSnapshot = std::make_unique<frontend::LayerSnapshot>(*snapshot); |
| 9446 | layers.emplace_back(legacyLayer, std::move(layerFE)); |
| 9447 | }); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9448 | |
| 9449 | return layers; |
| 9450 | }; |
| 9451 | } |
| 9452 | |
| 9453 | std::function<std::vector<std::pair<Layer*, sp<LayerFE>>>()> |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 9454 | SurfaceFlinger::getLayerSnapshotsForScreenshots(std::optional<ui::LayerStack> layerStack, |
| 9455 | uint32_t uid, |
| 9456 | std::unordered_set<uint32_t> excludeLayerIds) { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 9457 | return [&, layerStack, uid, |
| 9458 | excludeLayerIds = std::move(excludeLayerIds)]() FTL_FAKE_GUARD(kMainThreadContext) { |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 9459 | if (excludeLayerIds.empty()) { |
| 9460 | auto getLayerSnapshotsFn = |
| 9461 | getLayerSnapshotsForScreenshots(layerStack, uid, /*snapshotFilterFn=*/nullptr); |
| 9462 | std::vector<std::pair<Layer*, sp<LayerFE>>> layers = getLayerSnapshotsFn(); |
| 9463 | return layers; |
| 9464 | } |
| 9465 | |
| 9466 | frontend::LayerSnapshotBuilder::Args |
| 9467 | args{.root = mLayerHierarchyBuilder.getHierarchy(), |
| 9468 | .layerLifecycleManager = mLayerLifecycleManager, |
| 9469 | .forceUpdate = frontend::LayerSnapshotBuilder::ForceUpdateFlags::HIERARCHY, |
| 9470 | .displays = mFrontEndDisplayInfos, |
| 9471 | .displayChanges = true, |
| 9472 | .globalShadowSettings = mDrawingState.globalShadowSettings, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 9473 | .supportsBlur = mSupportsBlur, |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 9474 | .forceFullDamage = mForceFullDamage, |
| 9475 | .excludeLayerIds = std::move(excludeLayerIds), |
| 9476 | .supportedLayerGenericMetadata = |
| 9477 | getHwComposer().getSupportedLayerGenericMetadata(), |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 9478 | .genericLayerMetadataKeyMap = getGenericLayerMetadataKeyMap(), |
| 9479 | .skipRoundCornersWhenProtected = |
| 9480 | !getRenderEngine().supportsProtectedContent()}; |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 9481 | mLayerSnapshotBuilder.update(args); |
| 9482 | |
| 9483 | auto getLayerSnapshotsFn = |
| 9484 | getLayerSnapshotsForScreenshots(layerStack, uid, /*snapshotFilterFn=*/nullptr); |
| 9485 | std::vector<std::pair<Layer*, sp<LayerFE>>> layers = getLayerSnapshotsFn(); |
| 9486 | |
| 9487 | args.excludeLayerIds.clear(); |
| 9488 | mLayerSnapshotBuilder.update(args); |
| 9489 | |
| 9490 | return layers; |
| 9491 | }; |
| 9492 | } |
| 9493 | |
| 9494 | std::function<std::vector<std::pair<Layer*, sp<LayerFE>>>()> |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9495 | SurfaceFlinger::getLayerSnapshotsForScreenshots(uint32_t rootLayerId, uint32_t uid, |
| 9496 | std::unordered_set<uint32_t> excludeLayerIds, |
Vishnu Nair | 36d5f8e | 2023-03-19 13:31:35 -0700 | [diff] [blame] | 9497 | bool childrenOnly, |
| 9498 | const std::optional<FloatRect>& parentCrop) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 9499 | return [&, rootLayerId, uid, excludeLayerIds = std::move(excludeLayerIds), childrenOnly, |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 9500 | parentCrop]() FTL_FAKE_GUARD(kMainThreadContext) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 9501 | auto root = mLayerHierarchyBuilder.getPartialHierarchy(rootLayerId, childrenOnly); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9502 | frontend::LayerSnapshotBuilder::Args |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 9503 | args{.root = root, |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9504 | .layerLifecycleManager = mLayerLifecycleManager, |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 9505 | .forceUpdate = frontend::LayerSnapshotBuilder::ForceUpdateFlags::HIERARCHY, |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9506 | .displays = mFrontEndDisplayInfos, |
| 9507 | .displayChanges = true, |
| 9508 | .globalShadowSettings = mDrawingState.globalShadowSettings, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 9509 | .supportsBlur = mSupportsBlur, |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9510 | .forceFullDamage = mForceFullDamage, |
Vishnu Nair | 36d5f8e | 2023-03-19 13:31:35 -0700 | [diff] [blame] | 9511 | .parentCrop = parentCrop, |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 9512 | .excludeLayerIds = std::move(excludeLayerIds), |
| 9513 | .supportedLayerGenericMetadata = |
| 9514 | getHwComposer().getSupportedLayerGenericMetadata(), |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 9515 | .genericLayerMetadataKeyMap = getGenericLayerMetadataKeyMap(), |
| 9516 | .skipRoundCornersWhenProtected = |
| 9517 | !getRenderEngine().supportsProtectedContent()}; |
Vishnu Nair | c54fdb0 | 2023-11-21 17:19:58 -0800 | [diff] [blame] | 9518 | // The layer may not exist if it was just created and a screenshot was requested immediately |
| 9519 | // after. In this case, the hierarchy will be empty so we will not render any layers. |
| 9520 | args.rootSnapshot.isSecure = mLayerLifecycleManager.getLayerFromId(rootLayerId) && |
| 9521 | mLayerLifecycleManager.isLayerSecure(rootLayerId); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9522 | mLayerSnapshotBuilder.update(args); |
| 9523 | |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 9524 | auto getLayerSnapshotsFn = |
| 9525 | getLayerSnapshotsForScreenshots({}, uid, /*snapshotFilterFn=*/nullptr); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9526 | std::vector<std::pair<Layer*, sp<LayerFE>>> layers = getLayerSnapshotsFn(); |
| 9527 | args.root = mLayerHierarchyBuilder.getHierarchy(); |
| 9528 | args.parentCrop.reset(); |
| 9529 | args.excludeLayerIds.clear(); |
| 9530 | mLayerSnapshotBuilder.update(args); |
| 9531 | return layers; |
| 9532 | }; |
| 9533 | } |
| 9534 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 9535 | frontend::Update SurfaceFlinger::flushLifecycleUpdates() { |
| 9536 | frontend::Update update; |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 9537 | SFTRACE_NAME("TransactionHandler:flushTransactions"); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9538 | // Locking: |
| 9539 | // 1. to prevent onHandleDestroyed from being called while the state lock is held, |
| 9540 | // we must keep a copy of the transactions (specifically the composer |
| 9541 | // states) around outside the scope of the lock. |
| 9542 | // 2. Transactions and created layers do not share a lock. To prevent applying |
| 9543 | // transactions with layers still in the createdLayer queue, flush the transactions |
| 9544 | // before committing the created layers. |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 9545 | mTransactionHandler.collectTransactions(); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9546 | update.transactions = mTransactionHandler.flushTransactions(); |
| 9547 | { |
| 9548 | // TODO(b/238781169) lockless queue this and keep order. |
| 9549 | std::scoped_lock<std::mutex> lock(mCreatedLayersLock); |
| 9550 | update.layerCreatedStates = std::move(mCreatedLayers); |
| 9551 | mCreatedLayers.clear(); |
| 9552 | update.newLayers = std::move(mNewLayers); |
| 9553 | mNewLayers.clear(); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 9554 | update.layerCreationArgs = std::move(mNewLayerArgs); |
| 9555 | mNewLayerArgs.clear(); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 9556 | update.destroyedHandles = std::move(mDestroyedHandles); |
| 9557 | mDestroyedHandles.clear(); |
| 9558 | } |
| 9559 | return update; |
| 9560 | } |
| 9561 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 9562 | void SurfaceFlinger::doActiveLayersTracingIfNeeded(bool isCompositionComputed, |
| 9563 | bool visibleRegionDirty, TimePoint time, |
| 9564 | VsyncId vsyncId) { |
| 9565 | if (!mLayerTracing.isActiveTracingStarted()) { |
| 9566 | return; |
| 9567 | } |
| 9568 | if (isCompositionComputed != |
| 9569 | mLayerTracing.isActiveTracingFlagSet(LayerTracing::Flag::TRACE_COMPOSITION)) { |
| 9570 | return; |
| 9571 | } |
| 9572 | if (!visibleRegionDirty && |
| 9573 | !mLayerTracing.isActiveTracingFlagSet(LayerTracing::Flag::TRACE_BUFFERS)) { |
| 9574 | return; |
| 9575 | } |
| 9576 | auto snapshot = takeLayersSnapshotProto(mLayerTracing.getActiveTracingFlags(), time, vsyncId, |
| 9577 | visibleRegionDirty); |
| 9578 | mLayerTracing.addProtoSnapshotToOstream(std::move(snapshot), LayerTracing::Mode::MODE_ACTIVE); |
| 9579 | } |
| 9580 | |
| 9581 | perfetto::protos::LayersSnapshotProto SurfaceFlinger::takeLayersSnapshotProto( |
| 9582 | uint32_t traceFlags, TimePoint time, VsyncId vsyncId, bool visibleRegionDirty) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 9583 | SFTRACE_CALL(); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 9584 | perfetto::protos::LayersSnapshotProto snapshot; |
| 9585 | snapshot.set_elapsed_realtime_nanos(time.ns()); |
| 9586 | snapshot.set_vsync_id(ftl::to_underlying(vsyncId)); |
| 9587 | snapshot.set_where(visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched"); |
| 9588 | snapshot.set_excludes_composition_state((traceFlags & LayerTracing::Flag::TRACE_COMPOSITION) == |
| 9589 | 0); |
| 9590 | |
| 9591 | auto layers = dumpDrawingStateProto(traceFlags); |
| 9592 | if (traceFlags & LayerTracing::Flag::TRACE_EXTRA) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 9593 | dumpOffscreenLayersProto(layers); |
| 9594 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 9595 | *snapshot.mutable_layers() = std::move(layers); |
| 9596 | |
| 9597 | if (traceFlags & LayerTracing::Flag::TRACE_HWC) { |
| 9598 | std::string hwcDump; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 9599 | dumpHwc(hwcDump); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 9600 | snapshot.set_hwc_blob(std::move(hwcDump)); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 9601 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 9602 | |
| 9603 | *snapshot.mutable_displays() = dumpDisplayProto(); |
| 9604 | |
| 9605 | return snapshot; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 9606 | } |
| 9607 | |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 9608 | // sfdo functions |
| 9609 | |
| 9610 | void SurfaceFlinger::sfdo_enableRefreshRateOverlay(bool active) { |
| 9611 | auto future = mScheduler->schedule( |
| 9612 | [&]() FTL_FAKE_GUARD(mStateLock) |
| 9613 | FTL_FAKE_GUARD(kMainThreadContext) { enableRefreshRateOverlay(active); }); |
| 9614 | future.wait(); |
| 9615 | } |
| 9616 | |
| 9617 | void SurfaceFlinger::sfdo_setDebugFlash(int delay) { |
| 9618 | if (delay > 0) { |
| 9619 | mDebugFlashDelay = delay; |
| 9620 | } else { |
| 9621 | mDebugFlashDelay = mDebugFlashDelay ? 0 : 1; |
| 9622 | } |
| 9623 | scheduleRepaint(); |
| 9624 | } |
| 9625 | |
| 9626 | void SurfaceFlinger::sfdo_scheduleComposite() { |
| 9627 | scheduleComposite(SurfaceFlinger::FrameHint::kActive); |
| 9628 | } |
| 9629 | |
| 9630 | void SurfaceFlinger::sfdo_scheduleCommit() { |
| 9631 | Mutex::Autolock lock(mStateLock); |
| 9632 | setTransactionFlags(eTransactionNeeded | eDisplayTransactionNeeded | eTraversalNeeded); |
| 9633 | } |
| 9634 | |
Carlos Martinez Romero | b4ea9e6 | 2023-10-25 21:48:56 +0000 | [diff] [blame] | 9635 | void SurfaceFlinger::sfdo_forceClientComposition(bool enabled) { |
| 9636 | mDebugDisableHWC = enabled; |
| 9637 | scheduleRepaint(); |
| 9638 | } |
| 9639 | |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 9640 | // gui::ISurfaceComposer |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9641 | |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 9642 | binder::Status SurfaceComposerAIDL::bootFinished() { |
| 9643 | status_t status = checkAccessPermission(); |
| 9644 | if (status != OK) { |
| 9645 | return binderStatusFromStatusT(status); |
| 9646 | } |
| 9647 | mFlinger->bootFinished(); |
| 9648 | return binder::Status::ok(); |
| 9649 | } |
| 9650 | |
| 9651 | binder::Status SurfaceComposerAIDL::createDisplayEventConnection( |
| 9652 | VsyncSource vsyncSource, EventRegistration eventRegistration, |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 9653 | const sp<IBinder>& layerHandle, sp<IDisplayEventConnection>* outConnection) { |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 9654 | sp<IDisplayEventConnection> conn = |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 9655 | mFlinger->createDisplayEventConnection(vsyncSource, eventRegistration, layerHandle); |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 9656 | if (conn == nullptr) { |
| 9657 | *outConnection = nullptr; |
| 9658 | return binderStatusFromStatusT(BAD_VALUE); |
| 9659 | } else { |
| 9660 | *outConnection = conn; |
| 9661 | return binder::Status::ok(); |
| 9662 | } |
| 9663 | } |
| 9664 | |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 9665 | binder::Status SurfaceComposerAIDL::createConnection(sp<gui::ISurfaceComposerClient>* outClient) { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 9666 | const sp<Client> client = sp<Client>::make(mFlinger); |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 9667 | if (client->initCheck() == NO_ERROR) { |
| 9668 | *outClient = client; |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 9669 | if (FlagManager::getInstance().misc1()) { |
Ady Abraham | 07d03c4 | 2023-09-27 19:15:08 -0700 | [diff] [blame] | 9670 | const int policy = SCHED_FIFO; |
| 9671 | client->setMinSchedulerPolicy(policy, sched_get_priority_min(policy)); |
| 9672 | } |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 9673 | return binder::Status::ok(); |
| 9674 | } else { |
| 9675 | *outClient = nullptr; |
| 9676 | return binderStatusFromStatusT(BAD_VALUE); |
| 9677 | } |
| 9678 | } |
| 9679 | |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 9680 | binder::Status SurfaceComposerAIDL::createVirtualDisplay(const std::string& displayName, |
| 9681 | bool isSecure, const std::string& uniqueId, |
| 9682 | float requestedRefreshRate, |
| 9683 | sp<IBinder>* outDisplay) { |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9684 | status_t status = checkAccessPermission(); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9685 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9686 | return binderStatusFromStatusT(status); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9687 | } |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 9688 | *outDisplay = |
| 9689 | mFlinger->createVirtualDisplay(displayName, isSecure, uniqueId, requestedRefreshRate); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9690 | return binder::Status::ok(); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9691 | } |
| 9692 | |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 9693 | binder::Status SurfaceComposerAIDL::destroyVirtualDisplay(const sp<IBinder>& displayToken) { |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9694 | status_t status = checkAccessPermission(); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9695 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9696 | return binderStatusFromStatusT(status); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9697 | } |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 9698 | return binder::Status::fromStatusT(mFlinger->destroyVirtualDisplay(displayToken)); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9699 | } |
| 9700 | |
| 9701 | binder::Status SurfaceComposerAIDL::getPhysicalDisplayIds(std::vector<int64_t>* outDisplayIds) { |
| 9702 | std::vector<PhysicalDisplayId> physicalDisplayIds = mFlinger->getPhysicalDisplayIds(); |
| 9703 | std::vector<int64_t> displayIds; |
| 9704 | displayIds.reserve(physicalDisplayIds.size()); |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 9705 | for (const auto id : physicalDisplayIds) { |
| 9706 | displayIds.push_back(static_cast<int64_t>(id.value)); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9707 | } |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 9708 | *outDisplayIds = std::move(displayIds); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9709 | return binder::Status::ok(); |
| 9710 | } |
| 9711 | |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9712 | binder::Status SurfaceComposerAIDL::getPhysicalDisplayToken(int64_t displayId, |
| 9713 | sp<IBinder>* outDisplay) { |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 9714 | status_t status = checkAccessPermission(); |
| 9715 | if (status != OK) { |
| 9716 | return binderStatusFromStatusT(status); |
| 9717 | } |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9718 | const auto id = DisplayId::fromValue<PhysicalDisplayId>(static_cast<uint64_t>(displayId)); |
| 9719 | *outDisplay = mFlinger->getPhysicalDisplayToken(*id); |
| 9720 | return binder::Status::ok(); |
| 9721 | } |
| 9722 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9723 | binder::Status SurfaceComposerAIDL::setPowerMode(const sp<IBinder>& display, int mode) { |
| 9724 | status_t status = checkAccessPermission(); |
| 9725 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9726 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9727 | } |
| 9728 | mFlinger->setPowerMode(display, mode); |
| 9729 | return binder::Status::ok(); |
| 9730 | } |
| 9731 | |
Huihong Luo | 0a81aa3 | 2022-02-22 16:02:36 -0800 | [diff] [blame] | 9732 | binder::Status SurfaceComposerAIDL::getSupportedFrameTimestamps( |
| 9733 | std::vector<FrameEvent>* outSupported) { |
| 9734 | status_t status; |
| 9735 | if (!outSupported) { |
| 9736 | status = UNEXPECTED_NULL; |
| 9737 | } else { |
| 9738 | outSupported->clear(); |
| 9739 | status = mFlinger->getSupportedFrameTimestamps(outSupported); |
| 9740 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9741 | return binderStatusFromStatusT(status); |
Huihong Luo | 0a81aa3 | 2022-02-22 16:02:36 -0800 | [diff] [blame] | 9742 | } |
| 9743 | |
Huihong Luo | aa7fc2e | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9744 | binder::Status SurfaceComposerAIDL::getDisplayStats(const sp<IBinder>& display, |
| 9745 | gui::DisplayStatInfo* outStatInfo) { |
| 9746 | DisplayStatInfo statInfo; |
| 9747 | status_t status = mFlinger->getDisplayStats(display, &statInfo); |
| 9748 | if (status == NO_ERROR) { |
| 9749 | outStatInfo->vsyncTime = static_cast<long>(statInfo.vsyncTime); |
| 9750 | outStatInfo->vsyncPeriod = static_cast<long>(statInfo.vsyncPeriod); |
| 9751 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9752 | return binderStatusFromStatusT(status); |
Huihong Luo | aa7fc2e | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9753 | } |
| 9754 | |
| 9755 | binder::Status SurfaceComposerAIDL::getDisplayState(const sp<IBinder>& display, |
| 9756 | gui::DisplayState* outState) { |
| 9757 | ui::DisplayState state; |
| 9758 | status_t status = mFlinger->getDisplayState(display, &state); |
| 9759 | if (status == NO_ERROR) { |
| 9760 | outState->layerStack = state.layerStack.id; |
| 9761 | outState->orientation = static_cast<gui::Rotation>(state.orientation); |
| 9762 | outState->layerStackSpaceRect.width = state.layerStackSpaceRect.width; |
| 9763 | outState->layerStackSpaceRect.height = state.layerStackSpaceRect.height; |
| 9764 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9765 | return binderStatusFromStatusT(status); |
Huihong Luo | aa7fc2e | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9766 | } |
| 9767 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 9768 | binder::Status SurfaceComposerAIDL::getStaticDisplayInfo(int64_t displayId, |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 9769 | gui::StaticDisplayInfo* outInfo) { |
| 9770 | using Tag = gui::DeviceProductInfo::ManufactureOrModelDate::Tag; |
| 9771 | ui::StaticDisplayInfo info; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 9772 | |
| 9773 | status_t status = mFlinger->getStaticDisplayInfo(displayId, &info); |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 9774 | if (status == NO_ERROR) { |
| 9775 | // convert ui::StaticDisplayInfo to gui::StaticDisplayInfo |
| 9776 | outInfo->connectionType = static_cast<gui::DisplayConnectionType>(info.connectionType); |
| 9777 | outInfo->density = info.density; |
| 9778 | outInfo->secure = info.secure; |
| 9779 | outInfo->installOrientation = static_cast<gui::Rotation>(info.installOrientation); |
| 9780 | |
Snild Dolkow | 8d67564 | 2023-07-14 12:43:35 +0200 | [diff] [blame] | 9781 | if (const std::optional<DeviceProductInfo> dpi = info.deviceProductInfo) { |
| 9782 | gui::DeviceProductInfo dinfo; |
| 9783 | dinfo.name = std::move(dpi->name); |
| 9784 | dinfo.manufacturerPnpId = std::vector<uint8_t>(dpi->manufacturerPnpId.begin(), |
| 9785 | dpi->manufacturerPnpId.end()); |
| 9786 | dinfo.productId = dpi->productId; |
| 9787 | dinfo.relativeAddress = |
| 9788 | std::vector<uint8_t>(dpi->relativeAddress.begin(), dpi->relativeAddress.end()); |
| 9789 | if (const auto* model = |
| 9790 | std::get_if<DeviceProductInfo::ModelYear>(&dpi->manufactureOrModelDate)) { |
| 9791 | gui::DeviceProductInfo::ModelYear modelYear; |
| 9792 | modelYear.year = model->year; |
| 9793 | dinfo.manufactureOrModelDate.set<Tag::modelYear>(modelYear); |
| 9794 | } else if (const auto* manufacture = std::get_if<DeviceProductInfo::ManufactureYear>( |
| 9795 | &dpi->manufactureOrModelDate)) { |
| 9796 | gui::DeviceProductInfo::ManufactureYear date; |
| 9797 | date.modelYear.year = manufacture->year; |
| 9798 | dinfo.manufactureOrModelDate.set<Tag::manufactureYear>(date); |
| 9799 | } else if (const auto* manufacture = |
| 9800 | std::get_if<DeviceProductInfo::ManufactureWeekAndYear>( |
| 9801 | &dpi->manufactureOrModelDate)) { |
| 9802 | gui::DeviceProductInfo::ManufactureWeekAndYear date; |
| 9803 | date.manufactureYear.modelYear.year = manufacture->year; |
| 9804 | date.week = manufacture->week; |
| 9805 | dinfo.manufactureOrModelDate.set<Tag::manufactureWeekAndYear>(date); |
| 9806 | } |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 9807 | |
Snild Dolkow | 8d67564 | 2023-07-14 12:43:35 +0200 | [diff] [blame] | 9808 | outInfo->deviceProductInfo = dinfo; |
| 9809 | } |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 9810 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9811 | return binderStatusFromStatusT(status); |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 9812 | } |
| 9813 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 9814 | void SurfaceComposerAIDL::getDynamicDisplayInfoInternal(ui::DynamicDisplayInfo& info, |
| 9815 | gui::DynamicDisplayInfo*& outInfo) { |
| 9816 | // convert ui::DynamicDisplayInfo to gui::DynamicDisplayInfo |
| 9817 | outInfo->supportedDisplayModes.clear(); |
| 9818 | outInfo->supportedDisplayModes.reserve(info.supportedDisplayModes.size()); |
| 9819 | for (const auto& mode : info.supportedDisplayModes) { |
| 9820 | gui::DisplayMode outMode; |
| 9821 | outMode.id = mode.id; |
| 9822 | outMode.resolution.width = mode.resolution.width; |
| 9823 | outMode.resolution.height = mode.resolution.height; |
| 9824 | outMode.xDpi = mode.xDpi; |
| 9825 | outMode.yDpi = mode.yDpi; |
Alec Mouri | 55e3103 | 2023-10-02 20:34:18 +0000 | [diff] [blame] | 9826 | outMode.peakRefreshRate = mode.peakRefreshRate; |
Alec Mouri | d122a1c | 2023-09-30 01:36:33 +0000 | [diff] [blame] | 9827 | outMode.vsyncRate = mode.vsyncRate; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 9828 | outMode.appVsyncOffset = mode.appVsyncOffset; |
| 9829 | outMode.sfVsyncOffset = mode.sfVsyncOffset; |
| 9830 | outMode.presentationDeadline = mode.presentationDeadline; |
| 9831 | outMode.group = mode.group; |
| 9832 | std::transform(mode.supportedHdrTypes.begin(), mode.supportedHdrTypes.end(), |
| 9833 | std::back_inserter(outMode.supportedHdrTypes), |
| 9834 | [](const ui::Hdr& value) { return static_cast<int32_t>(value); }); |
| 9835 | outInfo->supportedDisplayModes.push_back(outMode); |
| 9836 | } |
| 9837 | |
| 9838 | outInfo->activeDisplayModeId = info.activeDisplayModeId; |
| 9839 | outInfo->renderFrameRate = info.renderFrameRate; |
| 9840 | |
| 9841 | outInfo->supportedColorModes.clear(); |
| 9842 | outInfo->supportedColorModes.reserve(info.supportedColorModes.size()); |
| 9843 | for (const auto& cmode : info.supportedColorModes) { |
| 9844 | outInfo->supportedColorModes.push_back(static_cast<int32_t>(cmode)); |
| 9845 | } |
| 9846 | |
| 9847 | outInfo->activeColorMode = static_cast<int32_t>(info.activeColorMode); |
| 9848 | |
| 9849 | gui::HdrCapabilities& hdrCapabilities = outInfo->hdrCapabilities; |
| 9850 | hdrCapabilities.supportedHdrTypes.clear(); |
| 9851 | hdrCapabilities.supportedHdrTypes.reserve(info.hdrCapabilities.getSupportedHdrTypes().size()); |
| 9852 | for (const auto& hdr : info.hdrCapabilities.getSupportedHdrTypes()) { |
| 9853 | hdrCapabilities.supportedHdrTypes.push_back(static_cast<int32_t>(hdr)); |
| 9854 | } |
| 9855 | hdrCapabilities.maxLuminance = info.hdrCapabilities.getDesiredMaxLuminance(); |
| 9856 | hdrCapabilities.maxAverageLuminance = info.hdrCapabilities.getDesiredMaxAverageLuminance(); |
| 9857 | hdrCapabilities.minLuminance = info.hdrCapabilities.getDesiredMinLuminance(); |
| 9858 | |
| 9859 | outInfo->autoLowLatencyModeSupported = info.autoLowLatencyModeSupported; |
| 9860 | outInfo->gameContentTypeSupported = info.gameContentTypeSupported; |
| 9861 | outInfo->preferredBootDisplayMode = info.preferredBootDisplayMode; |
| 9862 | } |
| 9863 | |
| 9864 | binder::Status SurfaceComposerAIDL::getDynamicDisplayInfoFromToken( |
| 9865 | const sp<IBinder>& display, gui::DynamicDisplayInfo* outInfo) { |
Huihong Luo | 38603fd | 2022-02-21 14:32:54 -0800 | [diff] [blame] | 9866 | ui::DynamicDisplayInfo info; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 9867 | status_t status = mFlinger->getDynamicDisplayInfoFromToken(display, &info); |
Huihong Luo | 38603fd | 2022-02-21 14:32:54 -0800 | [diff] [blame] | 9868 | if (status == NO_ERROR) { |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 9869 | getDynamicDisplayInfoInternal(info, outInfo); |
| 9870 | } |
| 9871 | return binderStatusFromStatusT(status); |
| 9872 | } |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 9873 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 9874 | binder::Status SurfaceComposerAIDL::getDynamicDisplayInfoFromId(int64_t displayId, |
| 9875 | gui::DynamicDisplayInfo* outInfo) { |
| 9876 | ui::DynamicDisplayInfo info; |
| 9877 | status_t status = mFlinger->getDynamicDisplayInfoFromId(displayId, &info); |
| 9878 | if (status == NO_ERROR) { |
| 9879 | getDynamicDisplayInfoInternal(info, outInfo); |
Huihong Luo | 38603fd | 2022-02-21 14:32:54 -0800 | [diff] [blame] | 9880 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9881 | return binderStatusFromStatusT(status); |
Huihong Luo | 38603fd | 2022-02-21 14:32:54 -0800 | [diff] [blame] | 9882 | } |
| 9883 | |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 9884 | binder::Status SurfaceComposerAIDL::getDisplayNativePrimaries(const sp<IBinder>& display, |
| 9885 | gui::DisplayPrimaries* outPrimaries) { |
| 9886 | ui::DisplayPrimaries primaries; |
| 9887 | status_t status = mFlinger->getDisplayNativePrimaries(display, primaries); |
| 9888 | if (status == NO_ERROR) { |
| 9889 | outPrimaries->red.X = primaries.red.X; |
| 9890 | outPrimaries->red.Y = primaries.red.Y; |
| 9891 | outPrimaries->red.Z = primaries.red.Z; |
| 9892 | |
| 9893 | outPrimaries->green.X = primaries.green.X; |
| 9894 | outPrimaries->green.Y = primaries.green.Y; |
| 9895 | outPrimaries->green.Z = primaries.green.Z; |
| 9896 | |
| 9897 | outPrimaries->blue.X = primaries.blue.X; |
| 9898 | outPrimaries->blue.Y = primaries.blue.Y; |
| 9899 | outPrimaries->blue.Z = primaries.blue.Z; |
| 9900 | |
| 9901 | outPrimaries->white.X = primaries.white.X; |
| 9902 | outPrimaries->white.Y = primaries.white.Y; |
| 9903 | outPrimaries->white.Z = primaries.white.Z; |
| 9904 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9905 | return binderStatusFromStatusT(status); |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 9906 | } |
| 9907 | |
| 9908 | binder::Status SurfaceComposerAIDL::setActiveColorMode(const sp<IBinder>& display, int colorMode) { |
| 9909 | status_t status = checkAccessPermission(); |
| 9910 | if (status == OK) { |
| 9911 | status = mFlinger->setActiveColorMode(display, static_cast<ui::ColorMode>(colorMode)); |
| 9912 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9913 | return binderStatusFromStatusT(status); |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 9914 | } |
| 9915 | |
| 9916 | binder::Status SurfaceComposerAIDL::setBootDisplayMode(const sp<IBinder>& display, |
| 9917 | int displayModeId) { |
| 9918 | status_t status = checkAccessPermission(); |
| 9919 | if (status == OK) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 9920 | status = mFlinger->setBootDisplayMode(display, DisplayModeId{displayModeId}); |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 9921 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9922 | return binderStatusFromStatusT(status); |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 9923 | } |
| 9924 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9925 | binder::Status SurfaceComposerAIDL::clearBootDisplayMode(const sp<IBinder>& display) { |
| 9926 | status_t status = checkAccessPermission(); |
| 9927 | if (status == OK) { |
| 9928 | status = mFlinger->clearBootDisplayMode(display); |
| 9929 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9930 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9931 | } |
| 9932 | |
Sally Qi | 0cbd08b | 2022-08-17 12:12:28 -0700 | [diff] [blame] | 9933 | binder::Status SurfaceComposerAIDL::getOverlaySupport(gui::OverlayProperties* outProperties) { |
| 9934 | status_t status = checkAccessPermission(); |
| 9935 | if (status == OK) { |
| 9936 | status = mFlinger->getOverlaySupport(outProperties); |
| 9937 | } |
| 9938 | return binderStatusFromStatusT(status); |
| 9939 | } |
| 9940 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9941 | binder::Status SurfaceComposerAIDL::getBootDisplayModeSupport(bool* outMode) { |
| 9942 | status_t status = checkAccessPermission(); |
| 9943 | if (status == OK) { |
| 9944 | status = mFlinger->getBootDisplayModeSupport(outMode); |
| 9945 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9946 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9947 | } |
| 9948 | |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 9949 | binder::Status SurfaceComposerAIDL::getHdrConversionCapabilities( |
| 9950 | std::vector<gui::HdrConversionCapability>* hdrConversionCapabilities) { |
| 9951 | status_t status = checkAccessPermission(); |
| 9952 | if (status == OK) { |
| 9953 | status = mFlinger->getHdrConversionCapabilities(hdrConversionCapabilities); |
| 9954 | } |
| 9955 | return binderStatusFromStatusT(status); |
| 9956 | } |
| 9957 | |
| 9958 | binder::Status SurfaceComposerAIDL::setHdrConversionStrategy( |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 9959 | const gui::HdrConversionStrategy& hdrConversionStrategy, |
| 9960 | int32_t* outPreferredHdrOutputType) { |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 9961 | status_t status = checkAccessPermission(); |
| 9962 | if (status == OK) { |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 9963 | status = mFlinger->setHdrConversionStrategy(hdrConversionStrategy, |
| 9964 | outPreferredHdrOutputType); |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 9965 | } |
| 9966 | return binderStatusFromStatusT(status); |
| 9967 | } |
| 9968 | |
| 9969 | binder::Status SurfaceComposerAIDL::getHdrOutputConversionSupport(bool* outMode) { |
| 9970 | status_t status = checkAccessPermission(); |
| 9971 | if (status == OK) { |
| 9972 | status = mFlinger->getHdrOutputConversionSupport(outMode); |
| 9973 | } |
| 9974 | return binderStatusFromStatusT(status); |
| 9975 | } |
| 9976 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9977 | binder::Status SurfaceComposerAIDL::setAutoLowLatencyMode(const sp<IBinder>& display, bool on) { |
| 9978 | status_t status = checkAccessPermission(); |
| 9979 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9980 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9981 | } |
| 9982 | mFlinger->setAutoLowLatencyMode(display, on); |
| 9983 | return binder::Status::ok(); |
| 9984 | } |
| 9985 | |
| 9986 | binder::Status SurfaceComposerAIDL::setGameContentType(const sp<IBinder>& display, bool on) { |
| 9987 | status_t status = checkAccessPermission(); |
| 9988 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9989 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9990 | } |
| 9991 | mFlinger->setGameContentType(display, on); |
| 9992 | return binder::Status::ok(); |
| 9993 | } |
| 9994 | |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 9995 | binder::Status SurfaceComposerAIDL::captureDisplay( |
| 9996 | const DisplayCaptureArgs& args, const sp<IScreenCaptureListener>& captureListener) { |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 9997 | mFlinger->captureDisplay(args, captureListener); |
| 9998 | return binderStatusFromStatusT(NO_ERROR); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 9999 | } |
| 10000 | |
| 10001 | binder::Status SurfaceComposerAIDL::captureDisplayById( |
John Reck | 7e4c487 | 2023-11-14 18:31:03 -0500 | [diff] [blame] | 10002 | int64_t displayId, const CaptureArgs& args, |
| 10003 | const sp<IScreenCaptureListener>& captureListener) { |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 10004 | // status_t status; |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 10005 | IPCThreadState* ipc = IPCThreadState::self(); |
| 10006 | const int uid = ipc->getCallingUid(); |
| 10007 | if (uid == AID_ROOT || uid == AID_GRAPHICS || uid == AID_SYSTEM || uid == AID_SHELL) { |
| 10008 | std::optional<DisplayId> id = DisplayId::fromValue(static_cast<uint64_t>(displayId)); |
John Reck | 7e4c487 | 2023-11-14 18:31:03 -0500 | [diff] [blame] | 10009 | mFlinger->captureDisplay(*id, args, captureListener); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 10010 | } else { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 10011 | ALOGD("Permission denied to captureDisplayById"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 10012 | invokeScreenCaptureError(PERMISSION_DENIED, captureListener); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 10013 | } |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 10014 | return binderStatusFromStatusT(NO_ERROR); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 10015 | } |
| 10016 | |
Chavi Weingarten | f6fb445 | 2024-01-23 21:10:30 +0000 | [diff] [blame] | 10017 | binder::Status SurfaceComposerAIDL::captureLayersSync(const LayerCaptureArgs& args, |
| 10018 | ScreenCaptureResults* outResults) { |
| 10019 | *outResults = mFlinger->captureLayersSync(args); |
| 10020 | return binderStatusFromStatusT(NO_ERROR); |
| 10021 | } |
| 10022 | |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 10023 | binder::Status SurfaceComposerAIDL::captureLayers( |
| 10024 | const LayerCaptureArgs& args, const sp<IScreenCaptureListener>& captureListener) { |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 10025 | mFlinger->captureLayers(args, captureListener); |
| 10026 | return binderStatusFromStatusT(NO_ERROR); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 10027 | } |
| 10028 | |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 10029 | binder::Status SurfaceComposerAIDL::overrideHdrTypes(const sp<IBinder>& display, |
| 10030 | const std::vector<int32_t>& hdrTypes) { |
| 10031 | // overrideHdrTypes is used by CTS tests, which acquire the necessary |
| 10032 | // permission dynamically. Don't use the permission cache for this check. |
| 10033 | status_t status = checkAccessPermission(false); |
| 10034 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10035 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 10036 | } |
| 10037 | |
| 10038 | std::vector<ui::Hdr> hdrTypesVector; |
| 10039 | for (int32_t i : hdrTypes) { |
| 10040 | hdrTypesVector.push_back(static_cast<ui::Hdr>(i)); |
| 10041 | } |
| 10042 | status = mFlinger->overrideHdrTypes(display, hdrTypesVector); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10043 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 10044 | } |
| 10045 | |
| 10046 | binder::Status SurfaceComposerAIDL::onPullAtom(int32_t atomId, gui::PullAtomData* outPullData) { |
| 10047 | status_t status; |
| 10048 | const int uid = IPCThreadState::self()->getCallingUid(); |
| 10049 | if (uid != AID_SYSTEM) { |
| 10050 | status = PERMISSION_DENIED; |
| 10051 | } else { |
| 10052 | status = mFlinger->onPullAtom(atomId, &outPullData->data, &outPullData->success); |
| 10053 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10054 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 10055 | } |
| 10056 | |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 10057 | binder::Status SurfaceComposerAIDL::getCompositionPreference(gui::CompositionPreference* outPref) { |
| 10058 | ui::Dataspace dataspace; |
| 10059 | ui::PixelFormat pixelFormat; |
| 10060 | ui::Dataspace wideColorGamutDataspace; |
| 10061 | ui::PixelFormat wideColorGamutPixelFormat; |
| 10062 | status_t status = |
| 10063 | mFlinger->getCompositionPreference(&dataspace, &pixelFormat, &wideColorGamutDataspace, |
| 10064 | &wideColorGamutPixelFormat); |
| 10065 | if (status == NO_ERROR) { |
| 10066 | outPref->defaultDataspace = static_cast<int32_t>(dataspace); |
| 10067 | outPref->defaultPixelFormat = static_cast<int32_t>(pixelFormat); |
| 10068 | outPref->wideColorGamutDataspace = static_cast<int32_t>(wideColorGamutDataspace); |
| 10069 | outPref->wideColorGamutPixelFormat = static_cast<int32_t>(wideColorGamutPixelFormat); |
| 10070 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10071 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 10072 | } |
| 10073 | |
| 10074 | binder::Status SurfaceComposerAIDL::getDisplayedContentSamplingAttributes( |
| 10075 | const sp<IBinder>& display, gui::ContentSamplingAttributes* outAttrs) { |
| 10076 | status_t status = checkAccessPermission(); |
| 10077 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10078 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 10079 | } |
| 10080 | |
| 10081 | ui::PixelFormat format; |
| 10082 | ui::Dataspace dataspace; |
| 10083 | uint8_t componentMask; |
| 10084 | status = mFlinger->getDisplayedContentSamplingAttributes(display, &format, &dataspace, |
| 10085 | &componentMask); |
| 10086 | if (status == NO_ERROR) { |
| 10087 | outAttrs->format = static_cast<int32_t>(format); |
| 10088 | outAttrs->dataspace = static_cast<int32_t>(dataspace); |
| 10089 | outAttrs->componentMask = static_cast<int8_t>(componentMask); |
| 10090 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10091 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 10092 | } |
| 10093 | |
| 10094 | binder::Status SurfaceComposerAIDL::setDisplayContentSamplingEnabled(const sp<IBinder>& display, |
| 10095 | bool enable, |
| 10096 | int8_t componentMask, |
| 10097 | int64_t maxFrames) { |
| 10098 | status_t status = checkAccessPermission(); |
| 10099 | if (status == OK) { |
| 10100 | status = mFlinger->setDisplayContentSamplingEnabled(display, enable, |
| 10101 | static_cast<uint8_t>(componentMask), |
| 10102 | static_cast<uint64_t>(maxFrames)); |
| 10103 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10104 | return binderStatusFromStatusT(status); |
| 10105 | } |
| 10106 | |
| 10107 | binder::Status SurfaceComposerAIDL::getDisplayedContentSample(const sp<IBinder>& display, |
| 10108 | int64_t maxFrames, int64_t timestamp, |
| 10109 | gui::DisplayedFrameStats* outStats) { |
| 10110 | if (!outStats) { |
| 10111 | return binderStatusFromStatusT(BAD_VALUE); |
| 10112 | } |
| 10113 | |
| 10114 | status_t status = checkAccessPermission(); |
| 10115 | if (status != OK) { |
| 10116 | return binderStatusFromStatusT(status); |
| 10117 | } |
| 10118 | |
| 10119 | DisplayedFrameStats stats; |
| 10120 | status = mFlinger->getDisplayedContentSample(display, static_cast<uint64_t>(maxFrames), |
| 10121 | static_cast<uint64_t>(timestamp), &stats); |
| 10122 | if (status == NO_ERROR) { |
| 10123 | // convert from ui::DisplayedFrameStats to gui::DisplayedFrameStats |
| 10124 | outStats->numFrames = static_cast<int64_t>(stats.numFrames); |
| 10125 | outStats->component_0_sample.reserve(stats.component_0_sample.size()); |
| 10126 | for (const auto& s : stats.component_0_sample) { |
| 10127 | outStats->component_0_sample.push_back(static_cast<int64_t>(s)); |
| 10128 | } |
| 10129 | outStats->component_1_sample.reserve(stats.component_1_sample.size()); |
| 10130 | for (const auto& s : stats.component_1_sample) { |
| 10131 | outStats->component_1_sample.push_back(static_cast<int64_t>(s)); |
| 10132 | } |
| 10133 | outStats->component_2_sample.reserve(stats.component_2_sample.size()); |
| 10134 | for (const auto& s : stats.component_2_sample) { |
| 10135 | outStats->component_2_sample.push_back(static_cast<int64_t>(s)); |
| 10136 | } |
| 10137 | outStats->component_3_sample.reserve(stats.component_3_sample.size()); |
| 10138 | for (const auto& s : stats.component_3_sample) { |
| 10139 | outStats->component_3_sample.push_back(static_cast<int64_t>(s)); |
| 10140 | } |
| 10141 | } |
| 10142 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 10143 | } |
| 10144 | |
| 10145 | binder::Status SurfaceComposerAIDL::getProtectedContentSupport(bool* outSupported) { |
| 10146 | status_t status = mFlinger->getProtectedContentSupport(outSupported); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10147 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 10148 | } |
| 10149 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 10150 | binder::Status SurfaceComposerAIDL::isWideColorDisplay(const sp<IBinder>& token, |
| 10151 | bool* outIsWideColorDisplay) { |
| 10152 | status_t status = mFlinger->isWideColorDisplay(token, outIsWideColorDisplay); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10153 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 10154 | } |
| 10155 | |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10156 | binder::Status SurfaceComposerAIDL::addRegionSamplingListener( |
| 10157 | const gui::ARect& samplingArea, const sp<IBinder>& stopLayerHandle, |
| 10158 | const sp<gui::IRegionSamplingListener>& listener) { |
| 10159 | status_t status = checkReadFrameBufferPermission(); |
| 10160 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10161 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10162 | } |
| 10163 | android::Rect rect; |
| 10164 | rect.left = samplingArea.left; |
| 10165 | rect.top = samplingArea.top; |
| 10166 | rect.right = samplingArea.right; |
| 10167 | rect.bottom = samplingArea.bottom; |
| 10168 | status = mFlinger->addRegionSamplingListener(rect, stopLayerHandle, listener); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10169 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10170 | } |
| 10171 | |
| 10172 | binder::Status SurfaceComposerAIDL::removeRegionSamplingListener( |
| 10173 | const sp<gui::IRegionSamplingListener>& listener) { |
| 10174 | status_t status = checkReadFrameBufferPermission(); |
| 10175 | if (status == OK) { |
| 10176 | status = mFlinger->removeRegionSamplingListener(listener); |
| 10177 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10178 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10179 | } |
| 10180 | |
| 10181 | binder::Status SurfaceComposerAIDL::addFpsListener(int32_t taskId, |
| 10182 | const sp<gui::IFpsListener>& listener) { |
| 10183 | status_t status = checkReadFrameBufferPermission(); |
| 10184 | if (status == OK) { |
| 10185 | status = mFlinger->addFpsListener(taskId, listener); |
| 10186 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10187 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10188 | } |
| 10189 | |
| 10190 | binder::Status SurfaceComposerAIDL::removeFpsListener(const sp<gui::IFpsListener>& listener) { |
| 10191 | status_t status = checkReadFrameBufferPermission(); |
| 10192 | if (status == OK) { |
| 10193 | status = mFlinger->removeFpsListener(listener); |
| 10194 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10195 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10196 | } |
| 10197 | |
| 10198 | binder::Status SurfaceComposerAIDL::addTunnelModeEnabledListener( |
| 10199 | const sp<gui::ITunnelModeEnabledListener>& listener) { |
| 10200 | status_t status = checkAccessPermission(); |
| 10201 | if (status == OK) { |
| 10202 | status = mFlinger->addTunnelModeEnabledListener(listener); |
| 10203 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10204 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10205 | } |
| 10206 | |
| 10207 | binder::Status SurfaceComposerAIDL::removeTunnelModeEnabledListener( |
| 10208 | const sp<gui::ITunnelModeEnabledListener>& listener) { |
| 10209 | status_t status = checkAccessPermission(); |
| 10210 | if (status == OK) { |
| 10211 | status = mFlinger->removeTunnelModeEnabledListener(listener); |
| 10212 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10213 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10214 | } |
| 10215 | |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 10216 | binder::Status SurfaceComposerAIDL::setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, |
| 10217 | const gui::DisplayModeSpecs& specs) { |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10218 | status_t status = checkAccessPermission(); |
| 10219 | if (status == OK) { |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 10220 | status = mFlinger->setDesiredDisplayModeSpecs(displayToken, specs); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10221 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10222 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10223 | } |
| 10224 | |
| 10225 | binder::Status SurfaceComposerAIDL::getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, |
| 10226 | gui::DisplayModeSpecs* outSpecs) { |
| 10227 | if (!outSpecs) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10228 | return binderStatusFromStatusT(BAD_VALUE); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10229 | } |
| 10230 | |
| 10231 | status_t status = checkAccessPermission(); |
| 10232 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10233 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10234 | } |
| 10235 | |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 10236 | status = mFlinger->getDesiredDisplayModeSpecs(displayToken, outSpecs); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10237 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10238 | } |
| 10239 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 10240 | binder::Status SurfaceComposerAIDL::getDisplayBrightnessSupport(const sp<IBinder>& displayToken, |
| 10241 | bool* outSupport) { |
| 10242 | status_t status = mFlinger->getDisplayBrightnessSupport(displayToken, outSupport); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10243 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 10244 | } |
| 10245 | |
| 10246 | binder::Status SurfaceComposerAIDL::setDisplayBrightness(const sp<IBinder>& displayToken, |
| 10247 | const gui::DisplayBrightness& brightness) { |
| 10248 | status_t status = checkControlDisplayBrightnessPermission(); |
| 10249 | if (status == OK) { |
| 10250 | status = mFlinger->setDisplayBrightness(displayToken, brightness); |
| 10251 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10252 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 10253 | } |
| 10254 | |
| 10255 | binder::Status SurfaceComposerAIDL::addHdrLayerInfoListener( |
| 10256 | const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) { |
| 10257 | status_t status = checkControlDisplayBrightnessPermission(); |
| 10258 | if (status == OK) { |
| 10259 | status = mFlinger->addHdrLayerInfoListener(displayToken, listener); |
| 10260 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10261 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 10262 | } |
| 10263 | |
| 10264 | binder::Status SurfaceComposerAIDL::removeHdrLayerInfoListener( |
| 10265 | const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) { |
| 10266 | status_t status = checkControlDisplayBrightnessPermission(); |
| 10267 | if (status == OK) { |
| 10268 | status = mFlinger->removeHdrLayerInfoListener(displayToken, listener); |
| 10269 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10270 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 10271 | } |
| 10272 | |
| 10273 | binder::Status SurfaceComposerAIDL::notifyPowerBoost(int boostId) { |
| 10274 | status_t status = checkAccessPermission(); |
| 10275 | if (status == OK) { |
| 10276 | status = mFlinger->notifyPowerBoost(boostId); |
| 10277 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10278 | return binderStatusFromStatusT(status); |
| 10279 | } |
| 10280 | |
| 10281 | binder::Status SurfaceComposerAIDL::setGlobalShadowSettings(const gui::Color& ambientColor, |
| 10282 | const gui::Color& spotColor, |
| 10283 | float lightPosY, float lightPosZ, |
| 10284 | float lightRadius) { |
| 10285 | status_t status = checkAccessPermission(); |
| 10286 | if (status != OK) { |
| 10287 | return binderStatusFromStatusT(status); |
| 10288 | } |
| 10289 | |
| 10290 | half4 ambientColorHalf = {ambientColor.r, ambientColor.g, ambientColor.b, ambientColor.a}; |
| 10291 | half4 spotColorHalf = {spotColor.r, spotColor.g, spotColor.b, spotColor.a}; |
| 10292 | status = mFlinger->setGlobalShadowSettings(ambientColorHalf, spotColorHalf, lightPosY, |
| 10293 | lightPosZ, lightRadius); |
| 10294 | return binderStatusFromStatusT(status); |
| 10295 | } |
| 10296 | |
| 10297 | binder::Status SurfaceComposerAIDL::getDisplayDecorationSupport( |
| 10298 | const sp<IBinder>& displayToken, std::optional<gui::DisplayDecorationSupport>* outSupport) { |
| 10299 | std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport> support; |
| 10300 | status_t status = mFlinger->getDisplayDecorationSupport(displayToken, &support); |
| 10301 | if (status != NO_ERROR) { |
| 10302 | ALOGE("getDisplayDecorationSupport failed with error %d", status); |
| 10303 | return binderStatusFromStatusT(status); |
| 10304 | } |
| 10305 | |
| 10306 | if (!support || !support.has_value()) { |
| 10307 | outSupport->reset(); |
| 10308 | } else { |
| 10309 | outSupport->emplace(); |
| 10310 | outSupport->value().format = static_cast<int32_t>(support->format); |
| 10311 | outSupport->value().alphaInterpretation = |
| 10312 | static_cast<int32_t>(support->alphaInterpretation); |
| 10313 | } |
| 10314 | |
| 10315 | return binder::Status::ok(); |
| 10316 | } |
| 10317 | |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 10318 | binder::Status SurfaceComposerAIDL::setGameModeFrameRateOverride(int32_t uid, float frameRate) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10319 | status_t status; |
| 10320 | const int c_uid = IPCThreadState::self()->getCallingUid(); |
| 10321 | if (c_uid == AID_ROOT || c_uid == AID_SYSTEM) { |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 10322 | status = mFlinger->setGameModeFrameRateOverride(uid, frameRate); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10323 | } else { |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 10324 | ALOGE("setGameModeFrameRateOverride() permission denied for uid: %d", c_uid); |
| 10325 | status = PERMISSION_DENIED; |
| 10326 | } |
| 10327 | return binderStatusFromStatusT(status); |
| 10328 | } |
| 10329 | |
| 10330 | binder::Status SurfaceComposerAIDL::setGameDefaultFrameRateOverride(int32_t uid, float frameRate) { |
| 10331 | status_t status; |
| 10332 | const int c_uid = IPCThreadState::self()->getCallingUid(); |
| 10333 | if (c_uid == AID_ROOT || c_uid == AID_SYSTEM) { |
| 10334 | status = mFlinger->setGameDefaultFrameRateOverride(uid, frameRate); |
| 10335 | } else { |
| 10336 | ALOGE("setGameDefaultFrameRateOverride() permission denied for uid: %d", c_uid); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10337 | status = PERMISSION_DENIED; |
| 10338 | } |
| 10339 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 10340 | } |
| 10341 | |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 10342 | binder::Status SurfaceComposerAIDL::enableRefreshRateOverlay(bool active) { |
| 10343 | mFlinger->sfdo_enableRefreshRateOverlay(active); |
| 10344 | return binder::Status::ok(); |
| 10345 | } |
| 10346 | |
| 10347 | binder::Status SurfaceComposerAIDL::setDebugFlash(int delay) { |
| 10348 | mFlinger->sfdo_setDebugFlash(delay); |
| 10349 | return binder::Status::ok(); |
| 10350 | } |
| 10351 | |
| 10352 | binder::Status SurfaceComposerAIDL::scheduleComposite() { |
| 10353 | mFlinger->sfdo_scheduleComposite(); |
| 10354 | return binder::Status::ok(); |
| 10355 | } |
| 10356 | |
| 10357 | binder::Status SurfaceComposerAIDL::scheduleCommit() { |
| 10358 | mFlinger->sfdo_scheduleCommit(); |
| 10359 | return binder::Status::ok(); |
| 10360 | } |
| 10361 | |
Carlos Martinez Romero | b4ea9e6 | 2023-10-25 21:48:56 +0000 | [diff] [blame] | 10362 | binder::Status SurfaceComposerAIDL::forceClientComposition(bool enabled) { |
| 10363 | mFlinger->sfdo_forceClientComposition(enabled); |
| 10364 | return binder::Status::ok(); |
| 10365 | } |
| 10366 | |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 10367 | binder::Status SurfaceComposerAIDL::updateSmallAreaDetection(const std::vector<int32_t>& appIds, |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 10368 | const std::vector<float>& thresholds) { |
| 10369 | status_t status; |
| 10370 | const int c_uid = IPCThreadState::self()->getCallingUid(); |
| 10371 | if (c_uid == AID_ROOT || c_uid == AID_SYSTEM) { |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 10372 | if (appIds.size() != thresholds.size()) return binderStatusFromStatusT(BAD_VALUE); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 10373 | |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 10374 | std::vector<std::pair<int32_t, float>> mappings; |
| 10375 | const size_t size = appIds.size(); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 10376 | mappings.reserve(size); |
| 10377 | for (int i = 0; i < size; i++) { |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 10378 | auto row = std::make_pair(appIds[i], thresholds[i]); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 10379 | mappings.push_back(row); |
| 10380 | } |
| 10381 | status = mFlinger->updateSmallAreaDetection(mappings); |
| 10382 | } else { |
| 10383 | ALOGE("updateSmallAreaDetection() permission denied for uid: %d", c_uid); |
| 10384 | status = PERMISSION_DENIED; |
| 10385 | } |
| 10386 | return binderStatusFromStatusT(status); |
| 10387 | } |
| 10388 | |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 10389 | binder::Status SurfaceComposerAIDL::setSmallAreaDetectionThreshold(int32_t appId, float threshold) { |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 10390 | status_t status; |
| 10391 | const int c_uid = IPCThreadState::self()->getCallingUid(); |
| 10392 | if (c_uid == AID_ROOT || c_uid == AID_SYSTEM) { |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 10393 | status = mFlinger->setSmallAreaDetectionThreshold(appId, threshold); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 10394 | } else { |
| 10395 | ALOGE("setSmallAreaDetectionThreshold() permission denied for uid: %d", c_uid); |
| 10396 | status = PERMISSION_DENIED; |
| 10397 | } |
| 10398 | return binderStatusFromStatusT(status); |
| 10399 | } |
| 10400 | |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10401 | binder::Status SurfaceComposerAIDL::getGpuContextPriority(int32_t* outPriority) { |
| 10402 | *outPriority = mFlinger->getGpuContextPriority(); |
| 10403 | return binder::Status::ok(); |
| 10404 | } |
| 10405 | |
| 10406 | binder::Status SurfaceComposerAIDL::getMaxAcquiredBufferCount(int32_t* buffers) { |
| 10407 | status_t status = mFlinger->getMaxAcquiredBufferCount(buffers); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10408 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10409 | } |
| 10410 | |
| 10411 | binder::Status SurfaceComposerAIDL::addWindowInfosListener( |
Patrick Williams | acd2258 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 10412 | const sp<gui::IWindowInfosListener>& windowInfosListener, |
| 10413 | gui::WindowInfosListenerInfo* outInfo) { |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10414 | status_t status; |
Patrick Williams | 0525e9b | 2023-02-13 17:24:18 -0600 | [diff] [blame] | 10415 | const int pid = IPCThreadState::self()->getCallingPid(); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10416 | const int uid = IPCThreadState::self()->getCallingUid(); |
Patrick Williams | 0525e9b | 2023-02-13 17:24:18 -0600 | [diff] [blame] | 10417 | // TODO(b/270566761) update permissions check so that only system_server and shell can add |
| 10418 | // WindowInfosListeners |
| 10419 | if (uid == AID_SYSTEM || uid == AID_GRAPHICS || |
| 10420 | checkPermission(sAccessSurfaceFlinger, pid, uid)) { |
Patrick Williams | acd2258 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 10421 | status = mFlinger->addWindowInfosListener(windowInfosListener, outInfo); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10422 | } else { |
| 10423 | status = PERMISSION_DENIED; |
| 10424 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10425 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10426 | } |
| 10427 | |
| 10428 | binder::Status SurfaceComposerAIDL::removeWindowInfosListener( |
| 10429 | const sp<gui::IWindowInfosListener>& windowInfosListener) { |
| 10430 | status_t status; |
Patrick Williams | 0525e9b | 2023-02-13 17:24:18 -0600 | [diff] [blame] | 10431 | const int pid = IPCThreadState::self()->getCallingPid(); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10432 | const int uid = IPCThreadState::self()->getCallingUid(); |
Patrick Williams | 0525e9b | 2023-02-13 17:24:18 -0600 | [diff] [blame] | 10433 | if (uid == AID_SYSTEM || uid == AID_GRAPHICS || |
| 10434 | checkPermission(sAccessSurfaceFlinger, pid, uid)) { |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10435 | status = mFlinger->removeWindowInfosListener(windowInfosListener); |
| 10436 | } else { |
| 10437 | status = PERMISSION_DENIED; |
| 10438 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 10439 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10440 | } |
| 10441 | |
Patrick Williams | 090ad06 | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 10442 | binder::Status SurfaceComposerAIDL::getStalledTransactionInfo( |
| 10443 | int pid, std::optional<gui::StalledTransactionInfo>* outInfo) { |
| 10444 | const int callingPid = IPCThreadState::self()->getCallingPid(); |
| 10445 | const int callingUid = IPCThreadState::self()->getCallingUid(); |
| 10446 | if (!checkPermission(sAccessSurfaceFlinger, callingPid, callingUid)) { |
| 10447 | return binderStatusFromStatusT(PERMISSION_DENIED); |
| 10448 | } |
| 10449 | |
| 10450 | std::optional<TransactionHandler::StalledTransactionInfo> stalledTransactionInfo; |
| 10451 | status_t status = mFlinger->getStalledTransactionInfo(pid, stalledTransactionInfo); |
| 10452 | if (stalledTransactionInfo) { |
| 10453 | gui::StalledTransactionInfo result; |
| 10454 | result.layerName = String16{stalledTransactionInfo->layerName.c_str()}, |
| 10455 | result.bufferId = stalledTransactionInfo->bufferId, |
| 10456 | result.frameNumber = stalledTransactionInfo->frameNumber, |
| 10457 | outInfo->emplace(std::move(result)); |
| 10458 | } else { |
| 10459 | outInfo->reset(); |
| 10460 | } |
| 10461 | return binderStatusFromStatusT(status); |
| 10462 | } |
| 10463 | |
Ady Abraham | 07d03c4 | 2023-09-27 19:15:08 -0700 | [diff] [blame] | 10464 | binder::Status SurfaceComposerAIDL::getSchedulingPolicy(gui::SchedulingPolicy* outPolicy) { |
| 10465 | return gui::getSchedulingPolicy(outPolicy); |
| 10466 | } |
| 10467 | |
Wenhui Yang | eafc18a | 2024-05-14 17:15:52 +0000 | [diff] [blame] | 10468 | binder::Status SurfaceComposerAIDL::notifyShutdown() { |
| 10469 | TransactionTraceWriter::getInstance().invoke("systemShutdown_", /* overwrite= */ false); |
| 10470 | return ::android::binder::Status::ok(); |
| 10471 | } |
| 10472 | |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 10473 | binder::Status SurfaceComposerAIDL::addJankListener(const sp<IBinder>& layerHandle, |
| 10474 | const sp<gui::IJankListener>& listener) { |
| 10475 | sp<Layer> layer = LayerHandle::getLayer(layerHandle); |
| 10476 | if (layer == nullptr) { |
| 10477 | return binder::Status::fromExceptionCode(binder::Status::EX_NULL_POINTER); |
| 10478 | } |
| 10479 | JankTracker::addJankListener(layer->sequence, IInterface::asBinder(listener)); |
| 10480 | return binder::Status::ok(); |
| 10481 | } |
| 10482 | |
| 10483 | binder::Status SurfaceComposerAIDL::flushJankData(int32_t layerId) { |
| 10484 | JankTracker::flushJankData(layerId); |
| 10485 | return binder::Status::ok(); |
| 10486 | } |
| 10487 | |
| 10488 | binder::Status SurfaceComposerAIDL::removeJankListener(int32_t layerId, |
| 10489 | const sp<gui::IJankListener>& listener, |
| 10490 | int64_t afterVsync) { |
| 10491 | JankTracker::removeJankListener(layerId, IInterface::asBinder(listener), afterVsync); |
| 10492 | return binder::Status::ok(); |
| 10493 | } |
| 10494 | |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 10495 | status_t SurfaceComposerAIDL::checkAccessPermission(bool usePermissionCache) { |
| 10496 | if (!mFlinger->callingThreadHasUnscopedSurfaceFlingerAccess(usePermissionCache)) { |
| 10497 | IPCThreadState* ipc = IPCThreadState::self(); |
| 10498 | ALOGE("Permission Denial: can't access SurfaceFlinger pid=%d, uid=%d", ipc->getCallingPid(), |
| 10499 | ipc->getCallingUid()); |
| 10500 | return PERMISSION_DENIED; |
| 10501 | } |
| 10502 | return OK; |
| 10503 | } |
| 10504 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 10505 | status_t SurfaceComposerAIDL::checkControlDisplayBrightnessPermission() { |
| 10506 | IPCThreadState* ipc = IPCThreadState::self(); |
| 10507 | const int pid = ipc->getCallingPid(); |
| 10508 | const int uid = ipc->getCallingUid(); |
Rheygine Medel | 9e9281e | 2022-09-22 10:13:56 -0700 | [diff] [blame] | 10509 | if ((uid != AID_GRAPHICS) && (uid != AID_SYSTEM) && |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 10510 | !PermissionCache::checkPermission(sControlDisplayBrightness, pid, uid)) { |
| 10511 | ALOGE("Permission Denial: can't control brightness pid=%d, uid=%d", pid, uid); |
| 10512 | return PERMISSION_DENIED; |
| 10513 | } |
| 10514 | return OK; |
| 10515 | } |
| 10516 | |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 10517 | status_t SurfaceComposerAIDL::checkReadFrameBufferPermission() { |
| 10518 | IPCThreadState* ipc = IPCThreadState::self(); |
| 10519 | const int pid = ipc->getCallingPid(); |
| 10520 | const int uid = ipc->getCallingUid(); |
| 10521 | if ((uid != AID_GRAPHICS) && !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) { |
| 10522 | ALOGE("Permission Denial: can't read framebuffer pid=%d, uid=%d", pid, uid); |
| 10523 | return PERMISSION_DENIED; |
| 10524 | } |
| 10525 | return OK; |
| 10526 | } |
| 10527 | |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 10528 | void SurfaceFlinger::forceFutureUpdate(int delayInMs) { |
| 10529 | static_cast<void>(mScheduler->scheduleDelayed([&]() { scheduleRepaint(); }, ms2ns(delayInMs))); |
| 10530 | } |
| 10531 | |
Vishnu Nair | 2f65e97 | 2023-04-04 16:36:28 +0000 | [diff] [blame] | 10532 | const DisplayDevice* SurfaceFlinger::getDisplayFromLayerStack(ui::LayerStack layerStack) { |
| 10533 | for (const auto& [_, display] : mDisplays) { |
| 10534 | if (display->getLayerStack() == layerStack) { |
| 10535 | return display.get(); |
| 10536 | } |
| 10537 | } |
| 10538 | return nullptr; |
| 10539 | } |
| 10540 | |
Dominik Laskowski | 9dab343 | 2019-03-27 13:21:10 -0700 | [diff] [blame] | 10541 | } // namespace android |
Lloyd Pique | 074e812 | 2018-07-26 12:57:23 -0700 | [diff] [blame] | 10542 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 10543 | #if defined(__gl_h_) |
| 10544 | #error "don't include gl/gl.h in this file" |
| 10545 | #endif |
| 10546 | |
| 10547 | #if defined(__gl2_h_) |
| 10548 | #error "don't include gl2/gl2.h in this file" |
Chia-I Wu | 767fcf7 | 2017-11-30 22:07:38 -0800 | [diff] [blame] | 10549 | #endif |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 10550 | |
| 10551 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 10552 | #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" |