mesa/objectlabel: handle NULL src string
[mesa.git] / src / mesa / main / objectlabel.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2013 Timothy Arceri 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 "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "arrayobj.h"
27 #include "bufferobj.h"
28 #include "context.h"
29 #include "dlist.h"
30 #include "enums.h"
31 #include "fbobject.h"
32 #include "objectlabel.h"
33 #include "pipelineobj.h"
34 #include "queryobj.h"
35 #include "samplerobj.h"
36 #include "shaderobj.h"
37 #include "syncobj.h"
38 #include "texobj.h"
39 #include "transformfeedback.h"
40
41
42 /**
43 * Helper for _mesa_ObjectLabel() and _mesa_ObjectPtrLabel().
44 */
45 static void
46 set_label(struct gl_context *ctx, char **labelPtr, const char *label,
47 int length, const char *caller)
48 {
49 free(*labelPtr);
50 *labelPtr = NULL;
51
52 /* set new label string */
53 if (label) {
54 if (length >= 0) {
55 if (length >= MAX_LABEL_LENGTH)
56 _mesa_error(ctx, GL_INVALID_VALUE,
57 "%s(length=%d, which is not less than "
58 "GL_MAX_LABEL_LENGTH=%d)", caller, length,
59 MAX_LABEL_LENGTH);
60
61 /* explicit length */
62 *labelPtr = malloc(length+1);
63 if (*labelPtr) {
64 memcpy(*labelPtr, label, length);
65 /* length is not required to include the null terminator so
66 * add one just in case
67 */
68 (*labelPtr)[length] = '\0';
69 }
70 }
71 else {
72 int len = strlen(label);
73 if (len >= MAX_LABEL_LENGTH)
74 _mesa_error(ctx, GL_INVALID_VALUE,
75 "%s(label length=%d, which is not less than "
76 "GL_MAX_LABEL_LENGTH=%d)", caller, len,
77 MAX_LABEL_LENGTH);
78
79 /* null-terminated string */
80 *labelPtr = strdup(label);
81 }
82 }
83 }
84
85 /**
86 * Helper for _mesa_GetObjectLabel() and _mesa_GetObjectPtrLabel().
87 * \param src the src label (may be null)
88 * \param dst pointer to dest buffer (may be null)
89 * \param length returns length of label (may be null)
90 * \param bufsize size of dst buffer
91 */
92 static void
93 copy_label(const GLchar *src, GLchar *dst, GLsizei *length, GLsizei bufSize)
94 {
95 int labelLen = 0;
96
97 /* From http://www.opengl.org/registry/specs/KHR/debug.txt:
98 * "If <length> is NULL, no length is returned. The maximum number of
99 * characters that may be written into <label>, including the null
100 * terminator, is specified by <bufSize>. If no debug label was specified
101 * for the object then <label> will contain a null-terminated empty string,
102 * and zero will be returned in <length>. If <label> is NULL and <length>
103 * is non-NULL then no string will be returned and the length of the label
104 * will be returned in <length>."
105 */
106
107 if (src)
108 labelLen = strlen(src);
109
110 if (bufSize == 0) {
111 if (length)
112 *length = labelLen;
113 return;
114 }
115
116 if (dst) {
117 if (src) {
118 if (bufSize <= labelLen)
119 labelLen = bufSize - 1;
120
121 memcpy(dst, src, labelLen);
122 }
123
124 dst[labelLen] = '\0';
125 }
126
127 if (length)
128 *length = labelLen;
129 }
130
131 /**
132 * Helper for _mesa_ObjectLabel() and _mesa_GetObjectLabel().
133 */
134 static char **
135 get_label_pointer(struct gl_context *ctx, GLenum identifier, GLuint name,
136 const char *caller)
137 {
138 char **labelPtr = NULL;
139
140 switch (identifier) {
141 case GL_BUFFER:
142 {
143 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
144 if (bufObj)
145 labelPtr = &bufObj->Label;
146 }
147 break;
148 case GL_SHADER:
149 {
150 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
151 if (shader)
152 labelPtr = &shader->Label;
153 }
154 break;
155 case GL_PROGRAM:
156 {
157 struct gl_shader_program *program =
158 _mesa_lookup_shader_program(ctx, name);
159 if (program)
160 labelPtr = &program->Label;
161 }
162 break;
163 case GL_VERTEX_ARRAY:
164 {
165 struct gl_vertex_array_object *obj = _mesa_lookup_vao(ctx, name);
166 if (obj)
167 labelPtr = &obj->Label;
168 }
169 break;
170 case GL_QUERY:
171 {
172 struct gl_query_object *query = _mesa_lookup_query_object(ctx, name);
173 if (query)
174 labelPtr = &query->Label;
175 }
176 break;
177 case GL_TRANSFORM_FEEDBACK:
178 {
179 struct gl_transform_feedback_object *tfo =
180 _mesa_lookup_transform_feedback_object(ctx, name);
181 if (tfo)
182 labelPtr = &tfo->Label;
183 }
184 break;
185 case GL_SAMPLER:
186 {
187 struct gl_sampler_object *so = _mesa_lookup_samplerobj(ctx, name);
188 if (so)
189 labelPtr = &so->Label;
190 }
191 break;
192 case GL_TEXTURE:
193 {
194 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
195 if (texObj)
196 labelPtr = &texObj->Label;
197 }
198 break;
199 case GL_RENDERBUFFER:
200 {
201 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
202 if (rb)
203 labelPtr = &rb->Label;
204 }
205 break;
206 case GL_FRAMEBUFFER:
207 {
208 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, name);
209 if (rb)
210 labelPtr = &rb->Label;
211 }
212 break;
213 case GL_DISPLAY_LIST:
214 if (ctx->API == API_OPENGL_COMPAT) {
215 struct gl_display_list *list = _mesa_lookup_list(ctx, name);
216 if (list)
217 labelPtr = &list->Label;
218 }
219 else {
220 goto invalid_enum;
221 }
222 break;
223 case GL_PROGRAM_PIPELINE:
224 {
225 struct gl_pipeline_object *pipe =
226 _mesa_lookup_pipeline_object(ctx, name);
227 if (pipe)
228 labelPtr = &pipe->Label;
229 }
230 break;
231 default:
232 goto invalid_enum;
233 }
234
235 if (NULL == labelPtr) {
236 _mesa_error(ctx, GL_INVALID_VALUE, "%s(name = %u)", caller, name);
237 }
238
239 return labelPtr;
240
241 invalid_enum:
242 _mesa_error(ctx, GL_INVALID_ENUM, "%s(identifier = %s)",
243 caller, _mesa_enum_to_string(identifier));
244 return NULL;
245 }
246
247 void GLAPIENTRY
248 _mesa_ObjectLabel(GLenum identifier, GLuint name, GLsizei length,
249 const GLchar *label)
250 {
251 GET_CURRENT_CONTEXT(ctx);
252 const char *callerstr;
253 char **labelPtr;
254
255 if (_mesa_is_desktop_gl(ctx))
256 callerstr = "glObjectLabel";
257 else
258 callerstr = "glObjectLabelKHR";
259
260 labelPtr = get_label_pointer(ctx, identifier, name, callerstr);
261 if (!labelPtr)
262 return;
263
264 set_label(ctx, labelPtr, label, length, callerstr);
265 }
266
267 void GLAPIENTRY
268 _mesa_GetObjectLabel(GLenum identifier, GLuint name, GLsizei bufSize,
269 GLsizei *length, GLchar *label)
270 {
271 GET_CURRENT_CONTEXT(ctx);
272 const char *callerstr;
273 char **labelPtr;
274
275 if (_mesa_is_desktop_gl(ctx))
276 callerstr = "glGetObjectLabel";
277 else
278 callerstr = "glGetObjectLabelKHR";
279
280 if (bufSize < 0) {
281 _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize = %d)", callerstr,
282 bufSize);
283 return;
284 }
285
286 labelPtr = get_label_pointer(ctx, identifier, name, callerstr);
287 if (!labelPtr)
288 return;
289
290 copy_label(*labelPtr, label, length, bufSize);
291 }
292
293 void GLAPIENTRY
294 _mesa_ObjectPtrLabel(const void *ptr, GLsizei length, const GLchar *label)
295 {
296 GET_CURRENT_CONTEXT(ctx);
297 struct gl_sync_object *syncObj;
298 const char *callerstr;
299 char **labelPtr;
300
301 syncObj = _mesa_get_and_ref_sync(ctx, (void*)ptr, true);
302
303 if (_mesa_is_desktop_gl(ctx))
304 callerstr = "glObjectPtrLabel";
305 else
306 callerstr = "glObjectPtrLabelKHR";
307
308 if (!syncObj) {
309 _mesa_error(ctx, GL_INVALID_VALUE, "%s (not a valid sync object)",
310 callerstr);
311 return;
312 }
313
314 labelPtr = &syncObj->Label;
315
316 set_label(ctx, labelPtr, label, length, callerstr);
317 _mesa_unref_sync_object(ctx, syncObj, 1);
318 }
319
320 void GLAPIENTRY
321 _mesa_GetObjectPtrLabel(const void *ptr, GLsizei bufSize, GLsizei *length,
322 GLchar *label)
323 {
324 GET_CURRENT_CONTEXT(ctx);
325 struct gl_sync_object *syncObj;
326 const char *callerstr;
327 char **labelPtr;
328
329 if (_mesa_is_desktop_gl(ctx))
330 callerstr = "glGetObjectPtrLabel";
331 else
332 callerstr = "glGetObjectPtrLabelKHR";
333
334 if (bufSize < 0) {
335 _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize = %d)", callerstr,
336 bufSize);
337 return;
338 }
339
340 syncObj = _mesa_get_and_ref_sync(ctx, (void*)ptr, true);
341 if (!syncObj) {
342 _mesa_error(ctx, GL_INVALID_VALUE, "%s (not a valid sync object)",
343 callerstr);
344 return;
345 }
346
347 labelPtr = &syncObj->Label;
348
349 copy_label(*labelPtr, label, length, bufSize);
350 _mesa_unref_sync_object(ctx, syncObj, 1);
351 }