Android: Introduce ANDROID_HOST_BUILD and rework logging.

Avoid using the Android logger directly. Instead, use the Common/Debug
and OpenGL/common/debug paths instead, which abstracts away use of the
logger.

Add ANDRIOD_HOST_BUILD to tell the build we are building an Android
swiftshader, but minimizing the use of platform features such as the
logger.

Change-Id: Ic6c70843d947c568d0e29fe66c55af74b8559a59
Reviewed-on: https://swiftshader-review.googlesource.com/18028
Tested-by: Alistair Strachan <astrachan@google.com>
Reviewed-by: Greg Hartman <ghartman@google.com>
Reviewed-by: Lingfeng Yang <lfy@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Common/Debug.hpp b/src/Common/Debug.hpp
index 5ccc35a..436854c 100644
--- a/src/Common/Debug.hpp
+++ b/src/Common/Debug.hpp
@@ -15,7 +15,7 @@
 #ifndef Debug_hpp
 #define Debug_hpp
 
-#ifdef __ANDROID__
+#if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD)
 #include "DebugAndroid.hpp"
 #else
 
diff --git a/src/Common/GrallocAndroid.cpp b/src/Common/GrallocAndroid.cpp
index 4f87368..c877e9933 100644
--- a/src/Common/GrallocAndroid.cpp
+++ b/src/Common/GrallocAndroid.cpp
@@ -13,8 +13,11 @@
 // limitations under the License.
 
 #include "GrallocAndroid.hpp"
+#include "Debug.hpp"
 
-#include <cutils/log.h>
+#ifdef HAVE_GRALLOC1
+#include <sync/sync.h>
+#endif
 
 GrallocModule *GrallocModule::getInstance()
 {
@@ -41,7 +44,63 @@
 		break;
 #endif
 	default:
-		ALOGE("unknown gralloc major version (%d)", m_major_version);
+		TRACE("unknown gralloc major version (%d)", m_major_version);
 		break;
 	}
 }
+
+int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr)
+{
+	switch(m_major_version)
+	{
+	case 0:
+		{
+			return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr);
+		}
+	case 1:
+#ifdef HAVE_GRALLOC1
+		{
+			gralloc1_rect_t outRect{};
+			outRect.left = left;
+			outRect.top = top;
+			outRect.width = width;
+			outRect.height = height;
+			return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1);
+		}
+#endif
+	default:
+		{
+			TRACE("no gralloc module to lock");
+			return -1;
+		}
+	}
+}
+
+int GrallocModule::unlock(buffer_handle_t handle)
+{
+	switch(m_major_version)
+	{
+	case 0:
+		{
+			return m_module->unlock(m_module, handle);
+		}
+	case 1:
+#ifdef HAVE_GRALLOC1
+		{
+			int32_t fenceFd = -1;
+			int error = m_gralloc1_unlock(m_gralloc1_device, handle, &fenceFd);
+			if (!error)
+			{
+				sync_wait(fenceFd, -1);
+				close(fenceFd);
+			}
+			return error;
+		}
+#endif
+	default:
+		{
+			TRACE("no gralloc module to unlock");
+			return -1;
+		}
+	}
+}
diff --git a/src/Common/GrallocAndroid.hpp b/src/Common/GrallocAndroid.hpp
index 3ebb5d7..fe0b15a 100644
--- a/src/Common/GrallocAndroid.hpp
+++ b/src/Common/GrallocAndroid.hpp
@@ -16,11 +16,9 @@
 #define GRALLOC_ANDROID
 
 #include <hardware/gralloc.h>
-#include <cutils/log.h>
 
 #ifdef HAVE_GRALLOC1
 #include <hardware/gralloc1.h>
-#include <sync/sync.h>
 #endif
 
 #include <unistd.h> // for close()
@@ -29,61 +27,8 @@
 {
 public:
 	static GrallocModule *getInstance();
-	int lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr)
-	{
-		switch(m_major_version)
-		{
-		case 0:
-			{
-				return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr);
-			}
-		case 1:
-#ifdef HAVE_GRALLOC1
-			{
-				gralloc1_rect_t outRect{};
-				outRect.left = left;
-				outRect.top = top;
-				outRect.width = width;
-				outRect.height = height;
-				return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1);
-			}
-#endif
-		default:
-			{
-				ALOGE("no gralloc module to lock");
-				return -1;
-			}
-		}
-	}
-
-	int unlock(buffer_handle_t handle)
-	{
-		switch(m_major_version)
-		{
-		case 0:
-			{
-				return m_module->unlock(m_module, handle);
-			}
-		case 1:
-#ifdef HAVE_GRALLOC1
-			{
-				int32_t fenceFd = -1;
-				int error = m_gralloc1_unlock(m_gralloc1_device, handle, &fenceFd);
-				if (!error)
-				{
-					sync_wait(fenceFd, -1);
-					close(fenceFd);
-				}
-				return error;
-			}
-#endif
-		default:
-			{
-				ALOGE("no gralloc module to unlock");
-				return -1;
-			}
-		}
-	}
+	int lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr);
+	int unlock(buffer_handle_t handle);
 
 private:
 	GrallocModule();
diff --git a/src/Main/FrameBuffer.cpp b/src/Main/FrameBuffer.cpp
index 82dff46..7a8ddc1 100644
--- a/src/Main/FrameBuffer.cpp
+++ b/src/Main/FrameBuffer.cpp
@@ -23,10 +23,6 @@
 #include <string.h>
 #include <time.h>
 
-#ifdef __ANDROID__
-#include <cutils/properties.h>
-#endif
-
 #define ASYNCHRONOUS_BLIT false   // FIXME: Currently leads to rare race conditions
 
 namespace sw
diff --git a/src/Main/FrameBufferAndroid.cpp b/src/Main/FrameBufferAndroid.cpp
index 9b47171..0ae5f09 100644
--- a/src/Main/FrameBufferAndroid.cpp
+++ b/src/Main/FrameBufferAndroid.cpp
@@ -17,7 +17,6 @@
 #include "Common/GrallocAndroid.hpp"
 
 #include <system/window.h>
-#include <cutils/log.h>
 
 namespace sw
 {
@@ -88,14 +87,14 @@
 		                 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
 		                 0, 0, buffer->width, buffer->height, &framebuffer) != 0)
 		{
-			ALOGE("%s failed to lock buffer %p", __FUNCTION__, buffer);
+			TRACE("%s failed to lock buffer %p", __FUNCTION__, buffer);
 			return nullptr;
 		}
 
 		if((buffer->width < width) || (buffer->height < height))
 		{
-			ALOGI("lock failed: buffer of %dx%d too small for window of %dx%d",
-				  buffer->width, buffer->height, width, height);
+			TRACE("lock failed: buffer of %dx%d too small for window of %dx%d",
+			      buffer->width, buffer->height, width, height);
 			return nullptr;
 		}
 
@@ -110,11 +109,11 @@
 		case HAL_PIXEL_FORMAT_BGRA_8888: format = FORMAT_A8R8G8B8; break;
 		case HAL_PIXEL_FORMAT_RGB_888:
 			// Frame buffers are expected to have 16-bit or 32-bit colors, not 24-bit.
-			ALOGE("Unsupported frame buffer format RGB_888"); ASSERT(false);
+			TRACE("Unsupported frame buffer format RGB_888"); ASSERT(false);
 			format = FORMAT_R8G8B8;   // Wrong component order.
 			break;
 		default:
-			ALOGE("Unsupported frame buffer format %d", buffer->format); ASSERT(false);
+			TRACE("Unsupported frame buffer format %d", buffer->format); ASSERT(false);
 			format = FORMAT_NULL;
 			break;
 		}
@@ -127,7 +126,7 @@
 	{
 		if(!buffer)
 		{
-			ALOGE("%s: badness unlock with no active buffer", __FUNCTION__);
+			TRACE("%s: badness unlock with no active buffer", __FUNCTION__);
 			return;
 		}
 
@@ -135,7 +134,7 @@
 
 		if(GrallocModule::getInstance()->unlock(buffer->handle) != 0)
 		{
-			ALOGE("%s: badness unlock failed", __FUNCTION__);
+			TRACE("%s: badness unlock failed", __FUNCTION__);
 		}
 	}
 }
diff --git a/src/OpenGL/common/Image.hpp b/src/OpenGL/common/Image.hpp
index a744763..f115a63 100644
--- a/src/OpenGL/common/Image.hpp
+++ b/src/OpenGL/common/Image.hpp
@@ -24,8 +24,11 @@
 #if defined(__ANDROID__)
 #include <system/window.h>
 #include "../../Common/GrallocAndroid.hpp"
+#endif
+
+#if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD)
 #include "../../Common/DebugAndroid.hpp"
-#define LOGLOCK(fmt, ...) // ALOGI(fmt " tid=%d", ##__VA_ARGS__, gettid())
+#define LOGLOCK(fmt, ...) // TRACE(fmt " tid=%d", ##__VA_ARGS__, gettid())
 #else
 #include <assert.h>
 #define LOGLOCK(...)
@@ -255,7 +258,7 @@
 #endif
 	case HAL_PIXEL_FORMAT_RGB_888:   // Unsupported.
 	default:
-		ALOGE("Unsupported EGL image format %d", halFormat); ASSERT(false);
+		ERR("Unsupported EGL image format %d", halFormat); ASSERT(false);
 		return GL_NONE;
 	}
 }
@@ -293,7 +296,7 @@
 		{
 			if(x != 0 || y != 0 || z != 0)
 			{
-				ALOGI("badness: %s called with unsupported parms: image=%p x=%d y=%d z=%d", __FUNCTION__, this, x, y, z);
+				TRACE("badness: %s called with unsupported parms: image=%p x=%d y=%d z=%d", __FUNCTION__, this, x, y, z);
 			}
 
 			LOGLOCK("image=%p op=%s.ani lock=%d", this, __FUNCTION__, lock);
diff --git a/src/OpenGL/common/debug.cpp b/src/OpenGL/common/debug.cpp
index a7ced89..3ef2885 100644
--- a/src/OpenGL/common/debug.cpp
+++ b/src/OpenGL/common/debug.cpp
@@ -26,8 +26,8 @@
 
 namespace es
 {
-#ifdef __ANDROID__
-	void output(const char *format, va_list vararg)
+#if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD)
+	static void output(const char *format, va_list vararg)
 	{
 		ALOGI("%s", android::String8::formatV(format, vararg).string());
 	}
diff --git a/src/OpenGL/common/debug.h b/src/OpenGL/common/debug.h
index aadb535..a5653b7 100644
--- a/src/OpenGL/common/debug.h
+++ b/src/OpenGL/common/debug.h
@@ -17,7 +17,7 @@
 #ifndef COMMON_DEBUG_H_
 #define COMMON_DEBUG_H_
 
-#ifdef __ANDROID__
+#if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD)
 #include "../../Common/DebugAndroid.hpp"
 #else
 #include <stdio.h>
diff --git a/src/OpenGL/compiler/ConstantUnion.h b/src/OpenGL/compiler/ConstantUnion.h
index 6b1050f..89b21e1 100644
--- a/src/OpenGL/compiler/ConstantUnion.h
+++ b/src/OpenGL/compiler/ConstantUnion.h
@@ -15,10 +15,10 @@
 #ifndef _CONSTANT_UNION_INCLUDED_
 #define _CONSTANT_UNION_INCLUDED_
 
-#ifndef __ANDROID__
-#include <assert.h>
-#else
+#if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD)
 #include "../../Common/DebugAndroid.hpp"
+#else
+#include <assert.h>
 #endif
 
 class ConstantUnion {
diff --git a/src/OpenGL/compiler/SymbolTable.h b/src/OpenGL/compiler/SymbolTable.h
index 3466f2f..2fbf3cf 100644
--- a/src/OpenGL/compiler/SymbolTable.h
+++ b/src/OpenGL/compiler/SymbolTable.h
@@ -38,10 +38,10 @@
 //   are tracked in the intermediate representation, not the symbol table.
 //
 
-#ifndef __ANDROID__
-#include <assert.h>
-#else
+#if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD)
 #include "../../Common/DebugAndroid.hpp"
+#else
+#include <assert.h>
 #endif
 
 #include "InfoSink.h"
diff --git a/src/OpenGL/compiler/debug.h b/src/OpenGL/compiler/debug.h
index 2755d23..77798ef 100644
--- a/src/OpenGL/compiler/debug.h
+++ b/src/OpenGL/compiler/debug.h
@@ -17,7 +17,7 @@
 #ifndef COMPILER_DEBUG_H_
 #define COMPILER_DEBUG_H_
 
-#ifdef __ANDROID__
+#if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD)
 #include "../../Common/DebugAndroid.hpp"
 
 #define Trace(...) ((void)0)
diff --git a/src/OpenGL/libEGL/Display.cpp b/src/OpenGL/libEGL/Display.cpp
index 5821ba9..8bbed9e 100644
--- a/src/OpenGL/libEGL/Display.cpp
+++ b/src/OpenGL/libEGL/Display.cpp
@@ -668,12 +668,12 @@
 	#elif defined(__ANDROID__)
 		if(!window)
 		{
-			ALOGE("%s called with window==NULL %s:%d", __FUNCTION__, __FILE__, __LINE__);
+			ERR("%s called with window==NULL %s:%d", __FUNCTION__, __FILE__, __LINE__);
 			return false;
 		}
 		if(static_cast<ANativeWindow*>(window)->common.magic != ANDROID_NATIVE_WINDOW_MAGIC)
 		{
-			ALOGE("%s called with window==%p bad magic %s:%d", __FUNCTION__, window, __FILE__, __LINE__);
+			ERR("%s called with window==%p bad magic %s:%d", __FUNCTION__, window, __FILE__, __LINE__);
 			return false;
 		}
 		return true;
diff --git a/src/OpenGL/libEGL/libEGL.cpp b/src/OpenGL/libEGL/libEGL.cpp
index b2ef237..a4f078d 100644
--- a/src/OpenGL/libEGL/libEGL.cpp
+++ b/src/OpenGL/libEGL/libEGL.cpp
@@ -1172,7 +1172,7 @@
 
 			if(!nativeBuffer || GLPixelFormatFromAndroid(nativeBuffer->format) == GL_NONE)
 			{
-				ALOGW("%s badness unsupported HAL format=%x", __FUNCTION__, nativeBuffer ? nativeBuffer->format : 0);
+				ERR("%s badness unsupported HAL format=%x", __FUNCTION__, nativeBuffer ? nativeBuffer->format : 0);
 				return error(EGL_BAD_ATTRIBUTE, EGL_NO_IMAGE_KHR);
 			}
 
diff --git a/src/OpenGL/libGLESv2/libGLESv2.cpp b/src/OpenGL/libGLESv2/libGLESv2.cpp
index b3f1d20..1577569 100644
--- a/src/OpenGL/libGLESv2/libGLESv2.cpp
+++ b/src/OpenGL/libGLESv2/libGLESv2.cpp
@@ -37,10 +37,6 @@
 #include <algorithm>
 #include <limits>
 
-#ifdef __ANDROID__
-#include <cutils/log.h>
-#endif
-
 namespace es2
 {
 
@@ -2807,11 +2803,7 @@
 	if(!context)
 	{
 		// Not strictly an error, but probably unintended or attempting to rely on non-compliant behavior
-		#ifdef __ANDROID__
-			ALOGI("expected_badness glGetIntegerv() called without current context.");
-		#else
-			ERR("glGetIntegerv() called without current context.");
-		#endif
+		ERR("glGetIntegerv() called without current context.");
 
 		// This is not spec compliant! When there is no current GL context, functions should
 		// have no side effects. Google Maps queries these values before creating a context,