blob: ec673123b00a4c5e4225c637bc7dc0bbe017073e [file] [log] [blame]
Jesse Hallb1352bc2015-09-04 16:12:33 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang0f475222019-04-11 19:38:00 -070019#include <android/hardware/graphics/common/1.0/types.h>
Jesse Hall79927812017-03-23 11:03:23 -070020#include <grallocusage/GrallocUsageConversion.h>
Yiwei Zhang69395cd2019-07-03 16:55:39 -070021#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070023#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070024#include <system/window.h>
25#include <ui/BufferQueueDefs.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080026#include <utils/StrongPointer.h>
Yiwei Zhang705c2e62019-12-18 23:12:43 -080027#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080028#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070029
30#include <algorithm>
31#include <unordered_set>
32#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070033
Chia-I Wu4a6a9162016-03-26 07:17:34 +080034#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070035
Daniel Kochf25f5bb2017-10-05 00:26:58 -040036using android::hardware::graphics::common::V1_0::BufferUsage;
37
Chia-I Wu62262232016-03-26 07:06:44 +080038namespace vulkan {
39namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070040
Jesse Halld7b994a2015-09-07 14:17:37 -070041namespace {
42
Jesse Hall55bc0972016-02-23 16:43:29 -080043const VkSurfaceTransformFlagsKHR kSupportedTransforms =
44 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
45 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
46 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
47 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070048 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
49 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
50 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
51 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080052 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
53
54VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
55 // Native and Vulkan transforms are isomorphic, but are represented
56 // differently. Vulkan transforms are built up of an optional horizontal
57 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
58 // transforms are built up from a horizontal flip, vertical flip, and
59 // 90-degree rotation, all optional but always in that order.
60
Jesse Hall55bc0972016-02-23 16:43:29 -080061 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070062 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080063 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070064 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
65 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
66 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
67 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
68 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080069 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070070 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080071 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070072 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
73 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
74 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
75 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
76 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -080077 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
78 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
79 default:
80 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
81 }
82}
83
Yiwei Zhang70a21962019-05-31 17:26:52 -070084int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
85 switch (transform) {
86 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
87 return NATIVE_WINDOW_TRANSFORM_ROT_90;
88 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
89 return NATIVE_WINDOW_TRANSFORM_ROT_180;
90 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
91 return NATIVE_WINDOW_TRANSFORM_ROT_270;
92 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
93 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
94 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
95 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
96 NATIVE_WINDOW_TRANSFORM_ROT_90;
97 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
98 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
99 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
100 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
101 NATIVE_WINDOW_TRANSFORM_ROT_90;
102 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
103 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
104 default:
105 return 0;
106 }
107}
108
Jesse Hall178b6962016-02-24 15:39:50 -0800109int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
110 switch (transform) {
111 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
112 return NATIVE_WINDOW_TRANSFORM_ROT_270;
113 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
114 return NATIVE_WINDOW_TRANSFORM_ROT_180;
115 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
116 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700117 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
118 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
119 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
120 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
121 NATIVE_WINDOW_TRANSFORM_ROT_90;
122 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
123 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
124 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
125 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
126 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800127 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
128 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
129 default:
130 return 0;
131 }
132}
133
Ian Elliott8a977262017-01-19 09:05:58 -0700134class TimingInfo {
135 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800136 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700137 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800138 native_frame_id_(nativeFrameId) {}
139 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700140 return (timestamp_desired_present_time_ !=
141 NATIVE_WINDOW_TIMESTAMP_PENDING &&
142 timestamp_actual_present_time_ !=
143 NATIVE_WINDOW_TIMESTAMP_PENDING &&
144 timestamp_render_complete_time_ !=
145 NATIVE_WINDOW_TIMESTAMP_PENDING &&
146 timestamp_composition_latch_time_ !=
147 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700148 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700149 void calculate(int64_t rdur) {
150 bool anyTimestampInvalid =
151 (timestamp_actual_present_time_ ==
152 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
153 (timestamp_render_complete_time_ ==
154 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
155 (timestamp_composition_latch_time_ ==
156 NATIVE_WINDOW_TIMESTAMP_INVALID);
157 if (anyTimestampInvalid) {
158 ALOGE("Unexpectedly received invalid timestamp.");
159 vals_.actualPresentTime = 0;
160 vals_.earliestPresentTime = 0;
161 vals_.presentMargin = 0;
162 return;
163 }
164
165 vals_.actualPresentTime =
166 static_cast<uint64_t>(timestamp_actual_present_time_);
167 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700168 timestamp_render_complete_time_);
169 // Calculate vals_.earliestPresentTime, and potentially adjust
170 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
171 // is vals_.actualPresentTime. If we can subtract rdur (the duration
172 // of a refresh cycle) from vals_.earliestPresentTime (and also from
173 // vals_.presentMargin) and still leave a positive margin, then we can
174 // report to the application that it could have presented earlier than
175 // it did (per the extension specification). If for some reason, we
176 // can do this subtraction repeatedly, we do, since
177 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700178 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700179 while ((margin > rdur) &&
180 ((early_time - rdur) > timestamp_composition_latch_time_)) {
181 early_time -= rdur;
182 margin -= rdur;
183 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700184 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
185 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700186 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800187 void get_values(VkPastPresentationTimingGOOGLE* values) const {
188 *values = vals_;
189 }
Ian Elliott8a977262017-01-19 09:05:58 -0700190
191 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800192 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700193
Brian Anderson1049d1d2016-12-16 17:25:57 -0800194 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700195 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
196 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
197 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
198 int64_t timestamp_composition_latch_time_
199 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700200};
201
Jesse Hall1356b0d2015-11-23 17:24:58 -0800202struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800203 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700204 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700205 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800206};
207
208VkSurfaceKHR HandleFromSurface(Surface* surface) {
209 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
210}
211
212Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800213 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800214}
215
Ian Elliott8a977262017-01-19 09:05:58 -0700216// Maximum number of TimingInfo structs to keep per swapchain:
217enum { MAX_TIMING_INFOS = 10 };
218// Minimum number of frames to look for in the past (so we don't cause
219// syncronous requests to Surface Flinger):
220enum { MIN_NUM_FRAMES_AGO = 5 };
221
Jesse Hall1356b0d2015-11-23 17:24:58 -0800222struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700223 Swapchain(Surface& surface_,
224 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700225 VkPresentModeKHR present_mode,
226 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700227 : surface(surface_),
228 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700229 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700230 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300231 frame_timestamps_enabled(false),
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800232 acquire_next_image_timeout(-1),
Chris Forbesf8835642017-03-30 19:31:40 +1300233 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800234 present_mode ==
235 VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700236 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700237 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700238 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700239 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700240 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600241 uint64_t get_refresh_duration()
242 {
243 ANativeWindow* window = surface.window.get();
244 native_window_get_refresh_cycle_duration(
245 window,
246 &refresh_duration);
247 return static_cast<uint64_t>(refresh_duration);
248
249 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800250
251 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700252 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700253 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700254 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700255 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700256 int64_t refresh_duration;
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800257 nsecs_t acquire_next_image_timeout;
Chris Forbesf8835642017-03-30 19:31:40 +1300258 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700259
260 struct Image {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700261 Image()
262 : image(VK_NULL_HANDLE),
263 dequeue_fence(-1),
264 release_fence(-1),
265 dequeued(false) {}
Jesse Halld7b994a2015-09-07 14:17:37 -0700266 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800267 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700268 // The fence is only valid when the buffer is dequeued, and should be
269 // -1 any other time. When valid, we own the fd, and must ensure it is
270 // closed: either by closing it explicitly when queueing the buffer,
271 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
272 int dequeue_fence;
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700273 // This fence is a dup of the sync fd returned from the driver via
274 // vkQueueSignalReleaseImageANDROID upon vkQueuePresentKHR. We must
275 // ensure it is closed upon re-presenting or releasing the image.
276 int release_fence;
Jesse Halld7b994a2015-09-07 14:17:37 -0700277 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800278 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700279
Yiwei Zhang5e862202019-06-21 14:59:16 -0700280 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700281};
282
283VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
284 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
285}
286
287Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800288 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700289}
290
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700291static bool IsFencePending(int fd) {
292 if (fd < 0)
293 return false;
294
295 errno = 0;
296 return sync_wait(fd, 0 /* timeout */) == -1 && errno == ETIME;
297}
298
Jesse Halldc225072016-05-30 22:40:14 -0700299void ReleaseSwapchainImage(VkDevice device,
300 ANativeWindow* window,
301 int release_fence,
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700302 Swapchain::Image& image,
303 bool defer_if_pending) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700304 ATRACE_CALL();
305
Jesse Halldc225072016-05-30 22:40:14 -0700306 ALOG_ASSERT(release_fence == -1 || image.dequeued,
307 "ReleaseSwapchainImage: can't provide a release fence for "
308 "non-dequeued images");
309
310 if (image.dequeued) {
311 if (release_fence >= 0) {
312 // We get here from vkQueuePresentKHR. The application is
313 // responsible for creating an execution dependency chain from
314 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
315 // (release_fence), so we can drop the dequeue_fence here.
316 if (image.dequeue_fence >= 0)
317 close(image.dequeue_fence);
318 } else {
319 // We get here during swapchain destruction, or various serious
320 // error cases e.g. when we can't create the release_fence during
321 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
322 // have already signalled, since the swapchain images are supposed
323 // to be idle before the swapchain is destroyed. In error cases,
324 // there may be rendering in flight to the image, but since we
325 // weren't able to create a release_fence, waiting for the
326 // dequeue_fence is about the best we can do.
327 release_fence = image.dequeue_fence;
328 }
329 image.dequeue_fence = -1;
330
331 if (window) {
332 window->cancelBuffer(window, image.buffer.get(), release_fence);
333 } else {
334 if (release_fence >= 0) {
335 sync_wait(release_fence, -1 /* forever */);
336 close(release_fence);
337 }
338 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700339 release_fence = -1;
Jesse Halldc225072016-05-30 22:40:14 -0700340 image.dequeued = false;
341 }
342
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700343 if (defer_if_pending && IsFencePending(image.release_fence))
344 return;
345
346 if (image.release_fence >= 0) {
347 close(image.release_fence);
348 image.release_fence = -1;
349 }
350
Jesse Halldc225072016-05-30 22:40:14 -0700351 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700352 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700353 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700354 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700355 image.image = VK_NULL_HANDLE;
356 }
357
358 image.buffer.clear();
359}
360
361void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
362 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
363 return;
Jesse Halldc225072016-05-30 22:40:14 -0700364 for (uint32_t i = 0; i < swapchain->num_images; i++) {
365 if (!swapchain->images[i].dequeued)
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700366 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i],
367 true);
Jesse Halldc225072016-05-30 22:40:14 -0700368 }
369 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700370 swapchain->timing.clear();
371}
372
373uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800374 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
375 return 0;
376 }
Ian Elliott8a977262017-01-19 09:05:58 -0700377
Brian Anderson1049d1d2016-12-16 17:25:57 -0800378 uint32_t num_ready = 0;
379 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
380 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700381 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800382 if (ti.ready()) {
383 // This TimingInfo is ready to be reported to the user. Add it
384 // to the num_ready.
385 num_ready++;
386 continue;
387 }
388 // This TimingInfo is not yet ready to be reported to the user,
389 // and so we should look for any available timestamps that
390 // might make it ready.
391 int64_t desired_present_time = 0;
392 int64_t render_complete_time = 0;
393 int64_t composition_latch_time = 0;
394 int64_t actual_present_time = 0;
395 // Obtain timestamps:
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800396 int err = native_window_get_frame_timestamps(
Brian Anderson1049d1d2016-12-16 17:25:57 -0800397 swapchain.surface.window.get(), ti.native_frame_id_,
398 &desired_present_time, &render_complete_time,
399 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700400 nullptr, //&first_composition_start_time,
401 nullptr, //&last_composition_start_time,
402 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800403 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700404 nullptr, //&dequeue_ready_time,
405 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800406
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800407 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800408 continue;
409 }
410
411 // Record the timestamp(s) we received, and then see if this TimingInfo
412 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700413 ti.timestamp_desired_present_time_ = desired_present_time;
414 ti.timestamp_actual_present_time_ = actual_present_time;
415 ti.timestamp_render_complete_time_ = render_complete_time;
416 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800417
418 if (ti.ready()) {
419 // The TimingInfo has received enough timestamps, and should now
420 // use those timestamps to calculate the info that should be
421 // reported to the user:
422 ti.calculate(swapchain.refresh_duration);
423 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700424 }
425 }
426 return num_ready;
427}
428
Ian Elliott8a977262017-01-19 09:05:58 -0700429void copy_ready_timings(Swapchain& swapchain,
430 uint32_t* count,
431 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800432 if (swapchain.timing.empty()) {
433 *count = 0;
434 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700435 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800436
437 size_t last_ready = swapchain.timing.size() - 1;
438 while (!swapchain.timing[last_ready].ready()) {
439 if (last_ready == 0) {
440 *count = 0;
441 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700442 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800443 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700444 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800445
446 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700447 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800448 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
449 const TimingInfo& ti = swapchain.timing[i];
450 if (ti.ready()) {
451 ti.get_values(&timings[num_copied]);
452 num_copied++;
453 }
454 num_to_remove++;
455 }
456
457 // Discard old frames that aren't ready if newer frames are ready.
458 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700459 swapchain.timing.erase(swapchain.timing.begin(),
460 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800461
Ian Elliott8a977262017-01-19 09:05:58 -0700462 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700463}
464
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700465android_pixel_format GetNativePixelFormat(VkFormat format) {
466 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
467 switch (format) {
468 case VK_FORMAT_R8G8B8A8_UNORM:
469 case VK_FORMAT_R8G8B8A8_SRGB:
470 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
471 break;
472 case VK_FORMAT_R5G6B5_UNORM_PACK16:
473 native_format = HAL_PIXEL_FORMAT_RGB_565;
474 break;
475 case VK_FORMAT_R16G16B16A16_SFLOAT:
476 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
477 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800478 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700479 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
480 break;
481 default:
482 ALOGV("unsupported swapchain format %d", format);
483 break;
484 }
485 return native_format;
486}
487
488android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
489 switch (colorspace) {
490 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
491 return HAL_DATASPACE_V0_SRGB;
492 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
493 return HAL_DATASPACE_DISPLAY_P3;
494 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
495 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600496 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
497 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700498 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
499 return HAL_DATASPACE_DCI_P3_LINEAR;
500 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
501 return HAL_DATASPACE_DCI_P3;
502 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
503 return HAL_DATASPACE_V0_SRGB_LINEAR;
504 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
505 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600506 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
507 return HAL_DATASPACE_BT2020_LINEAR;
508 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700509 return static_cast<android_dataspace>(
510 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
511 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600512 case VK_COLOR_SPACE_DOLBYVISION_EXT:
513 return static_cast<android_dataspace>(
514 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
515 HAL_DATASPACE_RANGE_FULL);
516 case VK_COLOR_SPACE_HDR10_HLG_EXT:
517 return static_cast<android_dataspace>(
518 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
519 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700520 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
521 return static_cast<android_dataspace>(
522 HAL_DATASPACE_STANDARD_ADOBE_RGB |
523 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
524 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
525 return HAL_DATASPACE_ADOBE_RGB;
526
527 // Pass through is intended to allow app to provide data that is passed
528 // to the display system without modification.
529 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
530 return HAL_DATASPACE_ARBITRARY;
531
532 default:
533 // This indicates that we don't know about the
534 // dataspace specified and we should indicate that
535 // it's unsupported
536 return HAL_DATASPACE_UNKNOWN;
537 }
538}
539
Jesse Halld7b994a2015-09-07 14:17:37 -0700540} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700541
Jesse Halle1b12782015-11-30 11:27:32 -0800542VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800543VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800544 VkInstance instance,
545 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
546 const VkAllocationCallbacks* allocator,
547 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800548 ATRACE_CALL();
549
Jesse Hall1f91d392015-12-11 16:28:44 -0800550 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800551 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800552 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
553 alignof(Surface),
554 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800555 if (!mem)
556 return VK_ERROR_OUT_OF_HOST_MEMORY;
557 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700558
Chia-I Wue8e689f2016-04-18 08:21:31 +0800559 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700560 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700561 int err = native_window_get_consumer_usage(surface->window.get(),
562 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800563 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700564 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
565 strerror(-err), err);
566 surface->~Surface();
567 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700568 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700569 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700570
Yiwei Zhang6435b322018-05-08 11:12:17 -0700571 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800572 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800573 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800574 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
575 err);
576 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800577 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700578 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800579 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700580
Jesse Hall1356b0d2015-11-23 17:24:58 -0800581 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700582 return VK_SUCCESS;
583}
584
Jesse Halle1b12782015-11-30 11:27:32 -0800585VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800586void DestroySurfaceKHR(VkInstance instance,
587 VkSurfaceKHR surface_handle,
588 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800589 ATRACE_CALL();
590
Jesse Hall1356b0d2015-11-23 17:24:58 -0800591 Surface* surface = SurfaceFromHandle(surface_handle);
592 if (!surface)
593 return;
594 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700595 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700596 "destroyed VkSurfaceKHR 0x%" PRIx64
597 " has active VkSwapchainKHR 0x%" PRIx64,
598 reinterpret_cast<uint64_t>(surface_handle),
599 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800600 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800601 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800602 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800603 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800604}
605
Jesse Halle1b12782015-11-30 11:27:32 -0800606VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800607VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
608 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000609 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800610 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000611 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800612 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800613}
614
Jesse Halle1b12782015-11-30 11:27:32 -0800615VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800616VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Ian Elliott2f7f0ff2022-02-18 16:44:58 -0700617 VkPhysicalDevice pdev,
Jesse Hallb00daad2015-11-29 19:46:20 -0800618 VkSurfaceKHR surface,
619 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800620 ATRACE_CALL();
621
Jesse Halld7b994a2015-09-07 14:17:37 -0700622 int err;
Jesse Halld7b994a2015-09-07 14:17:37 -0700623 int width, height;
Jesse Hall55bc0972016-02-23 16:43:29 -0800624 int transform_hint;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800625 int max_buffer_count;
Ian Elliott2f7f0ff2022-02-18 16:44:58 -0700626 if (surface == VK_NULL_HANDLE) {
627 const InstanceData& instance_data = GetData(pdev);
628 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
629 bool surfaceless_enabled =
630 instance_data.hook_extensions.test(surfaceless);
631 if (!surfaceless_enabled) {
632 // It is an error to pass a surface==VK_NULL_HANDLE unless the
633 // VK_GOOGLE_surfaceless_query extension is enabled
634 return VK_ERROR_SURFACE_LOST_KHR;
635 }
636 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
637 // extension for this function is for
638 // VkSurfaceProtectedCapabilitiesKHR::supportsProtected. The following
639 // four values cannot be known without a surface. Default values will
640 // be supplied anyway, but cannot be relied upon.
641 width = 1000;
642 height = 1000;
643 transform_hint = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
644 max_buffer_count = 10;
645 } else {
646 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
647
648 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
649 if (err != android::OK) {
650 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
651 strerror(-err), err);
652 return VK_ERROR_SURFACE_LOST_KHR;
653 }
654 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
655 if (err != android::OK) {
656 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
657 strerror(-err), err);
658 return VK_ERROR_SURFACE_LOST_KHR;
659 }
660
661 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
662 &transform_hint);
663 if (err != android::OK) {
664 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
665 strerror(-err), err);
666 return VK_ERROR_SURFACE_LOST_KHR;
667 }
668
669 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT,
670 &max_buffer_count);
671 if (err != android::OK) {
672 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
673 strerror(-err), err);
674 return VK_ERROR_SURFACE_LOST_KHR;
675 }
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800676 }
Ian Elliottf5737982021-12-01 12:35:57 -0700677 capabilities->minImageCount = std::min(max_buffer_count, 3);
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800678 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700679
Jesse Hallfe2662d2016-02-09 13:26:59 -0800680 capabilities->currentExtent =
681 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
682
Yiwei Zhang70a21962019-05-31 17:26:52 -0700683 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800684 capabilities->minImageExtent = VkExtent2D{1, 1};
685 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700686
Jesse Hallfe2662d2016-02-09 13:26:59 -0800687 capabilities->maxImageArrayLayers = 1;
688
Jesse Hall55bc0972016-02-23 16:43:29 -0800689 capabilities->supportedTransforms = kSupportedTransforms;
690 capabilities->currentTransform =
691 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700692
Jesse Hallfe2662d2016-02-09 13:26:59 -0800693 // On Android, window composition is a WindowManager property, not something
694 // associated with the bufferqueue. It can't be changed from here.
695 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700696
Jesse Hallb00daad2015-11-29 19:46:20 -0800697 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800698 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
699 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
700 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700701 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
702
Jesse Hallb1352bc2015-09-04 16:12:33 -0700703 return VK_SUCCESS;
704}
705
Jesse Halle1b12782015-11-30 11:27:32 -0800706VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700707VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
708 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800709 uint32_t* count,
710 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800711 ATRACE_CALL();
712
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700713 const InstanceData& instance_data = GetData(pdev);
714
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700715 bool wide_color_support = false;
Ian Elliott2f7f0ff2022-02-18 16:44:58 -0700716 uint64_t consumer_usage = 0;
717 bool swapchain_ext =
Shreshta Manu6acd8e82022-03-05 00:58:43 +0000718 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
Ian Elliott2f7f0ff2022-02-18 16:44:58 -0700719 if (surface_handle == VK_NULL_HANDLE) {
720 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
721 bool surfaceless_enabled =
722 instance_data.hook_extensions.test(surfaceless);
723 if (!surfaceless_enabled) {
724 return VK_ERROR_SURFACE_LOST_KHR;
725 }
726 // Support for VK_GOOGLE_surfaceless_query. The EGL loader
727 // unconditionally supports wide color formats, even if they will cause
728 // a SurfaceFlinger fallback. Based on that, wide_color_support will be
729 // set to true in this case.
730 wide_color_support = true;
731
732 // TODO(b/203826952): research proper value; temporarily use the
733 // values seen on Pixel
734 consumer_usage = AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
735 } else {
736 Surface& surface = *SurfaceFromHandle(surface_handle);
737 int err = native_window_get_wide_color_support(surface.window.get(),
738 &wide_color_support);
739 if (err) {
740 return VK_ERROR_SURFACE_LOST_KHR;
741 }
742 ALOGV("wide_color_support is: %d", wide_color_support);
743
744 consumer_usage = surface.consumer_usage;
745 }
746 wide_color_support = wide_color_support && swapchain_ext;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700747
Charlie Lao324191f2019-12-13 11:03:16 -0800748 AHardwareBuffer_Desc desc = {};
749 desc.width = 1;
750 desc.height = 1;
751 desc.layers = 1;
Ian Elliott2f7f0ff2022-02-18 16:44:58 -0700752 desc.usage = consumer_usage | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
Charlie Lao324191f2019-12-13 11:03:16 -0800753 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
754
755 // We must support R8G8B8A8
756 std::vector<VkSurfaceFormatKHR> all_formats = {
757 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
758 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}};
759
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700760 if (wide_color_support) {
Charlie Lao324191f2019-12-13 11:03:16 -0800761 all_formats.emplace_back(VkSurfaceFormatKHR{
762 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
763 all_formats.emplace_back(VkSurfaceFormatKHR{
764 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
765 }
766
Ian Elliott2f7f0ff2022-02-18 16:44:58 -0700767 // NOTE: Any new formats that are added must be coordinated across different
768 // Android users. This includes the ANGLE team (a layered implementation of
769 // OpenGL-ES).
770
Charlie Lao324191f2019-12-13 11:03:16 -0800771 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
772 if (AHardwareBuffer_isSupported(&desc)) {
773 all_formats.emplace_back(VkSurfaceFormatKHR{
774 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
775 }
776
777 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
778 if (AHardwareBuffer_isSupported(&desc)) {
779 all_formats.emplace_back(VkSurfaceFormatKHR{
780 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
781 if (wide_color_support) {
782 all_formats.emplace_back(
783 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
784 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
785 all_formats.emplace_back(
786 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
787 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
788 }
789 }
790
791 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
792 if (AHardwareBuffer_isSupported(&desc)) {
793 all_formats.emplace_back(
794 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
795 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
796 if (wide_color_support) {
797 all_formats.emplace_back(
798 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
799 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
800 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700801 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700802
Ian Elliott2f7f0ff2022-02-18 16:44:58 -0700803 // NOTE: Any new formats that are added must be coordinated across different
804 // Android users. This includes the ANGLE team (a layered implementation of
805 // OpenGL-ES).
806
Jesse Halld7b994a2015-09-07 14:17:37 -0700807 VkResult result = VK_SUCCESS;
808 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800809 uint32_t transfer_count = all_formats.size();
810 if (transfer_count > *count) {
811 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700812 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700813 }
Charlie Lao324191f2019-12-13 11:03:16 -0800814 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
815 formats);
816 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700817 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800818 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700819 }
Charlie Lao324191f2019-12-13 11:03:16 -0800820
Jesse Halld7b994a2015-09-07 14:17:37 -0700821 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700822}
823
Jesse Halle1b12782015-11-30 11:27:32 -0800824VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300825VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
826 VkPhysicalDevice physicalDevice,
827 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
828 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800829 ATRACE_CALL();
830
Chris Forbes2452cf72017-03-16 16:30:17 +1300831 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
832 physicalDevice, pSurfaceInfo->surface,
833 &pSurfaceCapabilities->surfaceCapabilities);
834
Chris Forbes06bc0092017-03-16 16:46:05 +1300835 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
836 while (caps->pNext) {
837 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
838
839 switch (caps->sType) {
840 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
841 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
842 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
843 caps);
844 // Claim same set of usage flags are supported for
845 // shared present modes as for other modes.
846 shared_caps->sharedPresentSupportedUsageFlags =
847 pSurfaceCapabilities->surfaceCapabilities
848 .supportedUsageFlags;
849 } break;
850
Ian Elliott6fcbd072022-02-28 16:47:43 -0700851 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
852 VkSurfaceProtectedCapabilitiesKHR* protected_caps =
853 reinterpret_cast<VkSurfaceProtectedCapabilitiesKHR*>(caps);
854 protected_caps->supportsProtected = VK_TRUE;
855 } break;
856
Chris Forbes06bc0092017-03-16 16:46:05 +1300857 default:
858 // Ignore all other extension structs
859 break;
860 }
861 }
862
Chris Forbes2452cf72017-03-16 16:30:17 +1300863 return result;
864}
865
866VKAPI_ATTR
867VkResult GetPhysicalDeviceSurfaceFormats2KHR(
868 VkPhysicalDevice physicalDevice,
869 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
870 uint32_t* pSurfaceFormatCount,
871 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800872 ATRACE_CALL();
873
Chris Forbes2452cf72017-03-16 16:30:17 +1300874 if (!pSurfaceFormats) {
875 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
876 pSurfaceInfo->surface,
877 pSurfaceFormatCount, nullptr);
878 } else {
879 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
880 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700881 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300882 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
883 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700884 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300885
886 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
887 // marshal results individually due to stride difference.
888 // completely ignore any chained extension structs.
889 uint32_t formats_to_marshal = *pSurfaceFormatCount;
890 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
891 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
892 }
893 }
894
895 return result;
896 }
897}
898
899VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300900VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800901 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800902 uint32_t* count,
903 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800904 ATRACE_CALL();
905
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800906 int err;
907 int query_value;
Shreshta Manu6acd8e82022-03-05 00:58:43 +0000908 std::vector<VkPresentModeKHR> present_modes;
Ian Elliott2f7f0ff2022-02-18 16:44:58 -0700909 if (surface == VK_NULL_HANDLE) {
910 const InstanceData& instance_data = GetData(pdev);
911 ProcHook::Extension surfaceless = ProcHook::GOOGLE_surfaceless_query;
912 bool surfaceless_enabled =
913 instance_data.hook_extensions.test(surfaceless);
914 if (!surfaceless_enabled) {
915 return VK_ERROR_SURFACE_LOST_KHR;
916 }
917 // Support for VK_GOOGLE_surfaceless_query. The primary purpose of this
918 // extension for this function is for
919 // VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR and
920 // VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR. We technically cannot
921 // know if VK_PRESENT_MODE_SHARED_MAILBOX_KHR is supported without a
922 // surface, and that cannot be relied upon.
Shreshta Manu6acd8e82022-03-05 00:58:43 +0000923 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Ian Elliott2f7f0ff2022-02-18 16:44:58 -0700924 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
925 } else {
926 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
927
928 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
929 &query_value);
930 if (err != android::OK || query_value < 0) {
931 ALOGE(
932 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
933 "value=%d",
934 strerror(-err), err, query_value);
935 return VK_ERROR_SURFACE_LOST_KHR;
936 }
937 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
938
939 err =
940 window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
941 if (err != android::OK || query_value < 0) {
942 ALOGE(
943 "NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
944 strerror(-err), err, query_value);
945 return VK_ERROR_SURFACE_LOST_KHR;
946 }
947 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
948
949 if (min_undequeued_buffers + 1 < max_buffer_count)
950 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
951 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
952 }
Chris Forbese8d79a62017-02-22 12:49:18 +1300953
954 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700955 QueryPresentationProperties(pdev, &present_properties);
956 if (present_properties.sharedImage) {
957 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
958 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300959 }
960
961 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700962
963 VkResult result = VK_SUCCESS;
964 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300965 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700966 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300967 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -0700968 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700969 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300970 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700971 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700972 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700973}
974
Jesse Halle1b12782015-11-30 11:27:32 -0800975VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400976VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600977 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400978 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800979 ATRACE_CALL();
980
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400981 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
982 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
983 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
984 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
985 pDeviceGroupPresentCapabilities->sType);
986
987 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
988 sizeof(pDeviceGroupPresentCapabilities->presentMask));
989
990 // assume device group of size 1
991 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
992 pDeviceGroupPresentCapabilities->modes =
993 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
994
995 return VK_SUCCESS;
996}
997
998VKAPI_ATTR
999VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -06001000 VkDevice,
1001 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001002 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001003 ATRACE_CALL();
1004
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001005 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
1006 return VK_SUCCESS;
1007}
1008
1009VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -06001010VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001011 VkSurfaceKHR surface,
1012 uint32_t* pRectCount,
1013 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001014 ATRACE_CALL();
1015
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001016 if (!pRects) {
1017 *pRectCount = 1;
1018 } else {
1019 uint32_t count = std::min(*pRectCount, 1u);
1020 bool incomplete = *pRectCount < 1;
1021
1022 *pRectCount = count;
1023
1024 if (incomplete) {
1025 return VK_INCOMPLETE;
1026 }
1027
1028 int err;
1029 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
1030
1031 int width = 0, height = 0;
1032 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001033 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001034 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1035 strerror(-err), err);
1036 }
1037 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001038 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001039 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
1040 strerror(-err), err);
1041 }
1042
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001043 pRects[0].offset.x = 0;
1044 pRects[0].offset.y = 0;
1045 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1046 static_cast<uint32_t>(height)};
1047 }
1048 return VK_SUCCESS;
1049}
1050
Yiwei Zhang533cea92019-06-03 18:43:24 -07001051static void DestroySwapchainInternal(VkDevice device,
1052 VkSwapchainKHR swapchain_handle,
1053 const VkAllocationCallbacks* allocator) {
1054 ATRACE_CALL();
1055
1056 const auto& dispatch = GetData(device).driver;
1057 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1058 if (!swapchain) {
1059 return;
1060 }
1061
1062 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1063 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1064
1065 if (window && swapchain->frame_timestamps_enabled) {
1066 native_window_enable_frame_timestamps(window, false);
1067 }
1068
1069 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001070 ReleaseSwapchainImage(device, window, -1, swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001071 }
1072
1073 if (active) {
1074 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1075 }
1076
1077 if (!allocator) {
1078 allocator = &GetData(device).allocator;
1079 }
1080
1081 swapchain->~Swapchain();
1082 allocator->pfnFree(allocator->pUserData, swapchain);
1083}
1084
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001085VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001086VkResult CreateSwapchainKHR(VkDevice device,
1087 const VkSwapchainCreateInfoKHR* create_info,
1088 const VkAllocationCallbacks* allocator,
1089 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001090 ATRACE_CALL();
1091
Jesse Halld7b994a2015-09-07 14:17:37 -07001092 int err;
1093 VkResult result = VK_SUCCESS;
1094
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001095 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1096 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1097 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1098 " oldSwapchain=0x%" PRIx64,
1099 reinterpret_cast<uint64_t>(create_info->surface),
1100 create_info->minImageCount, create_info->imageFormat,
1101 create_info->imageColorSpace, create_info->imageExtent.width,
1102 create_info->imageExtent.height, create_info->imageUsage,
1103 create_info->preTransform, create_info->presentMode,
1104 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1105
Jesse Hall1f91d392015-12-11 16:28:44 -08001106 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001107 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001108
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001109 android_pixel_format native_pixel_format =
1110 GetNativePixelFormat(create_info->imageFormat);
1111 android_dataspace native_dataspace =
1112 GetNativeDataspace(create_info->imageColorSpace);
1113 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1114 ALOGE(
1115 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1116 "failed: Unsupported color space",
1117 create_info->imageColorSpace);
1118 return VK_ERROR_INITIALIZATION_FAILED;
1119 }
1120
Jesse Hall42a9eec2016-06-03 12:39:49 -07001121 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001122 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001123 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001124 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001125 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001126 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001127 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001128 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001129 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1130 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001131 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001132 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001133
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001134 Surface& surface = *SurfaceFromHandle(create_info->surface);
1135
Jesse Halldc225072016-05-30 22:40:14 -07001136 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001137 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001138 " because it already has active swapchain 0x%" PRIx64
1139 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1140 reinterpret_cast<uint64_t>(create_info->surface),
1141 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1142 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1143 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1144 }
1145 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1146 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1147
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001148 // -- Reset the native window --
1149 // The native window might have been used previously, and had its properties
1150 // changed from defaults. That will affect the answer we get for queries
1151 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1152 // attempt such queries.
1153
Jesse Halldc225072016-05-30 22:40:14 -07001154 // The native window only allows dequeueing all buffers before any have
1155 // been queued, since after that point at least one is assumed to be in
1156 // non-FREE state at any given time. Disconnecting and re-connecting
1157 // orphans the previous buffers, getting us back to the state where we can
1158 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001159 //
1160 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001161 ANativeWindow* window = surface.window.get();
1162 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1163 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001164 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001165 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1166 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001167 strerror(-err), err);
1168
Nicolas Capens147b7da2021-04-09 14:53:06 -04001169 err =
1170 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001171 if (err != android::OK) {
1172 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1173 strerror(-err), err);
1174 return VK_ERROR_SURFACE_LOST_KHR;
1175 }
1176
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301177 int swap_interval =
1178 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001179 err = window->setSwapInterval(window, swap_interval);
1180 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001181 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1182 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001183 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001184 }
1185
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001186 err = native_window_set_shared_buffer_mode(window, false);
1187 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001188 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1189 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001190 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001191 }
1192
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001193 err = native_window_set_auto_refresh(window, false);
1194 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001195 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1196 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001197 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001198 }
1199
Jesse Halld7b994a2015-09-07 14:17:37 -07001200 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001201
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001202 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001203
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001204 err = native_window_set_buffers_format(window, native_pixel_format);
1205 if (err != android::OK) {
Jesse Hall517274a2016-02-10 00:07:18 -08001206 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001207 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001208 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001209 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001210 err = native_window_set_buffers_data_space(window, native_dataspace);
1211 if (err != android::OK) {
Jesse Hall517274a2016-02-10 00:07:18 -08001212 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001213 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001214 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001215 }
1216
Jesse Hall3dd678a2016-01-08 21:52:01 -08001217 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001218 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001219 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001220 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001221 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1222 create_info->imageExtent.width, create_info->imageExtent.height,
1223 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001224 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001225 }
1226
Jesse Hall178b6962016-02-24 15:39:50 -08001227 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1228 // applied during rendering. native_window_set_transform() expects the
1229 // inverse: the transform the app is requesting that the compositor perform
1230 // during composition. With native windows, pre-transform works by rendering
1231 // with the same transform the compositor is applying (as in Vulkan), but
1232 // then requesting the inverse transform, so that when the compositor does
1233 // it's job the two transforms cancel each other out and the compositor ends
1234 // up applying an identity transform to the app's buffer.
1235 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001236 window, InvertTransformToNative(create_info->preTransform));
1237 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001238 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1239 InvertTransformToNative(create_info->preTransform),
1240 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001241 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001242 }
1243
Jesse Hallf64ca122015-11-03 16:11:10 -08001244 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001245 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1246 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001247 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1248 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001249 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001250 }
1251
Chris Forbes97ef4612017-03-30 19:37:50 +13001252 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1253 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1254 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1255 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001256 err = native_window_set_shared_buffer_mode(window, true);
1257 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001258 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1259 return VK_ERROR_SURFACE_LOST_KHR;
1260 }
1261 }
1262
1263 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001264 err = native_window_set_auto_refresh(window, true);
1265 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001266 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1267 return VK_ERROR_SURFACE_LOST_KHR;
1268 }
1269 }
1270
Ian Elliottf5737982021-12-01 12:35:57 -07001271 int query_value;
1272 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1273 &query_value);
1274 if (err != android::OK || query_value < 0) {
1275 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1276 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001277 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001278 }
Ian Elliottf5737982021-12-01 12:35:57 -07001279 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1280 const auto mailbox_num_images = std::max(3u, create_info->minImageCount);
1281 const auto requested_images =
1282 swap_interval ? create_info->minImageCount : mailbox_num_images;
1283 uint32_t num_images = requested_images - 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001284
Ian Elliott5396b702021-12-13 19:32:34 -07001285 // Lower layer insists that we have at least min_undequeued_buffers + 1
1286 // buffers. This is wasteful and we'd like to relax it in the shared case,
1287 // but not all the pieces are in place for that to work yet. Note we only
1288 // lie to the lower layer--we don't want to give the app back a swapchain
1289 // with extra images (which they can't actually use!).
1290 uint32_t min_buffer_count = min_undequeued_buffers + 1;
1291 err = native_window_set_buffer_count(
1292 window, std::max(min_buffer_count, num_images));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001293 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001294 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1295 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001296 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001297 }
1298
Ian Elliott5396b702021-12-13 19:32:34 -07001299 // In shared mode the num_images must be one regardless of how many
1300 // buffers were allocated for the buffer queue.
1301 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1302 num_images = 1;
1303 }
1304
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001305 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001306 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001307 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001308 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001309 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1310 device, create_info->imageFormat, create_info->imageUsage,
1311 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001312 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001313 if (result != VK_SUCCESS) {
1314 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001315 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001316 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001317 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001318 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001319 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001320 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001321 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001322 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001323 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001324 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001325 if (result != VK_SUCCESS) {
1326 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001327 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001328 }
Jesse Hall70f93352015-11-04 09:41:31 -08001329 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001330 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1331
1332 bool createProtectedSwapchain = false;
1333 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1334 createProtectedSwapchain = true;
1335 native_usage |= BufferUsage::PROTECTED;
1336 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001337 err = native_window_set_usage(window, native_usage);
1338 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001339 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001340 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001341 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001342
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001343 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001344 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1345 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001346 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1347 strerror(-err), err);
1348 return VK_ERROR_SURFACE_LOST_KHR;
1349 }
1350
Jesse Halld7b994a2015-09-07 14:17:37 -07001351 // -- Allocate our Swapchain object --
1352 // After this point, we must deallocate the swapchain on error.
1353
Jesse Hall1f91d392015-12-11 16:28:44 -08001354 void* mem = allocator->pfnAllocation(allocator->pUserData,
1355 sizeof(Swapchain), alignof(Swapchain),
1356 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001357 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001358 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001359 Swapchain* swapchain = new (mem)
1360 Swapchain(surface, num_images, create_info->presentMode,
1361 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001362 // -- Dequeue all buffers and create a VkImage for each --
1363 // Any failures during or after this must cancel the dequeued buffers.
1364
Chris Forbesb56287a2017-01-12 14:28:58 +13001365 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1366#pragma clang diagnostic push
1367#pragma clang diagnostic ignored "-Wold-style-cast"
1368 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1369#pragma clang diagnostic pop
1370 .pNext = nullptr,
1371 .usage = swapchain_image_usage,
1372 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001373 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001374#pragma clang diagnostic push
1375#pragma clang diagnostic ignored "-Wold-style-cast"
1376 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1377#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001378 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001379 };
1380 VkImageCreateInfo image_create = {
1381 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1382 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001383 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001384 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001385 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001386 .extent = {0, 0, 1},
1387 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001388 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001389 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001390 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001391 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001392 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001393 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001394 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1395 };
1396
Jesse Halld7b994a2015-09-07 14:17:37 -07001397 for (uint32_t i = 0; i < num_images; i++) {
1398 Swapchain::Image& img = swapchain->images[i];
1399
1400 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001401 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1402 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001403 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001404 switch (-err) {
1405 case ENOMEM:
1406 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1407 break;
1408 default:
1409 result = VK_ERROR_SURFACE_LOST_KHR;
1410 break;
1411 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001412 break;
1413 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001414 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001415 img.dequeued = true;
1416
1417 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001418 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1419 static_cast<uint32_t>(img.buffer->height),
1420 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001421 image_native_buffer.handle = img.buffer->handle;
1422 image_native_buffer.stride = img.buffer->stride;
1423 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001424 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001425 android_convertGralloc0To1Usage(int(img.buffer->usage),
1426 &image_native_buffer.usage2.producer,
1427 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001428
Yiwei Zhang533cea92019-06-03 18:43:24 -07001429 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001430 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001431 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001432 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001433 if (result != VK_SUCCESS) {
1434 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1435 break;
1436 }
1437 }
1438
1439 // -- Cancel all buffers, returning them to the queue --
1440 // If an error occurred before, also destroy the VkImage and release the
1441 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001442 for (uint32_t i = 0; i < num_images; i++) {
1443 Swapchain::Image& img = swapchain->images[i];
1444 if (img.dequeued) {
1445 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001446 window->cancelBuffer(window, img.buffer.get(),
1447 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001448 img.dequeue_fence = -1;
1449 img.dequeued = false;
1450 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001451 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001452 }
1453
1454 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001455 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1456 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001457 return result;
1458 }
1459
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001460 if (transform_hint != swapchain->pre_transform) {
1461 // Log that the app is not doing pre-rotation.
1462 android::GraphicsEnv::getInstance().setTargetStats(
1463 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1464 }
1465
Jesse Halldc225072016-05-30 22:40:14 -07001466 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1467 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001468 return VK_SUCCESS;
1469}
1470
Jesse Halle1b12782015-11-30 11:27:32 -08001471VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001472void DestroySwapchainKHR(VkDevice device,
1473 VkSwapchainKHR swapchain_handle,
1474 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001475 ATRACE_CALL();
1476
Yiwei Zhang533cea92019-06-03 18:43:24 -07001477 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001478}
1479
Jesse Halle1b12782015-11-30 11:27:32 -08001480VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001481VkResult GetSwapchainImagesKHR(VkDevice,
1482 VkSwapchainKHR swapchain_handle,
1483 uint32_t* count,
1484 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001485 ATRACE_CALL();
1486
Jesse Halld7b994a2015-09-07 14:17:37 -07001487 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001488 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1489 "getting images for non-active swapchain 0x%" PRIx64
1490 "; only dequeued image handles are valid",
1491 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001492 VkResult result = VK_SUCCESS;
1493 if (images) {
1494 uint32_t n = swapchain.num_images;
1495 if (*count < swapchain.num_images) {
1496 n = *count;
1497 result = VK_INCOMPLETE;
1498 }
1499 for (uint32_t i = 0; i < n; i++)
1500 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001501 *count = n;
1502 } else {
1503 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001504 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001505 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001506}
1507
Jesse Halle1b12782015-11-30 11:27:32 -08001508VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001509VkResult AcquireNextImageKHR(VkDevice device,
1510 VkSwapchainKHR swapchain_handle,
1511 uint64_t timeout,
1512 VkSemaphore semaphore,
1513 VkFence vk_fence,
1514 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001515 ATRACE_CALL();
1516
Jesse Halld7b994a2015-09-07 14:17:37 -07001517 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001518 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001519 VkResult result;
1520 int err;
1521
Jesse Halldc225072016-05-30 22:40:14 -07001522 if (swapchain.surface.swapchain_handle != swapchain_handle)
1523 return VK_ERROR_OUT_OF_DATE_KHR;
1524
Chris Forbesc88409c2017-03-30 19:47:37 +13001525 if (swapchain.shared) {
1526 // In shared mode, we keep the buffer dequeued all the time, so we don't
1527 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1528 // the semaphore and fence passed to us will be signalled.
1529 *image_index = 0;
1530 result = GetData(device).driver.AcquireImageANDROID(
1531 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1532 return result;
1533 }
1534
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001535 const nsecs_t acquire_next_image_timeout =
1536 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1537 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1538 // Cache the timeout to avoid the duplicate binder cost.
1539 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1540 acquire_next_image_timeout);
1541 if (err != android::OK) {
1542 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1543 strerror(-err), err);
1544 return VK_ERROR_SURFACE_LOST_KHR;
1545 }
1546 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1547 }
1548
Jesse Halld7b994a2015-09-07 14:17:37 -07001549 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001550 int fence_fd;
1551 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001552 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001553 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1554 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1555 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001556 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001557 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001558 }
1559
1560 uint32_t idx;
1561 for (idx = 0; idx < swapchain.num_images; idx++) {
1562 if (swapchain.images[idx].buffer.get() == buffer) {
1563 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001564 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001565 break;
1566 }
1567 }
1568 if (idx == swapchain.num_images) {
1569 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001570 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001571 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001572 }
1573
1574 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001575 if (fence_fd != -1) {
1576 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001577 if (fence_clone == -1) {
1578 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1579 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001580 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001581 }
1582 }
1583
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001584 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001585 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001586 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001587 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1588 // even if the call fails. We could close it ourselves on failure, but
1589 // that would create a race condition if the driver closes it on a
1590 // failure path: some other thread might create an fd with the same
1591 // number between the time the driver closes it and the time we close
1592 // it. We must assume one of: the driver *always* closes it even on
1593 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001594 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001595 swapchain.images[idx].dequeued = false;
1596 swapchain.images[idx].dequeue_fence = -1;
1597 return result;
1598 }
1599
1600 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001601 return VK_SUCCESS;
1602}
1603
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001604VKAPI_ATTR
1605VkResult AcquireNextImage2KHR(VkDevice device,
1606 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1607 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001608 ATRACE_CALL();
1609
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001610 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1611 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1612 pAcquireInfo->fence, pImageIndex);
1613}
1614
Jesse Halldc225072016-05-30 22:40:14 -07001615static VkResult WorstPresentResult(VkResult a, VkResult b) {
1616 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1617 // (in spec version 1.0.14).
1618 static const VkResult kWorstToBest[] = {
1619 VK_ERROR_DEVICE_LOST,
1620 VK_ERROR_SURFACE_LOST_KHR,
1621 VK_ERROR_OUT_OF_DATE_KHR,
1622 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1623 VK_ERROR_OUT_OF_HOST_MEMORY,
1624 VK_SUBOPTIMAL_KHR,
1625 };
1626 for (auto result : kWorstToBest) {
1627 if (a == result || b == result)
1628 return result;
1629 }
1630 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1631 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1632 return a != VK_SUCCESS ? a : b;
1633}
1634
Jesse Halle1b12782015-11-30 11:27:32 -08001635VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001636VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001637 ATRACE_CALL();
1638
Jesse Halld7b994a2015-09-07 14:17:37 -07001639 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1640 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1641 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001642
Jesse Halldc225072016-05-30 22:40:14 -07001643 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001644 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001645 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001646
Ian Elliottcb351132016-12-13 10:30:40 -07001647 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001648 const VkPresentRegionsKHR* present_regions = nullptr;
1649 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001650 const VkPresentRegionsKHR* next =
1651 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1652 while (next) {
1653 switch (next->sType) {
1654 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1655 present_regions = next;
1656 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001657 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001658 present_times =
1659 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1660 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001661 default:
1662 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1663 next->sType);
1664 break;
1665 }
1666 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1667 }
1668 ALOGV_IF(
1669 present_regions &&
1670 present_regions->swapchainCount != present_info->swapchainCount,
1671 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001672 ALOGV_IF(present_times &&
1673 present_times->swapchainCount != present_info->swapchainCount,
1674 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1675 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001676 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001677 (present_regions) ? present_regions->pRegions : nullptr;
1678 const VkPresentTimeGOOGLE* times =
1679 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001680 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001681 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001682 uint32_t nrects = 0;
1683
Jesse Halld7b994a2015-09-07 14:17:37 -07001684 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1685 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001686 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001687 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001688 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001689 const VkPresentRegionKHR* region =
1690 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001691 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001692 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001693 VkResult result;
1694 int err;
1695
Jesse Halld7b994a2015-09-07 14:17:37 -07001696 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001697 result = dispatch.QueueSignalReleaseImageANDROID(
1698 queue, present_info->waitSemaphoreCount,
1699 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001700 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001701 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001702 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001703 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001704 if (img.release_fence >= 0)
1705 close(img.release_fence);
1706 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001707
Jesse Halldc225072016-05-30 22:40:14 -07001708 if (swapchain.surface.swapchain_handle ==
1709 present_info->pSwapchains[sc]) {
1710 ANativeWindow* window = swapchain.surface.window.get();
1711 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001712 if (region) {
1713 // Process the incremental-present hint for this swapchain:
1714 uint32_t rcount = region->rectangleCount;
1715 if (rcount > nrects) {
1716 android_native_rect_t* new_rects =
1717 static_cast<android_native_rect_t*>(
1718 allocator->pfnReallocation(
1719 allocator->pUserData, rects,
1720 sizeof(android_native_rect_t) * rcount,
1721 alignof(android_native_rect_t),
1722 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1723 if (new_rects) {
1724 rects = new_rects;
1725 nrects = rcount;
1726 } else {
1727 rcount = 0; // Ignore the hint for this swapchain
1728 }
1729 }
1730 for (uint32_t r = 0; r < rcount; ++r) {
1731 if (region->pRectangles[r].layer > 0) {
1732 ALOGV(
1733 "vkQueuePresentKHR ignoring invalid layer "
1734 "(%u); using layer 0 instead",
1735 region->pRectangles[r].layer);
1736 }
1737 int x = region->pRectangles[r].offset.x;
1738 int y = region->pRectangles[r].offset.y;
1739 int width = static_cast<int>(
1740 region->pRectangles[r].extent.width);
1741 int height = static_cast<int>(
1742 region->pRectangles[r].extent.height);
1743 android_native_rect_t* cur_rect = &rects[r];
1744 cur_rect->left = x;
1745 cur_rect->top = y + height;
1746 cur_rect->right = x + width;
1747 cur_rect->bottom = y;
1748 }
1749 native_window_set_surface_damage(window, rects, rcount);
1750 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001751 if (time) {
1752 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001753 ALOGV(
1754 "Calling "
1755 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001756 native_window_enable_frame_timestamps(window, true);
1757 swapchain.frame_timestamps_enabled = true;
1758 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001759
1760 // Record the nativeFrameId so it can be later correlated to
1761 // this present.
1762 uint64_t nativeFrameId = 0;
1763 err = native_window_get_next_frame_id(
1764 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001765 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001766 ALOGE("Failed to get next native frame ID.");
1767 }
1768
1769 // Add a new timing record with the user's presentID and
1770 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001771 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001772 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001773 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001774 }
1775 if (time->desiredPresentTime) {
1776 // Set the desiredPresentTime:
1777 ALOGV(
1778 "Calling "
1779 "native_window_set_buffers_timestamp(%" PRId64 ")",
1780 time->desiredPresentTime);
1781 native_window_set_buffers_timestamp(
1782 window,
1783 static_cast<int64_t>(time->desiredPresentTime));
1784 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001785 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001786
Jesse Halldc225072016-05-30 22:40:14 -07001787 err = window->queueBuffer(window, img.buffer.get(), fence);
1788 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001789 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001790 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1791 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001792 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001793 } else {
1794 if (img.dequeue_fence >= 0) {
1795 close(img.dequeue_fence);
1796 img.dequeue_fence = -1;
1797 }
1798 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001799 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001800
1801 // If the swapchain is in shared mode, immediately dequeue the
1802 // buffer so it can be presented again without an intervening
1803 // call to AcquireNextImageKHR. We expect to get the same buffer
1804 // back from every call to dequeueBuffer in this mode.
1805 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1806 ANativeWindowBuffer* buffer;
1807 int fence_fd;
1808 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001809 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001810 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1811 swapchain_result = WorstPresentResult(swapchain_result,
1812 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001813 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001814 ALOGE("got wrong image back for shared swapchain");
1815 swapchain_result = WorstPresentResult(swapchain_result,
1816 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001817 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13001818 img.dequeue_fence = fence_fd;
1819 img.dequeued = true;
1820 }
1821 }
Jesse Halldc225072016-05-30 22:40:14 -07001822 }
1823 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001824 OrphanSwapchain(device, &swapchain);
1825 }
silence_dogood73597592019-05-23 16:57:37 -07001826 int window_transform_hint;
1827 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1828 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001829 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07001830 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1831 strerror(-err), err);
1832 swapchain_result = WorstPresentResult(
1833 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1834 }
1835 if (swapchain.pre_transform != window_transform_hint) {
1836 swapchain_result =
1837 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1838 }
Jesse Halldc225072016-05-30 22:40:14 -07001839 } else {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001840 ReleaseSwapchainImage(device, nullptr, fence, img, true);
Jesse Halldc225072016-05-30 22:40:14 -07001841 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001842 }
1843
Jesse Halla9e57032015-11-30 01:03:10 -08001844 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001845 present_info->pResults[sc] = swapchain_result;
1846
1847 if (swapchain_result != final_result)
1848 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001849 }
Ian Elliottcb351132016-12-13 10:30:40 -07001850 if (rects) {
1851 allocator->pfnFree(allocator->pUserData, rects);
1852 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001853
1854 return final_result;
1855}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001856
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001857VKAPI_ATTR
1858VkResult GetRefreshCycleDurationGOOGLE(
1859 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001860 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001861 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001862 ATRACE_CALL();
1863
Ian Elliott62c48c92017-01-20 13:13:20 -07001864 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001865 VkResult result = VK_SUCCESS;
1866
Ian Elliott3568bfe2019-05-03 15:54:46 -06001867 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001868
1869 return result;
1870}
1871
1872VKAPI_ATTR
1873VkResult GetPastPresentationTimingGOOGLE(
1874 VkDevice,
1875 VkSwapchainKHR swapchain_handle,
1876 uint32_t* count,
1877 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001878 ATRACE_CALL();
1879
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001880 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001881 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1882 return VK_ERROR_OUT_OF_DATE_KHR;
1883 }
1884
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001885 ANativeWindow* window = swapchain.surface.window.get();
1886 VkResult result = VK_SUCCESS;
1887
1888 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001889 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001890 native_window_enable_frame_timestamps(window, true);
1891 swapchain.frame_timestamps_enabled = true;
1892 }
1893
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001894 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07001895 // Get the latest ready timing count before copying, since the copied
1896 // timing info will be erased in copy_ready_timings function.
1897 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07001898 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001899 // Check the *count here against the recorded ready timing count, since
1900 // *count can be overwritten per spec describes.
1901 if (*count < n) {
1902 result = VK_INCOMPLETE;
1903 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001904 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001905 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001906 }
1907
1908 return result;
1909}
1910
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001911VKAPI_ATTR
1912VkResult GetSwapchainStatusKHR(
1913 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001914 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001915 ATRACE_CALL();
1916
Chris Forbes4e18ba82017-01-20 12:50:17 +13001917 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001918 VkResult result = VK_SUCCESS;
1919
Chris Forbes4e18ba82017-01-20 12:50:17 +13001920 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1921 return VK_ERROR_OUT_OF_DATE_KHR;
1922 }
1923
Yiwei Zhanga885c062019-10-24 12:07:57 -07001924 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001925
1926 return result;
1927}
1928
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001929VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001930 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001931 uint32_t swapchainCount,
1932 const VkSwapchainKHR* pSwapchains,
1933 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001934 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001935
1936 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1937 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1938 if (!swapchain)
1939 continue;
1940
1941 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1942
1943 ANativeWindow* window = swapchain->surface.window.get();
1944
1945 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1946 const android_smpte2086_metadata smpteMetdata = {
1947 {vulkanMetadata.displayPrimaryRed.x,
1948 vulkanMetadata.displayPrimaryRed.y},
1949 {vulkanMetadata.displayPrimaryGreen.x,
1950 vulkanMetadata.displayPrimaryGreen.y},
1951 {vulkanMetadata.displayPrimaryBlue.x,
1952 vulkanMetadata.displayPrimaryBlue.y},
1953 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1954 vulkanMetadata.maxLuminance,
1955 vulkanMetadata.minLuminance};
1956 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1957
1958 const android_cta861_3_metadata cta8613Metadata = {
1959 vulkanMetadata.maxContentLightLevel,
1960 vulkanMetadata.maxFrameAverageLightLevel};
1961 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1962 }
1963
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001964 return;
1965}
1966
Yiwei Zhang0f475222019-04-11 19:38:00 -07001967static void InterceptBindImageMemory2(
1968 uint32_t bind_info_count,
1969 const VkBindImageMemoryInfo* bind_infos,
1970 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1971 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1972 out_native_buffers->clear();
1973 out_bind_infos->clear();
1974
1975 if (!bind_info_count)
1976 return;
1977
1978 std::unordered_set<uint32_t> intercepted_indexes;
1979
1980 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1981 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1982 bind_infos[idx].pNext);
1983 while (info &&
1984 info->sType !=
1985 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1986 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1987 info->pNext);
1988 }
1989
1990 if (!info)
1991 continue;
1992
1993 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1994 "swapchain handle must not be NULL");
1995 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1996 ALOG_ASSERT(
1997 info->imageIndex < swapchain->num_images,
1998 "imageIndex must be less than the number of images in swapchain");
1999
2000 ANativeWindowBuffer* buffer =
2001 swapchain->images[info->imageIndex].buffer.get();
2002 VkNativeBufferANDROID native_buffer = {
2003#pragma clang diagnostic push
2004#pragma clang diagnostic ignored "-Wold-style-cast"
2005 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
2006#pragma clang diagnostic pop
2007 .pNext = bind_infos[idx].pNext,
2008 .handle = buffer->handle,
2009 .stride = buffer->stride,
2010 .format = buffer->format,
2011 .usage = int(buffer->usage),
2012 };
2013 // Reserve enough space to avoid letting re-allocation invalidate the
2014 // addresses of the elements inside.
2015 out_native_buffers->reserve(bind_info_count);
2016 out_native_buffers->emplace_back(native_buffer);
2017
2018 // Reserve the space now since we know how much is needed now.
2019 out_bind_infos->reserve(bind_info_count);
2020 out_bind_infos->emplace_back(bind_infos[idx]);
2021 out_bind_infos->back().pNext = &out_native_buffers->back();
2022
2023 intercepted_indexes.insert(idx);
2024 }
2025
2026 if (intercepted_indexes.empty())
2027 return;
2028
2029 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
2030 if (intercepted_indexes.count(idx))
2031 continue;
2032 out_bind_infos->emplace_back(bind_infos[idx]);
2033 }
2034}
2035
Yiwei Zhang23143102019-04-10 18:24:05 -07002036VKAPI_ATTR
2037VkResult BindImageMemory2(VkDevice device,
2038 uint32_t bindInfoCount,
2039 const VkBindImageMemoryInfo* pBindInfos) {
2040 ATRACE_CALL();
2041
Yiwei Zhang0f475222019-04-11 19:38:00 -07002042 // out_native_buffers is for maintaining the lifecycle of the constructed
2043 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
2044 std::vector<VkNativeBufferANDROID> out_native_buffers;
2045 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2046 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2047 &out_bind_infos);
2048 return GetData(device).driver.BindImageMemory2(
2049 device, bindInfoCount,
2050 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002051}
2052
2053VKAPI_ATTR
2054VkResult BindImageMemory2KHR(VkDevice device,
2055 uint32_t bindInfoCount,
2056 const VkBindImageMemoryInfo* pBindInfos) {
2057 ATRACE_CALL();
2058
Yiwei Zhang0f475222019-04-11 19:38:00 -07002059 std::vector<VkNativeBufferANDROID> out_native_buffers;
2060 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2061 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2062 &out_bind_infos);
2063 return GetData(device).driver.BindImageMemory2KHR(
2064 device, bindInfoCount,
2065 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002066}
2067
Chia-I Wu62262232016-03-26 07:06:44 +08002068} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002069} // namespace vulkan