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