summaryrefslogtreecommitdiff
path: root/src/lib/ecore_drm2/ecore_drm2_fb.c
blob: 7142ab135aeea262eb24867de5f3e7782d966965 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include "ecore_drm2_private.h"

static Eina_Bool
_fb2_create(Ecore_Drm2_Fb *fb)
{
   struct drm_mode_fb_cmd2 cmd;
   uint32_t hdls[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
   uint64_t modifiers[4] = { 0 };

   hdls[0] = fb->hdl;
   pitches[0] = fb->stride;
   offsets[0] = 0;
   modifiers[0] = 0;

   memset(&cmd, 0, sizeof(struct drm_mode_fb_cmd2));
   cmd.fb_id = 0;
   cmd.width = fb->w;
   cmd.height = fb->h;
   cmd.pixel_format = fb->format;
   cmd.flags = 0;
   memcpy(cmd.handles, hdls, 4 * sizeof(hdls[0]));
   memcpy(cmd.pitches, pitches, 4 * sizeof(pitches[0]));
   memcpy(cmd.offsets, offsets, 4 * sizeof(offsets[0]));
   memcpy(cmd.modifier, modifiers, 4 * sizeof(modifiers[0]));

   if (drmIoctl(fb->fd, DRM_IOCTL_MODE_ADDFB2, &cmd))
     return EINA_FALSE;

   fb->id = cmd.fb_id;

   return EINA_TRUE;
}

#ifdef HAVE_ATOMIC_DRM
static int
_fb_atomic_flip(Ecore_Drm2_Output *output, Ecore_Drm2_Plane_State *pstate, uint32_t flags)
{
   int ret = 0;
   drmModeAtomicReq *req = NULL;
   Ecore_Drm2_Crtc_State *cstate;

   req = drmModeAtomicAlloc();
   if (!req) return -1;

   drmModeAtomicSetCursor(req, 0);

   cstate = output->crtc_state;

   ret = drmModeAtomicAddProperty(req, cstate->obj_id, cstate->mode.id,
                                  cstate->mode.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, cstate->obj_id, cstate->active.id,
                                  cstate->active.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, pstate->obj_id,
                                  pstate->cid.id, pstate->cid.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, pstate->obj_id,
                                  pstate->fid.id, pstate->fid.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, pstate->obj_id,
                                  pstate->sx.id, pstate->sx.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, pstate->obj_id,
                                  pstate->sy.id, pstate->sy.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, pstate->obj_id,
                                  pstate->sw.id, pstate->sw.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, pstate->obj_id,
                                  pstate->sh.id, pstate->sh.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, pstate->obj_id,
                                  pstate->cx.id, pstate->cx.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, pstate->obj_id,
                                  pstate->cy.id, pstate->cy.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, pstate->obj_id,
                                  pstate->cw.id, pstate->cw.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicAddProperty(req, pstate->obj_id,
                                  pstate->ch.id, pstate->ch.value);
   if (ret < 0) goto err;

   ret = drmModeAtomicCommit(output->fd, req, flags, output->user_data);
   if (ret < 0) ERR("Failed to commit Atomic FB Flip: %m");
   else ret = 0;

err:
   drmModeAtomicFree(req);
   return ret;
}
#endif

EAPI Ecore_Drm2_Fb *
ecore_drm2_fb_create(int fd, int width, int height, int depth, int bpp, unsigned int format)
{
   Ecore_Drm2_Fb *fb;
   struct drm_mode_create_dumb carg;
   struct drm_mode_destroy_dumb darg;
   struct drm_mode_map_dumb marg;
   int ret;

   EINA_SAFETY_ON_TRUE_RETURN_VAL((fd < 0), NULL);

   fb = calloc(1, sizeof(Ecore_Drm2_Fb));
   if (!fb) return NULL;

   fb->fd = fd;
   fb->w = width;
   fb->h = height;
   fb->bpp = bpp;
   fb->depth = depth;
   fb->format = format;

   memset(&carg, 0, sizeof(struct drm_mode_create_dumb));
   carg.bpp = bpp;
   carg.width = width;
   carg.height = height;

   ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &carg);
   if (ret) goto err;

   fb->hdl = carg.handle;
   fb->size = carg.size;
   fb->stride = carg.pitch;

   if (!_fb2_create(fb))
     {
        ret =
          drmModeAddFB(fd, width, height, depth, bpp,
                       fb->stride, fb->hdl, &fb->id);
        if (ret)
          {
             ERR("Could not add framebuffer: %m");
             goto add_err;
          }
     }

   memset(&marg, 0, sizeof(struct drm_mode_map_dumb));
   marg.handle = fb->hdl;
   ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &marg);
   if (ret)
     {
        ERR("Could not map framebuffer: %m");
        goto map_err;
     }

   fb->mmap = mmap(NULL, fb->size, PROT_WRITE, MAP_SHARED, fd, marg.offset);
   if (fb->mmap == MAP_FAILED)
     {
        ERR("Could not mmap framebuffer memory: %m");
        goto map_err;
     }

   return fb;

map_err:
   drmModeRmFB(fd, fb->id);
add_err:
   memset(&darg, 0, sizeof(struct drm_mode_destroy_dumb));
   darg.handle = fb->hdl;
   drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &darg);
err:
   free(fb);
   return NULL;
}

EAPI Ecore_Drm2_Fb *
ecore_drm2_fb_gbm_create(int fd, int width, int height, int depth, int bpp, unsigned int format, unsigned int handle, unsigned int stride, void *bo)
{
   struct drm_mode_map_dumb marg;
   Ecore_Drm2_Fb *fb;
   int ret;

   EINA_SAFETY_ON_TRUE_RETURN_VAL((fd < 0), NULL);

   fb = calloc(1, sizeof(Ecore_Drm2_Fb));
   if (!fb) return NULL;

   fb->gbm = EINA_TRUE;
   fb->gbm_bo = bo;

   fb->fd = fd;
   fb->w = width;
   fb->h = height;
   fb->bpp = bpp;
   fb->depth = depth;
   fb->format = format;
   fb->stride = stride;
   fb->size = fb->stride * fb->h;
   fb->hdl = handle;

   if (!_fb2_create(fb))
     {
        if (drmModeAddFB(fd, width, height, depth, bpp,
                         fb->stride, fb->hdl, &fb->id))
          {
             ERR("Could not add framebuffer: %m");
             goto err;
          }
     }

   /* mmap it if we can so screenshots are easy */
   memset(&marg, 0, sizeof(struct drm_mode_map_dumb));
   marg.handle = fb->hdl;
   ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &marg);
   if (!ret)
     {
        fb->mmap = mmap(NULL, fb->size, PROT_WRITE, MAP_SHARED, fd, marg.offset);
        if (fb->mmap == MAP_FAILED) fb->mmap = NULL;
     }
   return fb;

err:
   free(fb);
   return NULL;
}

EAPI void
ecore_drm2_fb_destroy(Ecore_Drm2_Fb *fb)
{
   EINA_SAFETY_ON_NULL_RETURN(fb);

   if (fb->mmap) munmap(fb->mmap, fb->size);

   if (fb->id) drmModeRmFB(fb->fd, fb->id);

   if (!fb->gbm)
     {
        struct drm_mode_destroy_dumb darg;

        memset(&darg, 0, sizeof(struct drm_mode_destroy_dumb));
        darg.handle = fb->hdl;
        drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &darg);
     }

   free(fb);
}

EAPI void *
ecore_drm2_fb_data_get(Ecore_Drm2_Fb *fb)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(fb, NULL);
   return fb->mmap;
}

EAPI unsigned int
ecore_drm2_fb_size_get(Ecore_Drm2_Fb *fb)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(fb, 0);
   return fb->size;
}

EAPI unsigned int
ecore_drm2_fb_stride_get(Ecore_Drm2_Fb *fb)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(fb, 0);
   return fb->stride;
}

EAPI void
ecore_drm2_fb_dirty(Ecore_Drm2_Fb *fb, Eina_Rectangle *rects, unsigned int count)
{
   EINA_SAFETY_ON_NULL_RETURN(fb);
   EINA_SAFETY_ON_NULL_RETURN(rects);

#ifdef DRM_MODE_FEATURE_DIRTYFB
   drmModeClip *clip;
   unsigned int i = 0;
   int ret;

   clip = alloca(count * sizeof(drmModeClip));
   for (i = 0; i < count; i++)
     {
        clip[i].x1 = rects[i].x;
        clip[i].y1 = rects[i].y;
        clip[i].x2 = rects[i].w;
        clip[i].y2 = rects[i].h;
     }

   ret = drmModeDirtyFB(fb->fd, fb->id, clip, count);
   if ((ret) && (ret == -EINVAL))
     WRN("Could not mark framebuffer as dirty: %m");
#endif
}

static void
_release_buffer(Ecore_Drm2_Output *output, Ecore_Drm2_Fb *b)
{
   b->busy = EINA_FALSE;
   if (output->release_cb) output->release_cb(output->release_data, b);
}

EAPI Eina_Bool
ecore_drm2_fb_flip_complete(Ecore_Drm2_Output *output)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(output, EINA_FALSE);

   if (output->current && (output->current != output->pending))
     _release_buffer(output, output->current);
   output->current = output->pending;
   output->pending = NULL;

   return !!output->next;
}

EAPI int
ecore_drm2_fb_flip(Ecore_Drm2_Fb *fb, Ecore_Drm2_Output *output)
{
   int ret = 0;

   EINA_SAFETY_ON_NULL_RETURN_VAL(output, -1);
   EINA_SAFETY_ON_NULL_RETURN_VAL(output->current_mode, -1);

   if (!output->enabled) return -1;

   if (output->pending)
     {
        if (output->next) _release_buffer(output, output->next);
        output->next = fb;
        if (output->next) output->next->busy = EINA_TRUE;
        return 0;
     }
   if (!fb) fb = output->next;

   /* So we can generate a tick by flipping to the current fb */
   if (!fb) fb = output->current;

   if (output->next)
     {
        output->next->busy = EINA_FALSE;
        output->next = NULL;
     }

   /* If we don't have an fb to set by now, BAIL! */
   if (!fb) return -1;

#ifdef HAVE_ATOMIC_DRM
   if (_ecore_drm2_use_atomic)
     {
        Ecore_Drm2_Plane_State *pstate;
        uint32_t flags =
          DRM_MODE_ATOMIC_NONBLOCK | DRM_MODE_PAGE_FLIP_EVENT |
          DRM_MODE_ATOMIC_ALLOW_MODESET;

        pstate = output->plane_state;

        pstate->cid.value = output->crtc_id;
        pstate->fid.value = fb->id;

        pstate->sx.value = 0;
        pstate->sy.value = 0;
        pstate->sw.value = fb->w << 16;
        pstate->sh.value = fb->h << 16;
        pstate->cx.value = output->x;
        pstate->cy.value = output->y;
        pstate->cw.value = output->current_mode->width;
        pstate->ch.value = output->current_mode->height;

        ret = _fb_atomic_flip(output, pstate, flags);
        if ((ret < 0) && (errno != EBUSY))
          {
             ERR("Atomic Pageflip Failed for Crtc %u on Connector %u: %m",
                 output->crtc_id, output->conn_id);
             return ret;
          }
        else if (ret < 0)
          {
             output->next = fb;
             if (output->next) output->next->busy = EINA_TRUE;

             return 0;
          }

        output->pending = fb;
        output->pending->busy = EINA_TRUE;

        return 0;
     }
   else
#endif
     {
        if ((!output->current) ||
            (output->current->stride != fb->stride))
          {
             ret =
               drmModeSetCrtc(fb->fd, output->crtc_id, fb->id,
                              output->x, output->y, &output->conn_id, 1,
                              &output->current_mode->info);
             if (ret)
               {
                  ERR("Failed to set Mode %dx%d for Output %s: %m",
                      output->current_mode->width, output->current_mode->height,
                      output->name);
                  return ret;
               }

             if (output->current) _release_buffer(output, output->current);
             output->current = fb;
             output->current->busy = EINA_TRUE;
             output->next = NULL;

             return 0;
          }

        ret =
          drmModePageFlip(fb->fd, output->crtc_id, fb->id,
                          DRM_MODE_PAGE_FLIP_EVENT, output->user_data);
        if ((ret < 0) && (errno != EBUSY))
          {
             DBG("Pageflip Failed for Crtc %u on Connector %u: %m",
                 output->crtc_id, output->conn_id);
             return ret;
          }
        else if (ret < 0)
          {
             output->next = fb;
             output->next->busy = EINA_TRUE;
             return 0;
          }

        output->pending = fb;
        output->pending->busy = EINA_TRUE;
        return 0;
     }
}

EAPI Eina_Bool
ecore_drm2_fb_busy_get(Ecore_Drm2_Fb *fb)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(fb, EINA_FALSE);
   return fb->busy;
}

EAPI void
ecore_drm2_fb_busy_set(Ecore_Drm2_Fb *fb, Eina_Bool busy)
{
   EINA_SAFETY_ON_NULL_RETURN(fb);
   fb->busy = busy;
}

EAPI void
ecore_drm2_fb_release(Ecore_Drm2_Output *o)
{
   EINA_SAFETY_ON_NULL_RETURN(o);

   if (o->next)
     {
        _release_buffer(o, o->next);
        o->next = NULL;
        return;
     }

   WRN("Buffer release request when no next buffer");
   /* If we have to release these we're going to see tearing.
    * Try to reclaim in decreasing order of visual awfulness
    */
   if (o->current)
     {
        _release_buffer(o, o->current);
        o->current = NULL;
        return;
     }

   if (o->pending)
     {
        _release_buffer(o, o->pending);
        o->pending = NULL;
        return;
     }
}

EAPI void *
ecore_drm2_fb_bo_get(Ecore_Drm2_Fb *fb)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(fb, NULL);
   return fb->gbm_bo;
}