John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 16 | |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 17 | #include <DeviceInfo.h> |
| 18 | |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 19 | #include "Properties.h" |
| 20 | |
John Reck | 8dc02f9 | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 21 | #include <gui/ISurfaceComposer.h> |
| 22 | #include <gui/SurfaceComposerClient.h> |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 23 | #include <ui/GraphicTypes.h> |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 24 | |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 25 | #include <mutex> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 26 | #include <thread> |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 27 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
| 29 | |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
John Reck | e170fb6 | 2018-05-07 08:12:07 -0700 | [diff] [blame] | 33 | static constexpr android::DisplayInfo sDummyDisplay{ |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 34 | 1080, // w |
| 35 | 1920, // h |
| 36 | 320.0, // xdpi |
| 37 | 320.0, // ydpi |
| 38 | 60.0, // fps |
| 39 | 2.0, // density |
| 40 | 0, // orientation |
| 41 | false, // secure? |
| 42 | 0, // appVsyncOffset |
| 43 | 0, // presentationDeadline |
Yiwei Zhang | 2c2bfc3 | 2018-08-23 17:24:55 -0700 | [diff] [blame] | 44 | 1080, // viewportW |
| 45 | 1920, // viewportH |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 48 | DeviceInfo* DeviceInfo::get() { |
Derek Sollenberger | 1766238 | 2018-09-13 14:14:00 -0400 | [diff] [blame] | 49 | static DeviceInfo sDeviceInfo; |
| 50 | return &sDeviceInfo; |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 51 | } |
| 52 | |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 53 | static DisplayInfo QueryDisplayInfo() { |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 54 | if (Properties::isolatedProcess) { |
| 55 | return sDummyDisplay; |
| 56 | } |
| 57 | |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 58 | const sp<IBinder> token = SurfaceComposerClient::getInternalDisplayToken(); |
| 59 | LOG_ALWAYS_FATAL_IF(token == nullptr, |
| 60 | "Failed to get display info because internal display is disconnected"); |
| 61 | |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 62 | DisplayInfo displayInfo; |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 63 | status_t status = SurfaceComposerClient::getDisplayInfo(token, &displayInfo); |
John Reck | 8dc02f9 | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 64 | LOG_ALWAYS_FATAL_IF(status, "Failed to get display info, error %d", status); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 65 | return displayInfo; |
John Reck | 8dc02f9 | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 66 | } |
| 67 | |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 68 | static float QueryMaxRefreshRate() { |
| 69 | if (Properties::isolatedProcess) { |
| 70 | return sDummyDisplay.fps; |
| 71 | } |
| 72 | |
| 73 | const sp<IBinder> token = SurfaceComposerClient::getInternalDisplayToken(); |
| 74 | LOG_ALWAYS_FATAL_IF(token == nullptr, |
| 75 | "Failed to get display info because internal display is disconnected"); |
| 76 | |
| 77 | Vector<DisplayInfo> configs; |
| 78 | configs.reserve(10); |
| 79 | status_t status = SurfaceComposerClient::getDisplayConfigs(token, &configs); |
| 80 | LOG_ALWAYS_FATAL_IF(status, "Failed to getDisplayConfigs, error %d", status); |
| 81 | LOG_ALWAYS_FATAL_IF(configs.size() == 0, "getDisplayConfigs returned 0 configs?"); |
| 82 | float max = 0.0f; |
| 83 | for (auto& info : configs) { |
| 84 | max = std::max(max, info.fps); |
| 85 | } |
| 86 | return max; |
| 87 | } |
| 88 | |
Brian Osman | e0cf597 | 2019-01-23 10:41:20 -0500 | [diff] [blame] | 89 | static void queryWideColorGamutPreference(sk_sp<SkColorSpace>* colorSpace, SkColorType* colorType) { |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 90 | if (Properties::isolatedProcess) { |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 91 | *colorSpace = SkColorSpace::MakeSRGB(); |
| 92 | *colorType = SkColorType::kN32_SkColorType; |
| 93 | return; |
| 94 | } |
| 95 | ui::Dataspace defaultDataspace, wcgDataspace; |
| 96 | ui::PixelFormat defaultPixelFormat, wcgPixelFormat; |
| 97 | status_t status = |
| 98 | SurfaceComposerClient::getCompositionPreference(&defaultDataspace, &defaultPixelFormat, |
| 99 | &wcgDataspace, &wcgPixelFormat); |
| 100 | LOG_ALWAYS_FATAL_IF(status, "Failed to get composition preference, error %d", status); |
| 101 | switch (wcgDataspace) { |
| 102 | case ui::Dataspace::DISPLAY_P3: |
Brian Osman | be8fac26 | 2019-01-14 17:02:23 -0500 | [diff] [blame] | 103 | *colorSpace = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3); |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 104 | break; |
| 105 | case ui::Dataspace::V0_SCRGB: |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 106 | *colorSpace = SkColorSpace::MakeSRGB(); |
| 107 | break; |
| 108 | case ui::Dataspace::V0_SRGB: |
| 109 | // when sRGB is returned, it means wide color gamut is not supported. |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 110 | *colorSpace = SkColorSpace::MakeSRGB(); |
| 111 | break; |
| 112 | default: |
| 113 | LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space."); |
| 114 | } |
| 115 | switch (wcgPixelFormat) { |
| 116 | case ui::PixelFormat::RGBA_8888: |
| 117 | *colorType = SkColorType::kN32_SkColorType; |
| 118 | break; |
| 119 | case ui::PixelFormat::RGBA_FP16: |
| 120 | *colorType = SkColorType::kRGBA_F16_SkColorType; |
| 121 | break; |
| 122 | default: |
| 123 | LOG_ALWAYS_FATAL("Unreachable: unsupported pixel format."); |
| 124 | } |
| 125 | } |
| 126 | |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 127 | DeviceInfo::DeviceInfo() : mMaxRefreshRate(QueryMaxRefreshRate()) { |
Derek Sollenberger | 1766238 | 2018-09-13 14:14:00 -0400 | [diff] [blame] | 128 | #if HWUI_NULL_GPU |
| 129 | mMaxTextureSize = NULL_GPU_MAX_TEXTURE_SIZE; |
| 130 | #else |
| 131 | mMaxTextureSize = -1; |
| 132 | #endif |
| 133 | mDisplayInfo = QueryDisplayInfo(); |
Brian Osman | e0cf597 | 2019-01-23 10:41:20 -0500 | [diff] [blame] | 134 | queryWideColorGamutPreference(&mWideColorSpace, &mWideColorType); |
Derek Sollenberger | 1766238 | 2018-09-13 14:14:00 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int DeviceInfo::maxTextureSize() const { |
| 138 | LOG_ALWAYS_FATAL_IF(mMaxTextureSize < 0, "MaxTextureSize has not been initialized yet."); |
| 139 | return mMaxTextureSize; |
| 140 | } |
| 141 | |
| 142 | void DeviceInfo::setMaxTextureSize(int maxTextureSize) { |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 143 | DeviceInfo::get()->mMaxTextureSize = maxTextureSize; |
| 144 | } |
| 145 | |
| 146 | void DeviceInfo::onDisplayConfigChanged() { |
| 147 | mDisplayInfo = QueryDisplayInfo(); |
Derek Sollenberger | 1766238 | 2018-09-13 14:14:00 -0400 | [diff] [blame] | 148 | } |
| 149 | |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 150 | } /* namespace uirenderer */ |
| 151 | } /* namespace android */ |