1 /**************************************************************************
5 Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8 Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 **************************************************************************/
32 #ifndef _TBM_BUFMGR_H_
33 #define _TBM_BUFMGR_H_
40 * \brief Tizen Buffer Manager
44 * @brief Definition for the tizen buffer manager
47 typedef struct _tbm_bufmgr * tbm_bufmgr;
50 * @brief Definition for the tizen buffer object
53 typedef struct _tbm_bo *tbm_bo;
55 * @brief Definition for the key associated with the buffer object
58 typedef uint32_t tbm_key;
60 * @brief Definition for the file descripter of the system buffer manager
63 typedef int32_t tbm_fd;
69 * @brief Definition for the device type to get the default handle
72 #define TBM_DEVICE_DEFAULT 0
74 * @brief Definition for the device type to get the virtual memory
77 #define TBM_DEVICE_CPU 1
79 * @brief Definition for the device type to get the 2D memory handle
82 #define TBM_DEVICE_2D 2
84 * @brief Definition for the device type to get the 3D memory handle
87 #define TBM_DEVICE_3D 3
89 * @brief Definition for the device type to get the multimedia handle
92 #define TBM_DEVICE_MM 4
95 * @brief Definition for the cache invalidate
98 #define TBM_CACHE_INV 0x01
100 * @brief Definition for the cache clean
103 #define TBM_CACHE_CLN 0x02
108 * @brief Definition for the access option to read
111 #define TBM_OPTION_READ (1 << 0)
113 * @brief Definition for the access option to write
116 #define TBM_OPTION_WRITE (1 << 1)
118 * @brief Definition for the vendor specific option that depends on the backend
121 #define TBM_OPTION_VENDOR (0xffff0000)
124 * @brief tbm_bo_handle abstraction of the memory handle by TBM_DEVICE_TYPE
127 typedef union _tbm_bo_handle
137 * @brief Enumeration of bo memory type
142 TBM_BO_DEFAULT = 0, /**< default memory: it depends on the backend */
143 TBM_BO_SCANOUT = (1<<0), /**< scanout memory */
144 TBM_BO_NONCACHABLE = (1<<1), /**< non-cachable memory */
145 TBM_BO_WC = (1<<2), /**< write-combine memory */
146 TBM_BO_VENDOR = (0xffff0000), /**< vendor specific memory: it depends on the backend */
153 /* Functions for buffer manager */
156 * @brief Initializes the buffer manager.
157 * @details If fd is lower than zero, fd is get drm fd in tbm_bufmgr_init function\n
158 * The user can decide the lock type and cache flush type with the environment variables, which are BUFMGR_LOCK_TYPE and BUFMGR_MAP_CACHE.\n
160 * BUFMGR_LOCK default is once\n
161 * once : The previous bo which is locked is unlock when the new bo is trying to be locked\n
162 * always : The new bo is locked until the previous bo which is locked is unlocked\n
163 * never : Every bo is never locked.\n
165 * BUFMGR_MAP_CACHE default is true\n
166 * true : use map cache flushing\n
167 * false : to use map cache flushing
169 * @param[in] fd : file descripter of the system buffer manager
170 * @return a buffer manager
171 * @retval #tbm_bufmgr
172 * @see tbm_bufmgr_deinit();
175 #include <tbm_bufmgr.h>
179 setenv("BUFMGR_LOCK_TYPE", "once", 1);
180 setenv("BUFMGR_MAP_CACHE", "true", 1);
183 bufmgr = tbm_bufmgr_init (bufmgr_fd);
187 tbm_bufmgr_deinit (bufmgr);
190 tbm_bufmgr tbm_bufmgr_init (int fd);
193 * @brief Deinitializes the buffer manager.
195 * @param[in] bufmgr : the buffer manager
196 * @see tbm_bufmgr_init()
199 #include <tbm_bufmgr.h>
203 bufmgr = tbm_bufmgr_init (bufmgr_fd);
207 tbm_bufmgr_deinit (bufmgr);
210 void tbm_bufmgr_deinit (tbm_bufmgr bufmgr);
212 /* Functions for bo */
215 * @brief Allocates the buffer object.
216 * @details This function create tbm_bo and set reference count to 1.\n
217 * The user can craete tbm_bo with memory type flag #TBM_BO_FLAGS\n\n
218 * #TBM_BO_DEFAULT indecates default memory: it depends on the backend\n
219 * #TBM_BO_SCANOUT indecates scanout memory\n
220 * #TBM_BO_NONCACHABLE indecates non-cachable memory\n
221 * #TBM_BO_WC indecates write-combine memory\n
222 * #TBM_BO_VENDOR indecates vendor specific memory: it depends on the tbm backend
224 * @param[in] bufmgr : the buffer manager
225 * @param[in] size : the size of buffer object
226 * @param[in] flags : the flags of memory type
227 * @return a buffer object
231 #include <tbm_bufmgr.h>
237 bufmgr = tbm_bufmgr_init (bufmgr_fd);
238 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
242 tbm_bufmgr_deinit (bufmgr);
245 tbm_bo tbm_bo_alloc (tbm_bufmgr bufmgr, int size, int flags);
248 * @brief Increases the reference count of bo.
250 * @param[in] bo : the buffer object
251 * @return a buffer object
253 * @see tbm_bo_unref()
256 #include <tbm_bufmgr.h>
262 bufmgr = tbm_bufmgr_init (bufmgr_fd);
263 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
267 bo = tbm_bo_ref (bo);
271 tbm_bufmgr_deinit (bufmgr);
274 tbm_bo tbm_bo_ref (tbm_bo bo);
277 * @brief Decreases the reference count of bo
279 * @param[in] bo : the buffer object
281 * @see tbm_bo_alloc()
284 #include <tbm_bufmgr.h>
290 bufmgr = tbm_bufmgr_init (bufmgr_fd);
291 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
296 tbm_bufmgr_deinit (bufmgr);
299 void tbm_bo_unref (tbm_bo bo);
302 * @brief Maps the buffer object according to the device type and the option.
303 * @details Cache flushing and Locking is executed, while tbm_bo is mapping in the proper condition according to the device type and the access option.\n
304 * If the cache flush type of bufmgr set true, the map cache flushing is executed
305 * If the lock type of bufmgr set once, the previous bo which is locked is unlock when the new bo is trying to be locked.\n
306 * If the lock type of bufmgr set always, the new bo is locked until the previous bo which is locked is unlocked.\n
307 * If the lock type of bufmgr set never, Every bo is never locked.\n\n
308 * #TBM_DEVICE_DEFAULT indecates the default handle.\n
309 * #TBM_DEVICE_2D indecates the 2D memory handle.\n
310 * #TBM_DEVICE_3D indecates the 3D memory handle.\n
311 * #TBM_DEVICE_CPU indecates the virtual memory handle.\n
312 * #TBM_DEVICE_MM indecates the multimedia handle.\n\n
313 * #TBM_OPTION_READ indecates the accss option to read.\n
314 * #TBM_OPTION_WRITE indecates the access option to write.\n
315 * #TBM_OPTION_VENDOR indecates the vendor specific option that depends on the backend.
317 * @param[in] bo : the buffer object
318 * @param[in] device : the device type to get a handle
319 * @param[in] opt : the option to access the buffer object
320 * @return the handle of the buffer object
322 * @see tbm_bo_unmap()
325 #include <tbm_bufmgr.h>
330 tbm_bo_handle handle;
332 bufmgr = tbm_bufmgr_init (bufmgr_fd);
333 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
337 handle = tbm_bo_map (bo, TBM_DEVICE_2D, TBM_OPTION_READ|TBM_OPTION_WRITE);
343 tbm_bufmgr_deinit (bufmgr);
346 tbm_bo_handle tbm_bo_map (tbm_bo bo, int device, int opt);
349 * @brief Unmaps the buffer object.
351 * @param[in] bo : the buffer object
352 * @return 1 if this function succeeds, otherwise 0.
356 #include <tbm_bufmgr.h>
361 tbm_bo_handle handle;
363 bufmgr = tbm_bufmgr_init (bufmgr_fd);
364 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
368 handle = tbm_bo_map (bo, TBM_DEVICE_2D, TBM_OPTION_READ|TBM_OPTION_WRITE);
374 tbm_bufmgr_deinit (bufmgr);
377 int tbm_bo_unmap (tbm_bo bo);
380 * @brief Gets the tbm_bo_handle according to the device type.
381 * @details The tbm_bo_handle can be get without the map of the tbm_bo.\n
382 * In this case, TBM does not guarantee the lock and the cache flush of the tbm_bo.\n\n
383 * #TBM_DEVICE_DEFAULT indecates the default handle.\n
384 * #TBM_DEVICE_2D indecates the 2D memory handle.\n
385 * #TBM_DEVICE_3D indecates the 3D memory handle.\n
386 * #TBM_DEVICE_CPU indecates the virtual memory handle.\n
387 * #TBM_DEVICE_MM indecates the multimedia handle.
389 * @param[in] bo : the buffer object
390 * @param[in] device : the device type to get a handle
391 * @return the handle of the buffer object
392 * @retval #tbm_bo_handle
395 #include <tbm_bufmgr.h>
400 tbm_bo_handle handle;
402 bufmgr = tbm_bufmgr_init (bufmgr_fd);
403 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
407 handle = tbm_bo_get_handle (bo, TBM_DEVICE_2D);
412 tbm_bufmgr_deinit (bufmgr);
415 tbm_bo_handle tbm_bo_get_handle (tbm_bo bo, int device);
418 * @brief Exports the buffer object by key.
419 * @details The tbm_bo can be exported to the anther process with the unique key associated with the the tbm_bo.
421 * @param[in] bo : the buffer object
422 * @return key associated with the buffer object
424 * @see tbm_bo_import()
427 #include <tbm_bufmgr.h>
434 bufmgr = tbm_bufmgr_init (bufmgr_fd);
435 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
436 key = tbm_bo_export (bo);
441 tbm_bufmgr_deinit (bufmgr);
444 tbm_key tbm_bo_export (tbm_bo bo);
447 * @brief Exports the buffer object by fd.
448 * @details The tbm_bo can be exported to the anther process with the unique fd associated with the the tbm_bo.
450 * @param[in] bo : the buffer object
451 * @return fd associated with the buffer object
453 * @see tbm_bo_import_fd()
456 #include <tbm_bufmgr.h>
463 bufmgr = tbm_bufmgr_init (bufmgr_fd);
464 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
465 bo_fd = tbm_bo_export (bo);
470 tbm_bufmgr_deinit (bufmgr);
473 tbm_fd tbm_bo_export_fd (tbm_bo bo);
476 * @brief Imports the buffer object associated with the key.
477 * @details The reference count of the tbm_bo is 1.
479 * @param[in] bufmgr : the buffer manager
480 * @param[in] key : the key associated with the buffer object
481 * @return a buffer object
483 * @see tbm_bo_export()
486 #include <tbm_bufmgr.h>
495 bufmgr = tbm_bufmgr_init (bufmgr_fd);
496 bo = tbm_bo_import (key);
501 tbm_bufmgr_deinit (bufmgr);
504 tbm_bo tbm_bo_import (tbm_bufmgr bufmgr, tbm_key key);
507 * @brief Imports the buffer object associated with the fd.
508 * @details The reference count of the tbm_bo is 1.
510 * @param[in] bufmgr : the buffer manager
511 * @param[in] fd : the fd associated with the buffer object
512 * @return a buffer object
514 * @see tbm_bo_export_fd()
517 #include <tbm_bufmgr.h>
526 bufmgr = tbm_bufmgr_init (bufmgr_fd);
527 bo = tbm_bo_import (bo_fd);
532 tbm_bufmgr_deinit (bufmgr);
535 tbm_bo tbm_bo_import_fd (tbm_bufmgr bufmgr, tbm_fd fd);
538 * @brief Gets the size of a bo.
540 * @param[in] bo : the buffer object
541 * @return 1 if this function succeeds, otherwise 0.
542 * @see tbm_bo_alloc()
545 #include <tbm_bufmgr.h>
552 bufmgr = tbm_bufmgr_init (bufmgr_fd);
553 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
554 size = tbm_bo_size (bo);
559 tbm_bufmgr_deinit (bufmgr);
562 int tbm_bo_size (tbm_bo bo);
565 * @brief Gets the state where the buffer object is locked.
567 * @param[in] bo : the buffer object
568 * @return 1 if this bo is locked, otherwise 0.
570 * @see tbm_bo_unmap()
573 #include <tbm_bufmgr.h>
579 bufmgr = tbm_bufmgr_init (bufmgr_fd);
580 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
584 if (tbm_bo_locked (bo))
590 tbm_bufmgr_deinit (bufmgr);
593 int tbm_bo_locked (tbm_bo bo);
596 * @brief Swaps the buffer object.
598 * @param[in] bo1 : the buffer object
599 * @param[in] bo2 : the buffer object
600 * @return 1 if this function succeeds, otherwise 0.
603 #include <tbm_bufmgr.h>
611 bufmgr = tbm_bufmgr_init (bufmgr_fd);
612 bo1 = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
613 bo2 = tbm_bo_alloc (bufmgr, 256 * 256, TBM_BO_DEFAULT);
617 ret = tbm_bo_swap (bo1, bo2);
623 tbm_bufmgr_deinit (bufmgr);
626 int tbm_bo_swap (tbm_bo bo1, tbm_bo bo2);
630 * @brief Called when the user data is deleted in buffer object.
632 * @param[in] user_data User_data to be passed to callback function
633 * @pre The callback must be registered using tbm_bo_add_user_data().\n
634 * tbm_bo_delete_user_data() must be called to invoke this callback.
635 * @see tbm_bo_add_user_data()
636 * @see tbm_bo_delete_user_data()
638 typedef void (*tbm_data_free)(void *user_data);
641 * @brief Adds a user_data to the buffer object.
643 * @param[in] bo : the buffer object
644 * @param[in] key : the key associated with the user_data
645 * @param[in] data_free_func : the function pointer to free the user_data
646 * @return 1 if this function succeeds, otherwise 0.
647 * @post tbm_data_free() will be called under certain conditions, after calling tbm_bo_delete_user_data().
648 * @see tbm_data_free()
649 * @see tbm_bo_set_user_data()
650 * @see tbm_bo_get_user_data()
651 * @see tbm_bo_delete_user_data()
654 #include <tbm_bufmgr.h>
656 void example_data_free (void *user_data)
658 char *data = (char*) user_data;
671 bufmgr = tbm_bufmgr_init (bufmgr_fd);
672 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
673 user_data = (char*) malloc (sizeof(char) * 128);
677 tbm_bo_add_user_data (bo, 1, example_data_free);
678 tbm_bo_set_user_data (bo, 1, user_data);
682 ret = tbm_bo_get_user_data (bo, 1, &get_data);
683 tbm_bo_delete_user_data (bo, 1);
688 tbm_bufmgr_deinit (bufmgr);
693 int tbm_bo_add_user_data (tbm_bo bo, unsigned long key, tbm_data_free data_free_func);
696 * @brief Deletes the user_data in the buffer object.
698 * @param[in] bo : the buffer object
699 * @param[in] key : the key associated with the user_date
700 * @return 1 if this function succeeds, otherwise 0.
701 * @see tbm_bo_add_user_data()
702 * @see tbm_bo_get_user_data()
703 * @see tbm_bo_delete_user_data()
706 #include <tbm_bufmgr.h>
708 void example_data_free (void *user_data)
710 char *data = (char*) user_data;
723 bufmgr = tbm_bufmgr_init (bufmgr_fd);
724 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
725 user_data = (char*) malloc (sizeof(char) * 128);
729 tbm_bo_add_user_data (bo, 1, example_data_free);
730 tbm_bo_set_user_data (bo, 1, user_data);
734 ret = tbm_bo_get_user_data (bo, 1, &get_data);
735 tbm_bo_delete_user_data (bo, 1);
740 tbm_bufmgr_deinit (bufmgr);
744 int tbm_bo_delete_user_data (tbm_bo bo, unsigned long key);
747 * @brief Sets a user_date to the buffer object.
749 * @param[in] bo : the buffer object
750 * @param[in] key : the key associated with the user_date
751 * @param[in] data : a pointer of the user_data
752 * @return 1 if this function succeeds, otherwise 0.
753 * @see tbm_bo_add_user_data()
754 * @see tbm_bo_set_user_data()
755 * @see tbm_bo_delete_user_data()
758 #include <tbm_bufmgr.h>
760 void example_data_free (void *user_data)
762 char *data = (char*) user_data;
775 bufmgr = tbm_bufmgr_init (bufmgr_fd);
776 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
777 user_data = (char*) malloc (sizeof(char) * 128);
781 tbm_bo_add_user_data (bo, 1, example_data_free);
782 tbm_bo_set_user_data (bo, 1, user_data);
786 ret = tbm_bo_get_user_data (bo, 1, &get_data);
787 tbm_bo_delete_user_data (bo, 1);
792 tbm_bufmgr_deinit (bufmgr);
796 int tbm_bo_set_user_data (tbm_bo bo, unsigned long key, void* data);
799 * @brief Gets a user_data from the buffer object with the key.
801 * @param[in] bo : the buffer object
802 * @param[in] key : the key associated with the user_date
803 * @param[out] data : to get the user data
804 * @return 1 if this function succeeds, otherwise 0.
805 * @see tbm_bo_add_user_data()
806 * @see tbm_bo_set_user_data()
807 * @see tbm_bo_get_user_data()
810 #include <tbm_bufmgr.h>
812 void example_data_free (void *user_data)
814 char *data = (char*) user_data;
827 bufmgr = tbm_bufmgr_init (bufmgr_fd);
828 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
829 user_data = (char*) malloc (sizeof(char) * 128);
833 tbm_bo_add_user_data (bo, 1, example_data_free);
834 tbm_bo_set_user_data (bo, 1, user_data);
838 ret = tbm_bo_get_user_data (bo, 1, &get_data);
839 tbm_bo_delete_user_data (bo, 1);
844 tbm_bufmgr_deinit (bufmgr);
848 int tbm_bo_get_user_data (tbm_bo bo, unsigned long key, void** data);
850 int tbm_bo_cache_flush (tbm_bo bo, int flags);
857 #endif /* _TBM_BUFMGR_H_ */