summaryrefslogtreecommitdiff
path: root/libhwc2.1/libmaindisplay/ExynosPrimaryDisplayModule.cpp
blob: 3f3439ff2719649cc15609c819ab36e51f913180 (plain)
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
/*
 * Copyright (C) 2022 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.
 */

#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)

#include "ExynosPrimaryDisplayModule.h"

#include <cutils/properties.h>

#include "ExynosHWCHelper.h"

#define OP_MANAGER_LOGD(msg, ...)                                                         \
    ALOGD("[%s] OperationRateManager::%s:" msg, mDisplay->mDisplayName.c_str(), __func__, \
          ##__VA_ARGS__)
#define OP_MANAGER_LOGI(msg, ...)                                                         \
    ALOGI("[%s] OperationRateManager::%s:" msg, mDisplay->mDisplayName.c_str(), __func__, \
          ##__VA_ARGS__)
#define OP_MANAGER_LOGE(msg, ...)                                                         \
    ALOGE("[%s] OperationRateManager::%s:" msg, mDisplay->mDisplayName.c_str(), __func__, \
          ##__VA_ARGS__)

using namespace zuma;

ExynosPrimaryDisplayModule::ExynosPrimaryDisplayModule(uint32_t index, ExynosDevice* device,
                                                       const std::string& displayName)
      : gs201::ExynosPrimaryDisplayModule(index, device, displayName) {
    int32_t hs_hz = property_get_int32("vendor.primarydisplay.op.hs_hz", 0);
    int32_t ns_hz = property_get_int32("vendor.primarydisplay.op.ns_hz", 0);

    if (hs_hz && ns_hz) {
        mOperationRateManager = std::make_unique<OperationRateManager>(this, hs_hz, ns_hz);
    }
}

ExynosPrimaryDisplayModule::~ExynosPrimaryDisplayModule ()
{

}

int32_t ExynosPrimaryDisplayModule::validateWinConfigData()
{
    return ExynosDisplay::validateWinConfigData();
}

int32_t ExynosPrimaryDisplayModule::OperationRateManager::getTargetOperationRate() {
    if (mDisplayPowerMode == HWC2_POWER_MODE_DOZE ||
        mDisplayPowerMode == HWC2_POWER_MODE_DOZE_SUSPEND) {
        return LP_OP_RATE;
    } else {
        return mDisplayTargetOperationRate;
    }
}

int32_t ExynosPrimaryDisplayModule::OperationRateManager::setTargetOperationRate(
        const int32_t rate) {
    if (mDisplayTargetOperationRate == rate) return NO_ERROR;

    OP_MANAGER_LOGI("set target operation rate %d", rate);
    mDisplayTargetOperationRate = rate;

    return NO_ERROR;
}

ExynosPrimaryDisplayModule::OperationRateManager::OperationRateManager(
        ExynosPrimaryDisplay* display, int32_t hsHz, int32_t nsHz)
      : gs201::ExynosPrimaryDisplayModule::OperationRateManager(),
        mDisplay(display),
        mDisplayHsOperationRate(hsHz),
        mDisplayNsOperationRate(nsHz),
        mDisplayPeakRefreshRate(0),
        mDisplayRefreshRate(0),
        mDisplayLastDbv(0),
        mDisplayDbv(0),
        mDisplayPowerMode(HWC2_POWER_MODE_ON),
        mDisplayLowBatteryModeEnabled(false) {
    mDisplayNsMinDbv = property_get_int32("vendor.primarydisplay.op.ns_min_dbv", 0);
    mDisplayTargetOperationRate = mDisplayHsOperationRate;
    OP_MANAGER_LOGI("Op Rate: NS=%d HS=%d NsMinDbv=%d", mDisplayNsOperationRate,
                    mDisplayHsOperationRate, mDisplayNsMinDbv);
}

ExynosPrimaryDisplayModule::OperationRateManager::~OperationRateManager() {}

int32_t ExynosPrimaryDisplayModule::OperationRateManager::onPeakRefreshRate(uint32_t rate) {
    Mutex::Autolock lock(mLock);
    char rateStr[PROP_VALUE_MAX];
    std::sprintf(rateStr, "%d", rate);

    OP_MANAGER_LOGD("rate=%d", rate);
    mDisplayPeakRefreshRate = rate;
    if (property_set("persist.vendor.primarydisplay.op.peak_refresh_rate", rateStr) < 0) {
        OP_MANAGER_LOGE("failed to set property persist.primarydisplay.op.peak_refresh_rate");
    }
    return 0;
}

int32_t ExynosPrimaryDisplayModule::OperationRateManager::onLowPowerMode(bool enabled) {
    Mutex::Autolock lock(mLock);
    OP_MANAGER_LOGD("enabled=%d", enabled);
    mDisplayLowBatteryModeEnabled = enabled;
    return 0;
}

int32_t ExynosPrimaryDisplayModule::OperationRateManager::onConfig(hwc2_config_t cfg) {
    Mutex::Autolock lock(mLock);
    mDisplayRefreshRate = mDisplay->getRefreshRate(cfg);
    OP_MANAGER_LOGD("rate=%d", mDisplayRefreshRate);
    updateOperationRateLocked(DispOpCondition::SET_CONFIG);
    return 0;
}

int32_t ExynosPrimaryDisplayModule::OperationRateManager::onBrightness(uint32_t dbv) {
    Mutex::Autolock lock(mLock);
    if (dbv == 0 || mDisplayLastDbv == dbv) return 0;
    OP_MANAGER_LOGD("dbv=%d", dbv);
    mDisplayDbv = dbv;

    /*
        Update peak_refresh_rate from persist/vendor prop after a brightness change.
        1. Otherwise there will be NS-HS-NS switch during the onPowerMode.
        2. When constructor is called, persist property is not ready yet and returns 0.
    */
    if (!mDisplayPeakRefreshRate) {
        char rateStr[PROP_VALUE_MAX];
        int32_t vendorPeakRefreshRate = 0, persistPeakRefreshRate = 0;
        if (property_get("persist.vendor.primarydisplay.op.peak_refresh_rate", rateStr, "0") >= 0 &&
            atoi(rateStr) > 0) {
            persistPeakRefreshRate = atoi(rateStr);
            mDisplayPeakRefreshRate = persistPeakRefreshRate;
        } else {
            vendorPeakRefreshRate =
                    property_get_int32("vendor.primarydisplay.op.peak_refresh_rate", 0);
            mDisplayPeakRefreshRate = vendorPeakRefreshRate;
        }

        OP_MANAGER_LOGD("peak_refresh_rate=%d[vendor: %d|persist %d]", mDisplayPeakRefreshRate,
                        vendorPeakRefreshRate, persistPeakRefreshRate);
    }

    return updateOperationRateLocked(DispOpCondition::SET_DBV);
}

int32_t ExynosPrimaryDisplayModule::OperationRateManager::onPowerMode(int32_t mode) {
    std::string modeName = "Unknown";
    if (mode == HWC2_POWER_MODE_ON) {
        modeName = "On";
    } else if (mode == HWC2_POWER_MODE_OFF) {
        modeName = "Off";
    } else if (mode == HWC2_POWER_MODE_DOZE || mode == HWC2_POWER_MODE_DOZE_SUSPEND) {
        modeName = "LP";
    }

    Mutex::Autolock lock(mLock);
    OP_MANAGER_LOGD("mode=%s", modeName.c_str());
    mDisplayPowerMode = static_cast<hwc2_power_mode_t>(mode);
    return updateOperationRateLocked(DispOpCondition::PANEL_SET_POWER);
}

int32_t ExynosPrimaryDisplayModule::OperationRateManager::updateOperationRateLocked(
        const DispOpCondition cond) {
    int32_t ret = HWC2_ERROR_NONE, dbv;

    ATRACE_CALL();
    if (cond == DispOpCondition::SET_DBV) {
        dbv = mDisplayDbv;
    } else {
        dbv = mDisplayLastDbv;
    }

    int32_t desiredOpRate = mDisplayHsOperationRate;
    int32_t curRefreshRate = mDisplay->getRefreshRate(mDisplay->mActiveConfig);
    bool isSteadyLowRefreshRate =
            (mDisplayPeakRefreshRate && mDisplayPeakRefreshRate <= mDisplayNsOperationRate) ||
            mDisplayLowBatteryModeEnabled;
    int32_t effectiveOpRate = 0;

    // check minimal operation rate needed
    if (isSteadyLowRefreshRate && curRefreshRate <= mDisplayNsOperationRate) {
        desiredOpRate = mDisplayNsOperationRate;
    }
    // check blocking zone
    if (dbv < mDisplayNsMinDbv) {
        desiredOpRate = mDisplayHsOperationRate;
    }

    if (mDisplayPowerMode == HWC2_POWER_MODE_DOZE ||
        mDisplayPowerMode == HWC2_POWER_MODE_DOZE_SUSPEND) {
        mDisplayTargetOperationRate = LP_OP_RATE;
        desiredOpRate = mDisplayTargetOperationRate;
        effectiveOpRate = desiredOpRate;
    } else if (mDisplayPowerMode != HWC2_POWER_MODE_ON) {
        return ret;
    }

    if (cond == DispOpCondition::SET_CONFIG) {
        curRefreshRate = mDisplayRefreshRate;
        if ((curRefreshRate > mDisplayNsOperationRate) &&
            (curRefreshRate <= mDisplayHsOperationRate))
            effectiveOpRate = mDisplayHsOperationRate;
    } else if (cond == DispOpCondition::PANEL_SET_POWER) {
        if (mDisplayPowerMode == HWC2_POWER_MODE_ON) {
            mDisplayTargetOperationRate = getTargetOperationRate();
        }
        effectiveOpRate = desiredOpRate;
    } else if (cond == DispOpCondition::SET_DBV) {
        // TODO: tune brightness delta for different brightness curve and values
        int32_t delta = abs(dbv - mDisplayLastDbv);
        if ((desiredOpRate == mDisplayHsOperationRate) || (delta > BRIGHTNESS_DELTA_THRESHOLD)) {
            effectiveOpRate = desiredOpRate;
        }
        mDisplayLastDbv = dbv;
        if (effectiveOpRate > LP_OP_RATE && (effectiveOpRate != mDisplayTargetOperationRate)) {
            OP_MANAGER_LOGD("brightness delta=%d", delta);
        } else {
            return ret;
        }
    }

    if (!mDisplay->isConfigSettingEnabled() && effectiveOpRate == mDisplayNsOperationRate) {
        OP_MANAGER_LOGI("rate switching is disabled, skip NS op rate update");
        return ret;
    } else if (effectiveOpRate > LP_OP_RATE) {
        ret = setTargetOperationRate(effectiveOpRate);
    }

    OP_MANAGER_LOGI("Target@%d(desired:%d) | Refresh@%d(peak:%d), Battery:%s, DBV:%d(NsMin:%d)",
                    mDisplayTargetOperationRate, desiredOpRate, curRefreshRate,
                    mDisplayPeakRefreshRate, mDisplayLowBatteryModeEnabled ? "Low" : "OK",
                    mDisplayLastDbv, mDisplayNsMinDbv);
    return ret;
}

void ExynosPrimaryDisplayModule::checkPreblendingRequirement() {
    if (!hasDisplayColor()) {
        DISPLAY_LOGD(eDebugTDM, "%s is skipped because of no displaycolor", __func__);
        return;
    }

    String8 log;
    int count = 0;

    auto checkPreblending = [&](const int idx, ExynosMPPSource* mppSrc) -> int {
        auto& dpp = getDppForLayer(mppSrc);
        mppSrc->mNeedPreblending =
                dpp.EotfLut().enable | dpp.Gm().enable | dpp.Dtm().enable | dpp.OetfLut().enable;
        if (hwcCheckDebugMessages(eDebugTDM)) {
            log.appendFormat(" i=%d,pb(%d-%d,%d,%d,%d)", idx, mppSrc->mNeedPreblending,
                             dpp.EotfLut().enable, dpp.Gm().enable, dpp.Dtm().enable,
                             dpp.OetfLut().enable);
        }
        return mppSrc->mNeedPreblending;
    };

    // for client target
    count += checkPreblending(-1, &mClientCompositionInfo);

    // for normal layers
    for (size_t i = 0; i < mLayers.size(); ++i) {
        count += checkPreblending(i, mLayers[i]);
    }
    DISPLAY_LOGD(eDebugTDM, "disp(%d),cnt=%d%s", mDisplayId, count, log.c_str());
}