2 * Mesa 3-D graphics library
4 * Copyright (C) 2013 Timothy Arceri All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
27 #include "bufferobj.h"
32 #include "objectlabel.h"
34 #include "samplerobj.h"
35 #include "shaderobj.h"
38 #include "transformfeedback.h"
42 * Helper for _mesa_ObjectLabel() and _mesa_ObjectPtrLabel().
45 set_label(struct gl_context *ctx, char **labelPtr, const char *label,
46 int length, const char *caller)
49 /* free old label string */
54 /* set new label string */
57 if (length >= MAX_LABEL_LENGTH)
58 _mesa_error(ctx, GL_INVALID_VALUE,
59 "%s(length=%d, which is not less than "
60 "GL_MAX_LABEL_LENGTH=%d)", caller, length,
64 *labelPtr = (char *) malloc(length+1);
66 memcpy(*labelPtr, label, length);
67 /* length is not required to include the null terminator so
68 * add one just in case
70 (*labelPtr)[length] = '\0';
74 int len = strlen(label);
75 if (len >= MAX_LABEL_LENGTH)
76 _mesa_error(ctx, GL_INVALID_VALUE,
77 "%s(label length=%d, which is not less than "
78 "GL_MAX_LABEL_LENGTH=%d)", caller, len,
81 /* null-terminated string */
82 *labelPtr = _mesa_strdup(label);
88 * Helper for _mesa_GetObjectLabel() and _mesa_GetObjectPtrLabel().
91 copy_label(char **labelPtr, char *label, int *length, int bufSize)
96 labelLen = strlen(*labelPtr);
99 if (bufSize <= labelLen)
100 labelLen = bufSize-1;
102 memcpy(label, *labelPtr, labelLen);
103 label[labelLen] = '\0';
111 * Helper for _mesa_ObjectLabel() and _mesa_GetObjectLabel().
114 get_label_pointer(struct gl_context *ctx, GLenum identifier, GLuint name,
117 char **labelPtr = NULL;
119 switch (identifier) {
122 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
124 labelPtr = &bufObj->Label;
129 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
131 labelPtr = &shader->Label;
136 struct gl_shader_program *program =
137 _mesa_lookup_shader_program(ctx, name);
139 labelPtr = &program->Label;
142 case GL_VERTEX_ARRAY:
144 struct gl_array_object *obj = _mesa_lookup_arrayobj(ctx, name);
146 labelPtr = &obj->Label;
151 struct gl_query_object *query = _mesa_lookup_query_object(ctx, name);
153 labelPtr = &query->Label;
156 case GL_TRANSFORM_FEEDBACK:
158 struct gl_transform_feedback_object *tfo =
159 _mesa_lookup_transform_feedback_object(ctx, name);
161 labelPtr = &tfo->Label;
166 struct gl_sampler_object *so = _mesa_lookup_samplerobj(ctx, name);
168 labelPtr = &so->Label;
173 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
175 labelPtr = &texObj->Label;
178 case GL_RENDERBUFFER:
180 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
182 labelPtr = &rb->Label;
187 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, name);
189 labelPtr = &rb->Label;
192 case GL_DISPLAY_LIST:
193 if (ctx->API == API_OPENGL_COMPAT) {
194 struct gl_display_list *list = _mesa_lookup_list(ctx, name);
196 labelPtr = &list->Label;
202 case GL_PROGRAM_PIPELINE:
203 /* requires GL 4.2 */
209 if (NULL == labelPtr) {
210 _mesa_error(ctx, GL_INVALID_VALUE, "glObjectLabel(name = %u)", name);
216 _mesa_error(ctx, GL_INVALID_ENUM, "%s(identifier = %s)",
217 caller, _mesa_lookup_enum_by_nr(identifier));
222 _mesa_ObjectLabel(GLenum identifier, GLuint name, GLsizei length,
225 GET_CURRENT_CONTEXT(ctx);
228 labelPtr = get_label_pointer(ctx, identifier, name, "glObjectLabel");
232 set_label(ctx, labelPtr, label, length, "glObjectLabel");
236 _mesa_GetObjectLabel(GLenum identifier, GLuint name, GLsizei bufSize,
237 GLsizei *length, GLchar *label)
239 GET_CURRENT_CONTEXT(ctx);
242 labelPtr = get_label_pointer(ctx, identifier, name, "glGetObjectLabel");
246 copy_label(labelPtr, label, length, bufSize);
250 _mesa_ObjectPtrLabel(const void *ptr, GLsizei length, const GLchar *label)
252 GET_CURRENT_CONTEXT(ctx);
254 struct gl_sync_object *const syncObj = (struct gl_sync_object *) ptr;
256 if (!_mesa_validate_sync(ctx, syncObj)) {
257 _mesa_error(ctx, GL_INVALID_VALUE, "glObjectPtrLabel (not a valid sync object)");
261 labelPtr = &syncObj->Label;
263 set_label(ctx, labelPtr, label, length, "glObjectPtrLabel");
267 _mesa_GetObjectPtrLabel(const void *ptr, GLsizei bufSize, GLsizei *length,
270 GET_CURRENT_CONTEXT(ctx);
272 struct gl_sync_object *const syncObj = (struct gl_sync_object *) ptr;
274 if (!_mesa_validate_sync(ctx, syncObj)) {
275 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectPtrLabel (not a valid sync object)");
279 labelPtr = &syncObj->Label;
281 copy_label(labelPtr, label, length, bufSize);