1 /*
2 * Copyright (c) 2017 The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *    * Redistributions of source code must retain the above copyright
8 *      notice, this list of conditions and the following disclaimer.
9 *    * Redistributions in binary form must reproduce the above
10 *      copyright notice, this list of conditions and the following
11 *      disclaimer in the documentation and/or other materials provided
12 *      with the distribution.
13 *    * Neither the name of The Linux Foundation. nor the names of its
14 *      contributors may be used to endorse or promote products derived
15 *      from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #include <vendor/display/config/1.0/IDisplayConfig.h>
31 
32 #include "DisplayConfig.h"
33 
34 namespace display {
35 
36 using vendor::display::config::V1_0::IDisplayConfig;
37 
38 //=============================================================================
39 // The functions below run in the client process and wherever necessary
40 // do a binder call to HWC to get/set data.
41 
MapDisplayType(int dpy)42 IDisplayConfig::DisplayType MapDisplayType(int dpy) {
43     switch (dpy) {
44         case DISPLAY_PRIMARY:
45             return IDisplayConfig::DisplayType::DISPLAY_PRIMARY;
46 
47         case DISPLAY_EXTERNAL:
48             return IDisplayConfig::DisplayType::DISPLAY_EXTERNAL;
49 
50         case DISPLAY_VIRTUAL:
51             return IDisplayConfig::DisplayType::DISPLAY_VIRTUAL;
52 
53         default:
54             break;
55     }
56 
57     return IDisplayConfig::DisplayType::INVALID;
58 }
59 
MapExternalStatus(uint32_t status)60 IDisplayConfig::DisplayExternalStatus MapExternalStatus(uint32_t status) {
61     switch (status) {
62         case EXTERNAL_OFFLINE:
63             return IDisplayConfig::DisplayExternalStatus::EXTERNAL_OFFLINE;
64 
65         case EXTERNAL_ONLINE:
66             return IDisplayConfig::DisplayExternalStatus::EXTERNAL_ONLINE;
67 
68         case EXTERNAL_PAUSE:
69             return IDisplayConfig::DisplayExternalStatus::EXTERNAL_PAUSE;
70 
71         case EXTERNAL_RESUME:
72             return IDisplayConfig::DisplayExternalStatus::EXTERNAL_RESUME;
73 
74         default:
75             break;
76     }
77 
78     return IDisplayConfig::DisplayExternalStatus::INVALID;
79 }
80 
MapDynRefreshRateOp(uint32_t op)81 IDisplayConfig::DisplayDynRefreshRateOp MapDynRefreshRateOp(uint32_t op) {
82     switch (op) {
83         case DISABLE_METADATA_DYN_REFRESH_RATE:
84             return IDisplayConfig::DisplayDynRefreshRateOp::DISABLE_METADATA_DYN_REFRESH_RATE;
85 
86         case ENABLE_METADATA_DYN_REFRESH_RATE:
87             return IDisplayConfig::DisplayDynRefreshRateOp::ENABLE_METADATA_DYN_REFRESH_RATE;
88 
89         case SET_BINDER_DYN_REFRESH_RATE:
90             return IDisplayConfig::DisplayDynRefreshRateOp::SET_BINDER_DYN_REFRESH_RATE;
91 
92         default:
93             break;
94     }
95 
96     return IDisplayConfig::DisplayDynRefreshRateOp::INVALID;
97 }
98 
MapDisplayPortType(IDisplayConfig::DisplayPortType panelType)99 int MapDisplayPortType(IDisplayConfig::DisplayPortType panelType) {
100     switch (panelType) {
101         case IDisplayConfig::DisplayPortType::DISPLAY_PORT_DEFAULT:
102             return DISPLAY_PORT_DEFAULT;
103 
104         case IDisplayConfig::DisplayPortType::DISPLAY_PORT_DSI:
105             return DISPLAY_PORT_DSI;
106 
107         case IDisplayConfig::DisplayPortType::DISPLAY_PORT_DTV:
108             return DISPLAY_PORT_DTV;
109 
110         case IDisplayConfig::DisplayPortType::DISPLAY_PORT_WRITEBACK:
111             return DISPLAY_PORT_WRITEBACK;
112 
113         case IDisplayConfig::DisplayPortType::DISPLAY_PORT_LVDS:
114             return DISPLAY_PORT_LVDS;
115 
116         case IDisplayConfig::DisplayPortType::DISPLAY_PORT_EDP:
117             return DISPLAY_PORT_EDP;
118 
119         case IDisplayConfig::DisplayPortType::DISPLAY_PORT_DP:
120             return DISPLAY_PORT_DP;
121 
122         default:
123             break;
124     }
125 
126     return -1;
127 }
128 
isExternalConnected()129 int isExternalConnected() {
130     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
131     if (intf == NULL) {
132         return 0;
133     }
134 
135     int connected = 0;
136     intf->isDisplayConnected(IDisplayConfig::DisplayType::DISPLAY_EXTERNAL,
137         [&](const auto &tmpError, const auto &tmpStatus) {
138             if (tmpError) {
139                 return;
140             }
141 
142             connected = tmpStatus;
143         });
144 
145     return connected;
146 }
147 
setSecondayDisplayStatus(int dpy,uint32_t status)148 int setSecondayDisplayStatus(int dpy, uint32_t status) {
149     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
150     if (intf == NULL) {
151         return -1;
152     }
153 
154     return intf->setSecondayDisplayStatus(MapDisplayType(dpy), MapExternalStatus(status));
155 }
156 
configureDynRefeshRate(uint32_t op,uint32_t refreshRate)157 int configureDynRefeshRate(uint32_t op, uint32_t refreshRate) {
158     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
159     if (intf == NULL) {
160         return -1;
161     }
162 
163     return intf->configureDynRefeshRate(MapDynRefreshRateOp(op), refreshRate);
164 }
165 
getConfigCount(int dpy)166 int getConfigCount(int dpy) {
167     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
168     if (intf == NULL) {
169         return -1;
170     }
171 
172     int count = 0;
173     intf->getConfigCount(MapDisplayType(dpy),
174         [&](const auto &tmpError, const auto &tmpCount) {
175             if (tmpError) {
176                 return;
177             }
178 
179             count = tmpCount;
180         });
181 
182     return count;
183 }
184 
getActiveConfig(int dpy)185 int getActiveConfig(int dpy) {
186     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
187     if (intf == NULL) {
188         return -1;
189     }
190 
191     int config = 0;
192     intf->getActiveConfig(MapDisplayType(dpy),
193         [&](const auto &tmpError, const auto &tmpConfig) {
194             if (tmpError) {
195                 return;
196             }
197 
198             config = tmpConfig;
199         });
200 
201     return config;
202 }
203 
setActiveConfig(int dpy,uint32_t config)204 int setActiveConfig(int dpy, uint32_t config) {
205     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
206     if (intf == NULL) {
207         return -1;
208     }
209 
210     return intf->setActiveConfig(MapDisplayType(dpy), config);
211 }
212 
getDisplayAttributes(uint32_t configIndex,int dpy)213 DisplayAttributes getDisplayAttributes(uint32_t configIndex, int dpy) {
214     DisplayAttributes attributes;
215 
216     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
217     if (intf == NULL) {
218         return attributes;
219     }
220 
221     intf->getDisplayAttributes(configIndex, MapDisplayType(dpy),
222         [&](const auto &tmpError, const auto &tmpAttributes) {
223             if (tmpError) {
224                 return;
225             }
226 
227             attributes.vsync_period = tmpAttributes.vsyncPeriod;
228             attributes.xres = tmpAttributes.xRes;
229             attributes.yres = tmpAttributes.yRes;
230             attributes.xdpi = tmpAttributes.xDpi;
231             attributes.ydpi = tmpAttributes.yDpi;
232             attributes.panel_type = MapDisplayPortType(tmpAttributes.panelType);
233             attributes.is_yuv = tmpAttributes.isYuv;
234         });
235 
236     return attributes;
237 }
238 
setPanelBrightness(uint32_t level)239 int setPanelBrightness(uint32_t level) {
240     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
241     if (intf == NULL) {
242         return -1;
243     }
244 
245     return intf->setPanelBrightness(level);
246 }
247 
getPanelBrightness()248 uint32_t getPanelBrightness() {
249     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
250     if (intf == NULL) {
251         return 0;
252     }
253 
254     int level = 0;
255     intf->getPanelBrightness(
256         [&](const auto &tmpError, const auto &tmpLevel) {
257             if (tmpError) {
258                 return;
259             }
260 
261             level = tmpLevel;
262         });
263 
264     return level;
265 }
266 
minHdcpEncryptionLevelChanged(int dpy,uint32_t min_enc_level)267 int minHdcpEncryptionLevelChanged(int dpy, uint32_t min_enc_level) {
268     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
269     if (intf == NULL) {
270         return -1;
271     }
272 
273     return intf->minHdcpEncryptionLevelChanged(MapDisplayType(dpy), min_enc_level);
274 }
275 
refreshScreen()276 int refreshScreen() {
277     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
278     if (intf == NULL) {
279         return -1;
280     }
281 
282     return intf->refreshScreen();
283 }
284 
controlPartialUpdate(int dpy,bool enable)285 int controlPartialUpdate(int dpy, bool enable) {
286     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
287     if (intf == NULL) {
288         return -1;
289     }
290 
291     return intf->controlPartialUpdate(MapDisplayType(dpy), enable);
292 }
293 
toggleScreenUpdate(uint32_t on)294 int toggleScreenUpdate(uint32_t on) {
295     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
296     if (intf == NULL) {
297         return -1;
298     }
299 
300     return intf->toggleScreenUpdate(on == 1);
301 }
302 
setIdleTimeout(uint32_t value)303 int setIdleTimeout(uint32_t value) {
304     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
305     if (intf == NULL) {
306         return -1;
307     }
308 
309     return intf->setIdleTimeout(value);
310 }
311 
getHDRCapabilities(int dpy,DisplayHDRCapabilities * caps)312 int getHDRCapabilities(int dpy, DisplayHDRCapabilities *caps) {
313     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
314     if (intf == NULL || caps == NULL) {
315         return -1;
316     }
317 
318     int error = -1;
319     intf->getHDRCapabilities(MapDisplayType(dpy),
320         [&](const auto &tmpError, const auto &tmpCaps) {
321             error = tmpError;
322             if (error) {
323                 return;
324             }
325 
326             caps->supported_hdr_types = tmpCaps.supportedHdrTypes;
327             caps->max_luminance = tmpCaps.maxLuminance;
328             caps->max_avg_luminance = tmpCaps.maxAvgLuminance;
329             caps->min_luminance = tmpCaps.minLuminance;
330         });
331 
332     return error;
333 }
334 
setCameraLaunchStatus(uint32_t on)335 int setCameraLaunchStatus(uint32_t on) {
336     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
337     if (intf == NULL) {
338         return -1;
339     }
340 
341     return intf->setCameraLaunchStatus(on);
342 }
343 
displayBWTransactionPending()344 bool displayBWTransactionPending() {
345     android::sp<IDisplayConfig> intf = IDisplayConfig::getService();
346     if (intf == NULL) {
347         return 0;
348     }
349 
350     int status = 0;
351     intf->displayBWTransactionPending(
352         [&](const auto &tmpError, const auto &tmpStatus) {
353             if (tmpError) {
354                 return;
355             }
356 
357             status = tmpStatus;
358         });
359 
360     return status;
361 }
362 
363 } // namespace display
364