summaryrefslogtreecommitdiff
path: root/src/modules/ecore_evas/engines/extn/ecore_evas_extn_buf.c
blob: 90266151d7c303d642fc37cd0b63c116e694a353 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "ecore_evas_extn_engine.h"

struct _Extnbuf
{
   const char *file, *lock;
   void *addr;
   int fd, lockfd;
   int w, h, stride, size;
   Eina_Bool have_lock : 1;
   Eina_Bool am_owner : 1;
};

// "owner" creates/frees the bufs, clients just open existing ones
Extnbuf *
_extnbuf_new(const char *base, int id, Eina_Bool sys, int num,
             int w, int h, Eina_Bool owner)
{
   Extnbuf *b;
   char file[PATH_MAX];
   mode_t mode = S_IRUSR | S_IWUSR;
   int page_size;
   Eina_Tmpstr *tmp;

   page_size = eina_cpu_page_size();

   b = calloc(1, sizeof(Extnbuf));
   b->fd = -1;
   b->lockfd = -1;
   b->addr = MAP_FAILED;
   b->w = w;
   b->h = h;
   b->stride = w * 4;
   b->size = page_size * (((b->stride * b->h) + (page_size - 1)) / page_size);
   b->am_owner = owner;

   snprintf(file, sizeof(file), "/%s-%i.%i", base, id, num);
   b->file = eina_stringshare_add(file);
   if (!b->file) goto err;
   
   if (sys) mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
   
   if (b->am_owner)
     {
        b->lockfd = eina_file_mkstemp("ee-lock-XXXXXX", &tmp);
        if (b->lockfd < 0) goto err;
        b->lock = eina_stringshare_add(file);
        if (!b->lock) goto err;
        b->fd = shm_open(b->file, O_RDWR | O_CREAT | O_EXCL, mode);
        if (b->fd < 0) goto err;
        if (ftruncate(b->fd, b->size) < 0) goto err;
     }
   else
     {
        b->fd = shm_open(b->file, O_RDWR, mode);
        if (b->fd < 0) goto err;
     }
   b->addr = mmap(NULL, b->size, PROT_READ | PROT_WRITE, MAP_SHARED,
                  b->fd, 0);
   if (b->addr == MAP_FAILED) goto err;
   return b;
err:
   if (tmp) eina_tmpstr_del(tmp);
   _extnbuf_free(b);
   return NULL;
}

void
_extnbuf_free(Extnbuf *b)
{
   if (b->have_lock) _extnbuf_unlock(b);

   if (b->am_owner)
     {
        if (b->file) shm_unlink(b->file);
        if (b->lock) unlink(b->lock);
     }
   
   if (b->addr != MAP_FAILED) munmap(b->addr, b->size);
   if (b->fd >= 0) close(b->fd);
   if (b->lockfd >= 0) close(b->lockfd);
   eina_stringshare_del(b->file);
   eina_stringshare_del(b->lock);
   b->file = NULL;
   b->lock = NULL;
   b->addr = MAP_FAILED;
   b->fd = 1;
   b->lockfd = 1;
   b->am_owner = EINA_FALSE;
   b->have_lock = EINA_FALSE;
   b->w = 0;
   b->h = 0;
   b->stride = 0;
   b->size = 0;
   free(b);
}

// both ends can lock or unlock a buf
void *
_extnbuf_data_get(Extnbuf *b, int *w, int *h, int *stride)
{
   if (w) *w = b->w;
   if (h) *h = b->h;
   if (stride) *stride = b->stride;
   return b->addr;
}

void *
_extnbuf_lock(Extnbuf *b, int *w, int *h, int *stride)
{
   if (!b->have_lock)
     {
        if (b->lockfd >= 0)
          {
             if (lockf(b->lockfd, F_LOCK, 0) < 0)
               {
                  ERR("lock take fail");
                  return NULL;
               }
          }
        b->have_lock = EINA_TRUE;
     }
   return _extnbuf_data_get(b, w, h, stride);
}

void
_extnbuf_unlock(Extnbuf *b)
{
   if (!b->have_lock) return;
   if (b->lockfd >= 0)
     {
        if (lockf(b->lockfd, F_ULOCK, 0) < 0)
          {
             ERR("lock release fail");
             return;
          }
     }
   b->have_lock = EINA_FALSE;
}

const char *
_extnbuf_lock_file_get(const Extnbuf *b)
{
   return b->lock;
}

Eina_Bool
_extnbuf_lock_file_set(Extnbuf *b, const char *file)
{
   if (b->am_owner) return EINA_FALSE;
   if (b->lock) eina_stringshare_del(b->lock);
   if (b->lockfd >= 0) close(b->lockfd);
   b->lockfd = -1;
   if (!file)
     {
        b->lock = NULL;
        b->lockfd = -1;
        return EINA_TRUE;
     }
   b->lock = eina_stringshare_add(file);
   if (!b->lock) goto err;
   b->lockfd = open(b->lock, O_RDWR);
   if (b->lockfd >= 0) return EINA_TRUE;
err:
   if (b->lock) eina_stringshare_del(b->lock);
   if (b->lockfd >= 0) close(b->lockfd);
   b->lockfd = -1;
   b->lock = NULL;
   return EINA_FALSE;
}

Eina_Bool
_extnbuf_lock_get(const Extnbuf *b)
{
   return b->have_lock;
}