1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BRIGHTNESS_CONTROLLER_H_
#define _BRIGHTNESS_CONTROLLER_H_
#include <drm/samsung_drm.h>
#include <utils/Looper.h>
#include <utils/Mutex.h>
#include <fstream>
#include <thread>
#include "ExynosDisplayDrmInterface.h"
/**
* Brightness change requests come from binder calls or HWC itself.
* The request could be applied via next drm commit or immeditely via sysfs.
*
* To make it simple, setDisplayBrightness from SF, if not triggering a HBM on/off,
* will be applied immediately via sysfs path. All other requests will be applied via next
* drm commit.
*
* Sysfs path is faster than drm path. So if there is a pending drm commit that may
* change brightness level, sfsfs path task should wait until it has completed.
*/
class BrightnessController {
public:
using HdrLayerState = displaycolor::HdrLayerState;
class DimmingMsgHandler : public virtual ::android::MessageHandler {
public:
enum {
MSG_QUIT,
MSG_DIMMING_OFF,
};
DimmingMsgHandler(BrightnessController* bc) : mBrightnessController(bc) {}
void handleMessage(const Message& message) override;
private:
BrightnessController* mBrightnessController;
};
BrightnessController(int32_t panelIndex, std::function<void(void)> refresh,
std::function<void(void)> updateDcLhbm);
~BrightnessController();
BrightnessController(int32_t panelIndex);
int initDrm(const DrmDevice& drmDevice,
const DrmConnector& connector);
int processEnhancedHbm(bool on);
int processDisplayBrightness(float bl, const nsecs_t vsyncNs, bool waitPresent = false);
int processLocalHbm(bool on);
int processDimBrightness(bool on);
bool isDbmSupported() { return mDbmSupported; }
int applyPendingChangeViaSysfs(const nsecs_t vsyncNs);
bool validateLayerBrightness(float brightness);
/**
* processInstantHbm for GHBM UDFPS
* - on true: turn on HBM at next frame with peak brightness
* false: turn off HBM at next frame and use system display brightness
* from processDisplayBrightness
*/
int processInstantHbm(bool on);
/**
* updateFrameStates
* - hdrState: hdr layer size in this frame
* - sdrDim: whether any dimmed sdr layer in this frame
*/
void updateFrameStates(HdrLayerState hdrState, bool sdrDim);
/**
* Dim ratio to keep the sdr brightness unchange after an instant hbm on with peak brightness.
*/
float getSdrDimRatioForInstantHbm();
void onClearDisplay(bool needModeClear);
/**
* apply brightness change on drm path.
* Note: only this path can hold the lock for a long time
*/
int prepareFrameCommit(ExynosDisplay& display,
const DrmConnector& connector,
ExynosDisplayDrmInterface::DrmModeAtomicReq& drmReq,
const bool mixedComposition,
bool& ghbmSync, bool& lhbmSync, bool& blSync);
bool isGhbmSupported() { return mGhbmSupported; }
bool isLhbmSupported() { return mLhbmSupported; }
bool isGhbmOn() {
std::lock_guard<std::recursive_mutex> lock(mBrightnessMutex);
return mGhbm.get() != HbmMode::OFF;
}
bool isLhbmOn() {
std::lock_guard<std::recursive_mutex> lock(mBrightnessMutex);
return mLhbm.get();
}
int checkSysfsStatus(const std::string& file, const std::vector<std::string>& expectedValue,
const nsecs_t timeoutNs);
bool fileExists(const std::string& file) {
struct stat sb;
return stat(file.c_str(), &sb) == 0;
}
void resetLhbmState();
uint32_t getBrightnessLevel() {
std::lock_guard<std::recursive_mutex> lock(mBrightnessMutex);
return mBrightnessLevel.get();
}
bool isDimSdr() {
std::lock_guard<std::recursive_mutex> lock(mBrightnessMutex);
return mInstantHbmReq.get();
}
HdrLayerState getHdrLayerState() {
return mHdrLayerState.get();
}
bool isSupported() {
// valid mMaxBrightness means both brightness and max_brightness sysfs exist
return mMaxBrightness > 0;
}
void dump(String8 &result);
void setOutdoorVisibility(LbeState state);
int updateCabcMode();
const std::string GetPanelSysfileByIndex(const char *file_pattern) {
String8 nodeName;
nodeName.appendFormat(file_pattern, mPanelIndex);
return nodeName.c_str();
}
const std::string GetPanelRefreshRateSysfile() {
String8 nodeName;
nodeName.appendFormat(kRefreshrateFileNode,
mPanelIndex == 0 ? "primary"
: mPanelIndex == 1 ? "secondary"
: "unknown");
return nodeName.c_str();
}
struct BrightnessTable {
float mBriStart;
float mBriEnd;
uint32_t mBklStart;
uint32_t mBklEnd;
uint32_t mNitsStart;
uint32_t mNitsEnd;
BrightnessTable() {}
BrightnessTable(const brightness_attribute &attr)
: mBriStart(static_cast<float>(attr.percentage.min) / 100.0f),
mBriEnd(static_cast<float>(attr.percentage.max) / 100.0f),
mBklStart(attr.level.min),
mBklEnd(attr.level.max),
mNitsStart(attr.nits.min),
mNitsEnd(attr.nits.max) {}
};
const BrightnessTable *getBrightnessTable() { return mBrightnessTable; }
/*
* WARNING: This enum is parsed by Battery Historian. Add new values, but
* do not modify/remove existing ones. Alternatively, consult with the
* Battery Historian team (b/239640926).
*/
enum class BrightnessRange : uint32_t {
NORMAL = 0,
HBM = 1,
MAX,
};
/*
* WARNING: This enum is parsed by Battery Historian. Add new values, but
* do not modify/remove existing ones. Alternatively, consult with the
* Battery Historian team (b/239640926).
*/
enum class HbmMode {
OFF = 0,
ON_IRC_ON = 1,
ON_IRC_OFF = 2,
};
/*
* LHBM command need take a couple of frames to become effective
* DISABLED - finish sending disabling command to panel
* ENABLED - panel finishes boosting brightness to the peak value
* ENABLING - finish sending enabling command to panel (panel begins boosting brightness)
* Note: the definition should be consistent with kernel driver
*/
enum class LhbmMode {
DISABLED = 0,
ENABLED = 1,
ENABLING = 2,
};
/*
* BrightnessDimmingUsage:
* NORMAL- enable dimming
* HBM- enable dimming only for hbm transition
* NONE- disable dimming
*
* WARNING: This enum is parsed by Battery Historian. Add new values, but
* do not modify/remove existing ones. Alternatively, consult with the
* Battery Historian team (b/239640926).
*/
enum class BrightnessDimmingUsage {
NORMAL = 0,
HBM = 1,
NONE,
};
static constexpr const char *kLocalHbmModeFileNode =
"/sys/class/backlight/panel%d-backlight/local_hbm_mode";
static constexpr const char* kDimBrightnessFileNode =
"/sys/class/backlight/panel%d-backlight/dim_brightness";
static constexpr const char* kRefreshrateFileNode =
"/sys/devices/platform/exynos-drm/%s-panel/refresh_rate";
private:
// sync brightness change for mixed composition when there is more than 50% luminance change.
// The percentage is calculated as:
// (big_lumi - small_lumi) / small_lumi
// For mixed composition, if remove brightness animations, the minimum brightness jump is
// between nbm peak and hbm peak. 50% will cover known panels
static constexpr float kBrightnessSyncThreshold = 0.5f;
// Worst case for panel with brightness range 2 nits to 1000 nits.
static constexpr float kGhbmMinDimRatio = 0.002;
static constexpr int32_t kHbmDimmingTimeUs = 5000000;
static constexpr const char *kGlobalHbmModeFileNode =
"/sys/class/backlight/panel%d-backlight/hbm_mode";
static constexpr const char* kDimmingUsagePropName =
"vendor.display.%d.brightness.dimming.usage";
static constexpr const char* kDimmingHbmTimePropName =
"vendor.display.%d.brightness.dimming.hbm_time";
int queryBrightness(float brightness, bool* ghbm = nullptr, uint32_t* level = nullptr,
float *nits = nullptr);
void initBrightnessTable(const DrmDevice& device, const DrmConnector& connector);
void initBrightnessSysfs();
void initCabcSysfs();
void initDimmingUsage();
int applyBrightnessViaSysfs(uint32_t level);
int applyCabcModeViaSysfs(uint8_t mode);
int updateStates() REQUIRES(mBrightnessMutex);
void dimmingThread();
void processDimmingOff();
void parseHbmModeEnums(const DrmProperty& property);
void printBrightnessStates(const char* path) REQUIRES(mBrightnessMutex);
bool mLhbmSupported = false;
bool mGhbmSupported = false;
bool mDbmSupported = false;
bool mBrightnessIntfSupported = false;
BrightnessTable mBrightnessTable[toUnderlying(BrightnessRange::MAX)];
int32_t mPanelIndex;
DrmEnumParser::MapHal2DrmEnum mHbmModeEnums;
// brightness state
std::recursive_mutex mBrightnessMutex;
// requests
CtrlValue<bool> mEnhanceHbmReq GUARDED_BY(mBrightnessMutex);
CtrlValue<bool> mLhbmReq GUARDED_BY(mBrightnessMutex);
CtrlValue<float> mBrightnessFloatReq GUARDED_BY(mBrightnessMutex);
CtrlValue<bool> mInstantHbmReq GUARDED_BY(mBrightnessMutex);
// states to drm after updateStates call
CtrlValue<uint32_t> mBrightnessLevel GUARDED_BY(mBrightnessMutex);
CtrlValue<HbmMode> mGhbm GUARDED_BY(mBrightnessMutex);
CtrlValue<bool> mDimming GUARDED_BY(mBrightnessMutex);
CtrlValue<bool> mLhbm GUARDED_BY(mBrightnessMutex);
CtrlValue<bool> mSdrDim GUARDED_BY(mBrightnessMutex);
CtrlValue<bool> mPrevSdrDim GUARDED_BY(mBrightnessMutex);
CtrlValue<bool> mDimBrightnessReq GUARDED_BY(mBrightnessMutex);
// Indicating if the last LHBM on has changed the brightness level
bool mLhbmBrightnessAdj = false;
std::function<void(void)> mFrameRefresh;
CtrlValue<HdrLayerState> mHdrLayerState;
// these are used by sysfs path to wait drm path bl change task
// indicationg an unchecked LHBM change in drm path
std::atomic<bool> mUncheckedLhbmRequest = false;
std::atomic<bool> mPendingLhbmStatus = false;
// indicationg an unchecked GHBM change in drm path
std::atomic<bool> mUncheckedGbhmRequest = false;
std::atomic<HbmMode> mPendingGhbmStatus = HbmMode::OFF;
// indicating an unchecked brightness change in drm path
std::atomic<bool> mUncheckedBlRequest = false;
std::atomic<uint32_t> mPendingBl = 0;
// these are dimming related
BrightnessDimmingUsage mBrightnessDimmingUsage = BrightnessDimmingUsage::NORMAL;
bool mHbmDimming GUARDED_BY(mBrightnessMutex) = false;
int32_t mHbmDimmingTimeUs = 0;
std::thread mDimmingThread;
std::atomic<bool> mDimmingThreadRunning;
::android::sp<::android::Looper> mDimmingLooper;
::android::sp<DimmingMsgHandler> mDimmingHandler;
// sysfs path
std::ofstream mBrightnessOfs;
uint32_t mMaxBrightness = 0; // read from sysfs
std::ofstream mCabcModeOfs;
bool mCabcSupport = false;
uint32_t mDimBrightness = 0;
// Note IRC or dimming is not in consideration for now.
float mDisplayWhitePointNits = 0;
float mPrevDisplayWhitePointNits = 0;
std::function<void(void)> mUpdateDcLhbm;
// state for control CABC state
enum class CabcMode {
OFF = 0,
CABC_UI_MODE,
CABC_STILL_MODE,
CABC_MOVIE_MODE,
};
static constexpr const char* kLocalCabcModeFileNode =
"/sys/class/backlight/panel%d-backlight/cabc_mode";
std::recursive_mutex mCabcModeMutex;
bool mOutdoorVisibility GUARDED_BY(mCabcModeMutex) = false;
bool isHdrLayerOn() { return mHdrLayerState.get() == HdrLayerState::kHdrLarge; }
CtrlValue<CabcMode> mCabcMode GUARDED_BY(mCabcModeMutex);
};
#endif // _BRIGHTNESS_CONTROLLER_H_
|