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 | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 433 | case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT: |
| 434 | return HAL_DATASPACE_DCI_P3_LINEAR; |
| 435 | case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT: |
| 436 | return HAL_DATASPACE_DCI_P3; |
| 437 | case VK_COLOR_SPACE_BT709_LINEAR_EXT: |
| 438 | return HAL_DATASPACE_V0_SRGB_LINEAR; |
| 439 | case VK_COLOR_SPACE_BT709_NONLINEAR_EXT: |
| 440 | return HAL_DATASPACE_V0_SRGB; |
Courtney Goeltzenleuchter | c45673f | 2017-03-13 15:58:15 -0600 | [diff] [blame] | 441 | case VK_COLOR_SPACE_BT2020_LINEAR_EXT: |
| 442 | return HAL_DATASPACE_BT2020_LINEAR; |
| 443 | case VK_COLOR_SPACE_HDR10_ST2084_EXT: |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 444 | return static_cast<android_dataspace>( |
| 445 | HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 | |
| 446 | HAL_DATASPACE_RANGE_FULL); |
Courtney Goeltzenleuchter | c45673f | 2017-03-13 15:58:15 -0600 | [diff] [blame] | 447 | case VK_COLOR_SPACE_DOLBYVISION_EXT: |
| 448 | return static_cast<android_dataspace>( |
| 449 | HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 | |
| 450 | HAL_DATASPACE_RANGE_FULL); |
| 451 | case VK_COLOR_SPACE_HDR10_HLG_EXT: |
| 452 | return static_cast<android_dataspace>( |
| 453 | HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG | |
| 454 | HAL_DATASPACE_RANGE_FULL); |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 455 | case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT: |
| 456 | return static_cast<android_dataspace>( |
| 457 | HAL_DATASPACE_STANDARD_ADOBE_RGB | |
| 458 | HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL); |
| 459 | case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT: |
| 460 | return HAL_DATASPACE_ADOBE_RGB; |
| 461 | |
| 462 | // Pass through is intended to allow app to provide data that is passed |
| 463 | // to the display system without modification. |
| 464 | case VK_COLOR_SPACE_PASS_THROUGH_EXT: |
| 465 | return HAL_DATASPACE_ARBITRARY; |
| 466 | |
| 467 | default: |
| 468 | // This indicates that we don't know about the |
| 469 | // dataspace specified and we should indicate that |
| 470 | // it's unsupported |
| 471 | return HAL_DATASPACE_UNKNOWN; |
| 472 | } |
| 473 | } |
| 474 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 475 | } // anonymous namespace |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 476 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 477 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 478 | VkResult CreateAndroidSurfaceKHR( |
Jesse Hall | f9fa9a5 | 2016-01-08 16:08:51 -0800 | [diff] [blame] | 479 | VkInstance instance, |
| 480 | const VkAndroidSurfaceCreateInfoKHR* pCreateInfo, |
| 481 | const VkAllocationCallbacks* allocator, |
| 482 | VkSurfaceKHR* out_surface) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 483 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 484 | allocator = &GetData(instance).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 485 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface), |
| 486 | alignof(Surface), |
| 487 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 488 | if (!mem) |
| 489 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 490 | Surface* surface = new (mem) Surface; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 491 | |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 492 | surface->window = pCreateInfo->window; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 493 | surface->swapchain_handle = VK_NULL_HANDLE; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 494 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 495 | // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN. |
| 496 | int err = |
| 497 | native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
| 498 | if (err != 0) { |
| 499 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 500 | // errors and translate them to valid Vulkan result codes? |
| 501 | ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err), |
| 502 | err); |
| 503 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 504 | allocator->pfnFree(allocator->pUserData, surface); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 505 | return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 506 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 507 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 508 | *out_surface = HandleFromSurface(surface); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 509 | return VK_SUCCESS; |
| 510 | } |
| 511 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 512 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 513 | void DestroySurfaceKHR(VkInstance instance, |
| 514 | VkSurfaceKHR surface_handle, |
| 515 | const VkAllocationCallbacks* allocator) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 516 | Surface* surface = SurfaceFromHandle(surface_handle); |
| 517 | if (!surface) |
| 518 | return; |
| 519 | native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 520 | ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 521 | "destroyed VkSurfaceKHR 0x%" PRIx64 |
| 522 | " has active VkSwapchainKHR 0x%" PRIx64, |
| 523 | reinterpret_cast<uint64_t>(surface_handle), |
| 524 | reinterpret_cast<uint64_t>(surface->swapchain_handle)); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 525 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 526 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 527 | allocator = &GetData(instance).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 528 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 531 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 532 | VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/, |
| 533 | uint32_t /*queue_family*/, |
| 534 | VkSurfaceKHR /*surface*/, |
| 535 | VkBool32* supported) { |
Jesse Hall | 0e74f00 | 2015-11-30 11:37:59 -0800 | [diff] [blame] | 536 | *supported = VK_TRUE; |
Jesse Hall | a642925 | 2015-11-29 18:59:42 -0800 | [diff] [blame] | 537 | return VK_SUCCESS; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 540 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 541 | VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR( |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 542 | VkPhysicalDevice /*pdev*/, |
| 543 | VkSurfaceKHR surface, |
| 544 | VkSurfaceCapabilitiesKHR* capabilities) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 545 | int err; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 546 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 547 | |
| 548 | int width, height; |
| 549 | err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width); |
| 550 | if (err != 0) { |
| 551 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 552 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 553 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 554 | } |
| 555 | err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height); |
| 556 | if (err != 0) { |
| 557 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 558 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 559 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 562 | int transform_hint; |
| 563 | err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint); |
| 564 | if (err != 0) { |
| 565 | ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)", |
| 566 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 567 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 570 | // TODO(jessehall): Figure out what the min/max values should be. |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 571 | capabilities->minImageCount = 2; |
| 572 | capabilities->maxImageCount = 3; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 573 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 574 | capabilities->currentExtent = |
| 575 | VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)}; |
| 576 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 577 | // TODO(jessehall): Figure out what the max extent should be. Maximum |
| 578 | // texture dimension maybe? |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 579 | capabilities->minImageExtent = VkExtent2D{1, 1}; |
| 580 | capabilities->maxImageExtent = VkExtent2D{4096, 4096}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 581 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 582 | capabilities->maxImageArrayLayers = 1; |
| 583 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 584 | capabilities->supportedTransforms = kSupportedTransforms; |
| 585 | capabilities->currentTransform = |
| 586 | TranslateNativeToVulkanTransform(transform_hint); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 587 | |
Jesse Hall | fe2662d | 2016-02-09 13:26:59 -0800 | [diff] [blame] | 588 | // On Android, window composition is a WindowManager property, not something |
| 589 | // associated with the bufferqueue. It can't be changed from here. |
| 590 | capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 591 | |
| 592 | // TODO(jessehall): I think these are right, but haven't thought hard about |
| 593 | // it. Do we need to query the driver for support of any of these? |
| 594 | // Currently not included: |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 595 | // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not |
| 596 | // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 597 | capabilities->supportedUsageFlags = |
Jesse Hall | 3fbc856 | 2015-11-29 22:10:52 -0800 | [diff] [blame] | 598 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 599 | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | |
| 600 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 601 | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 602 | |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 603 | return VK_SUCCESS; |
| 604 | } |
| 605 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 606 | VKAPI_ATTR |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 607 | VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev, |
| 608 | VkSurfaceKHR surface_handle, |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 609 | uint32_t* count, |
| 610 | VkSurfaceFormatKHR* formats) { |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 611 | const InstanceData& instance_data = GetData(pdev); |
| 612 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 613 | // TODO(jessehall): Fill out the set of supported formats. Longer term, add |
| 614 | // a new gralloc method to query whether a (format, usage) pair is |
| 615 | // supported, and check that for each gralloc format that corresponds to a |
| 616 | // Vulkan format. Shorter term, just add a few more formats to the ones |
| 617 | // hardcoded below. |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 618 | |
| 619 | const VkSurfaceFormatKHR kFormats[] = { |
Jesse Hall | 2676338 | 2016-05-20 07:13:52 -0700 | [diff] [blame] | 620 | {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
| 621 | {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
| 622 | {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 623 | }; |
| 624 | const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]); |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 625 | uint32_t total_num_formats = kNumFormats; |
| 626 | |
| 627 | bool wide_color_support = false; |
| 628 | Surface& surface = *SurfaceFromHandle(surface_handle); |
| 629 | int err = native_window_get_wide_color_support(surface.window.get(), |
| 630 | &wide_color_support); |
| 631 | if (err) { |
| 632 | // Not allowed to return a more sensible error code, so do this |
| 633 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 634 | } |
| 635 | ALOGV("wide_color_support is: %d", wide_color_support); |
| 636 | wide_color_support = |
| 637 | wide_color_support && |
| 638 | instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace); |
| 639 | |
| 640 | const VkSurfaceFormatKHR kWideColorFormats[] = { |
Courtney Goeltzenleuchter | bca34c9 | 2017-02-17 11:31:23 -0700 | [diff] [blame] | 641 | {VK_FORMAT_R16G16B16A16_SFLOAT, |
| 642 | VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT}, |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 643 | {VK_FORMAT_A2R10G10B10_UNORM_PACK32, |
| 644 | VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT}, |
| 645 | }; |
| 646 | const uint32_t kNumWideColorFormats = |
| 647 | sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]); |
| 648 | if (wide_color_support) { |
| 649 | total_num_formats += kNumWideColorFormats; |
| 650 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 651 | |
| 652 | VkResult result = VK_SUCCESS; |
| 653 | if (formats) { |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 654 | uint32_t out_count = 0; |
| 655 | uint32_t transfer_count = 0; |
| 656 | if (*count < total_num_formats) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 657 | result = VK_INCOMPLETE; |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 658 | transfer_count = std::min(*count, kNumFormats); |
| 659 | std::copy(kFormats, kFormats + transfer_count, formats); |
| 660 | out_count += transfer_count; |
| 661 | if (wide_color_support) { |
| 662 | transfer_count = std::min(*count - out_count, kNumWideColorFormats); |
| 663 | std::copy(kWideColorFormats, kWideColorFormats + transfer_count, |
| 664 | formats + out_count); |
| 665 | out_count += transfer_count; |
| 666 | } |
| 667 | *count = out_count; |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 668 | } else { |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 669 | *count = total_num_formats; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 670 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 671 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 674 | VKAPI_ATTR |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 675 | VkResult GetPhysicalDeviceSurfaceCapabilities2KHR( |
| 676 | VkPhysicalDevice physicalDevice, |
| 677 | const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo, |
| 678 | VkSurfaceCapabilities2KHR* pSurfaceCapabilities) { |
| 679 | VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR( |
| 680 | physicalDevice, pSurfaceInfo->surface, |
| 681 | &pSurfaceCapabilities->surfaceCapabilities); |
| 682 | |
Chris Forbes | 06bc009 | 2017-03-16 16:46:05 +1300 | [diff] [blame] | 683 | VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities; |
| 684 | while (caps->pNext) { |
| 685 | caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext); |
| 686 | |
| 687 | switch (caps->sType) { |
| 688 | case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: { |
| 689 | VkSharedPresentSurfaceCapabilitiesKHR* shared_caps = |
| 690 | reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>( |
| 691 | caps); |
| 692 | // Claim same set of usage flags are supported for |
| 693 | // shared present modes as for other modes. |
| 694 | shared_caps->sharedPresentSupportedUsageFlags = |
| 695 | pSurfaceCapabilities->surfaceCapabilities |
| 696 | .supportedUsageFlags; |
| 697 | } break; |
| 698 | |
| 699 | default: |
| 700 | // Ignore all other extension structs |
| 701 | break; |
| 702 | } |
| 703 | } |
| 704 | |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 705 | return result; |
| 706 | } |
| 707 | |
| 708 | VKAPI_ATTR |
| 709 | VkResult GetPhysicalDeviceSurfaceFormats2KHR( |
| 710 | VkPhysicalDevice physicalDevice, |
| 711 | const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo, |
| 712 | uint32_t* pSurfaceFormatCount, |
| 713 | VkSurfaceFormat2KHR* pSurfaceFormats) { |
| 714 | if (!pSurfaceFormats) { |
| 715 | return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, |
| 716 | pSurfaceInfo->surface, |
| 717 | pSurfaceFormatCount, nullptr); |
| 718 | } else { |
| 719 | // temp vector for forwarding; we'll marshal it into the pSurfaceFormats |
| 720 | // after the call. |
| 721 | android::Vector<VkSurfaceFormatKHR> surface_formats; |
| 722 | surface_formats.resize(*pSurfaceFormatCount); |
| 723 | VkResult result = GetPhysicalDeviceSurfaceFormatsKHR( |
| 724 | physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount, |
| 725 | &surface_formats.editItemAt(0)); |
| 726 | |
| 727 | if (result == VK_SUCCESS || result == VK_INCOMPLETE) { |
| 728 | // marshal results individually due to stride difference. |
| 729 | // completely ignore any chained extension structs. |
| 730 | uint32_t formats_to_marshal = *pSurfaceFormatCount; |
| 731 | for (uint32_t i = 0u; i < formats_to_marshal; i++) { |
| 732 | pSurfaceFormats[i].surfaceFormat = surface_formats[i]; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | return result; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | VKAPI_ATTR |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 741 | VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev, |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 742 | VkSurfaceKHR /*surface*/, |
| 743 | uint32_t* count, |
| 744 | VkPresentModeKHR* modes) { |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 745 | android::Vector<VkPresentModeKHR> present_modes; |
| 746 | present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR); |
| 747 | present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR); |
| 748 | |
| 749 | VkPhysicalDevicePresentationPropertiesANDROID present_properties; |
| 750 | if (QueryPresentationProperties(pdev, &present_properties)) { |
| 751 | if (present_properties.sharedImage) { |
| 752 | present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR); |
| 753 | present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | uint32_t num_modes = uint32_t(present_modes.size()); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 758 | |
| 759 | VkResult result = VK_SUCCESS; |
| 760 | if (modes) { |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 761 | if (*count < num_modes) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 762 | result = VK_INCOMPLETE; |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 763 | *count = std::min(*count, num_modes); |
| 764 | std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes); |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 765 | } else { |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 766 | *count = num_modes; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 767 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 768 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 771 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 772 | VkResult CreateSwapchainKHR(VkDevice device, |
| 773 | const VkSwapchainCreateInfoKHR* create_info, |
| 774 | const VkAllocationCallbacks* allocator, |
| 775 | VkSwapchainKHR* swapchain_handle) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 776 | int err; |
| 777 | VkResult result = VK_SUCCESS; |
| 778 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 779 | ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64 |
| 780 | " minImageCount=%u imageFormat=%u imageColorSpace=%u" |
| 781 | " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u" |
| 782 | " oldSwapchain=0x%" PRIx64, |
| 783 | reinterpret_cast<uint64_t>(create_info->surface), |
| 784 | create_info->minImageCount, create_info->imageFormat, |
| 785 | create_info->imageColorSpace, create_info->imageExtent.width, |
| 786 | create_info->imageExtent.height, create_info->imageUsage, |
| 787 | create_info->preTransform, create_info->presentMode, |
| 788 | reinterpret_cast<uint64_t>(create_info->oldSwapchain)); |
| 789 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 790 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 791 | allocator = &GetData(device).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 792 | |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 793 | android_pixel_format native_pixel_format = |
| 794 | GetNativePixelFormat(create_info->imageFormat); |
| 795 | android_dataspace native_dataspace = |
| 796 | GetNativeDataspace(create_info->imageColorSpace); |
| 797 | if (native_dataspace == HAL_DATASPACE_UNKNOWN) { |
| 798 | ALOGE( |
| 799 | "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) " |
| 800 | "failed: Unsupported color space", |
| 801 | create_info->imageColorSpace); |
| 802 | return VK_ERROR_INITIALIZATION_FAILED; |
| 803 | } |
| 804 | |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 805 | ALOGV_IF(create_info->imageArrayLayers != 1, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 806 | "swapchain imageArrayLayers=%u not supported", |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 807 | create_info->imageArrayLayers); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 808 | ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 809 | "swapchain preTransform=%#x not supported", |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 810 | create_info->preTransform); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 811 | ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR || |
Chris Forbes | 980ad05 | 2017-01-18 16:55:07 +1300 | [diff] [blame] | 812 | create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR || |
Chris Forbes | 1d5f68c | 2017-01-31 10:17:01 +1300 | [diff] [blame] | 813 | create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR || |
| 814 | create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR), |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 815 | "swapchain presentMode=%u not supported", |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 816 | create_info->presentMode); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 817 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 818 | Surface& surface = *SurfaceFromHandle(create_info->surface); |
| 819 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 820 | if (surface.swapchain_handle != create_info->oldSwapchain) { |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 821 | ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64 |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 822 | " because it already has active swapchain 0x%" PRIx64 |
| 823 | " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64, |
| 824 | reinterpret_cast<uint64_t>(create_info->surface), |
| 825 | reinterpret_cast<uint64_t>(surface.swapchain_handle), |
| 826 | reinterpret_cast<uint64_t>(create_info->oldSwapchain)); |
| 827 | return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR; |
| 828 | } |
| 829 | if (create_info->oldSwapchain != VK_NULL_HANDLE) |
| 830 | OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain)); |
| 831 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 832 | // -- Reset the native window -- |
| 833 | // The native window might have been used previously, and had its properties |
| 834 | // changed from defaults. That will affect the answer we get for queries |
| 835 | // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we |
| 836 | // attempt such queries. |
| 837 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 838 | // The native window only allows dequeueing all buffers before any have |
| 839 | // been queued, since after that point at least one is assumed to be in |
| 840 | // non-FREE state at any given time. Disconnecting and re-connecting |
| 841 | // orphans the previous buffers, getting us back to the state where we can |
| 842 | // dequeue all buffers. |
| 843 | err = native_window_api_disconnect(surface.window.get(), |
| 844 | NATIVE_WINDOW_API_EGL); |
| 845 | ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)", |
| 846 | strerror(-err), err); |
| 847 | err = |
| 848 | native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL); |
| 849 | ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)", |
| 850 | strerror(-err), err); |
| 851 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 852 | err = native_window_set_buffer_count(surface.window.get(), 0); |
| 853 | if (err != 0) { |
| 854 | ALOGE("native_window_set_buffer_count(0) failed: %s (%d)", |
| 855 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 856 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Hrishikesh Manohar | 9b7e453 | 2017-01-10 17:52:11 +0530 | [diff] [blame] | 859 | int swap_interval = |
| 860 | create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1; |
| 861 | err = surface.window->setSwapInterval(surface.window.get(), swap_interval); |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 862 | if (err != 0) { |
| 863 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 864 | // errors and translate them to valid Vulkan result codes? |
| 865 | ALOGE("native_window->setSwapInterval(1) failed: %s (%d)", |
| 866 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 867 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 870 | err = native_window_set_shared_buffer_mode(surface.window.get(), false); |
| 871 | if (err != 0) { |
| 872 | ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)", |
| 873 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 874 | return VK_ERROR_SURFACE_LOST_KHR; |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | err = native_window_set_auto_refresh(surface.window.get(), false); |
| 878 | if (err != 0) { |
| 879 | ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)", |
| 880 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 881 | return VK_ERROR_SURFACE_LOST_KHR; |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 882 | } |
| 883 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 884 | // -- Configure the native window -- |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 885 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 886 | const auto& dispatch = GetData(device).driver; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 887 | |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 888 | err = native_window_set_buffers_format(surface.window.get(), |
| 889 | native_pixel_format); |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 890 | if (err != 0) { |
| 891 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 892 | // errors and translate them to valid Vulkan result codes? |
| 893 | ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)", |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 894 | native_pixel_format, strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 895 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 896 | } |
| 897 | err = native_window_set_buffers_data_space(surface.window.get(), |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 898 | native_dataspace); |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 899 | if (err != 0) { |
| 900 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 901 | // errors and translate them to valid Vulkan result codes? |
| 902 | ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)", |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 903 | native_dataspace, strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 904 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 905 | } |
| 906 | |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 907 | err = native_window_set_buffers_dimensions( |
| 908 | surface.window.get(), static_cast<int>(create_info->imageExtent.width), |
| 909 | static_cast<int>(create_info->imageExtent.height)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 910 | if (err != 0) { |
| 911 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 912 | // errors and translate them to valid Vulkan result codes? |
| 913 | ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)", |
| 914 | create_info->imageExtent.width, create_info->imageExtent.height, |
| 915 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 916 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 919 | // VkSwapchainCreateInfo::preTransform indicates the transformation the app |
| 920 | // applied during rendering. native_window_set_transform() expects the |
| 921 | // inverse: the transform the app is requesting that the compositor perform |
| 922 | // during composition. With native windows, pre-transform works by rendering |
| 923 | // with the same transform the compositor is applying (as in Vulkan), but |
| 924 | // then requesting the inverse transform, so that when the compositor does |
| 925 | // it's job the two transforms cancel each other out and the compositor ends |
| 926 | // up applying an identity transform to the app's buffer. |
| 927 | err = native_window_set_buffers_transform( |
| 928 | surface.window.get(), |
| 929 | InvertTransformToNative(create_info->preTransform)); |
| 930 | if (err != 0) { |
| 931 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 932 | // errors and translate them to valid Vulkan result codes? |
| 933 | ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)", |
| 934 | InvertTransformToNative(create_info->preTransform), |
| 935 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 936 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 937 | } |
| 938 | |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 939 | err = native_window_set_scaling_mode( |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 940 | surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 941 | if (err != 0) { |
| 942 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 943 | // errors and translate them to valid Vulkan result codes? |
| 944 | ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)", |
| 945 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 946 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 947 | } |
| 948 | |
Chris Forbes | 97ef461 | 2017-03-30 19:37:50 +1300 | [diff] [blame] | 949 | VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0; |
| 950 | if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR || |
| 951 | create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) { |
| 952 | swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID; |
| 953 | err = native_window_set_shared_buffer_mode(surface.window.get(), true); |
| 954 | if (err != 0) { |
| 955 | ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err); |
| 956 | return VK_ERROR_SURFACE_LOST_KHR; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) { |
| 961 | err = native_window_set_auto_refresh(surface.window.get(), true); |
| 962 | if (err != 0) { |
| 963 | ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err); |
| 964 | return VK_ERROR_SURFACE_LOST_KHR; |
| 965 | } |
| 966 | } |
| 967 | |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 968 | int query_value; |
| 969 | err = surface.window->query(surface.window.get(), |
| 970 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 971 | &query_value); |
| 972 | if (err != 0 || query_value < 0) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 973 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 974 | // errors and translate them to valid Vulkan result codes? |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 975 | ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, |
| 976 | query_value); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 977 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 978 | } |
Jesse Hall | e6080bf | 2016-02-28 20:58:50 -0800 | [diff] [blame] | 979 | uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 980 | uint32_t num_images = |
| 981 | (create_info->minImageCount - 1) + min_undequeued_buffers; |
Chris Forbes | 2c8fc75 | 2017-03-17 11:28:32 +1300 | [diff] [blame] | 982 | |
| 983 | // Lower layer insists that we have at least two buffers. This is wasteful |
| 984 | // and we'd like to relax it in the shared case, but not all the pieces are |
| 985 | // in place for that to work yet. Note we only lie to the lower layer-- we |
| 986 | // don't want to give the app back a swapchain with extra images (which they |
| 987 | // can't actually use!). |
| 988 | 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] | 989 | if (err != 0) { |
| 990 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 991 | // errors and translate them to valid Vulkan result codes? |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 992 | ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images, |
| 993 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 994 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 997 | int gralloc_usage = 0; |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 998 | if (dispatch.GetSwapchainGrallocUsage2ANDROID) { |
Jesse Hall | d1abd74 | 2017-02-09 21:45:51 -0800 | [diff] [blame] | 999 | uint64_t consumer_usage, producer_usage; |
Courtney Goeltzenleuchter | 894780b | 2017-04-03 16:11:30 -0600 | [diff] [blame] | 1000 | result = dispatch.GetSwapchainGrallocUsage2ANDROID( |
| 1001 | device, create_info->imageFormat, create_info->imageUsage, |
| 1002 | swapchain_image_usage, &consumer_usage, &producer_usage); |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 1003 | if (result != VK_SUCCESS) { |
| 1004 | ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1005 | return VK_ERROR_SURFACE_LOST_KHR; |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 1006 | } |
Jesse Hall | d1abd74 | 2017-02-09 21:45:51 -0800 | [diff] [blame] | 1007 | gralloc_usage = |
Jesse Hall | 7992781 | 2017-03-23 11:03:23 -0700 | [diff] [blame] | 1008 | android_convertGralloc1To0Usage(producer_usage, consumer_usage); |
Chris Forbes | 8c47dc9 | 2017-01-12 11:13:58 +1300 | [diff] [blame] | 1009 | } else if (dispatch.GetSwapchainGrallocUsageANDROID) { |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1010 | result = dispatch.GetSwapchainGrallocUsageANDROID( |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1011 | device, create_info->imageFormat, create_info->imageUsage, |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1012 | &gralloc_usage); |
| 1013 | if (result != VK_SUCCESS) { |
| 1014 | ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1015 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1016 | } |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1017 | } |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1018 | err = native_window_set_usage(surface.window.get(), uint64_t(gralloc_usage)); |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1019 | if (err != 0) { |
| 1020 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1021 | // errors and translate them to valid Vulkan result codes? |
| 1022 | ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1023 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1024 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1025 | |
| 1026 | // -- Allocate our Swapchain object -- |
| 1027 | // After this point, we must deallocate the swapchain on error. |
| 1028 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1029 | void* mem = allocator->pfnAllocation(allocator->pUserData, |
| 1030 | sizeof(Swapchain), alignof(Swapchain), |
| 1031 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1032 | if (!mem) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1033 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 1034 | Swapchain* swapchain = |
| 1035 | new (mem) Swapchain(surface, num_images, create_info->presentMode); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1036 | |
| 1037 | // -- Dequeue all buffers and create a VkImage for each -- |
| 1038 | // Any failures during or after this must cancel the dequeued buffers. |
| 1039 | |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 1040 | VkSwapchainImageCreateInfoANDROID swapchain_image_create = { |
| 1041 | #pragma clang diagnostic push |
| 1042 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 1043 | .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID, |
| 1044 | #pragma clang diagnostic pop |
| 1045 | .pNext = nullptr, |
| 1046 | .usage = swapchain_image_usage, |
| 1047 | }; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1048 | VkNativeBufferANDROID image_native_buffer = { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1049 | #pragma clang diagnostic push |
| 1050 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 1051 | .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID, |
| 1052 | #pragma clang diagnostic pop |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 1053 | .pNext = &swapchain_image_create, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1054 | }; |
| 1055 | VkImageCreateInfo image_create = { |
| 1056 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 1057 | .pNext = &image_native_buffer, |
| 1058 | .imageType = VK_IMAGE_TYPE_2D, |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 1059 | .format = create_info->imageFormat, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1060 | .extent = {0, 0, 1}, |
| 1061 | .mipLevels = 1, |
Jesse Hall | a15a4bf | 2015-11-19 22:48:02 -0800 | [diff] [blame] | 1062 | .arrayLayers = 1, |
Jesse Hall | 091ed9e | 2015-11-30 00:55:29 -0800 | [diff] [blame] | 1063 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1064 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1065 | .usage = create_info->imageUsage, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1066 | .flags = 0, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1067 | .sharingMode = create_info->imageSharingMode, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1068 | .queueFamilyIndexCount = create_info->queueFamilyIndexCount, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1069 | .pQueueFamilyIndices = create_info->pQueueFamilyIndices, |
| 1070 | }; |
| 1071 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1072 | for (uint32_t i = 0; i < num_images; i++) { |
| 1073 | Swapchain::Image& img = swapchain->images[i]; |
| 1074 | |
| 1075 | ANativeWindowBuffer* buffer; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1076 | err = surface.window->dequeueBuffer(surface.window.get(), &buffer, |
| 1077 | &img.dequeue_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1078 | if (err != 0) { |
| 1079 | // TODO(jessehall): Improve error reporting. Can we enumerate |
| 1080 | // possible errors and translate them to valid Vulkan result codes? |
| 1081 | ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1082 | result = VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1083 | break; |
| 1084 | } |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 1085 | img.buffer = buffer; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1086 | img.dequeued = true; |
| 1087 | |
| 1088 | image_create.extent = |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 1089 | VkExtent3D{static_cast<uint32_t>(img.buffer->width), |
| 1090 | static_cast<uint32_t>(img.buffer->height), |
| 1091 | 1}; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1092 | image_native_buffer.handle = img.buffer->handle; |
| 1093 | image_native_buffer.stride = img.buffer->stride; |
| 1094 | image_native_buffer.format = img.buffer->format; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1095 | image_native_buffer.usage = int(img.buffer->usage); |
Jesse Hall | d1abd74 | 2017-02-09 21:45:51 -0800 | [diff] [blame] | 1096 | // TODO: Adjust once ANativeWindowBuffer supports gralloc1-style usage. |
| 1097 | // For now, this is the same translation Gralloc1On0Adapter does. |
| 1098 | image_native_buffer.usage2.consumer = |
| 1099 | static_cast<uint64_t>(img.buffer->usage); |
| 1100 | image_native_buffer.usage2.producer = |
| 1101 | static_cast<uint64_t>(img.buffer->usage); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1102 | |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1103 | result = |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1104 | dispatch.CreateImage(device, &image_create, nullptr, &img.image); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1105 | if (result != VK_SUCCESS) { |
| 1106 | ALOGD("vkCreateImage w/ native buffer failed: %u", result); |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | // -- Cancel all buffers, returning them to the queue -- |
| 1112 | // If an error occurred before, also destroy the VkImage and release the |
| 1113 | // buffer reference. Otherwise, we retain a strong reference to the buffer. |
| 1114 | // |
| 1115 | // TODO(jessehall): The error path here is the same as DestroySwapchain, |
| 1116 | // but not the non-error path. Should refactor/unify. |
Chris Forbes | e0ced03 | 2017-03-30 19:44:15 +1300 | [diff] [blame] | 1117 | if (!swapchain->shared) { |
| 1118 | for (uint32_t i = 0; i < num_images; i++) { |
| 1119 | Swapchain::Image& img = swapchain->images[i]; |
| 1120 | if (img.dequeued) { |
| 1121 | surface.window->cancelBuffer(surface.window.get(), img.buffer.get(), |
| 1122 | img.dequeue_fence); |
| 1123 | img.dequeue_fence = -1; |
| 1124 | img.dequeued = false; |
| 1125 | } |
| 1126 | if (result != VK_SUCCESS) { |
| 1127 | if (img.image) |
| 1128 | dispatch.DestroyImage(device, img.image, nullptr); |
| 1129 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | if (result != VK_SUCCESS) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1134 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1135 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1136 | return result; |
| 1137 | } |
| 1138 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1139 | surface.swapchain_handle = HandleFromSwapchain(swapchain); |
| 1140 | *swapchain_handle = surface.swapchain_handle; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1141 | return VK_SUCCESS; |
| 1142 | } |
| 1143 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1144 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1145 | void DestroySwapchainKHR(VkDevice device, |
| 1146 | VkSwapchainKHR swapchain_handle, |
| 1147 | const VkAllocationCallbacks* allocator) { |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1148 | const auto& dispatch = GetData(device).driver; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1149 | Swapchain* swapchain = SwapchainFromHandle(swapchain_handle); |
Daniel Koch | d78c2e8 | 2016-12-13 18:45:13 -0500 | [diff] [blame] | 1150 | if (!swapchain) |
| 1151 | return; |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 1152 | bool active = swapchain->surface.swapchain_handle == swapchain_handle; |
| 1153 | ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1154 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1155 | if (swapchain->frame_timestamps_enabled) { |
| 1156 | native_window_enable_frame_timestamps(window, false); |
| 1157 | } |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1158 | for (uint32_t i = 0; i < swapchain->num_images; i++) |
| 1159 | ReleaseSwapchainImage(device, window, -1, swapchain->images[i]); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 1160 | if (active) |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1161 | swapchain->surface.swapchain_handle = VK_NULL_HANDLE; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1162 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1163 | allocator = &GetData(device).allocator; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1164 | swapchain->~Swapchain(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1165 | allocator->pfnFree(allocator->pUserData, swapchain); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1168 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1169 | VkResult GetSwapchainImagesKHR(VkDevice, |
| 1170 | VkSwapchainKHR swapchain_handle, |
| 1171 | uint32_t* count, |
| 1172 | VkImage* images) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1173 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1174 | ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle, |
| 1175 | "getting images for non-active swapchain 0x%" PRIx64 |
| 1176 | "; only dequeued image handles are valid", |
| 1177 | reinterpret_cast<uint64_t>(swapchain_handle)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1178 | VkResult result = VK_SUCCESS; |
| 1179 | if (images) { |
| 1180 | uint32_t n = swapchain.num_images; |
| 1181 | if (*count < swapchain.num_images) { |
| 1182 | n = *count; |
| 1183 | result = VK_INCOMPLETE; |
| 1184 | } |
| 1185 | for (uint32_t i = 0; i < n; i++) |
| 1186 | images[i] = swapchain.images[i].image; |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 1187 | *count = n; |
| 1188 | } else { |
| 1189 | *count = swapchain.num_images; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1190 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1191 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1194 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1195 | VkResult AcquireNextImageKHR(VkDevice device, |
| 1196 | VkSwapchainKHR swapchain_handle, |
| 1197 | uint64_t timeout, |
| 1198 | VkSemaphore semaphore, |
| 1199 | VkFence vk_fence, |
| 1200 | uint32_t* image_index) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1201 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1202 | ANativeWindow* window = swapchain.surface.window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1203 | VkResult result; |
| 1204 | int err; |
| 1205 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1206 | if (swapchain.surface.swapchain_handle != swapchain_handle) |
| 1207 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 1208 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1209 | ALOGW_IF( |
| 1210 | timeout != UINT64_MAX, |
| 1211 | "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented"); |
| 1212 | |
Chris Forbes | c88409c | 2017-03-30 19:47:37 +1300 | [diff] [blame] | 1213 | if (swapchain.shared) { |
| 1214 | // In shared mode, we keep the buffer dequeued all the time, so we don't |
| 1215 | // want to dequeue a buffer here. Instead, just ask the driver to ensure |
| 1216 | // the semaphore and fence passed to us will be signalled. |
| 1217 | *image_index = 0; |
| 1218 | result = GetData(device).driver.AcquireImageANDROID( |
| 1219 | device, swapchain.images[*image_index].image, -1, semaphore, vk_fence); |
| 1220 | return result; |
| 1221 | } |
| 1222 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1223 | ANativeWindowBuffer* buffer; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1224 | int fence_fd; |
| 1225 | err = window->dequeueBuffer(window, &buffer, &fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1226 | if (err != 0) { |
| 1227 | // TODO(jessehall): Improve error reporting. Can we enumerate possible |
| 1228 | // errors and translate them to valid Vulkan result codes? |
| 1229 | ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1230 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | uint32_t idx; |
| 1234 | for (idx = 0; idx < swapchain.num_images; idx++) { |
| 1235 | if (swapchain.images[idx].buffer.get() == buffer) { |
| 1236 | swapchain.images[idx].dequeued = true; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1237 | swapchain.images[idx].dequeue_fence = fence_fd; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1238 | break; |
| 1239 | } |
| 1240 | } |
| 1241 | if (idx == swapchain.num_images) { |
| 1242 | ALOGE("dequeueBuffer returned unrecognized buffer"); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1243 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1244 | return VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | int fence_clone = -1; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1248 | if (fence_fd != -1) { |
| 1249 | fence_clone = dup(fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1250 | if (fence_clone == -1) { |
| 1251 | ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", |
| 1252 | strerror(errno), errno); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1253 | sync_wait(fence_fd, -1 /* forever */); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1257 | result = GetData(device).driver.AcquireImageANDROID( |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1258 | device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1259 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 1260 | // NOTE: we're relying on AcquireImageANDROID to close fence_clone, |
| 1261 | // even if the call fails. We could close it ourselves on failure, but |
| 1262 | // that would create a race condition if the driver closes it on a |
| 1263 | // failure path: some other thread might create an fd with the same |
| 1264 | // number between the time the driver closes it and the time we close |
| 1265 | // it. We must assume one of: the driver *always* closes it even on |
| 1266 | // failure, or *never* closes it on failure. |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 1267 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1268 | swapchain.images[idx].dequeued = false; |
| 1269 | swapchain.images[idx].dequeue_fence = -1; |
| 1270 | return result; |
| 1271 | } |
| 1272 | |
| 1273 | *image_index = idx; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1274 | return VK_SUCCESS; |
| 1275 | } |
| 1276 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1277 | static VkResult WorstPresentResult(VkResult a, VkResult b) { |
| 1278 | // See the error ranking for vkQueuePresentKHR at the end of section 29.6 |
| 1279 | // (in spec version 1.0.14). |
| 1280 | static const VkResult kWorstToBest[] = { |
| 1281 | VK_ERROR_DEVICE_LOST, |
| 1282 | VK_ERROR_SURFACE_LOST_KHR, |
| 1283 | VK_ERROR_OUT_OF_DATE_KHR, |
| 1284 | VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 1285 | VK_ERROR_OUT_OF_HOST_MEMORY, |
| 1286 | VK_SUBOPTIMAL_KHR, |
| 1287 | }; |
| 1288 | for (auto result : kWorstToBest) { |
| 1289 | if (a == result || b == result) |
| 1290 | return result; |
| 1291 | } |
| 1292 | ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a); |
| 1293 | ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b); |
| 1294 | return a != VK_SUCCESS ? a : b; |
| 1295 | } |
| 1296 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1297 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1298 | VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1299 | ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
| 1300 | "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d", |
| 1301 | present_info->sType); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1302 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1303 | VkDevice device = GetData(queue).driver_device; |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1304 | const auto& dispatch = GetData(queue).driver; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1305 | VkResult final_result = VK_SUCCESS; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1306 | |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1307 | // Look at the pNext chain for supported extension structs: |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1308 | const VkPresentRegionsKHR* present_regions = nullptr; |
| 1309 | const VkPresentTimesInfoGOOGLE* present_times = nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1310 | const VkPresentRegionsKHR* next = |
| 1311 | reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext); |
| 1312 | while (next) { |
| 1313 | switch (next->sType) { |
| 1314 | case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR: |
| 1315 | present_regions = next; |
| 1316 | break; |
Ian Elliott | 14866bb | 2017-01-20 09:15:48 -0700 | [diff] [blame] | 1317 | case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE: |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1318 | present_times = |
| 1319 | reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next); |
| 1320 | break; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1321 | default: |
| 1322 | ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x", |
| 1323 | next->sType); |
| 1324 | break; |
| 1325 | } |
| 1326 | next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext); |
| 1327 | } |
| 1328 | ALOGV_IF( |
| 1329 | present_regions && |
| 1330 | present_regions->swapchainCount != present_info->swapchainCount, |
| 1331 | "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1332 | ALOGV_IF(present_times && |
| 1333 | present_times->swapchainCount != present_info->swapchainCount, |
| 1334 | "VkPresentTimesInfoGOOGLE::swapchainCount != " |
| 1335 | "VkPresentInfo::swapchainCount"); |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1336 | const VkPresentRegionKHR* regions = |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1337 | (present_regions) ? present_regions->pRegions : nullptr; |
| 1338 | const VkPresentTimeGOOGLE* times = |
| 1339 | (present_times) ? present_times->pTimes : nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1340 | const VkAllocationCallbacks* allocator = &GetData(device).allocator; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1341 | android_native_rect_t* rects = nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1342 | uint32_t nrects = 0; |
| 1343 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1344 | for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) { |
| 1345 | Swapchain& swapchain = |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1346 | *SwapchainFromHandle(present_info->pSwapchains[sc]); |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1347 | uint32_t image_idx = present_info->pImageIndices[sc]; |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 1348 | Swapchain::Image& img = swapchain.images[image_idx]; |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 1349 | const VkPresentRegionKHR* region = |
| 1350 | (regions && !swapchain.mailbox_mode) ? ®ions[sc] : nullptr; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1351 | const VkPresentTimeGOOGLE* time = (times) ? ×[sc] : nullptr; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1352 | VkResult swapchain_result = VK_SUCCESS; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1353 | VkResult result; |
| 1354 | int err; |
| 1355 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1356 | int fence = -1; |
Jesse Hall | 275d76c | 2016-01-08 22:39:16 -0800 | [diff] [blame] | 1357 | result = dispatch.QueueSignalReleaseImageANDROID( |
| 1358 | queue, present_info->waitSemaphoreCount, |
| 1359 | present_info->pWaitSemaphores, img.image, &fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1360 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 1361 | ALOGE("QueueSignalReleaseImageANDROID failed: %d", result); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1362 | swapchain_result = result; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1365 | if (swapchain.surface.swapchain_handle == |
| 1366 | present_info->pSwapchains[sc]) { |
| 1367 | ANativeWindow* window = swapchain.surface.window.get(); |
| 1368 | if (swapchain_result == VK_SUCCESS) { |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1369 | if (region) { |
| 1370 | // Process the incremental-present hint for this swapchain: |
| 1371 | uint32_t rcount = region->rectangleCount; |
| 1372 | if (rcount > nrects) { |
| 1373 | android_native_rect_t* new_rects = |
| 1374 | static_cast<android_native_rect_t*>( |
| 1375 | allocator->pfnReallocation( |
| 1376 | allocator->pUserData, rects, |
| 1377 | sizeof(android_native_rect_t) * rcount, |
| 1378 | alignof(android_native_rect_t), |
| 1379 | VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); |
| 1380 | if (new_rects) { |
| 1381 | rects = new_rects; |
| 1382 | nrects = rcount; |
| 1383 | } else { |
| 1384 | rcount = 0; // Ignore the hint for this swapchain |
| 1385 | } |
| 1386 | } |
| 1387 | for (uint32_t r = 0; r < rcount; ++r) { |
| 1388 | if (region->pRectangles[r].layer > 0) { |
| 1389 | ALOGV( |
| 1390 | "vkQueuePresentKHR ignoring invalid layer " |
| 1391 | "(%u); using layer 0 instead", |
| 1392 | region->pRectangles[r].layer); |
| 1393 | } |
| 1394 | int x = region->pRectangles[r].offset.x; |
| 1395 | int y = region->pRectangles[r].offset.y; |
| 1396 | int width = static_cast<int>( |
| 1397 | region->pRectangles[r].extent.width); |
| 1398 | int height = static_cast<int>( |
| 1399 | region->pRectangles[r].extent.height); |
| 1400 | android_native_rect_t* cur_rect = &rects[r]; |
| 1401 | cur_rect->left = x; |
| 1402 | cur_rect->top = y + height; |
| 1403 | cur_rect->right = x + width; |
| 1404 | cur_rect->bottom = y; |
| 1405 | } |
| 1406 | native_window_set_surface_damage(window, rects, rcount); |
| 1407 | } |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1408 | if (time) { |
| 1409 | if (!swapchain.frame_timestamps_enabled) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1410 | ALOGV( |
| 1411 | "Calling " |
| 1412 | "native_window_enable_frame_timestamps(true)"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1413 | native_window_enable_frame_timestamps(window, true); |
| 1414 | swapchain.frame_timestamps_enabled = true; |
| 1415 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1416 | |
| 1417 | // Record the nativeFrameId so it can be later correlated to |
| 1418 | // this present. |
| 1419 | uint64_t nativeFrameId = 0; |
| 1420 | err = native_window_get_next_frame_id( |
| 1421 | window, &nativeFrameId); |
| 1422 | if (err != android::NO_ERROR) { |
| 1423 | ALOGE("Failed to get next native frame ID."); |
| 1424 | } |
| 1425 | |
| 1426 | // Add a new timing record with the user's presentID and |
| 1427 | // the nativeFrameId. |
| 1428 | swapchain.timing.push_back(TimingInfo(time, nativeFrameId)); |
| 1429 | while (swapchain.timing.size() > MAX_TIMING_INFOS) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1430 | swapchain.timing.removeAt(0); |
| 1431 | } |
| 1432 | if (time->desiredPresentTime) { |
| 1433 | // Set the desiredPresentTime: |
| 1434 | ALOGV( |
| 1435 | "Calling " |
| 1436 | "native_window_set_buffers_timestamp(%" PRId64 ")", |
| 1437 | time->desiredPresentTime); |
| 1438 | native_window_set_buffers_timestamp( |
| 1439 | window, |
| 1440 | static_cast<int64_t>(time->desiredPresentTime)); |
| 1441 | } |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1442 | } |
Chris Forbes | fca0f29 | 2017-03-30 19:48:39 +1300 | [diff] [blame] | 1443 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1444 | err = window->queueBuffer(window, img.buffer.get(), fence); |
| 1445 | // queueBuffer always closes fence, even on error |
| 1446 | if (err != 0) { |
| 1447 | // TODO(jessehall): What now? We should probably cancel the |
| 1448 | // buffer, I guess? |
| 1449 | ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err); |
| 1450 | swapchain_result = WorstPresentResult( |
| 1451 | swapchain_result, VK_ERROR_OUT_OF_DATE_KHR); |
| 1452 | } |
| 1453 | if (img.dequeue_fence >= 0) { |
| 1454 | close(img.dequeue_fence); |
| 1455 | img.dequeue_fence = -1; |
| 1456 | } |
| 1457 | img.dequeued = false; |
Chris Forbes | fca0f29 | 2017-03-30 19:48:39 +1300 | [diff] [blame] | 1458 | |
| 1459 | // If the swapchain is in shared mode, immediately dequeue the |
| 1460 | // buffer so it can be presented again without an intervening |
| 1461 | // call to AcquireNextImageKHR. We expect to get the same buffer |
| 1462 | // back from every call to dequeueBuffer in this mode. |
| 1463 | if (swapchain.shared && swapchain_result == VK_SUCCESS) { |
| 1464 | ANativeWindowBuffer* buffer; |
| 1465 | int fence_fd; |
| 1466 | err = window->dequeueBuffer(window, &buffer, &fence_fd); |
| 1467 | if (err != 0) { |
| 1468 | ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err); |
| 1469 | swapchain_result = WorstPresentResult(swapchain_result, |
| 1470 | VK_ERROR_SURFACE_LOST_KHR); |
| 1471 | } |
| 1472 | else if (img.buffer != buffer) { |
| 1473 | ALOGE("got wrong image back for shared swapchain"); |
| 1474 | swapchain_result = WorstPresentResult(swapchain_result, |
| 1475 | VK_ERROR_SURFACE_LOST_KHR); |
| 1476 | } |
| 1477 | else { |
| 1478 | img.dequeue_fence = fence_fd; |
| 1479 | img.dequeued = true; |
| 1480 | } |
| 1481 | } |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1482 | } |
| 1483 | if (swapchain_result != VK_SUCCESS) { |
| 1484 | ReleaseSwapchainImage(device, window, fence, img); |
| 1485 | OrphanSwapchain(device, &swapchain); |
| 1486 | } |
| 1487 | } else { |
| 1488 | ReleaseSwapchainImage(device, nullptr, fence, img); |
| 1489 | swapchain_result = VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 1492 | if (present_info->pResults) |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1493 | present_info->pResults[sc] = swapchain_result; |
| 1494 | |
| 1495 | if (swapchain_result != final_result) |
| 1496 | final_result = WorstPresentResult(final_result, swapchain_result); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1497 | } |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 1498 | if (rects) { |
| 1499 | allocator->pfnFree(allocator->pUserData, rects); |
| 1500 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1501 | |
| 1502 | return final_result; |
| 1503 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1504 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1505 | VKAPI_ATTR |
| 1506 | VkResult GetRefreshCycleDurationGOOGLE( |
| 1507 | VkDevice, |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 1508 | VkSwapchainKHR swapchain_handle, |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1509 | VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) { |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 1510 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1511 | VkResult result = VK_SUCCESS; |
| 1512 | |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 1513 | pDisplayTimingProperties->refreshDuration = |
| 1514 | static_cast<uint64_t>(swapchain.refresh_duration); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1515 | |
| 1516 | return result; |
| 1517 | } |
| 1518 | |
| 1519 | VKAPI_ATTR |
| 1520 | VkResult GetPastPresentationTimingGOOGLE( |
| 1521 | VkDevice, |
| 1522 | VkSwapchainKHR swapchain_handle, |
| 1523 | uint32_t* count, |
| 1524 | VkPastPresentationTimingGOOGLE* timings) { |
| 1525 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
| 1526 | ANativeWindow* window = swapchain.surface.window.get(); |
| 1527 | VkResult result = VK_SUCCESS; |
| 1528 | |
| 1529 | if (!swapchain.frame_timestamps_enabled) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1530 | ALOGV("Calling native_window_enable_frame_timestamps(true)"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1531 | native_window_enable_frame_timestamps(window, true); |
| 1532 | swapchain.frame_timestamps_enabled = true; |
| 1533 | } |
| 1534 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1535 | if (timings) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1536 | // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE) |
| 1537 | copy_ready_timings(swapchain, count, timings); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1538 | } else { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 1539 | *count = get_num_ready_timings(swapchain); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | return result; |
| 1543 | } |
| 1544 | |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 1545 | VKAPI_ATTR |
| 1546 | VkResult GetSwapchainStatusKHR( |
| 1547 | VkDevice, |
Chris Forbes | 4e18ba8 | 2017-01-20 12:50:17 +1300 | [diff] [blame] | 1548 | VkSwapchainKHR swapchain_handle) { |
| 1549 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 1550 | VkResult result = VK_SUCCESS; |
| 1551 | |
Chris Forbes | 4e18ba8 | 2017-01-20 12:50:17 +1300 | [diff] [blame] | 1552 | if (swapchain.surface.swapchain_handle != swapchain_handle) { |
| 1553 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 1554 | } |
| 1555 | |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 1556 | // TODO(chrisforbes): Implement this function properly |
| 1557 | |
| 1558 | return result; |
| 1559 | } |
| 1560 | |
Courtney Goeltzenleuchter | d634c48 | 2017-01-05 15:55:31 -0700 | [diff] [blame] | 1561 | VKAPI_ATTR void SetHdrMetadataEXT( |
| 1562 | VkDevice device, |
| 1563 | uint32_t swapchainCount, |
| 1564 | const VkSwapchainKHR* pSwapchains, |
| 1565 | const VkHdrMetadataEXT* pHdrMetadataEXTs) { |
| 1566 | // TODO: courtneygo: implement actual function |
| 1567 | (void)device; |
| 1568 | (void)swapchainCount; |
| 1569 | (void)pSwapchains; |
| 1570 | (void)pHdrMetadataEXTs; |
| 1571 | return; |
| 1572 | } |
| 1573 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1574 | } // namespace driver |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1575 | } // namespace vulkan |