blob: c46036a5e275d19f38067ac851130cb47400e360 [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
Yiwei Zhang0f475222019-04-11 19:38:00 -070019#include <android/hardware/graphics/common/1.0/types.h>
Jesse Hall79927812017-03-23 11:03:23 -070020#include <grallocusage/GrallocUsageConversion.h>
Yiwei Zhang69395cd2019-07-03 16:55:39 -070021#include <graphicsenv/GraphicsEnv.h>
John Reck97678d42022-08-23 11:24:51 -040022#include <hardware/gralloc.h>
23#include <hardware/gralloc1.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070024#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070025#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070026#include <system/window.h>
27#include <ui/BufferQueueDefs.h>
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -050028#include <ui/DebugUtils.h>
29#include <ui/PixelFormat.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080030#include <utils/StrongPointer.h>
Yiwei Zhang705c2e62019-12-18 23:12:43 -080031#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080032#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070033
34#include <algorithm>
35#include <unordered_set>
36#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070037
Chia-I Wu4a6a9162016-03-26 07:17:34 +080038#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070039
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
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500492android::PixelFormat GetNativePixelFormat(VkFormat format) {
493 android::PixelFormat native_format = android::PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700494 switch (format) {
495 case VK_FORMAT_R8G8B8A8_UNORM:
496 case VK_FORMAT_R8G8B8A8_SRGB:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500497 native_format = android::PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700498 break;
499 case VK_FORMAT_R5G6B5_UNORM_PACK16:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500500 native_format = android::PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700501 break;
502 case VK_FORMAT_R16G16B16A16_SFLOAT:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500503 native_format = android::PIXEL_FORMAT_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:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500506 native_format = android::PIXEL_FORMAT_RGBA_1010102;
507 break;
508 case VK_FORMAT_R8_UNORM:
509 native_format = android::PIXEL_FORMAT_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:
512 native_format = android::PIXEL_FORMAT_RGBA_10101010;
513 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
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001259 android::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
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001354 err = native_window_set_buffers_format(window, native_pixel_format);
1355 if (err != android::OK) {
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001356 ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)",
1357 decodePixelFormat(native_pixel_format).c_str(), strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001358 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001359 }
Yiwei Zhang51572c22022-07-22 23:08:30 +00001360
1361 /* Respect consumer default dataspace upon HAL_DATASPACE_ARBITRARY. */
1362 if (native_dataspace != HAL_DATASPACE_ARBITRARY) {
1363 err = native_window_set_buffers_data_space(window, native_dataspace);
1364 if (err != android::OK) {
1365 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1366 native_dataspace, strerror(-err), err);
1367 return VK_ERROR_SURFACE_LOST_KHR;
1368 }
Jesse Hall517274a2016-02-10 00:07:18 -08001369 }
1370
Jesse Hall3dd678a2016-01-08 21:52:01 -08001371 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001372 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001373 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001374 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001375 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1376 create_info->imageExtent.width, create_info->imageExtent.height,
1377 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001378 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001379 }
1380
Jesse Hall178b6962016-02-24 15:39:50 -08001381 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1382 // applied during rendering. native_window_set_transform() expects the
1383 // inverse: the transform the app is requesting that the compositor perform
1384 // during composition. With native windows, pre-transform works by rendering
1385 // with the same transform the compositor is applying (as in Vulkan), but
1386 // then requesting the inverse transform, so that when the compositor does
1387 // it's job the two transforms cancel each other out and the compositor ends
1388 // up applying an identity transform to the app's buffer.
1389 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001390 window, InvertTransformToNative(create_info->preTransform));
1391 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001392 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1393 InvertTransformToNative(create_info->preTransform),
1394 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001395 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001396 }
1397
Jesse Hallf64ca122015-11-03 16:11:10 -08001398 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001399 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1400 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001401 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1402 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001403 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001404 }
1405
Chris Forbes97ef4612017-03-30 19:37:50 +13001406 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1407 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1408 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1409 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001410 err = native_window_set_shared_buffer_mode(window, true);
1411 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001412 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1413 return VK_ERROR_SURFACE_LOST_KHR;
1414 }
1415 }
1416
1417 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001418 err = native_window_set_auto_refresh(window, true);
1419 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001420 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1421 return VK_ERROR_SURFACE_LOST_KHR;
1422 }
1423 }
1424
Ian Elliott16c443c2021-11-30 17:10:32 -07001425 int query_value;
1426 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1427 &query_value);
1428 if (err != android::OK || query_value < 0) {
1429 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1430 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001431 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001432 }
Ian Elliott16c443c2021-11-30 17:10:32 -07001433 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1434 const auto mailbox_num_images = std::max(3u, create_info->minImageCount);
1435 const auto requested_images =
1436 swap_interval ? create_info->minImageCount : mailbox_num_images;
1437 uint32_t num_images = requested_images - 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001438
Ian Elliott5396b702021-12-13 19:32:34 -07001439 // Lower layer insists that we have at least min_undequeued_buffers + 1
1440 // buffers. This is wasteful and we'd like to relax it in the shared case,
1441 // but not all the pieces are in place for that to work yet. Note we only
1442 // lie to the lower layer--we don't want to give the app back a swapchain
1443 // with extra images (which they can't actually use!).
1444 uint32_t min_buffer_count = min_undequeued_buffers + 1;
1445 err = native_window_set_buffer_count(
1446 window, std::max(min_buffer_count, num_images));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001447 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001448 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1449 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001450 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001451 }
1452
Ian Elliott12b7e2f2021-12-21 23:24:20 -07001453 // In shared mode the num_images must be one regardless of how many
1454 // buffers were allocated for the buffer queue.
1455 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1456 num_images = 1;
1457 }
1458
Trevor David Black2cc44682022-03-09 00:31:38 +00001459 void* usage_info_pNext = nullptr;
1460 VkImageCompressionControlEXT image_compression = {};
John Reck97678d42022-08-23 11:24:51 -04001461 uint64_t native_usage = 0;
Trevor David Black2cc44682022-03-09 00:31:38 +00001462 if (dispatch.GetSwapchainGrallocUsage3ANDROID) {
1463 ATRACE_BEGIN("GetSwapchainGrallocUsage3ANDROID");
1464 VkGrallocUsageInfoANDROID gralloc_usage_info = {};
1465 gralloc_usage_info.sType = VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID;
1466 gralloc_usage_info.format = create_info->imageFormat;
1467 gralloc_usage_info.imageUsage = create_info->imageUsage;
1468
1469 // Look through the pNext chain for an image compression control struct
1470 // if one is found AND the appropriate extensions are enabled,
1471 // append it to be the gralloc usage pNext chain
1472 const VkSwapchainCreateInfoKHR* create_infos = create_info;
1473 while (create_infos->pNext) {
1474 create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(
1475 create_infos->pNext);
1476 switch (create_infos->sType) {
1477 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: {
1478 const VkImageCompressionControlEXT* compression_infos =
1479 reinterpret_cast<const VkImageCompressionControlEXT*>(
1480 create_infos);
1481 image_compression = *compression_infos;
1482 image_compression.pNext = nullptr;
1483 usage_info_pNext = &image_compression;
1484 } break;
1485
1486 default:
1487 // Ignore all other info structs
1488 break;
1489 }
1490 }
1491 gralloc_usage_info.pNext = usage_info_pNext;
1492
1493 result = dispatch.GetSwapchainGrallocUsage3ANDROID(
1494 device, &gralloc_usage_info, &native_usage);
1495 ATRACE_END();
1496 if (result != VK_SUCCESS) {
1497 ALOGE("vkGetSwapchainGrallocUsage3ANDROID failed: %d", result);
1498 return VK_ERROR_SURFACE_LOST_KHR;
1499 }
1500 } else if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001501 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001502 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001503 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1504 device, create_info->imageFormat, create_info->imageUsage,
1505 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001506 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001507 if (result != VK_SUCCESS) {
1508 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001509 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001510 }
John Reck97678d42022-08-23 11:24:51 -04001511 native_usage =
Trevor David Black2cc44682022-03-09 00:31:38 +00001512 convertGralloc1ToBufferUsage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001513 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001514 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
John Reck97678d42022-08-23 11:24:51 -04001515 int32_t legacy_usage = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -08001516 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001517 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001518 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001519 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001520 if (result != VK_SUCCESS) {
1521 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001522 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001523 }
John Reck97678d42022-08-23 11:24:51 -04001524 native_usage = static_cast<uint64_t>(legacy_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001525 }
John Reck97678d42022-08-23 11:24:51 -04001526 native_usage |= surface.consumer_usage;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001527
1528 bool createProtectedSwapchain = false;
1529 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1530 createProtectedSwapchain = true;
1531 native_usage |= BufferUsage::PROTECTED;
1532 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001533 err = native_window_set_usage(window, native_usage);
1534 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001535 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001536 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001537 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001538
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001539 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001540 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1541 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001542 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1543 strerror(-err), err);
1544 return VK_ERROR_SURFACE_LOST_KHR;
1545 }
1546
Jesse Halld7b994a2015-09-07 14:17:37 -07001547 // -- Allocate our Swapchain object --
1548 // After this point, we must deallocate the swapchain on error.
1549
Jesse Hall1f91d392015-12-11 16:28:44 -08001550 void* mem = allocator->pfnAllocation(allocator->pUserData,
1551 sizeof(Swapchain), alignof(Swapchain),
1552 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001553 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001554 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001555 Swapchain* swapchain = new (mem)
1556 Swapchain(surface, num_images, create_info->presentMode,
1557 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001558 // -- Dequeue all buffers and create a VkImage for each --
1559 // Any failures during or after this must cancel the dequeued buffers.
1560
Chris Forbesb56287a2017-01-12 14:28:58 +13001561 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1562#pragma clang diagnostic push
1563#pragma clang diagnostic ignored "-Wold-style-cast"
1564 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1565#pragma clang diagnostic pop
Trevor David Black2cc44682022-03-09 00:31:38 +00001566 .pNext = usage_info_pNext,
Chris Forbesb56287a2017-01-12 14:28:58 +13001567 .usage = swapchain_image_usage,
1568 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001569 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001570#pragma clang diagnostic push
1571#pragma clang diagnostic ignored "-Wold-style-cast"
1572 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1573#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001574 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001575 };
1576 VkImageCreateInfo image_create = {
1577 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1578 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001579 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001580 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001581 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001582 .extent = {0, 0, 1},
1583 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001584 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001585 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001586 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001587 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001588 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001589 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001590 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1591 };
1592
Jesse Halld7b994a2015-09-07 14:17:37 -07001593 for (uint32_t i = 0; i < num_images; i++) {
1594 Swapchain::Image& img = swapchain->images[i];
1595
1596 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001597 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1598 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001599 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001600 switch (-err) {
1601 case ENOMEM:
1602 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1603 break;
1604 default:
1605 result = VK_ERROR_SURFACE_LOST_KHR;
1606 break;
1607 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001608 break;
1609 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001610 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001611 img.dequeued = true;
1612
1613 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001614 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1615 static_cast<uint32_t>(img.buffer->height),
1616 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001617 image_native_buffer.handle = img.buffer->handle;
1618 image_native_buffer.stride = img.buffer->stride;
1619 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001620 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001621 android_convertGralloc0To1Usage(int(img.buffer->usage),
1622 &image_native_buffer.usage2.producer,
1623 &image_native_buffer.usage2.consumer);
Trevor David Black2cc44682022-03-09 00:31:38 +00001624 image_native_buffer.usage3 = img.buffer->usage;
Jesse Halld7b994a2015-09-07 14:17:37 -07001625
Yiwei Zhang533cea92019-06-03 18:43:24 -07001626 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001627 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001628 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001629 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001630 if (result != VK_SUCCESS) {
1631 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1632 break;
1633 }
1634 }
1635
1636 // -- Cancel all buffers, returning them to the queue --
1637 // If an error occurred before, also destroy the VkImage and release the
1638 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001639 for (uint32_t i = 0; i < num_images; i++) {
1640 Swapchain::Image& img = swapchain->images[i];
1641 if (img.dequeued) {
1642 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001643 window->cancelBuffer(window, img.buffer.get(),
1644 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001645 img.dequeue_fence = -1;
1646 img.dequeued = false;
1647 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001648 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001649 }
1650
1651 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001652 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1653 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001654 return result;
1655 }
1656
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001657 if (transform_hint != swapchain->pre_transform) {
1658 // Log that the app is not doing pre-rotation.
1659 android::GraphicsEnv::getInstance().setTargetStats(
1660 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1661 }
1662
Jesse Halldc225072016-05-30 22:40:14 -07001663 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1664 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001665 return VK_SUCCESS;
1666}
1667
Jesse Halle1b12782015-11-30 11:27:32 -08001668VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001669void DestroySwapchainKHR(VkDevice device,
1670 VkSwapchainKHR swapchain_handle,
1671 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001672 ATRACE_CALL();
1673
Yiwei Zhang533cea92019-06-03 18:43:24 -07001674 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001675}
1676
Jesse Halle1b12782015-11-30 11:27:32 -08001677VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001678VkResult GetSwapchainImagesKHR(VkDevice,
1679 VkSwapchainKHR swapchain_handle,
1680 uint32_t* count,
1681 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001682 ATRACE_CALL();
1683
Jesse Halld7b994a2015-09-07 14:17:37 -07001684 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001685 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1686 "getting images for non-active swapchain 0x%" PRIx64
1687 "; only dequeued image handles are valid",
1688 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001689 VkResult result = VK_SUCCESS;
1690 if (images) {
1691 uint32_t n = swapchain.num_images;
1692 if (*count < swapchain.num_images) {
1693 n = *count;
1694 result = VK_INCOMPLETE;
1695 }
1696 for (uint32_t i = 0; i < n; i++)
1697 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001698 *count = n;
1699 } else {
1700 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001701 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001702 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001703}
1704
Jesse Halle1b12782015-11-30 11:27:32 -08001705VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001706VkResult AcquireNextImageKHR(VkDevice device,
1707 VkSwapchainKHR swapchain_handle,
1708 uint64_t timeout,
1709 VkSemaphore semaphore,
1710 VkFence vk_fence,
1711 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001712 ATRACE_CALL();
1713
Jesse Halld7b994a2015-09-07 14:17:37 -07001714 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001715 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001716 VkResult result;
1717 int err;
1718
Jesse Halldc225072016-05-30 22:40:14 -07001719 if (swapchain.surface.swapchain_handle != swapchain_handle)
1720 return VK_ERROR_OUT_OF_DATE_KHR;
1721
Chris Forbesc88409c2017-03-30 19:47:37 +13001722 if (swapchain.shared) {
1723 // In shared mode, we keep the buffer dequeued all the time, so we don't
1724 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1725 // the semaphore and fence passed to us will be signalled.
1726 *image_index = 0;
1727 result = GetData(device).driver.AcquireImageANDROID(
1728 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1729 return result;
1730 }
1731
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001732 const nsecs_t acquire_next_image_timeout =
1733 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1734 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1735 // Cache the timeout to avoid the duplicate binder cost.
1736 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1737 acquire_next_image_timeout);
1738 if (err != android::OK) {
1739 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1740 strerror(-err), err);
1741 return VK_ERROR_SURFACE_LOST_KHR;
1742 }
1743 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1744 }
1745
Jesse Halld7b994a2015-09-07 14:17:37 -07001746 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001747 int fence_fd;
1748 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001749 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001750 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1751 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1752 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001753 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001754 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001755 }
1756
1757 uint32_t idx;
1758 for (idx = 0; idx < swapchain.num_images; idx++) {
1759 if (swapchain.images[idx].buffer.get() == buffer) {
1760 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001761 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001762 break;
1763 }
1764 }
1765 if (idx == swapchain.num_images) {
1766 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001767 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001768 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001769 }
1770
1771 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001772 if (fence_fd != -1) {
1773 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001774 if (fence_clone == -1) {
1775 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1776 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001777 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001778 }
1779 }
1780
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001781 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001782 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001783 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001784 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1785 // even if the call fails. We could close it ourselves on failure, but
1786 // that would create a race condition if the driver closes it on a
1787 // failure path: some other thread might create an fd with the same
1788 // number between the time the driver closes it and the time we close
1789 // it. We must assume one of: the driver *always* closes it even on
1790 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001791 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001792 swapchain.images[idx].dequeued = false;
1793 swapchain.images[idx].dequeue_fence = -1;
1794 return result;
1795 }
1796
1797 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001798 return VK_SUCCESS;
1799}
1800
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001801VKAPI_ATTR
1802VkResult AcquireNextImage2KHR(VkDevice device,
1803 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1804 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001805 ATRACE_CALL();
1806
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001807 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1808 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1809 pAcquireInfo->fence, pImageIndex);
1810}
1811
Jesse Halldc225072016-05-30 22:40:14 -07001812static VkResult WorstPresentResult(VkResult a, VkResult b) {
1813 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1814 // (in spec version 1.0.14).
1815 static const VkResult kWorstToBest[] = {
1816 VK_ERROR_DEVICE_LOST,
1817 VK_ERROR_SURFACE_LOST_KHR,
1818 VK_ERROR_OUT_OF_DATE_KHR,
1819 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1820 VK_ERROR_OUT_OF_HOST_MEMORY,
1821 VK_SUBOPTIMAL_KHR,
1822 };
1823 for (auto result : kWorstToBest) {
1824 if (a == result || b == result)
1825 return result;
1826 }
1827 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1828 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1829 return a != VK_SUCCESS ? a : b;
1830}
1831
Jesse Halle1b12782015-11-30 11:27:32 -08001832VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001833VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001834 ATRACE_CALL();
1835
Jesse Halld7b994a2015-09-07 14:17:37 -07001836 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1837 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1838 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001839
Jesse Halldc225072016-05-30 22:40:14 -07001840 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001841 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001842 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001843
Ian Elliottcb351132016-12-13 10:30:40 -07001844 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001845 const VkPresentRegionsKHR* present_regions = nullptr;
1846 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001847 const VkPresentRegionsKHR* next =
1848 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1849 while (next) {
1850 switch (next->sType) {
1851 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1852 present_regions = next;
1853 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001854 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001855 present_times =
1856 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1857 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001858 default:
1859 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1860 next->sType);
1861 break;
1862 }
1863 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1864 }
1865 ALOGV_IF(
1866 present_regions &&
1867 present_regions->swapchainCount != present_info->swapchainCount,
1868 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001869 ALOGV_IF(present_times &&
1870 present_times->swapchainCount != present_info->swapchainCount,
1871 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1872 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001873 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001874 (present_regions) ? present_regions->pRegions : nullptr;
1875 const VkPresentTimeGOOGLE* times =
1876 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001877 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001878 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001879 uint32_t nrects = 0;
1880
Jesse Halld7b994a2015-09-07 14:17:37 -07001881 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1882 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001883 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001884 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001885 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001886 const VkPresentRegionKHR* region =
1887 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001888 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001889 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001890 VkResult result;
1891 int err;
1892
Jesse Halld7b994a2015-09-07 14:17:37 -07001893 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001894 result = dispatch.QueueSignalReleaseImageANDROID(
1895 queue, present_info->waitSemaphoreCount,
1896 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001897 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001898 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001899 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001900 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001901 if (img.release_fence >= 0)
1902 close(img.release_fence);
1903 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001904
Jesse Halldc225072016-05-30 22:40:14 -07001905 if (swapchain.surface.swapchain_handle ==
1906 present_info->pSwapchains[sc]) {
1907 ANativeWindow* window = swapchain.surface.window.get();
1908 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001909 if (region) {
1910 // Process the incremental-present hint for this swapchain:
1911 uint32_t rcount = region->rectangleCount;
1912 if (rcount > nrects) {
1913 android_native_rect_t* new_rects =
1914 static_cast<android_native_rect_t*>(
1915 allocator->pfnReallocation(
1916 allocator->pUserData, rects,
1917 sizeof(android_native_rect_t) * rcount,
1918 alignof(android_native_rect_t),
1919 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1920 if (new_rects) {
1921 rects = new_rects;
1922 nrects = rcount;
1923 } else {
1924 rcount = 0; // Ignore the hint for this swapchain
1925 }
1926 }
1927 for (uint32_t r = 0; r < rcount; ++r) {
1928 if (region->pRectangles[r].layer > 0) {
1929 ALOGV(
1930 "vkQueuePresentKHR ignoring invalid layer "
1931 "(%u); using layer 0 instead",
1932 region->pRectangles[r].layer);
1933 }
1934 int x = region->pRectangles[r].offset.x;
1935 int y = region->pRectangles[r].offset.y;
1936 int width = static_cast<int>(
1937 region->pRectangles[r].extent.width);
1938 int height = static_cast<int>(
1939 region->pRectangles[r].extent.height);
1940 android_native_rect_t* cur_rect = &rects[r];
1941 cur_rect->left = x;
1942 cur_rect->top = y + height;
1943 cur_rect->right = x + width;
1944 cur_rect->bottom = y;
1945 }
1946 native_window_set_surface_damage(window, rects, rcount);
1947 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001948 if (time) {
1949 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001950 ALOGV(
1951 "Calling "
1952 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001953 native_window_enable_frame_timestamps(window, true);
1954 swapchain.frame_timestamps_enabled = true;
1955 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001956
1957 // Record the nativeFrameId so it can be later correlated to
1958 // this present.
1959 uint64_t nativeFrameId = 0;
1960 err = native_window_get_next_frame_id(
1961 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001962 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001963 ALOGE("Failed to get next native frame ID.");
1964 }
1965
1966 // Add a new timing record with the user's presentID and
1967 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001968 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001969 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001970 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001971 }
1972 if (time->desiredPresentTime) {
1973 // Set the desiredPresentTime:
1974 ALOGV(
1975 "Calling "
1976 "native_window_set_buffers_timestamp(%" PRId64 ")",
1977 time->desiredPresentTime);
1978 native_window_set_buffers_timestamp(
1979 window,
1980 static_cast<int64_t>(time->desiredPresentTime));
1981 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001982 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001983
Jesse Halldc225072016-05-30 22:40:14 -07001984 err = window->queueBuffer(window, img.buffer.get(), fence);
1985 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001986 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001987 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1988 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001989 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001990 } else {
1991 if (img.dequeue_fence >= 0) {
1992 close(img.dequeue_fence);
1993 img.dequeue_fence = -1;
1994 }
1995 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001996 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001997
1998 // If the swapchain is in shared mode, immediately dequeue the
1999 // buffer so it can be presented again without an intervening
2000 // call to AcquireNextImageKHR. We expect to get the same buffer
2001 // back from every call to dequeueBuffer in this mode.
2002 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
2003 ANativeWindowBuffer* buffer;
2004 int fence_fd;
2005 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002006 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13002007 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
2008 swapchain_result = WorstPresentResult(swapchain_result,
2009 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002010 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13002011 ALOGE("got wrong image back for shared swapchain");
2012 swapchain_result = WorstPresentResult(swapchain_result,
2013 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002014 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13002015 img.dequeue_fence = fence_fd;
2016 img.dequeued = true;
2017 }
2018 }
Jesse Halldc225072016-05-30 22:40:14 -07002019 }
2020 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07002021 OrphanSwapchain(device, &swapchain);
2022 }
silence_dogood73597592019-05-23 16:57:37 -07002023 int window_transform_hint;
2024 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
2025 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002026 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07002027 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
2028 strerror(-err), err);
2029 swapchain_result = WorstPresentResult(
2030 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
2031 }
2032 if (swapchain.pre_transform != window_transform_hint) {
2033 swapchain_result =
2034 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
2035 }
Jesse Halldc225072016-05-30 22:40:14 -07002036 } else {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00002037 ReleaseSwapchainImage(device, swapchain.shared, nullptr, fence,
2038 img, true);
Jesse Halldc225072016-05-30 22:40:14 -07002039 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07002040 }
2041
Jesse Halla9e57032015-11-30 01:03:10 -08002042 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07002043 present_info->pResults[sc] = swapchain_result;
2044
2045 if (swapchain_result != final_result)
2046 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07002047 }
Ian Elliottcb351132016-12-13 10:30:40 -07002048 if (rects) {
2049 allocator->pfnFree(allocator->pUserData, rects);
2050 }
Jesse Halld7b994a2015-09-07 14:17:37 -07002051
2052 return final_result;
2053}
Jesse Hallb1352bc2015-09-04 16:12:33 -07002054
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002055VKAPI_ATTR
2056VkResult GetRefreshCycleDurationGOOGLE(
2057 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07002058 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002059 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002060 ATRACE_CALL();
2061
Ian Elliott62c48c92017-01-20 13:13:20 -07002062 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002063 VkResult result = VK_SUCCESS;
2064
Ian Elliott3568bfe2019-05-03 15:54:46 -06002065 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002066
2067 return result;
2068}
2069
2070VKAPI_ATTR
2071VkResult GetPastPresentationTimingGOOGLE(
2072 VkDevice,
2073 VkSwapchainKHR swapchain_handle,
2074 uint32_t* count,
2075 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002076 ATRACE_CALL();
2077
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002078 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002079 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2080 return VK_ERROR_OUT_OF_DATE_KHR;
2081 }
2082
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002083 ANativeWindow* window = swapchain.surface.window.get();
2084 VkResult result = VK_SUCCESS;
2085
2086 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07002087 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002088 native_window_enable_frame_timestamps(window, true);
2089 swapchain.frame_timestamps_enabled = true;
2090 }
2091
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002092 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07002093 // Get the latest ready timing count before copying, since the copied
2094 // timing info will be erased in copy_ready_timings function.
2095 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07002096 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002097 // Check the *count here against the recorded ready timing count, since
2098 // *count can be overwritten per spec describes.
2099 if (*count < n) {
2100 result = VK_INCOMPLETE;
2101 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002102 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07002103 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002104 }
2105
2106 return result;
2107}
2108
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002109VKAPI_ATTR
2110VkResult GetSwapchainStatusKHR(
2111 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13002112 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002113 ATRACE_CALL();
2114
Chris Forbes4e18ba82017-01-20 12:50:17 +13002115 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002116 VkResult result = VK_SUCCESS;
2117
Chris Forbes4e18ba82017-01-20 12:50:17 +13002118 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2119 return VK_ERROR_OUT_OF_DATE_KHR;
2120 }
2121
Yiwei Zhanga885c062019-10-24 12:07:57 -07002122 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002123
2124 return result;
2125}
2126
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002127VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002128 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002129 uint32_t swapchainCount,
2130 const VkSwapchainKHR* pSwapchains,
2131 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002132 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002133
2134 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
2135 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
2136 if (!swapchain)
2137 continue;
2138
2139 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
2140
2141 ANativeWindow* window = swapchain->surface.window.get();
2142
2143 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
2144 const android_smpte2086_metadata smpteMetdata = {
2145 {vulkanMetadata.displayPrimaryRed.x,
2146 vulkanMetadata.displayPrimaryRed.y},
2147 {vulkanMetadata.displayPrimaryGreen.x,
2148 vulkanMetadata.displayPrimaryGreen.y},
2149 {vulkanMetadata.displayPrimaryBlue.x,
2150 vulkanMetadata.displayPrimaryBlue.y},
2151 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
2152 vulkanMetadata.maxLuminance,
2153 vulkanMetadata.minLuminance};
2154 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
2155
2156 const android_cta861_3_metadata cta8613Metadata = {
2157 vulkanMetadata.maxContentLightLevel,
2158 vulkanMetadata.maxFrameAverageLightLevel};
2159 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
2160 }
2161
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002162 return;
2163}
2164
Yiwei Zhang0f475222019-04-11 19:38:00 -07002165static void InterceptBindImageMemory2(
2166 uint32_t bind_info_count,
2167 const VkBindImageMemoryInfo* bind_infos,
2168 std::vector<VkNativeBufferANDROID>* out_native_buffers,
2169 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
2170 out_native_buffers->clear();
2171 out_bind_infos->clear();
2172
2173 if (!bind_info_count)
2174 return;
2175
2176 std::unordered_set<uint32_t> intercepted_indexes;
2177
2178 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2179 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2180 bind_infos[idx].pNext);
2181 while (info &&
2182 info->sType !=
2183 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
2184 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2185 info->pNext);
2186 }
2187
2188 if (!info)
2189 continue;
2190
2191 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
2192 "swapchain handle must not be NULL");
2193 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
2194 ALOG_ASSERT(
2195 info->imageIndex < swapchain->num_images,
2196 "imageIndex must be less than the number of images in swapchain");
2197
2198 ANativeWindowBuffer* buffer =
2199 swapchain->images[info->imageIndex].buffer.get();
2200 VkNativeBufferANDROID native_buffer = {
2201#pragma clang diagnostic push
2202#pragma clang diagnostic ignored "-Wold-style-cast"
2203 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2204#pragma clang diagnostic pop
2205 .pNext = bind_infos[idx].pNext,
2206 .handle = buffer->handle,
2207 .stride = buffer->stride,
2208 .format = buffer->format,
2209 .usage = int(buffer->usage),
2210 };
2211 // Reserve enough space to avoid letting re-allocation invalidate the
2212 // addresses of the elements inside.
2213 out_native_buffers->reserve(bind_info_count);
2214 out_native_buffers->emplace_back(native_buffer);
2215
2216 // Reserve the space now since we know how much is needed now.
2217 out_bind_infos->reserve(bind_info_count);
2218 out_bind_infos->emplace_back(bind_infos[idx]);
2219 out_bind_infos->back().pNext = &out_native_buffers->back();
2220
2221 intercepted_indexes.insert(idx);
2222 }
2223
2224 if (intercepted_indexes.empty())
2225 return;
2226
2227 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2228 if (intercepted_indexes.count(idx))
2229 continue;
2230 out_bind_infos->emplace_back(bind_infos[idx]);
2231 }
2232}
2233
Yiwei Zhang23143102019-04-10 18:24:05 -07002234VKAPI_ATTR
2235VkResult BindImageMemory2(VkDevice device,
2236 uint32_t bindInfoCount,
2237 const VkBindImageMemoryInfo* pBindInfos) {
2238 ATRACE_CALL();
2239
Yiwei Zhang0f475222019-04-11 19:38:00 -07002240 // out_native_buffers is for maintaining the lifecycle of the constructed
2241 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2242 std::vector<VkNativeBufferANDROID> out_native_buffers;
2243 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2244 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2245 &out_bind_infos);
2246 return GetData(device).driver.BindImageMemory2(
2247 device, bindInfoCount,
2248 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002249}
2250
2251VKAPI_ATTR
2252VkResult BindImageMemory2KHR(VkDevice device,
2253 uint32_t bindInfoCount,
2254 const VkBindImageMemoryInfo* pBindInfos) {
2255 ATRACE_CALL();
2256
Yiwei Zhang0f475222019-04-11 19:38:00 -07002257 std::vector<VkNativeBufferANDROID> out_native_buffers;
2258 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2259 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2260 &out_bind_infos);
2261 return GetData(device).driver.BindImageMemory2KHR(
2262 device, bindInfoCount,
2263 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002264}
2265
Chia-I Wu62262232016-03-26 07:06:44 +08002266} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002267} // namespace vulkan