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