1 /*
2 * Copyright (c) 2015, 2019-2020, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * Redistributions in binary form must reproduce the above
10 *    copyright notice, this list of conditions and the following
11 *    disclaimer in the documentation and/or other materials provided
12 *    with the distribution.
13 *  * Neither the name of The Linux Foundation nor the names of its
14 *    contributors may be used to endorse or promote products derived
15 *    from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #include <errno.h>
31 #include <sync/sync.h>
32 #include <utils/constants.h>
33 #include <utils/debug.h>
34 #include <utils/fence.h>
35 
36 #include "hwc_debugger.h"
37 #include "hwc_buffer_sync_handler.h"
38 
39 #define __CLASS__ "HWCBufferSyncHandler"
40 
41 namespace sdm {
42 
43 HWCBufferSyncHandler HWCBufferSyncHandler::g_hwc_buffer_sync_handler_;
44 
HWCBufferSyncHandler()45 HWCBufferSyncHandler::HWCBufferSyncHandler() {
46   Fence::Set(this);
47 }
48 
SyncWait(int fd,int timeout)49 DisplayError HWCBufferSyncHandler::SyncWait(int fd, int timeout) {
50   // Assume invalid fd as signaled.
51   if (fd < 0) {
52     return kErrorNone;
53   }
54 
55   int error = sync_wait(fd, timeout);
56   if (!error) {
57     return kErrorNone;
58   }
59 
60   // Fence is not signaled yet.
61   if (errno == ETIME) {
62     return kErrorTimeOut;
63   }
64 
65   DLOGW("sync_wait fd = %d, timeout = %d ms, err = %d : %s", fd, timeout, errno, strerror(errno));
66 
67   return kErrorUndefined;
68 }
69 
SyncMerge(int fd1,int fd2,int * merged_fd)70 DisplayError HWCBufferSyncHandler::SyncMerge(int fd1, int fd2, int *merged_fd) {
71   // Caller owns fds, hence, if
72   //  one of the fence fd is invalid, create dup of valid fd and set to merged fd.
73   //  both fence fds are same, create dup of one of the fd and set to merged fd.
74   *merged_fd = -1;
75   if (fd1 < 0) {
76     *merged_fd = dup(fd2);
77   } else if ((fd2 < 0) || (fd1 == fd2)) {
78     *merged_fd = dup(fd1);
79   } else {
80     *merged_fd = sync_merge("SyncMerge", fd1, fd2);
81   }
82 
83   return kErrorNone;
84 }
85 
GetSyncInfo(int fd,std::ostringstream * os)86 void HWCBufferSyncHandler::GetSyncInfo(int fd, std::ostringstream *os) {
87   struct sync_file_info *file_info = sync_file_info(fd);
88   if (!file_info) {
89     return;
90   }
91 
92   *os << "SYNC_File status:: " << file_info->status;
93   *os << ", name: " << file_info->name;
94   *os << ", num_fences: " << file_info->num_fences;
95 
96   struct sync_fence_info *fence_info = sync_get_fence_info(file_info);
97   if (!fence_info) {
98     return;
99   }
100 
101   for (size_t i = 0; i < file_info->num_fences; i++) {
102     *os << ", fence[" << i << "]:: ";
103     *os << "status: " << fence_info[i].status;
104     *os << ", drv_name: " << fence_info[i].driver_name;
105     *os << ", obj_name: " << fence_info[i].obj_name;
106     *os << ", ts: " << fence_info[i].timestamp_ns;
107   }
108 }
109 
SyncWait(int fd)110 DisplayError HWCBufferSyncHandler::SyncWait(int fd) {
111   // Deprecated.
112   assert(false);
113   return kErrorUndefined;
114 }
115 
IsSyncSignaled(int fd)116 bool HWCBufferSyncHandler::IsSyncSignaled(int fd) {
117   // Deprecated.
118   assert(false);
119   return false;
120 }
121 
122 }  // namespace sdm
123