2 // Copyright (c) 2016 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "nativesync.h"
21 #include <sys/types.h>
24 #include <platformdefines.h>
28 namespace hwcomposer {
30 #ifndef USE_ANDROID_SYNC
31 struct sw_sync_create_fence_data {
34 __s32 fence; /* fd of new fence */
37 #define SW_SYNC_IOC_MAGIC 'W'
39 #define SW_SYNC_IOC_CREATE_FENCE \
40 _IOWR(SW_SYNC_IOC_MAGIC, 0, struct sw_sync_create_fence_data)
42 #define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
46 NativeSync::NativeSync() {
49 NativeSync::~NativeSync() {
50 if (timeline_fd_.get() >= 0)
51 IncreaseTimelineToPoint(timeline_);
54 bool NativeSync::Init() {
55 #ifdef USE_ANDROID_SYNC
56 timeline_fd_.Reset(open("/dev/sw_sync", O_RDWR));
58 timeline_fd_.Reset(open("/sys/kernel/debug/sync/sw_sync", O_RDWR));
60 if (timeline_fd_.get() < 0) {
61 ETRACE("Failed to create sw sync timeline %s", PRINTERROR());
68 int NativeSync::CreateNextTimelineFence() {
70 return sw_sync_fence_create(timeline_fd_.get(), "NativeSync", timeline_);
73 bool NativeSync::Wait(int fence) {
74 int ret = sync_wait(fence, 1000);
76 ETRACE("Failed to wait for fence ret=%s\n", PRINTERROR());
83 int NativeSync::IncreaseTimelineToPoint(int point) {
84 int timeline_increase = point - timeline_current_;
85 if (timeline_increase <= 0)
88 int ret = sw_sync_timeline_inc(timeline_fd_.get(), timeline_increase);
90 ETRACE("Failed to increment sync timeline %s", PRINTERROR());
92 timeline_current_ = point;
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));
104 size_t srclen = strlen(name);
105 strncpy(data.name, name, srclen);
106 data.name[srclen] = '\0';
109 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
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);
122 } // namespace hwcomposer