blob: e5f40aa84ad05c55ac349b23b539bcea5a3b4970 [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
58VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
59 // Native and Vulkan transforms are isomorphic, but are represented
60 // differently. Vulkan transforms are built up of an optional horizontal
61 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
62 // transforms are built up from a horizontal flip, vertical flip, and
63 // 90-degree rotation, all optional but always in that order.
64
65 // TODO(jessehall): For now, only support pure rotations, not
66 // flip or flip-and-rotate, until I have more time to test them and build
67 // sample code. As far as I know we never actually use anything besides
68 // pure rotations anyway.
69
70 switch (native) {
71 case 0: // 0x0
72 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
73 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
74 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
75 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
76 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
77 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
78 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
79 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
80 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
81 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
82 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
83 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
84 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
85 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
86 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
87 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
88 default:
89 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
90 }
91}
92
Jesse Hall178b6962016-02-24 15:39:50 -080093int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
94 switch (transform) {
95 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
96 return NATIVE_WINDOW_TRANSFORM_ROT_270;
97 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
98 return NATIVE_WINDOW_TRANSFORM_ROT_180;
99 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
100 return NATIVE_WINDOW_TRANSFORM_ROT_90;
101 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
102 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
103 // return NATIVE_WINDOW_TRANSFORM_FLIP_H;
104 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
105 // return NATIVE_WINDOW_TRANSFORM_FLIP_H |
106 // NATIVE_WINDOW_TRANSFORM_ROT_90;
107 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
108 // return NATIVE_WINDOW_TRANSFORM_FLIP_V;
109 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
110 // return NATIVE_WINDOW_TRANSFORM_FLIP_V |
111 // NATIVE_WINDOW_TRANSFORM_ROT_90;
112 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
113 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
114 default:
115 return 0;
116 }
117}
118
Ian Elliott8a977262017-01-19 09:05:58 -0700119class TimingInfo {
120 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800121 TimingInfo() = default;
122 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700123 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800124 native_frame_id_(nativeFrameId) {}
125 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700126 return (timestamp_desired_present_time_ !=
127 NATIVE_WINDOW_TIMESTAMP_PENDING &&
128 timestamp_actual_present_time_ !=
129 NATIVE_WINDOW_TIMESTAMP_PENDING &&
130 timestamp_render_complete_time_ !=
131 NATIVE_WINDOW_TIMESTAMP_PENDING &&
132 timestamp_composition_latch_time_ !=
133 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700134 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700135 void calculate(int64_t rdur) {
136 bool anyTimestampInvalid =
137 (timestamp_actual_present_time_ ==
138 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
139 (timestamp_render_complete_time_ ==
140 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
141 (timestamp_composition_latch_time_ ==
142 NATIVE_WINDOW_TIMESTAMP_INVALID);
143 if (anyTimestampInvalid) {
144 ALOGE("Unexpectedly received invalid timestamp.");
145 vals_.actualPresentTime = 0;
146 vals_.earliestPresentTime = 0;
147 vals_.presentMargin = 0;
148 return;
149 }
150
151 vals_.actualPresentTime =
152 static_cast<uint64_t>(timestamp_actual_present_time_);
153 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700154 timestamp_render_complete_time_);
155 // Calculate vals_.earliestPresentTime, and potentially adjust
156 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
157 // is vals_.actualPresentTime. If we can subtract rdur (the duration
158 // of a refresh cycle) from vals_.earliestPresentTime (and also from
159 // vals_.presentMargin) and still leave a positive margin, then we can
160 // report to the application that it could have presented earlier than
161 // it did (per the extension specification). If for some reason, we
162 // can do this subtraction repeatedly, we do, since
163 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700164 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700165 while ((margin > rdur) &&
166 ((early_time - rdur) > timestamp_composition_latch_time_)) {
167 early_time -= rdur;
168 margin -= rdur;
169 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700170 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
171 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700172 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800173 void get_values(VkPastPresentationTimingGOOGLE* values) const {
174 *values = vals_;
175 }
Ian Elliott8a977262017-01-19 09:05:58 -0700176
177 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800178 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700179
Brian Anderson1049d1d2016-12-16 17:25:57 -0800180 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700181 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
182 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
183 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
184 int64_t timestamp_composition_latch_time_
185 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700186};
187
Jesse Halld7b994a2015-09-07 14:17:37 -0700188// ----------------------------------------------------------------------------
189
Jesse Hall1356b0d2015-11-23 17:24:58 -0800190struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800191 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700192 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700193 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800194};
195
196VkSurfaceKHR HandleFromSurface(Surface* surface) {
197 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
198}
199
200Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800201 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800202}
203
Ian Elliott8a977262017-01-19 09:05:58 -0700204// Maximum number of TimingInfo structs to keep per swapchain:
205enum { MAX_TIMING_INFOS = 10 };
206// Minimum number of frames to look for in the past (so we don't cause
207// syncronous requests to Surface Flinger):
208enum { MIN_NUM_FRAMES_AGO = 5 };
209
Jesse Hall1356b0d2015-11-23 17:24:58 -0800210struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700211 Swapchain(Surface& surface_,
212 uint32_t num_images_,
213 VkPresentModeKHR present_mode)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700214 : surface(surface_),
215 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700216 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
Chris Forbesf8835642017-03-30 19:31:40 +1300217 frame_timestamps_enabled(false),
218 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
219 present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700220 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700221 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700222 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700223 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700224 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600225 uint64_t get_refresh_duration()
226 {
227 ANativeWindow* window = surface.window.get();
228 native_window_get_refresh_cycle_duration(
229 window,
230 &refresh_duration);
231 return static_cast<uint64_t>(refresh_duration);
232
233 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800234
235 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700236 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700237 bool mailbox_mode;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700238 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700239 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300240 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700241
242 struct Image {
243 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
244 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800245 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700246 // The fence is only valid when the buffer is dequeued, and should be
247 // -1 any other time. When valid, we own the fd, and must ensure it is
248 // closed: either by closing it explicitly when queueing the buffer,
249 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
250 int dequeue_fence;
251 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800252 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700253
Brian Anderson1049d1d2016-12-16 17:25:57 -0800254 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700255};
256
257VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
258 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
259}
260
261Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800262 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700263}
264
Jesse Halldc225072016-05-30 22:40:14 -0700265void ReleaseSwapchainImage(VkDevice device,
266 ANativeWindow* window,
267 int release_fence,
268 Swapchain::Image& image) {
269 ALOG_ASSERT(release_fence == -1 || image.dequeued,
270 "ReleaseSwapchainImage: can't provide a release fence for "
271 "non-dequeued images");
272
273 if (image.dequeued) {
274 if (release_fence >= 0) {
275 // We get here from vkQueuePresentKHR. The application is
276 // responsible for creating an execution dependency chain from
277 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
278 // (release_fence), so we can drop the dequeue_fence here.
279 if (image.dequeue_fence >= 0)
280 close(image.dequeue_fence);
281 } else {
282 // We get here during swapchain destruction, or various serious
283 // error cases e.g. when we can't create the release_fence during
284 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
285 // have already signalled, since the swapchain images are supposed
286 // to be idle before the swapchain is destroyed. In error cases,
287 // there may be rendering in flight to the image, but since we
288 // weren't able to create a release_fence, waiting for the
289 // dequeue_fence is about the best we can do.
290 release_fence = image.dequeue_fence;
291 }
292 image.dequeue_fence = -1;
293
294 if (window) {
295 window->cancelBuffer(window, image.buffer.get(), release_fence);
296 } else {
297 if (release_fence >= 0) {
298 sync_wait(release_fence, -1 /* forever */);
299 close(release_fence);
300 }
301 }
302
303 image.dequeued = false;
304 }
305
306 if (image.image) {
307 GetData(device).driver.DestroyImage(device, image.image, nullptr);
308 image.image = VK_NULL_HANDLE;
309 }
310
311 image.buffer.clear();
312}
313
314void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
315 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
316 return;
Jesse Halldc225072016-05-30 22:40:14 -0700317 for (uint32_t i = 0; i < swapchain->num_images; i++) {
318 if (!swapchain->images[i].dequeued)
319 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
320 }
321 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700322 swapchain->timing.clear();
323}
324
325uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800326 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
327 return 0;
328 }
Ian Elliott8a977262017-01-19 09:05:58 -0700329
Brian Anderson1049d1d2016-12-16 17:25:57 -0800330 uint32_t num_ready = 0;
331 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
332 for (uint32_t i = 0; i < num_timings; i++) {
333 TimingInfo& ti = swapchain.timing.editItemAt(i);
334 if (ti.ready()) {
335 // This TimingInfo is ready to be reported to the user. Add it
336 // to the num_ready.
337 num_ready++;
338 continue;
339 }
340 // This TimingInfo is not yet ready to be reported to the user,
341 // and so we should look for any available timestamps that
342 // might make it ready.
343 int64_t desired_present_time = 0;
344 int64_t render_complete_time = 0;
345 int64_t composition_latch_time = 0;
346 int64_t actual_present_time = 0;
347 // Obtain timestamps:
348 int ret = native_window_get_frame_timestamps(
349 swapchain.surface.window.get(), ti.native_frame_id_,
350 &desired_present_time, &render_complete_time,
351 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700352 nullptr, //&first_composition_start_time,
353 nullptr, //&last_composition_start_time,
354 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800355 // TODO(ianelliott): Maybe ask if this one is
356 // supported, at startup time (since it may not be
357 // supported):
358 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700359 nullptr, //&dequeue_ready_time,
360 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800361
362 if (ret != android::NO_ERROR) {
363 continue;
364 }
365
366 // Record the timestamp(s) we received, and then see if this TimingInfo
367 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700368 ti.timestamp_desired_present_time_ = desired_present_time;
369 ti.timestamp_actual_present_time_ = actual_present_time;
370 ti.timestamp_render_complete_time_ = render_complete_time;
371 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800372
373 if (ti.ready()) {
374 // The TimingInfo has received enough timestamps, and should now
375 // use those timestamps to calculate the info that should be
376 // reported to the user:
377 ti.calculate(swapchain.refresh_duration);
378 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700379 }
380 }
381 return num_ready;
382}
383
384// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
385void copy_ready_timings(Swapchain& swapchain,
386 uint32_t* count,
387 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800388 if (swapchain.timing.empty()) {
389 *count = 0;
390 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700391 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800392
393 size_t last_ready = swapchain.timing.size() - 1;
394 while (!swapchain.timing[last_ready].ready()) {
395 if (last_ready == 0) {
396 *count = 0;
397 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700398 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800399 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700400 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800401
402 uint32_t num_copied = 0;
403 size_t num_to_remove = 0;
404 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
405 const TimingInfo& ti = swapchain.timing[i];
406 if (ti.ready()) {
407 ti.get_values(&timings[num_copied]);
408 num_copied++;
409 }
410 num_to_remove++;
411 }
412
413 // Discard old frames that aren't ready if newer frames are ready.
414 // We don't expect to get the timing info for those old frames.
415 swapchain.timing.removeItemsAt(0, num_to_remove);
416
Ian Elliott8a977262017-01-19 09:05:58 -0700417 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700418}
419
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700420android_pixel_format GetNativePixelFormat(VkFormat format) {
421 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
422 switch (format) {
423 case VK_FORMAT_R8G8B8A8_UNORM:
424 case VK_FORMAT_R8G8B8A8_SRGB:
425 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
426 break;
427 case VK_FORMAT_R5G6B5_UNORM_PACK16:
428 native_format = HAL_PIXEL_FORMAT_RGB_565;
429 break;
430 case VK_FORMAT_R16G16B16A16_SFLOAT:
431 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
432 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800433 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700434 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
435 break;
436 default:
437 ALOGV("unsupported swapchain format %d", format);
438 break;
439 }
440 return native_format;
441}
442
443android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
444 switch (colorspace) {
445 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
446 return HAL_DATASPACE_V0_SRGB;
447 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
448 return HAL_DATASPACE_DISPLAY_P3;
449 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
450 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600451 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
452 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700453 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
454 return HAL_DATASPACE_DCI_P3_LINEAR;
455 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
456 return HAL_DATASPACE_DCI_P3;
457 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
458 return HAL_DATASPACE_V0_SRGB_LINEAR;
459 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
460 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600461 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
462 return HAL_DATASPACE_BT2020_LINEAR;
463 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700464 return static_cast<android_dataspace>(
465 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
466 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600467 case VK_COLOR_SPACE_DOLBYVISION_EXT:
468 return static_cast<android_dataspace>(
469 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
470 HAL_DATASPACE_RANGE_FULL);
471 case VK_COLOR_SPACE_HDR10_HLG_EXT:
472 return static_cast<android_dataspace>(
473 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
474 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700475 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
476 return static_cast<android_dataspace>(
477 HAL_DATASPACE_STANDARD_ADOBE_RGB |
478 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
479 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
480 return HAL_DATASPACE_ADOBE_RGB;
481
482 // Pass through is intended to allow app to provide data that is passed
483 // to the display system without modification.
484 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
485 return HAL_DATASPACE_ARBITRARY;
486
487 default:
488 // This indicates that we don't know about the
489 // dataspace specified and we should indicate that
490 // it's unsupported
491 return HAL_DATASPACE_UNKNOWN;
492 }
493}
494
Jesse Halld7b994a2015-09-07 14:17:37 -0700495} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700496
Jesse Halle1b12782015-11-30 11:27:32 -0800497VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800498VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800499 VkInstance instance,
500 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
501 const VkAllocationCallbacks* allocator,
502 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800503 ATRACE_CALL();
504
Jesse Hall1f91d392015-12-11 16:28:44 -0800505 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800506 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800507 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
508 alignof(Surface),
509 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800510 if (!mem)
511 return VK_ERROR_OUT_OF_HOST_MEMORY;
512 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700513
Chia-I Wue8e689f2016-04-18 08:21:31 +0800514 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700515 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700516 int err = native_window_get_consumer_usage(surface->window.get(),
517 &surface->consumer_usage);
518 if (err != android::NO_ERROR) {
519 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
520 strerror(-err), err);
521 surface->~Surface();
522 allocator->pfnFree(allocator->pUserData, surface);
523 return VK_ERROR_INITIALIZATION_FAILED;
524 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700525
Jesse Hall1356b0d2015-11-23 17:24:58 -0800526 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
Yiwei Zhang6435b322018-05-08 11:12:17 -0700527 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800528 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
529 if (err != 0) {
530 // TODO(jessehall): Improve error reporting. Can we enumerate possible
531 // errors and translate them to valid Vulkan result codes?
532 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
533 err);
534 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800535 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700536 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800537 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700538
Jesse Hall1356b0d2015-11-23 17:24:58 -0800539 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700540 return VK_SUCCESS;
541}
542
Jesse Halle1b12782015-11-30 11:27:32 -0800543VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800544void DestroySurfaceKHR(VkInstance instance,
545 VkSurfaceKHR surface_handle,
546 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800547 ATRACE_CALL();
548
Jesse Hall1356b0d2015-11-23 17:24:58 -0800549 Surface* surface = SurfaceFromHandle(surface_handle);
550 if (!surface)
551 return;
552 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700553 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700554 "destroyed VkSurfaceKHR 0x%" PRIx64
555 " has active VkSwapchainKHR 0x%" PRIx64,
556 reinterpret_cast<uint64_t>(surface_handle),
557 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800558 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800559 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800560 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800561 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800562}
563
Jesse Halle1b12782015-11-30 11:27:32 -0800564VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800565VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
566 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700567 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800568 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800569 ATRACE_CALL();
570
Yiwei Zhang6435b322018-05-08 11:12:17 -0700571 const Surface* surface = SurfaceFromHandle(surface_handle);
572 if (!surface) {
573 return VK_ERROR_SURFACE_LOST_KHR;
574 }
575 const ANativeWindow* window = surface->window.get();
576
577 int query_value;
578 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
579 if (err != 0 || query_value < 0) {
580 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
581 strerror(-err), err, query_value);
582 return VK_ERROR_SURFACE_LOST_KHR;
583 }
584
585 android_pixel_format native_format =
586 static_cast<android_pixel_format>(query_value);
587
588 bool format_supported = false;
589 switch (native_format) {
590 case HAL_PIXEL_FORMAT_RGBA_8888:
591 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700592 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800593 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700594 format_supported = true;
595 break;
596 default:
597 break;
598 }
599
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700600 *supported = static_cast<VkBool32>(
601 format_supported || (surface->consumer_usage &
602 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
603 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700604
Jesse Halla6429252015-11-29 18:59:42 -0800605 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800606}
607
Jesse Halle1b12782015-11-30 11:27:32 -0800608VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800609VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800610 VkPhysicalDevice /*pdev*/,
611 VkSurfaceKHR surface,
612 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800613 ATRACE_CALL();
614
Jesse Halld7b994a2015-09-07 14:17:37 -0700615 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800616 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700617
618 int width, height;
619 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
620 if (err != 0) {
621 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
622 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700623 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700624 }
625 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
626 if (err != 0) {
627 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
628 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700629 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700630 }
631
Jesse Hall55bc0972016-02-23 16:43:29 -0800632 int transform_hint;
633 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
634 if (err != 0) {
635 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
636 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700637 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800638 }
639
Jesse Halld7b994a2015-09-07 14:17:37 -0700640 // TODO(jessehall): Figure out what the min/max values should be.
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800641 int max_buffer_count;
642 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
643 if (err != 0) {
644 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
645 strerror(-err), err);
646 return VK_ERROR_SURFACE_LOST_KHR;
647 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700648 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800649 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700650
Jesse Hallfe2662d2016-02-09 13:26:59 -0800651 capabilities->currentExtent =
652 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
653
Jesse Halld7b994a2015-09-07 14:17:37 -0700654 // TODO(jessehall): Figure out what the max extent should be. Maximum
655 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800656 capabilities->minImageExtent = VkExtent2D{1, 1};
657 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700658
Jesse Hallfe2662d2016-02-09 13:26:59 -0800659 capabilities->maxImageArrayLayers = 1;
660
Jesse Hall55bc0972016-02-23 16:43:29 -0800661 capabilities->supportedTransforms = kSupportedTransforms;
662 capabilities->currentTransform =
663 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700664
Jesse Hallfe2662d2016-02-09 13:26:59 -0800665 // On Android, window composition is a WindowManager property, not something
666 // associated with the bufferqueue. It can't be changed from here.
667 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700668
669 // TODO(jessehall): I think these are right, but haven't thought hard about
670 // it. Do we need to query the driver for support of any of these?
671 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700672 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
673 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800674 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800675 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
676 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
677 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700678 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
679
Jesse Hallb1352bc2015-09-04 16:12:33 -0700680 return VK_SUCCESS;
681}
682
Jesse Halle1b12782015-11-30 11:27:32 -0800683VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700684VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
685 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800686 uint32_t* count,
687 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800688 ATRACE_CALL();
689
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700690 const InstanceData& instance_data = GetData(pdev);
691
Jesse Hall1356b0d2015-11-23 17:24:58 -0800692 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
693 // a new gralloc method to query whether a (format, usage) pair is
694 // supported, and check that for each gralloc format that corresponds to a
695 // Vulkan format. Shorter term, just add a few more formats to the ones
696 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700697
698 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700699 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
700 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
701 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Ian Elliott94ace212019-02-20 14:05:29 -0700702 {VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
703 {VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700704 };
705 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700706 uint32_t total_num_formats = kNumFormats;
707
708 bool wide_color_support = false;
709 Surface& surface = *SurfaceFromHandle(surface_handle);
710 int err = native_window_get_wide_color_support(surface.window.get(),
711 &wide_color_support);
712 if (err) {
713 // Not allowed to return a more sensible error code, so do this
714 return VK_ERROR_OUT_OF_HOST_MEMORY;
715 }
716 ALOGV("wide_color_support is: %d", wide_color_support);
717 wide_color_support =
718 wide_color_support &&
719 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
720
721 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600722 {VK_FORMAT_R8G8B8A8_UNORM,
723 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
724 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700725 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700726 {VK_FORMAT_R16G16B16A16_SFLOAT,
727 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
728 {VK_FORMAT_R16G16B16A16_SFLOAT,
729 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800730 {VK_FORMAT_A2B10G10R10_UNORM_PACK32,
731 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700732 };
733 const uint32_t kNumWideColorFormats =
734 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
735 if (wide_color_support) {
736 total_num_formats += kNumWideColorFormats;
737 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700738
739 VkResult result = VK_SUCCESS;
740 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700741 uint32_t out_count = 0;
742 uint32_t transfer_count = 0;
743 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700744 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700745 transfer_count = std::min(*count, kNumFormats);
746 std::copy(kFormats, kFormats + transfer_count, formats);
747 out_count += transfer_count;
748 if (wide_color_support) {
749 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
750 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
751 formats + out_count);
752 out_count += transfer_count;
753 }
754 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700755 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700756 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700757 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700758 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700759}
760
Jesse Halle1b12782015-11-30 11:27:32 -0800761VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300762VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
763 VkPhysicalDevice physicalDevice,
764 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
765 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800766 ATRACE_CALL();
767
Chris Forbes2452cf72017-03-16 16:30:17 +1300768 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
769 physicalDevice, pSurfaceInfo->surface,
770 &pSurfaceCapabilities->surfaceCapabilities);
771
Chris Forbes06bc0092017-03-16 16:46:05 +1300772 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
773 while (caps->pNext) {
774 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
775
776 switch (caps->sType) {
777 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
778 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
779 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
780 caps);
781 // Claim same set of usage flags are supported for
782 // shared present modes as for other modes.
783 shared_caps->sharedPresentSupportedUsageFlags =
784 pSurfaceCapabilities->surfaceCapabilities
785 .supportedUsageFlags;
786 } break;
787
788 default:
789 // Ignore all other extension structs
790 break;
791 }
792 }
793
Chris Forbes2452cf72017-03-16 16:30:17 +1300794 return result;
795}
796
797VKAPI_ATTR
798VkResult GetPhysicalDeviceSurfaceFormats2KHR(
799 VkPhysicalDevice physicalDevice,
800 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
801 uint32_t* pSurfaceFormatCount,
802 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800803 ATRACE_CALL();
804
Chris Forbes2452cf72017-03-16 16:30:17 +1300805 if (!pSurfaceFormats) {
806 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
807 pSurfaceInfo->surface,
808 pSurfaceFormatCount, nullptr);
809 } else {
810 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
811 // after the call.
812 android::Vector<VkSurfaceFormatKHR> surface_formats;
813 surface_formats.resize(*pSurfaceFormatCount);
814 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
815 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
816 &surface_formats.editItemAt(0));
817
818 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
819 // marshal results individually due to stride difference.
820 // completely ignore any chained extension structs.
821 uint32_t formats_to_marshal = *pSurfaceFormatCount;
822 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
823 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
824 }
825 }
826
827 return result;
828 }
829}
830
831VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300832VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800833 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800834 uint32_t* count,
835 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800836 ATRACE_CALL();
837
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800838 int err;
839 int query_value;
840 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
841
842 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
843 if (err != 0 || query_value < 0) {
844 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
845 strerror(-err), err, query_value);
846 return VK_ERROR_SURFACE_LOST_KHR;
847 }
848 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
849
850 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
851 if (err != 0 || query_value < 0) {
852 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
853 strerror(-err), err, query_value);
854 return VK_ERROR_SURFACE_LOST_KHR;
855 }
856 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
857
Chris Forbese8d79a62017-02-22 12:49:18 +1300858 android::Vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800859 if (min_undequeued_buffers + 1 < max_buffer_count)
860 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300861 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
862
863 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
864 if (QueryPresentationProperties(pdev, &present_properties)) {
865 if (present_properties.sharedImage) {
866 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
867 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
868 }
869 }
870
871 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700872
873 VkResult result = VK_SUCCESS;
874 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300875 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700876 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300877 *count = std::min(*count, num_modes);
878 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700879 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300880 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700881 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700882 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700883}
884
Jesse Halle1b12782015-11-30 11:27:32 -0800885VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400886VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600887 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400888 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800889 ATRACE_CALL();
890
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400891 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
892 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
893 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
894 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
895 pDeviceGroupPresentCapabilities->sType);
896
897 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
898 sizeof(pDeviceGroupPresentCapabilities->presentMask));
899
900 // assume device group of size 1
901 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
902 pDeviceGroupPresentCapabilities->modes =
903 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
904
905 return VK_SUCCESS;
906}
907
908VKAPI_ATTR
909VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600910 VkDevice,
911 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400912 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800913 ATRACE_CALL();
914
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400915 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
916 return VK_SUCCESS;
917}
918
919VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600920VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400921 VkSurfaceKHR surface,
922 uint32_t* pRectCount,
923 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800924 ATRACE_CALL();
925
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400926 if (!pRects) {
927 *pRectCount = 1;
928 } else {
929 uint32_t count = std::min(*pRectCount, 1u);
930 bool incomplete = *pRectCount < 1;
931
932 *pRectCount = count;
933
934 if (incomplete) {
935 return VK_INCOMPLETE;
936 }
937
938 int err;
939 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
940
941 int width = 0, height = 0;
942 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
943 if (err != 0) {
944 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
945 strerror(-err), err);
946 }
947 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
948 if (err != 0) {
949 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
950 strerror(-err), err);
951 }
952
953 // TODO: Return something better than "whole window"
954 pRects[0].offset.x = 0;
955 pRects[0].offset.y = 0;
956 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
957 static_cast<uint32_t>(height)};
958 }
959 return VK_SUCCESS;
960}
961
962VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800963VkResult CreateSwapchainKHR(VkDevice device,
964 const VkSwapchainCreateInfoKHR* create_info,
965 const VkAllocationCallbacks* allocator,
966 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800967 ATRACE_CALL();
968
Jesse Halld7b994a2015-09-07 14:17:37 -0700969 int err;
970 VkResult result = VK_SUCCESS;
971
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700972 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
973 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
974 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
975 " oldSwapchain=0x%" PRIx64,
976 reinterpret_cast<uint64_t>(create_info->surface),
977 create_info->minImageCount, create_info->imageFormat,
978 create_info->imageColorSpace, create_info->imageExtent.width,
979 create_info->imageExtent.height, create_info->imageUsage,
980 create_info->preTransform, create_info->presentMode,
981 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
982
Jesse Hall1f91d392015-12-11 16:28:44 -0800983 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800984 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800985
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700986 android_pixel_format native_pixel_format =
987 GetNativePixelFormat(create_info->imageFormat);
988 android_dataspace native_dataspace =
989 GetNativeDataspace(create_info->imageColorSpace);
990 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
991 ALOGE(
992 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
993 "failed: Unsupported color space",
994 create_info->imageColorSpace);
995 return VK_ERROR_INITIALIZATION_FAILED;
996 }
997
Jesse Hall42a9eec2016-06-03 12:39:49 -0700998 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700999 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001000 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001001 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001002 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001003 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001004 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001005 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001006 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1007 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001008 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001009 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001010
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001011 Surface& surface = *SurfaceFromHandle(create_info->surface);
1012
Jesse Halldc225072016-05-30 22:40:14 -07001013 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001014 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001015 " because it already has active swapchain 0x%" PRIx64
1016 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1017 reinterpret_cast<uint64_t>(create_info->surface),
1018 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1019 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1020 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1021 }
1022 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1023 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1024
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001025 // -- Reset the native window --
1026 // The native window might have been used previously, and had its properties
1027 // changed from defaults. That will affect the answer we get for queries
1028 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1029 // attempt such queries.
1030
Jesse Halldc225072016-05-30 22:40:14 -07001031 // The native window only allows dequeueing all buffers before any have
1032 // been queued, since after that point at least one is assumed to be in
1033 // non-FREE state at any given time. Disconnecting and re-connecting
1034 // orphans the previous buffers, getting us back to the state where we can
1035 // dequeue all buffers.
1036 err = native_window_api_disconnect(surface.window.get(),
1037 NATIVE_WINDOW_API_EGL);
1038 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1039 strerror(-err), err);
1040 err =
1041 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1042 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1043 strerror(-err), err);
1044
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001045 err = native_window_set_buffer_count(surface.window.get(), 0);
1046 if (err != 0) {
1047 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1048 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001049 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001050 }
1051
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301052 int swap_interval =
1053 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1054 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001055 if (err != 0) {
1056 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1057 // errors and translate them to valid Vulkan result codes?
1058 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1059 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001060 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001061 }
1062
Chris Forbesb8042d22017-01-18 18:07:05 +13001063 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1064 if (err != 0) {
1065 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1066 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001067 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001068 }
1069
1070 err = native_window_set_auto_refresh(surface.window.get(), false);
1071 if (err != 0) {
1072 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1073 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001074 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001075 }
1076
Jesse Halld7b994a2015-09-07 14:17:37 -07001077 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001078
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001079 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001080
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001081 err = native_window_set_buffers_format(surface.window.get(),
1082 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001083 if (err != 0) {
1084 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1085 // errors and translate them to valid Vulkan result codes?
1086 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001087 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001088 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001089 }
1090 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001091 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001092 if (err != 0) {
1093 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1094 // errors and translate them to valid Vulkan result codes?
1095 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001096 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001097 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001098 }
1099
Jesse Hall3dd678a2016-01-08 21:52:01 -08001100 err = native_window_set_buffers_dimensions(
1101 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1102 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001103 if (err != 0) {
1104 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1105 // errors and translate them to valid Vulkan result codes?
1106 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1107 create_info->imageExtent.width, create_info->imageExtent.height,
1108 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001109 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001110 }
1111
Jesse Hall178b6962016-02-24 15:39:50 -08001112 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1113 // applied during rendering. native_window_set_transform() expects the
1114 // inverse: the transform the app is requesting that the compositor perform
1115 // during composition. With native windows, pre-transform works by rendering
1116 // with the same transform the compositor is applying (as in Vulkan), but
1117 // then requesting the inverse transform, so that when the compositor does
1118 // it's job the two transforms cancel each other out and the compositor ends
1119 // up applying an identity transform to the app's buffer.
1120 err = native_window_set_buffers_transform(
1121 surface.window.get(),
1122 InvertTransformToNative(create_info->preTransform));
1123 if (err != 0) {
1124 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1125 // errors and translate them to valid Vulkan result codes?
1126 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1127 InvertTransformToNative(create_info->preTransform),
1128 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001129 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001130 }
1131
Jesse Hallf64ca122015-11-03 16:11:10 -08001132 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001133 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001134 if (err != 0) {
1135 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1136 // errors and translate them to valid Vulkan result codes?
1137 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1138 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001139 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001140 }
1141
Chris Forbes97ef4612017-03-30 19:37:50 +13001142 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1143 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1144 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1145 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1146 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1147 if (err != 0) {
1148 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1149 return VK_ERROR_SURFACE_LOST_KHR;
1150 }
1151 }
1152
1153 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1154 err = native_window_set_auto_refresh(surface.window.get(), true);
1155 if (err != 0) {
1156 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1157 return VK_ERROR_SURFACE_LOST_KHR;
1158 }
1159 }
1160
Jesse Halle6080bf2016-02-28 20:58:50 -08001161 int query_value;
1162 err = surface.window->query(surface.window.get(),
1163 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1164 &query_value);
1165 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001166 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1167 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -08001168 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1169 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001170 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001171 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001172 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001173 uint32_t num_images =
Ian Elliotta3515f42019-05-15 14:11:33 -06001174 (swap_interval ? create_info->minImageCount
1175 : std::max(3u, create_info->minImageCount)) -
1176 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001177
1178 // Lower layer insists that we have at least two buffers. This is wasteful
1179 // and we'd like to relax it in the shared case, but not all the pieces are
1180 // in place for that to work yet. Note we only lie to the lower layer-- we
1181 // don't want to give the app back a swapchain with extra images (which they
1182 // can't actually use!).
1183 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001184 if (err != 0) {
1185 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1186 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001187 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1188 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001189 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001190 }
1191
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001192 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001193 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001194 uint64_t consumer_usage, producer_usage;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001195 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001196 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1197 device, create_info->imageFormat, create_info->imageUsage,
1198 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001199 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001200 if (result != VK_SUCCESS) {
1201 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001202 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001203 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001204 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001205 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001206 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001207 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001208 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001209 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001210 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001211 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001212 if (result != VK_SUCCESS) {
1213 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001214 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001215 }
Jesse Hall70f93352015-11-04 09:41:31 -08001216 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001217 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1218
1219 bool createProtectedSwapchain = false;
1220 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1221 createProtectedSwapchain = true;
1222 native_usage |= BufferUsage::PROTECTED;
1223 }
1224 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001225 if (err != 0) {
1226 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1227 // errors and translate them to valid Vulkan result codes?
1228 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001229 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001230 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001231
1232 // -- Allocate our Swapchain object --
1233 // After this point, we must deallocate the swapchain on error.
1234
Jesse Hall1f91d392015-12-11 16:28:44 -08001235 void* mem = allocator->pfnAllocation(allocator->pUserData,
1236 sizeof(Swapchain), alignof(Swapchain),
1237 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001238 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001239 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001240 Swapchain* swapchain =
1241 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001242
1243 // -- Dequeue all buffers and create a VkImage for each --
1244 // Any failures during or after this must cancel the dequeued buffers.
1245
Chris Forbesb56287a2017-01-12 14:28:58 +13001246 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1247#pragma clang diagnostic push
1248#pragma clang diagnostic ignored "-Wold-style-cast"
1249 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1250#pragma clang diagnostic pop
1251 .pNext = nullptr,
1252 .usage = swapchain_image_usage,
1253 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001254 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001255#pragma clang diagnostic push
1256#pragma clang diagnostic ignored "-Wold-style-cast"
1257 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1258#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001259 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001260 };
1261 VkImageCreateInfo image_create = {
1262 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1263 .pNext = &image_native_buffer,
1264 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001265 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001266 .extent = {0, 0, 1},
1267 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001268 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001269 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001270 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001271 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001272 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001273 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001274 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001275 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1276 };
1277
Jesse Halld7b994a2015-09-07 14:17:37 -07001278 for (uint32_t i = 0; i < num_images; i++) {
1279 Swapchain::Image& img = swapchain->images[i];
1280
1281 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001282 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1283 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001284 if (err != 0) {
1285 // TODO(jessehall): Improve error reporting. Can we enumerate
1286 // possible errors and translate them to valid Vulkan result codes?
1287 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001288 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001289 break;
1290 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001291 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001292 img.dequeued = true;
1293
1294 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001295 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1296 static_cast<uint32_t>(img.buffer->height),
1297 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001298 image_native_buffer.handle = img.buffer->handle;
1299 image_native_buffer.stride = img.buffer->stride;
1300 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001301 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001302 android_convertGralloc0To1Usage(int(img.buffer->usage),
1303 &image_native_buffer.usage2.producer,
1304 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001305
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001306 ATRACE_BEGIN("dispatch.CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001307 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001308 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001309 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001310 if (result != VK_SUCCESS) {
1311 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1312 break;
1313 }
1314 }
1315
1316 // -- Cancel all buffers, returning them to the queue --
1317 // If an error occurred before, also destroy the VkImage and release the
1318 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1319 //
1320 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1321 // but not the non-error path. Should refactor/unify.
Chris Forbes31b85c22018-05-29 15:03:28 -07001322 for (uint32_t i = 0; i < num_images; i++) {
1323 Swapchain::Image& img = swapchain->images[i];
1324 if (img.dequeued) {
1325 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001326 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1327 img.dequeue_fence);
1328 img.dequeue_fence = -1;
1329 img.dequeued = false;
1330 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001331 }
1332 if (result != VK_SUCCESS) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001333 if (img.image) {
1334 ATRACE_BEGIN("dispatch.DestroyImage");
Chris Forbes31b85c22018-05-29 15:03:28 -07001335 dispatch.DestroyImage(device, img.image, nullptr);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001336 ATRACE_END();
1337 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001338 }
1339 }
1340
1341 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001342 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001343 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001344 return result;
1345 }
1346
Jesse Halldc225072016-05-30 22:40:14 -07001347 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1348 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001349 return VK_SUCCESS;
1350}
1351
Jesse Halle1b12782015-11-30 11:27:32 -08001352VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001353void DestroySwapchainKHR(VkDevice device,
1354 VkSwapchainKHR swapchain_handle,
1355 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001356 ATRACE_CALL();
1357
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001358 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001359 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001360 if (!swapchain)
1361 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001362 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1363 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001364
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001365 if (swapchain->frame_timestamps_enabled) {
1366 native_window_enable_frame_timestamps(window, false);
1367 }
Jesse Halldc225072016-05-30 22:40:14 -07001368 for (uint32_t i = 0; i < swapchain->num_images; i++)
1369 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001370 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001371 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001372 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001373 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001374 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001375 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001376}
1377
Jesse Halle1b12782015-11-30 11:27:32 -08001378VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001379VkResult GetSwapchainImagesKHR(VkDevice,
1380 VkSwapchainKHR swapchain_handle,
1381 uint32_t* count,
1382 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001383 ATRACE_CALL();
1384
Jesse Halld7b994a2015-09-07 14:17:37 -07001385 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001386 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1387 "getting images for non-active swapchain 0x%" PRIx64
1388 "; only dequeued image handles are valid",
1389 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001390 VkResult result = VK_SUCCESS;
1391 if (images) {
1392 uint32_t n = swapchain.num_images;
1393 if (*count < swapchain.num_images) {
1394 n = *count;
1395 result = VK_INCOMPLETE;
1396 }
1397 for (uint32_t i = 0; i < n; i++)
1398 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001399 *count = n;
1400 } else {
1401 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001402 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001403 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001404}
1405
Jesse Halle1b12782015-11-30 11:27:32 -08001406VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001407VkResult AcquireNextImageKHR(VkDevice device,
1408 VkSwapchainKHR swapchain_handle,
1409 uint64_t timeout,
1410 VkSemaphore semaphore,
1411 VkFence vk_fence,
1412 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001413 ATRACE_CALL();
1414
Jesse Halld7b994a2015-09-07 14:17:37 -07001415 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001416 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001417 VkResult result;
1418 int err;
1419
Jesse Halldc225072016-05-30 22:40:14 -07001420 if (swapchain.surface.swapchain_handle != swapchain_handle)
1421 return VK_ERROR_OUT_OF_DATE_KHR;
1422
Jesse Halld7b994a2015-09-07 14:17:37 -07001423 ALOGW_IF(
1424 timeout != UINT64_MAX,
1425 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1426
Chris Forbesc88409c2017-03-30 19:47:37 +13001427 if (swapchain.shared) {
1428 // In shared mode, we keep the buffer dequeued all the time, so we don't
1429 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1430 // the semaphore and fence passed to us will be signalled.
1431 *image_index = 0;
1432 result = GetData(device).driver.AcquireImageANDROID(
1433 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1434 return result;
1435 }
1436
Jesse Halld7b994a2015-09-07 14:17:37 -07001437 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001438 int fence_fd;
1439 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001440 if (err != 0) {
1441 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1442 // errors and translate them to valid Vulkan result codes?
1443 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001444 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001445 }
1446
1447 uint32_t idx;
1448 for (idx = 0; idx < swapchain.num_images; idx++) {
1449 if (swapchain.images[idx].buffer.get() == buffer) {
1450 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001451 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001452 break;
1453 }
1454 }
1455 if (idx == swapchain.num_images) {
1456 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001457 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001458 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001459 }
1460
1461 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001462 if (fence_fd != -1) {
1463 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001464 if (fence_clone == -1) {
1465 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1466 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001467 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001468 }
1469 }
1470
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001471 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001472 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001473 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001474 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1475 // even if the call fails. We could close it ourselves on failure, but
1476 // that would create a race condition if the driver closes it on a
1477 // failure path: some other thread might create an fd with the same
1478 // number between the time the driver closes it and the time we close
1479 // it. We must assume one of: the driver *always* closes it even on
1480 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001481 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001482 swapchain.images[idx].dequeued = false;
1483 swapchain.images[idx].dequeue_fence = -1;
1484 return result;
1485 }
1486
1487 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001488 return VK_SUCCESS;
1489}
1490
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001491VKAPI_ATTR
1492VkResult AcquireNextImage2KHR(VkDevice device,
1493 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1494 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001495 ATRACE_CALL();
1496
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001497 // TODO: this should actually be the other way around and this function
1498 // should handle any additional structures that get passed in
1499 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1500 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1501 pAcquireInfo->fence, pImageIndex);
1502}
1503
Jesse Halldc225072016-05-30 22:40:14 -07001504static VkResult WorstPresentResult(VkResult a, VkResult b) {
1505 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1506 // (in spec version 1.0.14).
1507 static const VkResult kWorstToBest[] = {
1508 VK_ERROR_DEVICE_LOST,
1509 VK_ERROR_SURFACE_LOST_KHR,
1510 VK_ERROR_OUT_OF_DATE_KHR,
1511 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1512 VK_ERROR_OUT_OF_HOST_MEMORY,
1513 VK_SUBOPTIMAL_KHR,
1514 };
1515 for (auto result : kWorstToBest) {
1516 if (a == result || b == result)
1517 return result;
1518 }
1519 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1520 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1521 return a != VK_SUCCESS ? a : b;
1522}
1523
Jesse Halle1b12782015-11-30 11:27:32 -08001524VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001525VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001526 ATRACE_CALL();
1527
Jesse Halld7b994a2015-09-07 14:17:37 -07001528 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1529 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1530 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001531
Jesse Halldc225072016-05-30 22:40:14 -07001532 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001533 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001534 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001535
Ian Elliottcb351132016-12-13 10:30:40 -07001536 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001537 const VkPresentRegionsKHR* present_regions = nullptr;
1538 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001539 const VkPresentRegionsKHR* next =
1540 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1541 while (next) {
1542 switch (next->sType) {
1543 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1544 present_regions = next;
1545 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001546 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001547 present_times =
1548 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1549 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001550 default:
1551 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1552 next->sType);
1553 break;
1554 }
1555 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1556 }
1557 ALOGV_IF(
1558 present_regions &&
1559 present_regions->swapchainCount != present_info->swapchainCount,
1560 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001561 ALOGV_IF(present_times &&
1562 present_times->swapchainCount != present_info->swapchainCount,
1563 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1564 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001565 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001566 (present_regions) ? present_regions->pRegions : nullptr;
1567 const VkPresentTimeGOOGLE* times =
1568 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001569 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001570 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001571 uint32_t nrects = 0;
1572
Jesse Halld7b994a2015-09-07 14:17:37 -07001573 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1574 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001575 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001576 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001577 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001578 const VkPresentRegionKHR* region =
1579 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001580 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001581 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001582 VkResult result;
1583 int err;
1584
Jesse Halld7b994a2015-09-07 14:17:37 -07001585 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001586 result = dispatch.QueueSignalReleaseImageANDROID(
1587 queue, present_info->waitSemaphoreCount,
1588 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001589 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001590 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001591 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001592 }
1593
Jesse Halldc225072016-05-30 22:40:14 -07001594 if (swapchain.surface.swapchain_handle ==
1595 present_info->pSwapchains[sc]) {
1596 ANativeWindow* window = swapchain.surface.window.get();
1597 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001598 if (region) {
1599 // Process the incremental-present hint for this swapchain:
1600 uint32_t rcount = region->rectangleCount;
1601 if (rcount > nrects) {
1602 android_native_rect_t* new_rects =
1603 static_cast<android_native_rect_t*>(
1604 allocator->pfnReallocation(
1605 allocator->pUserData, rects,
1606 sizeof(android_native_rect_t) * rcount,
1607 alignof(android_native_rect_t),
1608 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1609 if (new_rects) {
1610 rects = new_rects;
1611 nrects = rcount;
1612 } else {
1613 rcount = 0; // Ignore the hint for this swapchain
1614 }
1615 }
1616 for (uint32_t r = 0; r < rcount; ++r) {
1617 if (region->pRectangles[r].layer > 0) {
1618 ALOGV(
1619 "vkQueuePresentKHR ignoring invalid layer "
1620 "(%u); using layer 0 instead",
1621 region->pRectangles[r].layer);
1622 }
1623 int x = region->pRectangles[r].offset.x;
1624 int y = region->pRectangles[r].offset.y;
1625 int width = static_cast<int>(
1626 region->pRectangles[r].extent.width);
1627 int height = static_cast<int>(
1628 region->pRectangles[r].extent.height);
1629 android_native_rect_t* cur_rect = &rects[r];
1630 cur_rect->left = x;
1631 cur_rect->top = y + height;
1632 cur_rect->right = x + width;
1633 cur_rect->bottom = y;
1634 }
1635 native_window_set_surface_damage(window, rects, rcount);
1636 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001637 if (time) {
1638 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001639 ALOGV(
1640 "Calling "
1641 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001642 native_window_enable_frame_timestamps(window, true);
1643 swapchain.frame_timestamps_enabled = true;
1644 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001645
1646 // Record the nativeFrameId so it can be later correlated to
1647 // this present.
1648 uint64_t nativeFrameId = 0;
1649 err = native_window_get_next_frame_id(
1650 window, &nativeFrameId);
1651 if (err != android::NO_ERROR) {
1652 ALOGE("Failed to get next native frame ID.");
1653 }
1654
1655 // Add a new timing record with the user's presentID and
1656 // the nativeFrameId.
1657 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1658 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001659 swapchain.timing.removeAt(0);
1660 }
1661 if (time->desiredPresentTime) {
1662 // Set the desiredPresentTime:
1663 ALOGV(
1664 "Calling "
1665 "native_window_set_buffers_timestamp(%" PRId64 ")",
1666 time->desiredPresentTime);
1667 native_window_set_buffers_timestamp(
1668 window,
1669 static_cast<int64_t>(time->desiredPresentTime));
1670 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001671 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001672
Jesse Halldc225072016-05-30 22:40:14 -07001673 err = window->queueBuffer(window, img.buffer.get(), fence);
1674 // queueBuffer always closes fence, even on error
1675 if (err != 0) {
1676 // TODO(jessehall): What now? We should probably cancel the
1677 // buffer, I guess?
1678 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1679 swapchain_result = WorstPresentResult(
1680 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1681 }
1682 if (img.dequeue_fence >= 0) {
1683 close(img.dequeue_fence);
1684 img.dequeue_fence = -1;
1685 }
1686 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001687
1688 // If the swapchain is in shared mode, immediately dequeue the
1689 // buffer so it can be presented again without an intervening
1690 // call to AcquireNextImageKHR. We expect to get the same buffer
1691 // back from every call to dequeueBuffer in this mode.
1692 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1693 ANativeWindowBuffer* buffer;
1694 int fence_fd;
1695 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1696 if (err != 0) {
1697 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1698 swapchain_result = WorstPresentResult(swapchain_result,
1699 VK_ERROR_SURFACE_LOST_KHR);
1700 }
1701 else if (img.buffer != buffer) {
1702 ALOGE("got wrong image back for shared swapchain");
1703 swapchain_result = WorstPresentResult(swapchain_result,
1704 VK_ERROR_SURFACE_LOST_KHR);
1705 }
1706 else {
1707 img.dequeue_fence = fence_fd;
1708 img.dequeued = true;
1709 }
1710 }
Jesse Halldc225072016-05-30 22:40:14 -07001711 }
1712 if (swapchain_result != VK_SUCCESS) {
1713 ReleaseSwapchainImage(device, window, fence, img);
1714 OrphanSwapchain(device, &swapchain);
1715 }
1716 } else {
1717 ReleaseSwapchainImage(device, nullptr, fence, img);
1718 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001719 }
1720
Jesse Halla9e57032015-11-30 01:03:10 -08001721 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001722 present_info->pResults[sc] = swapchain_result;
1723
1724 if (swapchain_result != final_result)
1725 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001726 }
Ian Elliottcb351132016-12-13 10:30:40 -07001727 if (rects) {
1728 allocator->pfnFree(allocator->pUserData, rects);
1729 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001730
1731 return final_result;
1732}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001733
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001734VKAPI_ATTR
1735VkResult GetRefreshCycleDurationGOOGLE(
1736 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001737 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001738 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001739 ATRACE_CALL();
1740
Ian Elliott62c48c92017-01-20 13:13:20 -07001741 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001742 VkResult result = VK_SUCCESS;
1743
Ian Elliott3568bfe2019-05-03 15:54:46 -06001744 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001745
1746 return result;
1747}
1748
1749VKAPI_ATTR
1750VkResult GetPastPresentationTimingGOOGLE(
1751 VkDevice,
1752 VkSwapchainKHR swapchain_handle,
1753 uint32_t* count,
1754 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001755 ATRACE_CALL();
1756
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001757 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1758 ANativeWindow* window = swapchain.surface.window.get();
1759 VkResult result = VK_SUCCESS;
1760
1761 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001762 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001763 native_window_enable_frame_timestamps(window, true);
1764 swapchain.frame_timestamps_enabled = true;
1765 }
1766
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001767 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001768 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1769 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001770 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001771 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001772 }
1773
1774 return result;
1775}
1776
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001777VKAPI_ATTR
1778VkResult GetSwapchainStatusKHR(
1779 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001780 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001781 ATRACE_CALL();
1782
Chris Forbes4e18ba82017-01-20 12:50:17 +13001783 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001784 VkResult result = VK_SUCCESS;
1785
Chris Forbes4e18ba82017-01-20 12:50:17 +13001786 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1787 return VK_ERROR_OUT_OF_DATE_KHR;
1788 }
1789
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001790 // TODO(chrisforbes): Implement this function properly
1791
1792 return result;
1793}
1794
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001795VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001796 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001797 uint32_t swapchainCount,
1798 const VkSwapchainKHR* pSwapchains,
1799 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001800 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001801
1802 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1803 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1804 if (!swapchain)
1805 continue;
1806
1807 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1808
1809 ANativeWindow* window = swapchain->surface.window.get();
1810
1811 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1812 const android_smpte2086_metadata smpteMetdata = {
1813 {vulkanMetadata.displayPrimaryRed.x,
1814 vulkanMetadata.displayPrimaryRed.y},
1815 {vulkanMetadata.displayPrimaryGreen.x,
1816 vulkanMetadata.displayPrimaryGreen.y},
1817 {vulkanMetadata.displayPrimaryBlue.x,
1818 vulkanMetadata.displayPrimaryBlue.y},
1819 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1820 vulkanMetadata.maxLuminance,
1821 vulkanMetadata.minLuminance};
1822 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1823
1824 const android_cta861_3_metadata cta8613Metadata = {
1825 vulkanMetadata.maxContentLightLevel,
1826 vulkanMetadata.maxFrameAverageLightLevel};
1827 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1828 }
1829
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001830 return;
1831}
1832
Yiwei Zhang0f475222019-04-11 19:38:00 -07001833static void InterceptBindImageMemory2(
1834 uint32_t bind_info_count,
1835 const VkBindImageMemoryInfo* bind_infos,
1836 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1837 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1838 out_native_buffers->clear();
1839 out_bind_infos->clear();
1840
1841 if (!bind_info_count)
1842 return;
1843
1844 std::unordered_set<uint32_t> intercepted_indexes;
1845
1846 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1847 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1848 bind_infos[idx].pNext);
1849 while (info &&
1850 info->sType !=
1851 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1852 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1853 info->pNext);
1854 }
1855
1856 if (!info)
1857 continue;
1858
1859 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1860 "swapchain handle must not be NULL");
1861 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1862 ALOG_ASSERT(
1863 info->imageIndex < swapchain->num_images,
1864 "imageIndex must be less than the number of images in swapchain");
1865
1866 ANativeWindowBuffer* buffer =
1867 swapchain->images[info->imageIndex].buffer.get();
1868 VkNativeBufferANDROID native_buffer = {
1869#pragma clang diagnostic push
1870#pragma clang diagnostic ignored "-Wold-style-cast"
1871 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1872#pragma clang diagnostic pop
1873 .pNext = bind_infos[idx].pNext,
1874 .handle = buffer->handle,
1875 .stride = buffer->stride,
1876 .format = buffer->format,
1877 .usage = int(buffer->usage),
1878 };
1879 // Reserve enough space to avoid letting re-allocation invalidate the
1880 // addresses of the elements inside.
1881 out_native_buffers->reserve(bind_info_count);
1882 out_native_buffers->emplace_back(native_buffer);
1883
1884 // Reserve the space now since we know how much is needed now.
1885 out_bind_infos->reserve(bind_info_count);
1886 out_bind_infos->emplace_back(bind_infos[idx]);
1887 out_bind_infos->back().pNext = &out_native_buffers->back();
1888
1889 intercepted_indexes.insert(idx);
1890 }
1891
1892 if (intercepted_indexes.empty())
1893 return;
1894
1895 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1896 if (intercepted_indexes.count(idx))
1897 continue;
1898 out_bind_infos->emplace_back(bind_infos[idx]);
1899 }
1900}
1901
Yiwei Zhang23143102019-04-10 18:24:05 -07001902VKAPI_ATTR
1903VkResult BindImageMemory2(VkDevice device,
1904 uint32_t bindInfoCount,
1905 const VkBindImageMemoryInfo* pBindInfos) {
1906 ATRACE_CALL();
1907
Yiwei Zhang0f475222019-04-11 19:38:00 -07001908 // out_native_buffers is for maintaining the lifecycle of the constructed
1909 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1910 std::vector<VkNativeBufferANDROID> out_native_buffers;
1911 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1912 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1913 &out_bind_infos);
1914 return GetData(device).driver.BindImageMemory2(
1915 device, bindInfoCount,
1916 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001917}
1918
1919VKAPI_ATTR
1920VkResult BindImageMemory2KHR(VkDevice device,
1921 uint32_t bindInfoCount,
1922 const VkBindImageMemoryInfo* pBindInfos) {
1923 ATRACE_CALL();
1924
Yiwei Zhang0f475222019-04-11 19:38:00 -07001925 std::vector<VkNativeBufferANDROID> out_native_buffers;
1926 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1927 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1928 &out_bind_infos);
1929 return GetData(device).driver.BindImageMemory2KHR(
1930 device, bindInfoCount,
1931 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001932}
1933
Chia-I Wu62262232016-03-26 07:06:44 +08001934} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001935} // namespace vulkan