blob: 32e19f7ba36363ab42dbba2f391bcd7d1af02111 [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
Jesse Halld7b994a2015-09-07 14:17:37 -070019#include <algorithm>
Jesse Halld7b994a2015-09-07 14:17:37 -070020
Jesse Hall79927812017-03-23 11:03:23 -070021#include <grallocusage/GrallocUsageConversion.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <log/log.h>
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -080023#include <ui/BufferQueueDefs.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070024#include <sync/sync.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>
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070028#include <system/window.h>
Daniel Kochf25f5bb2017-10-05 00:26:58 -040029#include <android/hardware/graphics/common/1.0/types.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070030
Chia-I Wu4a6a9162016-03-26 07:17:34 +080031#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070032
Daniel Kochf25f5bb2017-10-05 00:26:58 -040033using android::hardware::graphics::common::V1_0::BufferUsage;
34
Jesse Hall5ae3abb2015-10-08 14:00:22 -070035// TODO(jessehall): Currently we don't have a good error code for when a native
36// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
37// versions (post SDK 0.9) of the API/extension have a better error code.
38// When updating to that version, audit all error returns.
Chia-I Wu62262232016-03-26 07:06:44 +080039namespace vulkan {
40namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070041
Jesse Halld7b994a2015-09-07 14:17:37 -070042namespace {
43
Jesse Hall55bc0972016-02-23 16:43:29 -080044const VkSurfaceTransformFlagsKHR kSupportedTransforms =
45 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
46 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
47 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
48 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
49 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
50 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
51 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
52 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
53 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
54 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
55
56VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
57 // Native and Vulkan transforms are isomorphic, but are represented
58 // differently. Vulkan transforms are built up of an optional horizontal
59 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
60 // transforms are built up from a horizontal flip, vertical flip, and
61 // 90-degree rotation, all optional but always in that order.
62
63 // TODO(jessehall): For now, only support pure rotations, not
64 // flip or flip-and-rotate, until I have more time to test them and build
65 // sample code. As far as I know we never actually use anything besides
66 // pure rotations anyway.
67
68 switch (native) {
69 case 0: // 0x0
70 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
71 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
72 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
73 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
74 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
75 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
76 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
77 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
78 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
79 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
80 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
81 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
82 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
83 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
84 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
85 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
86 default:
87 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
88 }
89}
90
Jesse Hall178b6962016-02-24 15:39:50 -080091int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
92 switch (transform) {
93 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
94 return NATIVE_WINDOW_TRANSFORM_ROT_270;
95 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
96 return NATIVE_WINDOW_TRANSFORM_ROT_180;
97 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
98 return NATIVE_WINDOW_TRANSFORM_ROT_90;
99 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
100 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
101 // return NATIVE_WINDOW_TRANSFORM_FLIP_H;
102 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
103 // return NATIVE_WINDOW_TRANSFORM_FLIP_H |
104 // NATIVE_WINDOW_TRANSFORM_ROT_90;
105 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
106 // return NATIVE_WINDOW_TRANSFORM_FLIP_V;
107 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
108 // return NATIVE_WINDOW_TRANSFORM_FLIP_V |
109 // NATIVE_WINDOW_TRANSFORM_ROT_90;
110 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
111 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
112 default:
113 return 0;
114 }
115}
116
Ian Elliott8a977262017-01-19 09:05:58 -0700117class TimingInfo {
118 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800119 TimingInfo() = default;
120 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700121 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800122 native_frame_id_(nativeFrameId) {}
123 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700124 return (timestamp_desired_present_time_ !=
125 NATIVE_WINDOW_TIMESTAMP_PENDING &&
126 timestamp_actual_present_time_ !=
127 NATIVE_WINDOW_TIMESTAMP_PENDING &&
128 timestamp_render_complete_time_ !=
129 NATIVE_WINDOW_TIMESTAMP_PENDING &&
130 timestamp_composition_latch_time_ !=
131 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700132 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700133 void calculate(int64_t rdur) {
134 bool anyTimestampInvalid =
135 (timestamp_actual_present_time_ ==
136 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
137 (timestamp_render_complete_time_ ==
138 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
139 (timestamp_composition_latch_time_ ==
140 NATIVE_WINDOW_TIMESTAMP_INVALID);
141 if (anyTimestampInvalid) {
142 ALOGE("Unexpectedly received invalid timestamp.");
143 vals_.actualPresentTime = 0;
144 vals_.earliestPresentTime = 0;
145 vals_.presentMargin = 0;
146 return;
147 }
148
149 vals_.actualPresentTime =
150 static_cast<uint64_t>(timestamp_actual_present_time_);
151 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700152 timestamp_render_complete_time_);
153 // Calculate vals_.earliestPresentTime, and potentially adjust
154 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
155 // is vals_.actualPresentTime. If we can subtract rdur (the duration
156 // of a refresh cycle) from vals_.earliestPresentTime (and also from
157 // vals_.presentMargin) and still leave a positive margin, then we can
158 // report to the application that it could have presented earlier than
159 // it did (per the extension specification). If for some reason, we
160 // can do this subtraction repeatedly, we do, since
161 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700162 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700163 while ((margin > rdur) &&
164 ((early_time - rdur) > timestamp_composition_latch_time_)) {
165 early_time -= rdur;
166 margin -= rdur;
167 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700168 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
169 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700170 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800171 void get_values(VkPastPresentationTimingGOOGLE* values) const {
172 *values = vals_;
173 }
Ian Elliott8a977262017-01-19 09:05:58 -0700174
175 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800176 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700177
Brian Anderson1049d1d2016-12-16 17:25:57 -0800178 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700179 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
180 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
181 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
182 int64_t timestamp_composition_latch_time_
183 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700184};
185
Jesse Halld7b994a2015-09-07 14:17:37 -0700186// ----------------------------------------------------------------------------
187
Jesse Hall1356b0d2015-11-23 17:24:58 -0800188struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800189 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700190 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700191 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800192};
193
194VkSurfaceKHR HandleFromSurface(Surface* surface) {
195 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
196}
197
198Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800199 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800200}
201
Ian Elliott8a977262017-01-19 09:05:58 -0700202// Maximum number of TimingInfo structs to keep per swapchain:
203enum { MAX_TIMING_INFOS = 10 };
204// Minimum number of frames to look for in the past (so we don't cause
205// syncronous requests to Surface Flinger):
206enum { MIN_NUM_FRAMES_AGO = 5 };
207
Jesse Hall1356b0d2015-11-23 17:24:58 -0800208struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700209 Swapchain(Surface& surface_,
210 uint32_t num_images_,
211 VkPresentModeKHR present_mode)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700212 : surface(surface_),
213 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700214 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
Chris Forbesf8835642017-03-30 19:31:40 +1300215 frame_timestamps_enabled(false),
216 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
217 present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700218 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700219 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700220 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700221 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700222 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800223
224 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700225 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700226 bool mailbox_mode;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700227 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700228 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300229 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700230
231 struct Image {
232 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
233 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800234 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700235 // The fence is only valid when the buffer is dequeued, and should be
236 // -1 any other time. When valid, we own the fd, and must ensure it is
237 // closed: either by closing it explicitly when queueing the buffer,
238 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
239 int dequeue_fence;
240 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800241 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700242
Brian Anderson1049d1d2016-12-16 17:25:57 -0800243 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700244};
245
246VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
247 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
248}
249
250Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800251 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700252}
253
Jesse Halldc225072016-05-30 22:40:14 -0700254void ReleaseSwapchainImage(VkDevice device,
255 ANativeWindow* window,
256 int release_fence,
257 Swapchain::Image& image) {
258 ALOG_ASSERT(release_fence == -1 || image.dequeued,
259 "ReleaseSwapchainImage: can't provide a release fence for "
260 "non-dequeued images");
261
262 if (image.dequeued) {
263 if (release_fence >= 0) {
264 // We get here from vkQueuePresentKHR. The application is
265 // responsible for creating an execution dependency chain from
266 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
267 // (release_fence), so we can drop the dequeue_fence here.
268 if (image.dequeue_fence >= 0)
269 close(image.dequeue_fence);
270 } else {
271 // We get here during swapchain destruction, or various serious
272 // error cases e.g. when we can't create the release_fence during
273 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
274 // have already signalled, since the swapchain images are supposed
275 // to be idle before the swapchain is destroyed. In error cases,
276 // there may be rendering in flight to the image, but since we
277 // weren't able to create a release_fence, waiting for the
278 // dequeue_fence is about the best we can do.
279 release_fence = image.dequeue_fence;
280 }
281 image.dequeue_fence = -1;
282
283 if (window) {
284 window->cancelBuffer(window, image.buffer.get(), release_fence);
285 } else {
286 if (release_fence >= 0) {
287 sync_wait(release_fence, -1 /* forever */);
288 close(release_fence);
289 }
290 }
291
292 image.dequeued = false;
293 }
294
295 if (image.image) {
296 GetData(device).driver.DestroyImage(device, image.image, nullptr);
297 image.image = VK_NULL_HANDLE;
298 }
299
300 image.buffer.clear();
301}
302
303void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
304 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
305 return;
Jesse Halldc225072016-05-30 22:40:14 -0700306 for (uint32_t i = 0; i < swapchain->num_images; i++) {
307 if (!swapchain->images[i].dequeued)
308 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
309 }
310 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700311 swapchain->timing.clear();
312}
313
314uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800315 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
316 return 0;
317 }
Ian Elliott8a977262017-01-19 09:05:58 -0700318
Brian Anderson1049d1d2016-12-16 17:25:57 -0800319 uint32_t num_ready = 0;
320 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
321 for (uint32_t i = 0; i < num_timings; i++) {
322 TimingInfo& ti = swapchain.timing.editItemAt(i);
323 if (ti.ready()) {
324 // This TimingInfo is ready to be reported to the user. Add it
325 // to the num_ready.
326 num_ready++;
327 continue;
328 }
329 // This TimingInfo is not yet ready to be reported to the user,
330 // and so we should look for any available timestamps that
331 // might make it ready.
332 int64_t desired_present_time = 0;
333 int64_t render_complete_time = 0;
334 int64_t composition_latch_time = 0;
335 int64_t actual_present_time = 0;
336 // Obtain timestamps:
337 int ret = native_window_get_frame_timestamps(
338 swapchain.surface.window.get(), ti.native_frame_id_,
339 &desired_present_time, &render_complete_time,
340 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700341 nullptr, //&first_composition_start_time,
342 nullptr, //&last_composition_start_time,
343 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800344 // TODO(ianelliott): Maybe ask if this one is
345 // supported, at startup time (since it may not be
346 // supported):
347 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700348 nullptr, //&dequeue_ready_time,
349 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800350
351 if (ret != android::NO_ERROR) {
352 continue;
353 }
354
355 // Record the timestamp(s) we received, and then see if this TimingInfo
356 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700357 ti.timestamp_desired_present_time_ = desired_present_time;
358 ti.timestamp_actual_present_time_ = actual_present_time;
359 ti.timestamp_render_complete_time_ = render_complete_time;
360 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800361
362 if (ti.ready()) {
363 // The TimingInfo has received enough timestamps, and should now
364 // use those timestamps to calculate the info that should be
365 // reported to the user:
366 ti.calculate(swapchain.refresh_duration);
367 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700368 }
369 }
370 return num_ready;
371}
372
373// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
374void copy_ready_timings(Swapchain& swapchain,
375 uint32_t* count,
376 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800377 if (swapchain.timing.empty()) {
378 *count = 0;
379 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700380 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800381
382 size_t last_ready = swapchain.timing.size() - 1;
383 while (!swapchain.timing[last_ready].ready()) {
384 if (last_ready == 0) {
385 *count = 0;
386 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700387 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800388 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700389 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800390
391 uint32_t num_copied = 0;
392 size_t num_to_remove = 0;
393 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
394 const TimingInfo& ti = swapchain.timing[i];
395 if (ti.ready()) {
396 ti.get_values(&timings[num_copied]);
397 num_copied++;
398 }
399 num_to_remove++;
400 }
401
402 // Discard old frames that aren't ready if newer frames are ready.
403 // We don't expect to get the timing info for those old frames.
404 swapchain.timing.removeItemsAt(0, num_to_remove);
405
Ian Elliott8a977262017-01-19 09:05:58 -0700406 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700407}
408
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700409android_pixel_format GetNativePixelFormat(VkFormat format) {
410 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
411 switch (format) {
412 case VK_FORMAT_R8G8B8A8_UNORM:
413 case VK_FORMAT_R8G8B8A8_SRGB:
414 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
415 break;
416 case VK_FORMAT_R5G6B5_UNORM_PACK16:
417 native_format = HAL_PIXEL_FORMAT_RGB_565;
418 break;
419 case VK_FORMAT_R16G16B16A16_SFLOAT:
420 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
421 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800422 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700423 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
424 break;
425 default:
426 ALOGV("unsupported swapchain format %d", format);
427 break;
428 }
429 return native_format;
430}
431
432android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
433 switch (colorspace) {
434 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
435 return HAL_DATASPACE_V0_SRGB;
436 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
437 return HAL_DATASPACE_DISPLAY_P3;
438 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
439 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600440 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
441 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700442 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
443 return HAL_DATASPACE_DCI_P3_LINEAR;
444 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
445 return HAL_DATASPACE_DCI_P3;
446 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
447 return HAL_DATASPACE_V0_SRGB_LINEAR;
448 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
449 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600450 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
451 return HAL_DATASPACE_BT2020_LINEAR;
452 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700453 return static_cast<android_dataspace>(
454 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
455 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600456 case VK_COLOR_SPACE_DOLBYVISION_EXT:
457 return static_cast<android_dataspace>(
458 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
459 HAL_DATASPACE_RANGE_FULL);
460 case VK_COLOR_SPACE_HDR10_HLG_EXT:
461 return static_cast<android_dataspace>(
462 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
463 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700464 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
465 return static_cast<android_dataspace>(
466 HAL_DATASPACE_STANDARD_ADOBE_RGB |
467 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
468 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
469 return HAL_DATASPACE_ADOBE_RGB;
470
471 // Pass through is intended to allow app to provide data that is passed
472 // to the display system without modification.
473 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
474 return HAL_DATASPACE_ARBITRARY;
475
476 default:
477 // This indicates that we don't know about the
478 // dataspace specified and we should indicate that
479 // it's unsupported
480 return HAL_DATASPACE_UNKNOWN;
481 }
482}
483
Jesse Halld7b994a2015-09-07 14:17:37 -0700484} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700485
Jesse Halle1b12782015-11-30 11:27:32 -0800486VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800487VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800488 VkInstance instance,
489 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
490 const VkAllocationCallbacks* allocator,
491 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800492 ATRACE_CALL();
493
Jesse Hall1f91d392015-12-11 16:28:44 -0800494 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800495 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800496 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
497 alignof(Surface),
498 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800499 if (!mem)
500 return VK_ERROR_OUT_OF_HOST_MEMORY;
501 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700502
Chia-I Wue8e689f2016-04-18 08:21:31 +0800503 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700504 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700505 int err = native_window_get_consumer_usage(surface->window.get(),
506 &surface->consumer_usage);
507 if (err != android::NO_ERROR) {
508 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
509 strerror(-err), err);
510 surface->~Surface();
511 allocator->pfnFree(allocator->pUserData, surface);
512 return VK_ERROR_INITIALIZATION_FAILED;
513 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700514
Jesse Hall1356b0d2015-11-23 17:24:58 -0800515 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
Yiwei Zhang6435b322018-05-08 11:12:17 -0700516 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800517 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
518 if (err != 0) {
519 // TODO(jessehall): Improve error reporting. Can we enumerate possible
520 // errors and translate them to valid Vulkan result codes?
521 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
522 err);
523 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800524 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700525 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800526 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700527
Jesse Hall1356b0d2015-11-23 17:24:58 -0800528 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700529 return VK_SUCCESS;
530}
531
Jesse Halle1b12782015-11-30 11:27:32 -0800532VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800533void DestroySurfaceKHR(VkInstance instance,
534 VkSurfaceKHR surface_handle,
535 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800536 ATRACE_CALL();
537
Jesse Hall1356b0d2015-11-23 17:24:58 -0800538 Surface* surface = SurfaceFromHandle(surface_handle);
539 if (!surface)
540 return;
541 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700542 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700543 "destroyed VkSurfaceKHR 0x%" PRIx64
544 " has active VkSwapchainKHR 0x%" PRIx64,
545 reinterpret_cast<uint64_t>(surface_handle),
546 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800547 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800548 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800549 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800550 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800551}
552
Jesse Halle1b12782015-11-30 11:27:32 -0800553VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800554VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
555 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700556 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800557 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800558 ATRACE_CALL();
559
Yiwei Zhang6435b322018-05-08 11:12:17 -0700560 const Surface* surface = SurfaceFromHandle(surface_handle);
561 if (!surface) {
562 return VK_ERROR_SURFACE_LOST_KHR;
563 }
564 const ANativeWindow* window = surface->window.get();
565
566 int query_value;
567 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
568 if (err != 0 || query_value < 0) {
569 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
570 strerror(-err), err, query_value);
571 return VK_ERROR_SURFACE_LOST_KHR;
572 }
573
574 android_pixel_format native_format =
575 static_cast<android_pixel_format>(query_value);
576
577 bool format_supported = false;
578 switch (native_format) {
579 case HAL_PIXEL_FORMAT_RGBA_8888:
580 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700581 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800582 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700583 format_supported = true;
584 break;
585 default:
586 break;
587 }
588
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700589 *supported = static_cast<VkBool32>(
590 format_supported || (surface->consumer_usage &
591 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
592 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700593
Jesse Halla6429252015-11-29 18:59:42 -0800594 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800595}
596
Jesse Halle1b12782015-11-30 11:27:32 -0800597VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800598VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800599 VkPhysicalDevice /*pdev*/,
600 VkSurfaceKHR surface,
601 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800602 ATRACE_CALL();
603
Jesse Halld7b994a2015-09-07 14:17:37 -0700604 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800605 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700606
607 int width, height;
608 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
609 if (err != 0) {
610 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
611 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700612 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700613 }
614 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
615 if (err != 0) {
616 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
617 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700618 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700619 }
620
Jesse Hall55bc0972016-02-23 16:43:29 -0800621 int transform_hint;
622 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
623 if (err != 0) {
624 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
625 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700626 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800627 }
628
Jesse Halld7b994a2015-09-07 14:17:37 -0700629 // TODO(jessehall): Figure out what the min/max values should be.
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800630 int max_buffer_count;
631 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
632 if (err != 0) {
633 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
634 strerror(-err), err);
635 return VK_ERROR_SURFACE_LOST_KHR;
636 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700637 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800638 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700639
Jesse Hallfe2662d2016-02-09 13:26:59 -0800640 capabilities->currentExtent =
641 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
642
Jesse Halld7b994a2015-09-07 14:17:37 -0700643 // TODO(jessehall): Figure out what the max extent should be. Maximum
644 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800645 capabilities->minImageExtent = VkExtent2D{1, 1};
646 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700647
Jesse Hallfe2662d2016-02-09 13:26:59 -0800648 capabilities->maxImageArrayLayers = 1;
649
Jesse Hall55bc0972016-02-23 16:43:29 -0800650 capabilities->supportedTransforms = kSupportedTransforms;
651 capabilities->currentTransform =
652 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700653
Jesse Hallfe2662d2016-02-09 13:26:59 -0800654 // On Android, window composition is a WindowManager property, not something
655 // associated with the bufferqueue. It can't be changed from here.
656 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700657
658 // TODO(jessehall): I think these are right, but haven't thought hard about
659 // it. Do we need to query the driver for support of any of these?
660 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700661 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
662 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800663 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800664 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
665 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
666 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700667 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
668
Jesse Hallb1352bc2015-09-04 16:12:33 -0700669 return VK_SUCCESS;
670}
671
Jesse Halle1b12782015-11-30 11:27:32 -0800672VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700673VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
674 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800675 uint32_t* count,
676 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800677 ATRACE_CALL();
678
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700679 const InstanceData& instance_data = GetData(pdev);
680
Jesse Hall1356b0d2015-11-23 17:24:58 -0800681 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
682 // a new gralloc method to query whether a (format, usage) pair is
683 // supported, and check that for each gralloc format that corresponds to a
684 // Vulkan format. Shorter term, just add a few more formats to the ones
685 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700686
687 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700688 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
689 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
690 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700691 };
692 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700693 uint32_t total_num_formats = kNumFormats;
694
695 bool wide_color_support = false;
696 Surface& surface = *SurfaceFromHandle(surface_handle);
697 int err = native_window_get_wide_color_support(surface.window.get(),
698 &wide_color_support);
699 if (err) {
700 // Not allowed to return a more sensible error code, so do this
701 return VK_ERROR_OUT_OF_HOST_MEMORY;
702 }
703 ALOGV("wide_color_support is: %d", wide_color_support);
704 wide_color_support =
705 wide_color_support &&
706 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
707
708 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600709 {VK_FORMAT_R8G8B8A8_UNORM,
710 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
711 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700712 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700713 {VK_FORMAT_R16G16B16A16_SFLOAT,
714 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
715 {VK_FORMAT_R16G16B16A16_SFLOAT,
716 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800717 {VK_FORMAT_A2B10G10R10_UNORM_PACK32,
718 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700719 };
720 const uint32_t kNumWideColorFormats =
721 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
722 if (wide_color_support) {
723 total_num_formats += kNumWideColorFormats;
724 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700725
726 VkResult result = VK_SUCCESS;
727 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700728 uint32_t out_count = 0;
729 uint32_t transfer_count = 0;
730 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700731 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700732 transfer_count = std::min(*count, kNumFormats);
733 std::copy(kFormats, kFormats + transfer_count, formats);
734 out_count += transfer_count;
735 if (wide_color_support) {
736 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
737 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
738 formats + out_count);
739 out_count += transfer_count;
740 }
741 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700742 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700743 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700744 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700745 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700746}
747
Jesse Halle1b12782015-11-30 11:27:32 -0800748VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300749VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
750 VkPhysicalDevice physicalDevice,
751 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
752 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800753 ATRACE_CALL();
754
Chris Forbes2452cf72017-03-16 16:30:17 +1300755 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
756 physicalDevice, pSurfaceInfo->surface,
757 &pSurfaceCapabilities->surfaceCapabilities);
758
Chris Forbes06bc0092017-03-16 16:46:05 +1300759 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
760 while (caps->pNext) {
761 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
762
763 switch (caps->sType) {
764 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
765 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
766 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
767 caps);
768 // Claim same set of usage flags are supported for
769 // shared present modes as for other modes.
770 shared_caps->sharedPresentSupportedUsageFlags =
771 pSurfaceCapabilities->surfaceCapabilities
772 .supportedUsageFlags;
773 } break;
774
775 default:
776 // Ignore all other extension structs
777 break;
778 }
779 }
780
Chris Forbes2452cf72017-03-16 16:30:17 +1300781 return result;
782}
783
784VKAPI_ATTR
785VkResult GetPhysicalDeviceSurfaceFormats2KHR(
786 VkPhysicalDevice physicalDevice,
787 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
788 uint32_t* pSurfaceFormatCount,
789 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800790 ATRACE_CALL();
791
Chris Forbes2452cf72017-03-16 16:30:17 +1300792 if (!pSurfaceFormats) {
793 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
794 pSurfaceInfo->surface,
795 pSurfaceFormatCount, nullptr);
796 } else {
797 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
798 // after the call.
799 android::Vector<VkSurfaceFormatKHR> surface_formats;
800 surface_formats.resize(*pSurfaceFormatCount);
801 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
802 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
803 &surface_formats.editItemAt(0));
804
805 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
806 // marshal results individually due to stride difference.
807 // completely ignore any chained extension structs.
808 uint32_t formats_to_marshal = *pSurfaceFormatCount;
809 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
810 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
811 }
812 }
813
814 return result;
815 }
816}
817
818VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300819VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800820 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800821 uint32_t* count,
822 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800823 ATRACE_CALL();
824
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800825 int err;
826 int query_value;
827 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
828
829 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
830 if (err != 0 || query_value < 0) {
831 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
832 strerror(-err), err, query_value);
833 return VK_ERROR_SURFACE_LOST_KHR;
834 }
835 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
836
837 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
838 if (err != 0 || query_value < 0) {
839 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
840 strerror(-err), err, query_value);
841 return VK_ERROR_SURFACE_LOST_KHR;
842 }
843 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
844
Chris Forbese8d79a62017-02-22 12:49:18 +1300845 android::Vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800846 if (min_undequeued_buffers + 1 < max_buffer_count)
847 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300848 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
849
850 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
851 if (QueryPresentationProperties(pdev, &present_properties)) {
852 if (present_properties.sharedImage) {
853 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
854 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
855 }
856 }
857
858 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700859
860 VkResult result = VK_SUCCESS;
861 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300862 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700863 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300864 *count = std::min(*count, num_modes);
865 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700866 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300867 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700868 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700869 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700870}
871
Jesse Halle1b12782015-11-30 11:27:32 -0800872VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400873VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600874 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400875 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800876 ATRACE_CALL();
877
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400878 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
879 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
880 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
881 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
882 pDeviceGroupPresentCapabilities->sType);
883
884 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
885 sizeof(pDeviceGroupPresentCapabilities->presentMask));
886
887 // assume device group of size 1
888 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
889 pDeviceGroupPresentCapabilities->modes =
890 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
891
892 return VK_SUCCESS;
893}
894
895VKAPI_ATTR
896VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600897 VkDevice,
898 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400899 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800900 ATRACE_CALL();
901
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400902 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
903 return VK_SUCCESS;
904}
905
906VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600907VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400908 VkSurfaceKHR surface,
909 uint32_t* pRectCount,
910 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800911 ATRACE_CALL();
912
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400913 if (!pRects) {
914 *pRectCount = 1;
915 } else {
916 uint32_t count = std::min(*pRectCount, 1u);
917 bool incomplete = *pRectCount < 1;
918
919 *pRectCount = count;
920
921 if (incomplete) {
922 return VK_INCOMPLETE;
923 }
924
925 int err;
926 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
927
928 int width = 0, height = 0;
929 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
930 if (err != 0) {
931 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
932 strerror(-err), err);
933 }
934 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
935 if (err != 0) {
936 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
937 strerror(-err), err);
938 }
939
940 // TODO: Return something better than "whole window"
941 pRects[0].offset.x = 0;
942 pRects[0].offset.y = 0;
943 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
944 static_cast<uint32_t>(height)};
945 }
946 return VK_SUCCESS;
947}
948
949VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800950VkResult CreateSwapchainKHR(VkDevice device,
951 const VkSwapchainCreateInfoKHR* create_info,
952 const VkAllocationCallbacks* allocator,
953 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800954 ATRACE_CALL();
955
Jesse Halld7b994a2015-09-07 14:17:37 -0700956 int err;
957 VkResult result = VK_SUCCESS;
958
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700959 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
960 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
961 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
962 " oldSwapchain=0x%" PRIx64,
963 reinterpret_cast<uint64_t>(create_info->surface),
964 create_info->minImageCount, create_info->imageFormat,
965 create_info->imageColorSpace, create_info->imageExtent.width,
966 create_info->imageExtent.height, create_info->imageUsage,
967 create_info->preTransform, create_info->presentMode,
968 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
969
Jesse Hall1f91d392015-12-11 16:28:44 -0800970 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800971 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800972
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700973 android_pixel_format native_pixel_format =
974 GetNativePixelFormat(create_info->imageFormat);
975 android_dataspace native_dataspace =
976 GetNativeDataspace(create_info->imageColorSpace);
977 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
978 ALOGE(
979 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
980 "failed: Unsupported color space",
981 create_info->imageColorSpace);
982 return VK_ERROR_INITIALIZATION_FAILED;
983 }
984
Jesse Hall42a9eec2016-06-03 12:39:49 -0700985 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700986 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800987 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700988 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700989 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800990 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700991 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +1300992 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300993 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
994 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700995 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800996 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700997
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700998 Surface& surface = *SurfaceFromHandle(create_info->surface);
999
Jesse Halldc225072016-05-30 22:40:14 -07001000 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001001 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001002 " because it already has active swapchain 0x%" PRIx64
1003 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1004 reinterpret_cast<uint64_t>(create_info->surface),
1005 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1006 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1007 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1008 }
1009 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1010 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1011
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001012 // -- Reset the native window --
1013 // The native window might have been used previously, and had its properties
1014 // changed from defaults. That will affect the answer we get for queries
1015 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1016 // attempt such queries.
1017
Jesse Halldc225072016-05-30 22:40:14 -07001018 // The native window only allows dequeueing all buffers before any have
1019 // been queued, since after that point at least one is assumed to be in
1020 // non-FREE state at any given time. Disconnecting and re-connecting
1021 // orphans the previous buffers, getting us back to the state where we can
1022 // dequeue all buffers.
1023 err = native_window_api_disconnect(surface.window.get(),
1024 NATIVE_WINDOW_API_EGL);
1025 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1026 strerror(-err), err);
1027 err =
1028 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1029 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1030 strerror(-err), err);
1031
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001032 err = native_window_set_buffer_count(surface.window.get(), 0);
1033 if (err != 0) {
1034 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1035 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001036 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001037 }
1038
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301039 int swap_interval =
1040 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1041 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001042 if (err != 0) {
1043 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1044 // errors and translate them to valid Vulkan result codes?
1045 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1046 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001047 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001048 }
1049
Chris Forbesb8042d22017-01-18 18:07:05 +13001050 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1051 if (err != 0) {
1052 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1053 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001054 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001055 }
1056
1057 err = native_window_set_auto_refresh(surface.window.get(), false);
1058 if (err != 0) {
1059 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1060 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001061 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001062 }
1063
Jesse Halld7b994a2015-09-07 14:17:37 -07001064 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001065
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001066 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001067
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001068 err = native_window_set_buffers_format(surface.window.get(),
1069 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001070 if (err != 0) {
1071 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1072 // errors and translate them to valid Vulkan result codes?
1073 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001074 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001075 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001076 }
1077 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001078 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001079 if (err != 0) {
1080 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1081 // errors and translate them to valid Vulkan result codes?
1082 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001083 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001084 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001085 }
1086
Jesse Hall3dd678a2016-01-08 21:52:01 -08001087 err = native_window_set_buffers_dimensions(
1088 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1089 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001090 if (err != 0) {
1091 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1092 // errors and translate them to valid Vulkan result codes?
1093 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1094 create_info->imageExtent.width, create_info->imageExtent.height,
1095 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001096 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001097 }
1098
Jesse Hall178b6962016-02-24 15:39:50 -08001099 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1100 // applied during rendering. native_window_set_transform() expects the
1101 // inverse: the transform the app is requesting that the compositor perform
1102 // during composition. With native windows, pre-transform works by rendering
1103 // with the same transform the compositor is applying (as in Vulkan), but
1104 // then requesting the inverse transform, so that when the compositor does
1105 // it's job the two transforms cancel each other out and the compositor ends
1106 // up applying an identity transform to the app's buffer.
1107 err = native_window_set_buffers_transform(
1108 surface.window.get(),
1109 InvertTransformToNative(create_info->preTransform));
1110 if (err != 0) {
1111 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1112 // errors and translate them to valid Vulkan result codes?
1113 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1114 InvertTransformToNative(create_info->preTransform),
1115 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001116 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001117 }
1118
Jesse Hallf64ca122015-11-03 16:11:10 -08001119 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001120 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001121 if (err != 0) {
1122 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1123 // errors and translate them to valid Vulkan result codes?
1124 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1125 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001126 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001127 }
1128
Chris Forbes97ef4612017-03-30 19:37:50 +13001129 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1130 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1131 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1132 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1133 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1134 if (err != 0) {
1135 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1136 return VK_ERROR_SURFACE_LOST_KHR;
1137 }
1138 }
1139
1140 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1141 err = native_window_set_auto_refresh(surface.window.get(), true);
1142 if (err != 0) {
1143 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1144 return VK_ERROR_SURFACE_LOST_KHR;
1145 }
1146 }
1147
Jesse Halle6080bf2016-02-28 20:58:50 -08001148 int query_value;
1149 err = surface.window->query(surface.window.get(),
1150 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1151 &query_value);
1152 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001153 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1154 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -08001155 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1156 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001157 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001158 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001159 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001160 uint32_t num_images =
1161 (create_info->minImageCount - 1) + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001162
1163 // Lower layer insists that we have at least two buffers. This is wasteful
1164 // and we'd like to relax it in the shared case, but not all the pieces are
1165 // in place for that to work yet. Note we only lie to the lower layer-- we
1166 // don't want to give the app back a swapchain with extra images (which they
1167 // can't actually use!).
1168 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001169 if (err != 0) {
1170 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1171 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001172 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1173 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001174 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001175 }
1176
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001177 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001178 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001179 uint64_t consumer_usage, producer_usage;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001180 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001181 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1182 device, create_info->imageFormat, create_info->imageUsage,
1183 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001184 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001185 if (result != VK_SUCCESS) {
1186 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001187 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001188 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001189 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001190 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001191 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001192 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001193 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001194 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001195 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001196 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001197 if (result != VK_SUCCESS) {
1198 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001199 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001200 }
Jesse Hall70f93352015-11-04 09:41:31 -08001201 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001202 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1203
1204 bool createProtectedSwapchain = false;
1205 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1206 createProtectedSwapchain = true;
1207 native_usage |= BufferUsage::PROTECTED;
1208 }
1209 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001210 if (err != 0) {
1211 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1212 // errors and translate them to valid Vulkan result codes?
1213 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001214 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001215 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001216
1217 // -- Allocate our Swapchain object --
1218 // After this point, we must deallocate the swapchain on error.
1219
Jesse Hall1f91d392015-12-11 16:28:44 -08001220 void* mem = allocator->pfnAllocation(allocator->pUserData,
1221 sizeof(Swapchain), alignof(Swapchain),
1222 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001223 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001224 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001225 Swapchain* swapchain =
1226 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001227
1228 // -- Dequeue all buffers and create a VkImage for each --
1229 // Any failures during or after this must cancel the dequeued buffers.
1230
Chris Forbesb56287a2017-01-12 14:28:58 +13001231 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1232#pragma clang diagnostic push
1233#pragma clang diagnostic ignored "-Wold-style-cast"
1234 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1235#pragma clang diagnostic pop
1236 .pNext = nullptr,
1237 .usage = swapchain_image_usage,
1238 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001239 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001240#pragma clang diagnostic push
1241#pragma clang diagnostic ignored "-Wold-style-cast"
1242 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1243#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001244 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001245 };
1246 VkImageCreateInfo image_create = {
1247 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1248 .pNext = &image_native_buffer,
1249 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001250 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001251 .extent = {0, 0, 1},
1252 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001253 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001254 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001255 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001256 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001257 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001258 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001259 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001260 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1261 };
1262
Jesse Halld7b994a2015-09-07 14:17:37 -07001263 for (uint32_t i = 0; i < num_images; i++) {
1264 Swapchain::Image& img = swapchain->images[i];
1265
1266 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001267 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1268 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001269 if (err != 0) {
1270 // TODO(jessehall): Improve error reporting. Can we enumerate
1271 // possible errors and translate them to valid Vulkan result codes?
1272 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001273 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001274 break;
1275 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001276 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001277 img.dequeued = true;
1278
1279 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001280 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1281 static_cast<uint32_t>(img.buffer->height),
1282 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001283 image_native_buffer.handle = img.buffer->handle;
1284 image_native_buffer.stride = img.buffer->stride;
1285 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001286 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001287 android_convertGralloc0To1Usage(int(img.buffer->usage),
1288 &image_native_buffer.usage2.producer,
1289 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001290
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001291 ATRACE_BEGIN("dispatch.CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001292 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001293 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001294 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001295 if (result != VK_SUCCESS) {
1296 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1297 break;
1298 }
1299 }
1300
1301 // -- Cancel all buffers, returning them to the queue --
1302 // If an error occurred before, also destroy the VkImage and release the
1303 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1304 //
1305 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1306 // but not the non-error path. Should refactor/unify.
Chris Forbes31b85c22018-05-29 15:03:28 -07001307 for (uint32_t i = 0; i < num_images; i++) {
1308 Swapchain::Image& img = swapchain->images[i];
1309 if (img.dequeued) {
1310 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001311 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1312 img.dequeue_fence);
1313 img.dequeue_fence = -1;
1314 img.dequeued = false;
1315 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001316 }
1317 if (result != VK_SUCCESS) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001318 if (img.image) {
1319 ATRACE_BEGIN("dispatch.DestroyImage");
Chris Forbes31b85c22018-05-29 15:03:28 -07001320 dispatch.DestroyImage(device, img.image, nullptr);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001321 ATRACE_END();
1322 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001323 }
1324 }
1325
1326 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001327 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001328 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001329 return result;
1330 }
1331
Jesse Halldc225072016-05-30 22:40:14 -07001332 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1333 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001334 return VK_SUCCESS;
1335}
1336
Jesse Halle1b12782015-11-30 11:27:32 -08001337VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001338void DestroySwapchainKHR(VkDevice device,
1339 VkSwapchainKHR swapchain_handle,
1340 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001341 ATRACE_CALL();
1342
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001343 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001344 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001345 if (!swapchain)
1346 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001347 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1348 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001349
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001350 if (swapchain->frame_timestamps_enabled) {
1351 native_window_enable_frame_timestamps(window, false);
1352 }
Jesse Halldc225072016-05-30 22:40:14 -07001353 for (uint32_t i = 0; i < swapchain->num_images; i++)
1354 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001355 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001356 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001357 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001358 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001359 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001360 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001361}
1362
Jesse Halle1b12782015-11-30 11:27:32 -08001363VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001364VkResult GetSwapchainImagesKHR(VkDevice,
1365 VkSwapchainKHR swapchain_handle,
1366 uint32_t* count,
1367 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001368 ATRACE_CALL();
1369
Jesse Halld7b994a2015-09-07 14:17:37 -07001370 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001371 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1372 "getting images for non-active swapchain 0x%" PRIx64
1373 "; only dequeued image handles are valid",
1374 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001375 VkResult result = VK_SUCCESS;
1376 if (images) {
1377 uint32_t n = swapchain.num_images;
1378 if (*count < swapchain.num_images) {
1379 n = *count;
1380 result = VK_INCOMPLETE;
1381 }
1382 for (uint32_t i = 0; i < n; i++)
1383 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001384 *count = n;
1385 } else {
1386 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001387 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001388 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001389}
1390
Jesse Halle1b12782015-11-30 11:27:32 -08001391VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001392VkResult AcquireNextImageKHR(VkDevice device,
1393 VkSwapchainKHR swapchain_handle,
1394 uint64_t timeout,
1395 VkSemaphore semaphore,
1396 VkFence vk_fence,
1397 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001398 ATRACE_CALL();
1399
Jesse Halld7b994a2015-09-07 14:17:37 -07001400 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001401 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001402 VkResult result;
1403 int err;
1404
Jesse Halldc225072016-05-30 22:40:14 -07001405 if (swapchain.surface.swapchain_handle != swapchain_handle)
1406 return VK_ERROR_OUT_OF_DATE_KHR;
1407
Jesse Halld7b994a2015-09-07 14:17:37 -07001408 ALOGW_IF(
1409 timeout != UINT64_MAX,
1410 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1411
Chris Forbesc88409c2017-03-30 19:47:37 +13001412 if (swapchain.shared) {
1413 // In shared mode, we keep the buffer dequeued all the time, so we don't
1414 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1415 // the semaphore and fence passed to us will be signalled.
1416 *image_index = 0;
1417 result = GetData(device).driver.AcquireImageANDROID(
1418 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1419 return result;
1420 }
1421
Jesse Halld7b994a2015-09-07 14:17:37 -07001422 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001423 int fence_fd;
1424 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001425 if (err != 0) {
1426 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1427 // errors and translate them to valid Vulkan result codes?
1428 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001429 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001430 }
1431
1432 uint32_t idx;
1433 for (idx = 0; idx < swapchain.num_images; idx++) {
1434 if (swapchain.images[idx].buffer.get() == buffer) {
1435 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001436 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001437 break;
1438 }
1439 }
1440 if (idx == swapchain.num_images) {
1441 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001442 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001443 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001444 }
1445
1446 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001447 if (fence_fd != -1) {
1448 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001449 if (fence_clone == -1) {
1450 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1451 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001452 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001453 }
1454 }
1455
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001456 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001457 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001458 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001459 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1460 // even if the call fails. We could close it ourselves on failure, but
1461 // that would create a race condition if the driver closes it on a
1462 // failure path: some other thread might create an fd with the same
1463 // number between the time the driver closes it and the time we close
1464 // it. We must assume one of: the driver *always* closes it even on
1465 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001466 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001467 swapchain.images[idx].dequeued = false;
1468 swapchain.images[idx].dequeue_fence = -1;
1469 return result;
1470 }
1471
1472 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001473 return VK_SUCCESS;
1474}
1475
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001476VKAPI_ATTR
1477VkResult AcquireNextImage2KHR(VkDevice device,
1478 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1479 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001480 ATRACE_CALL();
1481
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001482 // TODO: this should actually be the other way around and this function
1483 // should handle any additional structures that get passed in
1484 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1485 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1486 pAcquireInfo->fence, pImageIndex);
1487}
1488
Jesse Halldc225072016-05-30 22:40:14 -07001489static VkResult WorstPresentResult(VkResult a, VkResult b) {
1490 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1491 // (in spec version 1.0.14).
1492 static const VkResult kWorstToBest[] = {
1493 VK_ERROR_DEVICE_LOST,
1494 VK_ERROR_SURFACE_LOST_KHR,
1495 VK_ERROR_OUT_OF_DATE_KHR,
1496 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1497 VK_ERROR_OUT_OF_HOST_MEMORY,
1498 VK_SUBOPTIMAL_KHR,
1499 };
1500 for (auto result : kWorstToBest) {
1501 if (a == result || b == result)
1502 return result;
1503 }
1504 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1505 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1506 return a != VK_SUCCESS ? a : b;
1507}
1508
Jesse Halle1b12782015-11-30 11:27:32 -08001509VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001510VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001511 ATRACE_CALL();
1512
Jesse Halld7b994a2015-09-07 14:17:37 -07001513 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1514 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1515 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001516
Jesse Halldc225072016-05-30 22:40:14 -07001517 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001518 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001519 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001520
Ian Elliottcb351132016-12-13 10:30:40 -07001521 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001522 const VkPresentRegionsKHR* present_regions = nullptr;
1523 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001524 const VkPresentRegionsKHR* next =
1525 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1526 while (next) {
1527 switch (next->sType) {
1528 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1529 present_regions = next;
1530 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001531 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001532 present_times =
1533 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1534 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001535 default:
1536 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1537 next->sType);
1538 break;
1539 }
1540 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1541 }
1542 ALOGV_IF(
1543 present_regions &&
1544 present_regions->swapchainCount != present_info->swapchainCount,
1545 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001546 ALOGV_IF(present_times &&
1547 present_times->swapchainCount != present_info->swapchainCount,
1548 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1549 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001550 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001551 (present_regions) ? present_regions->pRegions : nullptr;
1552 const VkPresentTimeGOOGLE* times =
1553 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001554 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001555 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001556 uint32_t nrects = 0;
1557
Jesse Halld7b994a2015-09-07 14:17:37 -07001558 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1559 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001560 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001561 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001562 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001563 const VkPresentRegionKHR* region =
1564 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001565 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001566 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001567 VkResult result;
1568 int err;
1569
Jesse Halld7b994a2015-09-07 14:17:37 -07001570 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001571 result = dispatch.QueueSignalReleaseImageANDROID(
1572 queue, present_info->waitSemaphoreCount,
1573 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001574 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001575 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001576 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001577 }
1578
Jesse Halldc225072016-05-30 22:40:14 -07001579 if (swapchain.surface.swapchain_handle ==
1580 present_info->pSwapchains[sc]) {
1581 ANativeWindow* window = swapchain.surface.window.get();
1582 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001583 if (region) {
1584 // Process the incremental-present hint for this swapchain:
1585 uint32_t rcount = region->rectangleCount;
1586 if (rcount > nrects) {
1587 android_native_rect_t* new_rects =
1588 static_cast<android_native_rect_t*>(
1589 allocator->pfnReallocation(
1590 allocator->pUserData, rects,
1591 sizeof(android_native_rect_t) * rcount,
1592 alignof(android_native_rect_t),
1593 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1594 if (new_rects) {
1595 rects = new_rects;
1596 nrects = rcount;
1597 } else {
1598 rcount = 0; // Ignore the hint for this swapchain
1599 }
1600 }
1601 for (uint32_t r = 0; r < rcount; ++r) {
1602 if (region->pRectangles[r].layer > 0) {
1603 ALOGV(
1604 "vkQueuePresentKHR ignoring invalid layer "
1605 "(%u); using layer 0 instead",
1606 region->pRectangles[r].layer);
1607 }
1608 int x = region->pRectangles[r].offset.x;
1609 int y = region->pRectangles[r].offset.y;
1610 int width = static_cast<int>(
1611 region->pRectangles[r].extent.width);
1612 int height = static_cast<int>(
1613 region->pRectangles[r].extent.height);
1614 android_native_rect_t* cur_rect = &rects[r];
1615 cur_rect->left = x;
1616 cur_rect->top = y + height;
1617 cur_rect->right = x + width;
1618 cur_rect->bottom = y;
1619 }
1620 native_window_set_surface_damage(window, rects, rcount);
1621 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001622 if (time) {
1623 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001624 ALOGV(
1625 "Calling "
1626 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001627 native_window_enable_frame_timestamps(window, true);
1628 swapchain.frame_timestamps_enabled = true;
1629 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001630
1631 // Record the nativeFrameId so it can be later correlated to
1632 // this present.
1633 uint64_t nativeFrameId = 0;
1634 err = native_window_get_next_frame_id(
1635 window, &nativeFrameId);
1636 if (err != android::NO_ERROR) {
1637 ALOGE("Failed to get next native frame ID.");
1638 }
1639
1640 // Add a new timing record with the user's presentID and
1641 // the nativeFrameId.
1642 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1643 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001644 swapchain.timing.removeAt(0);
1645 }
1646 if (time->desiredPresentTime) {
1647 // Set the desiredPresentTime:
1648 ALOGV(
1649 "Calling "
1650 "native_window_set_buffers_timestamp(%" PRId64 ")",
1651 time->desiredPresentTime);
1652 native_window_set_buffers_timestamp(
1653 window,
1654 static_cast<int64_t>(time->desiredPresentTime));
1655 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001656 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001657
Jesse Halldc225072016-05-30 22:40:14 -07001658 err = window->queueBuffer(window, img.buffer.get(), fence);
1659 // queueBuffer always closes fence, even on error
1660 if (err != 0) {
1661 // TODO(jessehall): What now? We should probably cancel the
1662 // buffer, I guess?
1663 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1664 swapchain_result = WorstPresentResult(
1665 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1666 }
1667 if (img.dequeue_fence >= 0) {
1668 close(img.dequeue_fence);
1669 img.dequeue_fence = -1;
1670 }
1671 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001672
1673 // If the swapchain is in shared mode, immediately dequeue the
1674 // buffer so it can be presented again without an intervening
1675 // call to AcquireNextImageKHR. We expect to get the same buffer
1676 // back from every call to dequeueBuffer in this mode.
1677 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1678 ANativeWindowBuffer* buffer;
1679 int fence_fd;
1680 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1681 if (err != 0) {
1682 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1683 swapchain_result = WorstPresentResult(swapchain_result,
1684 VK_ERROR_SURFACE_LOST_KHR);
1685 }
1686 else if (img.buffer != buffer) {
1687 ALOGE("got wrong image back for shared swapchain");
1688 swapchain_result = WorstPresentResult(swapchain_result,
1689 VK_ERROR_SURFACE_LOST_KHR);
1690 }
1691 else {
1692 img.dequeue_fence = fence_fd;
1693 img.dequeued = true;
1694 }
1695 }
Jesse Halldc225072016-05-30 22:40:14 -07001696 }
1697 if (swapchain_result != VK_SUCCESS) {
1698 ReleaseSwapchainImage(device, window, fence, img);
1699 OrphanSwapchain(device, &swapchain);
1700 }
1701 } else {
1702 ReleaseSwapchainImage(device, nullptr, fence, img);
1703 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001704 }
1705
Jesse Halla9e57032015-11-30 01:03:10 -08001706 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001707 present_info->pResults[sc] = swapchain_result;
1708
1709 if (swapchain_result != final_result)
1710 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001711 }
Ian Elliottcb351132016-12-13 10:30:40 -07001712 if (rects) {
1713 allocator->pfnFree(allocator->pUserData, rects);
1714 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001715
1716 return final_result;
1717}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001718
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001719VKAPI_ATTR
1720VkResult GetRefreshCycleDurationGOOGLE(
1721 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001722 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001723 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001724 ATRACE_CALL();
1725
Ian Elliott62c48c92017-01-20 13:13:20 -07001726 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001727 VkResult result = VK_SUCCESS;
1728
Brian Andersondc96fdf2017-03-20 16:54:25 -07001729 pDisplayTimingProperties->refreshDuration =
1730 static_cast<uint64_t>(swapchain.refresh_duration);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001731
1732 return result;
1733}
1734
1735VKAPI_ATTR
1736VkResult GetPastPresentationTimingGOOGLE(
1737 VkDevice,
1738 VkSwapchainKHR swapchain_handle,
1739 uint32_t* count,
1740 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001741 ATRACE_CALL();
1742
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001743 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1744 ANativeWindow* window = swapchain.surface.window.get();
1745 VkResult result = VK_SUCCESS;
1746
1747 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001748 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001749 native_window_enable_frame_timestamps(window, true);
1750 swapchain.frame_timestamps_enabled = true;
1751 }
1752
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001753 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001754 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1755 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001756 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001757 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001758 }
1759
1760 return result;
1761}
1762
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001763VKAPI_ATTR
1764VkResult GetSwapchainStatusKHR(
1765 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001766 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001767 ATRACE_CALL();
1768
Chris Forbes4e18ba82017-01-20 12:50:17 +13001769 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001770 VkResult result = VK_SUCCESS;
1771
Chris Forbes4e18ba82017-01-20 12:50:17 +13001772 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1773 return VK_ERROR_OUT_OF_DATE_KHR;
1774 }
1775
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001776 // TODO(chrisforbes): Implement this function properly
1777
1778 return result;
1779}
1780
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001781VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001782 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001783 uint32_t swapchainCount,
1784 const VkSwapchainKHR* pSwapchains,
1785 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001786 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001787
1788 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1789 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1790 if (!swapchain)
1791 continue;
1792
1793 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1794
1795 ANativeWindow* window = swapchain->surface.window.get();
1796
1797 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1798 const android_smpte2086_metadata smpteMetdata = {
1799 {vulkanMetadata.displayPrimaryRed.x,
1800 vulkanMetadata.displayPrimaryRed.y},
1801 {vulkanMetadata.displayPrimaryGreen.x,
1802 vulkanMetadata.displayPrimaryGreen.y},
1803 {vulkanMetadata.displayPrimaryBlue.x,
1804 vulkanMetadata.displayPrimaryBlue.y},
1805 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1806 vulkanMetadata.maxLuminance,
1807 vulkanMetadata.minLuminance};
1808 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1809
1810 const android_cta861_3_metadata cta8613Metadata = {
1811 vulkanMetadata.maxContentLightLevel,
1812 vulkanMetadata.maxFrameAverageLightLevel};
1813 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1814 }
1815
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001816 return;
1817}
1818
Chia-I Wu62262232016-03-26 07:06:44 +08001819} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001820} // namespace vulkan