blob: a75108c543ef5acacf258220870cf05096782ddc [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
Jesse Hallfe2662d2016-02-09 13:26:59 -0800718 capabilities->maxImageArrayLayers = 1;
719
Jesse Hall55bc0972016-02-23 16:43:29 -0800720 capabilities->supportedTransforms = kSupportedTransforms;
721 capabilities->currentTransform =
722 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700723
Jesse Hallfe2662d2016-02-09 13:26:59 -0800724 // On Android, window composition is a WindowManager property, not something
725 // associated with the bufferqueue. It can't be changed from here.
726 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700727
Jesse Hallb00daad2015-11-29 19:46:20 -0800728 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800729 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
730 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
731 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700732 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
733
Jesse Hallb1352bc2015-09-04 16:12:33 -0700734 return VK_SUCCESS;
735}
736
Jesse Halle1b12782015-11-30 11:27:32 -0800737VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700738VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
739 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800740 uint32_t* count,
741 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800742 ATRACE_CALL();
743
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700744 const InstanceData& instance_data = GetData(pdev);
745
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700746 bool wide_color_support = false;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600747 uint64_t consumer_usage = 0;
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000748 bool colorspace_ext =
Ian Elliottf6df08e2022-03-16 21:27:49 -0600749 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600750 if (surface_handle == VK_NULL_HANDLE) {
751 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
752 bool surfaceless_enabled =
753 instance_data.hook_extensions.test(surfaceless);
754 if (!surfaceless_enabled) {
755 return VK_ERROR_SURFACE_LOST_KHR;
756 }
757 // Support for VK_GOOGLE_surfaceless_query. The EGL loader
758 // unconditionally supports wide color formats, even if they will cause
759 // a SurfaceFlinger fallback. Based on that, wide_color_support will be
760 // set to true in this case.
761 wide_color_support = true;
762
763 // TODO(b/203826952): research proper value; temporarily use the
764 // values seen on Pixel
765 consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
766 } else {
767 Surface& surface = *SurfaceFromHandle(surface_handle);
768 int err = native_window_get_wide_color_support(surface.window.get(),
769 &wide_color_support);
770 if (err) {
771 return VK_ERROR_SURFACE_LOST_KHR;
772 }
773 ALOGV("wide_color_support is: %d", wide_color_support);
774
775 consumer_usage = surface.consumer_usage;
Ian Elliott6ba85d92022-02-18 16:44:58 -0700776 }
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000777 wide_color_support = wide_color_support && colorspace_ext;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700778
Charlie Lao324191f2019-12-13 11:03:16 -0800779 AHardwareBuffer_Desc desc = {};
780 desc.width = 1;
781 desc.height = 1;
782 desc.layers = 1;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600783 desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
Charlie Lao324191f2019-12-13 11:03:16 -0800784 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
785
786 // We must support R8G8B8A8
787 std::vector<VkSurfaceFormatKHR> all_formats = {
788 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700789 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700790 };
Charlie Lao324191f2019-12-13 11:03:16 -0800791
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000792 if (colorspace_ext) {
793 all_formats.emplace_back(VkSurfaceFormatKHR{
Jason Macnakca506332022-11-09 11:00:36 -0800794 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT});
795 all_formats.emplace_back(VkSurfaceFormatKHR{
796 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_PASS_THROUGH_EXT});
797 all_formats.emplace_back(VkSurfaceFormatKHR{
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000798 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_BT709_LINEAR_EXT});
799 }
800
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700801 if (wide_color_support) {
Charlie Lao324191f2019-12-13 11:03:16 -0800802 all_formats.emplace_back(VkSurfaceFormatKHR{
803 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
804 all_formats.emplace_back(VkSurfaceFormatKHR{
805 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
806 }
807
Ian Elliott1ce053f2022-03-16 09:49:53 -0600808 // NOTE: Any new formats that are added must be coordinated across different
809 // Android users. This includes the ANGLE team (a layered implementation of
810 // OpenGL-ES).
811
Charlie Lao324191f2019-12-13 11:03:16 -0800812 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
813 if (AHardwareBuffer_isSupported(&desc)) {
814 all_formats.emplace_back(VkSurfaceFormatKHR{
815 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800816 if (colorspace_ext) {
817 all_formats.emplace_back(
818 VkSurfaceFormatKHR{VK_FORMAT_R5G6B5_UNORM_PACK16,
819 VK_COLOR_SPACE_PASS_THROUGH_EXT});
820 }
Charlie Lao324191f2019-12-13 11:03:16 -0800821 }
822
823 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
824 if (AHardwareBuffer_isSupported(&desc)) {
825 all_formats.emplace_back(VkSurfaceFormatKHR{
826 VK_FORMAT_R16G16B16A16_SFLOAT, 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_R16G16B16A16_SFLOAT,
830 VK_COLOR_SPACE_PASS_THROUGH_EXT});
831 }
Charlie Lao324191f2019-12-13 11:03:16 -0800832 if (wide_color_support) {
833 all_formats.emplace_back(
834 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
835 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
836 all_formats.emplace_back(
837 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
838 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
839 }
840 }
841
842 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
843 if (AHardwareBuffer_isSupported(&desc)) {
844 all_formats.emplace_back(
845 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
846 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnakca506332022-11-09 11:00:36 -0800847 if (colorspace_ext) {
848 all_formats.emplace_back(
849 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
850 VK_COLOR_SPACE_PASS_THROUGH_EXT});
851 }
Charlie Lao324191f2019-12-13 11:03:16 -0800852 if (wide_color_support) {
853 all_formats.emplace_back(
854 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
855 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
856 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700857 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700858
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500859 desc.format = AHARDWAREBUFFER_FORMAT_R8_UNORM;
860 if (AHardwareBuffer_isSupported(&desc)) {
Jason Macnakca506332022-11-09 11:00:36 -0800861 if (colorspace_ext) {
862 all_formats.emplace_back(VkSurfaceFormatKHR{
863 VK_FORMAT_R8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT});
864 }
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500865 }
866
Ian Elliott6ba85d92022-02-18 16:44:58 -0700867 // NOTE: Any new formats that are added must be coordinated across different
868 // Android users. This includes the ANGLE team (a layered implementation of
869 // OpenGL-ES).
870
Jesse Halld7b994a2015-09-07 14:17:37 -0700871 VkResult result = VK_SUCCESS;
872 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800873 uint32_t transfer_count = all_formats.size();
874 if (transfer_count > *count) {
875 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700876 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700877 }
Charlie Lao324191f2019-12-13 11:03:16 -0800878 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
879 formats);
880 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700881 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800882 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700883 }
Charlie Lao324191f2019-12-13 11:03:16 -0800884
Jesse Halld7b994a2015-09-07 14:17:37 -0700885 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700886}
887
Jesse Halle1b12782015-11-30 11:27:32 -0800888VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300889VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
890 VkPhysicalDevice physicalDevice,
891 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
892 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800893 ATRACE_CALL();
894
Chris Forbes2452cf72017-03-16 16:30:17 +1300895 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
896 physicalDevice, pSurfaceInfo->surface,
897 &pSurfaceCapabilities->surfaceCapabilities);
898
Chris Forbes06bc0092017-03-16 16:46:05 +1300899 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
900 while (caps->pNext) {
901 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
902
903 switch (caps->sType) {
904 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
905 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
906 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
907 caps);
908 // Claim same set of usage flags are supported for
909 // shared present modes as for other modes.
910 shared_caps->sharedPresentSupportedUsageFlags =
911 pSurfaceCapabilities->surfaceCapabilities
912 .supportedUsageFlags;
913 } break;
914
Ian Elliottbb67b242022-03-16 09:52:28 -0600915 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
916 VkSurfaceProtectedCapabilitiesKHR* protected_caps =
917 reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(caps);
918 protected_caps->supportsProtected = VK_TRUE;
919 } break;
920
Chris Forbes06bc0092017-03-16 16:46:05 +1300921 default:
922 // Ignore all other extension structs
923 break;
924 }
925 }
926
Chris Forbes2452cf72017-03-16 16:30:17 +1300927 return result;
928}
929
930VKAPI_ATTR
931VkResult GetPhysicalDeviceSurfaceFormats2KHR(
932 VkPhysicalDevice physicalDevice,
933 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
934 uint32_t* pSurfaceFormatCount,
935 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800936 ATRACE_CALL();
937
Chris Forbes2452cf72017-03-16 16:30:17 +1300938 if (!pSurfaceFormats) {
939 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
940 pSurfaceInfo->surface,
941 pSurfaceFormatCount, nullptr);
942 } else {
943 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
944 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700945 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300946 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
947 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700948 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300949
950 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
951 // marshal results individually due to stride difference.
952 // completely ignore any chained extension structs.
953 uint32_t formats_to_marshal = *pSurfaceFormatCount;
954 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
955 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
956 }
957 }
958
959 return result;
960 }
961}
962
963VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300964VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800965 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800966 uint32_t* count,
967 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800968 ATRACE_CALL();
969
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800970 int err;
971 int query_value;
Ian Elliotte7f036c2022-03-15 16:49:21 -0600972 std::vector<VkPresentModeKHR> present_modes;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600973 if (surface == VK_NULL_HANDLE) {
974 const InstanceData& instance_data = GetData(pdev);
975 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
976 bool surfaceless_enabled =
977 instance_data.hook_extensions.test(surfaceless);
978 if (!surfaceless_enabled) {
979 return VK_ERROR_SURFACE_LOST_KHR;
980 }
981 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
982 // extension for this function is for
983 // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and
984 // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot
985 // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a
Ian Elliott7e361142022-06-02 10:45:17 -0600986 // surface, and that cannot be relied upon. Therefore, don't return it.
Ian Elliott1ce053f2022-03-16 09:49:53 -0600987 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
988 } else {
989 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
990
991 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
992 &query_value);
993 if (err != android::OK || query_value < 0) {
994 ALOGE(
995 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
996 "value=%d",
997 strerror(-err), err, query_value);
998 return VK_ERROR_SURFACE_LOST_KHR;
999 }
1000 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1001
1002 err =
1003 window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
1004 if (err != android::OK || query_value < 0) {
1005 ALOGE(
1006 "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
1007 strerror(-err), err, query_value);
1008 return VK_ERROR_SURFACE_LOST_KHR;
1009 }
1010 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
1011
1012 if (min_undequeued_buffers + 1 < max_buffer_count)
1013 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
1014 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1015 }
Chris Forbese8d79a62017-02-22 12:49:18 +13001016
1017 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001018 QueryPresentationProperties(pdev, &present_properties);
1019 if (present_properties.sharedImage) {
1020 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
1021 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +13001022 }
1023
1024 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -07001025
1026 VkResult result = VK_SUCCESS;
1027 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +13001028 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -07001029 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +13001030 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -07001031 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -07001032 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +13001033 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -07001034 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001035 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001036}
1037
Jesse Halle1b12782015-11-30 11:27:32 -08001038VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001039VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001040 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001041 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001042 ATRACE_CALL();
1043
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001044 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
1045 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
1046 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
1047 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
1048 pDeviceGroupPresentCapabilities->sType);
1049
1050 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
1051 sizeof(pDeviceGroupPresentCapabilities->presentMask));
1052
1053 // assume device group of size 1
1054 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
1055 pDeviceGroupPresentCapabilities->modes =
1056 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1057
1058 return VK_SUCCESS;
1059}
1060
1061VKAPI_ATTR
1062VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001063 VkDevice,
1064 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001065 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001066 ATRACE_CALL();
1067
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001068 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1069 return VK_SUCCESS;
1070}
1071
1072VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -06001073VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001074 VkSurfaceKHR surface,
1075 uint32_t* pRectCount,
1076 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001077 ATRACE_CALL();
1078
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001079 if (!pRects) {
1080 *pRectCount = 1;
1081 } else {
1082 uint32_t count = std::min(*pRectCount, 1u);
1083 bool incomplete = *pRectCount < 1;
1084
1085 *pRectCount = count;
1086
1087 if (incomplete) {
1088 return VK_INCOMPLETE;
1089 }
1090
1091 int err;
1092 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1093
1094 int width = 0, height = 0;
1095 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001096 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001097 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1098 strerror(-err), err);
1099 }
1100 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001101 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001102 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1103 strerror(-err), err);
1104 }
1105
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001106 pRects[0].offset.x = 0;
1107 pRects[0].offset.y = 0;
1108 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1109 static_cast<uint32_t>(height)};
1110 }
1111 return VK_SUCCESS;
1112}
1113
Yiwei Zhang533cea92019-06-03 18:43:24 -07001114static void DestroySwapchainInternal(VkDevice device,
1115 VkSwapchainKHR swapchain_handle,
1116 const VkAllocationCallbacks* allocator) {
1117 ATRACE_CALL();
1118
1119 const auto& dispatch = GetData(device).driver;
1120 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1121 if (!swapchain) {
1122 return;
1123 }
1124
1125 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1126 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1127
1128 if (window && swapchain->frame_timestamps_enabled) {
1129 native_window_enable_frame_timestamps(window, false);
1130 }
1131
1132 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001133 ReleaseSwapchainImage(device, swapchain->shared, window, -1,
1134 swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001135 }
1136
1137 if (active) {
1138 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1139 }
1140
1141 if (!allocator) {
1142 allocator = &GetData(device).allocator;
1143 }
1144
1145 swapchain->~Swapchain();
1146 allocator->pfnFree(allocator->pUserData, swapchain);
1147}
1148
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001149VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001150VkResult CreateSwapchainKHR(VkDevice device,
1151 const VkSwapchainCreateInfoKHR* create_info,
1152 const VkAllocationCallbacks* allocator,
1153 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001154 ATRACE_CALL();
1155
Jesse Halld7b994a2015-09-07 14:17:37 -07001156 int err;
1157 VkResult result = VK_SUCCESS;
1158
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001159 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1160 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1161 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1162 " oldSwapchain=0x%" PRIx64,
1163 reinterpret_cast<uint64_t>(create_info->surface),
1164 create_info->minImageCount, create_info->imageFormat,
1165 create_info->imageColorSpace, create_info->imageExtent.width,
1166 create_info->imageExtent.height, create_info->imageUsage,
1167 create_info->preTransform, create_info->presentMode,
1168 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1169
Jesse Hall1f91d392015-12-11 16:28:44 -08001170 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001171 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001172
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001173 android::PixelFormat native_pixel_format =
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001174 GetNativePixelFormat(create_info->imageFormat);
1175 android_dataspace native_dataspace =
1176 GetNativeDataspace(create_info->imageColorSpace);
1177 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1178 ALOGE(
1179 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1180 "failed: Unsupported color space",
1181 create_info->imageColorSpace);
1182 return VK_ERROR_INITIALIZATION_FAILED;
1183 }
1184
Jesse Hall42a9eec2016-06-03 12:39:49 -07001185 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001186 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001187 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001188 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001189 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001190 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001191 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001192 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001193 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1194 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001195 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001196 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001197
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001198 Surface& surface = *SurfaceFromHandle(create_info->surface);
1199
Jesse Halldc225072016-05-30 22:40:14 -07001200 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001201 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001202 " because it already has active swapchain 0x%" PRIx64
1203 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1204 reinterpret_cast<uint64_t>(create_info->surface),
1205 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1206 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1207 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1208 }
1209 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1210 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1211
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001212 // -- Reset the native window --
1213 // The native window might have been used previously, and had its properties
1214 // changed from defaults. That will affect the answer we get for queries
1215 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1216 // attempt such queries.
1217
Jesse Halldc225072016-05-30 22:40:14 -07001218 // The native window only allows dequeueing all buffers before any have
1219 // been queued, since after that point at least one is assumed to be in
1220 // non-FREE state at any given time. Disconnecting and re-connecting
1221 // orphans the previous buffers, getting us back to the state where we can
1222 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001223 //
1224 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001225 ANativeWindow* window = surface.window.get();
1226 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1227 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001228 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001229 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1230 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001231 strerror(-err), err);
1232
Nicolas Capens147b7da2021-04-09 14:53:06 -04001233 err =
1234 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001235 if (err != android::OK) {
1236 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1237 strerror(-err), err);
1238 return VK_ERROR_SURFACE_LOST_KHR;
1239 }
1240
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301241 int swap_interval =
1242 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001243 err = window->setSwapInterval(window, swap_interval);
1244 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001245 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1246 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001247 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001248 }
1249
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001250 err = native_window_set_shared_buffer_mode(window, false);
1251 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001252 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1253 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001254 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001255 }
1256
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001257 err = native_window_set_auto_refresh(window, false);
1258 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001259 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1260 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001261 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001262 }
1263
Jesse Halld7b994a2015-09-07 14:17:37 -07001264 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001265
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001266 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001267
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001268 err = native_window_set_buffers_format(window, native_pixel_format);
1269 if (err != android::OK) {
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001270 ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)",
1271 decodePixelFormat(native_pixel_format).c_str(), strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001272 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001273 }
Yiwei Zhang51572c22022-07-22 23:08:30 +00001274
1275 /* Respect consumer default dataspace upon HAL_DATASPACE_ARBITRARY. */
1276 if (native_dataspace != HAL_DATASPACE_ARBITRARY) {
1277 err = native_window_set_buffers_data_space(window, native_dataspace);
1278 if (err != android::OK) {
1279 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1280 native_dataspace, strerror(-err), err);
1281 return VK_ERROR_SURFACE_LOST_KHR;
1282 }
Jesse Hall517274a2016-02-10 00:07:18 -08001283 }
1284
Jesse Hall3dd678a2016-01-08 21:52:01 -08001285 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001286 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001287 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001288 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001289 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1290 create_info->imageExtent.width, create_info->imageExtent.height,
1291 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001292 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001293 }
1294
Jesse Hall178b6962016-02-24 15:39:50 -08001295 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1296 // applied during rendering. native_window_set_transform() expects the
1297 // inverse: the transform the app is requesting that the compositor perform
1298 // during composition. With native windows, pre-transform works by rendering
1299 // with the same transform the compositor is applying (as in Vulkan), but
1300 // then requesting the inverse transform, so that when the compositor does
1301 // it's job the two transforms cancel each other out and the compositor ends
1302 // up applying an identity transform to the app's buffer.
1303 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001304 window, InvertTransformToNative(create_info->preTransform));
1305 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001306 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1307 InvertTransformToNative(create_info->preTransform),
1308 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001309 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001310 }
1311
Jesse Hallf64ca122015-11-03 16:11:10 -08001312 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001313 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1314 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001315 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1316 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001317 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001318 }
1319
Chris Forbes97ef4612017-03-30 19:37:50 +13001320 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1321 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1322 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1323 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001324 err = native_window_set_shared_buffer_mode(window, true);
1325 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001326 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1327 return VK_ERROR_SURFACE_LOST_KHR;
1328 }
1329 }
1330
1331 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001332 err = native_window_set_auto_refresh(window, true);
1333 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001334 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1335 return VK_ERROR_SURFACE_LOST_KHR;
1336 }
1337 }
1338
Ian Elliott16c443c2021-11-30 17:10:32 -07001339 int query_value;
1340 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1341 &query_value);
1342 if (err != android::OK || query_value < 0) {
1343 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1344 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001345 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001346 }
Ian Elliott16c443c2021-11-30 17:10:32 -07001347 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1348 const auto mailbox_num_images = std::max(3u, create_info->minImageCount);
1349 const auto requested_images =
1350 swap_interval ? create_info->minImageCount : mailbox_num_images;
1351 uint32_t num_images = requested_images - 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001352
Ian Elliott5396b702021-12-13 19:32:34 -07001353 // Lower layer insists that we have at least min_undequeued_buffers + 1
1354 // buffers. This is wasteful and we'd like to relax it in the shared case,
1355 // but not all the pieces are in place for that to work yet. Note we only
1356 // lie to the lower layer--we don't want to give the app back a swapchain
1357 // with extra images (which they can't actually use!).
1358 uint32_t min_buffer_count = min_undequeued_buffers + 1;
1359 err = native_window_set_buffer_count(
1360 window, std::max(min_buffer_count, num_images));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001361 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001362 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1363 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001364 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001365 }
1366
Ian Elliott12b7e2f2021-12-21 23:24:20 -07001367 // In shared mode the num_images must be one regardless of how many
1368 // buffers were allocated for the buffer queue.
1369 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1370 num_images = 1;
1371 }
1372
John Reck97678d42022-08-23 11:24:51 -04001373 uint64_t native_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001374 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001375 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001376 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001377 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1378 device, create_info->imageFormat, create_info->imageUsage,
1379 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001380 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001381 if (result != VK_SUCCESS) {
1382 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001383 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001384 }
John Reck97678d42022-08-23 11:24:51 -04001385 native_usage =
1386 convertGralloc1ToBufferUsage(consumer_usage, producer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001387 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001388 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
John Reck97678d42022-08-23 11:24:51 -04001389 int32_t legacy_usage = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -08001390 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001391 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001392 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001393 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001394 if (result != VK_SUCCESS) {
1395 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001396 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001397 }
John Reck97678d42022-08-23 11:24:51 -04001398 native_usage = static_cast<uint64_t>(legacy_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001399 }
John Reck97678d42022-08-23 11:24:51 -04001400 native_usage |= surface.consumer_usage;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001401
1402 bool createProtectedSwapchain = false;
1403 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1404 createProtectedSwapchain = true;
1405 native_usage |= BufferUsage::PROTECTED;
1406 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001407 err = native_window_set_usage(window, native_usage);
1408 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001409 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001410 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001411 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001412
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001413 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001414 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1415 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001416 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1417 strerror(-err), err);
1418 return VK_ERROR_SURFACE_LOST_KHR;
1419 }
1420
Jesse Halld7b994a2015-09-07 14:17:37 -07001421 // -- Allocate our Swapchain object --
1422 // After this point, we must deallocate the swapchain on error.
1423
Jesse Hall1f91d392015-12-11 16:28:44 -08001424 void* mem = allocator->pfnAllocation(allocator->pUserData,
1425 sizeof(Swapchain), alignof(Swapchain),
1426 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001427 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001428 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001429 Swapchain* swapchain = new (mem)
1430 Swapchain(surface, num_images, create_info->presentMode,
1431 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001432 // -- Dequeue all buffers and create a VkImage for each --
1433 // Any failures during or after this must cancel the dequeued buffers.
1434
Chris Forbesb56287a2017-01-12 14:28:58 +13001435 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1436#pragma clang diagnostic push
1437#pragma clang diagnostic ignored "-Wold-style-cast"
1438 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1439#pragma clang diagnostic pop
1440 .pNext = nullptr,
1441 .usage = swapchain_image_usage,
1442 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001443 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001444#pragma clang diagnostic push
1445#pragma clang diagnostic ignored "-Wold-style-cast"
1446 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1447#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001448 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001449 };
1450 VkImageCreateInfo image_create = {
1451 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1452 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001453 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001454 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001455 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001456 .extent = {0, 0, 1},
1457 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001458 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001459 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001460 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001461 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001462 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001463 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001464 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1465 };
1466
Jesse Halld7b994a2015-09-07 14:17:37 -07001467 for (uint32_t i = 0; i < num_images; i++) {
1468 Swapchain::Image& img = swapchain->images[i];
1469
1470 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001471 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1472 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001473 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001474 switch (-err) {
1475 case ENOMEM:
1476 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1477 break;
1478 default:
1479 result = VK_ERROR_SURFACE_LOST_KHR;
1480 break;
1481 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001482 break;
1483 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001484 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001485 img.dequeued = true;
1486
1487 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001488 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1489 static_cast<uint32_t>(img.buffer->height),
1490 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001491 image_native_buffer.handle = img.buffer->handle;
1492 image_native_buffer.stride = img.buffer->stride;
1493 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001494 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001495 android_convertGralloc0To1Usage(int(img.buffer->usage),
1496 &image_native_buffer.usage2.producer,
1497 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001498
Yiwei Zhang533cea92019-06-03 18:43:24 -07001499 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001500 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001501 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001502 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001503 if (result != VK_SUCCESS) {
1504 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1505 break;
1506 }
1507 }
1508
1509 // -- Cancel all buffers, returning them to the queue --
1510 // If an error occurred before, also destroy the VkImage and release the
1511 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001512 for (uint32_t i = 0; i < num_images; i++) {
1513 Swapchain::Image& img = swapchain->images[i];
1514 if (img.dequeued) {
1515 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001516 window->cancelBuffer(window, img.buffer.get(),
1517 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001518 img.dequeue_fence = -1;
1519 img.dequeued = false;
1520 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001521 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001522 }
1523
1524 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001525 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1526 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001527 return result;
1528 }
1529
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001530 if (transform_hint != swapchain->pre_transform) {
1531 // Log that the app is not doing pre-rotation.
1532 android::GraphicsEnv::getInstance().setTargetStats(
1533 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1534 }
1535
Jesse Halldc225072016-05-30 22:40:14 -07001536 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1537 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001538 return VK_SUCCESS;
1539}
1540
Jesse Halle1b12782015-11-30 11:27:32 -08001541VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001542void DestroySwapchainKHR(VkDevice device,
1543 VkSwapchainKHR swapchain_handle,
1544 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001545 ATRACE_CALL();
1546
Yiwei Zhang533cea92019-06-03 18:43:24 -07001547 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001548}
1549
Jesse Halle1b12782015-11-30 11:27:32 -08001550VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001551VkResult GetSwapchainImagesKHR(VkDevice,
1552 VkSwapchainKHR swapchain_handle,
1553 uint32_t* count,
1554 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001555 ATRACE_CALL();
1556
Jesse Halld7b994a2015-09-07 14:17:37 -07001557 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001558 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1559 "getting images for non-active swapchain 0x%" PRIx64
1560 "; only dequeued image handles are valid",
1561 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001562 VkResult result = VK_SUCCESS;
1563 if (images) {
1564 uint32_t n = swapchain.num_images;
1565 if (*count < swapchain.num_images) {
1566 n = *count;
1567 result = VK_INCOMPLETE;
1568 }
1569 for (uint32_t i = 0; i < n; i++)
1570 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001571 *count = n;
1572 } else {
1573 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001574 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001575 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001576}
1577
Jesse Halle1b12782015-11-30 11:27:32 -08001578VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001579VkResult AcquireNextImageKHR(VkDevice device,
1580 VkSwapchainKHR swapchain_handle,
1581 uint64_t timeout,
1582 VkSemaphore semaphore,
1583 VkFence vk_fence,
1584 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001585 ATRACE_CALL();
1586
Jesse Halld7b994a2015-09-07 14:17:37 -07001587 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001588 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001589 VkResult result;
1590 int err;
1591
Jesse Halldc225072016-05-30 22:40:14 -07001592 if (swapchain.surface.swapchain_handle != swapchain_handle)
1593 return VK_ERROR_OUT_OF_DATE_KHR;
1594
Chris Forbesc88409c2017-03-30 19:47:37 +13001595 if (swapchain.shared) {
1596 // In shared mode, we keep the buffer dequeued all the time, so we don't
1597 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1598 // the semaphore and fence passed to us will be signalled.
1599 *image_index = 0;
1600 result = GetData(device).driver.AcquireImageANDROID(
1601 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1602 return result;
1603 }
1604
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001605 const nsecs_t acquire_next_image_timeout =
1606 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1607 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1608 // Cache the timeout to avoid the duplicate binder cost.
1609 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1610 acquire_next_image_timeout);
1611 if (err != android::OK) {
1612 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1613 strerror(-err), err);
1614 return VK_ERROR_SURFACE_LOST_KHR;
1615 }
1616 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1617 }
1618
Jesse Halld7b994a2015-09-07 14:17:37 -07001619 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001620 int fence_fd;
1621 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001622 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001623 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1624 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1625 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001626 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001627 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001628 }
1629
1630 uint32_t idx;
1631 for (idx = 0; idx < swapchain.num_images; idx++) {
1632 if (swapchain.images[idx].buffer.get() == buffer) {
1633 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001634 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001635 break;
1636 }
1637 }
1638 if (idx == swapchain.num_images) {
1639 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001640 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001641 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001642 }
1643
1644 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001645 if (fence_fd != -1) {
1646 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001647 if (fence_clone == -1) {
1648 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1649 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001650 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001651 }
1652 }
1653
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001654 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001655 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001656 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001657 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1658 // even if the call fails. We could close it ourselves on failure, but
1659 // that would create a race condition if the driver closes it on a
1660 // failure path: some other thread might create an fd with the same
1661 // number between the time the driver closes it and the time we close
1662 // it. We must assume one of: the driver *always* closes it even on
1663 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001664 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001665 swapchain.images[idx].dequeued = false;
1666 swapchain.images[idx].dequeue_fence = -1;
1667 return result;
1668 }
1669
1670 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001671 return VK_SUCCESS;
1672}
1673
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001674VKAPI_ATTR
1675VkResult AcquireNextImage2KHR(VkDevice device,
1676 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1677 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001678 ATRACE_CALL();
1679
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001680 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1681 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1682 pAcquireInfo->fence, pImageIndex);
1683}
1684
Jesse Halldc225072016-05-30 22:40:14 -07001685static VkResult WorstPresentResult(VkResult a, VkResult b) {
1686 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1687 // (in spec version 1.0.14).
1688 static const VkResult kWorstToBest[] = {
1689 VK_ERROR_DEVICE_LOST,
1690 VK_ERROR_SURFACE_LOST_KHR,
1691 VK_ERROR_OUT_OF_DATE_KHR,
1692 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1693 VK_ERROR_OUT_OF_HOST_MEMORY,
1694 VK_SUBOPTIMAL_KHR,
1695 };
1696 for (auto result : kWorstToBest) {
1697 if (a == result || b == result)
1698 return result;
1699 }
1700 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1701 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1702 return a != VK_SUCCESS ? a : b;
1703}
1704
Jesse Halle1b12782015-11-30 11:27:32 -08001705VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001706VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001707 ATRACE_CALL();
1708
Jesse Halld7b994a2015-09-07 14:17:37 -07001709 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1710 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1711 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001712
Jesse Halldc225072016-05-30 22:40:14 -07001713 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001714 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001715 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001716
Ian Elliottcb351132016-12-13 10:30:40 -07001717 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001718 const VkPresentRegionsKHR* present_regions = nullptr;
1719 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001720 const VkPresentRegionsKHR* next =
1721 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1722 while (next) {
1723 switch (next->sType) {
1724 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1725 present_regions = next;
1726 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001727 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001728 present_times =
1729 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1730 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001731 default:
1732 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1733 next->sType);
1734 break;
1735 }
1736 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1737 }
1738 ALOGV_IF(
1739 present_regions &&
1740 present_regions->swapchainCount != present_info->swapchainCount,
1741 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001742 ALOGV_IF(present_times &&
1743 present_times->swapchainCount != present_info->swapchainCount,
1744 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1745 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001746 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001747 (present_regions) ? present_regions->pRegions : nullptr;
1748 const VkPresentTimeGOOGLE* times =
1749 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001750 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001751 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001752 uint32_t nrects = 0;
1753
Jesse Halld7b994a2015-09-07 14:17:37 -07001754 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1755 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001756 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001757 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001758 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001759 const VkPresentRegionKHR* region =
1760 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001761 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001762 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001763 VkResult result;
1764 int err;
1765
Jesse Halld7b994a2015-09-07 14:17:37 -07001766 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001767 result = dispatch.QueueSignalReleaseImageANDROID(
1768 queue, present_info->waitSemaphoreCount,
1769 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001770 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001771 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001772 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001773 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001774 if (img.release_fence >= 0)
1775 close(img.release_fence);
1776 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001777
Jesse Halldc225072016-05-30 22:40:14 -07001778 if (swapchain.surface.swapchain_handle ==
1779 present_info->pSwapchains[sc]) {
1780 ANativeWindow* window = swapchain.surface.window.get();
1781 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001782 if (region) {
1783 // Process the incremental-present hint for this swapchain:
1784 uint32_t rcount = region->rectangleCount;
1785 if (rcount > nrects) {
1786 android_native_rect_t* new_rects =
1787 static_cast<android_native_rect_t*>(
1788 allocator->pfnReallocation(
1789 allocator->pUserData, rects,
1790 sizeof(android_native_rect_t) * rcount,
1791 alignof(android_native_rect_t),
1792 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1793 if (new_rects) {
1794 rects = new_rects;
1795 nrects = rcount;
1796 } else {
1797 rcount = 0; // Ignore the hint for this swapchain
1798 }
1799 }
1800 for (uint32_t r = 0; r < rcount; ++r) {
1801 if (region->pRectangles[r].layer > 0) {
1802 ALOGV(
1803 "vkQueuePresentKHR ignoring invalid layer "
1804 "(%u); using layer 0 instead",
1805 region->pRectangles[r].layer);
1806 }
1807 int x = region->pRectangles[r].offset.x;
1808 int y = region->pRectangles[r].offset.y;
1809 int width = static_cast<int>(
1810 region->pRectangles[r].extent.width);
1811 int height = static_cast<int>(
1812 region->pRectangles[r].extent.height);
1813 android_native_rect_t* cur_rect = &rects[r];
1814 cur_rect->left = x;
1815 cur_rect->top = y + height;
1816 cur_rect->right = x + width;
1817 cur_rect->bottom = y;
1818 }
1819 native_window_set_surface_damage(window, rects, rcount);
1820 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001821 if (time) {
1822 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001823 ALOGV(
1824 "Calling "
1825 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001826 native_window_enable_frame_timestamps(window, true);
1827 swapchain.frame_timestamps_enabled = true;
1828 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001829
1830 // Record the nativeFrameId so it can be later correlated to
1831 // this present.
1832 uint64_t nativeFrameId = 0;
1833 err = native_window_get_next_frame_id(
1834 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001835 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001836 ALOGE("Failed to get next native frame ID.");
1837 }
1838
1839 // Add a new timing record with the user's presentID and
1840 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001841 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001842 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001843 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001844 }
1845 if (time->desiredPresentTime) {
1846 // Set the desiredPresentTime:
1847 ALOGV(
1848 "Calling "
1849 "native_window_set_buffers_timestamp(%" PRId64 ")",
1850 time->desiredPresentTime);
1851 native_window_set_buffers_timestamp(
1852 window,
1853 static_cast<int64_t>(time->desiredPresentTime));
1854 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001855 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001856
Jesse Halldc225072016-05-30 22:40:14 -07001857 err = window->queueBuffer(window, img.buffer.get(), fence);
1858 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001859 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001860 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1861 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001862 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001863 } else {
1864 if (img.dequeue_fence >= 0) {
1865 close(img.dequeue_fence);
1866 img.dequeue_fence = -1;
1867 }
1868 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001869 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001870
1871 // If the swapchain is in shared mode, immediately dequeue the
1872 // buffer so it can be presented again without an intervening
1873 // call to AcquireNextImageKHR. We expect to get the same buffer
1874 // back from every call to dequeueBuffer in this mode.
1875 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1876 ANativeWindowBuffer* buffer;
1877 int fence_fd;
1878 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001879 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001880 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1881 swapchain_result = WorstPresentResult(swapchain_result,
1882 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001883 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001884 ALOGE("got wrong image back for shared swapchain");
1885 swapchain_result = WorstPresentResult(swapchain_result,
1886 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001887 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13001888 img.dequeue_fence = fence_fd;
1889 img.dequeued = true;
1890 }
1891 }
Jesse Halldc225072016-05-30 22:40:14 -07001892 }
1893 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001894 OrphanSwapchain(device, &swapchain);
1895 }
silence_dogood73597592019-05-23 16:57:37 -07001896 int window_transform_hint;
1897 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1898 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001899 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07001900 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1901 strerror(-err), err);
1902 swapchain_result = WorstPresentResult(
1903 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1904 }
1905 if (swapchain.pre_transform != window_transform_hint) {
1906 swapchain_result =
1907 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1908 }
Jesse Halldc225072016-05-30 22:40:14 -07001909 } else {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001910 ReleaseSwapchainImage(device, swapchain.shared, nullptr, fence,
1911 img, true);
Jesse Halldc225072016-05-30 22:40:14 -07001912 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001913 }
1914
Jesse Halla9e57032015-11-30 01:03:10 -08001915 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001916 present_info->pResults[sc] = swapchain_result;
1917
1918 if (swapchain_result != final_result)
1919 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001920 }
Ian Elliottcb351132016-12-13 10:30:40 -07001921 if (rects) {
1922 allocator->pfnFree(allocator->pUserData, rects);
1923 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001924
1925 return final_result;
1926}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001927
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001928VKAPI_ATTR
1929VkResult GetRefreshCycleDurationGOOGLE(
1930 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001931 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001932 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001933 ATRACE_CALL();
1934
Ian Elliott62c48c92017-01-20 13:13:20 -07001935 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001936 VkResult result = VK_SUCCESS;
1937
Ian Elliott3568bfe2019-05-03 15:54:46 -06001938 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001939
1940 return result;
1941}
1942
1943VKAPI_ATTR
1944VkResult GetPastPresentationTimingGOOGLE(
1945 VkDevice,
1946 VkSwapchainKHR swapchain_handle,
1947 uint32_t* count,
1948 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001949 ATRACE_CALL();
1950
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001951 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001952 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1953 return VK_ERROR_OUT_OF_DATE_KHR;
1954 }
1955
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001956 ANativeWindow* window = swapchain.surface.window.get();
1957 VkResult result = VK_SUCCESS;
1958
1959 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001960 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001961 native_window_enable_frame_timestamps(window, true);
1962 swapchain.frame_timestamps_enabled = true;
1963 }
1964
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001965 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07001966 // Get the latest ready timing count before copying, since the copied
1967 // timing info will be erased in copy_ready_timings function.
1968 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07001969 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001970 // Check the *count here against the recorded ready timing count, since
1971 // *count can be overwritten per spec describes.
1972 if (*count < n) {
1973 result = VK_INCOMPLETE;
1974 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001975 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001976 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001977 }
1978
1979 return result;
1980}
1981
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001982VKAPI_ATTR
1983VkResult GetSwapchainStatusKHR(
1984 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001985 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001986 ATRACE_CALL();
1987
Chris Forbes4e18ba82017-01-20 12:50:17 +13001988 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001989 VkResult result = VK_SUCCESS;
1990
Chris Forbes4e18ba82017-01-20 12:50:17 +13001991 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1992 return VK_ERROR_OUT_OF_DATE_KHR;
1993 }
1994
Yiwei Zhanga885c062019-10-24 12:07:57 -07001995 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001996
1997 return result;
1998}
1999
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002000VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002001 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002002 uint32_t swapchainCount,
2003 const VkSwapchainKHR* pSwapchains,
2004 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08002005 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08002006
2007 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
2008 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
2009 if (!swapchain)
2010 continue;
2011
2012 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
2013
2014 ANativeWindow* window = swapchain->surface.window.get();
2015
2016 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
2017 const android_smpte2086_metadata smpteMetdata = {
2018 {vulkanMetadata.displayPrimaryRed.x,
2019 vulkanMetadata.displayPrimaryRed.y},
2020 {vulkanMetadata.displayPrimaryGreen.x,
2021 vulkanMetadata.displayPrimaryGreen.y},
2022 {vulkanMetadata.displayPrimaryBlue.x,
2023 vulkanMetadata.displayPrimaryBlue.y},
2024 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
2025 vulkanMetadata.maxLuminance,
2026 vulkanMetadata.minLuminance};
2027 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
2028
2029 const android_cta861_3_metadata cta8613Metadata = {
2030 vulkanMetadata.maxContentLightLevel,
2031 vulkanMetadata.maxFrameAverageLightLevel};
2032 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
2033 }
2034
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002035 return;
2036}
2037
Yiwei Zhang0f475222019-04-11 19:38:00 -07002038static void InterceptBindImageMemory2(
2039 uint32_t bind_info_count,
2040 const VkBindImageMemoryInfo* bind_infos,
2041 std::vector<VkNativeBufferANDROID>* out_native_buffers,
2042 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
2043 out_native_buffers->clear();
2044 out_bind_infos->clear();
2045
2046 if (!bind_info_count)
2047 return;
2048
2049 std::unordered_set<uint32_t> intercepted_indexes;
2050
2051 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2052 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2053 bind_infos[idx].pNext);
2054 while (info &&
2055 info->sType !=
2056 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
2057 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2058 info->pNext);
2059 }
2060
2061 if (!info)
2062 continue;
2063
2064 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
2065 "swapchain handle must not be NULL");
2066 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
2067 ALOG_ASSERT(
2068 info->imageIndex < swapchain->num_images,
2069 "imageIndex must be less than the number of images in swapchain");
2070
2071 ANativeWindowBuffer* buffer =
2072 swapchain->images[info->imageIndex].buffer.get();
2073 VkNativeBufferANDROID native_buffer = {
2074#pragma clang diagnostic push
2075#pragma clang diagnostic ignored "-Wold-style-cast"
2076 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2077#pragma clang diagnostic pop
2078 .pNext = bind_infos[idx].pNext,
2079 .handle = buffer->handle,
2080 .stride = buffer->stride,
2081 .format = buffer->format,
2082 .usage = int(buffer->usage),
2083 };
2084 // Reserve enough space to avoid letting re-allocation invalidate the
2085 // addresses of the elements inside.
2086 out_native_buffers->reserve(bind_info_count);
2087 out_native_buffers->emplace_back(native_buffer);
2088
2089 // Reserve the space now since we know how much is needed now.
2090 out_bind_infos->reserve(bind_info_count);
2091 out_bind_infos->emplace_back(bind_infos[idx]);
2092 out_bind_infos->back().pNext = &out_native_buffers->back();
2093
2094 intercepted_indexes.insert(idx);
2095 }
2096
2097 if (intercepted_indexes.empty())
2098 return;
2099
2100 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2101 if (intercepted_indexes.count(idx))
2102 continue;
2103 out_bind_infos->emplace_back(bind_infos[idx]);
2104 }
2105}
2106
Yiwei Zhang23143102019-04-10 18:24:05 -07002107VKAPI_ATTR
2108VkResult BindImageMemory2(VkDevice device,
2109 uint32_t bindInfoCount,
2110 const VkBindImageMemoryInfo* pBindInfos) {
2111 ATRACE_CALL();
2112
Yiwei Zhang0f475222019-04-11 19:38:00 -07002113 // out_native_buffers is for maintaining the lifecycle of the constructed
2114 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2115 std::vector<VkNativeBufferANDROID> out_native_buffers;
2116 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2117 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2118 &out_bind_infos);
2119 return GetData(device).driver.BindImageMemory2(
2120 device, bindInfoCount,
2121 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002122}
2123
2124VKAPI_ATTR
2125VkResult BindImageMemory2KHR(VkDevice device,
2126 uint32_t bindInfoCount,
2127 const VkBindImageMemoryInfo* pBindInfos) {
2128 ATRACE_CALL();
2129
Yiwei Zhang0f475222019-04-11 19:38:00 -07002130 std::vector<VkNativeBufferANDROID> out_native_buffers;
2131 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2132 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2133 &out_bind_infos);
2134 return GetData(device).driver.BindImageMemory2KHR(
2135 device, bindInfoCount,
2136 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002137}
2138
Chia-I Wu62262232016-03-26 07:06:44 +08002139} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002140} // namespace vulkan