blob: 071f07dc1321dd9d6a942b9011da04f479e91ddf [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017// #define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "DisplayDevice"
20
Peiyong Lincbc184f2018-08-22 13:24:10 -070021#include "DisplayDevice.h"
22
Chia-I Wube02ec02018-05-18 10:59:36 -070023#include <array>
24#include <unordered_set>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <math.h>
30
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -070031#include <android-base/stringprintf.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070032#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
33#include <configstore/Utils.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <cutils/properties.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070035#include <gui/Surface.h>
36#include <hardware/gralloc.h>
37#include <renderengine/RenderEngine.h>
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -060038#include <ui/DebugUtils.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070039#include <ui/DisplayInfo.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040#include <ui/PixelFormat.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070041#include <utils/Log.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070042#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
Jesse Hall99c7dbb2013-03-14 14:29:29 -070044#include "DisplayHardware/DisplaySurface.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070045#include "DisplayHardware/HWComposer.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080046#include "DisplayHardware/HWC2.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070047#include "SurfaceFlinger.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080048#include "Layer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070049
Peiyong Linfd997e02018-03-28 15:29:00 -070050namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Jaesoo Lee720a7242017-01-31 15:26:18 +090052// retrieve triple buffer setting from configstore
53using namespace android::hardware::configstore;
54using namespace android::hardware::configstore::V1_0;
Peiyong Linfd997e02018-03-28 15:29:00 -070055using android::ui::ColorMode;
Chia-I Wube02ec02018-05-18 10:59:36 -070056using android::ui::Dataspace;
Peiyong Lin62665892018-04-16 11:07:44 -070057using android::ui::Hdr;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -080058using android::ui::RenderIntent;
Jaesoo Lee720a7242017-01-31 15:26:18 +090059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060/*
61 * Initialize the display to the specified values.
62 *
63 */
64
Pablo Ceballos021623b2016-04-15 17:31:51 -070065uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
66
Chia-I Wube02ec02018-05-18 10:59:36 -070067namespace {
68
69// ordered list of known SDR color modes
Valerie Hau9758ae02018-10-09 16:05:09 -070070const std::array<ColorMode, 3> sSdrColorModes = {
71 ColorMode::DISPLAY_BT2020,
Chia-I Wube02ec02018-05-18 10:59:36 -070072 ColorMode::DISPLAY_P3,
73 ColorMode::SRGB,
74};
75
76// ordered list of known HDR color modes
77const std::array<ColorMode, 2> sHdrColorModes = {
78 ColorMode::BT2100_PQ,
79 ColorMode::BT2100_HLG,
80};
81
82// ordered list of known SDR render intents
83const std::array<RenderIntent, 2> sSdrRenderIntents = {
84 RenderIntent::ENHANCE,
85 RenderIntent::COLORIMETRIC,
86};
87
88// ordered list of known HDR render intents
89const std::array<RenderIntent, 2> sHdrRenderIntents = {
90 RenderIntent::TONE_MAP_ENHANCE,
91 RenderIntent::TONE_MAP_COLORIMETRIC,
92};
93
94// map known color mode to dataspace
95Dataspace colorModeToDataspace(ColorMode mode) {
96 switch (mode) {
97 case ColorMode::SRGB:
98 return Dataspace::SRGB;
99 case ColorMode::DISPLAY_P3:
100 return Dataspace::DISPLAY_P3;
Valerie Hau9758ae02018-10-09 16:05:09 -0700101 case ColorMode::DISPLAY_BT2020:
102 return Dataspace::DISPLAY_BT2020;
Chia-I Wube02ec02018-05-18 10:59:36 -0700103 case ColorMode::BT2100_HLG:
104 return Dataspace::BT2020_HLG;
105 case ColorMode::BT2100_PQ:
106 return Dataspace::BT2020_PQ;
107 default:
108 return Dataspace::UNKNOWN;
109 }
110}
111
112// Return a list of candidate color modes.
113std::vector<ColorMode> getColorModeCandidates(ColorMode mode) {
114 std::vector<ColorMode> candidates;
115
116 // add mode itself
117 candidates.push_back(mode);
118
119 // check if mode is HDR
120 bool isHdr = false;
121 for (auto hdrMode : sHdrColorModes) {
122 if (hdrMode == mode) {
123 isHdr = true;
124 break;
125 }
126 }
127
128 // add other HDR candidates when mode is HDR
129 if (isHdr) {
130 for (auto hdrMode : sHdrColorModes) {
131 if (hdrMode != mode) {
132 candidates.push_back(hdrMode);
133 }
134 }
135 }
136
137 // add other SDR candidates
138 for (auto sdrMode : sSdrColorModes) {
139 if (sdrMode != mode) {
140 candidates.push_back(sdrMode);
141 }
142 }
143
144 return candidates;
145}
146
147// Return a list of candidate render intents.
148std::vector<RenderIntent> getRenderIntentCandidates(RenderIntent intent) {
149 std::vector<RenderIntent> candidates;
150
151 // add intent itself
152 candidates.push_back(intent);
153
154 // check if intent is HDR
155 bool isHdr = false;
156 for (auto hdrIntent : sHdrRenderIntents) {
157 if (hdrIntent == intent) {
158 isHdr = true;
159 break;
160 }
161 }
162
Chia-I Wube02ec02018-05-18 10:59:36 -0700163 if (isHdr) {
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700164 // add other HDR candidates when intent is HDR
Chia-I Wube02ec02018-05-18 10:59:36 -0700165 for (auto hdrIntent : sHdrRenderIntents) {
166 if (hdrIntent != intent) {
167 candidates.push_back(hdrIntent);
168 }
169 }
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700170 } else {
171 // add other SDR candidates when intent is SDR
172 for (auto sdrIntent : sSdrRenderIntents) {
173 if (sdrIntent != intent) {
174 candidates.push_back(sdrIntent);
175 }
176 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700177 }
178
179 return candidates;
180}
181
182// Return the best color mode supported by HWC.
183ColorMode getHwcColorMode(
184 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
185 ColorMode mode) {
186 std::vector<ColorMode> candidates = getColorModeCandidates(mode);
187 for (auto candidate : candidates) {
188 auto iter = hwcColorModes.find(candidate);
189 if (iter != hwcColorModes.end()) {
190 return candidate;
191 }
192 }
193
194 return ColorMode::NATIVE;
195}
196
197// Return the best render intent supported by HWC.
198RenderIntent getHwcRenderIntent(const std::vector<RenderIntent>& hwcIntents, RenderIntent intent) {
199 std::vector<RenderIntent> candidates = getRenderIntentCandidates(intent);
200 for (auto candidate : candidates) {
201 for (auto hwcIntent : hwcIntents) {
202 if (candidate == hwcIntent) {
203 return candidate;
204 }
205 }
206 }
207
208 return RenderIntent::COLORIMETRIC;
209}
210
211} // anonymous namespace
212
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700213DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger,
214 const wp<IBinder>& displayToken,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700215 const std::optional<DisplayId>& displayId)
216 : flinger(flinger), displayToken(displayToken), displayId(displayId) {}
Chia-I Wube02ec02018-05-18 10:59:36 -0700217
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700218DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs&& args)
219 : lastCompositionHadVisibleLayers(false),
220 mFlinger(args.flinger),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700221 mDisplayToken(args.displayToken),
Dominik Laskowski075d3172018-05-24 15:50:06 -0700222 mId(args.displayId),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700223 mNativeWindow(args.nativeWindow),
224 mDisplaySurface(args.displaySurface),
225 mSurface{std::move(args.renderSurface)},
226 mDisplayWidth(args.displayWidth),
227 mDisplayHeight(args.displayHeight),
228 mDisplayInstallOrientation(args.displayInstallOrientation),
229 mPageFlipCount(0),
Dominik Laskowski075d3172018-05-24 15:50:06 -0700230 mIsVirtual(args.isVirtual),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700231 mIsSecure(args.isSecure),
232 mLayerStack(NO_LAYER_STACK),
233 mOrientation(),
234 mViewport(Rect::INVALID_RECT),
235 mFrame(Rect::INVALID_RECT),
236 mPowerMode(args.initialPowerMode),
237 mActiveConfig(0),
238 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
239 mHasWideColorGamut(args.hasWideColorGamut),
240 mHasHdr10(false),
241 mHasHLG(false),
242 mHasDolbyVision(false),
Dominik Laskowski075d3172018-05-24 15:50:06 -0700243 mSupportedPerFrameMetadata(args.supportedPerFrameMetadata),
244 mIsPrimary(args.isPrimary) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700245 populateColorModes(args.hwcColorModes);
246
247 ALOGE_IF(!mNativeWindow, "No native window was set for display");
248 ALOGE_IF(!mDisplaySurface, "No display surface was set for display");
249 ALOGE_IF(!mSurface, "No render surface was set for display");
250 ALOGE_IF(mDisplayWidth <= 0 || mDisplayHeight <= 0,
251 "Invalid dimensions of %d x %d were set for display", mDisplayWidth, mDisplayHeight);
252
253 std::vector<Hdr> types = args.hdrCapabilities.getSupportedHdrTypes();
Peiyong Linfb069302018-04-25 14:34:31 -0700254 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700255 switch (hdrType) {
256 case Hdr::HDR10:
257 mHasHdr10 = true;
258 break;
259 case Hdr::HLG:
260 mHasHLG = true;
261 break;
262 case Hdr::DOLBY_VISION:
263 mHasDolbyVision = true;
264 break;
265 default:
266 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
267 }
268 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700269
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700270 float minLuminance = args.hdrCapabilities.getDesiredMinLuminance();
271 float maxLuminance = args.hdrCapabilities.getDesiredMaxLuminance();
272 float maxAverageLuminance = args.hdrCapabilities.getDesiredMaxAverageLuminance();
Peiyong Linfb069302018-04-25 14:34:31 -0700273
274 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
275 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
276 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
277 if (this->hasWideColorGamut()) {
278 // insert HDR10/HLG as we will force client composition for HDR10/HLG
279 // layers
280 if (!hasHDR10Support()) {
281 types.push_back(Hdr::HDR10);
282 }
283
284 if (!hasHLGSupport()) {
285 types.push_back(Hdr::HLG);
286 }
287 }
288 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
289
Jesse Hallffe1f192013-03-22 15:13:48 -0700290 // initialize the display orientation transform.
291 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292}
293
Lloyd Pique09594832018-01-22 17:48:03 -0800294DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700295
Jesse Hall02d86562013-03-25 14:43:23 -0700296void DisplayDevice::disconnect(HWComposer& hwc) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700297 if (mId) {
298 hwc.disconnectDisplay(*mId);
299 mId.reset();
Jesse Hall02d86562013-03-25 14:43:23 -0700300 }
301}
302
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700303int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700304 return mDisplayWidth;
305}
306
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700307int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700308 return mDisplayHeight;
309}
310
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700311void DisplayDevice::setDisplayName(const std::string& displayName) {
312 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700313 // never override the name with an empty name
314 mDisplayName = displayName;
315 }
316}
317
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700318uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700319 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320}
321
Chia-I Wub02087d2017-11-09 10:19:54 -0800322void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700324 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700325 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326}
327
Dan Stoza71433162014-02-04 16:22:36 -0800328status_t DisplayDevice::beginFrame(bool mustRecompose) const {
329 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700330}
331
David Sodmanfb95bcc2017-12-22 15:45:30 -0800332status_t DisplayDevice::prepareFrame(HWComposer& hwc,
333 std::vector<CompositionInfo>& compositionData) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700334 if (mId) {
335 status_t error = hwc.prepare(*mId, compositionData);
336 if (error != NO_ERROR) {
337 return error;
338 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800339 }
340
341 DisplaySurface::CompositionType compositionType;
Dominik Laskowski7e045462018-05-30 13:02:02 -0700342 bool hasClient = hwc.hasClientComposition(mId);
343 bool hasDevice = hwc.hasDeviceComposition(mId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800344 if (hasClient && hasDevice) {
345 compositionType = DisplaySurface::COMPOSITION_MIXED;
346 } else if (hasClient) {
347 compositionType = DisplaySurface::COMPOSITION_GLES;
348 } else if (hasDevice) {
349 compositionType = DisplaySurface::COMPOSITION_HWC;
350 } else {
351 // Nothing to do -- when turning the screen off we get a frame like
352 // this. Call it a HWC frame since we won't be doing any GLES work but
353 // will do a prepare/set cycle.
354 compositionType = DisplaySurface::COMPOSITION_HWC;
355 }
356 return mDisplaySurface->prepareFrame(compositionType);
357}
Jesse Hall38efe862013-04-06 23:12:29 -0700358
Mathias Agopianda27af92012-09-13 18:17:13 -0700359void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700360 if (hwc.hasClientComposition(mId) || hwc.hasFlipClientTargetRequest(mId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800361 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700362 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700363
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700364 status_t result = mDisplaySurface->advanceFrame();
365 if (result != NO_ERROR) {
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700366 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplayName.c_str(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700367 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700368}
369
Dan Stoza9e56aa02015-11-02 13:00:03 -0800370void DisplayDevice::onSwapBuffersCompleted() const {
371 mDisplaySurface->onFrameCommitted();
372}
Mathias Agopianda27af92012-09-13 18:17:13 -0700373
Chia-I Wuf846a352017-11-10 09:22:52 -0800374bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800375 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700376 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800377 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700378}
379
Mathias Agopian875d8e12013-06-07 15:35:48 -0700380void DisplayDevice::setViewportAndProjection() const {
381 size_t w = mDisplayWidth;
382 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700383 Rect sourceCrop(0, 0, w, h);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700384 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, ui::Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700385}
386
Dan Stoza9e56aa02015-11-02 13:00:03 -0800387const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
388 return mDisplaySurface->getClientTargetAcquireFence();
389}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800390
Mathias Agopian1b031492012-06-20 17:51:20 -0700391// ----------------------------------------------------------------------------
392
Mathias Agopian13127d82013-03-05 17:47:11 -0800393void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700394 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700395}
396
Mathias Agopian13127d82013-03-05 17:47:11 -0800397const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700398 return mVisibleLayersSortedByZ;
399}
400
Chia-I Wu83806892017-11-16 10:50:20 -0800401void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
402 mLayersNeedingFences = layers;
403}
404
405const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
406 return mLayersNeedingFences;
407}
408
Mathias Agopiancd60f992012-08-16 16:28:27 -0700409Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
410 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700411 if (repaintEverything) {
412 dirty.set(getBounds());
413 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700414 const ui::Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700415 dirty = planeTransform.transform(this->dirtyRegion);
416 dirty.andSelf(getBounds());
417 }
418 return dirty;
419}
420
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700421// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700422void DisplayDevice::setPowerMode(int mode) {
423 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700424}
425
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700426int DisplayDevice::getPowerMode() const {
427 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700428}
429
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700430bool DisplayDevice::isPoweredOn() const {
431 return mPowerMode != HWC_POWER_MODE_OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700432}
433
434// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700435void DisplayDevice::setActiveConfig(int mode) {
436 mActiveConfig = mode;
437}
438
439int DisplayDevice::getActiveConfig() const {
440 return mActiveConfig;
441}
442
443// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700444void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700445 mActiveColorMode = mode;
446}
447
Peiyong Lina52f0292018-03-14 17:26:31 -0700448ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700449 return mActiveColorMode;
450}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600451
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800452RenderIntent DisplayDevice::getActiveRenderIntent() const {
453 return mActiveRenderIntent;
454}
455
456void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
457 mActiveRenderIntent = renderIntent;
458}
459
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800460void DisplayDevice::setColorTransform(const mat4& transform) {
461 const bool isIdentity = (transform == mat4());
462 mColorTransform =
463 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
464}
465
466android_color_transform_t DisplayDevice::getColorTransform() const {
467 return mColorTransform;
468}
469
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700470void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800471 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600472 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700473 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600474}
Michael Wright28f24d02016-07-12 13:30:53 -0700475
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800476ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
477 return mCompositionDataSpace;
478}
479
Michael Wright28f24d02016-07-12 13:30:53 -0700480// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700481
Mathias Agopian28947d72012-08-08 18:51:15 -0700482void DisplayDevice::setLayerStack(uint32_t stack) {
483 mLayerStack = stack;
484 dirtyRegion.set(bounds());
485}
486
487// ----------------------------------------------------------------------------
488
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700489uint32_t DisplayDevice::getOrientationTransform() const {
490 uint32_t transform = 0;
491 switch (mOrientation) {
492 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700493 transform = ui::Transform::ROT_0;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700494 break;
495 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700496 transform = ui::Transform::ROT_90;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700497 break;
498 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700499 transform = ui::Transform::ROT_180;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700500 break;
501 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700502 transform = ui::Transform::ROT_270;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700503 break;
504 }
505 return transform;
506}
507
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700508status_t DisplayDevice::orientationToTransfrom(
Peiyong Linefefaac2018-08-17 12:27:51 -0700509 int orientation, int w, int h, ui::Transform* tr)
Mathias Agopian1b031492012-06-20 17:51:20 -0700510{
511 uint32_t flags = 0;
512 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700513 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700514 flags = ui::Transform::ROT_0;
Mathias Agopian1b031492012-06-20 17:51:20 -0700515 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700516 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700517 flags = ui::Transform::ROT_90;
Mathias Agopian1b031492012-06-20 17:51:20 -0700518 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700519 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700520 flags = ui::Transform::ROT_180;
Mathias Agopian1b031492012-06-20 17:51:20 -0700521 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700522 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700523 flags = ui::Transform::ROT_270;
Mathias Agopian1b031492012-06-20 17:51:20 -0700524 break;
525 default:
526 return BAD_VALUE;
527 }
528 tr->set(flags, w, h);
529 return NO_ERROR;
530}
531
Michael Lentine47e45402014-07-18 15:34:25 -0700532void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
533 dirtyRegion.set(getBounds());
534
Lloyd Pique144e1162017-12-20 16:44:52 -0800535 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700536
Michael Lentine47e45402014-07-18 15:34:25 -0700537 mDisplaySurface->resizeBuffers(newWidth, newHeight);
538
539 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800540 mSurface->setNativeWindow(window);
Alec Mouri05483a02018-09-10 21:03:42 +0000541 mDisplayWidth = mSurface->getWidth();
542 mDisplayHeight = mSurface->getHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700543
544 LOG_FATAL_IF(mDisplayWidth != newWidth,
545 "Unable to set new width to %d", newWidth);
546 LOG_FATAL_IF(mDisplayHeight != newHeight,
547 "Unable to set new height to %d", newHeight);
548}
549
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700550void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800551 const Rect& newViewport, const Rect& newFrame) {
552 Rect viewport(newViewport);
553 Rect frame(newFrame);
554
555 const int w = mDisplayWidth;
556 const int h = mDisplayHeight;
557
Peiyong Linefefaac2018-08-17 12:27:51 -0700558 ui::Transform R;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800559 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
560
561 if (!frame.isValid()) {
562 // the destination frame can be invalid if it has never been set,
563 // in that case we assume the whole display frame.
564 frame = Rect(w, h);
565 }
566
567 if (viewport.isEmpty()) {
568 // viewport can be invalid if it has never been set, in that case
569 // we assume the whole display size.
570 // it's also invalid to have an empty viewport, so we handle that
571 // case in the same way.
572 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700573 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800574 // viewport is always specified in the logical orientation
575 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700576 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800577 }
578 }
579
580 dirtyRegion.set(getBounds());
581
Peiyong Linefefaac2018-08-17 12:27:51 -0700582 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800583 float src_width = viewport.width();
584 float src_height = viewport.height();
585 float dst_width = frame.width();
586 float dst_height = frame.height();
587 if (src_width != dst_width || src_height != dst_height) {
588 float sx = dst_width / src_width;
589 float sy = dst_height / src_height;
590 S.set(sx, 0, 0, sy);
591 }
592
593 float src_x = viewport.left;
594 float src_y = viewport.top;
595 float dst_x = frame.left;
596 float dst_y = frame.top;
597 TL.set(-src_x, -src_y);
598 TP.set(dst_x, dst_y);
599
Iris Chang7501ed62018-04-30 14:45:42 +0800600 // need to take care of primary display rotation for mGlobalTransform
601 // for case if the panel is not installed aligned with device orientation
Dominik Laskowski075d3172018-05-24 15:50:06 -0700602 if (isPrimary()) {
Iris Chang7501ed62018-04-30 14:45:42 +0800603 DisplayDevice::orientationToTransfrom(
Chia-I Wua02871c2018-08-27 14:38:23 -0700604 (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1),
Iris Chang7501ed62018-04-30 14:45:42 +0800605 w, h, &R);
606 }
607
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800608 // The viewport and frame are both in the logical orientation.
609 // Apply the logical translation, scale to physical size, apply the
610 // physical translation and finally rotate to the physical orientation.
611 mGlobalTransform = R * TP * S * TL;
612
613 const uint8_t type = mGlobalTransform.getType();
614 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
Peiyong Linefefaac2018-08-17 12:27:51 -0700615 (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800616
617 mScissor = mGlobalTransform.transform(viewport);
618 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700619 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800620 }
621
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700622 mOrientation = orientation;
Dominik Laskowski281644e2018-04-19 15:47:35 -0700623 if (isPrimary()) {
Pablo Ceballos021623b2016-04-15 17:31:51 -0700624 uint32_t transform = 0;
625 switch (mOrientation) {
626 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700627 transform = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700628 break;
629 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700630 transform = ui::Transform::ROT_90;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700631 break;
632 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700633 transform = ui::Transform::ROT_180;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700634 break;
635 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700636 transform = ui::Transform::ROT_270;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700637 break;
638 }
639 sPrimaryDisplayOrientation = transform;
640 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700641 mViewport = viewport;
642 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700643}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700644
Pablo Ceballos021623b2016-04-15 17:31:51 -0700645uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
646 return sPrimaryDisplayOrientation;
647}
648
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700649std::string DisplayDevice::getDebugName() const {
Dominik Laskowski34157762018-10-31 13:07:19 -0700650 const auto id = mId ? to_string(*mId) + ", " : std::string();
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700651 return base::StringPrintf("DisplayDevice{%s%s%s\"%s\"}", id.c_str(),
652 isPrimary() ? "primary, " : "", isVirtual() ? "virtual, " : "",
653 mDisplayName.c_str());
654}
655
Mathias Agopian74d211a2013-04-22 16:55:35 +0200656void DisplayDevice::dump(String8& result) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700657 const ui::Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600658 ANativeWindow* const window = mNativeWindow.get();
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700659 result.appendFormat("+ %s\n", getDebugName().c_str());
660 result.appendFormat(" layerStack=%u, (%4dx%4d), ANativeWindow=%p "
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600661 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
662 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700663 mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800664 mSurface->queryRedSize(), mSurface->queryGreenSize(),
665 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
666 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600667 mVisibleLayersSortedByZ.size());
668 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
669 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
670 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
671 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
672 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
673 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 -0600674 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700675 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800676 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
677 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800678 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700679 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700680
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700681 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800682 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700683 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700684}
Irvelffc9efc2016-07-27 15:16:37 -0700685
Chia-I Wube02ec02018-05-18 10:59:36 -0700686// Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
687// supported by HWC.
688void DisplayDevice::addColorMode(
689 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
690 const ColorMode mode, const RenderIntent intent) {
691 // find the best color mode
692 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
693
694 // find the best render intent
695 auto iter = hwcColorModes.find(hwcColorMode);
696 const auto& hwcIntents =
697 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
698 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
699
700 const Dataspace dataspace = colorModeToDataspace(mode);
701 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
702
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700703 ALOGV("%s: map (%s, %s) to (%s, %s, %s)", getDebugName().c_str(),
Chia-I Wube02ec02018-05-18 10:59:36 -0700704 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
705 decodeRenderIntent(intent).c_str(),
706 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
707 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
708
709 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
710}
711
712void DisplayDevice::populateColorModes(
713 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) {
714 if (!hasWideColorGamut()) {
715 return;
716 }
717
Chia-I Wu0d711262018-05-21 15:19:35 -0700718 // collect all known SDR render intents
719 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
720 sSdrRenderIntents.end());
721 auto iter = hwcColorModes.find(ColorMode::SRGB);
722 if (iter != hwcColorModes.end()) {
723 for (auto intent : iter->second) {
724 sdrRenderIntents.insert(intent);
725 }
726 }
727
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700728 // add all known SDR combinations
Chia-I Wu0d711262018-05-21 15:19:35 -0700729 for (auto intent : sdrRenderIntents) {
Chia-I Wube02ec02018-05-18 10:59:36 -0700730 for (auto mode : sSdrColorModes) {
731 addColorMode(hwcColorModes, mode, intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700732 }
733 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700734
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700735 // collect all known HDR render intents
736 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
737 sHdrRenderIntents.end());
738 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
739 if (iter != hwcColorModes.end()) {
740 for (auto intent : iter->second) {
741 hdrRenderIntents.insert(intent);
742 }
743 }
744
745 // add all known HDR combinations
Chia-I Wube02ec02018-05-18 10:59:36 -0700746 for (auto intent : sHdrRenderIntents) {
747 for (auto mode : sHdrColorModes) {
748 addColorMode(hwcColorModes, mode, intent);
749 }
750 }
751}
752
753bool DisplayDevice::hasRenderIntent(RenderIntent intent) const {
754 // assume a render intent is supported when SRGB supports it; we should
755 // get rid of that assumption.
756 auto iter = mColorModes.find(getColorModeKey(Dataspace::SRGB, intent));
757 return iter != mColorModes.end() && iter->second.renderIntent == intent;
758}
759
Peiyong Lindfde5112018-06-05 10:58:41 -0700760bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const {
Chia-I Wube02ec02018-05-18 10:59:36 -0700761 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
762 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
763 auto iter =
764 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
Peiyong Lindfde5112018-06-05 10:58:41 -0700765 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
Chia-I Wube02ec02018-05-18 10:59:36 -0700766 }
767
768 return false;
769}
770
771void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent,
772 Dataspace* outDataspace, ColorMode* outMode,
773 RenderIntent* outIntent) const {
774 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
775 if (iter != mColorModes.end()) {
776 *outDataspace = iter->second.dataspace;
777 *outMode = iter->second.colorMode;
778 *outIntent = iter->second.renderIntent;
779 } else {
Chia-I Wu6333af52018-10-16 13:46:36 -0700780 // this is unexpected on a WCG display
781 if (hasWideColorGamut()) {
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 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700786
787 *outDataspace = Dataspace::UNKNOWN;
788 *outMode = ColorMode::NATIVE;
789 *outIntent = RenderIntent::COLORIMETRIC;
790 }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700791}
792
Dominik Laskowski663bd282018-04-19 15:26:54 -0700793std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700794
795} // namespace android