1 // Copyright (C) 2024 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <vulkan/vulkan.h>
18 
19 #include <optional>
20 
21 #include "gfxstream/host/Features.h"
22 
23 namespace gfxstream {
24 namespace vk {
25 
26 // A physical device may have memory types that are not desirable or are not
27 // supportable by the host renderer. This class helps to track the original
28 // host memory types, helps to track the emulated memory types shared with the
29 // guest, and helps to convert between both.
30 class EmulatedPhysicalDeviceMemoryProperties {
31    public:
32     EmulatedPhysicalDeviceMemoryProperties(const VkPhysicalDeviceMemoryProperties& host,
33                                            uint32_t hostColorBufferMemoryTypeIndex,
34                                            const gfxstream::host::FeatureSet& features);
35 
36     struct HostMemoryInfo {
37         uint32_t index;
38         VkMemoryType memoryType;
39     };
40     std::optional<HostMemoryInfo> getHostMemoryInfoFromHostMemoryTypeIndex(
41         uint32_t hostMemoryTypeIndex) const;
42     std::optional<HostMemoryInfo> getHostMemoryInfoFromGuestMemoryTypeIndex(
43         uint32_t guestMemoryTypeIndex) const;
44 
getGuestMemoryProperties()45     const VkPhysicalDeviceMemoryProperties& getGuestMemoryProperties() const {
46         return mGuestMemoryProperties;
47     }
getHostMemoryProperties()48     const VkPhysicalDeviceMemoryProperties& getHostMemoryProperties() const {
49         return mHostMemoryProperties;
50     }
51 
getGuestColorBufferMemoryTypeIndex()52     const uint32_t getGuestColorBufferMemoryTypeIndex() const {
53         return mGuestColorBufferMemoryTypeIndex;
54     }
55 
56     void transformToGuestMemoryRequirements(VkMemoryRequirements* hostMemoryRequirements) const;
57 
58    private:
59     VkPhysicalDeviceMemoryProperties mGuestMemoryProperties;
60     VkPhysicalDeviceMemoryProperties mHostMemoryProperties;
61     uint32_t mGuestToHostMemoryTypeIndexMap[VK_MAX_MEMORY_TYPES];
62     uint32_t mHostToGuestMemoryTypeIndexMap[VK_MAX_MEMORY_TYPES];
63 
64     // The memory type index reported to the guest for VkDeviceMemory requirements which would
65     // try to import host ColorBuffer allocations
66     // (e.g. vkGetAndroidHardwareBufferPropertiesANDROID()).
67     uint32_t mGuestColorBufferMemoryTypeIndex;
68 };
69 
70 }  // namespace vk
71 }  // namespace gfxstream