• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 
19 #include <android/hardware/graphics/common/1.0/types.h>
20 #include <grallocusage/GrallocUsageConversion.h>
21 #include <graphicsenv/GraphicsEnv.h>
22 #include <log/log.h>
23 #include <sync/sync.h>
24 #include <system/window.h>
25 #include <ui/BufferQueueDefs.h>
26 #include <utils/StrongPointer.h>
27 #include <utils/Timers.h>
28 #include <utils/Trace.h>
29 
30 #include <algorithm>
31 #include <unordered_set>
32 #include <vector>
33 
34 #include "driver.h"
35 
36 using android::hardware::graphics::common::V1_0::BufferUsage;
37 
38 namespace vulkan {
39 namespace driver {
40 
41 namespace {
42 
43 const VkSurfaceTransformFlagsKHR kSupportedTransforms =
44     VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
45     VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
46     VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
47     VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
48     VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
49     VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
50     VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
51     VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
52     VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
53 
TranslateNativeToVulkanTransform(int native)54 VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
55     // Native and Vulkan transforms are isomorphic, but are represented
56     // differently. Vulkan transforms are built up of an optional horizontal
57     // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
58     // transforms are built up from a horizontal flip, vertical flip, and
59     // 90-degree rotation, all optional but always in that order.
60 
61     switch (native) {
62         case 0:
63             return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
64         case NATIVE_WINDOW_TRANSFORM_FLIP_H:
65             return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
66         case NATIVE_WINDOW_TRANSFORM_FLIP_V:
67             return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
68         case NATIVE_WINDOW_TRANSFORM_ROT_180:
69             return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
70         case NATIVE_WINDOW_TRANSFORM_ROT_90:
71             return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
72         case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
73             return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
74         case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
75             return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
76         case NATIVE_WINDOW_TRANSFORM_ROT_270:
77             return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
78         case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
79         default:
80             return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
81     }
82 }
83 
TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform)84 int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
85     switch (transform) {
86         case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
87             return NATIVE_WINDOW_TRANSFORM_ROT_90;
88         case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
89             return NATIVE_WINDOW_TRANSFORM_ROT_180;
90         case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
91             return NATIVE_WINDOW_TRANSFORM_ROT_270;
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 
InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform)109 int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
110     switch (transform) {
111         case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
112             return NATIVE_WINDOW_TRANSFORM_ROT_270;
113         case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
114             return NATIVE_WINDOW_TRANSFORM_ROT_180;
115         case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
116             return NATIVE_WINDOW_TRANSFORM_ROT_90;
117         case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
118             return NATIVE_WINDOW_TRANSFORM_FLIP_H;
119         case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
120             return NATIVE_WINDOW_TRANSFORM_FLIP_H |
121                    NATIVE_WINDOW_TRANSFORM_ROT_90;
122         case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
123             return NATIVE_WINDOW_TRANSFORM_FLIP_V;
124         case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
125             return NATIVE_WINDOW_TRANSFORM_FLIP_V |
126                    NATIVE_WINDOW_TRANSFORM_ROT_90;
127         case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
128         case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
129         default:
130             return 0;
131     }
132 }
133 
134 class TimingInfo {
135    public:
TimingInfo(const VkPresentTimeGOOGLE * qp,uint64_t nativeFrameId)136     TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
137         : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
138           native_frame_id_(nativeFrameId) {}
ready() const139     bool ready() const {
140         return (timestamp_desired_present_time_ !=
141                         NATIVE_WINDOW_TIMESTAMP_PENDING &&
142                 timestamp_actual_present_time_ !=
143                         NATIVE_WINDOW_TIMESTAMP_PENDING &&
144                 timestamp_render_complete_time_ !=
145                         NATIVE_WINDOW_TIMESTAMP_PENDING &&
146                 timestamp_composition_latch_time_ !=
147                         NATIVE_WINDOW_TIMESTAMP_PENDING);
148     }
calculate(int64_t rdur)149     void calculate(int64_t rdur) {
150         bool anyTimestampInvalid =
151                 (timestamp_actual_present_time_ ==
152                         NATIVE_WINDOW_TIMESTAMP_INVALID) ||
153                 (timestamp_render_complete_time_ ==
154                         NATIVE_WINDOW_TIMESTAMP_INVALID) ||
155                 (timestamp_composition_latch_time_ ==
156                         NATIVE_WINDOW_TIMESTAMP_INVALID);
157         if (anyTimestampInvalid) {
158             ALOGE("Unexpectedly received invalid timestamp.");
159             vals_.actualPresentTime = 0;
160             vals_.earliestPresentTime = 0;
161             vals_.presentMargin = 0;
162             return;
163         }
164 
165         vals_.actualPresentTime =
166                 static_cast<uint64_t>(timestamp_actual_present_time_);
167         int64_t margin = (timestamp_composition_latch_time_ -
168                            timestamp_render_complete_time_);
169         // Calculate vals_.earliestPresentTime, and potentially adjust
170         // vals_.presentMargin.  The initial value of vals_.earliestPresentTime
171         // is vals_.actualPresentTime.  If we can subtract rdur (the duration
172         // of a refresh cycle) from vals_.earliestPresentTime (and also from
173         // vals_.presentMargin) and still leave a positive margin, then we can
174         // report to the application that it could have presented earlier than
175         // it did (per the extension specification).  If for some reason, we
176         // can do this subtraction repeatedly, we do, since
177         // vals_.earliestPresentTime really is supposed to be the "earliest".
178         int64_t early_time = timestamp_actual_present_time_;
179         while ((margin > rdur) &&
180                ((early_time - rdur) > timestamp_composition_latch_time_)) {
181             early_time -= rdur;
182             margin -= rdur;
183         }
184         vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
185         vals_.presentMargin = static_cast<uint64_t>(margin);
186     }
get_values(VkPastPresentationTimingGOOGLE * values) const187     void get_values(VkPastPresentationTimingGOOGLE* values) const {
188         *values = vals_;
189     }
190 
191    public:
192     VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
193 
194     uint64_t native_frame_id_ { 0 };
195     int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
196     int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
197     int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
198     int64_t timestamp_composition_latch_time_
199             { NATIVE_WINDOW_TIMESTAMP_PENDING };
200 };
201 
202 struct Surface {
203     android::sp<ANativeWindow> window;
204     VkSwapchainKHR swapchain_handle;
205     uint64_t consumer_usage;
206 };
207 
HandleFromSurface(Surface * surface)208 VkSurfaceKHR HandleFromSurface(Surface* surface) {
209     return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
210 }
211 
SurfaceFromHandle(VkSurfaceKHR handle)212 Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
213     return reinterpret_cast<Surface*>(handle);
214 }
215 
216 // Maximum number of TimingInfo structs to keep per swapchain:
217 enum { MAX_TIMING_INFOS = 10 };
218 // Minimum number of frames to look for in the past (so we don't cause
219 // syncronous requests to Surface Flinger):
220 enum { MIN_NUM_FRAMES_AGO = 5 };
221 
222 struct Swapchain {
Swapchainvulkan::driver::__anon42b8a6d20111::Swapchain223     Swapchain(Surface& surface_,
224               uint32_t num_images_,
225               VkPresentModeKHR present_mode,
226               int pre_transform_)
227         : surface(surface_),
228           num_images(num_images_),
229           mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
230           pre_transform(pre_transform_),
231           frame_timestamps_enabled(false),
232           acquire_next_image_timeout(-1),
233           shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
234                  present_mode ==
235                      VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
236         ANativeWindow* window = surface.window.get();
237         native_window_get_refresh_cycle_duration(
238             window,
239             &refresh_duration);
240     }
get_refresh_durationvulkan::driver::__anon42b8a6d20111::Swapchain241     uint64_t get_refresh_duration()
242     {
243         ANativeWindow* window = surface.window.get();
244         native_window_get_refresh_cycle_duration(
245             window,
246             &refresh_duration);
247         return static_cast<uint64_t>(refresh_duration);
248 
249     }
250 
251     Surface& surface;
252     uint32_t num_images;
253     bool mailbox_mode;
254     int pre_transform;
255     bool frame_timestamps_enabled;
256     int64_t refresh_duration;
257     nsecs_t acquire_next_image_timeout;
258     bool shared;
259 
260     struct Image {
Imagevulkan::driver::__anon42b8a6d20111::Swapchain::Image261         Image()
262             : image(VK_NULL_HANDLE),
263               dequeue_fence(-1),
264               release_fence(-1),
265               dequeued(false) {}
266         VkImage image;
267         android::sp<ANativeWindowBuffer> buffer;
268         // The fence is only valid when the buffer is dequeued, and should be
269         // -1 any other time. When valid, we own the fd, and must ensure it is
270         // closed: either by closing it explicitly when queueing the buffer,
271         // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
272         int dequeue_fence;
273         // This fence is a dup of the sync fd returned from the driver via
274         // vkQueueSignalReleaseImageANDROID upon vkQueuePresentKHR. We must
275         // ensure it is closed upon re-presenting or releasing the image.
276         int release_fence;
277         bool dequeued;
278     } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
279 
280     std::vector<TimingInfo> timing;
281 };
282 
HandleFromSwapchain(Swapchain * swapchain)283 VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
284     return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
285 }
286 
SwapchainFromHandle(VkSwapchainKHR handle)287 Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
288     return reinterpret_cast<Swapchain*>(handle);
289 }
290 
IsFencePending(int fd)291 static bool IsFencePending(int fd) {
292     if (fd < 0)
293         return false;
294 
295     errno = 0;
296     return sync_wait(fd, 0 /* timeout */) == -1 && errno == ETIME;
297 }
298 
ReleaseSwapchainImage(VkDevice device,ANativeWindow * window,int release_fence,Swapchain::Image & image,bool defer_if_pending)299 void ReleaseSwapchainImage(VkDevice device,
300                            ANativeWindow* window,
301                            int release_fence,
302                            Swapchain::Image& image,
303                            bool defer_if_pending) {
304     ATRACE_CALL();
305 
306     ALOG_ASSERT(release_fence == -1 || image.dequeued,
307                 "ReleaseSwapchainImage: can't provide a release fence for "
308                 "non-dequeued images");
309 
310     if (image.dequeued) {
311         if (release_fence >= 0) {
312             // We get here from vkQueuePresentKHR. The application is
313             // responsible for creating an execution dependency chain from
314             // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
315             // (release_fence), so we can drop the dequeue_fence here.
316             if (image.dequeue_fence >= 0)
317                 close(image.dequeue_fence);
318         } else {
319             // We get here during swapchain destruction, or various serious
320             // error cases e.g. when we can't create the release_fence during
321             // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
322             // have already signalled, since the swapchain images are supposed
323             // to be idle before the swapchain is destroyed. In error cases,
324             // there may be rendering in flight to the image, but since we
325             // weren't able to create a release_fence, waiting for the
326             // dequeue_fence is about the best we can do.
327             release_fence = image.dequeue_fence;
328         }
329         image.dequeue_fence = -1;
330 
331         if (window) {
332             window->cancelBuffer(window, image.buffer.get(), release_fence);
333         } else {
334             if (release_fence >= 0) {
335                 sync_wait(release_fence, -1 /* forever */);
336                 close(release_fence);
337             }
338         }
339         release_fence = -1;
340         image.dequeued = false;
341     }
342 
343     if (defer_if_pending && IsFencePending(image.release_fence))
344         return;
345 
346     if (image.release_fence >= 0) {
347         close(image.release_fence);
348         image.release_fence = -1;
349     }
350 
351     if (image.image) {
352         ATRACE_BEGIN("DestroyImage");
353         GetData(device).driver.DestroyImage(device, image.image, nullptr);
354         ATRACE_END();
355         image.image = VK_NULL_HANDLE;
356     }
357 
358     image.buffer.clear();
359 }
360 
OrphanSwapchain(VkDevice device,Swapchain * swapchain)361 void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
362     if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
363         return;
364     for (uint32_t i = 0; i < swapchain->num_images; i++) {
365         if (!swapchain->images[i].dequeued)
366             ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i],
367                                   true);
368     }
369     swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
370     swapchain->timing.clear();
371 }
372 
get_num_ready_timings(Swapchain & swapchain)373 uint32_t get_num_ready_timings(Swapchain& swapchain) {
374     if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
375         return 0;
376     }
377 
378     uint32_t num_ready = 0;
379     const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
380     for (uint32_t i = 0; i < num_timings; i++) {
381         TimingInfo& ti = swapchain.timing[i];
382         if (ti.ready()) {
383             // This TimingInfo is ready to be reported to the user.  Add it
384             // to the num_ready.
385             num_ready++;
386             continue;
387         }
388         // This TimingInfo is not yet ready to be reported to the user,
389         // and so we should look for any available timestamps that
390         // might make it ready.
391         int64_t desired_present_time = 0;
392         int64_t render_complete_time = 0;
393         int64_t composition_latch_time = 0;
394         int64_t actual_present_time = 0;
395         // Obtain timestamps:
396         int err = native_window_get_frame_timestamps(
397             swapchain.surface.window.get(), ti.native_frame_id_,
398             &desired_present_time, &render_complete_time,
399             &composition_latch_time,
400             nullptr,  //&first_composition_start_time,
401             nullptr,  //&last_composition_start_time,
402             nullptr,  //&composition_finish_time,
403             &actual_present_time,
404             nullptr,  //&dequeue_ready_time,
405             nullptr /*&reads_done_time*/);
406 
407         if (err != android::OK) {
408             continue;
409         }
410 
411         // Record the timestamp(s) we received, and then see if this TimingInfo
412         // is ready to be reported to the user:
413         ti.timestamp_desired_present_time_ = desired_present_time;
414         ti.timestamp_actual_present_time_ = actual_present_time;
415         ti.timestamp_render_complete_time_ = render_complete_time;
416         ti.timestamp_composition_latch_time_ = composition_latch_time;
417 
418         if (ti.ready()) {
419             // The TimingInfo has received enough timestamps, and should now
420             // use those timestamps to calculate the info that should be
421             // reported to the user:
422             ti.calculate(swapchain.refresh_duration);
423             num_ready++;
424         }
425     }
426     return num_ready;
427 }
428 
copy_ready_timings(Swapchain & swapchain,uint32_t * count,VkPastPresentationTimingGOOGLE * timings)429 void copy_ready_timings(Swapchain& swapchain,
430                         uint32_t* count,
431                         VkPastPresentationTimingGOOGLE* timings) {
432     if (swapchain.timing.empty()) {
433         *count = 0;
434         return;
435     }
436 
437     size_t last_ready = swapchain.timing.size() - 1;
438     while (!swapchain.timing[last_ready].ready()) {
439         if (last_ready == 0) {
440             *count = 0;
441             return;
442         }
443         last_ready--;
444     }
445 
446     uint32_t num_copied = 0;
447     int32_t num_to_remove = 0;
448     for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
449         const TimingInfo& ti = swapchain.timing[i];
450         if (ti.ready()) {
451             ti.get_values(&timings[num_copied]);
452             num_copied++;
453         }
454         num_to_remove++;
455     }
456 
457     // Discard old frames that aren't ready if newer frames are ready.
458     // We don't expect to get the timing info for those old frames.
459     swapchain.timing.erase(swapchain.timing.begin(),
460                            swapchain.timing.begin() + num_to_remove);
461 
462     *count = num_copied;
463 }
464 
GetNativePixelFormat(VkFormat format)465 android_pixel_format GetNativePixelFormat(VkFormat format) {
466     android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
467     switch (format) {
468         case VK_FORMAT_R8G8B8A8_UNORM:
469         case VK_FORMAT_R8G8B8A8_SRGB:
470             native_format = HAL_PIXEL_FORMAT_RGBA_8888;
471             break;
472         case VK_FORMAT_R5G6B5_UNORM_PACK16:
473             native_format = HAL_PIXEL_FORMAT_RGB_565;
474             break;
475         case VK_FORMAT_R16G16B16A16_SFLOAT:
476             native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
477             break;
478         case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
479             native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
480             break;
481         default:
482             ALOGV("unsupported swapchain format %d", format);
483             break;
484     }
485     return native_format;
486 }
487 
GetNativeDataspace(VkColorSpaceKHR colorspace)488 android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
489     switch (colorspace) {
490         case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
491             return HAL_DATASPACE_V0_SRGB;
492         case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
493             return HAL_DATASPACE_DISPLAY_P3;
494         case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
495             return HAL_DATASPACE_V0_SCRGB_LINEAR;
496         case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
497             return HAL_DATASPACE_V0_SCRGB;
498         case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
499             return HAL_DATASPACE_DCI_P3_LINEAR;
500         case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
501             return HAL_DATASPACE_DCI_P3;
502         case VK_COLOR_SPACE_BT709_LINEAR_EXT:
503             return HAL_DATASPACE_V0_SRGB_LINEAR;
504         case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
505             return HAL_DATASPACE_V0_SRGB;
506         case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
507             return HAL_DATASPACE_BT2020_LINEAR;
508         case VK_COLOR_SPACE_HDR10_ST2084_EXT:
509             return static_cast<android_dataspace>(
510                 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
511                 HAL_DATASPACE_RANGE_FULL);
512         case VK_COLOR_SPACE_DOLBYVISION_EXT:
513             return static_cast<android_dataspace>(
514                 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
515                 HAL_DATASPACE_RANGE_FULL);
516         case VK_COLOR_SPACE_HDR10_HLG_EXT:
517             return static_cast<android_dataspace>(
518                 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
519                 HAL_DATASPACE_RANGE_FULL);
520         case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
521             return static_cast<android_dataspace>(
522                 HAL_DATASPACE_STANDARD_ADOBE_RGB |
523                 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
524         case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
525             return HAL_DATASPACE_ADOBE_RGB;
526 
527         // Pass through is intended to allow app to provide data that is passed
528         // to the display system without modification.
529         case VK_COLOR_SPACE_PASS_THROUGH_EXT:
530             return HAL_DATASPACE_ARBITRARY;
531 
532         default:
533             // This indicates that we don't know about the
534             // dataspace specified and we should indicate that
535             // it's unsupported
536             return HAL_DATASPACE_UNKNOWN;
537     }
538 }
539 
get_min_buffer_count(ANativeWindow * window,uint32_t * out_min_buffer_count)540 int get_min_buffer_count(ANativeWindow* window,
541                          uint32_t* out_min_buffer_count) {
542     constexpr int kExtraBuffers = 2;
543 
544     int err;
545     int min_undequeued_buffers;
546     err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
547                         &min_undequeued_buffers);
548     if (err != android::OK || min_undequeued_buffers < 0) {
549         ALOGE(
550             "NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) "
551             "value=%d",
552             strerror(-err), err, min_undequeued_buffers);
553         if (err == android::OK) {
554             err = android::UNKNOWN_ERROR;
555         }
556         return err;
557     }
558 
559     *out_min_buffer_count =
560         static_cast<uint32_t>(min_undequeued_buffers + kExtraBuffers);
561     return android::OK;
562 }
563 
564 }  // anonymous namespace
565 
566 VKAPI_ATTR
CreateAndroidSurfaceKHR(VkInstance instance,const VkAndroidSurfaceCreateInfoKHR * pCreateInfo,const VkAllocationCallbacks * allocator,VkSurfaceKHR * out_surface)567 VkResult CreateAndroidSurfaceKHR(
568     VkInstance instance,
569     const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
570     const VkAllocationCallbacks* allocator,
571     VkSurfaceKHR* out_surface) {
572     ATRACE_CALL();
573 
574     if (!allocator)
575         allocator = &GetData(instance).allocator;
576     void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
577                                          alignof(Surface),
578                                          VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
579     if (!mem)
580         return VK_ERROR_OUT_OF_HOST_MEMORY;
581     Surface* surface = new (mem) Surface;
582 
583     surface->window = pCreateInfo->window;
584     surface->swapchain_handle = VK_NULL_HANDLE;
585     int err = native_window_get_consumer_usage(surface->window.get(),
586                                                &surface->consumer_usage);
587     if (err != android::OK) {
588         ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
589               strerror(-err), err);
590         surface->~Surface();
591         allocator->pfnFree(allocator->pUserData, surface);
592         return VK_ERROR_SURFACE_LOST_KHR;
593     }
594 
595     err =
596         native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
597     if (err != android::OK) {
598         ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
599               err);
600         surface->~Surface();
601         allocator->pfnFree(allocator->pUserData, surface);
602         return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
603     }
604 
605     *out_surface = HandleFromSurface(surface);
606     return VK_SUCCESS;
607 }
608 
609 VKAPI_ATTR
DestroySurfaceKHR(VkInstance instance,VkSurfaceKHR surface_handle,const VkAllocationCallbacks * allocator)610 void DestroySurfaceKHR(VkInstance instance,
611                        VkSurfaceKHR surface_handle,
612                        const VkAllocationCallbacks* allocator) {
613     ATRACE_CALL();
614 
615     Surface* surface = SurfaceFromHandle(surface_handle);
616     if (!surface)
617         return;
618     native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
619     ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
620              "destroyed VkSurfaceKHR 0x%" PRIx64
621              " has active VkSwapchainKHR 0x%" PRIx64,
622              reinterpret_cast<uint64_t>(surface_handle),
623              reinterpret_cast<uint64_t>(surface->swapchain_handle));
624     surface->~Surface();
625     if (!allocator)
626         allocator = &GetData(instance).allocator;
627     allocator->pfnFree(allocator->pUserData, surface);
628 }
629 
630 VKAPI_ATTR
GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice,uint32_t,VkSurfaceKHR,VkBool32 * supported)631 VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
632                                             uint32_t /*queue_family*/,
633                                             VkSurfaceKHR /*surface_handle*/,
634                                             VkBool32* supported) {
635     *supported = VK_TRUE;
636     return VK_SUCCESS;
637 }
638 
639 VKAPI_ATTR
GetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice,VkSurfaceKHR surface,VkSurfaceCapabilitiesKHR * capabilities)640 VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
641     VkPhysicalDevice /*pdev*/,
642     VkSurfaceKHR surface,
643     VkSurfaceCapabilitiesKHR* capabilities) {
644     ATRACE_CALL();
645 
646     int err;
647     ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
648 
649     int width, height;
650     err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
651     if (err != android::OK) {
652         ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
653               strerror(-err), err);
654         return VK_ERROR_SURFACE_LOST_KHR;
655     }
656     err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
657     if (err != android::OK) {
658         ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
659               strerror(-err), err);
660         return VK_ERROR_SURFACE_LOST_KHR;
661     }
662 
663     int transform_hint;
664     err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
665     if (err != android::OK) {
666         ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
667               strerror(-err), err);
668         return VK_ERROR_SURFACE_LOST_KHR;
669     }
670 
671     int max_buffer_count;
672     err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
673     if (err != android::OK) {
674         ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
675               strerror(-err), err);
676         return VK_ERROR_SURFACE_LOST_KHR;
677     }
678     capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
679     capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
680 
681     capabilities->currentExtent =
682         VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
683 
684     // TODO(http://b/134182502): Figure out what the max extent should be.
685     capabilities->minImageExtent = VkExtent2D{1, 1};
686     capabilities->maxImageExtent = VkExtent2D{4096, 4096};
687 
688     capabilities->maxImageArrayLayers = 1;
689 
690     capabilities->supportedTransforms = kSupportedTransforms;
691     capabilities->currentTransform =
692         TranslateNativeToVulkanTransform(transform_hint);
693 
694     // On Android, window composition is a WindowManager property, not something
695     // associated with the bufferqueue. It can't be changed from here.
696     capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
697 
698     capabilities->supportedUsageFlags =
699         VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
700         VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
701         VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
702         VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
703 
704     return VK_SUCCESS;
705 }
706 
707 VKAPI_ATTR
GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,VkSurfaceKHR surface_handle,uint32_t * count,VkSurfaceFormatKHR * formats)708 VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
709                                             VkSurfaceKHR surface_handle,
710                                             uint32_t* count,
711                                             VkSurfaceFormatKHR* formats) {
712     ATRACE_CALL();
713 
714     const InstanceData& instance_data = GetData(pdev);
715 
716     bool wide_color_support = false;
717     Surface& surface = *SurfaceFromHandle(surface_handle);
718     int err = native_window_get_wide_color_support(surface.window.get(),
719                                                    &wide_color_support);
720     if (err) {
721         return VK_ERROR_SURFACE_LOST_KHR;
722     }
723     ALOGV("wide_color_support is: %d", wide_color_support);
724     wide_color_support =
725         wide_color_support &&
726         instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
727 
728     AHardwareBuffer_Desc desc = {};
729     desc.width = 1;
730     desc.height = 1;
731     desc.layers = 1;
732     desc.usage = surface.consumer_usage |
733                  AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
734                  AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
735 
736     // We must support R8G8B8A8
737     std::vector<VkSurfaceFormatKHR> all_formats = {
738         {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
739         {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}};
740 
741     if (wide_color_support) {
742         all_formats.emplace_back(VkSurfaceFormatKHR{
743             VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
744         all_formats.emplace_back(VkSurfaceFormatKHR{
745             VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
746     }
747 
748     desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
749     if (AHardwareBuffer_isSupported(&desc)) {
750         all_formats.emplace_back(VkSurfaceFormatKHR{
751             VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
752     }
753 
754     desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
755     if (AHardwareBuffer_isSupported(&desc)) {
756         all_formats.emplace_back(VkSurfaceFormatKHR{
757             VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
758         if (wide_color_support) {
759             all_formats.emplace_back(
760                 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
761                                    VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
762             all_formats.emplace_back(
763                 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
764                                    VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
765         }
766     }
767 
768     desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
769     if (AHardwareBuffer_isSupported(&desc)) {
770         all_formats.emplace_back(
771             VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
772                                VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
773         if (wide_color_support) {
774             all_formats.emplace_back(
775                 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
776                                    VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
777         }
778     }
779 
780     VkResult result = VK_SUCCESS;
781     if (formats) {
782         uint32_t transfer_count = all_formats.size();
783         if (transfer_count > *count) {
784             transfer_count = *count;
785             result = VK_INCOMPLETE;
786         }
787         std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
788                   formats);
789         *count = transfer_count;
790     } else {
791         *count = all_formats.size();
792     }
793 
794     return result;
795 }
796 
797 VKAPI_ATTR
GetPhysicalDeviceSurfaceCapabilities2KHR(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSurfaceInfo2KHR * pSurfaceInfo,VkSurfaceCapabilities2KHR * pSurfaceCapabilities)798 VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
799     VkPhysicalDevice physicalDevice,
800     const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
801     VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
802     ATRACE_CALL();
803 
804     VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
805         physicalDevice, pSurfaceInfo->surface,
806         &pSurfaceCapabilities->surfaceCapabilities);
807 
808     VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
809     while (caps->pNext) {
810         caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
811 
812         switch (caps->sType) {
813             case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
814                 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
815                     reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
816                         caps);
817                 // Claim same set of usage flags are supported for
818                 // shared present modes as for other modes.
819                 shared_caps->sharedPresentSupportedUsageFlags =
820                     pSurfaceCapabilities->surfaceCapabilities
821                         .supportedUsageFlags;
822             } break;
823 
824             default:
825                 // Ignore all other extension structs
826                 break;
827         }
828     }
829 
830     return result;
831 }
832 
833 VKAPI_ATTR
GetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSurfaceInfo2KHR * pSurfaceInfo,uint32_t * pSurfaceFormatCount,VkSurfaceFormat2KHR * pSurfaceFormats)834 VkResult GetPhysicalDeviceSurfaceFormats2KHR(
835     VkPhysicalDevice physicalDevice,
836     const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
837     uint32_t* pSurfaceFormatCount,
838     VkSurfaceFormat2KHR* pSurfaceFormats) {
839     ATRACE_CALL();
840 
841     if (!pSurfaceFormats) {
842         return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
843                                                   pSurfaceInfo->surface,
844                                                   pSurfaceFormatCount, nullptr);
845     } else {
846         // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
847         // after the call.
848         std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
849         VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
850             physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
851             surface_formats.data());
852 
853         if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
854             // marshal results individually due to stride difference.
855             // completely ignore any chained extension structs.
856             uint32_t formats_to_marshal = *pSurfaceFormatCount;
857             for (uint32_t i = 0u; i < formats_to_marshal; i++) {
858                 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
859             }
860         }
861 
862         return result;
863     }
864 }
865 
866 VKAPI_ATTR
GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,VkSurfaceKHR surface,uint32_t * count,VkPresentModeKHR * modes)867 VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
868                                                  VkSurfaceKHR surface,
869                                                  uint32_t* count,
870                                                  VkPresentModeKHR* modes) {
871     ATRACE_CALL();
872 
873     int err;
874     int query_value;
875     uint32_t min_buffer_count;
876     ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
877 
878     err = get_min_buffer_count(window, &min_buffer_count);
879     if (err != android::OK) {
880         return VK_ERROR_SURFACE_LOST_KHR;
881     }
882 
883     err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
884     if (err != android::OK || query_value < 0) {
885         ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
886               strerror(-err), err, query_value);
887         return VK_ERROR_SURFACE_LOST_KHR;
888     }
889     uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
890 
891     std::vector<VkPresentModeKHR> present_modes;
892     if (min_buffer_count < max_buffer_count)
893         present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
894     present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
895 
896     VkPhysicalDevicePresentationPropertiesANDROID present_properties;
897     QueryPresentationProperties(pdev, &present_properties);
898     if (present_properties.sharedImage) {
899         present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
900         present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
901     }
902 
903     uint32_t num_modes = uint32_t(present_modes.size());
904 
905     VkResult result = VK_SUCCESS;
906     if (modes) {
907         if (*count < num_modes)
908             result = VK_INCOMPLETE;
909         *count = std::min(*count, num_modes);
910         std::copy_n(present_modes.data(), *count, modes);
911     } else {
912         *count = num_modes;
913     }
914     return result;
915 }
916 
917 VKAPI_ATTR
GetDeviceGroupPresentCapabilitiesKHR(VkDevice,VkDeviceGroupPresentCapabilitiesKHR * pDeviceGroupPresentCapabilities)918 VkResult GetDeviceGroupPresentCapabilitiesKHR(
919     VkDevice,
920     VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
921     ATRACE_CALL();
922 
923     ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
924                  VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
925              "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
926              "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
927              pDeviceGroupPresentCapabilities->sType);
928 
929     memset(pDeviceGroupPresentCapabilities->presentMask, 0,
930            sizeof(pDeviceGroupPresentCapabilities->presentMask));
931 
932     // assume device group of size 1
933     pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
934     pDeviceGroupPresentCapabilities->modes =
935         VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
936 
937     return VK_SUCCESS;
938 }
939 
940 VKAPI_ATTR
GetDeviceGroupSurfacePresentModesKHR(VkDevice,VkSurfaceKHR,VkDeviceGroupPresentModeFlagsKHR * pModes)941 VkResult GetDeviceGroupSurfacePresentModesKHR(
942     VkDevice,
943     VkSurfaceKHR,
944     VkDeviceGroupPresentModeFlagsKHR* pModes) {
945     ATRACE_CALL();
946 
947     *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
948     return VK_SUCCESS;
949 }
950 
951 VKAPI_ATTR
GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,VkSurfaceKHR surface,uint32_t * pRectCount,VkRect2D * pRects)952 VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
953                                                VkSurfaceKHR surface,
954                                                uint32_t* pRectCount,
955                                                VkRect2D* pRects) {
956     ATRACE_CALL();
957 
958     if (!pRects) {
959         *pRectCount = 1;
960     } else {
961         uint32_t count = std::min(*pRectCount, 1u);
962         bool incomplete = *pRectCount < 1;
963 
964         *pRectCount = count;
965 
966         if (incomplete) {
967             return VK_INCOMPLETE;
968         }
969 
970         int err;
971         ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
972 
973         int width = 0, height = 0;
974         err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
975         if (err != android::OK) {
976             ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
977                   strerror(-err), err);
978         }
979         err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
980         if (err != android::OK) {
981             ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
982                   strerror(-err), err);
983         }
984 
985         pRects[0].offset.x = 0;
986         pRects[0].offset.y = 0;
987         pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
988                                       static_cast<uint32_t>(height)};
989     }
990     return VK_SUCCESS;
991 }
992 
DestroySwapchainInternal(VkDevice device,VkSwapchainKHR swapchain_handle,const VkAllocationCallbacks * allocator)993 static void DestroySwapchainInternal(VkDevice device,
994                                      VkSwapchainKHR swapchain_handle,
995                                      const VkAllocationCallbacks* allocator) {
996     ATRACE_CALL();
997 
998     const auto& dispatch = GetData(device).driver;
999     Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
1000     if (!swapchain) {
1001         return;
1002     }
1003 
1004     bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1005     ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
1006 
1007     if (window && swapchain->frame_timestamps_enabled) {
1008         native_window_enable_frame_timestamps(window, false);
1009     }
1010 
1011     for (uint32_t i = 0; i < swapchain->num_images; i++) {
1012         ReleaseSwapchainImage(device, window, -1, swapchain->images[i], false);
1013     }
1014 
1015     if (active) {
1016         swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1017     }
1018 
1019     if (!allocator) {
1020         allocator = &GetData(device).allocator;
1021     }
1022 
1023     swapchain->~Swapchain();
1024     allocator->pfnFree(allocator->pUserData, swapchain);
1025 }
1026 
1027 VKAPI_ATTR
CreateSwapchainKHR(VkDevice device,const VkSwapchainCreateInfoKHR * create_info,const VkAllocationCallbacks * allocator,VkSwapchainKHR * swapchain_handle)1028 VkResult CreateSwapchainKHR(VkDevice device,
1029                             const VkSwapchainCreateInfoKHR* create_info,
1030                             const VkAllocationCallbacks* allocator,
1031                             VkSwapchainKHR* swapchain_handle) {
1032     ATRACE_CALL();
1033 
1034     int err;
1035     VkResult result = VK_SUCCESS;
1036 
1037     ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1038           " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1039           " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1040           " oldSwapchain=0x%" PRIx64,
1041           reinterpret_cast<uint64_t>(create_info->surface),
1042           create_info->minImageCount, create_info->imageFormat,
1043           create_info->imageColorSpace, create_info->imageExtent.width,
1044           create_info->imageExtent.height, create_info->imageUsage,
1045           create_info->preTransform, create_info->presentMode,
1046           reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1047 
1048     if (!allocator)
1049         allocator = &GetData(device).allocator;
1050 
1051     android_pixel_format native_pixel_format =
1052         GetNativePixelFormat(create_info->imageFormat);
1053     android_dataspace native_dataspace =
1054         GetNativeDataspace(create_info->imageColorSpace);
1055     if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1056         ALOGE(
1057             "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1058             "failed: Unsupported color space",
1059             create_info->imageColorSpace);
1060         return VK_ERROR_INITIALIZATION_FAILED;
1061     }
1062 
1063     ALOGV_IF(create_info->imageArrayLayers != 1,
1064              "swapchain imageArrayLayers=%u not supported",
1065              create_info->imageArrayLayers);
1066     ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
1067              "swapchain preTransform=%#x not supported",
1068              create_info->preTransform);
1069     ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
1070                create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
1071                create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1072                create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
1073              "swapchain presentMode=%u not supported",
1074              create_info->presentMode);
1075 
1076     Surface& surface = *SurfaceFromHandle(create_info->surface);
1077 
1078     if (surface.swapchain_handle != create_info->oldSwapchain) {
1079         ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
1080               " because it already has active swapchain 0x%" PRIx64
1081               " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1082               reinterpret_cast<uint64_t>(create_info->surface),
1083               reinterpret_cast<uint64_t>(surface.swapchain_handle),
1084               reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1085         return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1086     }
1087     if (create_info->oldSwapchain != VK_NULL_HANDLE)
1088         OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1089 
1090     // -- Reset the native window --
1091     // The native window might have been used previously, and had its properties
1092     // changed from defaults. That will affect the answer we get for queries
1093     // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1094     // attempt such queries.
1095 
1096     // The native window only allows dequeueing all buffers before any have
1097     // been queued, since after that point at least one is assumed to be in
1098     // non-FREE state at any given time. Disconnecting and re-connecting
1099     // orphans the previous buffers, getting us back to the state where we can
1100     // dequeue all buffers.
1101     //
1102     // TODO(http://b/134186185) recycle swapchain images more efficiently
1103     ANativeWindow* window = surface.window.get();
1104     err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1105     ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
1106              strerror(-err), err);
1107     err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1108     ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
1109              strerror(-err), err);
1110 
1111     err =
1112         window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, nsecs_t{-1});
1113     if (err != android::OK) {
1114         ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1115               strerror(-err), err);
1116         return VK_ERROR_SURFACE_LOST_KHR;
1117     }
1118 
1119     int swap_interval =
1120         create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1121     err = window->setSwapInterval(window, swap_interval);
1122     if (err != android::OK) {
1123         ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1124               strerror(-err), err);
1125         return VK_ERROR_SURFACE_LOST_KHR;
1126     }
1127 
1128     err = native_window_set_shared_buffer_mode(window, false);
1129     if (err != android::OK) {
1130         ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1131               strerror(-err), err);
1132         return VK_ERROR_SURFACE_LOST_KHR;
1133     }
1134 
1135     err = native_window_set_auto_refresh(window, false);
1136     if (err != android::OK) {
1137         ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1138               strerror(-err), err);
1139         return VK_ERROR_SURFACE_LOST_KHR;
1140     }
1141 
1142     // -- Configure the native window --
1143 
1144     const auto& dispatch = GetData(device).driver;
1145 
1146     err = native_window_set_buffers_format(window, native_pixel_format);
1147     if (err != android::OK) {
1148         ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
1149               native_pixel_format, strerror(-err), err);
1150         return VK_ERROR_SURFACE_LOST_KHR;
1151     }
1152     err = native_window_set_buffers_data_space(window, native_dataspace);
1153     if (err != android::OK) {
1154         ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1155               native_dataspace, strerror(-err), err);
1156         return VK_ERROR_SURFACE_LOST_KHR;
1157     }
1158 
1159     err = native_window_set_buffers_dimensions(
1160         window, static_cast<int>(create_info->imageExtent.width),
1161         static_cast<int>(create_info->imageExtent.height));
1162     if (err != android::OK) {
1163         ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1164               create_info->imageExtent.width, create_info->imageExtent.height,
1165               strerror(-err), err);
1166         return VK_ERROR_SURFACE_LOST_KHR;
1167     }
1168 
1169     // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1170     // applied during rendering. native_window_set_transform() expects the
1171     // inverse: the transform the app is requesting that the compositor perform
1172     // during composition. With native windows, pre-transform works by rendering
1173     // with the same transform the compositor is applying (as in Vulkan), but
1174     // then requesting the inverse transform, so that when the compositor does
1175     // it's job the two transforms cancel each other out and the compositor ends
1176     // up applying an identity transform to the app's buffer.
1177     err = native_window_set_buffers_transform(
1178         window, InvertTransformToNative(create_info->preTransform));
1179     if (err != android::OK) {
1180         ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1181               InvertTransformToNative(create_info->preTransform),
1182               strerror(-err), err);
1183         return VK_ERROR_SURFACE_LOST_KHR;
1184     }
1185 
1186     err = native_window_set_scaling_mode(
1187         window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1188     if (err != android::OK) {
1189         ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1190               strerror(-err), err);
1191         return VK_ERROR_SURFACE_LOST_KHR;
1192     }
1193 
1194     VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1195     if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1196         create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1197         swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1198         err = native_window_set_shared_buffer_mode(window, true);
1199         if (err != android::OK) {
1200             ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1201             return VK_ERROR_SURFACE_LOST_KHR;
1202         }
1203     }
1204 
1205     if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1206         err = native_window_set_auto_refresh(window, true);
1207         if (err != android::OK) {
1208             ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1209             return VK_ERROR_SURFACE_LOST_KHR;
1210         }
1211     }
1212 
1213     uint32_t min_buffer_count;
1214     err = get_min_buffer_count(window, &min_buffer_count);
1215     if (err != android::OK) {
1216         return VK_ERROR_SURFACE_LOST_KHR;
1217     }
1218 
1219     uint32_t num_images =
1220         std::max(min_buffer_count, create_info->minImageCount);
1221 
1222     // Lower layer insists that we have at least two buffers. This is wasteful
1223     // and we'd like to relax it in the shared case, but not all the pieces are
1224     // in place for that to work yet. Note we only lie to the lower layer-- we
1225     // don't want to give the app back a swapchain with extra images (which they
1226     // can't actually use!).
1227     err = native_window_set_buffer_count(window, std::max(2u, num_images));
1228     if (err != android::OK) {
1229         ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1230               strerror(-err), err);
1231         return VK_ERROR_SURFACE_LOST_KHR;
1232     }
1233 
1234     int32_t legacy_usage = 0;
1235     if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
1236         uint64_t consumer_usage, producer_usage;
1237         ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
1238         result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1239             device, create_info->imageFormat, create_info->imageUsage,
1240             swapchain_image_usage, &consumer_usage, &producer_usage);
1241         ATRACE_END();
1242         if (result != VK_SUCCESS) {
1243             ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
1244             return VK_ERROR_SURFACE_LOST_KHR;
1245         }
1246         legacy_usage =
1247             android_convertGralloc1To0Usage(producer_usage, consumer_usage);
1248     } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
1249         ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
1250         result = dispatch.GetSwapchainGrallocUsageANDROID(
1251             device, create_info->imageFormat, create_info->imageUsage,
1252             &legacy_usage);
1253         ATRACE_END();
1254         if (result != VK_SUCCESS) {
1255             ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
1256             return VK_ERROR_SURFACE_LOST_KHR;
1257         }
1258     }
1259     uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1260 
1261     bool createProtectedSwapchain = false;
1262     if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1263         createProtectedSwapchain = true;
1264         native_usage |= BufferUsage::PROTECTED;
1265     }
1266     err = native_window_set_usage(window, native_usage);
1267     if (err != android::OK) {
1268         ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
1269         return VK_ERROR_SURFACE_LOST_KHR;
1270     }
1271 
1272     int transform_hint;
1273     err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1274     if (err != android::OK) {
1275         ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1276               strerror(-err), err);
1277         return VK_ERROR_SURFACE_LOST_KHR;
1278     }
1279 
1280     // -- Allocate our Swapchain object --
1281     // After this point, we must deallocate the swapchain on error.
1282 
1283     void* mem = allocator->pfnAllocation(allocator->pUserData,
1284                                          sizeof(Swapchain), alignof(Swapchain),
1285                                          VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1286     if (!mem)
1287         return VK_ERROR_OUT_OF_HOST_MEMORY;
1288     Swapchain* swapchain = new (mem)
1289         Swapchain(surface, num_images, create_info->presentMode,
1290                   TranslateVulkanToNativeTransform(create_info->preTransform));
1291     // -- Dequeue all buffers and create a VkImage for each --
1292     // Any failures during or after this must cancel the dequeued buffers.
1293 
1294     VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1295 #pragma clang diagnostic push
1296 #pragma clang diagnostic ignored "-Wold-style-cast"
1297         .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1298 #pragma clang diagnostic pop
1299         .pNext = nullptr,
1300         .usage = swapchain_image_usage,
1301     };
1302     VkNativeBufferANDROID image_native_buffer = {
1303 #pragma clang diagnostic push
1304 #pragma clang diagnostic ignored "-Wold-style-cast"
1305         .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1306 #pragma clang diagnostic pop
1307         .pNext = &swapchain_image_create,
1308     };
1309     VkImageCreateInfo image_create = {
1310         .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1311         .pNext = &image_native_buffer,
1312         .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
1313         .imageType = VK_IMAGE_TYPE_2D,
1314         .format = create_info->imageFormat,
1315         .extent = {0, 0, 1},
1316         .mipLevels = 1,
1317         .arrayLayers = 1,
1318         .samples = VK_SAMPLE_COUNT_1_BIT,
1319         .tiling = VK_IMAGE_TILING_OPTIMAL,
1320         .usage = create_info->imageUsage,
1321         .sharingMode = create_info->imageSharingMode,
1322         .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
1323         .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1324     };
1325 
1326     for (uint32_t i = 0; i < num_images; i++) {
1327         Swapchain::Image& img = swapchain->images[i];
1328 
1329         ANativeWindowBuffer* buffer;
1330         err = window->dequeueBuffer(window, &buffer, &img.dequeue_fence);
1331         if (err != android::OK) {
1332             ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
1333             switch (-err) {
1334                 case ENOMEM:
1335                     result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1336                     break;
1337                 default:
1338                     result = VK_ERROR_SURFACE_LOST_KHR;
1339                     break;
1340             }
1341             break;
1342         }
1343         img.buffer = buffer;
1344         img.dequeued = true;
1345 
1346         image_create.extent =
1347             VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1348                        static_cast<uint32_t>(img.buffer->height),
1349                        1};
1350         image_native_buffer.handle = img.buffer->handle;
1351         image_native_buffer.stride = img.buffer->stride;
1352         image_native_buffer.format = img.buffer->format;
1353         image_native_buffer.usage = int(img.buffer->usage);
1354         android_convertGralloc0To1Usage(int(img.buffer->usage),
1355             &image_native_buffer.usage2.producer,
1356             &image_native_buffer.usage2.consumer);
1357 
1358         ATRACE_BEGIN("CreateImage");
1359         result =
1360             dispatch.CreateImage(device, &image_create, nullptr, &img.image);
1361         ATRACE_END();
1362         if (result != VK_SUCCESS) {
1363             ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1364             break;
1365         }
1366     }
1367 
1368     // -- Cancel all buffers, returning them to the queue --
1369     // If an error occurred before, also destroy the VkImage and release the
1370     // buffer reference. Otherwise, we retain a strong reference to the buffer.
1371     for (uint32_t i = 0; i < num_images; i++) {
1372         Swapchain::Image& img = swapchain->images[i];
1373         if (img.dequeued) {
1374             if (!swapchain->shared) {
1375                 window->cancelBuffer(window, img.buffer.get(),
1376                                      img.dequeue_fence);
1377                 img.dequeue_fence = -1;
1378                 img.dequeued = false;
1379             }
1380         }
1381     }
1382 
1383     if (result != VK_SUCCESS) {
1384         DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1385                                  allocator);
1386         return result;
1387     }
1388 
1389     if (transform_hint != swapchain->pre_transform) {
1390         // Log that the app is not doing pre-rotation.
1391         android::GraphicsEnv::getInstance().setTargetStats(
1392             android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1393     }
1394 
1395     surface.swapchain_handle = HandleFromSwapchain(swapchain);
1396     *swapchain_handle = surface.swapchain_handle;
1397     return VK_SUCCESS;
1398 }
1399 
1400 VKAPI_ATTR
DestroySwapchainKHR(VkDevice device,VkSwapchainKHR swapchain_handle,const VkAllocationCallbacks * allocator)1401 void DestroySwapchainKHR(VkDevice device,
1402                          VkSwapchainKHR swapchain_handle,
1403                          const VkAllocationCallbacks* allocator) {
1404     ATRACE_CALL();
1405 
1406     DestroySwapchainInternal(device, swapchain_handle, allocator);
1407 }
1408 
1409 VKAPI_ATTR
GetSwapchainImagesKHR(VkDevice,VkSwapchainKHR swapchain_handle,uint32_t * count,VkImage * images)1410 VkResult GetSwapchainImagesKHR(VkDevice,
1411                                VkSwapchainKHR swapchain_handle,
1412                                uint32_t* count,
1413                                VkImage* images) {
1414     ATRACE_CALL();
1415 
1416     Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1417     ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1418              "getting images for non-active swapchain 0x%" PRIx64
1419              "; only dequeued image handles are valid",
1420              reinterpret_cast<uint64_t>(swapchain_handle));
1421     VkResult result = VK_SUCCESS;
1422     if (images) {
1423         uint32_t n = swapchain.num_images;
1424         if (*count < swapchain.num_images) {
1425             n = *count;
1426             result = VK_INCOMPLETE;
1427         }
1428         for (uint32_t i = 0; i < n; i++)
1429             images[i] = swapchain.images[i].image;
1430         *count = n;
1431     } else {
1432         *count = swapchain.num_images;
1433     }
1434     return result;
1435 }
1436 
1437 VKAPI_ATTR
AcquireNextImageKHR(VkDevice device,VkSwapchainKHR swapchain_handle,uint64_t timeout,VkSemaphore semaphore,VkFence vk_fence,uint32_t * image_index)1438 VkResult AcquireNextImageKHR(VkDevice device,
1439                              VkSwapchainKHR swapchain_handle,
1440                              uint64_t timeout,
1441                              VkSemaphore semaphore,
1442                              VkFence vk_fence,
1443                              uint32_t* image_index) {
1444     ATRACE_CALL();
1445 
1446     Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1447     ANativeWindow* window = swapchain.surface.window.get();
1448     VkResult result;
1449     int err;
1450 
1451     if (swapchain.surface.swapchain_handle != swapchain_handle)
1452         return VK_ERROR_OUT_OF_DATE_KHR;
1453 
1454     if (swapchain.shared) {
1455         // In shared mode, we keep the buffer dequeued all the time, so we don't
1456         // want to dequeue a buffer here. Instead, just ask the driver to ensure
1457         // the semaphore and fence passed to us will be signalled.
1458         *image_index = 0;
1459         result = GetData(device).driver.AcquireImageANDROID(
1460                 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1461         return result;
1462     }
1463 
1464     const nsecs_t acquire_next_image_timeout =
1465         timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1466     if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1467         // Cache the timeout to avoid the duplicate binder cost.
1468         err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1469                               acquire_next_image_timeout);
1470         if (err != android::OK) {
1471             ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1472                   strerror(-err), err);
1473             return VK_ERROR_SURFACE_LOST_KHR;
1474         }
1475         swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1476     }
1477 
1478     ANativeWindowBuffer* buffer;
1479     int fence_fd;
1480     err = window->dequeueBuffer(window, &buffer, &fence_fd);
1481     if (err == android::TIMED_OUT || err == android::INVALID_OPERATION) {
1482         ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1483         return timeout ? VK_TIMEOUT : VK_NOT_READY;
1484     } else if (err != android::OK) {
1485         ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1486         return VK_ERROR_SURFACE_LOST_KHR;
1487     }
1488 
1489     uint32_t idx;
1490     for (idx = 0; idx < swapchain.num_images; idx++) {
1491         if (swapchain.images[idx].buffer.get() == buffer) {
1492             swapchain.images[idx].dequeued = true;
1493             swapchain.images[idx].dequeue_fence = fence_fd;
1494             break;
1495         }
1496     }
1497     if (idx == swapchain.num_images) {
1498         ALOGE("dequeueBuffer returned unrecognized buffer");
1499         window->cancelBuffer(window, buffer, fence_fd);
1500         return VK_ERROR_OUT_OF_DATE_KHR;
1501     }
1502 
1503     int fence_clone = -1;
1504     if (fence_fd != -1) {
1505         fence_clone = dup(fence_fd);
1506         if (fence_clone == -1) {
1507             ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1508                   strerror(errno), errno);
1509             sync_wait(fence_fd, -1 /* forever */);
1510         }
1511     }
1512 
1513     result = GetData(device).driver.AcquireImageANDROID(
1514         device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
1515     if (result != VK_SUCCESS) {
1516         // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1517         // even if the call fails. We could close it ourselves on failure, but
1518         // that would create a race condition if the driver closes it on a
1519         // failure path: some other thread might create an fd with the same
1520         // number between the time the driver closes it and the time we close
1521         // it. We must assume one of: the driver *always* closes it even on
1522         // failure, or *never* closes it on failure.
1523         window->cancelBuffer(window, buffer, fence_fd);
1524         swapchain.images[idx].dequeued = false;
1525         swapchain.images[idx].dequeue_fence = -1;
1526         return result;
1527     }
1528 
1529     *image_index = idx;
1530     return VK_SUCCESS;
1531 }
1532 
1533 VKAPI_ATTR
AcquireNextImage2KHR(VkDevice device,const VkAcquireNextImageInfoKHR * pAcquireInfo,uint32_t * pImageIndex)1534 VkResult AcquireNextImage2KHR(VkDevice device,
1535                               const VkAcquireNextImageInfoKHR* pAcquireInfo,
1536                               uint32_t* pImageIndex) {
1537     ATRACE_CALL();
1538 
1539     return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1540                                pAcquireInfo->timeout, pAcquireInfo->semaphore,
1541                                pAcquireInfo->fence, pImageIndex);
1542 }
1543 
WorstPresentResult(VkResult a,VkResult b)1544 static VkResult WorstPresentResult(VkResult a, VkResult b) {
1545     // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1546     // (in spec version 1.0.14).
1547     static const VkResult kWorstToBest[] = {
1548         VK_ERROR_DEVICE_LOST,
1549         VK_ERROR_SURFACE_LOST_KHR,
1550         VK_ERROR_OUT_OF_DATE_KHR,
1551         VK_ERROR_OUT_OF_DEVICE_MEMORY,
1552         VK_ERROR_OUT_OF_HOST_MEMORY,
1553         VK_SUBOPTIMAL_KHR,
1554     };
1555     for (auto result : kWorstToBest) {
1556         if (a == result || b == result)
1557             return result;
1558     }
1559     ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1560     ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1561     return a != VK_SUCCESS ? a : b;
1562 }
1563 
1564 VKAPI_ATTR
QueuePresentKHR(VkQueue queue,const VkPresentInfoKHR * present_info)1565 VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
1566     ATRACE_CALL();
1567 
1568     ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1569              "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1570              present_info->sType);
1571 
1572     VkDevice device = GetData(queue).driver_device;
1573     const auto& dispatch = GetData(queue).driver;
1574     VkResult final_result = VK_SUCCESS;
1575 
1576     // Look at the pNext chain for supported extension structs:
1577     const VkPresentRegionsKHR* present_regions = nullptr;
1578     const VkPresentTimesInfoGOOGLE* present_times = nullptr;
1579     const VkPresentRegionsKHR* next =
1580         reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1581     while (next) {
1582         switch (next->sType) {
1583             case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1584                 present_regions = next;
1585                 break;
1586             case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
1587                 present_times =
1588                     reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1589                 break;
1590             default:
1591                 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1592                       next->sType);
1593                 break;
1594         }
1595         next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1596     }
1597     ALOGV_IF(
1598         present_regions &&
1599             present_regions->swapchainCount != present_info->swapchainCount,
1600         "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
1601     ALOGV_IF(present_times &&
1602                  present_times->swapchainCount != present_info->swapchainCount,
1603              "VkPresentTimesInfoGOOGLE::swapchainCount != "
1604              "VkPresentInfo::swapchainCount");
1605     const VkPresentRegionKHR* regions =
1606         (present_regions) ? present_regions->pRegions : nullptr;
1607     const VkPresentTimeGOOGLE* times =
1608         (present_times) ? present_times->pTimes : nullptr;
1609     const VkAllocationCallbacks* allocator = &GetData(device).allocator;
1610     android_native_rect_t* rects = nullptr;
1611     uint32_t nrects = 0;
1612 
1613     for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1614         Swapchain& swapchain =
1615             *SwapchainFromHandle(present_info->pSwapchains[sc]);
1616         uint32_t image_idx = present_info->pImageIndices[sc];
1617         Swapchain::Image& img = swapchain.images[image_idx];
1618         const VkPresentRegionKHR* region =
1619             (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
1620         const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
1621         VkResult swapchain_result = VK_SUCCESS;
1622         VkResult result;
1623         int err;
1624 
1625         int fence = -1;
1626         result = dispatch.QueueSignalReleaseImageANDROID(
1627             queue, present_info->waitSemaphoreCount,
1628             present_info->pWaitSemaphores, img.image, &fence);
1629         if (result != VK_SUCCESS) {
1630             ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
1631             swapchain_result = result;
1632         }
1633         if (img.release_fence >= 0)
1634             close(img.release_fence);
1635         img.release_fence = fence < 0 ? -1 : dup(fence);
1636 
1637         if (swapchain.surface.swapchain_handle ==
1638             present_info->pSwapchains[sc]) {
1639             ANativeWindow* window = swapchain.surface.window.get();
1640             if (swapchain_result == VK_SUCCESS) {
1641                 if (region) {
1642                     // Process the incremental-present hint for this swapchain:
1643                     uint32_t rcount = region->rectangleCount;
1644                     if (rcount > nrects) {
1645                         android_native_rect_t* new_rects =
1646                             static_cast<android_native_rect_t*>(
1647                                 allocator->pfnReallocation(
1648                                     allocator->pUserData, rects,
1649                                     sizeof(android_native_rect_t) * rcount,
1650                                     alignof(android_native_rect_t),
1651                                     VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1652                         if (new_rects) {
1653                             rects = new_rects;
1654                             nrects = rcount;
1655                         } else {
1656                             rcount = 0;  // Ignore the hint for this swapchain
1657                         }
1658                     }
1659                     for (uint32_t r = 0; r < rcount; ++r) {
1660                         if (region->pRectangles[r].layer > 0) {
1661                             ALOGV(
1662                                 "vkQueuePresentKHR ignoring invalid layer "
1663                                 "(%u); using layer 0 instead",
1664                                 region->pRectangles[r].layer);
1665                         }
1666                         int x = region->pRectangles[r].offset.x;
1667                         int y = region->pRectangles[r].offset.y;
1668                         int width = static_cast<int>(
1669                             region->pRectangles[r].extent.width);
1670                         int height = static_cast<int>(
1671                             region->pRectangles[r].extent.height);
1672                         android_native_rect_t* cur_rect = &rects[r];
1673                         cur_rect->left = x;
1674                         cur_rect->top = y + height;
1675                         cur_rect->right = x + width;
1676                         cur_rect->bottom = y;
1677                     }
1678                     native_window_set_surface_damage(window, rects, rcount);
1679                 }
1680                 if (time) {
1681                     if (!swapchain.frame_timestamps_enabled) {
1682                         ALOGV(
1683                             "Calling "
1684                             "native_window_enable_frame_timestamps(true)");
1685                         native_window_enable_frame_timestamps(window, true);
1686                         swapchain.frame_timestamps_enabled = true;
1687                     }
1688 
1689                     // Record the nativeFrameId so it can be later correlated to
1690                     // this present.
1691                     uint64_t nativeFrameId = 0;
1692                     err = native_window_get_next_frame_id(
1693                             window, &nativeFrameId);
1694                     if (err != android::OK) {
1695                         ALOGE("Failed to get next native frame ID.");
1696                     }
1697 
1698                     // Add a new timing record with the user's presentID and
1699                     // the nativeFrameId.
1700                     swapchain.timing.emplace_back(time, nativeFrameId);
1701                     while (swapchain.timing.size() > MAX_TIMING_INFOS) {
1702                         swapchain.timing.erase(swapchain.timing.begin());
1703                     }
1704                     if (time->desiredPresentTime) {
1705                         // Set the desiredPresentTime:
1706                         ALOGV(
1707                             "Calling "
1708                             "native_window_set_buffers_timestamp(%" PRId64 ")",
1709                             time->desiredPresentTime);
1710                         native_window_set_buffers_timestamp(
1711                             window,
1712                             static_cast<int64_t>(time->desiredPresentTime));
1713                     }
1714                 }
1715 
1716                 err = window->queueBuffer(window, img.buffer.get(), fence);
1717                 // queueBuffer always closes fence, even on error
1718                 if (err != android::OK) {
1719                     ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1720                     swapchain_result = WorstPresentResult(
1721                         swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1722                 } else {
1723                     if (img.dequeue_fence >= 0) {
1724                         close(img.dequeue_fence);
1725                         img.dequeue_fence = -1;
1726                     }
1727                     img.dequeued = false;
1728                 }
1729 
1730                 // If the swapchain is in shared mode, immediately dequeue the
1731                 // buffer so it can be presented again without an intervening
1732                 // call to AcquireNextImageKHR. We expect to get the same buffer
1733                 // back from every call to dequeueBuffer in this mode.
1734                 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1735                     ANativeWindowBuffer* buffer;
1736                     int fence_fd;
1737                     err = window->dequeueBuffer(window, &buffer, &fence_fd);
1738                     if (err != android::OK) {
1739                         ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1740                         swapchain_result = WorstPresentResult(swapchain_result,
1741                             VK_ERROR_SURFACE_LOST_KHR);
1742                     } else if (img.buffer != buffer) {
1743                         ALOGE("got wrong image back for shared swapchain");
1744                         swapchain_result = WorstPresentResult(swapchain_result,
1745                             VK_ERROR_SURFACE_LOST_KHR);
1746                     } else {
1747                         img.dequeue_fence = fence_fd;
1748                         img.dequeued = true;
1749                     }
1750                 }
1751             }
1752             if (swapchain_result != VK_SUCCESS) {
1753                 OrphanSwapchain(device, &swapchain);
1754             }
1755             int window_transform_hint;
1756             err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1757                                 &window_transform_hint);
1758             if (err != android::OK) {
1759                 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1760                       strerror(-err), err);
1761                 swapchain_result = WorstPresentResult(
1762                     swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1763             }
1764             if (swapchain.pre_transform != window_transform_hint) {
1765                 swapchain_result =
1766                     WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1767             }
1768         } else {
1769             ReleaseSwapchainImage(device, nullptr, fence, img, true);
1770             swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
1771         }
1772 
1773         if (present_info->pResults)
1774             present_info->pResults[sc] = swapchain_result;
1775 
1776         if (swapchain_result != final_result)
1777             final_result = WorstPresentResult(final_result, swapchain_result);
1778     }
1779     if (rects) {
1780         allocator->pfnFree(allocator->pUserData, rects);
1781     }
1782 
1783     return final_result;
1784 }
1785 
1786 VKAPI_ATTR
GetRefreshCycleDurationGOOGLE(VkDevice,VkSwapchainKHR swapchain_handle,VkRefreshCycleDurationGOOGLE * pDisplayTimingProperties)1787 VkResult GetRefreshCycleDurationGOOGLE(
1788     VkDevice,
1789     VkSwapchainKHR swapchain_handle,
1790     VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
1791     ATRACE_CALL();
1792 
1793     Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1794     VkResult result = VK_SUCCESS;
1795 
1796     pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
1797 
1798     return result;
1799 }
1800 
1801 VKAPI_ATTR
GetPastPresentationTimingGOOGLE(VkDevice,VkSwapchainKHR swapchain_handle,uint32_t * count,VkPastPresentationTimingGOOGLE * timings)1802 VkResult GetPastPresentationTimingGOOGLE(
1803     VkDevice,
1804     VkSwapchainKHR swapchain_handle,
1805     uint32_t* count,
1806     VkPastPresentationTimingGOOGLE* timings) {
1807     ATRACE_CALL();
1808 
1809     Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1810     if (swapchain.surface.swapchain_handle != swapchain_handle) {
1811         return VK_ERROR_OUT_OF_DATE_KHR;
1812     }
1813 
1814     ANativeWindow* window = swapchain.surface.window.get();
1815     VkResult result = VK_SUCCESS;
1816 
1817     if (!swapchain.frame_timestamps_enabled) {
1818         ALOGV("Calling native_window_enable_frame_timestamps(true)");
1819         native_window_enable_frame_timestamps(window, true);
1820         swapchain.frame_timestamps_enabled = true;
1821     }
1822 
1823     if (timings) {
1824         // Get the latest ready timing count before copying, since the copied
1825         // timing info will be erased in copy_ready_timings function.
1826         uint32_t n = get_num_ready_timings(swapchain);
1827         copy_ready_timings(swapchain, count, timings);
1828         // Check the *count here against the recorded ready timing count, since
1829         // *count can be overwritten per spec describes.
1830         if (*count < n) {
1831             result = VK_INCOMPLETE;
1832         }
1833     } else {
1834         *count = get_num_ready_timings(swapchain);
1835     }
1836 
1837     return result;
1838 }
1839 
1840 VKAPI_ATTR
GetSwapchainStatusKHR(VkDevice,VkSwapchainKHR swapchain_handle)1841 VkResult GetSwapchainStatusKHR(
1842     VkDevice,
1843     VkSwapchainKHR swapchain_handle) {
1844     ATRACE_CALL();
1845 
1846     Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1847     VkResult result = VK_SUCCESS;
1848 
1849     if (swapchain.surface.swapchain_handle != swapchain_handle) {
1850         return VK_ERROR_OUT_OF_DATE_KHR;
1851     }
1852 
1853     // TODO(b/143296009): Implement this function properly
1854 
1855     return result;
1856 }
1857 
SetHdrMetadataEXT(VkDevice,uint32_t swapchainCount,const VkSwapchainKHR * pSwapchains,const VkHdrMetadataEXT * pHdrMetadataEXTs)1858 VKAPI_ATTR void SetHdrMetadataEXT(
1859     VkDevice,
1860     uint32_t swapchainCount,
1861     const VkSwapchainKHR* pSwapchains,
1862     const VkHdrMetadataEXT* pHdrMetadataEXTs) {
1863     ATRACE_CALL();
1864 
1865     for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1866         Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1867         if (!swapchain)
1868             continue;
1869 
1870         if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1871 
1872         ANativeWindow* window = swapchain->surface.window.get();
1873 
1874         VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1875         const android_smpte2086_metadata smpteMetdata = {
1876             {vulkanMetadata.displayPrimaryRed.x,
1877              vulkanMetadata.displayPrimaryRed.y},
1878             {vulkanMetadata.displayPrimaryGreen.x,
1879              vulkanMetadata.displayPrimaryGreen.y},
1880             {vulkanMetadata.displayPrimaryBlue.x,
1881              vulkanMetadata.displayPrimaryBlue.y},
1882             {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1883             vulkanMetadata.maxLuminance,
1884             vulkanMetadata.minLuminance};
1885         native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1886 
1887         const android_cta861_3_metadata cta8613Metadata = {
1888             vulkanMetadata.maxContentLightLevel,
1889             vulkanMetadata.maxFrameAverageLightLevel};
1890         native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1891     }
1892 
1893     return;
1894 }
1895 
InterceptBindImageMemory2(uint32_t bind_info_count,const VkBindImageMemoryInfo * bind_infos,std::vector<VkNativeBufferANDROID> * out_native_buffers,std::vector<VkBindImageMemoryInfo> * out_bind_infos)1896 static void InterceptBindImageMemory2(
1897     uint32_t bind_info_count,
1898     const VkBindImageMemoryInfo* bind_infos,
1899     std::vector<VkNativeBufferANDROID>* out_native_buffers,
1900     std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1901     out_native_buffers->clear();
1902     out_bind_infos->clear();
1903 
1904     if (!bind_info_count)
1905         return;
1906 
1907     std::unordered_set<uint32_t> intercepted_indexes;
1908 
1909     for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1910         auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1911             bind_infos[idx].pNext);
1912         while (info &&
1913                info->sType !=
1914                    VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1915             info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1916                 info->pNext);
1917         }
1918 
1919         if (!info)
1920             continue;
1921 
1922         ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1923                     "swapchain handle must not be NULL");
1924         const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1925         ALOG_ASSERT(
1926             info->imageIndex < swapchain->num_images,
1927             "imageIndex must be less than the number of images in swapchain");
1928 
1929         ANativeWindowBuffer* buffer =
1930             swapchain->images[info->imageIndex].buffer.get();
1931         VkNativeBufferANDROID native_buffer = {
1932 #pragma clang diagnostic push
1933 #pragma clang diagnostic ignored "-Wold-style-cast"
1934             .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1935 #pragma clang diagnostic pop
1936             .pNext = bind_infos[idx].pNext,
1937             .handle = buffer->handle,
1938             .stride = buffer->stride,
1939             .format = buffer->format,
1940             .usage = int(buffer->usage),
1941         };
1942         // Reserve enough space to avoid letting re-allocation invalidate the
1943         // addresses of the elements inside.
1944         out_native_buffers->reserve(bind_info_count);
1945         out_native_buffers->emplace_back(native_buffer);
1946 
1947         // Reserve the space now since we know how much is needed now.
1948         out_bind_infos->reserve(bind_info_count);
1949         out_bind_infos->emplace_back(bind_infos[idx]);
1950         out_bind_infos->back().pNext = &out_native_buffers->back();
1951 
1952         intercepted_indexes.insert(idx);
1953     }
1954 
1955     if (intercepted_indexes.empty())
1956         return;
1957 
1958     for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1959         if (intercepted_indexes.count(idx))
1960             continue;
1961         out_bind_infos->emplace_back(bind_infos[idx]);
1962     }
1963 }
1964 
1965 VKAPI_ATTR
BindImageMemory2(VkDevice device,uint32_t bindInfoCount,const VkBindImageMemoryInfo * pBindInfos)1966 VkResult BindImageMemory2(VkDevice device,
1967                           uint32_t bindInfoCount,
1968                           const VkBindImageMemoryInfo* pBindInfos) {
1969     ATRACE_CALL();
1970 
1971     // out_native_buffers is for maintaining the lifecycle of the constructed
1972     // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1973     std::vector<VkNativeBufferANDROID> out_native_buffers;
1974     std::vector<VkBindImageMemoryInfo> out_bind_infos;
1975     InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1976                               &out_bind_infos);
1977     return GetData(device).driver.BindImageMemory2(
1978         device, bindInfoCount,
1979         out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
1980 }
1981 
1982 VKAPI_ATTR
BindImageMemory2KHR(VkDevice device,uint32_t bindInfoCount,const VkBindImageMemoryInfo * pBindInfos)1983 VkResult BindImageMemory2KHR(VkDevice device,
1984                              uint32_t bindInfoCount,
1985                              const VkBindImageMemoryInfo* pBindInfos) {
1986     ATRACE_CALL();
1987 
1988     std::vector<VkNativeBufferANDROID> out_native_buffers;
1989     std::vector<VkBindImageMemoryInfo> out_bind_infos;
1990     InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1991                               &out_bind_infos);
1992     return GetData(device).driver.BindImageMemory2KHR(
1993         device, bindInfoCount,
1994         out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
1995 }
1996 
1997 }  // namespace driver
1998 }  // namespace vulkan
1999