blob: 09b0a145aff2b8271b01faf02917de996b657c46 [file] [log] [blame]
Jesse Hallb1352bc2015-09-04 16:12:33 -07001/*
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
Yuxin Huf768e042024-02-08 22:15:06 +000017#include "vulkan/vulkan_core.h"
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
sergiuferentz47989332023-09-26 10:24:36 +000020#include <aidl/android/hardware/graphics/common/Dataspace.h>
Trevor David Blackf499b5a2023-07-14 17:30:41 +000021#include <aidl/android/hardware/graphics/common/PixelFormat.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070022#include <android/hardware/graphics/common/1.0/types.h>
Trevor David Blackd7320ef2023-08-23 21:21:51 +000023#include <android/hardware_buffer.h>
Jesse Hall79927812017-03-23 11:03:23 -070024#include <grallocusage/GrallocUsageConversion.h>
Yiwei Zhang69395cd2019-07-03 16:55:39 -070025#include <graphicsenv/GraphicsEnv.h>
John Reck97678d42022-08-23 11:24:51 -040026#include <hardware/gralloc.h>
27#include <hardware/gralloc1.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070028#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070029#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070030#include <system/window.h>
31#include <ui/BufferQueueDefs.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080032#include <utils/StrongPointer.h>
Yiwei Zhang705c2e62019-12-18 23:12:43 -080033#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080034#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070035
36#include <algorithm>
37#include <unordered_set>
38#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070039
Chia-I Wu4a6a9162016-03-26 07:17:34 +080040#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070041
Trevor David Blackf499b5a2023-07-14 17:30:41 +000042using PixelFormat = aidl::android::hardware::graphics::common::PixelFormat;
sergiuferentz47989332023-09-26 10:24:36 +000043using DataSpace = aidl::android::hardware::graphics::common::Dataspace;
Daniel Kochf25f5bb2017-10-05 00:26:58 -040044using android::hardware::graphics::common::V1_0::BufferUsage;
45
Chia-I Wu62262232016-03-26 07:06:44 +080046namespace vulkan {
47namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070048
Jesse Halld7b994a2015-09-07 14:17:37 -070049namespace {
50
John Reck97678d42022-08-23 11:24:51 -040051static uint64_t convertGralloc1ToBufferUsage(uint64_t producerUsage,
52 uint64_t consumerUsage) {
53 static_assert(uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) ==
54 uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN),
55 "expected ConsumerUsage and ProducerUsage CPU_READ_OFTEN "
56 "bits to match");
57 uint64_t merged = producerUsage | consumerUsage;
58 if ((merged & (GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN)) ==
59 GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) {
60 merged &= ~uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN);
61 merged |= BufferUsage::CPU_READ_OFTEN;
62 }
63 if ((merged & (GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) ==
64 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) {
65 merged &= ~uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN);
66 merged |= BufferUsage::CPU_WRITE_OFTEN;
67 }
68 return merged;
69}
70
Jesse Hall55bc0972016-02-23 16:43:29 -080071const VkSurfaceTransformFlagsKHR kSupportedTransforms =
72 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
73 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
74 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
75 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070076 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
77 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
78 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
79 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080080 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
81
82VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
83 // Native and Vulkan transforms are isomorphic, but are represented
84 // differently. Vulkan transforms are built up of an optional horizontal
85 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
86 // transforms are built up from a horizontal flip, vertical flip, and
87 // 90-degree rotation, all optional but always in that order.
88
Jesse Hall55bc0972016-02-23 16:43:29 -080089 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070090 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080091 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070092 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
93 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
94 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
95 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
96 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080097 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070098 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080099 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700100 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
101 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
102 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
103 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
104 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -0800105 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
106 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
107 default:
108 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
109 }
110}
111
Yiwei Zhang70a21962019-05-31 17:26:52 -0700112int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
113 switch (transform) {
114 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
115 return NATIVE_WINDOW_TRANSFORM_ROT_90;
116 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
117 return NATIVE_WINDOW_TRANSFORM_ROT_180;
118 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
119 return NATIVE_WINDOW_TRANSFORM_ROT_270;
120 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
121 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
122 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
123 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
124 NATIVE_WINDOW_TRANSFORM_ROT_90;
125 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
126 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
127 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
128 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
129 NATIVE_WINDOW_TRANSFORM_ROT_90;
130 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
131 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
132 default:
133 return 0;
134 }
135}
136
Jesse Hall178b6962016-02-24 15:39:50 -0800137int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
138 switch (transform) {
139 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
140 return NATIVE_WINDOW_TRANSFORM_ROT_270;
141 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
142 return NATIVE_WINDOW_TRANSFORM_ROT_180;
143 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
144 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700145 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
146 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
147 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
148 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
149 NATIVE_WINDOW_TRANSFORM_ROT_90;
150 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
151 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
152 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
153 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
154 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800155 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
156 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
157 default:
158 return 0;
159 }
160}
161
Yuxin Hu7facd572024-02-08 22:54:23 +0000162const static VkColorSpaceKHR colorSpaceSupportedByVkEXTSwapchainColorspace[] = {
163 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT,
164 VK_COLOR_SPACE_DISPLAY_P3_LINEAR_EXT,
165 VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT,
166 VK_COLOR_SPACE_BT709_LINEAR_EXT,
167 VK_COLOR_SPACE_BT709_NONLINEAR_EXT,
168 VK_COLOR_SPACE_BT2020_LINEAR_EXT,
169 VK_COLOR_SPACE_HDR10_ST2084_EXT,
170 VK_COLOR_SPACE_HDR10_HLG_EXT,
171 VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT,
172 VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT,
173 VK_COLOR_SPACE_PASS_THROUGH_EXT,
174 VK_COLOR_SPACE_DCI_P3_LINEAR_EXT};
175
176const static VkColorSpaceKHR
177 colorSpaceSupportedByVkEXTSwapchainColorspaceOnFP16SurfaceOnly[] = {
178 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT,
179 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT};
180
Ian Elliott8a977262017-01-19 09:05:58 -0700181class TimingInfo {
182 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800183 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700184 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800185 native_frame_id_(nativeFrameId) {}
186 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700187 return (timestamp_desired_present_time_ !=
188 NATIVE_WINDOW_TIMESTAMP_PENDING &&
189 timestamp_actual_present_time_ !=
190 NATIVE_WINDOW_TIMESTAMP_PENDING &&
191 timestamp_render_complete_time_ !=
192 NATIVE_WINDOW_TIMESTAMP_PENDING &&
193 timestamp_composition_latch_time_ !=
194 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700195 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700196 void calculate(int64_t rdur) {
197 bool anyTimestampInvalid =
198 (timestamp_actual_present_time_ ==
199 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
200 (timestamp_render_complete_time_ ==
201 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
202 (timestamp_composition_latch_time_ ==
203 NATIVE_WINDOW_TIMESTAMP_INVALID);
204 if (anyTimestampInvalid) {
205 ALOGE("Unexpectedly received invalid timestamp.");
206 vals_.actualPresentTime = 0;
207 vals_.earliestPresentTime = 0;
208 vals_.presentMargin = 0;
209 return;
210 }
211
212 vals_.actualPresentTime =
213 static_cast<uint64_t>(timestamp_actual_present_time_);
214 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700215 timestamp_render_complete_time_);
216 // Calculate vals_.earliestPresentTime, and potentially adjust
217 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
218 // is vals_.actualPresentTime. If we can subtract rdur (the duration
219 // of a refresh cycle) from vals_.earliestPresentTime (and also from
220 // vals_.presentMargin) and still leave a positive margin, then we can
221 // report to the application that it could have presented earlier than
222 // it did (per the extension specification). If for some reason, we
223 // can do this subtraction repeatedly, we do, since
224 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700225 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700226 while ((margin > rdur) &&
227 ((early_time - rdur) > timestamp_composition_latch_time_)) {
228 early_time -= rdur;
229 margin -= rdur;
230 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700231 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
232 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700233 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800234 void get_values(VkPastPresentationTimingGOOGLE* values) const {
235 *values = vals_;
236 }
Ian Elliott8a977262017-01-19 09:05:58 -0700237
238 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800239 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700240
Brian Anderson1049d1d2016-12-16 17:25:57 -0800241 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700242 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
243 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
244 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
245 int64_t timestamp_composition_latch_time_
246 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700247};
248
Jesse Hall1356b0d2015-11-23 17:24:58 -0800249struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800250 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700251 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700252 uint64_t consumer_usage;
Yiwei Zhang3b88f312023-04-18 23:11:35 +0000253
254 // Indicate whether this surface has been used by a swapchain, no matter the
255 // swapchain is still current or has been destroyed.
256 bool used_by_swapchain;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800257};
258
259VkSurfaceKHR HandleFromSurface(Surface* surface) {
260 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
261}
262
263Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800264 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800265}
266
Ian Elliott8a977262017-01-19 09:05:58 -0700267// Maximum number of TimingInfo structs to keep per swapchain:
268enum { MAX_TIMING_INFOS = 10 };
269// Minimum number of frames to look for in the past (so we don't cause
270// syncronous requests to Surface Flinger):
271enum { MIN_NUM_FRAMES_AGO = 5 };
272
Chris Forbes4cd01fb2022-10-19 11:35:16 +1300273bool IsSharedPresentMode(VkPresentModeKHR mode) {
274 return mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
275 mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR;
276}
277
Jesse Hall1356b0d2015-11-23 17:24:58 -0800278struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700279 Swapchain(Surface& surface_,
280 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700281 VkPresentModeKHR present_mode,
Alina Kalyakina3d6f3632023-03-22 17:13:47 +0000282 int pre_transform_,
283 int64_t refresh_duration_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700284 : surface(surface_),
285 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700286 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700287 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300288 frame_timestamps_enabled(false),
Alina Kalyakina3d6f3632023-03-22 17:13:47 +0000289 refresh_duration(refresh_duration_),
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800290 acquire_next_image_timeout(-1),
Chris Forbes4cd01fb2022-10-19 11:35:16 +1300291 shared(IsSharedPresentMode(present_mode)) {
Ian Elliott8a977262017-01-19 09:05:58 -0700292 }
Alina Kalyakina3d6f3632023-03-22 17:13:47 +0000293
294 VkResult get_refresh_duration(uint64_t& outRefreshDuration)
Ian Elliott3568bfe2019-05-03 15:54:46 -0600295 {
296 ANativeWindow* window = surface.window.get();
Alina Kalyakina3d6f3632023-03-22 17:13:47 +0000297 int err = native_window_get_refresh_cycle_duration(
Ian Elliott3568bfe2019-05-03 15:54:46 -0600298 window,
299 &refresh_duration);
Alina Kalyakina3d6f3632023-03-22 17:13:47 +0000300 if (err != android::OK) {
301 ALOGE("%s:native_window_get_refresh_cycle_duration failed: %s (%d)",
302 __func__, strerror(-err), err );
303 return VK_ERROR_SURFACE_LOST_KHR;
304 }
305 outRefreshDuration = refresh_duration;
306 return VK_SUCCESS;
Ian Elliott3568bfe2019-05-03 15:54:46 -0600307 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800308
309 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700310 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700311 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700312 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700313 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700314 int64_t refresh_duration;
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800315 nsecs_t acquire_next_image_timeout;
Chris Forbesf8835642017-03-30 19:31:40 +1300316 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700317
318 struct Image {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700319 Image()
320 : image(VK_NULL_HANDLE),
321 dequeue_fence(-1),
322 release_fence(-1),
323 dequeued(false) {}
Jesse Halld7b994a2015-09-07 14:17:37 -0700324 VkImage image;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000325 // If the image is bound to memory, an sp to the underlying gralloc buffer.
326 // Otherwise, nullptr; the image will be bound to memory as part of
327 // AcquireNextImage.
Chia-I Wue8e689f2016-04-18 08:21:31 +0800328 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700329 // The fence is only valid when the buffer is dequeued, and should be
330 // -1 any other time. When valid, we own the fd, and must ensure it is
331 // closed: either by closing it explicitly when queueing the buffer,
332 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
333 int dequeue_fence;
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700334 // This fence is a dup of the sync fd returned from the driver via
335 // vkQueueSignalReleaseImageANDROID upon vkQueuePresentKHR. We must
336 // ensure it is closed upon re-presenting or releasing the image.
337 int release_fence;
Jesse Halld7b994a2015-09-07 14:17:37 -0700338 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800339 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700340
Yiwei Zhang5e862202019-06-21 14:59:16 -0700341 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700342};
343
344VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
345 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
346}
347
348Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800349 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700350}
351
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700352static bool IsFencePending(int fd) {
353 if (fd < 0)
354 return false;
355
356 errno = 0;
357 return sync_wait(fd, 0 /* timeout */) == -1 && errno == ETIME;
358}
359
Jesse Halldc225072016-05-30 22:40:14 -0700360void ReleaseSwapchainImage(VkDevice device,
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000361 bool shared_present,
Jesse Halldc225072016-05-30 22:40:14 -0700362 ANativeWindow* window,
363 int release_fence,
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700364 Swapchain::Image& image,
365 bool defer_if_pending) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700366 ATRACE_CALL();
367
Jesse Halldc225072016-05-30 22:40:14 -0700368 ALOG_ASSERT(release_fence == -1 || image.dequeued,
369 "ReleaseSwapchainImage: can't provide a release fence for "
370 "non-dequeued images");
371
372 if (image.dequeued) {
373 if (release_fence >= 0) {
374 // We get here from vkQueuePresentKHR. The application is
375 // responsible for creating an execution dependency chain from
376 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
377 // (release_fence), so we can drop the dequeue_fence here.
378 if (image.dequeue_fence >= 0)
379 close(image.dequeue_fence);
380 } else {
381 // We get here during swapchain destruction, or various serious
382 // error cases e.g. when we can't create the release_fence during
383 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
384 // have already signalled, since the swapchain images are supposed
385 // to be idle before the swapchain is destroyed. In error cases,
386 // there may be rendering in flight to the image, but since we
387 // weren't able to create a release_fence, waiting for the
388 // dequeue_fence is about the best we can do.
389 release_fence = image.dequeue_fence;
390 }
391 image.dequeue_fence = -1;
392
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000393 // It's invalid to call cancelBuffer on a shared buffer
394 if (window && !shared_present) {
Jesse Halldc225072016-05-30 22:40:14 -0700395 window->cancelBuffer(window, image.buffer.get(), release_fence);
396 } else {
397 if (release_fence >= 0) {
398 sync_wait(release_fence, -1 /* forever */);
399 close(release_fence);
400 }
401 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700402 release_fence = -1;
Jesse Halldc225072016-05-30 22:40:14 -0700403 image.dequeued = false;
404 }
405
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700406 if (defer_if_pending && IsFencePending(image.release_fence))
407 return;
408
409 if (image.release_fence >= 0) {
410 close(image.release_fence);
411 image.release_fence = -1;
412 }
413
Jesse Halldc225072016-05-30 22:40:14 -0700414 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700415 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700416 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700417 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700418 image.image = VK_NULL_HANDLE;
419 }
420
421 image.buffer.clear();
422}
423
424void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
425 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
426 return;
Jesse Halldc225072016-05-30 22:40:14 -0700427 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000428 if (!swapchain->images[i].dequeued) {
429 ReleaseSwapchainImage(device, swapchain->shared, nullptr, -1,
430 swapchain->images[i], true);
431 }
Jesse Halldc225072016-05-30 22:40:14 -0700432 }
433 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700434 swapchain->timing.clear();
435}
436
437uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800438 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
439 return 0;
440 }
Ian Elliott8a977262017-01-19 09:05:58 -0700441
Brian Anderson1049d1d2016-12-16 17:25:57 -0800442 uint32_t num_ready = 0;
443 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
444 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700445 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800446 if (ti.ready()) {
447 // This TimingInfo is ready to be reported to the user. Add it
448 // to the num_ready.
449 num_ready++;
450 continue;
451 }
452 // This TimingInfo is not yet ready to be reported to the user,
453 // and so we should look for any available timestamps that
454 // might make it ready.
455 int64_t desired_present_time = 0;
456 int64_t render_complete_time = 0;
457 int64_t composition_latch_time = 0;
458 int64_t actual_present_time = 0;
459 // Obtain timestamps:
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800460 int err = native_window_get_frame_timestamps(
Brian Anderson1049d1d2016-12-16 17:25:57 -0800461 swapchain.surface.window.get(), ti.native_frame_id_,
462 &desired_present_time, &render_complete_time,
463 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700464 nullptr, //&first_composition_start_time,
465 nullptr, //&last_composition_start_time,
466 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800467 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700468 nullptr, //&dequeue_ready_time,
469 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800470
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800471 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800472 continue;
473 }
474
475 // Record the timestamp(s) we received, and then see if this TimingInfo
476 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700477 ti.timestamp_desired_present_time_ = desired_present_time;
478 ti.timestamp_actual_present_time_ = actual_present_time;
479 ti.timestamp_render_complete_time_ = render_complete_time;
480 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800481
482 if (ti.ready()) {
483 // The TimingInfo has received enough timestamps, and should now
484 // use those timestamps to calculate the info that should be
485 // reported to the user:
486 ti.calculate(swapchain.refresh_duration);
487 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700488 }
489 }
490 return num_ready;
491}
492
Ian Elliott8a977262017-01-19 09:05:58 -0700493void copy_ready_timings(Swapchain& swapchain,
494 uint32_t* count,
495 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800496 if (swapchain.timing.empty()) {
497 *count = 0;
498 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700499 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800500
501 size_t last_ready = swapchain.timing.size() - 1;
502 while (!swapchain.timing[last_ready].ready()) {
503 if (last_ready == 0) {
504 *count = 0;
505 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700506 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800507 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700508 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800509
510 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700511 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800512 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
513 const TimingInfo& ti = swapchain.timing[i];
514 if (ti.ready()) {
515 ti.get_values(&timings[num_copied]);
516 num_copied++;
517 }
518 num_to_remove++;
519 }
520
521 // Discard old frames that aren't ready if newer frames are ready.
522 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700523 swapchain.timing.erase(swapchain.timing.begin(),
524 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800525
Ian Elliott8a977262017-01-19 09:05:58 -0700526 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700527}
528
Android Culprit Assistante9d915d2024-05-01 10:19:50 +0000529PixelFormat GetNativePixelFormat(VkFormat format) {
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000530 PixelFormat native_format = PixelFormat::RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700531 switch (format) {
532 case VK_FORMAT_R8G8B8A8_UNORM:
533 case VK_FORMAT_R8G8B8A8_SRGB:
Android Culprit Assistante9d915d2024-05-01 10:19:50 +0000534 native_format = PixelFormat::RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700535 break;
536 case VK_FORMAT_R5G6B5_UNORM_PACK16:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000537 native_format = PixelFormat::RGB_565;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700538 break;
539 case VK_FORMAT_R16G16B16A16_SFLOAT:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000540 native_format = PixelFormat::RGBA_FP16;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700541 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800542 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000543 native_format = PixelFormat::RGBA_1010102;
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500544 break;
545 case VK_FORMAT_R8_UNORM:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000546 native_format = PixelFormat::R_8;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700547 break;
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000548 case VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000549 native_format = PixelFormat::RGBA_10101010;
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000550 break;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700551 default:
552 ALOGV("unsupported swapchain format %d", format);
553 break;
554 }
555 return native_format;
556}
557
Yiwei Zhang959f5182024-03-12 02:46:36 +0000558DataSpace GetNativeDataspace(VkColorSpaceKHR colorspace, VkFormat format) {
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700559 switch (colorspace) {
560 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
sergiuferentz47989332023-09-26 10:24:36 +0000561 return DataSpace::SRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700562 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000563 return DataSpace::DISPLAY_P3;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700564 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000565 return DataSpace::SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600566 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000567 return DataSpace::SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700568 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000569 return DataSpace::DCI_P3_LINEAR;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700570 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000571 return DataSpace::DCI_P3;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700572 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000573 return DataSpace::SRGB_LINEAR;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700574 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000575 return DataSpace::SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600576 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
Yiwei Zhang959f5182024-03-12 02:46:36 +0000577 if (format == VK_FORMAT_R16G16B16A16_SFLOAT) {
sergiuferentz47989332023-09-26 10:24:36 +0000578 return DataSpace::BT2020_LINEAR_EXTENDED;
Sally Qiaa238972023-07-17 12:52:05 +0800579 } else {
sergiuferentz47989332023-09-26 10:24:36 +0000580 return DataSpace::BT2020_LINEAR;
Sally Qiaa238972023-07-17 12:52:05 +0800581 }
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600582 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000583 return DataSpace::BT2020_PQ;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600584 case VK_COLOR_SPACE_DOLBYVISION_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000585 return DataSpace::BT2020_PQ;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600586 case VK_COLOR_SPACE_HDR10_HLG_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000587 return DataSpace::BT2020_HLG;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700588 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000589 return DataSpace::ADOBE_RGB_LINEAR;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700590 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000591 return DataSpace::ADOBE_RGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700592 // Pass through is intended to allow app to provide data that is passed
593 // to the display system without modification.
594 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
sergiuferentz47989332023-09-26 10:24:36 +0000595 return DataSpace::ARBITRARY;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700596
597 default:
598 // This indicates that we don't know about the
599 // dataspace specified and we should indicate that
600 // it's unsupported
sergiuferentz47989332023-09-26 10:24:36 +0000601 return DataSpace::UNKNOWN;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700602 }
603}
604
Jesse Halld7b994a2015-09-07 14:17:37 -0700605} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700606
Jesse Halle1b12782015-11-30 11:27:32 -0800607VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800608VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800609 VkInstance instance,
610 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
611 const VkAllocationCallbacks* allocator,
612 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800613 ATRACE_CALL();
614
Jesse Hall1f91d392015-12-11 16:28:44 -0800615 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800616 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800617 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
618 alignof(Surface),
619 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800620 if (!mem)
621 return VK_ERROR_OUT_OF_HOST_MEMORY;
622 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700623
Android Culprit Assistante9d915d2024-05-01 10:19:50 +0000624 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700625 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang3b88f312023-04-18 23:11:35 +0000626 surface->used_by_swapchain = false;
Android Culprit Assistante9d915d2024-05-01 10:19:50 +0000627 int err = native_window_get_consumer_usage(surface->window.get(),
628 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800629 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700630 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
631 strerror(-err), err);
632 surface->~Surface();
633 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700634 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700635 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700636
Android Culprit Assistante9d915d2024-05-01 10:19:50 +0000637 err =
638 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800639 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800640 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
641 err);
642 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800643 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700644 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800645 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700646
Jesse Hall1356b0d2015-11-23 17:24:58 -0800647 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700648 return VK_SUCCESS;
649}
650
Jesse Halle1b12782015-11-30 11:27:32 -0800651VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800652void DestroySurfaceKHR(VkInstance instance,
653 VkSurfaceKHR surface_handle,
654 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800655 ATRACE_CALL();
656
Jesse Hall1356b0d2015-11-23 17:24:58 -0800657 Surface* surface = SurfaceFromHandle(surface_handle);
658 if (!surface)
659 return;
660 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700661 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700662 "destroyed VkSurfaceKHR 0x%" PRIx64
663 " has active VkSwapchainKHR 0x%" PRIx64,
664 reinterpret_cast<uint64_t>(surface_handle),
665 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800666 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800667 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800668 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800669 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800670}
671
Jesse Halle1b12782015-11-30 11:27:32 -0800672VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800673VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
674 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000675 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800676 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000677 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800678 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800679}
680
Jesse Halle1b12782015-11-30 11:27:32 -0800681VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800682VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Ian Elliott1ce053f2022-03-16 09:49:53 -0600683 VkPhysicalDevice pdev,
Jesse Hallb00daad2015-11-29 19:46:20 -0800684 VkSurfaceKHR surface,
685 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800686 ATRACE_CALL();
687
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000688 // Implement in terms of GetPhysicalDeviceSurfaceCapabilities2KHR
689
690 VkPhysicalDeviceSurfaceInfo2KHR info2 = {
691 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR,
692 nullptr,
693 surface
694 };
695
696 VkSurfaceCapabilities2KHR caps2 = {
697 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR,
698 nullptr,
699 {},
700 };
701
702 VkResult result = GetPhysicalDeviceSurfaceCapabilities2KHR(pdev, &info2, &caps2);
703 *capabilities = caps2.surfaceCapabilities;
704 return result;
705}
706
707// Does the call-twice and VK_INCOMPLETE handling for querying lists
708// of things, where we already have the full set built in a vector.
709template <typename T>
710VkResult CopyWithIncomplete(std::vector<T> const& things,
711 T* callerPtr, uint32_t* callerCount) {
712 VkResult result = VK_SUCCESS;
713 if (callerPtr) {
714 if (things.size() > *callerCount)
715 result = VK_INCOMPLETE;
716 *callerCount = std::min(uint32_t(things.size()), *callerCount);
717 std::copy(things.begin(), things.begin() + *callerCount, callerPtr);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600718 } else {
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000719 *callerCount = things.size();
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800720 }
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000721 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700722}
723
Jesse Halle1b12782015-11-30 11:27:32 -0800724VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700725VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
726 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800727 uint32_t* count,
728 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800729 ATRACE_CALL();
730
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700731 const InstanceData& instance_data = GetData(pdev);
732
Ian Elliott1ce053f2022-03-16 09:49:53 -0600733 uint64_t consumer_usage = 0;
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000734 bool colorspace_ext =
Ian Elliottf6df08e2022-03-16 21:27:49 -0600735 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600736 if (surface_handle == VK_NULL_HANDLE) {
737 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
738 bool surfaceless_enabled =
739 instance_data.hook_extensions.test(surfaceless);
740 if (!surfaceless_enabled) {
741 return VK_ERROR_SURFACE_LOST_KHR;
742 }
Chris Forbesb1de3b12022-10-20 09:05:48 +1300743 // Support for VK_GOOGLE_surfaceless_query.
Ian Elliott1ce053f2022-03-16 09:49:53 -0600744
745 // TODO(b/203826952): research proper value; temporarily use the
746 // values seen on Pixel
747 consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
748 } else {
749 Surface& surface = *SurfaceFromHandle(surface_handle);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600750 consumer_usage = surface.consumer_usage;
Ian Elliott6ba85d92022-02-18 16:44:58 -0700751 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700752
Charlie Lao324191f2019-12-13 11:03:16 -0800753 AHardwareBuffer_Desc desc = {};
754 desc.width = 1;
755 desc.height = 1;
756 desc.layers = 1;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600757 desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
Charlie Lao324191f2019-12-13 11:03:16 -0800758 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
759
760 // We must support R8G8B8A8
761 std::vector<VkSurfaceFormatKHR> all_formats = {
762 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Yiwei Zhang0413cc02022-07-27 04:54:48 +0000763 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Yiwei Zhang0413cc02022-07-27 04:54:48 +0000764 };
Charlie Lao324191f2019-12-13 11:03:16 -0800765
Yiwei Zhang959f5182024-03-12 02:46:36 +0000766 VkFormat format = VK_FORMAT_UNDEFINED;
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000767 if (colorspace_ext) {
Yuxin Hu7facd572024-02-08 22:54:23 +0000768 for (VkColorSpaceKHR colorSpace :
769 colorSpaceSupportedByVkEXTSwapchainColorspace) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000770 format = VK_FORMAT_R8G8B8A8_UNORM;
771 if (GetNativeDataspace(colorSpace, format) != DataSpace::UNKNOWN) {
Yuxin Hu7facd572024-02-08 22:54:23 +0000772 all_formats.emplace_back(
Yiwei Zhang959f5182024-03-12 02:46:36 +0000773 VkSurfaceFormatKHR{format, colorSpace});
Yuxin Hu7facd572024-02-08 22:54:23 +0000774 }
775
Yiwei Zhang959f5182024-03-12 02:46:36 +0000776 format = VK_FORMAT_R8G8B8A8_SRGB;
777 if (GetNativeDataspace(colorSpace, format) != DataSpace::UNKNOWN) {
Yuxin Hu7facd572024-02-08 22:54:23 +0000778 all_formats.emplace_back(
Yiwei Zhang959f5182024-03-12 02:46:36 +0000779 VkSurfaceFormatKHR{format, colorSpace});
Yuxin Hu7facd572024-02-08 22:54:23 +0000780 }
781 }
Charlie Lao324191f2019-12-13 11:03:16 -0800782 }
783
Ian Elliott1ce053f2022-03-16 09:49:53 -0600784 // NOTE: Any new formats that are added must be coordinated across different
785 // Android users. This includes the ANGLE team (a layered implementation of
786 // OpenGL-ES).
787
Yiwei Zhang959f5182024-03-12 02:46:36 +0000788 format = VK_FORMAT_R5G6B5_UNORM_PACK16;
Charlie Lao324191f2019-12-13 11:03:16 -0800789 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
790 if (AHardwareBuffer_isSupported(&desc)) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000791 all_formats.emplace_back(
792 VkSurfaceFormatKHR{format, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800793 if (colorspace_ext) {
Yuxin Hu7facd572024-02-08 22:54:23 +0000794 for (VkColorSpaceKHR colorSpace :
795 colorSpaceSupportedByVkEXTSwapchainColorspace) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000796 if (GetNativeDataspace(colorSpace, format) !=
Yuxin Hu7facd572024-02-08 22:54:23 +0000797 DataSpace::UNKNOWN) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000798 all_formats.emplace_back(
799 VkSurfaceFormatKHR{format, colorSpace});
Yuxin Hu7facd572024-02-08 22:54:23 +0000800 }
801 }
Jason Macnakca506332022-11-09 11:00:36 -0800802 }
Charlie Lao324191f2019-12-13 11:03:16 -0800803 }
804
Yiwei Zhang959f5182024-03-12 02:46:36 +0000805 format = VK_FORMAT_R16G16B16A16_SFLOAT;
Charlie Lao324191f2019-12-13 11:03:16 -0800806 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
807 if (AHardwareBuffer_isSupported(&desc)) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000808 all_formats.emplace_back(
809 VkSurfaceFormatKHR{format, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800810 if (colorspace_ext) {
Yuxin Hu7facd572024-02-08 22:54:23 +0000811 for (VkColorSpaceKHR colorSpace :
812 colorSpaceSupportedByVkEXTSwapchainColorspace) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000813 if (GetNativeDataspace(colorSpace, format) !=
Yuxin Hu7facd572024-02-08 22:54:23 +0000814 DataSpace::UNKNOWN) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000815 all_formats.emplace_back(
816 VkSurfaceFormatKHR{format, colorSpace});
Yuxin Hu7facd572024-02-08 22:54:23 +0000817 }
818 }
819
820 for (
821 VkColorSpaceKHR colorSpace :
822 colorSpaceSupportedByVkEXTSwapchainColorspaceOnFP16SurfaceOnly) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000823 if (GetNativeDataspace(colorSpace, format) !=
Yuxin Hu7facd572024-02-08 22:54:23 +0000824 DataSpace::UNKNOWN) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000825 all_formats.emplace_back(
826 VkSurfaceFormatKHR{format, colorSpace});
Yuxin Hu7facd572024-02-08 22:54:23 +0000827 }
828 }
Charlie Lao324191f2019-12-13 11:03:16 -0800829 }
830 }
831
Yiwei Zhang959f5182024-03-12 02:46:36 +0000832 format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
Charlie Lao324191f2019-12-13 11:03:16 -0800833 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
834 if (AHardwareBuffer_isSupported(&desc)) {
835 all_formats.emplace_back(
Yiwei Zhang959f5182024-03-12 02:46:36 +0000836 VkSurfaceFormatKHR{format, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800837 if (colorspace_ext) {
Yuxin Hu7facd572024-02-08 22:54:23 +0000838 for (VkColorSpaceKHR colorSpace :
839 colorSpaceSupportedByVkEXTSwapchainColorspace) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000840 if (GetNativeDataspace(colorSpace, format) !=
Yuxin Hu7facd572024-02-08 22:54:23 +0000841 DataSpace::UNKNOWN) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000842 all_formats.emplace_back(
843 VkSurfaceFormatKHR{format, colorSpace});
Yuxin Hu7facd572024-02-08 22:54:23 +0000844 }
845 }
Charlie Lao324191f2019-12-13 11:03:16 -0800846 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700847 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700848
Yiwei Zhang959f5182024-03-12 02:46:36 +0000849 format = VK_FORMAT_R8_UNORM;
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500850 desc.format = AHARDWAREBUFFER_FORMAT_R8_UNORM;
851 if (AHardwareBuffer_isSupported(&desc)) {
Jason Macnakca506332022-11-09 11:00:36 -0800852 if (colorspace_ext) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000853 all_formats.emplace_back(
854 VkSurfaceFormatKHR{format, VK_COLOR_SPACE_PASS_THROUGH_EXT});
Jason Macnakca506332022-11-09 11:00:36 -0800855 }
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500856 }
857
Trevor David Black67e9b102023-03-07 05:28:15 +0000858 bool rgba10x6_formats_ext = false;
859 uint32_t exts_count;
860 const auto& driver = GetData(pdev).driver;
861 driver.EnumerateDeviceExtensionProperties(pdev, nullptr, &exts_count,
862 nullptr);
863 std::vector<VkExtensionProperties> props(exts_count);
864 driver.EnumerateDeviceExtensionProperties(pdev, nullptr, &exts_count,
865 props.data());
866 for (uint32_t i = 0; i < exts_count; i++) {
867 VkExtensionProperties prop = props[i];
868 if (strcmp(prop.extensionName,
869 VK_EXT_RGBA10X6_FORMATS_EXTENSION_NAME) == 0) {
870 rgba10x6_formats_ext = true;
871 }
872 }
Yiwei Zhang959f5182024-03-12 02:46:36 +0000873 format = VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16;
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000874 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM;
Trevor David Black67e9b102023-03-07 05:28:15 +0000875 if (AHardwareBuffer_isSupported(&desc) && rgba10x6_formats_ext) {
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000876 all_formats.emplace_back(
Yiwei Zhang959f5182024-03-12 02:46:36 +0000877 VkSurfaceFormatKHR{format, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000878 if (colorspace_ext) {
Yuxin Hu7facd572024-02-08 22:54:23 +0000879 for (VkColorSpaceKHR colorSpace :
880 colorSpaceSupportedByVkEXTSwapchainColorspace) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000881 if (GetNativeDataspace(colorSpace, format) !=
Yuxin Hu7facd572024-02-08 22:54:23 +0000882 DataSpace::UNKNOWN) {
Yiwei Zhang959f5182024-03-12 02:46:36 +0000883 all_formats.emplace_back(
884 VkSurfaceFormatKHR{format, colorSpace});
Yuxin Hu7facd572024-02-08 22:54:23 +0000885 }
886 }
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000887 }
888 }
889
Ian Elliott6ba85d92022-02-18 16:44:58 -0700890 // NOTE: Any new formats that are added must be coordinated across different
891 // Android users. This includes the ANGLE team (a layered implementation of
892 // OpenGL-ES).
893
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000894 return CopyWithIncomplete(all_formats, formats, count);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700895}
896
Jesse Halle1b12782015-11-30 11:27:32 -0800897VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300898VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
899 VkPhysicalDevice physicalDevice,
900 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
901 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800902 ATRACE_CALL();
903
Android Culprit Assistante9d915d2024-05-01 10:19:50 +0000904 auto surface = pSurfaceInfo->surface;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000905 auto capabilities = &pSurfaceCapabilities->surfaceCapabilities;
Chris Forbes2452cf72017-03-16 16:30:17 +1300906
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000907 VkSurfacePresentModeEXT const *pPresentMode = nullptr;
908 for (auto pNext = reinterpret_cast<VkBaseInStructure const *>(pSurfaceInfo->pNext);
909 pNext; pNext = reinterpret_cast<VkBaseInStructure const *>(pNext->pNext)) {
910 switch (pNext->sType) {
911 case VK_STRUCTURE_TYPE_SURFACE_PRESENT_MODE_EXT:
912 pPresentMode = reinterpret_cast<VkSurfacePresentModeEXT const *>(pNext);
913 break;
Chris Forbes06bc0092017-03-16 16:46:05 +1300914
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000915 default:
916 break;
917 }
918 }
919
920 int err;
921 int width, height;
922 int transform_hint;
923 int max_buffer_count;
Trevor David Black1d3509e2023-06-08 00:17:40 +0000924 int min_undequeued_buffers;
Android Culprit Assistante9d915d2024-05-01 10:19:50 +0000925 if (surface == VK_NULL_HANDLE) {
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000926 const InstanceData& instance_data = GetData(physicalDevice);
927 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
928 bool surfaceless_enabled =
929 instance_data.hook_extensions.test(surfaceless);
930 if (!surfaceless_enabled) {
931 // It is an error to pass a surface==VK_NULL_HANDLE unless the
932 // VK_GOOGLE_surfaceless_query extension is enabled
933 return VK_ERROR_SURFACE_LOST_KHR;
934 }
935 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
936 // extension for this function is for
937 // VkSurfaceProtectedCapabilitiesKHR::supportsProtected. The following
938 // four values cannot be known without a surface. Default values will
939 // be supplied anyway, but cannot be relied upon.
940 width = 0xFFFFFFFF;
941 height = 0xFFFFFFFF;
942 transform_hint = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
943 capabilities->minImageCount = 0xFFFFFFFF;
944 capabilities->maxImageCount = 0xFFFFFFFF;
945 } else {
Android Culprit Assistante9d915d2024-05-01 10:19:50 +0000946 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000947
948 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
949 if (err != android::OK) {
950 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
951 strerror(-err), err);
952 return VK_ERROR_SURFACE_LOST_KHR;
953 }
954 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
955 if (err != android::OK) {
956 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
957 strerror(-err), err);
958 return VK_ERROR_SURFACE_LOST_KHR;
959 }
960
961 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
962 &transform_hint);
963 if (err != android::OK) {
964 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
965 strerror(-err), err);
966 return VK_ERROR_SURFACE_LOST_KHR;
967 }
968
969 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT,
970 &max_buffer_count);
971 if (err != android::OK) {
972 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
973 strerror(-err), err);
974 return VK_ERROR_SURFACE_LOST_KHR;
975 }
976
Trevor David Black1d3509e2023-06-08 00:17:40 +0000977 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
978 &min_undequeued_buffers);
979 if (err != android::OK) {
980 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d)",
981 strerror(-err), err);
982 return VK_ERROR_SURFACE_LOST_KHR;
983 }
984
Trevor David Blacke4d23b92023-09-26 20:30:42 +0000985 // Additional buffer count over min_undequeued_buffers in vulkan came from 2 total
986 // being technically enough for fifo (although a poor experience) vs 3 being the
987 // absolute minimum for mailbox to be useful. So min_undequeued_buffers + 2 is sensible
988 static constexpr int default_additional_buffers = 2;
989
Trevor David Blacka93e21f2023-08-30 23:42:20 +0000990 if(pPresentMode != nullptr) {
991 switch (pPresentMode->presentMode) {
992 case VK_PRESENT_MODE_IMMEDIATE_KHR:
993 ALOGE("Swapchain present mode VK_PRESENT_MODE_IMMEDIATE_KHR is not supported");
994 break;
995 case VK_PRESENT_MODE_MAILBOX_KHR:
996 case VK_PRESENT_MODE_FIFO_KHR:
Trevor David Blacke4d23b92023-09-26 20:30:42 +0000997 capabilities->minImageCount = std::min(max_buffer_count,
998 min_undequeued_buffers + default_additional_buffers);
Trevor David Blacka93e21f2023-08-30 23:42:20 +0000999 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
1000 break;
1001 case VK_PRESENT_MODE_FIFO_RELAXED_KHR:
1002 ALOGE("Swapchain present mode VK_PRESENT_MODE_FIFO_RELEAXED_KHR "
1003 "is not supported");
1004 break;
1005 case VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR:
1006 case VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR:
1007 capabilities->minImageCount = 1;
1008 capabilities->maxImageCount = 1;
1009 break;
1010
1011 default:
1012 ALOGE("Unrecognized swapchain present mode %u is not supported",
1013 pPresentMode->presentMode);
1014 break;
1015 }
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001016 } else {
Trevor David Blacke4d23b92023-09-26 20:30:42 +00001017 capabilities->minImageCount = std::min(max_buffer_count,
1018 min_undequeued_buffers + default_additional_buffers);
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001019 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
1020 }
1021 }
1022
1023 capabilities->currentExtent =
1024 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
1025
1026 // TODO(http://b/134182502): Figure out what the max extent should be.
1027 capabilities->minImageExtent = VkExtent2D{1, 1};
1028 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
1029
1030 if (capabilities->maxImageExtent.height <
1031 capabilities->currentExtent.height) {
1032 capabilities->maxImageExtent.height =
1033 capabilities->currentExtent.height;
1034 }
1035
1036 if (capabilities->maxImageExtent.width <
1037 capabilities->currentExtent.width) {
1038 capabilities->maxImageExtent.width = capabilities->currentExtent.width;
1039 }
1040
1041 capabilities->maxImageArrayLayers = 1;
1042
1043 capabilities->supportedTransforms = kSupportedTransforms;
1044 capabilities->currentTransform =
1045 TranslateNativeToVulkanTransform(transform_hint);
1046
Android Culprit Assistante9d915d2024-05-01 10:19:50 +00001047 // On Android, window composition is a WindowManager property, not something
1048 // associated with the bufferqueue. It can't be changed from here.
1049 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001050
1051 capabilities->supportedUsageFlags =
1052 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1053 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
1054 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1055 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
1056
1057 for (auto pNext = reinterpret_cast<VkBaseOutStructure*>(pSurfaceCapabilities->pNext);
1058 pNext; pNext = reinterpret_cast<VkBaseOutStructure*>(pNext->pNext)) {
1059
1060 switch (pNext->sType) {
Chris Forbes06bc0092017-03-16 16:46:05 +13001061 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
1062 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001063 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(pNext);
Chris Forbes06bc0092017-03-16 16:46:05 +13001064 // Claim same set of usage flags are supported for
1065 // shared present modes as for other modes.
1066 shared_caps->sharedPresentSupportedUsageFlags =
1067 pSurfaceCapabilities->surfaceCapabilities
1068 .supportedUsageFlags;
1069 } break;
1070
Ian Elliottbb67b242022-03-16 09:52:28 -06001071 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
1072 VkSurfaceProtectedCapabilitiesKHR* protected_caps =
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001073 reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(pNext);
Ian Elliottbb67b242022-03-16 09:52:28 -06001074 protected_caps->supportsProtected = VK_TRUE;
1075 } break;
1076
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001077 case VK_STRUCTURE_TYPE_SURFACE_PRESENT_SCALING_CAPABILITIES_EXT: {
1078 VkSurfacePresentScalingCapabilitiesEXT* scaling_caps =
1079 reinterpret_cast<VkSurfacePresentScalingCapabilitiesEXT*>(pNext);
1080 // By default, Android stretches the buffer to fit the window,
1081 // without preserving aspect ratio. Other modes are technically possible
1082 // but consult with CoGS team before exposing them here!
1083 scaling_caps->supportedPresentScaling = VK_PRESENT_SCALING_STRETCH_BIT_EXT;
1084
1085 // Since we always scale, we don't support any gravity.
1086 scaling_caps->supportedPresentGravityX = 0;
1087 scaling_caps->supportedPresentGravityY = 0;
1088
1089 // Scaled image limits are just the basic image limits
1090 scaling_caps->minScaledImageExtent = capabilities->minImageExtent;
1091 scaling_caps->maxScaledImageExtent = capabilities->maxImageExtent;
1092 } break;
1093
1094 case VK_STRUCTURE_TYPE_SURFACE_PRESENT_MODE_COMPATIBILITY_EXT: {
1095 VkSurfacePresentModeCompatibilityEXT* mode_caps =
1096 reinterpret_cast<VkSurfacePresentModeCompatibilityEXT*>(pNext);
1097
1098 ALOG_ASSERT(pPresentMode,
1099 "querying VkSurfacePresentModeCompatibilityEXT "
1100 "requires VkSurfacePresentModeEXT to be provided");
1101 std::vector<VkPresentModeKHR> compatibleModes;
1102 compatibleModes.push_back(pPresentMode->presentMode);
1103
1104 switch (pPresentMode->presentMode) {
1105 // Shared modes are both compatible with each other.
1106 case VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR:
1107 compatibleModes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
1108 break;
1109 case VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR:
1110 compatibleModes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
1111 break;
1112 default:
1113 // Other modes are only compatible with themselves.
1114 // TODO: consider whether switching between FIFO and MAILBOX is reasonable
1115 break;
1116 }
1117
1118 // Note: this does not generate VK_INCOMPLETE since we're nested inside
1119 // a larger query and there would be no way to determine exactly where it came from.
1120 CopyWithIncomplete(compatibleModes, mode_caps->pPresentModes,
1121 &mode_caps->presentModeCount);
1122 } break;
1123
Chris Forbes06bc0092017-03-16 16:46:05 +13001124 default:
1125 // Ignore all other extension structs
1126 break;
1127 }
1128 }
1129
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001130 return VK_SUCCESS;
Chris Forbes2452cf72017-03-16 16:30:17 +13001131}
1132
1133VKAPI_ATTR
1134VkResult GetPhysicalDeviceSurfaceFormats2KHR(
1135 VkPhysicalDevice physicalDevice,
1136 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
1137 uint32_t* pSurfaceFormatCount,
1138 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001139 ATRACE_CALL();
1140
Chris Forbes2452cf72017-03-16 16:30:17 +13001141 if (!pSurfaceFormats) {
1142 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
1143 pSurfaceInfo->surface,
1144 pSurfaceFormatCount, nullptr);
Trevor David Black929e9cd2022-11-22 04:12:19 +00001145 }
Chris Forbes2452cf72017-03-16 16:30:17 +13001146
Trevor David Black929e9cd2022-11-22 04:12:19 +00001147 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
1148 // after the call.
1149 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
1150 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
1151 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
1152 surface_formats.data());
Trevor David Black2cc44682022-03-09 00:31:38 +00001153
Trevor David Black929e9cd2022-11-22 04:12:19 +00001154 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1155 return result;
1156 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001157
Trevor David Black929e9cd2022-11-22 04:12:19 +00001158 const auto& driver = GetData(physicalDevice).driver;
1159
1160 // marshal results individually due to stride difference.
1161 uint32_t formats_to_marshal = *pSurfaceFormatCount;
1162 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
1163 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
1164
1165 // Query the compression properties for the surface format
1166 VkSurfaceFormat2KHR* pSurfaceFormat = &pSurfaceFormats[i];
1167 while (pSurfaceFormat->pNext) {
1168 pSurfaceFormat =
1169 reinterpret_cast<VkSurfaceFormat2KHR*>(pSurfaceFormat->pNext);
1170 switch (pSurfaceFormat->sType) {
1171 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT: {
Trevor David Black2cc44682022-03-09 00:31:38 +00001172 VkImageCompressionPropertiesEXT* surfaceCompressionProps =
1173 reinterpret_cast<VkImageCompressionPropertiesEXT*>(
Trevor David Black929e9cd2022-11-22 04:12:19 +00001174 pSurfaceFormat);
Trevor David Black2cc44682022-03-09 00:31:38 +00001175
1176 if (surfaceCompressionProps &&
Chris Forbes0869c132024-06-05 13:49:11 +12001177 (driver.GetPhysicalDeviceImageFormatProperties2KHR ||
1178 driver.GetPhysicalDeviceImageFormatProperties2)) {
Trevor David Black2cc44682022-03-09 00:31:38 +00001179 VkPhysicalDeviceImageFormatInfo2 imageFormatInfo = {};
1180 imageFormatInfo.sType =
1181 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2;
1182 imageFormatInfo.format =
1183 pSurfaceFormats[i].surfaceFormat.format;
Trevor David Blacka4298c92023-06-20 17:11:58 +00001184 imageFormatInfo.type = VK_IMAGE_TYPE_2D;
1185 imageFormatInfo.usage =
1186 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
Trevor David Black2cc44682022-03-09 00:31:38 +00001187 imageFormatInfo.pNext = nullptr;
1188
1189 VkImageCompressionControlEXT compressionControl = {};
1190 compressionControl.sType =
1191 VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT;
1192 compressionControl.pNext = imageFormatInfo.pNext;
Trevor David Blacka4298c92023-06-20 17:11:58 +00001193 compressionControl.flags =
1194 VK_IMAGE_COMPRESSION_FIXED_RATE_DEFAULT_EXT;
Trevor David Black2cc44682022-03-09 00:31:38 +00001195
1196 imageFormatInfo.pNext = &compressionControl;
1197
1198 VkImageCompressionPropertiesEXT compressionProps = {};
1199 compressionProps.sType =
1200 VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT;
1201 compressionProps.pNext = nullptr;
1202
1203 VkImageFormatProperties2KHR imageFormatProps = {};
1204 imageFormatProps.sType =
1205 VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR;
1206 imageFormatProps.pNext = &compressionProps;
1207
1208 VkResult compressionRes =
Chris Forbes0869c132024-06-05 13:49:11 +12001209 GetPhysicalDeviceImageFormatProperties2(
Trevor David Black2cc44682022-03-09 00:31:38 +00001210 physicalDevice, &imageFormatInfo,
1211 &imageFormatProps);
1212 if (compressionRes == VK_SUCCESS) {
1213 surfaceCompressionProps->imageCompressionFlags =
1214 compressionProps.imageCompressionFlags;
1215 surfaceCompressionProps
1216 ->imageCompressionFixedRateFlags =
1217 compressionProps.imageCompressionFixedRateFlags;
Tom Murphy1bde0022024-08-22 20:46:07 +00001218 } else if (compressionRes ==
1219 VK_ERROR_OUT_OF_HOST_MEMORY ||
1220 compressionRes ==
1221 VK_ERROR_OUT_OF_DEVICE_MEMORY) {
Trevor David Black2cc44682022-03-09 00:31:38 +00001222 return compressionRes;
Tom Murphy1bde0022024-08-22 20:46:07 +00001223 } else {
1224 // For any of the *_NOT_SUPPORTED errors we continue
1225 // onto the next format
1226 continue;
Trevor David Black2cc44682022-03-09 00:31:38 +00001227 }
1228 }
Trevor David Black929e9cd2022-11-22 04:12:19 +00001229 } break;
1230
1231 default:
1232 // Ignore all other extension structs
1233 break;
Chris Forbes2452cf72017-03-16 16:30:17 +13001234 }
1235 }
Chris Forbes2452cf72017-03-16 16:30:17 +13001236 }
Trevor David Black929e9cd2022-11-22 04:12:19 +00001237
1238 return result;
Chris Forbes2452cf72017-03-16 16:30:17 +13001239}
1240
1241VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +13001242VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -08001243 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +08001244 uint32_t* count,
1245 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001246 ATRACE_CALL();
1247
Yiwei Zhange4a559c2018-02-15 11:27:36 -08001248 int err;
1249 int query_value;
Ian Elliotte7f036c2022-03-15 16:49:21 -06001250 std::vector<VkPresentModeKHR> present_modes;
Ian Elliott1ce053f2022-03-16 09:49:53 -06001251 if (surface == VK_NULL_HANDLE) {
1252 const InstanceData& instance_data = GetData(pdev);
1253 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
1254 bool surfaceless_enabled =
1255 instance_data.hook_extensions.test(surfaceless);
1256 if (!surfaceless_enabled) {
1257 return VK_ERROR_SURFACE_LOST_KHR;
1258 }
1259 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
1260 // extension for this function is for
1261 // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and
1262 // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot
1263 // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a
Ian Elliott7e361142022-06-02 10:45:17 -06001264 // surface, and that cannot be relied upon. Therefore, don't return it.
Ian Elliott1ce053f2022-03-16 09:49:53 -06001265 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1266 } else {
1267 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1268
1269 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1270 &query_value);
1271 if (err != android::OK || query_value < 0) {
1272 ALOGE(
1273 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
1274 "value=%d",
1275 strerror(-err), err, query_value);
1276 return VK_ERROR_SURFACE_LOST_KHR;
1277 }
1278 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1279
1280 err =
1281 window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
1282 if (err != android::OK || query_value < 0) {
1283 ALOGE(
1284 "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
1285 strerror(-err), err, query_value);
1286 return VK_ERROR_SURFACE_LOST_KHR;
1287 }
1288 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
1289
1290 if (min_undequeued_buffers + 1 < max_buffer_count)
1291 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
1292 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1293 }
Chris Forbese8d79a62017-02-22 12:49:18 +13001294
1295 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001296 QueryPresentationProperties(pdev, &present_properties);
1297 if (present_properties.sharedImage) {
1298 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
1299 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +13001300 }
1301
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001302 return CopyWithIncomplete(present_modes, modes, count);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001303}
1304
Jesse Halle1b12782015-11-30 11:27:32 -08001305VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001306VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001307 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001308 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001309 ATRACE_CALL();
1310
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001311 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
1312 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
1313 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
1314 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
1315 pDeviceGroupPresentCapabilities->sType);
1316
1317 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
1318 sizeof(pDeviceGroupPresentCapabilities->presentMask));
1319
1320 // assume device group of size 1
1321 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
1322 pDeviceGroupPresentCapabilities->modes =
1323 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1324
1325 return VK_SUCCESS;
1326}
1327
1328VKAPI_ATTR
1329VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001330 VkDevice,
1331 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001332 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001333 ATRACE_CALL();
1334
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001335 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1336 return VK_SUCCESS;
1337}
1338
1339VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -06001340VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001341 VkSurfaceKHR surface,
1342 uint32_t* pRectCount,
1343 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001344 ATRACE_CALL();
1345
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001346 if (!pRects) {
1347 *pRectCount = 1;
1348 } else {
1349 uint32_t count = std::min(*pRectCount, 1u);
1350 bool incomplete = *pRectCount < 1;
1351
1352 *pRectCount = count;
1353
1354 if (incomplete) {
1355 return VK_INCOMPLETE;
1356 }
1357
1358 int err;
1359 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1360
1361 int width = 0, height = 0;
1362 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001363 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001364 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1365 strerror(-err), err);
1366 }
1367 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001368 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001369 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1370 strerror(-err), err);
1371 }
1372
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001373 pRects[0].offset.x = 0;
1374 pRects[0].offset.y = 0;
1375 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1376 static_cast<uint32_t>(height)};
1377 }
1378 return VK_SUCCESS;
1379}
1380
Yiwei Zhang533cea92019-06-03 18:43:24 -07001381static void DestroySwapchainInternal(VkDevice device,
1382 VkSwapchainKHR swapchain_handle,
1383 const VkAllocationCallbacks* allocator) {
1384 ATRACE_CALL();
1385
1386 const auto& dispatch = GetData(device).driver;
1387 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1388 if (!swapchain) {
1389 return;
1390 }
1391
1392 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1393 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1394
1395 if (window && swapchain->frame_timestamps_enabled) {
1396 native_window_enable_frame_timestamps(window, false);
1397 }
1398
1399 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001400 ReleaseSwapchainImage(device, swapchain->shared, window, -1,
1401 swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001402 }
1403
1404 if (active) {
1405 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1406 }
1407
1408 if (!allocator) {
1409 allocator = &GetData(device).allocator;
1410 }
1411
1412 swapchain->~Swapchain();
1413 allocator->pfnFree(allocator->pUserData, swapchain);
1414}
1415
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001416static VkResult getProducerUsage(const VkDevice& device,
1417 const VkSwapchainCreateInfoKHR* create_info,
1418 const VkSwapchainImageUsageFlagsANDROID swapchain_image_usage,
1419 bool create_protected_swapchain,
1420 uint64_t* producer_usage) {
1421 // Get the physical device to query the appropriate producer usage
1422 const VkPhysicalDevice& pdev = GetData(device).driver_physical_device;
1423 const InstanceData& instance_data = GetData(pdev);
1424 const InstanceDriverTable& instance_dispatch = instance_data.driver;
Trevor David Black02926f52024-01-04 18:44:20 +00001425 if (instance_dispatch.GetPhysicalDeviceImageFormatProperties2 ||
1426 instance_dispatch.GetPhysicalDeviceImageFormatProperties2KHR) {
1427 // Look through the create_info pNext chain passed to createSwapchainKHR
1428 // for an image compression control struct.
1429 // if one is found AND the appropriate extensions are enabled, create a
1430 // VkImageCompressionControlEXT structure to pass on to
1431 // GetPhysicalDeviceImageFormatProperties2
1432 void* compression_control_pNext = nullptr;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001433 VkImageCompressionControlEXT image_compression = {};
Trevor David Black02926f52024-01-04 18:44:20 +00001434 const VkSwapchainCreateInfoKHR* create_infos = create_info;
1435 while (create_infos->pNext) {
1436 create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(create_infos->pNext);
1437 switch (create_infos->sType) {
1438 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: {
1439 const VkImageCompressionControlEXT* compression_infos =
1440 reinterpret_cast<const VkImageCompressionControlEXT*>(create_infos);
1441 image_compression = *compression_infos;
1442 image_compression.pNext = nullptr;
1443 compression_control_pNext = &image_compression;
1444 } break;
1445 default:
1446 // Ignore all other info structs
1447 break;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001448 }
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001449 }
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001450
Trevor David Black02926f52024-01-04 18:44:20 +00001451 // call GetPhysicalDeviceImageFormatProperties2KHR
1452 VkPhysicalDeviceExternalImageFormatInfo external_image_format_info = {
1453 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
1454 .pNext = compression_control_pNext,
1455 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
1456 };
1457
1458 // AHB does not have an sRGB format so we can't pass it to GPDIFP
1459 // We need to convert the format to unorm if it is srgb
1460 VkFormat format = create_info->imageFormat;
1461 if (format == VK_FORMAT_R8G8B8A8_SRGB) {
1462 format = VK_FORMAT_R8G8B8A8_UNORM;
1463 }
1464
1465 VkPhysicalDeviceImageFormatInfo2 image_format_info = {
1466 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1467 .pNext = &external_image_format_info,
1468 .format = format,
1469 .type = VK_IMAGE_TYPE_2D,
1470 .tiling = VK_IMAGE_TILING_OPTIMAL,
1471 .usage = create_info->imageUsage,
1472 .flags = create_protected_swapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
1473 };
1474
Tom Murphyea321842024-06-14 18:26:58 +00001475 // If supporting mutable format swapchain add the mutable format flag
1476 if (create_info->flags & VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR) {
1477 image_format_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
1478 image_format_info.flags |= VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR;
1479 }
1480
Trevor David Black02926f52024-01-04 18:44:20 +00001481 VkAndroidHardwareBufferUsageANDROID ahb_usage;
1482 ahb_usage.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_USAGE_ANDROID;
1483 ahb_usage.pNext = nullptr;
1484
1485 VkImageFormatProperties2 image_format_properties;
1486 image_format_properties.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2;
1487 image_format_properties.pNext = &ahb_usage;
1488
Chris Forbes6c9ce492024-06-12 11:41:14 +12001489 VkResult result = GetPhysicalDeviceImageFormatProperties2(
1490 pdev, &image_format_info, &image_format_properties);
1491 if (result != VK_SUCCESS) {
1492 ALOGE(
1493 "VkGetPhysicalDeviceImageFormatProperties2 for AHB usage "
1494 "failed: %d",
1495 result);
1496 return VK_ERROR_SURFACE_LOST_KHR;
Trevor David Black02926f52024-01-04 18:44:20 +00001497 }
1498
1499 // Determine if USAGE_FRONT_BUFFER is needed.
1500 // GPDIFP2 has no means of using VkSwapchainImageUsageFlagsANDROID when
1501 // querying for producer_usage. So androidHardwareBufferUsage will not
1502 // contain USAGE_FRONT_BUFFER. We need to manually check for usage here.
1503 if (!(swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID)) {
1504 *producer_usage = ahb_usage.androidHardwareBufferUsage;
1505 return VK_SUCCESS;
1506 }
1507
1508 // Check if USAGE_FRONT_BUFFER is supported for this swapchain
1509 AHardwareBuffer_Desc ahb_desc = {
1510 .width = create_info->imageExtent.width,
1511 .height = create_info->imageExtent.height,
1512 .layers = create_info->imageArrayLayers,
1513 .format = create_info->imageFormat,
1514 .usage = ahb_usage.androidHardwareBufferUsage | AHARDWAREBUFFER_USAGE_FRONT_BUFFER,
1515 .stride = 0, // stride is always ignored when calling isSupported()
1516 };
1517
1518 // If FRONT_BUFFER is not supported,
1519 // then we need to call GetSwapchainGrallocUsageXAndroid below
1520 if (AHardwareBuffer_isSupported(&ahb_desc)) {
1521 *producer_usage = ahb_usage.androidHardwareBufferUsage;
1522 *producer_usage |= AHARDWAREBUFFER_USAGE_FRONT_BUFFER;
1523 return VK_SUCCESS;
1524 }
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001525 }
1526
Trevor David Black02926f52024-01-04 18:44:20 +00001527 uint64_t native_usage = 0;
1528 void* usage_info_pNext = nullptr;
1529 VkResult result;
Vamsidhar reddy Gaddam7a844312023-11-14 10:59:05 +00001530 VkImageCompressionControlEXT image_compression = {};
Trevor David Black02926f52024-01-04 18:44:20 +00001531 const auto& dispatch = GetData(device).driver;
1532 if (dispatch.GetSwapchainGrallocUsage4ANDROID) {
1533 ATRACE_BEGIN("GetSwapchainGrallocUsage4ANDROID");
1534 VkGrallocUsageInfo2ANDROID gralloc_usage_info = {};
1535 gralloc_usage_info.sType =
1536 VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_2_ANDROID;
1537 gralloc_usage_info.format = create_info->imageFormat;
1538 gralloc_usage_info.imageUsage = create_info->imageUsage;
1539 gralloc_usage_info.swapchainImageUsage = swapchain_image_usage;
1540
1541 // Look through the pNext chain for an image compression control struct
1542 // if one is found AND the appropriate extensions are enabled,
1543 // append it to be the gralloc usage pNext chain
1544 const VkSwapchainCreateInfoKHR* create_infos = create_info;
1545 while (create_infos->pNext) {
1546 create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(
1547 create_infos->pNext);
1548 switch (create_infos->sType) {
1549 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: {
1550 const VkImageCompressionControlEXT* compression_infos =
1551 reinterpret_cast<const VkImageCompressionControlEXT*>(
1552 create_infos);
1553 image_compression = *compression_infos;
1554 image_compression.pNext = nullptr;
1555 usage_info_pNext = &image_compression;
1556 } break;
1557
1558 default:
1559 // Ignore all other info structs
1560 break;
1561 }
Vamsidhar reddy Gaddam7a844312023-11-14 10:59:05 +00001562 }
Trevor David Black02926f52024-01-04 18:44:20 +00001563 gralloc_usage_info.pNext = usage_info_pNext;
Vamsidhar reddy Gaddam7a844312023-11-14 10:59:05 +00001564
Trevor David Black02926f52024-01-04 18:44:20 +00001565 result = dispatch.GetSwapchainGrallocUsage4ANDROID(
1566 device, &gralloc_usage_info, &native_usage);
1567 ATRACE_END();
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001568 if (result != VK_SUCCESS) {
Trevor David Black02926f52024-01-04 18:44:20 +00001569 ALOGE("vkGetSwapchainGrallocUsage4ANDROID failed: %d", result);
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001570 return VK_ERROR_SURFACE_LOST_KHR;
1571 }
Trevor David Black02926f52024-01-04 18:44:20 +00001572 } else if (dispatch.GetSwapchainGrallocUsage3ANDROID) {
1573 ATRACE_BEGIN("GetSwapchainGrallocUsage3ANDROID");
1574 VkGrallocUsageInfoANDROID gralloc_usage_info = {};
1575 gralloc_usage_info.sType = VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID;
1576 gralloc_usage_info.format = create_info->imageFormat;
1577 gralloc_usage_info.imageUsage = create_info->imageUsage;
1578
1579 // Look through the pNext chain for an image compression control struct
1580 // if one is found AND the appropriate extensions are enabled,
1581 // append it to be the gralloc usage pNext chain
1582 const VkSwapchainCreateInfoKHR* create_infos = create_info;
1583 while (create_infos->pNext) {
1584 create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(
1585 create_infos->pNext);
1586 switch (create_infos->sType) {
1587 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: {
1588 const VkImageCompressionControlEXT* compression_infos =
1589 reinterpret_cast<const VkImageCompressionControlEXT*>(
1590 create_infos);
1591 image_compression = *compression_infos;
1592 image_compression.pNext = nullptr;
1593 usage_info_pNext = &image_compression;
1594 } break;
1595
1596 default:
1597 // Ignore all other info structs
1598 break;
1599 }
1600 }
1601 gralloc_usage_info.pNext = usage_info_pNext;
1602
1603 result = dispatch.GetSwapchainGrallocUsage3ANDROID(
1604 device, &gralloc_usage_info, &native_usage);
1605 ATRACE_END();
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001606 if (result != VK_SUCCESS) {
Trevor David Black02926f52024-01-04 18:44:20 +00001607 ALOGE("vkGetSwapchainGrallocUsage3ANDROID failed: %d", result);
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001608 return VK_ERROR_SURFACE_LOST_KHR;
1609 }
Trevor David Black02926f52024-01-04 18:44:20 +00001610 } else if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
1611 uint64_t consumer_usage, producer_usage;
1612 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
1613 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1614 device, create_info->imageFormat, create_info->imageUsage,
1615 swapchain_image_usage, &consumer_usage, &producer_usage);
1616 ATRACE_END();
1617 if (result != VK_SUCCESS) {
1618 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
1619 return VK_ERROR_SURFACE_LOST_KHR;
1620 }
1621 native_usage =
1622 convertGralloc1ToBufferUsage(producer_usage, consumer_usage);
1623 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
1624 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
1625 int32_t legacy_usage = 0;
1626 result = dispatch.GetSwapchainGrallocUsageANDROID(
1627 device, create_info->imageFormat, create_info->imageUsage,
1628 &legacy_usage);
1629 ATRACE_END();
1630 if (result != VK_SUCCESS) {
1631 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
1632 return VK_ERROR_SURFACE_LOST_KHR;
1633 }
1634 native_usage = static_cast<uint64_t>(legacy_usage);
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001635 }
Trevor David Black02926f52024-01-04 18:44:20 +00001636 *producer_usage = native_usage;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001637
1638 return VK_SUCCESS;
1639}
1640
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001641VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001642VkResult CreateSwapchainKHR(VkDevice device,
1643 const VkSwapchainCreateInfoKHR* create_info,
1644 const VkAllocationCallbacks* allocator,
1645 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001646 ATRACE_CALL();
1647
Jesse Halld7b994a2015-09-07 14:17:37 -07001648 int err;
1649 VkResult result = VK_SUCCESS;
1650
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001651 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1652 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
Android Culprit Assistante9d915d2024-05-01 10:19:50 +00001653 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1654 " oldSwapchain=0x%" PRIx64,
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001655 reinterpret_cast<uint64_t>(create_info->surface),
1656 create_info->minImageCount, create_info->imageFormat,
1657 create_info->imageColorSpace, create_info->imageExtent.width,
1658 create_info->imageExtent.height, create_info->imageUsage,
Android Culprit Assistante9d915d2024-05-01 10:19:50 +00001659 create_info->preTransform, create_info->presentMode,
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001660 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1661
Jesse Hall1f91d392015-12-11 16:28:44 -08001662 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001663 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001664
Android Culprit Assistante9d915d2024-05-01 10:19:50 +00001665 PixelFormat native_pixel_format =
1666 GetNativePixelFormat(create_info->imageFormat);
Yiwei Zhang959f5182024-03-12 02:46:36 +00001667 DataSpace native_dataspace = GetNativeDataspace(
1668 create_info->imageColorSpace, create_info->imageFormat);
sergiuferentz47989332023-09-26 10:24:36 +00001669 if (native_dataspace == DataSpace::UNKNOWN) {
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001670 ALOGE(
1671 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1672 "failed: Unsupported color space",
1673 create_info->imageColorSpace);
1674 return VK_ERROR_INITIALIZATION_FAILED;
1675 }
1676
Jesse Hall42a9eec2016-06-03 12:39:49 -07001677 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001678 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001679 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001680 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001681 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001682 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001683 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001684 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001685 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1686 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001687 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001688 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001689
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001690 Surface& surface = *SurfaceFromHandle(create_info->surface);
1691
Jesse Halldc225072016-05-30 22:40:14 -07001692 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001693 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001694 " because it already has active swapchain 0x%" PRIx64
1695 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1696 reinterpret_cast<uint64_t>(create_info->surface),
1697 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1698 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1699 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1700 }
1701 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1702 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1703
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001704 // -- Reset the native window --
1705 // The native window might have been used previously, and had its properties
1706 // changed from defaults. That will affect the answer we get for queries
1707 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1708 // attempt such queries.
1709
Jesse Halldc225072016-05-30 22:40:14 -07001710 // The native window only allows dequeueing all buffers before any have
1711 // been queued, since after that point at least one is assumed to be in
1712 // non-FREE state at any given time. Disconnecting and re-connecting
1713 // orphans the previous buffers, getting us back to the state where we can
1714 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001715 //
Yiwei Zhang3b88f312023-04-18 23:11:35 +00001716 // This is not necessary if the surface was never used previously.
1717 //
Yiwei Zhang70a21962019-05-31 17:26:52 -07001718 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001719 ANativeWindow* window = surface.window.get();
Yiwei Zhang3b88f312023-04-18 23:11:35 +00001720 if (surface.used_by_swapchain) {
1721 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1722 ALOGW_IF(err != android::OK,
1723 "native_window_api_disconnect failed: %s (%d)", strerror(-err),
1724 err);
1725 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1726 ALOGW_IF(err != android::OK,
1727 "native_window_api_connect failed: %s (%d)", strerror(-err),
1728 err);
1729 }
Jesse Halldc225072016-05-30 22:40:14 -07001730
Nicolas Capens147b7da2021-04-09 14:53:06 -04001731 err =
1732 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001733 if (err != android::OK) {
1734 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1735 strerror(-err), err);
1736 return VK_ERROR_SURFACE_LOST_KHR;
1737 }
1738
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301739 int swap_interval =
1740 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001741 err = window->setSwapInterval(window, swap_interval);
1742 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001743 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1744 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001745 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001746 }
1747
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001748 err = native_window_set_shared_buffer_mode(window, false);
1749 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001750 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1751 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001752 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001753 }
1754
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001755 err = native_window_set_auto_refresh(window, false);
1756 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001757 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1758 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001759 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001760 }
1761
Jesse Halld7b994a2015-09-07 14:17:37 -07001762 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001763
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001764 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001765
Trevor David Blackf499b5a2023-07-14 17:30:41 +00001766 err = native_window_set_buffers_format(
1767 window, static_cast<int>(native_pixel_format));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001768 if (err != android::OK) {
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001769 ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)",
Trevor David Blackf499b5a2023-07-14 17:30:41 +00001770 toString(native_pixel_format).c_str(), strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001771 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001772 }
Yiwei Zhang51572c22022-07-22 23:08:30 +00001773
1774 /* Respect consumer default dataspace upon HAL_DATASPACE_ARBITRARY. */
sergiuferentz47989332023-09-26 10:24:36 +00001775 if (native_dataspace != DataSpace::ARBITRARY) {
1776 err = native_window_set_buffers_data_space(
1777 window, static_cast<android_dataspace_t>(native_dataspace));
Yiwei Zhang51572c22022-07-22 23:08:30 +00001778 if (err != android::OK) {
1779 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1780 native_dataspace, strerror(-err), err);
1781 return VK_ERROR_SURFACE_LOST_KHR;
1782 }
Jesse Hall517274a2016-02-10 00:07:18 -08001783 }
1784
Jesse Hall3dd678a2016-01-08 21:52:01 -08001785 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001786 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001787 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001788 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001789 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1790 create_info->imageExtent.width, create_info->imageExtent.height,
1791 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001792 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001793 }
1794
Jesse Hall178b6962016-02-24 15:39:50 -08001795 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1796 // applied during rendering. native_window_set_transform() expects the
1797 // inverse: the transform the app is requesting that the compositor perform
1798 // during composition. With native windows, pre-transform works by rendering
1799 // with the same transform the compositor is applying (as in Vulkan), but
1800 // then requesting the inverse transform, so that when the compositor does
1801 // it's job the two transforms cancel each other out and the compositor ends
1802 // up applying an identity transform to the app's buffer.
1803 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001804 window, InvertTransformToNative(create_info->preTransform));
1805 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001806 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1807 InvertTransformToNative(create_info->preTransform),
1808 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001809 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001810 }
1811
Jesse Hallf64ca122015-11-03 16:11:10 -08001812 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001813 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1814 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001815 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1816 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001817 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001818 }
1819
Chris Forbes97ef4612017-03-30 19:37:50 +13001820 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001821 if (IsSharedPresentMode(create_info->presentMode)) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001822 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001823 err = native_window_set_shared_buffer_mode(window, true);
1824 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001825 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1826 return VK_ERROR_SURFACE_LOST_KHR;
1827 }
1828 }
1829
1830 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001831 err = native_window_set_auto_refresh(window, true);
1832 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001833 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1834 return VK_ERROR_SURFACE_LOST_KHR;
1835 }
1836 }
1837
Ian Elliott16c443c2021-11-30 17:10:32 -07001838 int query_value;
Trevor David Blackabab5a02023-12-19 20:27:07 +00001839 // TODO: Now that we are calling into GPDSC2 directly, this query may be redundant
1840 // the call to std::max(min_buffer_count, num_images) may be redundant as well
Ian Elliott16c443c2021-11-30 17:10:32 -07001841 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1842 &query_value);
1843 if (err != android::OK || query_value < 0) {
1844 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1845 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001846 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001847 }
Trevor David Black8c0122e2023-09-07 20:33:54 +00001848 const uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Chris Forbes2c8fc752017-03-17 11:28:32 +13001849
Ian Elliott5396b702021-12-13 19:32:34 -07001850 // Lower layer insists that we have at least min_undequeued_buffers + 1
1851 // buffers. This is wasteful and we'd like to relax it in the shared case,
1852 // but not all the pieces are in place for that to work yet. Note we only
1853 // lie to the lower layer--we don't want to give the app back a swapchain
1854 // with extra images (which they can't actually use!).
Trevor David Black8c0122e2023-09-07 20:33:54 +00001855 const uint32_t min_buffer_count = min_undequeued_buffers + 1;
1856
Trevor David Blackabab5a02023-12-19 20:27:07 +00001857 // Call into GPDSC2 to get the minimum and maximum allowable buffer count for the surface of
1858 // interest. This step is only necessary if the app requests a number of images
1859 // (create_info->minImageCount) that is less or more than the surface capabilities.
1860 // An app should be calling GPDSC2 and using those values to set create_info, but in the
1861 // event that the app has hard-coded image counts an error can occur
1862 VkSurfacePresentModeEXT present_mode = {
1863 VK_STRUCTURE_TYPE_SURFACE_PRESENT_MODE_EXT,
1864 nullptr,
1865 create_info->presentMode
1866 };
1867 VkPhysicalDeviceSurfaceInfo2KHR surface_info2 = {
1868 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR,
1869 &present_mode,
1870 create_info->surface
1871 };
1872 VkSurfaceCapabilities2KHR surface_capabilities2 = {
1873 VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR,
1874 nullptr,
1875 {},
1876 };
1877 result = GetPhysicalDeviceSurfaceCapabilities2KHR(GetData(device).driver_physical_device,
1878 &surface_info2, &surface_capabilities2);
1879
1880 uint32_t num_images = create_info->minImageCount;
1881 num_images = std::clamp(num_images,
1882 surface_capabilities2.surfaceCapabilities.minImageCount,
1883 surface_capabilities2.surfaceCapabilities.maxImageCount);
Trevor David Black8c0122e2023-09-07 20:33:54 +00001884
1885 const uint32_t buffer_count = std::max(min_buffer_count, num_images);
1886 err = native_window_set_buffer_count(window, buffer_count);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001887 if (err != android::OK) {
Trevor David Black8c0122e2023-09-07 20:33:54 +00001888 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", buffer_count,
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001889 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001890 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001891 }
1892
Ian Elliott12b7e2f2021-12-21 23:24:20 -07001893 // In shared mode the num_images must be one regardless of how many
1894 // buffers were allocated for the buffer queue.
1895 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1896 num_images = 1;
1897 }
1898
Tom Murphyea321842024-06-14 18:26:58 +00001899 VkImageFormatListCreateInfo extra_mutable_formats = {
1900 .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO_KHR,
1901 };
1902 VkImageFormatListCreateInfo* extra_mutable_formats_ptr;
1903
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001904 // Look through the create_info pNext chain passed to createSwapchainKHR
1905 // for an image compression control struct.
1906 // if one is found AND the appropriate extensions are enabled, create a
1907 // VkImageCompressionControlEXT structure to pass on to VkImageCreateInfo
1908 // TODO check for imageCompressionControlSwapchain feature is enabled
Trevor David Black2cc44682022-03-09 00:31:38 +00001909 void* usage_info_pNext = nullptr;
1910 VkImageCompressionControlEXT image_compression = {};
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001911 const VkSwapchainCreateInfoKHR* create_infos = create_info;
1912 while (create_infos->pNext) {
1913 create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(create_infos->pNext);
1914 switch (create_infos->sType) {
1915 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: {
1916 const VkImageCompressionControlEXT* compression_infos =
1917 reinterpret_cast<const VkImageCompressionControlEXT*>(create_infos);
1918 image_compression = *compression_infos;
1919 image_compression.pNext = nullptr;
1920 usage_info_pNext = &image_compression;
1921 } break;
Tom Murphyea321842024-06-14 18:26:58 +00001922 case VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO: {
1923 const VkImageFormatListCreateInfo* format_list =
1924 reinterpret_cast<const VkImageFormatListCreateInfo*>(
1925 create_infos);
1926 if (create_info->flags &
1927 VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR) {
1928 if (format_list && format_list->viewFormatCount > 0 &&
1929 format_list->pViewFormats) {
1930 extra_mutable_formats.viewFormatCount =
1931 format_list->viewFormatCount;
1932 extra_mutable_formats.pViewFormats =
1933 format_list->pViewFormats;
1934 extra_mutable_formats_ptr = &extra_mutable_formats;
1935 } else {
1936 ALOGE(
1937 "vk_swapchain_create_mutable_format_bit_khr was "
1938 "set during swapchain creation but no valid "
1939 "vkimageformatlistcreateinfo was found in the "
1940 "pnext chain");
1941 return VK_ERROR_INITIALIZATION_FAILED;
1942 }
1943 }
1944 } break;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001945 default:
1946 // Ignore all other info structs
1947 break;
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001948 }
Jesse Hall70f93352015-11-04 09:41:31 -08001949 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001950
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001951 // Get the appropriate native_usage for the images
1952 // Get the consumer usage
1953 uint64_t native_usage = surface.consumer_usage;
1954 // Determine if the swapchain is protected
1955 bool create_protected_swapchain = false;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001956 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001957 create_protected_swapchain = true;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001958 native_usage |= BufferUsage::PROTECTED;
1959 }
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001960 // Get the producer usage
1961 uint64_t producer_usage;
1962 result = getProducerUsage(device, create_info, swapchain_image_usage, create_protected_swapchain, &producer_usage);
1963 if (result != VK_SUCCESS) {
1964 return result;
1965 }
1966 native_usage |= producer_usage;
1967
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001968 err = native_window_set_usage(window, native_usage);
1969 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001970 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001971 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001972 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001973
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001974 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001975 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1976 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001977 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1978 strerror(-err), err);
1979 return VK_ERROR_SURFACE_LOST_KHR;
1980 }
1981
Alina Kalyakina3d6f3632023-03-22 17:13:47 +00001982 int64_t refresh_duration;
1983 err = native_window_get_refresh_cycle_duration(window, &refresh_duration);
1984 if (err != android::OK) {
1985 ALOGE("native_window_get_refresh_cycle_duration query failed: %s (%d)",
1986 strerror(-err), err);
1987 return VK_ERROR_SURFACE_LOST_KHR;
1988 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001989 // -- Allocate our Swapchain object --
1990 // After this point, we must deallocate the swapchain on error.
1991
Jesse Hall1f91d392015-12-11 16:28:44 -08001992 void* mem = allocator->pfnAllocation(allocator->pUserData,
1993 sizeof(Swapchain), alignof(Swapchain),
1994 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001995
Jesse Hall1356b0d2015-11-23 17:24:58 -08001996 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001997 return VK_ERROR_OUT_OF_HOST_MEMORY;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001998
silence_dogood73597592019-05-23 16:57:37 -07001999 Swapchain* swapchain = new (mem)
2000 Swapchain(surface, num_images, create_info->presentMode,
Alina Kalyakina3d6f3632023-03-22 17:13:47 +00002001 TranslateVulkanToNativeTransform(create_info->preTransform),
2002 refresh_duration);
Chris Forbesb56287a2017-01-12 14:28:58 +13002003 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
2004#pragma clang diagnostic push
2005#pragma clang diagnostic ignored "-Wold-style-cast"
2006 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
2007#pragma clang diagnostic pop
Trevor David Black2cc44682022-03-09 00:31:38 +00002008 .pNext = usage_info_pNext,
Chris Forbesb56287a2017-01-12 14:28:58 +13002009 .usage = swapchain_image_usage,
2010 };
Jesse Halld7b994a2015-09-07 14:17:37 -07002011 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07002012#pragma clang diagnostic push
2013#pragma clang diagnostic ignored "-Wold-style-cast"
2014 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2015#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13002016 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07002017 };
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002018
Jesse Halld7b994a2015-09-07 14:17:37 -07002019 VkImageCreateInfo image_create = {
2020 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002021 .pNext = nullptr,
Trevor David Blackd7320ef2023-08-23 21:21:51 +00002022 .flags = create_protected_swapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07002023 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08002024 .format = create_info->imageFormat,
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002025 .extent = {
2026 create_info->imageExtent.width,
2027 create_info->imageExtent.height,
2028 1
2029 },
Jesse Halld7b994a2015-09-07 14:17:37 -07002030 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08002031 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08002032 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07002033 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08002034 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08002035 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08002036 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07002037 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
2038 };
2039
Tom Murphyea321842024-06-14 18:26:58 +00002040 if (create_info->flags & VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR) {
2041 image_create.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
2042 image_create.flags |= VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR;
2043 }
2044
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002045 // Note: don't do deferred allocation for shared present modes. There's only one buffer
2046 // involved so very little benefit.
2047 if ((create_info->flags & VK_SWAPCHAIN_CREATE_DEFERRED_MEMORY_ALLOCATION_BIT_EXT) &&
2048 !IsSharedPresentMode(create_info->presentMode)) {
2049 // Don't want to touch the underlying gralloc buffers yet;
2050 // instead just create unbound VkImages which will later be bound to memory inside
2051 // AcquireNextImage.
2052 VkImageSwapchainCreateInfoKHR image_swapchain_create = {
2053 .sType = VK_STRUCTURE_TYPE_IMAGE_SWAPCHAIN_CREATE_INFO_KHR,
Tom Murphyea321842024-06-14 18:26:58 +00002054 .pNext = extra_mutable_formats_ptr,
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002055 .swapchain = HandleFromSwapchain(swapchain),
2056 };
2057 image_create.pNext = &image_swapchain_create;
Jesse Halld7b994a2015-09-07 14:17:37 -07002058
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002059 for (uint32_t i = 0; i < num_images; i++) {
2060 Swapchain::Image& img = swapchain->images[i];
2061 img.buffer = nullptr;
2062 img.dequeued = false;
2063
2064 result = dispatch.CreateImage(device, &image_create, nullptr, &img.image);
2065 if (result != VK_SUCCESS) {
2066 ALOGD("vkCreateImage w/ for deferred swapchain image failed: %u", result);
2067 break;
Yiwei Zhang702beb42019-11-29 17:59:55 -08002068 }
Jesse Halld7b994a2015-09-07 14:17:37 -07002069 }
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002070 } else {
2071 // -- Dequeue all buffers and create a VkImage for each --
2072 // Any failures during or after this must cancel the dequeued buffers.
Jesse Halld7b994a2015-09-07 14:17:37 -07002073
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002074 for (uint32_t i = 0; i < num_images; i++) {
2075 Swapchain::Image& img = swapchain->images[i];
Jesse Halld7b994a2015-09-07 14:17:37 -07002076
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002077 ANativeWindowBuffer* buffer;
2078 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
2079 if (err != android::OK) {
2080 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
2081 switch (-err) {
2082 case ENOMEM:
2083 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
2084 break;
2085 default:
2086 result = VK_ERROR_SURFACE_LOST_KHR;
2087 break;
2088 }
2089 break;
2090 }
2091 img.buffer = buffer;
2092 img.dequeued = true;
2093
2094 image_native_buffer.handle = img.buffer->handle;
2095 image_native_buffer.stride = img.buffer->stride;
2096 image_native_buffer.format = img.buffer->format;
2097 image_native_buffer.usage = int(img.buffer->usage);
2098 android_convertGralloc0To1Usage(int(img.buffer->usage),
2099 &image_native_buffer.usage2.producer,
2100 &image_native_buffer.usage2.consumer);
2101 image_native_buffer.usage3 = img.buffer->usage;
Trevor David Black0db7a092023-12-11 23:46:36 +00002102 image_native_buffer.ahb =
2103 ANativeWindowBuffer_getHardwareBuffer(img.buffer.get());
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002104 image_create.pNext = &image_native_buffer;
2105
Tom Murphyea321842024-06-14 18:26:58 +00002106 if (extra_mutable_formats_ptr) {
2107 extra_mutable_formats_ptr->pNext = image_create.pNext;
2108 image_create.pNext = extra_mutable_formats_ptr;
2109 }
2110
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002111 ATRACE_BEGIN("CreateImage");
2112 result =
2113 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
2114 ATRACE_END();
2115 if (result != VK_SUCCESS) {
2116 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
2117 break;
2118 }
Jesse Halld7b994a2015-09-07 14:17:37 -07002119 }
Jesse Halld7b994a2015-09-07 14:17:37 -07002120
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002121 // -- Cancel all buffers, returning them to the queue --
2122 // If an error occurred before, also destroy the VkImage and release the
2123 // buffer reference. Otherwise, we retain a strong reference to the buffer.
2124 for (uint32_t i = 0; i < num_images; i++) {
2125 Swapchain::Image& img = swapchain->images[i];
2126 if (img.dequeued) {
2127 if (!swapchain->shared) {
2128 window->cancelBuffer(window, img.buffer.get(),
2129 img.dequeue_fence);
2130 img.dequeue_fence = -1;
2131 img.dequeued = false;
2132 }
Chris Forbese0ced032017-03-30 19:44:15 +13002133 }
Chris Forbes31b85c22018-05-29 15:03:28 -07002134 }
Jesse Halld7b994a2015-09-07 14:17:37 -07002135 }
2136
2137 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07002138 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
2139 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07002140 return result;
2141 }
2142
Yiwei Zhang69395cd2019-07-03 16:55:39 -07002143 if (transform_hint != swapchain->pre_transform) {
2144 // Log that the app is not doing pre-rotation.
2145 android::GraphicsEnv::getInstance().setTargetStats(
2146 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
2147 }
2148
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00002149 // Set stats for creating a Vulkan swapchain
2150 android::GraphicsEnv::getInstance().setTargetStats(
2151 android::GpuStatsInfo::Stats::CREATED_VULKAN_SWAPCHAIN);
2152
Yiwei Zhang3b88f312023-04-18 23:11:35 +00002153 surface.used_by_swapchain = true;
Jesse Halldc225072016-05-30 22:40:14 -07002154 surface.swapchain_handle = HandleFromSwapchain(swapchain);
2155 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07002156 return VK_SUCCESS;
2157}
2158
Jesse Halle1b12782015-11-30 11:27:32 -08002159VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08002160void DestroySwapchainKHR(VkDevice device,
2161 VkSwapchainKHR swapchain_handle,
2162 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002163 ATRACE_CALL();
2164
Yiwei Zhang533cea92019-06-03 18:43:24 -07002165 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07002166}
2167
Jesse Halle1b12782015-11-30 11:27:32 -08002168VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08002169VkResult GetSwapchainImagesKHR(VkDevice,
2170 VkSwapchainKHR swapchain_handle,
2171 uint32_t* count,
2172 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002173 ATRACE_CALL();
2174
Jesse Halld7b994a2015-09-07 14:17:37 -07002175 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07002176 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
2177 "getting images for non-active swapchain 0x%" PRIx64
2178 "; only dequeued image handles are valid",
2179 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07002180 VkResult result = VK_SUCCESS;
2181 if (images) {
2182 uint32_t n = swapchain.num_images;
2183 if (*count < swapchain.num_images) {
2184 n = *count;
2185 result = VK_INCOMPLETE;
2186 }
2187 for (uint32_t i = 0; i < n; i++)
2188 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07002189 *count = n;
2190 } else {
2191 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07002192 }
Jesse Halld7b994a2015-09-07 14:17:37 -07002193 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07002194}
2195
Jesse Halle1b12782015-11-30 11:27:32 -08002196VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08002197VkResult AcquireNextImageKHR(VkDevice device,
2198 VkSwapchainKHR swapchain_handle,
2199 uint64_t timeout,
2200 VkSemaphore semaphore,
2201 VkFence vk_fence,
2202 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002203 ATRACE_CALL();
2204
Jesse Halld7b994a2015-09-07 14:17:37 -07002205 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08002206 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07002207 VkResult result;
2208 int err;
2209
Jesse Halldc225072016-05-30 22:40:14 -07002210 if (swapchain.surface.swapchain_handle != swapchain_handle)
2211 return VK_ERROR_OUT_OF_DATE_KHR;
2212
Chris Forbesc88409c2017-03-30 19:47:37 +13002213 if (swapchain.shared) {
2214 // In shared mode, we keep the buffer dequeued all the time, so we don't
2215 // want to dequeue a buffer here. Instead, just ask the driver to ensure
2216 // the semaphore and fence passed to us will be signalled.
2217 *image_index = 0;
2218 result = GetData(device).driver.AcquireImageANDROID(
2219 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
2220 return result;
2221 }
2222
Yiwei Zhang705c2e62019-12-18 23:12:43 -08002223 const nsecs_t acquire_next_image_timeout =
2224 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
2225 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
2226 // Cache the timeout to avoid the duplicate binder cost.
2227 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
2228 acquire_next_image_timeout);
2229 if (err != android::OK) {
2230 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
2231 strerror(-err), err);
2232 return VK_ERROR_SURFACE_LOST_KHR;
2233 }
2234 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
2235 }
2236
Jesse Halld7b994a2015-09-07 14:17:37 -07002237 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08002238 int fence_fd;
2239 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07002240 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08002241 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
2242 return timeout ? VK_TIMEOUT : VK_NOT_READY;
2243 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07002244 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07002245 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07002246 }
2247
2248 uint32_t idx;
2249 for (idx = 0; idx < swapchain.num_images; idx++) {
2250 if (swapchain.images[idx].buffer.get() == buffer) {
2251 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08002252 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07002253 break;
2254 }
2255 }
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002256
2257 // If this is a deferred alloc swapchain, this may be the first time we've
2258 // seen a particular buffer. If so, there should be an empty slot. Find it,
2259 // and bind the gralloc buffer to the VkImage for that slot. If there is no
2260 // empty slot, then we dequeued an unexpected buffer. Non-deferred swapchains
2261 // will also take this path, but will never have an empty slot since we
2262 // populated them all upfront.
2263 if (idx == swapchain.num_images) {
2264 for (idx = 0; idx < swapchain.num_images; idx++) {
2265 if (!swapchain.images[idx].buffer) {
2266 // Note: this structure is technically required for
2267 // Vulkan correctness, even though the driver is probably going
2268 // to use everything from the VkNativeBufferANDROID below.
2269 // This is kindof silly, but it's how we did the ANB
2270 // side of VK_KHR_swapchain v69, so we're stuck with it unless
2271 // we want to go tinkering with the ANB spec some more.
2272 VkBindImageMemorySwapchainInfoKHR bimsi = {
2273 .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR,
2274 .pNext = nullptr,
2275 .swapchain = swapchain_handle,
2276 .imageIndex = idx,
2277 };
2278 VkNativeBufferANDROID nb = {
2279 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2280 .pNext = &bimsi,
2281 .handle = buffer->handle,
2282 .stride = buffer->stride,
2283 .format = buffer->format,
2284 .usage = int(buffer->usage),
Trevor David Black0db7a092023-12-11 23:46:36 +00002285 .usage3 = buffer->usage,
2286 .ahb = ANativeWindowBuffer_getHardwareBuffer(buffer),
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002287 };
Trevor David Black0db7a092023-12-11 23:46:36 +00002288 android_convertGralloc0To1Usage(int(buffer->usage),
2289 &nb.usage2.producer,
2290 &nb.usage2.consumer);
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002291 VkBindImageMemoryInfo bimi = {
2292 .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO,
2293 .pNext = &nb,
2294 .image = swapchain.images[idx].image,
2295 .memory = VK_NULL_HANDLE,
2296 .memoryOffset = 0,
2297 };
2298 result = GetData(device).driver.BindImageMemory2(device, 1, &bimi);
2299 if (result != VK_SUCCESS) {
2300 // This shouldn't really happen. If it does, something is probably
2301 // unrecoverably wrong with the swapchain and its images. Cancel
2302 // the buffer and declare the swapchain broken.
2303 ALOGE("failed to do deferred gralloc buffer bind");
2304 window->cancelBuffer(window, buffer, fence_fd);
2305 return VK_ERROR_OUT_OF_DATE_KHR;
2306 }
2307
2308 swapchain.images[idx].dequeued = true;
2309 swapchain.images[idx].dequeue_fence = fence_fd;
2310 swapchain.images[idx].buffer = buffer;
2311 break;
2312 }
2313 }
2314 }
2315
2316 // The buffer doesn't match any slot. This shouldn't normally happen, but is
2317 // possible if the bufferqueue is reconfigured behind libvulkan's back. If this
2318 // happens, just declare the swapchain to be broken and the app will recreate it.
Jesse Halld7b994a2015-09-07 14:17:37 -07002319 if (idx == swapchain.num_images) {
2320 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08002321 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07002322 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07002323 }
2324
2325 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08002326 if (fence_fd != -1) {
2327 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07002328 if (fence_clone == -1) {
2329 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
2330 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08002331 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07002332 }
2333 }
2334
Chia-I Wu4a6a9162016-03-26 07:17:34 +08002335 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08002336 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07002337 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08002338 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
2339 // even if the call fails. We could close it ourselves on failure, but
2340 // that would create a race condition if the driver closes it on a
2341 // failure path: some other thread might create an fd with the same
2342 // number between the time the driver closes it and the time we close
2343 // it. We must assume one of: the driver *always* closes it even on
2344 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08002345 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07002346 swapchain.images[idx].dequeued = false;
2347 swapchain.images[idx].dequeue_fence = -1;
2348 return result;
2349 }
2350
2351 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07002352 return VK_SUCCESS;
2353}
2354
Daniel Kochf25f5bb2017-10-05 00:26:58 -04002355VKAPI_ATTR
2356VkResult AcquireNextImage2KHR(VkDevice device,
2357 const VkAcquireNextImageInfoKHR* pAcquireInfo,
2358 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002359 ATRACE_CALL();
2360
Daniel Kochf25f5bb2017-10-05 00:26:58 -04002361 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
2362 pAcquireInfo->timeout, pAcquireInfo->semaphore,
2363 pAcquireInfo->fence, pImageIndex);
2364}
2365
Jesse Halldc225072016-05-30 22:40:14 -07002366static VkResult WorstPresentResult(VkResult a, VkResult b) {
2367 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
2368 // (in spec version 1.0.14).
2369 static const VkResult kWorstToBest[] = {
2370 VK_ERROR_DEVICE_LOST,
2371 VK_ERROR_SURFACE_LOST_KHR,
2372 VK_ERROR_OUT_OF_DATE_KHR,
2373 VK_ERROR_OUT_OF_DEVICE_MEMORY,
2374 VK_ERROR_OUT_OF_HOST_MEMORY,
2375 VK_SUBOPTIMAL_KHR,
2376 };
2377 for (auto result : kWorstToBest) {
2378 if (a == result || b == result)
2379 return result;
2380 }
2381 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
2382 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
2383 return a != VK_SUCCESS ? a : b;
2384}
2385
Chris Forbes4cd01fb2022-10-19 11:35:16 +13002386// KHR_incremental_present aspect of QueuePresentKHR
2387static void SetSwapchainSurfaceDamage(ANativeWindow *window, const VkPresentRegionKHR *pRegion) {
2388 std::vector<android_native_rect_t> rects(pRegion->rectangleCount);
2389 for (auto i = 0u; i < pRegion->rectangleCount; i++) {
2390 auto const& rect = pRegion->pRectangles[i];
2391 if (rect.layer > 0) {
2392 ALOGV("vkQueuePresentKHR ignoring invalid layer (%u); using layer 0 instead",
2393 rect.layer);
2394 }
2395
2396 rects[i].left = rect.offset.x;
2397 rects[i].bottom = rect.offset.y;
2398 rects[i].right = rect.offset.x + rect.extent.width;
2399 rects[i].top = rect.offset.y + rect.extent.height;
2400 }
2401 native_window_set_surface_damage(window, rects.data(), rects.size());
2402}
2403
2404// GOOGLE_display_timing aspect of QueuePresentKHR
2405static void SetSwapchainFrameTimestamp(Swapchain &swapchain, const VkPresentTimeGOOGLE *pTime) {
2406 ANativeWindow *window = swapchain.surface.window.get();
2407
2408 // We don't know whether the app will actually use GOOGLE_display_timing
2409 // with a particular swapchain until QueuePresent; enable it on the BQ
2410 // now if needed
2411 if (!swapchain.frame_timestamps_enabled) {
2412 ALOGV("Calling native_window_enable_frame_timestamps(true)");
2413 native_window_enable_frame_timestamps(window, true);
2414 swapchain.frame_timestamps_enabled = true;
2415 }
2416
2417 // Record the nativeFrameId so it can be later correlated to
2418 // this present.
2419 uint64_t nativeFrameId = 0;
2420 int err = native_window_get_next_frame_id(
2421 window, &nativeFrameId);
2422 if (err != android::OK) {
2423 ALOGE("Failed to get next native frame ID.");
2424 }
2425
2426 // Add a new timing record with the user's presentID and
2427 // the nativeFrameId.
2428 swapchain.timing.emplace_back(pTime, nativeFrameId);
2429 if (swapchain.timing.size() > MAX_TIMING_INFOS) {
2430 swapchain.timing.erase(
2431 swapchain.timing.begin(),
2432 swapchain.timing.begin() + swapchain.timing.size() - MAX_TIMING_INFOS);
2433 }
2434 if (pTime->desiredPresentTime) {
2435 ALOGV(
2436 "Calling native_window_set_buffers_timestamp(%" PRId64 ")",
2437 pTime->desiredPresentTime);
2438 native_window_set_buffers_timestamp(
2439 window,
2440 static_cast<int64_t>(pTime->desiredPresentTime));
2441 }
2442}
2443
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002444// EXT_swapchain_maintenance1 present mode change
2445static bool SetSwapchainPresentMode(ANativeWindow *window, VkPresentModeKHR mode) {
2446 // There is no dynamic switching between non-shared present modes.
2447 // All we support is switching between demand and continuous refresh.
2448 if (!IsSharedPresentMode(mode))
2449 return true;
2450
2451 int err = native_window_set_auto_refresh(window,
2452 mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
2453 if (err != android::OK) {
2454 ALOGE("native_window_set_auto_refresh() failed: %s (%d)",
2455 strerror(-err), err);
2456 return false;
2457 }
2458
2459 return true;
2460}
2461
Chris Forbes4cd01fb2022-10-19 11:35:16 +13002462static VkResult PresentOneSwapchain(
2463 VkQueue queue,
2464 Swapchain& swapchain,
2465 uint32_t imageIndex,
2466 const VkPresentRegionKHR *pRegion,
2467 const VkPresentTimeGOOGLE *pTime,
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002468 VkFence presentFence,
2469 const VkPresentModeKHR *pPresentMode,
Chris Forbes4cd01fb2022-10-19 11:35:16 +13002470 uint32_t waitSemaphoreCount,
2471 const VkSemaphore *pWaitSemaphores) {
2472
2473 VkDevice device = GetData(queue).driver_device;
2474 const auto& dispatch = GetData(queue).driver;
2475
2476 Swapchain::Image& img = swapchain.images[imageIndex];
2477 VkResult swapchain_result = VK_SUCCESS;
2478 VkResult result;
2479 int err;
2480
2481 // XXX: long standing issue: QueueSignalReleaseImageANDROID consumes the
2482 // wait semaphores, so this doesn't actually work for the multiple swapchain
2483 // case.
2484 int fence = -1;
2485 result = dispatch.QueueSignalReleaseImageANDROID(
2486 queue, waitSemaphoreCount,
2487 pWaitSemaphores, img.image, &fence);
2488 if (result != VK_SUCCESS) {
2489 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
2490 swapchain_result = result;
2491 }
2492 if (img.release_fence >= 0)
2493 close(img.release_fence);
2494 img.release_fence = fence < 0 ? -1 : dup(fence);
2495
2496 if (swapchain.surface.swapchain_handle == HandleFromSwapchain(&swapchain)) {
2497 ANativeWindow* window = swapchain.surface.window.get();
2498 if (swapchain_result == VK_SUCCESS) {
2499
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002500 if (presentFence != VK_NULL_HANDLE) {
2501 int fence_copy = fence < 0 ? -1 : dup(fence);
2502 VkImportFenceFdInfoKHR iffi = {
2503 VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR,
2504 nullptr,
2505 presentFence,
2506 VK_FENCE_IMPORT_TEMPORARY_BIT,
2507 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT,
2508 fence_copy,
2509 };
2510 if (VK_SUCCESS != dispatch.ImportFenceFdKHR(device, &iffi) && fence_copy >= 0) {
2511 // ImportFenceFdKHR takes ownership only if it succeeds
2512 close(fence_copy);
2513 }
2514 }
2515
Chris Forbes4cd01fb2022-10-19 11:35:16 +13002516 if (pRegion) {
2517 SetSwapchainSurfaceDamage(window, pRegion);
2518 }
2519 if (pTime) {
2520 SetSwapchainFrameTimestamp(swapchain, pTime);
2521 }
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002522 if (pPresentMode) {
2523 if (!SetSwapchainPresentMode(window, *pPresentMode))
2524 swapchain_result = WorstPresentResult(swapchain_result,
2525 VK_ERROR_SURFACE_LOST_KHR);
2526 }
Chris Forbes4cd01fb2022-10-19 11:35:16 +13002527
2528 err = window->queueBuffer(window, img.buffer.get(), fence);
2529 // queueBuffer always closes fence, even on error
2530 if (err != android::OK) {
2531 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
2532 swapchain_result = WorstPresentResult(
2533 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
2534 } else {
2535 if (img.dequeue_fence >= 0) {
2536 close(img.dequeue_fence);
2537 img.dequeue_fence = -1;
2538 }
2539 img.dequeued = false;
2540 }
2541
2542 // If the swapchain is in shared mode, immediately dequeue the
2543 // buffer so it can be presented again without an intervening
2544 // call to AcquireNextImageKHR. We expect to get the same buffer
2545 // back from every call to dequeueBuffer in this mode.
2546 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
2547 ANativeWindowBuffer* buffer;
2548 int fence_fd;
2549 err = window->dequeueBuffer(window, &buffer, &fence_fd);
2550 if (err != android::OK) {
2551 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
2552 swapchain_result = WorstPresentResult(swapchain_result,
2553 VK_ERROR_SURFACE_LOST_KHR);
2554 } else if (img.buffer != buffer) {
2555 ALOGE("got wrong image back for shared swapchain");
2556 swapchain_result = WorstPresentResult(swapchain_result,
2557 VK_ERROR_SURFACE_LOST_KHR);
2558 } else {
2559 img.dequeue_fence = fence_fd;
2560 img.dequeued = true;
2561 }
2562 }
2563 }
2564 if (swapchain_result != VK_SUCCESS) {
2565 OrphanSwapchain(device, &swapchain);
2566 }
2567 // Android will only return VK_SUBOPTIMAL_KHR for vkQueuePresentKHR,
2568 // and only when the window's transform/rotation changes. Extent
2569 // changes will not cause VK_SUBOPTIMAL_KHR because of the
2570 // application issues that were caused when the following transform
2571 // change was added.
2572 int window_transform_hint;
2573 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
2574 &window_transform_hint);
2575 if (err != android::OK) {
2576 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
2577 strerror(-err), err);
2578 swapchain_result = WorstPresentResult(
2579 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
2580 }
2581 if (swapchain.pre_transform != window_transform_hint) {
2582 swapchain_result =
2583 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
2584 }
2585 } else {
2586 ReleaseSwapchainImage(device, swapchain.shared, nullptr, fence,
2587 img, true);
2588 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
2589 }
2590
2591 return swapchain_result;
2592}
2593
Jesse Halle1b12782015-11-30 11:27:32 -08002594VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08002595VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002596 ATRACE_CALL();
2597
Jesse Halld7b994a2015-09-07 14:17:37 -07002598 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
2599 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
2600 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07002601
Jesse Halld7b994a2015-09-07 14:17:37 -07002602 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07002603
Ian Elliottcb351132016-12-13 10:30:40 -07002604 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002605 const VkPresentRegionsKHR* present_regions = nullptr;
2606 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002607 const VkSwapchainPresentFenceInfoEXT* present_fences = nullptr;
2608 const VkSwapchainPresentModeInfoEXT* present_modes = nullptr;
2609
Ian Elliottcb351132016-12-13 10:30:40 -07002610 const VkPresentRegionsKHR* next =
2611 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
2612 while (next) {
2613 switch (next->sType) {
2614 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
2615 present_regions = next;
2616 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07002617 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002618 present_times =
2619 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
2620 break;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002621 case VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_FENCE_INFO_EXT:
2622 present_fences =
2623 reinterpret_cast<const VkSwapchainPresentFenceInfoEXT*>(next);
2624 break;
2625 case VK_STRUCTURE_TYPE_SWAPCHAIN_PRESENT_MODE_INFO_EXT:
2626 present_modes =
2627 reinterpret_cast<const VkSwapchainPresentModeInfoEXT*>(next);
2628 break;
Ian Elliottcb351132016-12-13 10:30:40 -07002629 default:
2630 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
2631 next->sType);
2632 break;
2633 }
2634 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
2635 }
2636 ALOGV_IF(
2637 present_regions &&
2638 present_regions->swapchainCount != present_info->swapchainCount,
2639 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002640 ALOGV_IF(present_times &&
2641 present_times->swapchainCount != present_info->swapchainCount,
2642 "VkPresentTimesInfoGOOGLE::swapchainCount != "
2643 "VkPresentInfo::swapchainCount");
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002644 ALOGV_IF(present_fences &&
2645 present_fences->swapchainCount != present_info->swapchainCount,
2646 "VkSwapchainPresentFenceInfoEXT::swapchainCount != "
2647 "VkPresentInfo::swapchainCount");
2648 ALOGV_IF(present_modes &&
2649 present_modes->swapchainCount != present_info->swapchainCount,
2650 "VkSwapchainPresentModeInfoEXT::swapchainCount != "
2651 "VkPresentInfo::swapchainCount");
2652
Ian Elliottcb351132016-12-13 10:30:40 -07002653 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002654 (present_regions) ? present_regions->pRegions : nullptr;
2655 const VkPresentTimeGOOGLE* times =
2656 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07002657
Jesse Halld7b994a2015-09-07 14:17:37 -07002658 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
2659 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08002660 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Halld7b994a2015-09-07 14:17:37 -07002661
Chris Forbes4cd01fb2022-10-19 11:35:16 +13002662 VkResult swapchain_result = PresentOneSwapchain(
2663 queue,
2664 swapchain,
2665 present_info->pImageIndices[sc],
2666 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr,
2667 times ? &times[sc] : nullptr,
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002668 present_fences ? present_fences->pFences[sc] : VK_NULL_HANDLE,
2669 present_modes ? &present_modes->pPresentModes[sc] : nullptr,
Chris Forbes4cd01fb2022-10-19 11:35:16 +13002670 present_info->waitSemaphoreCount,
2671 present_info->pWaitSemaphores);
Jesse Halld7b994a2015-09-07 14:17:37 -07002672
Jesse Halla9e57032015-11-30 01:03:10 -08002673 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07002674 present_info->pResults[sc] = swapchain_result;
2675
2676 if (swapchain_result != final_result)
2677 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07002678 }
2679
2680 return final_result;
2681}
Jesse Hallb1352bc2015-09-04 16:12:33 -07002682
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002683VKAPI_ATTR
2684VkResult GetRefreshCycleDurationGOOGLE(
2685 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07002686 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002687 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002688 ATRACE_CALL();
2689
Ian Elliott62c48c92017-01-20 13:13:20 -07002690 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Alina Kalyakina3d6f3632023-03-22 17:13:47 +00002691 VkResult result = swapchain.get_refresh_duration(pDisplayTimingProperties->refreshDuration);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002692
2693 return result;
2694}
2695
2696VKAPI_ATTR
2697VkResult GetPastPresentationTimingGOOGLE(
2698 VkDevice,
2699 VkSwapchainKHR swapchain_handle,
2700 uint32_t* count,
2701 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002702 ATRACE_CALL();
2703
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002704 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002705 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2706 return VK_ERROR_OUT_OF_DATE_KHR;
2707 }
2708
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002709 ANativeWindow* window = swapchain.surface.window.get();
2710 VkResult result = VK_SUCCESS;
2711
2712 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07002713 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002714 native_window_enable_frame_timestamps(window, true);
2715 swapchain.frame_timestamps_enabled = true;
2716 }
2717
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002718 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07002719 // Get the latest ready timing count before copying, since the copied
2720 // timing info will be erased in copy_ready_timings function.
2721 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07002722 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002723 // Check the *count here against the recorded ready timing count, since
2724 // *count can be overwritten per spec describes.
2725 if (*count < n) {
2726 result = VK_INCOMPLETE;
2727 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002728 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07002729 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002730 }
2731
2732 return result;
2733}
2734
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002735VKAPI_ATTR
2736VkResult GetSwapchainStatusKHR(
2737 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13002738 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002739 ATRACE_CALL();
2740
Chris Forbes4e18ba82017-01-20 12:50:17 +13002741 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002742 VkResult result = VK_SUCCESS;
2743
Chris Forbes4e18ba82017-01-20 12:50:17 +13002744 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2745 return VK_ERROR_OUT_OF_DATE_KHR;
2746 }
2747
Yiwei Zhanga885c062019-10-24 12:07:57 -07002748 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002749
2750 return result;
2751}
2752
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002753VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002754 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002755 uint32_t swapchainCount,
2756 const VkSwapchainKHR* pSwapchains,
2757 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002758 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002759
2760 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
2761 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
2762 if (!swapchain)
2763 continue;
2764
2765 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
2766
2767 ANativeWindow* window = swapchain->surface.window.get();
2768
2769 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
2770 const android_smpte2086_metadata smpteMetdata = {
2771 {vulkanMetadata.displayPrimaryRed.x,
2772 vulkanMetadata.displayPrimaryRed.y},
2773 {vulkanMetadata.displayPrimaryGreen.x,
2774 vulkanMetadata.displayPrimaryGreen.y},
2775 {vulkanMetadata.displayPrimaryBlue.x,
2776 vulkanMetadata.displayPrimaryBlue.y},
2777 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
2778 vulkanMetadata.maxLuminance,
2779 vulkanMetadata.minLuminance};
2780 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
2781
2782 const android_cta861_3_metadata cta8613Metadata = {
2783 vulkanMetadata.maxContentLightLevel,
2784 vulkanMetadata.maxFrameAverageLightLevel};
2785 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
2786 }
2787
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002788 return;
2789}
2790
Yiwei Zhang0f475222019-04-11 19:38:00 -07002791static void InterceptBindImageMemory2(
2792 uint32_t bind_info_count,
2793 const VkBindImageMemoryInfo* bind_infos,
2794 std::vector<VkNativeBufferANDROID>* out_native_buffers,
2795 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
2796 out_native_buffers->clear();
2797 out_bind_infos->clear();
2798
2799 if (!bind_info_count)
2800 return;
2801
2802 std::unordered_set<uint32_t> intercepted_indexes;
2803
2804 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2805 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2806 bind_infos[idx].pNext);
2807 while (info &&
2808 info->sType !=
2809 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
2810 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2811 info->pNext);
2812 }
2813
2814 if (!info)
2815 continue;
2816
2817 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
2818 "swapchain handle must not be NULL");
2819 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
2820 ALOG_ASSERT(
2821 info->imageIndex < swapchain->num_images,
2822 "imageIndex must be less than the number of images in swapchain");
2823
2824 ANativeWindowBuffer* buffer =
2825 swapchain->images[info->imageIndex].buffer.get();
2826 VkNativeBufferANDROID native_buffer = {
2827#pragma clang diagnostic push
2828#pragma clang diagnostic ignored "-Wold-style-cast"
2829 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2830#pragma clang diagnostic pop
2831 .pNext = bind_infos[idx].pNext,
2832 .handle = buffer->handle,
2833 .stride = buffer->stride,
2834 .format = buffer->format,
2835 .usage = int(buffer->usage),
Trevor David Black0db7a092023-12-11 23:46:36 +00002836 .usage3 = buffer->usage,
2837 .ahb = ANativeWindowBuffer_getHardwareBuffer(buffer),
Yiwei Zhang0f475222019-04-11 19:38:00 -07002838 };
Trevor David Black0db7a092023-12-11 23:46:36 +00002839 android_convertGralloc0To1Usage(int(buffer->usage),
2840 &native_buffer.usage2.producer,
2841 &native_buffer.usage2.consumer);
Yiwei Zhang0f475222019-04-11 19:38:00 -07002842 // Reserve enough space to avoid letting re-allocation invalidate the
2843 // addresses of the elements inside.
2844 out_native_buffers->reserve(bind_info_count);
2845 out_native_buffers->emplace_back(native_buffer);
2846
2847 // Reserve the space now since we know how much is needed now.
2848 out_bind_infos->reserve(bind_info_count);
2849 out_bind_infos->emplace_back(bind_infos[idx]);
2850 out_bind_infos->back().pNext = &out_native_buffers->back();
2851
2852 intercepted_indexes.insert(idx);
2853 }
2854
2855 if (intercepted_indexes.empty())
2856 return;
2857
2858 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2859 if (intercepted_indexes.count(idx))
2860 continue;
2861 out_bind_infos->emplace_back(bind_infos[idx]);
2862 }
2863}
2864
Yiwei Zhang23143102019-04-10 18:24:05 -07002865VKAPI_ATTR
2866VkResult BindImageMemory2(VkDevice device,
2867 uint32_t bindInfoCount,
2868 const VkBindImageMemoryInfo* pBindInfos) {
2869 ATRACE_CALL();
2870
Yiwei Zhang0f475222019-04-11 19:38:00 -07002871 // out_native_buffers is for maintaining the lifecycle of the constructed
2872 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2873 std::vector<VkNativeBufferANDROID> out_native_buffers;
2874 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2875 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2876 &out_bind_infos);
2877 return GetData(device).driver.BindImageMemory2(
2878 device, bindInfoCount,
2879 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002880}
2881
2882VKAPI_ATTR
2883VkResult BindImageMemory2KHR(VkDevice device,
2884 uint32_t bindInfoCount,
2885 const VkBindImageMemoryInfo* pBindInfos) {
2886 ATRACE_CALL();
2887
Yiwei Zhang0f475222019-04-11 19:38:00 -07002888 std::vector<VkNativeBufferANDROID> out_native_buffers;
2889 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2890 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2891 &out_bind_infos);
2892 return GetData(device).driver.BindImageMemory2KHR(
2893 device, bindInfoCount,
2894 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002895}
2896
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00002897VKAPI_ATTR
2898VkResult ReleaseSwapchainImagesEXT(VkDevice /*device*/,
2899 const VkReleaseSwapchainImagesInfoEXT* pReleaseInfo) {
2900 ATRACE_CALL();
2901
2902 Swapchain& swapchain = *SwapchainFromHandle(pReleaseInfo->swapchain);
2903 ANativeWindow* window = swapchain.surface.window.get();
2904
2905 // If in shared present mode, don't actually release the image back to the BQ.
2906 // Both sides share it forever.
2907 if (swapchain.shared)
2908 return VK_SUCCESS;
2909
2910 for (uint32_t i = 0; i < pReleaseInfo->imageIndexCount; i++) {
2911 Swapchain::Image& img = swapchain.images[pReleaseInfo->pImageIndices[i]];
2912 window->cancelBuffer(window, img.buffer.get(), img.dequeue_fence);
2913
2914 // cancelBuffer has taken ownership of the dequeue fence
2915 img.dequeue_fence = -1;
2916 // if we're still holding a release fence, get rid of it now
2917 if (img.release_fence >= 0) {
2918 close(img.release_fence);
2919 img.release_fence = -1;
2920 }
2921 img.dequeued = false;
2922 }
2923
2924 return VK_SUCCESS;
2925}
2926
Chia-I Wu62262232016-03-26 07:06:44 +08002927} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002928} // namespace vulkan