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