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