xref: /aosp_15_r20/frameworks/base/libs/hwui/DeviceInfo.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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  */
16 #ifndef DEVICEINFO_H
17 #define DEVICEINFO_H
18 
19 #include <SkColorSpace.h>
20 #include <SkImageInfo.h>
21 #include <SkRefCnt.h>
22 #include <android/data_space.h>
23 
24 #include <mutex>
25 
26 #include "Properties.h"
27 #include "utils/Macros.h"
28 
29 namespace android {
30 namespace uirenderer {
31 
32 namespace renderthread {
33     class RenderThread;
34 }
35 
36 class DeviceInfo {
37     PREVENT_COPY_AND_ASSIGN(DeviceInfo);
38 
39 public:
40     static DeviceInfo* get();
getWidth()41     static int32_t getWidth() { return get()->mWidth; }
getHeight()42     static int32_t getHeight() { return get()->mHeight; }
43     // Gets the density in density-independent pixels
getDensity()44     static float getDensity() { return sDensity.load(); }
getVsyncPeriod()45     static int64_t getVsyncPeriod() { return get()->mVsyncPeriod; }
getCompositorOffset()46     static int64_t getCompositorOffset() { return get()->getCompositorOffsetInternal(); }
getAppOffset()47     static int64_t getAppOffset() { return get()->mAppVsyncOffsetNanos; }
48     // Sets the density in density-independent pixels
setDensity(float density)49     static void setDensity(float density) { sDensity.store(density); }
setWidth(int32_t width)50     static void setWidth(int32_t width) { get()->mWidth = width; }
setHeight(int32_t height)51     static void setHeight(int32_t height) { get()->mHeight = height; }
setRefreshRate(float refreshRate)52     static void setRefreshRate(float refreshRate) {
53         get()->mVsyncPeriod = static_cast<int64_t>(1000000000 / refreshRate);
54     }
setPresentationDeadlineNanos(int64_t deadlineNanos)55     static void setPresentationDeadlineNanos(int64_t deadlineNanos) {
56         get()->mPresentationDeadlineNanos = deadlineNanos;
57     }
setAppVsyncOffsetNanos(int64_t offsetNanos)58     static void setAppVsyncOffsetNanos(int64_t offsetNanos) {
59         get()->mAppVsyncOffsetNanos = offsetNanos;
60     }
61     static void setWideColorDataspace(ADataSpace dataspace);
62 
63     static void setSupportFp16ForHdr(bool supportFp16ForHdr);
isSupportFp16ForHdr()64     static bool isSupportFp16ForHdr() {
65         if (!Properties::hdr10bitPlus) {
66             return false;
67         }
68 
69         return get()->mSupportFp16ForHdr;
70     };
71 
72     static void setSupportRgba10101010ForHdr(bool supportRgba10101010ForHdr);
isSupportRgba10101010ForHdr()73     static bool isSupportRgba10101010ForHdr() {
74         if (!Properties::hdr10bitPlus) {
75             return false;
76         }
77 
78         return get()->mSupportRgba10101010ForHdr;
79     };
80 
81     static void setSupportMixedColorSpaces(bool supportMixedColorSpaces);
isSupportMixedColorSpaces()82     static bool isSupportMixedColorSpaces() { return get()->mSupportMixedColorSpaces; };
83 
84     // 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;
hasMaxTextureSize()87     bool hasMaxTextureSize() const { return mMaxTextureSize > 0; }
getWideColorSpace()88     sk_sp<SkColorSpace> getWideColorSpace() const { return mWideColorSpace; }
getWideColorType()89     SkColorType getWideColorType() {
90         static std::once_flag kFlag;
91         // lazily update display info from SF here, so that the call is performed by RenderThread.
92         std::call_once(kFlag, [&, this]() { updateDisplayInfo(); });
93         return mWideColorType;
94     }
95 
96     // This method should be called whenever the display refresh rate changes.
97     void onRefreshRateChanged(int64_t vsyncPeriod);
98 
99 private:
100     friend class renderthread::RenderThread;
101     static void setMaxTextureSize(int maxTextureSize);
102     void updateDisplayInfo();
getCompositorOffsetInternal()103     int64_t getCompositorOffsetInternal() const {
104         // Assume that SF takes around a millisecond to latch buffers after
105         // waking up
106         return mVsyncPeriod - (mPresentationDeadlineNanos - 1000000);
107     }
108 
109     DeviceInfo();
110     ~DeviceInfo() = default;
111 
112     int mMaxTextureSize;
113     sk_sp<SkColorSpace> mWideColorSpace = SkColorSpace::MakeSRGB();
114     bool mSupportFp16ForHdr = false;
115     bool mSupportRgba10101010ForHdr = false;
116     bool mSupportMixedColorSpaces = false;
117     SkColorType mWideColorType = SkColorType::kN32_SkColorType;
118     int mDisplaysSize = 0;
119     int mPhysicalDisplayIndex = -1;
120     int32_t mWidth = 1080;
121     int32_t mHeight = 1920;
122     int64_t mVsyncPeriod = 16666666;
123     // Magically corresponds with an sf offset of 0 for a sane default.
124     int64_t mPresentationDeadlineNanos = 17666666;
125     int64_t mAppVsyncOffsetNanos = 0;
126 
127     // Density is not retrieved from the ADisplay apis, so this may potentially
128     // be called on multiple threads.
129     // Unit is density-independent pixels
130     static std::atomic<float> sDensity;
131 };
132 
133 } /* namespace uirenderer */
134 } /* namespace android */
135 
136 #endif /* DEVICEINFO_H */
137