blob: fbf6d0d2333a6a01362a174b758dd1835d47f047 [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>
Brian Anderson1049d1d2016-12-16 17:25:57 -080027#include <utils/Vector.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070028
29#include <algorithm>
30#include <unordered_set>
31#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070032
Chia-I Wu4a6a9162016-03-26 07:17:34 +080033#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070034
Daniel Kochf25f5bb2017-10-05 00:26:58 -040035using android::hardware::graphics::common::V1_0::BufferUsage;
36
Jesse Hall5ae3abb2015-10-08 14:00:22 -070037// TODO(jessehall): Currently we don't have a good error code for when a native
38// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
39// versions (post SDK 0.9) of the API/extension have a better error code.
40// When updating to that version, audit all error returns.
Chia-I Wu62262232016-03-26 07:06:44 +080041namespace vulkan {
42namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070043
Jesse Halld7b994a2015-09-07 14:17:37 -070044namespace {
45
Jesse Hall55bc0972016-02-23 16:43:29 -080046const VkSurfaceTransformFlagsKHR kSupportedTransforms =
47 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
48 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
49 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
50 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
51 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
52 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
53 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
54 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
55 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
56 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
57
silence_dogood73597592019-05-23 16:57:37 -070058int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
59 switch (transform) {
60 // TODO: See TODO in TranslateNativeToVulkanTransform
61 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
62 return NATIVE_WINDOW_TRANSFORM_ROT_90;
63 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
64 return NATIVE_WINDOW_TRANSFORM_ROT_180;
65 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
66 return NATIVE_WINDOW_TRANSFORM_ROT_270;
67 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
68 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
69 default:
70 return 0;
71 }
72}
73
Jesse Hall55bc0972016-02-23 16:43:29 -080074VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
75 // Native and Vulkan transforms are isomorphic, but are represented
76 // differently. Vulkan transforms are built up of an optional horizontal
77 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
78 // transforms are built up from a horizontal flip, vertical flip, and
79 // 90-degree rotation, all optional but always in that order.
80
81 // TODO(jessehall): For now, only support pure rotations, not
82 // flip or flip-and-rotate, until I have more time to test them and build
83 // sample code. As far as I know we never actually use anything besides
84 // pure rotations anyway.
85
86 switch (native) {
87 case 0: // 0x0
88 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
89 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
90 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
91 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
92 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
93 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
94 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
95 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
96 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
97 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
98 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
99 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
100 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
101 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
102 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
103 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
104 default:
105 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
106 }
107}
108
Jesse Hall178b6962016-02-24 15:39:50 -0800109int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
110 switch (transform) {
111 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
112 return NATIVE_WINDOW_TRANSFORM_ROT_270;
113 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
114 return NATIVE_WINDOW_TRANSFORM_ROT_180;
115 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
116 return NATIVE_WINDOW_TRANSFORM_ROT_90;
117 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
118 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
119 // return NATIVE_WINDOW_TRANSFORM_FLIP_H;
120 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
121 // return NATIVE_WINDOW_TRANSFORM_FLIP_H |
122 // NATIVE_WINDOW_TRANSFORM_ROT_90;
123 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
124 // return NATIVE_WINDOW_TRANSFORM_FLIP_V;
125 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
126 // return NATIVE_WINDOW_TRANSFORM_FLIP_V |
127 // NATIVE_WINDOW_TRANSFORM_ROT_90;
128 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
129 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
130 default:
131 return 0;
132 }
133}
134
Ian Elliott8a977262017-01-19 09:05:58 -0700135class TimingInfo {
136 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800137 TimingInfo() = default;
138 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700139 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800140 native_frame_id_(nativeFrameId) {}
141 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700142 return (timestamp_desired_present_time_ !=
143 NATIVE_WINDOW_TIMESTAMP_PENDING &&
144 timestamp_actual_present_time_ !=
145 NATIVE_WINDOW_TIMESTAMP_PENDING &&
146 timestamp_render_complete_time_ !=
147 NATIVE_WINDOW_TIMESTAMP_PENDING &&
148 timestamp_composition_latch_time_ !=
149 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700150 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700151 void calculate(int64_t rdur) {
152 bool anyTimestampInvalid =
153 (timestamp_actual_present_time_ ==
154 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
155 (timestamp_render_complete_time_ ==
156 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
157 (timestamp_composition_latch_time_ ==
158 NATIVE_WINDOW_TIMESTAMP_INVALID);
159 if (anyTimestampInvalid) {
160 ALOGE("Unexpectedly received invalid timestamp.");
161 vals_.actualPresentTime = 0;
162 vals_.earliestPresentTime = 0;
163 vals_.presentMargin = 0;
164 return;
165 }
166
167 vals_.actualPresentTime =
168 static_cast<uint64_t>(timestamp_actual_present_time_);
169 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700170 timestamp_render_complete_time_);
171 // Calculate vals_.earliestPresentTime, and potentially adjust
172 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
173 // is vals_.actualPresentTime. If we can subtract rdur (the duration
174 // of a refresh cycle) from vals_.earliestPresentTime (and also from
175 // vals_.presentMargin) and still leave a positive margin, then we can
176 // report to the application that it could have presented earlier than
177 // it did (per the extension specification). If for some reason, we
178 // can do this subtraction repeatedly, we do, since
179 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700180 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700181 while ((margin > rdur) &&
182 ((early_time - rdur) > timestamp_composition_latch_time_)) {
183 early_time -= rdur;
184 margin -= rdur;
185 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700186 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
187 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700188 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800189 void get_values(VkPastPresentationTimingGOOGLE* values) const {
190 *values = vals_;
191 }
Ian Elliott8a977262017-01-19 09:05:58 -0700192
193 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800194 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700195
Brian Anderson1049d1d2016-12-16 17:25:57 -0800196 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700197 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
198 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
199 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
200 int64_t timestamp_composition_latch_time_
201 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700202};
203
Jesse Halld7b994a2015-09-07 14:17:37 -0700204// ----------------------------------------------------------------------------
205
Jesse Hall1356b0d2015-11-23 17:24:58 -0800206struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800207 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700208 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700209 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800210};
211
212VkSurfaceKHR HandleFromSurface(Surface* surface) {
213 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
214}
215
216Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800217 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800218}
219
Ian Elliott8a977262017-01-19 09:05:58 -0700220// Maximum number of TimingInfo structs to keep per swapchain:
221enum { MAX_TIMING_INFOS = 10 };
222// Minimum number of frames to look for in the past (so we don't cause
223// syncronous requests to Surface Flinger):
224enum { MIN_NUM_FRAMES_AGO = 5 };
225
Jesse Hall1356b0d2015-11-23 17:24:58 -0800226struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700227 Swapchain(Surface& surface_,
228 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700229 VkPresentModeKHR present_mode,
230 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700231 : surface(surface_),
232 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700233 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700234 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300235 frame_timestamps_enabled(false),
236 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
237 present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700238 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700239 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700240 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700241 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700242 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600243 uint64_t get_refresh_duration()
244 {
245 ANativeWindow* window = surface.window.get();
246 native_window_get_refresh_cycle_duration(
247 window,
248 &refresh_duration);
249 return static_cast<uint64_t>(refresh_duration);
250
251 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800252
253 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700254 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700255 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700256 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700257 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700258 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300259 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700260
261 struct Image {
262 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
263 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800264 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700265 // The fence is only valid when the buffer is dequeued, and should be
266 // -1 any other time. When valid, we own the fd, and must ensure it is
267 // closed: either by closing it explicitly when queueing the buffer,
268 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
269 int dequeue_fence;
270 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800271 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700272
Brian Anderson1049d1d2016-12-16 17:25:57 -0800273 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700274};
275
276VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
277 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
278}
279
280Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800281 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700282}
283
Jesse Halldc225072016-05-30 22:40:14 -0700284void ReleaseSwapchainImage(VkDevice device,
285 ANativeWindow* window,
286 int release_fence,
287 Swapchain::Image& image) {
288 ALOG_ASSERT(release_fence == -1 || image.dequeued,
289 "ReleaseSwapchainImage: can't provide a release fence for "
290 "non-dequeued images");
291
292 if (image.dequeued) {
293 if (release_fence >= 0) {
294 // We get here from vkQueuePresentKHR. The application is
295 // responsible for creating an execution dependency chain from
296 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
297 // (release_fence), so we can drop the dequeue_fence here.
298 if (image.dequeue_fence >= 0)
299 close(image.dequeue_fence);
300 } else {
301 // We get here during swapchain destruction, or various serious
302 // error cases e.g. when we can't create the release_fence during
303 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
304 // have already signalled, since the swapchain images are supposed
305 // to be idle before the swapchain is destroyed. In error cases,
306 // there may be rendering in flight to the image, but since we
307 // weren't able to create a release_fence, waiting for the
308 // dequeue_fence is about the best we can do.
309 release_fence = image.dequeue_fence;
310 }
311 image.dequeue_fence = -1;
312
313 if (window) {
314 window->cancelBuffer(window, image.buffer.get(), release_fence);
315 } else {
316 if (release_fence >= 0) {
317 sync_wait(release_fence, -1 /* forever */);
318 close(release_fence);
319 }
320 }
321
322 image.dequeued = false;
323 }
324
325 if (image.image) {
326 GetData(device).driver.DestroyImage(device, image.image, nullptr);
327 image.image = VK_NULL_HANDLE;
328 }
329
330 image.buffer.clear();
331}
332
333void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
334 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
335 return;
Jesse Halldc225072016-05-30 22:40:14 -0700336 for (uint32_t i = 0; i < swapchain->num_images; i++) {
337 if (!swapchain->images[i].dequeued)
338 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
339 }
340 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700341 swapchain->timing.clear();
342}
343
344uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800345 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
346 return 0;
347 }
Ian Elliott8a977262017-01-19 09:05:58 -0700348
Brian Anderson1049d1d2016-12-16 17:25:57 -0800349 uint32_t num_ready = 0;
350 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
351 for (uint32_t i = 0; i < num_timings; i++) {
352 TimingInfo& ti = swapchain.timing.editItemAt(i);
353 if (ti.ready()) {
354 // This TimingInfo is ready to be reported to the user. Add it
355 // to the num_ready.
356 num_ready++;
357 continue;
358 }
359 // This TimingInfo is not yet ready to be reported to the user,
360 // and so we should look for any available timestamps that
361 // might make it ready.
362 int64_t desired_present_time = 0;
363 int64_t render_complete_time = 0;
364 int64_t composition_latch_time = 0;
365 int64_t actual_present_time = 0;
366 // Obtain timestamps:
367 int ret = native_window_get_frame_timestamps(
368 swapchain.surface.window.get(), ti.native_frame_id_,
369 &desired_present_time, &render_complete_time,
370 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700371 nullptr, //&first_composition_start_time,
372 nullptr, //&last_composition_start_time,
373 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800374 // TODO(ianelliott): Maybe ask if this one is
375 // supported, at startup time (since it may not be
376 // supported):
377 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700378 nullptr, //&dequeue_ready_time,
379 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800380
381 if (ret != android::NO_ERROR) {
382 continue;
383 }
384
385 // Record the timestamp(s) we received, and then see if this TimingInfo
386 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700387 ti.timestamp_desired_present_time_ = desired_present_time;
388 ti.timestamp_actual_present_time_ = actual_present_time;
389 ti.timestamp_render_complete_time_ = render_complete_time;
390 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800391
392 if (ti.ready()) {
393 // The TimingInfo has received enough timestamps, and should now
394 // use those timestamps to calculate the info that should be
395 // reported to the user:
396 ti.calculate(swapchain.refresh_duration);
397 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700398 }
399 }
400 return num_ready;
401}
402
403// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
404void copy_ready_timings(Swapchain& swapchain,
405 uint32_t* count,
406 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800407 if (swapchain.timing.empty()) {
408 *count = 0;
409 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700410 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800411
412 size_t last_ready = swapchain.timing.size() - 1;
413 while (!swapchain.timing[last_ready].ready()) {
414 if (last_ready == 0) {
415 *count = 0;
416 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700417 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800418 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700419 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800420
421 uint32_t num_copied = 0;
422 size_t num_to_remove = 0;
423 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
424 const TimingInfo& ti = swapchain.timing[i];
425 if (ti.ready()) {
426 ti.get_values(&timings[num_copied]);
427 num_copied++;
428 }
429 num_to_remove++;
430 }
431
432 // Discard old frames that aren't ready if newer frames are ready.
433 // We don't expect to get the timing info for those old frames.
434 swapchain.timing.removeItemsAt(0, num_to_remove);
435
Ian Elliott8a977262017-01-19 09:05:58 -0700436 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700437}
438
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700439android_pixel_format GetNativePixelFormat(VkFormat format) {
440 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
441 switch (format) {
442 case VK_FORMAT_R8G8B8A8_UNORM:
443 case VK_FORMAT_R8G8B8A8_SRGB:
444 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
445 break;
446 case VK_FORMAT_R5G6B5_UNORM_PACK16:
447 native_format = HAL_PIXEL_FORMAT_RGB_565;
448 break;
449 case VK_FORMAT_R16G16B16A16_SFLOAT:
450 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
451 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800452 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700453 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
454 break;
455 default:
456 ALOGV("unsupported swapchain format %d", format);
457 break;
458 }
459 return native_format;
460}
461
462android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
463 switch (colorspace) {
464 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
465 return HAL_DATASPACE_V0_SRGB;
466 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
467 return HAL_DATASPACE_DISPLAY_P3;
468 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
469 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600470 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
471 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700472 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
473 return HAL_DATASPACE_DCI_P3_LINEAR;
474 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
475 return HAL_DATASPACE_DCI_P3;
476 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
477 return HAL_DATASPACE_V0_SRGB_LINEAR;
478 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
479 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600480 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
481 return HAL_DATASPACE_BT2020_LINEAR;
482 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700483 return static_cast<android_dataspace>(
484 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
485 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600486 case VK_COLOR_SPACE_DOLBYVISION_EXT:
487 return static_cast<android_dataspace>(
488 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
489 HAL_DATASPACE_RANGE_FULL);
490 case VK_COLOR_SPACE_HDR10_HLG_EXT:
491 return static_cast<android_dataspace>(
492 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
493 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700494 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
495 return static_cast<android_dataspace>(
496 HAL_DATASPACE_STANDARD_ADOBE_RGB |
497 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
498 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
499 return HAL_DATASPACE_ADOBE_RGB;
500
501 // Pass through is intended to allow app to provide data that is passed
502 // to the display system without modification.
503 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
504 return HAL_DATASPACE_ARBITRARY;
505
506 default:
507 // This indicates that we don't know about the
508 // dataspace specified and we should indicate that
509 // it's unsupported
510 return HAL_DATASPACE_UNKNOWN;
511 }
512}
513
Jesse Halld7b994a2015-09-07 14:17:37 -0700514} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700515
Jesse Halle1b12782015-11-30 11:27:32 -0800516VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800517VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800518 VkInstance instance,
519 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
520 const VkAllocationCallbacks* allocator,
521 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800522 ATRACE_CALL();
523
Jesse Hall1f91d392015-12-11 16:28:44 -0800524 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800525 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800526 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
527 alignof(Surface),
528 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800529 if (!mem)
530 return VK_ERROR_OUT_OF_HOST_MEMORY;
531 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700532
Chia-I Wue8e689f2016-04-18 08:21:31 +0800533 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700534 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700535 int err = native_window_get_consumer_usage(surface->window.get(),
536 &surface->consumer_usage);
537 if (err != android::NO_ERROR) {
538 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
539 strerror(-err), err);
540 surface->~Surface();
541 allocator->pfnFree(allocator->pUserData, surface);
542 return VK_ERROR_INITIALIZATION_FAILED;
543 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700544
Jesse Hall1356b0d2015-11-23 17:24:58 -0800545 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
Yiwei Zhang6435b322018-05-08 11:12:17 -0700546 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800547 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
548 if (err != 0) {
549 // TODO(jessehall): Improve error reporting. Can we enumerate possible
550 // errors and translate them to valid Vulkan result codes?
551 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
552 err);
553 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800554 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700555 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800556 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700557
Jesse Hall1356b0d2015-11-23 17:24:58 -0800558 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700559 return VK_SUCCESS;
560}
561
Jesse Halle1b12782015-11-30 11:27:32 -0800562VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800563void DestroySurfaceKHR(VkInstance instance,
564 VkSurfaceKHR surface_handle,
565 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800566 ATRACE_CALL();
567
Jesse Hall1356b0d2015-11-23 17:24:58 -0800568 Surface* surface = SurfaceFromHandle(surface_handle);
569 if (!surface)
570 return;
571 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700572 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700573 "destroyed VkSurfaceKHR 0x%" PRIx64
574 " has active VkSwapchainKHR 0x%" PRIx64,
575 reinterpret_cast<uint64_t>(surface_handle),
576 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800577 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800578 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800579 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800580 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800581}
582
Jesse Halle1b12782015-11-30 11:27:32 -0800583VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800584VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
585 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700586 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800587 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800588 ATRACE_CALL();
589
Yiwei Zhang6435b322018-05-08 11:12:17 -0700590 const Surface* surface = SurfaceFromHandle(surface_handle);
591 if (!surface) {
592 return VK_ERROR_SURFACE_LOST_KHR;
593 }
594 const ANativeWindow* window = surface->window.get();
595
596 int query_value;
597 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
598 if (err != 0 || query_value < 0) {
599 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
600 strerror(-err), err, query_value);
601 return VK_ERROR_SURFACE_LOST_KHR;
602 }
603
604 android_pixel_format native_format =
605 static_cast<android_pixel_format>(query_value);
606
607 bool format_supported = false;
608 switch (native_format) {
609 case HAL_PIXEL_FORMAT_RGBA_8888:
610 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700611 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800612 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700613 format_supported = true;
614 break;
615 default:
616 break;
617 }
618
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700619 *supported = static_cast<VkBool32>(
620 format_supported || (surface->consumer_usage &
621 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
622 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700623
Jesse Halla6429252015-11-29 18:59:42 -0800624 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800625}
626
Jesse Halle1b12782015-11-30 11:27:32 -0800627VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800628VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800629 VkPhysicalDevice /*pdev*/,
630 VkSurfaceKHR surface,
631 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800632 ATRACE_CALL();
633
Jesse Halld7b994a2015-09-07 14:17:37 -0700634 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800635 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700636
637 int width, height;
638 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
639 if (err != 0) {
640 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
641 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700642 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700643 }
644 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
645 if (err != 0) {
646 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
647 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700648 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700649 }
650
Jesse Hall55bc0972016-02-23 16:43:29 -0800651 int transform_hint;
652 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
653 if (err != 0) {
654 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
655 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700656 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800657 }
658
Jesse Halld7b994a2015-09-07 14:17:37 -0700659 // TODO(jessehall): Figure out what the min/max values should be.
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800660 int max_buffer_count;
661 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
662 if (err != 0) {
663 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
664 strerror(-err), err);
665 return VK_ERROR_SURFACE_LOST_KHR;
666 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700667 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800668 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700669
Jesse Hallfe2662d2016-02-09 13:26:59 -0800670 capabilities->currentExtent =
671 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
672
Jesse Halld7b994a2015-09-07 14:17:37 -0700673 // TODO(jessehall): Figure out what the max extent should be. Maximum
674 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800675 capabilities->minImageExtent = VkExtent2D{1, 1};
676 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700677
Jesse Hallfe2662d2016-02-09 13:26:59 -0800678 capabilities->maxImageArrayLayers = 1;
679
Jesse Hall55bc0972016-02-23 16:43:29 -0800680 capabilities->supportedTransforms = kSupportedTransforms;
681 capabilities->currentTransform =
682 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700683
Jesse Hallfe2662d2016-02-09 13:26:59 -0800684 // On Android, window composition is a WindowManager property, not something
685 // associated with the bufferqueue. It can't be changed from here.
686 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700687
688 // TODO(jessehall): I think these are right, but haven't thought hard about
689 // it. Do we need to query the driver for support of any of these?
690 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700691 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
692 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800693 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800694 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
695 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
696 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700697 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
698
Jesse Hallb1352bc2015-09-04 16:12:33 -0700699 return VK_SUCCESS;
700}
701
Jesse Halle1b12782015-11-30 11:27:32 -0800702VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700703VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
704 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800705 uint32_t* count,
706 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800707 ATRACE_CALL();
708
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700709 const InstanceData& instance_data = GetData(pdev);
710
Jesse Hall1356b0d2015-11-23 17:24:58 -0800711 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
712 // a new gralloc method to query whether a (format, usage) pair is
713 // supported, and check that for each gralloc format that corresponds to a
714 // Vulkan format. Shorter term, just add a few more formats to the ones
715 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700716
717 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700718 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
719 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
720 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700721 };
722 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700723 uint32_t total_num_formats = kNumFormats;
724
725 bool wide_color_support = false;
726 Surface& surface = *SurfaceFromHandle(surface_handle);
727 int err = native_window_get_wide_color_support(surface.window.get(),
728 &wide_color_support);
729 if (err) {
730 // Not allowed to return a more sensible error code, so do this
731 return VK_ERROR_OUT_OF_HOST_MEMORY;
732 }
733 ALOGV("wide_color_support is: %d", wide_color_support);
734 wide_color_support =
735 wide_color_support &&
736 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
737
738 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600739 {VK_FORMAT_R8G8B8A8_UNORM,
740 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
741 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700742 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700743 {VK_FORMAT_R16G16B16A16_SFLOAT,
744 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
745 {VK_FORMAT_R16G16B16A16_SFLOAT,
746 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800747 {VK_FORMAT_A2B10G10R10_UNORM_PACK32,
748 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700749 };
750 const uint32_t kNumWideColorFormats =
751 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
752 if (wide_color_support) {
753 total_num_formats += kNumWideColorFormats;
754 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700755
756 VkResult result = VK_SUCCESS;
757 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700758 uint32_t out_count = 0;
759 uint32_t transfer_count = 0;
760 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700761 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700762 transfer_count = std::min(*count, kNumFormats);
763 std::copy(kFormats, kFormats + transfer_count, formats);
764 out_count += transfer_count;
765 if (wide_color_support) {
766 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
767 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
768 formats + out_count);
769 out_count += transfer_count;
770 }
771 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700772 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700773 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700774 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700775 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700776}
777
Jesse Halle1b12782015-11-30 11:27:32 -0800778VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300779VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
780 VkPhysicalDevice physicalDevice,
781 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
782 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800783 ATRACE_CALL();
784
Chris Forbes2452cf72017-03-16 16:30:17 +1300785 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
786 physicalDevice, pSurfaceInfo->surface,
787 &pSurfaceCapabilities->surfaceCapabilities);
788
Chris Forbes06bc0092017-03-16 16:46:05 +1300789 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
790 while (caps->pNext) {
791 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
792
793 switch (caps->sType) {
794 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
795 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
796 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
797 caps);
798 // Claim same set of usage flags are supported for
799 // shared present modes as for other modes.
800 shared_caps->sharedPresentSupportedUsageFlags =
801 pSurfaceCapabilities->surfaceCapabilities
802 .supportedUsageFlags;
803 } break;
804
805 default:
806 // Ignore all other extension structs
807 break;
808 }
809 }
810
Chris Forbes2452cf72017-03-16 16:30:17 +1300811 return result;
812}
813
814VKAPI_ATTR
815VkResult GetPhysicalDeviceSurfaceFormats2KHR(
816 VkPhysicalDevice physicalDevice,
817 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
818 uint32_t* pSurfaceFormatCount,
819 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800820 ATRACE_CALL();
821
Chris Forbes2452cf72017-03-16 16:30:17 +1300822 if (!pSurfaceFormats) {
823 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
824 pSurfaceInfo->surface,
825 pSurfaceFormatCount, nullptr);
826 } else {
827 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
828 // after the call.
829 android::Vector<VkSurfaceFormatKHR> surface_formats;
830 surface_formats.resize(*pSurfaceFormatCount);
831 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
832 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
833 &surface_formats.editItemAt(0));
834
835 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
836 // marshal results individually due to stride difference.
837 // completely ignore any chained extension structs.
838 uint32_t formats_to_marshal = *pSurfaceFormatCount;
839 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
840 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
841 }
842 }
843
844 return result;
845 }
846}
847
848VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300849VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800850 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800851 uint32_t* count,
852 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800853 ATRACE_CALL();
854
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800855 int err;
856 int query_value;
857 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
858
859 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
860 if (err != 0 || query_value < 0) {
861 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
862 strerror(-err), err, query_value);
863 return VK_ERROR_SURFACE_LOST_KHR;
864 }
865 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
866
867 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
868 if (err != 0 || query_value < 0) {
869 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
870 strerror(-err), err, query_value);
871 return VK_ERROR_SURFACE_LOST_KHR;
872 }
873 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
874
Chris Forbese8d79a62017-02-22 12:49:18 +1300875 android::Vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800876 if (min_undequeued_buffers + 1 < max_buffer_count)
877 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300878 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
879
880 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
881 if (QueryPresentationProperties(pdev, &present_properties)) {
882 if (present_properties.sharedImage) {
883 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
884 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
885 }
886 }
887
888 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700889
890 VkResult result = VK_SUCCESS;
891 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300892 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700893 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300894 *count = std::min(*count, num_modes);
895 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700896 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300897 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700898 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700899 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700900}
901
Jesse Halle1b12782015-11-30 11:27:32 -0800902VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400903VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600904 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400905 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800906 ATRACE_CALL();
907
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400908 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
909 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
910 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
911 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
912 pDeviceGroupPresentCapabilities->sType);
913
914 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
915 sizeof(pDeviceGroupPresentCapabilities->presentMask));
916
917 // assume device group of size 1
918 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
919 pDeviceGroupPresentCapabilities->modes =
920 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
921
922 return VK_SUCCESS;
923}
924
925VKAPI_ATTR
926VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600927 VkDevice,
928 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400929 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800930 ATRACE_CALL();
931
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400932 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
933 return VK_SUCCESS;
934}
935
936VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600937VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400938 VkSurfaceKHR surface,
939 uint32_t* pRectCount,
940 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800941 ATRACE_CALL();
942
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400943 if (!pRects) {
944 *pRectCount = 1;
945 } else {
946 uint32_t count = std::min(*pRectCount, 1u);
947 bool incomplete = *pRectCount < 1;
948
949 *pRectCount = count;
950
951 if (incomplete) {
952 return VK_INCOMPLETE;
953 }
954
955 int err;
956 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
957
958 int width = 0, height = 0;
959 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
960 if (err != 0) {
961 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
962 strerror(-err), err);
963 }
964 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
965 if (err != 0) {
966 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
967 strerror(-err), err);
968 }
969
970 // TODO: Return something better than "whole window"
971 pRects[0].offset.x = 0;
972 pRects[0].offset.y = 0;
973 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
974 static_cast<uint32_t>(height)};
975 }
976 return VK_SUCCESS;
977}
978
979VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800980VkResult CreateSwapchainKHR(VkDevice device,
981 const VkSwapchainCreateInfoKHR* create_info,
982 const VkAllocationCallbacks* allocator,
983 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800984 ATRACE_CALL();
985
Jesse Halld7b994a2015-09-07 14:17:37 -0700986 int err;
987 VkResult result = VK_SUCCESS;
988
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700989 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
990 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
991 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
992 " oldSwapchain=0x%" PRIx64,
993 reinterpret_cast<uint64_t>(create_info->surface),
994 create_info->minImageCount, create_info->imageFormat,
995 create_info->imageColorSpace, create_info->imageExtent.width,
996 create_info->imageExtent.height, create_info->imageUsage,
997 create_info->preTransform, create_info->presentMode,
998 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
999
Jesse Hall1f91d392015-12-11 16:28:44 -08001000 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001001 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001002
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001003 android_pixel_format native_pixel_format =
1004 GetNativePixelFormat(create_info->imageFormat);
1005 android_dataspace native_dataspace =
1006 GetNativeDataspace(create_info->imageColorSpace);
1007 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1008 ALOGE(
1009 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1010 "failed: Unsupported color space",
1011 create_info->imageColorSpace);
1012 return VK_ERROR_INITIALIZATION_FAILED;
1013 }
1014
Jesse Hall42a9eec2016-06-03 12:39:49 -07001015 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001016 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001017 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001018 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001019 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001020 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001021 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001022 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001023 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1024 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001025 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001026 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001027
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001028 Surface& surface = *SurfaceFromHandle(create_info->surface);
1029
Jesse Halldc225072016-05-30 22:40:14 -07001030 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001031 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001032 " because it already has active swapchain 0x%" PRIx64
1033 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1034 reinterpret_cast<uint64_t>(create_info->surface),
1035 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1036 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1037 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1038 }
1039 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1040 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1041
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001042 // -- Reset the native window --
1043 // The native window might have been used previously, and had its properties
1044 // changed from defaults. That will affect the answer we get for queries
1045 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1046 // attempt such queries.
1047
Jesse Halldc225072016-05-30 22:40:14 -07001048 // The native window only allows dequeueing all buffers before any have
1049 // been queued, since after that point at least one is assumed to be in
1050 // non-FREE state at any given time. Disconnecting and re-connecting
1051 // orphans the previous buffers, getting us back to the state where we can
1052 // dequeue all buffers.
1053 err = native_window_api_disconnect(surface.window.get(),
1054 NATIVE_WINDOW_API_EGL);
1055 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1056 strerror(-err), err);
1057 err =
1058 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1059 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1060 strerror(-err), err);
1061
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001062 err = native_window_set_buffer_count(surface.window.get(), 0);
1063 if (err != 0) {
1064 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1065 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001066 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001067 }
1068
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301069 int swap_interval =
1070 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1071 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001072 if (err != 0) {
1073 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1074 // errors and translate them to valid Vulkan result codes?
1075 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1076 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001077 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001078 }
1079
Chris Forbesb8042d22017-01-18 18:07:05 +13001080 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1081 if (err != 0) {
1082 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1083 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001084 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001085 }
1086
1087 err = native_window_set_auto_refresh(surface.window.get(), false);
1088 if (err != 0) {
1089 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1090 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001091 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001092 }
1093
Jesse Halld7b994a2015-09-07 14:17:37 -07001094 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001095
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001096 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001097
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001098 err = native_window_set_buffers_format(surface.window.get(),
1099 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001100 if (err != 0) {
1101 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1102 // errors and translate them to valid Vulkan result codes?
1103 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001104 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001105 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001106 }
1107 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001108 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001109 if (err != 0) {
1110 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1111 // errors and translate them to valid Vulkan result codes?
1112 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001113 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001114 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001115 }
1116
Jesse Hall3dd678a2016-01-08 21:52:01 -08001117 err = native_window_set_buffers_dimensions(
1118 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1119 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001120 if (err != 0) {
1121 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1122 // errors and translate them to valid Vulkan result codes?
1123 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1124 create_info->imageExtent.width, create_info->imageExtent.height,
1125 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001126 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001127 }
1128
Jesse Hall178b6962016-02-24 15:39:50 -08001129 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1130 // applied during rendering. native_window_set_transform() expects the
1131 // inverse: the transform the app is requesting that the compositor perform
1132 // during composition. With native windows, pre-transform works by rendering
1133 // with the same transform the compositor is applying (as in Vulkan), but
1134 // then requesting the inverse transform, so that when the compositor does
1135 // it's job the two transforms cancel each other out and the compositor ends
1136 // up applying an identity transform to the app's buffer.
1137 err = native_window_set_buffers_transform(
1138 surface.window.get(),
1139 InvertTransformToNative(create_info->preTransform));
1140 if (err != 0) {
1141 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1142 // errors and translate them to valid Vulkan result codes?
1143 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1144 InvertTransformToNative(create_info->preTransform),
1145 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001146 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001147 }
1148
Jesse Hallf64ca122015-11-03 16:11:10 -08001149 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001150 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001151 if (err != 0) {
1152 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1153 // errors and translate them to valid Vulkan result codes?
1154 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1155 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001156 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001157 }
1158
Chris Forbes97ef4612017-03-30 19:37:50 +13001159 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1160 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1161 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1162 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1163 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1164 if (err != 0) {
1165 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1166 return VK_ERROR_SURFACE_LOST_KHR;
1167 }
1168 }
1169
1170 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1171 err = native_window_set_auto_refresh(surface.window.get(), true);
1172 if (err != 0) {
1173 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1174 return VK_ERROR_SURFACE_LOST_KHR;
1175 }
1176 }
1177
Jesse Halle6080bf2016-02-28 20:58:50 -08001178 int query_value;
1179 err = surface.window->query(surface.window.get(),
1180 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1181 &query_value);
1182 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001183 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1184 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -08001185 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1186 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001187 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001188 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001189 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001190 uint32_t num_images =
Ian Elliotta3515f42019-05-15 14:11:33 -06001191 (swap_interval ? create_info->minImageCount
1192 : std::max(3u, create_info->minImageCount)) -
1193 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001194
1195 // Lower layer insists that we have at least two buffers. This is wasteful
1196 // and we'd like to relax it in the shared case, but not all the pieces are
1197 // in place for that to work yet. Note we only lie to the lower layer-- we
1198 // don't want to give the app back a swapchain with extra images (which they
1199 // can't actually use!).
1200 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001201 if (err != 0) {
1202 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1203 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001204 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1205 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001206 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001207 }
1208
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001209 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001210 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001211 uint64_t consumer_usage, producer_usage;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001212 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001213 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1214 device, create_info->imageFormat, create_info->imageUsage,
1215 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001216 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001217 if (result != VK_SUCCESS) {
1218 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001219 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001220 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001221 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001222 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001223 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001224 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001225 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001226 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001227 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001228 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001229 if (result != VK_SUCCESS) {
1230 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001231 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001232 }
Jesse Hall70f93352015-11-04 09:41:31 -08001233 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001234 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1235
1236 bool createProtectedSwapchain = false;
1237 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1238 createProtectedSwapchain = true;
1239 native_usage |= BufferUsage::PROTECTED;
1240 }
1241 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001242 if (err != 0) {
1243 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1244 // errors and translate them to valid Vulkan result codes?
1245 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001246 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001247 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001248
1249 // -- Allocate our Swapchain object --
1250 // After this point, we must deallocate the swapchain on error.
1251
Jesse Hall1f91d392015-12-11 16:28:44 -08001252 void* mem = allocator->pfnAllocation(allocator->pUserData,
1253 sizeof(Swapchain), alignof(Swapchain),
1254 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001255 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001256 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001257 Swapchain* swapchain = new (mem)
1258 Swapchain(surface, num_images, create_info->presentMode,
1259 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001260 // -- Dequeue all buffers and create a VkImage for each --
1261 // Any failures during or after this must cancel the dequeued buffers.
1262
Chris Forbesb56287a2017-01-12 14:28:58 +13001263 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1264#pragma clang diagnostic push
1265#pragma clang diagnostic ignored "-Wold-style-cast"
1266 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1267#pragma clang diagnostic pop
1268 .pNext = nullptr,
1269 .usage = swapchain_image_usage,
1270 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001271 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001272#pragma clang diagnostic push
1273#pragma clang diagnostic ignored "-Wold-style-cast"
1274 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1275#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001276 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001277 };
1278 VkImageCreateInfo image_create = {
1279 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1280 .pNext = &image_native_buffer,
Nick Desaulniersd04f4ed2019-10-15 19:12:05 -07001281 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001282 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001283 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001284 .extent = {0, 0, 1},
1285 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001286 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001287 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001288 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001289 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001290 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001291 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001292 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1293 };
1294
Jesse Halld7b994a2015-09-07 14:17:37 -07001295 for (uint32_t i = 0; i < num_images; i++) {
1296 Swapchain::Image& img = swapchain->images[i];
1297
1298 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001299 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1300 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001301 if (err != 0) {
1302 // TODO(jessehall): Improve error reporting. Can we enumerate
1303 // possible errors and translate them to valid Vulkan result codes?
1304 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001305 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001306 break;
1307 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001308 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001309 img.dequeued = true;
1310
1311 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001312 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1313 static_cast<uint32_t>(img.buffer->height),
1314 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001315 image_native_buffer.handle = img.buffer->handle;
1316 image_native_buffer.stride = img.buffer->stride;
1317 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001318 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001319 android_convertGralloc0To1Usage(int(img.buffer->usage),
1320 &image_native_buffer.usage2.producer,
1321 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001322
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001323 ATRACE_BEGIN("dispatch.CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001324 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001325 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001326 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001327 if (result != VK_SUCCESS) {
1328 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1329 break;
1330 }
1331 }
1332
1333 // -- Cancel all buffers, returning them to the queue --
1334 // If an error occurred before, also destroy the VkImage and release the
1335 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1336 //
1337 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1338 // but not the non-error path. Should refactor/unify.
Chris Forbes31b85c22018-05-29 15:03:28 -07001339 for (uint32_t i = 0; i < num_images; i++) {
1340 Swapchain::Image& img = swapchain->images[i];
1341 if (img.dequeued) {
1342 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001343 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1344 img.dequeue_fence);
1345 img.dequeue_fence = -1;
1346 img.dequeued = false;
1347 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001348 }
1349 if (result != VK_SUCCESS) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001350 if (img.image) {
1351 ATRACE_BEGIN("dispatch.DestroyImage");
Chris Forbes31b85c22018-05-29 15:03:28 -07001352 dispatch.DestroyImage(device, img.image, nullptr);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001353 ATRACE_END();
1354 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001355 }
1356 }
1357
1358 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001359 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001360 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001361 return result;
1362 }
1363
Jesse Halldc225072016-05-30 22:40:14 -07001364 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1365 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001366 return VK_SUCCESS;
1367}
1368
Jesse Halle1b12782015-11-30 11:27:32 -08001369VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001370void DestroySwapchainKHR(VkDevice device,
1371 VkSwapchainKHR swapchain_handle,
1372 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001373 ATRACE_CALL();
1374
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001375 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001376 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001377 if (!swapchain)
1378 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001379 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1380 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001381
Yiwei Zhangc81b5322019-08-21 13:09:34 -07001382 if (window && swapchain->frame_timestamps_enabled) {
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001383 native_window_enable_frame_timestamps(window, false);
1384 }
Jesse Halldc225072016-05-30 22:40:14 -07001385 for (uint32_t i = 0; i < swapchain->num_images; i++)
1386 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001387 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001388 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001389 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001390 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001391 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001392 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001393}
1394
Jesse Halle1b12782015-11-30 11:27:32 -08001395VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001396VkResult GetSwapchainImagesKHR(VkDevice,
1397 VkSwapchainKHR swapchain_handle,
1398 uint32_t* count,
1399 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001400 ATRACE_CALL();
1401
Jesse Halld7b994a2015-09-07 14:17:37 -07001402 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001403 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1404 "getting images for non-active swapchain 0x%" PRIx64
1405 "; only dequeued image handles are valid",
1406 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001407 VkResult result = VK_SUCCESS;
1408 if (images) {
1409 uint32_t n = swapchain.num_images;
1410 if (*count < swapchain.num_images) {
1411 n = *count;
1412 result = VK_INCOMPLETE;
1413 }
1414 for (uint32_t i = 0; i < n; i++)
1415 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001416 *count = n;
1417 } else {
1418 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001419 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001420 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001421}
1422
Jesse Halle1b12782015-11-30 11:27:32 -08001423VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001424VkResult AcquireNextImageKHR(VkDevice device,
1425 VkSwapchainKHR swapchain_handle,
1426 uint64_t timeout,
1427 VkSemaphore semaphore,
1428 VkFence vk_fence,
1429 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001430 ATRACE_CALL();
1431
Jesse Halld7b994a2015-09-07 14:17:37 -07001432 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001433 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001434 VkResult result;
1435 int err;
1436
Jesse Halldc225072016-05-30 22:40:14 -07001437 if (swapchain.surface.swapchain_handle != swapchain_handle)
1438 return VK_ERROR_OUT_OF_DATE_KHR;
1439
Jesse Halld7b994a2015-09-07 14:17:37 -07001440 ALOGW_IF(
1441 timeout != UINT64_MAX,
1442 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1443
Chris Forbesc88409c2017-03-30 19:47:37 +13001444 if (swapchain.shared) {
1445 // In shared mode, we keep the buffer dequeued all the time, so we don't
1446 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1447 // the semaphore and fence passed to us will be signalled.
1448 *image_index = 0;
1449 result = GetData(device).driver.AcquireImageANDROID(
1450 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1451 return result;
1452 }
1453
Jesse Halld7b994a2015-09-07 14:17:37 -07001454 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001455 int fence_fd;
1456 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001457 if (err != 0) {
1458 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1459 // errors and translate them to valid Vulkan result codes?
1460 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001461 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001462 }
1463
1464 uint32_t idx;
1465 for (idx = 0; idx < swapchain.num_images; idx++) {
1466 if (swapchain.images[idx].buffer.get() == buffer) {
1467 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001468 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001469 break;
1470 }
1471 }
1472 if (idx == swapchain.num_images) {
1473 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001474 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001475 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001476 }
1477
1478 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001479 if (fence_fd != -1) {
1480 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001481 if (fence_clone == -1) {
1482 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1483 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001484 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001485 }
1486 }
1487
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001488 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001489 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001490 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001491 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1492 // even if the call fails. We could close it ourselves on failure, but
1493 // that would create a race condition if the driver closes it on a
1494 // failure path: some other thread might create an fd with the same
1495 // number between the time the driver closes it and the time we close
1496 // it. We must assume one of: the driver *always* closes it even on
1497 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001498 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001499 swapchain.images[idx].dequeued = false;
1500 swapchain.images[idx].dequeue_fence = -1;
1501 return result;
1502 }
1503
1504 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001505 return VK_SUCCESS;
1506}
1507
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001508VKAPI_ATTR
1509VkResult AcquireNextImage2KHR(VkDevice device,
1510 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1511 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001512 ATRACE_CALL();
1513
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001514 // TODO: this should actually be the other way around and this function
1515 // should handle any additional structures that get passed in
1516 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1517 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1518 pAcquireInfo->fence, pImageIndex);
1519}
1520
Jesse Halldc225072016-05-30 22:40:14 -07001521static VkResult WorstPresentResult(VkResult a, VkResult b) {
1522 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1523 // (in spec version 1.0.14).
1524 static const VkResult kWorstToBest[] = {
1525 VK_ERROR_DEVICE_LOST,
1526 VK_ERROR_SURFACE_LOST_KHR,
1527 VK_ERROR_OUT_OF_DATE_KHR,
1528 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1529 VK_ERROR_OUT_OF_HOST_MEMORY,
1530 VK_SUBOPTIMAL_KHR,
1531 };
1532 for (auto result : kWorstToBest) {
1533 if (a == result || b == result)
1534 return result;
1535 }
1536 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1537 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1538 return a != VK_SUCCESS ? a : b;
1539}
1540
Jesse Halle1b12782015-11-30 11:27:32 -08001541VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001542VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001543 ATRACE_CALL();
1544
Jesse Halld7b994a2015-09-07 14:17:37 -07001545 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1546 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1547 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001548
Jesse Halldc225072016-05-30 22:40:14 -07001549 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001550 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001551 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001552
Ian Elliottcb351132016-12-13 10:30:40 -07001553 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001554 const VkPresentRegionsKHR* present_regions = nullptr;
1555 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001556 const VkPresentRegionsKHR* next =
1557 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1558 while (next) {
1559 switch (next->sType) {
1560 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1561 present_regions = next;
1562 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001563 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001564 present_times =
1565 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1566 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001567 default:
1568 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1569 next->sType);
1570 break;
1571 }
1572 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1573 }
1574 ALOGV_IF(
1575 present_regions &&
1576 present_regions->swapchainCount != present_info->swapchainCount,
1577 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001578 ALOGV_IF(present_times &&
1579 present_times->swapchainCount != present_info->swapchainCount,
1580 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1581 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001582 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001583 (present_regions) ? present_regions->pRegions : nullptr;
1584 const VkPresentTimeGOOGLE* times =
1585 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001586 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001587 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001588 uint32_t nrects = 0;
1589
Jesse Halld7b994a2015-09-07 14:17:37 -07001590 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1591 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001592 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001593 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001594 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001595 const VkPresentRegionKHR* region =
1596 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001597 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001598 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001599 VkResult result;
1600 int err;
1601
Jesse Halld7b994a2015-09-07 14:17:37 -07001602 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001603 result = dispatch.QueueSignalReleaseImageANDROID(
1604 queue, present_info->waitSemaphoreCount,
1605 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001606 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001607 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001608 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001609 }
1610
Jesse Halldc225072016-05-30 22:40:14 -07001611 if (swapchain.surface.swapchain_handle ==
1612 present_info->pSwapchains[sc]) {
1613 ANativeWindow* window = swapchain.surface.window.get();
1614 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001615 if (region) {
1616 // Process the incremental-present hint for this swapchain:
1617 uint32_t rcount = region->rectangleCount;
1618 if (rcount > nrects) {
1619 android_native_rect_t* new_rects =
1620 static_cast<android_native_rect_t*>(
1621 allocator->pfnReallocation(
1622 allocator->pUserData, rects,
1623 sizeof(android_native_rect_t) * rcount,
1624 alignof(android_native_rect_t),
1625 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1626 if (new_rects) {
1627 rects = new_rects;
1628 nrects = rcount;
1629 } else {
1630 rcount = 0; // Ignore the hint for this swapchain
1631 }
1632 }
1633 for (uint32_t r = 0; r < rcount; ++r) {
1634 if (region->pRectangles[r].layer > 0) {
1635 ALOGV(
1636 "vkQueuePresentKHR ignoring invalid layer "
1637 "(%u); using layer 0 instead",
1638 region->pRectangles[r].layer);
1639 }
1640 int x = region->pRectangles[r].offset.x;
1641 int y = region->pRectangles[r].offset.y;
1642 int width = static_cast<int>(
1643 region->pRectangles[r].extent.width);
1644 int height = static_cast<int>(
1645 region->pRectangles[r].extent.height);
1646 android_native_rect_t* cur_rect = &rects[r];
1647 cur_rect->left = x;
1648 cur_rect->top = y + height;
1649 cur_rect->right = x + width;
1650 cur_rect->bottom = y;
1651 }
1652 native_window_set_surface_damage(window, rects, rcount);
1653 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001654 if (time) {
1655 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001656 ALOGV(
1657 "Calling "
1658 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001659 native_window_enable_frame_timestamps(window, true);
1660 swapchain.frame_timestamps_enabled = true;
1661 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001662
1663 // Record the nativeFrameId so it can be later correlated to
1664 // this present.
1665 uint64_t nativeFrameId = 0;
1666 err = native_window_get_next_frame_id(
1667 window, &nativeFrameId);
1668 if (err != android::NO_ERROR) {
1669 ALOGE("Failed to get next native frame ID.");
1670 }
1671
1672 // Add a new timing record with the user's presentID and
1673 // the nativeFrameId.
1674 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1675 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001676 swapchain.timing.removeAt(0);
1677 }
1678 if (time->desiredPresentTime) {
1679 // Set the desiredPresentTime:
1680 ALOGV(
1681 "Calling "
1682 "native_window_set_buffers_timestamp(%" PRId64 ")",
1683 time->desiredPresentTime);
1684 native_window_set_buffers_timestamp(
1685 window,
1686 static_cast<int64_t>(time->desiredPresentTime));
1687 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001688 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001689
Jesse Halldc225072016-05-30 22:40:14 -07001690 err = window->queueBuffer(window, img.buffer.get(), fence);
1691 // queueBuffer always closes fence, even on error
1692 if (err != 0) {
1693 // TODO(jessehall): What now? We should probably cancel the
1694 // buffer, I guess?
1695 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1696 swapchain_result = WorstPresentResult(
1697 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1698 }
1699 if (img.dequeue_fence >= 0) {
1700 close(img.dequeue_fence);
1701 img.dequeue_fence = -1;
1702 }
1703 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001704
1705 // If the swapchain is in shared mode, immediately dequeue the
1706 // buffer so it can be presented again without an intervening
1707 // call to AcquireNextImageKHR. We expect to get the same buffer
1708 // back from every call to dequeueBuffer in this mode.
1709 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1710 ANativeWindowBuffer* buffer;
1711 int fence_fd;
1712 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1713 if (err != 0) {
1714 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1715 swapchain_result = WorstPresentResult(swapchain_result,
1716 VK_ERROR_SURFACE_LOST_KHR);
1717 }
1718 else if (img.buffer != buffer) {
1719 ALOGE("got wrong image back for shared swapchain");
1720 swapchain_result = WorstPresentResult(swapchain_result,
1721 VK_ERROR_SURFACE_LOST_KHR);
1722 }
1723 else {
1724 img.dequeue_fence = fence_fd;
1725 img.dequeued = true;
1726 }
1727 }
Jesse Halldc225072016-05-30 22:40:14 -07001728 }
1729 if (swapchain_result != VK_SUCCESS) {
1730 ReleaseSwapchainImage(device, window, fence, img);
1731 OrphanSwapchain(device, &swapchain);
1732 }
silence_dogood73597592019-05-23 16:57:37 -07001733 int window_transform_hint;
1734 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1735 &window_transform_hint);
1736 if (err != 0) {
1737 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1738 strerror(-err), err);
1739 swapchain_result = WorstPresentResult(
1740 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1741 }
1742 if (swapchain.pre_transform != window_transform_hint) {
1743 swapchain_result =
1744 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1745 }
Jesse Halldc225072016-05-30 22:40:14 -07001746 } else {
1747 ReleaseSwapchainImage(device, nullptr, fence, img);
1748 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001749 }
1750
Jesse Halla9e57032015-11-30 01:03:10 -08001751 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001752 present_info->pResults[sc] = swapchain_result;
1753
1754 if (swapchain_result != final_result)
1755 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001756 }
Ian Elliottcb351132016-12-13 10:30:40 -07001757 if (rects) {
1758 allocator->pfnFree(allocator->pUserData, rects);
1759 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001760
1761 return final_result;
1762}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001763
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001764VKAPI_ATTR
1765VkResult GetRefreshCycleDurationGOOGLE(
1766 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001767 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001768 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001769 ATRACE_CALL();
1770
Ian Elliott62c48c92017-01-20 13:13:20 -07001771 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001772 VkResult result = VK_SUCCESS;
1773
Ian Elliott3568bfe2019-05-03 15:54:46 -06001774 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001775
1776 return result;
1777}
1778
1779VKAPI_ATTR
1780VkResult GetPastPresentationTimingGOOGLE(
1781 VkDevice,
1782 VkSwapchainKHR swapchain_handle,
1783 uint32_t* count,
1784 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001785 ATRACE_CALL();
1786
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001787 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1788 ANativeWindow* window = swapchain.surface.window.get();
1789 VkResult result = VK_SUCCESS;
1790
1791 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001792 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001793 native_window_enable_frame_timestamps(window, true);
1794 swapchain.frame_timestamps_enabled = true;
1795 }
1796
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001797 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001798 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1799 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001800 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001801 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001802 }
1803
1804 return result;
1805}
1806
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001807VKAPI_ATTR
1808VkResult GetSwapchainStatusKHR(
1809 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001810 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001811 ATRACE_CALL();
1812
Chris Forbes4e18ba82017-01-20 12:50:17 +13001813 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001814 VkResult result = VK_SUCCESS;
1815
Chris Forbes4e18ba82017-01-20 12:50:17 +13001816 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1817 return VK_ERROR_OUT_OF_DATE_KHR;
1818 }
1819
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001820 // TODO(chrisforbes): Implement this function properly
1821
1822 return result;
1823}
1824
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001825VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001826 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001827 uint32_t swapchainCount,
1828 const VkSwapchainKHR* pSwapchains,
1829 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001830 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001831
1832 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1833 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1834 if (!swapchain)
1835 continue;
1836
1837 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1838
1839 ANativeWindow* window = swapchain->surface.window.get();
1840
1841 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1842 const android_smpte2086_metadata smpteMetdata = {
1843 {vulkanMetadata.displayPrimaryRed.x,
1844 vulkanMetadata.displayPrimaryRed.y},
1845 {vulkanMetadata.displayPrimaryGreen.x,
1846 vulkanMetadata.displayPrimaryGreen.y},
1847 {vulkanMetadata.displayPrimaryBlue.x,
1848 vulkanMetadata.displayPrimaryBlue.y},
1849 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1850 vulkanMetadata.maxLuminance,
1851 vulkanMetadata.minLuminance};
1852 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1853
1854 const android_cta861_3_metadata cta8613Metadata = {
1855 vulkanMetadata.maxContentLightLevel,
1856 vulkanMetadata.maxFrameAverageLightLevel};
1857 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1858 }
1859
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001860 return;
1861}
1862
Yiwei Zhang0f475222019-04-11 19:38:00 -07001863static void InterceptBindImageMemory2(
1864 uint32_t bind_info_count,
1865 const VkBindImageMemoryInfo* bind_infos,
1866 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1867 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1868 out_native_buffers->clear();
1869 out_bind_infos->clear();
1870
1871 if (!bind_info_count)
1872 return;
1873
1874 std::unordered_set<uint32_t> intercepted_indexes;
1875
1876 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1877 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1878 bind_infos[idx].pNext);
1879 while (info &&
1880 info->sType !=
1881 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1882 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1883 info->pNext);
1884 }
1885
1886 if (!info)
1887 continue;
1888
1889 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1890 "swapchain handle must not be NULL");
1891 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1892 ALOG_ASSERT(
1893 info->imageIndex < swapchain->num_images,
1894 "imageIndex must be less than the number of images in swapchain");
1895
1896 ANativeWindowBuffer* buffer =
1897 swapchain->images[info->imageIndex].buffer.get();
1898 VkNativeBufferANDROID native_buffer = {
1899#pragma clang diagnostic push
1900#pragma clang diagnostic ignored "-Wold-style-cast"
1901 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1902#pragma clang diagnostic pop
1903 .pNext = bind_infos[idx].pNext,
1904 .handle = buffer->handle,
1905 .stride = buffer->stride,
1906 .format = buffer->format,
1907 .usage = int(buffer->usage),
1908 };
1909 // Reserve enough space to avoid letting re-allocation invalidate the
1910 // addresses of the elements inside.
1911 out_native_buffers->reserve(bind_info_count);
1912 out_native_buffers->emplace_back(native_buffer);
1913
1914 // Reserve the space now since we know how much is needed now.
1915 out_bind_infos->reserve(bind_info_count);
1916 out_bind_infos->emplace_back(bind_infos[idx]);
1917 out_bind_infos->back().pNext = &out_native_buffers->back();
1918
1919 intercepted_indexes.insert(idx);
1920 }
1921
1922 if (intercepted_indexes.empty())
1923 return;
1924
1925 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1926 if (intercepted_indexes.count(idx))
1927 continue;
1928 out_bind_infos->emplace_back(bind_infos[idx]);
1929 }
1930}
1931
Yiwei Zhang23143102019-04-10 18:24:05 -07001932VKAPI_ATTR
1933VkResult BindImageMemory2(VkDevice device,
1934 uint32_t bindInfoCount,
1935 const VkBindImageMemoryInfo* pBindInfos) {
1936 ATRACE_CALL();
1937
Yiwei Zhang0f475222019-04-11 19:38:00 -07001938 // out_native_buffers is for maintaining the lifecycle of the constructed
1939 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1940 std::vector<VkNativeBufferANDROID> out_native_buffers;
1941 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1942 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1943 &out_bind_infos);
1944 return GetData(device).driver.BindImageMemory2(
1945 device, bindInfoCount,
1946 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001947}
1948
1949VKAPI_ATTR
1950VkResult BindImageMemory2KHR(VkDevice device,
1951 uint32_t bindInfoCount,
1952 const VkBindImageMemoryInfo* pBindInfos) {
1953 ATRACE_CALL();
1954
Yiwei Zhang0f475222019-04-11 19:38:00 -07001955 std::vector<VkNativeBufferANDROID> out_native_buffers;
1956 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1957 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1958 &out_bind_infos);
1959 return GetData(device).driver.BindImageMemory2KHR(
1960 device, bindInfoCount,
1961 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001962}
1963
Chia-I Wu62262232016-03-26 07:06:44 +08001964} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001965} // namespace vulkan