summaryrefslogtreecommitdiff
path: root/src/lib/ecore_wl2/ecore_wl2_surface.c
blob: cd810d21b55c8c178f5b0f0d42af4cb45976bb3e (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ecore_wl2_private.h"

#include <sys/types.h>
#include <sys/stat.h>

#include "linux-dmabuf-unstable-v1-client-protocol.h"

#define MAX_BUFFERS 4

static void
_evas_dmabuf_surface_reconfigure(Ecore_Wl2_Surface *s, int w, int h, uint32_t flags EINA_UNUSED, Eina_Bool force)
{
   Ecore_Wl2_Buffer *b;
   Eina_List *l, *tmp;

   if ((!w) || (!h)) return;
   EINA_LIST_FOREACH_SAFE(s->buffers, l, tmp, b)
     {
        int stride = b->stride;

        /* If stride is a little bigger than width we still fit */
        if (!force && (w >= b->w) && (w <= stride / 4) && (h == b->h))
          {
             b->w = w;
             continue;
          }
        ecore_wl2_buffer_destroy(b);
        s->buffers = eina_list_remove_list(s->buffers, l);
     }
   s->w = w;
   s->h = h;
}

static void *
_evas_dmabuf_surface_data_get(Ecore_Wl2_Surface *s, int *w, int *h)
{
   Ecore_Wl2_Buffer *b;
   void *ptr;

   b = s->current;
   if (!b) return NULL;

   /* We return stride/bpp because it may not match the allocated
    * width.  evas will figure out the clipping
    */
   if (w) *w = b->stride / 4;
   if (h) *h = b->h;
   if (b->locked) return b->mapping;

   ptr = ecore_wl2_buffer_map(b);
   if (!ptr)
     return NULL;

   b->mapping = ptr;
   b->locked = EINA_TRUE;
   return b->mapping;
}

static Ecore_Wl2_Buffer *
_evas_dmabuf_surface_wait(Ecore_Wl2_Surface *s)
{
   Ecore_Wl2_Buffer *b, *best = NULL;
   Eina_List *l;
   int best_age = -1;

   EINA_LIST_FOREACH(s->buffers, l, b)
     {
        if (b->locked || b->busy) continue;
        if (b->age > best_age)
          {
             best = b;
             best_age = b->age;
          }
     }

   if (!best && (eina_list_count(s->buffers) < MAX_BUFFERS))
     {
        Ecore_Wl2_Display *ewd;

        ewd = ecore_wl2_window_display_get(s->wl2_win);
        EINA_SAFETY_ON_NULL_RETURN_VAL(ewd, NULL);

        best = ecore_wl2_buffer_create(ewd, s->w, s->h, s->alpha);
        /* Start at -1 so it's age is incremented to 0 for first draw */
        best->age = -1;
        s->buffers = eina_list_append(s->buffers, best);
     }
   return best;
}

static int
_evas_dmabuf_surface_assign(Ecore_Wl2_Surface *s)
{
   Ecore_Wl2_Buffer *b;
   Eina_List *l;

   s->current = _evas_dmabuf_surface_wait(s);
   if (!s->current)
     {
        /* Should be unreachable and will result in graphical
         * anomalies - we should probably blow away all the
         * existing buffers and start over if we actually
         * see this happen...
         */
        WRN("No free DMAbuf buffers, dropping a frame");
        EINA_LIST_FOREACH(s->buffers, l, b)
          b->age = 0;
        return 0;
     }
   EINA_LIST_FOREACH(s->buffers, l, b)
     b->age++;

   return s->current->age;
}

static void
_evas_dmabuf_surface_post(Ecore_Wl2_Surface *s, Eina_Rectangle *rects, unsigned int count)
{
   Ecore_Wl2_Buffer *b;

   b = s->current;
   if (!b) return;

   ecore_wl2_buffer_unlock(b);

   s->current = NULL;
   b->busy = EINA_TRUE;
   b->age = 0;

   ecore_wl2_window_buffer_attach(s->wl2_win, b->wl_buffer, 0, 0, EINA_FALSE);
   ecore_wl2_window_damage(s->wl2_win, rects, count);

   ecore_wl2_window_commit(s->wl2_win, EINA_TRUE);
}

static void
_evas_dmabuf_surface_destroy(Ecore_Wl2_Surface *s)
{
   Ecore_Wl2_Buffer *b;

   if (!s) return;

   EINA_LIST_FREE(s->buffers, b)
     ecore_wl2_buffer_destroy(b);
}

static void
_surface_flush(Ecore_Wl2_Surface *surface)
{
   Ecore_Wl2_Buffer *b;

   EINA_SAFETY_ON_NULL_RETURN(surface);

   EINA_LIST_FREE(surface->buffers, b)
     ecore_wl2_buffer_destroy(b);
}


EAPI void
ecore_wl2_surface_destroy(Ecore_Wl2_Surface *surface)
{
   EINA_SAFETY_ON_NULL_RETURN(surface);

   surface->funcs.destroy(surface);
   surface->wl2_win = NULL;

   free(surface);
}

EAPI void
ecore_wl2_surface_reconfigure(Ecore_Wl2_Surface *surface, int w, int h, uint32_t flags, Eina_Bool force)
{
   EINA_SAFETY_ON_NULL_RETURN(surface);

   surface->funcs.reconfigure(surface, w, h, flags, force);
}

EAPI void *
ecore_wl2_surface_data_get(Ecore_Wl2_Surface *surface, int *w, int *h)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(surface, NULL);

   return surface->funcs.data_get(surface, w, h);
}

EAPI int
ecore_wl2_surface_assign(Ecore_Wl2_Surface *surface)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(surface, 0);

   return surface->funcs.assign(surface);
}

EAPI void
ecore_wl2_surface_post(Ecore_Wl2_Surface *surface, Eina_Rectangle *rects, unsigned int count)
{
   EINA_SAFETY_ON_NULL_RETURN(surface);

   surface->funcs.post(surface, rects, count);
}

EAPI void
ecore_wl2_surface_flush(Ecore_Wl2_Surface *surface)
{
   EINA_SAFETY_ON_NULL_RETURN(surface);

   surface->funcs.flush(surface);
}

EAPI Ecore_Wl2_Surface *
ecore_wl2_surface_create(Ecore_Wl2_Window *win, Eina_Bool alpha)
{
   Ecore_Wl2_Surface *out;
   Ecore_Wl2_Display *ewd;
   Ecore_Wl2_Buffer_Type types = 0;

   EINA_SAFETY_ON_NULL_RETURN_VAL(win, NULL);

   if (win->wl2_surface) return win->wl2_surface;

   out = calloc(1, sizeof(*out));
   if (!out) return NULL;
   out->wl2_win = win;

   ewd = ecore_wl2_window_display_get(win);
   if (ecore_wl2_display_shm_get(ewd))
     types |= ECORE_WL2_BUFFER_SHM;
   if (ecore_wl2_display_dmabuf_get(ewd))
     types |= ECORE_WL2_BUFFER_DMABUF;

   out->alpha = alpha;
   out->w = 0;
   out->h = 0;

   /* create surface buffers */
   if (!ecore_wl2_buffer_init(ewd, types)) goto err;

   out->funcs.destroy = _evas_dmabuf_surface_destroy;
   out->funcs.reconfigure = _evas_dmabuf_surface_reconfigure;
   out->funcs.data_get = _evas_dmabuf_surface_data_get;
   out->funcs.assign = _evas_dmabuf_surface_assign;
   out->funcs.post = _evas_dmabuf_surface_post;
   out->funcs.flush = _surface_flush;
   win->wl2_surface = out;
   return out;

err:
   free(out);
   return NULL;
}