blob: b068dafb193c99cbe8a0c21c00685482a7bd4c0b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
Dominik Laskowski718f9602019-11-09 20:01:35 -080017#pragma once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Peiyong Linefefaac2018-08-17 12:27:51 -070019#include <memory>
Dominik Laskowski075d3172018-05-24 15:50:06 -070020#include <optional>
Peiyong Linefefaac2018-08-17 12:27:51 -070021#include <string>
Peiyong Lin136fbbc2018-04-17 15:09:44 -070022#include <unordered_map>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Alec Mourif6fd29e2018-11-17 04:56:41 +000024#include <android/native_window.h>
Dan Stoza9e56aa02015-11-02 13:00:03 -080025#include <binder/IBinder.h>
Chia-I Wuf2aa3112018-08-27 14:54:37 -070026#include <gui/LayerState.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070027#include <math/mat4.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080028#include <renderengine/RenderEngine.h>
Alec Mourif6fd29e2018-11-17 04:56:41 +000029#include <system/window.h>
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020030#include <ui/DisplayId.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080031#include <ui/DisplayState.h>
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -070032#include <ui/GraphicTypes.h>
Peiyong Linfb069302018-04-25 14:34:31 -070033#include <ui/HdrCapabilities.h>
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -070034#include <ui/Region.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010035#include <ui/StaticDisplayInfo.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070036#include <ui/Transform.h>
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010037#include <utils/Errors.h>
Alec Mouri8d4f90a2018-11-14 22:33:24 +000038#include <utils/Mutex.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080039#include <utils/RefBase.h>
Mathias Agopian86303202012-07-24 22:46:10 -070040#include <utils/Timers.h>
41
Dominik Laskowski075d3172018-05-24 15:50:06 -070042#include "DisplayHardware/DisplayIdentification.h"
Marin Shalamanov23c44202020-12-22 19:09:20 +010043#include "DisplayHardware/DisplayMode.h"
Peiyong Line9d809e2020-04-14 13:10:48 -070044#include "DisplayHardware/Hal.h"
Lloyd Pique688abd42019-02-15 15:42:24 -080045#include "DisplayHardware/PowerAdvisor.h"
Mathias Agopianf4358632012-08-22 17:16:19 -070046
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047namespace android {
48
Dan Stoza9e56aa02015-11-02 13:00:03 -080049class Fence;
Lloyd Pique2eef1d22018-09-18 21:30:04 -070050class HWComposer;
Jesse Hall9e663de2013-08-16 14:28:37 -070051class IGraphicBufferProducer;
Mathias Agopian13127d82013-03-05 17:47:11 -080052class Layer;
Mathias Agopian86303202012-07-24 22:46:10 -070053class SurfaceFlinger;
Lloyd Pique2eef1d22018-09-18 21:30:04 -070054
David Sodmanfb95bcc2017-12-22 15:45:30 -080055struct CompositionInfo;
Lloyd Pique2eef1d22018-09-18 21:30:04 -070056struct DisplayDeviceCreationArgs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Lloyd Pique45a165a2018-10-19 11:54:47 -070058namespace compositionengine {
59class Display;
Lloyd Pique542307f2018-10-19 13:24:08 -070060class DisplaySurface;
Lloyd Pique45a165a2018-10-19 11:54:47 -070061} // namespace compositionengine
62
Marin Shalamanov1c434292020-06-12 01:47:29 +020063class DisplayDevice : public RefBase {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064public:
Peiyong Linfb069302018-04-25 14:34:31 -070065 constexpr static float sDefaultMinLumiance = 0.0;
66 constexpr static float sDefaultMaxLumiance = 500.0;
Evan Rosky2239b372021-05-20 13:43:47 -070067 enum { eReceivesInput = 0x01 };
Peiyong Linfb069302018-04-25 14:34:31 -070068
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070069 explicit DisplayDevice(DisplayDeviceCreationArgs& args);
Marin Shalamanov1c434292020-06-12 01:47:29 +020070
71 // Must be destroyed on the main thread because it may call into HWComposer.
Lloyd Pique45a165a2018-10-19 11:54:47 -070072 virtual ~DisplayDevice();
73
74 std::shared_ptr<compositionengine::Display> getCompositionDisplay() const {
75 return mCompositionDisplay;
76 }
Mathias Agopian92a979a2012-08-02 18:32:23 -070077
Marin Shalamanov228f46b2021-01-28 21:11:45 +010078 std::optional<ui::DisplayConnectionType> getConnectionType() const { return mConnectionType; }
Dominik Laskowski55c85402020-01-21 16:25:47 -080079
80 bool isVirtual() const { return !mConnectionType; }
Dominik Laskowski075d3172018-05-24 15:50:06 -070081 bool isPrimary() const { return mIsPrimary; }
Mathias Agopian92a979a2012-08-02 18:32:23 -070082
Jamie Gennisdd3cb842012-10-19 18:19:11 -070083 // isSecure indicates whether this display can be trusted to display
84 // secure surfaces.
Lloyd Pique45a165a2018-10-19 11:54:47 -070085 bool isSecure() const;
Jamie Gennisdd3cb842012-10-19 18:19:11 -070086
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080087 int getWidth() const;
88 int getHeight() const;
89 ui::Size getSize() const { return {getWidth(), getHeight()}; }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070090
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080091 void setLayerStack(ui::LayerStack);
92 void setDisplaySize(int width, int height);
Dominik Laskowski718f9602019-11-09 20:01:35 -080093 void setProjection(ui::Rotation orientation, Rect viewport, Rect frame);
Evan Rosky2239b372021-05-20 13:43:47 -070094 void setFlags(uint32_t flags);
Dominik Laskowski718f9602019-11-09 20:01:35 -080095
96 ui::Rotation getPhysicalOrientation() const { return mPhysicalOrientation; }
97 ui::Rotation getOrientation() const { return mOrientation; }
98
99 static ui::Transform::RotationFlags getPrimaryDisplayRotationFlags();
100
Garfield Tan54edd912020-10-21 16:31:41 -0700101 ui::Transform::RotationFlags getTransformHint() const;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102 const ui::Transform& getTransform() const;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200103 const Rect& getLayerStackSpaceRect() const;
104 const Rect& getOrientedDisplaySpaceRect() const;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105 bool needsFiltering() const;
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800106 ui::LayerStack getLayerStack() const;
Evan Rosky2239b372021-05-20 13:43:47 -0700107 bool receivesInput() const { return mFlags & eReceivesInput; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700108
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800109 DisplayId getId() const;
110
111 // Shorthand to upcast the ID of a display whose type is known as a precondition.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200112 PhysicalDisplayId getPhysicalId() const {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800113 const auto id = PhysicalDisplayId::tryCast(getId());
114 LOG_FATAL_IF(!id);
115 return *id;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200116 }
117
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800118 VirtualDisplayId getVirtualId() const {
119 const auto id = VirtualDisplayId::tryCast(getId());
120 LOG_FATAL_IF(!id);
121 return *id;
122 }
123
Dominik Laskowski075d3172018-05-24 15:50:06 -0700124 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
Dominik Laskowskie9774092018-12-11 10:04:24 -0800125 int32_t getSequenceId() const { return mSequenceId; }
Mathias Agopian87baae12012-07-31 12:38:26 -0700126
Lloyd Pique32cbe282018-10-19 13:09:22 -0700127 const Region& getUndefinedRegion() const;
128
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700129 int32_t getSupportedPerFrameMetadata() const;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700130
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700131 bool hasWideColorGamut() const;
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700132 // Whether h/w composer has native support for specific HDR type.
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700133 bool hasHDR10PlusSupport() const;
134 bool hasHDR10Support() const;
135 bool hasHLGSupport() const;
136 bool hasDolbyVisionSupport() const;
Chia-I Wube02ec02018-05-18 10:59:36 -0700137
Kriti Dang4bc7cdf2021-01-08 11:49:56 +0100138 void overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes);
139
Peiyong Linfb069302018-04-25 14:34:31 -0700140 // The returned HdrCapabilities is the combination of HDR capabilities from
141 // hardware composer and RenderEngine. When the DisplayDevice supports wide
142 // color gamut, RenderEngine is able to simulate HDR support in Display P3
143 // color space for both PQ and HLG HDR contents. The minimum and maximum
144 // luminance will be set to sDefaultMinLumiance and sDefaultMaxLumiance
145 // respectively if hardware composer doesn't return meaningful values.
Kriti Dang4bc7cdf2021-01-08 11:49:56 +0100146 HdrCapabilities getHdrCapabilities() const;
Jesse Hall38efe862013-04-06 23:12:29 -0700147
Chia-I Wube02ec02018-05-18 10:59:36 -0700148 // Return true if intent is supported by the display.
149 bool hasRenderIntent(ui::RenderIntent intent) const;
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700150
Lloyd Pique32cbe282018-10-19 13:09:22 -0700151 const Rect& getBounds() const;
152 const Rect& bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700154 void setDisplayName(const std::string& displayName);
155 const std::string& getDisplayName() const { return mDisplayName; }
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700156
Marin Shalamanove5cceea2020-04-30 18:13:10 +0200157 void setDeviceProductInfo(std::optional<DeviceProductInfo> info);
158 const std::optional<DeviceProductInfo>& getDeviceProductInfo() const {
159 return mDeviceProductInfo;
160 }
161
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700162 /* ------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700163 * Display power mode management.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700164 */
Peiyong Lin65248e02020-04-18 21:15:07 -0700165 hardware::graphics::composer::hal::PowerMode getPowerMode() const;
166 void setPowerMode(hardware::graphics::composer::hal::PowerMode mode);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700167 bool isPoweredOn() const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700168
Alec Mouridd8bf2d2021-05-08 16:36:33 -0700169 // Enables layer caching on this DisplayDevice
170 void enableLayerCaching(bool enable);
171
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800172 ui::Dataspace getCompositionDataSpace() const;
Michael Wright28f24d02016-07-12 13:30:53 -0700173
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700174 /* ------------------------------------------------------------------------
Marin Shalamanov5801c942020-12-17 17:00:13 +0100175 * Display mode management.
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700176 */
Marin Shalamanov5801c942020-12-17 17:00:13 +0100177 const DisplayModePtr& getActiveMode() const;
178 void setActiveMode(DisplayModeId);
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100179 status_t initiateModeChange(DisplayModeId modeId,
180 const hal::VsyncPeriodChangeConstraints& constraints,
181 hal::VsyncPeriodChangeTimeline* outTimeline) const;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100182
183 // Return the immutable list of supported display modes. The HWC may report different modes
184 // after a hotplug reconnect event, in which case the DisplayDevice object will be recreated.
185 // Hotplug reconnects are common for external displays.
186 const DisplayModes& getSupportedModes() const;
187
188 // Returns nullptr if the given mode ID is not supported. A previously
189 // supported mode may be no longer supported for some devices like TVs and
190 // set-top boxes after a hotplug reconnect.
191 DisplayModePtr getMode(DisplayModeId) const;
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700192
Marin Shalamanov045b7002021-01-07 16:56:24 +0100193 void onVsync(nsecs_t timestamp);
194 nsecs_t getVsyncPeriodFromHWC() const;
195 nsecs_t getRefreshTimestamp() const;
196
Jesse Hall02d86562013-03-25 14:43:23 -0700197 // release HWC resources (if any) for removable displays
Lloyd Pique45a165a2018-10-19 11:54:47 -0700198 void disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700199
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700200 /* ------------------------------------------------------------------------
201 * Debugging
202 */
203 uint32_t getPageFlipCount() const;
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700204 std::string getDebugName() const;
Yiwei Zhang5434a782018-12-05 18:06:32 -0800205 void dump(std::string& result) const;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700206
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207private:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700208 const sp<SurfaceFlinger> mFlinger;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100209 HWComposer& mHwComposer;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700210 const wp<IBinder> mDisplayToken;
Dominik Laskowskie9774092018-12-11 10:04:24 -0800211 const int32_t mSequenceId;
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100212 const std::optional<ui::DisplayConnectionType> mConnectionType;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700213
Lloyd Pique45a165a2018-10-19 11:54:47 -0700214 const std::shared_ptr<compositionengine::Display> mCompositionDisplay;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700215
Lloyd Pique31cb2942018-10-19 17:23:03 -0700216 std::string mDisplayName;
Mathias Agopian03e40722012-04-26 16:11:59 -0700217
Dominik Laskowski718f9602019-11-09 20:01:35 -0800218 const ui::Rotation mPhysicalOrientation;
219 ui::Rotation mOrientation = ui::ROTATION_0;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700220
Dominik Laskowski718f9602019-11-09 20:01:35 -0800221 static ui::Transform::RotationFlags sPrimaryDisplayRotationFlags;
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700222
Peiyong Lin65248e02020-04-18 21:15:07 -0700223 hardware::graphics::composer::hal::PowerMode mPowerMode =
224 hardware::graphics::composer::hal::PowerMode::OFF;
Marin Shalamanovf22e6ac2021-02-10 20:45:15 +0100225 DisplayModePtr mActiveMode;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100226 const DisplayModes mSupportedModes;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600227
Marin Shalamanov045b7002021-01-07 16:56:24 +0100228 std::atomic<nsecs_t> mLastHwVsync = 0;
229
Dominik Laskowski075d3172018-05-24 15:50:06 -0700230 // TODO(b/74619554): Remove special cases for primary display.
231 const bool mIsPrimary;
Marin Shalamanove5cceea2020-04-30 18:13:10 +0200232
Evan Rosky2239b372021-05-20 13:43:47 -0700233 uint32_t mFlags = 0;
234
Marin Shalamanove5cceea2020-04-30 18:13:10 +0200235 std::optional<DeviceProductInfo> mDeviceProductInfo;
Kriti Dang4bc7cdf2021-01-08 11:49:56 +0100236
237 std::vector<ui::Hdr> mOverrideHdrTypes;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238};
239
Irvelffc9efc2016-07-27 15:16:37 -0700240struct DisplayDeviceState {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800241 struct Physical {
Marin Shalamanova524a092020-07-27 21:39:55 +0200242 PhysicalDisplayId id;
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100243 ui::DisplayConnectionType type;
Peiyong Lin65248e02020-04-18 21:15:07 -0700244 hardware::graphics::composer::hal::HWDisplayId hwcDisplayId;
Marin Shalamanov7ce87642020-05-06 13:45:58 +0200245 std::optional<DeviceProductInfo> deviceProductInfo;
Marin Shalamanova903d032020-12-29 20:35:13 +0100246 DisplayModes supportedModes;
247 DisplayModePtr activeMode;
248
Dominik Laskowski55c85402020-01-21 16:25:47 -0800249 bool operator==(const Physical& other) const {
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100250 return id == other.id && type == other.type && hwcDisplayId == other.hwcDisplayId;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800251 }
252 };
253
254 bool isVirtual() const { return !physical; }
Irvelffc9efc2016-07-27 15:16:37 -0700255
Dominik Laskowski663bd282018-04-19 15:26:54 -0700256 int32_t sequenceId = sNextSequenceId++;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800257 std::optional<Physical> physical;
Irvelffc9efc2016-07-27 15:16:37 -0700258 sp<IGraphicBufferProducer> surface;
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800259 ui::LayerStack layerStack = ui::NO_LAYER_STACK;
Evan Rosky2239b372021-05-20 13:43:47 -0700260 uint32_t flags = 0;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200261 Rect layerStackSpaceRect;
262 Rect orientedDisplaySpaceRect;
Dominik Laskowski718f9602019-11-09 20:01:35 -0800263 ui::Rotation orientation = ui::ROTATION_0;
Irvelffc9efc2016-07-27 15:16:37 -0700264 uint32_t width = 0;
265 uint32_t height = 0;
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700266 std::string displayName;
Irvelffc9efc2016-07-27 15:16:37 -0700267 bool isSecure = false;
Dominik Laskowski663bd282018-04-19 15:26:54 -0700268
269private:
270 static std::atomic<int32_t> sNextSequenceId;
Irvelffc9efc2016-07-27 15:16:37 -0700271};
272
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700273struct DisplayDeviceCreationArgs {
274 // We use a constructor to ensure some of the values are set, without
275 // assuming a default value.
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100276 DisplayDeviceCreationArgs(const sp<SurfaceFlinger>&, HWComposer& hwComposer,
277 const wp<IBinder>& displayToken,
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700278 std::shared_ptr<compositionengine::Display>);
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700279 const sp<SurfaceFlinger> flinger;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100280 HWComposer& hwComposer;
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700281 const wp<IBinder> displayToken;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700282 const std::shared_ptr<compositionengine::Display> compositionDisplay;
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700283
Dominik Laskowskie9774092018-12-11 10:04:24 -0800284 int32_t sequenceId{0};
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100285 std::optional<ui::DisplayConnectionType> connectionType;
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700286 bool isSecure{false};
287 sp<ANativeWindow> nativeWindow;
Lloyd Pique542307f2018-10-19 13:24:08 -0700288 sp<compositionengine::DisplaySurface> displaySurface;
Dominik Laskowski718f9602019-11-09 20:01:35 -0800289 ui::Rotation physicalOrientation{ui::ROTATION_0};
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700290 bool hasWideColorGamut{false};
291 HdrCapabilities hdrCapabilities;
292 int32_t supportedPerFrameMetadata{0};
293 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hwcColorModes;
Peiyong Lin65248e02020-04-18 21:15:07 -0700294 hardware::graphics::composer::hal::PowerMode initialPowerMode{
295 hardware::graphics::composer::hal::PowerMode::ON};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700296 bool isPrimary{false};
Marin Shalamanov5801c942020-12-17 17:00:13 +0100297 DisplayModes supportedModes;
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700298};
299
Dominik Laskowski718f9602019-11-09 20:01:35 -0800300} // namespace android