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