blob: 6f645df73fcf9eb4a67af97b2dc36f0b4ca26f8f [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/Log.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070041#include <utils/RefBase.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
Valerie Hau9758ae02018-10-09 16:05:09 -070069const std::array<ColorMode, 3> sSdrColorModes = {
70 ColorMode::DISPLAY_BT2020,
Chia-I Wube02ec02018-05-18 10:59:36 -070071 ColorMode::DISPLAY_P3,
72 ColorMode::SRGB,
73};
74
75// ordered list of known HDR color modes
76const std::array<ColorMode, 2> sHdrColorModes = {
77 ColorMode::BT2100_PQ,
78 ColorMode::BT2100_HLG,
79};
80
81// ordered list of known SDR render intents
82const std::array<RenderIntent, 2> sSdrRenderIntents = {
83 RenderIntent::ENHANCE,
84 RenderIntent::COLORIMETRIC,
85};
86
87// ordered list of known HDR render intents
88const std::array<RenderIntent, 2> sHdrRenderIntents = {
89 RenderIntent::TONE_MAP_ENHANCE,
90 RenderIntent::TONE_MAP_COLORIMETRIC,
91};
92
93// map known color mode to dataspace
94Dataspace colorModeToDataspace(ColorMode mode) {
95 switch (mode) {
96 case ColorMode::SRGB:
97 return Dataspace::SRGB;
98 case ColorMode::DISPLAY_P3:
99 return Dataspace::DISPLAY_P3;
Valerie Hau9758ae02018-10-09 16:05:09 -0700100 case ColorMode::DISPLAY_BT2020:
101 return Dataspace::DISPLAY_BT2020;
Chia-I Wube02ec02018-05-18 10:59:36 -0700102 case ColorMode::BT2100_HLG:
103 return Dataspace::BT2020_HLG;
104 case ColorMode::BT2100_PQ:
105 return Dataspace::BT2020_PQ;
106 default:
107 return Dataspace::UNKNOWN;
108 }
109}
110
111// Return a list of candidate color modes.
112std::vector<ColorMode> getColorModeCandidates(ColorMode mode) {
113 std::vector<ColorMode> candidates;
114
115 // add mode itself
116 candidates.push_back(mode);
117
118 // check if mode is HDR
119 bool isHdr = false;
120 for (auto hdrMode : sHdrColorModes) {
121 if (hdrMode == mode) {
122 isHdr = true;
123 break;
124 }
125 }
126
127 // add other HDR candidates when mode is HDR
128 if (isHdr) {
129 for (auto hdrMode : sHdrColorModes) {
130 if (hdrMode != mode) {
131 candidates.push_back(hdrMode);
132 }
133 }
134 }
135
136 // add other SDR candidates
137 for (auto sdrMode : sSdrColorModes) {
138 if (sdrMode != mode) {
139 candidates.push_back(sdrMode);
140 }
141 }
142
143 return candidates;
144}
145
146// Return a list of candidate render intents.
147std::vector<RenderIntent> getRenderIntentCandidates(RenderIntent intent) {
148 std::vector<RenderIntent> candidates;
149
150 // add intent itself
151 candidates.push_back(intent);
152
153 // check if intent is HDR
154 bool isHdr = false;
155 for (auto hdrIntent : sHdrRenderIntents) {
156 if (hdrIntent == intent) {
157 isHdr = true;
158 break;
159 }
160 }
161
Chia-I Wube02ec02018-05-18 10:59:36 -0700162 if (isHdr) {
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700163 // add other HDR candidates when intent is HDR
Chia-I Wube02ec02018-05-18 10:59:36 -0700164 for (auto hdrIntent : sHdrRenderIntents) {
165 if (hdrIntent != intent) {
166 candidates.push_back(hdrIntent);
167 }
168 }
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700169 } else {
170 // add other SDR candidates when intent is SDR
171 for (auto sdrIntent : sSdrRenderIntents) {
172 if (sdrIntent != intent) {
173 candidates.push_back(sdrIntent);
174 }
175 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700176 }
177
178 return candidates;
179}
180
181// Return the best color mode supported by HWC.
182ColorMode getHwcColorMode(
183 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
184 ColorMode mode) {
185 std::vector<ColorMode> candidates = getColorModeCandidates(mode);
186 for (auto candidate : candidates) {
187 auto iter = hwcColorModes.find(candidate);
188 if (iter != hwcColorModes.end()) {
189 return candidate;
190 }
191 }
192
193 return ColorMode::NATIVE;
194}
195
196// Return the best render intent supported by HWC.
197RenderIntent getHwcRenderIntent(const std::vector<RenderIntent>& hwcIntents, RenderIntent intent) {
198 std::vector<RenderIntent> candidates = getRenderIntentCandidates(intent);
199 for (auto candidate : candidates) {
200 for (auto hwcIntent : hwcIntents) {
201 if (candidate == hwcIntent) {
202 return candidate;
203 }
204 }
205 }
206
207 return RenderIntent::COLORIMETRIC;
208}
209
210} // anonymous namespace
211
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700212DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger,
213 const wp<IBinder>& displayToken,
214 DisplayDevice::DisplayType type, int32_t id)
215 : flinger(flinger), displayToken(displayToken), type(type), id(id) {}
Chia-I Wube02ec02018-05-18 10:59:36 -0700216
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700217DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs&& args)
218 : lastCompositionHadVisibleLayers(false),
219 mFlinger(args.flinger),
220 mType(args.type),
221 mId(args.id),
222 mDisplayToken(args.displayToken),
223 mNativeWindow(args.nativeWindow),
224 mDisplaySurface(args.displaySurface),
225 mSurface{std::move(args.renderSurface)},
226 mDisplayWidth(args.displayWidth),
227 mDisplayHeight(args.displayHeight),
228 mDisplayInstallOrientation(args.displayInstallOrientation),
229 mPageFlipCount(0),
230 mIsSecure(args.isSecure),
231 mLayerStack(NO_LAYER_STACK),
232 mOrientation(),
233 mViewport(Rect::INVALID_RECT),
234 mFrame(Rect::INVALID_RECT),
235 mPowerMode(args.initialPowerMode),
236 mActiveConfig(0),
237 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
238 mHasWideColorGamut(args.hasWideColorGamut),
239 mHasHdr10(false),
240 mHasHLG(false),
241 mHasDolbyVision(false),
242 mSupportedPerFrameMetadata(args.supportedPerFrameMetadata) {
243 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");
247 ALOGE_IF(!mSurface, "No render surface was set for display");
248 ALOGE_IF(mDisplayWidth <= 0 || mDisplayHeight <= 0,
249 "Invalid dimensions of %d x %d were set for display", mDisplayWidth, mDisplayHeight);
250
251 std::vector<Hdr> types = args.hdrCapabilities.getSupportedHdrTypes();
Peiyong Linfb069302018-04-25 14:34:31 -0700252 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700253 switch (hdrType) {
254 case Hdr::HDR10:
255 mHasHdr10 = true;
256 break;
257 case Hdr::HLG:
258 mHasHLG = true;
259 break;
260 case Hdr::DOLBY_VISION:
261 mHasDolbyVision = true;
262 break;
263 default:
264 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
265 }
266 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700267
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700268 float minLuminance = args.hdrCapabilities.getDesiredMinLuminance();
269 float maxLuminance = args.hdrCapabilities.getDesiredMaxLuminance();
270 float maxAverageLuminance = args.hdrCapabilities.getDesiredMaxAverageLuminance();
Peiyong Linfb069302018-04-25 14:34:31 -0700271
272 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
273 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
274 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
275 if (this->hasWideColorGamut()) {
276 // insert HDR10/HLG as we will force client composition for HDR10/HLG
277 // layers
278 if (!hasHDR10Support()) {
279 types.push_back(Hdr::HDR10);
280 }
281
282 if (!hasHLGSupport()) {
283 types.push_back(Hdr::HLG);
284 }
285 }
286 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
287
Jesse Hallffe1f192013-03-22 15:13:48 -0700288 // initialize the display orientation transform.
289 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290}
291
Lloyd Pique09594832018-01-22 17:48:03 -0800292DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700293
Jesse Hall02d86562013-03-25 14:43:23 -0700294void DisplayDevice::disconnect(HWComposer& hwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700295 if (mId >= 0) {
296 hwc.disconnectDisplay(mId);
297 mId = -1;
Jesse Hall02d86562013-03-25 14:43:23 -0700298 }
299}
300
Mathias Agopian92a979a2012-08-02 18:32:23 -0700301bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800302 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303}
304
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700305int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700306 return mDisplayWidth;
307}
308
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700309int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700310 return mDisplayHeight;
311}
312
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700313void DisplayDevice::setDisplayName(const std::string& displayName) {
314 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700315 // never override the name with an empty name
316 mDisplayName = displayName;
317 }
318}
319
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700320uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700321 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322}
323
Chia-I Wub02087d2017-11-09 10:19:54 -0800324void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700326 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700327 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328}
329
Dan Stoza71433162014-02-04 16:22:36 -0800330status_t DisplayDevice::beginFrame(bool mustRecompose) const {
331 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700332}
333
David Sodmanfb95bcc2017-12-22 15:45:30 -0800334status_t DisplayDevice::prepareFrame(HWComposer& hwc,
335 std::vector<CompositionInfo>& compositionData) {
336 status_t error = hwc.prepare(*this, compositionData);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800337 if (error != NO_ERROR) {
338 return error;
339 }
340
341 DisplaySurface::CompositionType compositionType;
Dominik Laskowski7e045462018-05-30 13:02:02 -0700342 bool hasClient = hwc.hasClientComposition(mId);
343 bool hasDevice = hwc.hasDeviceComposition(mId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800344 if (hasClient && hasDevice) {
345 compositionType = DisplaySurface::COMPOSITION_MIXED;
346 } else if (hasClient) {
347 compositionType = DisplaySurface::COMPOSITION_GLES;
348 } else if (hasDevice) {
349 compositionType = DisplaySurface::COMPOSITION_HWC;
350 } else {
351 // Nothing to do -- when turning the screen off we get a frame like
352 // this. Call it a HWC frame since we won't be doing any GLES work but
353 // will do a prepare/set cycle.
354 compositionType = DisplaySurface::COMPOSITION_HWC;
355 }
356 return mDisplaySurface->prepareFrame(compositionType);
357}
Jesse Hall38efe862013-04-06 23:12:29 -0700358
Mathias Agopianda27af92012-09-13 18:17:13 -0700359void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700360 if (hwc.hasClientComposition(mId) || hwc.hasFlipClientTargetRequest(mId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800361 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700362 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700363
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700364 status_t result = mDisplaySurface->advanceFrame();
365 if (result != NO_ERROR) {
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700366 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplayName.c_str(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700367 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700368}
369
Dan Stoza9e56aa02015-11-02 13:00:03 -0800370void DisplayDevice::onSwapBuffersCompleted() const {
371 mDisplaySurface->onFrameCommitted();
372}
Mathias Agopianda27af92012-09-13 18:17:13 -0700373
Chia-I Wuf846a352017-11-10 09:22:52 -0800374bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800375 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700376 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800377 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700378}
379
Mathias Agopian875d8e12013-06-07 15:35:48 -0700380void DisplayDevice::setViewportAndProjection() const {
381 size_t w = mDisplayWidth;
382 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700383 Rect sourceCrop(0, 0, w, h);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700384 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, ui::Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700385}
386
Dan Stoza9e56aa02015-11-02 13:00:03 -0800387const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
388 return mDisplaySurface->getClientTargetAcquireFence();
389}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800390
Mathias Agopian1b031492012-06-20 17:51:20 -0700391// ----------------------------------------------------------------------------
392
Mathias Agopian13127d82013-03-05 17:47:11 -0800393void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700394 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700395}
396
Mathias Agopian13127d82013-03-05 17:47:11 -0800397const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700398 return mVisibleLayersSortedByZ;
399}
400
Chia-I Wu83806892017-11-16 10:50:20 -0800401void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
402 mLayersNeedingFences = layers;
403}
404
405const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
406 return mLayersNeedingFences;
407}
408
Mathias Agopiancd60f992012-08-16 16:28:27 -0700409Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
410 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700411 if (repaintEverything) {
412 dirty.set(getBounds());
413 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700414 const ui::Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700415 dirty = planeTransform.transform(this->dirtyRegion);
416 dirty.andSelf(getBounds());
417 }
418 return dirty;
419}
420
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700421// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700422void DisplayDevice::setPowerMode(int mode) {
423 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700424}
425
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700426int DisplayDevice::getPowerMode() const {
427 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700428}
429
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700430bool DisplayDevice::isPoweredOn() const {
431 return mPowerMode != HWC_POWER_MODE_OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700432}
433
434// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700435void DisplayDevice::setActiveConfig(int mode) {
436 mActiveConfig = mode;
437}
438
439int DisplayDevice::getActiveConfig() const {
440 return mActiveConfig;
441}
442
443// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700444void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700445 mActiveColorMode = mode;
446}
447
Peiyong Lina52f0292018-03-14 17:26:31 -0700448ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700449 return mActiveColorMode;
450}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600451
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800452RenderIntent DisplayDevice::getActiveRenderIntent() const {
453 return mActiveRenderIntent;
454}
455
456void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
457 mActiveRenderIntent = renderIntent;
458}
459
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800460void DisplayDevice::setColorTransform(const mat4& transform) {
461 const bool isIdentity = (transform == mat4());
462 mColorTransform =
463 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
464}
465
466android_color_transform_t DisplayDevice::getColorTransform() const {
467 return mColorTransform;
468}
469
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700470void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800471 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600472 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700473 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600474}
Michael Wright28f24d02016-07-12 13:30:53 -0700475
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800476ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
477 return mCompositionDataSpace;
478}
479
Michael Wright28f24d02016-07-12 13:30:53 -0700480// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700481
Mathias Agopian28947d72012-08-08 18:51:15 -0700482void DisplayDevice::setLayerStack(uint32_t stack) {
483 mLayerStack = stack;
484 dirtyRegion.set(bounds());
485}
486
487// ----------------------------------------------------------------------------
488
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700489uint32_t DisplayDevice::getOrientationTransform() const {
490 uint32_t transform = 0;
491 switch (mOrientation) {
492 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700493 transform = ui::Transform::ROT_0;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700494 break;
495 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700496 transform = ui::Transform::ROT_90;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700497 break;
498 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700499 transform = ui::Transform::ROT_180;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700500 break;
501 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700502 transform = ui::Transform::ROT_270;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700503 break;
504 }
505 return transform;
506}
507
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700508status_t DisplayDevice::orientationToTransfrom(
Peiyong Linefefaac2018-08-17 12:27:51 -0700509 int orientation, int w, int h, ui::Transform* tr)
Mathias Agopian1b031492012-06-20 17:51:20 -0700510{
511 uint32_t flags = 0;
512 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700513 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700514 flags = ui::Transform::ROT_0;
Mathias Agopian1b031492012-06-20 17:51:20 -0700515 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700516 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700517 flags = ui::Transform::ROT_90;
Mathias Agopian1b031492012-06-20 17:51:20 -0700518 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700519 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700520 flags = ui::Transform::ROT_180;
Mathias Agopian1b031492012-06-20 17:51:20 -0700521 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700522 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700523 flags = ui::Transform::ROT_270;
Mathias Agopian1b031492012-06-20 17:51:20 -0700524 break;
525 default:
526 return BAD_VALUE;
527 }
528 tr->set(flags, w, h);
529 return NO_ERROR;
530}
531
Michael Lentine47e45402014-07-18 15:34:25 -0700532void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
533 dirtyRegion.set(getBounds());
534
Lloyd Pique144e1162017-12-20 16:44:52 -0800535 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700536
Michael Lentine47e45402014-07-18 15:34:25 -0700537 mDisplaySurface->resizeBuffers(newWidth, newHeight);
538
539 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800540 mSurface->setNativeWindow(window);
Alec Mouri05483a02018-09-10 21:03:42 +0000541 mDisplayWidth = mSurface->getWidth();
542 mDisplayHeight = mSurface->getHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700543
544 LOG_FATAL_IF(mDisplayWidth != newWidth,
545 "Unable to set new width to %d", newWidth);
546 LOG_FATAL_IF(mDisplayHeight != newHeight,
547 "Unable to set new height to %d", newHeight);
548}
549
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700550void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800551 const Rect& newViewport, const Rect& newFrame) {
552 Rect viewport(newViewport);
553 Rect frame(newFrame);
554
555 const int w = mDisplayWidth;
556 const int h = mDisplayHeight;
557
Peiyong Linefefaac2018-08-17 12:27:51 -0700558 ui::Transform R;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800559 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
560
561 if (!frame.isValid()) {
562 // the destination frame can be invalid if it has never been set,
563 // in that case we assume the whole display frame.
564 frame = Rect(w, h);
565 }
566
567 if (viewport.isEmpty()) {
568 // viewport can be invalid if it has never been set, in that case
569 // we assume the whole display size.
570 // it's also invalid to have an empty viewport, so we handle that
571 // case in the same way.
572 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700573 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800574 // viewport is always specified in the logical orientation
575 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700576 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800577 }
578 }
579
580 dirtyRegion.set(getBounds());
581
Peiyong Linefefaac2018-08-17 12:27:51 -0700582 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800583 float src_width = viewport.width();
584 float src_height = viewport.height();
585 float dst_width = frame.width();
586 float dst_height = frame.height();
587 if (src_width != dst_width || src_height != dst_height) {
588 float sx = dst_width / src_width;
589 float sy = dst_height / src_height;
590 S.set(sx, 0, 0, sy);
591 }
592
593 float src_x = viewport.left;
594 float src_y = viewport.top;
595 float dst_x = frame.left;
596 float dst_y = frame.top;
597 TL.set(-src_x, -src_y);
598 TP.set(dst_x, dst_y);
599
Iris Chang7501ed62018-04-30 14:45:42 +0800600 // need to take care of primary display rotation for mGlobalTransform
601 // for case if the panel is not installed aligned with device orientation
602 if (mType == DisplayType::DISPLAY_PRIMARY) {
Iris Chang7501ed62018-04-30 14:45:42 +0800603 DisplayDevice::orientationToTransfrom(
Chia-I Wua02871c2018-08-27 14:38:23 -0700604 (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1),
Iris Chang7501ed62018-04-30 14:45:42 +0800605 w, h, &R);
606 }
607
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800608 // The viewport and frame are both in the logical orientation.
609 // Apply the logical translation, scale to physical size, apply the
610 // physical translation and finally rotate to the physical orientation.
611 mGlobalTransform = R * TP * S * TL;
612
613 const uint8_t type = mGlobalTransform.getType();
614 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
Peiyong Linefefaac2018-08-17 12:27:51 -0700615 (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800616
617 mScissor = mGlobalTransform.transform(viewport);
618 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700619 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800620 }
621
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700622 mOrientation = orientation;
Dominik Laskowski281644e2018-04-19 15:47:35 -0700623 if (isPrimary()) {
Pablo Ceballos021623b2016-04-15 17:31:51 -0700624 uint32_t transform = 0;
625 switch (mOrientation) {
626 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700627 transform = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700628 break;
629 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700630 transform = ui::Transform::ROT_90;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700631 break;
632 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700633 transform = ui::Transform::ROT_180;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700634 break;
635 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700636 transform = ui::Transform::ROT_270;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700637 break;
638 }
639 sPrimaryDisplayOrientation = transform;
640 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700641 mViewport = viewport;
642 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700643}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700644
Pablo Ceballos021623b2016-04-15 17:31:51 -0700645uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
646 return sPrimaryDisplayOrientation;
647}
648
Mathias Agopian74d211a2013-04-22 16:55:35 +0200649void DisplayDevice::dump(String8& result) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700650 const ui::Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600651 ANativeWindow* const window = mNativeWindow.get();
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700652 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.c_str());
Dominik Laskowski7e045462018-05-30 13:02:02 -0700653 result.appendFormat(" type=%x, ID=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600654 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
655 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Dominik Laskowski7e045462018-05-30 13:02:02 -0700656 mType, mId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800657 mSurface->queryRedSize(), mSurface->queryGreenSize(),
658 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
659 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600660 mVisibleLayersSortedByZ.size());
661 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
662 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
663 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
664 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
665 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
666 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 -0600667 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700668 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800669 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
670 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800671 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700672 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700673
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700674 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800675 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700676 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700677}
Irvelffc9efc2016-07-27 15:16:37 -0700678
Chia-I Wube02ec02018-05-18 10:59:36 -0700679// Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
680// supported by HWC.
681void DisplayDevice::addColorMode(
682 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
683 const ColorMode mode, const RenderIntent intent) {
684 // find the best color mode
685 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
686
687 // find the best render intent
688 auto iter = hwcColorModes.find(hwcColorMode);
689 const auto& hwcIntents =
690 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
691 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
692
693 const Dataspace dataspace = colorModeToDataspace(mode);
694 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
695
Dominik Laskowski7e045462018-05-30 13:02:02 -0700696 ALOGV("DisplayDevice %d/%d: map (%s, %s) to (%s, %s, %s)", mType, mId,
Chia-I Wube02ec02018-05-18 10:59:36 -0700697 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
698 decodeRenderIntent(intent).c_str(),
699 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
700 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
701
702 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
703}
704
705void DisplayDevice::populateColorModes(
706 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) {
707 if (!hasWideColorGamut()) {
708 return;
709 }
710
Chia-I Wu0d711262018-05-21 15:19:35 -0700711 // collect all known SDR render intents
712 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
713 sSdrRenderIntents.end());
714 auto iter = hwcColorModes.find(ColorMode::SRGB);
715 if (iter != hwcColorModes.end()) {
716 for (auto intent : iter->second) {
717 sdrRenderIntents.insert(intent);
718 }
719 }
720
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700721 // add all known SDR combinations
Chia-I Wu0d711262018-05-21 15:19:35 -0700722 for (auto intent : sdrRenderIntents) {
Chia-I Wube02ec02018-05-18 10:59:36 -0700723 for (auto mode : sSdrColorModes) {
724 addColorMode(hwcColorModes, mode, intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700725 }
726 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700727
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700728 // collect all known HDR render intents
729 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
730 sHdrRenderIntents.end());
731 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
732 if (iter != hwcColorModes.end()) {
733 for (auto intent : iter->second) {
734 hdrRenderIntents.insert(intent);
735 }
736 }
737
738 // add all known HDR combinations
Chia-I Wube02ec02018-05-18 10:59:36 -0700739 for (auto intent : sHdrRenderIntents) {
740 for (auto mode : sHdrColorModes) {
741 addColorMode(hwcColorModes, mode, intent);
742 }
743 }
744}
745
746bool DisplayDevice::hasRenderIntent(RenderIntent intent) const {
747 // assume a render intent is supported when SRGB supports it; we should
748 // get rid of that assumption.
749 auto iter = mColorModes.find(getColorModeKey(Dataspace::SRGB, intent));
750 return iter != mColorModes.end() && iter->second.renderIntent == intent;
751}
752
Peiyong Lindfde5112018-06-05 10:58:41 -0700753bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const {
Chia-I Wube02ec02018-05-18 10:59:36 -0700754 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
755 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
756 auto iter =
757 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
Peiyong Lindfde5112018-06-05 10:58:41 -0700758 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
Chia-I Wube02ec02018-05-18 10:59:36 -0700759 }
760
761 return false;
762}
763
764void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent,
765 Dataspace* outDataspace, ColorMode* outMode,
766 RenderIntent* outIntent) const {
767 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
768 if (iter != mColorModes.end()) {
769 *outDataspace = iter->second.dataspace;
770 *outMode = iter->second.colorMode;
771 *outIntent = iter->second.renderIntent;
772 } else {
Chia-I Wu6333af52018-10-16 13:46:36 -0700773 // this is unexpected on a WCG display
774 if (hasWideColorGamut()) {
775 ALOGE("map unknown (%s)/(%s) to default color mode",
776 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
777 decodeRenderIntent(intent).c_str());
778 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700779
780 *outDataspace = Dataspace::UNKNOWN;
781 *outMode = ColorMode::NATIVE;
782 *outIntent = RenderIntent::COLORIMETRIC;
783 }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700784}
785
Dominik Laskowski663bd282018-04-19 15:26:54 -0700786std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700787
788} // namespace android