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