blob: 776b84ae0bf88efed4cb4b8a7945abd5987277ed [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
Chia-I Wube02ec02018-05-18 10:59:36 -070021#include <array>
22#include <unordered_set>
23
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include <math.h>
28
29#include <cutils/properties.h>
30
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include <utils/Log.h>
33
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -060034#include <ui/DebugUtils.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038#include <gui/Surface.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Jesse Hall99c7dbb2013-03-14 14:29:29 -070042#include "DisplayHardware/DisplaySurface.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070043#include "DisplayHardware/HWComposer.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080044#include "DisplayHardware/HWC2.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070045#include "RenderEngine/RenderEngine.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070046
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070047#include "DisplayDevice.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070048#include "SurfaceFlinger.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080049#include "Layer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070050
Jaesoo Lee720a7242017-01-31 15:26:18 +090051#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
52#include <configstore/Utils.h>
53
Peiyong Linfd997e02018-03-28 15:29:00 -070054namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Jaesoo Lee720a7242017-01-31 15:26:18 +090056// retrieve triple buffer setting from configstore
57using namespace android::hardware::configstore;
58using namespace android::hardware::configstore::V1_0;
Peiyong Linfd997e02018-03-28 15:29:00 -070059using android::ui::ColorMode;
Chia-I Wube02ec02018-05-18 10:59:36 -070060using android::ui::Dataspace;
Peiyong Lin62665892018-04-16 11:07:44 -070061using android::ui::Hdr;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -080062using android::ui::RenderIntent;
Jaesoo Lee720a7242017-01-31 15:26:18 +090063
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064/*
65 * Initialize the display to the specified values.
66 *
67 */
68
Pablo Ceballos021623b2016-04-15 17:31:51 -070069uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
70
Chia-I Wube02ec02018-05-18 10:59:36 -070071namespace {
72
73// ordered list of known SDR color modes
74const std::array<ColorMode, 2> sSdrColorModes = {
75 ColorMode::DISPLAY_P3,
76 ColorMode::SRGB,
77};
78
79// ordered list of known HDR color modes
80const std::array<ColorMode, 2> sHdrColorModes = {
81 ColorMode::BT2100_PQ,
82 ColorMode::BT2100_HLG,
83};
84
85// ordered list of known SDR render intents
86const std::array<RenderIntent, 2> sSdrRenderIntents = {
87 RenderIntent::ENHANCE,
88 RenderIntent::COLORIMETRIC,
89};
90
91// ordered list of known HDR render intents
92const std::array<RenderIntent, 2> sHdrRenderIntents = {
93 RenderIntent::TONE_MAP_ENHANCE,
94 RenderIntent::TONE_MAP_COLORIMETRIC,
95};
96
97// map known color mode to dataspace
98Dataspace colorModeToDataspace(ColorMode mode) {
99 switch (mode) {
100 case ColorMode::SRGB:
101 return Dataspace::SRGB;
102 case ColorMode::DISPLAY_P3:
103 return Dataspace::DISPLAY_P3;
104 case ColorMode::BT2100_HLG:
105 return Dataspace::BT2020_HLG;
106 case ColorMode::BT2100_PQ:
107 return Dataspace::BT2020_PQ;
108 default:
109 return Dataspace::UNKNOWN;
110 }
111}
112
113// Return a list of candidate color modes.
114std::vector<ColorMode> getColorModeCandidates(ColorMode mode) {
115 std::vector<ColorMode> candidates;
116
117 // add mode itself
118 candidates.push_back(mode);
119
120 // check if mode is HDR
121 bool isHdr = false;
122 for (auto hdrMode : sHdrColorModes) {
123 if (hdrMode == mode) {
124 isHdr = true;
125 break;
126 }
127 }
128
129 // add other HDR candidates when mode is HDR
130 if (isHdr) {
131 for (auto hdrMode : sHdrColorModes) {
132 if (hdrMode != mode) {
133 candidates.push_back(hdrMode);
134 }
135 }
136 }
137
138 // add other SDR candidates
139 for (auto sdrMode : sSdrColorModes) {
140 if (sdrMode != mode) {
141 candidates.push_back(sdrMode);
142 }
143 }
144
145 return candidates;
146}
147
148// Return a list of candidate render intents.
149std::vector<RenderIntent> getRenderIntentCandidates(RenderIntent intent) {
150 std::vector<RenderIntent> candidates;
151
152 // add intent itself
153 candidates.push_back(intent);
154
155 // check if intent is HDR
156 bool isHdr = false;
157 for (auto hdrIntent : sHdrRenderIntents) {
158 if (hdrIntent == intent) {
159 isHdr = true;
160 break;
161 }
162 }
163
Chia-I Wube02ec02018-05-18 10:59:36 -0700164 if (isHdr) {
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700165 // add other HDR candidates when intent is HDR
Chia-I Wube02ec02018-05-18 10:59:36 -0700166 for (auto hdrIntent : sHdrRenderIntents) {
167 if (hdrIntent != intent) {
168 candidates.push_back(hdrIntent);
169 }
170 }
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700171 } else {
172 // add other SDR candidates when intent is SDR
173 for (auto sdrIntent : sSdrRenderIntents) {
174 if (sdrIntent != intent) {
175 candidates.push_back(sdrIntent);
176 }
177 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700178 }
179
180 return candidates;
181}
182
183// Return the best color mode supported by HWC.
184ColorMode getHwcColorMode(
185 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
186 ColorMode mode) {
187 std::vector<ColorMode> candidates = getColorModeCandidates(mode);
188 for (auto candidate : candidates) {
189 auto iter = hwcColorModes.find(candidate);
190 if (iter != hwcColorModes.end()) {
191 return candidate;
192 }
193 }
194
195 return ColorMode::NATIVE;
196}
197
198// Return the best render intent supported by HWC.
199RenderIntent getHwcRenderIntent(const std::vector<RenderIntent>& hwcIntents, RenderIntent intent) {
200 std::vector<RenderIntent> candidates = getRenderIntentCandidates(intent);
201 for (auto candidate : candidates) {
202 for (auto hwcIntent : hwcIntents) {
203 if (candidate == hwcIntent) {
204 return candidate;
205 }
206 }
207 }
208
209 return RenderIntent::COLORIMETRIC;
210}
211
212} // anonymous namespace
213
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600214// clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700215DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700217 DisplayType type,
Dominik Laskowski7e045462018-05-30 13:02:02 -0700218 int32_t id,
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700219 bool isSecure,
220 const wp<IBinder>& displayToken,
Lloyd Pique09594832018-01-22 17:48:03 -0800221 const sp<ANativeWindow>& nativeWindow,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700222 const sp<DisplaySurface>& displaySurface,
Lloyd Pique09594832018-01-22 17:48:03 -0800223 std::unique_ptr<RE::Surface> renderSurface,
224 int displayWidth,
225 int displayHeight,
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800226 bool hasWideColorGamut,
Peiyong Lin62665892018-04-16 11:07:44 -0700227 const HdrCapabilities& hdrCapabilities,
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700228 const int32_t supportedPerFrameMetadata,
Chia-I Wube02ec02018-05-18 10:59:36 -0700229 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
Lloyd Pique09594832018-01-22 17:48:03 -0800230 int initialPowerMode)
Jesse Hallb7a05492014-08-14 15:45:06 -0700231 : lastCompositionHadVisibleLayers(false),
232 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800233 mType(type),
Dominik Laskowski7e045462018-05-30 13:02:02 -0700234 mId(id),
Chih-Wei Huang27e25622013-01-07 17:33:56 +0800235 mDisplayToken(displayToken),
Lloyd Pique09594832018-01-22 17:48:03 -0800236 mNativeWindow(nativeWindow),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700237 mDisplaySurface(displaySurface),
Lloyd Pique09594832018-01-22 17:48:03 -0800238 mSurface{std::move(renderSurface)},
239 mDisplayWidth(displayWidth),
240 mDisplayHeight(displayHeight),
241 mPageFlipCount(0),
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700242 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -0800243 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700244 mOrientation(),
Lloyd Pique09594832018-01-22 17:48:03 -0800245 mViewport(Rect::INVALID_RECT),
246 mFrame(Rect::INVALID_RECT),
247 mPowerMode(initialPowerMode),
248 mActiveConfig(0),
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800249 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800250 mHasWideColorGamut(hasWideColorGamut),
Peiyong Lin62665892018-04-16 11:07:44 -0700251 mHasHdr10(false),
252 mHasHLG(false),
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700253 mHasDolbyVision(false),
Chia-I Wube02ec02018-05-18 10:59:36 -0700254 mSupportedPerFrameMetadata(supportedPerFrameMetadata)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600256 // clang-format on
Chia-I Wube02ec02018-05-18 10:59:36 -0700257 populateColorModes(hwcColorModes);
258
Peiyong Linfb069302018-04-25 14:34:31 -0700259 std::vector<Hdr> types = hdrCapabilities.getSupportedHdrTypes();
260 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700261 switch (hdrType) {
262 case Hdr::HDR10:
263 mHasHdr10 = true;
264 break;
265 case Hdr::HLG:
266 mHasHLG = true;
267 break;
268 case Hdr::DOLBY_VISION:
269 mHasDolbyVision = true;
270 break;
271 default:
272 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
273 }
274 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700275
Peiyong Linfb069302018-04-25 14:34:31 -0700276 float minLuminance = hdrCapabilities.getDesiredMinLuminance();
277 float maxLuminance = hdrCapabilities.getDesiredMaxLuminance();
278 float maxAverageLuminance = hdrCapabilities.getDesiredMaxAverageLuminance();
279
280 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
281 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
282 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
283 if (this->hasWideColorGamut()) {
284 // insert HDR10/HLG as we will force client composition for HDR10/HLG
285 // layers
286 if (!hasHDR10Support()) {
287 types.push_back(Hdr::HDR10);
288 }
289
290 if (!hasHLGSupport()) {
291 types.push_back(Hdr::HLG);
292 }
293 }
294 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
295
Jesse Hallffe1f192013-03-22 15:13:48 -0700296 // initialize the display orientation transform.
297 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298}
299
Lloyd Pique09594832018-01-22 17:48:03 -0800300DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700301
Jesse Hall02d86562013-03-25 14:43:23 -0700302void DisplayDevice::disconnect(HWComposer& hwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700303 if (mId >= 0) {
304 hwc.disconnectDisplay(mId);
305 mId = -1;
Jesse Hall02d86562013-03-25 14:43:23 -0700306 }
307}
308
Mathias Agopian92a979a2012-08-02 18:32:23 -0700309bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800310 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311}
312
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700313int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700314 return mDisplayWidth;
315}
316
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700317int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700318 return mDisplayHeight;
319}
320
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700321void DisplayDevice::setDisplayName(const std::string& displayName) {
322 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700323 // never override the name with an empty name
324 mDisplayName = displayName;
325 }
326}
327
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700328uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700329 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330}
331
Chia-I Wub02087d2017-11-09 10:19:54 -0800332void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700334 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700335 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336}
337
Dan Stoza71433162014-02-04 16:22:36 -0800338status_t DisplayDevice::beginFrame(bool mustRecompose) const {
339 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700340}
341
Dan Stoza9e56aa02015-11-02 13:00:03 -0800342status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
343 status_t error = hwc.prepare(*this);
344 if (error != NO_ERROR) {
345 return error;
346 }
347
348 DisplaySurface::CompositionType compositionType;
Dominik Laskowski7e045462018-05-30 13:02:02 -0700349 bool hasClient = hwc.hasClientComposition(mId);
350 bool hasDevice = hwc.hasDeviceComposition(mId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800351 if (hasClient && hasDevice) {
352 compositionType = DisplaySurface::COMPOSITION_MIXED;
353 } else if (hasClient) {
354 compositionType = DisplaySurface::COMPOSITION_GLES;
355 } else if (hasDevice) {
356 compositionType = DisplaySurface::COMPOSITION_HWC;
357 } else {
358 // Nothing to do -- when turning the screen off we get a frame like
359 // this. Call it a HWC frame since we won't be doing any GLES work but
360 // will do a prepare/set cycle.
361 compositionType = DisplaySurface::COMPOSITION_HWC;
362 }
363 return mDisplaySurface->prepareFrame(compositionType);
364}
Jesse Hall38efe862013-04-06 23:12:29 -0700365
Mathias Agopianda27af92012-09-13 18:17:13 -0700366void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700367 if (hwc.hasClientComposition(mId) || hwc.hasFlipClientTargetRequest(mId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800368 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700369 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700370
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700371 status_t result = mDisplaySurface->advanceFrame();
372 if (result != NO_ERROR) {
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700373 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplayName.c_str(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700374 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700375}
376
Dan Stoza9e56aa02015-11-02 13:00:03 -0800377void DisplayDevice::onSwapBuffersCompleted() const {
378 mDisplaySurface->onFrameCommitted();
379}
Mathias Agopianda27af92012-09-13 18:17:13 -0700380
Chia-I Wuf846a352017-11-10 09:22:52 -0800381bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800382 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700383 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800384 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700385}
386
Mathias Agopian875d8e12013-06-07 15:35:48 -0700387void DisplayDevice::setViewportAndProjection() const {
388 size_t w = mDisplayWidth;
389 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700390 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700391 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
Peiyong Linefefaac2018-08-17 12:27:51 -0700392 false, ui::Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700393}
394
Dan Stoza9e56aa02015-11-02 13:00:03 -0800395const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
396 return mDisplaySurface->getClientTargetAcquireFence();
397}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800398
Mathias Agopian1b031492012-06-20 17:51:20 -0700399// ----------------------------------------------------------------------------
400
Mathias Agopian13127d82013-03-05 17:47:11 -0800401void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700402 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700403}
404
Mathias Agopian13127d82013-03-05 17:47:11 -0800405const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700406 return mVisibleLayersSortedByZ;
407}
408
Chia-I Wu83806892017-11-16 10:50:20 -0800409void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
410 mLayersNeedingFences = layers;
411}
412
413const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
414 return mLayersNeedingFences;
415}
416
Mathias Agopiancd60f992012-08-16 16:28:27 -0700417Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
418 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700419 if (repaintEverything) {
420 dirty.set(getBounds());
421 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700422 const ui::Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700423 dirty = planeTransform.transform(this->dirtyRegion);
424 dirty.andSelf(getBounds());
425 }
426 return dirty;
427}
428
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700429// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700430void DisplayDevice::setPowerMode(int mode) {
431 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700432}
433
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700434int DisplayDevice::getPowerMode() const {
435 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700436}
437
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700438bool DisplayDevice::isPoweredOn() const {
439 return mPowerMode != HWC_POWER_MODE_OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700440}
441
442// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700443void DisplayDevice::setActiveConfig(int mode) {
444 mActiveConfig = mode;
445}
446
447int DisplayDevice::getActiveConfig() const {
448 return mActiveConfig;
449}
450
451// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700452void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700453 mActiveColorMode = mode;
454}
455
Peiyong Lina52f0292018-03-14 17:26:31 -0700456ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700457 return mActiveColorMode;
458}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600459
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800460RenderIntent DisplayDevice::getActiveRenderIntent() const {
461 return mActiveRenderIntent;
462}
463
464void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
465 mActiveRenderIntent = renderIntent;
466}
467
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800468void DisplayDevice::setColorTransform(const mat4& transform) {
469 const bool isIdentity = (transform == mat4());
470 mColorTransform =
471 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
472}
473
474android_color_transform_t DisplayDevice::getColorTransform() const {
475 return mColorTransform;
476}
477
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700478void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800479 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600480 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700481 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600482}
Michael Wright28f24d02016-07-12 13:30:53 -0700483
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800484ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
485 return mCompositionDataSpace;
486}
487
Michael Wright28f24d02016-07-12 13:30:53 -0700488// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700489
Mathias Agopian28947d72012-08-08 18:51:15 -0700490void DisplayDevice::setLayerStack(uint32_t stack) {
491 mLayerStack = stack;
492 dirtyRegion.set(bounds());
493}
494
495// ----------------------------------------------------------------------------
496
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700497uint32_t DisplayDevice::getOrientationTransform() const {
498 uint32_t transform = 0;
499 switch (mOrientation) {
500 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700501 transform = ui::Transform::ROT_0;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700502 break;
503 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700504 transform = ui::Transform::ROT_90;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700505 break;
506 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700507 transform = ui::Transform::ROT_180;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700508 break;
509 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700510 transform = ui::Transform::ROT_270;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700511 break;
512 }
513 return transform;
514}
515
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700516status_t DisplayDevice::orientationToTransfrom(
Peiyong Linefefaac2018-08-17 12:27:51 -0700517 int orientation, int w, int h, ui::Transform* tr)
Mathias Agopian1b031492012-06-20 17:51:20 -0700518{
519 uint32_t flags = 0;
520 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700521 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700522 flags = ui::Transform::ROT_0;
Mathias Agopian1b031492012-06-20 17:51:20 -0700523 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700524 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700525 flags = ui::Transform::ROT_90;
Mathias Agopian1b031492012-06-20 17:51:20 -0700526 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700527 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700528 flags = ui::Transform::ROT_180;
Mathias Agopian1b031492012-06-20 17:51:20 -0700529 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700530 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700531 flags = ui::Transform::ROT_270;
Mathias Agopian1b031492012-06-20 17:51:20 -0700532 break;
533 default:
534 return BAD_VALUE;
535 }
536 tr->set(flags, w, h);
537 return NO_ERROR;
538}
539
Michael Lentine47e45402014-07-18 15:34:25 -0700540void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
541 dirtyRegion.set(getBounds());
542
Lloyd Pique144e1162017-12-20 16:44:52 -0800543 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700544
Michael Lentine47e45402014-07-18 15:34:25 -0700545 mDisplaySurface->resizeBuffers(newWidth, newHeight);
546
547 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800548 mSurface->setNativeWindow(window);
549 mDisplayWidth = mSurface->queryWidth();
550 mDisplayHeight = mSurface->queryHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700551
552 LOG_FATAL_IF(mDisplayWidth != newWidth,
553 "Unable to set new width to %d", newWidth);
554 LOG_FATAL_IF(mDisplayHeight != newHeight,
555 "Unable to set new height to %d", newHeight);
556}
557
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700558void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800559 const Rect& newViewport, const Rect& newFrame) {
560 Rect viewport(newViewport);
561 Rect frame(newFrame);
562
563 const int w = mDisplayWidth;
564 const int h = mDisplayHeight;
565
Peiyong Linefefaac2018-08-17 12:27:51 -0700566 ui::Transform R;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800567 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
568
569 if (!frame.isValid()) {
570 // the destination frame can be invalid if it has never been set,
571 // in that case we assume the whole display frame.
572 frame = Rect(w, h);
573 }
574
575 if (viewport.isEmpty()) {
576 // viewport can be invalid if it has never been set, in that case
577 // we assume the whole display size.
578 // it's also invalid to have an empty viewport, so we handle that
579 // case in the same way.
580 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700581 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800582 // viewport is always specified in the logical orientation
583 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700584 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800585 }
586 }
587
588 dirtyRegion.set(getBounds());
589
Peiyong Linefefaac2018-08-17 12:27:51 -0700590 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800591 float src_width = viewport.width();
592 float src_height = viewport.height();
593 float dst_width = frame.width();
594 float dst_height = frame.height();
595 if (src_width != dst_width || src_height != dst_height) {
596 float sx = dst_width / src_width;
597 float sy = dst_height / src_height;
598 S.set(sx, 0, 0, sy);
599 }
600
601 float src_x = viewport.left;
602 float src_y = viewport.top;
603 float dst_x = frame.left;
604 float dst_y = frame.top;
605 TL.set(-src_x, -src_y);
606 TP.set(dst_x, dst_y);
607
Iris Chang7501ed62018-04-30 14:45:42 +0800608 // need to take care of primary display rotation for mGlobalTransform
609 // for case if the panel is not installed aligned with device orientation
610 if (mType == DisplayType::DISPLAY_PRIMARY) {
611 int primaryDisplayOrientation = mFlinger->getPrimaryDisplayOrientation();
612 DisplayDevice::orientationToTransfrom(
613 (orientation + primaryDisplayOrientation) % (DisplayState::eOrientation270 + 1),
614 w, h, &R);
615 }
616
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800617 // The viewport and frame are both in the logical orientation.
618 // Apply the logical translation, scale to physical size, apply the
619 // physical translation and finally rotate to the physical orientation.
620 mGlobalTransform = R * TP * S * TL;
621
622 const uint8_t type = mGlobalTransform.getType();
623 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
Peiyong Linefefaac2018-08-17 12:27:51 -0700624 (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800625
626 mScissor = mGlobalTransform.transform(viewport);
627 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700628 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800629 }
630
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700631 mOrientation = orientation;
Dominik Laskowski281644e2018-04-19 15:47:35 -0700632 if (isPrimary()) {
Pablo Ceballos021623b2016-04-15 17:31:51 -0700633 uint32_t transform = 0;
634 switch (mOrientation) {
635 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700636 transform = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700637 break;
638 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700639 transform = ui::Transform::ROT_90;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700640 break;
641 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700642 transform = ui::Transform::ROT_180;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700643 break;
644 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700645 transform = ui::Transform::ROT_270;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700646 break;
647 }
648 sPrimaryDisplayOrientation = transform;
649 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700650 mViewport = viewport;
651 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700652}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700653
Pablo Ceballos021623b2016-04-15 17:31:51 -0700654uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
655 return sPrimaryDisplayOrientation;
656}
657
Mathias Agopian74d211a2013-04-22 16:55:35 +0200658void DisplayDevice::dump(String8& result) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700659 const ui::Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600660 ANativeWindow* const window = mNativeWindow.get();
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700661 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.c_str());
Dominik Laskowski7e045462018-05-30 13:02:02 -0700662 result.appendFormat(" type=%x, ID=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600663 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
664 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Dominik Laskowski7e045462018-05-30 13:02:02 -0700665 mType, mId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800666 mSurface->queryRedSize(), mSurface->queryGreenSize(),
667 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
668 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600669 mVisibleLayersSortedByZ.size());
670 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
671 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
672 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
673 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
674 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
675 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 -0600676 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700677 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800678 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
679 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800680 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700681 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700682
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700683 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800684 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700685 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700686}
Irvelffc9efc2016-07-27 15:16:37 -0700687
Chia-I Wube02ec02018-05-18 10:59:36 -0700688// Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
689// supported by HWC.
690void DisplayDevice::addColorMode(
691 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
692 const ColorMode mode, const RenderIntent intent) {
693 // find the best color mode
694 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
695
696 // find the best render intent
697 auto iter = hwcColorModes.find(hwcColorMode);
698 const auto& hwcIntents =
699 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
700 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
701
702 const Dataspace dataspace = colorModeToDataspace(mode);
703 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
704
Dominik Laskowski7e045462018-05-30 13:02:02 -0700705 ALOGV("DisplayDevice %d/%d: map (%s, %s) to (%s, %s, %s)", mType, mId,
Chia-I Wube02ec02018-05-18 10:59:36 -0700706 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
707 decodeRenderIntent(intent).c_str(),
708 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
709 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
710
711 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
712}
713
714void DisplayDevice::populateColorModes(
715 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) {
716 if (!hasWideColorGamut()) {
717 return;
718 }
719
Chia-I Wu0d711262018-05-21 15:19:35 -0700720 // collect all known SDR render intents
721 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
722 sSdrRenderIntents.end());
723 auto iter = hwcColorModes.find(ColorMode::SRGB);
724 if (iter != hwcColorModes.end()) {
725 for (auto intent : iter->second) {
726 sdrRenderIntents.insert(intent);
727 }
728 }
729
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700730 // add all known SDR combinations
Chia-I Wu0d711262018-05-21 15:19:35 -0700731 for (auto intent : sdrRenderIntents) {
Chia-I Wube02ec02018-05-18 10:59:36 -0700732 for (auto mode : sSdrColorModes) {
733 addColorMode(hwcColorModes, mode, intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700734 }
735 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700736
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700737 // collect all known HDR render intents
738 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
739 sHdrRenderIntents.end());
740 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
741 if (iter != hwcColorModes.end()) {
742 for (auto intent : iter->second) {
743 hdrRenderIntents.insert(intent);
744 }
745 }
746
747 // add all known HDR combinations
Chia-I Wube02ec02018-05-18 10:59:36 -0700748 for (auto intent : sHdrRenderIntents) {
749 for (auto mode : sHdrColorModes) {
750 addColorMode(hwcColorModes, mode, intent);
751 }
752 }
753}
754
755bool DisplayDevice::hasRenderIntent(RenderIntent intent) const {
756 // assume a render intent is supported when SRGB supports it; we should
757 // get rid of that assumption.
758 auto iter = mColorModes.find(getColorModeKey(Dataspace::SRGB, intent));
759 return iter != mColorModes.end() && iter->second.renderIntent == intent;
760}
761
Peiyong Lindfde5112018-06-05 10:58:41 -0700762bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const {
Chia-I Wube02ec02018-05-18 10:59:36 -0700763 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
764 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
765 auto iter =
766 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
Peiyong Lindfde5112018-06-05 10:58:41 -0700767 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
Chia-I Wube02ec02018-05-18 10:59:36 -0700768 }
769
770 return false;
771}
772
773void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent,
774 Dataspace* outDataspace, ColorMode* outMode,
775 RenderIntent* outIntent) const {
776 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
777 if (iter != mColorModes.end()) {
778 *outDataspace = iter->second.dataspace;
779 *outMode = iter->second.colorMode;
780 *outIntent = iter->second.renderIntent;
781 } else {
782 ALOGE("map unknown (%s)/(%s) to default color mode",
783 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
784 decodeRenderIntent(intent).c_str());
785
786 *outDataspace = Dataspace::UNKNOWN;
787 *outMode = ColorMode::NATIVE;
788 *outIntent = RenderIntent::COLORIMETRIC;
789 }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700790}
791
Dominik Laskowski663bd282018-04-19 15:26:54 -0700792std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700793
794} // namespace android