blob: e9935e57817161fdc8581ab005cfe6e2dfe193e5 [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 // TODO: Do we need to query for VK_EXT_rgba10x6_formats here?
512 case VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16:
513 native_format = android::PIXEL_FORMAT_RGBA_10101010;
514 break;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700515 default:
516 ALOGV("unsupported swapchain format %d", format);
517 break;
518 }
519 return native_format;
520}
521
522android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
523 switch (colorspace) {
524 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
525 return HAL_DATASPACE_V0_SRGB;
526 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
527 return HAL_DATASPACE_DISPLAY_P3;
528 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
529 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600530 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
531 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700532 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
533 return HAL_DATASPACE_DCI_P3_LINEAR;
534 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
535 return HAL_DATASPACE_DCI_P3;
536 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
537 return HAL_DATASPACE_V0_SRGB_LINEAR;
538 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
539 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600540 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
541 return HAL_DATASPACE_BT2020_LINEAR;
542 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700543 return static_cast<android_dataspace>(
544 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
545 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600546 case VK_COLOR_SPACE_DOLBYVISION_EXT:
547 return static_cast<android_dataspace>(
548 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
549 HAL_DATASPACE_RANGE_FULL);
550 case VK_COLOR_SPACE_HDR10_HLG_EXT:
551 return static_cast<android_dataspace>(
552 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
553 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700554 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
555 return static_cast<android_dataspace>(
556 HAL_DATASPACE_STANDARD_ADOBE_RGB |
557 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
558 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
559 return HAL_DATASPACE_ADOBE_RGB;
560
561 // Pass through is intended to allow app to provide data that is passed
562 // to the display system without modification.
563 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
564 return HAL_DATASPACE_ARBITRARY;
565
566 default:
567 // This indicates that we don't know about the
568 // dataspace specified and we should indicate that
569 // it's unsupported
570 return HAL_DATASPACE_UNKNOWN;
571 }
572}
573
Jesse Halld7b994a2015-09-07 14:17:37 -0700574} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700575
Jesse Halle1b12782015-11-30 11:27:32 -0800576VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800577VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800578 VkInstance instance,
579 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
580 const VkAllocationCallbacks* allocator,
581 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800582 ATRACE_CALL();
583
Jesse Hall1f91d392015-12-11 16:28:44 -0800584 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800585 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800586 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
587 alignof(Surface),
588 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800589 if (!mem)
590 return VK_ERROR_OUT_OF_HOST_MEMORY;
591 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700592
Chia-I Wue8e689f2016-04-18 08:21:31 +0800593 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700594 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700595 int err = native_window_get_consumer_usage(surface->window.get(),
596 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800597 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700598 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
599 strerror(-err), err);
600 surface->~Surface();
601 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700602 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700603 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700604
Yiwei Zhang6435b322018-05-08 11:12:17 -0700605 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800606 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800607 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800608 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
609 err);
610 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800611 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700612 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800613 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700614
Jesse Hall1356b0d2015-11-23 17:24:58 -0800615 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700616 return VK_SUCCESS;
617}
618
Jesse Halle1b12782015-11-30 11:27:32 -0800619VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800620void DestroySurfaceKHR(VkInstance instance,
621 VkSurfaceKHR surface_handle,
622 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800623 ATRACE_CALL();
624
Jesse Hall1356b0d2015-11-23 17:24:58 -0800625 Surface* surface = SurfaceFromHandle(surface_handle);
626 if (!surface)
627 return;
628 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700629 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700630 "destroyed VkSurfaceKHR 0x%" PRIx64
631 " has active VkSwapchainKHR 0x%" PRIx64,
632 reinterpret_cast<uint64_t>(surface_handle),
633 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800634 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800635 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800636 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800637 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800638}
639
Jesse Halle1b12782015-11-30 11:27:32 -0800640VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800641VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
642 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000643 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800644 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000645 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800646 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800647}
648
Jesse Halle1b12782015-11-30 11:27:32 -0800649VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800650VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Ian Elliott1ce053f2022-03-16 09:49:53 -0600651 VkPhysicalDevice pdev,
Jesse Hallb00daad2015-11-29 19:46:20 -0800652 VkSurfaceKHR surface,
653 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800654 ATRACE_CALL();
655
Jesse Halld7b994a2015-09-07 14:17:37 -0700656 int err;
Jesse Halld7b994a2015-09-07 14:17:37 -0700657 int width, height;
Jesse Hall55bc0972016-02-23 16:43:29 -0800658 int transform_hint;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800659 int max_buffer_count;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600660 if (surface == VK_NULL_HANDLE) {
661 const InstanceData& instance_data = GetData(pdev);
662 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
663 bool surfaceless_enabled =
664 instance_data.hook_extensions.test(surfaceless);
665 if (!surfaceless_enabled) {
666 // It is an error to pass a surface==VK_NULL_HANDLE unless the
667 // VK_GOOGLE_surfaceless_query extension is enabled
668 return VK_ERROR_SURFACE_LOST_KHR;
669 }
670 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
671 // extension for this function is for
672 // VkSurfaceProtectedCapabilitiesKHR::supportsProtected. The following
673 // four values cannot be known without a surface. Default values will
674 // be supplied anyway, but cannot be relied upon.
Ian Elliottb4576372022-09-08 15:13:39 -0600675 width = 0xFFFFFFFF;
676 height = 0xFFFFFFFF;
677 transform_hint = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
678 capabilities->minImageCount = 0xFFFFFFFF;
679 capabilities->maxImageCount = 0xFFFFFFFF;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600680 } else {
681 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
682
683 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
684 if (err != android::OK) {
685 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
686 strerror(-err), err);
687 return VK_ERROR_SURFACE_LOST_KHR;
688 }
689 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
690 if (err != android::OK) {
691 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
692 strerror(-err), err);
693 return VK_ERROR_SURFACE_LOST_KHR;
694 }
695
696 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
697 &transform_hint);
698 if (err != android::OK) {
699 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
700 strerror(-err), err);
701 return VK_ERROR_SURFACE_LOST_KHR;
702 }
703
704 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT,
705 &max_buffer_count);
706 if (err != android::OK) {
707 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
708 strerror(-err), err);
709 return VK_ERROR_SURFACE_LOST_KHR;
710 }
Ian Elliottb4576372022-09-08 15:13:39 -0600711 capabilities->minImageCount = std::min(max_buffer_count, 3);
712 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800713 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700714
Jesse Hallfe2662d2016-02-09 13:26:59 -0800715 capabilities->currentExtent =
716 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
717
Yiwei Zhang70a21962019-05-31 17:26:52 -0700718 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800719 capabilities->minImageExtent = VkExtent2D{1, 1};
720 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700721
Trevor David Blackc00a1212022-11-14 21:13:14 +0000722 if (capabilities->maxImageExtent.height <
723 capabilities->currentExtent.height) {
724 capabilities->maxImageExtent.height =
725 capabilities->currentExtent.height;
726 }
727
728 if (capabilities->maxImageExtent.width <
729 capabilities->currentExtent.width) {
730 capabilities->maxImageExtent.width = capabilities->currentExtent.width;
731 }
732
Jesse Hallfe2662d2016-02-09 13:26:59 -0800733 capabilities->maxImageArrayLayers = 1;
734
Jesse Hall55bc0972016-02-23 16:43:29 -0800735 capabilities->supportedTransforms = kSupportedTransforms;
736 capabilities->currentTransform =
737 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700738
Jesse Hallfe2662d2016-02-09 13:26:59 -0800739 // On Android, window composition is a WindowManager property, not something
740 // associated with the bufferqueue. It can't be changed from here.
741 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700742
Jesse Hallb00daad2015-11-29 19:46:20 -0800743 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800744 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
745 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
746 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700747 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
748
Jesse Hallb1352bc2015-09-04 16:12:33 -0700749 return VK_SUCCESS;
750}
751
Jesse Halle1b12782015-11-30 11:27:32 -0800752VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700753VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
754 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800755 uint32_t* count,
756 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800757 ATRACE_CALL();
758
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700759 const InstanceData& instance_data = GetData(pdev);
760
Ian Elliott1ce053f2022-03-16 09:49:53 -0600761 uint64_t consumer_usage = 0;
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000762 bool colorspace_ext =
Ian Elliottf6df08e2022-03-16 21:27:49 -0600763 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600764 if (surface_handle == VK_NULL_HANDLE) {
765 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
766 bool surfaceless_enabled =
767 instance_data.hook_extensions.test(surfaceless);
768 if (!surfaceless_enabled) {
769 return VK_ERROR_SURFACE_LOST_KHR;
770 }
Chris Forbesa9e28de2022-10-20 09:05:48 +1300771 // Support for VK_GOOGLE_surfaceless_query.
Ian Elliott1ce053f2022-03-16 09:49:53 -0600772
773 // TODO(b/203826952): research proper value; temporarily use the
774 // values seen on Pixel
775 consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
776 } else {
777 Surface& surface = *SurfaceFromHandle(surface_handle);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600778 consumer_usage = surface.consumer_usage;
Ian Elliott6ba85d92022-02-18 16:44:58 -0700779 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700780
Charlie Lao324191f2019-12-13 11:03:16 -0800781 AHardwareBuffer_Desc desc = {};
782 desc.width = 1;
783 desc.height = 1;
784 desc.layers = 1;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600785 desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
Charlie Lao324191f2019-12-13 11:03:16 -0800786 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
787
788 // We must support R8G8B8A8
789 std::vector<VkSurfaceFormatKHR> all_formats = {
790 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700791 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700792 };
Charlie Lao324191f2019-12-13 11:03:16 -0800793
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000794 if (colorspace_ext) {
795 all_formats.emplace_back(VkSurfaceFormatKHR{
Jason Macnakca506332022-11-09 11:00:36 -0800796 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT});
797 all_formats.emplace_back(VkSurfaceFormatKHR{
798 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_PASS_THROUGH_EXT});
799 all_formats.emplace_back(VkSurfaceFormatKHR{
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000800 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_BT709_LINEAR_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800801 all_formats.emplace_back(VkSurfaceFormatKHR{
802 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
803 all_formats.emplace_back(VkSurfaceFormatKHR{
804 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
805 }
806
Ian Elliott1ce053f2022-03-16 09:49:53 -0600807 // NOTE: Any new formats that are added must be coordinated across different
808 // Android users. This includes the ANGLE team (a layered implementation of
809 // OpenGL-ES).
810
Charlie Lao324191f2019-12-13 11:03:16 -0800811 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
812 if (AHardwareBuffer_isSupported(&desc)) {
813 all_formats.emplace_back(VkSurfaceFormatKHR{
814 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800815 if (colorspace_ext) {
816 all_formats.emplace_back(
817 VkSurfaceFormatKHR{VK_FORMAT_R5G6B5_UNORM_PACK16,
818 VK_COLOR_SPACE_PASS_THROUGH_EXT});
819 }
Charlie Lao324191f2019-12-13 11:03:16 -0800820 }
821
822 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
823 if (AHardwareBuffer_isSupported(&desc)) {
824 all_formats.emplace_back(VkSurfaceFormatKHR{
825 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800826 if (colorspace_ext) {
827 all_formats.emplace_back(
828 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
829 VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800830 all_formats.emplace_back(
831 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
832 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
833 all_formats.emplace_back(
834 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
835 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
836 }
837 }
838
839 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
840 if (AHardwareBuffer_isSupported(&desc)) {
841 all_formats.emplace_back(
842 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
843 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800844 if (colorspace_ext) {
845 all_formats.emplace_back(
846 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
847 VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800848 all_formats.emplace_back(
849 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
850 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
851 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700852 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700853
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500854 desc.format = AHARDWAREBUFFER_FORMAT_R8_UNORM;
855 if (AHardwareBuffer_isSupported(&desc)) {
Jason Macnakca506332022-11-09 11:00:36 -0800856 if (colorspace_ext) {
857 all_formats.emplace_back(VkSurfaceFormatKHR{
858 VK_FORMAT_R8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT});
859 }
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500860 }
861
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000862 // TODO query VK_EXT_rgba10x6_formats support
863 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM;
864 if (AHardwareBuffer_isSupported(&desc)) {
865 all_formats.emplace_back(
866 VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
867 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
868 if (colorspace_ext) {
869 all_formats.emplace_back(
870 VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
871 VK_COLOR_SPACE_PASS_THROUGH_EXT});
872 all_formats.emplace_back(
873 VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
874 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
875 }
876 }
877
Ian Elliott6ba85d92022-02-18 16:44:58 -0700878 // NOTE: Any new formats that are added must be coordinated across different
879 // Android users. This includes the ANGLE team (a layered implementation of
880 // OpenGL-ES).
881
Jesse Halld7b994a2015-09-07 14:17:37 -0700882 VkResult result = VK_SUCCESS;
883 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800884 uint32_t transfer_count = all_formats.size();
885 if (transfer_count > *count) {
886 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700887 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700888 }
Charlie Lao324191f2019-12-13 11:03:16 -0800889 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
890 formats);
891 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700892 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800893 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700894 }
Charlie Lao324191f2019-12-13 11:03:16 -0800895
Jesse Halld7b994a2015-09-07 14:17:37 -0700896 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700897}
898
Jesse Halle1b12782015-11-30 11:27:32 -0800899VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300900VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
901 VkPhysicalDevice physicalDevice,
902 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
903 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800904 ATRACE_CALL();
905
Chris Forbes2452cf72017-03-16 16:30:17 +1300906 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
907 physicalDevice, pSurfaceInfo->surface,
908 &pSurfaceCapabilities->surfaceCapabilities);
909
Chris Forbes06bc0092017-03-16 16:46:05 +1300910 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
911 while (caps->pNext) {
912 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
913
914 switch (caps->sType) {
915 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
916 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
917 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
918 caps);
919 // Claim same set of usage flags are supported for
920 // shared present modes as for other modes.
921 shared_caps->sharedPresentSupportedUsageFlags =
922 pSurfaceCapabilities->surfaceCapabilities
923 .supportedUsageFlags;
924 } break;
925
Ian Elliottbb67b242022-03-16 09:52:28 -0600926 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
927 VkSurfaceProtectedCapabilitiesKHR* protected_caps =
928 reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(caps);
929 protected_caps->supportsProtected = VK_TRUE;
930 } break;
931
Chris Forbes06bc0092017-03-16 16:46:05 +1300932 default:
933 // Ignore all other extension structs
934 break;
935 }
936 }
937
Chris Forbes2452cf72017-03-16 16:30:17 +1300938 return result;
939}
940
941VKAPI_ATTR
942VkResult GetPhysicalDeviceSurfaceFormats2KHR(
943 VkPhysicalDevice physicalDevice,
944 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
945 uint32_t* pSurfaceFormatCount,
946 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800947 ATRACE_CALL();
948
Chris Forbes2452cf72017-03-16 16:30:17 +1300949 if (!pSurfaceFormats) {
950 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
951 pSurfaceInfo->surface,
952 pSurfaceFormatCount, nullptr);
Trevor David Black929e9cd2022-11-22 04:12:19 +0000953 }
Chris Forbes2452cf72017-03-16 16:30:17 +1300954
Trevor David Black929e9cd2022-11-22 04:12:19 +0000955 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
956 // after the call.
957 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
958 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
959 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
960 surface_formats.data());
Trevor David Black2cc44682022-03-09 00:31:38 +0000961
Trevor David Black929e9cd2022-11-22 04:12:19 +0000962 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
963 return result;
964 }
Trevor David Black2cc44682022-03-09 00:31:38 +0000965
Trevor David Black929e9cd2022-11-22 04:12:19 +0000966 const auto& driver = GetData(physicalDevice).driver;
967
968 // marshal results individually due to stride difference.
969 uint32_t formats_to_marshal = *pSurfaceFormatCount;
970 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
971 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
972
973 // Query the compression properties for the surface format
974 VkSurfaceFormat2KHR* pSurfaceFormat = &pSurfaceFormats[i];
975 while (pSurfaceFormat->pNext) {
976 pSurfaceFormat =
977 reinterpret_cast<VkSurfaceFormat2KHR*>(pSurfaceFormat->pNext);
978 switch (pSurfaceFormat->sType) {
979 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT: {
Trevor David Black2cc44682022-03-09 00:31:38 +0000980 VkImageCompressionPropertiesEXT* surfaceCompressionProps =
981 reinterpret_cast<VkImageCompressionPropertiesEXT*>(
Trevor David Black929e9cd2022-11-22 04:12:19 +0000982 pSurfaceFormat);
Trevor David Black2cc44682022-03-09 00:31:38 +0000983
984 if (surfaceCompressionProps &&
985 driver.GetPhysicalDeviceImageFormatProperties2KHR) {
986 VkPhysicalDeviceImageFormatInfo2 imageFormatInfo = {};
987 imageFormatInfo.sType =
988 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2;
989 imageFormatInfo.format =
990 pSurfaceFormats[i].surfaceFormat.format;
991 imageFormatInfo.pNext = nullptr;
992
993 VkImageCompressionControlEXT compressionControl = {};
994 compressionControl.sType =
995 VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT;
996 compressionControl.pNext = imageFormatInfo.pNext;
997
998 imageFormatInfo.pNext = &compressionControl;
999
1000 VkImageCompressionPropertiesEXT compressionProps = {};
1001 compressionProps.sType =
1002 VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT;
1003 compressionProps.pNext = nullptr;
1004
1005 VkImageFormatProperties2KHR imageFormatProps = {};
1006 imageFormatProps.sType =
1007 VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR;
1008 imageFormatProps.pNext = &compressionProps;
1009
1010 VkResult compressionRes =
1011 driver.GetPhysicalDeviceImageFormatProperties2KHR(
1012 physicalDevice, &imageFormatInfo,
1013 &imageFormatProps);
1014 if (compressionRes == VK_SUCCESS) {
1015 surfaceCompressionProps->imageCompressionFlags =
1016 compressionProps.imageCompressionFlags;
1017 surfaceCompressionProps
1018 ->imageCompressionFixedRateFlags =
1019 compressionProps.imageCompressionFixedRateFlags;
1020 } else {
1021 return compressionRes;
1022 }
1023 }
Trevor David Black929e9cd2022-11-22 04:12:19 +00001024 } break;
1025
1026 default:
1027 // Ignore all other extension structs
1028 break;
Chris Forbes2452cf72017-03-16 16:30:17 +13001029 }
1030 }
Chris Forbes2452cf72017-03-16 16:30:17 +13001031 }
Trevor David Black929e9cd2022-11-22 04:12:19 +00001032
1033 return result;
Chris Forbes2452cf72017-03-16 16:30:17 +13001034}
1035
1036VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +13001037VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -08001038 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +08001039 uint32_t* count,
1040 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001041 ATRACE_CALL();
1042
Yiwei Zhange4a559c2018-02-15 11:27:36 -08001043 int err;
1044 int query_value;
Ian Elliotte7f036c2022-03-15 16:49:21 -06001045 std::vector<VkPresentModeKHR> present_modes;
Ian Elliott1ce053f2022-03-16 09:49:53 -06001046 if (surface == VK_NULL_HANDLE) {
1047 const InstanceData& instance_data = GetData(pdev);
1048 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
1049 bool surfaceless_enabled =
1050 instance_data.hook_extensions.test(surfaceless);
1051 if (!surfaceless_enabled) {
1052 return VK_ERROR_SURFACE_LOST_KHR;
1053 }
1054 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
1055 // extension for this function is for
1056 // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and
1057 // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot
1058 // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a
Ian Elliott7e361142022-06-02 10:45:17 -06001059 // surface, and that cannot be relied upon. Therefore, don't return it.
Ian Elliott1ce053f2022-03-16 09:49:53 -06001060 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1061 } else {
1062 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1063
1064 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1065 &query_value);
1066 if (err != android::OK || query_value < 0) {
1067 ALOGE(
1068 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
1069 "value=%d",
1070 strerror(-err), err, query_value);
1071 return VK_ERROR_SURFACE_LOST_KHR;
1072 }
1073 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1074
1075 err =
1076 window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
1077 if (err != android::OK || query_value < 0) {
1078 ALOGE(
1079 "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
1080 strerror(-err), err, query_value);
1081 return VK_ERROR_SURFACE_LOST_KHR;
1082 }
1083 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
1084
1085 if (min_undequeued_buffers + 1 < max_buffer_count)
1086 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
1087 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1088 }
Chris Forbese8d79a62017-02-22 12:49:18 +13001089
1090 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001091 QueryPresentationProperties(pdev, &present_properties);
1092 if (present_properties.sharedImage) {
1093 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
1094 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +13001095 }
1096
1097 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -07001098
1099 VkResult result = VK_SUCCESS;
1100 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +13001101 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -07001102 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +13001103 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -07001104 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -07001105 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +13001106 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -07001107 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001108 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001109}
1110
Jesse Halle1b12782015-11-30 11:27:32 -08001111VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001112VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001113 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001114 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001115 ATRACE_CALL();
1116
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001117 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
1118 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
1119 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
1120 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
1121 pDeviceGroupPresentCapabilities->sType);
1122
1123 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
1124 sizeof(pDeviceGroupPresentCapabilities->presentMask));
1125
1126 // assume device group of size 1
1127 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
1128 pDeviceGroupPresentCapabilities->modes =
1129 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1130
1131 return VK_SUCCESS;
1132}
1133
1134VKAPI_ATTR
1135VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001136 VkDevice,
1137 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001138 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001139 ATRACE_CALL();
1140
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001141 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1142 return VK_SUCCESS;
1143}
1144
1145VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -06001146VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001147 VkSurfaceKHR surface,
1148 uint32_t* pRectCount,
1149 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001150 ATRACE_CALL();
1151
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001152 if (!pRects) {
1153 *pRectCount = 1;
1154 } else {
1155 uint32_t count = std::min(*pRectCount, 1u);
1156 bool incomplete = *pRectCount < 1;
1157
1158 *pRectCount = count;
1159
1160 if (incomplete) {
1161 return VK_INCOMPLETE;
1162 }
1163
1164 int err;
1165 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1166
1167 int width = 0, height = 0;
1168 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001169 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001170 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1171 strerror(-err), err);
1172 }
1173 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001174 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001175 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1176 strerror(-err), err);
1177 }
1178
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001179 pRects[0].offset.x = 0;
1180 pRects[0].offset.y = 0;
1181 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1182 static_cast<uint32_t>(height)};
1183 }
1184 return VK_SUCCESS;
1185}
1186
Yiwei Zhang533cea92019-06-03 18:43:24 -07001187static void DestroySwapchainInternal(VkDevice device,
1188 VkSwapchainKHR swapchain_handle,
1189 const VkAllocationCallbacks* allocator) {
1190 ATRACE_CALL();
1191
1192 const auto& dispatch = GetData(device).driver;
1193 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1194 if (!swapchain) {
1195 return;
1196 }
1197
1198 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1199 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1200
1201 if (window && swapchain->frame_timestamps_enabled) {
1202 native_window_enable_frame_timestamps(window, false);
1203 }
1204
1205 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001206 ReleaseSwapchainImage(device, swapchain->shared, window, -1,
1207 swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001208 }
1209
1210 if (active) {
1211 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1212 }
1213
1214 if (!allocator) {
1215 allocator = &GetData(device).allocator;
1216 }
1217
1218 swapchain->~Swapchain();
1219 allocator->pfnFree(allocator->pUserData, swapchain);
1220}
1221
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001222VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001223VkResult CreateSwapchainKHR(VkDevice device,
1224 const VkSwapchainCreateInfoKHR* create_info,
1225 const VkAllocationCallbacks* allocator,
1226 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001227 ATRACE_CALL();
1228
Jesse Halld7b994a2015-09-07 14:17:37 -07001229 int err;
1230 VkResult result = VK_SUCCESS;
1231
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001232 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1233 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1234 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1235 " oldSwapchain=0x%" PRIx64,
1236 reinterpret_cast<uint64_t>(create_info->surface),
1237 create_info->minImageCount, create_info->imageFormat,
1238 create_info->imageColorSpace, create_info->imageExtent.width,
1239 create_info->imageExtent.height, create_info->imageUsage,
1240 create_info->preTransform, create_info->presentMode,
1241 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1242
Jesse Hall1f91d392015-12-11 16:28:44 -08001243 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001244 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001245
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001246 android::PixelFormat native_pixel_format =
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001247 GetNativePixelFormat(create_info->imageFormat);
1248 android_dataspace native_dataspace =
1249 GetNativeDataspace(create_info->imageColorSpace);
1250 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1251 ALOGE(
1252 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1253 "failed: Unsupported color space",
1254 create_info->imageColorSpace);
1255 return VK_ERROR_INITIALIZATION_FAILED;
1256 }
1257
Jesse Hall42a9eec2016-06-03 12:39:49 -07001258 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001259 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001260 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001261 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001262 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001263 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001264 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001265 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001266 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1267 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001268 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001269 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001270
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001271 Surface& surface = *SurfaceFromHandle(create_info->surface);
1272
Jesse Halldc225072016-05-30 22:40:14 -07001273 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001274 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001275 " because it already has active swapchain 0x%" PRIx64
1276 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1277 reinterpret_cast<uint64_t>(create_info->surface),
1278 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1279 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1280 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1281 }
1282 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1283 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1284
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001285 // -- Reset the native window --
1286 // The native window might have been used previously, and had its properties
1287 // changed from defaults. That will affect the answer we get for queries
1288 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1289 // attempt such queries.
1290
Jesse Halldc225072016-05-30 22:40:14 -07001291 // The native window only allows dequeueing all buffers before any have
1292 // been queued, since after that point at least one is assumed to be in
1293 // non-FREE state at any given time. Disconnecting and re-connecting
1294 // orphans the previous buffers, getting us back to the state where we can
1295 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001296 //
1297 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001298 ANativeWindow* window = surface.window.get();
1299 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1300 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001301 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001302 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1303 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001304 strerror(-err), err);
1305
Nicolas Capens147b7da2021-04-09 14:53:06 -04001306 err =
1307 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001308 if (err != android::OK) {
1309 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1310 strerror(-err), err);
1311 return VK_ERROR_SURFACE_LOST_KHR;
1312 }
1313
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301314 int swap_interval =
1315 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001316 err = window->setSwapInterval(window, swap_interval);
1317 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001318 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1319 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001320 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001321 }
1322
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001323 err = native_window_set_shared_buffer_mode(window, false);
1324 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001325 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1326 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001327 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001328 }
1329
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001330 err = native_window_set_auto_refresh(window, false);
1331 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001332 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1333 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001334 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001335 }
1336
Jesse Halld7b994a2015-09-07 14:17:37 -07001337 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001338
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001339 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001340
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001341 err = native_window_set_buffers_format(window, native_pixel_format);
1342 if (err != android::OK) {
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001343 ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)",
1344 decodePixelFormat(native_pixel_format).c_str(), strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001345 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001346 }
Yiwei Zhang51572c22022-07-22 23:08:30 +00001347
1348 /* Respect consumer default dataspace upon HAL_DATASPACE_ARBITRARY. */
1349 if (native_dataspace != HAL_DATASPACE_ARBITRARY) {
1350 err = native_window_set_buffers_data_space(window, native_dataspace);
1351 if (err != android::OK) {
1352 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1353 native_dataspace, strerror(-err), err);
1354 return VK_ERROR_SURFACE_LOST_KHR;
1355 }
Jesse Hall517274a2016-02-10 00:07:18 -08001356 }
1357
Jesse Hall3dd678a2016-01-08 21:52:01 -08001358 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001359 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001360 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001361 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001362 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1363 create_info->imageExtent.width, create_info->imageExtent.height,
1364 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001365 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001366 }
1367
Jesse Hall178b6962016-02-24 15:39:50 -08001368 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1369 // applied during rendering. native_window_set_transform() expects the
1370 // inverse: the transform the app is requesting that the compositor perform
1371 // during composition. With native windows, pre-transform works by rendering
1372 // with the same transform the compositor is applying (as in Vulkan), but
1373 // then requesting the inverse transform, so that when the compositor does
1374 // it's job the two transforms cancel each other out and the compositor ends
1375 // up applying an identity transform to the app's buffer.
1376 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001377 window, InvertTransformToNative(create_info->preTransform));
1378 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001379 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1380 InvertTransformToNative(create_info->preTransform),
1381 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001382 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001383 }
1384
Jesse Hallf64ca122015-11-03 16:11:10 -08001385 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001386 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1387 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001388 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1389 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001390 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001391 }
1392
Chris Forbes97ef4612017-03-30 19:37:50 +13001393 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1394 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1395 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1396 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001397 err = native_window_set_shared_buffer_mode(window, true);
1398 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001399 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1400 return VK_ERROR_SURFACE_LOST_KHR;
1401 }
1402 }
1403
1404 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001405 err = native_window_set_auto_refresh(window, true);
1406 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001407 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1408 return VK_ERROR_SURFACE_LOST_KHR;
1409 }
1410 }
1411
Ian Elliott16c443c2021-11-30 17:10:32 -07001412 int query_value;
1413 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1414 &query_value);
1415 if (err != android::OK || query_value < 0) {
1416 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1417 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001418 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001419 }
Ian Elliott16c443c2021-11-30 17:10:32 -07001420 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1421 const auto mailbox_num_images = std::max(3u, create_info->minImageCount);
1422 const auto requested_images =
1423 swap_interval ? create_info->minImageCount : mailbox_num_images;
1424 uint32_t num_images = requested_images - 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001425
Ian Elliott5396b702021-12-13 19:32:34 -07001426 // Lower layer insists that we have at least min_undequeued_buffers + 1
1427 // buffers. This is wasteful and we'd like to relax it in the shared case,
1428 // but not all the pieces are in place for that to work yet. Note we only
1429 // lie to the lower layer--we don't want to give the app back a swapchain
1430 // with extra images (which they can't actually use!).
1431 uint32_t min_buffer_count = min_undequeued_buffers + 1;
1432 err = native_window_set_buffer_count(
1433 window, std::max(min_buffer_count, num_images));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001434 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001435 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1436 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001437 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001438 }
1439
Ian Elliott12b7e2f2021-12-21 23:24:20 -07001440 // In shared mode the num_images must be one regardless of how many
1441 // buffers were allocated for the buffer queue.
1442 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1443 num_images = 1;
1444 }
1445
Trevor David Black2cc44682022-03-09 00:31:38 +00001446 void* usage_info_pNext = nullptr;
1447 VkImageCompressionControlEXT image_compression = {};
John Reck97678d42022-08-23 11:24:51 -04001448 uint64_t native_usage = 0;
Trevor David Black2cc44682022-03-09 00:31:38 +00001449 if (dispatch.GetSwapchainGrallocUsage3ANDROID) {
1450 ATRACE_BEGIN("GetSwapchainGrallocUsage3ANDROID");
1451 VkGrallocUsageInfoANDROID gralloc_usage_info = {};
1452 gralloc_usage_info.sType = VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID;
1453 gralloc_usage_info.format = create_info->imageFormat;
1454 gralloc_usage_info.imageUsage = create_info->imageUsage;
1455
1456 // Look through the pNext chain for an image compression control struct
1457 // if one is found AND the appropriate extensions are enabled,
1458 // append it to be the gralloc usage pNext chain
1459 const VkSwapchainCreateInfoKHR* create_infos = create_info;
1460 while (create_infos->pNext) {
1461 create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(
1462 create_infos->pNext);
1463 switch (create_infos->sType) {
1464 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: {
1465 const VkImageCompressionControlEXT* compression_infos =
1466 reinterpret_cast<const VkImageCompressionControlEXT*>(
1467 create_infos);
1468 image_compression = *compression_infos;
1469 image_compression.pNext = nullptr;
1470 usage_info_pNext = &image_compression;
1471 } break;
1472
1473 default:
1474 // Ignore all other info structs
1475 break;
1476 }
1477 }
1478 gralloc_usage_info.pNext = usage_info_pNext;
1479
1480 result = dispatch.GetSwapchainGrallocUsage3ANDROID(
1481 device, &gralloc_usage_info, &native_usage);
1482 ATRACE_END();
1483 if (result != VK_SUCCESS) {
1484 ALOGE("vkGetSwapchainGrallocUsage3ANDROID failed: %d", result);
1485 return VK_ERROR_SURFACE_LOST_KHR;
1486 }
1487 } else if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001488 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001489 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001490 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1491 device, create_info->imageFormat, create_info->imageUsage,
1492 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001493 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001494 if (result != VK_SUCCESS) {
1495 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001496 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001497 }
John Reck97678d42022-08-23 11:24:51 -04001498 native_usage =
Trevor David Black2cc44682022-03-09 00:31:38 +00001499 convertGralloc1ToBufferUsage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001500 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001501 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
John Reck97678d42022-08-23 11:24:51 -04001502 int32_t legacy_usage = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -08001503 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001504 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001505 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001506 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001507 if (result != VK_SUCCESS) {
1508 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001509 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001510 }
John Reck97678d42022-08-23 11:24:51 -04001511 native_usage = static_cast<uint64_t>(legacy_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001512 }
John Reck97678d42022-08-23 11:24:51 -04001513 native_usage |= surface.consumer_usage;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001514
1515 bool createProtectedSwapchain = false;
1516 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1517 createProtectedSwapchain = true;
1518 native_usage |= BufferUsage::PROTECTED;
1519 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001520 err = native_window_set_usage(window, native_usage);
1521 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001522 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001523 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001524 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001525
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001526 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001527 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1528 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001529 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1530 strerror(-err), err);
1531 return VK_ERROR_SURFACE_LOST_KHR;
1532 }
1533
Jesse Halld7b994a2015-09-07 14:17:37 -07001534 // -- Allocate our Swapchain object --
1535 // After this point, we must deallocate the swapchain on error.
1536
Jesse Hall1f91d392015-12-11 16:28:44 -08001537 void* mem = allocator->pfnAllocation(allocator->pUserData,
1538 sizeof(Swapchain), alignof(Swapchain),
1539 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001540 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001541 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001542 Swapchain* swapchain = new (mem)
1543 Swapchain(surface, num_images, create_info->presentMode,
1544 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001545 // -- Dequeue all buffers and create a VkImage for each --
1546 // Any failures during or after this must cancel the dequeued buffers.
1547
Chris Forbesb56287a2017-01-12 14:28:58 +13001548 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1549#pragma clang diagnostic push
1550#pragma clang diagnostic ignored "-Wold-style-cast"
1551 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1552#pragma clang diagnostic pop
Trevor David Black2cc44682022-03-09 00:31:38 +00001553 .pNext = usage_info_pNext,
Chris Forbesb56287a2017-01-12 14:28:58 +13001554 .usage = swapchain_image_usage,
1555 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001556 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001557#pragma clang diagnostic push
1558#pragma clang diagnostic ignored "-Wold-style-cast"
1559 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1560#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001561 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001562 };
1563 VkImageCreateInfo image_create = {
1564 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1565 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001566 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001567 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001568 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001569 .extent = {0, 0, 1},
1570 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001571 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001572 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001573 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001574 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001575 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001576 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001577 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1578 };
1579
Jesse Halld7b994a2015-09-07 14:17:37 -07001580 for (uint32_t i = 0; i < num_images; i++) {
1581 Swapchain::Image& img = swapchain->images[i];
1582
1583 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001584 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1585 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001586 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001587 switch (-err) {
1588 case ENOMEM:
1589 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1590 break;
1591 default:
1592 result = VK_ERROR_SURFACE_LOST_KHR;
1593 break;
1594 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001595 break;
1596 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001597 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001598 img.dequeued = true;
1599
1600 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001601 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1602 static_cast<uint32_t>(img.buffer->height),
1603 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001604 image_native_buffer.handle = img.buffer->handle;
1605 image_native_buffer.stride = img.buffer->stride;
1606 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001607 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001608 android_convertGralloc0To1Usage(int(img.buffer->usage),
1609 &image_native_buffer.usage2.producer,
1610 &image_native_buffer.usage2.consumer);
Trevor David Black2cc44682022-03-09 00:31:38 +00001611 image_native_buffer.usage3 = img.buffer->usage;
Jesse Halld7b994a2015-09-07 14:17:37 -07001612
Yiwei Zhang533cea92019-06-03 18:43:24 -07001613 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001614 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001615 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001616 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001617 if (result != VK_SUCCESS) {
1618 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1619 break;
1620 }
1621 }
1622
1623 // -- Cancel all buffers, returning them to the queue --
1624 // If an error occurred before, also destroy the VkImage and release the
1625 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001626 for (uint32_t i = 0; i < num_images; i++) {
1627 Swapchain::Image& img = swapchain->images[i];
1628 if (img.dequeued) {
1629 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001630 window->cancelBuffer(window, img.buffer.get(),
1631 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001632 img.dequeue_fence = -1;
1633 img.dequeued = false;
1634 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001635 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001636 }
1637
1638 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001639 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1640 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001641 return result;
1642 }
1643
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001644 if (transform_hint != swapchain->pre_transform) {
1645 // Log that the app is not doing pre-rotation.
1646 android::GraphicsEnv::getInstance().setTargetStats(
1647 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1648 }
1649
Jesse Halldc225072016-05-30 22:40:14 -07001650 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1651 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001652 return VK_SUCCESS;
1653}
1654
Jesse Halle1b12782015-11-30 11:27:32 -08001655VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001656void DestroySwapchainKHR(VkDevice device,
1657 VkSwapchainKHR swapchain_handle,
1658 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001659 ATRACE_CALL();
1660
Yiwei Zhang533cea92019-06-03 18:43:24 -07001661 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001662}
1663
Jesse Halle1b12782015-11-30 11:27:32 -08001664VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001665VkResult GetSwapchainImagesKHR(VkDevice,
1666 VkSwapchainKHR swapchain_handle,
1667 uint32_t* count,
1668 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001669 ATRACE_CALL();
1670
Jesse Halld7b994a2015-09-07 14:17:37 -07001671 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001672 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1673 "getting images for non-active swapchain 0x%" PRIx64
1674 "; only dequeued image handles are valid",
1675 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001676 VkResult result = VK_SUCCESS;
1677 if (images) {
1678 uint32_t n = swapchain.num_images;
1679 if (*count < swapchain.num_images) {
1680 n = *count;
1681 result = VK_INCOMPLETE;
1682 }
1683 for (uint32_t i = 0; i < n; i++)
1684 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001685 *count = n;
1686 } else {
1687 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001688 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001689 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001690}
1691
Jesse Halle1b12782015-11-30 11:27:32 -08001692VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001693VkResult AcquireNextImageKHR(VkDevice device,
1694 VkSwapchainKHR swapchain_handle,
1695 uint64_t timeout,
1696 VkSemaphore semaphore,
1697 VkFence vk_fence,
1698 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001699 ATRACE_CALL();
1700
Jesse Halld7b994a2015-09-07 14:17:37 -07001701 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001702 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001703 VkResult result;
1704 int err;
1705
Jesse Halldc225072016-05-30 22:40:14 -07001706 if (swapchain.surface.swapchain_handle != swapchain_handle)
1707 return VK_ERROR_OUT_OF_DATE_KHR;
1708
Chris Forbesc88409c2017-03-30 19:47:37 +13001709 if (swapchain.shared) {
1710 // In shared mode, we keep the buffer dequeued all the time, so we don't
1711 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1712 // the semaphore and fence passed to us will be signalled.
1713 *image_index = 0;
1714 result = GetData(device).driver.AcquireImageANDROID(
1715 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1716 return result;
1717 }
1718
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001719 const nsecs_t acquire_next_image_timeout =
1720 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1721 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1722 // Cache the timeout to avoid the duplicate binder cost.
1723 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1724 acquire_next_image_timeout);
1725 if (err != android::OK) {
1726 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1727 strerror(-err), err);
1728 return VK_ERROR_SURFACE_LOST_KHR;
1729 }
1730 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1731 }
1732
Jesse Halld7b994a2015-09-07 14:17:37 -07001733 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001734 int fence_fd;
1735 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001736 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001737 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1738 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1739 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001740 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001741 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001742 }
1743
1744 uint32_t idx;
1745 for (idx = 0; idx < swapchain.num_images; idx++) {
1746 if (swapchain.images[idx].buffer.get() == buffer) {
1747 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001748 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001749 break;
1750 }
1751 }
1752 if (idx == swapchain.num_images) {
1753 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001754 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001755 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001756 }
1757
1758 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001759 if (fence_fd != -1) {
1760 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001761 if (fence_clone == -1) {
1762 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1763 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001764 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001765 }
1766 }
1767
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001768 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001769 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001770 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001771 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1772 // even if the call fails. We could close it ourselves on failure, but
1773 // that would create a race condition if the driver closes it on a
1774 // failure path: some other thread might create an fd with the same
1775 // number between the time the driver closes it and the time we close
1776 // it. We must assume one of: the driver *always* closes it even on
1777 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001778 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001779 swapchain.images[idx].dequeued = false;
1780 swapchain.images[idx].dequeue_fence = -1;
1781 return result;
1782 }
1783
1784 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001785 return VK_SUCCESS;
1786}
1787
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001788VKAPI_ATTR
1789VkResult AcquireNextImage2KHR(VkDevice device,
1790 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1791 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001792 ATRACE_CALL();
1793
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001794 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1795 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1796 pAcquireInfo->fence, pImageIndex);
1797}
1798
Jesse Halldc225072016-05-30 22:40:14 -07001799static VkResult WorstPresentResult(VkResult a, VkResult b) {
1800 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1801 // (in spec version 1.0.14).
1802 static const VkResult kWorstToBest[] = {
1803 VK_ERROR_DEVICE_LOST,
1804 VK_ERROR_SURFACE_LOST_KHR,
1805 VK_ERROR_OUT_OF_DATE_KHR,
1806 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1807 VK_ERROR_OUT_OF_HOST_MEMORY,
1808 VK_SUBOPTIMAL_KHR,
1809 };
1810 for (auto result : kWorstToBest) {
1811 if (a == result || b == result)
1812 return result;
1813 }
1814 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1815 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1816 return a != VK_SUCCESS ? a : b;
1817}
1818
Jesse Halle1b12782015-11-30 11:27:32 -08001819VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001820VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001821 ATRACE_CALL();
1822
Jesse Halld7b994a2015-09-07 14:17:37 -07001823 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1824 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1825 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001826
Jesse Halldc225072016-05-30 22:40:14 -07001827 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001828 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001829 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001830
Ian Elliottcb351132016-12-13 10:30:40 -07001831 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001832 const VkPresentRegionsKHR* present_regions = nullptr;
1833 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001834 const VkPresentRegionsKHR* next =
1835 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1836 while (next) {
1837 switch (next->sType) {
1838 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1839 present_regions = next;
1840 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001841 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001842 present_times =
1843 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1844 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001845 default:
1846 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1847 next->sType);
1848 break;
1849 }
1850 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1851 }
1852 ALOGV_IF(
1853 present_regions &&
1854 present_regions->swapchainCount != present_info->swapchainCount,
1855 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001856 ALOGV_IF(present_times &&
1857 present_times->swapchainCount != present_info->swapchainCount,
1858 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1859 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001860 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001861 (present_regions) ? present_regions->pRegions : nullptr;
1862 const VkPresentTimeGOOGLE* times =
1863 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001864 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001865 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001866 uint32_t nrects = 0;
1867
Jesse Halld7b994a2015-09-07 14:17:37 -07001868 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1869 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001870 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001871 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001872 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001873 const VkPresentRegionKHR* region =
1874 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001875 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001876 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001877 VkResult result;
1878 int err;
1879
Jesse Halld7b994a2015-09-07 14:17:37 -07001880 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001881 result = dispatch.QueueSignalReleaseImageANDROID(
1882 queue, present_info->waitSemaphoreCount,
1883 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001884 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001885 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001886 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001887 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001888 if (img.release_fence >= 0)
1889 close(img.release_fence);
1890 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001891
Jesse Halldc225072016-05-30 22:40:14 -07001892 if (swapchain.surface.swapchain_handle ==
1893 present_info->pSwapchains[sc]) {
1894 ANativeWindow* window = swapchain.surface.window.get();
1895 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001896 if (region) {
1897 // Process the incremental-present hint for this swapchain:
1898 uint32_t rcount = region->rectangleCount;
1899 if (rcount > nrects) {
1900 android_native_rect_t* new_rects =
1901 static_cast<android_native_rect_t*>(
1902 allocator->pfnReallocation(
1903 allocator->pUserData, rects,
1904 sizeof(android_native_rect_t) * rcount,
1905 alignof(android_native_rect_t),
1906 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1907 if (new_rects) {
1908 rects = new_rects;
1909 nrects = rcount;
1910 } else {
1911 rcount = 0; // Ignore the hint for this swapchain
1912 }
1913 }
1914 for (uint32_t r = 0; r < rcount; ++r) {
1915 if (region->pRectangles[r].layer > 0) {
1916 ALOGV(
1917 "vkQueuePresentKHR ignoring invalid layer "
1918 "(%u); using layer 0 instead",
1919 region->pRectangles[r].layer);
1920 }
1921 int x = region->pRectangles[r].offset.x;
1922 int y = region->pRectangles[r].offset.y;
1923 int width = static_cast<int>(
1924 region->pRectangles[r].extent.width);
1925 int height = static_cast<int>(
1926 region->pRectangles[r].extent.height);
1927 android_native_rect_t* cur_rect = &rects[r];
1928 cur_rect->left = x;
1929 cur_rect->top = y + height;
1930 cur_rect->right = x + width;
1931 cur_rect->bottom = y;
1932 }
1933 native_window_set_surface_damage(window, rects, rcount);
1934 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001935 if (time) {
1936 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001937 ALOGV(
1938 "Calling "
1939 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001940 native_window_enable_frame_timestamps(window, true);
1941 swapchain.frame_timestamps_enabled = true;
1942 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001943
1944 // Record the nativeFrameId so it can be later correlated to
1945 // this present.
1946 uint64_t nativeFrameId = 0;
1947 err = native_window_get_next_frame_id(
1948 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001949 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001950 ALOGE("Failed to get next native frame ID.");
1951 }
1952
1953 // Add a new timing record with the user's presentID and
1954 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001955 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001956 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001957 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001958 }
1959 if (time->desiredPresentTime) {
1960 // Set the desiredPresentTime:
1961 ALOGV(
1962 "Calling "
1963 "native_window_set_buffers_timestamp(%" PRId64 ")",
1964 time->desiredPresentTime);
1965 native_window_set_buffers_timestamp(
1966 window,
1967 static_cast<int64_t>(time->desiredPresentTime));
1968 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001969 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001970
Jesse Halldc225072016-05-30 22:40:14 -07001971 err = window->queueBuffer(window, img.buffer.get(), fence);
1972 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001973 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001974 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1975 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001976 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001977 } else {
1978 if (img.dequeue_fence >= 0) {
1979 close(img.dequeue_fence);
1980 img.dequeue_fence = -1;
1981 }
1982 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001983 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001984
1985 // If the swapchain is in shared mode, immediately dequeue the
1986 // buffer so it can be presented again without an intervening
1987 // call to AcquireNextImageKHR. We expect to get the same buffer
1988 // back from every call to dequeueBuffer in this mode.
1989 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1990 ANativeWindowBuffer* buffer;
1991 int fence_fd;
1992 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001993 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001994 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1995 swapchain_result = WorstPresentResult(swapchain_result,
1996 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001997 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001998 ALOGE("got wrong image back for shared swapchain");
1999 swapchain_result = WorstPresentResult(swapchain_result,
2000 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002001 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13002002 img.dequeue_fence = fence_fd;
2003 img.dequeued = true;
2004 }
2005 }
Jesse Halldc225072016-05-30 22:40:14 -07002006 }
2007 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07002008 OrphanSwapchain(device, &swapchain);
2009 }
silence_dogood73597592019-05-23 16:57:37 -07002010 int window_transform_hint;
2011 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
2012 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002013 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07002014 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
2015 strerror(-err), err);
2016 swapchain_result = WorstPresentResult(
2017 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
2018 }
2019 if (swapchain.pre_transform != window_transform_hint) {
2020 swapchain_result =
2021 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
2022 }
Jesse Halldc225072016-05-30 22:40:14 -07002023 } else {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00002024 ReleaseSwapchainImage(device, swapchain.shared, nullptr, fence,
2025 img, true);
Jesse Halldc225072016-05-30 22:40:14 -07002026 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07002027 }
2028
Jesse Halla9e57032015-11-30 01:03:10 -08002029 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07002030 present_info->pResults[sc] = swapchain_result;
2031
2032 if (swapchain_result != final_result)
2033 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07002034 }
Ian Elliottcb351132016-12-13 10:30:40 -07002035 if (rects) {
2036 allocator->pfnFree(allocator->pUserData, rects);
2037 }
Jesse Halld7b994a2015-09-07 14:17:37 -07002038
2039 return final_result;
2040}
Jesse Hallb1352bc2015-09-04 16:12:33 -07002041
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002042VKAPI_ATTR
2043VkResult GetRefreshCycleDurationGOOGLE(
2044 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07002045 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002046 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002047 ATRACE_CALL();
2048
Ian Elliott62c48c92017-01-20 13:13:20 -07002049 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002050 VkResult result = VK_SUCCESS;
2051
Ian Elliott3568bfe2019-05-03 15:54:46 -06002052 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002053
2054 return result;
2055}
2056
2057VKAPI_ATTR
2058VkResult GetPastPresentationTimingGOOGLE(
2059 VkDevice,
2060 VkSwapchainKHR swapchain_handle,
2061 uint32_t* count,
2062 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002063 ATRACE_CALL();
2064
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002065 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002066 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2067 return VK_ERROR_OUT_OF_DATE_KHR;
2068 }
2069
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002070 ANativeWindow* window = swapchain.surface.window.get();
2071 VkResult result = VK_SUCCESS;
2072
2073 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07002074 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002075 native_window_enable_frame_timestamps(window, true);
2076 swapchain.frame_timestamps_enabled = true;
2077 }
2078
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002079 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07002080 // Get the latest ready timing count before copying, since the copied
2081 // timing info will be erased in copy_ready_timings function.
2082 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07002083 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002084 // Check the *count here against the recorded ready timing count, since
2085 // *count can be overwritten per spec describes.
2086 if (*count < n) {
2087 result = VK_INCOMPLETE;
2088 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002089 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07002090 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002091 }
2092
2093 return result;
2094}
2095
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002096VKAPI_ATTR
2097VkResult GetSwapchainStatusKHR(
2098 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13002099 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002100 ATRACE_CALL();
2101
Chris Forbes4e18ba82017-01-20 12:50:17 +13002102 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002103 VkResult result = VK_SUCCESS;
2104
Chris Forbes4e18ba82017-01-20 12:50:17 +13002105 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2106 return VK_ERROR_OUT_OF_DATE_KHR;
2107 }
2108
Yiwei Zhanga885c062019-10-24 12:07:57 -07002109 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002110
2111 return result;
2112}
2113
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002114VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002115 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002116 uint32_t swapchainCount,
2117 const VkSwapchainKHR* pSwapchains,
2118 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002119 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002120
2121 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
2122 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
2123 if (!swapchain)
2124 continue;
2125
2126 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
2127
2128 ANativeWindow* window = swapchain->surface.window.get();
2129
2130 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
2131 const android_smpte2086_metadata smpteMetdata = {
2132 {vulkanMetadata.displayPrimaryRed.x,
2133 vulkanMetadata.displayPrimaryRed.y},
2134 {vulkanMetadata.displayPrimaryGreen.x,
2135 vulkanMetadata.displayPrimaryGreen.y},
2136 {vulkanMetadata.displayPrimaryBlue.x,
2137 vulkanMetadata.displayPrimaryBlue.y},
2138 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
2139 vulkanMetadata.maxLuminance,
2140 vulkanMetadata.minLuminance};
2141 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
2142
2143 const android_cta861_3_metadata cta8613Metadata = {
2144 vulkanMetadata.maxContentLightLevel,
2145 vulkanMetadata.maxFrameAverageLightLevel};
2146 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
2147 }
2148
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002149 return;
2150}
2151
Yiwei Zhang0f475222019-04-11 19:38:00 -07002152static void InterceptBindImageMemory2(
2153 uint32_t bind_info_count,
2154 const VkBindImageMemoryInfo* bind_infos,
2155 std::vector<VkNativeBufferANDROID>* out_native_buffers,
2156 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
2157 out_native_buffers->clear();
2158 out_bind_infos->clear();
2159
2160 if (!bind_info_count)
2161 return;
2162
2163 std::unordered_set<uint32_t> intercepted_indexes;
2164
2165 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2166 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2167 bind_infos[idx].pNext);
2168 while (info &&
2169 info->sType !=
2170 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
2171 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2172 info->pNext);
2173 }
2174
2175 if (!info)
2176 continue;
2177
2178 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
2179 "swapchain handle must not be NULL");
2180 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
2181 ALOG_ASSERT(
2182 info->imageIndex < swapchain->num_images,
2183 "imageIndex must be less than the number of images in swapchain");
2184
2185 ANativeWindowBuffer* buffer =
2186 swapchain->images[info->imageIndex].buffer.get();
2187 VkNativeBufferANDROID native_buffer = {
2188#pragma clang diagnostic push
2189#pragma clang diagnostic ignored "-Wold-style-cast"
2190 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2191#pragma clang diagnostic pop
2192 .pNext = bind_infos[idx].pNext,
2193 .handle = buffer->handle,
2194 .stride = buffer->stride,
2195 .format = buffer->format,
2196 .usage = int(buffer->usage),
2197 };
2198 // Reserve enough space to avoid letting re-allocation invalidate the
2199 // addresses of the elements inside.
2200 out_native_buffers->reserve(bind_info_count);
2201 out_native_buffers->emplace_back(native_buffer);
2202
2203 // Reserve the space now since we know how much is needed now.
2204 out_bind_infos->reserve(bind_info_count);
2205 out_bind_infos->emplace_back(bind_infos[idx]);
2206 out_bind_infos->back().pNext = &out_native_buffers->back();
2207
2208 intercepted_indexes.insert(idx);
2209 }
2210
2211 if (intercepted_indexes.empty())
2212 return;
2213
2214 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2215 if (intercepted_indexes.count(idx))
2216 continue;
2217 out_bind_infos->emplace_back(bind_infos[idx]);
2218 }
2219}
2220
Yiwei Zhang23143102019-04-10 18:24:05 -07002221VKAPI_ATTR
2222VkResult BindImageMemory2(VkDevice device,
2223 uint32_t bindInfoCount,
2224 const VkBindImageMemoryInfo* pBindInfos) {
2225 ATRACE_CALL();
2226
Yiwei Zhang0f475222019-04-11 19:38:00 -07002227 // out_native_buffers is for maintaining the lifecycle of the constructed
2228 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2229 std::vector<VkNativeBufferANDROID> out_native_buffers;
2230 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2231 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2232 &out_bind_infos);
2233 return GetData(device).driver.BindImageMemory2(
2234 device, bindInfoCount,
2235 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002236}
2237
2238VKAPI_ATTR
2239VkResult BindImageMemory2KHR(VkDevice device,
2240 uint32_t bindInfoCount,
2241 const VkBindImageMemoryInfo* pBindInfos) {
2242 ATRACE_CALL();
2243
Yiwei Zhang0f475222019-04-11 19:38:00 -07002244 std::vector<VkNativeBufferANDROID> out_native_buffers;
2245 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2246 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2247 &out_bind_infos);
2248 return GetData(device).driver.BindImageMemory2KHR(
2249 device, bindInfoCount,
2250 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002251}
2252
Chia-I Wu62262232016-03-26 07:06:44 +08002253} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002254} // namespace vulkan