blob: 48090af51397598984afe6fc91dc45b23de370bc [file] [log] [blame]
Jesse Hallb1352bc2015-09-04 16:12:33 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang0f475222019-04-11 19:38:00 -070019#include <android/hardware/graphics/common/1.0/types.h>
Jesse Hall79927812017-03-23 11:03:23 -070020#include <grallocusage/GrallocUsageConversion.h>
Yiwei Zhang69395cd2019-07-03 16:55:39 -070021#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070023#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070024#include <system/window.h>
25#include <ui/BufferQueueDefs.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080026#include <utils/StrongPointer.h>
Yiwei Zhang705c2e62019-12-18 23:12:43 -080027#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080028#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070029
30#include <algorithm>
31#include <unordered_set>
32#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070033
Chia-I Wu4a6a9162016-03-26 07:17:34 +080034#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070035
Daniel Kochf25f5bb2017-10-05 00:26:58 -040036using android::hardware::graphics::common::V1_0::BufferUsage;
37
Chia-I Wu62262232016-03-26 07:06:44 +080038namespace vulkan {
39namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070040
Jesse Halld7b994a2015-09-07 14:17:37 -070041namespace {
42
Jesse Hall55bc0972016-02-23 16:43:29 -080043const VkSurfaceTransformFlagsKHR kSupportedTransforms =
44 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
45 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
46 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
47 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070048 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
49 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
50 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
51 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080052 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
53
54VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
55 // Native and Vulkan transforms are isomorphic, but are represented
56 // differently. Vulkan transforms are built up of an optional horizontal
57 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
58 // transforms are built up from a horizontal flip, vertical flip, and
59 // 90-degree rotation, all optional but always in that order.
60
Jesse Hall55bc0972016-02-23 16:43:29 -080061 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070062 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080063 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070064 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
65 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
66 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
67 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
68 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080069 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070070 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080071 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070072 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
73 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
74 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
75 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
76 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -080077 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
78 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
79 default:
80 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
81 }
82}
83
Yiwei Zhang70a21962019-05-31 17:26:52 -070084int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
85 switch (transform) {
86 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
87 return NATIVE_WINDOW_TRANSFORM_ROT_90;
88 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
89 return NATIVE_WINDOW_TRANSFORM_ROT_180;
90 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
91 return NATIVE_WINDOW_TRANSFORM_ROT_270;
92 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
93 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
94 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
95 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
96 NATIVE_WINDOW_TRANSFORM_ROT_90;
97 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
98 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
99 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
100 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
101 NATIVE_WINDOW_TRANSFORM_ROT_90;
102 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
103 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
104 default:
105 return 0;
106 }
107}
108
Jesse Hall178b6962016-02-24 15:39:50 -0800109int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
110 switch (transform) {
111 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
112 return NATIVE_WINDOW_TRANSFORM_ROT_270;
113 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
114 return NATIVE_WINDOW_TRANSFORM_ROT_180;
115 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
116 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700117 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
118 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
119 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
120 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
121 NATIVE_WINDOW_TRANSFORM_ROT_90;
122 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
123 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
124 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
125 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
126 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800127 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
128 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
129 default:
130 return 0;
131 }
132}
133
Ian Elliott8a977262017-01-19 09:05:58 -0700134class TimingInfo {
135 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800136 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700137 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800138 native_frame_id_(nativeFrameId) {}
139 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700140 return (timestamp_desired_present_time_ !=
141 NATIVE_WINDOW_TIMESTAMP_PENDING &&
142 timestamp_actual_present_time_ !=
143 NATIVE_WINDOW_TIMESTAMP_PENDING &&
144 timestamp_render_complete_time_ !=
145 NATIVE_WINDOW_TIMESTAMP_PENDING &&
146 timestamp_composition_latch_time_ !=
147 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700148 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700149 void calculate(int64_t rdur) {
150 bool anyTimestampInvalid =
151 (timestamp_actual_present_time_ ==
152 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
153 (timestamp_render_complete_time_ ==
154 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
155 (timestamp_composition_latch_time_ ==
156 NATIVE_WINDOW_TIMESTAMP_INVALID);
157 if (anyTimestampInvalid) {
158 ALOGE("Unexpectedly received invalid timestamp.");
159 vals_.actualPresentTime = 0;
160 vals_.earliestPresentTime = 0;
161 vals_.presentMargin = 0;
162 return;
163 }
164
165 vals_.actualPresentTime =
166 static_cast<uint64_t>(timestamp_actual_present_time_);
167 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700168 timestamp_render_complete_time_);
169 // Calculate vals_.earliestPresentTime, and potentially adjust
170 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
171 // is vals_.actualPresentTime. If we can subtract rdur (the duration
172 // of a refresh cycle) from vals_.earliestPresentTime (and also from
173 // vals_.presentMargin) and still leave a positive margin, then we can
174 // report to the application that it could have presented earlier than
175 // it did (per the extension specification). If for some reason, we
176 // can do this subtraction repeatedly, we do, since
177 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700178 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700179 while ((margin > rdur) &&
180 ((early_time - rdur) > timestamp_composition_latch_time_)) {
181 early_time -= rdur;
182 margin -= rdur;
183 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700184 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
185 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700186 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800187 void get_values(VkPastPresentationTimingGOOGLE* values) const {
188 *values = vals_;
189 }
Ian Elliott8a977262017-01-19 09:05:58 -0700190
191 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800192 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700193
Brian Anderson1049d1d2016-12-16 17:25:57 -0800194 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700195 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
196 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
197 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
198 int64_t timestamp_composition_latch_time_
199 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700200};
201
Jesse Hall1356b0d2015-11-23 17:24:58 -0800202struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800203 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700204 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700205 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800206};
207
208VkSurfaceKHR HandleFromSurface(Surface* surface) {
209 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
210}
211
212Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800213 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800214}
215
Ian Elliott8a977262017-01-19 09:05:58 -0700216// Maximum number of TimingInfo structs to keep per swapchain:
217enum { MAX_TIMING_INFOS = 10 };
218// Minimum number of frames to look for in the past (so we don't cause
219// syncronous requests to Surface Flinger):
220enum { MIN_NUM_FRAMES_AGO = 5 };
221
Jesse Hall1356b0d2015-11-23 17:24:58 -0800222struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700223 Swapchain(Surface& surface_,
224 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700225 VkPresentModeKHR present_mode,
226 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700227 : surface(surface_),
228 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700229 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700230 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300231 frame_timestamps_enabled(false),
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800232 acquire_next_image_timeout(-1),
Chris Forbesf8835642017-03-30 19:31:40 +1300233 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800234 present_mode ==
235 VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700236 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700237 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700238 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700239 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700240 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600241 uint64_t get_refresh_duration()
242 {
243 ANativeWindow* window = surface.window.get();
244 native_window_get_refresh_cycle_duration(
245 window,
246 &refresh_duration);
247 return static_cast<uint64_t>(refresh_duration);
248
249 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800250
251 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700252 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700253 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700254 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700255 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700256 int64_t refresh_duration;
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800257 nsecs_t acquire_next_image_timeout;
Chris Forbesf8835642017-03-30 19:31:40 +1300258 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700259
260 struct Image {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700261 Image()
262 : image(VK_NULL_HANDLE),
263 dequeue_fence(-1),
264 release_fence(-1),
265 dequeued(false) {}
Jesse Halld7b994a2015-09-07 14:17:37 -0700266 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800267 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700268 // The fence is only valid when the buffer is dequeued, and should be
269 // -1 any other time. When valid, we own the fd, and must ensure it is
270 // closed: either by closing it explicitly when queueing the buffer,
271 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
272 int dequeue_fence;
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700273 // This fence is a dup of the sync fd returned from the driver via
274 // vkQueueSignalReleaseImageANDROID upon vkQueuePresentKHR. We must
275 // ensure it is closed upon re-presenting or releasing the image.
276 int release_fence;
Jesse Halld7b994a2015-09-07 14:17:37 -0700277 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800278 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700279
Yiwei Zhang5e862202019-06-21 14:59:16 -0700280 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700281};
282
283VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
284 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
285}
286
287Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800288 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700289}
290
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700291static bool IsFencePending(int fd) {
292 if (fd < 0)
293 return false;
294
295 errno = 0;
296 return sync_wait(fd, 0 /* timeout */) == -1 && errno == ETIME;
297}
298
Jesse Halldc225072016-05-30 22:40:14 -0700299void ReleaseSwapchainImage(VkDevice device,
300 ANativeWindow* window,
301 int release_fence,
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700302 Swapchain::Image& image,
303 bool defer_if_pending) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700304 ATRACE_CALL();
305
Jesse Halldc225072016-05-30 22:40:14 -0700306 ALOG_ASSERT(release_fence == -1 || image.dequeued,
307 "ReleaseSwapchainImage: can't provide a release fence for "
308 "non-dequeued images");
309
310 if (image.dequeued) {
311 if (release_fence >= 0) {
312 // We get here from vkQueuePresentKHR. The application is
313 // responsible for creating an execution dependency chain from
314 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
315 // (release_fence), so we can drop the dequeue_fence here.
316 if (image.dequeue_fence >= 0)
317 close(image.dequeue_fence);
318 } else {
319 // We get here during swapchain destruction, or various serious
320 // error cases e.g. when we can't create the release_fence during
321 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
322 // have already signalled, since the swapchain images are supposed
323 // to be idle before the swapchain is destroyed. In error cases,
324 // there may be rendering in flight to the image, but since we
325 // weren't able to create a release_fence, waiting for the
326 // dequeue_fence is about the best we can do.
327 release_fence = image.dequeue_fence;
328 }
329 image.dequeue_fence = -1;
330
331 if (window) {
332 window->cancelBuffer(window, image.buffer.get(), release_fence);
333 } else {
334 if (release_fence >= 0) {
335 sync_wait(release_fence, -1 /* forever */);
336 close(release_fence);
337 }
338 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700339 release_fence = -1;
Jesse Halldc225072016-05-30 22:40:14 -0700340 image.dequeued = false;
341 }
342
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700343 if (defer_if_pending && IsFencePending(image.release_fence))
344 return;
345
346 if (image.release_fence >= 0) {
347 close(image.release_fence);
348 image.release_fence = -1;
349 }
350
Jesse Halldc225072016-05-30 22:40:14 -0700351 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700352 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700353 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700354 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700355 image.image = VK_NULL_HANDLE;
356 }
357
358 image.buffer.clear();
359}
360
361void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
362 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
363 return;
Jesse Halldc225072016-05-30 22:40:14 -0700364 for (uint32_t i = 0; i < swapchain->num_images; i++) {
365 if (!swapchain->images[i].dequeued)
Yiwei Zhang9df5bff2020-09-22 10:08:03 -0700366 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i],
367 true);
Jesse Halldc225072016-05-30 22:40:14 -0700368 }
369 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700370 swapchain->timing.clear();
371}
372
373uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800374 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
375 return 0;
376 }
Ian Elliott8a977262017-01-19 09:05:58 -0700377
Brian Anderson1049d1d2016-12-16 17:25:57 -0800378 uint32_t num_ready = 0;
379 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
380 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700381 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800382 if (ti.ready()) {
383 // This TimingInfo is ready to be reported to the user. Add it
384 // to the num_ready.
385 num_ready++;
386 continue;
387 }
388 // This TimingInfo is not yet ready to be reported to the user,
389 // and so we should look for any available timestamps that
390 // might make it ready.
391 int64_t desired_present_time = 0;
392 int64_t render_complete_time = 0;
393 int64_t composition_latch_time = 0;
394 int64_t actual_present_time = 0;
395 // Obtain timestamps:
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800396 int err = native_window_get_frame_timestamps(
Brian Anderson1049d1d2016-12-16 17:25:57 -0800397 swapchain.surface.window.get(), ti.native_frame_id_,
398 &desired_present_time, &render_complete_time,
399 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700400 nullptr, //&first_composition_start_time,
401 nullptr, //&last_composition_start_time,
402 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800403 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700404 nullptr, //&dequeue_ready_time,
405 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800406
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800407 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800408 continue;
409 }
410
411 // Record the timestamp(s) we received, and then see if this TimingInfo
412 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700413 ti.timestamp_desired_present_time_ = desired_present_time;
414 ti.timestamp_actual_present_time_ = actual_present_time;
415 ti.timestamp_render_complete_time_ = render_complete_time;
416 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800417
418 if (ti.ready()) {
419 // The TimingInfo has received enough timestamps, and should now
420 // use those timestamps to calculate the info that should be
421 // reported to the user:
422 ti.calculate(swapchain.refresh_duration);
423 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700424 }
425 }
426 return num_ready;
427}
428
Ian Elliott8a977262017-01-19 09:05:58 -0700429void copy_ready_timings(Swapchain& swapchain,
430 uint32_t* count,
431 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800432 if (swapchain.timing.empty()) {
433 *count = 0;
434 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700435 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800436
437 size_t last_ready = swapchain.timing.size() - 1;
438 while (!swapchain.timing[last_ready].ready()) {
439 if (last_ready == 0) {
440 *count = 0;
441 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700442 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800443 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700444 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800445
446 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700447 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800448 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
449 const TimingInfo& ti = swapchain.timing[i];
450 if (ti.ready()) {
451 ti.get_values(&timings[num_copied]);
452 num_copied++;
453 }
454 num_to_remove++;
455 }
456
457 // Discard old frames that aren't ready if newer frames are ready.
458 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700459 swapchain.timing.erase(swapchain.timing.begin(),
460 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800461
Ian Elliott8a977262017-01-19 09:05:58 -0700462 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700463}
464
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700465android_pixel_format GetNativePixelFormat(VkFormat format) {
466 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
467 switch (format) {
468 case VK_FORMAT_R8G8B8A8_UNORM:
469 case VK_FORMAT_R8G8B8A8_SRGB:
470 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
471 break;
472 case VK_FORMAT_R5G6B5_UNORM_PACK16:
473 native_format = HAL_PIXEL_FORMAT_RGB_565;
474 break;
475 case VK_FORMAT_R16G16B16A16_SFLOAT:
476 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
477 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800478 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700479 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
480 break;
481 default:
482 ALOGV("unsupported swapchain format %d", format);
483 break;
484 }
485 return native_format;
486}
487
488android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
489 switch (colorspace) {
490 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
491 return HAL_DATASPACE_V0_SRGB;
492 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
493 return HAL_DATASPACE_DISPLAY_P3;
494 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
495 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600496 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
497 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700498 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
499 return HAL_DATASPACE_DCI_P3_LINEAR;
500 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
501 return HAL_DATASPACE_DCI_P3;
502 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
503 return HAL_DATASPACE_V0_SRGB_LINEAR;
504 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
505 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600506 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
507 return HAL_DATASPACE_BT2020_LINEAR;
508 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700509 return static_cast<android_dataspace>(
510 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
511 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600512 case VK_COLOR_SPACE_DOLBYVISION_EXT:
513 return static_cast<android_dataspace>(
514 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
515 HAL_DATASPACE_RANGE_FULL);
516 case VK_COLOR_SPACE_HDR10_HLG_EXT:
517 return static_cast<android_dataspace>(
518 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
519 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700520 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
521 return static_cast<android_dataspace>(
522 HAL_DATASPACE_STANDARD_ADOBE_RGB |
523 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
524 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
525 return HAL_DATASPACE_ADOBE_RGB;
526
527 // Pass through is intended to allow app to provide data that is passed
528 // to the display system without modification.
529 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
530 return HAL_DATASPACE_ARBITRARY;
531
532 default:
533 // This indicates that we don't know about the
534 // dataspace specified and we should indicate that
535 // it's unsupported
536 return HAL_DATASPACE_UNKNOWN;
537 }
538}
539
Jesse Halld7b994a2015-09-07 14:17:37 -0700540} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700541
Jesse Halle1b12782015-11-30 11:27:32 -0800542VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800543VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800544 VkInstance instance,
545 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
546 const VkAllocationCallbacks* allocator,
547 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800548 ATRACE_CALL();
549
Jesse Hall1f91d392015-12-11 16:28:44 -0800550 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800551 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800552 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
553 alignof(Surface),
554 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800555 if (!mem)
556 return VK_ERROR_OUT_OF_HOST_MEMORY;
557 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700558
Chia-I Wue8e689f2016-04-18 08:21:31 +0800559 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700560 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700561 int err = native_window_get_consumer_usage(surface->window.get(),
562 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800563 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700564 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
565 strerror(-err), err);
566 surface->~Surface();
567 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700568 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700569 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700570
Yiwei Zhang6435b322018-05-08 11:12:17 -0700571 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800572 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800573 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800574 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
575 err);
576 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800577 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700578 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800579 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700580
Jesse Hall1356b0d2015-11-23 17:24:58 -0800581 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700582 return VK_SUCCESS;
583}
584
Jesse Halle1b12782015-11-30 11:27:32 -0800585VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800586void DestroySurfaceKHR(VkInstance instance,
587 VkSurfaceKHR surface_handle,
588 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800589 ATRACE_CALL();
590
Jesse Hall1356b0d2015-11-23 17:24:58 -0800591 Surface* surface = SurfaceFromHandle(surface_handle);
592 if (!surface)
593 return;
594 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700595 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700596 "destroyed VkSurfaceKHR 0x%" PRIx64
597 " has active VkSwapchainKHR 0x%" PRIx64,
598 reinterpret_cast<uint64_t>(surface_handle),
599 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800600 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800601 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800602 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800603 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800604}
605
Jesse Halle1b12782015-11-30 11:27:32 -0800606VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800607VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
608 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700609 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800610 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800611 ATRACE_CALL();
612
Yiwei Zhang6435b322018-05-08 11:12:17 -0700613 const Surface* surface = SurfaceFromHandle(surface_handle);
614 if (!surface) {
615 return VK_ERROR_SURFACE_LOST_KHR;
616 }
617 const ANativeWindow* window = surface->window.get();
618
619 int query_value;
620 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800621 if (err != android::OK || query_value < 0) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700622 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
623 strerror(-err), err, query_value);
624 return VK_ERROR_SURFACE_LOST_KHR;
625 }
626
627 android_pixel_format native_format =
628 static_cast<android_pixel_format>(query_value);
629
630 bool format_supported = false;
631 switch (native_format) {
632 case HAL_PIXEL_FORMAT_RGBA_8888:
633 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700634 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800635 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700636 format_supported = true;
637 break;
638 default:
639 break;
640 }
641
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700642 *supported = static_cast<VkBool32>(
643 format_supported || (surface->consumer_usage &
644 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
645 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700646
Jesse Halla6429252015-11-29 18:59:42 -0800647 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800648}
649
Jesse Halle1b12782015-11-30 11:27:32 -0800650VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800651VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800652 VkPhysicalDevice /*pdev*/,
653 VkSurfaceKHR surface,
654 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800655 ATRACE_CALL();
656
Jesse Halld7b994a2015-09-07 14:17:37 -0700657 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800658 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700659
660 int width, height;
661 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800662 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700663 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
664 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700665 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700666 }
667 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800668 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700669 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
670 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700671 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700672 }
673
Jesse Hall55bc0972016-02-23 16:43:29 -0800674 int transform_hint;
675 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800676 if (err != android::OK) {
Jesse Hall55bc0972016-02-23 16:43:29 -0800677 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
678 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700679 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800680 }
681
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800682 int max_buffer_count;
683 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800684 if (err != android::OK) {
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800685 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
686 strerror(-err), err);
687 return VK_ERROR_SURFACE_LOST_KHR;
688 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700689 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800690 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700691
Jesse Hallfe2662d2016-02-09 13:26:59 -0800692 capabilities->currentExtent =
693 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
694
Yiwei Zhang70a21962019-05-31 17:26:52 -0700695 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800696 capabilities->minImageExtent = VkExtent2D{1, 1};
697 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700698
Jesse Hallfe2662d2016-02-09 13:26:59 -0800699 capabilities->maxImageArrayLayers = 1;
700
Jesse Hall55bc0972016-02-23 16:43:29 -0800701 capabilities->supportedTransforms = kSupportedTransforms;
702 capabilities->currentTransform =
703 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700704
Jesse Hallfe2662d2016-02-09 13:26:59 -0800705 // On Android, window composition is a WindowManager property, not something
706 // associated with the bufferqueue. It can't be changed from here.
707 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700708
Jesse Hallb00daad2015-11-29 19:46:20 -0800709 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800710 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
711 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
712 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700713 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
714
Jesse Hallb1352bc2015-09-04 16:12:33 -0700715 return VK_SUCCESS;
716}
717
Jesse Halle1b12782015-11-30 11:27:32 -0800718VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700719VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
720 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800721 uint32_t* count,
722 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800723 ATRACE_CALL();
724
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700725 const InstanceData& instance_data = GetData(pdev);
726
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700727 bool wide_color_support = false;
728 Surface& surface = *SurfaceFromHandle(surface_handle);
729 int err = native_window_get_wide_color_support(surface.window.get(),
730 &wide_color_support);
731 if (err) {
Yiwei Zhang70a21962019-05-31 17:26:52 -0700732 return VK_ERROR_SURFACE_LOST_KHR;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700733 }
734 ALOGV("wide_color_support is: %d", wide_color_support);
735 wide_color_support =
736 wide_color_support &&
737 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
738
Charlie Lao324191f2019-12-13 11:03:16 -0800739 AHardwareBuffer_Desc desc = {};
740 desc.width = 1;
741 desc.height = 1;
742 desc.layers = 1;
743 desc.usage = surface.consumer_usage |
744 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
745 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
746
747 // We must support R8G8B8A8
748 std::vector<VkSurfaceFormatKHR> all_formats = {
749 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
750 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}};
751
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700752 if (wide_color_support) {
Charlie Lao324191f2019-12-13 11:03:16 -0800753 all_formats.emplace_back(VkSurfaceFormatKHR{
754 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
755 all_formats.emplace_back(VkSurfaceFormatKHR{
756 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
757 }
758
759 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
760 if (AHardwareBuffer_isSupported(&desc)) {
761 all_formats.emplace_back(VkSurfaceFormatKHR{
762 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
763 }
764
765 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
766 if (AHardwareBuffer_isSupported(&desc)) {
767 all_formats.emplace_back(VkSurfaceFormatKHR{
768 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
769 if (wide_color_support) {
770 all_formats.emplace_back(
771 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
772 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
773 all_formats.emplace_back(
774 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
775 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
776 }
777 }
778
779 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
780 if (AHardwareBuffer_isSupported(&desc)) {
781 all_formats.emplace_back(
782 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
783 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
784 if (wide_color_support) {
785 all_formats.emplace_back(
786 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
787 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
788 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700789 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700790
791 VkResult result = VK_SUCCESS;
792 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800793 uint32_t transfer_count = all_formats.size();
794 if (transfer_count > *count) {
795 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700796 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700797 }
Charlie Lao324191f2019-12-13 11:03:16 -0800798 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
799 formats);
800 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700801 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800802 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700803 }
Charlie Lao324191f2019-12-13 11:03:16 -0800804
Jesse Halld7b994a2015-09-07 14:17:37 -0700805 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700806}
807
Jesse Halle1b12782015-11-30 11:27:32 -0800808VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300809VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
810 VkPhysicalDevice physicalDevice,
811 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
812 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800813 ATRACE_CALL();
814
Chris Forbes2452cf72017-03-16 16:30:17 +1300815 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
816 physicalDevice, pSurfaceInfo->surface,
817 &pSurfaceCapabilities->surfaceCapabilities);
818
Chris Forbes06bc0092017-03-16 16:46:05 +1300819 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
820 while (caps->pNext) {
821 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
822
823 switch (caps->sType) {
824 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
825 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
826 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
827 caps);
828 // Claim same set of usage flags are supported for
829 // shared present modes as for other modes.
830 shared_caps->sharedPresentSupportedUsageFlags =
831 pSurfaceCapabilities->surfaceCapabilities
832 .supportedUsageFlags;
833 } break;
834
835 default:
836 // Ignore all other extension structs
837 break;
838 }
839 }
840
Chris Forbes2452cf72017-03-16 16:30:17 +1300841 return result;
842}
843
844VKAPI_ATTR
845VkResult GetPhysicalDeviceSurfaceFormats2KHR(
846 VkPhysicalDevice physicalDevice,
847 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
848 uint32_t* pSurfaceFormatCount,
849 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800850 ATRACE_CALL();
851
Chris Forbes2452cf72017-03-16 16:30:17 +1300852 if (!pSurfaceFormats) {
853 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
854 pSurfaceInfo->surface,
855 pSurfaceFormatCount, nullptr);
856 } else {
857 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
858 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700859 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300860 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
861 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700862 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300863
864 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
865 // marshal results individually due to stride difference.
866 // completely ignore any chained extension structs.
867 uint32_t formats_to_marshal = *pSurfaceFormatCount;
868 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
869 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
870 }
871 }
872
873 return result;
874 }
875}
876
877VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300878VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800879 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800880 uint32_t* count,
881 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800882 ATRACE_CALL();
883
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800884 int err;
885 int query_value;
886 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
887
888 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800889 if (err != android::OK || query_value < 0) {
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800890 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
891 strerror(-err), err, query_value);
892 return VK_ERROR_SURFACE_LOST_KHR;
893 }
894 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
895
896 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800897 if (err != android::OK || query_value < 0) {
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800898 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
899 strerror(-err), err, query_value);
900 return VK_ERROR_SURFACE_LOST_KHR;
901 }
902 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
903
Yiwei Zhang5e862202019-06-21 14:59:16 -0700904 std::vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800905 if (min_undequeued_buffers + 1 < max_buffer_count)
906 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300907 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
908
909 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700910 QueryPresentationProperties(pdev, &present_properties);
911 if (present_properties.sharedImage) {
912 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
913 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300914 }
915
916 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700917
918 VkResult result = VK_SUCCESS;
919 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300920 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700921 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300922 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -0700923 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700924 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300925 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700926 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700927 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700928}
929
Jesse Halle1b12782015-11-30 11:27:32 -0800930VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400931VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600932 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400933 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800934 ATRACE_CALL();
935
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400936 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
937 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
938 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
939 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
940 pDeviceGroupPresentCapabilities->sType);
941
942 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
943 sizeof(pDeviceGroupPresentCapabilities->presentMask));
944
945 // assume device group of size 1
946 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
947 pDeviceGroupPresentCapabilities->modes =
948 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
949
950 return VK_SUCCESS;
951}
952
953VKAPI_ATTR
954VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600955 VkDevice,
956 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400957 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800958 ATRACE_CALL();
959
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400960 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
961 return VK_SUCCESS;
962}
963
964VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600965VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400966 VkSurfaceKHR surface,
967 uint32_t* pRectCount,
968 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800969 ATRACE_CALL();
970
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400971 if (!pRects) {
972 *pRectCount = 1;
973 } else {
974 uint32_t count = std::min(*pRectCount, 1u);
975 bool incomplete = *pRectCount < 1;
976
977 *pRectCount = count;
978
979 if (incomplete) {
980 return VK_INCOMPLETE;
981 }
982
983 int err;
984 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
985
986 int width = 0, height = 0;
987 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800988 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400989 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
990 strerror(-err), err);
991 }
992 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800993 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400994 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
995 strerror(-err), err);
996 }
997
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400998 pRects[0].offset.x = 0;
999 pRects[0].offset.y = 0;
1000 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
1001 static_cast<uint32_t>(height)};
1002 }
1003 return VK_SUCCESS;
1004}
1005
Yiwei Zhang533cea92019-06-03 18:43:24 -07001006static void DestroySwapchainInternal(VkDevice device,
1007 VkSwapchainKHR swapchain_handle,
1008 const VkAllocationCallbacks* allocator) {
1009 ATRACE_CALL();
1010
1011 const auto& dispatch = GetData(device).driver;
1012 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1013 if (!swapchain) {
1014 return;
1015 }
1016
1017 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1018 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1019
1020 if (window && swapchain->frame_timestamps_enabled) {
1021 native_window_enable_frame_timestamps(window, false);
1022 }
1023
1024 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001025 ReleaseSwapchainImage(device, window, -1, swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -07001026 }
1027
1028 if (active) {
1029 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1030 }
1031
1032 if (!allocator) {
1033 allocator = &GetData(device).allocator;
1034 }
1035
1036 swapchain->~Swapchain();
1037 allocator->pfnFree(allocator->pUserData, swapchain);
1038}
1039
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001040VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001041VkResult CreateSwapchainKHR(VkDevice device,
1042 const VkSwapchainCreateInfoKHR* create_info,
1043 const VkAllocationCallbacks* allocator,
1044 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001045 ATRACE_CALL();
1046
Jesse Halld7b994a2015-09-07 14:17:37 -07001047 int err;
1048 VkResult result = VK_SUCCESS;
1049
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001050 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1051 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1052 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1053 " oldSwapchain=0x%" PRIx64,
1054 reinterpret_cast<uint64_t>(create_info->surface),
1055 create_info->minImageCount, create_info->imageFormat,
1056 create_info->imageColorSpace, create_info->imageExtent.width,
1057 create_info->imageExtent.height, create_info->imageUsage,
1058 create_info->preTransform, create_info->presentMode,
1059 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1060
Jesse Hall1f91d392015-12-11 16:28:44 -08001061 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001062 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001063
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001064 android_pixel_format native_pixel_format =
1065 GetNativePixelFormat(create_info->imageFormat);
1066 android_dataspace native_dataspace =
1067 GetNativeDataspace(create_info->imageColorSpace);
1068 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1069 ALOGE(
1070 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1071 "failed: Unsupported color space",
1072 create_info->imageColorSpace);
1073 return VK_ERROR_INITIALIZATION_FAILED;
1074 }
1075
Jesse Hall42a9eec2016-06-03 12:39:49 -07001076 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001077 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001078 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001079 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001080 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001081 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001082 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001083 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001084 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1085 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001086 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001087 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001088
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001089 Surface& surface = *SurfaceFromHandle(create_info->surface);
1090
Jesse Halldc225072016-05-30 22:40:14 -07001091 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001092 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001093 " because it already has active swapchain 0x%" PRIx64
1094 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1095 reinterpret_cast<uint64_t>(create_info->surface),
1096 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1097 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1098 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1099 }
1100 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1101 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1102
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001103 // -- Reset the native window --
1104 // The native window might have been used previously, and had its properties
1105 // changed from defaults. That will affect the answer we get for queries
1106 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1107 // attempt such queries.
1108
Jesse Halldc225072016-05-30 22:40:14 -07001109 // The native window only allows dequeueing all buffers before any have
1110 // been queued, since after that point at least one is assumed to be in
1111 // non-FREE state at any given time. Disconnecting and re-connecting
1112 // orphans the previous buffers, getting us back to the state where we can
1113 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001114 //
1115 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001116 ANativeWindow* window = surface.window.get();
1117 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1118 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001119 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001120 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1121 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001122 strerror(-err), err);
1123
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001124 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, -1);
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001125 if (err != android::OK) {
1126 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1127 strerror(-err), err);
1128 return VK_ERROR_SURFACE_LOST_KHR;
1129 }
1130
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001131 err = native_window_set_buffer_count(window, 0);
1132 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001133 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1134 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001135 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001136 }
1137
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301138 int swap_interval =
1139 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001140 err = window->setSwapInterval(window, swap_interval);
1141 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001142 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1143 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001144 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001145 }
1146
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001147 err = native_window_set_shared_buffer_mode(window, false);
1148 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001149 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1150 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001151 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001152 }
1153
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001154 err = native_window_set_auto_refresh(window, false);
1155 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001156 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1157 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001158 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001159 }
1160
Jesse Halld7b994a2015-09-07 14:17:37 -07001161 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001162
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001163 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001164
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001165 err = native_window_set_buffers_format(window, native_pixel_format);
1166 if (err != android::OK) {
Jesse Hall517274a2016-02-10 00:07:18 -08001167 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001168 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001169 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001170 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001171 err = native_window_set_buffers_data_space(window, native_dataspace);
1172 if (err != android::OK) {
Jesse Hall517274a2016-02-10 00:07:18 -08001173 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001174 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001175 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001176 }
1177
Jesse Hall3dd678a2016-01-08 21:52:01 -08001178 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001179 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001180 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001181 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001182 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1183 create_info->imageExtent.width, create_info->imageExtent.height,
1184 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001185 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001186 }
1187
Jesse Hall178b6962016-02-24 15:39:50 -08001188 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1189 // applied during rendering. native_window_set_transform() expects the
1190 // inverse: the transform the app is requesting that the compositor perform
1191 // during composition. With native windows, pre-transform works by rendering
1192 // with the same transform the compositor is applying (as in Vulkan), but
1193 // then requesting the inverse transform, so that when the compositor does
1194 // it's job the two transforms cancel each other out and the compositor ends
1195 // up applying an identity transform to the app's buffer.
1196 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001197 window, InvertTransformToNative(create_info->preTransform));
1198 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001199 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1200 InvertTransformToNative(create_info->preTransform),
1201 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001202 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001203 }
1204
Jesse Hallf64ca122015-11-03 16:11:10 -08001205 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001206 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1207 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001208 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1209 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001210 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001211 }
1212
Chris Forbes97ef4612017-03-30 19:37:50 +13001213 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1214 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1215 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1216 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001217 err = native_window_set_shared_buffer_mode(window, true);
1218 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001219 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1220 return VK_ERROR_SURFACE_LOST_KHR;
1221 }
1222 }
1223
1224 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001225 err = native_window_set_auto_refresh(window, true);
1226 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001227 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1228 return VK_ERROR_SURFACE_LOST_KHR;
1229 }
1230 }
1231
Jesse Halle6080bf2016-02-28 20:58:50 -08001232 int query_value;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001233 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1234 &query_value);
1235 if (err != android::OK || query_value < 0) {
Jesse Halle6080bf2016-02-28 20:58:50 -08001236 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1237 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001238 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001239 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001240 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001241 uint32_t num_images =
Ian Elliotta3515f42019-05-15 14:11:33 -06001242 (swap_interval ? create_info->minImageCount
1243 : std::max(3u, create_info->minImageCount)) -
1244 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001245
1246 // Lower layer insists that we have at least two buffers. This is wasteful
1247 // and we'd like to relax it in the shared case, but not all the pieces are
1248 // in place for that to work yet. Note we only lie to the lower layer-- we
1249 // don't want to give the app back a swapchain with extra images (which they
1250 // can't actually use!).
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001251 err = native_window_set_buffer_count(window, std::max(2u, num_images));
1252 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001253 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1254 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001255 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001256 }
1257
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001258 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001259 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001260 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001261 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001262 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1263 device, create_info->imageFormat, create_info->imageUsage,
1264 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001265 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001266 if (result != VK_SUCCESS) {
1267 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001268 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001269 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001270 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001271 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001272 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001273 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001274 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001275 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001276 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001277 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001278 if (result != VK_SUCCESS) {
1279 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001280 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001281 }
Jesse Hall70f93352015-11-04 09:41:31 -08001282 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001283 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1284
1285 bool createProtectedSwapchain = false;
1286 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1287 createProtectedSwapchain = true;
1288 native_usage |= BufferUsage::PROTECTED;
1289 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001290 err = native_window_set_usage(window, native_usage);
1291 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001292 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001293 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001294 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001295
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001296 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001297 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1298 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001299 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1300 strerror(-err), err);
1301 return VK_ERROR_SURFACE_LOST_KHR;
1302 }
1303
Jesse Halld7b994a2015-09-07 14:17:37 -07001304 // -- Allocate our Swapchain object --
1305 // After this point, we must deallocate the swapchain on error.
1306
Jesse Hall1f91d392015-12-11 16:28:44 -08001307 void* mem = allocator->pfnAllocation(allocator->pUserData,
1308 sizeof(Swapchain), alignof(Swapchain),
1309 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001310 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001311 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001312 Swapchain* swapchain = new (mem)
1313 Swapchain(surface, num_images, create_info->presentMode,
1314 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001315 // -- Dequeue all buffers and create a VkImage for each --
1316 // Any failures during or after this must cancel the dequeued buffers.
1317
Chris Forbesb56287a2017-01-12 14:28:58 +13001318 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1319#pragma clang diagnostic push
1320#pragma clang diagnostic ignored "-Wold-style-cast"
1321 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1322#pragma clang diagnostic pop
1323 .pNext = nullptr,
1324 .usage = swapchain_image_usage,
1325 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001326 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001327#pragma clang diagnostic push
1328#pragma clang diagnostic ignored "-Wold-style-cast"
1329 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1330#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001331 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001332 };
1333 VkImageCreateInfo image_create = {
1334 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1335 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001336 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001337 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001338 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001339 .extent = {0, 0, 1},
1340 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001341 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001342 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001343 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001344 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001345 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001346 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001347 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1348 };
1349
Jesse Halld7b994a2015-09-07 14:17:37 -07001350 for (uint32_t i = 0; i < num_images; i++) {
1351 Swapchain::Image& img = swapchain->images[i];
1352
1353 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001354 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1355 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001356 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001357 switch (-err) {
1358 case ENOMEM:
1359 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1360 break;
1361 default:
1362 result = VK_ERROR_SURFACE_LOST_KHR;
1363 break;
1364 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001365 break;
1366 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001367 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001368 img.dequeued = true;
1369
1370 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001371 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1372 static_cast<uint32_t>(img.buffer->height),
1373 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001374 image_native_buffer.handle = img.buffer->handle;
1375 image_native_buffer.stride = img.buffer->stride;
1376 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001377 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001378 android_convertGralloc0To1Usage(int(img.buffer->usage),
1379 &image_native_buffer.usage2.producer,
1380 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001381
Yiwei Zhang533cea92019-06-03 18:43:24 -07001382 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001383 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001384 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001385 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001386 if (result != VK_SUCCESS) {
1387 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1388 break;
1389 }
1390 }
1391
1392 // -- Cancel all buffers, returning them to the queue --
1393 // If an error occurred before, also destroy the VkImage and release the
1394 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001395 for (uint32_t i = 0; i < num_images; i++) {
1396 Swapchain::Image& img = swapchain->images[i];
1397 if (img.dequeued) {
1398 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001399 window->cancelBuffer(window, img.buffer.get(),
1400 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001401 img.dequeue_fence = -1;
1402 img.dequeued = false;
1403 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001404 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001405 }
1406
1407 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001408 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1409 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001410 return result;
1411 }
1412
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001413 if (transform_hint != swapchain->pre_transform) {
1414 // Log that the app is not doing pre-rotation.
1415 android::GraphicsEnv::getInstance().setTargetStats(
1416 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1417 }
1418
Jesse Halldc225072016-05-30 22:40:14 -07001419 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1420 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001421 return VK_SUCCESS;
1422}
1423
Jesse Halle1b12782015-11-30 11:27:32 -08001424VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001425void DestroySwapchainKHR(VkDevice device,
1426 VkSwapchainKHR swapchain_handle,
1427 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001428 ATRACE_CALL();
1429
Yiwei Zhang533cea92019-06-03 18:43:24 -07001430 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001431}
1432
Jesse Halle1b12782015-11-30 11:27:32 -08001433VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001434VkResult GetSwapchainImagesKHR(VkDevice,
1435 VkSwapchainKHR swapchain_handle,
1436 uint32_t* count,
1437 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001438 ATRACE_CALL();
1439
Jesse Halld7b994a2015-09-07 14:17:37 -07001440 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001441 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1442 "getting images for non-active swapchain 0x%" PRIx64
1443 "; only dequeued image handles are valid",
1444 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001445 VkResult result = VK_SUCCESS;
1446 if (images) {
1447 uint32_t n = swapchain.num_images;
1448 if (*count < swapchain.num_images) {
1449 n = *count;
1450 result = VK_INCOMPLETE;
1451 }
1452 for (uint32_t i = 0; i < n; i++)
1453 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001454 *count = n;
1455 } else {
1456 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001457 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001458 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001459}
1460
Jesse Halle1b12782015-11-30 11:27:32 -08001461VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001462VkResult AcquireNextImageKHR(VkDevice device,
1463 VkSwapchainKHR swapchain_handle,
1464 uint64_t timeout,
1465 VkSemaphore semaphore,
1466 VkFence vk_fence,
1467 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001468 ATRACE_CALL();
1469
Jesse Halld7b994a2015-09-07 14:17:37 -07001470 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001471 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001472 VkResult result;
1473 int err;
1474
Jesse Halldc225072016-05-30 22:40:14 -07001475 if (swapchain.surface.swapchain_handle != swapchain_handle)
1476 return VK_ERROR_OUT_OF_DATE_KHR;
1477
Chris Forbesc88409c2017-03-30 19:47:37 +13001478 if (swapchain.shared) {
1479 // In shared mode, we keep the buffer dequeued all the time, so we don't
1480 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1481 // the semaphore and fence passed to us will be signalled.
1482 *image_index = 0;
1483 result = GetData(device).driver.AcquireImageANDROID(
1484 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1485 return result;
1486 }
1487
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001488 const nsecs_t acquire_next_image_timeout =
1489 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1490 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1491 // Cache the timeout to avoid the duplicate binder cost.
1492 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1493 acquire_next_image_timeout);
1494 if (err != android::OK) {
1495 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1496 strerror(-err), err);
1497 return VK_ERROR_SURFACE_LOST_KHR;
1498 }
1499 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1500 }
1501
Jesse Halld7b994a2015-09-07 14:17:37 -07001502 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001503 int fence_fd;
1504 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001505 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001506 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1507 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1508 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001509 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001510 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001511 }
1512
1513 uint32_t idx;
1514 for (idx = 0; idx < swapchain.num_images; idx++) {
1515 if (swapchain.images[idx].buffer.get() == buffer) {
1516 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001517 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001518 break;
1519 }
1520 }
1521 if (idx == swapchain.num_images) {
1522 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001523 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001524 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001525 }
1526
1527 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001528 if (fence_fd != -1) {
1529 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001530 if (fence_clone == -1) {
1531 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1532 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001533 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001534 }
1535 }
1536
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001537 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001538 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001539 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001540 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1541 // even if the call fails. We could close it ourselves on failure, but
1542 // that would create a race condition if the driver closes it on a
1543 // failure path: some other thread might create an fd with the same
1544 // number between the time the driver closes it and the time we close
1545 // it. We must assume one of: the driver *always* closes it even on
1546 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001547 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001548 swapchain.images[idx].dequeued = false;
1549 swapchain.images[idx].dequeue_fence = -1;
1550 return result;
1551 }
1552
1553 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001554 return VK_SUCCESS;
1555}
1556
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001557VKAPI_ATTR
1558VkResult AcquireNextImage2KHR(VkDevice device,
1559 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1560 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001561 ATRACE_CALL();
1562
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001563 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1564 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1565 pAcquireInfo->fence, pImageIndex);
1566}
1567
Jesse Halldc225072016-05-30 22:40:14 -07001568static VkResult WorstPresentResult(VkResult a, VkResult b) {
1569 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1570 // (in spec version 1.0.14).
1571 static const VkResult kWorstToBest[] = {
1572 VK_ERROR_DEVICE_LOST,
1573 VK_ERROR_SURFACE_LOST_KHR,
1574 VK_ERROR_OUT_OF_DATE_KHR,
1575 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1576 VK_ERROR_OUT_OF_HOST_MEMORY,
1577 VK_SUBOPTIMAL_KHR,
1578 };
1579 for (auto result : kWorstToBest) {
1580 if (a == result || b == result)
1581 return result;
1582 }
1583 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1584 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1585 return a != VK_SUCCESS ? a : b;
1586}
1587
Jesse Halle1b12782015-11-30 11:27:32 -08001588VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001589VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001590 ATRACE_CALL();
1591
Jesse Halld7b994a2015-09-07 14:17:37 -07001592 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1593 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1594 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001595
Jesse Halldc225072016-05-30 22:40:14 -07001596 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001597 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001598 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001599
Ian Elliottcb351132016-12-13 10:30:40 -07001600 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001601 const VkPresentRegionsKHR* present_regions = nullptr;
1602 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001603 const VkPresentRegionsKHR* next =
1604 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1605 while (next) {
1606 switch (next->sType) {
1607 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1608 present_regions = next;
1609 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001610 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001611 present_times =
1612 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1613 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001614 default:
1615 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1616 next->sType);
1617 break;
1618 }
1619 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1620 }
1621 ALOGV_IF(
1622 present_regions &&
1623 present_regions->swapchainCount != present_info->swapchainCount,
1624 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001625 ALOGV_IF(present_times &&
1626 present_times->swapchainCount != present_info->swapchainCount,
1627 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1628 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001629 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001630 (present_regions) ? present_regions->pRegions : nullptr;
1631 const VkPresentTimeGOOGLE* times =
1632 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001633 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001634 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001635 uint32_t nrects = 0;
1636
Jesse Halld7b994a2015-09-07 14:17:37 -07001637 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1638 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001639 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001640 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001641 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001642 const VkPresentRegionKHR* region =
1643 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001644 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001645 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001646 VkResult result;
1647 int err;
1648
Jesse Halld7b994a2015-09-07 14:17:37 -07001649 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001650 result = dispatch.QueueSignalReleaseImageANDROID(
1651 queue, present_info->waitSemaphoreCount,
1652 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001653 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001654 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001655 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001656 }
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001657 if (img.release_fence >= 0)
1658 close(img.release_fence);
1659 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001660
Jesse Halldc225072016-05-30 22:40:14 -07001661 if (swapchain.surface.swapchain_handle ==
1662 present_info->pSwapchains[sc]) {
1663 ANativeWindow* window = swapchain.surface.window.get();
1664 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001665 if (region) {
1666 // Process the incremental-present hint for this swapchain:
1667 uint32_t rcount = region->rectangleCount;
1668 if (rcount > nrects) {
1669 android_native_rect_t* new_rects =
1670 static_cast<android_native_rect_t*>(
1671 allocator->pfnReallocation(
1672 allocator->pUserData, rects,
1673 sizeof(android_native_rect_t) * rcount,
1674 alignof(android_native_rect_t),
1675 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1676 if (new_rects) {
1677 rects = new_rects;
1678 nrects = rcount;
1679 } else {
1680 rcount = 0; // Ignore the hint for this swapchain
1681 }
1682 }
1683 for (uint32_t r = 0; r < rcount; ++r) {
1684 if (region->pRectangles[r].layer > 0) {
1685 ALOGV(
1686 "vkQueuePresentKHR ignoring invalid layer "
1687 "(%u); using layer 0 instead",
1688 region->pRectangles[r].layer);
1689 }
1690 int x = region->pRectangles[r].offset.x;
1691 int y = region->pRectangles[r].offset.y;
1692 int width = static_cast<int>(
1693 region->pRectangles[r].extent.width);
1694 int height = static_cast<int>(
1695 region->pRectangles[r].extent.height);
1696 android_native_rect_t* cur_rect = &rects[r];
1697 cur_rect->left = x;
1698 cur_rect->top = y + height;
1699 cur_rect->right = x + width;
1700 cur_rect->bottom = y;
1701 }
1702 native_window_set_surface_damage(window, rects, rcount);
1703 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001704 if (time) {
1705 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001706 ALOGV(
1707 "Calling "
1708 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001709 native_window_enable_frame_timestamps(window, true);
1710 swapchain.frame_timestamps_enabled = true;
1711 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001712
1713 // Record the nativeFrameId so it can be later correlated to
1714 // this present.
1715 uint64_t nativeFrameId = 0;
1716 err = native_window_get_next_frame_id(
1717 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001718 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001719 ALOGE("Failed to get next native frame ID.");
1720 }
1721
1722 // Add a new timing record with the user's presentID and
1723 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001724 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001725 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001726 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001727 }
1728 if (time->desiredPresentTime) {
1729 // Set the desiredPresentTime:
1730 ALOGV(
1731 "Calling "
1732 "native_window_set_buffers_timestamp(%" PRId64 ")",
1733 time->desiredPresentTime);
1734 native_window_set_buffers_timestamp(
1735 window,
1736 static_cast<int64_t>(time->desiredPresentTime));
1737 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001738 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001739
Jesse Halldc225072016-05-30 22:40:14 -07001740 err = window->queueBuffer(window, img.buffer.get(), fence);
1741 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001742 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001743 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1744 swapchain_result = WorstPresentResult(
1745 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001746 } else {
1747 if (img.dequeue_fence >= 0) {
1748 close(img.dequeue_fence);
1749 img.dequeue_fence = -1;
1750 }
1751 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001752 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001753
1754 // If the swapchain is in shared mode, immediately dequeue the
1755 // buffer so it can be presented again without an intervening
1756 // call to AcquireNextImageKHR. We expect to get the same buffer
1757 // back from every call to dequeueBuffer in this mode.
1758 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1759 ANativeWindowBuffer* buffer;
1760 int fence_fd;
1761 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001762 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001763 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1764 swapchain_result = WorstPresentResult(swapchain_result,
1765 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001766 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001767 ALOGE("got wrong image back for shared swapchain");
1768 swapchain_result = WorstPresentResult(swapchain_result,
1769 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001770 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13001771 img.dequeue_fence = fence_fd;
1772 img.dequeued = true;
1773 }
1774 }
Jesse Halldc225072016-05-30 22:40:14 -07001775 }
1776 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001777 OrphanSwapchain(device, &swapchain);
1778 }
silence_dogood73597592019-05-23 16:57:37 -07001779 int window_transform_hint;
1780 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1781 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001782 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07001783 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1784 strerror(-err), err);
1785 swapchain_result = WorstPresentResult(
1786 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1787 }
1788 if (swapchain.pre_transform != window_transform_hint) {
1789 swapchain_result =
1790 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1791 }
Jesse Halldc225072016-05-30 22:40:14 -07001792 } else {
Yiwei Zhang9df5bff2020-09-22 10:08:03 -07001793 ReleaseSwapchainImage(device, nullptr, fence, img, true);
Jesse Halldc225072016-05-30 22:40:14 -07001794 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001795 }
1796
Jesse Halla9e57032015-11-30 01:03:10 -08001797 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001798 present_info->pResults[sc] = swapchain_result;
1799
1800 if (swapchain_result != final_result)
1801 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001802 }
Ian Elliottcb351132016-12-13 10:30:40 -07001803 if (rects) {
1804 allocator->pfnFree(allocator->pUserData, rects);
1805 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001806
1807 return final_result;
1808}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001809
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001810VKAPI_ATTR
1811VkResult GetRefreshCycleDurationGOOGLE(
1812 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001813 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001814 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001815 ATRACE_CALL();
1816
Ian Elliott62c48c92017-01-20 13:13:20 -07001817 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001818 VkResult result = VK_SUCCESS;
1819
Ian Elliott3568bfe2019-05-03 15:54:46 -06001820 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001821
1822 return result;
1823}
1824
1825VKAPI_ATTR
1826VkResult GetPastPresentationTimingGOOGLE(
1827 VkDevice,
1828 VkSwapchainKHR swapchain_handle,
1829 uint32_t* count,
1830 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001831 ATRACE_CALL();
1832
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001833 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001834 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1835 return VK_ERROR_OUT_OF_DATE_KHR;
1836 }
1837
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001838 ANativeWindow* window = swapchain.surface.window.get();
1839 VkResult result = VK_SUCCESS;
1840
1841 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001842 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001843 native_window_enable_frame_timestamps(window, true);
1844 swapchain.frame_timestamps_enabled = true;
1845 }
1846
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001847 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07001848 // Get the latest ready timing count before copying, since the copied
1849 // timing info will be erased in copy_ready_timings function.
1850 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07001851 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001852 // Check the *count here against the recorded ready timing count, since
1853 // *count can be overwritten per spec describes.
1854 if (*count < n) {
1855 result = VK_INCOMPLETE;
1856 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001857 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001858 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001859 }
1860
1861 return result;
1862}
1863
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001864VKAPI_ATTR
1865VkResult GetSwapchainStatusKHR(
1866 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001867 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001868 ATRACE_CALL();
1869
Chris Forbes4e18ba82017-01-20 12:50:17 +13001870 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001871 VkResult result = VK_SUCCESS;
1872
Chris Forbes4e18ba82017-01-20 12:50:17 +13001873 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1874 return VK_ERROR_OUT_OF_DATE_KHR;
1875 }
1876
Yiwei Zhanga885c062019-10-24 12:07:57 -07001877 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001878
1879 return result;
1880}
1881
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001882VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001883 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001884 uint32_t swapchainCount,
1885 const VkSwapchainKHR* pSwapchains,
1886 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001887 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001888
1889 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1890 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1891 if (!swapchain)
1892 continue;
1893
1894 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1895
1896 ANativeWindow* window = swapchain->surface.window.get();
1897
1898 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1899 const android_smpte2086_metadata smpteMetdata = {
1900 {vulkanMetadata.displayPrimaryRed.x,
1901 vulkanMetadata.displayPrimaryRed.y},
1902 {vulkanMetadata.displayPrimaryGreen.x,
1903 vulkanMetadata.displayPrimaryGreen.y},
1904 {vulkanMetadata.displayPrimaryBlue.x,
1905 vulkanMetadata.displayPrimaryBlue.y},
1906 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1907 vulkanMetadata.maxLuminance,
1908 vulkanMetadata.minLuminance};
1909 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1910
1911 const android_cta861_3_metadata cta8613Metadata = {
1912 vulkanMetadata.maxContentLightLevel,
1913 vulkanMetadata.maxFrameAverageLightLevel};
1914 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1915 }
1916
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001917 return;
1918}
1919
Yiwei Zhang0f475222019-04-11 19:38:00 -07001920static void InterceptBindImageMemory2(
1921 uint32_t bind_info_count,
1922 const VkBindImageMemoryInfo* bind_infos,
1923 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1924 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1925 out_native_buffers->clear();
1926 out_bind_infos->clear();
1927
1928 if (!bind_info_count)
1929 return;
1930
1931 std::unordered_set<uint32_t> intercepted_indexes;
1932
1933 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1934 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1935 bind_infos[idx].pNext);
1936 while (info &&
1937 info->sType !=
1938 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1939 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1940 info->pNext);
1941 }
1942
1943 if (!info)
1944 continue;
1945
1946 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1947 "swapchain handle must not be NULL");
1948 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1949 ALOG_ASSERT(
1950 info->imageIndex < swapchain->num_images,
1951 "imageIndex must be less than the number of images in swapchain");
1952
1953 ANativeWindowBuffer* buffer =
1954 swapchain->images[info->imageIndex].buffer.get();
1955 VkNativeBufferANDROID native_buffer = {
1956#pragma clang diagnostic push
1957#pragma clang diagnostic ignored "-Wold-style-cast"
1958 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1959#pragma clang diagnostic pop
1960 .pNext = bind_infos[idx].pNext,
1961 .handle = buffer->handle,
1962 .stride = buffer->stride,
1963 .format = buffer->format,
1964 .usage = int(buffer->usage),
1965 };
1966 // Reserve enough space to avoid letting re-allocation invalidate the
1967 // addresses of the elements inside.
1968 out_native_buffers->reserve(bind_info_count);
1969 out_native_buffers->emplace_back(native_buffer);
1970
1971 // Reserve the space now since we know how much is needed now.
1972 out_bind_infos->reserve(bind_info_count);
1973 out_bind_infos->emplace_back(bind_infos[idx]);
1974 out_bind_infos->back().pNext = &out_native_buffers->back();
1975
1976 intercepted_indexes.insert(idx);
1977 }
1978
1979 if (intercepted_indexes.empty())
1980 return;
1981
1982 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1983 if (intercepted_indexes.count(idx))
1984 continue;
1985 out_bind_infos->emplace_back(bind_infos[idx]);
1986 }
1987}
1988
Yiwei Zhang23143102019-04-10 18:24:05 -07001989VKAPI_ATTR
1990VkResult BindImageMemory2(VkDevice device,
1991 uint32_t bindInfoCount,
1992 const VkBindImageMemoryInfo* pBindInfos) {
1993 ATRACE_CALL();
1994
Yiwei Zhang0f475222019-04-11 19:38:00 -07001995 // out_native_buffers is for maintaining the lifecycle of the constructed
1996 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1997 std::vector<VkNativeBufferANDROID> out_native_buffers;
1998 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1999 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2000 &out_bind_infos);
2001 return GetData(device).driver.BindImageMemory2(
2002 device, bindInfoCount,
2003 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002004}
2005
2006VKAPI_ATTR
2007VkResult BindImageMemory2KHR(VkDevice device,
2008 uint32_t bindInfoCount,
2009 const VkBindImageMemoryInfo* pBindInfos) {
2010 ATRACE_CALL();
2011
Yiwei Zhang0f475222019-04-11 19:38:00 -07002012 std::vector<VkNativeBufferANDROID> out_native_buffers;
2013 std::vector<VkBindImageMemoryInfo> out_bind_infos;
2014 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
2015 &out_bind_infos);
2016 return GetData(device).driver.BindImageMemory2KHR(
2017 device, bindInfoCount,
2018 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07002019}
2020
Chia-I Wu62262232016-03-26 07:06:44 +08002021} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07002022} // namespace vulkan