1 /*
2  * Copyright (C) Texas Instruments Incorporated - http://www.ti.com/
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 <sstream>
18 
19 #include <cutils/trace.h>
20 #include <fcntl.h>
21 #include <poll.h>
22 #include <sys/ioctl.h>
23 #include <sys/resource.h>
24 
25 #include <log/log.h>
26 #include <cutils/properties.h>
27 #define HWC_REMOVE_DEPRECATED_VERSIONS 1
28 #include <hardware/hardware.h>
29 #include <hardware/hwcomposer.h>
30 #include <hardware_legacy/uevent.h>
31 
32 #include <kms++/kms++.h>
33 
34 #include "display.h"
35 #include "format.h"
36 #include "hwc_dev.h"
37 
38 #define LCD_DISPLAY_DEFAULT_HRES 1920
39 #define LCD_DISPLAY_DEFAULT_VRES 1080
40 #define LCD_DISPLAY_DEFAULT_FPS 60
41 
42 #define LCD_DISPLAY_DEFAULT_DPI 120
43 #define HDMI_DISPLAY_DEFAULT_DPI 75
44 
45 #define HWC_PRIORITY_LOW_DISPLAY (19)
46 
47 #ifndef ARRAY_SIZE
48 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
49 #endif
50 
51 #define WIDTH(rect) ((rect).right - (rect).left)
52 #define HEIGHT(rect) ((rect).bottom - (rect).top)
53 
is_valid_display(omap_hwc_device_t * hwc_dev,int disp)54 static bool is_valid_display(omap_hwc_device_t* hwc_dev, int disp)
55 {
56     if (disp < 0 || disp >= MAX_DISPLAYS || !hwc_dev->displays[disp])
57         return false;
58 
59     return true;
60 }
61 
62 struct drm_connector_type {
63     int type;
64     char name[64];
65 };
66 
67 #define CONNECTOR_TYPE_STR(type) { DRM_MODE_CONNECTOR_ ## type, #type }
68 
69 static const struct drm_connector_type connector_type_list[] = {
70     CONNECTOR_TYPE_STR(Unknown),
71     CONNECTOR_TYPE_STR(VGA),
72     CONNECTOR_TYPE_STR(DVII),
73     CONNECTOR_TYPE_STR(DVID),
74     CONNECTOR_TYPE_STR(DVIA),
75     CONNECTOR_TYPE_STR(Composite),
76     CONNECTOR_TYPE_STR(SVIDEO),
77     CONNECTOR_TYPE_STR(LVDS),
78     CONNECTOR_TYPE_STR(Component),
79     CONNECTOR_TYPE_STR(9PinDIN),
80     CONNECTOR_TYPE_STR(DisplayPort),
81     CONNECTOR_TYPE_STR(HDMIA),
82     CONNECTOR_TYPE_STR(HDMIB),
83     CONNECTOR_TYPE_STR(TV),
84     CONNECTOR_TYPE_STR(eDP),
85     CONNECTOR_TYPE_STR(VIRTUAL),
86     CONNECTOR_TYPE_STR(DSI),
87     CONNECTOR_TYPE_STR(DPI),
88 };
89 
90 /* The connectors for primary and external displays can be controlled
91  * by the below system properties
92  *   - ro.hwc.primary.conn
93  *   - ro.hwc.external.conn
94  * If these are not set (default), `DRM_CONNECTOR_TYPE_Unknown` will be
95  * considered as primary display connector, and `DRM_CONNECTOR_TYPE_HDMIA`
96  * will be considered as secondary display.
97  *
98  * The values are <string> constants, with acceptable values being as defined
99  * by the DRM interface `DRM_CONNECTOR_TYPE_<string>`.
100  *
101  * If nothing is set, external is `HDMIA` connector.
102  */
display_get_connector_type(int disp)103 static int display_get_connector_type(int disp)
104 {
105     char prop_val[PROPERTY_VALUE_MAX];
106 
107     if (disp == HWC_DISPLAY_PRIMARY)
108         property_get("ro.hwc.primary.conn", prop_val, "");
109     else
110         property_get("ro.hwc.external.conn", prop_val, "HDMIA");
111 
112     for (size_t i = 0; i < ARRAY_SIZE(connector_type_list); i++) {
113         if (!strncasecmp(prop_val, connector_type_list[i].name, 64))
114             return connector_type_list[i].type;
115     }
116 
117     return -1;
118 }
119 
get_connectors(omap_hwc_device_t * hwc_dev)120 static void get_connectors(omap_hwc_device_t* hwc_dev)
121 {
122     int primary_connector_type = display_get_connector_type(HWC_DISPLAY_PRIMARY);
123     int external_connector_type = display_get_connector_type(HWC_DISPLAY_EXTERNAL);
124 
125     // Find primary connector
126     for (auto connector : hwc_dev->card->get_connectors()) {
127         if (primary_connector_type != -1) {
128             if (connector->connector_type() == (uint32_t)primary_connector_type) {
129                 hwc_dev->primaryConector = connector;
130                 break;
131             }
132         } else {
133             /* If connector type is not specified use the first
134              * connector that is not our HWC_DISPLAY_EXTERNAL connector
135              */
136             if (connector->connector_type() != (uint32_t)external_connector_type) {
137                 hwc_dev->primaryConector = connector;
138                 break;
139             }
140         }
141     }
142 
143     // Find external connector
144     for (auto connector : hwc_dev->card->get_connectors()) {
145         if (connector->connector_type() == (uint32_t)external_connector_type) {
146             hwc_dev->externalConector = connector;
147             break;
148         }
149     }
150 }
151 
init_primary_display(omap_hwc_device_t * hwc_dev)152 static int init_primary_display(omap_hwc_device_t* hwc_dev)
153 {
154     if (hwc_dev->displays[HWC_DISPLAY_PRIMARY]) {
155         ALOGE("Display %d is already connected", HWC_DISPLAY_PRIMARY);
156         return -EBUSY;
157     }
158 
159     kms::Connector* connector = hwc_dev->primaryConector;
160 
161     HWCDisplay* display = new HWCDisplay(DISP_ROLE_PRIMARY);
162     hwc_dev->displays[HWC_DISPLAY_PRIMARY] = display;
163 
164     if (!connector) {
165         ALOGW("No connector found for primary display");
166         ALOGW("Using dummy primary display");
167 
168         display->is_dummy = true;
169 
170         display_config_t config;
171         config.xres = LCD_DISPLAY_DEFAULT_HRES;
172         config.yres = LCD_DISPLAY_DEFAULT_VRES;
173         config.fps = LCD_DISPLAY_DEFAULT_FPS;
174         config.xdpi = LCD_DISPLAY_DEFAULT_DPI;
175         config.ydpi = LCD_DISPLAY_DEFAULT_DPI;
176         display->configs.push_back(config);
177 
178         return 0;
179     }
180 
181     /* Always use default mode for now */
182     kms::Videomode mode = connector->get_default_mode();
183 
184     display_config_t config;
185     config.xres = mode.hdisplay;
186     config.yres = mode.vdisplay;
187     config.fps = mode.vrefresh;
188     config.xdpi = LCD_DISPLAY_DEFAULT_DPI;
189     config.ydpi = LCD_DISPLAY_DEFAULT_DPI;
190     display->configs.push_back(config);
191 
192     display->disp_link.card = hwc_dev->card;
193     display->disp_link.con = connector;
194     display->disp_link.crtc = connector->get_current_crtc();
195     // FIXME: user resource manager
196     if (!display->disp_link.crtc)
197         display->disp_link.crtc = connector->get_possible_crtcs()[0];
198     display->disp_link.mode = mode;
199 
200     display->setup_composition_pipes();
201 
202     return 0;
203 }
204 
add_external_hdmi_display(omap_hwc_device_t * hwc_dev)205 static int add_external_hdmi_display(omap_hwc_device_t* hwc_dev)
206 {
207     if (hwc_dev->displays[HWC_DISPLAY_EXTERNAL]) {
208         ALOGE("Display %d is already connected", HWC_DISPLAY_EXTERNAL);
209         return 0;
210     }
211 
212     kms::Connector* connector = hwc_dev->externalConector;
213 
214     if (!connector) {
215         ALOGE("No connector for external display");
216         return -1;
217     }
218 
219     /* wait until EDID read finishes */
220     do {
221         connector->refresh();
222     } while (connector->get_modes().size() == 0);
223 
224     // FIXME: Allow selecting other modes until HWC 1.4 support is added
225     kms::Videomode mode = connector->get_default_mode();
226 
227     HWCDisplay* display = new HWCDisplay(DISP_ROLE_SECONDARY);
228     hwc_dev->displays[HWC_DISPLAY_EXTERNAL] = display;
229 
230     display_config_t config;
231     config.xres = mode.hdisplay;
232     config.yres = mode.vdisplay;
233     config.fps = mode.vrefresh;
234     config.xdpi = HDMI_DISPLAY_DEFAULT_DPI;
235     config.ydpi = HDMI_DISPLAY_DEFAULT_DPI;
236     display->configs.push_back(config);
237 
238     display->disp_link.card = hwc_dev->card;
239     display->disp_link.con = connector;
240     display->disp_link.crtc = connector->get_current_crtc();
241     // FIXME: user resource manager
242     if (!display->disp_link.crtc)
243         display->disp_link.crtc = connector->get_possible_crtcs()[0];
244     display->disp_link.mode = mode;
245 
246     display->setup_composition_pipes();
247 
248     return 0;
249 }
250 
remove_external_hdmi_display(omap_hwc_device_t * hwc_dev)251 static void remove_external_hdmi_display(omap_hwc_device_t* hwc_dev)
252 {
253     HWCDisplay* display = hwc_dev->displays[HWC_DISPLAY_EXTERNAL];
254     if (!display) {
255         ALOGW("Failed to remove non-existent display %d", HWC_DISPLAY_EXTERNAL);
256         return;
257     }
258 
259     delete hwc_dev->displays[HWC_DISPLAY_EXTERNAL];
260     hwc_dev->displays[HWC_DISPLAY_EXTERNAL] = NULL;
261 }
262 
handle_hotplug(omap_hwc_device_t * hwc_dev,bool state)263 static void handle_hotplug(omap_hwc_device_t* hwc_dev, bool state)
264 {
265     if (state) {
266         int err = add_external_hdmi_display(hwc_dev);
267         if (err) {
268             remove_external_hdmi_display(hwc_dev);
269             return;
270         }
271         ALOGI("Added external display");
272     } else {
273         remove_external_hdmi_display(hwc_dev);
274         ALOGI("Removed external display");
275     }
276 }
277 
find_hdmi_connector_status(omap_hwc_device_t * hwc_dev)278 static int find_hdmi_connector_status(omap_hwc_device_t* hwc_dev)
279 {
280     auto connector = hwc_dev->externalConector;
281     if (!connector)
282         return false;
283 
284     bool old_state = connector->connected();
285     connector->refresh();
286     bool cur_state = connector->connected();
287 
288     if (cur_state != old_state)
289         ALOGI("%s event for connector %u\n",
290               cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug",
291               connector->id());
292 
293     return cur_state == DRM_MODE_CONNECTED;
294 }
295 
check_hotplug_status(omap_hwc_device_t * hwc_dev,bool old_state)296 static bool check_hotplug_status(omap_hwc_device_t* hwc_dev, bool old_state)
297 {
298     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
299 
300     bool state = find_hdmi_connector_status(hwc_dev);
301     if (state != old_state)
302         handle_hotplug(hwc_dev, state);
303 
304     lock.unlock();
305 
306     if (hwc_dev->cb_procs) {
307         if (hwc_dev->cb_procs->hotplug)
308             hwc_dev->cb_procs->hotplug(hwc_dev->cb_procs, HWC_DISPLAY_EXTERNAL, state);
309         if (hwc_dev->cb_procs->invalidate)
310             hwc_dev->cb_procs->invalidate(hwc_dev->cb_procs);
311     }
312 
313     return state;
314 }
315 
hwc_hdmi_thread(void * data)316 static void hwc_hdmi_thread(void* data)
317 {
318     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)data;
319 
320     setpriority(PRIO_PROCESS, 0, HWC_PRIORITY_LOW_DISPLAY);
321 
322     uevent_init();
323 
324     struct pollfd pfds[1] = {
325         {
326             pfds[0].fd = uevent_get_fd(),
327             pfds[0].events = POLLIN,
328             pfds[0].revents = 0,
329         }
330     };
331 
332     const char* hdmi_uevent_path = "change@/devices/platform/omapdrm.0/drm/card0";
333     static char uevent_desc[4096];
334     memset(uevent_desc, 0, sizeof(uevent_desc));
335 
336     /* Check outside of uevent loop in-case already plugged in */
337     bool state = check_hotplug_status(hwc_dev, false);
338 
339     while (true) {
340         int err = poll(pfds, ARRAY_SIZE(pfds), -1);
341         if (err < 0) {
342             ALOGE("received hdmi_thread poll() error event %d", err);
343             break;
344         }
345 
346         if (pfds[0].revents & POLLIN) {
347             /* keep last 2 zeroes to ensure double 0 termination */
348             uevent_next_event(uevent_desc, sizeof(uevent_desc) - 2);
349             if (strlen(hdmi_uevent_path) <= 0 || strcmp(uevent_desc, hdmi_uevent_path))
350                 continue; /* event not for us */
351 
352             state = check_hotplug_status(hwc_dev, state);
353         }
354     }
355 
356     ALOGE("HDMI polling thread exiting\n");
357 }
358 
359 /*
360  * DRM event polling thread
361  *   We poll for DRM events in this thread. DRM events can be vblank and/or
362  *   page-flip events both occurring on Vsync.
363  */
hwc_drm_event_thread(void * data)364 static void hwc_drm_event_thread(void* data)
365 {
366     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)data;
367 
368     setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
369 
370     struct pollfd pfds = {
371         pfds.fd = hwc_dev->card->fd(),
372         pfds.events = POLLIN,
373         pfds.revents = 0,
374     };
375 
376     drmEventContext evctx = {
377         evctx.version = 2,
378         evctx.vblank_handler = HWCDisplay::vblank_handler,
379         evctx.page_flip_handler = HWCDisplay::page_flip_handler,
380     };
381 
382     while (true) {
383         int ret = poll(&pfds, 1, 60000);
384         if (ret < 0) {
385             ALOGE("Event poll error %d", errno);
386             break;
387         } else if (ret == 0) {
388             ALOGI("Event poll timeout");
389             continue;
390         }
391         if (pfds.revents & POLLIN)
392             drmHandleEvent(pfds.fd, &evctx);
393     }
394 
395     ALOGE("DRM event polling thread exiting\n");
396 }
397 
adjust_drm_plane_to_layer(hwc_layer_1_t * layer,drm_plane_props_t * plane)398 static void adjust_drm_plane_to_layer(hwc_layer_1_t* layer, drm_plane_props_t* plane)
399 {
400     if (!layer || !plane) {
401         ALOGE("Bad layer or plane");
402         return;
403     }
404 
405     /* display position */
406     plane->crtc_x = layer->displayFrame.left;
407     plane->crtc_y = layer->displayFrame.top;
408     plane->crtc_w = WIDTH(layer->displayFrame);
409     plane->crtc_h = HEIGHT(layer->displayFrame);
410 
411     /* crop */
412     plane->src_x = layer->sourceCrop.left;
413     plane->src_y = layer->sourceCrop.top;
414     plane->src_w = WIDTH(layer->sourceCrop);
415     plane->src_h = HEIGHT(layer->sourceCrop);
416 
417     plane->layer = layer;
418 }
419 
hwc_prepare_for_display(omap_hwc_device_t * hwc_dev,int disp,hwc_display_contents_1_t * content)420 static int hwc_prepare_for_display(omap_hwc_device_t* hwc_dev, int disp, hwc_display_contents_1_t* content)
421 {
422     if (!is_valid_display(hwc_dev, disp))
423         return -ENODEV;
424 
425     HWCDisplay* display = hwc_dev->displays[disp];
426 
427     if (display->is_dummy) {
428         for (size_t i = 0; i < content->numHwLayers - 1; i++) {
429             hwc_layer_1_t* layer = &content->hwLayers[i];
430             layer->compositionType = HWC_OVERLAY;
431         }
432         return 0;
433     }
434 
435     /* Set the FB_TARGET layer */
436     adjust_drm_plane_to_layer(&content->hwLayers[content->numHwLayers - 1], &display->planeProps[disp]);
437 
438     return 0;
439 }
440 
hwc_prepare(struct hwc_composer_device_1 * dev,size_t numDisplays,hwc_display_contents_1_t ** displays)441 static int hwc_prepare(struct hwc_composer_device_1* dev, size_t numDisplays, hwc_display_contents_1_t** displays)
442 {
443     atrace_begin(ATRACE_TAG_HAL, "am57xx_hwc_prepare");
444     if (!numDisplays || !displays)
445         return 0;
446 
447     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
448     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
449 
450     int err = 0;
451 
452     for (size_t i = 0; i < numDisplays; i++) {
453         hwc_display_contents_1_t* contents = displays[i];
454 
455         if (!contents)
456             continue;
457 
458         if (contents->numHwLayers == 0) {
459             ALOGW("Prepare given no content for display %zu", i);
460             continue;
461         }
462 
463         int disp_err = hwc_prepare_for_display(hwc_dev, i, contents);
464         if (!err && disp_err)
465             err = disp_err;
466     }
467 
468     atrace_end(ATRACE_TAG_HAL);
469     return err;
470 }
471 
hwc_set_for_display(omap_hwc_device_t * hwc_dev,int disp,hwc_display_contents_1_t * content)472 static int hwc_set_for_display(omap_hwc_device_t* hwc_dev, int disp, hwc_display_contents_1_t* content)
473 {
474     if (!is_valid_display(hwc_dev, disp))
475         return -ENODEV;
476 
477     HWCDisplay* display = hwc_dev->displays[disp];
478     drm_plane_props_t* planeProp = &display->planeProps[disp];
479 
480     int err = 0;
481 
482     /*
483      * clear release and retire fence fd's here in case we do
484      * not set them in update_display()
485      */
486     for (size_t i = 0; i < content->numHwLayers; i++) {
487         hwc_layer_1_t* layer = &content->hwLayers[i];
488         layer->releaseFenceFd = -1;
489     }
490     content->retireFenceFd = -1;
491 
492     if (!display->is_dummy) {
493         err = display->update_display(planeProp);
494         if (err)
495             ALOGE("Failed to update display %d\n", disp);
496     }
497 
498     /* clear any remaining acquire fences */
499     for (size_t i = 0; i < content->numHwLayers; i++) {
500         hwc_layer_1_t* layer = &content->hwLayers[i];
501         if (layer->acquireFenceFd >= 0) {
502             close(layer->acquireFenceFd);
503             layer->acquireFenceFd = -1;
504         }
505     }
506 
507     return err;
508 }
509 
hwc_set(struct hwc_composer_device_1 * dev,size_t numDisplays,hwc_display_contents_1_t ** displays)510 static int hwc_set(struct hwc_composer_device_1* dev, size_t numDisplays, hwc_display_contents_1_t** displays)
511 {
512     atrace_begin(ATRACE_TAG_HAL, "am57xx_hwc_set");
513     if (!numDisplays || !displays)
514         return 0;
515 
516     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
517     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
518 
519     int err = 0;
520 
521     for (size_t i = 0; i < numDisplays; i++) {
522         hwc_display_contents_1_t* contents = displays[i];
523 
524         if (!contents)
525             continue;
526 
527         if (contents->numHwLayers == 0) {
528             ALOGE("Set given no content for display %zu", i);
529             continue;
530         }
531 
532         int disp_err = hwc_set_for_display(hwc_dev, i, contents);
533         if (!err && disp_err)
534             err = disp_err;
535     }
536 
537     atrace_end(ATRACE_TAG_HAL);
538     return err;
539 }
540 
hwc_eventControl(struct hwc_composer_device_1 * dev,int disp,int event,int enabled)541 static int hwc_eventControl(struct hwc_composer_device_1* dev, int disp, int event, int enabled)
542 {
543     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
544     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
545 
546     if (!is_valid_display(hwc_dev, disp))
547         return -EINVAL;
548 
549     switch (event) {
550     case HWC_EVENT_VSYNC:
551         // FIXME: This is a hack
552         hwc_dev->displays[disp]->cb_procs = hwc_dev->cb_procs;
553 
554         ALOGD("%s vsync for display %d", enabled ? "Enabling" : "Disabling", disp);
555         return hwc_dev->displays[disp]->set_vsync_state(enabled);
556 
557     default:
558         return -EINVAL;
559     }
560 
561     return 0;
562 }
563 
hwc_blank(struct hwc_composer_device_1 * dev,int disp,int blank)564 static int hwc_blank(struct hwc_composer_device_1* dev, int disp, int blank)
565 {
566     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
567     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
568 
569     ALOGD("%s display %d", blank ? "Blanking" : "Unblanking", disp);
570 
571     if (!is_valid_display(hwc_dev, disp))
572         return -EINVAL;
573 
574     hwc_dev->displays[disp]->blank(blank);
575 
576     return 0;
577 }
578 
hwc_query(struct hwc_composer_device_1 * dev,int what,int * value)579 static int hwc_query(struct hwc_composer_device_1* dev, int what, int* value)
580 {
581     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
582     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
583 
584     switch (what) {
585     case HWC_BACKGROUND_LAYER_SUPPORTED:
586         // we don't support the background layer yet
587         value[0] = 0;
588         break;
589     case HWC_VSYNC_PERIOD:
590         ALOGW("Query for deprecated vsync value, returning 60Hz");
591         *value = 1000 * 1000 * 1000 / 60;
592         break;
593     case HWC_DISPLAY_TYPES_SUPPORTED:
594         *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT;
595         break;
596     default:
597         // unsupported query
598         return -EINVAL;
599     }
600 
601     return 0;
602 }
603 
hwc_registerProcs(struct hwc_composer_device_1 * dev,hwc_procs_t const * procs)604 static void hwc_registerProcs(struct hwc_composer_device_1* dev, hwc_procs_t const* procs)
605 {
606     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
607     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
608 
609     hwc_dev->cb_procs = (typeof(hwc_dev->cb_procs))procs;
610 
611     /* now that cb_procs->hotplug is valid */
612     try {
613         hwc_dev->hdmi_thread = new std::thread(hwc_hdmi_thread, hwc_dev);
614     } catch (...) {
615         ALOGE("Failed to create HDMI listening thread (%s)", strerror(errno));
616     }
617 }
618 
hwc_getDisplayConfigs(struct hwc_composer_device_1 * dev,int disp,uint32_t * configs,size_t * numConfigs)619 static int hwc_getDisplayConfigs(struct hwc_composer_device_1* dev, int disp, uint32_t* configs, size_t* numConfigs)
620 {
621     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
622     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
623 
624     if (!is_valid_display(hwc_dev, disp))
625         return -EINVAL;
626 
627     HWCDisplay* display = hwc_dev->displays[disp];
628 
629     return display->get_display_configs(configs, numConfigs);
630 }
631 
hwc_getDisplayAttributes(struct hwc_composer_device_1 * dev,int disp,uint32_t config,const uint32_t * attributes,int32_t * values)632 static int hwc_getDisplayAttributes(struct hwc_composer_device_1* dev, int disp, uint32_t config, const uint32_t* attributes, int32_t* values)
633 {
634     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
635     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
636 
637     if (!is_valid_display(hwc_dev, disp))
638         return -EINVAL;
639 
640     HWCDisplay* display = hwc_dev->displays[disp];
641 
642     return display->get_display_attributes(config, attributes, values);
643 }
644 
hwc_device_close(hw_device_t * device)645 static int hwc_device_close(hw_device_t* device)
646 {
647     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)device;
648 
649     if (hwc_dev) {
650         if (hwc_dev->event_thread)
651             delete hwc_dev->event_thread;
652 
653         for (size_t i = 0; i < MAX_DISPLAYS; i++)
654             delete hwc_dev->displays[i];
655 
656         delete hwc_dev;
657     }
658 
659     return 0;
660 }
661 
hwc_device_open(const hw_module_t * module,const char * name,hw_device_t ** device)662 static int hwc_device_open(const hw_module_t* module, const char* name, hw_device_t** device)
663 {
664     if (strcmp(name, HWC_HARDWARE_COMPOSER))
665         return -EINVAL;
666 
667     omap_hwc_device_t* hwc_dev = new omap_hwc_device_t;
668     memset(hwc_dev, 0, sizeof(*hwc_dev));
669 
670     /* Open DRM device */
671     hwc_dev->card = new kms::Card();
672 
673     /* Find primary and external connectors */
674     get_connectors(hwc_dev);
675 
676     int ret = init_primary_display(hwc_dev);
677     if (ret) {
678         ALOGE("Could not initialize primary display");
679         return -1;
680     }
681 
682     hwc_dev->event_thread = new std::thread(hwc_drm_event_thread, hwc_dev);
683 
684     hwc_dev->device.common.tag = HARDWARE_DEVICE_TAG;
685     hwc_dev->device.common.version = HWC_DEVICE_API_VERSION_1_1;
686     hwc_dev->device.common.module = (hw_module_t*)module;
687     hwc_dev->device.common.close = hwc_device_close;
688     hwc_dev->device.prepare = hwc_prepare;
689     hwc_dev->device.set = hwc_set;
690     hwc_dev->device.eventControl = hwc_eventControl;
691     hwc_dev->device.blank = hwc_blank;
692     hwc_dev->device.query = hwc_query;
693     hwc_dev->device.registerProcs = hwc_registerProcs;
694     hwc_dev->device.getDisplayConfigs = hwc_getDisplayConfigs;
695     hwc_dev->device.getDisplayAttributes = hwc_getDisplayAttributes;
696 
697     *device = &hwc_dev->device.common;
698 
699     return 0;
700 }
701 
702 static struct hw_module_methods_t module_methods = {
703     .open = hwc_device_open,
704 };
705 
706 omap_hwc_module_t HAL_MODULE_INFO_SYM = {
707     .base = {
708         .common = {
709             .tag = HARDWARE_MODULE_TAG,
710             .module_api_version = HWC_MODULE_API_VERSION_0_1,
711             .hal_api_version = HARDWARE_HAL_API_VERSION,
712             .id = HWC_HARDWARE_MODULE_ID,
713             .name = "AM57xx Hardware Composer HAL",
714             .author = "Texas Instruments",
715             .methods = &module_methods,
716         },
717     },
718 };
719