OSDN Git Service

Add basic CONTRIBUTING file
[android-x86/external-libdrm.git] / xf86drmMode.c
index fa21986..9a15b5e 100644 (file)
  */
 
 /*
- * TODO the types we are after are defined in diffrent headers on diffrent
+ * TODO the types we are after are defined in different headers on different
  * platforms find which headers to include to get uint32_t
  */
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
 #include <limits.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -114,7 +110,6 @@ void drmModeFreeResources(drmModeResPtr ptr)
        drmFree(ptr->connectors);
        drmFree(ptr->encoders);
        drmFree(ptr);
-
 }
 
 void drmModeFreeFB(drmModeFBPtr ptr)
@@ -132,7 +127,6 @@ void drmModeFreeCrtc(drmModeCrtcPtr ptr)
                return;
 
        drmFree(ptr);
-
 }
 
 void drmModeFreeConnector(drmModeConnectorPtr ptr)
@@ -145,7 +139,6 @@ void drmModeFreeConnector(drmModeConnectorPtr ptr)
        drmFree(ptr->props);
        drmFree(ptr->modes);
        drmFree(ptr);
-
 }
 
 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
@@ -252,7 +245,7 @@ err_allocs:
 }
 
 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
-                 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
+                uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
                 uint32_t *buf_id)
 {
        struct drm_mode_fb_cmd f;
@@ -273,10 +266,10 @@ int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
        return 0;
 }
 
-int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
-                 uint32_t pixel_format, uint32_t bo_handles[4],
-                 uint32_t pitches[4], uint32_t offsets[4],
-                 uint32_t *buf_id, uint32_t flags)
+int drmModeAddFB2WithModifiers(int fd, uint32_t width, uint32_t height,
+                               uint32_t pixel_format, const uint32_t bo_handles[4],
+                               const uint32_t pitches[4], const uint32_t offsets[4],
+                               const uint64_t modifier[4], uint32_t *buf_id, uint32_t flags)
 {
        struct drm_mode_fb_cmd2 f;
        int ret;
@@ -289,6 +282,8 @@ int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
        memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
        memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
        memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
+       if (modifier)
+               memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
 
        if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
                return ret;
@@ -297,6 +292,17 @@ int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
        return 0;
 }
 
+int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
+                  uint32_t pixel_format, const uint32_t bo_handles[4],
+                  const uint32_t pitches[4], const uint32_t offsets[4],
+                  uint32_t *buf_id, uint32_t flags)
+{
+       return drmModeAddFB2WithModifiers(fd, width, height,
+                                         pixel_format, bo_handles,
+                                         pitches, offsets, NULL,
+                                         buf_id, flags);
+}
+
 int drmModeRmFB(int fd, uint32_t bufferId)
 {
        return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
@@ -340,7 +346,6 @@ int drmModeDirtyFB(int fd, uint32_t bufferId,
        return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
 }
 
-
 /*
  * Crtc functions
  */
@@ -377,9 +382,8 @@ drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
        return r;
 }
 
-
 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
-                   uint32_t x, uint32_t y, uint32_t *connectors, int count,
+                  uint32_t x, uint32_t y, uint32_t *connectors, int count,
                   drmModeModeInfoPtr mode)
 {
        struct drm_mode_crtc crtc;
@@ -480,12 +484,13 @@ _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
 {
        struct drm_mode_get_connector conn, counts;
        drmModeConnectorPtr r = NULL;
+       struct drm_mode_modeinfo stack_mode;
 
        memclear(conn);
        conn.connector_id = connector_id;
        if (!probe) {
                conn.count_modes = 1;
-               conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
+               conn.modes_ptr = VOID2U64(&stack_mode);
        }
 
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
@@ -509,7 +514,7 @@ retry:
                        goto err_allocs;
        } else {
                conn.count_modes = 1;
-               conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
+               conn.modes_ptr = VOID2U64(&stack_mode);
        }
 
        if (conn.count_encoders) {
@@ -530,7 +535,8 @@ retry:
            counts.count_encoders < conn.count_encoders) {
                drmFree(U642VOID(conn.props_ptr));
                drmFree(U642VOID(conn.prop_values_ptr));
-               drmFree(U642VOID(conn.modes_ptr));
+               if (U642VOID(conn.modes_ptr) != &stack_mode)
+                       drmFree(U642VOID(conn.modes_ptr));
                drmFree(U642VOID(conn.encoders_ptr));
 
                goto retry;
@@ -572,7 +578,8 @@ retry:
 err_allocs:
        drmFree(U642VOID(conn.prop_values_ptr));
        drmFree(U642VOID(conn.props_ptr));
-       drmFree(U642VOID(conn.modes_ptr));
+       if (U642VOID(conn.modes_ptr) != &stack_mode)
+               drmFree(U642VOID(conn.modes_ptr));
        drmFree(U642VOID(conn.encoders_ptr));
 
        return r;
@@ -610,7 +617,6 @@ int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_inf
        return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
 }
 
-
 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
 {
        struct drm_mode_get_property prop;
@@ -821,9 +827,25 @@ int drmCheckModesettingSupported(const char *busid)
        }
 #elif defined(__DragonFly__)
        return 0;
+#elif defined(__OpenBSD__)
+       int     fd;
+       struct drm_mode_card_res res;
+       drmModeResPtr r = 0;
+
+       if ((fd = drmOpen(NULL, busid)) < 0)
+               return -EINVAL;
+
+       memset(&res, 0, sizeof(struct drm_mode_card_res));
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
+               drmClose(fd);
+               return -errno;
+       }
+
+       drmClose(fd);
+       return 0;
 #endif
        return -ENOSYS;
-
 }
 
 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
@@ -862,7 +884,9 @@ int drmHandleEvent(int fd, drmEventContextPtr evctx)
        int len, i;
        struct drm_event *e;
        struct drm_event_vblank *vblank;
-       
+       struct drm_event_crtc_sequence *seq;
+       void *user_data;
+
        /* The DRM read semantics guarantees that we always get only
         * complete events. */
 
@@ -874,7 +898,7 @@ int drmHandleEvent(int fd, drmEventContextPtr evctx)
 
        i = 0;
        while (i < len) {
-               e = (struct drm_event *) &buffer[i];
+               e = (struct drm_event *)(buffer + i);
                switch (e->type) {
                case DRM_EVENT_VBLANK:
                        if (evctx->version < 1 ||
@@ -882,21 +906,36 @@ int drmHandleEvent(int fd, drmEventContextPtr evctx)
                                break;
                        vblank = (struct drm_event_vblank *) e;
                        evctx->vblank_handler(fd,
-                                             vblank->sequence, 
+                                             vblank->sequence,
                                              vblank->tv_sec,
                                              vblank->tv_usec,
                                              U642VOID (vblank->user_data));
                        break;
                case DRM_EVENT_FLIP_COMPLETE:
-                       if (evctx->version < 2 ||
-                           evctx->page_flip_handler == NULL)
-                               break;
                        vblank = (struct drm_event_vblank *) e;
-                       evctx->page_flip_handler(fd,
-                                                vblank->sequence,
-                                                vblank->tv_sec,
-                                                vblank->tv_usec,
-                                                U642VOID (vblank->user_data));
+                       user_data = U642VOID (vblank->user_data);
+
+                       if (evctx->version >= 3 && evctx->page_flip_handler2)
+                               evctx->page_flip_handler2(fd,
+                                                        vblank->sequence,
+                                                        vblank->tv_sec,
+                                                        vblank->tv_usec,
+                                                        vblank->crtc_id,
+                                                        user_data);
+                       else if (evctx->version >= 2 && evctx->page_flip_handler)
+                               evctx->page_flip_handler(fd,
+                                                        vblank->sequence,
+                                                        vblank->tv_sec,
+                                                        vblank->tv_usec,
+                                                        user_data);
+                       break;
+               case DRM_EVENT_CRTC_SEQUENCE:
+                       seq = (struct drm_event_crtc_sequence *) e;
+                       if (evctx->version >= 4 && evctx->sequence_handler)
+                               evctx->sequence_handler(fd,
+                                                       seq->sequence,
+                                                       seq->time_ns,
+                                                       seq->user_data);
                        break;
                default:
                        break;
@@ -921,13 +960,28 @@ int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
        return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
 }
 
+int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
+                         uint32_t flags, void *user_data,
+                         uint32_t target_vblank)
+{
+       struct drm_mode_crtc_page_flip_target flip_target;
+
+       memclear(flip_target);
+       flip_target.fb_id = fb_id;
+       flip_target.crtc_id = crtc_id;
+       flip_target.user_data = VOID2U64(user_data);
+       flip_target.flags = flags;
+       flip_target.sequence = target_vblank;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
+}
+
 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
                    uint32_t fb_id, uint32_t flags,
                    int32_t crtc_x, int32_t crtc_y,
                    uint32_t crtc_w, uint32_t crtc_h,
                    uint32_t src_x, uint32_t src_y,
                    uint32_t src_w, uint32_t src_h)
-
 {
        struct drm_mode_set_plane s;
 
@@ -948,7 +1002,6 @@ int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
        return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
 }
 
-
 drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
 {
        struct drm_mode_get_plane ovr, counts;
@@ -1180,6 +1233,9 @@ drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
 {
        drmModeAtomicReqPtr new;
 
+       if (!old)
+               return NULL;
+
        new = drmMalloc(sizeof *new);
        if (!new)
                return NULL;
@@ -1204,6 +1260,9 @@ drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old)
 
 int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
 {
+       if (!base)
+               return -EINVAL;
+
        if (!augment || augment->cursor == 0)
                return 0;
 
@@ -1230,12 +1289,15 @@ int drmModeAtomicMerge(drmModeAtomicReqPtr base, drmModeAtomicReqPtr augment)
 
 int drmModeAtomicGetCursor(drmModeAtomicReqPtr req)
 {
+       if (!req)
+               return -EINVAL;
        return req->cursor;
 }
 
 void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
 {
-       req->cursor = cursor;
+       if (req)
+               req->cursor = cursor;
 }
 
 int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
@@ -1243,6 +1305,12 @@ int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
                             uint32_t property_id,
                             uint64_t value)
 {
+       if (!req)
+               return -EINVAL;
+
+       if (object_id == 0 || property_id == 0)
+               return -EINVAL;
+
        if (req->cursor >= req->size_items) {
                drmModeAtomicReqItemPtr new;
 
@@ -1300,6 +1368,9 @@ int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, uint32_t flags,
        int obj_idx = -1;
        int ret = -1;
 
+       if (!req)
+               return -EINVAL;
+
        if (req->cursor == 0)
                return 0;
 
@@ -1421,3 +1492,92 @@ drmModeDestroyPropertyBlob(int fd, uint32_t id)
        destroy.blob_id = id;
        return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
 }
+
+int
+drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags, uint32_t *lessee_id)
+{
+       struct drm_mode_create_lease create;
+       int ret;
+
+       memclear(create);
+       create.object_ids = (uintptr_t) objects;
+       create.object_count = num_objects;
+       create.flags = flags;
+
+       ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create);
+       if (ret == 0) {
+               *lessee_id = create.lessee_id;
+               return create.fd;
+       }
+       return -errno;
+}
+
+drmModeLesseeListPtr
+drmModeListLessees(int fd)
+{
+       struct drm_mode_list_lessees list;
+       uint32_t count;
+       drmModeLesseeListPtr ret;
+
+       memclear(list);
+
+       if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list))
+               return NULL;
+
+       count = list.count_lessees;
+       ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0]));
+       if (!ret)
+               return NULL;
+
+       list.lessees_ptr = VOID2U64(&ret->lessees[0]);
+       if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) {
+               drmFree(ret);
+               return NULL;
+       }
+
+       ret->count = count;
+       return ret;
+}
+
+drmModeObjectListPtr
+drmModeGetLease(int fd)
+{
+       struct drm_mode_get_lease get;
+       uint32_t count;
+       drmModeObjectListPtr ret;
+
+       memclear(get);
+
+       if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get))
+               return NULL;
+
+       count = get.count_objects;
+       ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0]));
+       if (!ret)
+               return NULL;
+
+       get.objects_ptr = VOID2U64(&ret->objects[0]);
+       if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) {
+               drmFree(ret);
+               return NULL;
+       }
+
+       ret->count = count;
+       return ret;
+}
+
+int
+drmModeRevokeLease(int fd, uint32_t lessee_id)
+{
+       struct drm_mode_revoke_lease revoke;
+       int ret;
+
+       memclear(revoke);
+
+       revoke.lessee_id = lessee_id;
+
+       ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke);
+       if (ret == 0)
+               return 0;
+       return -errno;
+}