1 /*
2  * Copyright (C) 2021 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 #include "pixel-display.h"
18 
19 #include <aidlcommonsupport/NativeHandle.h>
20 #include <android-base/logging.h>
21 #include <android/binder_manager.h>
22 #include <android/binder_process.h>
23 #include <sys/types.h>
24 #include <utils/Errors.h>
25 
26 #include "ExynosDisplay.h"
27 
28 extern int32_t load_png_image(const char *filepath, buffer_handle_t buffer);
29 
30 using ::aidl::com::google::hardware::pixel::display::Display;
31 
PixelDisplayInit(ExynosDevice * device)32 void PixelDisplayInit(ExynosDevice *device) {
33     ABinderProcess_setThreadPoolMaxThreadCount(0);
34 
35     std::shared_ptr<Display> display = ndk::SharedRefBase::make<Display>(device);
36     LOG(INFO) << "pixel display service start...";
37     const std::string instance = std::string() + Display::descriptor + "/default";
38     binder_status_t status =
39             AServiceManager_addService(display->asBinder().get(), instance.c_str());
40     CHECK(status == STATUS_OK);
41 
42     ABinderProcess_startThreadPool();
43 }
44 
readCompensationImage(const aidl::android::hardware::common::NativeHandle & handle,const std::string & imageName)45 int32_t readCompensationImage(const aidl::android::hardware::common::NativeHandle &handle,
46                               const std::string &imageName) {
47     ALOGI("setCompensationImageHandle, imageName = %s", imageName.c_str());
48 
49     std::string shadowCompensationImage("/mnt/vendor/persist/display/");
50     shadowCompensationImage.append(imageName);
51 
52     native_handle_t *clone = makeFromAidl(handle);
53 
54     return load_png_image(shadowCompensationImage.c_str(), static_cast<buffer_handle_t>(clone));
55 }
56 
57 namespace aidl {
58 namespace com {
59 namespace google {
60 namespace hardware {
61 namespace pixel {
62 namespace display {
63 
64 // ----------------------------------------------------------------------------
65 
isHbmSupported(bool * _aidl_return)66 ndk::ScopedAStatus Display::isHbmSupported(bool *_aidl_return) {
67     *_aidl_return = false;
68     return ndk::ScopedAStatus::ok();
69 }
70 
setHbmState(HbmState state)71 ndk::ScopedAStatus Display::setHbmState(HbmState state) {
72     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
73 }
74 
getHbmState(HbmState * _aidl_return)75 ndk::ScopedAStatus Display::getHbmState(HbmState *_aidl_return) {
76     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
77 }
78 
isLbeSupported(bool * _aidl_return)79 ndk::ScopedAStatus Display::isLbeSupported(bool *_aidl_return) {
80     if (mDevice) {
81         *_aidl_return = mDevice->isLbeSupported();
82         return ndk::ScopedAStatus::ok();
83     }
84     *_aidl_return = false;
85     return ndk::ScopedAStatus::ok();
86 }
87 
setLbeState(LbeState state)88 ndk::ScopedAStatus Display::setLbeState(LbeState state) {
89     if (mDevice) {
90         mDevice->setLbeState(state);
91         return ndk::ScopedAStatus::ok();
92     }
93     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
94 }
95 
setLbeAmbientLight(int ambientLux)96 ndk::ScopedAStatus Display::setLbeAmbientLight(int ambientLux) {
97     if (mDevice) {
98         mDevice->setLbeAmbientLight(ambientLux);
99         return ndk::ScopedAStatus::ok();
100     }
101     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
102 }
103 
getLbeState(LbeState * _aidl_return)104 ndk::ScopedAStatus Display::getLbeState(LbeState *_aidl_return) {
105     if (mDevice) {
106         *_aidl_return = mDevice->getLbeState();
107         return ndk::ScopedAStatus::ok();
108     }
109     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
110 }
111 
isLhbmSupported(bool * _aidl_return)112 ndk::ScopedAStatus Display::isLhbmSupported(bool *_aidl_return) {
113     if (mDevice) {
114         *_aidl_return = mDevice->isLhbmSupported();
115         return ndk::ScopedAStatus::ok();
116     }
117     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
118 }
119 
setLhbmState(bool enabled)120 ndk::ScopedAStatus Display::setLhbmState(bool enabled) {
121     if (mDevice && mDevice->isLhbmSupported()) {
122         int32_t ret = mDevice->setLhbmState(enabled);
123         if (!ret)
124             return ndk::ScopedAStatus::ok();
125         else if (ret == TIMED_OUT)
126             return ndk::ScopedAStatus::fromExceptionCode(STATUS_TIMED_OUT);
127     }
128     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
129 }
130 
getLhbmState(bool * _aidl_return)131 ndk::ScopedAStatus Display::getLhbmState(bool *_aidl_return) {
132     if (mDevice && mDevice->isLhbmSupported()) {
133         *_aidl_return = mDevice->getLhbmState();
134         return ndk::ScopedAStatus::ok();
135     }
136     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
137 }
138 
setCompensationImageHandle(const NativeHandle & native_handle,const std::string & imageName,int * _aidl_return)139 ndk::ScopedAStatus Display::setCompensationImageHandle(const NativeHandle &native_handle,
140                                                        const std::string &imageName,
141                                                        int *_aidl_return) {
142     if (mDevice && mDevice->isColorCalibratedByDevice()) {
143         *_aidl_return = readCompensationImage(native_handle, imageName);
144     } else {
145         *_aidl_return = -1;
146     }
147     return ndk::ScopedAStatus::ok();
148 }
149 
setMinIdleRefreshRate(int fps,int * _aidl_return)150 ndk::ScopedAStatus Display::setMinIdleRefreshRate(int fps, int *_aidl_return) {
151     if (mDevice) {
152         *_aidl_return = mDevice->setMinIdleRefreshRate(fps);
153         return ndk::ScopedAStatus::ok();
154     }
155     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
156 }
157 
setRefreshRateThrottle(int delayMs,int * _aidl_return)158 ndk::ScopedAStatus Display::setRefreshRateThrottle(int delayMs, int *_aidl_return) {
159     if (mDevice) {
160         *_aidl_return = mDevice->setRefreshRateThrottle(delayMs);
161         return ndk::ScopedAStatus::ok();
162     }
163     return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
164 }
165 } // namespace display
166 } // namespace pixel
167 } // namespace hardware
168 } // namespace google
169 } // namespace com
170 } // namespace aidl
171