Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <memory> |
| 21 | |
| 22 | #include <gui/BufferQueue.h> |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 23 | #include <log/log.h> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 24 | #include <sync/sync.h> |
| 25 | |
| 26 | #include "loader.h" |
| 27 | |
| 28 | using namespace vulkan; |
| 29 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 30 | // TODO(jessehall): Currently we don't have a good error code for when a native |
| 31 | // window operation fails. Just returning INITIALIZATION_FAILED for now. Later |
| 32 | // versions (post SDK 0.9) of the API/extension have a better error code. |
| 33 | // When updating to that version, audit all error returns. |
| 34 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | // These functions/classes form an adaptor that allows objects to be refcounted |
| 39 | // by both android::sp<> and std::shared_ptr<> simultaneously, and delegates |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 40 | // allocation of the shared_ptr<> control structure to VkAllocationCallbacks. |
| 41 | // The |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 42 | // platform holds a reference to the ANativeWindow using its embedded reference |
| 43 | // count, and the ANativeWindow implementation holds references to the |
| 44 | // ANativeWindowBuffers using their embedded reference counts, so the |
| 45 | // shared_ptr *must* cooperate with these and hold at least one reference to |
| 46 | // the object using the embedded reference count. |
| 47 | |
| 48 | template <typename T> |
| 49 | struct NativeBaseDeleter { |
| 50 | void operator()(T* obj) { obj->common.decRef(&obj->common); } |
| 51 | }; |
| 52 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 53 | template <typename Host> |
| 54 | struct AllocScope {}; |
| 55 | |
| 56 | template <> |
| 57 | struct AllocScope<VkInstance> { |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 58 | static const VkSystemAllocationScope kScope = |
| 59 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | template <> |
| 63 | struct AllocScope<VkDevice> { |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 64 | static const VkSystemAllocationScope kScope = |
| 65 | VK_SYSTEM_ALLOCATION_SCOPE_DEVICE; |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 68 | template <typename T> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 69 | class VulkanAllocator { |
| 70 | public: |
| 71 | typedef T value_type; |
| 72 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 73 | VulkanAllocator(const VkAllocationCallbacks& allocator, |
| 74 | VkSystemAllocationScope scope) |
| 75 | : allocator_(allocator), scope_(scope) {} |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 76 | |
| 77 | template <typename U> |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 78 | explicit VulkanAllocator(const VulkanAllocator<U>& other) |
| 79 | : allocator_(other.allocator_), scope_(other.scope_) {} |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 80 | |
| 81 | T* allocate(size_t n) const { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 82 | return static_cast<T*>(allocator_.pfnAllocation( |
| 83 | allocator_.pUserData, n * sizeof(T), alignof(T), scope_)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 84 | } |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 85 | void deallocate(T* p, size_t) const { |
| 86 | return allocator_.pfnFree(allocator_.pUserData, p); |
| 87 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 88 | |
| 89 | private: |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 90 | template <typename U> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 91 | friend class VulkanAllocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 92 | const VkAllocationCallbacks& allocator_; |
| 93 | const VkSystemAllocationScope scope_; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 96 | template <typename T, typename Host> |
| 97 | std::shared_ptr<T> InitSharedPtr(Host host, T* obj) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 98 | obj->common.incRef(&obj->common); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 99 | return std::shared_ptr<T>( |
| 100 | obj, NativeBaseDeleter<T>(), |
| 101 | VulkanAllocator<T>(*GetAllocator(host), AllocScope<Host>::kScope)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // ---------------------------------------------------------------------------- |
| 105 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 106 | struct Surface { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 107 | std::shared_ptr<ANativeWindow> window; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | VkSurfaceKHR HandleFromSurface(Surface* surface) { |
| 111 | return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface)); |
| 112 | } |
| 113 | |
| 114 | Surface* SurfaceFromHandle(VkSurfaceKHR handle) { |
Jesse Hall | a3a7a1d | 2015-11-24 11:37:23 -0800 | [diff] [blame] | 115 | return reinterpret_cast<Surface*>(handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | struct Swapchain { |
| 119 | Swapchain(Surface& surface_, uint32_t num_images_) |
| 120 | : surface(surface_), num_images(num_images_) {} |
| 121 | |
| 122 | Surface& surface; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 123 | uint32_t num_images; |
| 124 | |
| 125 | struct Image { |
| 126 | Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {} |
| 127 | VkImage image; |
| 128 | std::shared_ptr<ANativeWindowBuffer> buffer; |
| 129 | // The fence is only valid when the buffer is dequeued, and should be |
| 130 | // -1 any other time. When valid, we own the fd, and must ensure it is |
| 131 | // closed: either by closing it explicitly when queueing the buffer, |
| 132 | // or by passing ownership e.g. to ANativeWindow::cancelBuffer(). |
| 133 | int dequeue_fence; |
| 134 | bool dequeued; |
| 135 | } images[android::BufferQueue::NUM_BUFFER_SLOTS]; |
| 136 | }; |
| 137 | |
| 138 | VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) { |
| 139 | return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain)); |
| 140 | } |
| 141 | |
| 142 | Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) { |
Jesse Hall | a3a7a1d | 2015-11-24 11:37:23 -0800 | [diff] [blame] | 143 | return reinterpret_cast<Swapchain*>(handle); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } // anonymous namespace |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 147 | |
| 148 | namespace vulkan { |
| 149 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 150 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 151 | VkResult CreateAndroidSurfaceKHR_Bottom(VkInstance instance, |
| 152 | ANativeWindow* window, |
| 153 | const VkAllocationCallbacks* allocator, |
| 154 | VkSurfaceKHR* out_surface) { |
| 155 | if (!allocator) |
| 156 | allocator = GetAllocator(instance); |
| 157 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface), |
| 158 | alignof(Surface), |
| 159 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 160 | if (!mem) |
| 161 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 162 | Surface* surface = new (mem) Surface; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 163 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 164 | surface->window = InitSharedPtr(instance, window); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 165 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 166 | // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN. |
| 167 | int err = |
| 168 | native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
| 169 | if (err != 0) { |
| 170 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 171 | // errors and translate them to valid Vulkan result codes? |
| 172 | ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err), |
| 173 | err); |
| 174 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 175 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 176 | return VK_ERROR_INITIALIZATION_FAILED; |
| 177 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 178 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 179 | *out_surface = HandleFromSurface(surface); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 180 | return VK_SUCCESS; |
| 181 | } |
| 182 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 183 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 184 | void DestroySurfaceKHR_Bottom(VkInstance instance, |
| 185 | VkSurfaceKHR surface_handle, |
| 186 | const VkAllocationCallbacks* allocator) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 187 | Surface* surface = SurfaceFromHandle(surface_handle); |
| 188 | if (!surface) |
| 189 | return; |
| 190 | native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
| 191 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 192 | if (!allocator) |
| 193 | allocator = GetAllocator(instance); |
| 194 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 197 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 198 | VkResult GetPhysicalDeviceSurfaceSupportKHR_Bottom(VkPhysicalDevice /*pdev*/, |
| 199 | uint32_t /*queue_family*/, |
| 200 | VkSurfaceKHR /*surface*/, |
| 201 | VkBool32* supported) { |
Jesse Hall | 0e74f00 | 2015-11-30 11:37:59 -0800 | [diff] [blame] | 202 | *supported = VK_TRUE; |
Jesse Hall | a642925 | 2015-11-29 18:59:42 -0800 | [diff] [blame] | 203 | return VK_SUCCESS; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 206 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 207 | VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Bottom( |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 208 | VkPhysicalDevice /*pdev*/, |
| 209 | VkSurfaceKHR surface, |
| 210 | VkSurfaceCapabilitiesKHR* capabilities) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 211 | int err; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 212 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 213 | |
| 214 | int width, height; |
| 215 | err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width); |
| 216 | if (err != 0) { |
| 217 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 218 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 219 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 220 | } |
| 221 | err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height); |
| 222 | if (err != 0) { |
| 223 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 224 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 225 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 228 | capabilities->currentExtent = VkExtent2D{width, height}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 229 | |
| 230 | // TODO(jessehall): Figure out what the min/max values should be. |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 231 | capabilities->minImageCount = 2; |
| 232 | capabilities->maxImageCount = 3; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 233 | |
| 234 | // TODO(jessehall): Figure out what the max extent should be. Maximum |
| 235 | // texture dimension maybe? |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 236 | capabilities->minImageExtent = VkExtent2D{1, 1}; |
| 237 | capabilities->maxImageExtent = VkExtent2D{4096, 4096}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 238 | |
| 239 | // TODO(jessehall): We can support all transforms, fix this once |
| 240 | // implemented. |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 241 | capabilities->supportedTransforms = VK_SURFACE_TRANSFORM_NONE_BIT_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 242 | |
| 243 | // TODO(jessehall): Implement based on NATIVE_WINDOW_TRANSFORM_HINT. |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 244 | capabilities->currentTransform = VK_SURFACE_TRANSFORM_NONE_BIT_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 245 | |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 246 | capabilities->maxImageArrayLayers = 1; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 247 | |
| 248 | // TODO(jessehall): I think these are right, but haven't thought hard about |
| 249 | // it. Do we need to query the driver for support of any of these? |
| 250 | // Currently not included: |
| 251 | // - VK_IMAGE_USAGE_GENERAL: maybe? does this imply cpu mappable? |
| 252 | // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not |
| 253 | // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 254 | capabilities->supportedUsageFlags = |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 255 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 256 | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | |
| 257 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 258 | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 259 | |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 260 | return VK_SUCCESS; |
| 261 | } |
| 262 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 263 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 264 | VkResult GetPhysicalDeviceSurfaceFormatsKHR_Bottom( |
| 265 | VkPhysicalDevice /*pdev*/, |
| 266 | VkSurfaceKHR /*surface*/, |
| 267 | uint32_t* count, |
| 268 | VkSurfaceFormatKHR* formats) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 269 | // TODO(jessehall): Fill out the set of supported formats. Longer term, add |
| 270 | // a new gralloc method to query whether a (format, usage) pair is |
| 271 | // supported, and check that for each gralloc format that corresponds to a |
| 272 | // Vulkan format. Shorter term, just add a few more formats to the ones |
| 273 | // hardcoded below. |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 274 | |
| 275 | const VkSurfaceFormatKHR kFormats[] = { |
| 276 | {VK_FORMAT_R8G8B8A8_UNORM, VK_COLORSPACE_SRGB_NONLINEAR_KHR}, |
| 277 | {VK_FORMAT_R8G8B8A8_SRGB, VK_COLORSPACE_SRGB_NONLINEAR_KHR}, |
| 278 | }; |
| 279 | const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]); |
| 280 | |
| 281 | VkResult result = VK_SUCCESS; |
| 282 | if (formats) { |
| 283 | if (*count < kNumFormats) |
| 284 | result = VK_INCOMPLETE; |
| 285 | std::copy(kFormats, kFormats + std::min(*count, kNumFormats), formats); |
| 286 | } |
| 287 | *count = kNumFormats; |
| 288 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 291 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 292 | VkResult GetPhysicalDeviceSurfacePresentModesKHR_Bottom( |
| 293 | VkPhysicalDevice /*pdev*/, |
| 294 | VkSurfaceKHR /*surface*/, |
| 295 | uint32_t* count, |
| 296 | VkPresentModeKHR* modes) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 297 | const VkPresentModeKHR kModes[] = { |
| 298 | VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR, |
| 299 | }; |
| 300 | const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]); |
| 301 | |
| 302 | VkResult result = VK_SUCCESS; |
| 303 | if (modes) { |
| 304 | if (*count < kNumModes) |
| 305 | result = VK_INCOMPLETE; |
| 306 | std::copy(kModes, kModes + std::min(*count, kNumModes), modes); |
| 307 | } |
| 308 | *count = kNumModes; |
| 309 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 312 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 313 | VkResult CreateSwapchainKHR_Bottom(VkDevice device, |
| 314 | const VkSwapchainCreateInfoKHR* create_info, |
| 315 | const VkAllocationCallbacks* allocator, |
| 316 | VkSwapchainKHR* swapchain_handle) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 317 | int err; |
| 318 | VkResult result = VK_SUCCESS; |
| 319 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 320 | if (!allocator) |
| 321 | allocator = GetAllocator(device); |
| 322 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 323 | ALOGV_IF(create_info->imageArraySize != 1, |
| 324 | "Swapchain imageArraySize (%u) != 1 not supported", |
| 325 | create_info->imageArraySize); |
| 326 | |
| 327 | ALOGE_IF(create_info->imageFormat != VK_FORMAT_R8G8B8A8_UNORM, |
| 328 | "swapchain formats other than R8G8B8A8_UNORM not yet implemented"); |
| 329 | ALOGE_IF(create_info->imageColorSpace != VK_COLORSPACE_SRGB_NONLINEAR_KHR, |
| 330 | "color spaces other than SRGB_NONLINEAR not yet implemented"); |
| 331 | ALOGE_IF(create_info->oldSwapchain, |
| 332 | "swapchain re-creation not yet implemented"); |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 333 | ALOGE_IF(create_info->preTransform != VK_SURFACE_TRANSFORM_NONE_BIT_KHR, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 334 | "swapchain preTransform not yet implemented"); |
| 335 | ALOGE_IF(create_info->presentMode != VK_PRESENT_MODE_FIFO_KHR, |
| 336 | "present modes other than FIFO are not yet implemented"); |
| 337 | |
| 338 | // -- Configure the native window -- |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 339 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 340 | Surface& surface = *SurfaceFromHandle(create_info->surface); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 341 | const DriverDispatchTable& dispatch = GetDriverDispatch(device); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 342 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 343 | err = native_window_set_buffers_dimensions(surface.window.get(), |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 344 | create_info->imageExtent.width, |
| 345 | create_info->imageExtent.height); |
| 346 | if (err != 0) { |
| 347 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 348 | // errors and translate them to valid Vulkan result codes? |
| 349 | ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)", |
| 350 | create_info->imageExtent.width, create_info->imageExtent.height, |
| 351 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 352 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 355 | err = native_window_set_scaling_mode( |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 356 | surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 357 | if (err != 0) { |
| 358 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 359 | // errors and translate them to valid Vulkan result codes? |
| 360 | ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)", |
| 361 | strerror(-err), err); |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 362 | return VK_ERROR_INITIALIZATION_FAILED; |
| 363 | } |
| 364 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 365 | uint32_t min_undequeued_buffers; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 366 | err = surface.window->query( |
| 367 | surface.window.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 368 | reinterpret_cast<int*>(&min_undequeued_buffers)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 369 | if (err != 0) { |
| 370 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 371 | // errors and translate them to valid Vulkan result codes? |
| 372 | ALOGE("window->query failed: %s (%d)", strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 373 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 374 | } |
| 375 | uint32_t num_images = |
| 376 | (create_info->minImageCount - 1) + min_undequeued_buffers; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 377 | err = native_window_set_buffer_count(surface.window.get(), num_images); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 378 | if (err != 0) { |
| 379 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 380 | // errors and translate them to valid Vulkan result codes? |
| 381 | ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err), |
| 382 | err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 383 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 386 | int gralloc_usage = 0; |
| 387 | // TODO(jessehall): Remove conditional once all drivers have been updated |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 388 | if (dispatch.GetSwapchainGrallocUsageANDROID) { |
| 389 | result = dispatch.GetSwapchainGrallocUsageANDROID( |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 390 | device, create_info->imageFormat, create_info->imageUsage, |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 391 | &gralloc_usage); |
| 392 | if (result != VK_SUCCESS) { |
| 393 | ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 394 | return VK_ERROR_INITIALIZATION_FAILED; |
| 395 | } |
| 396 | } else { |
| 397 | gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE; |
| 398 | } |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 399 | err = native_window_set_usage(surface.window.get(), gralloc_usage); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 400 | if (err != 0) { |
| 401 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 402 | // errors and translate them to valid Vulkan result codes? |
| 403 | ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 404 | return VK_ERROR_INITIALIZATION_FAILED; |
| 405 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 406 | |
| 407 | // -- Allocate our Swapchain object -- |
| 408 | // After this point, we must deallocate the swapchain on error. |
| 409 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 410 | void* mem = allocator->pfnAllocation(allocator->pUserData, |
| 411 | sizeof(Swapchain), alignof(Swapchain), |
| 412 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 413 | if (!mem) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 414 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 415 | Swapchain* swapchain = new (mem) Swapchain(surface, num_images); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 416 | |
| 417 | // -- Dequeue all buffers and create a VkImage for each -- |
| 418 | // Any failures during or after this must cancel the dequeued buffers. |
| 419 | |
| 420 | VkNativeBufferANDROID image_native_buffer = { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 421 | #pragma clang diagnostic push |
| 422 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 423 | .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID, |
| 424 | #pragma clang diagnostic pop |
| 425 | .pNext = nullptr, |
| 426 | }; |
| 427 | VkImageCreateInfo image_create = { |
| 428 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 429 | .pNext = &image_native_buffer, |
| 430 | .imageType = VK_IMAGE_TYPE_2D, |
| 431 | .format = VK_FORMAT_R8G8B8A8_UNORM, // TODO(jessehall) |
| 432 | .extent = {0, 0, 1}, |
| 433 | .mipLevels = 1, |
Jesse Hall | a15a4bf | 2015-11-19 22:48:02 -0800 | [diff] [blame] | 434 | .arrayLayers = 1, |
Jesse Hall | 091ed9e | 2015-11-30 00:55:29 -0800 | [diff] [blame] | 435 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 436 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 437 | .usage = create_info->imageUsage, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 438 | .flags = 0, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 439 | .sharingMode = create_info->imageSharingMode, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 440 | .queueFamilyIndexCount = create_info->queueFamilyIndexCount, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 441 | .pQueueFamilyIndices = create_info->pQueueFamilyIndices, |
| 442 | }; |
| 443 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 444 | for (uint32_t i = 0; i < num_images; i++) { |
| 445 | Swapchain::Image& img = swapchain->images[i]; |
| 446 | |
| 447 | ANativeWindowBuffer* buffer; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 448 | err = surface.window->dequeueBuffer(surface.window.get(), &buffer, |
| 449 | &img.dequeue_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 450 | if (err != 0) { |
| 451 | // TODO(jessehall): Improve error reporting. Can we enumerate |
| 452 | // possible errors and translate them to valid Vulkan result codes? |
| 453 | ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 454 | result = VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | img.buffer = InitSharedPtr(device, buffer); |
| 458 | img.dequeued = true; |
| 459 | |
| 460 | image_create.extent = |
| 461 | VkExtent3D{img.buffer->width, img.buffer->height, 1}; |
| 462 | image_native_buffer.handle = img.buffer->handle; |
| 463 | image_native_buffer.stride = img.buffer->stride; |
| 464 | image_native_buffer.format = img.buffer->format; |
| 465 | image_native_buffer.usage = img.buffer->usage; |
| 466 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 467 | result = |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 468 | dispatch.CreateImage(device, &image_create, nullptr, &img.image); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 469 | if (result != VK_SUCCESS) { |
| 470 | ALOGD("vkCreateImage w/ native buffer failed: %u", result); |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // -- Cancel all buffers, returning them to the queue -- |
| 476 | // If an error occurred before, also destroy the VkImage and release the |
| 477 | // buffer reference. Otherwise, we retain a strong reference to the buffer. |
| 478 | // |
| 479 | // TODO(jessehall): The error path here is the same as DestroySwapchain, |
| 480 | // but not the non-error path. Should refactor/unify. |
| 481 | for (uint32_t i = 0; i < num_images; i++) { |
| 482 | Swapchain::Image& img = swapchain->images[i]; |
| 483 | if (img.dequeued) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 484 | surface.window->cancelBuffer(surface.window.get(), img.buffer.get(), |
| 485 | img.dequeue_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 486 | img.dequeue_fence = -1; |
| 487 | img.dequeued = false; |
| 488 | } |
| 489 | if (result != VK_SUCCESS) { |
| 490 | if (img.image) |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 491 | dispatch.DestroyImage(device, img.image, nullptr); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | if (result != VK_SUCCESS) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 496 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 497 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 498 | return result; |
| 499 | } |
| 500 | |
| 501 | *swapchain_handle = HandleFromSwapchain(swapchain); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 502 | return VK_SUCCESS; |
| 503 | } |
| 504 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 505 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 506 | void DestroySwapchainKHR_Bottom(VkDevice device, |
| 507 | VkSwapchainKHR swapchain_handle, |
| 508 | const VkAllocationCallbacks* allocator) { |
| 509 | const DriverDispatchTable& dispatch = GetDriverDispatch(device); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 510 | Swapchain* swapchain = SwapchainFromHandle(swapchain_handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 511 | const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 512 | |
| 513 | for (uint32_t i = 0; i < swapchain->num_images; i++) { |
| 514 | Swapchain::Image& img = swapchain->images[i]; |
| 515 | if (img.dequeued) { |
| 516 | window->cancelBuffer(window.get(), img.buffer.get(), |
| 517 | img.dequeue_fence); |
| 518 | img.dequeue_fence = -1; |
| 519 | img.dequeued = false; |
| 520 | } |
| 521 | if (img.image) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 522 | dispatch.DestroyImage(device, img.image, nullptr); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 526 | if (!allocator) |
| 527 | allocator = GetAllocator(device); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 528 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 529 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 532 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 533 | VkResult GetSwapchainImagesKHR_Bottom(VkDevice, |
| 534 | VkSwapchainKHR swapchain_handle, |
| 535 | uint32_t* count, |
| 536 | VkImage* images) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 537 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
| 538 | VkResult result = VK_SUCCESS; |
| 539 | if (images) { |
| 540 | uint32_t n = swapchain.num_images; |
| 541 | if (*count < swapchain.num_images) { |
| 542 | n = *count; |
| 543 | result = VK_INCOMPLETE; |
| 544 | } |
| 545 | for (uint32_t i = 0; i < n; i++) |
| 546 | images[i] = swapchain.images[i].image; |
| 547 | } |
| 548 | *count = swapchain.num_images; |
| 549 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 552 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 553 | VkResult AcquireNextImageKHR_Bottom(VkDevice device, |
| 554 | VkSwapchainKHR swapchain_handle, |
| 555 | uint64_t timeout, |
| 556 | VkSemaphore semaphore, |
| 557 | VkFence vk_fence, |
| 558 | uint32_t* image_index) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 559 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 560 | ANativeWindow* window = swapchain.surface.window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 561 | VkResult result; |
| 562 | int err; |
| 563 | |
| 564 | ALOGW_IF( |
| 565 | timeout != UINT64_MAX, |
| 566 | "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented"); |
| 567 | |
| 568 | ANativeWindowBuffer* buffer; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 569 | int fence_fd; |
| 570 | err = window->dequeueBuffer(window, &buffer, &fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 571 | if (err != 0) { |
| 572 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 573 | // errors and translate them to valid Vulkan result codes? |
| 574 | ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 575 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | uint32_t idx; |
| 579 | for (idx = 0; idx < swapchain.num_images; idx++) { |
| 580 | if (swapchain.images[idx].buffer.get() == buffer) { |
| 581 | swapchain.images[idx].dequeued = true; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 582 | swapchain.images[idx].dequeue_fence = fence_fd; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 583 | break; |
| 584 | } |
| 585 | } |
| 586 | if (idx == swapchain.num_images) { |
| 587 | ALOGE("dequeueBuffer returned unrecognized buffer"); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 588 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 589 | return VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | int fence_clone = -1; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 593 | if (fence_fd != -1) { |
| 594 | fence_clone = dup(fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 595 | if (fence_clone == -1) { |
| 596 | ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", |
| 597 | strerror(errno), errno); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 598 | sync_wait(fence_fd, -1 /* forever */); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 602 | result = GetDriverDispatch(device).AcquireImageANDROID( |
| 603 | device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 604 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 605 | // NOTE: we're relying on AcquireImageANDROID to close fence_clone, |
| 606 | // even if the call fails. We could close it ourselves on failure, but |
| 607 | // that would create a race condition if the driver closes it on a |
| 608 | // failure path: some other thread might create an fd with the same |
| 609 | // number between the time the driver closes it and the time we close |
| 610 | // it. We must assume one of: the driver *always* closes it even on |
| 611 | // failure, or *never* closes it on failure. |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 612 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 613 | swapchain.images[idx].dequeued = false; |
| 614 | swapchain.images[idx].dequeue_fence = -1; |
| 615 | return result; |
| 616 | } |
| 617 | |
| 618 | *image_index = idx; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 619 | return VK_SUCCESS; |
| 620 | } |
| 621 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 622 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 623 | VkResult QueuePresentKHR_Bottom(VkQueue queue, |
| 624 | const VkPresentInfoKHR* present_info) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 625 | ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
| 626 | "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d", |
| 627 | present_info->sType); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 628 | ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL"); |
| 629 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 630 | const DriverDispatchTable& dispatch = GetDriverDispatch(queue); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 631 | VkResult final_result = VK_SUCCESS; |
| 632 | for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) { |
| 633 | Swapchain& swapchain = |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 634 | *SwapchainFromHandle(present_info->pSwapchains[sc]); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 635 | ANativeWindow* window = swapchain.surface.window.get(); |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 636 | uint32_t image_idx = present_info->pImageIndices[sc]; |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 637 | Swapchain::Image& img = swapchain.images[image_idx]; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 638 | VkResult result; |
| 639 | int err; |
| 640 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 641 | int fence = -1; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame^] | 642 | result = |
| 643 | dispatch.QueueSignalReleaseImageANDROID(queue, img.image, &fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 644 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 645 | ALOGE("QueueSignalReleaseImageANDROID failed: %d", result); |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 646 | if (present_info->pResults) |
| 647 | present_info->pResults[sc] = result; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 648 | if (final_result == VK_SUCCESS) |
| 649 | final_result = result; |
| 650 | // TODO(jessehall): What happens to the buffer here? Does the app |
| 651 | // still own it or not, i.e. should we cancel the buffer? Hard to |
| 652 | // do correctly without synchronizing, though I guess we could wait |
| 653 | // for the queue to idle. |
| 654 | continue; |
| 655 | } |
| 656 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 657 | err = window->queueBuffer(window, img.buffer.get(), fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 658 | if (err != 0) { |
| 659 | // TODO(jessehall): What now? We should probably cancel the buffer, |
| 660 | // I guess? |
| 661 | ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err); |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 662 | if (present_info->pResults) |
| 663 | present_info->pResults[sc] = result; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 664 | if (final_result == VK_SUCCESS) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 665 | final_result = VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 666 | continue; |
| 667 | } |
| 668 | |
| 669 | if (img.dequeue_fence != -1) { |
| 670 | close(img.dequeue_fence); |
| 671 | img.dequeue_fence = -1; |
| 672 | } |
| 673 | img.dequeued = false; |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 674 | |
| 675 | if (present_info->pResults) |
| 676 | present_info->pResults[sc] = VK_SUCCESS; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | return final_result; |
| 680 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 681 | |
| 682 | } // namespace vulkan |