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