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