Пример #1
0
    def __init__(self,
                 errFile=sys.stderr,
                 warnFile=sys.stderr,
                 diagFile=sys.stdout):
        OutputGenerator.__init__(self, errFile, warnFile, diagFile)

        self.typeInfo = cereal.VulkanTypeInfo()

        self.modules = {}
        self.protos = {}
        self.moduleList = []
        self.protoList = []

        self.wrappers = []

        self.codegen = cereal.CodeGen()

        self.host_cmake_generator = lambda cppFiles: """%s
set(OpenglRender_vulkan_cereal_src %s)
android_add_library(OpenglRender_vulkan_cereal)
target_compile_definitions(OpenglRender_vulkan_cereal PRIVATE -DVK_ANDROID_native_buffer -DVK_GOOGLE_address_space)
android_target_compile_definitions(OpenglRender_vulkan_cereal windows PRIVATE -DVK_USE_PLATFORM_WIN32_KHR)
target_link_libraries(OpenglRender_vulkan_cereal PRIVATE emugl_base OpenglRender_vulkan_cereal_proto)
target_link_libraries(OpenglRender_vulkan_cereal PUBLIC android-emu-base)

target_include_directories(OpenglRender_vulkan_cereal
                           PUBLIC
                           ${ANDROID_EMUGL_DIR}/host/libs/libOpenglRender/vulkan/cereal
                           PRIVATE
                           ${ANDROID_EMUGL_DIR}/host/include
                           ${ANDROID_EMUGL_DIR}/host/libs/libOpenglRender/
                           ${ANDROID_EMUGL_DIR}/host/libs/libOpenglRender/vulkan
                           ${ANDROID_EMUGL_DIR}/host/include/vulkan)
""" % (autogeneratedMkTemplate % banner_command(sys.argv), cppFiles)

        self.host_cmake_proto_generator = lambda protoFiles: """%s
android_add_big_protobuf(OpenglRender_vulkan_cereal_proto %s)
""" % (autogeneratedMkTemplate % banner_command(sys.argv), protoFiles)

        self.guest_android_mk_generator = lambda cppFiles: """%s
LOCAL_PATH := $(call my-dir)

$(call emugl-begin-shared-library,libvulkan_enc)
$(call emugl-export,C_INCLUDES,$(LOCAL_PATH))
$(call emugl-import,libOpenglCodecCommon$(GOLDFISH_OPENGL_LIB_SUFFIX) libandroidemu)

# Vulkan include dir
ifeq (true,$(GOLDFISH_OPENGL_BUILD_FOR_HOST))
LOCAL_C_INCLUDES += \\
    $(LOCAL_PATH) \\
    $(HOST_EMUGL_PATH)/host/include \\
    $(HOST_EMUGL_PATH)/host/include/vulkan
endif

ifneq (true,$(GOLDFISH_OPENGL_BUILD_FOR_HOST))
LOCAL_C_INCLUDES += \\
    $(LOCAL_PATH) \\
    $(LOCAL_PATH)/../vulkan_enc \\

LOCAL_HEADER_LIBRARIES += \\
    hwvulkan_headers \\
    vulkan_headers \\

endif

LOCAL_CFLAGS += \\
    -DLOG_TAG=\\"goldfish_vulkan\\" \\
    -DVK_ANDROID_native_buffer \\
    -DVK_GOOGLE_address_space \\
    -Wno-missing-field-initializers \\
    -Werror \\
    -fstrict-aliasing \\
    -DVK_USE_PLATFORM_ANDROID_KHR \\
    -DVK_NO_PROTOTYPES \\

LOCAL_SRC_FILES := AndroidHardwareBuffer.cpp \\
    HostVisibleMemoryVirtualization.cpp \\
    Resources.cpp \\
    Validation.cpp \\
    %s.cpp \\
    VulkanHandleMapping.cpp \\
    ResourceTracker.cpp \\
    %s

ifeq (true,$(GOLDFISH_OPENGL_BUILD_FOR_HOST))
LOCAL_CFLAGS += -D__ANDROID_API__=28
$(call emugl-export,SHARED_LIBRARIES,libgui)
else
$(call emugl-export,SHARED_LIBRARIES,libsync libnativewindow)
LOCAL_STATIC_LIBRARIES += libarect
endif

$(call emugl-end-module)
""" % (autogeneratedMkTemplate % banner_command(sys.argv),
        VULKAN_STREAM_TYPE_GUEST, cppFiles)

        encoderInclude = """
#include "goldfish_vk_private_defs.h"
#include <memory>
class IOStream;
"""
        encoderImplInclude = """
#include "IOStream.h"
#include "Resources.h"
#include "ResourceTracker.h"
#include "Validation.h"
#include "%s.h"

#include "android/base/AlignedBuf.h"
#include "android/base/Pool.h"

#include <cutils/properties.h>

#include "goldfish_vk_marshaling_guest.h"
#include "goldfish_vk_deepcopy_guest.h"
#include "goldfish_vk_handlemap_guest.h"
#include "goldfish_vk_private_defs.h"
#include "goldfish_vk_transform_guest.h"
""" % VULKAN_STREAM_TYPE_GUEST

        functableImplInclude = """
#include "VkEncoder.h"
#include "HostConnection.h"
#include "ResourceTracker.h"

#include "goldfish_vk_private_defs.h"

// Stuff we are not going to use but if included,
// will cause compile errors. These are Android Vulkan
// required extensions, but the approach will be to
// implement them completely on the guest side.
#undef VK_KHR_android_surface
"""
        marshalIncludeGuest = """
#include "goldfish_vk_marshaling_guest.h"
#include "goldfish_vk_private_defs.h"
#include "%s.h"

// Stuff we are not going to use but if included,
// will cause compile errors. These are Android Vulkan
// required extensions, but the approach will be to
// implement them completely on the guest side.
#undef VK_KHR_android_surface
#undef VK_ANDROID_external_memory_android_hardware_buffer
""" % VULKAN_STREAM_TYPE_GUEST

        vulkanStreamIncludeHost = """
#include "goldfish_vk_private_defs.h"

#include "%s.h"
#include "android/base/files/StreamSerializing.h"
""" % VULKAN_STREAM_TYPE

        testingInclude = """
#include "goldfish_vk_private_defs.h"
#include <string.h>
#include <functional>
using OnFailCompareFunc = std::function<void(const char*)>;
"""
        poolInclude = """
#include "goldfish_vk_private_defs.h"
#include "android/base/Pool.h"
using android::base::Pool;
"""
        handleMapInclude = """
#include "goldfish_vk_private_defs.h"
#include "VulkanHandleMapping.h"
"""
        transformIncludeGuest = """
#include "goldfish_vk_private_defs.h"
"""
        transformInclude = """
#include "goldfish_vk_private_defs.h"
#include "goldfish_vk_extension_structs.h"
"""
        transformImplIncludeGuest = """
#include "ResourceTracker.h"
"""
        transformImplInclude = """
#include "VkDecoderGlobalState.h"
"""
        unboxInclude = """
#include "goldfish_vk_private_defs.h"
#include "goldfish_vk_extension_structs.h"

namespace android {
namespace base {
class Pool;
} // namespace base
} // namespace android

using android::base::Pool;

"""
        unboxImplInclude = """
#include "android/base/Pool.h"
#include "VkDecoderGlobalState.h"
#include "goldfish_vk_deepcopy.h"
#include "goldfish_vk_handlemap.h"
"""
        baseProtoConversionInclude = """
#include "VulkanHandleMapping.h"

#include "android/base/Pool.h"
#include "goldfish_vk_private_defs.h"
#include "goldfish_vk_extension_structs.h"
#include "goldfish_vk_baseprotodefs.pb.h"

using android::base::Pool;
"""
        baseProtoConversionImplInclude = """
#include <vector>
#include <string.h>
"""
        poolIncludeGuest = """
#include "goldfish_vk_private_defs.h"
#include "android/base/Pool.h"
using android::base::Pool;
// Stuff we are not going to use but if included,
// will cause compile errors. These are Android Vulkan
// required extensions, but the approach will be to
// implement them completely on the guest side.
#undef VK_KHR_android_surface
#undef VK_ANDROID_external_memory_android_hardware_buffer
"""
        handleMapIncludeGuest = """
#include "goldfish_vk_private_defs.h"
#include "VulkanHandleMapping.h"
// Stuff we are not going to use but if included,
// will cause compile errors. These are Android Vulkan
// required extensions, but the approach will be to
// implement them completely on the guest side.
#undef VK_KHR_android_surface
#undef VK_ANDROID_external_memory_android_hardware_buffer
"""
        dispatchHeaderDefs = """
#include "goldfish_vk_private_defs.h"
namespace goldfish_vk {

struct VulkanDispatch;

} // namespace goldfish_vk
using DlOpenFunc = void* (void);
using DlSymFunc = void* (void*, const char*);
"""

        extensionStructsInclude = """
#include "goldfish_vk_private_defs.h"
"""

        extensionStructsIncludeGuest = """
#include "vk_platform_compat.h"
#include "goldfish_vk_private_defs.h"
// Stuff we are not going to use but if included,
// will cause compile errors. These are Android Vulkan
// required extensions, but the approach will be to
// implement them completely on the guest side.
#undef VK_KHR_android_surface
#undef VK_ANDROID_external_memory_android_hardware_buffer
"""
        commonCerealImplIncludes = """
#include "goldfish_vk_extension_structs.h"
#include "goldfish_vk_private_defs.h"
"""
        commonCerealIncludesGuest = """
#include "vk_platform_compat.h"
"""
        commonCerealImplIncludesGuest = """
#include "goldfish_vk_extension_structs_guest.h"
#include "goldfish_vk_private_defs.h"
"""

        dispatchImplIncludes = """
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
"""

        decoderSnapshotHeaderIncludes = """
#include <memory>
#include "common/goldfish_vk_private_defs.h"
"""
        decoderSnapshotImplIncludes = """
#include "VulkanHandleMapping.h"

#include "goldfish_vk_baseprotodefs.pb.h"
#include "common/goldfish_vk_baseprotoconversion.h"
"""

        decoderHeaderIncludes = """
#include <memory>

namespace android {
namespace base {
class Pool;
} // namespace android
} // namespace base

"""

        decoderImplIncludes = """
#include "common/goldfish_vk_marshaling.h"
#include "common/goldfish_vk_private_defs.h"
#include "common/goldfish_vk_transform.h"

#include "android/base/Pool.h"
#include "android/base/system/System.h"

#include "IOStream.h"
#include "emugl/common/logging.h"

#include "VkDecoderGlobalState.h"
#include "VkDecoderSnapshot.h"

#include "VulkanDispatch.h"
#include "%s.h"

#include <unordered_map>
""" % VULKAN_STREAM_TYPE

        self.guest_encoder_tag = "guest_encoder"
        self.guest_hal_tag = "guest_hal"
        self.host_tag = "host"

        self.guest_abs_encoder_destination = \
            os.path.join(
                os.getcwd(),
                "..", "..",
                "device", "generic", "goldfish-opengl",
                "system", "vulkan_enc")
        self.guest_abs_hal_destination = \
            os.path.join(
                os.getcwd(),
                "..", "..",
                "device", "generic", "goldfish-opengl",
                "system", "vulkan")
        self.host_abs_decoder_destination = \
            os.path.join(
                os.getcwd(),
                "android", "android-emugl", "host",
                "libs", "libOpenglRender", "vulkan")
        self.host_abs_proto_destination = \
            os.path.join(
                os.getcwd(),
                "android", "android-emugl", "host",
                "libs", "libOpenglRender", "vulkan", "cereal", "proto")

        self.addGuestEncoderModule("VkEncoder",
                                   extraHeader=encoderInclude,
                                   extraImpl=encoderImplInclude)

        self.addGuestEncoderModule("goldfish_vk_extension_structs_guest",
                                   extraHeader=extensionStructsIncludeGuest)
        self.addGuestEncoderModule("goldfish_vk_marshaling_guest",
                                   extraHeader=commonCerealIncludesGuest +
                                   marshalIncludeGuest,
                                   extraImpl=commonCerealImplIncludesGuest)
        self.addGuestEncoderModule("goldfish_vk_deepcopy_guest",
                                   extraHeader=commonCerealIncludesGuest +
                                   poolIncludeGuest,
                                   extraImpl=commonCerealImplIncludesGuest)
        self.addGuestEncoderModule("goldfish_vk_handlemap_guest",
                                   extraHeader=commonCerealIncludesGuest +
                                   handleMapIncludeGuest,
                                   extraImpl=commonCerealImplIncludesGuest)
        self.addGuestEncoderModule("goldfish_vk_transform_guest",
                                   extraHeader=commonCerealIncludesGuest +
                                   transformIncludeGuest,
                                   extraImpl=commonCerealImplIncludesGuest +
                                   transformImplIncludeGuest)

        self.addGuestHalModule("func_table", extraImpl=functableImplInclude)

        self.addModule("common",
                       "goldfish_vk_extension_structs",
                       extraHeader=extensionStructsInclude)
        self.addModule("common",
                       "goldfish_vk_marshaling",
                       extraHeader=vulkanStreamIncludeHost,
                       extraImpl=commonCerealImplIncludes)
        self.addModule("common",
                       "goldfish_vk_testing",
                       extraHeader=testingInclude,
                       extraImpl=commonCerealImplIncludes)
        self.addModule("common",
                       "goldfish_vk_deepcopy",
                       extraHeader=poolInclude,
                       extraImpl=commonCerealImplIncludes)
        self.addModule("common",
                       "goldfish_vk_handlemap",
                       extraHeader=handleMapInclude,
                       extraImpl=commonCerealImplIncludes)
        self.addModule("common",
                       "goldfish_vk_dispatch",
                       extraHeader=dispatchHeaderDefs,
                       extraImpl=dispatchImplIncludes)
        self.addModule("common",
                       "goldfish_vk_transform",
                       extraHeader=transformInclude,
                       extraImpl=transformImplInclude)
        self.addModule("common",
                       "goldfish_vk_unbox",
                       extraHeader=unboxInclude,
                       extraImpl=unboxImplInclude)
        self.addHostModule("VkDecoder",
                           extraHeader=decoderHeaderIncludes,
                           extraImpl=decoderImplIncludes,
                           useNamespace=False)
        self.addHostModule("VkDecoderSnapshot",
                           extraHeader=decoderSnapshotHeaderIncludes,
                           extraImpl=decoderSnapshotImplIncludes,
                           useNamespace=False)

        self.addProto("proto",
                      "goldfish_vk_baseprotodefs",
                      customAbsDir=self.host_abs_proto_destination)
        self.addModule("common",
                       "goldfish_vk_baseprotoconversion",
                       extraHeader=baseProtoConversionInclude,
                       extraImpl=baseProtoConversionImplInclude)

        self.addWrapper(cereal.VulkanEncoder, "VkEncoder")
        self.addWrapper(cereal.VulkanExtensionStructs,
                        "goldfish_vk_extension_structs_guest")
        self.addWrapper(cereal.VulkanMarshaling,
                        "goldfish_vk_marshaling_guest",
                        variant="guest")
        self.addWrapper(cereal.VulkanDeepcopy, "goldfish_vk_deepcopy_guest")
        self.addWrapper(cereal.VulkanHandleMap, "goldfish_vk_handlemap_guest")
        self.addWrapper(cereal.VulkanTransform, "goldfish_vk_transform_guest")
        self.addWrapper(cereal.VulkanFuncTable, "func_table")
        self.addWrapper(cereal.VulkanExtensionStructs,
                        "goldfish_vk_extension_structs")
        self.addWrapper(cereal.VulkanMarshaling, "goldfish_vk_marshaling")
        self.addWrapper(cereal.VulkanTesting, "goldfish_vk_testing")
        self.addWrapper(cereal.VulkanDeepcopy, "goldfish_vk_deepcopy")
        self.addWrapper(cereal.VulkanHandleMap, "goldfish_vk_handlemap")
        self.addWrapper(cereal.VulkanDispatch, "goldfish_vk_dispatch")
        self.addWrapper(cereal.VulkanTransform,
                        "goldfish_vk_transform",
                        resourceTrackerTypeName="VkDecoderGlobalState")
        self.addWrapper(cereal.VulkanUnbox, "goldfish_vk_unbox")
        self.addWrapper(cereal.VulkanDecoder, "VkDecoder")
        self.addWrapper(cereal.VulkanDecoderSnapshot, "VkDecoderSnapshot")

        self.addProtoWrapper(cereal.VulkanBaseProtoDefs,
                             "goldfish_vk_baseprotodefs")
        self.addWrapper(cereal.VulkanBaseProtoConversion,
                        "goldfish_vk_baseprotoconversion")

        self.guestAndroidMkCppFiles = ""
        self.hostCMakeCppFiles = ""
        self.hostCMakeProtoFiles = ""
        self.hostDecoderCMakeCppFiles = ""

        def addSrcEntry(m):
            mkSrcEntry = m.getMakefileSrcEntry()
            cmakeSrcEntry = m.getCMakeSrcEntry()
            if m.directory == self.guest_encoder_tag:
                self.guestAndroidMkCppFiles += mkSrcEntry
            elif m.directory == self.host_tag:
                self.hostDecoderCMakeCppFiles += cmakeSrcEntry
            elif m.directory != self.guest_hal_tag:
                self.hostCMakeCppFiles += cmakeSrcEntry

        def addProtoEntry(m):
            cmakeSrcEntry = m.getCMakeSrcEntry()
            self.hostCMakeProtoFiles += cmakeSrcEntry

        self.forEachModule(addSrcEntry)
        self.forEachProto(addProtoEntry)
Пример #2
0
    def __init__(self, errFile = sys.stderr,
                       warnFile = sys.stderr,
                       diagFile = sys.stdout):
        OutputGenerator.__init__(self, errFile, warnFile, diagFile)

        init_suppress_option()

        self.typeInfo = cereal.VulkanTypeInfo()

        self.modules = {}
        self.protos = {}
        self.moduleList = []
        self.protoList = []

        self.wrappers = []

        self.codegen = cereal.CodeGen()

        self.host_cmake_generator = lambda cppFiles: """%s
android_add_library(TARGET OpenglRender_vulkan_cereal LICENSE Apache-2.0 SRC %s)
target_compile_definitions(OpenglRender_vulkan_cereal PRIVATE -DVK_ANDROID_native_buffer -DVK_GOOGLE_address_space)
android_target_compile_definitions(OpenglRender_vulkan_cereal windows PRIVATE -DVK_USE_PLATFORM_WIN32_KHR)
target_link_libraries(OpenglRender_vulkan_cereal PRIVATE emugl_base)
target_link_libraries(OpenglRender_vulkan_cereal PUBLIC android-emu-base)

target_include_directories(OpenglRender_vulkan_cereal
                           PUBLIC
                           ${ANDROID_EMUGL_DIR}/host/libs/libOpenglRender/vulkan/cereal
                           PRIVATE
                           ${ANDROID_EMUGL_DIR}/host/include
                           ${ANDROID_EMUGL_DIR}/host/libs/libOpenglRender/
                           ${ANDROID_EMUGL_DIR}/host/libs/libOpenglRender/vulkan
                           ${ANDROID_EMUGL_DIR}/host/include/vulkan)
""" % (autogeneratedMkTemplate % banner_command(sys.argv), cppFiles)

        encoderInclude = """
#include "goldfish_vk_private_defs.h"
#include <memory>
class IOStream;
"""
        encoderImplInclude = """
#include "IOStream.h"
#include "Resources.h"
#include "ResourceTracker.h"
#include "Validation.h"
#include "%s.h"

#include "android/base/AlignedBuf.h"
#include "android/base/Pool.h"
#include "android/base/synchronization/AndroidLock.h"

#include <cutils/properties.h>

#include "goldfish_vk_marshaling_guest.h"
#include "goldfish_vk_deepcopy_guest.h"
#include "goldfish_vk_handlemap_guest.h"
#include "goldfish_vk_private_defs.h"
#include "goldfish_vk_transform_guest.h"

#include <unordered_map>

""" % VULKAN_STREAM_TYPE_GUEST

        functableImplInclude = """
#include "VkEncoder.h"
#include "HostConnection.h"
#include "ResourceTracker.h"

#include "goldfish_vk_private_defs.h"

#include <log/log.h>

// Stuff we are not going to use but if included,
// will cause compile errors. These are Android Vulkan
// required extensions, but the approach will be to
// implement them completely on the guest side.
#undef VK_KHR_android_surface
"""
        marshalIncludeGuest = """
#include "goldfish_vk_marshaling_guest.h"
#include "goldfish_vk_private_defs.h"
#include "%s.h"

// Stuff we are not going to use but if included,
// will cause compile errors. These are Android Vulkan
// required extensions, but the approach will be to
// implement them completely on the guest side.
#undef VK_KHR_android_surface
#undef VK_ANDROID_external_memory_android_hardware_buffer
""" % VULKAN_STREAM_TYPE_GUEST

        vulkanStreamIncludeHost = """
#include "goldfish_vk_private_defs.h"

#include "%s.h"
#include "android/base/files/StreamSerializing.h"
""" % VULKAN_STREAM_TYPE

        testingInclude = """
#include "goldfish_vk_private_defs.h"
#include <string.h>
#include <functional>
using OnFailCompareFunc = std::function<void(const char*)>;
"""
        poolInclude = """
#include "goldfish_vk_private_defs.h"
#include "android/base/Pool.h"
using android::base::Pool;
"""
        handleMapInclude = """
#include "goldfish_vk_private_defs.h"
#include "VulkanHandleMapping.h"
"""
        transformIncludeGuest = """
#include "goldfish_vk_private_defs.h"
"""
        transformInclude = """
#include "goldfish_vk_private_defs.h"
#include "goldfish_vk_extension_structs.h"
"""
        transformImplIncludeGuest = """
#include "ResourceTracker.h"
"""
        transformImplInclude = """
#include "VkDecoderGlobalState.h"
"""
        unboxInclude = """
#include "goldfish_vk_private_defs.h"
#include "goldfish_vk_extension_structs.h"

namespace android {
namespace base {
class Pool;
} // namespace base
} // namespace android

using android::base::Pool;

"""
        unboxImplInclude = """
#include "android/base/Pool.h"
#include "VkDecoderGlobalState.h"
#include "goldfish_vk_deepcopy.h"
#include "goldfish_vk_handlemap.h"
"""
        poolIncludeGuest = """
#include "goldfish_vk_private_defs.h"
#include "android/base/Pool.h"
using android::base::Pool;
// Stuff we are not going to use but if included,
// will cause compile errors. These are Android Vulkan
// required extensions, but the approach will be to
// implement them completely on the guest side.
#undef VK_KHR_android_surface
#undef VK_ANDROID_external_memory_android_hardware_buffer
"""
        handleMapIncludeGuest = """
#include "goldfish_vk_private_defs.h"
#include "VulkanHandleMapping.h"
// Stuff we are not going to use but if included,
// will cause compile errors. These are Android Vulkan
// required extensions, but the approach will be to
// implement them completely on the guest side.
#undef VK_KHR_android_surface
#undef VK_ANDROID_external_memory_android_hardware_buffer
"""
        dispatchHeaderDefs = """
#include "goldfish_vk_private_defs.h"
namespace goldfish_vk {

struct VulkanDispatch;

} // namespace goldfish_vk
using DlOpenFunc = void* (void);
using DlSymFunc = void* (void*, const char*);
"""

        extensionStructsInclude = """
#include "goldfish_vk_private_defs.h"
"""

        extensionStructsIncludeGuest = """
#include "vk_platform_compat.h"
#include "goldfish_vk_private_defs.h"
// Stuff we are not going to use but if included,
// will cause compile errors. These are Android Vulkan
// required extensions, but the approach will be to
// implement them completely on the guest side.
#undef VK_KHR_android_surface
#undef VK_ANDROID_external_memory_android_hardware_buffer
"""
        commonCerealImplIncludes = """
#include "goldfish_vk_extension_structs.h"
#include "goldfish_vk_private_defs.h"
"""
        commonCerealIncludesGuest = """
#include "vk_platform_compat.h"
"""
        commonCerealImplIncludesGuest = """
#include "goldfish_vk_extension_structs_guest.h"
#include "goldfish_vk_private_defs.h"
"""

        dispatchImplIncludes = """
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
"""

        decoderSnapshotHeaderIncludes = """
#include <memory>
#include "common/goldfish_vk_private_defs.h"
"""
        decoderSnapshotImplIncludes = """
#include "VulkanHandleMapping.h"
#include "VkDecoderGlobalState.h"
#include "VkReconstruction.h"

#include "android/base/synchronization/Lock.h"
"""

        decoderHeaderIncludes = """
#include <memory>

namespace android {
namespace base {
class Pool;
} // namespace android
} // namespace base

"""

        decoderImplIncludes = """
#include "common/goldfish_vk_marshaling.h"
#include "common/goldfish_vk_private_defs.h"
#include "common/goldfish_vk_transform.h"

#include "android/base/Pool.h"
#include "android/base/system/System.h"

#include "IOStream.h"
#include "emugl/common/logging.h"

#include "VkDecoderGlobalState.h"
#include "VkDecoderSnapshot.h"

#include "VulkanDispatch.h"
#include "%s.h"

#include <unordered_map>
""" % VULKAN_STREAM_TYPE

        self.guest_encoder_tag = "guest_encoder"
        self.guest_hal_tag = "guest_hal"
        self.host_tag = "host"

        self.guest_abs_encoder_destination = \
            os.path.join(
                os.getcwd(),
                "..", "..",
                "device", "generic", "goldfish-opengl",
                "system", "vulkan_enc")
        self.guest_abs_hal_destination = \
            os.path.join(
                os.getcwd(),
                "..", "..",
                "device", "generic", "goldfish-opengl",
                "system", "vulkan")
        self.host_abs_decoder_destination = \
            os.path.join(
                os.getcwd(),
                "android", "android-emugl", "host",
                "libs", "libOpenglRender", "vulkan")
        self.host_abs_proto_destination = \
            os.path.join(
                os.getcwd(),
                "android", "android-emugl", "host",
                "libs", "libOpenglRender", "vulkan", "cereal", "proto")

        self.addGuestEncoderModule(
            "VkEncoder",
            extraHeader = encoderInclude,
            extraImpl = encoderImplInclude)

        self.addGuestEncoderModule("goldfish_vk_extension_structs_guest",
                                   extraHeader=extensionStructsIncludeGuest)
        self.addGuestEncoderModule("goldfish_vk_marshaling_guest",
                                   extraHeader=commonCerealIncludesGuest + marshalIncludeGuest,
                                   extraImpl=commonCerealImplIncludesGuest)
        self.addGuestEncoderModule("goldfish_vk_deepcopy_guest",
                                   extraHeader=commonCerealIncludesGuest + poolIncludeGuest,
                                   extraImpl=commonCerealImplIncludesGuest)
        self.addGuestEncoderModule("goldfish_vk_handlemap_guest",
                                   extraHeader=commonCerealIncludesGuest + handleMapIncludeGuest,
                                   extraImpl=commonCerealImplIncludesGuest)
        self.addGuestEncoderModule("goldfish_vk_transform_guest",
                                   extraHeader=commonCerealIncludesGuest + transformIncludeGuest,
                                   extraImpl=commonCerealImplIncludesGuest + transformImplIncludeGuest)

        self.addGuestHalModule("func_table", extraImpl=functableImplInclude)

        self.addModule("common", "goldfish_vk_extension_structs",
                       extraHeader=extensionStructsInclude)
        self.addModule("common", "goldfish_vk_marshaling",
                       extraHeader=vulkanStreamIncludeHost,
                       extraImpl=commonCerealImplIncludes)
        self.addModule("common", "goldfish_vk_testing",
                       extraHeader=testingInclude,
                       extraImpl=commonCerealImplIncludes)
        self.addModule("common", "goldfish_vk_deepcopy",
                       extraHeader=poolInclude,
                       extraImpl=commonCerealImplIncludes)
        self.addModule("common", "goldfish_vk_handlemap",
                       extraHeader=handleMapInclude,
                       extraImpl=commonCerealImplIncludes)
        self.addModule("common", "goldfish_vk_dispatch",
                       extraHeader=dispatchHeaderDefs,
                       extraImpl=dispatchImplIncludes)
        self.addModule("common", "goldfish_vk_transform",
                       extraHeader=transformInclude,
                       extraImpl=transformImplInclude)
        self.addModule("common", "goldfish_vk_unbox",
                       extraHeader=unboxInclude,
                       extraImpl=unboxImplInclude)
        self.addHostModule("VkDecoder",
                           extraHeader=decoderHeaderIncludes,
                           extraImpl=decoderImplIncludes,
                           useNamespace=False)
        self.addHostModule("VkDecoderSnapshot",
                           extraHeader=decoderSnapshotHeaderIncludes,
                           extraImpl=decoderSnapshotImplIncludes,
                           useNamespace=False)

        self.addWrapper(cereal.VulkanEncoder, "VkEncoder")
        self.addWrapper(cereal.VulkanExtensionStructs, "goldfish_vk_extension_structs_guest")
        self.addWrapper(cereal.VulkanMarshaling, "goldfish_vk_marshaling_guest", variant = "guest")
        self.addWrapper(cereal.VulkanDeepcopy, "goldfish_vk_deepcopy_guest")
        self.addWrapper(cereal.VulkanHandleMap, "goldfish_vk_handlemap_guest")
        self.addWrapper(cereal.VulkanTransform, "goldfish_vk_transform_guest")
        self.addWrapper(cereal.VulkanFuncTable, "func_table")
        self.addWrapper(cereal.VulkanExtensionStructs, "goldfish_vk_extension_structs")
        self.addWrapper(cereal.VulkanMarshaling, "goldfish_vk_marshaling")
        self.addWrapper(cereal.VulkanTesting, "goldfish_vk_testing")
        self.addWrapper(cereal.VulkanDeepcopy, "goldfish_vk_deepcopy")
        self.addWrapper(cereal.VulkanHandleMap, "goldfish_vk_handlemap")
        self.addWrapper(cereal.VulkanDispatch, "goldfish_vk_dispatch")
        self.addWrapper(cereal.VulkanTransform, "goldfish_vk_transform", resourceTrackerTypeName="VkDecoderGlobalState")
        self.addWrapper(cereal.VulkanUnbox, "goldfish_vk_unbox")
        self.addWrapper(cereal.VulkanDecoder, "VkDecoder")
        self.addWrapper(cereal.VulkanDecoderSnapshot, "VkDecoderSnapshot")

        self.guestAndroidMkCppFiles = ""
        self.hostCMakeCppFiles = ""
        self.hostDecoderCMakeCppFiles = ""

        def addSrcEntry(m):
            mkSrcEntry = m.getMakefileSrcEntry()
            cmakeSrcEntry = m.getCMakeSrcEntry()
            if m.directory == self.guest_encoder_tag:
                self.guestAndroidMkCppFiles += mkSrcEntry
            elif m.directory == self.host_tag:
                self.hostDecoderCMakeCppFiles += cmakeSrcEntry
            elif m.directory != self.guest_hal_tag:
                self.hostCMakeCppFiles += cmakeSrcEntry

        self.forEachModule(addSrcEntry)