blob: 08e5db92d36f362edb7270289fd3662d50d51610 [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
Dan Stoza9e56aa02015-11-02 13:00:03 -080017// #define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "DisplayDevice"
20
Peiyong Lincbc184f2018-08-22 13:24:10 -070021#include "DisplayDevice.h"
22
Chia-I Wube02ec02018-05-18 10:59:36 -070023#include <array>
24#include <unordered_set>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <math.h>
30
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -070031#include <android-base/stringprintf.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070032#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070033#include <compositionengine/CompositionEngine.h>
34#include <compositionengine/Display.h>
35#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070036#include <compositionengine/DisplaySurface.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070037#include <compositionengine/RenderSurface.h>
38#include <compositionengine/RenderSurfaceCreationArgs.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070039#include <compositionengine/impl/OutputCompositionState.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070040#include <configstore/Utils.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <cutils/properties.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070042#include <gui/Surface.h>
43#include <hardware/gralloc.h>
44#include <renderengine/RenderEngine.h>
Alec Mouri0a9c7b82018-11-16 13:05:25 -080045#include <sync/sync.h>
46#include <system/window.h>
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -060047#include <ui/DebugUtils.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070048#include <ui/DisplayInfo.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070049#include <ui/PixelFormat.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070050#include <utils/Log.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070051#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
Mathias Agopian1b031492012-06-20 17:51:20 -070053#include "DisplayHardware/HWComposer.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080054#include "DisplayHardware/HWC2.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070055#include "SurfaceFlinger.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080056#include "Layer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070057
Peiyong Linfd997e02018-03-28 15:29:00 -070058namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Jaesoo Lee720a7242017-01-31 15:26:18 +090060// retrieve triple buffer setting from configstore
61using namespace android::hardware::configstore;
62using namespace android::hardware::configstore::V1_0;
Yiwei Zhang5434a782018-12-05 18:06:32 -080063using android::base::StringAppendF;
Peiyong Linfd997e02018-03-28 15:29:00 -070064using android::ui::ColorMode;
Chia-I Wube02ec02018-05-18 10:59:36 -070065using android::ui::Dataspace;
Peiyong Lin62665892018-04-16 11:07:44 -070066using android::ui::Hdr;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -080067using android::ui::RenderIntent;
Jaesoo Lee720a7242017-01-31 15:26:18 +090068
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069/*
70 * Initialize the display to the specified values.
71 *
72 */
73
Pablo Ceballos021623b2016-04-15 17:31:51 -070074uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
75
Chia-I Wube02ec02018-05-18 10:59:36 -070076namespace {
77
78// ordered list of known SDR color modes
Valerie Hau9758ae02018-10-09 16:05:09 -070079const std::array<ColorMode, 3> sSdrColorModes = {
80 ColorMode::DISPLAY_BT2020,
Chia-I Wube02ec02018-05-18 10:59:36 -070081 ColorMode::DISPLAY_P3,
82 ColorMode::SRGB,
83};
84
85// ordered list of known HDR color modes
86const std::array<ColorMode, 2> sHdrColorModes = {
87 ColorMode::BT2100_PQ,
88 ColorMode::BT2100_HLG,
89};
90
91// ordered list of known SDR render intents
92const std::array<RenderIntent, 2> sSdrRenderIntents = {
93 RenderIntent::ENHANCE,
94 RenderIntent::COLORIMETRIC,
95};
96
97// ordered list of known HDR render intents
98const std::array<RenderIntent, 2> sHdrRenderIntents = {
99 RenderIntent::TONE_MAP_ENHANCE,
100 RenderIntent::TONE_MAP_COLORIMETRIC,
101};
102
103// map known color mode to dataspace
104Dataspace colorModeToDataspace(ColorMode mode) {
105 switch (mode) {
106 case ColorMode::SRGB:
Peiyong Lin14724e62018-12-05 07:27:30 -0800107 return Dataspace::V0_SRGB;
Chia-I Wube02ec02018-05-18 10:59:36 -0700108 case ColorMode::DISPLAY_P3:
109 return Dataspace::DISPLAY_P3;
Valerie Hau9758ae02018-10-09 16:05:09 -0700110 case ColorMode::DISPLAY_BT2020:
111 return Dataspace::DISPLAY_BT2020;
Chia-I Wube02ec02018-05-18 10:59:36 -0700112 case ColorMode::BT2100_HLG:
113 return Dataspace::BT2020_HLG;
114 case ColorMode::BT2100_PQ:
115 return Dataspace::BT2020_PQ;
116 default:
117 return Dataspace::UNKNOWN;
118 }
119}
120
121// Return a list of candidate color modes.
122std::vector<ColorMode> getColorModeCandidates(ColorMode mode) {
123 std::vector<ColorMode> candidates;
124
125 // add mode itself
126 candidates.push_back(mode);
127
128 // check if mode is HDR
129 bool isHdr = false;
130 for (auto hdrMode : sHdrColorModes) {
131 if (hdrMode == mode) {
132 isHdr = true;
133 break;
134 }
135 }
136
137 // add other HDR candidates when mode is HDR
138 if (isHdr) {
139 for (auto hdrMode : sHdrColorModes) {
140 if (hdrMode != mode) {
141 candidates.push_back(hdrMode);
142 }
143 }
144 }
145
146 // add other SDR candidates
147 for (auto sdrMode : sSdrColorModes) {
148 if (sdrMode != mode) {
149 candidates.push_back(sdrMode);
150 }
151 }
152
153 return candidates;
154}
155
156// Return a list of candidate render intents.
157std::vector<RenderIntent> getRenderIntentCandidates(RenderIntent intent) {
158 std::vector<RenderIntent> candidates;
159
160 // add intent itself
161 candidates.push_back(intent);
162
163 // check if intent is HDR
164 bool isHdr = false;
165 for (auto hdrIntent : sHdrRenderIntents) {
166 if (hdrIntent == intent) {
167 isHdr = true;
168 break;
169 }
170 }
171
Chia-I Wube02ec02018-05-18 10:59:36 -0700172 if (isHdr) {
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700173 // add other HDR candidates when intent is HDR
Chia-I Wube02ec02018-05-18 10:59:36 -0700174 for (auto hdrIntent : sHdrRenderIntents) {
175 if (hdrIntent != intent) {
176 candidates.push_back(hdrIntent);
177 }
178 }
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700179 } else {
180 // add other SDR candidates when intent is SDR
181 for (auto sdrIntent : sSdrRenderIntents) {
182 if (sdrIntent != intent) {
183 candidates.push_back(sdrIntent);
184 }
185 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700186 }
187
188 return candidates;
189}
190
191// Return the best color mode supported by HWC.
192ColorMode getHwcColorMode(
193 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
194 ColorMode mode) {
195 std::vector<ColorMode> candidates = getColorModeCandidates(mode);
196 for (auto candidate : candidates) {
197 auto iter = hwcColorModes.find(candidate);
198 if (iter != hwcColorModes.end()) {
199 return candidate;
200 }
201 }
202
203 return ColorMode::NATIVE;
204}
205
206// Return the best render intent supported by HWC.
207RenderIntent getHwcRenderIntent(const std::vector<RenderIntent>& hwcIntents, RenderIntent intent) {
208 std::vector<RenderIntent> candidates = getRenderIntentCandidates(intent);
209 for (auto candidate : candidates) {
210 for (auto hwcIntent : hwcIntents) {
211 if (candidate == hwcIntent) {
212 return candidate;
213 }
214 }
215 }
216
217 return RenderIntent::COLORIMETRIC;
218}
219
220} // anonymous namespace
221
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700222DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger,
223 const wp<IBinder>& displayToken,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700224 const std::optional<DisplayId>& displayId)
225 : flinger(flinger), displayToken(displayToken), displayId(displayId) {}
Chia-I Wube02ec02018-05-18 10:59:36 -0700226
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700227DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs&& args)
Lloyd Pique32cbe282018-10-19 13:09:22 -0700228 : mFlinger(args.flinger),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700229 mDisplayToken(args.displayToken),
Dominik Laskowskie9774092018-12-11 10:04:24 -0800230 mSequenceId(args.sequenceId),
Lloyd Pique45a165a2018-10-19 11:54:47 -0700231 mDisplayInstallOrientation(args.displayInstallOrientation),
232 mCompositionDisplay{mFlinger->getCompositionEngine().createDisplay(
233 compositionengine::DisplayCreationArgs{args.isSecure, args.isVirtual,
234 args.displayId})},
Dominik Laskowski075d3172018-05-24 15:50:06 -0700235 mIsVirtual(args.isVirtual),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700236 mOrientation(),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700237 mActiveConfig(0),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700238 mHasWideColorGamut(args.hasWideColorGamut),
Valerie Haue9e843a2018-12-18 13:39:23 -0800239 mHasHdr10Plus(false),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700240 mHasHdr10(false),
241 mHasHLG(false),
242 mHasDolbyVision(false),
Dominik Laskowski075d3172018-05-24 15:50:06 -0700243 mSupportedPerFrameMetadata(args.supportedPerFrameMetadata),
244 mIsPrimary(args.isPrimary) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700245 mCompositionDisplay->createRenderSurface(
246 compositionengine::RenderSurfaceCreationArgs{ANativeWindow_getWidth(
247 args.nativeWindow.get()),
248 ANativeWindow_getHeight(
249 args.nativeWindow.get()),
250 args.nativeWindow, args.displaySurface});
251
Lloyd Pique32cbe282018-10-19 13:09:22 -0700252 if (!mCompositionDisplay->isValid()) {
253 ALOGE("Composition Display did not validate!");
254 }
255
Lloyd Pique31cb2942018-10-19 17:23:03 -0700256 mCompositionDisplay->getRenderSurface()->initialize();
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700257
Lloyd Pique31cb2942018-10-19 17:23:03 -0700258 populateColorModes(args.hwcColorModes);
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700259
260 std::vector<Hdr> types = args.hdrCapabilities.getSupportedHdrTypes();
Peiyong Linfb069302018-04-25 14:34:31 -0700261 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700262 switch (hdrType) {
Valerie Haue9e843a2018-12-18 13:39:23 -0800263 case Hdr::HDR10_PLUS:
264 mHasHdr10Plus = true;
265 break;
Peiyong Lin62665892018-04-16 11:07:44 -0700266 case Hdr::HDR10:
267 mHasHdr10 = true;
268 break;
269 case Hdr::HLG:
270 mHasHLG = true;
271 break;
272 case Hdr::DOLBY_VISION:
273 mHasDolbyVision = true;
274 break;
275 default:
276 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
277 }
278 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700279
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700280 float minLuminance = args.hdrCapabilities.getDesiredMinLuminance();
281 float maxLuminance = args.hdrCapabilities.getDesiredMaxLuminance();
282 float maxAverageLuminance = args.hdrCapabilities.getDesiredMaxAverageLuminance();
Peiyong Linfb069302018-04-25 14:34:31 -0700283
284 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
285 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
286 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
287 if (this->hasWideColorGamut()) {
288 // insert HDR10/HLG as we will force client composition for HDR10/HLG
289 // layers
290 if (!hasHDR10Support()) {
Valerie Haue9e843a2018-12-18 13:39:23 -0800291 types.push_back(Hdr::HDR10);
Peiyong Linfb069302018-04-25 14:34:31 -0700292 }
293
294 if (!hasHLGSupport()) {
Valerie Haue9e843a2018-12-18 13:39:23 -0800295 types.push_back(Hdr::HLG);
Peiyong Linfb069302018-04-25 14:34:31 -0700296 }
297 }
298 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
299
Lloyd Pique32cbe282018-10-19 13:09:22 -0700300 setPowerMode(args.initialPowerMode);
Alec Mouriba013fa2018-10-16 12:43:11 -0700301
Jesse Hallffe1f192013-03-22 15:13:48 -0700302 // initialize the display orientation transform.
Lloyd Pique32cbe282018-10-19 13:09:22 -0700303 setProjection(DisplayState::eOrientationDefault, Rect::INVALID_RECT, Rect::INVALID_RECT);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304}
305
Lloyd Pique09594832018-01-22 17:48:03 -0800306DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700307
Lloyd Pique45a165a2018-10-19 11:54:47 -0700308void DisplayDevice::disconnect() {
309 mCompositionDisplay->disconnect();
Jesse Hall02d86562013-03-25 14:43:23 -0700310}
311
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700312int DisplayDevice::getWidth() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700313 return mCompositionDisplay->getState().bounds.getWidth();
Mathias Agopiana4912602012-07-12 14:25:33 -0700314}
315
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700316int DisplayDevice::getHeight() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700317 return mCompositionDisplay->getState().bounds.getHeight();
Mathias Agopiana4912602012-07-12 14:25:33 -0700318}
319
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700320void DisplayDevice::setDisplayName(const std::string& displayName) {
321 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700322 // never override the name with an empty name
323 mDisplayName = displayName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700324 mCompositionDisplay->setName(displayName);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700325 }
326}
327
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700328uint32_t DisplayDevice::getPageFlipCount() const {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700329 return mCompositionDisplay->getRenderSurface()->getPageFlipCount();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800330}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800331
Mathias Agopian1b031492012-06-20 17:51:20 -0700332// ----------------------------------------------------------------------------
333
Mathias Agopian13127d82013-03-05 17:47:11 -0800334void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700335 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700336}
337
Mathias Agopian13127d82013-03-05 17:47:11 -0800338const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700339 return mVisibleLayersSortedByZ;
340}
341
Chia-I Wu83806892017-11-16 10:50:20 -0800342void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
343 mLayersNeedingFences = layers;
344}
345
346const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
347 return mLayersNeedingFences;
348}
349
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700350// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700351void DisplayDevice::setPowerMode(int mode) {
352 mPowerMode = mode;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700353 getCompositionDisplay()->setCompositionEnabled(mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700354}
355
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700356int DisplayDevice::getPowerMode() const {
357 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700358}
359
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700360bool DisplayDevice::isPoweredOn() const {
361 return mPowerMode != HWC_POWER_MODE_OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700362}
363
364// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700365void DisplayDevice::setActiveConfig(int mode) {
366 mActiveConfig = mode;
367}
368
369int DisplayDevice::getActiveConfig() const {
370 return mActiveConfig;
371}
372
373// ----------------------------------------------------------------------------
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800374
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800375ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700376 return mCompositionDisplay->getState().dataspace;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800377}
378
Michael Wright28f24d02016-07-12 13:30:53 -0700379// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700380
Mathias Agopian28947d72012-08-08 18:51:15 -0700381void DisplayDevice::setLayerStack(uint32_t stack) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700382 mCompositionDisplay->setLayerStackFilter(!isPrimary(), stack);
Mathias Agopian28947d72012-08-08 18:51:15 -0700383}
384
385// ----------------------------------------------------------------------------
386
Lloyd Pique32cbe282018-10-19 13:09:22 -0700387uint32_t DisplayDevice::displayStateOrientationToTransformOrientation(int orientation) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700388 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700389 case DisplayState::eOrientationDefault:
Lloyd Pique32cbe282018-10-19 13:09:22 -0700390 return ui::Transform::ROT_0;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700391 case DisplayState::eOrientation90:
Lloyd Pique32cbe282018-10-19 13:09:22 -0700392 return ui::Transform::ROT_90;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700393 case DisplayState::eOrientation180:
Lloyd Pique32cbe282018-10-19 13:09:22 -0700394 return ui::Transform::ROT_180;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700395 case DisplayState::eOrientation270:
Lloyd Pique32cbe282018-10-19 13:09:22 -0700396 return ui::Transform::ROT_270;
Mathias Agopian1b031492012-06-20 17:51:20 -0700397 default:
Lloyd Pique32cbe282018-10-19 13:09:22 -0700398 return ui::Transform::ROT_INVALID;
399 }
400}
401
402status_t DisplayDevice::orientationToTransfrom(int orientation, int w, int h, ui::Transform* tr) {
403 uint32_t flags = displayStateOrientationToTransformOrientation(orientation);
404 if (flags == ui::Transform::ROT_INVALID) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700405 return BAD_VALUE;
406 }
407 tr->set(flags, w, h);
408 return NO_ERROR;
409}
410
Michael Lentine47e45402014-07-18 15:34:25 -0700411void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700412 mCompositionDisplay->setBounds(ui::Size(newWidth, newHeight));
Michael Lentine47e45402014-07-18 15:34:25 -0700413}
414
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700415void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800416 const Rect& newViewport, const Rect& newFrame) {
417 Rect viewport(newViewport);
418 Rect frame(newFrame);
419
Lloyd Pique32cbe282018-10-19 13:09:22 -0700420 mOrientation = orientation;
421
422 const Rect& displayBounds = getCompositionDisplay()->getState().bounds;
423 const int w = displayBounds.width();
424 const int h = displayBounds.height();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800425
Peiyong Linefefaac2018-08-17 12:27:51 -0700426 ui::Transform R;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800427 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
428
429 if (!frame.isValid()) {
430 // the destination frame can be invalid if it has never been set,
431 // in that case we assume the whole display frame.
432 frame = Rect(w, h);
433 }
434
435 if (viewport.isEmpty()) {
436 // viewport can be invalid if it has never been set, in that case
437 // we assume the whole display size.
438 // it's also invalid to have an empty viewport, so we handle that
439 // case in the same way.
440 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700441 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800442 // viewport is always specified in the logical orientation
443 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700444 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800445 }
446 }
447
Peiyong Linefefaac2018-08-17 12:27:51 -0700448 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800449 float src_width = viewport.width();
450 float src_height = viewport.height();
451 float dst_width = frame.width();
452 float dst_height = frame.height();
453 if (src_width != dst_width || src_height != dst_height) {
454 float sx = dst_width / src_width;
455 float sy = dst_height / src_height;
456 S.set(sx, 0, 0, sy);
457 }
458
459 float src_x = viewport.left;
460 float src_y = viewport.top;
461 float dst_x = frame.left;
462 float dst_y = frame.top;
463 TL.set(-src_x, -src_y);
464 TP.set(dst_x, dst_y);
465
Lloyd Pique32cbe282018-10-19 13:09:22 -0700466 // need to take care of primary display rotation for globalTransform
Iris Chang7501ed62018-04-30 14:45:42 +0800467 // for case if the panel is not installed aligned with device orientation
Dominik Laskowski075d3172018-05-24 15:50:06 -0700468 if (isPrimary()) {
Iris Chang7501ed62018-04-30 14:45:42 +0800469 DisplayDevice::orientationToTransfrom(
Chia-I Wua02871c2018-08-27 14:38:23 -0700470 (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1),
Iris Chang7501ed62018-04-30 14:45:42 +0800471 w, h, &R);
472 }
473
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800474 // The viewport and frame are both in the logical orientation.
475 // Apply the logical translation, scale to physical size, apply the
476 // physical translation and finally rotate to the physical orientation.
Lloyd Pique32cbe282018-10-19 13:09:22 -0700477 ui::Transform globalTransform = R * TP * S * TL;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800478
Lloyd Pique32cbe282018-10-19 13:09:22 -0700479 const uint8_t type = globalTransform.getType();
480 const bool needsFiltering =
481 (!globalTransform.preserveRects() || (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800482
Lloyd Pique32cbe282018-10-19 13:09:22 -0700483 Rect scissor = globalTransform.transform(viewport);
484 if (scissor.isEmpty()) {
485 scissor = displayBounds;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800486 }
487
Dominik Laskowski281644e2018-04-19 15:47:35 -0700488 if (isPrimary()) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700489 sPrimaryDisplayOrientation = displayStateOrientationToTransformOrientation(orientation);
Pablo Ceballos021623b2016-04-15 17:31:51 -0700490 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700491
492 getCompositionDisplay()->setProjection(globalTransform,
493 displayStateOrientationToTransformOrientation(
494 orientation),
495 frame, viewport, scissor, needsFiltering);
Mathias Agopian1b031492012-06-20 17:51:20 -0700496}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700497
Pablo Ceballos021623b2016-04-15 17:31:51 -0700498uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
499 return sPrimaryDisplayOrientation;
500}
501
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700502std::string DisplayDevice::getDebugName() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -0700503 const auto id = getId() ? to_string(*getId()) + ", " : std::string();
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700504 return base::StringPrintf("DisplayDevice{%s%s%s\"%s\"}", id.c_str(),
505 isPrimary() ? "primary, " : "", isVirtual() ? "virtual, " : "",
506 mDisplayName.c_str());
507}
508
Yiwei Zhang5434a782018-12-05 18:06:32 -0800509void DisplayDevice::dump(std::string& result) const {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800510 StringAppendF(&result, "+ %s\n", getDebugName().c_str());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700511
512 result.append(" ");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700513 StringAppendF(&result, "powerMode=%d, ", mPowerMode);
514 StringAppendF(&result, "activeConfig=%d, ", mActiveConfig);
515 StringAppendF(&result, "numLayers=%zu\n", mVisibleLayersSortedByZ.size());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700516 StringAppendF(&result, "wideColorGamut=%d, ", mHasWideColorGamut);
517 StringAppendF(&result, "hdr10plus=%d\n", mHasHdr10Plus);
518 StringAppendF(&result, "hdr10=%d\n", mHasHdr10);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700519 getCompositionDisplay()->dump(result);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700520}
Irvelffc9efc2016-07-27 15:16:37 -0700521
Chia-I Wube02ec02018-05-18 10:59:36 -0700522// Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
523// supported by HWC.
524void DisplayDevice::addColorMode(
525 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
526 const ColorMode mode, const RenderIntent intent) {
527 // find the best color mode
528 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
529
530 // find the best render intent
531 auto iter = hwcColorModes.find(hwcColorMode);
532 const auto& hwcIntents =
533 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
534 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
535
536 const Dataspace dataspace = colorModeToDataspace(mode);
537 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
538
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700539 ALOGV("%s: map (%s, %s) to (%s, %s, %s)", getDebugName().c_str(),
Chia-I Wube02ec02018-05-18 10:59:36 -0700540 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
541 decodeRenderIntent(intent).c_str(),
542 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
543 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
544
545 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
546}
547
548void DisplayDevice::populateColorModes(
549 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) {
550 if (!hasWideColorGamut()) {
551 return;
552 }
553
Chia-I Wu0d711262018-05-21 15:19:35 -0700554 // collect all known SDR render intents
555 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
556 sSdrRenderIntents.end());
557 auto iter = hwcColorModes.find(ColorMode::SRGB);
558 if (iter != hwcColorModes.end()) {
559 for (auto intent : iter->second) {
560 sdrRenderIntents.insert(intent);
561 }
562 }
563
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700564 // add all known SDR combinations
Chia-I Wu0d711262018-05-21 15:19:35 -0700565 for (auto intent : sdrRenderIntents) {
Chia-I Wube02ec02018-05-18 10:59:36 -0700566 for (auto mode : sSdrColorModes) {
567 addColorMode(hwcColorModes, mode, intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700568 }
569 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700570
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700571 // collect all known HDR render intents
572 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
573 sHdrRenderIntents.end());
574 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
575 if (iter != hwcColorModes.end()) {
576 for (auto intent : iter->second) {
577 hdrRenderIntents.insert(intent);
578 }
579 }
580
581 // add all known HDR combinations
Chia-I Wube02ec02018-05-18 10:59:36 -0700582 for (auto intent : sHdrRenderIntents) {
583 for (auto mode : sHdrColorModes) {
584 addColorMode(hwcColorModes, mode, intent);
585 }
586 }
587}
588
589bool DisplayDevice::hasRenderIntent(RenderIntent intent) const {
590 // assume a render intent is supported when SRGB supports it; we should
591 // get rid of that assumption.
Peiyong Lin14724e62018-12-05 07:27:30 -0800592 auto iter = mColorModes.find(getColorModeKey(Dataspace::V0_SRGB, intent));
Chia-I Wube02ec02018-05-18 10:59:36 -0700593 return iter != mColorModes.end() && iter->second.renderIntent == intent;
594}
595
Peiyong Lindfde5112018-06-05 10:58:41 -0700596bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const {
Chia-I Wube02ec02018-05-18 10:59:36 -0700597 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
598 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
599 auto iter =
600 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
Peiyong Lindfde5112018-06-05 10:58:41 -0700601 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
Chia-I Wube02ec02018-05-18 10:59:36 -0700602 }
603
604 return false;
605}
606
607void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent,
608 Dataspace* outDataspace, ColorMode* outMode,
609 RenderIntent* outIntent) const {
610 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
611 if (iter != mColorModes.end()) {
612 *outDataspace = iter->second.dataspace;
613 *outMode = iter->second.colorMode;
614 *outIntent = iter->second.renderIntent;
615 } else {
Chia-I Wu6333af52018-10-16 13:46:36 -0700616 // this is unexpected on a WCG display
617 if (hasWideColorGamut()) {
618 ALOGE("map unknown (%s)/(%s) to default color mode",
619 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
620 decodeRenderIntent(intent).c_str());
621 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700622
623 *outDataspace = Dataspace::UNKNOWN;
624 *outMode = ColorMode::NATIVE;
625 *outIntent = RenderIntent::COLORIMETRIC;
626 }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700627}
628
Lloyd Pique45a165a2018-10-19 11:54:47 -0700629// ----------------------------------------------------------------------------
630
631const std::optional<DisplayId>& DisplayDevice::getId() const {
632 return mCompositionDisplay->getId();
633}
634
635bool DisplayDevice::isSecure() const {
636 return mCompositionDisplay->isSecure();
637}
638
Lloyd Pique32cbe282018-10-19 13:09:22 -0700639const Rect& DisplayDevice::getBounds() const {
640 return mCompositionDisplay->getState().bounds;
641}
642
643const Region& DisplayDevice::getUndefinedRegion() const {
644 return mCompositionDisplay->getState().undefinedRegion;
645}
646
647bool DisplayDevice::needsFiltering() const {
648 return mCompositionDisplay->getState().needsFiltering;
649}
650
651uint32_t DisplayDevice::getLayerStack() const {
652 return mCompositionDisplay->getState().singleLayerStackId;
653}
654
655const ui::Transform& DisplayDevice::getTransform() const {
656 return mCompositionDisplay->getState().transform;
657}
658
659const Rect& DisplayDevice::getViewport() const {
660 return mCompositionDisplay->getState().viewport;
661}
662
663const Rect& DisplayDevice::getFrame() const {
664 return mCompositionDisplay->getState().frame;
665}
666
667const Rect& DisplayDevice::getScissor() const {
668 return mCompositionDisplay->getState().scissor;
669}
670
Dominik Laskowski663bd282018-04-19 15:26:54 -0700671std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700672
673} // namespace android