blob: 1fe8800e0997c2d45830c8b251df092e6679fdb5 [file] [log] [blame]
Jesse Hallb1352bc2015-09-04 16:12:33 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang0f475222019-04-11 19:38:00 -070019#include <android/hardware/graphics/common/1.0/types.h>
Jesse Hall79927812017-03-23 11:03:23 -070020#include <grallocusage/GrallocUsageConversion.h>
Yiwei Zhang69395cd2019-07-03 16:55:39 -070021#include <graphicsenv/GraphicsEnv.h>
John Reck97678d42022-08-23 11:24:51 -040022#include <hardware/gralloc.h>
23#include <hardware/gralloc1.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070024#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070025#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070026#include <system/window.h>
27#include <ui/BufferQueueDefs.h>
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -050028#include <ui/DebugUtils.h>
29#include <ui/PixelFormat.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080030#include <utils/StrongPointer.h>
Yiwei Zhang705c2e62019-12-18 23:12:43 -080031#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080032#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070033
34#include <algorithm>
35#include <unordered_set>
36#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070037
Chia-I Wu4a6a9162016-03-26 07:17:34 +080038#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070039
Daniel Kochf25f5bb2017-10-05 00:26:58 -040040using android::hardware::graphics::common::V1_0::BufferUsage;
41
Chia-I Wu62262232016-03-26 07:06:44 +080042namespace vulkan {
43namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070044
Jesse Halld7b994a2015-09-07 14:17:37 -070045namespace {
46
John Reck97678d42022-08-23 11:24:51 -040047static uint64_t convertGralloc1ToBufferUsage(uint64_t producerUsage,
48 uint64_t consumerUsage) {
49 static_assert(uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) ==
50 uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN),
51 "expected ConsumerUsage and ProducerUsage CPU_READ_OFTEN "
52 "bits to match");
53 uint64_t merged = producerUsage | consumerUsage;
54 if ((merged & (GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN)) ==
55 GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) {
56 merged &= ~uint64_t(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN);
57 merged |= BufferUsage::CPU_READ_OFTEN;
58 }
59 if ((merged & (GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) ==
60 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) {
61 merged &= ~uint64_t(GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN);
62 merged |= BufferUsage::CPU_WRITE_OFTEN;
63 }
64 return merged;
65}
66
Jesse Hall55bc0972016-02-23 16:43:29 -080067const VkSurfaceTransformFlagsKHR kSupportedTransforms =
68 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
69 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
70 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
71 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070072 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
73 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
74 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
75 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080076 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
77
78VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
79 // Native and Vulkan transforms are isomorphic, but are represented
80 // differently. Vulkan transforms are built up of an optional horizontal
81 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
82 // transforms are built up from a horizontal flip, vertical flip, and
83 // 90-degree rotation, all optional but always in that order.
84
Jesse Hall55bc0972016-02-23 16:43:29 -080085 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070086 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080087 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070088 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
89 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
90 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
91 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
92 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080093 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070094 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080095 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070096 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
97 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
98 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
99 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
100 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -0800101 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
102 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
103 default:
104 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
105 }
106}
107
Yiwei Zhang70a21962019-05-31 17:26:52 -0700108int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
109 switch (transform) {
110 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
111 return NATIVE_WINDOW_TRANSFORM_ROT_90;
112 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
113 return NATIVE_WINDOW_TRANSFORM_ROT_180;
114 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
115 return NATIVE_WINDOW_TRANSFORM_ROT_270;
116 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
117 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
118 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
119 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
120 NATIVE_WINDOW_TRANSFORM_ROT_90;
121 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
122 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
123 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
124 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
125 NATIVE_WINDOW_TRANSFORM_ROT_90;
126 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
127 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
128 default:
129 return 0;
130 }
131}
132
Jesse Hall178b6962016-02-24 15:39:50 -0800133int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
134 switch (transform) {
135 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
136 return NATIVE_WINDOW_TRANSFORM_ROT_270;
137 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
138 return NATIVE_WINDOW_TRANSFORM_ROT_180;
139 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
140 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700141 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
142 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
143 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
144 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
145 NATIVE_WINDOW_TRANSFORM_ROT_90;
146 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
147 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
148 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
149 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
150 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800151 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
152 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
153 default:
154 return 0;
155 }
156}
157
Ian Elliott8a977262017-01-19 09:05:58 -0700158class TimingInfo {
159 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800160 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700161 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800162 native_frame_id_(nativeFrameId) {}
163 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700164 return (timestamp_desired_present_time_ !=
165 NATIVE_WINDOW_TIMESTAMP_PENDING &&
166 timestamp_actual_present_time_ !=
167 NATIVE_WINDOW_TIMESTAMP_PENDING &&
168 timestamp_render_complete_time_ !=
169 NATIVE_WINDOW_TIMESTAMP_PENDING &&
170 timestamp_composition_latch_time_ !=
171 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700172 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700173 void calculate(int64_t rdur) {
174 bool anyTimestampInvalid =
175 (timestamp_actual_present_time_ ==
176 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
177 (timestamp_render_complete_time_ ==
178 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
179 (timestamp_composition_latch_time_ ==
180 NATIVE_WINDOW_TIMESTAMP_INVALID);
181 if (anyTimestampInvalid) {
182 ALOGE("Unexpectedly received invalid timestamp.");
183 vals_.actualPresentTime = 0;
184 vals_.earliestPresentTime = 0;
185 vals_.presentMargin = 0;
186 return;
187 }
188
189 vals_.actualPresentTime =
190 static_cast<uint64_t>(timestamp_actual_present_time_);
191 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700192 timestamp_render_complete_time_);
193 // Calculate vals_.earliestPresentTime, and potentially adjust
194 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
195 // is vals_.actualPresentTime. If we can subtract rdur (the duration
196 // of a refresh cycle) from vals_.earliestPresentTime (and also from
197 // vals_.presentMargin) and still leave a positive margin, then we can
198 // report to the application that it could have presented earlier than
199 // it did (per the extension specification). If for some reason, we
200 // can do this subtraction repeatedly, we do, since
201 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700202 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700203 while ((margin > rdur) &&
204 ((early_time - rdur) > timestamp_composition_latch_time_)) {
205 early_time -= rdur;
206 margin -= rdur;
207 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700208 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
209 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700210 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800211 void get_values(VkPastPresentationTimingGOOGLE* values) const {
212 *values = vals_;
213 }
Ian Elliott8a977262017-01-19 09:05:58 -0700214
215 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800216 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700217
Brian Anderson1049d1d2016-12-16 17:25:57 -0800218 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700219 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
220 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
221 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
222 int64_t timestamp_composition_latch_time_
223 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700224};
225
Jesse Hall1356b0d2015-11-23 17:24:58 -0800226struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800227 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700228 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700229 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800230};
231
232VkSurfaceKHR HandleFromSurface(Surface* surface) {
233 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
234}
235
236Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800237 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800238}
239
Ian Elliott8a977262017-01-19 09:05:58 -0700240// Maximum number of TimingInfo structs to keep per swapchain:
241enum { MAX_TIMING_INFOS = 10 };
242// Minimum number of frames to look for in the past (so we don't cause
243// syncronous requests to Surface Flinger):
244enum { MIN_NUM_FRAMES_AGO = 5 };
245
Jesse Hall1356b0d2015-11-23 17:24:58 -0800246struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700247 Swapchain(Surface& surface_,
248 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700249 VkPresentModeKHR present_mode,
250 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700251 : surface(surface_),
252 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700253 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700254 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300255 frame_timestamps_enabled(false),
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800256 acquire_next_image_timeout(-1),
Chris Forbesf8835642017-03-30 19:31:40 +1300257 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800258 present_mode ==
259 VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700260 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700261 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700262 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700263 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700264 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600265 uint64_t get_refresh_duration()
266 {
267 ANativeWindow* window = surface.window.get();
268 native_window_get_refresh_cycle_duration(
269 window,
270 &refresh_duration);
271 return static_cast<uint64_t>(refresh_duration);
272
273 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800274
275 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700276 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700277 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700278 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700279 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700280 int64_t refresh_duration;
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800281 nsecs_t acquire_next_image_timeout;
Chris Forbesf8835642017-03-30 19:31:40 +1300282 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700283
284 struct Image {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700285 Image()
286 : image(VK_NULL_HANDLE),
287 dequeue_fence(-1),
288 release_fence(-1),
289 dequeued(false) {}
Jesse Halld7b994a2015-09-07 14:17:37 -0700290 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800291 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700292 // The fence is only valid when the buffer is dequeued, and should be
293 // -1 any other time. When valid, we own the fd, and must ensure it is
294 // closed: either by closing it explicitly when queueing the buffer,
295 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
296 int dequeue_fence;
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700297 // This fence is a dup of the sync fd returned from the driver via
298 // vkQueueSignalReleaseImageANDROID upon vkQueuePresentKHR. We must
299 // ensure it is closed upon re-presenting or releasing the image.
300 int release_fence;
Jesse Halld7b994a2015-09-07 14:17:37 -0700301 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800302 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700303
Yiwei Zhang5e862202019-06-21 14:59:16 -0700304 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700305};
306
307VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
308 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
309}
310
311Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800312 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700313}
314
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700315static bool IsFencePending(int fd) {
316 if (fd < 0)
317 return false;
318
319 errno = 0;
320 return sync_wait(fd, 0 /* timeout */) == -1 && errno == ETIME;
321}
322
Jesse Halldc225072016-05-30 22:40:14 -0700323void ReleaseSwapchainImage(VkDevice device,
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000324 bool shared_present,
Jesse Halldc225072016-05-30 22:40:14 -0700325 ANativeWindow* window,
326 int release_fence,
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700327 Swapchain::Image& image,
328 bool defer_if_pending) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700329 ATRACE_CALL();
330
Jesse Halldc225072016-05-30 22:40:14 -0700331 ALOG_ASSERT(release_fence == -1 || image.dequeued,
332 "ReleaseSwapchainImage: can't provide a release fence for "
333 "non-dequeued images");
334
335 if (image.dequeued) {
336 if (release_fence >= 0) {
337 // We get here from vkQueuePresentKHR. The application is
338 // responsible for creating an execution dependency chain from
339 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
340 // (release_fence), so we can drop the dequeue_fence here.
341 if (image.dequeue_fence >= 0)
342 close(image.dequeue_fence);
343 } else {
344 // We get here during swapchain destruction, or various serious
345 // error cases e.g. when we can't create the release_fence during
346 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
347 // have already signalled, since the swapchain images are supposed
348 // to be idle before the swapchain is destroyed. In error cases,
349 // there may be rendering in flight to the image, but since we
350 // weren't able to create a release_fence, waiting for the
351 // dequeue_fence is about the best we can do.
352 release_fence = image.dequeue_fence;
353 }
354 image.dequeue_fence = -1;
355
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000356 // It's invalid to call cancelBuffer on a shared buffer
357 if (window && !shared_present) {
Jesse Halldc225072016-05-30 22:40:14 -0700358 window->cancelBuffer(window, image.buffer.get(), release_fence);
359 } else {
360 if (release_fence >= 0) {
361 sync_wait(release_fence, -1 /* forever */);
362 close(release_fence);
363 }
364 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700365 release_fence = -1;
Jesse Halldc225072016-05-30 22:40:14 -0700366 image.dequeued = false;
367 }
368
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700369 if (defer_if_pending && IsFencePending(image.release_fence))
370 return;
371
372 if (image.release_fence >= 0) {
373 close(image.release_fence);
374 image.release_fence = -1;
375 }
376
Jesse Halldc225072016-05-30 22:40:14 -0700377 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700378 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700379 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700380 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700381 image.image = VK_NULL_HANDLE;
382 }
383
384 image.buffer.clear();
385}
386
387void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
388 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
389 return;
Jesse Halldc225072016-05-30 22:40:14 -0700390 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +0000391 if (!swapchain->images[i].dequeued) {
392 ReleaseSwapchainImage(device, swapchain->shared, nullptr, -1,
393 swapchain->images[i], true);
394 }
Jesse Halldc225072016-05-30 22:40:14 -0700395 }
396 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700397 swapchain->timing.clear();
398}
399
400uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800401 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
402 return 0;
403 }
Ian Elliott8a977262017-01-19 09:05:58 -0700404
Brian Anderson1049d1d2016-12-16 17:25:57 -0800405 uint32_t num_ready = 0;
406 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
407 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700408 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800409 if (ti.ready()) {
410 // This TimingInfo is ready to be reported to the user. Add it
411 // to the num_ready.
412 num_ready++;
413 continue;
414 }
415 // This TimingInfo is not yet ready to be reported to the user,
416 // and so we should look for any available timestamps that
417 // might make it ready.
418 int64_t desired_present_time = 0;
419 int64_t render_complete_time = 0;
420 int64_t composition_latch_time = 0;
421 int64_t actual_present_time = 0;
422 // Obtain timestamps:
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800423 int err = native_window_get_frame_timestamps(
Brian Anderson1049d1d2016-12-16 17:25:57 -0800424 swapchain.surface.window.get(), ti.native_frame_id_,
425 &desired_present_time, &render_complete_time,
426 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700427 nullptr, //&first_composition_start_time,
428 nullptr, //&last_composition_start_time,
429 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800430 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700431 nullptr, //&dequeue_ready_time,
432 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800433
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800434 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800435 continue;
436 }
437
438 // Record the timestamp(s) we received, and then see if this TimingInfo
439 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700440 ti.timestamp_desired_present_time_ = desired_present_time;
441 ti.timestamp_actual_present_time_ = actual_present_time;
442 ti.timestamp_render_complete_time_ = render_complete_time;
443 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800444
445 if (ti.ready()) {
446 // The TimingInfo has received enough timestamps, and should now
447 // use those timestamps to calculate the info that should be
448 // reported to the user:
449 ti.calculate(swapchain.refresh_duration);
450 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700451 }
452 }
453 return num_ready;
454}
455
Ian Elliott8a977262017-01-19 09:05:58 -0700456void copy_ready_timings(Swapchain& swapchain,
457 uint32_t* count,
458 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800459 if (swapchain.timing.empty()) {
460 *count = 0;
461 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700462 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800463
464 size_t last_ready = swapchain.timing.size() - 1;
465 while (!swapchain.timing[last_ready].ready()) {
466 if (last_ready == 0) {
467 *count = 0;
468 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700469 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800470 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700471 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800472
473 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700474 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800475 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
476 const TimingInfo& ti = swapchain.timing[i];
477 if (ti.ready()) {
478 ti.get_values(&timings[num_copied]);
479 num_copied++;
480 }
481 num_to_remove++;
482 }
483
484 // Discard old frames that aren't ready if newer frames are ready.
485 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700486 swapchain.timing.erase(swapchain.timing.begin(),
487 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800488
Ian Elliott8a977262017-01-19 09:05:58 -0700489 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700490}
491
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500492android::PixelFormat GetNativePixelFormat(VkFormat format) {
493 android::PixelFormat native_format = android::PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700494 switch (format) {
495 case VK_FORMAT_R8G8B8A8_UNORM:
496 case VK_FORMAT_R8G8B8A8_SRGB:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500497 native_format = android::PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700498 break;
499 case VK_FORMAT_R5G6B5_UNORM_PACK16:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500500 native_format = android::PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700501 break;
502 case VK_FORMAT_R16G16B16A16_SFLOAT:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500503 native_format = android::PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700504 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800505 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500506 native_format = android::PIXEL_FORMAT_RGBA_1010102;
507 break;
508 case VK_FORMAT_R8_UNORM:
509 native_format = android::PIXEL_FORMAT_R_8;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700510 break;
511 default:
512 ALOGV("unsupported swapchain format %d", format);
513 break;
514 }
515 return native_format;
516}
517
518android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
519 switch (colorspace) {
520 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
521 return HAL_DATASPACE_V0_SRGB;
522 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
523 return HAL_DATASPACE_DISPLAY_P3;
524 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
525 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600526 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
527 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700528 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
529 return HAL_DATASPACE_DCI_P3_LINEAR;
530 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
531 return HAL_DATASPACE_DCI_P3;
532 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
533 return HAL_DATASPACE_V0_SRGB_LINEAR;
534 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
535 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600536 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
537 return HAL_DATASPACE_BT2020_LINEAR;
538 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700539 return static_cast<android_dataspace>(
540 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
541 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600542 case VK_COLOR_SPACE_DOLBYVISION_EXT:
543 return static_cast<android_dataspace>(
544 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
545 HAL_DATASPACE_RANGE_FULL);
546 case VK_COLOR_SPACE_HDR10_HLG_EXT:
547 return static_cast<android_dataspace>(
548 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
549 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700550 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
551 return static_cast<android_dataspace>(
552 HAL_DATASPACE_STANDARD_ADOBE_RGB |
553 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
554 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
555 return HAL_DATASPACE_ADOBE_RGB;
556
557 // Pass through is intended to allow app to provide data that is passed
558 // to the display system without modification.
559 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
560 return HAL_DATASPACE_ARBITRARY;
561
562 default:
563 // This indicates that we don't know about the
564 // dataspace specified and we should indicate that
565 // it's unsupported
566 return HAL_DATASPACE_UNKNOWN;
567 }
568}
569
Jesse Halld7b994a2015-09-07 14:17:37 -0700570} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700571
Jesse Halle1b12782015-11-30 11:27:32 -0800572VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800573VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800574 VkInstance instance,
575 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
576 const VkAllocationCallbacks* allocator,
577 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800578 ATRACE_CALL();
579
Jesse Hall1f91d392015-12-11 16:28:44 -0800580 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800581 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800582 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
583 alignof(Surface),
584 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800585 if (!mem)
586 return VK_ERROR_OUT_OF_HOST_MEMORY;
587 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700588
Chia-I Wue8e689f2016-04-18 08:21:31 +0800589 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700590 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700591 int err = native_window_get_consumer_usage(surface->window.get(),
592 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800593 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700594 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
595 strerror(-err), err);
596 surface->~Surface();
597 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700598 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700599 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700600
Yiwei Zhang6435b322018-05-08 11:12:17 -0700601 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800602 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800603 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800604 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
605 err);
606 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800607 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700608 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800609 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700610
Jesse Hall1356b0d2015-11-23 17:24:58 -0800611 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700612 return VK_SUCCESS;
613}
614
Jesse Halle1b12782015-11-30 11:27:32 -0800615VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800616void DestroySurfaceKHR(VkInstance instance,
617 VkSurfaceKHR surface_handle,
618 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800619 ATRACE_CALL();
620
Jesse Hall1356b0d2015-11-23 17:24:58 -0800621 Surface* surface = SurfaceFromHandle(surface_handle);
622 if (!surface)
623 return;
624 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700625 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700626 "destroyed VkSurfaceKHR 0x%" PRIx64
627 " has active VkSwapchainKHR 0x%" PRIx64,
628 reinterpret_cast<uint64_t>(surface_handle),
629 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800630 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800631 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800632 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800633 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800634}
635
Jesse Halle1b12782015-11-30 11:27:32 -0800636VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800637VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
638 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000639 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800640 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000641 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800642 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800643}
644
Jesse Halle1b12782015-11-30 11:27:32 -0800645VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800646VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Ian Elliott1ce053f2022-03-16 09:49:53 -0600647 VkPhysicalDevice pdev,
Jesse Hallb00daad2015-11-29 19:46:20 -0800648 VkSurfaceKHR surface,
649 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800650 ATRACE_CALL();
651
Jesse Halld7b994a2015-09-07 14:17:37 -0700652 int err;
Jesse Halld7b994a2015-09-07 14:17:37 -0700653 int width, height;
Jesse Hall55bc0972016-02-23 16:43:29 -0800654 int transform_hint;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800655 int max_buffer_count;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600656 if (surface == VK_NULL_HANDLE) {
657 const InstanceData& instance_data = GetData(pdev);
658 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
659 bool surfaceless_enabled =
660 instance_data.hook_extensions.test(surfaceless);
661 if (!surfaceless_enabled) {
662 // It is an error to pass a surface==VK_NULL_HANDLE unless the
663 // VK_GOOGLE_surfaceless_query extension is enabled
664 return VK_ERROR_SURFACE_LOST_KHR;
665 }
666 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
667 // extension for this function is for
668 // VkSurfaceProtectedCapabilitiesKHR::supportsProtected. The following
669 // four values cannot be known without a surface. Default values will
670 // be supplied anyway, but cannot be relied upon.
671 width = 1000;
672 height = 1000;
673 transform_hint = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
674 max_buffer_count = 10;
675 } else {
676 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
677
678 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
679 if (err != android::OK) {
680 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
681 strerror(-err), err);
682 return VK_ERROR_SURFACE_LOST_KHR;
683 }
684 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
685 if (err != android::OK) {
686 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
687 strerror(-err), err);
688 return VK_ERROR_SURFACE_LOST_KHR;
689 }
690
691 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
692 &transform_hint);
693 if (err != android::OK) {
694 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
695 strerror(-err), err);
696 return VK_ERROR_SURFACE_LOST_KHR;
697 }
698
699 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT,
700 &max_buffer_count);
701 if (err != android::OK) {
702 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
703 strerror(-err), err);
704 return VK_ERROR_SURFACE_LOST_KHR;
705 }
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800706 }
Ian Elliott16c443c2021-11-30 17:10:32 -0700707 capabilities->minImageCount = std::min(max_buffer_count, 3);
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800708 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700709
Jesse Hallfe2662d2016-02-09 13:26:59 -0800710 capabilities->currentExtent =
711 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
712
Yiwei Zhang70a21962019-05-31 17:26:52 -0700713 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800714 capabilities->minImageExtent = VkExtent2D{1, 1};
715 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700716
Jesse Hallfe2662d2016-02-09 13:26:59 -0800717 capabilities->maxImageArrayLayers = 1;
718
Jesse Hall55bc0972016-02-23 16:43:29 -0800719 capabilities->supportedTransforms = kSupportedTransforms;
720 capabilities->currentTransform =
721 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700722
Jesse Hallfe2662d2016-02-09 13:26:59 -0800723 // On Android, window composition is a WindowManager property, not something
724 // associated with the bufferqueue. It can't be changed from here.
725 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700726
Jesse Hallb00daad2015-11-29 19:46:20 -0800727 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800728 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
729 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
730 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700731 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
732
Jesse Hallb1352bc2015-09-04 16:12:33 -0700733 return VK_SUCCESS;
734}
735
Jesse Halle1b12782015-11-30 11:27:32 -0800736VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700737VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
738 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800739 uint32_t* count,
740 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800741 ATRACE_CALL();
742
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700743 const InstanceData& instance_data = GetData(pdev);
744
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700745 bool wide_color_support = false;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600746 uint64_t consumer_usage = 0;
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000747 bool colorspace_ext =
Ian Elliottf6df08e2022-03-16 21:27:49 -0600748 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
Ian Elliott1ce053f2022-03-16 09:49:53 -0600749 if (surface_handle == VK_NULL_HANDLE) {
750 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
751 bool surfaceless_enabled =
752 instance_data.hook_extensions.test(surfaceless);
753 if (!surfaceless_enabled) {
754 return VK_ERROR_SURFACE_LOST_KHR;
755 }
756 // Support for VK_GOOGLE_surfaceless_query. The EGL loader
757 // unconditionally supports wide color formats, even if they will cause
758 // a SurfaceFlinger fallback. Based on that, wide_color_support will be
759 // set to true in this case.
760 wide_color_support = true;
761
762 // TODO(b/203826952): research proper value; temporarily use the
763 // values seen on Pixel
764 consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
765 } else {
766 Surface& surface = *SurfaceFromHandle(surface_handle);
767 int err = native_window_get_wide_color_support(surface.window.get(),
768 &wide_color_support);
769 if (err) {
770 return VK_ERROR_SURFACE_LOST_KHR;
771 }
772 ALOGV("wide_color_support is: %d", wide_color_support);
773
774 consumer_usage = surface.consumer_usage;
Ian Elliott6ba85d92022-02-18 16:44:58 -0700775 }
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000776 wide_color_support = wide_color_support && colorspace_ext;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700777
Charlie Lao324191f2019-12-13 11:03:16 -0800778 AHardwareBuffer_Desc desc = {};
779 desc.width = 1;
780 desc.height = 1;
781 desc.layers = 1;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600782 desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
Charlie Lao324191f2019-12-13 11:03:16 -0800783 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
784
785 // We must support R8G8B8A8
786 std::vector<VkSurfaceFormatKHR> all_formats = {
787 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Lingfeng Yange29191c2022-07-01 18:18:29 -0700788 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
789 // Also allow to use PASS_THROUGH + HAL_DATASPACE_ARBITRARY
790 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_PASS_THROUGH_EXT},
791 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_PASS_THROUGH_EXT},
792 };
Charlie Lao324191f2019-12-13 11:03:16 -0800793
Trevor David Black5e13d0c2022-03-10 21:18:35 +0000794 if (colorspace_ext) {
795 all_formats.emplace_back(VkSurfaceFormatKHR{
796 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_BT709_LINEAR_EXT});
797 }
798
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700799 if (wide_color_support) {
Charlie Lao324191f2019-12-13 11:03:16 -0800800 all_formats.emplace_back(VkSurfaceFormatKHR{
801 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
802 all_formats.emplace_back(VkSurfaceFormatKHR{
803 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
804 }
805
Ian Elliott1ce053f2022-03-16 09:49:53 -0600806 // NOTE: Any new formats that are added must be coordinated across different
807 // Android users. This includes the ANGLE team (a layered implementation of
808 // OpenGL-ES).
809
Charlie Lao324191f2019-12-13 11:03:16 -0800810 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
811 if (AHardwareBuffer_isSupported(&desc)) {
812 all_formats.emplace_back(VkSurfaceFormatKHR{
813 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnak52ac1e92022-07-18 13:33:10 -0700814 all_formats.emplace_back(VkSurfaceFormatKHR{
815 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800816 }
817
818 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
819 if (AHardwareBuffer_isSupported(&desc)) {
820 all_formats.emplace_back(VkSurfaceFormatKHR{
821 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnak52ac1e92022-07-18 13:33:10 -0700822 all_formats.emplace_back(VkSurfaceFormatKHR{
823 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800824 if (wide_color_support) {
825 all_formats.emplace_back(
826 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
827 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
828 all_formats.emplace_back(
829 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
830 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
831 }
832 }
833
834 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
835 if (AHardwareBuffer_isSupported(&desc)) {
836 all_formats.emplace_back(
837 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
838 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
Jason Macnak52ac1e92022-07-18 13:33:10 -0700839 all_formats.emplace_back(
840 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
841 VK_COLOR_SPACE_PASS_THROUGH_EXT});
Charlie Lao324191f2019-12-13 11:03:16 -0800842 if (wide_color_support) {
843 all_formats.emplace_back(
844 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
845 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
846 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700847 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700848
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -0500849 desc.format = AHARDWAREBUFFER_FORMAT_R8_UNORM;
850 if (AHardwareBuffer_isSupported(&desc)) {
851 all_formats.emplace_back(
852 VkSurfaceFormatKHR{VK_FORMAT_R8_UNORM,
853 VK_COLOR_SPACE_PASS_THROUGH_EXT});
854 }
855
Ian Elliott6ba85d92022-02-18 16:44:58 -0700856 // NOTE: Any new formats that are added must be coordinated across different
857 // Android users. This includes the ANGLE team (a layered implementation of
858 // OpenGL-ES).
859
Jesse Halld7b994a2015-09-07 14:17:37 -0700860 VkResult result = VK_SUCCESS;
861 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800862 uint32_t transfer_count = all_formats.size();
863 if (transfer_count > *count) {
864 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700865 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700866 }
Charlie Lao324191f2019-12-13 11:03:16 -0800867 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
868 formats);
869 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700870 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800871 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700872 }
Charlie Lao324191f2019-12-13 11:03:16 -0800873
Jesse Halld7b994a2015-09-07 14:17:37 -0700874 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700875}
876
Jesse Halle1b12782015-11-30 11:27:32 -0800877VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300878VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
879 VkPhysicalDevice physicalDevice,
880 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
881 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800882 ATRACE_CALL();
883
Chris Forbes2452cf72017-03-16 16:30:17 +1300884 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
885 physicalDevice, pSurfaceInfo->surface,
886 &pSurfaceCapabilities->surfaceCapabilities);
887
Chris Forbes06bc0092017-03-16 16:46:05 +1300888 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
889 while (caps->pNext) {
890 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
891
892 switch (caps->sType) {
893 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
894 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
895 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
896 caps);
897 // Claim same set of usage flags are supported for
898 // shared present modes as for other modes.
899 shared_caps->sharedPresentSupportedUsageFlags =
900 pSurfaceCapabilities->surfaceCapabilities
901 .supportedUsageFlags;
902 } break;
903
Ian Elliottbb67b242022-03-16 09:52:28 -0600904 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
905 VkSurfaceProtectedCapabilitiesKHR* protected_caps =
906 reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(caps);
907 protected_caps->supportsProtected = VK_TRUE;
908 } break;
909
Chris Forbes06bc0092017-03-16 16:46:05 +1300910 default:
911 // Ignore all other extension structs
912 break;
913 }
914 }
915
Chris Forbes2452cf72017-03-16 16:30:17 +1300916 return result;
917}
918
919VKAPI_ATTR
920VkResult GetPhysicalDeviceSurfaceFormats2KHR(
921 VkPhysicalDevice physicalDevice,
922 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
923 uint32_t* pSurfaceFormatCount,
924 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800925 ATRACE_CALL();
926
Chris Forbes2452cf72017-03-16 16:30:17 +1300927 if (!pSurfaceFormats) {
928 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
929 pSurfaceInfo->surface,
930 pSurfaceFormatCount, nullptr);
931 } else {
932 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
933 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700934 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300935 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
936 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700937 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300938
939 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
940 // marshal results individually due to stride difference.
941 // completely ignore any chained extension structs.
942 uint32_t formats_to_marshal = *pSurfaceFormatCount;
943 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
944 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
945 }
946 }
947
948 return result;
949 }
950}
951
952VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300953VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800954 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800955 uint32_t* count,
956 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800957 ATRACE_CALL();
958
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800959 int err;
960 int query_value;
Ian Elliotte7f036c2022-03-15 16:49:21 -0600961 std::vector<VkPresentModeKHR> present_modes;
Ian Elliott1ce053f2022-03-16 09:49:53 -0600962 if (surface == VK_NULL_HANDLE) {
963 const InstanceData& instance_data = GetData(pdev);
964 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
965 bool surfaceless_enabled =
966 instance_data.hook_extensions.test(surfaceless);
967 if (!surfaceless_enabled) {
968 return VK_ERROR_SURFACE_LOST_KHR;
969 }
970 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
971 // extension for this function is for
972 // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and
973 // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot
974 // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a
Ian Elliott7e361142022-06-02 10:45:17 -0600975 // surface, and that cannot be relied upon. Therefore, don't return it.
Ian Elliott1ce053f2022-03-16 09:49:53 -0600976 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
977 } else {
978 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
979
980 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
981 &query_value);
982 if (err != android::OK || query_value < 0) {
983 ALOGE(
984 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
985 "value=%d",
986 strerror(-err), err, query_value);
987 return VK_ERROR_SURFACE_LOST_KHR;
988 }
989 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
990
991 err =
992 window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
993 if (err != android::OK || query_value < 0) {
994 ALOGE(
995 "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
996 strerror(-err), err, query_value);
997 return VK_ERROR_SURFACE_LOST_KHR;
998 }
999 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
1000
1001 if (min_undequeued_buffers + 1 < max_buffer_count)
1002 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
1003 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
1004 }
Chris Forbese8d79a62017-02-22 12:49:18 +13001005
1006 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001007 QueryPresentationProperties(pdev, &present_properties);
1008 if (present_properties.sharedImage) {
1009 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
1010 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +13001011 }
1012
1013 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -07001014
1015 VkResult result = VK_SUCCESS;
1016 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +13001017 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -07001018 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +13001019 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -07001020 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -07001021 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +13001022 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -07001023 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001024 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001025}
1026
Jesse Halle1b12782015-11-30 11:27:32 -08001027VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001028VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001029 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001030 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001031 ATRACE_CALL();
1032
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001033 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
1034 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
1035 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
1036 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
1037 pDeviceGroupPresentCapabilities->sType);
1038
1039 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
1040 sizeof(pDeviceGroupPresentCapabilities->presentMask));
1041
1042 // assume device group of size 1
1043 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
1044 pDeviceGroupPresentCapabilities->modes =
1045 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1046
1047 return VK_SUCCESS;
1048}
1049
1050VKAPI_ATTR
1051VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001052 VkDevice,
1053 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001054 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001055 ATRACE_CALL();
1056
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001057 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1058 return VK_SUCCESS;
1059}
1060
1061VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -06001062VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001063 VkSurfaceKHR surface,
1064 uint32_t* pRectCount,
1065 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001066 ATRACE_CALL();
1067
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001068 if (!pRects) {
1069 *pRectCount = 1;
1070 } else {
1071 uint32_t count = std::min(*pRectCount, 1u);
1072 bool incomplete = *pRectCount < 1;
1073
1074 *pRectCount = count;
1075
1076 if (incomplete) {
1077 return VK_INCOMPLETE;
1078 }
1079
1080 int err;
1081 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1082
1083 int width = 0, height = 0;
1084 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001085 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001086 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1087 strerror(-err), err);
1088 }
1089 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001090 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001091 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1092 strerror(-err), err);
1093 }
1094
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001095 pRects[0].offset.x = 0;
1096 pRects[0].offset.y = 0;
1097 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1098 static_cast<uint32_t>(height)};
1099 }
1100 return VK_SUCCESS;
1101}
1102
Yiwei Zhang533cea92019-06-03 18:43:24 -07001103static void DestroySwapchainInternal(VkDevice device,
1104 VkSwapchainKHR swapchain_handle,
1105 const VkAllocationCallbacks* allocator) {
1106 ATRACE_CALL();
1107
1108 const auto& dispatch = GetData(device).driver;
1109 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1110 if (!swapchain) {
1111 return;
1112 }
1113
1114 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1115 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1116
1117 if (window && swapchain->frame_timestamps_enabled) {
1118 native_window_enable_frame_timestamps(window, false);
1119 }
1120
1121 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001122 ReleaseSwapchainImage(device, swapchain->shared, window, -1,
1123 swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001124 }
1125
1126 if (active) {
1127 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1128 }
1129
1130 if (!allocator) {
1131 allocator = &GetData(device).allocator;
1132 }
1133
1134 swapchain->~Swapchain();
1135 allocator->pfnFree(allocator->pUserData, swapchain);
1136}
1137
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001138VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001139VkResult CreateSwapchainKHR(VkDevice device,
1140 const VkSwapchainCreateInfoKHR* create_info,
1141 const VkAllocationCallbacks* allocator,
1142 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001143 ATRACE_CALL();
1144
Jesse Halld7b994a2015-09-07 14:17:37 -07001145 int err;
1146 VkResult result = VK_SUCCESS;
1147
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001148 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1149 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1150 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1151 " oldSwapchain=0x%" PRIx64,
1152 reinterpret_cast<uint64_t>(create_info->surface),
1153 create_info->minImageCount, create_info->imageFormat,
1154 create_info->imageColorSpace, create_info->imageExtent.width,
1155 create_info->imageExtent.height, create_info->imageUsage,
1156 create_info->preTransform, create_info->presentMode,
1157 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1158
Jesse Hall1f91d392015-12-11 16:28:44 -08001159 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001160 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001161
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001162 android::PixelFormat native_pixel_format =
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001163 GetNativePixelFormat(create_info->imageFormat);
1164 android_dataspace native_dataspace =
1165 GetNativeDataspace(create_info->imageColorSpace);
1166 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1167 ALOGE(
1168 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1169 "failed: Unsupported color space",
1170 create_info->imageColorSpace);
1171 return VK_ERROR_INITIALIZATION_FAILED;
1172 }
1173
Jesse Hall42a9eec2016-06-03 12:39:49 -07001174 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001175 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001176 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001177 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001178 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001179 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001180 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001181 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001182 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1183 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001184 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001185 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001186
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001187 Surface& surface = *SurfaceFromHandle(create_info->surface);
1188
Jesse Halldc225072016-05-30 22:40:14 -07001189 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001190 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001191 " because it already has active swapchain 0x%" PRIx64
1192 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1193 reinterpret_cast<uint64_t>(create_info->surface),
1194 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1195 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1196 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1197 }
1198 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1199 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1200
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001201 // -- Reset the native window --
1202 // The native window might have been used previously, and had its properties
1203 // changed from defaults. That will affect the answer we get for queries
1204 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1205 // attempt such queries.
1206
Jesse Halldc225072016-05-30 22:40:14 -07001207 // The native window only allows dequeueing all buffers before any have
1208 // been queued, since after that point at least one is assumed to be in
1209 // non-FREE state at any given time. Disconnecting and re-connecting
1210 // orphans the previous buffers, getting us back to the state where we can
1211 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001212 //
1213 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001214 ANativeWindow* window = surface.window.get();
1215 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1216 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001217 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001218 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1219 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001220 strerror(-err), err);
1221
Nicolas Capens147b7da2021-04-09 14:53:06 -04001222 err =
1223 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001224 if (err != android::OK) {
1225 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1226 strerror(-err), err);
1227 return VK_ERROR_SURFACE_LOST_KHR;
1228 }
1229
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301230 int swap_interval =
1231 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001232 err = window->setSwapInterval(window, swap_interval);
1233 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001234 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1235 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001236 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001237 }
1238
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001239 err = native_window_set_shared_buffer_mode(window, false);
1240 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001241 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1242 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001243 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001244 }
1245
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001246 err = native_window_set_auto_refresh(window, false);
1247 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001248 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1249 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001250 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001251 }
1252
Jesse Halld7b994a2015-09-07 14:17:37 -07001253 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001254
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001255 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001256
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001257 err = native_window_set_buffers_format(window, native_pixel_format);
1258 if (err != android::OK) {
Leon Scroggins IIIcb45fe72021-11-30 16:17:15 -05001259 ALOGE("native_window_set_buffers_format(%s) failed: %s (%d)",
1260 decodePixelFormat(native_pixel_format).c_str(), strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001261 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001262 }
Yiwei Zhang51572c22022-07-22 23:08:30 +00001263
1264 /* Respect consumer default dataspace upon HAL_DATASPACE_ARBITRARY. */
1265 if (native_dataspace != HAL_DATASPACE_ARBITRARY) {
1266 err = native_window_set_buffers_data_space(window, native_dataspace);
1267 if (err != android::OK) {
1268 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1269 native_dataspace, strerror(-err), err);
1270 return VK_ERROR_SURFACE_LOST_KHR;
1271 }
Jesse Hall517274a2016-02-10 00:07:18 -08001272 }
1273
Jesse Hall3dd678a2016-01-08 21:52:01 -08001274 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001275 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001276 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001277 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001278 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1279 create_info->imageExtent.width, create_info->imageExtent.height,
1280 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001281 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001282 }
1283
Jesse Hall178b6962016-02-24 15:39:50 -08001284 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1285 // applied during rendering. native_window_set_transform() expects the
1286 // inverse: the transform the app is requesting that the compositor perform
1287 // during composition. With native windows, pre-transform works by rendering
1288 // with the same transform the compositor is applying (as in Vulkan), but
1289 // then requesting the inverse transform, so that when the compositor does
1290 // it's job the two transforms cancel each other out and the compositor ends
1291 // up applying an identity transform to the app's buffer.
1292 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001293 window, InvertTransformToNative(create_info->preTransform));
1294 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001295 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1296 InvertTransformToNative(create_info->preTransform),
1297 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001298 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001299 }
1300
Jesse Hallf64ca122015-11-03 16:11:10 -08001301 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001302 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1303 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001304 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1305 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001306 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001307 }
1308
Chris Forbes97ef4612017-03-30 19:37:50 +13001309 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1310 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1311 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1312 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001313 err = native_window_set_shared_buffer_mode(window, true);
1314 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001315 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1316 return VK_ERROR_SURFACE_LOST_KHR;
1317 }
1318 }
1319
1320 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001321 err = native_window_set_auto_refresh(window, true);
1322 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001323 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1324 return VK_ERROR_SURFACE_LOST_KHR;
1325 }
1326 }
1327
Ian Elliott16c443c2021-11-30 17:10:32 -07001328 int query_value;
1329 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1330 &query_value);
1331 if (err != android::OK || query_value < 0) {
1332 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1333 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001334 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001335 }
Ian Elliott16c443c2021-11-30 17:10:32 -07001336 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1337 const auto mailbox_num_images = std::max(3u, create_info->minImageCount);
1338 const auto requested_images =
1339 swap_interval ? create_info->minImageCount : mailbox_num_images;
1340 uint32_t num_images = requested_images - 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001341
Ian Elliott5396b702021-12-13 19:32:34 -07001342 // Lower layer insists that we have at least min_undequeued_buffers + 1
1343 // buffers. This is wasteful and we'd like to relax it in the shared case,
1344 // but not all the pieces are in place for that to work yet. Note we only
1345 // lie to the lower layer--we don't want to give the app back a swapchain
1346 // with extra images (which they can't actually use!).
1347 uint32_t min_buffer_count = min_undequeued_buffers + 1;
1348 err = native_window_set_buffer_count(
1349 window, std::max(min_buffer_count, num_images));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001350 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001351 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1352 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001353 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001354 }
1355
Ian Elliott12b7e2f2021-12-21 23:24:20 -07001356 // In shared mode the num_images must be one regardless of how many
1357 // buffers were allocated for the buffer queue.
1358 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1359 num_images = 1;
1360 }
1361
John Reck97678d42022-08-23 11:24:51 -04001362 uint64_t native_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001363 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001364 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001365 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001366 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1367 device, create_info->imageFormat, create_info->imageUsage,
1368 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001369 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001370 if (result != VK_SUCCESS) {
1371 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001372 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001373 }
John Reck97678d42022-08-23 11:24:51 -04001374 native_usage =
1375 convertGralloc1ToBufferUsage(consumer_usage, producer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001376 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001377 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
John Reck97678d42022-08-23 11:24:51 -04001378 int32_t legacy_usage = 0;
Jesse Hall1f91d392015-12-11 16:28:44 -08001379 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001380 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001381 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001382 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001383 if (result != VK_SUCCESS) {
1384 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001385 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001386 }
John Reck97678d42022-08-23 11:24:51 -04001387 native_usage = static_cast<uint64_t>(legacy_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001388 }
John Reck97678d42022-08-23 11:24:51 -04001389 native_usage |= surface.consumer_usage;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001390
1391 bool createProtectedSwapchain = false;
1392 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1393 createProtectedSwapchain = true;
1394 native_usage |= BufferUsage::PROTECTED;
1395 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001396 err = native_window_set_usage(window, native_usage);
1397 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001398 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001399 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001400 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001401
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001402 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001403 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1404 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001405 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1406 strerror(-err), err);
1407 return VK_ERROR_SURFACE_LOST_KHR;
1408 }
1409
Jesse Halld7b994a2015-09-07 14:17:37 -07001410 // -- Allocate our Swapchain object --
1411 // After this point, we must deallocate the swapchain on error.
1412
Jesse Hall1f91d392015-12-11 16:28:44 -08001413 void* mem = allocator->pfnAllocation(allocator->pUserData,
1414 sizeof(Swapchain), alignof(Swapchain),
1415 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001416 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001417 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001418 Swapchain* swapchain = new (mem)
1419 Swapchain(surface, num_images, create_info->presentMode,
1420 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001421 // -- Dequeue all buffers and create a VkImage for each --
1422 // Any failures during or after this must cancel the dequeued buffers.
1423
Chris Forbesb56287a2017-01-12 14:28:58 +13001424 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1425#pragma clang diagnostic push
1426#pragma clang diagnostic ignored "-Wold-style-cast"
1427 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1428#pragma clang diagnostic pop
1429 .pNext = nullptr,
1430 .usage = swapchain_image_usage,
1431 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001432 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001433#pragma clang diagnostic push
1434#pragma clang diagnostic ignored "-Wold-style-cast"
1435 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1436#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001437 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001438 };
1439 VkImageCreateInfo image_create = {
1440 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1441 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001442 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001443 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001444 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001445 .extent = {0, 0, 1},
1446 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001447 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001448 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001449 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001450 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001451 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001452 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001453 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1454 };
1455
Jesse Halld7b994a2015-09-07 14:17:37 -07001456 for (uint32_t i = 0; i < num_images; i++) {
1457 Swapchain::Image& img = swapchain->images[i];
1458
1459 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001460 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1461 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001462 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001463 switch (-err) {
1464 case ENOMEM:
1465 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1466 break;
1467 default:
1468 result = VK_ERROR_SURFACE_LOST_KHR;
1469 break;
1470 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001471 break;
1472 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001473 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001474 img.dequeued = true;
1475
1476 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001477 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1478 static_cast<uint32_t>(img.buffer->height),
1479 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001480 image_native_buffer.handle = img.buffer->handle;
1481 image_native_buffer.stride = img.buffer->stride;
1482 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001483 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001484 android_convertGralloc0To1Usage(int(img.buffer->usage),
1485 &image_native_buffer.usage2.producer,
1486 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001487
Yiwei Zhang533cea92019-06-03 18:43:24 -07001488 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001489 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001490 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001491 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001492 if (result != VK_SUCCESS) {
1493 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1494 break;
1495 }
1496 }
1497
1498 // -- Cancel all buffers, returning them to the queue --
1499 // If an error occurred before, also destroy the VkImage and release the
1500 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001501 for (uint32_t i = 0; i < num_images; i++) {
1502 Swapchain::Image& img = swapchain->images[i];
1503 if (img.dequeued) {
1504 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001505 window->cancelBuffer(window, img.buffer.get(),
1506 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001507 img.dequeue_fence = -1;
1508 img.dequeued = false;
1509 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001510 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001511 }
1512
1513 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001514 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1515 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001516 return result;
1517 }
1518
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001519 if (transform_hint != swapchain->pre_transform) {
1520 // Log that the app is not doing pre-rotation.
1521 android::GraphicsEnv::getInstance().setTargetStats(
1522 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1523 }
1524
Jesse Halldc225072016-05-30 22:40:14 -07001525 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1526 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001527 return VK_SUCCESS;
1528}
1529
Jesse Halle1b12782015-11-30 11:27:32 -08001530VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001531void DestroySwapchainKHR(VkDevice device,
1532 VkSwapchainKHR swapchain_handle,
1533 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001534 ATRACE_CALL();
1535
Yiwei Zhang533cea92019-06-03 18:43:24 -07001536 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001537}
1538
Jesse Halle1b12782015-11-30 11:27:32 -08001539VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001540VkResult GetSwapchainImagesKHR(VkDevice,
1541 VkSwapchainKHR swapchain_handle,
1542 uint32_t* count,
1543 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001544 ATRACE_CALL();
1545
Jesse Halld7b994a2015-09-07 14:17:37 -07001546 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001547 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1548 "getting images for non-active swapchain 0x%" PRIx64
1549 "; only dequeued image handles are valid",
1550 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001551 VkResult result = VK_SUCCESS;
1552 if (images) {
1553 uint32_t n = swapchain.num_images;
1554 if (*count < swapchain.num_images) {
1555 n = *count;
1556 result = VK_INCOMPLETE;
1557 }
1558 for (uint32_t i = 0; i < n; i++)
1559 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001560 *count = n;
1561 } else {
1562 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001563 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001564 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001565}
1566
Jesse Halle1b12782015-11-30 11:27:32 -08001567VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001568VkResult AcquireNextImageKHR(VkDevice device,
1569 VkSwapchainKHR swapchain_handle,
1570 uint64_t timeout,
1571 VkSemaphore semaphore,
1572 VkFence vk_fence,
1573 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001574 ATRACE_CALL();
1575
Jesse Halld7b994a2015-09-07 14:17:37 -07001576 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001577 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001578 VkResult result;
1579 int err;
1580
Jesse Halldc225072016-05-30 22:40:14 -07001581 if (swapchain.surface.swapchain_handle != swapchain_handle)
1582 return VK_ERROR_OUT_OF_DATE_KHR;
1583
Chris Forbesc88409c2017-03-30 19:47:37 +13001584 if (swapchain.shared) {
1585 // In shared mode, we keep the buffer dequeued all the time, so we don't
1586 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1587 // the semaphore and fence passed to us will be signalled.
1588 *image_index = 0;
1589 result = GetData(device).driver.AcquireImageANDROID(
1590 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1591 return result;
1592 }
1593
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001594 const nsecs_t acquire_next_image_timeout =
1595 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1596 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1597 // Cache the timeout to avoid the duplicate binder cost.
1598 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1599 acquire_next_image_timeout);
1600 if (err != android::OK) {
1601 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1602 strerror(-err), err);
1603 return VK_ERROR_SURFACE_LOST_KHR;
1604 }
1605 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1606 }
1607
Jesse Halld7b994a2015-09-07 14:17:37 -07001608 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001609 int fence_fd;
1610 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001611 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001612 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1613 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1614 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001615 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001616 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001617 }
1618
1619 uint32_t idx;
1620 for (idx = 0; idx < swapchain.num_images; idx++) {
1621 if (swapchain.images[idx].buffer.get() == buffer) {
1622 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001623 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001624 break;
1625 }
1626 }
1627 if (idx == swapchain.num_images) {
1628 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001629 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001630 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001631 }
1632
1633 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001634 if (fence_fd != -1) {
1635 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001636 if (fence_clone == -1) {
1637 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1638 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001639 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001640 }
1641 }
1642
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001643 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001644 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001645 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001646 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1647 // even if the call fails. We could close it ourselves on failure, but
1648 // that would create a race condition if the driver closes it on a
1649 // failure path: some other thread might create an fd with the same
1650 // number between the time the driver closes it and the time we close
1651 // it. We must assume one of: the driver *always* closes it even on
1652 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001653 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001654 swapchain.images[idx].dequeued = false;
1655 swapchain.images[idx].dequeue_fence = -1;
1656 return result;
1657 }
1658
1659 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001660 return VK_SUCCESS;
1661}
1662
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001663VKAPI_ATTR
1664VkResult AcquireNextImage2KHR(VkDevice device,
1665 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1666 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001667 ATRACE_CALL();
1668
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001669 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1670 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1671 pAcquireInfo->fence, pImageIndex);
1672}
1673
Jesse Halldc225072016-05-30 22:40:14 -07001674static VkResult WorstPresentResult(VkResult a, VkResult b) {
1675 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1676 // (in spec version 1.0.14).
1677 static const VkResult kWorstToBest[] = {
1678 VK_ERROR_DEVICE_LOST,
1679 VK_ERROR_SURFACE_LOST_KHR,
1680 VK_ERROR_OUT_OF_DATE_KHR,
1681 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1682 VK_ERROR_OUT_OF_HOST_MEMORY,
1683 VK_SUBOPTIMAL_KHR,
1684 };
1685 for (auto result : kWorstToBest) {
1686 if (a == result || b == result)
1687 return result;
1688 }
1689 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1690 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1691 return a != VK_SUCCESS ? a : b;
1692}
1693
Jesse Halle1b12782015-11-30 11:27:32 -08001694VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001695VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001696 ATRACE_CALL();
1697
Jesse Halld7b994a2015-09-07 14:17:37 -07001698 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1699 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1700 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001701
Jesse Halldc225072016-05-30 22:40:14 -07001702 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001703 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001704 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001705
Ian Elliottcb351132016-12-13 10:30:40 -07001706 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001707 const VkPresentRegionsKHR* present_regions = nullptr;
1708 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001709 const VkPresentRegionsKHR* next =
1710 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1711 while (next) {
1712 switch (next->sType) {
1713 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1714 present_regions = next;
1715 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001716 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001717 present_times =
1718 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1719 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001720 default:
1721 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1722 next->sType);
1723 break;
1724 }
1725 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1726 }
1727 ALOGV_IF(
1728 present_regions &&
1729 present_regions->swapchainCount != present_info->swapchainCount,
1730 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001731 ALOGV_IF(present_times &&
1732 present_times->swapchainCount != present_info->swapchainCount,
1733 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1734 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001735 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001736 (present_regions) ? present_regions->pRegions : nullptr;
1737 const VkPresentTimeGOOGLE* times =
1738 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001739 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001740 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001741 uint32_t nrects = 0;
1742
Jesse Halld7b994a2015-09-07 14:17:37 -07001743 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1744 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001745 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001746 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001747 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001748 const VkPresentRegionKHR* region =
1749 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001750 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001751 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001752 VkResult result;
1753 int err;
1754
Jesse Halld7b994a2015-09-07 14:17:37 -07001755 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001756 result = dispatch.QueueSignalReleaseImageANDROID(
1757 queue, present_info->waitSemaphoreCount,
1758 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001759 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001760 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001761 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001762 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001763 if (img.release_fence >= 0)
1764 close(img.release_fence);
1765 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001766
Jesse Halldc225072016-05-30 22:40:14 -07001767 if (swapchain.surface.swapchain_handle ==
1768 present_info->pSwapchains[sc]) {
1769 ANativeWindow* window = swapchain.surface.window.get();
1770 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001771 if (region) {
1772 // Process the incremental-present hint for this swapchain:
1773 uint32_t rcount = region->rectangleCount;
1774 if (rcount > nrects) {
1775 android_native_rect_t* new_rects =
1776 static_cast<android_native_rect_t*>(
1777 allocator->pfnReallocation(
1778 allocator->pUserData, rects,
1779 sizeof(android_native_rect_t) * rcount,
1780 alignof(android_native_rect_t),
1781 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1782 if (new_rects) {
1783 rects = new_rects;
1784 nrects = rcount;
1785 } else {
1786 rcount = 0; // Ignore the hint for this swapchain
1787 }
1788 }
1789 for (uint32_t r = 0; r < rcount; ++r) {
1790 if (region->pRectangles[r].layer > 0) {
1791 ALOGV(
1792 "vkQueuePresentKHR ignoring invalid layer "
1793 "(%u); using layer 0 instead",
1794 region->pRectangles[r].layer);
1795 }
1796 int x = region->pRectangles[r].offset.x;
1797 int y = region->pRectangles[r].offset.y;
1798 int width = static_cast<int>(
1799 region->pRectangles[r].extent.width);
1800 int height = static_cast<int>(
1801 region->pRectangles[r].extent.height);
1802 android_native_rect_t* cur_rect = &rects[r];
1803 cur_rect->left = x;
1804 cur_rect->top = y + height;
1805 cur_rect->right = x + width;
1806 cur_rect->bottom = y;
1807 }
1808 native_window_set_surface_damage(window, rects, rcount);
1809 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001810 if (time) {
1811 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001812 ALOGV(
1813 "Calling "
1814 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001815 native_window_enable_frame_timestamps(window, true);
1816 swapchain.frame_timestamps_enabled = true;
1817 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001818
1819 // Record the nativeFrameId so it can be later correlated to
1820 // this present.
1821 uint64_t nativeFrameId = 0;
1822 err = native_window_get_next_frame_id(
1823 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001824 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001825 ALOGE("Failed to get next native frame ID.");
1826 }
1827
1828 // Add a new timing record with the user's presentID and
1829 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001830 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001831 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001832 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001833 }
1834 if (time->desiredPresentTime) {
1835 // Set the desiredPresentTime:
1836 ALOGV(
1837 "Calling "
1838 "native_window_set_buffers_timestamp(%" PRId64 ")",
1839 time->desiredPresentTime);
1840 native_window_set_buffers_timestamp(
1841 window,
1842 static_cast<int64_t>(time->desiredPresentTime));
1843 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001844 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001845
Jesse Halldc225072016-05-30 22:40:14 -07001846 err = window->queueBuffer(window, img.buffer.get(), fence);
1847 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001848 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001849 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1850 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001851 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001852 } else {
1853 if (img.dequeue_fence >= 0) {
1854 close(img.dequeue_fence);
1855 img.dequeue_fence = -1;
1856 }
1857 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001858 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001859
1860 // If the swapchain is in shared mode, immediately dequeue the
1861 // buffer so it can be presented again without an intervening
1862 // call to AcquireNextImageKHR. We expect to get the same buffer
1863 // back from every call to dequeueBuffer in this mode.
1864 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1865 ANativeWindowBuffer* buffer;
1866 int fence_fd;
1867 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001868 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001869 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1870 swapchain_result = WorstPresentResult(swapchain_result,
1871 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001872 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001873 ALOGE("got wrong image back for shared swapchain");
1874 swapchain_result = WorstPresentResult(swapchain_result,
1875 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001876 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13001877 img.dequeue_fence = fence_fd;
1878 img.dequeued = true;
1879 }
1880 }
Jesse Halldc225072016-05-30 22:40:14 -07001881 }
1882 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001883 OrphanSwapchain(device, &swapchain);
1884 }
silence_dogood73597592019-05-23 16:57:37 -07001885 int window_transform_hint;
1886 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1887 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001888 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07001889 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1890 strerror(-err), err);
1891 swapchain_result = WorstPresentResult(
1892 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1893 }
1894 if (swapchain.pre_transform != window_transform_hint) {
1895 swapchain_result =
1896 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1897 }
Jesse Halldc225072016-05-30 22:40:14 -07001898 } else {
Yiwei Zhangac1f0982022-04-09 07:10:10 +00001899 ReleaseSwapchainImage(device, swapchain.shared, nullptr, fence,
1900 img, true);
Jesse Halldc225072016-05-30 22:40:14 -07001901 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001902 }
1903
Jesse Halla9e57032015-11-30 01:03:10 -08001904 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001905 present_info->pResults[sc] = swapchain_result;
1906
1907 if (swapchain_result != final_result)
1908 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001909 }
Ian Elliottcb351132016-12-13 10:30:40 -07001910 if (rects) {
1911 allocator->pfnFree(allocator->pUserData, rects);
1912 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001913
1914 return final_result;
1915}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001916
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001917VKAPI_ATTR
1918VkResult GetRefreshCycleDurationGOOGLE(
1919 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001920 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001921 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001922 ATRACE_CALL();
1923
Ian Elliott62c48c92017-01-20 13:13:20 -07001924 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001925 VkResult result = VK_SUCCESS;
1926
Ian Elliott3568bfe2019-05-03 15:54:46 -06001927 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001928
1929 return result;
1930}
1931
1932VKAPI_ATTR
1933VkResult GetPastPresentationTimingGOOGLE(
1934 VkDevice,
1935 VkSwapchainKHR swapchain_handle,
1936 uint32_t* count,
1937 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001938 ATRACE_CALL();
1939
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001940 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001941 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1942 return VK_ERROR_OUT_OF_DATE_KHR;
1943 }
1944
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001945 ANativeWindow* window = swapchain.surface.window.get();
1946 VkResult result = VK_SUCCESS;
1947
1948 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001949 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001950 native_window_enable_frame_timestamps(window, true);
1951 swapchain.frame_timestamps_enabled = true;
1952 }
1953
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001954 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07001955 // Get the latest ready timing count before copying, since the copied
1956 // timing info will be erased in copy_ready_timings function.
1957 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07001958 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001959 // Check the *count here against the recorded ready timing count, since
1960 // *count can be overwritten per spec describes.
1961 if (*count < n) {
1962 result = VK_INCOMPLETE;
1963 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001964 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001965 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001966 }
1967
1968 return result;
1969}
1970
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001971VKAPI_ATTR
1972VkResult GetSwapchainStatusKHR(
1973 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001974 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001975 ATRACE_CALL();
1976
Chris Forbes4e18ba82017-01-20 12:50:17 +13001977 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001978 VkResult result = VK_SUCCESS;
1979
Chris Forbes4e18ba82017-01-20 12:50:17 +13001980 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1981 return VK_ERROR_OUT_OF_DATE_KHR;
1982 }
1983
Yiwei Zhanga885c062019-10-24 12:07:57 -07001984 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001985
1986 return result;
1987}
1988
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001989VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001990 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001991 uint32_t swapchainCount,
1992 const VkSwapchainKHR* pSwapchains,
1993 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001994 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001995
1996 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1997 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1998 if (!swapchain)
1999 continue;
2000
2001 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
2002
2003 ANativeWindow* window = swapchain->surface.window.get();
2004
2005 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
2006 const android_smpte2086_metadata smpteMetdata = {
2007 {vulkanMetadata.displayPrimaryRed.x,
2008 vulkanMetadata.displayPrimaryRed.y},
2009 {vulkanMetadata.displayPrimaryGreen.x,
2010 vulkanMetadata.displayPrimaryGreen.y},
2011 {vulkanMetadata.displayPrimaryBlue.x,
2012 vulkanMetadata.displayPrimaryBlue.y},
2013 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
2014 vulkanMetadata.maxLuminance,
2015 vulkanMetadata.minLuminance};
2016 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
2017
2018 const android_cta861_3_metadata cta8613Metadata = {
2019 vulkanMetadata.maxContentLightLevel,
2020 vulkanMetadata.maxFrameAverageLightLevel};
2021 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
2022 }
2023
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07002024 return;
2025}
2026
Yiwei Zhang0f475222019-04-11 19:38:00 -07002027static void InterceptBindImageMemory2(
2028 uint32_t bind_info_count,
2029 const VkBindImageMemoryInfo* bind_infos,
2030 std::vector<VkNativeBufferANDROID>* out_native_buffers,
2031 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
2032 out_native_buffers->clear();
2033 out_bind_infos->clear();
2034
2035 if (!bind_info_count)
2036 return;
2037
2038 std::unordered_set<uint32_t> intercepted_indexes;
2039
2040 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2041 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2042 bind_infos[idx].pNext);
2043 while (info &&
2044 info->sType !=
2045 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
2046 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
2047 info->pNext);
2048 }
2049
2050 if (!info)
2051 continue;
2052
2053 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
2054 "swapchain handle must not be NULL");
2055 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
2056 ALOG_ASSERT(
2057 info->imageIndex < swapchain->num_images,
2058 "imageIndex must be less than the number of images in swapchain");
2059
2060 ANativeWindowBuffer* buffer =
2061 swapchain->images[info->imageIndex].buffer.get();
2062 VkNativeBufferANDROID native_buffer = {
2063#pragma clang diagnostic push
2064#pragma clang diagnostic ignored "-Wold-style-cast"
2065 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2066#pragma clang diagnostic pop
2067 .pNext = bind_infos[idx].pNext,
2068 .handle = buffer->handle,
2069 .stride = buffer->stride,
2070 .format = buffer->format,
2071 .usage = int(buffer->usage),
2072 };
2073 // Reserve enough space to avoid letting re-allocation invalidate the
2074 // addresses of the elements inside.
2075 out_native_buffers->reserve(bind_info_count);
2076 out_native_buffers->emplace_back(native_buffer);
2077
2078 // Reserve the space now since we know how much is needed now.
2079 out_bind_infos->reserve(bind_info_count);
2080 out_bind_infos->emplace_back(bind_infos[idx]);
2081 out_bind_infos->back().pNext = &out_native_buffers->back();
2082
2083 intercepted_indexes.insert(idx);
2084 }
2085
2086 if (intercepted_indexes.empty())
2087 return;
2088
2089 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2090 if (intercepted_indexes.count(idx))
2091 continue;
2092 out_bind_infos->emplace_back(bind_infos[idx]);
2093 }
2094}
2095
Yiwei Zhang23143102019-04-10 18:24:05 -07002096VKAPI_ATTR
2097VkResult BindImageMemory2(VkDevice device,
2098 uint32_t bindInfoCount,
2099 const VkBindImageMemoryInfo* pBindInfos) {
2100 ATRACE_CALL();
2101
Yiwei Zhang0f475222019-04-11 19:38:00 -07002102 // out_native_buffers is for maintaining the lifecycle of the constructed
2103 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2104 std::vector<VkNativeBufferANDROID> out_native_buffers;
2105 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2106 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2107 &out_bind_infos);
2108 return GetData(device).driver.BindImageMemory2(
2109 device, bindInfoCount,
2110 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002111}
2112
2113VKAPI_ATTR
2114VkResult BindImageMemory2KHR(VkDevice device,
2115 uint32_t bindInfoCount,
2116 const VkBindImageMemoryInfo* pBindInfos) {
2117 ATRACE_CALL();
2118
Yiwei Zhang0f475222019-04-11 19:38:00 -07002119 std::vector<VkNativeBufferANDROID> out_native_buffers;
2120 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2121 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2122 &out_bind_infos);
2123 return GetData(device).driver.BindImageMemory2KHR(
2124 device, bindInfoCount,
2125 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002126}
2127
Chia-I Wu62262232016-03-26 07:06:44 +08002128} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002129} // namespace vulkan