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