blob: 5342bcf0a561bbf51bbc9f1aa1099a667fa881ab [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),
Alec Mouri05874642018-11-14 22:34:02 +0000225 mSurface{std::move(args.renderSurface)},
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700226 mDisplayInstallOrientation(args.displayInstallOrientation),
227 mPageFlipCount(0),
Dominik Laskowski075d3172018-05-24 15:50:06 -0700228 mIsVirtual(args.isVirtual),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700229 mIsSecure(args.isSecure),
230 mLayerStack(NO_LAYER_STACK),
231 mOrientation(),
232 mViewport(Rect::INVALID_RECT),
233 mFrame(Rect::INVALID_RECT),
234 mPowerMode(args.initialPowerMode),
235 mActiveConfig(0),
236 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
237 mHasWideColorGamut(args.hasWideColorGamut),
238 mHasHdr10(false),
239 mHasHLG(false),
240 mHasDolbyVision(false),
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241 mSupportedPerFrameMetadata(args.supportedPerFrameMetadata),
242 mIsPrimary(args.isPrimary) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700243 populateColorModes(args.hwcColorModes);
244
245 ALOGE_IF(!mNativeWindow, "No native window was set for display");
246 ALOGE_IF(!mDisplaySurface, "No display surface was set for display");
Alec Mouri05874642018-11-14 22:34:02 +0000247 ALOGE_IF(!mSurface, "No render surface was set for display");
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700248
249 std::vector<Hdr> types = args.hdrCapabilities.getSupportedHdrTypes();
Peiyong Linfb069302018-04-25 14:34:31 -0700250 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700251 switch (hdrType) {
252 case Hdr::HDR10:
253 mHasHdr10 = true;
254 break;
255 case Hdr::HLG:
256 mHasHLG = true;
257 break;
258 case Hdr::DOLBY_VISION:
259 mHasDolbyVision = true;
260 break;
261 default:
262 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
263 }
264 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700265
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700266 float minLuminance = args.hdrCapabilities.getDesiredMinLuminance();
267 float maxLuminance = args.hdrCapabilities.getDesiredMaxLuminance();
268 float maxAverageLuminance = args.hdrCapabilities.getDesiredMaxAverageLuminance();
Peiyong Linfb069302018-04-25 14:34:31 -0700269
270 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
271 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
272 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
273 if (this->hasWideColorGamut()) {
274 // insert HDR10/HLG as we will force client composition for HDR10/HLG
275 // layers
276 if (!hasHDR10Support()) {
277 types.push_back(Hdr::HDR10);
278 }
279
280 if (!hasHLGSupport()) {
281 types.push_back(Hdr::HLG);
282 }
283 }
284 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
285
Alec Mouriba013fa2018-10-16 12:43:11 -0700286 ANativeWindow* const window = mNativeWindow.get();
287 mDisplayWidth = ANativeWindow_getWidth(window);
288 mDisplayHeight = ANativeWindow_getHeight(window);
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{
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000324 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
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000359void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700360 if (hwc.hasClientComposition(mId) || hwc.hasFlipClientTargetRequest(mId)) {
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000361 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
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000370void DisplayDevice::onSwapBuffersCompleted() const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800371 mDisplaySurface->onFrameCommitted();
372}
Mathias Agopianda27af92012-09-13 18:17:13 -0700373
Alec Mouri05874642018-11-14 22:34:02 +0000374bool DisplayDevice::makeCurrent() const {
375 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
376 setViewportAndProjection();
377 return success;
378}
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
Alec Mouri05874642018-11-14 22:34:02 +0000535 mSurface->setNativeWindow(nullptr);
536
Michael Lentine47e45402014-07-18 15:34:25 -0700537 mDisplaySurface->resizeBuffers(newWidth, newHeight);
538
Alec Mouri05874642018-11-14 22:34:02 +0000539 ANativeWindow* const window = mNativeWindow.get();
540 mSurface->setNativeWindow(window);
Alec Mouriba013fa2018-10-16 12:43:11 -0700541 mDisplayWidth = newWidth;
542 mDisplayHeight = newHeight;
Michael Lentine47e45402014-07-18 15:34:25 -0700543}
544
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700545void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800546 const Rect& newViewport, const Rect& newFrame) {
547 Rect viewport(newViewport);
548 Rect frame(newFrame);
549
550 const int w = mDisplayWidth;
551 const int h = mDisplayHeight;
552
Peiyong Linefefaac2018-08-17 12:27:51 -0700553 ui::Transform R;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800554 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
555
556 if (!frame.isValid()) {
557 // the destination frame can be invalid if it has never been set,
558 // in that case we assume the whole display frame.
559 frame = Rect(w, h);
560 }
561
562 if (viewport.isEmpty()) {
563 // viewport can be invalid if it has never been set, in that case
564 // we assume the whole display size.
565 // it's also invalid to have an empty viewport, so we handle that
566 // case in the same way.
567 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700568 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800569 // viewport is always specified in the logical orientation
570 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700571 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800572 }
573 }
574
575 dirtyRegion.set(getBounds());
576
Peiyong Linefefaac2018-08-17 12:27:51 -0700577 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800578 float src_width = viewport.width();
579 float src_height = viewport.height();
580 float dst_width = frame.width();
581 float dst_height = frame.height();
582 if (src_width != dst_width || src_height != dst_height) {
583 float sx = dst_width / src_width;
584 float sy = dst_height / src_height;
585 S.set(sx, 0, 0, sy);
586 }
587
588 float src_x = viewport.left;
589 float src_y = viewport.top;
590 float dst_x = frame.left;
591 float dst_y = frame.top;
592 TL.set(-src_x, -src_y);
593 TP.set(dst_x, dst_y);
594
Iris Chang7501ed62018-04-30 14:45:42 +0800595 // need to take care of primary display rotation for mGlobalTransform
596 // for case if the panel is not installed aligned with device orientation
Dominik Laskowski075d3172018-05-24 15:50:06 -0700597 if (isPrimary()) {
Iris Chang7501ed62018-04-30 14:45:42 +0800598 DisplayDevice::orientationToTransfrom(
Chia-I Wua02871c2018-08-27 14:38:23 -0700599 (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1),
Iris Chang7501ed62018-04-30 14:45:42 +0800600 w, h, &R);
601 }
602
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800603 // The viewport and frame are both in the logical orientation.
604 // Apply the logical translation, scale to physical size, apply the
605 // physical translation and finally rotate to the physical orientation.
606 mGlobalTransform = R * TP * S * TL;
607
608 const uint8_t type = mGlobalTransform.getType();
609 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
Peiyong Linefefaac2018-08-17 12:27:51 -0700610 (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800611
612 mScissor = mGlobalTransform.transform(viewport);
613 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700614 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800615 }
616
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700617 mOrientation = orientation;
Dominik Laskowski281644e2018-04-19 15:47:35 -0700618 if (isPrimary()) {
Pablo Ceballos021623b2016-04-15 17:31:51 -0700619 uint32_t transform = 0;
620 switch (mOrientation) {
621 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700622 transform = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700623 break;
624 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700625 transform = ui::Transform::ROT_90;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700626 break;
627 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700628 transform = ui::Transform::ROT_180;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700629 break;
630 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700631 transform = ui::Transform::ROT_270;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700632 break;
633 }
634 sPrimaryDisplayOrientation = transform;
635 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700636 mViewport = viewport;
637 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700638}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700639
Pablo Ceballos021623b2016-04-15 17:31:51 -0700640uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
641 return sPrimaryDisplayOrientation;
642}
643
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700644std::string DisplayDevice::getDebugName() const {
Dominik Laskowski34157762018-10-31 13:07:19 -0700645 const auto id = mId ? to_string(*mId) + ", " : std::string();
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700646 return base::StringPrintf("DisplayDevice{%s%s%s\"%s\"}", id.c_str(),
647 isPrimary() ? "primary, " : "", isVirtual() ? "virtual, " : "",
648 mDisplayName.c_str());
649}
650
Mathias Agopian74d211a2013-04-22 16:55:35 +0200651void DisplayDevice::dump(String8& result) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700652 const ui::Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600653 ANativeWindow* const window = mNativeWindow.get();
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700654 result.appendFormat("+ %s\n", getDebugName().c_str());
655 result.appendFormat(" layerStack=%u, (%4dx%4d), ANativeWindow=%p "
Alec Mouri05874642018-11-14 22:34:02 +0000656 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
657 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700658 mLayerStack, mDisplayWidth, mDisplayHeight, window,
Alec Mouri05874642018-11-14 22:34:02 +0000659 mSurface->queryRedSize(), mSurface->queryGreenSize(),
660 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
661 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600662 mVisibleLayersSortedByZ.size());
663 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
664 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
665 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
666 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
667 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
668 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 -0600669 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700670 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800671 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
Alec Mouri05874642018-11-14 22:34:02 +0000672 mHasWideColorGamut, mHasHdr10,
673 decodeColorMode(mActiveColorMode).c_str(),
674 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700675
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700676 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800677 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700678 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700679}
Irvelffc9efc2016-07-27 15:16:37 -0700680
Chia-I Wube02ec02018-05-18 10:59:36 -0700681// Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
682// supported by HWC.
683void DisplayDevice::addColorMode(
684 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
685 const ColorMode mode, const RenderIntent intent) {
686 // find the best color mode
687 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
688
689 // find the best render intent
690 auto iter = hwcColorModes.find(hwcColorMode);
691 const auto& hwcIntents =
692 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
693 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
694
695 const Dataspace dataspace = colorModeToDataspace(mode);
696 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
697
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700698 ALOGV("%s: map (%s, %s) to (%s, %s, %s)", getDebugName().c_str(),
Chia-I Wube02ec02018-05-18 10:59:36 -0700699 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
700 decodeRenderIntent(intent).c_str(),
701 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
702 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
703
704 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
705}
706
707void DisplayDevice::populateColorModes(
708 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) {
709 if (!hasWideColorGamut()) {
710 return;
711 }
712
Chia-I Wu0d711262018-05-21 15:19:35 -0700713 // collect all known SDR render intents
714 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
715 sSdrRenderIntents.end());
716 auto iter = hwcColorModes.find(ColorMode::SRGB);
717 if (iter != hwcColorModes.end()) {
718 for (auto intent : iter->second) {
719 sdrRenderIntents.insert(intent);
720 }
721 }
722
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700723 // add all known SDR combinations
Chia-I Wu0d711262018-05-21 15:19:35 -0700724 for (auto intent : sdrRenderIntents) {
Chia-I Wube02ec02018-05-18 10:59:36 -0700725 for (auto mode : sSdrColorModes) {
726 addColorMode(hwcColorModes, mode, intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700727 }
728 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700729
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700730 // collect all known HDR render intents
731 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
732 sHdrRenderIntents.end());
733 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
734 if (iter != hwcColorModes.end()) {
735 for (auto intent : iter->second) {
736 hdrRenderIntents.insert(intent);
737 }
738 }
739
740 // add all known HDR combinations
Chia-I Wube02ec02018-05-18 10:59:36 -0700741 for (auto intent : sHdrRenderIntents) {
742 for (auto mode : sHdrColorModes) {
743 addColorMode(hwcColorModes, mode, intent);
744 }
745 }
746}
747
748bool DisplayDevice::hasRenderIntent(RenderIntent intent) const {
749 // assume a render intent is supported when SRGB supports it; we should
750 // get rid of that assumption.
751 auto iter = mColorModes.find(getColorModeKey(Dataspace::SRGB, intent));
752 return iter != mColorModes.end() && iter->second.renderIntent == intent;
753}
754
Peiyong Lindfde5112018-06-05 10:58:41 -0700755bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const {
Chia-I Wube02ec02018-05-18 10:59:36 -0700756 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
757 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
758 auto iter =
759 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
Peiyong Lindfde5112018-06-05 10:58:41 -0700760 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
Chia-I Wube02ec02018-05-18 10:59:36 -0700761 }
762
763 return false;
764}
765
766void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent,
767 Dataspace* outDataspace, ColorMode* outMode,
768 RenderIntent* outIntent) const {
769 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
770 if (iter != mColorModes.end()) {
771 *outDataspace = iter->second.dataspace;
772 *outMode = iter->second.colorMode;
773 *outIntent = iter->second.renderIntent;
774 } else {
Chia-I Wu6333af52018-10-16 13:46:36 -0700775 // this is unexpected on a WCG display
776 if (hasWideColorGamut()) {
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 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700781
782 *outDataspace = Dataspace::UNKNOWN;
783 *outMode = ColorMode::NATIVE;
784 *outIntent = RenderIntent::COLORIMETRIC;
785 }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700786}
787
Dominik Laskowski663bd282018-04-19 15:26:54 -0700788std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700789
790} // namespace android