]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/omapdrmtest.git/commitdiff
add video buffer lock/unlock API
authorRob Clark <rob@ti.com>
Thu, 26 Jan 2012 20:24:13 +0000 (14:24 -0600)
committerRob Clark <rob@ti.com>
Thu, 26 Jan 2012 20:24:17 +0000 (14:24 -0600)
Intended to make life easier for clients like video decode test, which
needs a pool of buffers it can allocate from, and later free back to.

util/list.h [new file with mode: 0644]
util/util.c
util/util.h

diff --git a/util/list.h b/util/list.h
new file mode 100644 (file)
index 0000000..3534773
--- /dev/null
@@ -0,0 +1,314 @@
+/*
+ * Copyright © 2010 Intel Corporation
+ * Copyright © 2010 Francisco Jerez <currojerez@riseup.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#ifndef _LIST_H_
+#define _LIST_H_
+
+/**
+ * @file Classic doubly-link circular list implementation.
+ * For real usage examples of the linked list, see the file test/list.c
+ *
+ * Example:
+ * We need to keep a list of struct foo in the parent struct bar, i.e. what
+ * we want is something like this.
+ *
+ *     struct bar {
+ *          ...
+ *          struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{}
+ *          ...
+ *     }
+ *
+ * We need one list head in bar and a list element in all list_of_foos (both are of
+ * data type 'struct list').
+ *
+ *     struct bar {
+ *          ...
+ *          struct list list_of_foos;
+ *          ...
+ *     }
+ *
+ *     struct foo {
+ *          ...
+ *          struct list entry;
+ *          ...
+ *     }
+ *
+ * Now we initialize the list head:
+ *
+ *     struct bar bar;
+ *     ...
+ *     list_init(&bar.list_of_foos);
+ *
+ * Then we create the first element and add it to this list:
+ *
+ *     struct foo *foo = malloc(...);
+ *     ....
+ *     list_add(&foo->entry, &bar.list_of_foos);
+ *
+ * Repeat the above for each element you want to add to the list. Deleting
+ * works with the element itself.
+ *      list_del(&foo->entry);
+ *      free(foo);
+ *
+ * Note: calling list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty
+ * list again.
+ *
+ * Looping through the list requires a 'struct foo' as iterator and the
+ * name of the field the subnodes use.
+ *
+ * struct foo *iterator;
+ * list_for_each_entry(iterator, &bar.list_of_foos, entry) {
+ *      if (iterator->something == ...)
+ *             ...
+ * }
+ *
+ * Note: You must not call list_del() on the iterator if you continue the
+ * loop. You need to run the safe for-each loop instead:
+ *
+ * struct foo *iterator, *next;
+ * list_for_each_entry_safe(iterator, next, &bar.list_of_foos, entry) {
+ *      if (...)
+ *              list_del(&iterator->entry);
+ * }
+ *
+ */
+
+/**
+ * The linkage struct for list nodes. This struct must be part of your
+ * to-be-linked struct. struct list is required for both the head of the
+ * list and for each list node.
+ *
+ * Position and name of the struct list field is irrelevant.
+ * There are no requirements that elements of a list are of the same type.
+ * There are no requirements for a list head, any struct list can be a list
+ * head.
+ */
+struct list {
+    struct list *next, *prev;
+};
+
+/**
+ * Initialize the list as an empty list.
+ *
+ * Example:
+ * list_init(&bar->list_of_foos);
+ *
+ * @param The list to initialized.
+ */
+static void
+list_init(struct list *list)
+{
+    list->next = list->prev = list;
+}
+
+static inline void
+__list_add(struct list *entry,
+           struct list *prev,
+           struct list *next)
+{
+    next->prev = entry;
+    entry->next = next;
+    entry->prev = prev;
+    prev->next = entry;
+}
+
+/**
+ * Insert a new element after the given list head. The new element does not
+ * need to be initialised as empty list.
+ * The list changes from:
+ *      head → some element → ...
+ * to
+ *      head → new element → older element → ...
+ *
+ * Example:
+ * struct foo *newfoo = malloc(...);
+ * list_add(&newfoo->entry, &bar->list_of_foos);
+ *
+ * @param entry The new element to prepend to the list.
+ * @param head The existing list.
+ */
+static inline void
+list_add(struct list *entry, struct list *head)
+{
+    __list_add(entry, head, head->next);
+}
+
+/**
+ * Append a new element to the end of the list given with this list head.
+ *
+ * The list changes from:
+ *      head → some element → ... → lastelement
+ * to
+ *      head → some element → ... → lastelement → new element
+ *
+ * Example:
+ * struct foo *newfoo = malloc(...);
+ * list_append(&newfoo->entry, &bar->list_of_foos);
+ *
+ * @param entry The new element to prepend to the list.
+ * @param head The existing list.
+ */
+static inline void
+list_append(struct list *entry, struct list *head)
+{
+    __list_add(entry, head->prev, head);
+}
+
+
+static inline void
+__list_del(struct list *prev, struct list *next)
+{
+    next->prev = prev;
+    prev->next = next;
+}
+
+/**
+ * Remove the element from the list it is in. Using this function will reset
+ * the pointers to/from this element so it is removed from the list. It does
+ * NOT free the element itself or manipulate it otherwise.
+ *
+ * Using list_del on a pure list head (like in the example at the top of
+ * this file) will NOT remove the first element from
+ * the list but rather reset the list as empty list.
+ *
+ * Example:
+ * list_del(&foo->entry);
+ *
+ * @param entry The element to remove.
+ */
+static inline void
+list_del(struct list *entry)
+{
+    __list_del(entry->prev, entry->next);
+    list_init(entry);
+}
+
+/**
+ * Check if the list is empty.
+ *
+ * Example:
+ * list_is_empty(&bar->list_of_foos);
+ *
+ * @return True if the list contains one or more elements or False otherwise.
+ */
+static inline bool
+list_is_empty(struct list *head)
+{
+    return head->next == head;
+}
+
+/**
+ * Returns a pointer to the container of this list element.
+ *
+ * Example:
+ * struct foo* f;
+ * f = container_of(&foo->entry, struct foo, entry);
+ * assert(f == foo);
+ *
+ * @param ptr Pointer to the struct list.
+ * @param type Data type of the list element.
+ * @param member Member name of the struct list field in the list element.
+ * @return A pointer to the data struct containing the list head.
+ */
+#ifndef container_of
+#define container_of(ptr, type, member) \
+    (type *)((char *)(ptr) - (char *) &((type *)0)->member)
+#endif
+
+/**
+ * Alias of container_of
+ */
+#define list_entry(ptr, type, member) \
+    container_of(ptr, type, member)
+
+/**
+ * Retrieve the first list entry for the given list pointer.
+ *
+ * Example:
+ * struct foo *first;
+ * first = list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
+ *
+ * @param ptr The list head
+ * @param type Data type of the list element to retrieve
+ * @param member Member name of the struct list field in the list element.
+ * @return A pointer to the first list element.
+ */
+#define list_first_entry(ptr, type, member) \
+    list_entry((ptr)->next, type, member)
+
+/**
+ * Retrieve the last list entry for the given listpointer.
+ *
+ * Example:
+ * struct foo *first;
+ * first = list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
+ *
+ * @param ptr The list head
+ * @param type Data type of the list element to retrieve
+ * @param member Member name of the struct list field in the list element.
+ * @return A pointer to the last list element.
+ */
+#define list_last_entry(ptr, type, member) \
+    list_entry((ptr)->prev, type, member)
+
+#define __container_of(ptr, sample, member)                            \
+    (void *)((char *)(ptr)                                             \
+            - ((char *)&(sample)->member - (char *)(sample)))
+/**
+ * Loop through the list given by head and set pos to struct in the list.
+ *
+ * Example:
+ * struct foo *iterator;
+ * list_for_each_entry(iterator, &bar->list_of_foos, entry) {
+ *      [modify iterator]
+ * }
+ *
+ * This macro is not safe for node deletion. Use list_for_each_entry_safe
+ * instead.
+ *
+ * @param pos Iterator variable of the type of the list elements.
+ * @param head List head
+ * @param member Member name of the struct list in the list elements.
+ *
+ */
+#define list_for_each_entry(pos, head, member)                         \
+    for (pos = __container_of((head)->next, pos, member);              \
+        &pos->member != (head);                                        \
+        pos = __container_of(pos->member.next, pos, member))
+
+/**
+ * Loop through the list, keeping a backup pointer to the element. This
+ * macro allows for the deletion of a list element while looping through the
+ * list.
+ *
+ * See list_for_each_entry for more details.
+ */
+#define list_for_each_entry_safe(pos, tmp, head, member)               \
+    for (pos = __container_of((head)->next, pos, member),              \
+        tmp = __container_of(pos->member.next, pos, member);           \
+        &pos->member != (head);                                        \
+        pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
+
+#endif
index 92f9a0c302285b7e8abfe9f9a3cccccaaec5622e..c9a8e4acc39a76e07affa4e137342b5a7bcca6b7 100644 (file)
@@ -56,6 +56,49 @@ disp_open(int argc, char **argv)
        return disp;
 }
 
        return disp;
 }
 
+struct buffer **
+disp_get_vid_buffers(struct display *disp, uint32_t n,
+               uint32_t fourcc, uint32_t w, uint32_t h)
+{
+       struct buffer **buffers;
+       int i;
+
+       buffers = disp->get_vid_buffers(disp, n, fourcc, w, h);
+       if (buffers) {
+               /* if allocation succeeded, store in the unlocked
+                * video buffer list
+                */
+               list_init(&disp->unlocked);
+               for (i = 0; i < n; i++)
+                       list_add(&buffers[i]->unlocked, &disp->unlocked);
+       }
+
+       return buffers;
+}
+
+struct buffer *
+disp_get_vid_buffer(struct display *disp)
+{
+       struct buffer *buf = NULL;
+       if (!list_is_empty(&disp->unlocked)) {
+               buf = list_last_entry(&disp->unlocked, struct buffer, unlocked);
+               list_del(&buf->unlocked);
+
+               /* barrier.. if we are using GPU blitting, we need to make sure
+                * that the GPU is finished:
+                */
+               omap_bo_cpu_prep(buf->bo[0], OMAP_GEM_WRITE);
+               omap_bo_cpu_fini(buf->bo[0], OMAP_GEM_WRITE);
+       }
+       return buf;
+}
+
+void
+disp_put_vid_buffer(struct display *disp, struct buffer *buf)
+{
+       list_add(&buf->unlocked, &disp->unlocked);
+}
+
 struct buffer *
 disp_get_fb(struct display *disp)
 {
 struct buffer *
 disp_get_fb(struct display *disp)
 {
index b05b526c017772e7bf0a70a2b6535d61a6d5b082..720c11b802cb77f5dddf2a36d00e8095ec62d6eb 100644 (file)
 #include <omap_drm.h>
 #include <omap_drmif.h>
 
 #include <omap_drm.h>
 #include <omap_drmif.h>
 
+typedef enum {
+       false = 0,
+       true = 1
+} bool;
+
+#include "list.h"
 
 /* Display Interface:
  *
 
 /* Display Interface:
  *
@@ -44,12 +50,14 @@ struct buffer {
        int nbo;
        struct omap_bo *bo[4];
        uint32_t pitches[4];
        int nbo;
        struct omap_bo *bo[4];
        uint32_t pitches[4];
+       struct list unlocked;
 };
 
 struct display {
        int fd;
        uint32_t width, height;
        struct omap_device *dev;
 };
 
 struct display {
        int fd;
        uint32_t width, height;
        struct omap_device *dev;
+       struct list unlocked;
 
        struct buffer ** (*get_buffers)(struct display *disp, uint32_t n);
        struct buffer ** (*get_vid_buffers)(struct display *disp,
 
        struct buffer ** (*get_buffers)(struct display *disp, uint32_t n);
        struct buffer ** (*get_vid_buffers)(struct display *disp,
@@ -75,12 +83,8 @@ disp_get_buffers(struct display *disp, uint32_t n)
 }
 
 /* Get video/overlay buffers (ie. can be YUV, scaled, etc) */
 }
 
 /* Get video/overlay buffers (ie. can be YUV, scaled, etc) */
-static inline struct buffer **
-disp_get_vid_buffers(struct display *disp, uint32_t n,
-               uint32_t fourcc, uint32_t w, uint32_t h)
-{
-       return disp->get_vid_buffers(disp, n, fourcc, w, h);
-}
+struct buffer ** disp_get_vid_buffers(struct display *disp, uint32_t n,
+               uint32_t fourcc, uint32_t w, uint32_t h);
 
 /* flip to / post the specified buffer */
 static inline int
 
 /* flip to / post the specified buffer */
 static inline int
@@ -97,6 +101,11 @@ disp_post_vid_buffer(struct display *disp, struct buffer *buf,
        return disp->post_vid_buffer(disp, buf, x, y, w, h);
 }
 
        return disp->post_vid_buffer(disp, buf, x, y, w, h);
 }
 
+/* allocate a buffer from pool created by disp_get_vid_buffers() */
+struct buffer * disp_get_vid_buffer(struct display *disp);
+/* free to video buffer pool */
+void disp_put_vid_buffer(struct display *disp, struct buffer *buf);
+
 /* helper to setup the display for apps that just need video with
  * no flipchain on the GUI layer
  */
 /* helper to setup the display for apps that just need video with
  * no flipchain on the GUI layer
  */
@@ -147,9 +156,27 @@ void fill(struct buffer *buf, int i);
     (type *)((char *)(ptr) - (char *) &((type *)0)->member)
 #endif
 
     (type *)((char *)(ptr) - (char *) &((type *)0)->member)
 #endif
 
-typedef enum {
-       false = 0,
-       true = 1
-} bool;
+#ifndef MIN
+#  define MIN(a,b)     (((a) < (b)) ? (a) : (b))
+#endif
+
+#ifndef MAX
+#  define MAX(a,b)     (((a) > (b)) ? (a) : (b))
+#endif
+
+/* align x to next highest multiple of 2^n */
+#define ALIGN2(x,n)   (((x) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
+
+#include <sys/time.h>
+static inline long
+mark(long *last)
+{
+       struct timeval t;
+       gettimeofday(&t, NULL);
+       if (last) {
+               return t.tv_usec - *last;
+       }
+       return t.tv_usec;
+}
 
 #endif /* UTIL_H_ */
 
 #endif /* UTIL_H_ */