OSDN Git Service

Android: Use same sw_sync ABI as supported by AOSP version.
[android-x86/external-IA-Hardware-Composer.git] / common / core / nativesync.cpp
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include "nativesync.h"
18
19 #include <string.h>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #include <platformdefines.h>
25
26 #include "hwctrace.h"
27
28 namespace hwcomposer {
29
30 #ifndef USE_ANDROID_SYNC
31 struct sw_sync_create_fence_data {
32   __u32 value;
33   char name[32];
34   __s32 fence; /* fd of new fence */
35 };
36
37 #define SW_SYNC_IOC_MAGIC 'W'
38
39 #define SW_SYNC_IOC_CREATE_FENCE \
40   _IOWR(SW_SYNC_IOC_MAGIC, 0, struct sw_sync_create_fence_data)
41
42 #define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
43
44 #endif
45
46 NativeSync::NativeSync() {
47 }
48
49 NativeSync::~NativeSync() {
50   if (timeline_fd_.get() >= 0)
51     IncreaseTimelineToPoint(timeline_);
52 }
53
54 bool NativeSync::Init() {
55 #ifdef USE_ANDROID_SYNC
56   timeline_fd_.Reset(open("/dev/sw_sync", O_RDWR));
57 #else
58   timeline_fd_.Reset(open("/sys/kernel/debug/sync/sw_sync", O_RDWR));
59 #endif
60   if (timeline_fd_.get() < 0) {
61     ETRACE("Failed to create sw sync timeline %s", PRINTERROR());
62     return false;
63   }
64
65   return true;
66 }
67
68 int NativeSync::CreateNextTimelineFence() {
69   ++timeline_;
70   return sw_sync_fence_create(timeline_fd_.get(), "NativeSync", timeline_);
71 }
72
73 bool NativeSync::Wait(int fence) {
74   int ret = sync_wait(fence, 1000);
75   if (ret) {
76     ETRACE("Failed to wait for fence ret=%s\n", PRINTERROR());
77     return false;
78   }
79
80   return true;
81 }
82
83 int NativeSync::IncreaseTimelineToPoint(int point) {
84   int timeline_increase = point - timeline_current_;
85   if (timeline_increase <= 0)
86     return 0;
87
88   int ret = sw_sync_timeline_inc(timeline_fd_.get(), timeline_increase);
89   if (ret)
90     ETRACE("Failed to increment sync timeline %s", PRINTERROR());
91   else
92     timeline_current_ = point;
93
94   return ret;
95 }
96
97 #ifndef USE_ANDROID_SYNC
98 int NativeSync::sw_sync_fence_create(int fd, const char *name, unsigned value) {
99   struct sw_sync_create_fence_data data;
100   memset(&data, 0, sizeof(data));
101   int err;
102
103   data.value = value;
104   size_t srclen = strlen(name);
105   strncpy(data.name, name, srclen);
106   data.name[srclen] = '\0';
107   data.fence = 0;
108
109   err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
110   if (err < 0)
111     return err;
112
113   return data.fence;
114 }
115
116 int NativeSync::sw_sync_timeline_inc(int fd, unsigned count) {
117   uint32_t arg = count;
118   return ioctl(fd, SW_SYNC_IOC_INC, &arg);
119 }
120 #endif
121
122 }  // namespace hwcomposer