blob: 2913850ab3bb983019320464a550d52afc9034f9 [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 Zhangda2e4792020-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 Zhangda2e4792020-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 Zhangda2e4792020-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 Zhangda2e4792020-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 Zhangda2e4792020-09-22 10:08:03 -0700339 release_fence = -1;
Jesse Halldc225072016-05-30 22:40:14 -0700340 image.dequeued = false;
341 }
342
Yiwei Zhangda2e4792020-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 Zhangda2e4792020-09-22 10:08:03 -0700366 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i],
367 true);
Jesse Halldc225072016-05-30 22:40:14 -0700368 }
369 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700370 swapchain->timing.clear();
371}
372
373uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800374 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
375 return 0;
376 }
Ian Elliott8a977262017-01-19 09:05:58 -0700377
Brian Anderson1049d1d2016-12-16 17:25:57 -0800378 uint32_t num_ready = 0;
379 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
380 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700381 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800382 if (ti.ready()) {
383 // This TimingInfo is ready to be reported to the user. Add it
384 // to the num_ready.
385 num_ready++;
386 continue;
387 }
388 // This TimingInfo is not yet ready to be reported to the user,
389 // and so we should look for any available timestamps that
390 // might make it ready.
391 int64_t desired_present_time = 0;
392 int64_t render_complete_time = 0;
393 int64_t composition_latch_time = 0;
394 int64_t actual_present_time = 0;
395 // Obtain timestamps:
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800396 int err = native_window_get_frame_timestamps(
Brian Anderson1049d1d2016-12-16 17:25:57 -0800397 swapchain.surface.window.get(), ti.native_frame_id_,
398 &desired_present_time, &render_complete_time,
399 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700400 nullptr, //&first_composition_start_time,
401 nullptr, //&last_composition_start_time,
402 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800403 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700404 nullptr, //&dequeue_ready_time,
405 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800406
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800407 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800408 continue;
409 }
410
411 // Record the timestamp(s) we received, and then see if this TimingInfo
412 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700413 ti.timestamp_desired_present_time_ = desired_present_time;
414 ti.timestamp_actual_present_time_ = actual_present_time;
415 ti.timestamp_render_complete_time_ = render_complete_time;
416 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800417
418 if (ti.ready()) {
419 // The TimingInfo has received enough timestamps, and should now
420 // use those timestamps to calculate the info that should be
421 // reported to the user:
422 ti.calculate(swapchain.refresh_duration);
423 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700424 }
425 }
426 return num_ready;
427}
428
Ian Elliott8a977262017-01-19 09:05:58 -0700429void copy_ready_timings(Swapchain& swapchain,
430 uint32_t* count,
431 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800432 if (swapchain.timing.empty()) {
433 *count = 0;
434 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700435 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800436
437 size_t last_ready = swapchain.timing.size() - 1;
438 while (!swapchain.timing[last_ready].ready()) {
439 if (last_ready == 0) {
440 *count = 0;
441 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700442 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800443 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700444 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800445
446 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700447 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800448 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
449 const TimingInfo& ti = swapchain.timing[i];
450 if (ti.ready()) {
451 ti.get_values(&timings[num_copied]);
452 num_copied++;
453 }
454 num_to_remove++;
455 }
456
457 // Discard old frames that aren't ready if newer frames are ready.
458 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700459 swapchain.timing.erase(swapchain.timing.begin(),
460 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800461
Ian Elliott8a977262017-01-19 09:05:58 -0700462 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700463}
464
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700465android_pixel_format GetNativePixelFormat(VkFormat format) {
466 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
467 switch (format) {
468 case VK_FORMAT_R8G8B8A8_UNORM:
469 case VK_FORMAT_R8G8B8A8_SRGB:
470 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
471 break;
472 case VK_FORMAT_R5G6B5_UNORM_PACK16:
473 native_format = HAL_PIXEL_FORMAT_RGB_565;
474 break;
475 case VK_FORMAT_R16G16B16A16_SFLOAT:
476 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
477 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800478 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700479 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
480 break;
481 default:
482 ALOGV("unsupported swapchain format %d", format);
483 break;
484 }
485 return native_format;
486}
487
488android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
489 switch (colorspace) {
490 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
491 return HAL_DATASPACE_V0_SRGB;
492 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
493 return HAL_DATASPACE_DISPLAY_P3;
494 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
495 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600496 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
497 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700498 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
499 return HAL_DATASPACE_DCI_P3_LINEAR;
500 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
501 return HAL_DATASPACE_DCI_P3;
502 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
503 return HAL_DATASPACE_V0_SRGB_LINEAR;
504 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
505 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600506 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
507 return HAL_DATASPACE_BT2020_LINEAR;
508 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700509 return static_cast<android_dataspace>(
510 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
511 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600512 case VK_COLOR_SPACE_DOLBYVISION_EXT:
513 return static_cast<android_dataspace>(
514 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
515 HAL_DATASPACE_RANGE_FULL);
516 case VK_COLOR_SPACE_HDR10_HLG_EXT:
517 return static_cast<android_dataspace>(
518 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
519 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700520 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
521 return static_cast<android_dataspace>(
522 HAL_DATASPACE_STANDARD_ADOBE_RGB |
523 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
524 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
525 return HAL_DATASPACE_ADOBE_RGB;
526
527 // Pass through is intended to allow app to provide data that is passed
528 // to the display system without modification.
529 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
530 return HAL_DATASPACE_ARBITRARY;
531
532 default:
533 // This indicates that we don't know about the
534 // dataspace specified and we should indicate that
535 // it's unsupported
536 return HAL_DATASPACE_UNKNOWN;
537 }
538}
539
Jesse Halld7b994a2015-09-07 14:17:37 -0700540} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700541
Jesse Halle1b12782015-11-30 11:27:32 -0800542VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800543VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800544 VkInstance instance,
545 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
546 const VkAllocationCallbacks* allocator,
547 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800548 ATRACE_CALL();
549
Jesse Hall1f91d392015-12-11 16:28:44 -0800550 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800551 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800552 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
553 alignof(Surface),
554 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800555 if (!mem)
556 return VK_ERROR_OUT_OF_HOST_MEMORY;
557 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700558
Chia-I Wue8e689f2016-04-18 08:21:31 +0800559 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700560 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700561 int err = native_window_get_consumer_usage(surface->window.get(),
562 &surface->consumer_usage);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800563 if (err != android::OK) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700564 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
565 strerror(-err), err);
566 surface->~Surface();
567 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700568 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700569 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700570
Yiwei Zhang6435b322018-05-08 11:12:17 -0700571 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800572 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800573 if (err != android::OK) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800574 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
575 err);
576 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800577 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700578 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800579 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700580
Jesse Hall1356b0d2015-11-23 17:24:58 -0800581 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700582 return VK_SUCCESS;
583}
584
Jesse Halle1b12782015-11-30 11:27:32 -0800585VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800586void DestroySurfaceKHR(VkInstance instance,
587 VkSurfaceKHR surface_handle,
588 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800589 ATRACE_CALL();
590
Jesse Hall1356b0d2015-11-23 17:24:58 -0800591 Surface* surface = SurfaceFromHandle(surface_handle);
592 if (!surface)
593 return;
594 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700595 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700596 "destroyed VkSurfaceKHR 0x%" PRIx64
597 " has active VkSwapchainKHR 0x%" PRIx64,
598 reinterpret_cast<uint64_t>(surface_handle),
599 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800600 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800601 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800602 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800603 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800604}
605
Jesse Halle1b12782015-11-30 11:27:32 -0800606VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800607VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
608 uint32_t /*queue_family*/,
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000609 VkSurfaceKHR /*surface_handle*/,
Chia-I Wu62262232016-03-26 07:06:44 +0800610 VkBool32* supported) {
Yiwei Zhangc7e46c42021-02-04 22:53:59 +0000611 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800612 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800613}
614
Jesse Halle1b12782015-11-30 11:27:32 -0800615VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800616VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800617 VkPhysicalDevice /*pdev*/,
618 VkSurfaceKHR surface,
619 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800620 ATRACE_CALL();
621
Jesse Halld7b994a2015-09-07 14:17:37 -0700622 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800623 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700624
625 int width, height;
626 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800627 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700628 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
629 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700630 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700631 }
632 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800633 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700634 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
635 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700636 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700637 }
638
Jesse Hall55bc0972016-02-23 16:43:29 -0800639 int transform_hint;
640 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800641 if (err != android::OK) {
Jesse Hall55bc0972016-02-23 16:43:29 -0800642 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
643 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700644 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800645 }
646
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800647 int max_buffer_count;
648 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800649 if (err != android::OK) {
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800650 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
651 strerror(-err), err);
652 return VK_ERROR_SURFACE_LOST_KHR;
653 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700654 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800655 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700656
Jesse Hallfe2662d2016-02-09 13:26:59 -0800657 capabilities->currentExtent =
658 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
659
Yiwei Zhang70a21962019-05-31 17:26:52 -0700660 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800661 capabilities->minImageExtent = VkExtent2D{1, 1};
662 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700663
Jesse Hallfe2662d2016-02-09 13:26:59 -0800664 capabilities->maxImageArrayLayers = 1;
665
Jesse Hall55bc0972016-02-23 16:43:29 -0800666 capabilities->supportedTransforms = kSupportedTransforms;
667 capabilities->currentTransform =
668 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700669
Jesse Hallfe2662d2016-02-09 13:26:59 -0800670 // On Android, window composition is a WindowManager property, not something
671 // associated with the bufferqueue. It can't be changed from here.
672 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700673
Jesse Hallb00daad2015-11-29 19:46:20 -0800674 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800675 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
676 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
677 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700678 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
679
Jesse Hallb1352bc2015-09-04 16:12:33 -0700680 return VK_SUCCESS;
681}
682
Jesse Halle1b12782015-11-30 11:27:32 -0800683VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700684VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
685 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800686 uint32_t* count,
687 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800688 ATRACE_CALL();
689
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700690 const InstanceData& instance_data = GetData(pdev);
691
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700692 bool wide_color_support = false;
693 Surface& surface = *SurfaceFromHandle(surface_handle);
694 int err = native_window_get_wide_color_support(surface.window.get(),
695 &wide_color_support);
696 if (err) {
Yiwei Zhang70a21962019-05-31 17:26:52 -0700697 return VK_ERROR_SURFACE_LOST_KHR;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700698 }
699 ALOGV("wide_color_support is: %d", wide_color_support);
700 wide_color_support =
701 wide_color_support &&
702 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
703
Charlie Lao324191f2019-12-13 11:03:16 -0800704 AHardwareBuffer_Desc desc = {};
705 desc.width = 1;
706 desc.height = 1;
707 desc.layers = 1;
708 desc.usage = surface.consumer_usage |
709 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
710 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
711
712 // We must support R8G8B8A8
713 std::vector<VkSurfaceFormatKHR> all_formats = {
714 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
715 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}};
716
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700717 if (wide_color_support) {
Charlie Lao324191f2019-12-13 11:03:16 -0800718 all_formats.emplace_back(VkSurfaceFormatKHR{
719 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
720 all_formats.emplace_back(VkSurfaceFormatKHR{
721 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
722 }
723
724 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
725 if (AHardwareBuffer_isSupported(&desc)) {
726 all_formats.emplace_back(VkSurfaceFormatKHR{
727 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
728 }
729
730 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
731 if (AHardwareBuffer_isSupported(&desc)) {
732 all_formats.emplace_back(VkSurfaceFormatKHR{
733 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
734 if (wide_color_support) {
735 all_formats.emplace_back(
736 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
737 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
738 all_formats.emplace_back(
739 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
740 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
741 }
742 }
743
744 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
745 if (AHardwareBuffer_isSupported(&desc)) {
746 all_formats.emplace_back(
747 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
748 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
749 if (wide_color_support) {
750 all_formats.emplace_back(
751 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
752 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
753 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700754 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700755
756 VkResult result = VK_SUCCESS;
757 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800758 uint32_t transfer_count = all_formats.size();
759 if (transfer_count > *count) {
760 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700761 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700762 }
Charlie Lao324191f2019-12-13 11:03:16 -0800763 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
764 formats);
765 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700766 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800767 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700768 }
Charlie Lao324191f2019-12-13 11:03:16 -0800769
Jesse Halld7b994a2015-09-07 14:17:37 -0700770 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700771}
772
Jesse Halle1b12782015-11-30 11:27:32 -0800773VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300774VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
775 VkPhysicalDevice physicalDevice,
776 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
777 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800778 ATRACE_CALL();
779
Chris Forbes2452cf72017-03-16 16:30:17 +1300780 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
781 physicalDevice, pSurfaceInfo->surface,
782 &pSurfaceCapabilities->surfaceCapabilities);
783
Chris Forbes06bc0092017-03-16 16:46:05 +1300784 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
785 while (caps->pNext) {
786 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
787
788 switch (caps->sType) {
789 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
790 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
791 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
792 caps);
793 // Claim same set of usage flags are supported for
794 // shared present modes as for other modes.
795 shared_caps->sharedPresentSupportedUsageFlags =
796 pSurfaceCapabilities->surfaceCapabilities
797 .supportedUsageFlags;
798 } break;
799
800 default:
801 // Ignore all other extension structs
802 break;
803 }
804 }
805
Chris Forbes2452cf72017-03-16 16:30:17 +1300806 return result;
807}
808
809VKAPI_ATTR
810VkResult GetPhysicalDeviceSurfaceFormats2KHR(
811 VkPhysicalDevice physicalDevice,
812 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
813 uint32_t* pSurfaceFormatCount,
814 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800815 ATRACE_CALL();
816
Chris Forbes2452cf72017-03-16 16:30:17 +1300817 if (!pSurfaceFormats) {
818 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
819 pSurfaceInfo->surface,
820 pSurfaceFormatCount, nullptr);
821 } else {
822 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
823 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700824 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300825 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
826 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700827 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300828
829 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
830 // marshal results individually due to stride difference.
831 // completely ignore any chained extension structs.
832 uint32_t formats_to_marshal = *pSurfaceFormatCount;
833 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
834 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
835 }
836 }
837
838 return result;
839 }
840}
841
842VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300843VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800844 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800845 uint32_t* count,
846 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800847 ATRACE_CALL();
848
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800849 int err;
850 int query_value;
851 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
852
853 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800854 if (err != android::OK || query_value < 0) {
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800855 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
856 strerror(-err), err, query_value);
857 return VK_ERROR_SURFACE_LOST_KHR;
858 }
859 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
860
861 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800862 if (err != android::OK || query_value < 0) {
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800863 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
864 strerror(-err), err, query_value);
865 return VK_ERROR_SURFACE_LOST_KHR;
866 }
867 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
868
Yiwei Zhang5e862202019-06-21 14:59:16 -0700869 std::vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800870 if (min_undequeued_buffers + 1 < max_buffer_count)
871 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300872 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
873
874 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
875 if (QueryPresentationProperties(pdev, &present_properties)) {
876 if (present_properties.sharedImage) {
877 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
878 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
879 }
880 }
881
882 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700883
884 VkResult result = VK_SUCCESS;
885 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300886 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700887 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300888 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -0700889 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700890 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300891 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700892 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700893 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700894}
895
Jesse Halle1b12782015-11-30 11:27:32 -0800896VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400897VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600898 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400899 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800900 ATRACE_CALL();
901
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400902 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
903 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
904 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
905 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
906 pDeviceGroupPresentCapabilities->sType);
907
908 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
909 sizeof(pDeviceGroupPresentCapabilities->presentMask));
910
911 // assume device group of size 1
912 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
913 pDeviceGroupPresentCapabilities->modes =
914 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
915
916 return VK_SUCCESS;
917}
918
919VKAPI_ATTR
920VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600921 VkDevice,
922 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400923 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800924 ATRACE_CALL();
925
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400926 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
927 return VK_SUCCESS;
928}
929
930VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600931VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400932 VkSurfaceKHR surface,
933 uint32_t* pRectCount,
934 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800935 ATRACE_CALL();
936
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400937 if (!pRects) {
938 *pRectCount = 1;
939 } else {
940 uint32_t count = std::min(*pRectCount, 1u);
941 bool incomplete = *pRectCount < 1;
942
943 *pRectCount = count;
944
945 if (incomplete) {
946 return VK_INCOMPLETE;
947 }
948
949 int err;
950 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
951
952 int width = 0, height = 0;
953 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800954 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400955 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
956 strerror(-err), err);
957 }
958 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
Yiwei Zhangf5030b42019-12-19 00:10:04 -0800959 if (err != android::OK) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400960 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
961 strerror(-err), err);
962 }
963
Yiwei Zhanga885c062019-10-24 12:07:57 -0700964 // TODO(b/143294545): Return something better than "whole window"
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400965 pRects[0].offset.x = 0;
966 pRects[0].offset.y = 0;
967 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
968 static_cast<uint32_t>(height)};
969 }
970 return VK_SUCCESS;
971}
972
Yiwei Zhang533cea92019-06-03 18:43:24 -0700973static void DestroySwapchainInternal(VkDevice device,
974 VkSwapchainKHR swapchain_handle,
975 const VkAllocationCallbacks* allocator) {
976 ATRACE_CALL();
977
978 const auto& dispatch = GetData(device).driver;
979 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
980 if (!swapchain) {
981 return;
982 }
983
984 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
985 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
986
987 if (window && swapchain->frame_timestamps_enabled) {
988 native_window_enable_frame_timestamps(window, false);
989 }
990
991 for (uint32_t i = 0; i < swapchain->num_images; i++) {
Yiwei Zhangda2e4792020-09-22 10:08:03 -0700992 ReleaseSwapchainImage(device, window, -1, swapchain->images[i], false);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700993 }
994
995 if (active) {
996 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
997 }
998
999 if (!allocator) {
1000 allocator = &GetData(device).allocator;
1001 }
1002
1003 swapchain->~Swapchain();
1004 allocator->pfnFree(allocator->pUserData, swapchain);
1005}
1006
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001007VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001008VkResult CreateSwapchainKHR(VkDevice device,
1009 const VkSwapchainCreateInfoKHR* create_info,
1010 const VkAllocationCallbacks* allocator,
1011 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001012 ATRACE_CALL();
1013
Jesse Halld7b994a2015-09-07 14:17:37 -07001014 int err;
1015 VkResult result = VK_SUCCESS;
1016
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001017 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1018 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1019 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1020 " oldSwapchain=0x%" PRIx64,
1021 reinterpret_cast<uint64_t>(create_info->surface),
1022 create_info->minImageCount, create_info->imageFormat,
1023 create_info->imageColorSpace, create_info->imageExtent.width,
1024 create_info->imageExtent.height, create_info->imageUsage,
1025 create_info->preTransform, create_info->presentMode,
1026 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1027
Jesse Hall1f91d392015-12-11 16:28:44 -08001028 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001029 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001030
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001031 android_pixel_format native_pixel_format =
1032 GetNativePixelFormat(create_info->imageFormat);
1033 android_dataspace native_dataspace =
1034 GetNativeDataspace(create_info->imageColorSpace);
1035 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1036 ALOGE(
1037 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1038 "failed: Unsupported color space",
1039 create_info->imageColorSpace);
1040 return VK_ERROR_INITIALIZATION_FAILED;
1041 }
1042
Jesse Hall42a9eec2016-06-03 12:39:49 -07001043 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001044 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001045 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001046 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001047 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001048 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001049 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001050 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001051 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1052 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001053 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001054 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001055
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001056 Surface& surface = *SurfaceFromHandle(create_info->surface);
1057
Jesse Halldc225072016-05-30 22:40:14 -07001058 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001059 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001060 " because it already has active swapchain 0x%" PRIx64
1061 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1062 reinterpret_cast<uint64_t>(create_info->surface),
1063 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1064 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1065 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1066 }
1067 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1068 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1069
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001070 // -- Reset the native window --
1071 // The native window might have been used previously, and had its properties
1072 // changed from defaults. That will affect the answer we get for queries
1073 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1074 // attempt such queries.
1075
Jesse Halldc225072016-05-30 22:40:14 -07001076 // The native window only allows dequeueing all buffers before any have
1077 // been queued, since after that point at least one is assumed to be in
1078 // non-FREE state at any given time. Disconnecting and re-connecting
1079 // orphans the previous buffers, getting us back to the state where we can
1080 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001081 //
1082 // TODO(http://b/134186185) recycle swapchain images more efficiently
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001083 ANativeWindow* window = surface.window.get();
1084 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1085 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001086 strerror(-err), err);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001087 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1088 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
Jesse Halldc225072016-05-30 22:40:14 -07001089 strerror(-err), err);
1090
Nicolas Capens147b7da2021-04-09 14:53:06 -04001091 err =
1092 window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001093 if (err != android::OK) {
1094 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1095 strerror(-err), err);
1096 return VK_ERROR_SURFACE_LOST_KHR;
1097 }
1098
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301099 int swap_interval =
1100 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001101 err = window->setSwapInterval(window, swap_interval);
1102 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001103 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1104 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001105 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001106 }
1107
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001108 err = native_window_set_shared_buffer_mode(window, false);
1109 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001110 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1111 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001112 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001113 }
1114
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001115 err = native_window_set_auto_refresh(window, false);
1116 if (err != android::OK) {
Chris Forbesb8042d22017-01-18 18:07:05 +13001117 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1118 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001119 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001120 }
1121
Jesse Halld7b994a2015-09-07 14:17:37 -07001122 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001123
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001124 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001125
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001126 err = native_window_set_buffers_format(window, native_pixel_format);
1127 if (err != android::OK) {
Jesse Hall517274a2016-02-10 00:07:18 -08001128 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001129 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001130 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001131 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001132 err = native_window_set_buffers_data_space(window, native_dataspace);
1133 if (err != android::OK) {
Jesse Hall517274a2016-02-10 00:07:18 -08001134 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001135 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001136 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001137 }
1138
Jesse Hall3dd678a2016-01-08 21:52:01 -08001139 err = native_window_set_buffers_dimensions(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001140 window, static_cast<int>(create_info->imageExtent.width),
Jesse Hall3dd678a2016-01-08 21:52:01 -08001141 static_cast<int>(create_info->imageExtent.height));
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001142 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001143 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1144 create_info->imageExtent.width, create_info->imageExtent.height,
1145 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001146 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001147 }
1148
Jesse Hall178b6962016-02-24 15:39:50 -08001149 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1150 // applied during rendering. native_window_set_transform() expects the
1151 // inverse: the transform the app is requesting that the compositor perform
1152 // during composition. With native windows, pre-transform works by rendering
1153 // with the same transform the compositor is applying (as in Vulkan), but
1154 // then requesting the inverse transform, so that when the compositor does
1155 // it's job the two transforms cancel each other out and the compositor ends
1156 // up applying an identity transform to the app's buffer.
1157 err = native_window_set_buffers_transform(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001158 window, InvertTransformToNative(create_info->preTransform));
1159 if (err != android::OK) {
Jesse Hall178b6962016-02-24 15:39:50 -08001160 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1161 InvertTransformToNative(create_info->preTransform),
1162 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001163 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001164 }
1165
Jesse Hallf64ca122015-11-03 16:11:10 -08001166 err = native_window_set_scaling_mode(
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001167 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1168 if (err != android::OK) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001169 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1170 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001171 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001172 }
1173
Chris Forbes97ef4612017-03-30 19:37:50 +13001174 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1175 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1176 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1177 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001178 err = native_window_set_shared_buffer_mode(window, true);
1179 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001180 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1181 return VK_ERROR_SURFACE_LOST_KHR;
1182 }
1183 }
1184
1185 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001186 err = native_window_set_auto_refresh(window, true);
1187 if (err != android::OK) {
Chris Forbes97ef4612017-03-30 19:37:50 +13001188 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1189 return VK_ERROR_SURFACE_LOST_KHR;
1190 }
1191 }
1192
Jesse Halle6080bf2016-02-28 20:58:50 -08001193 int query_value;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001194 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1195 &query_value);
1196 if (err != android::OK || query_value < 0) {
Jesse Halle6080bf2016-02-28 20:58:50 -08001197 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1198 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001199 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001200 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001201 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001202 uint32_t num_images =
Ian Elliotta3515f42019-05-15 14:11:33 -06001203 (swap_interval ? create_info->minImageCount
1204 : std::max(3u, create_info->minImageCount)) -
1205 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001206
1207 // Lower layer insists that we have at least two buffers. This is wasteful
1208 // and we'd like to relax it in the shared case, but not all the pieces are
1209 // in place for that to work yet. Note we only lie to the lower layer-- we
1210 // don't want to give the app back a swapchain with extra images (which they
1211 // can't actually use!).
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001212 err = native_window_set_buffer_count(window, std::max(2u, num_images));
1213 if (err != android::OK) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001214 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1215 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001216 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001217 }
1218
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001219 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001220 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001221 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001222 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001223 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1224 device, create_info->imageFormat, create_info->imageUsage,
1225 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001226 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001227 if (result != VK_SUCCESS) {
1228 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001229 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001230 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001231 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001232 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001233 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001234 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001235 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001236 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001237 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001238 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001239 if (result != VK_SUCCESS) {
1240 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001241 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001242 }
Jesse Hall70f93352015-11-04 09:41:31 -08001243 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001244 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1245
1246 bool createProtectedSwapchain = false;
1247 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1248 createProtectedSwapchain = true;
1249 native_usage |= BufferUsage::PROTECTED;
1250 }
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001251 err = native_window_set_usage(window, native_usage);
1252 if (err != android::OK) {
Jesse Hall70f93352015-11-04 09:41:31 -08001253 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001254 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001255 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001256
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001257 int transform_hint;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001258 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1259 if (err != android::OK) {
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001260 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1261 strerror(-err), err);
1262 return VK_ERROR_SURFACE_LOST_KHR;
1263 }
1264
Jesse Halld7b994a2015-09-07 14:17:37 -07001265 // -- Allocate our Swapchain object --
1266 // After this point, we must deallocate the swapchain on error.
1267
Jesse Hall1f91d392015-12-11 16:28:44 -08001268 void* mem = allocator->pfnAllocation(allocator->pUserData,
1269 sizeof(Swapchain), alignof(Swapchain),
1270 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001271 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001272 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001273 Swapchain* swapchain = new (mem)
1274 Swapchain(surface, num_images, create_info->presentMode,
1275 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001276 // -- Dequeue all buffers and create a VkImage for each --
1277 // Any failures during or after this must cancel the dequeued buffers.
1278
Chris Forbesb56287a2017-01-12 14:28:58 +13001279 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1280#pragma clang diagnostic push
1281#pragma clang diagnostic ignored "-Wold-style-cast"
1282 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1283#pragma clang diagnostic pop
1284 .pNext = nullptr,
1285 .usage = swapchain_image_usage,
1286 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001287 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001288#pragma clang diagnostic push
1289#pragma clang diagnostic ignored "-Wold-style-cast"
1290 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1291#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001292 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001293 };
1294 VkImageCreateInfo image_create = {
1295 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1296 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001297 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001298 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001299 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001300 .extent = {0, 0, 1},
1301 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001302 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001303 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001304 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001305 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001306 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001307 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001308 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1309 };
1310
Jesse Halld7b994a2015-09-07 14:17:37 -07001311 for (uint32_t i = 0; i < num_images; i++) {
1312 Swapchain::Image& img = swapchain->images[i];
1313
1314 ANativeWindowBuffer* buffer;
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001315 err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1316 if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001317 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001318 switch (-err) {
1319 case ENOMEM:
1320 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1321 break;
1322 default:
1323 result = VK_ERROR_SURFACE_LOST_KHR;
1324 break;
1325 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001326 break;
1327 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001328 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001329 img.dequeued = true;
1330
1331 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001332 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1333 static_cast<uint32_t>(img.buffer->height),
1334 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001335 image_native_buffer.handle = img.buffer->handle;
1336 image_native_buffer.stride = img.buffer->stride;
1337 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001338 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001339 android_convertGralloc0To1Usage(int(img.buffer->usage),
1340 &image_native_buffer.usage2.producer,
1341 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001342
Yiwei Zhang533cea92019-06-03 18:43:24 -07001343 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001344 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001345 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001346 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001347 if (result != VK_SUCCESS) {
1348 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1349 break;
1350 }
1351 }
1352
1353 // -- Cancel all buffers, returning them to the queue --
1354 // If an error occurred before, also destroy the VkImage and release the
1355 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001356 for (uint32_t i = 0; i < num_images; i++) {
1357 Swapchain::Image& img = swapchain->images[i];
1358 if (img.dequeued) {
1359 if (!swapchain->shared) {
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001360 window->cancelBuffer(window, img.buffer.get(),
1361 img.dequeue_fence);
Chris Forbese0ced032017-03-30 19:44:15 +13001362 img.dequeue_fence = -1;
1363 img.dequeued = false;
1364 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001365 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001366 }
1367
1368 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001369 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1370 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001371 return result;
1372 }
1373
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001374 if (transform_hint != swapchain->pre_transform) {
1375 // Log that the app is not doing pre-rotation.
1376 android::GraphicsEnv::getInstance().setTargetStats(
1377 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1378 }
1379
Jesse Halldc225072016-05-30 22:40:14 -07001380 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1381 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001382 return VK_SUCCESS;
1383}
1384
Jesse Halle1b12782015-11-30 11:27:32 -08001385VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001386void DestroySwapchainKHR(VkDevice device,
1387 VkSwapchainKHR swapchain_handle,
1388 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001389 ATRACE_CALL();
1390
Yiwei Zhang533cea92019-06-03 18:43:24 -07001391 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001392}
1393
Jesse Halle1b12782015-11-30 11:27:32 -08001394VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001395VkResult GetSwapchainImagesKHR(VkDevice,
1396 VkSwapchainKHR swapchain_handle,
1397 uint32_t* count,
1398 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001399 ATRACE_CALL();
1400
Jesse Halld7b994a2015-09-07 14:17:37 -07001401 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001402 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1403 "getting images for non-active swapchain 0x%" PRIx64
1404 "; only dequeued image handles are valid",
1405 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001406 VkResult result = VK_SUCCESS;
1407 if (images) {
1408 uint32_t n = swapchain.num_images;
1409 if (*count < swapchain.num_images) {
1410 n = *count;
1411 result = VK_INCOMPLETE;
1412 }
1413 for (uint32_t i = 0; i < n; i++)
1414 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001415 *count = n;
1416 } else {
1417 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001418 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001419 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001420}
1421
Jesse Halle1b12782015-11-30 11:27:32 -08001422VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001423VkResult AcquireNextImageKHR(VkDevice device,
1424 VkSwapchainKHR swapchain_handle,
1425 uint64_t timeout,
1426 VkSemaphore semaphore,
1427 VkFence vk_fence,
1428 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001429 ATRACE_CALL();
1430
Jesse Halld7b994a2015-09-07 14:17:37 -07001431 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001432 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001433 VkResult result;
1434 int err;
1435
Jesse Halldc225072016-05-30 22:40:14 -07001436 if (swapchain.surface.swapchain_handle != swapchain_handle)
1437 return VK_ERROR_OUT_OF_DATE_KHR;
1438
Chris Forbesc88409c2017-03-30 19:47:37 +13001439 if (swapchain.shared) {
1440 // In shared mode, we keep the buffer dequeued all the time, so we don't
1441 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1442 // the semaphore and fence passed to us will be signalled.
1443 *image_index = 0;
1444 result = GetData(device).driver.AcquireImageANDROID(
1445 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1446 return result;
1447 }
1448
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001449 const nsecs_t acquire_next_image_timeout =
1450 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1451 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1452 // Cache the timeout to avoid the duplicate binder cost.
1453 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1454 acquire_next_image_timeout);
1455 if (err != android::OK) {
1456 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1457 strerror(-err), err);
1458 return VK_ERROR_SURFACE_LOST_KHR;
1459 }
1460 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1461 }
1462
Jesse Halld7b994a2015-09-07 14:17:37 -07001463 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001464 int fence_fd;
1465 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangc0f8a2c2020-04-30 20:23:13 -07001466 if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001467 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1468 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1469 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001470 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001471 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001472 }
1473
1474 uint32_t idx;
1475 for (idx = 0; idx < swapchain.num_images; idx++) {
1476 if (swapchain.images[idx].buffer.get() == buffer) {
1477 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001478 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001479 break;
1480 }
1481 }
1482 if (idx == swapchain.num_images) {
1483 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001484 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001485 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001486 }
1487
1488 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001489 if (fence_fd != -1) {
1490 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001491 if (fence_clone == -1) {
1492 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1493 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001494 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001495 }
1496 }
1497
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001498 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001499 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001500 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001501 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1502 // even if the call fails. We could close it ourselves on failure, but
1503 // that would create a race condition if the driver closes it on a
1504 // failure path: some other thread might create an fd with the same
1505 // number between the time the driver closes it and the time we close
1506 // it. We must assume one of: the driver *always* closes it even on
1507 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001508 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001509 swapchain.images[idx].dequeued = false;
1510 swapchain.images[idx].dequeue_fence = -1;
1511 return result;
1512 }
1513
1514 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001515 return VK_SUCCESS;
1516}
1517
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001518VKAPI_ATTR
1519VkResult AcquireNextImage2KHR(VkDevice device,
1520 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1521 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001522 ATRACE_CALL();
1523
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001524 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1525 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1526 pAcquireInfo->fence, pImageIndex);
1527}
1528
Jesse Halldc225072016-05-30 22:40:14 -07001529static VkResult WorstPresentResult(VkResult a, VkResult b) {
1530 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1531 // (in spec version 1.0.14).
1532 static const VkResult kWorstToBest[] = {
1533 VK_ERROR_DEVICE_LOST,
1534 VK_ERROR_SURFACE_LOST_KHR,
1535 VK_ERROR_OUT_OF_DATE_KHR,
1536 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1537 VK_ERROR_OUT_OF_HOST_MEMORY,
1538 VK_SUBOPTIMAL_KHR,
1539 };
1540 for (auto result : kWorstToBest) {
1541 if (a == result || b == result)
1542 return result;
1543 }
1544 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1545 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1546 return a != VK_SUCCESS ? a : b;
1547}
1548
Jesse Halle1b12782015-11-30 11:27:32 -08001549VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001550VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001551 ATRACE_CALL();
1552
Jesse Halld7b994a2015-09-07 14:17:37 -07001553 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1554 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1555 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001556
Jesse Halldc225072016-05-30 22:40:14 -07001557 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001558 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001559 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001560
Ian Elliottcb351132016-12-13 10:30:40 -07001561 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001562 const VkPresentRegionsKHR* present_regions = nullptr;
1563 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001564 const VkPresentRegionsKHR* next =
1565 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1566 while (next) {
1567 switch (next->sType) {
1568 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1569 present_regions = next;
1570 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001571 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001572 present_times =
1573 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1574 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001575 default:
1576 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1577 next->sType);
1578 break;
1579 }
1580 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1581 }
1582 ALOGV_IF(
1583 present_regions &&
1584 present_regions->swapchainCount != present_info->swapchainCount,
1585 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001586 ALOGV_IF(present_times &&
1587 present_times->swapchainCount != present_info->swapchainCount,
1588 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1589 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001590 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001591 (present_regions) ? present_regions->pRegions : nullptr;
1592 const VkPresentTimeGOOGLE* times =
1593 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001594 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001595 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001596 uint32_t nrects = 0;
1597
Jesse Halld7b994a2015-09-07 14:17:37 -07001598 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1599 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001600 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001601 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001602 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001603 const VkPresentRegionKHR* region =
1604 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001605 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001606 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001607 VkResult result;
1608 int err;
1609
Jesse Halld7b994a2015-09-07 14:17:37 -07001610 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001611 result = dispatch.QueueSignalReleaseImageANDROID(
1612 queue, present_info->waitSemaphoreCount,
1613 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001614 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001615 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001616 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001617 }
Yiwei Zhangda2e4792020-09-22 10:08:03 -07001618 if (img.release_fence >= 0)
1619 close(img.release_fence);
1620 img.release_fence = fence < 0 ? -1 : dup(fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001621
Jesse Halldc225072016-05-30 22:40:14 -07001622 if (swapchain.surface.swapchain_handle ==
1623 present_info->pSwapchains[sc]) {
1624 ANativeWindow* window = swapchain.surface.window.get();
1625 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001626 if (region) {
1627 // Process the incremental-present hint for this swapchain:
1628 uint32_t rcount = region->rectangleCount;
1629 if (rcount > nrects) {
1630 android_native_rect_t* new_rects =
1631 static_cast<android_native_rect_t*>(
1632 allocator->pfnReallocation(
1633 allocator->pUserData, rects,
1634 sizeof(android_native_rect_t) * rcount,
1635 alignof(android_native_rect_t),
1636 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1637 if (new_rects) {
1638 rects = new_rects;
1639 nrects = rcount;
1640 } else {
1641 rcount = 0; // Ignore the hint for this swapchain
1642 }
1643 }
1644 for (uint32_t r = 0; r < rcount; ++r) {
1645 if (region->pRectangles[r].layer > 0) {
1646 ALOGV(
1647 "vkQueuePresentKHR ignoring invalid layer "
1648 "(%u); using layer 0 instead",
1649 region->pRectangles[r].layer);
1650 }
1651 int x = region->pRectangles[r].offset.x;
1652 int y = region->pRectangles[r].offset.y;
1653 int width = static_cast<int>(
1654 region->pRectangles[r].extent.width);
1655 int height = static_cast<int>(
1656 region->pRectangles[r].extent.height);
1657 android_native_rect_t* cur_rect = &rects[r];
1658 cur_rect->left = x;
1659 cur_rect->top = y + height;
1660 cur_rect->right = x + width;
1661 cur_rect->bottom = y;
1662 }
1663 native_window_set_surface_damage(window, rects, rcount);
1664 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001665 if (time) {
1666 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001667 ALOGV(
1668 "Calling "
1669 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001670 native_window_enable_frame_timestamps(window, true);
1671 swapchain.frame_timestamps_enabled = true;
1672 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001673
1674 // Record the nativeFrameId so it can be later correlated to
1675 // this present.
1676 uint64_t nativeFrameId = 0;
1677 err = native_window_get_next_frame_id(
1678 window, &nativeFrameId);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001679 if (err != android::OK) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001680 ALOGE("Failed to get next native frame ID.");
1681 }
1682
1683 // Add a new timing record with the user's presentID and
1684 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001685 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001686 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001687 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001688 }
1689 if (time->desiredPresentTime) {
1690 // Set the desiredPresentTime:
1691 ALOGV(
1692 "Calling "
1693 "native_window_set_buffers_timestamp(%" PRId64 ")",
1694 time->desiredPresentTime);
1695 native_window_set_buffers_timestamp(
1696 window,
1697 static_cast<int64_t>(time->desiredPresentTime));
1698 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001699 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001700
Jesse Halldc225072016-05-30 22:40:14 -07001701 err = window->queueBuffer(window, img.buffer.get(), fence);
1702 // queueBuffer always closes fence, even on error
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001703 if (err != android::OK) {
Jesse Halldc225072016-05-30 22:40:14 -07001704 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1705 swapchain_result = WorstPresentResult(
Yiwei Zhang492dd5f2021-03-09 22:54:38 +00001706 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001707 } else {
1708 if (img.dequeue_fence >= 0) {
1709 close(img.dequeue_fence);
1710 img.dequeue_fence = -1;
1711 }
1712 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001713 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001714
1715 // If the swapchain is in shared mode, immediately dequeue the
1716 // buffer so it can be presented again without an intervening
1717 // call to AcquireNextImageKHR. We expect to get the same buffer
1718 // back from every call to dequeueBuffer in this mode.
1719 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1720 ANativeWindowBuffer* buffer;
1721 int fence_fd;
1722 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001723 if (err != android::OK) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001724 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1725 swapchain_result = WorstPresentResult(swapchain_result,
1726 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001727 } else if (img.buffer != buffer) {
Chris Forbesfca0f292017-03-30 19:48:39 +13001728 ALOGE("got wrong image back for shared swapchain");
1729 swapchain_result = WorstPresentResult(swapchain_result,
1730 VK_ERROR_SURFACE_LOST_KHR);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001731 } else {
Chris Forbesfca0f292017-03-30 19:48:39 +13001732 img.dequeue_fence = fence_fd;
1733 img.dequeued = true;
1734 }
1735 }
Jesse Halldc225072016-05-30 22:40:14 -07001736 }
1737 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001738 OrphanSwapchain(device, &swapchain);
1739 }
silence_dogood73597592019-05-23 16:57:37 -07001740 int window_transform_hint;
1741 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1742 &window_transform_hint);
Yiwei Zhangf5030b42019-12-19 00:10:04 -08001743 if (err != android::OK) {
silence_dogood73597592019-05-23 16:57:37 -07001744 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1745 strerror(-err), err);
1746 swapchain_result = WorstPresentResult(
1747 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1748 }
1749 if (swapchain.pre_transform != window_transform_hint) {
1750 swapchain_result =
1751 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1752 }
Jesse Halldc225072016-05-30 22:40:14 -07001753 } else {
Yiwei Zhangda2e4792020-09-22 10:08:03 -07001754 ReleaseSwapchainImage(device, nullptr, fence, img, true);
Jesse Halldc225072016-05-30 22:40:14 -07001755 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001756 }
1757
Jesse Halla9e57032015-11-30 01:03:10 -08001758 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001759 present_info->pResults[sc] = swapchain_result;
1760
1761 if (swapchain_result != final_result)
1762 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001763 }
Ian Elliottcb351132016-12-13 10:30:40 -07001764 if (rects) {
1765 allocator->pfnFree(allocator->pUserData, rects);
1766 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001767
1768 return final_result;
1769}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001770
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001771VKAPI_ATTR
1772VkResult GetRefreshCycleDurationGOOGLE(
1773 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001774 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001775 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001776 ATRACE_CALL();
1777
Ian Elliott62c48c92017-01-20 13:13:20 -07001778 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001779 VkResult result = VK_SUCCESS;
1780
Ian Elliott3568bfe2019-05-03 15:54:46 -06001781 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001782
1783 return result;
1784}
1785
1786VKAPI_ATTR
1787VkResult GetPastPresentationTimingGOOGLE(
1788 VkDevice,
1789 VkSwapchainKHR swapchain_handle,
1790 uint32_t* count,
1791 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001792 ATRACE_CALL();
1793
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001794 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001795 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1796 return VK_ERROR_OUT_OF_DATE_KHR;
1797 }
1798
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001799 ANativeWindow* window = swapchain.surface.window.get();
1800 VkResult result = VK_SUCCESS;
1801
1802 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001803 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001804 native_window_enable_frame_timestamps(window, true);
1805 swapchain.frame_timestamps_enabled = true;
1806 }
1807
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001808 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07001809 // Get the latest ready timing count before copying, since the copied
1810 // timing info will be erased in copy_ready_timings function.
1811 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07001812 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001813 // Check the *count here against the recorded ready timing count, since
1814 // *count can be overwritten per spec describes.
1815 if (*count < n) {
1816 result = VK_INCOMPLETE;
1817 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001818 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001819 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001820 }
1821
1822 return result;
1823}
1824
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001825VKAPI_ATTR
1826VkResult GetSwapchainStatusKHR(
1827 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001828 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001829 ATRACE_CALL();
1830
Chris Forbes4e18ba82017-01-20 12:50:17 +13001831 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001832 VkResult result = VK_SUCCESS;
1833
Chris Forbes4e18ba82017-01-20 12:50:17 +13001834 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1835 return VK_ERROR_OUT_OF_DATE_KHR;
1836 }
1837
Yiwei Zhanga885c062019-10-24 12:07:57 -07001838 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001839
1840 return result;
1841}
1842
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001843VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001844 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001845 uint32_t swapchainCount,
1846 const VkSwapchainKHR* pSwapchains,
1847 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001848 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001849
1850 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1851 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1852 if (!swapchain)
1853 continue;
1854
1855 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1856
1857 ANativeWindow* window = swapchain->surface.window.get();
1858
1859 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1860 const android_smpte2086_metadata smpteMetdata = {
1861 {vulkanMetadata.displayPrimaryRed.x,
1862 vulkanMetadata.displayPrimaryRed.y},
1863 {vulkanMetadata.displayPrimaryGreen.x,
1864 vulkanMetadata.displayPrimaryGreen.y},
1865 {vulkanMetadata.displayPrimaryBlue.x,
1866 vulkanMetadata.displayPrimaryBlue.y},
1867 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1868 vulkanMetadata.maxLuminance,
1869 vulkanMetadata.minLuminance};
1870 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1871
1872 const android_cta861_3_metadata cta8613Metadata = {
1873 vulkanMetadata.maxContentLightLevel,
1874 vulkanMetadata.maxFrameAverageLightLevel};
1875 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1876 }
1877
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001878 return;
1879}
1880
Yiwei Zhang0f475222019-04-11 19:38:00 -07001881static void InterceptBindImageMemory2(
1882 uint32_t bind_info_count,
1883 const VkBindImageMemoryInfo* bind_infos,
1884 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1885 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1886 out_native_buffers->clear();
1887 out_bind_infos->clear();
1888
1889 if (!bind_info_count)
1890 return;
1891
1892 std::unordered_set<uint32_t> intercepted_indexes;
1893
1894 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1895 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1896 bind_infos[idx].pNext);
1897 while (info &&
1898 info->sType !=
1899 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1900 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1901 info->pNext);
1902 }
1903
1904 if (!info)
1905 continue;
1906
1907 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1908 "swapchain handle must not be NULL");
1909 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1910 ALOG_ASSERT(
1911 info->imageIndex < swapchain->num_images,
1912 "imageIndex must be less than the number of images in swapchain");
1913
1914 ANativeWindowBuffer* buffer =
1915 swapchain->images[info->imageIndex].buffer.get();
1916 VkNativeBufferANDROID native_buffer = {
1917#pragma clang diagnostic push
1918#pragma clang diagnostic ignored "-Wold-style-cast"
1919 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1920#pragma clang diagnostic pop
1921 .pNext = bind_infos[idx].pNext,
1922 .handle = buffer->handle,
1923 .stride = buffer->stride,
1924 .format = buffer->format,
1925 .usage = int(buffer->usage),
1926 };
1927 // Reserve enough space to avoid letting re-allocation invalidate the
1928 // addresses of the elements inside.
1929 out_native_buffers->reserve(bind_info_count);
1930 out_native_buffers->emplace_back(native_buffer);
1931
1932 // Reserve the space now since we know how much is needed now.
1933 out_bind_infos->reserve(bind_info_count);
1934 out_bind_infos->emplace_back(bind_infos[idx]);
1935 out_bind_infos->back().pNext = &out_native_buffers->back();
1936
1937 intercepted_indexes.insert(idx);
1938 }
1939
1940 if (intercepted_indexes.empty())
1941 return;
1942
1943 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1944 if (intercepted_indexes.count(idx))
1945 continue;
1946 out_bind_infos->emplace_back(bind_infos[idx]);
1947 }
1948}
1949
Yiwei Zhang23143102019-04-10 18:24:05 -07001950VKAPI_ATTR
1951VkResult BindImageMemory2(VkDevice device,
1952 uint32_t bindInfoCount,
1953 const VkBindImageMemoryInfo* pBindInfos) {
1954 ATRACE_CALL();
1955
Yiwei Zhang0f475222019-04-11 19:38:00 -07001956 // out_native_buffers is for maintaining the lifecycle of the constructed
1957 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1958 std::vector<VkNativeBufferANDROID> out_native_buffers;
1959 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1960 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1961 &out_bind_infos);
1962 return GetData(device).driver.BindImageMemory2(
1963 device, bindInfoCount,
1964 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001965}
1966
1967VKAPI_ATTR
1968VkResult BindImageMemory2KHR(VkDevice device,
1969 uint32_t bindInfoCount,
1970 const VkBindImageMemoryInfo* pBindInfos) {
1971 ATRACE_CALL();
1972
Yiwei Zhang0f475222019-04-11 19:38:00 -07001973 std::vector<VkNativeBufferANDROID> out_native_buffers;
1974 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1975 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1976 &out_bind_infos);
1977 return GetData(device).driver.BindImageMemory2KHR(
1978 device, bindInfoCount,
1979 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001980}
1981
Chia-I Wu62262232016-03-26 07:06:44 +08001982} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001983} // namespace vulkan