blob: 64393be3fee03f37705398b66f3ab46bf5597efb [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
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Trevor David Blackf499b5a2023-07-14 17:30:41 +000019#include <aidl/android/hardware/graphics/common/PixelFormat.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070020#include <android/hardware/graphics/common/1.0/types.h>
Jesse Hall79927812017-03-23 11:03:23 -070021#include <grallocusage/GrallocUsageConversion.h>
Yiwei Zhang69395cd2019-07-03 16:55:39 -070022#include <graphicsenv/GraphicsEnv.h>
John Reck97678d42022-08-23 11:24:51 -040023#include <hardware/gralloc.h>
24#include <hardware/gralloc1.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070025#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070026#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070027#include <system/window.h>
28#include <ui/BufferQueueDefs.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080029#include <utils/StrongPointer.h>
Yiwei Zhang705c2e62019-12-18 23:12:43 -080030#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080031#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070032
33#include <algorithm>
34#include <unordered_set>
35#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070036
Chia-I Wu4a6a9162016-03-26 07:17:34 +080037#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070038
Trevor David Blackf499b5a2023-07-14 17:30:41 +000039using PixelFormat = aidl::android::hardware::graphics::common::PixelFormat;
Daniel Kochf25f5bb2017-10-05 00:26:58 -040040using android::hardware::graphics::common::V1_0::BufferUsage;
41
Chia-I Wu62262232016-03-26 07:06:44 +080042namespace vulkan {
43namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070044
Jesse Halld7b994a2015-09-07 14:17:37 -070045namespace {
46
John Reck97678d42022-08-23 11:24:51 -040047static uint64_t convertGralloc1ToBufferUsage(uint64_t producerUsage,
48 uint64_t consumerUsage) {
49 static_assert(uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) ==
50 uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN),
51 "expected ConsumerUsage and ProducerUsage CPU_READ_OFTEN "
52 "bits to match");
53 uint64_t merged = producerUsage | consumerUsage;
54 if ((merged & (GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN)) ==
55 GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) {
56 merged &= ~uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN);
57 merged |= BufferUsage::CPU_READ_OFTEN;
58 }
59 if ((merged & (GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) ==
60 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) {
61 merged &= ~uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN);
62 merged |= BufferUsage::CPU_WRITE_OFTEN;
63 }
64 return merged;
65}
66
Jesse Hall55bc0972016-02-23 16:43:29 -080067const VkSurfaceTransformFlagsKHR kSupportedTransforms =
68 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
69 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
70 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
71 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070072 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
73 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
74 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
75 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080076 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
77
78VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
79 // Native and Vulkan transforms are isomorphic, but are represented
80 // differently. Vulkan transforms are built up of an optional horizontal
81 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
82 // transforms are built up from a horizontal flip, vertical flip, and
83 // 90-degree rotation, all optional but always in that order.
84
Jesse Hall55bc0972016-02-23 16:43:29 -080085 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070086 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080087 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070088 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
89 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
90 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
91 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
92 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080093 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070094 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080095 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070096 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
97 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
98 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
99 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
100 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -0800101 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
102 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
103 default:
104 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
105 }
106}
107
Yiwei Zhang70a21962019-05-31 17:26:52 -0700108int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
109 switch (transform) {
110 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
111 return NATIVE_WINDOW_TRANSFORM_ROT_90;
112 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
113 return NATIVE_WINDOW_TRANSFORM_ROT_180;
114 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
115 return NATIVE_WINDOW_TRANSFORM_ROT_270;
116 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
117 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
118 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
119 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
120 NATIVE_WINDOW_TRANSFORM_ROT_90;
121 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
122 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
123 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
124 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
125 NATIVE_WINDOW_TRANSFORM_ROT_90;
126 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
127 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
128 default:
129 return 0;
130 }
131}
132
Jesse Hall178b6962016-02-24 15:39:50 -0800133int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
134 switch (transform) {
135 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
136 return NATIVE_WINDOW_TRANSFORM_ROT_270;
137 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
138 return NATIVE_WINDOW_TRANSFORM_ROT_180;
139 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
140 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700141 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
142 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
143 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
144 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
145 NATIVE_WINDOW_TRANSFORM_ROT_90;
146 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
147 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
148 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
149 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
150 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800151 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
152 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
153 default:
154 return 0;
155 }
156}
157
Ian Elliott8a977262017-01-19 09:05:58 -0700158class TimingInfo {
159 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800160 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700161 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800162 native_frame_id_(nativeFrameId) {}
163 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700164 return (timestamp_desired_present_time_ !=
165 NATIVE_WINDOW_TIMESTAMP_PENDING &&
166 timestamp_actual_present_time_ !=
167 NATIVE_WINDOW_TIMESTAMP_PENDING &&
168 timestamp_render_complete_time_ !=
169 NATIVE_WINDOW_TIMESTAMP_PENDING &&
170 timestamp_composition_latch_time_ !=
171 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700172 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700173 void calculate(int64_t rdur) {
174 bool anyTimestampInvalid =
175 (timestamp_actual_present_time_ ==
176 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
177 (timestamp_render_complete_time_ ==
178 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
179 (timestamp_composition_latch_time_ ==
180 NATIVE_WINDOW_TIMESTAMP_INVALID);
181 if (anyTimestampInvalid) {
182 ALOGE("Unexpectedly received invalid timestamp.");
183 vals_.actualPresentTime = 0;
184 vals_.earliestPresentTime = 0;
185 vals_.presentMargin = 0;
186 return;
187 }
188
189 vals_.actualPresentTime =
190 static_cast<uint64_t>(timestamp_actual_present_time_);
191 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700192 timestamp_render_complete_time_);
193 // Calculate vals_.earliestPresentTime, and potentially adjust
194 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
195 // is vals_.actualPresentTime. If we can subtract rdur (the duration
196 // of a refresh cycle) from vals_.earliestPresentTime (and also from
197 // vals_.presentMargin) and still leave a positive margin, then we can
198 // report to the application that it could have presented earlier than
199 // it did (per the extension specification). If for some reason, we
200 // can do this subtraction repeatedly, we do, since
201 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700202 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700203 while ((margin > rdur) &&
204 ((early_time - rdur) > timestamp_composition_latch_time_)) {
205 early_time -= rdur;
206 margin -= rdur;
207 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700208 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
209 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700210 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800211 void get_values(VkPastPresentationTimingGOOGLE* values) const {
212 *values = vals_;
213 }
Ian Elliott8a977262017-01-19 09:05:58 -0700214
215 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800216 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700217
Brian Anderson1049d1d2016-12-16 17:25:57 -0800218 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700219 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
220 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
221 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
222 int64_t timestamp_composition_latch_time_
223 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700224};
225
Jesse Hall1356b0d2015-11-23 17:24:58 -0800226struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800227 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700228 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700229 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800230};
231
232VkSurfaceKHR HandleFromSurface(Surface* surface) {
233 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
234}
235
236Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800237 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800238}
239
Ian Elliott8a977262017-01-19 09:05:58 -0700240// Maximum number of TimingInfo structs to keep per swapchain:
241enum { MAX_TIMING_INFOS = 10 };
242// Minimum number of frames to look for in the past (so we don't cause
243// syncronous requests to Surface Flinger):
244enum { MIN_NUM_FRAMES_AGO = 5 };
245
Jesse Hall1356b0d2015-11-23 17:24:58 -0800246struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700247 Swapchain(Surface& surface_,
248 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700249 VkPresentModeKHR present_mode,
250 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700251 : surface(surface_),
252 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700253 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700254 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300255 frame_timestamps_enabled(false),
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800256 acquire_next_image_timeout(-1),
Chris Forbesf8835642017-03-30 19:31:40 +1300257 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800258 present_mode ==
259 VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700260 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700261 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700262 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700263 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700264 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600265 uint64_t get_refresh_duration()
266 {
267 ANativeWindow* window = surface.window.get();
268 native_window_get_refresh_cycle_duration(
269 window,
270 &refresh_duration);
271 return static_cast<uint64_t>(refresh_duration);
272
273 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800274
275 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700276 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700277 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700278 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700279 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700280 int64_t refresh_duration;
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800281 nsecs_t acquire_next_image_timeout;
Chris Forbesf8835642017-03-30 19:31:40 +1300282 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700283
284 struct Image {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700285 Image()
286 : image(VK_NULL_HANDLE),
287 dequeue_fence(-1),
288 release_fence(-1),
289 dequeued(false) {}
Jesse Halld7b994a2015-09-07 14:17:37 -0700290 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800291 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700292 // The fence is only valid when the buffer is dequeued, and should be
293 // -1 any other time. When valid, we own the fd, and must ensure it is
294 // closed: either by closing it explicitly when queueing the buffer,
295 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
296 int dequeue_fence;
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700297 // This fence is a dup of the sync fd returned from the driver via
298 // vkQueueSignalReleaseImageANDROID upon vkQueuePresentKHR. We must
299 // ensure it is closed upon re-presenting or releasing the image.
300 int release_fence;
Jesse Halld7b994a2015-09-07 14:17:37 -0700301 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800302 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700303
Yiwei Zhang5e862202019-06-21 14:59:16 -0700304 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700305};
306
307VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
308 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
309}
310
311Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800312 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700313}
314
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700315static bool IsFencePending(int fd) {
316 if (fd < 0)
317 return false;
318
319 errno = 0;
320 return sync_wait(fd, 0 /* timeout */) == -1 && errno == ETIME;
321}
322
Jesse Halldc225072016-05-30 22:40:14 -0700323void ReleaseSwapchainImage(VkDevice device,
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000324 bool shared_present,
Jesse Halldc225072016-05-30 22:40:14 -0700325 ANativeWindow* window,
326 int release_fence,
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700327 Swapchain::Image& image,
328 bool defer_if_pending) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700329 ATRACE_CALL();
330
Jesse Halldc225072016-05-30 22:40:14 -0700331 ALOG_ASSERT(release_fence == -1 || image.dequeued,
332 "ReleaseSwapchainImage: can't provide a release fence for "
333 "non-dequeued images");
334
335 if (image.dequeued) {
336 if (release_fence >= 0) {
337 // We get here from vkQueuePresentKHR. The application is
338 // responsible for creating an execution dependency chain from
339 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
340 // (release_fence), so we can drop the dequeue_fence here.
341 if (image.dequeue_fence >= 0)
342 close(image.dequeue_fence);
343 } else {
344 // We get here during swapchain destruction, or various serious
345 // error cases e.g. when we can't create the release_fence during
346 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
347 // have already signalled, since the swapchain images are supposed
348 // to be idle before the swapchain is destroyed. In error cases,
349 // there may be rendering in flight to the image, but since we
350 // weren't able to create a release_fence, waiting for the
351 // dequeue_fence is about the best we can do.
352 release_fence = image.dequeue_fence;
353 }
354 image.dequeue_fence = -1;
355
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000356 // It's invalid to call cancelBuffer on a shared buffer
357 if (window && !shared_present) {
Jesse Halldc225072016-05-30 22:40:14 -0700358 window->cancelBuffer(window, image.buffer.get(), release_fence);
359 } else {
360 if (release_fence >= 0) {
361 sync_wait(release_fence, -1 /* forever */);
362 close(release_fence);
363 }
364 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700365 release_fence = -1;
Jesse Halldc225072016-05-30 22:40:14 -0700366 image.dequeued = false;
367 }
368
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700369 if (defer_if_pending && IsFencePending(image.release_fence))
370 return;
371
372 if (image.release_fence >= 0) {
373 close(image.release_fence);
374 image.release_fence = -1;
375 }
376
Jesse Halldc225072016-05-30 22:40:14 -0700377 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700378 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700379 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700380 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700381 image.image = VK_NULL_HANDLE;
382 }
383
384 image.buffer.clear();
385}
386
387void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
388 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
389 return;
Jesse Halldc225072016-05-30 22:40:14 -0700390 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000391 if (!swapchain->images[i].dequeued) {
392 ReleaseSwapchainImage(device, swapchain->shared, nullptr, -1,
393 swapchain->images[i], true);
394 }
Jesse Halldc225072016-05-30 22:40:14 -0700395 }
396 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700397 swapchain->timing.clear();
398}
399
400uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800401 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
402 return 0;
403 }
Ian Elliott8a977262017-01-19 09:05:58 -0700404
Brian Anderson1049d1d2016-12-16 17:25:57 -0800405 uint32_t num_ready = 0;
406 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
407 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700408 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800409 if (ti.ready()) {
410 // This TimingInfo is ready to be reported to the user. Add it
411 // to the num_ready.
412 num_ready++;
413 continue;
414 }
415 // This TimingInfo is not yet ready to be reported to the user,
416 // and so we should look for any available timestamps that
417 // might make it ready.
418 int64_t desired_present_time = 0;
419 int64_t render_complete_time = 0;
420 int64_t composition_latch_time = 0;
421 int64_t actual_present_time = 0;
422 // Obtain timestamps:
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800423 int err = native_window_get_frame_timestamps(
Brian Anderson1049d1d2016-12-16 17:25:57 -0800424 swapchain.surface.window.get(), ti.native_frame_id_,
425 &desired_present_time, &render_complete_time,
426 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700427 nullptr, //&first_composition_start_time,
428 nullptr, //&last_composition_start_time,
429 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800430 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700431 nullptr, //&dequeue_ready_time,
432 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800433
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800434 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800435 continue;
436 }
437
438 // Record the timestamp(s) we received, and then see if this TimingInfo
439 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700440 ti.timestamp_desired_present_time_ = desired_present_time;
441 ti.timestamp_actual_present_time_ = actual_present_time;
442 ti.timestamp_render_complete_time_ = render_complete_time;
443 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800444
445 if (ti.ready()) {
446 // The TimingInfo has received enough timestamps, and should now
447 // use those timestamps to calculate the info that should be
448 // reported to the user:
449 ti.calculate(swapchain.refresh_duration);
450 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700451 }
452 }
453 return num_ready;
454}
455
Ian Elliott8a977262017-01-19 09:05:58 -0700456void copy_ready_timings(Swapchain& swapchain,
457 uint32_t* count,
458 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800459 if (swapchain.timing.empty()) {
460 *count = 0;
461 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700462 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800463
464 size_t last_ready = swapchain.timing.size() - 1;
465 while (!swapchain.timing[last_ready].ready()) {
466 if (last_ready == 0) {
467 *count = 0;
468 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700469 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800470 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700471 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800472
473 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700474 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800475 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
476 const TimingInfo& ti = swapchain.timing[i];
477 if (ti.ready()) {
478 ti.get_values(&timings[num_copied]);
479 num_copied++;
480 }
481 num_to_remove++;
482 }
483
484 // Discard old frames that aren't ready if newer frames are ready.
485 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700486 swapchain.timing.erase(swapchain.timing.begin(),
487 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800488
Ian Elliott8a977262017-01-19 09:05:58 -0700489 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700490}
491
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000492PixelFormat GetNativePixelFormat(VkFormat format) {
493 PixelFormat native_format = PixelFormat::RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700494 switch (format) {
495 case VK_FORMAT_R8G8B8A8_UNORM:
496 case VK_FORMAT_R8G8B8A8_SRGB:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000497 native_format = PixelFormat::RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700498 break;
499 case VK_FORMAT_R5G6B5_UNORM_PACK16:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000500 native_format = PixelFormat::RGB_565;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700501 break;
502 case VK_FORMAT_R16G16B16A16_SFLOAT:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000503 native_format = PixelFormat::RGBA_FP16;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700504 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800505 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000506 native_format = PixelFormat::RGBA_1010102;
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500507 break;
508 case VK_FORMAT_R8_UNORM:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000509 native_format = PixelFormat::R_8;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700510 break;
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000511 case VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000512 native_format = PixelFormat::RGBA_10101010;
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000513 break;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700514 default:
515 ALOGV("unsupported swapchain format %d", format);
516 break;
517 }
518 return native_format;
519}
520
521android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
522 switch (colorspace) {
523 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
524 return HAL_DATASPACE_V0_SRGB;
525 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
526 return HAL_DATASPACE_DISPLAY_P3;
527 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
528 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600529 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
530 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700531 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
532 return HAL_DATASPACE_DCI_P3_LINEAR;
533 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
534 return HAL_DATASPACE_DCI_P3;
535 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
536 return HAL_DATASPACE_V0_SRGB_LINEAR;
537 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
538 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600539 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
540 return HAL_DATASPACE_BT2020_LINEAR;
541 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700542 return static_cast<android_dataspace>(
543 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
544 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600545 case VK_COLOR_SPACE_DOLBYVISION_EXT:
546 return static_cast<android_dataspace>(
547 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
548 HAL_DATASPACE_RANGE_FULL);
549 case VK_COLOR_SPACE_HDR10_HLG_EXT:
550 return static_cast<android_dataspace>(
551 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
552 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700553 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
554 return static_cast<android_dataspace>(
555 HAL_DATASPACE_STANDARD_ADOBE_RGB |
556 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
557 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
558 return HAL_DATASPACE_ADOBE_RGB;
559
560 // Pass through is intended to allow app to provide data that is passed
561 // to the display system without modification.
562 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
563 return HAL_DATASPACE_ARBITRARY;
564
565 default:
566 // This indicates that we don't know about the
567 // dataspace specified and we should indicate that
568 // it's unsupported
569 return HAL_DATASPACE_UNKNOWN;
570 }
571}
572
Jesse Halld7b994a2015-09-07 14:17:37 -0700573} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700574
Jesse Halle1b12782015-11-30 11:27:32 -0800575VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800576VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800577 VkInstance instance,
578 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
579 const VkAllocationCallbacks* allocator,
580 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800581 ATRACE_CALL();
582
Jesse Hall1f91d392015-12-11 16:28:44 -0800583 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800584 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800585 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
586 alignof(Surface),
587 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800588 if (!mem)
589 return VK_ERROR_OUT_OF_HOST_MEMORY;
590 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700591
Chia-I Wue8e689f2016-04-18 08:21:31 +0800592 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700593 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700594 int err = native_window_get_consumer_usage(surface->window.get(),
595 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800596 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700597 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
598 strerror(-err), err);
599 surface->~Surface();
600 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700601 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700602 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700603
Yiwei Zhang6435b322018-05-08 11:12:17 -0700604 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800605 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800606 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800607 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
608 err);
609 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800610 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700611 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800612 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700613
Jesse Hall1356b0d2015-11-23 17:24:58 -0800614 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700615 return VK_SUCCESS;
616}
617
Jesse Halle1b12782015-11-30 11:27:32 -0800618VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800619void DestroySurfaceKHR(VkInstance instance,
620 VkSurfaceKHR surface_handle,
621 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800622 ATRACE_CALL();
623
Jesse Hall1356b0d2015-11-23 17:24:58 -0800624 Surface* surface = SurfaceFromHandle(surface_handle);
625 if (!surface)
626 return;
627 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700628 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700629 "destroyed VkSurfaceKHR 0x%" PRIx64
630 " has active VkSwapchainKHR 0x%" PRIx64,
631 reinterpret_cast<uint64_t>(surface_handle),
632 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800633 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800634 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800635 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800636 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800637}
638
Jesse Halle1b12782015-11-30 11:27:32 -0800639VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800640VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
641 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000642 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800643 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000644 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800645 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800646}
647
Jesse Halle1b12782015-11-30 11:27:32 -0800648VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800649VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Ian Elliott1ce053f2022-03-16 09:49:53 -0600650 VkPhysicalDevice pdev,
Jesse Hallb00daad2015-11-29 19:46:20 -0800651 VkSurfaceKHR surface,
652 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800653 ATRACE_CALL();
654
Jesse Halld7b994a2015-09-07 14:17:37 -0700655 int err;
Jesse Halld7b994a2015-09-07 14:17:37 -0700656 int width, height;
Jesse Hall55bc0972016-02-23 16:43:29 -0800657 int transform_hint;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800658 int max_buffer_count;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600659 if (surface == VK_NULL_HANDLE) {
660 const InstanceData& instance_data = GetData(pdev);
661 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
662 bool surfaceless_enabled =
663 instance_data.hook_extensions.test(surfaceless);
664 if (!surfaceless_enabled) {
665 // It is an error to pass a surface==VK_NULL_HANDLE unless the
666 // VK_GOOGLE_surfaceless_query extension is enabled
667 return VK_ERROR_SURFACE_LOST_KHR;
668 }
669 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
670 // extension for this function is for
671 // VkSurfaceProtectedCapabilitiesKHR::supportsProtected. The following
672 // four values cannot be known without a surface. Default values will
673 // be supplied anyway, but cannot be relied upon.
Ian Elliottb4576372022-09-08 15:13:39 -0600674 width = 0xFFFFFFFF;
675 height = 0xFFFFFFFF;
676 transform_hint = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
677 capabilities->minImageCount = 0xFFFFFFFF;
678 capabilities->maxImageCount = 0xFFFFFFFF;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600679 } else {
680 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
681
682 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
683 if (err != android::OK) {
684 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
685 strerror(-err), err);
686 return VK_ERROR_SURFACE_LOST_KHR;
687 }
688 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
689 if (err != android::OK) {
690 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
691 strerror(-err), err);
692 return VK_ERROR_SURFACE_LOST_KHR;
693 }
694
695 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
696 &transform_hint);
697 if (err != android::OK) {
698 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
699 strerror(-err), err);
700 return VK_ERROR_SURFACE_LOST_KHR;
701 }
702
703 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT,
704 &max_buffer_count);
705 if (err != android::OK) {
706 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
707 strerror(-err), err);
708 return VK_ERROR_SURFACE_LOST_KHR;
709 }
Ian Elliottb4576372022-09-08 15:13:39 -0600710 capabilities->minImageCount = std::min(max_buffer_count, 3);
711 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800712 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700713
Jesse Hallfe2662d2016-02-09 13:26:59 -0800714 capabilities->currentExtent =
715 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
716
Yiwei Zhang70a21962019-05-31 17:26:52 -0700717 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800718 capabilities->minImageExtent = VkExtent2D{1, 1};
719 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700720
Trevor David Blackc00a1212022-11-14 21:13:14 +0000721 if (capabilities->maxImageExtent.height <
722 capabilities->currentExtent.height) {
723 capabilities->maxImageExtent.height =
724 capabilities->currentExtent.height;
725 }
726
727 if (capabilities->maxImageExtent.width <
728 capabilities->currentExtent.width) {
729 capabilities->maxImageExtent.width = capabilities->currentExtent.width;
730 }
731
Jesse Hallfe2662d2016-02-09 13:26:59 -0800732 capabilities->maxImageArrayLayers = 1;
733
Jesse Hall55bc0972016-02-23 16:43:29 -0800734 capabilities->supportedTransforms = kSupportedTransforms;
735 capabilities->currentTransform =
736 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700737
Jesse Hallfe2662d2016-02-09 13:26:59 -0800738 // On Android, window composition is a WindowManager property, not something
739 // associated with the bufferqueue. It can't be changed from here.
740 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700741
Jesse Hallb00daad2015-11-29 19:46:20 -0800742 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800743 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
744 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
745 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700746 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
747
Jesse Hallb1352bc2015-09-04 16:12:33 -0700748 return VK_SUCCESS;
749}
750
Jesse Halle1b12782015-11-30 11:27:32 -0800751VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700752VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
753 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800754 uint32_t* count,
755 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800756 ATRACE_CALL();
757
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700758 const InstanceData& instance_data = GetData(pdev);
759
Ian Elliott1ce053f2022-03-16 09:49:53 -0600760 uint64_t consumer_usage = 0;
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000761 bool colorspace_ext =
Ian Elliottf6df08e2022-03-16 21:27:49 -0600762 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600763 if (surface_handle == VK_NULL_HANDLE) {
764 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
765 bool surfaceless_enabled =
766 instance_data.hook_extensions.test(surfaceless);
767 if (!surfaceless_enabled) {
768 return VK_ERROR_SURFACE_LOST_KHR;
769 }
Chris Forbesa9e28de2022-10-20 09:05:48 +1300770 // Support for VK_GOOGLE_surfaceless_query.
Ian Elliott1ce053f2022-03-16 09:49:53 -0600771
772 // TODO(b/203826952): research proper value; temporarily use the
773 // values seen on Pixel
774 consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
775 } else {
776 Surface& surface = *SurfaceFromHandle(surface_handle);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600777 consumer_usage = surface.consumer_usage;
Ian Elliott6ba85d92022-02-18 16:44:58 -0700778 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700779
Charlie Lao324191f2019-12-13 11:03:16 -0800780 AHardwareBuffer_Desc desc = {};
781 desc.width = 1;
782 desc.height = 1;
783 desc.layers = 1;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600784 desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
Charlie Lao324191f2019-12-13 11:03:16 -0800785 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
786
787 // We must support R8G8B8A8
788 std::vector<VkSurfaceFormatKHR> all_formats = {
789 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700790 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700791 };
Charlie Lao324191f2019-12-13 11:03:16 -0800792
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000793 if (colorspace_ext) {
794 all_formats.emplace_back(VkSurfaceFormatKHR{
Jason Macnakca506332022-11-09 11:00:36 -0800795 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT});
796 all_formats.emplace_back(VkSurfaceFormatKHR{
797 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_PASS_THROUGH_EXT});
798 all_formats.emplace_back(VkSurfaceFormatKHR{
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000799 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_BT709_LINEAR_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800800 all_formats.emplace_back(VkSurfaceFormatKHR{
801 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
802 all_formats.emplace_back(VkSurfaceFormatKHR{
803 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
804 }
805
Ian Elliott1ce053f2022-03-16 09:49:53 -0600806 // NOTE: Any new formats that are added must be coordinated across different
807 // Android users. This includes the ANGLE team (a layered implementation of
808 // OpenGL-ES).
809
Charlie Lao324191f2019-12-13 11:03:16 -0800810 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
811 if (AHardwareBuffer_isSupported(&desc)) {
812 all_formats.emplace_back(VkSurfaceFormatKHR{
813 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800814 if (colorspace_ext) {
815 all_formats.emplace_back(
816 VkSurfaceFormatKHR{VK_FORMAT_R5G6B5_UNORM_PACK16,
817 VK_COLOR_SPACE_PASS_THROUGH_EXT});
818 }
Charlie Lao324191f2019-12-13 11:03:16 -0800819 }
820
821 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
822 if (AHardwareBuffer_isSupported(&desc)) {
823 all_formats.emplace_back(VkSurfaceFormatKHR{
824 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800825 if (colorspace_ext) {
826 all_formats.emplace_back(
827 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
828 VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800829 all_formats.emplace_back(
830 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
831 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
832 all_formats.emplace_back(
833 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
834 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
835 }
836 }
837
838 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
839 if (AHardwareBuffer_isSupported(&desc)) {
840 all_formats.emplace_back(
841 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
842 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800843 if (colorspace_ext) {
844 all_formats.emplace_back(
845 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
846 VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800847 all_formats.emplace_back(
848 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
849 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
850 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700851 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700852
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500853 desc.format = AHARDWAREBUFFER_FORMAT_R8_UNORM;
854 if (AHardwareBuffer_isSupported(&desc)) {
Jason Macnakca506332022-11-09 11:00:36 -0800855 if (colorspace_ext) {
856 all_formats.emplace_back(VkSurfaceFormatKHR{
857 VK_FORMAT_R8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT});
858 }
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500859 }
860
Trevor David Black67e9b102023-03-07 05:28:15 +0000861 bool rgba10x6_formats_ext = false;
862 uint32_t exts_count;
863 const auto& driver = GetData(pdev).driver;
864 driver.EnumerateDeviceExtensionProperties(pdev, nullptr, &exts_count,
865 nullptr);
866 std::vector<VkExtensionProperties> props(exts_count);
867 driver.EnumerateDeviceExtensionProperties(pdev, nullptr, &exts_count,
868 props.data());
869 for (uint32_t i = 0; i < exts_count; i++) {
870 VkExtensionProperties prop = props[i];
871 if (strcmp(prop.extensionName,
872 VK_EXT_RGBA10X6_FORMATS_EXTENSION_NAME) == 0) {
873 rgba10x6_formats_ext = true;
874 }
875 }
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000876 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM;
Trevor David Black67e9b102023-03-07 05:28:15 +0000877 if (AHardwareBuffer_isSupported(&desc) && rgba10x6_formats_ext) {
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000878 all_formats.emplace_back(
879 VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
880 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
881 if (colorspace_ext) {
882 all_formats.emplace_back(
883 VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
884 VK_COLOR_SPACE_PASS_THROUGH_EXT});
885 all_formats.emplace_back(
886 VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
887 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
888 }
889 }
890
Ian Elliott6ba85d92022-02-18 16:44:58 -0700891 // NOTE: Any new formats that are added must be coordinated across different
892 // Android users. This includes the ANGLE team (a layered implementation of
893 // OpenGL-ES).
894
Jesse Halld7b994a2015-09-07 14:17:37 -0700895 VkResult result = VK_SUCCESS;
896 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800897 uint32_t transfer_count = all_formats.size();
898 if (transfer_count > *count) {
899 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700900 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700901 }
Charlie Lao324191f2019-12-13 11:03:16 -0800902 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
903 formats);
904 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700905 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800906 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700907 }
Charlie Lao324191f2019-12-13 11:03:16 -0800908
Jesse Halld7b994a2015-09-07 14:17:37 -0700909 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700910}
911
Jesse Halle1b12782015-11-30 11:27:32 -0800912VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300913VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
914 VkPhysicalDevice physicalDevice,
915 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
916 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800917 ATRACE_CALL();
918
Chris Forbes2452cf72017-03-16 16:30:17 +1300919 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
920 physicalDevice, pSurfaceInfo->surface,
921 &pSurfaceCapabilities->surfaceCapabilities);
922
Chris Forbes06bc0092017-03-16 16:46:05 +1300923 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
924 while (caps->pNext) {
925 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
926
927 switch (caps->sType) {
928 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
929 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
930 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
931 caps);
932 // Claim same set of usage flags are supported for
933 // shared present modes as for other modes.
934 shared_caps->sharedPresentSupportedUsageFlags =
935 pSurfaceCapabilities->surfaceCapabilities
936 .supportedUsageFlags;
937 } break;
938
Ian Elliottbb67b242022-03-16 09:52:28 -0600939 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
940 VkSurfaceProtectedCapabilitiesKHR* protected_caps =
941 reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(caps);
942 protected_caps->supportsProtected = VK_TRUE;
943 } break;
944
Chris Forbes06bc0092017-03-16 16:46:05 +1300945 default:
946 // Ignore all other extension structs
947 break;
948 }
949 }
950
Chris Forbes2452cf72017-03-16 16:30:17 +1300951 return result;
952}
953
954VKAPI_ATTR
955VkResult GetPhysicalDeviceSurfaceFormats2KHR(
956 VkPhysicalDevice physicalDevice,
957 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
958 uint32_t* pSurfaceFormatCount,
959 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800960 ATRACE_CALL();
961
Chris Forbes2452cf72017-03-16 16:30:17 +1300962 if (!pSurfaceFormats) {
963 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
964 pSurfaceInfo->surface,
965 pSurfaceFormatCount, nullptr);
Trevor David Black929e9cd2022-11-22 04:12:19 +0000966 }
Chris Forbes2452cf72017-03-16 16:30:17 +1300967
Trevor David Black929e9cd2022-11-22 04:12:19 +0000968 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
969 // after the call.
970 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
971 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
972 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
973 surface_formats.data());
Trevor David Black2cc44682022-03-09 00:31:38 +0000974
Trevor David Black929e9cd2022-11-22 04:12:19 +0000975 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
976 return result;
977 }
Trevor David Black2cc44682022-03-09 00:31:38 +0000978
Trevor David Black929e9cd2022-11-22 04:12:19 +0000979 const auto& driver = GetData(physicalDevice).driver;
980
981 // marshal results individually due to stride difference.
982 uint32_t formats_to_marshal = *pSurfaceFormatCount;
983 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
984 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
985
986 // Query the compression properties for the surface format
987 VkSurfaceFormat2KHR* pSurfaceFormat = &pSurfaceFormats[i];
988 while (pSurfaceFormat->pNext) {
989 pSurfaceFormat =
990 reinterpret_cast<VkSurfaceFormat2KHR*>(pSurfaceFormat->pNext);
991 switch (pSurfaceFormat->sType) {
992 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT: {
Trevor David Black2cc44682022-03-09 00:31:38 +0000993 VkImageCompressionPropertiesEXT* surfaceCompressionProps =
994 reinterpret_cast<VkImageCompressionPropertiesEXT*>(
Trevor David Black929e9cd2022-11-22 04:12:19 +0000995 pSurfaceFormat);
Trevor David Black2cc44682022-03-09 00:31:38 +0000996
997 if (surfaceCompressionProps &&
998 driver.GetPhysicalDeviceImageFormatProperties2KHR) {
999 VkPhysicalDeviceImageFormatInfo2 imageFormatInfo = {};
1000 imageFormatInfo.sType =
1001 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2;
1002 imageFormatInfo.format =
1003 pSurfaceFormats[i].surfaceFormat.format;
1004 imageFormatInfo.pNext = nullptr;
1005
1006 VkImageCompressionControlEXT compressionControl = {};
1007 compressionControl.sType =
1008 VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT;
1009 compressionControl.pNext = imageFormatInfo.pNext;
1010
1011 imageFormatInfo.pNext = &compressionControl;
1012
1013 VkImageCompressionPropertiesEXT compressionProps = {};
1014 compressionProps.sType =
1015 VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT;
1016 compressionProps.pNext = nullptr;
1017
1018 VkImageFormatProperties2KHR imageFormatProps = {};
1019 imageFormatProps.sType =
1020 VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR;
1021 imageFormatProps.pNext = &compressionProps;
1022
1023 VkResult compressionRes =
1024 driver.GetPhysicalDeviceImageFormatProperties2KHR(
1025 physicalDevice, &imageFormatInfo,
1026 &imageFormatProps);
1027 if (compressionRes == VK_SUCCESS) {
1028 surfaceCompressionProps->imageCompressionFlags =
1029 compressionProps.imageCompressionFlags;
1030 surfaceCompressionProps
1031 ->imageCompressionFixedRateFlags =
1032 compressionProps.imageCompressionFixedRateFlags;
1033 } else {
1034 return compressionRes;
1035 }
1036 }
Trevor David Black929e9cd2022-11-22 04:12:19 +00001037 } break;
1038
1039 default:
1040 // Ignore all other extension structs
1041 break;
Chris Forbes2452cf72017-03-16 16:30:17 +13001042 }
1043 }
Chris Forbes2452cf72017-03-16 16:30:17 +13001044 }
Trevor David Black929e9cd2022-11-22 04:12:19 +00001045
1046 return result;
Chris Forbes2452cf72017-03-16 16:30:17 +13001047}
1048
1049VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +13001050VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -08001051 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +08001052 uint32_t* count,
1053 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001054 ATRACE_CALL();
1055
Yiwei Zhange4a559c2018-02-15 11:27:36 -08001056 int err;
1057 int query_value;
Ian Elliotte7f036c2022-03-15 16:49:21 -06001058 std::vector<VkPresentModeKHR> present_modes;
Ian Elliott1ce053f2022-03-16 09:49:53 -06001059 if (surface == VK_NULL_HANDLE) {
1060 const InstanceData& instance_data = GetData(pdev);
1061 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
1062 bool surfaceless_enabled =
1063 instance_data.hook_extensions.test(surfaceless);
1064 if (!surfaceless_enabled) {
1065 return VK_ERROR_SURFACE_LOST_KHR;
1066 }
1067 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
1068 // extension for this function is for
1069 // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and
1070 // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot
1071 // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a
Ian Elliott7e361142022-06-02 10:45:17 -06001072 // surface, and that cannot be relied upon. Therefore, don't return it.
Ian Elliott1ce053f2022-03-16 09:49:53 -06001073 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1074 } else {
1075 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1076
1077 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1078 &query_value);
1079 if (err != android::OK || query_value < 0) {
1080 ALOGE(
1081 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
1082 "value=%d",
1083 strerror(-err), err, query_value);
1084 return VK_ERROR_SURFACE_LOST_KHR;
1085 }
1086 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1087
1088 err =
1089 window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
1090 if (err != android::OK || query_value < 0) {
1091 ALOGE(
1092 "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
1093 strerror(-err), err, query_value);
1094 return VK_ERROR_SURFACE_LOST_KHR;
1095 }
1096 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
1097
1098 if (min_undequeued_buffers + 1 < max_buffer_count)
1099 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
1100 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1101 }
Chris Forbese8d79a62017-02-22 12:49:18 +13001102
1103 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001104 QueryPresentationProperties(pdev, &present_properties);
1105 if (present_properties.sharedImage) {
1106 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
1107 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +13001108 }
1109
1110 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -07001111
1112 VkResult result = VK_SUCCESS;
1113 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +13001114 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -07001115 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +13001116 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -07001117 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -07001118 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +13001119 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -07001120 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001121 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001122}
1123
Jesse Halle1b12782015-11-30 11:27:32 -08001124VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001125VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001126 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001127 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001128 ATRACE_CALL();
1129
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001130 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
1131 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
1132 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
1133 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
1134 pDeviceGroupPresentCapabilities->sType);
1135
1136 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
1137 sizeof(pDeviceGroupPresentCapabilities->presentMask));
1138
1139 // assume device group of size 1
1140 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
1141 pDeviceGroupPresentCapabilities->modes =
1142 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1143
1144 return VK_SUCCESS;
1145}
1146
1147VKAPI_ATTR
1148VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001149 VkDevice,
1150 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001151 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001152 ATRACE_CALL();
1153
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001154 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1155 return VK_SUCCESS;
1156}
1157
1158VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -06001159VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001160 VkSurfaceKHR surface,
1161 uint32_t* pRectCount,
1162 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001163 ATRACE_CALL();
1164
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001165 if (!pRects) {
1166 *pRectCount = 1;
1167 } else {
1168 uint32_t count = std::min(*pRectCount, 1u);
1169 bool incomplete = *pRectCount < 1;
1170
1171 *pRectCount = count;
1172
1173 if (incomplete) {
1174 return VK_INCOMPLETE;
1175 }
1176
1177 int err;
1178 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1179
1180 int width = 0, height = 0;
1181 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001182 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001183 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1184 strerror(-err), err);
1185 }
1186 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001187 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001188 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1189 strerror(-err), err);
1190 }
1191
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001192 pRects[0].offset.x = 0;
1193 pRects[0].offset.y = 0;
1194 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1195 static_cast<uint32_t>(height)};
1196 }
1197 return VK_SUCCESS;
1198}
1199
Yiwei Zhang533cea92019-06-03 18:43:24 -07001200static void DestroySwapchainInternal(VkDevice device,
1201 VkSwapchainKHR swapchain_handle,
1202 const VkAllocationCallbacks* allocator) {
1203 ATRACE_CALL();
1204
1205 const auto& dispatch = GetData(device).driver;
1206 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1207 if (!swapchain) {
1208 return;
1209 }
1210
1211 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1212 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1213
1214 if (window && swapchain->frame_timestamps_enabled) {
1215 native_window_enable_frame_timestamps(window, false);
1216 }
1217
1218 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001219 ReleaseSwapchainImage(device, swapchain->shared, window, -1,
1220 swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001221 }
1222
1223 if (active) {
1224 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1225 }
1226
1227 if (!allocator) {
1228 allocator = &GetData(device).allocator;
1229 }
1230
1231 swapchain->~Swapchain();
1232 allocator->pfnFree(allocator->pUserData, swapchain);
1233}
1234
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001235VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001236VkResult CreateSwapchainKHR(VkDevice device,
1237 const VkSwapchainCreateInfoKHR* create_info,
1238 const VkAllocationCallbacks* allocator,
1239 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001240 ATRACE_CALL();
1241
Jesse Halld7b994a2015-09-07 14:17:37 -07001242 int err;
1243 VkResult result = VK_SUCCESS;
1244
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001245 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1246 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1247 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1248 " oldSwapchain=0x%" PRIx64,
1249 reinterpret_cast<uint64_t>(create_info->surface),
1250 create_info->minImageCount, create_info->imageFormat,
1251 create_info->imageColorSpace, create_info->imageExtent.width,
1252 create_info->imageExtent.height, create_info->imageUsage,
1253 create_info->preTransform, create_info->presentMode,
1254 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1255
Jesse Hall1f91d392015-12-11 16:28:44 -08001256 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001257 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001258
Trevor David Blackf499b5a2023-07-14 17:30:41 +00001259 PixelFormat native_pixel_format =
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001260 GetNativePixelFormat(create_info->imageFormat);
1261 android_dataspace native_dataspace =
1262 GetNativeDataspace(create_info->imageColorSpace);
1263 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1264 ALOGE(
1265 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1266 "failed: Unsupported color space",
1267 create_info->imageColorSpace);
1268 return VK_ERROR_INITIALIZATION_FAILED;
1269 }
1270
Jesse Hall42a9eec2016-06-03 12:39:49 -07001271 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001272 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001273 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001274 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001275 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001276 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001277 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001278 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001279 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1280 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001281 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001282 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001283
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001284 Surface& surface = *SurfaceFromHandle(create_info->surface);
1285
Jesse Halldc225072016-05-30 22:40:14 -07001286 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001287 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001288 " because it already has active swapchain 0x%" PRIx64
1289 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1290 reinterpret_cast<uint64_t>(create_info->surface),
1291 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1292 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1293 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1294 }
1295 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1296 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1297
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001298 // -- Reset the native window --
1299 // The native window might have been used previously, and had its properties
1300 // changed from defaults. That will affect the answer we get for queries
1301 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1302 // attempt such queries.
1303
Jesse Halldc225072016-05-30 22:40:14 -07001304 // The native window only allows dequeueing all buffers before any have
1305 // been queued, since after that point at least one is assumed to be in
1306 // non-FREE state at any given time. Disconnecting and re-connecting
1307 // orphans the previous buffers, getting us back to the state where we can
1308 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001309 //
1310 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001311 ANativeWindow* window = surface.window.get();
1312 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1313 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001314 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001315 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1316 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001317 strerror(-err), err);
1318
Nicolas Capens147b7da2021-04-09 14:53:06 -04001319 err =
1320 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001321 if (err != android::OK) {
1322 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1323 strerror(-err), err);
1324 return VK_ERROR_SURFACE_LOST_KHR;
1325 }
1326
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301327 int swap_interval =
1328 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001329 err = window->setSwapInterval(window, swap_interval);
1330 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001331 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1332 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001333 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001334 }
1335
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001336 err = native_window_set_shared_buffer_mode(window, false);
1337 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001338 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1339 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001340 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001341 }
1342
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001343 err = native_window_set_auto_refresh(window, false);
1344 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001345 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1346 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001347 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001348 }
1349
Jesse Halld7b994a2015-09-07 14:17:37 -07001350 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001351
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001352 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001353
Trevor David Blackf499b5a2023-07-14 17:30:41 +00001354 err = native_window_set_buffers_format(
1355 window, static_cast<int>(native_pixel_format));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001356 if (err != android::OK) {
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001357 ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)",
Trevor David Blackf499b5a2023-07-14 17:30:41 +00001358 toString(native_pixel_format).c_str(), strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001359 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001360 }
Yiwei Zhang51572c22022-07-22 23:08:30 +00001361
1362 /* Respect consumer default dataspace upon HAL_DATASPACE_ARBITRARY. */
1363 if (native_dataspace != HAL_DATASPACE_ARBITRARY) {
1364 err = native_window_set_buffers_data_space(window, native_dataspace);
1365 if (err != android::OK) {
1366 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1367 native_dataspace, strerror(-err), err);
1368 return VK_ERROR_SURFACE_LOST_KHR;
1369 }
Jesse Hall517274a2016-02-10 00:07:18 -08001370 }
1371
Jesse Hall3dd678a2016-01-08 21:52:01 -08001372 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001373 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001374 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001375 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001376 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1377 create_info->imageExtent.width, create_info->imageExtent.height,
1378 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001379 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001380 }
1381
Jesse Hall178b6962016-02-24 15:39:50 -08001382 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1383 // applied during rendering. native_window_set_transform() expects the
1384 // inverse: the transform the app is requesting that the compositor perform
1385 // during composition. With native windows, pre-transform works by rendering
1386 // with the same transform the compositor is applying (as in Vulkan), but
1387 // then requesting the inverse transform, so that when the compositor does
1388 // it's job the two transforms cancel each other out and the compositor ends
1389 // up applying an identity transform to the app's buffer.
1390 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001391 window, InvertTransformToNative(create_info->preTransform));
1392 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001393 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1394 InvertTransformToNative(create_info->preTransform),
1395 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001396 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001397 }
1398
Jesse Hallf64ca122015-11-03 16:11:10 -08001399 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001400 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1401 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001402 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1403 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001404 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001405 }
1406
Chris Forbes97ef4612017-03-30 19:37:50 +13001407 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1408 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1409 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1410 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001411 err = native_window_set_shared_buffer_mode(window, true);
1412 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001413 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1414 return VK_ERROR_SURFACE_LOST_KHR;
1415 }
1416 }
1417
1418 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001419 err = native_window_set_auto_refresh(window, true);
1420 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001421 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1422 return VK_ERROR_SURFACE_LOST_KHR;
1423 }
1424 }
1425
Ian Elliott16c443c2021-11-30 17:10:32 -07001426 int query_value;
1427 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1428 &query_value);
1429 if (err != android::OK || query_value < 0) {
1430 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1431 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001432 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001433 }
Ian Elliott16c443c2021-11-30 17:10:32 -07001434 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1435 const auto mailbox_num_images = std::max(3u, create_info->minImageCount);
1436 const auto requested_images =
1437 swap_interval ? create_info->minImageCount : mailbox_num_images;
1438 uint32_t num_images = requested_images - 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001439
Ian Elliott5396b702021-12-13 19:32:34 -07001440 // Lower layer insists that we have at least min_undequeued_buffers + 1
1441 // buffers. This is wasteful and we'd like to relax it in the shared case,
1442 // but not all the pieces are in place for that to work yet. Note we only
1443 // lie to the lower layer--we don't want to give the app back a swapchain
1444 // with extra images (which they can't actually use!).
1445 uint32_t min_buffer_count = min_undequeued_buffers + 1;
1446 err = native_window_set_buffer_count(
1447 window, std::max(min_buffer_count, num_images));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001448 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001449 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1450 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001451 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001452 }
1453
Ian Elliott12b7e2f2021-12-21 23:24:20 -07001454 // In shared mode the num_images must be one regardless of how many
1455 // buffers were allocated for the buffer queue.
1456 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1457 num_images = 1;
1458 }
1459
Trevor David Black2cc44682022-03-09 00:31:38 +00001460 void* usage_info_pNext = nullptr;
1461 VkImageCompressionControlEXT image_compression = {};
John Reck97678d42022-08-23 11:24:51 -04001462 uint64_t native_usage = 0;
Trevor David Black2cc44682022-03-09 00:31:38 +00001463 if (dispatch.GetSwapchainGrallocUsage3ANDROID) {
1464 ATRACE_BEGIN("GetSwapchainGrallocUsage3ANDROID");
1465 VkGrallocUsageInfoANDROID gralloc_usage_info = {};
1466 gralloc_usage_info.sType = VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID;
1467 gralloc_usage_info.format = create_info->imageFormat;
1468 gralloc_usage_info.imageUsage = create_info->imageUsage;
1469
1470 // Look through the pNext chain for an image compression control struct
1471 // if one is found AND the appropriate extensions are enabled,
1472 // append it to be the gralloc usage pNext chain
1473 const VkSwapchainCreateInfoKHR* create_infos = create_info;
1474 while (create_infos->pNext) {
1475 create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(
1476 create_infos->pNext);
1477 switch (create_infos->sType) {
1478 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: {
1479 const VkImageCompressionControlEXT* compression_infos =
1480 reinterpret_cast<const VkImageCompressionControlEXT*>(
1481 create_infos);
1482 image_compression = *compression_infos;
1483 image_compression.pNext = nullptr;
1484 usage_info_pNext = &image_compression;
1485 } break;
1486
1487 default:
1488 // Ignore all other info structs
1489 break;
1490 }
1491 }
1492 gralloc_usage_info.pNext = usage_info_pNext;
1493
1494 result = dispatch.GetSwapchainGrallocUsage3ANDROID(
1495 device, &gralloc_usage_info, &native_usage);
1496 ATRACE_END();
1497 if (result != VK_SUCCESS) {
1498 ALOGE("vkGetSwapchainGrallocUsage3ANDROID failed: %d", result);
1499 return VK_ERROR_SURFACE_LOST_KHR;
1500 }
1501 } else if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001502 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001503 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001504 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1505 device, create_info->imageFormat, create_info->imageUsage,
1506 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001507 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001508 if (result != VK_SUCCESS) {
1509 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001510 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001511 }
John Reck97678d42022-08-23 11:24:51 -04001512 native_usage =
Trevor David Black2cc44682022-03-09 00:31:38 +00001513 convertGralloc1ToBufferUsage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001514 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001515 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
John Reck97678d42022-08-23 11:24:51 -04001516 int32_t legacy_usage = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -08001517 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001518 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001519 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001520 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001521 if (result != VK_SUCCESS) {
1522 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001523 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001524 }
John Reck97678d42022-08-23 11:24:51 -04001525 native_usage = static_cast<uint64_t>(legacy_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001526 }
John Reck97678d42022-08-23 11:24:51 -04001527 native_usage |= surface.consumer_usage;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001528
1529 bool createProtectedSwapchain = false;
1530 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1531 createProtectedSwapchain = true;
1532 native_usage |= BufferUsage::PROTECTED;
1533 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001534 err = native_window_set_usage(window, native_usage);
1535 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001536 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001537 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001538 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001539
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001540 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001541 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1542 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001543 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1544 strerror(-err), err);
1545 return VK_ERROR_SURFACE_LOST_KHR;
1546 }
1547
Jesse Halld7b994a2015-09-07 14:17:37 -07001548 // -- Allocate our Swapchain object --
1549 // After this point, we must deallocate the swapchain on error.
1550
Jesse Hall1f91d392015-12-11 16:28:44 -08001551 void* mem = allocator->pfnAllocation(allocator->pUserData,
1552 sizeof(Swapchain), alignof(Swapchain),
1553 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001554 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001555 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001556 Swapchain* swapchain = new (mem)
1557 Swapchain(surface, num_images, create_info->presentMode,
1558 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001559 // -- Dequeue all buffers and create a VkImage for each --
1560 // Any failures during or after this must cancel the dequeued buffers.
1561
Chris Forbesb56287a2017-01-12 14:28:58 +13001562 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1563#pragma clang diagnostic push
1564#pragma clang diagnostic ignored "-Wold-style-cast"
1565 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1566#pragma clang diagnostic pop
Trevor David Black2cc44682022-03-09 00:31:38 +00001567 .pNext = usage_info_pNext,
Chris Forbesb56287a2017-01-12 14:28:58 +13001568 .usage = swapchain_image_usage,
1569 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001570 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001571#pragma clang diagnostic push
1572#pragma clang diagnostic ignored "-Wold-style-cast"
1573 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1574#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001575 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001576 };
1577 VkImageCreateInfo image_create = {
1578 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1579 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001580 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001581 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001582 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001583 .extent = {0, 0, 1},
1584 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001585 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001586 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001587 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001588 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001589 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001590 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001591 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1592 };
1593
Jesse Halld7b994a2015-09-07 14:17:37 -07001594 for (uint32_t i = 0; i < num_images; i++) {
1595 Swapchain::Image& img = swapchain->images[i];
1596
1597 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001598 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1599 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001600 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001601 switch (-err) {
1602 case ENOMEM:
1603 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1604 break;
1605 default:
1606 result = VK_ERROR_SURFACE_LOST_KHR;
1607 break;
1608 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001609 break;
1610 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001611 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001612 img.dequeued = true;
1613
1614 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001615 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1616 static_cast<uint32_t>(img.buffer->height),
1617 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001618 image_native_buffer.handle = img.buffer->handle;
1619 image_native_buffer.stride = img.buffer->stride;
1620 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001621 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001622 android_convertGralloc0To1Usage(int(img.buffer->usage),
1623 &image_native_buffer.usage2.producer,
1624 &image_native_buffer.usage2.consumer);
Trevor David Black2cc44682022-03-09 00:31:38 +00001625 image_native_buffer.usage3 = img.buffer->usage;
Jesse Halld7b994a2015-09-07 14:17:37 -07001626
Yiwei Zhang533cea92019-06-03 18:43:24 -07001627 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001628 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001629 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001630 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001631 if (result != VK_SUCCESS) {
1632 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1633 break;
1634 }
1635 }
1636
1637 // -- Cancel all buffers, returning them to the queue --
1638 // If an error occurred before, also destroy the VkImage and release the
1639 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001640 for (uint32_t i = 0; i < num_images; i++) {
1641 Swapchain::Image& img = swapchain->images[i];
1642 if (img.dequeued) {
1643 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001644 window->cancelBuffer(window, img.buffer.get(),
1645 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001646 img.dequeue_fence = -1;
1647 img.dequeued = false;
1648 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001649 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001650 }
1651
1652 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001653 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1654 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001655 return result;
1656 }
1657
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001658 if (transform_hint != swapchain->pre_transform) {
1659 // Log that the app is not doing pre-rotation.
1660 android::GraphicsEnv::getInstance().setTargetStats(
1661 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1662 }
1663
Jesse Halldc225072016-05-30 22:40:14 -07001664 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1665 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001666 return VK_SUCCESS;
1667}
1668
Jesse Halle1b12782015-11-30 11:27:32 -08001669VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001670void DestroySwapchainKHR(VkDevice device,
1671 VkSwapchainKHR swapchain_handle,
1672 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001673 ATRACE_CALL();
1674
Yiwei Zhang533cea92019-06-03 18:43:24 -07001675 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001676}
1677
Jesse Halle1b12782015-11-30 11:27:32 -08001678VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001679VkResult GetSwapchainImagesKHR(VkDevice,
1680 VkSwapchainKHR swapchain_handle,
1681 uint32_t* count,
1682 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001683 ATRACE_CALL();
1684
Jesse Halld7b994a2015-09-07 14:17:37 -07001685 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001686 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1687 "getting images for non-active swapchain 0x%" PRIx64
1688 "; only dequeued image handles are valid",
1689 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001690 VkResult result = VK_SUCCESS;
1691 if (images) {
1692 uint32_t n = swapchain.num_images;
1693 if (*count < swapchain.num_images) {
1694 n = *count;
1695 result = VK_INCOMPLETE;
1696 }
1697 for (uint32_t i = 0; i < n; i++)
1698 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001699 *count = n;
1700 } else {
1701 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001702 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001703 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001704}
1705
Jesse Halle1b12782015-11-30 11:27:32 -08001706VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001707VkResult AcquireNextImageKHR(VkDevice device,
1708 VkSwapchainKHR swapchain_handle,
1709 uint64_t timeout,
1710 VkSemaphore semaphore,
1711 VkFence vk_fence,
1712 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001713 ATRACE_CALL();
1714
Jesse Halld7b994a2015-09-07 14:17:37 -07001715 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001716 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001717 VkResult result;
1718 int err;
1719
Jesse Halldc225072016-05-30 22:40:14 -07001720 if (swapchain.surface.swapchain_handle != swapchain_handle)
1721 return VK_ERROR_OUT_OF_DATE_KHR;
1722
Chris Forbesc88409c2017-03-30 19:47:37 +13001723 if (swapchain.shared) {
1724 // In shared mode, we keep the buffer dequeued all the time, so we don't
1725 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1726 // the semaphore and fence passed to us will be signalled.
1727 *image_index = 0;
1728 result = GetData(device).driver.AcquireImageANDROID(
1729 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1730 return result;
1731 }
1732
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001733 const nsecs_t acquire_next_image_timeout =
1734 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1735 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1736 // Cache the timeout to avoid the duplicate binder cost.
1737 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1738 acquire_next_image_timeout);
1739 if (err != android::OK) {
1740 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1741 strerror(-err), err);
1742 return VK_ERROR_SURFACE_LOST_KHR;
1743 }
1744 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1745 }
1746
Jesse Halld7b994a2015-09-07 14:17:37 -07001747 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001748 int fence_fd;
1749 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001750 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001751 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1752 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1753 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001754 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001755 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001756 }
1757
1758 uint32_t idx;
1759 for (idx = 0; idx < swapchain.num_images; idx++) {
1760 if (swapchain.images[idx].buffer.get() == buffer) {
1761 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001762 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001763 break;
1764 }
1765 }
1766 if (idx == swapchain.num_images) {
1767 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001768 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001769 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001770 }
1771
1772 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001773 if (fence_fd != -1) {
1774 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001775 if (fence_clone == -1) {
1776 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1777 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001778 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001779 }
1780 }
1781
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001782 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001783 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001784 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001785 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1786 // even if the call fails. We could close it ourselves on failure, but
1787 // that would create a race condition if the driver closes it on a
1788 // failure path: some other thread might create an fd with the same
1789 // number between the time the driver closes it and the time we close
1790 // it. We must assume one of: the driver *always* closes it even on
1791 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001792 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001793 swapchain.images[idx].dequeued = false;
1794 swapchain.images[idx].dequeue_fence = -1;
1795 return result;
1796 }
1797
1798 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001799 return VK_SUCCESS;
1800}
1801
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001802VKAPI_ATTR
1803VkResult AcquireNextImage2KHR(VkDevice device,
1804 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1805 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001806 ATRACE_CALL();
1807
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001808 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1809 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1810 pAcquireInfo->fence, pImageIndex);
1811}
1812
Jesse Halldc225072016-05-30 22:40:14 -07001813static VkResult WorstPresentResult(VkResult a, VkResult b) {
1814 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1815 // (in spec version 1.0.14).
1816 static const VkResult kWorstToBest[] = {
1817 VK_ERROR_DEVICE_LOST,
1818 VK_ERROR_SURFACE_LOST_KHR,
1819 VK_ERROR_OUT_OF_DATE_KHR,
1820 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1821 VK_ERROR_OUT_OF_HOST_MEMORY,
1822 VK_SUBOPTIMAL_KHR,
1823 };
1824 for (auto result : kWorstToBest) {
1825 if (a == result || b == result)
1826 return result;
1827 }
1828 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1829 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1830 return a != VK_SUCCESS ? a : b;
1831}
1832
Jesse Halle1b12782015-11-30 11:27:32 -08001833VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001834VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001835 ATRACE_CALL();
1836
Jesse Halld7b994a2015-09-07 14:17:37 -07001837 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1838 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1839 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001840
Jesse Halldc225072016-05-30 22:40:14 -07001841 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001842 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001843 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001844
Ian Elliottcb351132016-12-13 10:30:40 -07001845 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001846 const VkPresentRegionsKHR* present_regions = nullptr;
1847 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001848 const VkPresentRegionsKHR* next =
1849 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1850 while (next) {
1851 switch (next->sType) {
1852 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1853 present_regions = next;
1854 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001855 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001856 present_times =
1857 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1858 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001859 default:
1860 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1861 next->sType);
1862 break;
1863 }
1864 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1865 }
1866 ALOGV_IF(
1867 present_regions &&
1868 present_regions->swapchainCount != present_info->swapchainCount,
1869 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001870 ALOGV_IF(present_times &&
1871 present_times->swapchainCount != present_info->swapchainCount,
1872 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1873 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001874 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001875 (present_regions) ? present_regions->pRegions : nullptr;
1876 const VkPresentTimeGOOGLE* times =
1877 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001878 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001879 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001880 uint32_t nrects = 0;
1881
Jesse Halld7b994a2015-09-07 14:17:37 -07001882 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1883 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001884 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001885 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001886 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001887 const VkPresentRegionKHR* region =
1888 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001889 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001890 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001891 VkResult result;
1892 int err;
1893
Jesse Halld7b994a2015-09-07 14:17:37 -07001894 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001895 result = dispatch.QueueSignalReleaseImageANDROID(
1896 queue, present_info->waitSemaphoreCount,
1897 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001898 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001899 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001900 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001901 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001902 if (img.release_fence >= 0)
1903 close(img.release_fence);
1904 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001905
Jesse Halldc225072016-05-30 22:40:14 -07001906 if (swapchain.surface.swapchain_handle ==
1907 present_info->pSwapchains[sc]) {
1908 ANativeWindow* window = swapchain.surface.window.get();
1909 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001910 if (region) {
1911 // Process the incremental-present hint for this swapchain:
1912 uint32_t rcount = region->rectangleCount;
1913 if (rcount > nrects) {
1914 android_native_rect_t* new_rects =
1915 static_cast<android_native_rect_t*>(
1916 allocator->pfnReallocation(
1917 allocator->pUserData, rects,
1918 sizeof(android_native_rect_t) * rcount,
1919 alignof(android_native_rect_t),
1920 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1921 if (new_rects) {
1922 rects = new_rects;
1923 nrects = rcount;
1924 } else {
1925 rcount = 0; // Ignore the hint for this swapchain
1926 }
1927 }
1928 for (uint32_t r = 0; r < rcount; ++r) {
1929 if (region->pRectangles[r].layer > 0) {
1930 ALOGV(
1931 "vkQueuePresentKHR ignoring invalid layer "
1932 "(%u); using layer 0 instead",
1933 region->pRectangles[r].layer);
1934 }
1935 int x = region->pRectangles[r].offset.x;
1936 int y = region->pRectangles[r].offset.y;
1937 int width = static_cast<int>(
1938 region->pRectangles[r].extent.width);
1939 int height = static_cast<int>(
1940 region->pRectangles[r].extent.height);
1941 android_native_rect_t* cur_rect = &rects[r];
1942 cur_rect->left = x;
1943 cur_rect->top = y + height;
1944 cur_rect->right = x + width;
1945 cur_rect->bottom = y;
1946 }
1947 native_window_set_surface_damage(window, rects, rcount);
1948 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001949 if (time) {
1950 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001951 ALOGV(
1952 "Calling "
1953 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001954 native_window_enable_frame_timestamps(window, true);
1955 swapchain.frame_timestamps_enabled = true;
1956 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001957
1958 // Record the nativeFrameId so it can be later correlated to
1959 // this present.
1960 uint64_t nativeFrameId = 0;
1961 err = native_window_get_next_frame_id(
1962 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001963 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001964 ALOGE("Failed to get next native frame ID.");
1965 }
1966
1967 // Add a new timing record with the user's presentID and
1968 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001969 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001970 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001971 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001972 }
1973 if (time->desiredPresentTime) {
1974 // Set the desiredPresentTime:
1975 ALOGV(
1976 "Calling "
1977 "native_window_set_buffers_timestamp(%" PRId64 ")",
1978 time->desiredPresentTime);
1979 native_window_set_buffers_timestamp(
1980 window,
1981 static_cast<int64_t>(time->desiredPresentTime));
1982 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001983 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001984
Jesse Halldc225072016-05-30 22:40:14 -07001985 err = window->queueBuffer(window, img.buffer.get(), fence);
1986 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001987 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001988 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1989 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001990 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001991 } else {
1992 if (img.dequeue_fence >= 0) {
1993 close(img.dequeue_fence);
1994 img.dequeue_fence = -1;
1995 }
1996 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001997 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001998
1999 // If the swapchain is in shared mode, immediately dequeue the
2000 // buffer so it can be presented again without an intervening
2001 // call to AcquireNextImageKHR. We expect to get the same buffer
2002 // back from every call to dequeueBuffer in this mode.
2003 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
2004 ANativeWindowBuffer* buffer;
2005 int fence_fd;
2006 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002007 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13002008 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
2009 swapchain_result = WorstPresentResult(swapchain_result,
2010 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002011 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13002012 ALOGE("got wrong image back for shared swapchain");
2013 swapchain_result = WorstPresentResult(swapchain_result,
2014 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002015 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13002016 img.dequeue_fence = fence_fd;
2017 img.dequeued = true;
2018 }
2019 }
Jesse Halldc225072016-05-30 22:40:14 -07002020 }
2021 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07002022 OrphanSwapchain(device, &swapchain);
2023 }
silence_dogood73597592019-05-23 16:57:37 -07002024 int window_transform_hint;
2025 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
2026 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002027 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07002028 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
2029 strerror(-err), err);
2030 swapchain_result = WorstPresentResult(
2031 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
2032 }
2033 if (swapchain.pre_transform != window_transform_hint) {
2034 swapchain_result =
2035 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
2036 }
Jesse Halldc225072016-05-30 22:40:14 -07002037 } else {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00002038 ReleaseSwapchainImage(device, swapchain.shared, nullptr, fence,
2039 img, true);
Jesse Halldc225072016-05-30 22:40:14 -07002040 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07002041 }
2042
Jesse Halla9e57032015-11-30 01:03:10 -08002043 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07002044 present_info->pResults[sc] = swapchain_result;
2045
2046 if (swapchain_result != final_result)
2047 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07002048 }
Ian Elliottcb351132016-12-13 10:30:40 -07002049 if (rects) {
2050 allocator->pfnFree(allocator->pUserData, rects);
2051 }
Jesse Halld7b994a2015-09-07 14:17:37 -07002052
2053 return final_result;
2054}
Jesse Hallb1352bc2015-09-04 16:12:33 -07002055
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002056VKAPI_ATTR
2057VkResult GetRefreshCycleDurationGOOGLE(
2058 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07002059 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002060 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002061 ATRACE_CALL();
2062
Ian Elliott62c48c92017-01-20 13:13:20 -07002063 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002064 VkResult result = VK_SUCCESS;
2065
Ian Elliott3568bfe2019-05-03 15:54:46 -06002066 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002067
2068 return result;
2069}
2070
2071VKAPI_ATTR
2072VkResult GetPastPresentationTimingGOOGLE(
2073 VkDevice,
2074 VkSwapchainKHR swapchain_handle,
2075 uint32_t* count,
2076 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002077 ATRACE_CALL();
2078
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002079 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002080 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2081 return VK_ERROR_OUT_OF_DATE_KHR;
2082 }
2083
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002084 ANativeWindow* window = swapchain.surface.window.get();
2085 VkResult result = VK_SUCCESS;
2086
2087 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07002088 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002089 native_window_enable_frame_timestamps(window, true);
2090 swapchain.frame_timestamps_enabled = true;
2091 }
2092
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002093 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07002094 // Get the latest ready timing count before copying, since the copied
2095 // timing info will be erased in copy_ready_timings function.
2096 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07002097 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002098 // Check the *count here against the recorded ready timing count, since
2099 // *count can be overwritten per spec describes.
2100 if (*count < n) {
2101 result = VK_INCOMPLETE;
2102 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002103 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07002104 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002105 }
2106
2107 return result;
2108}
2109
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002110VKAPI_ATTR
2111VkResult GetSwapchainStatusKHR(
2112 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13002113 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002114 ATRACE_CALL();
2115
Chris Forbes4e18ba82017-01-20 12:50:17 +13002116 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002117 VkResult result = VK_SUCCESS;
2118
Chris Forbes4e18ba82017-01-20 12:50:17 +13002119 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2120 return VK_ERROR_OUT_OF_DATE_KHR;
2121 }
2122
Yiwei Zhanga885c062019-10-24 12:07:57 -07002123 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002124
2125 return result;
2126}
2127
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002128VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002129 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002130 uint32_t swapchainCount,
2131 const VkSwapchainKHR* pSwapchains,
2132 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002133 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002134
2135 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
2136 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
2137 if (!swapchain)
2138 continue;
2139
2140 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
2141
2142 ANativeWindow* window = swapchain->surface.window.get();
2143
2144 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
2145 const android_smpte2086_metadata smpteMetdata = {
2146 {vulkanMetadata.displayPrimaryRed.x,
2147 vulkanMetadata.displayPrimaryRed.y},
2148 {vulkanMetadata.displayPrimaryGreen.x,
2149 vulkanMetadata.displayPrimaryGreen.y},
2150 {vulkanMetadata.displayPrimaryBlue.x,
2151 vulkanMetadata.displayPrimaryBlue.y},
2152 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
2153 vulkanMetadata.maxLuminance,
2154 vulkanMetadata.minLuminance};
2155 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
2156
2157 const android_cta861_3_metadata cta8613Metadata = {
2158 vulkanMetadata.maxContentLightLevel,
2159 vulkanMetadata.maxFrameAverageLightLevel};
2160 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
2161 }
2162
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002163 return;
2164}
2165
Yiwei Zhang0f475222019-04-11 19:38:00 -07002166static void InterceptBindImageMemory2(
2167 uint32_t bind_info_count,
2168 const VkBindImageMemoryInfo* bind_infos,
2169 std::vector<VkNativeBufferANDROID>* out_native_buffers,
2170 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
2171 out_native_buffers->clear();
2172 out_bind_infos->clear();
2173
2174 if (!bind_info_count)
2175 return;
2176
2177 std::unordered_set<uint32_t> intercepted_indexes;
2178
2179 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2180 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2181 bind_infos[idx].pNext);
2182 while (info &&
2183 info->sType !=
2184 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
2185 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2186 info->pNext);
2187 }
2188
2189 if (!info)
2190 continue;
2191
2192 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
2193 "swapchain handle must not be NULL");
2194 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
2195 ALOG_ASSERT(
2196 info->imageIndex < swapchain->num_images,
2197 "imageIndex must be less than the number of images in swapchain");
2198
2199 ANativeWindowBuffer* buffer =
2200 swapchain->images[info->imageIndex].buffer.get();
2201 VkNativeBufferANDROID native_buffer = {
2202#pragma clang diagnostic push
2203#pragma clang diagnostic ignored "-Wold-style-cast"
2204 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2205#pragma clang diagnostic pop
2206 .pNext = bind_infos[idx].pNext,
2207 .handle = buffer->handle,
2208 .stride = buffer->stride,
2209 .format = buffer->format,
2210 .usage = int(buffer->usage),
2211 };
2212 // Reserve enough space to avoid letting re-allocation invalidate the
2213 // addresses of the elements inside.
2214 out_native_buffers->reserve(bind_info_count);
2215 out_native_buffers->emplace_back(native_buffer);
2216
2217 // Reserve the space now since we know how much is needed now.
2218 out_bind_infos->reserve(bind_info_count);
2219 out_bind_infos->emplace_back(bind_infos[idx]);
2220 out_bind_infos->back().pNext = &out_native_buffers->back();
2221
2222 intercepted_indexes.insert(idx);
2223 }
2224
2225 if (intercepted_indexes.empty())
2226 return;
2227
2228 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2229 if (intercepted_indexes.count(idx))
2230 continue;
2231 out_bind_infos->emplace_back(bind_infos[idx]);
2232 }
2233}
2234
Yiwei Zhang23143102019-04-10 18:24:05 -07002235VKAPI_ATTR
2236VkResult BindImageMemory2(VkDevice device,
2237 uint32_t bindInfoCount,
2238 const VkBindImageMemoryInfo* pBindInfos) {
2239 ATRACE_CALL();
2240
Yiwei Zhang0f475222019-04-11 19:38:00 -07002241 // out_native_buffers is for maintaining the lifecycle of the constructed
2242 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2243 std::vector<VkNativeBufferANDROID> out_native_buffers;
2244 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2245 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2246 &out_bind_infos);
2247 return GetData(device).driver.BindImageMemory2(
2248 device, bindInfoCount,
2249 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002250}
2251
2252VKAPI_ATTR
2253VkResult BindImageMemory2KHR(VkDevice device,
2254 uint32_t bindInfoCount,
2255 const VkBindImageMemoryInfo* pBindInfos) {
2256 ATRACE_CALL();
2257
Yiwei Zhang0f475222019-04-11 19:38:00 -07002258 std::vector<VkNativeBufferANDROID> out_native_buffers;
2259 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2260 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2261 &out_bind_infos);
2262 return GetData(device).driver.BindImageMemory2KHR(
2263 device, bindInfoCount,
2264 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002265}
2266
Chia-I Wu62262232016-03-26 07:06:44 +08002267} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002268} // namespace vulkan