blob: 4becfabdebc7406b6f324c4e8afe0a141bf86913 [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
Peiyong Lincbc184f2018-08-22 13:24:10 -070031#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
32#include <configstore/Utils.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include <cutils/properties.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070034#include <gui/Surface.h>
35#include <hardware/gralloc.h>
36#include <renderengine/RenderEngine.h>
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -060037#include <ui/DebugUtils.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070038#include <ui/DisplayInfo.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039#include <ui/PixelFormat.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070040#include <utils/RefBase.h>
41#include <utils/Log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
Jesse Hall99c7dbb2013-03-14 14:29:29 -070043#include "DisplayHardware/DisplaySurface.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070044#include "DisplayHardware/HWComposer.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080045#include "DisplayHardware/HWC2.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070046#include "SurfaceFlinger.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080047#include "Layer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048
Peiyong Linfd997e02018-03-28 15:29:00 -070049namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Jaesoo Lee720a7242017-01-31 15:26:18 +090051// retrieve triple buffer setting from configstore
52using namespace android::hardware::configstore;
53using namespace android::hardware::configstore::V1_0;
Peiyong Linfd997e02018-03-28 15:29:00 -070054using android::ui::ColorMode;
Chia-I Wube02ec02018-05-18 10:59:36 -070055using android::ui::Dataspace;
Peiyong Lin62665892018-04-16 11:07:44 -070056using android::ui::Hdr;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -080057using android::ui::RenderIntent;
Jaesoo Lee720a7242017-01-31 15:26:18 +090058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059/*
60 * Initialize the display to the specified values.
61 *
62 */
63
Pablo Ceballos021623b2016-04-15 17:31:51 -070064uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
65
Chia-I Wube02ec02018-05-18 10:59:36 -070066namespace {
67
68// ordered list of known SDR color modes
69const std::array<ColorMode, 2> sSdrColorModes = {
70 ColorMode::DISPLAY_P3,
71 ColorMode::SRGB,
72};
73
74// ordered list of known HDR color modes
75const std::array<ColorMode, 2> sHdrColorModes = {
76 ColorMode::BT2100_PQ,
77 ColorMode::BT2100_HLG,
78};
79
80// ordered list of known SDR render intents
81const std::array<RenderIntent, 2> sSdrRenderIntents = {
82 RenderIntent::ENHANCE,
83 RenderIntent::COLORIMETRIC,
84};
85
86// ordered list of known HDR render intents
87const std::array<RenderIntent, 2> sHdrRenderIntents = {
88 RenderIntent::TONE_MAP_ENHANCE,
89 RenderIntent::TONE_MAP_COLORIMETRIC,
90};
91
92// map known color mode to dataspace
93Dataspace colorModeToDataspace(ColorMode mode) {
94 switch (mode) {
95 case ColorMode::SRGB:
96 return Dataspace::SRGB;
97 case ColorMode::DISPLAY_P3:
98 return Dataspace::DISPLAY_P3;
99 case ColorMode::BT2100_HLG:
100 return Dataspace::BT2020_HLG;
101 case ColorMode::BT2100_PQ:
102 return Dataspace::BT2020_PQ;
103 default:
104 return Dataspace::UNKNOWN;
105 }
106}
107
108// Return a list of candidate color modes.
109std::vector<ColorMode> getColorModeCandidates(ColorMode mode) {
110 std::vector<ColorMode> candidates;
111
112 // add mode itself
113 candidates.push_back(mode);
114
115 // check if mode is HDR
116 bool isHdr = false;
117 for (auto hdrMode : sHdrColorModes) {
118 if (hdrMode == mode) {
119 isHdr = true;
120 break;
121 }
122 }
123
124 // add other HDR candidates when mode is HDR
125 if (isHdr) {
126 for (auto hdrMode : sHdrColorModes) {
127 if (hdrMode != mode) {
128 candidates.push_back(hdrMode);
129 }
130 }
131 }
132
133 // add other SDR candidates
134 for (auto sdrMode : sSdrColorModes) {
135 if (sdrMode != mode) {
136 candidates.push_back(sdrMode);
137 }
138 }
139
140 return candidates;
141}
142
143// Return a list of candidate render intents.
144std::vector<RenderIntent> getRenderIntentCandidates(RenderIntent intent) {
145 std::vector<RenderIntent> candidates;
146
147 // add intent itself
148 candidates.push_back(intent);
149
150 // check if intent is HDR
151 bool isHdr = false;
152 for (auto hdrIntent : sHdrRenderIntents) {
153 if (hdrIntent == intent) {
154 isHdr = true;
155 break;
156 }
157 }
158
Chia-I Wube02ec02018-05-18 10:59:36 -0700159 if (isHdr) {
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700160 // add other HDR candidates when intent is HDR
Chia-I Wube02ec02018-05-18 10:59:36 -0700161 for (auto hdrIntent : sHdrRenderIntents) {
162 if (hdrIntent != intent) {
163 candidates.push_back(hdrIntent);
164 }
165 }
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700166 } else {
167 // add other SDR candidates when intent is SDR
168 for (auto sdrIntent : sSdrRenderIntents) {
169 if (sdrIntent != intent) {
170 candidates.push_back(sdrIntent);
171 }
172 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700173 }
174
175 return candidates;
176}
177
178// Return the best color mode supported by HWC.
179ColorMode getHwcColorMode(
180 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
181 ColorMode mode) {
182 std::vector<ColorMode> candidates = getColorModeCandidates(mode);
183 for (auto candidate : candidates) {
184 auto iter = hwcColorModes.find(candidate);
185 if (iter != hwcColorModes.end()) {
186 return candidate;
187 }
188 }
189
190 return ColorMode::NATIVE;
191}
192
193// Return the best render intent supported by HWC.
194RenderIntent getHwcRenderIntent(const std::vector<RenderIntent>& hwcIntents, RenderIntent intent) {
195 std::vector<RenderIntent> candidates = getRenderIntentCandidates(intent);
196 for (auto candidate : candidates) {
197 for (auto hwcIntent : hwcIntents) {
198 if (candidate == hwcIntent) {
199 return candidate;
200 }
201 }
202 }
203
204 return RenderIntent::COLORIMETRIC;
205}
206
207} // anonymous namespace
208
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600209// clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700210DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700212 DisplayType type,
Dominik Laskowski7e045462018-05-30 13:02:02 -0700213 int32_t id,
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700214 bool isSecure,
215 const wp<IBinder>& displayToken,
Lloyd Pique09594832018-01-22 17:48:03 -0800216 const sp<ANativeWindow>& nativeWindow,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700217 const sp<DisplaySurface>& displaySurface,
Peiyong Lin833074a2018-08-28 11:53:54 -0700218 std::unique_ptr<renderengine::Surface> renderSurface,
Lloyd Pique09594832018-01-22 17:48:03 -0800219 int displayWidth,
220 int displayHeight,
Chia-I Wua02871c2018-08-27 14:38:23 -0700221 int displayInstallOrientation,
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800222 bool hasWideColorGamut,
Peiyong Lin62665892018-04-16 11:07:44 -0700223 const HdrCapabilities& hdrCapabilities,
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700224 const int32_t supportedPerFrameMetadata,
Chia-I Wube02ec02018-05-18 10:59:36 -0700225 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
Lloyd Pique09594832018-01-22 17:48:03 -0800226 int initialPowerMode)
Jesse Hallb7a05492014-08-14 15:45:06 -0700227 : lastCompositionHadVisibleLayers(false),
228 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800229 mType(type),
Dominik Laskowski7e045462018-05-30 13:02:02 -0700230 mId(id),
Chih-Wei Huang27e25622013-01-07 17:33:56 +0800231 mDisplayToken(displayToken),
Lloyd Pique09594832018-01-22 17:48:03 -0800232 mNativeWindow(nativeWindow),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700233 mDisplaySurface(displaySurface),
Lloyd Pique09594832018-01-22 17:48:03 -0800234 mSurface{std::move(renderSurface)},
235 mDisplayWidth(displayWidth),
236 mDisplayHeight(displayHeight),
Chia-I Wua02871c2018-08-27 14:38:23 -0700237 mDisplayInstallOrientation(displayInstallOrientation),
Lloyd Pique09594832018-01-22 17:48:03 -0800238 mPageFlipCount(0),
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700239 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -0800240 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700241 mOrientation(),
Lloyd Pique09594832018-01-22 17:48:03 -0800242 mViewport(Rect::INVALID_RECT),
243 mFrame(Rect::INVALID_RECT),
244 mPowerMode(initialPowerMode),
245 mActiveConfig(0),
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800246 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800247 mHasWideColorGamut(hasWideColorGamut),
Peiyong Lin62665892018-04-16 11:07:44 -0700248 mHasHdr10(false),
249 mHasHLG(false),
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700250 mHasDolbyVision(false),
Chia-I Wube02ec02018-05-18 10:59:36 -0700251 mSupportedPerFrameMetadata(supportedPerFrameMetadata)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600253 // clang-format on
Chia-I Wube02ec02018-05-18 10:59:36 -0700254 populateColorModes(hwcColorModes);
255
Peiyong Linfb069302018-04-25 14:34:31 -0700256 std::vector<Hdr> types = hdrCapabilities.getSupportedHdrTypes();
257 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700258 switch (hdrType) {
259 case Hdr::HDR10:
260 mHasHdr10 = true;
261 break;
262 case Hdr::HLG:
263 mHasHLG = true;
264 break;
265 case Hdr::DOLBY_VISION:
266 mHasDolbyVision = true;
267 break;
268 default:
269 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
270 }
271 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700272
Peiyong Linfb069302018-04-25 14:34:31 -0700273 float minLuminance = hdrCapabilities.getDesiredMinLuminance();
274 float maxLuminance = hdrCapabilities.getDesiredMaxLuminance();
275 float maxAverageLuminance = hdrCapabilities.getDesiredMaxAverageLuminance();
276
277 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
278 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
279 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
280 if (this->hasWideColorGamut()) {
281 // insert HDR10/HLG as we will force client composition for HDR10/HLG
282 // layers
283 if (!hasHDR10Support()) {
284 types.push_back(Hdr::HDR10);
285 }
286
287 if (!hasHLGSupport()) {
288 types.push_back(Hdr::HLG);
289 }
290 }
291 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
292
Jesse Hallffe1f192013-03-22 15:13:48 -0700293 // initialize the display orientation transform.
294 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295}
296
Lloyd Pique09594832018-01-22 17:48:03 -0800297DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700298
Jesse Hall02d86562013-03-25 14:43:23 -0700299void DisplayDevice::disconnect(HWComposer& hwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700300 if (mId >= 0) {
301 hwc.disconnectDisplay(mId);
302 mId = -1;
Jesse Hall02d86562013-03-25 14:43:23 -0700303 }
304}
305
Mathias Agopian92a979a2012-08-02 18:32:23 -0700306bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800307 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308}
309
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700310int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700311 return mDisplayWidth;
312}
313
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700314int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700315 return mDisplayHeight;
316}
317
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700318void DisplayDevice::setDisplayName(const std::string& displayName) {
319 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700320 // never override the name with an empty name
321 mDisplayName = displayName;
322 }
323}
324
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700325uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327}
328
Chia-I Wub02087d2017-11-09 10:19:54 -0800329void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700331 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700332 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333}
334
Dan Stoza71433162014-02-04 16:22:36 -0800335status_t DisplayDevice::beginFrame(bool mustRecompose) const {
336 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700337}
338
David Sodmanfb95bcc2017-12-22 15:45:30 -0800339status_t DisplayDevice::prepareFrame(HWComposer& hwc,
340 std::vector<CompositionInfo>& compositionData) {
341 status_t error = hwc.prepare(*this, compositionData);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800342 if (error != NO_ERROR) {
343 return error;
344 }
345
346 DisplaySurface::CompositionType compositionType;
Dominik Laskowski7e045462018-05-30 13:02:02 -0700347 bool hasClient = hwc.hasClientComposition(mId);
348 bool hasDevice = hwc.hasDeviceComposition(mId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800349 if (hasClient && hasDevice) {
350 compositionType = DisplaySurface::COMPOSITION_MIXED;
351 } else if (hasClient) {
352 compositionType = DisplaySurface::COMPOSITION_GLES;
353 } else if (hasDevice) {
354 compositionType = DisplaySurface::COMPOSITION_HWC;
355 } else {
356 // Nothing to do -- when turning the screen off we get a frame like
357 // this. Call it a HWC frame since we won't be doing any GLES work but
358 // will do a prepare/set cycle.
359 compositionType = DisplaySurface::COMPOSITION_HWC;
360 }
361 return mDisplaySurface->prepareFrame(compositionType);
362}
Jesse Hall38efe862013-04-06 23:12:29 -0700363
Mathias Agopianda27af92012-09-13 18:17:13 -0700364void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700365 if (hwc.hasClientComposition(mId) || hwc.hasFlipClientTargetRequest(mId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800366 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700367 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700368
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700369 status_t result = mDisplaySurface->advanceFrame();
370 if (result != NO_ERROR) {
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700371 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplayName.c_str(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700372 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700373}
374
Dan Stoza9e56aa02015-11-02 13:00:03 -0800375void DisplayDevice::onSwapBuffersCompleted() const {
376 mDisplaySurface->onFrameCommitted();
377}
Mathias Agopianda27af92012-09-13 18:17:13 -0700378
Chia-I Wuf846a352017-11-10 09:22:52 -0800379bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800380 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700381 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800382 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700383}
384
Mathias Agopian875d8e12013-06-07 15:35:48 -0700385void DisplayDevice::setViewportAndProjection() const {
386 size_t w = mDisplayWidth;
387 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700388 Rect sourceCrop(0, 0, w, h);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700389 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, ui::Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700390}
391
Dan Stoza9e56aa02015-11-02 13:00:03 -0800392const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
393 return mDisplaySurface->getClientTargetAcquireFence();
394}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800395
Mathias Agopian1b031492012-06-20 17:51:20 -0700396// ----------------------------------------------------------------------------
397
Mathias Agopian13127d82013-03-05 17:47:11 -0800398void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700399 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700400}
401
Mathias Agopian13127d82013-03-05 17:47:11 -0800402const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700403 return mVisibleLayersSortedByZ;
404}
405
Chia-I Wu83806892017-11-16 10:50:20 -0800406void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
407 mLayersNeedingFences = layers;
408}
409
410const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
411 return mLayersNeedingFences;
412}
413
Mathias Agopiancd60f992012-08-16 16:28:27 -0700414Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
415 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700416 if (repaintEverything) {
417 dirty.set(getBounds());
418 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700419 const ui::Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700420 dirty = planeTransform.transform(this->dirtyRegion);
421 dirty.andSelf(getBounds());
422 }
423 return dirty;
424}
425
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700426// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700427void DisplayDevice::setPowerMode(int mode) {
428 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700429}
430
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700431int DisplayDevice::getPowerMode() const {
432 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700433}
434
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700435bool DisplayDevice::isPoweredOn() const {
436 return mPowerMode != HWC_POWER_MODE_OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700437}
438
439// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700440void DisplayDevice::setActiveConfig(int mode) {
441 mActiveConfig = mode;
442}
443
444int DisplayDevice::getActiveConfig() const {
445 return mActiveConfig;
446}
447
448// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700449void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700450 mActiveColorMode = mode;
451}
452
Peiyong Lina52f0292018-03-14 17:26:31 -0700453ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700454 return mActiveColorMode;
455}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600456
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800457RenderIntent DisplayDevice::getActiveRenderIntent() const {
458 return mActiveRenderIntent;
459}
460
461void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
462 mActiveRenderIntent = renderIntent;
463}
464
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800465void DisplayDevice::setColorTransform(const mat4& transform) {
466 const bool isIdentity = (transform == mat4());
467 mColorTransform =
468 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
469}
470
471android_color_transform_t DisplayDevice::getColorTransform() const {
472 return mColorTransform;
473}
474
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700475void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800476 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600477 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700478 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600479}
Michael Wright28f24d02016-07-12 13:30:53 -0700480
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800481ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
482 return mCompositionDataSpace;
483}
484
Michael Wright28f24d02016-07-12 13:30:53 -0700485// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700486
Mathias Agopian28947d72012-08-08 18:51:15 -0700487void DisplayDevice::setLayerStack(uint32_t stack) {
488 mLayerStack = stack;
489 dirtyRegion.set(bounds());
490}
491
492// ----------------------------------------------------------------------------
493
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700494uint32_t DisplayDevice::getOrientationTransform() const {
495 uint32_t transform = 0;
496 switch (mOrientation) {
497 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700498 transform = ui::Transform::ROT_0;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700499 break;
500 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700501 transform = ui::Transform::ROT_90;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700502 break;
503 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700504 transform = ui::Transform::ROT_180;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700505 break;
506 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700507 transform = ui::Transform::ROT_270;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700508 break;
509 }
510 return transform;
511}
512
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700513status_t DisplayDevice::orientationToTransfrom(
Peiyong Linefefaac2018-08-17 12:27:51 -0700514 int orientation, int w, int h, ui::Transform* tr)
Mathias Agopian1b031492012-06-20 17:51:20 -0700515{
516 uint32_t flags = 0;
517 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700518 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700519 flags = ui::Transform::ROT_0;
Mathias Agopian1b031492012-06-20 17:51:20 -0700520 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700521 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700522 flags = ui::Transform::ROT_90;
Mathias Agopian1b031492012-06-20 17:51:20 -0700523 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700524 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700525 flags = ui::Transform::ROT_180;
Mathias Agopian1b031492012-06-20 17:51:20 -0700526 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700527 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700528 flags = ui::Transform::ROT_270;
Mathias Agopian1b031492012-06-20 17:51:20 -0700529 break;
530 default:
531 return BAD_VALUE;
532 }
533 tr->set(flags, w, h);
534 return NO_ERROR;
535}
536
Michael Lentine47e45402014-07-18 15:34:25 -0700537void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
538 dirtyRegion.set(getBounds());
539
Lloyd Pique144e1162017-12-20 16:44:52 -0800540 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700541
Michael Lentine47e45402014-07-18 15:34:25 -0700542 mDisplaySurface->resizeBuffers(newWidth, newHeight);
543
544 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800545 mSurface->setNativeWindow(window);
Alec Mouri05483a02018-09-10 21:03:42 +0000546 mDisplayWidth = mSurface->getWidth();
547 mDisplayHeight = mSurface->getHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700548
549 LOG_FATAL_IF(mDisplayWidth != newWidth,
550 "Unable to set new width to %d", newWidth);
551 LOG_FATAL_IF(mDisplayHeight != newHeight,
552 "Unable to set new height to %d", newHeight);
553}
554
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700555void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800556 const Rect& newViewport, const Rect& newFrame) {
557 Rect viewport(newViewport);
558 Rect frame(newFrame);
559
560 const int w = mDisplayWidth;
561 const int h = mDisplayHeight;
562
Peiyong Linefefaac2018-08-17 12:27:51 -0700563 ui::Transform R;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800564 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
565
566 if (!frame.isValid()) {
567 // the destination frame can be invalid if it has never been set,
568 // in that case we assume the whole display frame.
569 frame = Rect(w, h);
570 }
571
572 if (viewport.isEmpty()) {
573 // viewport can be invalid if it has never been set, in that case
574 // we assume the whole display size.
575 // it's also invalid to have an empty viewport, so we handle that
576 // case in the same way.
577 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700578 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800579 // viewport is always specified in the logical orientation
580 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700581 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800582 }
583 }
584
585 dirtyRegion.set(getBounds());
586
Peiyong Linefefaac2018-08-17 12:27:51 -0700587 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800588 float src_width = viewport.width();
589 float src_height = viewport.height();
590 float dst_width = frame.width();
591 float dst_height = frame.height();
592 if (src_width != dst_width || src_height != dst_height) {
593 float sx = dst_width / src_width;
594 float sy = dst_height / src_height;
595 S.set(sx, 0, 0, sy);
596 }
597
598 float src_x = viewport.left;
599 float src_y = viewport.top;
600 float dst_x = frame.left;
601 float dst_y = frame.top;
602 TL.set(-src_x, -src_y);
603 TP.set(dst_x, dst_y);
604
Iris Chang7501ed62018-04-30 14:45:42 +0800605 // need to take care of primary display rotation for mGlobalTransform
606 // for case if the panel is not installed aligned with device orientation
607 if (mType == DisplayType::DISPLAY_PRIMARY) {
Iris Chang7501ed62018-04-30 14:45:42 +0800608 DisplayDevice::orientationToTransfrom(
Chia-I Wua02871c2018-08-27 14:38:23 -0700609 (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1),
Iris Chang7501ed62018-04-30 14:45:42 +0800610 w, h, &R);
611 }
612
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800613 // The viewport and frame are both in the logical orientation.
614 // Apply the logical translation, scale to physical size, apply the
615 // physical translation and finally rotate to the physical orientation.
616 mGlobalTransform = R * TP * S * TL;
617
618 const uint8_t type = mGlobalTransform.getType();
619 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
Peiyong Linefefaac2018-08-17 12:27:51 -0700620 (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800621
622 mScissor = mGlobalTransform.transform(viewport);
623 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700624 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800625 }
626
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700627 mOrientation = orientation;
Dominik Laskowski281644e2018-04-19 15:47:35 -0700628 if (isPrimary()) {
Pablo Ceballos021623b2016-04-15 17:31:51 -0700629 uint32_t transform = 0;
630 switch (mOrientation) {
631 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700632 transform = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700633 break;
634 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700635 transform = ui::Transform::ROT_90;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700636 break;
637 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700638 transform = ui::Transform::ROT_180;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700639 break;
640 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700641 transform = ui::Transform::ROT_270;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700642 break;
643 }
644 sPrimaryDisplayOrientation = transform;
645 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700646 mViewport = viewport;
647 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700648}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700649
Pablo Ceballos021623b2016-04-15 17:31:51 -0700650uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
651 return sPrimaryDisplayOrientation;
652}
653
Mathias Agopian74d211a2013-04-22 16:55:35 +0200654void DisplayDevice::dump(String8& result) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700655 const ui::Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600656 ANativeWindow* const window = mNativeWindow.get();
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700657 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.c_str());
Dominik Laskowski7e045462018-05-30 13:02:02 -0700658 result.appendFormat(" type=%x, ID=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600659 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
660 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Dominik Laskowski7e045462018-05-30 13:02:02 -0700661 mType, mId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800662 mSurface->queryRedSize(), mSurface->queryGreenSize(),
663 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
664 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600665 mVisibleLayersSortedByZ.size());
666 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
667 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
668 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
669 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
670 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
671 tr[0][1], tr[1][1], tr[2][1], tr[0][2], tr[1][2], tr[2][2]);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600672 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700673 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800674 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
675 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800676 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700677 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700678
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700679 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800680 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700681 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700682}
Irvelffc9efc2016-07-27 15:16:37 -0700683
Chia-I Wube02ec02018-05-18 10:59:36 -0700684// Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
685// supported by HWC.
686void DisplayDevice::addColorMode(
687 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
688 const ColorMode mode, const RenderIntent intent) {
689 // find the best color mode
690 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
691
692 // find the best render intent
693 auto iter = hwcColorModes.find(hwcColorMode);
694 const auto& hwcIntents =
695 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
696 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
697
698 const Dataspace dataspace = colorModeToDataspace(mode);
699 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
700
Dominik Laskowski7e045462018-05-30 13:02:02 -0700701 ALOGV("DisplayDevice %d/%d: map (%s, %s) to (%s, %s, %s)", mType, mId,
Chia-I Wube02ec02018-05-18 10:59:36 -0700702 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
703 decodeRenderIntent(intent).c_str(),
704 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
705 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
706
707 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
708}
709
710void DisplayDevice::populateColorModes(
711 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) {
712 if (!hasWideColorGamut()) {
713 return;
714 }
715
Chia-I Wu0d711262018-05-21 15:19:35 -0700716 // collect all known SDR render intents
717 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
718 sSdrRenderIntents.end());
719 auto iter = hwcColorModes.find(ColorMode::SRGB);
720 if (iter != hwcColorModes.end()) {
721 for (auto intent : iter->second) {
722 sdrRenderIntents.insert(intent);
723 }
724 }
725
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700726 // add all known SDR combinations
Chia-I Wu0d711262018-05-21 15:19:35 -0700727 for (auto intent : sdrRenderIntents) {
Chia-I Wube02ec02018-05-18 10:59:36 -0700728 for (auto mode : sSdrColorModes) {
729 addColorMode(hwcColorModes, mode, intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700730 }
731 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700732
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700733 // collect all known HDR render intents
734 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
735 sHdrRenderIntents.end());
736 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
737 if (iter != hwcColorModes.end()) {
738 for (auto intent : iter->second) {
739 hdrRenderIntents.insert(intent);
740 }
741 }
742
743 // add all known HDR combinations
Chia-I Wube02ec02018-05-18 10:59:36 -0700744 for (auto intent : sHdrRenderIntents) {
745 for (auto mode : sHdrColorModes) {
746 addColorMode(hwcColorModes, mode, intent);
747 }
748 }
749}
750
751bool DisplayDevice::hasRenderIntent(RenderIntent intent) const {
752 // assume a render intent is supported when SRGB supports it; we should
753 // get rid of that assumption.
754 auto iter = mColorModes.find(getColorModeKey(Dataspace::SRGB, intent));
755 return iter != mColorModes.end() && iter->second.renderIntent == intent;
756}
757
Peiyong Lindfde5112018-06-05 10:58:41 -0700758bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const {
Chia-I Wube02ec02018-05-18 10:59:36 -0700759 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
760 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
761 auto iter =
762 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
Peiyong Lindfde5112018-06-05 10:58:41 -0700763 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
Chia-I Wube02ec02018-05-18 10:59:36 -0700764 }
765
766 return false;
767}
768
769void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent,
770 Dataspace* outDataspace, ColorMode* outMode,
771 RenderIntent* outIntent) const {
772 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
773 if (iter != mColorModes.end()) {
774 *outDataspace = iter->second.dataspace;
775 *outMode = iter->second.colorMode;
776 *outIntent = iter->second.renderIntent;
777 } else {
778 ALOGE("map unknown (%s)/(%s) to default color mode",
779 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
780 decodeRenderIntent(intent).c_str());
781
782 *outDataspace = Dataspace::UNKNOWN;
783 *outMode = ColorMode::NATIVE;
784 *outIntent = RenderIntent::COLORIMETRIC;
785 }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700786}
787
Dominik Laskowski663bd282018-04-19 15:26:54 -0700788std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700789
790} // namespace android