blob: e53f3db085381978760a2911f7621b81666699fe [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>
Alec Mouri22d753f2019-09-05 17:11:45 -070018#include <log/log.h>
Alec Mouri70f2a922019-11-20 11:10:29 -080019#include <utils/Errors.h>
John Reck704bed02015-11-05 09:22:17 -080020
John Reck704bed02015-11-05 09:22:17 -080021#include <mutex>
John Reck1bcacfd2017-11-03 10:12:19 -070022#include <thread>
John Reck704bed02015-11-05 09:22:17 -080023
Alec Mouri22d753f2019-09-05 17:11:45 -070024#include "Properties.h"
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070025
John Reck704bed02015-11-05 09:22:17 -080026namespace android {
27namespace uirenderer {
28
John Reckcf185f52019-04-11 16:11:24 -070029DeviceInfo* DeviceInfo::get() {
Alec Mouri22d753f2019-09-05 17:11:45 -070030 static DeviceInfo sDeviceInfo;
31 return &sDeviceInfo;
John Reckcf185f52019-04-11 16:11:24 -070032}
33
Alec Mouri22d753f2019-09-05 17:11:45 -070034DeviceInfo::DeviceInfo() {
Derek Sollenberger17662382018-09-13 14:14:00 -040035#if HWUI_NULL_GPU
36 mMaxTextureSize = NULL_GPU_MAX_TEXTURE_SIZE;
37#else
38 mMaxTextureSize = -1;
39#endif
Alec Mouri22d753f2019-09-05 17:11:45 -070040 updateDisplayInfo();
Alec Mouri22d753f2019-09-05 17:11:45 -070041}
42DeviceInfo::~DeviceInfo() {
43 ADisplay_release(mDisplays);
Derek Sollenberger17662382018-09-13 14:14:00 -040044}
45
46int DeviceInfo::maxTextureSize() const {
47 LOG_ALWAYS_FATAL_IF(mMaxTextureSize < 0, "MaxTextureSize has not been initialized yet.");
48 return mMaxTextureSize;
49}
50
51void DeviceInfo::setMaxTextureSize(int maxTextureSize) {
John Reckcf185f52019-04-11 16:11:24 -070052 DeviceInfo::get()->mMaxTextureSize = maxTextureSize;
53}
54
55void DeviceInfo::onDisplayConfigChanged() {
Alec Mouri22d753f2019-09-05 17:11:45 -070056 updateDisplayInfo();
57}
58
59void DeviceInfo::updateDisplayInfo() {
60 if (Properties::isolatedProcess) {
61 return;
62 }
63
64 if (mCurrentConfig == nullptr) {
65 mDisplaysSize = ADisplay_acquirePhysicalDisplays(&mDisplays);
66 LOG_ALWAYS_FATAL_IF(mDisplays == nullptr || mDisplaysSize <= 0,
67 "Failed to get physical displays: no connected display: %d!", mDisplaysSize);
68 for (size_t i = 0; i < mDisplaysSize; i++) {
69 ADisplayType type = ADisplay_getDisplayType(mDisplays[i]);
70 if (type == ADisplayType::DISPLAY_TYPE_INTERNAL) {
71 mPhysicalDisplayIndex = i;
72 break;
73 }
74 }
75 LOG_ALWAYS_FATAL_IF(mPhysicalDisplayIndex < 0, "Failed to find a connected physical display!");
Alec Mouri70f2a922019-11-20 11:10:29 -080076
77
78 // Since we now just got the primary display for the first time, then
79 // store the primary display metadata here.
80 ADisplay* primaryDisplay = mDisplays[mPhysicalDisplayIndex];
81 mMaxRefreshRate = ADisplay_getMaxSupportedFps(primaryDisplay);
82 ADataSpace dataspace;
83 AHardwareBuffer_Format format;
84 ADisplay_getPreferredWideColorFormat(primaryDisplay, &dataspace, &format);
85 switch (dataspace) {
86 case ADATASPACE_DISPLAY_P3:
87 mWideColorSpace =
88 SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
89 break;
90 case ADATASPACE_SCRGB:
91 mWideColorSpace = SkColorSpace::MakeSRGB();
92 break;
93 case ADATASPACE_SRGB:
94 // when sRGB is returned, it means wide color gamut is not supported.
95 mWideColorSpace = SkColorSpace::MakeSRGB();
96 break;
97 default:
98 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
99 }
100 switch (format) {
101 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
102 mWideColorType = SkColorType::kN32_SkColorType;
103 break;
104 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
105 mWideColorType = SkColorType::kRGBA_F16_SkColorType;
106 break;
107 default:
108 LOG_ALWAYS_FATAL("Unreachable: unsupported pixel format.");
109 }
Alec Mouri22d753f2019-09-05 17:11:45 -0700110 }
Alec Mouri70f2a922019-11-20 11:10:29 -0800111 // This method may have been called when the display config changed, so
112 // sync with the current configuration.
113 ADisplay* primaryDisplay = mDisplays[mPhysicalDisplayIndex];
114 status_t status = ADisplay_getCurrentConfig(primaryDisplay, &mCurrentConfig);
Alec Mouri22d753f2019-09-05 17:11:45 -0700115 LOG_ALWAYS_FATAL_IF(status, "Failed to get display config, error %d", status);
116 mWidth = ADisplayConfig_getWidth(mCurrentConfig);
117 mHeight = ADisplayConfig_getHeight(mCurrentConfig);
118 mDensity = ADisplayConfig_getDensity(mCurrentConfig);
119 mRefreshRate = ADisplayConfig_getFps(mCurrentConfig);
120 mCompositorOffset = ADisplayConfig_getCompositorOffsetNanos(mCurrentConfig);
121 mAppOffset = ADisplayConfig_getAppVsyncOffsetNanos(mCurrentConfig);
Derek Sollenberger17662382018-09-13 14:14:00 -0400122}
123
John Reck704bed02015-11-05 09:22:17 -0800124} /* namespace uirenderer */
125} /* namespace android */