blob: 48a75c5c05fd78bedf5ae42f8f8317ce202243c0 [file] [log] [blame]
Jesse Hallb1352bc2015-09-04 16:12:33 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang0f475222019-04-11 19:38:00 -070019#include <android/hardware/graphics/common/1.0/types.h>
Jesse Hall79927812017-03-23 11:03:23 -070020#include <grallocusage/GrallocUsageConversion.h>
Yiwei Zhang69395cd2019-07-03 16:55:39 -070021#include <graphicsenv/GraphicsEnv.h>
John Reck97678d42022-08-23 11:24:51 -040022#include <hardware/gralloc.h>
23#include <hardware/gralloc1.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070024#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070025#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070026#include <system/window.h>
27#include <ui/BufferQueueDefs.h>
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -050028#include <ui/DebugUtils.h>
29#include <ui/PixelFormat.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080030#include <utils/StrongPointer.h>
Yiwei Zhang705c2e62019-12-18 23:12:43 -080031#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080032#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070033
34#include <algorithm>
35#include <unordered_set>
36#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070037
Chia-I Wu4a6a9162016-03-26 07:17:34 +080038#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070039
Daniel Kochf25f5bb2017-10-05 00:26:58 -040040using android::hardware::graphics::common::V1_0::BufferUsage;
41
Chia-I Wu62262232016-03-26 07:06:44 +080042namespace vulkan {
43namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070044
Jesse Halld7b994a2015-09-07 14:17:37 -070045namespace {
46
John Reck97678d42022-08-23 11:24:51 -040047static uint64_t convertGralloc1ToBufferUsage(uint64_t producerUsage,
48 uint64_t consumerUsage) {
49 static_assert(uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) ==
50 uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN),
51 "expected ConsumerUsage and ProducerUsage CPU_READ_OFTEN "
52 "bits to match");
53 uint64_t merged = producerUsage | consumerUsage;
54 if ((merged & (GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN)) ==
55 GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) {
56 merged &= ~uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN);
57 merged |= BufferUsage::CPU_READ_OFTEN;
58 }
59 if ((merged & (GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) ==
60 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) {
61 merged &= ~uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN);
62 merged |= BufferUsage::CPU_WRITE_OFTEN;
63 }
64 return merged;
65}
66
Jesse Hall55bc0972016-02-23 16:43:29 -080067const VkSurfaceTransformFlagsKHR kSupportedTransforms =
68 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
69 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
70 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
71 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070072 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
73 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
74 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
75 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080076 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
77
78VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
79 // Native and Vulkan transforms are isomorphic, but are represented
80 // differently. Vulkan transforms are built up of an optional horizontal
81 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
82 // transforms are built up from a horizontal flip, vertical flip, and
83 // 90-degree rotation, all optional but always in that order.
84
Jesse Hall55bc0972016-02-23 16:43:29 -080085 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070086 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080087 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070088 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
89 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
90 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
91 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
92 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080093 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070094 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080095 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070096 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
97 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
98 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
99 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
100 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -0800101 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
102 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
103 default:
104 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
105 }
106}
107
Yiwei Zhang70a21962019-05-31 17:26:52 -0700108int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
109 switch (transform) {
110 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
111 return NATIVE_WINDOW_TRANSFORM_ROT_90;
112 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
113 return NATIVE_WINDOW_TRANSFORM_ROT_180;
114 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
115 return NATIVE_WINDOW_TRANSFORM_ROT_270;
116 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
117 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
118 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
119 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
120 NATIVE_WINDOW_TRANSFORM_ROT_90;
121 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
122 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
123 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
124 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
125 NATIVE_WINDOW_TRANSFORM_ROT_90;
126 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
127 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
128 default:
129 return 0;
130 }
131}
132
Jesse Hall178b6962016-02-24 15:39:50 -0800133int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
134 switch (transform) {
135 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
136 return NATIVE_WINDOW_TRANSFORM_ROT_270;
137 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
138 return NATIVE_WINDOW_TRANSFORM_ROT_180;
139 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
140 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700141 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
142 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
143 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
144 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
145 NATIVE_WINDOW_TRANSFORM_ROT_90;
146 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
147 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
148 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
149 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
150 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800151 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
152 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
153 default:
154 return 0;
155 }
156}
157
Ian Elliott8a977262017-01-19 09:05:58 -0700158class TimingInfo {
159 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800160 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700161 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800162 native_frame_id_(nativeFrameId) {}
163 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700164 return (timestamp_desired_present_time_ !=
165 NATIVE_WINDOW_TIMESTAMP_PENDING &&
166 timestamp_actual_present_time_ !=
167 NATIVE_WINDOW_TIMESTAMP_PENDING &&
168 timestamp_render_complete_time_ !=
169 NATIVE_WINDOW_TIMESTAMP_PENDING &&
170 timestamp_composition_latch_time_ !=
171 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700172 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700173 void calculate(int64_t rdur) {
174 bool anyTimestampInvalid =
175 (timestamp_actual_present_time_ ==
176 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
177 (timestamp_render_complete_time_ ==
178 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
179 (timestamp_composition_latch_time_ ==
180 NATIVE_WINDOW_TIMESTAMP_INVALID);
181 if (anyTimestampInvalid) {
182 ALOGE("Unexpectedly received invalid timestamp.");
183 vals_.actualPresentTime = 0;
184 vals_.earliestPresentTime = 0;
185 vals_.presentMargin = 0;
186 return;
187 }
188
189 vals_.actualPresentTime =
190 static_cast<uint64_t>(timestamp_actual_present_time_);
191 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700192 timestamp_render_complete_time_);
193 // Calculate vals_.earliestPresentTime, and potentially adjust
194 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
195 // is vals_.actualPresentTime. If we can subtract rdur (the duration
196 // of a refresh cycle) from vals_.earliestPresentTime (and also from
197 // vals_.presentMargin) and still leave a positive margin, then we can
198 // report to the application that it could have presented earlier than
199 // it did (per the extension specification). If for some reason, we
200 // can do this subtraction repeatedly, we do, since
201 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700202 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700203 while ((margin > rdur) &&
204 ((early_time - rdur) > timestamp_composition_latch_time_)) {
205 early_time -= rdur;
206 margin -= rdur;
207 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700208 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
209 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700210 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800211 void get_values(VkPastPresentationTimingGOOGLE* values) const {
212 *values = vals_;
213 }
Ian Elliott8a977262017-01-19 09:05:58 -0700214
215 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800216 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700217
Brian Anderson1049d1d2016-12-16 17:25:57 -0800218 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700219 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
220 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
221 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
222 int64_t timestamp_composition_latch_time_
223 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700224};
225
Jesse Hall1356b0d2015-11-23 17:24:58 -0800226struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800227 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700228 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700229 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800230};
231
232VkSurfaceKHR HandleFromSurface(Surface* surface) {
233 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
234}
235
236Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800237 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800238}
239
Ian Elliott8a977262017-01-19 09:05:58 -0700240// Maximum number of TimingInfo structs to keep per swapchain:
241enum { MAX_TIMING_INFOS = 10 };
242// Minimum number of frames to look for in the past (so we don't cause
243// syncronous requests to Surface Flinger):
244enum { MIN_NUM_FRAMES_AGO = 5 };
245
Jesse Hall1356b0d2015-11-23 17:24:58 -0800246struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700247 Swapchain(Surface& surface_,
248 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700249 VkPresentModeKHR present_mode,
250 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700251 : surface(surface_),
252 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700253 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700254 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300255 frame_timestamps_enabled(false),
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800256 acquire_next_image_timeout(-1),
Chris Forbesf8835642017-03-30 19:31:40 +1300257 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800258 present_mode ==
259 VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700260 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700261 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700262 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700263 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700264 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600265 uint64_t get_refresh_duration()
266 {
267 ANativeWindow* window = surface.window.get();
268 native_window_get_refresh_cycle_duration(
269 window,
270 &refresh_duration);
271 return static_cast<uint64_t>(refresh_duration);
272
273 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800274
275 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700276 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700277 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700278 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700279 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700280 int64_t refresh_duration;
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800281 nsecs_t acquire_next_image_timeout;
Chris Forbesf8835642017-03-30 19:31:40 +1300282 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700283
284 struct Image {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700285 Image()
286 : image(VK_NULL_HANDLE),
287 dequeue_fence(-1),
288 release_fence(-1),
289 dequeued(false) {}
Jesse Halld7b994a2015-09-07 14:17:37 -0700290 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800291 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700292 // The fence is only valid when the buffer is dequeued, and should be
293 // -1 any other time. When valid, we own the fd, and must ensure it is
294 // closed: either by closing it explicitly when queueing the buffer,
295 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
296 int dequeue_fence;
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700297 // This fence is a dup of the sync fd returned from the driver via
298 // vkQueueSignalReleaseImageANDROID upon vkQueuePresentKHR. We must
299 // ensure it is closed upon re-presenting or releasing the image.
300 int release_fence;
Jesse Halld7b994a2015-09-07 14:17:37 -0700301 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800302 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700303
Yiwei Zhang5e862202019-06-21 14:59:16 -0700304 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700305};
306
307VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
308 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
309}
310
311Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800312 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700313}
314
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700315static bool IsFencePending(int fd) {
316 if (fd < 0)
317 return false;
318
319 errno = 0;
320 return sync_wait(fd, 0 /* timeout */) == -1 && errno == ETIME;
321}
322
Jesse Halldc225072016-05-30 22:40:14 -0700323void ReleaseSwapchainImage(VkDevice device,
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000324 bool shared_present,
Jesse Halldc225072016-05-30 22:40:14 -0700325 ANativeWindow* window,
326 int release_fence,
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700327 Swapchain::Image& image,
328 bool defer_if_pending) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700329 ATRACE_CALL();
330
Jesse Halldc225072016-05-30 22:40:14 -0700331 ALOG_ASSERT(release_fence == -1 || image.dequeued,
332 "ReleaseSwapchainImage: can't provide a release fence for "
333 "non-dequeued images");
334
335 if (image.dequeued) {
336 if (release_fence >= 0) {
337 // We get here from vkQueuePresentKHR. The application is
338 // responsible for creating an execution dependency chain from
339 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
340 // (release_fence), so we can drop the dequeue_fence here.
341 if (image.dequeue_fence >= 0)
342 close(image.dequeue_fence);
343 } else {
344 // We get here during swapchain destruction, or various serious
345 // error cases e.g. when we can't create the release_fence during
346 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
347 // have already signalled, since the swapchain images are supposed
348 // to be idle before the swapchain is destroyed. In error cases,
349 // there may be rendering in flight to the image, but since we
350 // weren't able to create a release_fence, waiting for the
351 // dequeue_fence is about the best we can do.
352 release_fence = image.dequeue_fence;
353 }
354 image.dequeue_fence = -1;
355
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000356 // It's invalid to call cancelBuffer on a shared buffer
357 if (window && !shared_present) {
Jesse Halldc225072016-05-30 22:40:14 -0700358 window->cancelBuffer(window, image.buffer.get(), release_fence);
359 } else {
360 if (release_fence >= 0) {
361 sync_wait(release_fence, -1 /* forever */);
362 close(release_fence);
363 }
364 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700365 release_fence = -1;
Jesse Halldc225072016-05-30 22:40:14 -0700366 image.dequeued = false;
367 }
368
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700369 if (defer_if_pending && IsFencePending(image.release_fence))
370 return;
371
372 if (image.release_fence >= 0) {
373 close(image.release_fence);
374 image.release_fence = -1;
375 }
376
Jesse Halldc225072016-05-30 22:40:14 -0700377 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700378 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700379 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700380 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700381 image.image = VK_NULL_HANDLE;
382 }
383
384 image.buffer.clear();
385}
386
387void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
388 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
389 return;
Jesse Halldc225072016-05-30 22:40:14 -0700390 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000391 if (!swapchain->images[i].dequeued) {
392 ReleaseSwapchainImage(device, swapchain->shared, nullptr, -1,
393 swapchain->images[i], true);
394 }
Jesse Halldc225072016-05-30 22:40:14 -0700395 }
396 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700397 swapchain->timing.clear();
398}
399
400uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800401 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
402 return 0;
403 }
Ian Elliott8a977262017-01-19 09:05:58 -0700404
Brian Anderson1049d1d2016-12-16 17:25:57 -0800405 uint32_t num_ready = 0;
406 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
407 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700408 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800409 if (ti.ready()) {
410 // This TimingInfo is ready to be reported to the user. Add it
411 // to the num_ready.
412 num_ready++;
413 continue;
414 }
415 // This TimingInfo is not yet ready to be reported to the user,
416 // and so we should look for any available timestamps that
417 // might make it ready.
418 int64_t desired_present_time = 0;
419 int64_t render_complete_time = 0;
420 int64_t composition_latch_time = 0;
421 int64_t actual_present_time = 0;
422 // Obtain timestamps:
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800423 int err = native_window_get_frame_timestamps(
Brian Anderson1049d1d2016-12-16 17:25:57 -0800424 swapchain.surface.window.get(), ti.native_frame_id_,
425 &desired_present_time, &render_complete_time,
426 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700427 nullptr, //&first_composition_start_time,
428 nullptr, //&last_composition_start_time,
429 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800430 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700431 nullptr, //&dequeue_ready_time,
432 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800433
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800434 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800435 continue;
436 }
437
438 // Record the timestamp(s) we received, and then see if this TimingInfo
439 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700440 ti.timestamp_desired_present_time_ = desired_present_time;
441 ti.timestamp_actual_present_time_ = actual_present_time;
442 ti.timestamp_render_complete_time_ = render_complete_time;
443 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800444
445 if (ti.ready()) {
446 // The TimingInfo has received enough timestamps, and should now
447 // use those timestamps to calculate the info that should be
448 // reported to the user:
449 ti.calculate(swapchain.refresh_duration);
450 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700451 }
452 }
453 return num_ready;
454}
455
Ian Elliott8a977262017-01-19 09:05:58 -0700456void copy_ready_timings(Swapchain& swapchain,
457 uint32_t* count,
458 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800459 if (swapchain.timing.empty()) {
460 *count = 0;
461 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700462 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800463
464 size_t last_ready = swapchain.timing.size() - 1;
465 while (!swapchain.timing[last_ready].ready()) {
466 if (last_ready == 0) {
467 *count = 0;
468 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700469 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800470 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700471 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800472
473 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700474 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800475 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
476 const TimingInfo& ti = swapchain.timing[i];
477 if (ti.ready()) {
478 ti.get_values(&timings[num_copied]);
479 num_copied++;
480 }
481 num_to_remove++;
482 }
483
484 // Discard old frames that aren't ready if newer frames are ready.
485 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700486 swapchain.timing.erase(swapchain.timing.begin(),
487 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800488
Ian Elliott8a977262017-01-19 09:05:58 -0700489 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700490}
491
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500492android::PixelFormat GetNativePixelFormat(VkFormat format) {
493 android::PixelFormat native_format = android::PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700494 switch (format) {
495 case VK_FORMAT_R8G8B8A8_UNORM:
496 case VK_FORMAT_R8G8B8A8_SRGB:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500497 native_format = android::PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700498 break;
499 case VK_FORMAT_R5G6B5_UNORM_PACK16:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500500 native_format = android::PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700501 break;
502 case VK_FORMAT_R16G16B16A16_SFLOAT:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500503 native_format = android::PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700504 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800505 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500506 native_format = android::PIXEL_FORMAT_RGBA_1010102;
507 break;
508 case VK_FORMAT_R8_UNORM:
509 native_format = android::PIXEL_FORMAT_R_8;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700510 break;
511 default:
512 ALOGV("unsupported swapchain format %d", format);
513 break;
514 }
515 return native_format;
516}
517
518android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
519 switch (colorspace) {
520 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
521 return HAL_DATASPACE_V0_SRGB;
522 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
523 return HAL_DATASPACE_DISPLAY_P3;
524 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
525 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600526 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
527 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700528 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
529 return HAL_DATASPACE_DCI_P3_LINEAR;
530 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
531 return HAL_DATASPACE_DCI_P3;
532 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
533 return HAL_DATASPACE_V0_SRGB_LINEAR;
534 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
535 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600536 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
537 return HAL_DATASPACE_BT2020_LINEAR;
538 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700539 return static_cast<android_dataspace>(
540 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
541 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600542 case VK_COLOR_SPACE_DOLBYVISION_EXT:
543 return static_cast<android_dataspace>(
544 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
545 HAL_DATASPACE_RANGE_FULL);
546 case VK_COLOR_SPACE_HDR10_HLG_EXT:
547 return static_cast<android_dataspace>(
548 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
549 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700550 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
551 return static_cast<android_dataspace>(
552 HAL_DATASPACE_STANDARD_ADOBE_RGB |
553 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
554 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
555 return HAL_DATASPACE_ADOBE_RGB;
556
557 // Pass through is intended to allow app to provide data that is passed
558 // to the display system without modification.
559 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
560 return HAL_DATASPACE_ARBITRARY;
561
562 default:
563 // This indicates that we don't know about the
564 // dataspace specified and we should indicate that
565 // it's unsupported
566 return HAL_DATASPACE_UNKNOWN;
567 }
568}
569
Jesse Halld7b994a2015-09-07 14:17:37 -0700570} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700571
Jesse Halle1b12782015-11-30 11:27:32 -0800572VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800573VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800574 VkInstance instance,
575 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
576 const VkAllocationCallbacks* allocator,
577 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800578 ATRACE_CALL();
579
Jesse Hall1f91d392015-12-11 16:28:44 -0800580 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800581 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800582 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
583 alignof(Surface),
584 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800585 if (!mem)
586 return VK_ERROR_OUT_OF_HOST_MEMORY;
587 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700588
Chia-I Wue8e689f2016-04-18 08:21:31 +0800589 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700590 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700591 int err = native_window_get_consumer_usage(surface->window.get(),
592 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800593 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700594 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
595 strerror(-err), err);
596 surface->~Surface();
597 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700598 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700599 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700600
Yiwei Zhang6435b322018-05-08 11:12:17 -0700601 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800602 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800603 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800604 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
605 err);
606 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800607 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700608 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800609 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700610
Jesse Hall1356b0d2015-11-23 17:24:58 -0800611 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700612 return VK_SUCCESS;
613}
614
Jesse Halle1b12782015-11-30 11:27:32 -0800615VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800616void DestroySurfaceKHR(VkInstance instance,
617 VkSurfaceKHR surface_handle,
618 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800619 ATRACE_CALL();
620
Jesse Hall1356b0d2015-11-23 17:24:58 -0800621 Surface* surface = SurfaceFromHandle(surface_handle);
622 if (!surface)
623 return;
624 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700625 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700626 "destroyed VkSurfaceKHR 0x%" PRIx64
627 " has active VkSwapchainKHR 0x%" PRIx64,
628 reinterpret_cast<uint64_t>(surface_handle),
629 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800630 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800631 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800632 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800633 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800634}
635
Jesse Halle1b12782015-11-30 11:27:32 -0800636VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800637VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
638 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000639 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800640 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000641 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800642 return VK_SUCCESS;
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 GetPhysicalDeviceSurfaceCapabilitiesKHR(
Ian Elliott1ce053f2022-03-16 09:49:53 -0600647 VkPhysicalDevice pdev,
Jesse Hallb00daad2015-11-29 19:46:20 -0800648 VkSurfaceKHR surface,
649 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800650 ATRACE_CALL();
651
Jesse Halld7b994a2015-09-07 14:17:37 -0700652 int err;
Jesse Halld7b994a2015-09-07 14:17:37 -0700653 int width, height;
Jesse Hall55bc0972016-02-23 16:43:29 -0800654 int transform_hint;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800655 int max_buffer_count;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600656 if (surface == VK_NULL_HANDLE) {
657 const InstanceData& instance_data = GetData(pdev);
658 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
659 bool surfaceless_enabled =
660 instance_data.hook_extensions.test(surfaceless);
661 if (!surfaceless_enabled) {
662 // It is an error to pass a surface==VK_NULL_HANDLE unless the
663 // VK_GOOGLE_surfaceless_query extension is enabled
664 return VK_ERROR_SURFACE_LOST_KHR;
665 }
666 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
667 // extension for this function is for
668 // VkSurfaceProtectedCapabilitiesKHR::supportsProtected. The following
669 // four values cannot be known without a surface. Default values will
670 // be supplied anyway, but cannot be relied upon.
Ian Elliottb4576372022-09-08 15:13:39 -0600671 width = 0xFFFFFFFF;
672 height = 0xFFFFFFFF;
673 transform_hint = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
674 capabilities->minImageCount = 0xFFFFFFFF;
675 capabilities->maxImageCount = 0xFFFFFFFF;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600676 } else {
677 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
678
679 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
680 if (err != android::OK) {
681 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
682 strerror(-err), err);
683 return VK_ERROR_SURFACE_LOST_KHR;
684 }
685 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
686 if (err != android::OK) {
687 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
688 strerror(-err), err);
689 return VK_ERROR_SURFACE_LOST_KHR;
690 }
691
692 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
693 &transform_hint);
694 if (err != android::OK) {
695 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
696 strerror(-err), err);
697 return VK_ERROR_SURFACE_LOST_KHR;
698 }
699
700 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT,
701 &max_buffer_count);
702 if (err != android::OK) {
703 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
704 strerror(-err), err);
705 return VK_ERROR_SURFACE_LOST_KHR;
706 }
Ian Elliottb4576372022-09-08 15:13:39 -0600707 capabilities->minImageCount = std::min(max_buffer_count, 3);
708 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800709 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700710
Jesse Hallfe2662d2016-02-09 13:26:59 -0800711 capabilities->currentExtent =
712 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
713
Yiwei Zhang70a21962019-05-31 17:26:52 -0700714 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800715 capabilities->minImageExtent = VkExtent2D{1, 1};
716 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700717
Trevor David Blackc00a1212022-11-14 21:13:14 +0000718 if (capabilities->maxImageExtent.height <
719 capabilities->currentExtent.height) {
720 capabilities->maxImageExtent.height =
721 capabilities->currentExtent.height;
722 }
723
724 if (capabilities->maxImageExtent.width <
725 capabilities->currentExtent.width) {
726 capabilities->maxImageExtent.width = capabilities->currentExtent.width;
727 }
728
Jesse Hallfe2662d2016-02-09 13:26:59 -0800729 capabilities->maxImageArrayLayers = 1;
730
Jesse Hall55bc0972016-02-23 16:43:29 -0800731 capabilities->supportedTransforms = kSupportedTransforms;
732 capabilities->currentTransform =
733 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700734
Jesse Hallfe2662d2016-02-09 13:26:59 -0800735 // On Android, window composition is a WindowManager property, not something
736 // associated with the bufferqueue. It can't be changed from here.
737 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700738
Jesse Hallb00daad2015-11-29 19:46:20 -0800739 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800740 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
741 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
742 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700743 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
744
Jesse Hallb1352bc2015-09-04 16:12:33 -0700745 return VK_SUCCESS;
746}
747
Jesse Halle1b12782015-11-30 11:27:32 -0800748VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700749VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
750 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800751 uint32_t* count,
752 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800753 ATRACE_CALL();
754
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700755 const InstanceData& instance_data = GetData(pdev);
756
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700757 bool wide_color_support = false;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600758 uint64_t consumer_usage = 0;
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000759 bool colorspace_ext =
Ian Elliottf6df08e2022-03-16 21:27:49 -0600760 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600761 if (surface_handle == VK_NULL_HANDLE) {
762 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
763 bool surfaceless_enabled =
764 instance_data.hook_extensions.test(surfaceless);
765 if (!surfaceless_enabled) {
766 return VK_ERROR_SURFACE_LOST_KHR;
767 }
768 // Support for VK_GOOGLE_surfaceless_query. The EGL loader
769 // unconditionally supports wide color formats, even if they will cause
770 // a SurfaceFlinger fallback. Based on that, wide_color_support will be
771 // set to true in this case.
772 wide_color_support = true;
773
774 // TODO(b/203826952): research proper value; temporarily use the
775 // values seen on Pixel
776 consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
777 } else {
778 Surface& surface = *SurfaceFromHandle(surface_handle);
779 int err = native_window_get_wide_color_support(surface.window.get(),
780 &wide_color_support);
781 if (err) {
782 return VK_ERROR_SURFACE_LOST_KHR;
783 }
784 ALOGV("wide_color_support is: %d", wide_color_support);
785
786 consumer_usage = surface.consumer_usage;
Ian Elliott6ba85d92022-02-18 16:44:58 -0700787 }
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000788 wide_color_support = wide_color_support && colorspace_ext;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700789
Charlie Lao324191f2019-12-13 11:03:16 -0800790 AHardwareBuffer_Desc desc = {};
791 desc.width = 1;
792 desc.height = 1;
793 desc.layers = 1;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600794 desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
Charlie Lao324191f2019-12-13 11:03:16 -0800795 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
796
797 // We must support R8G8B8A8
798 std::vector<VkSurfaceFormatKHR> all_formats = {
799 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700800 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700801 };
Charlie Lao324191f2019-12-13 11:03:16 -0800802
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000803 if (colorspace_ext) {
804 all_formats.emplace_back(VkSurfaceFormatKHR{
Jason Macnakca506332022-11-09 11:00:36 -0800805 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT});
806 all_formats.emplace_back(VkSurfaceFormatKHR{
807 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_PASS_THROUGH_EXT});
808 all_formats.emplace_back(VkSurfaceFormatKHR{
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000809 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_BT709_LINEAR_EXT});
810 }
811
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700812 if (wide_color_support) {
Charlie Lao324191f2019-12-13 11:03:16 -0800813 all_formats.emplace_back(VkSurfaceFormatKHR{
814 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
815 all_formats.emplace_back(VkSurfaceFormatKHR{
816 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
817 }
818
Ian Elliott1ce053f2022-03-16 09:49:53 -0600819 // NOTE: Any new formats that are added must be coordinated across different
820 // Android users. This includes the ANGLE team (a layered implementation of
821 // OpenGL-ES).
822
Charlie Lao324191f2019-12-13 11:03:16 -0800823 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
824 if (AHardwareBuffer_isSupported(&desc)) {
825 all_formats.emplace_back(VkSurfaceFormatKHR{
826 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800827 if (colorspace_ext) {
828 all_formats.emplace_back(
829 VkSurfaceFormatKHR{VK_FORMAT_R5G6B5_UNORM_PACK16,
830 VK_COLOR_SPACE_PASS_THROUGH_EXT});
831 }
Charlie Lao324191f2019-12-13 11:03:16 -0800832 }
833
834 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
835 if (AHardwareBuffer_isSupported(&desc)) {
836 all_formats.emplace_back(VkSurfaceFormatKHR{
837 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800838 if (colorspace_ext) {
839 all_formats.emplace_back(
840 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
841 VK_COLOR_SPACE_PASS_THROUGH_EXT});
842 }
Charlie Lao324191f2019-12-13 11:03:16 -0800843 if (wide_color_support) {
844 all_formats.emplace_back(
845 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
846 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
847 all_formats.emplace_back(
848 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
849 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
850 }
851 }
852
853 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
854 if (AHardwareBuffer_isSupported(&desc)) {
855 all_formats.emplace_back(
856 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
857 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800858 if (colorspace_ext) {
859 all_formats.emplace_back(
860 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
861 VK_COLOR_SPACE_PASS_THROUGH_EXT});
862 }
Charlie Lao324191f2019-12-13 11:03:16 -0800863 if (wide_color_support) {
864 all_formats.emplace_back(
865 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
866 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
867 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700868 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700869
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500870 desc.format = AHARDWAREBUFFER_FORMAT_R8_UNORM;
871 if (AHardwareBuffer_isSupported(&desc)) {
Jason Macnakca506332022-11-09 11:00:36 -0800872 if (colorspace_ext) {
873 all_formats.emplace_back(VkSurfaceFormatKHR{
874 VK_FORMAT_R8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT});
875 }
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500876 }
877
Ian Elliott6ba85d92022-02-18 16:44:58 -0700878 // NOTE: Any new formats that are added must be coordinated across different
879 // Android users. This includes the ANGLE team (a layered implementation of
880 // OpenGL-ES).
881
Jesse Halld7b994a2015-09-07 14:17:37 -0700882 VkResult result = VK_SUCCESS;
883 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800884 uint32_t transfer_count = all_formats.size();
885 if (transfer_count > *count) {
886 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700887 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700888 }
Charlie Lao324191f2019-12-13 11:03:16 -0800889 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
890 formats);
891 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700892 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800893 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700894 }
Charlie Lao324191f2019-12-13 11:03:16 -0800895
Jesse Halld7b994a2015-09-07 14:17:37 -0700896 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700897}
898
Jesse Halle1b12782015-11-30 11:27:32 -0800899VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300900VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
901 VkPhysicalDevice physicalDevice,
902 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
903 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800904 ATRACE_CALL();
905
Chris Forbes2452cf72017-03-16 16:30:17 +1300906 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
907 physicalDevice, pSurfaceInfo->surface,
908 &pSurfaceCapabilities->surfaceCapabilities);
909
Chris Forbes06bc0092017-03-16 16:46:05 +1300910 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
911 while (caps->pNext) {
912 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
913
914 switch (caps->sType) {
915 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
916 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
917 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
918 caps);
919 // Claim same set of usage flags are supported for
920 // shared present modes as for other modes.
921 shared_caps->sharedPresentSupportedUsageFlags =
922 pSurfaceCapabilities->surfaceCapabilities
923 .supportedUsageFlags;
924 } break;
925
Ian Elliottbb67b242022-03-16 09:52:28 -0600926 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
927 VkSurfaceProtectedCapabilitiesKHR* protected_caps =
928 reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(caps);
929 protected_caps->supportsProtected = VK_TRUE;
930 } break;
931
Chris Forbes06bc0092017-03-16 16:46:05 +1300932 default:
933 // Ignore all other extension structs
934 break;
935 }
936 }
937
Chris Forbes2452cf72017-03-16 16:30:17 +1300938 return result;
939}
940
941VKAPI_ATTR
942VkResult GetPhysicalDeviceSurfaceFormats2KHR(
943 VkPhysicalDevice physicalDevice,
944 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
945 uint32_t* pSurfaceFormatCount,
946 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800947 ATRACE_CALL();
948
Chris Forbes2452cf72017-03-16 16:30:17 +1300949 if (!pSurfaceFormats) {
950 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
951 pSurfaceInfo->surface,
952 pSurfaceFormatCount, nullptr);
953 } else {
954 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
955 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700956 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300957 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
958 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700959 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300960
961 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
Trevor David Black2cc44682022-03-09 00:31:38 +0000962 const auto& driver = GetData(physicalDevice).driver;
963
Chris Forbes2452cf72017-03-16 16:30:17 +1300964 // marshal results individually due to stride difference.
Chris Forbes2452cf72017-03-16 16:30:17 +1300965 uint32_t formats_to_marshal = *pSurfaceFormatCount;
966 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
967 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
Trevor David Black2cc44682022-03-09 00:31:38 +0000968
969 // Query the compression properties for the surface format
970 if (pSurfaceFormats[i].pNext) {
971 VkImageCompressionPropertiesEXT* surfaceCompressionProps =
972 reinterpret_cast<VkImageCompressionPropertiesEXT*>(
973 pSurfaceFormats[i].pNext);
974
975 if (surfaceCompressionProps &&
976 driver.GetPhysicalDeviceImageFormatProperties2KHR) {
977 VkPhysicalDeviceImageFormatInfo2 imageFormatInfo = {};
978 imageFormatInfo.sType =
979 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2;
980 imageFormatInfo.format =
981 pSurfaceFormats[i].surfaceFormat.format;
982 imageFormatInfo.pNext = nullptr;
983
984 VkImageCompressionControlEXT compressionControl = {};
985 compressionControl.sType =
986 VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT;
987 compressionControl.pNext = imageFormatInfo.pNext;
988
989 imageFormatInfo.pNext = &compressionControl;
990
991 VkImageCompressionPropertiesEXT compressionProps = {};
992 compressionProps.sType =
993 VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT;
994 compressionProps.pNext = nullptr;
995
996 VkImageFormatProperties2KHR imageFormatProps = {};
997 imageFormatProps.sType =
998 VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2_KHR;
999 imageFormatProps.pNext = &compressionProps;
1000
1001 VkResult compressionRes =
1002 driver.GetPhysicalDeviceImageFormatProperties2KHR(
1003 physicalDevice, &imageFormatInfo,
1004 &imageFormatProps);
1005 if (compressionRes == VK_SUCCESS) {
1006 surfaceCompressionProps->imageCompressionFlags =
1007 compressionProps.imageCompressionFlags;
1008 surfaceCompressionProps
1009 ->imageCompressionFixedRateFlags =
1010 compressionProps.imageCompressionFixedRateFlags;
1011 } else {
1012 return compressionRes;
1013 }
1014 }
1015 }
Chris Forbes2452cf72017-03-16 16:30:17 +13001016 }
1017 }
1018
1019 return result;
1020 }
1021}
1022
1023VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +13001024VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -08001025 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +08001026 uint32_t* count,
1027 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001028 ATRACE_CALL();
1029
Yiwei Zhange4a559c2018-02-15 11:27:36 -08001030 int err;
1031 int query_value;
Ian Elliotte7f036c2022-03-15 16:49:21 -06001032 std::vector<VkPresentModeKHR> present_modes;
Ian Elliott1ce053f2022-03-16 09:49:53 -06001033 if (surface == VK_NULL_HANDLE) {
1034 const InstanceData& instance_data = GetData(pdev);
1035 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
1036 bool surfaceless_enabled =
1037 instance_data.hook_extensions.test(surfaceless);
1038 if (!surfaceless_enabled) {
1039 return VK_ERROR_SURFACE_LOST_KHR;
1040 }
1041 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
1042 // extension for this function is for
1043 // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and
1044 // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot
1045 // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a
Ian Elliott7e361142022-06-02 10:45:17 -06001046 // surface, and that cannot be relied upon. Therefore, don't return it.
Ian Elliott1ce053f2022-03-16 09:49:53 -06001047 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1048 } else {
1049 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1050
1051 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1052 &query_value);
1053 if (err != android::OK || query_value < 0) {
1054 ALOGE(
1055 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
1056 "value=%d",
1057 strerror(-err), err, query_value);
1058 return VK_ERROR_SURFACE_LOST_KHR;
1059 }
1060 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1061
1062 err =
1063 window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
1064 if (err != android::OK || query_value < 0) {
1065 ALOGE(
1066 "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
1067 strerror(-err), err, query_value);
1068 return VK_ERROR_SURFACE_LOST_KHR;
1069 }
1070 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
1071
1072 if (min_undequeued_buffers + 1 < max_buffer_count)
1073 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
1074 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1075 }
Chris Forbese8d79a62017-02-22 12:49:18 +13001076
1077 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001078 QueryPresentationProperties(pdev, &present_properties);
1079 if (present_properties.sharedImage) {
1080 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
1081 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +13001082 }
1083
1084 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -07001085
1086 VkResult result = VK_SUCCESS;
1087 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +13001088 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -07001089 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +13001090 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -07001091 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -07001092 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +13001093 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -07001094 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001095 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001096}
1097
Jesse Halle1b12782015-11-30 11:27:32 -08001098VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001099VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001100 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001101 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001102 ATRACE_CALL();
1103
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001104 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
1105 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
1106 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
1107 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
1108 pDeviceGroupPresentCapabilities->sType);
1109
1110 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
1111 sizeof(pDeviceGroupPresentCapabilities->presentMask));
1112
1113 // assume device group of size 1
1114 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
1115 pDeviceGroupPresentCapabilities->modes =
1116 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1117
1118 return VK_SUCCESS;
1119}
1120
1121VKAPI_ATTR
1122VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001123 VkDevice,
1124 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001125 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001126 ATRACE_CALL();
1127
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001128 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1129 return VK_SUCCESS;
1130}
1131
1132VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -06001133VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001134 VkSurfaceKHR surface,
1135 uint32_t* pRectCount,
1136 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001137 ATRACE_CALL();
1138
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001139 if (!pRects) {
1140 *pRectCount = 1;
1141 } else {
1142 uint32_t count = std::min(*pRectCount, 1u);
1143 bool incomplete = *pRectCount < 1;
1144
1145 *pRectCount = count;
1146
1147 if (incomplete) {
1148 return VK_INCOMPLETE;
1149 }
1150
1151 int err;
1152 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1153
1154 int width = 0, height = 0;
1155 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001156 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001157 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1158 strerror(-err), err);
1159 }
1160 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001161 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001162 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1163 strerror(-err), err);
1164 }
1165
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001166 pRects[0].offset.x = 0;
1167 pRects[0].offset.y = 0;
1168 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1169 static_cast<uint32_t>(height)};
1170 }
1171 return VK_SUCCESS;
1172}
1173
Yiwei Zhang533cea92019-06-03 18:43:24 -07001174static void DestroySwapchainInternal(VkDevice device,
1175 VkSwapchainKHR swapchain_handle,
1176 const VkAllocationCallbacks* allocator) {
1177 ATRACE_CALL();
1178
1179 const auto& dispatch = GetData(device).driver;
1180 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1181 if (!swapchain) {
1182 return;
1183 }
1184
1185 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1186 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1187
1188 if (window && swapchain->frame_timestamps_enabled) {
1189 native_window_enable_frame_timestamps(window, false);
1190 }
1191
1192 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001193 ReleaseSwapchainImage(device, swapchain->shared, window, -1,
1194 swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001195 }
1196
1197 if (active) {
1198 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1199 }
1200
1201 if (!allocator) {
1202 allocator = &GetData(device).allocator;
1203 }
1204
1205 swapchain->~Swapchain();
1206 allocator->pfnFree(allocator->pUserData, swapchain);
1207}
1208
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001209VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001210VkResult CreateSwapchainKHR(VkDevice device,
1211 const VkSwapchainCreateInfoKHR* create_info,
1212 const VkAllocationCallbacks* allocator,
1213 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001214 ATRACE_CALL();
1215
Jesse Halld7b994a2015-09-07 14:17:37 -07001216 int err;
1217 VkResult result = VK_SUCCESS;
1218
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001219 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1220 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1221 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1222 " oldSwapchain=0x%" PRIx64,
1223 reinterpret_cast<uint64_t>(create_info->surface),
1224 create_info->minImageCount, create_info->imageFormat,
1225 create_info->imageColorSpace, create_info->imageExtent.width,
1226 create_info->imageExtent.height, create_info->imageUsage,
1227 create_info->preTransform, create_info->presentMode,
1228 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1229
Jesse Hall1f91d392015-12-11 16:28:44 -08001230 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001231 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001232
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001233 android::PixelFormat native_pixel_format =
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001234 GetNativePixelFormat(create_info->imageFormat);
1235 android_dataspace native_dataspace =
1236 GetNativeDataspace(create_info->imageColorSpace);
1237 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1238 ALOGE(
1239 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1240 "failed: Unsupported color space",
1241 create_info->imageColorSpace);
1242 return VK_ERROR_INITIALIZATION_FAILED;
1243 }
1244
Jesse Hall42a9eec2016-06-03 12:39:49 -07001245 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001246 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001247 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001248 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001249 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001250 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001251 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001252 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001253 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1254 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001255 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001256 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001257
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001258 Surface& surface = *SurfaceFromHandle(create_info->surface);
1259
Jesse Halldc225072016-05-30 22:40:14 -07001260 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001261 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001262 " because it already has active swapchain 0x%" PRIx64
1263 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1264 reinterpret_cast<uint64_t>(create_info->surface),
1265 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1266 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1267 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1268 }
1269 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1270 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1271
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001272 // -- Reset the native window --
1273 // The native window might have been used previously, and had its properties
1274 // changed from defaults. That will affect the answer we get for queries
1275 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1276 // attempt such queries.
1277
Jesse Halldc225072016-05-30 22:40:14 -07001278 // The native window only allows dequeueing all buffers before any have
1279 // been queued, since after that point at least one is assumed to be in
1280 // non-FREE state at any given time. Disconnecting and re-connecting
1281 // orphans the previous buffers, getting us back to the state where we can
1282 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001283 //
1284 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001285 ANativeWindow* window = surface.window.get();
1286 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1287 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001288 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001289 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1290 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001291 strerror(-err), err);
1292
Nicolas Capens147b7da2021-04-09 14:53:06 -04001293 err =
1294 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001295 if (err != android::OK) {
1296 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1297 strerror(-err), err);
1298 return VK_ERROR_SURFACE_LOST_KHR;
1299 }
1300
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301301 int swap_interval =
1302 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001303 err = window->setSwapInterval(window, swap_interval);
1304 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001305 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1306 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001307 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001308 }
1309
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001310 err = native_window_set_shared_buffer_mode(window, false);
1311 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001312 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1313 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001314 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001315 }
1316
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001317 err = native_window_set_auto_refresh(window, false);
1318 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001319 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1320 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001321 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001322 }
1323
Jesse Halld7b994a2015-09-07 14:17:37 -07001324 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001325
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001326 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001327
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001328 err = native_window_set_buffers_format(window, native_pixel_format);
1329 if (err != android::OK) {
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001330 ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)",
1331 decodePixelFormat(native_pixel_format).c_str(), strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001332 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001333 }
Yiwei Zhang51572c22022-07-22 23:08:30 +00001334
1335 /* Respect consumer default dataspace upon HAL_DATASPACE_ARBITRARY. */
1336 if (native_dataspace != HAL_DATASPACE_ARBITRARY) {
1337 err = native_window_set_buffers_data_space(window, native_dataspace);
1338 if (err != android::OK) {
1339 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1340 native_dataspace, strerror(-err), err);
1341 return VK_ERROR_SURFACE_LOST_KHR;
1342 }
Jesse Hall517274a2016-02-10 00:07:18 -08001343 }
1344
Jesse Hall3dd678a2016-01-08 21:52:01 -08001345 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001346 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001347 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001348 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001349 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1350 create_info->imageExtent.width, create_info->imageExtent.height,
1351 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001352 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001353 }
1354
Jesse Hall178b6962016-02-24 15:39:50 -08001355 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1356 // applied during rendering. native_window_set_transform() expects the
1357 // inverse: the transform the app is requesting that the compositor perform
1358 // during composition. With native windows, pre-transform works by rendering
1359 // with the same transform the compositor is applying (as in Vulkan), but
1360 // then requesting the inverse transform, so that when the compositor does
1361 // it's job the two transforms cancel each other out and the compositor ends
1362 // up applying an identity transform to the app's buffer.
1363 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001364 window, InvertTransformToNative(create_info->preTransform));
1365 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001366 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1367 InvertTransformToNative(create_info->preTransform),
1368 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001369 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001370 }
1371
Jesse Hallf64ca122015-11-03 16:11:10 -08001372 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001373 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1374 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001375 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1376 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001377 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001378 }
1379
Chris Forbes97ef4612017-03-30 19:37:50 +13001380 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1381 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1382 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1383 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001384 err = native_window_set_shared_buffer_mode(window, true);
1385 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001386 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1387 return VK_ERROR_SURFACE_LOST_KHR;
1388 }
1389 }
1390
1391 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001392 err = native_window_set_auto_refresh(window, true);
1393 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001394 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1395 return VK_ERROR_SURFACE_LOST_KHR;
1396 }
1397 }
1398
Ian Elliott16c443c2021-11-30 17:10:32 -07001399 int query_value;
1400 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1401 &query_value);
1402 if (err != android::OK || query_value < 0) {
1403 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1404 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001405 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001406 }
Ian Elliott16c443c2021-11-30 17:10:32 -07001407 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1408 const auto mailbox_num_images = std::max(3u, create_info->minImageCount);
1409 const auto requested_images =
1410 swap_interval ? create_info->minImageCount : mailbox_num_images;
1411 uint32_t num_images = requested_images - 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001412
Ian Elliott5396b702021-12-13 19:32:34 -07001413 // Lower layer insists that we have at least min_undequeued_buffers + 1
1414 // buffers. This is wasteful and we'd like to relax it in the shared case,
1415 // but not all the pieces are in place for that to work yet. Note we only
1416 // lie to the lower layer--we don't want to give the app back a swapchain
1417 // with extra images (which they can't actually use!).
1418 uint32_t min_buffer_count = min_undequeued_buffers + 1;
1419 err = native_window_set_buffer_count(
1420 window, std::max(min_buffer_count, num_images));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001421 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001422 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1423 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001424 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001425 }
1426
Ian Elliott12b7e2f2021-12-21 23:24:20 -07001427 // In shared mode the num_images must be one regardless of how many
1428 // buffers were allocated for the buffer queue.
1429 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1430 num_images = 1;
1431 }
1432
Trevor David Black2cc44682022-03-09 00:31:38 +00001433 void* usage_info_pNext = nullptr;
1434 VkImageCompressionControlEXT image_compression = {};
John Reck97678d42022-08-23 11:24:51 -04001435 uint64_t native_usage = 0;
Trevor David Black2cc44682022-03-09 00:31:38 +00001436 if (dispatch.GetSwapchainGrallocUsage3ANDROID) {
1437 ATRACE_BEGIN("GetSwapchainGrallocUsage3ANDROID");
1438 VkGrallocUsageInfoANDROID gralloc_usage_info = {};
1439 gralloc_usage_info.sType = VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID;
1440 gralloc_usage_info.format = create_info->imageFormat;
1441 gralloc_usage_info.imageUsage = create_info->imageUsage;
1442
1443 // Look through the pNext chain for an image compression control struct
1444 // if one is found AND the appropriate extensions are enabled,
1445 // append it to be the gralloc usage pNext chain
1446 const VkSwapchainCreateInfoKHR* create_infos = create_info;
1447 while (create_infos->pNext) {
1448 create_infos = reinterpret_cast<const VkSwapchainCreateInfoKHR*>(
1449 create_infos->pNext);
1450 switch (create_infos->sType) {
1451 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT: {
1452 const VkImageCompressionControlEXT* compression_infos =
1453 reinterpret_cast<const VkImageCompressionControlEXT*>(
1454 create_infos);
1455 image_compression = *compression_infos;
1456 image_compression.pNext = nullptr;
1457 usage_info_pNext = &image_compression;
1458 } break;
1459
1460 default:
1461 // Ignore all other info structs
1462 break;
1463 }
1464 }
1465 gralloc_usage_info.pNext = usage_info_pNext;
1466
1467 result = dispatch.GetSwapchainGrallocUsage3ANDROID(
1468 device, &gralloc_usage_info, &native_usage);
1469 ATRACE_END();
1470 if (result != VK_SUCCESS) {
1471 ALOGE("vkGetSwapchainGrallocUsage3ANDROID failed: %d", result);
1472 return VK_ERROR_SURFACE_LOST_KHR;
1473 }
1474 } else if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001475 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001476 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001477 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1478 device, create_info->imageFormat, create_info->imageUsage,
1479 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001480 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001481 if (result != VK_SUCCESS) {
1482 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001483 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001484 }
John Reck97678d42022-08-23 11:24:51 -04001485 native_usage =
Trevor David Black2cc44682022-03-09 00:31:38 +00001486 convertGralloc1ToBufferUsage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001487 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001488 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
John Reck97678d42022-08-23 11:24:51 -04001489 int32_t legacy_usage = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -08001490 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001491 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001492 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001493 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001494 if (result != VK_SUCCESS) {
1495 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001496 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001497 }
John Reck97678d42022-08-23 11:24:51 -04001498 native_usage = static_cast<uint64_t>(legacy_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001499 }
John Reck97678d42022-08-23 11:24:51 -04001500 native_usage |= surface.consumer_usage;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001501
1502 bool createProtectedSwapchain = false;
1503 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1504 createProtectedSwapchain = true;
1505 native_usage |= BufferUsage::PROTECTED;
1506 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001507 err = native_window_set_usage(window, native_usage);
1508 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001509 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001510 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001511 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001512
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001513 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001514 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1515 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001516 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1517 strerror(-err), err);
1518 return VK_ERROR_SURFACE_LOST_KHR;
1519 }
1520
Jesse Halld7b994a2015-09-07 14:17:37 -07001521 // -- Allocate our Swapchain object --
1522 // After this point, we must deallocate the swapchain on error.
1523
Jesse Hall1f91d392015-12-11 16:28:44 -08001524 void* mem = allocator->pfnAllocation(allocator->pUserData,
1525 sizeof(Swapchain), alignof(Swapchain),
1526 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001527 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001528 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001529 Swapchain* swapchain = new (mem)
1530 Swapchain(surface, num_images, create_info->presentMode,
1531 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001532 // -- Dequeue all buffers and create a VkImage for each --
1533 // Any failures during or after this must cancel the dequeued buffers.
1534
Chris Forbesb56287a2017-01-12 14:28:58 +13001535 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1536#pragma clang diagnostic push
1537#pragma clang diagnostic ignored "-Wold-style-cast"
1538 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1539#pragma clang diagnostic pop
Trevor David Black2cc44682022-03-09 00:31:38 +00001540 .pNext = usage_info_pNext,
Chris Forbesb56287a2017-01-12 14:28:58 +13001541 .usage = swapchain_image_usage,
1542 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001543 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001544#pragma clang diagnostic push
1545#pragma clang diagnostic ignored "-Wold-style-cast"
1546 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1547#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001548 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001549 };
1550 VkImageCreateInfo image_create = {
1551 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1552 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001553 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001554 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001555 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001556 .extent = {0, 0, 1},
1557 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001558 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001559 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001560 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001561 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001562 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001563 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001564 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1565 };
1566
Jesse Halld7b994a2015-09-07 14:17:37 -07001567 for (uint32_t i = 0; i < num_images; i++) {
1568 Swapchain::Image& img = swapchain->images[i];
1569
1570 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001571 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1572 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001573 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001574 switch (-err) {
1575 case ENOMEM:
1576 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1577 break;
1578 default:
1579 result = VK_ERROR_SURFACE_LOST_KHR;
1580 break;
1581 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001582 break;
1583 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001584 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001585 img.dequeued = true;
1586
1587 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001588 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1589 static_cast<uint32_t>(img.buffer->height),
1590 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001591 image_native_buffer.handle = img.buffer->handle;
1592 image_native_buffer.stride = img.buffer->stride;
1593 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001594 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001595 android_convertGralloc0To1Usage(int(img.buffer->usage),
1596 &image_native_buffer.usage2.producer,
1597 &image_native_buffer.usage2.consumer);
Trevor David Black2cc44682022-03-09 00:31:38 +00001598 image_native_buffer.usage3 = img.buffer->usage;
Jesse Halld7b994a2015-09-07 14:17:37 -07001599
Yiwei Zhang533cea92019-06-03 18:43:24 -07001600 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001601 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001602 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001603 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001604 if (result != VK_SUCCESS) {
1605 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1606 break;
1607 }
1608 }
1609
1610 // -- Cancel all buffers, returning them to the queue --
1611 // If an error occurred before, also destroy the VkImage and release the
1612 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001613 for (uint32_t i = 0; i < num_images; i++) {
1614 Swapchain::Image& img = swapchain->images[i];
1615 if (img.dequeued) {
1616 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001617 window->cancelBuffer(window, img.buffer.get(),
1618 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001619 img.dequeue_fence = -1;
1620 img.dequeued = false;
1621 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001622 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001623 }
1624
1625 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001626 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1627 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001628 return result;
1629 }
1630
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001631 if (transform_hint != swapchain->pre_transform) {
1632 // Log that the app is not doing pre-rotation.
1633 android::GraphicsEnv::getInstance().setTargetStats(
1634 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1635 }
1636
Jesse Halldc225072016-05-30 22:40:14 -07001637 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1638 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001639 return VK_SUCCESS;
1640}
1641
Jesse Halle1b12782015-11-30 11:27:32 -08001642VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001643void DestroySwapchainKHR(VkDevice device,
1644 VkSwapchainKHR swapchain_handle,
1645 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001646 ATRACE_CALL();
1647
Yiwei Zhang533cea92019-06-03 18:43:24 -07001648 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001649}
1650
Jesse Halle1b12782015-11-30 11:27:32 -08001651VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001652VkResult GetSwapchainImagesKHR(VkDevice,
1653 VkSwapchainKHR swapchain_handle,
1654 uint32_t* count,
1655 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001656 ATRACE_CALL();
1657
Jesse Halld7b994a2015-09-07 14:17:37 -07001658 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001659 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1660 "getting images for non-active swapchain 0x%" PRIx64
1661 "; only dequeued image handles are valid",
1662 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001663 VkResult result = VK_SUCCESS;
1664 if (images) {
1665 uint32_t n = swapchain.num_images;
1666 if (*count < swapchain.num_images) {
1667 n = *count;
1668 result = VK_INCOMPLETE;
1669 }
1670 for (uint32_t i = 0; i < n; i++)
1671 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001672 *count = n;
1673 } else {
1674 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001675 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001676 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001677}
1678
Jesse Halle1b12782015-11-30 11:27:32 -08001679VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001680VkResult AcquireNextImageKHR(VkDevice device,
1681 VkSwapchainKHR swapchain_handle,
1682 uint64_t timeout,
1683 VkSemaphore semaphore,
1684 VkFence vk_fence,
1685 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001686 ATRACE_CALL();
1687
Jesse Halld7b994a2015-09-07 14:17:37 -07001688 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001689 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001690 VkResult result;
1691 int err;
1692
Jesse Halldc225072016-05-30 22:40:14 -07001693 if (swapchain.surface.swapchain_handle != swapchain_handle)
1694 return VK_ERROR_OUT_OF_DATE_KHR;
1695
Chris Forbesc88409c2017-03-30 19:47:37 +13001696 if (swapchain.shared) {
1697 // In shared mode, we keep the buffer dequeued all the time, so we don't
1698 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1699 // the semaphore and fence passed to us will be signalled.
1700 *image_index = 0;
1701 result = GetData(device).driver.AcquireImageANDROID(
1702 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1703 return result;
1704 }
1705
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001706 const nsecs_t acquire_next_image_timeout =
1707 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1708 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1709 // Cache the timeout to avoid the duplicate binder cost.
1710 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1711 acquire_next_image_timeout);
1712 if (err != android::OK) {
1713 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1714 strerror(-err), err);
1715 return VK_ERROR_SURFACE_LOST_KHR;
1716 }
1717 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1718 }
1719
Jesse Halld7b994a2015-09-07 14:17:37 -07001720 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001721 int fence_fd;
1722 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001723 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001724 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1725 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1726 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001727 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001728 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001729 }
1730
1731 uint32_t idx;
1732 for (idx = 0; idx < swapchain.num_images; idx++) {
1733 if (swapchain.images[idx].buffer.get() == buffer) {
1734 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001735 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001736 break;
1737 }
1738 }
1739 if (idx == swapchain.num_images) {
1740 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001741 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001742 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001743 }
1744
1745 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001746 if (fence_fd != -1) {
1747 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001748 if (fence_clone == -1) {
1749 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1750 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001751 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001752 }
1753 }
1754
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001755 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001756 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001757 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001758 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1759 // even if the call fails. We could close it ourselves on failure, but
1760 // that would create a race condition if the driver closes it on a
1761 // failure path: some other thread might create an fd with the same
1762 // number between the time the driver closes it and the time we close
1763 // it. We must assume one of: the driver *always* closes it even on
1764 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001765 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001766 swapchain.images[idx].dequeued = false;
1767 swapchain.images[idx].dequeue_fence = -1;
1768 return result;
1769 }
1770
1771 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001772 return VK_SUCCESS;
1773}
1774
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001775VKAPI_ATTR
1776VkResult AcquireNextImage2KHR(VkDevice device,
1777 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1778 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001779 ATRACE_CALL();
1780
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001781 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1782 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1783 pAcquireInfo->fence, pImageIndex);
1784}
1785
Jesse Halldc225072016-05-30 22:40:14 -07001786static VkResult WorstPresentResult(VkResult a, VkResult b) {
1787 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1788 // (in spec version 1.0.14).
1789 static const VkResult kWorstToBest[] = {
1790 VK_ERROR_DEVICE_LOST,
1791 VK_ERROR_SURFACE_LOST_KHR,
1792 VK_ERROR_OUT_OF_DATE_KHR,
1793 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1794 VK_ERROR_OUT_OF_HOST_MEMORY,
1795 VK_SUBOPTIMAL_KHR,
1796 };
1797 for (auto result : kWorstToBest) {
1798 if (a == result || b == result)
1799 return result;
1800 }
1801 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1802 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1803 return a != VK_SUCCESS ? a : b;
1804}
1805
Jesse Halle1b12782015-11-30 11:27:32 -08001806VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001807VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001808 ATRACE_CALL();
1809
Jesse Halld7b994a2015-09-07 14:17:37 -07001810 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1811 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1812 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001813
Jesse Halldc225072016-05-30 22:40:14 -07001814 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001815 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001816 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001817
Ian Elliottcb351132016-12-13 10:30:40 -07001818 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001819 const VkPresentRegionsKHR* present_regions = nullptr;
1820 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001821 const VkPresentRegionsKHR* next =
1822 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1823 while (next) {
1824 switch (next->sType) {
1825 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1826 present_regions = next;
1827 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001828 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001829 present_times =
1830 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1831 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001832 default:
1833 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1834 next->sType);
1835 break;
1836 }
1837 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1838 }
1839 ALOGV_IF(
1840 present_regions &&
1841 present_regions->swapchainCount != present_info->swapchainCount,
1842 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001843 ALOGV_IF(present_times &&
1844 present_times->swapchainCount != present_info->swapchainCount,
1845 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1846 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001847 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001848 (present_regions) ? present_regions->pRegions : nullptr;
1849 const VkPresentTimeGOOGLE* times =
1850 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001851 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001852 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001853 uint32_t nrects = 0;
1854
Jesse Halld7b994a2015-09-07 14:17:37 -07001855 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1856 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001857 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001858 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001859 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001860 const VkPresentRegionKHR* region =
1861 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001862 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001863 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001864 VkResult result;
1865 int err;
1866
Jesse Halld7b994a2015-09-07 14:17:37 -07001867 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001868 result = dispatch.QueueSignalReleaseImageANDROID(
1869 queue, present_info->waitSemaphoreCount,
1870 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001871 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001872 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001873 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001874 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001875 if (img.release_fence >= 0)
1876 close(img.release_fence);
1877 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001878
Jesse Halldc225072016-05-30 22:40:14 -07001879 if (swapchain.surface.swapchain_handle ==
1880 present_info->pSwapchains[sc]) {
1881 ANativeWindow* window = swapchain.surface.window.get();
1882 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001883 if (region) {
1884 // Process the incremental-present hint for this swapchain:
1885 uint32_t rcount = region->rectangleCount;
1886 if (rcount > nrects) {
1887 android_native_rect_t* new_rects =
1888 static_cast<android_native_rect_t*>(
1889 allocator->pfnReallocation(
1890 allocator->pUserData, rects,
1891 sizeof(android_native_rect_t) * rcount,
1892 alignof(android_native_rect_t),
1893 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1894 if (new_rects) {
1895 rects = new_rects;
1896 nrects = rcount;
1897 } else {
1898 rcount = 0; // Ignore the hint for this swapchain
1899 }
1900 }
1901 for (uint32_t r = 0; r < rcount; ++r) {
1902 if (region->pRectangles[r].layer > 0) {
1903 ALOGV(
1904 "vkQueuePresentKHR ignoring invalid layer "
1905 "(%u); using layer 0 instead",
1906 region->pRectangles[r].layer);
1907 }
1908 int x = region->pRectangles[r].offset.x;
1909 int y = region->pRectangles[r].offset.y;
1910 int width = static_cast<int>(
1911 region->pRectangles[r].extent.width);
1912 int height = static_cast<int>(
1913 region->pRectangles[r].extent.height);
1914 android_native_rect_t* cur_rect = &rects[r];
1915 cur_rect->left = x;
1916 cur_rect->top = y + height;
1917 cur_rect->right = x + width;
1918 cur_rect->bottom = y;
1919 }
1920 native_window_set_surface_damage(window, rects, rcount);
1921 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001922 if (time) {
1923 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001924 ALOGV(
1925 "Calling "
1926 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001927 native_window_enable_frame_timestamps(window, true);
1928 swapchain.frame_timestamps_enabled = true;
1929 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001930
1931 // Record the nativeFrameId so it can be later correlated to
1932 // this present.
1933 uint64_t nativeFrameId = 0;
1934 err = native_window_get_next_frame_id(
1935 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001936 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001937 ALOGE("Failed to get next native frame ID.");
1938 }
1939
1940 // Add a new timing record with the user's presentID and
1941 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001942 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001943 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001944 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001945 }
1946 if (time->desiredPresentTime) {
1947 // Set the desiredPresentTime:
1948 ALOGV(
1949 "Calling "
1950 "native_window_set_buffers_timestamp(%" PRId64 ")",
1951 time->desiredPresentTime);
1952 native_window_set_buffers_timestamp(
1953 window,
1954 static_cast<int64_t>(time->desiredPresentTime));
1955 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001956 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001957
Jesse Halldc225072016-05-30 22:40:14 -07001958 err = window->queueBuffer(window, img.buffer.get(), fence);
1959 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001960 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001961 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1962 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001963 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001964 } else {
1965 if (img.dequeue_fence >= 0) {
1966 close(img.dequeue_fence);
1967 img.dequeue_fence = -1;
1968 }
1969 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001970 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001971
1972 // If the swapchain is in shared mode, immediately dequeue the
1973 // buffer so it can be presented again without an intervening
1974 // call to AcquireNextImageKHR. We expect to get the same buffer
1975 // back from every call to dequeueBuffer in this mode.
1976 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1977 ANativeWindowBuffer* buffer;
1978 int fence_fd;
1979 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001980 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001981 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1982 swapchain_result = WorstPresentResult(swapchain_result,
1983 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001984 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001985 ALOGE("got wrong image back for shared swapchain");
1986 swapchain_result = WorstPresentResult(swapchain_result,
1987 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001988 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13001989 img.dequeue_fence = fence_fd;
1990 img.dequeued = true;
1991 }
1992 }
Jesse Halldc225072016-05-30 22:40:14 -07001993 }
1994 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001995 OrphanSwapchain(device, &swapchain);
1996 }
silence_dogood73597592019-05-23 16:57:37 -07001997 int window_transform_hint;
1998 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1999 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08002000 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07002001 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
2002 strerror(-err), err);
2003 swapchain_result = WorstPresentResult(
2004 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
2005 }
2006 if (swapchain.pre_transform != window_transform_hint) {
2007 swapchain_result =
2008 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
2009 }
Jesse Halldc225072016-05-30 22:40:14 -07002010 } else {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00002011 ReleaseSwapchainImage(device, swapchain.shared, nullptr, fence,
2012 img, true);
Jesse Halldc225072016-05-30 22:40:14 -07002013 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07002014 }
2015
Jesse Halla9e57032015-11-30 01:03:10 -08002016 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07002017 present_info->pResults[sc] = swapchain_result;
2018
2019 if (swapchain_result != final_result)
2020 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07002021 }
Ian Elliottcb351132016-12-13 10:30:40 -07002022 if (rects) {
2023 allocator->pfnFree(allocator->pUserData, rects);
2024 }
Jesse Halld7b994a2015-09-07 14:17:37 -07002025
2026 return final_result;
2027}
Jesse Hallb1352bc2015-09-04 16:12:33 -07002028
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002029VKAPI_ATTR
2030VkResult GetRefreshCycleDurationGOOGLE(
2031 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07002032 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002033 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002034 ATRACE_CALL();
2035
Ian Elliott62c48c92017-01-20 13:13:20 -07002036 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002037 VkResult result = VK_SUCCESS;
2038
Ian Elliott3568bfe2019-05-03 15:54:46 -06002039 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002040
2041 return result;
2042}
2043
2044VKAPI_ATTR
2045VkResult GetPastPresentationTimingGOOGLE(
2046 VkDevice,
2047 VkSwapchainKHR swapchain_handle,
2048 uint32_t* count,
2049 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002050 ATRACE_CALL();
2051
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002052 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002053 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2054 return VK_ERROR_OUT_OF_DATE_KHR;
2055 }
2056
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002057 ANativeWindow* window = swapchain.surface.window.get();
2058 VkResult result = VK_SUCCESS;
2059
2060 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07002061 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002062 native_window_enable_frame_timestamps(window, true);
2063 swapchain.frame_timestamps_enabled = true;
2064 }
2065
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002066 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07002067 // Get the latest ready timing count before copying, since the copied
2068 // timing info will be erased in copy_ready_timings function.
2069 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07002070 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07002071 // Check the *count here against the recorded ready timing count, since
2072 // *count can be overwritten per spec describes.
2073 if (*count < n) {
2074 result = VK_INCOMPLETE;
2075 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002076 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07002077 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07002078 }
2079
2080 return result;
2081}
2082
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002083VKAPI_ATTR
2084VkResult GetSwapchainStatusKHR(
2085 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13002086 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002087 ATRACE_CALL();
2088
Chris Forbes4e18ba82017-01-20 12:50:17 +13002089 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002090 VkResult result = VK_SUCCESS;
2091
Chris Forbes4e18ba82017-01-20 12:50:17 +13002092 if (swapchain.surface.swapchain_handle != swapchain_handle) {
2093 return VK_ERROR_OUT_OF_DATE_KHR;
2094 }
2095
Yiwei Zhanga885c062019-10-24 12:07:57 -07002096 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13002097
2098 return result;
2099}
2100
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002101VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002102 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002103 uint32_t swapchainCount,
2104 const VkSwapchainKHR* pSwapchains,
2105 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002106 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002107
2108 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
2109 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
2110 if (!swapchain)
2111 continue;
2112
2113 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
2114
2115 ANativeWindow* window = swapchain->surface.window.get();
2116
2117 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
2118 const android_smpte2086_metadata smpteMetdata = {
2119 {vulkanMetadata.displayPrimaryRed.x,
2120 vulkanMetadata.displayPrimaryRed.y},
2121 {vulkanMetadata.displayPrimaryGreen.x,
2122 vulkanMetadata.displayPrimaryGreen.y},
2123 {vulkanMetadata.displayPrimaryBlue.x,
2124 vulkanMetadata.displayPrimaryBlue.y},
2125 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
2126 vulkanMetadata.maxLuminance,
2127 vulkanMetadata.minLuminance};
2128 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
2129
2130 const android_cta861_3_metadata cta8613Metadata = {
2131 vulkanMetadata.maxContentLightLevel,
2132 vulkanMetadata.maxFrameAverageLightLevel};
2133 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
2134 }
2135
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002136 return;
2137}
2138
Yiwei Zhang0f475222019-04-11 19:38:00 -07002139static void InterceptBindImageMemory2(
2140 uint32_t bind_info_count,
2141 const VkBindImageMemoryInfo* bind_infos,
2142 std::vector<VkNativeBufferANDROID>* out_native_buffers,
2143 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
2144 out_native_buffers->clear();
2145 out_bind_infos->clear();
2146
2147 if (!bind_info_count)
2148 return;
2149
2150 std::unordered_set<uint32_t> intercepted_indexes;
2151
2152 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2153 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2154 bind_infos[idx].pNext);
2155 while (info &&
2156 info->sType !=
2157 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
2158 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2159 info->pNext);
2160 }
2161
2162 if (!info)
2163 continue;
2164
2165 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
2166 "swapchain handle must not be NULL");
2167 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
2168 ALOG_ASSERT(
2169 info->imageIndex < swapchain->num_images,
2170 "imageIndex must be less than the number of images in swapchain");
2171
2172 ANativeWindowBuffer* buffer =
2173 swapchain->images[info->imageIndex].buffer.get();
2174 VkNativeBufferANDROID native_buffer = {
2175#pragma clang diagnostic push
2176#pragma clang diagnostic ignored "-Wold-style-cast"
2177 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2178#pragma clang diagnostic pop
2179 .pNext = bind_infos[idx].pNext,
2180 .handle = buffer->handle,
2181 .stride = buffer->stride,
2182 .format = buffer->format,
2183 .usage = int(buffer->usage),
2184 };
2185 // Reserve enough space to avoid letting re-allocation invalidate the
2186 // addresses of the elements inside.
2187 out_native_buffers->reserve(bind_info_count);
2188 out_native_buffers->emplace_back(native_buffer);
2189
2190 // Reserve the space now since we know how much is needed now.
2191 out_bind_infos->reserve(bind_info_count);
2192 out_bind_infos->emplace_back(bind_infos[idx]);
2193 out_bind_infos->back().pNext = &out_native_buffers->back();
2194
2195 intercepted_indexes.insert(idx);
2196 }
2197
2198 if (intercepted_indexes.empty())
2199 return;
2200
2201 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2202 if (intercepted_indexes.count(idx))
2203 continue;
2204 out_bind_infos->emplace_back(bind_infos[idx]);
2205 }
2206}
2207
Yiwei Zhang23143102019-04-10 18:24:05 -07002208VKAPI_ATTR
2209VkResult BindImageMemory2(VkDevice device,
2210 uint32_t bindInfoCount,
2211 const VkBindImageMemoryInfo* pBindInfos) {
2212 ATRACE_CALL();
2213
Yiwei Zhang0f475222019-04-11 19:38:00 -07002214 // out_native_buffers is for maintaining the lifecycle of the constructed
2215 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2216 std::vector<VkNativeBufferANDROID> out_native_buffers;
2217 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2218 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2219 &out_bind_infos);
2220 return GetData(device).driver.BindImageMemory2(
2221 device, bindInfoCount,
2222 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002223}
2224
2225VKAPI_ATTR
2226VkResult BindImageMemory2KHR(VkDevice device,
2227 uint32_t bindInfoCount,
2228 const VkBindImageMemoryInfo* pBindInfos) {
2229 ATRACE_CALL();
2230
Yiwei Zhang0f475222019-04-11 19:38:00 -07002231 std::vector<VkNativeBufferANDROID> out_native_buffers;
2232 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2233 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2234 &out_bind_infos);
2235 return GetData(device).driver.BindImageMemory2KHR(
2236 device, bindInfoCount,
2237 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002238}
2239
Chia-I Wu62262232016-03-26 07:06:44 +08002240} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002241} // namespace vulkan