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 "utils/Macros.h"
27 
28 namespace android {
29 namespace uirenderer {
30 
31 namespace renderthread {
32     class RenderThread;
33 }
34 
35 class DeviceInfo {
36     PREVENT_COPY_AND_ASSIGN(DeviceInfo);
37 
38 public:
39     static DeviceInfo* get();
getWidth()40     static int32_t getWidth() { return get()->mWidth; }
getHeight()41     static int32_t getHeight() { return get()->mHeight; }
42     // Gets the density in density-independent pixels
getDensity()43     static float getDensity() { return sDensity.load(); }
getVsyncPeriod()44     static int64_t getVsyncPeriod() { return get()->mVsyncPeriod; }
getCompositorOffset()45     static int64_t getCompositorOffset() { return get()->getCompositorOffsetInternal(); }
getAppOffset()46     static int64_t getAppOffset() { return get()->mAppVsyncOffsetNanos; }
47     // Sets the density in density-independent pixels
setDensity(float density)48     static void setDensity(float density) { sDensity.store(density); }
setWidth(int32_t width)49     static void setWidth(int32_t width) { get()->mWidth = width; }
setHeight(int32_t height)50     static void setHeight(int32_t height) { get()->mHeight = height; }
setRefreshRate(float refreshRate)51     static void setRefreshRate(float refreshRate) {
52         get()->mVsyncPeriod = static_cast<int64_t>(1000000000 / refreshRate);
53     }
setPresentationDeadlineNanos(int64_t deadlineNanos)54     static void setPresentationDeadlineNanos(int64_t deadlineNanos) {
55         get()->mPresentationDeadlineNanos = deadlineNanos;
56     }
setAppVsyncOffsetNanos(int64_t offsetNanos)57     static void setAppVsyncOffsetNanos(int64_t offsetNanos) {
58         get()->mAppVsyncOffsetNanos = offsetNanos;
59     }
60     static void setWideColorDataspace(ADataSpace dataspace);
61 
62     static void setSupportFp16ForHdr(bool supportFp16ForHdr);
isSupportFp16ForHdr()63     static bool isSupportFp16ForHdr() { return get()->mSupportFp16ForHdr; };
64 
65     static void setSupportMixedColorSpaces(bool supportMixedColorSpaces);
isSupportMixedColorSpaces()66     static bool isSupportMixedColorSpaces() { return get()->mSupportMixedColorSpaces; };
67 
68     // this value is only valid after the GPU has been initialized and there is a valid graphics
69     // context or if you are using the HWUI_NULL_GPU
70     int maxTextureSize() const;
getWideColorSpace()71     sk_sp<SkColorSpace> getWideColorSpace() const { return mWideColorSpace; }
getWideColorType()72     SkColorType getWideColorType() {
73         static std::once_flag kFlag;
74         // lazily update display info from SF here, so that the call is performed by RenderThread.
75         std::call_once(kFlag, [&, this]() { updateDisplayInfo(); });
76         return mWideColorType;
77     }
78 
79     // This method should be called whenever the display refresh rate changes.
80     void onRefreshRateChanged(int64_t vsyncPeriod);
81 
82 private:
83     friend class renderthread::RenderThread;
84     static void setMaxTextureSize(int maxTextureSize);
85     void updateDisplayInfo();
getCompositorOffsetInternal()86     int64_t getCompositorOffsetInternal() const {
87         // Assume that SF takes around a millisecond to latch buffers after
88         // waking up
89         return mVsyncPeriod - (mPresentationDeadlineNanos - 1000000);
90     }
91 
92     DeviceInfo();
93     ~DeviceInfo() = default;
94 
95     int mMaxTextureSize;
96     sk_sp<SkColorSpace> mWideColorSpace = SkColorSpace::MakeSRGB();
97     bool mSupportFp16ForHdr = false;
98     bool mSupportMixedColorSpaces = false;
99     SkColorType mWideColorType = SkColorType::kN32_SkColorType;
100     int mDisplaysSize = 0;
101     int mPhysicalDisplayIndex = -1;
102     int32_t mWidth = 1080;
103     int32_t mHeight = 1920;
104     int64_t mVsyncPeriod = 16666666;
105     // Magically corresponds with an sf offset of 0 for a sane default.
106     int64_t mPresentationDeadlineNanos = 17666666;
107     int64_t mAppVsyncOffsetNanos = 0;
108 
109     // Density is not retrieved from the ADisplay apis, so this may potentially
110     // be called on multiple threads.
111     // Unit is density-independent pixels
112     static std::atomic<float> sDensity;
113 };
114 
115 } /* namespace uirenderer */
116 } /* namespace android */
117 
118 #endif /* DEVICEINFO_H */
119