blob: 96da1b0657370b9ad966499652c60469a29f267d [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,
302 ANativeWindow* window,
303 int release_fence,
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700304 Swapchain::Image& image,
305 bool defer_if_pending) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700306 ATRACE_CALL();
307
Jesse Halldc225072016-05-30 22:40:14 -0700308 ALOG_ASSERT(release_fence == -1 || image.dequeued,
309 "ReleaseSwapchainImage: can't provide a release fence for "
310 "non-dequeued images");
311
312 if (image.dequeued) {
313 if (release_fence >= 0) {
314 // We get here from vkQueuePresentKHR. The application is
315 // responsible for creating an execution dependency chain from
316 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
317 // (release_fence), so we can drop the dequeue_fence here.
318 if (image.dequeue_fence >= 0)
319 close(image.dequeue_fence);
320 } else {
321 // We get here during swapchain destruction, or various serious
322 // error cases e.g. when we can't create the release_fence during
323 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
324 // have already signalled, since the swapchain images are supposed
325 // to be idle before the swapchain is destroyed. In error cases,
326 // there may be rendering in flight to the image, but since we
327 // weren't able to create a release_fence, waiting for the
328 // dequeue_fence is about the best we can do.
329 release_fence = image.dequeue_fence;
330 }
331 image.dequeue_fence = -1;
332
333 if (window) {
334 window->cancelBuffer(window, image.buffer.get(), release_fence);
335 } else {
336 if (release_fence >= 0) {
337 sync_wait(release_fence, -1 /* forever */);
338 close(release_fence);
339 }
340 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700341 release_fence = -1;
Jesse Halldc225072016-05-30 22:40:14 -0700342 image.dequeued = false;
343 }
344
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700345 if (defer_if_pending && IsFencePending(image.release_fence))
346 return;
347
348 if (image.release_fence >= 0) {
349 close(image.release_fence);
350 image.release_fence = -1;
351 }
352
Jesse Halldc225072016-05-30 22:40:14 -0700353 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700354 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700355 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700356 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700357 image.image = VK_NULL_HANDLE;
358 }
359
360 image.buffer.clear();
361}
362
363void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
364 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
365 return;
Jesse Halldc225072016-05-30 22:40:14 -0700366 for (uint32_t i = 0; i < swapchain->num_images; i++) {
367 if (!swapchain->images[i].dequeued)
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700368 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i],
369 true);
Jesse Halldc225072016-05-30 22:40:14 -0700370 }
371 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700372 swapchain->timing.clear();
373}
374
375uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800376 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
377 return 0;
378 }
Ian Elliott8a977262017-01-19 09:05:58 -0700379
Brian Anderson1049d1d2016-12-16 17:25:57 -0800380 uint32_t num_ready = 0;
381 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
382 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700383 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800384 if (ti.ready()) {
385 // This TimingInfo is ready to be reported to the user. Add it
386 // to the num_ready.
387 num_ready++;
388 continue;
389 }
390 // This TimingInfo is not yet ready to be reported to the user,
391 // and so we should look for any available timestamps that
392 // might make it ready.
393 int64_t desired_present_time = 0;
394 int64_t render_complete_time = 0;
395 int64_t composition_latch_time = 0;
396 int64_t actual_present_time = 0;
397 // Obtain timestamps:
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800398 int err = native_window_get_frame_timestamps(
Brian Anderson1049d1d2016-12-16 17:25:57 -0800399 swapchain.surface.window.get(), ti.native_frame_id_,
400 &desired_present_time, &render_complete_time,
401 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700402 nullptr, //&first_composition_start_time,
403 nullptr, //&last_composition_start_time,
404 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800405 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700406 nullptr, //&dequeue_ready_time,
407 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800408
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800409 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800410 continue;
411 }
412
413 // Record the timestamp(s) we received, and then see if this TimingInfo
414 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700415 ti.timestamp_desired_present_time_ = desired_present_time;
416 ti.timestamp_actual_present_time_ = actual_present_time;
417 ti.timestamp_render_complete_time_ = render_complete_time;
418 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800419
420 if (ti.ready()) {
421 // The TimingInfo has received enough timestamps, and should now
422 // use those timestamps to calculate the info that should be
423 // reported to the user:
424 ti.calculate(swapchain.refresh_duration);
425 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700426 }
427 }
428 return num_ready;
429}
430
Ian Elliott8a977262017-01-19 09:05:58 -0700431void copy_ready_timings(Swapchain& swapchain,
432 uint32_t* count,
433 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800434 if (swapchain.timing.empty()) {
435 *count = 0;
436 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700437 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800438
439 size_t last_ready = swapchain.timing.size() - 1;
440 while (!swapchain.timing[last_ready].ready()) {
441 if (last_ready == 0) {
442 *count = 0;
443 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700444 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800445 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700446 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800447
448 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700449 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800450 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
451 const TimingInfo& ti = swapchain.timing[i];
452 if (ti.ready()) {
453 ti.get_values(&timings[num_copied]);
454 num_copied++;
455 }
456 num_to_remove++;
457 }
458
459 // Discard old frames that aren't ready if newer frames are ready.
460 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700461 swapchain.timing.erase(swapchain.timing.begin(),
462 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800463
Ian Elliott8a977262017-01-19 09:05:58 -0700464 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700465}
466
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500467android::PixelFormat GetNativePixelFormat(VkFormat format) {
468 android::PixelFormat native_format = android::PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700469 switch (format) {
470 case VK_FORMAT_R8G8B8A8_UNORM:
471 case VK_FORMAT_R8G8B8A8_SRGB:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500472 native_format = android::PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700473 break;
474 case VK_FORMAT_R5G6B5_UNORM_PACK16:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500475 native_format = android::PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700476 break;
477 case VK_FORMAT_R16G16B16A16_SFLOAT:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500478 native_format = android::PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700479 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800480 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500481 native_format = android::PIXEL_FORMAT_RGBA_1010102;
482 break;
483 case VK_FORMAT_R8_UNORM:
484 native_format = android::PIXEL_FORMAT_R_8;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700485 break;
486 default:
487 ALOGV("unsupported swapchain format %d", format);
488 break;
489 }
490 return native_format;
491}
492
493android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
494 switch (colorspace) {
495 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
496 return HAL_DATASPACE_V0_SRGB;
497 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
498 return HAL_DATASPACE_DISPLAY_P3;
499 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
500 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600501 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
502 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700503 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
504 return HAL_DATASPACE_DCI_P3_LINEAR;
505 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
506 return HAL_DATASPACE_DCI_P3;
507 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
508 return HAL_DATASPACE_V0_SRGB_LINEAR;
509 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
510 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600511 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
512 return HAL_DATASPACE_BT2020_LINEAR;
513 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700514 return static_cast<android_dataspace>(
515 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
516 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600517 case VK_COLOR_SPACE_DOLBYVISION_EXT:
518 return static_cast<android_dataspace>(
519 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
520 HAL_DATASPACE_RANGE_FULL);
521 case VK_COLOR_SPACE_HDR10_HLG_EXT:
522 return static_cast<android_dataspace>(
523 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
524 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700525 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
526 return static_cast<android_dataspace>(
527 HAL_DATASPACE_STANDARD_ADOBE_RGB |
528 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
529 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
530 return HAL_DATASPACE_ADOBE_RGB;
531
532 // Pass through is intended to allow app to provide data that is passed
533 // to the display system without modification.
534 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
535 return HAL_DATASPACE_ARBITRARY;
536
537 default:
538 // This indicates that we don't know about the
539 // dataspace specified and we should indicate that
540 // it's unsupported
541 return HAL_DATASPACE_UNKNOWN;
542 }
543}
544
Jesse Halld7b994a2015-09-07 14:17:37 -0700545} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700546
Jesse Halle1b12782015-11-30 11:27:32 -0800547VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800548VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800549 VkInstance instance,
550 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
551 const VkAllocationCallbacks* allocator,
552 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800553 ATRACE_CALL();
554
Jesse Hall1f91d392015-12-11 16:28:44 -0800555 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800556 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800557 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
558 alignof(Surface),
559 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800560 if (!mem)
561 return VK_ERROR_OUT_OF_HOST_MEMORY;
562 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700563
Chia-I Wue8e689f2016-04-18 08:21:31 +0800564 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700565 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700566 int err = native_window_get_consumer_usage(surface->window.get(),
567 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800568 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700569 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
570 strerror(-err), err);
571 surface->~Surface();
572 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700573 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700574 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700575
Yiwei Zhang6435b322018-05-08 11:12:17 -0700576 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800577 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800578 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800579 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
580 err);
581 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800582 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700583 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800584 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700585
Jesse Hall1356b0d2015-11-23 17:24:58 -0800586 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700587 return VK_SUCCESS;
588}
589
Jesse Halle1b12782015-11-30 11:27:32 -0800590VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800591void DestroySurfaceKHR(VkInstance instance,
592 VkSurfaceKHR surface_handle,
593 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800594 ATRACE_CALL();
595
Jesse Hall1356b0d2015-11-23 17:24:58 -0800596 Surface* surface = SurfaceFromHandle(surface_handle);
597 if (!surface)
598 return;
599 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700600 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700601 "destroyed VkSurfaceKHR 0x%" PRIx64
602 " has active VkSwapchainKHR 0x%" PRIx64,
603 reinterpret_cast<uint64_t>(surface_handle),
604 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800605 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800606 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800607 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800608 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800609}
610
Jesse Halle1b12782015-11-30 11:27:32 -0800611VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800612VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
613 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000614 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800615 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000616 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800617 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800618}
619
Jesse Halle1b12782015-11-30 11:27:32 -0800620VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800621VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Ian Elliott1ce053f2022-03-16 09:49:53 -0600622 VkPhysicalDevice pdev,
Jesse Hallb00daad2015-11-29 19:46:20 -0800623 VkSurfaceKHR surface,
624 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800625 ATRACE_CALL();
626
Jesse Halld7b994a2015-09-07 14:17:37 -0700627 int err;
Jesse Halld7b994a2015-09-07 14:17:37 -0700628 int width, height;
Jesse Hall55bc0972016-02-23 16:43:29 -0800629 int transform_hint;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800630 int max_buffer_count;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600631 if (surface == VK_NULL_HANDLE) {
632 const InstanceData& instance_data = GetData(pdev);
633 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
634 bool surfaceless_enabled =
635 instance_data.hook_extensions.test(surfaceless);
636 if (!surfaceless_enabled) {
637 // It is an error to pass a surface==VK_NULL_HANDLE unless the
638 // VK_GOOGLE_surfaceless_query extension is enabled
639 return VK_ERROR_SURFACE_LOST_KHR;
640 }
641 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
642 // extension for this function is for
643 // VkSurfaceProtectedCapabilitiesKHR::supportsProtected. The following
644 // four values cannot be known without a surface. Default values will
645 // be supplied anyway, but cannot be relied upon.
646 width = 1000;
647 height = 1000;
648 transform_hint = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
649 max_buffer_count = 10;
650 } else {
651 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
652
653 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
654 if (err != android::OK) {
655 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
656 strerror(-err), err);
657 return VK_ERROR_SURFACE_LOST_KHR;
658 }
659 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
660 if (err != android::OK) {
661 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
662 strerror(-err), err);
663 return VK_ERROR_SURFACE_LOST_KHR;
664 }
665
666 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
667 &transform_hint);
668 if (err != android::OK) {
669 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
670 strerror(-err), err);
671 return VK_ERROR_SURFACE_LOST_KHR;
672 }
673
674 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT,
675 &max_buffer_count);
676 if (err != android::OK) {
677 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
678 strerror(-err), err);
679 return VK_ERROR_SURFACE_LOST_KHR;
680 }
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800681 }
Ian Elliott16c443c2021-11-30 17:10:32 -0700682 capabilities->minImageCount = std::min(max_buffer_count, 3);
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800683 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700684
Jesse Hallfe2662d2016-02-09 13:26:59 -0800685 capabilities->currentExtent =
686 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
687
Yiwei Zhang70a21962019-05-31 17:26:52 -0700688 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800689 capabilities->minImageExtent = VkExtent2D{1, 1};
690 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700691
Jesse Hallfe2662d2016-02-09 13:26:59 -0800692 capabilities->maxImageArrayLayers = 1;
693
Jesse Hall55bc0972016-02-23 16:43:29 -0800694 capabilities->supportedTransforms = kSupportedTransforms;
695 capabilities->currentTransform =
696 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700697
Jesse Hallfe2662d2016-02-09 13:26:59 -0800698 // On Android, window composition is a WindowManager property, not something
699 // associated with the bufferqueue. It can't be changed from here.
700 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700701
Jesse Hallb00daad2015-11-29 19:46:20 -0800702 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800703 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
704 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
705 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700706 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
707
Jesse Hallb1352bc2015-09-04 16:12:33 -0700708 return VK_SUCCESS;
709}
710
Jesse Halle1b12782015-11-30 11:27:32 -0800711VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700712VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
713 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800714 uint32_t* count,
715 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800716 ATRACE_CALL();
717
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700718 const InstanceData& instance_data = GetData(pdev);
719
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700720 bool wide_color_support = false;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600721 uint64_t consumer_usage = 0;
Ian Elliottf6df08e2022-03-16 21:27:49 -0600722 bool swapchain_ext =
723 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600724 if (surface_handle == VK_NULL_HANDLE) {
725 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
726 bool surfaceless_enabled =
727 instance_data.hook_extensions.test(surfaceless);
728 if (!surfaceless_enabled) {
729 return VK_ERROR_SURFACE_LOST_KHR;
730 }
731 // Support for VK_GOOGLE_surfaceless_query. The EGL loader
732 // unconditionally supports wide color formats, even if they will cause
733 // a SurfaceFlinger fallback. Based on that, wide_color_support will be
734 // set to true in this case.
735 wide_color_support = true;
736
737 // TODO(b/203826952): research proper value; temporarily use the
738 // values seen on Pixel
739 consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
740 } else {
741 Surface& surface = *SurfaceFromHandle(surface_handle);
742 int err = native_window_get_wide_color_support(surface.window.get(),
743 &wide_color_support);
744 if (err) {
745 return VK_ERROR_SURFACE_LOST_KHR;
746 }
747 ALOGV("wide_color_support is: %d", wide_color_support);
748
749 consumer_usage = surface.consumer_usage;
Ian Elliott6ba85d92022-02-18 16:44:58 -0700750 }
Ian Elliottf6df08e2022-03-16 21:27:49 -0600751 wide_color_support = wide_color_support && swapchain_ext;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700752
Charlie Lao324191f2019-12-13 11:03:16 -0800753 AHardwareBuffer_Desc desc = {};
754 desc.width = 1;
755 desc.height = 1;
756 desc.layers = 1;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600757 desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
Charlie Lao324191f2019-12-13 11:03:16 -0800758 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
759
760 // We must support R8G8B8A8
761 std::vector<VkSurfaceFormatKHR> all_formats = {
762 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Trevor David Black320ef3a2022-03-09 22:50:09 +0000763 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}};
Charlie Lao324191f2019-12-13 11:03:16 -0800764
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700765 if (wide_color_support) {
Charlie Lao324191f2019-12-13 11:03:16 -0800766 all_formats.emplace_back(VkSurfaceFormatKHR{
767 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
768 all_formats.emplace_back(VkSurfaceFormatKHR{
769 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
770 }
771
Ian Elliott1ce053f2022-03-16 09:49:53 -0600772 // NOTE: Any new formats that are added must be coordinated across different
773 // Android users. This includes the ANGLE team (a layered implementation of
774 // OpenGL-ES).
775
Charlie Lao324191f2019-12-13 11:03:16 -0800776 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
777 if (AHardwareBuffer_isSupported(&desc)) {
778 all_formats.emplace_back(VkSurfaceFormatKHR{
779 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
780 }
781
782 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
783 if (AHardwareBuffer_isSupported(&desc)) {
784 all_formats.emplace_back(VkSurfaceFormatKHR{
785 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
786 if (wide_color_support) {
787 all_formats.emplace_back(
788 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
789 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
790 all_formats.emplace_back(
791 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
792 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
793 }
794 }
795
796 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
797 if (AHardwareBuffer_isSupported(&desc)) {
798 all_formats.emplace_back(
799 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
800 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
801 if (wide_color_support) {
802 all_formats.emplace_back(
803 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
804 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
805 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700806 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700807
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500808 desc.format = AHARDWAREBUFFER_FORMAT_R8_UNORM;
809 if (AHardwareBuffer_isSupported(&desc)) {
810 all_formats.emplace_back(
811 VkSurfaceFormatKHR{VK_FORMAT_R8_UNORM,
812 VK_COLOR_SPACE_PASS_THROUGH_EXT});
813 }
814
Ian Elliott6ba85d92022-02-18 16:44:58 -0700815 // NOTE: Any new formats that are added must be coordinated across different
816 // Android users. This includes the ANGLE team (a layered implementation of
817 // OpenGL-ES).
818
Jesse Halld7b994a2015-09-07 14:17:37 -0700819 VkResult result = VK_SUCCESS;
820 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800821 uint32_t transfer_count = all_formats.size();
822 if (transfer_count > *count) {
823 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700824 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700825 }
Charlie Lao324191f2019-12-13 11:03:16 -0800826 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
827 formats);
828 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700829 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800830 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700831 }
Charlie Lao324191f2019-12-13 11:03:16 -0800832
Jesse Halld7b994a2015-09-07 14:17:37 -0700833 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700834}
835
Jesse Halle1b12782015-11-30 11:27:32 -0800836VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300837VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
838 VkPhysicalDevice physicalDevice,
839 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
840 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800841 ATRACE_CALL();
842
Chris Forbes2452cf72017-03-16 16:30:17 +1300843 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
844 physicalDevice, pSurfaceInfo->surface,
845 &pSurfaceCapabilities->surfaceCapabilities);
846
Chris Forbes06bc0092017-03-16 16:46:05 +1300847 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
848 while (caps->pNext) {
849 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
850
851 switch (caps->sType) {
852 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
853 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
854 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
855 caps);
856 // Claim same set of usage flags are supported for
857 // shared present modes as for other modes.
858 shared_caps->sharedPresentSupportedUsageFlags =
859 pSurfaceCapabilities->surfaceCapabilities
860 .supportedUsageFlags;
861 } break;
862
Ian Elliottbb67b242022-03-16 09:52:28 -0600863 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
864 VkSurfaceProtectedCapabilitiesKHR* protected_caps =
865 reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(caps);
866 protected_caps->supportsProtected = VK_TRUE;
867 } break;
868
Chris Forbes06bc0092017-03-16 16:46:05 +1300869 default:
870 // Ignore all other extension structs
871 break;
872 }
873 }
874
Chris Forbes2452cf72017-03-16 16:30:17 +1300875 return result;
876}
877
878VKAPI_ATTR
879VkResult GetPhysicalDeviceSurfaceFormats2KHR(
880 VkPhysicalDevice physicalDevice,
881 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
882 uint32_t* pSurfaceFormatCount,
883 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800884 ATRACE_CALL();
885
Chris Forbes2452cf72017-03-16 16:30:17 +1300886 if (!pSurfaceFormats) {
887 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
888 pSurfaceInfo->surface,
889 pSurfaceFormatCount, nullptr);
890 } else {
891 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
892 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700893 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300894 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
895 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700896 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300897
898 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
899 // marshal results individually due to stride difference.
900 // completely ignore any chained extension structs.
901 uint32_t formats_to_marshal = *pSurfaceFormatCount;
902 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
903 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
904 }
905 }
906
907 return result;
908 }
909}
910
911VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300912VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800913 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800914 uint32_t* count,
915 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800916 ATRACE_CALL();
917
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800918 int err;
919 int query_value;
Ian Elliotte7f036c2022-03-15 16:49:21 -0600920 std::vector<VkPresentModeKHR> present_modes;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600921 if (surface == VK_NULL_HANDLE) {
922 const InstanceData& instance_data = GetData(pdev);
923 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
924 bool surfaceless_enabled =
925 instance_data.hook_extensions.test(surfaceless);
926 if (!surfaceless_enabled) {
927 return VK_ERROR_SURFACE_LOST_KHR;
928 }
929 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
930 // extension for this function is for
931 // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and
932 // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot
933 // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a
934 // surface, and that cannot be relied upon.
Ian Elliotte7f036c2022-03-15 16:49:21 -0600935 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600936 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
937 } else {
938 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
939
940 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
941 &query_value);
942 if (err != android::OK || query_value < 0) {
943 ALOGE(
944 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
945 "value=%d",
946 strerror(-err), err, query_value);
947 return VK_ERROR_SURFACE_LOST_KHR;
948 }
949 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
950
951 err =
952 window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
953 if (err != android::OK || query_value < 0) {
954 ALOGE(
955 "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
956 strerror(-err), err, query_value);
957 return VK_ERROR_SURFACE_LOST_KHR;
958 }
959 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
960
961 if (min_undequeued_buffers + 1 < max_buffer_count)
962 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
963 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
964 }
Chris Forbese8d79a62017-02-22 12:49:18 +1300965
966 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700967 QueryPresentationProperties(pdev, &present_properties);
968 if (present_properties.sharedImage) {
969 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
970 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300971 }
972
973 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700974
975 VkResult result = VK_SUCCESS;
976 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300977 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700978 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300979 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -0700980 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700981 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300982 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700983 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700984 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700985}
986
Jesse Halle1b12782015-11-30 11:27:32 -0800987VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400988VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600989 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400990 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800991 ATRACE_CALL();
992
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400993 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
994 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
995 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
996 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
997 pDeviceGroupPresentCapabilities->sType);
998
999 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
1000 sizeof(pDeviceGroupPresentCapabilities->presentMask));
1001
1002 // assume device group of size 1
1003 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
1004 pDeviceGroupPresentCapabilities->modes =
1005 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1006
1007 return VK_SUCCESS;
1008}
1009
1010VKAPI_ATTR
1011VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001012 VkDevice,
1013 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001014 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001015 ATRACE_CALL();
1016
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001017 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1018 return VK_SUCCESS;
1019}
1020
1021VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -06001022VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001023 VkSurfaceKHR surface,
1024 uint32_t* pRectCount,
1025 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001026 ATRACE_CALL();
1027
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001028 if (!pRects) {
1029 *pRectCount = 1;
1030 } else {
1031 uint32_t count = std::min(*pRectCount, 1u);
1032 bool incomplete = *pRectCount < 1;
1033
1034 *pRectCount = count;
1035
1036 if (incomplete) {
1037 return VK_INCOMPLETE;
1038 }
1039
1040 int err;
1041 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1042
1043 int width = 0, height = 0;
1044 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001045 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001046 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1047 strerror(-err), err);
1048 }
1049 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001050 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001051 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1052 strerror(-err), err);
1053 }
1054
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001055 pRects[0].offset.x = 0;
1056 pRects[0].offset.y = 0;
1057 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1058 static_cast<uint32_t>(height)};
1059 }
1060 return VK_SUCCESS;
1061}
1062
Yiwei Zhang533cea92019-06-03 18:43:24 -07001063static void DestroySwapchainInternal(VkDevice device,
1064 VkSwapchainKHR swapchain_handle,
1065 const VkAllocationCallbacks* allocator) {
1066 ATRACE_CALL();
1067
1068 const auto& dispatch = GetData(device).driver;
1069 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1070 if (!swapchain) {
1071 return;
1072 }
1073
1074 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1075 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1076
1077 if (window && swapchain->frame_timestamps_enabled) {
1078 native_window_enable_frame_timestamps(window, false);
1079 }
1080
1081 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001082 ReleaseSwapchainImage(device, window, -1, swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001083 }
1084
1085 if (active) {
1086 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1087 }
1088
1089 if (!allocator) {
1090 allocator = &GetData(device).allocator;
1091 }
1092
1093 swapchain->~Swapchain();
1094 allocator->pfnFree(allocator->pUserData, swapchain);
1095}
1096
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001097VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001098VkResult CreateSwapchainKHR(VkDevice device,
1099 const VkSwapchainCreateInfoKHR* create_info,
1100 const VkAllocationCallbacks* allocator,
1101 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001102 ATRACE_CALL();
1103
Jesse Halld7b994a2015-09-07 14:17:37 -07001104 int err;
1105 VkResult result = VK_SUCCESS;
1106
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001107 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1108 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1109 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1110 " oldSwapchain=0x%" PRIx64,
1111 reinterpret_cast<uint64_t>(create_info->surface),
1112 create_info->minImageCount, create_info->imageFormat,
1113 create_info->imageColorSpace, create_info->imageExtent.width,
1114 create_info->imageExtent.height, create_info->imageUsage,
1115 create_info->preTransform, create_info->presentMode,
1116 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1117
Jesse Hall1f91d392015-12-11 16:28:44 -08001118 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001119 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001120
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001121 android::PixelFormat native_pixel_format =
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001122 GetNativePixelFormat(create_info->imageFormat);
1123 android_dataspace native_dataspace =
1124 GetNativeDataspace(create_info->imageColorSpace);
1125 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1126 ALOGE(
1127 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1128 "failed: Unsupported color space",
1129 create_info->imageColorSpace);
1130 return VK_ERROR_INITIALIZATION_FAILED;
1131 }
1132
Jesse Hall42a9eec2016-06-03 12:39:49 -07001133 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001134 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001135 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001136 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001137 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001138 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001139 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001140 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001141 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1142 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001143 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001144 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001145
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001146 Surface& surface = *SurfaceFromHandle(create_info->surface);
1147
Jesse Halldc225072016-05-30 22:40:14 -07001148 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001149 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001150 " because it already has active swapchain 0x%" PRIx64
1151 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1152 reinterpret_cast<uint64_t>(create_info->surface),
1153 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1154 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1155 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1156 }
1157 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1158 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1159
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001160 // -- Reset the native window --
1161 // The native window might have been used previously, and had its properties
1162 // changed from defaults. That will affect the answer we get for queries
1163 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1164 // attempt such queries.
1165
Jesse Halldc225072016-05-30 22:40:14 -07001166 // The native window only allows dequeueing all buffers before any have
1167 // been queued, since after that point at least one is assumed to be in
1168 // non-FREE state at any given time. Disconnecting and re-connecting
1169 // orphans the previous buffers, getting us back to the state where we can
1170 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001171 //
1172 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001173 ANativeWindow* window = surface.window.get();
1174 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1175 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001176 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001177 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1178 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001179 strerror(-err), err);
1180
Nicolas Capens147b7da2021-04-09 14:53:06 -04001181 err =
1182 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001183 if (err != android::OK) {
1184 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1185 strerror(-err), err);
1186 return VK_ERROR_SURFACE_LOST_KHR;
1187 }
1188
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301189 int swap_interval =
1190 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001191 err = window->setSwapInterval(window, swap_interval);
1192 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001193 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1194 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001195 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001196 }
1197
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001198 err = native_window_set_shared_buffer_mode(window, false);
1199 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001200 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1201 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001202 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001203 }
1204
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001205 err = native_window_set_auto_refresh(window, false);
1206 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001207 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1208 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001209 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001210 }
1211
Jesse Halld7b994a2015-09-07 14:17:37 -07001212 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001213
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001214 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001215
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001216 err = native_window_set_buffers_format(window, native_pixel_format);
1217 if (err != android::OK) {
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001218 ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)",
1219 decodePixelFormat(native_pixel_format).c_str(), strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001220 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001221 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001222 err = native_window_set_buffers_data_space(window, native_dataspace);
1223 if (err != android::OK) {
Jesse Hall517274a2016-02-10 00:07:18 -08001224 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001225 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001226 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001227 }
1228
Jesse Hall3dd678a2016-01-08 21:52:01 -08001229 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001230 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001231 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001232 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001233 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1234 create_info->imageExtent.width, create_info->imageExtent.height,
1235 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001236 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001237 }
1238
Jesse Hall178b6962016-02-24 15:39:50 -08001239 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1240 // applied during rendering. native_window_set_transform() expects the
1241 // inverse: the transform the app is requesting that the compositor perform
1242 // during composition. With native windows, pre-transform works by rendering
1243 // with the same transform the compositor is applying (as in Vulkan), but
1244 // then requesting the inverse transform, so that when the compositor does
1245 // it's job the two transforms cancel each other out and the compositor ends
1246 // up applying an identity transform to the app's buffer.
1247 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001248 window, InvertTransformToNative(create_info->preTransform));
1249 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001250 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1251 InvertTransformToNative(create_info->preTransform),
1252 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001253 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001254 }
1255
Jesse Hallf64ca122015-11-03 16:11:10 -08001256 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001257 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1258 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001259 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1260 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001261 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001262 }
1263
Chris Forbes97ef4612017-03-30 19:37:50 +13001264 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1265 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1266 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1267 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001268 err = native_window_set_shared_buffer_mode(window, true);
1269 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001270 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1271 return VK_ERROR_SURFACE_LOST_KHR;
1272 }
1273 }
1274
1275 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001276 err = native_window_set_auto_refresh(window, true);
1277 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001278 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1279 return VK_ERROR_SURFACE_LOST_KHR;
1280 }
1281 }
1282
Ian Elliott16c443c2021-11-30 17:10:32 -07001283 int query_value;
1284 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1285 &query_value);
1286 if (err != android::OK || query_value < 0) {
1287 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1288 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001289 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001290 }
Ian Elliott16c443c2021-11-30 17:10:32 -07001291 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1292 const auto mailbox_num_images = std::max(3u, create_info->minImageCount);
1293 const auto requested_images =
1294 swap_interval ? create_info->minImageCount : mailbox_num_images;
1295 uint32_t num_images = requested_images - 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001296
Ian Elliott5396b702021-12-13 19:32:34 -07001297 // Lower layer insists that we have at least min_undequeued_buffers + 1
1298 // buffers. This is wasteful and we'd like to relax it in the shared case,
1299 // but not all the pieces are in place for that to work yet. Note we only
1300 // lie to the lower layer--we don't want to give the app back a swapchain
1301 // with extra images (which they can't actually use!).
1302 uint32_t min_buffer_count = min_undequeued_buffers + 1;
1303 err = native_window_set_buffer_count(
1304 window, std::max(min_buffer_count, num_images));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001305 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001306 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1307 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001308 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001309 }
1310
Ian Elliott12b7e2f2021-12-21 23:24:20 -07001311 // In shared mode the num_images must be one regardless of how many
1312 // buffers were allocated for the buffer queue.
1313 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1314 num_images = 1;
1315 }
1316
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001317 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001318 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001319 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001320 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001321 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1322 device, create_info->imageFormat, create_info->imageUsage,
1323 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001324 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001325 if (result != VK_SUCCESS) {
1326 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001327 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001328 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001329 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001330 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001331 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001332 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001333 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001334 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001335 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001336 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001337 if (result != VK_SUCCESS) {
1338 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001339 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001340 }
Jesse Hall70f93352015-11-04 09:41:31 -08001341 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001342 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1343
1344 bool createProtectedSwapchain = false;
1345 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1346 createProtectedSwapchain = true;
1347 native_usage |= BufferUsage::PROTECTED;
1348 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001349 err = native_window_set_usage(window, native_usage);
1350 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001351 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001352 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001353 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001354
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001355 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001356 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1357 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001358 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1359 strerror(-err), err);
1360 return VK_ERROR_SURFACE_LOST_KHR;
1361 }
1362
Jesse Halld7b994a2015-09-07 14:17:37 -07001363 // -- Allocate our Swapchain object --
1364 // After this point, we must deallocate the swapchain on error.
1365
Jesse Hall1f91d392015-12-11 16:28:44 -08001366 void* mem = allocator->pfnAllocation(allocator->pUserData,
1367 sizeof(Swapchain), alignof(Swapchain),
1368 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001369 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001370 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001371 Swapchain* swapchain = new (mem)
1372 Swapchain(surface, num_images, create_info->presentMode,
1373 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001374 // -- Dequeue all buffers and create a VkImage for each --
1375 // Any failures during or after this must cancel the dequeued buffers.
1376
Chris Forbesb56287a2017-01-12 14:28:58 +13001377 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1378#pragma clang diagnostic push
1379#pragma clang diagnostic ignored "-Wold-style-cast"
1380 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1381#pragma clang diagnostic pop
1382 .pNext = nullptr,
1383 .usage = swapchain_image_usage,
1384 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001385 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001386#pragma clang diagnostic push
1387#pragma clang diagnostic ignored "-Wold-style-cast"
1388 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1389#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001390 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001391 };
1392 VkImageCreateInfo image_create = {
1393 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1394 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001395 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001396 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001397 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001398 .extent = {0, 0, 1},
1399 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001400 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001401 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001402 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001403 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001404 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001405 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001406 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1407 };
1408
Jesse Halld7b994a2015-09-07 14:17:37 -07001409 for (uint32_t i = 0; i < num_images; i++) {
1410 Swapchain::Image& img = swapchain->images[i];
1411
1412 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001413 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1414 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001415 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001416 switch (-err) {
1417 case ENOMEM:
1418 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1419 break;
1420 default:
1421 result = VK_ERROR_SURFACE_LOST_KHR;
1422 break;
1423 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001424 break;
1425 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001426 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001427 img.dequeued = true;
1428
1429 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001430 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1431 static_cast<uint32_t>(img.buffer->height),
1432 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001433 image_native_buffer.handle = img.buffer->handle;
1434 image_native_buffer.stride = img.buffer->stride;
1435 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001436 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001437 android_convertGralloc0To1Usage(int(img.buffer->usage),
1438 &image_native_buffer.usage2.producer,
1439 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001440
Yiwei Zhang533cea92019-06-03 18:43:24 -07001441 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001442 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001443 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001444 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001445 if (result != VK_SUCCESS) {
1446 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1447 break;
1448 }
1449 }
1450
1451 // -- Cancel all buffers, returning them to the queue --
1452 // If an error occurred before, also destroy the VkImage and release the
1453 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001454 for (uint32_t i = 0; i < num_images; i++) {
1455 Swapchain::Image& img = swapchain->images[i];
1456 if (img.dequeued) {
1457 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001458 window->cancelBuffer(window, img.buffer.get(),
1459 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001460 img.dequeue_fence = -1;
1461 img.dequeued = false;
1462 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001463 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001464 }
1465
1466 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001467 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1468 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001469 return result;
1470 }
1471
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001472 if (transform_hint != swapchain->pre_transform) {
1473 // Log that the app is not doing pre-rotation.
1474 android::GraphicsEnv::getInstance().setTargetStats(
1475 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1476 }
1477
Jesse Halldc225072016-05-30 22:40:14 -07001478 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1479 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001480 return VK_SUCCESS;
1481}
1482
Jesse Halle1b12782015-11-30 11:27:32 -08001483VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001484void DestroySwapchainKHR(VkDevice device,
1485 VkSwapchainKHR swapchain_handle,
1486 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001487 ATRACE_CALL();
1488
Yiwei Zhang533cea92019-06-03 18:43:24 -07001489 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001490}
1491
Jesse Halle1b12782015-11-30 11:27:32 -08001492VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001493VkResult GetSwapchainImagesKHR(VkDevice,
1494 VkSwapchainKHR swapchain_handle,
1495 uint32_t* count,
1496 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001497 ATRACE_CALL();
1498
Jesse Halld7b994a2015-09-07 14:17:37 -07001499 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001500 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1501 "getting images for non-active swapchain 0x%" PRIx64
1502 "; only dequeued image handles are valid",
1503 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001504 VkResult result = VK_SUCCESS;
1505 if (images) {
1506 uint32_t n = swapchain.num_images;
1507 if (*count < swapchain.num_images) {
1508 n = *count;
1509 result = VK_INCOMPLETE;
1510 }
1511 for (uint32_t i = 0; i < n; i++)
1512 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001513 *count = n;
1514 } else {
1515 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001516 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001517 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001518}
1519
Jesse Halle1b12782015-11-30 11:27:32 -08001520VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001521VkResult AcquireNextImageKHR(VkDevice device,
1522 VkSwapchainKHR swapchain_handle,
1523 uint64_t timeout,
1524 VkSemaphore semaphore,
1525 VkFence vk_fence,
1526 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001527 ATRACE_CALL();
1528
Jesse Halld7b994a2015-09-07 14:17:37 -07001529 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001530 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001531 VkResult result;
1532 int err;
1533
Jesse Halldc225072016-05-30 22:40:14 -07001534 if (swapchain.surface.swapchain_handle != swapchain_handle)
1535 return VK_ERROR_OUT_OF_DATE_KHR;
1536
Chris Forbesc88409c2017-03-30 19:47:37 +13001537 if (swapchain.shared) {
1538 // In shared mode, we keep the buffer dequeued all the time, so we don't
1539 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1540 // the semaphore and fence passed to us will be signalled.
1541 *image_index = 0;
1542 result = GetData(device).driver.AcquireImageANDROID(
1543 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1544 return result;
1545 }
1546
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001547 const nsecs_t acquire_next_image_timeout =
1548 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1549 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1550 // Cache the timeout to avoid the duplicate binder cost.
1551 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1552 acquire_next_image_timeout);
1553 if (err != android::OK) {
1554 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1555 strerror(-err), err);
1556 return VK_ERROR_SURFACE_LOST_KHR;
1557 }
1558 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1559 }
1560
Jesse Halld7b994a2015-09-07 14:17:37 -07001561 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001562 int fence_fd;
1563 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001564 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001565 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1566 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1567 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001568 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001569 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001570 }
1571
1572 uint32_t idx;
1573 for (idx = 0; idx < swapchain.num_images; idx++) {
1574 if (swapchain.images[idx].buffer.get() == buffer) {
1575 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001576 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001577 break;
1578 }
1579 }
1580 if (idx == swapchain.num_images) {
1581 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001582 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001583 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001584 }
1585
1586 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001587 if (fence_fd != -1) {
1588 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001589 if (fence_clone == -1) {
1590 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1591 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001592 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001593 }
1594 }
1595
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001596 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001597 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001598 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001599 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1600 // even if the call fails. We could close it ourselves on failure, but
1601 // that would create a race condition if the driver closes it on a
1602 // failure path: some other thread might create an fd with the same
1603 // number between the time the driver closes it and the time we close
1604 // it. We must assume one of: the driver *always* closes it even on
1605 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001606 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001607 swapchain.images[idx].dequeued = false;
1608 swapchain.images[idx].dequeue_fence = -1;
1609 return result;
1610 }
1611
1612 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001613 return VK_SUCCESS;
1614}
1615
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001616VKAPI_ATTR
1617VkResult AcquireNextImage2KHR(VkDevice device,
1618 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1619 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001620 ATRACE_CALL();
1621
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001622 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1623 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1624 pAcquireInfo->fence, pImageIndex);
1625}
1626
Jesse Halldc225072016-05-30 22:40:14 -07001627static VkResult WorstPresentResult(VkResult a, VkResult b) {
1628 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1629 // (in spec version 1.0.14).
1630 static const VkResult kWorstToBest[] = {
1631 VK_ERROR_DEVICE_LOST,
1632 VK_ERROR_SURFACE_LOST_KHR,
1633 VK_ERROR_OUT_OF_DATE_KHR,
1634 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1635 VK_ERROR_OUT_OF_HOST_MEMORY,
1636 VK_SUBOPTIMAL_KHR,
1637 };
1638 for (auto result : kWorstToBest) {
1639 if (a == result || b == result)
1640 return result;
1641 }
1642 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1643 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1644 return a != VK_SUCCESS ? a : b;
1645}
1646
Jesse Halle1b12782015-11-30 11:27:32 -08001647VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001648VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001649 ATRACE_CALL();
1650
Jesse Halld7b994a2015-09-07 14:17:37 -07001651 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1652 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1653 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001654
Jesse Halldc225072016-05-30 22:40:14 -07001655 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001656 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001657 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001658
Ian Elliottcb351132016-12-13 10:30:40 -07001659 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001660 const VkPresentRegionsKHR* present_regions = nullptr;
1661 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001662 const VkPresentRegionsKHR* next =
1663 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1664 while (next) {
1665 switch (next->sType) {
1666 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1667 present_regions = next;
1668 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001669 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001670 present_times =
1671 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1672 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001673 default:
1674 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1675 next->sType);
1676 break;
1677 }
1678 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1679 }
1680 ALOGV_IF(
1681 present_regions &&
1682 present_regions->swapchainCount != present_info->swapchainCount,
1683 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001684 ALOGV_IF(present_times &&
1685 present_times->swapchainCount != present_info->swapchainCount,
1686 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1687 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001688 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001689 (present_regions) ? present_regions->pRegions : nullptr;
1690 const VkPresentTimeGOOGLE* times =
1691 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001692 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001693 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001694 uint32_t nrects = 0;
1695
Jesse Halld7b994a2015-09-07 14:17:37 -07001696 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1697 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001698 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001699 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001700 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001701 const VkPresentRegionKHR* region =
1702 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001703 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001704 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001705 VkResult result;
1706 int err;
1707
Jesse Halld7b994a2015-09-07 14:17:37 -07001708 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001709 result = dispatch.QueueSignalReleaseImageANDROID(
1710 queue, present_info->waitSemaphoreCount,
1711 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001712 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001713 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001714 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001715 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001716 if (img.release_fence >= 0)
1717 close(img.release_fence);
1718 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001719
Jesse Halldc225072016-05-30 22:40:14 -07001720 if (swapchain.surface.swapchain_handle ==
1721 present_info->pSwapchains[sc]) {
1722 ANativeWindow* window = swapchain.surface.window.get();
1723 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001724 if (region) {
1725 // Process the incremental-present hint for this swapchain:
1726 uint32_t rcount = region->rectangleCount;
1727 if (rcount > nrects) {
1728 android_native_rect_t* new_rects =
1729 static_cast<android_native_rect_t*>(
1730 allocator->pfnReallocation(
1731 allocator->pUserData, rects,
1732 sizeof(android_native_rect_t) * rcount,
1733 alignof(android_native_rect_t),
1734 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1735 if (new_rects) {
1736 rects = new_rects;
1737 nrects = rcount;
1738 } else {
1739 rcount = 0; // Ignore the hint for this swapchain
1740 }
1741 }
1742 for (uint32_t r = 0; r < rcount; ++r) {
1743 if (region->pRectangles[r].layer > 0) {
1744 ALOGV(
1745 "vkQueuePresentKHR ignoring invalid layer "
1746 "(%u); using layer 0 instead",
1747 region->pRectangles[r].layer);
1748 }
1749 int x = region->pRectangles[r].offset.x;
1750 int y = region->pRectangles[r].offset.y;
1751 int width = static_cast<int>(
1752 region->pRectangles[r].extent.width);
1753 int height = static_cast<int>(
1754 region->pRectangles[r].extent.height);
1755 android_native_rect_t* cur_rect = &rects[r];
1756 cur_rect->left = x;
1757 cur_rect->top = y + height;
1758 cur_rect->right = x + width;
1759 cur_rect->bottom = y;
1760 }
1761 native_window_set_surface_damage(window, rects, rcount);
1762 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001763 if (time) {
1764 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001765 ALOGV(
1766 "Calling "
1767 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001768 native_window_enable_frame_timestamps(window, true);
1769 swapchain.frame_timestamps_enabled = true;
1770 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001771
1772 // Record the nativeFrameId so it can be later correlated to
1773 // this present.
1774 uint64_t nativeFrameId = 0;
1775 err = native_window_get_next_frame_id(
1776 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001777 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001778 ALOGE("Failed to get next native frame ID.");
1779 }
1780
1781 // Add a new timing record with the user's presentID and
1782 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001783 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001784 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001785 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001786 }
1787 if (time->desiredPresentTime) {
1788 // Set the desiredPresentTime:
1789 ALOGV(
1790 "Calling "
1791 "native_window_set_buffers_timestamp(%" PRId64 ")",
1792 time->desiredPresentTime);
1793 native_window_set_buffers_timestamp(
1794 window,
1795 static_cast<int64_t>(time->desiredPresentTime));
1796 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001797 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001798
Jesse Halldc225072016-05-30 22:40:14 -07001799 err = window->queueBuffer(window, img.buffer.get(), fence);
1800 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001801 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001802 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1803 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001804 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001805 } else {
1806 if (img.dequeue_fence >= 0) {
1807 close(img.dequeue_fence);
1808 img.dequeue_fence = -1;
1809 }
1810 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001811 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001812
1813 // If the swapchain is in shared mode, immediately dequeue the
1814 // buffer so it can be presented again without an intervening
1815 // call to AcquireNextImageKHR. We expect to get the same buffer
1816 // back from every call to dequeueBuffer in this mode.
1817 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1818 ANativeWindowBuffer* buffer;
1819 int fence_fd;
1820 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001821 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001822 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1823 swapchain_result = WorstPresentResult(swapchain_result,
1824 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001825 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001826 ALOGE("got wrong image back for shared swapchain");
1827 swapchain_result = WorstPresentResult(swapchain_result,
1828 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001829 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13001830 img.dequeue_fence = fence_fd;
1831 img.dequeued = true;
1832 }
1833 }
Jesse Halldc225072016-05-30 22:40:14 -07001834 }
1835 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001836 OrphanSwapchain(device, &swapchain);
1837 }
silence_dogood73597592019-05-23 16:57:37 -07001838 int window_transform_hint;
1839 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1840 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001841 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07001842 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1843 strerror(-err), err);
1844 swapchain_result = WorstPresentResult(
1845 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1846 }
1847 if (swapchain.pre_transform != window_transform_hint) {
1848 swapchain_result =
1849 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1850 }
Jesse Halldc225072016-05-30 22:40:14 -07001851 } else {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001852 ReleaseSwapchainImage(device, nullptr, fence, img, true);
Jesse Halldc225072016-05-30 22:40:14 -07001853 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001854 }
1855
Jesse Halla9e57032015-11-30 01:03:10 -08001856 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001857 present_info->pResults[sc] = swapchain_result;
1858
1859 if (swapchain_result != final_result)
1860 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001861 }
Ian Elliottcb351132016-12-13 10:30:40 -07001862 if (rects) {
1863 allocator->pfnFree(allocator->pUserData, rects);
1864 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001865
1866 return final_result;
1867}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001868
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001869VKAPI_ATTR
1870VkResult GetRefreshCycleDurationGOOGLE(
1871 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001872 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001873 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001874 ATRACE_CALL();
1875
Ian Elliott62c48c92017-01-20 13:13:20 -07001876 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001877 VkResult result = VK_SUCCESS;
1878
Ian Elliott3568bfe2019-05-03 15:54:46 -06001879 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001880
1881 return result;
1882}
1883
1884VKAPI_ATTR
1885VkResult GetPastPresentationTimingGOOGLE(
1886 VkDevice,
1887 VkSwapchainKHR swapchain_handle,
1888 uint32_t* count,
1889 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001890 ATRACE_CALL();
1891
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001892 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001893 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1894 return VK_ERROR_OUT_OF_DATE_KHR;
1895 }
1896
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001897 ANativeWindow* window = swapchain.surface.window.get();
1898 VkResult result = VK_SUCCESS;
1899
1900 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001901 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001902 native_window_enable_frame_timestamps(window, true);
1903 swapchain.frame_timestamps_enabled = true;
1904 }
1905
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001906 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07001907 // Get the latest ready timing count before copying, since the copied
1908 // timing info will be erased in copy_ready_timings function.
1909 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07001910 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001911 // Check the *count here against the recorded ready timing count, since
1912 // *count can be overwritten per spec describes.
1913 if (*count < n) {
1914 result = VK_INCOMPLETE;
1915 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001916 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001917 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001918 }
1919
1920 return result;
1921}
1922
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001923VKAPI_ATTR
1924VkResult GetSwapchainStatusKHR(
1925 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001926 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001927 ATRACE_CALL();
1928
Chris Forbes4e18ba82017-01-20 12:50:17 +13001929 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001930 VkResult result = VK_SUCCESS;
1931
Chris Forbes4e18ba82017-01-20 12:50:17 +13001932 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1933 return VK_ERROR_OUT_OF_DATE_KHR;
1934 }
1935
Yiwei Zhanga885c062019-10-24 12:07:57 -07001936 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001937
1938 return result;
1939}
1940
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001941VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001942 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001943 uint32_t swapchainCount,
1944 const VkSwapchainKHR* pSwapchains,
1945 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001946 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001947
1948 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1949 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1950 if (!swapchain)
1951 continue;
1952
1953 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1954
1955 ANativeWindow* window = swapchain->surface.window.get();
1956
1957 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1958 const android_smpte2086_metadata smpteMetdata = {
1959 {vulkanMetadata.displayPrimaryRed.x,
1960 vulkanMetadata.displayPrimaryRed.y},
1961 {vulkanMetadata.displayPrimaryGreen.x,
1962 vulkanMetadata.displayPrimaryGreen.y},
1963 {vulkanMetadata.displayPrimaryBlue.x,
1964 vulkanMetadata.displayPrimaryBlue.y},
1965 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1966 vulkanMetadata.maxLuminance,
1967 vulkanMetadata.minLuminance};
1968 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1969
1970 const android_cta861_3_metadata cta8613Metadata = {
1971 vulkanMetadata.maxContentLightLevel,
1972 vulkanMetadata.maxFrameAverageLightLevel};
1973 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1974 }
1975
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001976 return;
1977}
1978
Yiwei Zhang0f475222019-04-11 19:38:00 -07001979static void InterceptBindImageMemory2(
1980 uint32_t bind_info_count,
1981 const VkBindImageMemoryInfo* bind_infos,
1982 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1983 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1984 out_native_buffers->clear();
1985 out_bind_infos->clear();
1986
1987 if (!bind_info_count)
1988 return;
1989
1990 std::unordered_set<uint32_t> intercepted_indexes;
1991
1992 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1993 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1994 bind_infos[idx].pNext);
1995 while (info &&
1996 info->sType !=
1997 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1998 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1999 info->pNext);
2000 }
2001
2002 if (!info)
2003 continue;
2004
2005 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
2006 "swapchain handle must not be NULL");
2007 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
2008 ALOG_ASSERT(
2009 info->imageIndex < swapchain->num_images,
2010 "imageIndex must be less than the number of images in swapchain");
2011
2012 ANativeWindowBuffer* buffer =
2013 swapchain->images[info->imageIndex].buffer.get();
2014 VkNativeBufferANDROID native_buffer = {
2015#pragma clang diagnostic push
2016#pragma clang diagnostic ignored "-Wold-style-cast"
2017 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2018#pragma clang diagnostic pop
2019 .pNext = bind_infos[idx].pNext,
2020 .handle = buffer->handle,
2021 .stride = buffer->stride,
2022 .format = buffer->format,
2023 .usage = int(buffer->usage),
2024 };
2025 // Reserve enough space to avoid letting re-allocation invalidate the
2026 // addresses of the elements inside.
2027 out_native_buffers->reserve(bind_info_count);
2028 out_native_buffers->emplace_back(native_buffer);
2029
2030 // Reserve the space now since we know how much is needed now.
2031 out_bind_infos->reserve(bind_info_count);
2032 out_bind_infos->emplace_back(bind_infos[idx]);
2033 out_bind_infos->back().pNext = &out_native_buffers->back();
2034
2035 intercepted_indexes.insert(idx);
2036 }
2037
2038 if (intercepted_indexes.empty())
2039 return;
2040
2041 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2042 if (intercepted_indexes.count(idx))
2043 continue;
2044 out_bind_infos->emplace_back(bind_infos[idx]);
2045 }
2046}
2047
Yiwei Zhang23143102019-04-10 18:24:05 -07002048VKAPI_ATTR
2049VkResult BindImageMemory2(VkDevice device,
2050 uint32_t bindInfoCount,
2051 const VkBindImageMemoryInfo* pBindInfos) {
2052 ATRACE_CALL();
2053
Yiwei Zhang0f475222019-04-11 19:38:00 -07002054 // out_native_buffers is for maintaining the lifecycle of the constructed
2055 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2056 std::vector<VkNativeBufferANDROID> out_native_buffers;
2057 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2058 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2059 &out_bind_infos);
2060 return GetData(device).driver.BindImageMemory2(
2061 device, bindInfoCount,
2062 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002063}
2064
2065VKAPI_ATTR
2066VkResult BindImageMemory2KHR(VkDevice device,
2067 uint32_t bindInfoCount,
2068 const VkBindImageMemoryInfo* pBindInfos) {
2069 ATRACE_CALL();
2070
Yiwei Zhang0f475222019-04-11 19:38:00 -07002071 std::vector<VkNativeBufferANDROID> out_native_buffers;
2072 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2073 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2074 &out_bind_infos);
2075 return GetData(device).driver.BindImageMemory2KHR(
2076 device, bindInfoCount,
2077 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002078}
2079
Chia-I Wu62262232016-03-26 07:06:44 +08002080} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002081} // namespace vulkan