libdrm-radeon: new tracker tools
[platform/upstream/libdrm.git] / libdrm / radeon / radeon_bo.h
1 /* 
2  * Copyright © 2008 Jérôme Glisse
3  * All Rights Reserved.
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16  * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17  * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  */
26 /*
27  * Authors:
28  *      Jérôme Glisse <glisse@freedesktop.org>
29  */
30 #ifndef RADEON_BO_H
31 #define RADEON_BO_H
32
33 #include <stdio.h>
34 #include <stdint.h>
35 #include "radeon_track.h"
36
37 /* bo object */
38 #define RADEON_BO_FLAGS_MACRO_TILE  1
39 #define RADEON_BO_FLAGS_MICRO_TILE  2
40
41 struct radeon_bo_manager;
42
43 struct radeon_bo {
44     uint32_t                    alignment;
45     uint32_t                    handle;
46     uint32_t                    size;
47     uint32_t                    domains;
48     uint32_t                    flags;
49     unsigned                    cref;
50 #ifdef RADEON_BO_TRACK
51     struct radeon_track         *track;
52 #endif
53     void                        *ptr;
54     struct radeon_bo_manager    *bom;
55 };
56
57 /* bo functions */
58 struct radeon_bo_funcs {
59     struct radeon_bo *(*bo_open)(struct radeon_bo_manager *bom,
60                                  uint32_t handle,
61                                  uint32_t size,
62                                  uint32_t alignment,
63                                  uint32_t domains,
64                                  uint32_t flags);
65     void (*bo_ref)(struct radeon_bo *bo);
66     void (*bo_unref)(struct radeon_bo *bo);
67     int (*bo_map)(struct radeon_bo *bo, int write);
68     int (*bo_unmap)(struct radeon_bo *bo);
69 };
70
71 struct radeon_bo_manager {
72     struct radeon_bo_funcs  *funcs;
73     int                     fd;
74     struct radeon_tracker   tracker;
75 };
76     
77 static inline void _radeon_bo_debug(struct radeon_bo *bo,
78                                     int opcode,
79                                     const char *file,
80                                     const char *func,
81                                     int line)
82 {
83     fprintf(stderr, "%02d %p 0x%08X 0x%08X 0x%08X [%s %s %d]\n",
84             opcode, bo, bo->handle, bo->size, bo->cref, file, func, line);
85 }
86
87 static inline struct radeon_bo *_radeon_bo_open(struct radeon_bo_manager *bom,
88                                                 uint32_t handle,
89                                                 uint32_t size,
90                                                 uint32_t alignment,
91                                                 uint32_t domains,
92                                                 uint32_t flags,
93                                                 const char *file,
94                                                 const char *func,
95                                                 int line)
96 {
97     struct radeon_bo *bo;
98
99     bo = bom->funcs->bo_open(bom, handle, size, alignment, domains, flags);
100 #ifdef RADEON_BO_TRACK
101     if (bo) {
102         bo->track = radeon_tracker_add_track(&bom->tracker, bo->handle);
103         radeon_track_add_event(bo->track, file, func, "open", line);
104     }
105 #endif
106     return bo;
107 }
108
109 static inline void _radeon_bo_ref(struct radeon_bo *bo,
110                                   const char *file,
111                                   const char *func,
112                                   int line)
113 {
114     bo->cref++;
115 #ifdef RADEON_BO_TRACK
116     radeon_track_add_event(bo->track, file, func, "ref", line); 
117 #endif
118     bo->bom->funcs->bo_ref(bo);
119 }
120
121 static inline void _radeon_bo_unref(struct radeon_bo *bo,
122                                     const char *file,
123                                     const char *func,
124                                     int line)
125 {
126     bo->cref--;
127 #ifdef RADEON_BO_TRACK
128     radeon_track_add_event(bo->track, file, func, "unref", line);
129     if (bo->cref <= 0) {
130         radeon_tracker_remove_track(&bo->bom->tracker, bo->track);
131         bo->track = NULL;
132     }
133 #endif
134     bo->bom->funcs->bo_unref(bo);
135 }
136
137 static inline int _radeon_bo_map(struct radeon_bo *bo,
138                                  int write,
139                                  const char *file,
140                                  const char *func,
141                                  int line)
142 {
143     return bo->bom->funcs->bo_map(bo, write);
144 }
145
146 static inline int _radeon_bo_unmap(struct radeon_bo *bo,
147                                    const char *file,
148                                    const char *func,
149                                    int line)
150 {
151     return bo->bom->funcs->bo_unmap(bo);
152 }
153
154 #define radeon_bo_open(bom, h, s, a, d, f)\
155     _radeon_bo_open(bom, h, s, a, d, f, __FILE__, __FUNCTION__, __LINE__)
156 #define radeon_bo_ref(bo)\
157     _radeon_bo_ref(bo, __FILE__, __FUNCTION__, __LINE__)
158 #define radeon_bo_unref(bo)\
159     _radeon_bo_unref(bo, __FILE__, __FUNCTION__, __LINE__)
160 #define radeon_bo_map(bo, w)\
161     _radeon_bo_map(bo, w, __FILE__, __FUNCTION__, __LINE__)
162 #define radeon_bo_unmap(bo)\
163     _radeon_bo_unmap(bo, __FILE__, __FUNCTION__, __LINE__)
164 #define radeon_bo_debug(bo, opcode)\
165     _radeon_bo_debug(bo, opcode, __FILE__, __FUNCTION__, __LINE__)
166
167 #endif