blob: bc00190779975b6243d8bf414a4f7a336e4af3a8 [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 =
1165 (create_info->minImageCount - 1) + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001166
1167 // Lower layer insists that we have at least two buffers. This is wasteful
1168 // and we'd like to relax it in the shared case, but not all the pieces are
1169 // in place for that to work yet. Note we only lie to the lower layer-- we
1170 // don't want to give the app back a swapchain with extra images (which they
1171 // can't actually use!).
1172 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001173 if (err != 0) {
1174 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1175 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001176 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1177 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001178 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001179 }
1180
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001181 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001182 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001183 uint64_t consumer_usage, producer_usage;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001184 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001185 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1186 device, create_info->imageFormat, create_info->imageUsage,
1187 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001188 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001189 if (result != VK_SUCCESS) {
1190 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001191 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001192 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001193 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001194 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001195 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001196 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001197 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001198 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001199 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001200 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001201 if (result != VK_SUCCESS) {
1202 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001203 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001204 }
Jesse Hall70f93352015-11-04 09:41:31 -08001205 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001206 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1207
1208 bool createProtectedSwapchain = false;
1209 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1210 createProtectedSwapchain = true;
1211 native_usage |= BufferUsage::PROTECTED;
1212 }
1213 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001214 if (err != 0) {
1215 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1216 // errors and translate them to valid Vulkan result codes?
1217 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001218 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001219 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001220
1221 // -- Allocate our Swapchain object --
1222 // After this point, we must deallocate the swapchain on error.
1223
Jesse Hall1f91d392015-12-11 16:28:44 -08001224 void* mem = allocator->pfnAllocation(allocator->pUserData,
1225 sizeof(Swapchain), alignof(Swapchain),
1226 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001227 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001228 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001229 Swapchain* swapchain =
1230 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001231
1232 // -- Dequeue all buffers and create a VkImage for each --
1233 // Any failures during or after this must cancel the dequeued buffers.
1234
Chris Forbesb56287a2017-01-12 14:28:58 +13001235 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1236#pragma clang diagnostic push
1237#pragma clang diagnostic ignored "-Wold-style-cast"
1238 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1239#pragma clang diagnostic pop
1240 .pNext = nullptr,
1241 .usage = swapchain_image_usage,
1242 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001243 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001244#pragma clang diagnostic push
1245#pragma clang diagnostic ignored "-Wold-style-cast"
1246 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1247#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001248 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001249 };
1250 VkImageCreateInfo image_create = {
1251 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1252 .pNext = &image_native_buffer,
1253 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001254 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001255 .extent = {0, 0, 1},
1256 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001257 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001258 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001259 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001260 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001261 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001262 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001263 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001264 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1265 };
1266
Jesse Halld7b994a2015-09-07 14:17:37 -07001267 for (uint32_t i = 0; i < num_images; i++) {
1268 Swapchain::Image& img = swapchain->images[i];
1269
1270 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001271 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1272 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001273 if (err != 0) {
1274 // TODO(jessehall): Improve error reporting. Can we enumerate
1275 // possible errors and translate them to valid Vulkan result codes?
1276 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001277 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001278 break;
1279 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001280 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001281 img.dequeued = true;
1282
1283 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001284 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1285 static_cast<uint32_t>(img.buffer->height),
1286 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001287 image_native_buffer.handle = img.buffer->handle;
1288 image_native_buffer.stride = img.buffer->stride;
1289 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001290 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001291 android_convertGralloc0To1Usage(int(img.buffer->usage),
1292 &image_native_buffer.usage2.producer,
1293 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001294
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001295 ATRACE_BEGIN("dispatch.CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001296 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001297 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001298 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001299 if (result != VK_SUCCESS) {
1300 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1301 break;
1302 }
1303 }
1304
1305 // -- Cancel all buffers, returning them to the queue --
1306 // If an error occurred before, also destroy the VkImage and release the
1307 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1308 //
1309 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1310 // but not the non-error path. Should refactor/unify.
Chris Forbes31b85c22018-05-29 15:03:28 -07001311 for (uint32_t i = 0; i < num_images; i++) {
1312 Swapchain::Image& img = swapchain->images[i];
1313 if (img.dequeued) {
1314 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001315 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1316 img.dequeue_fence);
1317 img.dequeue_fence = -1;
1318 img.dequeued = false;
1319 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001320 }
1321 if (result != VK_SUCCESS) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001322 if (img.image) {
1323 ATRACE_BEGIN("dispatch.DestroyImage");
Chris Forbes31b85c22018-05-29 15:03:28 -07001324 dispatch.DestroyImage(device, img.image, nullptr);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001325 ATRACE_END();
1326 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001327 }
1328 }
1329
1330 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001331 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001332 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001333 return result;
1334 }
1335
Jesse Halldc225072016-05-30 22:40:14 -07001336 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1337 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001338 return VK_SUCCESS;
1339}
1340
Jesse Halle1b12782015-11-30 11:27:32 -08001341VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001342void DestroySwapchainKHR(VkDevice device,
1343 VkSwapchainKHR swapchain_handle,
1344 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001345 ATRACE_CALL();
1346
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001347 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001348 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001349 if (!swapchain)
1350 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001351 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1352 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001353
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001354 if (swapchain->frame_timestamps_enabled) {
1355 native_window_enable_frame_timestamps(window, false);
1356 }
Jesse Halldc225072016-05-30 22:40:14 -07001357 for (uint32_t i = 0; i < swapchain->num_images; i++)
1358 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001359 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001360 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001361 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001362 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001363 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001364 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001365}
1366
Jesse Halle1b12782015-11-30 11:27:32 -08001367VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001368VkResult GetSwapchainImagesKHR(VkDevice,
1369 VkSwapchainKHR swapchain_handle,
1370 uint32_t* count,
1371 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001372 ATRACE_CALL();
1373
Jesse Halld7b994a2015-09-07 14:17:37 -07001374 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001375 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1376 "getting images for non-active swapchain 0x%" PRIx64
1377 "; only dequeued image handles are valid",
1378 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001379 VkResult result = VK_SUCCESS;
1380 if (images) {
1381 uint32_t n = swapchain.num_images;
1382 if (*count < swapchain.num_images) {
1383 n = *count;
1384 result = VK_INCOMPLETE;
1385 }
1386 for (uint32_t i = 0; i < n; i++)
1387 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001388 *count = n;
1389 } else {
1390 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001391 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001392 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001393}
1394
Jesse Halle1b12782015-11-30 11:27:32 -08001395VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001396VkResult AcquireNextImageKHR(VkDevice device,
1397 VkSwapchainKHR swapchain_handle,
1398 uint64_t timeout,
1399 VkSemaphore semaphore,
1400 VkFence vk_fence,
1401 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001402 ATRACE_CALL();
1403
Jesse Halld7b994a2015-09-07 14:17:37 -07001404 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001405 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001406 VkResult result;
1407 int err;
1408
Jesse Halldc225072016-05-30 22:40:14 -07001409 if (swapchain.surface.swapchain_handle != swapchain_handle)
1410 return VK_ERROR_OUT_OF_DATE_KHR;
1411
Jesse Halld7b994a2015-09-07 14:17:37 -07001412 ALOGW_IF(
1413 timeout != UINT64_MAX,
1414 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1415
Chris Forbesc88409c2017-03-30 19:47:37 +13001416 if (swapchain.shared) {
1417 // In shared mode, we keep the buffer dequeued all the time, so we don't
1418 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1419 // the semaphore and fence passed to us will be signalled.
1420 *image_index = 0;
1421 result = GetData(device).driver.AcquireImageANDROID(
1422 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1423 return result;
1424 }
1425
Jesse Halld7b994a2015-09-07 14:17:37 -07001426 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001427 int fence_fd;
1428 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001429 if (err != 0) {
1430 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1431 // errors and translate them to valid Vulkan result codes?
1432 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001433 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001434 }
1435
1436 uint32_t idx;
1437 for (idx = 0; idx < swapchain.num_images; idx++) {
1438 if (swapchain.images[idx].buffer.get() == buffer) {
1439 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001440 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001441 break;
1442 }
1443 }
1444 if (idx == swapchain.num_images) {
1445 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001446 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001447 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001448 }
1449
1450 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001451 if (fence_fd != -1) {
1452 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001453 if (fence_clone == -1) {
1454 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1455 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001456 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001457 }
1458 }
1459
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001460 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001461 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001462 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001463 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1464 // even if the call fails. We could close it ourselves on failure, but
1465 // that would create a race condition if the driver closes it on a
1466 // failure path: some other thread might create an fd with the same
1467 // number between the time the driver closes it and the time we close
1468 // it. We must assume one of: the driver *always* closes it even on
1469 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001470 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001471 swapchain.images[idx].dequeued = false;
1472 swapchain.images[idx].dequeue_fence = -1;
1473 return result;
1474 }
1475
1476 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001477 return VK_SUCCESS;
1478}
1479
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001480VKAPI_ATTR
1481VkResult AcquireNextImage2KHR(VkDevice device,
1482 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1483 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001484 ATRACE_CALL();
1485
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001486 // TODO: this should actually be the other way around and this function
1487 // should handle any additional structures that get passed in
1488 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1489 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1490 pAcquireInfo->fence, pImageIndex);
1491}
1492
Jesse Halldc225072016-05-30 22:40:14 -07001493static VkResult WorstPresentResult(VkResult a, VkResult b) {
1494 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1495 // (in spec version 1.0.14).
1496 static const VkResult kWorstToBest[] = {
1497 VK_ERROR_DEVICE_LOST,
1498 VK_ERROR_SURFACE_LOST_KHR,
1499 VK_ERROR_OUT_OF_DATE_KHR,
1500 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1501 VK_ERROR_OUT_OF_HOST_MEMORY,
1502 VK_SUBOPTIMAL_KHR,
1503 };
1504 for (auto result : kWorstToBest) {
1505 if (a == result || b == result)
1506 return result;
1507 }
1508 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1509 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1510 return a != VK_SUCCESS ? a : b;
1511}
1512
Jesse Halle1b12782015-11-30 11:27:32 -08001513VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001514VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001515 ATRACE_CALL();
1516
Jesse Halld7b994a2015-09-07 14:17:37 -07001517 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1518 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1519 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001520
Jesse Halldc225072016-05-30 22:40:14 -07001521 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001522 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001523 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001524
Ian Elliottcb351132016-12-13 10:30:40 -07001525 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001526 const VkPresentRegionsKHR* present_regions = nullptr;
1527 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001528 const VkPresentRegionsKHR* next =
1529 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1530 while (next) {
1531 switch (next->sType) {
1532 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1533 present_regions = next;
1534 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001535 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001536 present_times =
1537 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1538 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001539 default:
1540 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1541 next->sType);
1542 break;
1543 }
1544 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1545 }
1546 ALOGV_IF(
1547 present_regions &&
1548 present_regions->swapchainCount != present_info->swapchainCount,
1549 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001550 ALOGV_IF(present_times &&
1551 present_times->swapchainCount != present_info->swapchainCount,
1552 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1553 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001554 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001555 (present_regions) ? present_regions->pRegions : nullptr;
1556 const VkPresentTimeGOOGLE* times =
1557 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001558 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001559 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001560 uint32_t nrects = 0;
1561
Jesse Halld7b994a2015-09-07 14:17:37 -07001562 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1563 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001564 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001565 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001566 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001567 const VkPresentRegionKHR* region =
1568 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001569 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001570 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001571 VkResult result;
1572 int err;
1573
Jesse Halld7b994a2015-09-07 14:17:37 -07001574 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001575 result = dispatch.QueueSignalReleaseImageANDROID(
1576 queue, present_info->waitSemaphoreCount,
1577 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001578 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001579 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001580 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001581 }
1582
Jesse Halldc225072016-05-30 22:40:14 -07001583 if (swapchain.surface.swapchain_handle ==
1584 present_info->pSwapchains[sc]) {
1585 ANativeWindow* window = swapchain.surface.window.get();
1586 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001587 if (region) {
1588 // Process the incremental-present hint for this swapchain:
1589 uint32_t rcount = region->rectangleCount;
1590 if (rcount > nrects) {
1591 android_native_rect_t* new_rects =
1592 static_cast<android_native_rect_t*>(
1593 allocator->pfnReallocation(
1594 allocator->pUserData, rects,
1595 sizeof(android_native_rect_t) * rcount,
1596 alignof(android_native_rect_t),
1597 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1598 if (new_rects) {
1599 rects = new_rects;
1600 nrects = rcount;
1601 } else {
1602 rcount = 0; // Ignore the hint for this swapchain
1603 }
1604 }
1605 for (uint32_t r = 0; r < rcount; ++r) {
1606 if (region->pRectangles[r].layer > 0) {
1607 ALOGV(
1608 "vkQueuePresentKHR ignoring invalid layer "
1609 "(%u); using layer 0 instead",
1610 region->pRectangles[r].layer);
1611 }
1612 int x = region->pRectangles[r].offset.x;
1613 int y = region->pRectangles[r].offset.y;
1614 int width = static_cast<int>(
1615 region->pRectangles[r].extent.width);
1616 int height = static_cast<int>(
1617 region->pRectangles[r].extent.height);
1618 android_native_rect_t* cur_rect = &rects[r];
1619 cur_rect->left = x;
1620 cur_rect->top = y + height;
1621 cur_rect->right = x + width;
1622 cur_rect->bottom = y;
1623 }
1624 native_window_set_surface_damage(window, rects, rcount);
1625 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001626 if (time) {
1627 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001628 ALOGV(
1629 "Calling "
1630 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001631 native_window_enable_frame_timestamps(window, true);
1632 swapchain.frame_timestamps_enabled = true;
1633 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001634
1635 // Record the nativeFrameId so it can be later correlated to
1636 // this present.
1637 uint64_t nativeFrameId = 0;
1638 err = native_window_get_next_frame_id(
1639 window, &nativeFrameId);
1640 if (err != android::NO_ERROR) {
1641 ALOGE("Failed to get next native frame ID.");
1642 }
1643
1644 // Add a new timing record with the user's presentID and
1645 // the nativeFrameId.
1646 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1647 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001648 swapchain.timing.removeAt(0);
1649 }
1650 if (time->desiredPresentTime) {
1651 // Set the desiredPresentTime:
1652 ALOGV(
1653 "Calling "
1654 "native_window_set_buffers_timestamp(%" PRId64 ")",
1655 time->desiredPresentTime);
1656 native_window_set_buffers_timestamp(
1657 window,
1658 static_cast<int64_t>(time->desiredPresentTime));
1659 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001660 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001661
Jesse Halldc225072016-05-30 22:40:14 -07001662 err = window->queueBuffer(window, img.buffer.get(), fence);
1663 // queueBuffer always closes fence, even on error
1664 if (err != 0) {
1665 // TODO(jessehall): What now? We should probably cancel the
1666 // buffer, I guess?
1667 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1668 swapchain_result = WorstPresentResult(
1669 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1670 }
1671 if (img.dequeue_fence >= 0) {
1672 close(img.dequeue_fence);
1673 img.dequeue_fence = -1;
1674 }
1675 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001676
1677 // If the swapchain is in shared mode, immediately dequeue the
1678 // buffer so it can be presented again without an intervening
1679 // call to AcquireNextImageKHR. We expect to get the same buffer
1680 // back from every call to dequeueBuffer in this mode.
1681 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1682 ANativeWindowBuffer* buffer;
1683 int fence_fd;
1684 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1685 if (err != 0) {
1686 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1687 swapchain_result = WorstPresentResult(swapchain_result,
1688 VK_ERROR_SURFACE_LOST_KHR);
1689 }
1690 else if (img.buffer != buffer) {
1691 ALOGE("got wrong image back for shared swapchain");
1692 swapchain_result = WorstPresentResult(swapchain_result,
1693 VK_ERROR_SURFACE_LOST_KHR);
1694 }
1695 else {
1696 img.dequeue_fence = fence_fd;
1697 img.dequeued = true;
1698 }
1699 }
Jesse Halldc225072016-05-30 22:40:14 -07001700 }
1701 if (swapchain_result != VK_SUCCESS) {
1702 ReleaseSwapchainImage(device, window, fence, img);
1703 OrphanSwapchain(device, &swapchain);
1704 }
1705 } else {
1706 ReleaseSwapchainImage(device, nullptr, fence, img);
1707 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001708 }
1709
Jesse Halla9e57032015-11-30 01:03:10 -08001710 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001711 present_info->pResults[sc] = swapchain_result;
1712
1713 if (swapchain_result != final_result)
1714 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001715 }
Ian Elliottcb351132016-12-13 10:30:40 -07001716 if (rects) {
1717 allocator->pfnFree(allocator->pUserData, rects);
1718 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001719
1720 return final_result;
1721}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001722
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001723VKAPI_ATTR
1724VkResult GetRefreshCycleDurationGOOGLE(
1725 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001726 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001727 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001728 ATRACE_CALL();
1729
Ian Elliott62c48c92017-01-20 13:13:20 -07001730 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001731 VkResult result = VK_SUCCESS;
1732
Brian Andersondc96fdf2017-03-20 16:54:25 -07001733 pDisplayTimingProperties->refreshDuration =
1734 static_cast<uint64_t>(swapchain.refresh_duration);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001735
1736 return result;
1737}
1738
1739VKAPI_ATTR
1740VkResult GetPastPresentationTimingGOOGLE(
1741 VkDevice,
1742 VkSwapchainKHR swapchain_handle,
1743 uint32_t* count,
1744 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001745 ATRACE_CALL();
1746
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001747 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1748 ANativeWindow* window = swapchain.surface.window.get();
1749 VkResult result = VK_SUCCESS;
1750
1751 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001752 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001753 native_window_enable_frame_timestamps(window, true);
1754 swapchain.frame_timestamps_enabled = true;
1755 }
1756
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001757 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001758 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1759 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001760 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001761 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001762 }
1763
1764 return result;
1765}
1766
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001767VKAPI_ATTR
1768VkResult GetSwapchainStatusKHR(
1769 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001770 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001771 ATRACE_CALL();
1772
Chris Forbes4e18ba82017-01-20 12:50:17 +13001773 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001774 VkResult result = VK_SUCCESS;
1775
Chris Forbes4e18ba82017-01-20 12:50:17 +13001776 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1777 return VK_ERROR_OUT_OF_DATE_KHR;
1778 }
1779
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001780 // TODO(chrisforbes): Implement this function properly
1781
1782 return result;
1783}
1784
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001785VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001786 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001787 uint32_t swapchainCount,
1788 const VkSwapchainKHR* pSwapchains,
1789 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001790 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001791
1792 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1793 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1794 if (!swapchain)
1795 continue;
1796
1797 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1798
1799 ANativeWindow* window = swapchain->surface.window.get();
1800
1801 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1802 const android_smpte2086_metadata smpteMetdata = {
1803 {vulkanMetadata.displayPrimaryRed.x,
1804 vulkanMetadata.displayPrimaryRed.y},
1805 {vulkanMetadata.displayPrimaryGreen.x,
1806 vulkanMetadata.displayPrimaryGreen.y},
1807 {vulkanMetadata.displayPrimaryBlue.x,
1808 vulkanMetadata.displayPrimaryBlue.y},
1809 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1810 vulkanMetadata.maxLuminance,
1811 vulkanMetadata.minLuminance};
1812 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1813
1814 const android_cta861_3_metadata cta8613Metadata = {
1815 vulkanMetadata.maxContentLightLevel,
1816 vulkanMetadata.maxFrameAverageLightLevel};
1817 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1818 }
1819
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001820 return;
1821}
1822
Yiwei Zhang0f475222019-04-11 19:38:00 -07001823static void InterceptBindImageMemory2(
1824 uint32_t bind_info_count,
1825 const VkBindImageMemoryInfo* bind_infos,
1826 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1827 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1828 out_native_buffers->clear();
1829 out_bind_infos->clear();
1830
1831 if (!bind_info_count)
1832 return;
1833
1834 std::unordered_set<uint32_t> intercepted_indexes;
1835
1836 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1837 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1838 bind_infos[idx].pNext);
1839 while (info &&
1840 info->sType !=
1841 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1842 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1843 info->pNext);
1844 }
1845
1846 if (!info)
1847 continue;
1848
1849 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1850 "swapchain handle must not be NULL");
1851 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1852 ALOG_ASSERT(
1853 info->imageIndex < swapchain->num_images,
1854 "imageIndex must be less than the number of images in swapchain");
1855
1856 ANativeWindowBuffer* buffer =
1857 swapchain->images[info->imageIndex].buffer.get();
1858 VkNativeBufferANDROID native_buffer = {
1859#pragma clang diagnostic push
1860#pragma clang diagnostic ignored "-Wold-style-cast"
1861 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1862#pragma clang diagnostic pop
1863 .pNext = bind_infos[idx].pNext,
1864 .handle = buffer->handle,
1865 .stride = buffer->stride,
1866 .format = buffer->format,
1867 .usage = int(buffer->usage),
1868 };
1869 // Reserve enough space to avoid letting re-allocation invalidate the
1870 // addresses of the elements inside.
1871 out_native_buffers->reserve(bind_info_count);
1872 out_native_buffers->emplace_back(native_buffer);
1873
1874 // Reserve the space now since we know how much is needed now.
1875 out_bind_infos->reserve(bind_info_count);
1876 out_bind_infos->emplace_back(bind_infos[idx]);
1877 out_bind_infos->back().pNext = &out_native_buffers->back();
1878
1879 intercepted_indexes.insert(idx);
1880 }
1881
1882 if (intercepted_indexes.empty())
1883 return;
1884
1885 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1886 if (intercepted_indexes.count(idx))
1887 continue;
1888 out_bind_infos->emplace_back(bind_infos[idx]);
1889 }
1890}
1891
Yiwei Zhang23143102019-04-10 18:24:05 -07001892VKAPI_ATTR
1893VkResult BindImageMemory2(VkDevice device,
1894 uint32_t bindInfoCount,
1895 const VkBindImageMemoryInfo* pBindInfos) {
1896 ATRACE_CALL();
1897
Yiwei Zhang0f475222019-04-11 19:38:00 -07001898 // out_native_buffers is for maintaining the lifecycle of the constructed
1899 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1900 std::vector<VkNativeBufferANDROID> out_native_buffers;
1901 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1902 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1903 &out_bind_infos);
1904 return GetData(device).driver.BindImageMemory2(
1905 device, bindInfoCount,
1906 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001907}
1908
1909VKAPI_ATTR
1910VkResult BindImageMemory2KHR(VkDevice device,
1911 uint32_t bindInfoCount,
1912 const VkBindImageMemoryInfo* pBindInfos) {
1913 ATRACE_CALL();
1914
Yiwei Zhang0f475222019-04-11 19:38:00 -07001915 std::vector<VkNativeBufferANDROID> out_native_buffers;
1916 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1917 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1918 &out_bind_infos);
1919 return GetData(device).driver.BindImageMemory2KHR(
1920 device, bindInfoCount,
1921 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001922}
1923
Chia-I Wu62262232016-03-26 07:06:44 +08001924} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001925} // namespace vulkan