• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include "VkCommonOperations.h"
17 
18 #include <vulkan/vulkan.h>
19 
20 #include "cereal/common/goldfish_vk_private_defs.h"
21 
22 #include <vector>
23 
24 namespace goldfish_vk {
25 
26 struct VulkanDispatch;
27 
28 // This class provides methods to create and query information about Android
29 // native buffers in the context of creating Android swapchain images that have
30 // Android native buffer backing.
31 
32 // This is to be refactored to move to external memory only once we get that
33 // working.
34 
35 struct AndroidNativeBufferInfo {
36     VkDevice device = VK_NULL_HANDLE;
37     VkFormat vkFormat;
38     VkExtent3D extent;
39     VkImageUsageFlags usage;
40     VkSharingMode sharingMode;
41     std::vector<uint32_t> queueFamilyIndices;
42 
43     int format;
44     int stride;
45     uint32_t colorBufferHandle;
46     bool externallyBacked = false;
47     bool isGlTexture = false;
48 
49     // We will be using separate allocations for image versus staging memory,
50     // because not all host Vulkan drivers will support directly rendering to
51     // host visible memory in a layout that glTexSubImage2D can consume.
52 
53     // If we are using external memory, these memories are imported
54     // to the current instance.
55     VkDeviceMemory imageMemory = VK_NULL_HANDLE;
56     VkDeviceMemory stagingMemory = VK_NULL_HANDLE;
57 
58     VkBuffer stagingBuffer = VK_NULL_HANDLE;
59 
60     uint32_t imageMemoryTypeIndex;
61     uint32_t stagingMemoryTypeIndex;
62 
63     uint8_t* mappedStagingPtr = nullptr;
64 
65     // To be populated later as we go.
66     VkImage image = VK_NULL_HANDLE;
67     VkMemoryRequirements memReqs;
68 
69     // The queue over which we send the buffer/image copy commands depends on
70     // the queue over which vkQueueSignalReleaseImageANDROID happens.
71     // It is assumed that the VkImage object has been created by Android swapchain layer
72     // with all the relevant queue family indices for sharing set properly.
73     struct QueueState {
74         VkQueue queue = VK_NULL_HANDLE;
75         VkCommandPool pool = VK_NULL_HANDLE;
76         VkCommandBuffer cb = VK_NULL_HANDLE;
77         VkCommandBuffer cb2 = VK_NULL_HANDLE;
78         VkFence fence = VK_NULL_HANDLE;
79         uint32_t queueFamilyIndex = 0;
80         void setup(
81             VulkanDispatch* vk,
82             VkDevice device,
83             VkQueue queue,
84             uint32_t queueFamilyIndex);
85         void teardown(VulkanDispatch* vk, VkDevice device);
86     };
87     // We keep one QueueState for each queue family index used by the guest
88     // in vkQueuePresentKHR.
89     std::vector<QueueState> queueStates;
90     // Did we ever sync the Vulkan image with a ColorBuffer?
91     // If so, set everSynced along with the queue family index
92     // used to do that.
93     // If the swapchain image was created with exclusive sharing
94     // mode (reflected in this struct's |sharingMode| field),
95     // this part doesn't really matter.
96     bool everSynced = false;
97     uint32_t lastUsedQueueFamilyIndex;
98 
99     // On first acquire, we might use a different queue family
100     // to initially set the semaphore/fence to be signaled.
101     // Track that here.
102     bool everAcquired = false;
103     QueueState acquireQueueState;
104 };
105 
106 VkResult prepareAndroidNativeBufferImage(
107     VulkanDispatch* vk,
108     VkDevice device,
109     const VkImageCreateInfo* pCreateInfo,
110     const VkNativeBufferANDROID* nativeBufferANDROID,
111     const VkAllocationCallbacks* pAllocator,
112     const VkPhysicalDeviceMemoryProperties* memProps,
113     AndroidNativeBufferInfo* out);
114 
115 void teardownAndroidNativeBufferImage(
116     VulkanDispatch* vk,
117     AndroidNativeBufferInfo* anbInfo);
118 
119 void getGralloc0Usage(VkFormat format, VkImageUsageFlags imageUsage,
120                       int* usage_out);
121 void getGralloc1Usage(VkFormat format, VkImageUsageFlags imageUsage,
122                       VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
123                       uint64_t* consumerUsage_out,
124                       uint64_t* producerUsage_out);
125 
126 VkResult setAndroidNativeImageSemaphoreSignaled(
127     VulkanDispatch* vk,
128     VkDevice device,
129     VkQueue defaultQueue,
130     uint32_t defaultQueueFamilyIndex,
131     VkSemaphore semaphore,
132     VkFence fence,
133     AndroidNativeBufferInfo* anbInfo);
134 
135 VkResult syncImageToColorBuffer(
136     VulkanDispatch* vk,
137     uint32_t queueFamilyIndex,
138     VkQueue queue,
139     uint32_t waitSemaphoreCount,
140     const VkSemaphore* pWaitSemaphores,
141     int* pNativeFenceFd,
142     AndroidNativeBufferInfo* anbInfo);
143 
144 } // namespace goldfish_vk
145