blob: a22f60e33097d933f780f750fc26db97a779629d [file] [log] [blame]
Jesse Hallb1352bc2015-09-04 16:12:33 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang0f475222019-04-11 19:38:00 -070019#include <android/hardware/graphics/common/1.0/types.h>
Jesse Hall79927812017-03-23 11:03:23 -070020#include <grallocusage/GrallocUsageConversion.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070021#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070022#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070023#include <system/window.h>
24#include <ui/BufferQueueDefs.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080025#include <utils/StrongPointer.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080026#include <utils/Trace.h>
Brian Anderson1049d1d2016-12-16 17:25:57 -080027#include <utils/Vector.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070028
29#include <algorithm>
30#include <unordered_set>
31#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070032
Chia-I Wu4a6a9162016-03-26 07:17:34 +080033#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070034
Daniel Kochf25f5bb2017-10-05 00:26:58 -040035using android::hardware::graphics::common::V1_0::BufferUsage;
36
Jesse Hall5ae3abb2015-10-08 14:00:22 -070037// TODO(jessehall): Currently we don't have a good error code for when a native
38// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
39// versions (post SDK 0.9) of the API/extension have a better error code.
40// When updating to that version, audit all error returns.
Chia-I Wu62262232016-03-26 07:06:44 +080041namespace vulkan {
42namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070043
Jesse Halld7b994a2015-09-07 14:17:37 -070044namespace {
45
Jesse Hall55bc0972016-02-23 16:43:29 -080046const VkSurfaceTransformFlagsKHR kSupportedTransforms =
47 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
48 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
49 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
50 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
51 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
52 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
53 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
54 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
55 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
56 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
57
58VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
59 // Native and Vulkan transforms are isomorphic, but are represented
60 // differently. Vulkan transforms are built up of an optional horizontal
61 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
62 // transforms are built up from a horizontal flip, vertical flip, and
63 // 90-degree rotation, all optional but always in that order.
64
65 // TODO(jessehall): For now, only support pure rotations, not
66 // flip or flip-and-rotate, until I have more time to test them and build
67 // sample code. As far as I know we never actually use anything besides
68 // pure rotations anyway.
69
70 switch (native) {
71 case 0: // 0x0
72 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
73 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
74 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
75 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
76 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
77 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
78 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
79 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
80 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
81 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
82 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
83 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
84 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
85 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
86 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
87 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
88 default:
89 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
90 }
91}
92
Jesse Hall178b6962016-02-24 15:39:50 -080093int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
94 switch (transform) {
95 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
96 return NATIVE_WINDOW_TRANSFORM_ROT_270;
97 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
98 return NATIVE_WINDOW_TRANSFORM_ROT_180;
99 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
100 return NATIVE_WINDOW_TRANSFORM_ROT_90;
101 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
102 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
103 // return NATIVE_WINDOW_TRANSFORM_FLIP_H;
104 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
105 // return NATIVE_WINDOW_TRANSFORM_FLIP_H |
106 // NATIVE_WINDOW_TRANSFORM_ROT_90;
107 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
108 // return NATIVE_WINDOW_TRANSFORM_FLIP_V;
109 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
110 // return NATIVE_WINDOW_TRANSFORM_FLIP_V |
111 // NATIVE_WINDOW_TRANSFORM_ROT_90;
112 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
113 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
114 default:
115 return 0;
116 }
117}
118
Ian Elliott8a977262017-01-19 09:05:58 -0700119class TimingInfo {
120 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800121 TimingInfo() = default;
122 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700123 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800124 native_frame_id_(nativeFrameId) {}
125 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700126 return (timestamp_desired_present_time_ !=
127 NATIVE_WINDOW_TIMESTAMP_PENDING &&
128 timestamp_actual_present_time_ !=
129 NATIVE_WINDOW_TIMESTAMP_PENDING &&
130 timestamp_render_complete_time_ !=
131 NATIVE_WINDOW_TIMESTAMP_PENDING &&
132 timestamp_composition_latch_time_ !=
133 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700134 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700135 void calculate(int64_t rdur) {
136 bool anyTimestampInvalid =
137 (timestamp_actual_present_time_ ==
138 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
139 (timestamp_render_complete_time_ ==
140 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
141 (timestamp_composition_latch_time_ ==
142 NATIVE_WINDOW_TIMESTAMP_INVALID);
143 if (anyTimestampInvalid) {
144 ALOGE("Unexpectedly received invalid timestamp.");
145 vals_.actualPresentTime = 0;
146 vals_.earliestPresentTime = 0;
147 vals_.presentMargin = 0;
148 return;
149 }
150
151 vals_.actualPresentTime =
152 static_cast<uint64_t>(timestamp_actual_present_time_);
153 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700154 timestamp_render_complete_time_);
155 // Calculate vals_.earliestPresentTime, and potentially adjust
156 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
157 // is vals_.actualPresentTime. If we can subtract rdur (the duration
158 // of a refresh cycle) from vals_.earliestPresentTime (and also from
159 // vals_.presentMargin) and still leave a positive margin, then we can
160 // report to the application that it could have presented earlier than
161 // it did (per the extension specification). If for some reason, we
162 // can do this subtraction repeatedly, we do, since
163 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700164 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700165 while ((margin > rdur) &&
166 ((early_time - rdur) > timestamp_composition_latch_time_)) {
167 early_time -= rdur;
168 margin -= rdur;
169 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700170 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
171 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700172 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800173 void get_values(VkPastPresentationTimingGOOGLE* values) const {
174 *values = vals_;
175 }
Ian Elliott8a977262017-01-19 09:05:58 -0700176
177 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800178 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700179
Brian Anderson1049d1d2016-12-16 17:25:57 -0800180 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700181 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
182 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
183 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
184 int64_t timestamp_composition_latch_time_
185 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700186};
187
Jesse Halld7b994a2015-09-07 14:17:37 -0700188// ----------------------------------------------------------------------------
189
Jesse Hall1356b0d2015-11-23 17:24:58 -0800190struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800191 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700192 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700193 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800194};
195
196VkSurfaceKHR HandleFromSurface(Surface* surface) {
197 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
198}
199
200Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800201 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800202}
203
Ian Elliott8a977262017-01-19 09:05:58 -0700204// Maximum number of TimingInfo structs to keep per swapchain:
205enum { MAX_TIMING_INFOS = 10 };
206// Minimum number of frames to look for in the past (so we don't cause
207// syncronous requests to Surface Flinger):
208enum { MIN_NUM_FRAMES_AGO = 5 };
209
Jesse Hall1356b0d2015-11-23 17:24:58 -0800210struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700211 Swapchain(Surface& surface_,
212 uint32_t num_images_,
213 VkPresentModeKHR present_mode)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700214 : surface(surface_),
215 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700216 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
Chris Forbesf8835642017-03-30 19:31:40 +1300217 frame_timestamps_enabled(false),
218 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
219 present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700220 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700221 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700222 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700223 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700224 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600225 uint64_t get_refresh_duration()
226 {
227 ANativeWindow* window = surface.window.get();
228 native_window_get_refresh_cycle_duration(
229 window,
230 &refresh_duration);
231 return static_cast<uint64_t>(refresh_duration);
232
233 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800234
235 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700236 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700237 bool mailbox_mode;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700238 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700239 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300240 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700241
242 struct Image {
243 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
244 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800245 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700246 // The fence is only valid when the buffer is dequeued, and should be
247 // -1 any other time. When valid, we own the fd, and must ensure it is
248 // closed: either by closing it explicitly when queueing the buffer,
249 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
250 int dequeue_fence;
251 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800252 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700253
Brian Anderson1049d1d2016-12-16 17:25:57 -0800254 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700255};
256
257VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
258 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
259}
260
261Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800262 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700263}
264
Jesse Halldc225072016-05-30 22:40:14 -0700265void ReleaseSwapchainImage(VkDevice device,
266 ANativeWindow* window,
267 int release_fence,
268 Swapchain::Image& image) {
269 ALOG_ASSERT(release_fence == -1 || image.dequeued,
270 "ReleaseSwapchainImage: can't provide a release fence for "
271 "non-dequeued images");
272
273 if (image.dequeued) {
274 if (release_fence >= 0) {
275 // We get here from vkQueuePresentKHR. The application is
276 // responsible for creating an execution dependency chain from
277 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
278 // (release_fence), so we can drop the dequeue_fence here.
279 if (image.dequeue_fence >= 0)
280 close(image.dequeue_fence);
281 } else {
282 // We get here during swapchain destruction, or various serious
283 // error cases e.g. when we can't create the release_fence during
284 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
285 // have already signalled, since the swapchain images are supposed
286 // to be idle before the swapchain is destroyed. In error cases,
287 // there may be rendering in flight to the image, but since we
288 // weren't able to create a release_fence, waiting for the
289 // dequeue_fence is about the best we can do.
290 release_fence = image.dequeue_fence;
291 }
292 image.dequeue_fence = -1;
293
294 if (window) {
295 window->cancelBuffer(window, image.buffer.get(), release_fence);
296 } else {
297 if (release_fence >= 0) {
298 sync_wait(release_fence, -1 /* forever */);
299 close(release_fence);
300 }
301 }
302
303 image.dequeued = false;
304 }
305
306 if (image.image) {
307 GetData(device).driver.DestroyImage(device, image.image, nullptr);
308 image.image = VK_NULL_HANDLE;
309 }
310
311 image.buffer.clear();
312}
313
314void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
315 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
316 return;
Jesse Halldc225072016-05-30 22:40:14 -0700317 for (uint32_t i = 0; i < swapchain->num_images; i++) {
318 if (!swapchain->images[i].dequeued)
319 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
320 }
321 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700322 swapchain->timing.clear();
323}
324
325uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800326 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
327 return 0;
328 }
Ian Elliott8a977262017-01-19 09:05:58 -0700329
Brian Anderson1049d1d2016-12-16 17:25:57 -0800330 uint32_t num_ready = 0;
331 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
332 for (uint32_t i = 0; i < num_timings; i++) {
333 TimingInfo& ti = swapchain.timing.editItemAt(i);
334 if (ti.ready()) {
335 // This TimingInfo is ready to be reported to the user. Add it
336 // to the num_ready.
337 num_ready++;
338 continue;
339 }
340 // This TimingInfo is not yet ready to be reported to the user,
341 // and so we should look for any available timestamps that
342 // might make it ready.
343 int64_t desired_present_time = 0;
344 int64_t render_complete_time = 0;
345 int64_t composition_latch_time = 0;
346 int64_t actual_present_time = 0;
347 // Obtain timestamps:
348 int ret = native_window_get_frame_timestamps(
349 swapchain.surface.window.get(), ti.native_frame_id_,
350 &desired_present_time, &render_complete_time,
351 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700352 nullptr, //&first_composition_start_time,
353 nullptr, //&last_composition_start_time,
354 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800355 // TODO(ianelliott): Maybe ask if this one is
356 // supported, at startup time (since it may not be
357 // supported):
358 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700359 nullptr, //&dequeue_ready_time,
360 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800361
362 if (ret != android::NO_ERROR) {
363 continue;
364 }
365
366 // Record the timestamp(s) we received, and then see if this TimingInfo
367 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700368 ti.timestamp_desired_present_time_ = desired_present_time;
369 ti.timestamp_actual_present_time_ = actual_present_time;
370 ti.timestamp_render_complete_time_ = render_complete_time;
371 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800372
373 if (ti.ready()) {
374 // The TimingInfo has received enough timestamps, and should now
375 // use those timestamps to calculate the info that should be
376 // reported to the user:
377 ti.calculate(swapchain.refresh_duration);
378 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700379 }
380 }
381 return num_ready;
382}
383
384// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
385void copy_ready_timings(Swapchain& swapchain,
386 uint32_t* count,
387 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800388 if (swapchain.timing.empty()) {
389 *count = 0;
390 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700391 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800392
393 size_t last_ready = swapchain.timing.size() - 1;
394 while (!swapchain.timing[last_ready].ready()) {
395 if (last_ready == 0) {
396 *count = 0;
397 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700398 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800399 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700400 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800401
402 uint32_t num_copied = 0;
403 size_t num_to_remove = 0;
404 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
405 const TimingInfo& ti = swapchain.timing[i];
406 if (ti.ready()) {
407 ti.get_values(&timings[num_copied]);
408 num_copied++;
409 }
410 num_to_remove++;
411 }
412
413 // Discard old frames that aren't ready if newer frames are ready.
414 // We don't expect to get the timing info for those old frames.
415 swapchain.timing.removeItemsAt(0, num_to_remove);
416
Ian Elliott8a977262017-01-19 09:05:58 -0700417 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700418}
419
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700420android_pixel_format GetNativePixelFormat(VkFormat format) {
421 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
422 switch (format) {
423 case VK_FORMAT_R8G8B8A8_UNORM:
424 case VK_FORMAT_R8G8B8A8_SRGB:
425 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
426 break;
427 case VK_FORMAT_R5G6B5_UNORM_PACK16:
428 native_format = HAL_PIXEL_FORMAT_RGB_565;
429 break;
430 case VK_FORMAT_R16G16B16A16_SFLOAT:
431 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
432 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800433 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700434 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
435 break;
436 default:
437 ALOGV("unsupported swapchain format %d", format);
438 break;
439 }
440 return native_format;
441}
442
443android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
444 switch (colorspace) {
445 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
446 return HAL_DATASPACE_V0_SRGB;
447 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
448 return HAL_DATASPACE_DISPLAY_P3;
449 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
450 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600451 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
452 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700453 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
454 return HAL_DATASPACE_DCI_P3_LINEAR;
455 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
456 return HAL_DATASPACE_DCI_P3;
457 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
458 return HAL_DATASPACE_V0_SRGB_LINEAR;
459 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
460 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600461 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
462 return HAL_DATASPACE_BT2020_LINEAR;
463 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700464 return static_cast<android_dataspace>(
465 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
466 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600467 case VK_COLOR_SPACE_DOLBYVISION_EXT:
468 return static_cast<android_dataspace>(
469 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
470 HAL_DATASPACE_RANGE_FULL);
471 case VK_COLOR_SPACE_HDR10_HLG_EXT:
472 return static_cast<android_dataspace>(
473 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
474 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700475 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
476 return static_cast<android_dataspace>(
477 HAL_DATASPACE_STANDARD_ADOBE_RGB |
478 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
479 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
480 return HAL_DATASPACE_ADOBE_RGB;
481
482 // Pass through is intended to allow app to provide data that is passed
483 // to the display system without modification.
484 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
485 return HAL_DATASPACE_ARBITRARY;
486
487 default:
488 // This indicates that we don't know about the
489 // dataspace specified and we should indicate that
490 // it's unsupported
491 return HAL_DATASPACE_UNKNOWN;
492 }
493}
494
Jesse Halld7b994a2015-09-07 14:17:37 -0700495} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700496
Jesse Halle1b12782015-11-30 11:27:32 -0800497VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800498VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800499 VkInstance instance,
500 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
501 const VkAllocationCallbacks* allocator,
502 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800503 ATRACE_CALL();
504
Jesse Hall1f91d392015-12-11 16:28:44 -0800505 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800506 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800507 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
508 alignof(Surface),
509 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800510 if (!mem)
511 return VK_ERROR_OUT_OF_HOST_MEMORY;
512 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700513
Chia-I Wue8e689f2016-04-18 08:21:31 +0800514 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700515 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700516 int err = native_window_get_consumer_usage(surface->window.get(),
517 &surface->consumer_usage);
518 if (err != android::NO_ERROR) {
519 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
520 strerror(-err), err);
521 surface->~Surface();
522 allocator->pfnFree(allocator->pUserData, surface);
523 return VK_ERROR_INITIALIZATION_FAILED;
524 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700525
Jesse Hall1356b0d2015-11-23 17:24:58 -0800526 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
Yiwei Zhang6435b322018-05-08 11:12:17 -0700527 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800528 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
529 if (err != 0) {
530 // TODO(jessehall): Improve error reporting. Can we enumerate possible
531 // errors and translate them to valid Vulkan result codes?
532 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
533 err);
534 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800535 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700536 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800537 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700538
Jesse Hall1356b0d2015-11-23 17:24:58 -0800539 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700540 return VK_SUCCESS;
541}
542
Jesse Halle1b12782015-11-30 11:27:32 -0800543VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800544void DestroySurfaceKHR(VkInstance instance,
545 VkSurfaceKHR surface_handle,
546 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800547 ATRACE_CALL();
548
Jesse Hall1356b0d2015-11-23 17:24:58 -0800549 Surface* surface = SurfaceFromHandle(surface_handle);
550 if (!surface)
551 return;
552 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700553 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700554 "destroyed VkSurfaceKHR 0x%" PRIx64
555 " has active VkSwapchainKHR 0x%" PRIx64,
556 reinterpret_cast<uint64_t>(surface_handle),
557 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800558 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800559 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800560 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800561 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800562}
563
Jesse Halle1b12782015-11-30 11:27:32 -0800564VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800565VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
566 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700567 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800568 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800569 ATRACE_CALL();
570
Yiwei Zhang6435b322018-05-08 11:12:17 -0700571 const Surface* surface = SurfaceFromHandle(surface_handle);
572 if (!surface) {
573 return VK_ERROR_SURFACE_LOST_KHR;
574 }
575 const ANativeWindow* window = surface->window.get();
576
577 int query_value;
578 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
579 if (err != 0 || query_value < 0) {
580 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
581 strerror(-err), err, query_value);
582 return VK_ERROR_SURFACE_LOST_KHR;
583 }
584
585 android_pixel_format native_format =
586 static_cast<android_pixel_format>(query_value);
587
588 bool format_supported = false;
589 switch (native_format) {
590 case HAL_PIXEL_FORMAT_RGBA_8888:
591 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700592 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800593 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700594 format_supported = true;
595 break;
596 default:
597 break;
598 }
599
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700600 *supported = static_cast<VkBool32>(
601 format_supported || (surface->consumer_usage &
602 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
603 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700604
Jesse Halla6429252015-11-29 18:59:42 -0800605 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800606}
607
Jesse Halle1b12782015-11-30 11:27:32 -0800608VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800609VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800610 VkPhysicalDevice /*pdev*/,
611 VkSurfaceKHR surface,
612 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800613 ATRACE_CALL();
614
Jesse Halld7b994a2015-09-07 14:17:37 -0700615 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800616 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700617
618 int width, height;
619 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
620 if (err != 0) {
621 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
622 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700623 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700624 }
625 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
626 if (err != 0) {
627 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
628 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700629 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700630 }
631
Jesse Hall55bc0972016-02-23 16:43:29 -0800632 int transform_hint;
633 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
634 if (err != 0) {
635 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
636 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700637 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800638 }
639
Jesse Halld7b994a2015-09-07 14:17:37 -0700640 // TODO(jessehall): Figure out what the min/max values should be.
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800641 int max_buffer_count;
642 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
643 if (err != 0) {
644 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
645 strerror(-err), err);
646 return VK_ERROR_SURFACE_LOST_KHR;
647 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700648 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800649 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700650
Jesse Hallfe2662d2016-02-09 13:26:59 -0800651 capabilities->currentExtent =
652 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
653
Jesse Halld7b994a2015-09-07 14:17:37 -0700654 // TODO(jessehall): Figure out what the max extent should be. Maximum
655 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800656 capabilities->minImageExtent = VkExtent2D{1, 1};
657 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700658
Jesse Hallfe2662d2016-02-09 13:26:59 -0800659 capabilities->maxImageArrayLayers = 1;
660
Jesse Hall55bc0972016-02-23 16:43:29 -0800661 capabilities->supportedTransforms = kSupportedTransforms;
662 capabilities->currentTransform =
663 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700664
Jesse Hallfe2662d2016-02-09 13:26:59 -0800665 // On Android, window composition is a WindowManager property, not something
666 // associated with the bufferqueue. It can't be changed from here.
667 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700668
669 // TODO(jessehall): I think these are right, but haven't thought hard about
670 // it. Do we need to query the driver for support of any of these?
671 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700672 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
673 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800674 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800675 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
676 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
677 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700678 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
679
Jesse Hallb1352bc2015-09-04 16:12:33 -0700680 return VK_SUCCESS;
681}
682
Jesse Halle1b12782015-11-30 11:27:32 -0800683VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700684VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
685 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800686 uint32_t* count,
687 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800688 ATRACE_CALL();
689
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700690 const InstanceData& instance_data = GetData(pdev);
691
Jesse Hall1356b0d2015-11-23 17:24:58 -0800692 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
693 // a new gralloc method to query whether a (format, usage) pair is
694 // supported, and check that for each gralloc format that corresponds to a
695 // Vulkan format. Shorter term, just add a few more formats to the ones
696 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700697
698 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700699 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
700 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
701 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Ian Elliott94ace212019-02-20 14:05:29 -0700702 {VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
703 {VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700704 };
705 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700706 uint32_t total_num_formats = kNumFormats;
707
708 bool wide_color_support = false;
709 Surface& surface = *SurfaceFromHandle(surface_handle);
710 int err = native_window_get_wide_color_support(surface.window.get(),
711 &wide_color_support);
712 if (err) {
713 // Not allowed to return a more sensible error code, so do this
714 return VK_ERROR_OUT_OF_HOST_MEMORY;
715 }
716 ALOGV("wide_color_support is: %d", wide_color_support);
717 wide_color_support =
718 wide_color_support &&
719 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
720
721 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600722 {VK_FORMAT_R8G8B8A8_UNORM,
723 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
724 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700725 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700726 {VK_FORMAT_R16G16B16A16_SFLOAT,
727 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
728 {VK_FORMAT_R16G16B16A16_SFLOAT,
729 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800730 {VK_FORMAT_A2B10G10R10_UNORM_PACK32,
731 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700732 };
733 const uint32_t kNumWideColorFormats =
734 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
735 if (wide_color_support) {
736 total_num_formats += kNumWideColorFormats;
737 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700738
739 VkResult result = VK_SUCCESS;
740 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700741 uint32_t out_count = 0;
742 uint32_t transfer_count = 0;
743 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700744 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700745 transfer_count = std::min(*count, kNumFormats);
746 std::copy(kFormats, kFormats + transfer_count, formats);
747 out_count += transfer_count;
748 if (wide_color_support) {
749 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
750 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
751 formats + out_count);
752 out_count += transfer_count;
753 }
754 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700755 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700756 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700757 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700758 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700759}
760
Jesse Halle1b12782015-11-30 11:27:32 -0800761VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300762VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
763 VkPhysicalDevice physicalDevice,
764 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
765 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800766 ATRACE_CALL();
767
Chris Forbes2452cf72017-03-16 16:30:17 +1300768 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
769 physicalDevice, pSurfaceInfo->surface,
770 &pSurfaceCapabilities->surfaceCapabilities);
771
Chris Forbes06bc0092017-03-16 16:46:05 +1300772 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
773 while (caps->pNext) {
774 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
775
776 switch (caps->sType) {
777 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
778 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
779 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
780 caps);
781 // Claim same set of usage flags are supported for
782 // shared present modes as for other modes.
783 shared_caps->sharedPresentSupportedUsageFlags =
784 pSurfaceCapabilities->surfaceCapabilities
785 .supportedUsageFlags;
786 } break;
787
788 default:
789 // Ignore all other extension structs
790 break;
791 }
792 }
793
Chris Forbes2452cf72017-03-16 16:30:17 +1300794 return result;
795}
796
797VKAPI_ATTR
798VkResult GetPhysicalDeviceSurfaceFormats2KHR(
799 VkPhysicalDevice physicalDevice,
800 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
801 uint32_t* pSurfaceFormatCount,
802 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800803 ATRACE_CALL();
804
Chris Forbes2452cf72017-03-16 16:30:17 +1300805 if (!pSurfaceFormats) {
806 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
807 pSurfaceInfo->surface,
808 pSurfaceFormatCount, nullptr);
809 } else {
810 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
811 // after the call.
812 android::Vector<VkSurfaceFormatKHR> surface_formats;
813 surface_formats.resize(*pSurfaceFormatCount);
814 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
815 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
816 &surface_formats.editItemAt(0));
817
818 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
819 // marshal results individually due to stride difference.
820 // completely ignore any chained extension structs.
821 uint32_t formats_to_marshal = *pSurfaceFormatCount;
822 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
823 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
824 }
825 }
826
827 return result;
828 }
829}
830
831VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300832VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800833 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800834 uint32_t* count,
835 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800836 ATRACE_CALL();
837
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800838 int err;
839 int query_value;
840 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
841
842 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
843 if (err != 0 || query_value < 0) {
844 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
845 strerror(-err), err, query_value);
846 return VK_ERROR_SURFACE_LOST_KHR;
847 }
848 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
849
850 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
851 if (err != 0 || query_value < 0) {
852 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
853 strerror(-err), err, query_value);
854 return VK_ERROR_SURFACE_LOST_KHR;
855 }
856 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
857
Chris Forbese8d79a62017-02-22 12:49:18 +1300858 android::Vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800859 if (min_undequeued_buffers + 1 < max_buffer_count)
860 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300861 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
862
863 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
864 if (QueryPresentationProperties(pdev, &present_properties)) {
865 if (present_properties.sharedImage) {
866 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
867 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
868 }
869 }
870
871 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700872
873 VkResult result = VK_SUCCESS;
874 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300875 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700876 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300877 *count = std::min(*count, num_modes);
878 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700879 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300880 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700881 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700882 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700883}
884
Jesse Halle1b12782015-11-30 11:27:32 -0800885VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400886VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600887 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400888 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800889 ATRACE_CALL();
890
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400891 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
892 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
893 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
894 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
895 pDeviceGroupPresentCapabilities->sType);
896
897 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
898 sizeof(pDeviceGroupPresentCapabilities->presentMask));
899
900 // assume device group of size 1
901 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
902 pDeviceGroupPresentCapabilities->modes =
903 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
904
905 return VK_SUCCESS;
906}
907
908VKAPI_ATTR
909VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600910 VkDevice,
911 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400912 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800913 ATRACE_CALL();
914
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400915 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
916 return VK_SUCCESS;
917}
918
919VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600920VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400921 VkSurfaceKHR surface,
922 uint32_t* pRectCount,
923 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800924 ATRACE_CALL();
925
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400926 if (!pRects) {
927 *pRectCount = 1;
928 } else {
929 uint32_t count = std::min(*pRectCount, 1u);
930 bool incomplete = *pRectCount < 1;
931
932 *pRectCount = count;
933
934 if (incomplete) {
935 return VK_INCOMPLETE;
936 }
937
938 int err;
939 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
940
941 int width = 0, height = 0;
942 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
943 if (err != 0) {
944 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
945 strerror(-err), err);
946 }
947 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
948 if (err != 0) {
949 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
950 strerror(-err), err);
951 }
952
953 // TODO: Return something better than "whole window"
954 pRects[0].offset.x = 0;
955 pRects[0].offset.y = 0;
956 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
957 static_cast<uint32_t>(height)};
958 }
959 return VK_SUCCESS;
960}
961
962VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800963VkResult CreateSwapchainKHR(VkDevice device,
964 const VkSwapchainCreateInfoKHR* create_info,
965 const VkAllocationCallbacks* allocator,
966 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800967 ATRACE_CALL();
968
Jesse Halld7b994a2015-09-07 14:17:37 -0700969 int err;
970 VkResult result = VK_SUCCESS;
971
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700972 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
973 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
974 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
975 " oldSwapchain=0x%" PRIx64,
976 reinterpret_cast<uint64_t>(create_info->surface),
977 create_info->minImageCount, create_info->imageFormat,
978 create_info->imageColorSpace, create_info->imageExtent.width,
979 create_info->imageExtent.height, create_info->imageUsage,
980 create_info->preTransform, create_info->presentMode,
981 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
982
Jesse Hall1f91d392015-12-11 16:28:44 -0800983 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800984 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800985
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700986 android_pixel_format native_pixel_format =
987 GetNativePixelFormat(create_info->imageFormat);
988 android_dataspace native_dataspace =
989 GetNativeDataspace(create_info->imageColorSpace);
990 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
991 ALOGE(
992 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
993 "failed: Unsupported color space",
994 create_info->imageColorSpace);
995 return VK_ERROR_INITIALIZATION_FAILED;
996 }
997
Jesse Hall42a9eec2016-06-03 12:39:49 -0700998 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700999 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001000 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001001 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001002 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001003 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001004 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001005 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001006 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1007 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001008 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001009 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001010
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001011 Surface& surface = *SurfaceFromHandle(create_info->surface);
1012
Jesse Halldc225072016-05-30 22:40:14 -07001013 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001014 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001015 " because it already has active swapchain 0x%" PRIx64
1016 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1017 reinterpret_cast<uint64_t>(create_info->surface),
1018 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1019 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1020 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1021 }
1022 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1023 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1024
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001025 // -- Reset the native window --
1026 // The native window might have been used previously, and had its properties
1027 // changed from defaults. That will affect the answer we get for queries
1028 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1029 // attempt such queries.
1030
Jesse Halldc225072016-05-30 22:40:14 -07001031 // The native window only allows dequeueing all buffers before any have
1032 // been queued, since after that point at least one is assumed to be in
1033 // non-FREE state at any given time. Disconnecting and re-connecting
1034 // orphans the previous buffers, getting us back to the state where we can
1035 // dequeue all buffers.
1036 err = native_window_api_disconnect(surface.window.get(),
1037 NATIVE_WINDOW_API_EGL);
1038 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1039 strerror(-err), err);
1040 err =
1041 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1042 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1043 strerror(-err), err);
1044
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001045 err = native_window_set_buffer_count(surface.window.get(), 0);
1046 if (err != 0) {
1047 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1048 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001049 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001050 }
1051
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301052 int swap_interval =
1053 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1054 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001055 if (err != 0) {
1056 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1057 // errors and translate them to valid Vulkan result codes?
1058 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1059 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001060 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001061 }
1062
Chris Forbesb8042d22017-01-18 18:07:05 +13001063 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1064 if (err != 0) {
1065 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1066 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001067 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001068 }
1069
1070 err = native_window_set_auto_refresh(surface.window.get(), false);
1071 if (err != 0) {
1072 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1073 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001074 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001075 }
1076
Jesse Halld7b994a2015-09-07 14:17:37 -07001077 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001078
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001079 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001080
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001081 err = native_window_set_buffers_format(surface.window.get(),
1082 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001083 if (err != 0) {
1084 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1085 // errors and translate them to valid Vulkan result codes?
1086 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001087 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001088 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001089 }
1090 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001091 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001092 if (err != 0) {
1093 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1094 // errors and translate them to valid Vulkan result codes?
1095 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001096 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001097 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001098 }
1099
Jesse Hall3dd678a2016-01-08 21:52:01 -08001100 err = native_window_set_buffers_dimensions(
1101 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1102 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001103 if (err != 0) {
1104 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1105 // errors and translate them to valid Vulkan result codes?
1106 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1107 create_info->imageExtent.width, create_info->imageExtent.height,
1108 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001109 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001110 }
1111
Jesse Hall178b6962016-02-24 15:39:50 -08001112 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1113 // applied during rendering. native_window_set_transform() expects the
1114 // inverse: the transform the app is requesting that the compositor perform
1115 // during composition. With native windows, pre-transform works by rendering
1116 // with the same transform the compositor is applying (as in Vulkan), but
1117 // then requesting the inverse transform, so that when the compositor does
1118 // it's job the two transforms cancel each other out and the compositor ends
1119 // up applying an identity transform to the app's buffer.
1120 err = native_window_set_buffers_transform(
1121 surface.window.get(),
1122 InvertTransformToNative(create_info->preTransform));
1123 if (err != 0) {
1124 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1125 // errors and translate them to valid Vulkan result codes?
1126 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1127 InvertTransformToNative(create_info->preTransform),
1128 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001129 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001130 }
1131
Jesse Hallf64ca122015-11-03 16:11:10 -08001132 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001133 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001134 if (err != 0) {
1135 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1136 // errors and translate them to valid Vulkan result codes?
1137 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1138 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001139 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001140 }
1141
Chris Forbes97ef4612017-03-30 19:37:50 +13001142 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1143 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1144 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1145 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1146 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1147 if (err != 0) {
1148 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1149 return VK_ERROR_SURFACE_LOST_KHR;
1150 }
1151 }
1152
1153 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1154 err = native_window_set_auto_refresh(surface.window.get(), true);
1155 if (err != 0) {
1156 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1157 return VK_ERROR_SURFACE_LOST_KHR;
1158 }
1159 }
1160
Jesse Halle6080bf2016-02-28 20:58:50 -08001161 int query_value;
1162 err = surface.window->query(surface.window.get(),
1163 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1164 &query_value);
1165 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001166 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1167 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -08001168 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1169 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001170 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001171 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001172 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001173 uint32_t num_images =
1174 (create_info->minImageCount - 1) + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001175
1176 // Lower layer insists that we have at least two buffers. This is wasteful
1177 // and we'd like to relax it in the shared case, but not all the pieces are
1178 // in place for that to work yet. Note we only lie to the lower layer-- we
1179 // don't want to give the app back a swapchain with extra images (which they
1180 // can't actually use!).
1181 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001182 if (err != 0) {
1183 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1184 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001185 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1186 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001187 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001188 }
1189
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001190 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001191 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001192 uint64_t consumer_usage, producer_usage;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001193 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001194 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1195 device, create_info->imageFormat, create_info->imageUsage,
1196 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001197 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001198 if (result != VK_SUCCESS) {
1199 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001200 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001201 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001202 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001203 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001204 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001205 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001206 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001207 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001208 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001209 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001210 if (result != VK_SUCCESS) {
1211 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001212 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001213 }
Jesse Hall70f93352015-11-04 09:41:31 -08001214 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001215 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1216
1217 bool createProtectedSwapchain = false;
1218 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1219 createProtectedSwapchain = true;
1220 native_usage |= BufferUsage::PROTECTED;
1221 }
1222 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001223 if (err != 0) {
1224 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1225 // errors and translate them to valid Vulkan result codes?
1226 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001227 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001228 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001229
1230 // -- Allocate our Swapchain object --
1231 // After this point, we must deallocate the swapchain on error.
1232
Jesse Hall1f91d392015-12-11 16:28:44 -08001233 void* mem = allocator->pfnAllocation(allocator->pUserData,
1234 sizeof(Swapchain), alignof(Swapchain),
1235 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001236 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001237 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001238 Swapchain* swapchain =
1239 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001240
1241 // -- Dequeue all buffers and create a VkImage for each --
1242 // Any failures during or after this must cancel the dequeued buffers.
1243
Chris Forbesb56287a2017-01-12 14:28:58 +13001244 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1245#pragma clang diagnostic push
1246#pragma clang diagnostic ignored "-Wold-style-cast"
1247 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1248#pragma clang diagnostic pop
1249 .pNext = nullptr,
1250 .usage = swapchain_image_usage,
1251 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001252 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001253#pragma clang diagnostic push
1254#pragma clang diagnostic ignored "-Wold-style-cast"
1255 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1256#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001257 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001258 };
1259 VkImageCreateInfo image_create = {
1260 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1261 .pNext = &image_native_buffer,
1262 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001263 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001264 .extent = {0, 0, 1},
1265 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001266 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001267 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001268 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001269 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001270 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001271 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001272 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001273 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1274 };
1275
Jesse Halld7b994a2015-09-07 14:17:37 -07001276 for (uint32_t i = 0; i < num_images; i++) {
1277 Swapchain::Image& img = swapchain->images[i];
1278
1279 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001280 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1281 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001282 if (err != 0) {
1283 // TODO(jessehall): Improve error reporting. Can we enumerate
1284 // possible errors and translate them to valid Vulkan result codes?
1285 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001286 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001287 break;
1288 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001289 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001290 img.dequeued = true;
1291
1292 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001293 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1294 static_cast<uint32_t>(img.buffer->height),
1295 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001296 image_native_buffer.handle = img.buffer->handle;
1297 image_native_buffer.stride = img.buffer->stride;
1298 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001299 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001300 android_convertGralloc0To1Usage(int(img.buffer->usage),
1301 &image_native_buffer.usage2.producer,
1302 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001303
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001304 ATRACE_BEGIN("dispatch.CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001305 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001306 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001307 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001308 if (result != VK_SUCCESS) {
1309 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1310 break;
1311 }
1312 }
1313
1314 // -- Cancel all buffers, returning them to the queue --
1315 // If an error occurred before, also destroy the VkImage and release the
1316 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1317 //
1318 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1319 // but not the non-error path. Should refactor/unify.
Chris Forbes31b85c22018-05-29 15:03:28 -07001320 for (uint32_t i = 0; i < num_images; i++) {
1321 Swapchain::Image& img = swapchain->images[i];
1322 if (img.dequeued) {
1323 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001324 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1325 img.dequeue_fence);
1326 img.dequeue_fence = -1;
1327 img.dequeued = false;
1328 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001329 }
1330 if (result != VK_SUCCESS) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001331 if (img.image) {
1332 ATRACE_BEGIN("dispatch.DestroyImage");
Chris Forbes31b85c22018-05-29 15:03:28 -07001333 dispatch.DestroyImage(device, img.image, nullptr);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001334 ATRACE_END();
1335 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001336 }
1337 }
1338
1339 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001340 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001341 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001342 return result;
1343 }
1344
Jesse Halldc225072016-05-30 22:40:14 -07001345 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1346 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001347 return VK_SUCCESS;
1348}
1349
Jesse Halle1b12782015-11-30 11:27:32 -08001350VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001351void DestroySwapchainKHR(VkDevice device,
1352 VkSwapchainKHR swapchain_handle,
1353 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001354 ATRACE_CALL();
1355
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001356 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001357 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001358 if (!swapchain)
1359 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001360 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1361 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001362
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001363 if (swapchain->frame_timestamps_enabled) {
1364 native_window_enable_frame_timestamps(window, false);
1365 }
Jesse Halldc225072016-05-30 22:40:14 -07001366 for (uint32_t i = 0; i < swapchain->num_images; i++)
1367 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001368 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001369 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001370 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001371 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001372 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001373 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001374}
1375
Jesse Halle1b12782015-11-30 11:27:32 -08001376VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001377VkResult GetSwapchainImagesKHR(VkDevice,
1378 VkSwapchainKHR swapchain_handle,
1379 uint32_t* count,
1380 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001381 ATRACE_CALL();
1382
Jesse Halld7b994a2015-09-07 14:17:37 -07001383 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001384 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1385 "getting images for non-active swapchain 0x%" PRIx64
1386 "; only dequeued image handles are valid",
1387 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001388 VkResult result = VK_SUCCESS;
1389 if (images) {
1390 uint32_t n = swapchain.num_images;
1391 if (*count < swapchain.num_images) {
1392 n = *count;
1393 result = VK_INCOMPLETE;
1394 }
1395 for (uint32_t i = 0; i < n; i++)
1396 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001397 *count = n;
1398 } else {
1399 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001400 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001401 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001402}
1403
Jesse Halle1b12782015-11-30 11:27:32 -08001404VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001405VkResult AcquireNextImageKHR(VkDevice device,
1406 VkSwapchainKHR swapchain_handle,
1407 uint64_t timeout,
1408 VkSemaphore semaphore,
1409 VkFence vk_fence,
1410 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001411 ATRACE_CALL();
1412
Jesse Halld7b994a2015-09-07 14:17:37 -07001413 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001414 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001415 VkResult result;
1416 int err;
1417
Jesse Halldc225072016-05-30 22:40:14 -07001418 if (swapchain.surface.swapchain_handle != swapchain_handle)
1419 return VK_ERROR_OUT_OF_DATE_KHR;
1420
Jesse Halld7b994a2015-09-07 14:17:37 -07001421 ALOGW_IF(
1422 timeout != UINT64_MAX,
1423 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1424
Chris Forbesc88409c2017-03-30 19:47:37 +13001425 if (swapchain.shared) {
1426 // In shared mode, we keep the buffer dequeued all the time, so we don't
1427 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1428 // the semaphore and fence passed to us will be signalled.
1429 *image_index = 0;
1430 result = GetData(device).driver.AcquireImageANDROID(
1431 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1432 return result;
1433 }
1434
Jesse Halld7b994a2015-09-07 14:17:37 -07001435 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001436 int fence_fd;
1437 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001438 if (err != 0) {
1439 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1440 // errors and translate them to valid Vulkan result codes?
1441 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001442 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001443 }
1444
1445 uint32_t idx;
1446 for (idx = 0; idx < swapchain.num_images; idx++) {
1447 if (swapchain.images[idx].buffer.get() == buffer) {
1448 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001449 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001450 break;
1451 }
1452 }
1453 if (idx == swapchain.num_images) {
1454 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001455 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001456 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001457 }
1458
1459 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001460 if (fence_fd != -1) {
1461 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001462 if (fence_clone == -1) {
1463 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1464 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001465 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001466 }
1467 }
1468
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001469 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001470 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001471 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001472 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1473 // even if the call fails. We could close it ourselves on failure, but
1474 // that would create a race condition if the driver closes it on a
1475 // failure path: some other thread might create an fd with the same
1476 // number between the time the driver closes it and the time we close
1477 // it. We must assume one of: the driver *always* closes it even on
1478 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001479 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001480 swapchain.images[idx].dequeued = false;
1481 swapchain.images[idx].dequeue_fence = -1;
1482 return result;
1483 }
1484
1485 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001486 return VK_SUCCESS;
1487}
1488
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001489VKAPI_ATTR
1490VkResult AcquireNextImage2KHR(VkDevice device,
1491 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1492 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001493 ATRACE_CALL();
1494
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001495 // TODO: this should actually be the other way around and this function
1496 // should handle any additional structures that get passed in
1497 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1498 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1499 pAcquireInfo->fence, pImageIndex);
1500}
1501
Jesse Halldc225072016-05-30 22:40:14 -07001502static VkResult WorstPresentResult(VkResult a, VkResult b) {
1503 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1504 // (in spec version 1.0.14).
1505 static const VkResult kWorstToBest[] = {
1506 VK_ERROR_DEVICE_LOST,
1507 VK_ERROR_SURFACE_LOST_KHR,
1508 VK_ERROR_OUT_OF_DATE_KHR,
1509 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1510 VK_ERROR_OUT_OF_HOST_MEMORY,
1511 VK_SUBOPTIMAL_KHR,
1512 };
1513 for (auto result : kWorstToBest) {
1514 if (a == result || b == result)
1515 return result;
1516 }
1517 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1518 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1519 return a != VK_SUCCESS ? a : b;
1520}
1521
Jesse Halle1b12782015-11-30 11:27:32 -08001522VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001523VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001524 ATRACE_CALL();
1525
Jesse Halld7b994a2015-09-07 14:17:37 -07001526 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1527 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1528 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001529
Jesse Halldc225072016-05-30 22:40:14 -07001530 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001531 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001532 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001533
Ian Elliottcb351132016-12-13 10:30:40 -07001534 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001535 const VkPresentRegionsKHR* present_regions = nullptr;
1536 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001537 const VkPresentRegionsKHR* next =
1538 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1539 while (next) {
1540 switch (next->sType) {
1541 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1542 present_regions = next;
1543 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001544 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001545 present_times =
1546 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1547 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001548 default:
1549 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1550 next->sType);
1551 break;
1552 }
1553 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1554 }
1555 ALOGV_IF(
1556 present_regions &&
1557 present_regions->swapchainCount != present_info->swapchainCount,
1558 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001559 ALOGV_IF(present_times &&
1560 present_times->swapchainCount != present_info->swapchainCount,
1561 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1562 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001563 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001564 (present_regions) ? present_regions->pRegions : nullptr;
1565 const VkPresentTimeGOOGLE* times =
1566 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001567 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001568 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001569 uint32_t nrects = 0;
1570
Jesse Halld7b994a2015-09-07 14:17:37 -07001571 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1572 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001573 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001574 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001575 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001576 const VkPresentRegionKHR* region =
1577 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001578 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001579 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001580 VkResult result;
1581 int err;
1582
Jesse Halld7b994a2015-09-07 14:17:37 -07001583 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001584 result = dispatch.QueueSignalReleaseImageANDROID(
1585 queue, present_info->waitSemaphoreCount,
1586 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001587 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001588 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001589 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001590 }
1591
Jesse Halldc225072016-05-30 22:40:14 -07001592 if (swapchain.surface.swapchain_handle ==
1593 present_info->pSwapchains[sc]) {
1594 ANativeWindow* window = swapchain.surface.window.get();
1595 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001596 if (region) {
1597 // Process the incremental-present hint for this swapchain:
1598 uint32_t rcount = region->rectangleCount;
1599 if (rcount > nrects) {
1600 android_native_rect_t* new_rects =
1601 static_cast<android_native_rect_t*>(
1602 allocator->pfnReallocation(
1603 allocator->pUserData, rects,
1604 sizeof(android_native_rect_t) * rcount,
1605 alignof(android_native_rect_t),
1606 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1607 if (new_rects) {
1608 rects = new_rects;
1609 nrects = rcount;
1610 } else {
1611 rcount = 0; // Ignore the hint for this swapchain
1612 }
1613 }
1614 for (uint32_t r = 0; r < rcount; ++r) {
1615 if (region->pRectangles[r].layer > 0) {
1616 ALOGV(
1617 "vkQueuePresentKHR ignoring invalid layer "
1618 "(%u); using layer 0 instead",
1619 region->pRectangles[r].layer);
1620 }
1621 int x = region->pRectangles[r].offset.x;
1622 int y = region->pRectangles[r].offset.y;
1623 int width = static_cast<int>(
1624 region->pRectangles[r].extent.width);
1625 int height = static_cast<int>(
1626 region->pRectangles[r].extent.height);
1627 android_native_rect_t* cur_rect = &rects[r];
1628 cur_rect->left = x;
1629 cur_rect->top = y + height;
1630 cur_rect->right = x + width;
1631 cur_rect->bottom = y;
1632 }
1633 native_window_set_surface_damage(window, rects, rcount);
1634 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001635 if (time) {
1636 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001637 ALOGV(
1638 "Calling "
1639 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001640 native_window_enable_frame_timestamps(window, true);
1641 swapchain.frame_timestamps_enabled = true;
1642 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001643
1644 // Record the nativeFrameId so it can be later correlated to
1645 // this present.
1646 uint64_t nativeFrameId = 0;
1647 err = native_window_get_next_frame_id(
1648 window, &nativeFrameId);
1649 if (err != android::NO_ERROR) {
1650 ALOGE("Failed to get next native frame ID.");
1651 }
1652
1653 // Add a new timing record with the user's presentID and
1654 // the nativeFrameId.
1655 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1656 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001657 swapchain.timing.removeAt(0);
1658 }
1659 if (time->desiredPresentTime) {
1660 // Set the desiredPresentTime:
1661 ALOGV(
1662 "Calling "
1663 "native_window_set_buffers_timestamp(%" PRId64 ")",
1664 time->desiredPresentTime);
1665 native_window_set_buffers_timestamp(
1666 window,
1667 static_cast<int64_t>(time->desiredPresentTime));
1668 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001669 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001670
Jesse Halldc225072016-05-30 22:40:14 -07001671 err = window->queueBuffer(window, img.buffer.get(), fence);
1672 // queueBuffer always closes fence, even on error
1673 if (err != 0) {
1674 // TODO(jessehall): What now? We should probably cancel the
1675 // buffer, I guess?
1676 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1677 swapchain_result = WorstPresentResult(
1678 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1679 }
1680 if (img.dequeue_fence >= 0) {
1681 close(img.dequeue_fence);
1682 img.dequeue_fence = -1;
1683 }
1684 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001685
1686 // If the swapchain is in shared mode, immediately dequeue the
1687 // buffer so it can be presented again without an intervening
1688 // call to AcquireNextImageKHR. We expect to get the same buffer
1689 // back from every call to dequeueBuffer in this mode.
1690 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1691 ANativeWindowBuffer* buffer;
1692 int fence_fd;
1693 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1694 if (err != 0) {
1695 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1696 swapchain_result = WorstPresentResult(swapchain_result,
1697 VK_ERROR_SURFACE_LOST_KHR);
1698 }
1699 else if (img.buffer != buffer) {
1700 ALOGE("got wrong image back for shared swapchain");
1701 swapchain_result = WorstPresentResult(swapchain_result,
1702 VK_ERROR_SURFACE_LOST_KHR);
1703 }
1704 else {
1705 img.dequeue_fence = fence_fd;
1706 img.dequeued = true;
1707 }
1708 }
Jesse Halldc225072016-05-30 22:40:14 -07001709 }
1710 if (swapchain_result != VK_SUCCESS) {
1711 ReleaseSwapchainImage(device, window, fence, img);
1712 OrphanSwapchain(device, &swapchain);
1713 }
1714 } else {
1715 ReleaseSwapchainImage(device, nullptr, fence, img);
1716 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001717 }
1718
Jesse Halla9e57032015-11-30 01:03:10 -08001719 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001720 present_info->pResults[sc] = swapchain_result;
1721
1722 if (swapchain_result != final_result)
1723 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001724 }
Ian Elliottcb351132016-12-13 10:30:40 -07001725 if (rects) {
1726 allocator->pfnFree(allocator->pUserData, rects);
1727 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001728
1729 return final_result;
1730}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001731
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001732VKAPI_ATTR
1733VkResult GetRefreshCycleDurationGOOGLE(
1734 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001735 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001736 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001737 ATRACE_CALL();
1738
Ian Elliott62c48c92017-01-20 13:13:20 -07001739 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001740 VkResult result = VK_SUCCESS;
1741
Ian Elliott3568bfe2019-05-03 15:54:46 -06001742 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001743
1744 return result;
1745}
1746
1747VKAPI_ATTR
1748VkResult GetPastPresentationTimingGOOGLE(
1749 VkDevice,
1750 VkSwapchainKHR swapchain_handle,
1751 uint32_t* count,
1752 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001753 ATRACE_CALL();
1754
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001755 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1756 ANativeWindow* window = swapchain.surface.window.get();
1757 VkResult result = VK_SUCCESS;
1758
1759 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001760 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001761 native_window_enable_frame_timestamps(window, true);
1762 swapchain.frame_timestamps_enabled = true;
1763 }
1764
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001765 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001766 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1767 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001768 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001769 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001770 }
1771
1772 return result;
1773}
1774
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001775VKAPI_ATTR
1776VkResult GetSwapchainStatusKHR(
1777 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001778 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001779 ATRACE_CALL();
1780
Chris Forbes4e18ba82017-01-20 12:50:17 +13001781 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001782 VkResult result = VK_SUCCESS;
1783
Chris Forbes4e18ba82017-01-20 12:50:17 +13001784 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1785 return VK_ERROR_OUT_OF_DATE_KHR;
1786 }
1787
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001788 // TODO(chrisforbes): Implement this function properly
1789
1790 return result;
1791}
1792
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001793VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001794 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001795 uint32_t swapchainCount,
1796 const VkSwapchainKHR* pSwapchains,
1797 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001798 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001799
1800 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1801 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1802 if (!swapchain)
1803 continue;
1804
1805 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1806
1807 ANativeWindow* window = swapchain->surface.window.get();
1808
1809 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1810 const android_smpte2086_metadata smpteMetdata = {
1811 {vulkanMetadata.displayPrimaryRed.x,
1812 vulkanMetadata.displayPrimaryRed.y},
1813 {vulkanMetadata.displayPrimaryGreen.x,
1814 vulkanMetadata.displayPrimaryGreen.y},
1815 {vulkanMetadata.displayPrimaryBlue.x,
1816 vulkanMetadata.displayPrimaryBlue.y},
1817 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1818 vulkanMetadata.maxLuminance,
1819 vulkanMetadata.minLuminance};
1820 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1821
1822 const android_cta861_3_metadata cta8613Metadata = {
1823 vulkanMetadata.maxContentLightLevel,
1824 vulkanMetadata.maxFrameAverageLightLevel};
1825 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1826 }
1827
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001828 return;
1829}
1830
Yiwei Zhang0f475222019-04-11 19:38:00 -07001831static void InterceptBindImageMemory2(
1832 uint32_t bind_info_count,
1833 const VkBindImageMemoryInfo* bind_infos,
1834 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1835 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1836 out_native_buffers->clear();
1837 out_bind_infos->clear();
1838
1839 if (!bind_info_count)
1840 return;
1841
1842 std::unordered_set<uint32_t> intercepted_indexes;
1843
1844 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1845 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1846 bind_infos[idx].pNext);
1847 while (info &&
1848 info->sType !=
1849 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1850 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1851 info->pNext);
1852 }
1853
1854 if (!info)
1855 continue;
1856
1857 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1858 "swapchain handle must not be NULL");
1859 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1860 ALOG_ASSERT(
1861 info->imageIndex < swapchain->num_images,
1862 "imageIndex must be less than the number of images in swapchain");
1863
1864 ANativeWindowBuffer* buffer =
1865 swapchain->images[info->imageIndex].buffer.get();
1866 VkNativeBufferANDROID native_buffer = {
1867#pragma clang diagnostic push
1868#pragma clang diagnostic ignored "-Wold-style-cast"
1869 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1870#pragma clang diagnostic pop
1871 .pNext = bind_infos[idx].pNext,
1872 .handle = buffer->handle,
1873 .stride = buffer->stride,
1874 .format = buffer->format,
1875 .usage = int(buffer->usage),
1876 };
1877 // Reserve enough space to avoid letting re-allocation invalidate the
1878 // addresses of the elements inside.
1879 out_native_buffers->reserve(bind_info_count);
1880 out_native_buffers->emplace_back(native_buffer);
1881
1882 // Reserve the space now since we know how much is needed now.
1883 out_bind_infos->reserve(bind_info_count);
1884 out_bind_infos->emplace_back(bind_infos[idx]);
1885 out_bind_infos->back().pNext = &out_native_buffers->back();
1886
1887 intercepted_indexes.insert(idx);
1888 }
1889
1890 if (intercepted_indexes.empty())
1891 return;
1892
1893 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1894 if (intercepted_indexes.count(idx))
1895 continue;
1896 out_bind_infos->emplace_back(bind_infos[idx]);
1897 }
1898}
1899
Yiwei Zhang23143102019-04-10 18:24:05 -07001900VKAPI_ATTR
1901VkResult BindImageMemory2(VkDevice device,
1902 uint32_t bindInfoCount,
1903 const VkBindImageMemoryInfo* pBindInfos) {
1904 ATRACE_CALL();
1905
Yiwei Zhang0f475222019-04-11 19:38:00 -07001906 // out_native_buffers is for maintaining the lifecycle of the constructed
1907 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1908 std::vector<VkNativeBufferANDROID> out_native_buffers;
1909 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1910 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1911 &out_bind_infos);
1912 return GetData(device).driver.BindImageMemory2(
1913 device, bindInfoCount,
1914 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001915}
1916
1917VKAPI_ATTR
1918VkResult BindImageMemory2KHR(VkDevice device,
1919 uint32_t bindInfoCount,
1920 const VkBindImageMemoryInfo* pBindInfos) {
1921 ATRACE_CALL();
1922
Yiwei Zhang0f475222019-04-11 19:38:00 -07001923 std::vector<VkNativeBufferANDROID> out_native_buffers;
1924 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1925 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1926 &out_bind_infos);
1927 return GetData(device).driver.BindImageMemory2KHR(
1928 device, bindInfoCount,
1929 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001930}
1931
Chia-I Wu62262232016-03-26 07:06:44 +08001932} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001933} // namespace vulkan