The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "DisplayDevice" |
| 20 | |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 21 | #include "DisplayDevice.h" |
| 22 | |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 23 | #include <array> |
| 24 | #include <unordered_set> |
| 25 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <math.h> |
| 30 | |
Dominik Laskowski | 9b17b2c2 | 2018-11-01 14:49:03 -0700 | [diff] [blame] | 31 | #include <android-base/stringprintf.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 32 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 33 | #include <compositionengine/CompositionEngine.h> |
| 34 | #include <compositionengine/Display.h> |
| 35 | #include <compositionengine/DisplayCreationArgs.h> |
Lloyd Pique | 542307f | 2018-10-19 13:24:08 -0700 | [diff] [blame^] | 36 | #include <compositionengine/DisplaySurface.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 37 | #include <compositionengine/impl/OutputCompositionState.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 38 | #include <configstore/Utils.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | #include <cutils/properties.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 40 | #include <gui/Surface.h> |
| 41 | #include <hardware/gralloc.h> |
| 42 | #include <renderengine/RenderEngine.h> |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 43 | #include <sync/sync.h> |
| 44 | #include <system/window.h> |
Courtney Goeltzenleuchter | 152279d | 2017-08-14 18:18:30 -0600 | [diff] [blame] | 45 | #include <ui/DebugUtils.h> |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 46 | #include <ui/DisplayInfo.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 47 | #include <ui/PixelFormat.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 48 | #include <utils/Log.h> |
Valerie Hau | 9758ae0 | 2018-10-09 16:05:09 -0700 | [diff] [blame] | 49 | #include <utils/RefBase.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 51 | #include "DisplayHardware/HWComposer.h" |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 52 | #include "DisplayHardware/HWC2.h" |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 53 | #include "SurfaceFlinger.h" |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 54 | #include "Layer.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 55 | |
Peiyong Lin | fd997e0 | 2018-03-28 15:29:00 -0700 | [diff] [blame] | 56 | namespace android { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | |
Jaesoo Lee | 720a724 | 2017-01-31 15:26:18 +0900 | [diff] [blame] | 58 | // retrieve triple buffer setting from configstore |
| 59 | using namespace android::hardware::configstore; |
| 60 | using namespace android::hardware::configstore::V1_0; |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 61 | using android::base::StringAppendF; |
Peiyong Lin | fd997e0 | 2018-03-28 15:29:00 -0700 | [diff] [blame] | 62 | using android::ui::ColorMode; |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 63 | using android::ui::Dataspace; |
Peiyong Lin | 6266589 | 2018-04-16 11:07:44 -0700 | [diff] [blame] | 64 | using android::ui::Hdr; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 65 | using android::ui::RenderIntent; |
Jaesoo Lee | 720a724 | 2017-01-31 15:26:18 +0900 | [diff] [blame] | 66 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | /* |
| 68 | * Initialize the display to the specified values. |
| 69 | * |
| 70 | */ |
| 71 | |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 72 | uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0; |
| 73 | |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 74 | namespace { |
| 75 | |
| 76 | // ordered list of known SDR color modes |
Valerie Hau | 9758ae0 | 2018-10-09 16:05:09 -0700 | [diff] [blame] | 77 | const std::array<ColorMode, 3> sSdrColorModes = { |
| 78 | ColorMode::DISPLAY_BT2020, |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 79 | ColorMode::DISPLAY_P3, |
| 80 | ColorMode::SRGB, |
| 81 | }; |
| 82 | |
| 83 | // ordered list of known HDR color modes |
| 84 | const std::array<ColorMode, 2> sHdrColorModes = { |
| 85 | ColorMode::BT2100_PQ, |
| 86 | ColorMode::BT2100_HLG, |
| 87 | }; |
| 88 | |
| 89 | // ordered list of known SDR render intents |
| 90 | const std::array<RenderIntent, 2> sSdrRenderIntents = { |
| 91 | RenderIntent::ENHANCE, |
| 92 | RenderIntent::COLORIMETRIC, |
| 93 | }; |
| 94 | |
| 95 | // ordered list of known HDR render intents |
| 96 | const std::array<RenderIntent, 2> sHdrRenderIntents = { |
| 97 | RenderIntent::TONE_MAP_ENHANCE, |
| 98 | RenderIntent::TONE_MAP_COLORIMETRIC, |
| 99 | }; |
| 100 | |
| 101 | // map known color mode to dataspace |
| 102 | Dataspace colorModeToDataspace(ColorMode mode) { |
| 103 | switch (mode) { |
| 104 | case ColorMode::SRGB: |
Peiyong Lin | 14724e6 | 2018-12-05 07:27:30 -0800 | [diff] [blame] | 105 | return Dataspace::V0_SRGB; |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 106 | case ColorMode::DISPLAY_P3: |
| 107 | return Dataspace::DISPLAY_P3; |
Valerie Hau | 9758ae0 | 2018-10-09 16:05:09 -0700 | [diff] [blame] | 108 | case ColorMode::DISPLAY_BT2020: |
| 109 | return Dataspace::DISPLAY_BT2020; |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 110 | case ColorMode::BT2100_HLG: |
| 111 | return Dataspace::BT2020_HLG; |
| 112 | case ColorMode::BT2100_PQ: |
| 113 | return Dataspace::BT2020_PQ; |
| 114 | default: |
| 115 | return Dataspace::UNKNOWN; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Return a list of candidate color modes. |
| 120 | std::vector<ColorMode> getColorModeCandidates(ColorMode mode) { |
| 121 | std::vector<ColorMode> candidates; |
| 122 | |
| 123 | // add mode itself |
| 124 | candidates.push_back(mode); |
| 125 | |
| 126 | // check if mode is HDR |
| 127 | bool isHdr = false; |
| 128 | for (auto hdrMode : sHdrColorModes) { |
| 129 | if (hdrMode == mode) { |
| 130 | isHdr = true; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // add other HDR candidates when mode is HDR |
| 136 | if (isHdr) { |
| 137 | for (auto hdrMode : sHdrColorModes) { |
| 138 | if (hdrMode != mode) { |
| 139 | candidates.push_back(hdrMode); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // add other SDR candidates |
| 145 | for (auto sdrMode : sSdrColorModes) { |
| 146 | if (sdrMode != mode) { |
| 147 | candidates.push_back(sdrMode); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return candidates; |
| 152 | } |
| 153 | |
| 154 | // Return a list of candidate render intents. |
| 155 | std::vector<RenderIntent> getRenderIntentCandidates(RenderIntent intent) { |
| 156 | std::vector<RenderIntent> candidates; |
| 157 | |
| 158 | // add intent itself |
| 159 | candidates.push_back(intent); |
| 160 | |
| 161 | // check if intent is HDR |
| 162 | bool isHdr = false; |
| 163 | for (auto hdrIntent : sHdrRenderIntents) { |
| 164 | if (hdrIntent == intent) { |
| 165 | isHdr = true; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 170 | if (isHdr) { |
Chia-I Wu | c4b08bd | 2018-05-29 12:57:23 -0700 | [diff] [blame] | 171 | // add other HDR candidates when intent is HDR |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 172 | for (auto hdrIntent : sHdrRenderIntents) { |
| 173 | if (hdrIntent != intent) { |
| 174 | candidates.push_back(hdrIntent); |
| 175 | } |
| 176 | } |
Chia-I Wu | c4b08bd | 2018-05-29 12:57:23 -0700 | [diff] [blame] | 177 | } else { |
| 178 | // add other SDR candidates when intent is SDR |
| 179 | for (auto sdrIntent : sSdrRenderIntents) { |
| 180 | if (sdrIntent != intent) { |
| 181 | candidates.push_back(sdrIntent); |
| 182 | } |
| 183 | } |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | return candidates; |
| 187 | } |
| 188 | |
| 189 | // Return the best color mode supported by HWC. |
| 190 | ColorMode getHwcColorMode( |
| 191 | const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes, |
| 192 | ColorMode mode) { |
| 193 | std::vector<ColorMode> candidates = getColorModeCandidates(mode); |
| 194 | for (auto candidate : candidates) { |
| 195 | auto iter = hwcColorModes.find(candidate); |
| 196 | if (iter != hwcColorModes.end()) { |
| 197 | return candidate; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return ColorMode::NATIVE; |
| 202 | } |
| 203 | |
| 204 | // Return the best render intent supported by HWC. |
| 205 | RenderIntent getHwcRenderIntent(const std::vector<RenderIntent>& hwcIntents, RenderIntent intent) { |
| 206 | std::vector<RenderIntent> candidates = getRenderIntentCandidates(intent); |
| 207 | for (auto candidate : candidates) { |
| 208 | for (auto hwcIntent : hwcIntents) { |
| 209 | if (candidate == hwcIntent) { |
| 210 | return candidate; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return RenderIntent::COLORIMETRIC; |
| 216 | } |
| 217 | |
| 218 | } // anonymous namespace |
| 219 | |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 220 | DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger, |
| 221 | const wp<IBinder>& displayToken, |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 222 | const std::optional<DisplayId>& displayId) |
| 223 | : flinger(flinger), displayToken(displayToken), displayId(displayId) {} |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 224 | |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 225 | DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs&& args) |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 226 | : mFlinger(args.flinger), |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 227 | mDisplayToken(args.displayToken), |
Dominik Laskowski | e977409 | 2018-12-11 10:04:24 -0800 | [diff] [blame] | 228 | mSequenceId(args.sequenceId), |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 229 | mDisplayInstallOrientation(args.displayInstallOrientation), |
| 230 | mCompositionDisplay{mFlinger->getCompositionEngine().createDisplay( |
| 231 | compositionengine::DisplayCreationArgs{args.isSecure, args.isVirtual, |
| 232 | args.displayId})}, |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 233 | mNativeWindow(args.nativeWindow), |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 234 | mGraphicBuffer(nullptr), |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 235 | mDisplaySurface(args.displaySurface), |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 236 | mPageFlipCount(0), |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 237 | mIsVirtual(args.isVirtual), |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 238 | mOrientation(), |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 239 | mActiveConfig(0), |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 240 | mHasWideColorGamut(args.hasWideColorGamut), |
Valerie Hau | e9e843a | 2018-12-18 13:39:23 -0800 | [diff] [blame] | 241 | mHasHdr10Plus(false), |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 242 | mHasHdr10(false), |
| 243 | mHasHLG(false), |
| 244 | mHasDolbyVision(false), |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 245 | mSupportedPerFrameMetadata(args.supportedPerFrameMetadata), |
| 246 | mIsPrimary(args.isPrimary) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 247 | if (!mCompositionDisplay->isValid()) { |
| 248 | ALOGE("Composition Display did not validate!"); |
| 249 | } |
| 250 | |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 251 | populateColorModes(args.hwcColorModes); |
| 252 | |
| 253 | ALOGE_IF(!mNativeWindow, "No native window was set for display"); |
| 254 | ALOGE_IF(!mDisplaySurface, "No display surface was set for display"); |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 255 | |
| 256 | std::vector<Hdr> types = args.hdrCapabilities.getSupportedHdrTypes(); |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 257 | for (Hdr hdrType : types) { |
Peiyong Lin | 6266589 | 2018-04-16 11:07:44 -0700 | [diff] [blame] | 258 | switch (hdrType) { |
Valerie Hau | e9e843a | 2018-12-18 13:39:23 -0800 | [diff] [blame] | 259 | case Hdr::HDR10_PLUS: |
| 260 | mHasHdr10Plus = true; |
| 261 | break; |
Peiyong Lin | 6266589 | 2018-04-16 11:07:44 -0700 | [diff] [blame] | 262 | case Hdr::HDR10: |
| 263 | mHasHdr10 = true; |
| 264 | break; |
| 265 | case Hdr::HLG: |
| 266 | mHasHLG = true; |
| 267 | break; |
| 268 | case Hdr::DOLBY_VISION: |
| 269 | mHasDolbyVision = true; |
| 270 | break; |
| 271 | default: |
| 272 | ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType)); |
| 273 | } |
| 274 | } |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 275 | |
Lloyd Pique | 2eef1d2 | 2018-09-18 21:30:04 -0700 | [diff] [blame] | 276 | float minLuminance = args.hdrCapabilities.getDesiredMinLuminance(); |
| 277 | float maxLuminance = args.hdrCapabilities.getDesiredMaxLuminance(); |
| 278 | float maxAverageLuminance = args.hdrCapabilities.getDesiredMaxAverageLuminance(); |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 279 | |
| 280 | minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance; |
| 281 | maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance; |
| 282 | maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance; |
| 283 | if (this->hasWideColorGamut()) { |
| 284 | // insert HDR10/HLG as we will force client composition for HDR10/HLG |
| 285 | // layers |
| 286 | if (!hasHDR10Support()) { |
Valerie Hau | e9e843a | 2018-12-18 13:39:23 -0800 | [diff] [blame] | 287 | types.push_back(Hdr::HDR10); |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | if (!hasHLGSupport()) { |
Valerie Hau | e9e843a | 2018-12-18 13:39:23 -0800 | [diff] [blame] | 291 | types.push_back(Hdr::HLG); |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance); |
| 295 | |
Alec Mouri | ba013fa | 2018-10-16 12:43:11 -0700 | [diff] [blame] | 296 | ANativeWindow* const window = mNativeWindow.get(); |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 297 | |
Alec Mouri | 4efb6c2 | 2018-11-16 13:07:47 -0800 | [diff] [blame] | 298 | int status = native_window_api_connect(window, NATIVE_WINDOW_API_EGL); |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 299 | ALOGE_IF(status != NO_ERROR, "Unable to connect BQ producer: %d", status); |
Alec Mouri | 4efb6c2 | 2018-11-16 13:07:47 -0800 | [diff] [blame] | 300 | status = native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888); |
| 301 | ALOGE_IF(status != NO_ERROR, "Unable to set BQ format to RGBA888: %d", status); |
| 302 | status = native_window_set_usage(window, GRALLOC_USAGE_HW_RENDER); |
| 303 | ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for GPU rendering: %d", status); |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 304 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 305 | mCompositionDisplay->setBounds( |
| 306 | Rect(ANativeWindow_getWidth(window), ANativeWindow_getHeight(window))); |
| 307 | |
| 308 | setPowerMode(args.initialPowerMode); |
Alec Mouri | ba013fa | 2018-10-16 12:43:11 -0700 | [diff] [blame] | 309 | |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 310 | // initialize the display orientation transform. |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 311 | setProjection(DisplayState::eOrientationDefault, Rect::INVALID_RECT, Rect::INVALID_RECT); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Lloyd Pique | 0959483 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 314 | DisplayDevice::~DisplayDevice() = default; |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 315 | |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 316 | void DisplayDevice::disconnect() { |
| 317 | mCompositionDisplay->disconnect(); |
Jesse Hall | 02d8656 | 2013-03-25 14:43:23 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 320 | int DisplayDevice::getWidth() const { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 321 | return mCompositionDisplay->getState().bounds.getWidth(); |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 324 | int DisplayDevice::getHeight() const { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 325 | return mCompositionDisplay->getState().bounds.getHeight(); |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Dominik Laskowski | bf170d9 | 2018-04-19 15:08:05 -0700 | [diff] [blame] | 328 | void DisplayDevice::setDisplayName(const std::string& displayName) { |
| 329 | if (!displayName.empty()) { |
Mathias Agopian | 9e2463e | 2012-09-21 18:26:16 -0700 | [diff] [blame] | 330 | // never override the name with an empty name |
| 331 | mDisplayName = displayName; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 332 | mCompositionDisplay->setName(displayName); |
Mathias Agopian | 9e2463e | 2012-09-21 18:26:16 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 336 | uint32_t DisplayDevice::getPageFlipCount() const { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 337 | return mPageFlipCount; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Chia-I Wu | b02087d | 2017-11-09 10:19:54 -0800 | [diff] [blame] | 340 | void DisplayDevice::flip() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 342 | mPageFlipCount++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 345 | status_t DisplayDevice::beginFrame(bool mustRecompose) const { |
| 346 | return mDisplaySurface->beginFrame(mustRecompose); |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 347 | } |
| 348 | |
David Sodman | fb95bcc | 2017-12-22 15:45:30 -0800 | [diff] [blame] | 349 | status_t DisplayDevice::prepareFrame(HWComposer& hwc, |
| 350 | std::vector<CompositionInfo>& compositionData) { |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 351 | const auto id = getId(); |
| 352 | if (id) { |
| 353 | status_t error = hwc.prepare(id.value(), compositionData); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 354 | if (error != NO_ERROR) { |
| 355 | return error; |
| 356 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Lloyd Pique | 542307f | 2018-10-19 13:24:08 -0700 | [diff] [blame^] | 359 | compositionengine::DisplaySurface::CompositionType compositionType; |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 360 | bool hasClient = hwc.hasClientComposition(id); |
| 361 | bool hasDevice = hwc.hasDeviceComposition(id); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 362 | if (hasClient && hasDevice) { |
Lloyd Pique | 542307f | 2018-10-19 13:24:08 -0700 | [diff] [blame^] | 363 | compositionType = compositionengine::DisplaySurface::COMPOSITION_MIXED; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 364 | } else if (hasClient) { |
Lloyd Pique | 542307f | 2018-10-19 13:24:08 -0700 | [diff] [blame^] | 365 | compositionType = compositionengine::DisplaySurface::COMPOSITION_GLES; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 366 | } else if (hasDevice) { |
Lloyd Pique | 542307f | 2018-10-19 13:24:08 -0700 | [diff] [blame^] | 367 | compositionType = compositionengine::DisplaySurface::COMPOSITION_HWC; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 368 | } else { |
| 369 | // Nothing to do -- when turning the screen off we get a frame like |
| 370 | // this. Call it a HWC frame since we won't be doing any GLES work but |
| 371 | // will do a prepare/set cycle. |
Lloyd Pique | 542307f | 2018-10-19 13:24:08 -0700 | [diff] [blame^] | 372 | compositionType = compositionengine::DisplaySurface::COMPOSITION_HWC; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 373 | } |
| 374 | return mDisplaySurface->prepareFrame(compositionType); |
| 375 | } |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 376 | |
Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 377 | void DisplayDevice::setProtected(bool useProtected) { |
| 378 | uint64_t usageFlags = GRALLOC_USAGE_HW_RENDER; |
| 379 | if (useProtected) { |
| 380 | usageFlags |= GRALLOC_USAGE_PROTECTED; |
| 381 | } |
| 382 | const int status = native_window_set_usage(mNativeWindow.get(), usageFlags); |
| 383 | ALOGE_IF(status != NO_ERROR, "Unable to set BQ usage bits for protected content: %d", status); |
| 384 | } |
| 385 | |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 386 | sp<GraphicBuffer> DisplayDevice::dequeueBuffer() { |
| 387 | int fd; |
| 388 | ANativeWindowBuffer* buffer; |
| 389 | |
| 390 | status_t res = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &fd); |
| 391 | |
| 392 | if (res != NO_ERROR) { |
| 393 | ALOGE("ANativeWindow::dequeueBuffer failed for display [%s] with error: %d", |
| 394 | getDisplayName().c_str(), res); |
| 395 | // Return fast here as we can't do much more - any rendering we do |
| 396 | // now will just be wrong. |
| 397 | return mGraphicBuffer; |
| 398 | } |
| 399 | |
| 400 | ALOGW_IF(mGraphicBuffer != nullptr, "Clobbering a non-null pointer to a buffer [%p].", |
| 401 | mGraphicBuffer->getNativeBuffer()->handle); |
| 402 | mGraphicBuffer = GraphicBuffer::from(buffer); |
| 403 | |
| 404 | // Block until the buffer is ready |
| 405 | // TODO(alecmouri): it's perhaps more appropriate to block renderengine so |
| 406 | // that the gl driver can block instead. |
| 407 | if (fd >= 0) { |
| 408 | sync_wait(fd, -1); |
| 409 | close(fd); |
| 410 | } |
| 411 | |
| 412 | return mGraphicBuffer; |
| 413 | } |
| 414 | |
| 415 | void DisplayDevice::queueBuffer(HWComposer& hwc) { |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 416 | const auto id = getId(); |
| 417 | if (hwc.hasClientComposition(id) || hwc.hasFlipClientTargetRequest(id)) { |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 418 | // hasFlipClientTargetRequest could return true even if we haven't |
| 419 | // dequeued a buffer before. Try dequeueing one if we don't have a |
| 420 | // buffer ready. |
| 421 | if (mGraphicBuffer == nullptr) { |
| 422 | ALOGI("Attempting to queue a client composited buffer without one " |
| 423 | "previously dequeued for display [%s]. Attempting to dequeue " |
| 424 | "a scratch buffer now", |
| 425 | mDisplayName.c_str()); |
| 426 | // We shouldn't deadlock here, since mGraphicBuffer == nullptr only |
| 427 | // after a successful call to queueBuffer, or if dequeueBuffer has |
| 428 | // never been called. |
| 429 | dequeueBuffer(); |
| 430 | } |
| 431 | |
| 432 | if (mGraphicBuffer == nullptr) { |
| 433 | ALOGE("No buffer is ready for display [%s]", mDisplayName.c_str()); |
| 434 | } else { |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 435 | status_t res = mNativeWindow->queueBuffer(mNativeWindow.get(), |
Alec Mouri | 745163a | 2018-12-04 15:15:50 -0800 | [diff] [blame] | 436 | mGraphicBuffer->getNativeBuffer(), |
| 437 | dup(mBufferReady)); |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 438 | if (res != NO_ERROR) { |
| 439 | ALOGE("Error when queueing buffer for display [%s]: %d", mDisplayName.c_str(), res); |
| 440 | // We risk blocking on dequeueBuffer if the primary display failed |
| 441 | // to queue up its buffer, so crash here. |
| 442 | if (isPrimary()) { |
| 443 | LOG_ALWAYS_FATAL("ANativeWindow::queueBuffer failed with error: %d", res); |
| 444 | } else { |
| 445 | mNativeWindow->cancelBuffer(mNativeWindow.get(), |
Alec Mouri | 745163a | 2018-12-04 15:15:50 -0800 | [diff] [blame] | 446 | mGraphicBuffer->getNativeBuffer(), |
| 447 | dup(mBufferReady)); |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 448 | } |
| 449 | } |
Alec Mouri | 745163a | 2018-12-04 15:15:50 -0800 | [diff] [blame] | 450 | |
| 451 | mBufferReady.reset(); |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 452 | mGraphicBuffer = nullptr; |
| 453 | } |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 454 | } |
Mathias Agopian | 52e2148 | 2012-09-24 18:07:21 -0700 | [diff] [blame] | 455 | |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 456 | status_t result = mDisplaySurface->advanceFrame(); |
| 457 | if (result != NO_ERROR) { |
Dominik Laskowski | bf170d9 | 2018-04-19 15:08:05 -0700 | [diff] [blame] | 458 | ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplayName.c_str(), result); |
Mathias Agopian | 3234138 | 2012-09-25 19:16:28 -0700 | [diff] [blame] | 459 | } |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 462 | void DisplayDevice::onPresentDisplayCompleted() { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 463 | mDisplaySurface->onFrameCommitted(); |
| 464 | } |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 465 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 466 | void DisplayDevice::setViewportAndProjection() const { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 467 | size_t w = getWidth(); |
| 468 | size_t h = getHeight(); |
Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 469 | Rect sourceCrop(0, 0, w, h); |
Chia-I Wu | 1be50b5 | 2018-08-29 10:44:48 -0700 | [diff] [blame] | 470 | mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, ui::Transform::ROT_0); |
Mathias Agopian | bae92d0 | 2012-09-28 01:00:47 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 473 | void DisplayDevice::finishBuffer() { |
| 474 | mBufferReady = mFlinger->getRenderEngine().flush(); |
| 475 | if (mBufferReady.get() < 0) { |
| 476 | mFlinger->getRenderEngine().finish(); |
| 477 | } |
| 478 | } |
| 479 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 480 | const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const { |
| 481 | return mDisplaySurface->getClientTargetAcquireFence(); |
| 482 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 483 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 484 | // ---------------------------------------------------------------------------- |
| 485 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 486 | void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 487 | mVisibleLayersSortedByZ = layers; |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 490 | const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 491 | return mVisibleLayersSortedByZ; |
| 492 | } |
| 493 | |
Chia-I Wu | 8380689 | 2017-11-16 10:50:20 -0800 | [diff] [blame] | 494 | void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) { |
| 495 | mLayersNeedingFences = layers; |
| 496 | } |
| 497 | |
| 498 | const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const { |
| 499 | return mLayersNeedingFences; |
| 500 | } |
| 501 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 502 | // ---------------------------------------------------------------------------- |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 503 | void DisplayDevice::setPowerMode(int mode) { |
| 504 | mPowerMode = mode; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 505 | getCompositionDisplay()->setCompositionEnabled(mPowerMode != HWC_POWER_MODE_OFF); |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 508 | int DisplayDevice::getPowerMode() const { |
| 509 | return mPowerMode; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 512 | bool DisplayDevice::isPoweredOn() const { |
| 513 | return mPowerMode != HWC_POWER_MODE_OFF; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // ---------------------------------------------------------------------------- |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 517 | void DisplayDevice::setActiveConfig(int mode) { |
| 518 | mActiveConfig = mode; |
| 519 | } |
| 520 | |
| 521 | int DisplayDevice::getActiveConfig() const { |
| 522 | return mActiveConfig; |
| 523 | } |
| 524 | |
| 525 | // ---------------------------------------------------------------------------- |
Yiwei Zhang | 7c64f17 | 2018-03-07 14:52:28 -0800 | [diff] [blame] | 526 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 527 | void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) { |
Courtney Goeltzenleuchter | 79d2724 | 2017-07-13 17:54:01 -0600 | [diff] [blame] | 528 | ANativeWindow* const window = mNativeWindow.get(); |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 529 | native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace)); |
Courtney Goeltzenleuchter | 79d2724 | 2017-07-13 17:54:01 -0600 | [diff] [blame] | 530 | } |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 531 | |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 532 | ui::Dataspace DisplayDevice::getCompositionDataSpace() const { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 533 | return mCompositionDisplay->getState().dataspace; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 536 | // ---------------------------------------------------------------------------- |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 537 | |
Mathias Agopian | 28947d7 | 2012-08-08 18:51:15 -0700 | [diff] [blame] | 538 | void DisplayDevice::setLayerStack(uint32_t stack) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 539 | mCompositionDisplay->setLayerStackFilter(!isPrimary(), stack); |
Mathias Agopian | 28947d7 | 2012-08-08 18:51:15 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // ---------------------------------------------------------------------------- |
| 543 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 544 | uint32_t DisplayDevice::displayStateOrientationToTransformOrientation(int orientation) { |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 545 | switch (orientation) { |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 546 | case DisplayState::eOrientationDefault: |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 547 | return ui::Transform::ROT_0; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 548 | case DisplayState::eOrientation90: |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 549 | return ui::Transform::ROT_90; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 550 | case DisplayState::eOrientation180: |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 551 | return ui::Transform::ROT_180; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 552 | case DisplayState::eOrientation270: |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 553 | return ui::Transform::ROT_270; |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 554 | default: |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 555 | return ui::Transform::ROT_INVALID; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | status_t DisplayDevice::orientationToTransfrom(int orientation, int w, int h, ui::Transform* tr) { |
| 560 | uint32_t flags = displayStateOrientationToTransformOrientation(orientation); |
| 561 | if (flags == ui::Transform::ROT_INVALID) { |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 562 | return BAD_VALUE; |
| 563 | } |
| 564 | tr->set(flags, w, h); |
| 565 | return NO_ERROR; |
| 566 | } |
| 567 | |
Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 568 | void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) { |
Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 569 | mDisplaySurface->resizeBuffers(newWidth, newHeight); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 570 | mCompositionDisplay->setBounds(Rect(newWidth, newHeight)); |
Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Mathias Agopian | 00e8c7a | 2012-09-04 19:30:46 -0700 | [diff] [blame] | 573 | void DisplayDevice::setProjection(int orientation, |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 574 | const Rect& newViewport, const Rect& newFrame) { |
| 575 | Rect viewport(newViewport); |
| 576 | Rect frame(newFrame); |
| 577 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 578 | mOrientation = orientation; |
| 579 | |
| 580 | const Rect& displayBounds = getCompositionDisplay()->getState().bounds; |
| 581 | const int w = displayBounds.width(); |
| 582 | const int h = displayBounds.height(); |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 583 | |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 584 | ui::Transform R; |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 585 | DisplayDevice::orientationToTransfrom(orientation, w, h, &R); |
| 586 | |
| 587 | if (!frame.isValid()) { |
| 588 | // the destination frame can be invalid if it has never been set, |
| 589 | // in that case we assume the whole display frame. |
| 590 | frame = Rect(w, h); |
| 591 | } |
| 592 | |
| 593 | if (viewport.isEmpty()) { |
| 594 | // viewport can be invalid if it has never been set, in that case |
| 595 | // we assume the whole display size. |
| 596 | // it's also invalid to have an empty viewport, so we handle that |
| 597 | // case in the same way. |
| 598 | viewport = Rect(w, h); |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 599 | if (R.getOrientation() & ui::Transform::ROT_90) { |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 600 | // viewport is always specified in the logical orientation |
| 601 | // of the display (ie: post-rotation). |
Peiyong Lin | 3db4234 | 2018-08-16 09:15:59 -0700 | [diff] [blame] | 602 | std::swap(viewport.right, viewport.bottom); |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 606 | ui::Transform TL, TP, S; |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 607 | float src_width = viewport.width(); |
| 608 | float src_height = viewport.height(); |
| 609 | float dst_width = frame.width(); |
| 610 | float dst_height = frame.height(); |
| 611 | if (src_width != dst_width || src_height != dst_height) { |
| 612 | float sx = dst_width / src_width; |
| 613 | float sy = dst_height / src_height; |
| 614 | S.set(sx, 0, 0, sy); |
| 615 | } |
| 616 | |
| 617 | float src_x = viewport.left; |
| 618 | float src_y = viewport.top; |
| 619 | float dst_x = frame.left; |
| 620 | float dst_y = frame.top; |
| 621 | TL.set(-src_x, -src_y); |
| 622 | TP.set(dst_x, dst_y); |
| 623 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 624 | // need to take care of primary display rotation for globalTransform |
Iris Chang | 7501ed6 | 2018-04-30 14:45:42 +0800 | [diff] [blame] | 625 | // for case if the panel is not installed aligned with device orientation |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 626 | if (isPrimary()) { |
Iris Chang | 7501ed6 | 2018-04-30 14:45:42 +0800 | [diff] [blame] | 627 | DisplayDevice::orientationToTransfrom( |
Chia-I Wu | a02871c | 2018-08-27 14:38:23 -0700 | [diff] [blame] | 628 | (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1), |
Iris Chang | 7501ed6 | 2018-04-30 14:45:42 +0800 | [diff] [blame] | 629 | w, h, &R); |
| 630 | } |
| 631 | |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 632 | // The viewport and frame are both in the logical orientation. |
| 633 | // Apply the logical translation, scale to physical size, apply the |
| 634 | // physical translation and finally rotate to the physical orientation. |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 635 | ui::Transform globalTransform = R * TP * S * TL; |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 636 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 637 | const uint8_t type = globalTransform.getType(); |
| 638 | const bool needsFiltering = |
| 639 | (!globalTransform.preserveRects() || (type >= ui::Transform::SCALE)); |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 640 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 641 | Rect scissor = globalTransform.transform(viewport); |
| 642 | if (scissor.isEmpty()) { |
| 643 | scissor = displayBounds; |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 646 | if (isPrimary()) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 647 | sPrimaryDisplayOrientation = displayStateOrientationToTransformOrientation(orientation); |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 648 | } |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 649 | |
| 650 | getCompositionDisplay()->setProjection(globalTransform, |
| 651 | displayStateOrientationToTransformOrientation( |
| 652 | orientation), |
| 653 | frame, viewport, scissor, needsFiltering); |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 654 | } |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 655 | |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 656 | uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() { |
| 657 | return sPrimaryDisplayOrientation; |
| 658 | } |
| 659 | |
Dominik Laskowski | 9b17b2c2 | 2018-11-01 14:49:03 -0700 | [diff] [blame] | 660 | std::string DisplayDevice::getDebugName() const { |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 661 | const auto id = getId() ? to_string(*getId()) + ", " : std::string(); |
Dominik Laskowski | 9b17b2c2 | 2018-11-01 14:49:03 -0700 | [diff] [blame] | 662 | return base::StringPrintf("DisplayDevice{%s%s%s\"%s\"}", id.c_str(), |
| 663 | isPrimary() ? "primary, " : "", isVirtual() ? "virtual, " : "", |
| 664 | mDisplayName.c_str()); |
| 665 | } |
| 666 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 667 | void DisplayDevice::dump(std::string& result) const { |
Courtney Goeltzenleuchter | 152279d | 2017-08-14 18:18:30 -0600 | [diff] [blame] | 668 | ANativeWindow* const window = mNativeWindow.get(); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 669 | StringAppendF(&result, "+ %s\n", getDebugName().c_str()); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 670 | |
| 671 | result.append(" "); |
| 672 | StringAppendF(&result, "ANativeWindow=%p (format %d), ", window, |
| 673 | ANativeWindow_getFormat(window)); |
| 674 | StringAppendF(&result, "flips=%u, ", getPageFlipCount()); |
| 675 | StringAppendF(&result, "powerMode=%d, ", mPowerMode); |
| 676 | StringAppendF(&result, "activeConfig=%d, ", mActiveConfig); |
| 677 | StringAppendF(&result, "numLayers=%zu\n", mVisibleLayersSortedByZ.size()); |
| 678 | getCompositionDisplay()->dump(result); |
Courtney Goeltzenleuchter | 152279d | 2017-08-14 18:18:30 -0600 | [diff] [blame] | 679 | auto const surface = static_cast<Surface*>(window); |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 680 | ui::Dataspace dataspace = surface->getBuffersDataSpace(); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 681 | StringAppendF(&result, " wideColorGamut=%d, hdr10plus =%d, hdr10=%d, dataspace: %s (%d)\n", |
Valerie Hau | e9e843a | 2018-12-18 13:39:23 -0800 | [diff] [blame] | 682 | mHasWideColorGamut, mHasHdr10Plus, mHasHdr10, |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 683 | dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace); |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 684 | |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 685 | String8 surfaceDump; |
Dan Stoza | f10c46e | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 686 | mDisplaySurface->dumpAsString(surfaceDump); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 687 | result.append(surfaceDump.string(), surfaceDump.size()); |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 688 | } |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 689 | |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 690 | // Map dataspace/intent to the best matched dataspace/colorMode/renderIntent |
| 691 | // supported by HWC. |
| 692 | void DisplayDevice::addColorMode( |
| 693 | const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes, |
| 694 | const ColorMode mode, const RenderIntent intent) { |
| 695 | // find the best color mode |
| 696 | const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode); |
| 697 | |
| 698 | // find the best render intent |
| 699 | auto iter = hwcColorModes.find(hwcColorMode); |
| 700 | const auto& hwcIntents = |
| 701 | iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>(); |
| 702 | const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent); |
| 703 | |
| 704 | const Dataspace dataspace = colorModeToDataspace(mode); |
| 705 | const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode); |
| 706 | |
Dominik Laskowski | 9b17b2c2 | 2018-11-01 14:49:03 -0700 | [diff] [blame] | 707 | ALOGV("%s: map (%s, %s) to (%s, %s, %s)", getDebugName().c_str(), |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 708 | dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(), |
| 709 | decodeRenderIntent(intent).c_str(), |
| 710 | dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(), |
| 711 | decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str()); |
| 712 | |
| 713 | mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent}; |
| 714 | } |
| 715 | |
| 716 | void DisplayDevice::populateColorModes( |
| 717 | const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes) { |
| 718 | if (!hasWideColorGamut()) { |
| 719 | return; |
| 720 | } |
| 721 | |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 722 | // collect all known SDR render intents |
| 723 | std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(), |
| 724 | sSdrRenderIntents.end()); |
| 725 | auto iter = hwcColorModes.find(ColorMode::SRGB); |
| 726 | if (iter != hwcColorModes.end()) { |
| 727 | for (auto intent : iter->second) { |
| 728 | sdrRenderIntents.insert(intent); |
| 729 | } |
| 730 | } |
| 731 | |
Chia-I Wu | c4b08bd | 2018-05-29 12:57:23 -0700 | [diff] [blame] | 732 | // add all known SDR combinations |
Chia-I Wu | 0d71126 | 2018-05-21 15:19:35 -0700 | [diff] [blame] | 733 | for (auto intent : sdrRenderIntents) { |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 734 | for (auto mode : sSdrColorModes) { |
| 735 | addColorMode(hwcColorModes, mode, intent); |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 738 | |
Chia-I Wu | c4b08bd | 2018-05-29 12:57:23 -0700 | [diff] [blame] | 739 | // collect all known HDR render intents |
| 740 | std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(), |
| 741 | sHdrRenderIntents.end()); |
| 742 | iter = hwcColorModes.find(ColorMode::BT2100_PQ); |
| 743 | if (iter != hwcColorModes.end()) { |
| 744 | for (auto intent : iter->second) { |
| 745 | hdrRenderIntents.insert(intent); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | // add all known HDR combinations |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 750 | for (auto intent : sHdrRenderIntents) { |
| 751 | for (auto mode : sHdrColorModes) { |
| 752 | addColorMode(hwcColorModes, mode, intent); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | bool DisplayDevice::hasRenderIntent(RenderIntent intent) const { |
| 758 | // assume a render intent is supported when SRGB supports it; we should |
| 759 | // get rid of that assumption. |
Peiyong Lin | 14724e6 | 2018-12-05 07:27:30 -0800 | [diff] [blame] | 760 | auto iter = mColorModes.find(getColorModeKey(Dataspace::V0_SRGB, intent)); |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 761 | return iter != mColorModes.end() && iter->second.renderIntent == intent; |
| 762 | } |
| 763 | |
Peiyong Lin | dfde511 | 2018-06-05 10:58:41 -0700 | [diff] [blame] | 764 | bool DisplayDevice::hasLegacyHdrSupport(Dataspace dataspace) const { |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 765 | if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) || |
| 766 | (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) { |
| 767 | auto iter = |
| 768 | mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC)); |
Peiyong Lin | dfde511 | 2018-06-05 10:58:41 -0700 | [diff] [blame] | 769 | return iter == mColorModes.end() || iter->second.dataspace != dataspace; |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | void DisplayDevice::getBestColorMode(Dataspace dataspace, RenderIntent intent, |
| 776 | Dataspace* outDataspace, ColorMode* outMode, |
| 777 | RenderIntent* outIntent) const { |
| 778 | auto iter = mColorModes.find(getColorModeKey(dataspace, intent)); |
| 779 | if (iter != mColorModes.end()) { |
| 780 | *outDataspace = iter->second.dataspace; |
| 781 | *outMode = iter->second.colorMode; |
| 782 | *outIntent = iter->second.renderIntent; |
| 783 | } else { |
Chia-I Wu | 6333af5 | 2018-10-16 13:46:36 -0700 | [diff] [blame] | 784 | // this is unexpected on a WCG display |
| 785 | if (hasWideColorGamut()) { |
| 786 | ALOGE("map unknown (%s)/(%s) to default color mode", |
| 787 | dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(), |
| 788 | decodeRenderIntent(intent).c_str()); |
| 789 | } |
Chia-I Wu | be02ec0 | 2018-05-18 10:59:36 -0700 | [diff] [blame] | 790 | |
| 791 | *outDataspace = Dataspace::UNKNOWN; |
| 792 | *outMode = ColorMode::NATIVE; |
| 793 | *outIntent = RenderIntent::COLORIMETRIC; |
| 794 | } |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 797 | // ---------------------------------------------------------------------------- |
| 798 | |
| 799 | const std::optional<DisplayId>& DisplayDevice::getId() const { |
| 800 | return mCompositionDisplay->getId(); |
| 801 | } |
| 802 | |
| 803 | bool DisplayDevice::isSecure() const { |
| 804 | return mCompositionDisplay->isSecure(); |
| 805 | } |
| 806 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 807 | const Rect& DisplayDevice::getBounds() const { |
| 808 | return mCompositionDisplay->getState().bounds; |
| 809 | } |
| 810 | |
| 811 | const Region& DisplayDevice::getUndefinedRegion() const { |
| 812 | return mCompositionDisplay->getState().undefinedRegion; |
| 813 | } |
| 814 | |
| 815 | bool DisplayDevice::needsFiltering() const { |
| 816 | return mCompositionDisplay->getState().needsFiltering; |
| 817 | } |
| 818 | |
| 819 | uint32_t DisplayDevice::getLayerStack() const { |
| 820 | return mCompositionDisplay->getState().singleLayerStackId; |
| 821 | } |
| 822 | |
| 823 | const ui::Transform& DisplayDevice::getTransform() const { |
| 824 | return mCompositionDisplay->getState().transform; |
| 825 | } |
| 826 | |
| 827 | const Rect& DisplayDevice::getViewport() const { |
| 828 | return mCompositionDisplay->getState().viewport; |
| 829 | } |
| 830 | |
| 831 | const Rect& DisplayDevice::getFrame() const { |
| 832 | return mCompositionDisplay->getState().frame; |
| 833 | } |
| 834 | |
| 835 | const Rect& DisplayDevice::getScissor() const { |
| 836 | return mCompositionDisplay->getState().scissor; |
| 837 | } |
| 838 | |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 839 | std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1); |
Peiyong Lin | fd997e0 | 2018-03-28 15:29:00 -0700 | [diff] [blame] | 840 | |
| 841 | } // namespace android |