blob: 570c01eb4b0c5e90979bb37031ff011f9f547497 [file] [log] [blame]
Jesse Hallb1352bc2015-09-04 16:12:33 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Trevor David Blackf499b5a2023-07-14 17:30:41 +000019#include <aidl/android/hardware/graphics/common/PixelFormat.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070020#include <android/hardware/graphics/common/1.0/types.h>
Jesse Hall79927812017-03-23 11:03:23 -070021#include <grallocusage/GrallocUsageConversion.h>
Yiwei Zhang69395cd2019-07-03 16:55:39 -070022#include <graphicsenv/GraphicsEnv.h>
John Reck97678d42022-08-23 11:24:51 -040023#include <hardware/gralloc.h>
24#include <hardware/gralloc1.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070025#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070026#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070027#include <system/window.h>
28#include <ui/BufferQueueDefs.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080029#include <utils/StrongPointer.h>
Yiwei Zhang705c2e62019-12-18 23:12:43 -080030#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080031#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070032
33#include <algorithm>
34#include <unordered_set>
35#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070036
Chia-I Wu4a6a9162016-03-26 07:17:34 +080037#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070038
Trevor David Blackf499b5a2023-07-14 17:30:41 +000039using PixelFormat = aidl::android::hardware::graphics::common::PixelFormat;
Daniel Kochf25f5bb2017-10-05 00:26:58 -040040using android::hardware::graphics::common::V1_0::BufferUsage;
41
Chia-I Wu62262232016-03-26 07:06:44 +080042namespace vulkan {
43namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070044
Jesse Halld7b994a2015-09-07 14:17:37 -070045namespace {
46
John Reck97678d42022-08-23 11:24:51 -040047static uint64_t convertGralloc1ToBufferUsage(uint64_t producerUsage,
48 uint64_t consumerUsage) {
49 static_assert(uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) ==
50 uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN),
51 "expected ConsumerUsage and ProducerUsage CPU_READ_OFTEN "
52 "bits to match");
53 uint64_t merged = producerUsage | consumerUsage;
54 if ((merged & (GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN)) ==
55 GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) {
56 merged &= ~uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN);
57 merged |= BufferUsage::CPU_READ_OFTEN;
58 }
59 if ((merged & (GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) ==
60 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) {
61 merged &= ~uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN);
62 merged |= BufferUsage::CPU_WRITE_OFTEN;
63 }
64 return merged;
65}
66
Jesse Hall55bc0972016-02-23 16:43:29 -080067const VkSurfaceTransformFlagsKHR kSupportedTransforms =
68 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
69 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
70 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
71 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070072 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
73 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
74 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
75 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080076 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
77
78VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
79 // Native and Vulkan transforms are isomorphic, but are represented
80 // differently. Vulkan transforms are built up of an optional horizontal
81 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
82 // transforms are built up from a horizontal flip, vertical flip, and
83 // 90-degree rotation, all optional but always in that order.
84
Jesse Hall55bc0972016-02-23 16:43:29 -080085 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070086 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080087 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070088 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
89 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
90 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
91 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
92 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080093 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070094 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080095 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070096 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
97 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
98 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
99 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
100 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -0800101 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
102 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
103 default:
104 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
105 }
106}
107
Yiwei Zhang70a21962019-05-31 17:26:52 -0700108int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
109 switch (transform) {
110 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
111 return NATIVE_WINDOW_TRANSFORM_ROT_90;
112 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
113 return NATIVE_WINDOW_TRANSFORM_ROT_180;
114 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
115 return NATIVE_WINDOW_TRANSFORM_ROT_270;
116 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
117 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
118 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
119 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
120 NATIVE_WINDOW_TRANSFORM_ROT_90;
121 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
122 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
123 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
124 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
125 NATIVE_WINDOW_TRANSFORM_ROT_90;
126 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
127 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
128 default:
129 return 0;
130 }
131}
132
Jesse Hall178b6962016-02-24 15:39:50 -0800133int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
134 switch (transform) {
135 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
136 return NATIVE_WINDOW_TRANSFORM_ROT_270;
137 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
138 return NATIVE_WINDOW_TRANSFORM_ROT_180;
139 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
140 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700141 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
142 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
143 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
144 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
145 NATIVE_WINDOW_TRANSFORM_ROT_90;
146 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
147 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
148 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
149 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
150 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800151 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
152 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
153 default:
154 return 0;
155 }
156}
157
Ian Elliott8a977262017-01-19 09:05:58 -0700158class TimingInfo {
159 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800160 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700161 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800162 native_frame_id_(nativeFrameId) {}
163 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700164 return (timestamp_desired_present_time_ !=
165 NATIVE_WINDOW_TIMESTAMP_PENDING &&
166 timestamp_actual_present_time_ !=
167 NATIVE_WINDOW_TIMESTAMP_PENDING &&
168 timestamp_render_complete_time_ !=
169 NATIVE_WINDOW_TIMESTAMP_PENDING &&
170 timestamp_composition_latch_time_ !=
171 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700172 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700173 void calculate(int64_t rdur) {
174 bool anyTimestampInvalid =
175 (timestamp_actual_present_time_ ==
176 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
177 (timestamp_render_complete_time_ ==
178 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
179 (timestamp_composition_latch_time_ ==
180 NATIVE_WINDOW_TIMESTAMP_INVALID);
181 if (anyTimestampInvalid) {
182 ALOGE("Unexpectedly received invalid timestamp.");
183 vals_.actualPresentTime = 0;
184 vals_.earliestPresentTime = 0;
185 vals_.presentMargin = 0;
186 return;
187 }
188
189 vals_.actualPresentTime =
190 static_cast<uint64_t>(timestamp_actual_present_time_);
191 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700192 timestamp_render_complete_time_);
193 // Calculate vals_.earliestPresentTime, and potentially adjust
194 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
195 // is vals_.actualPresentTime. If we can subtract rdur (the duration
196 // of a refresh cycle) from vals_.earliestPresentTime (and also from
197 // vals_.presentMargin) and still leave a positive margin, then we can
198 // report to the application that it could have presented earlier than
199 // it did (per the extension specification). If for some reason, we
200 // can do this subtraction repeatedly, we do, since
201 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700202 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700203 while ((margin > rdur) &&
204 ((early_time - rdur) > timestamp_composition_latch_time_)) {
205 early_time -= rdur;
206 margin -= rdur;
207 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700208 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
209 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700210 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800211 void get_values(VkPastPresentationTimingGOOGLE* values) const {
212 *values = vals_;
213 }
Ian Elliott8a977262017-01-19 09:05:58 -0700214
215 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800216 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700217
Brian Anderson1049d1d2016-12-16 17:25:57 -0800218 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700219 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
220 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
221 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
222 int64_t timestamp_composition_latch_time_
223 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700224};
225
Jesse Hall1356b0d2015-11-23 17:24:58 -0800226struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800227 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700228 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700229 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800230};
231
232VkSurfaceKHR HandleFromSurface(Surface* surface) {
233 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
234}
235
236Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800237 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800238}
239
Ian Elliott8a977262017-01-19 09:05:58 -0700240// Maximum number of TimingInfo structs to keep per swapchain:
241enum { MAX_TIMING_INFOS = 10 };
242// Minimum number of frames to look for in the past (so we don't cause
243// syncronous requests to Surface Flinger):
244enum { MIN_NUM_FRAMES_AGO = 5 };
245
Jesse Hall1356b0d2015-11-23 17:24:58 -0800246struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700247 Swapchain(Surface& surface_,
248 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700249 VkPresentModeKHR present_mode,
250 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700251 : surface(surface_),
252 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700253 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700254 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300255 frame_timestamps_enabled(false),
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800256 acquire_next_image_timeout(-1),
Chris Forbesf8835642017-03-30 19:31:40 +1300257 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800258 present_mode ==
259 VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700260 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700261 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700262 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700263 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700264 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600265 uint64_t get_refresh_duration()
266 {
267 ANativeWindow* window = surface.window.get();
268 native_window_get_refresh_cycle_duration(
269 window,
270 &refresh_duration);
271 return static_cast<uint64_t>(refresh_duration);
272
273 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800274
275 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700276 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700277 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700278 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700279 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700280 int64_t refresh_duration;
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800281 nsecs_t acquire_next_image_timeout;
Chris Forbesf8835642017-03-30 19:31:40 +1300282 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700283
284 struct Image {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700285 Image()
286 : image(VK_NULL_HANDLE),
287 dequeue_fence(-1),
288 release_fence(-1),
289 dequeued(false) {}
Jesse Halld7b994a2015-09-07 14:17:37 -0700290 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800291 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700292 // The fence is only valid when the buffer is dequeued, and should be
293 // -1 any other time. When valid, we own the fd, and must ensure it is
294 // closed: either by closing it explicitly when queueing the buffer,
295 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
296 int dequeue_fence;
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700297 // This fence is a dup of the sync fd returned from the driver via
298 // vkQueueSignalReleaseImageANDROID upon vkQueuePresentKHR. We must
299 // ensure it is closed upon re-presenting or releasing the image.
300 int release_fence;
Jesse Halld7b994a2015-09-07 14:17:37 -0700301 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800302 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700303
Yiwei Zhang5e862202019-06-21 14:59:16 -0700304 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700305};
306
307VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
308 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
309}
310
311Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800312 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700313}
314
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700315static bool IsFencePending(int fd) {
316 if (fd < 0)
317 return false;
318
319 errno = 0;
320 return sync_wait(fd, 0 /* timeout */) == -1 && errno == ETIME;
321}
322
Jesse Halldc225072016-05-30 22:40:14 -0700323void ReleaseSwapchainImage(VkDevice device,
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000324 bool shared_present,
Jesse Halldc225072016-05-30 22:40:14 -0700325 ANativeWindow* window,
326 int release_fence,
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700327 Swapchain::Image& image,
328 bool defer_if_pending) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700329 ATRACE_CALL();
330
Jesse Halldc225072016-05-30 22:40:14 -0700331 ALOG_ASSERT(release_fence == -1 || image.dequeued,
332 "ReleaseSwapchainImage: can't provide a release fence for "
333 "non-dequeued images");
334
335 if (image.dequeued) {
336 if (release_fence >= 0) {
337 // We get here from vkQueuePresentKHR. The application is
338 // responsible for creating an execution dependency chain from
339 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
340 // (release_fence), so we can drop the dequeue_fence here.
341 if (image.dequeue_fence >= 0)
342 close(image.dequeue_fence);
343 } else {
344 // We get here during swapchain destruction, or various serious
345 // error cases e.g. when we can't create the release_fence during
346 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
347 // have already signalled, since the swapchain images are supposed
348 // to be idle before the swapchain is destroyed. In error cases,
349 // there may be rendering in flight to the image, but since we
350 // weren't able to create a release_fence, waiting for the
351 // dequeue_fence is about the best we can do.
352 release_fence = image.dequeue_fence;
353 }
354 image.dequeue_fence = -1;
355
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000356 // It's invalid to call cancelBuffer on a shared buffer
357 if (window && !shared_present) {
Jesse Halldc225072016-05-30 22:40:14 -0700358 window->cancelBuffer(window, image.buffer.get(), release_fence);
359 } else {
360 if (release_fence >= 0) {
361 sync_wait(release_fence, -1 /* forever */);
362 close(release_fence);
363 }
364 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700365 release_fence = -1;
Jesse Halldc225072016-05-30 22:40:14 -0700366 image.dequeued = false;
367 }
368
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700369 if (defer_if_pending && IsFencePending(image.release_fence))
370 return;
371
372 if (image.release_fence >= 0) {
373 close(image.release_fence);
374 image.release_fence = -1;
375 }
376
Jesse Halldc225072016-05-30 22:40:14 -0700377 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700378 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700379 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700380 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700381 image.image = VK_NULL_HANDLE;
382 }
383
384 image.buffer.clear();
385}
386
387void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
388 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
389 return;
Jesse Halldc225072016-05-30 22:40:14 -0700390 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000391 if (!swapchain->images[i].dequeued) {
392 ReleaseSwapchainImage(device, swapchain->shared, nullptr, -1,
393 swapchain->images[i], true);
394 }
Jesse Halldc225072016-05-30 22:40:14 -0700395 }
396 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700397 swapchain->timing.clear();
398}
399
400uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800401 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
402 return 0;
403 }
Ian Elliott8a977262017-01-19 09:05:58 -0700404
Brian Anderson1049d1d2016-12-16 17:25:57 -0800405 uint32_t num_ready = 0;
406 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
407 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700408 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800409 if (ti.ready()) {
410 // This TimingInfo is ready to be reported to the user. Add it
411 // to the num_ready.
412 num_ready++;
413 continue;
414 }
415 // This TimingInfo is not yet ready to be reported to the user,
416 // and so we should look for any available timestamps that
417 // might make it ready.
418 int64_t desired_present_time = 0;
419 int64_t render_complete_time = 0;
420 int64_t composition_latch_time = 0;
421 int64_t actual_present_time = 0;
422 // Obtain timestamps:
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800423 int err = native_window_get_frame_timestamps(
Brian Anderson1049d1d2016-12-16 17:25:57 -0800424 swapchain.surface.window.get(), ti.native_frame_id_,
425 &desired_present_time, &render_complete_time,
426 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700427 nullptr, //&first_composition_start_time,
428 nullptr, //&last_composition_start_time,
429 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800430 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700431 nullptr, //&dequeue_ready_time,
432 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800433
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800434 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800435 continue;
436 }
437
438 // Record the timestamp(s) we received, and then see if this TimingInfo
439 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700440 ti.timestamp_desired_present_time_ = desired_present_time;
441 ti.timestamp_actual_present_time_ = actual_present_time;
442 ti.timestamp_render_complete_time_ = render_complete_time;
443 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800444
445 if (ti.ready()) {
446 // The TimingInfo has received enough timestamps, and should now
447 // use those timestamps to calculate the info that should be
448 // reported to the user:
449 ti.calculate(swapchain.refresh_duration);
450 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700451 }
452 }
453 return num_ready;
454}
455
Ian Elliott8a977262017-01-19 09:05:58 -0700456void copy_ready_timings(Swapchain& swapchain,
457 uint32_t* count,
458 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800459 if (swapchain.timing.empty()) {
460 *count = 0;
461 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700462 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800463
464 size_t last_ready = swapchain.timing.size() - 1;
465 while (!swapchain.timing[last_ready].ready()) {
466 if (last_ready == 0) {
467 *count = 0;
468 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700469 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800470 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700471 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800472
473 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700474 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800475 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
476 const TimingInfo& ti = swapchain.timing[i];
477 if (ti.ready()) {
478 ti.get_values(&timings[num_copied]);
479 num_copied++;
480 }
481 num_to_remove++;
482 }
483
484 // Discard old frames that aren't ready if newer frames are ready.
485 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700486 swapchain.timing.erase(swapchain.timing.begin(),
487 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800488
Ian Elliott8a977262017-01-19 09:05:58 -0700489 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700490}
491
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000492PixelFormat GetNativePixelFormat(VkFormat format) {
493 PixelFormat native_format = PixelFormat::RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700494 switch (format) {
495 case VK_FORMAT_R8G8B8A8_UNORM:
496 case VK_FORMAT_R8G8B8A8_SRGB:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000497 native_format = PixelFormat::RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700498 break;
499 case VK_FORMAT_R5G6B5_UNORM_PACK16:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000500 native_format = PixelFormat::RGB_565;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700501 break;
502 case VK_FORMAT_R16G16B16A16_SFLOAT:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000503 native_format = PixelFormat::RGBA_FP16;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700504 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800505 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000506 native_format = PixelFormat::RGBA_1010102;
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500507 break;
508 case VK_FORMAT_R8_UNORM:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000509 native_format = PixelFormat::R_8;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700510 break;
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000511 case VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16:
Trevor David Blackf499b5a2023-07-14 17:30:41 +0000512 native_format = PixelFormat::RGBA_10101010;
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000513 break;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700514 default:
515 ALOGV("unsupported swapchain format %d", format);
516 break;
517 }
518 return native_format;
519}
520
Sally Qi03a1a752023-07-17 12:52:05 +0800521android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace,
522 PixelFormat pixelFormat) {
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700523 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:
Sally Qi03a1a752023-07-17 12:52:05 +0800541 if (pixelFormat == PixelFormat::RGBA_FP16) {
542 return static_cast<android_dataspace>(
543 HAL_DATASPACE_STANDARD_BT2020 |
544 HAL_DATASPACE_TRANSFER_LINEAR |
545 HAL_DATASPACE_RANGE_EXTENDED);
546 } else {
547 return HAL_DATASPACE_BT2020_LINEAR;
548 }
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600549 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700550 return static_cast<android_dataspace>(
551 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
552 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600553 case VK_COLOR_SPACE_DOLBYVISION_EXT:
554 return static_cast<android_dataspace>(
555 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
556 HAL_DATASPACE_RANGE_FULL);
557 case VK_COLOR_SPACE_HDR10_HLG_EXT:
Sally Qi03a1a752023-07-17 12:52:05 +0800558 return static_cast<android_dataspace>(HAL_DATASPACE_BT2020_HLG);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700559 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
560 return static_cast<android_dataspace>(
561 HAL_DATASPACE_STANDARD_ADOBE_RGB |
562 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
563 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
564 return HAL_DATASPACE_ADOBE_RGB;
565
566 // Pass through is intended to allow app to provide data that is passed
567 // to the display system without modification.
568 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
569 return HAL_DATASPACE_ARBITRARY;
570
571 default:
572 // This indicates that we don't know about the
573 // dataspace specified and we should indicate that
574 // it's unsupported
575 return HAL_DATASPACE_UNKNOWN;
576 }
577}
578
Jesse Halld7b994a2015-09-07 14:17:37 -0700579} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700580
Jesse Halle1b12782015-11-30 11:27:32 -0800581VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800582VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800583 VkInstance instance,
584 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
585 const VkAllocationCallbacks* allocator,
586 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800587 ATRACE_CALL();
588
Jesse Hall1f91d392015-12-11 16:28:44 -0800589 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800590 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800591 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
592 alignof(Surface),
593 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800594 if (!mem)
595 return VK_ERROR_OUT_OF_HOST_MEMORY;
596 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700597
Chia-I Wue8e689f2016-04-18 08:21:31 +0800598 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700599 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700600 int err = native_window_get_consumer_usage(surface->window.get(),
601 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800602 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700603 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
604 strerror(-err), err);
605 surface->~Surface();
606 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700607 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700608 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700609
Yiwei Zhang6435b322018-05-08 11:12:17 -0700610 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800611 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800612 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800613 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
614 err);
615 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800616 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700617 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800618 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700619
Jesse Hall1356b0d2015-11-23 17:24:58 -0800620 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700621 return VK_SUCCESS;
622}
623
Jesse Halle1b12782015-11-30 11:27:32 -0800624VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800625void DestroySurfaceKHR(VkInstance instance,
626 VkSurfaceKHR surface_handle,
627 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800628 ATRACE_CALL();
629
Jesse Hall1356b0d2015-11-23 17:24:58 -0800630 Surface* surface = SurfaceFromHandle(surface_handle);
631 if (!surface)
632 return;
633 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700634 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700635 "destroyed VkSurfaceKHR 0x%" PRIx64
636 " has active VkSwapchainKHR 0x%" PRIx64,
637 reinterpret_cast<uint64_t>(surface_handle),
638 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800639 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800640 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800641 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800642 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800643}
644
Jesse Halle1b12782015-11-30 11:27:32 -0800645VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800646VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
647 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000648 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800649 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000650 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800651 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800652}
653
Jesse Halle1b12782015-11-30 11:27:32 -0800654VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800655VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Ian Elliott1ce053f2022-03-16 09:49:53 -0600656 VkPhysicalDevice pdev,
Jesse Hallb00daad2015-11-29 19:46:20 -0800657 VkSurfaceKHR surface,
658 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800659 ATRACE_CALL();
660
Jesse Halld7b994a2015-09-07 14:17:37 -0700661 int err;
Jesse Halld7b994a2015-09-07 14:17:37 -0700662 int width, height;
Jesse Hall55bc0972016-02-23 16:43:29 -0800663 int transform_hint;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800664 int max_buffer_count;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600665 if (surface == VK_NULL_HANDLE) {
666 const InstanceData& instance_data = GetData(pdev);
667 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
668 bool surfaceless_enabled =
669 instance_data.hook_extensions.test(surfaceless);
670 if (!surfaceless_enabled) {
671 // It is an error to pass a surface==VK_NULL_HANDLE unless the
672 // VK_GOOGLE_surfaceless_query extension is enabled
673 return VK_ERROR_SURFACE_LOST_KHR;
674 }
675 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
676 // extension for this function is for
677 // VkSurfaceProtectedCapabilitiesKHR::supportsProtected. The following
678 // four values cannot be known without a surface. Default values will
679 // be supplied anyway, but cannot be relied upon.
Ian Elliottb4576372022-09-08 15:13:39 -0600680 width = 0xFFFFFFFF;
681 height = 0xFFFFFFFF;
682 transform_hint = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
683 capabilities->minImageCount = 0xFFFFFFFF;
684 capabilities->maxImageCount = 0xFFFFFFFF;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600685 } else {
686 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
687
688 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
689 if (err != android::OK) {
690 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
691 strerror(-err), err);
692 return VK_ERROR_SURFACE_LOST_KHR;
693 }
694 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
695 if (err != android::OK) {
696 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
697 strerror(-err), err);
698 return VK_ERROR_SURFACE_LOST_KHR;
699 }
700
701 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
702 &transform_hint);
703 if (err != android::OK) {
704 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
705 strerror(-err), err);
706 return VK_ERROR_SURFACE_LOST_KHR;
707 }
708
709 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT,
710 &max_buffer_count);
711 if (err != android::OK) {
712 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
713 strerror(-err), err);
714 return VK_ERROR_SURFACE_LOST_KHR;
715 }
Ian Elliottb4576372022-09-08 15:13:39 -0600716 capabilities->minImageCount = std::min(max_buffer_count, 3);
717 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800718 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700719
Jesse Hallfe2662d2016-02-09 13:26:59 -0800720 capabilities->currentExtent =
721 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
722
Yiwei Zhang70a21962019-05-31 17:26:52 -0700723 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800724 capabilities->minImageExtent = VkExtent2D{1, 1};
725 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700726
Trevor David Blackc00a1212022-11-14 21:13:14 +0000727 if (capabilities->maxImageExtent.height <
728 capabilities->currentExtent.height) {
729 capabilities->maxImageExtent.height =
730 capabilities->currentExtent.height;
731 }
732
733 if (capabilities->maxImageExtent.width <
734 capabilities->currentExtent.width) {
735 capabilities->maxImageExtent.width = capabilities->currentExtent.width;
736 }
737
Jesse Hallfe2662d2016-02-09 13:26:59 -0800738 capabilities->maxImageArrayLayers = 1;
739
Jesse Hall55bc0972016-02-23 16:43:29 -0800740 capabilities->supportedTransforms = kSupportedTransforms;
741 capabilities->currentTransform =
742 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700743
Jesse Hallfe2662d2016-02-09 13:26:59 -0800744 // On Android, window composition is a WindowManager property, not something
745 // associated with the bufferqueue. It can't be changed from here.
746 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700747
Jesse Hallb00daad2015-11-29 19:46:20 -0800748 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800749 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
750 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
751 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700752 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
753
Jesse Hallb1352bc2015-09-04 16:12:33 -0700754 return VK_SUCCESS;
755}
756
Jesse Halle1b12782015-11-30 11:27:32 -0800757VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700758VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
759 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800760 uint32_t* count,
761 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800762 ATRACE_CALL();
763
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700764 const InstanceData& instance_data = GetData(pdev);
765
Ian Elliott1ce053f2022-03-16 09:49:53 -0600766 uint64_t consumer_usage = 0;
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000767 bool colorspace_ext =
Ian Elliottf6df08e2022-03-16 21:27:49 -0600768 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600769 if (surface_handle == VK_NULL_HANDLE) {
770 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
771 bool surfaceless_enabled =
772 instance_data.hook_extensions.test(surfaceless);
773 if (!surfaceless_enabled) {
774 return VK_ERROR_SURFACE_LOST_KHR;
775 }
Chris Forbesa9e28de2022-10-20 09:05:48 +1300776 // Support for VK_GOOGLE_surfaceless_query.
Ian Elliott1ce053f2022-03-16 09:49:53 -0600777
778 // TODO(b/203826952): research proper value; temporarily use the
779 // values seen on Pixel
780 consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
781 } else {
782 Surface& surface = *SurfaceFromHandle(surface_handle);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600783 consumer_usage = surface.consumer_usage;
Ian Elliott6ba85d92022-02-18 16:44:58 -0700784 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700785
Charlie Lao324191f2019-12-13 11:03:16 -0800786 AHardwareBuffer_Desc desc = {};
787 desc.width = 1;
788 desc.height = 1;
789 desc.layers = 1;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600790 desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
Charlie Lao324191f2019-12-13 11:03:16 -0800791 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
792
793 // We must support R8G8B8A8
794 std::vector<VkSurfaceFormatKHR> all_formats = {
795 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700796 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700797 };
Charlie Lao324191f2019-12-13 11:03:16 -0800798
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000799 if (colorspace_ext) {
800 all_formats.emplace_back(VkSurfaceFormatKHR{
Jason Macnakca506332022-11-09 11:00:36 -0800801 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT});
802 all_formats.emplace_back(VkSurfaceFormatKHR{
803 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_PASS_THROUGH_EXT});
804 all_formats.emplace_back(VkSurfaceFormatKHR{
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000805 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_BT709_LINEAR_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800806 all_formats.emplace_back(VkSurfaceFormatKHR{
807 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
808 all_formats.emplace_back(VkSurfaceFormatKHR{
809 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
810 }
811
Ian Elliott1ce053f2022-03-16 09:49:53 -0600812 // NOTE: Any new formats that are added must be coordinated across different
813 // Android users. This includes the ANGLE team (a layered implementation of
814 // OpenGL-ES).
815
Charlie Lao324191f2019-12-13 11:03:16 -0800816 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
817 if (AHardwareBuffer_isSupported(&desc)) {
818 all_formats.emplace_back(VkSurfaceFormatKHR{
819 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800820 if (colorspace_ext) {
821 all_formats.emplace_back(
822 VkSurfaceFormatKHR{VK_FORMAT_R5G6B5_UNORM_PACK16,
823 VK_COLOR_SPACE_PASS_THROUGH_EXT});
824 }
Charlie Lao324191f2019-12-13 11:03:16 -0800825 }
826
827 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
828 if (AHardwareBuffer_isSupported(&desc)) {
829 all_formats.emplace_back(VkSurfaceFormatKHR{
830 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800831 if (colorspace_ext) {
832 all_formats.emplace_back(
833 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
834 VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800835 all_formats.emplace_back(
836 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
837 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
838 all_formats.emplace_back(
839 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
840 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
841 }
842 }
843
844 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
845 if (AHardwareBuffer_isSupported(&desc)) {
846 all_formats.emplace_back(
847 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
848 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800849 if (colorspace_ext) {
850 all_formats.emplace_back(
851 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
852 VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800853 all_formats.emplace_back(
854 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
855 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
856 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700857 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700858
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500859 desc.format = AHARDWAREBUFFER_FORMAT_R8_UNORM;
860 if (AHardwareBuffer_isSupported(&desc)) {
Jason Macnakca506332022-11-09 11:00:36 -0800861 if (colorspace_ext) {
862 all_formats.emplace_back(VkSurfaceFormatKHR{
863 VK_FORMAT_R8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT});
864 }
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500865 }
866
Trevor David Black67e9b102023-03-07 05:28:15 +0000867 bool rgba10x6_formats_ext = false;
868 uint32_t exts_count;
869 const auto& driver = GetData(pdev).driver;
870 driver.EnumerateDeviceExtensionProperties(pdev, nullptr, &exts_count,
871 nullptr);
872 std::vector<VkExtensionProperties> props(exts_count);
873 driver.EnumerateDeviceExtensionProperties(pdev, nullptr, &exts_count,
874 props.data());
875 for (uint32_t i = 0; i < exts_count; i++) {
876 VkExtensionProperties prop = props[i];
877 if (strcmp(prop.extensionName,
878 VK_EXT_RGBA10X6_FORMATS_EXTENSION_NAME) == 0) {
879 rgba10x6_formats_ext = true;
880 }
881 }
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000882 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM;
Trevor David Black67e9b102023-03-07 05:28:15 +0000883 if (AHardwareBuffer_isSupported(&desc) && rgba10x6_formats_ext) {
Trevor David Black9cfe1ed2022-12-05 20:04:57 +0000884 all_formats.emplace_back(
885 VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
886 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
887 if (colorspace_ext) {
888 all_formats.emplace_back(
889 VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
890 VK_COLOR_SPACE_PASS_THROUGH_EXT});
891 all_formats.emplace_back(
892 VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
893 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
894 }
895 }
896
Ian Elliott6ba85d92022-02-18 16:44:58 -0700897 // NOTE: Any new formats that are added must be coordinated across different
898 // Android users. This includes the ANGLE team (a layered implementation of
899 // OpenGL-ES).
900
Jesse Halld7b994a2015-09-07 14:17:37 -0700901 VkResult result = VK_SUCCESS;
902 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800903 uint32_t transfer_count = all_formats.size();
904 if (transfer_count > *count) {
905 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700906 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700907 }
Charlie Lao324191f2019-12-13 11:03:16 -0800908 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
909 formats);
910 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700911 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800912 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700913 }
Charlie Lao324191f2019-12-13 11:03:16 -0800914
Jesse Halld7b994a2015-09-07 14:17:37 -0700915 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700916}
917
Jesse Halle1b12782015-11-30 11:27:32 -0800918VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300919VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
920 VkPhysicalDevice physicalDevice,
921 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
922 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800923 ATRACE_CALL();
924
Chris Forbes2452cf72017-03-16 16:30:17 +1300925 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
926 physicalDevice, pSurfaceInfo->surface,
927 &pSurfaceCapabilities->surfaceCapabilities);
928
Chris Forbes06bc0092017-03-16 16:46:05 +1300929 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
930 while (caps->pNext) {
931 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
932
933 switch (caps->sType) {
934 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
935 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
936 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
937 caps);
938 // Claim same set of usage flags are supported for
939 // shared present modes as for other modes.
940 shared_caps->sharedPresentSupportedUsageFlags =
941 pSurfaceCapabilities->surfaceCapabilities
942 .supportedUsageFlags;
943 } break;
944
Ian Elliottbb67b242022-03-16 09:52:28 -0600945 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
946 VkSurfaceProtectedCapabilitiesKHR* protected_caps =
947 reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(caps);
948 protected_caps->supportsProtected = VK_TRUE;
949 } break;
950
Chris Forbes06bc0092017-03-16 16:46:05 +1300951 default:
952 // Ignore all other extension structs
953 break;
954 }
955 }
956
Chris Forbes2452cf72017-03-16 16:30:17 +1300957 return result;
958}
959
960VKAPI_ATTR
961VkResult GetPhysicalDeviceSurfaceFormats2KHR(
962 VkPhysicalDevice physicalDevice,
963 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
964 uint32_t* pSurfaceFormatCount,
965 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800966 ATRACE_CALL();
967
Chris Forbes2452cf72017-03-16 16:30:17 +1300968 if (!pSurfaceFormats) {
969 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
970 pSurfaceInfo->surface,
971 pSurfaceFormatCount, nullptr);
Trevor David Black929e9cd2022-11-22 04:12:19 +0000972 }
Chris Forbes2452cf72017-03-16 16:30:17 +1300973
Trevor David Black929e9cd2022-11-22 04:12:19 +0000974 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
975 // after the call.
976 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
977 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
978 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
979 surface_formats.data());
Trevor David Black2cc44682022-03-09 00:31:38 +0000980
Trevor David Black929e9cd2022-11-22 04:12:19 +0000981 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
982 return result;
983 }
Trevor David Black2cc44682022-03-09 00:31:38 +0000984
Trevor David Black929e9cd2022-11-22 04:12:19 +0000985 const auto& driver = GetData(physicalDevice).driver;
986
987 // marshal results individually due to stride difference.
988 uint32_t formats_to_marshal = *pSurfaceFormatCount;
989 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
990 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
991
992 // Query the compression properties for the surface format
993 VkSurfaceFormat2KHR* pSurfaceFormat = &pSurfaceFormats[i];
994 while (pSurfaceFormat->pNext) {
995 pSurfaceFormat =
996 reinterpret_cast<VkSurfaceFormat2KHR*>(pSurfaceFormat->pNext);
997 switch (pSurfaceFormat->sType) {
998 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT: {
Trevor David Black2cc44682022-03-09 00:31:38 +0000999 VkImageCompressionPropertiesEXT* surfaceCompressionProps =
1000 reinterpret_cast<VkImageCompressionPropertiesEXT*>(
Trevor David Black929e9cd2022-11-22 04:12:19 +00001001 pSurfaceFormat);
Trevor David Black2cc44682022-03-09 00:31:38 +00001002
1003 if (surfaceCompressionProps &&
1004 driver.GetPhysicalDeviceImageFormatProperties2KHR) {
1005 VkPhysicalDeviceImageFormatInfo2 imageFormatInfo = {};
1006 imageFormatInfo.sType =
1007 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2;
1008 imageFormatInfo.format =
1009 pSurfaceFormats[i].surfaceFormat.format;
1010 imageFormatInfo.pNext = nullptr;
1011
1012 VkImageCompressionControlEXT compressionControl = {};
1013 compressionControl.sType =
1014 VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT;
1015 compressionControl.pNext = imageFormatInfo.pNext;
1016
1017 imageFormatInfo.pNext = &compressionControl;
1018
1019 VkImageCompressionPropertiesEXT compressionProps = {};
1020 compressionProps.sType =
1021 VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT;
1022 compressionProps.pNext = nullptr;
1023
1024 VkImageFormatProperties2KHR imageFormatProps = {};
1025 imageFormatProps.sType =
1026 VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR;
1027 imageFormatProps.pNext = &compressionProps;
1028
1029 VkResult compressionRes =
1030 driver.GetPhysicalDeviceImageFormatProperties2KHR(
1031 physicalDevice, &imageFormatInfo,
1032 &imageFormatProps);
1033 if (compressionRes == VK_SUCCESS) {
1034 surfaceCompressionProps->imageCompressionFlags =
1035 compressionProps.imageCompressionFlags;
1036 surfaceCompressionProps
1037 ->imageCompressionFixedRateFlags =
1038 compressionProps.imageCompressionFixedRateFlags;
1039 } else {
1040 return compressionRes;
1041 }
1042 }
Trevor David Black929e9cd2022-11-22 04:12:19 +00001043 } break;
1044
1045 default:
1046 // Ignore all other extension structs
1047 break;
Chris Forbes2452cf72017-03-16 16:30:17 +13001048 }
1049 }
Chris Forbes2452cf72017-03-16 16:30:17 +13001050 }
Trevor David Black929e9cd2022-11-22 04:12:19 +00001051
1052 return result;
Chris Forbes2452cf72017-03-16 16:30:17 +13001053}
1054
1055VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +13001056VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -08001057 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +08001058 uint32_t* count,
1059 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001060 ATRACE_CALL();
1061
Yiwei Zhange4a559c2018-02-15 11:27:36 -08001062 int err;
1063 int query_value;
Ian Elliotte7f036c2022-03-15 16:49:21 -06001064 std::vector<VkPresentModeKHR> present_modes;
Ian Elliott1ce053f2022-03-16 09:49:53 -06001065 if (surface == VK_NULL_HANDLE) {
1066 const InstanceData& instance_data = GetData(pdev);
1067 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
1068 bool surfaceless_enabled =
1069 instance_data.hook_extensions.test(surfaceless);
1070 if (!surfaceless_enabled) {
1071 return VK_ERROR_SURFACE_LOST_KHR;
1072 }
1073 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
1074 // extension for this function is for
1075 // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and
1076 // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot
1077 // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a
Ian Elliott7e361142022-06-02 10:45:17 -06001078 // surface, and that cannot be relied upon. Therefore, don't return it.
Ian Elliott1ce053f2022-03-16 09:49:53 -06001079 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1080 } else {
1081 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1082
1083 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1084 &query_value);
1085 if (err != android::OK || query_value < 0) {
1086 ALOGE(
1087 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
1088 "value=%d",
1089 strerror(-err), err, query_value);
1090 return VK_ERROR_SURFACE_LOST_KHR;
1091 }
1092 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1093
1094 err =
1095 window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
1096 if (err != android::OK || query_value < 0) {
1097 ALOGE(
1098 "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
1099 strerror(-err), err, query_value);
1100 return VK_ERROR_SURFACE_LOST_KHR;
1101 }
1102 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
1103
1104 if (min_undequeued_buffers + 1 < max_buffer_count)
1105 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
1106 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1107 }
Chris Forbese8d79a62017-02-22 12:49:18 +13001108
1109 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001110 QueryPresentationProperties(pdev, &present_properties);
1111 if (present_properties.sharedImage) {
1112 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
1113 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +13001114 }
1115
1116 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -07001117
1118 VkResult result = VK_SUCCESS;
1119 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +13001120 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -07001121 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +13001122 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -07001123 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -07001124 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +13001125 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -07001126 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001127 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001128}
1129
Jesse Halle1b12782015-11-30 11:27:32 -08001130VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001131VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001132 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001133 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001134 ATRACE_CALL();
1135
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001136 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
1137 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
1138 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
1139 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
1140 pDeviceGroupPresentCapabilities->sType);
1141
1142 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
1143 sizeof(pDeviceGroupPresentCapabilities->presentMask));
1144
1145 // assume device group of size 1
1146 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
1147 pDeviceGroupPresentCapabilities->modes =
1148 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1149
1150 return VK_SUCCESS;
1151}
1152
1153VKAPI_ATTR
1154VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001155 VkDevice,
1156 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001157 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001158 ATRACE_CALL();
1159
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001160 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1161 return VK_SUCCESS;
1162}
1163
1164VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -06001165VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001166 VkSurfaceKHR surface,
1167 uint32_t* pRectCount,
1168 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001169 ATRACE_CALL();
1170
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001171 if (!pRects) {
1172 *pRectCount = 1;
1173 } else {
1174 uint32_t count = std::min(*pRectCount, 1u);
1175 bool incomplete = *pRectCount < 1;
1176
1177 *pRectCount = count;
1178
1179 if (incomplete) {
1180 return VK_INCOMPLETE;
1181 }
1182
1183 int err;
1184 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1185
1186 int width = 0, height = 0;
1187 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001188 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001189 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1190 strerror(-err), err);
1191 }
1192 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001193 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001194 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1195 strerror(-err), err);
1196 }
1197
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001198 pRects[0].offset.x = 0;
1199 pRects[0].offset.y = 0;
1200 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1201 static_cast<uint32_t>(height)};
1202 }
1203 return VK_SUCCESS;
1204}
1205
Yiwei Zhang533cea92019-06-03 18:43:24 -07001206static void DestroySwapchainInternal(VkDevice device,
1207 VkSwapchainKHR swapchain_handle,
1208 const VkAllocationCallbacks* allocator) {
1209 ATRACE_CALL();
1210
1211 const auto& dispatch = GetData(device).driver;
1212 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1213 if (!swapchain) {
1214 return;
1215 }
1216
1217 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1218 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1219
1220 if (window && swapchain->frame_timestamps_enabled) {
1221 native_window_enable_frame_timestamps(window, false);
1222 }
1223
1224 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001225 ReleaseSwapchainImage(device, swapchain->shared, window, -1,
1226 swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001227 }
1228
1229 if (active) {
1230 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1231 }
1232
1233 if (!allocator) {
1234 allocator = &GetData(device).allocator;
1235 }
1236
1237 swapchain->~Swapchain();
1238 allocator->pfnFree(allocator->pUserData, swapchain);
1239}
1240
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001241VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001242VkResult CreateSwapchainKHR(VkDevice device,
1243 const VkSwapchainCreateInfoKHR* create_info,
1244 const VkAllocationCallbacks* allocator,
1245 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001246 ATRACE_CALL();
1247
Jesse Halld7b994a2015-09-07 14:17:37 -07001248 int err;
1249 VkResult result = VK_SUCCESS;
1250
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001251 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1252 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1253 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1254 " oldSwapchain=0x%" PRIx64,
1255 reinterpret_cast<uint64_t>(create_info->surface),
1256 create_info->minImageCount, create_info->imageFormat,
1257 create_info->imageColorSpace, create_info->imageExtent.width,
1258 create_info->imageExtent.height, create_info->imageUsage,
1259 create_info->preTransform, create_info->presentMode,
1260 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1261
Jesse Hall1f91d392015-12-11 16:28:44 -08001262 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001263 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001264
Trevor David Blackf499b5a2023-07-14 17:30:41 +00001265 PixelFormat native_pixel_format =
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001266 GetNativePixelFormat(create_info->imageFormat);
1267 android_dataspace native_dataspace =
Sally Qi03a1a752023-07-17 12:52:05 +08001268 GetNativeDataspace(create_info->imageColorSpace, native_pixel_format);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001269 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1270 ALOGE(
1271 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1272 "failed: Unsupported color space",
1273 create_info->imageColorSpace);
1274 return VK_ERROR_INITIALIZATION_FAILED;
1275 }
1276
Jesse Hall42a9eec2016-06-03 12:39:49 -07001277 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001278 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001279 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001280 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001281 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001282 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001283 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001284 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001285 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1286 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001287 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001288 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001289
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001290 Surface& surface = *SurfaceFromHandle(create_info->surface);
1291
Jesse Halldc225072016-05-30 22:40:14 -07001292 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001293 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001294 " because it already has active swapchain 0x%" PRIx64
1295 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1296 reinterpret_cast<uint64_t>(create_info->surface),
1297 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1298 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1299 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1300 }
1301 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1302 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1303
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001304 // -- Reset the native window --
1305 // The native window might have been used previously, and had its properties
1306 // changed from defaults. That will affect the answer we get for queries
1307 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1308 // attempt such queries.
1309
Jesse Halldc225072016-05-30 22:40:14 -07001310 // The native window only allows dequeueing all buffers before any have
1311 // been queued, since after that point at least one is assumed to be in
1312 // non-FREE state at any given time. Disconnecting and re-connecting
1313 // orphans the previous buffers, getting us back to the state where we can
1314 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001315 //
1316 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001317 ANativeWindow* window = surface.window.get();
1318 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1319 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001320 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001321 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1322 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001323 strerror(-err), err);
1324
Nicolas Capens147b7da2021-04-09 14:53:06 -04001325 err =
1326 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001327 if (err != android::OK) {
1328 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1329 strerror(-err), err);
1330 return VK_ERROR_SURFACE_LOST_KHR;
1331 }
1332
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301333 int swap_interval =
1334 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001335 err = window->setSwapInterval(window, swap_interval);
1336 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001337 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1338 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001339 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001340 }
1341
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001342 err = native_window_set_shared_buffer_mode(window, false);
1343 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001344 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1345 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001346 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001347 }
1348
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001349 err = native_window_set_auto_refresh(window, false);
1350 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001351 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1352 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001353 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001354 }
1355
Jesse Halld7b994a2015-09-07 14:17:37 -07001356 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001357
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001358 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001359
Trevor David Blackf499b5a2023-07-14 17:30:41 +00001360 err = native_window_set_buffers_format(
1361 window, static_cast<int>(native_pixel_format));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001362 if (err != android::OK) {
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001363 ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)",
Trevor David Blackf499b5a2023-07-14 17:30:41 +00001364 toString(native_pixel_format).c_str(), strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001365 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001366 }
Yiwei Zhang51572c22022-07-22 23:08:30 +00001367
1368 /* Respect consumer default dataspace upon HAL_DATASPACE_ARBITRARY. */
1369 if (native_dataspace != HAL_DATASPACE_ARBITRARY) {
1370 err = native_window_set_buffers_data_space(window, native_dataspace);
1371 if (err != android::OK) {
1372 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1373 native_dataspace, strerror(-err), err);
1374 return VK_ERROR_SURFACE_LOST_KHR;
1375 }
Jesse Hall517274a2016-02-10 00:07:18 -08001376 }
1377
Jesse Hall3dd678a2016-01-08 21:52:01 -08001378 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001379 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001380 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001381 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001382 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1383 create_info->imageExtent.width, create_info->imageExtent.height,
1384 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001385 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001386 }
1387
Jesse Hall178b6962016-02-24 15:39:50 -08001388 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1389 // applied during rendering. native_window_set_transform() expects the
1390 // inverse: the transform the app is requesting that the compositor perform
1391 // during composition. With native windows, pre-transform works by rendering
1392 // with the same transform the compositor is applying (as in Vulkan), but
1393 // then requesting the inverse transform, so that when the compositor does
1394 // it's job the two transforms cancel each other out and the compositor ends
1395 // up applying an identity transform to the app's buffer.
1396 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001397 window, InvertTransformToNative(create_info->preTransform));
1398 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001399 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1400 InvertTransformToNative(create_info->preTransform),
1401 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001402 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001403 }
1404
Jesse Hallf64ca122015-11-03 16:11:10 -08001405 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001406 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1407 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001408 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1409 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001410 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001411 }
1412
Chris Forbes97ef4612017-03-30 19:37:50 +13001413 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1414 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1415 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1416 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001417 err = native_window_set_shared_buffer_mode(window, true);
1418 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001419 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1420 return VK_ERROR_SURFACE_LOST_KHR;
1421 }
1422 }
1423
1424 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001425 err = native_window_set_auto_refresh(window, true);
1426 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001427 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1428 return VK_ERROR_SURFACE_LOST_KHR;
1429 }
1430 }
1431
Ian Elliott16c443c2021-11-30 17:10:32 -07001432 int query_value;
1433 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1434 &query_value);
1435 if (err != android::OK || query_value < 0) {
1436 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1437 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001438 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001439 }
Ian Elliott16c443c2021-11-30 17:10:32 -07001440 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1441 const auto mailbox_num_images = std::max(3u, create_info->minImageCount);
1442 const auto requested_images =
1443 swap_interval ? create_info->minImageCount : mailbox_num_images;
1444 uint32_t num_images = requested_images - 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001445
Ian Elliott5396b702021-12-13 19:32:34 -07001446 // Lower layer insists that we have at least min_undequeued_buffers + 1
1447 // buffers. This is wasteful and we'd like to relax it in the shared case,
1448 // but not all the pieces are in place for that to work yet. Note we only
1449 // lie to the lower layer--we don't want to give the app back a swapchain
1450 // with extra images (which they can't actually use!).
1451 uint32_t min_buffer_count = min_undequeued_buffers + 1;
1452 err = native_window_set_buffer_count(
1453 window, std::max(min_buffer_count, num_images));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001454 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001455 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1456 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001457 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001458 }
1459
Ian Elliott12b7e2f2021-12-21 23:24:20 -07001460 // In shared mode the num_images must be one regardless of how many
1461 // buffers were allocated for the buffer queue.
1462 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1463 num_images = 1;
1464 }
1465
Trevor David Black2cc44682022-03-09 00:31:38 +00001466 void* usage_info_pNext = nullptr;
1467 VkImageCompressionControlEXT image_compression = {};
John Reck97678d42022-08-23 11:24:51 -04001468 uint64_t native_usage = 0;
Trevor David Black2cc44682022-03-09 00:31:38 +00001469 if (dispatch.GetSwapchainGrallocUsage3ANDROID) {
1470 ATRACE_BEGIN("GetSwapchainGrallocUsage3ANDROID");
1471 VkGrallocUsageInfoANDROID gralloc_usage_info = {};
1472 gralloc_usage_info.sType = VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID;
1473 gralloc_usage_info.format = create_info->imageFormat;
1474 gralloc_usage_info.imageUsage = create_info->imageUsage;
1475
1476 // Look through the pNext chain for an image compression control struct
1477 // if one is found AND the appropriate extensions are enabled,
1478 // append it to be the gralloc usage pNext chain
1479 const VkSwapchainCreateInfoKHR* create_infos = create_info;
1480 while (create_infos->pNext) {
1481 create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(
1482 create_infos->pNext);
1483 switch (create_infos->sType) {
1484 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: {
1485 const VkImageCompressionControlEXT* compression_infos =
1486 reinterpret_cast<const VkImageCompressionControlEXT*>(
1487 create_infos);
1488 image_compression = *compression_infos;
1489 image_compression.pNext = nullptr;
1490 usage_info_pNext = &image_compression;
1491 } break;
1492
1493 default:
1494 // Ignore all other info structs
1495 break;
1496 }
1497 }
1498 gralloc_usage_info.pNext = usage_info_pNext;
1499
1500 result = dispatch.GetSwapchainGrallocUsage3ANDROID(
1501 device, &gralloc_usage_info, &native_usage);
1502 ATRACE_END();
1503 if (result != VK_SUCCESS) {
1504 ALOGE("vkGetSwapchainGrallocUsage3ANDROID failed: %d", result);
1505 return VK_ERROR_SURFACE_LOST_KHR;
1506 }
1507 } else if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001508 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001509 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001510 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1511 device, create_info->imageFormat, create_info->imageUsage,
1512 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001513 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001514 if (result != VK_SUCCESS) {
1515 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001516 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001517 }
John Reck97678d42022-08-23 11:24:51 -04001518 native_usage =
Trevor David Black2cc44682022-03-09 00:31:38 +00001519 convertGralloc1ToBufferUsage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001520 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001521 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
John Reck97678d42022-08-23 11:24:51 -04001522 int32_t legacy_usage = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -08001523 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001524 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001525 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001526 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001527 if (result != VK_SUCCESS) {
1528 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001529 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001530 }
John Reck97678d42022-08-23 11:24:51 -04001531 native_usage = static_cast<uint64_t>(legacy_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001532 }
John Reck97678d42022-08-23 11:24:51 -04001533 native_usage |= surface.consumer_usage;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001534
1535 bool createProtectedSwapchain = false;
1536 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1537 createProtectedSwapchain = true;
1538 native_usage |= BufferUsage::PROTECTED;
1539 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001540 err = native_window_set_usage(window, native_usage);
1541 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001542 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001543 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001544 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001545
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001546 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001547 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1548 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001549 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1550 strerror(-err), err);
1551 return VK_ERROR_SURFACE_LOST_KHR;
1552 }
1553
Jesse Halld7b994a2015-09-07 14:17:37 -07001554 // -- Allocate our Swapchain object --
1555 // After this point, we must deallocate the swapchain on error.
1556
Jesse Hall1f91d392015-12-11 16:28:44 -08001557 void* mem = allocator->pfnAllocation(allocator->pUserData,
1558 sizeof(Swapchain), alignof(Swapchain),
1559 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001560 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001561 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001562 Swapchain* swapchain = new (mem)
1563 Swapchain(surface, num_images, create_info->presentMode,
1564 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001565 // -- Dequeue all buffers and create a VkImage for each --
1566 // Any failures during or after this must cancel the dequeued buffers.
1567
Chris Forbesb56287a2017-01-12 14:28:58 +13001568 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1569#pragma clang diagnostic push
1570#pragma clang diagnostic ignored "-Wold-style-cast"
1571 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1572#pragma clang diagnostic pop
Trevor David Black2cc44682022-03-09 00:31:38 +00001573 .pNext = usage_info_pNext,
Chris Forbesb56287a2017-01-12 14:28:58 +13001574 .usage = swapchain_image_usage,
1575 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001576 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001577#pragma clang diagnostic push
1578#pragma clang diagnostic ignored "-Wold-style-cast"
1579 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1580#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001581 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001582 };
1583 VkImageCreateInfo image_create = {
1584 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1585 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001586 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001587 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001588 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001589 .extent = {0, 0, 1},
1590 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001591 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001592 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001593 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001594 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001595 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001596 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001597 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1598 };
1599
Jesse Halld7b994a2015-09-07 14:17:37 -07001600 for (uint32_t i = 0; i < num_images; i++) {
1601 Swapchain::Image& img = swapchain->images[i];
1602
1603 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001604 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1605 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001606 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001607 switch (-err) {
1608 case ENOMEM:
1609 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1610 break;
1611 default:
1612 result = VK_ERROR_SURFACE_LOST_KHR;
1613 break;
1614 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001615 break;
1616 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001617 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001618 img.dequeued = true;
1619
1620 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001621 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1622 static_cast<uint32_t>(img.buffer->height),
1623 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001624 image_native_buffer.handle = img.buffer->handle;
1625 image_native_buffer.stride = img.buffer->stride;
1626 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001627 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001628 android_convertGralloc0To1Usage(int(img.buffer->usage),
1629 &image_native_buffer.usage2.producer,
1630 &image_native_buffer.usage2.consumer);
Trevor David Black2cc44682022-03-09 00:31:38 +00001631 image_native_buffer.usage3 = img.buffer->usage;
Jesse Halld7b994a2015-09-07 14:17:37 -07001632
Yiwei Zhang533cea92019-06-03 18:43:24 -07001633 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001634 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001635 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001636 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001637 if (result != VK_SUCCESS) {
1638 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1639 break;
1640 }
1641 }
1642
1643 // -- Cancel all buffers, returning them to the queue --
1644 // If an error occurred before, also destroy the VkImage and release the
1645 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001646 for (uint32_t i = 0; i < num_images; i++) {
1647 Swapchain::Image& img = swapchain->images[i];
1648 if (img.dequeued) {
1649 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001650 window->cancelBuffer(window, img.buffer.get(),
1651 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001652 img.dequeue_fence = -1;
1653 img.dequeued = false;
1654 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001655 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001656 }
1657
1658 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001659 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1660 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001661 return result;
1662 }
1663
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001664 if (transform_hint != swapchain->pre_transform) {
1665 // Log that the app is not doing pre-rotation.
1666 android::GraphicsEnv::getInstance().setTargetStats(
1667 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1668 }
1669
Jesse Halldc225072016-05-30 22:40:14 -07001670 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1671 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001672 return VK_SUCCESS;
1673}
1674
Jesse Halle1b12782015-11-30 11:27:32 -08001675VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001676void DestroySwapchainKHR(VkDevice device,
1677 VkSwapchainKHR swapchain_handle,
1678 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001679 ATRACE_CALL();
1680
Yiwei Zhang533cea92019-06-03 18:43:24 -07001681 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001682}
1683
Jesse Halle1b12782015-11-30 11:27:32 -08001684VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001685VkResult GetSwapchainImagesKHR(VkDevice,
1686 VkSwapchainKHR swapchain_handle,
1687 uint32_t* count,
1688 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001689 ATRACE_CALL();
1690
Jesse Halld7b994a2015-09-07 14:17:37 -07001691 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001692 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1693 "getting images for non-active swapchain 0x%" PRIx64
1694 "; only dequeued image handles are valid",
1695 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001696 VkResult result = VK_SUCCESS;
1697 if (images) {
1698 uint32_t n = swapchain.num_images;
1699 if (*count < swapchain.num_images) {
1700 n = *count;
1701 result = VK_INCOMPLETE;
1702 }
1703 for (uint32_t i = 0; i < n; i++)
1704 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001705 *count = n;
1706 } else {
1707 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001708 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001709 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001710}
1711
Jesse Halle1b12782015-11-30 11:27:32 -08001712VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001713VkResult AcquireNextImageKHR(VkDevice device,
1714 VkSwapchainKHR swapchain_handle,
1715 uint64_t timeout,
1716 VkSemaphore semaphore,
1717 VkFence vk_fence,
1718 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001719 ATRACE_CALL();
1720
Jesse Halld7b994a2015-09-07 14:17:37 -07001721 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001722 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001723 VkResult result;
1724 int err;
1725
Jesse Halldc225072016-05-30 22:40:14 -07001726 if (swapchain.surface.swapchain_handle != swapchain_handle)
1727 return VK_ERROR_OUT_OF_DATE_KHR;
1728
Chris Forbesc88409c2017-03-30 19:47:37 +13001729 if (swapchain.shared) {
1730 // In shared mode, we keep the buffer dequeued all the time, so we don't
1731 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1732 // the semaphore and fence passed to us will be signalled.
1733 *image_index = 0;
1734 result = GetData(device).driver.AcquireImageANDROID(
1735 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1736 return result;
1737 }
1738
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001739 const nsecs_t acquire_next_image_timeout =
1740 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1741 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1742 // Cache the timeout to avoid the duplicate binder cost.
1743 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1744 acquire_next_image_timeout);
1745 if (err != android::OK) {
1746 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1747 strerror(-err), err);
1748 return VK_ERROR_SURFACE_LOST_KHR;
1749 }
1750 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1751 }
1752
Jesse Halld7b994a2015-09-07 14:17:37 -07001753 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001754 int fence_fd;
1755 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001756 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001757 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1758 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1759 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001760 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001761 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001762 }
1763
1764 uint32_t idx;
1765 for (idx = 0; idx < swapchain.num_images; idx++) {
1766 if (swapchain.images[idx].buffer.get() == buffer) {
1767 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001768 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001769 break;
1770 }
1771 }
1772 if (idx == swapchain.num_images) {
1773 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001774 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001775 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001776 }
1777
1778 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001779 if (fence_fd != -1) {
1780 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001781 if (fence_clone == -1) {
1782 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1783 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001784 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001785 }
1786 }
1787
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001788 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001789 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001790 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001791 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1792 // even if the call fails. We could close it ourselves on failure, but
1793 // that would create a race condition if the driver closes it on a
1794 // failure path: some other thread might create an fd with the same
1795 // number between the time the driver closes it and the time we close
1796 // it. We must assume one of: the driver *always* closes it even on
1797 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001798 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001799 swapchain.images[idx].dequeued = false;
1800 swapchain.images[idx].dequeue_fence = -1;
1801 return result;
1802 }
1803
1804 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001805 return VK_SUCCESS;
1806}
1807
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001808VKAPI_ATTR
1809VkResult AcquireNextImage2KHR(VkDevice device,
1810 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1811 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001812 ATRACE_CALL();
1813
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001814 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1815 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1816 pAcquireInfo->fence, pImageIndex);
1817}
1818
Jesse Halldc225072016-05-30 22:40:14 -07001819static VkResult WorstPresentResult(VkResult a, VkResult b) {
1820 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1821 // (in spec version 1.0.14).
1822 static const VkResult kWorstToBest[] = {
1823 VK_ERROR_DEVICE_LOST,
1824 VK_ERROR_SURFACE_LOST_KHR,
1825 VK_ERROR_OUT_OF_DATE_KHR,
1826 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1827 VK_ERROR_OUT_OF_HOST_MEMORY,
1828 VK_SUBOPTIMAL_KHR,
1829 };
1830 for (auto result : kWorstToBest) {
1831 if (a == result || b == result)
1832 return result;
1833 }
1834 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1835 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1836 return a != VK_SUCCESS ? a : b;
1837}
1838
Jesse Halle1b12782015-11-30 11:27:32 -08001839VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001840VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001841 ATRACE_CALL();
1842
Jesse Halld7b994a2015-09-07 14:17:37 -07001843 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1844 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1845 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001846
Jesse Halldc225072016-05-30 22:40:14 -07001847 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001848 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001849 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001850
Ian Elliottcb351132016-12-13 10:30:40 -07001851 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001852 const VkPresentRegionsKHR* present_regions = nullptr;
1853 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001854 const VkPresentRegionsKHR* next =
1855 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1856 while (next) {
1857 switch (next->sType) {
1858 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1859 present_regions = next;
1860 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001861 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001862 present_times =
1863 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1864 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001865 default:
1866 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1867 next->sType);
1868 break;
1869 }
1870 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1871 }
1872 ALOGV_IF(
1873 present_regions &&
1874 present_regions->swapchainCount != present_info->swapchainCount,
1875 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001876 ALOGV_IF(present_times &&
1877 present_times->swapchainCount != present_info->swapchainCount,
1878 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1879 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001880 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001881 (present_regions) ? present_regions->pRegions : nullptr;
1882 const VkPresentTimeGOOGLE* times =
1883 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001884 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001885 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001886 uint32_t nrects = 0;
1887
Jesse Halld7b994a2015-09-07 14:17:37 -07001888 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1889 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001890 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001891 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001892 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001893 const VkPresentRegionKHR* region =
1894 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001895 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001896 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001897 VkResult result;
1898 int err;
1899
Jesse Halld7b994a2015-09-07 14:17:37 -07001900 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001901 result = dispatch.QueueSignalReleaseImageANDROID(
1902 queue, present_info->waitSemaphoreCount,
1903 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001904 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001905 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001906 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001907 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001908 if (img.release_fence >= 0)
1909 close(img.release_fence);
1910 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001911
Jesse Halldc225072016-05-30 22:40:14 -07001912 if (swapchain.surface.swapchain_handle ==
1913 present_info->pSwapchains[sc]) {
1914 ANativeWindow* window = swapchain.surface.window.get();
1915 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001916 if (region) {
1917 // Process the incremental-present hint for this swapchain:
1918 uint32_t rcount = region->rectangleCount;
1919 if (rcount > nrects) {
1920 android_native_rect_t* new_rects =
1921 static_cast<android_native_rect_t*>(
1922 allocator->pfnReallocation(
1923 allocator->pUserData, rects,
1924 sizeof(android_native_rect_t) * rcount,
1925 alignof(android_native_rect_t),
1926 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1927 if (new_rects) {
1928 rects = new_rects;
1929 nrects = rcount;
1930 } else {
1931 rcount = 0; // Ignore the hint for this swapchain
1932 }
1933 }
1934 for (uint32_t r = 0; r < rcount; ++r) {
1935 if (region->pRectangles[r].layer > 0) {
1936 ALOGV(
1937 "vkQueuePresentKHR ignoring invalid layer "
1938 "(%u); using layer 0 instead",
1939 region->pRectangles[r].layer);
1940 }
1941 int x = region->pRectangles[r].offset.x;
1942 int y = region->pRectangles[r].offset.y;
1943 int width = static_cast<int>(
1944 region->pRectangles[r].extent.width);
1945 int height = static_cast<int>(
1946 region->pRectangles[r].extent.height);
1947 android_native_rect_t* cur_rect = &rects[r];
1948 cur_rect->left = x;
1949 cur_rect->top = y + height;
1950 cur_rect->right = x + width;
1951 cur_rect->bottom = y;
1952 }
1953 native_window_set_surface_damage(window, rects, rcount);
1954 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001955 if (time) {
1956 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001957 ALOGV(
1958 "Calling "
1959 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001960 native_window_enable_frame_timestamps(window, true);
1961 swapchain.frame_timestamps_enabled = true;
1962 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001963
1964 // Record the nativeFrameId so it can be later correlated to
1965 // this present.
1966 uint64_t nativeFrameId = 0;
1967 err = native_window_get_next_frame_id(
1968 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001969 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001970 ALOGE("Failed to get next native frame ID.");
1971 }
1972
1973 // Add a new timing record with the user's presentID and
1974 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001975 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001976 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001977 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001978 }
1979 if (time->desiredPresentTime) {
1980 // Set the desiredPresentTime:
1981 ALOGV(
1982 "Calling "
1983 "native_window_set_buffers_timestamp(%" PRId64 ")",
1984 time->desiredPresentTime);
1985 native_window_set_buffers_timestamp(
1986 window,
1987 static_cast<int64_t>(time->desiredPresentTime));
1988 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001989 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001990
Jesse Halldc225072016-05-30 22:40:14 -07001991 err = window->queueBuffer(window, img.buffer.get(), fence);
1992 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001993 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001994 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1995 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001996 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001997 } else {
1998 if (img.dequeue_fence >= 0) {
1999 close(img.dequeue_fence);
2000 img.dequeue_fence = -1;
2001 }
2002 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07002003 }
Chris Forbesfca0f292017-03-30 19:48:39 +13002004
2005 // If the swapchain is in shared mode, immediately dequeue the
2006 // buffer so it can be presented again without an intervening
2007 // call to AcquireNextImageKHR. We expect to get the same buffer
2008 // back from every call to dequeueBuffer in this mode.
2009 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
2010 ANativeWindowBuffer* buffer;
2011 int fence_fd;
2012 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002013 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13002014 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
2015 swapchain_result = WorstPresentResult(swapchain_result,
2016 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002017 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13002018 ALOGE("got wrong image back for shared swapchain");
2019 swapchain_result = WorstPresentResult(swapchain_result,
2020 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002021 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13002022 img.dequeue_fence = fence_fd;
2023 img.dequeued = true;
2024 }
2025 }
Jesse Halldc225072016-05-30 22:40:14 -07002026 }
2027 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07002028 OrphanSwapchain(device, &swapchain);
2029 }
silence_dogood73597592019-05-23 16:57:37 -07002030 int window_transform_hint;
2031 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
2032 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002033 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07002034 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
2035 strerror(-err), err);
2036 swapchain_result = WorstPresentResult(
2037 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
2038 }
2039 if (swapchain.pre_transform != window_transform_hint) {
2040 swapchain_result =
2041 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
2042 }
Jesse Halldc225072016-05-30 22:40:14 -07002043 } else {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00002044 ReleaseSwapchainImage(device, swapchain.shared, nullptr, fence,
2045 img, true);
Jesse Halldc225072016-05-30 22:40:14 -07002046 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07002047 }
2048
Jesse Halla9e57032015-11-30 01:03:10 -08002049 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07002050 present_info->pResults[sc] = swapchain_result;
2051
2052 if (swapchain_result != final_result)
2053 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07002054 }
Ian Elliottcb351132016-12-13 10:30:40 -07002055 if (rects) {
2056 allocator->pfnFree(allocator->pUserData, rects);
2057 }
Jesse Halld7b994a2015-09-07 14:17:37 -07002058
2059 return final_result;
2060}
Jesse Hallb1352bc2015-09-04 16:12:33 -07002061
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002062VKAPI_ATTR
2063VkResult GetRefreshCycleDurationGOOGLE(
2064 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07002065 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002066 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002067 ATRACE_CALL();
2068
Ian Elliott62c48c92017-01-20 13:13:20 -07002069 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002070 VkResult result = VK_SUCCESS;
2071
Ian Elliott3568bfe2019-05-03 15:54:46 -06002072 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002073
2074 return result;
2075}
2076
2077VKAPI_ATTR
2078VkResult GetPastPresentationTimingGOOGLE(
2079 VkDevice,
2080 VkSwapchainKHR swapchain_handle,
2081 uint32_t* count,
2082 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002083 ATRACE_CALL();
2084
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002085 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002086 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2087 return VK_ERROR_OUT_OF_DATE_KHR;
2088 }
2089
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002090 ANativeWindow* window = swapchain.surface.window.get();
2091 VkResult result = VK_SUCCESS;
2092
2093 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07002094 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002095 native_window_enable_frame_timestamps(window, true);
2096 swapchain.frame_timestamps_enabled = true;
2097 }
2098
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002099 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07002100 // Get the latest ready timing count before copying, since the copied
2101 // timing info will be erased in copy_ready_timings function.
2102 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07002103 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002104 // Check the *count here against the recorded ready timing count, since
2105 // *count can be overwritten per spec describes.
2106 if (*count < n) {
2107 result = VK_INCOMPLETE;
2108 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002109 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07002110 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002111 }
2112
2113 return result;
2114}
2115
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002116VKAPI_ATTR
2117VkResult GetSwapchainStatusKHR(
2118 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13002119 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002120 ATRACE_CALL();
2121
Chris Forbes4e18ba82017-01-20 12:50:17 +13002122 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002123 VkResult result = VK_SUCCESS;
2124
Chris Forbes4e18ba82017-01-20 12:50:17 +13002125 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2126 return VK_ERROR_OUT_OF_DATE_KHR;
2127 }
2128
Yiwei Zhanga885c062019-10-24 12:07:57 -07002129 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002130
2131 return result;
2132}
2133
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002134VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002135 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002136 uint32_t swapchainCount,
2137 const VkSwapchainKHR* pSwapchains,
2138 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002139 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002140
2141 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
2142 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
2143 if (!swapchain)
2144 continue;
2145
2146 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
2147
2148 ANativeWindow* window = swapchain->surface.window.get();
2149
2150 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
2151 const android_smpte2086_metadata smpteMetdata = {
2152 {vulkanMetadata.displayPrimaryRed.x,
2153 vulkanMetadata.displayPrimaryRed.y},
2154 {vulkanMetadata.displayPrimaryGreen.x,
2155 vulkanMetadata.displayPrimaryGreen.y},
2156 {vulkanMetadata.displayPrimaryBlue.x,
2157 vulkanMetadata.displayPrimaryBlue.y},
2158 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
2159 vulkanMetadata.maxLuminance,
2160 vulkanMetadata.minLuminance};
2161 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
2162
2163 const android_cta861_3_metadata cta8613Metadata = {
2164 vulkanMetadata.maxContentLightLevel,
2165 vulkanMetadata.maxFrameAverageLightLevel};
2166 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
2167 }
2168
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002169 return;
2170}
2171
Yiwei Zhang0f475222019-04-11 19:38:00 -07002172static void InterceptBindImageMemory2(
2173 uint32_t bind_info_count,
2174 const VkBindImageMemoryInfo* bind_infos,
2175 std::vector<VkNativeBufferANDROID>* out_native_buffers,
2176 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
2177 out_native_buffers->clear();
2178 out_bind_infos->clear();
2179
2180 if (!bind_info_count)
2181 return;
2182
2183 std::unordered_set<uint32_t> intercepted_indexes;
2184
2185 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2186 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2187 bind_infos[idx].pNext);
2188 while (info &&
2189 info->sType !=
2190 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
2191 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2192 info->pNext);
2193 }
2194
2195 if (!info)
2196 continue;
2197
2198 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
2199 "swapchain handle must not be NULL");
2200 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
2201 ALOG_ASSERT(
2202 info->imageIndex < swapchain->num_images,
2203 "imageIndex must be less than the number of images in swapchain");
2204
2205 ANativeWindowBuffer* buffer =
2206 swapchain->images[info->imageIndex].buffer.get();
2207 VkNativeBufferANDROID native_buffer = {
2208#pragma clang diagnostic push
2209#pragma clang diagnostic ignored "-Wold-style-cast"
2210 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2211#pragma clang diagnostic pop
2212 .pNext = bind_infos[idx].pNext,
2213 .handle = buffer->handle,
2214 .stride = buffer->stride,
2215 .format = buffer->format,
2216 .usage = int(buffer->usage),
2217 };
2218 // Reserve enough space to avoid letting re-allocation invalidate the
2219 // addresses of the elements inside.
2220 out_native_buffers->reserve(bind_info_count);
2221 out_native_buffers->emplace_back(native_buffer);
2222
2223 // Reserve the space now since we know how much is needed now.
2224 out_bind_infos->reserve(bind_info_count);
2225 out_bind_infos->emplace_back(bind_infos[idx]);
2226 out_bind_infos->back().pNext = &out_native_buffers->back();
2227
2228 intercepted_indexes.insert(idx);
2229 }
2230
2231 if (intercepted_indexes.empty())
2232 return;
2233
2234 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2235 if (intercepted_indexes.count(idx))
2236 continue;
2237 out_bind_infos->emplace_back(bind_infos[idx]);
2238 }
2239}
2240
Yiwei Zhang23143102019-04-10 18:24:05 -07002241VKAPI_ATTR
2242VkResult BindImageMemory2(VkDevice device,
2243 uint32_t bindInfoCount,
2244 const VkBindImageMemoryInfo* pBindInfos) {
2245 ATRACE_CALL();
2246
Yiwei Zhang0f475222019-04-11 19:38:00 -07002247 // out_native_buffers is for maintaining the lifecycle of the constructed
2248 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2249 std::vector<VkNativeBufferANDROID> out_native_buffers;
2250 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2251 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2252 &out_bind_infos);
2253 return GetData(device).driver.BindImageMemory2(
2254 device, bindInfoCount,
2255 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002256}
2257
2258VKAPI_ATTR
2259VkResult BindImageMemory2KHR(VkDevice device,
2260 uint32_t bindInfoCount,
2261 const VkBindImageMemoryInfo* pBindInfos) {
2262 ATRACE_CALL();
2263
Yiwei Zhang0f475222019-04-11 19:38:00 -07002264 std::vector<VkNativeBufferANDROID> out_native_buffers;
2265 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2266 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2267 &out_bind_infos);
2268 return GetData(device).driver.BindImageMemory2KHR(
2269 device, bindInfoCount,
2270 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002271}
2272
Chia-I Wu62262232016-03-26 07:06:44 +08002273} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002274} // namespace vulkan