xref: /aosp_15_r20/external/mesa3d/src/mesa/main/debug_output.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2016  Brian Paul, et al   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 <stdarg.h>
27 #include <stdio.h>
28 #include "context.h"
29 #include "debug_output.h"
30 #include "enums.h"
31 
32 #include "hash.h"
33 #include "mtypes.h"
34 #include "version.h"
35 #include "util/hash_table.h"
36 #include "util/list.h"
37 #include "util/log.h"
38 #include "util/u_memory.h"
39 #include "api_exec_decl.h"
40 
41 #include "pipe/p_context.h"
42 
43 static GLuint PrevDynamicID = 0;
44 
45 
46 /**
47  * A namespace element.
48  */
49 struct gl_debug_element
50 {
51    struct list_head link;
52 
53    GLuint ID;
54    /* at which severity levels (mesa_debug_severity) is the message enabled */
55    GLbitfield State;
56 };
57 
58 
59 struct gl_debug_namespace
60 {
61    struct list_head Elements;
62    GLbitfield DefaultState;
63 };
64 
65 
66 struct gl_debug_group {
67    struct gl_debug_namespace Namespaces[MESA_DEBUG_SOURCE_COUNT][MESA_DEBUG_TYPE_COUNT];
68 };
69 
70 
71 /**
72  * An error, warning, or other piece of debug information for an application
73  * to consume via GL_ARB_debug_output/GL_KHR_debug.
74  */
75 struct gl_debug_message
76 {
77    enum mesa_debug_source source;
78    enum mesa_debug_type type;
79    GLuint id;
80    enum mesa_debug_severity severity;
81    /* length as given by the user - if message was explicitly null terminated,
82     * length can be negative */
83    GLsizei length;
84    GLcharARB *message;
85 };
86 
87 
88 /**
89  * Debug message log.  It works like a ring buffer.
90  */
91 struct gl_debug_log {
92    struct gl_debug_message Messages[MAX_DEBUG_LOGGED_MESSAGES];
93    GLint NextMessage;
94    GLint NumMessages;
95 };
96 
97 
98 struct gl_debug_state
99 {
100    GLDEBUGPROC Callback;
101    const void *CallbackData;
102    GLboolean SyncOutput;
103    GLboolean DebugOutput;
104    GLboolean LogToStderr;
105 
106    struct gl_debug_group *Groups[MAX_DEBUG_GROUP_STACK_DEPTH];
107    struct gl_debug_message GroupMessages[MAX_DEBUG_GROUP_STACK_DEPTH];
108    GLint CurrentGroup; // GroupStackDepth - 1
109 
110    struct gl_debug_log Log;
111 };
112 
113 
114 static char out_of_memory[] = "Debugging error: out of memory";
115 
116 static const GLenum debug_source_enums[] = {
117    GL_DEBUG_SOURCE_API,
118    GL_DEBUG_SOURCE_WINDOW_SYSTEM,
119    GL_DEBUG_SOURCE_SHADER_COMPILER,
120    GL_DEBUG_SOURCE_THIRD_PARTY,
121    GL_DEBUG_SOURCE_APPLICATION,
122    GL_DEBUG_SOURCE_OTHER,
123 };
124 
125 static const GLenum debug_type_enums[] = {
126    GL_DEBUG_TYPE_ERROR,
127    GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
128    GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
129    GL_DEBUG_TYPE_PORTABILITY,
130    GL_DEBUG_TYPE_PERFORMANCE,
131    GL_DEBUG_TYPE_OTHER,
132    GL_DEBUG_TYPE_MARKER,
133    GL_DEBUG_TYPE_PUSH_GROUP,
134    GL_DEBUG_TYPE_POP_GROUP,
135 };
136 
137 static const GLenum debug_severity_enums[] = {
138    GL_DEBUG_SEVERITY_LOW,
139    GL_DEBUG_SEVERITY_MEDIUM,
140    GL_DEBUG_SEVERITY_HIGH,
141    GL_DEBUG_SEVERITY_NOTIFICATION,
142 };
143 
144 
145 static enum mesa_debug_source
gl_enum_to_debug_source(GLenum e)146 gl_enum_to_debug_source(GLenum e)
147 {
148    unsigned i;
149 
150    for (i = 0; i < ARRAY_SIZE(debug_source_enums); i++) {
151       if (debug_source_enums[i] == e)
152          break;
153    }
154    return i;
155 }
156 
157 static enum mesa_debug_type
gl_enum_to_debug_type(GLenum e)158 gl_enum_to_debug_type(GLenum e)
159 {
160    unsigned i;
161 
162    for (i = 0; i < ARRAY_SIZE(debug_type_enums); i++) {
163       if (debug_type_enums[i] == e)
164          break;
165    }
166    return i;
167 }
168 
169 static enum mesa_debug_severity
gl_enum_to_debug_severity(GLenum e)170 gl_enum_to_debug_severity(GLenum e)
171 {
172    unsigned i;
173 
174    for (i = 0; i < ARRAY_SIZE(debug_severity_enums); i++) {
175       if (debug_severity_enums[i] == e)
176          break;
177    }
178    return i;
179 }
180 
181 
182 /**
183  * Handles generating a GL_ARB_debug_output message ID generated by the GL or
184  * GLSL compiler.
185  *
186  * The GL API has this "ID" mechanism, where the intention is to allow a
187  * client to filter in/out messages based on source, type, and ID.  Of course,
188  * building a giant enum list of all debug output messages that Mesa might
189  * generate is ridiculous, so instead we have our caller pass us a pointer to
190  * static storage where the ID should get stored.  This ID will be shared
191  * across all contexts for that message (which seems like a desirable
192  * property, even if it's not expected by the spec), but note that it won't be
193  * the same between executions if messages aren't generated in the same order.
194  */
195 void
_mesa_debug_get_id(GLuint * id)196 _mesa_debug_get_id(GLuint *id)
197 {
198    if (!(*id)) {
199       /* Don't update *id if we raced with some other thread. */
200       p_atomic_cmpxchg(id, 0, p_atomic_inc_return(&PrevDynamicID));
201    }
202 }
203 
204 static void
debug_message_clear(struct gl_debug_message * msg)205 debug_message_clear(struct gl_debug_message *msg)
206 {
207    if (msg->message != (char*)out_of_memory)
208       free(msg->message);
209    msg->message = NULL;
210    msg->length = 0;
211 }
212 
213 static void
debug_message_store(struct gl_debug_message * msg,enum mesa_debug_source source,enum mesa_debug_type type,GLuint id,enum mesa_debug_severity severity,GLsizei len,const char * buf)214 debug_message_store(struct gl_debug_message *msg,
215                     enum mesa_debug_source source,
216                     enum mesa_debug_type type, GLuint id,
217                     enum mesa_debug_severity severity,
218                     GLsizei len, const char *buf)
219 {
220    GLsizei length = len;
221 
222    assert(!msg->message && !msg->length);
223 
224    if (length < 0)
225       length = strlen(buf);
226 
227    msg->message = malloc(length+1);
228    if (msg->message) {
229       (void) strncpy(msg->message, buf, (size_t)length);
230       msg->message[length] = '\0';
231 
232       msg->length = len;
233       msg->source = source;
234       msg->type = type;
235       msg->id = id;
236       msg->severity = severity;
237    } else {
238       static GLuint oom_msg_id = 0;
239       _mesa_debug_get_id(&oom_msg_id);
240 
241       /* malloc failed! */
242       msg->message = out_of_memory;
243       msg->length = -1;
244       msg->source = MESA_DEBUG_SOURCE_OTHER;
245       msg->type = MESA_DEBUG_TYPE_ERROR;
246       msg->id = oom_msg_id;
247       msg->severity = MESA_DEBUG_SEVERITY_HIGH;
248    }
249 }
250 
251 static void
debug_namespace_init(struct gl_debug_namespace * ns)252 debug_namespace_init(struct gl_debug_namespace *ns)
253 {
254    list_inithead(&ns->Elements);
255 
256    /* Enable all the messages with severity HIGH or MEDIUM by default */
257    ns->DefaultState = (1 << MESA_DEBUG_SEVERITY_MEDIUM ) |
258                       (1 << MESA_DEBUG_SEVERITY_HIGH) |
259                       (1 << MESA_DEBUG_SEVERITY_NOTIFICATION);
260 }
261 
262 static void
debug_namespace_clear(struct gl_debug_namespace * ns)263 debug_namespace_clear(struct gl_debug_namespace *ns)
264 {
265    list_for_each_entry_safe(struct gl_debug_element, elem, &ns->Elements, link)
266       free(elem);
267 }
268 
269 static bool
debug_namespace_copy(struct gl_debug_namespace * dst,const struct gl_debug_namespace * src)270 debug_namespace_copy(struct gl_debug_namespace *dst,
271                      const struct gl_debug_namespace *src)
272 {
273    dst->DefaultState = src->DefaultState;
274 
275    list_inithead(&dst->Elements);
276    list_for_each_entry(struct gl_debug_element, elem, &src->Elements, link) {
277       struct gl_debug_element *copy;
278 
279       copy = malloc(sizeof(*copy));
280       if (!copy) {
281          debug_namespace_clear(dst);
282          return false;
283       }
284 
285       copy->ID = elem->ID;
286       copy->State = elem->State;
287       list_addtail(&copy->link, &dst->Elements);
288    }
289 
290    return true;
291 }
292 
293 /**
294  * Set the state of \p id in the namespace.
295  */
296 static bool
debug_namespace_set(struct gl_debug_namespace * ns,GLuint id,bool enabled)297 debug_namespace_set(struct gl_debug_namespace *ns,
298                     GLuint id, bool enabled)
299 {
300    const uint32_t state = (enabled) ?
301       ((1 << MESA_DEBUG_SEVERITY_COUNT) - 1) : 0;
302    struct gl_debug_element *elem = NULL;
303 
304    /* find the element */
305    list_for_each_entry(struct gl_debug_element, tmp, &ns->Elements, link) {
306       if (tmp->ID == id) {
307          elem = tmp;
308          break;
309       }
310    }
311 
312    /* we do not need the element if it has the default state */
313    if (ns->DefaultState == state) {
314       if (elem) {
315          list_del(&elem->link);
316          free(elem);
317       }
318       return true;
319    }
320 
321    if (!elem) {
322       elem = malloc(sizeof(*elem));
323       if (!elem)
324          return false;
325 
326       elem->ID = id;
327       list_addtail(&elem->link, &ns->Elements);
328    }
329 
330    elem->State = state;
331 
332    return true;
333 }
334 
335 /**
336  * Set the default state of the namespace for \p severity.  When \p severity
337  * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
338  * updated.
339  */
340 static void
debug_namespace_set_all(struct gl_debug_namespace * ns,enum mesa_debug_severity severity,bool enabled)341 debug_namespace_set_all(struct gl_debug_namespace *ns,
342                         enum mesa_debug_severity severity,
343                         bool enabled)
344 {
345    uint32_t mask, val;
346 
347    /* set all elements to the same state */
348    if (severity == MESA_DEBUG_SEVERITY_COUNT) {
349       ns->DefaultState = (enabled) ? ((1 << severity) - 1) : 0;
350       debug_namespace_clear(ns);
351       list_inithead(&ns->Elements);
352       return;
353    }
354 
355    mask = 1 << severity;
356    val = (enabled) ? mask : 0;
357 
358    ns->DefaultState = (ns->DefaultState & ~mask) | val;
359 
360    list_for_each_entry_safe(struct gl_debug_element, elem, &ns->Elements,
361                             link) {
362       elem->State = (elem->State & ~mask) | val;
363       if (elem->State == ns->DefaultState) {
364          list_del(&elem->link);
365          free(elem);
366       }
367    }
368 }
369 
370 /**
371  * Get the state of \p id in the namespace.
372  */
373 static bool
debug_namespace_get(const struct gl_debug_namespace * ns,GLuint id,enum mesa_debug_severity severity)374 debug_namespace_get(const struct gl_debug_namespace *ns, GLuint id,
375                     enum mesa_debug_severity severity)
376 {
377    uint32_t state;
378 
379    state = ns->DefaultState;
380    list_for_each_entry(struct gl_debug_element, elem, &ns->Elements, link) {
381       if (elem->ID == id) {
382          state = elem->State;
383          break;
384       }
385    }
386 
387    return (state & (1 << severity));
388 }
389 
390 /**
391  * Allocate and initialize context debug state.
392  */
393 static struct gl_debug_state *
debug_create(void)394 debug_create(void)
395 {
396    struct gl_debug_state *debug;
397    int s, t;
398 
399    debug = CALLOC_STRUCT(gl_debug_state);
400    if (!debug)
401       return NULL;
402 
403    debug->Groups[0] = malloc(sizeof(*debug->Groups[0]));
404    if (!debug->Groups[0]) {
405       free(debug);
406       return NULL;
407    }
408 
409    /* Initialize state for filtering known debug messages. */
410    for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
411       for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++)
412          debug_namespace_init(&debug->Groups[0]->Namespaces[s][t]);
413    }
414 
415    return debug;
416 }
417 
418 /**
419  * Return true if the top debug group points to the group below it.
420  */
421 static bool
debug_is_group_read_only(const struct gl_debug_state * debug)422 debug_is_group_read_only(const struct gl_debug_state *debug)
423 {
424    const GLint gstack = debug->CurrentGroup;
425    return (gstack > 0 && debug->Groups[gstack] == debug->Groups[gstack - 1]);
426 }
427 
428 /**
429  * Make the top debug group writable.
430  */
431 static bool
debug_make_group_writable(struct gl_debug_state * debug)432 debug_make_group_writable(struct gl_debug_state *debug)
433 {
434    const GLint gstack = debug->CurrentGroup;
435    const struct gl_debug_group *src = debug->Groups[gstack];
436    struct gl_debug_group *dst;
437    int s, t;
438 
439    if (!debug_is_group_read_only(debug))
440       return true;
441 
442    dst = malloc(sizeof(*dst));
443    if (!dst)
444       return false;
445 
446    for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
447       for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) {
448          if (!debug_namespace_copy(&dst->Namespaces[s][t],
449                                    &src->Namespaces[s][t])) {
450             /* error path! */
451             for (t = t - 1; t >= 0; t--)
452                debug_namespace_clear(&dst->Namespaces[s][t]);
453             for (s = s - 1; s >= 0; s--) {
454                for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++)
455                   debug_namespace_clear(&dst->Namespaces[s][t]);
456             }
457             free(dst);
458             return false;
459          }
460       }
461    }
462 
463    debug->Groups[gstack] = dst;
464 
465    return true;
466 }
467 
468 /**
469  * Free the top debug group.
470  */
471 static void
debug_clear_group(struct gl_debug_state * debug)472 debug_clear_group(struct gl_debug_state *debug)
473 {
474    const GLint gstack = debug->CurrentGroup;
475 
476    if (!debug_is_group_read_only(debug)) {
477       struct gl_debug_group *grp = debug->Groups[gstack];
478       int s, t;
479 
480       for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
481          for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++)
482             debug_namespace_clear(&grp->Namespaces[s][t]);
483       }
484 
485       free(grp);
486    }
487 
488    debug->Groups[gstack] = NULL;
489 }
490 
491 /**
492  * Delete the oldest debug messages out of the log.
493  */
494 static void
debug_delete_messages(struct gl_debug_state * debug,int count)495 debug_delete_messages(struct gl_debug_state *debug, int count)
496 {
497    struct gl_debug_log *log = &debug->Log;
498 
499    if (count > log->NumMessages)
500       count = log->NumMessages;
501 
502    while (count--) {
503       struct gl_debug_message *msg = &log->Messages[log->NextMessage];
504 
505       debug_message_clear(msg);
506 
507       log->NumMessages--;
508       log->NextMessage++;
509       log->NextMessage %= MAX_DEBUG_LOGGED_MESSAGES;
510    }
511 }
512 
513 /**
514  * Loop through debug group stack tearing down states for
515  * filtering debug messages.  Then free debug output state.
516  */
517 static void
debug_destroy(struct gl_debug_state * debug)518 debug_destroy(struct gl_debug_state *debug)
519 {
520    while (debug->CurrentGroup > 0) {
521       debug_clear_group(debug);
522       debug->CurrentGroup--;
523    }
524 
525    debug_clear_group(debug);
526    debug_delete_messages(debug, debug->Log.NumMessages);
527    free(debug);
528 }
529 
530 /**
531  * Sets the state of the given message source/type/ID tuple.
532  */
533 static void
debug_set_message_enable(struct gl_debug_state * debug,enum mesa_debug_source source,enum mesa_debug_type type,GLuint id,GLboolean enabled)534 debug_set_message_enable(struct gl_debug_state *debug,
535                          enum mesa_debug_source source,
536                          enum mesa_debug_type type,
537                          GLuint id, GLboolean enabled)
538 {
539    const GLint gstack = debug->CurrentGroup;
540    struct gl_debug_namespace *ns;
541 
542    debug_make_group_writable(debug);
543    ns = &debug->Groups[gstack]->Namespaces[source][type];
544 
545    debug_namespace_set(ns, id, enabled);
546 }
547 
548 /*
549  * Set the state of all message IDs found in the given intersection of
550  * 'source', 'type', and 'severity'.  The _COUNT enum can be used for
551  * GL_DONT_CARE (include all messages in the class).
552  *
553  * This requires both setting the state of all previously seen message
554  * IDs in the hash table, and setting the default state for all
555  * applicable combinations of source/type/severity, so that all the
556  * yet-unknown message IDs that may be used in the future will be
557  * impacted as if they were already known.
558  */
559 static void
debug_set_message_enable_all(struct gl_debug_state * debug,enum mesa_debug_source source,enum mesa_debug_type type,enum mesa_debug_severity severity,GLboolean enabled)560 debug_set_message_enable_all(struct gl_debug_state *debug,
561                              enum mesa_debug_source source,
562                              enum mesa_debug_type type,
563                              enum mesa_debug_severity severity,
564                              GLboolean enabled)
565 {
566    const GLint gstack = debug->CurrentGroup;
567    int s, t, smax, tmax;
568 
569    if (source == MESA_DEBUG_SOURCE_COUNT) {
570       source = 0;
571       smax = MESA_DEBUG_SOURCE_COUNT;
572    } else {
573       smax = source+1;
574    }
575 
576    if (type == MESA_DEBUG_TYPE_COUNT) {
577       type = 0;
578       tmax = MESA_DEBUG_TYPE_COUNT;
579    } else {
580       tmax = type+1;
581    }
582 
583    debug_make_group_writable(debug);
584 
585    for (s = source; s < smax; s++) {
586       for (t = type; t < tmax; t++) {
587          struct gl_debug_namespace *nspace =
588             &debug->Groups[gstack]->Namespaces[s][t];
589          debug_namespace_set_all(nspace, severity, enabled);
590       }
591    }
592 }
593 
594 /**
595  * Returns if the given message source/type/ID tuple is enabled.
596  */
597 bool
_mesa_debug_is_message_enabled(const struct gl_debug_state * debug,enum mesa_debug_source source,enum mesa_debug_type type,GLuint id,enum mesa_debug_severity severity)598 _mesa_debug_is_message_enabled(const struct gl_debug_state *debug,
599                                enum mesa_debug_source source,
600                                enum mesa_debug_type type,
601                                GLuint id,
602                                enum mesa_debug_severity severity)
603 {
604    const GLint gstack = debug->CurrentGroup;
605    struct gl_debug_group *grp = debug->Groups[gstack];
606    struct gl_debug_namespace *nspace = &grp->Namespaces[source][type];
607 
608    if (!debug->DebugOutput)
609       return false;
610 
611    return debug_namespace_get(nspace, id, severity);
612 }
613 
614 /**
615  * 'buf' is not necessarily a null-terminated string. When logging, copy
616  * 'len' characters from it, store them in a new, null-terminated string,
617  * and remember the number of bytes used by that string, *including*
618  * the null terminator this time.
619  */
620 static void
debug_log_message(struct gl_debug_state * debug,enum mesa_debug_source source,enum mesa_debug_type type,GLuint id,enum mesa_debug_severity severity,GLsizei len,const char * buf)621 debug_log_message(struct gl_debug_state *debug,
622                   enum mesa_debug_source source,
623                   enum mesa_debug_type type, GLuint id,
624                   enum mesa_debug_severity severity,
625                   GLsizei len, const char *buf)
626 {
627    struct gl_debug_log *log = &debug->Log;
628    GLint nextEmpty;
629    struct gl_debug_message *emptySlot;
630 
631    if (debug->LogToStderr) {
632       _mesa_log("Mesa debug output: %.*s\n", len, buf);
633    }
634 
635    assert(len < MAX_DEBUG_MESSAGE_LENGTH);
636 
637    if (log->NumMessages == MAX_DEBUG_LOGGED_MESSAGES)
638       return;
639 
640    nextEmpty = (log->NextMessage + log->NumMessages)
641       % MAX_DEBUG_LOGGED_MESSAGES;
642    emptySlot = &log->Messages[nextEmpty];
643 
644    debug_message_store(emptySlot, source, type,
645                        id, severity, len, buf);
646 
647    log->NumMessages++;
648 }
649 
650 /**
651  * Return the oldest debug message out of the log.
652  */
653 static const struct gl_debug_message *
debug_fetch_message(const struct gl_debug_state * debug)654 debug_fetch_message(const struct gl_debug_state *debug)
655 {
656    const struct gl_debug_log *log = &debug->Log;
657 
658    return (log->NumMessages) ? &log->Messages[log->NextMessage] : NULL;
659 }
660 
661 static struct gl_debug_message *
debug_get_group_message(struct gl_debug_state * debug)662 debug_get_group_message(struct gl_debug_state *debug)
663 {
664    return &debug->GroupMessages[debug->CurrentGroup];
665 }
666 
667 static void
debug_push_group(struct gl_debug_state * debug)668 debug_push_group(struct gl_debug_state *debug)
669 {
670    const GLint gstack = debug->CurrentGroup;
671 
672    /* just point to the previous stack */
673    debug->Groups[gstack + 1] = debug->Groups[gstack];
674    debug->CurrentGroup++;
675 }
676 
677 static void
debug_pop_group(struct gl_debug_state * debug)678 debug_pop_group(struct gl_debug_state *debug)
679 {
680    debug_clear_group(debug);
681    debug->CurrentGroup--;
682 }
683 
684 
685 /**
686  * Installed as util_debug_callback when GL_DEBUG_OUTPUT is enabled.
687  */
688 static void
_debug_message(void * data,unsigned * id,enum util_debug_type ptype,const char * fmt,va_list args)689 _debug_message(void *data,
690                unsigned *id,
691                enum util_debug_type ptype,
692                const char *fmt,
693                va_list args)
694 {
695    struct gl_context *ctx = data;
696    enum mesa_debug_source source;
697    enum mesa_debug_type type;
698    enum mesa_debug_severity severity;
699 
700    switch (ptype) {
701    case UTIL_DEBUG_TYPE_OUT_OF_MEMORY:
702       source = MESA_DEBUG_SOURCE_API;
703       type = MESA_DEBUG_TYPE_ERROR;
704       severity = MESA_DEBUG_SEVERITY_MEDIUM;
705       break;
706    case UTIL_DEBUG_TYPE_ERROR:
707       source = MESA_DEBUG_SOURCE_API;
708       type = MESA_DEBUG_TYPE_ERROR;
709       severity = MESA_DEBUG_SEVERITY_MEDIUM;
710       break;
711    case UTIL_DEBUG_TYPE_SHADER_INFO:
712       source = MESA_DEBUG_SOURCE_SHADER_COMPILER;
713       type = MESA_DEBUG_TYPE_OTHER;
714       severity = MESA_DEBUG_SEVERITY_NOTIFICATION;
715       break;
716    case UTIL_DEBUG_TYPE_PERF_INFO:
717       source = MESA_DEBUG_SOURCE_API;
718       type = MESA_DEBUG_TYPE_PERFORMANCE;
719       severity = MESA_DEBUG_SEVERITY_NOTIFICATION;
720       break;
721    case UTIL_DEBUG_TYPE_INFO:
722       source = MESA_DEBUG_SOURCE_API;
723       type = MESA_DEBUG_TYPE_OTHER;
724       severity = MESA_DEBUG_SEVERITY_NOTIFICATION;
725       break;
726    case UTIL_DEBUG_TYPE_FALLBACK:
727       source = MESA_DEBUG_SOURCE_API;
728       type = MESA_DEBUG_TYPE_PERFORMANCE;
729       severity = MESA_DEBUG_SEVERITY_NOTIFICATION;
730       break;
731    case UTIL_DEBUG_TYPE_CONFORMANCE:
732       source = MESA_DEBUG_SOURCE_API;
733       type = MESA_DEBUG_TYPE_OTHER;
734       severity = MESA_DEBUG_SEVERITY_NOTIFICATION;
735       break;
736    default:
737       unreachable("invalid debug type");
738    }
739    _mesa_gl_vdebugf(ctx, id, source, type, severity, fmt, args);
740 }
741 
742 void
_mesa_update_debug_callback(struct gl_context * ctx)743 _mesa_update_debug_callback(struct gl_context *ctx)
744 {
745    struct pipe_context *pipe = ctx->pipe;
746 
747    if (!pipe->set_debug_callback)
748       return;
749 
750    if (_mesa_get_debug_state_int(ctx, GL_DEBUG_OUTPUT)) {
751       struct util_debug_callback cb;
752       memset(&cb, 0, sizeof(cb));
753       cb.async = !_mesa_get_debug_state_int(ctx, GL_DEBUG_OUTPUT_SYNCHRONOUS);
754       cb.debug_message = _debug_message;
755       cb.data = ctx;
756       pipe->set_debug_callback(pipe, &cb);
757    } else {
758       pipe->set_debug_callback(pipe, NULL);
759    }
760 }
761 
762 /**
763  * Lock and return debug state for the context.  The debug state will be
764  * allocated and initialized upon the first call.  When NULL is returned, the
765  * debug state is not locked.
766  */
767 static struct gl_debug_state *
_mesa_lock_debug_state(struct gl_context * ctx)768 _mesa_lock_debug_state(struct gl_context *ctx)
769 {
770    simple_mtx_lock(&ctx->DebugMutex);
771 
772    if (!ctx->Debug) {
773       ctx->Debug = debug_create();
774       if (!ctx->Debug) {
775          GET_CURRENT_CONTEXT(cur);
776          simple_mtx_unlock(&ctx->DebugMutex);
777 
778          /*
779           * This function may be called from other threads.  When that is the
780           * case, we cannot record this OOM error.
781           */
782          if (ctx == cur)
783             _mesa_error(ctx, GL_OUT_OF_MEMORY, "allocating debug state");
784 
785          return NULL;
786       }
787    }
788 
789    return ctx->Debug;
790 }
791 
792 static void
_mesa_unlock_debug_state(struct gl_context * ctx)793 _mesa_unlock_debug_state(struct gl_context *ctx)
794 {
795    simple_mtx_unlock(&ctx->DebugMutex);
796 }
797 
798 /**
799  * Set the integer debug state specified by \p pname.  This can be called from
800  * _mesa_set_enable for example.
801  */
802 bool
_mesa_set_debug_state_int(struct gl_context * ctx,GLenum pname,GLint val)803 _mesa_set_debug_state_int(struct gl_context *ctx, GLenum pname, GLint val)
804 {
805    struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
806 
807    if (!debug)
808       return false;
809 
810    switch (pname) {
811    case GL_DEBUG_OUTPUT:
812       debug->DebugOutput = (val != 0);
813       break;
814    case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB:
815       debug->SyncOutput = (val != 0);
816       break;
817    default:
818       assert(!"unknown debug output param");
819       break;
820    }
821 
822    _mesa_unlock_debug_state(ctx);
823 
824    return true;
825 }
826 
827 /**
828  * Query the integer debug state specified by \p pname.  This can be called
829  * _mesa_GetIntegerv for example.
830  */
831 GLint
_mesa_get_debug_state_int(struct gl_context * ctx,GLenum pname)832 _mesa_get_debug_state_int(struct gl_context *ctx, GLenum pname)
833 {
834    GLint val;
835 
836    struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
837    if (!debug)
838       return 0;
839 
840    switch (pname) {
841    case GL_DEBUG_OUTPUT:
842       val = debug->DebugOutput;
843       break;
844    case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB:
845       val = debug->SyncOutput;
846       break;
847    case GL_DEBUG_LOGGED_MESSAGES:
848       val = debug->Log.NumMessages;
849       break;
850    case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH:
851       val = (debug->Log.NumMessages) ?
852          debug->Log.Messages[debug->Log.NextMessage].length + 1 : 0;
853       break;
854    case GL_DEBUG_GROUP_STACK_DEPTH:
855       val = debug->CurrentGroup + 1;
856       break;
857    default:
858       assert(!"unknown debug output param");
859       val = 0;
860       break;
861    }
862 
863    _mesa_unlock_debug_state(ctx);
864 
865    return val;
866 }
867 
868 /**
869  * Query the pointer debug state specified by \p pname.  This can be called
870  * _mesa_GetPointerv for example.
871  */
872 void *
_mesa_get_debug_state_ptr(struct gl_context * ctx,GLenum pname)873 _mesa_get_debug_state_ptr(struct gl_context *ctx, GLenum pname)
874 {
875    void *val;
876    struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
877 
878    if (!debug)
879       return NULL;
880 
881    switch (pname) {
882    case GL_DEBUG_CALLBACK_FUNCTION_ARB:
883       val = (void *) debug->Callback;
884       break;
885    case GL_DEBUG_CALLBACK_USER_PARAM_ARB:
886       val = (void *) debug->CallbackData;
887       break;
888    default:
889       assert(!"unknown debug output param");
890       val = NULL;
891       break;
892    }
893 
894    _mesa_unlock_debug_state(ctx);
895 
896    return val;
897 }
898 
899 /**
900  * Insert a debug message.  The mutex is assumed to be locked, and will be
901  * unlocked by this call.
902  */
903 static void
log_msg_locked_and_unlock(struct gl_context * ctx,enum mesa_debug_source source,enum mesa_debug_type type,GLuint id,enum mesa_debug_severity severity,GLint len,const char * buf)904 log_msg_locked_and_unlock(struct gl_context *ctx,
905                           enum mesa_debug_source source,
906                           enum mesa_debug_type type, GLuint id,
907                           enum mesa_debug_severity severity,
908                           GLint len, const char *buf)
909 {
910    struct gl_debug_state *debug = ctx->Debug;
911 
912    if (!_mesa_debug_is_message_enabled(debug, source, type, id, severity)) {
913       _mesa_unlock_debug_state(ctx);
914       return;
915    }
916 
917    if (ctx->Debug->Callback) {
918       /* Call the user's callback function */
919       GLenum gl_source = debug_source_enums[source];
920       GLenum gl_type = debug_type_enums[type];
921       GLenum gl_severity = debug_severity_enums[severity];
922       GLDEBUGPROC callback = ctx->Debug->Callback;
923       const void *data = ctx->Debug->CallbackData;
924 
925       /*
926        * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for
927        * unsynchronous calls.  When it is GL_TRUE, we will not spawn threads.
928        * In either case, we can call the callback unlocked.
929        */
930       _mesa_unlock_debug_state(ctx);
931       callback(gl_source, gl_type, id, gl_severity, len, buf, data);
932    }
933    else {
934       /* add debug message to queue */
935       debug_log_message(ctx->Debug, source, type, id, severity, len, buf);
936       _mesa_unlock_debug_state(ctx);
937    }
938 }
939 
940 /**
941  * Log a client or driver debug message.
942  */
943 void
_mesa_log_msg(struct gl_context * ctx,enum mesa_debug_source source,enum mesa_debug_type type,GLuint id,enum mesa_debug_severity severity,GLint len,const char * buf)944 _mesa_log_msg(struct gl_context *ctx, enum mesa_debug_source source,
945               enum mesa_debug_type type, GLuint id,
946               enum mesa_debug_severity severity, GLint len, const char *buf)
947 {
948    struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
949 
950    if (!debug)
951       return;
952 
953    log_msg_locked_and_unlock(ctx, source, type, id, severity, len, buf);
954 }
955 
956 
957 /**
958  * Verify that source, type, and severity are valid enums.
959  *
960  * The 'caller' param is used for handling values available
961  * only in glDebugMessageInsert or glDebugMessageControl
962  */
963 static GLboolean
validate_params(struct gl_context * ctx,unsigned caller,const char * callerstr,GLenum source,GLenum type,GLenum severity)964 validate_params(struct gl_context *ctx, unsigned caller,
965                 const char *callerstr, GLenum source, GLenum type,
966                 GLenum severity)
967 {
968 #define INSERT 1
969 #define CONTROL 2
970    switch(source) {
971    case GL_DEBUG_SOURCE_APPLICATION_ARB:
972    case GL_DEBUG_SOURCE_THIRD_PARTY_ARB:
973       break;
974    case GL_DEBUG_SOURCE_API_ARB:
975    case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB:
976    case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB:
977    case GL_DEBUG_SOURCE_OTHER_ARB:
978       if (caller != INSERT)
979          break;
980       else
981          goto error;
982    case GL_DONT_CARE:
983       if (caller == CONTROL)
984          break;
985       else
986          goto error;
987    default:
988       goto error;
989    }
990 
991    switch(type) {
992    case GL_DEBUG_TYPE_ERROR_ARB:
993    case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
994    case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
995    case GL_DEBUG_TYPE_PERFORMANCE_ARB:
996    case GL_DEBUG_TYPE_PORTABILITY_ARB:
997    case GL_DEBUG_TYPE_OTHER_ARB:
998    case GL_DEBUG_TYPE_MARKER:
999    case GL_DEBUG_TYPE_PUSH_GROUP:
1000    case GL_DEBUG_TYPE_POP_GROUP:
1001       break;
1002    case GL_DONT_CARE:
1003       if (caller == CONTROL)
1004          break;
1005       else
1006          goto error;
1007    default:
1008       goto error;
1009    }
1010 
1011    switch(severity) {
1012    case GL_DEBUG_SEVERITY_HIGH_ARB:
1013    case GL_DEBUG_SEVERITY_MEDIUM_ARB:
1014    case GL_DEBUG_SEVERITY_LOW_ARB:
1015    case GL_DEBUG_SEVERITY_NOTIFICATION:
1016       break;
1017    case GL_DONT_CARE:
1018       if (caller == CONTROL)
1019          break;
1020       else
1021          goto error;
1022    default:
1023       goto error;
1024    }
1025    return GL_TRUE;
1026 
1027 error:
1028    _mesa_error(ctx, GL_INVALID_ENUM, "bad values passed to %s"
1029                "(source=0x%x, type=0x%x, severity=0x%x)", callerstr,
1030                source, type, severity);
1031 
1032    return GL_FALSE;
1033 }
1034 
1035 
1036 static GLboolean
validate_length(struct gl_context * ctx,const char * callerstr,GLsizei length,const GLchar * buf)1037 validate_length(struct gl_context *ctx, const char *callerstr, GLsizei length,
1038                 const GLchar *buf)
1039 {
1040 
1041    if (length < 0) {
1042       GLsizei len = strlen(buf);
1043 
1044       if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
1045          _mesa_error(ctx, GL_INVALID_VALUE,
1046                     "%s(null terminated string length=%d, is not less than "
1047                     "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr, len,
1048                     MAX_DEBUG_MESSAGE_LENGTH);
1049          return GL_FALSE;
1050       }
1051    }
1052 
1053    if (length >= MAX_DEBUG_MESSAGE_LENGTH) {
1054       _mesa_error(ctx, GL_INVALID_VALUE,
1055                  "%s(length=%d, which is not less than "
1056                  "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr, length,
1057                  MAX_DEBUG_MESSAGE_LENGTH);
1058       return GL_FALSE;
1059    }
1060 
1061    return GL_TRUE;
1062 }
1063 
1064 
1065 void GLAPIENTRY
_mesa_DebugMessageInsert(GLenum source,GLenum type,GLuint id,GLenum severity,GLint length,const GLchar * buf)1066 _mesa_DebugMessageInsert(GLenum source, GLenum type, GLuint id,
1067                          GLenum severity, GLint length,
1068                          const GLchar *buf)
1069 {
1070    GET_CURRENT_CONTEXT(ctx);
1071    const char *callerstr;
1072 
1073    if (_mesa_is_desktop_gl(ctx))
1074       callerstr = "glDebugMessageInsert";
1075    else
1076       callerstr = "glDebugMessageInsertKHR";
1077 
1078    if (!validate_params(ctx, INSERT, callerstr, source, type, severity))
1079       return; /* GL_INVALID_ENUM */
1080 
1081    if (!validate_length(ctx, callerstr, length, buf))
1082       return; /* GL_INVALID_VALUE */
1083 
1084    /* if length not specified, string will be null terminated: */
1085    if (length < 0)
1086       length = strlen(buf);
1087 
1088    _mesa_log_msg(ctx, gl_enum_to_debug_source(source),
1089                  gl_enum_to_debug_type(type), id,
1090                  gl_enum_to_debug_severity(severity),
1091                  length, buf);
1092 
1093    if (type == GL_DEBUG_TYPE_MARKER && ctx->has_string_marker) {
1094       ctx->pipe->emit_string_marker(ctx->pipe, buf, length);
1095    }
1096 }
1097 
1098 
1099 GLuint GLAPIENTRY
_mesa_GetDebugMessageLog(GLuint count,GLsizei logSize,GLenum * sources,GLenum * types,GLenum * ids,GLenum * severities,GLsizei * lengths,GLchar * messageLog)1100 _mesa_GetDebugMessageLog(GLuint count, GLsizei logSize, GLenum *sources,
1101                          GLenum *types, GLenum *ids, GLenum *severities,
1102                          GLsizei *lengths, GLchar *messageLog)
1103 {
1104    GET_CURRENT_CONTEXT(ctx);
1105    struct gl_debug_state *debug;
1106    const char *callerstr;
1107    GLuint ret;
1108 
1109    if (_mesa_is_desktop_gl(ctx))
1110       callerstr = "glGetDebugMessageLog";
1111    else
1112       callerstr = "glGetDebugMessageLogKHR";
1113 
1114    if (!messageLog)
1115       logSize = 0;
1116 
1117    if (logSize < 0) {
1118       _mesa_error(ctx, GL_INVALID_VALUE,
1119                   "%s(logSize=%d : logSize must not be negative)",
1120                   callerstr, logSize);
1121       return 0;
1122    }
1123 
1124    debug = _mesa_lock_debug_state(ctx);
1125    if (!debug)
1126       return 0;
1127 
1128    for (ret = 0; ret < count; ret++) {
1129       const struct gl_debug_message *msg = debug_fetch_message(debug);
1130       GLsizei len;
1131 
1132       if (!msg)
1133          break;
1134 
1135       len = msg->length;
1136       if (len < 0)
1137          len = strlen(msg->message);
1138 
1139       if (logSize < len+1 && messageLog != NULL)
1140          break;
1141 
1142       if (messageLog) {
1143          assert(msg->message[len] == '\0');
1144          (void) strncpy(messageLog, msg->message, (size_t)len+1);
1145 
1146          messageLog += len+1;
1147          logSize -= len+1;
1148       }
1149 
1150       if (lengths)
1151          *lengths++ = len+1;
1152       if (severities)
1153          *severities++ = debug_severity_enums[msg->severity];
1154       if (sources)
1155          *sources++ = debug_source_enums[msg->source];
1156       if (types)
1157          *types++ = debug_type_enums[msg->type];
1158       if (ids)
1159          *ids++ = msg->id;
1160 
1161       debug_delete_messages(debug, 1);
1162    }
1163 
1164    _mesa_unlock_debug_state(ctx);
1165 
1166    return ret;
1167 }
1168 
1169 
1170 void GLAPIENTRY
_mesa_DebugMessageControl(GLenum gl_source,GLenum gl_type,GLenum gl_severity,GLsizei count,const GLuint * ids,GLboolean enabled)1171 _mesa_DebugMessageControl(GLenum gl_source, GLenum gl_type,
1172                           GLenum gl_severity, GLsizei count,
1173                           const GLuint *ids, GLboolean enabled)
1174 {
1175    GET_CURRENT_CONTEXT(ctx);
1176    enum mesa_debug_source source = gl_enum_to_debug_source(gl_source);
1177    enum mesa_debug_type type = gl_enum_to_debug_type(gl_type);
1178    enum mesa_debug_severity severity = gl_enum_to_debug_severity(gl_severity);
1179    const char *callerstr;
1180    struct gl_debug_state *debug;
1181 
1182    if (_mesa_is_desktop_gl(ctx))
1183       callerstr = "glDebugMessageControl";
1184    else
1185       callerstr = "glDebugMessageControlKHR";
1186 
1187    if (count < 0) {
1188       _mesa_error(ctx, GL_INVALID_VALUE,
1189                   "%s(count=%d : count must not be negative)", callerstr,
1190                   count);
1191       return;
1192    }
1193 
1194    if (!validate_params(ctx, CONTROL, callerstr, gl_source, gl_type,
1195                         gl_severity))
1196       return; /* GL_INVALID_ENUM */
1197 
1198    if (count && (gl_severity != GL_DONT_CARE || gl_type == GL_DONT_CARE
1199                  || gl_source == GL_DONT_CARE)) {
1200       _mesa_error(ctx, GL_INVALID_OPERATION,
1201                   "%s(When passing an array of ids, severity must be"
1202          " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1203                   callerstr);
1204       return;
1205    }
1206 
1207    debug = _mesa_lock_debug_state(ctx);
1208    if (!debug)
1209       return;
1210 
1211    if (count) {
1212       GLsizei i;
1213       for (i = 0; i < count; i++)
1214          debug_set_message_enable(debug, source, type, ids[i], enabled);
1215    }
1216    else {
1217       debug_set_message_enable_all(debug, source, type, severity, enabled);
1218    }
1219 
1220    _mesa_unlock_debug_state(ctx);
1221 }
1222 
1223 
1224 void GLAPIENTRY
_mesa_DebugMessageCallback(GLDEBUGPROC callback,const void * userParam)1225 _mesa_DebugMessageCallback(GLDEBUGPROC callback, const void *userParam)
1226 {
1227    GET_CURRENT_CONTEXT(ctx);
1228    struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
1229    if (debug) {
1230       debug->Callback = callback;
1231       debug->CallbackData = userParam;
1232       _mesa_unlock_debug_state(ctx);
1233    }
1234 }
1235 
1236 
1237 void GLAPIENTRY
_mesa_PushDebugGroup(GLenum source,GLuint id,GLsizei length,const GLchar * message)1238 _mesa_PushDebugGroup(GLenum source, GLuint id, GLsizei length,
1239                      const GLchar *message)
1240 {
1241    GET_CURRENT_CONTEXT(ctx);
1242    const char *callerstr;
1243    struct gl_debug_state *debug;
1244    struct gl_debug_message *emptySlot;
1245 
1246    if (_mesa_is_desktop_gl(ctx))
1247       callerstr = "glPushDebugGroup";
1248    else
1249       callerstr = "glPushDebugGroupKHR";
1250 
1251    switch(source) {
1252    case GL_DEBUG_SOURCE_APPLICATION:
1253    case GL_DEBUG_SOURCE_THIRD_PARTY:
1254       break;
1255    default:
1256       _mesa_error(ctx, GL_INVALID_ENUM, "bad value passed to %s"
1257                   "(source=0x%x)", callerstr, source);
1258       return;
1259    }
1260 
1261    if (!validate_length(ctx, callerstr, length, message))
1262       return; /* GL_INVALID_VALUE */
1263 
1264    if (length < 0)
1265       length = strlen(message);
1266 
1267    debug = _mesa_lock_debug_state(ctx);
1268    if (!debug)
1269       return;
1270 
1271    if (debug->CurrentGroup >= MAX_DEBUG_GROUP_STACK_DEPTH-1) {
1272       _mesa_unlock_debug_state(ctx);
1273       _mesa_error(ctx, GL_STACK_OVERFLOW, "%s", callerstr);
1274       return;
1275    }
1276 
1277    /* pop reuses the message details from push so we store this */
1278    emptySlot = debug_get_group_message(debug);
1279    debug_message_store(emptySlot,
1280                        gl_enum_to_debug_source(source),
1281                        gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP),
1282                        id,
1283                        gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION),
1284                        length, message);
1285 
1286    debug_push_group(debug);
1287 
1288    log_msg_locked_and_unlock(ctx,
1289          gl_enum_to_debug_source(source),
1290          MESA_DEBUG_TYPE_PUSH_GROUP, id,
1291          MESA_DEBUG_SEVERITY_NOTIFICATION, length,
1292          message);
1293 }
1294 
1295 
1296 void GLAPIENTRY
_mesa_PopDebugGroup(void)1297 _mesa_PopDebugGroup(void)
1298 {
1299    GET_CURRENT_CONTEXT(ctx);
1300    const char *callerstr;
1301    struct gl_debug_state *debug;
1302    struct gl_debug_message *gdmessage, msg;
1303 
1304    if (_mesa_is_desktop_gl(ctx))
1305       callerstr = "glPopDebugGroup";
1306    else
1307       callerstr = "glPopDebugGroupKHR";
1308 
1309    debug = _mesa_lock_debug_state(ctx);
1310    if (!debug)
1311       return;
1312 
1313    if (debug->CurrentGroup <= 0) {
1314       _mesa_unlock_debug_state(ctx);
1315       _mesa_error(ctx, GL_STACK_UNDERFLOW, "%s", callerstr);
1316       return;
1317    }
1318 
1319    debug_pop_group(debug);
1320 
1321    /* make a shallow copy */
1322    gdmessage = debug_get_group_message(debug);
1323    msg = *gdmessage;
1324    gdmessage->message = NULL;
1325    gdmessage->length = 0;
1326 
1327    log_msg_locked_and_unlock(ctx,
1328          msg.source,
1329          gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP),
1330          msg.id,
1331          gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION),
1332          msg.length, msg.message);
1333 
1334    debug_message_clear(&msg);
1335 }
1336 
1337 
1338 void
_mesa_init_debug_output(struct gl_context * ctx)1339 _mesa_init_debug_output(struct gl_context *ctx)
1340 {
1341    simple_mtx_init(&ctx->DebugMutex, mtx_plain);
1342 
1343    if (MESA_DEBUG_FLAGS & DEBUG_CONTEXT) {
1344       /* If the MESA_DEBUG env is set to "context", we'll turn on the
1345        * GL_CONTEXT_FLAG_DEBUG_BIT context flag and log debug output
1346        * messages to stderr (or whatever MESA_LOG_FILE points at).
1347        */
1348       struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
1349       if (!debug) {
1350          return;
1351       }
1352       debug->DebugOutput = GL_TRUE;
1353       debug->LogToStderr = GL_TRUE;
1354       ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
1355       _mesa_unlock_debug_state(ctx);
1356    }
1357 }
1358 
1359 
1360 void
_mesa_destroy_debug_output(struct gl_context * ctx)1361 _mesa_destroy_debug_output(struct gl_context *ctx)
1362 {
1363    if (ctx->Debug) {
1364       debug_destroy(ctx->Debug);
1365       /* set to NULL just in case it is used before context is completely gone. */
1366       ctx->Debug = NULL;
1367    }
1368 
1369    simple_mtx_destroy(&ctx->DebugMutex);
1370 }
1371 
1372 void GLAPIENTRY
_mesa_StringMarkerGREMEDY(GLsizei len,const GLvoid * string)1373 _mesa_StringMarkerGREMEDY(GLsizei len, const GLvoid *string)
1374 {
1375    GET_CURRENT_CONTEXT(ctx);
1376    if (ctx->Extensions.GREMEDY_string_marker) {
1377       /* if length not specified, string will be null terminated: */
1378       if (len <= 0)
1379          len = strlen(string);
1380       ctx->pipe->emit_string_marker(ctx->pipe, string, len);
1381    } else {
1382       _mesa_error(ctx, GL_INVALID_OPERATION, "StringMarkerGREMEDY");
1383    }
1384 }
1385