Merge branch 'mesa_7_5_branch'
[mesa.git] / src / gallium / drivers / trace / tr_drm.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "state_tracker/drm_api.h"
29
30 #include "util/u_memory.h"
31 #include "trace/tr_drm.h"
32 #include "trace/tr_screen.h"
33 #include "trace/tr_context.h"
34 #include "trace/tr_buffer.h"
35 #include "trace/tr_texture.h"
36
37 struct trace_drm_api
38 {
39 struct drm_api base;
40
41 struct drm_api *api;
42 };
43
44 static INLINE struct trace_drm_api *
45 trace_drm_api(struct drm_api *_api)
46 {
47 return (struct trace_drm_api *)_api;
48 }
49
50 static struct pipe_screen *
51 trace_drm_create_screen(struct drm_api *_api, int fd,
52 struct drm_create_screen_arg *arg)
53 {
54 struct trace_drm_api *tr_api = trace_drm_api(_api);
55 struct drm_api *api = tr_api->api;
56 struct pipe_screen *screen;
57
58 /* TODO trace call */
59
60 if (arg && arg->mode != DRM_CREATE_NORMAL)
61 return NULL;
62
63 screen = api->create_screen(api, fd, arg);
64
65 return trace_screen_create(screen);
66 };
67
68 static struct pipe_context *
69 trace_drm_create_context(struct drm_api *_api,
70 struct pipe_screen *_screen)
71 {
72 struct trace_screen *tr_screen = trace_screen(_screen);
73 struct trace_drm_api *tr_api = trace_drm_api(_api);
74 struct pipe_screen *screen = tr_screen->screen;
75 struct drm_api *api = tr_api->api;
76 struct pipe_context *pipe;
77
78 /* TODO trace call */
79
80 pipe = api->create_context(api, screen);
81
82 pipe = trace_context_create(_screen, pipe);
83
84 return pipe;
85 };
86
87 static boolean
88 trace_drm_buffer_from_texture(struct drm_api *_api,
89 struct pipe_texture *_texture,
90 struct pipe_buffer **_buffer,
91 unsigned *stride)
92 {
93 struct trace_texture *tr_texture = trace_texture(_texture);
94 struct trace_drm_api *tr_api = trace_drm_api(_api);
95 struct pipe_texture *texture = tr_texture->texture;
96 struct drm_api *api = tr_api->api;
97 struct pipe_buffer *buffer = NULL;
98 boolean result;
99
100 /* TODO trace call */
101
102 result = api->buffer_from_texture(api, texture, &buffer, stride);
103
104 if (result && _buffer)
105 buffer = trace_buffer_create(trace_screen(texture->screen), buffer);
106
107 if (_buffer)
108 *_buffer = buffer;
109 else
110 pipe_buffer_reference(&buffer, NULL);
111
112 return result;
113 }
114
115 static struct pipe_buffer *
116 trace_drm_buffer_from_handle(struct drm_api *_api,
117 struct pipe_screen *_screen,
118 const char *name,
119 unsigned handle)
120 {
121 struct trace_screen *tr_screen = trace_screen(_screen);
122 struct trace_drm_api *tr_api = trace_drm_api(_api);
123 struct pipe_screen *screen = tr_screen->screen;
124 struct drm_api *api = tr_api->api;
125 struct pipe_buffer *result;
126
127 /* TODO trace call */
128
129 result = api->buffer_from_handle(api, screen, name, handle);
130
131 result = trace_buffer_create(trace_screen(_screen), result);
132
133 return result;
134 }
135
136 static boolean
137 trace_drm_handle_from_buffer(struct drm_api *_api,
138 struct pipe_screen *_screen,
139 struct pipe_buffer *_buffer,
140 unsigned *handle)
141 {
142 struct trace_screen *tr_screen = trace_screen(_screen);
143 struct trace_buffer *tr_buffer = trace_buffer(_buffer);
144 struct trace_drm_api *tr_api = trace_drm_api(_api);
145 struct pipe_screen *screen = tr_screen->screen;
146 struct pipe_buffer *buffer = tr_buffer->buffer;
147 struct drm_api *api = tr_api->api;
148
149 /* TODO trace call */
150
151 return api->handle_from_buffer(api, screen, buffer, handle);
152 }
153
154 static boolean
155 trace_drm_global_handle_from_buffer(struct drm_api *_api,
156 struct pipe_screen *_screen,
157 struct pipe_buffer *_buffer,
158 unsigned *handle)
159 {
160 struct trace_screen *tr_screen = trace_screen(_screen);
161 struct trace_buffer *tr_buffer = trace_buffer(_buffer);
162 struct trace_drm_api *tr_api = trace_drm_api(_api);
163 struct pipe_screen *screen = tr_screen->screen;
164 struct pipe_buffer *buffer = tr_buffer->buffer;
165 struct drm_api *api = tr_api->api;
166
167 /* TODO trace call */
168
169 return api->global_handle_from_buffer(api, screen, buffer, handle);
170 }
171
172 static void
173 trace_drm_destroy(struct drm_api *_api)
174 {
175 struct trace_drm_api *tr_api = trace_drm_api(_api);
176 struct drm_api *api = tr_api->api;
177 api->destroy(api);
178
179 free(tr_api);
180 }
181
182 struct drm_api *
183 trace_drm_create(struct drm_api *api)
184 {
185 struct trace_drm_api *tr_api;
186
187 if (!api)
188 goto error;
189
190 if (!trace_enabled())
191 goto error;
192
193 tr_api = CALLOC_STRUCT(trace_drm_api);
194
195 if (!tr_api)
196 goto error;
197
198 tr_api->base.create_screen = trace_drm_create_screen;
199 tr_api->base.create_context = trace_drm_create_context;
200 tr_api->base.buffer_from_texture = trace_drm_buffer_from_texture;
201 tr_api->base.buffer_from_handle = trace_drm_buffer_from_handle;
202 tr_api->base.handle_from_buffer = trace_drm_handle_from_buffer;
203 tr_api->base.global_handle_from_buffer = trace_drm_global_handle_from_buffer;
204 tr_api->base.destroy = trace_drm_destroy;
205 tr_api->api = api;
206
207 return &tr_api->base;
208
209 error:
210 return api;
211 }