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> |
Jim Shargo | 6ccc5e8 | 2024-07-27 03:42:08 +0000 | [diff] [blame] | 43 | #include <com_android_graphics_libgui_flags.h> |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 44 | #include <com_android_graphics_surfaceflinger_flags.h> |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 45 | #include <common/FlagManager.h> |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 46 | #include <common/trace.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 47 | #include <compositionengine/CompositionEngine.h> |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 48 | #include <compositionengine/CompositionRefreshArgs.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 49 | #include <compositionengine/Display.h> |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 50 | #include <compositionengine/DisplayColorProfile.h> |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 51 | #include <compositionengine/DisplayColorProfileCreationArgs.h> |
Lloyd Pique | 9370a48 | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 52 | #include <compositionengine/DisplayCreationArgs.h> |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 53 | #include <compositionengine/LayerFECompositionState.h> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 54 | #include <compositionengine/OutputLayer.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 55 | #include <compositionengine/RenderSurface.h> |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 56 | #include <compositionengine/impl/DisplayColorProfile.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 57 | #include <compositionengine/impl/OutputCompositionState.h> |
John Reck | db1a16b | 2021-07-09 13:05:52 -0400 | [diff] [blame] | 58 | #include <compositionengine/impl/OutputLayerCompositionState.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 59 | #include <configstore/Utils.h> |
| 60 | #include <cutils/compiler.h> |
| 61 | #include <cutils/properties.h> |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 62 | #include <fmt/format.h> |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 63 | #include <ftl/algorithm.h> |
Dominik Laskowski | 5d7de5f | 2022-11-03 12:38:32 -0400 | [diff] [blame] | 64 | #include <ftl/concat.h> |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 65 | #include <ftl/fake_guard.h> |
Dominik Laskowski | 4e2b71f | 2020-11-10 15:05:32 -0800 | [diff] [blame] | 66 | #include <ftl/future.h> |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 67 | #include <ftl/unit.h> |
Alec Mouri | 924f950 | 2024-08-15 20:01:08 +0000 | [diff] [blame] | 68 | #include <gui/AidlUtil.h> |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 69 | #include <gui/BufferQueue.h> |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 70 | #include <gui/DebugEGLImageTracker.h> |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 71 | #include <gui/IProducerListener.h> |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 72 | #include <gui/LayerMetadata.h> |
Steven Thomas | 62a4cf8 | 2020-01-31 12:04:03 -0800 | [diff] [blame] | 73 | #include <gui/LayerState.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 74 | #include <gui/Surface.h> |
Brian Johnson | 51d8d5d | 2023-07-28 10:18:47 -0700 | [diff] [blame] | 75 | #include <gui/SurfaceComposerClient.h> |
Steven Moreland | 7c8af94 | 2020-07-08 18:35:51 +0000 | [diff] [blame] | 76 | #include <hidl/ServiceManagement.h> |
Vishnu Nair | 6aac09e | 2024-07-26 21:38:58 +0000 | [diff] [blame] | 77 | #include <layerproto/LayerProtoHeader.h> |
Elliott Hughes | e3ffd13 | 2023-06-28 19:45:15 +0000 | [diff] [blame] | 78 | #include <linux/sched/types.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 79 | #include <log/log.h> |
| 80 | #include <private/android_filesystem_config.h> |
| 81 | #include <private/gui/SyncFeatures.h> |
Wei Wang | 976a645 | 2021-06-11 15:26:00 -0700 | [diff] [blame] | 82 | #include <processgroup/processgroup.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 83 | #include <renderengine/RenderEngine.h> |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 84 | #include <renderengine/impl/ExternalTexture.h> |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 85 | #include <scheduler/FrameTargeter.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 86 | #include <sys/types.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 87 | #include <ui/ColorSpace.h> |
| 88 | #include <ui/DebugUtils.h> |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 89 | #include <ui/DisplayId.h> |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 90 | #include <ui/DisplayMode.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 91 | #include <ui/DisplayStatInfo.h> |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 92 | #include <ui/DisplayState.h> |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 93 | #include <ui/DynamicDisplayInfo.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 94 | #include <ui/GraphicBufferAllocator.h> |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 95 | #include <ui/HdrRenderTypeUtils.h> |
Vishnu Nair | afd527a | 2022-10-18 08:56:10 -0700 | [diff] [blame] | 96 | #include <ui/LayerStack.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 97 | #include <ui/PixelFormat.h> |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 98 | #include <ui/StaticDisplayInfo.h> |
Brian Johnson | 51d8d5d | 2023-07-28 10:18:47 -0700 | [diff] [blame] | 99 | #include <unistd.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | #include <utils/StopWatch.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 101 | #include <utils/String16.h> |
| 102 | #include <utils/String8.h> |
Yusuke Sato | 0a688f5 | 2015-07-30 23:05:39 -0700 | [diff] [blame] | 103 | #include <utils/Timers.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 104 | #include <utils/misc.h> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 105 | #include <algorithm> |
Dominik Laskowski | 4e2b71f | 2020-11-10 15:05:32 -0800 | [diff] [blame] | 106 | #include <cerrno> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 107 | #include <cinttypes> |
| 108 | #include <cmath> |
| 109 | #include <cstdint> |
Patrick Williams | 8b731c1 | 2023-10-24 09:56:24 -0500 | [diff] [blame] | 110 | #include <filesystem> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 111 | #include <functional> |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 112 | #include <memory> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 113 | #include <mutex> |
| 114 | #include <optional> |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 115 | #include <string> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 116 | #include <type_traits> |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 117 | #include <unordered_map> |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 118 | #include <vector> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | |
Alec Mouri | 9b133ca | 2023-11-14 19:00:01 +0000 | [diff] [blame] | 120 | #include <common/FlagManager.h> |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 121 | #include <gui/LayerStatePermissions.h> |
Ady Abraham | 07d03c4 | 2023-09-27 19:15:08 -0700 | [diff] [blame] | 122 | #include <gui/SchedulingPolicy.h> |
Chavi Weingarten | f6fb445 | 2024-01-23 21:10:30 +0000 | [diff] [blame] | 123 | #include <gui/SyncScreenCaptureListener.h> |
Alec Mouri | ff79387 | 2022-01-13 17:45:06 -0800 | [diff] [blame] | 124 | #include <ui/DisplayIdentification.h> |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 125 | #include "BackgroundExecutor.h" |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 126 | #include "Client.h" |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 127 | #include "ClientCache.h" |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 128 | #include "Colorizer.h" |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 129 | #include "DisplayDevice.h" |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 130 | #include "DisplayHardware/ComposerHal.h" |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 131 | #include "DisplayHardware/FramebufferSurface.h" |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 132 | #include "DisplayHardware/HWComposer.h" |
Marin Shalamanov | d3b5c5d | 2021-02-11 18:26:14 +0100 | [diff] [blame] | 133 | #include "DisplayHardware/Hal.h" |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 134 | #include "DisplayHardware/PowerAdvisor.h" |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 135 | #include "DisplayHardware/VirtualDisplaySurface.h" |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 136 | #include "DisplayRenderArea.h" |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 137 | #include "Effects/Daltonizer.h" |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 138 | #include "FpsReporter.h" |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 139 | #include "FrameTimeline/FrameTimeline.h" |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 140 | #include "FrameTracer/FrameTracer.h" |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 141 | #include "FrontEnd/LayerCreationArgs.h" |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 142 | #include "FrontEnd/LayerHandle.h" |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 143 | #include "FrontEnd/LayerLifecycleManager.h" |
Vishnu Nair | 854ce1c | 2023-08-19 15:00:13 -0700 | [diff] [blame] | 144 | #include "FrontEnd/LayerLog.h" |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 145 | #include "FrontEnd/LayerSnapshot.h" |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 146 | #include "HdrLayerInfoReporter.h" |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 147 | #include "Jank/JankTracker.h" |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 148 | #include "Layer.h" |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 149 | #include "LayerProtoHelper.h" |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 150 | #include "LayerRenderArea.h" |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 151 | #include "LayerVector.h" |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 152 | #include "MutexUtils.h" |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 153 | #include "NativeWindowSurface.h" |
Dominik Laskowski | 9dab343 | 2019-03-27 13:21:10 -0700 | [diff] [blame] | 154 | #include "RegionSamplingThread.h" |
Melody Hsu | 41ade20 | 2024-05-03 01:25:50 +0000 | [diff] [blame] | 155 | #include "RenderAreaBuilder.h" |
Ana Krulec | fefcb58 | 2018-08-07 14:22:37 -0700 | [diff] [blame] | 156 | #include "Scheduler/EventThread.h" |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 157 | #include "Scheduler/LayerHistory.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 158 | #include "Scheduler/Scheduler.h" |
Ady Abraham | c581d3c | 2020-08-06 17:34:27 -0700 | [diff] [blame] | 159 | #include "Scheduler/VsyncConfiguration.h" |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 160 | #include "Scheduler/VsyncModulator.h" |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 161 | #include "ScreenCaptureOutput.h" |
Alec Mouri | 5f487d4 | 2020-02-06 09:26:19 -0800 | [diff] [blame] | 162 | #include "SurfaceFlingerProperties.h" |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 163 | #include "TimeStats/TimeStats.h" |
Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 164 | #include "TunnelModeEnabledReporter.h" |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame] | 165 | #include "Utils/Dumper.h" |
chaviw | 60c9d3e | 2021-06-04 12:52:17 -0500 | [diff] [blame] | 166 | #include "WindowInfosListenerInvoker.h" |
David Sodman | 7e4ae11 | 2018-02-09 15:02:28 -0800 | [diff] [blame] | 167 | |
Leon Scroggins III | e7c51c6 | 2022-02-01 15:53:54 -0500 | [diff] [blame] | 168 | #include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h> |
Leon Scroggins III | 5967aec | 2021-12-29 11:14:22 -0500 | [diff] [blame] | 169 | #include <aidl/android/hardware/graphics/composer3/DisplayCapability.h> |
Alec Mouri | fcedb9c | 2022-04-11 20:02:17 +0000 | [diff] [blame] | 170 | #include <aidl/android/hardware/graphics/composer3/RenderIntent.h> |
Leon Scroggins III | 5967aec | 2021-12-29 11:14:22 -0500 | [diff] [blame] | 171 | |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 172 | #undef NO_THREAD_SAFETY_ANALYSIS |
| 173 | #define NO_THREAD_SAFETY_ANALYSIS \ |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 174 | _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] | 175 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | namespace android { |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 177 | using namespace std::chrono_literals; |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 178 | using namespace std::string_literals; |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 179 | using namespace std::string_view_literals; |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 180 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 181 | using namespace hardware::configstore; |
| 182 | using namespace hardware::configstore::V1_0; |
| 183 | using namespace sysprop; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 184 | using ftl::Flags; |
| 185 | using namespace ftl::flag_operators; |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 186 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 187 | using aidl::android::hardware::graphics::common::DisplayDecorationSupport; |
| 188 | using aidl::android::hardware::graphics::composer3::Capability; |
| 189 | using aidl::android::hardware::graphics::composer3::DisplayCapability; |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 190 | using CompositionStrategyPredictionState = android::compositionengine::impl:: |
| 191 | OutputCompositionState::CompositionStrategyPredictionState; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 192 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 193 | using base::StringAppendF; |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 194 | using display::PhysicalDisplay; |
| 195 | using display::PhysicalDisplays; |
Vishnu Nair | af6d297 | 2022-11-18 06:26:38 +0000 | [diff] [blame] | 196 | using frontend::TransactionHandler; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 197 | using gui::DisplayInfo; |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 198 | using gui::GameMode; |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 199 | using gui::IDisplayEventConnection; |
chaviw | 60c9d3e | 2021-06-04 12:52:17 -0500 | [diff] [blame] | 200 | using gui::IWindowInfosListener; |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 201 | using gui::LayerMetadata; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 202 | using gui::WindowInfo; |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 203 | using gui::aidl_utils::binderStatusFromStatusT; |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 204 | using scheduler::VsyncModulator; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 205 | using ui::Dataspace; |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 206 | using ui::DisplayPrimaries; |
Peiyong Lin | 0e7a791 | 2018-04-05 14:36:36 -0700 | [diff] [blame] | 207 | using ui::RenderIntent; |
Jiyong Park | 4b20c2e | 2017-01-14 19:45:11 +0900 | [diff] [blame] | 208 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 209 | namespace hal = android::hardware::graphics::composer::hal; |
| 210 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 211 | namespace { |
Peiyong Lin | fca547f | 2018-07-09 13:03:33 -0700 | [diff] [blame] | 212 | |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 213 | static constexpr int FOUR_K_WIDTH = 3840; |
| 214 | static constexpr int FOUR_K_HEIGHT = 2160; |
| 215 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 216 | // TODO(b/141333600): Consolidate with DisplayMode::Builder::getDefaultDensity. |
Dominik Laskowski | d125d0e | 2020-05-08 12:36:39 -0700 | [diff] [blame] | 217 | constexpr float FALLBACK_DENSITY = ACONFIGURATION_DENSITY_TV; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 218 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 219 | float getDensityFromProperty(const char* property, bool required) { |
| 220 | char value[PROPERTY_VALUE_MAX]; |
| 221 | const float density = property_get(property, value, nullptr) > 0 ? std::atof(value) : 0.f; |
| 222 | if (!density && required) { |
| 223 | ALOGE("%s must be defined as a build property", property); |
| 224 | return FALLBACK_DENSITY; |
| 225 | } |
| 226 | return density; |
| 227 | } |
| 228 | |
Peiyong Lin | 9d846a5 | 2018-11-05 13:18:20 -0800 | [diff] [blame] | 229 | // Currently we only support V0_SRGB and DISPLAY_P3 as composition preference. |
| 230 | bool validateCompositionDataspace(Dataspace dataspace) { |
| 231 | return dataspace == Dataspace::V0_SRGB || dataspace == Dataspace::DISPLAY_P3; |
| 232 | } |
| 233 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 234 | std::chrono::milliseconds getIdleTimerTimeout(PhysicalDisplayId displayId) { |
Dominik Laskowski | dd374e1 | 2024-02-21 10:28:15 -0500 | [diff] [blame] | 235 | if (const int32_t displayIdleTimerMs = |
| 236 | base::GetIntProperty("debug.sf.set_idle_timer_ms_"s + |
| 237 | std::to_string(displayId.value), |
| 238 | 0); |
| 239 | displayIdleTimerMs > 0) { |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 240 | return std::chrono::milliseconds(displayIdleTimerMs); |
| 241 | } |
| 242 | |
Dominik Laskowski | dd374e1 | 2024-02-21 10:28:15 -0500 | [diff] [blame] | 243 | 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] | 244 | const int32_t millis = setIdleTimerMs ? setIdleTimerMs : sysprop::set_idle_timer_ms(0); |
| 245 | return std::chrono::milliseconds(millis); |
| 246 | } |
| 247 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 248 | bool getKernelIdleTimerSyspropConfig(PhysicalDisplayId displayId) { |
Dominik Laskowski | dd374e1 | 2024-02-21 10:28:15 -0500 | [diff] [blame] | 249 | const bool displaySupportKernelIdleTimer = |
| 250 | base::GetBoolProperty("debug.sf.support_kernel_idle_timer_"s + |
| 251 | std::to_string(displayId.value), |
| 252 | false); |
Ady Abraham | 6d88593 | 2021-09-03 18:05:48 -0700 | [diff] [blame] | 253 | |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 254 | return displaySupportKernelIdleTimer || sysprop::support_kernel_idle_timer(false); |
Ady Abraham | 6d88593 | 2021-09-03 18:05:48 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 257 | bool isAbove4k30(const ui::DisplayMode& outMode) { |
| 258 | using fps_approx_ops::operator>; |
Alec Mouri | 55e3103 | 2023-10-02 20:34:18 +0000 | [diff] [blame] | 259 | Fps refreshRate = Fps::fromValue(outMode.peakRefreshRate); |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 260 | return outMode.resolution.getWidth() >= FOUR_K_WIDTH && |
| 261 | outMode.resolution.getHeight() >= FOUR_K_HEIGHT && refreshRate > 30_Hz; |
| 262 | } |
| 263 | |
| 264 | void excludeDolbyVisionIf4k30Present(const std::vector<ui::Hdr>& displayHdrTypes, |
| 265 | ui::DisplayMode& outMode) { |
| 266 | if (isAbove4k30(outMode) && |
| 267 | std::any_of(displayHdrTypes.begin(), displayHdrTypes.end(), |
| 268 | [](ui::Hdr type) { return type == ui::Hdr::DOLBY_VISION_4K30; })) { |
| 269 | for (ui::Hdr type : displayHdrTypes) { |
| 270 | if (type != ui::Hdr::DOLBY_VISION_4K30 && type != ui::Hdr::DOLBY_VISION) { |
| 271 | outMode.supportedHdrTypes.push_back(type); |
| 272 | } |
| 273 | } |
| 274 | } else { |
| 275 | for (ui::Hdr type : displayHdrTypes) { |
| 276 | if (type != ui::Hdr::DOLBY_VISION_4K30) { |
| 277 | outMode.supportedHdrTypes.push_back(type); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | HdrCapabilities filterOut4k30(const HdrCapabilities& displayHdrCapabilities) { |
| 284 | std::vector<ui::Hdr> hdrTypes; |
| 285 | for (ui::Hdr type : displayHdrCapabilities.getSupportedHdrTypes()) { |
| 286 | if (type != ui::Hdr::DOLBY_VISION_4K30) { |
| 287 | hdrTypes.push_back(type); |
| 288 | } |
| 289 | } |
| 290 | return {hdrTypes, displayHdrCapabilities.getDesiredMaxLuminance(), |
| 291 | displayHdrCapabilities.getDesiredMaxAverageLuminance(), |
| 292 | displayHdrCapabilities.getDesiredMinLuminance()}; |
| 293 | } |
| 294 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 295 | uint32_t getLayerIdFromSurfaceControl(sp<SurfaceControl> surfaceControl) { |
| 296 | if (!surfaceControl) { |
| 297 | return UNASSIGNED_LAYER_ID; |
| 298 | } |
| 299 | return LayerHandle::getLayerId(surfaceControl->getHandle()); |
| 300 | } |
| 301 | |
Patrick Williams | 8b731c1 | 2023-10-24 09:56:24 -0500 | [diff] [blame] | 302 | /** |
| 303 | * Returns true if the file at path exists and is newer than duration. |
| 304 | */ |
| 305 | bool fileNewerThan(const std::string& path, std::chrono::minutes duration) { |
| 306 | using Clock = std::filesystem::file_time_type::clock; |
| 307 | std::error_code error; |
| 308 | std::filesystem::file_time_type updateTime = std::filesystem::last_write_time(path, error); |
| 309 | if (error) { |
| 310 | return false; |
| 311 | } |
| 312 | return duration > (Clock::now() - updateTime); |
| 313 | } |
| 314 | |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 315 | bool isFrameIntervalOnCadence(TimePoint expectedPresentTime, TimePoint lastExpectedPresentTimestamp, |
| 316 | Fps lastFrameInterval, Period timeout, Duration threshold) { |
| 317 | if (lastFrameInterval.getPeriodNsecs() == 0) { |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | const auto expectedPresentTimeDeltaNs = |
| 322 | expectedPresentTime.ns() - lastExpectedPresentTimestamp.ns(); |
| 323 | |
| 324 | if (expectedPresentTimeDeltaNs > timeout.ns()) { |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | const auto expectedPresentPeriods = static_cast<nsecs_t>( |
| 329 | std::round(static_cast<float>(expectedPresentTimeDeltaNs) / |
| 330 | static_cast<float>(lastFrameInterval.getPeriodNsecs()))); |
| 331 | const auto calculatedPeriodsOutNs = lastFrameInterval.getPeriodNsecs() * expectedPresentPeriods; |
| 332 | const auto calculatedExpectedPresentTimeNs = |
| 333 | lastExpectedPresentTimestamp.ns() + calculatedPeriodsOutNs; |
| 334 | const auto presentTimeDelta = |
| 335 | std::abs(expectedPresentTime.ns() - calculatedExpectedPresentTimeNs); |
| 336 | return presentTimeDelta < threshold.ns(); |
| 337 | } |
| 338 | |
| 339 | bool isExpectedPresentWithinTimeout(TimePoint expectedPresentTime, |
| 340 | TimePoint lastExpectedPresentTimestamp, |
| 341 | std::optional<Period> timeoutOpt, Duration threshold) { |
| 342 | if (!timeoutOpt) { |
| 343 | // Always within timeout if timeoutOpt is absent and don't send hint |
| 344 | // for the timeout |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | if (timeoutOpt->ns() == 0) { |
| 349 | // Always outside timeout if timeoutOpt is 0 and always send |
| 350 | // the hint for the timeout. |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | if (expectedPresentTime.ns() < lastExpectedPresentTimestamp.ns() + timeoutOpt->ns()) { |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | // Check if within the threshold as it can be just outside the timeout |
| 359 | return std::abs(expectedPresentTime.ns() - |
| 360 | (lastExpectedPresentTimestamp.ns() + timeoutOpt->ns())) < threshold.ns(); |
| 361 | } |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 362 | } // namespace anonymous |
| 363 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | // --------------------------------------------------------------------------- |
| 365 | |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 366 | const String16 sHardwareTest("android.permission.HARDWARE_TEST"); |
| 367 | const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"); |
Hongwei Wang | 46213ea | 2020-12-04 17:17:31 -0800 | [diff] [blame] | 368 | const String16 sRotateSurfaceFlinger("android.permission.ROTATE_SURFACE_FLINGER"); |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 369 | const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER"); |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 370 | const String16 sControlDisplayBrightness("android.permission.CONTROL_DISPLAY_BRIGHTNESS"); |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 371 | const String16 sDump("android.permission.DUMP"); |
chaviw | f5bb97b | 2021-04-28 15:35:37 -0500 | [diff] [blame] | 372 | const String16 sCaptureBlackoutContent("android.permission.CAPTURE_BLACKOUT_CONTENT"); |
Leon Scroggins | 9a20f72 | 2021-12-28 14:43:12 +0000 | [diff] [blame] | 373 | const String16 sInternalSystemWindow("android.permission.INTERNAL_SYSTEM_WINDOW"); |
Vishnu Nair | 884549f | 2022-12-01 17:20:59 -0800 | [diff] [blame] | 374 | const String16 sWakeupSurfaceFlinger("android.permission.WAKEUP_SURFACE_FLINGER"); |
chaviw | f5bb97b | 2021-04-28 15:35:37 -0500 | [diff] [blame] | 375 | |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 376 | // --------------------------------------------------------------------------- |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 377 | int64_t SurfaceFlinger::dispSyncPresentTimeOffset; |
| 378 | bool SurfaceFlinger::useHwcForRgbToYuv; |
| 379 | bool SurfaceFlinger::hasSyncFramework; |
| 380 | int64_t SurfaceFlinger::maxFrameBufferAcquiredBuffers; |
| 381 | int64_t SurfaceFlinger::minAcquiredBuffers = 1; |
| 382 | uint32_t SurfaceFlinger::maxGraphicsWidth; |
| 383 | uint32_t SurfaceFlinger::maxGraphicsHeight; |
| 384 | bool SurfaceFlinger::useContextPriority; |
| 385 | Dataspace SurfaceFlinger::defaultCompositionDataspace = Dataspace::V0_SRGB; |
| 386 | ui::PixelFormat SurfaceFlinger::defaultCompositionPixelFormat = ui::PixelFormat::RGBA_8888; |
| 387 | Dataspace SurfaceFlinger::wideColorGamutCompositionDataspace = Dataspace::V0_SRGB; |
| 388 | ui::PixelFormat SurfaceFlinger::wideColorGamutCompositionPixelFormat = ui::PixelFormat::RGBA_8888; |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 389 | LatchUnsignaledConfig SurfaceFlinger::enableLatchUnsignaledConfig; |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 390 | |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 391 | std::string decodeDisplayColorSetting(DisplayColorSetting displayColorSetting) { |
| 392 | switch(displayColorSetting) { |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 393 | case DisplayColorSetting::kManaged: |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 394 | return std::string("Managed"); |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 395 | case DisplayColorSetting::kUnmanaged: |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 396 | return std::string("Unmanaged"); |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 397 | case DisplayColorSetting::kEnhanced: |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 398 | return std::string("Enhanced"); |
| 399 | default: |
| 400 | return std::string("Unknown ") + |
| 401 | std::to_string(static_cast<int>(displayColorSetting)); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 402 | } |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Vishnu Nair | 884549f | 2022-12-01 17:20:59 -0800 | [diff] [blame] | 405 | bool callingThreadHasPermission(const String16& permission) { |
Hongwei Wang | 46213ea | 2020-12-04 17:17:31 -0800 | [diff] [blame] | 406 | IPCThreadState* ipc = IPCThreadState::self(); |
| 407 | const int pid = ipc->getCallingPid(); |
| 408 | const int uid = ipc->getCallingUid(); |
| 409 | return uid == AID_GRAPHICS || uid == AID_SYSTEM || |
Vishnu Nair | 884549f | 2022-12-01 17:20:59 -0800 | [diff] [blame] | 410 | PermissionCache::checkPermission(permission, pid, uid); |
Leon Scroggins | 9a20f72 | 2021-12-28 14:43:12 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Leon Scroggins III | 85d4b22 | 2023-05-09 13:58:18 -0400 | [diff] [blame] | 413 | ui::Transform::RotationFlags SurfaceFlinger::sActiveDisplayRotationFlags = ui::Transform::ROT_0; |
| 414 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 415 | SurfaceFlinger::SurfaceFlinger(Factory& factory, SkipInitializationTag) |
| 416 | : mFactory(factory), |
| 417 | mPid(getpid()), |
Yiwei Zhang | f0229a7 | 2019-09-09 19:28:02 -0700 | [diff] [blame] | 418 | mTimeStats(std::make_shared<impl::TimeStats>()), |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 419 | mFrameTracer(mFactory.createFrameTracer()), |
| 420 | mFrameTimeline(mFactory.createFrameTimeline(mTimeStats, mPid)), |
| 421 | mCompositionEngine(mFactory.createCompositionEngine()), |
| 422 | mHwcServiceName(base::GetProperty("debug.sf.hwc_service_name"s, "default"s)), |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 423 | mTunnelModeEnabledReporter(sp<TunnelModeEnabledReporter>::make()), |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 424 | mEmulatedDisplayDensity(getDensityFromProperty("qemu.sf.lcd_density", false)), |
| 425 | mInternalDisplayDensity( |
| 426 | getDensityFromProperty("ro.sf.lcd_density", !mEmulatedDisplayDensity)), |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 427 | mPowerAdvisor(std::make_unique<Hwc2::impl::PowerAdvisor>(*this)), |
Robert Horvath | dce110a | 2022-09-16 11:08:47 +0200 | [diff] [blame] | 428 | mWindowInfosListenerInvoker(sp<WindowInfosListenerInvoker>::make()), |
| 429 | mSkipPowerOnForQuiescent(base::GetBoolProperty("ro.boot.quiescent"s, false)) { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 430 | ALOGI("Using HWComposer service: %s", mHwcServiceName.c_str()); |
| 431 | } |
Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 432 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 433 | SurfaceFlinger::SurfaceFlinger(Factory& factory) : SurfaceFlinger(factory, SkipInitialization) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 434 | SFTRACE_CALL(); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 435 | ALOGI("SurfaceFlinger is starting"); |
| 436 | |
| 437 | hasSyncFramework = running_without_sync_framework(true); |
| 438 | |
| 439 | dispSyncPresentTimeOffset = present_time_offset_from_vsync_ns(0); |
| 440 | |
| 441 | useHwcForRgbToYuv = force_hwc_copy_for_virtual_displays(false); |
| 442 | |
| 443 | maxFrameBufferAcquiredBuffers = max_frame_buffer_acquired_buffers(2); |
| 444 | minAcquiredBuffers = |
| 445 | SurfaceFlingerProperties::min_acquired_buffers().value_or(minAcquiredBuffers); |
| 446 | |
| 447 | maxGraphicsWidth = std::max(max_graphics_width(0), 0); |
| 448 | maxGraphicsHeight = std::max(max_graphics_height(0), 0); |
| 449 | |
| 450 | mSupportsWideColor = has_wide_color_display(false); |
| 451 | mDefaultCompositionDataspace = |
| 452 | static_cast<ui::Dataspace>(default_composition_dataspace(Dataspace::V0_SRGB)); |
| 453 | mWideColorGamutCompositionDataspace = static_cast<ui::Dataspace>(wcg_composition_dataspace( |
| 454 | mSupportsWideColor ? Dataspace::DISPLAY_P3 : Dataspace::V0_SRGB)); |
| 455 | defaultCompositionDataspace = mDefaultCompositionDataspace; |
| 456 | wideColorGamutCompositionDataspace = mWideColorGamutCompositionDataspace; |
| 457 | defaultCompositionPixelFormat = static_cast<ui::PixelFormat>( |
| 458 | default_composition_pixel_format(ui::PixelFormat::RGBA_8888)); |
| 459 | wideColorGamutCompositionPixelFormat = |
| 460 | static_cast<ui::PixelFormat>(wcg_composition_pixel_format(ui::PixelFormat::RGBA_8888)); |
| 461 | |
Dominik Laskowski | dd374e1 | 2024-02-21 10:28:15 -0500 | [diff] [blame] | 462 | mLayerCachingEnabled = |
| 463 | base::GetBoolProperty("debug.sf.enable_layer_caching"s, |
| 464 | sysprop::SurfaceFlingerProperties::enable_layer_caching() |
| 465 | .value_or(false)); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 466 | |
| 467 | useContextPriority = use_context_priority(true); |
| 468 | |
| 469 | mInternalDisplayPrimaries = sysprop::getDisplayNativePrimaries(); |
| 470 | |
| 471 | // debugging stuff... |
| 472 | char value[PROPERTY_VALUE_MAX]; |
| 473 | |
| 474 | property_get("ro.build.type", value, "user"); |
| 475 | mIsUserBuild = strcmp(value, "user") == 0; |
| 476 | |
| 477 | mDebugFlashDelay = base::GetUintProperty("debug.sf.showupdates"s, 0u); |
| 478 | |
| 479 | mBackpressureGpuComposition = base::GetBoolProperty("debug.sf.enable_gl_backpressure"s, true); |
| 480 | ALOGI_IF(mBackpressureGpuComposition, "Enabling backpressure for GPU composition"); |
| 481 | |
| 482 | property_get("ro.surface_flinger.supports_background_blur", value, "0"); |
| 483 | bool supportsBlurs = atoi(value); |
| 484 | mSupportsBlur = supportsBlurs; |
| 485 | ALOGI_IF(!mSupportsBlur, "Disabling blur effects, they are not supported."); |
| 486 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 487 | property_get("debug.sf.luma_sampling", value, "1"); |
| 488 | mLumaSampling = atoi(value); |
| 489 | |
| 490 | property_get("debug.sf.disable_client_composition_cache", value, "0"); |
| 491 | mDisableClientCompositionCache = atoi(value); |
| 492 | |
| 493 | property_get("debug.sf.predict_hwc_composition_strategy", value, "1"); |
| 494 | mPredictCompositionStrategy = atoi(value); |
| 495 | |
| 496 | property_get("debug.sf.treat_170m_as_sRGB", value, "0"); |
| 497 | mTreat170mAsSrgb = atoi(value); |
| 498 | |
Alec Mouri | 9af38ba | 2023-08-16 22:41:14 +0000 | [diff] [blame] | 499 | property_get("debug.sf.dim_in_gamma_in_enhanced_screenshots", value, 0); |
| 500 | mDimInGammaSpaceForEnhancedScreenshots = atoi(value); |
| 501 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 502 | mIgnoreHwcPhysicalDisplayOrientation = |
| 503 | base::GetBoolProperty("debug.sf.ignore_hwc_physical_display_orientation"s, false); |
| 504 | |
| 505 | // We should be reading 'persist.sys.sf.color_saturation' here |
| 506 | // but since /data may be encrypted, we need to wait until after vold |
| 507 | // comes online to attempt to read the property. The property is |
| 508 | // instead read after the boot animation |
| 509 | |
| 510 | if (base::GetBoolProperty("debug.sf.treble_testing_override"s, false)) { |
Kalle Raita | a099a24 | 2017-01-11 11:17:29 -0800 | [diff] [blame] | 511 | // Without the override SurfaceFlinger cannot connect to HIDL |
| 512 | // services that are not listed in the manifests. Considered |
| 513 | // deriving the setting from the set service name, but it |
| 514 | // would be brittle if the name that's not 'default' is used |
| 515 | // for production purposes later on. |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 516 | ALOGI("Enabling Treble testing override"); |
Steven Moreland | 7c8af94 | 2020-07-08 18:35:51 +0000 | [diff] [blame] | 517 | android::hardware::details::setTrebleTestingOverride(true); |
Kalle Raita | a099a24 | 2017-01-11 11:17:29 -0800 | [diff] [blame] | 518 | } |
Ana Krulec | 3803b8d | 2020-02-03 16:35:46 -0800 | [diff] [blame] | 519 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 520 | // TODO (b/270966065) Update the HWC based refresh rate overlay to support spinner |
| 521 | mRefreshRateOverlaySpinner = property_get_bool("debug.sf.show_refresh_rate_overlay_spinner", 0); |
| 522 | mRefreshRateOverlayRenderRate = |
| 523 | property_get_bool("debug.sf.show_refresh_rate_overlay_render_rate", 0); |
| 524 | mRefreshRateOverlayShowInMiddle = |
| 525 | property_get_bool("debug.sf.show_refresh_rate_overlay_in_middle", 0); |
| 526 | |
| 527 | if (!mIsUserBuild && base::GetBoolProperty("debug.sf.enable_transaction_tracing"s, true)) { |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 528 | mTransactionTracing.emplace(); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 529 | mLayerTracing.setTransactionTracing(*mTransactionTracing); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 530 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 531 | |
| 532 | mIgnoreHdrCameraLayers = ignore_hdr_camera_layers(false); |
| 533 | |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 534 | // These are set by the HWC implementation to indicate that they will use the workarounds. |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 535 | mIsHdcpViaNegVsync = base::GetBoolProperty("debug.sf.hwc_hdcp_via_neg_vsync"s, false); |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | LatchUnsignaledConfig SurfaceFlinger::getLatchUnsignaledConfig() { |
Ady Abraham | d9e8d1b | 2022-02-25 00:36:10 +0000 | [diff] [blame] | 539 | if (base::GetBoolProperty("debug.sf.auto_latch_unsignaled"s, true)) { |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 540 | return LatchUnsignaledConfig::AutoSingleLayer; |
| 541 | } |
| 542 | |
| 543 | return LatchUnsignaledConfig::Disabled; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 546 | SurfaceFlinger::~SurfaceFlinger() = default; |
| 547 | |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 548 | void SurfaceFlinger::binderDied(const wp<IBinder>&) { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 549 | // the window manager died on us. prepare its eulogy. |
Rob Carr | 2aa78cb | 2020-03-10 14:27:49 -0700 | [diff] [blame] | 550 | mBootFinished = false; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 551 | |
Dominik Laskowski | b5f40e5 | 2022-09-29 13:25:05 -0400 | [diff] [blame] | 552 | static_cast<void>(mScheduler->schedule([this]() FTL_FAKE_GUARD(kMainThreadContext) { |
| 553 | // Sever the link to inputflinger since it's gone as well. |
| 554 | mInputFlinger.clear(); |
Vishnu Nair | b9df470 | 2021-03-01 11:53:15 -0800 | [diff] [blame] | 555 | |
Dominik Laskowski | b5f40e5 | 2022-09-29 13:25:05 -0400 | [diff] [blame] | 556 | initializeDisplays(); |
| 557 | })); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 558 | |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 559 | mInitBootPropsFuture.callOnce([this] { |
| 560 | return std::async(std::launch::async, &SurfaceFlinger::initBootProperties, this); |
| 561 | }); |
Dominik Laskowski | efb131e | 2024-02-16 10:57:40 -0500 | [diff] [blame] | 562 | |
| 563 | mInitBootPropsFuture.wait(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 566 | void SurfaceFlinger::run() { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 567 | mScheduler->run(); |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 570 | sp<IBinder> SurfaceFlinger::createVirtualDisplay(const std::string& displayName, bool isSecure, |
| 571 | const std::string& uniqueId, |
| 572 | float requestedRefreshRate) { |
Patrick Williams | e58a92b | 2024-02-29 14:52:25 -0600 | [diff] [blame] | 573 | // SurfaceComposerAIDL checks for some permissions, but adding an additional check here. |
| 574 | // 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] | 575 | // 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] | 576 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 577 | if (isSecure && uid != AID_ROOT && uid != AID_GRAPHICS && uid != AID_SYSTEM) { |
chaviw | d4a6164 | 2020-09-01 14:53:46 -0700 | [diff] [blame] | 578 | ALOGE("Only privileged processes can create a secure display"); |
| 579 | return nullptr; |
| 580 | } |
| 581 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 582 | class DisplayToken : public BBinder { |
| 583 | sp<SurfaceFlinger> flinger; |
| 584 | virtual ~DisplayToken() { |
| 585 | // no more references, this display must be terminated |
| 586 | Mutex::Autolock _l(flinger->mStateLock); |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 587 | flinger->mCurrentState.displays.removeItem(wp<IBinder>::fromExisting(this)); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 588 | flinger->setTransactionFlags(eDisplayTransactionNeeded); |
| 589 | } |
| 590 | public: |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 591 | explicit DisplayToken(const sp<SurfaceFlinger>& flinger) |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 592 | : flinger(flinger) { |
| 593 | } |
| 594 | }; |
| 595 | |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 596 | sp<BBinder> token = sp<DisplayToken>::make(sp<SurfaceFlinger>::fromExisting(this)); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 597 | |
| 598 | Mutex::Autolock _l(mStateLock); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 599 | // Display ID is assigned when virtual display is allocated by HWC. |
| 600 | DisplayDeviceState state; |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 601 | state.isSecure = isSecure; |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 602 | // Set display as protected when marked as secure to ensure no behavior change |
| 603 | // TODO (b/314820005): separate as a different arg when creating the display. |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 604 | state.isProtected = isSecure; |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 605 | state.displayName = displayName; |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 606 | state.uniqueId = uniqueId; |
Huihong Luo | 1768cb0 | 2022-10-11 11:10:34 -0700 | [diff] [blame] | 607 | state.requestedRefreshRate = Fps::fromValue(requestedRefreshRate); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 608 | mCurrentState.displays.add(token, state); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 609 | return token; |
| 610 | } |
| 611 | |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 612 | status_t SurfaceFlinger::destroyVirtualDisplay(const sp<IBinder>& displayToken) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 613 | Mutex::Autolock lock(mStateLock); |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 614 | |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 615 | const ssize_t index = mCurrentState.displays.indexOfKey(displayToken); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 616 | if (index < 0) { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 617 | ALOGE("%s: Invalid display token %p", __func__, displayToken.get()); |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 618 | return NAME_NOT_FOUND; |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 621 | const DisplayDeviceState& state = mCurrentState.displays.valueAt(index); |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 622 | if (state.physical) { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 623 | ALOGE("%s: Invalid operation on physical display", __func__); |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 624 | return INVALID_OPERATION; |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 625 | } |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 626 | mCurrentState.displays.removeItemsAt(index); |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 627 | setTransactionFlags(eDisplayTransactionNeeded); |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 628 | return NO_ERROR; |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 631 | void SurfaceFlinger::enableHalVirtualDisplays(bool enable) { |
| 632 | auto& generator = mVirtualDisplayIdGenerators.hal; |
| 633 | if (!generator && enable) { |
| 634 | ALOGI("Enabling HAL virtual displays"); |
| 635 | generator.emplace(getHwComposer().getMaxVirtualDisplayCount()); |
| 636 | } else if (generator && !enable) { |
| 637 | ALOGW_IF(generator->inUse(), "Disabling HAL virtual displays while in use"); |
| 638 | generator.reset(); |
| 639 | } |
| 640 | } |
| 641 | |
Dominik Laskowski | 263eec4 | 2021-07-21 23:13:24 -0700 | [diff] [blame] | 642 | VirtualDisplayId SurfaceFlinger::acquireVirtualDisplay(ui::Size resolution, |
| 643 | ui::PixelFormat format) { |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 644 | if (auto& generator = mVirtualDisplayIdGenerators.hal) { |
| 645 | if (const auto id = generator->generateId()) { |
Dominik Laskowski | 263eec4 | 2021-07-21 23:13:24 -0700 | [diff] [blame] | 646 | if (getHwComposer().allocateVirtualDisplay(*id, resolution, &format)) { |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 647 | return *id; |
| 648 | } |
| 649 | |
| 650 | generator->releaseId(*id); |
| 651 | } else { |
| 652 | ALOGW("%s: Exhausted HAL virtual displays", __func__); |
| 653 | } |
| 654 | |
| 655 | ALOGW("%s: Falling back to GPU virtual display", __func__); |
| 656 | } |
| 657 | |
| 658 | const auto id = mVirtualDisplayIdGenerators.gpu.generateId(); |
| 659 | LOG_ALWAYS_FATAL_IF(!id, "Failed to generate ID for GPU virtual display"); |
| 660 | return *id; |
| 661 | } |
| 662 | |
| 663 | void SurfaceFlinger::releaseVirtualDisplay(VirtualDisplayId displayId) { |
| 664 | if (const auto id = HalVirtualDisplayId::tryCast(displayId)) { |
| 665 | if (auto& generator = mVirtualDisplayIdGenerators.hal) { |
| 666 | generator->releaseId(*id); |
| 667 | } |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | const auto id = GpuVirtualDisplayId::tryCast(displayId); |
| 672 | LOG_ALWAYS_FATAL_IF(!id); |
| 673 | mVirtualDisplayIdGenerators.gpu.releaseId(*id); |
| 674 | } |
| 675 | |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 676 | std::vector<PhysicalDisplayId> SurfaceFlinger::getPhysicalDisplayIdsLocked() const { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 677 | std::vector<PhysicalDisplayId> displayIds; |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 678 | displayIds.reserve(mPhysicalDisplays.size()); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 679 | |
Dominik Laskowski | 3c36324 | 2022-04-07 10:44:12 -0700 | [diff] [blame] | 680 | const auto defaultDisplayId = getDefaultDisplayDeviceLocked()->getPhysicalId(); |
Ady Abraham | 2a0066d | 2021-07-15 20:16:58 -0700 | [diff] [blame] | 681 | displayIds.push_back(defaultDisplayId); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 682 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 683 | for (const auto& [id, display] : mPhysicalDisplays) { |
Ady Abraham | 2a0066d | 2021-07-15 20:16:58 -0700 | [diff] [blame] | 684 | if (id != defaultDisplayId) { |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 685 | displayIds.push_back(id); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 686 | } |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 687 | } |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 688 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 689 | return displayIds; |
| 690 | } |
| 691 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 692 | std::optional<PhysicalDisplayId> SurfaceFlinger::getPhysicalDisplayIdLocked( |
| 693 | const sp<display::DisplayToken>& displayToken) const { |
| 694 | return ftl::find_if(mPhysicalDisplays, PhysicalDisplay::hasToken(displayToken)) |
| 695 | .transform(&ftl::to_key<PhysicalDisplays>); |
| 696 | } |
| 697 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 698 | sp<IBinder> SurfaceFlinger::getPhysicalDisplayToken(PhysicalDisplayId displayId) const { |
| 699 | Mutex::Autolock lock(mStateLock); |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 700 | return getPhysicalDisplayTokenLocked(displayId); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 703 | HWComposer& SurfaceFlinger::getHwComposer() const { |
| 704 | return mCompositionEngine->getHwComposer(); |
| 705 | } |
| 706 | |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 707 | renderengine::RenderEngine& SurfaceFlinger::getRenderEngine() const { |
Patrick Williams | 0a525a4 | 2022-10-26 20:20:50 +0000 | [diff] [blame] | 708 | return *mRenderEngine; |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 711 | compositionengine::CompositionEngine& SurfaceFlinger::getCompositionEngine() const { |
| 712 | return *mCompositionEngine.get(); |
| 713 | } |
| 714 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 715 | void SurfaceFlinger::bootFinished() { |
Rob Carr | 2aa78cb | 2020-03-10 14:27:49 -0700 | [diff] [blame] | 716 | if (mBootFinished == true) { |
| 717 | ALOGE("Extra call to bootFinished"); |
| 718 | return; |
| 719 | } |
| 720 | mBootFinished = true; |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame] | 721 | FlagManager::getMutableInstance().markBootCompleted(); |
Dominik Laskowski | efb131e | 2024-02-16 10:57:40 -0500 | [diff] [blame] | 722 | |
Vishnu Nair | 47b6e68 | 2024-07-12 18:46:07 +0000 | [diff] [blame] | 723 | ::tracing_perfetto::registerWithPerfetto(); |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 724 | mInitBootPropsFuture.wait(); |
| 725 | mRenderEnginePrimeCacheFuture.wait(); |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 726 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | const nsecs_t now = systemTime(); |
| 728 | const nsecs_t duration = now - mBootTime; |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 729 | ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) ); |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 730 | |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 731 | mFrameTracer->initialize(); |
Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 732 | mFrameTimeline->onBootFinished(); |
Ady Abraham | 53b0c49 | 2023-10-23 11:47:14 -0700 | [diff] [blame] | 733 | getRenderEngine().setEnableTracing(FlagManager::getInstance().use_skia_tracing()); |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 734 | |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 735 | // wait patiently for the window manager death |
| 736 | const String16 name("window"); |
Cody Heiner | bcfa7fa | 2023-04-17 18:34:30 -0700 | [diff] [blame] | 737 | mWindowManager = defaultServiceManager()->waitForService(name); |
Steven Moreland | a904bb9 | 2019-07-02 17:37:23 -0700 | [diff] [blame] | 738 | if (mWindowManager != 0) { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 739 | mWindowManager->linkToDeath(sp<IBinder::DeathRecipient>::fromExisting(this)); |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | // stop boot animation |
Mathias Agopian | a67e418 | 2012-06-19 17:26:12 -0700 | [diff] [blame] | 743 | // formerly we would just kill the process, but we now ask it to exit so it |
| 744 | // can choose where to stop the animation. |
| 745 | property_set("service.bootanim.exit", "1"); |
Yusuke Sato | 0a688f5 | 2015-07-30 23:05:39 -0700 | [diff] [blame] | 746 | |
| 747 | const int LOGTAG_SF_STOP_BOOTANIM = 60110; |
| 748 | LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM, |
| 749 | ns2ms(systemTime(SYSTEM_TIME_MONOTONIC))); |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 750 | |
Cody Heiner | bcfa7fa | 2023-04-17 18:34:30 -0700 | [diff] [blame] | 751 | sp<IBinder> input(defaultServiceManager()->waitForService(String16("inputflinger"))); |
Denis Hsu | e70cc6c | 2020-06-17 10:17:30 +0800 | [diff] [blame] | 752 | |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 753 | static_cast<void>(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(kMainThreadContext) { |
Denis Hsu | e70cc6c | 2020-06-17 10:17:30 +0800 | [diff] [blame] | 754 | if (input == nullptr) { |
| 755 | ALOGE("Failed to link to input service"); |
| 756 | } else { |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 757 | mInputFlinger = interface_cast<os::IInputFlinger>(input); |
Denis Hsu | e70cc6c | 2020-06-17 10:17:30 +0800 | [diff] [blame] | 758 | } |
| 759 | |
Chia-I Wu | 14c9c7b | 2018-06-26 10:18:18 +0800 | [diff] [blame] | 760 | readPersistentProperties(); |
Ady Abraham | 53b0c49 | 2023-10-23 11:47:14 -0700 | [diff] [blame] | 761 | const bool hintSessionEnabled = FlagManager::getInstance().use_adpf_cpu_hint(); |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 762 | mPowerAdvisor->enablePowerHintSession(hintSessionEnabled); |
| 763 | const bool hintSessionUsed = mPowerAdvisor->usePowerHintSession(); |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 764 | // Ordering is important here, as onBootFinished signals to PowerAdvisor that concurrency |
| 765 | // is safe because its variables are initialized. |
| 766 | mPowerAdvisor->onBootFinished(); |
Matt Buckley | 618c43f | 2022-08-12 18:09:48 +0000 | [diff] [blame] | 767 | ALOGD("Power hint is %s", |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 768 | hintSessionUsed ? "supported" : (hintSessionEnabled ? "unsupported" : "disabled")); |
| 769 | if (hintSessionUsed) { |
Matt Buckley | 618c43f | 2022-08-12 18:09:48 +0000 | [diff] [blame] | 770 | std::optional<pid_t> renderEngineTid = getRenderEngine().getRenderEngineTid(); |
| 771 | std::vector<int32_t> tidList; |
| 772 | tidList.emplace_back(gettid()); |
| 773 | if (renderEngineTid.has_value()) { |
| 774 | tidList.emplace_back(*renderEngineTid); |
| 775 | } |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 776 | if (!mPowerAdvisor->startPowerHintSession(std::move(tidList))) { |
Matt Buckley | 618c43f | 2022-08-12 18:09:48 +0000 | [diff] [blame] | 777 | ALOGW("Cannot start power hint session"); |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 778 | } |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Chia-I Wu | 14c9c7b | 2018-06-26 10:18:18 +0800 | [diff] [blame] | 781 | mBootStage = BootStage::FINISHED; |
Ana Krulec | bd6654b | 2019-02-15 15:18:15 -0800 | [diff] [blame] | 782 | |
Dominik Laskowski | 02bb207 | 2022-08-26 15:02:52 -0700 | [diff] [blame] | 783 | if (base::GetBoolProperty("sf.debug.show_refresh_rate_overlay"s, false)) { |
| 784 | ftl::FakeGuard guard(mStateLock); |
| 785 | enableRefreshRateOverlay(true); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 786 | } |
Chia-I Wu | 14c9c7b | 2018-06-26 10:18:18 +0800 | [diff] [blame] | 787 | })); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | } |
| 789 | |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 790 | void chooseRenderEngineType(renderengine::RenderEngineCreationArgs::Builder& builder) { |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 791 | char prop[PROPERTY_VALUE_MAX]; |
Leon Scroggins III | f840ae5 | 2024-01-25 15:10:12 -0500 | [diff] [blame] | 792 | property_get(PROPERTY_DEBUG_RENDERENGINE_BACKEND, prop, ""); |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 793 | |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 794 | // TODO: b/293371537 - Once GraphiteVk is deemed relatively stable, log a warning that |
| 795 | // PROPERTY_DEBUG_RENDERENGINE_BACKEND is deprecated |
Alec Mouri | e2b61c6 | 2023-08-15 19:04:54 +0000 | [diff] [blame] | 796 | if (strcmp(prop, "skiagl") == 0) { |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 797 | builder.setThreaded(renderengine::RenderEngine::Threaded::NO) |
| 798 | .setGraphicsApi(renderengine::RenderEngine::GraphicsApi::GL); |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 799 | } else if (strcmp(prop, "skiaglthreaded") == 0) { |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 800 | builder.setThreaded(renderengine::RenderEngine::Threaded::YES) |
| 801 | .setGraphicsApi(renderengine::RenderEngine::GraphicsApi::GL); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 802 | } else if (strcmp(prop, "skiavk") == 0) { |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 803 | builder.setThreaded(renderengine::RenderEngine::Threaded::NO) |
| 804 | .setGraphicsApi(renderengine::RenderEngine::GraphicsApi::VK); |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 805 | } else if (strcmp(prop, "skiavkthreaded") == 0) { |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 806 | builder.setThreaded(renderengine::RenderEngine::Threaded::YES) |
| 807 | .setGraphicsApi(renderengine::RenderEngine::GraphicsApi::VK); |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 808 | } else { |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 809 | const auto kVulkan = renderengine::RenderEngine::GraphicsApi::VK; |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 810 | // TODO: b/341728634 - Clean up conditional compilation. |
| 811 | // Note: this guard in particular must check e.g. |
| 812 | // COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS_GRAPHITE_RENDERENGINE directly (instead of calling e.g. |
| 813 | // COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS(GRAPHITE_RENDERENGINE)) because that macro is undefined |
| 814 | // in the libsurfaceflingerflags_test variant of com_android_graphics_surfaceflinger_flags.h, which |
| 815 | // is used by layertracegenerator (which also needs SurfaceFlinger.cpp). :) |
| 816 | #if COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS_GRAPHITE_RENDERENGINE || \ |
| 817 | COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS_FORCE_COMPILE_GRAPHITE_RENDERENGINE |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 818 | const bool useGraphite = FlagManager::getInstance().graphite_renderengine() && |
| 819 | renderengine::RenderEngine::canSupport(kVulkan); |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 820 | #else |
| 821 | const bool useGraphite = false; |
| 822 | if (FlagManager::getInstance().graphite_renderengine()) { |
| 823 | ALOGE("RenderEngine's Graphite Skia backend was requested with the " |
| 824 | "debug.renderengine.graphite system property, but it is not compiled in this " |
| 825 | "build! Falling back to Ganesh backend selection logic."); |
| 826 | } |
| 827 | #endif |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 828 | const bool useVulkan = useGraphite || |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 829 | (FlagManager::getInstance().vulkan_renderengine() && |
| 830 | renderengine::RenderEngine::canSupport(kVulkan)); |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 831 | |
| 832 | builder.setSkiaBackend(useGraphite ? renderengine::RenderEngine::SkiaBackend::GRAPHITE |
| 833 | : renderengine::RenderEngine::SkiaBackend::GANESH); |
Leon Scroggins III | f3369ed | 2024-02-16 17:52:42 -0500 | [diff] [blame] | 834 | builder.setGraphicsApi(useVulkan ? kVulkan : renderengine::RenderEngine::GraphicsApi::GL); |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
Robin Lee | 7338bd9 | 2024-04-04 14:05:07 +0000 | [diff] [blame] | 838 | /** |
| 839 | * Choose a suggested blurring algorithm if supportsBlur is true. By default Kawase will be |
| 840 | * suggested as it's faster than a full Gaussian blur and looks close enough. |
| 841 | */ |
| 842 | renderengine::RenderEngine::BlurAlgorithm chooseBlurAlgorithm(bool supportsBlur) { |
| 843 | if (!supportsBlur) { |
| 844 | return renderengine::RenderEngine::BlurAlgorithm::NONE; |
| 845 | } |
| 846 | |
| 847 | auto const algorithm = base::GetProperty(PROPERTY_DEBUG_RENDERENGINE_BLUR_ALGORITHM, ""); |
| 848 | if (algorithm == "gaussian") { |
| 849 | return renderengine::RenderEngine::BlurAlgorithm::GAUSSIAN; |
Robin Lee | 2631520 | 2021-08-10 22:54:51 +0200 | [diff] [blame] | 850 | } else if (algorithm == "kawase2") { |
| 851 | return renderengine::RenderEngine::BlurAlgorithm::KAWASE_DUAL_FILTER; |
Robin Lee | 7338bd9 | 2024-04-04 14:05:07 +0000 | [diff] [blame] | 852 | } else { |
| 853 | return renderengine::RenderEngine::BlurAlgorithm::KAWASE; |
| 854 | } |
| 855 | } |
| 856 | |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 857 | void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 858 | SFTRACE_CALL(); |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 859 | ALOGI( "SurfaceFlinger's main thread ready to run. " |
| 860 | "Initializing graphics H/W..."); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 861 | addTransactionReadyFilters(); |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 862 | Mutex::Autolock lock(mStateLock); |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 863 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 864 | // Get a RenderEngine for the given display / config (can't fail) |
Peiyong Lin | 0256f72 | 2018-08-31 15:45:10 -0700 | [diff] [blame] | 865 | // 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] | 866 | // Sending maxFrameBufferAcquiredBuffers as the cache size is tightly tuned to single-display. |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 867 | auto builder = renderengine::RenderEngineCreationArgs::Builder() |
| 868 | .setPixelFormat(static_cast<int32_t>(defaultCompositionPixelFormat)) |
| 869 | .setImageCacheSize(maxFrameBufferAcquiredBuffers) |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 870 | .setEnableProtectedContext(enable_protected_contents(false)) |
| 871 | .setPrecacheToneMapperShaderOnly(false) |
Robin Lee | 7338bd9 | 2024-04-04 14:05:07 +0000 | [diff] [blame] | 872 | .setBlurAlgorithm(chooseBlurAlgorithm(mSupportsBlur)) |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 873 | .setContextPriority( |
| 874 | useContextPriority |
| 875 | ? renderengine::RenderEngine::ContextPriority::REALTIME |
| 876 | : renderengine::RenderEngine::ContextPriority::MEDIUM); |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 877 | chooseRenderEngineType(builder); |
Patrick Williams | 0a525a4 | 2022-10-26 20:20:50 +0000 | [diff] [blame] | 878 | mRenderEngine = renderengine::RenderEngine::create(builder.build()); |
| 879 | mCompositionEngine->setRenderEngine(mRenderEngine.get()); |
Garfield Tan | 9c9c191 | 2021-07-19 12:02:16 -0700 | [diff] [blame] | 880 | mMaxRenderTargetSize = |
| 881 | std::min(getRenderEngine().getMaxTextureSize(), getRenderEngine().getMaxViewportDims()); |
Wei Wang | 976a645 | 2021-06-11 15:26:00 -0700 | [diff] [blame] | 882 | |
| 883 | // Set SF main policy after initializing RenderEngine which has its own policy. |
| 884 | if (!SetTaskProfiles(0, {"SFMainPolicy"})) { |
| 885 | ALOGW("Failed to set main task profile"); |
| 886 | } |
| 887 | |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 888 | mCompositionEngine->setTimeStats(mTimeStats); |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 889 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 890 | mCompositionEngine->setHwComposer(getFactory().createHWComposer(mHwcServiceName)); |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 891 | auto& composer = mCompositionEngine->getHwComposer(); |
| 892 | composer.setCallback(*this); |
| 893 | mDisplayModeController.setHwComposer(&composer); |
| 894 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 895 | ClientCache::getInstance().setRenderEngine(&getRenderEngine()); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 896 | |
Dominik Laskowski | e62606d | 2024-02-22 11:42:13 -0500 | [diff] [blame] | 897 | mHasReliablePresentFences = |
| 898 | !getHwComposer().hasCapability(Capability::PRESENT_FENCE_IS_NOT_RELIABLE); |
| 899 | |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 900 | enableLatchUnsignaledConfig = getLatchUnsignaledConfig(); |
| 901 | |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 902 | if (base::GetBoolProperty("debug.sf.enable_hwc_vds"s, false)) { |
| 903 | enableHalVirtualDisplays(true); |
| 904 | } |
| 905 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 906 | // Process hotplug for displays connected at boot. |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 907 | LOG_ALWAYS_FATAL_IF(!configureLocked(), |
| 908 | "Initial display configuration failed: HWC did not hotplug"); |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 909 | |
Dominik Laskowski | a0858db | 2024-07-23 16:45:11 -0400 | [diff] [blame] | 910 | mActiveDisplayId = getPrimaryDisplayIdLocked(); |
| 911 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 912 | // Commit primary display. |
| 913 | sp<const DisplayDevice> display; |
Dominik Laskowski | a0858db | 2024-07-23 16:45:11 -0400 | [diff] [blame] | 914 | if (const auto indexOpt = mCurrentState.getDisplayIndex(mActiveDisplayId)) { |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 915 | const auto& displays = mCurrentState.displays; |
| 916 | |
| 917 | const auto& token = displays.keyAt(*indexOpt); |
| 918 | const auto& state = displays.valueAt(*indexOpt); |
| 919 | |
| 920 | processDisplayAdded(token, state); |
| 921 | mDrawingState.displays.add(token, state); |
| 922 | |
| 923 | display = getDefaultDisplayDeviceLocked(); |
| 924 | } |
| 925 | |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 926 | LOG_ALWAYS_FATAL_IF(!display, "Failed to configure the primary display"); |
| 927 | LOG_ALWAYS_FATAL_IF(!getHwComposer().isConnected(display->getPhysicalId()), |
| 928 | "Primary display is disconnected"); |
Jesse Hall | 692c723 | 2012-11-08 15:41:56 -0800 | [diff] [blame] | 929 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 930 | // TODO(b/241285876): The Scheduler needlessly depends on creating the CompositionEngine part of |
| 931 | // the DisplayDevice, hence the above commit of the primary display. Remove that special case by |
| 932 | // initializing the Scheduler after configureLocked, once decoupled from DisplayDevice. |
| 933 | initScheduler(display); |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 934 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 935 | // Start listening after creating the Scheduler, since the listener calls into it. |
| 936 | mDisplayModeController.setActiveModeListener( |
| 937 | display::DisplayModeController::ActiveModeListener::make( |
| 938 | [this](PhysicalDisplayId displayId, Fps vsyncRate, Fps renderRate) { |
| 939 | // This callback cannot lock mStateLock, as some callers already lock it. |
| 940 | // Instead, switch context to the main thread. |
| 941 | static_cast<void>(mScheduler->schedule([=, |
| 942 | this]() FTL_FAKE_GUARD(mStateLock) { |
| 943 | if (const auto display = getDisplayDeviceLocked(displayId)) { |
| 944 | display->updateRefreshRateOverlayRate(vsyncRate, renderRate); |
| 945 | } |
| 946 | })); |
| 947 | })); |
| 948 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 949 | mLayerTracing.setTakeLayersSnapshotProtoFunction([&](uint32_t traceFlags) { |
| 950 | auto snapshot = perfetto::protos::LayersSnapshotProto{}; |
| 951 | mScheduler |
| 952 | ->schedule([&]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD(kMainThreadContext) { |
| 953 | snapshot = takeLayersSnapshotProto(traceFlags, TimePoint::now(), |
| 954 | mLastCommittedVsyncId, true); |
| 955 | }) |
| 956 | .wait(); |
| 957 | return snapshot; |
| 958 | }); |
| 959 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 960 | // Commit secondary display(s). |
| 961 | processDisplayChangesLocked(); |
| 962 | |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 963 | // initialize our drawing state |
| 964 | mDrawingState = mCurrentState; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 965 | |
Dominik Laskowski | a39e32f | 2023-02-08 16:40:13 -0500 | [diff] [blame] | 966 | onActiveDisplayChangedLocked(nullptr, *display); |
Dominik Laskowski | b5f40e5 | 2022-09-29 13:25:05 -0400 | [diff] [blame] | 967 | |
| 968 | static_cast<void>(mScheduler->schedule( |
| 969 | [this]() FTL_FAKE_GUARD(kMainThreadContext) { initializeDisplays(); })); |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 970 | |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 971 | mPowerAdvisor->init(); |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 972 | |
Bruno BELANYI | fd02878 | 2023-10-13 14:39:53 +0000 | [diff] [blame] | 973 | if (base::GetBoolProperty("service.sf.prime_shader_cache"s, true)) { |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 974 | if (setSchedFifo(false) != NO_ERROR) { |
| 975 | ALOGW("Can't set SCHED_OTHER for primeCache"); |
| 976 | } |
| 977 | |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 978 | mRenderEnginePrimeCacheFuture.callOnce([this] { |
Russell Myers | 3348c74 | 2024-04-29 20:22:42 +0000 | [diff] [blame] | 979 | renderengine::PrimeCacheConfig config; |
| 980 | config.cacheHolePunchLayer = |
| 981 | base::GetBoolProperty("debug.sf.prime_shader_cache.hole_punch"s, true); |
| 982 | config.cacheSolidLayers = |
| 983 | base::GetBoolProperty("debug.sf.prime_shader_cache.solid_layers"s, true); |
| 984 | config.cacheSolidDimmedLayers = |
| 985 | base::GetBoolProperty("debug.sf.prime_shader_cache.solid_dimmed_layers"s, true); |
| 986 | config.cacheImageLayers = |
| 987 | base::GetBoolProperty("debug.sf.prime_shader_cache.image_layers"s, true); |
| 988 | config.cacheImageDimmedLayers = |
| 989 | base::GetBoolProperty("debug.sf.prime_shader_cache.image_dimmed_layers"s, true); |
| 990 | config.cacheClippedLayers = |
| 991 | base::GetBoolProperty("debug.sf.prime_shader_cache.clipped_layers"s, true); |
| 992 | config.cacheShadowLayers = |
| 993 | base::GetBoolProperty("debug.sf.prime_shader_cache.shadow_layers"s, true); |
| 994 | config.cachePIPImageLayers = |
| 995 | base::GetBoolProperty("debug.sf.prime_shader_cache.pip_image_layers"s, true); |
| 996 | config.cacheTransparentImageDimmedLayers = base:: |
| 997 | GetBoolProperty("debug.sf.prime_shader_cache.transparent_image_dimmed_layers"s, |
| 998 | true); |
| 999 | config.cacheClippedDimmedImageLayers = base:: |
| 1000 | GetBoolProperty("debug.sf.prime_shader_cache.clipped_dimmed_image_layers"s, |
| 1001 | true); |
| 1002 | // ro.surface_flinger.prime_chader_cache.ultrahdr exists as a previous ro property |
| 1003 | // which we maintain for backwards compatibility. |
| 1004 | config.cacheUltraHDR = |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 1005 | base::GetBoolProperty("ro.surface_flinger.prime_shader_cache.ultrahdr"s, false); |
Russell Myers | 3348c74 | 2024-04-29 20:22:42 +0000 | [diff] [blame] | 1006 | return getRenderEngine().primeCache(config); |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 1007 | }); |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 1008 | |
| 1009 | if (setSchedFifo(true) != NO_ERROR) { |
linkai | d696a4f | 2023-11-14 10:13:36 +0800 | [diff] [blame] | 1010 | ALOGW("Can't set SCHED_FIFO after primeCache"); |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 1011 | } |
Steven Thomas | 137d4bc | 2019-07-25 16:55:14 -0700 | [diff] [blame] | 1012 | } |
Dan Stoza | 4e63777 | 2016-07-28 13:31:51 -0700 | [diff] [blame] | 1013 | |
Dominik Laskowski | efb131e | 2024-02-16 10:57:40 -0500 | [diff] [blame] | 1014 | // Avoid blocking the main thread on `init` to set properties. |
Dominik Laskowski | d4a2275 | 2024-03-08 17:39:04 -0500 | [diff] [blame] | 1015 | mInitBootPropsFuture.callOnce([this] { |
| 1016 | return std::async(std::launch::async, &SurfaceFlinger::initBootProperties, this); |
| 1017 | }); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 1019 | initTransactionTraceWriter(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1020 | ALOGV("Done initializing"); |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Dominik Laskowski | efb131e | 2024-02-16 10:57:40 -0500 | [diff] [blame] | 1023 | // During boot, offload `initBootProperties` to another thread. `property_set` depends on |
| 1024 | // `property_service`, which may be delayed by slow operations like `mount_all --late` in |
| 1025 | // the `init` process. See b/34499826 and b/63844978. |
| 1026 | void SurfaceFlinger::initBootProperties() { |
| 1027 | property_set("service.sf.present_timestamp", mHasReliablePresentFences ? "1" : "0"); |
| 1028 | |
Dominik Laskowski | b0fdfe7 | 2024-02-21 10:42:23 -0500 | [diff] [blame] | 1029 | if (base::GetBoolProperty("debug.sf.boot_animation"s, true)) { |
| 1030 | // Reset and (if needed) start BootAnimation. |
| 1031 | property_set("service.bootanim.exit", "0"); |
| 1032 | property_set("service.bootanim.progress", "0"); |
| 1033 | property_set("ctl.start", "bootanim"); |
| 1034 | } |
Dominik Laskowski | efb131e | 2024-02-16 10:57:40 -0500 | [diff] [blame] | 1035 | } |
| 1036 | |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 1037 | void SurfaceFlinger::initTransactionTraceWriter() { |
| 1038 | if (!mTransactionTracing) { |
| 1039 | return; |
| 1040 | } |
| 1041 | TransactionTraceWriter::getInstance().setWriterFunction( |
| 1042 | [&](const std::string& filename, bool overwrite) { |
| 1043 | auto writeFn = [&]() { |
Patrick Williams | 8b731c1 | 2023-10-24 09:56:24 -0500 | [diff] [blame] | 1044 | if (!overwrite && fileNewerThan(filename, std::chrono::minutes{10})) { |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 1045 | ALOGD("TransactionTraceWriter: file=%s already exists", filename.c_str()); |
| 1046 | return; |
| 1047 | } |
Wenhui Yang | eafc18a | 2024-05-14 17:15:52 +0000 | [diff] [blame] | 1048 | ALOGD("TransactionTraceWriter: writing file=%s", filename.c_str()); |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 1049 | mTransactionTracing->writeToFile(filename); |
Wenhui Yang | eafc18a | 2024-05-14 17:15:52 +0000 | [diff] [blame] | 1050 | mTransactionTracing->flush(); |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 1051 | }; |
| 1052 | if (std::this_thread::get_id() == mMainThreadId) { |
| 1053 | writeFn(); |
| 1054 | } else { |
| 1055 | mScheduler->schedule(writeFn).get(); |
| 1056 | } |
| 1057 | }); |
| 1058 | } |
| 1059 | |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 1060 | void SurfaceFlinger::readPersistentProperties() { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 1061 | Mutex::Autolock _l(mStateLock); |
| 1062 | |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 1063 | char value[PROPERTY_VALUE_MAX]; |
| 1064 | |
| 1065 | property_get("persist.sys.sf.color_saturation", value, "1.0"); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1066 | mGlobalSaturationFactor = atof(value); |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 1067 | updateColorMatrixLocked(); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1068 | ALOGV("Saturation is set to %.2f", mGlobalSaturationFactor); |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 1069 | |
| 1070 | property_get("persist.sys.sf.native_mode", value, "0"); |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 1071 | mDisplayColorSetting = static_cast<DisplayColorSetting>(atoi(value)); |
Peiyong Lin | a3ea559 | 2019-02-10 14:45:00 -0800 | [diff] [blame] | 1072 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1073 | mForceColorMode = |
| 1074 | 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] | 1075 | } |
| 1076 | |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 1077 | status_t SurfaceFlinger::getSupportedFrameTimestamps( |
| 1078 | std::vector<FrameEvent>* outSupported) const { |
| 1079 | *outSupported = { |
| 1080 | FrameEvent::REQUESTED_PRESENT, |
| 1081 | FrameEvent::ACQUIRE, |
| 1082 | FrameEvent::LATCH, |
| 1083 | FrameEvent::FIRST_REFRESH_START, |
| 1084 | FrameEvent::LAST_REFRESH_START, |
| 1085 | FrameEvent::GPU_COMPOSITION_DONE, |
| 1086 | FrameEvent::DEQUEUE_READY, |
| 1087 | FrameEvent::RELEASE, |
| 1088 | }; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 1089 | |
Dominik Laskowski | e62606d | 2024-02-22 11:42:13 -0500 | [diff] [blame] | 1090 | if (mHasReliablePresentFences) { |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 1091 | outSupported->push_back(FrameEvent::DISPLAY_PRESENT); |
| 1092 | } |
| 1093 | return NO_ERROR; |
| 1094 | } |
| 1095 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1096 | status_t SurfaceFlinger::getDisplayState(const sp<IBinder>& displayToken, ui::DisplayState* state) { |
| 1097 | if (!displayToken || !state) { |
| 1098 | return BAD_VALUE; |
| 1099 | } |
| 1100 | |
| 1101 | Mutex::Autolock lock(mStateLock); |
| 1102 | |
| 1103 | const auto display = getDisplayDeviceLocked(displayToken); |
| 1104 | if (!display) { |
| 1105 | return NAME_NOT_FOUND; |
| 1106 | } |
| 1107 | |
| 1108 | state->layerStack = display->getLayerStack(); |
| 1109 | state->orientation = display->getOrientation(); |
| 1110 | |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 1111 | const Rect layerStackRect = display->getLayerStackSpaceRect(); |
| 1112 | state->layerStackSpaceRect = |
| 1113 | layerStackRect.isValid() ? layerStackRect.getSize() : display->getSize(); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1114 | |
| 1115 | return NO_ERROR; |
| 1116 | } |
| 1117 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 1118 | status_t SurfaceFlinger::getStaticDisplayInfo(int64_t displayId, ui::StaticDisplayInfo* info) { |
| 1119 | if (!info) { |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1120 | return BAD_VALUE; |
| 1121 | } |
| 1122 | |
| 1123 | Mutex::Autolock lock(mStateLock); |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 1124 | const auto id = DisplayId::fromValue<PhysicalDisplayId>(static_cast<uint64_t>(displayId)); |
| 1125 | const auto displayOpt = mPhysicalDisplays.get(*id).and_then(getDisplayDeviceAndSnapshot()); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1126 | |
| 1127 | if (!displayOpt) { |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1128 | return NAME_NOT_FOUND; |
| 1129 | } |
| 1130 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1131 | const auto& [display, snapshotRef] = *displayOpt; |
| 1132 | const auto& snapshot = snapshotRef.get(); |
| 1133 | |
| 1134 | info->connectionType = snapshot.connectionType(); |
| 1135 | info->deviceProductInfo = snapshot.deviceProductInfo(); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1136 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1137 | if (mEmulatedDisplayDensity) { |
| 1138 | info->density = mEmulatedDisplayDensity; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1139 | } else { |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1140 | info->density = info->connectionType == ui::DisplayConnectionType::Internal |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1141 | ? mInternalDisplayDensity |
Dominik Laskowski | 55c8540 | 2020-01-21 16:25:47 -0800 | [diff] [blame] | 1142 | : FALLBACK_DENSITY; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1143 | } |
Dominik Laskowski | d125d0e | 2020-05-08 12:36:39 -0700 | [diff] [blame] | 1144 | info->density /= ACONFIGURATION_DENSITY_MEDIUM; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1145 | |
| 1146 | info->secure = display->isSecure(); |
ramindani | 06e518e | 2022-03-14 18:47:53 +0000 | [diff] [blame] | 1147 | info->installOrientation = display->getPhysicalOrientation(); |
Vishnu Nair | d0a8965 | 2022-01-13 12:05:54 -0800 | [diff] [blame] | 1148 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1149 | return NO_ERROR; |
| 1150 | } |
| 1151 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 1152 | void SurfaceFlinger::getDynamicDisplayInfoInternal(ui::DynamicDisplayInfo*& info, |
| 1153 | const sp<DisplayDevice>& display, |
| 1154 | const display::DisplaySnapshot& snapshot) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1155 | const auto& displayModes = snapshot.displayModes(); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1156 | info->supportedDisplayModes.clear(); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1157 | info->supportedDisplayModes.reserve(displayModes.size()); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1158 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1159 | for (const auto& [id, mode] : displayModes) { |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1160 | ui::DisplayMode outMode; |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1161 | outMode.id = ftl::to_underlying(id); |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 1162 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1163 | auto [width, height] = mode->getResolution(); |
| 1164 | auto [xDpi, yDpi] = mode->getDpi(); |
Yiwei Zhang | 27de5df | 2018-08-23 17:04:51 -0700 | [diff] [blame] | 1165 | |
ramindani | 06e518e | 2022-03-14 18:47:53 +0000 | [diff] [blame] | 1166 | if (const auto physicalOrientation = display->getPhysicalOrientation(); |
| 1167 | physicalOrientation == ui::ROTATION_90 || physicalOrientation == ui::ROTATION_270) { |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1168 | std::swap(width, height); |
| 1169 | std::swap(xDpi, yDpi); |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1172 | outMode.resolution = ui::Size(width, height); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 1173 | |
Huan Song | f7eeb10 | 2022-03-28 11:02:38 -0700 | [diff] [blame] | 1174 | outMode.xDpi = xDpi; |
| 1175 | outMode.yDpi = yDpi; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1176 | |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 1177 | const auto peakFps = mode->getPeakFps(); |
Alec Mouri | 55e3103 | 2023-10-02 20:34:18 +0000 | [diff] [blame] | 1178 | outMode.peakRefreshRate = peakFps.getValue(); |
Alec Mouri | d122a1c | 2023-09-30 01:36:33 +0000 | [diff] [blame] | 1179 | outMode.vsyncRate = mode->getVsyncRate().getValue(); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 1180 | |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 1181 | const auto vsyncConfigSet = mScheduler->getVsyncConfiguration().getConfigsForRefreshRate( |
Alec Mouri | 55e3103 | 2023-10-02 20:34:18 +0000 | [diff] [blame] | 1182 | Fps::fromValue(outMode.peakRefreshRate)); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1183 | outMode.appVsyncOffset = vsyncConfigSet.late.appOffset; |
| 1184 | outMode.sfVsyncOffset = vsyncConfigSet.late.sfOffset; |
| 1185 | outMode.group = mode->getGroup(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1186 | |
Andy McFadden | 91b2ca8 | 2014-06-13 14:04:23 -0700 | [diff] [blame] | 1187 | // This is how far in advance a buffer must be queued for |
| 1188 | // presentation at a given time. If you want a buffer to appear |
| 1189 | // on the screen at time N, you must submit the buffer before |
| 1190 | // (N - presentationDeadline). |
| 1191 | // |
| 1192 | // Normally it's one full refresh period (to give SF a chance to |
| 1193 | // latch the buffer), but this can be reduced by configuring a |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 1194 | // VsyncController offset. Any additional delays introduced by the hardware |
Andy McFadden | 91b2ca8 | 2014-06-13 14:04:23 -0700 | [diff] [blame] | 1195 | // composer or panel must be accounted for here. |
| 1196 | // |
| 1197 | // We add an additional 1ms to allow for processing time and |
| 1198 | // differences between the ideal and actual refresh rate. |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 1199 | outMode.presentationDeadline = peakFps.getPeriodNsecs() - outMode.sfVsyncOffset + 1000000; |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 1200 | excludeDolbyVisionIf4k30Present(display->getHdrCapabilities().getSupportedHdrTypes(), |
| 1201 | outMode); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1202 | info->supportedDisplayModes.push_back(outMode); |
Mathias Agopian | 8b736f1 | 2012-08-13 17:54:26 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1205 | info->supportedColorModes = snapshot.filterColorModes(mSupportsWideColor); |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1206 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1207 | const PhysicalDisplayId displayId = snapshot.displayId(); |
| 1208 | |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 1209 | const auto mode = display->refreshRateSelector().getActiveMode(); |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1210 | info->activeDisplayModeId = ftl::to_underlying(mode.modePtr->getId()); |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 1211 | info->renderFrameRate = mode.fps.getValue(); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 1212 | info->activeColorMode = display->getCompositionDisplay()->getState().colorMode; |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 1213 | info->hdrCapabilities = filterOut4k30(display->getHdrCapabilities()); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1214 | |
Marin Shalamanov | b173f75 | 2021-02-16 19:38:36 +0100 | [diff] [blame] | 1215 | info->autoLowLatencyModeSupported = |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1216 | getHwComposer().hasDisplayCapability(displayId, |
Leon Scroggins III | 5967aec | 2021-12-29 11:14:22 -0500 | [diff] [blame] | 1217 | DisplayCapability::AUTO_LOW_LATENCY_MODE); |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1218 | info->gameContentTypeSupported = |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1219 | getHwComposer().supportsContentType(displayId, hal::ContentType::GAME); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1220 | |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1221 | info->preferredBootDisplayMode = static_cast<ui::DisplayModeId>(-1); |
Kriti Dang | ac0a74d | 2022-03-16 11:52:35 +0100 | [diff] [blame] | 1222 | |
| 1223 | if (getHwComposer().hasCapability(Capability::BOOT_DISPLAY_CONFIG)) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1224 | if (const auto hwcId = getHwComposer().getPreferredBootDisplayMode(displayId)) { |
| 1225 | if (const auto modeId = snapshot.translateModeId(*hwcId)) { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1226 | info->preferredBootDisplayMode = ftl::to_underlying(*modeId); |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | } |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 1230 | } |
Kriti Dang | 646f8ec | 2022-01-18 14:35:02 +0100 | [diff] [blame] | 1231 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 1232 | status_t SurfaceFlinger::getDynamicDisplayInfoFromId(int64_t physicalDisplayId, |
| 1233 | ui::DynamicDisplayInfo* info) { |
| 1234 | if (!info) { |
| 1235 | return BAD_VALUE; |
| 1236 | } |
| 1237 | |
| 1238 | Mutex::Autolock lock(mStateLock); |
| 1239 | |
| 1240 | const auto id_ = |
| 1241 | DisplayId::fromValue<PhysicalDisplayId>(static_cast<uint64_t>(physicalDisplayId)); |
| 1242 | const auto displayOpt = mPhysicalDisplays.get(*id_).and_then(getDisplayDeviceAndSnapshot()); |
| 1243 | |
| 1244 | if (!displayOpt) { |
| 1245 | return NAME_NOT_FOUND; |
| 1246 | } |
| 1247 | |
| 1248 | const auto& [display, snapshotRef] = *displayOpt; |
| 1249 | getDynamicDisplayInfoInternal(info, display, snapshotRef.get()); |
| 1250 | return NO_ERROR; |
| 1251 | } |
| 1252 | |
| 1253 | status_t SurfaceFlinger::getDynamicDisplayInfoFromToken(const sp<IBinder>& displayToken, |
| 1254 | ui::DynamicDisplayInfo* info) { |
| 1255 | if (!displayToken || !info) { |
| 1256 | return BAD_VALUE; |
| 1257 | } |
| 1258 | |
| 1259 | Mutex::Autolock lock(mStateLock); |
| 1260 | |
| 1261 | const auto displayOpt = ftl::find_if(mPhysicalDisplays, PhysicalDisplay::hasToken(displayToken)) |
| 1262 | .transform(&ftl::to_mapped_ref<PhysicalDisplays>) |
| 1263 | .and_then(getDisplayDeviceAndSnapshot()); |
| 1264 | |
| 1265 | if (!displayOpt) { |
| 1266 | return NAME_NOT_FOUND; |
| 1267 | } |
| 1268 | |
| 1269 | const auto& [display, snapshotRef] = *displayOpt; |
| 1270 | getDynamicDisplayInfoInternal(info, display, snapshotRef.get()); |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 1271 | return NO_ERROR; |
| 1272 | } |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 1273 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1274 | status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& displayToken, |
| 1275 | DisplayStatInfo* outStats) { |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 1276 | if (!outStats) { |
Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 1277 | return BAD_VALUE; |
| 1278 | } |
| 1279 | |
Dominik Laskowski | cfaaceb | 2024-08-04 16:02:58 -0400 | [diff] [blame] | 1280 | // TODO: b/277364366 - Require a display token from clients and remove fallback to pacesetter. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1281 | std::optional<PhysicalDisplayId> displayIdOpt; |
Dominik Laskowski | cfaaceb | 2024-08-04 16:02:58 -0400 | [diff] [blame] | 1282 | if (displayToken) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1283 | Mutex::Autolock lock(mStateLock); |
Dominik Laskowski | cfaaceb | 2024-08-04 16:02:58 -0400 | [diff] [blame] | 1284 | displayIdOpt = getPhysicalDisplayIdLocked(displayToken); |
| 1285 | if (!displayIdOpt) { |
| 1286 | ALOGW("%s: Invalid physical display token %p", __func__, displayToken.get()); |
| 1287 | return NAME_NOT_FOUND; |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 1288 | } |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1289 | } |
| 1290 | |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 1291 | const auto schedule = mScheduler->getVsyncSchedule(displayIdOpt); |
| 1292 | if (!schedule) { |
| 1293 | ALOGE("%s: Missing VSYNC schedule for display %s!", __func__, |
| 1294 | to_string(*displayIdOpt).c_str()); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1295 | return NAME_NOT_FOUND; |
| 1296 | } |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1297 | outStats->vsyncTime = schedule->vsyncDeadlineAfter(TimePoint::now()).ns(); |
| 1298 | outStats->vsyncPeriod = schedule->period().ns(); |
Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 1299 | return NO_ERROR; |
| 1300 | } |
| 1301 | |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 1302 | void SurfaceFlinger::setDesiredMode(display::DisplayModeRequest&& desiredMode) { |
| 1303 | const auto mode = desiredMode.mode; |
| 1304 | const auto displayId = mode.modePtr->getPhysicalDisplayId(); |
| 1305 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1306 | SFTRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str()); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1307 | |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 1308 | const bool emitEvent = desiredMode.emitEvent; |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 1309 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1310 | using DesiredModeAction = display::DisplayModeController::DesiredModeAction; |
| 1311 | |
| 1312 | switch (mDisplayModeController.setDesiredMode(displayId, std::move(desiredMode))) { |
| 1313 | case DesiredModeAction::InitiateDisplayModeSwitch: { |
| 1314 | const auto selectorPtr = mDisplayModeController.selectorPtrFor(displayId); |
| 1315 | if (!selectorPtr) break; |
| 1316 | |
| 1317 | const Fps renderRate = selectorPtr->getActiveMode().fps; |
| 1318 | |
| 1319 | // DisplayModeController::setDesiredMode updated the render rate, so inform Scheduler. |
| 1320 | mScheduler->setRenderRate(displayId, renderRate, true /* applyImmediately */); |
Ady Abraham | 95a37e2 | 2023-01-18 22:57:28 -0800 | [diff] [blame] | 1321 | |
| 1322 | // Schedule a new frame to initiate the display mode switch. |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1323 | scheduleComposite(FrameHint::kNone); |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 1324 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1325 | // Start receiving vsync samples now, so that we can detect a period |
| 1326 | // switch. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1327 | mScheduler->resyncToHardwareVsync(displayId, true /* allowToEnable */, |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 1328 | mode.modePtr.get()); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1329 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1330 | // As we called to set period, we will call to onRefreshRateChangeCompleted once |
| 1331 | // VsyncController model is locked. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 1332 | mScheduler->modulateVsync(displayId, &VsyncModulator::onRefreshRateChangeInitiated); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1333 | |
Dominik Laskowski | e37c04e | 2024-08-04 15:48:00 -0400 | [diff] [blame] | 1334 | mScheduler->updatePhaseConfiguration(displayId, mode.fps); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1335 | mScheduler->setModeChangePending(true); |
| 1336 | break; |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1337 | } |
| 1338 | case DesiredModeAction::InitiateRenderRateSwitch: |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 1339 | mScheduler->setRenderRate(displayId, mode.fps, /*applyImmediately*/ false); |
Dominik Laskowski | e37c04e | 2024-08-04 15:48:00 -0400 | [diff] [blame] | 1340 | mScheduler->updatePhaseConfiguration(displayId, mode.fps); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1341 | |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1342 | if (emitEvent) { |
Dominik Laskowski | bda5236 | 2024-08-04 00:41:46 -0400 | [diff] [blame] | 1343 | mScheduler->onDisplayModeChanged(displayId, mode); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1344 | } |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1345 | break; |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1346 | case DesiredModeAction::None: |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1347 | break; |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1348 | } |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1351 | status_t SurfaceFlinger::setActiveModeFromBackdoor(const sp<display::DisplayToken>& displayToken, |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 1352 | DisplayModeId modeId, Fps minFps, Fps maxFps) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1353 | SFTRACE_CALL(); |
Ady Abraham | 838de06 | 2019-02-04 10:24:03 -0800 | [diff] [blame] | 1354 | |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 1355 | if (!displayToken) { |
| 1356 | return BAD_VALUE; |
| 1357 | } |
Ady Abraham | 838de06 | 2019-02-04 10:24:03 -0800 | [diff] [blame] | 1358 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1359 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1360 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(kMainThreadContext) -> status_t { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1361 | const auto displayOpt = |
| 1362 | FTL_FAKE_GUARD(mStateLock, |
| 1363 | ftl::find_if(mPhysicalDisplays, |
| 1364 | PhysicalDisplay::hasToken(displayToken)) |
| 1365 | .transform(&ftl::to_mapped_ref<PhysicalDisplays>) |
| 1366 | .and_then(getDisplayDeviceAndSnapshot())); |
| 1367 | if (!displayOpt) { |
| 1368 | ALOGE("%s: Invalid physical display token %p", whence, displayToken.get()); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 1369 | return NAME_NOT_FOUND; |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1370 | } |
| 1371 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1372 | const auto& [display, snapshotRef] = *displayOpt; |
| 1373 | const auto& snapshot = snapshotRef.get(); |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1374 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1375 | const auto fpsOpt = snapshot.displayModes().get(modeId).transform( |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 1376 | [](const DisplayModePtr& mode) { return mode->getPeakFps(); }); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1377 | |
| 1378 | if (!fpsOpt) { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1379 | 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] | 1380 | to_string(snapshot.displayId()).c_str()); |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1381 | return BAD_VALUE; |
| 1382 | } |
| 1383 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1384 | const Fps fps = *fpsOpt; |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 1385 | const FpsRange physical = {fps, fps}; |
| 1386 | const FpsRange render = {minFps.isValid() ? minFps : fps, maxFps.isValid() ? maxFps : fps}; |
| 1387 | const FpsRanges ranges = {physical, render}; |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 1388 | |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1389 | // Keep the old switching type. |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1390 | const bool allowGroupSwitching = |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 1391 | display->refreshRateSelector().getCurrentPolicy().allowGroupSwitching; |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1392 | |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 1393 | const scheduler::RefreshRateSelector::DisplayManagerPolicy policy{modeId, ranges, ranges, |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 1394 | allowGroupSwitching}; |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 1395 | |
| 1396 | return setDesiredDisplayModeSpecsInternal(display, policy); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 1397 | }); |
| 1398 | |
| 1399 | return future.get(); |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
Dominik Laskowski | b2fc423 | 2024-07-15 08:24:49 -0400 | [diff] [blame] | 1402 | // TODO: b/241285876 - Restore thread safety analysis once mStateLock below is unconditional. |
| 1403 | [[clang::no_thread_safety_analysis]] |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1404 | void SurfaceFlinger::finalizeDisplayModeChange(PhysicalDisplayId displayId) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1405 | SFTRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str()); |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1406 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1407 | const auto pendingModeOpt = mDisplayModeController.getPendingMode(displayId); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1408 | if (!pendingModeOpt) { |
| 1409 | // There is no pending mode change. This can happen if the active |
| 1410 | // display changed and the mode change happened on a different display. |
| 1411 | return; |
| 1412 | } |
Ady Abraham | faac6da | 2021-07-15 10:04:40 -0700 | [diff] [blame] | 1413 | |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1414 | const auto& activeMode = pendingModeOpt->mode; |
Dominik Laskowski | 1ab20c5 | 2023-11-27 11:45:57 -0500 | [diff] [blame] | 1415 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1416 | if (const auto oldResolution = |
| 1417 | mDisplayModeController.getActiveMode(displayId).modePtr->getResolution(); |
| 1418 | oldResolution != activeMode.modePtr->getResolution()) { |
Dominik Laskowski | b2fc423 | 2024-07-15 08:24:49 -0400 | [diff] [blame] | 1419 | ConditionalLock lock(mStateLock, !FlagManager::getInstance().connected_display()); |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1420 | |
| 1421 | auto& state = mCurrentState.displays.editValueFor(getPhysicalDisplayTokenLocked(displayId)); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1422 | // We need to generate new sequenceId in order to recreate the display (and this |
| 1423 | // way the framebuffer). |
| 1424 | state.sequenceId = DisplayDeviceState{}.sequenceId; |
| 1425 | state.physical->activeMode = activeMode.modePtr.get(); |
| 1426 | processDisplayChangesLocked(); |
Marin Shalamanov | 993ddf4 | 2021-05-26 16:54:40 +0200 | [diff] [blame] | 1427 | |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1428 | // processDisplayChangesLocked will update all necessary components so we're done here. |
| 1429 | return; |
| 1430 | } |
Alec Mouri | 8de697e | 2020-03-19 10:52:01 -0700 | [diff] [blame] | 1431 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1432 | mDisplayModeController.finalizeModeChange(displayId, activeMode.modePtr->getId(), |
| 1433 | activeMode.modePtr->getVsyncRate(), activeMode.fps); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1434 | |
Dominik Laskowski | e37c04e | 2024-08-04 15:48:00 -0400 | [diff] [blame] | 1435 | mScheduler->updatePhaseConfiguration(displayId, activeMode.fps); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1436 | |
| 1437 | if (pendingModeOpt->emitEvent) { |
Dominik Laskowski | bda5236 | 2024-08-04 00:41:46 -0400 | [diff] [blame] | 1438 | mScheduler->onDisplayModeChanged(displayId, activeMode); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1439 | } |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1442 | void SurfaceFlinger::dropModeRequest(PhysicalDisplayId displayId) { |
| 1443 | mDisplayModeController.clearDesiredMode(displayId); |
| 1444 | if (displayId == mActiveDisplayId) { |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1445 | // TODO(b/255635711): Check for pending mode changes on other displays. |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1446 | mScheduler->setModeChangePending(false); |
| 1447 | } |
Ady Abraham | 53852a5 | 2019-05-28 18:07:44 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1450 | void SurfaceFlinger::applyActiveMode(PhysicalDisplayId displayId) { |
| 1451 | const auto activeModeOpt = mDisplayModeController.getDesiredMode(displayId); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1452 | auto activeModePtr = activeModeOpt->mode.modePtr; |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1453 | const auto renderFps = activeModeOpt->mode.fps; |
Dominik Laskowski | 1ab20c5 | 2023-11-27 11:45:57 -0500 | [diff] [blame] | 1454 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1455 | dropModeRequest(displayId); |
Dominik Laskowski | 1ab20c5 | 2023-11-27 11:45:57 -0500 | [diff] [blame] | 1456 | |
| 1457 | constexpr bool kAllowToEnable = true; |
| 1458 | mScheduler->resyncToHardwareVsync(displayId, kAllowToEnable, std::move(activeModePtr).take()); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1459 | |
Dominik Laskowski | e37c04e | 2024-08-04 15:48:00 -0400 | [diff] [blame] | 1460 | mScheduler->setRenderRate(displayId, renderFps, /*applyImmediately*/ true); |
| 1461 | mScheduler->updatePhaseConfiguration(displayId, renderFps); |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 1462 | } |
| 1463 | |
Ady Abraham | 0b10360 | 2024-05-21 20:37:49 +0000 | [diff] [blame] | 1464 | void SurfaceFlinger::initiateDisplayModeChanges() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1465 | SFTRACE_CALL(); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1466 | |
Dominik Laskowski | b2fc423 | 2024-07-15 08:24:49 -0400 | [diff] [blame] | 1467 | for (const auto& [displayId, physical] : mPhysicalDisplays) { |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1468 | auto desiredModeOpt = mDisplayModeController.getDesiredMode(displayId); |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 1469 | if (!desiredModeOpt) { |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1470 | continue; |
| 1471 | } |
| 1472 | |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1473 | const auto desiredModeId = desiredModeOpt->mode.modePtr->getId(); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 1474 | const auto displayModePtrOpt = physical.snapshot().displayModes().get(desiredModeId); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1475 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1476 | if (!displayModePtrOpt) { |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1477 | ALOGW("Desired display mode is no longer supported. Mode ID = %d", |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1478 | ftl::to_underlying(desiredModeId)); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1479 | continue; |
| 1480 | } |
| 1481 | |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1482 | ALOGV("%s changing active mode to %d(%s) for display %s", __func__, |
| 1483 | ftl::to_underlying(desiredModeId), |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1484 | to_string(displayModePtrOpt->get()->getVsyncRate()).c_str(), |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1485 | to_string(displayId).c_str()); |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1486 | |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 1487 | if ((!FlagManager::getInstance().connected_display() || !desiredModeOpt->force) && |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1488 | mDisplayModeController.getActiveMode(displayId) == desiredModeOpt->mode) { |
| 1489 | applyActiveMode(displayId); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1490 | continue; |
| 1491 | } |
| 1492 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1493 | const auto selectorPtr = mDisplayModeController.selectorPtrFor(displayId); |
| 1494 | |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1495 | // Desired active mode was set, it is different than the mode currently in use, however |
| 1496 | // allowed modes might have changed by the time we process the refresh. |
| 1497 | // Make sure the desired mode is still allowed |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1498 | if (!selectorPtr->isModeAllowed(desiredModeOpt->mode)) { |
| 1499 | dropModeRequest(displayId); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1500 | continue; |
| 1501 | } |
| 1502 | |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1503 | // TODO(b/142753666) use constrains |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1504 | hal::VsyncPeriodChangeConstraints constraints; |
| 1505 | constraints.desiredTimeNanos = systemTime(); |
| 1506 | constraints.seamlessRequired = false; |
| 1507 | hal::VsyncPeriodChangeTimeline outTimeline; |
| 1508 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1509 | if (!mDisplayModeController.initiateModeChange(displayId, std::move(*desiredModeOpt), |
| 1510 | constraints, outTimeline)) { |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1511 | continue; |
| 1512 | } |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 1513 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 1514 | selectorPtr->onModeChangeInitiated(); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 1515 | mScheduler->onNewVsyncPeriodChangeTimeline(outTimeline); |
| 1516 | |
Marin Shalamanov | dd59431 | 2021-11-09 16:37:37 +0100 | [diff] [blame] | 1517 | if (outTimeline.refreshRequired) { |
Ady Abraham | 0b10360 | 2024-05-21 20:37:49 +0000 | [diff] [blame] | 1518 | scheduleComposite(FrameHint::kNone); |
Marin Shalamanov | dd59431 | 2021-11-09 16:37:37 +0100 | [diff] [blame] | 1519 | } else { |
Dominik Laskowski | 92900f8 | 2024-06-25 11:50:43 -0400 | [diff] [blame] | 1520 | // HWC has requested to apply the mode change immediately rather than on the next frame. |
| 1521 | finalizeDisplayModeChange(displayId); |
Marin Shalamanov | dd59431 | 2021-11-09 16:37:37 +0100 | [diff] [blame] | 1522 | |
Dominik Laskowski | 92900f8 | 2024-06-25 11:50:43 -0400 | [diff] [blame] | 1523 | const auto desiredModeOpt = mDisplayModeController.getDesiredMode(displayId); |
| 1524 | if (desiredModeOpt && |
| 1525 | mDisplayModeController.getActiveMode(displayId) == desiredModeOpt->mode) { |
| 1526 | applyActiveMode(displayId); |
| 1527 | } |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 1528 | } |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 1529 | } |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 1530 | } |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 1531 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 1532 | void SurfaceFlinger::disableExpensiveRendering() { |
Dominik Laskowski | 4d5052d | 2022-03-23 10:35:47 -0700 | [diff] [blame] | 1533 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1534 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 1535 | SFTRACE_NAME(whence); |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 1536 | if (mPowerAdvisor->isUsingExpensiveRendering()) { |
Dominik Laskowski | bc6c860 | 2022-01-11 08:53:24 -0800 | [diff] [blame] | 1537 | for (const auto& [_, display] : mDisplays) { |
| 1538 | constexpr bool kDisable = false; |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 1539 | mPowerAdvisor->setExpensiveRenderingExpected(display->getId(), kDisable); |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 1540 | } |
| 1541 | } |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1542 | }); |
| 1543 | |
| 1544 | future.wait(); |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 1547 | status_t SurfaceFlinger::getDisplayNativePrimaries(const sp<IBinder>& displayToken, |
Dominik Laskowski | 3c36324 | 2022-04-07 10:44:12 -0700 | [diff] [blame] | 1548 | ui::DisplayPrimaries& primaries) { |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 1549 | if (!displayToken) { |
| 1550 | return BAD_VALUE; |
| 1551 | } |
| 1552 | |
Dominik Laskowski | 3c36324 | 2022-04-07 10:44:12 -0700 | [diff] [blame] | 1553 | Mutex::Autolock lock(mStateLock); |
| 1554 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1555 | const auto display = ftl::find_if(mPhysicalDisplays, PhysicalDisplay::hasToken(displayToken)) |
| 1556 | .transform(&ftl::to_mapped_ref<PhysicalDisplays>); |
Dominik Laskowski | 3c36324 | 2022-04-07 10:44:12 -0700 | [diff] [blame] | 1557 | if (!display) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1558 | return NAME_NOT_FOUND; |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 1559 | } |
| 1560 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1561 | if (!display.transform(&PhysicalDisplay::isInternal).value()) { |
Dominik Laskowski | 3c36324 | 2022-04-07 10:44:12 -0700 | [diff] [blame] | 1562 | return INVALID_OPERATION; |
| 1563 | } |
| 1564 | |
| 1565 | // 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] | 1566 | primaries = mInternalDisplayPrimaries; |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 1567 | return NO_ERROR; |
| 1568 | } |
| 1569 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1570 | status_t SurfaceFlinger::setActiveColorMode(const sp<IBinder>& displayToken, ui::ColorMode mode) { |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1571 | if (!displayToken) { |
| 1572 | return BAD_VALUE; |
| 1573 | } |
| 1574 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1575 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1576 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) -> status_t { |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1577 | const auto displayOpt = |
| 1578 | ftl::find_if(mPhysicalDisplays, PhysicalDisplay::hasToken(displayToken)) |
| 1579 | .transform(&ftl::to_mapped_ref<PhysicalDisplays>) |
| 1580 | .and_then(getDisplayDeviceAndSnapshot()); |
| 1581 | |
| 1582 | if (!displayOpt) { |
| 1583 | ALOGE("%s: Invalid physical display token %p", whence, displayToken.get()); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1584 | return NAME_NOT_FOUND; |
| 1585 | } |
| 1586 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1587 | const auto& [display, snapshotRef] = *displayOpt; |
| 1588 | const auto& snapshot = snapshotRef.get(); |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 1589 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1590 | const auto modes = snapshot.filterColorModes(mSupportsWideColor); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1591 | const bool exists = std::find(modes.begin(), modes.end(), mode) != modes.end(); |
| 1592 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 1593 | if (mode < ui::ColorMode::NATIVE || !exists) { |
| 1594 | ALOGE("%s: Invalid color mode %s (%d) for display %s", whence, |
| 1595 | decodeColorMode(mode).c_str(), mode, to_string(snapshot.displayId()).c_str()); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1596 | return BAD_VALUE; |
| 1597 | } |
| 1598 | |
| 1599 | display->getCompositionDisplay()->setColorProfile( |
Alec Mouri | 88790f3 | 2023-07-21 01:25:14 +0000 | [diff] [blame] | 1600 | {mode, Dataspace::UNKNOWN, RenderIntent::COLORIMETRIC}); |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 1601 | |
| 1602 | return NO_ERROR; |
| 1603 | }); |
| 1604 | |
Dominik Laskowski | 0a5f921 | 2021-08-05 17:00:04 -0700 | [diff] [blame] | 1605 | // TODO(b/195698395): Propagate error. |
| 1606 | future.wait(); |
| 1607 | return NO_ERROR; |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1608 | } |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 1609 | |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1610 | status_t SurfaceFlinger::getBootDisplayModeSupport(bool* outSupport) const { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 1611 | auto future = mScheduler->schedule( |
| 1612 | [this] { return getHwComposer().hasCapability(Capability::BOOT_DISPLAY_CONFIG); }); |
| 1613 | |
| 1614 | *outSupport = future.get(); |
| 1615 | return NO_ERROR; |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1616 | } |
| 1617 | |
Sally Qi | bb866c1 | 2022-10-17 11:31:20 -0700 | [diff] [blame] | 1618 | status_t SurfaceFlinger::getOverlaySupport(gui::OverlayProperties* outProperties) const { |
| 1619 | const auto& aidlProperties = getHwComposer().getOverlaySupport(); |
| 1620 | // convert aidl OverlayProperties to gui::OverlayProperties |
| 1621 | outProperties->combinations.reserve(aidlProperties.combinations.size()); |
| 1622 | for (const auto& combination : aidlProperties.combinations) { |
| 1623 | std::vector<int32_t> pixelFormats; |
| 1624 | pixelFormats.reserve(combination.pixelFormats.size()); |
| 1625 | std::transform(combination.pixelFormats.cbegin(), combination.pixelFormats.cend(), |
| 1626 | std::back_inserter(pixelFormats), |
| 1627 | [](const auto& val) { return static_cast<int32_t>(val); }); |
Sally Qi | 4119e22 | 2023-01-13 14:52:18 -0800 | [diff] [blame] | 1628 | std::vector<int32_t> standards; |
| 1629 | standards.reserve(combination.standards.size()); |
| 1630 | std::transform(combination.standards.cbegin(), combination.standards.cend(), |
| 1631 | std::back_inserter(standards), |
| 1632 | [](const auto& val) { return static_cast<int32_t>(val); }); |
| 1633 | std::vector<int32_t> transfers; |
| 1634 | transfers.reserve(combination.transfers.size()); |
| 1635 | std::transform(combination.transfers.cbegin(), combination.transfers.cend(), |
| 1636 | std::back_inserter(transfers), |
| 1637 | [](const auto& val) { return static_cast<int32_t>(val); }); |
| 1638 | std::vector<int32_t> ranges; |
| 1639 | ranges.reserve(combination.ranges.size()); |
| 1640 | std::transform(combination.ranges.cbegin(), combination.ranges.cend(), |
| 1641 | std::back_inserter(ranges), |
Sally Qi | bb866c1 | 2022-10-17 11:31:20 -0700 | [diff] [blame] | 1642 | [](const auto& val) { return static_cast<int32_t>(val); }); |
| 1643 | gui::OverlayProperties::SupportedBufferCombinations outCombination; |
| 1644 | outCombination.pixelFormats = std::move(pixelFormats); |
Sally Qi | 4119e22 | 2023-01-13 14:52:18 -0800 | [diff] [blame] | 1645 | outCombination.standards = std::move(standards); |
| 1646 | outCombination.transfers = std::move(transfers); |
| 1647 | outCombination.ranges = std::move(ranges); |
Sally Qi | bb866c1 | 2022-10-17 11:31:20 -0700 | [diff] [blame] | 1648 | outProperties->combinations.emplace_back(outCombination); |
| 1649 | } |
Sally Qi | 8b4cb55 | 2022-12-14 16:24:31 -0800 | [diff] [blame] | 1650 | outProperties->supportMixedColorSpaces = aidlProperties.supportMixedColorSpaces; |
Sally Qi | 95f669a | 2024-08-27 11:31:42 -0700 | [diff] [blame^] | 1651 | if (aidlProperties.lutProperties.has_value()) { |
| 1652 | std::vector<gui::LutProperties> outLutProperties; |
| 1653 | for (const auto& properties : aidlProperties.lutProperties.value()) { |
| 1654 | gui::LutProperties currentProperties; |
| 1655 | currentProperties.dimension = |
| 1656 | static_cast<gui::LutProperties::Dimension>(properties->dimension); |
| 1657 | currentProperties.size = properties->size; |
| 1658 | currentProperties.samplingKeys.reserve(properties->samplingKeys.size()); |
| 1659 | std::transform(properties->samplingKeys.cbegin(), properties->samplingKeys.cend(), |
| 1660 | std::back_inserter(currentProperties.samplingKeys), [](const auto& val) { |
| 1661 | return static_cast<gui::LutProperties::SamplingKey>(val); |
| 1662 | }); |
| 1663 | outLutProperties.push_back(std::move(currentProperties)); |
| 1664 | } |
| 1665 | outProperties->lutProperties.emplace(outLutProperties.begin(), outLutProperties.end()); |
| 1666 | } |
Sally Qi | 0cbd08b | 2022-08-17 12:12:28 -0700 | [diff] [blame] | 1667 | return NO_ERROR; |
| 1668 | } |
| 1669 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1670 | status_t SurfaceFlinger::setBootDisplayMode(const sp<display::DisplayToken>& displayToken, |
| 1671 | DisplayModeId modeId) { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1672 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1673 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) -> status_t { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1674 | const auto snapshotOpt = |
| 1675 | ftl::find_if(mPhysicalDisplays, PhysicalDisplay::hasToken(displayToken)) |
| 1676 | .transform(&ftl::to_mapped_ref<PhysicalDisplays>) |
| 1677 | .transform(&PhysicalDisplay::snapshotRef); |
| 1678 | |
| 1679 | if (!snapshotOpt) { |
| 1680 | ALOGE("%s: Invalid physical display token %p", whence, displayToken.get()); |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1681 | return NAME_NOT_FOUND; |
| 1682 | } |
Kriti Dang | f50d677 | 2022-02-18 15:09:12 +0100 | [diff] [blame] | 1683 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1684 | const auto& snapshot = snapshotOpt->get(); |
| 1685 | const auto hwcIdOpt = snapshot.displayModes().get(modeId).transform( |
| 1686 | [](const DisplayModePtr& mode) { return mode->getHwcId(); }); |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1687 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1688 | if (!hwcIdOpt) { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 1689 | 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] | 1690 | to_string(snapshot.displayId()).c_str()); |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1691 | return BAD_VALUE; |
| 1692 | } |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1693 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 1694 | return getHwComposer().setBootDisplayMode(snapshot.displayId(), *hwcIdOpt); |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1695 | }); |
| 1696 | return future.get(); |
| 1697 | } |
| 1698 | |
| 1699 | status_t SurfaceFlinger::clearBootDisplayMode(const sp<IBinder>& displayToken) { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1700 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1701 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) -> status_t { |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1702 | if (const auto displayId = getPhysicalDisplayIdLocked(displayToken)) { |
| 1703 | return getHwComposer().clearBootDisplayMode(*displayId); |
| 1704 | } else { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1705 | ALOGE("%s: Invalid display token %p", whence, displayToken.get()); |
Kriti Dang | 7defaf3 | 2021-11-15 11:55:43 +0100 | [diff] [blame] | 1706 | return BAD_VALUE; |
| 1707 | } |
| 1708 | }); |
| 1709 | return future.get(); |
| 1710 | } |
| 1711 | |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1712 | status_t SurfaceFlinger::getHdrConversionCapabilities( |
| 1713 | std::vector<gui::HdrConversionCapability>* hdrConversionCapabilities) const { |
| 1714 | bool hdrOutputConversionSupport; |
| 1715 | getHdrOutputConversionSupport(&hdrOutputConversionSupport); |
| 1716 | if (hdrOutputConversionSupport == false) { |
| 1717 | ALOGE("hdrOutputConversion is not supported by this device."); |
| 1718 | return INVALID_OPERATION; |
| 1719 | } |
| 1720 | const auto aidlConversionCapability = getHwComposer().getHdrConversionCapabilities(); |
| 1721 | for (auto capability : aidlConversionCapability) { |
| 1722 | gui::HdrConversionCapability tempCapability; |
Kriti Dang | 3c243dc | 2023-03-07 15:00:13 +0100 | [diff] [blame] | 1723 | tempCapability.sourceType = static_cast<int>(capability.sourceType); |
| 1724 | tempCapability.outputType = static_cast<int>(capability.outputType); |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1725 | tempCapability.addsLatency = capability.addsLatency; |
| 1726 | hdrConversionCapabilities->push_back(tempCapability); |
| 1727 | } |
| 1728 | return NO_ERROR; |
| 1729 | } |
| 1730 | |
| 1731 | status_t SurfaceFlinger::setHdrConversionStrategy( |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 1732 | const gui::HdrConversionStrategy& hdrConversionStrategy, |
| 1733 | int32_t* outPreferredHdrOutputType) { |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1734 | bool hdrOutputConversionSupport; |
| 1735 | getHdrOutputConversionSupport(&hdrOutputConversionSupport); |
| 1736 | if (hdrOutputConversionSupport == false) { |
| 1737 | ALOGE("hdrOutputConversion is not supported by this device."); |
| 1738 | return INVALID_OPERATION; |
| 1739 | } |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1740 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) mutable -> status_t { |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1741 | using AidlHdrConversionStrategy = |
| 1742 | aidl::android::hardware::graphics::common::HdrConversionStrategy; |
| 1743 | using GuiHdrConversionStrategyTag = gui::HdrConversionStrategy::Tag; |
| 1744 | AidlHdrConversionStrategy aidlConversionStrategy; |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 1745 | status_t status; |
| 1746 | aidl::android::hardware::graphics::common::Hdr aidlPreferredHdrOutputType; |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1747 | switch (hdrConversionStrategy.getTag()) { |
| 1748 | case GuiHdrConversionStrategyTag::passthrough: { |
| 1749 | aidlConversionStrategy.set<AidlHdrConversionStrategy::Tag::passthrough>( |
| 1750 | hdrConversionStrategy.get<GuiHdrConversionStrategyTag::passthrough>()); |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 1751 | status = getHwComposer().setHdrConversionStrategy(aidlConversionStrategy, |
| 1752 | &aidlPreferredHdrOutputType); |
| 1753 | *outPreferredHdrOutputType = static_cast<int32_t>(aidlPreferredHdrOutputType); |
| 1754 | return status; |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1755 | } |
| 1756 | case GuiHdrConversionStrategyTag::autoAllowedHdrTypes: { |
| 1757 | auto autoHdrTypes = |
| 1758 | hdrConversionStrategy |
| 1759 | .get<GuiHdrConversionStrategyTag::autoAllowedHdrTypes>(); |
| 1760 | std::vector<aidl::android::hardware::graphics::common::Hdr> aidlAutoHdrTypes; |
| 1761 | for (auto type : autoHdrTypes) { |
| 1762 | aidlAutoHdrTypes.push_back( |
| 1763 | static_cast<aidl::android::hardware::graphics::common::Hdr>(type)); |
| 1764 | } |
| 1765 | aidlConversionStrategy.set<AidlHdrConversionStrategy::Tag::autoAllowedHdrTypes>( |
| 1766 | aidlAutoHdrTypes); |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 1767 | status = getHwComposer().setHdrConversionStrategy(aidlConversionStrategy, |
| 1768 | &aidlPreferredHdrOutputType); |
| 1769 | *outPreferredHdrOutputType = static_cast<int32_t>(aidlPreferredHdrOutputType); |
| 1770 | return status; |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1771 | } |
| 1772 | case GuiHdrConversionStrategyTag::forceHdrConversion: { |
| 1773 | auto forceHdrConversion = |
| 1774 | hdrConversionStrategy |
| 1775 | .get<GuiHdrConversionStrategyTag::forceHdrConversion>(); |
| 1776 | aidlConversionStrategy.set<AidlHdrConversionStrategy::Tag::forceHdrConversion>( |
| 1777 | static_cast<aidl::android::hardware::graphics::common::Hdr>( |
| 1778 | forceHdrConversion)); |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 1779 | status = getHwComposer().setHdrConversionStrategy(aidlConversionStrategy, |
| 1780 | &aidlPreferredHdrOutputType); |
| 1781 | *outPreferredHdrOutputType = static_cast<int32_t>(aidlPreferredHdrOutputType); |
| 1782 | return status; |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | }); |
| 1786 | return future.get(); |
| 1787 | } |
| 1788 | |
| 1789 | status_t SurfaceFlinger::getHdrOutputConversionSupport(bool* outSupport) const { |
| 1790 | auto future = mScheduler->schedule([this] { |
| 1791 | return getHwComposer().hasCapability(Capability::HDR_OUTPUT_CONVERSION_CONFIG); |
| 1792 | }); |
| 1793 | |
| 1794 | *outSupport = future.get(); |
| 1795 | return NO_ERROR; |
| 1796 | } |
| 1797 | |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 1798 | void SurfaceFlinger::setAutoLowLatencyMode(const sp<IBinder>& displayToken, bool on) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1799 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1800 | static_cast<void>(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1801 | if (const auto displayId = getPhysicalDisplayIdLocked(displayToken)) { |
| 1802 | getHwComposer().setAutoLowLatencyMode(*displayId, on); |
| 1803 | } else { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1804 | ALOGE("%s: Invalid display token %p", whence, displayToken.get()); |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1805 | } |
| 1806 | })); |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 1807 | } |
| 1808 | |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 1809 | void SurfaceFlinger::setGameContentType(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)) { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 1813 | const auto type = on ? hal::ContentType::GAME : hal::ContentType::NONE; |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1814 | getHwComposer().setContentType(*displayId, type); |
| 1815 | } else { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1816 | ALOGE("%s: Invalid display token %p", whence, displayToken.get()); |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1817 | } |
| 1818 | })); |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 1819 | } |
| 1820 | |
Kriti Dang | 49ad413 | 2021-01-08 11:49:56 +0100 | [diff] [blame] | 1821 | status_t SurfaceFlinger::overrideHdrTypes(const sp<IBinder>& displayToken, |
| 1822 | const std::vector<ui::Hdr>& hdrTypes) { |
| 1823 | Mutex::Autolock lock(mStateLock); |
| 1824 | |
| 1825 | auto display = getDisplayDeviceLocked(displayToken); |
| 1826 | if (!display) { |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 1827 | ALOGE("%s: Invalid display token %p", __func__, displayToken.get()); |
Kriti Dang | 49ad413 | 2021-01-08 11:49:56 +0100 | [diff] [blame] | 1828 | return NAME_NOT_FOUND; |
| 1829 | } |
| 1830 | |
| 1831 | display->overrideHdrTypes(hdrTypes); |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 1832 | mScheduler->dispatchHotplug(display->getPhysicalId(), scheduler::Scheduler::Hotplug::Connected); |
Kriti Dang | 49ad413 | 2021-01-08 11:49:56 +0100 | [diff] [blame] | 1833 | return NO_ERROR; |
| 1834 | } |
| 1835 | |
Huihong Luo | 30aa437 | 2022-10-03 14:54:12 -0700 | [diff] [blame] | 1836 | status_t SurfaceFlinger::onPullAtom(const int32_t atomId, std::vector<uint8_t>* pulledData, |
| 1837 | bool* success) { |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 1838 | *success = mTimeStats->onPullAtom(atomId, pulledData); |
| 1839 | return NO_ERROR; |
| 1840 | } |
| 1841 | |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 1842 | status_t SurfaceFlinger::getDisplayedContentSamplingAttributes(const sp<IBinder>& displayToken, |
| 1843 | ui::PixelFormat* outFormat, |
| 1844 | ui::Dataspace* outDataspace, |
| 1845 | uint8_t* outComponentMask) const { |
| 1846 | if (!outFormat || !outDataspace || !outComponentMask) { |
| 1847 | return BAD_VALUE; |
| 1848 | } |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1849 | |
| 1850 | Mutex::Autolock lock(mStateLock); |
| 1851 | |
| 1852 | const auto displayId = getPhysicalDisplayIdLocked(displayToken); |
| 1853 | if (!displayId) { |
| 1854 | return NAME_NOT_FOUND; |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 1855 | } |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1856 | |
| 1857 | return getHwComposer().getDisplayedContentSamplingAttributes(*displayId, outFormat, |
Kevin DuBois | 9c0a176 | 2018-10-16 13:32:31 -0700 | [diff] [blame] | 1858 | outDataspace, outComponentMask); |
| 1859 | } |
| 1860 | |
Kevin DuBois | 74e5377 | 2018-11-19 10:52:38 -0800 | [diff] [blame] | 1861 | status_t SurfaceFlinger::setDisplayContentSamplingEnabled(const sp<IBinder>& displayToken, |
| 1862 | bool enable, uint8_t componentMask, |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1863 | uint64_t maxFrames) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1864 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 1865 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) -> status_t { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 1866 | if (const auto displayId = getPhysicalDisplayIdLocked(displayToken)) { |
| 1867 | return getHwComposer().setDisplayContentSamplingEnabled(*displayId, enable, |
| 1868 | componentMask, maxFrames); |
| 1869 | } else { |
| 1870 | ALOGE("%s: Invalid display token %p", whence, displayToken.get()); |
| 1871 | return NAME_NOT_FOUND; |
| 1872 | } |
| 1873 | }); |
| 1874 | |
| 1875 | return future.get(); |
Kevin DuBois | 74e5377 | 2018-11-19 10:52:38 -0800 | [diff] [blame] | 1876 | } |
| 1877 | |
Kevin DuBois | 1d4249a | 2018-08-29 10:45:14 -0700 | [diff] [blame] | 1878 | status_t SurfaceFlinger::getDisplayedContentSample(const sp<IBinder>& displayToken, |
| 1879 | uint64_t maxFrames, uint64_t timestamp, |
| 1880 | DisplayedFrameStats* outStats) const { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1881 | Mutex::Autolock lock(mStateLock); |
| 1882 | |
| 1883 | const auto displayId = getPhysicalDisplayIdLocked(displayToken); |
| 1884 | if (!displayId) { |
| 1885 | return NAME_NOT_FOUND; |
Kevin DuBois | 1d4249a | 2018-08-29 10:45:14 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1888 | return getHwComposer().getDisplayedContentSample(*displayId, maxFrames, timestamp, outStats); |
Kevin DuBois | 1d4249a | 2018-08-29 10:45:14 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
Peiyong Lin | 3c2791e | 2019-01-14 17:05:18 -0800 | [diff] [blame] | 1891 | status_t SurfaceFlinger::getProtectedContentSupport(bool* outSupported) const { |
| 1892 | if (!outSupported) { |
| 1893 | return BAD_VALUE; |
| 1894 | } |
| 1895 | *outSupported = getRenderEngine().supportsProtectedContent(); |
| 1896 | return NO_ERROR; |
| 1897 | } |
| 1898 | |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 1899 | status_t SurfaceFlinger::isWideColorDisplay(const sp<IBinder>& displayToken, |
| 1900 | bool* outIsWideColorDisplay) const { |
| 1901 | if (!displayToken || !outIsWideColorDisplay) { |
| 1902 | return BAD_VALUE; |
| 1903 | } |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1904 | |
| 1905 | Mutex::Autolock lock(mStateLock); |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 1906 | const auto display = getDisplayDeviceLocked(displayToken); |
| 1907 | if (!display) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1908 | return NAME_NOT_FOUND; |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 1909 | } |
Peiyong Lin | ff84a15 | 2019-05-17 18:36:19 -0700 | [diff] [blame] | 1910 | |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1911 | *outIsWideColorDisplay = |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1912 | display->isPrimary() ? mSupportsWideColor : display->hasWideColorGamut(); |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 1913 | return NO_ERROR; |
| 1914 | } |
| 1915 | |
Peiyong Lin | c678097 | 2018-10-28 15:24:08 -0700 | [diff] [blame] | 1916 | status_t SurfaceFlinger::getCompositionPreference( |
| 1917 | Dataspace* outDataspace, ui::PixelFormat* outPixelFormat, |
| 1918 | Dataspace* outWideColorGamutDataspace, |
| 1919 | ui::PixelFormat* outWideColorGamutPixelFormat) const { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 1920 | *outDataspace = mDefaultCompositionDataspace; |
| 1921 | *outPixelFormat = defaultCompositionPixelFormat; |
| 1922 | *outWideColorGamutDataspace = mWideColorGamutCompositionDataspace; |
| 1923 | *outWideColorGamutPixelFormat = wideColorGamutCompositionPixelFormat; |
Peiyong Lin | 0256f72 | 2018-08-31 15:45:10 -0700 | [diff] [blame] | 1924 | return NO_ERROR; |
| 1925 | } |
| 1926 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1927 | status_t SurfaceFlinger::addRegionSamplingListener(const Rect& samplingArea, |
| 1928 | const sp<IBinder>& stopLayerHandle, |
| 1929 | const sp<IRegionSamplingListener>& listener) { |
Leon Scroggins III | ae16b80 | 2022-01-12 11:42:05 -0500 | [diff] [blame] | 1930 | if (!listener || samplingArea == Rect::INVALID_RECT || samplingArea.isEmpty()) { |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1931 | return BAD_VALUE; |
| 1932 | } |
Alec Mouri | 9a02eda | 2020-04-21 17:39:34 -0700 | [diff] [blame] | 1933 | |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 1934 | // LayerHandle::getLayer promotes the layer object in a binder thread but we will not destroy |
| 1935 | // 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] | 1936 | const sp<Layer> stopLayer = LayerHandle::getLayer(stopLayerHandle); |
| 1937 | mRegionSamplingThread->addListener(samplingArea, |
| 1938 | stopLayer ? stopLayer->getSequence() : UNASSIGNED_LAYER_ID, |
| 1939 | listener); |
Dan Stoza | 84ab937 | 2018-12-17 15:27:57 -0800 | [diff] [blame] | 1940 | return NO_ERROR; |
| 1941 | } |
| 1942 | |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1943 | status_t SurfaceFlinger::removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener) { |
tangrobin | af45f01 | 2019-02-26 18:10:10 +0800 | [diff] [blame] | 1944 | if (!listener) { |
| 1945 | return BAD_VALUE; |
| 1946 | } |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1947 | mRegionSamplingThread->removeListener(listener); |
Dan Stoza | 84ab937 | 2018-12-17 15:27:57 -0800 | [diff] [blame] | 1948 | return NO_ERROR; |
| 1949 | } |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 1950 | |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 1951 | status_t SurfaceFlinger::addFpsListener(int32_t taskId, const sp<gui::IFpsListener>& listener) { |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 1952 | if (!listener) { |
| 1953 | return BAD_VALUE; |
| 1954 | } |
| 1955 | |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 1956 | mFpsReporter->addListener(listener, taskId); |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 1957 | return NO_ERROR; |
| 1958 | } |
| 1959 | |
| 1960 | status_t SurfaceFlinger::removeFpsListener(const sp<gui::IFpsListener>& listener) { |
| 1961 | if (!listener) { |
| 1962 | return BAD_VALUE; |
| 1963 | } |
| 1964 | mFpsReporter->removeListener(listener); |
| 1965 | return NO_ERROR; |
| 1966 | } |
| 1967 | |
Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 1968 | status_t SurfaceFlinger::addTunnelModeEnabledListener( |
| 1969 | const sp<gui::ITunnelModeEnabledListener>& listener) { |
| 1970 | if (!listener) { |
| 1971 | return BAD_VALUE; |
| 1972 | } |
| 1973 | |
| 1974 | mTunnelModeEnabledReporter->addListener(listener); |
| 1975 | return NO_ERROR; |
| 1976 | } |
| 1977 | |
| 1978 | status_t SurfaceFlinger::removeTunnelModeEnabledListener( |
| 1979 | const sp<gui::ITunnelModeEnabledListener>& listener) { |
| 1980 | if (!listener) { |
| 1981 | return BAD_VALUE; |
| 1982 | } |
| 1983 | |
| 1984 | mTunnelModeEnabledReporter->removeListener(listener); |
| 1985 | return NO_ERROR; |
| 1986 | } |
| 1987 | |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 1988 | status_t SurfaceFlinger::getDisplayBrightnessSupport(const sp<IBinder>& displayToken, |
| 1989 | bool* outSupport) const { |
| 1990 | if (!displayToken || !outSupport) { |
| 1991 | return BAD_VALUE; |
| 1992 | } |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 1993 | |
| 1994 | Mutex::Autolock lock(mStateLock); |
| 1995 | |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 1996 | const auto displayId = getPhysicalDisplayIdLocked(displayToken); |
| 1997 | if (!displayId) { |
| 1998 | return NAME_NOT_FOUND; |
| 1999 | } |
Leon Scroggins III | 5967aec | 2021-12-29 11:14:22 -0500 | [diff] [blame] | 2000 | *outSupport = getHwComposer().hasDisplayCapability(*displayId, DisplayCapability::BRIGHTNESS); |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 2001 | return NO_ERROR; |
| 2002 | } |
| 2003 | |
John Reck | 22be696 | 2021-03-10 12:59:54 -0500 | [diff] [blame] | 2004 | status_t SurfaceFlinger::setDisplayBrightness(const sp<IBinder>& displayToken, |
| 2005 | const gui::DisplayBrightness& brightness) { |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 2006 | if (!displayToken) { |
| 2007 | return BAD_VALUE; |
| 2008 | } |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 2009 | |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 2010 | const char* const whence = __func__; |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 2011 | return ftl::Future(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) { |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 2012 | // 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] | 2013 | if (const auto display = getDisplayDeviceLocked(displayToken)) { |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 2014 | const bool supportsDisplayBrightnessCommand = |
| 2015 | getHwComposer().getComposer()->isSupported( |
| 2016 | Hwc2::Composer::OptionalFeature::DisplayBrightnessCommand); |
| 2017 | // If we support applying display brightness as a command, then we also support |
| 2018 | // dimming SDR layers. |
Alec Mouri | 90a1927 | 2021-12-30 14:11:38 -0800 | [diff] [blame] | 2019 | if (supportsDisplayBrightnessCommand) { |
Alec Mouri | 6da0e27 | 2022-02-07 12:45:57 -0800 | [diff] [blame] | 2020 | auto compositionDisplay = display->getCompositionDisplay(); |
| 2021 | float currentDimmingRatio = |
| 2022 | compositionDisplay->editState().sdrWhitePointNits / |
| 2023 | compositionDisplay->editState().displayBrightnessNits; |
Sally Qi | 0ea47b5 | 2023-06-22 13:27:20 -0700 | [diff] [blame] | 2024 | static constexpr float kDimmingThreshold = 0.02f; |
| 2025 | if (brightness.sdrWhitePointNits == 0.f || |
| 2026 | abs(brightness.sdrWhitePointNits - brightness.displayBrightnessNits) / |
| 2027 | brightness.sdrWhitePointNits >= |
| 2028 | kDimmingThreshold) { |
| 2029 | // to optimize, skip brightness setter if the brightness difference ratio |
| 2030 | // is lower than threshold |
| 2031 | compositionDisplay |
| 2032 | ->setDisplayBrightness(brightness.sdrWhitePointNits, |
| 2033 | brightness.displayBrightnessNits); |
| 2034 | } else { |
| 2035 | compositionDisplay->setDisplayBrightness(brightness.sdrWhitePointNits, |
| 2036 | brightness.sdrWhitePointNits); |
| 2037 | } |
| 2038 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 2039 | FTL_FAKE_GUARD(kMainThreadContext, |
| 2040 | display->stageBrightness(brightness.displayBrightness)); |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 2041 | float currentHdrSdrRatio = |
| 2042 | compositionDisplay->editState().displayBrightnessNits / |
| 2043 | compositionDisplay->editState().sdrWhitePointNits; |
| 2044 | FTL_FAKE_GUARD(kMainThreadContext, |
| 2045 | display->updateHdrSdrRatioOverlayRatio(currentHdrSdrRatio)); |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 2046 | |
Alec Mouri | 6da0e27 | 2022-02-07 12:45:57 -0800 | [diff] [blame] | 2047 | if (brightness.sdrWhitePointNits / brightness.displayBrightnessNits != |
| 2048 | currentDimmingRatio) { |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 2049 | scheduleComposite(FrameHint::kNone); |
| 2050 | } else { |
| 2051 | scheduleCommit(FrameHint::kNone); |
| 2052 | } |
| 2053 | return ftl::yield<status_t>(OK); |
| 2054 | } else { |
Alec Mouri | 90a1927 | 2021-12-30 14:11:38 -0800 | [diff] [blame] | 2055 | return getHwComposer() |
| 2056 | .setDisplayBrightness(display->getPhysicalId(), |
| 2057 | brightness.displayBrightness, |
Alec Mouri | 4d8a05d | 2022-03-23 18:14:26 +0000 | [diff] [blame] | 2058 | brightness.displayBrightnessNits, |
Alec Mouri | 90a1927 | 2021-12-30 14:11:38 -0800 | [diff] [blame] | 2059 | Hwc2::Composer::DisplayBrightnessOptions{ |
| 2060 | .applyImmediately = true}); |
John Reck | ac09e45 | 2021-04-07 16:35:37 -0400 | [diff] [blame] | 2061 | } |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 2062 | } else { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 2063 | ALOGE("%s: Invalid display token %p", whence, displayToken.get()); |
Dominik Laskowski | 4e2b71f | 2020-11-10 15:05:32 -0800 | [diff] [blame] | 2064 | return ftl::yield<status_t>(NAME_NOT_FOUND); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 2065 | } |
Dominik Laskowski | 5690bde | 2020-04-23 19:04:22 -0700 | [diff] [blame] | 2066 | })) |
Dominik Laskowski | b17c621 | 2022-05-09 09:36:19 -0700 | [diff] [blame] | 2067 | .then([](ftl::Future<status_t> task) { return task; }) |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 2068 | .get(); |
Dan Gittik | 57e63c5 | 2019-01-18 16:37:54 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 2071 | status_t SurfaceFlinger::addHdrLayerInfoListener(const sp<IBinder>& displayToken, |
| 2072 | const sp<gui::IHdrLayerInfoListener>& listener) { |
| 2073 | if (!displayToken) { |
| 2074 | return BAD_VALUE; |
| 2075 | } |
| 2076 | |
| 2077 | Mutex::Autolock lock(mStateLock); |
| 2078 | |
| 2079 | const auto display = getDisplayDeviceLocked(displayToken); |
| 2080 | if (!display) { |
| 2081 | return NAME_NOT_FOUND; |
| 2082 | } |
| 2083 | const auto displayId = display->getId(); |
| 2084 | sp<HdrLayerInfoReporter>& hdrInfoReporter = mHdrLayerInfoListeners[displayId]; |
| 2085 | if (!hdrInfoReporter) { |
| 2086 | hdrInfoReporter = sp<HdrLayerInfoReporter>::make(); |
| 2087 | } |
| 2088 | hdrInfoReporter->addListener(listener); |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 2089 | |
| 2090 | |
| 2091 | mAddingHDRLayerInfoListener = true; |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 2092 | return OK; |
| 2093 | } |
| 2094 | |
| 2095 | status_t SurfaceFlinger::removeHdrLayerInfoListener( |
| 2096 | const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) { |
| 2097 | if (!displayToken) { |
| 2098 | return BAD_VALUE; |
| 2099 | } |
| 2100 | |
| 2101 | Mutex::Autolock lock(mStateLock); |
| 2102 | |
| 2103 | const auto display = getDisplayDeviceLocked(displayToken); |
| 2104 | if (!display) { |
| 2105 | return NAME_NOT_FOUND; |
| 2106 | } |
| 2107 | const auto displayId = display->getId(); |
| 2108 | sp<HdrLayerInfoReporter>& hdrInfoReporter = mHdrLayerInfoListeners[displayId]; |
| 2109 | if (hdrInfoReporter) { |
| 2110 | hdrInfoReporter->removeListener(listener); |
| 2111 | } |
| 2112 | return OK; |
| 2113 | } |
| 2114 | |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 2115 | status_t SurfaceFlinger::notifyPowerBoost(int32_t boostId) { |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 2116 | using aidl::android::hardware::power::Boost; |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 2117 | Boost powerBoost = static_cast<Boost>(boostId); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 2118 | |
Lais Andrade | 3a6e47d | 2020-04-02 11:20:16 +0100 | [diff] [blame] | 2119 | if (powerBoost == Boost::INTERACTION) { |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2120 | mScheduler->onTouchHint(); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | return NO_ERROR; |
| 2124 | } |
| 2125 | |
Leon Scroggins III | e7c51c6 | 2022-02-01 15:53:54 -0500 | [diff] [blame] | 2126 | status_t SurfaceFlinger::getDisplayDecorationSupport( |
| 2127 | const sp<IBinder>& displayToken, |
| 2128 | std::optional<DisplayDecorationSupport>* outSupport) const { |
Leon Scroggins III | e5cff63 | 2021-12-29 11:53:36 -0500 | [diff] [blame] | 2129 | if (!displayToken || !outSupport) { |
| 2130 | return BAD_VALUE; |
| 2131 | } |
| 2132 | |
| 2133 | Mutex::Autolock lock(mStateLock); |
| 2134 | |
| 2135 | const auto displayId = getPhysicalDisplayIdLocked(displayToken); |
| 2136 | if (!displayId) { |
| 2137 | return NAME_NOT_FOUND; |
| 2138 | } |
Leon Scroggins III | e7c51c6 | 2022-02-01 15:53:54 -0500 | [diff] [blame] | 2139 | getHwComposer().getDisplayDecorationSupport(*displayId, outSupport); |
Leon Scroggins III | e5cff63 | 2021-12-29 11:53:36 -0500 | [diff] [blame] | 2140 | return NO_ERROR; |
| 2141 | } |
| 2142 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 2143 | // ---------------------------------------------------------------------------- |
| 2144 | |
Jorim Jaggi | b1e2f8d | 2017-06-08 15:43:59 -0700 | [diff] [blame] | 2145 | sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection( |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 2146 | gui::ISurfaceComposer::VsyncSource vsyncSource, EventRegistrationFlags eventRegistration, |
| 2147 | const sp<IBinder>& layerHandle) { |
Ady Abraham | b334758 | 2024-04-03 01:01:51 +0000 | [diff] [blame] | 2148 | const auto cycle = [&] { |
| 2149 | if (FlagManager::getInstance().deprecate_vsync_sf()) { |
| 2150 | ALOGW_IF(vsyncSource == gui::ISurfaceComposer::VsyncSource::eVsyncSourceSurfaceFlinger, |
| 2151 | "requested unsupported config eVsyncSourceSurfaceFlinger"); |
| 2152 | return scheduler::Cycle::Render; |
| 2153 | } |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 2154 | |
Ady Abraham | b334758 | 2024-04-03 01:01:51 +0000 | [diff] [blame] | 2155 | return vsyncSource == gui::ISurfaceComposer::VsyncSource::eVsyncSourceSurfaceFlinger |
| 2156 | ? scheduler::Cycle::LastComposite |
| 2157 | : scheduler::Cycle::Render; |
| 2158 | }(); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 2159 | return mScheduler->createDisplayEventConnection(cycle, eventRegistration, layerHandle); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
Ady Abraham | 9953e06 | 2024-07-24 22:56:22 -0700 | [diff] [blame] | 2162 | void SurfaceFlinger::scheduleCommit(FrameHint hint, Duration workDurationSlack) { |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2163 | if (hint == FrameHint::kActive) { |
| 2164 | mScheduler->resetIdleTimer(); |
| 2165 | } |
Matt Buckley | 15ecd1c | 2022-11-01 21:57:16 +0000 | [diff] [blame] | 2166 | mPowerAdvisor->notifyDisplayUpdateImminentAndCpuReset(); |
Ady Abraham | 9953e06 | 2024-07-24 22:56:22 -0700 | [diff] [blame] | 2167 | mScheduler->scheduleFrame(workDurationSlack); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 2168 | } |
| 2169 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2170 | void SurfaceFlinger::scheduleComposite(FrameHint hint) { |
| 2171 | mMustComposite = true; |
| 2172 | scheduleCommit(hint); |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | void SurfaceFlinger::scheduleRepaint() { |
| 2176 | mGeometryDirty = true; |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2177 | scheduleComposite(FrameHint::kActive); |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2180 | void SurfaceFlinger::scheduleSample() { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 2181 | static_cast<void>(mScheduler->schedule([this] { sample(); })); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 2182 | } |
| 2183 | |
Dominik Laskowski | 0deb06e | 2021-04-16 23:18:31 -0700 | [diff] [blame] | 2184 | void SurfaceFlinger::onComposerHalVsync(hal::HWDisplayId hwcDisplayId, int64_t timestamp, |
| 2185 | std::optional<hal::VsyncPeriodNanos> vsyncPeriod) { |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 2186 | if (FlagManager::getInstance().connected_display() && timestamp < 0 && |
| 2187 | vsyncPeriod.has_value()) { |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 2188 | if (mIsHdcpViaNegVsync && vsyncPeriod.value() == ~1) { |
| 2189 | const int32_t value = static_cast<int32_t>(-timestamp); |
| 2190 | // one byte is good enough to encode android.hardware.drm.HdcpLevel |
| 2191 | const int32_t maxLevel = (value >> 8) & 0xFF; |
| 2192 | const int32_t connectedLevel = value & 0xFF; |
Huihong Luo | 817ebb8 | 2023-12-13 12:54:24 -0800 | [diff] [blame] | 2193 | ALOGD("%s: HDCP levels changed (connected=%d, max=%d) for hwcDisplayId %" PRIu64, |
| 2194 | __func__, connectedLevel, maxLevel, hwcDisplayId); |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 2195 | updateHdcpLevels(hwcDisplayId, connectedLevel, maxLevel); |
| 2196 | return; |
| 2197 | } |
Huihong Luo | aac3a77 | 2023-09-19 20:51:59 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2200 | SFTRACE_NAME(vsyncPeriod |
| 2201 | ? ftl::Concat(__func__, ' ', hwcDisplayId, ' ', *vsyncPeriod, "ns").c_str() |
| 2202 | : ftl::Concat(__func__, ' ', hwcDisplayId).c_str()); |
Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 2203 | |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 2204 | Mutex::Autolock lock(mStateLock); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 2205 | if (const auto displayIdOpt = getHwComposer().onVsync(hwcDisplayId, timestamp)) { |
| 2206 | if (mScheduler->addResyncSample(*displayIdOpt, timestamp, vsyncPeriod)) { |
| 2207 | // period flushed |
| 2208 | mScheduler->modulateVsync(displayIdOpt, &VsyncModulator::onRefreshRateChangeCompleted); |
| 2209 | } |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 2210 | } |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 2211 | } |
| 2212 | |
Huihong Luo | 791bef9 | 2023-09-19 21:18:45 -0700 | [diff] [blame] | 2213 | void SurfaceFlinger::onComposerHalHotplugEvent(hal::HWDisplayId hwcDisplayId, |
| 2214 | DisplayHotplugEvent event) { |
| 2215 | if (event == DisplayHotplugEvent::CONNECTED || event == DisplayHotplugEvent::DISCONNECTED) { |
| 2216 | hal::Connection connection = (event == DisplayHotplugEvent::CONNECTED) |
| 2217 | ? hal::Connection::CONNECTED |
| 2218 | : hal::Connection::DISCONNECTED; |
| 2219 | { |
| 2220 | std::lock_guard<std::mutex> lock(mHotplugMutex); |
| 2221 | mPendingHotplugEvents.push_back(HotplugEvent{hwcDisplayId, connection}); |
| 2222 | } |
| 2223 | |
| 2224 | if (mScheduler) { |
| 2225 | mScheduler->scheduleConfigure(); |
| 2226 | } |
| 2227 | |
| 2228 | return; |
Jiwen 'Steve' Cai | ccfd682 | 2018-02-21 16:15:58 -0800 | [diff] [blame] | 2229 | } |
| 2230 | |
Huihong Luo | 791bef9 | 2023-09-19 21:18:45 -0700 | [diff] [blame] | 2231 | if (FlagManager::getInstance().hotplug2()) { |
Huihong Luo | 791bef9 | 2023-09-19 21:18:45 -0700 | [diff] [blame] | 2232 | // TODO(b/311403559): use enum type instead of int |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 2233 | const auto errorCode = static_cast<int32_t>(event); |
Huihong Luo | 817ebb8 | 2023-12-13 12:54:24 -0800 | [diff] [blame] | 2234 | ALOGD("%s: Hotplug error %d for hwcDisplayId %" PRIu64, __func__, errorCode, hwcDisplayId); |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 2235 | mScheduler->dispatchHotplugError(errorCode); |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 2236 | } |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 2237 | } |
| 2238 | |
Dominik Laskowski | 0deb06e | 2021-04-16 23:18:31 -0700 | [diff] [blame] | 2239 | void SurfaceFlinger::onComposerHalVsyncPeriodTimingChanged( |
| 2240 | hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline& timeline) { |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 2241 | Mutex::Autolock lock(mStateLock); |
Dominik Laskowski | 0deb06e | 2021-04-16 23:18:31 -0700 | [diff] [blame] | 2242 | mScheduler->onNewVsyncPeriodChangeTimeline(timeline); |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame] | 2243 | |
| 2244 | if (timeline.refreshRequired) { |
| 2245 | scheduleComposite(FrameHint::kNone); |
| 2246 | } |
Ady Abraham | 7159f57 | 2019-10-11 11:10:18 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
Dominik Laskowski | 0deb06e | 2021-04-16 23:18:31 -0700 | [diff] [blame] | 2249 | void SurfaceFlinger::onComposerHalSeamlessPossible(hal::HWDisplayId) { |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 2250 | // TODO(b/142753666): use constraints when calling to setActiveModeWithConstraints and |
Ady Abraham | b0433bc | 2020-01-08 17:31:06 -0800 | [diff] [blame] | 2251 | // use this callback to know when to retry in case of SEAMLESS_NOT_POSSIBLE. |
| 2252 | } |
| 2253 | |
Dominik Laskowski | 0deb06e | 2021-04-16 23:18:31 -0700 | [diff] [blame] | 2254 | void SurfaceFlinger::onComposerHalRefresh(hal::HWDisplayId) { |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 2255 | Mutex::Autolock lock(mStateLock); |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2256 | scheduleComposite(FrameHint::kNone); |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 2257 | } |
| 2258 | |
Yichi Chen | 3401b56 | 2022-01-17 15:42:35 +0800 | [diff] [blame] | 2259 | void SurfaceFlinger::onComposerHalVsyncIdle(hal::HWDisplayId) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2260 | SFTRACE_CALL(); |
Ady Abraham | e9befd7 | 2022-02-24 17:24:44 -0800 | [diff] [blame] | 2261 | mScheduler->forceNextResync(); |
Yichi Chen | 3401b56 | 2022-01-17 15:42:35 +0800 | [diff] [blame] | 2262 | } |
| 2263 | |
ramindani | b2158ee | 2023-02-13 20:29:59 -0800 | [diff] [blame] | 2264 | void SurfaceFlinger::onRefreshRateChangedDebug(const RefreshRateChangedDebugData& data) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2265 | SFTRACE_CALL(); |
Dominik Laskowski | 7372a60 | 2024-04-29 16:40:42 -0400 | [diff] [blame] | 2266 | const char* const whence = __func__; |
| 2267 | static_cast<void>(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD( |
| 2268 | kMainThreadContext) { |
| 2269 | if (const auto displayIdOpt = getHwComposer().toPhysicalDisplayId(data.display)) { |
| 2270 | if (const auto display = getDisplayDeviceLocked(*displayIdOpt)) { |
Ying Wei | 22d59cc | 2024-05-11 02:41:08 +0000 | [diff] [blame] | 2271 | const Fps refreshRate = Fps::fromPeriodNsecs( |
| 2272 | getHwComposer().getComposer()->isVrrSupported() ? data.refreshPeriodNanos |
| 2273 | : data.vsyncPeriodNanos); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2274 | SFTRACE_FORMAT("%s refresh rate = %d", whence, refreshRate.getIntValue()); |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 2275 | |
| 2276 | const auto renderRate = mDisplayModeController.getActiveMode(*displayIdOpt).fps; |
| 2277 | constexpr bool kSetByHwc = true; |
| 2278 | display->updateRefreshRateOverlayRate(refreshRate, renderRate, kSetByHwc); |
Dominik Laskowski | 7372a60 | 2024-04-29 16:40:42 -0400 | [diff] [blame] | 2279 | } |
| 2280 | } |
| 2281 | })); |
ramindani | 12bfe6b | 2023-02-03 13:29:19 -0800 | [diff] [blame] | 2282 | } |
| 2283 | |
Huihong Luo | 817ebb8 | 2023-12-13 12:54:24 -0800 | [diff] [blame] | 2284 | void SurfaceFlinger::onComposerHalHdcpLevelsChanged(hal::HWDisplayId hwcDisplayId, |
| 2285 | const HdcpLevels& levels) { |
| 2286 | if (FlagManager::getInstance().hdcp_level_hal()) { |
| 2287 | // TODO(b/362270040): propagate enum constants |
| 2288 | const int32_t maxLevel = static_cast<int32_t>(levels.maxLevel); |
| 2289 | const int32_t connectedLevel = static_cast<int32_t>(levels.connectedLevel); |
| 2290 | ALOGD("%s: HDCP levels changed (connected=%d, max=%d) for hwcDisplayId %" PRIu64, __func__, |
| 2291 | connectedLevel, maxLevel, hwcDisplayId); |
| 2292 | updateHdcpLevels(hwcDisplayId, connectedLevel, maxLevel); |
| 2293 | } |
| 2294 | } |
| 2295 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2296 | void SurfaceFlinger::configure() { |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 2297 | Mutex::Autolock lock(mStateLock); |
| 2298 | if (configureLocked()) { |
| 2299 | setTransactionFlags(eDisplayTransactionNeeded); |
| 2300 | } |
| 2301 | } |
| 2302 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2303 | void SurfaceFlinger::updateLayerHistory(nsecs_t now) { |
| 2304 | for (const auto& snapshot : mLayerSnapshotBuilder.getSnapshots()) { |
| 2305 | using Changes = frontend::RequestedLayerState::Changes; |
| 2306 | if (snapshot->path.isClone()) { |
| 2307 | continue; |
| 2308 | } |
Vishnu Nair | 80e8cfe | 2023-09-29 17:03:45 -0700 | [diff] [blame] | 2309 | |
Jerry Chang | 3667800 | 2023-11-29 16:56:17 +0000 | [diff] [blame] | 2310 | const bool updateSmallDirty = FlagManager::getInstance().enable_small_area_detection() && |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 2311 | ((snapshot->clientChanges & layer_state_t::eSurfaceDamageRegionChanged) || |
| 2312 | snapshot->changes.any(Changes::Geometry)); |
| 2313 | |
| 2314 | const bool hasChanges = |
Vishnu Nair | 41376b6 | 2023-11-08 05:08:58 -0800 | [diff] [blame] | 2315 | snapshot->changes.any(Changes::FrameRate | Changes::Buffer | Changes::Animation | |
| 2316 | Changes::Geometry | Changes::Visibility) || |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 2317 | (snapshot->clientChanges & layer_state_t::eDefaultFrameRateCompatibilityChanged) != |
| 2318 | 0; |
| 2319 | |
| 2320 | if (!updateSmallDirty && !hasChanges) { |
| 2321 | continue; |
| 2322 | } |
| 2323 | |
| 2324 | auto it = mLegacyLayers.find(snapshot->sequence); |
Vishnu Nair | 5943bf6 | 2023-12-08 22:04:20 +0000 | [diff] [blame] | 2325 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 2326 | "Couldn't find layer object for %s", |
| 2327 | snapshot->getDebugString().c_str()); |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 2328 | |
| 2329 | if (updateSmallDirty) { |
| 2330 | // Update small dirty flag while surface damage region or geometry changed |
| 2331 | it->second->setIsSmallDirty(snapshot.get()); |
| 2332 | } |
| 2333 | |
| 2334 | if (!hasChanges) { |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2335 | continue; |
| 2336 | } |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 2337 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2338 | const auto layerProps = scheduler::LayerProps{ |
| 2339 | .visible = snapshot->isVisible, |
| 2340 | .bounds = snapshot->geomLayerBounds, |
| 2341 | .transform = snapshot->geomLayerTransform, |
| 2342 | .setFrameRateVote = snapshot->frameRate, |
| 2343 | .frameRateSelectionPriority = snapshot->frameRateSelectionPriority, |
Arthur Hung | 69f9522 | 2023-10-04 07:39:02 +0000 | [diff] [blame] | 2344 | .isSmallDirty = snapshot->isSmallDirty, |
Alec Mouri | 89f5d4e | 2023-10-20 17:12:49 +0000 | [diff] [blame] | 2345 | .isFrontBuffered = snapshot->isFrontBuffered(), |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2346 | }; |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 2347 | |
Vishnu Nair | 41376b6 | 2023-11-08 05:08:58 -0800 | [diff] [blame] | 2348 | if (snapshot->changes.any(Changes::Geometry | Changes::Visibility)) { |
| 2349 | mScheduler->setLayerProperties(snapshot->sequence, layerProps); |
| 2350 | } |
| 2351 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2352 | if (snapshot->clientChanges & layer_state_t::eDefaultFrameRateCompatibilityChanged) { |
| 2353 | mScheduler->setDefaultFrameRateCompatibility(snapshot->sequence, |
| 2354 | snapshot->defaultFrameRateCompatibility); |
| 2355 | } |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 2356 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2357 | if (snapshot->changes.test(Changes::Animation)) { |
| 2358 | it->second->recordLayerHistoryAnimationTx(layerProps, now); |
| 2359 | } |
Vishnu Nair | 80e8cfe | 2023-09-29 17:03:45 -0700 | [diff] [blame] | 2360 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2361 | if (snapshot->changes.test(Changes::FrameRate)) { |
| 2362 | it->second->setFrameRateForLayerTree(snapshot->frameRate, layerProps, now); |
| 2363 | } |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 2364 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 2365 | if (snapshot->changes.test(Changes::Buffer)) { |
| 2366 | it->second->recordLayerHistoryBufferUpdate(layerProps, now); |
| 2367 | } |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 2368 | } |
| 2369 | } |
| 2370 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2371 | bool SurfaceFlinger::updateLayerSnapshots(VsyncId vsyncId, nsecs_t frameTimeNs, |
| 2372 | bool flushTransactions, bool& outTransactionsAreEmpty) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2373 | using Changes = frontend::RequestedLayerState::Changes; |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2374 | SFTRACE_CALL(); |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2375 | frontend::Update update; |
| 2376 | if (flushTransactions) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2377 | SFTRACE_NAME("TransactionHandler:flushTransactions"); |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2378 | // Locking: |
| 2379 | // 1. to prevent onHandleDestroyed from being called while the state lock is held, |
| 2380 | // we must keep a copy of the transactions (specifically the composer |
| 2381 | // states) around outside the scope of the lock. |
| 2382 | // 2. Transactions and created layers do not share a lock. To prevent applying |
| 2383 | // transactions with layers still in the createdLayer queue, collect the transactions |
| 2384 | // before committing the created layers. |
| 2385 | // 3. Transactions can only be flushed after adding layers, since the layer can be a newly |
| 2386 | // created one |
| 2387 | mTransactionHandler.collectTransactions(); |
| 2388 | { |
| 2389 | // TODO(b/238781169) lockless queue this and keep order. |
| 2390 | std::scoped_lock<std::mutex> lock(mCreatedLayersLock); |
Vishnu Nair | a12c5c7 | 2024-08-28 15:42:17 -0700 | [diff] [blame] | 2391 | update.legacyLayers = std::move(mCreatedLayers); |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2392 | mCreatedLayers.clear(); |
| 2393 | update.newLayers = std::move(mNewLayers); |
| 2394 | mNewLayers.clear(); |
| 2395 | update.layerCreationArgs = std::move(mNewLayerArgs); |
| 2396 | mNewLayerArgs.clear(); |
| 2397 | update.destroyedHandles = std::move(mDestroyedHandles); |
| 2398 | mDestroyedHandles.clear(); |
| 2399 | } |
| 2400 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2401 | mLayerLifecycleManager.addLayers(std::move(update.newLayers)); |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 2402 | update.transactions = mTransactionHandler.flushTransactions(); |
| 2403 | if (mTransactionTracing) { |
| 2404 | mTransactionTracing->addCommittedTransactions(ftl::to_underlying(vsyncId), frameTimeNs, |
| 2405 | update, mFrontEndDisplayInfos, |
| 2406 | mFrontEndDisplayInfosChanged); |
| 2407 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2408 | mLayerLifecycleManager.applyTransactions(update.transactions); |
| 2409 | mLayerLifecycleManager.onHandlesDestroyed(update.destroyedHandles); |
Vishnu Nair | a12c5c7 | 2024-08-28 15:42:17 -0700 | [diff] [blame] | 2410 | for (auto& legacyLayer : update.legacyLayers) { |
| 2411 | mLegacyLayers[legacyLayer->sequence] = legacyLayer; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2412 | } |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 2413 | mLayerHierarchyBuilder.update(mLayerLifecycleManager); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 2416 | // Keep a copy of the drawing state (that is going to be overwritten |
| 2417 | // by commitTransactionsLocked) outside of mStateLock so that the side |
| 2418 | // effects of the State assignment don't happen with mStateLock held, |
| 2419 | // which can cause deadlocks. |
| 2420 | State drawingState(mDrawingState); |
| 2421 | Mutex::Autolock lock(mStateLock); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 2422 | bool mustComposite = false; |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 2423 | mustComposite |= applyAndCommitDisplayTransactionStatesLocked(update.transactions); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2424 | |
| 2425 | { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2426 | SFTRACE_NAME("LayerSnapshotBuilder:update"); |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 2427 | frontend::LayerSnapshotBuilder::Args |
| 2428 | args{.root = mLayerHierarchyBuilder.getHierarchy(), |
| 2429 | .layerLifecycleManager = mLayerLifecycleManager, |
Melody Hsu | 63e3dcc | 2024-04-03 23:22:20 +0000 | [diff] [blame] | 2430 | .includeMetadata = mCompositionEngine->getFeatureFlags().test( |
| 2431 | compositionengine::Feature::kSnapshotLayerMetadata), |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 2432 | .displays = mFrontEndDisplayInfos, |
| 2433 | .displayChanges = mFrontEndDisplayInfosChanged, |
| 2434 | .globalShadowSettings = mDrawingState.globalShadowSettings, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 2435 | .supportsBlur = mSupportsBlur, |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 2436 | .forceFullDamage = mForceFullDamage, |
| 2437 | .supportedLayerGenericMetadata = |
| 2438 | getHwComposer().getSupportedLayerGenericMetadata(), |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 2439 | .genericLayerMetadataKeyMap = getGenericLayerMetadataKeyMap(), |
| 2440 | .skipRoundCornersWhenProtected = |
| 2441 | !getRenderEngine().supportsProtectedContent()}; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2442 | mLayerSnapshotBuilder.update(args); |
| 2443 | } |
| 2444 | |
| 2445 | if (mLayerLifecycleManager.getGlobalChanges().any(Changes::Geometry | Changes::Input | |
Vishnu Nair | 40d0228 | 2023-02-28 21:11:40 +0000 | [diff] [blame] | 2446 | Changes::Hierarchy | Changes::Visibility)) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2447 | mUpdateInputInfo = true; |
| 2448 | } |
| 2449 | if (mLayerLifecycleManager.getGlobalChanges().any(Changes::VisibleRegion | Changes::Hierarchy | |
Vishnu Nair | 3996ee3 | 2023-08-14 04:32:31 +0000 | [diff] [blame] | 2450 | Changes::Visibility | Changes::Geometry)) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2451 | mVisibleRegionsDirty = true; |
| 2452 | } |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 2453 | if (mLayerLifecycleManager.getGlobalChanges().any(Changes::Hierarchy | Changes::FrameRate)) { |
| 2454 | // The frame rate of attached choreographers can only change as a result of a |
| 2455 | // FrameRate change (including when Hierarchy changes). |
| 2456 | mUpdateAttachedChoreographer = true; |
| 2457 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2458 | outTransactionsAreEmpty = mLayerLifecycleManager.getGlobalChanges().get() == 0; |
Vishnu Nair | df59f47 | 2024-05-17 16:51:33 +0000 | [diff] [blame] | 2459 | if (FlagManager::getInstance().vrr_bugfix_24q4()) { |
Ady Abraham | 6846ade | 2024-05-20 21:58:27 +0000 | [diff] [blame] | 2460 | mustComposite |= mLayerLifecycleManager.getGlobalChanges().any( |
| 2461 | frontend::RequestedLayerState::kMustComposite); |
Vishnu Nair | df59f47 | 2024-05-17 16:51:33 +0000 | [diff] [blame] | 2462 | } else { |
| 2463 | mustComposite |= mLayerLifecycleManager.getGlobalChanges().get() != 0; |
| 2464 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2465 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 2466 | bool newDataLatched = false; |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2467 | SFTRACE_NAME("DisplayCallbackAndStatsUpdates"); |
Shuangxi Xiang | a97d59d | 2024-08-27 07:28:20 +0000 | [diff] [blame] | 2468 | mustComposite |= applyTransactionsLocked(update.transactions); |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2469 | traverseLegacyLayers([&](Layer* layer) { layer->commitTransaction(); }); |
| 2470 | const nsecs_t latchTime = systemTime(); |
| 2471 | bool unused = false; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2472 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2473 | for (auto& layer : mLayerLifecycleManager.getLayers()) { |
| 2474 | if (layer->changes.test(frontend::RequestedLayerState::Changes::Created) && |
| 2475 | layer->bgColorLayer) { |
| 2476 | sp<Layer> bgColorLayer = getFactory().createEffectLayer( |
| 2477 | LayerCreationArgs(this, nullptr, layer->name, |
| 2478 | ISurfaceComposerClient::eFXSurfaceEffect, LayerMetadata(), |
| 2479 | std::make_optional(layer->id), true)); |
| 2480 | mLegacyLayers[bgColorLayer->sequence] = bgColorLayer; |
| 2481 | } |
| 2482 | const bool willReleaseBufferOnLatch = layer->willReleaseBufferOnLatch(); |
Matthew DeVore | ce6bde8 | 2024-04-28 16:58:06 +0000 | [diff] [blame] | 2483 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2484 | auto it = mLegacyLayers.find(layer->id); |
| 2485 | if (it == mLegacyLayers.end() && |
| 2486 | layer->changes.test(frontend::RequestedLayerState::Changes::Destroyed)) { |
| 2487 | // Layer handle was created and immediately destroyed. It was destroyed before it |
| 2488 | // was added to the map. |
| 2489 | continue; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2492 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 2493 | "Couldnt find layer object for %s", |
| 2494 | layer->getDebugString().c_str()); |
| 2495 | if (!layer->hasReadyFrame() && !willReleaseBufferOnLatch) { |
| 2496 | if (!it->second->hasBuffer()) { |
| 2497 | // The last latch time is used to classify a missed frame as buffer stuffing |
| 2498 | // instead of a missed frame. This is used to identify scenarios where we |
| 2499 | // could not latch a buffer or apply a transaction due to backpressure. |
| 2500 | // We only update the latch time for buffer less layers here, the latch time |
| 2501 | // is updated for buffer layers when the buffer is latched. |
| 2502 | it->second->updateLastLatchTime(latchTime); |
| 2503 | } |
| 2504 | continue; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2507 | const bool bgColorOnly = |
| 2508 | !layer->externalTexture && (layer->bgColorLayerId != UNASSIGNED_LAYER_ID); |
| 2509 | if (willReleaseBufferOnLatch) { |
| 2510 | mLayersWithBuffersRemoved.emplace(it->second); |
Matthew DeVore | ce6bde8 | 2024-04-28 16:58:06 +0000 | [diff] [blame] | 2511 | } |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2512 | it->second->latchBufferImpl(unused, latchTime, bgColorOnly); |
| 2513 | newDataLatched = true; |
Matthew DeVore | ce6bde8 | 2024-04-28 16:58:06 +0000 | [diff] [blame] | 2514 | |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 2515 | frontend::LayerSnapshot* snapshot = mLayerSnapshotBuilder.getSnapshot(it->second->sequence); |
| 2516 | gui::GameMode gameMode = (snapshot) ? snapshot->gameMode : gui::GameMode::Unsupported; |
| 2517 | mLayersWithQueuedFrames.emplace(it->second, gameMode); |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2518 | mLayersIdsWithQueuedFrames.emplace(it->second->sequence); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2519 | } |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2520 | |
| 2521 | updateLayerHistory(latchTime); |
Vishnu Nair | 438a0ac | 2024-08-15 08:09:32 +0000 | [diff] [blame] | 2522 | mLayerSnapshotBuilder.forEachSnapshot([&](const frontend::LayerSnapshot& snapshot) { |
| 2523 | // update output dirty region if we have a queued buffer that is visible or a snapshot |
| 2524 | // recently became invisible |
| 2525 | // TODO(b/360050020) investigate if we need to update dirty region when layer color changes |
| 2526 | if ((snapshot.isVisible && |
| 2527 | (mLayersIdsWithQueuedFrames.find(snapshot.path.id) != |
| 2528 | mLayersIdsWithQueuedFrames.end())) || |
| 2529 | (!snapshot.isVisible && snapshot.changes.test(Changes::Visibility))) { |
| 2530 | Region visibleReg; |
| 2531 | visibleReg.set(snapshot.transformedBoundsWithoutTransparentRegion); |
| 2532 | invalidateLayerStack(snapshot.outputFilter, visibleReg); |
| 2533 | } |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2534 | }); |
| 2535 | |
| 2536 | for (auto& destroyedLayer : mLayerLifecycleManager.getDestroyedLayers()) { |
| 2537 | mLegacyLayers.erase(destroyedLayer->id); |
| 2538 | } |
| 2539 | |
| 2540 | { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2541 | SFTRACE_NAME("LLM:commitChanges"); |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2542 | mLayerLifecycleManager.commitChanges(); |
| 2543 | } |
| 2544 | |
| 2545 | // enter boot animation on first buffer latch |
| 2546 | if (CC_UNLIKELY(mBootStage == BootStage::BOOTLOADER && newDataLatched)) { |
| 2547 | ALOGI("Enter boot animation"); |
| 2548 | mBootStage = BootStage::BOOTANIMATION; |
| 2549 | } |
| 2550 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 2551 | mustComposite |= (getTransactionFlags() & ~eTransactionFlushNeeded) || newDataLatched; |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2552 | if (mustComposite) { |
Vishnu Nair | 8134c1a | 2023-10-17 22:55:49 -0700 | [diff] [blame] | 2553 | commitTransactions(); |
| 2554 | } |
| 2555 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2556 | return mustComposite; |
| 2557 | } |
| 2558 | |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 2559 | bool SurfaceFlinger::commit(PhysicalDisplayId pacesetterId, |
| 2560 | const scheduler::FrameTargets& frameTargets) { |
| 2561 | const scheduler::FrameTarget& pacesetterFrameTarget = *frameTargets.get(pacesetterId)->get(); |
| 2562 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2563 | const VsyncId vsyncId = pacesetterFrameTarget.vsyncId(); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2564 | SFTRACE_NAME(ftl::Concat(__func__, ' ', ftl::to_underlying(vsyncId)).c_str()); |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2565 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2566 | if (pacesetterFrameTarget.didMissFrame()) { |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2567 | mTimeStats->incrementMissedFrames(); |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2568 | } |
| 2569 | |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 2570 | // If a mode set is pending and the fence hasn't fired yet, wait for the next commit. |
| 2571 | if (std::any_of(frameTargets.begin(), frameTargets.end(), |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 2572 | [this](const auto& pair) FTL_FAKE_GUARD(kMainThreadContext) { |
| 2573 | const auto [displayId, target] = pair; |
| 2574 | return target->isFramePending() && |
| 2575 | mDisplayModeController.isModeSetPending(displayId); |
| 2576 | })) { |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 2577 | mScheduler->scheduleFrame(); |
| 2578 | return false; |
| 2579 | } |
| 2580 | |
Dominik Laskowski | b2fc423 | 2024-07-15 08:24:49 -0400 | [diff] [blame] | 2581 | { |
| 2582 | ConditionalLock lock(mStateLock, FlagManager::getInstance().connected_display()); |
| 2583 | |
| 2584 | for (const auto [displayId, _] : frameTargets) { |
| 2585 | if (mDisplayModeController.isModeSetPending(displayId)) { |
| 2586 | finalizeDisplayModeChange(displayId); |
| 2587 | } |
yuhui.zhang | 087d374 | 2021-12-11 15:23:52 +0800 | [diff] [blame] | 2588 | } |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2589 | } |
| 2590 | |
Ady Abraham | ed1283a | 2024-07-24 15:49:16 -0700 | [diff] [blame] | 2591 | if (pacesetterFrameTarget.wouldBackpressureHwc()) { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 2592 | if (mBackpressureGpuComposition || pacesetterFrameTarget.didMissHwcFrame()) { |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 2593 | if (FlagManager::getInstance().vrr_config()) { |
| 2594 | mScheduler->getVsyncSchedule()->getTracker().onFrameMissed( |
| 2595 | pacesetterFrameTarget.expectedPresentTime()); |
| 2596 | } |
Ady Abraham | 9953e06 | 2024-07-24 22:56:22 -0700 | [diff] [blame] | 2597 | const Duration slack = FlagManager::getInstance().allow_n_vsyncs_in_targeter() |
| 2598 | ? TimePoint::now() - pacesetterFrameTarget.frameBeginTime() |
| 2599 | : Duration::fromNs(0); |
| 2600 | scheduleCommit(FrameHint::kNone, slack); |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2601 | return false; |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2602 | } |
| 2603 | } |
| 2604 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2605 | const Period vsyncPeriod = mScheduler->getVsyncSchedule()->period(); |
| 2606 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2607 | // Save this once per commit + composite to ensure consistency |
Matt Buckley | ba8d15a | 2022-08-02 20:10:15 +0000 | [diff] [blame] | 2608 | // TODO (b/240619471): consider removing active display check once AOD is fixed |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 2609 | const auto activeDisplay = FTL_FAKE_GUARD(mStateLock, getDisplayDeviceLocked(mActiveDisplayId)); |
Matt Buckley | ba8d15a | 2022-08-02 20:10:15 +0000 | [diff] [blame] | 2610 | mPowerHintSessionEnabled = mPowerAdvisor->usePowerHintSession() && activeDisplay && |
| 2611 | activeDisplay->getPowerMode() == hal::PowerMode::ON; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2612 | if (mPowerHintSessionEnabled) { |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2613 | mPowerAdvisor->setCommitStart(pacesetterFrameTarget.frameBeginTime()); |
| 2614 | mPowerAdvisor->setExpectedPresentTime(pacesetterFrameTarget.expectedPresentTime()); |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 2615 | |
| 2616 | // 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] | 2617 | const Duration idealSfWorkDuration = |
| 2618 | mScheduler->vsyncModulator().getVsyncConfig().sfWorkDuration; |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2619 | const Duration frameDelay = |
| 2620 | idealSfWorkDuration - pacesetterFrameTarget.expectedFrameDuration(); |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 2621 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 2622 | mPowerAdvisor->setFrameDelay(frameDelay); |
| 2623 | mPowerAdvisor->setTotalFrameTargetWorkDuration(idealSfWorkDuration); |
Matt Buckley | 676e439 | 2023-05-25 22:09:26 +0000 | [diff] [blame] | 2624 | |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 2625 | const Period idealVsyncPeriod = |
| 2626 | mDisplayModeController.getActiveMode(pacesetterId).fps.getPeriod(); |
Matt Buckley | 676e439 | 2023-05-25 22:09:26 +0000 | [diff] [blame] | 2627 | mPowerAdvisor->updateTargetWorkDuration(idealVsyncPeriod); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 2630 | if (mRefreshRateOverlaySpinner || mHdrSdrRatioOverlay) { |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 2631 | Mutex::Autolock lock(mStateLock); |
| 2632 | if (const auto display = getDefaultDisplayDeviceLocked()) { |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 2633 | display->animateOverlay(); |
Ady Abraham | 29d0da3 | 2020-07-16 18:39:33 -0700 | [diff] [blame] | 2634 | } |
| 2635 | } |
| 2636 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2637 | // Composite if transactions were committed, or if requested by HWC. |
| 2638 | bool mustComposite = mMustComposite.exchange(false); |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2639 | { |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2640 | mFrameTimeline->setSfWakeUp(ftl::to_underlying(vsyncId), |
| 2641 | pacesetterFrameTarget.frameBeginTime().ns(), |
Pascal Muetschard | ac7bcd9 | 2023-10-03 15:05:36 +0200 | [diff] [blame] | 2642 | Fps::fromPeriodNsecs(vsyncPeriod.ns()), |
| 2643 | mScheduler->getPacesetterRefreshRate()); |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 2644 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2645 | const bool flushTransactions = clearTransactionFlags(eTransactionFlushNeeded); |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 2646 | bool transactionsAreEmpty = false; |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 2647 | mustComposite |= updateLayerSnapshots(vsyncId, pacesetterFrameTarget.frameBeginTime().ns(), |
| 2648 | flushTransactions, transactionsAreEmpty); |
chaviw | 6b9ffea | 2021-11-08 09:25:48 -0600 | [diff] [blame] | 2649 | |
Ady Abraham | aa7ebd6 | 2024-03-26 02:27:57 +0000 | [diff] [blame] | 2650 | // Tell VsyncTracker that we are going to present this frame before scheduling |
| 2651 | // setTransactionFlags which will schedule another SF frame. This was if the tracker |
| 2652 | // needs to adjust the vsync timeline, it will be done before the next frame. |
| 2653 | if (FlagManager::getInstance().vrr_config() && mustComposite) { |
| 2654 | mScheduler->getVsyncSchedule()->getTracker().onFrameBegin( |
| 2655 | pacesetterFrameTarget.expectedPresentTime(), |
| 2656 | pacesetterFrameTarget.lastSignaledFrameTime()); |
| 2657 | } |
chaviw | 6b9ffea | 2021-11-08 09:25:48 -0600 | [diff] [blame] | 2658 | if (transactionFlushNeeded()) { |
| 2659 | setTransactionFlags(eTransactionFlushNeeded); |
| 2660 | } |
| 2661 | |
chaviw | 6b9ffea | 2021-11-08 09:25:48 -0600 | [diff] [blame] | 2662 | // This has to be called after latchBuffers because we want to include the layers that have |
| 2663 | // been latched in the commit callback |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 2664 | if (transactionsAreEmpty) { |
chaviw | 6b9ffea | 2021-11-08 09:25:48 -0600 | [diff] [blame] | 2665 | // Invoke empty transaction callbacks early. |
| 2666 | mTransactionCallbackInvoker.sendCallbacks(false /* onCommitOnly */); |
| 2667 | } else { |
| 2668 | // Invoke OnCommit callbacks. |
| 2669 | mTransactionCallbackInvoker.sendCallbacks(true /* onCommitOnly */); |
| 2670 | } |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | // Layers need to get updated (in the previous line) before we can use them for |
| 2674 | // choosing the refresh rate. |
| 2675 | // Hold mStateLock as chooseRefreshRateForContent promotes wp<Layer> to sp<Layer> |
| 2676 | // and may eventually call to ~Layer() if it holds the last reference |
| 2677 | { |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 2678 | bool updateAttachedChoreographer = mUpdateAttachedChoreographer; |
| 2679 | mUpdateAttachedChoreographer = false; |
| 2680 | |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 2681 | Mutex::Autolock lock(mStateLock); |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 2682 | mScheduler->chooseRefreshRateForContent(&mLayerHierarchyBuilder.getHierarchy(), |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 2683 | updateAttachedChoreographer); |
Dominik Laskowski | b2fc423 | 2024-07-15 08:24:49 -0400 | [diff] [blame] | 2684 | |
| 2685 | if (FlagManager::getInstance().connected_display()) { |
| 2686 | initiateDisplayModeChanges(); |
| 2687 | } |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2688 | } |
| 2689 | |
Dominik Laskowski | b2fc423 | 2024-07-15 08:24:49 -0400 | [diff] [blame] | 2690 | if (!FlagManager::getInstance().connected_display()) { |
| 2691 | ftl::FakeGuard guard(mStateLock); |
| 2692 | initiateDisplayModeChanges(); |
| 2693 | } |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 2694 | |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2695 | updateCursorAsync(); |
Vishnu Nair | d2bfbb3 | 2023-07-18 00:45:45 +0000 | [diff] [blame] | 2696 | if (!mustComposite) { |
| 2697 | updateInputFlinger(vsyncId, pacesetterFrameTarget.frameBeginTime()); |
| 2698 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 2699 | doActiveLayersTracingIfNeeded(false, mVisibleRegionsDirty, |
| 2700 | pacesetterFrameTarget.frameBeginTime(), vsyncId); |
Vishnu Nair | 1fe1117 | 2023-04-03 18:10:09 +0000 | [diff] [blame] | 2701 | |
Pablo Gamito | 6be8244 | 2022-06-13 11:04:34 +0000 | [diff] [blame] | 2702 | mLastCommittedVsyncId = vsyncId; |
Vishnu Nair | b64a3b4 | 2022-01-13 15:29:32 -0800 | [diff] [blame] | 2703 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 2704 | persistDisplayBrightness(mustComposite); |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 2705 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2706 | return mustComposite && CC_LIKELY(mBootStage != BootStage::BOOTLOADER); |
Dan Stoza | 28d46a5 | 2020-04-28 09:54:54 -0700 | [diff] [blame] | 2707 | } |
| 2708 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2709 | CompositeResultsPerDisplay SurfaceFlinger::composite( |
| 2710 | PhysicalDisplayId pacesetterId, const scheduler::FrameTargeters& frameTargeters) { |
| 2711 | const scheduler::FrameTarget& pacesetterTarget = |
| 2712 | frameTargeters.get(pacesetterId)->get()->target(); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2713 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2714 | const VsyncId vsyncId = pacesetterTarget.vsyncId(); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2715 | SFTRACE_NAME(ftl::Concat(__func__, ' ', ftl::to_underlying(vsyncId)).c_str()); |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 2716 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 2717 | compositionengine::CompositionRefreshArgs refreshArgs; |
jimmyshiu | 4e21177 | 2023-06-15 15:18:38 +0000 | [diff] [blame] | 2718 | refreshArgs.powerCallback = this; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 2719 | const auto& displays = FTL_FAKE_GUARD(mStateLock, mDisplays); |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 2720 | refreshArgs.outputs.reserve(displays.size()); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2721 | |
| 2722 | // Add outputs for physical displays. |
| 2723 | for (const auto& [id, targeter] : frameTargeters) { |
| 2724 | ftl::FakeGuard guard(mStateLock); |
| 2725 | |
| 2726 | if (const auto display = getCompositionDisplayLocked(id)) { |
| 2727 | refreshArgs.outputs.push_back(display); |
| 2728 | } |
Leon Scroggins III | 370b8b5 | 2022-12-08 13:20:45 -0500 | [diff] [blame] | 2729 | |
| 2730 | refreshArgs.frameTargets.try_emplace(id, &targeter->target()); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2731 | } |
| 2732 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2733 | std::vector<DisplayId> displayIds; |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 2734 | for (const auto& [_, display] : displays) { |
Dominik Laskowski | e85d7fa | 2023-05-15 10:50:22 -0400 | [diff] [blame] | 2735 | displayIds.push_back(display->getId()); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2736 | display->tracePowerMode(); |
| 2737 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2738 | // Add outputs for virtual displays. |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2739 | if (display->isVirtual()) { |
| 2740 | const Fps refreshRate = display->getAdjustedRefreshRate(); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2741 | |
| 2742 | if (!refreshRate.isValid() || |
| 2743 | mScheduler->isVsyncInPhase(pacesetterTarget.frameBeginTime(), refreshRate)) { |
| 2744 | refreshArgs.outputs.push_back(display->getCompositionDisplay()); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2745 | } |
| 2746 | } |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 2747 | } |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2748 | mPowerAdvisor->setDisplays(displayIds); |
Tianhao Yao | 67dd712 | 2022-02-22 17:48:33 +0000 | [diff] [blame] | 2749 | |
Vishnu Nair | 0a4fb00 | 2022-08-08 02:40:42 +0000 | [diff] [blame] | 2750 | const bool updateTaskMetadata = mCompositionEngine->getFeatureFlags().test( |
| 2751 | compositionengine::Feature::kSnapshotLayerMetadata); |
Vishnu Nair | 0a4fb00 | 2022-08-08 02:40:42 +0000 | [diff] [blame] | 2752 | |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 2753 | refreshArgs.bufferIdsToUncache = std::move(mBufferIdsToUncache); |
| 2754 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2755 | if (!FlagManager::getInstance().ce_fence_promise()) { |
| 2756 | refreshArgs.layersWithQueuedFrames.reserve(mLayersWithQueuedFrames.size()); |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 2757 | for (auto& [layer, _] : mLayersWithQueuedFrames) { |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 2758 | if (const auto& layerFE = layer->getCompositionEngineLayerFE( |
| 2759 | {static_cast<uint32_t>(layer->sequence)})) |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2760 | refreshArgs.layersWithQueuedFrames.push_back(layerFE); |
| 2761 | } |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 2762 | } |
| 2763 | |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 2764 | refreshArgs.outputColorSetting = mDisplayColorSetting; |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 2765 | refreshArgs.forceOutputColorMode = mForceColorMode; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 2766 | |
| 2767 | refreshArgs.updatingOutputGeometryThisFrame = mVisibleRegionsDirty; |
Vishnu Nair | 995f4cf | 2024-03-07 19:40:03 -0800 | [diff] [blame] | 2768 | refreshArgs.updatingGeometryThisFrame = mGeometryDirty.exchange(false) || |
| 2769 | mVisibleRegionsDirty || mDrawingState.colorMatrixChanged; |
Leon Scroggins III | 85d4b22 | 2023-05-09 13:58:18 -0400 | [diff] [blame] | 2770 | refreshArgs.internalDisplayRotationFlags = getActiveDisplayRotationFlags(); |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 2771 | |
| 2772 | if (CC_UNLIKELY(mDrawingState.colorMatrixChanged)) { |
| 2773 | refreshArgs.colorTransformMatrix = mDrawingState.colorMatrix; |
| 2774 | mDrawingState.colorMatrixChanged = false; |
| 2775 | } |
| 2776 | |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2777 | refreshArgs.devOptForceClientComposition = mDebugDisableHWC; |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 2778 | |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 2779 | if (mDebugFlashDelay != 0) { |
| 2780 | refreshArgs.devOptForceClientComposition = true; |
| 2781 | refreshArgs.devOptFlashDirtyRegionsDelay = std::chrono::milliseconds(mDebugFlashDelay); |
Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 2782 | } |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 2783 | |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 2784 | // TODO(b/255601557) Update frameInterval per display |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 2785 | refreshArgs.frameInterval = |
| 2786 | mScheduler->getNextFrameInterval(pacesetterId, pacesetterTarget.expectedPresentTime()); |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 2787 | const auto scheduledFrameResultOpt = mScheduler->getScheduledFrameResult(); |
| 2788 | const auto scheduledFrameTimeOpt = scheduledFrameResultOpt |
| 2789 | ? std::optional{scheduledFrameResultOpt->callbackTime} |
| 2790 | : std::nullopt; |
| 2791 | refreshArgs.scheduledFrameTime = scheduledFrameTimeOpt; |
Chavi Weingarten | 545da0e | 2023-02-09 14:55:57 +0000 | [diff] [blame] | 2792 | refreshArgs.hasTrustedPresentationListener = mNumTrustedPresentationListeners > 0; |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 2793 | // Store the present time just before calling to the composition engine so we could notify |
| 2794 | // the scheduler. |
| 2795 | const auto presentTime = systemTime(); |
| 2796 | |
Dominik Laskowski | 16a345b | 2023-04-27 17:07:33 -0400 | [diff] [blame] | 2797 | constexpr bool kCursorOnly = false; |
| 2798 | const auto layers = moveSnapshotsToCompositionArgs(refreshArgs, kCursorOnly); |
| 2799 | |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 2800 | if (!mVisibleRegionsDirty) { |
Vishnu Nair | 854ce1c | 2023-08-19 15:00:13 -0700 | [diff] [blame] | 2801 | for (const auto& [token, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
| 2802 | auto compositionDisplay = display->getCompositionDisplay(); |
| 2803 | if (!compositionDisplay->getState().isEnabled) continue; |
| 2804 | for (auto outputLayer : compositionDisplay->getOutputLayersOrderedByZ()) { |
Vishnu Nair | 0109d3d | 2023-08-22 20:23:32 -0700 | [diff] [blame] | 2805 | if (outputLayer->getLayerFE().getCompositionState() == nullptr) { |
| 2806 | // This is unexpected but instead of crashing, capture traces to disk |
| 2807 | // and recover gracefully by forcing CE to rebuild layer stack. |
| 2808 | ALOGE("Output layer %s for display %s %" PRIu64 " has a null " |
| 2809 | "snapshot. Forcing mVisibleRegionsDirty", |
| 2810 | outputLayer->getLayerFE().getDebugName(), |
| 2811 | compositionDisplay->getName().c_str(), compositionDisplay->getId().value); |
| 2812 | |
| 2813 | TransactionTraceWriter::getInstance().invoke(__func__, /* overwrite= */ false); |
| 2814 | mVisibleRegionsDirty = true; |
| 2815 | refreshArgs.updatingOutputGeometryThisFrame = mVisibleRegionsDirty; |
| 2816 | refreshArgs.updatingGeometryThisFrame = mVisibleRegionsDirty; |
| 2817 | } |
Vishnu Nair | 854ce1c | 2023-08-19 15:00:13 -0700 | [diff] [blame] | 2818 | } |
| 2819 | } |
| 2820 | } |
| 2821 | |
Melody Hsu | c949cde | 2024-03-12 01:43:34 +0000 | [diff] [blame] | 2822 | refreshArgs.refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2823 | for (auto& [layer, layerFE] : layers) { |
Melody Hsu | c949cde | 2024-03-12 01:43:34 +0000 | [diff] [blame] | 2824 | layer->onPreComposition(refreshArgs.refreshStartTime); |
| 2825 | } |
| 2826 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2827 | if (FlagManager::getInstance().ce_fence_promise()) { |
| 2828 | for (auto& [layer, layerFE] : layers) { |
| 2829 | attachReleaseFenceFutureToLayer(layer, layerFE, |
| 2830 | layerFE->mSnapshot->outputFilter.layerStack); |
Vishnu Nair | e14c6b3 | 2022-08-06 04:20:15 +0000 | [diff] [blame] | 2831 | } |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2832 | |
| 2833 | refreshArgs.layersWithQueuedFrames.reserve(mLayersWithQueuedFrames.size()); |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 2834 | for (auto& [layer, _] : mLayersWithQueuedFrames) { |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 2835 | if (const auto& layerFE = layer->getCompositionEngineLayerFE( |
| 2836 | {static_cast<uint32_t>(layer->sequence)})) { |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2837 | refreshArgs.layersWithQueuedFrames.push_back(layerFE); |
| 2838 | // Some layers are not displayed and do not yet have a future release fence |
| 2839 | if (layerFE->getReleaseFencePromiseStatus() == |
| 2840 | LayerFE::ReleaseFencePromiseStatus::UNINITIALIZED || |
| 2841 | layerFE->getReleaseFencePromiseStatus() == |
| 2842 | LayerFE::ReleaseFencePromiseStatus::FULFILLED) { |
| 2843 | // layerStack is invalid because layer is not on a display |
| 2844 | attachReleaseFenceFutureToLayer(layer.get(), layerFE.get(), |
| 2845 | ui::INVALID_LAYER_STACK); |
| 2846 | } |
| 2847 | } |
| 2848 | } |
| 2849 | |
| 2850 | mCompositionEngine->present(refreshArgs); |
| 2851 | moveSnapshotsFromCompositionArgs(refreshArgs, layers); |
| 2852 | |
| 2853 | for (auto& [layer, layerFE] : layers) { |
| 2854 | CompositionResult compositionResult{layerFE->stealCompositionResult()}; |
| 2855 | if (compositionResult.lastClientCompositionFence) { |
| 2856 | layer->setWasClientComposed(compositionResult.lastClientCompositionFence); |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | } else { |
| 2861 | mCompositionEngine->present(refreshArgs); |
| 2862 | moveSnapshotsFromCompositionArgs(refreshArgs, layers); |
| 2863 | |
| 2864 | for (auto [layer, layerFE] : layers) { |
| 2865 | CompositionResult compositionResult{layerFE->stealCompositionResult()}; |
| 2866 | for (auto& [releaseFence, layerStack] : compositionResult.releaseFences) { |
Vishnu Nair | 620fefd | 2024-08-15 12:28:18 +0000 | [diff] [blame] | 2867 | layer->onLayerDisplayed(std::move(releaseFence), layerStack); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 2868 | } |
| 2869 | if (compositionResult.lastClientCompositionFence) { |
| 2870 | layer->setWasClientComposed(compositionResult.lastClientCompositionFence); |
| 2871 | } |
Vishnu Nair | e14c6b3 | 2022-08-06 04:20:15 +0000 | [diff] [blame] | 2872 | } |
| 2873 | } |
| 2874 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 2875 | SFTRACE_NAME("postComposition"); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2876 | mTimeStats->recordFrameDuration(pacesetterTarget.frameBeginTime().ns(), systemTime()); |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 2877 | |
Dominik Laskowski | 61dfca4 | 2023-05-19 14:42:24 -0400 | [diff] [blame] | 2878 | // Send a power hint after presentation is finished. |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2879 | if (mPowerHintSessionEnabled) { |
Dominik Laskowski | 61dfca4 | 2023-05-19 14:42:24 -0400 | [diff] [blame] | 2880 | // Now that the current frame has been presented above, PowerAdvisor needs the present time |
| 2881 | // of the previous frame (whose fence is signaled by now) to determine how long the HWC had |
| 2882 | // waited on that fence to retire before presenting. |
Ady Abraham | ed1283a | 2024-07-24 15:49:16 -0700 | [diff] [blame] | 2883 | // TODO(b/355238809) `presentFenceForPreviousFrame` might not always be signaled (e.g. on |
| 2884 | // devices |
| 2885 | // where HWC does not block on the previous present fence). Revise this assumtion. |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2886 | const auto& previousPresentFence = pacesetterTarget.presentFenceForPreviousFrame(); |
Dominik Laskowski | e85d7fa | 2023-05-15 10:50:22 -0400 | [diff] [blame] | 2887 | |
Dominik Laskowski | 61dfca4 | 2023-05-19 14:42:24 -0400 | [diff] [blame] | 2888 | mPowerAdvisor->setSfPresentTiming(TimePoint::fromNs(previousPresentFence->getSignalTime()), |
| 2889 | TimePoint::now()); |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 2890 | mPowerAdvisor->reportActualWorkDuration(); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Leon Scroggins III | 5b58149 | 2023-10-31 14:29:41 -0400 | [diff] [blame] | 2893 | if (mScheduler->onCompositionPresented(presentTime)) { |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame] | 2894 | scheduleComposite(FrameHint::kNone); |
| 2895 | } |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 2896 | |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 2897 | mNotifyExpectedPresentMap[pacesetterId].hintStatus = NotifyExpectedPresentHintStatus::Start; |
Leon Scroggins III | 5b58149 | 2023-10-31 14:29:41 -0400 | [diff] [blame] | 2898 | onCompositionPresented(pacesetterId, frameTargeters, presentTime); |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 2899 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2900 | const bool hadGpuComposited = |
| 2901 | multiDisplayUnion(mCompositionCoverage).test(CompositionCoverage::Gpu); |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2902 | mCompositionCoverage.clear(); |
Alec Mouri | 8f7a010 | 2020-04-15 12:11:10 -0700 | [diff] [blame] | 2903 | |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 2904 | TimeStats::ClientCompositionRecord clientCompositionRecord; |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2905 | |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 2906 | for (const auto& [_, display] : displays) { |
| 2907 | const auto& state = display->getCompositionDisplay()->getState(); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2908 | CompositionCoverageFlags& flags = |
| 2909 | mCompositionCoverage.try_emplace(display->getId()).first->second; |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2910 | |
| 2911 | if (state.usesDeviceComposition) { |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2912 | flags |= CompositionCoverage::Hwc; |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | if (state.reusedClientComposition) { |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2916 | flags |= CompositionCoverage::GpuReuse; |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2917 | } else if (state.usesClientComposition) { |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2918 | flags |= CompositionCoverage::Gpu; |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2919 | } |
| 2920 | |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 2921 | clientCompositionRecord.predicted |= |
| 2922 | (state.strategyPrediction != CompositionStrategyPredictionState::DISABLED); |
| 2923 | clientCompositionRecord.predictionSucceeded |= |
| 2924 | (state.strategyPrediction == CompositionStrategyPredictionState::SUCCESS); |
Alec Mouri | 8f7a010 | 2020-04-15 12:11:10 -0700 | [diff] [blame] | 2925 | } |
| 2926 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2927 | const auto coverage = multiDisplayUnion(mCompositionCoverage); |
| 2928 | const bool hasGpuComposited = coverage.test(CompositionCoverage::Gpu); |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2929 | |
| 2930 | clientCompositionRecord.hadClientComposition = hasGpuComposited; |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2931 | clientCompositionRecord.reused = coverage.test(CompositionCoverage::GpuReuse); |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2932 | clientCompositionRecord.changed = hadGpuComposited != hasGpuComposited; |
| 2933 | |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 2934 | mTimeStats->pushCompositionStrategyState(clientCompositionRecord); |
| 2935 | |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 2936 | using namespace ftl::flag_operators; |
| 2937 | |
| 2938 | // TODO(b/160583065): Enable skip validation when SF caches all client composition layers. |
| 2939 | const bool hasGpuUseOrReuse = |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2940 | coverage.any(CompositionCoverage::Gpu | CompositionCoverage::GpuReuse); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 2941 | mScheduler->modulateVsync({}, &VsyncModulator::onDisplayRefresh, hasGpuUseOrReuse); |
Dan Stoza | 14cd37c | 2015-07-09 12:43:33 -0700 | [diff] [blame] | 2942 | |
David Sodman | 7e4ae11 | 2018-02-09 15:02:28 -0800 | [diff] [blame] | 2943 | mLayersWithQueuedFrames.clear(); |
Vishnu Nair | 6f209e2 | 2023-08-04 16:33:23 +0000 | [diff] [blame] | 2944 | mLayersIdsWithQueuedFrames.clear(); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 2945 | doActiveLayersTracingIfNeeded(true, mVisibleRegionsDirty, pacesetterTarget.frameBeginTime(), |
| 2946 | vsyncId); |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 2947 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2948 | updateInputFlinger(vsyncId, pacesetterTarget.frameBeginTime()); |
Vishnu Nair | d2bfbb3 | 2023-07-18 00:45:45 +0000 | [diff] [blame] | 2949 | |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 2950 | if (mVisibleRegionsDirty) mHdrLayerInfoChanged = true; |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 2951 | mVisibleRegionsDirty = false; |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 2952 | |
| 2953 | if (mCompositionEngine->needsAnotherUpdate()) { |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 2954 | scheduleCommit(FrameHint::kNone); |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 2955 | } |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 2956 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 2957 | if (mPowerHintSessionEnabled) { |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 2958 | mPowerAdvisor->setCompositeEnd(TimePoint::now()); |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 2959 | } |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 2960 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 2961 | CompositeResultsPerDisplay resultsPerDisplay; |
| 2962 | |
| 2963 | // Filter out virtual displays. |
| 2964 | for (const auto& [id, coverage] : mCompositionCoverage) { |
| 2965 | if (const auto idOpt = PhysicalDisplayId::tryCast(id)) { |
| 2966 | resultsPerDisplay.try_emplace(*idOpt, CompositeResult{coverage}); |
| 2967 | } |
| 2968 | } |
| 2969 | |
| 2970 | return resultsPerDisplay; |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 2971 | } |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2972 | |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 2973 | bool SurfaceFlinger::isHdrLayer(const frontend::LayerSnapshot& snapshot) const { |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 2974 | // Even though the camera layer may be using an HDR transfer function or otherwise be "HDR" |
| 2975 | // the device may need to avoid boosting the brightness as a result of these layers to |
| 2976 | // reduce power consumption during camera recording |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 2977 | if (mIgnoreHdrCameraLayers) { |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 2978 | if (snapshot.externalTexture && |
| 2979 | (snapshot.externalTexture->getUsage() & GRALLOC_USAGE_HW_CAMERA_WRITE) != 0) { |
John Reck | 49d9ad3 | 2022-02-23 19:03:31 -0500 | [diff] [blame] | 2980 | return false; |
| 2981 | } |
| 2982 | } |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 2983 | // RANGE_EXTENDED layer may identify themselves as being "HDR" |
| 2984 | // via a desired hdr/sdr ratio |
| 2985 | auto pixelFormat = snapshot.buffer |
| 2986 | ? std::make_optional(static_cast<ui::PixelFormat>(snapshot.buffer->getPixelFormat())) |
| 2987 | : std::nullopt; |
| 2988 | |
| 2989 | if (getHdrRenderType(snapshot.dataspace, pixelFormat, snapshot.desiredHdrSdrRatio) != |
| 2990 | HdrRenderType::SDR) { |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 2991 | return true; |
| 2992 | } |
| 2993 | // If the layer is not allowed to be dimmed, treat it as HDR. WindowManager may disable |
| 2994 | // dimming in order to keep animations invoking SDR screenshots of HDR layers seamless. |
| 2995 | // Treat such tagged layers as HDR so that DisplayManagerService does not try to change |
| 2996 | // the screen brightness |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 2997 | if (!snapshot.dimmingEnabled) { |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 2998 | return true; |
| 2999 | } |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 3000 | return false; |
John Reck | 49d9ad3 | 2022-02-23 19:03:31 -0500 | [diff] [blame] | 3001 | } |
| 3002 | |
ramindani | 06e518e | 2022-03-14 18:47:53 +0000 | [diff] [blame] | 3003 | ui::Rotation SurfaceFlinger::getPhysicalDisplayOrientation(DisplayId displayId, |
| 3004 | bool isPrimary) const { |
| 3005 | const auto id = PhysicalDisplayId::tryCast(displayId); |
| 3006 | if (!id) { |
| 3007 | return ui::ROTATION_0; |
| 3008 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3009 | if (!mIgnoreHwcPhysicalDisplayOrientation && |
Neha Jain | 7b31cca | 2023-01-09 23:10:42 +0000 | [diff] [blame] | 3010 | getHwComposer().getComposer()->isSupported( |
ramindani | 06e518e | 2022-03-14 18:47:53 +0000 | [diff] [blame] | 3011 | Hwc2::Composer::OptionalFeature::PhysicalDisplayOrientation)) { |
| 3012 | switch (getHwComposer().getPhysicalDisplayOrientation(*id)) { |
| 3013 | case Hwc2::AidlTransform::ROT_90: |
| 3014 | return ui::ROTATION_90; |
| 3015 | case Hwc2::AidlTransform::ROT_180: |
| 3016 | return ui::ROTATION_180; |
| 3017 | case Hwc2::AidlTransform::ROT_270: |
| 3018 | return ui::ROTATION_270; |
| 3019 | default: |
| 3020 | return ui::ROTATION_0; |
| 3021 | } |
| 3022 | } |
| 3023 | |
| 3024 | if (isPrimary) { |
| 3025 | using Values = SurfaceFlingerProperties::primary_display_orientation_values; |
| 3026 | switch (primary_display_orientation(Values::ORIENTATION_0)) { |
| 3027 | case Values::ORIENTATION_90: |
| 3028 | return ui::ROTATION_90; |
| 3029 | case Values::ORIENTATION_180: |
| 3030 | return ui::ROTATION_180; |
| 3031 | case Values::ORIENTATION_270: |
| 3032 | return ui::ROTATION_270; |
| 3033 | default: |
| 3034 | break; |
| 3035 | } |
| 3036 | } |
| 3037 | return ui::ROTATION_0; |
| 3038 | } |
| 3039 | |
Leon Scroggins III | 5b58149 | 2023-10-31 14:29:41 -0400 | [diff] [blame] | 3040 | void SurfaceFlinger::onCompositionPresented(PhysicalDisplayId pacesetterId, |
| 3041 | const scheduler::FrameTargeters& frameTargeters, |
| 3042 | nsecs_t presentStartTime) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 3043 | SFTRACE_CALL(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3044 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3045 | ui::PhysicalDisplayMap<PhysicalDisplayId, std::shared_ptr<FenceTime>> presentFences; |
| 3046 | ui::PhysicalDisplayMap<PhysicalDisplayId, const sp<Fence>> gpuCompositionDoneFences; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 3047 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3048 | for (const auto& [id, targeter] : frameTargeters) { |
| 3049 | auto presentFence = getHwComposer().getPresentFence(id); |
| 3050 | |
| 3051 | if (id == pacesetterId) { |
| 3052 | mTransactionCallbackInvoker.addPresentFence(presentFence); |
| 3053 | } |
| 3054 | |
| 3055 | if (auto fenceTime = targeter->setPresentFence(std::move(presentFence)); |
| 3056 | fenceTime->isValid()) { |
| 3057 | presentFences.try_emplace(id, std::move(fenceTime)); |
| 3058 | } |
| 3059 | |
| 3060 | ftl::FakeGuard guard(mStateLock); |
| 3061 | if (const auto display = getCompositionDisplayLocked(id); |
| 3062 | display && display->getState().usesClientComposition) { |
| 3063 | gpuCompositionDoneFences |
| 3064 | .try_emplace(id, display->getRenderSurface()->getClientTargetAcquireFence()); |
| 3065 | } |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 3066 | } |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 3067 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3068 | const auto pacesetterDisplay = FTL_FAKE_GUARD(mStateLock, getDisplayDeviceLocked(pacesetterId)); |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 3069 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3070 | std::shared_ptr<FenceTime> pacesetterPresentFenceTime = |
| 3071 | presentFences.get(pacesetterId) |
| 3072 | .transform([](const FenceTimePtr& ptr) { return ptr; }) |
| 3073 | .value_or(FenceTime::NO_FENCE); |
| 3074 | |
| 3075 | std::shared_ptr<FenceTime> pacesetterGpuCompositionDoneFenceTime = |
| 3076 | gpuCompositionDoneFences.get(pacesetterId) |
| 3077 | .transform([](sp<Fence> fence) { |
| 3078 | return std::make_shared<FenceTime>(std::move(fence)); |
| 3079 | }) |
| 3080 | .value_or(FenceTime::NO_FENCE); |
| 3081 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 3082 | const TimePoint presentTime = TimePoint::now(); |
Vishnu Nair | 9a69a04 | 2021-06-18 13:19:49 -0700 | [diff] [blame] | 3083 | |
Jorim Jaggi | 8d34c90 | 2020-12-16 21:43:07 +0100 | [diff] [blame] | 3084 | // Set presentation information before calling Layer::releasePendingBuffer, such that jank |
| 3085 | // information from previous' frame classification is already available when sending jank info |
| 3086 | // to clients, so they get jank classification as early as possible. |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3087 | mFrameTimeline->setSfPresent(presentTime.ns(), pacesetterPresentFenceTime, |
| 3088 | pacesetterGpuCompositionDoneFenceTime); |
Jorim Jaggi | 8d34c90 | 2020-12-16 21:43:07 +0100 | [diff] [blame] | 3089 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 3090 | // We use the CompositionEngine::getLastFrameRefreshTimestamp() which might |
| 3091 | // 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] | 3092 | // but that should be okay since CompositorTiming has snapping logic. |
Dominik Laskowski | 80c7727 | 2022-07-08 12:47:29 -0700 | [diff] [blame] | 3093 | const TimePoint compositeTime = |
| 3094 | TimePoint::fromNs(mCompositionEngine->getLastFrameRefreshTimestamp()); |
Dominik Laskowski | e62606d | 2024-02-22 11:42:13 -0500 | [diff] [blame] | 3095 | const Duration presentLatency = mHasReliablePresentFences |
| 3096 | ? mPresentLatencyTracker.trackPendingFrame(compositeTime, pacesetterPresentFenceTime) |
| 3097 | : Duration::zero(); |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 3098 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 3099 | const auto schedule = mScheduler->getVsyncSchedule(); |
| 3100 | const TimePoint vsyncDeadline = schedule->vsyncDeadlineAfter(presentTime); |
| 3101 | const Period vsyncPeriod = schedule->period(); |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 3102 | const nsecs_t vsyncPhase = |
| 3103 | mScheduler->getVsyncConfiguration().getCurrentConfigs().late.sfOffset; |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 3104 | |
Dominik Laskowski | 5a5e01e | 2022-07-08 07:52:44 -0700 | [diff] [blame] | 3105 | const CompositorTiming compositorTiming(vsyncDeadline.ns(), vsyncPeriod.ns(), vsyncPhase, |
Dominik Laskowski | 80c7727 | 2022-07-08 12:47:29 -0700 | [diff] [blame] | 3106 | presentLatency.ns()); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 3107 | |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 3108 | ui::DisplayMap<ui::LayerStack, const DisplayDevice*> layerStackToDisplay; |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 3109 | { |
| 3110 | if (!mLayersWithBuffersRemoved.empty() || mNumTrustedPresentationListeners > 0) { |
| 3111 | Mutex::Autolock lock(mStateLock); |
| 3112 | for (const auto& [token, display] : mDisplays) { |
| 3113 | layerStackToDisplay.emplace_or_replace(display->getLayerStack(), display.get()); |
| 3114 | } |
| 3115 | } |
| 3116 | } |
| 3117 | |
| 3118 | for (auto layer : mLayersWithBuffersRemoved) { |
Vishnu Nair | f10a87b | 2023-05-01 14:02:51 -0700 | [diff] [blame] | 3119 | std::vector<ui::LayerStack> previouslyPresentedLayerStacks = |
| 3120 | std::move(layer->mPreviouslyPresentedLayerStacks); |
| 3121 | layer->mPreviouslyPresentedLayerStacks.clear(); |
| 3122 | for (auto layerStack : previouslyPresentedLayerStacks) { |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 3123 | auto optDisplay = layerStackToDisplay.get(layerStack); |
| 3124 | if (optDisplay && !optDisplay->get()->isVirtual()) { |
| 3125 | auto fence = getHwComposer().getPresentFence(optDisplay->get()->getPhysicalId()); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 3126 | if (FlagManager::getInstance().ce_fence_promise()) { |
| 3127 | layer->prepareReleaseCallbacks(ftl::yield<FenceResult>(fence), |
| 3128 | ui::INVALID_LAYER_STACK); |
| 3129 | } else { |
| 3130 | layer->onLayerDisplayed(ftl::yield<FenceResult>(fence).share(), |
| 3131 | ui::INVALID_LAYER_STACK); |
| 3132 | } |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 3133 | } |
| 3134 | } |
| 3135 | layer->releasePendingBuffer(presentTime.ns()); |
| 3136 | } |
| 3137 | mLayersWithBuffersRemoved.clear(); |
| 3138 | |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 3139 | for (const auto& [layer, gameMode] : mLayersWithQueuedFrames) { |
Leon Scroggins III | 5b58149 | 2023-10-31 14:29:41 -0400 | [diff] [blame] | 3140 | layer->onCompositionPresented(pacesetterDisplay.get(), |
| 3141 | pacesetterGpuCompositionDoneFenceTime, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 3142 | pacesetterPresentFenceTime, compositorTiming, gameMode); |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 3143 | layer->releasePendingBuffer(presentTime.ns()); |
Robert Carr | c747ad0 | 2021-06-11 14:01:24 -0700 | [diff] [blame] | 3144 | } |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 3145 | |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 3146 | std::vector<std::pair<std::shared_ptr<compositionengine::Display>, sp<HdrLayerInfoReporter>>> |
| 3147 | hdrInfoListeners; |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3148 | bool haveNewListeners = false; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 3149 | { |
| 3150 | Mutex::Autolock lock(mStateLock); |
| 3151 | if (mFpsReporter) { |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 3152 | mFpsReporter->dispatchLayerFps(mLayerHierarchyBuilder.getHierarchy()); |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 3153 | } |
Galia Peycheva | 8f04b30 | 2021-04-27 13:25:38 +0200 | [diff] [blame] | 3154 | |
| 3155 | if (mTunnelModeEnabledReporter) { |
| 3156 | mTunnelModeEnabledReporter->updateTunnelModeStatus(); |
| 3157 | } |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 3158 | hdrInfoListeners.reserve(mHdrLayerInfoListeners.size()); |
Dominik Laskowski | 7b9bf4c | 2021-03-18 12:21:29 -0700 | [diff] [blame] | 3159 | for (const auto& [displayId, reporter] : mHdrLayerInfoListeners) { |
| 3160 | if (reporter && reporter->hasListeners()) { |
| 3161 | if (const auto display = getDisplayDeviceLocked(displayId)) { |
| 3162 | hdrInfoListeners.emplace_back(display->getCompositionDisplay(), reporter); |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | } |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3166 | haveNewListeners = mAddingHDRLayerInfoListener; // grab this with state lock |
| 3167 | mAddingHDRLayerInfoListener = false; |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 3168 | } |
| 3169 | |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 3170 | if (haveNewListeners || mHdrLayerInfoChanged) { |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3171 | for (auto& [compositionDisplay, listener] : hdrInfoListeners) { |
| 3172 | HdrLayerInfoReporter::HdrLayerInfo info; |
| 3173 | int32_t maxArea = 0; |
Alec Mouri | 1b0d4e1 | 2024-02-12 22:27:19 +0000 | [diff] [blame] | 3174 | |
Vishnu Nair | 9a5463a | 2023-06-17 12:18:36 -0700 | [diff] [blame] | 3175 | auto updateInfoFn = |
| 3176 | [&](const std::shared_ptr<compositionengine::Display>& compositionDisplay, |
| 3177 | const frontend::LayerSnapshot& snapshot, const sp<LayerFE>& layerFe) { |
| 3178 | if (snapshot.isVisible && |
| 3179 | compositionDisplay->includesLayer(snapshot.outputFilter)) { |
| 3180 | if (isHdrLayer(snapshot)) { |
| 3181 | const auto* outputLayer = |
| 3182 | compositionDisplay->getOutputLayerForLayer(layerFe); |
| 3183 | if (outputLayer) { |
| 3184 | const float desiredHdrSdrRatio = |
Alec Mouri | 1b0d4e1 | 2024-02-12 22:27:19 +0000 | [diff] [blame] | 3185 | snapshot.desiredHdrSdrRatio < 1.f |
Vishnu Nair | 9a5463a | 2023-06-17 12:18:36 -0700 | [diff] [blame] | 3186 | ? std::numeric_limits<float>::infinity() |
| 3187 | : snapshot.desiredHdrSdrRatio; |
| 3188 | info.mergeDesiredRatio(desiredHdrSdrRatio); |
| 3189 | info.numberOfHdrLayers++; |
| 3190 | const auto displayFrame = outputLayer->getState().displayFrame; |
| 3191 | const int32_t area = |
| 3192 | displayFrame.width() * displayFrame.height(); |
| 3193 | if (area > maxArea) { |
| 3194 | maxArea = area; |
| 3195 | info.maxW = displayFrame.width(); |
| 3196 | info.maxH = displayFrame.height(); |
| 3197 | } |
| 3198 | } |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3199 | } |
John Reck | db1a16b | 2021-07-09 13:05:52 -0400 | [diff] [blame] | 3200 | } |
Vishnu Nair | 9a5463a | 2023-06-17 12:18:36 -0700 | [diff] [blame] | 3201 | }; |
| 3202 | |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 3203 | mLayerSnapshotBuilder.forEachVisibleSnapshot( |
| 3204 | [&, compositionDisplay = compositionDisplay]( |
| 3205 | std::unique_ptr<frontend::LayerSnapshot>& snapshot) |
| 3206 | FTL_FAKE_GUARD(kMainThreadContext) { |
| 3207 | auto it = mLegacyLayers.find(snapshot->sequence); |
| 3208 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 3209 | "Couldnt find layer object for %s", |
| 3210 | snapshot->getDebugString().c_str()); |
| 3211 | auto& legacyLayer = it->second; |
| 3212 | sp<LayerFE> layerFe = |
| 3213 | legacyLayer->getCompositionEngineLayerFE(snapshot->path); |
Vishnu Nair | 9a5463a | 2023-06-17 12:18:36 -0700 | [diff] [blame] | 3214 | |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 3215 | updateInfoFn(compositionDisplay, *snapshot, layerFe); |
| 3216 | }); |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3217 | listener->dispatchHdrLayerInfo(info); |
| 3218 | } |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 3219 | } |
| 3220 | |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 3221 | mHdrLayerInfoChanged = false; |
Robert Carr | 167bdde | 2021-07-28 11:26:51 -0700 | [diff] [blame] | 3222 | |
Vishnu Nair | 1fe1117 | 2023-04-03 18:10:09 +0000 | [diff] [blame] | 3223 | mTransactionCallbackInvoker.sendCallbacks(false /* onCommitOnly */); |
| 3224 | mTransactionCallbackInvoker.clearCompletedTransactions(); |
| 3225 | |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 3226 | mTimeStats->incrementTotalFrames(); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3227 | mTimeStats->setPresentFenceGlobal(pacesetterPresentFenceTime); |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 3228 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3229 | for (auto&& [id, presentFence] : presentFences) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 3230 | ftl::FakeGuard guard(mStateLock); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3231 | const bool isInternalDisplay = |
| 3232 | mPhysicalDisplays.get(id).transform(&PhysicalDisplay::isInternal).value_or(false); |
| 3233 | |
| 3234 | if (isInternalDisplay) { |
| 3235 | mScheduler->addPresentFence(id, std::move(presentFence)); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 3236 | } |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 3237 | } |
| 3238 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3239 | const bool hasPacesetterDisplay = |
| 3240 | pacesetterDisplay && getHwComposer().isConnected(pacesetterId); |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 3241 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3242 | if (!hasSyncFramework) { |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3243 | if (hasPacesetterDisplay && pacesetterDisplay->isPoweredOn()) { |
| 3244 | mScheduler->enableHardwareVsync(pacesetterId); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 3245 | } |
| 3246 | } |
| 3247 | |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 3248 | if (hasPacesetterDisplay && !pacesetterDisplay->isPoweredOn()) { |
Lars Svensson | d9e5469 | 2021-12-21 07:41:57 +0100 | [diff] [blame] | 3249 | getRenderEngine().cleanupPostRender(); |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 3250 | return; |
| 3251 | } |
| 3252 | |
Lingfeng Yang | 2e4fef6 | 2020-04-24 14:48:43 +0000 | [diff] [blame] | 3253 | // Cleanup any outstanding resources due to rendering a prior frame. |
| 3254 | getRenderEngine().cleanupPostRender(); |
| 3255 | |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 3256 | if (mNumTrustedPresentationListeners > 0) { |
| 3257 | // 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] | 3258 | traverseLegacyLayers([&](Layer* layer) FTL_FAKE_GUARD(kMainThreadContext) { |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 3259 | if (!layer->hasTrustedPresentationListener()) { |
| 3260 | return; |
| 3261 | } |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 3262 | const frontend::LayerSnapshot* snapshot = |
| 3263 | mLayerSnapshotBuilder.getSnapshot(layer->sequence); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 3264 | std::optional<const DisplayDevice*> displayOpt = std::nullopt; |
| 3265 | if (snapshot) { |
| 3266 | displayOpt = layerStackToDisplay.get(snapshot->outputFilter.layerStack); |
| 3267 | } |
Vishnu Nair | fad494c | 2023-02-01 19:52:54 +0000 | [diff] [blame] | 3268 | const DisplayDevice* display = displayOpt.value_or(nullptr); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 3269 | layer->updateTrustedPresentationState(display, snapshot, |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 3270 | nanoseconds_to_milliseconds(presentStartTime), |
| 3271 | false); |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 3272 | }); |
| 3273 | } |
| 3274 | |
Vishnu Nair | 2665ca9 | 2024-07-09 22:08:15 +0000 | [diff] [blame] | 3275 | // Even though SFTRACE_INT64 already checks if tracing is enabled, it doesn't prevent the |
Dan Stoza | 45de5ba | 2019-04-25 14:12:09 -0700 | [diff] [blame] | 3276 | // side-effect of getTotalSize(), so we check that again here |
Vishnu Nair | 2665ca9 | 2024-07-09 22:08:15 +0000 | [diff] [blame] | 3277 | if (SFTRACE_ENABLED()) { |
Marissa Wall | 22b2de1 | 2019-12-02 18:11:43 -0800 | [diff] [blame] | 3278 | // getTotalSize returns the total number of buffers that were allocated by SurfaceFlinger |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 3279 | SFTRACE_INT64("Total Buffer Size", GraphicBufferAllocator::get().getTotalSize()); |
Dan Stoza | 45de5ba | 2019-04-25 14:12:09 -0700 | [diff] [blame] | 3280 | } |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 3281 | |
| 3282 | logFrameStats(presentTime); |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 3283 | } |
| 3284 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 3285 | void SurfaceFlinger::commitTransactions() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 3286 | SFTRACE_CALL(); |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 3287 | mDebugInTransaction = systemTime(); |
| 3288 | |
| 3289 | // Here we're guaranteed that some transaction flags are set |
| 3290 | // so we can call commitTransactionsLocked unconditionally. |
| 3291 | // We clear the flags with mStateLock held to guarantee that |
| 3292 | // mCurrentState won't change until the transaction is committed. |
| 3293 | mScheduler->modulateVsync({}, &VsyncModulator::onTransactionCommit); |
| 3294 | commitTransactionsLocked(clearTransactionFlags(eTransactionMask)); |
| 3295 | mDebugInTransaction = 0; |
| 3296 | } |
| 3297 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3298 | std::pair<DisplayModes, DisplayModePtr> SurfaceFlinger::loadDisplayModes( |
| 3299 | PhysicalDisplayId displayId) const { |
Marin Shalamanov | d3b5c5d | 2021-02-11 18:26:14 +0100 | [diff] [blame] | 3300 | std::vector<HWComposer::HWCDisplayMode> hwcModes; |
Dominik Laskowski | d940a01 | 2024-01-28 13:25:44 -0500 | [diff] [blame] | 3301 | std::optional<hal::HWConfigId> activeModeHwcIdOpt; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3302 | |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 3303 | const bool isExternalDisplay = FlagManager::getInstance().connected_display() && |
| 3304 | getHwComposer().getDisplayConnectionType(displayId) == |
| 3305 | ui::DisplayConnectionType::External; |
| 3306 | |
Marin Shalamanov | d3b5c5d | 2021-02-11 18:26:14 +0100 | [diff] [blame] | 3307 | int attempt = 0; |
| 3308 | constexpr int kMaxAttempts = 3; |
| 3309 | do { |
ramindani | 263a3f1 | 2023-07-18 20:44:49 -0700 | [diff] [blame] | 3310 | hwcModes = getHwComposer().getModes(displayId, |
| 3311 | scheduler::RefreshRateSelector::kMinSupportedFrameRate |
| 3312 | .getPeriodNsecs()); |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 3313 | const auto activeModeHwcIdExp = getHwComposer().getActiveMode(displayId); |
| 3314 | activeModeHwcIdOpt = activeModeHwcIdExp.value_opt(); |
| 3315 | |
| 3316 | if (isExternalDisplay && |
| 3317 | activeModeHwcIdExp.has_error([](status_t error) { return error == NO_INIT; })) { |
| 3318 | constexpr nsecs_t k59HzVsyncPeriod = 16949153; |
| 3319 | constexpr nsecs_t k60HzVsyncPeriod = 16666667; |
| 3320 | |
| 3321 | // DM sets the initial mode for an external display to 1080p@60, but |
| 3322 | // this comes after SF creates its own state (including the |
| 3323 | // DisplayDevice). For now, pick the same mode in order to avoid |
| 3324 | // inconsistent state and unnecessary mode switching. |
| 3325 | // TODO (b/318534874): Let DM decide the initial mode. |
| 3326 | // |
| 3327 | // Try to find 1920x1080 @ 60 Hz |
| 3328 | if (const auto iter = std::find_if(hwcModes.begin(), hwcModes.end(), |
| 3329 | [](const auto& mode) { |
| 3330 | return mode.width == 1920 && |
| 3331 | mode.height == 1080 && |
| 3332 | mode.vsyncPeriod == k60HzVsyncPeriod; |
| 3333 | }); |
| 3334 | iter != hwcModes.end()) { |
| 3335 | activeModeHwcIdOpt = iter->hwcId; |
| 3336 | break; |
| 3337 | } |
| 3338 | |
| 3339 | // Try to find 1920x1080 @ 59-60 Hz |
| 3340 | if (const auto iter = std::find_if(hwcModes.begin(), hwcModes.end(), |
| 3341 | [](const auto& mode) { |
| 3342 | return mode.width == 1920 && |
| 3343 | mode.height == 1080 && |
| 3344 | mode.vsyncPeriod >= k60HzVsyncPeriod && |
| 3345 | mode.vsyncPeriod <= k59HzVsyncPeriod; |
| 3346 | }); |
| 3347 | iter != hwcModes.end()) { |
| 3348 | activeModeHwcIdOpt = iter->hwcId; |
| 3349 | break; |
| 3350 | } |
| 3351 | |
| 3352 | // The display does not support 1080p@60, and this is the last attempt to pick a display |
| 3353 | // mode. Prefer 60 Hz if available, with the closest resolution to 1080p. |
| 3354 | if (attempt + 1 == kMaxAttempts) { |
| 3355 | std::vector<HWComposer::HWCDisplayMode> hwcModeOpts; |
| 3356 | |
| 3357 | for (const auto& mode : hwcModes) { |
| 3358 | if (mode.width <= 1920 && mode.height <= 1080 && |
| 3359 | mode.vsyncPeriod >= k60HzVsyncPeriod && |
| 3360 | mode.vsyncPeriod <= k59HzVsyncPeriod) { |
| 3361 | hwcModeOpts.push_back(mode); |
| 3362 | } |
| 3363 | } |
| 3364 | |
| 3365 | if (const auto iter = std::max_element(hwcModeOpts.begin(), hwcModeOpts.end(), |
| 3366 | [](const auto& a, const auto& b) { |
| 3367 | const auto aSize = a.width * a.height; |
| 3368 | const auto bSize = b.width * b.height; |
| 3369 | if (aSize < bSize) |
| 3370 | return true; |
| 3371 | else if (aSize == bSize) |
| 3372 | return a.vsyncPeriod > b.vsyncPeriod; |
| 3373 | else |
| 3374 | return false; |
| 3375 | }); |
| 3376 | iter != hwcModeOpts.end()) { |
| 3377 | activeModeHwcIdOpt = iter->hwcId; |
| 3378 | break; |
| 3379 | } |
| 3380 | |
| 3381 | // hwcModeOpts was empty, use hwcModes[0] as the last resort |
| 3382 | activeModeHwcIdOpt = hwcModes[0].hwcId; |
| 3383 | } |
| 3384 | } |
Marin Shalamanov | d3b5c5d | 2021-02-11 18:26:14 +0100 | [diff] [blame] | 3385 | |
Dominik Laskowski | d940a01 | 2024-01-28 13:25:44 -0500 | [diff] [blame] | 3386 | const auto isActiveMode = [activeModeHwcIdOpt](const HWComposer::HWCDisplayMode& mode) { |
| 3387 | return mode.hwcId == activeModeHwcIdOpt; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3388 | }; |
| 3389 | |
| 3390 | if (std::any_of(hwcModes.begin(), hwcModes.end(), isActiveMode)) { |
| 3391 | break; |
| 3392 | } |
| 3393 | } while (++attempt < kMaxAttempts); |
| 3394 | |
Dominik Laskowski | bab5128 | 2022-08-12 09:28:55 -0700 | [diff] [blame] | 3395 | if (attempt == kMaxAttempts) { |
| 3396 | const std::string activeMode = |
Dominik Laskowski | d940a01 | 2024-01-28 13:25:44 -0500 | [diff] [blame] | 3397 | activeModeHwcIdOpt ? std::to_string(*activeModeHwcIdOpt) : "unknown"s; |
Dominik Laskowski | bab5128 | 2022-08-12 09:28:55 -0700 | [diff] [blame] | 3398 | ALOGE("HWC failed to report an active mode that is supported: activeModeHwcId=%s, " |
| 3399 | "hwcModes={%s}", |
| 3400 | activeMode.c_str(), base::Join(hwcModes, ", ").c_str()); |
| 3401 | return {}; |
| 3402 | } |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3403 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3404 | const DisplayModes oldModes = mPhysicalDisplays.get(displayId) |
| 3405 | .transform([](const PhysicalDisplay& display) { |
| 3406 | return display.snapshot().displayModes(); |
| 3407 | }) |
| 3408 | .value_or(DisplayModes{}); |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3409 | |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 3410 | DisplayModeId nextModeId = std::accumulate(oldModes.begin(), oldModes.end(), DisplayModeId(-1), |
| 3411 | [](DisplayModeId max, const auto& pair) { |
| 3412 | return std::max(max, pair.first); |
| 3413 | }); |
| 3414 | ++nextModeId; |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3415 | |
| 3416 | DisplayModes newModes; |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3417 | for (const auto& hwcMode : hwcModes) { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 3418 | const auto id = nextModeId++; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3419 | newModes.try_emplace(id, |
| 3420 | DisplayMode::Builder(hwcMode.hwcId) |
| 3421 | .setId(id) |
| 3422 | .setPhysicalDisplayId(displayId) |
| 3423 | .setResolution({hwcMode.width, hwcMode.height}) |
| 3424 | .setVsyncPeriod(hwcMode.vsyncPeriod) |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 3425 | .setVrrConfig(hwcMode.vrrConfig) |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3426 | .setDpiX(hwcMode.dpiX) |
| 3427 | .setDpiY(hwcMode.dpiY) |
| 3428 | .setGroup(hwcMode.configGroup) |
| 3429 | .build()); |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3430 | } |
| 3431 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3432 | const bool sameModes = |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3433 | std::equal(newModes.begin(), newModes.end(), oldModes.begin(), oldModes.end(), |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3434 | [](const auto& lhs, const auto& rhs) { |
| 3435 | return equalsExceptDisplayModeId(*lhs.second, *rhs.second); |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3436 | }); |
| 3437 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3438 | // Keep IDs if modes have not changed. |
| 3439 | const auto& modes = sameModes ? oldModes : newModes; |
| 3440 | const DisplayModePtr activeMode = |
Dominik Laskowski | d940a01 | 2024-01-28 13:25:44 -0500 | [diff] [blame] | 3441 | std::find_if(modes.begin(), modes.end(), [activeModeHwcIdOpt](const auto& pair) { |
| 3442 | return pair.second->getHwcId() == activeModeHwcIdOpt; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3443 | })->second; |
Marin Shalamanov | f22e6ac | 2021-02-10 20:45:15 +0100 | [diff] [blame] | 3444 | |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 3445 | if (isExternalDisplay) { |
| 3446 | ALOGI("External display %s initial mode: {%s}", to_string(displayId).c_str(), |
| 3447 | to_string(*activeMode).c_str()); |
| 3448 | } |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3449 | return {modes, activeMode}; |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 3450 | } |
| 3451 | |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 3452 | bool SurfaceFlinger::configureLocked() { |
| 3453 | std::vector<HotplugEvent> events; |
| 3454 | { |
| 3455 | std::lock_guard<std::mutex> lock(mHotplugMutex); |
| 3456 | events = std::move(mPendingHotplugEvents); |
| 3457 | } |
| 3458 | |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3459 | for (const auto [hwcDisplayId, connection] : events) { |
| 3460 | if (auto info = getHwComposer().onHotplug(hwcDisplayId, connection)) { |
| 3461 | const auto displayId = info->id; |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3462 | const ftl::Concat displayString("display ", displayId.value, "(HAL ID ", hwcDisplayId, |
| 3463 | ')'); |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 3464 | |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3465 | if (connection == hal::Connection::CONNECTED) { |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3466 | const auto activeModeIdOpt = |
| 3467 | processHotplugConnect(displayId, hwcDisplayId, std::move(*info), |
| 3468 | displayString.c_str()); |
| 3469 | if (!activeModeIdOpt) { |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3470 | if (FlagManager::getInstance().hotplug2()) { |
| 3471 | mScheduler->dispatchHotplugError( |
| 3472 | static_cast<int32_t>(DisplayHotplugEvent::ERROR_UNKNOWN)); |
| 3473 | } |
| 3474 | getHwComposer().disconnectDisplay(displayId); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3475 | continue; |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3476 | } |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3477 | |
| 3478 | const auto [kernelIdleTimerController, idleTimerTimeoutMs] = |
| 3479 | getKernelIdleTimerProperties(displayId); |
| 3480 | |
| 3481 | using Config = scheduler::RefreshRateSelector::Config; |
| 3482 | const Config config = |
| 3483 | {.enableFrameRateOverride = sysprop::enable_frame_rate_override(true) |
| 3484 | ? Config::FrameRateOverride::Enabled |
| 3485 | : Config::FrameRateOverride::Disabled, |
| 3486 | .frameRateMultipleThreshold = |
| 3487 | base::GetIntProperty("debug.sf.frame_rate_multiple_threshold"s, 0), |
| 3488 | .legacyIdleTimerTimeout = idleTimerTimeoutMs, |
| 3489 | .kernelIdleTimerController = kernelIdleTimerController}; |
| 3490 | |
| 3491 | const auto snapshotOpt = |
| 3492 | mPhysicalDisplays.get(displayId).transform(&PhysicalDisplay::snapshotRef); |
| 3493 | LOG_ALWAYS_FATAL_IF(!snapshotOpt); |
| 3494 | |
| 3495 | mDisplayModeController.registerDisplay(*snapshotOpt, *activeModeIdOpt, config); |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3496 | } else { |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3497 | // Unregister before destroying the DisplaySnapshot below. |
| 3498 | mDisplayModeController.unregisterDisplay(displayId); |
| 3499 | |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3500 | processHotplugDisconnect(displayId, displayString.c_str()); |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3501 | } |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 3502 | } |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 3503 | } |
| 3504 | |
Dominik Laskowski | f11728a | 2022-07-28 13:07:42 -0700 | [diff] [blame] | 3505 | return !events.empty(); |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 3506 | } |
| 3507 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3508 | std::optional<DisplayModeId> SurfaceFlinger::processHotplugConnect(PhysicalDisplayId displayId, |
| 3509 | hal::HWDisplayId hwcDisplayId, |
| 3510 | DisplayIdentificationInfo&& info, |
| 3511 | const char* displayString) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3512 | auto [displayModes, activeMode] = loadDisplayModes(displayId); |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3513 | if (!activeMode) { |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3514 | ALOGE("Failed to hotplug %s", displayString); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3515 | return std::nullopt; |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3516 | } |
| 3517 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3518 | const DisplayModeId activeModeId = activeMode->getId(); |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 3519 | ui::ColorModes colorModes = getHwComposer().getColorModes(displayId); |
| 3520 | |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3521 | if (const auto displayOpt = mPhysicalDisplays.get(displayId)) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3522 | const auto& display = displayOpt->get(); |
| 3523 | const auto& snapshot = display.snapshot(); |
| 3524 | |
| 3525 | std::optional<DeviceProductInfo> deviceProductInfo; |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3526 | if (getHwComposer().updatesDeviceProductInfoOnHotplugReconnect()) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3527 | deviceProductInfo = std::move(info.deviceProductInfo); |
| 3528 | } else { |
| 3529 | deviceProductInfo = snapshot.deviceProductInfo(); |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3530 | } |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3531 | |
| 3532 | const auto it = |
| 3533 | mPhysicalDisplays.try_replace(displayId, display.token(), displayId, |
| 3534 | snapshot.connectionType(), std::move(displayModes), |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 3535 | std::move(colorModes), std::move(deviceProductInfo)); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3536 | |
| 3537 | auto& state = mCurrentState.displays.editValueFor(it->second.token()); |
| 3538 | state.sequenceId = DisplayDeviceState{}.sequenceId; // Generate new sequenceId. |
| 3539 | state.physical->activeMode = std::move(activeMode); |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3540 | ALOGI("Reconnecting %s", displayString); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3541 | return activeModeId; |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3542 | } |
| 3543 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3544 | const sp<IBinder> token = sp<BBinder>::make(); |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 3545 | const ui::DisplayConnectionType connectionType = |
| 3546 | getHwComposer().getDisplayConnectionType(displayId); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3547 | |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 3548 | mPhysicalDisplays.try_emplace(displayId, token, displayId, connectionType, |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 3549 | std::move(displayModes), std::move(colorModes), |
| 3550 | std::move(info.deviceProductInfo)); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3551 | |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3552 | DisplayDeviceState state; |
| 3553 | state.physical = {.id = displayId, |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3554 | .hwcDisplayId = hwcDisplayId, |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3555 | .activeMode = std::move(activeMode)}; |
Huihong Luo | 82e1563 | 2024-06-27 02:37:08 +0000 | [diff] [blame] | 3556 | if (mIsHdcpViaNegVsync) { |
| 3557 | state.isSecure = connectionType == ui::DisplayConnectionType::Internal; |
| 3558 | } else { |
| 3559 | // TODO(b/349703362): Remove this when HDCP aidl API becomes ready |
| 3560 | state.isSecure = true; // All physical displays are currently considered secure. |
| 3561 | } |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 3562 | state.isProtected = true; |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3563 | state.displayName = std::move(info.name); |
| 3564 | |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3565 | mCurrentState.displays.add(token, state); |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3566 | ALOGI("Connecting %s", displayString); |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3567 | return activeModeId; |
Dominik Laskowski | bb8267b | 2024-04-26 16:23:07 -0400 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | void SurfaceFlinger::processHotplugDisconnect(PhysicalDisplayId displayId, |
| 3571 | const char* displayString) { |
| 3572 | ALOGI("Disconnecting %s", displayString); |
| 3573 | |
| 3574 | const auto displayOpt = mPhysicalDisplays.get(displayId); |
| 3575 | LOG_ALWAYS_FATAL_IF(!displayOpt); |
| 3576 | const auto& display = displayOpt->get(); |
| 3577 | |
| 3578 | if (const ssize_t index = mCurrentState.displays.indexOfKey(display.token()); index >= 0) { |
| 3579 | mCurrentState.displays.removeItemsAt(index); |
| 3580 | } |
| 3581 | |
| 3582 | mPhysicalDisplays.erase(displayId); |
Dominik Laskowski | bb66e63 | 2022-08-17 08:00:25 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3585 | sp<DisplayDevice> SurfaceFlinger::setupNewDisplayDeviceInternal( |
Lloyd Pique | 9370a48 | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 3586 | const wp<IBinder>& displayToken, |
| 3587 | std::shared_ptr<compositionengine::Display> compositionDisplay, |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3588 | const DisplayDeviceState& state, |
| 3589 | const sp<compositionengine::DisplaySurface>& displaySurface, |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 3590 | const sp<IGraphicBufferProducer>& producer) { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 3591 | DisplayDeviceCreationArgs creationArgs(sp<SurfaceFlinger>::fromExisting(this), getHwComposer(), |
| 3592 | displayToken, compositionDisplay); |
Dominik Laskowski | e977409 | 2018-12-11 10:04:24 -0800 | [diff] [blame] | 3593 | creationArgs.sequenceId = state.sequenceId; |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 3594 | creationArgs.isSecure = state.isSecure; |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 3595 | creationArgs.isProtected = state.isProtected; |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3596 | creationArgs.displaySurface = displaySurface; |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 3597 | creationArgs.hasWideColorGamut = false; |
| 3598 | creationArgs.supportedPerFrameMetadata = 0; |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame] | 3599 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3600 | if (const auto physicalIdOpt = PhysicalDisplayId::tryCast(compositionDisplay->getId())) { |
| 3601 | const auto physicalId = *physicalIdOpt; |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 3602 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3603 | creationArgs.isPrimary = physicalId == getPrimaryDisplayIdLocked(); |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 3604 | creationArgs.refreshRateSelector = |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3605 | FTL_FAKE_GUARD(kMainThreadContext, |
| 3606 | mDisplayModeController.selectorPtrFor(physicalId)); |
Dominik Laskowski | 55c8540 | 2020-01-21 16:25:47 -0800 | [diff] [blame] | 3607 | |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3608 | mPhysicalDisplays.get(physicalId) |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 3609 | .transform(&PhysicalDisplay::snapshotRef) |
| 3610 | .transform(ftl::unit_fn([&](const display::DisplaySnapshot& snapshot) { |
| 3611 | for (const auto mode : snapshot.colorModes()) { |
| 3612 | creationArgs.hasWideColorGamut |= ui::isWideColorMode(mode); |
| 3613 | creationArgs.hwcColorModes |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 3614 | .emplace(mode, getHwComposer().getRenderIntents(physicalId, mode)); |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 3615 | } |
| 3616 | })); |
tangrobin | 6753a02 | 2018-08-10 10:58:54 +0800 | [diff] [blame] | 3617 | } |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3618 | |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 3619 | if (const auto id = HalDisplayId::tryCast(compositionDisplay->getId())) { |
| 3620 | getHwComposer().getHdrCapabilities(*id, &creationArgs.hdrCapabilities); |
| 3621 | creationArgs.supportedPerFrameMetadata = getHwComposer().getSupportedPerFrameMetadata(*id); |
Chia-I Wu | 0f509fb | 2018-06-21 15:52:50 +0800 | [diff] [blame] | 3622 | } |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3623 | |
Lloyd Pique | 90c115d | 2018-09-18 21:39:42 -0700 | [diff] [blame] | 3624 | auto nativeWindowSurface = getFactory().createNativeWindowSurface(producer); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3625 | auto nativeWindow = nativeWindowSurface->getNativeWindow(); |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 3626 | creationArgs.nativeWindow = nativeWindow; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3627 | |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3628 | // Make sure that composition can never be stalled by a virtual display |
| 3629 | // 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] | 3630 | // here, in case the display is composed entirely by HWC. |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 3631 | if (state.isVirtual()) { |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3632 | nativeWindow->setSwapInterval(nativeWindow.get(), 0); |
| 3633 | } |
| 3634 | |
Dominik Laskowski | 718f960 | 2019-11-09 20:01:35 -0800 | [diff] [blame] | 3635 | creationArgs.physicalOrientation = |
ramindani | 06e518e | 2022-03-14 18:47:53 +0000 | [diff] [blame] | 3636 | getPhysicalDisplayOrientation(compositionDisplay->getId(), creationArgs.isPrimary); |
| 3637 | ALOGV("Display Orientation: %s", toCString(creationArgs.physicalOrientation)); |
Chia-I Wu | a02871c | 2018-08-27 14:38:23 -0700 | [diff] [blame] | 3638 | |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 3639 | creationArgs.initialPowerMode = state.isVirtual() ? hal::PowerMode::ON : hal::PowerMode::OFF; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3640 | |
Huihong Luo | 1768cb0 | 2022-10-11 11:10:34 -0700 | [diff] [blame] | 3641 | creationArgs.requestedRefreshRate = state.requestedRefreshRate; |
| 3642 | |
Lloyd Pique | 9370a48 | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 3643 | sp<DisplayDevice> display = getFactory().createDisplayDevice(creationArgs); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3644 | |
Ady Abraham | 8565ad2 | 2021-06-22 17:45:44 -0700 | [diff] [blame] | 3645 | nativeWindowSurface->preallocateBuffers(); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3646 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 3647 | ui::ColorMode defaultColorMode = ui::ColorMode::NATIVE; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 3648 | Dataspace defaultDataSpace = Dataspace::UNKNOWN; |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 3649 | if (display->hasWideColorGamut()) { |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 3650 | defaultColorMode = ui::ColorMode::SRGB; |
Peiyong Lin | 14724e6 | 2018-12-05 07:27:30 -0800 | [diff] [blame] | 3651 | defaultDataSpace = Dataspace::V0_SRGB; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3652 | } |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 3653 | display->getCompositionDisplay()->setColorProfile( |
| 3654 | compositionengine::Output::ColorProfile{defaultColorMode, defaultDataSpace, |
Alec Mouri | 88790f3 | 2023-07-21 01:25:14 +0000 | [diff] [blame] | 3655 | RenderIntent::COLORIMETRIC}); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3656 | |
| 3657 | if (const auto& physical = state.physical) { |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 3658 | const auto& mode = *physical->activeMode; |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 3659 | mDisplayModeController.setActiveMode(physical->id, mode.getId(), mode.getVsyncRate(), |
ramindani | 914d106 | 2024-07-15 13:16:52 -0700 | [diff] [blame] | 3660 | mode.getPeakFps()); |
Lloyd Pique | 3c085a0 | 2018-05-09 19:38:32 -0700 | [diff] [blame] | 3661 | } |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 3662 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3663 | display->setLayerFilter(makeLayerFilterForDisplay(display->getId(), state.layerStack)); |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 3664 | display->setProjection(state.orientation, state.layerStackSpaceRect, |
| 3665 | state.orientedDisplaySpaceRect); |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 3666 | display->setDisplayName(state.displayName); |
Vishnu Nair | a119aaa | 2021-09-24 07:19:35 -0700 | [diff] [blame] | 3667 | display->setFlags(state.flags); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3668 | |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 3669 | return display; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 3670 | } |
| 3671 | |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3672 | void SurfaceFlinger::processDisplayAdded(const wp<IBinder>& displayToken, |
| 3673 | const DisplayDeviceState& state) { |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 3674 | ui::Size resolution(0, 0); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3675 | ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(PIXEL_FORMAT_UNKNOWN); |
| 3676 | if (state.physical) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 3677 | resolution = state.physical->activeMode->getResolution(); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3678 | pixelFormat = static_cast<ui::PixelFormat>(PIXEL_FORMAT_RGBA_8888); |
| 3679 | } else if (state.surface != nullptr) { |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 3680 | int status = state.surface->query(NATIVE_WINDOW_WIDTH, &resolution.width); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3681 | ALOGE_IF(status != NO_ERROR, "Unable to query width (%d)", status); |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 3682 | status = state.surface->query(NATIVE_WINDOW_HEIGHT, &resolution.height); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3683 | ALOGE_IF(status != NO_ERROR, "Unable to query height (%d)", status); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3684 | int format; |
| 3685 | status = state.surface->query(NATIVE_WINDOW_FORMAT, &format); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3686 | ALOGE_IF(status != NO_ERROR, "Unable to query format (%d)", status); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3687 | pixelFormat = static_cast<ui::PixelFormat>(format); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3688 | } else { |
| 3689 | // Virtual displays without a surface are dormant: |
| 3690 | // they have external state (layer stack, projection, |
| 3691 | // etc.) but no internal state (i.e. a DisplayDevice). |
| 3692 | return; |
| 3693 | } |
| 3694 | |
| 3695 | compositionengine::DisplayCreationArgsBuilder builder; |
| 3696 | if (const auto& physical = state.physical) { |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3697 | builder.setId(physical->id); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3698 | } else { |
Dominik Laskowski | 263eec4 | 2021-07-21 23:13:24 -0700 | [diff] [blame] | 3699 | builder.setId(acquireVirtualDisplay(resolution, pixelFormat)); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3700 | } |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3701 | |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 3702 | builder.setPixels(resolution); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3703 | builder.setIsSecure(state.isSecure); |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 3704 | builder.setIsProtected(state.isProtected); |
Xiang Wang | 839fe5b | 2022-04-04 17:39:38 +0000 | [diff] [blame] | 3705 | builder.setPowerAdvisor(mPowerAdvisor.get()); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3706 | builder.setName(state.displayName); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3707 | auto compositionDisplay = getCompositionEngine().createDisplay(builder.build()); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3708 | compositionDisplay->setLayerCachingEnabled(mLayerCachingEnabled); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3709 | |
| 3710 | sp<compositionengine::DisplaySurface> displaySurface; |
| 3711 | sp<IGraphicBufferProducer> producer; |
| 3712 | sp<IGraphicBufferProducer> bqProducer; |
| 3713 | sp<IGraphicBufferConsumer> bqConsumer; |
| 3714 | getFactory().createBufferQueue(&bqProducer, &bqConsumer, /*consumerIsSurfaceFlinger =*/false); |
| 3715 | |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3716 | if (state.isVirtual()) { |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3717 | const auto displayId = VirtualDisplayId::tryCast(compositionDisplay->getId()); |
| 3718 | LOG_FATAL_IF(!displayId); |
| 3719 | auto surface = sp<VirtualDisplaySurface>::make(getHwComposer(), *displayId, state.surface, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3720 | bqProducer, bqConsumer, state.displayName); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3721 | displaySurface = surface; |
| 3722 | producer = std::move(surface); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3723 | } else { |
| 3724 | ALOGE_IF(state.surface != nullptr, |
| 3725 | "adding a supported display, but rendering " |
| 3726 | "surface is provided (%p), ignoring it", |
| 3727 | state.surface.get()); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3728 | const auto displayId = PhysicalDisplayId::tryCast(compositionDisplay->getId()); |
| 3729 | LOG_FATAL_IF(!displayId); |
Jim Shargo | 6ccc5e8 | 2024-07-27 03:42:08 +0000 | [diff] [blame] | 3730 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 3731 | const auto frameBufferSurface = |
| 3732 | sp<FramebufferSurface>::make(getHwComposer(), *displayId, bqProducer, bqConsumer, |
| 3733 | state.physical->activeMode->getResolution(), |
| 3734 | ui::Size(maxGraphicsWidth, maxGraphicsHeight)); |
| 3735 | displaySurface = frameBufferSurface; |
| 3736 | producer = frameBufferSurface->getSurface()->getIGraphicBufferProducer(); |
| 3737 | #else |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 3738 | displaySurface = |
| 3739 | sp<FramebufferSurface>::make(getHwComposer(), *displayId, bqConsumer, |
| 3740 | state.physical->activeMode->getResolution(), |
| 3741 | ui::Size(maxGraphicsWidth, maxGraphicsHeight)); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3742 | producer = bqProducer; |
Jim Shargo | 6ccc5e8 | 2024-07-27 03:42:08 +0000 | [diff] [blame] | 3743 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3744 | } |
| 3745 | |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3746 | LOG_FATAL_IF(!displaySurface); |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 3747 | auto display = setupNewDisplayDeviceInternal(displayToken, std::move(compositionDisplay), state, |
| 3748 | displaySurface, producer); |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 3749 | |
| 3750 | if (mScheduler && !display->isVirtual()) { |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 3751 | // TODO(b/241285876): Annotate `processDisplayAdded` instead. |
| 3752 | ftl::FakeGuard guard(kMainThreadContext); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 3753 | |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 3754 | // For hotplug reconnect, renew the registration since display modes have been reloaded. |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 3755 | mScheduler->registerDisplay(display->getPhysicalId(), display->holdRefreshRateSelector(), |
| 3756 | mActiveDisplayId); |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3757 | } |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 3758 | |
Huihong Luo | 1768cb0 | 2022-10-11 11:10:34 -0700 | [diff] [blame] | 3759 | if (display->isVirtual()) { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 3760 | display->adjustRefreshRate(mScheduler->getPacesetterRefreshRate()); |
Huihong Luo | 1768cb0 | 2022-10-11 11:10:34 -0700 | [diff] [blame] | 3761 | } |
| 3762 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 3763 | mDisplays.try_emplace(displayToken, std::move(display)); |
Dominik Laskowski | 8809687 | 2023-12-08 15:26:04 -0500 | [diff] [blame] | 3764 | |
| 3765 | // For an external display, loadDisplayModes already attempted to select the same mode |
| 3766 | // as DM, but SF still needs to be updated to match. |
| 3767 | // TODO (b/318534874): Let DM decide the initial mode. |
| 3768 | if (const auto& physical = state.physical; |
| 3769 | mScheduler && physical && FlagManager::getInstance().connected_display()) { |
| 3770 | const bool isInternalDisplay = mPhysicalDisplays.get(physical->id) |
| 3771 | .transform(&PhysicalDisplay::isInternal) |
| 3772 | .value_or(false); |
| 3773 | |
| 3774 | if (!isInternalDisplay) { |
| 3775 | auto activeModePtr = physical->activeMode; |
| 3776 | const auto fps = activeModePtr->getPeakFps(); |
| 3777 | |
| 3778 | setDesiredMode( |
| 3779 | {.mode = scheduler::FrameRateMode{fps, |
| 3780 | ftl::as_non_null(std::move(activeModePtr))}, |
| 3781 | .emitEvent = false, |
| 3782 | .force = true}); |
| 3783 | } |
| 3784 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3785 | } |
| 3786 | |
| 3787 | void SurfaceFlinger::processDisplayRemoved(const wp<IBinder>& displayToken) { |
Vishnu Nair | f78589c | 2020-12-02 18:36:03 -0800 | [diff] [blame] | 3788 | auto display = getDisplayDeviceLocked(displayToken); |
| 3789 | if (display) { |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3790 | display->disconnect(); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3791 | |
| 3792 | if (display->isVirtual()) { |
| 3793 | releaseVirtualDisplay(display->getVirtualId()); |
| 3794 | } else { |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 3795 | mScheduler->unregisterDisplay(display->getPhysicalId(), mActiveDisplayId); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3796 | } |
| 3797 | } |
| 3798 | |
| 3799 | mDisplays.erase(displayToken); |
Vishnu Nair | f78589c | 2020-12-02 18:36:03 -0800 | [diff] [blame] | 3800 | |
| 3801 | if (display && display->isVirtual()) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 3802 | static_cast<void>(mScheduler->schedule([display = std::move(display)] { |
Vishnu Nair | f78589c | 2020-12-02 18:36:03 -0800 | [diff] [blame] | 3803 | // Destroy the display without holding the mStateLock. |
| 3804 | // This is a temporary solution until we can manage transaction queues without |
| 3805 | // holding the mStateLock. |
| 3806 | // With blast, the IGBP that is passed to the VirtualDisplaySurface is owned by the |
| 3807 | // client. When the IGBP is disconnected, its buffer cache in SF will be cleared |
| 3808 | // via SurfaceComposerClient::doUncacheBufferTransaction. This call from the client |
| 3809 | // ends up running on the main thread causing a deadlock since setTransactionstate |
| 3810 | // will try to acquire the mStateLock. Instead we extend the lifetime of |
| 3811 | // DisplayDevice and destroy it in the main thread without holding the mStateLock. |
| 3812 | // The display will be disconnected and removed from the mDisplays list so it will |
| 3813 | // not be accessible. |
| 3814 | })); |
| 3815 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | void SurfaceFlinger::processDisplayChanged(const wp<IBinder>& displayToken, |
| 3819 | const DisplayDeviceState& currentState, |
| 3820 | const DisplayDeviceState& drawingState) { |
| 3821 | const sp<IBinder> currentBinder = IInterface::asBinder(currentState.surface); |
| 3822 | const sp<IBinder> drawingBinder = IInterface::asBinder(drawingState.surface); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3823 | |
| 3824 | // Recreate the DisplayDevice if the surface or sequence ID changed. |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3825 | if (currentBinder != drawingBinder || currentState.sequenceId != drawingState.sequenceId) { |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3826 | if (const auto display = getDisplayDeviceLocked(displayToken)) { |
| 3827 | display->disconnect(); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3828 | if (display->isVirtual()) { |
| 3829 | releaseVirtualDisplay(display->getVirtualId()); |
| 3830 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3831 | } |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3832 | |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3833 | mDisplays.erase(displayToken); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3834 | |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3835 | if (const auto& physical = currentState.physical) { |
Lucas Berthou | 8d0a0c4 | 2024-08-27 14:32:31 +0000 | [diff] [blame] | 3836 | getHwComposer().allocatePhysicalDisplay(physical->hwcDisplayId, physical->id, |
| 3837 | /*physicalSize=*/std::nullopt); |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3838 | } |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3839 | |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3840 | processDisplayAdded(displayToken, currentState); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 3841 | |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3842 | if (currentState.physical) { |
Marin Shalamanov | 4c5e301 | 2020-06-04 21:41:42 +0200 | [diff] [blame] | 3843 | const auto display = getDisplayDeviceLocked(displayToken); |
Robert Horvath | dce110a | 2022-09-16 11:08:47 +0200 | [diff] [blame] | 3844 | if (!mSkipPowerOnForQuiescent) { |
| 3845 | setPowerModeInternal(display, hal::PowerMode::ON); |
| 3846 | } |
Marin Shalamanov | f7f6b3c | 2020-12-09 13:19:38 +0100 | [diff] [blame] | 3847 | |
Dominik Laskowski | f504875 | 2024-08-04 15:08:00 -0400 | [diff] [blame] | 3848 | if (display->getPhysicalId() == mActiveDisplayId) { |
| 3849 | onActiveDisplayChangedLocked(nullptr, *display); |
Marin Shalamanov | f7f6b3c | 2020-12-09 13:19:38 +0100 | [diff] [blame] | 3850 | } |
Marin Shalamanov | 700e639 | 2020-02-12 20:22:26 +0100 | [diff] [blame] | 3851 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3852 | return; |
| 3853 | } |
| 3854 | |
| 3855 | if (const auto display = getDisplayDeviceLocked(displayToken)) { |
| 3856 | if (currentState.layerStack != drawingState.layerStack) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 3857 | display->setLayerFilter( |
| 3858 | makeLayerFilterForDisplay(display->getId(), currentState.layerStack)); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3859 | } |
Evan Rosky | 2239b37 | 2021-05-20 13:43:47 -0700 | [diff] [blame] | 3860 | if (currentState.flags != drawingState.flags) { |
| 3861 | display->setFlags(currentState.flags); |
| 3862 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3863 | if ((currentState.orientation != drawingState.orientation) || |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 3864 | (currentState.layerStackSpaceRect != drawingState.layerStackSpaceRect) || |
| 3865 | (currentState.orientedDisplaySpaceRect != drawingState.orientedDisplaySpaceRect)) { |
| 3866 | display->setProjection(currentState.orientation, currentState.layerStackSpaceRect, |
| 3867 | currentState.orientedDisplaySpaceRect); |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 3868 | if (display->getId() == mActiveDisplayId) { |
Ady Abraham | 1273ac1 | 2021-08-26 17:31:53 -0700 | [diff] [blame] | 3869 | mActiveDisplayTransformHint = display->getTransformHint(); |
Leon Scroggins III | 85d4b22 | 2023-05-09 13:58:18 -0400 | [diff] [blame] | 3870 | sActiveDisplayRotationFlags = |
| 3871 | ui::Transform::toRotationFlags(display->getOrientation()); |
Arthur Hung | b6aa9a0 | 2021-06-09 14:23:01 +0800 | [diff] [blame] | 3872 | } |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3873 | } |
| 3874 | if (currentState.width != drawingState.width || |
| 3875 | currentState.height != drawingState.height) { |
| 3876 | display->setDisplaySize(currentState.width, currentState.height); |
Dominik Laskowski | 2013464 | 2020-04-20 22:36:44 -0700 | [diff] [blame] | 3877 | |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 3878 | if (display->getId() == mActiveDisplayId) { |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 3879 | onActiveDisplaySizeChanged(*display); |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3880 | } |
| 3881 | } |
| 3882 | } |
| 3883 | } |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 3884 | |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 3885 | void SurfaceFlinger::processDisplayChangesLocked() { |
| 3886 | // here we take advantage of Vector's copy-on-write semantics to |
| 3887 | // improve performance by skipping the transaction entirely when |
| 3888 | // know that the lists are identical |
| 3889 | const KeyedVector<wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays); |
| 3890 | const KeyedVector<wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays); |
| 3891 | if (!curr.isIdenticalTo(draw)) { |
| 3892 | mVisibleRegionsDirty = true; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 3893 | mUpdateInputInfo = true; |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 3894 | |
Dominik Laskowski | 01f191c | 2024-01-29 11:51:23 -0500 | [diff] [blame] | 3895 | // Apply the current color matrix to any added or changed display. |
| 3896 | mCurrentState.colorMatrixChanged = true; |
| 3897 | |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 3898 | // find the displays that were removed |
| 3899 | // (ie: in drawing state but not in current state) |
| 3900 | // also handle displays that changed |
| 3901 | // (ie: displays that are in both lists) |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3902 | for (size_t i = 0; i < draw.size(); i++) { |
| 3903 | const wp<IBinder>& displayToken = draw.keyAt(i); |
| 3904 | const ssize_t j = curr.indexOfKey(displayToken); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 3905 | if (j < 0) { |
| 3906 | // in drawing state but not in current state |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3907 | processDisplayRemoved(displayToken); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 3908 | } else { |
| 3909 | // this display is in both lists. see if something changed. |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3910 | const DisplayDeviceState& currentState = curr[j]; |
| 3911 | const DisplayDeviceState& drawingState = draw[i]; |
| 3912 | processDisplayChanged(displayToken, currentState, drawingState); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 3913 | } |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | // find displays that were added |
| 3917 | // (ie: in current state but not in drawing state) |
Marin Shalamanov | ae68559 | 2020-02-12 17:12:22 +0100 | [diff] [blame] | 3918 | for (size_t i = 0; i < curr.size(); i++) { |
| 3919 | const wp<IBinder>& displayToken = curr.keyAt(i); |
| 3920 | if (draw.indexOfKey(displayToken) < 0) { |
| 3921 | processDisplayAdded(displayToken, curr[i]); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | } |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 3925 | |
| 3926 | mDrawingState.displays = mCurrentState.displays; |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 3927 | } |
| 3928 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 3929 | void SurfaceFlinger::commitTransactionsLocked(uint32_t transactionFlags) { |
| 3930 | // Commit display transactions. |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 3931 | const bool displayTransactionNeeded = transactionFlags & eDisplayTransactionNeeded; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 3932 | mFrontEndDisplayInfosChanged = displayTransactionNeeded; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3933 | |
Robert Carr | e450fb5 | 2021-06-11 13:21:09 -0700 | [diff] [blame] | 3934 | if (mSomeChildrenChanged) { |
| 3935 | mVisibleRegionsDirty = true; |
| 3936 | mSomeChildrenChanged = false; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 3937 | mUpdateInputInfo = true; |
Robert Carr | e450fb5 | 2021-06-11 13:21:09 -0700 | [diff] [blame] | 3938 | } |
| 3939 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3940 | if (mLayersAdded) { |
| 3941 | mLayersAdded = false; |
| 3942 | // Layers have been added. |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 3943 | mVisibleRegionsDirty = true; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 3944 | mUpdateInputInfo = true; |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | // some layers might have been removed, so |
| 3948 | // we need to update the regions they're exposing. |
| 3949 | if (mLayersRemoved) { |
| 3950 | mLayersRemoved = false; |
| 3951 | mVisibleRegionsDirty = true; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 3952 | mUpdateInputInfo = true; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3953 | } |
| 3954 | |
Patrick Williams | af06b79 | 2023-02-16 17:46:22 -0600 | [diff] [blame] | 3955 | if (transactionFlags & eInputInfoUpdateNeeded) { |
| 3956 | mUpdateInputInfo = true; |
| 3957 | } |
| 3958 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 3959 | doCommitTransactions(); |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 3960 | } |
| 3961 | |
Patrick Williams | 6681c02 | 2023-05-15 13:38:16 -0500 | [diff] [blame] | 3962 | void SurfaceFlinger::updateInputFlinger(VsyncId vsyncId, TimePoint frameTime) { |
Patrick Williams | d86bd8c | 2023-04-21 11:54:11 -0500 | [diff] [blame] | 3963 | if (!mInputFlinger || (!mUpdateInputInfo && mInputWindowCommands.empty())) { |
Vishnu Nair | 42a27b5 | 2021-11-18 15:35:22 -0800 | [diff] [blame] | 3964 | return; |
| 3965 | } |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 3966 | SFTRACE_CALL(); |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 3967 | |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 3968 | std::vector<WindowInfo> windowInfos; |
| 3969 | std::vector<DisplayInfo> displayInfos; |
| 3970 | bool updateWindowInfo = false; |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 3971 | if (mUpdateInputInfo) { |
| 3972 | mUpdateInputInfo = false; |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 3973 | updateWindowInfo = true; |
| 3974 | buildWindowInfos(windowInfos, displayInfos); |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 3975 | } |
Arthur Hung | 9ed4339 | 2022-05-27 06:31:57 +0000 | [diff] [blame] | 3976 | |
Patrick Williams | d86bd8c | 2023-04-21 11:54:11 -0500 | [diff] [blame] | 3977 | std::unordered_set<int32_t> visibleWindowIds; |
| 3978 | for (WindowInfo& windowInfo : windowInfos) { |
| 3979 | if (!windowInfo.inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
| 3980 | visibleWindowIds.insert(windowInfo.id); |
Patrick Williams | 1caf3b7 | 2023-03-31 10:59:10 -0500 | [diff] [blame] | 3981 | } |
Patrick Williams | d86bd8c | 2023-04-21 11:54:11 -0500 | [diff] [blame] | 3982 | } |
| 3983 | bool visibleWindowsChanged = false; |
| 3984 | if (visibleWindowIds != mVisibleWindowIds) { |
| 3985 | visibleWindowsChanged = true; |
| 3986 | mVisibleWindowIds = std::move(visibleWindowIds); |
Patrick Williams | 1caf3b7 | 2023-03-31 10:59:10 -0500 | [diff] [blame] | 3987 | } |
| 3988 | |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 3989 | BackgroundExecutor::getInstance().sendCallbacks({[updateWindowInfo, |
| 3990 | windowInfos = std::move(windowInfos), |
| 3991 | displayInfos = std::move(displayInfos), |
| 3992 | inputWindowCommands = |
| 3993 | std::move(mInputWindowCommands), |
Patrick Williams | 1caf3b7 | 2023-03-31 10:59:10 -0500 | [diff] [blame] | 3994 | inputFlinger = mInputFlinger, this, |
Patrick Williams | 6681c02 | 2023-05-15 13:38:16 -0500 | [diff] [blame] | 3995 | visibleWindowsChanged, vsyncId, frameTime]() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 3996 | SFTRACE_NAME("BackgroundExecutor::updateInputFlinger"); |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 3997 | if (updateWindowInfo) { |
Patrick Williams | 641f7f2 | 2022-06-22 19:25:35 +0000 | [diff] [blame] | 3998 | mWindowInfosListenerInvoker |
Patrick Williams | 6681c02 | 2023-05-15 13:38:16 -0500 | [diff] [blame] | 3999 | ->windowInfosChanged(gui::WindowInfosUpdate{std::move(windowInfos), |
| 4000 | std::move(displayInfos), |
| 4001 | ftl::to_underlying(vsyncId), |
| 4002 | frameTime.ns()}, |
Patrick Williams | 1caf3b7 | 2023-03-31 10:59:10 -0500 | [diff] [blame] | 4003 | std::move( |
| 4004 | inputWindowCommands.windowInfosReportedListeners), |
Patrick Williams | d86bd8c | 2023-04-21 11:54:11 -0500 | [diff] [blame] | 4005 | /* forceImmediateCall= */ visibleWindowsChanged || |
Patrick Williams | 6681c02 | 2023-05-15 13:38:16 -0500 | [diff] [blame] | 4006 | !inputWindowCommands.focusRequests.empty()); |
Patrick Williams | 641f7f2 | 2022-06-22 19:25:35 +0000 | [diff] [blame] | 4007 | } else { |
| 4008 | // If there are listeners but no changes to input windows, call the listeners |
| 4009 | // immediately. |
| 4010 | for (const auto& listener : inputWindowCommands.windowInfosReportedListeners) { |
| 4011 | if (IInterface::asBinder(listener)->isBinderAlive()) { |
| 4012 | listener->onWindowInfosReported(); |
| 4013 | } |
| 4014 | } |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 4015 | } |
| 4016 | for (const auto& focusRequest : inputWindowCommands.focusRequests) { |
| 4017 | inputFlinger->setFocusedWindow(focusRequest); |
| 4018 | } |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 4019 | }}); |
Jaineel Mehta | ac331c5 | 2021-11-29 21:38:10 +0000 | [diff] [blame] | 4020 | |
Arthur Hung | 6cbb975 | 2019-09-05 16:38:18 +0800 | [diff] [blame] | 4021 | mInputWindowCommands.clear(); |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 4022 | } |
| 4023 | |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 4024 | void SurfaceFlinger::persistDisplayBrightness(bool needsComposite) { |
| 4025 | const bool supportsDisplayBrightnessCommand = getHwComposer().getComposer()->isSupported( |
| 4026 | Hwc2::Composer::OptionalFeature::DisplayBrightnessCommand); |
| 4027 | if (!supportsDisplayBrightnessCommand) { |
| 4028 | return; |
| 4029 | } |
| 4030 | |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 4031 | for (const auto& [_, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 4032 | if (const auto brightness = display->getStagedBrightness(); brightness) { |
| 4033 | if (!needsComposite) { |
| 4034 | const status_t error = |
| 4035 | getHwComposer() |
| 4036 | .setDisplayBrightness(display->getPhysicalId(), *brightness, |
Alec Mouri | 4d8a05d | 2022-03-23 18:14:26 +0000 | [diff] [blame] | 4037 | display->getCompositionDisplay() |
| 4038 | ->getState() |
| 4039 | .displayBrightnessNits, |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 4040 | Hwc2::Composer::DisplayBrightnessOptions{ |
| 4041 | .applyImmediately = true}) |
| 4042 | .get(); |
| 4043 | |
| 4044 | ALOGE_IF(error != NO_ERROR, |
| 4045 | "Error setting display brightness for display %s: %d (%s)", |
Dominik Laskowski | 5d7de5f | 2022-11-03 12:38:32 -0400 | [diff] [blame] | 4046 | to_string(display->getId()).c_str(), error, strerror(error)); |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 4047 | } |
| 4048 | display->persistBrightness(needsComposite); |
| 4049 | } |
| 4050 | } |
| 4051 | } |
| 4052 | |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 4053 | void SurfaceFlinger::buildWindowInfos(std::vector<WindowInfo>& outWindowInfos, |
| 4054 | std::vector<DisplayInfo>& outDisplayInfos) { |
Dominik Laskowski | 9f410f0 | 2022-01-08 16:22:46 -0800 | [diff] [blame] | 4055 | static size_t sNumWindowInfos = 0; |
| 4056 | outWindowInfos.reserve(sNumWindowInfos); |
| 4057 | sNumWindowInfos = 0; |
| 4058 | |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 4059 | mLayerSnapshotBuilder.forEachInputSnapshot( |
| 4060 | [&outWindowInfos](const frontend::LayerSnapshot& snapshot) { |
| 4061 | outWindowInfos.push_back(snapshot.inputInfo); |
| 4062 | }); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 4063 | |
Dominik Laskowski | 9f410f0 | 2022-01-08 16:22:46 -0800 | [diff] [blame] | 4064 | sNumWindowInfos = outWindowInfos.size(); |
| 4065 | |
Vishnu Nair | 67b431c | 2022-11-16 01:54:05 +0000 | [diff] [blame] | 4066 | outDisplayInfos.reserve(mFrontEndDisplayInfos.size()); |
| 4067 | for (const auto& [_, info] : mFrontEndDisplayInfos) { |
Dominik Laskowski | 9f410f0 | 2022-01-08 16:22:46 -0800 | [diff] [blame] | 4068 | outDisplayInfos.push_back(info.info); |
Prabir Pradhan | d0aba78 | 2021-12-14 00:44:21 -0800 | [diff] [blame] | 4069 | } |
Robert Carr | 720e506 | 2018-07-30 17:45:14 -0700 | [diff] [blame] | 4070 | } |
| 4071 | |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 4072 | void SurfaceFlinger::updateCursorAsync() { |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 4073 | compositionengine::CompositionRefreshArgs refreshArgs; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 4074 | for (const auto& [_, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 4075 | if (HalDisplayId::tryCast(display->getId())) { |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 4076 | refreshArgs.outputs.push_back(display->getCompositionDisplay()); |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 4077 | } |
| 4078 | } |
Dominik Laskowski | 16a345b | 2023-04-27 17:07:33 -0400 | [diff] [blame] | 4079 | |
| 4080 | constexpr bool kCursorOnly = true; |
| 4081 | const auto layers = moveSnapshotsToCompositionArgs(refreshArgs, kCursorOnly); |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 4082 | mCompositionEngine->updateCursorAsync(refreshArgs); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4083 | moveSnapshotsFromCompositionArgs(refreshArgs, layers); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 4084 | } |
| 4085 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 4086 | void SurfaceFlinger::requestHardwareVsync(PhysicalDisplayId displayId, bool enable) { |
| 4087 | getHwComposer().setVsyncEnabled(displayId, enable ? hal::Vsync::ENABLE : hal::Vsync::DISABLE); |
| 4088 | } |
| 4089 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4090 | void SurfaceFlinger::requestDisplayModes(std::vector<display::DisplayModeRequest> modeRequests) { |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 4091 | if (mBootStage != BootStage::FINISHED) { |
| 4092 | ALOGV("Currently in the boot stage, skipping display mode changes"); |
| 4093 | return; |
| 4094 | } |
| 4095 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4096 | SFTRACE_CALL(); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4097 | |
Bart Sears | 2d7e7f8 | 2024-07-13 19:42:29 +0000 | [diff] [blame] | 4098 | // If this is called from the main thread mStateLock must be locked before |
| 4099 | // Currently the only way to call this function from the main thread is from |
| 4100 | // Scheduler::chooseRefreshRateForContent |
| 4101 | |
| 4102 | ConditionalLock lock(mStateLock, std::this_thread::get_id() != mMainThreadId); |
| 4103 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4104 | for (auto& request : modeRequests) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 4105 | const auto& modePtr = request.mode.modePtr; |
Bart Sears | 2d7e7f8 | 2024-07-13 19:42:29 +0000 | [diff] [blame] | 4106 | |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 4107 | const auto displayId = modePtr->getPhysicalDisplayId(); |
Bart Sears | 2d7e7f8 | 2024-07-13 19:42:29 +0000 | [diff] [blame] | 4108 | const auto display = getDisplayDeviceLocked(displayId); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4109 | |
Bart Sears | 2d7e7f8 | 2024-07-13 19:42:29 +0000 | [diff] [blame] | 4110 | if (!display) continue; |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4111 | |
Bart Sears | 2d7e7f8 | 2024-07-13 19:42:29 +0000 | [diff] [blame] | 4112 | if (display->refreshRateSelector().isModeAllowed(request.mode)) { |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 4113 | setDesiredMode(std::move(request)); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4114 | } else { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 4115 | ALOGV("%s: Mode %d is disallowed for display %s", __func__, |
| 4116 | ftl::to_underlying(modePtr->getId()), to_string(displayId).c_str()); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 4117 | } |
| 4118 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 4119 | } |
| 4120 | |
jimmyshiu | 4e21177 | 2023-06-15 15:18:38 +0000 | [diff] [blame] | 4121 | void SurfaceFlinger::notifyCpuLoadUp() { |
| 4122 | mPowerAdvisor->notifyCpuLoadUp(); |
| 4123 | } |
| 4124 | |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 4125 | void SurfaceFlinger::onChoreographerAttached() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4126 | SFTRACE_CALL(); |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 4127 | mUpdateAttachedChoreographer = true; |
| 4128 | scheduleCommit(FrameHint::kNone); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 4129 | } |
| 4130 | |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4131 | void SurfaceFlinger::onExpectedPresentTimePosted(TimePoint expectedPresentTime, |
| 4132 | ftl::NonNull<DisplayModePtr> modePtr, |
| 4133 | Fps renderRate) { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 4134 | const auto vsyncPeriod = modePtr->getVsyncRate().getPeriod(); |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4135 | const auto timeoutOpt = [&]() -> std::optional<Period> { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 4136 | const auto vrrConfig = modePtr->getVrrConfig(); |
| 4137 | if (!vrrConfig) return std::nullopt; |
| 4138 | |
| 4139 | const auto notifyExpectedPresentConfig = |
| 4140 | modePtr->getVrrConfig()->notifyExpectedPresentConfig; |
| 4141 | if (!notifyExpectedPresentConfig) return std::nullopt; |
ramindani | cbd7a6d | 2023-12-19 16:00:30 -0800 | [diff] [blame] | 4142 | return Period::fromNs(notifyExpectedPresentConfig->timeoutNs); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 4143 | }(); |
| 4144 | |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4145 | notifyExpectedPresentIfRequired(modePtr->getPhysicalDisplayId(), vsyncPeriod, |
| 4146 | expectedPresentTime, renderRate, timeoutOpt); |
| 4147 | } |
| 4148 | |
| 4149 | void SurfaceFlinger::notifyExpectedPresentIfRequired(PhysicalDisplayId displayId, |
| 4150 | Period vsyncPeriod, |
| 4151 | TimePoint expectedPresentTime, |
| 4152 | Fps frameInterval, |
| 4153 | std::optional<Period> timeoutOpt) { |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4154 | auto& data = mNotifyExpectedPresentMap[displayId]; |
| 4155 | const auto lastExpectedPresentTimestamp = data.lastExpectedPresentTimestamp; |
| 4156 | const auto lastFrameInterval = data.lastFrameInterval; |
| 4157 | data.lastFrameInterval = frameInterval; |
| 4158 | data.lastExpectedPresentTimestamp = expectedPresentTime; |
| 4159 | const auto threshold = Duration::fromNs(vsyncPeriod.ns() / 2); |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4160 | |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4161 | const constexpr nsecs_t kOneSecondNs = |
| 4162 | std::chrono::duration_cast<std::chrono::nanoseconds>(1s).count(); |
| 4163 | const auto timeout = |
| 4164 | Period::fromNs(timeoutOpt && timeoutOpt->ns() > 0 ? timeoutOpt->ns() : kOneSecondNs); |
| 4165 | const bool frameIntervalIsOnCadence = |
| 4166 | isFrameIntervalOnCadence(expectedPresentTime, lastExpectedPresentTimestamp, |
| 4167 | lastFrameInterval, timeout, threshold); |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4168 | |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4169 | const bool expectedPresentWithinTimeout = |
| 4170 | isExpectedPresentWithinTimeout(expectedPresentTime, lastExpectedPresentTimestamp, |
| 4171 | timeoutOpt, threshold); |
| 4172 | if (expectedPresentWithinTimeout && frameIntervalIsOnCadence) { |
| 4173 | return; |
| 4174 | } |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4175 | |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4176 | auto hintStatus = data.hintStatus.load(); |
| 4177 | if (!expectedPresentWithinTimeout) { |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4178 | if ((hintStatus != NotifyExpectedPresentHintStatus::Sent && |
| 4179 | hintStatus != NotifyExpectedPresentHintStatus::ScheduleOnTx) || |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4180 | (timeoutOpt && timeoutOpt->ns() == 0)) { |
| 4181 | // Send the hint immediately if timeout, as the hint gets |
| 4182 | // delayed otherwise, as the frame is scheduled close |
| 4183 | // to the actual present. |
| 4184 | if (data.hintStatus |
| 4185 | .compare_exchange_strong(hintStatus, |
| 4186 | NotifyExpectedPresentHintStatus::ScheduleOnTx)) { |
| 4187 | scheduleNotifyExpectedPresentHint(displayId); |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4188 | return; |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4189 | } |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4190 | } |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4191 | } |
| 4192 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4193 | if (hintStatus == NotifyExpectedPresentHintStatus::Sent && |
| 4194 | data.hintStatus.compare_exchange_strong(hintStatus, |
| 4195 | NotifyExpectedPresentHintStatus::ScheduleOnTx)) { |
| 4196 | return; |
| 4197 | } |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4198 | if (hintStatus != NotifyExpectedPresentHintStatus::Start) { |
| 4199 | return; |
| 4200 | } |
| 4201 | data.hintStatus.store(NotifyExpectedPresentHintStatus::ScheduleOnPresent); |
| 4202 | mScheduler->scheduleFrame(); |
| 4203 | } |
| 4204 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4205 | void SurfaceFlinger::scheduleNotifyExpectedPresentHint(PhysicalDisplayId displayId, |
| 4206 | VsyncId vsyncId) { |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4207 | auto itr = mNotifyExpectedPresentMap.find(displayId); |
| 4208 | if (itr == mNotifyExpectedPresentMap.end()) { |
| 4209 | return; |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 4210 | } |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4211 | |
| 4212 | const char* const whence = __func__; |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4213 | const auto sendHint = [=, this]() { |
| 4214 | auto& data = mNotifyExpectedPresentMap.at(displayId); |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4215 | TimePoint expectedPresentTime = data.lastExpectedPresentTimestamp; |
| 4216 | if (ftl::to_underlying(vsyncId) != FrameTimelineInfo::INVALID_VSYNC_ID) { |
| 4217 | const auto predictionOpt = mFrameTimeline->getTokenManager()->getPredictionsForToken( |
| 4218 | ftl::to_underlying(vsyncId)); |
| 4219 | const auto expectedPresentTimeOnPredictor = TimePoint::fromNs( |
| 4220 | predictionOpt ? predictionOpt->presentTime : expectedPresentTime.ns()); |
| 4221 | const auto scheduledFrameResultOpt = mScheduler->getScheduledFrameResult(); |
| 4222 | const auto expectedPresentTimeOnScheduler = scheduledFrameResultOpt.has_value() |
| 4223 | ? scheduledFrameResultOpt->vsyncTime |
| 4224 | : TimePoint::fromNs(0); |
| 4225 | expectedPresentTime = |
| 4226 | std::max(expectedPresentTimeOnPredictor, expectedPresentTimeOnScheduler); |
| 4227 | } |
| 4228 | |
| 4229 | if (expectedPresentTime < TimePoint::now()) { |
| 4230 | expectedPresentTime = |
| 4231 | mScheduler->getVsyncSchedule()->vsyncDeadlineAfter(TimePoint::now()); |
| 4232 | if (mScheduler->vsyncModulator().getVsyncConfig().sfWorkDuration > |
| 4233 | mScheduler->getVsyncSchedule(displayId)->period()) { |
| 4234 | expectedPresentTime += mScheduler->getVsyncSchedule(displayId)->period(); |
| 4235 | } |
| 4236 | } |
| 4237 | const auto status = getHwComposer().notifyExpectedPresent(displayId, expectedPresentTime, |
| 4238 | data.lastFrameInterval); |
ramindani | c67d22c | 2023-11-28 14:58:47 -0800 | [diff] [blame] | 4239 | if (status != NO_ERROR) { |
| 4240 | ALOGE("%s failed to notifyExpectedPresentHint for display %" PRId64, whence, |
| 4241 | displayId.value); |
| 4242 | } |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4243 | }; |
| 4244 | |
| 4245 | if (itr->second.hintStatus == NotifyExpectedPresentHintStatus::ScheduleOnTx) { |
| 4246 | return static_cast<void>(mScheduler->schedule([=, |
| 4247 | this]() FTL_FAKE_GUARD(kMainThreadContext) { |
| 4248 | auto& data = mNotifyExpectedPresentMap.at(displayId); |
| 4249 | auto scheduleHintOnTx = NotifyExpectedPresentHintStatus::ScheduleOnTx; |
| 4250 | if (data.hintStatus.compare_exchange_strong(scheduleHintOnTx, |
| 4251 | NotifyExpectedPresentHintStatus::Sent)) { |
| 4252 | sendHint(); |
ramindani | 2ff3cd4 | 2024-08-27 10:46:26 -0700 | [diff] [blame] | 4253 | constexpr bool kAllowToEnable = true; |
| 4254 | mScheduler->resyncToHardwareVsync(displayId, kAllowToEnable); |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4255 | } |
| 4256 | })); |
| 4257 | } |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4258 | auto scheduleHintOnPresent = NotifyExpectedPresentHintStatus::ScheduleOnPresent; |
| 4259 | if (itr->second.hintStatus.compare_exchange_strong(scheduleHintOnPresent, |
| 4260 | NotifyExpectedPresentHintStatus::Sent)) { |
| 4261 | sendHint(); |
| 4262 | } |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | void SurfaceFlinger::sendNotifyExpectedPresentHint(PhysicalDisplayId displayId) { |
| 4266 | if (auto itr = mNotifyExpectedPresentMap.find(displayId); |
| 4267 | itr == mNotifyExpectedPresentMap.end() || |
| 4268 | itr->second.hintStatus != NotifyExpectedPresentHintStatus::ScheduleOnPresent) { |
| 4269 | return; |
| 4270 | } |
| 4271 | scheduleNotifyExpectedPresentHint(displayId); |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 4272 | } |
| 4273 | |
Ady Abraham | 14beed7 | 2024-05-15 17:16:45 -0700 | [diff] [blame] | 4274 | void SurfaceFlinger::onCommitNotComposited(PhysicalDisplayId pacesetterDisplayId) { |
| 4275 | if (FlagManager::getInstance().commit_not_composited()) { |
| 4276 | mFrameTimeline->onCommitNotComposited(); |
| 4277 | } |
| 4278 | } |
| 4279 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 4280 | void SurfaceFlinger::initScheduler(const sp<const DisplayDevice>& display) { |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 4281 | using namespace scheduler; |
| 4282 | |
Dominik Laskowski | 6839927 | 2022-09-15 07:54:46 -0700 | [diff] [blame] | 4283 | LOG_ALWAYS_FATAL_IF(mScheduler); |
| 4284 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 4285 | const auto activeMode = display->refreshRateSelector().getActiveMode(); |
| 4286 | const Fps activeRefreshRate = activeMode.fps; |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 4287 | |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 4288 | FeatureFlags features; |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 4289 | |
Ady Abraham | 3f84c50 | 2023-11-30 18:18:06 -0800 | [diff] [blame] | 4290 | const auto defaultContentDetectionValue = |
| 4291 | FlagManager::getInstance().enable_fro_dependent_features() && |
| 4292 | sysprop::enable_frame_rate_override(true); |
| 4293 | if (sysprop::use_content_detection_for_refresh_rate(defaultContentDetectionValue)) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 4294 | features |= Feature::kContentDetection; |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 4295 | if (FlagManager::getInstance().enable_small_area_detection()) { |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 4296 | features |= Feature::kSmallDirtyContentDetection; |
| 4297 | } |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 4298 | } |
| 4299 | if (base::GetBoolProperty("debug.sf.show_predicted_vsync"s, false)) { |
| 4300 | features |= Feature::kTracePredictedVsync; |
| 4301 | } |
| 4302 | if (!base::GetBoolProperty("debug.sf.vsync_reactor_ignore_present_fences"s, false) && |
Dominik Laskowski | e62606d | 2024-02-22 11:42:13 -0500 | [diff] [blame] | 4303 | mHasReliablePresentFences) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 4304 | features |= Feature::kPresentFences; |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 4305 | } |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 4306 | if (display->refreshRateSelector().kernelIdleTimerController()) { |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 4307 | features |= Feature::kKernelIdleTimer; |
| 4308 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 4309 | if (mBackpressureGpuComposition) { |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 4310 | features |= Feature::kBackpressureGpuComposition; |
| 4311 | } |
Leon Scroggins III | 0bd0d4c | 2022-12-08 13:20:45 -0500 | [diff] [blame] | 4312 | if (getHwComposer().getComposer()->isSupported( |
| 4313 | Hwc2::Composer::OptionalFeature::ExpectedPresentTime)) { |
| 4314 | features |= Feature::kExpectedPresentTime; |
| 4315 | } |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 4316 | |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 4317 | mScheduler = std::make_unique<Scheduler>(static_cast<ICompositor&>(*this), |
| 4318 | static_cast<ISchedulerCallback&>(*this), features, |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 4319 | getFactory(), activeRefreshRate, *mTimeStats); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 4320 | |
| 4321 | // The pacesetter must be registered before EventThread creation below. |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 4322 | mScheduler->registerDisplay(display->getPhysicalId(), display->holdRefreshRateSelector(), |
| 4323 | mActiveDisplayId); |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 4324 | if (FlagManager::getInstance().vrr_config()) { |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 4325 | mScheduler->setRenderRate(display->getPhysicalId(), activeMode.fps, |
| 4326 | /*applyImmediately*/ true); |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 4327 | } |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 4328 | |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 4329 | const auto configs = mScheduler->getVsyncConfiguration().getCurrentConfigs(); |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 4330 | |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 4331 | mScheduler->createEventThread(scheduler::Cycle::Render, mFrameTimeline->getTokenManager(), |
| 4332 | /* workDuration */ configs.late.appWorkDuration, |
| 4333 | /* readyDuration */ configs.late.sfWorkDuration); |
| 4334 | mScheduler->createEventThread(scheduler::Cycle::LastComposite, |
| 4335 | mFrameTimeline->getTokenManager(), |
| 4336 | /* workDuration */ activeRefreshRate.getPeriod(), |
| 4337 | /* readyDuration */ configs.late.sfWorkDuration); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 4338 | |
Dominik Laskowski | 091129a | 2024-02-21 14:26:03 -0500 | [diff] [blame] | 4339 | // Dispatch after EventThread creation, since registerDisplay above skipped dispatch. |
| 4340 | mScheduler->dispatchHotplug(display->getPhysicalId(), scheduler::Scheduler::Hotplug::Connected); |
| 4341 | |
Leon Scroggins III | a7be94e | 2024-01-23 12:24:30 -0500 | [diff] [blame] | 4342 | mScheduler->initVsync(*mFrameTimeline->getTokenManager(), configs.late.sfWorkDuration); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 4343 | |
| 4344 | mRegionSamplingThread = |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 4345 | sp<RegionSamplingThread>::make(*this, |
| 4346 | RegionSamplingThread::EnvironmentTimingTunables()); |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 4347 | mFpsReporter = sp<FpsReporter>::make(*mFrameTimeline); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 4348 | |
| 4349 | // Timer callbacks may fire, so do this last. |
| 4350 | mScheduler->startTimers(); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 4351 | } |
| 4352 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4353 | void SurfaceFlinger::doCommitTransactions() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4354 | SFTRACE_CALL(); |
Lloyd Pique | 58e24a3 | 2019-08-01 15:50:14 -0700 | [diff] [blame] | 4355 | mDrawingState = mCurrentState; |
Lloyd Pique | 58e24a3 | 2019-08-01 15:50:14 -0700 | [diff] [blame] | 4356 | mCurrentState.colorMatrixChanged = false; |
Lloyd Pique | 58e24a3 | 2019-08-01 15:50:14 -0700 | [diff] [blame] | 4357 | } |
| 4358 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4359 | void SurfaceFlinger::invalidateLayerStack(const ui::LayerFilter& layerFilter, const Region& dirty) { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 4360 | for (const auto& [token, displayDevice] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 4361 | auto display = displayDevice->getCompositionDisplay(); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4362 | if (display->includesLayer(layerFilter)) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 4363 | display->editState().dirtyRegion.orSelf(dirty); |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 4364 | } |
| 4365 | } |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 4366 | } |
| 4367 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 4368 | status_t SurfaceFlinger::addClientLayer(LayerCreationArgs& args, const sp<IBinder>& handle, |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 4369 | const sp<Layer>& layer, const wp<Layer>& parent, |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 4370 | uint32_t* outTransformHint) { |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 4371 | if (mNumLayers >= MAX_LAYERS) { |
qinyige1 | 0e011d0 | 2024-05-23 15:59:08 +0800 | [diff] [blame] | 4372 | static std::atomic<nsecs_t> lasttime{0}; |
| 4373 | nsecs_t now = systemTime(); |
| 4374 | if (lasttime != 0 && ns2s(now - lasttime.load()) < 10) { |
| 4375 | ALOGE("AddClientLayer already dumped 10s before"); |
| 4376 | return NO_MEMORY; |
| 4377 | } else { |
| 4378 | lasttime = now; |
| 4379 | } |
| 4380 | |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 4381 | ALOGE("AddClientLayer failed, mNumLayers (%zu) >= MAX_LAYERS (%zu)", mNumLayers.load(), |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 4382 | MAX_LAYERS); |
qinyige1 | 0e011d0 | 2024-05-23 15:59:08 +0800 | [diff] [blame] | 4383 | static_cast<void>(mScheduler->schedule([&]() FTL_FAKE_GUARD(kMainThreadContext) { |
| 4384 | ALOGE("Dumping on-screen layers."); |
| 4385 | mLayerHierarchyBuilder.dumpLayerSample(mLayerHierarchyBuilder.getHierarchy()); |
| 4386 | ALOGE("Dumping off-screen layers."); |
| 4387 | mLayerHierarchyBuilder.dumpLayerSample(mLayerHierarchyBuilder.getOffscreenHierarchy()); |
Robert Carr | 26b98f4 | 2022-04-11 15:54:31 -0700 | [diff] [blame] | 4388 | })); |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 4389 | return NO_MEMORY; |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 4390 | } |
| 4391 | |
Vishnu Nair | 86e1190 | 2022-09-06 16:03:27 +0000 | [diff] [blame] | 4392 | if (outTransformHint) { |
| 4393 | *outTransformHint = mActiveDisplayTransformHint; |
| 4394 | } |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 4395 | args.parentId = LayerHandle::getLayerId(args.parentHandle.promote()); |
| 4396 | args.layerIdToMirror = LayerHandle::getLayerId(args.mirrorLayerHandle.promote()); |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 4397 | { |
| 4398 | std::scoped_lock<std::mutex> lock(mCreatedLayersLock); |
Vishnu Nair | a12c5c7 | 2024-08-28 15:42:17 -0700 | [diff] [blame] | 4399 | mCreatedLayers.emplace_back(layer); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4400 | mNewLayers.emplace_back(std::make_unique<frontend::RequestedLayerState>(args)); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 4401 | args.mirrorLayerHandle.clear(); |
| 4402 | args.parentHandle.clear(); |
| 4403 | mNewLayerArgs.emplace_back(std::move(args)); |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 4404 | } |
arthurhung | dba591c | 2021-02-08 17:28:49 +0800 | [diff] [blame] | 4405 | |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 4406 | setTransactionFlags(eTransactionNeeded); |
| 4407 | return NO_ERROR; |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 4408 | } |
| 4409 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4410 | uint32_t SurfaceFlinger::getTransactionFlags() const { |
Lloyd Pique | f1c675b | 2018-09-12 20:45:39 -0700 | [diff] [blame] | 4411 | return mTransactionFlags; |
Mathias Agopian | dea20b1 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 4412 | } |
| 4413 | |
Dominik Laskowski | 9e168db | 2021-05-27 16:05:12 -0700 | [diff] [blame] | 4414 | uint32_t SurfaceFlinger::clearTransactionFlags(uint32_t mask) { |
Vishnu Nair | e4f00c5 | 2023-01-31 20:20:18 -0800 | [diff] [blame] | 4415 | uint32_t transactionFlags = mTransactionFlags.fetch_and(~mask); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4416 | SFTRACE_INT("mTransactionFlags", transactionFlags); |
Vishnu Nair | e4f00c5 | 2023-01-31 20:20:18 -0800 | [diff] [blame] | 4417 | return transactionFlags & mask; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4418 | } |
| 4419 | |
Dominik Laskowski | 94000c4 | 2022-03-17 12:55:14 -0700 | [diff] [blame] | 4420 | void SurfaceFlinger::setTransactionFlags(uint32_t mask, TransactionSchedule schedule, |
Dominik Laskowski | 1f6fc70 | 2022-03-21 08:34:50 -0700 | [diff] [blame] | 4421 | const sp<IBinder>& applyToken, FrameHint frameHint) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 4422 | mScheduler->modulateVsync({}, &VsyncModulator::setTransactionSchedule, schedule, applyToken); |
Vishnu Nair | e4f00c5 | 2023-01-31 20:20:18 -0800 | [diff] [blame] | 4423 | uint32_t transactionFlags = mTransactionFlags.fetch_or(mask); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4424 | SFTRACE_INT("mTransactionFlags", transactionFlags); |
Dominik Laskowski | 94000c4 | 2022-03-17 12:55:14 -0700 | [diff] [blame] | 4425 | |
Vishnu Nair | e4f00c5 | 2023-01-31 20:20:18 -0800 | [diff] [blame] | 4426 | if (const bool scheduled = transactionFlags & mask; !scheduled) { |
Dominik Laskowski | 1f6fc70 | 2022-03-21 08:34:50 -0700 | [diff] [blame] | 4427 | scheduleCommit(frameHint); |
Ady Abraham | 27bcb55 | 2023-01-21 04:34:22 +0000 | [diff] [blame] | 4428 | } else if (frameHint == FrameHint::kActive) { |
| 4429 | // Even if the next frame is already scheduled, we should reset the idle timer |
| 4430 | // as a new activity just happened. |
| 4431 | mScheduler->resetIdleTimer(); |
Dominik Laskowski | 94000c4 | 2022-03-17 12:55:14 -0700 | [diff] [blame] | 4432 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4433 | } |
| 4434 | |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4435 | TransactionHandler::TransactionReadiness SurfaceFlinger::transactionReadyTimelineCheck( |
| 4436 | const TransactionHandler::TransactionFlushState& flushState) { |
Dominik Laskowski | e85d7fa | 2023-05-15 10:50:22 -0400 | [diff] [blame] | 4437 | const auto& transaction = *flushState.transaction; |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 4438 | |
| 4439 | const TimePoint desiredPresentTime = TimePoint::fromNs(transaction.desiredPresentTime); |
Dominik Laskowski | ec0eac2 | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 4440 | const TimePoint expectedPresentTime = mScheduler->expectedPresentTimeForPacesetter(); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 4441 | |
| 4442 | using TransactionReadiness = TransactionHandler::TransactionReadiness; |
| 4443 | |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4444 | // Do not present if the desiredPresentTime has not passed unless it is more than |
| 4445 | // one second in the future. We ignore timestamps more than 1 second in the future |
| 4446 | // for stability reasons. |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 4447 | if (!transaction.isAutoTimestamp && desiredPresentTime >= expectedPresentTime && |
| 4448 | desiredPresentTime < expectedPresentTime + 1s) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4449 | SFTRACE_FORMAT("not current desiredPresentTime: %" PRId64 " expectedPresentTime: %" PRId64, |
| 4450 | desiredPresentTime, expectedPresentTime); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4451 | return TransactionReadiness::NotReady; |
Ady Abraham | e1bfaac | 2022-02-22 21:32:08 -0800 | [diff] [blame] | 4452 | } |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4453 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 4454 | const auto vsyncId = VsyncId{transaction.frameTimelineInfo.vsyncId}; |
| 4455 | |
| 4456 | // Transactions with VsyncId are already throttled by the vsyncId (i.e. Choreographer issued |
| 4457 | // the vsyncId according to the frame rate override cadence) so we shouldn't throttle again |
| 4458 | // when applying the transaction. Otherwise we might throttle older transactions |
| 4459 | // incorrectly as the frame rate of SF changed before it drained the older transactions. |
| 4460 | if (ftl::to_underlying(vsyncId) == FrameTimelineInfo::INVALID_VSYNC_ID && |
| 4461 | !mScheduler->isVsyncValid(expectedPresentTime, transaction.originUid)) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4462 | SFTRACE_FORMAT("!isVsyncValid expectedPresentTime: %" PRId64 " uid: %d", |
| 4463 | expectedPresentTime, transaction.originUid); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4464 | return TransactionReadiness::NotReady; |
| 4465 | } |
| 4466 | |
| 4467 | // If the client didn't specify desiredPresentTime, use the vsyncId to determine the |
| 4468 | // expected present time of this transaction. |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 4469 | if (transaction.isAutoTimestamp && frameIsEarly(expectedPresentTime, vsyncId)) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4470 | SFTRACE_FORMAT("frameIsEarly vsyncId: %" PRId64 " expectedPresentTime: %" PRId64, |
| 4471 | transaction.frameTimelineInfo.vsyncId, expectedPresentTime); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4472 | return TransactionReadiness::NotReady; |
| 4473 | } |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 4474 | |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4475 | return TransactionReadiness::Ready; |
| 4476 | } |
| 4477 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4478 | TransactionHandler::TransactionReadiness SurfaceFlinger::transactionReadyBufferCheck( |
| 4479 | const TransactionHandler::TransactionFlushState& flushState) { |
| 4480 | using TransactionReadiness = TransactionHandler::TransactionReadiness; |
| 4481 | auto ready = TransactionReadiness::Ready; |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4482 | flushState.transaction->traverseStatesWithBuffersWhileTrue( |
| 4483 | [&](const ResolvedComposerState& resolvedState) FTL_FAKE_GUARD( |
| 4484 | kMainThreadContext) -> bool { |
| 4485 | const frontend::RequestedLayerState* layer = |
| 4486 | mLayerLifecycleManager.getLayerFromId(resolvedState.layerId); |
| 4487 | const auto& transaction = *flushState.transaction; |
| 4488 | const auto& s = resolvedState.state; |
| 4489 | // check for barrier frames |
| 4490 | if (s.bufferData->hasBarrier) { |
| 4491 | // The current producerId is already a newer producer than the buffer that has a |
| 4492 | // barrier. This means the incoming buffer is older and we can release it here. |
| 4493 | // We don't wait on the barrier since we know that's stale information. |
| 4494 | if (layer->barrierProducerId > s.bufferData->producerId) { |
| 4495 | if (s.bufferData->releaseBufferListener) { |
| 4496 | uint32_t currentMaxAcquiredBufferCount = |
| 4497 | getMaxAcquiredBufferCountForCurrentRefreshRate( |
| 4498 | layer->ownerUid.val()); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4499 | SFTRACE_FORMAT_INSTANT("callReleaseBufferCallback %s - %" PRIu64, |
| 4500 | layer->name.c_str(), s.bufferData->frameNumber); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4501 | s.bufferData->releaseBufferListener |
| 4502 | ->onReleaseBuffer({resolvedState.externalTexture->getBuffer() |
| 4503 | ->getId(), |
| 4504 | s.bufferData->frameNumber}, |
| 4505 | s.bufferData->acquireFence |
| 4506 | ? s.bufferData->acquireFence |
| 4507 | : Fence::NO_FENCE, |
| 4508 | currentMaxAcquiredBufferCount); |
| 4509 | } |
| 4510 | |
| 4511 | // Delete the entire state at this point and not just release the buffer |
| 4512 | // because everything associated with the Layer in this Transaction is now |
| 4513 | // out of date. |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4514 | SFTRACE_FORMAT("DeleteStaleBuffer %s barrierProducerId:%d > %d", |
| 4515 | layer->name.c_str(), layer->barrierProducerId, |
| 4516 | s.bufferData->producerId); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4517 | return TraverseBuffersReturnValues::DELETE_AND_CONTINUE_TRAVERSAL; |
| 4518 | } |
| 4519 | |
| 4520 | if (layer->barrierFrameNumber < s.bufferData->barrierFrameNumber) { |
| 4521 | const bool willApplyBarrierFrame = |
| 4522 | flushState.bufferLayersReadyToPresent.contains(s.surface.get()) && |
| 4523 | ((flushState.bufferLayersReadyToPresent.get(s.surface.get()) >= |
| 4524 | s.bufferData->barrierFrameNumber)); |
| 4525 | if (!willApplyBarrierFrame) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4526 | SFTRACE_FORMAT("NotReadyBarrier %s barrierFrameNumber:%" PRId64 |
| 4527 | " > %" PRId64, |
| 4528 | layer->name.c_str(), layer->barrierFrameNumber, |
| 4529 | s.bufferData->barrierFrameNumber); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4530 | ready = TransactionReadiness::NotReadyBarrier; |
| 4531 | return TraverseBuffersReturnValues::STOP_TRAVERSAL; |
| 4532 | } |
| 4533 | } |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4536 | // If backpressure is enabled and we already have a buffer to commit, keep |
| 4537 | // the transaction in the queue. |
| 4538 | const bool hasPendingBuffer = |
| 4539 | flushState.bufferLayersReadyToPresent.contains(s.surface.get()); |
| 4540 | if (layer->backpressureEnabled() && hasPendingBuffer && |
| 4541 | transaction.isAutoTimestamp) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4542 | SFTRACE_FORMAT("hasPendingBuffer %s", layer->name.c_str()); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4543 | ready = TransactionReadiness::NotReady; |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4544 | return TraverseBuffersReturnValues::STOP_TRAVERSAL; |
| 4545 | } |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4546 | |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4547 | const bool acquireFenceAvailable = s.bufferData && |
| 4548 | s.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged) && |
| 4549 | s.bufferData->acquireFence; |
| 4550 | const bool fenceSignaled = !acquireFenceAvailable || |
| 4551 | s.bufferData->acquireFence->getStatus() != Fence::Status::Unsignaled; |
| 4552 | if (!fenceSignaled) { |
| 4553 | // check fence status |
| 4554 | const bool allowLatchUnsignaled = |
| 4555 | shouldLatchUnsignaled(s, transaction.states.size(), |
| 4556 | flushState.firstTransaction) && |
| 4557 | layer->isSimpleBufferUpdate(s); |
| 4558 | if (allowLatchUnsignaled) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4559 | SFTRACE_FORMAT("fence unsignaled try allowLatchUnsignaled %s", |
| 4560 | layer->name.c_str()); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4561 | ready = TransactionReadiness::NotReadyUnsignaled; |
| 4562 | } else { |
| 4563 | ready = TransactionReadiness::NotReady; |
| 4564 | auto& listener = s.bufferData->releaseBufferListener; |
| 4565 | if (listener && |
| 4566 | (flushState.queueProcessTime - transaction.postTime) > |
| 4567 | std::chrono::nanoseconds(4s).count()) { |
| 4568 | mTransactionHandler |
| 4569 | .onTransactionQueueStalled(transaction.id, |
| 4570 | {.pid = layer->ownerPid.val(), |
| 4571 | .layerId = layer->id, |
| 4572 | .layerName = layer->name, |
| 4573 | .bufferId = s.bufferData->getId(), |
| 4574 | .frameNumber = |
| 4575 | s.bufferData->frameNumber}); |
| 4576 | } |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4577 | SFTRACE_FORMAT("fence unsignaled %s", layer->name.c_str()); |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4578 | return TraverseBuffersReturnValues::STOP_TRAVERSAL; |
| 4579 | } |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4580 | } |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4581 | return TraverseBuffersReturnValues::CONTINUE_TRAVERSAL; |
| 4582 | }); |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4583 | return ready; |
| 4584 | } |
| 4585 | |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4586 | void SurfaceFlinger::addTransactionReadyFilters() { |
| 4587 | mTransactionHandler.addTransactionReadyFilter( |
| 4588 | std::bind(&SurfaceFlinger::transactionReadyTimelineCheck, this, std::placeholders::_1)); |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 4589 | mTransactionHandler.addTransactionReadyFilter( |
| 4590 | std::bind(&SurfaceFlinger::transactionReadyBufferCheck, this, std::placeholders::_1)); |
Ady Abraham | e1bfaac | 2022-02-22 21:32:08 -0800 | [diff] [blame] | 4591 | } |
| 4592 | |
Vishnu Nair | 42ad319 | 2023-01-24 19:17:59 -0800 | [diff] [blame] | 4593 | // For tests only |
Shuangxi Xiang | a97d59d | 2024-08-27 07:28:20 +0000 | [diff] [blame] | 4594 | bool SurfaceFlinger::flushTransactionQueues() { |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4595 | mTransactionHandler.collectTransactions(); |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4596 | std::vector<TransactionState> transactions = mTransactionHandler.flushTransactions(); |
Shuangxi Xiang | a97d59d | 2024-08-27 07:28:20 +0000 | [diff] [blame] | 4597 | return applyTransactions(transactions); |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
Shuangxi Xiang | a97d59d | 2024-08-27 07:28:20 +0000 | [diff] [blame] | 4600 | bool SurfaceFlinger::applyTransactions(std::vector<TransactionState>& transactions) { |
Dominik Laskowski | 1686374 | 2022-09-29 11:57:40 -0400 | [diff] [blame] | 4601 | Mutex::Autolock lock(mStateLock); |
Shuangxi Xiang | a97d59d | 2024-08-27 07:28:20 +0000 | [diff] [blame] | 4602 | return applyTransactionsLocked(transactions); |
Vishnu Nair | 42ad319 | 2023-01-24 19:17:59 -0800 | [diff] [blame] | 4603 | } |
| 4604 | |
Shuangxi Xiang | a97d59d | 2024-08-27 07:28:20 +0000 | [diff] [blame] | 4605 | bool SurfaceFlinger::applyTransactionsLocked(std::vector<TransactionState>& transactions) { |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 4606 | bool needsTraversal = false; |
| 4607 | // Now apply all transactions. |
Robert Carr | ff7663b | 2022-02-08 12:46:49 -0800 | [diff] [blame] | 4608 | for (auto& transaction : transactions) { |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 4609 | needsTraversal |= |
| 4610 | applyTransactionState(transaction.frameTimelineInfo, transaction.states, |
| 4611 | transaction.displays, transaction.flags, |
| 4612 | transaction.inputWindowCommands, |
| 4613 | transaction.desiredPresentTime, transaction.isAutoTimestamp, |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 4614 | std::move(transaction.uncacheBufferIds), transaction.postTime, |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 4615 | transaction.hasListenerCallbacks, |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 4616 | transaction.listenerCallbacks, transaction.originPid, |
| 4617 | transaction.originUid, transaction.id); |
ramindani | 4d48f90 | 2021-09-20 21:07:45 +0000 | [diff] [blame] | 4618 | } |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 4619 | return needsTraversal; |
Marissa Wall | e6e3c0d | 2019-03-29 10:28:30 -0700 | [diff] [blame] | 4620 | } |
| 4621 | |
| 4622 | bool SurfaceFlinger::transactionFlushNeeded() { |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4623 | return mTransactionHandler.hasPendingTransactions(); |
Marissa Wall | 713b63f | 2018-10-17 15:42:43 -0700 | [diff] [blame] | 4624 | } |
| 4625 | |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 4626 | bool SurfaceFlinger::frameIsEarly(TimePoint expectedPresentTime, VsyncId vsyncId) const { |
| 4627 | const auto prediction = |
Dominik Laskowski | 16a345b | 2023-04-27 17:07:33 -0400 | [diff] [blame] | 4628 | mFrameTimeline->getTokenManager()->getPredictionsForToken(ftl::to_underlying(vsyncId)); |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 4629 | if (!prediction) { |
Ady Abraham | 8db1010 | 2021-03-15 17:19:23 -0700 | [diff] [blame] | 4630 | return false; |
| 4631 | } |
| 4632 | |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 4633 | const auto predictedPresentTime = TimePoint::fromNs(prediction->presentTime); |
| 4634 | |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 4635 | if (std::chrono::abs(predictedPresentTime - expectedPresentTime) >= |
| 4636 | scheduler::VsyncConfig::kEarlyLatchMaxThreshold) { |
Ady Abraham | 8db1010 | 2021-03-15 17:19:23 -0700 | [diff] [blame] | 4637 | return false; |
| 4638 | } |
| 4639 | |
Ady Abraham | 3db8a3c | 2023-11-20 17:53:47 -0800 | [diff] [blame] | 4640 | const Duration earlyLatchVsyncThreshold = mScheduler->getVsyncSchedule()->minFramePeriod() / 2; |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 4641 | |
| 4642 | return predictedPresentTime >= expectedPresentTime && |
| 4643 | predictedPresentTime - expectedPresentTime >= earlyLatchVsyncThreshold; |
Ady Abraham | 8db1010 | 2021-03-15 17:19:23 -0700 | [diff] [blame] | 4644 | } |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 4645 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4646 | bool SurfaceFlinger::shouldLatchUnsignaled(const layer_state_t& state, size_t numStates, |
| 4647 | bool firstTransaction) const { |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 4648 | if (enableLatchUnsignaledConfig == LatchUnsignaledConfig::Disabled) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4649 | SFTRACE_FORMAT_INSTANT("%s: false (LatchUnsignaledConfig::Disabled)", __func__); |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 4650 | return false; |
| 4651 | } |
Ady Abraham | 8db1010 | 2021-03-15 17:19:23 -0700 | [diff] [blame] | 4652 | |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 4653 | // We only want to latch unsignaled when a single layer is updated in this |
| 4654 | // transaction (i.e. not a blast sync transaction). |
| 4655 | if (numStates != 1) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4656 | SFTRACE_FORMAT_INSTANT("%s: false (numStates=%zu)", __func__, numStates); |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 4657 | return false; |
| 4658 | } |
| 4659 | |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 4660 | if (enableLatchUnsignaledConfig == LatchUnsignaledConfig::AutoSingleLayer) { |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4661 | if (!firstTransaction) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4662 | SFTRACE_FORMAT_INSTANT("%s: false (LatchUnsignaledConfig::AutoSingleLayer; not first " |
| 4663 | "transaction)", |
| 4664 | __func__); |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 4665 | return false; |
| 4666 | } |
| 4667 | |
| 4668 | // We don't want to latch unsignaled if are in early / client composition |
| 4669 | // as it leads to jank due to RenderEngine waiting for unsignaled buffer |
| 4670 | // or window animations being slow. |
Dominik Laskowski | 14956dc | 2023-02-22 13:43:57 -0500 | [diff] [blame] | 4671 | if (mScheduler->vsyncModulator().isVsyncConfigEarly()) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4672 | SFTRACE_FORMAT_INSTANT("%s: false (LatchUnsignaledConfig::AutoSingleLayer; " |
| 4673 | "isVsyncConfigEarly)", |
| 4674 | __func__); |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 4675 | return false; |
| 4676 | } |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 4677 | } |
| 4678 | |
Vishnu Nair | 4d9cef9 | 2023-06-24 22:34:41 +0000 | [diff] [blame] | 4679 | return true; |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 4680 | } |
| 4681 | |
chaviw | 308ddba | 2020-08-11 16:23:51 -0700 | [diff] [blame] | 4682 | status_t SurfaceFlinger::setTransactionState( |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 4683 | const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& states, |
Patrick Williams | f65da8a | 2024-07-24 17:11:19 +0000 | [diff] [blame] | 4684 | Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken, |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 4685 | InputWindowCommands inputWindowCommands, int64_t desiredPresentTime, bool isAutoTimestamp, |
| 4686 | const std::vector<client_cache_t>& uncacheBuffers, bool hasListenerCallbacks, |
Pablo Gamito | 23780be | 2023-04-18 08:30:00 +0000 | [diff] [blame] | 4687 | const std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId, |
| 4688 | const std::vector<uint64_t>& mergedTransactionIds) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 4689 | SFTRACE_CALL(); |
Robert Carr | 14167e0 | 2019-02-13 13:50:55 -0800 | [diff] [blame] | 4690 | |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 4691 | IPCThreadState* ipc = IPCThreadState::self(); |
| 4692 | const int originPid = ipc->getCallingPid(); |
| 4693 | const int originUid = ipc->getCallingUid(); |
| 4694 | uint32_t permissions = LayerStatePermissions::getTransactionPermissions(originPid, originUid); |
Patrick Williams | 04e4176 | 2024-04-23 19:05:38 -0500 | [diff] [blame] | 4695 | for (auto& composerState : states) { |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 4696 | composerState.state.sanitize(permissions); |
Vishnu Nair | 24e3bba | 2021-02-23 14:19:36 -0800 | [diff] [blame] | 4697 | } |
| 4698 | |
Patrick Williams | f65da8a | 2024-07-24 17:11:19 +0000 | [diff] [blame] | 4699 | for (DisplayState& display : displays) { |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 4700 | display.sanitize(permissions); |
| 4701 | } |
| 4702 | |
| 4703 | if (!inputWindowCommands.empty() && |
| 4704 | (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) == 0) { |
| 4705 | ALOGE("Only privileged callers are allowed to send input commands."); |
| 4706 | inputWindowCommands.clear(); |
Leon Scroggins | 9a20f72 | 2021-12-28 14:43:12 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
Vishnu Nair | 884549f | 2022-12-01 17:20:59 -0800 | [diff] [blame] | 4709 | if (flags & (eEarlyWakeupStart | eEarlyWakeupEnd)) { |
| 4710 | const bool hasPermission = |
| 4711 | (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) || |
| 4712 | callingThreadHasPermission(sWakeupSurfaceFlinger); |
| 4713 | if (!hasPermission) { |
| 4714 | ALOGE("Caller needs permission android.permission.WAKEUP_SURFACE_FLINGER to use " |
| 4715 | "eEarlyWakeup[Start|End] flags"); |
| 4716 | flags &= ~(eEarlyWakeupStart | eEarlyWakeupEnd); |
| 4717 | } |
arthurhung | f1b7c7b | 2021-03-03 17:42:23 +0800 | [diff] [blame] | 4718 | } |
| 4719 | |
Vishnu Nair | 24e3bba | 2021-02-23 14:19:36 -0800 | [diff] [blame] | 4720 | const int64_t postTime = systemTime(); |
| 4721 | |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 4722 | std::vector<uint64_t> uncacheBufferIds; |
| 4723 | uncacheBufferIds.reserve(uncacheBuffers.size()); |
| 4724 | for (const auto& uncacheBuffer : uncacheBuffers) { |
| 4725 | sp<GraphicBuffer> buffer = ClientCache::getInstance().erase(uncacheBuffer); |
| 4726 | if (buffer != nullptr) { |
| 4727 | uncacheBufferIds.push_back(buffer->getId()); |
| 4728 | } |
| 4729 | } |
| 4730 | |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 4731 | std::vector<ResolvedComposerState> resolvedStates; |
| 4732 | resolvedStates.reserve(states.size()); |
| 4733 | for (auto& state : states) { |
| 4734 | resolvedStates.emplace_back(std::move(state)); |
| 4735 | auto& resolvedState = resolvedStates.back(); |
| 4736 | if (resolvedState.state.hasBufferChanges() && resolvedState.state.hasValidBuffer() && |
| 4737 | resolvedState.state.surface) { |
Vishnu Nair | c6086dd | 2022-11-04 04:39:35 +0000 | [diff] [blame] | 4738 | sp<Layer> layer = LayerHandle::getLayer(resolvedState.state.surface); |
| 4739 | std::string layerName = (layer) ? |
| 4740 | layer->getDebugName() : std::to_string(resolvedState.state.layerId); |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 4741 | resolvedState.externalTexture = |
| 4742 | getExternalTextureFromBufferData(*resolvedState.state.bufferData, |
Vishnu Nair | c6086dd | 2022-11-04 04:39:35 +0000 | [diff] [blame] | 4743 | layerName.c_str(), transactionId); |
Patrick Williams | 38d920e | 2023-08-31 17:31:18 -0500 | [diff] [blame] | 4744 | if (resolvedState.externalTexture) { |
| 4745 | resolvedState.state.bufferData->buffer = resolvedState.externalTexture->getBuffer(); |
| 4746 | } |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 4747 | mBufferCountTracker.increment(resolvedState.state.surface->localBinder()); |
| 4748 | } |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame] | 4749 | resolvedState.layerId = LayerHandle::getLayerId(resolvedState.state.surface); |
| 4750 | if (resolvedState.state.what & layer_state_t::eReparent) { |
| 4751 | resolvedState.parentId = |
| 4752 | getLayerIdFromSurfaceControl(resolvedState.state.parentSurfaceControlForChild); |
| 4753 | } |
| 4754 | if (resolvedState.state.what & layer_state_t::eRelativeLayerChanged) { |
| 4755 | resolvedState.relativeParentId = |
| 4756 | getLayerIdFromSurfaceControl(resolvedState.state.relativeLayerSurfaceControl); |
| 4757 | } |
| 4758 | if (resolvedState.state.what & layer_state_t::eInputInfoChanged) { |
| 4759 | wp<IBinder>& touchableRegionCropHandle = |
| 4760 | resolvedState.state.windowInfoHandle->editInfo()->touchableRegionCropHandle; |
| 4761 | resolvedState.touchCropId = |
| 4762 | LayerHandle::getLayerId(touchableRegionCropHandle.promote()); |
| 4763 | } |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 4764 | } |
| 4765 | |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 4766 | TransactionState state{frameTimelineInfo, |
| 4767 | resolvedStates, |
| 4768 | displays, |
| 4769 | flags, |
| 4770 | applyToken, |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 4771 | std::move(inputWindowCommands), |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 4772 | desiredPresentTime, |
| 4773 | isAutoTimestamp, |
| 4774 | std::move(uncacheBufferIds), |
| 4775 | postTime, |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 4776 | hasListenerCallbacks, |
| 4777 | listenerCallbacks, |
| 4778 | originPid, |
| 4779 | originUid, |
Pablo Gamito | 23780be | 2023-04-18 08:30:00 +0000 | [diff] [blame] | 4780 | transactionId, |
| 4781 | mergedTransactionIds}; |
Vishnu Nair | 83df034 | 2021-03-30 11:50:44 -0700 | [diff] [blame] | 4782 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 4783 | if (mTransactionTracing) { |
| 4784 | mTransactionTracing->addQueuedTransaction(state); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 4785 | } |
Vishnu Nair | 59f6d2d | 2022-10-05 16:59:56 -0700 | [diff] [blame] | 4786 | |
| 4787 | const auto schedule = [](uint32_t flags) { |
| 4788 | if (flags & eEarlyWakeupEnd) return TransactionSchedule::EarlyEnd; |
| 4789 | if (flags & eEarlyWakeupStart) return TransactionSchedule::EarlyStart; |
| 4790 | return TransactionSchedule::Late; |
| 4791 | }(state.flags); |
| 4792 | |
| 4793 | const auto frameHint = state.isFrameActive() ? FrameHint::kActive : FrameHint::kNone; |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 4794 | { |
| 4795 | // Transactions are added via a lockless queue and does not need to be added from the main |
| 4796 | // thread. |
| 4797 | ftl::FakeGuard guard(kMainThreadContext); |
| 4798 | mTransactionHandler.queueTransaction(std::move(state)); |
| 4799 | } |
| 4800 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 4801 | for (const auto& [displayId, data] : mNotifyExpectedPresentMap) { |
| 4802 | if (data.hintStatus.load() == NotifyExpectedPresentHintStatus::ScheduleOnTx) { |
| 4803 | scheduleNotifyExpectedPresentHint(displayId, VsyncId{frameTimelineInfo.vsyncId}); |
| 4804 | } |
| 4805 | } |
Vishnu Nair | e4f00c5 | 2023-01-31 20:20:18 -0800 | [diff] [blame] | 4806 | setTransactionFlags(eTransactionFlushNeeded, schedule, applyToken, frameHint); |
Zhuoyao Zhang | 3d3540d | 2021-01-14 05:14:54 +0000 | [diff] [blame] | 4807 | return NO_ERROR; |
| 4808 | } |
Marissa Wall | 713b63f | 2018-10-17 15:42:43 -0700 | [diff] [blame] | 4809 | |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 4810 | bool SurfaceFlinger::applyTransactionState(const FrameTimelineInfo& frameTimelineInfo, |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 4811 | std::vector<ResolvedComposerState>& states, |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 4812 | Vector<DisplayState>& displays, uint32_t flags, |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 4813 | const InputWindowCommands& inputWindowCommands, |
| 4814 | const int64_t desiredPresentTime, bool isAutoTimestamp, |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 4815 | const std::vector<uint64_t>& uncacheBufferIds, |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 4816 | const int64_t postTime, bool hasListenerCallbacks, |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 4817 | const std::vector<ListenerCallbacks>& listenerCallbacks, |
| 4818 | int originPid, int originUid, uint64_t transactionId) { |
Zhuoyao Zhang | 3d3540d | 2021-01-14 05:14:54 +0000 | [diff] [blame] | 4819 | uint32_t transactionFlags = 0; |
Jamie Gennis | b8d69a5 | 2011-10-10 15:48:06 -0700 | [diff] [blame] | 4820 | |
Jiakai Zhang | a5505cb | 2021-11-09 11:46:30 +0000 | [diff] [blame] | 4821 | // start and end registration for listeners w/ no surface so they can get their callback. Note |
| 4822 | // that listeners with SurfaceControls will start registration during setClientStateLocked |
| 4823 | // below. |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 4824 | for (const auto& listener : listenerCallbacks) { |
Jiakai Zhang | a5505cb | 2021-11-09 11:46:30 +0000 | [diff] [blame] | 4825 | mTransactionCallbackInvoker.addEmptyTransaction(listener); |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 4826 | } |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 4827 | uint32_t clientStateFlags = 0; |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 4828 | for (auto& resolvedState : states) { |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 4829 | clientStateFlags |= |
| 4830 | updateLayerCallbacksAndStats(frameTimelineInfo, resolvedState, desiredPresentTime, |
| 4831 | isAutoTimestamp, postTime, transactionId); |
Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 4832 | } |
| 4833 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 4834 | transactionFlags |= clientStateFlags; |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 4835 | transactionFlags |= addInputWindowCommands(inputWindowCommands); |
chaviw | 273171b | 2018-12-26 11:46:30 -0800 | [diff] [blame] | 4836 | |
Patrick Williams | 6c6dd3b | 2023-02-13 22:53:06 +0000 | [diff] [blame] | 4837 | for (uint64_t uncacheBufferId : uncacheBufferIds) { |
| 4838 | mBufferIdsToUncache.push_back(uncacheBufferId); |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame] | 4839 | } |
| 4840 | |
Jorim Jaggi | bdcf09c | 2017-08-08 15:22:08 +0200 | [diff] [blame] | 4841 | // If a synchronous transaction is explicitly requested without any changes, force a transaction |
| 4842 | // anyway. This can be used as a flush mechanism for previous async transactions. |
| 4843 | // Empty animation transaction can be used to simulate back-pressure, so also force a |
| 4844 | // transaction for empty animation transactions. |
Vishnu Nair | 1523dad | 2022-09-29 16:05:18 -0700 | [diff] [blame] | 4845 | if (transactionFlags == 0 && (flags & eAnimation)) { |
Robert Carr | 2a7dbb4 | 2016-05-24 11:41:28 -0700 | [diff] [blame] | 4846 | transactionFlags = eTransactionNeeded; |
| 4847 | } |
| 4848 | |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 4849 | bool needsTraversal = false; |
Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 4850 | if (transactionFlags) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4851 | // We are on the main thread, we are about to perform a traversal. Clear the traversal bit |
| 4852 | // 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] | 4853 | if (transactionFlags & eTraversalNeeded) { |
| 4854 | transactionFlags = transactionFlags & (~eTraversalNeeded); |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 4855 | needsTraversal = true; |
Arthur Hung | 1bedccb | 2020-09-24 10:09:25 +0000 | [diff] [blame] | 4856 | } |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 4857 | if (transactionFlags) { |
| 4858 | setTransactionFlags(transactionFlags); |
Arthur Hung | 1bedccb | 2020-09-24 10:09:25 +0000 | [diff] [blame] | 4859 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4860 | } |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 4861 | |
| 4862 | return needsTraversal; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4863 | } |
| 4864 | |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 4865 | bool SurfaceFlinger::applyAndCommitDisplayTransactionStatesLocked( |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4866 | std::vector<TransactionState>& transactions) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4867 | bool needsTraversal = false; |
| 4868 | uint32_t transactionFlags = 0; |
| 4869 | for (auto& transaction : transactions) { |
| 4870 | for (DisplayState& display : transaction.displays) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4871 | transactionFlags |= setDisplayStateLocked(display); |
| 4872 | } |
| 4873 | } |
| 4874 | |
| 4875 | if (transactionFlags) { |
| 4876 | // We are on the main thread, we are about to perform a traversal. Clear the traversal bit |
| 4877 | // so we don't have to wake up again next frame to perform an unnecessary traversal. |
| 4878 | if (transactionFlags & eTraversalNeeded) { |
| 4879 | transactionFlags = transactionFlags & (~eTraversalNeeded); |
| 4880 | needsTraversal = true; |
| 4881 | } |
| 4882 | if (transactionFlags) { |
| 4883 | setTransactionFlags(transactionFlags); |
| 4884 | } |
| 4885 | } |
| 4886 | |
| 4887 | mFrontEndDisplayInfosChanged = mTransactionFlags & eDisplayTransactionNeeded; |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 4888 | if (mFrontEndDisplayInfosChanged) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4889 | processDisplayChangesLocked(); |
| 4890 | mFrontEndDisplayInfos.clear(); |
| 4891 | for (const auto& [_, display] : mDisplays) { |
| 4892 | mFrontEndDisplayInfos.try_emplace(display->getLayerStack(), display->getFrontEndInfo()); |
| 4893 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 4894 | needsTraversal = true; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | return needsTraversal; |
| 4898 | } |
| 4899 | |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 4900 | uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s) { |
| 4901 | const ssize_t index = mCurrentState.displays.indexOfKey(s.token); |
| 4902 | if (index < 0) return 0; |
Jesse Hall | 9a14392 | 2012-10-04 16:29:19 -0700 | [diff] [blame] | 4903 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 4904 | uint32_t flags = 0; |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 4905 | DisplayDeviceState& state = mCurrentState.displays.editValueAt(index); |
| 4906 | |
| 4907 | const uint32_t what = s.what; |
| 4908 | if (what & DisplayState::eSurfaceChanged) { |
| 4909 | if (IInterface::asBinder(state.surface) != IInterface::asBinder(s.surface)) { |
| 4910 | state.surface = s.surface; |
| 4911 | flags |= eDisplayTransactionNeeded; |
Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 4912 | } |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 4913 | } |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 4914 | if (what & DisplayState::eLayerStackChanged) { |
| 4915 | if (state.layerStack != s.layerStack) { |
| 4916 | state.layerStack = s.layerStack; |
| 4917 | flags |= eDisplayTransactionNeeded; |
| 4918 | } |
| 4919 | } |
Evan Rosky | 2239b37 | 2021-05-20 13:43:47 -0700 | [diff] [blame] | 4920 | if (what & DisplayState::eFlagsChanged) { |
| 4921 | if (state.flags != s.flags) { |
| 4922 | state.flags = s.flags; |
| 4923 | flags |= eDisplayTransactionNeeded; |
| 4924 | } |
| 4925 | } |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 4926 | if (what & DisplayState::eDisplayProjectionChanged) { |
| 4927 | if (state.orientation != s.orientation) { |
| 4928 | state.orientation = s.orientation; |
| 4929 | flags |= eDisplayTransactionNeeded; |
| 4930 | } |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 4931 | if (state.orientedDisplaySpaceRect != s.orientedDisplaySpaceRect) { |
| 4932 | state.orientedDisplaySpaceRect = s.orientedDisplaySpaceRect; |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 4933 | flags |= eDisplayTransactionNeeded; |
| 4934 | } |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 4935 | if (state.layerStackSpaceRect != s.layerStackSpaceRect) { |
| 4936 | state.layerStackSpaceRect = s.layerStackSpaceRect; |
Dominik Laskowski | f07b85b | 2018-06-11 12:49:15 -0700 | [diff] [blame] | 4937 | flags |= eDisplayTransactionNeeded; |
| 4938 | } |
| 4939 | } |
| 4940 | if (what & DisplayState::eDisplaySizeChanged) { |
| 4941 | if (state.width != s.width) { |
| 4942 | state.width = s.width; |
| 4943 | flags |= eDisplayTransactionNeeded; |
| 4944 | } |
| 4945 | if (state.height != s.height) { |
| 4946 | state.height = s.height; |
| 4947 | flags |= eDisplayTransactionNeeded; |
| 4948 | } |
| 4949 | } |
| 4950 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 4951 | return flags; |
| 4952 | } |
| 4953 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 4954 | bool SurfaceFlinger::callingThreadHasUnscopedSurfaceFlingerAccess(bool usePermissionCache) { |
Robert Carr | d4ae7f3 | 2018-06-07 16:10:57 -0700 | [diff] [blame] | 4955 | IPCThreadState* ipc = IPCThreadState::self(); |
| 4956 | const int pid = ipc->getCallingPid(); |
| 4957 | const int uid = ipc->getCallingUid(); |
Robert Carr | d4ae7f3 | 2018-06-07 16:10:57 -0700 | [diff] [blame] | 4958 | if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) && |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 4959 | (usePermissionCache ? !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid) |
| 4960 | : !checkPermission(sAccessSurfaceFlinger, pid, uid))) { |
Robert Carr | d4ae7f3 | 2018-06-07 16:10:57 -0700 | [diff] [blame] | 4961 | return false; |
| 4962 | } |
| 4963 | return true; |
| 4964 | } |
| 4965 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4966 | uint32_t SurfaceFlinger::updateLayerCallbacksAndStats(const FrameTimelineInfo& frameTimelineInfo, |
| 4967 | ResolvedComposerState& composerState, |
| 4968 | int64_t desiredPresentTime, |
| 4969 | bool isAutoTimestamp, int64_t postTime, |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4970 | uint64_t transactionId) { |
| 4971 | layer_state_t& s = composerState.state; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 4972 | |
| 4973 | std::vector<ListenerCallbacks> filteredListeners; |
| 4974 | for (auto& listener : s.listeners) { |
| 4975 | // Starts a registration but separates the callback ids according to callback type. This |
| 4976 | // allows the callback invoker to send on latch callbacks earlier. |
| 4977 | // note that startRegistration will not re-register if the listener has |
| 4978 | // already be registered for a prior surface control |
| 4979 | |
| 4980 | ListenerCallbacks onCommitCallbacks = listener.filter(CallbackId::Type::ON_COMMIT); |
| 4981 | if (!onCommitCallbacks.callbackIds.empty()) { |
| 4982 | filteredListeners.push_back(onCommitCallbacks); |
| 4983 | } |
| 4984 | |
| 4985 | ListenerCallbacks onCompleteCallbacks = listener.filter(CallbackId::Type::ON_COMPLETE); |
| 4986 | if (!onCompleteCallbacks.callbackIds.empty()) { |
| 4987 | filteredListeners.push_back(onCompleteCallbacks); |
| 4988 | } |
| 4989 | } |
| 4990 | |
| 4991 | const uint64_t what = s.what; |
| 4992 | uint32_t flags = 0; |
| 4993 | sp<Layer> layer = nullptr; |
| 4994 | if (s.surface) { |
| 4995 | layer = LayerHandle::getLayer(s.surface); |
| 4996 | } else { |
| 4997 | // The client may provide us a null handle. Treat it as if the layer was removed. |
| 4998 | ALOGW("Attempt to set client state with a null layer handle"); |
| 4999 | } |
| 5000 | if (layer == nullptr) { |
| 5001 | for (auto& [listener, callbackIds] : s.listeners) { |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 5002 | mTransactionCallbackInvoker.addCallbackHandle( |
| 5003 | sp<CallbackHandle>::make(listener, callbackIds, s.surface)); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5004 | } |
| 5005 | return 0; |
| 5006 | } |
| 5007 | if (what & layer_state_t::eProducerDisconnect) { |
| 5008 | layer->onDisconnect(); |
| 5009 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5010 | |
| 5011 | std::vector<sp<CallbackHandle>> callbackHandles; |
| 5012 | if ((what & layer_state_t::eHasListenerCallbacksChanged) && (!filteredListeners.empty())) { |
| 5013 | for (auto& [listener, callbackIds] : filteredListeners) { |
| 5014 | callbackHandles.emplace_back( |
| 5015 | sp<CallbackHandle>::make(listener, callbackIds, s.surface)); |
| 5016 | } |
| 5017 | } |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 5018 | |
| 5019 | frontend::LayerSnapshot* snapshot = nullptr; |
| 5020 | gui::GameMode gameMode = gui::GameMode::Unsupported; |
| 5021 | if (what & (layer_state_t::eSidebandStreamChanged | layer_state_t::eBufferChanged) || |
| 5022 | frameTimelineInfo.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) { |
| 5023 | snapshot = mLayerSnapshotBuilder.getSnapshot(layer->sequence); |
| 5024 | if (snapshot) { |
| 5025 | gameMode = snapshot->gameMode; |
| 5026 | } |
| 5027 | } |
| 5028 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 5029 | // TODO(b/238781169) remove after screenshot refactor, currently screenshots |
| 5030 | // requires to read drawing state from binder thread. So we need to fix that |
| 5031 | // before removing this. |
Vishnu Nair | aeaf9d7 | 2023-09-12 15:47:41 -0700 | [diff] [blame] | 5032 | if (what & layer_state_t::eBufferTransformChanged) { |
| 5033 | if (layer->setTransform(s.bufferTransform)) flags |= eTraversalNeeded; |
| 5034 | } |
| 5035 | if (what & layer_state_t::eTransformToDisplayInverseChanged) { |
| 5036 | if (layer->setTransformToDisplayInverse(s.transformToDisplayInverse)) |
| 5037 | flags |= eTraversalNeeded; |
| 5038 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 5039 | if (what & layer_state_t::eCropChanged) { |
| 5040 | if (layer->setCrop(s.crop)) flags |= eTraversalNeeded; |
| 5041 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5042 | if (what & layer_state_t::eSidebandStreamChanged) { |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 5043 | if (layer->setSidebandStream(s.sidebandStream, frameTimelineInfo, postTime, gameMode)) |
Dorin Drimus | e5374e5 | 2023-08-02 17:52:43 +0000 | [diff] [blame] | 5044 | flags |= eTraversalNeeded; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5045 | } |
Vishnu Nair | 3996ee3 | 2023-08-14 04:32:31 +0000 | [diff] [blame] | 5046 | if (what & layer_state_t::eDataspaceChanged) { |
| 5047 | if (layer->setDataspace(s.dataspace)) flags |= eTraversalNeeded; |
| 5048 | } |
Vishnu Nair | c5e3416 | 2023-08-22 13:21:20 -0700 | [diff] [blame] | 5049 | if (what & layer_state_t::eExtendedRangeBrightnessChanged) { |
| 5050 | if (layer->setExtendedRangeBrightness(s.currentHdrSdrRatio, s.desiredHdrSdrRatio)) { |
| 5051 | flags |= eTraversalNeeded; |
| 5052 | } |
| 5053 | } |
Alec Mouri | 1b0d4e1 | 2024-02-12 22:27:19 +0000 | [diff] [blame] | 5054 | if (what & layer_state_t::eDesiredHdrHeadroomChanged) { |
| 5055 | if (layer->setDesiredHdrHeadroom(s.desiredHdrSdrRatio)) { |
| 5056 | flags |= eTraversalNeeded; |
| 5057 | } |
| 5058 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5059 | if (what & layer_state_t::eBufferChanged) { |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 5060 | std::optional<ui::Transform::RotationFlags> transformHint = std::nullopt; |
Vishnu Nair | b76d99a | 2023-03-19 18:22:31 -0700 | [diff] [blame] | 5061 | if (snapshot) { |
| 5062 | transformHint = snapshot->transformHint; |
| 5063 | } |
| 5064 | layer->setTransformHint(transformHint); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5065 | if (layer->setBuffer(composerState.externalTexture, *s.bufferData, postTime, |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 5066 | desiredPresentTime, isAutoTimestamp, frameTimelineInfo, gameMode)) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5067 | flags |= eTraversalNeeded; |
| 5068 | } |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 5069 | mLayersWithQueuedFrames.emplace(layer, gameMode); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5070 | } else if (frameTimelineInfo.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) { |
Vishnu Nair | 39a74a9 | 2024-07-29 19:01:50 +0000 | [diff] [blame] | 5071 | layer->setFrameTimelineVsyncForBufferlessTransaction(frameTimelineInfo, postTime, gameMode); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 5074 | if ((what & layer_state_t::eBufferChanged) == 0) { |
| 5075 | layer->setDesiredPresentTime(desiredPresentTime, isAutoTimestamp); |
| 5076 | } |
| 5077 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5078 | if (what & layer_state_t::eTrustedPresentationInfoChanged) { |
Chavi Weingarten | 328a831 | 2023-01-26 21:17:52 +0000 | [diff] [blame] | 5079 | if (layer->setTrustedPresentationInfo(s.trustedPresentationThresholds, |
| 5080 | s.trustedPresentationListener)) { |
| 5081 | flags |= eTraversalNeeded; |
| 5082 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Patrick Williams | 7c9fa27 | 2024-08-30 12:38:43 +0000 | [diff] [blame] | 5085 | if (what & layer_state_t::eBufferReleaseChannelChanged) { |
| 5086 | layer->setBufferReleaseChannel(s.bufferReleaseChannel); |
| 5087 | } |
| 5088 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 5089 | const auto& requestedLayerState = mLayerLifecycleManager.getLayerFromId(layer->getSequence()); |
Vishnu Nair | 7ee4f46 | 2023-04-19 09:54:09 -0700 | [diff] [blame] | 5090 | bool willPresentCurrentTransaction = requestedLayerState && |
| 5091 | (requestedLayerState->hasReadyFrame() || |
| 5092 | requestedLayerState->willReleaseBufferOnLatch()); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5093 | if (layer->setTransactionCompletedListeners(callbackHandles, willPresentCurrentTransaction)) |
| 5094 | flags |= eTraversalNeeded; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 5095 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5096 | return flags; |
| 5097 | } |
| 5098 | |
chaviw | 273171b | 2018-12-26 11:46:30 -0800 | [diff] [blame] | 5099 | uint32_t SurfaceFlinger::addInputWindowCommands(const InputWindowCommands& inputWindowCommands) { |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 5100 | bool hasChanges = mInputWindowCommands.merge(inputWindowCommands); |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5101 | return hasChanges ? eTraversalNeeded : 0; |
chaviw | 273171b | 2018-12-26 11:46:30 -0800 | [diff] [blame] | 5102 | } |
| 5103 | |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 5104 | status_t SurfaceFlinger::mirrorLayer(const LayerCreationArgs& args, |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5105 | const sp<IBinder>& mirrorFromHandle, |
| 5106 | gui::CreateSurfaceResult& outResult) { |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5107 | if (!mirrorFromHandle) { |
| 5108 | return NAME_NOT_FOUND; |
| 5109 | } |
| 5110 | |
| 5111 | sp<Layer> mirrorLayer; |
| 5112 | sp<Layer> mirrorFrom; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 5113 | LayerCreationArgs mirrorArgs = LayerCreationArgs::fromOtherArgs(args); |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5114 | { |
| 5115 | Mutex::Autolock _l(mStateLock); |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 5116 | mirrorFrom = LayerHandle::getLayer(mirrorFromHandle); |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5117 | if (!mirrorFrom) { |
| 5118 | return NAME_NOT_FOUND; |
| 5119 | } |
Vishnu Nair | b21272e | 2022-07-01 22:47:33 +0000 | [diff] [blame] | 5120 | mirrorArgs.flags |= ISurfaceComposerClient::eNoColorFill; |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 5121 | mirrorArgs.mirrorLayerHandle = mirrorFromHandle; |
| 5122 | mirrorArgs.addToRoot = false; |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5123 | status_t result = createEffectLayer(mirrorArgs, &outResult.handle, &mirrorLayer); |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5124 | if (result != NO_ERROR) { |
| 5125 | return result; |
| 5126 | } |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5127 | } |
| 5128 | |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5129 | outResult.layerId = mirrorLayer->sequence; |
| 5130 | outResult.layerName = String16(mirrorLayer->getDebugName()); |
Vishnu Nair | 8319e36 | 2022-10-24 18:59:20 +0000 | [diff] [blame] | 5131 | return addClientLayer(mirrorArgs, outResult.handle, mirrorLayer /* layer */, |
| 5132 | nullptr /* parent */, nullptr /* outTransformHint */); |
chaviw | fe94a22 | 2019-08-21 13:52:59 -0700 | [diff] [blame] | 5133 | } |
| 5134 | |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 5135 | status_t SurfaceFlinger::mirrorDisplay(DisplayId displayId, const LayerCreationArgs& args, |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5136 | gui::CreateSurfaceResult& outResult) { |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 5137 | IPCThreadState* ipc = IPCThreadState::self(); |
| 5138 | const int uid = ipc->getCallingUid(); |
| 5139 | if (uid != AID_ROOT && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != AID_SHELL) { |
| 5140 | ALOGE("Permission denied when trying to mirror display"); |
| 5141 | return PERMISSION_DENIED; |
| 5142 | } |
| 5143 | |
| 5144 | ui::LayerStack layerStack; |
| 5145 | sp<Layer> rootMirrorLayer; |
| 5146 | status_t result = 0; |
| 5147 | |
| 5148 | { |
| 5149 | Mutex::Autolock lock(mStateLock); |
| 5150 | |
| 5151 | const auto display = getDisplayDeviceLocked(displayId); |
| 5152 | if (!display) { |
| 5153 | return NAME_NOT_FOUND; |
| 5154 | } |
| 5155 | |
| 5156 | layerStack = display->getLayerStack(); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 5157 | LayerCreationArgs mirrorArgs = LayerCreationArgs::fromOtherArgs(args); |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 5158 | mirrorArgs.flags |= ISurfaceComposerClient::eNoColorFill; |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 5159 | mirrorArgs.addToRoot = true; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5160 | mirrorArgs.layerStackToMirror = layerStack; |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5161 | result = createEffectLayer(mirrorArgs, &outResult.handle, &rootMirrorLayer); |
| 5162 | outResult.layerId = rootMirrorLayer->sequence; |
| 5163 | outResult.layerName = String16(rootMirrorLayer->getDebugName()); |
Vishnu Nair | 8319e36 | 2022-10-24 18:59:20 +0000 | [diff] [blame] | 5164 | result |= addClientLayer(mirrorArgs, outResult.handle, rootMirrorLayer /* layer */, |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 5165 | nullptr /* parent */, nullptr /* outTransformHint */); |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | if (result != NO_ERROR) { |
| 5169 | return result; |
| 5170 | } |
| 5171 | |
Chavi Weingarten | 7043a7d | 2022-07-19 23:40:35 +0000 | [diff] [blame] | 5172 | setTransactionFlags(eTransactionFlushNeeded); |
| 5173 | return NO_ERROR; |
| 5174 | } |
| 5175 | |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 5176 | status_t SurfaceFlinger::createLayer(LayerCreationArgs& args, gui::CreateSurfaceResult& outResult) { |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 5177 | status_t result = NO_ERROR; |
| 5178 | |
| 5179 | sp<Layer> layer; |
| 5180 | |
Vishnu Nair | 7fb9e5a | 2021-11-08 12:44:05 -0800 | [diff] [blame] | 5181 | switch (args.flags & ISurfaceComposerClient::eFXSurfaceMask) { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 5182 | case ISurfaceComposerClient::eFXSurfaceBufferQueue: |
Vishnu Nair | bc4ee5c | 2022-08-16 03:19:37 +0000 | [diff] [blame] | 5183 | case ISurfaceComposerClient::eFXSurfaceContainer: |
| 5184 | case ISurfaceComposerClient::eFXSurfaceBufferState: |
| 5185 | args.flags |= ISurfaceComposerClient::eNoColorFill; |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5186 | [[fallthrough]]; |
Vishnu Nair | bc4ee5c | 2022-08-16 03:19:37 +0000 | [diff] [blame] | 5187 | case ISurfaceComposerClient::eFXSurfaceEffect: { |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5188 | result = createBufferStateLayer(args, &outResult.handle, &layer); |
Vishnu Nair | 8eda69e | 2021-02-26 10:42:10 -0800 | [diff] [blame] | 5189 | std::atomic<int32_t>* pendingBufferCounter = layer->getPendingBufferCounter(); |
| 5190 | if (pendingBufferCounter) { |
| 5191 | std::string counterName = layer->getPendingBufferCounterName(); |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5192 | mBufferCountTracker.add(outResult.handle->localBinder(), counterName, |
Vishnu Nair | 8eda69e | 2021-02-26 10:42:10 -0800 | [diff] [blame] | 5193 | pendingBufferCounter); |
| 5194 | } |
| 5195 | } break; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 5196 | default: |
| 5197 | result = BAD_VALUE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5198 | break; |
| 5199 | } |
| 5200 | |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 5201 | if (result != NO_ERROR) { |
| 5202 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5203 | } |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 5204 | |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 5205 | args.addToRoot = args.addToRoot && callingThreadHasUnscopedSurfaceFlingerAccess(); |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 5206 | // We can safely promote the parent layer in binder thread because we have a strong reference |
| 5207 | // to the layer's handle inside this scope. |
| 5208 | sp<Layer> parent = LayerHandle::getLayer(args.parentHandle.promote()); |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 5209 | if (args.parentHandle != nullptr && parent == nullptr) { |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 5210 | ALOGE("Invalid parent handle %p", args.parentHandle.promote().get()); |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 5211 | args.addToRoot = false; |
Garfield Tan | dbc93d7 | 2021-10-26 18:28:57 -0700 | [diff] [blame] | 5212 | } |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 5213 | |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5214 | uint32_t outTransformHint; |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame] | 5215 | result = addClientLayer(args, outResult.handle, layer, parent, &outTransformHint); |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 5216 | if (result != NO_ERROR) { |
| 5217 | return result; |
| 5218 | } |
| 5219 | |
Patrick Williams | a361de6 | 2022-10-06 20:34:10 +0000 | [diff] [blame] | 5220 | outResult.transformHint = static_cast<int32_t>(outTransformHint); |
| 5221 | outResult.layerId = layer->sequence; |
| 5222 | outResult.layerName = String16(layer->getDebugName()); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 5223 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5224 | } |
| 5225 | |
Vishnu Nair | 7fb9e5a | 2021-11-08 12:44:05 -0800 | [diff] [blame] | 5226 | status_t SurfaceFlinger::createBufferStateLayer(LayerCreationArgs& args, sp<IBinder>* handle, |
Vishnu Nair | 6213bd9 | 2020-05-08 17:42:25 -0700 | [diff] [blame] | 5227 | sp<Layer>* outLayer) { |
Vishnu Nair | 7fb9e5a | 2021-11-08 12:44:05 -0800 | [diff] [blame] | 5228 | *outLayer = getFactory().createBufferStateLayer(args); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 5229 | *handle = (*outLayer)->getHandle(); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 5230 | return NO_ERROR; |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 5231 | } |
| 5232 | |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 5233 | status_t SurfaceFlinger::createEffectLayer(const LayerCreationArgs& args, sp<IBinder>* handle, |
Vishnu Nair | 7fb9e5a | 2021-11-08 12:44:05 -0800 | [diff] [blame] | 5234 | sp<Layer>* outLayer) { |
| 5235 | *outLayer = getFactory().createEffectLayer(args); |
| 5236 | *handle = (*outLayer)->getHandle(); |
| 5237 | return NO_ERROR; |
| 5238 | } |
| 5239 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 5240 | void SurfaceFlinger::onHandleDestroyed(BBinder* handle, sp<Layer>& layer, uint32_t layerId) { |
| 5241 | { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5242 | // Used to remove stalled transactions which uses an internal lock. |
| 5243 | ftl::FakeGuard guard(kMainThreadContext); |
| 5244 | mTransactionHandler.onLayerDestroyed(layerId); |
| 5245 | } |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 5246 | JankTracker::flushJankData(layerId); |
| 5247 | |
Vishnu Nair | a12c5c7 | 2024-08-28 15:42:17 -0700 | [diff] [blame] | 5248 | std::scoped_lock<std::mutex> lock(mCreatedLayersLock); |
| 5249 | mDestroyedHandles.emplace_back(layerId, layer->getDebugName()); |
| 5250 | |
| 5251 | Mutex::Autolock stateLock(mStateLock); |
Vishnu Nair | 787aa78 | 2023-03-17 13:46:46 -0700 | [diff] [blame] | 5252 | layer->onHandleDestroyed(); |
Vishnu Nair | f909665 | 2021-07-20 18:49:42 -0700 | [diff] [blame] | 5253 | mBufferCountTracker.remove(handle); |
Robert Carr | 695d528 | 2018-12-18 15:27:58 -0800 | [diff] [blame] | 5254 | layer.clear(); |
Vishnu Nair | a12c5c7 | 2024-08-28 15:42:17 -0700 | [diff] [blame] | 5255 | setTransactionFlags(eTransactionFlushNeeded | eTransactionNeeded); |
Rob Carr | 4bba370 | 2018-10-08 21:53:30 +0000 | [diff] [blame] | 5256 | } |
| 5257 | |
Dominik Laskowski | b5f40e5 | 2022-09-29 13:25:05 -0400 | [diff] [blame] | 5258 | void SurfaceFlinger::initializeDisplays() { |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 5259 | TransactionState state; |
| 5260 | state.inputWindowCommands = mInputWindowCommands; |
Dominik Laskowski | 1686374 | 2022-09-29 11:57:40 -0400 | [diff] [blame] | 5261 | const nsecs_t now = systemTime(); |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 5262 | state.desiredPresentTime = now; |
| 5263 | state.postTime = now; |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 5264 | state.originPid = mPid; |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 5265 | state.originUid = static_cast<int>(getuid()); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 5266 | const uint64_t transactionId = (static_cast<uint64_t>(mPid) << 32) | mUniqueTransactionId++; |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 5267 | state.id = transactionId; |
| 5268 | |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 5269 | auto layerStack = ui::DEFAULT_LAYER_STACK.id; |
| 5270 | for (const auto& [id, display] : FTL_FAKE_GUARD(mStateLock, mPhysicalDisplays)) { |
| 5271 | state.displays.push(DisplayState(display.token(), ui::LayerStack::fromValue(layerStack++))); |
| 5272 | } |
Arthur Hung | 5814427 | 2021-01-16 03:43:53 +0000 | [diff] [blame] | 5273 | |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 5274 | std::vector<TransactionState> transactions; |
| 5275 | transactions.emplace_back(state); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 5276 | |
Melody Hsu | ce0fe52 | 2024-05-03 01:54:56 +0000 | [diff] [blame] | 5277 | { |
Vishnu Nair | 878911f | 2024-03-27 22:20:24 -0700 | [diff] [blame] | 5278 | Mutex::Autolock lock(mStateLock); |
| 5279 | applyAndCommitDisplayTransactionStatesLocked(transactions); |
Vishnu Nair | a7caedc | 2023-02-26 03:24:58 +0000 | [diff] [blame] | 5280 | } |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 5281 | |
Dominik Laskowski | 1686374 | 2022-09-29 11:57:40 -0400 | [diff] [blame] | 5282 | { |
| 5283 | ftl::FakeGuard guard(mStateLock); |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 5284 | |
| 5285 | // In case of a restart, ensure all displays are off. |
| 5286 | for (const auto& [id, display] : mPhysicalDisplays) { |
| 5287 | setPowerModeInternal(getDisplayDeviceLocked(id), hal::PowerMode::OFF); |
| 5288 | } |
| 5289 | |
| 5290 | // Power on all displays. The primary display is first, so becomes the active display. Also, |
| 5291 | // the DisplayCapability set of a display is populated on its first powering on. Do this now |
| 5292 | // before responding to any Binder query from DisplayManager about display capabilities. |
Robert Horvath | dce110a | 2022-09-16 11:08:47 +0200 | [diff] [blame] | 5293 | // Additionally, do not turn on displays if the boot should be quiescent. |
| 5294 | if (!mSkipPowerOnForQuiescent) { |
| 5295 | for (const auto& [id, display] : mPhysicalDisplays) { |
| 5296 | setPowerModeInternal(getDisplayDeviceLocked(id), hal::PowerMode::ON); |
| 5297 | } |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 5298 | } |
Dominik Laskowski | 1686374 | 2022-09-29 11:57:40 -0400 | [diff] [blame] | 5299 | } |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 5300 | } |
| 5301 | |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 5302 | void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& display, hal::PowerMode mode) { |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 5303 | if (display->isVirtual()) { |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 5304 | // TODO(b/241285876): This code path should not be reachable, so enforce this at compile |
| 5305 | // time. |
Dominik Laskowski | 6c7b36e | 2022-03-03 08:27:58 -0800 | [diff] [blame] | 5306 | ALOGE("%s: Invalid operation on virtual display", __func__); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 5307 | return; |
| 5308 | } |
| 5309 | |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 5310 | const auto displayId = display->getPhysicalId(); |
| 5311 | 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] | 5312 | |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 5313 | const auto currentMode = display->getPowerMode(); |
| 5314 | if (currentMode == mode) { |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 5315 | return; |
| 5316 | } |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 5317 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 5318 | const bool isInternalDisplay = mPhysicalDisplays.get(displayId) |
| 5319 | .transform(&PhysicalDisplay::isInternal) |
| 5320 | .value_or(false); |
| 5321 | |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 5322 | const auto activeDisplay = getDisplayDeviceLocked(mActiveDisplayId); |
Dominik Laskowski | c132a15 | 2022-11-30 15:44:52 -0500 | [diff] [blame] | 5323 | |
Dominik Laskowski | 31afdda | 2022-12-08 19:15:15 -0500 | [diff] [blame] | 5324 | ALOGW_IF(display != activeDisplay && isInternalDisplay && activeDisplay && |
| 5325 | activeDisplay->isPoweredOn(), |
Dominik Laskowski | c132a15 | 2022-11-30 15:44:52 -0500 | [diff] [blame] | 5326 | "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] | 5327 | |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 5328 | display->setPowerMode(mode); |
| 5329 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 5330 | const auto activeMode = display->refreshRateSelector().getActiveMode().modePtr; |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 5331 | if (currentMode == hal::PowerMode::OFF) { |
Ady Abraham | 4f960d1 | 2021-10-13 16:59:49 -0700 | [diff] [blame] | 5332 | // Turn on the display |
Dominik Laskowski | 31afdda | 2022-12-08 19:15:15 -0500 | [diff] [blame] | 5333 | |
Dominik Laskowski | a39e32f | 2023-02-08 16:40:13 -0500 | [diff] [blame] | 5334 | // Activate the display (which involves a modeset to the active mode) when the inner or |
| 5335 | // outer display of a foldable is powered on. This condition relies on the above |
| 5336 | // DisplayDevice::setPowerMode. If `display` and `activeDisplay` are the same display, |
| 5337 | // then the `activeDisplay->isPoweredOn()` below is true, such that the display is not |
| 5338 | // activated every time it is powered on. |
Dominik Laskowski | 31afdda | 2022-12-08 19:15:15 -0500 | [diff] [blame] | 5339 | // |
| 5340 | // TODO(b/255635821): Remove the concept of active display. |
Dominik Laskowski | a39e32f | 2023-02-08 16:40:13 -0500 | [diff] [blame] | 5341 | if (isInternalDisplay && (!activeDisplay || !activeDisplay->isPoweredOn())) { |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 5342 | onActiveDisplayChangedLocked(activeDisplay.get(), *display); |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 5343 | } |
Dominik Laskowski | 31afdda | 2022-12-08 19:15:15 -0500 | [diff] [blame] | 5344 | |
Dominik Laskowski | cf0bb43 | 2023-05-09 11:49:16 -0400 | [diff] [blame] | 5345 | if (displayId == mActiveDisplayId) { |
| 5346 | // TODO(b/281692563): Merge the syscalls. For now, keep uclamp in a separate syscall and |
| 5347 | // set it before SCHED_FIFO due to b/190237315. |
| 5348 | if (setSchedAttr(true) != NO_ERROR) { |
| 5349 | ALOGW("Failed to set uclamp.min after powering on active display: %s", |
| 5350 | strerror(errno)); |
| 5351 | } |
| 5352 | if (setSchedFifo(true) != NO_ERROR) { |
| 5353 | ALOGW("Failed to set SCHED_FIFO after powering on active display: %s", |
| 5354 | strerror(errno)); |
| 5355 | } |
Wei Wang | 23aa5a6 | 2021-06-04 15:56:57 -0700 | [diff] [blame] | 5356 | } |
Dominik Laskowski | cf0bb43 | 2023-05-09 11:49:16 -0400 | [diff] [blame] | 5357 | |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 5358 | getHwComposer().setPowerMode(displayId, mode); |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 5359 | if (mode != hal::PowerMode::DOZE_SUSPEND && |
| 5360 | (displayId == mActiveDisplayId || FlagManager::getInstance().multithreaded_present())) { |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 5361 | const bool enable = |
| 5362 | mScheduler->getVsyncSchedule(displayId)->getPendingHardwareVsyncState(); |
| 5363 | requestHardwareVsync(displayId, enable); |
| 5364 | |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 5365 | if (displayId == mActiveDisplayId) { |
| 5366 | mScheduler->enableSyntheticVsync(false); |
| 5367 | } |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 5368 | |
| 5369 | constexpr bool kAllowToEnable = true; |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 5370 | mScheduler->resyncToHardwareVsync(displayId, kAllowToEnable, activeMode.get()); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 5371 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5372 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 5373 | mVisibleRegionsDirty = true; |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 5374 | scheduleComposite(FrameHint::kActive); |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 5375 | } else if (mode == hal::PowerMode::OFF) { |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 5376 | const bool currentModeNotDozeSuspend = (currentMode != hal::PowerMode::DOZE_SUSPEND); |
Tim Murray | f9d4e44 | 2016-08-02 15:43:59 -0700 | [diff] [blame] | 5377 | // Turn off the display |
Dominik Laskowski | cf0bb43 | 2023-05-09 11:49:16 -0400 | [diff] [blame] | 5378 | if (displayId == mActiveDisplayId) { |
Dominik Laskowski | 3de6727 | 2023-08-09 13:15:31 -0400 | [diff] [blame] | 5379 | if (const auto display = getActivatableDisplay()) { |
| 5380 | onActiveDisplayChangedLocked(activeDisplay.get(), *display); |
| 5381 | } else { |
| 5382 | if (setSchedFifo(false) != NO_ERROR) { |
| 5383 | ALOGW("Failed to set SCHED_OTHER after powering off active display: %s", |
| 5384 | strerror(errno)); |
| 5385 | } |
| 5386 | if (setSchedAttr(false) != NO_ERROR) { |
| 5387 | ALOGW("Failed set uclamp.min after powering off active display: %s", |
| 5388 | strerror(errno)); |
| 5389 | } |
Dominik Laskowski | cf0bb43 | 2023-05-09 11:49:16 -0400 | [diff] [blame] | 5390 | |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 5391 | if (currentModeNotDozeSuspend) { |
| 5392 | if (!FlagManager::getInstance().multithreaded_present()) { |
| 5393 | mScheduler->disableHardwareVsync(displayId, true); |
| 5394 | } |
Dominik Laskowski | 3de6727 | 2023-08-09 13:15:31 -0400 | [diff] [blame] | 5395 | mScheduler->enableSyntheticVsync(); |
| 5396 | } |
Dominik Laskowski | cf0bb43 | 2023-05-09 11:49:16 -0400 | [diff] [blame] | 5397 | } |
Mathias Agopian | cde87a3 | 2012-09-13 14:09:01 -0700 | [diff] [blame] | 5398 | } |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 5399 | if (currentModeNotDozeSuspend && FlagManager::getInstance().multithreaded_present()) { |
| 5400 | constexpr bool kDisallow = true; |
| 5401 | mScheduler->disableHardwareVsync(displayId, kDisallow); |
| 5402 | } |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 5403 | |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 5404 | // We must disable VSYNC *before* turning off the display. The call to |
| 5405 | // disableHardwareVsync, above, schedules a task to turn it off after |
| 5406 | // this method returns. But by that point, the display is OFF, so the |
| 5407 | // call just updates the pending state, without actually disabling |
| 5408 | // VSYNC. |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 5409 | requestHardwareVsync(displayId, false); |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 5410 | getHwComposer().setPowerMode(displayId, mode); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 5411 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 5412 | mVisibleRegionsDirty = true; |
| 5413 | // from this point on, SF will stop drawing on this display |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 5414 | } else if (mode == hal::PowerMode::DOZE || mode == hal::PowerMode::ON) { |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 5415 | // Update display while dozing |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 5416 | getHwComposer().setPowerMode(displayId, mode); |
Dominik Laskowski | a42d539 | 2022-09-29 14:26:53 -0400 | [diff] [blame] | 5417 | if (currentMode == hal::PowerMode::DOZE_SUSPEND && |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 5418 | (displayId == mActiveDisplayId || FlagManager::getInstance().multithreaded_present())) { |
| 5419 | if (displayId == mActiveDisplayId) { |
| 5420 | ALOGI("Force repainting for DOZE_SUSPEND -> DOZE or ON."); |
| 5421 | mVisibleRegionsDirty = true; |
| 5422 | scheduleRepaint(); |
| 5423 | mScheduler->enableSyntheticVsync(false); |
| 5424 | } |
| 5425 | constexpr bool kAllowToEnable = true; |
| 5426 | mScheduler->resyncToHardwareVsync(displayId, kAllowToEnable, activeMode.get()); |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 5427 | } |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 5428 | } else if (mode == hal::PowerMode::DOZE_SUSPEND) { |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 5429 | // Leave display going to doze |
Leon Scroggins III | 1fc9e7f | 2023-04-28 17:11:28 -0400 | [diff] [blame] | 5430 | if (displayId == mActiveDisplayId || FlagManager::getInstance().multithreaded_present()) { |
| 5431 | constexpr bool kDisallow = true; |
| 5432 | mScheduler->disableHardwareVsync(displayId, kDisallow); |
| 5433 | } |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 5434 | if (displayId == mActiveDisplayId) { |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 5435 | mScheduler->enableSyntheticVsync(); |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 5436 | } |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 5437 | getHwComposer().setPowerMode(displayId, mode); |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 5438 | } else { |
Matthew Bouyack | 38d4961 | 2017-05-12 12:49:32 -0700 | [diff] [blame] | 5439 | ALOGE("Attempting to set unknown power mode: %d\n", mode); |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 5440 | getHwComposer().setPowerMode(displayId, mode); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 5441 | } |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 5442 | |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 5443 | if (displayId == mActiveDisplayId) { |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 5444 | mTimeStats->setPowerMode(mode); |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 5445 | mScheduler->setActiveDisplayPowerModeForRefreshRateStats(mode); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 5446 | } |
| 5447 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 5448 | mScheduler->setDisplayPowerMode(displayId, mode); |
| 5449 | |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 5450 | 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] | 5451 | } |
| 5452 | |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 5453 | void SurfaceFlinger::setPowerMode(const sp<IBinder>& displayToken, int mode) { |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 5454 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD( |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 5455 | kMainThreadContext) { |
Robert Horvath | dce110a | 2022-09-16 11:08:47 +0200 | [diff] [blame] | 5456 | mSkipPowerOnForQuiescent = false; |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 5457 | const auto display = getDisplayDeviceLocked(displayToken); |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 5458 | if (!display) { |
| 5459 | ALOGE("Attempt to set power mode %d for invalid display token %p", mode, |
| 5460 | displayToken.get()); |
| 5461 | } else if (display->isVirtual()) { |
| 5462 | ALOGW("Attempt to set power mode %d for virtual display", mode); |
| 5463 | } else { |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 5464 | setPowerModeInternal(display, static_cast<hal::PowerMode>(mode)); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 5465 | } |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 5466 | }); |
| 5467 | |
| 5468 | future.wait(); |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 5469 | } |
| 5470 | |
Dominik Laskowski | 0a1435d | 2020-04-21 00:27:31 -0700 | [diff] [blame] | 5471 | status_t SurfaceFlinger::doDump(int fd, const DumpArgs& args, bool asProto) { |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5472 | std::string result; |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 5473 | |
Mathias Agopian | bd11533 | 2013-04-18 16:41:04 -0700 | [diff] [blame] | 5474 | IPCThreadState* ipc = IPCThreadState::self(); |
| 5475 | const int pid = ipc->getCallingPid(); |
| 5476 | const int uid = ipc->getCallingUid(); |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 5477 | |
Mathias Agopian | bd11533 | 2013-04-18 16:41:04 -0700 | [diff] [blame] | 5478 | if ((uid != AID_SHELL) && |
| 5479 | !PermissionCache::checkPermission(sDump, pid, uid)) { |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5480 | StringAppendF(&result, "Permission Denial: can't dump SurfaceFlinger from pid=%d, uid=%d\n", |
| 5481 | pid, uid); |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 5482 | write(fd, result.c_str(), result.size()); |
| 5483 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5484 | } |
Ady Abraham | 5ffd98a | 2023-08-24 17:37:01 -0700 | [diff] [blame] | 5485 | |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 5486 | if (asProto && args.empty()) { |
| 5487 | perfetto::protos::LayersTraceFileProto traceFileProto = |
| 5488 | mLayerTracing.createTraceFileProto(); |
| 5489 | perfetto::protos::LayersSnapshotProto* layersTrace = traceFileProto.add_entry(); |
| 5490 | perfetto::protos::LayersProto layersProto = dumpProtoFromMainThread(); |
| 5491 | layersTrace->mutable_layers()->Swap(&layersProto); |
| 5492 | auto displayProtos = dumpDisplayProto(); |
| 5493 | layersTrace->mutable_displays()->Swap(&displayProtos); |
| 5494 | result.append(traceFileProto.SerializeAsString()); |
| 5495 | write(fd, result.c_str(), result.size()); |
| 5496 | return NO_ERROR; |
| 5497 | } |
| 5498 | |
| 5499 | static const std::unordered_map<std::string, Dumper> dumpers = { |
| 5500 | {"--comp-displays"s, dumper(&SurfaceFlinger::dumpCompositionDisplays)}, |
| 5501 | {"--display-id"s, dumper(&SurfaceFlinger::dumpDisplayIdentificationData)}, |
| 5502 | {"--displays"s, dumper(&SurfaceFlinger::dumpDisplays)}, |
| 5503 | {"--edid"s, argsDumper(&SurfaceFlinger::dumpRawDisplayIdentificationData)}, |
| 5504 | {"--events"s, dumper(&SurfaceFlinger::dumpEvents)}, |
| 5505 | {"--frametimeline"s, argsDumper(&SurfaceFlinger::dumpFrameTimeline)}, |
| 5506 | {"--frontend"s, mainThreadDumper(&SurfaceFlinger::dumpFrontEnd)}, |
| 5507 | {"--hdrinfo"s, dumper(&SurfaceFlinger::dumpHdrInfo)}, |
| 5508 | {"--hwclayers"s, mainThreadDumper(&SurfaceFlinger::dumpHwcLayersMinidump)}, |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 5509 | {"--latency"s, argsMainThreadDumper(&SurfaceFlinger::dumpStats)}, |
| 5510 | {"--latency-clear"s, argsMainThreadDumper(&SurfaceFlinger::clearStats)}, |
| 5511 | {"--list"s, mainThreadDumper(&SurfaceFlinger::listLayers)}, |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 5512 | {"--planner"s, argsDumper(&SurfaceFlinger::dumpPlannerInfo)}, |
| 5513 | {"--scheduler"s, dumper(&SurfaceFlinger::dumpScheduler)}, |
| 5514 | {"--timestats"s, protoDumper(&SurfaceFlinger::dumpTimeStats)}, |
| 5515 | {"--vsync"s, dumper(&SurfaceFlinger::dumpVsync)}, |
| 5516 | {"--wide-color"s, dumper(&SurfaceFlinger::dumpWideColorInfo)}, |
| 5517 | }; |
| 5518 | |
| 5519 | const auto flag = args.empty() ? ""s : std::string(String8(args[0])); |
| 5520 | if (const auto it = dumpers.find(flag); it != dumpers.end()) { |
| 5521 | (it->second)(args, asProto, result); |
| 5522 | write(fd, result.c_str(), result.size()); |
| 5523 | return NO_ERROR; |
| 5524 | } |
| 5525 | |
Vishnu Nair | 11db986 | 2024-06-21 22:06:14 +0000 | [diff] [blame] | 5526 | // Collect debug data from main thread |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 5527 | std::string compositionLayers; |
| 5528 | mScheduler |
| 5529 | ->schedule([&]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD(kMainThreadContext) { |
| 5530 | dumpVisibleFrontEnd(compositionLayers); |
| 5531 | }) |
| 5532 | .get(); |
Vishnu Nair | 11db986 | 2024-06-21 22:06:14 +0000 | [diff] [blame] | 5533 | // get window info listener data without the state lock |
| 5534 | auto windowInfosDebug = mWindowInfosListenerInvoker->getDebugInfo(); |
| 5535 | compositionLayers.append("Window Infos:\n"); |
| 5536 | StringAppendF(&compositionLayers, " max send vsync id: %" PRId64 "\n", |
| 5537 | ftl::to_underlying(windowInfosDebug.maxSendDelayVsyncId)); |
| 5538 | StringAppendF(&compositionLayers, " max send delay (ns): %" PRId64 " ns\n", |
| 5539 | windowInfosDebug.maxSendDelayDuration); |
| 5540 | StringAppendF(&compositionLayers, " unsent messages: %zu\n", |
| 5541 | windowInfosDebug.pendingMessageCount); |
| 5542 | compositionLayers.append("\n"); |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 5543 | dumpAll(args, compositionLayers, result); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5544 | write(fd, result.c_str(), result.size()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5545 | return NO_ERROR; |
| 5546 | } |
| 5547 | |
Nataniel Borges | 8e7dc72 | 2019-02-28 15:10:28 -0800 | [diff] [blame] | 5548 | status_t SurfaceFlinger::dumpCritical(int fd, const DumpArgs&, bool asProto) { |
Nataniel Borges | 8e7dc72 | 2019-02-28 15:10:28 -0800 | [diff] [blame] | 5549 | return doDump(fd, DumpArgs(), asProto); |
| 5550 | } |
| 5551 | |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 5552 | void SurfaceFlinger::listLayers(std::string& result) const { |
| 5553 | for (const auto& layer : mLayerLifecycleManager.getLayers()) { |
| 5554 | StringAppendF(&result, "%s\n", layer->getDebugString().c_str()); |
| 5555 | } |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 5556 | } |
| 5557 | |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 5558 | void SurfaceFlinger::dumpStats(const DumpArgs& args, std::string& result) const { |
Dominik Laskowski | fe749dc | 2024-07-26 10:09:31 -0400 | [diff] [blame] | 5559 | StringAppendF(&result, "%" PRId64 "\n", mScheduler->getPacesetterVsyncPeriod().ns()); |
Dominik Laskowski | 66296b2 | 2022-07-12 08:20:36 -0700 | [diff] [blame] | 5560 | if (args.size() < 2) return; |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 5561 | |
Dominik Laskowski | 66296b2 | 2022-07-12 08:20:36 -0700 | [diff] [blame] | 5562 | const auto name = String8(args[1]); |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 5563 | traverseLegacyLayers([&](Layer* layer) { |
Tomasz Wasilczyk | b19fe0c | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 5564 | if (layer->getName() == name.c_str()) { |
Dominik Laskowski | 66296b2 | 2022-07-12 08:20:36 -0700 | [diff] [blame] | 5565 | layer->dumpFrameStats(result); |
| 5566 | } |
| 5567 | }); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 5568 | } |
| 5569 | |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 5570 | void SurfaceFlinger::clearStats(const DumpArgs& args, std::string&) { |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 5571 | const bool clearAll = args.size() < 2; |
| 5572 | const auto name = clearAll ? String8() : String8(args[1]); |
| 5573 | |
Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 5574 | traverseLegacyLayers([&](Layer* layer) { |
Tomasz Wasilczyk | b19fe0c | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 5575 | if (clearAll || layer->getName() == name.c_str()) { |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 5576 | layer->clearFrameStats(); |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 5577 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 5578 | }); |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 5579 | } |
| 5580 | |
Dominik Laskowski | c286714 | 2019-01-21 11:33:38 -0800 | [diff] [blame] | 5581 | void SurfaceFlinger::dumpTimeStats(const DumpArgs& args, bool asProto, std::string& result) const { |
| 5582 | mTimeStats->parseArgs(asProto, args, result); |
| 5583 | } |
| 5584 | |
Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 5585 | void SurfaceFlinger::dumpFrameTimeline(const DumpArgs& args, std::string& result) const { |
| 5586 | mFrameTimeline->parseArgs(args, result); |
| 5587 | } |
| 5588 | |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 5589 | void SurfaceFlinger::logFrameStats(TimePoint now) { |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 5590 | static TimePoint sTimestamp = now; |
| 5591 | if (now - sTimestamp < 30min) return; |
| 5592 | sTimestamp = now; |
| 5593 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 5594 | SFTRACE_CALL(); |
Vishnu Nair | ad4c67a | 2024-08-15 09:25:55 +0000 | [diff] [blame] | 5595 | traverseLegacyLayers([&](Layer* layer) { layer->logFrameStats(); }); |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 5596 | } |
| 5597 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5598 | void SurfaceFlinger::appendSfConfigString(std::string& result) const { |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 5599 | result.append(" [sf"); |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 5600 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 5601 | StringAppendF(&result, " PRESENT_TIME_OFFSET=%" PRId64, dispSyncPresentTimeOffset); |
| 5602 | StringAppendF(&result, " FORCE_HWC_FOR_RBG_TO_YUV=%d", useHwcForRgbToYuv); |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 5603 | StringAppendF(&result, " MAX_VIRT_DISPLAY_DIM=%zu", |
| 5604 | getHwComposer().getMaxVirtualDisplayDimension()); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 5605 | StringAppendF(&result, " RUNNING_WITHOUT_SYNC_FRAMEWORK=%d", !hasSyncFramework); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5606 | StringAppendF(&result, " NUM_FRAMEBUFFER_SURFACE_BUFFERS=%" PRId64, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 5607 | maxFrameBufferAcquiredBuffers); |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 5608 | result.append("]"); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 5609 | } |
| 5610 | |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 5611 | void SurfaceFlinger::dumpScheduler(std::string& result) const { |
| 5612 | utils::Dumper dumper{result}; |
| 5613 | |
| 5614 | mScheduler->dump(dumper); |
| 5615 | |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 5616 | // TODO(b/241285876): Move to DisplayModeController. |
| 5617 | dumper.dump("debugDisplayModeSetByBackdoor"sv, mDebugDisplayModeSetByBackdoor); |
| 5618 | dumper.eol(); |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 5619 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 5620 | |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 5621 | void SurfaceFlinger::dumpEvents(std::string& result) const { |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 5622 | mScheduler->dump(scheduler::Cycle::Render, result); |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | void SurfaceFlinger::dumpVsync(std::string& result) const { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 5626 | mScheduler->dumpVsync(result); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 5627 | } |
| 5628 | |
Dan Stoza | 269dc4d | 2021-01-15 15:07:43 -0800 | [diff] [blame] | 5629 | void SurfaceFlinger::dumpPlannerInfo(const DumpArgs& args, std::string& result) const { |
| 5630 | for (const auto& [token, display] : mDisplays) { |
| 5631 | const auto compositionDisplay = display->getCompositionDisplay(); |
| 5632 | compositionDisplay->dumpPlannerInfo(args, result); |
| 5633 | } |
| 5634 | } |
| 5635 | |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 5636 | void SurfaceFlinger::dumpCompositionDisplays(std::string& result) const { |
| 5637 | for (const auto& [token, display] : mDisplays) { |
| 5638 | display->getCompositionDisplay()->dump(result); |
| 5639 | result += '\n'; |
| 5640 | } |
| 5641 | } |
| 5642 | |
| 5643 | void SurfaceFlinger::dumpDisplays(std::string& result) const { |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame] | 5644 | utils::Dumper dumper{result}; |
| 5645 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 5646 | for (const auto& [id, display] : mPhysicalDisplays) { |
Dominik Laskowski | 5d7de5f | 2022-11-03 12:38:32 -0400 | [diff] [blame] | 5647 | utils::Dumper::Section section(dumper, ftl::Concat("Display ", id.value).str()); |
| 5648 | |
| 5649 | display.snapshot().dump(dumper); |
| 5650 | |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 5651 | if (const auto device = getDisplayDeviceLocked(id)) { |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame] | 5652 | device->dump(dumper); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 5653 | } |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 5654 | } |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 5655 | |
| 5656 | for (const auto& [token, display] : mDisplays) { |
| 5657 | if (display->isVirtual()) { |
Dominik Laskowski | 5d7de5f | 2022-11-03 12:38:32 -0400 | [diff] [blame] | 5658 | const auto displayId = display->getId(); |
| 5659 | utils::Dumper::Section section(dumper, |
| 5660 | ftl::Concat("Virtual Display ", displayId.value).str()); |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame] | 5661 | display->dump(dumper); |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 5662 | } |
| 5663 | } |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 5664 | } |
| 5665 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5666 | void SurfaceFlinger::dumpDisplayIdentificationData(std::string& result) const { |
Dominik Laskowski | 9fae102 | 2018-05-29 13:17:40 -0700 | [diff] [blame] | 5667 | for (const auto& [token, display] : mDisplays) { |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 5668 | const auto displayId = PhysicalDisplayId::tryCast(display->getId()); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 5669 | if (!displayId) { |
| 5670 | continue; |
| 5671 | } |
Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 5672 | const auto hwcDisplayId = getHwComposer().fromPhysicalDisplayId(*displayId); |
Dominik Laskowski | 7e04546 | 2018-05-30 13:02:02 -0700 | [diff] [blame] | 5673 | if (!hwcDisplayId) { |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 5674 | continue; |
| 5675 | } |
| 5676 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5677 | StringAppendF(&result, |
| 5678 | "Display %s (HWC display %" PRIu64 "): ", to_string(*displayId).c_str(), |
| 5679 | *hwcDisplayId); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 5680 | uint8_t port; |
| 5681 | DisplayIdentificationData data; |
Dominik Laskowski | 7e04546 | 2018-05-30 13:02:02 -0700 | [diff] [blame] | 5682 | if (!getHwComposer().getDisplayIdentificationData(*hwcDisplayId, &port, &data)) { |
Brian Lindahl | 8a96ef9 | 2024-05-24 14:46:29 +0000 | [diff] [blame] | 5683 | result.append("no display identification data\n"); |
| 5684 | continue; |
| 5685 | } |
| 5686 | |
| 5687 | if (data.empty()) { |
| 5688 | result.append("empty display identification data\n"); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 5689 | continue; |
| 5690 | } |
| 5691 | |
| 5692 | if (!isEdid(data)) { |
Brian Lindahl | 8a96ef9 | 2024-05-24 14:46:29 +0000 | [diff] [blame] | 5693 | result.append("unknown format for display identification data\n"); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 5694 | continue; |
| 5695 | } |
| 5696 | |
| 5697 | const auto edid = parseEdid(data); |
| 5698 | if (!edid) { |
Dominik Laskowski | b6e5437 | 2019-09-04 14:06:28 -0700 | [diff] [blame] | 5699 | result.append("invalid EDID\n"); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 5700 | continue; |
| 5701 | } |
| 5702 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5703 | StringAppendF(&result, "port=%u pnpId=%s displayName=\"", port, edid->pnpId.data()); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 5704 | result.append(edid->displayName.data(), edid->displayName.length()); |
| 5705 | result.append("\"\n"); |
| 5706 | } |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 5707 | } |
| 5708 | |
Dominik Laskowski | b6e5437 | 2019-09-04 14:06:28 -0700 | [diff] [blame] | 5709 | void SurfaceFlinger::dumpRawDisplayIdentificationData(const DumpArgs& args, |
| 5710 | std::string& result) const { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 5711 | hal::HWDisplayId hwcDisplayId; |
Dominik Laskowski | b6e5437 | 2019-09-04 14:06:28 -0700 | [diff] [blame] | 5712 | uint8_t port; |
| 5713 | DisplayIdentificationData data; |
| 5714 | |
| 5715 | if (args.size() > 1 && base::ParseUint(String8(args[1]), &hwcDisplayId) && |
| 5716 | getHwComposer().getDisplayIdentificationData(hwcDisplayId, &port, &data)) { |
| 5717 | result.append(reinterpret_cast<const char*>(data.data()), data.size()); |
| 5718 | } |
| 5719 | } |
| 5720 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5721 | void SurfaceFlinger::dumpWideColorInfo(std::string& result) const { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 5722 | StringAppendF(&result, "Device supports wide color: %d\n", mSupportsWideColor); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5723 | StringAppendF(&result, "DisplayColorSetting: %s\n", |
| 5724 | decodeDisplayColorSetting(mDisplayColorSetting).c_str()); |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 5725 | |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 5726 | // TODO: print out if wide-color mode is active or not. |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 5727 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 5728 | for (const auto& [id, display] : mPhysicalDisplays) { |
| 5729 | StringAppendF(&result, "Display %s color modes:\n", to_string(id).c_str()); |
| 5730 | for (const auto mode : display.snapshot().colorModes()) { |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 5731 | StringAppendF(&result, " %s (%d)\n", decodeColorMode(mode).c_str(), |
| 5732 | fmt::underlying(mode)); |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 5733 | } |
| 5734 | |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 5735 | if (const auto display = getDisplayDeviceLocked(id)) { |
| 5736 | ui::ColorMode currentMode = display->getCompositionDisplay()->getState().colorMode; |
| 5737 | StringAppendF(&result, " Current color mode: %s (%d)\n", |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 5738 | decodeColorMode(currentMode).c_str(), fmt::underlying(currentMode)); |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 5739 | } |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 5740 | } |
| 5741 | result.append("\n"); |
| 5742 | } |
| 5743 | |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 5744 | void SurfaceFlinger::dumpHdrInfo(std::string& result) const { |
| 5745 | for (const auto& [displayId, listener] : mHdrLayerInfoListeners) { |
| 5746 | StringAppendF(&result, "HDR events for display %" PRIu64 "\n", displayId.value); |
| 5747 | listener->dump(result); |
| 5748 | result.append("\n"); |
| 5749 | } |
| 5750 | } |
| 5751 | |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 5752 | void SurfaceFlinger::dumpFrontEnd(std::string& result) { |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 5753 | std::ostringstream out; |
| 5754 | out << "\nComposition list\n"; |
| 5755 | ui::LayerStack lastPrintedLayerStackHeader = ui::INVALID_LAYER_STACK; |
| 5756 | for (const auto& snapshot : mLayerSnapshotBuilder.getSnapshots()) { |
| 5757 | if (lastPrintedLayerStackHeader != snapshot->outputFilter.layerStack) { |
| 5758 | lastPrintedLayerStackHeader = snapshot->outputFilter.layerStack; |
| 5759 | out << "LayerStack=" << lastPrintedLayerStackHeader.id << "\n"; |
| 5760 | } |
| 5761 | out << " " << *snapshot << "\n"; |
| 5762 | } |
| 5763 | |
| 5764 | out << "\nInput list\n"; |
| 5765 | lastPrintedLayerStackHeader = ui::INVALID_LAYER_STACK; |
| 5766 | mLayerSnapshotBuilder.forEachInputSnapshot([&](const frontend::LayerSnapshot& snapshot) { |
| 5767 | if (lastPrintedLayerStackHeader != snapshot.outputFilter.layerStack) { |
| 5768 | lastPrintedLayerStackHeader = snapshot.outputFilter.layerStack; |
| 5769 | out << "LayerStack=" << lastPrintedLayerStackHeader.id << "\n"; |
| 5770 | } |
| 5771 | out << " " << snapshot << "\n"; |
| 5772 | }); |
| 5773 | |
| 5774 | out << "\nLayer Hierarchy\n" |
| 5775 | << mLayerHierarchyBuilder.getHierarchy().dump() << "\nOffscreen Hierarchy\n" |
| 5776 | << mLayerHierarchyBuilder.getOffscreenHierarchy().dump() << "\n\n"; |
| 5777 | result.append(out.str()); |
| 5778 | } |
| 5779 | |
| 5780 | void SurfaceFlinger::dumpVisibleFrontEnd(std::string& result) { |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 5781 | std::ostringstream out; |
| 5782 | out << "\nComposition list\n"; |
| 5783 | ui::LayerStack lastPrintedLayerStackHeader = ui::INVALID_LAYER_STACK; |
| 5784 | mLayerSnapshotBuilder.forEachVisibleSnapshot( |
| 5785 | [&](std::unique_ptr<frontend::LayerSnapshot>& snapshot) { |
| 5786 | if (snapshot->hasSomethingToDraw()) { |
| 5787 | if (lastPrintedLayerStackHeader != snapshot->outputFilter.layerStack) { |
| 5788 | lastPrintedLayerStackHeader = snapshot->outputFilter.layerStack; |
| 5789 | out << "LayerStack=" << lastPrintedLayerStackHeader.id << "\n"; |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 5790 | } |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 5791 | out << " " << *snapshot << "\n"; |
| 5792 | } |
| 5793 | }); |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 5794 | |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 5795 | out << "\nInput list\n"; |
| 5796 | lastPrintedLayerStackHeader = ui::INVALID_LAYER_STACK; |
| 5797 | mLayerSnapshotBuilder.forEachInputSnapshot([&](const frontend::LayerSnapshot& snapshot) { |
| 5798 | if (lastPrintedLayerStackHeader != snapshot.outputFilter.layerStack) { |
| 5799 | lastPrintedLayerStackHeader = snapshot.outputFilter.layerStack; |
| 5800 | out << "LayerStack=" << lastPrintedLayerStackHeader.id << "\n"; |
| 5801 | } |
| 5802 | out << " " << snapshot << "\n"; |
| 5803 | }); |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 5804 | |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 5805 | out << "\nLayer Hierarchy\n" |
| 5806 | << mLayerHierarchyBuilder.getHierarchy() << "\nOffscreen Hierarchy\n" |
| 5807 | << mLayerHierarchyBuilder.getOffscreenHierarchy() << "\n\n"; |
| 5808 | result = out.str(); |
| 5809 | dumpHwcLayersMinidump(result); |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 5810 | } |
| 5811 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 5812 | perfetto::protos::LayersProto SurfaceFlinger::dumpDrawingStateProto(uint32_t traceFlags) const { |
Nataniel Borges | 63b3c87 | 2022-11-03 16:22:24 +0000 | [diff] [blame] | 5813 | std::unordered_set<uint64_t> stackIdsToSkip; |
| 5814 | |
| 5815 | // Determine if virtual layers display should be skipped |
| 5816 | if ((traceFlags & LayerTracing::TRACE_VIRTUAL_DISPLAYS) == 0) { |
| 5817 | for (const auto& [_, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
| 5818 | if (display->isVirtual()) { |
| 5819 | stackIdsToSkip.insert(display->getLayerStack().id); |
| 5820 | } |
| 5821 | } |
| 5822 | } |
| 5823 | |
Vishnu Nair | b46e37c | 2024-08-16 14:30:28 +0000 | [diff] [blame] | 5824 | auto traceGenerator = |
| 5825 | LayerProtoFromSnapshotGenerator(mLayerSnapshotBuilder, mFrontEndDisplayInfos, |
| 5826 | mLegacyLayers, traceFlags) |
| 5827 | .with(mLayerHierarchyBuilder.getHierarchy()); |
| 5828 | |
| 5829 | if (traceFlags & LayerTracing::Flag::TRACE_EXTRA) { |
| 5830 | traceGenerator.withOffscreenLayers(mLayerHierarchyBuilder.getOffscreenHierarchy()); |
| 5831 | } |
| 5832 | |
| 5833 | return traceGenerator.generate(); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 5834 | } |
| 5835 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 5836 | google::protobuf::RepeatedPtrField<perfetto::protos::DisplayProto> |
| 5837 | SurfaceFlinger::dumpDisplayProto() const { |
| 5838 | google::protobuf::RepeatedPtrField<perfetto::protos::DisplayProto> displays; |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 5839 | for (const auto& [_, display] : FTL_FAKE_GUARD(mStateLock, mDisplays)) { |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 5840 | perfetto::protos::DisplayProto* displayProto = displays.Add(); |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 5841 | displayProto->set_id(display->getId().value); |
| 5842 | displayProto->set_name(display->getDisplayName()); |
| 5843 | displayProto->set_layer_stack(display->getLayerStack().id); |
DoÄŸancan Emek | fc4b6ed | 2023-07-26 15:24:04 +0000 | [diff] [blame] | 5844 | |
| 5845 | if (!display->isVirtual()) { |
| 5846 | const auto dpi = display->refreshRateSelector().getActiveMode().modePtr->getDpi(); |
| 5847 | displayProto->set_dpi_x(dpi.x); |
| 5848 | displayProto->set_dpi_y(dpi.y); |
| 5849 | } |
| 5850 | |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 5851 | LayerProtoHelper::writeSizeToProto(display->getWidth(), display->getHeight(), |
| 5852 | [&]() { return displayProto->mutable_size(); }); |
| 5853 | LayerProtoHelper::writeToProto(display->getLayerStackSpaceRect(), [&]() { |
| 5854 | return displayProto->mutable_layer_stack_space_rect(); |
| 5855 | }); |
| 5856 | LayerProtoHelper::writeTransformToProto(display->getTransform(), |
| 5857 | displayProto->mutable_transform()); |
Vishnu Nair | 791d48a | 2021-12-02 16:55:13 -0800 | [diff] [blame] | 5858 | displayProto->set_is_virtual(display->isVirtual()); |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 5859 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 5860 | return displays; |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 5861 | } |
| 5862 | |
Alec Mouri | 6b9e991 | 2020-01-21 10:50:24 -0800 | [diff] [blame] | 5863 | void SurfaceFlinger::dumpHwc(std::string& result) const { |
| 5864 | getHwComposer().dump(result); |
| 5865 | } |
| 5866 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 5867 | perfetto::protos::LayersProto SurfaceFlinger::dumpProtoFromMainThread(uint32_t traceFlags) { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5868 | return mScheduler |
| 5869 | ->schedule([=, this]() FTL_FAKE_GUARD(kMainThreadContext) { |
| 5870 | return dumpDrawingStateProto(traceFlags); |
| 5871 | }) |
| 5872 | .get(); |
Vishnu Nair | 8406fd7 | 2019-07-30 11:29:31 -0700 | [diff] [blame] | 5873 | } |
| 5874 | |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 5875 | void SurfaceFlinger::dumpHwcLayersMinidump(std::string& result) const { |
Dominik Laskowski | 48bc171 | 2024-07-23 13:16:04 -0400 | [diff] [blame] | 5876 | for (const auto& [token, display] : mDisplays) { |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 5877 | const auto displayId = HalDisplayId::tryCast(display->getId()); |
| 5878 | if (!displayId) { |
| 5879 | continue; |
| 5880 | } |
| 5881 | |
| 5882 | StringAppendF(&result, "Display %s (%s) HWC layers:\n", to_string(*displayId).c_str(), |
| 5883 | displayId == mActiveDisplayId ? "active" : "inactive"); |
| 5884 | Layer::miniDumpHeader(result); |
| 5885 | |
| 5886 | const DisplayDevice& ref = *display; |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 5887 | mLayerSnapshotBuilder.forEachVisibleSnapshot( |
| 5888 | [&](const frontend::LayerSnapshot& snapshot) FTL_FAKE_GUARD(kMainThreadContext) { |
| 5889 | if (!snapshot.hasSomethingToDraw() || |
| 5890 | ref.getLayerStack() != snapshot.outputFilter.layerStack) { |
| 5891 | return; |
| 5892 | } |
| 5893 | auto it = mLegacyLayers.find(snapshot.sequence); |
| 5894 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 5895 | "Couldnt find layer object for %s", |
| 5896 | snapshot.getDebugString().c_str()); |
| 5897 | it->second->miniDump(result, snapshot, ref); |
| 5898 | }); |
Ady Abraham | 5ad28d6 | 2022-08-11 11:26:57 -0700 | [diff] [blame] | 5899 | result.append("\n"); |
| 5900 | } |
| 5901 | } |
| 5902 | |
Vishnu Nair | d7ff85f | 2023-12-08 09:51:31 -0800 | [diff] [blame] | 5903 | void SurfaceFlinger::dumpAll(const DumpArgs& args, const std::string& compositionLayers, |
| 5904 | std::string& result) const { |
| 5905 | TimedLock lock(mStateLock, s2ns(1), __func__); |
| 5906 | if (!lock.locked()) { |
| 5907 | StringAppendF(&result, "Dumping without lock after timeout: %s (%d)\n", |
| 5908 | strerror(-lock.status), lock.status); |
| 5909 | } |
| 5910 | |
Dominik Laskowski | c286714 | 2019-01-21 11:33:38 -0800 | [diff] [blame] | 5911 | const bool colorize = !args.empty() && args[0] == String16("--color"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5912 | Colorizer colorizer(colorize); |
| 5913 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 5914 | // figure out if we're stuck somewhere |
| 5915 | const nsecs_t now = systemTime(); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 5916 | const nsecs_t inTransaction(mDebugInTransaction); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 5917 | nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0; |
| 5918 | |
| 5919 | /* |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 5920 | * Dump library configuration. |
| 5921 | */ |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5922 | |
| 5923 | colorizer.bold(result); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 5924 | result.append("Build configuration:"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5925 | colorizer.reset(result); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 5926 | appendSfConfigString(result); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 5927 | result.append("\n"); |
| 5928 | |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 5929 | result.append("\nDisplay identification data:\n"); |
| 5930 | dumpDisplayIdentificationData(result); |
| 5931 | |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 5932 | result.append("\nWide-Color information:\n"); |
| 5933 | dumpWideColorInfo(result); |
| 5934 | |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 5935 | dumpHdrInfo(result); |
| 5936 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5937 | colorizer.bold(result); |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 5938 | result.append("Sync configuration: "); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5939 | colorizer.reset(result); |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 5940 | result.append(SyncFeatures::getInstance().toString()); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 5941 | result.append("\n\n"); |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 5942 | |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 5943 | colorizer.bold(result); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 5944 | result.append("Scheduler:\n"); |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 5945 | colorizer.reset(result); |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 5946 | dumpScheduler(result); |
| 5947 | dumpEvents(result); |
| 5948 | dumpVsync(result); |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 5949 | result.append("\n"); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 5950 | |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 5951 | /* |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 5952 | * Dump the visible layer list |
| 5953 | */ |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5954 | colorizer.bold(result); |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 5955 | StringAppendF(&result, "SurfaceFlinger New Frontend Enabled:%s\n", "true"); |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 5956 | StringAppendF(&result, "Active Layers - layers with client handles (count = %zu)\n", |
| 5957 | mNumLayers.load()); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5958 | colorizer.reset(result); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 5959 | |
Alec Mouri | 6761733 | 2022-09-26 21:37:01 +0000 | [diff] [blame] | 5960 | result.append(compositionLayers); |
Lloyd Pique | 207def9 | 2019-02-28 16:09:52 -0800 | [diff] [blame] | 5961 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5962 | colorizer.bold(result); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 5963 | StringAppendF(&result, "Displays (%zu entries)\n", mDisplays.size()); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5964 | colorizer.reset(result); |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 5965 | dumpDisplays(result); |
| 5966 | dumpCompositionDisplays(result); |
| 5967 | result.push_back('\n'); |
Lloyd Pique | c3cb729 | 2019-05-17 15:25:14 -0700 | [diff] [blame] | 5968 | |
| 5969 | mCompositionEngine->dump(result); |
| 5970 | |
| 5971 | /* |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 5972 | * Dump SurfaceFlinger global state |
| 5973 | */ |
| 5974 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5975 | colorizer.bold(result); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 5976 | result.append("SurfaceFlinger global state:\n"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 5977 | colorizer.reset(result); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 5978 | |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 5979 | getRenderEngine().dump(result); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 5980 | |
Robert Carr | beba6f0 | 2021-02-10 21:06:27 -0800 | [diff] [blame] | 5981 | result.append("ClientCache state:\n"); |
| 5982 | ClientCache::getInstance().dump(result); |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 5983 | DebugEGLImageTracker::getInstance()->dump(result); |
| 5984 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 5985 | if (const auto display = getDefaultDisplayDeviceLocked()) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 5986 | display->getCompositionDisplay()->getState().undefinedRegion.dump(result, |
| 5987 | "undefinedRegion"); |
Dominik Laskowski | 718f960 | 2019-11-09 20:01:35 -0800 | [diff] [blame] | 5988 | StringAppendF(&result, " orientation=%s, isPoweredOn=%d\n", |
| 5989 | toCString(display->getOrientation()), display->isPoweredOn()); |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 5990 | } |
Carlos Martinez Romero | 779dc1f | 2022-10-27 23:23:30 +0000 | [diff] [blame] | 5991 | StringAppendF(&result, " transaction-flags : %08x\n", mTransactionFlags.load()); |
Dominik Laskowski | 45de9bd | 2018-06-11 17:44:10 -0700 | [diff] [blame] | 5992 | |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 5993 | if (const auto display = getDefaultDisplayDeviceLocked()) { |
Ying Wei | 784a8e7 | 2024-04-20 08:46:42 +0000 | [diff] [blame] | 5994 | std::string peakFps, xDpi, yDpi; |
| 5995 | const auto activeMode = display->refreshRateSelector().getActiveMode(); |
| 5996 | if (const auto activeModePtr = activeMode.modePtr.get()) { |
| 5997 | peakFps = to_string(activeMode.modePtr->getPeakFps()); |
Dominik Laskowski | 02bb207 | 2022-08-26 15:02:52 -0700 | [diff] [blame] | 5998 | const auto dpi = activeModePtr->getDpi(); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 5999 | xDpi = base::StringPrintf("%.2f", dpi.x); |
| 6000 | yDpi = base::StringPrintf("%.2f", dpi.y); |
Marin Shalamanov | 9afcbd8 | 2020-08-19 12:34:59 +0200 | [diff] [blame] | 6001 | } else { |
Ying Wei | 784a8e7 | 2024-04-20 08:46:42 +0000 | [diff] [blame] | 6002 | peakFps = "unknown"; |
Marin Shalamanov | 9afcbd8 | 2020-08-19 12:34:59 +0200 | [diff] [blame] | 6003 | xDpi = "unknown"; |
| 6004 | yDpi = "unknown"; |
| 6005 | } |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6006 | StringAppendF(&result, |
Ying Wei | 784a8e7 | 2024-04-20 08:46:42 +0000 | [diff] [blame] | 6007 | " peak-refresh-rate : %s\n" |
Marin Shalamanov | 9afcbd8 | 2020-08-19 12:34:59 +0200 | [diff] [blame] | 6008 | " x-dpi : %s\n" |
| 6009 | " y-dpi : %s\n", |
Ying Wei | 784a8e7 | 2024-04-20 08:46:42 +0000 | [diff] [blame] | 6010 | peakFps.c_str(), xDpi.c_str(), yDpi.c_str()); |
Dominik Laskowski | 45de9bd | 2018-06-11 17:44:10 -0700 | [diff] [blame] | 6011 | } |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6012 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6013 | StringAppendF(&result, " transaction time: %f us\n", inTransactionDuration / 1000.0); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6014 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 6015 | result.append("\nTransaction tracing: "); |
| 6016 | if (mTransactionTracing) { |
| 6017 | result.append("enabled\n"); |
| 6018 | mTransactionTracing->dump(result); |
| 6019 | } else { |
| 6020 | result.append("disabled\n"); |
| 6021 | } |
| 6022 | result.push_back('\n'); |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 6023 | |
Ady Abraham | 2790e9f | 2021-04-28 13:14:20 -0700 | [diff] [blame] | 6024 | { |
| 6025 | DumpArgs plannerArgs; |
| 6026 | plannerArgs.add(); // first argument is ignored |
| 6027 | plannerArgs.add(String16("--layers")); |
| 6028 | dumpPlannerInfo(plannerArgs, result); |
| 6029 | } |
| 6030 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6031 | /* |
| 6032 | * Dump HWComposer state |
| 6033 | */ |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6034 | colorizer.bold(result); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 6035 | result.append("h/w composer state:\n"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 6036 | colorizer.reset(result); |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6037 | const bool hwcDisabled = mDebugDisableHWC || mDebugFlashDelay; |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 6038 | StringAppendF(&result, " h/w composer %s\n", hwcDisabled ? "disabled" : "enabled"); |
Ady Abraham | c4acf51 | 2022-02-18 17:11:59 -0800 | [diff] [blame] | 6039 | dumpHwc(result); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6040 | |
| 6041 | /* |
| 6042 | * Dump gralloc state |
| 6043 | */ |
| 6044 | const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
| 6045 | alloc.dump(result); |
Karthik Ravi Shankar | 171155a | 2017-06-28 15:40:24 -0700 | [diff] [blame] | 6046 | |
rnlee | a2ecd83 | 2021-07-29 13:58:30 -0700 | [diff] [blame] | 6047 | /* |
| 6048 | * Dump flag/property manager state |
| 6049 | */ |
Ady Abraham | 53b0c49 | 2023-10-23 11:47:14 -0700 | [diff] [blame] | 6050 | FlagManager::getInstance().dump(result); |
rnlee | a2ecd83 | 2021-07-29 13:58:30 -0700 | [diff] [blame] | 6051 | |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 6052 | result.append(mTimeStats->miniDump()); |
| 6053 | result.append("\n"); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 6054 | } |
| 6055 | |
chaviw | 17ac24b | 2021-01-28 18:50:05 -0800 | [diff] [blame] | 6056 | mat4 SurfaceFlinger::calculateColorMatrix(float saturation) { |
| 6057 | if (saturation == 1) { |
| 6058 | return mat4(); |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6059 | } |
| 6060 | |
chaviw | 17ac24b | 2021-01-28 18:50:05 -0800 | [diff] [blame] | 6061 | float3 luminance{0.213f, 0.715f, 0.072f}; |
| 6062 | luminance *= 1.0f - saturation; |
| 6063 | mat4 saturationMatrix = mat4(vec4{luminance.r + saturation, luminance.r, luminance.r, 0.0f}, |
| 6064 | vec4{luminance.g, luminance.g + saturation, luminance.g, 0.0f}, |
| 6065 | vec4{luminance.b, luminance.b, luminance.b + saturation, 0.0f}, |
| 6066 | vec4{0.0f, 0.0f, 0.0f, 1.0f}); |
| 6067 | return saturationMatrix; |
| 6068 | } |
| 6069 | |
| 6070 | void SurfaceFlinger::updateColorMatrixLocked() { |
| 6071 | mat4 colorMatrix = |
| 6072 | mClientColorMatrix * calculateColorMatrix(mGlobalSaturationFactor) * mDaltonizer(); |
| 6073 | |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6074 | if (mCurrentState.colorMatrix != colorMatrix) { |
| 6075 | mCurrentState.colorMatrix = colorMatrix; |
| 6076 | mCurrentState.colorMatrixChanged = true; |
| 6077 | setTransactionFlags(eTransactionNeeded); |
| 6078 | } |
| 6079 | } |
| 6080 | |
Fabien Sanglard | 6e8e98a | 2016-10-27 20:04:11 -0700 | [diff] [blame] | 6081 | status_t SurfaceFlinger::CheckTransactCodeCredentials(uint32_t code) { |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 6082 | #pragma clang diagnostic push |
| 6083 | #pragma clang diagnostic error "-Wswitch-enum" |
| 6084 | switch (static_cast<ISurfaceComposerTag>(code)) { |
| 6085 | // These methods should at minimum make sure that the client requested |
| 6086 | // access to SF. |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 6087 | case GET_HDR_CAPABILITIES: |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 6088 | case GET_AUTO_LOW_LATENCY_MODE_SUPPORT: |
Galia Peycheva | 5492cb5 | 2019-10-30 14:13:16 +0100 | [diff] [blame] | 6089 | case GET_GAME_CONTENT_TYPE_SUPPORT: |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 6090 | case ACQUIRE_FRAME_RATE_FLEXIBILITY_TOKEN: { |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 6091 | // OVERRIDE_HDR_TYPES is used by CTS tests, which acquire the necessary |
| 6092 | // permission dynamically. Don't use the permission cache for this check. |
| 6093 | bool usePermissionCache = code != OVERRIDE_HDR_TYPES; |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 6094 | if (!callingThreadHasUnscopedSurfaceFlingerAccess(usePermissionCache)) { |
Robert Carr | d4ae7f3 | 2018-06-07 16:10:57 -0700 | [diff] [blame] | 6095 | IPCThreadState* ipc = IPCThreadState::self(); |
| 6096 | ALOGE("Permission Denial: can't access SurfaceFlinger pid=%d, uid=%d", |
| 6097 | ipc->getCallingPid(), ipc->getCallingUid()); |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 6098 | return PERMISSION_DENIED; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6099 | } |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 6100 | return OK; |
| 6101 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 6102 | // The following calls are currently used by clients that do not |
| 6103 | // request necessary permissions. However, they do not expose any secret |
| 6104 | // information, so it is OK to pass them. |
Peiyong Lin | d1fedb4 | 2019-03-11 17:48:41 -0700 | [diff] [blame] | 6105 | case GET_ACTIVE_COLOR_MODE: |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 6106 | case GET_ACTIVE_DISPLAY_MODE: |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 6107 | case GET_DISPLAY_COLOR_MODES: |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 6108 | case GET_DISPLAY_MODES: |
Ady Abraham | 07d03c4 | 2023-09-27 19:15:08 -0700 | [diff] [blame] | 6109 | case GET_SCHEDULING_POLICY: |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 6110 | // Calling setTransactionState is safe, because you need to have been |
| 6111 | // granted a reference to Client* and Handle* to do anything with it. |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 6112 | case SET_TRANSACTION_STATE: { |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 6113 | // This is not sensitive information, so should not require permission control. |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 6114 | return OK; |
| 6115 | } |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 6116 | case BOOT_FINISHED: |
| 6117 | // Used by apps to hook Choreographer to SurfaceFlinger. |
| 6118 | case CREATE_DISPLAY_EVENT_CONNECTION: |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 6119 | case CREATE_CONNECTION: |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 6120 | case CREATE_VIRTUAL_DISPLAY: |
| 6121 | case DESTROY_VIRTUAL_DISPLAY: |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 6122 | case GET_PRIMARY_PHYSICAL_DISPLAY_ID: |
| 6123 | case GET_PHYSICAL_DISPLAY_IDS: |
| 6124 | case GET_PHYSICAL_DISPLAY_TOKEN: |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 6125 | case AUTHENTICATE_SURFACE: |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 6126 | case SET_POWER_MODE: |
Huihong Luo | 0a81aa3 | 2022-02-22 16:02:36 -0800 | [diff] [blame] | 6127 | case GET_SUPPORTED_FRAME_TIMESTAMPS: |
Huihong Luo | aa7fc2e | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 6128 | case GET_DISPLAY_STATE: |
| 6129 | case GET_DISPLAY_STATS: |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 6130 | case GET_STATIC_DISPLAY_INFO: |
Huihong Luo | 38603fd | 2022-02-21 14:32:54 -0800 | [diff] [blame] | 6131 | case GET_DYNAMIC_DISPLAY_INFO: |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 6132 | case GET_DISPLAY_NATIVE_PRIMARIES: |
| 6133 | case SET_ACTIVE_COLOR_MODE: |
| 6134 | case SET_BOOT_DISPLAY_MODE: |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 6135 | case CLEAR_BOOT_DISPLAY_MODE: |
| 6136 | case GET_BOOT_DISPLAY_MODE_SUPPORT: |
| 6137 | case SET_AUTO_LOW_LATENCY_MODE: |
| 6138 | case SET_GAME_CONTENT_TYPE: |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 6139 | case CAPTURE_LAYERS: |
| 6140 | case CAPTURE_DISPLAY: |
| 6141 | case CAPTURE_DISPLAY_BY_ID: |
Huihong Luo | 4ed1c91 | 2022-02-22 14:30:01 -0800 | [diff] [blame] | 6142 | case CLEAR_ANIMATION_FRAME_STATS: |
| 6143 | case GET_ANIMATION_FRAME_STATS: |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 6144 | case OVERRIDE_HDR_TYPES: |
| 6145 | case ON_PULL_ATOM: |
| 6146 | case ENABLE_VSYNC_INJECTIONS: |
| 6147 | case INJECT_VSYNC: |
| 6148 | case GET_LAYER_DEBUG_INFO: |
| 6149 | case GET_COLOR_MANAGEMENT: |
| 6150 | case GET_COMPOSITION_PREFERENCE: |
| 6151 | case GET_DISPLAYED_CONTENT_SAMPLING_ATTRIBUTES: |
| 6152 | case SET_DISPLAY_CONTENT_SAMPLING_ENABLED: |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 6153 | case GET_DISPLAYED_CONTENT_SAMPLE: |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 6154 | case GET_PROTECTED_CONTENT_SUPPORT: |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 6155 | case IS_WIDE_COLOR_DISPLAY: |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 6156 | case ADD_REGION_SAMPLING_LISTENER: |
| 6157 | case REMOVE_REGION_SAMPLING_LISTENER: |
| 6158 | case ADD_FPS_LISTENER: |
| 6159 | case REMOVE_FPS_LISTENER: |
| 6160 | case ADD_TUNNEL_MODE_ENABLED_LISTENER: |
| 6161 | case REMOVE_TUNNEL_MODE_ENABLED_LISTENER: |
| 6162 | case ADD_WINDOW_INFOS_LISTENER: |
| 6163 | case REMOVE_WINDOW_INFOS_LISTENER: |
| 6164 | case SET_DESIRED_DISPLAY_MODE_SPECS: |
| 6165 | case GET_DESIRED_DISPLAY_MODE_SPECS: |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 6166 | case GET_DISPLAY_BRIGHTNESS_SUPPORT: |
| 6167 | case SET_DISPLAY_BRIGHTNESS: |
| 6168 | case ADD_HDR_LAYER_INFO_LISTENER: |
| 6169 | case REMOVE_HDR_LAYER_INFO_LISTENER: |
| 6170 | case NOTIFY_POWER_BOOST: |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 6171 | case SET_GLOBAL_SHADOW_SETTINGS: |
| 6172 | case GET_DISPLAY_DECORATION_SUPPORT: |
| 6173 | case SET_FRAME_RATE: |
| 6174 | case SET_OVERRIDE_FRAME_RATE: |
| 6175 | case SET_FRAME_TIMELINE_INFO: |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 6176 | case ADD_TRANSACTION_TRACE_LISTENER: |
| 6177 | case GET_GPU_CONTEXT_PRIORITY: |
| 6178 | case GET_MAX_ACQUIRED_BUFFER_COUNT: |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 6179 | LOG_FATAL("Deprecated opcode: %d, migrated to AIDL", code); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 6180 | return PERMISSION_DENIED; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6181 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 6182 | |
| 6183 | // These codes are used for the IBinder protocol to either interrogate the recipient |
| 6184 | // side of the transaction for its canonical interface descriptor or to dump its state. |
| 6185 | // We let them pass by default. |
| 6186 | if (code == IBinder::INTERFACE_TRANSACTION || code == IBinder::DUMP_TRANSACTION || |
| 6187 | code == IBinder::PING_TRANSACTION || code == IBinder::SHELL_COMMAND_TRANSACTION || |
| 6188 | code == IBinder::SYSPROPS_TRANSACTION) { |
| 6189 | return OK; |
| 6190 | } |
Alec Mouri | 1c7938e | 2023-09-22 04:17:23 +0000 | [diff] [blame] | 6191 | // Numbers from 1000 to 1045 are currently used for backdoors. The code |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 6192 | // 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] | 6193 | if (code >= 1000 && code <= 1045) { |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 6194 | ALOGV("Accessing SurfaceFlinger through backdoor code: %u", code); |
| 6195 | return OK; |
| 6196 | } |
| 6197 | ALOGE("Permission Denial: SurfaceFlinger did not recognize request code: %u", code); |
| 6198 | return PERMISSION_DENIED; |
| 6199 | #pragma clang diagnostic pop |
Fabien Sanglard | 6e8e98a | 2016-10-27 20:04:11 -0700 | [diff] [blame] | 6200 | } |
| 6201 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 6202 | status_t SurfaceFlinger::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 6203 | uint32_t flags) { |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6204 | if (const status_t error = CheckTransactCodeCredentials(code); error != OK) { |
| 6205 | return error; |
Fabien Sanglard | 6e8e98a | 2016-10-27 20:04:11 -0700 | [diff] [blame] | 6206 | } |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 6207 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6208 | status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); |
| 6209 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 6210 | CHECK_INTERFACE(ISurfaceComposer, data, reply); |
Romain Guy | 8c6bbd6 | 2017-06-05 13:51:43 -0700 | [diff] [blame] | 6211 | IPCThreadState* ipc = IPCThreadState::self(); |
| 6212 | const int uid = ipc->getCallingUid(); |
| 6213 | if (CC_UNLIKELY(uid != AID_SYSTEM |
| 6214 | && !PermissionCache::checkCallingPermission(sHardwareTest))) { |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 6215 | const int pid = ipc->getCallingPid(); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6216 | ALOGE("Permission Denial: " |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 6217 | "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] | 6218 | return PERMISSION_DENIED; |
| 6219 | } |
| 6220 | int n; |
| 6221 | switch (code) { |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6222 | case 1000: // Unused. |
| 6223 | case 1001: |
| 6224 | return NAME_NOT_FOUND; |
| 6225 | case 1002: // Toggle flashing on surface damage. |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 6226 | sfdo_setDebugFlash(data.readInt32()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6227 | return NO_ERROR; |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 6228 | case 1004: // Force composite ahead of next VSYNC. |
Dominik Laskowski | 46f3e3b | 2021-08-10 11:44:24 -0700 | [diff] [blame] | 6229 | case 1006: |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 6230 | sfdo_scheduleComposite(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6231 | return NO_ERROR; |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6232 | case 1005: { // Force commit ahead of next VSYNC. |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 6233 | sfdo_scheduleCommit(); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 6234 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6235 | } |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6236 | case 1007: // Unused. |
| 6237 | return NAME_NOT_FOUND; |
| 6238 | case 1008: // Toggle forced GPU composition. |
Carlos Martinez Romero | b4ea9e6 | 2023-10-25 21:48:56 +0000 | [diff] [blame] | 6239 | sfdo_forceClientComposition(data.readInt32() != 0); |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 6240 | return NO_ERROR; |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6241 | case 1009: // Toggle use of transform hint. |
| 6242 | mDebugDisableTransformHint = data.readInt32() != 0; |
| 6243 | scheduleRepaint(); |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 6244 | return NO_ERROR; |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6245 | case 1010: // Interrogate. |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 6246 | reply->writeInt32(0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6247 | reply->writeInt32(0); |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6248 | reply->writeInt32(mDebugFlashDelay); |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 6249 | reply->writeInt32(0); |
Dianne Hackborn | 12839be | 2012-02-06 21:21:05 -0800 | [diff] [blame] | 6250 | reply->writeInt32(mDebugDisableHWC); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6251 | return NO_ERROR; |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 6252 | case 1013: // Unused. |
| 6253 | return NAME_NOT_FOUND; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 6254 | case 1014: { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6255 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 6256 | // daltonize |
| 6257 | n = data.readInt32(); |
Isaac Chai | 1ec2d9a | 2024-04-11 20:10:34 +0000 | [diff] [blame] | 6258 | mDaltonizer.setLevel(data.readInt32()); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 6259 | switch (n % 10) { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 6260 | case 1: |
| 6261 | mDaltonizer.setType(ColorBlindnessType::Protanomaly); |
| 6262 | break; |
| 6263 | case 2: |
| 6264 | mDaltonizer.setType(ColorBlindnessType::Deuteranomaly); |
| 6265 | break; |
| 6266 | case 3: |
| 6267 | mDaltonizer.setType(ColorBlindnessType::Tritanomaly); |
| 6268 | break; |
| 6269 | default: |
| 6270 | mDaltonizer.setType(ColorBlindnessType::None); |
| 6271 | break; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 6272 | } |
| 6273 | if (n >= 10) { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 6274 | mDaltonizer.setMode(ColorBlindnessMode::Correction); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 6275 | } else { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 6276 | mDaltonizer.setMode(ColorBlindnessMode::Simulation); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 6277 | } |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6278 | |
| 6279 | updateColorMatrixLocked(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 6280 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6281 | } |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 6282 | case 1015: { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6283 | Mutex::Autolock _l(mStateLock); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 6284 | // apply a color matrix |
| 6285 | n = data.readInt32(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 6286 | if (n) { |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 6287 | // color matrix is sent as a column-major mat4 matrix |
Alan Viverette | 794c5ba | 2013-10-03 16:40:52 -0700 | [diff] [blame] | 6288 | for (size_t i = 0 ; i < 4; i++) { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 6289 | for (size_t j = 0; j < 4; j++) { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6290 | mClientColorMatrix[i][j] = data.readFloat(); |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 6291 | } |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 6292 | } |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 6293 | } else { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6294 | mClientColorMatrix = mat4(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 6295 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 6296 | |
| 6297 | // Check that supplied matrix's last row is {0,0,0,1} so we can avoid |
| 6298 | // the division by w in the fragment shader |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6299 | float4 lastRow(transpose(mClientColorMatrix)[3]); |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 6300 | if (any(greaterThan(abs(lastRow - float4{0, 0, 0, 1}), float4{1e-4f}))) { |
| 6301 | ALOGE("The color transform's last row must be (0, 0, 0, 1)"); |
| 6302 | } |
| 6303 | |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6304 | updateColorMatrixLocked(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 6305 | return NO_ERROR; |
| 6306 | } |
Dominik Laskowski | 8d32ddc | 2019-08-07 11:25:35 -0700 | [diff] [blame] | 6307 | case 1016: { // Unused. |
| 6308 | return NAME_NOT_FOUND; |
Ruchi Kandoi | f52b3c8 | 2014-04-24 16:42:35 -0700 | [diff] [blame] | 6309 | } |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 6310 | case 1017: { |
| 6311 | n = data.readInt32(); |
Dominik Laskowski | 9dab343 | 2019-03-27 13:21:10 -0700 | [diff] [blame] | 6312 | mForceFullDamage = n != 0; |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 6313 | return NO_ERROR; |
| 6314 | } |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 6315 | case 1018: { // Set the render deadline as a duration until VSYNC. |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 6316 | n = data.readInt32(); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 6317 | mScheduler->setDuration(scheduler::Cycle::Render, std::chrono::nanoseconds(n), 0ns); |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 6318 | return NO_ERROR; |
| 6319 | } |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 6320 | 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] | 6321 | n = data.readInt32(); |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 6322 | mScheduler->setDuration(scheduler::Cycle::LastComposite, |
| 6323 | std::chrono::nanoseconds(n), 0ns); |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 6324 | return NO_ERROR; |
| 6325 | } |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 6326 | case 1020: { // Unused |
| 6327 | return NAME_NOT_FOUND; |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 6328 | } |
Dan Stoza | 8cf150a | 2016-08-02 10:27:31 -0700 | [diff] [blame] | 6329 | case 1021: { // Disable HWC virtual displays |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 6330 | const bool enable = data.readInt32() != 0; |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6331 | static_cast<void>( |
| 6332 | mScheduler->schedule([this, enable] { enableHalVirtualDisplays(enable); })); |
Dan Stoza | 8cf150a | 2016-08-02 10:27:31 -0700 | [diff] [blame] | 6333 | return NO_ERROR; |
| 6334 | } |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 6335 | case 1022: { // Set saturation boost |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6336 | Mutex::Autolock _l(mStateLock); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 6337 | mGlobalSaturationFactor = std::max(0.0f, std::min(data.readFloat(), 2.0f)); |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 6338 | |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 6339 | updateColorMatrixLocked(); |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 6340 | return NO_ERROR; |
| 6341 | } |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 6342 | case 1023: { // Set color mode. |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 6343 | mDisplayColorSetting = static_cast<DisplayColorSetting>(data.readInt32()); |
Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 6344 | |
| 6345 | if (int32_t colorMode; data.readInt32(&colorMode) == NO_ERROR) { |
| 6346 | mForceColorMode = static_cast<ui::ColorMode>(colorMode); |
Daniel Solomon | 7c7ede2 | 2019-07-11 16:35:40 -0700 | [diff] [blame] | 6347 | } |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6348 | scheduleRepaint(); |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 6349 | return NO_ERROR; |
| 6350 | } |
Peiyong Lin | 4bac91c | 2018-10-25 09:48:33 -0700 | [diff] [blame] | 6351 | // Deprecate, use 1030 to check whether the device is color managed. |
| 6352 | case 1024: { |
| 6353 | return NAME_NOT_FOUND; |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 6354 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 6355 | // Deprecated, use perfetto to start/stop the layer tracing |
| 6356 | case 1025: { |
| 6357 | return NAME_NOT_FOUND; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 6358 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 6359 | // Deprecated, execute "adb shell perfetto --query" to see the ongoing tracing sessions |
| 6360 | case 1026: { |
| 6361 | return NAME_NOT_FOUND; |
Vishnu Nair | 87704c9 | 2018-01-08 15:32:57 -0800 | [diff] [blame] | 6362 | } |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 6363 | // Is a DisplayColorSetting supported? |
| 6364 | case 1027: { |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 6365 | const auto display = getDefaultDisplayDevice(); |
| 6366 | if (!display) { |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 6367 | return NAME_NOT_FOUND; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 6368 | } |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 6369 | |
| 6370 | DisplayColorSetting setting = static_cast<DisplayColorSetting>(data.readInt32()); |
| 6371 | switch (setting) { |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 6372 | case DisplayColorSetting::kManaged: |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 6373 | case DisplayColorSetting::kUnmanaged: |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 6374 | reply->writeBool(true); |
| 6375 | break; |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 6376 | case DisplayColorSetting::kEnhanced: |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 6377 | reply->writeBool(display->hasRenderIntent(RenderIntent::ENHANCE)); |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 6378 | break; |
| 6379 | default: // vendor display color setting |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 6380 | reply->writeBool( |
| 6381 | display->hasRenderIntent(static_cast<RenderIntent>(setting))); |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 6382 | break; |
| 6383 | } |
| 6384 | return NO_ERROR; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 6385 | } |
Alec Mouri | 9c58bcd | 2020-09-09 18:57:07 -0700 | [diff] [blame] | 6386 | case 1028: { // Unused. |
| 6387 | return NAME_NOT_FOUND; |
Steven Thomas | 97f1f4c | 2018-06-01 12:04:16 -0700 | [diff] [blame] | 6388 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 6389 | // Deprecated, use perfetto to set the active layer tracing buffer size |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 6390 | case 1029: { |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 6391 | return NAME_NOT_FOUND; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 6392 | } |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 6393 | // Is device color managed? |
| 6394 | case 1030: { |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 6395 | // ColorDisplayManager stil calls this |
| 6396 | reply->writeBool(true); |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 6397 | return NO_ERROR; |
| 6398 | } |
Peiyong Lin | 9d846a5 | 2018-11-05 13:18:20 -0800 | [diff] [blame] | 6399 | // Override default composition data space |
| 6400 | // adb shell service call SurfaceFlinger 1031 i32 1 DATASPACE_NUMBER DATASPACE_NUMBER \ |
| 6401 | // && adb shell stop zygote && adb shell start zygote |
| 6402 | // to restore: adb shell service call SurfaceFlinger 1031 i32 0 && \ |
| 6403 | // adb shell stop zygote && adb shell start zygote |
| 6404 | case 1031: { |
| 6405 | Mutex::Autolock _l(mStateLock); |
| 6406 | n = data.readInt32(); |
| 6407 | if (n) { |
| 6408 | n = data.readInt32(); |
| 6409 | if (n) { |
| 6410 | Dataspace dataspace = static_cast<Dataspace>(n); |
| 6411 | if (!validateCompositionDataspace(dataspace)) { |
| 6412 | return BAD_VALUE; |
| 6413 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6414 | mDefaultCompositionDataspace = dataspace; |
Peiyong Lin | 9d846a5 | 2018-11-05 13:18:20 -0800 | [diff] [blame] | 6415 | } |
| 6416 | n = data.readInt32(); |
| 6417 | if (n) { |
| 6418 | Dataspace dataspace = static_cast<Dataspace>(n); |
| 6419 | if (!validateCompositionDataspace(dataspace)) { |
| 6420 | return BAD_VALUE; |
| 6421 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6422 | mWideColorGamutCompositionDataspace = dataspace; |
Peiyong Lin | 9d846a5 | 2018-11-05 13:18:20 -0800 | [diff] [blame] | 6423 | } |
| 6424 | } else { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6425 | // restore composition data space. |
| 6426 | mDefaultCompositionDataspace = defaultCompositionDataspace; |
| 6427 | mWideColorGamutCompositionDataspace = wideColorGamutCompositionDataspace; |
Peiyong Lin | 9d846a5 | 2018-11-05 13:18:20 -0800 | [diff] [blame] | 6428 | } |
| 6429 | return NO_ERROR; |
| 6430 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 6431 | // Deprecated, use perfetto to set layer trace flags |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 6432 | case 1033: { |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 6433 | return NAME_NOT_FOUND; |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 6434 | } |
Ady Abraham | 03b02dd | 2019-03-21 15:40:11 -0700 | [diff] [blame] | 6435 | case 1034: { |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 6436 | n = data.readInt32(); |
| 6437 | if (n == 0 || n == 1) { |
| 6438 | sfdo_enableRefreshRateOverlay(static_cast<bool>(n)); |
| 6439 | } else { |
| 6440 | Mutex::Autolock lock(mStateLock); |
| 6441 | reply->writeBool(isRefreshRateOverlayEnabled()); |
| 6442 | } |
Ady Abraham | 03b02dd | 2019-03-21 15:40:11 -0700 | [diff] [blame] | 6443 | return NO_ERROR; |
| 6444 | } |
Ady Abraham | 34392f7 | 2019-04-10 11:29:27 -0700 | [diff] [blame] | 6445 | case 1035: { |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 6446 | // Parameters: |
| 6447 | // - (required) i32 mode id. |
| 6448 | // - (optional) i64 display id. Using default display if not provided. |
| 6449 | // - (optional) f min render rate. Using mode's fps is not provided. |
| 6450 | // - (optional) f max render rate. Using mode's fps is not provided. |
| 6451 | |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 6452 | const int modeId = data.readInt32(); |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 6453 | |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 6454 | const auto display = [&]() -> sp<IBinder> { |
| 6455 | uint64_t value; |
| 6456 | if (data.readUint64(&value) != NO_ERROR) { |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 6457 | return getDefaultDisplayDevice()->getDisplayToken().promote(); |
Ady Abraham | fb85366 | 2021-04-09 11:46:40 -0700 | [diff] [blame] | 6458 | } |
| 6459 | |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 6460 | if (const auto id = DisplayId::fromValue<PhysicalDisplayId>(value)) { |
| 6461 | return getPhysicalDisplayToken(*id); |
| 6462 | } |
| 6463 | |
| 6464 | ALOGE("Invalid physical display ID"); |
| 6465 | return nullptr; |
Ady Abraham | fb85366 | 2021-04-09 11:46:40 -0700 | [diff] [blame] | 6466 | }(); |
| 6467 | |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 6468 | const auto getFps = [&] { |
| 6469 | float value; |
| 6470 | if (data.readFloat(&value) == NO_ERROR) { |
| 6471 | return Fps::fromValue(value); |
| 6472 | } |
| 6473 | |
| 6474 | return Fps(); |
| 6475 | }; |
| 6476 | |
| 6477 | const auto minFps = getFps(); |
| 6478 | const auto maxFps = getFps(); |
| 6479 | |
Shiyong Li | 2bcece9 | 2021-08-30 20:38:50 +0000 | [diff] [blame] | 6480 | mDebugDisplayModeSetByBackdoor = false; |
Ady Abraham | 3802726 | 2023-10-17 16:58:25 -0700 | [diff] [blame] | 6481 | const status_t result = |
| 6482 | setActiveModeFromBackdoor(display, DisplayModeId{modeId}, minFps, maxFps); |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 6483 | mDebugDisplayModeSetByBackdoor = result == NO_ERROR; |
| 6484 | return result; |
Ady Abraham | 34392f7 | 2019-04-10 11:29:27 -0700 | [diff] [blame] | 6485 | } |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 6486 | // Turn on/off frame rate flexibility mode. When turned on it overrides the display |
| 6487 | // manager frame rate policy a new policy which allows switching between all refresh |
| 6488 | // rates. |
Ady Abraham | 07e5470 | 2020-04-16 15:05:37 -0700 | [diff] [blame] | 6489 | case 1036: { |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 6490 | if (data.readInt32() > 0) { // turn on |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6491 | return mScheduler |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 6492 | ->schedule([this]() FTL_FAKE_GUARD(kMainThreadContext) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6493 | const auto display = |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 6494 | FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked()); |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 6495 | |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6496 | // This is a little racy, but not in a way that hurts anything. As |
| 6497 | // we grab the defaultMode from the display manager policy, we could |
| 6498 | // be setting a new display manager policy, leaving us using a stale |
| 6499 | // defaultMode. The defaultMode doesn't matter for the override |
| 6500 | // policy though, since we set allowGroupSwitching to true, so it's |
| 6501 | // not a problem. |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 6502 | scheduler::RefreshRateSelector::OverridePolicy overridePolicy; |
| 6503 | overridePolicy.defaultMode = display->refreshRateSelector() |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6504 | .getDisplayManagerPolicy() |
| 6505 | .defaultMode; |
| 6506 | overridePolicy.allowGroupSwitching = true; |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 6507 | return setDesiredDisplayModeSpecsInternal(display, overridePolicy); |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6508 | }) |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 6509 | .get(); |
| 6510 | } else { // turn off |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6511 | return mScheduler |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 6512 | ->schedule([this]() FTL_FAKE_GUARD(kMainThreadContext) { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6513 | const auto display = |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 6514 | FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked()); |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 6515 | return setDesiredDisplayModeSpecsInternal( |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 6516 | display, |
| 6517 | scheduler::RefreshRateSelector::NoOverridePolicy{}); |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6518 | }) |
Marin Shalamanov | fa39ee7 | 2021-11-01 14:04:27 +0100 | [diff] [blame] | 6519 | .get(); |
Ady Abraham | 07e5470 | 2020-04-16 15:05:37 -0700 | [diff] [blame] | 6520 | } |
Ady Abraham | 07e5470 | 2020-04-16 15:05:37 -0700 | [diff] [blame] | 6521 | } |
Marin Shalamanov | e6332b0 | 2020-09-03 11:30:37 +0200 | [diff] [blame] | 6522 | // Inject a hotplug connected event for the primary display. This will deallocate and |
| 6523 | // reallocate the display state including framebuffers. |
| 6524 | case 1037: { |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 6525 | const hal::HWDisplayId hwcId = |
| 6526 | (Mutex::Autolock(mStateLock), getHwComposer().getPrimaryHwcDisplayId()); |
| 6527 | |
Huihong Luo | 791bef9 | 2023-09-19 21:18:45 -0700 | [diff] [blame] | 6528 | onComposerHalHotplugEvent(hwcId, DisplayHotplugEvent::CONNECTED); |
Marin Shalamanov | e6332b0 | 2020-09-03 11:30:37 +0200 | [diff] [blame] | 6529 | return NO_ERROR; |
| 6530 | } |
Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 6531 | // Modify the max number of display frames stored within FrameTimeline |
| 6532 | case 1038: { |
| 6533 | n = data.readInt32(); |
| 6534 | if (n < 0 || n > MAX_ALLOWED_DISPLAY_FRAMES) { |
| 6535 | ALOGW("Invalid max size. Maximum allowed is %d", MAX_ALLOWED_DISPLAY_FRAMES); |
| 6536 | return BAD_VALUE; |
| 6537 | } |
| 6538 | if (n == 0) { |
| 6539 | // restore to default |
| 6540 | mFrameTimeline->reset(); |
| 6541 | return NO_ERROR; |
| 6542 | } |
| 6543 | mFrameTimeline->setMaxDisplayFrames(n); |
| 6544 | return NO_ERROR; |
| 6545 | } |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 6546 | case 1039: { |
Dominik Laskowski | 1d7be06 | 2024-07-24 13:59:07 -0400 | [diff] [blame] | 6547 | const auto uid = static_cast<uid_t>(data.readInt32()); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 6548 | const auto refreshRate = data.readFloat(); |
Dominik Laskowski | 1d7be06 | 2024-07-24 13:59:07 -0400 | [diff] [blame] | 6549 | mScheduler->setPreferredRefreshRateForUid(FrameRateOverride{uid, refreshRate}); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 6550 | return NO_ERROR; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 6551 | } |
Alec Mouri | dd8bf2d | 2021-05-08 16:36:33 -0700 | [diff] [blame] | 6552 | // Toggle caching feature |
| 6553 | // First argument is an int32 - nonzero enables caching and zero disables caching |
| 6554 | // Second argument is an optional uint64 - if present, then limits enabling/disabling |
| 6555 | // caching to a particular physical display |
| 6556 | case 1040: { |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6557 | auto future = mScheduler->schedule([&] { |
| 6558 | n = data.readInt32(); |
| 6559 | std::optional<PhysicalDisplayId> inputId = std::nullopt; |
| 6560 | if (uint64_t inputDisplayId; data.readUint64(&inputDisplayId) == NO_ERROR) { |
| 6561 | inputId = DisplayId::fromValue<PhysicalDisplayId>(inputDisplayId); |
| 6562 | if (!inputId || getPhysicalDisplayToken(*inputId)) { |
| 6563 | ALOGE("No display with id: %" PRIu64, inputDisplayId); |
| 6564 | return NAME_NOT_FOUND; |
| 6565 | } |
| 6566 | } |
| 6567 | { |
| 6568 | Mutex::Autolock lock(mStateLock); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6569 | mLayerCachingEnabled = n != 0; |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6570 | for (const auto& [_, display] : mDisplays) { |
| 6571 | if (!inputId || *inputId == display->getPhysicalId()) { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 6572 | display->enableLayerCaching(mLayerCachingEnabled); |
Alec Mouri | cdd722b | 2021-05-19 14:29:05 -0700 | [diff] [blame] | 6573 | } |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6574 | } |
| 6575 | } |
| 6576 | return OK; |
| 6577 | }); |
Alec Mouri | cdd722b | 2021-05-19 14:29:05 -0700 | [diff] [blame] | 6578 | |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 6579 | if (const status_t error = future.get(); error != OK) { |
Alec Mouri | cdd722b | 2021-05-19 14:29:05 -0700 | [diff] [blame] | 6580 | return error; |
Alec Mouri | dd8bf2d | 2021-05-08 16:36:33 -0700 | [diff] [blame] | 6581 | } |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 6582 | scheduleRepaint(); |
Alec Mouri | dd8bf2d | 2021-05-08 16:36:33 -0700 | [diff] [blame] | 6583 | return NO_ERROR; |
| 6584 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 6585 | case 1041: { // Transaction tracing |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 6586 | if (mTransactionTracing) { |
Vishnu Nair | 640261c | 2023-09-21 21:22:51 -0700 | [diff] [blame] | 6587 | int arg = data.readInt32(); |
| 6588 | if (arg == -1) { |
Vishnu Nair | 50858e6 | 2023-10-13 23:28:24 +0000 | [diff] [blame] | 6589 | mScheduler->schedule([&]() { mTransactionTracing.reset(); }).get(); |
Vishnu Nair | 640261c | 2023-09-21 21:22:51 -0700 | [diff] [blame] | 6590 | } else if (arg > 0) { |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 6591 | // Transaction tracing is always running but allow the user to temporarily |
| 6592 | // increase the buffer when actively debugging. |
| 6593 | mTransactionTracing->setBufferSize( |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 6594 | TransactionTracing::LEGACY_ACTIVE_TRACING_BUFFER_SIZE); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 6595 | } else { |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 6596 | TransactionTraceWriter::getInstance().invoke("", /* overwrite= */ true); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 6597 | mTransactionTracing->setBufferSize( |
| 6598 | TransactionTracing::CONTINUOUS_TRACING_BUFFER_SIZE); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 6599 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 6600 | } |
| 6601 | reply->writeInt32(NO_ERROR); |
| 6602 | return NO_ERROR; |
| 6603 | } |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 6604 | case 1042: { // Write transaction trace to file |
Nataniel Borges | 533c2f8 | 2022-01-21 12:07:02 +0100 | [diff] [blame] | 6605 | if (mTransactionTracing) { |
| 6606 | mTransactionTracing->writeToFile(); |
| 6607 | } |
Nataniel Borges | 533c2f8 | 2022-01-21 12:07:02 +0100 | [diff] [blame] | 6608 | reply->writeInt32(NO_ERROR); |
| 6609 | return NO_ERROR; |
| 6610 | } |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 6611 | // hdr sdr ratio overlay |
| 6612 | case 1043: { |
| 6613 | auto future = mScheduler->schedule( |
| 6614 | [&]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD(kMainThreadContext) { |
| 6615 | n = data.readInt32(); |
Sally Qi | 0cd9e86 | 2024-05-15 18:37:02 -0700 | [diff] [blame] | 6616 | if (n == 0 || n == 1) { |
| 6617 | mHdrSdrRatioOverlay = n != 0; |
| 6618 | enableHdrSdrRatioOverlay(mHdrSdrRatioOverlay); |
| 6619 | } else { |
| 6620 | reply->writeBool(isHdrSdrRatioOverlayEnabled()); |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 6621 | } |
| 6622 | }); |
| 6623 | future.wait(); |
| 6624 | return NO_ERROR; |
| 6625 | } |
Brian Johnson | 51d8d5d | 2023-07-28 10:18:47 -0700 | [diff] [blame] | 6626 | |
| 6627 | case 1044: { // Enable/Disable mirroring from one display to another |
| 6628 | /* |
| 6629 | * Mirror one display onto another. |
| 6630 | * Ensure the source and destination displays are on. |
| 6631 | * Commands: |
| 6632 | * 0: Mirror one display to another |
| 6633 | * 1: Disable mirroring to a previously mirrored display |
| 6634 | * 2: Disable mirroring on previously mirrored displays |
| 6635 | * |
| 6636 | * Ex: |
| 6637 | * Get the display ids: |
| 6638 | * adb shell dumpsys SurfaceFlinger --display-id |
| 6639 | * Mirror first display to the second: |
| 6640 | * adb shell service call SurfaceFlinger 1044 i64 0 i64 4619827677550801152 i64 |
| 6641 | * 4619827677550801153 |
| 6642 | * Stop mirroring: |
| 6643 | * adb shell service call SurfaceFlinger 1044 i64 1 |
| 6644 | */ |
| 6645 | |
| 6646 | int64_t arg0 = data.readInt64(); |
| 6647 | |
| 6648 | switch (arg0) { |
| 6649 | case 0: { |
| 6650 | // Mirror arg1 to arg2 |
| 6651 | int64_t arg1 = data.readInt64(); |
| 6652 | int64_t arg2 = data.readInt64(); |
| 6653 | // Enable mirroring for one display |
| 6654 | const auto display1id = DisplayId::fromValue(arg1); |
| 6655 | auto mirrorRoot = SurfaceComposerClient::getDefault()->mirrorDisplay( |
| 6656 | display1id.value()); |
| 6657 | auto id2 = DisplayId::fromValue<PhysicalDisplayId>(arg2); |
| 6658 | const auto token2 = getPhysicalDisplayToken(*id2); |
| 6659 | ui::LayerStack layerStack; |
| 6660 | { |
| 6661 | Mutex::Autolock lock(mStateLock); |
| 6662 | sp<DisplayDevice> display = getDisplayDeviceLocked(token2); |
| 6663 | layerStack = display->getLayerStack(); |
| 6664 | } |
| 6665 | SurfaceComposerClient::Transaction t; |
| 6666 | t.setDisplayLayerStack(token2, layerStack); |
| 6667 | t.setLayer(mirrorRoot, INT_MAX); // Top-most layer |
| 6668 | t.setLayerStack(mirrorRoot, layerStack); |
| 6669 | t.apply(); |
| 6670 | |
| 6671 | mMirrorMapForDebug.emplace_or_replace(arg2, mirrorRoot); |
| 6672 | break; |
| 6673 | } |
| 6674 | |
| 6675 | case 1: { |
| 6676 | // Disable mirroring for arg1 |
| 6677 | int64_t arg1 = data.readInt64(); |
| 6678 | mMirrorMapForDebug.erase(arg1); |
| 6679 | break; |
| 6680 | } |
| 6681 | |
| 6682 | case 2: { |
| 6683 | // Disable mirroring for all displays |
| 6684 | mMirrorMapForDebug.clear(); |
| 6685 | break; |
| 6686 | } |
| 6687 | |
| 6688 | default: |
| 6689 | return BAD_VALUE; |
| 6690 | } |
| 6691 | return NO_ERROR; |
| 6692 | } |
Alec Mouri | 1c7938e | 2023-09-22 04:17:23 +0000 | [diff] [blame] | 6693 | // Inject jank |
| 6694 | // First argument is a float that describes the fraction of frame duration to jank by. |
| 6695 | // Second argument is a delay in ms for triggering the jank. This is useful for working |
| 6696 | // with tools that steal the adb connection. This argument is optional. |
| 6697 | case 1045: { |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 6698 | if (FlagManager::getInstance().vrr_config()) { |
Alec Mouri | 1c7938e | 2023-09-22 04:17:23 +0000 | [diff] [blame] | 6699 | float jankAmount = data.readFloat(); |
| 6700 | int32_t jankDelayMs = 0; |
| 6701 | if (data.readInt32(&jankDelayMs) != NO_ERROR) { |
| 6702 | jankDelayMs = 0; |
| 6703 | } |
| 6704 | |
| 6705 | const auto jankDelayDuration = Duration(std::chrono::milliseconds(jankDelayMs)); |
| 6706 | |
| 6707 | const bool jankAmountValid = jankAmount > 0.0 && jankAmount < 100.0; |
| 6708 | |
| 6709 | if (!jankAmountValid) { |
| 6710 | ALOGD("Ignoring invalid jank amount: %f", jankAmount); |
| 6711 | reply->writeInt32(BAD_VALUE); |
| 6712 | return BAD_VALUE; |
| 6713 | } |
| 6714 | |
| 6715 | (void)mScheduler->scheduleDelayed( |
| 6716 | [&, jankAmount]() FTL_FAKE_GUARD(kMainThreadContext) { |
| 6717 | mScheduler->injectPacesetterDelay(jankAmount); |
| 6718 | scheduleComposite(FrameHint::kActive); |
| 6719 | }, |
| 6720 | jankDelayDuration.ns()); |
| 6721 | reply->writeInt32(NO_ERROR); |
| 6722 | return NO_ERROR; |
| 6723 | } |
| 6724 | return err; |
| 6725 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6726 | } |
| 6727 | } |
| 6728 | return err; |
| 6729 | } |
| 6730 | |
Ady Abraham | a09852a | 2020-02-20 14:23:42 -0800 | [diff] [blame] | 6731 | void SurfaceFlinger::kernelTimerChanged(bool expired) { |
| 6732 | static bool updateOverlay = |
| 6733 | property_get_bool("debug.sf.kernel_idle_timer_update_overlay", true); |
Dominik Laskowski | 2013464 | 2020-04-20 22:36:44 -0700 | [diff] [blame] | 6734 | if (!updateOverlay) return; |
Ady Abraham | a09852a | 2020-02-20 14:23:42 -0800 | [diff] [blame] | 6735 | |
| 6736 | // Update the overlay on the main thread to avoid race conditions with |
Dominik Laskowski | 63abc2b | 2023-12-21 19:33:25 +0000 | [diff] [blame] | 6737 | // RefreshRateSelector::getActiveMode |
Dominik Laskowski | 4383967 | 2024-08-04 02:01:48 -0400 | [diff] [blame] | 6738 | static_cast<void>(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(kMainThreadContext) { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 6739 | const auto display = FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked()); |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 6740 | if (!display) { |
| 6741 | ALOGW("%s: default display is null", __func__); |
| 6742 | return; |
| 6743 | } |
Adrian Salido | a942af8 | 2022-02-17 19:22:31 -0800 | [diff] [blame] | 6744 | if (!display->isRefreshRateOverlayEnabled()) return; |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 6745 | |
Dominik Laskowski | 4383967 | 2024-08-04 02:01:48 -0400 | [diff] [blame] | 6746 | const auto state = mDisplayModeController.getKernelIdleTimerState(display->getPhysicalId()); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 6747 | |
Dominik Laskowski | 4383967 | 2024-08-04 02:01:48 -0400 | [diff] [blame] | 6748 | if (display->onKernelTimerChanged(state.desiredModeIdOpt, state.isEnabled && expired)) { |
Dominik Laskowski | 46f3e3b | 2021-08-10 11:44:24 -0700 | [diff] [blame] | 6749 | mScheduler->scheduleFrame(); |
Ady Abraham | a09852a | 2020-02-20 14:23:42 -0800 | [diff] [blame] | 6750 | } |
| 6751 | })); |
| 6752 | } |
| 6753 | |
Ady Abraham | eba039c | 2024-07-10 17:45:29 -0700 | [diff] [blame] | 6754 | void SurfaceFlinger::vrrDisplayIdle(bool idle) { |
| 6755 | // Update the overlay on the main thread to avoid race conditions with |
| 6756 | // RefreshRateSelector::getActiveMode |
| 6757 | static_cast<void>(mScheduler->schedule([=, this] { |
| 6758 | const auto display = FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked()); |
| 6759 | if (!display) { |
| 6760 | ALOGW("%s: default display is null", __func__); |
| 6761 | return; |
| 6762 | } |
| 6763 | if (!display->isRefreshRateOverlayEnabled()) return; |
| 6764 | |
| 6765 | display->onVrrIdle(idle); |
| 6766 | mScheduler->scheduleFrame(); |
| 6767 | })); |
| 6768 | } |
| 6769 | |
Dominik Laskowski | 4383967 | 2024-08-04 02:01:48 -0400 | [diff] [blame] | 6770 | auto SurfaceFlinger::getKernelIdleTimerProperties(PhysicalDisplayId displayId) |
| 6771 | -> std::pair<std::optional<KernelIdleTimerController>, std::chrono::milliseconds> { |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 6772 | const bool isKernelIdleTimerHwcSupported = getHwComposer().getComposer()->isSupported( |
| 6773 | android::Hwc2::Composer::OptionalFeature::KernelIdleTimer); |
| 6774 | const auto timeout = getIdleTimerTimeout(displayId); |
| 6775 | if (isKernelIdleTimerHwcSupported) { |
Dominik Laskowski | 6e46515 | 2022-09-28 11:00:25 -0400 | [diff] [blame] | 6776 | if (getHwComposer().hasDisplayIdleTimerCapability(displayId)) { |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 6777 | // In order to decide if we can use the HWC api for idle timer |
| 6778 | // we query DisplayCapability::DISPLAY_IDLE_TIMER directly on the composer |
| 6779 | // without relying on hasDisplayCapability. |
| 6780 | // hasDisplayCapability relies on DisplayCapabilities |
| 6781 | // which are updated after we set the PowerMode::ON. |
| 6782 | // DISPLAY_IDLE_TIMER is a display driver property |
| 6783 | // and is available before the PowerMode::ON |
| 6784 | return {KernelIdleTimerController::HwcApi, timeout}; |
| 6785 | } |
| 6786 | return {std::nullopt, timeout}; |
| 6787 | } |
| 6788 | if (getKernelIdleTimerSyspropConfig(displayId)) { |
| 6789 | return {KernelIdleTimerController::Sysprop, timeout}; |
| 6790 | } |
| 6791 | |
| 6792 | return {std::nullopt, timeout}; |
| 6793 | } |
| 6794 | |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 6795 | // A simple RAII class to disconnect from an ANativeWindow* when it goes out of scope |
| 6796 | class WindowDisconnector { |
Mathias Agopian | 2ed0fe5 | 2013-07-08 17:09:41 -0700 | [diff] [blame] | 6797 | public: |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 6798 | WindowDisconnector(ANativeWindow* window, int api) : mWindow(window), mApi(api) {} |
| 6799 | ~WindowDisconnector() { |
| 6800 | native_window_api_disconnect(mWindow, mApi); |
Mathias Agopian | 2ed0fe5 | 2013-07-08 17:09:41 -0700 | [diff] [blame] | 6801 | } |
| 6802 | |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 6803 | private: |
| 6804 | ANativeWindow* mWindow; |
| 6805 | const int mApi; |
Mathias Agopian | 2ed0fe5 | 2013-07-08 17:09:41 -0700 | [diff] [blame] | 6806 | }; |
| 6807 | |
chaviw | f5bb97b | 2021-04-28 15:35:37 -0500 | [diff] [blame] | 6808 | static bool hasCaptureBlackoutContentPermission() { |
| 6809 | IPCThreadState* ipc = IPCThreadState::self(); |
| 6810 | const int pid = ipc->getCallingPid(); |
| 6811 | const int uid = ipc->getCallingUid(); |
| 6812 | return uid == AID_GRAPHICS || uid == AID_SYSTEM || |
| 6813 | PermissionCache::checkPermission(sCaptureBlackoutContent, pid, uid); |
| 6814 | } |
| 6815 | |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 6816 | static status_t validateScreenshotPermissions(const CaptureArgs& captureArgs) { |
| 6817 | IPCThreadState* ipc = IPCThreadState::self(); |
| 6818 | const int pid = ipc->getCallingPid(); |
| 6819 | const int uid = ipc->getCallingUid(); |
Huihong Luo | fcadcff | 2024-08-26 17:22:57 +0000 | [diff] [blame] | 6820 | if (uid == AID_GRAPHICS || uid == AID_SYSTEM || |
| 6821 | PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) { |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 6822 | return OK; |
| 6823 | } |
| 6824 | |
| 6825 | // If the caller doesn't have the correct permissions but is only attempting to screenshot |
| 6826 | // itself, we allow it to continue. |
| 6827 | if (captureArgs.uid == uid) { |
| 6828 | return OK; |
| 6829 | } |
| 6830 | |
| 6831 | ALOGE("Permission Denial: can't take screenshot pid=%d, uid=%d", pid, uid); |
| 6832 | return PERMISSION_DENIED; |
| 6833 | } |
| 6834 | |
Peiyong Lin | 05cc011 | 2020-10-14 16:16:37 -0700 | [diff] [blame] | 6835 | status_t SurfaceFlinger::setSchedFifo(bool enabled) { |
| 6836 | static constexpr int kFifoPriority = 2; |
| 6837 | static constexpr int kOtherPriority = 0; |
| 6838 | |
| 6839 | struct sched_param param = {0}; |
| 6840 | int sched_policy; |
| 6841 | if (enabled) { |
| 6842 | sched_policy = SCHED_FIFO; |
| 6843 | param.sched_priority = kFifoPriority; |
| 6844 | } else { |
| 6845 | sched_policy = SCHED_OTHER; |
| 6846 | param.sched_priority = kOtherPriority; |
| 6847 | } |
| 6848 | |
| 6849 | if (sched_setscheduler(0, sched_policy, ¶m) != 0) { |
| 6850 | return -errno; |
| 6851 | } |
Wei Wang | 23aa5a6 | 2021-06-04 15:56:57 -0700 | [diff] [blame] | 6852 | |
| 6853 | return NO_ERROR; |
| 6854 | } |
| 6855 | |
| 6856 | status_t SurfaceFlinger::setSchedAttr(bool enabled) { |
| 6857 | static const unsigned int kUclampMin = |
Dominik Laskowski | dd374e1 | 2024-02-21 10:28:15 -0500 | [diff] [blame] | 6858 | base::GetUintProperty<unsigned int>("ro.surface_flinger.uclamp.min"s, 0U); |
Wei Wang | 23aa5a6 | 2021-06-04 15:56:57 -0700 | [diff] [blame] | 6859 | |
| 6860 | if (!kUclampMin) { |
| 6861 | // uclamp.min set to 0 (default), skip setting |
| 6862 | return NO_ERROR; |
| 6863 | } |
| 6864 | |
Wei Wang | 23aa5a6 | 2021-06-04 15:56:57 -0700 | [diff] [blame] | 6865 | sched_attr attr = {}; |
| 6866 | attr.size = sizeof(attr); |
| 6867 | |
| 6868 | attr.sched_flags = (SCHED_FLAG_KEEP_ALL | SCHED_FLAG_UTIL_CLAMP); |
| 6869 | attr.sched_util_min = enabled ? kUclampMin : 0; |
| 6870 | attr.sched_util_max = 1024; |
| 6871 | |
| 6872 | if (syscall(__NR_sched_setattr, 0, &attr, 0)) { |
| 6873 | return -errno; |
| 6874 | } |
| 6875 | |
Peiyong Lin | 05cc011 | 2020-10-14 16:16:37 -0700 | [diff] [blame] | 6876 | return NO_ERROR; |
| 6877 | } |
| 6878 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 6879 | namespace { |
| 6880 | |
Melody Hsu | 683fe04 | 2024-06-04 17:22:04 +0000 | [diff] [blame] | 6881 | ui::Dataspace pickBestDataspace(ui::Dataspace requestedDataspace, |
| 6882 | const compositionengine::impl::OutputCompositionState& state, |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 6883 | bool capturingHdrLayers, bool hintForSeamlessTransition) { |
Melody Hsu | 683fe04 | 2024-06-04 17:22:04 +0000 | [diff] [blame] | 6884 | if (requestedDataspace != ui::Dataspace::UNKNOWN) { |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 6885 | return requestedDataspace; |
| 6886 | } |
| 6887 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 6888 | const auto dataspaceForColorMode = ui::pickDataspaceFor(state.colorMode); |
| 6889 | |
Alec Mouri | 2964072 | 2023-05-16 23:22:41 +0000 | [diff] [blame] | 6890 | // TODO: Enable once HDR screenshots are ready. |
| 6891 | if constexpr (/* DISABLES CODE */ (false)) { |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 6892 | // For now since we only support 8-bit screenshots, just use HLG and |
| 6893 | // assume that 1.0 >= display max luminance. This isn't quite as future |
| 6894 | // proof as PQ is, but is good enough. |
| 6895 | // Consider using PQ once we support 16-bit screenshots and we're able |
| 6896 | // to consistently supply metadata to image encoders. |
| 6897 | return ui::Dataspace::BT2020_HLG; |
| 6898 | } |
| 6899 | |
| 6900 | return dataspaceForColorMode; |
| 6901 | } |
| 6902 | |
| 6903 | } // namespace |
| 6904 | |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 6905 | static void invokeScreenCaptureError(const status_t status, |
| 6906 | const sp<IScreenCaptureListener>& captureListener) { |
| 6907 | ScreenCaptureResults captureResults; |
| 6908 | captureResults.fenceResult = base::unexpected(status); |
| 6909 | captureListener->onScreenCaptureCompleted(captureResults); |
| 6910 | } |
| 6911 | |
| 6912 | void SurfaceFlinger::captureDisplay(const DisplayCaptureArgs& args, |
| 6913 | const sp<IScreenCaptureListener>& captureListener) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 6914 | SFTRACE_CALL(); |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 6915 | |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 6916 | const auto& captureArgs = args.captureArgs; |
| 6917 | status_t validate = validateScreenshotPermissions(captureArgs); |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 6918 | if (validate != OK) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 6919 | ALOGD("Permission denied to captureDisplay"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 6920 | invokeScreenCaptureError(validate, captureListener); |
| 6921 | return; |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 6922 | } |
| 6923 | |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 6924 | if (!args.displayToken) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 6925 | ALOGD("Invalid display token to captureDisplay"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 6926 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 6927 | return; |
| 6928 | } |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 6929 | |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 6930 | if (captureArgs.captureSecureLayers && !hasCaptureBlackoutContentPermission()) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 6931 | ALOGD("Attempting to capture secure layers without CAPTURE_BLACKOUT_CONTENT"); |
Chavi Weingarten | f5fb5b0 | 2023-11-30 17:27:52 +0000 | [diff] [blame] | 6932 | invokeScreenCaptureError(PERMISSION_DENIED, captureListener); |
| 6933 | return; |
| 6934 | } |
| 6935 | |
Dominik Laskowski | 7b9bf4c | 2021-03-18 12:21:29 -0700 | [diff] [blame] | 6936 | wp<const DisplayDevice> displayWeak; |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 6937 | ui::LayerStack layerStack; |
| 6938 | ui::Size reqSize(args.width, args.height); |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 6939 | std::unordered_set<uint32_t> excludeLayerIds; |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 6940 | { |
| 6941 | Mutex::Autolock lock(mStateLock); |
| 6942 | sp<DisplayDevice> display = getDisplayDeviceLocked(args.displayToken); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 6943 | if (!display) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 6944 | ALOGD("Unable to find display device for captureDisplay"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 6945 | invokeScreenCaptureError(NAME_NOT_FOUND, captureListener); |
| 6946 | return; |
| 6947 | } |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 6948 | displayWeak = display; |
| 6949 | layerStack = display->getLayerStack(); |
| 6950 | |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 6951 | // 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] | 6952 | if (args.width == 0 || args.height == 0) { |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 6953 | reqSize = display->getLayerStackSpaceRect().getSize(); |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 6954 | } |
| 6955 | |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 6956 | for (const auto& handle : captureArgs.excludeHandles) { |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 6957 | uint32_t excludeLayer = LayerHandle::getLayerId(handle); |
| 6958 | if (excludeLayer != UNASSIGNED_LAYER_ID) { |
| 6959 | excludeLayerIds.emplace(excludeLayer); |
| 6960 | } else { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 6961 | ALOGD("Invalid layer handle passed as excludeLayer to captureDisplay"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 6962 | invokeScreenCaptureError(NAME_NOT_FOUND, captureListener); |
| 6963 | return; |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 6964 | } |
| 6965 | } |
chaviw | d243289 | 2020-07-24 17:42:39 -0700 | [diff] [blame] | 6966 | } |
| 6967 | |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 6968 | GetLayerSnapshotsFunction getLayerSnapshotsFn = |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 6969 | getLayerSnapshotsForScreenshots(layerStack, captureArgs.uid, |
| 6970 | std::move(excludeLayerIds)); |
chaviw | 0390077 | 2020-08-18 12:34:51 -0700 | [diff] [blame] | 6971 | |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 6972 | ftl::Flags<RenderArea::Options> options; |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 6973 | if (captureArgs.captureSecureLayers) options |= RenderArea::Options::CAPTURE_SECURE_LAYERS; |
| 6974 | if (captureArgs.hintForSeamlessTransition) |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 6975 | options |= RenderArea::Options::HINT_FOR_SEAMLESS_TRANSITION; |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 6976 | captureScreenCommon(RenderAreaBuilderVariant(std::in_place_type<DisplayRenderAreaBuilder>, |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 6977 | gui::aidl_utils::fromARect(captureArgs.sourceCrop), |
| 6978 | reqSize, |
| 6979 | static_cast<ui::Dataspace>(captureArgs.dataspace), |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 6980 | displayWeak, options), |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 6981 | getLayerSnapshotsFn, reqSize, |
| 6982 | static_cast<ui::PixelFormat>(captureArgs.pixelFormat), |
| 6983 | captureArgs.allowProtected, captureArgs.grayscale, captureListener); |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 6984 | } |
| 6985 | |
John Reck | 7e4c487 | 2023-11-14 18:31:03 -0500 | [diff] [blame] | 6986 | void SurfaceFlinger::captureDisplay(DisplayId displayId, const CaptureArgs& args, |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 6987 | const sp<IScreenCaptureListener>& captureListener) { |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 6988 | ui::LayerStack layerStack; |
Dominik Laskowski | 7b9bf4c | 2021-03-18 12:21:29 -0700 | [diff] [blame] | 6989 | wp<const DisplayDevice> displayWeak; |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 6990 | ui::Size size; |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 6991 | { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 6992 | Mutex::Autolock lock(mStateLock); |
Dominik Laskowski | 7b9bf4c | 2021-03-18 12:21:29 -0700 | [diff] [blame] | 6993 | |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 6994 | const auto display = getDisplayDeviceLocked(displayId); |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 6995 | if (!display) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 6996 | ALOGD("Unable to find display device for captureDisplay"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 6997 | invokeScreenCaptureError(NAME_NOT_FOUND, captureListener); |
| 6998 | return; |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 6999 | } |
| 7000 | |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 7001 | displayWeak = display; |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 7002 | layerStack = display->getLayerStack(); |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 7003 | size = display->getLayerStackSpaceRect().getSize(); |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 7004 | } |
| 7005 | |
John Reck | 7e4c487 | 2023-11-14 18:31:03 -0500 | [diff] [blame] | 7006 | size.width *= args.frameScaleX; |
| 7007 | size.height *= args.frameScaleY; |
| 7008 | |
| 7009 | // We could query a real value for this but it'll be a long, long time until we support |
| 7010 | // displays that need upwards of 1GB per buffer so... |
| 7011 | constexpr auto kMaxTextureSize = 16384; |
| 7012 | if (size.width <= 0 || size.height <= 0 || size.width >= kMaxTextureSize || |
| 7013 | size.height >= kMaxTextureSize) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7014 | 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] | 7015 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 7016 | return; |
| 7017 | } |
| 7018 | |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 7019 | GetLayerSnapshotsFunction getLayerSnapshotsFn = |
| 7020 | getLayerSnapshotsForScreenshots(layerStack, CaptureArgs::UNSET_UID, |
| 7021 | /*snapshotFilterFn=*/nullptr); |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 7022 | |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 7023 | if (captureListener == nullptr) { |
| 7024 | ALOGE("capture screen must provide a capture listener callback"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7025 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 7026 | return; |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 7027 | } |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 7028 | |
| 7029 | constexpr bool kAllowProtected = false; |
| 7030 | constexpr bool kGrayscale = false; |
| 7031 | |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 7032 | ftl::Flags<RenderArea::Options> options; |
| 7033 | if (args.hintForSeamlessTransition) |
| 7034 | options |= RenderArea::Options::HINT_FOR_SEAMLESS_TRANSITION; |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7035 | captureScreenCommon(RenderAreaBuilderVariant(std::in_place_type<DisplayRenderAreaBuilder>, |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7036 | Rect(), size, |
| 7037 | static_cast<ui::Dataspace>(args.dataspace), |
| 7038 | displayWeak, options), |
| 7039 | getLayerSnapshotsFn, size, static_cast<ui::PixelFormat>(args.pixelFormat), |
| 7040 | kAllowProtected, kGrayscale, captureListener); |
chaviw | 93df2ea | 2019-04-30 16:45:12 -0700 | [diff] [blame] | 7041 | } |
| 7042 | |
Chavi Weingarten | f6fb445 | 2024-01-23 21:10:30 +0000 | [diff] [blame] | 7043 | ScreenCaptureResults SurfaceFlinger::captureLayersSync(const LayerCaptureArgs& args) { |
| 7044 | sp<SyncScreenCaptureListener> captureListener = sp<SyncScreenCaptureListener>::make(); |
| 7045 | captureLayers(args, captureListener); |
| 7046 | return captureListener->waitForResults(); |
| 7047 | } |
| 7048 | |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7049 | void SurfaceFlinger::captureLayers(const LayerCaptureArgs& args, |
| 7050 | const sp<IScreenCaptureListener>& captureListener) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 7051 | SFTRACE_CALL(); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 7052 | |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7053 | const auto& captureArgs = args.captureArgs; |
| 7054 | |
| 7055 | status_t validate = validateScreenshotPermissions(captureArgs); |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 7056 | if (validate != OK) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7057 | ALOGD("Permission denied to captureLayers"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7058 | invokeScreenCaptureError(validate, captureListener); |
| 7059 | return; |
chaviw | 4b9d5e1 | 2020-08-04 18:30:35 -0700 | [diff] [blame] | 7060 | } |
| 7061 | |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7062 | auto crop = gui::aidl_utils::fromARect(captureArgs.sourceCrop); |
| 7063 | |
Marin Shalamanov | 1c43429 | 2020-06-12 01:47:29 +0200 | [diff] [blame] | 7064 | ui::Size reqSize; |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 7065 | sp<Layer> parent; |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 7066 | std::unordered_set<uint32_t> excludeLayerIds; |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7067 | ui::Dataspace dataspace = static_cast<ui::Dataspace>(captureArgs.dataspace); |
chaviw | f5bb97b | 2021-04-28 15:35:37 -0500 | [diff] [blame] | 7068 | |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7069 | if (captureArgs.captureSecureLayers && !hasCaptureBlackoutContentPermission()) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7070 | ALOGD("Attempting to capture secure layers without CAPTURE_BLACKOUT_CONTENT"); |
Chavi Weingarten | f5fb5b0 | 2023-11-30 17:27:52 +0000 | [diff] [blame] | 7071 | invokeScreenCaptureError(PERMISSION_DENIED, captureListener); |
| 7072 | return; |
| 7073 | } |
chaviw | f5bb97b | 2021-04-28 15:35:37 -0500 | [diff] [blame] | 7074 | |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 7075 | { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 7076 | Mutex::Autolock lock(mStateLock); |
chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 7077 | |
Vishnu Nair | 07e2a48 | 2022-10-18 19:18:16 +0000 | [diff] [blame] | 7078 | parent = LayerHandle::getLayer(args.layerHandle); |
chaviw | 79468ab | 2021-10-27 11:11:24 -0500 | [diff] [blame] | 7079 | if (parent == nullptr) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7080 | ALOGD("captureLayers called with an invalid or removed parent"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7081 | invokeScreenCaptureError(NAME_NOT_FOUND, captureListener); |
| 7082 | return; |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 7083 | } |
| 7084 | |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 7085 | Rect parentSourceBounds = parent->getCroppedBufferSize(parent->getDrawingState()); |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7086 | if (crop.width() <= 0) { |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 7087 | crop.left = 0; |
Vishnu Nair | efc42e2 | 2019-12-03 17:36:12 -0800 | [diff] [blame] | 7088 | crop.right = parentSourceBounds.getWidth(); |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 7089 | } |
| 7090 | |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7091 | if (crop.height() <= 0) { |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 7092 | crop.top = 0; |
Vishnu Nair | efc42e2 | 2019-12-03 17:36:12 -0800 | [diff] [blame] | 7093 | crop.bottom = parentSourceBounds.getHeight(); |
| 7094 | } |
| 7095 | |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7096 | if (crop.isEmpty() || captureArgs.frameScaleX <= 0.0f || captureArgs.frameScaleY <= 0.0f) { |
Vishnu Nair | efc42e2 | 2019-12-03 17:36:12 -0800 | [diff] [blame] | 7097 | // Error out if the layer has no source bounds (i.e. they are boundless) and a source |
| 7098 | // crop was not specified, or an invalid frame scale was provided. |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7099 | ALOGD("Boundless layer, unspecified crop, or invalid frame scale to captureLayers"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7100 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 7101 | return; |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 7102 | } |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7103 | reqSize = ui::Size(crop.width() * captureArgs.frameScaleX, |
| 7104 | crop.height() * captureArgs.frameScaleY); |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 7105 | |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7106 | for (const auto& handle : captureArgs.excludeHandles) { |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 7107 | uint32_t excludeLayer = LayerHandle::getLayerId(handle); |
| 7108 | if (excludeLayer != UNASSIGNED_LAYER_ID) { |
| 7109 | excludeLayerIds.emplace(excludeLayer); |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 7110 | } else { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7111 | ALOGD("Invalid layer handle passed as excludeLayer to captureLayers"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7112 | invokeScreenCaptureError(NAME_NOT_FOUND, captureListener); |
| 7113 | return; |
Robert Carr | c0df312 | 2019-04-11 13:18:21 -0700 | [diff] [blame] | 7114 | } |
| 7115 | } |
| 7116 | } // mStateLock |
chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 7117 | |
Chia-I Wu | 20261cb | 2018-08-23 12:55:44 -0700 | [diff] [blame] | 7118 | // really small crop or frameScale |
chaviw | 79468ab | 2021-10-27 11:11:24 -0500 | [diff] [blame] | 7119 | if (reqSize.width <= 0 || reqSize.height <= 0) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7120 | ALOGD("Failed to captureLayers: crop or scale too small"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7121 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 7122 | return; |
Chia-I Wu | 20261cb | 2018-08-23 12:55:44 -0700 | [diff] [blame] | 7123 | } |
| 7124 | |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 7125 | std::optional<FloatRect> parentCrop = std::nullopt; |
| 7126 | if (args.childrenOnly) { |
| 7127 | parentCrop = crop.isEmpty() ? FloatRect(0, 0, reqSize.width, reqSize.height) |
| 7128 | : crop.toFloatRect(); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 7129 | } |
Robert Carr | 108b2c7 | 2019-04-02 16:32:58 -0700 | [diff] [blame] | 7130 | |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 7131 | GetLayerSnapshotsFunction getLayerSnapshotsFn = |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7132 | getLayerSnapshotsForScreenshots(parent->sequence, captureArgs.uid, |
| 7133 | std::move(excludeLayerIds), args.childrenOnly, |
| 7134 | parentCrop); |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 7135 | |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 7136 | if (captureListener == nullptr) { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 7137 | ALOGD("capture screen must provide a capture listener callback"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7138 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 7139 | return; |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 7140 | } |
| 7141 | |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 7142 | ftl::Flags<RenderArea::Options> options; |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7143 | if (captureArgs.captureSecureLayers) options |= RenderArea::Options::CAPTURE_SECURE_LAYERS; |
| 7144 | if (captureArgs.hintForSeamlessTransition) |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 7145 | options |= RenderArea::Options::HINT_FOR_SEAMLESS_TRANSITION; |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7146 | captureScreenCommon(RenderAreaBuilderVariant(std::in_place_type<LayerRenderAreaBuilder>, crop, |
Vishnu Nair | 871886e | 2024-06-26 17:17:51 -0700 | [diff] [blame] | 7147 | reqSize, dataspace, parent, args.childrenOnly, |
| 7148 | options), |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame] | 7149 | getLayerSnapshotsFn, reqSize, |
| 7150 | static_cast<ui::PixelFormat>(captureArgs.pixelFormat), |
| 7151 | captureArgs.allowProtected, captureArgs.grayscale, captureListener); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 7152 | } |
| 7153 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 7154 | // Creates a Future release fence for a layer and keeps track of it in a list to |
| 7155 | // release the buffer when the Future is complete. Calls from composittion |
| 7156 | // involve needing to refresh the composition start time for stats. |
| 7157 | void SurfaceFlinger::attachReleaseFenceFutureToLayer(Layer* layer, LayerFE* layerFE, |
| 7158 | ui::LayerStack layerStack) { |
| 7159 | ftl::Future<FenceResult> futureFence = layerFE->createReleaseFenceFuture(); |
Vishnu Nair | 620fefd | 2024-08-15 12:28:18 +0000 | [diff] [blame] | 7160 | layer->prepareReleaseCallbacks(std::move(futureFence), layerStack); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 7161 | } |
| 7162 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7163 | // Loop over all visible layers to see whether there's any protected layer. A protected layer is |
| 7164 | // typically a layer with DRM contents, or have the GRALLOC_USAGE_PROTECTED set on the buffer. |
| 7165 | // A protected layer has no implication on whether it's secure, which is explicitly set by |
| 7166 | // application to avoid being screenshot or drawn via unsecure display. |
| 7167 | bool SurfaceFlinger::layersHasProtectedLayer(const std::vector<sp<LayerFE>>& layers) const { |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 7168 | bool protectedLayerFound = false; |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7169 | for (auto& layerFE : layers) { |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 7170 | protectedLayerFound |= |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7171 | (layerFE->mSnapshot->isVisible && layerFE->mSnapshot->hasProtectedContent); |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 7172 | if (protectedLayerFound) { |
| 7173 | break; |
| 7174 | } |
| 7175 | } |
| 7176 | return protectedLayerFound; |
| 7177 | } |
| 7178 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7179 | // Getting layer snapshots and display should take place on main thread. |
| 7180 | // Accessing display requires mStateLock, and contention for this lock |
| 7181 | // is reduced when grabbed from the main thread, thus also reducing |
| 7182 | // risk of deadlocks. |
Vishnu Nair | 5a93ccb | 2024-06-26 00:38:32 +0000 | [diff] [blame] | 7183 | std::optional<SurfaceFlinger::OutputCompositionState> SurfaceFlinger::getSnapshotsFromMainThread( |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7184 | RenderAreaBuilderVariant& renderAreaBuilder, GetLayerSnapshotsFunction getLayerSnapshotsFn, |
| 7185 | std::vector<sp<LayerFE>>& layerFEs) { |
| 7186 | return mScheduler |
| 7187 | ->schedule([=, this, &renderAreaBuilder, &layerFEs]() REQUIRES(kMainThreadContext) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 7188 | SFTRACE_NAME("getSnapshotsFromMainThread"); |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7189 | auto layers = getLayerSnapshotsFn(); |
| 7190 | for (auto& [layer, layerFE] : layers) { |
| 7191 | attachReleaseFenceFutureToLayer(layer, layerFE.get(), ui::INVALID_LAYER_STACK); |
| 7192 | } |
| 7193 | layerFEs = extractLayerFEs(layers); |
| 7194 | return getDisplayStateFromRenderAreaBuilder(renderAreaBuilder); |
| 7195 | }) |
| 7196 | .get(); |
| 7197 | } |
| 7198 | |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7199 | void SurfaceFlinger::captureScreenCommon(RenderAreaBuilderVariant renderAreaBuilder, |
Melody Hsu | eddfe93 | 2024-06-04 18:10:41 +0000 | [diff] [blame] | 7200 | GetLayerSnapshotsFunction getLayerSnapshotsFn, |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7201 | ui::Size bufferSize, ui::PixelFormat reqPixelFormat, |
| 7202 | bool allowProtected, bool grayscale, |
| 7203 | const sp<IScreenCaptureListener>& captureListener) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 7204 | SFTRACE_CALL(); |
Mathias Agopian | 2a9fc49 | 2013-03-01 13:42:57 -0800 | [diff] [blame] | 7205 | |
Garfield Tan | 9c9c191 | 2021-07-19 12:02:16 -0700 | [diff] [blame] | 7206 | if (exceedsMaxRenderTargetSize(bufferSize.getWidth(), bufferSize.getHeight())) { |
| 7207 | ALOGE("Attempted to capture screen with size (%" PRId32 ", %" PRId32 |
| 7208 | ") that exceeds render target size limit.", |
| 7209 | bufferSize.getWidth(), bufferSize.getHeight()); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 7210 | invokeScreenCaptureError(BAD_VALUE, captureListener); |
| 7211 | return; |
Garfield Tan | 9c9c191 | 2021-07-19 12:02:16 -0700 | [diff] [blame] | 7212 | } |
| 7213 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7214 | if (FlagManager::getInstance().single_hop_screenshot() && |
Vishnu Nair | 5aadd24 | 2024-06-29 01:17:02 +0000 | [diff] [blame] | 7215 | FlagManager::getInstance().ce_fence_promise() && mRenderEngine->isThreaded()) { |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7216 | std::vector<sp<LayerFE>> layerFEs; |
| 7217 | auto displayState = |
Vishnu Nair | 5a93ccb | 2024-06-26 00:38:32 +0000 | [diff] [blame] | 7218 | getSnapshotsFromMainThread(renderAreaBuilder, getLayerSnapshotsFn, layerFEs); |
Alec Mouri | 7013b6f | 2021-02-12 11:16:54 -0800 | [diff] [blame] | 7219 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7220 | const bool supportsProtected = getRenderEngine().supportsProtectedContent(); |
| 7221 | bool hasProtectedLayer = false; |
| 7222 | if (allowProtected && supportsProtected) { |
| 7223 | hasProtectedLayer = layersHasProtectedLayer(layerFEs); |
| 7224 | } |
| 7225 | const bool isProtected = hasProtectedLayer && allowProtected && supportsProtected; |
| 7226 | const uint32_t usage = GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER | |
| 7227 | GRALLOC_USAGE_HW_TEXTURE | |
| 7228 | (isProtected ? GRALLOC_USAGE_PROTECTED |
| 7229 | : GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 7230 | sp<GraphicBuffer> buffer = |
| 7231 | getFactory().createGraphicBuffer(bufferSize.getWidth(), bufferSize.getHeight(), |
| 7232 | static_cast<android_pixel_format>(reqPixelFormat), |
| 7233 | 1 /* layerCount */, usage, "screenshot"); |
| 7234 | |
| 7235 | const status_t bufferStatus = buffer->initCheck(); |
| 7236 | if (bufferStatus != OK) { |
| 7237 | // Animations may end up being really janky, but don't crash here. |
| 7238 | // Otherwise an irreponsible process may cause an SF crash by allocating |
| 7239 | // too much. |
| 7240 | ALOGE("%s: Buffer failed to allocate: %d", __func__, bufferStatus); |
| 7241 | invokeScreenCaptureError(bufferStatus, captureListener); |
| 7242 | return; |
| 7243 | } |
| 7244 | const std::shared_ptr<renderengine::ExternalTexture> texture = std::make_shared< |
| 7245 | renderengine::impl::ExternalTexture>(buffer, getRenderEngine(), |
| 7246 | renderengine::impl::ExternalTexture::Usage:: |
| 7247 | WRITEABLE); |
| 7248 | auto futureFence = |
| 7249 | captureScreenshot(renderAreaBuilder, texture, false /* regionSampling */, grayscale, |
| 7250 | isProtected, captureListener, displayState, layerFEs); |
| 7251 | futureFence.get(); |
| 7252 | |
| 7253 | } else { |
| 7254 | const bool supportsProtected = getRenderEngine().supportsProtectedContent(); |
| 7255 | bool hasProtectedLayer = false; |
| 7256 | if (allowProtected && supportsProtected) { |
| 7257 | auto layers = mScheduler->schedule([=]() { return getLayerSnapshotsFn(); }).get(); |
| 7258 | hasProtectedLayer = layersHasProtectedLayer(extractLayerFEs(layers)); |
| 7259 | } |
| 7260 | const bool isProtected = hasProtectedLayer && allowProtected && supportsProtected; |
| 7261 | const uint32_t usage = GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER | |
| 7262 | GRALLOC_USAGE_HW_TEXTURE | |
| 7263 | (isProtected ? GRALLOC_USAGE_PROTECTED |
| 7264 | : GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 7265 | sp<GraphicBuffer> buffer = |
| 7266 | getFactory().createGraphicBuffer(bufferSize.getWidth(), bufferSize.getHeight(), |
| 7267 | static_cast<android_pixel_format>(reqPixelFormat), |
| 7268 | 1 /* layerCount */, usage, "screenshot"); |
| 7269 | |
| 7270 | const status_t bufferStatus = buffer->initCheck(); |
| 7271 | if (bufferStatus != OK) { |
| 7272 | // Animations may end up being really janky, but don't crash here. |
| 7273 | // Otherwise an irreponsible process may cause an SF crash by allocating |
| 7274 | // too much. |
| 7275 | ALOGE("%s: Buffer failed to allocate: %d", __func__, bufferStatus); |
| 7276 | invokeScreenCaptureError(bufferStatus, captureListener); |
| 7277 | return; |
| 7278 | } |
| 7279 | const std::shared_ptr<renderengine::ExternalTexture> texture = std::make_shared< |
| 7280 | renderengine::impl::ExternalTexture>(buffer, getRenderEngine(), |
| 7281 | renderengine::impl::ExternalTexture::Usage:: |
| 7282 | WRITEABLE); |
| 7283 | auto futureFence = captureScreenshotLegacy(renderAreaBuilder, getLayerSnapshotsFn, texture, |
| 7284 | false /* regionSampling */, grayscale, |
| 7285 | isProtected, captureListener); |
| 7286 | futureFence.get(); |
Alec Mouri | d8f17c5 | 2022-07-21 18:43:10 +0000 | [diff] [blame] | 7287 | } |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 7288 | } |
| 7289 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7290 | std::optional<SurfaceFlinger::OutputCompositionState> |
| 7291 | SurfaceFlinger::getDisplayStateFromRenderAreaBuilder(RenderAreaBuilderVariant& renderAreaBuilder) { |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7292 | sp<const DisplayDevice> display = nullptr; |
| 7293 | { |
| 7294 | Mutex::Autolock lock(mStateLock); |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7295 | if (auto* layerRenderAreaBuilder = |
| 7296 | std::get_if<LayerRenderAreaBuilder>(&renderAreaBuilder)) { |
| 7297 | // LayerSnapshotBuilder should only be accessed from the main thread. |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7298 | const frontend::LayerSnapshot* snapshot = |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7299 | mLayerSnapshotBuilder.getSnapshot(layerRenderAreaBuilder->layer->getSequence()); |
| 7300 | if (!snapshot) { |
| 7301 | ALOGW("Couldn't find layer snapshot for %d", |
| 7302 | layerRenderAreaBuilder->layer->getSequence()); |
| 7303 | } else { |
Melody Hsu | 50c151a | 2024-05-14 06:11:12 +0000 | [diff] [blame] | 7304 | layerRenderAreaBuilder->setLayerSnapshot(*snapshot); |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7305 | display = findDisplay( |
| 7306 | [layerStack = snapshot->outputFilter.layerStack](const auto& display) { |
| 7307 | return display.getLayerStack() == layerStack; |
| 7308 | }); |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7309 | } |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7310 | } else if (auto* displayRenderAreaBuilder = |
| 7311 | std::get_if<DisplayRenderAreaBuilder>(&renderAreaBuilder)) { |
| 7312 | display = displayRenderAreaBuilder->displayWeak.promote(); |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7313 | } |
| 7314 | |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7315 | if (display == nullptr) { |
| 7316 | display = getDefaultDisplayDeviceLocked(); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 7317 | } |
| 7318 | |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7319 | if (display != nullptr) { |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7320 | return std::optional{display->getCompositionDisplay()->getState()}; |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7321 | } |
| 7322 | } |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7323 | return std::nullopt; |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7324 | } |
| 7325 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7326 | std::vector<sp<LayerFE>> SurfaceFlinger::extractLayerFEs( |
| 7327 | const std::vector<std::pair<Layer*, sp<LayerFE>>>& layers) const { |
| 7328 | std::vector<sp<LayerFE>> layerFEs; |
| 7329 | layerFEs.reserve(layers.size()); |
| 7330 | for (const auto& [_, layerFE] : layers) { |
| 7331 | layerFEs.push_back(layerFE); |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7332 | } |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7333 | return layerFEs; |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7334 | } |
| 7335 | |
| 7336 | ftl::SharedFuture<FenceResult> SurfaceFlinger::captureScreenshot( |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7337 | const RenderAreaBuilderVariant& renderAreaBuilder, |
| 7338 | const std::shared_ptr<renderengine::ExternalTexture>& buffer, bool regionSampling, |
| 7339 | bool grayscale, bool isProtected, const sp<IScreenCaptureListener>& captureListener, |
| 7340 | std::optional<OutputCompositionState>& displayState, std::vector<sp<LayerFE>>& layerFEs) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 7341 | SFTRACE_CALL(); |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7342 | |
| 7343 | ScreenCaptureResults captureResults; |
| 7344 | std::unique_ptr<const RenderArea> renderArea = |
| 7345 | std::visit([](auto&& arg) -> std::unique_ptr<RenderArea> { return arg.build(); }, |
| 7346 | renderAreaBuilder); |
| 7347 | |
| 7348 | if (!renderArea) { |
| 7349 | ALOGW("Skipping screen capture because of invalid render area."); |
| 7350 | if (captureListener) { |
| 7351 | captureResults.fenceResult = base::unexpected(NO_MEMORY); |
| 7352 | captureListener->onScreenCaptureCompleted(captureResults); |
| 7353 | } |
| 7354 | return ftl::yield<FenceResult>(base::unexpected(NO_ERROR)).share(); |
| 7355 | } |
| 7356 | |
| 7357 | // Empty vector needed to pass into renderScreenImpl for legacy path |
| 7358 | std::vector<std::pair<Layer*, sp<android::LayerFE>>> layers; |
| 7359 | ftl::SharedFuture<FenceResult> renderFuture = |
| 7360 | renderScreenImpl(std::move(renderArea), buffer, regionSampling, grayscale, isProtected, |
| 7361 | captureResults, displayState, layers, layerFEs); |
| 7362 | |
| 7363 | if (captureListener) { |
| 7364 | // Defer blocking on renderFuture back to the Binder thread. |
| 7365 | return ftl::Future(std::move(renderFuture)) |
| 7366 | .then([captureListener, captureResults = std::move(captureResults)]( |
| 7367 | FenceResult fenceResult) mutable -> FenceResult { |
| 7368 | captureResults.fenceResult = std::move(fenceResult); |
| 7369 | captureListener->onScreenCaptureCompleted(captureResults); |
| 7370 | return base::unexpected(NO_ERROR); |
| 7371 | }) |
| 7372 | .share(); |
| 7373 | } |
| 7374 | return renderFuture; |
| 7375 | } |
| 7376 | |
| 7377 | ftl::SharedFuture<FenceResult> SurfaceFlinger::captureScreenshotLegacy( |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7378 | RenderAreaBuilderVariant renderAreaBuilder, GetLayerSnapshotsFunction getLayerSnapshotsFn, |
| 7379 | const std::shared_ptr<renderengine::ExternalTexture>& buffer, bool regionSampling, |
| 7380 | bool grayscale, bool isProtected, const sp<IScreenCaptureListener>& captureListener) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 7381 | SFTRACE_CALL(); |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7382 | |
| 7383 | auto takeScreenshotFn = [=, this, renderAreaBuilder = std::move(renderAreaBuilder)]() REQUIRES( |
| 7384 | kMainThreadContext) mutable -> ftl::SharedFuture<FenceResult> { |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7385 | auto layers = getLayerSnapshotsFn(); |
| 7386 | if (FlagManager::getInstance().ce_fence_promise()) { |
| 7387 | for (auto& [layer, layerFE] : layers) { |
| 7388 | attachReleaseFenceFutureToLayer(layer, layerFE.get(), ui::INVALID_LAYER_STACK); |
| 7389 | } |
| 7390 | } |
| 7391 | auto displayState = getDisplayStateFromRenderAreaBuilder(renderAreaBuilder); |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7392 | |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 7393 | ScreenCaptureResults captureResults; |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7394 | std::unique_ptr<const RenderArea> renderArea = |
| 7395 | std::visit([](auto&& arg) -> std::unique_ptr<RenderArea> { return arg.build(); }, |
| 7396 | renderAreaBuilder); |
| 7397 | |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 7398 | if (!renderArea) { |
| 7399 | ALOGW("Skipping screen capture because of invalid render area."); |
| 7400 | if (captureListener) { |
| 7401 | captureResults.fenceResult = base::unexpected(NO_MEMORY); |
| 7402 | captureListener->onScreenCaptureCompleted(captureResults); |
| 7403 | } |
| 7404 | return ftl::yield<FenceResult>(base::unexpected(NO_ERROR)).share(); |
| 7405 | } |
| 7406 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7407 | auto layerFEs = extractLayerFEs(layers); |
Melody Hsu | 9db32ff | 2024-04-17 19:52:55 +0000 | [diff] [blame] | 7408 | ftl::SharedFuture<FenceResult> renderFuture = |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7409 | renderScreenImpl(std::move(renderArea), buffer, regionSampling, grayscale, |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7410 | isProtected, captureResults, displayState, layers, layerFEs); |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 7411 | |
| 7412 | if (captureListener) { |
| 7413 | // Defer blocking on renderFuture back to the Binder thread. |
| 7414 | return ftl::Future(std::move(renderFuture)) |
| 7415 | .then([captureListener, captureResults = std::move(captureResults)]( |
| 7416 | FenceResult fenceResult) mutable -> FenceResult { |
| 7417 | captureResults.fenceResult = std::move(fenceResult); |
Nolan Scobie | 1d2deb2 | 2022-11-18 12:57:47 -0500 | [diff] [blame] | 7418 | captureListener->onScreenCaptureCompleted(captureResults); |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 7419 | return base::unexpected(NO_ERROR); |
| 7420 | }) |
| 7421 | .share(); |
| 7422 | } |
| 7423 | return renderFuture; |
| 7424 | }; |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 7425 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 7426 | // 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] | 7427 | // of calls on the main thread. |
Melody Hsu | 82d524e | 2024-02-23 02:37:38 +0000 | [diff] [blame] | 7428 | auto future = |
| 7429 | mScheduler->schedule(FTL_FAKE_GUARD(kMainThreadContext, std::move(takeScreenshotFn))); |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 7430 | |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 7431 | // Flatten nested futures. |
Dominik Laskowski | b17c621 | 2022-05-09 09:36:19 -0700 | [diff] [blame] | 7432 | auto chain = ftl::Future(std::move(future)).then([](ftl::SharedFuture<FenceResult> future) { |
| 7433 | return future; |
| 7434 | }); |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 7435 | |
| 7436 | return chain.share(); |
Mathias Agopian | 2a9fc49 | 2013-03-01 13:42:57 -0800 | [diff] [blame] | 7437 | } |
| 7438 | |
Dominik Laskowski | b17c621 | 2022-05-09 09:36:19 -0700 | [diff] [blame] | 7439 | ftl::SharedFuture<FenceResult> SurfaceFlinger::renderScreenImpl( |
Melody Hsu | 2ffa36b | 2024-05-08 03:51:46 +0000 | [diff] [blame] | 7440 | std::unique_ptr<const RenderArea> renderArea, |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 7441 | const std::shared_ptr<renderengine::ExternalTexture>& buffer, bool regionSampling, |
| 7442 | bool grayscale, bool isProtected, ScreenCaptureResults& captureResults, |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7443 | std::optional<OutputCompositionState>& displayState, |
| 7444 | 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] | 7445 | SFTRACE_CALL(); |
Alec Mouri | b4814de | 2023-09-11 22:09:03 +0000 | [diff] [blame] | 7446 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7447 | for (auto& layerFE : layerFEs) { |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 7448 | frontend::LayerSnapshot* snapshot = layerFE->mSnapshot.get(); |
| 7449 | captureResults.capturedSecureLayers |= (snapshot->isVisible && snapshot->isSecure); |
| 7450 | captureResults.capturedHdrLayers |= isHdrLayer(*snapshot); |
| 7451 | layerFE->mSnapshot->geomLayerTransform = |
| 7452 | renderArea->getTransform() * layerFE->mSnapshot->geomLayerTransform; |
Patrick Williams | c6e0cce | 2023-02-28 23:04:45 -0600 | [diff] [blame] | 7453 | layerFE->mSnapshot->geomInverseLayerTransform = |
| 7454 | layerFE->mSnapshot->geomLayerTransform.inverse(); |
Vishnu Nair | 7aa0eb7 | 2023-01-24 03:59:27 +0000 | [diff] [blame] | 7455 | } |
chaviw | 426b5c0 | 2020-07-27 11:20:15 -0700 | [diff] [blame] | 7456 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7457 | auto capturedBuffer = buffer; |
| 7458 | |
| 7459 | auto requestedDataspace = renderArea->getReqDataSpace(); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7460 | auto parent = renderArea->getParentLayer(); |
Alec Mouri | fcedb9c | 2022-04-11 20:02:17 +0000 | [diff] [blame] | 7461 | auto renderIntent = RenderIntent::TONE_MAP_COLORIMETRIC; |
Sally Qi | 6c903ca | 2022-04-25 11:24:20 -0700 | [diff] [blame] | 7462 | auto sdrWhitePointNits = DisplayDevice::sDefaultMaxLumiance; |
| 7463 | auto displayBrightnessNits = DisplayDevice::sDefaultMaxLumiance; |
| 7464 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7465 | captureResults.capturedDataspace = requestedDataspace; |
| 7466 | |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 7467 | const bool enableLocalTonemapping = FlagManager::getInstance().local_tonemap_screenshots() && |
| 7468 | !renderArea->getHintForSeamlessTransition(); |
| 7469 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7470 | if (displayState) { |
| 7471 | const auto& state = displayState.value(); |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7472 | captureResults.capturedDataspace = |
Melody Hsu | 683fe04 | 2024-06-04 17:22:04 +0000 | [diff] [blame] | 7473 | pickBestDataspace(requestedDataspace, state, captureResults.capturedHdrLayers, |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7474 | renderArea->getHintForSeamlessTransition()); |
| 7475 | sdrWhitePointNits = state.sdrWhitePointNits; |
Robert Carr | 12a3b9b | 2022-03-10 09:55:29 -0800 | [diff] [blame] | 7476 | |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7477 | if (!captureResults.capturedHdrLayers) { |
| 7478 | displayBrightnessNits = sdrWhitePointNits; |
| 7479 | } else { |
| 7480 | displayBrightnessNits = state.displayBrightnessNits; |
| 7481 | if (!enableLocalTonemapping) { |
| 7482 | // Only clamp the display brightness if this is not a seamless transition. |
| 7483 | // Otherwise for seamless transitions it's important to match the current |
| 7484 | // display state as the buffer will be shown under these same conditions, and we |
| 7485 | // want to avoid any flickers |
| 7486 | if (sdrWhitePointNits > 1.0f && !renderArea->getHintForSeamlessTransition()) { |
| 7487 | // Restrict the amount of HDR "headroom" in the screenshot to avoid |
| 7488 | // over-dimming the SDR portion. 2.0 chosen by experimentation |
| 7489 | constexpr float kMaxScreenshotHeadroom = 2.0f; |
| 7490 | displayBrightnessNits = std::min(sdrWhitePointNits * kMaxScreenshotHeadroom, |
| 7491 | displayBrightnessNits); |
Alec Mouri | b4814de | 2023-09-11 22:09:03 +0000 | [diff] [blame] | 7492 | } |
Alec Mouri | 5e488f8 | 2023-09-15 01:27:49 +0000 | [diff] [blame] | 7493 | } |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7494 | } |
Alec Mouri | ffb3221 | 2023-09-16 19:29:15 +0000 | [diff] [blame] | 7495 | |
Melody Hsu | 335ef3e | 2024-05-31 22:58:40 +0000 | [diff] [blame] | 7496 | // Screenshots leaving the device should be colorimetric |
| 7497 | if (requestedDataspace == ui::Dataspace::UNKNOWN && |
| 7498 | renderArea->getHintForSeamlessTransition()) { |
| 7499 | renderIntent = state.renderIntent; |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7500 | } |
Robert Carr | 12a3b9b | 2022-03-10 09:55:29 -0800 | [diff] [blame] | 7501 | } |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7502 | |
| 7503 | captureResults.buffer = capturedBuffer->getBuffer(); |
chaviw | 426b5c0 | 2020-07-27 11:20:15 -0700 | [diff] [blame] | 7504 | |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7505 | ui::LayerStack layerStack{ui::DEFAULT_LAYER_STACK}; |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7506 | if (!layerFEs.empty()) { |
| 7507 | const sp<LayerFE>& layerFE = layerFEs.back(); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7508 | layerStack = layerFE->getCompositionState()->outputFilter.layerStack; |
Yichi Chen | 40773d9 | 2020-04-01 10:10:18 +0800 | [diff] [blame] | 7509 | } |
Ady Abraham | 9959994 | 2021-01-27 20:27:23 -0800 | [diff] [blame] | 7510 | |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7511 | auto copyLayerFEs = [&layerFEs]() { |
| 7512 | std::vector<sp<compositionengine::LayerFE>> ceLayerFEs; |
| 7513 | ceLayerFEs.reserve(layerFEs.size()); |
| 7514 | for (const auto& layerFE : layerFEs) { |
| 7515 | ceLayerFEs.push_back(layerFE); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7516 | } |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7517 | return ceLayerFEs; |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7518 | }; |
| 7519 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7520 | auto present = [this, buffer = capturedBuffer, dataspace = captureResults.capturedDataspace, |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 7521 | sdrWhitePointNits, displayBrightnessNits, grayscale, isProtected, |
| 7522 | layerFEs = copyLayerFEs(), layerStack, regionSampling, |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 7523 | renderArea = std::move(renderArea), renderIntent, |
| 7524 | enableLocalTonemapping]() -> FenceResult { |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7525 | std::unique_ptr<compositionengine::CompositionEngine> compositionEngine = |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 7526 | mFactory.createCompositionEngine(); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7527 | compositionEngine->setRenderEngine(mRenderEngine.get()); |
| 7528 | |
| 7529 | compositionengine::Output::ColorProfile colorProfile{.dataspace = dataspace, |
| 7530 | .renderIntent = renderIntent}; |
| 7531 | |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7532 | float targetBrightness = 1.0f; |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 7533 | if (enableLocalTonemapping) { |
| 7534 | // Boost the whole scene so that SDR white is at 1.0 while still communicating the hdr |
| 7535 | // sdr ratio via display brightness / sdrWhite nits. |
| 7536 | targetBrightness = sdrWhitePointNits / displayBrightnessNits; |
| 7537 | } else if (dataspace == ui::Dataspace::BT2020_HLG) { |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7538 | const float maxBrightnessNits = displayBrightnessNits / sdrWhitePointNits * 203; |
| 7539 | // With a low dimming ratio, don't fit the entire curve. Otherwise mixed content |
| 7540 | // will appear way too bright. |
| 7541 | if (maxBrightnessNits < 1000.f) { |
| 7542 | targetBrightness = 1000.f / maxBrightnessNits; |
| 7543 | } |
| 7544 | } |
| 7545 | |
Alec Mouri | ffb3221 | 2023-09-16 19:29:15 +0000 | [diff] [blame] | 7546 | // Screenshots leaving the device must not dim in gamma space. |
| 7547 | const bool dimInGammaSpaceForEnhancedScreenshots = mDimInGammaSpaceForEnhancedScreenshots && |
| 7548 | renderArea->getHintForSeamlessTransition(); |
| 7549 | |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7550 | std::shared_ptr<ScreenCaptureOutput> output = createScreenCaptureOutput( |
| 7551 | ScreenCaptureOutputArgs{.compositionEngine = *compositionEngine, |
| 7552 | .colorProfile = colorProfile, |
| 7553 | .renderArea = *renderArea, |
| 7554 | .layerStack = layerStack, |
| 7555 | .buffer = std::move(buffer), |
| 7556 | .sdrWhitePointNits = sdrWhitePointNits, |
| 7557 | .displayBrightnessNits = displayBrightnessNits, |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 7558 | .targetBrightness = targetBrightness, |
Alec Mouri | 7dae9c1 | 2023-07-13 19:40:53 +0000 | [diff] [blame] | 7559 | .regionSampling = regionSampling, |
Alec Mouri | 9af38ba | 2023-08-16 22:41:14 +0000 | [diff] [blame] | 7560 | .treat170mAsSrgb = mTreat170mAsSrgb, |
| 7561 | .dimInGammaSpaceForEnhancedScreenshots = |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 7562 | dimInGammaSpaceForEnhancedScreenshots, |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 7563 | .isProtected = isProtected, |
| 7564 | .enableLocalTonemapping = enableLocalTonemapping}); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7565 | |
| 7566 | const float colorSaturation = grayscale ? 0 : 1; |
| 7567 | compositionengine::CompositionRefreshArgs refreshArgs{ |
| 7568 | .outputs = {output}, |
| 7569 | .layers = std::move(layerFEs), |
| 7570 | .updatingOutputGeometryThisFrame = true, |
| 7571 | .updatingGeometryThisFrame = true, |
| 7572 | .colorTransformMatrix = calculateColorMatrix(colorSaturation), |
| 7573 | }; |
| 7574 | compositionEngine->present(refreshArgs); |
| 7575 | |
| 7576 | return output->getRenderSurface()->getClientTargetAcquireFence(); |
| 7577 | }; |
| 7578 | |
| 7579 | // If RenderEngine is threaded, we can safely call CompositionEngine::present off the main |
| 7580 | // thread as the RenderEngine::drawLayers call will run on RenderEngine's thread. Otherwise, |
| 7581 | // we need RenderEngine to run on the main thread so we call CompositionEngine::present |
| 7582 | // immediately. |
| 7583 | // |
| 7584 | // TODO(b/196334700) Once we use RenderEngineThreaded everywhere we can always defer the call |
| 7585 | // to CompositionEngine::present. |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7586 | ftl::SharedFuture<FenceResult> presentFuture; |
| 7587 | if (FlagManager::getInstance().single_hop_screenshot() && |
Vishnu Nair | 5aadd24 | 2024-06-29 01:17:02 +0000 | [diff] [blame] | 7588 | FlagManager::getInstance().ce_fence_promise() && mRenderEngine->isThreaded()) { |
| 7589 | presentFuture = ftl::yield(present()).share(); |
Melody Hsu | 8c42cf1 | 2024-06-05 00:07:03 +0000 | [diff] [blame] | 7590 | } else { |
| 7591 | presentFuture = mRenderEngine->isThreaded() ? ftl::defer(std::move(present)).share() |
| 7592 | : ftl::yield(present()).share(); |
| 7593 | } |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7594 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 7595 | if (!FlagManager::getInstance().ce_fence_promise()) { |
| 7596 | for (auto& [layer, layerFE] : layers) { |
| 7597 | layer->onLayerDisplayed(presentFuture, ui::INVALID_LAYER_STACK, |
| 7598 | [layerFE = std::move(layerFE)](FenceResult) { |
| 7599 | if (FlagManager::getInstance() |
| 7600 | .screenshot_fence_preservation()) { |
| 7601 | const auto compositionResult = |
| 7602 | layerFE->stealCompositionResult(); |
| 7603 | const auto& fences = compositionResult.releaseFences; |
| 7604 | // CompositionEngine may choose to cull layers that |
| 7605 | // aren't visible, so pass a non-fence. |
| 7606 | return fences.empty() ? Fence::NO_FENCE |
| 7607 | : fences.back().first.get(); |
| 7608 | } else { |
| 7609 | return layerFE->stealCompositionResult() |
| 7610 | .releaseFences.back() |
| 7611 | .first.get(); |
| 7612 | } |
| 7613 | }); |
| 7614 | } |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 7615 | } |
| 7616 | |
| 7617 | return presentFuture; |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 7618 | } |
| 7619 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 7620 | void SurfaceFlinger::traverseLegacyLayers(const LayerVector::Visitor& visitor) const { |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 7621 | for (auto& layer : mLegacyLayers) { |
| 7622 | visitor(layer.second.get()); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 7623 | } |
| 7624 | } |
| 7625 | |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 7626 | // --------------------------------------------------------------------------- |
| 7627 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 7628 | ftl::Optional<scheduler::FrameRateMode> SurfaceFlinger::getPreferredDisplayMode( |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 7629 | PhysicalDisplayId displayId, DisplayModeId defaultModeId) const { |
| 7630 | if (const auto schedulerMode = mScheduler->getPreferredDisplayMode(); |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 7631 | schedulerMode.modePtr->getPhysicalDisplayId() == displayId) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 7632 | return schedulerMode; |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 7633 | } |
| 7634 | |
| 7635 | return mPhysicalDisplays.get(displayId) |
| 7636 | .transform(&PhysicalDisplay::snapshotRef) |
| 7637 | .and_then([&](const display::DisplaySnapshot& snapshot) { |
| 7638 | return snapshot.displayModes().get(defaultModeId); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 7639 | }) |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 7640 | .transform([](const DisplayModePtr& modePtr) { |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 7641 | return scheduler::FrameRateMode{modePtr->getPeakFps(), ftl::as_non_null(modePtr)}; |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 7642 | }); |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 7643 | } |
| 7644 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 7645 | status_t SurfaceFlinger::setDesiredDisplayModeSpecsInternal( |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 7646 | const sp<DisplayDevice>& display, |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7647 | const scheduler::RefreshRateSelector::PolicyVariant& policy) { |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 7648 | const auto displayId = display->getPhysicalId(); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 7649 | SFTRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str()); |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 7650 | |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 7651 | Mutex::Autolock lock(mStateLock); |
| 7652 | |
Ady Abraham | fb85366 | 2021-04-09 11:46:40 -0700 | [diff] [blame] | 7653 | if (mDebugDisplayModeSetByBackdoor) { |
| 7654 | // ignore this request as mode is overridden by backdoor |
| 7655 | return NO_ERROR; |
| 7656 | } |
| 7657 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7658 | auto& selector = display->refreshRateSelector(); |
| 7659 | using SetPolicyResult = scheduler::RefreshRateSelector::SetPolicyResult; |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 7660 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7661 | switch (selector.setPolicy(policy)) { |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 7662 | case SetPolicyResult::Invalid: |
| 7663 | return BAD_VALUE; |
| 7664 | case SetPolicyResult::Unchanged: |
| 7665 | return NO_ERROR; |
| 7666 | case SetPolicyResult::Changed: |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 7667 | break; |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7668 | } |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 7669 | |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 7670 | return applyRefreshRateSelectorPolicy(displayId, selector); |
Dominik Laskowski | 3f72640 | 2022-11-21 17:21:22 -0500 | [diff] [blame] | 7671 | } |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7672 | |
Dominik Laskowski | 3f72640 | 2022-11-21 17:21:22 -0500 | [diff] [blame] | 7673 | status_t SurfaceFlinger::applyRefreshRateSelectorPolicy( |
Dominik Laskowski | 1746afd | 2024-04-10 19:21:37 -0400 | [diff] [blame] | 7674 | PhysicalDisplayId displayId, const scheduler::RefreshRateSelector& selector) { |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7675 | const scheduler::RefreshRateSelector::Policy currentPolicy = selector.getCurrentPolicy(); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 7676 | ALOGV("Setting desired display mode specs: %s", currentPolicy.toString().c_str()); |
Adrian Salido | ab6ef6c | 2019-08-28 10:02:49 -0700 | [diff] [blame] | 7677 | |
Dominik Laskowski | bda5236 | 2024-08-04 00:41:46 -0400 | [diff] [blame] | 7678 | if (const bool isPacesetter = |
| 7679 | mScheduler->onDisplayModeChanged(displayId, selector.getActiveMode())) { |
Dominik Laskowski | 4383967 | 2024-08-04 02:01:48 -0400 | [diff] [blame] | 7680 | mDisplayModeController.updateKernelIdleTimer(displayId); |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 7681 | } |
Ady Abraham | 838de06 | 2019-02-04 10:24:03 -0800 | [diff] [blame] | 7682 | |
Dominik Laskowski | e591b55 | 2022-09-09 10:44:56 -0700 | [diff] [blame] | 7683 | auto preferredModeOpt = getPreferredDisplayMode(displayId, currentPolicy.defaultMode); |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 7684 | if (!preferredModeOpt) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 7685 | ALOGE("%s: Preferred mode is unknown", __func__); |
| 7686 | return NAME_NOT_FOUND; |
| 7687 | } |
| 7688 | |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 7689 | auto preferredMode = std::move(*preferredModeOpt); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 7690 | const auto preferredModeId = preferredMode.modePtr->getId(); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 7691 | |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 7692 | const Fps preferredFps = preferredMode.fps; |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 7693 | ALOGV("Switching to Scheduler preferred mode %d (%s)", ftl::to_underlying(preferredModeId), |
Dominik Laskowski | 5974651 | 2023-11-19 09:30:24 -0500 | [diff] [blame] | 7694 | to_string(preferredFps).c_str()); |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 7695 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 7696 | if (!selector.isModeAllowed(preferredMode)) { |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 7697 | ALOGE("%s: Preferred mode %d is disallowed", __func__, ftl::to_underlying(preferredModeId)); |
Dominik Laskowski | f259544 | 2022-08-29 13:51:57 -0700 | [diff] [blame] | 7698 | return INVALID_OPERATION; |
Dominik Laskowski | 4f1dd8c | 2019-04-17 15:54:30 -0700 | [diff] [blame] | 7699 | } |
| 7700 | |
Dominik Laskowski | 1746afd | 2024-04-10 19:21:37 -0400 | [diff] [blame] | 7701 | setDesiredMode({std::move(preferredMode), .emitEvent = true}); |
Ady Abraham | 33a386b | 2023-07-18 15:37:11 -0700 | [diff] [blame] | 7702 | |
| 7703 | // Update the frameRateOverride list as the display render rate might have changed |
Dominik Laskowski | 1d7be06 | 2024-07-24 13:59:07 -0400 | [diff] [blame] | 7704 | mScheduler->updateFrameRateOverrides(scheduler::GlobalSignals{}, preferredFps); |
Ady Abraham | d9b3ea6 | 2019-02-26 14:08:03 -0800 | [diff] [blame] | 7705 | return NO_ERROR; |
| 7706 | } |
| 7707 | |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 7708 | namespace { |
| 7709 | FpsRange translate(const gui::DisplayModeSpecs::RefreshRateRanges::RefreshRateRange& aidlRange) { |
| 7710 | return FpsRange{Fps::fromValue(aidlRange.min), Fps::fromValue(aidlRange.max)}; |
| 7711 | } |
| 7712 | |
| 7713 | FpsRanges translate(const gui::DisplayModeSpecs::RefreshRateRanges& aidlRanges) { |
| 7714 | return FpsRanges{translate(aidlRanges.physical), translate(aidlRanges.render)}; |
| 7715 | } |
| 7716 | |
| 7717 | gui::DisplayModeSpecs::RefreshRateRanges::RefreshRateRange translate(const FpsRange& range) { |
| 7718 | gui::DisplayModeSpecs::RefreshRateRanges::RefreshRateRange aidlRange; |
| 7719 | aidlRange.min = range.min.getValue(); |
| 7720 | aidlRange.max = range.max.getValue(); |
| 7721 | return aidlRange; |
| 7722 | } |
| 7723 | |
| 7724 | gui::DisplayModeSpecs::RefreshRateRanges translate(const FpsRanges& ranges) { |
| 7725 | gui::DisplayModeSpecs::RefreshRateRanges aidlRanges; |
| 7726 | aidlRanges.physical = translate(ranges.physical); |
| 7727 | aidlRanges.render = translate(ranges.render); |
| 7728 | return aidlRanges; |
| 7729 | } |
| 7730 | |
| 7731 | } // namespace |
| 7732 | |
| 7733 | status_t SurfaceFlinger::setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, |
| 7734 | const gui::DisplayModeSpecs& specs) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 7735 | SFTRACE_CALL(); |
Ana Krulec | 0782b88 | 2019-10-15 17:34:54 -0700 | [diff] [blame] | 7736 | |
| 7737 | if (!displayToken) { |
| 7738 | return BAD_VALUE; |
| 7739 | } |
| 7740 | |
Nolan Scobie | 0074cd6 | 2023-12-14 16:51:14 -0500 | [diff] [blame] | 7741 | auto future = mScheduler->schedule([=, this]() FTL_FAKE_GUARD(kMainThreadContext) -> status_t { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 7742 | const auto display = FTL_FAKE_GUARD(mStateLock, getDisplayDeviceLocked(displayToken)); |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 7743 | if (!display) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 7744 | ALOGE("Attempt to set desired display modes for invalid display token %p", |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 7745 | displayToken.get()); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 7746 | return NAME_NOT_FOUND; |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 7747 | } else if (display->isVirtual()) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 7748 | ALOGW("Attempt to set desired display modes for virtual display"); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 7749 | return INVALID_OPERATION; |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 7750 | } else { |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7751 | using Policy = scheduler::RefreshRateSelector::DisplayManagerPolicy; |
Ady Abraham | 6723172 | 2024-03-21 18:06:21 -0700 | [diff] [blame] | 7752 | const auto idleScreenConfigOpt = |
| 7753 | FlagManager::getInstance().idle_screen_refresh_rate_timeout() |
| 7754 | ? specs.idleScreenRefreshRateConfig |
| 7755 | : std::nullopt; |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 7756 | const Policy policy{DisplayModeId(specs.defaultMode), translate(specs.primaryRanges), |
Ady Abraham | 6723172 | 2024-03-21 18:06:21 -0700 | [diff] [blame] | 7757 | translate(specs.appRequestRanges), specs.allowGroupSwitching, |
| 7758 | idleScreenConfigOpt}; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 7759 | |
Dominik Laskowski | 36dced8 | 2022-09-02 09:24:00 -0700 | [diff] [blame] | 7760 | return setDesiredDisplayModeSpecsInternal(display, policy); |
Dominik Laskowski | dd4ef27 | 2020-04-23 14:02:12 -0700 | [diff] [blame] | 7761 | } |
| 7762 | }); |
| 7763 | |
| 7764 | return future.get(); |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 7765 | } |
| 7766 | |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 7767 | status_t SurfaceFlinger::getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 7768 | gui::DisplayModeSpecs* outSpecs) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 7769 | SFTRACE_CALL(); |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 7770 | |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 7771 | if (!displayToken || !outSpecs) { |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 7772 | return BAD_VALUE; |
| 7773 | } |
| 7774 | |
| 7775 | Mutex::Autolock lock(mStateLock); |
| 7776 | const auto display = getDisplayDeviceLocked(displayToken); |
| 7777 | if (!display) { |
| 7778 | return NAME_NOT_FOUND; |
| 7779 | } |
| 7780 | |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7781 | if (display->isVirtual()) { |
Dominik Laskowski | 470df5f | 2020-04-02 22:27:42 -0700 | [diff] [blame] | 7782 | return INVALID_OPERATION; |
Ana Krulec | 234bb16 | 2019-11-10 22:55:55 +0100 | [diff] [blame] | 7783 | } |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7784 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7785 | scheduler::RefreshRateSelector::Policy policy = |
| 7786 | display->refreshRateSelector().getDisplayManagerPolicy(); |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 7787 | outSpecs->defaultMode = ftl::to_underlying(policy.defaultMode); |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 7788 | outSpecs->allowGroupSwitching = policy.allowGroupSwitching; |
| 7789 | outSpecs->primaryRanges = translate(policy.primaryRanges); |
| 7790 | outSpecs->appRequestRanges = translate(policy.appRequestRanges); |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7791 | return NO_ERROR; |
Ana Krulec | 0782b88 | 2019-10-15 17:34:54 -0700 | [diff] [blame] | 7792 | } |
| 7793 | |
Dominik Laskowski | 7584836 | 2019-11-11 17:57:20 -0800 | [diff] [blame] | 7794 | void SurfaceFlinger::onLayerFirstRef(Layer* layer) { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 7795 | mNumLayers++; |
Vishnu Nair | a12c5c7 | 2024-08-28 15:42:17 -0700 | [diff] [blame] | 7796 | mScheduler->registerLayer(layer, scheduler::FrameRateCompatibility::Default); |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 7797 | } |
| 7798 | |
| 7799 | void SurfaceFlinger::onLayerDestroyed(Layer* layer) { |
| 7800 | mNumLayers--; |
Vishnu Nair | a12c5c7 | 2024-08-28 15:42:17 -0700 | [diff] [blame] | 7801 | mScheduler->deregisterLayer(layer); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 7802 | if (mTransactionTracing) { |
| 7803 | mTransactionTracing->onLayerRemoved(layer->getSequence()); |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 7804 | } |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 7805 | mScheduler->onLayerDestroyed(layer); |
Valerie Hau | c568680 | 2019-11-22 14:18:09 -0800 | [diff] [blame] | 7806 | } |
| 7807 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 7808 | void SurfaceFlinger::onLayerUpdate() { |
| 7809 | scheduleCommit(FrameHint::kActive); |
| 7810 | } |
| 7811 | |
Vishnu Nair | 08f6eae | 2019-11-26 14:01:39 -0800 | [diff] [blame] | 7812 | status_t SurfaceFlinger::setGlobalShadowSettings(const half4& ambientColor, const half4& spotColor, |
| 7813 | float lightPosY, float lightPosZ, |
| 7814 | float lightRadius) { |
| 7815 | Mutex::Autolock _l(mStateLock); |
| 7816 | mCurrentState.globalShadowSettings.ambientColor = vec4(ambientColor); |
| 7817 | mCurrentState.globalShadowSettings.spotColor = vec4(spotColor); |
| 7818 | mCurrentState.globalShadowSettings.lightPos.y = lightPosY; |
| 7819 | mCurrentState.globalShadowSettings.lightPos.z = lightPosZ; |
| 7820 | mCurrentState.globalShadowSettings.lightRadius = lightRadius; |
| 7821 | |
| 7822 | // these values are overridden when calculating the shadow settings for a layer. |
| 7823 | mCurrentState.globalShadowSettings.lightPos.x = 0.f; |
| 7824 | mCurrentState.globalShadowSettings.length = 0.f; |
Vishnu Nair | b13bb95 | 2019-11-15 10:24:08 -0800 | [diff] [blame] | 7825 | return NO_ERROR; |
| 7826 | } |
| 7827 | |
Lloyd Pique | 8d9f836 | 2020-02-11 19:13:09 -0800 | [diff] [blame] | 7828 | const std::unordered_map<std::string, uint32_t>& SurfaceFlinger::getGenericLayerMetadataKeyMap() |
| 7829 | const { |
| 7830 | // TODO(b/149500060): Remove this fixed/static mapping. Please prefer taking |
| 7831 | // on the work to remove the table in that bug rather than adding more to |
| 7832 | // it. |
| 7833 | static const std::unordered_map<std::string, uint32_t> genericLayerMetadataKeyMap{ |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 7834 | {"org.chromium.arc.V1_0.TaskId", gui::METADATA_TASK_ID}, |
| 7835 | {"org.chromium.arc.V1_0.CursorInfo", gui::METADATA_MOUSE_CURSOR}, |
Lloyd Pique | 8d9f836 | 2020-02-11 19:13:09 -0800 | [diff] [blame] | 7836 | }; |
| 7837 | return genericLayerMetadataKeyMap; |
| 7838 | } |
| 7839 | |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 7840 | status_t SurfaceFlinger::setGameModeFrameRateOverride(uid_t uid, float frameRate) { |
Dominik Laskowski | 1d7be06 | 2024-07-24 13:59:07 -0400 | [diff] [blame] | 7841 | mScheduler->setGameModeFrameRateForUid(FrameRateOverride{uid, frameRate}); |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 7842 | return NO_ERROR; |
| 7843 | } |
| 7844 | |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 7845 | status_t SurfaceFlinger::setGameDefaultFrameRateOverride(uid_t uid, float frameRate) { |
| 7846 | if (FlagManager::getInstance().game_default_frame_rate()) { |
| 7847 | mScheduler->setGameDefaultFrameRateForUid( |
| 7848 | FrameRateOverride{static_cast<uid_t>(uid), frameRate}); |
| 7849 | } |
| 7850 | return NO_ERROR; |
| 7851 | } |
| 7852 | |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 7853 | status_t SurfaceFlinger::updateSmallAreaDetection( |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 7854 | std::vector<std::pair<int32_t, float>>& appIdThresholdMappings) { |
| 7855 | mScheduler->updateSmallAreaDetection(appIdThresholdMappings); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 7856 | return NO_ERROR; |
| 7857 | } |
| 7858 | |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 7859 | status_t SurfaceFlinger::setSmallAreaDetectionThreshold(int32_t appId, float threshold) { |
| 7860 | mScheduler->setSmallAreaDetectionThreshold(appId, threshold); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 7861 | return NO_ERROR; |
| 7862 | } |
| 7863 | |
Dominik Laskowski | 2013464 | 2020-04-20 22:36:44 -0700 | [diff] [blame] | 7864 | void SurfaceFlinger::enableRefreshRateOverlay(bool enable) { |
ramindani | b2158ee | 2023-02-13 20:29:59 -0800 | [diff] [blame] | 7865 | bool setByHwc = getHwComposer().hasCapability(Capability::REFRESH_RATE_CHANGED_CALLBACK_DEBUG); |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 7866 | for (const auto& [displayId, physical] : mPhysicalDisplays) { |
| 7867 | if (physical.snapshot().connectionType() == ui::DisplayConnectionType::Internal || |
Sally Qi | de329f2 | 2023-09-26 16:21:39 -0700 | [diff] [blame] | 7868 | FlagManager::getInstance().refresh_rate_overlay_on_external_display()) { |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 7869 | if (const auto display = getDisplayDeviceLocked(displayId)) { |
| 7870 | const auto enableOverlay = [&](bool setByHwc) FTL_FAKE_GUARD(kMainThreadContext) { |
| 7871 | const auto activeMode = mDisplayModeController.getActiveMode(displayId); |
| 7872 | const Fps refreshRate = activeMode.modePtr->getVsyncRate(); |
| 7873 | const Fps renderFps = activeMode.fps; |
| 7874 | |
| 7875 | display->enableRefreshRateOverlay(enable, setByHwc, refreshRate, renderFps, |
| 7876 | mRefreshRateOverlaySpinner, |
| 7877 | mRefreshRateOverlayRenderRate, |
| 7878 | mRefreshRateOverlayShowInMiddle); |
ramindani | 3c722da | 2023-07-12 20:37:59 -0700 | [diff] [blame] | 7879 | }; |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 7880 | |
ramindani | 3c722da | 2023-07-12 20:37:59 -0700 | [diff] [blame] | 7881 | enableOverlay(setByHwc); |
| 7882 | if (setByHwc) { |
| 7883 | const auto status = |
Dominik Laskowski | 5c989f5 | 2024-04-11 13:57:14 -0400 | [diff] [blame] | 7884 | getHwComposer().setRefreshRateChangedCallbackDebugEnabled(displayId, |
| 7885 | enable); |
ramindani | 3c722da | 2023-07-12 20:37:59 -0700 | [diff] [blame] | 7886 | if (status != NO_ERROR) { |
Ying Wei | 22d59cc | 2024-05-11 02:41:08 +0000 | [diff] [blame] | 7887 | ALOGE("Error %s refresh rate changed callback debug", |
| 7888 | enable ? "enabling" : "disabling"); |
ramindani | 3c722da | 2023-07-12 20:37:59 -0700 | [diff] [blame] | 7889 | enableOverlay(/*setByHwc*/ false); |
| 7890 | } |
| 7891 | } |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 7892 | } |
Dominik Laskowski | 2013464 | 2020-04-20 22:36:44 -0700 | [diff] [blame] | 7893 | } |
Ady Abraham | 1b11bc6 | 2021-06-03 19:51:19 -0700 | [diff] [blame] | 7894 | } |
Dominik Laskowski | 2013464 | 2020-04-20 22:36:44 -0700 | [diff] [blame] | 7895 | } |
| 7896 | |
Sally Qi | 147581b | 2023-06-27 11:55:34 -0700 | [diff] [blame] | 7897 | void SurfaceFlinger::enableHdrSdrRatioOverlay(bool enable) { |
| 7898 | for (const auto& [id, display] : mPhysicalDisplays) { |
| 7899 | if (display.snapshot().connectionType() == ui::DisplayConnectionType::Internal) { |
| 7900 | if (const auto device = getDisplayDeviceLocked(id)) { |
| 7901 | device->enableHdrSdrRatioOverlay(enable); |
| 7902 | } |
| 7903 | } |
| 7904 | } |
| 7905 | } |
| 7906 | |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 7907 | int SurfaceFlinger::getGpuContextPriority() { |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 7908 | return getRenderEngine().getContextPriority(); |
Ana Krulec | 31f2b3c | 2020-12-14 14:30:09 -0800 | [diff] [blame] | 7909 | } |
| 7910 | |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 7911 | int SurfaceFlinger::calculateMaxAcquiredBufferCount(Fps refreshRate, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 7912 | std::chrono::nanoseconds presentLatency) { |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 7913 | auto pipelineDepth = presentLatency.count() / refreshRate.getPeriodNsecs(); |
| 7914 | if (presentLatency.count() % refreshRate.getPeriodNsecs()) { |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 7915 | pipelineDepth++; |
| 7916 | } |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 7917 | return std::max(minAcquiredBuffers, static_cast<int64_t>(pipelineDepth - 1)); |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 7918 | } |
| 7919 | |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 7920 | status_t SurfaceFlinger::getMaxAcquiredBufferCount(int* buffers) const { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 7921 | Fps maxRefreshRate = 60_Hz; |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 7922 | |
| 7923 | if (!getHwComposer().isHeadless()) { |
| 7924 | if (const auto display = getDefaultDisplayDevice()) { |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 7925 | maxRefreshRate = display->refreshRateSelector().getSupportedRefreshRateRange().max; |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7926 | } |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 7927 | } |
| 7928 | |
| 7929 | *buffers = getMaxAcquiredBufferCountForRefreshRate(maxRefreshRate); |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 7930 | return NO_ERROR; |
| 7931 | } |
| 7932 | |
rnlee | ed20fa4 | 2021-08-10 18:00:03 -0700 | [diff] [blame] | 7933 | uint32_t SurfaceFlinger::getMaxAcquiredBufferCountForCurrentRefreshRate(uid_t uid) const { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 7934 | Fps refreshRate = 60_Hz; |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7935 | |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 7936 | if (const auto frameRateOverride = mScheduler->getFrameRateOverride(uid)) { |
| 7937 | refreshRate = *frameRateOverride; |
| 7938 | } else if (!getHwComposer().isHeadless()) { |
Dominik Laskowski | 298b08e | 2022-02-15 13:45:02 -0800 | [diff] [blame] | 7939 | if (const auto display = FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked())) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 7940 | refreshRate = display->refreshRateSelector().getActiveMode().fps; |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7941 | } |
Dominik Laskowski | f8db0f0 | 2021-04-19 11:05:25 -0700 | [diff] [blame] | 7942 | } |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 7943 | |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 7944 | return getMaxAcquiredBufferCountForRefreshRate(refreshRate); |
| 7945 | } |
| 7946 | |
| 7947 | int SurfaceFlinger::getMaxAcquiredBufferCountForRefreshRate(Fps refreshRate) const { |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 7948 | const auto vsyncConfig = |
| 7949 | mScheduler->getVsyncConfiguration().getConfigsForRefreshRate(refreshRate).late; |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 7950 | const auto presentLatency = vsyncConfig.appWorkDuration + vsyncConfig.sfWorkDuration; |
| 7951 | return calculateMaxAcquiredBufferCount(refreshRate, presentLatency); |
| 7952 | } |
| 7953 | |
Dominik Laskowski | e0e0cde | 2021-07-30 10:42:05 -0700 | [diff] [blame] | 7954 | void SurfaceFlinger::sample() { |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 7955 | if (!mLumaSampling || !mRegionSamplingThread) { |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 7956 | return; |
| 7957 | } |
| 7958 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 7959 | const auto scheduledFrameResultOpt = mScheduler->getScheduledFrameResult(); |
| 7960 | const auto scheduleFrameTimeOpt = scheduledFrameResultOpt |
| 7961 | ? std::optional{scheduledFrameResultOpt->callbackTime} |
| 7962 | : std::nullopt; |
| 7963 | mRegionSamplingThread->onCompositionComplete(scheduleFrameTimeOpt); |
Ady Abraham | 562c271 | 2021-05-07 15:10:42 -0700 | [diff] [blame] | 7964 | } |
| 7965 | |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 7966 | void SurfaceFlinger::onActiveDisplaySizeChanged(const DisplayDevice& activeDisplay) { |
| 7967 | mScheduler->onActiveDisplayAreaChanged(activeDisplay.getWidth() * activeDisplay.getHeight()); |
| 7968 | getRenderEngine().onActiveDisplaySizeChanged(activeDisplay.getSize()); |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 7969 | } |
| 7970 | |
Dominik Laskowski | 3de6727 | 2023-08-09 13:15:31 -0400 | [diff] [blame] | 7971 | sp<DisplayDevice> SurfaceFlinger::getActivatableDisplay() const { |
| 7972 | if (mPhysicalDisplays.size() == 1) return nullptr; |
| 7973 | |
| 7974 | // TODO(b/255635821): Choose the pacesetter display, considering both internal and external |
| 7975 | // displays. For now, pick the other internal display, assuming a dual-display foldable. |
| 7976 | return findDisplay([this](const DisplayDevice& display) REQUIRES(mStateLock) { |
| 7977 | const auto idOpt = PhysicalDisplayId::tryCast(display.getId()); |
| 7978 | return idOpt && *idOpt != mActiveDisplayId && display.isPoweredOn() && |
| 7979 | mPhysicalDisplays.get(*idOpt) |
| 7980 | .transform(&PhysicalDisplay::isInternal) |
| 7981 | .value_or(false); |
| 7982 | }); |
| 7983 | } |
| 7984 | |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 7985 | void SurfaceFlinger::onActiveDisplayChangedLocked(const DisplayDevice* inactiveDisplayPtr, |
| 7986 | const DisplayDevice& activeDisplay) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 7987 | SFTRACE_CALL(); |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 7988 | |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 7989 | if (inactiveDisplayPtr) { |
| 7990 | inactiveDisplayPtr->getCompositionDisplay()->setLayerCachingTexturePoolEnabled(false); |
Ady Abraham | db036a8 | 2021-07-16 14:18:34 -0700 | [diff] [blame] | 7991 | } |
| 7992 | |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 7993 | mActiveDisplayId = activeDisplay.getPhysicalId(); |
| 7994 | activeDisplay.getCompositionDisplay()->setLayerCachingTexturePoolEnabled(true); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 7995 | |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 7996 | // TODO(b/255635711): Check for pending mode changes on other displays. |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 7997 | mScheduler->setModeChangePending(false); |
Dominik Laskowski | fdac565 | 2023-06-29 12:01:13 -0400 | [diff] [blame] | 7998 | |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 7999 | mScheduler->setPacesetterDisplay(mActiveDisplayId); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 8000 | |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 8001 | onActiveDisplaySizeChanged(activeDisplay); |
Dominik Laskowski | 39dfc94 | 2023-02-08 16:27:05 -0500 | [diff] [blame] | 8002 | mActiveDisplayTransformHint = activeDisplay.getTransformHint(); |
Leon Scroggins III | 85d4b22 | 2023-05-09 13:58:18 -0400 | [diff] [blame] | 8003 | sActiveDisplayRotationFlags = ui::Transform::toRotationFlags(activeDisplay.getOrientation()); |
Ady Abraham | 85d692d | 2021-09-28 14:08:54 -0700 | [diff] [blame] | 8004 | |
Dominik Laskowski | 1746afd | 2024-04-10 19:21:37 -0400 | [diff] [blame] | 8005 | // Whether or not the policy of the new active/pacesetter display changed while it was inactive |
| 8006 | // (in which case its preferred mode has already been propagated to HWC via setDesiredMode), the |
| 8007 | // Scheduler's cachedModeChangedParams must be initialized to the newly active mode, and the |
| 8008 | // kernel idle timer of the newly active display must be toggled. |
| 8009 | applyRefreshRateSelectorPolicy(mActiveDisplayId, activeDisplay.refreshRateSelector()); |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 8010 | } |
| 8011 | |
Patrick Williams | acd2258 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 8012 | status_t SurfaceFlinger::addWindowInfosListener(const sp<IWindowInfosListener>& windowInfosListener, |
| 8013 | gui::WindowInfosListenerInfo* outInfo) { |
| 8014 | mWindowInfosListenerInvoker->addWindowInfosListener(windowInfosListener, outInfo); |
Patrick Williams | af06b79 | 2023-02-16 17:46:22 -0600 | [diff] [blame] | 8015 | setTransactionFlags(eInputInfoUpdateNeeded); |
chaviw | 60c9d3e | 2021-06-04 12:52:17 -0500 | [diff] [blame] | 8016 | return NO_ERROR; |
| 8017 | } |
| 8018 | |
| 8019 | status_t SurfaceFlinger::removeWindowInfosListener( |
| 8020 | const sp<IWindowInfosListener>& windowInfosListener) const { |
| 8021 | mWindowInfosListenerInvoker->removeWindowInfosListener(windowInfosListener); |
| 8022 | return NO_ERROR; |
| 8023 | } |
| 8024 | |
Patrick Williams | 090ad06 | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 8025 | status_t SurfaceFlinger::getStalledTransactionInfo( |
| 8026 | int pid, std::optional<TransactionHandler::StalledTransactionInfo>& result) { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 8027 | // Used to add a stalled transaction which uses an internal lock. |
| 8028 | ftl::FakeGuard guard(kMainThreadContext); |
Patrick Williams | 090ad06 | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 8029 | result = mTransactionHandler.getStalledTransactionInfo(pid); |
| 8030 | return NO_ERROR; |
| 8031 | } |
| 8032 | |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 8033 | void SurfaceFlinger::updateHdcpLevels(hal::HWDisplayId hwcDisplayId, int32_t connectedLevel, |
| 8034 | int32_t maxLevel) { |
| 8035 | if (!FlagManager::getInstance().connected_display()) { |
| 8036 | return; |
| 8037 | } |
| 8038 | |
| 8039 | Mutex::Autolock lock(mStateLock); |
| 8040 | |
| 8041 | const auto idOpt = getHwComposer().toPhysicalDisplayId(hwcDisplayId); |
| 8042 | if (!idOpt) { |
| 8043 | ALOGE("No display found for HDCP level changed event: connected=%d, max=%d for " |
| 8044 | "display=%" PRIu64, |
| 8045 | connectedLevel, maxLevel, hwcDisplayId); |
| 8046 | return; |
| 8047 | } |
| 8048 | |
| 8049 | const bool isInternalDisplay = |
| 8050 | mPhysicalDisplays.get(*idOpt).transform(&PhysicalDisplay::isInternal).value_or(false); |
| 8051 | if (isInternalDisplay) { |
| 8052 | ALOGW("Unexpected HDCP level changed for internal display: connected=%d, max=%d for " |
| 8053 | "display=%" PRIu64, |
| 8054 | connectedLevel, maxLevel, hwcDisplayId); |
| 8055 | return; |
| 8056 | } |
| 8057 | |
| 8058 | static_cast<void>(mScheduler->schedule([this, displayId = *idOpt, connectedLevel, maxLevel]() { |
| 8059 | if (const auto display = FTL_FAKE_GUARD(mStateLock, getDisplayDeviceLocked(displayId))) { |
| 8060 | Mutex::Autolock lock(mStateLock); |
| 8061 | display->setSecure(connectedLevel >= 2 /* HDCP_V1 */); |
| 8062 | } |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 8063 | mScheduler->onHdcpLevelsChanged(scheduler::Cycle::Render, displayId, connectedLevel, |
| 8064 | maxLevel); |
Huihong Luo | 9ebb7a7 | 2023-06-27 17:01:50 -0700 | [diff] [blame] | 8065 | })); |
| 8066 | } |
| 8067 | |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 8068 | std::shared_ptr<renderengine::ExternalTexture> SurfaceFlinger::getExternalTextureFromBufferData( |
Patrick Williams | f1e5df1 | 2022-10-17 21:37:42 +0000 | [diff] [blame] | 8069 | BufferData& bufferData, const char* layerName, uint64_t transactionId) { |
| 8070 | if (bufferData.buffer && |
| 8071 | exceedsMaxRenderTargetSize(bufferData.buffer->getWidth(), bufferData.buffer->getHeight())) { |
Patrick Williams | ae9ab53 | 2022-11-14 18:41:38 +0000 | [diff] [blame] | 8072 | std::string errorMessage = |
| 8073 | base::StringPrintf("Attempted to create an ExternalTexture with size (%u, %u) for " |
| 8074 | "layer %s that exceeds render target size limit of %u.", |
| 8075 | bufferData.buffer->getWidth(), bufferData.buffer->getHeight(), |
| 8076 | layerName, static_cast<uint32_t>(mMaxRenderTargetSize)); |
| 8077 | ALOGD("%s", errorMessage.c_str()); |
| 8078 | if (bufferData.releaseBufferListener) { |
Huihong Luo | ffee3bc | 2023-01-17 16:14:35 +0000 | [diff] [blame] | 8079 | bufferData.releaseBufferListener->onTransactionQueueStalled( |
| 8080 | String8(errorMessage.c_str())); |
Patrick Williams | ae9ab53 | 2022-11-14 18:41:38 +0000 | [diff] [blame] | 8081 | } |
Patrick Williams | f1e5df1 | 2022-10-17 21:37:42 +0000 | [diff] [blame] | 8082 | return nullptr; |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 8083 | } |
Patrick Williams | f1e5df1 | 2022-10-17 21:37:42 +0000 | [diff] [blame] | 8084 | |
| 8085 | bool cachedBufferChanged = |
| 8086 | bufferData.flags.test(BufferData::BufferDataChange::cachedBufferChanged); |
| 8087 | if (cachedBufferChanged && bufferData.buffer) { |
| 8088 | auto result = ClientCache::getInstance().add(bufferData.cachedBuffer, bufferData.buffer); |
| 8089 | if (result.ok()) { |
| 8090 | return result.value(); |
| 8091 | } |
| 8092 | |
| 8093 | if (result.error() == ClientCache::AddError::CacheFull) { |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 8094 | ALOGE("Attempted to create an ExternalTexture for layer %s but CacheFull", layerName); |
| 8095 | |
| 8096 | if (bufferData.releaseBufferListener) { |
| 8097 | bufferData.releaseBufferListener->onTransactionQueueStalled( |
Huihong Luo | ffee3bc | 2023-01-17 16:14:35 +0000 | [diff] [blame] | 8098 | String8("Buffer processing hung due to full buffer cache")); |
Vishnu Nair | 40fff5c | 2022-11-04 02:46:28 +0000 | [diff] [blame] | 8099 | } |
Patrick Williams | f1e5df1 | 2022-10-17 21:37:42 +0000 | [diff] [blame] | 8100 | } |
| 8101 | |
| 8102 | return nullptr; |
| 8103 | } |
| 8104 | |
| 8105 | if (cachedBufferChanged) { |
| 8106 | return ClientCache::getInstance().get(bufferData.cachedBuffer); |
| 8107 | } |
| 8108 | |
| 8109 | if (bufferData.buffer) { |
| 8110 | return std::make_shared< |
| 8111 | renderengine::impl::ExternalTexture>(bufferData.buffer, getRenderEngine(), |
| 8112 | renderengine::impl::ExternalTexture::Usage:: |
| 8113 | READABLE); |
| 8114 | } |
| 8115 | |
| 8116 | return nullptr; |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 8117 | } |
Vishnu Nair | 0fc7af5 | 2022-01-13 08:11:34 -0800 | [diff] [blame] | 8118 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8119 | void SurfaceFlinger::moveSnapshotsFromCompositionArgs( |
| 8120 | compositionengine::CompositionRefreshArgs& refreshArgs, |
Dominik Laskowski | 16a345b | 2023-04-27 17:07:33 -0400 | [diff] [blame] | 8121 | const std::vector<std::pair<Layer*, LayerFE*>>& layers) { |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 8122 | std::vector<std::unique_ptr<frontend::LayerSnapshot>>& snapshots = |
| 8123 | mLayerSnapshotBuilder.getSnapshots(); |
| 8124 | for (auto [_, layerFE] : layers) { |
| 8125 | auto i = layerFE->mSnapshot->globalZ; |
| 8126 | snapshots[i] = std::move(layerFE->mSnapshot); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8127 | } |
| 8128 | } |
| 8129 | |
| 8130 | std::vector<std::pair<Layer*, LayerFE*>> SurfaceFlinger::moveSnapshotsToCompositionArgs( |
Dominik Laskowski | 16a345b | 2023-04-27 17:07:33 -0400 | [diff] [blame] | 8131 | compositionengine::CompositionRefreshArgs& refreshArgs, bool cursorOnly) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8132 | std::vector<std::pair<Layer*, LayerFE*>> layers; |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 8133 | nsecs_t currentTime = systemTime(); |
| 8134 | const bool needsMetadata = mCompositionEngine->getFeatureFlags().test( |
| 8135 | compositionengine::Feature::kSnapshotLayerMetadata); |
| 8136 | mLayerSnapshotBuilder.forEachSnapshot( |
| 8137 | [&](std::unique_ptr<frontend::LayerSnapshot>& snapshot) FTL_FAKE_GUARD( |
| 8138 | kMainThreadContext) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8139 | if (cursorOnly && |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 8140 | snapshot->compositionType != |
| 8141 | aidl::android::hardware::graphics::composer3::Composition::CURSOR) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8142 | return; |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 8143 | } |
| 8144 | |
| 8145 | if (!snapshot->hasSomethingToDraw()) { |
| 8146 | return; |
| 8147 | } |
| 8148 | |
| 8149 | auto it = mLegacyLayers.find(snapshot->sequence); |
| 8150 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 8151 | "Couldnt find layer object for %s", |
| 8152 | snapshot->getDebugString().c_str()); |
| 8153 | auto& legacyLayer = it->second; |
| 8154 | sp<LayerFE> layerFE = legacyLayer->getCompositionEngineLayerFE(snapshot->path); |
| 8155 | snapshot->fps = getLayerFramerate(currentTime, snapshot->sequence); |
| 8156 | layerFE->mSnapshot = std::move(snapshot); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8157 | refreshArgs.layers.push_back(layerFE); |
Melody Hsu | 86ab512 | 2024-04-17 20:47:55 +0000 | [diff] [blame] | 8158 | layers.emplace_back(legacyLayer.get(), layerFE.get()); |
| 8159 | }, |
| 8160 | [needsMetadata](const frontend::LayerSnapshot& snapshot) { |
| 8161 | return snapshot.isVisible || |
| 8162 | (needsMetadata && |
| 8163 | snapshot.changes.test(frontend::RequestedLayerState::Changes::Metadata)); |
| 8164 | }); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8165 | return layers; |
| 8166 | } |
| 8167 | |
| 8168 | std::function<std::vector<std::pair<Layer*, sp<LayerFE>>>()> |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 8169 | SurfaceFlinger::getLayerSnapshotsForScreenshots( |
| 8170 | std::optional<ui::LayerStack> layerStack, uint32_t uid, |
| 8171 | std::function<bool(const frontend::LayerSnapshot&, bool& outStopTraversal)> |
| 8172 | snapshotFilterFn) { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 8173 | return [&, layerStack, uid]() FTL_FAKE_GUARD(kMainThreadContext) { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8174 | std::vector<std::pair<Layer*, sp<LayerFE>>> layers; |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 8175 | bool stopTraversal = false; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8176 | mLayerSnapshotBuilder.forEachVisibleSnapshot( |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 8177 | [&](std::unique_ptr<frontend::LayerSnapshot>& snapshot) FTL_FAKE_GUARD( |
| 8178 | kMainThreadContext) { |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 8179 | if (stopTraversal) { |
| 8180 | return; |
| 8181 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8182 | if (layerStack && snapshot->outputFilter.layerStack != *layerStack) { |
| 8183 | return; |
| 8184 | } |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 8185 | if (uid != CaptureArgs::UNSET_UID && snapshot->uid != gui::Uid(uid)) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8186 | return; |
| 8187 | } |
| 8188 | if (!snapshot->hasSomethingToDraw()) { |
| 8189 | return; |
| 8190 | } |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 8191 | if (snapshotFilterFn && !snapshotFilterFn(*snapshot, stopTraversal)) { |
| 8192 | return; |
| 8193 | } |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8194 | |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8195 | auto it = mLegacyLayers.find(snapshot->sequence); |
Vishnu Nair | 5943bf6 | 2023-12-08 22:04:20 +0000 | [diff] [blame] | 8196 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mLegacyLayers.end(), |
| 8197 | "Couldnt find layer object for %s", |
| 8198 | snapshot->getDebugString().c_str()); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8199 | Layer* legacyLayer = (it == mLegacyLayers.end()) ? nullptr : it->second.get(); |
Lloyd Pique | 70ddc67 | 2024-04-30 18:20:40 -0700 | [diff] [blame] | 8200 | sp<LayerFE> layerFE = getFactory().createLayerFE(snapshot->name, legacyLayer); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8201 | layerFE->mSnapshot = std::make_unique<frontend::LayerSnapshot>(*snapshot); |
| 8202 | layers.emplace_back(legacyLayer, std::move(layerFE)); |
| 8203 | }); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8204 | |
| 8205 | return layers; |
| 8206 | }; |
| 8207 | } |
| 8208 | |
| 8209 | std::function<std::vector<std::pair<Layer*, sp<LayerFE>>>()> |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 8210 | SurfaceFlinger::getLayerSnapshotsForScreenshots(std::optional<ui::LayerStack> layerStack, |
| 8211 | uint32_t uid, |
| 8212 | std::unordered_set<uint32_t> excludeLayerIds) { |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 8213 | return [&, layerStack, uid, |
| 8214 | excludeLayerIds = std::move(excludeLayerIds)]() FTL_FAKE_GUARD(kMainThreadContext) { |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 8215 | if (excludeLayerIds.empty()) { |
| 8216 | auto getLayerSnapshotsFn = |
| 8217 | getLayerSnapshotsForScreenshots(layerStack, uid, /*snapshotFilterFn=*/nullptr); |
| 8218 | std::vector<std::pair<Layer*, sp<LayerFE>>> layers = getLayerSnapshotsFn(); |
| 8219 | return layers; |
| 8220 | } |
| 8221 | |
| 8222 | frontend::LayerSnapshotBuilder::Args |
| 8223 | args{.root = mLayerHierarchyBuilder.getHierarchy(), |
| 8224 | .layerLifecycleManager = mLayerLifecycleManager, |
| 8225 | .forceUpdate = frontend::LayerSnapshotBuilder::ForceUpdateFlags::HIERARCHY, |
| 8226 | .displays = mFrontEndDisplayInfos, |
| 8227 | .displayChanges = true, |
| 8228 | .globalShadowSettings = mDrawingState.globalShadowSettings, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 8229 | .supportsBlur = mSupportsBlur, |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 8230 | .forceFullDamage = mForceFullDamage, |
| 8231 | .excludeLayerIds = std::move(excludeLayerIds), |
| 8232 | .supportedLayerGenericMetadata = |
| 8233 | getHwComposer().getSupportedLayerGenericMetadata(), |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 8234 | .genericLayerMetadataKeyMap = getGenericLayerMetadataKeyMap(), |
| 8235 | .skipRoundCornersWhenProtected = |
| 8236 | !getRenderEngine().supportsProtectedContent()}; |
Ajinkya Chalke | 0284463 | 2023-03-01 12:10:14 +0000 | [diff] [blame] | 8237 | mLayerSnapshotBuilder.update(args); |
| 8238 | |
| 8239 | auto getLayerSnapshotsFn = |
| 8240 | getLayerSnapshotsForScreenshots(layerStack, uid, /*snapshotFilterFn=*/nullptr); |
| 8241 | std::vector<std::pair<Layer*, sp<LayerFE>>> layers = getLayerSnapshotsFn(); |
| 8242 | |
| 8243 | args.excludeLayerIds.clear(); |
| 8244 | mLayerSnapshotBuilder.update(args); |
| 8245 | |
| 8246 | return layers; |
| 8247 | }; |
| 8248 | } |
| 8249 | |
| 8250 | std::function<std::vector<std::pair<Layer*, sp<LayerFE>>>()> |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8251 | SurfaceFlinger::getLayerSnapshotsForScreenshots(uint32_t rootLayerId, uint32_t uid, |
| 8252 | std::unordered_set<uint32_t> excludeLayerIds, |
Vishnu Nair | 36d5f8e | 2023-03-19 13:31:35 -0700 | [diff] [blame] | 8253 | bool childrenOnly, |
| 8254 | const std::optional<FloatRect>& parentCrop) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8255 | return [&, rootLayerId, uid, excludeLayerIds = std::move(excludeLayerIds), childrenOnly, |
Vishnu Nair | 7be2760 | 2024-03-28 20:27:09 -0700 | [diff] [blame] | 8256 | parentCrop]() FTL_FAKE_GUARD(kMainThreadContext) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8257 | auto root = mLayerHierarchyBuilder.getPartialHierarchy(rootLayerId, childrenOnly); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8258 | frontend::LayerSnapshotBuilder::Args |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8259 | args{.root = root, |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8260 | .layerLifecycleManager = mLayerLifecycleManager, |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 8261 | .forceUpdate = frontend::LayerSnapshotBuilder::ForceUpdateFlags::HIERARCHY, |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8262 | .displays = mFrontEndDisplayInfos, |
| 8263 | .displayChanges = true, |
| 8264 | .globalShadowSettings = mDrawingState.globalShadowSettings, |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 8265 | .supportsBlur = mSupportsBlur, |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8266 | .forceFullDamage = mForceFullDamage, |
Vishnu Nair | 36d5f8e | 2023-03-19 13:31:35 -0700 | [diff] [blame] | 8267 | .parentCrop = parentCrop, |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 8268 | .excludeLayerIds = std::move(excludeLayerIds), |
| 8269 | .supportedLayerGenericMetadata = |
| 8270 | getHwComposer().getSupportedLayerGenericMetadata(), |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 8271 | .genericLayerMetadataKeyMap = getGenericLayerMetadataKeyMap(), |
| 8272 | .skipRoundCornersWhenProtected = |
| 8273 | !getRenderEngine().supportsProtectedContent()}; |
Vishnu Nair | c54fdb0 | 2023-11-21 17:19:58 -0800 | [diff] [blame] | 8274 | // The layer may not exist if it was just created and a screenshot was requested immediately |
| 8275 | // after. In this case, the hierarchy will be empty so we will not render any layers. |
| 8276 | args.rootSnapshot.isSecure = mLayerLifecycleManager.getLayerFromId(rootLayerId) && |
| 8277 | mLayerLifecycleManager.isLayerSecure(rootLayerId); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8278 | mLayerSnapshotBuilder.update(args); |
| 8279 | |
Vishnu Nair | c5a5b6e | 2023-02-28 00:19:07 +0000 | [diff] [blame] | 8280 | auto getLayerSnapshotsFn = |
| 8281 | getLayerSnapshotsForScreenshots({}, uid, /*snapshotFilterFn=*/nullptr); |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 8282 | std::vector<std::pair<Layer*, sp<LayerFE>>> layers = getLayerSnapshotsFn(); |
| 8283 | args.root = mLayerHierarchyBuilder.getHierarchy(); |
| 8284 | args.parentCrop.reset(); |
| 8285 | args.excludeLayerIds.clear(); |
| 8286 | mLayerSnapshotBuilder.update(args); |
| 8287 | return layers; |
| 8288 | }; |
| 8289 | } |
| 8290 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 8291 | void SurfaceFlinger::doActiveLayersTracingIfNeeded(bool isCompositionComputed, |
| 8292 | bool visibleRegionDirty, TimePoint time, |
| 8293 | VsyncId vsyncId) { |
| 8294 | if (!mLayerTracing.isActiveTracingStarted()) { |
| 8295 | return; |
| 8296 | } |
| 8297 | if (isCompositionComputed != |
| 8298 | mLayerTracing.isActiveTracingFlagSet(LayerTracing::Flag::TRACE_COMPOSITION)) { |
| 8299 | return; |
| 8300 | } |
| 8301 | if (!visibleRegionDirty && |
| 8302 | !mLayerTracing.isActiveTracingFlagSet(LayerTracing::Flag::TRACE_BUFFERS)) { |
| 8303 | return; |
| 8304 | } |
| 8305 | auto snapshot = takeLayersSnapshotProto(mLayerTracing.getActiveTracingFlags(), time, vsyncId, |
| 8306 | visibleRegionDirty); |
| 8307 | mLayerTracing.addProtoSnapshotToOstream(std::move(snapshot), LayerTracing::Mode::MODE_ACTIVE); |
| 8308 | } |
| 8309 | |
| 8310 | perfetto::protos::LayersSnapshotProto SurfaceFlinger::takeLayersSnapshotProto( |
| 8311 | uint32_t traceFlags, TimePoint time, VsyncId vsyncId, bool visibleRegionDirty) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 8312 | SFTRACE_CALL(); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 8313 | perfetto::protos::LayersSnapshotProto snapshot; |
| 8314 | snapshot.set_elapsed_realtime_nanos(time.ns()); |
| 8315 | snapshot.set_vsync_id(ftl::to_underlying(vsyncId)); |
| 8316 | snapshot.set_where(visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched"); |
| 8317 | snapshot.set_excludes_composition_state((traceFlags & LayerTracing::Flag::TRACE_COMPOSITION) == |
| 8318 | 0); |
| 8319 | |
| 8320 | auto layers = dumpDrawingStateProto(traceFlags); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 8321 | *snapshot.mutable_layers() = std::move(layers); |
| 8322 | |
| 8323 | if (traceFlags & LayerTracing::Flag::TRACE_HWC) { |
| 8324 | std::string hwcDump; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 8325 | dumpHwc(hwcDump); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 8326 | snapshot.set_hwc_blob(std::move(hwcDump)); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 8327 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 8328 | |
| 8329 | *snapshot.mutable_displays() = dumpDisplayProto(); |
| 8330 | |
| 8331 | return snapshot; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 8332 | } |
| 8333 | |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 8334 | // sfdo functions |
| 8335 | |
| 8336 | void SurfaceFlinger::sfdo_enableRefreshRateOverlay(bool active) { |
| 8337 | auto future = mScheduler->schedule( |
| 8338 | [&]() FTL_FAKE_GUARD(mStateLock) |
| 8339 | FTL_FAKE_GUARD(kMainThreadContext) { enableRefreshRateOverlay(active); }); |
| 8340 | future.wait(); |
| 8341 | } |
| 8342 | |
| 8343 | void SurfaceFlinger::sfdo_setDebugFlash(int delay) { |
| 8344 | if (delay > 0) { |
| 8345 | mDebugFlashDelay = delay; |
| 8346 | } else { |
| 8347 | mDebugFlashDelay = mDebugFlashDelay ? 0 : 1; |
| 8348 | } |
| 8349 | scheduleRepaint(); |
| 8350 | } |
| 8351 | |
| 8352 | void SurfaceFlinger::sfdo_scheduleComposite() { |
| 8353 | scheduleComposite(SurfaceFlinger::FrameHint::kActive); |
| 8354 | } |
| 8355 | |
| 8356 | void SurfaceFlinger::sfdo_scheduleCommit() { |
| 8357 | Mutex::Autolock lock(mStateLock); |
| 8358 | setTransactionFlags(eTransactionNeeded | eDisplayTransactionNeeded | eTraversalNeeded); |
| 8359 | } |
| 8360 | |
Carlos Martinez Romero | b4ea9e6 | 2023-10-25 21:48:56 +0000 | [diff] [blame] | 8361 | void SurfaceFlinger::sfdo_forceClientComposition(bool enabled) { |
| 8362 | mDebugDisableHWC = enabled; |
| 8363 | scheduleRepaint(); |
| 8364 | } |
| 8365 | |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 8366 | // gui::ISurfaceComposer |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8367 | |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 8368 | binder::Status SurfaceComposerAIDL::bootFinished() { |
| 8369 | status_t status = checkAccessPermission(); |
| 8370 | if (status != OK) { |
| 8371 | return binderStatusFromStatusT(status); |
| 8372 | } |
| 8373 | mFlinger->bootFinished(); |
| 8374 | return binder::Status::ok(); |
| 8375 | } |
| 8376 | |
| 8377 | binder::Status SurfaceComposerAIDL::createDisplayEventConnection( |
| 8378 | VsyncSource vsyncSource, EventRegistration eventRegistration, |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 8379 | const sp<IBinder>& layerHandle, sp<IDisplayEventConnection>* outConnection) { |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 8380 | sp<IDisplayEventConnection> conn = |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 8381 | mFlinger->createDisplayEventConnection(vsyncSource, eventRegistration, layerHandle); |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 8382 | if (conn == nullptr) { |
| 8383 | *outConnection = nullptr; |
| 8384 | return binderStatusFromStatusT(BAD_VALUE); |
| 8385 | } else { |
| 8386 | *outConnection = conn; |
| 8387 | return binder::Status::ok(); |
| 8388 | } |
| 8389 | } |
| 8390 | |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 8391 | binder::Status SurfaceComposerAIDL::createConnection(sp<gui::ISurfaceComposerClient>* outClient) { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 8392 | const sp<Client> client = sp<Client>::make(mFlinger); |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 8393 | if (client->initCheck() == NO_ERROR) { |
| 8394 | *outClient = client; |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 8395 | if (FlagManager::getInstance().misc1()) { |
Ady Abraham | 07d03c4 | 2023-09-27 19:15:08 -0700 | [diff] [blame] | 8396 | const int policy = SCHED_FIFO; |
| 8397 | client->setMinSchedulerPolicy(policy, sched_get_priority_min(policy)); |
| 8398 | } |
Huihong Luo | d3d8f8e | 2022-03-08 14:48:46 -0800 | [diff] [blame] | 8399 | return binder::Status::ok(); |
| 8400 | } else { |
| 8401 | *outClient = nullptr; |
| 8402 | return binderStatusFromStatusT(BAD_VALUE); |
| 8403 | } |
| 8404 | } |
| 8405 | |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 8406 | binder::Status SurfaceComposerAIDL::createVirtualDisplay(const std::string& displayName, |
| 8407 | bool isSecure, const std::string& uniqueId, |
| 8408 | float requestedRefreshRate, |
| 8409 | sp<IBinder>* outDisplay) { |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8410 | status_t status = checkAccessPermission(); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8411 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8412 | return binderStatusFromStatusT(status); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8413 | } |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 8414 | *outDisplay = |
| 8415 | mFlinger->createVirtualDisplay(displayName, isSecure, uniqueId, requestedRefreshRate); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8416 | return binder::Status::ok(); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8417 | } |
| 8418 | |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 8419 | binder::Status SurfaceComposerAIDL::destroyVirtualDisplay(const sp<IBinder>& displayToken) { |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8420 | status_t status = checkAccessPermission(); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8421 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8422 | return binderStatusFromStatusT(status); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8423 | } |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 8424 | return binder::Status::fromStatusT(mFlinger->destroyVirtualDisplay(displayToken)); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8425 | } |
| 8426 | |
| 8427 | binder::Status SurfaceComposerAIDL::getPhysicalDisplayIds(std::vector<int64_t>* outDisplayIds) { |
| 8428 | std::vector<PhysicalDisplayId> physicalDisplayIds = mFlinger->getPhysicalDisplayIds(); |
| 8429 | std::vector<int64_t> displayIds; |
| 8430 | displayIds.reserve(physicalDisplayIds.size()); |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 8431 | for (const auto id : physicalDisplayIds) { |
| 8432 | displayIds.push_back(static_cast<int64_t>(id.value)); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8433 | } |
Alan Ding | fc6eab5 | 2024-05-02 18:28:45 -0700 | [diff] [blame] | 8434 | *outDisplayIds = std::move(displayIds); |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8435 | return binder::Status::ok(); |
| 8436 | } |
| 8437 | |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8438 | binder::Status SurfaceComposerAIDL::getPhysicalDisplayToken(int64_t displayId, |
| 8439 | sp<IBinder>* outDisplay) { |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 8440 | status_t status = checkAccessPermission(); |
| 8441 | if (status != OK) { |
| 8442 | return binderStatusFromStatusT(status); |
| 8443 | } |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 8444 | const auto id = DisplayId::fromValue<PhysicalDisplayId>(static_cast<uint64_t>(displayId)); |
| 8445 | *outDisplay = mFlinger->getPhysicalDisplayToken(*id); |
| 8446 | return binder::Status::ok(); |
| 8447 | } |
| 8448 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8449 | binder::Status SurfaceComposerAIDL::setPowerMode(const sp<IBinder>& display, int mode) { |
| 8450 | status_t status = checkAccessPermission(); |
| 8451 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8452 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8453 | } |
| 8454 | mFlinger->setPowerMode(display, mode); |
| 8455 | return binder::Status::ok(); |
| 8456 | } |
| 8457 | |
Huihong Luo | 0a81aa3 | 2022-02-22 16:02:36 -0800 | [diff] [blame] | 8458 | binder::Status SurfaceComposerAIDL::getSupportedFrameTimestamps( |
| 8459 | std::vector<FrameEvent>* outSupported) { |
| 8460 | status_t status; |
| 8461 | if (!outSupported) { |
| 8462 | status = UNEXPECTED_NULL; |
| 8463 | } else { |
| 8464 | outSupported->clear(); |
| 8465 | status = mFlinger->getSupportedFrameTimestamps(outSupported); |
| 8466 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8467 | return binderStatusFromStatusT(status); |
Huihong Luo | 0a81aa3 | 2022-02-22 16:02:36 -0800 | [diff] [blame] | 8468 | } |
| 8469 | |
Huihong Luo | aa7fc2e | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8470 | binder::Status SurfaceComposerAIDL::getDisplayStats(const sp<IBinder>& display, |
| 8471 | gui::DisplayStatInfo* outStatInfo) { |
| 8472 | DisplayStatInfo statInfo; |
| 8473 | status_t status = mFlinger->getDisplayStats(display, &statInfo); |
| 8474 | if (status == NO_ERROR) { |
| 8475 | outStatInfo->vsyncTime = static_cast<long>(statInfo.vsyncTime); |
| 8476 | outStatInfo->vsyncPeriod = static_cast<long>(statInfo.vsyncPeriod); |
| 8477 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8478 | return binderStatusFromStatusT(status); |
Huihong Luo | aa7fc2e | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8479 | } |
| 8480 | |
| 8481 | binder::Status SurfaceComposerAIDL::getDisplayState(const sp<IBinder>& display, |
| 8482 | gui::DisplayState* outState) { |
| 8483 | ui::DisplayState state; |
| 8484 | status_t status = mFlinger->getDisplayState(display, &state); |
| 8485 | if (status == NO_ERROR) { |
| 8486 | outState->layerStack = state.layerStack.id; |
| 8487 | outState->orientation = static_cast<gui::Rotation>(state.orientation); |
| 8488 | outState->layerStackSpaceRect.width = state.layerStackSpaceRect.width; |
| 8489 | outState->layerStackSpaceRect.height = state.layerStackSpaceRect.height; |
| 8490 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8491 | return binderStatusFromStatusT(status); |
Huihong Luo | aa7fc2e | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8492 | } |
| 8493 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 8494 | binder::Status SurfaceComposerAIDL::getStaticDisplayInfo(int64_t displayId, |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 8495 | gui::StaticDisplayInfo* outInfo) { |
| 8496 | using Tag = gui::DeviceProductInfo::ManufactureOrModelDate::Tag; |
| 8497 | ui::StaticDisplayInfo info; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 8498 | |
| 8499 | status_t status = mFlinger->getStaticDisplayInfo(displayId, &info); |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 8500 | if (status == NO_ERROR) { |
| 8501 | // convert ui::StaticDisplayInfo to gui::StaticDisplayInfo |
| 8502 | outInfo->connectionType = static_cast<gui::DisplayConnectionType>(info.connectionType); |
| 8503 | outInfo->density = info.density; |
| 8504 | outInfo->secure = info.secure; |
| 8505 | outInfo->installOrientation = static_cast<gui::Rotation>(info.installOrientation); |
| 8506 | |
Snild Dolkow | 8d67564 | 2023-07-14 12:43:35 +0200 | [diff] [blame] | 8507 | if (const std::optional<DeviceProductInfo> dpi = info.deviceProductInfo) { |
| 8508 | gui::DeviceProductInfo dinfo; |
| 8509 | dinfo.name = std::move(dpi->name); |
| 8510 | dinfo.manufacturerPnpId = std::vector<uint8_t>(dpi->manufacturerPnpId.begin(), |
| 8511 | dpi->manufacturerPnpId.end()); |
| 8512 | dinfo.productId = dpi->productId; |
| 8513 | dinfo.relativeAddress = |
| 8514 | std::vector<uint8_t>(dpi->relativeAddress.begin(), dpi->relativeAddress.end()); |
| 8515 | if (const auto* model = |
| 8516 | std::get_if<DeviceProductInfo::ModelYear>(&dpi->manufactureOrModelDate)) { |
| 8517 | gui::DeviceProductInfo::ModelYear modelYear; |
| 8518 | modelYear.year = model->year; |
| 8519 | dinfo.manufactureOrModelDate.set<Tag::modelYear>(modelYear); |
| 8520 | } else if (const auto* manufacture = std::get_if<DeviceProductInfo::ManufactureYear>( |
| 8521 | &dpi->manufactureOrModelDate)) { |
| 8522 | gui::DeviceProductInfo::ManufactureYear date; |
| 8523 | date.modelYear.year = manufacture->year; |
| 8524 | dinfo.manufactureOrModelDate.set<Tag::manufactureYear>(date); |
| 8525 | } else if (const auto* manufacture = |
| 8526 | std::get_if<DeviceProductInfo::ManufactureWeekAndYear>( |
| 8527 | &dpi->manufactureOrModelDate)) { |
| 8528 | gui::DeviceProductInfo::ManufactureWeekAndYear date; |
| 8529 | date.manufactureYear.modelYear.year = manufacture->year; |
| 8530 | date.week = manufacture->week; |
| 8531 | dinfo.manufactureOrModelDate.set<Tag::manufactureWeekAndYear>(date); |
| 8532 | } |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 8533 | |
Snild Dolkow | 8d67564 | 2023-07-14 12:43:35 +0200 | [diff] [blame] | 8534 | outInfo->deviceProductInfo = dinfo; |
| 8535 | } |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 8536 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8537 | return binderStatusFromStatusT(status); |
Huihong Luo | a79ddf4 | 2022-02-17 00:01:38 -0800 | [diff] [blame] | 8538 | } |
| 8539 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 8540 | void SurfaceComposerAIDL::getDynamicDisplayInfoInternal(ui::DynamicDisplayInfo& info, |
| 8541 | gui::DynamicDisplayInfo*& outInfo) { |
| 8542 | // convert ui::DynamicDisplayInfo to gui::DynamicDisplayInfo |
| 8543 | outInfo->supportedDisplayModes.clear(); |
| 8544 | outInfo->supportedDisplayModes.reserve(info.supportedDisplayModes.size()); |
| 8545 | for (const auto& mode : info.supportedDisplayModes) { |
| 8546 | gui::DisplayMode outMode; |
| 8547 | outMode.id = mode.id; |
| 8548 | outMode.resolution.width = mode.resolution.width; |
| 8549 | outMode.resolution.height = mode.resolution.height; |
| 8550 | outMode.xDpi = mode.xDpi; |
| 8551 | outMode.yDpi = mode.yDpi; |
Alec Mouri | 55e3103 | 2023-10-02 20:34:18 +0000 | [diff] [blame] | 8552 | outMode.peakRefreshRate = mode.peakRefreshRate; |
Alec Mouri | d122a1c | 2023-09-30 01:36:33 +0000 | [diff] [blame] | 8553 | outMode.vsyncRate = mode.vsyncRate; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 8554 | outMode.appVsyncOffset = mode.appVsyncOffset; |
| 8555 | outMode.sfVsyncOffset = mode.sfVsyncOffset; |
| 8556 | outMode.presentationDeadline = mode.presentationDeadline; |
| 8557 | outMode.group = mode.group; |
| 8558 | std::transform(mode.supportedHdrTypes.begin(), mode.supportedHdrTypes.end(), |
| 8559 | std::back_inserter(outMode.supportedHdrTypes), |
| 8560 | [](const ui::Hdr& value) { return static_cast<int32_t>(value); }); |
| 8561 | outInfo->supportedDisplayModes.push_back(outMode); |
| 8562 | } |
| 8563 | |
| 8564 | outInfo->activeDisplayModeId = info.activeDisplayModeId; |
| 8565 | outInfo->renderFrameRate = info.renderFrameRate; |
| 8566 | |
| 8567 | outInfo->supportedColorModes.clear(); |
| 8568 | outInfo->supportedColorModes.reserve(info.supportedColorModes.size()); |
| 8569 | for (const auto& cmode : info.supportedColorModes) { |
| 8570 | outInfo->supportedColorModes.push_back(static_cast<int32_t>(cmode)); |
| 8571 | } |
| 8572 | |
| 8573 | outInfo->activeColorMode = static_cast<int32_t>(info.activeColorMode); |
| 8574 | |
| 8575 | gui::HdrCapabilities& hdrCapabilities = outInfo->hdrCapabilities; |
| 8576 | hdrCapabilities.supportedHdrTypes.clear(); |
| 8577 | hdrCapabilities.supportedHdrTypes.reserve(info.hdrCapabilities.getSupportedHdrTypes().size()); |
| 8578 | for (const auto& hdr : info.hdrCapabilities.getSupportedHdrTypes()) { |
| 8579 | hdrCapabilities.supportedHdrTypes.push_back(static_cast<int32_t>(hdr)); |
| 8580 | } |
| 8581 | hdrCapabilities.maxLuminance = info.hdrCapabilities.getDesiredMaxLuminance(); |
| 8582 | hdrCapabilities.maxAverageLuminance = info.hdrCapabilities.getDesiredMaxAverageLuminance(); |
| 8583 | hdrCapabilities.minLuminance = info.hdrCapabilities.getDesiredMinLuminance(); |
| 8584 | |
| 8585 | outInfo->autoLowLatencyModeSupported = info.autoLowLatencyModeSupported; |
| 8586 | outInfo->gameContentTypeSupported = info.gameContentTypeSupported; |
| 8587 | outInfo->preferredBootDisplayMode = info.preferredBootDisplayMode; |
| 8588 | } |
| 8589 | |
| 8590 | binder::Status SurfaceComposerAIDL::getDynamicDisplayInfoFromToken( |
| 8591 | const sp<IBinder>& display, gui::DynamicDisplayInfo* outInfo) { |
Huihong Luo | 38603fd | 2022-02-21 14:32:54 -0800 | [diff] [blame] | 8592 | ui::DynamicDisplayInfo info; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 8593 | status_t status = mFlinger->getDynamicDisplayInfoFromToken(display, &info); |
Huihong Luo | 38603fd | 2022-02-21 14:32:54 -0800 | [diff] [blame] | 8594 | if (status == NO_ERROR) { |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 8595 | getDynamicDisplayInfoInternal(info, outInfo); |
| 8596 | } |
| 8597 | return binderStatusFromStatusT(status); |
| 8598 | } |
Marc Kassis | bdf7e4b | 2022-11-04 17:26:48 +0100 | [diff] [blame] | 8599 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 8600 | binder::Status SurfaceComposerAIDL::getDynamicDisplayInfoFromId(int64_t displayId, |
| 8601 | gui::DynamicDisplayInfo* outInfo) { |
| 8602 | ui::DynamicDisplayInfo info; |
| 8603 | status_t status = mFlinger->getDynamicDisplayInfoFromId(displayId, &info); |
| 8604 | if (status == NO_ERROR) { |
| 8605 | getDynamicDisplayInfoInternal(info, outInfo); |
Huihong Luo | 38603fd | 2022-02-21 14:32:54 -0800 | [diff] [blame] | 8606 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8607 | return binderStatusFromStatusT(status); |
Huihong Luo | 38603fd | 2022-02-21 14:32:54 -0800 | [diff] [blame] | 8608 | } |
| 8609 | |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 8610 | binder::Status SurfaceComposerAIDL::getDisplayNativePrimaries(const sp<IBinder>& display, |
| 8611 | gui::DisplayPrimaries* outPrimaries) { |
| 8612 | ui::DisplayPrimaries primaries; |
| 8613 | status_t status = mFlinger->getDisplayNativePrimaries(display, primaries); |
| 8614 | if (status == NO_ERROR) { |
| 8615 | outPrimaries->red.X = primaries.red.X; |
| 8616 | outPrimaries->red.Y = primaries.red.Y; |
| 8617 | outPrimaries->red.Z = primaries.red.Z; |
| 8618 | |
| 8619 | outPrimaries->green.X = primaries.green.X; |
| 8620 | outPrimaries->green.Y = primaries.green.Y; |
| 8621 | outPrimaries->green.Z = primaries.green.Z; |
| 8622 | |
| 8623 | outPrimaries->blue.X = primaries.blue.X; |
| 8624 | outPrimaries->blue.Y = primaries.blue.Y; |
| 8625 | outPrimaries->blue.Z = primaries.blue.Z; |
| 8626 | |
| 8627 | outPrimaries->white.X = primaries.white.X; |
| 8628 | outPrimaries->white.Y = primaries.white.Y; |
| 8629 | outPrimaries->white.Z = primaries.white.Z; |
| 8630 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8631 | return binderStatusFromStatusT(status); |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 8632 | } |
| 8633 | |
| 8634 | binder::Status SurfaceComposerAIDL::setActiveColorMode(const sp<IBinder>& display, int colorMode) { |
| 8635 | status_t status = checkAccessPermission(); |
| 8636 | if (status == OK) { |
| 8637 | status = mFlinger->setActiveColorMode(display, static_cast<ui::ColorMode>(colorMode)); |
| 8638 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8639 | return binderStatusFromStatusT(status); |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 8640 | } |
| 8641 | |
| 8642 | binder::Status SurfaceComposerAIDL::setBootDisplayMode(const sp<IBinder>& display, |
| 8643 | int displayModeId) { |
| 8644 | status_t status = checkAccessPermission(); |
| 8645 | if (status == OK) { |
Dominik Laskowski | b363c4c | 2022-08-02 14:03:41 -0700 | [diff] [blame] | 8646 | status = mFlinger->setBootDisplayMode(display, DisplayModeId{displayModeId}); |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 8647 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8648 | return binderStatusFromStatusT(status); |
Huihong Luo | ca3d9a4 | 2022-02-22 11:07:34 -0800 | [diff] [blame] | 8649 | } |
| 8650 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8651 | binder::Status SurfaceComposerAIDL::clearBootDisplayMode(const sp<IBinder>& display) { |
| 8652 | status_t status = checkAccessPermission(); |
| 8653 | if (status == OK) { |
| 8654 | status = mFlinger->clearBootDisplayMode(display); |
| 8655 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8656 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8657 | } |
| 8658 | |
Sally Qi | 0cbd08b | 2022-08-17 12:12:28 -0700 | [diff] [blame] | 8659 | binder::Status SurfaceComposerAIDL::getOverlaySupport(gui::OverlayProperties* outProperties) { |
| 8660 | status_t status = checkAccessPermission(); |
| 8661 | if (status == OK) { |
| 8662 | status = mFlinger->getOverlaySupport(outProperties); |
| 8663 | } |
| 8664 | return binderStatusFromStatusT(status); |
| 8665 | } |
| 8666 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8667 | binder::Status SurfaceComposerAIDL::getBootDisplayModeSupport(bool* outMode) { |
| 8668 | status_t status = checkAccessPermission(); |
| 8669 | if (status == OK) { |
| 8670 | status = mFlinger->getBootDisplayModeSupport(outMode); |
| 8671 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8672 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8673 | } |
| 8674 | |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 8675 | binder::Status SurfaceComposerAIDL::getHdrConversionCapabilities( |
| 8676 | std::vector<gui::HdrConversionCapability>* hdrConversionCapabilities) { |
| 8677 | status_t status = checkAccessPermission(); |
| 8678 | if (status == OK) { |
| 8679 | status = mFlinger->getHdrConversionCapabilities(hdrConversionCapabilities); |
| 8680 | } |
| 8681 | return binderStatusFromStatusT(status); |
| 8682 | } |
| 8683 | |
| 8684 | binder::Status SurfaceComposerAIDL::setHdrConversionStrategy( |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 8685 | const gui::HdrConversionStrategy& hdrConversionStrategy, |
| 8686 | int32_t* outPreferredHdrOutputType) { |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 8687 | status_t status = checkAccessPermission(); |
| 8688 | if (status == OK) { |
Kriti Dang | d432bb5 | 2023-02-09 18:21:04 +0100 | [diff] [blame] | 8689 | status = mFlinger->setHdrConversionStrategy(hdrConversionStrategy, |
| 8690 | outPreferredHdrOutputType); |
Kriti Dang | 674b937 | 2022-11-18 10:58:44 +0100 | [diff] [blame] | 8691 | } |
| 8692 | return binderStatusFromStatusT(status); |
| 8693 | } |
| 8694 | |
| 8695 | binder::Status SurfaceComposerAIDL::getHdrOutputConversionSupport(bool* outMode) { |
| 8696 | status_t status = checkAccessPermission(); |
| 8697 | if (status == OK) { |
| 8698 | status = mFlinger->getHdrOutputConversionSupport(outMode); |
| 8699 | } |
| 8700 | return binderStatusFromStatusT(status); |
| 8701 | } |
| 8702 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8703 | binder::Status SurfaceComposerAIDL::setAutoLowLatencyMode(const sp<IBinder>& display, bool on) { |
| 8704 | status_t status = checkAccessPermission(); |
| 8705 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8706 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8707 | } |
| 8708 | mFlinger->setAutoLowLatencyMode(display, on); |
| 8709 | return binder::Status::ok(); |
| 8710 | } |
| 8711 | |
| 8712 | binder::Status SurfaceComposerAIDL::setGameContentType(const sp<IBinder>& display, bool on) { |
| 8713 | status_t status = checkAccessPermission(); |
| 8714 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8715 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8716 | } |
| 8717 | mFlinger->setGameContentType(display, on); |
| 8718 | return binder::Status::ok(); |
| 8719 | } |
| 8720 | |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 8721 | binder::Status SurfaceComposerAIDL::captureDisplay( |
| 8722 | const DisplayCaptureArgs& args, const sp<IScreenCaptureListener>& captureListener) { |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8723 | mFlinger->captureDisplay(args, captureListener); |
| 8724 | return binderStatusFromStatusT(NO_ERROR); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 8725 | } |
| 8726 | |
| 8727 | binder::Status SurfaceComposerAIDL::captureDisplayById( |
John Reck | 7e4c487 | 2023-11-14 18:31:03 -0500 | [diff] [blame] | 8728 | int64_t displayId, const CaptureArgs& args, |
| 8729 | const sp<IScreenCaptureListener>& captureListener) { |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8730 | // status_t status; |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 8731 | IPCThreadState* ipc = IPCThreadState::self(); |
| 8732 | const int uid = ipc->getCallingUid(); |
| 8733 | if (uid == AID_ROOT || uid == AID_GRAPHICS || uid == AID_SYSTEM || uid == AID_SHELL) { |
| 8734 | std::optional<DisplayId> id = DisplayId::fromValue(static_cast<uint64_t>(displayId)); |
John Reck | 7e4c487 | 2023-11-14 18:31:03 -0500 | [diff] [blame] | 8735 | mFlinger->captureDisplay(*id, args, captureListener); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 8736 | } else { |
Melody Hsu | 70c2ee6 | 2024-03-29 21:58:45 +0000 | [diff] [blame] | 8737 | ALOGD("Permission denied to captureDisplayById"); |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8738 | invokeScreenCaptureError(PERMISSION_DENIED, captureListener); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 8739 | } |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8740 | return binderStatusFromStatusT(NO_ERROR); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 8741 | } |
| 8742 | |
Chavi Weingarten | f6fb445 | 2024-01-23 21:10:30 +0000 | [diff] [blame] | 8743 | binder::Status SurfaceComposerAIDL::captureLayersSync(const LayerCaptureArgs& args, |
| 8744 | ScreenCaptureResults* outResults) { |
| 8745 | *outResults = mFlinger->captureLayersSync(args); |
| 8746 | return binderStatusFromStatusT(NO_ERROR); |
| 8747 | } |
| 8748 | |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 8749 | binder::Status SurfaceComposerAIDL::captureLayers( |
| 8750 | const LayerCaptureArgs& args, const sp<IScreenCaptureListener>& captureListener) { |
jeimysantiago | 9be7d0c | 2023-07-12 18:37:06 +0000 | [diff] [blame] | 8751 | mFlinger->captureLayers(args, captureListener); |
| 8752 | return binderStatusFromStatusT(NO_ERROR); |
Huihong Luo | f502922 | 2021-12-16 14:33:46 -0800 | [diff] [blame] | 8753 | } |
| 8754 | |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 8755 | binder::Status SurfaceComposerAIDL::overrideHdrTypes(const sp<IBinder>& display, |
| 8756 | const std::vector<int32_t>& hdrTypes) { |
| 8757 | // overrideHdrTypes is used by CTS tests, which acquire the necessary |
| 8758 | // permission dynamically. Don't use the permission cache for this check. |
| 8759 | status_t status = checkAccessPermission(false); |
| 8760 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8761 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 8762 | } |
| 8763 | |
| 8764 | std::vector<ui::Hdr> hdrTypesVector; |
| 8765 | for (int32_t i : hdrTypes) { |
| 8766 | hdrTypesVector.push_back(static_cast<ui::Hdr>(i)); |
| 8767 | } |
| 8768 | status = mFlinger->overrideHdrTypes(display, hdrTypesVector); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8769 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 8770 | } |
| 8771 | |
| 8772 | binder::Status SurfaceComposerAIDL::onPullAtom(int32_t atomId, gui::PullAtomData* outPullData) { |
| 8773 | status_t status; |
| 8774 | const int uid = IPCThreadState::self()->getCallingUid(); |
| 8775 | if (uid != AID_SYSTEM) { |
| 8776 | status = PERMISSION_DENIED; |
| 8777 | } else { |
| 8778 | status = mFlinger->onPullAtom(atomId, &outPullData->data, &outPullData->success); |
| 8779 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8780 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 8781 | } |
| 8782 | |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 8783 | binder::Status SurfaceComposerAIDL::getCompositionPreference(gui::CompositionPreference* outPref) { |
| 8784 | ui::Dataspace dataspace; |
| 8785 | ui::PixelFormat pixelFormat; |
| 8786 | ui::Dataspace wideColorGamutDataspace; |
| 8787 | ui::PixelFormat wideColorGamutPixelFormat; |
| 8788 | status_t status = |
| 8789 | mFlinger->getCompositionPreference(&dataspace, &pixelFormat, &wideColorGamutDataspace, |
| 8790 | &wideColorGamutPixelFormat); |
| 8791 | if (status == NO_ERROR) { |
| 8792 | outPref->defaultDataspace = static_cast<int32_t>(dataspace); |
| 8793 | outPref->defaultPixelFormat = static_cast<int32_t>(pixelFormat); |
| 8794 | outPref->wideColorGamutDataspace = static_cast<int32_t>(wideColorGamutDataspace); |
| 8795 | outPref->wideColorGamutPixelFormat = static_cast<int32_t>(wideColorGamutPixelFormat); |
| 8796 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8797 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 8798 | } |
| 8799 | |
| 8800 | binder::Status SurfaceComposerAIDL::getDisplayedContentSamplingAttributes( |
| 8801 | const sp<IBinder>& display, gui::ContentSamplingAttributes* outAttrs) { |
| 8802 | status_t status = checkAccessPermission(); |
| 8803 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8804 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 8805 | } |
| 8806 | |
| 8807 | ui::PixelFormat format; |
| 8808 | ui::Dataspace dataspace; |
| 8809 | uint8_t componentMask; |
| 8810 | status = mFlinger->getDisplayedContentSamplingAttributes(display, &format, &dataspace, |
| 8811 | &componentMask); |
| 8812 | if (status == NO_ERROR) { |
| 8813 | outAttrs->format = static_cast<int32_t>(format); |
| 8814 | outAttrs->dataspace = static_cast<int32_t>(dataspace); |
| 8815 | outAttrs->componentMask = static_cast<int8_t>(componentMask); |
| 8816 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8817 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 8818 | } |
| 8819 | |
| 8820 | binder::Status SurfaceComposerAIDL::setDisplayContentSamplingEnabled(const sp<IBinder>& display, |
| 8821 | bool enable, |
| 8822 | int8_t componentMask, |
| 8823 | int64_t maxFrames) { |
| 8824 | status_t status = checkAccessPermission(); |
| 8825 | if (status == OK) { |
| 8826 | status = mFlinger->setDisplayContentSamplingEnabled(display, enable, |
| 8827 | static_cast<uint8_t>(componentMask), |
| 8828 | static_cast<uint64_t>(maxFrames)); |
| 8829 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8830 | return binderStatusFromStatusT(status); |
| 8831 | } |
| 8832 | |
| 8833 | binder::Status SurfaceComposerAIDL::getDisplayedContentSample(const sp<IBinder>& display, |
| 8834 | int64_t maxFrames, int64_t timestamp, |
| 8835 | gui::DisplayedFrameStats* outStats) { |
| 8836 | if (!outStats) { |
| 8837 | return binderStatusFromStatusT(BAD_VALUE); |
| 8838 | } |
| 8839 | |
| 8840 | status_t status = checkAccessPermission(); |
| 8841 | if (status != OK) { |
| 8842 | return binderStatusFromStatusT(status); |
| 8843 | } |
| 8844 | |
| 8845 | DisplayedFrameStats stats; |
| 8846 | status = mFlinger->getDisplayedContentSample(display, static_cast<uint64_t>(maxFrames), |
| 8847 | static_cast<uint64_t>(timestamp), &stats); |
| 8848 | if (status == NO_ERROR) { |
| 8849 | // convert from ui::DisplayedFrameStats to gui::DisplayedFrameStats |
| 8850 | outStats->numFrames = static_cast<int64_t>(stats.numFrames); |
| 8851 | outStats->component_0_sample.reserve(stats.component_0_sample.size()); |
| 8852 | for (const auto& s : stats.component_0_sample) { |
| 8853 | outStats->component_0_sample.push_back(static_cast<int64_t>(s)); |
| 8854 | } |
| 8855 | outStats->component_1_sample.reserve(stats.component_1_sample.size()); |
| 8856 | for (const auto& s : stats.component_1_sample) { |
| 8857 | outStats->component_1_sample.push_back(static_cast<int64_t>(s)); |
| 8858 | } |
| 8859 | outStats->component_2_sample.reserve(stats.component_2_sample.size()); |
| 8860 | for (const auto& s : stats.component_2_sample) { |
| 8861 | outStats->component_2_sample.push_back(static_cast<int64_t>(s)); |
| 8862 | } |
| 8863 | outStats->component_3_sample.reserve(stats.component_3_sample.size()); |
| 8864 | for (const auto& s : stats.component_3_sample) { |
| 8865 | outStats->component_3_sample.push_back(static_cast<int64_t>(s)); |
| 8866 | } |
| 8867 | } |
| 8868 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 8869 | } |
| 8870 | |
| 8871 | binder::Status SurfaceComposerAIDL::getProtectedContentSupport(bool* outSupported) { |
| 8872 | status_t status = mFlinger->getProtectedContentSupport(outSupported); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8873 | return binderStatusFromStatusT(status); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 8874 | } |
| 8875 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8876 | binder::Status SurfaceComposerAIDL::isWideColorDisplay(const sp<IBinder>& token, |
| 8877 | bool* outIsWideColorDisplay) { |
| 8878 | status_t status = mFlinger->isWideColorDisplay(token, outIsWideColorDisplay); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8879 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8880 | } |
| 8881 | |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8882 | binder::Status SurfaceComposerAIDL::addRegionSamplingListener( |
| 8883 | const gui::ARect& samplingArea, const sp<IBinder>& stopLayerHandle, |
| 8884 | const sp<gui::IRegionSamplingListener>& listener) { |
| 8885 | status_t status = checkReadFrameBufferPermission(); |
| 8886 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8887 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8888 | } |
| 8889 | android::Rect rect; |
| 8890 | rect.left = samplingArea.left; |
| 8891 | rect.top = samplingArea.top; |
| 8892 | rect.right = samplingArea.right; |
| 8893 | rect.bottom = samplingArea.bottom; |
| 8894 | status = mFlinger->addRegionSamplingListener(rect, stopLayerHandle, listener); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8895 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8896 | } |
| 8897 | |
| 8898 | binder::Status SurfaceComposerAIDL::removeRegionSamplingListener( |
| 8899 | const sp<gui::IRegionSamplingListener>& listener) { |
| 8900 | status_t status = checkReadFrameBufferPermission(); |
| 8901 | if (status == OK) { |
| 8902 | status = mFlinger->removeRegionSamplingListener(listener); |
| 8903 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8904 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8905 | } |
| 8906 | |
| 8907 | binder::Status SurfaceComposerAIDL::addFpsListener(int32_t taskId, |
| 8908 | const sp<gui::IFpsListener>& listener) { |
| 8909 | status_t status = checkReadFrameBufferPermission(); |
| 8910 | if (status == OK) { |
| 8911 | status = mFlinger->addFpsListener(taskId, listener); |
| 8912 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8913 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8914 | } |
| 8915 | |
| 8916 | binder::Status SurfaceComposerAIDL::removeFpsListener(const sp<gui::IFpsListener>& listener) { |
| 8917 | status_t status = checkReadFrameBufferPermission(); |
| 8918 | if (status == OK) { |
| 8919 | status = mFlinger->removeFpsListener(listener); |
| 8920 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8921 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8922 | } |
| 8923 | |
| 8924 | binder::Status SurfaceComposerAIDL::addTunnelModeEnabledListener( |
| 8925 | const sp<gui::ITunnelModeEnabledListener>& listener) { |
| 8926 | status_t status = checkAccessPermission(); |
| 8927 | if (status == OK) { |
| 8928 | status = mFlinger->addTunnelModeEnabledListener(listener); |
| 8929 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8930 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8931 | } |
| 8932 | |
| 8933 | binder::Status SurfaceComposerAIDL::removeTunnelModeEnabledListener( |
| 8934 | const sp<gui::ITunnelModeEnabledListener>& listener) { |
| 8935 | status_t status = checkAccessPermission(); |
| 8936 | if (status == OK) { |
| 8937 | status = mFlinger->removeTunnelModeEnabledListener(listener); |
| 8938 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8939 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8940 | } |
| 8941 | |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 8942 | binder::Status SurfaceComposerAIDL::setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, |
| 8943 | const gui::DisplayModeSpecs& specs) { |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8944 | status_t status = checkAccessPermission(); |
| 8945 | if (status == OK) { |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 8946 | status = mFlinger->setDesiredDisplayModeSpecs(displayToken, specs); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8947 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8948 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8949 | } |
| 8950 | |
| 8951 | binder::Status SurfaceComposerAIDL::getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken, |
| 8952 | gui::DisplayModeSpecs* outSpecs) { |
| 8953 | if (!outSpecs) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8954 | return binderStatusFromStatusT(BAD_VALUE); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8955 | } |
| 8956 | |
| 8957 | status_t status = checkAccessPermission(); |
| 8958 | if (status != OK) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8959 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8960 | } |
| 8961 | |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 8962 | status = mFlinger->getDesiredDisplayModeSpecs(displayToken, outSpecs); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8963 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 8964 | } |
| 8965 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8966 | binder::Status SurfaceComposerAIDL::getDisplayBrightnessSupport(const sp<IBinder>& displayToken, |
| 8967 | bool* outSupport) { |
| 8968 | status_t status = mFlinger->getDisplayBrightnessSupport(displayToken, outSupport); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8969 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8970 | } |
| 8971 | |
| 8972 | binder::Status SurfaceComposerAIDL::setDisplayBrightness(const sp<IBinder>& displayToken, |
| 8973 | const gui::DisplayBrightness& brightness) { |
| 8974 | status_t status = checkControlDisplayBrightnessPermission(); |
| 8975 | if (status == OK) { |
| 8976 | status = mFlinger->setDisplayBrightness(displayToken, brightness); |
| 8977 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8978 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8979 | } |
| 8980 | |
| 8981 | binder::Status SurfaceComposerAIDL::addHdrLayerInfoListener( |
| 8982 | const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) { |
| 8983 | status_t status = checkControlDisplayBrightnessPermission(); |
| 8984 | if (status == OK) { |
| 8985 | status = mFlinger->addHdrLayerInfoListener(displayToken, listener); |
| 8986 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8987 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8988 | } |
| 8989 | |
| 8990 | binder::Status SurfaceComposerAIDL::removeHdrLayerInfoListener( |
| 8991 | const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) { |
| 8992 | status_t status = checkControlDisplayBrightnessPermission(); |
| 8993 | if (status == OK) { |
| 8994 | status = mFlinger->removeHdrLayerInfoListener(displayToken, listener); |
| 8995 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 8996 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 8997 | } |
| 8998 | |
| 8999 | binder::Status SurfaceComposerAIDL::notifyPowerBoost(int boostId) { |
| 9000 | status_t status = checkAccessPermission(); |
| 9001 | if (status == OK) { |
| 9002 | status = mFlinger->notifyPowerBoost(boostId); |
| 9003 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9004 | return binderStatusFromStatusT(status); |
| 9005 | } |
| 9006 | |
| 9007 | binder::Status SurfaceComposerAIDL::setGlobalShadowSettings(const gui::Color& ambientColor, |
| 9008 | const gui::Color& spotColor, |
| 9009 | float lightPosY, float lightPosZ, |
| 9010 | float lightRadius) { |
| 9011 | status_t status = checkAccessPermission(); |
| 9012 | if (status != OK) { |
| 9013 | return binderStatusFromStatusT(status); |
| 9014 | } |
| 9015 | |
| 9016 | half4 ambientColorHalf = {ambientColor.r, ambientColor.g, ambientColor.b, ambientColor.a}; |
| 9017 | half4 spotColorHalf = {spotColor.r, spotColor.g, spotColor.b, spotColor.a}; |
| 9018 | status = mFlinger->setGlobalShadowSettings(ambientColorHalf, spotColorHalf, lightPosY, |
| 9019 | lightPosZ, lightRadius); |
| 9020 | return binderStatusFromStatusT(status); |
| 9021 | } |
| 9022 | |
| 9023 | binder::Status SurfaceComposerAIDL::getDisplayDecorationSupport( |
| 9024 | const sp<IBinder>& displayToken, std::optional<gui::DisplayDecorationSupport>* outSupport) { |
| 9025 | std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport> support; |
| 9026 | status_t status = mFlinger->getDisplayDecorationSupport(displayToken, &support); |
| 9027 | if (status != NO_ERROR) { |
| 9028 | ALOGE("getDisplayDecorationSupport failed with error %d", status); |
| 9029 | return binderStatusFromStatusT(status); |
| 9030 | } |
| 9031 | |
| 9032 | if (!support || !support.has_value()) { |
| 9033 | outSupport->reset(); |
| 9034 | } else { |
| 9035 | outSupport->emplace(); |
| 9036 | outSupport->value().format = static_cast<int32_t>(support->format); |
| 9037 | outSupport->value().alphaInterpretation = |
| 9038 | static_cast<int32_t>(support->alphaInterpretation); |
| 9039 | } |
| 9040 | |
| 9041 | return binder::Status::ok(); |
| 9042 | } |
| 9043 | |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 9044 | binder::Status SurfaceComposerAIDL::setGameModeFrameRateOverride(int32_t uid, float frameRate) { |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9045 | status_t status; |
| 9046 | const int c_uid = IPCThreadState::self()->getCallingUid(); |
| 9047 | if (c_uid == AID_ROOT || c_uid == AID_SYSTEM) { |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 9048 | status = mFlinger->setGameModeFrameRateOverride(uid, frameRate); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9049 | } else { |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 9050 | ALOGE("setGameModeFrameRateOverride() permission denied for uid: %d", c_uid); |
| 9051 | status = PERMISSION_DENIED; |
| 9052 | } |
| 9053 | return binderStatusFromStatusT(status); |
| 9054 | } |
| 9055 | |
| 9056 | binder::Status SurfaceComposerAIDL::setGameDefaultFrameRateOverride(int32_t uid, float frameRate) { |
| 9057 | status_t status; |
| 9058 | const int c_uid = IPCThreadState::self()->getCallingUid(); |
| 9059 | if (c_uid == AID_ROOT || c_uid == AID_SYSTEM) { |
| 9060 | status = mFlinger->setGameDefaultFrameRateOverride(uid, frameRate); |
| 9061 | } else { |
| 9062 | ALOGE("setGameDefaultFrameRateOverride() permission denied for uid: %d", c_uid); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9063 | status = PERMISSION_DENIED; |
| 9064 | } |
| 9065 | return binderStatusFromStatusT(status); |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9066 | } |
| 9067 | |
Carlos Martinez Romero | 5ec086e | 2023-02-14 00:44:36 +0000 | [diff] [blame] | 9068 | binder::Status SurfaceComposerAIDL::enableRefreshRateOverlay(bool active) { |
| 9069 | mFlinger->sfdo_enableRefreshRateOverlay(active); |
| 9070 | return binder::Status::ok(); |
| 9071 | } |
| 9072 | |
| 9073 | binder::Status SurfaceComposerAIDL::setDebugFlash(int delay) { |
| 9074 | mFlinger->sfdo_setDebugFlash(delay); |
| 9075 | return binder::Status::ok(); |
| 9076 | } |
| 9077 | |
| 9078 | binder::Status SurfaceComposerAIDL::scheduleComposite() { |
| 9079 | mFlinger->sfdo_scheduleComposite(); |
| 9080 | return binder::Status::ok(); |
| 9081 | } |
| 9082 | |
| 9083 | binder::Status SurfaceComposerAIDL::scheduleCommit() { |
| 9084 | mFlinger->sfdo_scheduleCommit(); |
| 9085 | return binder::Status::ok(); |
| 9086 | } |
| 9087 | |
Carlos Martinez Romero | b4ea9e6 | 2023-10-25 21:48:56 +0000 | [diff] [blame] | 9088 | binder::Status SurfaceComposerAIDL::forceClientComposition(bool enabled) { |
| 9089 | mFlinger->sfdo_forceClientComposition(enabled); |
| 9090 | return binder::Status::ok(); |
| 9091 | } |
| 9092 | |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 9093 | binder::Status SurfaceComposerAIDL::updateSmallAreaDetection(const std::vector<int32_t>& appIds, |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 9094 | const std::vector<float>& thresholds) { |
| 9095 | status_t status; |
| 9096 | const int c_uid = IPCThreadState::self()->getCallingUid(); |
| 9097 | if (c_uid == AID_ROOT || c_uid == AID_SYSTEM) { |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 9098 | if (appIds.size() != thresholds.size()) return binderStatusFromStatusT(BAD_VALUE); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 9099 | |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 9100 | std::vector<std::pair<int32_t, float>> mappings; |
| 9101 | const size_t size = appIds.size(); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 9102 | mappings.reserve(size); |
| 9103 | for (int i = 0; i < size; i++) { |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 9104 | auto row = std::make_pair(appIds[i], thresholds[i]); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 9105 | mappings.push_back(row); |
| 9106 | } |
| 9107 | status = mFlinger->updateSmallAreaDetection(mappings); |
| 9108 | } else { |
| 9109 | ALOGE("updateSmallAreaDetection() permission denied for uid: %d", c_uid); |
| 9110 | status = PERMISSION_DENIED; |
| 9111 | } |
| 9112 | return binderStatusFromStatusT(status); |
| 9113 | } |
| 9114 | |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 9115 | binder::Status SurfaceComposerAIDL::setSmallAreaDetectionThreshold(int32_t appId, float threshold) { |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 9116 | status_t status; |
| 9117 | const int c_uid = IPCThreadState::self()->getCallingUid(); |
| 9118 | if (c_uid == AID_ROOT || c_uid == AID_SYSTEM) { |
Tony Huang | f362110 | 2023-09-04 17:14:22 +0800 | [diff] [blame] | 9119 | status = mFlinger->setSmallAreaDetectionThreshold(appId, threshold); |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 9120 | } else { |
| 9121 | ALOGE("setSmallAreaDetectionThreshold() permission denied for uid: %d", c_uid); |
| 9122 | status = PERMISSION_DENIED; |
| 9123 | } |
| 9124 | return binderStatusFromStatusT(status); |
| 9125 | } |
| 9126 | |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 9127 | binder::Status SurfaceComposerAIDL::getGpuContextPriority(int32_t* outPriority) { |
| 9128 | *outPriority = mFlinger->getGpuContextPriority(); |
| 9129 | return binder::Status::ok(); |
| 9130 | } |
| 9131 | |
| 9132 | binder::Status SurfaceComposerAIDL::getMaxAcquiredBufferCount(int32_t* buffers) { |
| 9133 | status_t status = mFlinger->getMaxAcquiredBufferCount(buffers); |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9134 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 9135 | } |
| 9136 | |
| 9137 | binder::Status SurfaceComposerAIDL::addWindowInfosListener( |
Patrick Williams | acd2258 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 9138 | const sp<gui::IWindowInfosListener>& windowInfosListener, |
| 9139 | gui::WindowInfosListenerInfo* outInfo) { |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 9140 | status_t status; |
Patrick Williams | 0525e9b | 2023-02-13 17:24:18 -0600 | [diff] [blame] | 9141 | const int pid = IPCThreadState::self()->getCallingPid(); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 9142 | const int uid = IPCThreadState::self()->getCallingUid(); |
Patrick Williams | 0525e9b | 2023-02-13 17:24:18 -0600 | [diff] [blame] | 9143 | // TODO(b/270566761) update permissions check so that only system_server and shell can add |
| 9144 | // WindowInfosListeners |
| 9145 | if (uid == AID_SYSTEM || uid == AID_GRAPHICS || |
| 9146 | checkPermission(sAccessSurfaceFlinger, pid, uid)) { |
Patrick Williams | acd2258 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 9147 | status = mFlinger->addWindowInfosListener(windowInfosListener, outInfo); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 9148 | } else { |
| 9149 | status = PERMISSION_DENIED; |
| 9150 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9151 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 9152 | } |
| 9153 | |
| 9154 | binder::Status SurfaceComposerAIDL::removeWindowInfosListener( |
| 9155 | const sp<gui::IWindowInfosListener>& windowInfosListener) { |
| 9156 | status_t status; |
Patrick Williams | 0525e9b | 2023-02-13 17:24:18 -0600 | [diff] [blame] | 9157 | const int pid = IPCThreadState::self()->getCallingPid(); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 9158 | const int uid = IPCThreadState::self()->getCallingUid(); |
Patrick Williams | 0525e9b | 2023-02-13 17:24:18 -0600 | [diff] [blame] | 9159 | if (uid == AID_SYSTEM || uid == AID_GRAPHICS || |
| 9160 | checkPermission(sAccessSurfaceFlinger, pid, uid)) { |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 9161 | status = mFlinger->removeWindowInfosListener(windowInfosListener); |
| 9162 | } else { |
| 9163 | status = PERMISSION_DENIED; |
| 9164 | } |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 9165 | return binderStatusFromStatusT(status); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 9166 | } |
| 9167 | |
Patrick Williams | 090ad06 | 2023-08-08 12:30:10 -0500 | [diff] [blame] | 9168 | binder::Status SurfaceComposerAIDL::getStalledTransactionInfo( |
| 9169 | int pid, std::optional<gui::StalledTransactionInfo>* outInfo) { |
| 9170 | const int callingPid = IPCThreadState::self()->getCallingPid(); |
| 9171 | const int callingUid = IPCThreadState::self()->getCallingUid(); |
| 9172 | if (!checkPermission(sAccessSurfaceFlinger, callingPid, callingUid)) { |
| 9173 | return binderStatusFromStatusT(PERMISSION_DENIED); |
| 9174 | } |
| 9175 | |
| 9176 | std::optional<TransactionHandler::StalledTransactionInfo> stalledTransactionInfo; |
| 9177 | status_t status = mFlinger->getStalledTransactionInfo(pid, stalledTransactionInfo); |
| 9178 | if (stalledTransactionInfo) { |
| 9179 | gui::StalledTransactionInfo result; |
| 9180 | result.layerName = String16{stalledTransactionInfo->layerName.c_str()}, |
| 9181 | result.bufferId = stalledTransactionInfo->bufferId, |
| 9182 | result.frameNumber = stalledTransactionInfo->frameNumber, |
| 9183 | outInfo->emplace(std::move(result)); |
| 9184 | } else { |
| 9185 | outInfo->reset(); |
| 9186 | } |
| 9187 | return binderStatusFromStatusT(status); |
| 9188 | } |
| 9189 | |
Ady Abraham | 07d03c4 | 2023-09-27 19:15:08 -0700 | [diff] [blame] | 9190 | binder::Status SurfaceComposerAIDL::getSchedulingPolicy(gui::SchedulingPolicy* outPolicy) { |
| 9191 | return gui::getSchedulingPolicy(outPolicy); |
| 9192 | } |
| 9193 | |
Wenhui Yang | eafc18a | 2024-05-14 17:15:52 +0000 | [diff] [blame] | 9194 | binder::Status SurfaceComposerAIDL::notifyShutdown() { |
| 9195 | TransactionTraceWriter::getInstance().invoke("systemShutdown_", /* overwrite= */ false); |
| 9196 | return ::android::binder::Status::ok(); |
| 9197 | } |
| 9198 | |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 9199 | binder::Status SurfaceComposerAIDL::addJankListener(const sp<IBinder>& layerHandle, |
| 9200 | const sp<gui::IJankListener>& listener) { |
| 9201 | sp<Layer> layer = LayerHandle::getLayer(layerHandle); |
| 9202 | if (layer == nullptr) { |
| 9203 | return binder::Status::fromExceptionCode(binder::Status::EX_NULL_POINTER); |
| 9204 | } |
| 9205 | JankTracker::addJankListener(layer->sequence, IInterface::asBinder(listener)); |
| 9206 | return binder::Status::ok(); |
| 9207 | } |
| 9208 | |
| 9209 | binder::Status SurfaceComposerAIDL::flushJankData(int32_t layerId) { |
| 9210 | JankTracker::flushJankData(layerId); |
| 9211 | return binder::Status::ok(); |
| 9212 | } |
| 9213 | |
| 9214 | binder::Status SurfaceComposerAIDL::removeJankListener(int32_t layerId, |
| 9215 | const sp<gui::IJankListener>& listener, |
| 9216 | int64_t afterVsync) { |
| 9217 | JankTracker::removeJankListener(layerId, IInterface::asBinder(listener), afterVsync); |
| 9218 | return binder::Status::ok(); |
| 9219 | } |
| 9220 | |
Huihong Luo | 07e7236 | 2022-02-14 14:26:04 -0800 | [diff] [blame] | 9221 | status_t SurfaceComposerAIDL::checkAccessPermission(bool usePermissionCache) { |
| 9222 | if (!mFlinger->callingThreadHasUnscopedSurfaceFlingerAccess(usePermissionCache)) { |
| 9223 | IPCThreadState* ipc = IPCThreadState::self(); |
| 9224 | ALOGE("Permission Denial: can't access SurfaceFlinger pid=%d, uid=%d", ipc->getCallingPid(), |
| 9225 | ipc->getCallingUid()); |
| 9226 | return PERMISSION_DENIED; |
| 9227 | } |
| 9228 | return OK; |
| 9229 | } |
| 9230 | |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9231 | status_t SurfaceComposerAIDL::checkControlDisplayBrightnessPermission() { |
| 9232 | IPCThreadState* ipc = IPCThreadState::self(); |
| 9233 | const int pid = ipc->getCallingPid(); |
| 9234 | const int uid = ipc->getCallingUid(); |
Rheygine Medel | 9e9281e | 2022-09-22 10:13:56 -0700 | [diff] [blame] | 9235 | if ((uid != AID_GRAPHICS) && (uid != AID_SYSTEM) && |
Huihong Luo | 37396db | 2022-02-15 10:43:00 -0800 | [diff] [blame] | 9236 | !PermissionCache::checkPermission(sControlDisplayBrightness, pid, uid)) { |
| 9237 | ALOGE("Permission Denial: can't control brightness pid=%d, uid=%d", pid, uid); |
| 9238 | return PERMISSION_DENIED; |
| 9239 | } |
| 9240 | return OK; |
| 9241 | } |
| 9242 | |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 9243 | status_t SurfaceComposerAIDL::checkReadFrameBufferPermission() { |
| 9244 | IPCThreadState* ipc = IPCThreadState::self(); |
| 9245 | const int pid = ipc->getCallingPid(); |
| 9246 | const int uid = ipc->getCallingUid(); |
| 9247 | if ((uid != AID_GRAPHICS) && !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) { |
| 9248 | ALOGE("Permission Denial: can't read framebuffer pid=%d, uid=%d", pid, uid); |
| 9249 | return PERMISSION_DENIED; |
| 9250 | } |
| 9251 | return OK; |
| 9252 | } |
| 9253 | |
Chavi Weingarten | 076acac | 2023-01-19 17:20:43 +0000 | [diff] [blame] | 9254 | void SurfaceFlinger::forceFutureUpdate(int delayInMs) { |
| 9255 | static_cast<void>(mScheduler->scheduleDelayed([&]() { scheduleRepaint(); }, ms2ns(delayInMs))); |
| 9256 | } |
| 9257 | |
Vishnu Nair | 2f65e97 | 2023-04-04 16:36:28 +0000 | [diff] [blame] | 9258 | const DisplayDevice* SurfaceFlinger::getDisplayFromLayerStack(ui::LayerStack layerStack) { |
| 9259 | for (const auto& [_, display] : mDisplays) { |
| 9260 | if (display->getLayerStack() == layerStack) { |
| 9261 | return display.get(); |
| 9262 | } |
| 9263 | } |
| 9264 | return nullptr; |
| 9265 | } |
| 9266 | |
Dominik Laskowski | 9dab343 | 2019-03-27 13:21:10 -0700 | [diff] [blame] | 9267 | } // namespace android |
Lloyd Pique | 074e812 | 2018-07-26 12:57:23 -0700 | [diff] [blame] | 9268 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 9269 | #if defined(__gl_h_) |
| 9270 | #error "don't include gl/gl.h in this file" |
| 9271 | #endif |
| 9272 | |
| 9273 | #if defined(__gl2_h_) |
| 9274 | #error "don't include gl2/gl2.h in this file" |
Chia-I Wu | 767fcf7 | 2017-11-30 22:07:38 -0800 | [diff] [blame] | 9275 | #endif |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 9276 | |
| 9277 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 9278 | #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" |