blob: 41e9b4be6649bfe5f6f2e2904bd88126c06cbe38 [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>
John Reck8dc02f92017-07-17 09:55:02 -070018#include <gui/SurfaceComposerClient.h>
Alec Mouri22d753f2019-09-05 17:11:45 -070019#include <log/log.h>
Peiyong Lin3bff1352018-12-11 07:56:07 -080020#include <ui/GraphicTypes.h>
John Reck704bed02015-11-05 09:22:17 -080021
John Reck704bed02015-11-05 09:22:17 -080022#include <mutex>
John Reck1bcacfd2017-11-03 10:12:19 -070023#include <thread>
John Reck704bed02015-11-05 09:22:17 -080024
Alec Mouri22d753f2019-09-05 17:11:45 -070025#include "Properties.h"
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070026
John Reck704bed02015-11-05 09:22:17 -080027namespace android {
28namespace uirenderer {
29
John Reckcf185f52019-04-11 16:11:24 -070030DeviceInfo* DeviceInfo::get() {
Alec Mouri22d753f2019-09-05 17:11:45 -070031 static DeviceInfo sDeviceInfo;
32 return &sDeviceInfo;
John Reckcf185f52019-04-11 16:11:24 -070033}
34
Brian Osmane0cf5972019-01-23 10:41:20 -050035static void queryWideColorGamutPreference(sk_sp<SkColorSpace>* colorSpace, SkColorType* colorType) {
Peiyong Lin3bff1352018-12-11 07:56:07 -080036 if (Properties::isolatedProcess) {
Peiyong Lin3bff1352018-12-11 07:56:07 -080037 *colorSpace = SkColorSpace::MakeSRGB();
38 *colorType = SkColorType::kN32_SkColorType;
39 return;
40 }
41 ui::Dataspace defaultDataspace, wcgDataspace;
42 ui::PixelFormat defaultPixelFormat, wcgPixelFormat;
43 status_t status =
44 SurfaceComposerClient::getCompositionPreference(&defaultDataspace, &defaultPixelFormat,
45 &wcgDataspace, &wcgPixelFormat);
46 LOG_ALWAYS_FATAL_IF(status, "Failed to get composition preference, error %d", status);
47 switch (wcgDataspace) {
48 case ui::Dataspace::DISPLAY_P3:
Brian Osmanbe8fac262019-01-14 17:02:23 -050049 *colorSpace = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
Peiyong Lin3bff1352018-12-11 07:56:07 -080050 break;
51 case ui::Dataspace::V0_SCRGB:
Peiyong Lin3bff1352018-12-11 07:56:07 -080052 *colorSpace = SkColorSpace::MakeSRGB();
53 break;
54 case ui::Dataspace::V0_SRGB:
55 // when sRGB is returned, it means wide color gamut is not supported.
Peiyong Lin3bff1352018-12-11 07:56:07 -080056 *colorSpace = SkColorSpace::MakeSRGB();
57 break;
58 default:
59 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
60 }
61 switch (wcgPixelFormat) {
62 case ui::PixelFormat::RGBA_8888:
63 *colorType = SkColorType::kN32_SkColorType;
64 break;
65 case ui::PixelFormat::RGBA_FP16:
66 *colorType = SkColorType::kRGBA_F16_SkColorType;
67 break;
68 default:
69 LOG_ALWAYS_FATAL("Unreachable: unsupported pixel format.");
70 }
71}
72
Alec Mouri22d753f2019-09-05 17:11:45 -070073DeviceInfo::DeviceInfo() {
Derek Sollenberger17662382018-09-13 14:14:00 -040074#if HWUI_NULL_GPU
75 mMaxTextureSize = NULL_GPU_MAX_TEXTURE_SIZE;
76#else
77 mMaxTextureSize = -1;
78#endif
Alec Mouri22d753f2019-09-05 17:11:45 -070079 updateDisplayInfo();
80 queryWideColorGamutPreference(&mWideColorSpace, &mWideColorType);
81}
82DeviceInfo::~DeviceInfo() {
83 ADisplay_release(mDisplays);
Derek Sollenberger17662382018-09-13 14:14:00 -040084}
85
86int DeviceInfo::maxTextureSize() const {
87 LOG_ALWAYS_FATAL_IF(mMaxTextureSize < 0, "MaxTextureSize has not been initialized yet.");
88 return mMaxTextureSize;
89}
90
91void DeviceInfo::setMaxTextureSize(int maxTextureSize) {
John Reckcf185f52019-04-11 16:11:24 -070092 DeviceInfo::get()->mMaxTextureSize = maxTextureSize;
93}
94
95void DeviceInfo::onDisplayConfigChanged() {
Alec Mouri22d753f2019-09-05 17:11:45 -070096 updateDisplayInfo();
97}
98
99void DeviceInfo::updateDisplayInfo() {
100 if (Properties::isolatedProcess) {
101 return;
102 }
103
104 if (mCurrentConfig == nullptr) {
105 mDisplaysSize = ADisplay_acquirePhysicalDisplays(&mDisplays);
106 LOG_ALWAYS_FATAL_IF(mDisplays == nullptr || mDisplaysSize <= 0,
107 "Failed to get physical displays: no connected display: %d!", mDisplaysSize);
108 for (size_t i = 0; i < mDisplaysSize; i++) {
109 ADisplayType type = ADisplay_getDisplayType(mDisplays[i]);
110 if (type == ADisplayType::DISPLAY_TYPE_INTERNAL) {
111 mPhysicalDisplayIndex = i;
112 break;
113 }
114 }
115 LOG_ALWAYS_FATAL_IF(mPhysicalDisplayIndex < 0, "Failed to find a connected physical display!");
116 mMaxRefreshRate = ADisplay_getMaxSupportedFps(mDisplays[mPhysicalDisplayIndex]);
117 }
118 status_t status = ADisplay_getCurrentConfig(mDisplays[mPhysicalDisplayIndex], &mCurrentConfig);
119 LOG_ALWAYS_FATAL_IF(status, "Failed to get display config, error %d", status);
120 mWidth = ADisplayConfig_getWidth(mCurrentConfig);
121 mHeight = ADisplayConfig_getHeight(mCurrentConfig);
122 mDensity = ADisplayConfig_getDensity(mCurrentConfig);
123 mRefreshRate = ADisplayConfig_getFps(mCurrentConfig);
124 mCompositorOffset = ADisplayConfig_getCompositorOffsetNanos(mCurrentConfig);
125 mAppOffset = ADisplayConfig_getAppVsyncOffsetNanos(mCurrentConfig);
Derek Sollenberger17662382018-09-13 14:14:00 -0400126}
127
John Reck704bed02015-11-05 09:22:17 -0800128} /* namespace uirenderer */
129} /* namespace android */