Enable ASTC on supported builds only

To temporarily work around build issues related to Google3 integration,
make ASTC support optional and only enable it for CMake, Chromium, and
Android builds.

Bug: b/150600814
Change-Id: Iac6a404322c65a6f2727807ea8af2edfb219a5a2
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/41828
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Sean Risser <srisser@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/Android.bp b/Android.bp
index 9ac0441..248a1c6 100644
--- a/Android.bp
+++ b/Android.bp
@@ -30,7 +30,7 @@
     cppflags: [
         "-Woverloaded-virtual",
         "-DVK_USE_PLATFORM_ANDROID_KHR",
-	"-DVK_EXPORT= ",
+        "-DVK_EXPORT= ",
     ],
     cpp_std: "c++14",
 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5d80303..91b4615 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -123,7 +123,8 @@
 option_if_not_defined(REACTOR_ENABLE_PRINT "Enable RR_PRINT macros" 0)
 option_if_not_defined(REACTOR_VERIFY_LLVM_IR "Check reactor-generated LLVM IR is valid even in release builds" 0)
 option_if_not_defined(SWIFTSHADER_LESS_DEBUG_INFO "Generate less debug info to reduce file size" 0)
-option_if_not_defined(SWIFTSHADER_ENABLE_VULKAN_DEBUGGER "Enable vulkan debugger support" 0)
+option_if_not_defined(SWIFTSHADER_ENABLE_VULKAN_DEBUGGER "Enable Vulkan debugger support" 0)
+option_if_not_defined(SWIFTSHADER_ENABLE_ASTC "Enable ASTC compressed textures support" 1)  # TODO(b/150130101)
 
 set(BUILD_MARL ${SWIFTSHADER_BUILD_VULKAN})
 
@@ -1866,8 +1867,6 @@
     ${SOURCE_DIR}/WSI/VkSurfaceKHR.hpp
     ${SOURCE_DIR}/WSI/VkSwapchainKHR.cpp
     ${SOURCE_DIR}/WSI/VkSwapchainKHR.hpp
-    ${ASTC_DIR}/Source/*.cpp
-    ${ASTC_DIR}/Source/*.h
     ${CMAKE_CURRENT_SOURCE_DIR}/include/vulkan/*.h}
 )
 
@@ -1881,6 +1880,15 @@
     list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-DENABLE_VK_DEBUGGER")
 endif()
 
+if(SWIFTSHADER_ENABLE_ASTC)
+    file(GLOB_RECURSE VULKAN_ASTC_LIST
+        ${ASTC_DIR}/Source/*.cpp
+        ${ASTC_DIR}/Source/*.h
+    )
+    list(APPEND VULKAN_LIST ${VULKAN_ASTC_LIST})
+    list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-DSWIFTSHADER_ENABLE_ASTC")
+endif()
+
 if(LINUX OR ANDROID)
     list(APPEND VULKAN_LIST
         ${SOURCE_DIR}/System/Linux/MemFd.cpp
diff --git a/src/Android.bp b/src/Android.bp
index a400a4c..2f8683a 100644
--- a/src/Android.bp
+++ b/src/Android.bp
@@ -617,6 +617,7 @@
     cflags: [
         "-DLOG_TAG=\"swiftshader\"",
         "-DSWIFTSHADER_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER",
+        "-DSWIFTSHADER_ENABLE_ASTC",  // TODO(b/150130101)
     ],
 
     srcs: [
diff --git a/src/Device/ASTC_Decoder.cpp b/src/Device/ASTC_Decoder.cpp
index 344988d..aef8795 100644
--- a/src/Device/ASTC_Decoder.cpp
+++ b/src/Device/ASTC_Decoder.cpp
@@ -13,14 +13,19 @@
 // limitations under the License.
 
 #include "ASTC_Decoder.hpp"
-#include "../third_party/astc-encoder/Source/astc_codec_internals.h"
+
 #include "System/Math.hpp"
 
+#ifdef SWIFTSHADER_ENABLE_ASTC
+#	include "../third_party/astc-encoder/Source/astc_codec_internals.h"
+#endif
+
 #include <memory>
 #include <unordered_map>
 
 namespace {
 
+#ifdef SWIFTSHADER_ENABLE_ASTC
 void write_imageblock(unsigned char *img,
                       // picture-block to initialize with image data. We assume that orig_data is valid
                       const imageblock *pb,
@@ -91,6 +96,7 @@
 		}
 	}
 }
+#endif
 
 }  // namespace
 
@@ -100,6 +106,7 @@
                           int xBlockSize, int yBlockSize, int zBlockSize,
                           int xblocks, int yblocks, int zblocks, bool isUnsignedByte)
 {
+#ifdef SWIFTSHADER_ENABLE_ASTC
 	build_quantization_mode_table();
 
 	astc_decode_mode decode_mode = isUnsignedByte ? DECODE_LDR : DECODE_HDR;
@@ -124,4 +131,5 @@
 	}
 
 	term_block_size_descriptor(bsd.get());
+#endif
 }
diff --git a/src/Vulkan/BUILD.gn b/src/Vulkan/BUILD.gn
index 063704e..b167c68 100644
--- a/src/Vulkan/BUILD.gn
+++ b/src/Vulkan/BUILD.gn
@@ -50,6 +50,10 @@
       "-Wno-switch",
     ]
   }
+
+  defines += [
+     "SWIFTSHADER_ENABLE_ASTC",  # TODO(b/150130101)
+  ]
 }
 
 swiftshader_source_set("swiftshader_libvulkan_headers") {
diff --git a/src/Vulkan/VkImage.cpp b/src/Vulkan/VkImage.cpp
index a85b301..7c2872a 100644
--- a/src/Vulkan/VkImage.cpp
+++ b/src/Vulkan/VkImage.cpp
@@ -13,19 +13,21 @@
 // limitations under the License.
 
 #include "VkImage.hpp"
+
 #include "VkBuffer.hpp"
 #include "VkDevice.hpp"
 #include "VkDeviceMemory.hpp"
-#include "Device/ASTC_Decoder.hpp"
 #include "Device/BC_Decoder.hpp"
 #include "Device/Blitter.hpp"
 #include "Device/ETC_Decoder.hpp"
-#include <cstring>
+#include "Device/ASTC_Decoder.hpp"
 
 #ifdef __ANDROID__
 #	include "System/GrallocAndroid.hpp"
 #endif
 
+#include <cstring>
+
 namespace {
 
 ETC_Decoder::InputType GetInputType(const vk::Format &format)
diff --git a/src/Vulkan/VkPhysicalDevice.cpp b/src/Vulkan/VkPhysicalDevice.cpp
index 03df1e5..ba22551 100644
--- a/src/Vulkan/VkPhysicalDevice.cpp
+++ b/src/Vulkan/VkPhysicalDevice.cpp
@@ -76,7 +76,11 @@
 		VK_FALSE,  // multiViewport
 		VK_TRUE,   // samplerAnisotropy
 		VK_TRUE,   // textureCompressionETC2
-		VK_TRUE,   // textureCompressionASTC_LDR
+#ifdef SWIFTSHADER_ENABLE_ASTC
+		VK_TRUE,  // textureCompressionASTC_LDR
+#else
+		VK_FALSE,  // textureCompressionASTC_LDR
+#endif
 		VK_FALSE,  // textureCompressionBC
 		VK_FALSE,  // occlusionQueryPrecise
 		VK_FALSE,  // pipelineStatisticsQuery
@@ -527,6 +531,7 @@
 		case VK_FORMAT_EAC_R11_SNORM_BLOCK:
 		case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
 		case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
+#ifdef SWIFTSHADER_ENABLE_ASTC
 		case VK_FORMAT_ASTC_4x4_UNORM_BLOCK:
 		case VK_FORMAT_ASTC_5x4_UNORM_BLOCK:
 		case VK_FORMAT_ASTC_5x5_UNORM_BLOCK:
@@ -555,6 +560,7 @@
 		case VK_FORMAT_ASTC_10x10_SRGB_BLOCK:
 		case VK_FORMAT_ASTC_12x10_SRGB_BLOCK:
 		case VK_FORMAT_ASTC_12x12_SRGB_BLOCK:
+#endif
 		case VK_FORMAT_D16_UNORM:
 		case VK_FORMAT_D32_SFLOAT:
 		case VK_FORMAT_D32_SFLOAT_S8_UINT: