blob: 0fbd4594f86d02623504e0f135a869c04f323022 [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,
Lloyd Pique09594832018-01-22 17:48:03 -0800218 std::unique_ptr<RE::Surface> renderSurface,
219 int displayWidth,
220 int displayHeight,
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800221 bool hasWideColorGamut,
Peiyong Lin62665892018-04-16 11:07:44 -0700222 const HdrCapabilities& hdrCapabilities,
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700223 const int32_t supportedPerFrameMetadata,
Chia-I Wube02ec02018-05-18 10:59:36 -0700224 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
Lloyd Pique09594832018-01-22 17:48:03 -0800225 int initialPowerMode)
Jesse Hallb7a05492014-08-14 15:45:06 -0700226 : lastCompositionHadVisibleLayers(false),
227 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800228 mType(type),
Dominik Laskowski7e045462018-05-30 13:02:02 -0700229 mId(id),
Chih-Wei Huang27e25622013-01-07 17:33:56 +0800230 mDisplayToken(displayToken),
Lloyd Pique09594832018-01-22 17:48:03 -0800231 mNativeWindow(nativeWindow),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700232 mDisplaySurface(displaySurface),
Lloyd Pique09594832018-01-22 17:48:03 -0800233 mSurface{std::move(renderSurface)},
234 mDisplayWidth(displayWidth),
235 mDisplayHeight(displayHeight),
236 mPageFlipCount(0),
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700237 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -0800238 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700239 mOrientation(),
Lloyd Pique09594832018-01-22 17:48:03 -0800240 mViewport(Rect::INVALID_RECT),
241 mFrame(Rect::INVALID_RECT),
242 mPowerMode(initialPowerMode),
243 mActiveConfig(0),
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800244 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800245 mHasWideColorGamut(hasWideColorGamut),
Peiyong Lin62665892018-04-16 11:07:44 -0700246 mHasHdr10(false),
247 mHasHLG(false),
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700248 mHasDolbyVision(false),
Chia-I Wube02ec02018-05-18 10:59:36 -0700249 mSupportedPerFrameMetadata(supportedPerFrameMetadata)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600251 // clang-format on
Chia-I Wube02ec02018-05-18 10:59:36 -0700252 populateColorModes(hwcColorModes);
253
Peiyong Linfb069302018-04-25 14:34:31 -0700254 std::vector<Hdr> types = hdrCapabilities.getSupportedHdrTypes();
255 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700256 switch (hdrType) {
257 case Hdr::HDR10:
258 mHasHdr10 = true;
259 break;
260 case Hdr::HLG:
261 mHasHLG = true;
262 break;
263 case Hdr::DOLBY_VISION:
264 mHasDolbyVision = true;
265 break;
266 default:
267 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
268 }
269 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700270
Peiyong Linfb069302018-04-25 14:34:31 -0700271 float minLuminance = hdrCapabilities.getDesiredMinLuminance();
272 float maxLuminance = hdrCapabilities.getDesiredMaxLuminance();
273 float maxAverageLuminance = hdrCapabilities.getDesiredMaxAverageLuminance();
274
275 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
276 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
277 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
278 if (this->hasWideColorGamut()) {
279 // insert HDR10/HLG as we will force client composition for HDR10/HLG
280 // layers
281 if (!hasHDR10Support()) {
282 types.push_back(Hdr::HDR10);
283 }
284
285 if (!hasHLGSupport()) {
286 types.push_back(Hdr::HLG);
287 }
288 }
289 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
290
Jesse Hallffe1f192013-03-22 15:13:48 -0700291 // initialize the display orientation transform.
292 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293}
294
Lloyd Pique09594832018-01-22 17:48:03 -0800295DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700296
Jesse Hall02d86562013-03-25 14:43:23 -0700297void DisplayDevice::disconnect(HWComposer& hwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700298 if (mId >= 0) {
299 hwc.disconnectDisplay(mId);
300 mId = -1;
Jesse Hall02d86562013-03-25 14:43:23 -0700301 }
302}
303
Mathias Agopian92a979a2012-08-02 18:32:23 -0700304bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800305 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306}
307
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700308int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700309 return mDisplayWidth;
310}
311
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700312int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700313 return mDisplayHeight;
314}
315
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700316void DisplayDevice::setDisplayName(const std::string& displayName) {
317 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700318 // never override the name with an empty name
319 mDisplayName = displayName;
320 }
321}
322
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700323uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325}
326
Chia-I Wub02087d2017-11-09 10:19:54 -0800327void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700329 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700330 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331}
332
Dan Stoza71433162014-02-04 16:22:36 -0800333status_t DisplayDevice::beginFrame(bool mustRecompose) const {
334 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700335}
336
Dan Stoza9e56aa02015-11-02 13:00:03 -0800337status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
338 status_t error = hwc.prepare(*this);
339 if (error != NO_ERROR) {
340 return error;
341 }
342
343 DisplaySurface::CompositionType compositionType;
Dominik Laskowski7e045462018-05-30 13:02:02 -0700344 bool hasClient = hwc.hasClientComposition(mId);
345 bool hasDevice = hwc.hasDeviceComposition(mId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800346 if (hasClient && hasDevice) {
347 compositionType = DisplaySurface::COMPOSITION_MIXED;
348 } else if (hasClient) {
349 compositionType = DisplaySurface::COMPOSITION_GLES;
350 } else if (hasDevice) {
351 compositionType = DisplaySurface::COMPOSITION_HWC;
352 } else {
353 // Nothing to do -- when turning the screen off we get a frame like
354 // this. Call it a HWC frame since we won't be doing any GLES work but
355 // will do a prepare/set cycle.
356 compositionType = DisplaySurface::COMPOSITION_HWC;
357 }
358 return mDisplaySurface->prepareFrame(compositionType);
359}
Jesse Hall38efe862013-04-06 23:12:29 -0700360
Mathias Agopianda27af92012-09-13 18:17:13 -0700361void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700362 if (hwc.hasClientComposition(mId) || hwc.hasFlipClientTargetRequest(mId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800363 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700364 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700365
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700366 status_t result = mDisplaySurface->advanceFrame();
367 if (result != NO_ERROR) {
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700368 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplayName.c_str(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700369 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700370}
371
Dan Stoza9e56aa02015-11-02 13:00:03 -0800372void DisplayDevice::onSwapBuffersCompleted() const {
373 mDisplaySurface->onFrameCommitted();
374}
Mathias Agopianda27af92012-09-13 18:17:13 -0700375
Chia-I Wuf846a352017-11-10 09:22:52 -0800376bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800377 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700378 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800379 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700380}
381
Mathias Agopian875d8e12013-06-07 15:35:48 -0700382void DisplayDevice::setViewportAndProjection() const {
383 size_t w = mDisplayWidth;
384 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700385 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700386 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
Peiyong Linefefaac2018-08-17 12:27:51 -0700387 false, ui::Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700388}
389
Dan Stoza9e56aa02015-11-02 13:00:03 -0800390const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
391 return mDisplaySurface->getClientTargetAcquireFence();
392}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800393
Mathias Agopian1b031492012-06-20 17:51:20 -0700394// ----------------------------------------------------------------------------
395
Mathias Agopian13127d82013-03-05 17:47:11 -0800396void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700397 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700398}
399
Mathias Agopian13127d82013-03-05 17:47:11 -0800400const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700401 return mVisibleLayersSortedByZ;
402}
403
Chia-I Wu83806892017-11-16 10:50:20 -0800404void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
405 mLayersNeedingFences = layers;
406}
407
408const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
409 return mLayersNeedingFences;
410}
411
Mathias Agopiancd60f992012-08-16 16:28:27 -0700412Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
413 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700414 if (repaintEverything) {
415 dirty.set(getBounds());
416 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700417 const ui::Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700418 dirty = planeTransform.transform(this->dirtyRegion);
419 dirty.andSelf(getBounds());
420 }
421 return dirty;
422}
423
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700424// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700425void DisplayDevice::setPowerMode(int mode) {
426 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700427}
428
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700429int DisplayDevice::getPowerMode() const {
430 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700431}
432
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700433bool DisplayDevice::isPoweredOn() const {
434 return mPowerMode != HWC_POWER_MODE_OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700435}
436
437// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700438void DisplayDevice::setActiveConfig(int mode) {
439 mActiveConfig = mode;
440}
441
442int DisplayDevice::getActiveConfig() const {
443 return mActiveConfig;
444}
445
446// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700447void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700448 mActiveColorMode = mode;
449}
450
Peiyong Lina52f0292018-03-14 17:26:31 -0700451ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700452 return mActiveColorMode;
453}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600454
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800455RenderIntent DisplayDevice::getActiveRenderIntent() const {
456 return mActiveRenderIntent;
457}
458
459void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
460 mActiveRenderIntent = renderIntent;
461}
462
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800463void DisplayDevice::setColorTransform(const mat4& transform) {
464 const bool isIdentity = (transform == mat4());
465 mColorTransform =
466 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
467}
468
469android_color_transform_t DisplayDevice::getColorTransform() const {
470 return mColorTransform;
471}
472
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700473void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800474 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600475 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700476 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600477}
Michael Wright28f24d02016-07-12 13:30:53 -0700478
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800479ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
480 return mCompositionDataSpace;
481}
482
Michael Wright28f24d02016-07-12 13:30:53 -0700483// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700484
Mathias Agopian28947d72012-08-08 18:51:15 -0700485void DisplayDevice::setLayerStack(uint32_t stack) {
486 mLayerStack = stack;
487 dirtyRegion.set(bounds());
488}
489
490// ----------------------------------------------------------------------------
491
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700492uint32_t DisplayDevice::getOrientationTransform() const {
493 uint32_t transform = 0;
494 switch (mOrientation) {
495 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700496 transform = ui::Transform::ROT_0;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700497 break;
498 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700499 transform = ui::Transform::ROT_90;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700500 break;
501 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700502 transform = ui::Transform::ROT_180;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700503 break;
504 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700505 transform = ui::Transform::ROT_270;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700506 break;
507 }
508 return transform;
509}
510
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700511status_t DisplayDevice::orientationToTransfrom(
Peiyong Linefefaac2018-08-17 12:27:51 -0700512 int orientation, int w, int h, ui::Transform* tr)
Mathias Agopian1b031492012-06-20 17:51:20 -0700513{
514 uint32_t flags = 0;
515 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700516 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700517 flags = ui::Transform::ROT_0;
Mathias Agopian1b031492012-06-20 17:51:20 -0700518 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700519 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700520 flags = ui::Transform::ROT_90;
Mathias Agopian1b031492012-06-20 17:51:20 -0700521 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700522 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700523 flags = ui::Transform::ROT_180;
Mathias Agopian1b031492012-06-20 17:51:20 -0700524 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700525 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700526 flags = ui::Transform::ROT_270;
Mathias Agopian1b031492012-06-20 17:51:20 -0700527 break;
528 default:
529 return BAD_VALUE;
530 }
531 tr->set(flags, w, h);
532 return NO_ERROR;
533}
534
Michael Lentine47e45402014-07-18 15:34:25 -0700535void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
536 dirtyRegion.set(getBounds());
537
Lloyd Pique144e1162017-12-20 16:44:52 -0800538 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700539
Michael Lentine47e45402014-07-18 15:34:25 -0700540 mDisplaySurface->resizeBuffers(newWidth, newHeight);
541
542 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800543 mSurface->setNativeWindow(window);
544 mDisplayWidth = mSurface->queryWidth();
545 mDisplayHeight = mSurface->queryHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700546
547 LOG_FATAL_IF(mDisplayWidth != newWidth,
548 "Unable to set new width to %d", newWidth);
549 LOG_FATAL_IF(mDisplayHeight != newHeight,
550 "Unable to set new height to %d", newHeight);
551}
552
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700553void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800554 const Rect& newViewport, const Rect& newFrame) {
555 Rect viewport(newViewport);
556 Rect frame(newFrame);
557
558 const int w = mDisplayWidth;
559 const int h = mDisplayHeight;
560
Peiyong Linefefaac2018-08-17 12:27:51 -0700561 ui::Transform R;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800562 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
563
564 if (!frame.isValid()) {
565 // the destination frame can be invalid if it has never been set,
566 // in that case we assume the whole display frame.
567 frame = Rect(w, h);
568 }
569
570 if (viewport.isEmpty()) {
571 // viewport can be invalid if it has never been set, in that case
572 // we assume the whole display size.
573 // it's also invalid to have an empty viewport, so we handle that
574 // case in the same way.
575 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700576 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800577 // viewport is always specified in the logical orientation
578 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700579 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800580 }
581 }
582
583 dirtyRegion.set(getBounds());
584
Peiyong Linefefaac2018-08-17 12:27:51 -0700585 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800586 float src_width = viewport.width();
587 float src_height = viewport.height();
588 float dst_width = frame.width();
589 float dst_height = frame.height();
590 if (src_width != dst_width || src_height != dst_height) {
591 float sx = dst_width / src_width;
592 float sy = dst_height / src_height;
593 S.set(sx, 0, 0, sy);
594 }
595
596 float src_x = viewport.left;
597 float src_y = viewport.top;
598 float dst_x = frame.left;
599 float dst_y = frame.top;
600 TL.set(-src_x, -src_y);
601 TP.set(dst_x, dst_y);
602
Iris Chang7501ed62018-04-30 14:45:42 +0800603 // need to take care of primary display rotation for mGlobalTransform
604 // for case if the panel is not installed aligned with device orientation
605 if (mType == DisplayType::DISPLAY_PRIMARY) {
606 int primaryDisplayOrientation = mFlinger->getPrimaryDisplayOrientation();
607 DisplayDevice::orientationToTransfrom(
608 (orientation + primaryDisplayOrientation) % (DisplayState::eOrientation270 + 1),
609 w, h, &R);
610 }
611
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800612 // The viewport and frame are both in the logical orientation.
613 // Apply the logical translation, scale to physical size, apply the
614 // physical translation and finally rotate to the physical orientation.
615 mGlobalTransform = R * TP * S * TL;
616
617 const uint8_t type = mGlobalTransform.getType();
618 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
Peiyong Linefefaac2018-08-17 12:27:51 -0700619 (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800620
621 mScissor = mGlobalTransform.transform(viewport);
622 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700623 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800624 }
625
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700626 mOrientation = orientation;
Dominik Laskowski281644e2018-04-19 15:47:35 -0700627 if (isPrimary()) {
Pablo Ceballos021623b2016-04-15 17:31:51 -0700628 uint32_t transform = 0;
629 switch (mOrientation) {
630 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700631 transform = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700632 break;
633 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700634 transform = ui::Transform::ROT_90;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700635 break;
636 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700637 transform = ui::Transform::ROT_180;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700638 break;
639 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700640 transform = ui::Transform::ROT_270;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700641 break;
642 }
643 sPrimaryDisplayOrientation = transform;
644 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700645 mViewport = viewport;
646 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700647}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700648
Pablo Ceballos021623b2016-04-15 17:31:51 -0700649uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
650 return sPrimaryDisplayOrientation;
651}
652
Mathias Agopian74d211a2013-04-22 16:55:35 +0200653void DisplayDevice::dump(String8& result) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700654 const ui::Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600655 ANativeWindow* const window = mNativeWindow.get();
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700656 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.c_str());
Dominik Laskowski7e045462018-05-30 13:02:02 -0700657 result.appendFormat(" type=%x, ID=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600658 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
659 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Dominik Laskowski7e045462018-05-30 13:02:02 -0700660 mType, mId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800661 mSurface->queryRedSize(), mSurface->queryGreenSize(),
662 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
663 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600664 mVisibleLayersSortedByZ.size());
665 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
666 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
667 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
668 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
669 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
670 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 -0600671 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700672 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800673 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
674 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800675 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700676 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700677
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700678 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800679 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700680 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700681}
Irvelffc9efc2016-07-27 15:16:37 -0700682
Chia-I Wube02ec02018-05-18 10:59:36 -0700683// Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
684// supported by HWC.
685void DisplayDevice::addColorMode(
686 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
687 const ColorMode mode, const RenderIntent intent) {
688 // find the best color mode
689 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
690
691 // find the best render intent
692 auto iter = hwcColorModes.find(hwcColorMode);
693 const auto& hwcIntents =
694 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
695 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
696
697 const Dataspace dataspace = colorModeToDataspace(mode);
698 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
699
Dominik Laskowski7e045462018-05-30 13:02:02 -0700700 ALOGV("DisplayDevice %d/%d: map (%s, %s) to (%s, %s, %s)", mType, mId,
Chia-I Wube02ec02018-05-18 10:59:36 -0700701 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
702 decodeRenderIntent(intent).c_str(),
703 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
704 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
705
706 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
707}
708
709void DisplayDevice::populateColorModes(
710 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) {
711 if (!hasWideColorGamut()) {
712 return;
713 }
714
Chia-I Wu0d711262018-05-21 15:19:35 -0700715 // collect all known SDR render intents
716 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
717 sSdrRenderIntents.end());
718 auto iter = hwcColorModes.find(ColorMode::SRGB);
719 if (iter != hwcColorModes.end()) {
720 for (auto intent : iter->second) {
721 sdrRenderIntents.insert(intent);
722 }
723 }
724
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700725 // add all known SDR combinations
Chia-I Wu0d711262018-05-21 15:19:35 -0700726 for (auto intent : sdrRenderIntents) {
Chia-I Wube02ec02018-05-18 10:59:36 -0700727 for (auto mode : sSdrColorModes) {
728 addColorMode(hwcColorModes, mode, intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700729 }
730 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700731
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700732 // collect all known HDR render intents
733 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
734 sHdrRenderIntents.end());
735 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
736 if (iter != hwcColorModes.end()) {
737 for (auto intent : iter->second) {
738 hdrRenderIntents.insert(intent);
739 }
740 }
741
742 // add all known HDR combinations
Chia-I Wube02ec02018-05-18 10:59:36 -0700743 for (auto intent : sHdrRenderIntents) {
744 for (auto mode : sHdrColorModes) {
745 addColorMode(hwcColorModes, mode, intent);
746 }
747 }
748}
749
750bool DisplayDevice::hasRenderIntent(RenderIntent intent) const {
751 // assume a render intent is supported when SRGB supports it; we should
752 // get rid of that assumption.
753 auto iter = mColorModes.find(getColorModeKey(Dataspace::SRGB, intent));
754 return iter != mColorModes.end() && iter->second.renderIntent == intent;
755}
756
Peiyong Lindfde5112018-06-05 10:58:41 -0700757bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const {
Chia-I Wube02ec02018-05-18 10:59:36 -0700758 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
759 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
760 auto iter =
761 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
Peiyong Lindfde5112018-06-05 10:58:41 -0700762 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
Chia-I Wube02ec02018-05-18 10:59:36 -0700763 }
764
765 return false;
766}
767
768void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent,
769 Dataspace* outDataspace, ColorMode* outMode,
770 RenderIntent* outIntent) const {
771 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
772 if (iter != mColorModes.end()) {
773 *outDataspace = iter->second.dataspace;
774 *outMode = iter->second.colorMode;
775 *outIntent = iter->second.renderIntent;
776 } else {
777 ALOGE("map unknown (%s)/(%s) to default color mode",
778 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
779 decodeRenderIntent(intent).c_str());
780
781 *outDataspace = Dataspace::UNKNOWN;
782 *outMode = ColorMode::NATIVE;
783 *outIntent = RenderIntent::COLORIMETRIC;
784 }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700785}
786
Dominik Laskowski663bd282018-04-19 15:26:54 -0700787std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700788
789} // namespace android