blob: a0d3ff995e78a927eed91b25bf6fe2b548d41d36 [file] [log] [blame]
John Reck704bed02015-11-05 09:22:17 -08001/*
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 Salyzyn52eb4e02016-09-28 16:15:30 -070016
John Reck704bed02015-11-05 09:22:17 -080017#include <DeviceInfo.h>
18
John Reck56428472018-03-16 17:27:17 -070019#include "Properties.h"
20
John Reck8dc02f92017-07-17 09:55:02 -070021#include <gui/SurfaceComposerClient.h>
Peiyong Lin3bff1352018-12-11 07:56:07 -080022#include <ui/GraphicTypes.h>
John Reck704bed02015-11-05 09:22:17 -080023
John Reck704bed02015-11-05 09:22:17 -080024#include <mutex>
John Reck1bcacfd2017-11-03 10:12:19 -070025#include <thread>
John Reck704bed02015-11-05 09:22:17 -080026
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070027#include <log/log.h>
28
John Reck704bed02015-11-05 09:22:17 -080029namespace android {
30namespace uirenderer {
31
John Recke170fb62018-05-07 08:12:07 -070032static constexpr android::DisplayInfo sDummyDisplay{
John Reck56428472018-03-16 17:27:17 -070033 1080, // w
34 1920, // h
35 320.0, // xdpi
36 320.0, // ydpi
37 60.0, // fps
38 2.0, // density
39 0, // orientation
40 false, // secure?
41 0, // appVsyncOffset
42 0, // presentationDeadline
Yiwei Zhang2c2bfc32018-08-23 17:24:55 -070043 1080, // viewportW
44 1920, // viewportH
John Reck56428472018-03-16 17:27:17 -070045};
46
John Reckcf185f52019-04-11 16:11:24 -070047DeviceInfo* DeviceInfo::get() {
Derek Sollenberger17662382018-09-13 14:14:00 -040048 static DeviceInfo sDeviceInfo;
49 return &sDeviceInfo;
John Reck704bed02015-11-05 09:22:17 -080050}
51
John Reckcf185f52019-04-11 16:11:24 -070052static DisplayInfo QueryDisplayInfo() {
John Reck56428472018-03-16 17:27:17 -070053 if (Properties::isolatedProcess) {
54 return sDummyDisplay;
55 }
56
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080057 const sp<IBinder> token = SurfaceComposerClient::getInternalDisplayToken();
58 LOG_ALWAYS_FATAL_IF(token == nullptr,
59 "Failed to get display info because internal display is disconnected");
60
John Reck56428472018-03-16 17:27:17 -070061 DisplayInfo displayInfo;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080062 status_t status = SurfaceComposerClient::getDisplayInfo(token, &displayInfo);
John Reck8dc02f92017-07-17 09:55:02 -070063 LOG_ALWAYS_FATAL_IF(status, "Failed to get display info, error %d", status);
John Reck56428472018-03-16 17:27:17 -070064 return displayInfo;
John Reck8dc02f92017-07-17 09:55:02 -070065}
66
John Reckcf185f52019-04-11 16:11:24 -070067static float QueryMaxRefreshRate() {
68 if (Properties::isolatedProcess) {
69 return sDummyDisplay.fps;
70 }
71
72 const sp<IBinder> token = SurfaceComposerClient::getInternalDisplayToken();
73 LOG_ALWAYS_FATAL_IF(token == nullptr,
74 "Failed to get display info because internal display is disconnected");
75
76 Vector<DisplayInfo> configs;
77 configs.reserve(10);
78 status_t status = SurfaceComposerClient::getDisplayConfigs(token, &configs);
79 LOG_ALWAYS_FATAL_IF(status, "Failed to getDisplayConfigs, error %d", status);
80 LOG_ALWAYS_FATAL_IF(configs.size() == 0, "getDisplayConfigs returned 0 configs?");
81 float max = 0.0f;
82 for (auto& info : configs) {
83 max = std::max(max, info.fps);
84 }
85 return max;
86}
87
Brian Osmane0cf5972019-01-23 10:41:20 -050088static void queryWideColorGamutPreference(sk_sp<SkColorSpace>* colorSpace, SkColorType* colorType) {
Peiyong Lin3bff1352018-12-11 07:56:07 -080089 if (Properties::isolatedProcess) {
Peiyong Lin3bff1352018-12-11 07:56:07 -080090 *colorSpace = SkColorSpace::MakeSRGB();
91 *colorType = SkColorType::kN32_SkColorType;
92 return;
93 }
94 ui::Dataspace defaultDataspace, wcgDataspace;
95 ui::PixelFormat defaultPixelFormat, wcgPixelFormat;
96 status_t status =
97 SurfaceComposerClient::getCompositionPreference(&defaultDataspace, &defaultPixelFormat,
98 &wcgDataspace, &wcgPixelFormat);
99 LOG_ALWAYS_FATAL_IF(status, "Failed to get composition preference, error %d", status);
100 switch (wcgDataspace) {
101 case ui::Dataspace::DISPLAY_P3:
Brian Osmanbe8fac262019-01-14 17:02:23 -0500102 *colorSpace = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800103 break;
104 case ui::Dataspace::V0_SCRGB:
Peiyong Lin3bff1352018-12-11 07:56:07 -0800105 *colorSpace = SkColorSpace::MakeSRGB();
106 break;
107 case ui::Dataspace::V0_SRGB:
108 // when sRGB is returned, it means wide color gamut is not supported.
Peiyong Lin3bff1352018-12-11 07:56:07 -0800109 *colorSpace = SkColorSpace::MakeSRGB();
110 break;
111 default:
112 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
113 }
114 switch (wcgPixelFormat) {
115 case ui::PixelFormat::RGBA_8888:
116 *colorType = SkColorType::kN32_SkColorType;
117 break;
118 case ui::PixelFormat::RGBA_FP16:
119 *colorType = SkColorType::kRGBA_F16_SkColorType;
120 break;
121 default:
122 LOG_ALWAYS_FATAL("Unreachable: unsupported pixel format.");
123 }
124}
125
John Reckcf185f52019-04-11 16:11:24 -0700126DeviceInfo::DeviceInfo() : mMaxRefreshRate(QueryMaxRefreshRate()) {
Derek Sollenberger17662382018-09-13 14:14:00 -0400127#if HWUI_NULL_GPU
128 mMaxTextureSize = NULL_GPU_MAX_TEXTURE_SIZE;
129#else
130 mMaxTextureSize = -1;
131#endif
132 mDisplayInfo = QueryDisplayInfo();
Brian Osmane0cf5972019-01-23 10:41:20 -0500133 queryWideColorGamutPreference(&mWideColorSpace, &mWideColorType);
Derek Sollenberger17662382018-09-13 14:14:00 -0400134}
135
136int DeviceInfo::maxTextureSize() const {
137 LOG_ALWAYS_FATAL_IF(mMaxTextureSize < 0, "MaxTextureSize has not been initialized yet.");
138 return mMaxTextureSize;
139}
140
141void DeviceInfo::setMaxTextureSize(int maxTextureSize) {
John Reckcf185f52019-04-11 16:11:24 -0700142 DeviceInfo::get()->mMaxTextureSize = maxTextureSize;
143}
144
145void DeviceInfo::onDisplayConfigChanged() {
146 mDisplayInfo = QueryDisplayInfo();
Derek Sollenberger17662382018-09-13 14:14:00 -0400147}
148
John Reck704bed02015-11-05 09:22:17 -0800149} /* namespace uirenderer */
150} /* namespace android */