blob: 761d128d793c872c8b9b566db380f9934df93e21 [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>
Mark Salyzyn7823e122016-09-29 08:08:05 -070021#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070022#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070023#include <system/window.h>
24#include <ui/BufferQueueDefs.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080025#include <utils/StrongPointer.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080026#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070027
28#include <algorithm>
29#include <unordered_set>
30#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070031
Chia-I Wu4a6a9162016-03-26 07:17:34 +080032#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070033
Daniel Kochf25f5bb2017-10-05 00:26:58 -040034using android::hardware::graphics::common::V1_0::BufferUsage;
35
Chia-I Wu62262232016-03-26 07:06:44 +080036namespace vulkan {
37namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070038
Jesse Halld7b994a2015-09-07 14:17:37 -070039namespace {
40
Jesse Hall55bc0972016-02-23 16:43:29 -080041const VkSurfaceTransformFlagsKHR kSupportedTransforms =
42 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
43 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
44 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
45 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070046 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
47 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
48 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
49 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080050 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
51
52VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
53 // Native and Vulkan transforms are isomorphic, but are represented
54 // differently. Vulkan transforms are built up of an optional horizontal
55 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
56 // transforms are built up from a horizontal flip, vertical flip, and
57 // 90-degree rotation, all optional but always in that order.
58
Jesse Hall55bc0972016-02-23 16:43:29 -080059 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070060 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080061 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070062 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
63 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
64 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
65 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
66 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080067 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070068 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080069 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070070 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
71 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
72 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
73 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
74 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -080075 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
76 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
77 default:
78 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
79 }
80}
81
Yiwei Zhang70a21962019-05-31 17:26:52 -070082int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
83 switch (transform) {
84 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
85 return NATIVE_WINDOW_TRANSFORM_ROT_90;
86 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
87 return NATIVE_WINDOW_TRANSFORM_ROT_180;
88 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
89 return NATIVE_WINDOW_TRANSFORM_ROT_270;
90 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
91 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
92 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
93 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
94 NATIVE_WINDOW_TRANSFORM_ROT_90;
95 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
96 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
97 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
98 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
99 NATIVE_WINDOW_TRANSFORM_ROT_90;
100 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
101 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
102 default:
103 return 0;
104 }
105}
106
Jesse Hall178b6962016-02-24 15:39:50 -0800107int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
108 switch (transform) {
109 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
110 return NATIVE_WINDOW_TRANSFORM_ROT_270;
111 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
112 return NATIVE_WINDOW_TRANSFORM_ROT_180;
113 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
114 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700115 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
116 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
117 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
118 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
119 NATIVE_WINDOW_TRANSFORM_ROT_90;
120 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
121 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
122 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
123 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
124 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800125 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
126 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
127 default:
128 return 0;
129 }
130}
131
Ian Elliott8a977262017-01-19 09:05:58 -0700132class TimingInfo {
133 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800134 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700135 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800136 native_frame_id_(nativeFrameId) {}
137 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700138 return (timestamp_desired_present_time_ !=
139 NATIVE_WINDOW_TIMESTAMP_PENDING &&
140 timestamp_actual_present_time_ !=
141 NATIVE_WINDOW_TIMESTAMP_PENDING &&
142 timestamp_render_complete_time_ !=
143 NATIVE_WINDOW_TIMESTAMP_PENDING &&
144 timestamp_composition_latch_time_ !=
145 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700146 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700147 void calculate(int64_t rdur) {
148 bool anyTimestampInvalid =
149 (timestamp_actual_present_time_ ==
150 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
151 (timestamp_render_complete_time_ ==
152 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
153 (timestamp_composition_latch_time_ ==
154 NATIVE_WINDOW_TIMESTAMP_INVALID);
155 if (anyTimestampInvalid) {
156 ALOGE("Unexpectedly received invalid timestamp.");
157 vals_.actualPresentTime = 0;
158 vals_.earliestPresentTime = 0;
159 vals_.presentMargin = 0;
160 return;
161 }
162
163 vals_.actualPresentTime =
164 static_cast<uint64_t>(timestamp_actual_present_time_);
165 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700166 timestamp_render_complete_time_);
167 // Calculate vals_.earliestPresentTime, and potentially adjust
168 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
169 // is vals_.actualPresentTime. If we can subtract rdur (the duration
170 // of a refresh cycle) from vals_.earliestPresentTime (and also from
171 // vals_.presentMargin) and still leave a positive margin, then we can
172 // report to the application that it could have presented earlier than
173 // it did (per the extension specification). If for some reason, we
174 // can do this subtraction repeatedly, we do, since
175 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700176 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700177 while ((margin > rdur) &&
178 ((early_time - rdur) > timestamp_composition_latch_time_)) {
179 early_time -= rdur;
180 margin -= rdur;
181 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700182 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
183 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700184 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800185 void get_values(VkPastPresentationTimingGOOGLE* values) const {
186 *values = vals_;
187 }
Ian Elliott8a977262017-01-19 09:05:58 -0700188
189 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800190 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700191
Brian Anderson1049d1d2016-12-16 17:25:57 -0800192 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700193 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
194 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
195 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
196 int64_t timestamp_composition_latch_time_
197 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700198};
199
Jesse Hall1356b0d2015-11-23 17:24:58 -0800200struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800201 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700202 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700203 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800204};
205
206VkSurfaceKHR HandleFromSurface(Surface* surface) {
207 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
208}
209
210Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800211 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800212}
213
Ian Elliott8a977262017-01-19 09:05:58 -0700214// Maximum number of TimingInfo structs to keep per swapchain:
215enum { MAX_TIMING_INFOS = 10 };
216// Minimum number of frames to look for in the past (so we don't cause
217// syncronous requests to Surface Flinger):
218enum { MIN_NUM_FRAMES_AGO = 5 };
219
Jesse Hall1356b0d2015-11-23 17:24:58 -0800220struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700221 Swapchain(Surface& surface_,
222 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700223 VkPresentModeKHR present_mode,
224 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700225 : surface(surface_),
226 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700227 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700228 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300229 frame_timestamps_enabled(false),
230 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
231 present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700232 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700233 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700234 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700235 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700236 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600237 uint64_t get_refresh_duration()
238 {
239 ANativeWindow* window = surface.window.get();
240 native_window_get_refresh_cycle_duration(
241 window,
242 &refresh_duration);
243 return static_cast<uint64_t>(refresh_duration);
244
245 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800246
247 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700248 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700249 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700250 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700251 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700252 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300253 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700254
255 struct Image {
256 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
257 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800258 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700259 // The fence is only valid when the buffer is dequeued, and should be
260 // -1 any other time. When valid, we own the fd, and must ensure it is
261 // closed: either by closing it explicitly when queueing the buffer,
262 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
263 int dequeue_fence;
264 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800265 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700266
Yiwei Zhang5e862202019-06-21 14:59:16 -0700267 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700268};
269
270VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
271 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
272}
273
274Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800275 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700276}
277
Jesse Halldc225072016-05-30 22:40:14 -0700278void ReleaseSwapchainImage(VkDevice device,
279 ANativeWindow* window,
280 int release_fence,
281 Swapchain::Image& image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700282 ATRACE_CALL();
283
Jesse Halldc225072016-05-30 22:40:14 -0700284 ALOG_ASSERT(release_fence == -1 || image.dequeued,
285 "ReleaseSwapchainImage: can't provide a release fence for "
286 "non-dequeued images");
287
288 if (image.dequeued) {
289 if (release_fence >= 0) {
290 // We get here from vkQueuePresentKHR. The application is
291 // responsible for creating an execution dependency chain from
292 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
293 // (release_fence), so we can drop the dequeue_fence here.
294 if (image.dequeue_fence >= 0)
295 close(image.dequeue_fence);
296 } else {
297 // We get here during swapchain destruction, or various serious
298 // error cases e.g. when we can't create the release_fence during
299 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
300 // have already signalled, since the swapchain images are supposed
301 // to be idle before the swapchain is destroyed. In error cases,
302 // there may be rendering in flight to the image, but since we
303 // weren't able to create a release_fence, waiting for the
304 // dequeue_fence is about the best we can do.
305 release_fence = image.dequeue_fence;
306 }
307 image.dequeue_fence = -1;
308
309 if (window) {
310 window->cancelBuffer(window, image.buffer.get(), release_fence);
311 } else {
312 if (release_fence >= 0) {
313 sync_wait(release_fence, -1 /* forever */);
314 close(release_fence);
315 }
316 }
317
318 image.dequeued = false;
319 }
320
321 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700322 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700323 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700324 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700325 image.image = VK_NULL_HANDLE;
326 }
327
328 image.buffer.clear();
329}
330
331void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
332 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
333 return;
Jesse Halldc225072016-05-30 22:40:14 -0700334 for (uint32_t i = 0; i < swapchain->num_images; i++) {
335 if (!swapchain->images[i].dequeued)
336 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
337 }
338 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700339 swapchain->timing.clear();
340}
341
342uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800343 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
344 return 0;
345 }
Ian Elliott8a977262017-01-19 09:05:58 -0700346
Brian Anderson1049d1d2016-12-16 17:25:57 -0800347 uint32_t num_ready = 0;
348 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
349 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700350 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800351 if (ti.ready()) {
352 // This TimingInfo is ready to be reported to the user. Add it
353 // to the num_ready.
354 num_ready++;
355 continue;
356 }
357 // This TimingInfo is not yet ready to be reported to the user,
358 // and so we should look for any available timestamps that
359 // might make it ready.
360 int64_t desired_present_time = 0;
361 int64_t render_complete_time = 0;
362 int64_t composition_latch_time = 0;
363 int64_t actual_present_time = 0;
364 // Obtain timestamps:
365 int ret = native_window_get_frame_timestamps(
366 swapchain.surface.window.get(), ti.native_frame_id_,
367 &desired_present_time, &render_complete_time,
368 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700369 nullptr, //&first_composition_start_time,
370 nullptr, //&last_composition_start_time,
371 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800372 // TODO(ianelliott): Maybe ask if this one is
373 // supported, at startup time (since it may not be
374 // supported):
375 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700376 nullptr, //&dequeue_ready_time,
377 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800378
379 if (ret != android::NO_ERROR) {
380 continue;
381 }
382
383 // Record the timestamp(s) we received, and then see if this TimingInfo
384 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700385 ti.timestamp_desired_present_time_ = desired_present_time;
386 ti.timestamp_actual_present_time_ = actual_present_time;
387 ti.timestamp_render_complete_time_ = render_complete_time;
388 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800389
390 if (ti.ready()) {
391 // The TimingInfo has received enough timestamps, and should now
392 // use those timestamps to calculate the info that should be
393 // reported to the user:
394 ti.calculate(swapchain.refresh_duration);
395 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700396 }
397 }
398 return num_ready;
399}
400
401// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
402void copy_ready_timings(Swapchain& swapchain,
403 uint32_t* count,
404 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800405 if (swapchain.timing.empty()) {
406 *count = 0;
407 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700408 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800409
410 size_t last_ready = swapchain.timing.size() - 1;
411 while (!swapchain.timing[last_ready].ready()) {
412 if (last_ready == 0) {
413 *count = 0;
414 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700415 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800416 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700417 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800418
419 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700420 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800421 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
422 const TimingInfo& ti = swapchain.timing[i];
423 if (ti.ready()) {
424 ti.get_values(&timings[num_copied]);
425 num_copied++;
426 }
427 num_to_remove++;
428 }
429
430 // Discard old frames that aren't ready if newer frames are ready.
431 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700432 swapchain.timing.erase(swapchain.timing.begin(),
433 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800434
Ian Elliott8a977262017-01-19 09:05:58 -0700435 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700436}
437
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700438android_pixel_format GetNativePixelFormat(VkFormat format) {
439 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
440 switch (format) {
441 case VK_FORMAT_R8G8B8A8_UNORM:
442 case VK_FORMAT_R8G8B8A8_SRGB:
443 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
444 break;
445 case VK_FORMAT_R5G6B5_UNORM_PACK16:
446 native_format = HAL_PIXEL_FORMAT_RGB_565;
447 break;
448 case VK_FORMAT_R16G16B16A16_SFLOAT:
449 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
450 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800451 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700452 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
453 break;
454 default:
455 ALOGV("unsupported swapchain format %d", format);
456 break;
457 }
458 return native_format;
459}
460
461android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
462 switch (colorspace) {
463 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
464 return HAL_DATASPACE_V0_SRGB;
465 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
466 return HAL_DATASPACE_DISPLAY_P3;
467 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
468 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600469 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
470 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700471 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
472 return HAL_DATASPACE_DCI_P3_LINEAR;
473 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
474 return HAL_DATASPACE_DCI_P3;
475 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
476 return HAL_DATASPACE_V0_SRGB_LINEAR;
477 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
478 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600479 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
480 return HAL_DATASPACE_BT2020_LINEAR;
481 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700482 return static_cast<android_dataspace>(
483 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
484 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600485 case VK_COLOR_SPACE_DOLBYVISION_EXT:
486 return static_cast<android_dataspace>(
487 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
488 HAL_DATASPACE_RANGE_FULL);
489 case VK_COLOR_SPACE_HDR10_HLG_EXT:
490 return static_cast<android_dataspace>(
491 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
492 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700493 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
494 return static_cast<android_dataspace>(
495 HAL_DATASPACE_STANDARD_ADOBE_RGB |
496 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
497 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
498 return HAL_DATASPACE_ADOBE_RGB;
499
500 // Pass through is intended to allow app to provide data that is passed
501 // to the display system without modification.
502 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
503 return HAL_DATASPACE_ARBITRARY;
504
505 default:
506 // This indicates that we don't know about the
507 // dataspace specified and we should indicate that
508 // it's unsupported
509 return HAL_DATASPACE_UNKNOWN;
510 }
511}
512
Jesse Halld7b994a2015-09-07 14:17:37 -0700513} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700514
Jesse Halle1b12782015-11-30 11:27:32 -0800515VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800516VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800517 VkInstance instance,
518 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
519 const VkAllocationCallbacks* allocator,
520 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800521 ATRACE_CALL();
522
Jesse Hall1f91d392015-12-11 16:28:44 -0800523 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800524 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800525 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
526 alignof(Surface),
527 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800528 if (!mem)
529 return VK_ERROR_OUT_OF_HOST_MEMORY;
530 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700531
Chia-I Wue8e689f2016-04-18 08:21:31 +0800532 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700533 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700534 int err = native_window_get_consumer_usage(surface->window.get(),
535 &surface->consumer_usage);
536 if (err != android::NO_ERROR) {
537 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
538 strerror(-err), err);
539 surface->~Surface();
540 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700541 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700542 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700543
Yiwei Zhang6435b322018-05-08 11:12:17 -0700544 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800545 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
546 if (err != 0) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800547 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
548 err);
549 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800550 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700551 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800552 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700553
Jesse Hall1356b0d2015-11-23 17:24:58 -0800554 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700555 return VK_SUCCESS;
556}
557
Jesse Halle1b12782015-11-30 11:27:32 -0800558VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800559void DestroySurfaceKHR(VkInstance instance,
560 VkSurfaceKHR surface_handle,
561 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800562 ATRACE_CALL();
563
Jesse Hall1356b0d2015-11-23 17:24:58 -0800564 Surface* surface = SurfaceFromHandle(surface_handle);
565 if (!surface)
566 return;
567 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700568 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700569 "destroyed VkSurfaceKHR 0x%" PRIx64
570 " has active VkSwapchainKHR 0x%" PRIx64,
571 reinterpret_cast<uint64_t>(surface_handle),
572 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800573 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800574 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800575 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800576 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800577}
578
Jesse Halle1b12782015-11-30 11:27:32 -0800579VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800580VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
581 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700582 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800583 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800584 ATRACE_CALL();
585
Yiwei Zhang6435b322018-05-08 11:12:17 -0700586 const Surface* surface = SurfaceFromHandle(surface_handle);
587 if (!surface) {
588 return VK_ERROR_SURFACE_LOST_KHR;
589 }
590 const ANativeWindow* window = surface->window.get();
591
592 int query_value;
593 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
594 if (err != 0 || query_value < 0) {
595 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
596 strerror(-err), err, query_value);
597 return VK_ERROR_SURFACE_LOST_KHR;
598 }
599
600 android_pixel_format native_format =
601 static_cast<android_pixel_format>(query_value);
602
603 bool format_supported = false;
604 switch (native_format) {
605 case HAL_PIXEL_FORMAT_RGBA_8888:
606 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700607 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800608 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700609 format_supported = true;
610 break;
611 default:
612 break;
613 }
614
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700615 *supported = static_cast<VkBool32>(
616 format_supported || (surface->consumer_usage &
617 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
618 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700619
Jesse Halla6429252015-11-29 18:59:42 -0800620 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800621}
622
Jesse Halle1b12782015-11-30 11:27:32 -0800623VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800624VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800625 VkPhysicalDevice /*pdev*/,
626 VkSurfaceKHR surface,
627 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800628 ATRACE_CALL();
629
Jesse Halld7b994a2015-09-07 14:17:37 -0700630 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800631 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700632
633 int width, height;
634 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
635 if (err != 0) {
636 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
637 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700638 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700639 }
640 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
641 if (err != 0) {
642 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
643 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700644 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700645 }
646
Jesse Hall55bc0972016-02-23 16:43:29 -0800647 int transform_hint;
648 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
649 if (err != 0) {
650 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
651 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700652 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800653 }
654
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800655 int max_buffer_count;
656 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
657 if (err != 0) {
658 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
659 strerror(-err), err);
660 return VK_ERROR_SURFACE_LOST_KHR;
661 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700662 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800663 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700664
Jesse Hallfe2662d2016-02-09 13:26:59 -0800665 capabilities->currentExtent =
666 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
667
Yiwei Zhang70a21962019-05-31 17:26:52 -0700668 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800669 capabilities->minImageExtent = VkExtent2D{1, 1};
670 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700671
Jesse Hallfe2662d2016-02-09 13:26:59 -0800672 capabilities->maxImageArrayLayers = 1;
673
Jesse Hall55bc0972016-02-23 16:43:29 -0800674 capabilities->supportedTransforms = kSupportedTransforms;
675 capabilities->currentTransform =
676 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700677
Jesse Hallfe2662d2016-02-09 13:26:59 -0800678 // On Android, window composition is a WindowManager property, not something
679 // associated with the bufferqueue. It can't be changed from here.
680 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700681
Jesse Hallb00daad2015-11-29 19:46:20 -0800682 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800683 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
684 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
685 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700686 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
687
Jesse Hallb1352bc2015-09-04 16:12:33 -0700688 return VK_SUCCESS;
689}
690
Jesse Halle1b12782015-11-30 11:27:32 -0800691VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700692VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
693 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800694 uint32_t* count,
695 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800696 ATRACE_CALL();
697
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700698 const InstanceData& instance_data = GetData(pdev);
699
Jesse Hall1356b0d2015-11-23 17:24:58 -0800700 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
701 // a new gralloc method to query whether a (format, usage) pair is
702 // supported, and check that for each gralloc format that corresponds to a
703 // Vulkan format. Shorter term, just add a few more formats to the ones
704 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700705
706 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700707 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
708 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
709 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Ian Elliott94ace212019-02-20 14:05:29 -0700710 {VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
711 {VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700712 };
713 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700714 uint32_t total_num_formats = kNumFormats;
715
716 bool wide_color_support = false;
717 Surface& surface = *SurfaceFromHandle(surface_handle);
718 int err = native_window_get_wide_color_support(surface.window.get(),
719 &wide_color_support);
720 if (err) {
Yiwei Zhang70a21962019-05-31 17:26:52 -0700721 return VK_ERROR_SURFACE_LOST_KHR;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700722 }
723 ALOGV("wide_color_support is: %d", wide_color_support);
724 wide_color_support =
725 wide_color_support &&
726 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
727
728 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600729 {VK_FORMAT_R8G8B8A8_UNORM,
730 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
731 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700732 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700733 {VK_FORMAT_R16G16B16A16_SFLOAT,
734 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
735 {VK_FORMAT_R16G16B16A16_SFLOAT,
736 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800737 {VK_FORMAT_A2B10G10R10_UNORM_PACK32,
738 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700739 };
740 const uint32_t kNumWideColorFormats =
741 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
742 if (wide_color_support) {
743 total_num_formats += kNumWideColorFormats;
744 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700745
746 VkResult result = VK_SUCCESS;
747 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700748 uint32_t out_count = 0;
749 uint32_t transfer_count = 0;
750 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700751 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700752 transfer_count = std::min(*count, kNumFormats);
753 std::copy(kFormats, kFormats + transfer_count, formats);
754 out_count += transfer_count;
755 if (wide_color_support) {
756 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
757 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
758 formats + out_count);
759 out_count += transfer_count;
760 }
761 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700762 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700763 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700764 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700765 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700766}
767
Jesse Halle1b12782015-11-30 11:27:32 -0800768VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300769VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
770 VkPhysicalDevice physicalDevice,
771 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
772 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800773 ATRACE_CALL();
774
Chris Forbes2452cf72017-03-16 16:30:17 +1300775 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
776 physicalDevice, pSurfaceInfo->surface,
777 &pSurfaceCapabilities->surfaceCapabilities);
778
Chris Forbes06bc0092017-03-16 16:46:05 +1300779 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
780 while (caps->pNext) {
781 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
782
783 switch (caps->sType) {
784 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
785 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
786 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
787 caps);
788 // Claim same set of usage flags are supported for
789 // shared present modes as for other modes.
790 shared_caps->sharedPresentSupportedUsageFlags =
791 pSurfaceCapabilities->surfaceCapabilities
792 .supportedUsageFlags;
793 } break;
794
795 default:
796 // Ignore all other extension structs
797 break;
798 }
799 }
800
Chris Forbes2452cf72017-03-16 16:30:17 +1300801 return result;
802}
803
804VKAPI_ATTR
805VkResult GetPhysicalDeviceSurfaceFormats2KHR(
806 VkPhysicalDevice physicalDevice,
807 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
808 uint32_t* pSurfaceFormatCount,
809 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800810 ATRACE_CALL();
811
Chris Forbes2452cf72017-03-16 16:30:17 +1300812 if (!pSurfaceFormats) {
813 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
814 pSurfaceInfo->surface,
815 pSurfaceFormatCount, nullptr);
816 } else {
817 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
818 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700819 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300820 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
821 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700822 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300823
824 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
825 // marshal results individually due to stride difference.
826 // completely ignore any chained extension structs.
827 uint32_t formats_to_marshal = *pSurfaceFormatCount;
828 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
829 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
830 }
831 }
832
833 return result;
834 }
835}
836
837VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300838VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800839 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800840 uint32_t* count,
841 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800842 ATRACE_CALL();
843
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800844 int err;
845 int query_value;
846 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
847
848 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
849 if (err != 0 || query_value < 0) {
850 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
851 strerror(-err), err, query_value);
852 return VK_ERROR_SURFACE_LOST_KHR;
853 }
854 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
855
856 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
857 if (err != 0 || query_value < 0) {
858 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
859 strerror(-err), err, query_value);
860 return VK_ERROR_SURFACE_LOST_KHR;
861 }
862 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
863
Yiwei Zhang5e862202019-06-21 14:59:16 -0700864 std::vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800865 if (min_undequeued_buffers + 1 < max_buffer_count)
866 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300867 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
868
869 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
870 if (QueryPresentationProperties(pdev, &present_properties)) {
871 if (present_properties.sharedImage) {
872 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
873 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
874 }
875 }
876
877 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700878
879 VkResult result = VK_SUCCESS;
880 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300881 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700882 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300883 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -0700884 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700885 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300886 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700887 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700888 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700889}
890
Jesse Halle1b12782015-11-30 11:27:32 -0800891VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400892VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600893 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400894 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800895 ATRACE_CALL();
896
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400897 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
898 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
899 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
900 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
901 pDeviceGroupPresentCapabilities->sType);
902
903 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
904 sizeof(pDeviceGroupPresentCapabilities->presentMask));
905
906 // assume device group of size 1
907 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
908 pDeviceGroupPresentCapabilities->modes =
909 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
910
911 return VK_SUCCESS;
912}
913
914VKAPI_ATTR
915VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600916 VkDevice,
917 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400918 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800919 ATRACE_CALL();
920
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400921 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
922 return VK_SUCCESS;
923}
924
925VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600926VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400927 VkSurfaceKHR surface,
928 uint32_t* pRectCount,
929 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800930 ATRACE_CALL();
931
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400932 if (!pRects) {
933 *pRectCount = 1;
934 } else {
935 uint32_t count = std::min(*pRectCount, 1u);
936 bool incomplete = *pRectCount < 1;
937
938 *pRectCount = count;
939
940 if (incomplete) {
941 return VK_INCOMPLETE;
942 }
943
944 int err;
945 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
946
947 int width = 0, height = 0;
948 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
949 if (err != 0) {
950 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
951 strerror(-err), err);
952 }
953 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
954 if (err != 0) {
955 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
956 strerror(-err), err);
957 }
958
959 // TODO: Return something better than "whole window"
960 pRects[0].offset.x = 0;
961 pRects[0].offset.y = 0;
962 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
963 static_cast<uint32_t>(height)};
964 }
965 return VK_SUCCESS;
966}
967
Yiwei Zhang533cea92019-06-03 18:43:24 -0700968static void DestroySwapchainInternal(VkDevice device,
969 VkSwapchainKHR swapchain_handle,
970 const VkAllocationCallbacks* allocator) {
971 ATRACE_CALL();
972
973 const auto& dispatch = GetData(device).driver;
974 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
975 if (!swapchain) {
976 return;
977 }
978
979 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
980 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
981
982 if (window && swapchain->frame_timestamps_enabled) {
983 native_window_enable_frame_timestamps(window, false);
984 }
985
986 for (uint32_t i = 0; i < swapchain->num_images; i++) {
987 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
988 }
989
990 if (active) {
991 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
992 }
993
994 if (!allocator) {
995 allocator = &GetData(device).allocator;
996 }
997
998 swapchain->~Swapchain();
999 allocator->pfnFree(allocator->pUserData, swapchain);
1000}
1001
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001002VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001003VkResult CreateSwapchainKHR(VkDevice device,
1004 const VkSwapchainCreateInfoKHR* create_info,
1005 const VkAllocationCallbacks* allocator,
1006 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001007 ATRACE_CALL();
1008
Jesse Halld7b994a2015-09-07 14:17:37 -07001009 int err;
1010 VkResult result = VK_SUCCESS;
1011
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001012 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1013 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1014 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1015 " oldSwapchain=0x%" PRIx64,
1016 reinterpret_cast<uint64_t>(create_info->surface),
1017 create_info->minImageCount, create_info->imageFormat,
1018 create_info->imageColorSpace, create_info->imageExtent.width,
1019 create_info->imageExtent.height, create_info->imageUsage,
1020 create_info->preTransform, create_info->presentMode,
1021 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1022
Jesse Hall1f91d392015-12-11 16:28:44 -08001023 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001024 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001025
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001026 android_pixel_format native_pixel_format =
1027 GetNativePixelFormat(create_info->imageFormat);
1028 android_dataspace native_dataspace =
1029 GetNativeDataspace(create_info->imageColorSpace);
1030 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1031 ALOGE(
1032 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1033 "failed: Unsupported color space",
1034 create_info->imageColorSpace);
1035 return VK_ERROR_INITIALIZATION_FAILED;
1036 }
1037
Jesse Hall42a9eec2016-06-03 12:39:49 -07001038 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001039 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001040 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001041 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001042 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001043 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001044 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001045 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001046 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1047 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001048 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001049 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001050
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001051 Surface& surface = *SurfaceFromHandle(create_info->surface);
1052
Jesse Halldc225072016-05-30 22:40:14 -07001053 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001054 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001055 " because it already has active swapchain 0x%" PRIx64
1056 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1057 reinterpret_cast<uint64_t>(create_info->surface),
1058 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1059 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1060 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1061 }
1062 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1063 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1064
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001065 // -- Reset the native window --
1066 // The native window might have been used previously, and had its properties
1067 // changed from defaults. That will affect the answer we get for queries
1068 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1069 // attempt such queries.
1070
Jesse Halldc225072016-05-30 22:40:14 -07001071 // The native window only allows dequeueing all buffers before any have
1072 // been queued, since after that point at least one is assumed to be in
1073 // non-FREE state at any given time. Disconnecting and re-connecting
1074 // orphans the previous buffers, getting us back to the state where we can
1075 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001076 //
1077 // TODO(http://b/134186185) recycle swapchain images more efficiently
Jesse Halldc225072016-05-30 22:40:14 -07001078 err = native_window_api_disconnect(surface.window.get(),
1079 NATIVE_WINDOW_API_EGL);
1080 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1081 strerror(-err), err);
1082 err =
1083 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1084 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1085 strerror(-err), err);
1086
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001087 err = native_window_set_buffer_count(surface.window.get(), 0);
1088 if (err != 0) {
1089 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1090 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001091 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001092 }
1093
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301094 int swap_interval =
1095 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1096 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001097 if (err != 0) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001098 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1099 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001100 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001101 }
1102
Chris Forbesb8042d22017-01-18 18:07:05 +13001103 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1104 if (err != 0) {
1105 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1106 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001107 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001108 }
1109
1110 err = native_window_set_auto_refresh(surface.window.get(), false);
1111 if (err != 0) {
1112 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1113 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001114 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001115 }
1116
Jesse Halld7b994a2015-09-07 14:17:37 -07001117 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001118
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001119 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001120
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001121 err = native_window_set_buffers_format(surface.window.get(),
1122 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001123 if (err != 0) {
Jesse Hall517274a2016-02-10 00:07:18 -08001124 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001125 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001126 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001127 }
1128 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001129 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001130 if (err != 0) {
Jesse Hall517274a2016-02-10 00:07:18 -08001131 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001132 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001133 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001134 }
1135
Jesse Hall3dd678a2016-01-08 21:52:01 -08001136 err = native_window_set_buffers_dimensions(
1137 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1138 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001139 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001140 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1141 create_info->imageExtent.width, create_info->imageExtent.height,
1142 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001143 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001144 }
1145
Jesse Hall178b6962016-02-24 15:39:50 -08001146 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1147 // applied during rendering. native_window_set_transform() expects the
1148 // inverse: the transform the app is requesting that the compositor perform
1149 // during composition. With native windows, pre-transform works by rendering
1150 // with the same transform the compositor is applying (as in Vulkan), but
1151 // then requesting the inverse transform, so that when the compositor does
1152 // it's job the two transforms cancel each other out and the compositor ends
1153 // up applying an identity transform to the app's buffer.
1154 err = native_window_set_buffers_transform(
1155 surface.window.get(),
1156 InvertTransformToNative(create_info->preTransform));
1157 if (err != 0) {
Jesse Hall178b6962016-02-24 15:39:50 -08001158 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1159 InvertTransformToNative(create_info->preTransform),
1160 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001161 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001162 }
1163
Jesse Hallf64ca122015-11-03 16:11:10 -08001164 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001165 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001166 if (err != 0) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001167 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1168 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001169 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001170 }
1171
Chris Forbes97ef4612017-03-30 19:37:50 +13001172 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1173 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1174 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1175 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1176 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1177 if (err != 0) {
1178 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1179 return VK_ERROR_SURFACE_LOST_KHR;
1180 }
1181 }
1182
1183 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1184 err = native_window_set_auto_refresh(surface.window.get(), true);
1185 if (err != 0) {
1186 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1187 return VK_ERROR_SURFACE_LOST_KHR;
1188 }
1189 }
1190
Jesse Halle6080bf2016-02-28 20:58:50 -08001191 int query_value;
1192 err = surface.window->query(surface.window.get(),
1193 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1194 &query_value);
1195 if (err != 0 || query_value < 0) {
Jesse Halle6080bf2016-02-28 20:58:50 -08001196 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1197 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001198 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001199 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001200 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001201 uint32_t num_images =
Ian Elliotta3515f42019-05-15 14:11:33 -06001202 (swap_interval ? create_info->minImageCount
1203 : std::max(3u, create_info->minImageCount)) -
1204 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001205
1206 // Lower layer insists that we have at least two buffers. This is wasteful
1207 // and we'd like to relax it in the shared case, but not all the pieces are
1208 // in place for that to work yet. Note we only lie to the lower layer-- we
1209 // don't want to give the app back a swapchain with extra images (which they
1210 // can't actually use!).
1211 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001212 if (err != 0) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001213 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1214 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001215 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001216 }
1217
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001218 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001219 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001220 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001221 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001222 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1223 device, create_info->imageFormat, create_info->imageUsage,
1224 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001225 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001226 if (result != VK_SUCCESS) {
1227 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001228 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001229 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001230 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001231 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001232 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001233 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001234 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001235 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001236 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001237 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001238 if (result != VK_SUCCESS) {
1239 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001240 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001241 }
Jesse Hall70f93352015-11-04 09:41:31 -08001242 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001243 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1244
1245 bool createProtectedSwapchain = false;
1246 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1247 createProtectedSwapchain = true;
1248 native_usage |= BufferUsage::PROTECTED;
1249 }
1250 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001251 if (err != 0) {
Jesse Hall70f93352015-11-04 09:41:31 -08001252 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001253 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001254 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001255
1256 // -- Allocate our Swapchain object --
1257 // After this point, we must deallocate the swapchain on error.
1258
Jesse Hall1f91d392015-12-11 16:28:44 -08001259 void* mem = allocator->pfnAllocation(allocator->pUserData,
1260 sizeof(Swapchain), alignof(Swapchain),
1261 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001262 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001263 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001264 Swapchain* swapchain = new (mem)
1265 Swapchain(surface, num_images, create_info->presentMode,
1266 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001267 // -- Dequeue all buffers and create a VkImage for each --
1268 // Any failures during or after this must cancel the dequeued buffers.
1269
Chris Forbesb56287a2017-01-12 14:28:58 +13001270 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1271#pragma clang diagnostic push
1272#pragma clang diagnostic ignored "-Wold-style-cast"
1273 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1274#pragma clang diagnostic pop
1275 .pNext = nullptr,
1276 .usage = swapchain_image_usage,
1277 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001278 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001279#pragma clang diagnostic push
1280#pragma clang diagnostic ignored "-Wold-style-cast"
1281 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1282#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001283 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001284 };
1285 VkImageCreateInfo image_create = {
1286 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1287 .pNext = &image_native_buffer,
1288 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001289 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001290 .extent = {0, 0, 1},
1291 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001292 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001293 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001294 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001295 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001296 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001297 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001298 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001299 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1300 };
1301
Jesse Halld7b994a2015-09-07 14:17:37 -07001302 for (uint32_t i = 0; i < num_images; i++) {
1303 Swapchain::Image& img = swapchain->images[i];
1304
1305 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001306 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1307 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001308 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001309 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001310 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001311 break;
1312 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001313 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001314 img.dequeued = true;
1315
1316 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001317 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1318 static_cast<uint32_t>(img.buffer->height),
1319 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001320 image_native_buffer.handle = img.buffer->handle;
1321 image_native_buffer.stride = img.buffer->stride;
1322 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001323 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001324 android_convertGralloc0To1Usage(int(img.buffer->usage),
1325 &image_native_buffer.usage2.producer,
1326 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001327
Yiwei Zhang533cea92019-06-03 18:43:24 -07001328 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001329 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001330 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001331 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001332 if (result != VK_SUCCESS) {
1333 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1334 break;
1335 }
1336 }
1337
1338 // -- Cancel all buffers, returning them to the queue --
1339 // If an error occurred before, also destroy the VkImage and release the
1340 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001341 for (uint32_t i = 0; i < num_images; i++) {
1342 Swapchain::Image& img = swapchain->images[i];
1343 if (img.dequeued) {
1344 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001345 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1346 img.dequeue_fence);
1347 img.dequeue_fence = -1;
1348 img.dequeued = false;
1349 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001350 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001351 }
1352
1353 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001354 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1355 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001356 return result;
1357 }
1358
Jesse Halldc225072016-05-30 22:40:14 -07001359 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1360 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001361 return VK_SUCCESS;
1362}
1363
Jesse Halle1b12782015-11-30 11:27:32 -08001364VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001365void DestroySwapchainKHR(VkDevice device,
1366 VkSwapchainKHR swapchain_handle,
1367 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001368 ATRACE_CALL();
1369
Yiwei Zhang533cea92019-06-03 18:43:24 -07001370 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001371}
1372
Jesse Halle1b12782015-11-30 11:27:32 -08001373VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001374VkResult GetSwapchainImagesKHR(VkDevice,
1375 VkSwapchainKHR swapchain_handle,
1376 uint32_t* count,
1377 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001378 ATRACE_CALL();
1379
Jesse Halld7b994a2015-09-07 14:17:37 -07001380 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001381 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1382 "getting images for non-active swapchain 0x%" PRIx64
1383 "; only dequeued image handles are valid",
1384 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001385 VkResult result = VK_SUCCESS;
1386 if (images) {
1387 uint32_t n = swapchain.num_images;
1388 if (*count < swapchain.num_images) {
1389 n = *count;
1390 result = VK_INCOMPLETE;
1391 }
1392 for (uint32_t i = 0; i < n; i++)
1393 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001394 *count = n;
1395 } else {
1396 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001397 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001398 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001399}
1400
Jesse Halle1b12782015-11-30 11:27:32 -08001401VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001402VkResult AcquireNextImageKHR(VkDevice device,
1403 VkSwapchainKHR swapchain_handle,
1404 uint64_t timeout,
1405 VkSemaphore semaphore,
1406 VkFence vk_fence,
1407 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001408 ATRACE_CALL();
1409
Jesse Halld7b994a2015-09-07 14:17:37 -07001410 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001411 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001412 VkResult result;
1413 int err;
1414
Jesse Halldc225072016-05-30 22:40:14 -07001415 if (swapchain.surface.swapchain_handle != swapchain_handle)
1416 return VK_ERROR_OUT_OF_DATE_KHR;
1417
Jesse Halld7b994a2015-09-07 14:17:37 -07001418 ALOGW_IF(
1419 timeout != UINT64_MAX,
1420 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1421
Chris Forbesc88409c2017-03-30 19:47:37 +13001422 if (swapchain.shared) {
1423 // In shared mode, we keep the buffer dequeued all the time, so we don't
1424 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1425 // the semaphore and fence passed to us will be signalled.
1426 *image_index = 0;
1427 result = GetData(device).driver.AcquireImageANDROID(
1428 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1429 return result;
1430 }
1431
Jesse Halld7b994a2015-09-07 14:17:37 -07001432 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001433 int fence_fd;
1434 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001435 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001436 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001437 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001438 }
1439
1440 uint32_t idx;
1441 for (idx = 0; idx < swapchain.num_images; idx++) {
1442 if (swapchain.images[idx].buffer.get() == buffer) {
1443 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001444 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001445 break;
1446 }
1447 }
1448 if (idx == swapchain.num_images) {
1449 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001450 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001451 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001452 }
1453
1454 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001455 if (fence_fd != -1) {
1456 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001457 if (fence_clone == -1) {
1458 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1459 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001460 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001461 }
1462 }
1463
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001464 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001465 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001466 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001467 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1468 // even if the call fails. We could close it ourselves on failure, but
1469 // that would create a race condition if the driver closes it on a
1470 // failure path: some other thread might create an fd with the same
1471 // number between the time the driver closes it and the time we close
1472 // it. We must assume one of: the driver *always* closes it even on
1473 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001474 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001475 swapchain.images[idx].dequeued = false;
1476 swapchain.images[idx].dequeue_fence = -1;
1477 return result;
1478 }
1479
1480 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001481 return VK_SUCCESS;
1482}
1483
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001484VKAPI_ATTR
1485VkResult AcquireNextImage2KHR(VkDevice device,
1486 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1487 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001488 ATRACE_CALL();
1489
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001490 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1491 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1492 pAcquireInfo->fence, pImageIndex);
1493}
1494
Jesse Halldc225072016-05-30 22:40:14 -07001495static VkResult WorstPresentResult(VkResult a, VkResult b) {
1496 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1497 // (in spec version 1.0.14).
1498 static const VkResult kWorstToBest[] = {
1499 VK_ERROR_DEVICE_LOST,
1500 VK_ERROR_SURFACE_LOST_KHR,
1501 VK_ERROR_OUT_OF_DATE_KHR,
1502 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1503 VK_ERROR_OUT_OF_HOST_MEMORY,
1504 VK_SUBOPTIMAL_KHR,
1505 };
1506 for (auto result : kWorstToBest) {
1507 if (a == result || b == result)
1508 return result;
1509 }
1510 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1511 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1512 return a != VK_SUCCESS ? a : b;
1513}
1514
Jesse Halle1b12782015-11-30 11:27:32 -08001515VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001516VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001517 ATRACE_CALL();
1518
Jesse Halld7b994a2015-09-07 14:17:37 -07001519 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1520 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1521 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001522
Jesse Halldc225072016-05-30 22:40:14 -07001523 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001524 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001525 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001526
Ian Elliottcb351132016-12-13 10:30:40 -07001527 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001528 const VkPresentRegionsKHR* present_regions = nullptr;
1529 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001530 const VkPresentRegionsKHR* next =
1531 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1532 while (next) {
1533 switch (next->sType) {
1534 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1535 present_regions = next;
1536 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001537 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001538 present_times =
1539 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1540 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001541 default:
1542 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1543 next->sType);
1544 break;
1545 }
1546 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1547 }
1548 ALOGV_IF(
1549 present_regions &&
1550 present_regions->swapchainCount != present_info->swapchainCount,
1551 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001552 ALOGV_IF(present_times &&
1553 present_times->swapchainCount != present_info->swapchainCount,
1554 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1555 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001556 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001557 (present_regions) ? present_regions->pRegions : nullptr;
1558 const VkPresentTimeGOOGLE* times =
1559 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001560 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001561 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001562 uint32_t nrects = 0;
1563
Jesse Halld7b994a2015-09-07 14:17:37 -07001564 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1565 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001566 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001567 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001568 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001569 const VkPresentRegionKHR* region =
1570 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001571 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001572 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001573 VkResult result;
1574 int err;
1575
Jesse Halld7b994a2015-09-07 14:17:37 -07001576 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001577 result = dispatch.QueueSignalReleaseImageANDROID(
1578 queue, present_info->waitSemaphoreCount,
1579 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001580 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001581 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001582 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001583 }
1584
Jesse Halldc225072016-05-30 22:40:14 -07001585 if (swapchain.surface.swapchain_handle ==
1586 present_info->pSwapchains[sc]) {
1587 ANativeWindow* window = swapchain.surface.window.get();
1588 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001589 if (region) {
1590 // Process the incremental-present hint for this swapchain:
1591 uint32_t rcount = region->rectangleCount;
1592 if (rcount > nrects) {
1593 android_native_rect_t* new_rects =
1594 static_cast<android_native_rect_t*>(
1595 allocator->pfnReallocation(
1596 allocator->pUserData, rects,
1597 sizeof(android_native_rect_t) * rcount,
1598 alignof(android_native_rect_t),
1599 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1600 if (new_rects) {
1601 rects = new_rects;
1602 nrects = rcount;
1603 } else {
1604 rcount = 0; // Ignore the hint for this swapchain
1605 }
1606 }
1607 for (uint32_t r = 0; r < rcount; ++r) {
1608 if (region->pRectangles[r].layer > 0) {
1609 ALOGV(
1610 "vkQueuePresentKHR ignoring invalid layer "
1611 "(%u); using layer 0 instead",
1612 region->pRectangles[r].layer);
1613 }
1614 int x = region->pRectangles[r].offset.x;
1615 int y = region->pRectangles[r].offset.y;
1616 int width = static_cast<int>(
1617 region->pRectangles[r].extent.width);
1618 int height = static_cast<int>(
1619 region->pRectangles[r].extent.height);
1620 android_native_rect_t* cur_rect = &rects[r];
1621 cur_rect->left = x;
1622 cur_rect->top = y + height;
1623 cur_rect->right = x + width;
1624 cur_rect->bottom = y;
1625 }
1626 native_window_set_surface_damage(window, rects, rcount);
1627 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001628 if (time) {
1629 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001630 ALOGV(
1631 "Calling "
1632 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001633 native_window_enable_frame_timestamps(window, true);
1634 swapchain.frame_timestamps_enabled = true;
1635 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001636
1637 // Record the nativeFrameId so it can be later correlated to
1638 // this present.
1639 uint64_t nativeFrameId = 0;
1640 err = native_window_get_next_frame_id(
1641 window, &nativeFrameId);
1642 if (err != android::NO_ERROR) {
1643 ALOGE("Failed to get next native frame ID.");
1644 }
1645
1646 // Add a new timing record with the user's presentID and
1647 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001648 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001649 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001650 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001651 }
1652 if (time->desiredPresentTime) {
1653 // Set the desiredPresentTime:
1654 ALOGV(
1655 "Calling "
1656 "native_window_set_buffers_timestamp(%" PRId64 ")",
1657 time->desiredPresentTime);
1658 native_window_set_buffers_timestamp(
1659 window,
1660 static_cast<int64_t>(time->desiredPresentTime));
1661 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001662 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001663
Jesse Halldc225072016-05-30 22:40:14 -07001664 err = window->queueBuffer(window, img.buffer.get(), fence);
1665 // queueBuffer always closes fence, even on error
1666 if (err != 0) {
Jesse Halldc225072016-05-30 22:40:14 -07001667 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1668 swapchain_result = WorstPresentResult(
1669 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001670 } else {
1671 if (img.dequeue_fence >= 0) {
1672 close(img.dequeue_fence);
1673 img.dequeue_fence = -1;
1674 }
1675 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001676 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001677
1678 // If the swapchain is in shared mode, immediately dequeue the
1679 // buffer so it can be presented again without an intervening
1680 // call to AcquireNextImageKHR. We expect to get the same buffer
1681 // back from every call to dequeueBuffer in this mode.
1682 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1683 ANativeWindowBuffer* buffer;
1684 int fence_fd;
1685 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1686 if (err != 0) {
1687 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1688 swapchain_result = WorstPresentResult(swapchain_result,
1689 VK_ERROR_SURFACE_LOST_KHR);
1690 }
1691 else if (img.buffer != buffer) {
1692 ALOGE("got wrong image back for shared swapchain");
1693 swapchain_result = WorstPresentResult(swapchain_result,
1694 VK_ERROR_SURFACE_LOST_KHR);
1695 }
1696 else {
1697 img.dequeue_fence = fence_fd;
1698 img.dequeued = true;
1699 }
1700 }
Jesse Halldc225072016-05-30 22:40:14 -07001701 }
1702 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001703 OrphanSwapchain(device, &swapchain);
1704 }
silence_dogood73597592019-05-23 16:57:37 -07001705 int window_transform_hint;
1706 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1707 &window_transform_hint);
1708 if (err != 0) {
1709 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1710 strerror(-err), err);
1711 swapchain_result = WorstPresentResult(
1712 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1713 }
1714 if (swapchain.pre_transform != window_transform_hint) {
1715 swapchain_result =
1716 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1717 }
Jesse Halldc225072016-05-30 22:40:14 -07001718 } else {
1719 ReleaseSwapchainImage(device, nullptr, fence, img);
1720 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001721 }
1722
Jesse Halla9e57032015-11-30 01:03:10 -08001723 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001724 present_info->pResults[sc] = swapchain_result;
1725
1726 if (swapchain_result != final_result)
1727 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001728 }
Ian Elliottcb351132016-12-13 10:30:40 -07001729 if (rects) {
1730 allocator->pfnFree(allocator->pUserData, rects);
1731 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001732
1733 return final_result;
1734}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001735
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001736VKAPI_ATTR
1737VkResult GetRefreshCycleDurationGOOGLE(
1738 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001739 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001740 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001741 ATRACE_CALL();
1742
Ian Elliott62c48c92017-01-20 13:13:20 -07001743 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001744 VkResult result = VK_SUCCESS;
1745
Ian Elliott3568bfe2019-05-03 15:54:46 -06001746 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001747
1748 return result;
1749}
1750
1751VKAPI_ATTR
1752VkResult GetPastPresentationTimingGOOGLE(
1753 VkDevice,
1754 VkSwapchainKHR swapchain_handle,
1755 uint32_t* count,
1756 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001757 ATRACE_CALL();
1758
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001759 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1760 ANativeWindow* window = swapchain.surface.window.get();
1761 VkResult result = VK_SUCCESS;
1762
1763 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001764 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001765 native_window_enable_frame_timestamps(window, true);
1766 swapchain.frame_timestamps_enabled = true;
1767 }
1768
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001769 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001770 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1771 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001772 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001773 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001774 }
1775
1776 return result;
1777}
1778
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001779VKAPI_ATTR
1780VkResult GetSwapchainStatusKHR(
1781 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001782 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001783 ATRACE_CALL();
1784
Chris Forbes4e18ba82017-01-20 12:50:17 +13001785 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001786 VkResult result = VK_SUCCESS;
1787
Chris Forbes4e18ba82017-01-20 12:50:17 +13001788 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1789 return VK_ERROR_OUT_OF_DATE_KHR;
1790 }
1791
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001792 // TODO(chrisforbes): Implement this function properly
1793
1794 return result;
1795}
1796
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001797VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001798 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001799 uint32_t swapchainCount,
1800 const VkSwapchainKHR* pSwapchains,
1801 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001802 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001803
1804 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1805 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1806 if (!swapchain)
1807 continue;
1808
1809 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1810
1811 ANativeWindow* window = swapchain->surface.window.get();
1812
1813 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1814 const android_smpte2086_metadata smpteMetdata = {
1815 {vulkanMetadata.displayPrimaryRed.x,
1816 vulkanMetadata.displayPrimaryRed.y},
1817 {vulkanMetadata.displayPrimaryGreen.x,
1818 vulkanMetadata.displayPrimaryGreen.y},
1819 {vulkanMetadata.displayPrimaryBlue.x,
1820 vulkanMetadata.displayPrimaryBlue.y},
1821 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1822 vulkanMetadata.maxLuminance,
1823 vulkanMetadata.minLuminance};
1824 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1825
1826 const android_cta861_3_metadata cta8613Metadata = {
1827 vulkanMetadata.maxContentLightLevel,
1828 vulkanMetadata.maxFrameAverageLightLevel};
1829 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1830 }
1831
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001832 return;
1833}
1834
Yiwei Zhang0f475222019-04-11 19:38:00 -07001835static void InterceptBindImageMemory2(
1836 uint32_t bind_info_count,
1837 const VkBindImageMemoryInfo* bind_infos,
1838 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1839 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1840 out_native_buffers->clear();
1841 out_bind_infos->clear();
1842
1843 if (!bind_info_count)
1844 return;
1845
1846 std::unordered_set<uint32_t> intercepted_indexes;
1847
1848 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1849 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1850 bind_infos[idx].pNext);
1851 while (info &&
1852 info->sType !=
1853 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1854 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1855 info->pNext);
1856 }
1857
1858 if (!info)
1859 continue;
1860
1861 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1862 "swapchain handle must not be NULL");
1863 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1864 ALOG_ASSERT(
1865 info->imageIndex < swapchain->num_images,
1866 "imageIndex must be less than the number of images in swapchain");
1867
1868 ANativeWindowBuffer* buffer =
1869 swapchain->images[info->imageIndex].buffer.get();
1870 VkNativeBufferANDROID native_buffer = {
1871#pragma clang diagnostic push
1872#pragma clang diagnostic ignored "-Wold-style-cast"
1873 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1874#pragma clang diagnostic pop
1875 .pNext = bind_infos[idx].pNext,
1876 .handle = buffer->handle,
1877 .stride = buffer->stride,
1878 .format = buffer->format,
1879 .usage = int(buffer->usage),
1880 };
1881 // Reserve enough space to avoid letting re-allocation invalidate the
1882 // addresses of the elements inside.
1883 out_native_buffers->reserve(bind_info_count);
1884 out_native_buffers->emplace_back(native_buffer);
1885
1886 // Reserve the space now since we know how much is needed now.
1887 out_bind_infos->reserve(bind_info_count);
1888 out_bind_infos->emplace_back(bind_infos[idx]);
1889 out_bind_infos->back().pNext = &out_native_buffers->back();
1890
1891 intercepted_indexes.insert(idx);
1892 }
1893
1894 if (intercepted_indexes.empty())
1895 return;
1896
1897 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1898 if (intercepted_indexes.count(idx))
1899 continue;
1900 out_bind_infos->emplace_back(bind_infos[idx]);
1901 }
1902}
1903
Yiwei Zhang23143102019-04-10 18:24:05 -07001904VKAPI_ATTR
1905VkResult BindImageMemory2(VkDevice device,
1906 uint32_t bindInfoCount,
1907 const VkBindImageMemoryInfo* pBindInfos) {
1908 ATRACE_CALL();
1909
Yiwei Zhang0f475222019-04-11 19:38:00 -07001910 // out_native_buffers is for maintaining the lifecycle of the constructed
1911 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1912 std::vector<VkNativeBufferANDROID> out_native_buffers;
1913 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1914 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1915 &out_bind_infos);
1916 return GetData(device).driver.BindImageMemory2(
1917 device, bindInfoCount,
1918 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001919}
1920
1921VKAPI_ATTR
1922VkResult BindImageMemory2KHR(VkDevice device,
1923 uint32_t bindInfoCount,
1924 const VkBindImageMemoryInfo* pBindInfos) {
1925 ATRACE_CALL();
1926
Yiwei Zhang0f475222019-04-11 19:38:00 -07001927 std::vector<VkNativeBufferANDROID> out_native_buffers;
1928 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1929 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1930 &out_bind_infos);
1931 return GetData(device).driver.BindImageMemory2KHR(
1932 device, bindInfoCount,
1933 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001934}
1935
Chia-I Wu62262232016-03-26 07:06:44 +08001936} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001937} // namespace vulkan