blob: 91b18c9fd747d9356640fd2f6b94874378a681f5 [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,
215 DisplayDevice::DisplayType type, int32_t id)
216 : flinger(flinger), displayToken(displayToken), type(type), id(id) {}
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),
221 mType(args.type),
222 mId(args.id),
223 mDisplayToken(args.displayToken),
224 mNativeWindow(args.nativeWindow),
225 mDisplaySurface(args.displaySurface),
226 mSurface{std::move(args.renderSurface)},
227 mDisplayWidth(args.displayWidth),
228 mDisplayHeight(args.displayHeight),
229 mDisplayInstallOrientation(args.displayInstallOrientation),
230 mPageFlipCount(0),
231 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),
243 mSupportedPerFrameMetadata(args.supportedPerFrameMetadata) {
244 populateColorModes(args.hwcColorModes);
245
246 ALOGE_IF(!mNativeWindow, "No native window was set for display");
247 ALOGE_IF(!mDisplaySurface, "No display surface was set for display");
248 ALOGE_IF(!mSurface, "No render surface was set for display");
249 ALOGE_IF(mDisplayWidth <= 0 || mDisplayHeight <= 0,
250 "Invalid dimensions of %d x %d were set for display", mDisplayWidth, mDisplayHeight);
251
252 std::vector<Hdr> types = args.hdrCapabilities.getSupportedHdrTypes();
Peiyong Linfb069302018-04-25 14:34:31 -0700253 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700254 switch (hdrType) {
255 case Hdr::HDR10:
256 mHasHdr10 = true;
257 break;
258 case Hdr::HLG:
259 mHasHLG = true;
260 break;
261 case Hdr::DOLBY_VISION:
262 mHasDolbyVision = true;
263 break;
264 default:
265 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
266 }
267 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700268
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700269 float minLuminance = args.hdrCapabilities.getDesiredMinLuminance();
270 float maxLuminance = args.hdrCapabilities.getDesiredMaxLuminance();
271 float maxAverageLuminance = args.hdrCapabilities.getDesiredMaxAverageLuminance();
Peiyong Linfb069302018-04-25 14:34:31 -0700272
273 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
274 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
275 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
276 if (this->hasWideColorGamut()) {
277 // insert HDR10/HLG as we will force client composition for HDR10/HLG
278 // layers
279 if (!hasHDR10Support()) {
280 types.push_back(Hdr::HDR10);
281 }
282
283 if (!hasHLGSupport()) {
284 types.push_back(Hdr::HLG);
285 }
286 }
287 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
288
Jesse Hallffe1f192013-03-22 15:13:48 -0700289 // initialize the display orientation transform.
290 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291}
292
Lloyd Pique09594832018-01-22 17:48:03 -0800293DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700294
Jesse Hall02d86562013-03-25 14:43:23 -0700295void DisplayDevice::disconnect(HWComposer& hwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700296 if (mId >= 0) {
297 hwc.disconnectDisplay(mId);
298 mId = -1;
Jesse Hall02d86562013-03-25 14:43:23 -0700299 }
300}
301
Mathias Agopian92a979a2012-08-02 18:32:23 -0700302bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800303 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304}
305
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700306int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700307 return mDisplayWidth;
308}
309
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700310int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700311 return mDisplayHeight;
312}
313
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700314void DisplayDevice::setDisplayName(const std::string& displayName) {
315 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700316 // never override the name with an empty name
317 mDisplayName = displayName;
318 }
319}
320
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700321uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700322 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323}
324
Chia-I Wub02087d2017-11-09 10:19:54 -0800325void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700327 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329}
330
Dan Stoza71433162014-02-04 16:22:36 -0800331status_t DisplayDevice::beginFrame(bool mustRecompose) const {
332 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700333}
334
David Sodmanfb95bcc2017-12-22 15:45:30 -0800335status_t DisplayDevice::prepareFrame(HWComposer& hwc,
336 std::vector<CompositionInfo>& compositionData) {
337 status_t error = hwc.prepare(*this, compositionData);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800338 if (error != NO_ERROR) {
339 return error;
340 }
341
342 DisplaySurface::CompositionType compositionType;
Dominik Laskowski7e045462018-05-30 13:02:02 -0700343 bool hasClient = hwc.hasClientComposition(mId);
344 bool hasDevice = hwc.hasDeviceComposition(mId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800345 if (hasClient && hasDevice) {
346 compositionType = DisplaySurface::COMPOSITION_MIXED;
347 } else if (hasClient) {
348 compositionType = DisplaySurface::COMPOSITION_GLES;
349 } else if (hasDevice) {
350 compositionType = DisplaySurface::COMPOSITION_HWC;
351 } else {
352 // Nothing to do -- when turning the screen off we get a frame like
353 // this. Call it a HWC frame since we won't be doing any GLES work but
354 // will do a prepare/set cycle.
355 compositionType = DisplaySurface::COMPOSITION_HWC;
356 }
357 return mDisplaySurface->prepareFrame(compositionType);
358}
Jesse Hall38efe862013-04-06 23:12:29 -0700359
Mathias Agopianda27af92012-09-13 18:17:13 -0700360void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700361 if (hwc.hasClientComposition(mId) || hwc.hasFlipClientTargetRequest(mId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800362 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700363 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700364
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700365 status_t result = mDisplaySurface->advanceFrame();
366 if (result != NO_ERROR) {
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700367 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplayName.c_str(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700368 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700369}
370
Dan Stoza9e56aa02015-11-02 13:00:03 -0800371void DisplayDevice::onSwapBuffersCompleted() const {
372 mDisplaySurface->onFrameCommitted();
373}
Mathias Agopianda27af92012-09-13 18:17:13 -0700374
Chia-I Wuf846a352017-11-10 09:22:52 -0800375bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800376 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700377 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800378 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700379}
380
Mathias Agopian875d8e12013-06-07 15:35:48 -0700381void DisplayDevice::setViewportAndProjection() const {
382 size_t w = mDisplayWidth;
383 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700384 Rect sourceCrop(0, 0, w, h);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700385 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, ui::Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700386}
387
Dan Stoza9e56aa02015-11-02 13:00:03 -0800388const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
389 return mDisplaySurface->getClientTargetAcquireFence();
390}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800391
Mathias Agopian1b031492012-06-20 17:51:20 -0700392// ----------------------------------------------------------------------------
393
Mathias Agopian13127d82013-03-05 17:47:11 -0800394void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700395 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700396}
397
Mathias Agopian13127d82013-03-05 17:47:11 -0800398const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700399 return mVisibleLayersSortedByZ;
400}
401
Chia-I Wu83806892017-11-16 10:50:20 -0800402void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
403 mLayersNeedingFences = layers;
404}
405
406const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
407 return mLayersNeedingFences;
408}
409
Mathias Agopiancd60f992012-08-16 16:28:27 -0700410Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
411 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700412 if (repaintEverything) {
413 dirty.set(getBounds());
414 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700415 const ui::Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700416 dirty = planeTransform.transform(this->dirtyRegion);
417 dirty.andSelf(getBounds());
418 }
419 return dirty;
420}
421
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700422// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700423void DisplayDevice::setPowerMode(int mode) {
424 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700425}
426
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700427int DisplayDevice::getPowerMode() const {
428 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700429}
430
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700431bool DisplayDevice::isPoweredOn() const {
432 return mPowerMode != HWC_POWER_MODE_OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700433}
434
435// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700436void DisplayDevice::setActiveConfig(int mode) {
437 mActiveConfig = mode;
438}
439
440int DisplayDevice::getActiveConfig() const {
441 return mActiveConfig;
442}
443
444// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700445void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700446 mActiveColorMode = mode;
447}
448
Peiyong Lina52f0292018-03-14 17:26:31 -0700449ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700450 return mActiveColorMode;
451}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600452
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800453RenderIntent DisplayDevice::getActiveRenderIntent() const {
454 return mActiveRenderIntent;
455}
456
457void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
458 mActiveRenderIntent = renderIntent;
459}
460
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800461void DisplayDevice::setColorTransform(const mat4& transform) {
462 const bool isIdentity = (transform == mat4());
463 mColorTransform =
464 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
465}
466
467android_color_transform_t DisplayDevice::getColorTransform() const {
468 return mColorTransform;
469}
470
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700471void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800472 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600473 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700474 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600475}
Michael Wright28f24d02016-07-12 13:30:53 -0700476
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800477ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
478 return mCompositionDataSpace;
479}
480
Michael Wright28f24d02016-07-12 13:30:53 -0700481// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700482
Mathias Agopian28947d72012-08-08 18:51:15 -0700483void DisplayDevice::setLayerStack(uint32_t stack) {
484 mLayerStack = stack;
485 dirtyRegion.set(bounds());
486}
487
488// ----------------------------------------------------------------------------
489
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700490uint32_t DisplayDevice::getOrientationTransform() const {
491 uint32_t transform = 0;
492 switch (mOrientation) {
493 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700494 transform = ui::Transform::ROT_0;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700495 break;
496 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700497 transform = ui::Transform::ROT_90;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700498 break;
499 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700500 transform = ui::Transform::ROT_180;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700501 break;
502 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700503 transform = ui::Transform::ROT_270;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700504 break;
505 }
506 return transform;
507}
508
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700509status_t DisplayDevice::orientationToTransfrom(
Peiyong Linefefaac2018-08-17 12:27:51 -0700510 int orientation, int w, int h, ui::Transform* tr)
Mathias Agopian1b031492012-06-20 17:51:20 -0700511{
512 uint32_t flags = 0;
513 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700514 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700515 flags = ui::Transform::ROT_0;
Mathias Agopian1b031492012-06-20 17:51:20 -0700516 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700517 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700518 flags = ui::Transform::ROT_90;
Mathias Agopian1b031492012-06-20 17:51:20 -0700519 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700520 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700521 flags = ui::Transform::ROT_180;
Mathias Agopian1b031492012-06-20 17:51:20 -0700522 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700523 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700524 flags = ui::Transform::ROT_270;
Mathias Agopian1b031492012-06-20 17:51:20 -0700525 break;
526 default:
527 return BAD_VALUE;
528 }
529 tr->set(flags, w, h);
530 return NO_ERROR;
531}
532
Michael Lentine47e45402014-07-18 15:34:25 -0700533void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
534 dirtyRegion.set(getBounds());
535
Lloyd Pique144e1162017-12-20 16:44:52 -0800536 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700537
Michael Lentine47e45402014-07-18 15:34:25 -0700538 mDisplaySurface->resizeBuffers(newWidth, newHeight);
539
540 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800541 mSurface->setNativeWindow(window);
Alec Mouri05483a02018-09-10 21:03:42 +0000542 mDisplayWidth = mSurface->getWidth();
543 mDisplayHeight = mSurface->getHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700544
545 LOG_FATAL_IF(mDisplayWidth != newWidth,
546 "Unable to set new width to %d", newWidth);
547 LOG_FATAL_IF(mDisplayHeight != newHeight,
548 "Unable to set new height to %d", newHeight);
549}
550
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700551void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800552 const Rect& newViewport, const Rect& newFrame) {
553 Rect viewport(newViewport);
554 Rect frame(newFrame);
555
556 const int w = mDisplayWidth;
557 const int h = mDisplayHeight;
558
Peiyong Linefefaac2018-08-17 12:27:51 -0700559 ui::Transform R;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800560 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
561
562 if (!frame.isValid()) {
563 // the destination frame can be invalid if it has never been set,
564 // in that case we assume the whole display frame.
565 frame = Rect(w, h);
566 }
567
568 if (viewport.isEmpty()) {
569 // viewport can be invalid if it has never been set, in that case
570 // we assume the whole display size.
571 // it's also invalid to have an empty viewport, so we handle that
572 // case in the same way.
573 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700574 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800575 // viewport is always specified in the logical orientation
576 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700577 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800578 }
579 }
580
581 dirtyRegion.set(getBounds());
582
Peiyong Linefefaac2018-08-17 12:27:51 -0700583 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800584 float src_width = viewport.width();
585 float src_height = viewport.height();
586 float dst_width = frame.width();
587 float dst_height = frame.height();
588 if (src_width != dst_width || src_height != dst_height) {
589 float sx = dst_width / src_width;
590 float sy = dst_height / src_height;
591 S.set(sx, 0, 0, sy);
592 }
593
594 float src_x = viewport.left;
595 float src_y = viewport.top;
596 float dst_x = frame.left;
597 float dst_y = frame.top;
598 TL.set(-src_x, -src_y);
599 TP.set(dst_x, dst_y);
600
Iris Chang7501ed62018-04-30 14:45:42 +0800601 // need to take care of primary display rotation for mGlobalTransform
602 // for case if the panel is not installed aligned with device orientation
603 if (mType == DisplayType::DISPLAY_PRIMARY) {
Iris Chang7501ed62018-04-30 14:45:42 +0800604 DisplayDevice::orientationToTransfrom(
Chia-I Wua02871c2018-08-27 14:38:23 -0700605 (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1),
Iris Chang7501ed62018-04-30 14:45:42 +0800606 w, h, &R);
607 }
608
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800609 // The viewport and frame are both in the logical orientation.
610 // Apply the logical translation, scale to physical size, apply the
611 // physical translation and finally rotate to the physical orientation.
612 mGlobalTransform = R * TP * S * TL;
613
614 const uint8_t type = mGlobalTransform.getType();
615 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
Peiyong Linefefaac2018-08-17 12:27:51 -0700616 (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800617
618 mScissor = mGlobalTransform.transform(viewport);
619 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700620 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800621 }
622
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700623 mOrientation = orientation;
Dominik Laskowski281644e2018-04-19 15:47:35 -0700624 if (isPrimary()) {
Pablo Ceballos021623b2016-04-15 17:31:51 -0700625 uint32_t transform = 0;
626 switch (mOrientation) {
627 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700628 transform = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700629 break;
630 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700631 transform = ui::Transform::ROT_90;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700632 break;
633 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700634 transform = ui::Transform::ROT_180;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700635 break;
636 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700637 transform = ui::Transform::ROT_270;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700638 break;
639 }
640 sPrimaryDisplayOrientation = transform;
641 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700642 mViewport = viewport;
643 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700644}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700645
Pablo Ceballos021623b2016-04-15 17:31:51 -0700646uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
647 return sPrimaryDisplayOrientation;
648}
649
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700650std::string DisplayDevice::getDebugName() const {
651 const auto id = mId >= 0 ? base::StringPrintf("%d, ", mId) : std::string();
652 return base::StringPrintf("DisplayDevice{%s%s%s\"%s\"}", id.c_str(),
653 isPrimary() ? "primary, " : "", isVirtual() ? "virtual, " : "",
654 mDisplayName.c_str());
655}
656
Mathias Agopian74d211a2013-04-22 16:55:35 +0200657void DisplayDevice::dump(String8& result) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700658 const ui::Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600659 ANativeWindow* const window = mNativeWindow.get();
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700660 result.appendFormat("+ %s\n", getDebugName().c_str());
661 result.appendFormat(" layerStack=%u, (%4dx%4d), ANativeWindow=%p "
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600662 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
663 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700664 mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800665 mSurface->queryRedSize(), mSurface->queryGreenSize(),
666 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
667 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600668 mVisibleLayersSortedByZ.size());
669 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
670 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
671 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
672 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
673 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
674 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 -0600675 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700676 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800677 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
678 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800679 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700680 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700681
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700682 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800683 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700684 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700685}
Irvelffc9efc2016-07-27 15:16:37 -0700686
Chia-I Wube02ec02018-05-18 10:59:36 -0700687// Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
688// supported by HWC.
689void DisplayDevice::addColorMode(
690 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
691 const ColorMode mode, const RenderIntent intent) {
692 // find the best color mode
693 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
694
695 // find the best render intent
696 auto iter = hwcColorModes.find(hwcColorMode);
697 const auto& hwcIntents =
698 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
699 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
700
701 const Dataspace dataspace = colorModeToDataspace(mode);
702 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
703
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700704 ALOGV("%s: map (%s, %s) to (%s, %s, %s)", getDebugName().c_str(),
Chia-I Wube02ec02018-05-18 10:59:36 -0700705 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
706 decodeRenderIntent(intent).c_str(),
707 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
708 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
709
710 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
711}
712
713void DisplayDevice::populateColorModes(
714 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) {
715 if (!hasWideColorGamut()) {
716 return;
717 }
718
Chia-I Wu0d711262018-05-21 15:19:35 -0700719 // collect all known SDR render intents
720 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
721 sSdrRenderIntents.end());
722 auto iter = hwcColorModes.find(ColorMode::SRGB);
723 if (iter != hwcColorModes.end()) {
724 for (auto intent : iter->second) {
725 sdrRenderIntents.insert(intent);
726 }
727 }
728
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700729 // add all known SDR combinations
Chia-I Wu0d711262018-05-21 15:19:35 -0700730 for (auto intent : sdrRenderIntents) {
Chia-I Wube02ec02018-05-18 10:59:36 -0700731 for (auto mode : sSdrColorModes) {
732 addColorMode(hwcColorModes, mode, intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700733 }
734 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700735
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700736 // collect all known HDR render intents
737 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
738 sHdrRenderIntents.end());
739 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
740 if (iter != hwcColorModes.end()) {
741 for (auto intent : iter->second) {
742 hdrRenderIntents.insert(intent);
743 }
744 }
745
746 // add all known HDR combinations
Chia-I Wube02ec02018-05-18 10:59:36 -0700747 for (auto intent : sHdrRenderIntents) {
748 for (auto mode : sHdrColorModes) {
749 addColorMode(hwcColorModes, mode, intent);
750 }
751 }
752}
753
754bool DisplayDevice::hasRenderIntent(RenderIntent intent) const {
755 // assume a render intent is supported when SRGB supports it; we should
756 // get rid of that assumption.
757 auto iter = mColorModes.find(getColorModeKey(Dataspace::SRGB, intent));
758 return iter != mColorModes.end() && iter->second.renderIntent == intent;
759}
760
Peiyong Lindfde5112018-06-05 10:58:41 -0700761bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const {
Chia-I Wube02ec02018-05-18 10:59:36 -0700762 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
763 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
764 auto iter =
765 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
Peiyong Lindfde5112018-06-05 10:58:41 -0700766 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
Chia-I Wube02ec02018-05-18 10:59:36 -0700767 }
768
769 return false;
770}
771
772void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent,
773 Dataspace* outDataspace, ColorMode* outMode,
774 RenderIntent* outIntent) const {
775 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
776 if (iter != mColorModes.end()) {
777 *outDataspace = iter->second.dataspace;
778 *outMode = iter->second.colorMode;
779 *outIntent = iter->second.renderIntent;
780 } else {
Chia-I Wu6333af52018-10-16 13:46:36 -0700781 // this is unexpected on a WCG display
782 if (hasWideColorGamut()) {
783 ALOGE("map unknown (%s)/(%s) to default color mode",
784 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
785 decodeRenderIntent(intent).c_str());
786 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700787
788 *outDataspace = Dataspace::UNKNOWN;
789 *outMode = ColorMode::NATIVE;
790 *outIntent = RenderIntent::COLORIMETRIC;
791 }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700792}
793
Dominik Laskowski663bd282018-04-19 15:26:54 -0700794std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700795
796} // namespace android