OSDN Git Service

mesa/objectlabel: don't do memcpy if bufSize is 0 (v2)
[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 "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 (bufSize == 0) {
108       if (length)
109          *length = strlen(src);
110       return;
111    }
112    if (src)
113       labelLen = strlen(src);
114
115    if (dst) {
116       if (src) {
117          if (bufSize <= labelLen)
118             labelLen = bufSize - 1;
119
120          memcpy(dst, src, labelLen);
121       }
122
123       dst[labelLen] = '\0';
124    }
125
126    if (length)
127       *length = labelLen;
128 }
129
130 /**
131  * Helper for _mesa_ObjectLabel() and _mesa_GetObjectLabel().
132  */
133 static char **
134 get_label_pointer(struct gl_context *ctx, GLenum identifier, GLuint name,
135                   const char *caller)
136 {
137    char **labelPtr = NULL;
138
139    switch (identifier) {
140    case GL_BUFFER:
141       {
142          struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
143          if (bufObj)
144             labelPtr = &bufObj->Label;
145       }
146       break;
147    case GL_SHADER:
148       {
149          struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
150          if (shader)
151             labelPtr = &shader->Label;
152       }
153       break;
154    case GL_PROGRAM:
155       {
156          struct gl_shader_program *program =
157             _mesa_lookup_shader_program(ctx, name);
158          if (program)
159             labelPtr = &program->Label;
160       }
161       break;
162    case GL_VERTEX_ARRAY:
163       {
164          struct gl_vertex_array_object *obj = _mesa_lookup_vao(ctx, name);
165          if (obj)
166             labelPtr = &obj->Label;
167       }
168       break;
169    case GL_QUERY:
170       {
171          struct gl_query_object *query = _mesa_lookup_query_object(ctx, name);
172          if (query)
173             labelPtr = &query->Label;
174       }
175       break;
176    case GL_TRANSFORM_FEEDBACK:
177       {
178          struct gl_transform_feedback_object *tfo =
179             _mesa_lookup_transform_feedback_object(ctx, name);
180          if (tfo)
181             labelPtr = &tfo->Label;
182       }
183       break;
184    case GL_SAMPLER:
185       {
186          struct gl_sampler_object *so = _mesa_lookup_samplerobj(ctx, name);
187          if (so)
188             labelPtr = &so->Label;
189       }
190       break;
191    case GL_TEXTURE:
192       {
193          struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
194          if (texObj)
195             labelPtr = &texObj->Label;
196       }
197       break;
198    case GL_RENDERBUFFER:
199       {
200          struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
201          if (rb)
202             labelPtr = &rb->Label;
203       }
204       break;
205    case GL_FRAMEBUFFER:
206       {
207          struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, name);
208          if (rb)
209             labelPtr = &rb->Label;
210       }
211       break;
212    case GL_DISPLAY_LIST:
213       if (ctx->API == API_OPENGL_COMPAT) {
214          struct gl_display_list *list = _mesa_lookup_list(ctx, name);
215          if (list)
216             labelPtr = &list->Label;
217       }
218       else {
219          goto invalid_enum;
220       }
221       break;
222    case GL_PROGRAM_PIPELINE:
223       {
224          struct gl_pipeline_object *pipe =
225             _mesa_lookup_pipeline_object(ctx, name);
226          if (pipe)
227             labelPtr = &pipe->Label;
228       }
229       break;
230    default:
231       goto invalid_enum;
232    }
233
234    if (NULL == labelPtr) {
235       _mesa_error(ctx, GL_INVALID_VALUE, "%s(name = %u)", caller, name);
236    }
237
238    return labelPtr;
239
240 invalid_enum:
241    _mesa_error(ctx, GL_INVALID_ENUM, "%s(identifier = %s)",
242                caller, _mesa_enum_to_string(identifier));
243    return NULL;
244 }
245
246 void GLAPIENTRY
247 _mesa_ObjectLabel(GLenum identifier, GLuint name, GLsizei length,
248                   const GLchar *label)
249 {
250    GET_CURRENT_CONTEXT(ctx);
251    const char *callerstr;
252    char **labelPtr;
253
254    if (_mesa_is_desktop_gl(ctx))
255       callerstr = "glObjectLabel";
256    else
257       callerstr = "glObjectLabelKHR";
258
259    labelPtr = get_label_pointer(ctx, identifier, name, callerstr);
260    if (!labelPtr)
261       return;
262
263    set_label(ctx, labelPtr, label, length, callerstr);
264 }
265
266 void GLAPIENTRY
267 _mesa_GetObjectLabel(GLenum identifier, GLuint name, GLsizei bufSize,
268                      GLsizei *length, GLchar *label)
269 {
270    GET_CURRENT_CONTEXT(ctx);
271    const char *callerstr;
272    char **labelPtr;
273
274    if (_mesa_is_desktop_gl(ctx))
275       callerstr = "glGetObjectLabel";
276    else
277       callerstr = "glGetObjectLabelKHR";
278
279    if (bufSize < 0) {
280       _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize = %d)", callerstr,
281                   bufSize);
282       return;
283    }
284
285    labelPtr = get_label_pointer(ctx, identifier, name, callerstr);
286    if (!labelPtr)
287       return;
288
289    copy_label(*labelPtr, label, length, bufSize);
290 }
291
292 void GLAPIENTRY
293 _mesa_ObjectPtrLabel(const void *ptr, GLsizei length, const GLchar *label)
294 {
295    GET_CURRENT_CONTEXT(ctx);
296    struct gl_sync_object *syncObj;
297    const char *callerstr;
298    char **labelPtr;
299
300    syncObj = _mesa_get_and_ref_sync(ctx, (void*)ptr, true);
301
302    if (_mesa_is_desktop_gl(ctx))
303       callerstr = "glObjectPtrLabel";
304    else
305       callerstr = "glObjectPtrLabelKHR";
306
307    if (!syncObj) {
308       _mesa_error(ctx, GL_INVALID_VALUE, "%s (not a valid sync object)",
309                   callerstr);
310       return;
311    }
312
313    labelPtr = &syncObj->Label;
314
315    set_label(ctx, labelPtr, label, length, callerstr);
316    _mesa_unref_sync_object(ctx, syncObj, 1);
317 }
318
319 void GLAPIENTRY
320 _mesa_GetObjectPtrLabel(const void *ptr, GLsizei bufSize, GLsizei *length,
321                         GLchar *label)
322 {
323    GET_CURRENT_CONTEXT(ctx);
324    struct gl_sync_object *syncObj;
325    const char *callerstr;
326    char **labelPtr;
327
328    if (_mesa_is_desktop_gl(ctx))
329       callerstr = "glGetObjectPtrLabel";
330    else
331       callerstr = "glGetObjectPtrLabelKHR";
332
333    if (bufSize < 0) {
334       _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize = %d)", callerstr,
335                   bufSize);
336       return;
337    }
338
339    syncObj = _mesa_get_and_ref_sync(ctx, (void*)ptr, true);
340    if (!syncObj) {
341       _mesa_error(ctx, GL_INVALID_VALUE, "%s (not a valid sync object)",
342                   callerstr);
343       return;
344    }
345
346    labelPtr = &syncObj->Label;
347
348    copy_label(*labelPtr, label, length, bufSize);
349    _mesa_unref_sync_object(ctx, syncObj, 1);
350 }