loader: Changes to use new WSI swapchain extensions.

Use device extension list, not global.  When validating the PhysicalDevice
extensions really need to use the device extension list not the loader's global
list.

Fix include to find aligned_alloc
diff --git a/loader/CMakeLists.txt b/loader/CMakeLists.txt
index f9ae4bc..c06d04b 100644
--- a/loader/CMakeLists.txt
+++ b/loader/CMakeLists.txt
@@ -18,8 +18,8 @@
     loader.h
     vk_loader_platform.h
     trampoline.c
-    wsi_lunarg.c
-    wsi_lunarg.h
+    wsi_swapchain.c
+    wsi_swapchain.h
     debug_report.c
     debug_report.h
     table_ops.h
@@ -28,6 +28,7 @@
     cJSON.h
 )
 
+
 if (WIN32)
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DVK_PROTOTYPES -D_CRT_SECURE_NO_WARNINGS")
 
diff --git a/loader/gpa_helper.h b/loader/gpa_helper.h
index 43432d0..5373603 100644
--- a/loader/gpa_helper.h
+++ b/loader/gpa_helper.h
@@ -23,7 +23,7 @@
  */
 
 #include <string.h>
-#include "wsi_lunarg.h"
+#include "wsi_swapchain.h"
 
 static inline void* globalGetProcAddr(const char *name)
 {
@@ -373,8 +373,6 @@
         return (void*) vkGetDeviceQueue;
     if (!strcmp(name, "CreateCommandBuffer"))
         return (void*) vkCreateCommandBuffer;
-    if (!strcmp(name, "CreateSwapChainWSI"))
-        return (void*) wsi_lunarg_CreateSwapChainWSI;
 
     return NULL;
 }
diff --git a/loader/loader.c b/loader/loader.c
index bb1ba99..f89db4a 100644
--- a/loader/loader.c
+++ b/loader/loader.c
@@ -42,7 +42,6 @@
 #endif // WIN32
 #include "vk_loader_platform.h"
 #include "loader.h"
-#include "wsi_lunarg.h"
 #include "gpa_helper.h"
 #include "table_ops.h"
 #include "debug_report.h"
@@ -83,6 +82,9 @@
 // additionally CreateDevice and DestroyDevice needs to be locked
 loader_platform_thread_mutex loader_lock;
 
+// This table contains the loader's instance dispatch table, which contains
+// default functions if no instance layers are activated.  This contains
+// pointers to "terminator functions".
 const VkLayerInstanceDispatchTable instance_disp = {
     .GetInstanceProcAddr = loader_GetInstanceProcAddr,
     .CreateInstance = loader_CreateInstance,
@@ -98,6 +100,7 @@
     .GetPhysicalDeviceExtensionProperties = loader_GetPhysicalDeviceExtensionProperties,
     .GetPhysicalDeviceLayerProperties = loader_GetPhysicalDeviceLayerProperties,
     .GetPhysicalDeviceSparseImageFormatProperties = loader_GetPhysicalDeviceSparseImageFormatProperties,
+    .GetPhysicalDeviceSurfaceSupportWSI = loader_GetPhysicalDeviceSurfaceSupportWSI,
     .DbgCreateMsgCallback = loader_DbgCreateMsgCallback,
     .DbgDestroyMsgCallback = loader_DbgDestroyMsgCallback,
 };
@@ -822,6 +825,7 @@
     };
 
     // Traverse loader's extensions, adding non-duplicate extensions to the list
+    wsi_swapchain_add_instance_extensions(&loader.global_extensions);
     debug_report_add_instance_extensions(&loader.global_extensions);
 }
 
@@ -1083,6 +1087,10 @@
     LOOKUP(DbgDestroyMsgCallback);
 #undef LOOKUP
 
+    icd->GetPhysicalDeviceSurfaceSupportWSI =
+        (PFN_vkGetPhysicalDeviceSurfaceSupportWSI) loader_platform_get_proc_address(scanned_icds->handle,
+                                                                                    "vkGetPhysicalDeviceSurfaceSupportWSI");
+
     return;
 }
 
@@ -2843,8 +2851,7 @@
         return addr;
     }
 
-    /* TODO Remove this once WSI has no loader special code */
-    addr = wsi_lunarg_GetInstanceProcAddr(instance, pName);
+    addr = wsi_swapchain_GetInstanceProcAddr(ptr_instance, pName);
     if (addr) {
         return addr;
     }
@@ -2881,13 +2888,6 @@
         return addr;
     }
 
-    /* return any extension device entrypoints the loader knows about */
-    /* TODO once WSI has no loader special code remove this */
-    addr = wsi_lunarg_GetDeviceProcAddr(device, pName);
-    if (addr) {
-        return addr;
-    }
-
     /* return the dispatch table entrypoint for the fastest case */
     const VkLayerDispatchTable *disp_table = * (VkLayerDispatchTable **) device;
     if (disp_table == NULL)
diff --git a/loader/loader.h b/loader/loader.h
index 1714106..2bd6b3e 100644
--- a/loader/loader.h
+++ b/loader/loader.h
@@ -30,7 +30,7 @@
 
 #include <vulkan.h>
 #include <vk_debug_report_lunarg.h>
-#include <vk_wsi_lunarg.h>
+#include <vk_wsi_swapchain.h>
 #include <vk_layer.h>
 #include <vk_icd.h>
 #include <assert.h>
@@ -154,6 +154,7 @@
     PFN_vkGetPhysicalDeviceExtensionProperties GetPhysicalDeviceExtensionProperties;
     PFN_vkGetPhysicalDeviceLayerProperties GetPhysicalDeviceLayerProperties;
     PFN_vkGetPhysicalDeviceSparseImageFormatProperties GetPhysicalDeviceSparseImageFormatProperties;
+    PFN_vkGetPhysicalDeviceSurfaceSupportWSI GetPhysicalDeviceSurfaceSupportWSI;
     PFN_vkDbgCreateMsgCallback DbgCreateMsgCallback;
     PFN_vkDbgDestroyMsgCallback DbgDestroyMsgCallback;
 
@@ -252,6 +253,8 @@
     VkLayerDbgFunctionNode *DbgFunctionHead;
 
     VkAllocCallbacks alloc_callbacks;
+
+    bool wsi_swapchain_enabled;
 };
 
 struct loader_struct {
diff --git a/loader/table_ops.h b/loader/table_ops.h
index bbb83e0..de75eb2 100644
--- a/loader/table_ops.h
+++ b/loader/table_ops.h
@@ -523,6 +523,8 @@
         return (void *) table->GetPhysicalDeviceExtensionProperties;
     if (!strcmp(name, "GetPhysicalDeviceLayerProperties"))
         return (void *) table->GetPhysicalDeviceLayerProperties;
+    if (!strcmp(name, "GetPhysicalDeviceSurfaceSupportWSI"))
+        return (void *) table->GetPhysicalDeviceSurfaceSupportWSI;
     if (!strcmp(name, "DbgCreateMsgCallback"))
         return (void *) table->DbgCreateMsgCallback;
     if (!strcmp(name, "DbgDestroyMsgCallback"))
diff --git a/loader/trampoline.c b/loader/trampoline.c
index 66978c1..5e4a8bf 100644
--- a/loader/trampoline.c
+++ b/loader/trampoline.c
@@ -28,6 +28,7 @@
 #include "vk_loader_platform.h"
 #include "loader.h"
 #include "debug_report.h"
+#include "wsi_swapchain.h"
 
 #if defined(WIN32)
 // On Windows need to disable global optimization for function entrypoints or
@@ -111,6 +112,7 @@
         return res;
     }
 
+    wsi_swapchain_create_instance(ptr_instance, pCreateInfo);
     debug_report_create_instance(ptr_instance, pCreateInfo);
 
     /* enable any layers on instance chain */
diff --git a/loader/wsi_swapchain.c b/loader/wsi_swapchain.c
new file mode 100644
index 0000000..c41841d
--- /dev/null
+++ b/loader/wsi_swapchain.c
@@ -0,0 +1,127 @@
+/*
+ * Vulkan
+ *
+ * Copyright (C) 2015 LunarG, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Jon Ashburn <jon@lunarg.com>
+ *   Courtney Goeltzenleuchter <courtney@lunarg.com>
+ *   Ian Elliott <ian@lunarg.com>
+ */
+
+#define _ISOC11_SOURCE /* for aligned_alloc() */
+#include <stdlib.h>
+#include <string.h>
+#include "vk_loader_platform.h"
+#include "loader.h"
+#include "wsi_swapchain.h"
+
+static const struct loader_extension_property wsi_swapchain_extension_info = {
+    .info =  {
+        .extName = VK_WSI_SWAPCHAIN_EXTENSION_NAME,
+        .specVersion = VK_WSI_SWAPCHAIN_REVISION,
+        },
+    .origin = VK_EXTENSION_ORIGIN_LOADER,
+};
+
+void wsi_swapchain_add_instance_extensions(
+        struct loader_extension_list *ext_list)
+{
+    loader_add_to_ext_list(ext_list, 1, &wsi_swapchain_extension_info);
+}
+
+void wsi_swapchain_create_instance(
+        struct loader_instance *ptr_instance,
+        const VkInstanceCreateInfo *pCreateInfo)
+{
+    ptr_instance->wsi_swapchain_enabled = false;
+
+    for (uint32_t i = 0; i < pCreateInfo->extensionCount; i++) {
+        if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_WSI_SWAPCHAIN_EXTENSION_NAME) == 0) {
+            ptr_instance->wsi_swapchain_enabled = true;
+            return;
+        }
+    }
+}
+
+/*
+ * This is the trampoline entrypoint
+ * for GetPhysicalDeviceSurfaceSupportWSI
+ */
+VkResult wsi_swapchain_GetPhysicalDeviceSurfaceSupportWSI(
+        VkPhysicalDevice                        physicalDevice,
+        uint32_t                                queueNodeIndex,
+        const VkSurfaceDescriptionWSI*          pSurfaceDescription,
+        VkBool32*                               pSupported)
+{
+    const VkLayerInstanceDispatchTable *disp;
+// TBD/TODO: DO WE NEED TO DO LOCKING FOR THIS FUNCTION?
+    disp = loader_get_instance_dispatch(physicalDevice);
+    loader_platform_thread_lock_mutex(&loader_lock);
+    VkResult res = disp->GetPhysicalDeviceSurfaceSupportWSI(
+                                                      physicalDevice,
+                                                      queueNodeIndex,
+                                                      pSurfaceDescription,
+                                                      pSupported);
+    loader_platform_thread_unlock_mutex(&loader_lock);
+    return res;
+}
+
+/*
+ * This is the instance chain terminator function
+ * for GetPhysicalDeviceSurfaceSupportWSI
+ */
+VkResult loader_GetPhysicalDeviceSurfaceSupportWSI(
+        VkPhysicalDevice                        physicalDevice,
+        uint32_t                                queueNodeIndex,
+        const VkSurfaceDescriptionWSI*          pSurfaceDescription,
+        VkBool32*                               pSupported)
+{
+    uint32_t gpu_index;
+    struct loader_icd *icd = loader_get_icd(physicalDevice, &gpu_index);
+    VkResult res = VK_ERROR_UNAVAILABLE;
+    *pSupported = false;
+
+    if (icd->GetPhysicalDeviceSurfaceSupportWSI) {
+        res = icd->GetPhysicalDeviceSurfaceSupportWSI(physicalDevice,
+                                                      queueNodeIndex,
+                                                      pSurfaceDescription,
+                                                      pSupported);
+    }
+
+    return res;
+}
+
+
+void *wsi_swapchain_GetInstanceProcAddr(
+        struct loader_instance *ptr_instance,
+        const char*                             pName)
+{
+    if (ptr_instance == VK_NULL_HANDLE || !ptr_instance->wsi_swapchain_enabled) {
+        return NULL;
+    }
+
+    if (!strcmp(pName, "vkGetPhysicalDeviceSurfaceSupportWSI")) {
+        return (void*) wsi_swapchain_GetPhysicalDeviceSurfaceSupportWSI;
+    }
+
+    return NULL;
+}
diff --git a/loader/wsi_swapchain.h b/loader/wsi_swapchain.h
new file mode 100644
index 0000000..598ffd4
--- /dev/null
+++ b/loader/wsi_swapchain.h
@@ -0,0 +1,49 @@
+/*
+ * Vulkan
+ *
+ * Copyright (C) 2015 LunarG, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Jon Ashburn <jon@lunarg.com>
+ *   Courtney Goeltzenleuchter <courtney@lunarg.com>
+ *   Ian Elliott <ian@lunarg.com>
+ */
+
+#include "vk_loader_platform.h"
+#include "loader.h"
+#include "vk_wsi_swapchain.h"
+
+void wsi_swapchain_add_instance_extensions(
+        struct loader_extension_list *ext_list);
+
+void wsi_swapchain_create_instance(
+        struct loader_instance *ptr_instance,
+        const VkInstanceCreateInfo *pCreateInfo);
+
+void *wsi_swapchain_GetInstanceProcAddr(
+        struct loader_instance                  *ptr_instance,
+        const char*                             pName);
+
+VkResult loader_GetPhysicalDeviceSurfaceSupportWSI(
+        VkPhysicalDevice                        physicalDevice,
+        uint32_t                                queueNodeIndex,
+        const VkSurfaceDescriptionWSI*          pSurfaceDescription,
+        VkBool32*                               pSupported);