blob: ea1119ac3c6f8014eec2f442716dc680b9292259 [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;
422 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
423 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 Zhang6435b322018-05-08 11:12:17 -0700582 format_supported = true;
583 break;
584 default:
585 break;
586 }
587
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700588 *supported = static_cast<VkBool32>(
589 format_supported || (surface->consumer_usage &
590 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
591 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700592
Jesse Halla6429252015-11-29 18:59:42 -0800593 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800594}
595
Jesse Halle1b12782015-11-30 11:27:32 -0800596VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800597VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800598 VkPhysicalDevice /*pdev*/,
599 VkSurfaceKHR surface,
600 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800601 ATRACE_CALL();
602
Jesse Halld7b994a2015-09-07 14:17:37 -0700603 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800604 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700605
606 int width, height;
607 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
608 if (err != 0) {
609 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
610 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700611 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700612 }
613 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
614 if (err != 0) {
615 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
616 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700617 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700618 }
619
Jesse Hall55bc0972016-02-23 16:43:29 -0800620 int transform_hint;
621 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
622 if (err != 0) {
623 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
624 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700625 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800626 }
627
Jesse Halld7b994a2015-09-07 14:17:37 -0700628 // TODO(jessehall): Figure out what the min/max values should be.
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800629 int max_buffer_count;
630 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
631 if (err != 0) {
632 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
633 strerror(-err), err);
634 return VK_ERROR_SURFACE_LOST_KHR;
635 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700636 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800637 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700638
Jesse Hallfe2662d2016-02-09 13:26:59 -0800639 capabilities->currentExtent =
640 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
641
Jesse Halld7b994a2015-09-07 14:17:37 -0700642 // TODO(jessehall): Figure out what the max extent should be. Maximum
643 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800644 capabilities->minImageExtent = VkExtent2D{1, 1};
645 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700646
Jesse Hallfe2662d2016-02-09 13:26:59 -0800647 capabilities->maxImageArrayLayers = 1;
648
Jesse Hall55bc0972016-02-23 16:43:29 -0800649 capabilities->supportedTransforms = kSupportedTransforms;
650 capabilities->currentTransform =
651 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700652
Jesse Hallfe2662d2016-02-09 13:26:59 -0800653 // On Android, window composition is a WindowManager property, not something
654 // associated with the bufferqueue. It can't be changed from here.
655 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700656
657 // TODO(jessehall): I think these are right, but haven't thought hard about
658 // it. Do we need to query the driver for support of any of these?
659 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700660 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
661 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800662 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800663 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
664 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
665 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700666 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
667
Jesse Hallb1352bc2015-09-04 16:12:33 -0700668 return VK_SUCCESS;
669}
670
Jesse Halle1b12782015-11-30 11:27:32 -0800671VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700672VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
673 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800674 uint32_t* count,
675 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800676 ATRACE_CALL();
677
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700678 const InstanceData& instance_data = GetData(pdev);
679
Jesse Hall1356b0d2015-11-23 17:24:58 -0800680 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
681 // a new gralloc method to query whether a (format, usage) pair is
682 // supported, and check that for each gralloc format that corresponds to a
683 // Vulkan format. Shorter term, just add a few more formats to the ones
684 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700685
686 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700687 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
688 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
689 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700690 };
691 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700692 uint32_t total_num_formats = kNumFormats;
693
694 bool wide_color_support = false;
695 Surface& surface = *SurfaceFromHandle(surface_handle);
696 int err = native_window_get_wide_color_support(surface.window.get(),
697 &wide_color_support);
698 if (err) {
699 // Not allowed to return a more sensible error code, so do this
700 return VK_ERROR_OUT_OF_HOST_MEMORY;
701 }
702 ALOGV("wide_color_support is: %d", wide_color_support);
703 wide_color_support =
704 wide_color_support &&
705 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
706
707 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600708 {VK_FORMAT_R8G8B8A8_UNORM,
709 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
710 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700711 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700712 {VK_FORMAT_R16G16B16A16_SFLOAT,
713 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
714 {VK_FORMAT_R16G16B16A16_SFLOAT,
715 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700716 };
717 const uint32_t kNumWideColorFormats =
718 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
719 if (wide_color_support) {
720 total_num_formats += kNumWideColorFormats;
721 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700722
723 VkResult result = VK_SUCCESS;
724 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700725 uint32_t out_count = 0;
726 uint32_t transfer_count = 0;
727 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700728 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700729 transfer_count = std::min(*count, kNumFormats);
730 std::copy(kFormats, kFormats + transfer_count, formats);
731 out_count += transfer_count;
732 if (wide_color_support) {
733 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
734 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
735 formats + out_count);
736 out_count += transfer_count;
737 }
738 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700739 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700740 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700741 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700742 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700743}
744
Jesse Halle1b12782015-11-30 11:27:32 -0800745VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300746VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
747 VkPhysicalDevice physicalDevice,
748 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
749 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800750 ATRACE_CALL();
751
Chris Forbes2452cf72017-03-16 16:30:17 +1300752 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
753 physicalDevice, pSurfaceInfo->surface,
754 &pSurfaceCapabilities->surfaceCapabilities);
755
Chris Forbes06bc0092017-03-16 16:46:05 +1300756 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
757 while (caps->pNext) {
758 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
759
760 switch (caps->sType) {
761 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
762 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
763 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
764 caps);
765 // Claim same set of usage flags are supported for
766 // shared present modes as for other modes.
767 shared_caps->sharedPresentSupportedUsageFlags =
768 pSurfaceCapabilities->surfaceCapabilities
769 .supportedUsageFlags;
770 } break;
771
772 default:
773 // Ignore all other extension structs
774 break;
775 }
776 }
777
Chris Forbes2452cf72017-03-16 16:30:17 +1300778 return result;
779}
780
781VKAPI_ATTR
782VkResult GetPhysicalDeviceSurfaceFormats2KHR(
783 VkPhysicalDevice physicalDevice,
784 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
785 uint32_t* pSurfaceFormatCount,
786 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800787 ATRACE_CALL();
788
Chris Forbes2452cf72017-03-16 16:30:17 +1300789 if (!pSurfaceFormats) {
790 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
791 pSurfaceInfo->surface,
792 pSurfaceFormatCount, nullptr);
793 } else {
794 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
795 // after the call.
796 android::Vector<VkSurfaceFormatKHR> surface_formats;
797 surface_formats.resize(*pSurfaceFormatCount);
798 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
799 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
800 &surface_formats.editItemAt(0));
801
802 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
803 // marshal results individually due to stride difference.
804 // completely ignore any chained extension structs.
805 uint32_t formats_to_marshal = *pSurfaceFormatCount;
806 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
807 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
808 }
809 }
810
811 return result;
812 }
813}
814
815VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300816VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800817 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800818 uint32_t* count,
819 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800820 ATRACE_CALL();
821
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800822 int err;
823 int query_value;
824 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
825
826 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
827 if (err != 0 || query_value < 0) {
828 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
829 strerror(-err), err, query_value);
830 return VK_ERROR_SURFACE_LOST_KHR;
831 }
832 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
833
834 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
835 if (err != 0 || query_value < 0) {
836 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
837 strerror(-err), err, query_value);
838 return VK_ERROR_SURFACE_LOST_KHR;
839 }
840 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
841
Chris Forbese8d79a62017-02-22 12:49:18 +1300842 android::Vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800843 if (min_undequeued_buffers + 1 < max_buffer_count)
844 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300845 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
846
847 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
848 if (QueryPresentationProperties(pdev, &present_properties)) {
849 if (present_properties.sharedImage) {
850 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
851 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
852 }
853 }
854
855 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700856
857 VkResult result = VK_SUCCESS;
858 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300859 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700860 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300861 *count = std::min(*count, num_modes);
862 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700863 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300864 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700865 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700866 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700867}
868
Jesse Halle1b12782015-11-30 11:27:32 -0800869VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400870VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600871 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400872 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800873 ATRACE_CALL();
874
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400875 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
876 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
877 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
878 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
879 pDeviceGroupPresentCapabilities->sType);
880
881 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
882 sizeof(pDeviceGroupPresentCapabilities->presentMask));
883
884 // assume device group of size 1
885 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
886 pDeviceGroupPresentCapabilities->modes =
887 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
888
889 return VK_SUCCESS;
890}
891
892VKAPI_ATTR
893VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600894 VkDevice,
895 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400896 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800897 ATRACE_CALL();
898
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400899 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
900 return VK_SUCCESS;
901}
902
903VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600904VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400905 VkSurfaceKHR surface,
906 uint32_t* pRectCount,
907 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800908 ATRACE_CALL();
909
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400910 if (!pRects) {
911 *pRectCount = 1;
912 } else {
913 uint32_t count = std::min(*pRectCount, 1u);
914 bool incomplete = *pRectCount < 1;
915
916 *pRectCount = count;
917
918 if (incomplete) {
919 return VK_INCOMPLETE;
920 }
921
922 int err;
923 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
924
925 int width = 0, height = 0;
926 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
927 if (err != 0) {
928 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
929 strerror(-err), err);
930 }
931 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
932 if (err != 0) {
933 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
934 strerror(-err), err);
935 }
936
937 // TODO: Return something better than "whole window"
938 pRects[0].offset.x = 0;
939 pRects[0].offset.y = 0;
940 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
941 static_cast<uint32_t>(height)};
942 }
943 return VK_SUCCESS;
944}
945
946VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800947VkResult CreateSwapchainKHR(VkDevice device,
948 const VkSwapchainCreateInfoKHR* create_info,
949 const VkAllocationCallbacks* allocator,
950 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800951 ATRACE_CALL();
952
Jesse Halld7b994a2015-09-07 14:17:37 -0700953 int err;
954 VkResult result = VK_SUCCESS;
955
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700956 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
957 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
958 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
959 " oldSwapchain=0x%" PRIx64,
960 reinterpret_cast<uint64_t>(create_info->surface),
961 create_info->minImageCount, create_info->imageFormat,
962 create_info->imageColorSpace, create_info->imageExtent.width,
963 create_info->imageExtent.height, create_info->imageUsage,
964 create_info->preTransform, create_info->presentMode,
965 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
966
Jesse Hall1f91d392015-12-11 16:28:44 -0800967 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800968 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800969
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700970 android_pixel_format native_pixel_format =
971 GetNativePixelFormat(create_info->imageFormat);
972 android_dataspace native_dataspace =
973 GetNativeDataspace(create_info->imageColorSpace);
974 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
975 ALOGE(
976 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
977 "failed: Unsupported color space",
978 create_info->imageColorSpace);
979 return VK_ERROR_INITIALIZATION_FAILED;
980 }
981
Jesse Hall42a9eec2016-06-03 12:39:49 -0700982 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700983 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800984 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700985 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700986 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800987 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700988 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +1300989 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300990 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
991 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700992 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800993 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700994
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700995 Surface& surface = *SurfaceFromHandle(create_info->surface);
996
Jesse Halldc225072016-05-30 22:40:14 -0700997 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -0700998 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -0700999 " because it already has active swapchain 0x%" PRIx64
1000 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1001 reinterpret_cast<uint64_t>(create_info->surface),
1002 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1003 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1004 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1005 }
1006 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1007 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1008
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001009 // -- Reset the native window --
1010 // The native window might have been used previously, and had its properties
1011 // changed from defaults. That will affect the answer we get for queries
1012 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1013 // attempt such queries.
1014
Jesse Halldc225072016-05-30 22:40:14 -07001015 // The native window only allows dequeueing all buffers before any have
1016 // been queued, since after that point at least one is assumed to be in
1017 // non-FREE state at any given time. Disconnecting and re-connecting
1018 // orphans the previous buffers, getting us back to the state where we can
1019 // dequeue all buffers.
1020 err = native_window_api_disconnect(surface.window.get(),
1021 NATIVE_WINDOW_API_EGL);
1022 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1023 strerror(-err), err);
1024 err =
1025 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1026 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1027 strerror(-err), err);
1028
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001029 err = native_window_set_buffer_count(surface.window.get(), 0);
1030 if (err != 0) {
1031 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1032 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001033 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001034 }
1035
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301036 int swap_interval =
1037 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1038 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001039 if (err != 0) {
1040 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1041 // errors and translate them to valid Vulkan result codes?
1042 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1043 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001044 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001045 }
1046
Chris Forbesb8042d22017-01-18 18:07:05 +13001047 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1048 if (err != 0) {
1049 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1050 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001051 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001052 }
1053
1054 err = native_window_set_auto_refresh(surface.window.get(), false);
1055 if (err != 0) {
1056 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1057 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001058 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001059 }
1060
Jesse Halld7b994a2015-09-07 14:17:37 -07001061 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001062
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001063 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001064
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001065 err = native_window_set_buffers_format(surface.window.get(),
1066 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001067 if (err != 0) {
1068 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1069 // errors and translate them to valid Vulkan result codes?
1070 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001071 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001072 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001073 }
1074 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001075 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001076 if (err != 0) {
1077 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1078 // errors and translate them to valid Vulkan result codes?
1079 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001080 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001081 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001082 }
1083
Jesse Hall3dd678a2016-01-08 21:52:01 -08001084 err = native_window_set_buffers_dimensions(
1085 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1086 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001087 if (err != 0) {
1088 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1089 // errors and translate them to valid Vulkan result codes?
1090 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1091 create_info->imageExtent.width, create_info->imageExtent.height,
1092 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001093 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001094 }
1095
Jesse Hall178b6962016-02-24 15:39:50 -08001096 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1097 // applied during rendering. native_window_set_transform() expects the
1098 // inverse: the transform the app is requesting that the compositor perform
1099 // during composition. With native windows, pre-transform works by rendering
1100 // with the same transform the compositor is applying (as in Vulkan), but
1101 // then requesting the inverse transform, so that when the compositor does
1102 // it's job the two transforms cancel each other out and the compositor ends
1103 // up applying an identity transform to the app's buffer.
1104 err = native_window_set_buffers_transform(
1105 surface.window.get(),
1106 InvertTransformToNative(create_info->preTransform));
1107 if (err != 0) {
1108 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1109 // errors and translate them to valid Vulkan result codes?
1110 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1111 InvertTransformToNative(create_info->preTransform),
1112 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001113 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001114 }
1115
Jesse Hallf64ca122015-11-03 16:11:10 -08001116 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001117 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001118 if (err != 0) {
1119 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1120 // errors and translate them to valid Vulkan result codes?
1121 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1122 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001123 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001124 }
1125
Chris Forbes97ef4612017-03-30 19:37:50 +13001126 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1127 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1128 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1129 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1130 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1131 if (err != 0) {
1132 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1133 return VK_ERROR_SURFACE_LOST_KHR;
1134 }
1135 }
1136
1137 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1138 err = native_window_set_auto_refresh(surface.window.get(), true);
1139 if (err != 0) {
1140 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1141 return VK_ERROR_SURFACE_LOST_KHR;
1142 }
1143 }
1144
Jesse Halle6080bf2016-02-28 20:58:50 -08001145 int query_value;
1146 err = surface.window->query(surface.window.get(),
1147 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1148 &query_value);
1149 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001150 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1151 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -08001152 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1153 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001154 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001155 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001156 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001157 uint32_t num_images =
1158 (create_info->minImageCount - 1) + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001159
1160 // Lower layer insists that we have at least two buffers. This is wasteful
1161 // and we'd like to relax it in the shared case, but not all the pieces are
1162 // in place for that to work yet. Note we only lie to the lower layer-- we
1163 // don't want to give the app back a swapchain with extra images (which they
1164 // can't actually use!).
1165 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001166 if (err != 0) {
1167 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1168 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001169 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1170 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001171 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001172 }
1173
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001174 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001175 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001176 uint64_t consumer_usage, producer_usage;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001177 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001178 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1179 device, create_info->imageFormat, create_info->imageUsage,
1180 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001181 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001182 if (result != VK_SUCCESS) {
1183 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001184 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001185 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001186 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001187 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001188 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001189 ATRACE_BEGIN("dispatch.GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001190 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001191 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001192 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001193 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001194 if (result != VK_SUCCESS) {
1195 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001196 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001197 }
Jesse Hall70f93352015-11-04 09:41:31 -08001198 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001199 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1200
1201 bool createProtectedSwapchain = false;
1202 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1203 createProtectedSwapchain = true;
1204 native_usage |= BufferUsage::PROTECTED;
1205 }
1206 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001207 if (err != 0) {
1208 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1209 // errors and translate them to valid Vulkan result codes?
1210 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001211 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001212 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001213
1214 // -- Allocate our Swapchain object --
1215 // After this point, we must deallocate the swapchain on error.
1216
Jesse Hall1f91d392015-12-11 16:28:44 -08001217 void* mem = allocator->pfnAllocation(allocator->pUserData,
1218 sizeof(Swapchain), alignof(Swapchain),
1219 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001220 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001221 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001222 Swapchain* swapchain =
1223 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001224
1225 // -- Dequeue all buffers and create a VkImage for each --
1226 // Any failures during or after this must cancel the dequeued buffers.
1227
Chris Forbesb56287a2017-01-12 14:28:58 +13001228 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1229#pragma clang diagnostic push
1230#pragma clang diagnostic ignored "-Wold-style-cast"
1231 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1232#pragma clang diagnostic pop
1233 .pNext = nullptr,
1234 .usage = swapchain_image_usage,
1235 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001236 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001237#pragma clang diagnostic push
1238#pragma clang diagnostic ignored "-Wold-style-cast"
1239 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1240#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001241 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001242 };
1243 VkImageCreateInfo image_create = {
1244 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1245 .pNext = &image_native_buffer,
1246 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001247 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001248 .extent = {0, 0, 1},
1249 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001250 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001251 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001252 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001253 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001254 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001255 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001256 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001257 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1258 };
1259
Jesse Halld7b994a2015-09-07 14:17:37 -07001260 for (uint32_t i = 0; i < num_images; i++) {
1261 Swapchain::Image& img = swapchain->images[i];
1262
1263 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001264 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1265 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001266 if (err != 0) {
1267 // TODO(jessehall): Improve error reporting. Can we enumerate
1268 // possible errors and translate them to valid Vulkan result codes?
1269 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001270 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001271 break;
1272 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001273 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001274 img.dequeued = true;
1275
1276 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001277 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1278 static_cast<uint32_t>(img.buffer->height),
1279 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001280 image_native_buffer.handle = img.buffer->handle;
1281 image_native_buffer.stride = img.buffer->stride;
1282 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001283 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001284 android_convertGralloc0To1Usage(int(img.buffer->usage),
1285 &image_native_buffer.usage2.producer,
1286 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001287
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001288 ATRACE_BEGIN("dispatch.CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001289 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001290 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001291 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001292 if (result != VK_SUCCESS) {
1293 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1294 break;
1295 }
1296 }
1297
1298 // -- Cancel all buffers, returning them to the queue --
1299 // If an error occurred before, also destroy the VkImage and release the
1300 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1301 //
1302 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1303 // but not the non-error path. Should refactor/unify.
Chris Forbes31b85c22018-05-29 15:03:28 -07001304 for (uint32_t i = 0; i < num_images; i++) {
1305 Swapchain::Image& img = swapchain->images[i];
1306 if (img.dequeued) {
1307 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001308 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1309 img.dequeue_fence);
1310 img.dequeue_fence = -1;
1311 img.dequeued = false;
1312 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001313 }
1314 if (result != VK_SUCCESS) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001315 if (img.image) {
1316 ATRACE_BEGIN("dispatch.DestroyImage");
Chris Forbes31b85c22018-05-29 15:03:28 -07001317 dispatch.DestroyImage(device, img.image, nullptr);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001318 ATRACE_END();
1319 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001320 }
1321 }
1322
1323 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001324 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001325 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001326 return result;
1327 }
1328
Jesse Halldc225072016-05-30 22:40:14 -07001329 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1330 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001331 return VK_SUCCESS;
1332}
1333
Jesse Halle1b12782015-11-30 11:27:32 -08001334VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001335void DestroySwapchainKHR(VkDevice device,
1336 VkSwapchainKHR swapchain_handle,
1337 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001338 ATRACE_CALL();
1339
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001340 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001341 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001342 if (!swapchain)
1343 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001344 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1345 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001346
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001347 if (swapchain->frame_timestamps_enabled) {
1348 native_window_enable_frame_timestamps(window, false);
1349 }
Jesse Halldc225072016-05-30 22:40:14 -07001350 for (uint32_t i = 0; i < swapchain->num_images; i++)
1351 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001352 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001353 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001354 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001355 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001356 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001357 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001358}
1359
Jesse Halle1b12782015-11-30 11:27:32 -08001360VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001361VkResult GetSwapchainImagesKHR(VkDevice,
1362 VkSwapchainKHR swapchain_handle,
1363 uint32_t* count,
1364 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001365 ATRACE_CALL();
1366
Jesse Halld7b994a2015-09-07 14:17:37 -07001367 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001368 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1369 "getting images for non-active swapchain 0x%" PRIx64
1370 "; only dequeued image handles are valid",
1371 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001372 VkResult result = VK_SUCCESS;
1373 if (images) {
1374 uint32_t n = swapchain.num_images;
1375 if (*count < swapchain.num_images) {
1376 n = *count;
1377 result = VK_INCOMPLETE;
1378 }
1379 for (uint32_t i = 0; i < n; i++)
1380 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001381 *count = n;
1382 } else {
1383 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001384 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001385 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001386}
1387
Jesse Halle1b12782015-11-30 11:27:32 -08001388VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001389VkResult AcquireNextImageKHR(VkDevice device,
1390 VkSwapchainKHR swapchain_handle,
1391 uint64_t timeout,
1392 VkSemaphore semaphore,
1393 VkFence vk_fence,
1394 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001395 ATRACE_CALL();
1396
Jesse Halld7b994a2015-09-07 14:17:37 -07001397 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001398 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001399 VkResult result;
1400 int err;
1401
Jesse Halldc225072016-05-30 22:40:14 -07001402 if (swapchain.surface.swapchain_handle != swapchain_handle)
1403 return VK_ERROR_OUT_OF_DATE_KHR;
1404
Jesse Halld7b994a2015-09-07 14:17:37 -07001405 ALOGW_IF(
1406 timeout != UINT64_MAX,
1407 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1408
Chris Forbesc88409c2017-03-30 19:47:37 +13001409 if (swapchain.shared) {
1410 // In shared mode, we keep the buffer dequeued all the time, so we don't
1411 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1412 // the semaphore and fence passed to us will be signalled.
1413 *image_index = 0;
1414 result = GetData(device).driver.AcquireImageANDROID(
1415 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1416 return result;
1417 }
1418
Jesse Halld7b994a2015-09-07 14:17:37 -07001419 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001420 int fence_fd;
1421 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001422 if (err != 0) {
1423 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1424 // errors and translate them to valid Vulkan result codes?
1425 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001426 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001427 }
1428
1429 uint32_t idx;
1430 for (idx = 0; idx < swapchain.num_images; idx++) {
1431 if (swapchain.images[idx].buffer.get() == buffer) {
1432 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001433 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001434 break;
1435 }
1436 }
1437 if (idx == swapchain.num_images) {
1438 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001439 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001440 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001441 }
1442
1443 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001444 if (fence_fd != -1) {
1445 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001446 if (fence_clone == -1) {
1447 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1448 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001449 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001450 }
1451 }
1452
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001453 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001454 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001455 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001456 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1457 // even if the call fails. We could close it ourselves on failure, but
1458 // that would create a race condition if the driver closes it on a
1459 // failure path: some other thread might create an fd with the same
1460 // number between the time the driver closes it and the time we close
1461 // it. We must assume one of: the driver *always* closes it even on
1462 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001463 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001464 swapchain.images[idx].dequeued = false;
1465 swapchain.images[idx].dequeue_fence = -1;
1466 return result;
1467 }
1468
1469 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001470 return VK_SUCCESS;
1471}
1472
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001473VKAPI_ATTR
1474VkResult AcquireNextImage2KHR(VkDevice device,
1475 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1476 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001477 ATRACE_CALL();
1478
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001479 // TODO: this should actually be the other way around and this function
1480 // should handle any additional structures that get passed in
1481 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1482 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1483 pAcquireInfo->fence, pImageIndex);
1484}
1485
Jesse Halldc225072016-05-30 22:40:14 -07001486static VkResult WorstPresentResult(VkResult a, VkResult b) {
1487 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1488 // (in spec version 1.0.14).
1489 static const VkResult kWorstToBest[] = {
1490 VK_ERROR_DEVICE_LOST,
1491 VK_ERROR_SURFACE_LOST_KHR,
1492 VK_ERROR_OUT_OF_DATE_KHR,
1493 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1494 VK_ERROR_OUT_OF_HOST_MEMORY,
1495 VK_SUBOPTIMAL_KHR,
1496 };
1497 for (auto result : kWorstToBest) {
1498 if (a == result || b == result)
1499 return result;
1500 }
1501 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1502 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1503 return a != VK_SUCCESS ? a : b;
1504}
1505
Jesse Halle1b12782015-11-30 11:27:32 -08001506VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001507VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001508 ATRACE_CALL();
1509
Jesse Halld7b994a2015-09-07 14:17:37 -07001510 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1511 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1512 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001513
Jesse Halldc225072016-05-30 22:40:14 -07001514 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001515 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001516 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001517
Ian Elliottcb351132016-12-13 10:30:40 -07001518 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001519 const VkPresentRegionsKHR* present_regions = nullptr;
1520 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001521 const VkPresentRegionsKHR* next =
1522 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1523 while (next) {
1524 switch (next->sType) {
1525 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1526 present_regions = next;
1527 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001528 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001529 present_times =
1530 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1531 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001532 default:
1533 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1534 next->sType);
1535 break;
1536 }
1537 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1538 }
1539 ALOGV_IF(
1540 present_regions &&
1541 present_regions->swapchainCount != present_info->swapchainCount,
1542 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001543 ALOGV_IF(present_times &&
1544 present_times->swapchainCount != present_info->swapchainCount,
1545 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1546 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001547 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001548 (present_regions) ? present_regions->pRegions : nullptr;
1549 const VkPresentTimeGOOGLE* times =
1550 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001551 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001552 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001553 uint32_t nrects = 0;
1554
Jesse Halld7b994a2015-09-07 14:17:37 -07001555 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1556 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001557 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001558 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001559 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001560 const VkPresentRegionKHR* region =
1561 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001562 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001563 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001564 VkResult result;
1565 int err;
1566
Jesse Halld7b994a2015-09-07 14:17:37 -07001567 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001568 result = dispatch.QueueSignalReleaseImageANDROID(
1569 queue, present_info->waitSemaphoreCount,
1570 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001571 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001572 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001573 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001574 }
1575
Jesse Halldc225072016-05-30 22:40:14 -07001576 if (swapchain.surface.swapchain_handle ==
1577 present_info->pSwapchains[sc]) {
1578 ANativeWindow* window = swapchain.surface.window.get();
1579 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001580 if (region) {
1581 // Process the incremental-present hint for this swapchain:
1582 uint32_t rcount = region->rectangleCount;
1583 if (rcount > nrects) {
1584 android_native_rect_t* new_rects =
1585 static_cast<android_native_rect_t*>(
1586 allocator->pfnReallocation(
1587 allocator->pUserData, rects,
1588 sizeof(android_native_rect_t) * rcount,
1589 alignof(android_native_rect_t),
1590 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1591 if (new_rects) {
1592 rects = new_rects;
1593 nrects = rcount;
1594 } else {
1595 rcount = 0; // Ignore the hint for this swapchain
1596 }
1597 }
1598 for (uint32_t r = 0; r < rcount; ++r) {
1599 if (region->pRectangles[r].layer > 0) {
1600 ALOGV(
1601 "vkQueuePresentKHR ignoring invalid layer "
1602 "(%u); using layer 0 instead",
1603 region->pRectangles[r].layer);
1604 }
1605 int x = region->pRectangles[r].offset.x;
1606 int y = region->pRectangles[r].offset.y;
1607 int width = static_cast<int>(
1608 region->pRectangles[r].extent.width);
1609 int height = static_cast<int>(
1610 region->pRectangles[r].extent.height);
1611 android_native_rect_t* cur_rect = &rects[r];
1612 cur_rect->left = x;
1613 cur_rect->top = y + height;
1614 cur_rect->right = x + width;
1615 cur_rect->bottom = y;
1616 }
1617 native_window_set_surface_damage(window, rects, rcount);
1618 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001619 if (time) {
1620 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001621 ALOGV(
1622 "Calling "
1623 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001624 native_window_enable_frame_timestamps(window, true);
1625 swapchain.frame_timestamps_enabled = true;
1626 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001627
1628 // Record the nativeFrameId so it can be later correlated to
1629 // this present.
1630 uint64_t nativeFrameId = 0;
1631 err = native_window_get_next_frame_id(
1632 window, &nativeFrameId);
1633 if (err != android::NO_ERROR) {
1634 ALOGE("Failed to get next native frame ID.");
1635 }
1636
1637 // Add a new timing record with the user's presentID and
1638 // the nativeFrameId.
1639 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1640 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001641 swapchain.timing.removeAt(0);
1642 }
1643 if (time->desiredPresentTime) {
1644 // Set the desiredPresentTime:
1645 ALOGV(
1646 "Calling "
1647 "native_window_set_buffers_timestamp(%" PRId64 ")",
1648 time->desiredPresentTime);
1649 native_window_set_buffers_timestamp(
1650 window,
1651 static_cast<int64_t>(time->desiredPresentTime));
1652 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001653 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001654
Jesse Halldc225072016-05-30 22:40:14 -07001655 err = window->queueBuffer(window, img.buffer.get(), fence);
1656 // queueBuffer always closes fence, even on error
1657 if (err != 0) {
1658 // TODO(jessehall): What now? We should probably cancel the
1659 // buffer, I guess?
1660 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1661 swapchain_result = WorstPresentResult(
1662 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1663 }
1664 if (img.dequeue_fence >= 0) {
1665 close(img.dequeue_fence);
1666 img.dequeue_fence = -1;
1667 }
1668 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001669
1670 // If the swapchain is in shared mode, immediately dequeue the
1671 // buffer so it can be presented again without an intervening
1672 // call to AcquireNextImageKHR. We expect to get the same buffer
1673 // back from every call to dequeueBuffer in this mode.
1674 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1675 ANativeWindowBuffer* buffer;
1676 int fence_fd;
1677 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1678 if (err != 0) {
1679 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1680 swapchain_result = WorstPresentResult(swapchain_result,
1681 VK_ERROR_SURFACE_LOST_KHR);
1682 }
1683 else if (img.buffer != buffer) {
1684 ALOGE("got wrong image back for shared swapchain");
1685 swapchain_result = WorstPresentResult(swapchain_result,
1686 VK_ERROR_SURFACE_LOST_KHR);
1687 }
1688 else {
1689 img.dequeue_fence = fence_fd;
1690 img.dequeued = true;
1691 }
1692 }
Jesse Halldc225072016-05-30 22:40:14 -07001693 }
1694 if (swapchain_result != VK_SUCCESS) {
1695 ReleaseSwapchainImage(device, window, fence, img);
1696 OrphanSwapchain(device, &swapchain);
1697 }
1698 } else {
1699 ReleaseSwapchainImage(device, nullptr, fence, img);
1700 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001701 }
1702
Jesse Halla9e57032015-11-30 01:03:10 -08001703 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001704 present_info->pResults[sc] = swapchain_result;
1705
1706 if (swapchain_result != final_result)
1707 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001708 }
Ian Elliottcb351132016-12-13 10:30:40 -07001709 if (rects) {
1710 allocator->pfnFree(allocator->pUserData, rects);
1711 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001712
1713 return final_result;
1714}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001715
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001716VKAPI_ATTR
1717VkResult GetRefreshCycleDurationGOOGLE(
1718 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001719 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001720 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001721 ATRACE_CALL();
1722
Ian Elliott62c48c92017-01-20 13:13:20 -07001723 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001724 VkResult result = VK_SUCCESS;
1725
Brian Andersondc96fdf2017-03-20 16:54:25 -07001726 pDisplayTimingProperties->refreshDuration =
1727 static_cast<uint64_t>(swapchain.refresh_duration);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001728
1729 return result;
1730}
1731
1732VKAPI_ATTR
1733VkResult GetPastPresentationTimingGOOGLE(
1734 VkDevice,
1735 VkSwapchainKHR swapchain_handle,
1736 uint32_t* count,
1737 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001738 ATRACE_CALL();
1739
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001740 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1741 ANativeWindow* window = swapchain.surface.window.get();
1742 VkResult result = VK_SUCCESS;
1743
1744 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001745 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001746 native_window_enable_frame_timestamps(window, true);
1747 swapchain.frame_timestamps_enabled = true;
1748 }
1749
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001750 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001751 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1752 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001753 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001754 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001755 }
1756
1757 return result;
1758}
1759
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001760VKAPI_ATTR
1761VkResult GetSwapchainStatusKHR(
1762 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001763 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001764 ATRACE_CALL();
1765
Chris Forbes4e18ba82017-01-20 12:50:17 +13001766 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001767 VkResult result = VK_SUCCESS;
1768
Chris Forbes4e18ba82017-01-20 12:50:17 +13001769 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1770 return VK_ERROR_OUT_OF_DATE_KHR;
1771 }
1772
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001773 // TODO(chrisforbes): Implement this function properly
1774
1775 return result;
1776}
1777
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001778VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001779 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001780 uint32_t swapchainCount,
1781 const VkSwapchainKHR* pSwapchains,
1782 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001783 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001784
1785 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1786 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1787 if (!swapchain)
1788 continue;
1789
1790 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1791
1792 ANativeWindow* window = swapchain->surface.window.get();
1793
1794 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1795 const android_smpte2086_metadata smpteMetdata = {
1796 {vulkanMetadata.displayPrimaryRed.x,
1797 vulkanMetadata.displayPrimaryRed.y},
1798 {vulkanMetadata.displayPrimaryGreen.x,
1799 vulkanMetadata.displayPrimaryGreen.y},
1800 {vulkanMetadata.displayPrimaryBlue.x,
1801 vulkanMetadata.displayPrimaryBlue.y},
1802 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1803 vulkanMetadata.maxLuminance,
1804 vulkanMetadata.minLuminance};
1805 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1806
1807 const android_cta861_3_metadata cta8613Metadata = {
1808 vulkanMetadata.maxContentLightLevel,
1809 vulkanMetadata.maxFrameAverageLightLevel};
1810 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1811 }
1812
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001813 return;
1814}
1815
Chia-I Wu62262232016-03-26 07:06:44 +08001816} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001817} // namespace vulkan