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> |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 23 | #include <utils/Vector.h> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 24 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 25 | #include "driver.h" |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 26 | |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 27 | // TODO(jessehall): Currently we don't have a good error code for when a native |
| 28 | // window operation fails. Just returning INITIALIZATION_FAILED for now. Later |
| 29 | // versions (post SDK 0.9) of the API/extension have a better error code. |
| 30 | // When updating to that version, audit all error returns. |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 31 | namespace vulkan { |
| 32 | namespace driver { |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 33 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 36 | const VkSurfaceTransformFlagsKHR kSupportedTransforms = |
| 37 | VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR | |
| 38 | VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR | |
| 39 | VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR | |
| 40 | VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR | |
| 41 | // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform. |
| 42 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR | |
| 43 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR | |
| 44 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR | |
| 45 | // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR | |
| 46 | VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR; |
| 47 | |
| 48 | VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) { |
| 49 | // Native and Vulkan transforms are isomorphic, but are represented |
| 50 | // differently. Vulkan transforms are built up of an optional horizontal |
| 51 | // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native |
| 52 | // transforms are built up from a horizontal flip, vertical flip, and |
| 53 | // 90-degree rotation, all optional but always in that order. |
| 54 | |
| 55 | // TODO(jessehall): For now, only support pure rotations, not |
| 56 | // flip or flip-and-rotate, until I have more time to test them and build |
| 57 | // sample code. As far as I know we never actually use anything besides |
| 58 | // pure rotations anyway. |
| 59 | |
| 60 | switch (native) { |
| 61 | case 0: // 0x0 |
| 62 | return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 63 | // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1 |
| 64 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR; |
| 65 | // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2 |
| 66 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR; |
| 67 | case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V |
| 68 | return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR; |
| 69 | case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4 |
| 70 | return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR; |
| 71 | // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90: |
| 72 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR; |
| 73 | // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90: |
| 74 | // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR; |
| 75 | case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90 |
| 76 | return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR; |
| 77 | case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY: |
| 78 | default: |
| 79 | return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 80 | } |
| 81 | } |
| 82 | |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 83 | int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) { |
| 84 | switch (transform) { |
| 85 | case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR: |
| 86 | return NATIVE_WINDOW_TRANSFORM_ROT_270; |
| 87 | case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR: |
| 88 | return NATIVE_WINDOW_TRANSFORM_ROT_180; |
| 89 | case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR: |
| 90 | return NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 91 | // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform. |
| 92 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR: |
| 93 | // return NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 94 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR: |
| 95 | // return NATIVE_WINDOW_TRANSFORM_FLIP_H | |
| 96 | // NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 97 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR: |
| 98 | // return NATIVE_WINDOW_TRANSFORM_FLIP_V; |
| 99 | // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR: |
| 100 | // return NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 101 | // NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 102 | case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR: |
| 103 | case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR: |
| 104 | default: |
| 105 | return 0; |
| 106 | } |
| 107 | } |
| 108 | |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 109 | class TimingInfo { |
| 110 | public: |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 111 | TimingInfo() = default; |
| 112 | TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId) |
Ian Elliott | 2c6355d | 2017-01-19 11:02:13 -0700 | [diff] [blame] | 113 | : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0}, |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 114 | native_frame_id_(nativeFrameId) {} |
| 115 | bool ready() const { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 116 | return (timestamp_desired_present_time_ && |
| 117 | timestamp_actual_present_time_ && |
| 118 | timestamp_render_complete_time_ && |
| 119 | timestamp_composition_latch_time_); |
| 120 | } |
| 121 | void calculate(uint64_t rdur) { |
| 122 | vals_.actualPresentTime = timestamp_actual_present_time_; |
| 123 | uint64_t margin = (timestamp_composition_latch_time_ - |
| 124 | timestamp_render_complete_time_); |
| 125 | // Calculate vals_.earliestPresentTime, and potentially adjust |
| 126 | // vals_.presentMargin. The initial value of vals_.earliestPresentTime |
| 127 | // is vals_.actualPresentTime. If we can subtract rdur (the duration |
| 128 | // of a refresh cycle) from vals_.earliestPresentTime (and also from |
| 129 | // vals_.presentMargin) and still leave a positive margin, then we can |
| 130 | // report to the application that it could have presented earlier than |
| 131 | // it did (per the extension specification). If for some reason, we |
| 132 | // can do this subtraction repeatedly, we do, since |
| 133 | // vals_.earliestPresentTime really is supposed to be the "earliest". |
| 134 | uint64_t early_time = vals_.actualPresentTime; |
| 135 | while ((margin > rdur) && |
| 136 | ((early_time - rdur) > timestamp_composition_latch_time_)) { |
| 137 | early_time -= rdur; |
| 138 | margin -= rdur; |
| 139 | } |
| 140 | vals_.earliestPresentTime = early_time; |
| 141 | vals_.presentMargin = margin; |
| 142 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 143 | void get_values(VkPastPresentationTimingGOOGLE* values) const { |
| 144 | *values = vals_; |
| 145 | } |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 146 | |
| 147 | public: |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 148 | VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 }; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 149 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 150 | uint64_t native_frame_id_ { 0 }; |
| 151 | uint64_t timestamp_desired_present_time_ { 0 }; |
| 152 | uint64_t timestamp_actual_present_time_ { 0 }; |
| 153 | uint64_t timestamp_render_complete_time_ { 0 }; |
| 154 | uint64_t timestamp_composition_latch_time_ { 0 }; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 157 | // ---------------------------------------------------------------------------- |
| 158 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 159 | struct Surface { |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 160 | android::sp<ANativeWindow> window; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 161 | VkSwapchainKHR swapchain_handle; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | VkSurfaceKHR HandleFromSurface(Surface* surface) { |
| 165 | return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface)); |
| 166 | } |
| 167 | |
| 168 | Surface* SurfaceFromHandle(VkSurfaceKHR handle) { |
Jesse Hall | a3a7a1d | 2015-11-24 11:37:23 -0800 | [diff] [blame] | 169 | return reinterpret_cast<Surface*>(handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 172 | // Maximum number of TimingInfo structs to keep per swapchain: |
| 173 | enum { MAX_TIMING_INFOS = 10 }; |
| 174 | // Minimum number of frames to look for in the past (so we don't cause |
| 175 | // syncronous requests to Surface Flinger): |
| 176 | enum { MIN_NUM_FRAMES_AGO = 5 }; |
| 177 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 178 | struct Swapchain { |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 179 | Swapchain(Surface& surface_, |
| 180 | uint32_t num_images_, |
| 181 | VkPresentModeKHR present_mode) |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 182 | : surface(surface_), |
| 183 | num_images(num_images_), |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 184 | mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR), |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 185 | frame_timestamps_enabled(false) { |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 186 | ANativeWindow* window = surface.window.get(); |
Ian Elliott | be833a2 | 2017-01-25 13:09:20 -0700 | [diff] [blame] | 187 | int64_t rdur; |
| 188 | native_window_get_refresh_cycle_duration( |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 189 | window, |
Ian Elliott | be833a2 | 2017-01-25 13:09:20 -0700 | [diff] [blame] | 190 | &rdur); |
| 191 | refresh_duration = static_cast<uint64_t>(rdur); |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 192 | } |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 193 | |
| 194 | Surface& surface; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 195 | uint32_t num_images; |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 196 | bool mailbox_mode; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 197 | bool frame_timestamps_enabled; |
Ian Elliott | be833a2 | 2017-01-25 13:09:20 -0700 | [diff] [blame] | 198 | uint64_t refresh_duration; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 199 | |
| 200 | struct Image { |
| 201 | Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {} |
| 202 | VkImage image; |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 203 | android::sp<ANativeWindowBuffer> buffer; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 204 | // The fence is only valid when the buffer is dequeued, and should be |
| 205 | // -1 any other time. When valid, we own the fd, and must ensure it is |
| 206 | // closed: either by closing it explicitly when queueing the buffer, |
| 207 | // or by passing ownership e.g. to ANativeWindow::cancelBuffer(). |
| 208 | int dequeue_fence; |
| 209 | bool dequeued; |
| 210 | } images[android::BufferQueue::NUM_BUFFER_SLOTS]; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 211 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 212 | android::Vector<TimingInfo> timing; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) { |
| 216 | return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain)); |
| 217 | } |
| 218 | |
| 219 | Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) { |
Jesse Hall | a3a7a1d | 2015-11-24 11:37:23 -0800 | [diff] [blame] | 220 | return reinterpret_cast<Swapchain*>(handle); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 223 | void ReleaseSwapchainImage(VkDevice device, |
| 224 | ANativeWindow* window, |
| 225 | int release_fence, |
| 226 | Swapchain::Image& image) { |
| 227 | ALOG_ASSERT(release_fence == -1 || image.dequeued, |
| 228 | "ReleaseSwapchainImage: can't provide a release fence for " |
| 229 | "non-dequeued images"); |
| 230 | |
| 231 | if (image.dequeued) { |
| 232 | if (release_fence >= 0) { |
| 233 | // We get here from vkQueuePresentKHR. The application is |
| 234 | // responsible for creating an execution dependency chain from |
| 235 | // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR |
| 236 | // (release_fence), so we can drop the dequeue_fence here. |
| 237 | if (image.dequeue_fence >= 0) |
| 238 | close(image.dequeue_fence); |
| 239 | } else { |
| 240 | // We get here during swapchain destruction, or various serious |
| 241 | // error cases e.g. when we can't create the release_fence during |
| 242 | // vkQueuePresentKHR. In non-error cases, the dequeue_fence should |
| 243 | // have already signalled, since the swapchain images are supposed |
| 244 | // to be idle before the swapchain is destroyed. In error cases, |
| 245 | // there may be rendering in flight to the image, but since we |
| 246 | // weren't able to create a release_fence, waiting for the |
| 247 | // dequeue_fence is about the best we can do. |
| 248 | release_fence = image.dequeue_fence; |
| 249 | } |
| 250 | image.dequeue_fence = -1; |
| 251 | |
| 252 | if (window) { |
| 253 | window->cancelBuffer(window, image.buffer.get(), release_fence); |
| 254 | } else { |
| 255 | if (release_fence >= 0) { |
| 256 | sync_wait(release_fence, -1 /* forever */); |
| 257 | close(release_fence); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | image.dequeued = false; |
| 262 | } |
| 263 | |
| 264 | if (image.image) { |
| 265 | GetData(device).driver.DestroyImage(device, image.image, nullptr); |
| 266 | image.image = VK_NULL_HANDLE; |
| 267 | } |
| 268 | |
| 269 | image.buffer.clear(); |
| 270 | } |
| 271 | |
| 272 | void OrphanSwapchain(VkDevice device, Swapchain* swapchain) { |
| 273 | if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain)) |
| 274 | return; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 275 | for (uint32_t i = 0; i < swapchain->num_images; i++) { |
| 276 | if (!swapchain->images[i].dequeued) |
| 277 | ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]); |
| 278 | } |
| 279 | swapchain->surface.swapchain_handle = VK_NULL_HANDLE; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 280 | swapchain->timing.clear(); |
| 281 | } |
| 282 | |
| 283 | uint32_t get_num_ready_timings(Swapchain& swapchain) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 284 | if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) { |
| 285 | return 0; |
| 286 | } |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 287 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 288 | uint32_t num_ready = 0; |
| 289 | const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1; |
| 290 | for (uint32_t i = 0; i < num_timings; i++) { |
| 291 | TimingInfo& ti = swapchain.timing.editItemAt(i); |
| 292 | if (ti.ready()) { |
| 293 | // This TimingInfo is ready to be reported to the user. Add it |
| 294 | // to the num_ready. |
| 295 | num_ready++; |
| 296 | continue; |
| 297 | } |
| 298 | // This TimingInfo is not yet ready to be reported to the user, |
| 299 | // and so we should look for any available timestamps that |
| 300 | // might make it ready. |
| 301 | int64_t desired_present_time = 0; |
| 302 | int64_t render_complete_time = 0; |
| 303 | int64_t composition_latch_time = 0; |
| 304 | int64_t actual_present_time = 0; |
| 305 | // Obtain timestamps: |
| 306 | int ret = native_window_get_frame_timestamps( |
| 307 | swapchain.surface.window.get(), ti.native_frame_id_, |
| 308 | &desired_present_time, &render_complete_time, |
| 309 | &composition_latch_time, |
| 310 | NULL, //&first_composition_start_time, |
| 311 | NULL, //&last_composition_start_time, |
| 312 | NULL, //&composition_finish_time, |
| 313 | // TODO(ianelliott): Maybe ask if this one is |
| 314 | // supported, at startup time (since it may not be |
| 315 | // supported): |
| 316 | &actual_present_time, |
| 317 | NULL, //&display_retire_time, |
| 318 | NULL, //&dequeue_ready_time, |
| 319 | NULL /*&reads_done_time*/); |
| 320 | |
| 321 | if (ret != android::NO_ERROR) { |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | // Record the timestamp(s) we received, and then see if this TimingInfo |
| 326 | // is ready to be reported to the user: |
| 327 | ti.timestamp_desired_present_time_ = |
| 328 | static_cast<uint64_t>(desired_present_time); |
| 329 | ti.timestamp_actual_present_time_ = |
| 330 | static_cast<uint64_t>(actual_present_time); |
| 331 | ti.timestamp_render_complete_time_ = |
| 332 | static_cast<uint64_t>(render_complete_time); |
| 333 | ti.timestamp_composition_latch_time_ = |
| 334 | static_cast<uint64_t>(composition_latch_time); |
| 335 | |
| 336 | if (ti.ready()) { |
| 337 | // The TimingInfo has received enough timestamps, and should now |
| 338 | // use those timestamps to calculate the info that should be |
| 339 | // reported to the user: |
| 340 | ti.calculate(swapchain.refresh_duration); |
| 341 | num_ready++; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | return num_ready; |
| 345 | } |
| 346 | |
| 347 | // TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!! |
| 348 | void copy_ready_timings(Swapchain& swapchain, |
| 349 | uint32_t* count, |
| 350 | VkPastPresentationTimingGOOGLE* timings) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 351 | if (swapchain.timing.empty()) { |
| 352 | *count = 0; |
| 353 | return; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 354 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 355 | |
| 356 | size_t last_ready = swapchain.timing.size() - 1; |
| 357 | while (!swapchain.timing[last_ready].ready()) { |
| 358 | if (last_ready == 0) { |
| 359 | *count = 0; |
| 360 | return; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 361 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 362 | last_ready--; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 363 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 364 | |
| 365 | uint32_t num_copied = 0; |
| 366 | size_t num_to_remove = 0; |
| 367 | for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) { |
| 368 | const TimingInfo& ti = swapchain.timing[i]; |
| 369 | if (ti.ready()) { |
| 370 | ti.get_values(&timings[num_copied]); |
| 371 | num_copied++; |
| 372 | } |
| 373 | num_to_remove++; |
| 374 | } |
| 375 | |
| 376 | // Discard old frames that aren't ready if newer frames are ready. |
| 377 | // We don't expect to get the timing info for those old frames. |
| 378 | swapchain.timing.removeItemsAt(0, num_to_remove); |
| 379 | |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 380 | *count = num_copied; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 383 | } // anonymous namespace |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 384 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 385 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 386 | VkResult CreateAndroidSurfaceKHR( |
Jesse Hall | f9fa9a5 | 2016-01-08 16:08:51 -0800 | [diff] [blame] | 387 | VkInstance instance, |
| 388 | const VkAndroidSurfaceCreateInfoKHR* pCreateInfo, |
| 389 | const VkAllocationCallbacks* allocator, |
| 390 | VkSurfaceKHR* out_surface) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 391 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 392 | allocator = &GetData(instance).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 393 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface), |
| 394 | alignof(Surface), |
| 395 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 396 | if (!mem) |
| 397 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 398 | Surface* surface = new (mem) Surface; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 399 | |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 400 | surface->window = pCreateInfo->window; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 401 | surface->swapchain_handle = VK_NULL_HANDLE; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 402 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 403 | // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN. |
| 404 | int err = |
| 405 | native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
| 406 | if (err != 0) { |
| 407 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 408 | // errors and translate them to valid Vulkan result codes? |
| 409 | ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err), |
| 410 | err); |
| 411 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 412 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 413 | return VK_ERROR_INITIALIZATION_FAILED; |
| 414 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 415 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 416 | *out_surface = HandleFromSurface(surface); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 417 | return VK_SUCCESS; |
| 418 | } |
| 419 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 420 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 421 | void DestroySurfaceKHR(VkInstance instance, |
| 422 | VkSurfaceKHR surface_handle, |
| 423 | const VkAllocationCallbacks* allocator) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 424 | Surface* surface = SurfaceFromHandle(surface_handle); |
| 425 | if (!surface) |
| 426 | return; |
| 427 | native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 428 | ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 429 | "destroyed VkSurfaceKHR 0x%" PRIx64 |
| 430 | " has active VkSwapchainKHR 0x%" PRIx64, |
| 431 | reinterpret_cast<uint64_t>(surface_handle), |
| 432 | reinterpret_cast<uint64_t>(surface->swapchain_handle)); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 433 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 434 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 435 | allocator = &GetData(instance).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 436 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 439 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 440 | VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/, |
| 441 | uint32_t /*queue_family*/, |
| 442 | VkSurfaceKHR /*surface*/, |
| 443 | VkBool32* supported) { |
Jesse Hall | 0e74f00 | 2015-11-30 11:37:59 -0800 | [diff] [blame] | 444 | *supported = VK_TRUE; |
Jesse Hall | a642925 | 2015-11-29 18:59:42 -0800 | [diff] [blame] | 445 | return VK_SUCCESS; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 448 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 449 | VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR( |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 450 | VkPhysicalDevice /*pdev*/, |
| 451 | VkSurfaceKHR surface, |
| 452 | VkSurfaceCapabilitiesKHR* capabilities) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 453 | int err; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 454 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 455 | |
| 456 | int width, height; |
| 457 | err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width); |
| 458 | if (err != 0) { |
| 459 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 460 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 461 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 462 | } |
| 463 | err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height); |
| 464 | if (err != 0) { |
| 465 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 466 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 467 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 470 | int transform_hint; |
| 471 | err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint); |
| 472 | if (err != 0) { |
| 473 | ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)", |
| 474 | strerror(-err), err); |
| 475 | return VK_ERROR_INITIALIZATION_FAILED; |
| 476 | } |
| 477 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 478 | // TODO(jessehall): Figure out what the min/max values should be. |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 479 | capabilities->minImageCount = 2; |
| 480 | capabilities->maxImageCount = 3; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 481 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 482 | capabilities->currentExtent = |
| 483 | VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)}; |
| 484 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 485 | // TODO(jessehall): Figure out what the max extent should be. Maximum |
| 486 | // texture dimension maybe? |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 487 | capabilities->minImageExtent = VkExtent2D{1, 1}; |
| 488 | capabilities->maxImageExtent = VkExtent2D{4096, 4096}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 489 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 490 | capabilities->maxImageArrayLayers = 1; |
| 491 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 492 | capabilities->supportedTransforms = kSupportedTransforms; |
| 493 | capabilities->currentTransform = |
| 494 | TranslateNativeToVulkanTransform(transform_hint); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 495 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 496 | // On Android, window composition is a WindowManager property, not something |
| 497 | // associated with the bufferqueue. It can't be changed from here. |
| 498 | capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 499 | |
| 500 | // TODO(jessehall): I think these are right, but haven't thought hard about |
| 501 | // it. Do we need to query the driver for support of any of these? |
| 502 | // Currently not included: |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 503 | // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not |
| 504 | // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 505 | capabilities->supportedUsageFlags = |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 506 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 507 | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | |
| 508 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 509 | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 510 | |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 511 | return VK_SUCCESS; |
| 512 | } |
| 513 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 514 | VKAPI_ATTR |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 515 | VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev, |
| 516 | VkSurfaceKHR surface_handle, |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 517 | uint32_t* count, |
| 518 | VkSurfaceFormatKHR* formats) { |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 519 | const InstanceData& instance_data = GetData(pdev); |
| 520 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 521 | // TODO(jessehall): Fill out the set of supported formats. Longer term, add |
| 522 | // a new gralloc method to query whether a (format, usage) pair is |
| 523 | // supported, and check that for each gralloc format that corresponds to a |
| 524 | // Vulkan format. Shorter term, just add a few more formats to the ones |
| 525 | // hardcoded below. |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 526 | |
| 527 | const VkSurfaceFormatKHR kFormats[] = { |
Jesse Hall | 2676338 | 2016-05-20 07:13:52 -0700 | [diff] [blame] | 528 | {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
| 529 | {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
| 530 | {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 531 | }; |
| 532 | const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]); |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 533 | uint32_t total_num_formats = kNumFormats; |
| 534 | |
| 535 | bool wide_color_support = false; |
| 536 | Surface& surface = *SurfaceFromHandle(surface_handle); |
| 537 | int err = native_window_get_wide_color_support(surface.window.get(), |
| 538 | &wide_color_support); |
| 539 | if (err) { |
| 540 | // Not allowed to return a more sensible error code, so do this |
| 541 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 542 | } |
| 543 | ALOGV("wide_color_support is: %d", wide_color_support); |
| 544 | wide_color_support = |
| 545 | wide_color_support && |
| 546 | instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace); |
| 547 | |
| 548 | const VkSurfaceFormatKHR kWideColorFormats[] = { |
Courtney Goeltzenleuchter | bca34c9 | 2017-02-17 11:31:23 -0700 | [diff] [blame^] | 549 | {VK_FORMAT_R16G16B16A16_SFLOAT, |
| 550 | VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT}, |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 551 | {VK_FORMAT_A2R10G10B10_UNORM_PACK32, |
| 552 | VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT}, |
| 553 | }; |
| 554 | const uint32_t kNumWideColorFormats = |
| 555 | sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]); |
| 556 | if (wide_color_support) { |
| 557 | total_num_formats += kNumWideColorFormats; |
| 558 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 559 | |
| 560 | VkResult result = VK_SUCCESS; |
| 561 | if (formats) { |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 562 | uint32_t out_count = 0; |
| 563 | uint32_t transfer_count = 0; |
| 564 | if (*count < total_num_formats) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 565 | result = VK_INCOMPLETE; |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 566 | transfer_count = std::min(*count, kNumFormats); |
| 567 | std::copy(kFormats, kFormats + transfer_count, formats); |
| 568 | out_count += transfer_count; |
| 569 | if (wide_color_support) { |
| 570 | transfer_count = std::min(*count - out_count, kNumWideColorFormats); |
| 571 | std::copy(kWideColorFormats, kWideColorFormats + transfer_count, |
| 572 | formats + out_count); |
| 573 | out_count += transfer_count; |
| 574 | } |
| 575 | *count = out_count; |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 576 | } else { |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 577 | *count = total_num_formats; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 578 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 579 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 582 | VKAPI_ATTR |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 583 | VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev, |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 584 | VkSurfaceKHR /*surface*/, |
| 585 | uint32_t* count, |
| 586 | VkPresentModeKHR* modes) { |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 587 | android::Vector<VkPresentModeKHR> present_modes; |
| 588 | present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR); |
| 589 | present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR); |
| 590 | |
| 591 | VkPhysicalDevicePresentationPropertiesANDROID present_properties; |
| 592 | if (QueryPresentationProperties(pdev, &present_properties)) { |
| 593 | if (present_properties.sharedImage) { |
| 594 | present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR); |
| 595 | present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | uint32_t num_modes = uint32_t(present_modes.size()); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 600 | |
| 601 | VkResult result = VK_SUCCESS; |
| 602 | if (modes) { |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 603 | if (*count < num_modes) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 604 | result = VK_INCOMPLETE; |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 605 | *count = std::min(*count, num_modes); |
| 606 | std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes); |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 607 | } else { |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 608 | *count = num_modes; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 609 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 610 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 613 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 614 | VkResult CreateSwapchainKHR(VkDevice device, |
| 615 | const VkSwapchainCreateInfoKHR* create_info, |
| 616 | const VkAllocationCallbacks* allocator, |
| 617 | VkSwapchainKHR* swapchain_handle) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 618 | int err; |
| 619 | VkResult result = VK_SUCCESS; |
| 620 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 621 | ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64 |
| 622 | " minImageCount=%u imageFormat=%u imageColorSpace=%u" |
| 623 | " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u" |
| 624 | " oldSwapchain=0x%" PRIx64, |
| 625 | reinterpret_cast<uint64_t>(create_info->surface), |
| 626 | create_info->minImageCount, create_info->imageFormat, |
| 627 | create_info->imageColorSpace, create_info->imageExtent.width, |
| 628 | create_info->imageExtent.height, create_info->imageUsage, |
| 629 | create_info->preTransform, create_info->presentMode, |
| 630 | reinterpret_cast<uint64_t>(create_info->oldSwapchain)); |
| 631 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 632 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 633 | allocator = &GetData(device).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 634 | |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 635 | ALOGV_IF(create_info->imageArrayLayers != 1, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 636 | "swapchain imageArrayLayers=%u not supported", |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 637 | create_info->imageArrayLayers); |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 638 | ALOGV_IF( |
| 639 | create_info->imageColorSpace != VK_COLOR_SPACE_SRGB_NONLINEAR_KHR && |
| 640 | create_info->imageColorSpace != VK_COLOR_SPACE_SCRGB_LINEAR_EXT && |
| 641 | create_info->imageColorSpace != |
| 642 | VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT, |
| 643 | "swapchain imageColorSpace=%u not supported", |
| 644 | create_info->imageColorSpace); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 645 | ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 646 | "swapchain preTransform=%#x not supported", |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 647 | create_info->preTransform); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 648 | ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR || |
Chris Forbes | 980ad05 | 2017-01-18 16:55:07 +1300 | [diff] [blame] | 649 | create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR || |
Chris Forbes | 1d5f68c | 2017-01-31 10:17:01 +1300 | [diff] [blame] | 650 | create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR || |
| 651 | create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR), |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 652 | "swapchain presentMode=%u not supported", |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 653 | create_info->presentMode); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 654 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 655 | Surface& surface = *SurfaceFromHandle(create_info->surface); |
| 656 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 657 | if (surface.swapchain_handle != create_info->oldSwapchain) { |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 658 | ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64 |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 659 | " because it already has active swapchain 0x%" PRIx64 |
| 660 | " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64, |
| 661 | reinterpret_cast<uint64_t>(create_info->surface), |
| 662 | reinterpret_cast<uint64_t>(surface.swapchain_handle), |
| 663 | reinterpret_cast<uint64_t>(create_info->oldSwapchain)); |
| 664 | return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR; |
| 665 | } |
| 666 | if (create_info->oldSwapchain != VK_NULL_HANDLE) |
| 667 | OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain)); |
| 668 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 669 | // -- Reset the native window -- |
| 670 | // The native window might have been used previously, and had its properties |
| 671 | // changed from defaults. That will affect the answer we get for queries |
| 672 | // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we |
| 673 | // attempt such queries. |
| 674 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 675 | // The native window only allows dequeueing all buffers before any have |
| 676 | // been queued, since after that point at least one is assumed to be in |
| 677 | // non-FREE state at any given time. Disconnecting and re-connecting |
| 678 | // orphans the previous buffers, getting us back to the state where we can |
| 679 | // dequeue all buffers. |
| 680 | err = native_window_api_disconnect(surface.window.get(), |
| 681 | NATIVE_WINDOW_API_EGL); |
| 682 | ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)", |
| 683 | strerror(-err), err); |
| 684 | err = |
| 685 | native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL); |
| 686 | ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)", |
| 687 | strerror(-err), err); |
| 688 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 689 | err = native_window_set_buffer_count(surface.window.get(), 0); |
| 690 | if (err != 0) { |
| 691 | ALOGE("native_window_set_buffer_count(0) failed: %s (%d)", |
| 692 | strerror(-err), err); |
| 693 | return VK_ERROR_INITIALIZATION_FAILED; |
| 694 | } |
| 695 | |
| 696 | err = surface.window->setSwapInterval(surface.window.get(), 1); |
| 697 | if (err != 0) { |
| 698 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 699 | // errors and translate them to valid Vulkan result codes? |
| 700 | ALOGE("native_window->setSwapInterval(1) failed: %s (%d)", |
| 701 | strerror(-err), err); |
| 702 | return VK_ERROR_INITIALIZATION_FAILED; |
| 703 | } |
| 704 | |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 705 | err = native_window_set_shared_buffer_mode(surface.window.get(), false); |
| 706 | if (err != 0) { |
| 707 | ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)", |
| 708 | strerror(-err), err); |
| 709 | return VK_ERROR_INITIALIZATION_FAILED; |
| 710 | } |
| 711 | |
| 712 | err = native_window_set_auto_refresh(surface.window.get(), false); |
| 713 | if (err != 0) { |
| 714 | ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)", |
| 715 | strerror(-err), err); |
| 716 | return VK_ERROR_INITIALIZATION_FAILED; |
| 717 | } |
| 718 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 719 | // -- Configure the native window -- |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 720 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 721 | const auto& dispatch = GetData(device).driver; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 722 | |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 723 | int native_format = HAL_PIXEL_FORMAT_RGBA_8888; |
| 724 | switch (create_info->imageFormat) { |
| 725 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 726 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 727 | native_format = HAL_PIXEL_FORMAT_RGBA_8888; |
| 728 | break; |
| 729 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 730 | native_format = HAL_PIXEL_FORMAT_RGB_565; |
| 731 | break; |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 732 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 733 | native_format = HAL_PIXEL_FORMAT_RGBA_FP16; |
| 734 | break; |
| 735 | case VK_FORMAT_A2R10G10B10_UNORM_PACK32: |
| 736 | native_format = HAL_PIXEL_FORMAT_RGBA_1010102; |
| 737 | break; |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 738 | default: |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 739 | ALOGV("unsupported swapchain format %d", create_info->imageFormat); |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 740 | break; |
| 741 | } |
| 742 | err = native_window_set_buffers_format(surface.window.get(), native_format); |
| 743 | if (err != 0) { |
| 744 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 745 | // errors and translate them to valid Vulkan result codes? |
| 746 | ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)", |
| 747 | native_format, strerror(-err), err); |
| 748 | return VK_ERROR_INITIALIZATION_FAILED; |
| 749 | } |
| 750 | err = native_window_set_buffers_data_space(surface.window.get(), |
| 751 | HAL_DATASPACE_SRGB_LINEAR); |
| 752 | if (err != 0) { |
| 753 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 754 | // errors and translate them to valid Vulkan result codes? |
| 755 | ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)", |
| 756 | HAL_DATASPACE_SRGB_LINEAR, strerror(-err), err); |
| 757 | return VK_ERROR_INITIALIZATION_FAILED; |
| 758 | } |
| 759 | |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 760 | err = native_window_set_buffers_dimensions( |
| 761 | surface.window.get(), static_cast<int>(create_info->imageExtent.width), |
| 762 | static_cast<int>(create_info->imageExtent.height)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 763 | if (err != 0) { |
| 764 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 765 | // errors and translate them to valid Vulkan result codes? |
| 766 | ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)", |
| 767 | create_info->imageExtent.width, create_info->imageExtent.height, |
| 768 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 769 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 772 | // VkSwapchainCreateInfo::preTransform indicates the transformation the app |
| 773 | // applied during rendering. native_window_set_transform() expects the |
| 774 | // inverse: the transform the app is requesting that the compositor perform |
| 775 | // during composition. With native windows, pre-transform works by rendering |
| 776 | // with the same transform the compositor is applying (as in Vulkan), but |
| 777 | // then requesting the inverse transform, so that when the compositor does |
| 778 | // it's job the two transforms cancel each other out and the compositor ends |
| 779 | // up applying an identity transform to the app's buffer. |
| 780 | err = native_window_set_buffers_transform( |
| 781 | surface.window.get(), |
| 782 | InvertTransformToNative(create_info->preTransform)); |
| 783 | if (err != 0) { |
| 784 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 785 | // errors and translate them to valid Vulkan result codes? |
| 786 | ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)", |
| 787 | InvertTransformToNative(create_info->preTransform), |
| 788 | strerror(-err), err); |
| 789 | return VK_ERROR_INITIALIZATION_FAILED; |
| 790 | } |
| 791 | |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 792 | err = native_window_set_scaling_mode( |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 793 | surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 794 | if (err != 0) { |
| 795 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 796 | // errors and translate them to valid Vulkan result codes? |
| 797 | ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)", |
| 798 | strerror(-err), err); |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 799 | return VK_ERROR_INITIALIZATION_FAILED; |
| 800 | } |
| 801 | |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 802 | int query_value; |
| 803 | err = surface.window->query(surface.window.get(), |
| 804 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 805 | &query_value); |
| 806 | if (err != 0 || query_value < 0) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 807 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 808 | // errors and translate them to valid Vulkan result codes? |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 809 | ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, |
| 810 | query_value); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 811 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 812 | } |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 813 | uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value); |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 814 | // The MIN_UNDEQUEUED_BUFFERS query doesn't know whether we'll be using |
| 815 | // async mode or not, and assumes not. But in async mode, the BufferQueue |
| 816 | // requires an extra undequeued buffer. |
| 817 | // See BufferQueueCore::getMinUndequeuedBufferCountLocked(). |
| 818 | if (create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR) |
| 819 | min_undequeued_buffers += 1; |
| 820 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 821 | uint32_t num_images = |
| 822 | (create_info->minImageCount - 1) + min_undequeued_buffers; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 823 | err = native_window_set_buffer_count(surface.window.get(), num_images); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 824 | if (err != 0) { |
| 825 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 826 | // errors and translate them to valid Vulkan result codes? |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 827 | ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images, |
| 828 | strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 829 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 832 | VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0; |
Chris Forbes | 1d5f68c | 2017-01-31 10:17:01 +1300 | [diff] [blame] | 833 | if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR || |
| 834 | create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) { |
Chris Forbes | 4da65b9 | 2017-01-31 11:48:50 +1300 | [diff] [blame] | 835 | swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID; |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 836 | |
| 837 | err = native_window_set_shared_buffer_mode(surface.window.get(), true); |
| 838 | if (err != 0) { |
| 839 | ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err); |
| 840 | return VK_ERROR_INITIALIZATION_FAILED; |
| 841 | } |
| 842 | } |
| 843 | |
Chris Forbes | 1d5f68c | 2017-01-31 10:17:01 +1300 | [diff] [blame] | 844 | if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) { |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 845 | err = native_window_set_auto_refresh(surface.window.get(), true); |
| 846 | if (err != 0) { |
| 847 | ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err); |
| 848 | return VK_ERROR_INITIALIZATION_FAILED; |
| 849 | } |
Chris Forbes | b442152 | 2017-01-18 16:57:02 +1300 | [diff] [blame] | 850 | } |
| 851 | |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 852 | int gralloc_usage = 0; |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 853 | if (dispatch.GetSwapchainGrallocUsage2ANDROID) { |
Jesse Hall | d1abd74 | 2017-02-09 21:45:51 -0800 | [diff] [blame] | 854 | uint64_t consumer_usage, producer_usage; |
Kalle Raita | fc00a1d | 2017-02-22 13:05:45 -0800 | [diff] [blame] | 855 | uint32_t driver_version = GetData(device).driver_version; |
| 856 | if (driver_version == 256587285 || driver_version == 96011958) { |
Jesse Hall | 85bb0c5 | 2017-02-09 22:13:02 -0800 | [diff] [blame] | 857 | // HACK workaround for loader/driver mismatch during transition to |
| 858 | // vkGetSwapchainGrallocUsage2ANDROID. |
| 859 | typedef VkResult(VKAPI_PTR * |
| 860 | PFN_vkGetSwapchainGrallocUsage2ANDROID_HACK)( |
| 861 | VkDevice device, VkFormat format, VkImageUsageFlags imageUsage, |
| 862 | uint64_t * grallocConsumerUsage, |
| 863 | uint64_t * grallocProducerUsage); |
| 864 | auto get_swapchain_gralloc_usage = |
| 865 | reinterpret_cast<PFN_vkGetSwapchainGrallocUsage2ANDROID_HACK>( |
| 866 | dispatch.GetSwapchainGrallocUsage2ANDROID); |
| 867 | result = get_swapchain_gralloc_usage( |
| 868 | device, create_info->imageFormat, create_info->imageUsage, |
| 869 | &consumer_usage, &producer_usage); |
| 870 | } else { |
| 871 | result = dispatch.GetSwapchainGrallocUsage2ANDROID( |
| 872 | device, create_info->imageFormat, create_info->imageUsage, |
| 873 | swapchain_image_usage, &consumer_usage, &producer_usage); |
| 874 | } |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 875 | if (result != VK_SUCCESS) { |
| 876 | ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result); |
| 877 | return VK_ERROR_INITIALIZATION_FAILED; |
| 878 | } |
Jesse Hall | d1abd74 | 2017-02-09 21:45:51 -0800 | [diff] [blame] | 879 | // TODO: This is the same translation done by Gralloc1On0Adapter. |
| 880 | // Remove it once ANativeWindow has been updated to take gralloc1-style |
| 881 | // usages. |
| 882 | gralloc_usage = |
| 883 | static_cast<int>(consumer_usage) | static_cast<int>(producer_usage); |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 884 | } else if (dispatch.GetSwapchainGrallocUsageANDROID) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 885 | result = dispatch.GetSwapchainGrallocUsageANDROID( |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 886 | device, create_info->imageFormat, create_info->imageUsage, |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 887 | &gralloc_usage); |
| 888 | if (result != VK_SUCCESS) { |
| 889 | ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 890 | return VK_ERROR_INITIALIZATION_FAILED; |
| 891 | } |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 892 | } |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 893 | err = native_window_set_usage(surface.window.get(), gralloc_usage); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 894 | if (err != 0) { |
| 895 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 896 | // errors and translate them to valid Vulkan result codes? |
| 897 | ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 898 | return VK_ERROR_INITIALIZATION_FAILED; |
| 899 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 900 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 901 | int swap_interval = |
| 902 | create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1; |
| 903 | err = surface.window->setSwapInterval(surface.window.get(), swap_interval); |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 904 | if (err != 0) { |
| 905 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 906 | // errors and translate them to valid Vulkan result codes? |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 907 | ALOGE("native_window->setSwapInterval(%d) failed: %s (%d)", |
| 908 | swap_interval, strerror(-err), err); |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 909 | return VK_ERROR_INITIALIZATION_FAILED; |
| 910 | } |
| 911 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 912 | // -- Allocate our Swapchain object -- |
| 913 | // After this point, we must deallocate the swapchain on error. |
| 914 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 915 | void* mem = allocator->pfnAllocation(allocator->pUserData, |
| 916 | sizeof(Swapchain), alignof(Swapchain), |
| 917 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 918 | if (!mem) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 919 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 920 | Swapchain* swapchain = |
| 921 | new (mem) Swapchain(surface, num_images, create_info->presentMode); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 922 | |
| 923 | // -- Dequeue all buffers and create a VkImage for each -- |
| 924 | // Any failures during or after this must cancel the dequeued buffers. |
| 925 | |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 926 | VkSwapchainImageCreateInfoANDROID swapchain_image_create = { |
| 927 | #pragma clang diagnostic push |
| 928 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 929 | .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID, |
| 930 | #pragma clang diagnostic pop |
| 931 | .pNext = nullptr, |
| 932 | .usage = swapchain_image_usage, |
| 933 | }; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 934 | VkNativeBufferANDROID image_native_buffer = { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 935 | #pragma clang diagnostic push |
| 936 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 937 | .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID, |
| 938 | #pragma clang diagnostic pop |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 939 | .pNext = &swapchain_image_create, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 940 | }; |
| 941 | VkImageCreateInfo image_create = { |
| 942 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 943 | .pNext = &image_native_buffer, |
| 944 | .imageType = VK_IMAGE_TYPE_2D, |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 945 | .format = create_info->imageFormat, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 946 | .extent = {0, 0, 1}, |
| 947 | .mipLevels = 1, |
Jesse Hall | a15a4bf | 2015-11-19 22:48:02 -0800 | [diff] [blame] | 948 | .arrayLayers = 1, |
Jesse Hall | 091ed9e | 2015-11-30 00:55:29 -0800 | [diff] [blame] | 949 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 950 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 951 | .usage = create_info->imageUsage, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 952 | .flags = 0, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 953 | .sharingMode = create_info->imageSharingMode, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 954 | .queueFamilyIndexCount = create_info->queueFamilyIndexCount, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 955 | .pQueueFamilyIndices = create_info->pQueueFamilyIndices, |
| 956 | }; |
| 957 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 958 | for (uint32_t i = 0; i < num_images; i++) { |
| 959 | Swapchain::Image& img = swapchain->images[i]; |
| 960 | |
| 961 | ANativeWindowBuffer* buffer; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 962 | err = surface.window->dequeueBuffer(surface.window.get(), &buffer, |
| 963 | &img.dequeue_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 964 | if (err != 0) { |
| 965 | // TODO(jessehall): Improve error reporting. Can we enumerate |
| 966 | // possible errors and translate them to valid Vulkan result codes? |
| 967 | ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 968 | result = VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 969 | break; |
| 970 | } |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 971 | img.buffer = buffer; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 972 | img.dequeued = true; |
| 973 | |
| 974 | image_create.extent = |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 975 | VkExtent3D{static_cast<uint32_t>(img.buffer->width), |
| 976 | static_cast<uint32_t>(img.buffer->height), |
| 977 | 1}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 978 | image_native_buffer.handle = img.buffer->handle; |
| 979 | image_native_buffer.stride = img.buffer->stride; |
| 980 | image_native_buffer.format = img.buffer->format; |
| 981 | image_native_buffer.usage = img.buffer->usage; |
Jesse Hall | d1abd74 | 2017-02-09 21:45:51 -0800 | [diff] [blame] | 982 | // TODO: Adjust once ANativeWindowBuffer supports gralloc1-style usage. |
| 983 | // For now, this is the same translation Gralloc1On0Adapter does. |
| 984 | image_native_buffer.usage2.consumer = |
| 985 | static_cast<uint64_t>(img.buffer->usage); |
| 986 | image_native_buffer.usage2.producer = |
| 987 | static_cast<uint64_t>(img.buffer->usage); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 988 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 989 | result = |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 990 | dispatch.CreateImage(device, &image_create, nullptr, &img.image); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 991 | if (result != VK_SUCCESS) { |
| 992 | ALOGD("vkCreateImage w/ native buffer failed: %u", result); |
| 993 | break; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | // -- Cancel all buffers, returning them to the queue -- |
| 998 | // If an error occurred before, also destroy the VkImage and release the |
| 999 | // buffer reference. Otherwise, we retain a strong reference to the buffer. |
| 1000 | // |
| 1001 | // TODO(jessehall): The error path here is the same as DestroySwapchain, |
| 1002 | // but not the non-error path. Should refactor/unify. |
| 1003 | for (uint32_t i = 0; i < num_images; i++) { |
| 1004 | Swapchain::Image& img = swapchain->images[i]; |
| 1005 | if (img.dequeued) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1006 | surface.window->cancelBuffer(surface.window.get(), img.buffer.get(), |
| 1007 | img.dequeue_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1008 | img.dequeue_fence = -1; |
| 1009 | img.dequeued = false; |
| 1010 | } |
| 1011 | if (result != VK_SUCCESS) { |
| 1012 | if (img.image) |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1013 | dispatch.DestroyImage(device, img.image, nullptr); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | if (result != VK_SUCCESS) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1018 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1019 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1020 | return result; |
| 1021 | } |
| 1022 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1023 | surface.swapchain_handle = HandleFromSwapchain(swapchain); |
| 1024 | *swapchain_handle = surface.swapchain_handle; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1025 | return VK_SUCCESS; |
| 1026 | } |
| 1027 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1028 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1029 | void DestroySwapchainKHR(VkDevice device, |
| 1030 | VkSwapchainKHR swapchain_handle, |
| 1031 | const VkAllocationCallbacks* allocator) { |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1032 | const auto& dispatch = GetData(device).driver; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1033 | Swapchain* swapchain = SwapchainFromHandle(swapchain_handle); |
Daniel Koch | d78c2e8 | 2016-12-13 18:45:13 -0500 | [diff] [blame] | 1034 | if (!swapchain) |
| 1035 | return; |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 1036 | bool active = swapchain->surface.swapchain_handle == swapchain_handle; |
| 1037 | ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1038 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1039 | if (swapchain->frame_timestamps_enabled) { |
| 1040 | native_window_enable_frame_timestamps(window, false); |
| 1041 | } |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1042 | for (uint32_t i = 0; i < swapchain->num_images; i++) |
| 1043 | ReleaseSwapchainImage(device, window, -1, swapchain->images[i]); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 1044 | if (active) |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1045 | swapchain->surface.swapchain_handle = VK_NULL_HANDLE; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1046 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1047 | allocator = &GetData(device).allocator; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1048 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1049 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1052 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1053 | VkResult GetSwapchainImagesKHR(VkDevice, |
| 1054 | VkSwapchainKHR swapchain_handle, |
| 1055 | uint32_t* count, |
| 1056 | VkImage* images) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1057 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1058 | ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle, |
| 1059 | "getting images for non-active swapchain 0x%" PRIx64 |
| 1060 | "; only dequeued image handles are valid", |
| 1061 | reinterpret_cast<uint64_t>(swapchain_handle)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1062 | VkResult result = VK_SUCCESS; |
| 1063 | if (images) { |
| 1064 | uint32_t n = swapchain.num_images; |
| 1065 | if (*count < swapchain.num_images) { |
| 1066 | n = *count; |
| 1067 | result = VK_INCOMPLETE; |
| 1068 | } |
| 1069 | for (uint32_t i = 0; i < n; i++) |
| 1070 | images[i] = swapchain.images[i].image; |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 1071 | *count = n; |
| 1072 | } else { |
| 1073 | *count = swapchain.num_images; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1074 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1075 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1078 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1079 | VkResult AcquireNextImageKHR(VkDevice device, |
| 1080 | VkSwapchainKHR swapchain_handle, |
| 1081 | uint64_t timeout, |
| 1082 | VkSemaphore semaphore, |
| 1083 | VkFence vk_fence, |
| 1084 | uint32_t* image_index) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1085 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1086 | ANativeWindow* window = swapchain.surface.window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1087 | VkResult result; |
| 1088 | int err; |
| 1089 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1090 | if (swapchain.surface.swapchain_handle != swapchain_handle) |
| 1091 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 1092 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1093 | ALOGW_IF( |
| 1094 | timeout != UINT64_MAX, |
| 1095 | "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented"); |
| 1096 | |
| 1097 | ANativeWindowBuffer* buffer; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1098 | int fence_fd; |
| 1099 | err = window->dequeueBuffer(window, &buffer, &fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1100 | if (err != 0) { |
| 1101 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1102 | // errors and translate them to valid Vulkan result codes? |
| 1103 | ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err); |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 1104 | return VK_ERROR_INITIALIZATION_FAILED; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | uint32_t idx; |
| 1108 | for (idx = 0; idx < swapchain.num_images; idx++) { |
| 1109 | if (swapchain.images[idx].buffer.get() == buffer) { |
| 1110 | swapchain.images[idx].dequeued = true; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1111 | swapchain.images[idx].dequeue_fence = fence_fd; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1112 | break; |
| 1113 | } |
| 1114 | } |
| 1115 | if (idx == swapchain.num_images) { |
| 1116 | ALOGE("dequeueBuffer returned unrecognized buffer"); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1117 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1118 | return VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | int fence_clone = -1; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1122 | if (fence_fd != -1) { |
| 1123 | fence_clone = dup(fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1124 | if (fence_clone == -1) { |
| 1125 | ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", |
| 1126 | strerror(errno), errno); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1127 | sync_wait(fence_fd, -1 /* forever */); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1131 | result = GetData(device).driver.AcquireImageANDROID( |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1132 | device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1133 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 1134 | // NOTE: we're relying on AcquireImageANDROID to close fence_clone, |
| 1135 | // even if the call fails. We could close it ourselves on failure, but |
| 1136 | // that would create a race condition if the driver closes it on a |
| 1137 | // failure path: some other thread might create an fd with the same |
| 1138 | // number between the time the driver closes it and the time we close |
| 1139 | // it. We must assume one of: the driver *always* closes it even on |
| 1140 | // failure, or *never* closes it on failure. |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1141 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1142 | swapchain.images[idx].dequeued = false; |
| 1143 | swapchain.images[idx].dequeue_fence = -1; |
| 1144 | return result; |
| 1145 | } |
| 1146 | |
| 1147 | *image_index = idx; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1148 | return VK_SUCCESS; |
| 1149 | } |
| 1150 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1151 | static VkResult WorstPresentResult(VkResult a, VkResult b) { |
| 1152 | // See the error ranking for vkQueuePresentKHR at the end of section 29.6 |
| 1153 | // (in spec version 1.0.14). |
| 1154 | static const VkResult kWorstToBest[] = { |
| 1155 | VK_ERROR_DEVICE_LOST, |
| 1156 | VK_ERROR_SURFACE_LOST_KHR, |
| 1157 | VK_ERROR_OUT_OF_DATE_KHR, |
| 1158 | VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 1159 | VK_ERROR_OUT_OF_HOST_MEMORY, |
| 1160 | VK_SUBOPTIMAL_KHR, |
| 1161 | }; |
| 1162 | for (auto result : kWorstToBest) { |
| 1163 | if (a == result || b == result) |
| 1164 | return result; |
| 1165 | } |
| 1166 | ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a); |
| 1167 | ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b); |
| 1168 | return a != VK_SUCCESS ? a : b; |
| 1169 | } |
| 1170 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1171 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1172 | VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1173 | ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
| 1174 | "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d", |
| 1175 | present_info->sType); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1176 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1177 | VkDevice device = GetData(queue).driver_device; |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1178 | const auto& dispatch = GetData(queue).driver; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1179 | VkResult final_result = VK_SUCCESS; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1180 | |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1181 | // Look at the pNext chain for supported extension structs: |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1182 | const VkPresentRegionsKHR* present_regions = nullptr; |
| 1183 | const VkPresentTimesInfoGOOGLE* present_times = nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1184 | const VkPresentRegionsKHR* next = |
| 1185 | reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext); |
| 1186 | while (next) { |
| 1187 | switch (next->sType) { |
| 1188 | case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR: |
| 1189 | present_regions = next; |
| 1190 | break; |
Ian Elliott | 14866bb | 2017-01-20 09:15:48 -0700 | [diff] [blame] | 1191 | case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE: |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1192 | present_times = |
| 1193 | reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next); |
| 1194 | break; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1195 | default: |
| 1196 | ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x", |
| 1197 | next->sType); |
| 1198 | break; |
| 1199 | } |
| 1200 | next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext); |
| 1201 | } |
| 1202 | ALOGV_IF( |
| 1203 | present_regions && |
| 1204 | present_regions->swapchainCount != present_info->swapchainCount, |
| 1205 | "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1206 | ALOGV_IF(present_times && |
| 1207 | present_times->swapchainCount != present_info->swapchainCount, |
| 1208 | "VkPresentTimesInfoGOOGLE::swapchainCount != " |
| 1209 | "VkPresentInfo::swapchainCount"); |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1210 | const VkPresentRegionKHR* regions = |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1211 | (present_regions) ? present_regions->pRegions : nullptr; |
| 1212 | const VkPresentTimeGOOGLE* times = |
| 1213 | (present_times) ? present_times->pTimes : nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1214 | const VkAllocationCallbacks* allocator = &GetData(device).allocator; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1215 | android_native_rect_t* rects = nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1216 | uint32_t nrects = 0; |
| 1217 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1218 | for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) { |
| 1219 | Swapchain& swapchain = |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1220 | *SwapchainFromHandle(present_info->pSwapchains[sc]); |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1221 | uint32_t image_idx = present_info->pImageIndices[sc]; |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 1222 | Swapchain::Image& img = swapchain.images[image_idx]; |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 1223 | const VkPresentRegionKHR* region = |
| 1224 | (regions && !swapchain.mailbox_mode) ? ®ions[sc] : nullptr; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1225 | const VkPresentTimeGOOGLE* time = (times) ? ×[sc] : nullptr; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1226 | VkResult swapchain_result = VK_SUCCESS; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1227 | VkResult result; |
| 1228 | int err; |
| 1229 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1230 | int fence = -1; |
Jesse Hall | 275d76c | 2016-01-08 22:39:16 -0800 | [diff] [blame] | 1231 | result = dispatch.QueueSignalReleaseImageANDROID( |
| 1232 | queue, present_info->waitSemaphoreCount, |
| 1233 | present_info->pWaitSemaphores, img.image, &fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1234 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 1235 | ALOGE("QueueSignalReleaseImageANDROID failed: %d", result); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1236 | swapchain_result = result; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1239 | if (swapchain.surface.swapchain_handle == |
| 1240 | present_info->pSwapchains[sc]) { |
| 1241 | ANativeWindow* window = swapchain.surface.window.get(); |
| 1242 | if (swapchain_result == VK_SUCCESS) { |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1243 | if (region) { |
| 1244 | // Process the incremental-present hint for this swapchain: |
| 1245 | uint32_t rcount = region->rectangleCount; |
| 1246 | if (rcount > nrects) { |
| 1247 | android_native_rect_t* new_rects = |
| 1248 | static_cast<android_native_rect_t*>( |
| 1249 | allocator->pfnReallocation( |
| 1250 | allocator->pUserData, rects, |
| 1251 | sizeof(android_native_rect_t) * rcount, |
| 1252 | alignof(android_native_rect_t), |
| 1253 | VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); |
| 1254 | if (new_rects) { |
| 1255 | rects = new_rects; |
| 1256 | nrects = rcount; |
| 1257 | } else { |
| 1258 | rcount = 0; // Ignore the hint for this swapchain |
| 1259 | } |
| 1260 | } |
| 1261 | for (uint32_t r = 0; r < rcount; ++r) { |
| 1262 | if (region->pRectangles[r].layer > 0) { |
| 1263 | ALOGV( |
| 1264 | "vkQueuePresentKHR ignoring invalid layer " |
| 1265 | "(%u); using layer 0 instead", |
| 1266 | region->pRectangles[r].layer); |
| 1267 | } |
| 1268 | int x = region->pRectangles[r].offset.x; |
| 1269 | int y = region->pRectangles[r].offset.y; |
| 1270 | int width = static_cast<int>( |
| 1271 | region->pRectangles[r].extent.width); |
| 1272 | int height = static_cast<int>( |
| 1273 | region->pRectangles[r].extent.height); |
| 1274 | android_native_rect_t* cur_rect = &rects[r]; |
| 1275 | cur_rect->left = x; |
| 1276 | cur_rect->top = y + height; |
| 1277 | cur_rect->right = x + width; |
| 1278 | cur_rect->bottom = y; |
| 1279 | } |
| 1280 | native_window_set_surface_damage(window, rects, rcount); |
| 1281 | } |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1282 | if (time) { |
| 1283 | if (!swapchain.frame_timestamps_enabled) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1284 | ALOGV( |
| 1285 | "Calling " |
| 1286 | "native_window_enable_frame_timestamps(true)"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1287 | native_window_enable_frame_timestamps(window, true); |
| 1288 | swapchain.frame_timestamps_enabled = true; |
| 1289 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1290 | |
| 1291 | // Record the nativeFrameId so it can be later correlated to |
| 1292 | // this present. |
| 1293 | uint64_t nativeFrameId = 0; |
| 1294 | err = native_window_get_next_frame_id( |
| 1295 | window, &nativeFrameId); |
| 1296 | if (err != android::NO_ERROR) { |
| 1297 | ALOGE("Failed to get next native frame ID."); |
| 1298 | } |
| 1299 | |
| 1300 | // Add a new timing record with the user's presentID and |
| 1301 | // the nativeFrameId. |
| 1302 | swapchain.timing.push_back(TimingInfo(time, nativeFrameId)); |
| 1303 | while (swapchain.timing.size() > MAX_TIMING_INFOS) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1304 | swapchain.timing.removeAt(0); |
| 1305 | } |
| 1306 | if (time->desiredPresentTime) { |
| 1307 | // Set the desiredPresentTime: |
| 1308 | ALOGV( |
| 1309 | "Calling " |
| 1310 | "native_window_set_buffers_timestamp(%" PRId64 ")", |
| 1311 | time->desiredPresentTime); |
| 1312 | native_window_set_buffers_timestamp( |
| 1313 | window, |
| 1314 | static_cast<int64_t>(time->desiredPresentTime)); |
| 1315 | } |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1316 | } |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1317 | err = window->queueBuffer(window, img.buffer.get(), fence); |
| 1318 | // queueBuffer always closes fence, even on error |
| 1319 | if (err != 0) { |
| 1320 | // TODO(jessehall): What now? We should probably cancel the |
| 1321 | // buffer, I guess? |
| 1322 | ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err); |
| 1323 | swapchain_result = WorstPresentResult( |
| 1324 | swapchain_result, VK_ERROR_OUT_OF_DATE_KHR); |
| 1325 | } |
| 1326 | if (img.dequeue_fence >= 0) { |
| 1327 | close(img.dequeue_fence); |
| 1328 | img.dequeue_fence = -1; |
| 1329 | } |
| 1330 | img.dequeued = false; |
| 1331 | } |
| 1332 | if (swapchain_result != VK_SUCCESS) { |
| 1333 | ReleaseSwapchainImage(device, window, fence, img); |
| 1334 | OrphanSwapchain(device, &swapchain); |
| 1335 | } |
| 1336 | } else { |
| 1337 | ReleaseSwapchainImage(device, nullptr, fence, img); |
| 1338 | swapchain_result = VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 1341 | if (present_info->pResults) |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1342 | present_info->pResults[sc] = swapchain_result; |
| 1343 | |
| 1344 | if (swapchain_result != final_result) |
| 1345 | final_result = WorstPresentResult(final_result, swapchain_result); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1346 | } |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1347 | if (rects) { |
| 1348 | allocator->pfnFree(allocator->pUserData, rects); |
| 1349 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1350 | |
| 1351 | return final_result; |
| 1352 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1353 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1354 | VKAPI_ATTR |
| 1355 | VkResult GetRefreshCycleDurationGOOGLE( |
| 1356 | VkDevice, |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 1357 | VkSwapchainKHR swapchain_handle, |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1358 | VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) { |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 1359 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1360 | VkResult result = VK_SUCCESS; |
| 1361 | |
Ian Elliott | be833a2 | 2017-01-25 13:09:20 -0700 | [diff] [blame] | 1362 | pDisplayTimingProperties->refreshDuration = swapchain.refresh_duration; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1363 | |
| 1364 | return result; |
| 1365 | } |
| 1366 | |
| 1367 | VKAPI_ATTR |
| 1368 | VkResult GetPastPresentationTimingGOOGLE( |
| 1369 | VkDevice, |
| 1370 | VkSwapchainKHR swapchain_handle, |
| 1371 | uint32_t* count, |
| 1372 | VkPastPresentationTimingGOOGLE* timings) { |
| 1373 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
| 1374 | ANativeWindow* window = swapchain.surface.window.get(); |
| 1375 | VkResult result = VK_SUCCESS; |
| 1376 | |
| 1377 | if (!swapchain.frame_timestamps_enabled) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1378 | ALOGV("Calling native_window_enable_frame_timestamps(true)"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1379 | native_window_enable_frame_timestamps(window, true); |
| 1380 | swapchain.frame_timestamps_enabled = true; |
| 1381 | } |
| 1382 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1383 | if (timings) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1384 | // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE) |
| 1385 | copy_ready_timings(swapchain, count, timings); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1386 | } else { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1387 | *count = get_num_ready_timings(swapchain); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | return result; |
| 1391 | } |
| 1392 | |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 1393 | VKAPI_ATTR |
| 1394 | VkResult GetSwapchainStatusKHR( |
| 1395 | VkDevice, |
Chris Forbes | 4e18ba8 | 2017-01-20 12:50:17 +1300 | [diff] [blame] | 1396 | VkSwapchainKHR swapchain_handle) { |
| 1397 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 1398 | VkResult result = VK_SUCCESS; |
| 1399 | |
Chris Forbes | 4e18ba8 | 2017-01-20 12:50:17 +1300 | [diff] [blame] | 1400 | if (swapchain.surface.swapchain_handle != swapchain_handle) { |
| 1401 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 1402 | } |
| 1403 | |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 1404 | // TODO(chrisforbes): Implement this function properly |
| 1405 | |
| 1406 | return result; |
| 1407 | } |
| 1408 | |
Courtney Goeltzenleuchter | d634c48 | 2017-01-05 15:55:31 -0700 | [diff] [blame] | 1409 | VKAPI_ATTR void SetHdrMetadataEXT( |
| 1410 | VkDevice device, |
| 1411 | uint32_t swapchainCount, |
| 1412 | const VkSwapchainKHR* pSwapchains, |
| 1413 | const VkHdrMetadataEXT* pHdrMetadataEXTs) { |
| 1414 | // TODO: courtneygo: implement actual function |
| 1415 | (void)device; |
| 1416 | (void)swapchainCount; |
| 1417 | (void)pSwapchains; |
| 1418 | (void)pHdrMetadataEXTs; |
| 1419 | return; |
| 1420 | } |
| 1421 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1422 | } // namespace driver |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1423 | } // namespace vulkan |