summaryrefslogtreecommitdiff
path: root/libhwc2.1/pixel-display.cpp
blob: 5edd1942ec1a1cbd0bb04d861a3b119a6bf3e87c (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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * 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.
 */

#include "pixel-display.h"

#include <aidlcommonsupport/NativeHandle.h>
#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
#include <sys/types.h>
#include <utils/Errors.h>

#include "ExynosDisplay.h"
#include "ExynosPrimaryDisplay.h"
#include "HistogramController.h"

extern int32_t load_png_image(const char *filepath, buffer_handle_t buffer);

using ::aidl::com::google::hardware::pixel::display::Display;

void PixelDisplayInit(ExynosDisplay *exynos_display, const std::string_view instance_str) {
    ABinderProcess_setThreadPoolMaxThreadCount(0);

    std::shared_ptr<Display> display = ndk::SharedRefBase::make<Display>(exynos_display);
    const std::string instance = std::string() + Display::descriptor + "/" + std::string(instance_str).c_str();
    binder_status_t status =
            AServiceManager_addService(display->asBinder().get(), instance.c_str());
    LOG(INFO) << instance.c_str() << " service start...";
    CHECK(status == STATUS_OK);

    ABinderProcess_startThreadPool();
}

int32_t readCompensationImage(const aidl::android::hardware::common::NativeHandle &handle,
                              const std::string &imageName) {
    ALOGI("setCompensationImageHandle, imageName = %s", imageName.c_str());

    std::string shadowCompensationImage("/mnt/vendor/persist/display/");
    shadowCompensationImage.append(imageName);

    native_handle_t *clone = makeFromAidl(handle);

    return load_png_image(shadowCompensationImage.c_str(), static_cast<buffer_handle_t>(clone));
}

namespace aidl {
namespace com {
namespace google {
namespace hardware {
namespace pixel {
namespace display {

// ----------------------------------------------------------------------------

ndk::ScopedAStatus Display::isHbmSupported(bool *_aidl_return) {
    *_aidl_return = false;
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Display::setHbmState(HbmState state) {
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::getHbmState(HbmState *_aidl_return) {
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::isLbeSupported(bool *_aidl_return) {
    if (mDisplay) {
        *_aidl_return = mDisplay->isLbeSupported();
        return ndk::ScopedAStatus::ok();
    }
    *_aidl_return = false;
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Display::setLbeState(LbeState state) {
    if (mDisplay) {
        mDisplay->setLbeState(state);
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::setLbeAmbientLight(int ambientLux) {
    if (mDisplay) {
        mDisplay->setLbeAmbientLight(ambientLux);
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::getLbeState(LbeState *_aidl_return) {
    if (mDisplay) {
        *_aidl_return = mDisplay->getLbeState();
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::isLhbmSupported(bool *_aidl_return) {
    if (mDisplay) {
        *_aidl_return = mDisplay->isLhbmSupported();
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::setLhbmState(bool enabled) {
    if (mDisplay && mDisplay->isLhbmSupported()) {
        int32_t ret = mDisplay->setLhbmState(enabled);
        if (!ret)
            return ndk::ScopedAStatus::ok();
        else if (ret == TIMED_OUT)
            return ndk::ScopedAStatus::fromExceptionCode(STATUS_TIMED_OUT);
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::getLhbmState(bool *_aidl_return) {
    if (mDisplay && mDisplay->isLhbmSupported()) {
        *_aidl_return = mDisplay->getLhbmState();
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::setPeakRefreshRate(int rate) {
    if (mDisplay && mDisplay->mOperationRateManager) {
        mDisplay->mOperationRateManager->onPeakRefreshRate(rate);
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::setLowPowerMode(bool enabled) {
    if (mDisplay && mDisplay->mOperationRateManager) {
        mDisplay->mOperationRateManager->onLowPowerMode(enabled);
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::isOperationRateSupported(bool *_aidl_return) {
    if (mDisplay) {
        *_aidl_return = mDisplay->isOperationRateSupported();
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::setCompensationImageHandle(const NativeHandle &native_handle,
                                                       const std::string &imageName,
                                                       int *_aidl_return) {
    if (mDisplay && mDisplay->isColorCalibratedByDevice()) {
        *_aidl_return = readCompensationImage(native_handle, imageName);
    } else {
        *_aidl_return = -1;
    }
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Display::setMinIdleRefreshRate(int fps, int *_aidl_return) {
    if (mDisplay) {
        *_aidl_return = mDisplay->setMinIdleRefreshRate(fps, RrThrottleRequester::PIXEL_DISP);
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::setRefreshRateThrottle(int delayMs, int *_aidl_return) {
    if (mDisplay) {
        if (delayMs < 0) {
            *_aidl_return = BAD_VALUE;
            ALOGW("%s fail: delayMs(%d) is less than 0", __func__, delayMs);
            return ndk::ScopedAStatus::ok();
        }

        *_aidl_return =
                mDisplay->setRefreshRateThrottleNanos(std::chrono::duration_cast<
                                                              std::chrono::nanoseconds>(
                                                              std::chrono::milliseconds(delayMs))
                                                              .count(),
                                                      RrThrottleRequester::PIXEL_DISP);
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

bool Display::runMediator(const RoiRect &roi, const Weight &weight, const HistogramPos &pos,
                            std::vector<char16_t> *histogrambuffer) {
    bool isConfigChanged;
    histogram::HistogramMediator::HistogramConfig pendingConfig(roi, weight, pos);

    {
        std::scoped_lock lock(mMediator.mConfigMutex);
        isConfigChanged = mMediator.mConfig != pendingConfig;

        if (isConfigChanged) {
            if (mMediator.setRoiWeightThreshold(roi, weight, pos) != HistogramErrorCode::NONE) {
                ALOGE("histogram error, SET_ROI_WEIGHT_THRESHOLD ERROR\n");
                return false;
            }
            mMediator.mConfig = pendingConfig;
        }
    }

    if (!mMediator.histRequested() &&
        mMediator.requestHist() == HistogramErrorCode::ENABLE_HIST_ERROR) {
        ALOGE("histogram error, ENABLE_HIST ERROR\n");
    }

    /*
     * DPU driver maintains always-on histogram engine state with up to date histogram data.
     * Therefore we don't have explicitly to trigger onRefresh in case histogram configuration
     * does not change.
     */
    if (isConfigChanged) {
        mDisplay->mDevice->onRefresh(mDisplay->mDisplayId);
    }

    if (mMediator.collectRoiLuma(histogrambuffer) != HistogramErrorCode::NONE) {
        ALOGE("histogram error, COLLECT_ROI_LUMA ERROR\n");
        return false;
    }
    return true;
}

ndk::ScopedAStatus Display::histogramSample(const RoiRect &roi, const Weight &weight,
                                            HistogramPos pos, Priority pri,
                                            std::vector<char16_t> *histogrambuffer,
                                            HistogramErrorCode *_aidl_return) {
    if (!mDisplay) {
        ALOGI("mDisplay is NULL \n");
        return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
    }
    if (histogrambuffer == nullptr) {
        ALOGE("histogrambuffer is null");
        *_aidl_return = HistogramErrorCode::BAD_HIST_DATA;
        return ndk::ScopedAStatus::ok();
    }
    if (mDisplay->isPowerModeOff() == true) {
        *_aidl_return = HistogramErrorCode::DISPLAY_POWEROFF; // panel is off
        return ndk::ScopedAStatus::ok();
    }
    if (mDisplay->isSecureContentPresenting() == true) {
        *_aidl_return = HistogramErrorCode::DRM_PLAYING; // panel is playing DRM content
        return ndk::ScopedAStatus::ok();
    }
    if ((roi.left < 0) || (roi.top < 0) || ((roi.right - roi.left) <= 0) ||
        ((roi.bottom - roi.top) <= 0)) {
        *_aidl_return = HistogramErrorCode::BAD_ROI;
        ALOGE("histogram error, BAD_ROI (%d, %d, %d, %d) \n", roi.left, roi.top, roi.right,
              roi.bottom);
        return ndk::ScopedAStatus::ok();
    }
    if ((weight.weightR + weight.weightG + weight.weightB) != (histogram::WEIGHT_SUM)) {
        *_aidl_return = HistogramErrorCode::BAD_WEIGHT;
        ALOGE("histogram error, BAD_WEIGHT(%d, %d, %d)\n", weight.weightR, weight.weightG,
              weight.weightB);
        return ndk::ScopedAStatus::ok();
    }
    if (pos != HistogramPos::POST && pos != HistogramPos::PRE) {
        *_aidl_return = HistogramErrorCode::BAD_POSITION;
        ALOGE("histogram error, BAD_POSITION(%d)\n", (int)pos);
        return ndk::ScopedAStatus::ok();
    }
    if (pri != Priority::NORMAL && pri != Priority::PRIORITY) {
        *_aidl_return = HistogramErrorCode::BAD_PRIORITY;
        ALOGE("histogram error, BAD_PRIORITY(%d)\n", (int)pri);
        return ndk::ScopedAStatus::ok();
    }
    RoiRect roiCaled = mMediator.calRoi(roi); // fit roi coordinates to RRS
    runMediator(roiCaled, weight, pos, histogrambuffer);
    if (mDisplay->isSecureContentPresenting() == true) {
        /* clear data to avoid leakage */
        std::fill(histogrambuffer->begin(), histogrambuffer->end(), 0);
        histogrambuffer->clear();
        *_aidl_return = HistogramErrorCode::DRM_PLAYING; // panel is playing DRM content
        return ndk::ScopedAStatus::ok();
    }

    *_aidl_return = HistogramErrorCode::NONE;
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Display::getPanelCalibrationStatus(PanelCalibrationStatus *_aidl_return){
    if (mDisplay) {
        *_aidl_return = mDisplay->getPanelCalibrationStatus();
        return ndk::ScopedAStatus::ok();
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::isDbmSupported(bool *_aidl_return) {
    if (!mDisplay) {
        return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
    }
    *_aidl_return = mDisplay->isDbmSupported();
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Display::setDbmState(bool enabled) {
    if (!mDisplay) {
        return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
    }
    mDisplay->setDbmState(enabled);
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus Display::getHistogramCapability(HistogramCapability *_aidl_return) {
    if (mDisplay && mDisplay->mHistogramController) {
        return mDisplay->mHistogramController->getHistogramCapability(_aidl_return);
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::registerHistogram(const ndk::SpAIBinder &token,
                                              const HistogramConfig &histogramConfig,
                                              HistogramErrorCode *_aidl_return) {
    if (mDisplay && mDisplay->mHistogramController) {
        return mDisplay->mHistogramController->registerHistogram(token, histogramConfig,
                                                                 _aidl_return);
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::queryHistogram(const ndk::SpAIBinder &token,
                                           std::vector<char16_t> *histogramBuffer,
                                           HistogramErrorCode *_aidl_return) {
    if (mDisplay && mDisplay->mHistogramController) {
        return mDisplay->mHistogramController->queryHistogram(token, histogramBuffer, _aidl_return);
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::reconfigHistogram(const ndk::SpAIBinder &token,
                                              const HistogramConfig &histogramConfig,
                                              HistogramErrorCode *_aidl_return) {
    if (mDisplay && mDisplay->mHistogramController) {
        return mDisplay->mHistogramController->reconfigHistogram(token, histogramConfig,
                                                                 _aidl_return);
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

ndk::ScopedAStatus Display::unregisterHistogram(const ndk::SpAIBinder &token,
                                                HistogramErrorCode *_aidl_return) {
    if (mDisplay && mDisplay->mHistogramController) {
        return mDisplay->mHistogramController->unregisterHistogram(token, _aidl_return);
    }
    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

} // namespace display
} // namespace pixel
} // namespace hardware
} // namespace google
} // namespace com
} // namespace aidl