blob: 0a337601a686790620949e2ba5cdce7461bd0557 [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>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070023#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070024#include <system/window.h>
25#include <ui/BufferQueueDefs.h>
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -050026#include <ui/DebugUtils.h>
27#include <ui/PixelFormat.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080028#include <utils/StrongPointer.h>
Yiwei Zhang705c2e62019-12-18 23:12:43 -080029#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080030#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070031
32#include <algorithm>
33#include <unordered_set>
34#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070035
Chia-I Wu4a6a9162016-03-26 07:17:34 +080036#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070037
Daniel Kochf25f5bb2017-10-05 00:26:58 -040038using android::hardware::graphics::common::V1_0::BufferUsage;
39
Chia-I Wu62262232016-03-26 07:06:44 +080040namespace vulkan {
41namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070042
Jesse Halld7b994a2015-09-07 14:17:37 -070043namespace {
44
Jesse Hall55bc0972016-02-23 16:43:29 -080045const VkSurfaceTransformFlagsKHR kSupportedTransforms =
46 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
47 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
48 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
49 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070050 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
51 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
52 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
53 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080054 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
55
56VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
57 // Native and Vulkan transforms are isomorphic, but are represented
58 // differently. Vulkan transforms are built up of an optional horizontal
59 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
60 // transforms are built up from a horizontal flip, vertical flip, and
61 // 90-degree rotation, all optional but always in that order.
62
Jesse Hall55bc0972016-02-23 16:43:29 -080063 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070064 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080065 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070066 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
67 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
68 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
69 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
70 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080071 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070072 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080073 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070074 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
75 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
76 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
77 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
78 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -080079 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
80 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
81 default:
82 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
83 }
84}
85
Yiwei Zhang70a21962019-05-31 17:26:52 -070086int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
87 switch (transform) {
88 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
89 return NATIVE_WINDOW_TRANSFORM_ROT_90;
90 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
91 return NATIVE_WINDOW_TRANSFORM_ROT_180;
92 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
93 return NATIVE_WINDOW_TRANSFORM_ROT_270;
94 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
95 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
96 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
97 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
98 NATIVE_WINDOW_TRANSFORM_ROT_90;
99 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
100 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
101 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
102 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
103 NATIVE_WINDOW_TRANSFORM_ROT_90;
104 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
105 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
106 default:
107 return 0;
108 }
109}
110
Jesse Hall178b6962016-02-24 15:39:50 -0800111int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
112 switch (transform) {
113 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
114 return NATIVE_WINDOW_TRANSFORM_ROT_270;
115 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
116 return NATIVE_WINDOW_TRANSFORM_ROT_180;
117 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
118 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700119 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
120 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
121 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
122 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
123 NATIVE_WINDOW_TRANSFORM_ROT_90;
124 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
125 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
126 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
127 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
128 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800129 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
130 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
131 default:
132 return 0;
133 }
134}
135
Ian Elliott8a977262017-01-19 09:05:58 -0700136class TimingInfo {
137 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800138 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700139 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800140 native_frame_id_(nativeFrameId) {}
141 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700142 return (timestamp_desired_present_time_ !=
143 NATIVE_WINDOW_TIMESTAMP_PENDING &&
144 timestamp_actual_present_time_ !=
145 NATIVE_WINDOW_TIMESTAMP_PENDING &&
146 timestamp_render_complete_time_ !=
147 NATIVE_WINDOW_TIMESTAMP_PENDING &&
148 timestamp_composition_latch_time_ !=
149 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700150 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700151 void calculate(int64_t rdur) {
152 bool anyTimestampInvalid =
153 (timestamp_actual_present_time_ ==
154 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
155 (timestamp_render_complete_time_ ==
156 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
157 (timestamp_composition_latch_time_ ==
158 NATIVE_WINDOW_TIMESTAMP_INVALID);
159 if (anyTimestampInvalid) {
160 ALOGE("Unexpectedly received invalid timestamp.");
161 vals_.actualPresentTime = 0;
162 vals_.earliestPresentTime = 0;
163 vals_.presentMargin = 0;
164 return;
165 }
166
167 vals_.actualPresentTime =
168 static_cast<uint64_t>(timestamp_actual_present_time_);
169 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700170 timestamp_render_complete_time_);
171 // Calculate vals_.earliestPresentTime, and potentially adjust
172 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
173 // is vals_.actualPresentTime. If we can subtract rdur (the duration
174 // of a refresh cycle) from vals_.earliestPresentTime (and also from
175 // vals_.presentMargin) and still leave a positive margin, then we can
176 // report to the application that it could have presented earlier than
177 // it did (per the extension specification). If for some reason, we
178 // can do this subtraction repeatedly, we do, since
179 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700180 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700181 while ((margin > rdur) &&
182 ((early_time - rdur) > timestamp_composition_latch_time_)) {
183 early_time -= rdur;
184 margin -= rdur;
185 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700186 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
187 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700188 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800189 void get_values(VkPastPresentationTimingGOOGLE* values) const {
190 *values = vals_;
191 }
Ian Elliott8a977262017-01-19 09:05:58 -0700192
193 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800194 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700195
Brian Anderson1049d1d2016-12-16 17:25:57 -0800196 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700197 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
198 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
199 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
200 int64_t timestamp_composition_latch_time_
201 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700202};
203
Jesse Hall1356b0d2015-11-23 17:24:58 -0800204struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800205 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700206 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700207 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800208};
209
210VkSurfaceKHR HandleFromSurface(Surface* surface) {
211 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
212}
213
214Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800215 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800216}
217
Ian Elliott8a977262017-01-19 09:05:58 -0700218// Maximum number of TimingInfo structs to keep per swapchain:
219enum { MAX_TIMING_INFOS = 10 };
220// Minimum number of frames to look for in the past (so we don't cause
221// syncronous requests to Surface Flinger):
222enum { MIN_NUM_FRAMES_AGO = 5 };
223
Jesse Hall1356b0d2015-11-23 17:24:58 -0800224struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700225 Swapchain(Surface& surface_,
226 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700227 VkPresentModeKHR present_mode,
228 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700229 : surface(surface_),
230 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700231 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700232 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300233 frame_timestamps_enabled(false),
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800234 acquire_next_image_timeout(-1),
Chris Forbesf8835642017-03-30 19:31:40 +1300235 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800236 present_mode ==
237 VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700238 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700239 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700240 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700241 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700242 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600243 uint64_t get_refresh_duration()
244 {
245 ANativeWindow* window = surface.window.get();
246 native_window_get_refresh_cycle_duration(
247 window,
248 &refresh_duration);
249 return static_cast<uint64_t>(refresh_duration);
250
251 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800252
253 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700254 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700255 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700256 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700257 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700258 int64_t refresh_duration;
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800259 nsecs_t acquire_next_image_timeout;
Chris Forbesf8835642017-03-30 19:31:40 +1300260 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700261
262 struct Image {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700263 Image()
264 : image(VK_NULL_HANDLE),
265 dequeue_fence(-1),
266 release_fence(-1),
267 dequeued(false) {}
Jesse Halld7b994a2015-09-07 14:17:37 -0700268 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800269 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700270 // The fence is only valid when the buffer is dequeued, and should be
271 // -1 any other time. When valid, we own the fd, and must ensure it is
272 // closed: either by closing it explicitly when queueing the buffer,
273 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
274 int dequeue_fence;
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700275 // This fence is a dup of the sync fd returned from the driver via
276 // vkQueueSignalReleaseImageANDROID upon vkQueuePresentKHR. We must
277 // ensure it is closed upon re-presenting or releasing the image.
278 int release_fence;
Jesse Halld7b994a2015-09-07 14:17:37 -0700279 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800280 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700281
Yiwei Zhang5e862202019-06-21 14:59:16 -0700282 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700283};
284
285VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
286 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
287}
288
289Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800290 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700291}
292
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700293static bool IsFencePending(int fd) {
294 if (fd < 0)
295 return false;
296
297 errno = 0;
298 return sync_wait(fd, 0 /* timeout */) == -1 && errno == ETIME;
299}
300
Jesse Halldc225072016-05-30 22:40:14 -0700301void ReleaseSwapchainImage(VkDevice device,
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000302 bool shared_present,
Jesse Halldc225072016-05-30 22:40:14 -0700303 ANativeWindow* window,
304 int release_fence,
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700305 Swapchain::Image& image,
306 bool defer_if_pending) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700307 ATRACE_CALL();
308
Jesse Halldc225072016-05-30 22:40:14 -0700309 ALOG_ASSERT(release_fence == -1 || image.dequeued,
310 "ReleaseSwapchainImage: can't provide a release fence for "
311 "non-dequeued images");
312
313 if (image.dequeued) {
314 if (release_fence >= 0) {
315 // We get here from vkQueuePresentKHR. The application is
316 // responsible for creating an execution dependency chain from
317 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
318 // (release_fence), so we can drop the dequeue_fence here.
319 if (image.dequeue_fence >= 0)
320 close(image.dequeue_fence);
321 } else {
322 // We get here during swapchain destruction, or various serious
323 // error cases e.g. when we can't create the release_fence during
324 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
325 // have already signalled, since the swapchain images are supposed
326 // to be idle before the swapchain is destroyed. In error cases,
327 // there may be rendering in flight to the image, but since we
328 // weren't able to create a release_fence, waiting for the
329 // dequeue_fence is about the best we can do.
330 release_fence = image.dequeue_fence;
331 }
332 image.dequeue_fence = -1;
333
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000334 // It's invalid to call cancelBuffer on a shared buffer
335 if (window && !shared_present) {
Jesse Halldc225072016-05-30 22:40:14 -0700336 window->cancelBuffer(window, image.buffer.get(), release_fence);
337 } else {
338 if (release_fence >= 0) {
339 sync_wait(release_fence, -1 /* forever */);
340 close(release_fence);
341 }
342 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700343 release_fence = -1;
Jesse Halldc225072016-05-30 22:40:14 -0700344 image.dequeued = false;
345 }
346
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700347 if (defer_if_pending && IsFencePending(image.release_fence))
348 return;
349
350 if (image.release_fence >= 0) {
351 close(image.release_fence);
352 image.release_fence = -1;
353 }
354
Jesse Halldc225072016-05-30 22:40:14 -0700355 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700356 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700357 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700358 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700359 image.image = VK_NULL_HANDLE;
360 }
361
362 image.buffer.clear();
363}
364
365void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
366 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
367 return;
Jesse Halldc225072016-05-30 22:40:14 -0700368 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000369 if (!swapchain->images[i].dequeued) {
370 ReleaseSwapchainImage(device, swapchain->shared, nullptr, -1,
371 swapchain->images[i], true);
372 }
Jesse Halldc225072016-05-30 22:40:14 -0700373 }
374 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700375 swapchain->timing.clear();
376}
377
378uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800379 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
380 return 0;
381 }
Ian Elliott8a977262017-01-19 09:05:58 -0700382
Brian Anderson1049d1d2016-12-16 17:25:57 -0800383 uint32_t num_ready = 0;
384 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
385 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700386 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800387 if (ti.ready()) {
388 // This TimingInfo is ready to be reported to the user. Add it
389 // to the num_ready.
390 num_ready++;
391 continue;
392 }
393 // This TimingInfo is not yet ready to be reported to the user,
394 // and so we should look for any available timestamps that
395 // might make it ready.
396 int64_t desired_present_time = 0;
397 int64_t render_complete_time = 0;
398 int64_t composition_latch_time = 0;
399 int64_t actual_present_time = 0;
400 // Obtain timestamps:
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800401 int err = native_window_get_frame_timestamps(
Brian Anderson1049d1d2016-12-16 17:25:57 -0800402 swapchain.surface.window.get(), ti.native_frame_id_,
403 &desired_present_time, &render_complete_time,
404 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700405 nullptr, //&first_composition_start_time,
406 nullptr, //&last_composition_start_time,
407 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800408 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700409 nullptr, //&dequeue_ready_time,
410 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800411
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800412 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800413 continue;
414 }
415
416 // Record the timestamp(s) we received, and then see if this TimingInfo
417 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700418 ti.timestamp_desired_present_time_ = desired_present_time;
419 ti.timestamp_actual_present_time_ = actual_present_time;
420 ti.timestamp_render_complete_time_ = render_complete_time;
421 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800422
423 if (ti.ready()) {
424 // The TimingInfo has received enough timestamps, and should now
425 // use those timestamps to calculate the info that should be
426 // reported to the user:
427 ti.calculate(swapchain.refresh_duration);
428 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700429 }
430 }
431 return num_ready;
432}
433
Ian Elliott8a977262017-01-19 09:05:58 -0700434void copy_ready_timings(Swapchain& swapchain,
435 uint32_t* count,
436 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800437 if (swapchain.timing.empty()) {
438 *count = 0;
439 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700440 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800441
442 size_t last_ready = swapchain.timing.size() - 1;
443 while (!swapchain.timing[last_ready].ready()) {
444 if (last_ready == 0) {
445 *count = 0;
446 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700447 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800448 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700449 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800450
451 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700452 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800453 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
454 const TimingInfo& ti = swapchain.timing[i];
455 if (ti.ready()) {
456 ti.get_values(&timings[num_copied]);
457 num_copied++;
458 }
459 num_to_remove++;
460 }
461
462 // Discard old frames that aren't ready if newer frames are ready.
463 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700464 swapchain.timing.erase(swapchain.timing.begin(),
465 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800466
Ian Elliott8a977262017-01-19 09:05:58 -0700467 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700468}
469
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500470android::PixelFormat GetNativePixelFormat(VkFormat format) {
471 android::PixelFormat native_format = android::PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700472 switch (format) {
473 case VK_FORMAT_R8G8B8A8_UNORM:
474 case VK_FORMAT_R8G8B8A8_SRGB:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500475 native_format = android::PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700476 break;
477 case VK_FORMAT_R5G6B5_UNORM_PACK16:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500478 native_format = android::PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700479 break;
480 case VK_FORMAT_R16G16B16A16_SFLOAT:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500481 native_format = android::PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700482 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800483 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500484 native_format = android::PIXEL_FORMAT_RGBA_1010102;
485 break;
486 case VK_FORMAT_R8_UNORM:
487 native_format = android::PIXEL_FORMAT_R_8;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700488 break;
489 default:
490 ALOGV("unsupported swapchain format %d", format);
491 break;
492 }
493 return native_format;
494}
495
496android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
497 switch (colorspace) {
498 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
499 return HAL_DATASPACE_V0_SRGB;
500 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
501 return HAL_DATASPACE_DISPLAY_P3;
502 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
503 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600504 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
505 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700506 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
507 return HAL_DATASPACE_DCI_P3_LINEAR;
508 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
509 return HAL_DATASPACE_DCI_P3;
510 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
511 return HAL_DATASPACE_V0_SRGB_LINEAR;
512 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
513 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600514 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
515 return HAL_DATASPACE_BT2020_LINEAR;
516 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700517 return static_cast<android_dataspace>(
518 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
519 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600520 case VK_COLOR_SPACE_DOLBYVISION_EXT:
521 return static_cast<android_dataspace>(
522 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
523 HAL_DATASPACE_RANGE_FULL);
524 case VK_COLOR_SPACE_HDR10_HLG_EXT:
525 return static_cast<android_dataspace>(
526 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
527 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700528 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
529 return static_cast<android_dataspace>(
530 HAL_DATASPACE_STANDARD_ADOBE_RGB |
531 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
532 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
533 return HAL_DATASPACE_ADOBE_RGB;
534
535 // Pass through is intended to allow app to provide data that is passed
536 // to the display system without modification.
537 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
538 return HAL_DATASPACE_ARBITRARY;
539
540 default:
541 // This indicates that we don't know about the
542 // dataspace specified and we should indicate that
543 // it's unsupported
544 return HAL_DATASPACE_UNKNOWN;
545 }
546}
547
Jesse Halld7b994a2015-09-07 14:17:37 -0700548} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700549
Jesse Halle1b12782015-11-30 11:27:32 -0800550VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800551VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800552 VkInstance instance,
553 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
554 const VkAllocationCallbacks* allocator,
555 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800556 ATRACE_CALL();
557
Jesse Hall1f91d392015-12-11 16:28:44 -0800558 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800559 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800560 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
561 alignof(Surface),
562 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800563 if (!mem)
564 return VK_ERROR_OUT_OF_HOST_MEMORY;
565 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700566
Chia-I Wue8e689f2016-04-18 08:21:31 +0800567 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700568 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700569 int err = native_window_get_consumer_usage(surface->window.get(),
570 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800571 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700572 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
573 strerror(-err), err);
574 surface->~Surface();
575 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700576 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700577 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700578
Yiwei Zhang6435b322018-05-08 11:12:17 -0700579 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800580 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800581 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800582 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
583 err);
584 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800585 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700586 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800587 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700588
Jesse Hall1356b0d2015-11-23 17:24:58 -0800589 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700590 return VK_SUCCESS;
591}
592
Jesse Halle1b12782015-11-30 11:27:32 -0800593VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800594void DestroySurfaceKHR(VkInstance instance,
595 VkSurfaceKHR surface_handle,
596 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800597 ATRACE_CALL();
598
Jesse Hall1356b0d2015-11-23 17:24:58 -0800599 Surface* surface = SurfaceFromHandle(surface_handle);
600 if (!surface)
601 return;
602 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700603 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700604 "destroyed VkSurfaceKHR 0x%" PRIx64
605 " has active VkSwapchainKHR 0x%" PRIx64,
606 reinterpret_cast<uint64_t>(surface_handle),
607 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800608 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800609 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800610 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800611 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800612}
613
Jesse Halle1b12782015-11-30 11:27:32 -0800614VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800615VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
616 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000617 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800618 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000619 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800620 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800621}
622
Jesse Halle1b12782015-11-30 11:27:32 -0800623VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800624VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Ian Elliott1ce053f2022-03-16 09:49:53 -0600625 VkPhysicalDevice pdev,
Jesse Hallb00daad2015-11-29 19:46:20 -0800626 VkSurfaceKHR surface,
627 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800628 ATRACE_CALL();
629
Jesse Halld7b994a2015-09-07 14:17:37 -0700630 int err;
Jesse Halld7b994a2015-09-07 14:17:37 -0700631 int width, height;
Jesse Hall55bc0972016-02-23 16:43:29 -0800632 int transform_hint;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800633 int max_buffer_count;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600634 if (surface == VK_NULL_HANDLE) {
635 const InstanceData& instance_data = GetData(pdev);
636 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
637 bool surfaceless_enabled =
638 instance_data.hook_extensions.test(surfaceless);
639 if (!surfaceless_enabled) {
640 // It is an error to pass a surface==VK_NULL_HANDLE unless the
641 // VK_GOOGLE_surfaceless_query extension is enabled
642 return VK_ERROR_SURFACE_LOST_KHR;
643 }
644 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
645 // extension for this function is for
646 // VkSurfaceProtectedCapabilitiesKHR::supportsProtected. The following
647 // four values cannot be known without a surface. Default values will
648 // be supplied anyway, but cannot be relied upon.
649 width = 1000;
650 height = 1000;
651 transform_hint = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
652 max_buffer_count = 10;
653 } else {
654 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
655
656 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
657 if (err != android::OK) {
658 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
659 strerror(-err), err);
660 return VK_ERROR_SURFACE_LOST_KHR;
661 }
662 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
663 if (err != android::OK) {
664 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
665 strerror(-err), err);
666 return VK_ERROR_SURFACE_LOST_KHR;
667 }
668
669 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
670 &transform_hint);
671 if (err != android::OK) {
672 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
673 strerror(-err), err);
674 return VK_ERROR_SURFACE_LOST_KHR;
675 }
676
677 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT,
678 &max_buffer_count);
679 if (err != android::OK) {
680 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
681 strerror(-err), err);
682 return VK_ERROR_SURFACE_LOST_KHR;
683 }
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800684 }
Ian Elliott16c443c2021-11-30 17:10:32 -0700685 capabilities->minImageCount = std::min(max_buffer_count, 3);
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800686 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700687
Jesse Hallfe2662d2016-02-09 13:26:59 -0800688 capabilities->currentExtent =
689 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
690
Yiwei Zhang70a21962019-05-31 17:26:52 -0700691 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800692 capabilities->minImageExtent = VkExtent2D{1, 1};
693 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700694
Jesse Hallfe2662d2016-02-09 13:26:59 -0800695 capabilities->maxImageArrayLayers = 1;
696
Jesse Hall55bc0972016-02-23 16:43:29 -0800697 capabilities->supportedTransforms = kSupportedTransforms;
698 capabilities->currentTransform =
699 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700700
Jesse Hallfe2662d2016-02-09 13:26:59 -0800701 // On Android, window composition is a WindowManager property, not something
702 // associated with the bufferqueue. It can't be changed from here.
703 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700704
Jesse Hallb00daad2015-11-29 19:46:20 -0800705 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800706 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
707 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
708 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700709 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
710
Jesse Hallb1352bc2015-09-04 16:12:33 -0700711 return VK_SUCCESS;
712}
713
Jesse Halle1b12782015-11-30 11:27:32 -0800714VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700715VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
716 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800717 uint32_t* count,
718 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800719 ATRACE_CALL();
720
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700721 const InstanceData& instance_data = GetData(pdev);
722
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700723 bool wide_color_support = false;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600724 uint64_t consumer_usage = 0;
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000725 bool colorspace_ext =
Ian Elliottf6df08e2022-03-16 21:27:49 -0600726 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600727 if (surface_handle == VK_NULL_HANDLE) {
728 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
729 bool surfaceless_enabled =
730 instance_data.hook_extensions.test(surfaceless);
731 if (!surfaceless_enabled) {
732 return VK_ERROR_SURFACE_LOST_KHR;
733 }
734 // Support for VK_GOOGLE_surfaceless_query. The EGL loader
735 // unconditionally supports wide color formats, even if they will cause
736 // a SurfaceFlinger fallback. Based on that, wide_color_support will be
737 // set to true in this case.
738 wide_color_support = true;
739
740 // TODO(b/203826952): research proper value; temporarily use the
741 // values seen on Pixel
742 consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
743 } else {
744 Surface& surface = *SurfaceFromHandle(surface_handle);
745 int err = native_window_get_wide_color_support(surface.window.get(),
746 &wide_color_support);
747 if (err) {
748 return VK_ERROR_SURFACE_LOST_KHR;
749 }
750 ALOGV("wide_color_support is: %d", wide_color_support);
751
752 consumer_usage = surface.consumer_usage;
Ian Elliott6ba85d92022-02-18 16:44:58 -0700753 }
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000754 wide_color_support = wide_color_support && colorspace_ext;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700755
Charlie Lao324191f2019-12-13 11:03:16 -0800756 AHardwareBuffer_Desc desc = {};
757 desc.width = 1;
758 desc.height = 1;
759 desc.layers = 1;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600760 desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
Charlie Lao324191f2019-12-13 11:03:16 -0800761 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
762
763 // We must support R8G8B8A8
764 std::vector<VkSurfaceFormatKHR> all_formats = {
765 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Neha Jainf9a9b902022-07-20 02:24:57 +0000766 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}};
Charlie Lao324191f2019-12-13 11:03:16 -0800767
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000768 if (colorspace_ext) {
769 all_formats.emplace_back(VkSurfaceFormatKHR{
770 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_BT709_LINEAR_EXT});
771 }
772
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700773 if (wide_color_support) {
Charlie Lao324191f2019-12-13 11:03:16 -0800774 all_formats.emplace_back(VkSurfaceFormatKHR{
775 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
776 all_formats.emplace_back(VkSurfaceFormatKHR{
777 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
778 }
779
Ian Elliott1ce053f2022-03-16 09:49:53 -0600780 // NOTE: Any new formats that are added must be coordinated across different
781 // Android users. This includes the ANGLE team (a layered implementation of
782 // OpenGL-ES).
783
Charlie Lao324191f2019-12-13 11:03:16 -0800784 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
785 if (AHardwareBuffer_isSupported(&desc)) {
786 all_formats.emplace_back(VkSurfaceFormatKHR{
787 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnak52ac1e92022-07-18 13:33:10 -0700788 all_formats.emplace_back(VkSurfaceFormatKHR{
789 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800790 }
791
792 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
793 if (AHardwareBuffer_isSupported(&desc)) {
794 all_formats.emplace_back(VkSurfaceFormatKHR{
795 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnak52ac1e92022-07-18 13:33:10 -0700796 all_formats.emplace_back(VkSurfaceFormatKHR{
797 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800798 if (wide_color_support) {
799 all_formats.emplace_back(
800 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
801 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
802 all_formats.emplace_back(
803 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
804 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
805 }
806 }
807
808 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
809 if (AHardwareBuffer_isSupported(&desc)) {
810 all_formats.emplace_back(
811 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
812 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnak52ac1e92022-07-18 13:33:10 -0700813 all_formats.emplace_back(
814 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
815 VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800816 if (wide_color_support) {
817 all_formats.emplace_back(
818 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
819 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
820 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700821 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700822
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500823 desc.format = AHARDWAREBUFFER_FORMAT_R8_UNORM;
824 if (AHardwareBuffer_isSupported(&desc)) {
825 all_formats.emplace_back(
826 VkSurfaceFormatKHR{VK_FORMAT_R8_UNORM,
827 VK_COLOR_SPACE_PASS_THROUGH_EXT});
828 }
829
Ian Elliott6ba85d92022-02-18 16:44:58 -0700830 // NOTE: Any new formats that are added must be coordinated across different
831 // Android users. This includes the ANGLE team (a layered implementation of
832 // OpenGL-ES).
833
Jesse Halld7b994a2015-09-07 14:17:37 -0700834 VkResult result = VK_SUCCESS;
835 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800836 uint32_t transfer_count = all_formats.size();
837 if (transfer_count > *count) {
838 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700839 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700840 }
Charlie Lao324191f2019-12-13 11:03:16 -0800841 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
842 formats);
843 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700844 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800845 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700846 }
Charlie Lao324191f2019-12-13 11:03:16 -0800847
Jesse Halld7b994a2015-09-07 14:17:37 -0700848 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700849}
850
Jesse Halle1b12782015-11-30 11:27:32 -0800851VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300852VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
853 VkPhysicalDevice physicalDevice,
854 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
855 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800856 ATRACE_CALL();
857
Chris Forbes2452cf72017-03-16 16:30:17 +1300858 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
859 physicalDevice, pSurfaceInfo->surface,
860 &pSurfaceCapabilities->surfaceCapabilities);
861
Chris Forbes06bc0092017-03-16 16:46:05 +1300862 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
863 while (caps->pNext) {
864 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
865
866 switch (caps->sType) {
867 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
868 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
869 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
870 caps);
871 // Claim same set of usage flags are supported for
872 // shared present modes as for other modes.
873 shared_caps->sharedPresentSupportedUsageFlags =
874 pSurfaceCapabilities->surfaceCapabilities
875 .supportedUsageFlags;
876 } break;
877
Ian Elliottbb67b242022-03-16 09:52:28 -0600878 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
879 VkSurfaceProtectedCapabilitiesKHR* protected_caps =
880 reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(caps);
881 protected_caps->supportsProtected = VK_TRUE;
882 } break;
883
Chris Forbes06bc0092017-03-16 16:46:05 +1300884 default:
885 // Ignore all other extension structs
886 break;
887 }
888 }
889
Chris Forbes2452cf72017-03-16 16:30:17 +1300890 return result;
891}
892
893VKAPI_ATTR
894VkResult GetPhysicalDeviceSurfaceFormats2KHR(
895 VkPhysicalDevice physicalDevice,
896 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
897 uint32_t* pSurfaceFormatCount,
898 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800899 ATRACE_CALL();
900
Chris Forbes2452cf72017-03-16 16:30:17 +1300901 if (!pSurfaceFormats) {
902 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
903 pSurfaceInfo->surface,
904 pSurfaceFormatCount, nullptr);
905 } else {
906 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
907 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700908 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300909 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
910 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700911 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300912
913 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
914 // marshal results individually due to stride difference.
915 // completely ignore any chained extension structs.
916 uint32_t formats_to_marshal = *pSurfaceFormatCount;
917 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
918 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
919 }
920 }
921
922 return result;
923 }
924}
925
926VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300927VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800928 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800929 uint32_t* count,
930 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800931 ATRACE_CALL();
932
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800933 int err;
934 int query_value;
Ian Elliotte7f036c2022-03-15 16:49:21 -0600935 std::vector<VkPresentModeKHR> present_modes;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600936 if (surface == VK_NULL_HANDLE) {
937 const InstanceData& instance_data = GetData(pdev);
938 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
939 bool surfaceless_enabled =
940 instance_data.hook_extensions.test(surfaceless);
941 if (!surfaceless_enabled) {
942 return VK_ERROR_SURFACE_LOST_KHR;
943 }
944 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
945 // extension for this function is for
946 // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and
947 // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot
948 // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a
Ian Elliott7e361142022-06-02 10:45:17 -0600949 // surface, and that cannot be relied upon. Therefore, don't return it.
Ian Elliott1ce053f2022-03-16 09:49:53 -0600950 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
951 } else {
952 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
953
954 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
955 &query_value);
956 if (err != android::OK || query_value < 0) {
957 ALOGE(
958 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
959 "value=%d",
960 strerror(-err), err, query_value);
961 return VK_ERROR_SURFACE_LOST_KHR;
962 }
963 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
964
965 err =
966 window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
967 if (err != android::OK || query_value < 0) {
968 ALOGE(
969 "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
970 strerror(-err), err, query_value);
971 return VK_ERROR_SURFACE_LOST_KHR;
972 }
973 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
974
975 if (min_undequeued_buffers + 1 < max_buffer_count)
976 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
977 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
978 }
Chris Forbese8d79a62017-02-22 12:49:18 +1300979
980 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700981 QueryPresentationProperties(pdev, &present_properties);
982 if (present_properties.sharedImage) {
983 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
984 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300985 }
986
987 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700988
989 VkResult result = VK_SUCCESS;
990 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300991 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700992 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300993 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -0700994 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700995 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300996 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700997 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700998 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700999}
1000
Jesse Halle1b12782015-11-30 11:27:32 -08001001VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001002VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001003 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001004 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001005 ATRACE_CALL();
1006
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001007 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
1008 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
1009 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
1010 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
1011 pDeviceGroupPresentCapabilities->sType);
1012
1013 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
1014 sizeof(pDeviceGroupPresentCapabilities->presentMask));
1015
1016 // assume device group of size 1
1017 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
1018 pDeviceGroupPresentCapabilities->modes =
1019 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1020
1021 return VK_SUCCESS;
1022}
1023
1024VKAPI_ATTR
1025VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001026 VkDevice,
1027 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001028 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001029 ATRACE_CALL();
1030
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001031 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1032 return VK_SUCCESS;
1033}
1034
1035VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -06001036VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001037 VkSurfaceKHR surface,
1038 uint32_t* pRectCount,
1039 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001040 ATRACE_CALL();
1041
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001042 if (!pRects) {
1043 *pRectCount = 1;
1044 } else {
1045 uint32_t count = std::min(*pRectCount, 1u);
1046 bool incomplete = *pRectCount < 1;
1047
1048 *pRectCount = count;
1049
1050 if (incomplete) {
1051 return VK_INCOMPLETE;
1052 }
1053
1054 int err;
1055 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1056
1057 int width = 0, height = 0;
1058 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001059 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001060 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1061 strerror(-err), err);
1062 }
1063 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001064 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001065 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1066 strerror(-err), err);
1067 }
1068
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001069 pRects[0].offset.x = 0;
1070 pRects[0].offset.y = 0;
1071 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1072 static_cast<uint32_t>(height)};
1073 }
1074 return VK_SUCCESS;
1075}
1076
Yiwei Zhang533cea92019-06-03 18:43:24 -07001077static void DestroySwapchainInternal(VkDevice device,
1078 VkSwapchainKHR swapchain_handle,
1079 const VkAllocationCallbacks* allocator) {
1080 ATRACE_CALL();
1081
1082 const auto& dispatch = GetData(device).driver;
1083 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1084 if (!swapchain) {
1085 return;
1086 }
1087
1088 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1089 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1090
1091 if (window && swapchain->frame_timestamps_enabled) {
1092 native_window_enable_frame_timestamps(window, false);
1093 }
1094
1095 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001096 ReleaseSwapchainImage(device, swapchain->shared, window, -1,
1097 swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001098 }
1099
1100 if (active) {
1101 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1102 }
1103
1104 if (!allocator) {
1105 allocator = &GetData(device).allocator;
1106 }
1107
1108 swapchain->~Swapchain();
1109 allocator->pfnFree(allocator->pUserData, swapchain);
1110}
1111
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001112VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001113VkResult CreateSwapchainKHR(VkDevice device,
1114 const VkSwapchainCreateInfoKHR* create_info,
1115 const VkAllocationCallbacks* allocator,
1116 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001117 ATRACE_CALL();
1118
Jesse Halld7b994a2015-09-07 14:17:37 -07001119 int err;
1120 VkResult result = VK_SUCCESS;
1121
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001122 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1123 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1124 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1125 " oldSwapchain=0x%" PRIx64,
1126 reinterpret_cast<uint64_t>(create_info->surface),
1127 create_info->minImageCount, create_info->imageFormat,
1128 create_info->imageColorSpace, create_info->imageExtent.width,
1129 create_info->imageExtent.height, create_info->imageUsage,
1130 create_info->preTransform, create_info->presentMode,
1131 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1132
Jesse Hall1f91d392015-12-11 16:28:44 -08001133 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001134 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001135
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001136 android::PixelFormat native_pixel_format =
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001137 GetNativePixelFormat(create_info->imageFormat);
1138 android_dataspace native_dataspace =
1139 GetNativeDataspace(create_info->imageColorSpace);
1140 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1141 ALOGE(
1142 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1143 "failed: Unsupported color space",
1144 create_info->imageColorSpace);
1145 return VK_ERROR_INITIALIZATION_FAILED;
1146 }
1147
Jesse Hall42a9eec2016-06-03 12:39:49 -07001148 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001149 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001150 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001151 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001152 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001153 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001154 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001155 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001156 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1157 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001158 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001159 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001160
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001161 Surface& surface = *SurfaceFromHandle(create_info->surface);
1162
Jesse Halldc225072016-05-30 22:40:14 -07001163 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001164 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001165 " because it already has active swapchain 0x%" PRIx64
1166 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1167 reinterpret_cast<uint64_t>(create_info->surface),
1168 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1169 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1170 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1171 }
1172 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1173 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1174
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001175 // -- Reset the native window --
1176 // The native window might have been used previously, and had its properties
1177 // changed from defaults. That will affect the answer we get for queries
1178 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1179 // attempt such queries.
1180
Jesse Halldc225072016-05-30 22:40:14 -07001181 // The native window only allows dequeueing all buffers before any have
1182 // been queued, since after that point at least one is assumed to be in
1183 // non-FREE state at any given time. Disconnecting and re-connecting
1184 // orphans the previous buffers, getting us back to the state where we can
1185 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001186 //
1187 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001188 ANativeWindow* window = surface.window.get();
1189 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1190 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001191 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001192 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1193 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001194 strerror(-err), err);
1195
Nicolas Capens147b7da2021-04-09 14:53:06 -04001196 err =
1197 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001198 if (err != android::OK) {
1199 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1200 strerror(-err), err);
1201 return VK_ERROR_SURFACE_LOST_KHR;
1202 }
1203
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301204 int swap_interval =
1205 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001206 err = window->setSwapInterval(window, swap_interval);
1207 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001208 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1209 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001210 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001211 }
1212
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001213 err = native_window_set_shared_buffer_mode(window, false);
1214 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001215 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1216 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001217 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001218 }
1219
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001220 err = native_window_set_auto_refresh(window, false);
1221 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001222 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1223 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001224 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001225 }
1226
Jesse Halld7b994a2015-09-07 14:17:37 -07001227 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001228
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001229 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001230
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001231 err = native_window_set_buffers_format(window, native_pixel_format);
1232 if (err != android::OK) {
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001233 ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)",
1234 decodePixelFormat(native_pixel_format).c_str(), strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001235 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001236 }
Yiwei Zhang51572c22022-07-22 23:08:30 +00001237
1238 /* Respect consumer default dataspace upon HAL_DATASPACE_ARBITRARY. */
1239 if (native_dataspace != HAL_DATASPACE_ARBITRARY) {
1240 err = native_window_set_buffers_data_space(window, native_dataspace);
1241 if (err != android::OK) {
1242 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1243 native_dataspace, strerror(-err), err);
1244 return VK_ERROR_SURFACE_LOST_KHR;
1245 }
Jesse Hall517274a2016-02-10 00:07:18 -08001246 }
1247
Jesse Hall3dd678a2016-01-08 21:52:01 -08001248 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001249 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001250 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001251 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001252 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1253 create_info->imageExtent.width, create_info->imageExtent.height,
1254 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001255 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001256 }
1257
Jesse Hall178b6962016-02-24 15:39:50 -08001258 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1259 // applied during rendering. native_window_set_transform() expects the
1260 // inverse: the transform the app is requesting that the compositor perform
1261 // during composition. With native windows, pre-transform works by rendering
1262 // with the same transform the compositor is applying (as in Vulkan), but
1263 // then requesting the inverse transform, so that when the compositor does
1264 // it's job the two transforms cancel each other out and the compositor ends
1265 // up applying an identity transform to the app's buffer.
1266 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001267 window, InvertTransformToNative(create_info->preTransform));
1268 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001269 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1270 InvertTransformToNative(create_info->preTransform),
1271 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001272 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001273 }
1274
Jesse Hallf64ca122015-11-03 16:11:10 -08001275 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001276 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1277 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001278 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1279 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001280 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001281 }
1282
Chris Forbes97ef4612017-03-30 19:37:50 +13001283 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1284 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1285 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1286 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001287 err = native_window_set_shared_buffer_mode(window, true);
1288 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001289 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1290 return VK_ERROR_SURFACE_LOST_KHR;
1291 }
1292 }
1293
1294 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001295 err = native_window_set_auto_refresh(window, true);
1296 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001297 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1298 return VK_ERROR_SURFACE_LOST_KHR;
1299 }
1300 }
1301
Ian Elliott16c443c2021-11-30 17:10:32 -07001302 int query_value;
1303 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1304 &query_value);
1305 if (err != android::OK || query_value < 0) {
1306 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1307 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001308 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001309 }
Ian Elliott16c443c2021-11-30 17:10:32 -07001310 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1311 const auto mailbox_num_images = std::max(3u, create_info->minImageCount);
1312 const auto requested_images =
1313 swap_interval ? create_info->minImageCount : mailbox_num_images;
1314 uint32_t num_images = requested_images - 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001315
Ian Elliott5396b702021-12-13 19:32:34 -07001316 // Lower layer insists that we have at least min_undequeued_buffers + 1
1317 // buffers. This is wasteful and we'd like to relax it in the shared case,
1318 // but not all the pieces are in place for that to work yet. Note we only
1319 // lie to the lower layer--we don't want to give the app back a swapchain
1320 // with extra images (which they can't actually use!).
1321 uint32_t min_buffer_count = min_undequeued_buffers + 1;
1322 err = native_window_set_buffer_count(
1323 window, std::max(min_buffer_count, num_images));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001324 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001325 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1326 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001327 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001328 }
1329
Ian Elliott12b7e2f2021-12-21 23:24:20 -07001330 // In shared mode the num_images must be one regardless of how many
1331 // buffers were allocated for the buffer queue.
1332 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1333 num_images = 1;
1334 }
1335
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001336 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001337 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001338 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001339 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001340 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1341 device, create_info->imageFormat, create_info->imageUsage,
1342 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001343 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001344 if (result != VK_SUCCESS) {
1345 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001346 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001347 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001348 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001349 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001350 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001351 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001352 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001353 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001354 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001355 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001356 if (result != VK_SUCCESS) {
1357 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001358 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001359 }
Jesse Hall70f93352015-11-04 09:41:31 -08001360 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001361 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1362
1363 bool createProtectedSwapchain = false;
1364 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1365 createProtectedSwapchain = true;
1366 native_usage |= BufferUsage::PROTECTED;
1367 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001368 err = native_window_set_usage(window, native_usage);
1369 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001370 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001371 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001372 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001373
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001374 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001375 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1376 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001377 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1378 strerror(-err), err);
1379 return VK_ERROR_SURFACE_LOST_KHR;
1380 }
1381
Jesse Halld7b994a2015-09-07 14:17:37 -07001382 // -- Allocate our Swapchain object --
1383 // After this point, we must deallocate the swapchain on error.
1384
Jesse Hall1f91d392015-12-11 16:28:44 -08001385 void* mem = allocator->pfnAllocation(allocator->pUserData,
1386 sizeof(Swapchain), alignof(Swapchain),
1387 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001388 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001389 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001390 Swapchain* swapchain = new (mem)
1391 Swapchain(surface, num_images, create_info->presentMode,
1392 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001393 // -- Dequeue all buffers and create a VkImage for each --
1394 // Any failures during or after this must cancel the dequeued buffers.
1395
Chris Forbesb56287a2017-01-12 14:28:58 +13001396 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1397#pragma clang diagnostic push
1398#pragma clang diagnostic ignored "-Wold-style-cast"
1399 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1400#pragma clang diagnostic pop
1401 .pNext = nullptr,
1402 .usage = swapchain_image_usage,
1403 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001404 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001405#pragma clang diagnostic push
1406#pragma clang diagnostic ignored "-Wold-style-cast"
1407 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1408#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001409 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001410 };
1411 VkImageCreateInfo image_create = {
1412 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1413 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001414 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001415 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001416 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001417 .extent = {0, 0, 1},
1418 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001419 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001420 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001421 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001422 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001423 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001424 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001425 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1426 };
1427
Jesse Halld7b994a2015-09-07 14:17:37 -07001428 for (uint32_t i = 0; i < num_images; i++) {
1429 Swapchain::Image& img = swapchain->images[i];
1430
1431 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001432 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1433 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001434 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001435 switch (-err) {
1436 case ENOMEM:
1437 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1438 break;
1439 default:
1440 result = VK_ERROR_SURFACE_LOST_KHR;
1441 break;
1442 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001443 break;
1444 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001445 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001446 img.dequeued = true;
1447
1448 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001449 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1450 static_cast<uint32_t>(img.buffer->height),
1451 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001452 image_native_buffer.handle = img.buffer->handle;
1453 image_native_buffer.stride = img.buffer->stride;
1454 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001455 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001456 android_convertGralloc0To1Usage(int(img.buffer->usage),
1457 &image_native_buffer.usage2.producer,
1458 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001459
Yiwei Zhang533cea92019-06-03 18:43:24 -07001460 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001461 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001462 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001463 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001464 if (result != VK_SUCCESS) {
1465 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1466 break;
1467 }
1468 }
1469
1470 // -- Cancel all buffers, returning them to the queue --
1471 // If an error occurred before, also destroy the VkImage and release the
1472 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001473 for (uint32_t i = 0; i < num_images; i++) {
1474 Swapchain::Image& img = swapchain->images[i];
1475 if (img.dequeued) {
1476 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001477 window->cancelBuffer(window, img.buffer.get(),
1478 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001479 img.dequeue_fence = -1;
1480 img.dequeued = false;
1481 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001482 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001483 }
1484
1485 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001486 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1487 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001488 return result;
1489 }
1490
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001491 if (transform_hint != swapchain->pre_transform) {
1492 // Log that the app is not doing pre-rotation.
1493 android::GraphicsEnv::getInstance().setTargetStats(
1494 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1495 }
1496
Jesse Halldc225072016-05-30 22:40:14 -07001497 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1498 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001499 return VK_SUCCESS;
1500}
1501
Jesse Halle1b12782015-11-30 11:27:32 -08001502VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001503void DestroySwapchainKHR(VkDevice device,
1504 VkSwapchainKHR swapchain_handle,
1505 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001506 ATRACE_CALL();
1507
Yiwei Zhang533cea92019-06-03 18:43:24 -07001508 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001509}
1510
Jesse Halle1b12782015-11-30 11:27:32 -08001511VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001512VkResult GetSwapchainImagesKHR(VkDevice,
1513 VkSwapchainKHR swapchain_handle,
1514 uint32_t* count,
1515 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001516 ATRACE_CALL();
1517
Jesse Halld7b994a2015-09-07 14:17:37 -07001518 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001519 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1520 "getting images for non-active swapchain 0x%" PRIx64
1521 "; only dequeued image handles are valid",
1522 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001523 VkResult result = VK_SUCCESS;
1524 if (images) {
1525 uint32_t n = swapchain.num_images;
1526 if (*count < swapchain.num_images) {
1527 n = *count;
1528 result = VK_INCOMPLETE;
1529 }
1530 for (uint32_t i = 0; i < n; i++)
1531 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001532 *count = n;
1533 } else {
1534 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001535 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001536 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001537}
1538
Jesse Halle1b12782015-11-30 11:27:32 -08001539VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001540VkResult AcquireNextImageKHR(VkDevice device,
1541 VkSwapchainKHR swapchain_handle,
1542 uint64_t timeout,
1543 VkSemaphore semaphore,
1544 VkFence vk_fence,
1545 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001546 ATRACE_CALL();
1547
Jesse Halld7b994a2015-09-07 14:17:37 -07001548 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001549 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001550 VkResult result;
1551 int err;
1552
Jesse Halldc225072016-05-30 22:40:14 -07001553 if (swapchain.surface.swapchain_handle != swapchain_handle)
1554 return VK_ERROR_OUT_OF_DATE_KHR;
1555
Chris Forbesc88409c2017-03-30 19:47:37 +13001556 if (swapchain.shared) {
1557 // In shared mode, we keep the buffer dequeued all the time, so we don't
1558 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1559 // the semaphore and fence passed to us will be signalled.
1560 *image_index = 0;
1561 result = GetData(device).driver.AcquireImageANDROID(
1562 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1563 return result;
1564 }
1565
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001566 const nsecs_t acquire_next_image_timeout =
1567 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1568 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1569 // Cache the timeout to avoid the duplicate binder cost.
1570 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1571 acquire_next_image_timeout);
1572 if (err != android::OK) {
1573 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1574 strerror(-err), err);
1575 return VK_ERROR_SURFACE_LOST_KHR;
1576 }
1577 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1578 }
1579
Jesse Halld7b994a2015-09-07 14:17:37 -07001580 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001581 int fence_fd;
1582 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001583 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001584 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1585 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1586 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001587 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001588 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001589 }
1590
1591 uint32_t idx;
1592 for (idx = 0; idx < swapchain.num_images; idx++) {
1593 if (swapchain.images[idx].buffer.get() == buffer) {
1594 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001595 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001596 break;
1597 }
1598 }
1599 if (idx == swapchain.num_images) {
1600 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001601 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001602 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001603 }
1604
1605 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001606 if (fence_fd != -1) {
1607 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001608 if (fence_clone == -1) {
1609 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1610 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001611 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001612 }
1613 }
1614
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001615 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001616 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001617 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001618 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1619 // even if the call fails. We could close it ourselves on failure, but
1620 // that would create a race condition if the driver closes it on a
1621 // failure path: some other thread might create an fd with the same
1622 // number between the time the driver closes it and the time we close
1623 // it. We must assume one of: the driver *always* closes it even on
1624 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001625 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001626 swapchain.images[idx].dequeued = false;
1627 swapchain.images[idx].dequeue_fence = -1;
1628 return result;
1629 }
1630
1631 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001632 return VK_SUCCESS;
1633}
1634
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001635VKAPI_ATTR
1636VkResult AcquireNextImage2KHR(VkDevice device,
1637 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1638 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001639 ATRACE_CALL();
1640
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001641 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1642 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1643 pAcquireInfo->fence, pImageIndex);
1644}
1645
Jesse Halldc225072016-05-30 22:40:14 -07001646static VkResult WorstPresentResult(VkResult a, VkResult b) {
1647 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1648 // (in spec version 1.0.14).
1649 static const VkResult kWorstToBest[] = {
1650 VK_ERROR_DEVICE_LOST,
1651 VK_ERROR_SURFACE_LOST_KHR,
1652 VK_ERROR_OUT_OF_DATE_KHR,
1653 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1654 VK_ERROR_OUT_OF_HOST_MEMORY,
1655 VK_SUBOPTIMAL_KHR,
1656 };
1657 for (auto result : kWorstToBest) {
1658 if (a == result || b == result)
1659 return result;
1660 }
1661 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1662 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1663 return a != VK_SUCCESS ? a : b;
1664}
1665
Jesse Halle1b12782015-11-30 11:27:32 -08001666VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001667VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001668 ATRACE_CALL();
1669
Jesse Halld7b994a2015-09-07 14:17:37 -07001670 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1671 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1672 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001673
Jesse Halldc225072016-05-30 22:40:14 -07001674 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001675 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001676 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001677
Ian Elliottcb351132016-12-13 10:30:40 -07001678 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001679 const VkPresentRegionsKHR* present_regions = nullptr;
1680 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001681 const VkPresentRegionsKHR* next =
1682 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1683 while (next) {
1684 switch (next->sType) {
1685 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1686 present_regions = next;
1687 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001688 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001689 present_times =
1690 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1691 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001692 default:
1693 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1694 next->sType);
1695 break;
1696 }
1697 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1698 }
1699 ALOGV_IF(
1700 present_regions &&
1701 present_regions->swapchainCount != present_info->swapchainCount,
1702 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001703 ALOGV_IF(present_times &&
1704 present_times->swapchainCount != present_info->swapchainCount,
1705 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1706 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001707 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001708 (present_regions) ? present_regions->pRegions : nullptr;
1709 const VkPresentTimeGOOGLE* times =
1710 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001711 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001712 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001713 uint32_t nrects = 0;
1714
Jesse Halld7b994a2015-09-07 14:17:37 -07001715 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1716 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001717 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001718 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001719 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001720 const VkPresentRegionKHR* region =
1721 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001722 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001723 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001724 VkResult result;
1725 int err;
1726
Jesse Halld7b994a2015-09-07 14:17:37 -07001727 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001728 result = dispatch.QueueSignalReleaseImageANDROID(
1729 queue, present_info->waitSemaphoreCount,
1730 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001731 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001732 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001733 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001734 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001735 if (img.release_fence >= 0)
1736 close(img.release_fence);
1737 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001738
Jesse Halldc225072016-05-30 22:40:14 -07001739 if (swapchain.surface.swapchain_handle ==
1740 present_info->pSwapchains[sc]) {
1741 ANativeWindow* window = swapchain.surface.window.get();
1742 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001743 if (region) {
1744 // Process the incremental-present hint for this swapchain:
1745 uint32_t rcount = region->rectangleCount;
1746 if (rcount > nrects) {
1747 android_native_rect_t* new_rects =
1748 static_cast<android_native_rect_t*>(
1749 allocator->pfnReallocation(
1750 allocator->pUserData, rects,
1751 sizeof(android_native_rect_t) * rcount,
1752 alignof(android_native_rect_t),
1753 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1754 if (new_rects) {
1755 rects = new_rects;
1756 nrects = rcount;
1757 } else {
1758 rcount = 0; // Ignore the hint for this swapchain
1759 }
1760 }
1761 for (uint32_t r = 0; r < rcount; ++r) {
1762 if (region->pRectangles[r].layer > 0) {
1763 ALOGV(
1764 "vkQueuePresentKHR ignoring invalid layer "
1765 "(%u); using layer 0 instead",
1766 region->pRectangles[r].layer);
1767 }
1768 int x = region->pRectangles[r].offset.x;
1769 int y = region->pRectangles[r].offset.y;
1770 int width = static_cast<int>(
1771 region->pRectangles[r].extent.width);
1772 int height = static_cast<int>(
1773 region->pRectangles[r].extent.height);
1774 android_native_rect_t* cur_rect = &rects[r];
1775 cur_rect->left = x;
1776 cur_rect->top = y + height;
1777 cur_rect->right = x + width;
1778 cur_rect->bottom = y;
1779 }
1780 native_window_set_surface_damage(window, rects, rcount);
1781 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001782 if (time) {
1783 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001784 ALOGV(
1785 "Calling "
1786 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001787 native_window_enable_frame_timestamps(window, true);
1788 swapchain.frame_timestamps_enabled = true;
1789 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001790
1791 // Record the nativeFrameId so it can be later correlated to
1792 // this present.
1793 uint64_t nativeFrameId = 0;
1794 err = native_window_get_next_frame_id(
1795 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001796 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001797 ALOGE("Failed to get next native frame ID.");
1798 }
1799
1800 // Add a new timing record with the user's presentID and
1801 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001802 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001803 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001804 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001805 }
1806 if (time->desiredPresentTime) {
1807 // Set the desiredPresentTime:
1808 ALOGV(
1809 "Calling "
1810 "native_window_set_buffers_timestamp(%" PRId64 ")",
1811 time->desiredPresentTime);
1812 native_window_set_buffers_timestamp(
1813 window,
1814 static_cast<int64_t>(time->desiredPresentTime));
1815 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001816 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001817
Jesse Halldc225072016-05-30 22:40:14 -07001818 err = window->queueBuffer(window, img.buffer.get(), fence);
1819 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001820 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001821 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1822 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001823 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001824 } else {
1825 if (img.dequeue_fence >= 0) {
1826 close(img.dequeue_fence);
1827 img.dequeue_fence = -1;
1828 }
1829 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001830 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001831
1832 // If the swapchain is in shared mode, immediately dequeue the
1833 // buffer so it can be presented again without an intervening
1834 // call to AcquireNextImageKHR. We expect to get the same buffer
1835 // back from every call to dequeueBuffer in this mode.
1836 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1837 ANativeWindowBuffer* buffer;
1838 int fence_fd;
1839 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001840 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001841 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1842 swapchain_result = WorstPresentResult(swapchain_result,
1843 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001844 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001845 ALOGE("got wrong image back for shared swapchain");
1846 swapchain_result = WorstPresentResult(swapchain_result,
1847 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001848 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13001849 img.dequeue_fence = fence_fd;
1850 img.dequeued = true;
1851 }
1852 }
Jesse Halldc225072016-05-30 22:40:14 -07001853 }
1854 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001855 OrphanSwapchain(device, &swapchain);
1856 }
Ian Elliott1290ea22022-06-14 19:09:15 -06001857 // Android will only return VK_SUBOPTIMAL_KHR for vkQueuePresentKHR,
1858 // and only when the window's transform/rotation changes. Extent
1859 // changes will not cause VK_SUBOPTIMAL_KHR because of the
1860 // application issues that were caused when the following transform
1861 // change was added.
silence_dogood73597592019-05-23 16:57:37 -07001862 int window_transform_hint;
1863 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1864 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001865 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07001866 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1867 strerror(-err), err);
1868 swapchain_result = WorstPresentResult(
1869 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1870 }
1871 if (swapchain.pre_transform != window_transform_hint) {
1872 swapchain_result =
1873 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1874 }
Jesse Halldc225072016-05-30 22:40:14 -07001875 } else {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001876 ReleaseSwapchainImage(device, swapchain.shared, nullptr, fence,
1877 img, true);
Jesse Halldc225072016-05-30 22:40:14 -07001878 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001879 }
1880
Jesse Halla9e57032015-11-30 01:03:10 -08001881 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001882 present_info->pResults[sc] = swapchain_result;
1883
1884 if (swapchain_result != final_result)
1885 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001886 }
Ian Elliottcb351132016-12-13 10:30:40 -07001887 if (rects) {
1888 allocator->pfnFree(allocator->pUserData, rects);
1889 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001890
1891 return final_result;
1892}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001893
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001894VKAPI_ATTR
1895VkResult GetRefreshCycleDurationGOOGLE(
1896 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001897 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001898 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001899 ATRACE_CALL();
1900
Ian Elliott62c48c92017-01-20 13:13:20 -07001901 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001902 VkResult result = VK_SUCCESS;
1903
Ian Elliott3568bfe2019-05-03 15:54:46 -06001904 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001905
1906 return result;
1907}
1908
1909VKAPI_ATTR
1910VkResult GetPastPresentationTimingGOOGLE(
1911 VkDevice,
1912 VkSwapchainKHR swapchain_handle,
1913 uint32_t* count,
1914 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001915 ATRACE_CALL();
1916
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001917 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001918 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1919 return VK_ERROR_OUT_OF_DATE_KHR;
1920 }
1921
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001922 ANativeWindow* window = swapchain.surface.window.get();
1923 VkResult result = VK_SUCCESS;
1924
1925 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001926 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001927 native_window_enable_frame_timestamps(window, true);
1928 swapchain.frame_timestamps_enabled = true;
1929 }
1930
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001931 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07001932 // Get the latest ready timing count before copying, since the copied
1933 // timing info will be erased in copy_ready_timings function.
1934 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07001935 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001936 // Check the *count here against the recorded ready timing count, since
1937 // *count can be overwritten per spec describes.
1938 if (*count < n) {
1939 result = VK_INCOMPLETE;
1940 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001941 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001942 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001943 }
1944
1945 return result;
1946}
1947
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001948VKAPI_ATTR
1949VkResult GetSwapchainStatusKHR(
1950 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001951 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001952 ATRACE_CALL();
1953
Chris Forbes4e18ba82017-01-20 12:50:17 +13001954 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001955 VkResult result = VK_SUCCESS;
1956
Chris Forbes4e18ba82017-01-20 12:50:17 +13001957 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1958 return VK_ERROR_OUT_OF_DATE_KHR;
1959 }
1960
Yiwei Zhanga885c062019-10-24 12:07:57 -07001961 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001962
1963 return result;
1964}
1965
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001966VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001967 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001968 uint32_t swapchainCount,
1969 const VkSwapchainKHR* pSwapchains,
1970 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001971 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001972
1973 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1974 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1975 if (!swapchain)
1976 continue;
1977
1978 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1979
1980 ANativeWindow* window = swapchain->surface.window.get();
1981
1982 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1983 const android_smpte2086_metadata smpteMetdata = {
1984 {vulkanMetadata.displayPrimaryRed.x,
1985 vulkanMetadata.displayPrimaryRed.y},
1986 {vulkanMetadata.displayPrimaryGreen.x,
1987 vulkanMetadata.displayPrimaryGreen.y},
1988 {vulkanMetadata.displayPrimaryBlue.x,
1989 vulkanMetadata.displayPrimaryBlue.y},
1990 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1991 vulkanMetadata.maxLuminance,
1992 vulkanMetadata.minLuminance};
1993 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1994
1995 const android_cta861_3_metadata cta8613Metadata = {
1996 vulkanMetadata.maxContentLightLevel,
1997 vulkanMetadata.maxFrameAverageLightLevel};
1998 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1999 }
2000
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002001 return;
2002}
2003
Yiwei Zhang0f475222019-04-11 19:38:00 -07002004static void InterceptBindImageMemory2(
2005 uint32_t bind_info_count,
2006 const VkBindImageMemoryInfo* bind_infos,
2007 std::vector<VkNativeBufferANDROID>* out_native_buffers,
2008 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
2009 out_native_buffers->clear();
2010 out_bind_infos->clear();
2011
2012 if (!bind_info_count)
2013 return;
2014
2015 std::unordered_set<uint32_t> intercepted_indexes;
2016
2017 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2018 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2019 bind_infos[idx].pNext);
2020 while (info &&
2021 info->sType !=
2022 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
2023 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2024 info->pNext);
2025 }
2026
2027 if (!info)
2028 continue;
2029
2030 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
2031 "swapchain handle must not be NULL");
2032 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
2033 ALOG_ASSERT(
2034 info->imageIndex < swapchain->num_images,
2035 "imageIndex must be less than the number of images in swapchain");
2036
2037 ANativeWindowBuffer* buffer =
2038 swapchain->images[info->imageIndex].buffer.get();
2039 VkNativeBufferANDROID native_buffer = {
2040#pragma clang diagnostic push
2041#pragma clang diagnostic ignored "-Wold-style-cast"
2042 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2043#pragma clang diagnostic pop
2044 .pNext = bind_infos[idx].pNext,
2045 .handle = buffer->handle,
2046 .stride = buffer->stride,
2047 .format = buffer->format,
2048 .usage = int(buffer->usage),
2049 };
2050 // Reserve enough space to avoid letting re-allocation invalidate the
2051 // addresses of the elements inside.
2052 out_native_buffers->reserve(bind_info_count);
2053 out_native_buffers->emplace_back(native_buffer);
2054
2055 // Reserve the space now since we know how much is needed now.
2056 out_bind_infos->reserve(bind_info_count);
2057 out_bind_infos->emplace_back(bind_infos[idx]);
2058 out_bind_infos->back().pNext = &out_native_buffers->back();
2059
2060 intercepted_indexes.insert(idx);
2061 }
2062
2063 if (intercepted_indexes.empty())
2064 return;
2065
2066 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2067 if (intercepted_indexes.count(idx))
2068 continue;
2069 out_bind_infos->emplace_back(bind_infos[idx]);
2070 }
2071}
2072
Yiwei Zhang23143102019-04-10 18:24:05 -07002073VKAPI_ATTR
2074VkResult BindImageMemory2(VkDevice device,
2075 uint32_t bindInfoCount,
2076 const VkBindImageMemoryInfo* pBindInfos) {
2077 ATRACE_CALL();
2078
Yiwei Zhang0f475222019-04-11 19:38:00 -07002079 // out_native_buffers is for maintaining the lifecycle of the constructed
2080 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2081 std::vector<VkNativeBufferANDROID> out_native_buffers;
2082 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2083 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2084 &out_bind_infos);
2085 return GetData(device).driver.BindImageMemory2(
2086 device, bindInfoCount,
2087 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002088}
2089
2090VKAPI_ATTR
2091VkResult BindImageMemory2KHR(VkDevice device,
2092 uint32_t bindInfoCount,
2093 const VkBindImageMemoryInfo* pBindInfos) {
2094 ATRACE_CALL();
2095
Yiwei Zhang0f475222019-04-11 19:38:00 -07002096 std::vector<VkNativeBufferANDROID> out_native_buffers;
2097 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2098 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2099 &out_bind_infos);
2100 return GetData(device).driver.BindImageMemory2KHR(
2101 device, bindInfoCount,
2102 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002103}
2104
Chia-I Wu62262232016-03-26 07:06:44 +08002105} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002106} // namespace vulkan