blob: e89a49b04c5f1d13546ba4bc70f9a5e7da43a68b [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
Ady Abraham148bad02021-06-18 12:32:33 -0700540int get_min_buffer_count(ANativeWindow* window,
541 uint32_t* out_min_buffer_count) {
542 constexpr int kExtraBuffers = 2;
543
544 int err;
545 int min_undequeued_buffers;
546 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
547 &min_undequeued_buffers);
548 if (err != android::OK || min_undequeued_buffers < 0) {
549 ALOGE(
550 "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
551 "value=%d",
552 strerror(-err), err, min_undequeued_buffers);
553 if (err == android::OK) {
554 err = android::UNKNOWN_ERROR;
555 }
556 return err;
557 }
558
559 *out_min_buffer_count =
560 static_cast<uint32_t>(min_undequeued_buffers + kExtraBuffers);
561 return android::OK;
562}
563
Jesse Halld7b994a2015-09-07 14:17:37 -0700564} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700565
Jesse Halle1b12782015-11-30 11:27:32 -0800566VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800567VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800568 VkInstance instance,
569 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
570 const VkAllocationCallbacks* allocator,
571 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800572 ATRACE_CALL();
573
Jesse Hall1f91d392015-12-11 16:28:44 -0800574 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800575 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800576 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
577 alignof(Surface),
578 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800579 if (!mem)
580 return VK_ERROR_OUT_OF_HOST_MEMORY;
581 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700582
Chia-I Wue8e689f2016-04-18 08:21:31 +0800583 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700584 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700585 int err = native_window_get_consumer_usage(surface->window.get(),
586 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800587 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700588 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
589 strerror(-err), err);
590 surface->~Surface();
591 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700592 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700593 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700594
Yiwei Zhang6435b322018-05-08 11:12:17 -0700595 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800596 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800597 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800598 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
599 err);
600 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800601 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700602 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800603 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700604
Jesse Hall1356b0d2015-11-23 17:24:58 -0800605 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700606 return VK_SUCCESS;
607}
608
Jesse Halle1b12782015-11-30 11:27:32 -0800609VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800610void DestroySurfaceKHR(VkInstance instance,
611 VkSurfaceKHR surface_handle,
612 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800613 ATRACE_CALL();
614
Jesse Hall1356b0d2015-11-23 17:24:58 -0800615 Surface* surface = SurfaceFromHandle(surface_handle);
616 if (!surface)
617 return;
618 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700619 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700620 "destroyed VkSurfaceKHR 0x%" PRIx64
621 " has active VkSwapchainKHR 0x%" PRIx64,
622 reinterpret_cast<uint64_t>(surface_handle),
623 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800624 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800625 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800626 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800627 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800628}
629
Jesse Halle1b12782015-11-30 11:27:32 -0800630VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800631VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
632 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000633 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800634 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000635 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800636 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800637}
638
Jesse Halle1b12782015-11-30 11:27:32 -0800639VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800640VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800641 VkPhysicalDevice /*pdev*/,
642 VkSurfaceKHR surface,
643 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800644 ATRACE_CALL();
645
Jesse Halld7b994a2015-09-07 14:17:37 -0700646 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800647 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700648
649 int width, height;
650 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800651 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700652 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
653 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700654 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700655 }
656 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800657 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700658 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
659 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700660 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700661 }
662
Jesse Hall55bc0972016-02-23 16:43:29 -0800663 int transform_hint;
664 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800665 if (err != android::OK) {
Jesse Hall55bc0972016-02-23 16:43:29 -0800666 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
667 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700668 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800669 }
670
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800671 int max_buffer_count;
672 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800673 if (err != android::OK) {
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800674 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
675 strerror(-err), err);
676 return VK_ERROR_SURFACE_LOST_KHR;
677 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700678 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800679 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700680
Jesse Hallfe2662d2016-02-09 13:26:59 -0800681 capabilities->currentExtent =
682 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
683
Yiwei Zhang70a21962019-05-31 17:26:52 -0700684 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800685 capabilities->minImageExtent = VkExtent2D{1, 1};
686 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700687
Jesse Hallfe2662d2016-02-09 13:26:59 -0800688 capabilities->maxImageArrayLayers = 1;
689
Jesse Hall55bc0972016-02-23 16:43:29 -0800690 capabilities->supportedTransforms = kSupportedTransforms;
691 capabilities->currentTransform =
692 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700693
Jesse Hallfe2662d2016-02-09 13:26:59 -0800694 // On Android, window composition is a WindowManager property, not something
695 // associated with the bufferqueue. It can't be changed from here.
696 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700697
Jesse Hallb00daad2015-11-29 19:46:20 -0800698 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800699 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
700 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
701 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700702 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
703
Jesse Hallb1352bc2015-09-04 16:12:33 -0700704 return VK_SUCCESS;
705}
706
Jesse Halle1b12782015-11-30 11:27:32 -0800707VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700708VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
709 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800710 uint32_t* count,
711 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800712 ATRACE_CALL();
713
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700714 const InstanceData& instance_data = GetData(pdev);
715
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700716 bool wide_color_support = false;
717 Surface& surface = *SurfaceFromHandle(surface_handle);
718 int err = native_window_get_wide_color_support(surface.window.get(),
719 &wide_color_support);
720 if (err) {
Yiwei Zhang70a21962019-05-31 17:26:52 -0700721 return VK_ERROR_SURFACE_LOST_KHR;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700722 }
723 ALOGV("wide_color_support is: %d", wide_color_support);
724 wide_color_support =
725 wide_color_support &&
726 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
727
Charlie Lao324191f2019-12-13 11:03:16 -0800728 AHardwareBuffer_Desc desc = {};
729 desc.width = 1;
730 desc.height = 1;
731 desc.layers = 1;
732 desc.usage = surface.consumer_usage |
733 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
734 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
735
736 // We must support R8G8B8A8
737 std::vector<VkSurfaceFormatKHR> all_formats = {
738 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Trevor David Black737b5b42021-10-19 16:05:13 +0000739 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
740 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_BT709_LINEAR_EXT}};
Charlie Lao324191f2019-12-13 11:03:16 -0800741
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700742 if (wide_color_support) {
Charlie Lao324191f2019-12-13 11:03:16 -0800743 all_formats.emplace_back(VkSurfaceFormatKHR{
744 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
745 all_formats.emplace_back(VkSurfaceFormatKHR{
746 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
747 }
748
749 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
750 if (AHardwareBuffer_isSupported(&desc)) {
751 all_formats.emplace_back(VkSurfaceFormatKHR{
752 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
753 }
754
755 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
756 if (AHardwareBuffer_isSupported(&desc)) {
757 all_formats.emplace_back(VkSurfaceFormatKHR{
758 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
759 if (wide_color_support) {
760 all_formats.emplace_back(
761 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
762 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
763 all_formats.emplace_back(
764 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
765 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
766 }
767 }
768
769 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
770 if (AHardwareBuffer_isSupported(&desc)) {
771 all_formats.emplace_back(
772 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
773 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
774 if (wide_color_support) {
775 all_formats.emplace_back(
776 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
777 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
778 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700779 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700780
781 VkResult result = VK_SUCCESS;
782 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800783 uint32_t transfer_count = all_formats.size();
784 if (transfer_count > *count) {
785 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700786 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700787 }
Charlie Lao324191f2019-12-13 11:03:16 -0800788 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
789 formats);
790 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700791 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800792 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700793 }
Charlie Lao324191f2019-12-13 11:03:16 -0800794
Jesse Halld7b994a2015-09-07 14:17:37 -0700795 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700796}
797
Jesse Halle1b12782015-11-30 11:27:32 -0800798VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300799VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
800 VkPhysicalDevice physicalDevice,
801 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
802 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800803 ATRACE_CALL();
804
Chris Forbes2452cf72017-03-16 16:30:17 +1300805 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
806 physicalDevice, pSurfaceInfo->surface,
807 &pSurfaceCapabilities->surfaceCapabilities);
808
Chris Forbes06bc0092017-03-16 16:46:05 +1300809 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
810 while (caps->pNext) {
811 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
812
813 switch (caps->sType) {
814 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
815 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
816 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
817 caps);
818 // Claim same set of usage flags are supported for
819 // shared present modes as for other modes.
820 shared_caps->sharedPresentSupportedUsageFlags =
821 pSurfaceCapabilities->surfaceCapabilities
822 .supportedUsageFlags;
823 } break;
824
825 default:
826 // Ignore all other extension structs
827 break;
828 }
829 }
830
Chris Forbes2452cf72017-03-16 16:30:17 +1300831 return result;
832}
833
834VKAPI_ATTR
835VkResult GetPhysicalDeviceSurfaceFormats2KHR(
836 VkPhysicalDevice physicalDevice,
837 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
838 uint32_t* pSurfaceFormatCount,
839 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800840 ATRACE_CALL();
841
Chris Forbes2452cf72017-03-16 16:30:17 +1300842 if (!pSurfaceFormats) {
843 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
844 pSurfaceInfo->surface,
845 pSurfaceFormatCount, nullptr);
846 } else {
847 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
848 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700849 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300850 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
851 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700852 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300853
854 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
855 // marshal results individually due to stride difference.
856 // completely ignore any chained extension structs.
857 uint32_t formats_to_marshal = *pSurfaceFormatCount;
858 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
859 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
860 }
861 }
862
863 return result;
864 }
865}
866
867VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300868VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800869 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800870 uint32_t* count,
871 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800872 ATRACE_CALL();
873
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800874 int err;
875 int query_value;
Ady Abraham148bad02021-06-18 12:32:33 -0700876 uint32_t min_buffer_count;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800877 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
878
Ady Abraham148bad02021-06-18 12:32:33 -0700879 err = get_min_buffer_count(window, &min_buffer_count);
880 if (err != android::OK) {
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800881 return VK_ERROR_SURFACE_LOST_KHR;
882 }
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800883
884 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800885 if (err != android::OK || query_value < 0) {
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800886 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
887 strerror(-err), err, query_value);
888 return VK_ERROR_SURFACE_LOST_KHR;
889 }
890 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
891
Yiwei Zhang5e862202019-06-21 14:59:16 -0700892 std::vector<VkPresentModeKHR> present_modes;
Ady Abraham148bad02021-06-18 12:32:33 -0700893 if (min_buffer_count < max_buffer_count)
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800894 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300895 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
896
897 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700898 QueryPresentationProperties(pdev, &present_properties);
899 if (present_properties.sharedImage) {
900 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
901 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300902 }
903
904 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700905
906 VkResult result = VK_SUCCESS;
907 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300908 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700909 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300910 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -0700911 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700912 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300913 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700914 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700915 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700916}
917
Jesse Halle1b12782015-11-30 11:27:32 -0800918VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400919VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600920 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400921 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800922 ATRACE_CALL();
923
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400924 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
925 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
926 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
927 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
928 pDeviceGroupPresentCapabilities->sType);
929
930 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
931 sizeof(pDeviceGroupPresentCapabilities->presentMask));
932
933 // assume device group of size 1
934 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
935 pDeviceGroupPresentCapabilities->modes =
936 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
937
938 return VK_SUCCESS;
939}
940
941VKAPI_ATTR
942VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600943 VkDevice,
944 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400945 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800946 ATRACE_CALL();
947
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400948 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
949 return VK_SUCCESS;
950}
951
952VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600953VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400954 VkSurfaceKHR surface,
955 uint32_t* pRectCount,
956 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800957 ATRACE_CALL();
958
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400959 if (!pRects) {
960 *pRectCount = 1;
961 } else {
962 uint32_t count = std::min(*pRectCount, 1u);
963 bool incomplete = *pRectCount < 1;
964
965 *pRectCount = count;
966
967 if (incomplete) {
968 return VK_INCOMPLETE;
969 }
970
971 int err;
972 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
973
974 int width = 0, height = 0;
975 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800976 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400977 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
978 strerror(-err), err);
979 }
980 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800981 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400982 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
983 strerror(-err), err);
984 }
985
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400986 pRects[0].offset.x = 0;
987 pRects[0].offset.y = 0;
988 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
989 static_cast<uint32_t>(height)};
990 }
991 return VK_SUCCESS;
992}
993
Yiwei Zhang533cea92019-06-03 18:43:24 -0700994static void DestroySwapchainInternal(VkDevice device,
995 VkSwapchainKHR swapchain_handle,
996 const VkAllocationCallbacks* allocator) {
997 ATRACE_CALL();
998
999 const auto& dispatch = GetData(device).driver;
1000 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1001 if (!swapchain) {
1002 return;
1003 }
1004
1005 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1006 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1007
1008 if (window && swapchain->frame_timestamps_enabled) {
1009 native_window_enable_frame_timestamps(window, false);
1010 }
1011
1012 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001013 ReleaseSwapchainImage(device, window, -1, swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001014 }
1015
1016 if (active) {
1017 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1018 }
1019
1020 if (!allocator) {
1021 allocator = &GetData(device).allocator;
1022 }
1023
1024 swapchain->~Swapchain();
1025 allocator->pfnFree(allocator->pUserData, swapchain);
1026}
1027
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001028VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001029VkResult CreateSwapchainKHR(VkDevice device,
1030 const VkSwapchainCreateInfoKHR* create_info,
1031 const VkAllocationCallbacks* allocator,
1032 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001033 ATRACE_CALL();
1034
Jesse Halld7b994a2015-09-07 14:17:37 -07001035 int err;
1036 VkResult result = VK_SUCCESS;
1037
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001038 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1039 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1040 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1041 " oldSwapchain=0x%" PRIx64,
1042 reinterpret_cast<uint64_t>(create_info->surface),
1043 create_info->minImageCount, create_info->imageFormat,
1044 create_info->imageColorSpace, create_info->imageExtent.width,
1045 create_info->imageExtent.height, create_info->imageUsage,
1046 create_info->preTransform, create_info->presentMode,
1047 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1048
Jesse Hall1f91d392015-12-11 16:28:44 -08001049 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001050 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001051
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001052 android_pixel_format native_pixel_format =
1053 GetNativePixelFormat(create_info->imageFormat);
1054 android_dataspace native_dataspace =
1055 GetNativeDataspace(create_info->imageColorSpace);
1056 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1057 ALOGE(
1058 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1059 "failed: Unsupported color space",
1060 create_info->imageColorSpace);
1061 return VK_ERROR_INITIALIZATION_FAILED;
1062 }
1063
Jesse Hall42a9eec2016-06-03 12:39:49 -07001064 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001065 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001066 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001067 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001068 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001069 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001070 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001071 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001072 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1073 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001074 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001075 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001076
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001077 Surface& surface = *SurfaceFromHandle(create_info->surface);
1078
Jesse Halldc225072016-05-30 22:40:14 -07001079 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001080 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001081 " because it already has active swapchain 0x%" PRIx64
1082 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1083 reinterpret_cast<uint64_t>(create_info->surface),
1084 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1085 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1086 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1087 }
1088 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1089 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1090
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001091 // -- Reset the native window --
1092 // The native window might have been used previously, and had its properties
1093 // changed from defaults. That will affect the answer we get for queries
1094 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1095 // attempt such queries.
1096
Jesse Halldc225072016-05-30 22:40:14 -07001097 // The native window only allows dequeueing all buffers before any have
1098 // been queued, since after that point at least one is assumed to be in
1099 // non-FREE state at any given time. Disconnecting and re-connecting
1100 // orphans the previous buffers, getting us back to the state where we can
1101 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001102 //
1103 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001104 ANativeWindow* window = surface.window.get();
1105 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1106 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001107 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001108 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1109 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001110 strerror(-err), err);
1111
Nicolas Capens147b7da2021-04-09 14:53:06 -04001112 err =
1113 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001114 if (err != android::OK) {
1115 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1116 strerror(-err), err);
1117 return VK_ERROR_SURFACE_LOST_KHR;
1118 }
1119
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301120 int swap_interval =
1121 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001122 err = window->setSwapInterval(window, swap_interval);
1123 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001124 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1125 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001126 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001127 }
1128
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001129 err = native_window_set_shared_buffer_mode(window, false);
1130 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001131 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1132 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001133 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001134 }
1135
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001136 err = native_window_set_auto_refresh(window, false);
1137 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001138 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1139 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001140 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001141 }
1142
Jesse Halld7b994a2015-09-07 14:17:37 -07001143 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001144
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001145 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001146
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001147 err = native_window_set_buffers_format(window, native_pixel_format);
1148 if (err != android::OK) {
Jesse Hall517274a2016-02-10 00:07:18 -08001149 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001150 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001151 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001152 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001153 err = native_window_set_buffers_data_space(window, native_dataspace);
1154 if (err != android::OK) {
Jesse Hall517274a2016-02-10 00:07:18 -08001155 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001156 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001157 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001158 }
1159
Jesse Hall3dd678a2016-01-08 21:52:01 -08001160 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001161 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001162 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001163 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001164 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1165 create_info->imageExtent.width, create_info->imageExtent.height,
1166 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001167 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001168 }
1169
Jesse Hall178b6962016-02-24 15:39:50 -08001170 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1171 // applied during rendering. native_window_set_transform() expects the
1172 // inverse: the transform the app is requesting that the compositor perform
1173 // during composition. With native windows, pre-transform works by rendering
1174 // with the same transform the compositor is applying (as in Vulkan), but
1175 // then requesting the inverse transform, so that when the compositor does
1176 // it's job the two transforms cancel each other out and the compositor ends
1177 // up applying an identity transform to the app's buffer.
1178 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001179 window, InvertTransformToNative(create_info->preTransform));
1180 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001181 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1182 InvertTransformToNative(create_info->preTransform),
1183 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001184 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001185 }
1186
Jesse Hallf64ca122015-11-03 16:11:10 -08001187 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001188 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1189 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001190 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1191 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001192 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001193 }
1194
Chris Forbes97ef4612017-03-30 19:37:50 +13001195 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1196 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1197 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1198 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001199 err = native_window_set_shared_buffer_mode(window, true);
1200 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001201 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1202 return VK_ERROR_SURFACE_LOST_KHR;
1203 }
1204 }
1205
1206 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001207 err = native_window_set_auto_refresh(window, true);
1208 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001209 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1210 return VK_ERROR_SURFACE_LOST_KHR;
1211 }
1212 }
1213
Ady Abraham148bad02021-06-18 12:32:33 -07001214 uint32_t min_buffer_count;
1215 err = get_min_buffer_count(window, &min_buffer_count);
1216 if (err != android::OK) {
Mike Stroyan762c8132017-02-22 11:43:09 -07001217 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001218 }
Ady Abraham148bad02021-06-18 12:32:33 -07001219
Jesse Halld7b994a2015-09-07 14:17:37 -07001220 uint32_t num_images =
Ady Abraham148bad02021-06-18 12:32:33 -07001221 std::max(min_buffer_count, create_info->minImageCount);
Chris Forbes2c8fc752017-03-17 11:28:32 +13001222
1223 // Lower layer insists that we have at least two buffers. This is wasteful
1224 // and we'd like to relax it in the shared case, but not all the pieces are
1225 // in place for that to work yet. Note we only lie to the lower layer-- we
1226 // don't want to give the app back a swapchain with extra images (which they
1227 // can't actually use!).
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001228 err = native_window_set_buffer_count(window, std::max(2u, num_images));
1229 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001230 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1231 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001232 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001233 }
1234
Ady Abrahamef141462021-08-26 13:12:30 -07001235 // In shared mode the num_images must be one regardless of how many
1236 // buffers were allocated for the buffer queue.
1237 if (swapchain_image_usage & VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID) {
1238 num_images = 1;
1239 }
1240
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001241 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001242 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001243 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001244 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001245 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1246 device, create_info->imageFormat, create_info->imageUsage,
1247 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001248 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001249 if (result != VK_SUCCESS) {
1250 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001251 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001252 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001253 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001254 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001255 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001256 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001257 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001258 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001259 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001260 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001261 if (result != VK_SUCCESS) {
1262 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001263 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001264 }
Jesse Hall70f93352015-11-04 09:41:31 -08001265 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001266 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1267
1268 bool createProtectedSwapchain = false;
1269 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1270 createProtectedSwapchain = true;
1271 native_usage |= BufferUsage::PROTECTED;
1272 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001273 err = native_window_set_usage(window, native_usage);
1274 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001275 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001276 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001277 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001278
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001279 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001280 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1281 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001282 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1283 strerror(-err), err);
1284 return VK_ERROR_SURFACE_LOST_KHR;
1285 }
1286
Jesse Halld7b994a2015-09-07 14:17:37 -07001287 // -- Allocate our Swapchain object --
1288 // After this point, we must deallocate the swapchain on error.
1289
Jesse Hall1f91d392015-12-11 16:28:44 -08001290 void* mem = allocator->pfnAllocation(allocator->pUserData,
1291 sizeof(Swapchain), alignof(Swapchain),
1292 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001293 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001294 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001295 Swapchain* swapchain = new (mem)
1296 Swapchain(surface, num_images, create_info->presentMode,
1297 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001298 // -- Dequeue all buffers and create a VkImage for each --
1299 // Any failures during or after this must cancel the dequeued buffers.
1300
Chris Forbesb56287a2017-01-12 14:28:58 +13001301 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1302#pragma clang diagnostic push
1303#pragma clang diagnostic ignored "-Wold-style-cast"
1304 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1305#pragma clang diagnostic pop
1306 .pNext = nullptr,
1307 .usage = swapchain_image_usage,
1308 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001309 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001310#pragma clang diagnostic push
1311#pragma clang diagnostic ignored "-Wold-style-cast"
1312 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1313#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001314 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001315 };
1316 VkImageCreateInfo image_create = {
1317 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1318 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001319 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001320 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001321 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001322 .extent = {0, 0, 1},
1323 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001324 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001325 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001326 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001327 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001328 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001329 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001330 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1331 };
1332
Jesse Halld7b994a2015-09-07 14:17:37 -07001333 for (uint32_t i = 0; i < num_images; i++) {
1334 Swapchain::Image& img = swapchain->images[i];
1335
1336 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001337 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1338 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001339 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001340 switch (-err) {
1341 case ENOMEM:
1342 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1343 break;
1344 default:
1345 result = VK_ERROR_SURFACE_LOST_KHR;
1346 break;
1347 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001348 break;
1349 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001350 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001351 img.dequeued = true;
1352
1353 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001354 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1355 static_cast<uint32_t>(img.buffer->height),
1356 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001357 image_native_buffer.handle = img.buffer->handle;
1358 image_native_buffer.stride = img.buffer->stride;
1359 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001360 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001361 android_convertGralloc0To1Usage(int(img.buffer->usage),
1362 &image_native_buffer.usage2.producer,
1363 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001364
Yiwei Zhang533cea92019-06-03 18:43:24 -07001365 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001366 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001367 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001368 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001369 if (result != VK_SUCCESS) {
1370 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1371 break;
1372 }
1373 }
1374
1375 // -- Cancel all buffers, returning them to the queue --
1376 // If an error occurred before, also destroy the VkImage and release the
1377 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001378 for (uint32_t i = 0; i < num_images; i++) {
1379 Swapchain::Image& img = swapchain->images[i];
1380 if (img.dequeued) {
1381 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001382 window->cancelBuffer(window, img.buffer.get(),
1383 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001384 img.dequeue_fence = -1;
1385 img.dequeued = false;
1386 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001387 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001388 }
1389
1390 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001391 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1392 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001393 return result;
1394 }
1395
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001396 if (transform_hint != swapchain->pre_transform) {
1397 // Log that the app is not doing pre-rotation.
1398 android::GraphicsEnv::getInstance().setTargetStats(
1399 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1400 }
1401
Jesse Halldc225072016-05-30 22:40:14 -07001402 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1403 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001404 return VK_SUCCESS;
1405}
1406
Jesse Halle1b12782015-11-30 11:27:32 -08001407VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001408void DestroySwapchainKHR(VkDevice device,
1409 VkSwapchainKHR swapchain_handle,
1410 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001411 ATRACE_CALL();
1412
Yiwei Zhang533cea92019-06-03 18:43:24 -07001413 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001414}
1415
Jesse Halle1b12782015-11-30 11:27:32 -08001416VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001417VkResult GetSwapchainImagesKHR(VkDevice,
1418 VkSwapchainKHR swapchain_handle,
1419 uint32_t* count,
1420 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001421 ATRACE_CALL();
1422
Jesse Halld7b994a2015-09-07 14:17:37 -07001423 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001424 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1425 "getting images for non-active swapchain 0x%" PRIx64
1426 "; only dequeued image handles are valid",
1427 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001428 VkResult result = VK_SUCCESS;
1429 if (images) {
1430 uint32_t n = swapchain.num_images;
1431 if (*count < swapchain.num_images) {
1432 n = *count;
1433 result = VK_INCOMPLETE;
1434 }
1435 for (uint32_t i = 0; i < n; i++)
1436 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001437 *count = n;
1438 } else {
1439 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001440 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001441 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001442}
1443
Jesse Halle1b12782015-11-30 11:27:32 -08001444VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001445VkResult AcquireNextImageKHR(VkDevice device,
1446 VkSwapchainKHR swapchain_handle,
1447 uint64_t timeout,
1448 VkSemaphore semaphore,
1449 VkFence vk_fence,
1450 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001451 ATRACE_CALL();
1452
Jesse Halld7b994a2015-09-07 14:17:37 -07001453 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001454 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001455 VkResult result;
1456 int err;
1457
Jesse Halldc225072016-05-30 22:40:14 -07001458 if (swapchain.surface.swapchain_handle != swapchain_handle)
1459 return VK_ERROR_OUT_OF_DATE_KHR;
1460
Chris Forbesc88409c2017-03-30 19:47:37 +13001461 if (swapchain.shared) {
1462 // In shared mode, we keep the buffer dequeued all the time, so we don't
1463 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1464 // the semaphore and fence passed to us will be signalled.
1465 *image_index = 0;
1466 result = GetData(device).driver.AcquireImageANDROID(
1467 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1468 return result;
1469 }
1470
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001471 const nsecs_t acquire_next_image_timeout =
1472 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1473 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1474 // Cache the timeout to avoid the duplicate binder cost.
1475 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1476 acquire_next_image_timeout);
1477 if (err != android::OK) {
1478 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1479 strerror(-err), err);
1480 return VK_ERROR_SURFACE_LOST_KHR;
1481 }
1482 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1483 }
1484
Jesse Halld7b994a2015-09-07 14:17:37 -07001485 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001486 int fence_fd;
1487 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001488 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001489 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1490 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1491 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001492 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001493 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001494 }
1495
1496 uint32_t idx;
1497 for (idx = 0; idx < swapchain.num_images; idx++) {
1498 if (swapchain.images[idx].buffer.get() == buffer) {
1499 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001500 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001501 break;
1502 }
1503 }
1504 if (idx == swapchain.num_images) {
1505 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001506 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001507 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001508 }
1509
1510 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001511 if (fence_fd != -1) {
1512 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001513 if (fence_clone == -1) {
1514 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1515 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001516 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001517 }
1518 }
1519
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001520 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001521 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001522 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001523 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1524 // even if the call fails. We could close it ourselves on failure, but
1525 // that would create a race condition if the driver closes it on a
1526 // failure path: some other thread might create an fd with the same
1527 // number between the time the driver closes it and the time we close
1528 // it. We must assume one of: the driver *always* closes it even on
1529 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001530 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001531 swapchain.images[idx].dequeued = false;
1532 swapchain.images[idx].dequeue_fence = -1;
1533 return result;
1534 }
1535
1536 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001537 return VK_SUCCESS;
1538}
1539
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001540VKAPI_ATTR
1541VkResult AcquireNextImage2KHR(VkDevice device,
1542 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1543 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001544 ATRACE_CALL();
1545
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001546 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1547 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1548 pAcquireInfo->fence, pImageIndex);
1549}
1550
Jesse Halldc225072016-05-30 22:40:14 -07001551static VkResult WorstPresentResult(VkResult a, VkResult b) {
1552 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1553 // (in spec version 1.0.14).
1554 static const VkResult kWorstToBest[] = {
1555 VK_ERROR_DEVICE_LOST,
1556 VK_ERROR_SURFACE_LOST_KHR,
1557 VK_ERROR_OUT_OF_DATE_KHR,
1558 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1559 VK_ERROR_OUT_OF_HOST_MEMORY,
1560 VK_SUBOPTIMAL_KHR,
1561 };
1562 for (auto result : kWorstToBest) {
1563 if (a == result || b == result)
1564 return result;
1565 }
1566 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1567 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1568 return a != VK_SUCCESS ? a : b;
1569}
1570
Jesse Halle1b12782015-11-30 11:27:32 -08001571VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001572VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001573 ATRACE_CALL();
1574
Jesse Halld7b994a2015-09-07 14:17:37 -07001575 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1576 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1577 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001578
Jesse Halldc225072016-05-30 22:40:14 -07001579 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001580 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001581 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001582
Ian Elliottcb351132016-12-13 10:30:40 -07001583 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001584 const VkPresentRegionsKHR* present_regions = nullptr;
1585 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001586 const VkPresentRegionsKHR* next =
1587 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1588 while (next) {
1589 switch (next->sType) {
1590 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1591 present_regions = next;
1592 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001593 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001594 present_times =
1595 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1596 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001597 default:
1598 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1599 next->sType);
1600 break;
1601 }
1602 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1603 }
1604 ALOGV_IF(
1605 present_regions &&
1606 present_regions->swapchainCount != present_info->swapchainCount,
1607 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001608 ALOGV_IF(present_times &&
1609 present_times->swapchainCount != present_info->swapchainCount,
1610 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1611 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001612 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001613 (present_regions) ? present_regions->pRegions : nullptr;
1614 const VkPresentTimeGOOGLE* times =
1615 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001616 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001617 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001618 uint32_t nrects = 0;
1619
Jesse Halld7b994a2015-09-07 14:17:37 -07001620 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1621 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001622 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001623 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001624 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001625 const VkPresentRegionKHR* region =
1626 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001627 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001628 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001629 VkResult result;
1630 int err;
1631
Jesse Halld7b994a2015-09-07 14:17:37 -07001632 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001633 result = dispatch.QueueSignalReleaseImageANDROID(
1634 queue, present_info->waitSemaphoreCount,
1635 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001636 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001637 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001638 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001639 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001640 if (img.release_fence >= 0)
1641 close(img.release_fence);
1642 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001643
Jesse Halldc225072016-05-30 22:40:14 -07001644 if (swapchain.surface.swapchain_handle ==
1645 present_info->pSwapchains[sc]) {
1646 ANativeWindow* window = swapchain.surface.window.get();
1647 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001648 if (region) {
1649 // Process the incremental-present hint for this swapchain:
1650 uint32_t rcount = region->rectangleCount;
1651 if (rcount > nrects) {
1652 android_native_rect_t* new_rects =
1653 static_cast<android_native_rect_t*>(
1654 allocator->pfnReallocation(
1655 allocator->pUserData, rects,
1656 sizeof(android_native_rect_t) * rcount,
1657 alignof(android_native_rect_t),
1658 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1659 if (new_rects) {
1660 rects = new_rects;
1661 nrects = rcount;
1662 } else {
1663 rcount = 0; // Ignore the hint for this swapchain
1664 }
1665 }
1666 for (uint32_t r = 0; r < rcount; ++r) {
1667 if (region->pRectangles[r].layer > 0) {
1668 ALOGV(
1669 "vkQueuePresentKHR ignoring invalid layer "
1670 "(%u); using layer 0 instead",
1671 region->pRectangles[r].layer);
1672 }
1673 int x = region->pRectangles[r].offset.x;
1674 int y = region->pRectangles[r].offset.y;
1675 int width = static_cast<int>(
1676 region->pRectangles[r].extent.width);
1677 int height = static_cast<int>(
1678 region->pRectangles[r].extent.height);
1679 android_native_rect_t* cur_rect = &rects[r];
1680 cur_rect->left = x;
1681 cur_rect->top = y + height;
1682 cur_rect->right = x + width;
1683 cur_rect->bottom = y;
1684 }
1685 native_window_set_surface_damage(window, rects, rcount);
1686 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001687 if (time) {
1688 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001689 ALOGV(
1690 "Calling "
1691 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001692 native_window_enable_frame_timestamps(window, true);
1693 swapchain.frame_timestamps_enabled = true;
1694 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001695
1696 // Record the nativeFrameId so it can be later correlated to
1697 // this present.
1698 uint64_t nativeFrameId = 0;
1699 err = native_window_get_next_frame_id(
1700 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001701 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001702 ALOGE("Failed to get next native frame ID.");
1703 }
1704
1705 // Add a new timing record with the user's presentID and
1706 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001707 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001708 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001709 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001710 }
1711 if (time->desiredPresentTime) {
1712 // Set the desiredPresentTime:
1713 ALOGV(
1714 "Calling "
1715 "native_window_set_buffers_timestamp(%" PRId64 ")",
1716 time->desiredPresentTime);
1717 native_window_set_buffers_timestamp(
1718 window,
1719 static_cast<int64_t>(time->desiredPresentTime));
1720 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001721 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001722
Jesse Halldc225072016-05-30 22:40:14 -07001723 err = window->queueBuffer(window, img.buffer.get(), fence);
1724 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001725 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001726 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1727 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001728 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001729 } else {
1730 if (img.dequeue_fence >= 0) {
1731 close(img.dequeue_fence);
1732 img.dequeue_fence = -1;
1733 }
1734 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001735 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001736
1737 // If the swapchain is in shared mode, immediately dequeue the
1738 // buffer so it can be presented again without an intervening
1739 // call to AcquireNextImageKHR. We expect to get the same buffer
1740 // back from every call to dequeueBuffer in this mode.
1741 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1742 ANativeWindowBuffer* buffer;
1743 int fence_fd;
1744 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001745 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001746 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1747 swapchain_result = WorstPresentResult(swapchain_result,
1748 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001749 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001750 ALOGE("got wrong image back for shared swapchain");
1751 swapchain_result = WorstPresentResult(swapchain_result,
1752 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001753 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13001754 img.dequeue_fence = fence_fd;
1755 img.dequeued = true;
1756 }
1757 }
Jesse Halldc225072016-05-30 22:40:14 -07001758 }
1759 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001760 OrphanSwapchain(device, &swapchain);
1761 }
silence_dogood73597592019-05-23 16:57:37 -07001762 int window_transform_hint;
1763 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1764 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001765 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07001766 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1767 strerror(-err), err);
1768 swapchain_result = WorstPresentResult(
1769 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1770 }
1771 if (swapchain.pre_transform != window_transform_hint) {
1772 swapchain_result =
1773 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1774 }
Jesse Halldc225072016-05-30 22:40:14 -07001775 } else {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001776 ReleaseSwapchainImage(device, nullptr, fence, img, true);
Jesse Halldc225072016-05-30 22:40:14 -07001777 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001778 }
1779
Jesse Halla9e57032015-11-30 01:03:10 -08001780 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001781 present_info->pResults[sc] = swapchain_result;
1782
1783 if (swapchain_result != final_result)
1784 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001785 }
Ian Elliottcb351132016-12-13 10:30:40 -07001786 if (rects) {
1787 allocator->pfnFree(allocator->pUserData, rects);
1788 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001789
1790 return final_result;
1791}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001792
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001793VKAPI_ATTR
1794VkResult GetRefreshCycleDurationGOOGLE(
1795 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001796 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001797 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001798 ATRACE_CALL();
1799
Ian Elliott62c48c92017-01-20 13:13:20 -07001800 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001801 VkResult result = VK_SUCCESS;
1802
Ian Elliott3568bfe2019-05-03 15:54:46 -06001803 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001804
1805 return result;
1806}
1807
1808VKAPI_ATTR
1809VkResult GetPastPresentationTimingGOOGLE(
1810 VkDevice,
1811 VkSwapchainKHR swapchain_handle,
1812 uint32_t* count,
1813 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001814 ATRACE_CALL();
1815
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001816 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001817 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1818 return VK_ERROR_OUT_OF_DATE_KHR;
1819 }
1820
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001821 ANativeWindow* window = swapchain.surface.window.get();
1822 VkResult result = VK_SUCCESS;
1823
1824 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001825 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001826 native_window_enable_frame_timestamps(window, true);
1827 swapchain.frame_timestamps_enabled = true;
1828 }
1829
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001830 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07001831 // Get the latest ready timing count before copying, since the copied
1832 // timing info will be erased in copy_ready_timings function.
1833 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07001834 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001835 // Check the *count here against the recorded ready timing count, since
1836 // *count can be overwritten per spec describes.
1837 if (*count < n) {
1838 result = VK_INCOMPLETE;
1839 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001840 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001841 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001842 }
1843
1844 return result;
1845}
1846
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001847VKAPI_ATTR
1848VkResult GetSwapchainStatusKHR(
1849 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001850 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001851 ATRACE_CALL();
1852
Chris Forbes4e18ba82017-01-20 12:50:17 +13001853 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001854 VkResult result = VK_SUCCESS;
1855
Chris Forbes4e18ba82017-01-20 12:50:17 +13001856 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1857 return VK_ERROR_OUT_OF_DATE_KHR;
1858 }
1859
Yiwei Zhanga885c062019-10-24 12:07:57 -07001860 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001861
1862 return result;
1863}
1864
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001865VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001866 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001867 uint32_t swapchainCount,
1868 const VkSwapchainKHR* pSwapchains,
1869 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001870 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001871
1872 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1873 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1874 if (!swapchain)
1875 continue;
1876
1877 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1878
1879 ANativeWindow* window = swapchain->surface.window.get();
1880
1881 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1882 const android_smpte2086_metadata smpteMetdata = {
1883 {vulkanMetadata.displayPrimaryRed.x,
1884 vulkanMetadata.displayPrimaryRed.y},
1885 {vulkanMetadata.displayPrimaryGreen.x,
1886 vulkanMetadata.displayPrimaryGreen.y},
1887 {vulkanMetadata.displayPrimaryBlue.x,
1888 vulkanMetadata.displayPrimaryBlue.y},
1889 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1890 vulkanMetadata.maxLuminance,
1891 vulkanMetadata.minLuminance};
1892 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1893
1894 const android_cta861_3_metadata cta8613Metadata = {
1895 vulkanMetadata.maxContentLightLevel,
1896 vulkanMetadata.maxFrameAverageLightLevel};
1897 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1898 }
1899
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001900 return;
1901}
1902
Yiwei Zhang0f475222019-04-11 19:38:00 -07001903static void InterceptBindImageMemory2(
1904 uint32_t bind_info_count,
1905 const VkBindImageMemoryInfo* bind_infos,
1906 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1907 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1908 out_native_buffers->clear();
1909 out_bind_infos->clear();
1910
1911 if (!bind_info_count)
1912 return;
1913
1914 std::unordered_set<uint32_t> intercepted_indexes;
1915
1916 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1917 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1918 bind_infos[idx].pNext);
1919 while (info &&
1920 info->sType !=
1921 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1922 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1923 info->pNext);
1924 }
1925
1926 if (!info)
1927 continue;
1928
1929 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1930 "swapchain handle must not be NULL");
1931 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1932 ALOG_ASSERT(
1933 info->imageIndex < swapchain->num_images,
1934 "imageIndex must be less than the number of images in swapchain");
1935
1936 ANativeWindowBuffer* buffer =
1937 swapchain->images[info->imageIndex].buffer.get();
1938 VkNativeBufferANDROID native_buffer = {
1939#pragma clang diagnostic push
1940#pragma clang diagnostic ignored "-Wold-style-cast"
1941 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1942#pragma clang diagnostic pop
1943 .pNext = bind_infos[idx].pNext,
1944 .handle = buffer->handle,
1945 .stride = buffer->stride,
1946 .format = buffer->format,
1947 .usage = int(buffer->usage),
1948 };
1949 // Reserve enough space to avoid letting re-allocation invalidate the
1950 // addresses of the elements inside.
1951 out_native_buffers->reserve(bind_info_count);
1952 out_native_buffers->emplace_back(native_buffer);
1953
1954 // Reserve the space now since we know how much is needed now.
1955 out_bind_infos->reserve(bind_info_count);
1956 out_bind_infos->emplace_back(bind_infos[idx]);
1957 out_bind_infos->back().pNext = &out_native_buffers->back();
1958
1959 intercepted_indexes.insert(idx);
1960 }
1961
1962 if (intercepted_indexes.empty())
1963 return;
1964
1965 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1966 if (intercepted_indexes.count(idx))
1967 continue;
1968 out_bind_infos->emplace_back(bind_infos[idx]);
1969 }
1970}
1971
Yiwei Zhang23143102019-04-10 18:24:05 -07001972VKAPI_ATTR
1973VkResult BindImageMemory2(VkDevice device,
1974 uint32_t bindInfoCount,
1975 const VkBindImageMemoryInfo* pBindInfos) {
1976 ATRACE_CALL();
1977
Yiwei Zhang0f475222019-04-11 19:38:00 -07001978 // out_native_buffers is for maintaining the lifecycle of the constructed
1979 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1980 std::vector<VkNativeBufferANDROID> out_native_buffers;
1981 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1982 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1983 &out_bind_infos);
1984 return GetData(device).driver.BindImageMemory2(
1985 device, bindInfoCount,
1986 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001987}
1988
1989VKAPI_ATTR
1990VkResult BindImageMemory2KHR(VkDevice device,
1991 uint32_t bindInfoCount,
1992 const VkBindImageMemoryInfo* pBindInfos) {
1993 ATRACE_CALL();
1994
Yiwei Zhang0f475222019-04-11 19:38:00 -07001995 std::vector<VkNativeBufferANDROID> out_native_buffers;
1996 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1997 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1998 &out_bind_infos);
1999 return GetData(device).driver.BindImageMemory2KHR(
2000 device, bindInfoCount,
2001 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002002}
2003
Chia-I Wu62262232016-03-26 07:06:44 +08002004} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002005} // namespace vulkan