blob: 341dfd53de97785766a16ee0f1dfec5c7ccee407 [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
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700209DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger,
210 const wp<IBinder>& displayToken,
211 DisplayDevice::DisplayType type, int32_t id)
212 : flinger(flinger), displayToken(displayToken), type(type), id(id) {}
Chia-I Wube02ec02018-05-18 10:59:36 -0700213
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700214DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs&& args)
215 : lastCompositionHadVisibleLayers(false),
216 mFlinger(args.flinger),
217 mType(args.type),
218 mId(args.id),
219 mDisplayToken(args.displayToken),
220 mNativeWindow(args.nativeWindow),
221 mDisplaySurface(args.displaySurface),
222 mSurface{std::move(args.renderSurface)},
223 mDisplayWidth(args.displayWidth),
224 mDisplayHeight(args.displayHeight),
225 mDisplayInstallOrientation(args.displayInstallOrientation),
226 mPageFlipCount(0),
227 mIsSecure(args.isSecure),
228 mLayerStack(NO_LAYER_STACK),
229 mOrientation(),
230 mViewport(Rect::INVALID_RECT),
231 mFrame(Rect::INVALID_RECT),
232 mPowerMode(args.initialPowerMode),
233 mActiveConfig(0),
234 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
235 mHasWideColorGamut(args.hasWideColorGamut),
236 mHasHdr10(false),
237 mHasHLG(false),
238 mHasDolbyVision(false),
239 mSupportedPerFrameMetadata(args.supportedPerFrameMetadata) {
240 populateColorModes(args.hwcColorModes);
241
242 ALOGE_IF(!mNativeWindow, "No native window was set for display");
243 ALOGE_IF(!mDisplaySurface, "No display surface was set for display");
244 ALOGE_IF(!mSurface, "No render surface was set for display");
245 ALOGE_IF(mDisplayWidth <= 0 || mDisplayHeight <= 0,
246 "Invalid dimensions of %d x %d were set for display", mDisplayWidth, mDisplayHeight);
247
248 std::vector<Hdr> types = args.hdrCapabilities.getSupportedHdrTypes();
Peiyong Linfb069302018-04-25 14:34:31 -0700249 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700250 switch (hdrType) {
251 case Hdr::HDR10:
252 mHasHdr10 = true;
253 break;
254 case Hdr::HLG:
255 mHasHLG = true;
256 break;
257 case Hdr::DOLBY_VISION:
258 mHasDolbyVision = true;
259 break;
260 default:
261 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
262 }
263 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700264
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700265 float minLuminance = args.hdrCapabilities.getDesiredMinLuminance();
266 float maxLuminance = args.hdrCapabilities.getDesiredMaxLuminance();
267 float maxAverageLuminance = args.hdrCapabilities.getDesiredMaxAverageLuminance();
Peiyong Linfb069302018-04-25 14:34:31 -0700268
269 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
270 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
271 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
272 if (this->hasWideColorGamut()) {
273 // insert HDR10/HLG as we will force client composition for HDR10/HLG
274 // layers
275 if (!hasHDR10Support()) {
276 types.push_back(Hdr::HDR10);
277 }
278
279 if (!hasHLGSupport()) {
280 types.push_back(Hdr::HLG);
281 }
282 }
283 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
284
Jesse Hallffe1f192013-03-22 15:13:48 -0700285 // initialize the display orientation transform.
286 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287}
288
Lloyd Pique09594832018-01-22 17:48:03 -0800289DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700290
Jesse Hall02d86562013-03-25 14:43:23 -0700291void DisplayDevice::disconnect(HWComposer& hwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700292 if (mId >= 0) {
293 hwc.disconnectDisplay(mId);
294 mId = -1;
Jesse Hall02d86562013-03-25 14:43:23 -0700295 }
296}
297
Mathias Agopian92a979a2012-08-02 18:32:23 -0700298bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800299 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300}
301
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700302int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700303 return mDisplayWidth;
304}
305
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700306int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700307 return mDisplayHeight;
308}
309
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700310void DisplayDevice::setDisplayName(const std::string& displayName) {
311 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700312 // never override the name with an empty name
313 mDisplayName = displayName;
314 }
315}
316
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700317uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700318 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319}
320
Chia-I Wub02087d2017-11-09 10:19:54 -0800321void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700323 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325}
326
Dan Stoza71433162014-02-04 16:22:36 -0800327status_t DisplayDevice::beginFrame(bool mustRecompose) const {
328 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700329}
330
David Sodmanfb95bcc2017-12-22 15:45:30 -0800331status_t DisplayDevice::prepareFrame(HWComposer& hwc,
332 std::vector<CompositionInfo>& compositionData) {
333 status_t error = hwc.prepare(*this, compositionData);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800334 if (error != NO_ERROR) {
335 return error;
336 }
337
338 DisplaySurface::CompositionType compositionType;
Dominik Laskowski7e045462018-05-30 13:02:02 -0700339 bool hasClient = hwc.hasClientComposition(mId);
340 bool hasDevice = hwc.hasDeviceComposition(mId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800341 if (hasClient && hasDevice) {
342 compositionType = DisplaySurface::COMPOSITION_MIXED;
343 } else if (hasClient) {
344 compositionType = DisplaySurface::COMPOSITION_GLES;
345 } else if (hasDevice) {
346 compositionType = DisplaySurface::COMPOSITION_HWC;
347 } else {
348 // Nothing to do -- when turning the screen off we get a frame like
349 // this. Call it a HWC frame since we won't be doing any GLES work but
350 // will do a prepare/set cycle.
351 compositionType = DisplaySurface::COMPOSITION_HWC;
352 }
353 return mDisplaySurface->prepareFrame(compositionType);
354}
Jesse Hall38efe862013-04-06 23:12:29 -0700355
Mathias Agopianda27af92012-09-13 18:17:13 -0700356void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700357 if (hwc.hasClientComposition(mId) || hwc.hasFlipClientTargetRequest(mId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800358 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700359 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700360
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700361 status_t result = mDisplaySurface->advanceFrame();
362 if (result != NO_ERROR) {
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700363 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplayName.c_str(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700364 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700365}
366
Dan Stoza9e56aa02015-11-02 13:00:03 -0800367void DisplayDevice::onSwapBuffersCompleted() const {
368 mDisplaySurface->onFrameCommitted();
369}
Mathias Agopianda27af92012-09-13 18:17:13 -0700370
Chia-I Wuf846a352017-11-10 09:22:52 -0800371bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800372 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700373 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800374 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700375}
376
Mathias Agopian875d8e12013-06-07 15:35:48 -0700377void DisplayDevice::setViewportAndProjection() const {
378 size_t w = mDisplayWidth;
379 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700380 Rect sourceCrop(0, 0, w, h);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700381 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, ui::Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700382}
383
Dan Stoza9e56aa02015-11-02 13:00:03 -0800384const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
385 return mDisplaySurface->getClientTargetAcquireFence();
386}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800387
Mathias Agopian1b031492012-06-20 17:51:20 -0700388// ----------------------------------------------------------------------------
389
Mathias Agopian13127d82013-03-05 17:47:11 -0800390void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700391 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700392}
393
Mathias Agopian13127d82013-03-05 17:47:11 -0800394const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700395 return mVisibleLayersSortedByZ;
396}
397
Chia-I Wu83806892017-11-16 10:50:20 -0800398void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
399 mLayersNeedingFences = layers;
400}
401
402const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
403 return mLayersNeedingFences;
404}
405
Mathias Agopiancd60f992012-08-16 16:28:27 -0700406Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
407 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700408 if (repaintEverything) {
409 dirty.set(getBounds());
410 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700411 const ui::Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700412 dirty = planeTransform.transform(this->dirtyRegion);
413 dirty.andSelf(getBounds());
414 }
415 return dirty;
416}
417
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700418// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700419void DisplayDevice::setPowerMode(int mode) {
420 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700421}
422
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700423int DisplayDevice::getPowerMode() const {
424 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700425}
426
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700427bool DisplayDevice::isPoweredOn() const {
428 return mPowerMode != HWC_POWER_MODE_OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700429}
430
431// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700432void DisplayDevice::setActiveConfig(int mode) {
433 mActiveConfig = mode;
434}
435
436int DisplayDevice::getActiveConfig() const {
437 return mActiveConfig;
438}
439
440// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700441void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700442 mActiveColorMode = mode;
443}
444
Peiyong Lina52f0292018-03-14 17:26:31 -0700445ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700446 return mActiveColorMode;
447}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600448
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800449RenderIntent DisplayDevice::getActiveRenderIntent() const {
450 return mActiveRenderIntent;
451}
452
453void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
454 mActiveRenderIntent = renderIntent;
455}
456
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800457void DisplayDevice::setColorTransform(const mat4& transform) {
458 const bool isIdentity = (transform == mat4());
459 mColorTransform =
460 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
461}
462
463android_color_transform_t DisplayDevice::getColorTransform() const {
464 return mColorTransform;
465}
466
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700467void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800468 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600469 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700470 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600471}
Michael Wright28f24d02016-07-12 13:30:53 -0700472
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800473ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
474 return mCompositionDataSpace;
475}
476
Michael Wright28f24d02016-07-12 13:30:53 -0700477// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700478
Mathias Agopian28947d72012-08-08 18:51:15 -0700479void DisplayDevice::setLayerStack(uint32_t stack) {
480 mLayerStack = stack;
481 dirtyRegion.set(bounds());
482}
483
484// ----------------------------------------------------------------------------
485
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700486uint32_t DisplayDevice::getOrientationTransform() const {
487 uint32_t transform = 0;
488 switch (mOrientation) {
489 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700490 transform = ui::Transform::ROT_0;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700491 break;
492 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700493 transform = ui::Transform::ROT_90;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700494 break;
495 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700496 transform = ui::Transform::ROT_180;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700497 break;
498 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700499 transform = ui::Transform::ROT_270;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700500 break;
501 }
502 return transform;
503}
504
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700505status_t DisplayDevice::orientationToTransfrom(
Peiyong Linefefaac2018-08-17 12:27:51 -0700506 int orientation, int w, int h, ui::Transform* tr)
Mathias Agopian1b031492012-06-20 17:51:20 -0700507{
508 uint32_t flags = 0;
509 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700510 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700511 flags = ui::Transform::ROT_0;
Mathias Agopian1b031492012-06-20 17:51:20 -0700512 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700513 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700514 flags = ui::Transform::ROT_90;
Mathias Agopian1b031492012-06-20 17:51:20 -0700515 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700516 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700517 flags = ui::Transform::ROT_180;
Mathias Agopian1b031492012-06-20 17:51:20 -0700518 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700519 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700520 flags = ui::Transform::ROT_270;
Mathias Agopian1b031492012-06-20 17:51:20 -0700521 break;
522 default:
523 return BAD_VALUE;
524 }
525 tr->set(flags, w, h);
526 return NO_ERROR;
527}
528
Michael Lentine47e45402014-07-18 15:34:25 -0700529void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
530 dirtyRegion.set(getBounds());
531
Lloyd Pique144e1162017-12-20 16:44:52 -0800532 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700533
Michael Lentine47e45402014-07-18 15:34:25 -0700534 mDisplaySurface->resizeBuffers(newWidth, newHeight);
535
536 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800537 mSurface->setNativeWindow(window);
Alec Mouri05483a02018-09-10 21:03:42 +0000538 mDisplayWidth = mSurface->getWidth();
539 mDisplayHeight = mSurface->getHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700540
541 LOG_FATAL_IF(mDisplayWidth != newWidth,
542 "Unable to set new width to %d", newWidth);
543 LOG_FATAL_IF(mDisplayHeight != newHeight,
544 "Unable to set new height to %d", newHeight);
545}
546
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700547void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800548 const Rect& newViewport, const Rect& newFrame) {
549 Rect viewport(newViewport);
550 Rect frame(newFrame);
551
552 const int w = mDisplayWidth;
553 const int h = mDisplayHeight;
554
Peiyong Linefefaac2018-08-17 12:27:51 -0700555 ui::Transform R;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800556 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
557
558 if (!frame.isValid()) {
559 // the destination frame can be invalid if it has never been set,
560 // in that case we assume the whole display frame.
561 frame = Rect(w, h);
562 }
563
564 if (viewport.isEmpty()) {
565 // viewport can be invalid if it has never been set, in that case
566 // we assume the whole display size.
567 // it's also invalid to have an empty viewport, so we handle that
568 // case in the same way.
569 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700570 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800571 // viewport is always specified in the logical orientation
572 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700573 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800574 }
575 }
576
577 dirtyRegion.set(getBounds());
578
Peiyong Linefefaac2018-08-17 12:27:51 -0700579 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800580 float src_width = viewport.width();
581 float src_height = viewport.height();
582 float dst_width = frame.width();
583 float dst_height = frame.height();
584 if (src_width != dst_width || src_height != dst_height) {
585 float sx = dst_width / src_width;
586 float sy = dst_height / src_height;
587 S.set(sx, 0, 0, sy);
588 }
589
590 float src_x = viewport.left;
591 float src_y = viewport.top;
592 float dst_x = frame.left;
593 float dst_y = frame.top;
594 TL.set(-src_x, -src_y);
595 TP.set(dst_x, dst_y);
596
Iris Chang7501ed62018-04-30 14:45:42 +0800597 // need to take care of primary display rotation for mGlobalTransform
598 // for case if the panel is not installed aligned with device orientation
599 if (mType == DisplayType::DISPLAY_PRIMARY) {
Iris Chang7501ed62018-04-30 14:45:42 +0800600 DisplayDevice::orientationToTransfrom(
Chia-I Wua02871c2018-08-27 14:38:23 -0700601 (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1),
Iris Chang7501ed62018-04-30 14:45:42 +0800602 w, h, &R);
603 }
604
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800605 // The viewport and frame are both in the logical orientation.
606 // Apply the logical translation, scale to physical size, apply the
607 // physical translation and finally rotate to the physical orientation.
608 mGlobalTransform = R * TP * S * TL;
609
610 const uint8_t type = mGlobalTransform.getType();
611 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
Peiyong Linefefaac2018-08-17 12:27:51 -0700612 (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800613
614 mScissor = mGlobalTransform.transform(viewport);
615 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700616 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800617 }
618
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700619 mOrientation = orientation;
Dominik Laskowski281644e2018-04-19 15:47:35 -0700620 if (isPrimary()) {
Pablo Ceballos021623b2016-04-15 17:31:51 -0700621 uint32_t transform = 0;
622 switch (mOrientation) {
623 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700624 transform = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700625 break;
626 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700627 transform = ui::Transform::ROT_90;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700628 break;
629 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700630 transform = ui::Transform::ROT_180;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700631 break;
632 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700633 transform = ui::Transform::ROT_270;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700634 break;
635 }
636 sPrimaryDisplayOrientation = transform;
637 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700638 mViewport = viewport;
639 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700640}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700641
Pablo Ceballos021623b2016-04-15 17:31:51 -0700642uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
643 return sPrimaryDisplayOrientation;
644}
645
Mathias Agopian74d211a2013-04-22 16:55:35 +0200646void DisplayDevice::dump(String8& result) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700647 const ui::Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600648 ANativeWindow* const window = mNativeWindow.get();
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700649 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.c_str());
Dominik Laskowski7e045462018-05-30 13:02:02 -0700650 result.appendFormat(" type=%x, ID=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600651 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
652 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Dominik Laskowski7e045462018-05-30 13:02:02 -0700653 mType, mId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800654 mSurface->queryRedSize(), mSurface->queryGreenSize(),
655 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
656 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600657 mVisibleLayersSortedByZ.size());
658 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
659 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
660 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
661 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
662 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
663 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 -0600664 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700665 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800666 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
667 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800668 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700669 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700670
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700671 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800672 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700673 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700674}
Irvelffc9efc2016-07-27 15:16:37 -0700675
Chia-I Wube02ec02018-05-18 10:59:36 -0700676// Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
677// supported by HWC.
678void DisplayDevice::addColorMode(
679 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
680 const ColorMode mode, const RenderIntent intent) {
681 // find the best color mode
682 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
683
684 // find the best render intent
685 auto iter = hwcColorModes.find(hwcColorMode);
686 const auto& hwcIntents =
687 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
688 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
689
690 const Dataspace dataspace = colorModeToDataspace(mode);
691 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
692
Dominik Laskowski7e045462018-05-30 13:02:02 -0700693 ALOGV("DisplayDevice %d/%d: map (%s, %s) to (%s, %s, %s)", mType, mId,
Chia-I Wube02ec02018-05-18 10:59:36 -0700694 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
695 decodeRenderIntent(intent).c_str(),
696 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
697 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
698
699 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
700}
701
702void DisplayDevice::populateColorModes(
703 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) {
704 if (!hasWideColorGamut()) {
705 return;
706 }
707
Chia-I Wu0d711262018-05-21 15:19:35 -0700708 // collect all known SDR render intents
709 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
710 sSdrRenderIntents.end());
711 auto iter = hwcColorModes.find(ColorMode::SRGB);
712 if (iter != hwcColorModes.end()) {
713 for (auto intent : iter->second) {
714 sdrRenderIntents.insert(intent);
715 }
716 }
717
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700718 // add all known SDR combinations
Chia-I Wu0d711262018-05-21 15:19:35 -0700719 for (auto intent : sdrRenderIntents) {
Chia-I Wube02ec02018-05-18 10:59:36 -0700720 for (auto mode : sSdrColorModes) {
721 addColorMode(hwcColorModes, mode, intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700722 }
723 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700724
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700725 // collect all known HDR render intents
726 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
727 sHdrRenderIntents.end());
728 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
729 if (iter != hwcColorModes.end()) {
730 for (auto intent : iter->second) {
731 hdrRenderIntents.insert(intent);
732 }
733 }
734
735 // add all known HDR combinations
Chia-I Wube02ec02018-05-18 10:59:36 -0700736 for (auto intent : sHdrRenderIntents) {
737 for (auto mode : sHdrColorModes) {
738 addColorMode(hwcColorModes, mode, intent);
739 }
740 }
741}
742
743bool DisplayDevice::hasRenderIntent(RenderIntent intent) const {
744 // assume a render intent is supported when SRGB supports it; we should
745 // get rid of that assumption.
746 auto iter = mColorModes.find(getColorModeKey(Dataspace::SRGB, intent));
747 return iter != mColorModes.end() && iter->second.renderIntent == intent;
748}
749
Peiyong Lindfde5112018-06-05 10:58:41 -0700750bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const {
Chia-I Wube02ec02018-05-18 10:59:36 -0700751 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
752 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
753 auto iter =
754 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
Peiyong Lindfde5112018-06-05 10:58:41 -0700755 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
Chia-I Wube02ec02018-05-18 10:59:36 -0700756 }
757
758 return false;
759}
760
761void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent,
762 Dataspace* outDataspace, ColorMode* outMode,
763 RenderIntent* outIntent) const {
764 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
765 if (iter != mColorModes.end()) {
766 *outDataspace = iter->second.dataspace;
767 *outMode = iter->second.colorMode;
768 *outIntent = iter->second.renderIntent;
769 } else {
770 ALOGE("map unknown (%s)/(%s) to default color mode",
771 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
772 decodeRenderIntent(intent).c_str());
773
774 *outDataspace = Dataspace::UNKNOWN;
775 *outMode = ColorMode::NATIVE;
776 *outIntent = RenderIntent::COLORIMETRIC;
777 }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700778}
779
Dominik Laskowski663bd282018-04-19 15:26:54 -0700780std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700781
782} // namespace android