• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // Debug.cpp: Defines debug state used for GL_KHR_debug
8 
9 #include "libANGLE/Debug.h"
10 
11 #include "common/debug.h"
12 
13 #include <algorithm>
14 #include <tuple>
15 
16 namespace
17 {
GLSeverityToString(GLenum severity)18 const char *GLSeverityToString(GLenum severity)
19 {
20     switch (severity)
21     {
22         case GL_DEBUG_SEVERITY_HIGH:
23             return "HIGH";
24         case GL_DEBUG_SEVERITY_MEDIUM:
25             return "MEDIUM";
26         case GL_DEBUG_SEVERITY_LOW:
27             return "LOW";
28         case GL_DEBUG_SEVERITY_NOTIFICATION:
29         default:
30             return "NOTIFICATION";
31     }
32 }
33 
EGLMessageTypeToString(egl::MessageType messageType)34 const char *EGLMessageTypeToString(egl::MessageType messageType)
35 {
36     switch (messageType)
37     {
38         case egl::MessageType::Critical:
39             return "CRITICAL";
40         case egl::MessageType::Error:
41             return "ERROR";
42         case egl::MessageType::Warn:
43             return "WARNING";
44         case egl::MessageType::Info:
45         default:
46             return "INFO";
47     }
48 }
49 
GLMessageTypeToString(GLenum type)50 const char *GLMessageTypeToString(GLenum type)
51 {
52     switch (type)
53     {
54         case GL_DEBUG_TYPE_ERROR:
55             return "error";
56         case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
57             return "deprecated behavior";
58         case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
59             return "undefined behavior";
60         case GL_DEBUG_TYPE_PORTABILITY:
61             return "portability";
62         case GL_DEBUG_TYPE_PERFORMANCE:
63             return "performance";
64         case GL_DEBUG_TYPE_MARKER:
65             return "marker";
66         case GL_DEBUG_TYPE_PUSH_GROUP:
67             return "start of group";
68         case GL_DEBUG_TYPE_POP_GROUP:
69             return "end of group";
70         case GL_DEBUG_TYPE_OTHER:
71         default:
72             return "other message";
73     }
74 }
75 }  // namespace
76 
77 namespace gl
78 {
79 
Control()80 Debug::Control::Control() {}
81 
~Control()82 Debug::Control::~Control() {}
83 
84 Debug::Control::Control(const Control &other) = default;
85 
Group()86 Debug::Group::Group() {}
87 
~Group()88 Debug::Group::~Group() {}
89 
90 Debug::Group::Group(const Group &other) = default;
91 
Debug(bool initialDebugState)92 Debug::Debug(bool initialDebugState)
93     : mOutputEnabled(initialDebugState),
94       mCallbackFunction(nullptr),
95       mCallbackUserParam(nullptr),
96       mMessages(),
97       mMaxLoggedMessages(0),
98       mOutputSynchronous(false),
99       mGroups()
100 {
101     pushDefaultGroup();
102 }
103 
~Debug()104 Debug::~Debug() {}
105 
setMaxLoggedMessages(GLuint maxLoggedMessages)106 void Debug::setMaxLoggedMessages(GLuint maxLoggedMessages)
107 {
108     mMaxLoggedMessages = maxLoggedMessages;
109 }
110 
setOutputEnabled(bool enabled)111 void Debug::setOutputEnabled(bool enabled)
112 {
113     mOutputEnabled = enabled;
114 }
115 
isOutputEnabled() const116 bool Debug::isOutputEnabled() const
117 {
118     return mOutputEnabled;
119 }
120 
setOutputSynchronous(bool synchronous)121 void Debug::setOutputSynchronous(bool synchronous)
122 {
123     mOutputSynchronous = synchronous;
124 }
125 
isOutputSynchronous() const126 bool Debug::isOutputSynchronous() const
127 {
128     return mOutputSynchronous;
129 }
130 
setCallback(GLDEBUGPROCKHR callback,const void * userParam)131 void Debug::setCallback(GLDEBUGPROCKHR callback, const void *userParam)
132 {
133     mCallbackFunction  = callback;
134     mCallbackUserParam = userParam;
135 }
136 
getCallback() const137 GLDEBUGPROCKHR Debug::getCallback() const
138 {
139     return mCallbackFunction;
140 }
141 
getUserParam() const142 const void *Debug::getUserParam() const
143 {
144     return mCallbackUserParam;
145 }
146 
insertMessage(GLenum source,GLenum type,GLuint id,GLenum severity,const std::string & message,gl::LogSeverity logSeverity) const147 void Debug::insertMessage(GLenum source,
148                           GLenum type,
149                           GLuint id,
150                           GLenum severity,
151                           const std::string &message,
152                           gl::LogSeverity logSeverity) const
153 {
154     std::string messageCopy(message);
155     insertMessage(source, type, id, severity, std::move(messageCopy), logSeverity);
156 }
157 
insertMessage(GLenum source,GLenum type,GLuint id,GLenum severity,std::string && message,gl::LogSeverity logSeverity) const158 void Debug::insertMessage(GLenum source,
159                           GLenum type,
160                           GLuint id,
161                           GLenum severity,
162                           std::string &&message,
163                           gl::LogSeverity logSeverity) const
164 {
165     {
166         // output all messages to the debug log
167         const char *messageTypeString = GLMessageTypeToString(type);
168         const char *severityString    = GLSeverityToString(severity);
169         std::ostringstream messageStream;
170         messageStream << "GL " << messageTypeString << ": " << severityString << ": " << message;
171         switch (logSeverity)
172         {
173             case gl::LOG_FATAL:
174                 FATAL() << messageStream.str();
175                 break;
176             case gl::LOG_ERR:
177                 ERR() << messageStream.str();
178                 break;
179             case gl::LOG_WARN:
180                 WARN() << messageStream.str();
181                 break;
182             case gl::LOG_INFO:
183                 INFO() << messageStream.str();
184                 break;
185             case gl::LOG_EVENT:
186                 ANGLE_LOG(EVENT) << messageStream.str();
187                 break;
188         }
189     }
190 
191     if (!isMessageEnabled(source, type, id, severity))
192     {
193         return;
194     }
195 
196     if (mCallbackFunction != nullptr)
197     {
198         // TODO(geofflang) Check the synchronous flag and potentially flush messages from another
199         // thread.
200         mCallbackFunction(source, type, id, severity, static_cast<GLsizei>(message.length()),
201                           message.c_str(), mCallbackUserParam);
202     }
203     else
204     {
205         if (mMessages.size() >= mMaxLoggedMessages)
206         {
207             // Drop messages over the limit
208             return;
209         }
210 
211         Message m;
212         m.source   = source;
213         m.type     = type;
214         m.id       = id;
215         m.severity = severity;
216         m.message  = std::move(message);
217 
218         mMessages.push_back(std::move(m));
219     }
220 }
221 
getMessages(GLuint count,GLsizei bufSize,GLenum * sources,GLenum * types,GLuint * ids,GLenum * severities,GLsizei * lengths,GLchar * messageLog)222 size_t Debug::getMessages(GLuint count,
223                           GLsizei bufSize,
224                           GLenum *sources,
225                           GLenum *types,
226                           GLuint *ids,
227                           GLenum *severities,
228                           GLsizei *lengths,
229                           GLchar *messageLog)
230 {
231     size_t messageCount       = 0;
232     size_t messageStringIndex = 0;
233     while (messageCount <= count && !mMessages.empty())
234     {
235         const Message &m = mMessages.front();
236 
237         if (messageLog != nullptr)
238         {
239             // Check that this message can fit in the message buffer
240             if (messageStringIndex + m.message.length() + 1 > static_cast<size_t>(bufSize))
241             {
242                 break;
243             }
244 
245             std::copy(m.message.begin(), m.message.end(), messageLog + messageStringIndex);
246             messageStringIndex += m.message.length();
247 
248             messageLog[messageStringIndex] = '\0';
249             messageStringIndex += 1;
250         }
251 
252         if (sources != nullptr)
253         {
254             sources[messageCount] = m.source;
255         }
256 
257         if (types != nullptr)
258         {
259             types[messageCount] = m.type;
260         }
261 
262         if (ids != nullptr)
263         {
264             ids[messageCount] = m.id;
265         }
266 
267         if (severities != nullptr)
268         {
269             severities[messageCount] = m.severity;
270         }
271 
272         if (lengths != nullptr)
273         {
274             lengths[messageCount] = static_cast<GLsizei>(m.message.length()) + 1;
275         }
276 
277         mMessages.pop_front();
278 
279         messageCount++;
280     }
281 
282     return messageCount;
283 }
284 
getNextMessageLength() const285 size_t Debug::getNextMessageLength() const
286 {
287     return mMessages.empty() ? 0 : mMessages.front().message.length() + 1;
288 }
289 
getMessageCount() const290 size_t Debug::getMessageCount() const
291 {
292     return mMessages.size();
293 }
294 
setMessageControl(GLenum source,GLenum type,GLenum severity,std::vector<GLuint> && ids,bool enabled)295 void Debug::setMessageControl(GLenum source,
296                               GLenum type,
297                               GLenum severity,
298                               std::vector<GLuint> &&ids,
299                               bool enabled)
300 {
301     Control c;
302     c.source   = source;
303     c.type     = type;
304     c.severity = severity;
305     c.ids      = std::move(ids);
306     c.enabled  = enabled;
307 
308     auto &controls = mGroups.back().controls;
309     controls.push_back(std::move(c));
310 }
311 
pushGroup(GLenum source,GLuint id,std::string && message)312 void Debug::pushGroup(GLenum source, GLuint id, std::string &&message)
313 {
314     insertMessage(source, GL_DEBUG_TYPE_PUSH_GROUP, id, GL_DEBUG_SEVERITY_NOTIFICATION,
315                   std::string(message), gl::LOG_INFO);
316 
317     Group g;
318     g.source  = source;
319     g.id      = id;
320     g.message = std::move(message);
321     mGroups.push_back(std::move(g));
322 }
323 
popGroup()324 void Debug::popGroup()
325 {
326     // Make sure the default group is not about to be popped
327     ASSERT(mGroups.size() > 1);
328 
329     Group g = mGroups.back();
330     mGroups.pop_back();
331 
332     insertMessage(g.source, GL_DEBUG_TYPE_POP_GROUP, g.id, GL_DEBUG_SEVERITY_NOTIFICATION,
333                   g.message, gl::LOG_INFO);
334 }
335 
getGroupStackDepth() const336 size_t Debug::getGroupStackDepth() const
337 {
338     return mGroups.size();
339 }
340 
insertPerfWarning(GLenum severity,const char * message,uint32_t * repeatCount) const341 void Debug::insertPerfWarning(GLenum severity, const char *message, uint32_t *repeatCount) const
342 {
343     bool repeatLast;
344 
345     {
346         constexpr uint32_t kMaxRepeat = 4;
347         std::lock_guard<std::mutex> lock(GetDebugMutex());
348 
349         if (*repeatCount >= kMaxRepeat)
350         {
351             return;
352         }
353 
354         ++*repeatCount;
355         repeatLast = (*repeatCount == kMaxRepeat);
356     }
357 
358     std::string msg = message;
359     if (repeatLast)
360     {
361         msg += " (this message will no longer repeat)";
362     }
363 
364     // Release the lock before we call insertMessage. It will re-acquire the lock.
365     insertMessage(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_PERFORMANCE, 0, severity, std::move(msg),
366                   gl::LOG_INFO);
367 }
368 
isMessageEnabled(GLenum source,GLenum type,GLuint id,GLenum severity) const369 bool Debug::isMessageEnabled(GLenum source, GLenum type, GLuint id, GLenum severity) const
370 {
371     if (!mOutputEnabled)
372     {
373         return false;
374     }
375 
376     for (auto groupIter = mGroups.rbegin(); groupIter != mGroups.rend(); groupIter++)
377     {
378         const auto &controls = groupIter->controls;
379         for (auto controlIter = controls.rbegin(); controlIter != controls.rend(); controlIter++)
380         {
381             const auto &control = *controlIter;
382 
383             if (control.source != GL_DONT_CARE && control.source != source)
384             {
385                 continue;
386             }
387 
388             if (control.type != GL_DONT_CARE && control.type != type)
389             {
390                 continue;
391             }
392 
393             if (control.severity != GL_DONT_CARE && control.severity != severity)
394             {
395                 continue;
396             }
397 
398             if (!control.ids.empty() &&
399                 std::find(control.ids.begin(), control.ids.end(), id) == control.ids.end())
400             {
401                 continue;
402             }
403 
404             return control.enabled;
405         }
406     }
407 
408     return true;
409 }
410 
pushDefaultGroup()411 void Debug::pushDefaultGroup()
412 {
413     Group g;
414     g.source  = GL_NONE;
415     g.id      = 0;
416     g.message = "";
417 
418     Control c0;
419     c0.source   = GL_DONT_CARE;
420     c0.type     = GL_DONT_CARE;
421     c0.severity = GL_DONT_CARE;
422     c0.enabled  = true;
423     g.controls.push_back(std::move(c0));
424 
425     Control c1;
426     c1.source   = GL_DONT_CARE;
427     c1.type     = GL_DONT_CARE;
428     c1.severity = GL_DEBUG_SEVERITY_LOW;
429     c1.enabled  = false;
430     g.controls.push_back(std::move(c1));
431 
432     mGroups.push_back(std::move(g));
433 }
434 }  // namespace gl
435 
436 namespace egl
437 {
438 
439 namespace
440 {
GetDefaultMessageTypeBits()441 angle::PackedEnumBitSet<MessageType> GetDefaultMessageTypeBits()
442 {
443     angle::PackedEnumBitSet<MessageType> result;
444     result.set(MessageType::Critical);
445     result.set(MessageType::Error);
446     return result;
447 }
448 }  // anonymous namespace
449 
Debug()450 Debug::Debug() : mCallback(nullptr), mEnabledMessageTypes(GetDefaultMessageTypeBits()) {}
451 
setCallback(EGLDEBUGPROCKHR callback,const AttributeMap & attribs)452 void Debug::setCallback(EGLDEBUGPROCKHR callback, const AttributeMap &attribs)
453 {
454     mCallback = callback;
455 
456     const angle::PackedEnumBitSet<MessageType> defaultMessageTypes = GetDefaultMessageTypeBits();
457     if (mCallback != nullptr)
458     {
459         for (MessageType messageType : angle::AllEnums<MessageType>())
460         {
461             mEnabledMessageTypes[messageType] =
462                 (attribs.getAsInt(egl::ToEGLenum(messageType), defaultMessageTypes[messageType]) ==
463                  EGL_TRUE);
464         }
465     }
466 }
467 
getCallback() const468 EGLDEBUGPROCKHR Debug::getCallback() const
469 {
470     return mCallback;
471 }
472 
isMessageTypeEnabled(MessageType type) const473 bool Debug::isMessageTypeEnabled(MessageType type) const
474 {
475     return mEnabledMessageTypes[type];
476 }
477 
insertMessage(EGLenum error,const char * command,MessageType messageType,EGLLabelKHR threadLabel,EGLLabelKHR objectLabel,const std::string & message) const478 void Debug::insertMessage(EGLenum error,
479                           const char *command,
480                           MessageType messageType,
481                           EGLLabelKHR threadLabel,
482                           EGLLabelKHR objectLabel,
483                           const std::string &message) const
484 {
485     {
486         // output all messages to the debug log
487         const char *messageTypeString = EGLMessageTypeToString(messageType);
488         std::ostringstream messageStream;
489         messageStream << "EGL " << messageTypeString << ": " << command << ": " << message;
490         INFO() << messageStream.str();
491     }
492 
493     // TODO(geofflang): Lock before checking the callback. http://anglebug.com/2464
494     if (mCallback && isMessageTypeEnabled(messageType))
495     {
496         mCallback(error, command, egl::ToEGLenum(messageType), threadLabel, objectLabel,
497                   message.c_str());
498     }
499 }
500 
501 }  // namespace egl
502