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_
37 * @addtogroup CAPI_UI_TBM_BUFMGR_MODULE
46 * \brief Tizen Buffer Manager
50 * @brief Definition for the tizen buffer manager
53 typedef struct _tbm_bufmgr * tbm_bufmgr;
56 * @brief Definition for the tizen buffer object
59 typedef struct _tbm_bo *tbm_bo;
61 * @brief Definition for the key associated with the buffer object
64 typedef uint32_t tbm_key;
66 * @brief Definition for the file descripter of the system buffer manager
69 typedef int32_t tbm_fd;
75 * @brief Definition for the device type to get the default handle
78 #define TBM_DEVICE_DEFAULT 0
80 * @brief Definition for the device type to get the virtual memory
83 #define TBM_DEVICE_CPU 1
85 * @brief Definition for the device type to get the 2D memory handle
88 #define TBM_DEVICE_2D 2
90 * @brief Definition for the device type to get the 3D memory handle
93 #define TBM_DEVICE_3D 3
95 * @brief Definition for the device type to get the multimedia handle
98 #define TBM_DEVICE_MM 4
101 * @brief Definition for the cache invalidate
104 #define TBM_CACHE_INV 0x01
106 * @brief Definition for the cache clean
109 #define TBM_CACHE_CLN 0x02
114 * @brief Definition for the access option to read
117 #define TBM_OPTION_READ (1 << 0)
119 * @brief Definition for the access option to write
122 #define TBM_OPTION_WRITE (1 << 1)
124 * @brief Definition for the vendor specific option that depends on the backend
127 #define TBM_OPTION_VENDOR (0xffff0000)
130 * @brief tbm_bo_handle abstraction of the memory handle by TBM_DEVICE_TYPE
133 typedef union _tbm_bo_handle
143 * @brief Enumeration of bo memory type
148 TBM_BO_DEFAULT = 0, /**< default memory: it depends on the backend */
149 TBM_BO_SCANOUT = (1<<0), /**< scanout memory */
150 TBM_BO_NONCACHABLE = (1<<1), /**< non-cachable memory */
151 TBM_BO_WC = (1<<2), /**< write-combine memory */
152 TBM_BO_VENDOR = (0xffff0000), /**< vendor specific memory: it depends on the backend */
159 /* Functions for buffer manager */
162 * @brief Initializes the buffer manager.
163 * @details If fd is lower than zero, fd is get drm fd in tbm_bufmgr_init function\n
164 * 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
166 * BUFMGR_LOCK default is once\n
167 * once : The previous bo which is locked is unlock when the new bo is trying to be locked\n
168 * always : The new bo is locked until the previous bo which is locked is unlocked\n
169 * never : Every bo is never locked.\n
171 * BUFMGR_MAP_CACHE default is true\n
172 * true : use map cache flushing\n
173 * false : to use map cache flushing
175 * @param[in] fd : file descripter of the system buffer manager
176 * @return a buffer manager
177 * @retval #tbm_bufmgr
178 * @see tbm_bufmgr_deinit();
181 #include <tbm_bufmgr.h>
185 setenv("BUFMGR_LOCK_TYPE", "once", 1);
186 setenv("BUFMGR_MAP_CACHE", "true", 1);
189 bufmgr = tbm_bufmgr_init (bufmgr_fd);
193 tbm_bufmgr_deinit (bufmgr);
196 tbm_bufmgr tbm_bufmgr_init (int fd);
199 * @brief Deinitializes the buffer manager.
201 * @param[in] bufmgr : the buffer manager
202 * @see tbm_bufmgr_init()
205 #include <tbm_bufmgr.h>
209 bufmgr = tbm_bufmgr_init (bufmgr_fd);
213 tbm_bufmgr_deinit (bufmgr);
216 void tbm_bufmgr_deinit (tbm_bufmgr bufmgr);
218 /* Functions for bo */
221 * @brief Allocates the buffer object.
222 * @details This function create tbm_bo and set reference count to 1.\n
223 * The user can craete tbm_bo with memory type flag #TBM_BO_FLAGS\n\n
224 * #TBM_BO_DEFAULT indecates default memory: it depends on the backend\n
225 * #TBM_BO_SCANOUT indecates scanout memory\n
226 * #TBM_BO_NONCACHABLE indecates non-cachable memory\n
227 * #TBM_BO_WC indecates write-combine memory\n
228 * #TBM_BO_VENDOR indecates vendor specific memory: it depends on the tbm backend
230 * @param[in] bufmgr : the buffer manager
231 * @param[in] size : the size of buffer object
232 * @param[in] flags : the flags of memory type
233 * @return a buffer object
237 #include <tbm_bufmgr.h>
243 bufmgr = tbm_bufmgr_init (bufmgr_fd);
244 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
248 tbm_bufmgr_deinit (bufmgr);
251 tbm_bo tbm_bo_alloc (tbm_bufmgr bufmgr, int size, int flags);
254 * @brief Increases the reference count of bo.
256 * @param[in] bo : the buffer object
257 * @return a buffer object
259 * @see tbm_bo_unref()
262 #include <tbm_bufmgr.h>
268 bufmgr = tbm_bufmgr_init (bufmgr_fd);
269 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
273 bo = tbm_bo_ref (bo);
277 tbm_bufmgr_deinit (bufmgr);
280 tbm_bo tbm_bo_ref (tbm_bo bo);
283 * @brief Decreases the reference count of bo
285 * @param[in] bo : the buffer object
287 * @see tbm_bo_alloc()
290 #include <tbm_bufmgr.h>
296 bufmgr = tbm_bufmgr_init (bufmgr_fd);
297 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
302 tbm_bufmgr_deinit (bufmgr);
305 void tbm_bo_unref (tbm_bo bo);
308 * @brief Maps the buffer object according to the device type and the option.
309 * @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
310 * If the cache flush type of bufmgr set true, the map cache flushing is executed
311 * 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
312 * If the lock type of bufmgr set always, the new bo is locked until the previous bo which is locked is unlocked.\n
313 * If the lock type of bufmgr set never, Every bo is never locked.\n\n
314 * #TBM_DEVICE_DEFAULT indecates the default handle.\n
315 * #TBM_DEVICE_2D indecates the 2D memory handle.\n
316 * #TBM_DEVICE_3D indecates the 3D memory handle.\n
317 * #TBM_DEVICE_CPU indecates the virtual memory handle.\n
318 * #TBM_DEVICE_MM indecates the multimedia handle.\n\n
319 * #TBM_OPTION_READ indecates the accss option to read.\n
320 * #TBM_OPTION_WRITE indecates the access option to write.\n
321 * #TBM_OPTION_VENDOR indecates the vendor specific option that depends on the backend.
323 * @param[in] bo : the buffer object
324 * @param[in] device : the device type to get a handle
325 * @param[in] opt : the option to access the buffer object
326 * @return the handle of the buffer object
328 * @see tbm_bo_unmap()
331 #include <tbm_bufmgr.h>
336 tbm_bo_handle handle;
338 bufmgr = tbm_bufmgr_init (bufmgr_fd);
339 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
343 handle = tbm_bo_map (bo, TBM_DEVICE_2D, TBM_OPTION_READ|TBM_OPTION_WRITE);
349 tbm_bufmgr_deinit (bufmgr);
352 tbm_bo_handle tbm_bo_map (tbm_bo bo, int device, int opt);
355 * @brief Unmaps the buffer object.
357 * @param[in] bo : the buffer object
358 * @return 1 if this function succeeds, otherwise 0.
362 #include <tbm_bufmgr.h>
367 tbm_bo_handle handle;
369 bufmgr = tbm_bufmgr_init (bufmgr_fd);
370 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
374 handle = tbm_bo_map (bo, TBM_DEVICE_2D, TBM_OPTION_READ|TBM_OPTION_WRITE);
380 tbm_bufmgr_deinit (bufmgr);
383 int tbm_bo_unmap (tbm_bo bo);
386 * @brief Gets the tbm_bo_handle according to the device type.
387 * @details The tbm_bo_handle can be get without the map of the tbm_bo.\n
388 * In this case, TBM does not guarantee the lock and the cache flush of the tbm_bo.\n\n
389 * #TBM_DEVICE_DEFAULT indecates the default handle.\n
390 * #TBM_DEVICE_2D indecates the 2D memory handle.\n
391 * #TBM_DEVICE_3D indecates the 3D memory handle.\n
392 * #TBM_DEVICE_CPU indecates the virtual memory handle.\n
393 * #TBM_DEVICE_MM indecates the multimedia handle.
395 * @param[in] bo : the buffer object
396 * @param[in] device : the device type to get a handle
397 * @return the handle of the buffer object
398 * @retval #tbm_bo_handle
401 #include <tbm_bufmgr.h>
406 tbm_bo_handle handle;
408 bufmgr = tbm_bufmgr_init (bufmgr_fd);
409 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
413 handle = tbm_bo_get_handle (bo, TBM_DEVICE_2D);
418 tbm_bufmgr_deinit (bufmgr);
421 tbm_bo_handle tbm_bo_get_handle (tbm_bo bo, int device);
424 * @brief Exports the buffer object by key.
425 * @details The tbm_bo can be exported to the anther process with the unique key associated with the the tbm_bo.
427 * @param[in] bo : the buffer object
428 * @return key associated with the buffer object
430 * @see tbm_bo_import()
433 #include <tbm_bufmgr.h>
440 bufmgr = tbm_bufmgr_init (bufmgr_fd);
441 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
442 key = tbm_bo_export (bo);
447 tbm_bufmgr_deinit (bufmgr);
450 tbm_key tbm_bo_export (tbm_bo bo);
453 * @brief Exports the buffer object by fd.
454 * @details The tbm_bo can be exported to the anther process with the unique fd associated with the the tbm_bo.
456 * @param[in] bo : the buffer object
457 * @return fd associated with the buffer object
459 * @see tbm_bo_import_fd()
462 #include <tbm_bufmgr.h>
469 bufmgr = tbm_bufmgr_init (bufmgr_fd);
470 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
471 bo_fd = tbm_bo_export (bo);
476 tbm_bufmgr_deinit (bufmgr);
479 tbm_fd tbm_bo_export_fd (tbm_bo bo);
482 * @brief Imports the buffer object associated with the key.
483 * @details The reference count of the tbm_bo is 1.
485 * @param[in] bufmgr : the buffer manager
486 * @param[in] key : the key associated with the buffer object
487 * @return a buffer object
489 * @see tbm_bo_export()
492 #include <tbm_bufmgr.h>
501 bufmgr = tbm_bufmgr_init (bufmgr_fd);
502 bo = tbm_bo_import (key);
507 tbm_bufmgr_deinit (bufmgr);
510 tbm_bo tbm_bo_import (tbm_bufmgr bufmgr, tbm_key key);
513 * @brief Imports the buffer object associated with the fd.
514 * @details The reference count of the tbm_bo is 1.
516 * @param[in] bufmgr : the buffer manager
517 * @param[in] fd : the fd associated with the buffer object
518 * @return a buffer object
520 * @see tbm_bo_export_fd()
523 #include <tbm_bufmgr.h>
532 bufmgr = tbm_bufmgr_init (bufmgr_fd);
533 bo = tbm_bo_import (bo_fd);
538 tbm_bufmgr_deinit (bufmgr);
541 tbm_bo tbm_bo_import_fd (tbm_bufmgr bufmgr, tbm_fd fd);
544 * @brief Gets the size of a bo.
546 * @param[in] bo : the buffer object
547 * @return 1 if this function succeeds, otherwise 0.
548 * @see tbm_bo_alloc()
551 #include <tbm_bufmgr.h>
558 bufmgr = tbm_bufmgr_init (bufmgr_fd);
559 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
560 size = tbm_bo_size (bo);
565 tbm_bufmgr_deinit (bufmgr);
568 int tbm_bo_size (tbm_bo bo);
571 * @brief Gets the state where the buffer object is locked.
573 * @param[in] bo : the buffer object
574 * @return 1 if this bo is locked, otherwise 0.
576 * @see tbm_bo_unmap()
579 #include <tbm_bufmgr.h>
585 bufmgr = tbm_bufmgr_init (bufmgr_fd);
586 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
590 if (tbm_bo_locked (bo))
596 tbm_bufmgr_deinit (bufmgr);
599 int tbm_bo_locked (tbm_bo bo);
602 * @brief Swaps the buffer object.
604 * @param[in] bo1 : the buffer object
605 * @param[in] bo2 : the buffer object
606 * @return 1 if this function succeeds, otherwise 0.
609 #include <tbm_bufmgr.h>
617 bufmgr = tbm_bufmgr_init (bufmgr_fd);
618 bo1 = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
619 bo2 = tbm_bo_alloc (bufmgr, 256 * 256, TBM_BO_DEFAULT);
623 ret = tbm_bo_swap (bo1, bo2);
629 tbm_bufmgr_deinit (bufmgr);
632 int tbm_bo_swap (tbm_bo bo1, tbm_bo bo2);
636 * @brief Called when the user data is deleted in buffer object.
638 * @param[in] user_data User_data to be passed to callback function
639 * @pre The callback must be registered using tbm_bo_add_user_data().\n
640 * tbm_bo_delete_user_data() must be called to invoke this callback.
641 * @see tbm_bo_add_user_data()
642 * @see tbm_bo_delete_user_data()
644 typedef void (*tbm_data_free)(void *user_data);
647 * @brief Adds a user_data to the buffer object.
649 * @param[in] bo : the buffer object
650 * @param[in] key : the key associated with the user_data
651 * @param[in] data_free_func : the function pointer to free the user_data
652 * @return 1 if this function succeeds, otherwise 0.
653 * @post tbm_data_free() will be called under certain conditions, after calling tbm_bo_delete_user_data().
654 * @see tbm_data_free()
655 * @see tbm_bo_set_user_data()
656 * @see tbm_bo_get_user_data()
657 * @see tbm_bo_delete_user_data()
660 #include <tbm_bufmgr.h>
662 void example_data_free (void *user_data)
664 char *data = (char*) user_data;
677 bufmgr = tbm_bufmgr_init (bufmgr_fd);
678 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
679 user_data = (char*) malloc (sizeof(char) * 128);
683 tbm_bo_add_user_data (bo, 1, example_data_free);
684 tbm_bo_set_user_data (bo, 1, user_data);
688 ret = tbm_bo_get_user_data (bo, 1, &get_data);
689 tbm_bo_delete_user_data (bo, 1);
694 tbm_bufmgr_deinit (bufmgr);
699 int tbm_bo_add_user_data (tbm_bo bo, unsigned long key, tbm_data_free data_free_func);
702 * @brief Deletes the user_data in the buffer object.
704 * @param[in] bo : the buffer object
705 * @param[in] key : the key associated with the user_date
706 * @return 1 if this function succeeds, otherwise 0.
707 * @see tbm_bo_add_user_data()
708 * @see tbm_bo_get_user_data()
709 * @see tbm_bo_delete_user_data()
712 #include <tbm_bufmgr.h>
714 void example_data_free (void *user_data)
716 char *data = (char*) user_data;
729 bufmgr = tbm_bufmgr_init (bufmgr_fd);
730 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
731 user_data = (char*) malloc (sizeof(char) * 128);
735 tbm_bo_add_user_data (bo, 1, example_data_free);
736 tbm_bo_set_user_data (bo, 1, user_data);
740 ret = tbm_bo_get_user_data (bo, 1, &get_data);
741 tbm_bo_delete_user_data (bo, 1);
746 tbm_bufmgr_deinit (bufmgr);
750 int tbm_bo_delete_user_data (tbm_bo bo, unsigned long key);
753 * @brief Sets a user_date to the buffer object.
755 * @param[in] bo : the buffer object
756 * @param[in] key : the key associated with the user_date
757 * @param[in] data : a pointer of the user_data
758 * @return 1 if this function succeeds, otherwise 0.
759 * @see tbm_bo_add_user_data()
760 * @see tbm_bo_set_user_data()
761 * @see tbm_bo_delete_user_data()
764 #include <tbm_bufmgr.h>
766 void example_data_free (void *user_data)
768 char *data = (char*) user_data;
781 bufmgr = tbm_bufmgr_init (bufmgr_fd);
782 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
783 user_data = (char*) malloc (sizeof(char) * 128);
787 tbm_bo_add_user_data (bo, 1, example_data_free);
788 tbm_bo_set_user_data (bo, 1, user_data);
792 ret = tbm_bo_get_user_data (bo, 1, &get_data);
793 tbm_bo_delete_user_data (bo, 1);
798 tbm_bufmgr_deinit (bufmgr);
802 int tbm_bo_set_user_data (tbm_bo bo, unsigned long key, void* data);
805 * @brief Gets a user_data from the buffer object with the key.
807 * @param[in] bo : the buffer object
808 * @param[in] key : the key associated with the user_date
809 * @param[out] data : to get the user data
810 * @return 1 if this function succeeds, otherwise 0.
811 * @see tbm_bo_add_user_data()
812 * @see tbm_bo_set_user_data()
813 * @see tbm_bo_get_user_data()
816 #include <tbm_bufmgr.h>
818 void example_data_free (void *user_data)
820 char *data = (char*) user_data;
833 bufmgr = tbm_bufmgr_init (bufmgr_fd);
834 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
835 user_data = (char*) malloc (sizeof(char) * 128);
839 tbm_bo_add_user_data (bo, 1, example_data_free);
840 tbm_bo_set_user_data (bo, 1, user_data);
844 ret = tbm_bo_get_user_data (bo, 1, &get_data);
845 tbm_bo_delete_user_data (bo, 1);
850 tbm_bufmgr_deinit (bufmgr);
854 int tbm_bo_get_user_data (tbm_bo bo, unsigned long key, void** data);
856 int tbm_bo_cache_flush (tbm_bo bo, int flags);
867 #endif /* _TBM_BUFMGR_H_ */