blob: 27be622699598042a5266b912934c6f6187d2939 [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
Peiyong Lin3bff1352018-12-11 07:56:07 -080019#include <SkImageInfo.h>
Alec Mourie55aa632020-04-15 19:46:05 -070020#include <android/data_space.h>
21
22#include <mutex>
John Reck8dc02f92017-07-17 09:55:02 -070023
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "utils/Macros.h"
John Reck704bed02015-11-05 09:22:17 -080025
26namespace android {
27namespace uirenderer {
28
Derek Sollenberger17662382018-09-13 14:14:00 -040029namespace renderthread {
30 class RenderThread;
31}
32
John Reck704bed02015-11-05 09:22:17 -080033class DeviceInfo {
34 PREVENT_COPY_AND_ASSIGN(DeviceInfo);
John Reck1bcacfd2017-11-03 10:12:19 -070035
John Reck704bed02015-11-05 09:22:17 -080036public:
John Reckcf185f52019-04-11 16:11:24 -070037 static DeviceInfo* get();
Alec Mouri22d753f2019-09-05 17:11:45 -070038 static float getMaxRefreshRate() { return get()->mMaxRefreshRate; }
39 static int32_t getWidth() { return get()->mWidth; }
40 static int32_t getHeight() { return get()->mHeight; }
Alec Mourid5fa1dc2020-04-06 13:15:28 -070041 // Gets the density in density-independent pixels
42 static float getDensity() { return sDensity.load(); }
Alec Mouri4a818f12019-11-19 16:17:53 -080043 static int64_t getVsyncPeriod() { return get()->mVsyncPeriod; }
Alec Mourie55aa632020-04-15 19:46:05 -070044 static int64_t getCompositorOffset() { return get()->getCompositorOffsetInternal(); }
45 static int64_t getAppOffset() { return get()->mAppVsyncOffsetNanos; }
Alec Mourid5fa1dc2020-04-06 13:15:28 -070046 // Sets the density in density-independent pixels
47 static void setDensity(float density) { sDensity.store(density); }
Alec Mourie55aa632020-04-15 19:46:05 -070048 static void setMaxRefreshRate(float refreshRate) { get()->mMaxRefreshRate = refreshRate; }
49 static void setWidth(int32_t width) { get()->mWidth = width; }
50 static void setHeight(int32_t height) { get()->mHeight = height; }
51 static void setRefreshRate(float refreshRate) {
52 get()->mVsyncPeriod = static_cast<int64_t>(1000000000 / refreshRate);
53 }
54 static void setPresentationDeadlineNanos(int64_t deadlineNanos) {
55 get()->mPresentationDeadlineNanos = deadlineNanos;
56 }
57 static void setAppVsyncOffsetNanos(int64_t offsetNanos) {
58 get()->mAppVsyncOffsetNanos = offsetNanos;
59 }
60 static void setWideColorDataspace(ADataSpace dataspace);
John Reck704bed02015-11-05 09:22:17 -080061
Derek Sollenberger17662382018-09-13 14:14:00 -040062 // this value is only valid after the GPU has been initialized and there is a valid graphics
63 // context or if you are using the HWUI_NULL_GPU
64 int maxTextureSize() const;
Peiyong Lin3bff1352018-12-11 07:56:07 -080065 sk_sp<SkColorSpace> getWideColorSpace() const { return mWideColorSpace; }
Alec Mourie55aa632020-04-15 19:46:05 -070066 SkColorType getWideColorType() {
67 static std::once_flag kFlag;
68 // lazily update display info from SF here, so that the call is performed by RenderThread.
69 std::call_once(kFlag, [&, this]() { updateDisplayInfo(); });
70 return mWideColorType;
71 }
John Reckcf185f52019-04-11 16:11:24 -070072
Alec Mouri4a818f12019-11-19 16:17:53 -080073 // This method should be called whenever the display refresh rate changes.
74 void onRefreshRateChanged(int64_t vsyncPeriod);
John Reck56428472018-03-16 17:27:17 -070075
John Reck704bed02015-11-05 09:22:17 -080076private:
Derek Sollenberger17662382018-09-13 14:14:00 -040077 friend class renderthread::RenderThread;
78 static void setMaxTextureSize(int maxTextureSize);
Alec Mouri22d753f2019-09-05 17:11:45 -070079 void updateDisplayInfo();
Alec Mourie55aa632020-04-15 19:46:05 -070080 int64_t getCompositorOffsetInternal() const {
81 // Assume that SF takes around a millisecond to latch buffers after
82 // waking up
83 return mVsyncPeriod - (mPresentationDeadlineNanos - 1000000);
84 }
Peiyong Lin1f6aa122018-09-10 16:28:08 -070085
Derek Sollenberger17662382018-09-13 14:14:00 -040086 DeviceInfo();
Alec Mourie55aa632020-04-15 19:46:05 -070087 ~DeviceInfo() = default;
John Reck704bed02015-11-05 09:22:17 -080088
John Reck704bed02015-11-05 09:22:17 -080089 int mMaxTextureSize;
Alec Mouri70f2a922019-11-20 11:10:29 -080090 sk_sp<SkColorSpace> mWideColorSpace = SkColorSpace::MakeSRGB();
91 SkColorType mWideColorType = SkColorType::kN32_SkColorType;
Alec Mouri22d753f2019-09-05 17:11:45 -070092 int mDisplaysSize = 0;
93 int mPhysicalDisplayIndex = -1;
94 float mMaxRefreshRate = 60.0;
95 int32_t mWidth = 1080;
96 int32_t mHeight = 1920;
Alec Mouri4a818f12019-11-19 16:17:53 -080097 int64_t mVsyncPeriod = 16666666;
Alec Mouri978a2cc2020-05-12 22:51:15 -070098 // Magically corresponds with an sf offset of 0 for a sane default.
99 int64_t mPresentationDeadlineNanos = 17666666;
Alec Mourie55aa632020-04-15 19:46:05 -0700100 int64_t mAppVsyncOffsetNanos = 0;
Alec Mourid5fa1dc2020-04-06 13:15:28 -0700101
102 // Density is not retrieved from the ADisplay apis, so this may potentially
103 // be called on multiple threads.
104 // Unit is density-independent pixels
105 static std::atomic<float> sDensity;
John Reck704bed02015-11-05 09:22:17 -0800106};
107
108} /* namespace uirenderer */
109} /* namespace android */
110
111#endif /* DEVICEINFO_H */