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