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> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 18 | |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 19 | #include <log/log.h> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 20 | #include <gui/BufferQueue.h> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 21 | #include <sync/sync.h> |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 22 | #include <utils/StrongPointer.h> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 23 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 24 | #include "driver.h" |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 25 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 26 | // TODO(jessehall): Currently we don't have a good error code for when a native |
| 27 | // window operation fails. Just returning INITIALIZATION_FAILED for now. Later |
| 28 | // versions (post SDK 0.9) of the API/extension have a better error code. |
| 29 | // When updating to that version, audit all error returns. |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 30 | namespace vulkan { |
| 31 | namespace driver { |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 32 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 33 | namespace { |
| 34 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 35 | const VkSurfaceTransformFlagsKHR kSupportedTransforms = |
| 36 | VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR | |
| 37 | VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR | |
| 38 | VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR | |
| 39 | VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR | |
| 40 | // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform. |
| 41 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR | |
| 42 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR | |
| 43 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR | |
| 44 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR | |
| 45 | VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR; |
| 46 | |
| 47 | VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) { |
| 48 | // Native and Vulkan transforms are isomorphic, but are represented |
| 49 | // differently. Vulkan transforms are built up of an optional horizontal |
| 50 | // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native |
| 51 | // transforms are built up from a horizontal flip, vertical flip, and |
| 52 | // 90-degree rotation, all optional but always in that order. |
| 53 | |
| 54 | // TODO(jessehall): For now, only support pure rotations, not |
| 55 | // flip or flip-and-rotate, until I have more time to test them and build |
| 56 | // sample code. As far as I know we never actually use anything besides |
| 57 | // pure rotations anyway. |
| 58 | |
| 59 | switch (native) { |
| 60 | case 0: // 0x0 |
| 61 | return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 62 | // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1 |
| 63 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR; |
| 64 | // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2 |
| 65 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR; |
| 66 | case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V |
| 67 | return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR; |
| 68 | case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4 |
| 69 | return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR; |
| 70 | // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90: |
| 71 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR; |
| 72 | // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90: |
| 73 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR; |
| 74 | case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90 |
| 75 | return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR; |
| 76 | case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY: |
| 77 | default: |
| 78 | return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 79 | } |
| 80 | } |
| 81 | |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 82 | int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) { |
| 83 | switch (transform) { |
| 84 | case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR: |
| 85 | return NATIVE_WINDOW_TRANSFORM_ROT_270; |
| 86 | case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR: |
| 87 | return NATIVE_WINDOW_TRANSFORM_ROT_180; |
| 88 | case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR: |
| 89 | return NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 90 | // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform. |
| 91 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR: |
| 92 | // return NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 93 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR: |
| 94 | // return NATIVE_WINDOW_TRANSFORM_FLIP_H | |
| 95 | // NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 96 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR: |
| 97 | // return NATIVE_WINDOW_TRANSFORM_FLIP_V; |
| 98 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR: |
| 99 | // return NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 100 | // NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 101 | case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR: |
| 102 | case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR: |
| 103 | default: |
| 104 | return 0; |
| 105 | } |
| 106 | } |
| 107 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 108 | // ---------------------------------------------------------------------------- |
| 109 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 110 | struct Surface { |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 111 | android::sp<ANativeWindow> window; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 112 | VkSwapchainKHR swapchain_handle; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | VkSurfaceKHR HandleFromSurface(Surface* surface) { |
| 116 | return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface)); |
| 117 | } |
| 118 | |
| 119 | Surface* SurfaceFromHandle(VkSurfaceKHR handle) { |
Jesse Hall | a3a7a1d | 2015-11-24 11:37:23 -0800 | [diff] [blame] | 120 | return reinterpret_cast<Surface*>(handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | struct Swapchain { |
| 124 | Swapchain(Surface& surface_, uint32_t num_images_) |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 125 | : surface(surface_), |
| 126 | num_images(num_images_), |
| 127 | frame_timestamps_enabled(false) {} |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 128 | |
| 129 | Surface& surface; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 130 | uint32_t num_images; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 131 | bool frame_timestamps_enabled; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 132 | |
| 133 | struct Image { |
| 134 | Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {} |
| 135 | VkImage image; |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 136 | android::sp<ANativeWindowBuffer> buffer; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 137 | // The fence is only valid when the buffer is dequeued, and should be |
| 138 | // -1 any other time. When valid, we own the fd, and must ensure it is |
| 139 | // closed: either by closing it explicitly when queueing the buffer, |
| 140 | // or by passing ownership e.g. to ANativeWindow::cancelBuffer(). |
| 141 | int dequeue_fence; |
| 142 | bool dequeued; |
| 143 | } images[android::BufferQueue::NUM_BUFFER_SLOTS]; |
| 144 | }; |
| 145 | |
| 146 | VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) { |
| 147 | return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain)); |
| 148 | } |
| 149 | |
| 150 | Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) { |
Jesse Hall | a3a7a1d | 2015-11-24 11:37:23 -0800 | [diff] [blame] | 151 | return reinterpret_cast<Swapchain*>(handle); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 154 | void ReleaseSwapchainImage(VkDevice device, |
| 155 | ANativeWindow* window, |
| 156 | int release_fence, |
| 157 | Swapchain::Image& image) { |
| 158 | ALOG_ASSERT(release_fence == -1 || image.dequeued, |
| 159 | "ReleaseSwapchainImage: can't provide a release fence for " |
| 160 | "non-dequeued images"); |
| 161 | |
| 162 | if (image.dequeued) { |
| 163 | if (release_fence >= 0) { |
| 164 | // We get here from vkQueuePresentKHR. The application is |
| 165 | // responsible for creating an execution dependency chain from |
| 166 | // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR |
| 167 | // (release_fence), so we can drop the dequeue_fence here. |
| 168 | if (image.dequeue_fence >= 0) |
| 169 | close(image.dequeue_fence); |
| 170 | } else { |
| 171 | // We get here during swapchain destruction, or various serious |
| 172 | // error cases e.g. when we can't create the release_fence during |
| 173 | // vkQueuePresentKHR. In non-error cases, the dequeue_fence should |
| 174 | // have already signalled, since the swapchain images are supposed |
| 175 | // to be idle before the swapchain is destroyed. In error cases, |
| 176 | // there may be rendering in flight to the image, but since we |
| 177 | // weren't able to create a release_fence, waiting for the |
| 178 | // dequeue_fence is about the best we can do. |
| 179 | release_fence = image.dequeue_fence; |
| 180 | } |
| 181 | image.dequeue_fence = -1; |
| 182 | |
| 183 | if (window) { |
| 184 | window->cancelBuffer(window, image.buffer.get(), release_fence); |
| 185 | } else { |
| 186 | if (release_fence >= 0) { |
| 187 | sync_wait(release_fence, -1 /* forever */); |
| 188 | close(release_fence); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | image.dequeued = false; |
| 193 | } |
| 194 | |
| 195 | if (image.image) { |
| 196 | GetData(device).driver.DestroyImage(device, image.image, nullptr); |
| 197 | image.image = VK_NULL_HANDLE; |
| 198 | } |
| 199 | |
| 200 | image.buffer.clear(); |
| 201 | } |
| 202 | |
| 203 | void OrphanSwapchain(VkDevice device, Swapchain* swapchain) { |
| 204 | if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain)) |
| 205 | return; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 206 | for (uint32_t i = 0; i < swapchain->num_images; i++) { |
| 207 | if (!swapchain->images[i].dequeued) |
| 208 | ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]); |
| 209 | } |
| 210 | swapchain->surface.swapchain_handle = VK_NULL_HANDLE; |
| 211 | } |
| 212 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 213 | } // anonymous namespace |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 214 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 215 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 216 | VkResult CreateAndroidSurfaceKHR( |
Jesse Hall | f9fa9a5 | 2016-01-08 16:08:51 -0800 | [diff] [blame] | 217 | VkInstance instance, |
| 218 | const VkAndroidSurfaceCreateInfoKHR* pCreateInfo, |
| 219 | const VkAllocationCallbacks* allocator, |
| 220 | VkSurfaceKHR* out_surface) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 221 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 222 | allocator = &GetData(instance).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 223 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface), |
| 224 | alignof(Surface), |
| 225 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 226 | if (!mem) |
| 227 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 228 | Surface* surface = new (mem) Surface; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 229 | |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 230 | surface->window = pCreateInfo->window; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 231 | surface->swapchain_handle = VK_NULL_HANDLE; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 232 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 233 | // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN. |
| 234 | int err = |
| 235 | native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
| 236 | if (err != 0) { |
| 237 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 238 | // errors and translate them to valid Vulkan result codes? |
| 239 | ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err), |
| 240 | err); |
| 241 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 242 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 243 | return VK_ERROR_INITIALIZATION_FAILED; |
| 244 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 245 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 246 | *out_surface = HandleFromSurface(surface); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 247 | return VK_SUCCESS; |
| 248 | } |
| 249 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 250 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 251 | void DestroySurfaceKHR(VkInstance instance, |
| 252 | VkSurfaceKHR surface_handle, |
| 253 | const VkAllocationCallbacks* allocator) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 254 | Surface* surface = SurfaceFromHandle(surface_handle); |
| 255 | if (!surface) |
| 256 | return; |
| 257 | native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 258 | ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 259 | "destroyed VkSurfaceKHR 0x%" PRIx64 |
| 260 | " has active VkSwapchainKHR 0x%" PRIx64, |
| 261 | reinterpret_cast<uint64_t>(surface_handle), |
| 262 | reinterpret_cast<uint64_t>(surface->swapchain_handle)); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 263 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 264 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 265 | allocator = &GetData(instance).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 266 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 269 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 270 | VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/, |
| 271 | uint32_t /*queue_family*/, |
| 272 | VkSurfaceKHR /*surface*/, |
| 273 | VkBool32* supported) { |
Jesse Hall | 0e74f00 | 2015-11-30 11:37:59 -0800 | [diff] [blame] | 274 | *supported = VK_TRUE; |
Jesse Hall | a642925 | 2015-11-29 18:59:42 -0800 | [diff] [blame] | 275 | return VK_SUCCESS; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 278 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 279 | VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR( |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 280 | VkPhysicalDevice /*pdev*/, |
| 281 | VkSurfaceKHR surface, |
| 282 | VkSurfaceCapabilitiesKHR* capabilities) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 283 | int err; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 284 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 285 | |
| 286 | int width, height; |
| 287 | err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width); |
| 288 | if (err != 0) { |
| 289 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 290 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 291 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 292 | } |
| 293 | err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height); |
| 294 | if (err != 0) { |
| 295 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 296 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 297 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 300 | int transform_hint; |
| 301 | err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint); |
| 302 | if (err != 0) { |
| 303 | ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)", |
| 304 | strerror(-err), err); |
| 305 | return VK_ERROR_INITIALIZATION_FAILED; |
| 306 | } |
| 307 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 308 | // TODO(jessehall): Figure out what the min/max values should be. |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 309 | capabilities->minImageCount = 2; |
| 310 | capabilities->maxImageCount = 3; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 311 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 312 | capabilities->currentExtent = |
| 313 | VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)}; |
| 314 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 315 | // TODO(jessehall): Figure out what the max extent should be. Maximum |
| 316 | // texture dimension maybe? |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 317 | capabilities->minImageExtent = VkExtent2D{1, 1}; |
| 318 | capabilities->maxImageExtent = VkExtent2D{4096, 4096}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 319 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 320 | capabilities->maxImageArrayLayers = 1; |
| 321 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 322 | capabilities->supportedTransforms = kSupportedTransforms; |
| 323 | capabilities->currentTransform = |
| 324 | TranslateNativeToVulkanTransform(transform_hint); |
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 | // On Android, window composition is a WindowManager property, not something |
| 327 | // associated with the bufferqueue. It can't be changed from here. |
| 328 | capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 329 | |
| 330 | // TODO(jessehall): I think these are right, but haven't thought hard about |
| 331 | // it. Do we need to query the driver for support of any of these? |
| 332 | // Currently not included: |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 333 | // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not |
| 334 | // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 335 | capabilities->supportedUsageFlags = |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 336 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 337 | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | |
| 338 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 339 | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 340 | |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 341 | return VK_SUCCESS; |
| 342 | } |
| 343 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 344 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 345 | VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice /*pdev*/, |
| 346 | VkSurfaceKHR /*surface*/, |
| 347 | uint32_t* count, |
| 348 | VkSurfaceFormatKHR* formats) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 349 | // TODO(jessehall): Fill out the set of supported formats. Longer term, add |
| 350 | // a new gralloc method to query whether a (format, usage) pair is |
| 351 | // supported, and check that for each gralloc format that corresponds to a |
| 352 | // Vulkan format. Shorter term, just add a few more formats to the ones |
| 353 | // hardcoded below. |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 354 | |
| 355 | const VkSurfaceFormatKHR kFormats[] = { |
Jesse Hall | 2676338 | 2016-05-20 07:13:52 -0700 | [diff] [blame] | 356 | {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
| 357 | {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
| 358 | {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 359 | }; |
| 360 | const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]); |
| 361 | |
| 362 | VkResult result = VK_SUCCESS; |
| 363 | if (formats) { |
| 364 | if (*count < kNumFormats) |
| 365 | result = VK_INCOMPLETE; |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 366 | *count = std::min(*count, kNumFormats); |
| 367 | std::copy(kFormats, kFormats + *count, formats); |
| 368 | } else { |
| 369 | *count = kNumFormats; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 370 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 371 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 374 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 375 | VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice /*pdev*/, |
| 376 | VkSurfaceKHR /*surface*/, |
| 377 | uint32_t* count, |
| 378 | VkPresentModeKHR* modes) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 379 | const VkPresentModeKHR kModes[] = { |
| 380 | VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR, |
Chris Forbes | 980ad05 | 2017-01-18 16:55:07 +1300 | [diff] [blame^] | 381 | // TODO(chrisforbes): should only expose this if the driver can. |
| 382 | VK_PRESENT_MODE_FRONT_BUFFERED_DEMAND_REFRESH_KHR, |
| 383 | VK_PRESENT_MODE_FRONT_BUFFERED_CONTINUOUS_REFRESH_KHR, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 384 | }; |
| 385 | const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]); |
| 386 | |
| 387 | VkResult result = VK_SUCCESS; |
| 388 | if (modes) { |
| 389 | if (*count < kNumModes) |
| 390 | result = VK_INCOMPLETE; |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 391 | *count = std::min(*count, kNumModes); |
| 392 | std::copy(kModes, kModes + *count, modes); |
| 393 | } else { |
| 394 | *count = kNumModes; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 395 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 396 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 399 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 400 | VkResult CreateSwapchainKHR(VkDevice device, |
| 401 | const VkSwapchainCreateInfoKHR* create_info, |
| 402 | const VkAllocationCallbacks* allocator, |
| 403 | VkSwapchainKHR* swapchain_handle) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 404 | int err; |
| 405 | VkResult result = VK_SUCCESS; |
| 406 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 407 | ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64 |
| 408 | " minImageCount=%u imageFormat=%u imageColorSpace=%u" |
| 409 | " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u" |
| 410 | " oldSwapchain=0x%" PRIx64, |
| 411 | reinterpret_cast<uint64_t>(create_info->surface), |
| 412 | create_info->minImageCount, create_info->imageFormat, |
| 413 | create_info->imageColorSpace, create_info->imageExtent.width, |
| 414 | create_info->imageExtent.height, create_info->imageUsage, |
| 415 | create_info->preTransform, create_info->presentMode, |
| 416 | reinterpret_cast<uint64_t>(create_info->oldSwapchain)); |
| 417 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 418 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 419 | allocator = &GetData(device).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 420 | |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 421 | ALOGV_IF(create_info->imageArrayLayers != 1, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 422 | "swapchain imageArrayLayers=%u not supported", |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 423 | create_info->imageArrayLayers); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 424 | ALOGV_IF(create_info->imageColorSpace != VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 425 | "swapchain imageColorSpace=%u not supported", |
| 426 | create_info->imageColorSpace); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 427 | ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 428 | "swapchain preTransform=%#x not supported", |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 429 | create_info->preTransform); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 430 | ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR || |
Chris Forbes | 980ad05 | 2017-01-18 16:55:07 +1300 | [diff] [blame^] | 431 | create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR || |
| 432 | create_info->presentMode == VK_PRESENT_MODE_FRONT_BUFFERED_DEMAND_REFRESH_KHR || |
| 433 | create_info->presentMode == VK_PRESENT_MODE_FRONT_BUFFERED_CONTINUOUS_REFRESH_KHR), |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 434 | "swapchain presentMode=%u not supported", |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 435 | create_info->presentMode); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 436 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 437 | Surface& surface = *SurfaceFromHandle(create_info->surface); |
| 438 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 439 | if (surface.swapchain_handle != create_info->oldSwapchain) { |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 440 | ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64 |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 441 | " because it already has active swapchain 0x%" PRIx64 |
| 442 | " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64, |
| 443 | reinterpret_cast<uint64_t>(create_info->surface), |
| 444 | reinterpret_cast<uint64_t>(surface.swapchain_handle), |
| 445 | reinterpret_cast<uint64_t>(create_info->oldSwapchain)); |
| 446 | return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR; |
| 447 | } |
| 448 | if (create_info->oldSwapchain != VK_NULL_HANDLE) |
| 449 | OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain)); |
| 450 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 451 | // -- Reset the native window -- |
| 452 | // The native window might have been used previously, and had its properties |
| 453 | // changed from defaults. That will affect the answer we get for queries |
| 454 | // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we |
| 455 | // attempt such queries. |
| 456 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 457 | // The native window only allows dequeueing all buffers before any have |
| 458 | // been queued, since after that point at least one is assumed to be in |
| 459 | // non-FREE state at any given time. Disconnecting and re-connecting |
| 460 | // orphans the previous buffers, getting us back to the state where we can |
| 461 | // dequeue all buffers. |
| 462 | err = native_window_api_disconnect(surface.window.get(), |
| 463 | NATIVE_WINDOW_API_EGL); |
| 464 | ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)", |
| 465 | strerror(-err), err); |
| 466 | err = |
| 467 | native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL); |
| 468 | ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)", |
| 469 | strerror(-err), err); |
| 470 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 471 | err = native_window_set_buffer_count(surface.window.get(), 0); |
| 472 | if (err != 0) { |
| 473 | ALOGE("native_window_set_buffer_count(0) failed: %s (%d)", |
| 474 | strerror(-err), err); |
| 475 | return VK_ERROR_INITIALIZATION_FAILED; |
| 476 | } |
| 477 | |
| 478 | err = surface.window->setSwapInterval(surface.window.get(), 1); |
| 479 | if (err != 0) { |
| 480 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 481 | // errors and translate them to valid Vulkan result codes? |
| 482 | ALOGE("native_window->setSwapInterval(1) failed: %s (%d)", |
| 483 | strerror(-err), err); |
| 484 | return VK_ERROR_INITIALIZATION_FAILED; |
| 485 | } |
| 486 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 487 | // -- Configure the native window -- |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 488 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 489 | const auto& dispatch = GetData(device).driver; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 490 | |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 491 | int native_format = HAL_PIXEL_FORMAT_RGBA_8888; |
| 492 | switch (create_info->imageFormat) { |
| 493 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 494 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 495 | native_format = HAL_PIXEL_FORMAT_RGBA_8888; |
| 496 | break; |
| 497 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 498 | native_format = HAL_PIXEL_FORMAT_RGB_565; |
| 499 | break; |
| 500 | default: |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 501 | ALOGV("unsupported swapchain format %d", create_info->imageFormat); |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 502 | break; |
| 503 | } |
| 504 | err = native_window_set_buffers_format(surface.window.get(), native_format); |
| 505 | if (err != 0) { |
| 506 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 507 | // errors and translate them to valid Vulkan result codes? |
| 508 | ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)", |
| 509 | native_format, strerror(-err), err); |
| 510 | return VK_ERROR_INITIALIZATION_FAILED; |
| 511 | } |
| 512 | err = native_window_set_buffers_data_space(surface.window.get(), |
| 513 | HAL_DATASPACE_SRGB_LINEAR); |
| 514 | if (err != 0) { |
| 515 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 516 | // errors and translate them to valid Vulkan result codes? |
| 517 | ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)", |
| 518 | HAL_DATASPACE_SRGB_LINEAR, strerror(-err), err); |
| 519 | return VK_ERROR_INITIALIZATION_FAILED; |
| 520 | } |
| 521 | |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 522 | err = native_window_set_buffers_dimensions( |
| 523 | surface.window.get(), static_cast<int>(create_info->imageExtent.width), |
| 524 | static_cast<int>(create_info->imageExtent.height)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 525 | if (err != 0) { |
| 526 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 527 | // errors and translate them to valid Vulkan result codes? |
| 528 | ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)", |
| 529 | create_info->imageExtent.width, create_info->imageExtent.height, |
| 530 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 531 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 534 | // VkSwapchainCreateInfo::preTransform indicates the transformation the app |
| 535 | // applied during rendering. native_window_set_transform() expects the |
| 536 | // inverse: the transform the app is requesting that the compositor perform |
| 537 | // during composition. With native windows, pre-transform works by rendering |
| 538 | // with the same transform the compositor is applying (as in Vulkan), but |
| 539 | // then requesting the inverse transform, so that when the compositor does |
| 540 | // it's job the two transforms cancel each other out and the compositor ends |
| 541 | // up applying an identity transform to the app's buffer. |
| 542 | err = native_window_set_buffers_transform( |
| 543 | surface.window.get(), |
| 544 | InvertTransformToNative(create_info->preTransform)); |
| 545 | if (err != 0) { |
| 546 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 547 | // errors and translate them to valid Vulkan result codes? |
| 548 | ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)", |
| 549 | InvertTransformToNative(create_info->preTransform), |
| 550 | strerror(-err), err); |
| 551 | return VK_ERROR_INITIALIZATION_FAILED; |
| 552 | } |
| 553 | |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 554 | err = native_window_set_scaling_mode( |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 555 | surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 556 | if (err != 0) { |
| 557 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 558 | // errors and translate them to valid Vulkan result codes? |
| 559 | ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)", |
| 560 | strerror(-err), err); |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 561 | return VK_ERROR_INITIALIZATION_FAILED; |
| 562 | } |
| 563 | |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 564 | int query_value; |
| 565 | err = surface.window->query(surface.window.get(), |
| 566 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 567 | &query_value); |
| 568 | if (err != 0 || query_value < 0) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 569 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 570 | // errors and translate them to valid Vulkan result codes? |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 571 | ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, |
| 572 | query_value); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 573 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 574 | } |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 575 | uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value); |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 576 | // The MIN_UNDEQUEUED_BUFFERS query doesn't know whether we'll be using |
| 577 | // async mode or not, and assumes not. But in async mode, the BufferQueue |
| 578 | // requires an extra undequeued buffer. |
| 579 | // See BufferQueueCore::getMinUndequeuedBufferCountLocked(). |
| 580 | if (create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR) |
| 581 | min_undequeued_buffers += 1; |
| 582 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 583 | uint32_t num_images = |
| 584 | (create_info->minImageCount - 1) + min_undequeued_buffers; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 585 | err = native_window_set_buffer_count(surface.window.get(), num_images); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 586 | if (err != 0) { |
| 587 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 588 | // errors and translate them to valid Vulkan result codes? |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 589 | ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images, |
| 590 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 591 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 594 | VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 595 | int gralloc_usage = 0; |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 596 | if (dispatch.GetSwapchainGrallocUsage2ANDROID) { |
| 597 | result = dispatch.GetSwapchainGrallocUsage2ANDROID( |
| 598 | device, create_info->imageFormat, create_info->imageUsage, |
| 599 | swapchain_image_usage, &gralloc_usage); |
| 600 | if (result != VK_SUCCESS) { |
| 601 | ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result); |
| 602 | return VK_ERROR_INITIALIZATION_FAILED; |
| 603 | } |
| 604 | } else if (dispatch.GetSwapchainGrallocUsageANDROID) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 605 | result = dispatch.GetSwapchainGrallocUsageANDROID( |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 606 | device, create_info->imageFormat, create_info->imageUsage, |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 607 | &gralloc_usage); |
| 608 | if (result != VK_SUCCESS) { |
| 609 | ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 610 | return VK_ERROR_INITIALIZATION_FAILED; |
| 611 | } |
| 612 | } else { |
| 613 | gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE; |
| 614 | } |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 615 | err = native_window_set_usage(surface.window.get(), gralloc_usage); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 616 | if (err != 0) { |
| 617 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 618 | // errors and translate them to valid Vulkan result codes? |
| 619 | ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 620 | return VK_ERROR_INITIALIZATION_FAILED; |
| 621 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 622 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 623 | int swap_interval = |
| 624 | create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1; |
| 625 | err = surface.window->setSwapInterval(surface.window.get(), swap_interval); |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 626 | if (err != 0) { |
| 627 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 628 | // errors and translate them to valid Vulkan result codes? |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 629 | ALOGE("native_window->setSwapInterval(%d) failed: %s (%d)", |
| 630 | swap_interval, strerror(-err), err); |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 631 | return VK_ERROR_INITIALIZATION_FAILED; |
| 632 | } |
| 633 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 634 | // -- Allocate our Swapchain object -- |
| 635 | // After this point, we must deallocate the swapchain on error. |
| 636 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 637 | void* mem = allocator->pfnAllocation(allocator->pUserData, |
| 638 | sizeof(Swapchain), alignof(Swapchain), |
| 639 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 640 | if (!mem) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 641 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 642 | Swapchain* swapchain = new (mem) Swapchain(surface, num_images); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 643 | |
| 644 | // -- Dequeue all buffers and create a VkImage for each -- |
| 645 | // Any failures during or after this must cancel the dequeued buffers. |
| 646 | |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 647 | VkSwapchainImageCreateInfoANDROID swapchain_image_create = { |
| 648 | #pragma clang diagnostic push |
| 649 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 650 | .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID, |
| 651 | #pragma clang diagnostic pop |
| 652 | .pNext = nullptr, |
| 653 | .usage = swapchain_image_usage, |
| 654 | }; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 655 | VkNativeBufferANDROID image_native_buffer = { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 656 | #pragma clang diagnostic push |
| 657 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 658 | .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID, |
| 659 | #pragma clang diagnostic pop |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 660 | .pNext = &swapchain_image_create, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 661 | }; |
| 662 | VkImageCreateInfo image_create = { |
| 663 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 664 | .pNext = &image_native_buffer, |
| 665 | .imageType = VK_IMAGE_TYPE_2D, |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 666 | .format = create_info->imageFormat, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 667 | .extent = {0, 0, 1}, |
| 668 | .mipLevels = 1, |
Jesse Hall | a15a4bf | 2015-11-19 22:48:02 -0800 | [diff] [blame] | 669 | .arrayLayers = 1, |
Jesse Hall | 091ed9e | 2015-11-30 00:55:29 -0800 | [diff] [blame] | 670 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 671 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 672 | .usage = create_info->imageUsage, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 673 | .flags = 0, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 674 | .sharingMode = create_info->imageSharingMode, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 675 | .queueFamilyIndexCount = create_info->queueFamilyIndexCount, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 676 | .pQueueFamilyIndices = create_info->pQueueFamilyIndices, |
| 677 | }; |
| 678 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 679 | for (uint32_t i = 0; i < num_images; i++) { |
| 680 | Swapchain::Image& img = swapchain->images[i]; |
| 681 | |
| 682 | ANativeWindowBuffer* buffer; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 683 | err = surface.window->dequeueBuffer(surface.window.get(), &buffer, |
| 684 | &img.dequeue_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 685 | if (err != 0) { |
| 686 | // TODO(jessehall): Improve error reporting. Can we enumerate |
| 687 | // possible errors and translate them to valid Vulkan result codes? |
| 688 | ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 689 | result = VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 690 | break; |
| 691 | } |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 692 | img.buffer = buffer; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 693 | img.dequeued = true; |
| 694 | |
| 695 | image_create.extent = |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 696 | VkExtent3D{static_cast<uint32_t>(img.buffer->width), |
| 697 | static_cast<uint32_t>(img.buffer->height), |
| 698 | 1}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 699 | image_native_buffer.handle = img.buffer->handle; |
| 700 | image_native_buffer.stride = img.buffer->stride; |
| 701 | image_native_buffer.format = img.buffer->format; |
| 702 | image_native_buffer.usage = img.buffer->usage; |
| 703 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 704 | result = |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 705 | dispatch.CreateImage(device, &image_create, nullptr, &img.image); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 706 | if (result != VK_SUCCESS) { |
| 707 | ALOGD("vkCreateImage w/ native buffer failed: %u", result); |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // -- Cancel all buffers, returning them to the queue -- |
| 713 | // If an error occurred before, also destroy the VkImage and release the |
| 714 | // buffer reference. Otherwise, we retain a strong reference to the buffer. |
| 715 | // |
| 716 | // TODO(jessehall): The error path here is the same as DestroySwapchain, |
| 717 | // but not the non-error path. Should refactor/unify. |
| 718 | for (uint32_t i = 0; i < num_images; i++) { |
| 719 | Swapchain::Image& img = swapchain->images[i]; |
| 720 | if (img.dequeued) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 721 | surface.window->cancelBuffer(surface.window.get(), img.buffer.get(), |
| 722 | img.dequeue_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 723 | img.dequeue_fence = -1; |
| 724 | img.dequeued = false; |
| 725 | } |
| 726 | if (result != VK_SUCCESS) { |
| 727 | if (img.image) |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 728 | dispatch.DestroyImage(device, img.image, nullptr); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
| 732 | if (result != VK_SUCCESS) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 733 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 734 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 735 | return result; |
| 736 | } |
| 737 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 738 | surface.swapchain_handle = HandleFromSwapchain(swapchain); |
| 739 | *swapchain_handle = surface.swapchain_handle; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 740 | return VK_SUCCESS; |
| 741 | } |
| 742 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 743 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 744 | void DestroySwapchainKHR(VkDevice device, |
| 745 | VkSwapchainKHR swapchain_handle, |
| 746 | const VkAllocationCallbacks* allocator) { |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 747 | const auto& dispatch = GetData(device).driver; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 748 | Swapchain* swapchain = SwapchainFromHandle(swapchain_handle); |
Daniel Koch | d78c2e8 | 2016-12-13 18:45:13 -0500 | [diff] [blame] | 749 | if (!swapchain) |
| 750 | return; |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 751 | bool active = swapchain->surface.swapchain_handle == swapchain_handle; |
| 752 | ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 753 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 754 | if (swapchain->frame_timestamps_enabled) { |
| 755 | native_window_enable_frame_timestamps(window, false); |
| 756 | } |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 757 | for (uint32_t i = 0; i < swapchain->num_images; i++) |
| 758 | ReleaseSwapchainImage(device, window, -1, swapchain->images[i]); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 759 | if (active) |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 760 | swapchain->surface.swapchain_handle = VK_NULL_HANDLE; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 761 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 762 | allocator = &GetData(device).allocator; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 763 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 764 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 767 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 768 | VkResult GetSwapchainImagesKHR(VkDevice, |
| 769 | VkSwapchainKHR swapchain_handle, |
| 770 | uint32_t* count, |
| 771 | VkImage* images) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 772 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 773 | ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle, |
| 774 | "getting images for non-active swapchain 0x%" PRIx64 |
| 775 | "; only dequeued image handles are valid", |
| 776 | reinterpret_cast<uint64_t>(swapchain_handle)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 777 | VkResult result = VK_SUCCESS; |
| 778 | if (images) { |
| 779 | uint32_t n = swapchain.num_images; |
| 780 | if (*count < swapchain.num_images) { |
| 781 | n = *count; |
| 782 | result = VK_INCOMPLETE; |
| 783 | } |
| 784 | for (uint32_t i = 0; i < n; i++) |
| 785 | images[i] = swapchain.images[i].image; |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 786 | *count = n; |
| 787 | } else { |
| 788 | *count = swapchain.num_images; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 789 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 790 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 793 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 794 | VkResult AcquireNextImageKHR(VkDevice device, |
| 795 | VkSwapchainKHR swapchain_handle, |
| 796 | uint64_t timeout, |
| 797 | VkSemaphore semaphore, |
| 798 | VkFence vk_fence, |
| 799 | uint32_t* image_index) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 800 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 801 | ANativeWindow* window = swapchain.surface.window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 802 | VkResult result; |
| 803 | int err; |
| 804 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 805 | if (swapchain.surface.swapchain_handle != swapchain_handle) |
| 806 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 807 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 808 | ALOGW_IF( |
| 809 | timeout != UINT64_MAX, |
| 810 | "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented"); |
| 811 | |
| 812 | ANativeWindowBuffer* buffer; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 813 | int fence_fd; |
| 814 | err = window->dequeueBuffer(window, &buffer, &fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 815 | if (err != 0) { |
| 816 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 817 | // errors and translate them to valid Vulkan result codes? |
| 818 | ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 819 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | uint32_t idx; |
| 823 | for (idx = 0; idx < swapchain.num_images; idx++) { |
| 824 | if (swapchain.images[idx].buffer.get() == buffer) { |
| 825 | swapchain.images[idx].dequeued = true; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 826 | swapchain.images[idx].dequeue_fence = fence_fd; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 827 | break; |
| 828 | } |
| 829 | } |
| 830 | if (idx == swapchain.num_images) { |
| 831 | ALOGE("dequeueBuffer returned unrecognized buffer"); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 832 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 833 | return VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | int fence_clone = -1; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 837 | if (fence_fd != -1) { |
| 838 | fence_clone = dup(fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 839 | if (fence_clone == -1) { |
| 840 | ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", |
| 841 | strerror(errno), errno); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 842 | sync_wait(fence_fd, -1 /* forever */); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 846 | result = GetData(device).driver.AcquireImageANDROID( |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 847 | device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 848 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 849 | // NOTE: we're relying on AcquireImageANDROID to close fence_clone, |
| 850 | // even if the call fails. We could close it ourselves on failure, but |
| 851 | // that would create a race condition if the driver closes it on a |
| 852 | // failure path: some other thread might create an fd with the same |
| 853 | // number between the time the driver closes it and the time we close |
| 854 | // it. We must assume one of: the driver *always* closes it even on |
| 855 | // failure, or *never* closes it on failure. |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 856 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 857 | swapchain.images[idx].dequeued = false; |
| 858 | swapchain.images[idx].dequeue_fence = -1; |
| 859 | return result; |
| 860 | } |
| 861 | |
| 862 | *image_index = idx; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 863 | return VK_SUCCESS; |
| 864 | } |
| 865 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 866 | static VkResult WorstPresentResult(VkResult a, VkResult b) { |
| 867 | // See the error ranking for vkQueuePresentKHR at the end of section 29.6 |
| 868 | // (in spec version 1.0.14). |
| 869 | static const VkResult kWorstToBest[] = { |
| 870 | VK_ERROR_DEVICE_LOST, |
| 871 | VK_ERROR_SURFACE_LOST_KHR, |
| 872 | VK_ERROR_OUT_OF_DATE_KHR, |
| 873 | VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 874 | VK_ERROR_OUT_OF_HOST_MEMORY, |
| 875 | VK_SUBOPTIMAL_KHR, |
| 876 | }; |
| 877 | for (auto result : kWorstToBest) { |
| 878 | if (a == result || b == result) |
| 879 | return result; |
| 880 | } |
| 881 | ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a); |
| 882 | ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b); |
| 883 | return a != VK_SUCCESS ? a : b; |
| 884 | } |
| 885 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 886 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 887 | VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 888 | ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
| 889 | "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d", |
| 890 | present_info->sType); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 891 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 892 | VkDevice device = GetData(queue).driver_device; |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 893 | const auto& dispatch = GetData(queue).driver; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 894 | VkResult final_result = VK_SUCCESS; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 895 | |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 896 | // Look at the pNext chain for supported extension structs: |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 897 | const VkPresentRegionsKHR* present_regions = nullptr; |
| 898 | const VkPresentTimesInfoGOOGLE* present_times = nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 899 | const VkPresentRegionsKHR* next = |
| 900 | reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext); |
| 901 | while (next) { |
| 902 | switch (next->sType) { |
| 903 | case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR: |
| 904 | present_regions = next; |
| 905 | break; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 906 | case VK_STRUCTURE_TYPE_PRESENT_TIMES_GOOGLE: |
| 907 | present_times = |
| 908 | reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next); |
| 909 | break; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 910 | default: |
| 911 | ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x", |
| 912 | next->sType); |
| 913 | break; |
| 914 | } |
| 915 | next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext); |
| 916 | } |
| 917 | ALOGV_IF( |
| 918 | present_regions && |
| 919 | present_regions->swapchainCount != present_info->swapchainCount, |
| 920 | "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 921 | ALOGV_IF(present_times && |
| 922 | present_times->swapchainCount != present_info->swapchainCount, |
| 923 | "VkPresentTimesInfoGOOGLE::swapchainCount != " |
| 924 | "VkPresentInfo::swapchainCount"); |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 925 | const VkPresentRegionKHR* regions = |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 926 | (present_regions) ? present_regions->pRegions : nullptr; |
| 927 | const VkPresentTimeGOOGLE* times = |
| 928 | (present_times) ? present_times->pTimes : nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 929 | const VkAllocationCallbacks* allocator = &GetData(device).allocator; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 930 | android_native_rect_t* rects = nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 931 | uint32_t nrects = 0; |
| 932 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 933 | for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) { |
| 934 | Swapchain& swapchain = |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 935 | *SwapchainFromHandle(present_info->pSwapchains[sc]); |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 936 | uint32_t image_idx = present_info->pImageIndices[sc]; |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 937 | Swapchain::Image& img = swapchain.images[image_idx]; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 938 | const VkPresentRegionKHR* region = (regions) ? ®ions[sc] : nullptr; |
| 939 | const VkPresentTimeGOOGLE* time = (times) ? ×[sc] : nullptr; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 940 | VkResult swapchain_result = VK_SUCCESS; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 941 | VkResult result; |
| 942 | int err; |
| 943 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 944 | int fence = -1; |
Jesse Hall | 275d76c | 2016-01-08 22:39:16 -0800 | [diff] [blame] | 945 | result = dispatch.QueueSignalReleaseImageANDROID( |
| 946 | queue, present_info->waitSemaphoreCount, |
| 947 | present_info->pWaitSemaphores, img.image, &fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 948 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 949 | ALOGE("QueueSignalReleaseImageANDROID failed: %d", result); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 950 | swapchain_result = result; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 953 | if (swapchain.surface.swapchain_handle == |
| 954 | present_info->pSwapchains[sc]) { |
| 955 | ANativeWindow* window = swapchain.surface.window.get(); |
| 956 | if (swapchain_result == VK_SUCCESS) { |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 957 | if (region) { |
| 958 | // Process the incremental-present hint for this swapchain: |
| 959 | uint32_t rcount = region->rectangleCount; |
| 960 | if (rcount > nrects) { |
| 961 | android_native_rect_t* new_rects = |
| 962 | static_cast<android_native_rect_t*>( |
| 963 | allocator->pfnReallocation( |
| 964 | allocator->pUserData, rects, |
| 965 | sizeof(android_native_rect_t) * rcount, |
| 966 | alignof(android_native_rect_t), |
| 967 | VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); |
| 968 | if (new_rects) { |
| 969 | rects = new_rects; |
| 970 | nrects = rcount; |
| 971 | } else { |
| 972 | rcount = 0; // Ignore the hint for this swapchain |
| 973 | } |
| 974 | } |
| 975 | for (uint32_t r = 0; r < rcount; ++r) { |
| 976 | if (region->pRectangles[r].layer > 0) { |
| 977 | ALOGV( |
| 978 | "vkQueuePresentKHR ignoring invalid layer " |
| 979 | "(%u); using layer 0 instead", |
| 980 | region->pRectangles[r].layer); |
| 981 | } |
| 982 | int x = region->pRectangles[r].offset.x; |
| 983 | int y = region->pRectangles[r].offset.y; |
| 984 | int width = static_cast<int>( |
| 985 | region->pRectangles[r].extent.width); |
| 986 | int height = static_cast<int>( |
| 987 | region->pRectangles[r].extent.height); |
| 988 | android_native_rect_t* cur_rect = &rects[r]; |
| 989 | cur_rect->left = x; |
| 990 | cur_rect->top = y + height; |
| 991 | cur_rect->right = x + width; |
| 992 | cur_rect->bottom = y; |
| 993 | } |
| 994 | native_window_set_surface_damage(window, rects, rcount); |
| 995 | } |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 996 | if (time) { |
| 997 | if (!swapchain.frame_timestamps_enabled) { |
| 998 | native_window_enable_frame_timestamps(window, true); |
| 999 | swapchain.frame_timestamps_enabled = true; |
| 1000 | } |
| 1001 | // TODO(ianelliott): need to store the presentID (and |
| 1002 | // desiredPresentTime), so it can be later correlated to |
| 1003 | // this present. Probably modify the following function |
| 1004 | // (and below) to plumb a path to store it in FrameEvents |
| 1005 | // code, on the producer side. |
| 1006 | native_window_set_buffers_timestamp( |
| 1007 | window, static_cast<int64_t>(time->desiredPresentTime)); |
| 1008 | } |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1009 | err = window->queueBuffer(window, img.buffer.get(), fence); |
| 1010 | // queueBuffer always closes fence, even on error |
| 1011 | if (err != 0) { |
| 1012 | // TODO(jessehall): What now? We should probably cancel the |
| 1013 | // buffer, I guess? |
| 1014 | ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err); |
| 1015 | swapchain_result = WorstPresentResult( |
| 1016 | swapchain_result, VK_ERROR_OUT_OF_DATE_KHR); |
| 1017 | } |
| 1018 | if (img.dequeue_fence >= 0) { |
| 1019 | close(img.dequeue_fence); |
| 1020 | img.dequeue_fence = -1; |
| 1021 | } |
| 1022 | img.dequeued = false; |
| 1023 | } |
| 1024 | if (swapchain_result != VK_SUCCESS) { |
| 1025 | ReleaseSwapchainImage(device, window, fence, img); |
| 1026 | OrphanSwapchain(device, &swapchain); |
| 1027 | } |
| 1028 | } else { |
| 1029 | ReleaseSwapchainImage(device, nullptr, fence, img); |
| 1030 | swapchain_result = VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 1033 | if (present_info->pResults) |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1034 | present_info->pResults[sc] = swapchain_result; |
| 1035 | |
| 1036 | if (swapchain_result != final_result) |
| 1037 | final_result = WorstPresentResult(final_result, swapchain_result); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1038 | } |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1039 | if (rects) { |
| 1040 | allocator->pfnFree(allocator->pUserData, rects); |
| 1041 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1042 | |
| 1043 | return final_result; |
| 1044 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1045 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1046 | VKAPI_ATTR |
| 1047 | VkResult GetRefreshCycleDurationGOOGLE( |
| 1048 | VkDevice, |
| 1049 | VkSwapchainKHR, |
| 1050 | VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) { |
| 1051 | VkResult result = VK_SUCCESS; |
| 1052 | |
| 1053 | // TODO(ianelliott): FULLY IMPLEMENT THIS FUNCTION!!! |
| 1054 | pDisplayTimingProperties->minRefreshDuration = 16666666666; |
| 1055 | pDisplayTimingProperties->maxRefreshDuration = 16666666666; |
| 1056 | |
| 1057 | return result; |
| 1058 | } |
| 1059 | |
| 1060 | VKAPI_ATTR |
| 1061 | VkResult GetPastPresentationTimingGOOGLE( |
| 1062 | VkDevice, |
| 1063 | VkSwapchainKHR swapchain_handle, |
| 1064 | uint32_t* count, |
| 1065 | VkPastPresentationTimingGOOGLE* timings) { |
| 1066 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
| 1067 | ANativeWindow* window = swapchain.surface.window.get(); |
| 1068 | VkResult result = VK_SUCCESS; |
| 1069 | |
| 1070 | if (!swapchain.frame_timestamps_enabled) { |
| 1071 | native_window_enable_frame_timestamps(window, true); |
| 1072 | swapchain.frame_timestamps_enabled = true; |
| 1073 | } |
| 1074 | |
| 1075 | // TODO(ianelliott): FULLY IMPLEMENT THIS FUNCTION!!! |
| 1076 | if (timings) { |
| 1077 | *count = 0; |
| 1078 | } else { |
| 1079 | *count = 0; |
| 1080 | } |
| 1081 | |
| 1082 | return result; |
| 1083 | } |
| 1084 | |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 1085 | VKAPI_ATTR |
| 1086 | VkResult GetSwapchainStatusKHR( |
| 1087 | VkDevice, |
| 1088 | VkSwapchainKHR) { |
| 1089 | VkResult result = VK_SUCCESS; |
| 1090 | |
| 1091 | // TODO(chrisforbes): Implement this function properly |
| 1092 | |
| 1093 | return result; |
| 1094 | } |
| 1095 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1096 | } // namespace driver |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1097 | } // namespace vulkan |