blob: a346c0ac76f2c8522b75e1d7e12fceed89b686a6 [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
Jesse Halld7b994a2015-09-07 14:17:37 -070017#include <algorithm>
Jesse Halld7b994a2015-09-07 14:17:37 -070018
Jesse Hall79927812017-03-23 11:03:23 -070019#include <grallocusage/GrallocUsageConversion.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070020#include <log/log.h>
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -080021#include <ui/BufferQueueDefs.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070022#include <sync/sync.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080023#include <utils/StrongPointer.h>
Brian Anderson1049d1d2016-12-16 17:25:57 -080024#include <utils/Vector.h>
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070025#include <system/window.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070026
Chia-I Wu4a6a9162016-03-26 07:17:34 +080027#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070028
Jesse Hall5ae3abb2015-10-08 14:00:22 -070029// TODO(jessehall): Currently we don't have a good error code for when a native
30// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
31// versions (post SDK 0.9) of the API/extension have a better error code.
32// When updating to that version, audit all error returns.
Chia-I Wu62262232016-03-26 07:06:44 +080033namespace vulkan {
34namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070035
Jesse Halld7b994a2015-09-07 14:17:37 -070036namespace {
37
Jesse Hall55bc0972016-02-23 16:43:29 -080038const VkSurfaceTransformFlagsKHR kSupportedTransforms =
39 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
40 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
41 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
42 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
43 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
44 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
45 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
46 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
47 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
48 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
49
50VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
51 // Native and Vulkan transforms are isomorphic, but are represented
52 // differently. Vulkan transforms are built up of an optional horizontal
53 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
54 // transforms are built up from a horizontal flip, vertical flip, and
55 // 90-degree rotation, all optional but always in that order.
56
57 // TODO(jessehall): For now, only support pure rotations, not
58 // flip or flip-and-rotate, until I have more time to test them and build
59 // sample code. As far as I know we never actually use anything besides
60 // pure rotations anyway.
61
62 switch (native) {
63 case 0: // 0x0
64 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
65 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
66 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
67 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
68 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
69 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
70 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
71 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
72 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
73 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
74 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
75 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
76 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
77 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
78 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
79 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
80 default:
81 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
82 }
83}
84
Jesse Hall178b6962016-02-24 15:39:50 -080085int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
86 switch (transform) {
87 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
88 return NATIVE_WINDOW_TRANSFORM_ROT_270;
89 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
90 return NATIVE_WINDOW_TRANSFORM_ROT_180;
91 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
92 return NATIVE_WINDOW_TRANSFORM_ROT_90;
93 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
94 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
95 // return NATIVE_WINDOW_TRANSFORM_FLIP_H;
96 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
97 // return NATIVE_WINDOW_TRANSFORM_FLIP_H |
98 // NATIVE_WINDOW_TRANSFORM_ROT_90;
99 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
100 // return NATIVE_WINDOW_TRANSFORM_FLIP_V;
101 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
102 // return NATIVE_WINDOW_TRANSFORM_FLIP_V |
103 // NATIVE_WINDOW_TRANSFORM_ROT_90;
104 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
105 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
106 default:
107 return 0;
108 }
109}
110
Ian Elliott8a977262017-01-19 09:05:58 -0700111class TimingInfo {
112 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800113 TimingInfo() = default;
114 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700115 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800116 native_frame_id_(nativeFrameId) {}
117 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700118 return (timestamp_desired_present_time_ !=
119 NATIVE_WINDOW_TIMESTAMP_PENDING &&
120 timestamp_actual_present_time_ !=
121 NATIVE_WINDOW_TIMESTAMP_PENDING &&
122 timestamp_render_complete_time_ !=
123 NATIVE_WINDOW_TIMESTAMP_PENDING &&
124 timestamp_composition_latch_time_ !=
125 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700126 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700127 void calculate(int64_t rdur) {
128 bool anyTimestampInvalid =
129 (timestamp_actual_present_time_ ==
130 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
131 (timestamp_render_complete_time_ ==
132 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
133 (timestamp_composition_latch_time_ ==
134 NATIVE_WINDOW_TIMESTAMP_INVALID);
135 if (anyTimestampInvalid) {
136 ALOGE("Unexpectedly received invalid timestamp.");
137 vals_.actualPresentTime = 0;
138 vals_.earliestPresentTime = 0;
139 vals_.presentMargin = 0;
140 return;
141 }
142
143 vals_.actualPresentTime =
144 static_cast<uint64_t>(timestamp_actual_present_time_);
145 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700146 timestamp_render_complete_time_);
147 // Calculate vals_.earliestPresentTime, and potentially adjust
148 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
149 // is vals_.actualPresentTime. If we can subtract rdur (the duration
150 // of a refresh cycle) from vals_.earliestPresentTime (and also from
151 // vals_.presentMargin) and still leave a positive margin, then we can
152 // report to the application that it could have presented earlier than
153 // it did (per the extension specification). If for some reason, we
154 // can do this subtraction repeatedly, we do, since
155 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700156 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700157 while ((margin > rdur) &&
158 ((early_time - rdur) > timestamp_composition_latch_time_)) {
159 early_time -= rdur;
160 margin -= rdur;
161 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700162 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
163 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700164 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800165 void get_values(VkPastPresentationTimingGOOGLE* values) const {
166 *values = vals_;
167 }
Ian Elliott8a977262017-01-19 09:05:58 -0700168
169 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800170 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700171
Brian Anderson1049d1d2016-12-16 17:25:57 -0800172 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700173 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
174 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
175 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
176 int64_t timestamp_composition_latch_time_
177 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700178};
179
Jesse Halld7b994a2015-09-07 14:17:37 -0700180// ----------------------------------------------------------------------------
181
Jesse Hall1356b0d2015-11-23 17:24:58 -0800182struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800183 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700184 VkSwapchainKHR swapchain_handle;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800185};
186
187VkSurfaceKHR HandleFromSurface(Surface* surface) {
188 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
189}
190
191Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800192 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800193}
194
Ian Elliott8a977262017-01-19 09:05:58 -0700195// Maximum number of TimingInfo structs to keep per swapchain:
196enum { MAX_TIMING_INFOS = 10 };
197// Minimum number of frames to look for in the past (so we don't cause
198// syncronous requests to Surface Flinger):
199enum { MIN_NUM_FRAMES_AGO = 5 };
200
Jesse Hall1356b0d2015-11-23 17:24:58 -0800201struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700202 Swapchain(Surface& surface_,
203 uint32_t num_images_,
204 VkPresentModeKHR present_mode)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700205 : surface(surface_),
206 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700207 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
Chris Forbesf8835642017-03-30 19:31:40 +1300208 frame_timestamps_enabled(false),
209 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
210 present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700211 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700212 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700213 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700214 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700215 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800216
217 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700218 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700219 bool mailbox_mode;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700220 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700221 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300222 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700223
224 struct Image {
225 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
226 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800227 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700228 // The fence is only valid when the buffer is dequeued, and should be
229 // -1 any other time. When valid, we own the fd, and must ensure it is
230 // closed: either by closing it explicitly when queueing the buffer,
231 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
232 int dequeue_fence;
233 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800234 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700235
Brian Anderson1049d1d2016-12-16 17:25:57 -0800236 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700237};
238
239VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
240 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
241}
242
243Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800244 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700245}
246
Jesse Halldc225072016-05-30 22:40:14 -0700247void ReleaseSwapchainImage(VkDevice device,
248 ANativeWindow* window,
249 int release_fence,
250 Swapchain::Image& image) {
251 ALOG_ASSERT(release_fence == -1 || image.dequeued,
252 "ReleaseSwapchainImage: can't provide a release fence for "
253 "non-dequeued images");
254
255 if (image.dequeued) {
256 if (release_fence >= 0) {
257 // We get here from vkQueuePresentKHR. The application is
258 // responsible for creating an execution dependency chain from
259 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
260 // (release_fence), so we can drop the dequeue_fence here.
261 if (image.dequeue_fence >= 0)
262 close(image.dequeue_fence);
263 } else {
264 // We get here during swapchain destruction, or various serious
265 // error cases e.g. when we can't create the release_fence during
266 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
267 // have already signalled, since the swapchain images are supposed
268 // to be idle before the swapchain is destroyed. In error cases,
269 // there may be rendering in flight to the image, but since we
270 // weren't able to create a release_fence, waiting for the
271 // dequeue_fence is about the best we can do.
272 release_fence = image.dequeue_fence;
273 }
274 image.dequeue_fence = -1;
275
276 if (window) {
277 window->cancelBuffer(window, image.buffer.get(), release_fence);
278 } else {
279 if (release_fence >= 0) {
280 sync_wait(release_fence, -1 /* forever */);
281 close(release_fence);
282 }
283 }
284
285 image.dequeued = false;
286 }
287
288 if (image.image) {
289 GetData(device).driver.DestroyImage(device, image.image, nullptr);
290 image.image = VK_NULL_HANDLE;
291 }
292
293 image.buffer.clear();
294}
295
296void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
297 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
298 return;
Jesse Halldc225072016-05-30 22:40:14 -0700299 for (uint32_t i = 0; i < swapchain->num_images; i++) {
300 if (!swapchain->images[i].dequeued)
301 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
302 }
303 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700304 swapchain->timing.clear();
305}
306
307uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800308 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
309 return 0;
310 }
Ian Elliott8a977262017-01-19 09:05:58 -0700311
Brian Anderson1049d1d2016-12-16 17:25:57 -0800312 uint32_t num_ready = 0;
313 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
314 for (uint32_t i = 0; i < num_timings; i++) {
315 TimingInfo& ti = swapchain.timing.editItemAt(i);
316 if (ti.ready()) {
317 // This TimingInfo is ready to be reported to the user. Add it
318 // to the num_ready.
319 num_ready++;
320 continue;
321 }
322 // This TimingInfo is not yet ready to be reported to the user,
323 // and so we should look for any available timestamps that
324 // might make it ready.
325 int64_t desired_present_time = 0;
326 int64_t render_complete_time = 0;
327 int64_t composition_latch_time = 0;
328 int64_t actual_present_time = 0;
329 // Obtain timestamps:
330 int ret = native_window_get_frame_timestamps(
331 swapchain.surface.window.get(), ti.native_frame_id_,
332 &desired_present_time, &render_complete_time,
333 &composition_latch_time,
334 NULL, //&first_composition_start_time,
335 NULL, //&last_composition_start_time,
336 NULL, //&composition_finish_time,
337 // TODO(ianelliott): Maybe ask if this one is
338 // supported, at startup time (since it may not be
339 // supported):
340 &actual_present_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800341 NULL, //&dequeue_ready_time,
342 NULL /*&reads_done_time*/);
343
344 if (ret != android::NO_ERROR) {
345 continue;
346 }
347
348 // Record the timestamp(s) we received, and then see if this TimingInfo
349 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700350 ti.timestamp_desired_present_time_ = desired_present_time;
351 ti.timestamp_actual_present_time_ = actual_present_time;
352 ti.timestamp_render_complete_time_ = render_complete_time;
353 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800354
355 if (ti.ready()) {
356 // The TimingInfo has received enough timestamps, and should now
357 // use those timestamps to calculate the info that should be
358 // reported to the user:
359 ti.calculate(swapchain.refresh_duration);
360 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700361 }
362 }
363 return num_ready;
364}
365
366// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
367void copy_ready_timings(Swapchain& swapchain,
368 uint32_t* count,
369 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800370 if (swapchain.timing.empty()) {
371 *count = 0;
372 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700373 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800374
375 size_t last_ready = swapchain.timing.size() - 1;
376 while (!swapchain.timing[last_ready].ready()) {
377 if (last_ready == 0) {
378 *count = 0;
379 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700380 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800381 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700382 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800383
384 uint32_t num_copied = 0;
385 size_t num_to_remove = 0;
386 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
387 const TimingInfo& ti = swapchain.timing[i];
388 if (ti.ready()) {
389 ti.get_values(&timings[num_copied]);
390 num_copied++;
391 }
392 num_to_remove++;
393 }
394
395 // Discard old frames that aren't ready if newer frames are ready.
396 // We don't expect to get the timing info for those old frames.
397 swapchain.timing.removeItemsAt(0, num_to_remove);
398
Ian Elliott8a977262017-01-19 09:05:58 -0700399 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700400}
401
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700402android_pixel_format GetNativePixelFormat(VkFormat format) {
403 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
404 switch (format) {
405 case VK_FORMAT_R8G8B8A8_UNORM:
406 case VK_FORMAT_R8G8B8A8_SRGB:
407 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
408 break;
409 case VK_FORMAT_R5G6B5_UNORM_PACK16:
410 native_format = HAL_PIXEL_FORMAT_RGB_565;
411 break;
412 case VK_FORMAT_R16G16B16A16_SFLOAT:
413 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
414 break;
415 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
416 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
417 break;
418 default:
419 ALOGV("unsupported swapchain format %d", format);
420 break;
421 }
422 return native_format;
423}
424
425android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
426 switch (colorspace) {
427 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
428 return HAL_DATASPACE_V0_SRGB;
429 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
430 return HAL_DATASPACE_DISPLAY_P3;
431 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
432 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600433 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
434 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700435 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
436 return HAL_DATASPACE_DCI_P3_LINEAR;
437 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
438 return HAL_DATASPACE_DCI_P3;
439 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
440 return HAL_DATASPACE_V0_SRGB_LINEAR;
441 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
442 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600443 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
444 return HAL_DATASPACE_BT2020_LINEAR;
445 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700446 return static_cast<android_dataspace>(
447 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
448 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600449 case VK_COLOR_SPACE_DOLBYVISION_EXT:
450 return static_cast<android_dataspace>(
451 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
452 HAL_DATASPACE_RANGE_FULL);
453 case VK_COLOR_SPACE_HDR10_HLG_EXT:
454 return static_cast<android_dataspace>(
455 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
456 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700457 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
458 return static_cast<android_dataspace>(
459 HAL_DATASPACE_STANDARD_ADOBE_RGB |
460 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
461 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
462 return HAL_DATASPACE_ADOBE_RGB;
463
464 // Pass through is intended to allow app to provide data that is passed
465 // to the display system without modification.
466 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
467 return HAL_DATASPACE_ARBITRARY;
468
469 default:
470 // This indicates that we don't know about the
471 // dataspace specified and we should indicate that
472 // it's unsupported
473 return HAL_DATASPACE_UNKNOWN;
474 }
475}
476
Jesse Halld7b994a2015-09-07 14:17:37 -0700477} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700478
Jesse Halle1b12782015-11-30 11:27:32 -0800479VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800480VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800481 VkInstance instance,
482 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
483 const VkAllocationCallbacks* allocator,
484 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800485 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800486 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800487 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
488 alignof(Surface),
489 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800490 if (!mem)
491 return VK_ERROR_OUT_OF_HOST_MEMORY;
492 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700493
Chia-I Wue8e689f2016-04-18 08:21:31 +0800494 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700495 surface->swapchain_handle = VK_NULL_HANDLE;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700496
Jesse Hall1356b0d2015-11-23 17:24:58 -0800497 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
498 int err =
499 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
500 if (err != 0) {
501 // TODO(jessehall): Improve error reporting. Can we enumerate possible
502 // errors and translate them to valid Vulkan result codes?
503 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
504 err);
505 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800506 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700507 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800508 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700509
Jesse Hall1356b0d2015-11-23 17:24:58 -0800510 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700511 return VK_SUCCESS;
512}
513
Jesse Halle1b12782015-11-30 11:27:32 -0800514VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800515void DestroySurfaceKHR(VkInstance instance,
516 VkSurfaceKHR surface_handle,
517 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800518 Surface* surface = SurfaceFromHandle(surface_handle);
519 if (!surface)
520 return;
521 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700522 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700523 "destroyed VkSurfaceKHR 0x%" PRIx64
524 " has active VkSwapchainKHR 0x%" PRIx64,
525 reinterpret_cast<uint64_t>(surface_handle),
526 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800527 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800528 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800529 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800530 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800531}
532
Jesse Halle1b12782015-11-30 11:27:32 -0800533VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800534VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
535 uint32_t /*queue_family*/,
536 VkSurfaceKHR /*surface*/,
537 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800538 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800539 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800540}
541
Jesse Halle1b12782015-11-30 11:27:32 -0800542VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800543VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800544 VkPhysicalDevice /*pdev*/,
545 VkSurfaceKHR surface,
546 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700547 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800548 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700549
550 int width, height;
551 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
552 if (err != 0) {
553 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
554 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700555 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700556 }
557 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
558 if (err != 0) {
559 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
560 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700561 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700562 }
563
Jesse Hall55bc0972016-02-23 16:43:29 -0800564 int transform_hint;
565 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
566 if (err != 0) {
567 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
568 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700569 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800570 }
571
Jesse Halld7b994a2015-09-07 14:17:37 -0700572 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800573 capabilities->minImageCount = 2;
574 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700575
Jesse Hallfe2662d2016-02-09 13:26:59 -0800576 capabilities->currentExtent =
577 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
578
Jesse Halld7b994a2015-09-07 14:17:37 -0700579 // TODO(jessehall): Figure out what the max extent should be. Maximum
580 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800581 capabilities->minImageExtent = VkExtent2D{1, 1};
582 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700583
Jesse Hallfe2662d2016-02-09 13:26:59 -0800584 capabilities->maxImageArrayLayers = 1;
585
Jesse Hall55bc0972016-02-23 16:43:29 -0800586 capabilities->supportedTransforms = kSupportedTransforms;
587 capabilities->currentTransform =
588 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700589
Jesse Hallfe2662d2016-02-09 13:26:59 -0800590 // On Android, window composition is a WindowManager property, not something
591 // associated with the bufferqueue. It can't be changed from here.
592 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700593
594 // TODO(jessehall): I think these are right, but haven't thought hard about
595 // it. Do we need to query the driver for support of any of these?
596 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700597 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
598 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800599 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800600 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
601 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
602 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700603 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
604
Jesse Hallb1352bc2015-09-04 16:12:33 -0700605 return VK_SUCCESS;
606}
607
Jesse Halle1b12782015-11-30 11:27:32 -0800608VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700609VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
610 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800611 uint32_t* count,
612 VkSurfaceFormatKHR* formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700613 const InstanceData& instance_data = GetData(pdev);
614
Jesse Hall1356b0d2015-11-23 17:24:58 -0800615 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
616 // a new gralloc method to query whether a (format, usage) pair is
617 // supported, and check that for each gralloc format that corresponds to a
618 // Vulkan format. Shorter term, just add a few more formats to the ones
619 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700620
621 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700622 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
623 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
624 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700625 };
626 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700627 uint32_t total_num_formats = kNumFormats;
628
629 bool wide_color_support = false;
630 Surface& surface = *SurfaceFromHandle(surface_handle);
631 int err = native_window_get_wide_color_support(surface.window.get(),
632 &wide_color_support);
633 if (err) {
634 // Not allowed to return a more sensible error code, so do this
635 return VK_ERROR_OUT_OF_HOST_MEMORY;
636 }
637 ALOGV("wide_color_support is: %d", wide_color_support);
638 wide_color_support =
639 wide_color_support &&
640 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
641
642 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbca34c92017-02-17 11:31:23 -0700643 {VK_FORMAT_R16G16B16A16_SFLOAT,
644 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
Courtney Goeltzenleuchter3329c082017-07-09 12:50:57 -0600645 {VK_FORMAT_R16G16B16A16_SFLOAT,
646 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700647 {VK_FORMAT_A2R10G10B10_UNORM_PACK32,
648 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
649 };
650 const uint32_t kNumWideColorFormats =
651 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
652 if (wide_color_support) {
653 total_num_formats += kNumWideColorFormats;
654 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700655
656 VkResult result = VK_SUCCESS;
657 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700658 uint32_t out_count = 0;
659 uint32_t transfer_count = 0;
660 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700661 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700662 transfer_count = std::min(*count, kNumFormats);
663 std::copy(kFormats, kFormats + transfer_count, formats);
664 out_count += transfer_count;
665 if (wide_color_support) {
666 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
667 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
668 formats + out_count);
669 out_count += transfer_count;
670 }
671 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700672 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700673 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700674 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700675 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700676}
677
Jesse Halle1b12782015-11-30 11:27:32 -0800678VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300679VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
680 VkPhysicalDevice physicalDevice,
681 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
682 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
683 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
684 physicalDevice, pSurfaceInfo->surface,
685 &pSurfaceCapabilities->surfaceCapabilities);
686
Chris Forbes06bc0092017-03-16 16:46:05 +1300687 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
688 while (caps->pNext) {
689 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
690
691 switch (caps->sType) {
692 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
693 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
694 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
695 caps);
696 // Claim same set of usage flags are supported for
697 // shared present modes as for other modes.
698 shared_caps->sharedPresentSupportedUsageFlags =
699 pSurfaceCapabilities->surfaceCapabilities
700 .supportedUsageFlags;
701 } break;
702
703 default:
704 // Ignore all other extension structs
705 break;
706 }
707 }
708
Chris Forbes2452cf72017-03-16 16:30:17 +1300709 return result;
710}
711
712VKAPI_ATTR
713VkResult GetPhysicalDeviceSurfaceFormats2KHR(
714 VkPhysicalDevice physicalDevice,
715 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
716 uint32_t* pSurfaceFormatCount,
717 VkSurfaceFormat2KHR* pSurfaceFormats) {
718 if (!pSurfaceFormats) {
719 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
720 pSurfaceInfo->surface,
721 pSurfaceFormatCount, nullptr);
722 } else {
723 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
724 // after the call.
725 android::Vector<VkSurfaceFormatKHR> surface_formats;
726 surface_formats.resize(*pSurfaceFormatCount);
727 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
728 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
729 &surface_formats.editItemAt(0));
730
731 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
732 // marshal results individually due to stride difference.
733 // completely ignore any chained extension structs.
734 uint32_t formats_to_marshal = *pSurfaceFormatCount;
735 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
736 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
737 }
738 }
739
740 return result;
741 }
742}
743
744VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300745VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Chia-I Wu62262232016-03-26 07:06:44 +0800746 VkSurfaceKHR /*surface*/,
747 uint32_t* count,
748 VkPresentModeKHR* modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300749 android::Vector<VkPresentModeKHR> present_modes;
750 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
751 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
752
753 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
754 if (QueryPresentationProperties(pdev, &present_properties)) {
755 if (present_properties.sharedImage) {
756 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
757 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
758 }
759 }
760
761 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700762
763 VkResult result = VK_SUCCESS;
764 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300765 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700766 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300767 *count = std::min(*count, num_modes);
768 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700769 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300770 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700771 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700772 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700773}
774
Jesse Halle1b12782015-11-30 11:27:32 -0800775VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800776VkResult CreateSwapchainKHR(VkDevice device,
777 const VkSwapchainCreateInfoKHR* create_info,
778 const VkAllocationCallbacks* allocator,
779 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700780 int err;
781 VkResult result = VK_SUCCESS;
782
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700783 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
784 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
785 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
786 " oldSwapchain=0x%" PRIx64,
787 reinterpret_cast<uint64_t>(create_info->surface),
788 create_info->minImageCount, create_info->imageFormat,
789 create_info->imageColorSpace, create_info->imageExtent.width,
790 create_info->imageExtent.height, create_info->imageUsage,
791 create_info->preTransform, create_info->presentMode,
792 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
793
Jesse Hall1f91d392015-12-11 16:28:44 -0800794 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800795 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800796
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700797 android_pixel_format native_pixel_format =
798 GetNativePixelFormat(create_info->imageFormat);
799 android_dataspace native_dataspace =
800 GetNativeDataspace(create_info->imageColorSpace);
801 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
802 ALOGE(
803 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
804 "failed: Unsupported color space",
805 create_info->imageColorSpace);
806 return VK_ERROR_INITIALIZATION_FAILED;
807 }
808
Jesse Hall42a9eec2016-06-03 12:39:49 -0700809 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700810 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800811 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700812 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700813 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800814 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700815 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +1300816 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300817 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
818 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700819 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800820 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700821
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700822 Surface& surface = *SurfaceFromHandle(create_info->surface);
823
Jesse Halldc225072016-05-30 22:40:14 -0700824 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -0700825 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -0700826 " because it already has active swapchain 0x%" PRIx64
827 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
828 reinterpret_cast<uint64_t>(create_info->surface),
829 reinterpret_cast<uint64_t>(surface.swapchain_handle),
830 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
831 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
832 }
833 if (create_info->oldSwapchain != VK_NULL_HANDLE)
834 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
835
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700836 // -- Reset the native window --
837 // The native window might have been used previously, and had its properties
838 // changed from defaults. That will affect the answer we get for queries
839 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
840 // attempt such queries.
841
Jesse Halldc225072016-05-30 22:40:14 -0700842 // The native window only allows dequeueing all buffers before any have
843 // been queued, since after that point at least one is assumed to be in
844 // non-FREE state at any given time. Disconnecting and re-connecting
845 // orphans the previous buffers, getting us back to the state where we can
846 // dequeue all buffers.
847 err = native_window_api_disconnect(surface.window.get(),
848 NATIVE_WINDOW_API_EGL);
849 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
850 strerror(-err), err);
851 err =
852 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
853 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
854 strerror(-err), err);
855
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700856 err = native_window_set_buffer_count(surface.window.get(), 0);
857 if (err != 0) {
858 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
859 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700860 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700861 }
862
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +0530863 int swap_interval =
864 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
865 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700866 if (err != 0) {
867 // TODO(jessehall): Improve error reporting. Can we enumerate possible
868 // errors and translate them to valid Vulkan result codes?
869 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
870 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700871 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700872 }
873
Chris Forbesb8042d22017-01-18 18:07:05 +1300874 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
875 if (err != 0) {
876 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
877 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700878 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300879 }
880
881 err = native_window_set_auto_refresh(surface.window.get(), false);
882 if (err != 0) {
883 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
884 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700885 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300886 }
887
Jesse Halld7b994a2015-09-07 14:17:37 -0700888 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700889
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800890 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -0800891
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700892 err = native_window_set_buffers_format(surface.window.get(),
893 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -0800894 if (err != 0) {
895 // TODO(jessehall): Improve error reporting. Can we enumerate possible
896 // errors and translate them to valid Vulkan result codes?
897 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700898 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700899 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -0800900 }
901 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700902 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -0800903 if (err != 0) {
904 // TODO(jessehall): Improve error reporting. Can we enumerate possible
905 // errors and translate them to valid Vulkan result codes?
906 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700907 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700908 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -0800909 }
910
Jesse Hall3dd678a2016-01-08 21:52:01 -0800911 err = native_window_set_buffers_dimensions(
912 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
913 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700914 if (err != 0) {
915 // TODO(jessehall): Improve error reporting. Can we enumerate possible
916 // errors and translate them to valid Vulkan result codes?
917 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
918 create_info->imageExtent.width, create_info->imageExtent.height,
919 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700920 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700921 }
922
Jesse Hall178b6962016-02-24 15:39:50 -0800923 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
924 // applied during rendering. native_window_set_transform() expects the
925 // inverse: the transform the app is requesting that the compositor perform
926 // during composition. With native windows, pre-transform works by rendering
927 // with the same transform the compositor is applying (as in Vulkan), but
928 // then requesting the inverse transform, so that when the compositor does
929 // it's job the two transforms cancel each other out and the compositor ends
930 // up applying an identity transform to the app's buffer.
931 err = native_window_set_buffers_transform(
932 surface.window.get(),
933 InvertTransformToNative(create_info->preTransform));
934 if (err != 0) {
935 // TODO(jessehall): Improve error reporting. Can we enumerate possible
936 // errors and translate them to valid Vulkan result codes?
937 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
938 InvertTransformToNative(create_info->preTransform),
939 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700940 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -0800941 }
942
Jesse Hallf64ca122015-11-03 16:11:10 -0800943 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800944 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800945 if (err != 0) {
946 // TODO(jessehall): Improve error reporting. Can we enumerate possible
947 // errors and translate them to valid Vulkan result codes?
948 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
949 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700950 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -0800951 }
952
Chris Forbes97ef4612017-03-30 19:37:50 +1300953 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
954 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
955 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
956 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
957 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
958 if (err != 0) {
959 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
960 return VK_ERROR_SURFACE_LOST_KHR;
961 }
962 }
963
964 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
965 err = native_window_set_auto_refresh(surface.window.get(), true);
966 if (err != 0) {
967 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
968 return VK_ERROR_SURFACE_LOST_KHR;
969 }
970 }
971
Jesse Halle6080bf2016-02-28 20:58:50 -0800972 int query_value;
973 err = surface.window->query(surface.window.get(),
974 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
975 &query_value);
976 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700977 // TODO(jessehall): Improve error reporting. Can we enumerate possible
978 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -0800979 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
980 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -0700981 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700982 }
Jesse Halle6080bf2016-02-28 20:58:50 -0800983 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -0700984 uint32_t num_images =
985 (create_info->minImageCount - 1) + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +1300986
987 // Lower layer insists that we have at least two buffers. This is wasteful
988 // and we'd like to relax it in the shared case, but not all the pieces are
989 // in place for that to work yet. Note we only lie to the lower layer-- we
990 // don't want to give the app back a swapchain with extra images (which they
991 // can't actually use!).
992 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -0700993 if (err != 0) {
994 // TODO(jessehall): Improve error reporting. Can we enumerate possible
995 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700996 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
997 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700998 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700999 }
1000
Jesse Hall70f93352015-11-04 09:41:31 -08001001 int gralloc_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001002 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001003 uint64_t consumer_usage, producer_usage;
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001004 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1005 device, create_info->imageFormat, create_info->imageUsage,
1006 swapchain_image_usage, &consumer_usage, &producer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001007 if (result != VK_SUCCESS) {
1008 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001009 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001010 }
Jesse Halld1abd742017-02-09 21:45:51 -08001011 gralloc_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001012 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001013 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001014 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001015 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -08001016 &gralloc_usage);
1017 if (result != VK_SUCCESS) {
1018 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001019 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001020 }
Jesse Hall70f93352015-11-04 09:41:31 -08001021 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001022 err = native_window_set_usage(surface.window.get(), uint64_t(gralloc_usage));
Jesse Hall70f93352015-11-04 09:41:31 -08001023 if (err != 0) {
1024 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1025 // errors and translate them to valid Vulkan result codes?
1026 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001027 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001028 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001029
1030 // -- Allocate our Swapchain object --
1031 // After this point, we must deallocate the swapchain on error.
1032
Jesse Hall1f91d392015-12-11 16:28:44 -08001033 void* mem = allocator->pfnAllocation(allocator->pUserData,
1034 sizeof(Swapchain), alignof(Swapchain),
1035 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001036 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001037 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001038 Swapchain* swapchain =
1039 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001040
1041 // -- Dequeue all buffers and create a VkImage for each --
1042 // Any failures during or after this must cancel the dequeued buffers.
1043
Chris Forbesb56287a2017-01-12 14:28:58 +13001044 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1045#pragma clang diagnostic push
1046#pragma clang diagnostic ignored "-Wold-style-cast"
1047 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1048#pragma clang diagnostic pop
1049 .pNext = nullptr,
1050 .usage = swapchain_image_usage,
1051 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001052 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001053#pragma clang diagnostic push
1054#pragma clang diagnostic ignored "-Wold-style-cast"
1055 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1056#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001057 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001058 };
1059 VkImageCreateInfo image_create = {
1060 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1061 .pNext = &image_native_buffer,
1062 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001063 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001064 .extent = {0, 0, 1},
1065 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001066 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001067 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001068 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001069 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -07001070 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001071 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001072 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001073 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1074 };
1075
Jesse Halld7b994a2015-09-07 14:17:37 -07001076 for (uint32_t i = 0; i < num_images; i++) {
1077 Swapchain::Image& img = swapchain->images[i];
1078
1079 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001080 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1081 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001082 if (err != 0) {
1083 // TODO(jessehall): Improve error reporting. Can we enumerate
1084 // possible errors and translate them to valid Vulkan result codes?
1085 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001086 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001087 break;
1088 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001089 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001090 img.dequeued = true;
1091
1092 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001093 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1094 static_cast<uint32_t>(img.buffer->height),
1095 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001096 image_native_buffer.handle = img.buffer->handle;
1097 image_native_buffer.stride = img.buffer->stride;
1098 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001099 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001100 android_convertGralloc0To1Usage(int(img.buffer->usage),
1101 &image_native_buffer.usage2.producer,
1102 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001103
Jesse Hall03b6fe12015-11-24 12:44:21 -08001104 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001105 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -07001106 if (result != VK_SUCCESS) {
1107 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1108 break;
1109 }
1110 }
1111
1112 // -- Cancel all buffers, returning them to the queue --
1113 // If an error occurred before, also destroy the VkImage and release the
1114 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1115 //
1116 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1117 // but not the non-error path. Should refactor/unify.
Chris Forbese0ced032017-03-30 19:44:15 +13001118 if (!swapchain->shared) {
1119 for (uint32_t i = 0; i < num_images; i++) {
1120 Swapchain::Image& img = swapchain->images[i];
1121 if (img.dequeued) {
1122 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1123 img.dequeue_fence);
1124 img.dequeue_fence = -1;
1125 img.dequeued = false;
1126 }
1127 if (result != VK_SUCCESS) {
1128 if (img.image)
1129 dispatch.DestroyImage(device, img.image, nullptr);
1130 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001131 }
1132 }
1133
1134 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001135 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001136 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001137 return result;
1138 }
1139
Jesse Halldc225072016-05-30 22:40:14 -07001140 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1141 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001142 return VK_SUCCESS;
1143}
1144
Jesse Halle1b12782015-11-30 11:27:32 -08001145VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001146void DestroySwapchainKHR(VkDevice device,
1147 VkSwapchainKHR swapchain_handle,
1148 const VkAllocationCallbacks* allocator) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001149 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001150 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001151 if (!swapchain)
1152 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001153 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1154 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001155
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001156 if (swapchain->frame_timestamps_enabled) {
1157 native_window_enable_frame_timestamps(window, false);
1158 }
Jesse Halldc225072016-05-30 22:40:14 -07001159 for (uint32_t i = 0; i < swapchain->num_images; i++)
1160 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001161 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001162 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001163 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001164 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001165 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001166 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001167}
1168
Jesse Halle1b12782015-11-30 11:27:32 -08001169VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001170VkResult GetSwapchainImagesKHR(VkDevice,
1171 VkSwapchainKHR swapchain_handle,
1172 uint32_t* count,
1173 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001174 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001175 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1176 "getting images for non-active swapchain 0x%" PRIx64
1177 "; only dequeued image handles are valid",
1178 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001179 VkResult result = VK_SUCCESS;
1180 if (images) {
1181 uint32_t n = swapchain.num_images;
1182 if (*count < swapchain.num_images) {
1183 n = *count;
1184 result = VK_INCOMPLETE;
1185 }
1186 for (uint32_t i = 0; i < n; i++)
1187 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001188 *count = n;
1189 } else {
1190 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001191 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001192 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001193}
1194
Jesse Halle1b12782015-11-30 11:27:32 -08001195VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001196VkResult AcquireNextImageKHR(VkDevice device,
1197 VkSwapchainKHR swapchain_handle,
1198 uint64_t timeout,
1199 VkSemaphore semaphore,
1200 VkFence vk_fence,
1201 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001202 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001203 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001204 VkResult result;
1205 int err;
1206
Jesse Halldc225072016-05-30 22:40:14 -07001207 if (swapchain.surface.swapchain_handle != swapchain_handle)
1208 return VK_ERROR_OUT_OF_DATE_KHR;
1209
Jesse Halld7b994a2015-09-07 14:17:37 -07001210 ALOGW_IF(
1211 timeout != UINT64_MAX,
1212 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1213
Chris Forbesc88409c2017-03-30 19:47:37 +13001214 if (swapchain.shared) {
1215 // In shared mode, we keep the buffer dequeued all the time, so we don't
1216 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1217 // the semaphore and fence passed to us will be signalled.
1218 *image_index = 0;
1219 result = GetData(device).driver.AcquireImageANDROID(
1220 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1221 return result;
1222 }
1223
Jesse Halld7b994a2015-09-07 14:17:37 -07001224 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001225 int fence_fd;
1226 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001227 if (err != 0) {
1228 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1229 // errors and translate them to valid Vulkan result codes?
1230 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001231 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001232 }
1233
1234 uint32_t idx;
1235 for (idx = 0; idx < swapchain.num_images; idx++) {
1236 if (swapchain.images[idx].buffer.get() == buffer) {
1237 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001238 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001239 break;
1240 }
1241 }
1242 if (idx == swapchain.num_images) {
1243 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001244 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001245 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001246 }
1247
1248 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001249 if (fence_fd != -1) {
1250 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001251 if (fence_clone == -1) {
1252 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1253 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001254 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001255 }
1256 }
1257
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001258 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001259 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001260 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001261 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1262 // even if the call fails. We could close it ourselves on failure, but
1263 // that would create a race condition if the driver closes it on a
1264 // failure path: some other thread might create an fd with the same
1265 // number between the time the driver closes it and the time we close
1266 // it. We must assume one of: the driver *always* closes it even on
1267 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001268 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001269 swapchain.images[idx].dequeued = false;
1270 swapchain.images[idx].dequeue_fence = -1;
1271 return result;
1272 }
1273
1274 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001275 return VK_SUCCESS;
1276}
1277
Jesse Halldc225072016-05-30 22:40:14 -07001278static VkResult WorstPresentResult(VkResult a, VkResult b) {
1279 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1280 // (in spec version 1.0.14).
1281 static const VkResult kWorstToBest[] = {
1282 VK_ERROR_DEVICE_LOST,
1283 VK_ERROR_SURFACE_LOST_KHR,
1284 VK_ERROR_OUT_OF_DATE_KHR,
1285 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1286 VK_ERROR_OUT_OF_HOST_MEMORY,
1287 VK_SUBOPTIMAL_KHR,
1288 };
1289 for (auto result : kWorstToBest) {
1290 if (a == result || b == result)
1291 return result;
1292 }
1293 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1294 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1295 return a != VK_SUCCESS ? a : b;
1296}
1297
Jesse Halle1b12782015-11-30 11:27:32 -08001298VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001299VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001300 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1301 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1302 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001303
Jesse Halldc225072016-05-30 22:40:14 -07001304 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001305 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001306 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001307
Ian Elliottcb351132016-12-13 10:30:40 -07001308 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001309 const VkPresentRegionsKHR* present_regions = nullptr;
1310 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001311 const VkPresentRegionsKHR* next =
1312 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1313 while (next) {
1314 switch (next->sType) {
1315 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1316 present_regions = next;
1317 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001318 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001319 present_times =
1320 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1321 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001322 default:
1323 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1324 next->sType);
1325 break;
1326 }
1327 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1328 }
1329 ALOGV_IF(
1330 present_regions &&
1331 present_regions->swapchainCount != present_info->swapchainCount,
1332 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001333 ALOGV_IF(present_times &&
1334 present_times->swapchainCount != present_info->swapchainCount,
1335 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1336 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001337 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001338 (present_regions) ? present_regions->pRegions : nullptr;
1339 const VkPresentTimeGOOGLE* times =
1340 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001341 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001342 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001343 uint32_t nrects = 0;
1344
Jesse Halld7b994a2015-09-07 14:17:37 -07001345 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1346 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001347 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001348 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001349 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001350 const VkPresentRegionKHR* region =
1351 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001352 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001353 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001354 VkResult result;
1355 int err;
1356
Jesse Halld7b994a2015-09-07 14:17:37 -07001357 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001358 result = dispatch.QueueSignalReleaseImageANDROID(
1359 queue, present_info->waitSemaphoreCount,
1360 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001361 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001362 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001363 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001364 }
1365
Jesse Halldc225072016-05-30 22:40:14 -07001366 if (swapchain.surface.swapchain_handle ==
1367 present_info->pSwapchains[sc]) {
1368 ANativeWindow* window = swapchain.surface.window.get();
1369 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001370 if (region) {
1371 // Process the incremental-present hint for this swapchain:
1372 uint32_t rcount = region->rectangleCount;
1373 if (rcount > nrects) {
1374 android_native_rect_t* new_rects =
1375 static_cast<android_native_rect_t*>(
1376 allocator->pfnReallocation(
1377 allocator->pUserData, rects,
1378 sizeof(android_native_rect_t) * rcount,
1379 alignof(android_native_rect_t),
1380 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1381 if (new_rects) {
1382 rects = new_rects;
1383 nrects = rcount;
1384 } else {
1385 rcount = 0; // Ignore the hint for this swapchain
1386 }
1387 }
1388 for (uint32_t r = 0; r < rcount; ++r) {
1389 if (region->pRectangles[r].layer > 0) {
1390 ALOGV(
1391 "vkQueuePresentKHR ignoring invalid layer "
1392 "(%u); using layer 0 instead",
1393 region->pRectangles[r].layer);
1394 }
1395 int x = region->pRectangles[r].offset.x;
1396 int y = region->pRectangles[r].offset.y;
1397 int width = static_cast<int>(
1398 region->pRectangles[r].extent.width);
1399 int height = static_cast<int>(
1400 region->pRectangles[r].extent.height);
1401 android_native_rect_t* cur_rect = &rects[r];
1402 cur_rect->left = x;
1403 cur_rect->top = y + height;
1404 cur_rect->right = x + width;
1405 cur_rect->bottom = y;
1406 }
1407 native_window_set_surface_damage(window, rects, rcount);
1408 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001409 if (time) {
1410 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001411 ALOGV(
1412 "Calling "
1413 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001414 native_window_enable_frame_timestamps(window, true);
1415 swapchain.frame_timestamps_enabled = true;
1416 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001417
1418 // Record the nativeFrameId so it can be later correlated to
1419 // this present.
1420 uint64_t nativeFrameId = 0;
1421 err = native_window_get_next_frame_id(
1422 window, &nativeFrameId);
1423 if (err != android::NO_ERROR) {
1424 ALOGE("Failed to get next native frame ID.");
1425 }
1426
1427 // Add a new timing record with the user's presentID and
1428 // the nativeFrameId.
1429 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1430 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001431 swapchain.timing.removeAt(0);
1432 }
1433 if (time->desiredPresentTime) {
1434 // Set the desiredPresentTime:
1435 ALOGV(
1436 "Calling "
1437 "native_window_set_buffers_timestamp(%" PRId64 ")",
1438 time->desiredPresentTime);
1439 native_window_set_buffers_timestamp(
1440 window,
1441 static_cast<int64_t>(time->desiredPresentTime));
1442 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001443 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001444
Jesse Halldc225072016-05-30 22:40:14 -07001445 err = window->queueBuffer(window, img.buffer.get(), fence);
1446 // queueBuffer always closes fence, even on error
1447 if (err != 0) {
1448 // TODO(jessehall): What now? We should probably cancel the
1449 // buffer, I guess?
1450 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1451 swapchain_result = WorstPresentResult(
1452 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1453 }
1454 if (img.dequeue_fence >= 0) {
1455 close(img.dequeue_fence);
1456 img.dequeue_fence = -1;
1457 }
1458 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001459
1460 // If the swapchain is in shared mode, immediately dequeue the
1461 // buffer so it can be presented again without an intervening
1462 // call to AcquireNextImageKHR. We expect to get the same buffer
1463 // back from every call to dequeueBuffer in this mode.
1464 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1465 ANativeWindowBuffer* buffer;
1466 int fence_fd;
1467 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1468 if (err != 0) {
1469 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1470 swapchain_result = WorstPresentResult(swapchain_result,
1471 VK_ERROR_SURFACE_LOST_KHR);
1472 }
1473 else if (img.buffer != buffer) {
1474 ALOGE("got wrong image back for shared swapchain");
1475 swapchain_result = WorstPresentResult(swapchain_result,
1476 VK_ERROR_SURFACE_LOST_KHR);
1477 }
1478 else {
1479 img.dequeue_fence = fence_fd;
1480 img.dequeued = true;
1481 }
1482 }
Jesse Halldc225072016-05-30 22:40:14 -07001483 }
1484 if (swapchain_result != VK_SUCCESS) {
1485 ReleaseSwapchainImage(device, window, fence, img);
1486 OrphanSwapchain(device, &swapchain);
1487 }
1488 } else {
1489 ReleaseSwapchainImage(device, nullptr, fence, img);
1490 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001491 }
1492
Jesse Halla9e57032015-11-30 01:03:10 -08001493 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001494 present_info->pResults[sc] = swapchain_result;
1495
1496 if (swapchain_result != final_result)
1497 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001498 }
Ian Elliottcb351132016-12-13 10:30:40 -07001499 if (rects) {
1500 allocator->pfnFree(allocator->pUserData, rects);
1501 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001502
1503 return final_result;
1504}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001505
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001506VKAPI_ATTR
1507VkResult GetRefreshCycleDurationGOOGLE(
1508 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001509 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001510 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Ian Elliott62c48c92017-01-20 13:13:20 -07001511 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001512 VkResult result = VK_SUCCESS;
1513
Brian Andersondc96fdf2017-03-20 16:54:25 -07001514 pDisplayTimingProperties->refreshDuration =
1515 static_cast<uint64_t>(swapchain.refresh_duration);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001516
1517 return result;
1518}
1519
1520VKAPI_ATTR
1521VkResult GetPastPresentationTimingGOOGLE(
1522 VkDevice,
1523 VkSwapchainKHR swapchain_handle,
1524 uint32_t* count,
1525 VkPastPresentationTimingGOOGLE* timings) {
1526 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1527 ANativeWindow* window = swapchain.surface.window.get();
1528 VkResult result = VK_SUCCESS;
1529
1530 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001531 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001532 native_window_enable_frame_timestamps(window, true);
1533 swapchain.frame_timestamps_enabled = true;
1534 }
1535
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001536 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001537 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1538 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001539 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001540 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001541 }
1542
1543 return result;
1544}
1545
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001546VKAPI_ATTR
1547VkResult GetSwapchainStatusKHR(
1548 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001549 VkSwapchainKHR swapchain_handle) {
1550 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001551 VkResult result = VK_SUCCESS;
1552
Chris Forbes4e18ba82017-01-20 12:50:17 +13001553 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1554 return VK_ERROR_OUT_OF_DATE_KHR;
1555 }
1556
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001557 // TODO(chrisforbes): Implement this function properly
1558
1559 return result;
1560}
1561
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001562VKAPI_ATTR void SetHdrMetadataEXT(
1563 VkDevice device,
1564 uint32_t swapchainCount,
1565 const VkSwapchainKHR* pSwapchains,
1566 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
1567 // TODO: courtneygo: implement actual function
1568 (void)device;
1569 (void)swapchainCount;
1570 (void)pSwapchains;
1571 (void)pHdrMetadataEXTs;
1572 return;
1573}
1574
Chia-I Wu62262232016-03-26 07:06:44 +08001575} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001576} // namespace vulkan