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 | |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 19 | #include <aidl/android/hardware/graphics/common/Dataspace.h> |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 20 | #include <aidl/android/hardware/graphics/common/PixelFormat.h> |
Yiwei Zhang | 0f47522 | 2019-04-11 19:38:00 -0700 | [diff] [blame] | 21 | #include <android/hardware/graphics/common/1.0/types.h> |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 22 | #include <android/hardware_buffer.h> |
Jesse Hall | 7992781 | 2017-03-23 11:03:23 -0700 | [diff] [blame] | 23 | #include <grallocusage/GrallocUsageConversion.h> |
Yiwei Zhang | 69395cd | 2019-07-03 16:55:39 -0700 | [diff] [blame] | 24 | #include <graphicsenv/GraphicsEnv.h> |
John Reck | 97678d4 | 2022-08-23 11:24:51 -0400 | [diff] [blame] | 25 | #include <hardware/gralloc.h> |
| 26 | #include <hardware/gralloc1.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 27 | #include <log/log.h> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 28 | #include <sync/sync.h> |
Yiwei Zhang | 0f47522 | 2019-04-11 19:38:00 -0700 | [diff] [blame] | 29 | #include <system/window.h> |
| 30 | #include <ui/BufferQueueDefs.h> |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 31 | #include <utils/StrongPointer.h> |
Yiwei Zhang | 705c2e6 | 2019-12-18 23:12:43 -0800 | [diff] [blame] | 32 | #include <utils/Timers.h> |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Yiwei Zhang | 0f47522 | 2019-04-11 19:38:00 -0700 | [diff] [blame] | 34 | |
| 35 | #include <algorithm> |
| 36 | #include <unordered_set> |
| 37 | #include <vector> |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 38 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 39 | #include "driver.h" |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 40 | |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 41 | using PixelFormat = aidl::android::hardware::graphics::common::PixelFormat; |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 42 | using DataSpace = aidl::android::hardware::graphics::common::Dataspace; |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 43 | using android::hardware::graphics::common::V1_0::BufferUsage; |
| 44 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 45 | namespace vulkan { |
| 46 | namespace driver { |
Jesse Hall | 5ae3abb | 2015-10-08 14:00:22 -0700 | [diff] [blame] | 47 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 48 | namespace { |
| 49 | |
John Reck | 97678d4 | 2022-08-23 11:24:51 -0400 | [diff] [blame] | 50 | static uint64_t convertGralloc1ToBufferUsage(uint64_t producerUsage, |
| 51 | uint64_t consumerUsage) { |
| 52 | static_assert(uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) == |
| 53 | uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN), |
| 54 | "expected ConsumerUsage and ProducerUsage CPU_READ_OFTEN " |
| 55 | "bits to match"); |
| 56 | uint64_t merged = producerUsage | consumerUsage; |
| 57 | if ((merged & (GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN)) == |
| 58 | GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) { |
| 59 | merged &= ~uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN); |
| 60 | merged |= BufferUsage::CPU_READ_OFTEN; |
| 61 | } |
| 62 | if ((merged & (GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) == |
| 63 | GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) { |
| 64 | merged &= ~uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN); |
| 65 | merged |= BufferUsage::CPU_WRITE_OFTEN; |
| 66 | } |
| 67 | return merged; |
| 68 | } |
| 69 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 70 | const VkSurfaceTransformFlagsKHR kSupportedTransforms = |
| 71 | VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR | |
| 72 | VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR | |
| 73 | VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR | |
| 74 | VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR | |
Yiwei Zhang | 70a2196 | 2019-05-31 17:26:52 -0700 | [diff] [blame] | 75 | VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR | |
| 76 | VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR | |
| 77 | VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR | |
| 78 | VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 79 | VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR; |
| 80 | |
| 81 | VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) { |
| 82 | // Native and Vulkan transforms are isomorphic, but are represented |
| 83 | // differently. Vulkan transforms are built up of an optional horizontal |
| 84 | // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native |
| 85 | // transforms are built up from a horizontal flip, vertical flip, and |
| 86 | // 90-degree rotation, all optional but always in that order. |
| 87 | |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 88 | switch (native) { |
Yiwei Zhang | 70a2196 | 2019-05-31 17:26:52 -0700 | [diff] [blame] | 89 | case 0: |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 90 | return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
Yiwei Zhang | 70a2196 | 2019-05-31 17:26:52 -0700 | [diff] [blame] | 91 | case NATIVE_WINDOW_TRANSFORM_FLIP_H: |
| 92 | return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR; |
| 93 | case NATIVE_WINDOW_TRANSFORM_FLIP_V: |
| 94 | return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR; |
| 95 | case NATIVE_WINDOW_TRANSFORM_ROT_180: |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 96 | return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR; |
Yiwei Zhang | 70a2196 | 2019-05-31 17:26:52 -0700 | [diff] [blame] | 97 | case NATIVE_WINDOW_TRANSFORM_ROT_90: |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 98 | return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR; |
Yiwei Zhang | 70a2196 | 2019-05-31 17:26:52 -0700 | [diff] [blame] | 99 | case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90: |
| 100 | return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR; |
| 101 | case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90: |
| 102 | return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR; |
| 103 | case NATIVE_WINDOW_TRANSFORM_ROT_270: |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 104 | return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR; |
| 105 | case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY: |
| 106 | default: |
| 107 | return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 108 | } |
| 109 | } |
| 110 | |
Yiwei Zhang | 70a2196 | 2019-05-31 17:26:52 -0700 | [diff] [blame] | 111 | int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) { |
| 112 | switch (transform) { |
| 113 | case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR: |
| 114 | return NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 115 | case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR: |
| 116 | return NATIVE_WINDOW_TRANSFORM_ROT_180; |
| 117 | case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR: |
| 118 | return NATIVE_WINDOW_TRANSFORM_ROT_270; |
| 119 | case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR: |
| 120 | return NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 121 | case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR: |
| 122 | return NATIVE_WINDOW_TRANSFORM_FLIP_H | |
| 123 | NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 124 | case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR: |
| 125 | return NATIVE_WINDOW_TRANSFORM_FLIP_V; |
| 126 | case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR: |
| 127 | return NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 128 | NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 129 | case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR: |
| 130 | case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR: |
| 131 | default: |
| 132 | return 0; |
| 133 | } |
| 134 | } |
| 135 | |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 136 | int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) { |
| 137 | switch (transform) { |
| 138 | case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR: |
| 139 | return NATIVE_WINDOW_TRANSFORM_ROT_270; |
| 140 | case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR: |
| 141 | return NATIVE_WINDOW_TRANSFORM_ROT_180; |
| 142 | case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR: |
| 143 | return NATIVE_WINDOW_TRANSFORM_ROT_90; |
Yiwei Zhang | 70a2196 | 2019-05-31 17:26:52 -0700 | [diff] [blame] | 144 | case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR: |
| 145 | return NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 146 | case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR: |
| 147 | return NATIVE_WINDOW_TRANSFORM_FLIP_H | |
| 148 | NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 149 | case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR: |
| 150 | return NATIVE_WINDOW_TRANSFORM_FLIP_V; |
| 151 | case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR: |
| 152 | return NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 153 | NATIVE_WINDOW_TRANSFORM_ROT_90; |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 154 | case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR: |
| 155 | case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR: |
| 156 | default: |
| 157 | return 0; |
| 158 | } |
| 159 | } |
| 160 | |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 161 | class TimingInfo { |
| 162 | public: |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 163 | TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId) |
Ian Elliott | 2c6355d | 2017-01-19 11:02:13 -0700 | [diff] [blame] | 164 | : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0}, |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 165 | native_frame_id_(nativeFrameId) {} |
| 166 | bool ready() const { |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 167 | return (timestamp_desired_present_time_ != |
| 168 | NATIVE_WINDOW_TIMESTAMP_PENDING && |
| 169 | timestamp_actual_present_time_ != |
| 170 | NATIVE_WINDOW_TIMESTAMP_PENDING && |
| 171 | timestamp_render_complete_time_ != |
| 172 | NATIVE_WINDOW_TIMESTAMP_PENDING && |
| 173 | timestamp_composition_latch_time_ != |
| 174 | NATIVE_WINDOW_TIMESTAMP_PENDING); |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 175 | } |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 176 | void calculate(int64_t rdur) { |
| 177 | bool anyTimestampInvalid = |
| 178 | (timestamp_actual_present_time_ == |
| 179 | NATIVE_WINDOW_TIMESTAMP_INVALID) || |
| 180 | (timestamp_render_complete_time_ == |
| 181 | NATIVE_WINDOW_TIMESTAMP_INVALID) || |
| 182 | (timestamp_composition_latch_time_ == |
| 183 | NATIVE_WINDOW_TIMESTAMP_INVALID); |
| 184 | if (anyTimestampInvalid) { |
| 185 | ALOGE("Unexpectedly received invalid timestamp."); |
| 186 | vals_.actualPresentTime = 0; |
| 187 | vals_.earliestPresentTime = 0; |
| 188 | vals_.presentMargin = 0; |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | vals_.actualPresentTime = |
| 193 | static_cast<uint64_t>(timestamp_actual_present_time_); |
| 194 | int64_t margin = (timestamp_composition_latch_time_ - |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 195 | timestamp_render_complete_time_); |
| 196 | // Calculate vals_.earliestPresentTime, and potentially adjust |
| 197 | // vals_.presentMargin. The initial value of vals_.earliestPresentTime |
| 198 | // is vals_.actualPresentTime. If we can subtract rdur (the duration |
| 199 | // of a refresh cycle) from vals_.earliestPresentTime (and also from |
| 200 | // vals_.presentMargin) and still leave a positive margin, then we can |
| 201 | // report to the application that it could have presented earlier than |
| 202 | // it did (per the extension specification). If for some reason, we |
| 203 | // can do this subtraction repeatedly, we do, since |
| 204 | // vals_.earliestPresentTime really is supposed to be the "earliest". |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 205 | int64_t early_time = timestamp_actual_present_time_; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 206 | while ((margin > rdur) && |
| 207 | ((early_time - rdur) > timestamp_composition_latch_time_)) { |
| 208 | early_time -= rdur; |
| 209 | margin -= rdur; |
| 210 | } |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 211 | vals_.earliestPresentTime = static_cast<uint64_t>(early_time); |
| 212 | vals_.presentMargin = static_cast<uint64_t>(margin); |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 213 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 214 | void get_values(VkPastPresentationTimingGOOGLE* values) const { |
| 215 | *values = vals_; |
| 216 | } |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 217 | |
| 218 | public: |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 219 | VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 }; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 220 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 221 | uint64_t native_frame_id_ { 0 }; |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 222 | int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING }; |
| 223 | int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING }; |
| 224 | int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING }; |
| 225 | int64_t timestamp_composition_latch_time_ |
| 226 | { NATIVE_WINDOW_TIMESTAMP_PENDING }; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 229 | struct Surface { |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 230 | android::sp<ANativeWindow> window; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 231 | VkSwapchainKHR swapchain_handle; |
Yiwei Zhang | 6435b32 | 2018-05-08 11:12:17 -0700 | [diff] [blame] | 232 | uint64_t consumer_usage; |
Yiwei Zhang | 3b88f31 | 2023-04-18 23:11:35 +0000 | [diff] [blame] | 233 | |
| 234 | // Indicate whether this surface has been used by a swapchain, no matter the |
| 235 | // swapchain is still current or has been destroyed. |
| 236 | bool used_by_swapchain; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | VkSurfaceKHR HandleFromSurface(Surface* surface) { |
| 240 | return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface)); |
| 241 | } |
| 242 | |
| 243 | Surface* SurfaceFromHandle(VkSurfaceKHR handle) { |
Jesse Hall | a3a7a1d | 2015-11-24 11:37:23 -0800 | [diff] [blame] | 244 | return reinterpret_cast<Surface*>(handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 247 | // Maximum number of TimingInfo structs to keep per swapchain: |
| 248 | enum { MAX_TIMING_INFOS = 10 }; |
| 249 | // Minimum number of frames to look for in the past (so we don't cause |
| 250 | // syncronous requests to Surface Flinger): |
| 251 | enum { MIN_NUM_FRAMES_AGO = 5 }; |
| 252 | |
Chris Forbes | 4cd01fb | 2022-10-19 11:35:16 +1300 | [diff] [blame] | 253 | bool IsSharedPresentMode(VkPresentModeKHR mode) { |
| 254 | return mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR || |
| 255 | mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR; |
| 256 | } |
| 257 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 258 | struct Swapchain { |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 259 | Swapchain(Surface& surface_, |
| 260 | uint32_t num_images_, |
silence_dogood | 7359759 | 2019-05-23 16:57:37 -0700 | [diff] [blame] | 261 | VkPresentModeKHR present_mode, |
Alina Kalyakina | 3d6f363 | 2023-03-22 17:13:47 +0000 | [diff] [blame] | 262 | int pre_transform_, |
| 263 | int64_t refresh_duration_) |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 264 | : surface(surface_), |
| 265 | num_images(num_images_), |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 266 | mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR), |
silence_dogood | 7359759 | 2019-05-23 16:57:37 -0700 | [diff] [blame] | 267 | pre_transform(pre_transform_), |
Chris Forbes | f883564 | 2017-03-30 19:31:40 +1300 | [diff] [blame] | 268 | frame_timestamps_enabled(false), |
Alina Kalyakina | 3d6f363 | 2023-03-22 17:13:47 +0000 | [diff] [blame] | 269 | refresh_duration(refresh_duration_), |
Yiwei Zhang | 705c2e6 | 2019-12-18 23:12:43 -0800 | [diff] [blame] | 270 | acquire_next_image_timeout(-1), |
Chris Forbes | 4cd01fb | 2022-10-19 11:35:16 +1300 | [diff] [blame] | 271 | shared(IsSharedPresentMode(present_mode)) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 272 | } |
Alina Kalyakina | 3d6f363 | 2023-03-22 17:13:47 +0000 | [diff] [blame] | 273 | |
| 274 | VkResult get_refresh_duration(uint64_t& outRefreshDuration) |
Ian Elliott | 3568bfe | 2019-05-03 15:54:46 -0600 | [diff] [blame] | 275 | { |
| 276 | ANativeWindow* window = surface.window.get(); |
Alina Kalyakina | 3d6f363 | 2023-03-22 17:13:47 +0000 | [diff] [blame] | 277 | int err = native_window_get_refresh_cycle_duration( |
Ian Elliott | 3568bfe | 2019-05-03 15:54:46 -0600 | [diff] [blame] | 278 | window, |
| 279 | &refresh_duration); |
Alina Kalyakina | 3d6f363 | 2023-03-22 17:13:47 +0000 | [diff] [blame] | 280 | if (err != android::OK) { |
| 281 | ALOGE("%s:native_window_get_refresh_cycle_duration failed: %s (%d)", |
| 282 | __func__, strerror(-err), err ); |
| 283 | return VK_ERROR_SURFACE_LOST_KHR; |
| 284 | } |
| 285 | outRefreshDuration = refresh_duration; |
| 286 | return VK_SUCCESS; |
Ian Elliott | 3568bfe | 2019-05-03 15:54:46 -0600 | [diff] [blame] | 287 | } |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 288 | |
| 289 | Surface& surface; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 290 | uint32_t num_images; |
Ian Elliott | ffedb65 | 2017-02-14 10:58:30 -0700 | [diff] [blame] | 291 | bool mailbox_mode; |
silence_dogood | 7359759 | 2019-05-23 16:57:37 -0700 | [diff] [blame] | 292 | int pre_transform; |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 293 | bool frame_timestamps_enabled; |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 294 | int64_t refresh_duration; |
Yiwei Zhang | 705c2e6 | 2019-12-18 23:12:43 -0800 | [diff] [blame] | 295 | nsecs_t acquire_next_image_timeout; |
Chris Forbes | f883564 | 2017-03-30 19:31:40 +1300 | [diff] [blame] | 296 | bool shared; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 297 | |
| 298 | struct Image { |
Yiwei Zhang | 9df5bff | 2020-09-22 10:08:03 -0700 | [diff] [blame] | 299 | Image() |
| 300 | : image(VK_NULL_HANDLE), |
| 301 | dequeue_fence(-1), |
| 302 | release_fence(-1), |
| 303 | dequeued(false) {} |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 304 | VkImage image; |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 305 | // If the image is bound to memory, an sp to the underlying gralloc buffer. |
| 306 | // Otherwise, nullptr; the image will be bound to memory as part of |
| 307 | // AcquireNextImage. |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 308 | android::sp<ANativeWindowBuffer> buffer; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 309 | // The fence is only valid when the buffer is dequeued, and should be |
| 310 | // -1 any other time. When valid, we own the fd, and must ensure it is |
| 311 | // closed: either by closing it explicitly when queueing the buffer, |
| 312 | // or by passing ownership e.g. to ANativeWindow::cancelBuffer(). |
| 313 | int dequeue_fence; |
Yiwei Zhang | 9df5bff | 2020-09-22 10:08:03 -0700 | [diff] [blame] | 314 | // This fence is a dup of the sync fd returned from the driver via |
| 315 | // vkQueueSignalReleaseImageANDROID upon vkQueuePresentKHR. We must |
| 316 | // ensure it is closed upon re-presenting or releasing the image. |
| 317 | int release_fence; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 318 | bool dequeued; |
Pawin Vongmasa | 6e1193a | 2017-03-07 13:08:40 -0800 | [diff] [blame] | 319 | } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS]; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 320 | |
Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 321 | std::vector<TimingInfo> timing; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) { |
| 325 | return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain)); |
| 326 | } |
| 327 | |
| 328 | Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) { |
Jesse Hall | a3a7a1d | 2015-11-24 11:37:23 -0800 | [diff] [blame] | 329 | return reinterpret_cast<Swapchain*>(handle); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Yiwei Zhang | 9df5bff | 2020-09-22 10:08:03 -0700 | [diff] [blame] | 332 | static bool IsFencePending(int fd) { |
| 333 | if (fd < 0) |
| 334 | return false; |
| 335 | |
| 336 | errno = 0; |
| 337 | return sync_wait(fd, 0 /* timeout */) == -1 && errno == ETIME; |
| 338 | } |
| 339 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 340 | void ReleaseSwapchainImage(VkDevice device, |
Yiwei Zhang | ac1f098 | 2022-04-09 07:10:10 +0000 | [diff] [blame] | 341 | bool shared_present, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 342 | ANativeWindow* window, |
| 343 | int release_fence, |
Yiwei Zhang | 9df5bff | 2020-09-22 10:08:03 -0700 | [diff] [blame] | 344 | Swapchain::Image& image, |
| 345 | bool defer_if_pending) { |
Yiwei Zhang | 533cea9 | 2019-06-03 18:43:24 -0700 | [diff] [blame] | 346 | ATRACE_CALL(); |
| 347 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 348 | ALOG_ASSERT(release_fence == -1 || image.dequeued, |
| 349 | "ReleaseSwapchainImage: can't provide a release fence for " |
| 350 | "non-dequeued images"); |
| 351 | |
| 352 | if (image.dequeued) { |
| 353 | if (release_fence >= 0) { |
| 354 | // We get here from vkQueuePresentKHR. The application is |
| 355 | // responsible for creating an execution dependency chain from |
| 356 | // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR |
| 357 | // (release_fence), so we can drop the dequeue_fence here. |
| 358 | if (image.dequeue_fence >= 0) |
| 359 | close(image.dequeue_fence); |
| 360 | } else { |
| 361 | // We get here during swapchain destruction, or various serious |
| 362 | // error cases e.g. when we can't create the release_fence during |
| 363 | // vkQueuePresentKHR. In non-error cases, the dequeue_fence should |
| 364 | // have already signalled, since the swapchain images are supposed |
| 365 | // to be idle before the swapchain is destroyed. In error cases, |
| 366 | // there may be rendering in flight to the image, but since we |
| 367 | // weren't able to create a release_fence, waiting for the |
| 368 | // dequeue_fence is about the best we can do. |
| 369 | release_fence = image.dequeue_fence; |
| 370 | } |
| 371 | image.dequeue_fence = -1; |
| 372 | |
Yiwei Zhang | ac1f098 | 2022-04-09 07:10:10 +0000 | [diff] [blame] | 373 | // It's invalid to call cancelBuffer on a shared buffer |
| 374 | if (window && !shared_present) { |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 375 | window->cancelBuffer(window, image.buffer.get(), release_fence); |
| 376 | } else { |
| 377 | if (release_fence >= 0) { |
| 378 | sync_wait(release_fence, -1 /* forever */); |
| 379 | close(release_fence); |
| 380 | } |
| 381 | } |
Yiwei Zhang | 9df5bff | 2020-09-22 10:08:03 -0700 | [diff] [blame] | 382 | release_fence = -1; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 383 | image.dequeued = false; |
| 384 | } |
| 385 | |
Yiwei Zhang | 9df5bff | 2020-09-22 10:08:03 -0700 | [diff] [blame] | 386 | if (defer_if_pending && IsFencePending(image.release_fence)) |
| 387 | return; |
| 388 | |
| 389 | if (image.release_fence >= 0) { |
| 390 | close(image.release_fence); |
| 391 | image.release_fence = -1; |
| 392 | } |
| 393 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 394 | if (image.image) { |
Yiwei Zhang | 533cea9 | 2019-06-03 18:43:24 -0700 | [diff] [blame] | 395 | ATRACE_BEGIN("DestroyImage"); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 396 | GetData(device).driver.DestroyImage(device, image.image, nullptr); |
Yiwei Zhang | 533cea9 | 2019-06-03 18:43:24 -0700 | [diff] [blame] | 397 | ATRACE_END(); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 398 | image.image = VK_NULL_HANDLE; |
| 399 | } |
| 400 | |
| 401 | image.buffer.clear(); |
| 402 | } |
| 403 | |
| 404 | void OrphanSwapchain(VkDevice device, Swapchain* swapchain) { |
| 405 | if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain)) |
| 406 | return; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 407 | for (uint32_t i = 0; i < swapchain->num_images; i++) { |
Yiwei Zhang | ac1f098 | 2022-04-09 07:10:10 +0000 | [diff] [blame] | 408 | if (!swapchain->images[i].dequeued) { |
| 409 | ReleaseSwapchainImage(device, swapchain->shared, nullptr, -1, |
| 410 | swapchain->images[i], true); |
| 411 | } |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 412 | } |
| 413 | swapchain->surface.swapchain_handle = VK_NULL_HANDLE; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 414 | swapchain->timing.clear(); |
| 415 | } |
| 416 | |
| 417 | uint32_t get_num_ready_timings(Swapchain& swapchain) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 418 | if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) { |
| 419 | return 0; |
| 420 | } |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 421 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 422 | uint32_t num_ready = 0; |
| 423 | const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1; |
| 424 | for (uint32_t i = 0; i < num_timings; i++) { |
Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 425 | TimingInfo& ti = swapchain.timing[i]; |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 426 | if (ti.ready()) { |
| 427 | // This TimingInfo is ready to be reported to the user. Add it |
| 428 | // to the num_ready. |
| 429 | num_ready++; |
| 430 | continue; |
| 431 | } |
| 432 | // This TimingInfo is not yet ready to be reported to the user, |
| 433 | // and so we should look for any available timestamps that |
| 434 | // might make it ready. |
| 435 | int64_t desired_present_time = 0; |
| 436 | int64_t render_complete_time = 0; |
| 437 | int64_t composition_latch_time = 0; |
| 438 | int64_t actual_present_time = 0; |
| 439 | // Obtain timestamps: |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 440 | int err = native_window_get_frame_timestamps( |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 441 | swapchain.surface.window.get(), ti.native_frame_id_, |
| 442 | &desired_present_time, &render_complete_time, |
| 443 | &composition_latch_time, |
Yi Kong | bcbc73a | 2018-07-18 10:13:04 -0700 | [diff] [blame] | 444 | nullptr, //&first_composition_start_time, |
| 445 | nullptr, //&last_composition_start_time, |
| 446 | nullptr, //&composition_finish_time, |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 447 | &actual_present_time, |
Yi Kong | bcbc73a | 2018-07-18 10:13:04 -0700 | [diff] [blame] | 448 | nullptr, //&dequeue_ready_time, |
| 449 | nullptr /*&reads_done_time*/); |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 450 | |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 451 | if (err != android::OK) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 452 | continue; |
| 453 | } |
| 454 | |
| 455 | // Record the timestamp(s) we received, and then see if this TimingInfo |
| 456 | // is ready to be reported to the user: |
Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 457 | ti.timestamp_desired_present_time_ = desired_present_time; |
| 458 | ti.timestamp_actual_present_time_ = actual_present_time; |
| 459 | ti.timestamp_render_complete_time_ = render_complete_time; |
| 460 | ti.timestamp_composition_latch_time_ = composition_latch_time; |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 461 | |
| 462 | if (ti.ready()) { |
| 463 | // The TimingInfo has received enough timestamps, and should now |
| 464 | // use those timestamps to calculate the info that should be |
| 465 | // reported to the user: |
| 466 | ti.calculate(swapchain.refresh_duration); |
| 467 | num_ready++; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | return num_ready; |
| 471 | } |
| 472 | |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 473 | void copy_ready_timings(Swapchain& swapchain, |
| 474 | uint32_t* count, |
| 475 | VkPastPresentationTimingGOOGLE* timings) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 476 | if (swapchain.timing.empty()) { |
| 477 | *count = 0; |
| 478 | return; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 479 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 480 | |
| 481 | size_t last_ready = swapchain.timing.size() - 1; |
| 482 | while (!swapchain.timing[last_ready].ready()) { |
| 483 | if (last_ready == 0) { |
| 484 | *count = 0; |
| 485 | return; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 486 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 487 | last_ready--; |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 488 | } |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 489 | |
| 490 | uint32_t num_copied = 0; |
Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 491 | int32_t num_to_remove = 0; |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 492 | for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) { |
| 493 | const TimingInfo& ti = swapchain.timing[i]; |
| 494 | if (ti.ready()) { |
| 495 | ti.get_values(&timings[num_copied]); |
| 496 | num_copied++; |
| 497 | } |
| 498 | num_to_remove++; |
| 499 | } |
| 500 | |
| 501 | // Discard old frames that aren't ready if newer frames are ready. |
| 502 | // We don't expect to get the timing info for those old frames. |
Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 503 | swapchain.timing.erase(swapchain.timing.begin(), |
| 504 | swapchain.timing.begin() + num_to_remove); |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 505 | |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 506 | *count = num_copied; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 509 | PixelFormat GetNativePixelFormat(VkFormat format) { |
| 510 | PixelFormat native_format = PixelFormat::RGBA_8888; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 511 | switch (format) { |
| 512 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 513 | case VK_FORMAT_R8G8B8A8_SRGB: |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 514 | native_format = PixelFormat::RGBA_8888; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 515 | break; |
| 516 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 517 | native_format = PixelFormat::RGB_565; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 518 | break; |
| 519 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 520 | native_format = PixelFormat::RGBA_FP16; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 521 | break; |
Yiwei Zhang | c1ea815 | 2019-02-05 15:11:32 -0800 | [diff] [blame] | 522 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 523 | native_format = PixelFormat::RGBA_1010102; |
Leon Scroggins III | cb45fe7 | 2021-11-30 16:17:15 -0500 | [diff] [blame] | 524 | break; |
| 525 | case VK_FORMAT_R8_UNORM: |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 526 | native_format = PixelFormat::R_8; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 527 | break; |
Trevor David Black | 9cfe1ed | 2022-12-05 20:04:57 +0000 | [diff] [blame] | 528 | case VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16: |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 529 | native_format = PixelFormat::RGBA_10101010; |
Trevor David Black | 9cfe1ed | 2022-12-05 20:04:57 +0000 | [diff] [blame] | 530 | break; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 531 | default: |
| 532 | ALOGV("unsupported swapchain format %d", format); |
| 533 | break; |
| 534 | } |
| 535 | return native_format; |
| 536 | } |
| 537 | |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 538 | DataSpace GetNativeDataspace(VkColorSpaceKHR colorspace, |
| 539 | PixelFormat pixelFormat) { |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 540 | switch (colorspace) { |
| 541 | case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 542 | return DataSpace::SRGB; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 543 | case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 544 | return DataSpace::DISPLAY_P3; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 545 | case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 546 | return DataSpace::SCRGB_LINEAR; |
Courtney Goeltzenleuchter | b52abee | 2017-08-07 17:13:04 -0600 | [diff] [blame] | 547 | case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 548 | return DataSpace::SCRGB; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 549 | case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 550 | return DataSpace::DCI_P3_LINEAR; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 551 | case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 552 | return DataSpace::DCI_P3; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 553 | case VK_COLOR_SPACE_BT709_LINEAR_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 554 | return DataSpace::SRGB_LINEAR; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 555 | case VK_COLOR_SPACE_BT709_NONLINEAR_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 556 | return DataSpace::SRGB; |
Courtney Goeltzenleuchter | c45673f | 2017-03-13 15:58:15 -0600 | [diff] [blame] | 557 | case VK_COLOR_SPACE_BT2020_LINEAR_EXT: |
Sally Qi | aa23897 | 2023-07-17 12:52:05 +0800 | [diff] [blame] | 558 | if (pixelFormat == PixelFormat::RGBA_FP16) { |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 559 | return DataSpace::BT2020_LINEAR_EXTENDED; |
Sally Qi | aa23897 | 2023-07-17 12:52:05 +0800 | [diff] [blame] | 560 | } else { |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 561 | return DataSpace::BT2020_LINEAR; |
Sally Qi | aa23897 | 2023-07-17 12:52:05 +0800 | [diff] [blame] | 562 | } |
Courtney Goeltzenleuchter | c45673f | 2017-03-13 15:58:15 -0600 | [diff] [blame] | 563 | case VK_COLOR_SPACE_HDR10_ST2084_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 564 | return DataSpace::BT2020_PQ; |
Courtney Goeltzenleuchter | c45673f | 2017-03-13 15:58:15 -0600 | [diff] [blame] | 565 | case VK_COLOR_SPACE_DOLBYVISION_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 566 | return DataSpace::BT2020_PQ; |
Courtney Goeltzenleuchter | c45673f | 2017-03-13 15:58:15 -0600 | [diff] [blame] | 567 | case VK_COLOR_SPACE_HDR10_HLG_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 568 | return DataSpace::BT2020_HLG; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 569 | case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 570 | return DataSpace::ADOBE_RGB_LINEAR; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 571 | case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 572 | return DataSpace::ADOBE_RGB; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 573 | // Pass through is intended to allow app to provide data that is passed |
| 574 | // to the display system without modification. |
| 575 | case VK_COLOR_SPACE_PASS_THROUGH_EXT: |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 576 | return DataSpace::ARBITRARY; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 577 | |
| 578 | default: |
| 579 | // This indicates that we don't know about the |
| 580 | // dataspace specified and we should indicate that |
| 581 | // it's unsupported |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 582 | return DataSpace::UNKNOWN; |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 586 | } // anonymous namespace |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 587 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 588 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 589 | VkResult CreateAndroidSurfaceKHR( |
Jesse Hall | f9fa9a5 | 2016-01-08 16:08:51 -0800 | [diff] [blame] | 590 | VkInstance instance, |
| 591 | const VkAndroidSurfaceCreateInfoKHR* pCreateInfo, |
| 592 | const VkAllocationCallbacks* allocator, |
| 593 | VkSurfaceKHR* out_surface) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 594 | ATRACE_CALL(); |
| 595 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 596 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 597 | allocator = &GetData(instance).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 598 | void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface), |
| 599 | alignof(Surface), |
| 600 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 601 | if (!mem) |
| 602 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 603 | Surface* surface = new (mem) Surface; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 604 | |
Chia-I Wu | e8e689f | 2016-04-18 08:21:31 +0800 | [diff] [blame] | 605 | surface->window = pCreateInfo->window; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 606 | surface->swapchain_handle = VK_NULL_HANDLE; |
Yiwei Zhang | 3b88f31 | 2023-04-18 23:11:35 +0000 | [diff] [blame] | 607 | surface->used_by_swapchain = false; |
Yiwei Zhang | 6435b32 | 2018-05-08 11:12:17 -0700 | [diff] [blame] | 608 | int err = native_window_get_consumer_usage(surface->window.get(), |
| 609 | &surface->consumer_usage); |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 610 | if (err != android::OK) { |
Yiwei Zhang | 6435b32 | 2018-05-08 11:12:17 -0700 | [diff] [blame] | 611 | ALOGE("native_window_get_consumer_usage() failed: %s (%d)", |
| 612 | strerror(-err), err); |
| 613 | surface->~Surface(); |
| 614 | allocator->pfnFree(allocator->pUserData, surface); |
Yiwei Zhang | 70a2196 | 2019-05-31 17:26:52 -0700 | [diff] [blame] | 615 | return VK_ERROR_SURFACE_LOST_KHR; |
Yiwei Zhang | 6435b32 | 2018-05-08 11:12:17 -0700 | [diff] [blame] | 616 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 617 | |
Yiwei Zhang | 6435b32 | 2018-05-08 11:12:17 -0700 | [diff] [blame] | 618 | err = |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 619 | native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 620 | if (err != android::OK) { |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 621 | ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err), |
| 622 | err); |
| 623 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 624 | allocator->pfnFree(allocator->pUserData, surface); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 625 | return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 626 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 627 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 628 | *out_surface = HandleFromSurface(surface); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 629 | return VK_SUCCESS; |
| 630 | } |
| 631 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 632 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 633 | void DestroySurfaceKHR(VkInstance instance, |
| 634 | VkSurfaceKHR surface_handle, |
| 635 | const VkAllocationCallbacks* allocator) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 636 | ATRACE_CALL(); |
| 637 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 638 | Surface* surface = SurfaceFromHandle(surface_handle); |
| 639 | if (!surface) |
| 640 | return; |
| 641 | native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 642 | ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 643 | "destroyed VkSurfaceKHR 0x%" PRIx64 |
| 644 | " has active VkSwapchainKHR 0x%" PRIx64, |
| 645 | reinterpret_cast<uint64_t>(surface_handle), |
| 646 | reinterpret_cast<uint64_t>(surface->swapchain_handle)); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 647 | surface->~Surface(); |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 648 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 649 | allocator = &GetData(instance).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 650 | allocator->pfnFree(allocator->pUserData, surface); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 653 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 654 | VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/, |
| 655 | uint32_t /*queue_family*/, |
Yiwei Zhang | c7e46c4 | 2021-02-04 22:53:59 +0000 | [diff] [blame] | 656 | VkSurfaceKHR /*surface_handle*/, |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 657 | VkBool32* supported) { |
Yiwei Zhang | c7e46c4 | 2021-02-04 22:53:59 +0000 | [diff] [blame] | 658 | *supported = VK_TRUE; |
Jesse Hall | a642925 | 2015-11-29 18:59:42 -0800 | [diff] [blame] | 659 | return VK_SUCCESS; |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 660 | } |
| 661 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 662 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 663 | VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR( |
Ian Elliott | 1ce053f | 2022-03-16 09:49:53 -0600 | [diff] [blame] | 664 | VkPhysicalDevice pdev, |
Jesse Hall | b00daad | 2015-11-29 19:46:20 -0800 | [diff] [blame] | 665 | VkSurfaceKHR surface, |
| 666 | VkSurfaceCapabilitiesKHR* capabilities) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 667 | ATRACE_CALL(); |
| 668 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 669 | // Implement in terms of GetPhysicalDeviceSurfaceCapabilities2KHR |
| 670 | |
| 671 | VkPhysicalDeviceSurfaceInfo2KHR info2 = { |
| 672 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR, |
| 673 | nullptr, |
| 674 | surface |
| 675 | }; |
| 676 | |
| 677 | VkSurfaceCapabilities2KHR caps2 = { |
| 678 | VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR, |
| 679 | nullptr, |
| 680 | {}, |
| 681 | }; |
| 682 | |
| 683 | VkResult result = GetPhysicalDeviceSurfaceCapabilities2KHR(pdev, &info2, &caps2); |
| 684 | *capabilities = caps2.surfaceCapabilities; |
| 685 | return result; |
| 686 | } |
| 687 | |
| 688 | // Does the call-twice and VK_INCOMPLETE handling for querying lists |
| 689 | // of things, where we already have the full set built in a vector. |
| 690 | template <typename T> |
| 691 | VkResult CopyWithIncomplete(std::vector<T> const& things, |
| 692 | T* callerPtr, uint32_t* callerCount) { |
| 693 | VkResult result = VK_SUCCESS; |
| 694 | if (callerPtr) { |
| 695 | if (things.size() > *callerCount) |
| 696 | result = VK_INCOMPLETE; |
| 697 | *callerCount = std::min(uint32_t(things.size()), *callerCount); |
| 698 | std::copy(things.begin(), things.begin() + *callerCount, callerPtr); |
Ian Elliott | 1ce053f | 2022-03-16 09:49:53 -0600 | [diff] [blame] | 699 | } else { |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 700 | *callerCount = things.size(); |
Yiwei Zhang | dbd9615 | 2018-02-08 14:22:53 -0800 | [diff] [blame] | 701 | } |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 702 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 705 | VKAPI_ATTR |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 706 | VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev, |
| 707 | VkSurfaceKHR surface_handle, |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 708 | uint32_t* count, |
| 709 | VkSurfaceFormatKHR* formats) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 710 | ATRACE_CALL(); |
| 711 | |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 712 | const InstanceData& instance_data = GetData(pdev); |
| 713 | |
Ian Elliott | 1ce053f | 2022-03-16 09:49:53 -0600 | [diff] [blame] | 714 | uint64_t consumer_usage = 0; |
Trevor David Black | 5e13d0c | 2022-03-10 21:18:35 +0000 | [diff] [blame] | 715 | bool colorspace_ext = |
Ian Elliott | f6df08e | 2022-03-16 21:27:49 -0600 | [diff] [blame] | 716 | instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace); |
Ian Elliott | 1ce053f | 2022-03-16 09:49:53 -0600 | [diff] [blame] | 717 | if (surface_handle == VK_NULL_HANDLE) { |
| 718 | ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query; |
| 719 | bool surfaceless_enabled = |
| 720 | instance_data.hook_extensions.test(surfaceless); |
| 721 | if (!surfaceless_enabled) { |
| 722 | return VK_ERROR_SURFACE_LOST_KHR; |
| 723 | } |
Chris Forbes | b1de3b1 | 2022-10-20 09:05:48 +1300 | [diff] [blame] | 724 | // Support for VK_GOOGLE_surfaceless_query. |
Ian Elliott | 1ce053f | 2022-03-16 09:49:53 -0600 | [diff] [blame] | 725 | |
| 726 | // TODO(b/203826952): research proper value; temporarily use the |
| 727 | // values seen on Pixel |
| 728 | consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY; |
| 729 | } else { |
| 730 | Surface& surface = *SurfaceFromHandle(surface_handle); |
Ian Elliott | 1ce053f | 2022-03-16 09:49:53 -0600 | [diff] [blame] | 731 | consumer_usage = surface.consumer_usage; |
Ian Elliott | 6ba85d9 | 2022-02-18 16:44:58 -0700 | [diff] [blame] | 732 | } |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 733 | |
Charlie Lao | 324191f | 2019-12-13 11:03:16 -0800 | [diff] [blame] | 734 | AHardwareBuffer_Desc desc = {}; |
| 735 | desc.width = 1; |
| 736 | desc.height = 1; |
| 737 | desc.layers = 1; |
Ian Elliott | 1ce053f | 2022-03-16 09:49:53 -0600 | [diff] [blame] | 738 | desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | |
Charlie Lao | 324191f | 2019-12-13 11:03:16 -0800 | [diff] [blame] | 739 | AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER; |
| 740 | |
| 741 | // We must support R8G8B8A8 |
| 742 | std::vector<VkSurfaceFormatKHR> all_formats = { |
| 743 | {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
Yiwei Zhang | 0413cc0 | 2022-07-27 04:54:48 +0000 | [diff] [blame] | 744 | {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}, |
Yiwei Zhang | 0413cc0 | 2022-07-27 04:54:48 +0000 | [diff] [blame] | 745 | }; |
Charlie Lao | 324191f | 2019-12-13 11:03:16 -0800 | [diff] [blame] | 746 | |
Trevor David Black | 5e13d0c | 2022-03-10 21:18:35 +0000 | [diff] [blame] | 747 | if (colorspace_ext) { |
| 748 | all_formats.emplace_back(VkSurfaceFormatKHR{ |
Jason Macnak | ca50633 | 2022-11-09 11:00:36 -0800 | [diff] [blame] | 749 | VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT}); |
| 750 | all_formats.emplace_back(VkSurfaceFormatKHR{ |
| 751 | VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_PASS_THROUGH_EXT}); |
| 752 | all_formats.emplace_back(VkSurfaceFormatKHR{ |
Trevor David Black | 5e13d0c | 2022-03-10 21:18:35 +0000 | [diff] [blame] | 753 | VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_BT709_LINEAR_EXT}); |
Charlie Lao | 324191f | 2019-12-13 11:03:16 -0800 | [diff] [blame] | 754 | all_formats.emplace_back(VkSurfaceFormatKHR{ |
| 755 | VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT}); |
| 756 | all_formats.emplace_back(VkSurfaceFormatKHR{ |
| 757 | VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT}); |
| 758 | } |
| 759 | |
Ian Elliott | 1ce053f | 2022-03-16 09:49:53 -0600 | [diff] [blame] | 760 | // NOTE: Any new formats that are added must be coordinated across different |
| 761 | // Android users. This includes the ANGLE team (a layered implementation of |
| 762 | // OpenGL-ES). |
| 763 | |
Charlie Lao | 324191f | 2019-12-13 11:03:16 -0800 | [diff] [blame] | 764 | desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM; |
| 765 | if (AHardwareBuffer_isSupported(&desc)) { |
| 766 | all_formats.emplace_back(VkSurfaceFormatKHR{ |
| 767 | VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}); |
Jason Macnak | ca50633 | 2022-11-09 11:00:36 -0800 | [diff] [blame] | 768 | if (colorspace_ext) { |
| 769 | all_formats.emplace_back( |
| 770 | VkSurfaceFormatKHR{VK_FORMAT_R5G6B5_UNORM_PACK16, |
| 771 | VK_COLOR_SPACE_PASS_THROUGH_EXT}); |
| 772 | } |
Charlie Lao | 324191f | 2019-12-13 11:03:16 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT; |
| 776 | if (AHardwareBuffer_isSupported(&desc)) { |
| 777 | all_formats.emplace_back(VkSurfaceFormatKHR{ |
| 778 | VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}); |
Jason Macnak | ca50633 | 2022-11-09 11:00:36 -0800 | [diff] [blame] | 779 | if (colorspace_ext) { |
| 780 | all_formats.emplace_back( |
| 781 | VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT, |
| 782 | VK_COLOR_SPACE_PASS_THROUGH_EXT}); |
Charlie Lao | 324191f | 2019-12-13 11:03:16 -0800 | [diff] [blame] | 783 | all_formats.emplace_back( |
| 784 | VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT, |
| 785 | VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT}); |
| 786 | all_formats.emplace_back( |
| 787 | VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT, |
| 788 | VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT}); |
Yuxin Hu | d3ff521 | 2024-02-06 02:39:19 +0000 | [diff] [blame^] | 789 | all_formats.emplace_back( |
| 790 | VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT, |
| 791 | VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT}); |
Charlie Lao | 324191f | 2019-12-13 11:03:16 -0800 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM; |
| 796 | if (AHardwareBuffer_isSupported(&desc)) { |
| 797 | all_formats.emplace_back( |
| 798 | VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32, |
| 799 | VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}); |
Jason Macnak | ca50633 | 2022-11-09 11:00:36 -0800 | [diff] [blame] | 800 | if (colorspace_ext) { |
| 801 | all_formats.emplace_back( |
| 802 | VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32, |
| 803 | VK_COLOR_SPACE_PASS_THROUGH_EXT}); |
Charlie Lao | 324191f | 2019-12-13 11:03:16 -0800 | [diff] [blame] | 804 | all_formats.emplace_back( |
| 805 | VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32, |
| 806 | VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT}); |
| 807 | } |
Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 808 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 809 | |
Leon Scroggins III | cb45fe7 | 2021-11-30 16:17:15 -0500 | [diff] [blame] | 810 | desc.format = AHARDWAREBUFFER_FORMAT_R8_UNORM; |
| 811 | if (AHardwareBuffer_isSupported(&desc)) { |
Jason Macnak | ca50633 | 2022-11-09 11:00:36 -0800 | [diff] [blame] | 812 | if (colorspace_ext) { |
| 813 | all_formats.emplace_back(VkSurfaceFormatKHR{ |
| 814 | VK_FORMAT_R8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT}); |
| 815 | } |
Leon Scroggins III | cb45fe7 | 2021-11-30 16:17:15 -0500 | [diff] [blame] | 816 | } |
| 817 | |
Trevor David Black | 67e9b10 | 2023-03-07 05:28:15 +0000 | [diff] [blame] | 818 | bool rgba10x6_formats_ext = false; |
| 819 | uint32_t exts_count; |
| 820 | const auto& driver = GetData(pdev).driver; |
| 821 | driver.EnumerateDeviceExtensionProperties(pdev, nullptr, &exts_count, |
| 822 | nullptr); |
| 823 | std::vector<VkExtensionProperties> props(exts_count); |
| 824 | driver.EnumerateDeviceExtensionProperties(pdev, nullptr, &exts_count, |
| 825 | props.data()); |
| 826 | for (uint32_t i = 0; i < exts_count; i++) { |
| 827 | VkExtensionProperties prop = props[i]; |
| 828 | if (strcmp(prop.extensionName, |
| 829 | VK_EXT_RGBA10X6_FORMATS_EXTENSION_NAME) == 0) { |
| 830 | rgba10x6_formats_ext = true; |
| 831 | } |
| 832 | } |
Trevor David Black | 9cfe1ed | 2022-12-05 20:04:57 +0000 | [diff] [blame] | 833 | desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM; |
Trevor David Black | 67e9b10 | 2023-03-07 05:28:15 +0000 | [diff] [blame] | 834 | if (AHardwareBuffer_isSupported(&desc) && rgba10x6_formats_ext) { |
Trevor David Black | 9cfe1ed | 2022-12-05 20:04:57 +0000 | [diff] [blame] | 835 | all_formats.emplace_back( |
| 836 | VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16, |
| 837 | VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}); |
| 838 | if (colorspace_ext) { |
| 839 | all_formats.emplace_back( |
| 840 | VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16, |
| 841 | VK_COLOR_SPACE_PASS_THROUGH_EXT}); |
| 842 | all_formats.emplace_back( |
| 843 | VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16, |
| 844 | VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT}); |
| 845 | } |
| 846 | } |
| 847 | |
Ian Elliott | 6ba85d9 | 2022-02-18 16:44:58 -0700 | [diff] [blame] | 848 | // NOTE: Any new formats that are added must be coordinated across different |
| 849 | // Android users. This includes the ANGLE team (a layered implementation of |
| 850 | // OpenGL-ES). |
| 851 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 852 | return CopyWithIncomplete(all_formats, formats, count); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 855 | VKAPI_ATTR |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 856 | VkResult GetPhysicalDeviceSurfaceCapabilities2KHR( |
| 857 | VkPhysicalDevice physicalDevice, |
| 858 | const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo, |
| 859 | VkSurfaceCapabilities2KHR* pSurfaceCapabilities) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 860 | ATRACE_CALL(); |
| 861 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 862 | auto surface = pSurfaceInfo->surface; |
| 863 | auto capabilities = &pSurfaceCapabilities->surfaceCapabilities; |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 864 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 865 | VkSurfacePresentModeEXT const *pPresentMode = nullptr; |
| 866 | for (auto pNext = reinterpret_cast<VkBaseInStructure const *>(pSurfaceInfo->pNext); |
| 867 | pNext; pNext = reinterpret_cast<VkBaseInStructure const *>(pNext->pNext)) { |
| 868 | switch (pNext->sType) { |
| 869 | case VK_STRUCTURE_TYPE_SURFACE_PRESENT_MODE_EXT: |
| 870 | pPresentMode = reinterpret_cast<VkSurfacePresentModeEXT const *>(pNext); |
| 871 | break; |
Chris Forbes | 06bc009 | 2017-03-16 16:46:05 +1300 | [diff] [blame] | 872 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 873 | default: |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | int err; |
| 879 | int width, height; |
| 880 | int transform_hint; |
| 881 | int max_buffer_count; |
Trevor David Black | 1d3509e | 2023-06-08 00:17:40 +0000 | [diff] [blame] | 882 | int min_undequeued_buffers; |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 883 | if (surface == VK_NULL_HANDLE) { |
| 884 | const InstanceData& instance_data = GetData(physicalDevice); |
| 885 | ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query; |
| 886 | bool surfaceless_enabled = |
| 887 | instance_data.hook_extensions.test(surfaceless); |
| 888 | if (!surfaceless_enabled) { |
| 889 | // It is an error to pass a surface==VK_NULL_HANDLE unless the |
| 890 | // VK_GOOGLE_surfaceless_query extension is enabled |
| 891 | return VK_ERROR_SURFACE_LOST_KHR; |
| 892 | } |
| 893 | // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this |
| 894 | // extension for this function is for |
| 895 | // VkSurfaceProtectedCapabilitiesKHR::supportsProtected. The following |
| 896 | // four values cannot be known without a surface. Default values will |
| 897 | // be supplied anyway, but cannot be relied upon. |
| 898 | width = 0xFFFFFFFF; |
| 899 | height = 0xFFFFFFFF; |
| 900 | transform_hint = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR; |
| 901 | capabilities->minImageCount = 0xFFFFFFFF; |
| 902 | capabilities->maxImageCount = 0xFFFFFFFF; |
| 903 | } else { |
| 904 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
| 905 | |
| 906 | err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width); |
| 907 | if (err != android::OK) { |
| 908 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 909 | strerror(-err), err); |
| 910 | return VK_ERROR_SURFACE_LOST_KHR; |
| 911 | } |
| 912 | err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height); |
| 913 | if (err != android::OK) { |
| 914 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 915 | strerror(-err), err); |
| 916 | return VK_ERROR_SURFACE_LOST_KHR; |
| 917 | } |
| 918 | |
| 919 | err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, |
| 920 | &transform_hint); |
| 921 | if (err != android::OK) { |
| 922 | ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)", |
| 923 | strerror(-err), err); |
| 924 | return VK_ERROR_SURFACE_LOST_KHR; |
| 925 | } |
| 926 | |
| 927 | err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, |
| 928 | &max_buffer_count); |
| 929 | if (err != android::OK) { |
| 930 | ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)", |
| 931 | strerror(-err), err); |
| 932 | return VK_ERROR_SURFACE_LOST_KHR; |
| 933 | } |
| 934 | |
Trevor David Black | 1d3509e | 2023-06-08 00:17:40 +0000 | [diff] [blame] | 935 | err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 936 | &min_undequeued_buffers); |
| 937 | if (err != android::OK) { |
| 938 | ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d)", |
| 939 | strerror(-err), err); |
| 940 | return VK_ERROR_SURFACE_LOST_KHR; |
| 941 | } |
| 942 | |
Trevor David Black | e4d23b9 | 2023-09-26 20:30:42 +0000 | [diff] [blame] | 943 | // Additional buffer count over min_undequeued_buffers in vulkan came from 2 total |
| 944 | // being technically enough for fifo (although a poor experience) vs 3 being the |
| 945 | // absolute minimum for mailbox to be useful. So min_undequeued_buffers + 2 is sensible |
| 946 | static constexpr int default_additional_buffers = 2; |
| 947 | |
Trevor David Black | a93e21f | 2023-08-30 23:42:20 +0000 | [diff] [blame] | 948 | if(pPresentMode != nullptr) { |
| 949 | switch (pPresentMode->presentMode) { |
| 950 | case VK_PRESENT_MODE_IMMEDIATE_KHR: |
| 951 | ALOGE("Swapchain present mode VK_PRESENT_MODE_IMMEDIATE_KHR is not supported"); |
| 952 | break; |
| 953 | case VK_PRESENT_MODE_MAILBOX_KHR: |
| 954 | case VK_PRESENT_MODE_FIFO_KHR: |
Trevor David Black | e4d23b9 | 2023-09-26 20:30:42 +0000 | [diff] [blame] | 955 | capabilities->minImageCount = std::min(max_buffer_count, |
| 956 | min_undequeued_buffers + default_additional_buffers); |
Trevor David Black | a93e21f | 2023-08-30 23:42:20 +0000 | [diff] [blame] | 957 | capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count); |
| 958 | break; |
| 959 | case VK_PRESENT_MODE_FIFO_RELAXED_KHR: |
| 960 | ALOGE("Swapchain present mode VK_PRESENT_MODE_FIFO_RELEAXED_KHR " |
| 961 | "is not supported"); |
| 962 | break; |
| 963 | case VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR: |
| 964 | case VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR: |
| 965 | capabilities->minImageCount = 1; |
| 966 | capabilities->maxImageCount = 1; |
| 967 | break; |
| 968 | |
| 969 | default: |
| 970 | ALOGE("Unrecognized swapchain present mode %u is not supported", |
| 971 | pPresentMode->presentMode); |
| 972 | break; |
| 973 | } |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 974 | } else { |
Trevor David Black | e4d23b9 | 2023-09-26 20:30:42 +0000 | [diff] [blame] | 975 | capabilities->minImageCount = std::min(max_buffer_count, |
| 976 | min_undequeued_buffers + default_additional_buffers); |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 977 | capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | capabilities->currentExtent = |
| 982 | VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)}; |
| 983 | |
| 984 | // TODO(http://b/134182502): Figure out what the max extent should be. |
| 985 | capabilities->minImageExtent = VkExtent2D{1, 1}; |
| 986 | capabilities->maxImageExtent = VkExtent2D{4096, 4096}; |
| 987 | |
| 988 | if (capabilities->maxImageExtent.height < |
| 989 | capabilities->currentExtent.height) { |
| 990 | capabilities->maxImageExtent.height = |
| 991 | capabilities->currentExtent.height; |
| 992 | } |
| 993 | |
| 994 | if (capabilities->maxImageExtent.width < |
| 995 | capabilities->currentExtent.width) { |
| 996 | capabilities->maxImageExtent.width = capabilities->currentExtent.width; |
| 997 | } |
| 998 | |
| 999 | capabilities->maxImageArrayLayers = 1; |
| 1000 | |
| 1001 | capabilities->supportedTransforms = kSupportedTransforms; |
| 1002 | capabilities->currentTransform = |
| 1003 | TranslateNativeToVulkanTransform(transform_hint); |
| 1004 | |
| 1005 | // On Android, window composition is a WindowManager property, not something |
| 1006 | // associated with the bufferqueue. It can't be changed from here. |
| 1007 | capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; |
| 1008 | |
| 1009 | capabilities->supportedUsageFlags = |
| 1010 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 1011 | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | |
| 1012 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 1013 | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 1014 | |
| 1015 | for (auto pNext = reinterpret_cast<VkBaseOutStructure*>(pSurfaceCapabilities->pNext); |
| 1016 | pNext; pNext = reinterpret_cast<VkBaseOutStructure*>(pNext->pNext)) { |
| 1017 | |
| 1018 | switch (pNext->sType) { |
Chris Forbes | 06bc009 | 2017-03-16 16:46:05 +1300 | [diff] [blame] | 1019 | case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: { |
| 1020 | VkSharedPresentSurfaceCapabilitiesKHR* shared_caps = |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1021 | reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(pNext); |
Chris Forbes | 06bc009 | 2017-03-16 16:46:05 +1300 | [diff] [blame] | 1022 | // Claim same set of usage flags are supported for |
| 1023 | // shared present modes as for other modes. |
| 1024 | shared_caps->sharedPresentSupportedUsageFlags = |
| 1025 | pSurfaceCapabilities->surfaceCapabilities |
| 1026 | .supportedUsageFlags; |
| 1027 | } break; |
| 1028 | |
Ian Elliott | bb67b24 | 2022-03-16 09:52:28 -0600 | [diff] [blame] | 1029 | case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: { |
| 1030 | VkSurfaceProtectedCapabilitiesKHR* protected_caps = |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1031 | reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(pNext); |
Ian Elliott | bb67b24 | 2022-03-16 09:52:28 -0600 | [diff] [blame] | 1032 | protected_caps->supportsProtected = VK_TRUE; |
| 1033 | } break; |
| 1034 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1035 | case VK_STRUCTURE_TYPE_SURFACE_PRESENT_SCALING_CAPABILITIES_EXT: { |
| 1036 | VkSurfacePresentScalingCapabilitiesEXT* scaling_caps = |
| 1037 | reinterpret_cast<VkSurfacePresentScalingCapabilitiesEXT*>(pNext); |
| 1038 | // By default, Android stretches the buffer to fit the window, |
| 1039 | // without preserving aspect ratio. Other modes are technically possible |
| 1040 | // but consult with CoGS team before exposing them here! |
| 1041 | scaling_caps->supportedPresentScaling = VK_PRESENT_SCALING_STRETCH_BIT_EXT; |
| 1042 | |
| 1043 | // Since we always scale, we don't support any gravity. |
| 1044 | scaling_caps->supportedPresentGravityX = 0; |
| 1045 | scaling_caps->supportedPresentGravityY = 0; |
| 1046 | |
| 1047 | // Scaled image limits are just the basic image limits |
| 1048 | scaling_caps->minScaledImageExtent = capabilities->minImageExtent; |
| 1049 | scaling_caps->maxScaledImageExtent = capabilities->maxImageExtent; |
| 1050 | } break; |
| 1051 | |
| 1052 | case VK_STRUCTURE_TYPE_SURFACE_PRESENT_MODE_COMPATIBILITY_EXT: { |
| 1053 | VkSurfacePresentModeCompatibilityEXT* mode_caps = |
| 1054 | reinterpret_cast<VkSurfacePresentModeCompatibilityEXT*>(pNext); |
| 1055 | |
| 1056 | ALOG_ASSERT(pPresentMode, |
| 1057 | "querying VkSurfacePresentModeCompatibilityEXT " |
| 1058 | "requires VkSurfacePresentModeEXT to be provided"); |
| 1059 | std::vector<VkPresentModeKHR> compatibleModes; |
| 1060 | compatibleModes.push_back(pPresentMode->presentMode); |
| 1061 | |
| 1062 | switch (pPresentMode->presentMode) { |
| 1063 | // Shared modes are both compatible with each other. |
| 1064 | case VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR: |
| 1065 | compatibleModes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR); |
| 1066 | break; |
| 1067 | case VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR: |
| 1068 | compatibleModes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR); |
| 1069 | break; |
| 1070 | default: |
| 1071 | // Other modes are only compatible with themselves. |
| 1072 | // TODO: consider whether switching between FIFO and MAILBOX is reasonable |
| 1073 | break; |
| 1074 | } |
| 1075 | |
| 1076 | // Note: this does not generate VK_INCOMPLETE since we're nested inside |
| 1077 | // a larger query and there would be no way to determine exactly where it came from. |
| 1078 | CopyWithIncomplete(compatibleModes, mode_caps->pPresentModes, |
| 1079 | &mode_caps->presentModeCount); |
| 1080 | } break; |
| 1081 | |
Chris Forbes | 06bc009 | 2017-03-16 16:46:05 +1300 | [diff] [blame] | 1082 | default: |
| 1083 | // Ignore all other extension structs |
| 1084 | break; |
| 1085 | } |
| 1086 | } |
| 1087 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1088 | return VK_SUCCESS; |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | VKAPI_ATTR |
| 1092 | VkResult GetPhysicalDeviceSurfaceFormats2KHR( |
| 1093 | VkPhysicalDevice physicalDevice, |
| 1094 | const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo, |
| 1095 | uint32_t* pSurfaceFormatCount, |
| 1096 | VkSurfaceFormat2KHR* pSurfaceFormats) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1097 | ATRACE_CALL(); |
| 1098 | |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 1099 | if (!pSurfaceFormats) { |
| 1100 | return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, |
| 1101 | pSurfaceInfo->surface, |
| 1102 | pSurfaceFormatCount, nullptr); |
Trevor David Black | 929e9cd | 2022-11-22 04:12:19 +0000 | [diff] [blame] | 1103 | } |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 1104 | |
Trevor David Black | 929e9cd | 2022-11-22 04:12:19 +0000 | [diff] [blame] | 1105 | // temp vector for forwarding; we'll marshal it into the pSurfaceFormats |
| 1106 | // after the call. |
| 1107 | std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount); |
| 1108 | VkResult result = GetPhysicalDeviceSurfaceFormatsKHR( |
| 1109 | physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount, |
| 1110 | surface_formats.data()); |
Trevor David Black | 2cc4468 | 2022-03-09 00:31:38 +0000 | [diff] [blame] | 1111 | |
Trevor David Black | 929e9cd | 2022-11-22 04:12:19 +0000 | [diff] [blame] | 1112 | if (result != VK_SUCCESS && result != VK_INCOMPLETE) { |
| 1113 | return result; |
| 1114 | } |
Trevor David Black | 2cc4468 | 2022-03-09 00:31:38 +0000 | [diff] [blame] | 1115 | |
Trevor David Black | 929e9cd | 2022-11-22 04:12:19 +0000 | [diff] [blame] | 1116 | const auto& driver = GetData(physicalDevice).driver; |
| 1117 | |
| 1118 | // marshal results individually due to stride difference. |
| 1119 | uint32_t formats_to_marshal = *pSurfaceFormatCount; |
| 1120 | for (uint32_t i = 0u; i < formats_to_marshal; i++) { |
| 1121 | pSurfaceFormats[i].surfaceFormat = surface_formats[i]; |
| 1122 | |
| 1123 | // Query the compression properties for the surface format |
| 1124 | VkSurfaceFormat2KHR* pSurfaceFormat = &pSurfaceFormats[i]; |
| 1125 | while (pSurfaceFormat->pNext) { |
| 1126 | pSurfaceFormat = |
| 1127 | reinterpret_cast<VkSurfaceFormat2KHR*>(pSurfaceFormat->pNext); |
| 1128 | switch (pSurfaceFormat->sType) { |
| 1129 | case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT: { |
Trevor David Black | 2cc4468 | 2022-03-09 00:31:38 +0000 | [diff] [blame] | 1130 | VkImageCompressionPropertiesEXT* surfaceCompressionProps = |
| 1131 | reinterpret_cast<VkImageCompressionPropertiesEXT*>( |
Trevor David Black | 929e9cd | 2022-11-22 04:12:19 +0000 | [diff] [blame] | 1132 | pSurfaceFormat); |
Trevor David Black | 2cc4468 | 2022-03-09 00:31:38 +0000 | [diff] [blame] | 1133 | |
| 1134 | if (surfaceCompressionProps && |
| 1135 | driver.GetPhysicalDeviceImageFormatProperties2KHR) { |
| 1136 | VkPhysicalDeviceImageFormatInfo2 imageFormatInfo = {}; |
| 1137 | imageFormatInfo.sType = |
| 1138 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2; |
| 1139 | imageFormatInfo.format = |
| 1140 | pSurfaceFormats[i].surfaceFormat.format; |
Trevor David Black | a4298c9 | 2023-06-20 17:11:58 +0000 | [diff] [blame] | 1141 | imageFormatInfo.type = VK_IMAGE_TYPE_2D; |
| 1142 | imageFormatInfo.usage = |
| 1143 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
Trevor David Black | 2cc4468 | 2022-03-09 00:31:38 +0000 | [diff] [blame] | 1144 | imageFormatInfo.pNext = nullptr; |
| 1145 | |
| 1146 | VkImageCompressionControlEXT compressionControl = {}; |
| 1147 | compressionControl.sType = |
| 1148 | VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT; |
| 1149 | compressionControl.pNext = imageFormatInfo.pNext; |
Trevor David Black | a4298c9 | 2023-06-20 17:11:58 +0000 | [diff] [blame] | 1150 | compressionControl.flags = |
| 1151 | VK_IMAGE_COMPRESSION_FIXED_RATE_DEFAULT_EXT; |
Trevor David Black | 2cc4468 | 2022-03-09 00:31:38 +0000 | [diff] [blame] | 1152 | |
| 1153 | imageFormatInfo.pNext = &compressionControl; |
| 1154 | |
| 1155 | VkImageCompressionPropertiesEXT compressionProps = {}; |
| 1156 | compressionProps.sType = |
| 1157 | VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT; |
| 1158 | compressionProps.pNext = nullptr; |
| 1159 | |
| 1160 | VkImageFormatProperties2KHR imageFormatProps = {}; |
| 1161 | imageFormatProps.sType = |
| 1162 | VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR; |
| 1163 | imageFormatProps.pNext = &compressionProps; |
| 1164 | |
| 1165 | VkResult compressionRes = |
| 1166 | driver.GetPhysicalDeviceImageFormatProperties2KHR( |
| 1167 | physicalDevice, &imageFormatInfo, |
| 1168 | &imageFormatProps); |
| 1169 | if (compressionRes == VK_SUCCESS) { |
| 1170 | surfaceCompressionProps->imageCompressionFlags = |
| 1171 | compressionProps.imageCompressionFlags; |
| 1172 | surfaceCompressionProps |
| 1173 | ->imageCompressionFixedRateFlags = |
| 1174 | compressionProps.imageCompressionFixedRateFlags; |
| 1175 | } else { |
| 1176 | return compressionRes; |
| 1177 | } |
| 1178 | } |
Trevor David Black | 929e9cd | 2022-11-22 04:12:19 +0000 | [diff] [blame] | 1179 | } break; |
| 1180 | |
| 1181 | default: |
| 1182 | // Ignore all other extension structs |
| 1183 | break; |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 1184 | } |
| 1185 | } |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 1186 | } |
Trevor David Black | 929e9cd | 2022-11-22 04:12:19 +0000 | [diff] [blame] | 1187 | |
| 1188 | return result; |
Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | VKAPI_ATTR |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 1192 | VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev, |
Yiwei Zhang | e4a559c | 2018-02-15 11:27:36 -0800 | [diff] [blame] | 1193 | VkSurfaceKHR surface, |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1194 | uint32_t* count, |
| 1195 | VkPresentModeKHR* modes) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1196 | ATRACE_CALL(); |
| 1197 | |
Yiwei Zhang | e4a559c | 2018-02-15 11:27:36 -0800 | [diff] [blame] | 1198 | int err; |
| 1199 | int query_value; |
Ian Elliott | e7f036c | 2022-03-15 16:49:21 -0600 | [diff] [blame] | 1200 | std::vector<VkPresentModeKHR> present_modes; |
Ian Elliott | 1ce053f | 2022-03-16 09:49:53 -0600 | [diff] [blame] | 1201 | if (surface == VK_NULL_HANDLE) { |
| 1202 | const InstanceData& instance_data = GetData(pdev); |
| 1203 | ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query; |
| 1204 | bool surfaceless_enabled = |
| 1205 | instance_data.hook_extensions.test(surfaceless); |
| 1206 | if (!surfaceless_enabled) { |
| 1207 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1208 | } |
| 1209 | // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this |
| 1210 | // extension for this function is for |
| 1211 | // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and |
| 1212 | // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot |
| 1213 | // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a |
Ian Elliott | 7e36114 | 2022-06-02 10:45:17 -0600 | [diff] [blame] | 1214 | // surface, and that cannot be relied upon. Therefore, don't return it. |
Ian Elliott | 1ce053f | 2022-03-16 09:49:53 -0600 | [diff] [blame] | 1215 | present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR); |
| 1216 | } else { |
| 1217 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
| 1218 | |
| 1219 | err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 1220 | &query_value); |
| 1221 | if (err != android::OK || query_value < 0) { |
| 1222 | ALOGE( |
| 1223 | "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) " |
| 1224 | "value=%d", |
| 1225 | strerror(-err), err, query_value); |
| 1226 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1227 | } |
| 1228 | uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value); |
| 1229 | |
| 1230 | err = |
| 1231 | window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value); |
| 1232 | if (err != android::OK || query_value < 0) { |
| 1233 | ALOGE( |
| 1234 | "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d", |
| 1235 | strerror(-err), err, query_value); |
| 1236 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1237 | } |
| 1238 | uint32_t max_buffer_count = static_cast<uint32_t>(query_value); |
| 1239 | |
| 1240 | if (min_undequeued_buffers + 1 < max_buffer_count) |
| 1241 | present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR); |
| 1242 | present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR); |
| 1243 | } |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 1244 | |
| 1245 | VkPhysicalDevicePresentationPropertiesANDROID present_properties; |
Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 1246 | QueryPresentationProperties(pdev, &present_properties); |
| 1247 | if (present_properties.sharedImage) { |
| 1248 | present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR); |
| 1249 | present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR); |
Chris Forbes | e8d79a6 | 2017-02-22 12:49:18 +1300 | [diff] [blame] | 1250 | } |
| 1251 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1252 | return CopyWithIncomplete(present_modes, modes, count); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 1255 | VKAPI_ATTR |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1256 | VkResult GetDeviceGroupPresentCapabilitiesKHR( |
Ian Elliott | cd8ad33 | 2017-10-13 09:21:12 -0600 | [diff] [blame] | 1257 | VkDevice, |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1258 | VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1259 | ATRACE_CALL(); |
| 1260 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1261 | ALOGV_IF(pDeviceGroupPresentCapabilities->sType != |
| 1262 | VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR, |
| 1263 | "vkGetDeviceGroupPresentCapabilitiesKHR: invalid " |
| 1264 | "VkDeviceGroupPresentCapabilitiesKHR structure type %d", |
| 1265 | pDeviceGroupPresentCapabilities->sType); |
| 1266 | |
| 1267 | memset(pDeviceGroupPresentCapabilities->presentMask, 0, |
| 1268 | sizeof(pDeviceGroupPresentCapabilities->presentMask)); |
| 1269 | |
| 1270 | // assume device group of size 1 |
| 1271 | pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0; |
| 1272 | pDeviceGroupPresentCapabilities->modes = |
| 1273 | VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR; |
| 1274 | |
| 1275 | return VK_SUCCESS; |
| 1276 | } |
| 1277 | |
| 1278 | VKAPI_ATTR |
| 1279 | VkResult GetDeviceGroupSurfacePresentModesKHR( |
Ian Elliott | cd8ad33 | 2017-10-13 09:21:12 -0600 | [diff] [blame] | 1280 | VkDevice, |
| 1281 | VkSurfaceKHR, |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1282 | VkDeviceGroupPresentModeFlagsKHR* pModes) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1283 | ATRACE_CALL(); |
| 1284 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1285 | *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR; |
| 1286 | return VK_SUCCESS; |
| 1287 | } |
| 1288 | |
| 1289 | VKAPI_ATTR |
Ian Elliott | cd8ad33 | 2017-10-13 09:21:12 -0600 | [diff] [blame] | 1290 | VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice, |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1291 | VkSurfaceKHR surface, |
| 1292 | uint32_t* pRectCount, |
| 1293 | VkRect2D* pRects) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1294 | ATRACE_CALL(); |
| 1295 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1296 | if (!pRects) { |
| 1297 | *pRectCount = 1; |
| 1298 | } else { |
| 1299 | uint32_t count = std::min(*pRectCount, 1u); |
| 1300 | bool incomplete = *pRectCount < 1; |
| 1301 | |
| 1302 | *pRectCount = count; |
| 1303 | |
| 1304 | if (incomplete) { |
| 1305 | return VK_INCOMPLETE; |
| 1306 | } |
| 1307 | |
| 1308 | int err; |
| 1309 | ANativeWindow* window = SurfaceFromHandle(surface)->window.get(); |
| 1310 | |
| 1311 | int width = 0, height = 0; |
| 1312 | err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width); |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1313 | if (err != android::OK) { |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1314 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 1315 | strerror(-err), err); |
| 1316 | } |
| 1317 | err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height); |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1318 | if (err != android::OK) { |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1319 | ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)", |
| 1320 | strerror(-err), err); |
| 1321 | } |
| 1322 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1323 | pRects[0].offset.x = 0; |
| 1324 | pRects[0].offset.y = 0; |
| 1325 | pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width), |
| 1326 | static_cast<uint32_t>(height)}; |
| 1327 | } |
| 1328 | return VK_SUCCESS; |
| 1329 | } |
| 1330 | |
Yiwei Zhang | 533cea9 | 2019-06-03 18:43:24 -0700 | [diff] [blame] | 1331 | static void DestroySwapchainInternal(VkDevice device, |
| 1332 | VkSwapchainKHR swapchain_handle, |
| 1333 | const VkAllocationCallbacks* allocator) { |
| 1334 | ATRACE_CALL(); |
| 1335 | |
| 1336 | const auto& dispatch = GetData(device).driver; |
| 1337 | Swapchain* swapchain = SwapchainFromHandle(swapchain_handle); |
| 1338 | if (!swapchain) { |
| 1339 | return; |
| 1340 | } |
| 1341 | |
| 1342 | bool active = swapchain->surface.swapchain_handle == swapchain_handle; |
| 1343 | ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr; |
| 1344 | |
| 1345 | if (window && swapchain->frame_timestamps_enabled) { |
| 1346 | native_window_enable_frame_timestamps(window, false); |
| 1347 | } |
| 1348 | |
| 1349 | for (uint32_t i = 0; i < swapchain->num_images; i++) { |
Yiwei Zhang | ac1f098 | 2022-04-09 07:10:10 +0000 | [diff] [blame] | 1350 | ReleaseSwapchainImage(device, swapchain->shared, window, -1, |
| 1351 | swapchain->images[i], false); |
Yiwei Zhang | 533cea9 | 2019-06-03 18:43:24 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | if (active) { |
| 1355 | swapchain->surface.swapchain_handle = VK_NULL_HANDLE; |
| 1356 | } |
| 1357 | |
| 1358 | if (!allocator) { |
| 1359 | allocator = &GetData(device).allocator; |
| 1360 | } |
| 1361 | |
| 1362 | swapchain->~Swapchain(); |
| 1363 | allocator->pfnFree(allocator->pUserData, swapchain); |
| 1364 | } |
| 1365 | |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1366 | static VkResult getProducerUsage(const VkDevice& device, |
| 1367 | const VkSwapchainCreateInfoKHR* create_info, |
| 1368 | const VkSwapchainImageUsageFlagsANDROID swapchain_image_usage, |
| 1369 | bool create_protected_swapchain, |
| 1370 | uint64_t* producer_usage) { |
| 1371 | // Get the physical device to query the appropriate producer usage |
| 1372 | const VkPhysicalDevice& pdev = GetData(device).driver_physical_device; |
| 1373 | const InstanceData& instance_data = GetData(pdev); |
| 1374 | const InstanceDriverTable& instance_dispatch = instance_data.driver; |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1375 | if (instance_dispatch.GetPhysicalDeviceImageFormatProperties2 || |
| 1376 | instance_dispatch.GetPhysicalDeviceImageFormatProperties2KHR) { |
| 1377 | // Look through the create_info pNext chain passed to createSwapchainKHR |
| 1378 | // for an image compression control struct. |
| 1379 | // if one is found AND the appropriate extensions are enabled, create a |
| 1380 | // VkImageCompressionControlEXT structure to pass on to |
| 1381 | // GetPhysicalDeviceImageFormatProperties2 |
| 1382 | void* compression_control_pNext = nullptr; |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1383 | VkImageCompressionControlEXT image_compression = {}; |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1384 | const VkSwapchainCreateInfoKHR* create_infos = create_info; |
| 1385 | while (create_infos->pNext) { |
| 1386 | create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(create_infos->pNext); |
| 1387 | switch (create_infos->sType) { |
| 1388 | case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: { |
| 1389 | const VkImageCompressionControlEXT* compression_infos = |
| 1390 | reinterpret_cast<const VkImageCompressionControlEXT*>(create_infos); |
| 1391 | image_compression = *compression_infos; |
| 1392 | image_compression.pNext = nullptr; |
| 1393 | compression_control_pNext = &image_compression; |
| 1394 | } break; |
| 1395 | default: |
| 1396 | // Ignore all other info structs |
| 1397 | break; |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1398 | } |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1399 | } |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1400 | |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1401 | // call GetPhysicalDeviceImageFormatProperties2KHR |
| 1402 | VkPhysicalDeviceExternalImageFormatInfo external_image_format_info = { |
| 1403 | .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO, |
| 1404 | .pNext = compression_control_pNext, |
| 1405 | .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID, |
| 1406 | }; |
| 1407 | |
| 1408 | // AHB does not have an sRGB format so we can't pass it to GPDIFP |
| 1409 | // We need to convert the format to unorm if it is srgb |
| 1410 | VkFormat format = create_info->imageFormat; |
| 1411 | if (format == VK_FORMAT_R8G8B8A8_SRGB) { |
| 1412 | format = VK_FORMAT_R8G8B8A8_UNORM; |
| 1413 | } |
| 1414 | |
| 1415 | VkPhysicalDeviceImageFormatInfo2 image_format_info = { |
| 1416 | .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2, |
| 1417 | .pNext = &external_image_format_info, |
| 1418 | .format = format, |
| 1419 | .type = VK_IMAGE_TYPE_2D, |
| 1420 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
| 1421 | .usage = create_info->imageUsage, |
| 1422 | .flags = create_protected_swapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u, |
| 1423 | }; |
| 1424 | |
| 1425 | VkAndroidHardwareBufferUsageANDROID ahb_usage; |
| 1426 | ahb_usage.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_USAGE_ANDROID; |
| 1427 | ahb_usage.pNext = nullptr; |
| 1428 | |
| 1429 | VkImageFormatProperties2 image_format_properties; |
| 1430 | image_format_properties.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2; |
| 1431 | image_format_properties.pNext = &ahb_usage; |
| 1432 | |
| 1433 | if (instance_dispatch.GetPhysicalDeviceImageFormatProperties2) { |
| 1434 | VkResult result = instance_dispatch.GetPhysicalDeviceImageFormatProperties2( |
| 1435 | pdev, &image_format_info, &image_format_properties); |
| 1436 | if (result != VK_SUCCESS) { |
| 1437 | ALOGE("VkGetPhysicalDeviceImageFormatProperties2 for AHB usage failed: %d", result); |
| 1438 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1439 | } |
| 1440 | } |
| 1441 | else { |
| 1442 | VkResult result = instance_dispatch.GetPhysicalDeviceImageFormatProperties2KHR( |
| 1443 | pdev, &image_format_info, |
| 1444 | &image_format_properties); |
| 1445 | if (result != VK_SUCCESS) { |
| 1446 | ALOGE("VkGetPhysicalDeviceImageFormatProperties2KHR for AHB usage failed: %d", |
| 1447 | result); |
| 1448 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | // Determine if USAGE_FRONT_BUFFER is needed. |
| 1453 | // GPDIFP2 has no means of using VkSwapchainImageUsageFlagsANDROID when |
| 1454 | // querying for producer_usage. So androidHardwareBufferUsage will not |
| 1455 | // contain USAGE_FRONT_BUFFER. We need to manually check for usage here. |
| 1456 | if (!(swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID)) { |
| 1457 | *producer_usage = ahb_usage.androidHardwareBufferUsage; |
| 1458 | return VK_SUCCESS; |
| 1459 | } |
| 1460 | |
| 1461 | // Check if USAGE_FRONT_BUFFER is supported for this swapchain |
| 1462 | AHardwareBuffer_Desc ahb_desc = { |
| 1463 | .width = create_info->imageExtent.width, |
| 1464 | .height = create_info->imageExtent.height, |
| 1465 | .layers = create_info->imageArrayLayers, |
| 1466 | .format = create_info->imageFormat, |
| 1467 | .usage = ahb_usage.androidHardwareBufferUsage | AHARDWAREBUFFER_USAGE_FRONT_BUFFER, |
| 1468 | .stride = 0, // stride is always ignored when calling isSupported() |
| 1469 | }; |
| 1470 | |
| 1471 | // If FRONT_BUFFER is not supported, |
| 1472 | // then we need to call GetSwapchainGrallocUsageXAndroid below |
| 1473 | if (AHardwareBuffer_isSupported(&ahb_desc)) { |
| 1474 | *producer_usage = ahb_usage.androidHardwareBufferUsage; |
| 1475 | *producer_usage |= AHARDWAREBUFFER_USAGE_FRONT_BUFFER; |
| 1476 | return VK_SUCCESS; |
| 1477 | } |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1480 | uint64_t native_usage = 0; |
| 1481 | void* usage_info_pNext = nullptr; |
| 1482 | VkResult result; |
Vamsidhar reddy Gaddam | 7a84431 | 2023-11-14 10:59:05 +0000 | [diff] [blame] | 1483 | VkImageCompressionControlEXT image_compression = {}; |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1484 | const auto& dispatch = GetData(device).driver; |
| 1485 | if (dispatch.GetSwapchainGrallocUsage4ANDROID) { |
| 1486 | ATRACE_BEGIN("GetSwapchainGrallocUsage4ANDROID"); |
| 1487 | VkGrallocUsageInfo2ANDROID gralloc_usage_info = {}; |
| 1488 | gralloc_usage_info.sType = |
| 1489 | VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_2_ANDROID; |
| 1490 | gralloc_usage_info.format = create_info->imageFormat; |
| 1491 | gralloc_usage_info.imageUsage = create_info->imageUsage; |
| 1492 | gralloc_usage_info.swapchainImageUsage = swapchain_image_usage; |
| 1493 | |
| 1494 | // Look through the pNext chain for an image compression control struct |
| 1495 | // if one is found AND the appropriate extensions are enabled, |
| 1496 | // append it to be the gralloc usage pNext chain |
| 1497 | const VkSwapchainCreateInfoKHR* create_infos = create_info; |
| 1498 | while (create_infos->pNext) { |
| 1499 | create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>( |
| 1500 | create_infos->pNext); |
| 1501 | switch (create_infos->sType) { |
| 1502 | case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: { |
| 1503 | const VkImageCompressionControlEXT* compression_infos = |
| 1504 | reinterpret_cast<const VkImageCompressionControlEXT*>( |
| 1505 | create_infos); |
| 1506 | image_compression = *compression_infos; |
| 1507 | image_compression.pNext = nullptr; |
| 1508 | usage_info_pNext = &image_compression; |
| 1509 | } break; |
| 1510 | |
| 1511 | default: |
| 1512 | // Ignore all other info structs |
| 1513 | break; |
| 1514 | } |
Vamsidhar reddy Gaddam | 7a84431 | 2023-11-14 10:59:05 +0000 | [diff] [blame] | 1515 | } |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1516 | gralloc_usage_info.pNext = usage_info_pNext; |
Vamsidhar reddy Gaddam | 7a84431 | 2023-11-14 10:59:05 +0000 | [diff] [blame] | 1517 | |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1518 | result = dispatch.GetSwapchainGrallocUsage4ANDROID( |
| 1519 | device, &gralloc_usage_info, &native_usage); |
| 1520 | ATRACE_END(); |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1521 | if (result != VK_SUCCESS) { |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1522 | ALOGE("vkGetSwapchainGrallocUsage4ANDROID failed: %d", result); |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1523 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1524 | } |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1525 | } else if (dispatch.GetSwapchainGrallocUsage3ANDROID) { |
| 1526 | ATRACE_BEGIN("GetSwapchainGrallocUsage3ANDROID"); |
| 1527 | VkGrallocUsageInfoANDROID gralloc_usage_info = {}; |
| 1528 | gralloc_usage_info.sType = VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID; |
| 1529 | gralloc_usage_info.format = create_info->imageFormat; |
| 1530 | gralloc_usage_info.imageUsage = create_info->imageUsage; |
| 1531 | |
| 1532 | // Look through the pNext chain for an image compression control struct |
| 1533 | // if one is found AND the appropriate extensions are enabled, |
| 1534 | // append it to be the gralloc usage pNext chain |
| 1535 | const VkSwapchainCreateInfoKHR* create_infos = create_info; |
| 1536 | while (create_infos->pNext) { |
| 1537 | create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>( |
| 1538 | create_infos->pNext); |
| 1539 | switch (create_infos->sType) { |
| 1540 | case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: { |
| 1541 | const VkImageCompressionControlEXT* compression_infos = |
| 1542 | reinterpret_cast<const VkImageCompressionControlEXT*>( |
| 1543 | create_infos); |
| 1544 | image_compression = *compression_infos; |
| 1545 | image_compression.pNext = nullptr; |
| 1546 | usage_info_pNext = &image_compression; |
| 1547 | } break; |
| 1548 | |
| 1549 | default: |
| 1550 | // Ignore all other info structs |
| 1551 | break; |
| 1552 | } |
| 1553 | } |
| 1554 | gralloc_usage_info.pNext = usage_info_pNext; |
| 1555 | |
| 1556 | result = dispatch.GetSwapchainGrallocUsage3ANDROID( |
| 1557 | device, &gralloc_usage_info, &native_usage); |
| 1558 | ATRACE_END(); |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1559 | if (result != VK_SUCCESS) { |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1560 | ALOGE("vkGetSwapchainGrallocUsage3ANDROID failed: %d", result); |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1561 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1562 | } |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1563 | } else if (dispatch.GetSwapchainGrallocUsage2ANDROID) { |
| 1564 | uint64_t consumer_usage, producer_usage; |
| 1565 | ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID"); |
| 1566 | result = dispatch.GetSwapchainGrallocUsage2ANDROID( |
| 1567 | device, create_info->imageFormat, create_info->imageUsage, |
| 1568 | swapchain_image_usage, &consumer_usage, &producer_usage); |
| 1569 | ATRACE_END(); |
| 1570 | if (result != VK_SUCCESS) { |
| 1571 | ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result); |
| 1572 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1573 | } |
| 1574 | native_usage = |
| 1575 | convertGralloc1ToBufferUsage(producer_usage, consumer_usage); |
| 1576 | } else if (dispatch.GetSwapchainGrallocUsageANDROID) { |
| 1577 | ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID"); |
| 1578 | int32_t legacy_usage = 0; |
| 1579 | result = dispatch.GetSwapchainGrallocUsageANDROID( |
| 1580 | device, create_info->imageFormat, create_info->imageUsage, |
| 1581 | &legacy_usage); |
| 1582 | ATRACE_END(); |
| 1583 | if (result != VK_SUCCESS) { |
| 1584 | ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result); |
| 1585 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1586 | } |
| 1587 | native_usage = static_cast<uint64_t>(legacy_usage); |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1588 | } |
Trevor David Black | 02926f5 | 2024-01-04 18:44:20 +0000 | [diff] [blame] | 1589 | *producer_usage = native_usage; |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1590 | |
| 1591 | return VK_SUCCESS; |
| 1592 | } |
| 1593 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1594 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 1595 | VkResult CreateSwapchainKHR(VkDevice device, |
| 1596 | const VkSwapchainCreateInfoKHR* create_info, |
| 1597 | const VkAllocationCallbacks* allocator, |
| 1598 | VkSwapchainKHR* swapchain_handle) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1599 | ATRACE_CALL(); |
| 1600 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1601 | int err; |
| 1602 | VkResult result = VK_SUCCESS; |
| 1603 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1604 | ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64 |
| 1605 | " minImageCount=%u imageFormat=%u imageColorSpace=%u" |
| 1606 | " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u" |
| 1607 | " oldSwapchain=0x%" PRIx64, |
| 1608 | reinterpret_cast<uint64_t>(create_info->surface), |
| 1609 | create_info->minImageCount, create_info->imageFormat, |
| 1610 | create_info->imageColorSpace, create_info->imageExtent.width, |
| 1611 | create_info->imageExtent.height, create_info->imageUsage, |
| 1612 | create_info->preTransform, create_info->presentMode, |
| 1613 | reinterpret_cast<uint64_t>(create_info->oldSwapchain)); |
| 1614 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1615 | if (!allocator) |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1616 | allocator = &GetData(device).allocator; |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1617 | |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 1618 | PixelFormat native_pixel_format = |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 1619 | GetNativePixelFormat(create_info->imageFormat); |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 1620 | DataSpace native_dataspace = |
Sally Qi | aa23897 | 2023-07-17 12:52:05 +0800 | [diff] [blame] | 1621 | GetNativeDataspace(create_info->imageColorSpace, native_pixel_format); |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 1622 | if (native_dataspace == DataSpace::UNKNOWN) { |
Courtney Goeltzenleuchter | 7d4a64a | 2017-02-17 12:59:11 -0700 | [diff] [blame] | 1623 | ALOGE( |
| 1624 | "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) " |
| 1625 | "failed: Unsupported color space", |
| 1626 | create_info->imageColorSpace); |
| 1627 | return VK_ERROR_INITIALIZATION_FAILED; |
| 1628 | } |
| 1629 | |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 1630 | ALOGV_IF(create_info->imageArrayLayers != 1, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1631 | "swapchain imageArrayLayers=%u not supported", |
Jesse Hall | 715b86a | 2016-01-16 16:34:29 -0800 | [diff] [blame] | 1632 | create_info->imageArrayLayers); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 1633 | ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0, |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1634 | "swapchain preTransform=%#x not supported", |
Jesse Hall | 55bc097 | 2016-02-23 16:43:29 -0800 | [diff] [blame] | 1635 | create_info->preTransform); |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 1636 | ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR || |
Chris Forbes | 980ad05 | 2017-01-18 16:55:07 +1300 | [diff] [blame] | 1637 | create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR || |
Chris Forbes | 1d5f68c | 2017-01-31 10:17:01 +1300 | [diff] [blame] | 1638 | create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR || |
| 1639 | create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR), |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1640 | "swapchain presentMode=%u not supported", |
Jesse Hall | 0ae0dce | 2016-02-09 22:13:34 -0800 | [diff] [blame] | 1641 | create_info->presentMode); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1642 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1643 | Surface& surface = *SurfaceFromHandle(create_info->surface); |
| 1644 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1645 | if (surface.swapchain_handle != create_info->oldSwapchain) { |
Jesse Hall | 42a9eec | 2016-06-03 12:39:49 -0700 | [diff] [blame] | 1646 | ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64 |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1647 | " because it already has active swapchain 0x%" PRIx64 |
| 1648 | " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64, |
| 1649 | reinterpret_cast<uint64_t>(create_info->surface), |
| 1650 | reinterpret_cast<uint64_t>(surface.swapchain_handle), |
| 1651 | reinterpret_cast<uint64_t>(create_info->oldSwapchain)); |
| 1652 | return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR; |
| 1653 | } |
| 1654 | if (create_info->oldSwapchain != VK_NULL_HANDLE) |
| 1655 | OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain)); |
| 1656 | |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1657 | // -- Reset the native window -- |
| 1658 | // The native window might have been used previously, and had its properties |
| 1659 | // changed from defaults. That will affect the answer we get for queries |
| 1660 | // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we |
| 1661 | // attempt such queries. |
| 1662 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1663 | // The native window only allows dequeueing all buffers before any have |
| 1664 | // been queued, since after that point at least one is assumed to be in |
| 1665 | // non-FREE state at any given time. Disconnecting and re-connecting |
| 1666 | // orphans the previous buffers, getting us back to the state where we can |
| 1667 | // dequeue all buffers. |
Yiwei Zhang | 70a2196 | 2019-05-31 17:26:52 -0700 | [diff] [blame] | 1668 | // |
Yiwei Zhang | 3b88f31 | 2023-04-18 23:11:35 +0000 | [diff] [blame] | 1669 | // This is not necessary if the surface was never used previously. |
| 1670 | // |
Yiwei Zhang | 70a2196 | 2019-05-31 17:26:52 -0700 | [diff] [blame] | 1671 | // TODO(http://b/134186185) recycle swapchain images more efficiently |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1672 | ANativeWindow* window = surface.window.get(); |
Yiwei Zhang | 3b88f31 | 2023-04-18 23:11:35 +0000 | [diff] [blame] | 1673 | if (surface.used_by_swapchain) { |
| 1674 | err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL); |
| 1675 | ALOGW_IF(err != android::OK, |
| 1676 | "native_window_api_disconnect failed: %s (%d)", strerror(-err), |
| 1677 | err); |
| 1678 | err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL); |
| 1679 | ALOGW_IF(err != android::OK, |
| 1680 | "native_window_api_connect failed: %s (%d)", strerror(-err), |
| 1681 | err); |
| 1682 | } |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1683 | |
Nicolas Capens | 147b7da | 2021-04-09 14:53:06 -0400 | [diff] [blame] | 1684 | err = |
| 1685 | window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1}); |
Yiwei Zhang | 705c2e6 | 2019-12-18 23:12:43 -0800 | [diff] [blame] | 1686 | if (err != android::OK) { |
| 1687 | ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)", |
| 1688 | strerror(-err), err); |
| 1689 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1690 | } |
| 1691 | |
Hrishikesh Manohar | 9b7e453 | 2017-01-10 17:52:11 +0530 | [diff] [blame] | 1692 | int swap_interval = |
| 1693 | create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1; |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1694 | err = window->setSwapInterval(window, swap_interval); |
| 1695 | if (err != android::OK) { |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1696 | ALOGE("native_window->setSwapInterval(1) failed: %s (%d)", |
| 1697 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1698 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1701 | err = native_window_set_shared_buffer_mode(window, false); |
| 1702 | if (err != android::OK) { |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 1703 | ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)", |
| 1704 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1705 | return VK_ERROR_SURFACE_LOST_KHR; |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 1706 | } |
| 1707 | |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1708 | err = native_window_set_auto_refresh(window, false); |
| 1709 | if (err != android::OK) { |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 1710 | ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)", |
| 1711 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1712 | return VK_ERROR_SURFACE_LOST_KHR; |
Chris Forbes | b8042d2 | 2017-01-18 18:07:05 +1300 | [diff] [blame] | 1713 | } |
| 1714 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1715 | // -- Configure the native window -- |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1716 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 1717 | const auto& dispatch = GetData(device).driver; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1718 | |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 1719 | err = native_window_set_buffers_format( |
| 1720 | window, static_cast<int>(native_pixel_format)); |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1721 | if (err != android::OK) { |
Leon Scroggins III | cb45fe7 | 2021-11-30 16:17:15 -0500 | [diff] [blame] | 1722 | ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)", |
Trevor David Black | f499b5a | 2023-07-14 17:30:41 +0000 | [diff] [blame] | 1723 | toString(native_pixel_format).c_str(), strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1724 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 1725 | } |
Yiwei Zhang | 51572c2 | 2022-07-22 23:08:30 +0000 | [diff] [blame] | 1726 | |
| 1727 | /* Respect consumer default dataspace upon HAL_DATASPACE_ARBITRARY. */ |
sergiuferentz | 4798933 | 2023-09-26 10:24:36 +0000 | [diff] [blame] | 1728 | if (native_dataspace != DataSpace::ARBITRARY) { |
| 1729 | err = native_window_set_buffers_data_space( |
| 1730 | window, static_cast<android_dataspace_t>(native_dataspace)); |
Yiwei Zhang | 51572c2 | 2022-07-22 23:08:30 +0000 | [diff] [blame] | 1731 | if (err != android::OK) { |
| 1732 | ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)", |
| 1733 | native_dataspace, strerror(-err), err); |
| 1734 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1735 | } |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 1736 | } |
| 1737 | |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 1738 | err = native_window_set_buffers_dimensions( |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1739 | window, static_cast<int>(create_info->imageExtent.width), |
Jesse Hall | 3dd678a | 2016-01-08 21:52:01 -0800 | [diff] [blame] | 1740 | static_cast<int>(create_info->imageExtent.height)); |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1741 | if (err != android::OK) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1742 | ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)", |
| 1743 | create_info->imageExtent.width, create_info->imageExtent.height, |
| 1744 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1745 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 1748 | // VkSwapchainCreateInfo::preTransform indicates the transformation the app |
| 1749 | // applied during rendering. native_window_set_transform() expects the |
| 1750 | // inverse: the transform the app is requesting that the compositor perform |
| 1751 | // during composition. With native windows, pre-transform works by rendering |
| 1752 | // with the same transform the compositor is applying (as in Vulkan), but |
| 1753 | // then requesting the inverse transform, so that when the compositor does |
| 1754 | // it's job the two transforms cancel each other out and the compositor ends |
| 1755 | // up applying an identity transform to the app's buffer. |
| 1756 | err = native_window_set_buffers_transform( |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1757 | window, InvertTransformToNative(create_info->preTransform)); |
| 1758 | if (err != android::OK) { |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 1759 | ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)", |
| 1760 | InvertTransformToNative(create_info->preTransform), |
| 1761 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1762 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 178b696 | 2016-02-24 15:39:50 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 1765 | err = native_window_set_scaling_mode( |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1766 | window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
| 1767 | if (err != android::OK) { |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 1768 | ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)", |
| 1769 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1770 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | f64ca12 | 2015-11-03 16:11:10 -0800 | [diff] [blame] | 1771 | } |
| 1772 | |
Chris Forbes | 97ef461 | 2017-03-30 19:37:50 +1300 | [diff] [blame] | 1773 | VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0; |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1774 | if (IsSharedPresentMode(create_info->presentMode)) { |
Chris Forbes | 97ef461 | 2017-03-30 19:37:50 +1300 | [diff] [blame] | 1775 | swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID; |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1776 | err = native_window_set_shared_buffer_mode(window, true); |
| 1777 | if (err != android::OK) { |
Chris Forbes | 97ef461 | 2017-03-30 19:37:50 +1300 | [diff] [blame] | 1778 | ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err); |
| 1779 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) { |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1784 | err = native_window_set_auto_refresh(window, true); |
| 1785 | if (err != android::OK) { |
Chris Forbes | 97ef461 | 2017-03-30 19:37:50 +1300 | [diff] [blame] | 1786 | ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err); |
| 1787 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1788 | } |
| 1789 | } |
| 1790 | |
Ian Elliott | 16c443c | 2021-11-30 17:10:32 -0700 | [diff] [blame] | 1791 | int query_value; |
Trevor David Black | abab5a0 | 2023-12-19 20:27:07 +0000 | [diff] [blame] | 1792 | // TODO: Now that we are calling into GPDSC2 directly, this query may be redundant |
| 1793 | // the call to std::max(min_buffer_count, num_images) may be redundant as well |
Ian Elliott | 16c443c | 2021-11-30 17:10:32 -0700 | [diff] [blame] | 1794 | err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 1795 | &query_value); |
| 1796 | if (err != android::OK || query_value < 0) { |
| 1797 | ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, |
| 1798 | query_value); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1799 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1800 | } |
Trevor David Black | 8c0122e | 2023-09-07 20:33:54 +0000 | [diff] [blame] | 1801 | const uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value); |
Chris Forbes | 2c8fc75 | 2017-03-17 11:28:32 +1300 | [diff] [blame] | 1802 | |
Ian Elliott | 5396b70 | 2021-12-13 19:32:34 -0700 | [diff] [blame] | 1803 | // Lower layer insists that we have at least min_undequeued_buffers + 1 |
| 1804 | // buffers. This is wasteful and we'd like to relax it in the shared case, |
| 1805 | // but not all the pieces are in place for that to work yet. Note we only |
| 1806 | // lie to the lower layer--we don't want to give the app back a swapchain |
| 1807 | // with extra images (which they can't actually use!). |
Trevor David Black | 8c0122e | 2023-09-07 20:33:54 +0000 | [diff] [blame] | 1808 | const uint32_t min_buffer_count = min_undequeued_buffers + 1; |
| 1809 | |
Trevor David Black | abab5a0 | 2023-12-19 20:27:07 +0000 | [diff] [blame] | 1810 | // Call into GPDSC2 to get the minimum and maximum allowable buffer count for the surface of |
| 1811 | // interest. This step is only necessary if the app requests a number of images |
| 1812 | // (create_info->minImageCount) that is less or more than the surface capabilities. |
| 1813 | // An app should be calling GPDSC2 and using those values to set create_info, but in the |
| 1814 | // event that the app has hard-coded image counts an error can occur |
| 1815 | VkSurfacePresentModeEXT present_mode = { |
| 1816 | VK_STRUCTURE_TYPE_SURFACE_PRESENT_MODE_EXT, |
| 1817 | nullptr, |
| 1818 | create_info->presentMode |
| 1819 | }; |
| 1820 | VkPhysicalDeviceSurfaceInfo2KHR surface_info2 = { |
| 1821 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR, |
| 1822 | &present_mode, |
| 1823 | create_info->surface |
| 1824 | }; |
| 1825 | VkSurfaceCapabilities2KHR surface_capabilities2 = { |
| 1826 | VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR, |
| 1827 | nullptr, |
| 1828 | {}, |
| 1829 | }; |
| 1830 | result = GetPhysicalDeviceSurfaceCapabilities2KHR(GetData(device).driver_physical_device, |
| 1831 | &surface_info2, &surface_capabilities2); |
| 1832 | |
| 1833 | uint32_t num_images = create_info->minImageCount; |
| 1834 | num_images = std::clamp(num_images, |
| 1835 | surface_capabilities2.surfaceCapabilities.minImageCount, |
| 1836 | surface_capabilities2.surfaceCapabilities.maxImageCount); |
Trevor David Black | 8c0122e | 2023-09-07 20:33:54 +0000 | [diff] [blame] | 1837 | |
| 1838 | const uint32_t buffer_count = std::max(min_buffer_count, num_images); |
| 1839 | err = native_window_set_buffer_count(window, buffer_count); |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1840 | if (err != android::OK) { |
Trevor David Black | 8c0122e | 2023-09-07 20:33:54 +0000 | [diff] [blame] | 1841 | ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", buffer_count, |
Jesse Hall | 3d1c82a | 2016-04-22 15:28:29 -0700 | [diff] [blame] | 1842 | strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1843 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1844 | } |
| 1845 | |
Ian Elliott | 12b7e2f | 2021-12-21 23:24:20 -0700 | [diff] [blame] | 1846 | // In shared mode the num_images must be one regardless of how many |
| 1847 | // buffers were allocated for the buffer queue. |
| 1848 | if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) { |
| 1849 | num_images = 1; |
| 1850 | } |
| 1851 | |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1852 | // Look through the create_info pNext chain passed to createSwapchainKHR |
| 1853 | // for an image compression control struct. |
| 1854 | // if one is found AND the appropriate extensions are enabled, create a |
| 1855 | // VkImageCompressionControlEXT structure to pass on to VkImageCreateInfo |
| 1856 | // TODO check for imageCompressionControlSwapchain feature is enabled |
Trevor David Black | 2cc4468 | 2022-03-09 00:31:38 +0000 | [diff] [blame] | 1857 | void* usage_info_pNext = nullptr; |
| 1858 | VkImageCompressionControlEXT image_compression = {}; |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1859 | const VkSwapchainCreateInfoKHR* create_infos = create_info; |
| 1860 | while (create_infos->pNext) { |
| 1861 | create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(create_infos->pNext); |
| 1862 | switch (create_infos->sType) { |
| 1863 | case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: { |
| 1864 | const VkImageCompressionControlEXT* compression_infos = |
| 1865 | reinterpret_cast<const VkImageCompressionControlEXT*>(create_infos); |
| 1866 | image_compression = *compression_infos; |
| 1867 | image_compression.pNext = nullptr; |
| 1868 | usage_info_pNext = &image_compression; |
| 1869 | } break; |
Trevor David Black | b6ca842 | 2023-07-26 20:00:04 +0000 | [diff] [blame] | 1870 | |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1871 | default: |
| 1872 | // Ignore all other info structs |
| 1873 | break; |
Trevor David Black | b6ca842 | 2023-07-26 20:00:04 +0000 | [diff] [blame] | 1874 | } |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1875 | } |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1876 | |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1877 | // Get the appropriate native_usage for the images |
| 1878 | // Get the consumer usage |
| 1879 | uint64_t native_usage = surface.consumer_usage; |
| 1880 | // Determine if the swapchain is protected |
| 1881 | bool create_protected_swapchain = false; |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1882 | if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) { |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1883 | create_protected_swapchain = true; |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1884 | native_usage |= BufferUsage::PROTECTED; |
| 1885 | } |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1886 | // Get the producer usage |
| 1887 | uint64_t producer_usage; |
| 1888 | result = getProducerUsage(device, create_info, swapchain_image_usage, create_protected_swapchain, &producer_usage); |
| 1889 | if (result != VK_SUCCESS) { |
| 1890 | return result; |
| 1891 | } |
| 1892 | native_usage |= producer_usage; |
| 1893 | |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1894 | err = native_window_set_usage(window, native_usage); |
| 1895 | if (err != android::OK) { |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1896 | ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 1897 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | 70f9335 | 2015-11-04 09:41:31 -0800 | [diff] [blame] | 1898 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1899 | |
Yiwei Zhang | 69395cd | 2019-07-03 16:55:39 -0700 | [diff] [blame] | 1900 | int transform_hint; |
Yiwei Zhang | f5030b4 | 2019-12-19 00:10:04 -0800 | [diff] [blame] | 1901 | err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint); |
| 1902 | if (err != android::OK) { |
Yiwei Zhang | 69395cd | 2019-07-03 16:55:39 -0700 | [diff] [blame] | 1903 | ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)", |
| 1904 | strerror(-err), err); |
| 1905 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1906 | } |
| 1907 | |
Alina Kalyakina | 3d6f363 | 2023-03-22 17:13:47 +0000 | [diff] [blame] | 1908 | int64_t refresh_duration; |
| 1909 | err = native_window_get_refresh_cycle_duration(window, &refresh_duration); |
| 1910 | if (err != android::OK) { |
| 1911 | ALOGE("native_window_get_refresh_cycle_duration query failed: %s (%d)", |
| 1912 | strerror(-err), err); |
| 1913 | return VK_ERROR_SURFACE_LOST_KHR; |
| 1914 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1915 | // -- Allocate our Swapchain object -- |
| 1916 | // After this point, we must deallocate the swapchain on error. |
| 1917 | |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 1918 | void* mem = allocator->pfnAllocation(allocator->pUserData, |
| 1919 | sizeof(Swapchain), alignof(Swapchain), |
| 1920 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1921 | |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 1922 | if (!mem) |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1923 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1924 | |
silence_dogood | 7359759 | 2019-05-23 16:57:37 -0700 | [diff] [blame] | 1925 | Swapchain* swapchain = new (mem) |
| 1926 | Swapchain(surface, num_images, create_info->presentMode, |
Alina Kalyakina | 3d6f363 | 2023-03-22 17:13:47 +0000 | [diff] [blame] | 1927 | TranslateVulkanToNativeTransform(create_info->preTransform), |
| 1928 | refresh_duration); |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 1929 | VkSwapchainImageCreateInfoANDROID swapchain_image_create = { |
| 1930 | #pragma clang diagnostic push |
| 1931 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 1932 | .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID, |
| 1933 | #pragma clang diagnostic pop |
Trevor David Black | 2cc4468 | 2022-03-09 00:31:38 +0000 | [diff] [blame] | 1934 | .pNext = usage_info_pNext, |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 1935 | .usage = swapchain_image_usage, |
| 1936 | }; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1937 | VkNativeBufferANDROID image_native_buffer = { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1938 | #pragma clang diagnostic push |
| 1939 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 1940 | .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID, |
| 1941 | #pragma clang diagnostic pop |
Chris Forbes | b56287a | 2017-01-12 14:28:58 +1300 | [diff] [blame] | 1942 | .pNext = &swapchain_image_create, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1943 | }; |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1944 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1945 | VkImageCreateInfo image_create = { |
| 1946 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1947 | .pNext = nullptr, |
Trevor David Black | d7320ef | 2023-08-23 21:21:51 +0000 | [diff] [blame] | 1948 | .flags = create_protected_swapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1949 | .imageType = VK_IMAGE_TYPE_2D, |
Jesse Hall | 517274a | 2016-02-10 00:07:18 -0800 | [diff] [blame] | 1950 | .format = create_info->imageFormat, |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1951 | .extent = { |
| 1952 | create_info->imageExtent.width, |
| 1953 | create_info->imageExtent.height, |
| 1954 | 1 |
| 1955 | }, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1956 | .mipLevels = 1, |
Jesse Hall | a15a4bf | 2015-11-19 22:48:02 -0800 | [diff] [blame] | 1957 | .arrayLayers = 1, |
Jesse Hall | 091ed9e | 2015-11-30 00:55:29 -0800 | [diff] [blame] | 1958 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1959 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1960 | .usage = create_info->imageUsage, |
Jesse Hall | f4ab2b1 | 2015-11-30 16:04:55 -0800 | [diff] [blame] | 1961 | .sharingMode = create_info->imageSharingMode, |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 1962 | .queueFamilyIndexCount = create_info->queueFamilyIndexCount, |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1963 | .pQueueFamilyIndices = create_info->pQueueFamilyIndices, |
| 1964 | }; |
| 1965 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1966 | // Note: don't do deferred allocation for shared present modes. There's only one buffer |
| 1967 | // involved so very little benefit. |
| 1968 | if ((create_info->flags & VK_SWAPCHAIN_CREATE_DEFERRED_MEMORY_ALLOCATION_BIT_EXT) && |
| 1969 | !IsSharedPresentMode(create_info->presentMode)) { |
| 1970 | // Don't want to touch the underlying gralloc buffers yet; |
| 1971 | // instead just create unbound VkImages which will later be bound to memory inside |
| 1972 | // AcquireNextImage. |
| 1973 | VkImageSwapchainCreateInfoKHR image_swapchain_create = { |
| 1974 | .sType = VK_STRUCTURE_TYPE_IMAGE_SWAPCHAIN_CREATE_INFO_KHR, |
| 1975 | .pNext = nullptr, |
| 1976 | .swapchain = HandleFromSwapchain(swapchain), |
| 1977 | }; |
| 1978 | image_create.pNext = &image_swapchain_create; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1979 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1980 | for (uint32_t i = 0; i < num_images; i++) { |
| 1981 | Swapchain::Image& img = swapchain->images[i]; |
| 1982 | img.buffer = nullptr; |
| 1983 | img.dequeued = false; |
| 1984 | |
| 1985 | result = dispatch.CreateImage(device, &image_create, nullptr, &img.image); |
| 1986 | if (result != VK_SUCCESS) { |
| 1987 | ALOGD("vkCreateImage w/ for deferred swapchain image failed: %u", result); |
| 1988 | break; |
Yiwei Zhang | 702beb4 | 2019-11-29 17:59:55 -0800 | [diff] [blame] | 1989 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1990 | } |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1991 | } else { |
| 1992 | // -- Dequeue all buffers and create a VkImage for each -- |
| 1993 | // Any failures during or after this must cancel the dequeued buffers. |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1994 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1995 | for (uint32_t i = 0; i < num_images; i++) { |
| 1996 | Swapchain::Image& img = swapchain->images[i]; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 1997 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 1998 | ANativeWindowBuffer* buffer; |
| 1999 | err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence); |
| 2000 | if (err != android::OK) { |
| 2001 | ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err); |
| 2002 | switch (-err) { |
| 2003 | case ENOMEM: |
| 2004 | result = VK_ERROR_OUT_OF_DEVICE_MEMORY; |
| 2005 | break; |
| 2006 | default: |
| 2007 | result = VK_ERROR_SURFACE_LOST_KHR; |
| 2008 | break; |
| 2009 | } |
| 2010 | break; |
| 2011 | } |
| 2012 | img.buffer = buffer; |
| 2013 | img.dequeued = true; |
| 2014 | |
| 2015 | image_native_buffer.handle = img.buffer->handle; |
| 2016 | image_native_buffer.stride = img.buffer->stride; |
| 2017 | image_native_buffer.format = img.buffer->format; |
| 2018 | image_native_buffer.usage = int(img.buffer->usage); |
| 2019 | android_convertGralloc0To1Usage(int(img.buffer->usage), |
| 2020 | &image_native_buffer.usage2.producer, |
| 2021 | &image_native_buffer.usage2.consumer); |
| 2022 | image_native_buffer.usage3 = img.buffer->usage; |
Trevor David Black | 0db7a09 | 2023-12-11 23:46:36 +0000 | [diff] [blame] | 2023 | image_native_buffer.ahb = |
| 2024 | ANativeWindowBuffer_getHardwareBuffer(img.buffer.get()); |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2025 | image_create.pNext = &image_native_buffer; |
| 2026 | |
| 2027 | ATRACE_BEGIN("CreateImage"); |
| 2028 | result = |
| 2029 | dispatch.CreateImage(device, &image_create, nullptr, &img.image); |
| 2030 | ATRACE_END(); |
| 2031 | if (result != VK_SUCCESS) { |
| 2032 | ALOGD("vkCreateImage w/ native buffer failed: %u", result); |
| 2033 | break; |
| 2034 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2035 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2036 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2037 | // -- Cancel all buffers, returning them to the queue -- |
| 2038 | // If an error occurred before, also destroy the VkImage and release the |
| 2039 | // buffer reference. Otherwise, we retain a strong reference to the buffer. |
| 2040 | for (uint32_t i = 0; i < num_images; i++) { |
| 2041 | Swapchain::Image& img = swapchain->images[i]; |
| 2042 | if (img.dequeued) { |
| 2043 | if (!swapchain->shared) { |
| 2044 | window->cancelBuffer(window, img.buffer.get(), |
| 2045 | img.dequeue_fence); |
| 2046 | img.dequeue_fence = -1; |
| 2047 | img.dequeued = false; |
| 2048 | } |
Chris Forbes | e0ced03 | 2017-03-30 19:44:15 +1300 | [diff] [blame] | 2049 | } |
Chris Forbes | 31b85c2 | 2018-05-29 15:03:28 -0700 | [diff] [blame] | 2050 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | if (result != VK_SUCCESS) { |
Yiwei Zhang | 533cea9 | 2019-06-03 18:43:24 -0700 | [diff] [blame] | 2054 | DestroySwapchainInternal(device, HandleFromSwapchain(swapchain), |
| 2055 | allocator); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2056 | return result; |
| 2057 | } |
| 2058 | |
Yiwei Zhang | 69395cd | 2019-07-03 16:55:39 -0700 | [diff] [blame] | 2059 | if (transform_hint != swapchain->pre_transform) { |
| 2060 | // Log that the app is not doing pre-rotation. |
| 2061 | android::GraphicsEnv::getInstance().setTargetStats( |
| 2062 | android::GpuStatsInfo::Stats::FALSE_PREROTATION); |
| 2063 | } |
| 2064 | |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 2065 | // Set stats for creating a Vulkan swapchain |
| 2066 | android::GraphicsEnv::getInstance().setTargetStats( |
| 2067 | android::GpuStatsInfo::Stats::CREATED_VULKAN_SWAPCHAIN); |
| 2068 | |
Yiwei Zhang | 3b88f31 | 2023-04-18 23:11:35 +0000 | [diff] [blame] | 2069 | surface.used_by_swapchain = true; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 2070 | surface.swapchain_handle = HandleFromSwapchain(swapchain); |
| 2071 | *swapchain_handle = surface.swapchain_handle; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 2072 | return VK_SUCCESS; |
| 2073 | } |
| 2074 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 2075 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 2076 | void DestroySwapchainKHR(VkDevice device, |
| 2077 | VkSwapchainKHR swapchain_handle, |
| 2078 | const VkAllocationCallbacks* allocator) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 2079 | ATRACE_CALL(); |
| 2080 | |
Yiwei Zhang | 533cea9 | 2019-06-03 18:43:24 -0700 | [diff] [blame] | 2081 | DestroySwapchainInternal(device, swapchain_handle, allocator); |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 2084 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 2085 | VkResult GetSwapchainImagesKHR(VkDevice, |
| 2086 | VkSwapchainKHR swapchain_handle, |
| 2087 | uint32_t* count, |
| 2088 | VkImage* images) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 2089 | ATRACE_CALL(); |
| 2090 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2091 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 2092 | ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle, |
| 2093 | "getting images for non-active swapchain 0x%" PRIx64 |
| 2094 | "; only dequeued image handles are valid", |
| 2095 | reinterpret_cast<uint64_t>(swapchain_handle)); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2096 | VkResult result = VK_SUCCESS; |
| 2097 | if (images) { |
| 2098 | uint32_t n = swapchain.num_images; |
| 2099 | if (*count < swapchain.num_images) { |
| 2100 | n = *count; |
| 2101 | result = VK_INCOMPLETE; |
| 2102 | } |
| 2103 | for (uint32_t i = 0; i < n; i++) |
| 2104 | images[i] = swapchain.images[i].image; |
Jesse Hall | 7331e22 | 2016-09-15 21:26:01 -0700 | [diff] [blame] | 2105 | *count = n; |
| 2106 | } else { |
| 2107 | *count = swapchain.num_images; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2108 | } |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2109 | return result; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 2112 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 2113 | VkResult AcquireNextImageKHR(VkDevice device, |
| 2114 | VkSwapchainKHR swapchain_handle, |
| 2115 | uint64_t timeout, |
| 2116 | VkSemaphore semaphore, |
| 2117 | VkFence vk_fence, |
| 2118 | uint32_t* image_index) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 2119 | ATRACE_CALL(); |
| 2120 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2121 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Jesse Hall | 1356b0d | 2015-11-23 17:24:58 -0800 | [diff] [blame] | 2122 | ANativeWindow* window = swapchain.surface.window.get(); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2123 | VkResult result; |
| 2124 | int err; |
| 2125 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 2126 | if (swapchain.surface.swapchain_handle != swapchain_handle) |
| 2127 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 2128 | |
Chris Forbes | c88409c | 2017-03-30 19:47:37 +1300 | [diff] [blame] | 2129 | if (swapchain.shared) { |
| 2130 | // In shared mode, we keep the buffer dequeued all the time, so we don't |
| 2131 | // want to dequeue a buffer here. Instead, just ask the driver to ensure |
| 2132 | // the semaphore and fence passed to us will be signalled. |
| 2133 | *image_index = 0; |
| 2134 | result = GetData(device).driver.AcquireImageANDROID( |
| 2135 | device, swapchain.images[*image_index].image, -1, semaphore, vk_fence); |
| 2136 | return result; |
| 2137 | } |
| 2138 | |
Yiwei Zhang | 705c2e6 | 2019-12-18 23:12:43 -0800 | [diff] [blame] | 2139 | const nsecs_t acquire_next_image_timeout = |
| 2140 | timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout; |
| 2141 | if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) { |
| 2142 | // Cache the timeout to avoid the duplicate binder cost. |
| 2143 | err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, |
| 2144 | acquire_next_image_timeout); |
| 2145 | if (err != android::OK) { |
| 2146 | ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)", |
| 2147 | strerror(-err), err); |
| 2148 | return VK_ERROR_SURFACE_LOST_KHR; |
| 2149 | } |
| 2150 | swapchain.acquire_next_image_timeout = acquire_next_image_timeout; |
| 2151 | } |
| 2152 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2153 | ANativeWindowBuffer* buffer; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 2154 | int fence_fd; |
| 2155 | err = window->dequeueBuffer(window, &buffer, &fence_fd); |
Yiwei Zhang | c0f8a2c | 2020-04-30 20:23:13 -0700 | [diff] [blame] | 2156 | if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) { |
Yiwei Zhang | 705c2e6 | 2019-12-18 23:12:43 -0800 | [diff] [blame] | 2157 | ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err); |
| 2158 | return timeout ? VK_TIMEOUT : VK_NOT_READY; |
| 2159 | } else if (err != android::OK) { |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2160 | ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err); |
Mike Stroyan | 762c813 | 2017-02-22 11:43:09 -0700 | [diff] [blame] | 2161 | return VK_ERROR_SURFACE_LOST_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | uint32_t idx; |
| 2165 | for (idx = 0; idx < swapchain.num_images; idx++) { |
| 2166 | if (swapchain.images[idx].buffer.get() == buffer) { |
| 2167 | swapchain.images[idx].dequeued = true; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 2168 | swapchain.images[idx].dequeue_fence = fence_fd; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2169 | break; |
| 2170 | } |
| 2171 | } |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2172 | |
| 2173 | // If this is a deferred alloc swapchain, this may be the first time we've |
| 2174 | // seen a particular buffer. If so, there should be an empty slot. Find it, |
| 2175 | // and bind the gralloc buffer to the VkImage for that slot. If there is no |
| 2176 | // empty slot, then we dequeued an unexpected buffer. Non-deferred swapchains |
| 2177 | // will also take this path, but will never have an empty slot since we |
| 2178 | // populated them all upfront. |
| 2179 | if (idx == swapchain.num_images) { |
| 2180 | for (idx = 0; idx < swapchain.num_images; idx++) { |
| 2181 | if (!swapchain.images[idx].buffer) { |
| 2182 | // Note: this structure is technically required for |
| 2183 | // Vulkan correctness, even though the driver is probably going |
| 2184 | // to use everything from the VkNativeBufferANDROID below. |
| 2185 | // This is kindof silly, but it's how we did the ANB |
| 2186 | // side of VK_KHR_swapchain v69, so we're stuck with it unless |
| 2187 | // we want to go tinkering with the ANB spec some more. |
| 2188 | VkBindImageMemorySwapchainInfoKHR bimsi = { |
| 2189 | .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR, |
| 2190 | .pNext = nullptr, |
| 2191 | .swapchain = swapchain_handle, |
| 2192 | .imageIndex = idx, |
| 2193 | }; |
| 2194 | VkNativeBufferANDROID nb = { |
| 2195 | .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID, |
| 2196 | .pNext = &bimsi, |
| 2197 | .handle = buffer->handle, |
| 2198 | .stride = buffer->stride, |
| 2199 | .format = buffer->format, |
| 2200 | .usage = int(buffer->usage), |
Trevor David Black | 0db7a09 | 2023-12-11 23:46:36 +0000 | [diff] [blame] | 2201 | .usage3 = buffer->usage, |
| 2202 | .ahb = ANativeWindowBuffer_getHardwareBuffer(buffer), |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2203 | }; |
Trevor David Black | 0db7a09 | 2023-12-11 23:46:36 +0000 | [diff] [blame] | 2204 | android_convertGralloc0To1Usage(int(buffer->usage), |
| 2205 | &nb.usage2.producer, |
| 2206 | &nb.usage2.consumer); |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2207 | VkBindImageMemoryInfo bimi = { |
| 2208 | .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO, |
| 2209 | .pNext = &nb, |
| 2210 | .image = swapchain.images[idx].image, |
| 2211 | .memory = VK_NULL_HANDLE, |
| 2212 | .memoryOffset = 0, |
| 2213 | }; |
| 2214 | result = GetData(device).driver.BindImageMemory2(device, 1, &bimi); |
| 2215 | if (result != VK_SUCCESS) { |
| 2216 | // This shouldn't really happen. If it does, something is probably |
| 2217 | // unrecoverably wrong with the swapchain and its images. Cancel |
| 2218 | // the buffer and declare the swapchain broken. |
| 2219 | ALOGE("failed to do deferred gralloc buffer bind"); |
| 2220 | window->cancelBuffer(window, buffer, fence_fd); |
| 2221 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 2222 | } |
| 2223 | |
| 2224 | swapchain.images[idx].dequeued = true; |
| 2225 | swapchain.images[idx].dequeue_fence = fence_fd; |
| 2226 | swapchain.images[idx].buffer = buffer; |
| 2227 | break; |
| 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | // The buffer doesn't match any slot. This shouldn't normally happen, but is |
| 2233 | // possible if the bufferqueue is reconfigured behind libvulkan's back. If this |
| 2234 | // happens, just declare the swapchain to be broken and the app will recreate it. |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2235 | if (idx == swapchain.num_images) { |
| 2236 | ALOGE("dequeueBuffer returned unrecognized buffer"); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 2237 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2238 | return VK_ERROR_OUT_OF_DATE_KHR; |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | int fence_clone = -1; |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 2242 | if (fence_fd != -1) { |
| 2243 | fence_clone = dup(fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2244 | if (fence_clone == -1) { |
| 2245 | ALOGE("dup(fence) failed, stalling until signalled: %s (%d)", |
| 2246 | strerror(errno), errno); |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 2247 | sync_wait(fence_fd, -1 /* forever */); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2248 | } |
| 2249 | } |
| 2250 | |
Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 2251 | result = GetData(device).driver.AcquireImageANDROID( |
Jesse Hall | 1f91d39 | 2015-12-11 16:28:44 -0800 | [diff] [blame] | 2252 | device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2253 | if (result != VK_SUCCESS) { |
Jesse Hall | ab9aeef | 2015-11-04 10:56:20 -0800 | [diff] [blame] | 2254 | // NOTE: we're relying on AcquireImageANDROID to close fence_clone, |
| 2255 | // even if the call fails. We could close it ourselves on failure, but |
| 2256 | // that would create a race condition if the driver closes it on a |
| 2257 | // failure path: some other thread might create an fd with the same |
| 2258 | // number between the time the driver closes it and the time we close |
| 2259 | // it. We must assume one of: the driver *always* closes it even on |
| 2260 | // failure, or *never* closes it on failure. |
Jesse Hall | 0619380 | 2015-12-03 16:12:51 -0800 | [diff] [blame] | 2261 | window->cancelBuffer(window, buffer, fence_fd); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2262 | swapchain.images[idx].dequeued = false; |
| 2263 | swapchain.images[idx].dequeue_fence = -1; |
| 2264 | return result; |
| 2265 | } |
| 2266 | |
| 2267 | *image_index = idx; |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 2268 | return VK_SUCCESS; |
| 2269 | } |
| 2270 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 2271 | VKAPI_ATTR |
| 2272 | VkResult AcquireNextImage2KHR(VkDevice device, |
| 2273 | const VkAcquireNextImageInfoKHR* pAcquireInfo, |
| 2274 | uint32_t* pImageIndex) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 2275 | ATRACE_CALL(); |
| 2276 | |
Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 2277 | return AcquireNextImageKHR(device, pAcquireInfo->swapchain, |
| 2278 | pAcquireInfo->timeout, pAcquireInfo->semaphore, |
| 2279 | pAcquireInfo->fence, pImageIndex); |
| 2280 | } |
| 2281 | |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 2282 | static VkResult WorstPresentResult(VkResult a, VkResult b) { |
| 2283 | // See the error ranking for vkQueuePresentKHR at the end of section 29.6 |
| 2284 | // (in spec version 1.0.14). |
| 2285 | static const VkResult kWorstToBest[] = { |
| 2286 | VK_ERROR_DEVICE_LOST, |
| 2287 | VK_ERROR_SURFACE_LOST_KHR, |
| 2288 | VK_ERROR_OUT_OF_DATE_KHR, |
| 2289 | VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 2290 | VK_ERROR_OUT_OF_HOST_MEMORY, |
| 2291 | VK_SUBOPTIMAL_KHR, |
| 2292 | }; |
| 2293 | for (auto result : kWorstToBest) { |
| 2294 | if (a == result || b == result) |
| 2295 | return result; |
| 2296 | } |
| 2297 | ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a); |
| 2298 | ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b); |
| 2299 | return a != VK_SUCCESS ? a : b; |
| 2300 | } |
| 2301 | |
Chris Forbes | 4cd01fb | 2022-10-19 11:35:16 +1300 | [diff] [blame] | 2302 | // KHR_incremental_present aspect of QueuePresentKHR |
| 2303 | static void SetSwapchainSurfaceDamage(ANativeWindow *window, const VkPresentRegionKHR *pRegion) { |
| 2304 | std::vector<android_native_rect_t> rects(pRegion->rectangleCount); |
| 2305 | for (auto i = 0u; i < pRegion->rectangleCount; i++) { |
| 2306 | auto const& rect = pRegion->pRectangles[i]; |
| 2307 | if (rect.layer > 0) { |
| 2308 | ALOGV("vkQueuePresentKHR ignoring invalid layer (%u); using layer 0 instead", |
| 2309 | rect.layer); |
| 2310 | } |
| 2311 | |
| 2312 | rects[i].left = rect.offset.x; |
| 2313 | rects[i].bottom = rect.offset.y; |
| 2314 | rects[i].right = rect.offset.x + rect.extent.width; |
| 2315 | rects[i].top = rect.offset.y + rect.extent.height; |
| 2316 | } |
| 2317 | native_window_set_surface_damage(window, rects.data(), rects.size()); |
| 2318 | } |
| 2319 | |
| 2320 | // GOOGLE_display_timing aspect of QueuePresentKHR |
| 2321 | static void SetSwapchainFrameTimestamp(Swapchain &swapchain, const VkPresentTimeGOOGLE *pTime) { |
| 2322 | ANativeWindow *window = swapchain.surface.window.get(); |
| 2323 | |
| 2324 | // We don't know whether the app will actually use GOOGLE_display_timing |
| 2325 | // with a particular swapchain until QueuePresent; enable it on the BQ |
| 2326 | // now if needed |
| 2327 | if (!swapchain.frame_timestamps_enabled) { |
| 2328 | ALOGV("Calling native_window_enable_frame_timestamps(true)"); |
| 2329 | native_window_enable_frame_timestamps(window, true); |
| 2330 | swapchain.frame_timestamps_enabled = true; |
| 2331 | } |
| 2332 | |
| 2333 | // Record the nativeFrameId so it can be later correlated to |
| 2334 | // this present. |
| 2335 | uint64_t nativeFrameId = 0; |
| 2336 | int err = native_window_get_next_frame_id( |
| 2337 | window, &nativeFrameId); |
| 2338 | if (err != android::OK) { |
| 2339 | ALOGE("Failed to get next native frame ID."); |
| 2340 | } |
| 2341 | |
| 2342 | // Add a new timing record with the user's presentID and |
| 2343 | // the nativeFrameId. |
| 2344 | swapchain.timing.emplace_back(pTime, nativeFrameId); |
| 2345 | if (swapchain.timing.size() > MAX_TIMING_INFOS) { |
| 2346 | swapchain.timing.erase( |
| 2347 | swapchain.timing.begin(), |
| 2348 | swapchain.timing.begin() + swapchain.timing.size() - MAX_TIMING_INFOS); |
| 2349 | } |
| 2350 | if (pTime->desiredPresentTime) { |
| 2351 | ALOGV( |
| 2352 | "Calling native_window_set_buffers_timestamp(%" PRId64 ")", |
| 2353 | pTime->desiredPresentTime); |
| 2354 | native_window_set_buffers_timestamp( |
| 2355 | window, |
| 2356 | static_cast<int64_t>(pTime->desiredPresentTime)); |
| 2357 | } |
| 2358 | } |
| 2359 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2360 | // EXT_swapchain_maintenance1 present mode change |
| 2361 | static bool SetSwapchainPresentMode(ANativeWindow *window, VkPresentModeKHR mode) { |
| 2362 | // There is no dynamic switching between non-shared present modes. |
| 2363 | // All we support is switching between demand and continuous refresh. |
| 2364 | if (!IsSharedPresentMode(mode)) |
| 2365 | return true; |
| 2366 | |
| 2367 | int err = native_window_set_auto_refresh(window, |
| 2368 | mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR); |
| 2369 | if (err != android::OK) { |
| 2370 | ALOGE("native_window_set_auto_refresh() failed: %s (%d)", |
| 2371 | strerror(-err), err); |
| 2372 | return false; |
| 2373 | } |
| 2374 | |
| 2375 | return true; |
| 2376 | } |
| 2377 | |
Chris Forbes | 4cd01fb | 2022-10-19 11:35:16 +1300 | [diff] [blame] | 2378 | static VkResult PresentOneSwapchain( |
| 2379 | VkQueue queue, |
| 2380 | Swapchain& swapchain, |
| 2381 | uint32_t imageIndex, |
| 2382 | const VkPresentRegionKHR *pRegion, |
| 2383 | const VkPresentTimeGOOGLE *pTime, |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2384 | VkFence presentFence, |
| 2385 | const VkPresentModeKHR *pPresentMode, |
Chris Forbes | 4cd01fb | 2022-10-19 11:35:16 +1300 | [diff] [blame] | 2386 | uint32_t waitSemaphoreCount, |
| 2387 | const VkSemaphore *pWaitSemaphores) { |
| 2388 | |
| 2389 | VkDevice device = GetData(queue).driver_device; |
| 2390 | const auto& dispatch = GetData(queue).driver; |
| 2391 | |
| 2392 | Swapchain::Image& img = swapchain.images[imageIndex]; |
| 2393 | VkResult swapchain_result = VK_SUCCESS; |
| 2394 | VkResult result; |
| 2395 | int err; |
| 2396 | |
| 2397 | // XXX: long standing issue: QueueSignalReleaseImageANDROID consumes the |
| 2398 | // wait semaphores, so this doesn't actually work for the multiple swapchain |
| 2399 | // case. |
| 2400 | int fence = -1; |
| 2401 | result = dispatch.QueueSignalReleaseImageANDROID( |
| 2402 | queue, waitSemaphoreCount, |
| 2403 | pWaitSemaphores, img.image, &fence); |
| 2404 | if (result != VK_SUCCESS) { |
| 2405 | ALOGE("QueueSignalReleaseImageANDROID failed: %d", result); |
| 2406 | swapchain_result = result; |
| 2407 | } |
| 2408 | if (img.release_fence >= 0) |
| 2409 | close(img.release_fence); |
| 2410 | img.release_fence = fence < 0 ? -1 : dup(fence); |
| 2411 | |
| 2412 | if (swapchain.surface.swapchain_handle == HandleFromSwapchain(&swapchain)) { |
| 2413 | ANativeWindow* window = swapchain.surface.window.get(); |
| 2414 | if (swapchain_result == VK_SUCCESS) { |
| 2415 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2416 | if (presentFence != VK_NULL_HANDLE) { |
| 2417 | int fence_copy = fence < 0 ? -1 : dup(fence); |
| 2418 | VkImportFenceFdInfoKHR iffi = { |
| 2419 | VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR, |
| 2420 | nullptr, |
| 2421 | presentFence, |
| 2422 | VK_FENCE_IMPORT_TEMPORARY_BIT, |
| 2423 | VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT, |
| 2424 | fence_copy, |
| 2425 | }; |
| 2426 | if (VK_SUCCESS != dispatch.ImportFenceFdKHR(device, &iffi) && fence_copy >= 0) { |
| 2427 | // ImportFenceFdKHR takes ownership only if it succeeds |
| 2428 | close(fence_copy); |
| 2429 | } |
| 2430 | } |
| 2431 | |
Chris Forbes | 4cd01fb | 2022-10-19 11:35:16 +1300 | [diff] [blame] | 2432 | if (pRegion) { |
| 2433 | SetSwapchainSurfaceDamage(window, pRegion); |
| 2434 | } |
| 2435 | if (pTime) { |
| 2436 | SetSwapchainFrameTimestamp(swapchain, pTime); |
| 2437 | } |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2438 | if (pPresentMode) { |
| 2439 | if (!SetSwapchainPresentMode(window, *pPresentMode)) |
| 2440 | swapchain_result = WorstPresentResult(swapchain_result, |
| 2441 | VK_ERROR_SURFACE_LOST_KHR); |
| 2442 | } |
Chris Forbes | 4cd01fb | 2022-10-19 11:35:16 +1300 | [diff] [blame] | 2443 | |
| 2444 | err = window->queueBuffer(window, img.buffer.get(), fence); |
| 2445 | // queueBuffer always closes fence, even on error |
| 2446 | if (err != android::OK) { |
| 2447 | ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err); |
| 2448 | swapchain_result = WorstPresentResult( |
| 2449 | swapchain_result, VK_ERROR_SURFACE_LOST_KHR); |
| 2450 | } else { |
| 2451 | if (img.dequeue_fence >= 0) { |
| 2452 | close(img.dequeue_fence); |
| 2453 | img.dequeue_fence = -1; |
| 2454 | } |
| 2455 | img.dequeued = false; |
| 2456 | } |
| 2457 | |
| 2458 | // If the swapchain is in shared mode, immediately dequeue the |
| 2459 | // buffer so it can be presented again without an intervening |
| 2460 | // call to AcquireNextImageKHR. We expect to get the same buffer |
| 2461 | // back from every call to dequeueBuffer in this mode. |
| 2462 | if (swapchain.shared && swapchain_result == VK_SUCCESS) { |
| 2463 | ANativeWindowBuffer* buffer; |
| 2464 | int fence_fd; |
| 2465 | err = window->dequeueBuffer(window, &buffer, &fence_fd); |
| 2466 | if (err != android::OK) { |
| 2467 | ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err); |
| 2468 | swapchain_result = WorstPresentResult(swapchain_result, |
| 2469 | VK_ERROR_SURFACE_LOST_KHR); |
| 2470 | } else if (img.buffer != buffer) { |
| 2471 | ALOGE("got wrong image back for shared swapchain"); |
| 2472 | swapchain_result = WorstPresentResult(swapchain_result, |
| 2473 | VK_ERROR_SURFACE_LOST_KHR); |
| 2474 | } else { |
| 2475 | img.dequeue_fence = fence_fd; |
| 2476 | img.dequeued = true; |
| 2477 | } |
| 2478 | } |
| 2479 | } |
| 2480 | if (swapchain_result != VK_SUCCESS) { |
| 2481 | OrphanSwapchain(device, &swapchain); |
| 2482 | } |
| 2483 | // Android will only return VK_SUBOPTIMAL_KHR for vkQueuePresentKHR, |
| 2484 | // and only when the window's transform/rotation changes. Extent |
| 2485 | // changes will not cause VK_SUBOPTIMAL_KHR because of the |
| 2486 | // application issues that were caused when the following transform |
| 2487 | // change was added. |
| 2488 | int window_transform_hint; |
| 2489 | err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, |
| 2490 | &window_transform_hint); |
| 2491 | if (err != android::OK) { |
| 2492 | ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)", |
| 2493 | strerror(-err), err); |
| 2494 | swapchain_result = WorstPresentResult( |
| 2495 | swapchain_result, VK_ERROR_SURFACE_LOST_KHR); |
| 2496 | } |
| 2497 | if (swapchain.pre_transform != window_transform_hint) { |
| 2498 | swapchain_result = |
| 2499 | WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR); |
| 2500 | } |
| 2501 | } else { |
| 2502 | ReleaseSwapchainImage(device, swapchain.shared, nullptr, fence, |
| 2503 | img, true); |
| 2504 | swapchain_result = VK_ERROR_OUT_OF_DATE_KHR; |
| 2505 | } |
| 2506 | |
| 2507 | return swapchain_result; |
| 2508 | } |
| 2509 | |
Jesse Hall | e1b1278 | 2015-11-30 11:27:32 -0800 | [diff] [blame] | 2510 | VKAPI_ATTR |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 2511 | VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 2512 | ATRACE_CALL(); |
| 2513 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2514 | ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
| 2515 | "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d", |
| 2516 | present_info->sType); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2517 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2518 | VkResult final_result = VK_SUCCESS; |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 2519 | |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 2520 | // Look at the pNext chain for supported extension structs: |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2521 | const VkPresentRegionsKHR* present_regions = nullptr; |
| 2522 | const VkPresentTimesInfoGOOGLE* present_times = nullptr; |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2523 | const VkSwapchainPresentFenceInfoEXT* present_fences = nullptr; |
| 2524 | const VkSwapchainPresentModeInfoEXT* present_modes = nullptr; |
| 2525 | |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 2526 | const VkPresentRegionsKHR* next = |
| 2527 | reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext); |
| 2528 | while (next) { |
| 2529 | switch (next->sType) { |
| 2530 | case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR: |
| 2531 | present_regions = next; |
| 2532 | break; |
Ian Elliott | 14866bb | 2017-01-20 09:15:48 -0700 | [diff] [blame] | 2533 | case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE: |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2534 | present_times = |
| 2535 | reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next); |
| 2536 | break; |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2537 | case VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_FENCE_INFO_EXT: |
| 2538 | present_fences = |
| 2539 | reinterpret_cast<const VkSwapchainPresentFenceInfoEXT*>(next); |
| 2540 | break; |
| 2541 | case VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_MODE_INFO_EXT: |
| 2542 | present_modes = |
| 2543 | reinterpret_cast<const VkSwapchainPresentModeInfoEXT*>(next); |
| 2544 | break; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 2545 | default: |
| 2546 | ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x", |
| 2547 | next->sType); |
| 2548 | break; |
| 2549 | } |
| 2550 | next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext); |
| 2551 | } |
| 2552 | ALOGV_IF( |
| 2553 | present_regions && |
| 2554 | present_regions->swapchainCount != present_info->swapchainCount, |
| 2555 | "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2556 | ALOGV_IF(present_times && |
| 2557 | present_times->swapchainCount != present_info->swapchainCount, |
| 2558 | "VkPresentTimesInfoGOOGLE::swapchainCount != " |
| 2559 | "VkPresentInfo::swapchainCount"); |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2560 | ALOGV_IF(present_fences && |
| 2561 | present_fences->swapchainCount != present_info->swapchainCount, |
| 2562 | "VkSwapchainPresentFenceInfoEXT::swapchainCount != " |
| 2563 | "VkPresentInfo::swapchainCount"); |
| 2564 | ALOGV_IF(present_modes && |
| 2565 | present_modes->swapchainCount != present_info->swapchainCount, |
| 2566 | "VkSwapchainPresentModeInfoEXT::swapchainCount != " |
| 2567 | "VkPresentInfo::swapchainCount"); |
| 2568 | |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 2569 | const VkPresentRegionKHR* regions = |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2570 | (present_regions) ? present_regions->pRegions : nullptr; |
| 2571 | const VkPresentTimeGOOGLE* times = |
| 2572 | (present_times) ? present_times->pTimes : nullptr; |
Ian Elliott | cb35113 | 2016-12-13 10:30:40 -0700 | [diff] [blame] | 2573 | |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2574 | for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) { |
| 2575 | Swapchain& swapchain = |
Jesse Hall | 03b6fe1 | 2015-11-24 12:44:21 -0800 | [diff] [blame] | 2576 | *SwapchainFromHandle(present_info->pSwapchains[sc]); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2577 | |
Chris Forbes | 4cd01fb | 2022-10-19 11:35:16 +1300 | [diff] [blame] | 2578 | VkResult swapchain_result = PresentOneSwapchain( |
| 2579 | queue, |
| 2580 | swapchain, |
| 2581 | present_info->pImageIndices[sc], |
| 2582 | (regions && !swapchain.mailbox_mode) ? ®ions[sc] : nullptr, |
| 2583 | times ? ×[sc] : nullptr, |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2584 | present_fences ? present_fences->pFences[sc] : VK_NULL_HANDLE, |
| 2585 | present_modes ? &present_modes->pPresentModes[sc] : nullptr, |
Chris Forbes | 4cd01fb | 2022-10-19 11:35:16 +1300 | [diff] [blame] | 2586 | present_info->waitSemaphoreCount, |
| 2587 | present_info->pWaitSemaphores); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2588 | |
Jesse Hall | a9e5703 | 2015-11-30 01:03:10 -0800 | [diff] [blame] | 2589 | if (present_info->pResults) |
Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 2590 | present_info->pResults[sc] = swapchain_result; |
| 2591 | |
| 2592 | if (swapchain_result != final_result) |
| 2593 | final_result = WorstPresentResult(final_result, swapchain_result); |
Jesse Hall | d7b994a | 2015-09-07 14:17:37 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | return final_result; |
| 2597 | } |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 2598 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2599 | VKAPI_ATTR |
| 2600 | VkResult GetRefreshCycleDurationGOOGLE( |
| 2601 | VkDevice, |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 2602 | VkSwapchainKHR swapchain_handle, |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2603 | VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 2604 | ATRACE_CALL(); |
| 2605 | |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 2606 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Alina Kalyakina | 3d6f363 | 2023-03-22 17:13:47 +0000 | [diff] [blame] | 2607 | VkResult result = swapchain.get_refresh_duration(pDisplayTimingProperties->refreshDuration); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2608 | |
| 2609 | return result; |
| 2610 | } |
| 2611 | |
| 2612 | VKAPI_ATTR |
| 2613 | VkResult GetPastPresentationTimingGOOGLE( |
| 2614 | VkDevice, |
| 2615 | VkSwapchainKHR swapchain_handle, |
| 2616 | uint32_t* count, |
| 2617 | VkPastPresentationTimingGOOGLE* timings) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 2618 | ATRACE_CALL(); |
| 2619 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2620 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Yiwei Zhang | 9d18783 | 2019-07-22 15:15:47 -0700 | [diff] [blame] | 2621 | if (swapchain.surface.swapchain_handle != swapchain_handle) { |
| 2622 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 2623 | } |
| 2624 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2625 | ANativeWindow* window = swapchain.surface.window.get(); |
| 2626 | VkResult result = VK_SUCCESS; |
| 2627 | |
| 2628 | if (!swapchain.frame_timestamps_enabled) { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 2629 | ALOGV("Calling native_window_enable_frame_timestamps(true)"); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2630 | native_window_enable_frame_timestamps(window, true); |
| 2631 | swapchain.frame_timestamps_enabled = true; |
| 2632 | } |
| 2633 | |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2634 | if (timings) { |
Yiwei Zhang | 9d18783 | 2019-07-22 15:15:47 -0700 | [diff] [blame] | 2635 | // Get the latest ready timing count before copying, since the copied |
| 2636 | // timing info will be erased in copy_ready_timings function. |
| 2637 | uint32_t n = get_num_ready_timings(swapchain); |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 2638 | copy_ready_timings(swapchain, count, timings); |
Yiwei Zhang | 9d18783 | 2019-07-22 15:15:47 -0700 | [diff] [blame] | 2639 | // Check the *count here against the recorded ready timing count, since |
| 2640 | // *count can be overwritten per spec describes. |
| 2641 | if (*count < n) { |
| 2642 | result = VK_INCOMPLETE; |
| 2643 | } |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2644 | } else { |
Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 2645 | *count = get_num_ready_timings(swapchain); |
Ian Elliott | 4c8bb2a | 2016-12-29 11:07:26 -0700 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | return result; |
| 2649 | } |
| 2650 | |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 2651 | VKAPI_ATTR |
| 2652 | VkResult GetSwapchainStatusKHR( |
| 2653 | VkDevice, |
Chris Forbes | 4e18ba8 | 2017-01-20 12:50:17 +1300 | [diff] [blame] | 2654 | VkSwapchainKHR swapchain_handle) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 2655 | ATRACE_CALL(); |
| 2656 | |
Chris Forbes | 4e18ba8 | 2017-01-20 12:50:17 +1300 | [diff] [blame] | 2657 | Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle); |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 2658 | VkResult result = VK_SUCCESS; |
| 2659 | |
Chris Forbes | 4e18ba8 | 2017-01-20 12:50:17 +1300 | [diff] [blame] | 2660 | if (swapchain.surface.swapchain_handle != swapchain_handle) { |
| 2661 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 2662 | } |
| 2663 | |
Yiwei Zhang | a885c06 | 2019-10-24 12:07:57 -0700 | [diff] [blame] | 2664 | // TODO(b/143296009): Implement this function properly |
Chris Forbes | 0f2ac2e | 2017-01-18 13:33:53 +1300 | [diff] [blame] | 2665 | |
| 2666 | return result; |
| 2667 | } |
| 2668 | |
Courtney Goeltzenleuchter | d634c48 | 2017-01-05 15:55:31 -0700 | [diff] [blame] | 2669 | VKAPI_ATTR void SetHdrMetadataEXT( |
Courtney Goeltzenleuchter | 7671d46 | 2018-01-24 11:51:01 -0800 | [diff] [blame] | 2670 | VkDevice, |
Courtney Goeltzenleuchter | d634c48 | 2017-01-05 15:55:31 -0700 | [diff] [blame] | 2671 | uint32_t swapchainCount, |
| 2672 | const VkSwapchainKHR* pSwapchains, |
| 2673 | const VkHdrMetadataEXT* pHdrMetadataEXTs) { |
Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 2674 | ATRACE_CALL(); |
Courtney Goeltzenleuchter | 7671d46 | 2018-01-24 11:51:01 -0800 | [diff] [blame] | 2675 | |
| 2676 | for (uint32_t idx = 0; idx < swapchainCount; idx++) { |
| 2677 | Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]); |
| 2678 | if (!swapchain) |
| 2679 | continue; |
| 2680 | |
| 2681 | if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue; |
| 2682 | |
| 2683 | ANativeWindow* window = swapchain->surface.window.get(); |
| 2684 | |
| 2685 | VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx]; |
| 2686 | const android_smpte2086_metadata smpteMetdata = { |
| 2687 | {vulkanMetadata.displayPrimaryRed.x, |
| 2688 | vulkanMetadata.displayPrimaryRed.y}, |
| 2689 | {vulkanMetadata.displayPrimaryGreen.x, |
| 2690 | vulkanMetadata.displayPrimaryGreen.y}, |
| 2691 | {vulkanMetadata.displayPrimaryBlue.x, |
| 2692 | vulkanMetadata.displayPrimaryBlue.y}, |
| 2693 | {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y}, |
| 2694 | vulkanMetadata.maxLuminance, |
| 2695 | vulkanMetadata.minLuminance}; |
| 2696 | native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata); |
| 2697 | |
| 2698 | const android_cta861_3_metadata cta8613Metadata = { |
| 2699 | vulkanMetadata.maxContentLightLevel, |
| 2700 | vulkanMetadata.maxFrameAverageLightLevel}; |
| 2701 | native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata); |
| 2702 | } |
| 2703 | |
Courtney Goeltzenleuchter | d634c48 | 2017-01-05 15:55:31 -0700 | [diff] [blame] | 2704 | return; |
| 2705 | } |
| 2706 | |
Yiwei Zhang | 0f47522 | 2019-04-11 19:38:00 -0700 | [diff] [blame] | 2707 | static void InterceptBindImageMemory2( |
| 2708 | uint32_t bind_info_count, |
| 2709 | const VkBindImageMemoryInfo* bind_infos, |
| 2710 | std::vector<VkNativeBufferANDROID>* out_native_buffers, |
| 2711 | std::vector<VkBindImageMemoryInfo>* out_bind_infos) { |
| 2712 | out_native_buffers->clear(); |
| 2713 | out_bind_infos->clear(); |
| 2714 | |
| 2715 | if (!bind_info_count) |
| 2716 | return; |
| 2717 | |
| 2718 | std::unordered_set<uint32_t> intercepted_indexes; |
| 2719 | |
| 2720 | for (uint32_t idx = 0; idx < bind_info_count; idx++) { |
| 2721 | auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>( |
| 2722 | bind_infos[idx].pNext); |
| 2723 | while (info && |
| 2724 | info->sType != |
| 2725 | VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) { |
| 2726 | info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>( |
| 2727 | info->pNext); |
| 2728 | } |
| 2729 | |
| 2730 | if (!info) |
| 2731 | continue; |
| 2732 | |
| 2733 | ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE, |
| 2734 | "swapchain handle must not be NULL"); |
| 2735 | const Swapchain* swapchain = SwapchainFromHandle(info->swapchain); |
| 2736 | ALOG_ASSERT( |
| 2737 | info->imageIndex < swapchain->num_images, |
| 2738 | "imageIndex must be less than the number of images in swapchain"); |
| 2739 | |
| 2740 | ANativeWindowBuffer* buffer = |
| 2741 | swapchain->images[info->imageIndex].buffer.get(); |
| 2742 | VkNativeBufferANDROID native_buffer = { |
| 2743 | #pragma clang diagnostic push |
| 2744 | #pragma clang diagnostic ignored "-Wold-style-cast" |
| 2745 | .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID, |
| 2746 | #pragma clang diagnostic pop |
| 2747 | .pNext = bind_infos[idx].pNext, |
| 2748 | .handle = buffer->handle, |
| 2749 | .stride = buffer->stride, |
| 2750 | .format = buffer->format, |
| 2751 | .usage = int(buffer->usage), |
Trevor David Black | 0db7a09 | 2023-12-11 23:46:36 +0000 | [diff] [blame] | 2752 | .usage3 = buffer->usage, |
| 2753 | .ahb = ANativeWindowBuffer_getHardwareBuffer(buffer), |
Yiwei Zhang | 0f47522 | 2019-04-11 19:38:00 -0700 | [diff] [blame] | 2754 | }; |
Trevor David Black | 0db7a09 | 2023-12-11 23:46:36 +0000 | [diff] [blame] | 2755 | android_convertGralloc0To1Usage(int(buffer->usage), |
| 2756 | &native_buffer.usage2.producer, |
| 2757 | &native_buffer.usage2.consumer); |
Yiwei Zhang | 0f47522 | 2019-04-11 19:38:00 -0700 | [diff] [blame] | 2758 | // Reserve enough space to avoid letting re-allocation invalidate the |
| 2759 | // addresses of the elements inside. |
| 2760 | out_native_buffers->reserve(bind_info_count); |
| 2761 | out_native_buffers->emplace_back(native_buffer); |
| 2762 | |
| 2763 | // Reserve the space now since we know how much is needed now. |
| 2764 | out_bind_infos->reserve(bind_info_count); |
| 2765 | out_bind_infos->emplace_back(bind_infos[idx]); |
| 2766 | out_bind_infos->back().pNext = &out_native_buffers->back(); |
| 2767 | |
| 2768 | intercepted_indexes.insert(idx); |
| 2769 | } |
| 2770 | |
| 2771 | if (intercepted_indexes.empty()) |
| 2772 | return; |
| 2773 | |
| 2774 | for (uint32_t idx = 0; idx < bind_info_count; idx++) { |
| 2775 | if (intercepted_indexes.count(idx)) |
| 2776 | continue; |
| 2777 | out_bind_infos->emplace_back(bind_infos[idx]); |
| 2778 | } |
| 2779 | } |
| 2780 | |
Yiwei Zhang | 2314310 | 2019-04-10 18:24:05 -0700 | [diff] [blame] | 2781 | VKAPI_ATTR |
| 2782 | VkResult BindImageMemory2(VkDevice device, |
| 2783 | uint32_t bindInfoCount, |
| 2784 | const VkBindImageMemoryInfo* pBindInfos) { |
| 2785 | ATRACE_CALL(); |
| 2786 | |
Yiwei Zhang | 0f47522 | 2019-04-11 19:38:00 -0700 | [diff] [blame] | 2787 | // out_native_buffers is for maintaining the lifecycle of the constructed |
| 2788 | // VkNativeBufferANDROID objects inside InterceptBindImageMemory2. |
| 2789 | std::vector<VkNativeBufferANDROID> out_native_buffers; |
| 2790 | std::vector<VkBindImageMemoryInfo> out_bind_infos; |
| 2791 | InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers, |
| 2792 | &out_bind_infos); |
| 2793 | return GetData(device).driver.BindImageMemory2( |
| 2794 | device, bindInfoCount, |
| 2795 | out_bind_infos.empty() ? pBindInfos : out_bind_infos.data()); |
Yiwei Zhang | 2314310 | 2019-04-10 18:24:05 -0700 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | VKAPI_ATTR |
| 2799 | VkResult BindImageMemory2KHR(VkDevice device, |
| 2800 | uint32_t bindInfoCount, |
| 2801 | const VkBindImageMemoryInfo* pBindInfos) { |
| 2802 | ATRACE_CALL(); |
| 2803 | |
Yiwei Zhang | 0f47522 | 2019-04-11 19:38:00 -0700 | [diff] [blame] | 2804 | std::vector<VkNativeBufferANDROID> out_native_buffers; |
| 2805 | std::vector<VkBindImageMemoryInfo> out_bind_infos; |
| 2806 | InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers, |
| 2807 | &out_bind_infos); |
| 2808 | return GetData(device).driver.BindImageMemory2KHR( |
| 2809 | device, bindInfoCount, |
| 2810 | out_bind_infos.empty() ? pBindInfos : out_bind_infos.data()); |
Yiwei Zhang | 2314310 | 2019-04-10 18:24:05 -0700 | [diff] [blame] | 2811 | } |
| 2812 | |
Chris Forbes | 9d0d9ff | 2022-12-28 01:58:31 +0000 | [diff] [blame] | 2813 | VKAPI_ATTR |
| 2814 | VkResult ReleaseSwapchainImagesEXT(VkDevice /*device*/, |
| 2815 | const VkReleaseSwapchainImagesInfoEXT* pReleaseInfo) { |
| 2816 | ATRACE_CALL(); |
| 2817 | |
| 2818 | Swapchain& swapchain = *SwapchainFromHandle(pReleaseInfo->swapchain); |
| 2819 | ANativeWindow* window = swapchain.surface.window.get(); |
| 2820 | |
| 2821 | // If in shared present mode, don't actually release the image back to the BQ. |
| 2822 | // Both sides share it forever. |
| 2823 | if (swapchain.shared) |
| 2824 | return VK_SUCCESS; |
| 2825 | |
| 2826 | for (uint32_t i = 0; i < pReleaseInfo->imageIndexCount; i++) { |
| 2827 | Swapchain::Image& img = swapchain.images[pReleaseInfo->pImageIndices[i]]; |
| 2828 | window->cancelBuffer(window, img.buffer.get(), img.dequeue_fence); |
| 2829 | |
| 2830 | // cancelBuffer has taken ownership of the dequeue fence |
| 2831 | img.dequeue_fence = -1; |
| 2832 | // if we're still holding a release fence, get rid of it now |
| 2833 | if (img.release_fence >= 0) { |
| 2834 | close(img.release_fence); |
| 2835 | img.release_fence = -1; |
| 2836 | } |
| 2837 | img.dequeued = false; |
| 2838 | } |
| 2839 | |
| 2840 | return VK_SUCCESS; |
| 2841 | } |
| 2842 | |
Chia-I Wu | 6226223 | 2016-03-26 07:06:44 +0800 | [diff] [blame] | 2843 | } // namespace driver |
Jesse Hall | b1352bc | 2015-09-04 16:12:33 -0700 | [diff] [blame] | 2844 | } // namespace vulkan |