libdebug: Add common debug utility.

- Add sdm and drm independent debug utility.

CRs-Fixed: 2211061
Change-Id: I48f9d8080ce05cff0ac0982f8e7123fec969e695
diff --git a/Android.mk b/Android.mk
index a9b1175..bf7dd6a 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,6 +1,6 @@
 ifneq ($(TARGET_DISABLE_DISPLAY),true)
 sdm-libs := sdm/libs
-display-hals := include libqservice libqdutils $(sdm-libs)/utils $(sdm-libs)/core
+display-hals := include libdebug libqservice libqdutils $(sdm-libs)/utils $(sdm-libs)/core
 
 ifneq ($(TARGET_IS_HEADLESS), true)
     display-hals += libcopybit liblight libmemtrack hdmi_cec \
diff --git a/libdebug/Android.mk b/libdebug/Android.mk
new file mode 100644
index 0000000..3e7ce25
--- /dev/null
+++ b/libdebug/Android.mk
@@ -0,0 +1,16 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE                  := libdisplaydebug
+LOCAL_VENDOR_MODULE           := true
+LOCAL_MODULE_TAGS             := optional
+LOCAL_C_INCLUDES              := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
+LOCAL_SHARED_LIBRARIES        := libdl
+LOCAL_CFLAGS                  := -DLOG_TAG=\"SDM\" -Wall -std=c++11 -Werror -fno-operator-names
+LOCAL_CLANG                   := true
+LOCAL_ADDITIONAL_DEPENDENCIES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
+LOCAL_SRC_FILES               := debug_handler.cpp
+LOCAL_COPY_HEADERS_TO         := qcom/display
+LOCAL_COPY_HEADERS            := debug_handler.h
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/libdebug/debug_handler.cpp b/libdebug/debug_handler.cpp
new file mode 100644
index 0000000..00c3ec9
--- /dev/null
+++ b/libdebug/debug_handler.cpp
@@ -0,0 +1,60 @@
+/*
+* Copyright (c) 2018, The Linux Foundation. All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are
+* met:
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above
+*       copyright notice, this list of conditions and the following
+*       disclaimer in the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of The Linux Foundation nor the names of its
+*       contributors may be used to endorse or promote products derived
+*       from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include "debug_handler.h"
+
+namespace display {
+
+// By default, drop any log messages/traces. It need to be overridden by client.
+class DefaultDebugHandler : public DebugHandler {
+ public:
+  virtual void Error(const char *, ...) { }
+  virtual void Warning(const char *, ...) { }
+  virtual void Info(const char *, ...) { }
+  virtual void Debug(const char *, ...) { }
+  virtual void Verbose(const char *, ...) { }
+  virtual void BeginTrace(const char *, const char *, const char *) { }
+  virtual void EndTrace() { }
+  virtual int GetProperty(const char *, int *) { return -1; }
+  virtual int GetProperty(const char *, char *) { return -1; }
+};
+
+DefaultDebugHandler g_default_debug_handler;
+DebugHandler * DebugHandler::debug_handler_ = &g_default_debug_handler;
+std::bitset<32> DebugHandler::log_mask_ = 0x1;  // Always print logs tagged with value 0
+
+void DebugHandler::Set(DebugHandler *debug_handler) {
+  if (debug_handler) {
+    debug_handler_ = debug_handler;
+  } else {
+    debug_handler_ = &g_default_debug_handler;
+  }
+}
+
+}  // namespace display
diff --git a/libdebug/debug_handler.h b/libdebug/debug_handler.h
new file mode 100644
index 0000000..0ae5c28
--- /dev/null
+++ b/libdebug/debug_handler.h
@@ -0,0 +1,101 @@
+/*
+* Copyright (c) 2018, The Linux Foundation. All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are
+* met:
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above
+*       copyright notice, this list of conditions and the following
+*       disclaimer in the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of The Linux Foundation nor the names of its
+*       contributors may be used to endorse or promote products derived
+*       from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef __DEBUG_HANDLER_H__
+#define __DEBUG_HANDLER_H__
+
+#include <bitset>
+
+#define DLOG(method, format, ...) \
+  display::DebugHandler::Get()->method(__CLASS__ "::%s: " format, __FUNCTION__, ##__VA_ARGS__)
+
+#define DLOG_IF(tag, method, format, ...) \
+  if (display::DebugHandler::GetLogMask()[tag]) { \
+    DLOG(method, format, ##__VA_ARGS__); \
+  }
+
+#define DLOGE_IF(tag, format, ...) DLOG_IF(tag, Error, format, ##__VA_ARGS__)
+#define DLOGW_IF(tag, format, ...) DLOG_IF(tag, Warning, format, ##__VA_ARGS__)
+#define DLOGI_IF(tag, format, ...) DLOG_IF(tag, Info, format, ##__VA_ARGS__)
+#define DLOGD_IF(tag, format, ...) DLOG_IF(tag, Debug, format, ##__VA_ARGS__)
+#define DLOGV_IF(tag, format, ...) DLOG_IF(tag, Verbose, format, ##__VA_ARGS__)
+
+#define DLOGE(format, ...) DLOG(Error, format, ##__VA_ARGS__)
+#define DLOGD(format, ...) DLOG(Warning, format, ##__VA_ARGS__)
+#define DLOGW(format, ...) DLOG(Info, format, ##__VA_ARGS__)
+#define DLOGI(format, ...) DLOG(Debug, format, ##__VA_ARGS__)
+#define DLOGV(format, ...) DLOG(Verbose, format, ##__VA_ARGS__)
+
+#define DTRACE_BEGIN(custom_string) display::DebugHandler::Get()->BeginTrace( \
+                                          __CLASS__, __FUNCTION__, custom_string)
+#define DTRACE_END() display::DebugHandler::Get()->EndTrace()
+#define DTRACE_SCOPED() display::ScopeTracer <display::DebugHandler> \
+                                          scope_tracer(__CLASS__, __FUNCTION__)
+
+namespace display {
+
+class DebugHandler {
+ public:
+  virtual void Error(const char *format, ...) = 0;
+  virtual void Warning(const char *format, ...) = 0;
+  virtual void Info(const char *format, ...) = 0;
+  virtual void Debug(const char *format, ...) = 0;
+  virtual void Verbose(const char *format, ...) = 0;
+  virtual void BeginTrace(const char *class_name, const char *function_name,
+                          const char *custom_string) = 0;
+  virtual void EndTrace() = 0;
+  virtual int GetProperty(const char *property_name, int *value) = 0;
+  virtual int GetProperty(const char *property_name, char *value) = 0;
+
+  static inline DebugHandler *Get() { return debug_handler_; }
+  static void Set(DebugHandler *debug_handler);
+  static inline std::bitset<32> & GetLogMask() { return log_mask_; }
+  static void SetLogMask(const std::bitset<32> &log_mask) { log_mask_ = log_mask; }
+
+ protected:
+  virtual ~DebugHandler() { }
+
+ private:
+  static DebugHandler *debug_handler_;
+  static std::bitset<32> log_mask_;
+};
+
+template <class T>
+class ScopeTracer {
+ public:
+  ScopeTracer(const char *class_name, const char *function_name) {
+    T::Get()->BeginTrace(class_name, function_name, "");
+  }
+
+  ~ScopeTracer() { T::Get()->EndTrace(); }
+};
+
+}  // namespace display
+
+#endif  // __DEBUG_HANDLER_H__