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