blob: fb58a69747b36ce77257024bc380fc1e09776f43 [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 */
16#ifndef DEVICEINFO_H
17#define DEVICEINFO_H
18
Kevin Lubick07d6aae2022-04-01 14:03:11 -040019#include <SkColorSpace.h>
Peiyong Lin3bff1352018-12-11 07:56:07 -080020#include <SkImageInfo.h>
Kevin Lubick07d6aae2022-04-01 14:03:11 -040021#include <SkRefCnt.h>
Alec Mourie55aa632020-04-15 19:46:05 -070022#include <android/data_space.h>
23
24#include <mutex>
John Reck8dc02f92017-07-17 09:55:02 -070025
Alec Mouri22ab7f32023-09-06 02:11:56 +000026#include "Properties.h"
John Reck1bcacfd2017-11-03 10:12:19 -070027#include "utils/Macros.h"
John Reck704bed02015-11-05 09:22:17 -080028
29namespace android {
30namespace uirenderer {
31
Derek Sollenberger17662382018-09-13 14:14:00 -040032namespace renderthread {
33 class RenderThread;
34}
35
John Reck704bed02015-11-05 09:22:17 -080036class DeviceInfo {
37 PREVENT_COPY_AND_ASSIGN(DeviceInfo);
John Reck1bcacfd2017-11-03 10:12:19 -070038
John Reck704bed02015-11-05 09:22:17 -080039public:
John Reckcf185f52019-04-11 16:11:24 -070040 static DeviceInfo* get();
Alec Mouri22d753f2019-09-05 17:11:45 -070041 static int32_t getWidth() { return get()->mWidth; }
42 static int32_t getHeight() { return get()->mHeight; }
Alec Mourid5fa1dc2020-04-06 13:15:28 -070043 // Gets the density in density-independent pixels
44 static float getDensity() { return sDensity.load(); }
Alec Mouri4a818f12019-11-19 16:17:53 -080045 static int64_t getVsyncPeriod() { return get()->mVsyncPeriod; }
Alec Mourie55aa632020-04-15 19:46:05 -070046 static int64_t getCompositorOffset() { return get()->getCompositorOffsetInternal(); }
47 static int64_t getAppOffset() { return get()->mAppVsyncOffsetNanos; }
Alec Mourid5fa1dc2020-04-06 13:15:28 -070048 // Sets the density in density-independent pixels
49 static void setDensity(float density) { sDensity.store(density); }
Alec Mourie55aa632020-04-15 19:46:05 -070050 static void setWidth(int32_t width) { get()->mWidth = width; }
51 static void setHeight(int32_t height) { get()->mHeight = height; }
52 static void setRefreshRate(float refreshRate) {
53 get()->mVsyncPeriod = static_cast<int64_t>(1000000000 / refreshRate);
54 }
55 static void setPresentationDeadlineNanos(int64_t deadlineNanos) {
56 get()->mPresentationDeadlineNanos = deadlineNanos;
57 }
58 static void setAppVsyncOffsetNanos(int64_t offsetNanos) {
59 get()->mAppVsyncOffsetNanos = offsetNanos;
60 }
61 static void setWideColorDataspace(ADataSpace dataspace);
John Reck704bed02015-11-05 09:22:17 -080062
Sally Qi02068d52022-08-30 16:29:14 -070063 static void setSupportFp16ForHdr(bool supportFp16ForHdr);
Alec Mouri22ab7f32023-09-06 02:11:56 +000064 static bool isSupportFp16ForHdr() {
65 if (!Properties::hdr10bitPlus) {
66 return false;
67 }
68
69 return get()->mSupportFp16ForHdr;
70 };
Sally Qi02068d52022-08-30 16:29:14 -070071
Alec Mouri4a3035e2024-03-04 23:12:42 +000072 static void setSupportRgba10101010ForHdr(bool supportRgba10101010ForHdr);
73 static bool isSupportRgba10101010ForHdr() {
74 if (!Properties::hdr10bitPlus) {
75 return false;
76 }
77
78 return get()->mSupportRgba10101010ForHdr;
79 };
80
Sally Qi830b0412022-12-14 16:26:49 -080081 static void setSupportMixedColorSpaces(bool supportMixedColorSpaces);
82 static bool isSupportMixedColorSpaces() { return get()->mSupportMixedColorSpaces; };
83
Derek Sollenberger17662382018-09-13 14:14:00 -040084 // this value is only valid after the GPU has been initialized and there is a valid graphics
85 // context or if you are using the HWUI_NULL_GPU
86 int maxTextureSize() const;
Peiyong Lin3bff1352018-12-11 07:56:07 -080087 sk_sp<SkColorSpace> getWideColorSpace() const { return mWideColorSpace; }
Alec Mourie55aa632020-04-15 19:46:05 -070088 SkColorType getWideColorType() {
89 static std::once_flag kFlag;
90 // lazily update display info from SF here, so that the call is performed by RenderThread.
91 std::call_once(kFlag, [&, this]() { updateDisplayInfo(); });
92 return mWideColorType;
93 }
John Reckcf185f52019-04-11 16:11:24 -070094
Alec Mouri4a818f12019-11-19 16:17:53 -080095 // This method should be called whenever the display refresh rate changes.
96 void onRefreshRateChanged(int64_t vsyncPeriod);
John Reck56428472018-03-16 17:27:17 -070097
John Reck704bed02015-11-05 09:22:17 -080098private:
Derek Sollenberger17662382018-09-13 14:14:00 -040099 friend class renderthread::RenderThread;
100 static void setMaxTextureSize(int maxTextureSize);
Alec Mouri22d753f2019-09-05 17:11:45 -0700101 void updateDisplayInfo();
Alec Mourie55aa632020-04-15 19:46:05 -0700102 int64_t getCompositorOffsetInternal() const {
103 // Assume that SF takes around a millisecond to latch buffers after
104 // waking up
105 return mVsyncPeriod - (mPresentationDeadlineNanos - 1000000);
106 }
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700107
Derek Sollenberger17662382018-09-13 14:14:00 -0400108 DeviceInfo();
Alec Mourie55aa632020-04-15 19:46:05 -0700109 ~DeviceInfo() = default;
John Reck704bed02015-11-05 09:22:17 -0800110
John Reck704bed02015-11-05 09:22:17 -0800111 int mMaxTextureSize;
Alec Mouri70f2a922019-11-20 11:10:29 -0800112 sk_sp<SkColorSpace> mWideColorSpace = SkColorSpace::MakeSRGB();
Sally Qi02068d52022-08-30 16:29:14 -0700113 bool mSupportFp16ForHdr = false;
Alec Mouri4a3035e2024-03-04 23:12:42 +0000114 bool mSupportRgba10101010ForHdr = false;
Sally Qi830b0412022-12-14 16:26:49 -0800115 bool mSupportMixedColorSpaces = false;
Alec Mouri70f2a922019-11-20 11:10:29 -0800116 SkColorType mWideColorType = SkColorType::kN32_SkColorType;
Alec Mouri22d753f2019-09-05 17:11:45 -0700117 int mDisplaysSize = 0;
118 int mPhysicalDisplayIndex = -1;
Alec Mouri22d753f2019-09-05 17:11:45 -0700119 int32_t mWidth = 1080;
120 int32_t mHeight = 1920;
Alec Mouri4a818f12019-11-19 16:17:53 -0800121 int64_t mVsyncPeriod = 16666666;
Alec Mouri978a2cc2020-05-12 22:51:15 -0700122 // Magically corresponds with an sf offset of 0 for a sane default.
123 int64_t mPresentationDeadlineNanos = 17666666;
Alec Mourie55aa632020-04-15 19:46:05 -0700124 int64_t mAppVsyncOffsetNanos = 0;
Alec Mourid5fa1dc2020-04-06 13:15:28 -0700125
126 // Density is not retrieved from the ADisplay apis, so this may potentially
127 // be called on multiple threads.
128 // Unit is density-independent pixels
129 static std::atomic<float> sDensity;
John Reck704bed02015-11-05 09:22:17 -0800130};
131
132} /* namespace uirenderer */
133} /* namespace android */
134
135#endif /* DEVICEINFO_H */