米系色彩空间小研究喵

荆雪

小米 HyperOS 4 色彩风格与三大护眼模式解析喵

一、小米官方底层全景架构(Framework 到 SDM 逆向)

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
flowchart TD
subgraph Framework [1. 框架层 Framework]
UI[MISettings: ScreenEffectFragment] -->|设置切换| DFMS[DisplayFeatureManagerService]
DFMS -->|AIDL setDisplayFeature| DFHAL[vendor.xiaomi.hardware.displayfeature]
end

subgraph HalCore [2. HAL 核心状态机 displayfeature.default.so]
DFHAL --> DispatchTable{FourCC 转译分发}
DispatchTable -->|mode=0| M_NORM[Norm: 生动]
DispatchTable -->|mode=1| M_EHAN[Ehan: 鲜艳]
DispatchTable -->|mode=2| M_STAN[Stan: 原色]
DispatchTable -->|mode=3| M_EYEC[EyeC: 经典护眼]
DispatchTable -->|mode=26| M_EXPT[EXPT: 高级色彩风格]
DispatchTable -->|mode=31| M_PAPQ[PaPq: 纸质护眼]
DispatchTable -->|mode=70| M_RECS[RECS: 节律护眼]
end

subgraph AlgoCalib [3. 标定与算法计算层]
XML_PANEL[cct_pcc.txt / 面板 XML] --> EyeCareAlgo[libeyecare.so: PCCGen / FindPCC]
XML_RHY[mdss_dsi_*.xml] --> RhyAlgo[librhytheyecare.so: 24h 5-Zone 褪黑素模型]
XML_DF[df_default.xml] --> PaperAlgo[libpaperMode.so: 纸质矩阵乘法]

M_EYEC --> EyeCareAlgo
M_RECS --> RhyAlgo
M_PAPQ --> PaperAlgo
end

subgraph HwEgress [4. 硬件出口层 Hardware Egress]
PaperAlgo -->|M_paper * M_eyecare| StcService[出口 1: SnapdragonColor / MiStcService CMD 22]
EyeCareAlgo -->|PCC RGB gains| StcService
RhyAlgo -->|9 PCC coeffs| StcService

CLSTC[libmiClstc.so: SDM DMA 插件] -->|17x17x17 3D LUT| SDM[高通 SDM 硬件混叠管线]
StcService --> SDM

M_EYEC -.->|色彩变动期间| PU_CTRL[HandlePUMode: QService CMD 19 挂起 PU]
PU_CTRL -->|全帧更新保障无撕裂| SDM
end

二、小米三个护眼模式具体算法与数学推导

1. 经典护眼(Classic Eye Care, mode=3)

  • 标定依据:/odm/etc/disp0/<panel>/cct_pcc.txt。
  • 数据结构:按 50K 间隔离散采样的查找表,覆盖 3000K 至 8000K。
    • $3000\text{K}$(极致暖色):$R=1.0000, G=0.6209, B=0.2351$
    • $4500\text{K}$(标准护眼):$R=1.0000, G=0.8505, B=0.5722$
    • $6500\text{K}$(标准 D65):$R=1.0000, G=1.0000, B=0.9944$
    • $8000\text{K}$(冷白白点):$R=0.8194, G=0.8597, B=1.0000$
  • **反汇编算法实现 (EyeCare::FindPCC):
    给定滑块等级 $L \in [0, 35]$,计算对应的色温 $\text{CCT} = \text{MinCCT} + (35 - L) \times \frac{\text{MaxCCT} - \text{MinCCT}}{35}$。
    在查找表中定位相邻下界档位 $L_{\text{low}}$ 与上界档位 $L_{\text{high}}$,计算比率 $\alpha = \frac{\text{CCT} - \text{CCT}_{\text{low}}}{\text{Step}}$,执行
    分段线性插值 (Piecewise Linear Interpolation)**:
    $$\vec{g}{\text{pcc}} = (1 - \alpha) \cdot \vec{g}[L{\text{low}}] + \alpha \cdot \vec{g}[L_{\text{high}}]$$

2. 纸质护眼(Paper Mode, mode=31)

  • 核心原理:模拟纸张在环境光下的反射质感与油墨发色,通过矩阵变换压缩色彩饱和度或转为高舒适度灰阶。
  • **数学模型 (PaperMode::setPCCConfigPaperMode)**:
    从 df_default.xml 中读取预定义的 $3\times 3$ 纸质矩阵 $M_{\text{paper}}$:
    • **全彩纸质 (paperCoeff0)**:单位矩阵
      $$M_0 = \begin{bmatrix} 1 & 0 & 0 \ 0 & 1 & 0 \ 0 & 0 & 1 \end{bmatrix}$$
    • **淡彩纸质 (paperCoeff1)**:色彩空间降饱和度重映射矩阵
      $$M_1 = \begin{bmatrix} 0.686 & 0.262 & 0.052 \ 0.146 & 0.816 & 0.038 \ 0.080 & 0.268 & 0.652 \end{bmatrix}$$
    • **黑白纸质 (paperCoeff2)**:Rec.709 亮度单色矩阵(去除蓝光并保留对比度)
      $$M_2 = \begin{bmatrix} 0.218 & 0.693 & 0.089 \ 0.218 & 0.693 & 0.089 \ 0.218 & 0.693 & 0.089 \end{bmatrix}$$
  • 复合叠加机制:
    纸质护眼可与色温增益无缝叠加。在反汇编 PaperMode::setPCCCoeffsToHWC 中证实,系统将 $M_{\text{paper}}$ 与护眼色温矩阵 $M_{\text{eyecare}}$ 进行矩阵点乘:
    $$M_{\text{final}} = M_{\text{paper}} \times M_{\text{eyecare}}$$
    打包这 9 个 double 系数,通过 IMiStcService::dispatch(22) 注入硬件管线。

3. 节律护眼(Rhythmic Eye Care, mode=70,小米青山护眼)

  • 核心原理:基于人体生物钟褪黑素分泌模型,根据本地时间(24 小时动态衰减)与当前前台应用类型(apptype),动态连续平滑调节屏幕光谱。
  • **时区五阶段模型 (RhyEyeCare::ComputeRhyCCT)**(来自面板 XML 标定):
    1. **日出升温区 (RiseZone, 早晨 06:00–09:00)**:色温从夜间 4800K 平滑攀升至日间 6500K。
    2. **日间恒温区 (DayZone, 09:00–17:00)**:维持 6600K–7100K 高清晰明亮色温。
    3. **午后渐降区 (PmDropZone, 17:00–20:00)**:随日落自然下降至 5500K–5000K。
    4. **入夜衰减区 (NightDropZone, 20:00–23:00)**:加速抑制蓝光,降至 4500K–4100K。
    5. **深夜维持区 (NightZone, 23:00–06:00)**:固定在 4100K 极限防蓝光色温,促进睡眠。
  • **三次多项式连续拟合公式 (RhyD65Coeff0..8)**:
    在任意计算时刻的色温 $\text{CCT}$ 下,$3\times 3$ 增益矩阵的各通道主对角线增益由以下三次多项式严格生成(非对角项由于阶数为 $10^{-28}$ 可忽略):
    $$g_R(\text{CCT}) = -1.05827 \times 10^{-11} \cdot \text{CCT}^3 + 1.54963 \times 10^{-7} \cdot \text{CCT}^2 - 7.46168 \times 10^{-4} \cdot \text{CCT} + 2.18254$$
    $$g_G(\text{CCT}) = -1.03528 \times 10^{-11} \cdot \text{CCT}^3 + 1.35378 \times 10^{-7} \cdot \text{CCT}^2 - 4.51272 \times 10^{-4} \cdot \text{CCT} + 1.03007$$
    $$g_B(\text{CCT}) = -1.65666 \times 10^{-11} \cdot \text{CCT}^3 + 2.44478 \times 10^{-7} \cdot \text{CCT}^2 - 9.49496 \times 10^{-4} \cdot \text{CCT} + 1.36344$$
  • 实测数值验证(输入 CCT 验算结果):
    • $6500\text{K}$:$R=0.9733, G=0.9734, B=0.9713$(各通道平衡,精准呈现 D65 白点)
    • $5000\text{K}$:$R=1.0029, G=0.8641, B=0.6571$(蓝通道压低至 65.7%)
    • $4500\text{K}$:$R=0.9984, G=0.7973, B=0.5318$(蓝通道斩半至 53.2%)
    • $3800\text{K}$:$R=1.0041, G=0.7020, B=0.3766$(深夜模式,蓝通道深度压低至 37.6%)
  • 时序平滑执行:
    系统启动后台定时器,步进时间 $T_{\text{step}} = 64\text{ms}$,阶梯式过渡时间 $T_{\text{dimming}} = 4\text{s}$,人眼完全无法察觉任何突变台阶。

三、小米 SDM 硬件插件:CLSTC 与 SnapdragonColor

在供体系统中,小米并非简单地写寄存器,而是深度改造了高通 SDM 管线:

  1. **libmiClstc.so (Mi Color Layer Spatial-Temporal Control)**:
    • 实现了 qClient::BnQClient(挂在 QService 上)与 sdm::GenericIntf(挂在高通 SDM 硬件混叠器上)。
    • 解析 17×17×17 的 3D LUT 数据(ClstcLut0.txt 至 ClstcLut8.txt)以及逆 Gamma / 正 Gamma 曲线(clstc_igc_0.txt / clstc_gc_0.txt)。
    • 在 SDM 的每个 DMA Pipe 合成前,直接硬件注入 3D LUT 做色彩重映射与 HDR 色调映射。
  2. **libmi-stc-HW-modulate.so (SnapdragonColor)**:
    • 实现了 snapdragoncolor::MiStcService(Binder 描述符 snapdragoncolor.IMiStcService)。
    • 提供后台调光线程(threadDimmingPA, threadDimmingGC, threadDimmingIGC),在硬件管线内完成高动态平滑过渡。