[vulkan] Representation of Vulkan resources
bug: 111137294
bug: 119104304
bug: 119157982
Even though the intent of the driver is to be as passthrough as
possible, we still need to have custom internal representations of
Vulkan resources:
1. Android Vulkan loader requires HWVULKAN_DISPATCH_MAGIC at the
beginning of dispatchable handles (instance, physical device, device,
queue, command buffer).
2. For non-dispatchable handles, we are going to have some level of
custom work anyway because
- VkDeviceMemory needs to have special handling for crossing VM
boundary, either with pipe streaming or DMA.
- Allocation callback support.
+ Rename to Resources.h/cpp, HandleWrappers isn't desriptive enough
Change-Id: Id1a554c9cf361a68adffb7b29940766172298fce
diff --git a/system/vulkan_enc/HandleWrappers.cpp b/system/vulkan_enc/HandleWrappers.cpp
deleted file mode 100644
index b12ef64..0000000
--- a/system/vulkan_enc/HandleWrappers.cpp
+++ /dev/null
@@ -1,51 +0,0 @@
-// Copyright (C) 2018 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-#include "HandleWrappers.h"
-
-#include <stdlib.h>
-
-extern "C" {
-
-#define GOLDFISH_VK_NEW_FROM_HOST_IMPL(type) \
- type new_from_host_##type(type underlying) { \
- struct goldfish_##type* res = \
- static_cast<goldfish_##type*>(malloc(sizeof(goldfish_##type))); \
- res->dispatch.magic = HWVULKAN_DISPATCH_MAGIC; \
- res->dispatch.vtbl = nullptr; \
- res->underlying = underlying; \
- return reinterpret_cast<type>(res); \
- } \
-
-#define GOLDFISH_VK_AS_GOLDFISH_IMPL(type) \
- struct goldfish_##type* as_goldfish_##type(type toCast) { \
- return reinterpret_cast<goldfish_##type*>(toCast); \
- } \
-
-#define GOLDFISH_VK_GET_HOST_IMPL(type) \
- type get_host_##type(type toUnwrap) { \
- auto as_goldfish = as_goldfish_##type(toUnwrap); \
- return as_goldfish->underlying; \
- } \
-
-#define GOLDFISH_VK_DELETE_GOLDFISH_IMPL(type) \
- void delete_goldfish_##type(type toDelete) { \
- free(as_goldfish_##type(toDelete)); \
- } \
-
-GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPESS(GOLDFISH_VK_NEW_FROM_HOST_IMPL)
-GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPESS(GOLDFISH_VK_AS_GOLDFISH_IMPL)
-GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPESS(GOLDFISH_VK_GET_HOST_IMPL)
-GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPESS(GOLDFISH_VK_DELETE_GOLDFISH_IMPL)
-
-} // extern "C"
\ No newline at end of file
diff --git a/system/vulkan_enc/HandleWrappers.h b/system/vulkan_enc/HandleWrappers.h
deleted file mode 100644
index 7f7ebe0..0000000
--- a/system/vulkan_enc/HandleWrappers.h
+++ /dev/null
@@ -1,52 +0,0 @@
-// Copyright (C) 2018 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-#pragma once
-
-#include <hardware/hwvulkan.h>
-#include <vulkan/vulkan.h>
-
-extern "C" {
-
-#define GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPESS(f) \
- f(VkInstance) \
- f(VkPhysicalDevice) \
- f(VkDevice) \
- f(VkQueue) \
- f(VkCommandBuffer) \
-
-#define GOLDFISH_VK_DEFINE_DISPATCHABLE_HANDLE_STRUCT(type) \
- struct goldfish_##type { \
- hwvulkan_dispatch_t dispatch; \
- type underlying; \
- }; \
-
-#define GOLDFISH_VK_NEW_FROM_HOST_DECL(type) \
- type new_from_host_##type(type);
-
-#define GOLDFISH_VK_AS_GOLDFISH_DECL(type) \
- struct goldfish_##type* as_goldfish_##type(type);
-
-#define GOLDFISH_VK_GET_HOST_DECL(type) \
- type get_host_##type(type);
-
-#define GOLDFISH_VK_DELETE_GOLDFISH_DECL(type) \
- void delete_goldfish_##type(type);
-
-GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPESS(GOLDFISH_VK_DEFINE_DISPATCHABLE_HANDLE_STRUCT)
-GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPESS(GOLDFISH_VK_NEW_FROM_HOST_DECL)
-GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPESS(GOLDFISH_VK_AS_GOLDFISH_DECL)
-GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPESS(GOLDFISH_VK_GET_HOST_DECL)
-GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPESS(GOLDFISH_VK_DELETE_GOLDFISH_DECL)
-
-} // extern "C"
\ No newline at end of file
diff --git a/system/vulkan_enc/Resources.cpp b/system/vulkan_enc/Resources.cpp
new file mode 100644
index 0000000..07f393b
--- /dev/null
+++ b/system/vulkan_enc/Resources.cpp
@@ -0,0 +1,128 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+#include "Resources.h"
+
+#include "android/base/AlignedBuf.h"
+
+#include <log/log.h>
+#include <stdlib.h>
+
+using android::aligned_buf_alloc;
+using android::aligned_buf_free;
+
+extern "C" {
+
+#define GOLDFISH_VK_NEW_DISPATCHABLE_FROM_HOST_IMPL(type) \
+ type new_from_host_##type(type underlying) { \
+ struct goldfish_##type* res = \
+ static_cast<goldfish_##type*>(malloc(sizeof(goldfish_##type))); \
+ if (!res) { \
+ ALOGE("FATAL: Failed to alloc " #type " handle"); \
+ abort(); \
+ } \
+ res->dispatch.magic = HWVULKAN_DISPATCH_MAGIC; \
+ res->underlying = underlying; \
+ return reinterpret_cast<type>(res); \
+ } \
+
+#define GOLDFISH_VK_NEW_TRIVIAL_NON_DISPATCHABLE_FROM_HOST_IMPL(type) \
+ type new_from_host_##type(type underlying) { \
+ struct goldfish_##type* res = \
+ static_cast<goldfish_##type*>(malloc(sizeof(goldfish_##type))); \
+ res->underlying = underlying; \
+ return reinterpret_cast<type>(res); \
+ } \
+
+#define GOLDFISH_VK_AS_GOLDFISH_IMPL(type) \
+ struct goldfish_##type* as_goldfish_##type(type toCast) { \
+ return reinterpret_cast<goldfish_##type*>(toCast); \
+ } \
+
+#define GOLDFISH_VK_GET_HOST_IMPL(type) \
+ type get_host_##type(type toUnwrap) { \
+ if (!toUnwrap) return VK_NULL_HANDLE; \
+ auto as_goldfish = as_goldfish_##type(toUnwrap); \
+ return as_goldfish->underlying; \
+ } \
+
+#define GOLDFISH_VK_DELETE_GOLDFISH_IMPL(type) \
+ void delete_goldfish_##type(type toDelete) { \
+ free(as_goldfish_##type(toDelete)); \
+ } \
+
+GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_DISPATCHABLE_FROM_HOST_IMPL)
+GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_AS_GOLDFISH_IMPL)
+GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_IMPL)
+GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DELETE_GOLDFISH_IMPL)
+
+GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_AS_GOLDFISH_IMPL)
+GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_IMPL)
+
+GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_TRIVIAL_NON_DISPATCHABLE_FROM_HOST_IMPL)
+GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DELETE_GOLDFISH_IMPL)
+
+// Custom definitions///////////////////////////////////////////////////////////
+
+VkDeviceMemory new_from_host_VkDeviceMemory(VkDeviceMemory mem) {
+ struct goldfish_VkDeviceMemory *res =
+ (struct goldfish_VkDeviceMemory *)malloc(sizeof(goldfish_VkDeviceMemory));
+
+ if (!res) {
+ ALOGE("FATAL: Failed to alloc VkDeviceMemory handle");
+ abort();
+ }
+
+ memset(res, 0x0, sizeof(goldfish_VkDeviceMemory));
+
+ res->underlying = mem;
+
+ return reinterpret_cast<VkDeviceMemory>(res);
+}
+
+void delete_goldfish_VkDeviceMemory(VkDeviceMemory mem) {
+ struct goldfish_VkDeviceMemory* goldfish_mem =
+ as_goldfish_VkDeviceMemory(mem);
+
+ if (goldfish_mem->ptr) {
+ // TODO: unmap the pointer with address space device
+ aligned_buf_free(goldfish_mem->ptr);
+ }
+
+ free(goldfish_mem);
+}
+
+void goldfish_VkDeviceMemory_allocate(
+ struct goldfish_VkDeviceMemory *mem,
+ VkDeviceSize size) {
+ // This is a strict alignment; we do not expect any
+ // actual device to have more stringent requirements
+ // than this.
+ mem->ptr = aligned_buf_alloc(4096, size);
+ mem->size = size;
+ // TODO: Use goldfish_address_space to obtain the pointer.
+}
+
+void* goldfish_VkDeviceMemory_map(
+ struct goldfish_VkDeviceMemory *mem,
+ VkDeviceSize offset, VkDeviceSize size) {
+
+ if (!mem->ptr) abort();
+ if (mem->size < offset + size) abort();
+
+ uint8_t* asBytes = (uint8_t*)mem->ptr;
+
+ return asBytes + offset;
+}
+
+} // extern "C"
\ No newline at end of file
diff --git a/system/vulkan_enc/Resources.h b/system/vulkan_enc/Resources.h
new file mode 100644
index 0000000..f3cdb15
--- /dev/null
+++ b/system/vulkan_enc/Resources.h
@@ -0,0 +1,117 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+#pragma once
+
+#include <hardware/hwvulkan.h>
+#include <vulkan/vulkan.h>
+
+extern "C" {
+
+#define GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(f) \
+ f(VkInstance) \
+ f(VkPhysicalDevice) \
+ f(VkDevice) \
+ f(VkQueue) \
+ f(VkCommandBuffer) \
+
+#define GOLDFISH_VK_DEFINE_DISPATCHABLE_HANDLE_STRUCT(type) \
+ struct goldfish_##type { \
+ hwvulkan_dispatch_t dispatch; \
+ type underlying; \
+ }; \
+
+#define GOLDFISH_VK_DEFINE_TRIVIAL_NON_DISPATCHABLE_HANDLE_STRUCT(type) \
+ struct goldfish_##type { \
+ type underlying; \
+ }; \
+
+#define GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(f) \
+ f(VkBuffer) \
+ f(VkBufferView) \
+ f(VkImage) \
+ f(VkImageView) \
+ f(VkShaderModule) \
+ f(VkDescriptorPool) \
+ f(VkDescriptorSetLayout) \
+ f(VkDescriptorSet) \
+ f(VkSampler) \
+ f(VkPipeline) \
+ f(VkPipelineCache) \
+ f(VkPipelineLayout) \
+ f(VkRenderPass) \
+ f(VkFramebuffer) \
+ f(VkCommandPool) \
+ f(VkFence) \
+ f(VkSemaphore) \
+ f(VkEvent) \
+ f(VkQueryPool) \
+ f(VkSamplerYcbcrConversion) \
+ f(VkDescriptorUpdateTemplate) \
+ f(VkSurfaceKHR) \
+ f(VkSwapchainKHR) \
+ f(VkDisplayKHR) \
+ f(VkDisplayModeKHR) \
+ f(VkObjectTableNVX) \
+ f(VkIndirectCommandsLayoutNVX) \
+ f(VkValidationCacheEXT) \
+ f(VkDebugReportCallbackEXT) \
+ f(VkDebugUtilsMessengerEXT) \
+
+#define GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(f) \
+ f(VkDeviceMemory) \
+ GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(f) \
+
+#define GOLDFISH_VK_NEW_FROM_HOST_DECL(type) \
+ type new_from_host_##type(type);
+
+#define GOLDFISH_VK_AS_GOLDFISH_DECL(type) \
+ struct goldfish_##type* as_goldfish_##type(type);
+
+#define GOLDFISH_VK_GET_HOST_DECL(type) \
+ type get_host_##type(type);
+
+#define GOLDFISH_VK_DELETE_GOLDFISH_DECL(type) \
+ void delete_goldfish_##type(type);
+
+GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DEFINE_DISPATCHABLE_HANDLE_STRUCT)
+GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_FROM_HOST_DECL)
+GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_AS_GOLDFISH_DECL)
+GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_DECL)
+GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DELETE_GOLDFISH_DECL)
+
+GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_NEW_FROM_HOST_DECL)
+GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_AS_GOLDFISH_DECL)
+GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_GET_HOST_DECL)
+GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DELETE_GOLDFISH_DECL)
+
+GOLDFISH_VK_LIST_TRIVIAL_NON_DISPATCHABLE_HANDLE_TYPES(GOLDFISH_VK_DEFINE_TRIVIAL_NON_DISPATCHABLE_HANDLE_STRUCT)
+
+// Custom definitions///////////////////////////////////////////////////////////
+
+struct goldfish_VkDeviceMemory {
+ VkDeviceMemory underlying;
+ void* ptr;
+ VkDeviceSize size;
+};
+
+void goldfish_VkDeviceMemory_allocate(
+ struct goldfish_VkDeviceMemory *mem,
+ VkDeviceSize size);
+
+void* goldfish_VkDeviceMemory_map(
+ struct goldfish_VkDeviceMemory *mem,
+ VkDeviceSize offset,
+ VkDeviceSize size);
+
+} // extern "C"
\ No newline at end of file