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