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