Home | History | Annotate | Download | only in libhwcomposer
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  * Copyright (C) 2012-2014, The Linux Foundation. All rights reserved.
      4  *
      5  * Not a Contribution, Apache license notifications and license are
      6  * retained for attribution purposes only.
      7 
      8  * Licensed under the Apache License, Version 2.0 (the "License");
      9  * you may not use this file except in compliance with the License.
     10  * You may obtain a copy of the License at
     11  *
     12  *      http://www.apache.org/licenses/LICENSE-2.0
     13  *
     14  * Unless required by applicable law or agreed to in writing, software
     15  * distributed under the License is distributed on an "AS IS" BASIS,
     16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17  * See the License for the specific language governing permissions and
     18  * limitations under the License.
     19  */
     20 
     21 #include <cutils/properties.h>
     22 #include <utils/Log.h>
     23 #include <fcntl.h>
     24 #include <sys/ioctl.h>
     25 #include <linux/msm_mdp.h>
     26 #include <sys/resource.h>
     27 #include <sys/prctl.h>
     28 #include <poll.h>
     29 #include "hwc_utils.h"
     30 #include "string.h"
     31 #include "external.h"
     32 #include "overlay.h"
     33 #include <inttypes.h>
     34 
     35 namespace qhwc {
     36 
     37 #define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
     38 #define MAX_SYSFS_FILE_PATH             255
     39 #define PANEL_ON_STR "panel_power_on ="
     40 #define ARRAY_LENGTH(array) (sizeof((array))/sizeof((array)[0]))
     41 const int MAX_DATA = 64;
     42 bool logvsync = false;
     43 
     44 int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable)
     45 {
     46     int ret = 0;
     47     if(!ctx->vstate.fakevsync &&
     48        ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
     49              &enable) < 0) {
     50         ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s",
     51               __FUNCTION__, dpy, enable, strerror(errno));
     52         ret = -errno;
     53     }
     54     return ret;
     55 }
     56 
     57 static void handle_vsync_event(hwc_context_t* ctx, int dpy, char *data,
     58         ssize_t len __unused)
     59 {
     60     // extract timestamp
     61     uint64_t timestamp = 0;
     62     if (!strncmp(data, "VSYNC=", strlen("VSYNC="))) {
     63         timestamp = strtoull(data + strlen("VSYNC="), NULL, 0);
     64     }
     65     // send timestamp to SurfaceFlinger
     66     ALOGD_IF (logvsync, "%s: timestamp %" PRIu64 " sent to SF for dpy=%d",
     67             __FUNCTION__, timestamp, dpy);
     68     ctx->proc->vsync(ctx->proc, dpy, timestamp);
     69 }
     70 
     71 static void handle_blank_event(hwc_context_t* ctx, int dpy, char *data,
     72         ssize_t len __unused)
     73 {
     74     if (!strncmp(data, PANEL_ON_STR, strlen(PANEL_ON_STR))) {
     75         uint32_t poweron = strtoul(data + strlen(PANEL_ON_STR), NULL, 0);
     76         ALOGI("%s: dpy:%d panel power state: %d", __FUNCTION__, dpy, poweron);
     77         ctx->dpyAttr[dpy].isActive = poweron ? true: false;
     78     }
     79 }
     80 
     81 static void handle_cec_event(hwc_context_t* ctx, int dpy, char *data,
     82         ssize_t len)
     83 {
     84     ALOGD("%s: Got CEC event from driver dpy:%d", __FUNCTION__, dpy);
     85     ctx->mQService->onCECMessageReceived(data, len);
     86 }
     87 
     88 struct event {
     89     const char* name;
     90     void (*callback)(hwc_context_t* ctx, int dpy, char *data, ssize_t len);
     91 };
     92 
     93 struct event event_list[] =  {
     94     { "vsync_event", handle_vsync_event },
     95     { "show_blank_event", handle_blank_event },
     96     { "cec/rd_msg", handle_cec_event },
     97 };
     98 
     99 #define num_events ARRAY_LENGTH(event_list)
    100 
    101 static void *vsync_loop(void *param)
    102 {
    103     hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
    104 
    105     char thread_name[64] = HWC_VSYNC_THREAD_NAME;
    106     prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
    107     setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
    108                 android::PRIORITY_MORE_FAVORABLE);
    109 
    110     char vdata[MAX_DATA];
    111     //Number of physical displays
    112     //We poll on all the nodes.
    113     int num_displays = HWC_NUM_DISPLAY_TYPES - 1;
    114     struct pollfd pfd[num_displays][num_events];
    115 
    116     char property[PROPERTY_VALUE_MAX];
    117     if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
    118         if(atoi(property) == 1)
    119             ctx->vstate.fakevsync = true;
    120     }
    121 
    122     if(property_get("debug.hwc.logvsync", property, 0) > 0) {
    123         if(atoi(property) == 1)
    124             logvsync = true;
    125     }
    126 
    127     char node_path[MAX_SYSFS_FILE_PATH];
    128 
    129     for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
    130         for(size_t ev = 0; ev < num_events; ev++) {
    131             snprintf(node_path, sizeof(node_path),
    132                     "/sys/class/graphics/fb%d/%s",
    133                     dpy == HWC_DISPLAY_PRIMARY ? 0 :
    134                     overlay::Overlay::getInstance()->
    135                     getFbForDpy(HWC_DISPLAY_EXTERNAL),
    136                     event_list[ev].name);
    137 
    138             ALOGI("%s: Reading event %zu for dpy %d from %s", __FUNCTION__,
    139                     ev, dpy, node_path);
    140             pfd[dpy][ev].fd = open(node_path, O_RDONLY);
    141 
    142             if (dpy == HWC_DISPLAY_PRIMARY && pfd[dpy][ev].fd < 0) {
    143                 // Make sure fb device is opened before starting
    144                 // this thread so this never happens.
    145                 ALOGE ("%s:unable to open event node for dpy=%d event=%zu, %s",
    146                         __FUNCTION__, dpy, ev, strerror(errno));
    147                 if (ev == 0) {
    148                     ctx->vstate.fakevsync = true;
    149                     //XXX: Blank events don't work with fake vsync,
    150                     //but we shouldn't be running on fake vsync anyway.
    151                     break;
    152                 }
    153             }
    154 
    155             pread(pfd[dpy][ev].fd, vdata , MAX_DATA, 0);
    156             if (pfd[dpy][ev].fd >= 0)
    157                 pfd[dpy][ev].events = POLLPRI | POLLERR;
    158         }
    159     }
    160 
    161     if (LIKELY(!ctx->vstate.fakevsync)) {
    162         do {
    163             int err = poll(*pfd, num_displays * num_events, -1);
    164             if(err > 0) {
    165                 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
    166                     for(size_t ev = 0; ev < num_events; ev++) {
    167                         if (pfd[dpy][ev].revents & POLLPRI) {
    168                             ssize_t len = pread(pfd[dpy][ev].fd, vdata, MAX_DATA, 0);
    169                             if (UNLIKELY(len < 0)) {
    170                                 // If the read was just interrupted - it is not
    171                                 // a fatal error. Just continue in this case
    172                                 ALOGE ("%s: Unable to read event:%zu for \
    173                                         dpy=%d : %s",
    174                                         __FUNCTION__, ev, dpy, strerror(errno));
    175                                 continue;
    176                             }
    177                             event_list[ev].callback(ctx, dpy, vdata, len);
    178                         }
    179                     }
    180                 }
    181             } else {
    182                 ALOGE("%s: poll failed errno: %s", __FUNCTION__,
    183                         strerror(errno));
    184                 continue;
    185             }
    186         } while (true);
    187 
    188     } else {
    189 
    190         //Fake vsync is used only when set explicitly through a property or when
    191         //the vsync timestamp node cannot be opened at bootup. There is no
    192         //fallback to fake vsync from the true vsync loop, ever, as the
    193         //condition can easily escape detection.
    194         //Also, fake vsync is delivered only for the primary display.
    195         do {
    196             usleep(16666);
    197             uint64_t timestamp = systemTime();
    198             ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY, timestamp);
    199 
    200         } while (true);
    201     }
    202 
    203     for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) {
    204         for( size_t event = 0; event < num_events; event++) {
    205             if(pfd[dpy][event].fd >= 0)
    206                 close (pfd[dpy][event].fd);
    207         }
    208     }
    209 
    210     return NULL;
    211 }
    212 
    213 void init_vsync_thread(hwc_context_t* ctx)
    214 {
    215     int ret;
    216     pthread_t vsync_thread;
    217     ALOGI("Initializing VSYNC Thread");
    218     ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
    219     if (ret) {
    220         ALOGE("%s: failed to create %s: %s", __FUNCTION__,
    221               HWC_VSYNC_THREAD_NAME, strerror(ret));
    222     }
    223 }
    224 
    225 }; //namespace
    226