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