Fixes for JBMR0 compile (API 16)

Change-Id: Ibb2ebd66116f3dfd0008217153006bd6c7a49b9e
Reviewed-on: https://swiftshader-review.googlesource.com/4322
Reviewed-by: Greg Hartman <ghartman@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
Tested-by: Nicolas Capens <capn@google.com>
diff --git a/src/Android.mk b/src/Android.mk
index 58bfe1a..9a74930 100644
--- a/src/Android.mk
+++ b/src/Android.mk
@@ -82,12 +82,28 @@
 	OpenGL/common/Object.cpp \
 	OpenGL/common/MatrixStack.cpp \
 
-COMMON_CFLAGS := -DLOG_TAG=\"swiftshader\" -Wno-unused-parameter -Wno-implicit-exception-spec-mismatch -Wno-overloaded-virtual -fno-operator-names -msse2 -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -std=c++11 -Xclang -fuse-init-array
+COMMON_CFLAGS := \
+	-DLOG_TAG=\"swiftshader\" \
+	-Wno-unused-parameter \
+	-Wno-implicit-exception-spec-mismatch \
+	-Wno-overloaded-virtual \
+	-fno-operator-names \
+	-msse2 \
+	-D__STDC_CONSTANT_MACROS \
+	-D__STDC_LIMIT_MACROS \
+	-DANDROID_PLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION) \
+	-std=c++11
 
 ifneq ($(filter gce_x86 gce calypso, $(TARGET_DEVICE)),)
 COMMON_CFLAGS += -DDISPLAY_LOGO=0
 endif
 
+ifneq (16,${PLATFORM_SDK_VERSION})
+COMMON_CFLAGS += -Xclang -fuse-init-array
+else
+COMMON_CFLAGS += -D__STDC_INT64__
+endif
+
 include $(CLEAR_VARS)
 LOCAL_CLANG := true
 LOCAL_MODULE := swiftshader_top_release
diff --git a/src/LLVM/Android.mk b/src/LLVM/Android.mk
index e573f11..8f2ec86 100644
--- a/src/LLVM/Android.mk
+++ b/src/LLVM/Android.mk
@@ -399,8 +399,13 @@
 LOCAL_CFLAGS += -DLOG_TAG=\"libLLVM_swiftshader\" \
 	-Wno-unused-parameter \
 	-Wno-implicit-exception-spec-mismatch \
-	-Wno-overloaded-virtual \
-	-Xclang -fuse-init-array
+	-Wno-overloaded-virtual
+
+ifneq (16,${PLATFORM_SDK_VERSION})
+LOCAL_CFLAGS += -Xclang -fuse-init-array
+else
+LOCAL_CFLAGS += -D__STDC_INT64__
+endif
 
 LOCAL_CFLAGS += -fomit-frame-pointer -Os -ffunction-sections -fdata-sections
 LOCAL_CFLAGS += -fno-operator-names -msse2 -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS
diff --git a/src/Main/FrameBufferAndroid.cpp b/src/Main/FrameBufferAndroid.cpp
index ff1148b..9d20ac6 100644
--- a/src/Main/FrameBufferAndroid.cpp
+++ b/src/Main/FrameBufferAndroid.cpp
@@ -1,10 +1,36 @@
 #include "FrameBufferAndroid.hpp"
 
 #include <cutils/log.h>
-#include <ui/Fence.h>
 
 namespace sw
 {
+	inline int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer)
+	{
+		#if ANDROID_PLATFORM_SDK_VERSION > 16
+			return native_window_dequeue_buffer_and_wait(window, buffer);
+		#else
+			return window->dequeueBuffer(window, buffer);
+		#endif
+	}
+
+	inline int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
+	{
+		#if ANDROID_PLATFORM_SDK_VERSION > 16
+			return window->queueBuffer(window, buffer, fenceFd);
+		#else
+			return window->queueBuffer(window, buffer);
+		#endif
+	}
+
+	inline int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
+	{
+		#if ANDROID_PLATFORM_SDK_VERSION > 16
+			return window->cancelBuffer(window, buffer, fenceFd);
+		#else
+			return window->cancelBuffer(window, buffer);
+		#endif
+	}
+
     FrameBufferAndroid::FrameBufferAndroid(ANativeWindow* window, int width, int height)
 			: FrameBuffer(width, height, false, false),
 			  nativeWindow(window), buffer(0), gralloc(0)
@@ -22,7 +48,7 @@
         if (buffer)
         {
             // Probably doesn't have to cancel assuming a success queueing earlier
-            nativeWindow->cancelBuffer(nativeWindow, buffer, -1);
+            cancelBuffer(nativeWindow, buffer, -1);
             buffer = 0;
         }
         nativeWindow->common.decRef(&nativeWindow->common);
@@ -33,7 +59,7 @@
         copy(source, sourceFormat, sourceStride);
 		if (buffer)
 		{
-			nativeWindow->queueBuffer(nativeWindow, buffer, -1);
+			queueBuffer(nativeWindow, buffer, -1);
 			if (locked)
 			{
 				locked = 0;
@@ -45,26 +71,17 @@
 
     void* FrameBufferAndroid::lock()
     {
-        int fenceFd = -1;
-        if (nativeWindow->dequeueBuffer(nativeWindow, &buffer, &fenceFd) != android::NO_ERROR)
+        if (dequeueBuffer(nativeWindow, &buffer) != 0)
         {
             return NULL;
         }
 
-        android::sp<android::Fence> fence(new android::Fence(fenceFd));
-        if (fence->wait(android::Fence::TIMEOUT_NEVER) != android::NO_ERROR)
-        {
-            nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
-            return NULL;
-        }
-
         buffer->common.incRef(&buffer->common);
 
         if (gralloc->lock(
 				gralloc, buffer->handle,
 				GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
-				0, 0, buffer->width, buffer->height, &locked)
-			!= android::NO_ERROR)
+				0, 0, buffer->width, buffer->height, &locked) != 0)
         {
             ALOGE("%s failed to lock buffer %p", __FUNCTION__, buffer);
             return NULL;
@@ -99,7 +116,7 @@
 			return;
 		}
 		locked = 0;
-        if (gralloc->unlock(gralloc, buffer->handle) != android::NO_ERROR)
+        if (gralloc->unlock(gralloc, buffer->handle) != 0)
 		{
 			ALOGE("%s: badness unlock failed", __FUNCTION__);
 		}
diff --git a/src/OpenGL/common/AndroidCommon.cpp b/src/OpenGL/common/AndroidCommon.cpp
index 861cf91..0d11ce6 100644
--- a/src/OpenGL/common/AndroidCommon.cpp
+++ b/src/OpenGL/common/AndroidCommon.cpp
@@ -26,8 +26,6 @@
 		return GL_RGB565;
 	case HAL_PIXEL_FORMAT_YV12:
 		return SW_YV12_BT601;
-	case HAL_PIXEL_FORMAT_BLOB:
-	case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
 	default:
 		ALOGE("%s badness unsupported HAL format=%x", __FUNCTION__, halFormat);
 	}
@@ -48,8 +46,6 @@
 		return GL_UNSIGNED_SHORT_5_6_5;
 	case HAL_PIXEL_FORMAT_YV12:
 		return GL_UNSIGNED_BYTE;
-	case HAL_PIXEL_FORMAT_BLOB:
-	case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
 	default:
 		ALOGE("%s badness unsupported HAL format=%x", __FUNCTION__, halFormat);
 	}
diff --git a/src/OpenGL/compiler/Android.mk b/src/OpenGL/compiler/Android.mk
index 65bfb0b..612dcfe 100644
--- a/src/OpenGL/compiler/Android.mk
+++ b/src/OpenGL/compiler/Android.mk
@@ -24,8 +24,13 @@
 	-msse2 \
 	-D__STDC_CONSTANT_MACROS \
 	-D__STDC_LIMIT_MACROS \
-	-std=c++11 \
-	-Xclang -fuse-init-array
+	-std=c++11
+
+ifneq (16,${PLATFORM_SDK_VERSION})
+COMMON_CFLAGS += -Xclang -fuse-init-array
+else
+COMMON_CFLAGS += -D__STDC_INT64__
+endif
 
 COMMON_SRC_FILES := \
 	preprocessor/Diagnostics.cpp \
diff --git a/src/OpenGL/libEGL/Android.mk b/src/OpenGL/libEGL/Android.mk
index 73faaed..301b171 100644
--- a/src/OpenGL/libEGL/Android.mk
+++ b/src/OpenGL/libEGL/Android.mk
@@ -7,8 +7,13 @@
 	-DEGL_EGLEXT_PROTOTYPES \
 	-Wno-unused-parameter \
 	-Wno-implicit-exception-spec-mismatch \
-	-Wno-overloaded-virtual \
-	-Xclang -fuse-init-array
+	-Wno-overloaded-virtual
+
+ifneq (16,${PLATFORM_SDK_VERSION})
+COMMON_CFLAGS += -Xclang -fuse-init-array
+else
+COMMON_CFLAGS += -D__STDC_INT64__
+endif
 
 COMMON_SRC_FILES := \
 	Config.cpp \
diff --git a/src/OpenGL/libEGL/Surface.cpp b/src/OpenGL/libEGL/Surface.cpp
index c4dfeeb..3db1db2 100644
--- a/src/OpenGL/libEGL/Surface.cpp
+++ b/src/OpenGL/libEGL/Surface.cpp
@@ -257,7 +257,7 @@
 	if(backBuffer && frameBuffer)

     {

 		void *source = backBuffer->lockInternal(0, 0, 0, sw::LOCK_READONLY, sw::PUBLIC);

-		frameBuffer->flip(source, backBuffer->Surface::getInternalFormat(), backBuffer->getInternalPitchB());

+		frameBuffer->flip(source, backBuffer->sw::Surface::getInternalFormat(), backBuffer->getInternalPitchB());

 		backBuffer->unlockInternal();

 

         checkForResize();

diff --git a/src/OpenGL/libGLES_CM/Android.mk b/src/OpenGL/libGLES_CM/Android.mk
index f005853..4183d36 100644
--- a/src/OpenGL/libGLES_CM/Android.mk
+++ b/src/OpenGL/libGLES_CM/Android.mk
@@ -13,8 +13,13 @@
 	-DGL_GLEXT_PROTOTYPES \
 	-Wno-unused-parameter \
 	-Wno-implicit-exception-spec-mismatch \
-	-Wno-overloaded-virtual \
-	-Xclang -fuse-init-array
+	-Wno-overloaded-virtual
+
+ifneq (16,${PLATFORM_SDK_VERSION})
+COMMON_CFLAGS += -Xclang -fuse-init-array
+else
+COMMON_CFLAGS += -D__STDC_INT64__
+endif
 
 
 COMMON_SRC_FILES := \
diff --git a/src/OpenGL/libGLESv2/Android.mk b/src/OpenGL/libGLESv2/Android.mk
index 3f2cb1e..b538e74 100644
--- a/src/OpenGL/libGLESv2/Android.mk
+++ b/src/OpenGL/libGLESv2/Android.mk
@@ -12,8 +12,13 @@
 	-DGL_GLEXT_PROTOTYPES \
 	-Wno-unused-parameter \
 	-Wno-implicit-exception-spec-mismatch \
-	-Wno-overloaded-virtual \
-	-Xclang -fuse-init-array
+	-Wno-overloaded-virtual
+
+ifneq (16,${PLATFORM_SDK_VERSION})
+COMMON_CFLAGS += -Xclang -fuse-init-array
+else
+COMMON_CFLAGS += -D__STDC_INT64__
+endif
 
 COMMON_SRC_FILES := \
 	Buffer.cpp \