swapchain.cpp revision 1ed3d7936e1d45aff12461bd981d4f50df7ff0ba
1/*
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
17#include <algorithm>
18
19#include <log/log.h>
20#include <gui/BufferQueue.h>
21#include <sync/sync.h>
22#include <utils/StrongPointer.h>
23#include <utils/Vector.h>
24
25#include "driver.h"
26
27// TODO(jessehall): Currently we don't have a good error code for when a native
28// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
29// versions (post SDK 0.9) of the API/extension have a better error code.
30// When updating to that version, audit all error returns.
31namespace vulkan {
32namespace driver {
33
34namespace {
35
36const VkSurfaceTransformFlagsKHR kSupportedTransforms =
37    VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
38    VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
39    VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
40    VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
41    // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
42    // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
43    // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
44    // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
45    // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
46    VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
47
48VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
49    // Native and Vulkan transforms are isomorphic, but are represented
50    // differently. Vulkan transforms are built up of an optional horizontal
51    // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
52    // transforms are built up from a horizontal flip, vertical flip, and
53    // 90-degree rotation, all optional but always in that order.
54
55    // TODO(jessehall): For now, only support pure rotations, not
56    // flip or flip-and-rotate, until I have more time to test them and build
57    // sample code. As far as I know we never actually use anything besides
58    // pure rotations anyway.
59
60    switch (native) {
61        case 0:  // 0x0
62            return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
63        // case NATIVE_WINDOW_TRANSFORM_FLIP_H:  // 0x1
64        //     return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
65        // case NATIVE_WINDOW_TRANSFORM_FLIP_V:  // 0x2
66        //     return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
67        case NATIVE_WINDOW_TRANSFORM_ROT_180:  // FLIP_H | FLIP_V
68            return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
69        case NATIVE_WINDOW_TRANSFORM_ROT_90:  // 0x4
70            return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
71        // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
72        //     return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
73        // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
74        //     return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
75        case NATIVE_WINDOW_TRANSFORM_ROT_270:  // FLIP_H | FLIP_V | ROT_90
76            return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
77        case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
78        default:
79            return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
80    }
81}
82
83int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
84    switch (transform) {
85        case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
86            return NATIVE_WINDOW_TRANSFORM_ROT_270;
87        case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
88            return NATIVE_WINDOW_TRANSFORM_ROT_180;
89        case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
90            return NATIVE_WINDOW_TRANSFORM_ROT_90;
91        // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
92        // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
93        //     return NATIVE_WINDOW_TRANSFORM_FLIP_H;
94        // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
95        //     return NATIVE_WINDOW_TRANSFORM_FLIP_H |
96        //            NATIVE_WINDOW_TRANSFORM_ROT_90;
97        // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
98        //     return NATIVE_WINDOW_TRANSFORM_FLIP_V;
99        // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
100        //     return NATIVE_WINDOW_TRANSFORM_FLIP_V |
101        //            NATIVE_WINDOW_TRANSFORM_ROT_90;
102        case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
103        case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
104        default:
105            return 0;
106    }
107}
108
109class TimingInfo {
110   public:
111    TimingInfo() = default;
112    TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
113        : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
114          native_frame_id_(nativeFrameId) {}
115    bool ready() const {
116        return (timestamp_desired_present_time_ &&
117                timestamp_actual_present_time_ &&
118                timestamp_render_complete_time_ &&
119                timestamp_composition_latch_time_);
120    }
121    void calculate(uint64_t rdur) {
122        vals_.actualPresentTime = timestamp_actual_present_time_;
123        uint64_t margin = (timestamp_composition_latch_time_ -
124                           timestamp_render_complete_time_);
125        // Calculate vals_.earliestPresentTime, and potentially adjust
126        // vals_.presentMargin.  The initial value of vals_.earliestPresentTime
127        // is vals_.actualPresentTime.  If we can subtract rdur (the duration
128        // of a refresh cycle) from vals_.earliestPresentTime (and also from
129        // vals_.presentMargin) and still leave a positive margin, then we can
130        // report to the application that it could have presented earlier than
131        // it did (per the extension specification).  If for some reason, we
132        // can do this subtraction repeatedly, we do, since
133        // vals_.earliestPresentTime really is supposed to be the "earliest".
134        uint64_t early_time = vals_.actualPresentTime;
135        while ((margin > rdur) &&
136               ((early_time - rdur) > timestamp_composition_latch_time_)) {
137            early_time -= rdur;
138            margin -= rdur;
139        }
140        vals_.earliestPresentTime = early_time;
141        vals_.presentMargin = margin;
142    }
143    void get_values(VkPastPresentationTimingGOOGLE* values) const {
144        *values = vals_;
145    }
146
147   public:
148    VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
149
150    uint64_t native_frame_id_ { 0 };
151    uint64_t timestamp_desired_present_time_ { 0 };
152    uint64_t timestamp_actual_present_time_ { 0 };
153    uint64_t timestamp_render_complete_time_ { 0 };
154    uint64_t timestamp_composition_latch_time_ { 0 };
155};
156
157// ----------------------------------------------------------------------------
158
159struct Surface {
160    android::sp<ANativeWindow> window;
161    VkSwapchainKHR swapchain_handle;
162};
163
164VkSurfaceKHR HandleFromSurface(Surface* surface) {
165    return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
166}
167
168Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
169    return reinterpret_cast<Surface*>(handle);
170}
171
172// Maximum number of TimingInfo structs to keep per swapchain:
173enum { MAX_TIMING_INFOS = 10 };
174// Minimum number of frames to look for in the past (so we don't cause
175// syncronous requests to Surface Flinger):
176enum { MIN_NUM_FRAMES_AGO = 5 };
177
178struct Swapchain {
179    Swapchain(Surface& surface_,
180              uint32_t num_images_,
181              VkPresentModeKHR present_mode)
182        : surface(surface_),
183          num_images(num_images_),
184          mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
185          frame_timestamps_enabled(false) {
186        ANativeWindow* window = surface.window.get();
187        int64_t rdur;
188        native_window_get_refresh_cycle_duration(
189            window,
190            &rdur);
191        refresh_duration = static_cast<uint64_t>(rdur);
192    }
193
194    Surface& surface;
195    uint32_t num_images;
196    bool mailbox_mode;
197    bool frame_timestamps_enabled;
198    uint64_t refresh_duration;
199
200    struct Image {
201        Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
202        VkImage image;
203        android::sp<ANativeWindowBuffer> buffer;
204        // The fence is only valid when the buffer is dequeued, and should be
205        // -1 any other time. When valid, we own the fd, and must ensure it is
206        // closed: either by closing it explicitly when queueing the buffer,
207        // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
208        int dequeue_fence;
209        bool dequeued;
210    } images[android::BufferQueue::NUM_BUFFER_SLOTS];
211
212    android::Vector<TimingInfo> timing;
213};
214
215VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
216    return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
217}
218
219Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
220    return reinterpret_cast<Swapchain*>(handle);
221}
222
223void ReleaseSwapchainImage(VkDevice device,
224                           ANativeWindow* window,
225                           int release_fence,
226                           Swapchain::Image& image) {
227    ALOG_ASSERT(release_fence == -1 || image.dequeued,
228                "ReleaseSwapchainImage: can't provide a release fence for "
229                "non-dequeued images");
230
231    if (image.dequeued) {
232        if (release_fence >= 0) {
233            // We get here from vkQueuePresentKHR. The application is
234            // responsible for creating an execution dependency chain from
235            // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
236            // (release_fence), so we can drop the dequeue_fence here.
237            if (image.dequeue_fence >= 0)
238                close(image.dequeue_fence);
239        } else {
240            // We get here during swapchain destruction, or various serious
241            // error cases e.g. when we can't create the release_fence during
242            // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
243            // have already signalled, since the swapchain images are supposed
244            // to be idle before the swapchain is destroyed. In error cases,
245            // there may be rendering in flight to the image, but since we
246            // weren't able to create a release_fence, waiting for the
247            // dequeue_fence is about the best we can do.
248            release_fence = image.dequeue_fence;
249        }
250        image.dequeue_fence = -1;
251
252        if (window) {
253            window->cancelBuffer(window, image.buffer.get(), release_fence);
254        } else {
255            if (release_fence >= 0) {
256                sync_wait(release_fence, -1 /* forever */);
257                close(release_fence);
258            }
259        }
260
261        image.dequeued = false;
262    }
263
264    if (image.image) {
265        GetData(device).driver.DestroyImage(device, image.image, nullptr);
266        image.image = VK_NULL_HANDLE;
267    }
268
269    image.buffer.clear();
270}
271
272void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
273    if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
274        return;
275    for (uint32_t i = 0; i < swapchain->num_images; i++) {
276        if (!swapchain->images[i].dequeued)
277            ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
278    }
279    swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
280    swapchain->timing.clear();
281}
282
283uint32_t get_num_ready_timings(Swapchain& swapchain) {
284    if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
285        return 0;
286    }
287
288    uint32_t num_ready = 0;
289    const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
290    for (uint32_t i = 0; i < num_timings; i++) {
291        TimingInfo& ti = swapchain.timing.editItemAt(i);
292        if (ti.ready()) {
293            // This TimingInfo is ready to be reported to the user.  Add it
294            // to the num_ready.
295            num_ready++;
296            continue;
297        }
298        // This TimingInfo is not yet ready to be reported to the user,
299        // and so we should look for any available timestamps that
300        // might make it ready.
301        int64_t desired_present_time = 0;
302        int64_t render_complete_time = 0;
303        int64_t composition_latch_time = 0;
304        int64_t actual_present_time = 0;
305        // Obtain timestamps:
306        int ret = native_window_get_frame_timestamps(
307            swapchain.surface.window.get(), ti.native_frame_id_,
308            &desired_present_time, &render_complete_time,
309            &composition_latch_time,
310            NULL,  //&first_composition_start_time,
311            NULL,  //&last_composition_start_time,
312            NULL,  //&composition_finish_time,
313            // TODO(ianelliott): Maybe ask if this one is
314            // supported, at startup time (since it may not be
315            // supported):
316            &actual_present_time,
317            NULL,  //&display_retire_time,
318            NULL,  //&dequeue_ready_time,
319            NULL /*&reads_done_time*/);
320
321        if (ret != android::NO_ERROR) {
322            continue;
323        }
324
325        // Record the timestamp(s) we received, and then see if this TimingInfo
326        // is ready to be reported to the user:
327        ti.timestamp_desired_present_time_ =
328            static_cast<uint64_t>(desired_present_time);
329        ti.timestamp_actual_present_time_ =
330            static_cast<uint64_t>(actual_present_time);
331        ti.timestamp_render_complete_time_ =
332            static_cast<uint64_t>(render_complete_time);
333        ti.timestamp_composition_latch_time_ =
334               static_cast<uint64_t>(composition_latch_time);
335
336        if (ti.ready()) {
337            // The TimingInfo has received enough timestamps, and should now
338            // use those timestamps to calculate the info that should be
339            // reported to the user:
340            ti.calculate(swapchain.refresh_duration);
341            num_ready++;
342        }
343    }
344    return num_ready;
345}
346
347// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
348void copy_ready_timings(Swapchain& swapchain,
349                        uint32_t* count,
350                        VkPastPresentationTimingGOOGLE* timings) {
351    if (swapchain.timing.empty()) {
352        *count = 0;
353        return;
354    }
355
356    size_t last_ready = swapchain.timing.size() - 1;
357    while (!swapchain.timing[last_ready].ready()) {
358        if (last_ready == 0) {
359            *count = 0;
360            return;
361        }
362        last_ready--;
363    }
364
365    uint32_t num_copied = 0;
366    size_t num_to_remove = 0;
367    for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
368        const TimingInfo& ti = swapchain.timing[i];
369        if (ti.ready()) {
370            ti.get_values(&timings[num_copied]);
371            num_copied++;
372        }
373        num_to_remove++;
374    }
375
376    // Discard old frames that aren't ready if newer frames are ready.
377    // We don't expect to get the timing info for those old frames.
378    swapchain.timing.removeItemsAt(0, num_to_remove);
379
380    *count = num_copied;
381}
382
383android_pixel_format GetNativePixelFormat(VkFormat format) {
384    android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
385    switch (format) {
386        case VK_FORMAT_R8G8B8A8_UNORM:
387        case VK_FORMAT_R8G8B8A8_SRGB:
388            native_format = HAL_PIXEL_FORMAT_RGBA_8888;
389            break;
390        case VK_FORMAT_R5G6B5_UNORM_PACK16:
391            native_format = HAL_PIXEL_FORMAT_RGB_565;
392            break;
393        case VK_FORMAT_R16G16B16A16_SFLOAT:
394            native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
395            break;
396        case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
397            native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
398            break;
399        default:
400            ALOGV("unsupported swapchain format %d", format);
401            break;
402    }
403    return native_format;
404}
405
406android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
407    switch (colorspace) {
408        case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
409            return HAL_DATASPACE_V0_SRGB;
410        case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
411            return HAL_DATASPACE_DISPLAY_P3;
412        case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
413            return HAL_DATASPACE_V0_SCRGB_LINEAR;
414        case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
415            return HAL_DATASPACE_V0_SCRGB;
416        case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
417            return HAL_DATASPACE_DCI_P3_LINEAR;
418        case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
419            return HAL_DATASPACE_DCI_P3;
420        case VK_COLOR_SPACE_BT709_LINEAR_EXT:
421            return HAL_DATASPACE_V0_SRGB_LINEAR;
422        case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
423            return HAL_DATASPACE_V0_SRGB;
424        case VK_COLOR_SPACE_BT2020_170M_EXT:
425            return static_cast<android_dataspace>(
426                HAL_DATASPACE_STANDARD_BT2020 |
427                HAL_DATASPACE_TRANSFER_SMPTE_170M | HAL_DATASPACE_RANGE_FULL);
428        case VK_COLOR_SPACE_BT2020_ST2084_EXT:
429            return static_cast<android_dataspace>(
430                HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
431                HAL_DATASPACE_RANGE_FULL);
432        case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
433            return static_cast<android_dataspace>(
434                HAL_DATASPACE_STANDARD_ADOBE_RGB |
435                HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
436        case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
437            return HAL_DATASPACE_ADOBE_RGB;
438
439        // Pass through is intended to allow app to provide data that is passed
440        // to the display system without modification.
441        case VK_COLOR_SPACE_PASS_THROUGH_EXT:
442            return HAL_DATASPACE_ARBITRARY;
443
444        default:
445            // This indicates that we don't know about the
446            // dataspace specified and we should indicate that
447            // it's unsupported
448            return HAL_DATASPACE_UNKNOWN;
449    }
450}
451
452}  // anonymous namespace
453
454VKAPI_ATTR
455VkResult CreateAndroidSurfaceKHR(
456    VkInstance instance,
457    const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
458    const VkAllocationCallbacks* allocator,
459    VkSurfaceKHR* out_surface) {
460    if (!allocator)
461        allocator = &GetData(instance).allocator;
462    void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
463                                         alignof(Surface),
464                                         VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
465    if (!mem)
466        return VK_ERROR_OUT_OF_HOST_MEMORY;
467    Surface* surface = new (mem) Surface;
468
469    surface->window = pCreateInfo->window;
470    surface->swapchain_handle = VK_NULL_HANDLE;
471
472    // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
473    int err =
474        native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
475    if (err != 0) {
476        // TODO(jessehall): Improve error reporting. Can we enumerate possible
477        // errors and translate them to valid Vulkan result codes?
478        ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
479              err);
480        surface->~Surface();
481        allocator->pfnFree(allocator->pUserData, surface);
482        return VK_ERROR_INITIALIZATION_FAILED;
483    }
484
485    *out_surface = HandleFromSurface(surface);
486    return VK_SUCCESS;
487}
488
489VKAPI_ATTR
490void DestroySurfaceKHR(VkInstance instance,
491                       VkSurfaceKHR surface_handle,
492                       const VkAllocationCallbacks* allocator) {
493    Surface* surface = SurfaceFromHandle(surface_handle);
494    if (!surface)
495        return;
496    native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
497    ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
498             "destroyed VkSurfaceKHR 0x%" PRIx64
499             " has active VkSwapchainKHR 0x%" PRIx64,
500             reinterpret_cast<uint64_t>(surface_handle),
501             reinterpret_cast<uint64_t>(surface->swapchain_handle));
502    surface->~Surface();
503    if (!allocator)
504        allocator = &GetData(instance).allocator;
505    allocator->pfnFree(allocator->pUserData, surface);
506}
507
508VKAPI_ATTR
509VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
510                                            uint32_t /*queue_family*/,
511                                            VkSurfaceKHR /*surface*/,
512                                            VkBool32* supported) {
513    *supported = VK_TRUE;
514    return VK_SUCCESS;
515}
516
517VKAPI_ATTR
518VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
519    VkPhysicalDevice /*pdev*/,
520    VkSurfaceKHR surface,
521    VkSurfaceCapabilitiesKHR* capabilities) {
522    int err;
523    ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
524
525    int width, height;
526    err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
527    if (err != 0) {
528        ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
529              strerror(-err), err);
530        return VK_ERROR_INITIALIZATION_FAILED;
531    }
532    err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
533    if (err != 0) {
534        ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
535              strerror(-err), err);
536        return VK_ERROR_INITIALIZATION_FAILED;
537    }
538
539    int transform_hint;
540    err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
541    if (err != 0) {
542        ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
543              strerror(-err), err);
544        return VK_ERROR_INITIALIZATION_FAILED;
545    }
546
547    // TODO(jessehall): Figure out what the min/max values should be.
548    capabilities->minImageCount = 2;
549    capabilities->maxImageCount = 3;
550
551    capabilities->currentExtent =
552        VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
553
554    // TODO(jessehall): Figure out what the max extent should be. Maximum
555    // texture dimension maybe?
556    capabilities->minImageExtent = VkExtent2D{1, 1};
557    capabilities->maxImageExtent = VkExtent2D{4096, 4096};
558
559    capabilities->maxImageArrayLayers = 1;
560
561    capabilities->supportedTransforms = kSupportedTransforms;
562    capabilities->currentTransform =
563        TranslateNativeToVulkanTransform(transform_hint);
564
565    // On Android, window composition is a WindowManager property, not something
566    // associated with the bufferqueue. It can't be changed from here.
567    capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
568
569    // TODO(jessehall): I think these are right, but haven't thought hard about
570    // it. Do we need to query the driver for support of any of these?
571    // Currently not included:
572    // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
573    // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
574    capabilities->supportedUsageFlags =
575        VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
576        VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
577        VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
578        VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
579
580    return VK_SUCCESS;
581}
582
583VKAPI_ATTR
584VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
585                                            VkSurfaceKHR surface_handle,
586                                            uint32_t* count,
587                                            VkSurfaceFormatKHR* formats) {
588    const InstanceData& instance_data = GetData(pdev);
589
590    // TODO(jessehall): Fill out the set of supported formats. Longer term, add
591    // a new gralloc method to query whether a (format, usage) pair is
592    // supported, and check that for each gralloc format that corresponds to a
593    // Vulkan format. Shorter term, just add a few more formats to the ones
594    // hardcoded below.
595
596    const VkSurfaceFormatKHR kFormats[] = {
597        {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
598        {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
599        {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
600    };
601    const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
602    uint32_t total_num_formats = kNumFormats;
603
604    bool wide_color_support = false;
605    Surface& surface = *SurfaceFromHandle(surface_handle);
606    int err = native_window_get_wide_color_support(surface.window.get(),
607                                                   &wide_color_support);
608    if (err) {
609        // Not allowed to return a more sensible error code, so do this
610        return VK_ERROR_OUT_OF_HOST_MEMORY;
611    }
612    ALOGV("wide_color_support is: %d", wide_color_support);
613    wide_color_support =
614        wide_color_support &&
615        instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
616
617    const VkSurfaceFormatKHR kWideColorFormats[] = {
618        {VK_FORMAT_R16G16B16A16_SFLOAT,
619         VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
620        {VK_FORMAT_A2R10G10B10_UNORM_PACK32,
621         VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
622    };
623    const uint32_t kNumWideColorFormats =
624        sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
625    if (wide_color_support) {
626        total_num_formats += kNumWideColorFormats;
627    }
628
629    VkResult result = VK_SUCCESS;
630    if (formats) {
631        uint32_t out_count = 0;
632        uint32_t transfer_count = 0;
633        if (*count < total_num_formats)
634            result = VK_INCOMPLETE;
635        transfer_count = std::min(*count, kNumFormats);
636        std::copy(kFormats, kFormats + transfer_count, formats);
637        out_count += transfer_count;
638        if (wide_color_support) {
639            transfer_count = std::min(*count - out_count, kNumWideColorFormats);
640            std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
641                      formats + out_count);
642            out_count += transfer_count;
643        }
644        *count = out_count;
645    } else {
646        *count = total_num_formats;
647    }
648    return result;
649}
650
651VKAPI_ATTR
652VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
653                                                 VkSurfaceKHR /*surface*/,
654                                                 uint32_t* count,
655                                                 VkPresentModeKHR* modes) {
656    android::Vector<VkPresentModeKHR> present_modes;
657    present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
658    present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
659
660    VkPhysicalDevicePresentationPropertiesANDROID present_properties;
661    if (QueryPresentationProperties(pdev, &present_properties)) {
662        if (present_properties.sharedImage) {
663            present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
664            present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
665        }
666    }
667
668    uint32_t num_modes = uint32_t(present_modes.size());
669
670    VkResult result = VK_SUCCESS;
671    if (modes) {
672        if (*count < num_modes)
673            result = VK_INCOMPLETE;
674        *count = std::min(*count, num_modes);
675        std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
676    } else {
677        *count = num_modes;
678    }
679    return result;
680}
681
682VKAPI_ATTR
683VkResult CreateSwapchainKHR(VkDevice device,
684                            const VkSwapchainCreateInfoKHR* create_info,
685                            const VkAllocationCallbacks* allocator,
686                            VkSwapchainKHR* swapchain_handle) {
687    int err;
688    VkResult result = VK_SUCCESS;
689
690    ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
691          " minImageCount=%u imageFormat=%u imageColorSpace=%u"
692          " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
693          " oldSwapchain=0x%" PRIx64,
694          reinterpret_cast<uint64_t>(create_info->surface),
695          create_info->minImageCount, create_info->imageFormat,
696          create_info->imageColorSpace, create_info->imageExtent.width,
697          create_info->imageExtent.height, create_info->imageUsage,
698          create_info->preTransform, create_info->presentMode,
699          reinterpret_cast<uint64_t>(create_info->oldSwapchain));
700
701    if (!allocator)
702        allocator = &GetData(device).allocator;
703
704    android_pixel_format native_pixel_format =
705        GetNativePixelFormat(create_info->imageFormat);
706    android_dataspace native_dataspace =
707        GetNativeDataspace(create_info->imageColorSpace);
708    if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
709        ALOGE(
710            "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
711            "failed: Unsupported color space",
712            create_info->imageColorSpace);
713        return VK_ERROR_INITIALIZATION_FAILED;
714    }
715
716    ALOGV_IF(create_info->imageArrayLayers != 1,
717             "swapchain imageArrayLayers=%u not supported",
718             create_info->imageArrayLayers);
719    ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
720             "swapchain preTransform=%#x not supported",
721             create_info->preTransform);
722    ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
723               create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
724               create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
725               create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
726             "swapchain presentMode=%u not supported",
727             create_info->presentMode);
728
729    Surface& surface = *SurfaceFromHandle(create_info->surface);
730
731    if (surface.swapchain_handle != create_info->oldSwapchain) {
732        ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
733              " because it already has active swapchain 0x%" PRIx64
734              " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
735              reinterpret_cast<uint64_t>(create_info->surface),
736              reinterpret_cast<uint64_t>(surface.swapchain_handle),
737              reinterpret_cast<uint64_t>(create_info->oldSwapchain));
738        return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
739    }
740    if (create_info->oldSwapchain != VK_NULL_HANDLE)
741        OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
742
743    // -- Reset the native window --
744    // The native window might have been used previously, and had its properties
745    // changed from defaults. That will affect the answer we get for queries
746    // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
747    // attempt such queries.
748
749    // The native window only allows dequeueing all buffers before any have
750    // been queued, since after that point at least one is assumed to be in
751    // non-FREE state at any given time. Disconnecting and re-connecting
752    // orphans the previous buffers, getting us back to the state where we can
753    // dequeue all buffers.
754    err = native_window_api_disconnect(surface.window.get(),
755                                       NATIVE_WINDOW_API_EGL);
756    ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
757             strerror(-err), err);
758    err =
759        native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
760    ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
761             strerror(-err), err);
762
763    err = native_window_set_buffer_count(surface.window.get(), 0);
764    if (err != 0) {
765        ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
766              strerror(-err), err);
767        return VK_ERROR_INITIALIZATION_FAILED;
768    }
769
770    int swap_interval =
771        create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
772    err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
773    if (err != 0) {
774        // TODO(jessehall): Improve error reporting. Can we enumerate possible
775        // errors and translate them to valid Vulkan result codes?
776        ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
777              strerror(-err), err);
778        return VK_ERROR_INITIALIZATION_FAILED;
779    }
780
781    err = native_window_set_shared_buffer_mode(surface.window.get(), false);
782    if (err != 0) {
783        ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
784              strerror(-err), err);
785        return VK_ERROR_INITIALIZATION_FAILED;
786    }
787
788    err = native_window_set_auto_refresh(surface.window.get(), false);
789    if (err != 0) {
790        ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
791              strerror(-err), err);
792        return VK_ERROR_INITIALIZATION_FAILED;
793    }
794
795    // -- Configure the native window --
796
797    const auto& dispatch = GetData(device).driver;
798
799    err = native_window_set_buffers_format(surface.window.get(),
800                                           native_pixel_format);
801    if (err != 0) {
802        // TODO(jessehall): Improve error reporting. Can we enumerate possible
803        // errors and translate them to valid Vulkan result codes?
804        ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
805              native_pixel_format, strerror(-err), err);
806        return VK_ERROR_INITIALIZATION_FAILED;
807    }
808    err = native_window_set_buffers_data_space(surface.window.get(),
809                                               native_dataspace);
810    if (err != 0) {
811        // TODO(jessehall): Improve error reporting. Can we enumerate possible
812        // errors and translate them to valid Vulkan result codes?
813        ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
814              native_dataspace, strerror(-err), err);
815        return VK_ERROR_INITIALIZATION_FAILED;
816    }
817
818    err = native_window_set_buffers_dimensions(
819        surface.window.get(), static_cast<int>(create_info->imageExtent.width),
820        static_cast<int>(create_info->imageExtent.height));
821    if (err != 0) {
822        // TODO(jessehall): Improve error reporting. Can we enumerate possible
823        // errors and translate them to valid Vulkan result codes?
824        ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
825              create_info->imageExtent.width, create_info->imageExtent.height,
826              strerror(-err), err);
827        return VK_ERROR_INITIALIZATION_FAILED;
828    }
829
830    // VkSwapchainCreateInfo::preTransform indicates the transformation the app
831    // applied during rendering. native_window_set_transform() expects the
832    // inverse: the transform the app is requesting that the compositor perform
833    // during composition. With native windows, pre-transform works by rendering
834    // with the same transform the compositor is applying (as in Vulkan), but
835    // then requesting the inverse transform, so that when the compositor does
836    // it's job the two transforms cancel each other out and the compositor ends
837    // up applying an identity transform to the app's buffer.
838    err = native_window_set_buffers_transform(
839        surface.window.get(),
840        InvertTransformToNative(create_info->preTransform));
841    if (err != 0) {
842        // TODO(jessehall): Improve error reporting. Can we enumerate possible
843        // errors and translate them to valid Vulkan result codes?
844        ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
845              InvertTransformToNative(create_info->preTransform),
846              strerror(-err), err);
847        return VK_ERROR_INITIALIZATION_FAILED;
848    }
849
850    err = native_window_set_scaling_mode(
851        surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
852    if (err != 0) {
853        // TODO(jessehall): Improve error reporting. Can we enumerate possible
854        // errors and translate them to valid Vulkan result codes?
855        ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
856              strerror(-err), err);
857        return VK_ERROR_INITIALIZATION_FAILED;
858    }
859
860    int query_value;
861    err = surface.window->query(surface.window.get(),
862                                NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
863                                &query_value);
864    if (err != 0 || query_value < 0) {
865        // TODO(jessehall): Improve error reporting. Can we enumerate possible
866        // errors and translate them to valid Vulkan result codes?
867        ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
868              query_value);
869        return VK_ERROR_INITIALIZATION_FAILED;
870    }
871    uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
872    uint32_t num_images =
873        (create_info->minImageCount - 1) + min_undequeued_buffers;
874    err = native_window_set_buffer_count(surface.window.get(), num_images);
875    if (err != 0) {
876        // TODO(jessehall): Improve error reporting. Can we enumerate possible
877        // errors and translate them to valid Vulkan result codes?
878        ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
879              strerror(-err), err);
880        return VK_ERROR_INITIALIZATION_FAILED;
881    }
882
883    VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
884    if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
885        create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
886        swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
887
888        err = native_window_set_shared_buffer_mode(surface.window.get(), true);
889        if (err != 0) {
890            ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
891            return VK_ERROR_INITIALIZATION_FAILED;
892        }
893    }
894
895    if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
896        err = native_window_set_auto_refresh(surface.window.get(), true);
897        if (err != 0) {
898            ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
899            return VK_ERROR_INITIALIZATION_FAILED;
900        }
901    }
902
903    int gralloc_usage = 0;
904    if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
905        uint64_t consumer_usage, producer_usage;
906        uint32_t driver_version = GetData(device).driver_version;
907        if (driver_version == 256587285 || driver_version == 96011958) {
908            // HACK workaround for loader/driver mismatch during transition to
909            // vkGetSwapchainGrallocUsage2ANDROID.
910            typedef VkResult(VKAPI_PTR *
911                             PFN_vkGetSwapchainGrallocUsage2ANDROID_HACK)(
912                VkDevice device, VkFormat format, VkImageUsageFlags imageUsage,
913                uint64_t * grallocConsumerUsage,
914                uint64_t * grallocProducerUsage);
915            auto get_swapchain_gralloc_usage =
916                reinterpret_cast<PFN_vkGetSwapchainGrallocUsage2ANDROID_HACK>(
917                    dispatch.GetSwapchainGrallocUsage2ANDROID);
918            result = get_swapchain_gralloc_usage(
919                device, create_info->imageFormat, create_info->imageUsage,
920                &consumer_usage, &producer_usage);
921        } else {
922            result = dispatch.GetSwapchainGrallocUsage2ANDROID(
923                device, create_info->imageFormat, create_info->imageUsage,
924                swapchain_image_usage, &consumer_usage, &producer_usage);
925        }
926        if (result != VK_SUCCESS) {
927            ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
928            return VK_ERROR_INITIALIZATION_FAILED;
929        }
930        // TODO: This is the same translation done by Gralloc1On0Adapter.
931        // Remove it once ANativeWindow has been updated to take gralloc1-style
932        // usages.
933        gralloc_usage =
934            static_cast<int>(consumer_usage) | static_cast<int>(producer_usage);
935    } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
936        result = dispatch.GetSwapchainGrallocUsageANDROID(
937            device, create_info->imageFormat, create_info->imageUsage,
938            &gralloc_usage);
939        if (result != VK_SUCCESS) {
940            ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
941            return VK_ERROR_INITIALIZATION_FAILED;
942        }
943    }
944    err = native_window_set_usage(surface.window.get(), gralloc_usage);
945    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_usage failed: %s (%d)", strerror(-err), err);
949        return VK_ERROR_INITIALIZATION_FAILED;
950    }
951
952    // -- Allocate our Swapchain object --
953    // After this point, we must deallocate the swapchain on error.
954
955    void* mem = allocator->pfnAllocation(allocator->pUserData,
956                                         sizeof(Swapchain), alignof(Swapchain),
957                                         VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
958    if (!mem)
959        return VK_ERROR_OUT_OF_HOST_MEMORY;
960    Swapchain* swapchain =
961        new (mem) Swapchain(surface, num_images, create_info->presentMode);
962
963    // -- Dequeue all buffers and create a VkImage for each --
964    // Any failures during or after this must cancel the dequeued buffers.
965
966    VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
967#pragma clang diagnostic push
968#pragma clang diagnostic ignored "-Wold-style-cast"
969        .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
970#pragma clang diagnostic pop
971        .pNext = nullptr,
972        .usage = swapchain_image_usage,
973    };
974    VkNativeBufferANDROID image_native_buffer = {
975#pragma clang diagnostic push
976#pragma clang diagnostic ignored "-Wold-style-cast"
977        .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
978#pragma clang diagnostic pop
979        .pNext = &swapchain_image_create,
980    };
981    VkImageCreateInfo image_create = {
982        .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
983        .pNext = &image_native_buffer,
984        .imageType = VK_IMAGE_TYPE_2D,
985        .format = create_info->imageFormat,
986        .extent = {0, 0, 1},
987        .mipLevels = 1,
988        .arrayLayers = 1,
989        .samples = VK_SAMPLE_COUNT_1_BIT,
990        .tiling = VK_IMAGE_TILING_OPTIMAL,
991        .usage = create_info->imageUsage,
992        .flags = 0,
993        .sharingMode = create_info->imageSharingMode,
994        .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
995        .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
996    };
997
998    for (uint32_t i = 0; i < num_images; i++) {
999        Swapchain::Image& img = swapchain->images[i];
1000
1001        ANativeWindowBuffer* buffer;
1002        err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1003                                            &img.dequeue_fence);
1004        if (err != 0) {
1005            // TODO(jessehall): Improve error reporting. Can we enumerate
1006            // possible errors and translate them to valid Vulkan result codes?
1007            ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
1008            result = VK_ERROR_INITIALIZATION_FAILED;
1009            break;
1010        }
1011        img.buffer = buffer;
1012        img.dequeued = true;
1013
1014        image_create.extent =
1015            VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1016                       static_cast<uint32_t>(img.buffer->height),
1017                       1};
1018        image_native_buffer.handle = img.buffer->handle;
1019        image_native_buffer.stride = img.buffer->stride;
1020        image_native_buffer.format = img.buffer->format;
1021        image_native_buffer.usage = img.buffer->usage;
1022        // TODO: Adjust once ANativeWindowBuffer supports gralloc1-style usage.
1023        // For now, this is the same translation Gralloc1On0Adapter does.
1024        image_native_buffer.usage2.consumer =
1025            static_cast<uint64_t>(img.buffer->usage);
1026        image_native_buffer.usage2.producer =
1027            static_cast<uint64_t>(img.buffer->usage);
1028
1029        result =
1030            dispatch.CreateImage(device, &image_create, nullptr, &img.image);
1031        if (result != VK_SUCCESS) {
1032            ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1033            break;
1034        }
1035    }
1036
1037    // -- Cancel all buffers, returning them to the queue --
1038    // If an error occurred before, also destroy the VkImage and release the
1039    // buffer reference. Otherwise, we retain a strong reference to the buffer.
1040    //
1041    // TODO(jessehall): The error path here is the same as DestroySwapchain,
1042    // but not the non-error path. Should refactor/unify.
1043    for (uint32_t i = 0; i < num_images; i++) {
1044        Swapchain::Image& img = swapchain->images[i];
1045        if (img.dequeued) {
1046            surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1047                                         img.dequeue_fence);
1048            img.dequeue_fence = -1;
1049            img.dequeued = false;
1050        }
1051        if (result != VK_SUCCESS) {
1052            if (img.image)
1053                dispatch.DestroyImage(device, img.image, nullptr);
1054        }
1055    }
1056
1057    if (result != VK_SUCCESS) {
1058        swapchain->~Swapchain();
1059        allocator->pfnFree(allocator->pUserData, swapchain);
1060        return result;
1061    }
1062
1063    surface.swapchain_handle = HandleFromSwapchain(swapchain);
1064    *swapchain_handle = surface.swapchain_handle;
1065    return VK_SUCCESS;
1066}
1067
1068VKAPI_ATTR
1069void DestroySwapchainKHR(VkDevice device,
1070                         VkSwapchainKHR swapchain_handle,
1071                         const VkAllocationCallbacks* allocator) {
1072    const auto& dispatch = GetData(device).driver;
1073    Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1074    if (!swapchain)
1075        return;
1076    bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1077    ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1078
1079    if (swapchain->frame_timestamps_enabled) {
1080        native_window_enable_frame_timestamps(window, false);
1081    }
1082    for (uint32_t i = 0; i < swapchain->num_images; i++)
1083        ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
1084    if (active)
1085        swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1086    if (!allocator)
1087        allocator = &GetData(device).allocator;
1088    swapchain->~Swapchain();
1089    allocator->pfnFree(allocator->pUserData, swapchain);
1090}
1091
1092VKAPI_ATTR
1093VkResult GetSwapchainImagesKHR(VkDevice,
1094                               VkSwapchainKHR swapchain_handle,
1095                               uint32_t* count,
1096                               VkImage* images) {
1097    Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1098    ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1099             "getting images for non-active swapchain 0x%" PRIx64
1100             "; only dequeued image handles are valid",
1101             reinterpret_cast<uint64_t>(swapchain_handle));
1102    VkResult result = VK_SUCCESS;
1103    if (images) {
1104        uint32_t n = swapchain.num_images;
1105        if (*count < swapchain.num_images) {
1106            n = *count;
1107            result = VK_INCOMPLETE;
1108        }
1109        for (uint32_t i = 0; i < n; i++)
1110            images[i] = swapchain.images[i].image;
1111        *count = n;
1112    } else {
1113        *count = swapchain.num_images;
1114    }
1115    return result;
1116}
1117
1118VKAPI_ATTR
1119VkResult AcquireNextImageKHR(VkDevice device,
1120                             VkSwapchainKHR swapchain_handle,
1121                             uint64_t timeout,
1122                             VkSemaphore semaphore,
1123                             VkFence vk_fence,
1124                             uint32_t* image_index) {
1125    Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1126    ANativeWindow* window = swapchain.surface.window.get();
1127    VkResult result;
1128    int err;
1129
1130    if (swapchain.surface.swapchain_handle != swapchain_handle)
1131        return VK_ERROR_OUT_OF_DATE_KHR;
1132
1133    ALOGW_IF(
1134        timeout != UINT64_MAX,
1135        "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1136
1137    ANativeWindowBuffer* buffer;
1138    int fence_fd;
1139    err = window->dequeueBuffer(window, &buffer, &fence_fd);
1140    if (err != 0) {
1141        // TODO(jessehall): Improve error reporting. Can we enumerate possible
1142        // errors and translate them to valid Vulkan result codes?
1143        ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1144        return VK_ERROR_INITIALIZATION_FAILED;
1145    }
1146
1147    uint32_t idx;
1148    for (idx = 0; idx < swapchain.num_images; idx++) {
1149        if (swapchain.images[idx].buffer.get() == buffer) {
1150            swapchain.images[idx].dequeued = true;
1151            swapchain.images[idx].dequeue_fence = fence_fd;
1152            break;
1153        }
1154    }
1155    if (idx == swapchain.num_images) {
1156        ALOGE("dequeueBuffer returned unrecognized buffer");
1157        window->cancelBuffer(window, buffer, fence_fd);
1158        return VK_ERROR_OUT_OF_DATE_KHR;
1159    }
1160
1161    int fence_clone = -1;
1162    if (fence_fd != -1) {
1163        fence_clone = dup(fence_fd);
1164        if (fence_clone == -1) {
1165            ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1166                  strerror(errno), errno);
1167            sync_wait(fence_fd, -1 /* forever */);
1168        }
1169    }
1170
1171    result = GetData(device).driver.AcquireImageANDROID(
1172        device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
1173    if (result != VK_SUCCESS) {
1174        // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1175        // even if the call fails. We could close it ourselves on failure, but
1176        // that would create a race condition if the driver closes it on a
1177        // failure path: some other thread might create an fd with the same
1178        // number between the time the driver closes it and the time we close
1179        // it. We must assume one of: the driver *always* closes it even on
1180        // failure, or *never* closes it on failure.
1181        window->cancelBuffer(window, buffer, fence_fd);
1182        swapchain.images[idx].dequeued = false;
1183        swapchain.images[idx].dequeue_fence = -1;
1184        return result;
1185    }
1186
1187    *image_index = idx;
1188    return VK_SUCCESS;
1189}
1190
1191static VkResult WorstPresentResult(VkResult a, VkResult b) {
1192    // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1193    // (in spec version 1.0.14).
1194    static const VkResult kWorstToBest[] = {
1195        VK_ERROR_DEVICE_LOST,
1196        VK_ERROR_SURFACE_LOST_KHR,
1197        VK_ERROR_OUT_OF_DATE_KHR,
1198        VK_ERROR_OUT_OF_DEVICE_MEMORY,
1199        VK_ERROR_OUT_OF_HOST_MEMORY,
1200        VK_SUBOPTIMAL_KHR,
1201    };
1202    for (auto result : kWorstToBest) {
1203        if (a == result || b == result)
1204            return result;
1205    }
1206    ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1207    ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1208    return a != VK_SUCCESS ? a : b;
1209}
1210
1211VKAPI_ATTR
1212VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
1213    ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1214             "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1215             present_info->sType);
1216
1217    VkDevice device = GetData(queue).driver_device;
1218    const auto& dispatch = GetData(queue).driver;
1219    VkResult final_result = VK_SUCCESS;
1220
1221    // Look at the pNext chain for supported extension structs:
1222    const VkPresentRegionsKHR* present_regions = nullptr;
1223    const VkPresentTimesInfoGOOGLE* present_times = nullptr;
1224    const VkPresentRegionsKHR* next =
1225        reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1226    while (next) {
1227        switch (next->sType) {
1228            case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1229                present_regions = next;
1230                break;
1231            case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
1232                present_times =
1233                    reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1234                break;
1235            default:
1236                ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1237                      next->sType);
1238                break;
1239        }
1240        next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1241    }
1242    ALOGV_IF(
1243        present_regions &&
1244            present_regions->swapchainCount != present_info->swapchainCount,
1245        "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
1246    ALOGV_IF(present_times &&
1247                 present_times->swapchainCount != present_info->swapchainCount,
1248             "VkPresentTimesInfoGOOGLE::swapchainCount != "
1249             "VkPresentInfo::swapchainCount");
1250    const VkPresentRegionKHR* regions =
1251        (present_regions) ? present_regions->pRegions : nullptr;
1252    const VkPresentTimeGOOGLE* times =
1253        (present_times) ? present_times->pTimes : nullptr;
1254    const VkAllocationCallbacks* allocator = &GetData(device).allocator;
1255    android_native_rect_t* rects = nullptr;
1256    uint32_t nrects = 0;
1257
1258    for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1259        Swapchain& swapchain =
1260            *SwapchainFromHandle(present_info->pSwapchains[sc]);
1261        uint32_t image_idx = present_info->pImageIndices[sc];
1262        Swapchain::Image& img = swapchain.images[image_idx];
1263        const VkPresentRegionKHR* region =
1264            (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
1265        const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
1266        VkResult swapchain_result = VK_SUCCESS;
1267        VkResult result;
1268        int err;
1269
1270        int fence = -1;
1271        result = dispatch.QueueSignalReleaseImageANDROID(
1272            queue, present_info->waitSemaphoreCount,
1273            present_info->pWaitSemaphores, img.image, &fence);
1274        if (result != VK_SUCCESS) {
1275            ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
1276            swapchain_result = result;
1277        }
1278
1279        if (swapchain.surface.swapchain_handle ==
1280            present_info->pSwapchains[sc]) {
1281            ANativeWindow* window = swapchain.surface.window.get();
1282            if (swapchain_result == VK_SUCCESS) {
1283                if (region) {
1284                    // Process the incremental-present hint for this swapchain:
1285                    uint32_t rcount = region->rectangleCount;
1286                    if (rcount > nrects) {
1287                        android_native_rect_t* new_rects =
1288                            static_cast<android_native_rect_t*>(
1289                                allocator->pfnReallocation(
1290                                    allocator->pUserData, rects,
1291                                    sizeof(android_native_rect_t) * rcount,
1292                                    alignof(android_native_rect_t),
1293                                    VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1294                        if (new_rects) {
1295                            rects = new_rects;
1296                            nrects = rcount;
1297                        } else {
1298                            rcount = 0;  // Ignore the hint for this swapchain
1299                        }
1300                    }
1301                    for (uint32_t r = 0; r < rcount; ++r) {
1302                        if (region->pRectangles[r].layer > 0) {
1303                            ALOGV(
1304                                "vkQueuePresentKHR ignoring invalid layer "
1305                                "(%u); using layer 0 instead",
1306                                region->pRectangles[r].layer);
1307                        }
1308                        int x = region->pRectangles[r].offset.x;
1309                        int y = region->pRectangles[r].offset.y;
1310                        int width = static_cast<int>(
1311                            region->pRectangles[r].extent.width);
1312                        int height = static_cast<int>(
1313                            region->pRectangles[r].extent.height);
1314                        android_native_rect_t* cur_rect = &rects[r];
1315                        cur_rect->left = x;
1316                        cur_rect->top = y + height;
1317                        cur_rect->right = x + width;
1318                        cur_rect->bottom = y;
1319                    }
1320                    native_window_set_surface_damage(window, rects, rcount);
1321                }
1322                if (time) {
1323                    if (!swapchain.frame_timestamps_enabled) {
1324                        ALOGV(
1325                            "Calling "
1326                            "native_window_enable_frame_timestamps(true)");
1327                        native_window_enable_frame_timestamps(window, true);
1328                        swapchain.frame_timestamps_enabled = true;
1329                    }
1330
1331                    // Record the nativeFrameId so it can be later correlated to
1332                    // this present.
1333                    uint64_t nativeFrameId = 0;
1334                    err = native_window_get_next_frame_id(
1335                            window, &nativeFrameId);
1336                    if (err != android::NO_ERROR) {
1337                        ALOGE("Failed to get next native frame ID.");
1338                    }
1339
1340                    // Add a new timing record with the user's presentID and
1341                    // the nativeFrameId.
1342                    swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1343                    while (swapchain.timing.size() > MAX_TIMING_INFOS) {
1344                        swapchain.timing.removeAt(0);
1345                    }
1346                    if (time->desiredPresentTime) {
1347                        // Set the desiredPresentTime:
1348                        ALOGV(
1349                            "Calling "
1350                            "native_window_set_buffers_timestamp(%" PRId64 ")",
1351                            time->desiredPresentTime);
1352                        native_window_set_buffers_timestamp(
1353                            window,
1354                            static_cast<int64_t>(time->desiredPresentTime));
1355                    }
1356                }
1357                err = window->queueBuffer(window, img.buffer.get(), fence);
1358                // queueBuffer always closes fence, even on error
1359                if (err != 0) {
1360                    // TODO(jessehall): What now? We should probably cancel the
1361                    // buffer, I guess?
1362                    ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1363                    swapchain_result = WorstPresentResult(
1364                        swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1365                }
1366                if (img.dequeue_fence >= 0) {
1367                    close(img.dequeue_fence);
1368                    img.dequeue_fence = -1;
1369                }
1370                img.dequeued = false;
1371            }
1372            if (swapchain_result != VK_SUCCESS) {
1373                ReleaseSwapchainImage(device, window, fence, img);
1374                OrphanSwapchain(device, &swapchain);
1375            }
1376        } else {
1377            ReleaseSwapchainImage(device, nullptr, fence, img);
1378            swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
1379        }
1380
1381        if (present_info->pResults)
1382            present_info->pResults[sc] = swapchain_result;
1383
1384        if (swapchain_result != final_result)
1385            final_result = WorstPresentResult(final_result, swapchain_result);
1386    }
1387    if (rects) {
1388        allocator->pfnFree(allocator->pUserData, rects);
1389    }
1390
1391    return final_result;
1392}
1393
1394VKAPI_ATTR
1395VkResult GetRefreshCycleDurationGOOGLE(
1396    VkDevice,
1397    VkSwapchainKHR swapchain_handle,
1398    VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
1399    Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1400    VkResult result = VK_SUCCESS;
1401
1402    pDisplayTimingProperties->refreshDuration = swapchain.refresh_duration;
1403
1404    return result;
1405}
1406
1407VKAPI_ATTR
1408VkResult GetPastPresentationTimingGOOGLE(
1409    VkDevice,
1410    VkSwapchainKHR swapchain_handle,
1411    uint32_t* count,
1412    VkPastPresentationTimingGOOGLE* timings) {
1413    Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1414    ANativeWindow* window = swapchain.surface.window.get();
1415    VkResult result = VK_SUCCESS;
1416
1417    if (!swapchain.frame_timestamps_enabled) {
1418        ALOGV("Calling native_window_enable_frame_timestamps(true)");
1419        native_window_enable_frame_timestamps(window, true);
1420        swapchain.frame_timestamps_enabled = true;
1421    }
1422
1423    if (timings) {
1424        // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1425        copy_ready_timings(swapchain, count, timings);
1426    } else {
1427        *count = get_num_ready_timings(swapchain);
1428    }
1429
1430    return result;
1431}
1432
1433VKAPI_ATTR
1434VkResult GetSwapchainStatusKHR(
1435    VkDevice,
1436    VkSwapchainKHR swapchain_handle) {
1437    Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1438    VkResult result = VK_SUCCESS;
1439
1440    if (swapchain.surface.swapchain_handle != swapchain_handle) {
1441        return VK_ERROR_OUT_OF_DATE_KHR;
1442    }
1443
1444    // TODO(chrisforbes): Implement this function properly
1445
1446    return result;
1447}
1448
1449VKAPI_ATTR void SetHdrMetadataEXT(
1450    VkDevice device,
1451    uint32_t swapchainCount,
1452    const VkSwapchainKHR* pSwapchains,
1453    const VkHdrMetadataEXT* pHdrMetadataEXTs) {
1454    // TODO: courtneygo: implement actual function
1455    (void)device;
1456    (void)swapchainCount;
1457    (void)pSwapchains;
1458    (void)pHdrMetadataEXTs;
1459    return;
1460}
1461
1462}  // namespace driver
1463}  // namespace vulkan
1464