mesa: refactor _mesa_get_debug_state

Move gl_debug_state allocation to a new function, debug_create.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: refactor debug output should_log

Move the message filtering logic to debug_is_message_filtered.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: refactor debug output set_message_state

Move message state update to debug_set_message_filter.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: refactor debug out log_msg

Move message logging to debug_log_message.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: refactor debug output get_msg

Move message fetching to debug_fetch_message and message deletion to
debug_delete_messages.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: refactor debug output control_messages

Move most of the code to debug_set_default_filter.

v2: rename debug_set_default_filter to debug_set_message_filter_all

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: refactor _mesa_PushDebugGroup

Move group copying to debug_push_group.  Save group message in the group
before pushing instead of after.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: refactor _mesa_PopDebugGroup and _mesa_free_errors_data

Move group clearing to debug_clear_group.  Add debug_pop_group and
debug_destroy for use in _mesa_PopDebugGroup and _mesa_free_errors_data
respectively.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: refactor _mesa_GetDebugMessageLog

Merge get_msg into the function.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: refactor _mesa_DebugMessageControl

Merge set_message_state, control_messages, and control_app_messages into the
function.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: eliminate debug output should_log

In both call sites, it could be easily replaced by direct
debug_is_message_filtered calls.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: eliminate debug output message_insert

Add validate_length and use it and log_msg directly.

v2: make sure length is non-negative (i.e., known) before calling
    validate_length, noted by Timothy Arceri

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: use accessors for gl_debug_state

When GL_DEBUG_OUTPUT_SYNCHRONOUS is GL_TRUE, drivers are allowed to log debug
messages from other threads.  That requires gl_debug_state to be protected by
a mutex even that it belongs to the context.  While we do not spawn threads in
Mesa yet, this commit makes it easier when we want to.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: hide struct gl_debug_state

struct gl_context as well as the rest of the driver does not need to know the
details of struct gl_debug_state, as it is not allocated until used.  Unlike
other context states, the spec also allows it to be accessed from multiple
threads.  Having it opaque makes it harder to use it incorrectly.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: add struct gl_debug_log for the message log

Move members of struct gl_debug_state that belong to the message log to the
new struct gl_debug_log.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: add struct gl_debug_group for debug groups

Change gl_debug_state::Namespaces to gl_debug_state::Groups of type struct
gl_debug_group to reduce the dimension from three to one.

Move gl_debug_state::Defaults to struct gl_debug_namespace to reduce the
dimension from 4 to 1.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: refactor debug namespace functions

Add functions to provide these operations for a debug namespace:

  init():    initialize the namespace
  clear():   clear all elements in the namespace
  copy():    copy all elements to another namespace
  set():     set the value of an element
  set_all(): set the value of all elements
  get():     return the value of an element

A namespace is like a sparse array.  The length of the array is huge,
2^sizeof(GLuint), but most of the elements assume the same value sepcified by
set_all().

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: delay copying of debug groups

Do not copy the debug group until it is about to be written.  One likely
scenario of using glPushDebugGroup/glPopDebugGroup is to enclose a sequence of
GL commands and give them a human-readable description.  There is no message
control change in this scenario, and thus no need to copy.

This also reduces the initial size of gl_debug_state from 306KB to 7KB.

Signed-off-by: Chia-I Wu <olv@lunarg.com>
mesa: rework struct gl_debug_namespace

_mesa_HashTable is not well-suited for us: it locks a mutex unnecessary and it
does not accept 0 as the key (and have branches to handle 1 specially).  What
we really need is a sparse integer array.  Whether it should be implemented as
a hash table, a list, or a bsearch()-able array requires more investigations
of the use models.  We choose to implement it as a list for now, assuming it
is common to have a short list of IDs in each (source, type) namespace.

This also fixes KHR_debug/message-control piglit test.