• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,int zorder,drm_plane_props_t * plane)398 static void adjust_drm_plane_to_layer(hwc_layer_1_t* layer, int zorder, 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->zorder = zorder;
418     plane->layer = layer;
419 
420     if (layer->blending == HWC_BLENDING_PREMULT)
421         plane->pre_mult_alpha = 1;
422     else
423         plane->pre_mult_alpha = 0;
424 }
425 
hwc_prepare_for_display(omap_hwc_device_t * hwc_dev,int disp,hwc_display_contents_1_t * content)426 static int hwc_prepare_for_display(omap_hwc_device_t* hwc_dev, int disp, hwc_display_contents_1_t* content)
427 {
428     if (!is_valid_display(hwc_dev, disp))
429         return -ENODEV;
430 
431     HWCDisplay* display = hwc_dev->displays[disp];
432 
433     if (display->is_dummy) {
434         for (size_t i = 0; i < content->numHwLayers - 1; i++) {
435             hwc_layer_1_t* layer = &content->hwLayers[i];
436             layer->compositionType = HWC_OVERLAY;
437         }
438         return 0;
439     }
440 
441     /* Set the FB_TARGET layer */
442     adjust_drm_plane_to_layer(&content->hwLayers[content->numHwLayers - 1], 0, &display->planeProps[disp]);
443 
444     return 0;
445 }
446 
hwc_prepare(struct hwc_composer_device_1 * dev,size_t numDisplays,hwc_display_contents_1_t ** displays)447 static int hwc_prepare(struct hwc_composer_device_1* dev, size_t numDisplays, hwc_display_contents_1_t** displays)
448 {
449     atrace_begin(ATRACE_TAG_HAL, "am57xx_hwc_prepare");
450     if (!numDisplays || !displays)
451         return 0;
452 
453     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
454     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
455 
456     int err = 0;
457 
458     for (size_t i = 0; i < numDisplays; i++) {
459         hwc_display_contents_1_t* contents = displays[i];
460 
461         if (!contents)
462             continue;
463 
464         if (contents->numHwLayers == 0) {
465             ALOGW("Prepare given no content for display %zu", i);
466             continue;
467         }
468 
469         int disp_err = hwc_prepare_for_display(hwc_dev, i, contents);
470         if (!err && disp_err)
471             err = disp_err;
472     }
473 
474     atrace_end(ATRACE_TAG_HAL);
475     return err;
476 }
477 
hwc_set_for_display(omap_hwc_device_t * hwc_dev,int disp,hwc_display_contents_1_t * content)478 static int hwc_set_for_display(omap_hwc_device_t* hwc_dev, int disp, hwc_display_contents_1_t* content)
479 {
480     if (!is_valid_display(hwc_dev, disp))
481         return -ENODEV;
482 
483     HWCDisplay* display = hwc_dev->displays[disp];
484     drm_plane_props_t* planeProp = &display->planeProps[disp];
485 
486     int err = 0;
487 
488     /*
489      * clear release and retire fence fd's here in case we do
490      * not set them in update_display()
491      */
492     for (size_t i = 0; i < content->numHwLayers; i++) {
493         hwc_layer_1_t* layer = &content->hwLayers[i];
494         layer->releaseFenceFd = -1;
495     }
496     content->retireFenceFd = -1;
497 
498     if (!display->is_dummy) {
499         err = display->update_display(planeProp);
500         if (err)
501             ALOGE("Failed to update display %d\n", disp);
502     }
503 
504     /* clear any remaining acquire fences */
505     for (size_t i = 0; i < content->numHwLayers; i++) {
506         hwc_layer_1_t* layer = &content->hwLayers[i];
507         if (layer->acquireFenceFd >= 0) {
508             close(layer->acquireFenceFd);
509             layer->acquireFenceFd = -1;
510         }
511     }
512 
513     return err;
514 }
515 
hwc_set(struct hwc_composer_device_1 * dev,size_t numDisplays,hwc_display_contents_1_t ** displays)516 static int hwc_set(struct hwc_composer_device_1* dev, size_t numDisplays, hwc_display_contents_1_t** displays)
517 {
518     atrace_begin(ATRACE_TAG_HAL, "am57xx_hwc_set");
519     if (!numDisplays || !displays)
520         return 0;
521 
522     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
523     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
524 
525     int err = 0;
526 
527     for (size_t i = 0; i < numDisplays; i++) {
528         hwc_display_contents_1_t* contents = displays[i];
529 
530         if (!contents)
531             continue;
532 
533         if (contents->numHwLayers == 0) {
534             ALOGE("Set given no content for display %zu", i);
535             continue;
536         }
537 
538         int disp_err = hwc_set_for_display(hwc_dev, i, contents);
539         if (!err && disp_err)
540             err = disp_err;
541     }
542 
543     atrace_end(ATRACE_TAG_HAL);
544     return err;
545 }
546 
hwc_eventControl(struct hwc_composer_device_1 * dev,int disp,int event,int enabled)547 static int hwc_eventControl(struct hwc_composer_device_1* dev, int disp, int event, int enabled)
548 {
549     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
550     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
551 
552     if (!is_valid_display(hwc_dev, disp))
553         return -EINVAL;
554 
555     switch (event) {
556     case HWC_EVENT_VSYNC:
557         // FIXME: This is a hack
558         hwc_dev->displays[disp]->cb_procs = hwc_dev->cb_procs;
559 
560         ALOGD("%s vsync for display %d", enabled ? "Enabling" : "Disabling", disp);
561         return hwc_dev->displays[disp]->set_vsync_state(enabled);
562 
563     default:
564         return -EINVAL;
565     }
566 
567     return 0;
568 }
569 
hwc_blank(struct hwc_composer_device_1 * dev,int disp,int blank)570 static int hwc_blank(struct hwc_composer_device_1* dev, int disp, int blank)
571 {
572     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
573     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
574 
575     ALOGD("%s display %d", blank ? "Blanking" : "Unblanking", disp);
576 
577     if (!is_valid_display(hwc_dev, disp))
578         return -EINVAL;
579 
580     hwc_dev->displays[disp]->blank(blank);
581 
582     return 0;
583 }
584 
hwc_query(struct hwc_composer_device_1 * dev,int what,int * value)585 static int hwc_query(struct hwc_composer_device_1* dev, int what, int* value)
586 {
587     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
588     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
589 
590     switch (what) {
591     case HWC_BACKGROUND_LAYER_SUPPORTED:
592         // we don't support the background layer yet
593         value[0] = 0;
594         break;
595     case HWC_VSYNC_PERIOD:
596         ALOGW("Query for deprecated vsync value, returning 60Hz");
597         *value = 1000 * 1000 * 1000 / 60;
598         break;
599     case HWC_DISPLAY_TYPES_SUPPORTED:
600         *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT;
601         break;
602     default:
603         // unsupported query
604         return -EINVAL;
605     }
606 
607     return 0;
608 }
609 
hwc_registerProcs(struct hwc_composer_device_1 * dev,hwc_procs_t const * procs)610 static void hwc_registerProcs(struct hwc_composer_device_1* dev, hwc_procs_t const* procs)
611 {
612     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
613     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
614 
615     hwc_dev->cb_procs = (typeof(hwc_dev->cb_procs))procs;
616 
617     /* now that cb_procs->hotplug is valid */
618     try {
619         hwc_dev->hdmi_thread = new std::thread(hwc_hdmi_thread, hwc_dev);
620     } catch (...) {
621         ALOGE("Failed to create HDMI listening thread (%s)", strerror(errno));
622     }
623 }
624 
hwc_getDisplayConfigs(struct hwc_composer_device_1 * dev,int disp,uint32_t * configs,size_t * numConfigs)625 static int hwc_getDisplayConfigs(struct hwc_composer_device_1* dev, int disp, uint32_t* configs, size_t* numConfigs)
626 {
627     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
628     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
629 
630     if (!is_valid_display(hwc_dev, disp))
631         return -EINVAL;
632 
633     HWCDisplay* display = hwc_dev->displays[disp];
634 
635     return display->get_display_configs(configs, numConfigs);
636 }
637 
hwc_getDisplayAttributes(struct hwc_composer_device_1 * dev,int disp,uint32_t config,const uint32_t * attributes,int32_t * values)638 static int hwc_getDisplayAttributes(struct hwc_composer_device_1* dev, int disp, uint32_t config, const uint32_t* attributes, int32_t* values)
639 {
640     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)dev;
641     std::unique_lock<std::mutex> lock(hwc_dev->mutex);
642 
643     if (!is_valid_display(hwc_dev, disp))
644         return -EINVAL;
645 
646     HWCDisplay* display = hwc_dev->displays[disp];
647 
648     return display->get_display_attributes(config, attributes, values);
649 }
650 
hwc_device_close(hw_device_t * device)651 static int hwc_device_close(hw_device_t* device)
652 {
653     omap_hwc_device_t* hwc_dev = (omap_hwc_device_t*)device;
654 
655     if (hwc_dev) {
656         if (hwc_dev->event_thread)
657             delete hwc_dev->event_thread;
658 
659         for (size_t i = 0; i < MAX_DISPLAYS; i++)
660             delete hwc_dev->displays[i];
661 
662         delete hwc_dev;
663     }
664 
665     return 0;
666 }
667 
hwc_device_open(const hw_module_t * module,const char * name,hw_device_t ** device)668 static int hwc_device_open(const hw_module_t* module, const char* name, hw_device_t** device)
669 {
670     if (strcmp(name, HWC_HARDWARE_COMPOSER))
671         return -EINVAL;
672 
673     omap_hwc_device_t* hwc_dev = new omap_hwc_device_t;
674     memset(hwc_dev, 0, sizeof(*hwc_dev));
675 
676     /* Open DRM device */
677     hwc_dev->card = new kms::Card();
678 
679     /* Find primary and external connectors */
680     get_connectors(hwc_dev);
681 
682     int ret = init_primary_display(hwc_dev);
683     if (ret) {
684         ALOGE("Could not initialize primary display");
685         return -1;
686     }
687 
688     hwc_dev->event_thread = new std::thread(hwc_drm_event_thread, hwc_dev);
689 
690     hwc_dev->device.common.tag = HARDWARE_DEVICE_TAG;
691     hwc_dev->device.common.version = HWC_DEVICE_API_VERSION_1_1;
692     hwc_dev->device.common.module = (hw_module_t*)module;
693     hwc_dev->device.common.close = hwc_device_close;
694     hwc_dev->device.prepare = hwc_prepare;
695     hwc_dev->device.set = hwc_set;
696     hwc_dev->device.eventControl = hwc_eventControl;
697     hwc_dev->device.blank = hwc_blank;
698     hwc_dev->device.query = hwc_query;
699     hwc_dev->device.registerProcs = hwc_registerProcs;
700     hwc_dev->device.getDisplayConfigs = hwc_getDisplayConfigs;
701     hwc_dev->device.getDisplayAttributes = hwc_getDisplayAttributes;
702 
703     *device = &hwc_dev->device.common;
704 
705     return 0;
706 }
707 
708 static struct hw_module_methods_t module_methods = {
709     .open = hwc_device_open,
710 };
711 
712 omap_hwc_module_t HAL_MODULE_INFO_SYM = {
713     .base = {
714         .common = {
715             .tag = HARDWARE_MODULE_TAG,
716             .module_api_version = HWC_MODULE_API_VERSION_0_1,
717             .hal_api_version = HARDWARE_HAL_API_VERSION,
718             .id = HWC_HARDWARE_MODULE_ID,
719             .name = "AM57xx Hardware Composer HAL",
720             .author = "Texas Instruments",
721             .methods = &module_methods,
722         },
723     },
724 };
725