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 | f9fa9a5 | 2016-01-08 16:08:51 -0800 | [diff] [blame] | 151 | VkResult CreateAndroidSurfaceKHR_Bottom( |
| 152 | VkInstance instance, |
| 153 | const VkAndroidSurfaceCreateInfoKHR* pCreateInfo, |
| 154 | const VkAllocationCallbacks* allocator, |
| 155 | VkSurfaceKHR* out_surface) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 156 | if (!allocator) |
| 157 | allocator = GetAllocator(instance); |
| 158 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface), |
| 159 | alignof(Surface), |
| 160 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 161 | if (!mem) |
| 162 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 163 | Surface* surface = new (mem) Surface; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 164 | |
Jesse Hall | f9fa9a5 | 2016-01-08 16:08:51 -0800 | [diff] [blame] | 165 | surface->window = InitSharedPtr(instance, pCreateInfo->window); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 166 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 167 | // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN. |
| 168 | int err = |
| 169 | native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
| 170 | if (err != 0) { |
| 171 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 172 | // errors and translate them to valid Vulkan result codes? |
| 173 | ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err), |
| 174 | err); |
| 175 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 176 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 177 | return VK_ERROR_INITIALIZATION_FAILED; |
| 178 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 179 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 180 | *out_surface = HandleFromSurface(surface); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 181 | return VK_SUCCESS; |
| 182 | } |
| 183 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 184 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 185 | void DestroySurfaceKHR_Bottom(VkInstance instance, |
| 186 | VkSurfaceKHR surface_handle, |
| 187 | const VkAllocationCallbacks* allocator) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 188 | Surface* surface = SurfaceFromHandle(surface_handle); |
| 189 | if (!surface) |
| 190 | return; |
| 191 | native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
| 192 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 193 | if (!allocator) |
| 194 | allocator = GetAllocator(instance); |
| 195 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 198 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 199 | VkResult GetPhysicalDeviceSurfaceSupportKHR_Bottom(VkPhysicalDevice /*pdev*/, |
| 200 | uint32_t /*queue_family*/, |
| 201 | VkSurfaceKHR /*surface*/, |
| 202 | VkBool32* supported) { |
Jesse Hall | 0e74f00 | 2015-11-30 11:37:59 -0800 | [diff] [blame] | 203 | *supported = VK_TRUE; |
Jesse Hall | a642925 | 2015-11-29 18:59:42 -0800 | [diff] [blame] | 204 | return VK_SUCCESS; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 207 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 208 | VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Bottom( |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 209 | VkPhysicalDevice /*pdev*/, |
| 210 | VkSurfaceKHR surface, |
| 211 | VkSurfaceCapabilitiesKHR* capabilities) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 212 | int err; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 213 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 214 | |
| 215 | int width, height; |
| 216 | err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width); |
| 217 | if (err != 0) { |
| 218 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 219 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 220 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 221 | } |
| 222 | err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height); |
| 223 | if (err != 0) { |
| 224 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 225 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 226 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 229 | capabilities->currentExtent = |
| 230 | VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 231 | |
| 232 | // TODO(jessehall): Figure out what the min/max values should be. |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 233 | capabilities->minImageCount = 2; |
| 234 | capabilities->maxImageCount = 3; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 235 | |
| 236 | // TODO(jessehall): Figure out what the max extent should be. Maximum |
| 237 | // texture dimension maybe? |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 238 | capabilities->minImageExtent = VkExtent2D{1, 1}; |
| 239 | capabilities->maxImageExtent = VkExtent2D{4096, 4096}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 240 | |
| 241 | // TODO(jessehall): We can support all transforms, fix this once |
| 242 | // implemented. |
Jesse Hall | f9fa9a5 | 2016-01-08 16:08:51 -0800 | [diff] [blame] | 243 | capabilities->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 244 | |
| 245 | // TODO(jessehall): Implement based on NATIVE_WINDOW_TRANSFORM_HINT. |
Jesse Hall | f9fa9a5 | 2016-01-08 16:08:51 -0800 | [diff] [blame] | 246 | capabilities->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 247 | |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 248 | capabilities->maxImageArrayLayers = 1; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 249 | |
| 250 | // TODO(jessehall): I think these are right, but haven't thought hard about |
| 251 | // it. Do we need to query the driver for support of any of these? |
| 252 | // Currently not included: |
| 253 | // - VK_IMAGE_USAGE_GENERAL: maybe? does this imply cpu mappable? |
| 254 | // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not |
| 255 | // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 256 | capabilities->supportedUsageFlags = |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 257 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 258 | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | |
| 259 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 260 | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 261 | |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 262 | return VK_SUCCESS; |
| 263 | } |
| 264 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 265 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 266 | VkResult GetPhysicalDeviceSurfaceFormatsKHR_Bottom( |
| 267 | VkPhysicalDevice /*pdev*/, |
| 268 | VkSurfaceKHR /*surface*/, |
| 269 | uint32_t* count, |
| 270 | VkSurfaceFormatKHR* formats) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 271 | // TODO(jessehall): Fill out the set of supported formats. Longer term, add |
| 272 | // a new gralloc method to query whether a (format, usage) pair is |
| 273 | // supported, and check that for each gralloc format that corresponds to a |
| 274 | // Vulkan format. Shorter term, just add a few more formats to the ones |
| 275 | // hardcoded below. |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 276 | |
| 277 | const VkSurfaceFormatKHR kFormats[] = { |
| 278 | {VK_FORMAT_R8G8B8A8_UNORM, VK_COLORSPACE_SRGB_NONLINEAR_KHR}, |
| 279 | {VK_FORMAT_R8G8B8A8_SRGB, VK_COLORSPACE_SRGB_NONLINEAR_KHR}, |
| 280 | }; |
| 281 | const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]); |
| 282 | |
| 283 | VkResult result = VK_SUCCESS; |
| 284 | if (formats) { |
| 285 | if (*count < kNumFormats) |
| 286 | result = VK_INCOMPLETE; |
| 287 | std::copy(kFormats, kFormats + std::min(*count, kNumFormats), formats); |
| 288 | } |
| 289 | *count = kNumFormats; |
| 290 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 293 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 294 | VkResult GetPhysicalDeviceSurfacePresentModesKHR_Bottom( |
| 295 | VkPhysicalDevice /*pdev*/, |
| 296 | VkSurfaceKHR /*surface*/, |
| 297 | uint32_t* count, |
| 298 | VkPresentModeKHR* modes) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 299 | const VkPresentModeKHR kModes[] = { |
| 300 | VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR, |
| 301 | }; |
| 302 | const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]); |
| 303 | |
| 304 | VkResult result = VK_SUCCESS; |
| 305 | if (modes) { |
| 306 | if (*count < kNumModes) |
| 307 | result = VK_INCOMPLETE; |
| 308 | std::copy(kModes, kModes + std::min(*count, kNumModes), modes); |
| 309 | } |
| 310 | *count = kNumModes; |
| 311 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 314 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 315 | VkResult CreateSwapchainKHR_Bottom(VkDevice device, |
| 316 | const VkSwapchainCreateInfoKHR* create_info, |
| 317 | const VkAllocationCallbacks* allocator, |
| 318 | VkSwapchainKHR* swapchain_handle) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 319 | int err; |
| 320 | VkResult result = VK_SUCCESS; |
| 321 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 322 | if (!allocator) |
| 323 | allocator = GetAllocator(device); |
| 324 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 325 | ALOGV_IF(create_info->imageArraySize != 1, |
| 326 | "Swapchain imageArraySize (%u) != 1 not supported", |
| 327 | create_info->imageArraySize); |
| 328 | |
| 329 | ALOGE_IF(create_info->imageFormat != VK_FORMAT_R8G8B8A8_UNORM, |
| 330 | "swapchain formats other than R8G8B8A8_UNORM not yet implemented"); |
| 331 | ALOGE_IF(create_info->imageColorSpace != VK_COLORSPACE_SRGB_NONLINEAR_KHR, |
| 332 | "color spaces other than SRGB_NONLINEAR not yet implemented"); |
| 333 | ALOGE_IF(create_info->oldSwapchain, |
| 334 | "swapchain re-creation not yet implemented"); |
Jesse Hall | f9fa9a5 | 2016-01-08 16:08:51 -0800 | [diff] [blame] | 335 | ALOGE_IF(create_info->preTransform != VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 336 | "swapchain preTransform not yet implemented"); |
| 337 | ALOGE_IF(create_info->presentMode != VK_PRESENT_MODE_FIFO_KHR, |
| 338 | "present modes other than FIFO are not yet implemented"); |
| 339 | |
| 340 | // -- Configure the native window -- |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 341 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 342 | Surface& surface = *SurfaceFromHandle(create_info->surface); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 343 | const DriverDispatchTable& dispatch = GetDriverDispatch(device); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 344 | |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 345 | err = native_window_set_buffers_dimensions( |
| 346 | surface.window.get(), static_cast<int>(create_info->imageExtent.width), |
| 347 | static_cast<int>(create_info->imageExtent.height)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 348 | if (err != 0) { |
| 349 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 350 | // errors and translate them to valid Vulkan result codes? |
| 351 | ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)", |
| 352 | create_info->imageExtent.width, create_info->imageExtent.height, |
| 353 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 354 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 357 | err = native_window_set_scaling_mode( |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 358 | surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 359 | if (err != 0) { |
| 360 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 361 | // errors and translate them to valid Vulkan result codes? |
| 362 | ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)", |
| 363 | strerror(-err), err); |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 364 | return VK_ERROR_INITIALIZATION_FAILED; |
| 365 | } |
| 366 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 367 | uint32_t min_undequeued_buffers; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 368 | err = surface.window->query( |
| 369 | surface.window.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 370 | reinterpret_cast<int*>(&min_undequeued_buffers)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 371 | if (err != 0) { |
| 372 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 373 | // errors and translate them to valid Vulkan result codes? |
| 374 | ALOGE("window->query failed: %s (%d)", strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 375 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 376 | } |
| 377 | uint32_t num_images = |
| 378 | (create_info->minImageCount - 1) + min_undequeued_buffers; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 379 | err = native_window_set_buffer_count(surface.window.get(), num_images); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 380 | if (err != 0) { |
| 381 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 382 | // errors and translate them to valid Vulkan result codes? |
| 383 | ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err), |
| 384 | err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 385 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 388 | int gralloc_usage = 0; |
| 389 | // TODO(jessehall): Remove conditional once all drivers have been updated |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 390 | if (dispatch.GetSwapchainGrallocUsageANDROID) { |
| 391 | result = dispatch.GetSwapchainGrallocUsageANDROID( |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 392 | device, create_info->imageFormat, create_info->imageUsage, |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 393 | &gralloc_usage); |
| 394 | if (result != VK_SUCCESS) { |
| 395 | ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 396 | return VK_ERROR_INITIALIZATION_FAILED; |
| 397 | } |
| 398 | } else { |
| 399 | gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE; |
| 400 | } |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 401 | err = native_window_set_usage(surface.window.get(), gralloc_usage); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 402 | if (err != 0) { |
| 403 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 404 | // errors and translate them to valid Vulkan result codes? |
| 405 | ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 406 | return VK_ERROR_INITIALIZATION_FAILED; |
| 407 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 408 | |
| 409 | // -- Allocate our Swapchain object -- |
| 410 | // After this point, we must deallocate the swapchain on error. |
| 411 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 412 | void* mem = allocator->pfnAllocation(allocator->pUserData, |
| 413 | sizeof(Swapchain), alignof(Swapchain), |
| 414 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 415 | if (!mem) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 416 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 417 | Swapchain* swapchain = new (mem) Swapchain(surface, num_images); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 418 | |
| 419 | // -- Dequeue all buffers and create a VkImage for each -- |
| 420 | // Any failures during or after this must cancel the dequeued buffers. |
| 421 | |
| 422 | VkNativeBufferANDROID image_native_buffer = { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 423 | #pragma clang diagnostic push |
| 424 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 425 | .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID, |
| 426 | #pragma clang diagnostic pop |
| 427 | .pNext = nullptr, |
| 428 | }; |
| 429 | VkImageCreateInfo image_create = { |
| 430 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 431 | .pNext = &image_native_buffer, |
| 432 | .imageType = VK_IMAGE_TYPE_2D, |
| 433 | .format = VK_FORMAT_R8G8B8A8_UNORM, // TODO(jessehall) |
| 434 | .extent = {0, 0, 1}, |
| 435 | .mipLevels = 1, |
Jesse Hall | a15a4bf | 2015-11-19 22:48:02 -0800 | [diff] [blame] | 436 | .arrayLayers = 1, |
Jesse Hall | 091ed9e | 2015-11-30 00:55:29 -0800 | [diff] [blame] | 437 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 438 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 439 | .usage = create_info->imageUsage, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 440 | .flags = 0, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 441 | .sharingMode = create_info->imageSharingMode, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 442 | .queueFamilyIndexCount = create_info->queueFamilyIndexCount, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 443 | .pQueueFamilyIndices = create_info->pQueueFamilyIndices, |
| 444 | }; |
| 445 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 446 | for (uint32_t i = 0; i < num_images; i++) { |
| 447 | Swapchain::Image& img = swapchain->images[i]; |
| 448 | |
| 449 | ANativeWindowBuffer* buffer; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 450 | err = surface.window->dequeueBuffer(surface.window.get(), &buffer, |
| 451 | &img.dequeue_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 452 | if (err != 0) { |
| 453 | // TODO(jessehall): Improve error reporting. Can we enumerate |
| 454 | // possible errors and translate them to valid Vulkan result codes? |
| 455 | ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 456 | result = VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 457 | break; |
| 458 | } |
| 459 | img.buffer = InitSharedPtr(device, buffer); |
| 460 | img.dequeued = true; |
| 461 | |
| 462 | image_create.extent = |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 463 | VkExtent3D{static_cast<uint32_t>(img.buffer->width), |
| 464 | static_cast<uint32_t>(img.buffer->height), |
| 465 | 1}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 466 | image_native_buffer.handle = img.buffer->handle; |
| 467 | image_native_buffer.stride = img.buffer->stride; |
| 468 | image_native_buffer.format = img.buffer->format; |
| 469 | image_native_buffer.usage = img.buffer->usage; |
| 470 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 471 | result = |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 472 | dispatch.CreateImage(device, &image_create, nullptr, &img.image); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 473 | if (result != VK_SUCCESS) { |
| 474 | ALOGD("vkCreateImage w/ native buffer failed: %u", result); |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // -- Cancel all buffers, returning them to the queue -- |
| 480 | // If an error occurred before, also destroy the VkImage and release the |
| 481 | // buffer reference. Otherwise, we retain a strong reference to the buffer. |
| 482 | // |
| 483 | // TODO(jessehall): The error path here is the same as DestroySwapchain, |
| 484 | // but not the non-error path. Should refactor/unify. |
| 485 | for (uint32_t i = 0; i < num_images; i++) { |
| 486 | Swapchain::Image& img = swapchain->images[i]; |
| 487 | if (img.dequeued) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 488 | surface.window->cancelBuffer(surface.window.get(), img.buffer.get(), |
| 489 | img.dequeue_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 490 | img.dequeue_fence = -1; |
| 491 | img.dequeued = false; |
| 492 | } |
| 493 | if (result != VK_SUCCESS) { |
| 494 | if (img.image) |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 495 | dispatch.DestroyImage(device, img.image, nullptr); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | if (result != VK_SUCCESS) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 500 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 501 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 502 | return result; |
| 503 | } |
| 504 | |
| 505 | *swapchain_handle = HandleFromSwapchain(swapchain); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 506 | return VK_SUCCESS; |
| 507 | } |
| 508 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 509 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 510 | void DestroySwapchainKHR_Bottom(VkDevice device, |
| 511 | VkSwapchainKHR swapchain_handle, |
| 512 | const VkAllocationCallbacks* allocator) { |
| 513 | const DriverDispatchTable& dispatch = GetDriverDispatch(device); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 514 | Swapchain* swapchain = SwapchainFromHandle(swapchain_handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 515 | const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 516 | |
| 517 | for (uint32_t i = 0; i < swapchain->num_images; i++) { |
| 518 | Swapchain::Image& img = swapchain->images[i]; |
| 519 | if (img.dequeued) { |
| 520 | window->cancelBuffer(window.get(), img.buffer.get(), |
| 521 | img.dequeue_fence); |
| 522 | img.dequeue_fence = -1; |
| 523 | img.dequeued = false; |
| 524 | } |
| 525 | if (img.image) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 526 | dispatch.DestroyImage(device, img.image, nullptr); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 530 | if (!allocator) |
| 531 | allocator = GetAllocator(device); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 532 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 533 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 536 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 537 | VkResult GetSwapchainImagesKHR_Bottom(VkDevice, |
| 538 | VkSwapchainKHR swapchain_handle, |
| 539 | uint32_t* count, |
| 540 | VkImage* images) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 541 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
| 542 | VkResult result = VK_SUCCESS; |
| 543 | if (images) { |
| 544 | uint32_t n = swapchain.num_images; |
| 545 | if (*count < swapchain.num_images) { |
| 546 | n = *count; |
| 547 | result = VK_INCOMPLETE; |
| 548 | } |
| 549 | for (uint32_t i = 0; i < n; i++) |
| 550 | images[i] = swapchain.images[i].image; |
| 551 | } |
| 552 | *count = swapchain.num_images; |
| 553 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 556 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 557 | VkResult AcquireNextImageKHR_Bottom(VkDevice device, |
| 558 | VkSwapchainKHR swapchain_handle, |
| 559 | uint64_t timeout, |
| 560 | VkSemaphore semaphore, |
| 561 | VkFence vk_fence, |
| 562 | uint32_t* image_index) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 563 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 564 | ANativeWindow* window = swapchain.surface.window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 565 | VkResult result; |
| 566 | int err; |
| 567 | |
| 568 | ALOGW_IF( |
| 569 | timeout != UINT64_MAX, |
| 570 | "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented"); |
| 571 | |
| 572 | ANativeWindowBuffer* buffer; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 573 | int fence_fd; |
| 574 | err = window->dequeueBuffer(window, &buffer, &fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 575 | if (err != 0) { |
| 576 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 577 | // errors and translate them to valid Vulkan result codes? |
| 578 | ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 579 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | uint32_t idx; |
| 583 | for (idx = 0; idx < swapchain.num_images; idx++) { |
| 584 | if (swapchain.images[idx].buffer.get() == buffer) { |
| 585 | swapchain.images[idx].dequeued = true; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 586 | swapchain.images[idx].dequeue_fence = fence_fd; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 587 | break; |
| 588 | } |
| 589 | } |
| 590 | if (idx == swapchain.num_images) { |
| 591 | ALOGE("dequeueBuffer returned unrecognized buffer"); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 592 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 593 | return VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | int fence_clone = -1; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 597 | if (fence_fd != -1) { |
| 598 | fence_clone = dup(fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 599 | if (fence_clone == -1) { |
| 600 | ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", |
| 601 | strerror(errno), errno); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 602 | sync_wait(fence_fd, -1 /* forever */); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 606 | result = GetDriverDispatch(device).AcquireImageANDROID( |
| 607 | device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 608 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 609 | // NOTE: we're relying on AcquireImageANDROID to close fence_clone, |
| 610 | // even if the call fails. We could close it ourselves on failure, but |
| 611 | // that would create a race condition if the driver closes it on a |
| 612 | // failure path: some other thread might create an fd with the same |
| 613 | // number between the time the driver closes it and the time we close |
| 614 | // it. We must assume one of: the driver *always* closes it even on |
| 615 | // failure, or *never* closes it on failure. |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 616 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 617 | swapchain.images[idx].dequeued = false; |
| 618 | swapchain.images[idx].dequeue_fence = -1; |
| 619 | return result; |
| 620 | } |
| 621 | |
| 622 | *image_index = idx; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 623 | return VK_SUCCESS; |
| 624 | } |
| 625 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 626 | VKAPI_ATTR |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 627 | VkResult QueuePresentKHR_Bottom(VkQueue queue, |
| 628 | const VkPresentInfoKHR* present_info) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 629 | ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
| 630 | "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d", |
| 631 | present_info->sType); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 632 | ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL"); |
| 633 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 634 | const DriverDispatchTable& dispatch = GetDriverDispatch(queue); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 635 | VkResult final_result = VK_SUCCESS; |
| 636 | for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) { |
| 637 | Swapchain& swapchain = |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 638 | *SwapchainFromHandle(present_info->pSwapchains[sc]); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 639 | ANativeWindow* window = swapchain.surface.window.get(); |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 640 | uint32_t image_idx = present_info->pImageIndices[sc]; |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 641 | Swapchain::Image& img = swapchain.images[image_idx]; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 642 | VkResult result; |
| 643 | int err; |
| 644 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 645 | int fence = -1; |
Jesse Hall | 275d76c | 2016-01-08 22:39:16 -0800 | [diff] [blame^] | 646 | result = dispatch.QueueSignalReleaseImageANDROID( |
| 647 | queue, present_info->waitSemaphoreCount, |
| 648 | present_info->pWaitSemaphores, img.image, &fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 649 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 650 | ALOGE("QueueSignalReleaseImageANDROID failed: %d", result); |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 651 | if (present_info->pResults) |
| 652 | present_info->pResults[sc] = result; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 653 | if (final_result == VK_SUCCESS) |
| 654 | final_result = result; |
| 655 | // TODO(jessehall): What happens to the buffer here? Does the app |
| 656 | // still own it or not, i.e. should we cancel the buffer? Hard to |
| 657 | // do correctly without synchronizing, though I guess we could wait |
| 658 | // for the queue to idle. |
| 659 | continue; |
| 660 | } |
| 661 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 662 | err = window->queueBuffer(window, img.buffer.get(), fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 663 | if (err != 0) { |
| 664 | // TODO(jessehall): What now? We should probably cancel the buffer, |
| 665 | // I guess? |
| 666 | ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err); |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 667 | if (present_info->pResults) |
| 668 | present_info->pResults[sc] = result; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 669 | if (final_result == VK_SUCCESS) |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 670 | final_result = VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 671 | continue; |
| 672 | } |
| 673 | |
| 674 | if (img.dequeue_fence != -1) { |
| 675 | close(img.dequeue_fence); |
| 676 | img.dequeue_fence = -1; |
| 677 | } |
| 678 | img.dequeued = false; |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 679 | |
| 680 | if (present_info->pResults) |
| 681 | present_info->pResults[sc] = VK_SUCCESS; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | return final_result; |
| 685 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 686 | |
| 687 | } // namespace vulkan |