blob: f1a73b1daa13e007741ac8aafea42f6ae1735bd2 [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 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800225
226 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700227 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700228 bool mailbox_mode;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700229 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700230 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300231 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700232
233 struct Image {
234 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
235 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800236 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700237 // The fence is only valid when the buffer is dequeued, and should be
238 // -1 any other time. When valid, we own the fd, and must ensure it is
239 // closed: either by closing it explicitly when queueing the buffer,
240 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
241 int dequeue_fence;
242 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800243 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700244
Brian Anderson1049d1d2016-12-16 17:25:57 -0800245 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700246};
247
248VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
249 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
250}
251
252Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800253 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700254}
255
Jesse Halldc225072016-05-30 22:40:14 -0700256void ReleaseSwapchainImage(VkDevice device,
257 ANativeWindow* window,
258 int release_fence,
259 Swapchain::Image& image) {
260 ALOG_ASSERT(release_fence == -1 || image.dequeued,
261 "ReleaseSwapchainImage: can't provide a release fence for "
262 "non-dequeued images");
263
264 if (image.dequeued) {
265 if (release_fence >= 0) {
266 // We get here from vkQueuePresentKHR. The application is
267 // responsible for creating an execution dependency chain from
268 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
269 // (release_fence), so we can drop the dequeue_fence here.
270 if (image.dequeue_fence >= 0)
271 close(image.dequeue_fence);
272 } else {
273 // We get here during swapchain destruction, or various serious
274 // error cases e.g. when we can't create the release_fence during
275 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
276 // have already signalled, since the swapchain images are supposed
277 // to be idle before the swapchain is destroyed. In error cases,
278 // there may be rendering in flight to the image, but since we
279 // weren't able to create a release_fence, waiting for the
280 // dequeue_fence is about the best we can do.
281 release_fence = image.dequeue_fence;
282 }
283 image.dequeue_fence = -1;
284
285 if (window) {
286 window->cancelBuffer(window, image.buffer.get(), release_fence);
287 } else {
288 if (release_fence >= 0) {
289 sync_wait(release_fence, -1 /* forever */);
290 close(release_fence);
291 }
292 }
293
294 image.dequeued = false;
295 }
296
297 if (image.image) {
298 GetData(device).driver.DestroyImage(device, image.image, nullptr);
299 image.image = VK_NULL_HANDLE;
300 }
301
302 image.buffer.clear();
303}
304
305void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
306 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
307 return;
Jesse Halldc225072016-05-30 22:40:14 -0700308 for (uint32_t i = 0; i < swapchain->num_images; i++) {
309 if (!swapchain->images[i].dequeued)
310 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
311 }
312 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700313 swapchain->timing.clear();
314}
315
316uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800317 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
318 return 0;
319 }
Ian Elliott8a977262017-01-19 09:05:58 -0700320
Brian Anderson1049d1d2016-12-16 17:25:57 -0800321 uint32_t num_ready = 0;
322 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
323 for (uint32_t i = 0; i < num_timings; i++) {
324 TimingInfo& ti = swapchain.timing.editItemAt(i);
325 if (ti.ready()) {
326 // This TimingInfo is ready to be reported to the user. Add it
327 // to the num_ready.
328 num_ready++;
329 continue;
330 }
331 // This TimingInfo is not yet ready to be reported to the user,
332 // and so we should look for any available timestamps that
333 // might make it ready.
334 int64_t desired_present_time = 0;
335 int64_t render_complete_time = 0;
336 int64_t composition_latch_time = 0;
337 int64_t actual_present_time = 0;
338 // Obtain timestamps:
339 int ret = native_window_get_frame_timestamps(
340 swapchain.surface.window.get(), ti.native_frame_id_,
341 &desired_present_time, &render_complete_time,
342 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700343 nullptr, //&first_composition_start_time,
344 nullptr, //&last_composition_start_time,
345 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800346 // TODO(ianelliott): Maybe ask if this one is
347 // supported, at startup time (since it may not be
348 // supported):
349 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700350 nullptr, //&dequeue_ready_time,
351 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800352
353 if (ret != android::NO_ERROR) {
354 continue;
355 }
356
357 // Record the timestamp(s) we received, and then see if this TimingInfo
358 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700359 ti.timestamp_desired_present_time_ = desired_present_time;
360 ti.timestamp_actual_present_time_ = actual_present_time;
361 ti.timestamp_render_complete_time_ = render_complete_time;
362 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800363
364 if (ti.ready()) {
365 // The TimingInfo has received enough timestamps, and should now
366 // use those timestamps to calculate the info that should be
367 // reported to the user:
368 ti.calculate(swapchain.refresh_duration);
369 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700370 }
371 }
372 return num_ready;
373}
374
375// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
376void copy_ready_timings(Swapchain& swapchain,
377 uint32_t* count,
378 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800379 if (swapchain.timing.empty()) {
380 *count = 0;
381 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700382 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800383
384 size_t last_ready = swapchain.timing.size() - 1;
385 while (!swapchain.timing[last_ready].ready()) {
386 if (last_ready == 0) {
387 *count = 0;
388 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700389 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800390 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700391 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800392
393 uint32_t num_copied = 0;
394 size_t num_to_remove = 0;
395 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
396 const TimingInfo& ti = swapchain.timing[i];
397 if (ti.ready()) {
398 ti.get_values(&timings[num_copied]);
399 num_copied++;
400 }
401 num_to_remove++;
402 }
403
404 // Discard old frames that aren't ready if newer frames are ready.
405 // We don't expect to get the timing info for those old frames.
406 swapchain.timing.removeItemsAt(0, num_to_remove);
407
Ian Elliott8a977262017-01-19 09:05:58 -0700408 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700409}
410
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700411android_pixel_format GetNativePixelFormat(VkFormat format) {
412 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
413 switch (format) {
414 case VK_FORMAT_R8G8B8A8_UNORM:
415 case VK_FORMAT_R8G8B8A8_SRGB:
416 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
417 break;
418 case VK_FORMAT_R5G6B5_UNORM_PACK16:
419 native_format = HAL_PIXEL_FORMAT_RGB_565;
420 break;
421 case VK_FORMAT_R16G16B16A16_SFLOAT:
422 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
423 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800424 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700425 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
426 break;
427 default:
428 ALOGV("unsupported swapchain format %d", format);
429 break;
430 }
431 return native_format;
432}
433
434android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
435 switch (colorspace) {
436 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
437 return HAL_DATASPACE_V0_SRGB;
438 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
439 return HAL_DATASPACE_DISPLAY_P3;
440 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
441 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600442 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
443 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700444 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
445 return HAL_DATASPACE_DCI_P3_LINEAR;
446 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
447 return HAL_DATASPACE_DCI_P3;
448 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
449 return HAL_DATASPACE_V0_SRGB_LINEAR;
450 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
451 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600452 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
453 return HAL_DATASPACE_BT2020_LINEAR;
454 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700455 return static_cast<android_dataspace>(
456 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
457 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600458 case VK_COLOR_SPACE_DOLBYVISION_EXT:
459 return static_cast<android_dataspace>(
460 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
461 HAL_DATASPACE_RANGE_FULL);
462 case VK_COLOR_SPACE_HDR10_HLG_EXT:
463 return static_cast<android_dataspace>(
464 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
465 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700466 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
467 return static_cast<android_dataspace>(
468 HAL_DATASPACE_STANDARD_ADOBE_RGB |
469 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
470 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
471 return HAL_DATASPACE_ADOBE_RGB;
472
473 // Pass through is intended to allow app to provide data that is passed
474 // to the display system without modification.
475 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
476 return HAL_DATASPACE_ARBITRARY;
477
478 default:
479 // This indicates that we don't know about the
480 // dataspace specified and we should indicate that
481 // it's unsupported
482 return HAL_DATASPACE_UNKNOWN;
483 }
484}
485
Jesse Halld7b994a2015-09-07 14:17:37 -0700486} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700487
Jesse Halle1b12782015-11-30 11:27:32 -0800488VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800489VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800490 VkInstance instance,
491 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
492 const VkAllocationCallbacks* allocator,
493 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800494 ATRACE_CALL();
495
Jesse Hall1f91d392015-12-11 16:28:44 -0800496 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800497 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800498 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
499 alignof(Surface),
500 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800501 if (!mem)
502 return VK_ERROR_OUT_OF_HOST_MEMORY;
503 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700504
Chia-I Wue8e689f2016-04-18 08:21:31 +0800505 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700506 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700507 int err = native_window_get_consumer_usage(surface->window.get(),
508 &surface->consumer_usage);
509 if (err != android::NO_ERROR) {
510 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
511 strerror(-err), err);
512 surface->~Surface();
513 allocator->pfnFree(allocator->pUserData, surface);
514 return VK_ERROR_INITIALIZATION_FAILED;
515 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700516
Jesse Hall1356b0d2015-11-23 17:24:58 -0800517 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
Yiwei Zhang6435b322018-05-08 11:12:17 -0700518 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800519 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
520 if (err != 0) {
521 // TODO(jessehall): Improve error reporting. Can we enumerate possible
522 // errors and translate them to valid Vulkan result codes?
523 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
524 err);
525 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800526 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700527 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800528 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700529
Jesse Hall1356b0d2015-11-23 17:24:58 -0800530 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700531 return VK_SUCCESS;
532}
533
Jesse Halle1b12782015-11-30 11:27:32 -0800534VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800535void DestroySurfaceKHR(VkInstance instance,
536 VkSurfaceKHR surface_handle,
537 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800538 ATRACE_CALL();
539
Jesse Hall1356b0d2015-11-23 17:24:58 -0800540 Surface* surface = SurfaceFromHandle(surface_handle);
541 if (!surface)
542 return;
543 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700544 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700545 "destroyed VkSurfaceKHR 0x%" PRIx64
546 " has active VkSwapchainKHR 0x%" PRIx64,
547 reinterpret_cast<uint64_t>(surface_handle),
548 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800549 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800550 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800551 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800552 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800553}
554
Jesse Halle1b12782015-11-30 11:27:32 -0800555VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800556VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
557 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700558 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800559 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800560 ATRACE_CALL();
561
Yiwei Zhang6435b322018-05-08 11:12:17 -0700562 const Surface* surface = SurfaceFromHandle(surface_handle);
563 if (!surface) {
564 return VK_ERROR_SURFACE_LOST_KHR;
565 }
566 const ANativeWindow* window = surface->window.get();
567
568 int query_value;
569 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
570 if (err != 0 || query_value < 0) {
571 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
572 strerror(-err), err, query_value);
573 return VK_ERROR_SURFACE_LOST_KHR;
574 }
575
576 android_pixel_format native_format =
577 static_cast<android_pixel_format>(query_value);
578
579 bool format_supported = false;
580 switch (native_format) {
581 case HAL_PIXEL_FORMAT_RGBA_8888:
582 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700583 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800584 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700585 format_supported = true;
586 break;
587 default:
588 break;
589 }
590
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700591 *supported = static_cast<VkBool32>(
592 format_supported || (surface->consumer_usage &
593 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
594 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700595
Jesse Halla6429252015-11-29 18:59:42 -0800596 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800597}
598
Jesse Halle1b12782015-11-30 11:27:32 -0800599VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800600VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800601 VkPhysicalDevice /*pdev*/,
602 VkSurfaceKHR surface,
603 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800604 ATRACE_CALL();
605
Jesse Halld7b994a2015-09-07 14:17:37 -0700606 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800607 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700608
609 int width, height;
610 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
611 if (err != 0) {
612 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
613 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700614 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700615 }
616 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
617 if (err != 0) {
618 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
619 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700620 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700621 }
622
Jesse Hall55bc0972016-02-23 16:43:29 -0800623 int transform_hint;
624 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
625 if (err != 0) {
626 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
627 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700628 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800629 }
630
Jesse Halld7b994a2015-09-07 14:17:37 -0700631 // TODO(jessehall): Figure out what the min/max values should be.
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800632 int max_buffer_count;
633 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
634 if (err != 0) {
635 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
636 strerror(-err), err);
637 return VK_ERROR_SURFACE_LOST_KHR;
638 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700639 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800640 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700641
Jesse Hallfe2662d2016-02-09 13:26:59 -0800642 capabilities->currentExtent =
643 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
644
Jesse Halld7b994a2015-09-07 14:17:37 -0700645 // TODO(jessehall): Figure out what the max extent should be. Maximum
646 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800647 capabilities->minImageExtent = VkExtent2D{1, 1};
648 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700649
Jesse Hallfe2662d2016-02-09 13:26:59 -0800650 capabilities->maxImageArrayLayers = 1;
651
Jesse Hall55bc0972016-02-23 16:43:29 -0800652 capabilities->supportedTransforms = kSupportedTransforms;
653 capabilities->currentTransform =
654 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700655
Jesse Hallfe2662d2016-02-09 13:26:59 -0800656 // On Android, window composition is a WindowManager property, not something
657 // associated with the bufferqueue. It can't be changed from here.
658 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700659
660 // TODO(jessehall): I think these are right, but haven't thought hard about
661 // it. Do we need to query the driver for support of any of these?
662 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700663 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
664 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800665 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800666 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
667 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
668 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700669 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
670
Jesse Hallb1352bc2015-09-04 16:12:33 -0700671 return VK_SUCCESS;
672}
673
Jesse Halle1b12782015-11-30 11:27:32 -0800674VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700675VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
676 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800677 uint32_t* count,
678 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800679 ATRACE_CALL();
680
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700681 const InstanceData& instance_data = GetData(pdev);
682
Jesse Hall1356b0d2015-11-23 17:24:58 -0800683 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
684 // a new gralloc method to query whether a (format, usage) pair is
685 // supported, and check that for each gralloc format that corresponds to a
686 // Vulkan format. Shorter term, just add a few more formats to the ones
687 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700688
689 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700690 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
691 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
692 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Ian Elliott94ace212019-02-20 14:05:29 -0700693 {VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
694 {VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700695 };
696 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700697 uint32_t total_num_formats = kNumFormats;
698
699 bool wide_color_support = false;
700 Surface& surface = *SurfaceFromHandle(surface_handle);
701 int err = native_window_get_wide_color_support(surface.window.get(),
702 &wide_color_support);
703 if (err) {
704 // Not allowed to return a more sensible error code, so do this
705 return VK_ERROR_OUT_OF_HOST_MEMORY;
706 }
707 ALOGV("wide_color_support is: %d", wide_color_support);
708 wide_color_support =
709 wide_color_support &&
710 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
711
712 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600713 {VK_FORMAT_R8G8B8A8_UNORM,
714 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
715 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700716 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700717 {VK_FORMAT_R16G16B16A16_SFLOAT,
718 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
719 {VK_FORMAT_R16G16B16A16_SFLOAT,
720 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800721 {VK_FORMAT_A2B10G10R10_UNORM_PACK32,
722 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700723 };
724 const uint32_t kNumWideColorFormats =
725 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
726 if (wide_color_support) {
727 total_num_formats += kNumWideColorFormats;
728 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700729
730 VkResult result = VK_SUCCESS;
731 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700732 uint32_t out_count = 0;
733 uint32_t transfer_count = 0;
734 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700735 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700736 transfer_count = std::min(*count, kNumFormats);
737 std::copy(kFormats, kFormats + transfer_count, formats);
738 out_count += transfer_count;
739 if (wide_color_support) {
740 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
741 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
742 formats + out_count);
743 out_count += transfer_count;
744 }
745 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700746 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700747 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700748 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700749 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700750}
751
Jesse Halle1b12782015-11-30 11:27:32 -0800752VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300753VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
754 VkPhysicalDevice physicalDevice,
755 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
756 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800757 ATRACE_CALL();
758
Chris Forbes2452cf72017-03-16 16:30:17 +1300759 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
760 physicalDevice, pSurfaceInfo->surface,
761 &pSurfaceCapabilities->surfaceCapabilities);
762
Chris Forbes06bc0092017-03-16 16:46:05 +1300763 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
764 while (caps->pNext) {
765 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
766
767 switch (caps->sType) {
768 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
769 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
770 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
771 caps);
772 // Claim same set of usage flags are supported for
773 // shared present modes as for other modes.
774 shared_caps->sharedPresentSupportedUsageFlags =
775 pSurfaceCapabilities->surfaceCapabilities
776 .supportedUsageFlags;
777 } break;
778
779 default:
780 // Ignore all other extension structs
781 break;
782 }
783 }
784
Chris Forbes2452cf72017-03-16 16:30:17 +1300785 return result;
786}
787
788VKAPI_ATTR
789VkResult GetPhysicalDeviceSurfaceFormats2KHR(
790 VkPhysicalDevice physicalDevice,
791 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
792 uint32_t* pSurfaceFormatCount,
793 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800794 ATRACE_CALL();
795
Chris Forbes2452cf72017-03-16 16:30:17 +1300796 if (!pSurfaceFormats) {
797 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
798 pSurfaceInfo->surface,
799 pSurfaceFormatCount, nullptr);
800 } else {
801 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
802 // after the call.
803 android::Vector<VkSurfaceFormatKHR> surface_formats;
804 surface_formats.resize(*pSurfaceFormatCount);
805 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
806 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
807 &surface_formats.editItemAt(0));
808
809 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
810 // marshal results individually due to stride difference.
811 // completely ignore any chained extension structs.
812 uint32_t formats_to_marshal = *pSurfaceFormatCount;
813 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
814 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
815 }
816 }
817
818 return result;
819 }
820}
821
822VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300823VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800824 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800825 uint32_t* count,
826 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800827 ATRACE_CALL();
828
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800829 int err;
830 int query_value;
831 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
832
833 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
834 if (err != 0 || query_value < 0) {
835 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
836 strerror(-err), err, query_value);
837 return VK_ERROR_SURFACE_LOST_KHR;
838 }
839 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
840
841 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
842 if (err != 0 || query_value < 0) {
843 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
844 strerror(-err), err, query_value);
845 return VK_ERROR_SURFACE_LOST_KHR;
846 }
847 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
848
Chris Forbese8d79a62017-02-22 12:49:18 +1300849 android::Vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800850 if (min_undequeued_buffers + 1 < max_buffer_count)
851 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300852 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
853
854 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
855 if (QueryPresentationProperties(pdev, &present_properties)) {
856 if (present_properties.sharedImage) {
857 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
858 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
859 }
860 }
861
862 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700863
864 VkResult result = VK_SUCCESS;
865 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300866 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700867 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300868 *count = std::min(*count, num_modes);
869 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700870 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300871 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700872 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700873 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700874}
875
Jesse Halle1b12782015-11-30 11:27:32 -0800876VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400877VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600878 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400879 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800880 ATRACE_CALL();
881
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400882 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
883 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
884 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
885 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
886 pDeviceGroupPresentCapabilities->sType);
887
888 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
889 sizeof(pDeviceGroupPresentCapabilities->presentMask));
890
891 // assume device group of size 1
892 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
893 pDeviceGroupPresentCapabilities->modes =
894 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
895
896 return VK_SUCCESS;
897}
898
899VKAPI_ATTR
900VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600901 VkDevice,
902 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400903 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800904 ATRACE_CALL();
905
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400906 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
907 return VK_SUCCESS;
908}
909
910VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600911VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400912 VkSurfaceKHR surface,
913 uint32_t* pRectCount,
914 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800915 ATRACE_CALL();
916
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400917 if (!pRects) {
918 *pRectCount = 1;
919 } else {
920 uint32_t count = std::min(*pRectCount, 1u);
921 bool incomplete = *pRectCount < 1;
922
923 *pRectCount = count;
924
925 if (incomplete) {
926 return VK_INCOMPLETE;
927 }
928
929 int err;
930 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
931
932 int width = 0, height = 0;
933 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
934 if (err != 0) {
935 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
936 strerror(-err), err);
937 }
938 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
939 if (err != 0) {
940 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
941 strerror(-err), err);
942 }
943
944 // TODO: Return something better than "whole window"
945 pRects[0].offset.x = 0;
946 pRects[0].offset.y = 0;
947 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
948 static_cast<uint32_t>(height)};
949 }
950 return VK_SUCCESS;
951}
952
953VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800954VkResult CreateSwapchainKHR(VkDevice device,
955 const VkSwapchainCreateInfoKHR* create_info,
956 const VkAllocationCallbacks* allocator,
957 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800958 ATRACE_CALL();
959
Jesse Halld7b994a2015-09-07 14:17:37 -0700960 int err;
961 VkResult result = VK_SUCCESS;
962
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700963 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
964 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
965 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
966 " oldSwapchain=0x%" PRIx64,
967 reinterpret_cast<uint64_t>(create_info->surface),
968 create_info->minImageCount, create_info->imageFormat,
969 create_info->imageColorSpace, create_info->imageExtent.width,
970 create_info->imageExtent.height, create_info->imageUsage,
971 create_info->preTransform, create_info->presentMode,
972 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
973
Jesse Hall1f91d392015-12-11 16:28:44 -0800974 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800975 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800976
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700977 android_pixel_format native_pixel_format =
978 GetNativePixelFormat(create_info->imageFormat);
979 android_dataspace native_dataspace =
980 GetNativeDataspace(create_info->imageColorSpace);
981 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
982 ALOGE(
983 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
984 "failed: Unsupported color space",
985 create_info->imageColorSpace);
986 return VK_ERROR_INITIALIZATION_FAILED;
987 }
988
Jesse Hall42a9eec2016-06-03 12:39:49 -0700989 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700990 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800991 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700992 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700993 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800994 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700995 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +1300996 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300997 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
998 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700999 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001000 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001001
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001002 Surface& surface = *SurfaceFromHandle(create_info->surface);
1003
Jesse Halldc225072016-05-30 22:40:14 -07001004 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001005 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001006 " because it already has active swapchain 0x%" PRIx64
1007 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1008 reinterpret_cast<uint64_t>(create_info->surface),
1009 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1010 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1011 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1012 }
1013 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1014 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1015
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001016 // -- Reset the native window --
1017 // The native window might have been used previously, and had its properties
1018 // changed from defaults. That will affect the answer we get for queries
1019 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1020 // attempt such queries.
1021
Jesse Halldc225072016-05-30 22:40:14 -07001022 // The native window only allows dequeueing all buffers before any have
1023 // been queued, since after that point at least one is assumed to be in
1024 // non-FREE state at any given time. Disconnecting and re-connecting
1025 // orphans the previous buffers, getting us back to the state where we can
1026 // dequeue all buffers.
1027 err = native_window_api_disconnect(surface.window.get(),
1028 NATIVE_WINDOW_API_EGL);
1029 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1030 strerror(-err), err);
1031 err =
1032 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1033 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1034 strerror(-err), err);
1035
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001036 err = native_window_set_buffer_count(surface.window.get(), 0);
1037 if (err != 0) {
1038 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1039 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001040 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001041 }
1042
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301043 int swap_interval =
1044 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1045 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001046 if (err != 0) {
1047 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1048 // errors and translate them to valid Vulkan result codes?
1049 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1050 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001051 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001052 }
1053
Chris Forbesb8042d22017-01-18 18:07:05 +13001054 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1055 if (err != 0) {
1056 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1057 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001058 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001059 }
1060
1061 err = native_window_set_auto_refresh(surface.window.get(), false);
1062 if (err != 0) {
1063 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1064 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001065 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001066 }
1067
Jesse Halld7b994a2015-09-07 14:17:37 -07001068 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001069
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001070 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001071
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001072 err = native_window_set_buffers_format(surface.window.get(),
1073 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001074 if (err != 0) {
1075 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1076 // errors and translate them to valid Vulkan result codes?
1077 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001078 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001079 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001080 }
1081 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001082 native_dataspace);
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_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001087 native_dataspace, 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
Jesse Hall3dd678a2016-01-08 21:52:01 -08001091 err = native_window_set_buffers_dimensions(
1092 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1093 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001094 if (err != 0) {
1095 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1096 // errors and translate them to valid Vulkan result codes?
1097 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1098 create_info->imageExtent.width, create_info->imageExtent.height,
1099 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001100 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001101 }
1102
Jesse Hall178b6962016-02-24 15:39:50 -08001103 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1104 // applied during rendering. native_window_set_transform() expects the
1105 // inverse: the transform the app is requesting that the compositor perform
1106 // during composition. With native windows, pre-transform works by rendering
1107 // with the same transform the compositor is applying (as in Vulkan), but
1108 // then requesting the inverse transform, so that when the compositor does
1109 // it's job the two transforms cancel each other out and the compositor ends
1110 // up applying an identity transform to the app's buffer.
1111 err = native_window_set_buffers_transform(
1112 surface.window.get(),
1113 InvertTransformToNative(create_info->preTransform));
1114 if (err != 0) {
1115 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1116 // errors and translate them to valid Vulkan result codes?
1117 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1118 InvertTransformToNative(create_info->preTransform),
1119 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001120 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001121 }
1122
Jesse Hallf64ca122015-11-03 16:11:10 -08001123 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001124 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001125 if (err != 0) {
1126 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1127 // errors and translate them to valid Vulkan result codes?
1128 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1129 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001130 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001131 }
1132
Chris Forbes97ef4612017-03-30 19:37:50 +13001133 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1134 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1135 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1136 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1137 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1138 if (err != 0) {
1139 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1140 return VK_ERROR_SURFACE_LOST_KHR;
1141 }
1142 }
1143
1144 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1145 err = native_window_set_auto_refresh(surface.window.get(), true);
1146 if (err != 0) {
1147 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1148 return VK_ERROR_SURFACE_LOST_KHR;
1149 }
1150 }
1151
Jesse Halle6080bf2016-02-28 20:58:50 -08001152 int query_value;
1153 err = surface.window->query(surface.window.get(),
1154 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1155 &query_value);
1156 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001157 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1158 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -08001159 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1160 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001161 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001162 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001163 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001164 uint32_t num_images =
Ian Elliotta3515f42019-05-15 14:11:33 -06001165 (swap_interval ? create_info->minImageCount
1166 : std::max(3u, create_info->minImageCount)) -
1167 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001168
1169 // Lower layer insists that we have at least two buffers. This is wasteful
1170 // and we'd like to relax it in the shared case, but not all the pieces are
1171 // in place for that to work yet. Note we only lie to the lower layer-- we
1172 // don't want to give the app back a swapchain with extra images (which they
1173 // can't actually use!).
1174 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001175 if (err != 0) {
1176 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1177 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001178 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1179 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001180 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001181 }
1182
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001183 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001184 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001185 uint64_t consumer_usage, producer_usage;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001186 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001187 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1188 device, create_info->imageFormat, create_info->imageUsage,
1189 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001190 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001191 if (result != VK_SUCCESS) {
1192 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001193 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001194 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001195 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001196 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001197 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001198 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001199 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001200 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001201 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001202 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001203 if (result != VK_SUCCESS) {
1204 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001205 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001206 }
Jesse Hall70f93352015-11-04 09:41:31 -08001207 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001208 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1209
1210 bool createProtectedSwapchain = false;
1211 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1212 createProtectedSwapchain = true;
1213 native_usage |= BufferUsage::PROTECTED;
1214 }
1215 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001216 if (err != 0) {
1217 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1218 // errors and translate them to valid Vulkan result codes?
1219 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001220 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001221 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001222
1223 // -- Allocate our Swapchain object --
1224 // After this point, we must deallocate the swapchain on error.
1225
Jesse Hall1f91d392015-12-11 16:28:44 -08001226 void* mem = allocator->pfnAllocation(allocator->pUserData,
1227 sizeof(Swapchain), alignof(Swapchain),
1228 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001229 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001230 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001231 Swapchain* swapchain =
1232 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001233
1234 // -- Dequeue all buffers and create a VkImage for each --
1235 // Any failures during or after this must cancel the dequeued buffers.
1236
Chris Forbesb56287a2017-01-12 14:28:58 +13001237 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1238#pragma clang diagnostic push
1239#pragma clang diagnostic ignored "-Wold-style-cast"
1240 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1241#pragma clang diagnostic pop
1242 .pNext = nullptr,
1243 .usage = swapchain_image_usage,
1244 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001245 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001246#pragma clang diagnostic push
1247#pragma clang diagnostic ignored "-Wold-style-cast"
1248 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1249#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001250 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001251 };
1252 VkImageCreateInfo image_create = {
1253 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1254 .pNext = &image_native_buffer,
1255 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001256 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001257 .extent = {0, 0, 1},
1258 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001259 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001260 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001261 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001262 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001263 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001264 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001265 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001266 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1267 };
1268
Jesse Halld7b994a2015-09-07 14:17:37 -07001269 for (uint32_t i = 0; i < num_images; i++) {
1270 Swapchain::Image& img = swapchain->images[i];
1271
1272 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001273 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1274 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001275 if (err != 0) {
1276 // TODO(jessehall): Improve error reporting. Can we enumerate
1277 // possible errors and translate them to valid Vulkan result codes?
1278 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001279 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001280 break;
1281 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001282 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001283 img.dequeued = true;
1284
1285 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001286 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1287 static_cast<uint32_t>(img.buffer->height),
1288 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001289 image_native_buffer.handle = img.buffer->handle;
1290 image_native_buffer.stride = img.buffer->stride;
1291 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001292 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001293 android_convertGralloc0To1Usage(int(img.buffer->usage),
1294 &image_native_buffer.usage2.producer,
1295 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001296
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001297 ATRACE_BEGIN("dispatch.CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001298 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001299 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001300 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001301 if (result != VK_SUCCESS) {
1302 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1303 break;
1304 }
1305 }
1306
1307 // -- Cancel all buffers, returning them to the queue --
1308 // If an error occurred before, also destroy the VkImage and release the
1309 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1310 //
1311 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1312 // but not the non-error path. Should refactor/unify.
Chris Forbes31b85c22018-05-29 15:03:28 -07001313 for (uint32_t i = 0; i < num_images; i++) {
1314 Swapchain::Image& img = swapchain->images[i];
1315 if (img.dequeued) {
1316 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001317 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1318 img.dequeue_fence);
1319 img.dequeue_fence = -1;
1320 img.dequeued = false;
1321 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001322 }
1323 if (result != VK_SUCCESS) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001324 if (img.image) {
1325 ATRACE_BEGIN("dispatch.DestroyImage");
Chris Forbes31b85c22018-05-29 15:03:28 -07001326 dispatch.DestroyImage(device, img.image, nullptr);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001327 ATRACE_END();
1328 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001329 }
1330 }
1331
1332 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001333 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001334 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001335 return result;
1336 }
1337
Jesse Halldc225072016-05-30 22:40:14 -07001338 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1339 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001340 return VK_SUCCESS;
1341}
1342
Jesse Halle1b12782015-11-30 11:27:32 -08001343VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001344void DestroySwapchainKHR(VkDevice device,
1345 VkSwapchainKHR swapchain_handle,
1346 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001347 ATRACE_CALL();
1348
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001349 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001350 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001351 if (!swapchain)
1352 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001353 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1354 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001355
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001356 if (swapchain->frame_timestamps_enabled) {
1357 native_window_enable_frame_timestamps(window, false);
1358 }
Jesse Halldc225072016-05-30 22:40:14 -07001359 for (uint32_t i = 0; i < swapchain->num_images; i++)
1360 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001361 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001362 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001363 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001364 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001365 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001366 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001367}
1368
Jesse Halle1b12782015-11-30 11:27:32 -08001369VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001370VkResult GetSwapchainImagesKHR(VkDevice,
1371 VkSwapchainKHR swapchain_handle,
1372 uint32_t* count,
1373 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001374 ATRACE_CALL();
1375
Jesse Halld7b994a2015-09-07 14:17:37 -07001376 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001377 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1378 "getting images for non-active swapchain 0x%" PRIx64
1379 "; only dequeued image handles are valid",
1380 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001381 VkResult result = VK_SUCCESS;
1382 if (images) {
1383 uint32_t n = swapchain.num_images;
1384 if (*count < swapchain.num_images) {
1385 n = *count;
1386 result = VK_INCOMPLETE;
1387 }
1388 for (uint32_t i = 0; i < n; i++)
1389 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001390 *count = n;
1391 } else {
1392 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001393 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001394 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001395}
1396
Jesse Halle1b12782015-11-30 11:27:32 -08001397VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001398VkResult AcquireNextImageKHR(VkDevice device,
1399 VkSwapchainKHR swapchain_handle,
1400 uint64_t timeout,
1401 VkSemaphore semaphore,
1402 VkFence vk_fence,
1403 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001404 ATRACE_CALL();
1405
Jesse Halld7b994a2015-09-07 14:17:37 -07001406 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001407 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001408 VkResult result;
1409 int err;
1410
Jesse Halldc225072016-05-30 22:40:14 -07001411 if (swapchain.surface.swapchain_handle != swapchain_handle)
1412 return VK_ERROR_OUT_OF_DATE_KHR;
1413
Jesse Halld7b994a2015-09-07 14:17:37 -07001414 ALOGW_IF(
1415 timeout != UINT64_MAX,
1416 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1417
Chris Forbesc88409c2017-03-30 19:47:37 +13001418 if (swapchain.shared) {
1419 // In shared mode, we keep the buffer dequeued all the time, so we don't
1420 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1421 // the semaphore and fence passed to us will be signalled.
1422 *image_index = 0;
1423 result = GetData(device).driver.AcquireImageANDROID(
1424 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1425 return result;
1426 }
1427
Jesse Halld7b994a2015-09-07 14:17:37 -07001428 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001429 int fence_fd;
1430 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001431 if (err != 0) {
1432 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1433 // errors and translate them to valid Vulkan result codes?
1434 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001435 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001436 }
1437
1438 uint32_t idx;
1439 for (idx = 0; idx < swapchain.num_images; idx++) {
1440 if (swapchain.images[idx].buffer.get() == buffer) {
1441 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001442 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001443 break;
1444 }
1445 }
1446 if (idx == swapchain.num_images) {
1447 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001448 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001449 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001450 }
1451
1452 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001453 if (fence_fd != -1) {
1454 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001455 if (fence_clone == -1) {
1456 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1457 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001458 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001459 }
1460 }
1461
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001462 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001463 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001464 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001465 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1466 // even if the call fails. We could close it ourselves on failure, but
1467 // that would create a race condition if the driver closes it on a
1468 // failure path: some other thread might create an fd with the same
1469 // number between the time the driver closes it and the time we close
1470 // it. We must assume one of: the driver *always* closes it even on
1471 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001472 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001473 swapchain.images[idx].dequeued = false;
1474 swapchain.images[idx].dequeue_fence = -1;
1475 return result;
1476 }
1477
1478 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001479 return VK_SUCCESS;
1480}
1481
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001482VKAPI_ATTR
1483VkResult AcquireNextImage2KHR(VkDevice device,
1484 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1485 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001486 ATRACE_CALL();
1487
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001488 // TODO: this should actually be the other way around and this function
1489 // should handle any additional structures that get passed in
1490 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1491 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1492 pAcquireInfo->fence, pImageIndex);
1493}
1494
Jesse Halldc225072016-05-30 22:40:14 -07001495static VkResult WorstPresentResult(VkResult a, VkResult b) {
1496 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1497 // (in spec version 1.0.14).
1498 static const VkResult kWorstToBest[] = {
1499 VK_ERROR_DEVICE_LOST,
1500 VK_ERROR_SURFACE_LOST_KHR,
1501 VK_ERROR_OUT_OF_DATE_KHR,
1502 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1503 VK_ERROR_OUT_OF_HOST_MEMORY,
1504 VK_SUBOPTIMAL_KHR,
1505 };
1506 for (auto result : kWorstToBest) {
1507 if (a == result || b == result)
1508 return result;
1509 }
1510 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1511 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1512 return a != VK_SUCCESS ? a : b;
1513}
1514
Jesse Halle1b12782015-11-30 11:27:32 -08001515VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001516VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001517 ATRACE_CALL();
1518
Jesse Halld7b994a2015-09-07 14:17:37 -07001519 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1520 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1521 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001522
Jesse Halldc225072016-05-30 22:40:14 -07001523 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001524 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001525 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001526
Ian Elliottcb351132016-12-13 10:30:40 -07001527 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001528 const VkPresentRegionsKHR* present_regions = nullptr;
1529 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001530 const VkPresentRegionsKHR* next =
1531 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1532 while (next) {
1533 switch (next->sType) {
1534 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1535 present_regions = next;
1536 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001537 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001538 present_times =
1539 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1540 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001541 default:
1542 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1543 next->sType);
1544 break;
1545 }
1546 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1547 }
1548 ALOGV_IF(
1549 present_regions &&
1550 present_regions->swapchainCount != present_info->swapchainCount,
1551 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001552 ALOGV_IF(present_times &&
1553 present_times->swapchainCount != present_info->swapchainCount,
1554 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1555 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001556 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001557 (present_regions) ? present_regions->pRegions : nullptr;
1558 const VkPresentTimeGOOGLE* times =
1559 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001560 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001561 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001562 uint32_t nrects = 0;
1563
Jesse Halld7b994a2015-09-07 14:17:37 -07001564 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1565 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001566 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001567 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001568 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001569 const VkPresentRegionKHR* region =
1570 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001571 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001572 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001573 VkResult result;
1574 int err;
1575
Jesse Halld7b994a2015-09-07 14:17:37 -07001576 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001577 result = dispatch.QueueSignalReleaseImageANDROID(
1578 queue, present_info->waitSemaphoreCount,
1579 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001580 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001581 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001582 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001583 }
1584
Jesse Halldc225072016-05-30 22:40:14 -07001585 if (swapchain.surface.swapchain_handle ==
1586 present_info->pSwapchains[sc]) {
1587 ANativeWindow* window = swapchain.surface.window.get();
1588 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001589 if (region) {
1590 // Process the incremental-present hint for this swapchain:
1591 uint32_t rcount = region->rectangleCount;
1592 if (rcount > nrects) {
1593 android_native_rect_t* new_rects =
1594 static_cast<android_native_rect_t*>(
1595 allocator->pfnReallocation(
1596 allocator->pUserData, rects,
1597 sizeof(android_native_rect_t) * rcount,
1598 alignof(android_native_rect_t),
1599 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1600 if (new_rects) {
1601 rects = new_rects;
1602 nrects = rcount;
1603 } else {
1604 rcount = 0; // Ignore the hint for this swapchain
1605 }
1606 }
1607 for (uint32_t r = 0; r < rcount; ++r) {
1608 if (region->pRectangles[r].layer > 0) {
1609 ALOGV(
1610 "vkQueuePresentKHR ignoring invalid layer "
1611 "(%u); using layer 0 instead",
1612 region->pRectangles[r].layer);
1613 }
1614 int x = region->pRectangles[r].offset.x;
1615 int y = region->pRectangles[r].offset.y;
1616 int width = static_cast<int>(
1617 region->pRectangles[r].extent.width);
1618 int height = static_cast<int>(
1619 region->pRectangles[r].extent.height);
1620 android_native_rect_t* cur_rect = &rects[r];
1621 cur_rect->left = x;
1622 cur_rect->top = y + height;
1623 cur_rect->right = x + width;
1624 cur_rect->bottom = y;
1625 }
1626 native_window_set_surface_damage(window, rects, rcount);
1627 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001628 if (time) {
1629 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001630 ALOGV(
1631 "Calling "
1632 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001633 native_window_enable_frame_timestamps(window, true);
1634 swapchain.frame_timestamps_enabled = true;
1635 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001636
1637 // Record the nativeFrameId so it can be later correlated to
1638 // this present.
1639 uint64_t nativeFrameId = 0;
1640 err = native_window_get_next_frame_id(
1641 window, &nativeFrameId);
1642 if (err != android::NO_ERROR) {
1643 ALOGE("Failed to get next native frame ID.");
1644 }
1645
1646 // Add a new timing record with the user's presentID and
1647 // the nativeFrameId.
1648 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1649 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001650 swapchain.timing.removeAt(0);
1651 }
1652 if (time->desiredPresentTime) {
1653 // Set the desiredPresentTime:
1654 ALOGV(
1655 "Calling "
1656 "native_window_set_buffers_timestamp(%" PRId64 ")",
1657 time->desiredPresentTime);
1658 native_window_set_buffers_timestamp(
1659 window,
1660 static_cast<int64_t>(time->desiredPresentTime));
1661 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001662 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001663
Jesse Halldc225072016-05-30 22:40:14 -07001664 err = window->queueBuffer(window, img.buffer.get(), fence);
1665 // queueBuffer always closes fence, even on error
1666 if (err != 0) {
1667 // TODO(jessehall): What now? We should probably cancel the
1668 // buffer, I guess?
1669 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1670 swapchain_result = WorstPresentResult(
1671 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1672 }
1673 if (img.dequeue_fence >= 0) {
1674 close(img.dequeue_fence);
1675 img.dequeue_fence = -1;
1676 }
1677 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001678
1679 // If the swapchain is in shared mode, immediately dequeue the
1680 // buffer so it can be presented again without an intervening
1681 // call to AcquireNextImageKHR. We expect to get the same buffer
1682 // back from every call to dequeueBuffer in this mode.
1683 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1684 ANativeWindowBuffer* buffer;
1685 int fence_fd;
1686 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1687 if (err != 0) {
1688 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1689 swapchain_result = WorstPresentResult(swapchain_result,
1690 VK_ERROR_SURFACE_LOST_KHR);
1691 }
1692 else if (img.buffer != buffer) {
1693 ALOGE("got wrong image back for shared swapchain");
1694 swapchain_result = WorstPresentResult(swapchain_result,
1695 VK_ERROR_SURFACE_LOST_KHR);
1696 }
1697 else {
1698 img.dequeue_fence = fence_fd;
1699 img.dequeued = true;
1700 }
1701 }
Jesse Halldc225072016-05-30 22:40:14 -07001702 }
1703 if (swapchain_result != VK_SUCCESS) {
1704 ReleaseSwapchainImage(device, window, fence, img);
1705 OrphanSwapchain(device, &swapchain);
1706 }
1707 } else {
1708 ReleaseSwapchainImage(device, nullptr, fence, img);
1709 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001710 }
1711
Jesse Halla9e57032015-11-30 01:03:10 -08001712 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001713 present_info->pResults[sc] = swapchain_result;
1714
1715 if (swapchain_result != final_result)
1716 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001717 }
Ian Elliottcb351132016-12-13 10:30:40 -07001718 if (rects) {
1719 allocator->pfnFree(allocator->pUserData, rects);
1720 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001721
1722 return final_result;
1723}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001724
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001725VKAPI_ATTR
1726VkResult GetRefreshCycleDurationGOOGLE(
1727 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001728 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001729 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001730 ATRACE_CALL();
1731
Ian Elliott62c48c92017-01-20 13:13:20 -07001732 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001733 VkResult result = VK_SUCCESS;
1734
Brian Andersondc96fdf2017-03-20 16:54:25 -07001735 pDisplayTimingProperties->refreshDuration =
1736 static_cast<uint64_t>(swapchain.refresh_duration);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001737
1738 return result;
1739}
1740
1741VKAPI_ATTR
1742VkResult GetPastPresentationTimingGOOGLE(
1743 VkDevice,
1744 VkSwapchainKHR swapchain_handle,
1745 uint32_t* count,
1746 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001747 ATRACE_CALL();
1748
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001749 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1750 ANativeWindow* window = swapchain.surface.window.get();
1751 VkResult result = VK_SUCCESS;
1752
1753 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001754 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001755 native_window_enable_frame_timestamps(window, true);
1756 swapchain.frame_timestamps_enabled = true;
1757 }
1758
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001759 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001760 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1761 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001762 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001763 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001764 }
1765
1766 return result;
1767}
1768
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001769VKAPI_ATTR
1770VkResult GetSwapchainStatusKHR(
1771 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001772 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001773 ATRACE_CALL();
1774
Chris Forbes4e18ba82017-01-20 12:50:17 +13001775 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001776 VkResult result = VK_SUCCESS;
1777
Chris Forbes4e18ba82017-01-20 12:50:17 +13001778 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1779 return VK_ERROR_OUT_OF_DATE_KHR;
1780 }
1781
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001782 // TODO(chrisforbes): Implement this function properly
1783
1784 return result;
1785}
1786
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001787VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001788 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001789 uint32_t swapchainCount,
1790 const VkSwapchainKHR* pSwapchains,
1791 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001792 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001793
1794 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1795 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1796 if (!swapchain)
1797 continue;
1798
1799 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1800
1801 ANativeWindow* window = swapchain->surface.window.get();
1802
1803 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1804 const android_smpte2086_metadata smpteMetdata = {
1805 {vulkanMetadata.displayPrimaryRed.x,
1806 vulkanMetadata.displayPrimaryRed.y},
1807 {vulkanMetadata.displayPrimaryGreen.x,
1808 vulkanMetadata.displayPrimaryGreen.y},
1809 {vulkanMetadata.displayPrimaryBlue.x,
1810 vulkanMetadata.displayPrimaryBlue.y},
1811 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1812 vulkanMetadata.maxLuminance,
1813 vulkanMetadata.minLuminance};
1814 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1815
1816 const android_cta861_3_metadata cta8613Metadata = {
1817 vulkanMetadata.maxContentLightLevel,
1818 vulkanMetadata.maxFrameAverageLightLevel};
1819 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1820 }
1821
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001822 return;
1823}
1824
Yiwei Zhang0f475222019-04-11 19:38:00 -07001825static void InterceptBindImageMemory2(
1826 uint32_t bind_info_count,
1827 const VkBindImageMemoryInfo* bind_infos,
1828 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1829 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1830 out_native_buffers->clear();
1831 out_bind_infos->clear();
1832
1833 if (!bind_info_count)
1834 return;
1835
1836 std::unordered_set<uint32_t> intercepted_indexes;
1837
1838 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1839 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1840 bind_infos[idx].pNext);
1841 while (info &&
1842 info->sType !=
1843 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1844 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1845 info->pNext);
1846 }
1847
1848 if (!info)
1849 continue;
1850
1851 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1852 "swapchain handle must not be NULL");
1853 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1854 ALOG_ASSERT(
1855 info->imageIndex < swapchain->num_images,
1856 "imageIndex must be less than the number of images in swapchain");
1857
1858 ANativeWindowBuffer* buffer =
1859 swapchain->images[info->imageIndex].buffer.get();
1860 VkNativeBufferANDROID native_buffer = {
1861#pragma clang diagnostic push
1862#pragma clang diagnostic ignored "-Wold-style-cast"
1863 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1864#pragma clang diagnostic pop
1865 .pNext = bind_infos[idx].pNext,
1866 .handle = buffer->handle,
1867 .stride = buffer->stride,
1868 .format = buffer->format,
1869 .usage = int(buffer->usage),
1870 };
1871 // Reserve enough space to avoid letting re-allocation invalidate the
1872 // addresses of the elements inside.
1873 out_native_buffers->reserve(bind_info_count);
1874 out_native_buffers->emplace_back(native_buffer);
1875
1876 // Reserve the space now since we know how much is needed now.
1877 out_bind_infos->reserve(bind_info_count);
1878 out_bind_infos->emplace_back(bind_infos[idx]);
1879 out_bind_infos->back().pNext = &out_native_buffers->back();
1880
1881 intercepted_indexes.insert(idx);
1882 }
1883
1884 if (intercepted_indexes.empty())
1885 return;
1886
1887 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1888 if (intercepted_indexes.count(idx))
1889 continue;
1890 out_bind_infos->emplace_back(bind_infos[idx]);
1891 }
1892}
1893
Yiwei Zhang23143102019-04-10 18:24:05 -07001894VKAPI_ATTR
1895VkResult BindImageMemory2(VkDevice device,
1896 uint32_t bindInfoCount,
1897 const VkBindImageMemoryInfo* pBindInfos) {
1898 ATRACE_CALL();
1899
Yiwei Zhang0f475222019-04-11 19:38:00 -07001900 // out_native_buffers is for maintaining the lifecycle of the constructed
1901 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1902 std::vector<VkNativeBufferANDROID> out_native_buffers;
1903 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1904 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1905 &out_bind_infos);
1906 return GetData(device).driver.BindImageMemory2(
1907 device, bindInfoCount,
1908 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001909}
1910
1911VKAPI_ATTR
1912VkResult BindImageMemory2KHR(VkDevice device,
1913 uint32_t bindInfoCount,
1914 const VkBindImageMemoryInfo* pBindInfos) {
1915 ATRACE_CALL();
1916
Yiwei Zhang0f475222019-04-11 19:38:00 -07001917 std::vector<VkNativeBufferANDROID> out_native_buffers;
1918 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1919 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1920 &out_bind_infos);
1921 return GetData(device).driver.BindImageMemory2KHR(
1922 device, bindInfoCount,
1923 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001924}
1925
Chia-I Wu62262232016-03-26 07:06:44 +08001926} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001927} // namespace vulkan