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