OSDN Git Service

mesa: Implement KHR_debug ObjectLabel functions
[android-x86/external-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 "queryobj.h"
34 #include "samplerobj.h"
35 #include "shaderobj.h"
36 #include "syncobj.h"
37 #include "texobj.h"
38 #include "transformfeedback.h"
39
40
41 /**
42  * Helper for _mesa_ObjectLabel() and _mesa_ObjectPtrLabel().
43  */
44 static void
45 set_label(struct gl_context *ctx, char **labelPtr, const char *label,
46           int length, const char *caller)
47 {
48    if (*labelPtr) {
49       /* free old label string */
50       free(*labelPtr);
51       *labelPtr = NULL;
52    }
53
54    /* set new label string */
55    if (label) {
56       if (length >= 0) {
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,
61                         MAX_LABEL_LENGTH);
62
63          /* explicit length */
64          *labelPtr = (char *) malloc(length+1);
65          if (*labelPtr) {
66             memcpy(*labelPtr, label, length);
67             /* length is not required to include the null terminator so
68              * add one just in case
69              */
70             (*labelPtr)[length] = '\0';
71          }
72       }
73       else {
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,
79                 MAX_LABEL_LENGTH);
80
81          /* null-terminated string */
82          *labelPtr = _mesa_strdup(label);
83       }
84    }
85 }
86
87 /**
88  * Helper for _mesa_GetObjectLabel() and _mesa_GetObjectPtrLabel().
89  */
90 static void
91 copy_label(char **labelPtr, char *label, int *length, int bufSize)
92 {
93    int labelLen = 0;
94
95    if (*labelPtr)
96       labelLen = strlen(*labelPtr);
97
98    if (label) {
99       if (bufSize <= labelLen)
100          labelLen =  bufSize-1;
101
102       memcpy(label, *labelPtr, labelLen);
103       label[labelLen] = '\0';
104    }
105
106    if (length)
107       *length = labelLen;
108 }
109
110 /**
111  * Helper for _mesa_ObjectLabel() and _mesa_GetObjectLabel().
112  */
113 static char **
114 get_label_pointer(struct gl_context *ctx, GLenum identifier, GLuint name,
115                   const char *caller)
116 {
117    char **labelPtr = NULL;
118
119    switch (identifier) {
120    case GL_BUFFER:
121       {
122          struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
123          if (bufObj)
124             labelPtr = &bufObj->Label;
125       }
126       break;
127    case GL_SHADER:
128       {
129          struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
130          if (shader)
131             labelPtr = &shader->Label;
132       }
133       break;
134    case GL_PROGRAM:
135       {
136          struct gl_shader_program *program =
137             _mesa_lookup_shader_program(ctx, name);
138          if (program)
139             labelPtr = &program->Label;
140       }
141       break;
142    case GL_VERTEX_ARRAY:
143       {
144          struct gl_array_object *obj = _mesa_lookup_arrayobj(ctx, name);
145          if (obj)
146             labelPtr = &obj->Label;
147       }
148       break;
149    case GL_QUERY:
150       {
151          struct gl_query_object *query = _mesa_lookup_query_object(ctx, name);
152          if (query)
153             labelPtr = &query->Label;
154       }
155       break;
156    case GL_TRANSFORM_FEEDBACK:
157       {
158          struct gl_transform_feedback_object *tfo =
159             _mesa_lookup_transform_feedback_object(ctx, name);
160          if (tfo)
161             labelPtr = &tfo->Label;
162       }
163       break;
164    case GL_SAMPLER:
165       {
166          struct gl_sampler_object *so = _mesa_lookup_samplerobj(ctx, name);
167          if (so)
168             labelPtr = &so->Label;
169       }
170       break;
171    case GL_TEXTURE:
172       {
173          struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
174          if (texObj)
175             labelPtr = &texObj->Label;
176       }
177       break;
178    case GL_RENDERBUFFER:
179       {
180          struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
181          if (rb)
182             labelPtr = &rb->Label;
183       }
184       break;
185    case GL_FRAMEBUFFER:
186       {
187          struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, name);
188          if (rb)
189             labelPtr = &rb->Label;
190       }
191       break;
192    case GL_DISPLAY_LIST:
193       if (ctx->API == API_OPENGL_COMPAT) {
194          struct gl_display_list *list = _mesa_lookup_list(ctx, name);
195          if (list)
196             labelPtr = &list->Label;
197       }
198       else {
199          goto invalid_enum;
200       }
201       break;
202    case GL_PROGRAM_PIPELINE:
203       /* requires GL 4.2 */
204       goto invalid_enum;
205    default:
206       goto invalid_enum;
207    }
208
209    if (NULL == labelPtr) {
210       _mesa_error(ctx, GL_INVALID_VALUE, "glObjectLabel(name = %u)", name);
211    }
212
213    return labelPtr;
214
215 invalid_enum:
216    _mesa_error(ctx, GL_INVALID_ENUM, "%s(identifier = %s)",
217                caller, _mesa_lookup_enum_by_nr(identifier));
218    return NULL;
219 }
220
221 void GLAPIENTRY
222 _mesa_ObjectLabel(GLenum identifier, GLuint name, GLsizei length,
223                   const GLchar *label)
224 {
225    GET_CURRENT_CONTEXT(ctx);
226    char **labelPtr;
227
228    labelPtr = get_label_pointer(ctx, identifier, name, "glObjectLabel");
229    if (!labelPtr)
230       return;
231
232    set_label(ctx, labelPtr, label, length, "glObjectLabel");
233 }
234
235 void GLAPIENTRY
236 _mesa_GetObjectLabel(GLenum identifier, GLuint name, GLsizei bufSize,
237                      GLsizei *length, GLchar *label)
238 {
239    GET_CURRENT_CONTEXT(ctx);
240    char **labelPtr;
241
242    labelPtr = get_label_pointer(ctx, identifier, name, "glGetObjectLabel");
243    if (!labelPtr)
244       return;
245
246    copy_label(labelPtr, label, length, bufSize);
247 }
248
249 void GLAPIENTRY
250 _mesa_ObjectPtrLabel(const void *ptr, GLsizei length, const GLchar *label)
251 {
252    GET_CURRENT_CONTEXT(ctx);
253    char **labelPtr;
254    struct gl_sync_object *const syncObj = (struct gl_sync_object *) ptr;
255
256    if (!_mesa_validate_sync(ctx, syncObj)) {
257       _mesa_error(ctx, GL_INVALID_VALUE, "glObjectPtrLabel (not a valid sync object)");
258       return;
259    }
260
261    labelPtr = &syncObj->Label;
262
263    set_label(ctx, labelPtr, label, length, "glObjectPtrLabel");
264 }
265
266 void GLAPIENTRY
267 _mesa_GetObjectPtrLabel(const void *ptr, GLsizei bufSize, GLsizei *length,
268                         GLchar *label)
269 {
270    GET_CURRENT_CONTEXT(ctx);
271    char **labelPtr;
272    struct gl_sync_object *const syncObj = (struct gl_sync_object *) ptr;
273
274    if (!_mesa_validate_sync(ctx, syncObj)) {
275       _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectPtrLabel (not a valid sync object)");
276       return;
277    }
278
279    labelPtr = &syncObj->Label;
280
281    copy_label(labelPtr, label, length, bufSize);
282 }