1 /*
2 * Copyright (C) 2022 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 #include "histogram_mediator.h"
17
HistogramMediator(ExynosDisplay * display)18 histogram::HistogramMediator::HistogramMediator(ExynosDisplay *display) {
19 mDisplay = display;
20 ExynosDisplayDrmInterface *moduleDisplayInterface =
21 static_cast<ExynosDisplayDrmInterface *>(display->mDisplayInterface.get());
22 mIDLHistogram = std::make_shared<HistogramReceiver>();
23
24 moduleDisplayInterface->registerHistogramInfo(mIDLHistogram);
25 }
getFrameCount()26 uint32_t histogram::HistogramMediator::getFrameCount() {
27 ExynosDisplayDrmInterface *moduleDisplayInterface =
28 static_cast<ExynosDisplayDrmInterface *>(mDisplay->mDisplayInterface.get());
29 return moduleDisplayInterface->getFrameCount();
30 }
31
requestHist()32 histogram::HistogramErrorCode histogram::HistogramMediator::requestHist() {
33 if (mDisplay->isSecureContentPresenting()) {
34 return histogram::HistogramErrorCode::DRM_PLAYING;
35 }
36 ExynosDisplayDrmInterface *moduleDisplayInterface =
37 static_cast<ExynosDisplayDrmInterface *>(mDisplay->mDisplayInterface.get());
38
39 {
40 std::unique_lock<std::mutex> lk(mIDLHistogram->mDataCollectingMutex);
41 if (moduleDisplayInterface->setHistogramControl(
42 hidl_histogram_control_t::HISTOGRAM_CONTROL_REQUEST) != NO_ERROR) {
43 return histogram::HistogramErrorCode::ENABLE_HIST_ERROR;
44 }
45 mIDLHistogram->mHistReq_pending = true;
46 }
47 return histogram::HistogramErrorCode::NONE;
48 }
49
cancelHistRequest()50 histogram::HistogramErrorCode histogram::HistogramMediator::cancelHistRequest() {
51 ExynosDisplayDrmInterface *moduleDisplayInterface =
52 static_cast<ExynosDisplayDrmInterface *>(mDisplay->mDisplayInterface.get());
53
54 if (moduleDisplayInterface->setHistogramControl(
55 hidl_histogram_control_t::HISTOGRAM_CONTROL_CANCEL) != NO_ERROR) {
56 return histogram::HistogramErrorCode::DISABLE_HIST_ERROR;
57 }
58 return histogram::HistogramErrorCode::NONE;
59 }
60
callbackHistogram(char16_t * bin)61 void histogram::HistogramMediator::HistogramReceiver::callbackHistogram(char16_t *bin) {
62 std::unique_lock<std::mutex> lk(mDataCollectingMutex);
63 if (mHistReq_pending == true) {
64 std::memcpy(mHistData, bin, HISTOGRAM_BINS_SIZE * sizeof(char16_t));
65 mHistReq_pending = false;
66 }
67 mHistData_cv.notify_all();
68 }
69
calculateThreshold(const RoiRect & roi)70 int histogram::HistogramMediator::calculateThreshold(const RoiRect &roi) {
71 int threshold = ((roi.bottom - roi.top) * (roi.right - roi.left)) >> 16;
72 return threshold + 1;
73 }
74
setRoiWeightThreshold(const RoiRect & roi,const Weight & weight,const HistogramPos & pos)75 histogram::HistogramErrorCode histogram::HistogramMediator::setRoiWeightThreshold(
76 const RoiRect &roi, const Weight &weight, const HistogramPos &pos) {
77 int threshold = calculateThreshold(roi);
78 mIDLHistogram->setHistogramROI((uint16_t)roi.left, (uint16_t)roi.top,
79 (uint16_t)(roi.right - roi.left),
80 (uint16_t)(roi.bottom - roi.top));
81 mIDLHistogram->setHistogramWeights(weight.weightR, weight.weightG, weight.weightB);
82 mIDLHistogram->setHistogramThreshold(threshold);
83 mIDLHistogram->setHistogramPos(pos);
84
85 return histogram::HistogramErrorCode::NONE;
86 }
87
collectRoiLuma(std::vector<char16_t> * buf)88 histogram::HistogramErrorCode histogram::HistogramMediator::collectRoiLuma(
89 std::vector<char16_t> *buf) {
90 std::unique_lock<std::mutex> lk(mIDLHistogram->mDataCollectingMutex);
91
92 mIDLHistogram->mHistData_cv.wait_for(lk, std::chrono::milliseconds(50), [this]() {
93 return (!mDisplay->isPowerModeOff() && !mIDLHistogram->mHistReq_pending);
94 });
95 if (mIDLHistogram->mHistReq_pending == false) setSampleFrameCounter(getFrameCount());
96 buf->assign(mIDLHistogram->mHistData, mIDLHistogram->mHistData + HISTOGRAM_BINS_SIZE);
97
98 return histogram::HistogramErrorCode::NONE;
99 }
100
calRoi(const RoiRect & roi)101 histogram::RoiRect histogram::HistogramMediator::calRoi(const RoiRect &roi) {
102 RoiRect roi_return = {-1, -1, -1, -1};
103 ExynosDisplayDrmInterface *moduleDisplayInterface =
104 static_cast<ExynosDisplayDrmInterface *>(mDisplay->mDisplayInterface.get());
105 roi_return.left = roi.left * moduleDisplayInterface->getActiveModeHDisplay() /
106 moduleDisplayInterface->getPanelFullResolutionHSize();
107 roi_return.top = roi.top * moduleDisplayInterface->getActiveModeVDisplay() /
108 moduleDisplayInterface->getPanelFullResolutionVSize();
109 roi_return.right = roi.right * moduleDisplayInterface->getActiveModeHDisplay() /
110 moduleDisplayInterface->getPanelFullResolutionHSize();
111 roi_return.bottom = roi.bottom * moduleDisplayInterface->getActiveModeVDisplay() /
112 moduleDisplayInterface->getPanelFullResolutionVSize();
113 return roi_return;
114 }
115