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 #include <tizen_error.h>
40 /* this define will be removed when the capi-common is the version 0.1.1 */
41 #ifndef TIZEN_ERROR_TBM
42 #define TIZEN_ERROR_TBM -0x02830000
47 * \brief Tizen Buffer Manager
51 * @brief Definition for the tizen buffer manager
54 typedef struct _tbm_bufmgr * tbm_bufmgr;
57 * @brief Definition for the tizen buffer object
60 typedef struct _tbm_bo *tbm_bo;
62 * @brief Definition for the key associated with the buffer object
65 typedef uint32_t tbm_key;
67 * @brief Definition for the file descripter of the system buffer manager
70 typedef int32_t tbm_fd;
76 * @brief Definition for the device type to get the default handle
79 #define TBM_DEVICE_DEFAULT 0
81 * @brief Definition for the device type to get the virtual memory
84 #define TBM_DEVICE_CPU 1
86 * @brief Definition for the device type to get the 2D memory handle
89 #define TBM_DEVICE_2D 2
91 * @brief Definition for the device type to get the 3D memory handle
94 #define TBM_DEVICE_3D 3
96 * @brief Definition for the device type to get the multimedia handle
99 #define TBM_DEVICE_MM 4
102 * @brief Definition for the cache invalidate
105 #define TBM_CACHE_INV 0x01
107 * @brief Definition for the cache clean
110 #define TBM_CACHE_CLN 0x02
115 * @brief Definition for the access option to read
118 #define TBM_OPTION_READ (1 << 0)
120 * @brief Definition for the access option to write
123 #define TBM_OPTION_WRITE (1 << 1)
125 * @brief Definition for the vendor specific option that depends on the backend
128 #define TBM_OPTION_VENDOR (0xffff0000)
131 * @brief tbm_bo_handle abstraction of the memory handle by TBM_DEVICE_TYPE
134 typedef union _tbm_bo_handle
144 * @brief Enumeration of bo memory type
149 TBM_BO_DEFAULT = 0, /**< default memory: it depends on the backend */
150 TBM_BO_SCANOUT = (1<<0), /**< scanout memory */
151 TBM_BO_NONCACHABLE = (1<<1), /**< non-cachable memory */
152 TBM_BO_WC = (1<<2), /**< write-combine memory */
153 TBM_BO_VENDOR = (0xffff0000), /**< vendor specific memory: it depends on the backend */
158 * @brief Enumeration for tbm error type.
163 TBM_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */
164 TBM_ERROR_BO_LOCK_FAILED = TIZEN_ERROR_TBM|0x0101, /**< tbm_bo lock failed */
165 TBM_ERROR_BO_MAP_FAILED = TIZEN_ERROR_TBM|0x0102, /**< tbm_bo map failed */
173 /* Functions for buffer manager */
176 * @brief Initializes the buffer manager.
177 * @details If fd is lower than zero, fd is get drm fd in tbm_bufmgr_init function\n
178 * 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
180 * BUFMGR_LOCK default is once\n
181 * once : The previous bo which is locked is unlock when the new bo is trying to be locked\n
182 * always : The new bo is locked until the previous bo which is locked is unlocked\n
183 * never : Every bo is never locked.\n
185 * BUFMGR_MAP_CACHE default is true\n
186 * true : use map cache flushing\n
187 * false : to use map cache flushing
189 * @param[in] fd : file descripter of the system buffer manager
190 * @return a buffer manager
191 * @retval #tbm_bufmgr
192 * @see tbm_bufmgr_deinit();
195 #include <tbm_bufmgr.h>
199 setenv("BUFMGR_LOCK_TYPE", "once", 1);
200 setenv("BUFMGR_MAP_CACHE", "true", 1);
203 bufmgr = tbm_bufmgr_init (bufmgr_fd);
207 tbm_bufmgr_deinit (bufmgr);
210 tbm_bufmgr tbm_bufmgr_init (int fd);
213 * @brief Deinitializes the buffer manager.
215 * @param[in] bufmgr : the buffer manager
216 * @see tbm_bufmgr_init()
219 #include <tbm_bufmgr.h>
223 bufmgr = tbm_bufmgr_init (bufmgr_fd);
227 tbm_bufmgr_deinit (bufmgr);
230 void tbm_bufmgr_deinit (tbm_bufmgr bufmgr);
232 /* Functions for bo */
235 * @brief Allocates the buffer object.
236 * @details This function create tbm_bo and set reference count to 1.\n
237 * The user can craete tbm_bo with memory type flag #TBM_BO_FLAGS\n\n
238 * #TBM_BO_DEFAULT indecates default memory: it depends on the backend\n
239 * #TBM_BO_SCANOUT indecates scanout memory\n
240 * #TBM_BO_NONCACHABLE indecates non-cachable memory\n
241 * #TBM_BO_WC indecates write-combine memory\n
242 * #TBM_BO_VENDOR indecates vendor specific memory: it depends on the tbm backend
244 * @param[in] bufmgr : the buffer manager
245 * @param[in] size : the size of buffer object
246 * @param[in] flags : the flags of memory type
247 * @return a buffer object
251 #include <tbm_bufmgr.h>
257 bufmgr = tbm_bufmgr_init (bufmgr_fd);
258 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
262 tbm_bufmgr_deinit (bufmgr);
265 tbm_bo tbm_bo_alloc (tbm_bufmgr bufmgr, int size, int flags);
268 * @brief Increases the reference count of bo.
270 * @param[in] bo : the buffer object
271 * @return a buffer object
273 * @see tbm_bo_unref()
276 #include <tbm_bufmgr.h>
282 bufmgr = tbm_bufmgr_init (bufmgr_fd);
283 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
287 bo = tbm_bo_ref (bo);
291 tbm_bufmgr_deinit (bufmgr);
294 tbm_bo tbm_bo_ref (tbm_bo bo);
297 * @brief Decreases the reference count of bo
299 * @param[in] bo : the buffer object
301 * @see tbm_bo_alloc()
304 #include <tbm_bufmgr.h>
310 bufmgr = tbm_bufmgr_init (bufmgr_fd);
311 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
316 tbm_bufmgr_deinit (bufmgr);
319 void tbm_bo_unref (tbm_bo bo);
322 * @brief Maps the buffer object according to the device type and the option.
323 * @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
324 * If the cache flush type of bufmgr set true, the map cache flushing is executed
325 * 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
326 * If the lock type of bufmgr set always, the new bo is locked until the previous bo which is locked is unlocked.\n
327 * If the lock type of bufmgr set never, Every bo is never locked.\n\n
328 * #TBM_DEVICE_DEFAULT indecates the default handle.\n
329 * #TBM_DEVICE_2D indecates the 2D memory handle.\n
330 * #TBM_DEVICE_3D indecates the 3D memory handle.\n
331 * #TBM_DEVICE_CPU indecates the virtual memory handle.\n
332 * #TBM_DEVICE_MM indecates the multimedia handle.\n\n
333 * #TBM_OPTION_READ indecates the accss option to read.\n
334 * #TBM_OPTION_WRITE indecates the access option to write.\n
335 * #TBM_OPTION_VENDOR indecates the vendor specific option that depends on the backend.
337 * @param[in] bo : the buffer object
338 * @param[in] device : the device type to get a handle
339 * @param[in] opt : the option to access the buffer object
340 * @return the handle of the buffer object
341 * @exception #TBM_ERROR_NONE Success
342 * @exception #TBM_ERROR_BO_LOCK_FAILED tbm_bo lock failed
343 * @exception #TBM_ERROR_BO_MAP_FAILED tbm_bo map failed
345 * @see tbm_bo_unmap()
348 #include <tbm_bufmgr.h>
353 tbm_bo_handle handle;
356 bufmgr = tbm_bufmgr_init (bufmgr_fd);
357 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
361 handle = tbm_bo_map (bo, TBM_DEVICE_2D, TBM_OPTION_READ|TBM_OPTION_WRITE);
362 if (handle.ptr == NULL)
364 error = get_last_result ();
372 tbm_bufmgr_deinit (bufmgr);
375 tbm_bo_handle tbm_bo_map (tbm_bo bo, int device, int opt);
378 * @brief Unmaps the buffer object.
380 * @param[in] bo : the buffer object
381 * @return 1 if this function succeeds, otherwise 0.
385 #include <tbm_bufmgr.h>
390 tbm_bo_handle handle;
392 bufmgr = tbm_bufmgr_init (bufmgr_fd);
393 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
397 handle = tbm_bo_map (bo, TBM_DEVICE_2D, TBM_OPTION_READ|TBM_OPTION_WRITE);
403 tbm_bufmgr_deinit (bufmgr);
406 int tbm_bo_unmap (tbm_bo bo);
409 * @brief Gets the tbm_bo_handle according to the device type.
410 * @details The tbm_bo_handle can be get without the map of the tbm_bo.\n
411 * In this case, TBM does not guarantee the lock and the cache flush of the tbm_bo.\n\n
412 * #TBM_DEVICE_DEFAULT indecates the default handle.\n
413 * #TBM_DEVICE_2D indecates the 2D memory handle.\n
414 * #TBM_DEVICE_3D indecates the 3D memory handle.\n
415 * #TBM_DEVICE_CPU indecates the virtual memory handle.\n
416 * #TBM_DEVICE_MM indecates the multimedia handle.
418 * @param[in] bo : the buffer object
419 * @param[in] device : the device type to get a handle
420 * @return the handle of the buffer object
421 * @retval #tbm_bo_handle
424 #include <tbm_bufmgr.h>
429 tbm_bo_handle handle;
431 bufmgr = tbm_bufmgr_init (bufmgr_fd);
432 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
436 handle = tbm_bo_get_handle (bo, TBM_DEVICE_2D);
441 tbm_bufmgr_deinit (bufmgr);
444 tbm_bo_handle tbm_bo_get_handle (tbm_bo bo, int device);
447 * @brief Exports the buffer object by key.
448 * @details The tbm_bo can be exported to the anther process with the unique key associated with the the tbm_bo.
450 * @param[in] bo : the buffer object
451 * @return key associated with the buffer object
453 * @see tbm_bo_import()
456 #include <tbm_bufmgr.h>
463 bufmgr = tbm_bufmgr_init (bufmgr_fd);
464 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
465 key = tbm_bo_export (bo);
470 tbm_bufmgr_deinit (bufmgr);
473 tbm_key tbm_bo_export (tbm_bo bo);
476 * @brief Exports the buffer object by fd.
477 * @details The tbm_bo can be exported to the anther process with the unique fd associated with the the tbm_bo.
479 * @param[in] bo : the buffer object
480 * @return fd associated with the buffer object
482 * @see tbm_bo_import_fd()
485 #include <tbm_bufmgr.h>
492 bufmgr = tbm_bufmgr_init (bufmgr_fd);
493 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
494 bo_fd = tbm_bo_export (bo);
499 tbm_bufmgr_deinit (bufmgr);
502 tbm_fd tbm_bo_export_fd (tbm_bo bo);
505 * @brief Imports the buffer object associated with the key.
506 * @details The reference count of the tbm_bo is 1.
508 * @param[in] bufmgr : the buffer manager
509 * @param[in] key : the key associated with the buffer object
510 * @return a buffer object
512 * @see tbm_bo_export()
515 #include <tbm_bufmgr.h>
524 bufmgr = tbm_bufmgr_init (bufmgr_fd);
525 bo = tbm_bo_import (key);
530 tbm_bufmgr_deinit (bufmgr);
533 tbm_bo tbm_bo_import (tbm_bufmgr bufmgr, tbm_key key);
536 * @brief Imports the buffer object associated with the fd.
537 * @details The reference count of the tbm_bo is 1.
539 * @param[in] bufmgr : the buffer manager
540 * @param[in] fd : the fd associated with the buffer object
541 * @return a buffer object
543 * @see tbm_bo_export_fd()
546 #include <tbm_bufmgr.h>
555 bufmgr = tbm_bufmgr_init (bufmgr_fd);
556 bo = tbm_bo_import_fd (bo_fd);
561 tbm_bufmgr_deinit (bufmgr);
564 tbm_bo tbm_bo_import_fd (tbm_bufmgr bufmgr, tbm_fd fd);
567 * @brief Gets the size of a bo.
569 * @param[in] bo : the buffer object
570 * @return 1 if this function succeeds, otherwise 0.
571 * @see tbm_bo_alloc()
574 #include <tbm_bufmgr.h>
581 bufmgr = tbm_bufmgr_init (bufmgr_fd);
582 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
583 size = tbm_bo_size (bo);
588 tbm_bufmgr_deinit (bufmgr);
591 int tbm_bo_size (tbm_bo bo);
594 * @brief Gets the state where the buffer object is locked.
596 * @param[in] bo : the buffer object
597 * @return 1 if this bo is locked, otherwise 0.
599 * @see tbm_bo_unmap()
602 #include <tbm_bufmgr.h>
608 bufmgr = tbm_bufmgr_init (bufmgr_fd);
609 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
613 if (tbm_bo_locked (bo))
619 tbm_bufmgr_deinit (bufmgr);
622 int tbm_bo_locked (tbm_bo bo);
625 * @brief Swaps the buffer object.
627 * @param[in] bo1 : the buffer object
628 * @param[in] bo2 : the buffer object
629 * @return 1 if this function succeeds, otherwise 0.
632 #include <tbm_bufmgr.h>
640 bufmgr = tbm_bufmgr_init (bufmgr_fd);
641 bo1 = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
642 bo2 = tbm_bo_alloc (bufmgr, 256 * 256, TBM_BO_DEFAULT);
646 ret = tbm_bo_swap (bo1, bo2);
652 tbm_bufmgr_deinit (bufmgr);
655 int tbm_bo_swap (tbm_bo bo1, tbm_bo bo2);
659 * @brief Called when the user data is deleted in buffer object.
661 * @param[in] user_data User_data to be passed to callback function
662 * @pre The callback must be registered using tbm_bo_add_user_data().\n
663 * tbm_bo_delete_user_data() must be called to invoke this callback.
664 * @see tbm_bo_add_user_data()
665 * @see tbm_bo_delete_user_data()
667 typedef void (*tbm_data_free)(void *user_data);
670 * @brief Adds a user_data to the buffer object.
672 * @param[in] bo : the buffer object
673 * @param[in] key : the key associated with the user_data
674 * @param[in] data_free_func : the function pointer to free the user_data
675 * @return 1 if this function succeeds, otherwise 0.
676 * @post tbm_data_free() will be called under certain conditions, after calling tbm_bo_delete_user_data().
677 * @see tbm_data_free()
678 * @see tbm_bo_set_user_data()
679 * @see tbm_bo_get_user_data()
680 * @see tbm_bo_delete_user_data()
683 #include <tbm_bufmgr.h>
685 void example_data_free (void *user_data)
687 char *data = (char*) user_data;
700 bufmgr = tbm_bufmgr_init (bufmgr_fd);
701 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
702 user_data = (char*) malloc (sizeof(char) * 128);
706 tbm_bo_add_user_data (bo, 1, example_data_free);
707 tbm_bo_set_user_data (bo, 1, user_data);
711 ret = tbm_bo_get_user_data (bo, 1, &get_data);
712 tbm_bo_delete_user_data (bo, 1);
717 tbm_bufmgr_deinit (bufmgr);
722 int tbm_bo_add_user_data (tbm_bo bo, unsigned long key, tbm_data_free data_free_func);
725 * @brief Deletes the user_data in the buffer object.
727 * @param[in] bo : the buffer object
728 * @param[in] key : the key associated with the user_date
729 * @return 1 if this function succeeds, otherwise 0.
730 * @see tbm_bo_add_user_data()
731 * @see tbm_bo_get_user_data()
732 * @see tbm_bo_delete_user_data()
735 #include <tbm_bufmgr.h>
737 void example_data_free (void *user_data)
739 char *data = (char*) user_data;
752 bufmgr = tbm_bufmgr_init (bufmgr_fd);
753 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
754 user_data = (char*) malloc (sizeof(char) * 128);
758 tbm_bo_add_user_data (bo, 1, example_data_free);
759 tbm_bo_set_user_data (bo, 1, user_data);
763 ret = tbm_bo_get_user_data (bo, 1, &get_data);
764 tbm_bo_delete_user_data (bo, 1);
769 tbm_bufmgr_deinit (bufmgr);
773 int tbm_bo_delete_user_data (tbm_bo bo, unsigned long key);
776 * @brief Sets a user_date to the buffer object.
778 * @param[in] bo : the buffer object
779 * @param[in] key : the key associated with the user_date
780 * @param[in] data : a pointer of the user_data
781 * @return 1 if this function succeeds, otherwise 0.
782 * @see tbm_bo_add_user_data()
783 * @see tbm_bo_set_user_data()
784 * @see tbm_bo_delete_user_data()
787 #include <tbm_bufmgr.h>
789 void example_data_free (void *user_data)
791 char *data = (char*) user_data;
804 bufmgr = tbm_bufmgr_init (bufmgr_fd);
805 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
806 user_data = (char*) malloc (sizeof(char) * 128);
810 tbm_bo_add_user_data (bo, 1, example_data_free);
811 tbm_bo_set_user_data (bo, 1, user_data);
815 ret = tbm_bo_get_user_data (bo, 1, &get_data);
816 tbm_bo_delete_user_data (bo, 1);
821 tbm_bufmgr_deinit (bufmgr);
825 int tbm_bo_set_user_data (tbm_bo bo, unsigned long key, void* data);
828 * @brief Gets a user_data from the buffer object with the key.
830 * @param[in] bo : the buffer object
831 * @param[in] key : the key associated with the user_date
832 * @param[out] data : to get the user data
833 * @return 1 if this function succeeds, otherwise 0.
834 * @see tbm_bo_add_user_data()
835 * @see tbm_bo_set_user_data()
836 * @see tbm_bo_get_user_data()
839 #include <tbm_bufmgr.h>
841 void example_data_free (void *user_data)
843 char *data = (char*) user_data;
856 bufmgr = tbm_bufmgr_init (bufmgr_fd);
857 bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
858 user_data = (char*) malloc (sizeof(char) * 128);
862 tbm_bo_add_user_data (bo, 1, example_data_free);
863 tbm_bo_set_user_data (bo, 1, user_data);
867 ret = tbm_bo_get_user_data (bo, 1, &get_data);
868 tbm_bo_delete_user_data (bo, 1);
873 tbm_bufmgr_deinit (bufmgr);
877 int tbm_bo_get_user_data (tbm_bo bo, unsigned long key, void** data);
883 #endif /* _TBM_BUFMGR_H_ */