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 #define __STDC_FORMAT_MACROS 1
     34 #include <inttypes.h>
     35 
     36 namespace qhwc {
     37 
     38 #define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
     39 #define MAX_SYSFS_FILE_PATH             255
     40 #define PANEL_ON_STR "panel_power_on ="
     41 #define ARRAY_LENGTH(array) (sizeof((array))/sizeof((array)[0]))
     42 const int MAX_DATA = 64;
     43 bool logvsync = false;
     44 
     45 int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable)
     46 {
     47     int ret = 0;
     48     if(!ctx->vstate.fakevsync &&
     49        ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
     50              &enable) < 0) {
     51         ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s",
     52               __FUNCTION__, dpy, enable, strerror(errno));
     53         ret = -errno;
     54     }
     55     return ret;
     56 }
     57 
     58 static void handle_vsync_event(hwc_context_t* ctx, int dpy, char *data,
     59         ssize_t len __unused)
     60 {
     61     // extract timestamp
     62     uint64_t timestamp = 0;
     63     if (!strncmp(data, "VSYNC=", strlen("VSYNC="))) {
     64         timestamp = strtoull(data + strlen("VSYNC="), NULL, 0);
     65     }
     66     // send timestamp to SurfaceFlinger
     67     ALOGD_IF (logvsync, "%s: timestamp %" PRIu64 " sent to SF for dpy=%d",
     68             __FUNCTION__, timestamp, dpy);
     69     ctx->proc->vsync(ctx->proc, dpy, timestamp);
     70 }
     71 
     72 static void handle_blank_event(hwc_context_t* ctx, int dpy, char *data,
     73         ssize_t len __unused)
     74 {
     75     if (!strncmp(data, PANEL_ON_STR, strlen(PANEL_ON_STR))) {
     76         uint32_t poweron = strtoul(data + strlen(PANEL_ON_STR), NULL, 0);
     77         ALOGI("%s: dpy:%d panel power state: %d", __FUNCTION__, dpy, poweron);
     78         ctx->dpyAttr[dpy].isActive = poweron ? true: false;
     79     }
     80 }
     81 
     82 static void handle_cec_event(hwc_context_t* ctx, int dpy, char *data,
     83         ssize_t len)
     84 {
     85     ALOGD("%s: Got CEC event from driver dpy:%d", __FUNCTION__, dpy);
     86     ctx->mQService->onCECMessageReceived(data, len);
     87 }
     88 
     89 struct event {
     90     const char* name;
     91     void (*callback)(hwc_context_t* ctx, int dpy, char *data, ssize_t len);
     92 };
     93 
     94 struct event event_list[] =  {
     95     { "vsync_event", handle_vsync_event },
     96     { "show_blank_event", handle_blank_event },
     97     { "cec/rd_msg", handle_cec_event },
     98 };
     99 
    100 #define num_events ARRAY_LENGTH(event_list)
    101 
    102 static void *vsync_loop(void *param)
    103 {
    104     hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
    105 
    106     char thread_name[64] = HWC_VSYNC_THREAD_NAME;
    107     prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
    108     setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
    109                 android::PRIORITY_MORE_FAVORABLE);
    110 
    111     char vdata[MAX_DATA];
    112     //Number of physical displays
    113     //We poll on all the nodes.
    114     int num_displays = HWC_NUM_DISPLAY_TYPES - 1;
    115     struct pollfd pfd[num_displays][num_events];
    116 
    117     char property[PROPERTY_VALUE_MAX];
    118     if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
    119         if(atoi(property) == 1)
    120             ctx->vstate.fakevsync = true;
    121     }
    122 
    123     if(property_get("debug.hwc.logvsync", property, 0) > 0) {
    124         if(atoi(property) == 1)
    125             logvsync = true;
    126     }
    127 
    128     char node_path[MAX_SYSFS_FILE_PATH];
    129 
    130     for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
    131         for(size_t ev = 0; ev < num_events; ev++) {
    132             snprintf(node_path, sizeof(node_path),
    133                     "/sys/class/graphics/fb%d/%s",
    134                     dpy == HWC_DISPLAY_PRIMARY ? 0 :
    135                     overlay::Overlay::getInstance()->
    136                     getFbForDpy(HWC_DISPLAY_EXTERNAL),
    137                     event_list[ev].name);
    138 
    139             ALOGI("%s: Reading event %zu for dpy %d from %s", __FUNCTION__,
    140                     ev, dpy, node_path);
    141             pfd[dpy][ev].fd = open(node_path, O_RDONLY);
    142 
    143             if (dpy == HWC_DISPLAY_PRIMARY && pfd[dpy][ev].fd < 0) {
    144                 // Make sure fb device is opened before starting
    145                 // this thread so this never happens.
    146                 ALOGE ("%s:unable to open event node for dpy=%d event=%zu, %s",
    147                         __FUNCTION__, dpy, ev, strerror(errno));
    148                 if (ev == 0) {
    149                     ctx->vstate.fakevsync = true;
    150                     //XXX: Blank events don't work with fake vsync,
    151                     //but we shouldn't be running on fake vsync anyway.
    152                     break;
    153                 }
    154             }
    155 
    156             pread(pfd[dpy][ev].fd, vdata , MAX_DATA, 0);
    157             if (pfd[dpy][ev].fd >= 0)
    158                 pfd[dpy][ev].events = POLLPRI | POLLERR;
    159         }
    160     }
    161 
    162     if (LIKELY(!ctx->vstate.fakevsync)) {
    163         do {
    164             int err = poll(*pfd, num_displays * num_events, -1);
    165             if(err > 0) {
    166                 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
    167                     for(size_t ev = 0; ev < num_events; ev++) {
    168                         if (pfd[dpy][ev].revents & POLLPRI) {
    169                             ssize_t len = pread(pfd[dpy][ev].fd, vdata, MAX_DATA, 0);
    170                             if (UNLIKELY(len < 0)) {
    171                                 // If the read was just interrupted - it is not
    172                                 // a fatal error. Just continue in this case
    173                                 ALOGE ("%s: Unable to read event:%zu for \
    174                                         dpy=%d : %s",
    175                                         __FUNCTION__, ev, dpy, strerror(errno));
    176                                 continue;
    177                             }
    178                             event_list[ev].callback(ctx, dpy, vdata, len);
    179                         }
    180                     }
    181                 }
    182             } else {
    183                 ALOGE("%s: poll failed errno: %s", __FUNCTION__,
    184                         strerror(errno));
    185                 continue;
    186             }
    187         } while (true);
    188 
    189     } else {
    190 
    191         //Fake vsync is used only when set explicitly through a property or when
    192         //the vsync timestamp node cannot be opened at bootup. There is no
    193         //fallback to fake vsync from the true vsync loop, ever, as the
    194         //condition can easily escape detection.
    195         //Also, fake vsync is delivered only for the primary display.
    196         do {
    197             usleep(16666);
    198             uint64_t timestamp = systemTime();
    199             ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY, timestamp);
    200 
    201         } while (true);
    202     }
    203 
    204     for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) {
    205         for( size_t event = 0; event < num_events; event++) {
    206             if(pfd[dpy][event].fd >= 0)
    207                 close (pfd[dpy][event].fd);
    208         }
    209     }
    210 
    211     return NULL;
    212 }
    213 
    214 void init_vsync_thread(hwc_context_t* ctx)
    215 {
    216     int ret;
    217     pthread_t vsync_thread;
    218     ALOGI("Initializing VSYNC Thread");
    219     ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
    220     if (ret) {
    221         ALOGE("%s: failed to create %s: %s", __FUNCTION__,
    222               HWC_VSYNC_THREAD_NAME, strerror(ret));
    223     }
    224 }
    225 
    226 }; //namespace
    227