1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * Copyright (C) 2012-2013, 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 "hwc_utils.h" 29 #include "string.h" 30 #include "external.h" 31 32 namespace qhwc { 33 34 #define HWC_VSYNC_THREAD_NAME "hwcVsyncThread" 35 36 int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable) 37 { 38 int ret = 0; 39 if(!ctx->vstate.fakevsync && 40 ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL, 41 &enable) < 0) { 42 ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s", 43 __FUNCTION__, dpy, enable, strerror(errno)); 44 ret = -errno; 45 } 46 return ret; 47 } 48 49 static void *vsync_loop(void *param) 50 { 51 const char* vsync_timestamp_fb0 = "/sys/class/graphics/fb0/vsync_event"; 52 const char* vsync_timestamp_fb1 = "/sys/class/graphics/fb1/vsync_event"; 53 int dpy = HWC_DISPLAY_PRIMARY; 54 55 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param); 56 57 char thread_name[64] = HWC_VSYNC_THREAD_NAME; 58 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0); 59 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY + 60 android::PRIORITY_MORE_FAVORABLE); 61 62 const int MAX_DATA = 64; 63 static char vdata[MAX_DATA]; 64 65 uint64_t cur_timestamp=0; 66 ssize_t len = -1; 67 int fd_timestamp = -1; 68 bool fb1_vsync = false; 69 bool logvsync = false; 70 71 char property[PROPERTY_VALUE_MAX]; 72 if(property_get("debug.hwc.fakevsync", property, NULL) > 0) { 73 if(atoi(property) == 1) 74 ctx->vstate.fakevsync = true; 75 } 76 77 if(property_get("debug.hwc.logvsync", property, 0) > 0) { 78 if(atoi(property) == 1) 79 logvsync = true; 80 } 81 82 /* Currently read vsync timestamp from drivers 83 e.g. VSYNC=41800875994 84 */ 85 fd_timestamp = open(vsync_timestamp_fb0, O_RDONLY); 86 if (fd_timestamp < 0) { 87 // Make sure fb device is opened before starting this thread so this 88 // never happens. 89 ALOGE ("FATAL:%s:not able to open file:%s, %s", __FUNCTION__, 90 (fb1_vsync) ? vsync_timestamp_fb1 : vsync_timestamp_fb0, 91 strerror(errno)); 92 ctx->vstate.fakevsync = true; 93 } 94 95 do { 96 if (LIKELY(!ctx->vstate.fakevsync)) { 97 len = pread(fd_timestamp, vdata, MAX_DATA, 0); 98 if (len < 0) { 99 // If the read was just interrupted - it is not a fatal error 100 // In either case, just continue. 101 if (errno != EAGAIN && 102 errno != EINTR && 103 errno != EBUSY) { 104 ALOGE ("FATAL:%s:not able to read file:%s, %s", 105 __FUNCTION__, 106 vsync_timestamp_fb0, strerror(errno)); 107 } 108 continue; 109 } 110 // extract timestamp 111 const char *str = vdata; 112 if (!strncmp(str, "VSYNC=", strlen("VSYNC="))) { 113 cur_timestamp = strtoull(str + strlen("VSYNC="), NULL, 0); 114 } 115 } else { 116 usleep(16666); 117 cur_timestamp = systemTime(); 118 } 119 // send timestamp to HAL 120 if(ctx->vstate.enable) { 121 ALOGD_IF (logvsync, "%s: timestamp %llu sent to HWC for %s", 122 __FUNCTION__, cur_timestamp, "fb0"); 123 ctx->proc->vsync(ctx->proc, dpy, cur_timestamp); 124 } 125 126 } while (true); 127 if(fd_timestamp >= 0) 128 close (fd_timestamp); 129 130 return NULL; 131 } 132 133 void init_vsync_thread(hwc_context_t* ctx) 134 { 135 int ret; 136 pthread_t vsync_thread; 137 ALOGI("Initializing VSYNC Thread"); 138 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx); 139 if (ret) { 140 ALOGE("%s: failed to create %s: %s", __FUNCTION__, 141 HWC_VSYNC_THREAD_NAME, strerror(ret)); 142 } 143 } 144 145 }; //namespace 146