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