• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef HISTOGRAM_MEDIATOR_H_
18 #define HISTOGRAM_MEDIATOR_H_
19 
20 #include <aidl/android/hardware/graphics/common/Rect.h>
21 #include <aidl/com/google/hardware/pixel/display/BnDisplay.h>
22 #include <aidl/com/google/hardware/pixel/display/HistogramErrorCode.h>
23 #include <aidl/com/google/hardware/pixel/display/HistogramPos.h>
24 #include <aidl/com/google/hardware/pixel/display/Priority.h>
25 #include <aidl/com/google/hardware/pixel/display/Weight.h>
26 #include <xf86drm.h>
27 #include <xf86drmMode.h>
28 
29 #include <condition_variable>
30 #include <mutex>
31 #include <vector>
32 
33 #define HWC2_INCLUDE_STRINGIFICATION
34 #define HWC2_USE_CPP11
35 #include <hardware/hwcomposer2.h>
36 #undef HWC2_INCLUDE_STRINGIFICATION
37 #undef HWC2_USE_CPP11
38 #include "ExynosDevice.h"
39 #include "ExynosDisplay.h"
40 #include "ExynosDisplayDrmInterfaceModule.h"
41 #include "ExynosDisplayInterface.h"
42 #include "ExynosLayer.h"
43 
44 namespace histogram {
45 using RoiRect = ::aidl::android::hardware::graphics::common::Rect;
46 using Weight = ::aidl::com::google::hardware::pixel::display::Weight;
47 using HistogramPos = ::aidl::com::google::hardware::pixel::display::HistogramPos;
48 using Priority = ::aidl::com::google::hardware::pixel::display::Priority;
49 using HistogramErrorCode = ::aidl::com::google::hardware::pixel::display::HistogramErrorCode;
50 
51 constexpr size_t HISTOGRAM_BINS_SIZE = 256;
52 constexpr size_t WEIGHT_SUM = 1024;
53 
54 class HistogramMediator {
55 public:
56     HistogramMediator() = default;
57     HistogramMediator(ExynosDisplay *display);
~HistogramMediator()58     ~HistogramMediator() {}
59 
60     HistogramErrorCode requestHist();
61     HistogramErrorCode cancelHistRequest();
62     HistogramErrorCode collectRoiLuma(std::vector<char16_t> *buf);
63     HistogramErrorCode setRoiWeightThreshold(const RoiRect &roi, const Weight &weight,
64                                              const HistogramPos &pos);
65     RoiRect calRoi(const RoiRect &roi);
66     struct HistogramReceiver : public IDLHistogram {
HistogramReceiverHistogramReceiver67         HistogramReceiver() : mHistData(){};
68         void callbackHistogram(char16_t *bin) override;
69         uint16_t mHistData[HISTOGRAM_BINS_SIZE]; // luma buffer
70         std::condition_variable mHistData_cv;    // for pullback data sync ctrl
71         bool mHistReq_pending = false;
72         std::mutex mDataCollectingMutex; // for data collecting operations
73     };
74 
75     struct HistogramConfig {
76         RoiRect mRoi;
77         Weight mWeights;
78         HistogramPos mPos;
79 
HistogramConfigHistogramConfig80         HistogramConfig() {}
81 
HistogramConfigHistogramConfig82         HistogramConfig(const RoiRect &roi, const Weight &weights, const HistogramPos &pos) {
83             mRoi = roi;
84             mWeights = weights;
85             mPos = pos;
86         }
87 
88         bool operator!=(const HistogramConfig &rhs) {
89             return mRoi != rhs.mRoi || mWeights != rhs.mWeights || mPos != rhs.mPos;
90         }
91 
92         HistogramConfig &operator=(const HistogramConfig &rhs) {
93             mRoi = rhs.mRoi;
94             mWeights = rhs.mWeights;
95             mPos = rhs.mPos;
96             return *this;
97         }
98     };
99 
100     uint32_t getFrameCount();
setSampleFrameCounter(int32_t id)101     void setSampleFrameCounter(int32_t id) { mSampledFrameCounter = id; }
getSampleFrameCounter()102     uint32_t getSampleFrameCounter() { return mSampledFrameCounter; }
histRequested()103     bool histRequested() { return mIDLHistogram->mHistReq_pending; }
104     std::mutex mConfigMutex;
105     HistogramConfig mConfig GUARDED_BY(mConfigMutex);
106 
107 private:
108     int calculateThreshold(const RoiRect &roi);
109     std::shared_ptr<HistogramReceiver> mIDLHistogram;
110     ExynosDisplay *mDisplay = nullptr;
111     uint32_t mSampledFrameCounter = 0;
112 };
113 
114 } // namespace histogram
115 
116 #endif // HISTOGRAM_MEDIATOR_H_
117