blob: f51daf3dad827d0546436b839ec8a6e92baa5d1a [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>
Alec Mouri8147b0e2018-10-09 20:57:19 -070038#include <sync/sync.h>
39#include <system/window.h>
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -060040#include <ui/DebugUtils.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070041#include <ui/DisplayInfo.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070042#include <ui/PixelFormat.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070043#include <utils/Log.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070044#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Jesse Hall99c7dbb2013-03-14 14:29:29 -070046#include "DisplayHardware/DisplaySurface.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070047#include "DisplayHardware/HWComposer.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080048#include "DisplayHardware/HWC2.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070049#include "SurfaceFlinger.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080050#include "Layer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070051
Peiyong Linfd997e02018-03-28 15:29:00 -070052namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Jaesoo Lee720a7242017-01-31 15:26:18 +090054// retrieve triple buffer setting from configstore
55using namespace android::hardware::configstore;
56using namespace android::hardware::configstore::V1_0;
Peiyong Linfd997e02018-03-28 15:29:00 -070057using android::ui::ColorMode;
Chia-I Wube02ec02018-05-18 10:59:36 -070058using android::ui::Dataspace;
Peiyong Lin62665892018-04-16 11:07:44 -070059using android::ui::Hdr;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -080060using android::ui::RenderIntent;
Jaesoo Lee720a7242017-01-31 15:26:18 +090061
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062/*
63 * Initialize the display to the specified values.
64 *
65 */
66
Pablo Ceballos021623b2016-04-15 17:31:51 -070067uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
68
Chia-I Wube02ec02018-05-18 10:59:36 -070069namespace {
70
71// ordered list of known SDR color modes
Valerie Hau9758ae02018-10-09 16:05:09 -070072const std::array<ColorMode, 3> sSdrColorModes = {
73 ColorMode::DISPLAY_BT2020,
Chia-I Wube02ec02018-05-18 10:59:36 -070074 ColorMode::DISPLAY_P3,
75 ColorMode::SRGB,
76};
77
78// ordered list of known HDR color modes
79const std::array<ColorMode, 2> sHdrColorModes = {
80 ColorMode::BT2100_PQ,
81 ColorMode::BT2100_HLG,
82};
83
84// ordered list of known SDR render intents
85const std::array<RenderIntent, 2> sSdrRenderIntents = {
86 RenderIntent::ENHANCE,
87 RenderIntent::COLORIMETRIC,
88};
89
90// ordered list of known HDR render intents
91const std::array<RenderIntent, 2> sHdrRenderIntents = {
92 RenderIntent::TONE_MAP_ENHANCE,
93 RenderIntent::TONE_MAP_COLORIMETRIC,
94};
95
96// map known color mode to dataspace
97Dataspace colorModeToDataspace(ColorMode mode) {
98 switch (mode) {
99 case ColorMode::SRGB:
100 return Dataspace::SRGB;
101 case ColorMode::DISPLAY_P3:
102 return Dataspace::DISPLAY_P3;
Valerie Hau9758ae02018-10-09 16:05:09 -0700103 case ColorMode::DISPLAY_BT2020:
104 return Dataspace::DISPLAY_BT2020;
Chia-I Wube02ec02018-05-18 10:59:36 -0700105 case ColorMode::BT2100_HLG:
106 return Dataspace::BT2020_HLG;
107 case ColorMode::BT2100_PQ:
108 return Dataspace::BT2020_PQ;
109 default:
110 return Dataspace::UNKNOWN;
111 }
112}
113
114// Return a list of candidate color modes.
115std::vector<ColorMode> getColorModeCandidates(ColorMode mode) {
116 std::vector<ColorMode> candidates;
117
118 // add mode itself
119 candidates.push_back(mode);
120
121 // check if mode is HDR
122 bool isHdr = false;
123 for (auto hdrMode : sHdrColorModes) {
124 if (hdrMode == mode) {
125 isHdr = true;
126 break;
127 }
128 }
129
130 // add other HDR candidates when mode is HDR
131 if (isHdr) {
132 for (auto hdrMode : sHdrColorModes) {
133 if (hdrMode != mode) {
134 candidates.push_back(hdrMode);
135 }
136 }
137 }
138
139 // add other SDR candidates
140 for (auto sdrMode : sSdrColorModes) {
141 if (sdrMode != mode) {
142 candidates.push_back(sdrMode);
143 }
144 }
145
146 return candidates;
147}
148
149// Return a list of candidate render intents.
150std::vector<RenderIntent> getRenderIntentCandidates(RenderIntent intent) {
151 std::vector<RenderIntent> candidates;
152
153 // add intent itself
154 candidates.push_back(intent);
155
156 // check if intent is HDR
157 bool isHdr = false;
158 for (auto hdrIntent : sHdrRenderIntents) {
159 if (hdrIntent == intent) {
160 isHdr = true;
161 break;
162 }
163 }
164
Chia-I Wube02ec02018-05-18 10:59:36 -0700165 if (isHdr) {
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700166 // add other HDR candidates when intent is HDR
Chia-I Wube02ec02018-05-18 10:59:36 -0700167 for (auto hdrIntent : sHdrRenderIntents) {
168 if (hdrIntent != intent) {
169 candidates.push_back(hdrIntent);
170 }
171 }
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700172 } else {
173 // add other SDR candidates when intent is SDR
174 for (auto sdrIntent : sSdrRenderIntents) {
175 if (sdrIntent != intent) {
176 candidates.push_back(sdrIntent);
177 }
178 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700179 }
180
181 return candidates;
182}
183
184// Return the best color mode supported by HWC.
185ColorMode getHwcColorMode(
186 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
187 ColorMode mode) {
188 std::vector<ColorMode> candidates = getColorModeCandidates(mode);
189 for (auto candidate : candidates) {
190 auto iter = hwcColorModes.find(candidate);
191 if (iter != hwcColorModes.end()) {
192 return candidate;
193 }
194 }
195
196 return ColorMode::NATIVE;
197}
198
199// Return the best render intent supported by HWC.
200RenderIntent getHwcRenderIntent(const std::vector<RenderIntent>& hwcIntents, RenderIntent intent) {
201 std::vector<RenderIntent> candidates = getRenderIntentCandidates(intent);
202 for (auto candidate : candidates) {
203 for (auto hwcIntent : hwcIntents) {
204 if (candidate == hwcIntent) {
205 return candidate;
206 }
207 }
208 }
209
210 return RenderIntent::COLORIMETRIC;
211}
212
213} // anonymous namespace
214
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700215DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger,
216 const wp<IBinder>& displayToken,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700217 const std::optional<DisplayId>& displayId)
218 : flinger(flinger), displayToken(displayToken), displayId(displayId) {}
Chia-I Wube02ec02018-05-18 10:59:36 -0700219
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700220DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs&& args)
221 : lastCompositionHadVisibleLayers(false),
222 mFlinger(args.flinger),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700223 mDisplayToken(args.displayToken),
Dominik Laskowski075d3172018-05-24 15:50:06 -0700224 mId(args.displayId),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700225 mNativeWindow(args.nativeWindow),
Alec Mouri8147b0e2018-10-09 20:57:19 -0700226 mGraphicBuffer(nullptr),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700227 mDisplaySurface(args.displaySurface),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700228 mDisplayInstallOrientation(args.displayInstallOrientation),
229 mPageFlipCount(0),
Dominik Laskowski075d3172018-05-24 15:50:06 -0700230 mIsVirtual(args.isVirtual),
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700231 mIsSecure(args.isSecure),
232 mLayerStack(NO_LAYER_STACK),
233 mOrientation(),
234 mViewport(Rect::INVALID_RECT),
235 mFrame(Rect::INVALID_RECT),
236 mPowerMode(args.initialPowerMode),
237 mActiveConfig(0),
238 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
239 mHasWideColorGamut(args.hasWideColorGamut),
240 mHasHdr10(false),
241 mHasHLG(false),
242 mHasDolbyVision(false),
Dominik Laskowski075d3172018-05-24 15:50:06 -0700243 mSupportedPerFrameMetadata(args.supportedPerFrameMetadata),
244 mIsPrimary(args.isPrimary) {
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700245 populateColorModes(args.hwcColorModes);
246
247 ALOGE_IF(!mNativeWindow, "No native window was set for display");
248 ALOGE_IF(!mDisplaySurface, "No display surface was set for display");
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700249
250 std::vector<Hdr> types = args.hdrCapabilities.getSupportedHdrTypes();
Peiyong Linfb069302018-04-25 14:34:31 -0700251 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700252 switch (hdrType) {
253 case Hdr::HDR10:
254 mHasHdr10 = true;
255 break;
256 case Hdr::HLG:
257 mHasHLG = true;
258 break;
259 case Hdr::DOLBY_VISION:
260 mHasDolbyVision = true;
261 break;
262 default:
263 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
264 }
265 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700266
Lloyd Pique2eef1d22018-09-18 21:30:04 -0700267 float minLuminance = args.hdrCapabilities.getDesiredMinLuminance();
268 float maxLuminance = args.hdrCapabilities.getDesiredMaxLuminance();
269 float maxAverageLuminance = args.hdrCapabilities.getDesiredMaxAverageLuminance();
Peiyong Linfb069302018-04-25 14:34:31 -0700270
271 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
272 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
273 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
274 if (this->hasWideColorGamut()) {
275 // insert HDR10/HLG as we will force client composition for HDR10/HLG
276 // layers
277 if (!hasHDR10Support()) {
278 types.push_back(Hdr::HDR10);
279 }
280
281 if (!hasHLGSupport()) {
282 types.push_back(Hdr::HLG);
283 }
284 }
285 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
286
Alec Mouriba013fa2018-10-16 12:43:11 -0700287 ANativeWindow* const window = mNativeWindow.get();
Alec Mouri8147b0e2018-10-09 20:57:19 -0700288
289 int status = native_window_api_connect(mNativeWindow.get(), NATIVE_WINDOW_API_EGL);
290 ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status);
291
Alec Mouriba013fa2018-10-16 12:43:11 -0700292 mDisplayWidth = ANativeWindow_getWidth(window);
293 mDisplayHeight = ANativeWindow_getHeight(window);
294
Jesse Hallffe1f192013-03-22 15:13:48 -0700295 // initialize the display orientation transform.
296 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297}
298
Lloyd Pique09594832018-01-22 17:48:03 -0800299DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700300
Jesse Hall02d86562013-03-25 14:43:23 -0700301void DisplayDevice::disconnect(HWComposer& hwc) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700302 if (mId) {
303 hwc.disconnectDisplay(*mId);
304 mId.reset();
Jesse Hall02d86562013-03-25 14:43:23 -0700305 }
306}
307
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700308int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700309 return mDisplayWidth;
310}
311
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700312int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700313 return mDisplayHeight;
314}
315
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700316void DisplayDevice::setDisplayName(const std::string& displayName) {
317 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700318 // never override the name with an empty name
319 mDisplayName = displayName;
320 }
321}
322
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700323uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325}
326
Chia-I Wub02087d2017-11-09 10:19:54 -0800327void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700329 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330}
331
Dan Stoza71433162014-02-04 16:22:36 -0800332status_t DisplayDevice::beginFrame(bool mustRecompose) const {
333 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700334}
335
David Sodmanfb95bcc2017-12-22 15:45:30 -0800336status_t DisplayDevice::prepareFrame(HWComposer& hwc,
337 std::vector<CompositionInfo>& compositionData) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700338 if (mId) {
339 status_t error = hwc.prepare(*mId, compositionData);
340 if (error != NO_ERROR) {
341 return error;
342 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800343 }
344
345 DisplaySurface::CompositionType compositionType;
Dominik Laskowski7e045462018-05-30 13:02:02 -0700346 bool hasClient = hwc.hasClientComposition(mId);
347 bool hasDevice = hwc.hasDeviceComposition(mId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800348 if (hasClient && hasDevice) {
349 compositionType = DisplaySurface::COMPOSITION_MIXED;
350 } else if (hasClient) {
351 compositionType = DisplaySurface::COMPOSITION_GLES;
352 } else if (hasDevice) {
353 compositionType = DisplaySurface::COMPOSITION_HWC;
354 } else {
355 // Nothing to do -- when turning the screen off we get a frame like
356 // this. Call it a HWC frame since we won't be doing any GLES work but
357 // will do a prepare/set cycle.
358 compositionType = DisplaySurface::COMPOSITION_HWC;
359 }
360 return mDisplaySurface->prepareFrame(compositionType);
361}
Jesse Hall38efe862013-04-06 23:12:29 -0700362
Alec Mouri8147b0e2018-10-09 20:57:19 -0700363sp<GraphicBuffer> DisplayDevice::dequeueBuffer() {
364 int fd;
365 ANativeWindowBuffer* buffer;
366
367 status_t res = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd);
368
369 if (res != NO_ERROR) {
370 ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d",
371 getDisplayName().c_str(), res);
372 // Return fast here as we can't do much more - any rendering we do
373 // now will just be wrong.
374 return mGraphicBuffer;
375 }
376
377 ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].",
378 mGraphicBuffer->getNativeBuffer()->handle);
379 mGraphicBuffer = GraphicBuffer::from(buffer);
380
381 // Block until the buffer is ready
382 // TODO(alecmouri): it's perhaps more appropriate to block renderengine so
383 // that the gl driver can block instead.
384 if (fd >= 0) {
385 sync_wait(fd, -1);
386 close(fd);
387 }
388
389 return mGraphicBuffer;
390}
391
392void DisplayDevice::queueBuffer(HWComposer& hwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700393 if (hwc.hasClientComposition(mId) || hwc.hasFlipClientTargetRequest(mId)) {
Alec Mouri8147b0e2018-10-09 20:57:19 -0700394 // hasFlipClientTargetRequest could return true even if we haven't
395 // dequeued a buffer before. Try dequeueing one if we don't have a
396 // buffer ready.
397 if (mGraphicBuffer == nullptr) {
398 ALOGI("Attempting to queue a client composited buffer without one "
399 "previously dequeued for display [%s]. Attempting to dequeue "
400 "a scratch buffer now",
401 mDisplayName.c_str());
402 // We shouldn't deadlock here, since mGraphicBuffer == nullptr only
403 // after a successful call to queueBuffer, or if dequeueBuffer has
404 // never been called.
405 dequeueBuffer();
406 }
407
408 if (mGraphicBuffer == nullptr) {
409 ALOGE("No buffer is ready for display [%s]", mDisplayName.c_str());
410 } else {
411 int fd = mBufferReady.release();
412
413 status_t res = mNativeWindow->queueBuffer(mNativeWindow.get(),
414 mGraphicBuffer->getNativeBuffer(), fd);
415 if (res != NO_ERROR) {
416 ALOGE("Error when queueing buffer for display [%s]: %d", mDisplayName.c_str(), res);
417 // We risk blocking on dequeueBuffer if the primary display failed
418 // to queue up its buffer, so crash here.
419 if (isPrimary()) {
420 LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", res);
421 } else {
422 mNativeWindow->cancelBuffer(mNativeWindow.get(),
423 mGraphicBuffer->getNativeBuffer(), fd);
424 }
425 }
426 mGraphicBuffer = nullptr;
427 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700428 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700429
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700430 status_t result = mDisplaySurface->advanceFrame();
431 if (result != NO_ERROR) {
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700432 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplayName.c_str(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700433 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700434}
435
Alec Mouri8147b0e2018-10-09 20:57:19 -0700436void DisplayDevice::onPresentDisplayCompleted() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800437 mDisplaySurface->onFrameCommitted();
438}
Mathias Agopianda27af92012-09-13 18:17:13 -0700439
Mathias Agopian875d8e12013-06-07 15:35:48 -0700440void DisplayDevice::setViewportAndProjection() const {
441 size_t w = mDisplayWidth;
442 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700443 Rect sourceCrop(0, 0, w, h);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700444 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, ui::Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700445}
446
Alec Mouri8147b0e2018-10-09 20:57:19 -0700447void DisplayDevice::finishBuffer() {
448 mBufferReady = mFlinger->getRenderEngine().flush();
449 if (mBufferReady.get() < 0) {
450 mFlinger->getRenderEngine().finish();
451 }
452}
453
Dan Stoza9e56aa02015-11-02 13:00:03 -0800454const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
455 return mDisplaySurface->getClientTargetAcquireFence();
456}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800457
Mathias Agopian1b031492012-06-20 17:51:20 -0700458// ----------------------------------------------------------------------------
459
Mathias Agopian13127d82013-03-05 17:47:11 -0800460void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700461 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700462}
463
Mathias Agopian13127d82013-03-05 17:47:11 -0800464const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700465 return mVisibleLayersSortedByZ;
466}
467
Chia-I Wu83806892017-11-16 10:50:20 -0800468void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
469 mLayersNeedingFences = layers;
470}
471
472const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
473 return mLayersNeedingFences;
474}
475
Mathias Agopiancd60f992012-08-16 16:28:27 -0700476Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
477 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700478 if (repaintEverything) {
479 dirty.set(getBounds());
480 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700481 const ui::Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700482 dirty = planeTransform.transform(this->dirtyRegion);
483 dirty.andSelf(getBounds());
484 }
485 return dirty;
486}
487
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700488// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700489void DisplayDevice::setPowerMode(int mode) {
490 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700491}
492
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700493int DisplayDevice::getPowerMode() const {
494 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700495}
496
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700497bool DisplayDevice::isPoweredOn() const {
498 return mPowerMode != HWC_POWER_MODE_OFF;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700499}
500
501// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700502void DisplayDevice::setActiveConfig(int mode) {
503 mActiveConfig = mode;
504}
505
506int DisplayDevice::getActiveConfig() const {
507 return mActiveConfig;
508}
509
510// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700511void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700512 mActiveColorMode = mode;
513}
514
Peiyong Lina52f0292018-03-14 17:26:31 -0700515ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700516 return mActiveColorMode;
517}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600518
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800519RenderIntent DisplayDevice::getActiveRenderIntent() const {
520 return mActiveRenderIntent;
521}
522
523void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
524 mActiveRenderIntent = renderIntent;
525}
526
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800527void DisplayDevice::setColorTransform(const mat4& transform) {
528 const bool isIdentity = (transform == mat4());
529 mColorTransform =
530 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
531}
532
533android_color_transform_t DisplayDevice::getColorTransform() const {
534 return mColorTransform;
535}
536
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700537void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800538 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600539 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700540 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600541}
Michael Wright28f24d02016-07-12 13:30:53 -0700542
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800543ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
544 return mCompositionDataSpace;
545}
546
Michael Wright28f24d02016-07-12 13:30:53 -0700547// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700548
Mathias Agopian28947d72012-08-08 18:51:15 -0700549void DisplayDevice::setLayerStack(uint32_t stack) {
550 mLayerStack = stack;
551 dirtyRegion.set(bounds());
552}
553
554// ----------------------------------------------------------------------------
555
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700556uint32_t DisplayDevice::getOrientationTransform() const {
557 uint32_t transform = 0;
558 switch (mOrientation) {
559 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700560 transform = ui::Transform::ROT_0;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700561 break;
562 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700563 transform = ui::Transform::ROT_90;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700564 break;
565 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700566 transform = ui::Transform::ROT_180;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700567 break;
568 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700569 transform = ui::Transform::ROT_270;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700570 break;
571 }
572 return transform;
573}
574
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700575status_t DisplayDevice::orientationToTransfrom(
Peiyong Linefefaac2018-08-17 12:27:51 -0700576 int orientation, int w, int h, ui::Transform* tr)
Mathias Agopian1b031492012-06-20 17:51:20 -0700577{
578 uint32_t flags = 0;
579 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700580 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700581 flags = ui::Transform::ROT_0;
Mathias Agopian1b031492012-06-20 17:51:20 -0700582 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700583 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700584 flags = ui::Transform::ROT_90;
Mathias Agopian1b031492012-06-20 17:51:20 -0700585 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700586 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700587 flags = ui::Transform::ROT_180;
Mathias Agopian1b031492012-06-20 17:51:20 -0700588 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700589 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700590 flags = ui::Transform::ROT_270;
Mathias Agopian1b031492012-06-20 17:51:20 -0700591 break;
592 default:
593 return BAD_VALUE;
594 }
595 tr->set(flags, w, h);
596 return NO_ERROR;
597}
598
Michael Lentine47e45402014-07-18 15:34:25 -0700599void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
600 dirtyRegion.set(getBounds());
601
602 mDisplaySurface->resizeBuffers(newWidth, newHeight);
603
Alec Mouriba013fa2018-10-16 12:43:11 -0700604 mDisplayWidth = newWidth;
605 mDisplayHeight = newHeight;
Michael Lentine47e45402014-07-18 15:34:25 -0700606}
607
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700608void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800609 const Rect& newViewport, const Rect& newFrame) {
610 Rect viewport(newViewport);
611 Rect frame(newFrame);
612
613 const int w = mDisplayWidth;
614 const int h = mDisplayHeight;
615
Peiyong Linefefaac2018-08-17 12:27:51 -0700616 ui::Transform R;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800617 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
618
619 if (!frame.isValid()) {
620 // the destination frame can be invalid if it has never been set,
621 // in that case we assume the whole display frame.
622 frame = Rect(w, h);
623 }
624
625 if (viewport.isEmpty()) {
626 // viewport can be invalid if it has never been set, in that case
627 // we assume the whole display size.
628 // it's also invalid to have an empty viewport, so we handle that
629 // case in the same way.
630 viewport = Rect(w, h);
Peiyong Linefefaac2018-08-17 12:27:51 -0700631 if (R.getOrientation() & ui::Transform::ROT_90) {
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800632 // viewport is always specified in the logical orientation
633 // of the display (ie: post-rotation).
Peiyong Lin3db42342018-08-16 09:15:59 -0700634 std::swap(viewport.right, viewport.bottom);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800635 }
636 }
637
638 dirtyRegion.set(getBounds());
639
Peiyong Linefefaac2018-08-17 12:27:51 -0700640 ui::Transform TL, TP, S;
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800641 float src_width = viewport.width();
642 float src_height = viewport.height();
643 float dst_width = frame.width();
644 float dst_height = frame.height();
645 if (src_width != dst_width || src_height != dst_height) {
646 float sx = dst_width / src_width;
647 float sy = dst_height / src_height;
648 S.set(sx, 0, 0, sy);
649 }
650
651 float src_x = viewport.left;
652 float src_y = viewport.top;
653 float dst_x = frame.left;
654 float dst_y = frame.top;
655 TL.set(-src_x, -src_y);
656 TP.set(dst_x, dst_y);
657
Iris Chang7501ed62018-04-30 14:45:42 +0800658 // need to take care of primary display rotation for mGlobalTransform
659 // for case if the panel is not installed aligned with device orientation
Dominik Laskowski075d3172018-05-24 15:50:06 -0700660 if (isPrimary()) {
Iris Chang7501ed62018-04-30 14:45:42 +0800661 DisplayDevice::orientationToTransfrom(
Chia-I Wua02871c2018-08-27 14:38:23 -0700662 (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1),
Iris Chang7501ed62018-04-30 14:45:42 +0800663 w, h, &R);
664 }
665
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800666 // The viewport and frame are both in the logical orientation.
667 // Apply the logical translation, scale to physical size, apply the
668 // physical translation and finally rotate to the physical orientation.
669 mGlobalTransform = R * TP * S * TL;
670
671 const uint8_t type = mGlobalTransform.getType();
672 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
Peiyong Linefefaac2018-08-17 12:27:51 -0700673 (type >= ui::Transform::SCALE));
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800674
675 mScissor = mGlobalTransform.transform(viewport);
676 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700677 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800678 }
679
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700680 mOrientation = orientation;
Dominik Laskowski281644e2018-04-19 15:47:35 -0700681 if (isPrimary()) {
Pablo Ceballos021623b2016-04-15 17:31:51 -0700682 uint32_t transform = 0;
683 switch (mOrientation) {
684 case DisplayState::eOrientationDefault:
Peiyong Linefefaac2018-08-17 12:27:51 -0700685 transform = ui::Transform::ROT_0;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700686 break;
687 case DisplayState::eOrientation90:
Peiyong Linefefaac2018-08-17 12:27:51 -0700688 transform = ui::Transform::ROT_90;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700689 break;
690 case DisplayState::eOrientation180:
Peiyong Linefefaac2018-08-17 12:27:51 -0700691 transform = ui::Transform::ROT_180;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700692 break;
693 case DisplayState::eOrientation270:
Peiyong Linefefaac2018-08-17 12:27:51 -0700694 transform = ui::Transform::ROT_270;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700695 break;
696 }
697 sPrimaryDisplayOrientation = transform;
698 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700699 mViewport = viewport;
700 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700701}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700702
Pablo Ceballos021623b2016-04-15 17:31:51 -0700703uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
704 return sPrimaryDisplayOrientation;
705}
706
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700707std::string DisplayDevice::getDebugName() const {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700708 const auto id = mId ? base::StringPrintf("%" PRIu64 ", ", *mId) : std::string();
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700709 return base::StringPrintf("DisplayDevice{%s%s%s\"%s\"}", id.c_str(),
710 isPrimary() ? "primary, " : "", isVirtual() ? "virtual, " : "",
711 mDisplayName.c_str());
712}
713
Mathias Agopian74d211a2013-04-22 16:55:35 +0200714void DisplayDevice::dump(String8& result) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700715 const ui::Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600716 ANativeWindow* const window = mNativeWindow.get();
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700717 result.appendFormat("+ %s\n", getDebugName().c_str());
718 result.appendFormat(" layerStack=%u, (%4dx%4d), ANativeWindow=%p "
Alec Mourice0ad962018-10-17 14:33:41 -0700719 "format=%d, orient=%2d (type=%08x), flips=%u, isSecure=%d, "
720 "powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700721 mLayerStack, mDisplayWidth, mDisplayHeight, window,
Alec Mourice0ad962018-10-17 14:33:41 -0700722 ANativeWindow_getFormat(window), mOrientation, tr.getType(),
723 getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600724 mVisibleLayersSortedByZ.size());
725 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
726 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
727 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
728 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
729 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
730 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 -0600731 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700732 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800733 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
Alec Mourice0ad962018-10-17 14:33:41 -0700734 mHasWideColorGamut, mHasHdr10, decodeColorMode(mActiveColorMode).c_str(),
735 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(),
736 dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700737
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700738 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800739 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700740 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700741}
Irvelffc9efc2016-07-27 15:16:37 -0700742
Chia-I Wube02ec02018-05-18 10:59:36 -0700743// Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
744// supported by HWC.
745void DisplayDevice::addColorMode(
746 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
747 const ColorMode mode, const RenderIntent intent) {
748 // find the best color mode
749 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
750
751 // find the best render intent
752 auto iter = hwcColorModes.find(hwcColorMode);
753 const auto& hwcIntents =
754 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
755 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
756
757 const Dataspace dataspace = colorModeToDataspace(mode);
758 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
759
Dominik Laskowski9b17b2c22018-11-01 14:49:03 -0700760 ALOGV("%s: map (%s, %s) to (%s, %s, %s)", getDebugName().c_str(),
Chia-I Wube02ec02018-05-18 10:59:36 -0700761 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
762 decodeRenderIntent(intent).c_str(),
763 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
764 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
765
766 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
767}
768
769void DisplayDevice::populateColorModes(
770 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) {
771 if (!hasWideColorGamut()) {
772 return;
773 }
774
Chia-I Wu0d711262018-05-21 15:19:35 -0700775 // collect all known SDR render intents
776 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
777 sSdrRenderIntents.end());
778 auto iter = hwcColorModes.find(ColorMode::SRGB);
779 if (iter != hwcColorModes.end()) {
780 for (auto intent : iter->second) {
781 sdrRenderIntents.insert(intent);
782 }
783 }
784
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700785 // add all known SDR combinations
Chia-I Wu0d711262018-05-21 15:19:35 -0700786 for (auto intent : sdrRenderIntents) {
Chia-I Wube02ec02018-05-18 10:59:36 -0700787 for (auto mode : sSdrColorModes) {
788 addColorMode(hwcColorModes, mode, intent);
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700789 }
790 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700791
Chia-I Wuc4b08bd2018-05-29 12:57:23 -0700792 // collect all known HDR render intents
793 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
794 sHdrRenderIntents.end());
795 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
796 if (iter != hwcColorModes.end()) {
797 for (auto intent : iter->second) {
798 hdrRenderIntents.insert(intent);
799 }
800 }
801
802 // add all known HDR combinations
Chia-I Wube02ec02018-05-18 10:59:36 -0700803 for (auto intent : sHdrRenderIntents) {
804 for (auto mode : sHdrColorModes) {
805 addColorMode(hwcColorModes, mode, intent);
806 }
807 }
808}
809
810bool DisplayDevice::hasRenderIntent(RenderIntent intent) const {
811 // assume a render intent is supported when SRGB supports it; we should
812 // get rid of that assumption.
813 auto iter = mColorModes.find(getColorModeKey(Dataspace::SRGB, intent));
814 return iter != mColorModes.end() && iter->second.renderIntent == intent;
815}
816
Peiyong Lindfde5112018-06-05 10:58:41 -0700817bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const {
Chia-I Wube02ec02018-05-18 10:59:36 -0700818 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
819 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
820 auto iter =
821 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
Peiyong Lindfde5112018-06-05 10:58:41 -0700822 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
Chia-I Wube02ec02018-05-18 10:59:36 -0700823 }
824
825 return false;
826}
827
828void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent,
829 Dataspace* outDataspace, ColorMode* outMode,
830 RenderIntent* outIntent) const {
831 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
832 if (iter != mColorModes.end()) {
833 *outDataspace = iter->second.dataspace;
834 *outMode = iter->second.colorMode;
835 *outIntent = iter->second.renderIntent;
836 } else {
Chia-I Wu6333af52018-10-16 13:46:36 -0700837 // this is unexpected on a WCG display
838 if (hasWideColorGamut()) {
839 ALOGE("map unknown (%s)/(%s) to default color mode",
840 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
841 decodeRenderIntent(intent).c_str());
842 }
Chia-I Wube02ec02018-05-18 10:59:36 -0700843
844 *outDataspace = Dataspace::UNKNOWN;
845 *outMode = ColorMode::NATIVE;
846 *outIntent = RenderIntent::COLORIMETRIC;
847 }
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700848}
849
Dominik Laskowski663bd282018-04-19 15:26:54 -0700850std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700851
852} // namespace android