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::__anon3017a7eb0111::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::__anon3017a7eb0111::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::__anon3017a7eb0111::Swapchain::Image261 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
262 VkImage image;
263 android::sp<ANativeWindowBuffer> buffer;
264 // The fence is only valid when the buffer is dequeued, and should be
265 // -1 any other time. When valid, we own the fd, and must ensure it is
266 // closed: either by closing it explicitly when queueing the buffer,
267 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
268 int dequeue_fence;
269 bool dequeued;
270 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
271
272 std::vector<TimingInfo> timing;
273 };
274
HandleFromSwapchain(Swapchain * swapchain)275 VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
276 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
277 }
278
SwapchainFromHandle(VkSwapchainKHR handle)279 Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
280 return reinterpret_cast<Swapchain*>(handle);
281 }
282
ReleaseSwapchainImage(VkDevice device,ANativeWindow * window,int release_fence,Swapchain::Image & image)283 void ReleaseSwapchainImage(VkDevice device,
284 ANativeWindow* window,
285 int release_fence,
286 Swapchain::Image& image) {
287 ATRACE_CALL();
288
289 ALOG_ASSERT(release_fence == -1 || image.dequeued,
290 "ReleaseSwapchainImage: can't provide a release fence for "
291 "non-dequeued images");
292
293 if (image.dequeued) {
294 if (release_fence >= 0) {
295 // We get here from vkQueuePresentKHR. The application is
296 // responsible for creating an execution dependency chain from
297 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
298 // (release_fence), so we can drop the dequeue_fence here.
299 if (image.dequeue_fence >= 0)
300 close(image.dequeue_fence);
301 } else {
302 // We get here during swapchain destruction, or various serious
303 // error cases e.g. when we can't create the release_fence during
304 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
305 // have already signalled, since the swapchain images are supposed
306 // to be idle before the swapchain is destroyed. In error cases,
307 // there may be rendering in flight to the image, but since we
308 // weren't able to create a release_fence, waiting for the
309 // dequeue_fence is about the best we can do.
310 release_fence = image.dequeue_fence;
311 }
312 image.dequeue_fence = -1;
313
314 if (window) {
315 window->cancelBuffer(window, image.buffer.get(), release_fence);
316 } else {
317 if (release_fence >= 0) {
318 sync_wait(release_fence, -1 /* forever */);
319 close(release_fence);
320 }
321 }
322
323 image.dequeued = false;
324 }
325
326 if (image.image) {
327 ATRACE_BEGIN("DestroyImage");
328 GetData(device).driver.DestroyImage(device, image.image, nullptr);
329 ATRACE_END();
330 image.image = VK_NULL_HANDLE;
331 }
332
333 image.buffer.clear();
334 }
335
OrphanSwapchain(VkDevice device,Swapchain * swapchain)336 void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
337 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
338 return;
339 for (uint32_t i = 0; i < swapchain->num_images; i++) {
340 if (!swapchain->images[i].dequeued)
341 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
342 }
343 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
344 swapchain->timing.clear();
345 }
346
get_num_ready_timings(Swapchain & swapchain)347 uint32_t get_num_ready_timings(Swapchain& swapchain) {
348 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
349 return 0;
350 }
351
352 uint32_t num_ready = 0;
353 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
354 for (uint32_t i = 0; i < num_timings; i++) {
355 TimingInfo& ti = swapchain.timing[i];
356 if (ti.ready()) {
357 // This TimingInfo is ready to be reported to the user. Add it
358 // to the num_ready.
359 num_ready++;
360 continue;
361 }
362 // This TimingInfo is not yet ready to be reported to the user,
363 // and so we should look for any available timestamps that
364 // might make it ready.
365 int64_t desired_present_time = 0;
366 int64_t render_complete_time = 0;
367 int64_t composition_latch_time = 0;
368 int64_t actual_present_time = 0;
369 // Obtain timestamps:
370 int err = native_window_get_frame_timestamps(
371 swapchain.surface.window.get(), ti.native_frame_id_,
372 &desired_present_time, &render_complete_time,
373 &composition_latch_time,
374 nullptr, //&first_composition_start_time,
375 nullptr, //&last_composition_start_time,
376 nullptr, //&composition_finish_time,
377 &actual_present_time,
378 nullptr, //&dequeue_ready_time,
379 nullptr /*&reads_done_time*/);
380
381 if (err != android::OK) {
382 continue;
383 }
384
385 // Record the timestamp(s) we received, and then see if this TimingInfo
386 // is ready to be reported to the user:
387 ti.timestamp_desired_present_time_ = desired_present_time;
388 ti.timestamp_actual_present_time_ = actual_present_time;
389 ti.timestamp_render_complete_time_ = render_complete_time;
390 ti.timestamp_composition_latch_time_ = composition_latch_time;
391
392 if (ti.ready()) {
393 // The TimingInfo has received enough timestamps, and should now
394 // use those timestamps to calculate the info that should be
395 // reported to the user:
396 ti.calculate(swapchain.refresh_duration);
397 num_ready++;
398 }
399 }
400 return num_ready;
401 }
402
copy_ready_timings(Swapchain & swapchain,uint32_t * count,VkPastPresentationTimingGOOGLE * timings)403 void copy_ready_timings(Swapchain& swapchain,
404 uint32_t* count,
405 VkPastPresentationTimingGOOGLE* timings) {
406 if (swapchain.timing.empty()) {
407 *count = 0;
408 return;
409 }
410
411 size_t last_ready = swapchain.timing.size() - 1;
412 while (!swapchain.timing[last_ready].ready()) {
413 if (last_ready == 0) {
414 *count = 0;
415 return;
416 }
417 last_ready--;
418 }
419
420 uint32_t num_copied = 0;
421 int32_t num_to_remove = 0;
422 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
423 const TimingInfo& ti = swapchain.timing[i];
424 if (ti.ready()) {
425 ti.get_values(&timings[num_copied]);
426 num_copied++;
427 }
428 num_to_remove++;
429 }
430
431 // Discard old frames that aren't ready if newer frames are ready.
432 // We don't expect to get the timing info for those old frames.
433 swapchain.timing.erase(swapchain.timing.begin(),
434 swapchain.timing.begin() + num_to_remove);
435
436 *count = num_copied;
437 }
438
GetNativePixelFormat(VkFormat format)439 android_pixel_format GetNativePixelFormat(VkFormat format) {
440 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
441 switch (format) {
442 case VK_FORMAT_R8G8B8A8_UNORM:
443 case VK_FORMAT_R8G8B8A8_SRGB:
444 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
445 break;
446 case VK_FORMAT_R5G6B5_UNORM_PACK16:
447 native_format = HAL_PIXEL_FORMAT_RGB_565;
448 break;
449 case VK_FORMAT_R16G16B16A16_SFLOAT:
450 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
451 break;
452 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
453 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
454 break;
455 default:
456 ALOGV("unsupported swapchain format %d", format);
457 break;
458 }
459 return native_format;
460 }
461
GetNativeDataspace(VkColorSpaceKHR colorspace)462 android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
463 switch (colorspace) {
464 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
465 return HAL_DATASPACE_V0_SRGB;
466 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
467 return HAL_DATASPACE_DISPLAY_P3;
468 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
469 return HAL_DATASPACE_V0_SCRGB_LINEAR;
470 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
471 return HAL_DATASPACE_V0_SCRGB;
472 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
473 return HAL_DATASPACE_DCI_P3_LINEAR;
474 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
475 return HAL_DATASPACE_DCI_P3;
476 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
477 return HAL_DATASPACE_V0_SRGB_LINEAR;
478 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
479 return HAL_DATASPACE_V0_SRGB;
480 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
481 return HAL_DATASPACE_BT2020_LINEAR;
482 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
483 return static_cast<android_dataspace>(
484 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
485 HAL_DATASPACE_RANGE_FULL);
486 case VK_COLOR_SPACE_DOLBYVISION_EXT:
487 return static_cast<android_dataspace>(
488 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
489 HAL_DATASPACE_RANGE_FULL);
490 case VK_COLOR_SPACE_HDR10_HLG_EXT:
491 return static_cast<android_dataspace>(
492 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
493 HAL_DATASPACE_RANGE_FULL);
494 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
495 return static_cast<android_dataspace>(
496 HAL_DATASPACE_STANDARD_ADOBE_RGB |
497 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
498 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
499 return HAL_DATASPACE_ADOBE_RGB;
500
501 // Pass through is intended to allow app to provide data that is passed
502 // to the display system without modification.
503 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
504 return HAL_DATASPACE_ARBITRARY;
505
506 default:
507 // This indicates that we don't know about the
508 // dataspace specified and we should indicate that
509 // it's unsupported
510 return HAL_DATASPACE_UNKNOWN;
511 }
512 }
513
514 } // anonymous namespace
515
516 VKAPI_ATTR
CreateAndroidSurfaceKHR(VkInstance instance,const VkAndroidSurfaceCreateInfoKHR * pCreateInfo,const VkAllocationCallbacks * allocator,VkSurfaceKHR * out_surface)517 VkResult CreateAndroidSurfaceKHR(
518 VkInstance instance,
519 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
520 const VkAllocationCallbacks* allocator,
521 VkSurfaceKHR* out_surface) {
522 ATRACE_CALL();
523
524 if (!allocator)
525 allocator = &GetData(instance).allocator;
526 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
527 alignof(Surface),
528 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
529 if (!mem)
530 return VK_ERROR_OUT_OF_HOST_MEMORY;
531 Surface* surface = new (mem) Surface;
532
533 surface->window = pCreateInfo->window;
534 surface->swapchain_handle = VK_NULL_HANDLE;
535 int err = native_window_get_consumer_usage(surface->window.get(),
536 &surface->consumer_usage);
537 if (err != android::OK) {
538 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
539 strerror(-err), err);
540 surface->~Surface();
541 allocator->pfnFree(allocator->pUserData, surface);
542 return VK_ERROR_SURFACE_LOST_KHR;
543 }
544
545 err =
546 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
547 if (err != android::OK) {
548 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
549 err);
550 surface->~Surface();
551 allocator->pfnFree(allocator->pUserData, surface);
552 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
553 }
554
555 *out_surface = HandleFromSurface(surface);
556 return VK_SUCCESS;
557 }
558
559 VKAPI_ATTR
DestroySurfaceKHR(VkInstance instance,VkSurfaceKHR surface_handle,const VkAllocationCallbacks * allocator)560 void DestroySurfaceKHR(VkInstance instance,
561 VkSurfaceKHR surface_handle,
562 const VkAllocationCallbacks* allocator) {
563 ATRACE_CALL();
564
565 Surface* surface = SurfaceFromHandle(surface_handle);
566 if (!surface)
567 return;
568 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
569 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
570 "destroyed VkSurfaceKHR 0x%" PRIx64
571 " has active VkSwapchainKHR 0x%" PRIx64,
572 reinterpret_cast<uint64_t>(surface_handle),
573 reinterpret_cast<uint64_t>(surface->swapchain_handle));
574 surface->~Surface();
575 if (!allocator)
576 allocator = &GetData(instance).allocator;
577 allocator->pfnFree(allocator->pUserData, surface);
578 }
579
580 VKAPI_ATTR
GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice,uint32_t,VkSurfaceKHR surface_handle,VkBool32 * supported)581 VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
582 uint32_t /*queue_family*/,
583 VkSurfaceKHR surface_handle,
584 VkBool32* supported) {
585 ATRACE_CALL();
586
587 const Surface* surface = SurfaceFromHandle(surface_handle);
588 if (!surface) {
589 return VK_ERROR_SURFACE_LOST_KHR;
590 }
591 const ANativeWindow* window = surface->window.get();
592
593 int query_value;
594 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
595 if (err != android::OK || query_value < 0) {
596 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
597 strerror(-err), err, query_value);
598 return VK_ERROR_SURFACE_LOST_KHR;
599 }
600
601 android_pixel_format native_format =
602 static_cast<android_pixel_format>(query_value);
603
604 bool format_supported = false;
605 switch (native_format) {
606 case HAL_PIXEL_FORMAT_RGBA_8888:
607 case HAL_PIXEL_FORMAT_RGB_565:
608 case HAL_PIXEL_FORMAT_RGBA_FP16:
609 case HAL_PIXEL_FORMAT_RGBA_1010102:
610 format_supported = true;
611 break;
612 default:
613 break;
614 }
615
616 *supported = static_cast<VkBool32>(
617 format_supported || (surface->consumer_usage &
618 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
619 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
620
621 return VK_SUCCESS;
622 }
623
624 VKAPI_ATTR
GetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice,VkSurfaceKHR surface,VkSurfaceCapabilitiesKHR * capabilities)625 VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
626 VkPhysicalDevice /*pdev*/,
627 VkSurfaceKHR surface,
628 VkSurfaceCapabilitiesKHR* capabilities) {
629 ATRACE_CALL();
630
631 int err;
632 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
633
634 int width, height;
635 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
636 if (err != android::OK) {
637 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
638 strerror(-err), err);
639 return VK_ERROR_SURFACE_LOST_KHR;
640 }
641 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
642 if (err != android::OK) {
643 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
644 strerror(-err), err);
645 return VK_ERROR_SURFACE_LOST_KHR;
646 }
647
648 int transform_hint;
649 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
650 if (err != android::OK) {
651 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
652 strerror(-err), err);
653 return VK_ERROR_SURFACE_LOST_KHR;
654 }
655
656 int max_buffer_count;
657 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
658 if (err != android::OK) {
659 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
660 strerror(-err), err);
661 return VK_ERROR_SURFACE_LOST_KHR;
662 }
663 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
664 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
665
666 capabilities->currentExtent =
667 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
668
669 // TODO(http://b/134182502): Figure out what the max extent should be.
670 capabilities->minImageExtent = VkExtent2D{1, 1};
671 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
672
673 capabilities->maxImageArrayLayers = 1;
674
675 capabilities->supportedTransforms = kSupportedTransforms;
676 capabilities->currentTransform =
677 TranslateNativeToVulkanTransform(transform_hint);
678
679 // On Android, window composition is a WindowManager property, not something
680 // associated with the bufferqueue. It can't be changed from here.
681 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
682
683 capabilities->supportedUsageFlags =
684 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
685 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
686 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
687 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
688
689 return VK_SUCCESS;
690 }
691
692 VKAPI_ATTR
GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,VkSurfaceKHR surface_handle,uint32_t * count,VkSurfaceFormatKHR * formats)693 VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
694 VkSurfaceKHR surface_handle,
695 uint32_t* count,
696 VkSurfaceFormatKHR* formats) {
697 ATRACE_CALL();
698
699 const InstanceData& instance_data = GetData(pdev);
700
701 bool wide_color_support = false;
702 Surface& surface = *SurfaceFromHandle(surface_handle);
703 int err = native_window_get_wide_color_support(surface.window.get(),
704 &wide_color_support);
705 if (err) {
706 return VK_ERROR_SURFACE_LOST_KHR;
707 }
708 ALOGV("wide_color_support is: %d", wide_color_support);
709 wide_color_support =
710 wide_color_support &&
711 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
712
713 AHardwareBuffer_Desc desc = {};
714 desc.width = 1;
715 desc.height = 1;
716 desc.layers = 1;
717 desc.usage = surface.consumer_usage |
718 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
719 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
720
721 // We must support R8G8B8A8
722 std::vector<VkSurfaceFormatKHR> all_formats = {
723 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
724 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}};
725
726 if (wide_color_support) {
727 all_formats.emplace_back(VkSurfaceFormatKHR{
728 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
729 all_formats.emplace_back(VkSurfaceFormatKHR{
730 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
731 }
732
733 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
734 if (AHardwareBuffer_isSupported(&desc)) {
735 all_formats.emplace_back(VkSurfaceFormatKHR{
736 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
737 }
738
739 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
740 if (AHardwareBuffer_isSupported(&desc)) {
741 all_formats.emplace_back(VkSurfaceFormatKHR{
742 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
743 if (wide_color_support) {
744 all_formats.emplace_back(
745 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
746 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
747 all_formats.emplace_back(
748 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
749 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
750 }
751 }
752
753 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
754 if (AHardwareBuffer_isSupported(&desc)) {
755 all_formats.emplace_back(
756 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
757 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
758 if (wide_color_support) {
759 all_formats.emplace_back(
760 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
761 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
762 }
763 }
764
765 VkResult result = VK_SUCCESS;
766 if (formats) {
767 uint32_t transfer_count = all_formats.size();
768 if (transfer_count > *count) {
769 transfer_count = *count;
770 result = VK_INCOMPLETE;
771 }
772 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
773 formats);
774 *count = transfer_count;
775 } else {
776 *count = all_formats.size();
777 }
778
779 return result;
780 }
781
782 VKAPI_ATTR
GetPhysicalDeviceSurfaceCapabilities2KHR(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSurfaceInfo2KHR * pSurfaceInfo,VkSurfaceCapabilities2KHR * pSurfaceCapabilities)783 VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
784 VkPhysicalDevice physicalDevice,
785 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
786 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
787 ATRACE_CALL();
788
789 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
790 physicalDevice, pSurfaceInfo->surface,
791 &pSurfaceCapabilities->surfaceCapabilities);
792
793 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
794 while (caps->pNext) {
795 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
796
797 switch (caps->sType) {
798 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
799 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
800 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
801 caps);
802 // Claim same set of usage flags are supported for
803 // shared present modes as for other modes.
804 shared_caps->sharedPresentSupportedUsageFlags =
805 pSurfaceCapabilities->surfaceCapabilities
806 .supportedUsageFlags;
807 } break;
808
809 default:
810 // Ignore all other extension structs
811 break;
812 }
813 }
814
815 return result;
816 }
817
818 VKAPI_ATTR
GetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSurfaceInfo2KHR * pSurfaceInfo,uint32_t * pSurfaceFormatCount,VkSurfaceFormat2KHR * pSurfaceFormats)819 VkResult GetPhysicalDeviceSurfaceFormats2KHR(
820 VkPhysicalDevice physicalDevice,
821 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
822 uint32_t* pSurfaceFormatCount,
823 VkSurfaceFormat2KHR* pSurfaceFormats) {
824 ATRACE_CALL();
825
826 if (!pSurfaceFormats) {
827 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
828 pSurfaceInfo->surface,
829 pSurfaceFormatCount, nullptr);
830 } else {
831 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
832 // after the call.
833 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
834 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
835 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
836 surface_formats.data());
837
838 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
839 // marshal results individually due to stride difference.
840 // completely ignore any chained extension structs.
841 uint32_t formats_to_marshal = *pSurfaceFormatCount;
842 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
843 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
844 }
845 }
846
847 return result;
848 }
849 }
850
851 VKAPI_ATTR
GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,VkSurfaceKHR surface,uint32_t * count,VkPresentModeKHR * modes)852 VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
853 VkSurfaceKHR surface,
854 uint32_t* count,
855 VkPresentModeKHR* modes) {
856 ATRACE_CALL();
857
858 int err;
859 int query_value;
860 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
861
862 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
863 if (err != android::OK || query_value < 0) {
864 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
865 strerror(-err), err, query_value);
866 return VK_ERROR_SURFACE_LOST_KHR;
867 }
868 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
869
870 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
871 if (err != android::OK || query_value < 0) {
872 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
873 strerror(-err), err, query_value);
874 return VK_ERROR_SURFACE_LOST_KHR;
875 }
876 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
877
878 std::vector<VkPresentModeKHR> present_modes;
879 if (min_undequeued_buffers + 1 < max_buffer_count)
880 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
881 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
882
883 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
884 if (QueryPresentationProperties(pdev, &present_properties)) {
885 if (present_properties.sharedImage) {
886 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
887 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
888 }
889 }
890
891 uint32_t num_modes = uint32_t(present_modes.size());
892
893 VkResult result = VK_SUCCESS;
894 if (modes) {
895 if (*count < num_modes)
896 result = VK_INCOMPLETE;
897 *count = std::min(*count, num_modes);
898 std::copy_n(present_modes.data(), *count, modes);
899 } else {
900 *count = num_modes;
901 }
902 return result;
903 }
904
905 VKAPI_ATTR
GetDeviceGroupPresentCapabilitiesKHR(VkDevice,VkDeviceGroupPresentCapabilitiesKHR * pDeviceGroupPresentCapabilities)906 VkResult GetDeviceGroupPresentCapabilitiesKHR(
907 VkDevice,
908 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
909 ATRACE_CALL();
910
911 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
912 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
913 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
914 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
915 pDeviceGroupPresentCapabilities->sType);
916
917 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
918 sizeof(pDeviceGroupPresentCapabilities->presentMask));
919
920 // assume device group of size 1
921 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
922 pDeviceGroupPresentCapabilities->modes =
923 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
924
925 return VK_SUCCESS;
926 }
927
928 VKAPI_ATTR
GetDeviceGroupSurfacePresentModesKHR(VkDevice,VkSurfaceKHR,VkDeviceGroupPresentModeFlagsKHR * pModes)929 VkResult GetDeviceGroupSurfacePresentModesKHR(
930 VkDevice,
931 VkSurfaceKHR,
932 VkDeviceGroupPresentModeFlagsKHR* pModes) {
933 ATRACE_CALL();
934
935 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
936 return VK_SUCCESS;
937 }
938
939 VKAPI_ATTR
GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,VkSurfaceKHR surface,uint32_t * pRectCount,VkRect2D * pRects)940 VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
941 VkSurfaceKHR surface,
942 uint32_t* pRectCount,
943 VkRect2D* pRects) {
944 ATRACE_CALL();
945
946 if (!pRects) {
947 *pRectCount = 1;
948 } else {
949 uint32_t count = std::min(*pRectCount, 1u);
950 bool incomplete = *pRectCount < 1;
951
952 *pRectCount = count;
953
954 if (incomplete) {
955 return VK_INCOMPLETE;
956 }
957
958 int err;
959 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
960
961 int width = 0, height = 0;
962 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
963 if (err != android::OK) {
964 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
965 strerror(-err), err);
966 }
967 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
968 if (err != android::OK) {
969 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
970 strerror(-err), err);
971 }
972
973 // TODO(b/143294545): Return something better than "whole window"
974 pRects[0].offset.x = 0;
975 pRects[0].offset.y = 0;
976 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
977 static_cast<uint32_t>(height)};
978 }
979 return VK_SUCCESS;
980 }
981
DestroySwapchainInternal(VkDevice device,VkSwapchainKHR swapchain_handle,const VkAllocationCallbacks * allocator)982 static void DestroySwapchainInternal(VkDevice device,
983 VkSwapchainKHR swapchain_handle,
984 const VkAllocationCallbacks* allocator) {
985 ATRACE_CALL();
986
987 const auto& dispatch = GetData(device).driver;
988 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
989 if (!swapchain) {
990 return;
991 }
992
993 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
994 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
995
996 if (window && swapchain->frame_timestamps_enabled) {
997 native_window_enable_frame_timestamps(window, false);
998 }
999
1000 for (uint32_t i = 0; i < swapchain->num_images; i++) {
1001 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
1002 }
1003
1004 if (active) {
1005 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1006 }
1007
1008 if (!allocator) {
1009 allocator = &GetData(device).allocator;
1010 }
1011
1012 swapchain->~Swapchain();
1013 allocator->pfnFree(allocator->pUserData, swapchain);
1014 }
1015
1016 VKAPI_ATTR
CreateSwapchainKHR(VkDevice device,const VkSwapchainCreateInfoKHR * create_info,const VkAllocationCallbacks * allocator,VkSwapchainKHR * swapchain_handle)1017 VkResult CreateSwapchainKHR(VkDevice device,
1018 const VkSwapchainCreateInfoKHR* create_info,
1019 const VkAllocationCallbacks* allocator,
1020 VkSwapchainKHR* swapchain_handle) {
1021 ATRACE_CALL();
1022
1023 int err;
1024 VkResult result = VK_SUCCESS;
1025
1026 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1027 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1028 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1029 " oldSwapchain=0x%" PRIx64,
1030 reinterpret_cast<uint64_t>(create_info->surface),
1031 create_info->minImageCount, create_info->imageFormat,
1032 create_info->imageColorSpace, create_info->imageExtent.width,
1033 create_info->imageExtent.height, create_info->imageUsage,
1034 create_info->preTransform, create_info->presentMode,
1035 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1036
1037 if (!allocator)
1038 allocator = &GetData(device).allocator;
1039
1040 android_pixel_format native_pixel_format =
1041 GetNativePixelFormat(create_info->imageFormat);
1042 android_dataspace native_dataspace =
1043 GetNativeDataspace(create_info->imageColorSpace);
1044 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1045 ALOGE(
1046 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1047 "failed: Unsupported color space",
1048 create_info->imageColorSpace);
1049 return VK_ERROR_INITIALIZATION_FAILED;
1050 }
1051
1052 ALOGV_IF(create_info->imageArrayLayers != 1,
1053 "swapchain imageArrayLayers=%u not supported",
1054 create_info->imageArrayLayers);
1055 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
1056 "swapchain preTransform=%#x not supported",
1057 create_info->preTransform);
1058 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
1059 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
1060 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1061 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
1062 "swapchain presentMode=%u not supported",
1063 create_info->presentMode);
1064
1065 Surface& surface = *SurfaceFromHandle(create_info->surface);
1066
1067 if (surface.swapchain_handle != create_info->oldSwapchain) {
1068 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
1069 " because it already has active swapchain 0x%" PRIx64
1070 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1071 reinterpret_cast<uint64_t>(create_info->surface),
1072 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1073 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1074 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1075 }
1076 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1077 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1078
1079 // -- Reset the native window --
1080 // The native window might have been used previously, and had its properties
1081 // changed from defaults. That will affect the answer we get for queries
1082 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1083 // attempt such queries.
1084
1085 // The native window only allows dequeueing all buffers before any have
1086 // been queued, since after that point at least one is assumed to be in
1087 // non-FREE state at any given time. Disconnecting and re-connecting
1088 // orphans the previous buffers, getting us back to the state where we can
1089 // dequeue all buffers.
1090 //
1091 // TODO(http://b/134186185) recycle swapchain images more efficiently
1092 ANativeWindow* window = surface.window.get();
1093 err = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
1094 ALOGW_IF(err != android::OK, "native_window_api_disconnect failed: %s (%d)",
1095 strerror(-err), err);
1096 err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
1097 ALOGW_IF(err != android::OK, "native_window_api_connect failed: %s (%d)",
1098 strerror(-err), err);
1099
1100 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, -1);
1101 if (err != android::OK) {
1102 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1103 strerror(-err), err);
1104 return VK_ERROR_SURFACE_LOST_KHR;
1105 }
1106
1107 err = native_window_set_buffer_count(window, 0);
1108 if (err != android::OK) {
1109 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1110 strerror(-err), err);
1111 return VK_ERROR_SURFACE_LOST_KHR;
1112 }
1113
1114 int swap_interval =
1115 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1116 err = window->setSwapInterval(window, swap_interval);
1117 if (err != android::OK) {
1118 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1119 strerror(-err), err);
1120 return VK_ERROR_SURFACE_LOST_KHR;
1121 }
1122
1123 err = native_window_set_shared_buffer_mode(window, false);
1124 if (err != android::OK) {
1125 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1126 strerror(-err), err);
1127 return VK_ERROR_SURFACE_LOST_KHR;
1128 }
1129
1130 err = native_window_set_auto_refresh(window, false);
1131 if (err != android::OK) {
1132 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1133 strerror(-err), err);
1134 return VK_ERROR_SURFACE_LOST_KHR;
1135 }
1136
1137 // -- Configure the native window --
1138
1139 const auto& dispatch = GetData(device).driver;
1140
1141 err = native_window_set_buffers_format(window, native_pixel_format);
1142 if (err != android::OK) {
1143 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
1144 native_pixel_format, strerror(-err), err);
1145 return VK_ERROR_SURFACE_LOST_KHR;
1146 }
1147 err = native_window_set_buffers_data_space(window, native_dataspace);
1148 if (err != android::OK) {
1149 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
1150 native_dataspace, strerror(-err), err);
1151 return VK_ERROR_SURFACE_LOST_KHR;
1152 }
1153
1154 err = native_window_set_buffers_dimensions(
1155 window, static_cast<int>(create_info->imageExtent.width),
1156 static_cast<int>(create_info->imageExtent.height));
1157 if (err != android::OK) {
1158 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1159 create_info->imageExtent.width, create_info->imageExtent.height,
1160 strerror(-err), err);
1161 return VK_ERROR_SURFACE_LOST_KHR;
1162 }
1163
1164 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1165 // applied during rendering. native_window_set_transform() expects the
1166 // inverse: the transform the app is requesting that the compositor perform
1167 // during composition. With native windows, pre-transform works by rendering
1168 // with the same transform the compositor is applying (as in Vulkan), but
1169 // then requesting the inverse transform, so that when the compositor does
1170 // it's job the two transforms cancel each other out and the compositor ends
1171 // up applying an identity transform to the app's buffer.
1172 err = native_window_set_buffers_transform(
1173 window, InvertTransformToNative(create_info->preTransform));
1174 if (err != android::OK) {
1175 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1176 InvertTransformToNative(create_info->preTransform),
1177 strerror(-err), err);
1178 return VK_ERROR_SURFACE_LOST_KHR;
1179 }
1180
1181 err = native_window_set_scaling_mode(
1182 window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
1183 if (err != android::OK) {
1184 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1185 strerror(-err), err);
1186 return VK_ERROR_SURFACE_LOST_KHR;
1187 }
1188
1189 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1190 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1191 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1192 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1193 err = native_window_set_shared_buffer_mode(window, true);
1194 if (err != android::OK) {
1195 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1196 return VK_ERROR_SURFACE_LOST_KHR;
1197 }
1198 }
1199
1200 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1201 err = native_window_set_auto_refresh(window, true);
1202 if (err != android::OK) {
1203 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1204 return VK_ERROR_SURFACE_LOST_KHR;
1205 }
1206 }
1207
1208 int query_value;
1209 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1210 &query_value);
1211 if (err != android::OK || query_value < 0) {
1212 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1213 query_value);
1214 return VK_ERROR_SURFACE_LOST_KHR;
1215 }
1216 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
1217 uint32_t num_images =
1218 (swap_interval ? create_info->minImageCount
1219 : std::max(3u, create_info->minImageCount)) -
1220 1 + min_undequeued_buffers;
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) ? ®ions[sc] : nullptr;
1620 const VkPresentTimeGOOGLE* time = (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
1634 if (swapchain.surface.swapchain_handle ==
1635 present_info->pSwapchains[sc]) {
1636 ANativeWindow* window = swapchain.surface.window.get();
1637 if (swapchain_result == VK_SUCCESS) {
1638 if (region) {
1639 // Process the incremental-present hint for this swapchain:
1640 uint32_t rcount = region->rectangleCount;
1641 if (rcount > nrects) {
1642 android_native_rect_t* new_rects =
1643 static_cast<android_native_rect_t*>(
1644 allocator->pfnReallocation(
1645 allocator->pUserData, rects,
1646 sizeof(android_native_rect_t) * rcount,
1647 alignof(android_native_rect_t),
1648 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1649 if (new_rects) {
1650 rects = new_rects;
1651 nrects = rcount;
1652 } else {
1653 rcount = 0; // Ignore the hint for this swapchain
1654 }
1655 }
1656 for (uint32_t r = 0; r < rcount; ++r) {
1657 if (region->pRectangles[r].layer > 0) {
1658 ALOGV(
1659 "vkQueuePresentKHR ignoring invalid layer "
1660 "(%u); using layer 0 instead",
1661 region->pRectangles[r].layer);
1662 }
1663 int x = region->pRectangles[r].offset.x;
1664 int y = region->pRectangles[r].offset.y;
1665 int width = static_cast<int>(
1666 region->pRectangles[r].extent.width);
1667 int height = static_cast<int>(
1668 region->pRectangles[r].extent.height);
1669 android_native_rect_t* cur_rect = &rects[r];
1670 cur_rect->left = x;
1671 cur_rect->top = y + height;
1672 cur_rect->right = x + width;
1673 cur_rect->bottom = y;
1674 }
1675 native_window_set_surface_damage(window, rects, rcount);
1676 }
1677 if (time) {
1678 if (!swapchain.frame_timestamps_enabled) {
1679 ALOGV(
1680 "Calling "
1681 "native_window_enable_frame_timestamps(true)");
1682 native_window_enable_frame_timestamps(window, true);
1683 swapchain.frame_timestamps_enabled = true;
1684 }
1685
1686 // Record the nativeFrameId so it can be later correlated to
1687 // this present.
1688 uint64_t nativeFrameId = 0;
1689 err = native_window_get_next_frame_id(
1690 window, &nativeFrameId);
1691 if (err != android::OK) {
1692 ALOGE("Failed to get next native frame ID.");
1693 }
1694
1695 // Add a new timing record with the user's presentID and
1696 // the nativeFrameId.
1697 swapchain.timing.emplace_back(time, nativeFrameId);
1698 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
1699 swapchain.timing.erase(swapchain.timing.begin());
1700 }
1701 if (time->desiredPresentTime) {
1702 // Set the desiredPresentTime:
1703 ALOGV(
1704 "Calling "
1705 "native_window_set_buffers_timestamp(%" PRId64 ")",
1706 time->desiredPresentTime);
1707 native_window_set_buffers_timestamp(
1708 window,
1709 static_cast<int64_t>(time->desiredPresentTime));
1710 }
1711 }
1712
1713 err = window->queueBuffer(window, img.buffer.get(), fence);
1714 // queueBuffer always closes fence, even on error
1715 if (err != android::OK) {
1716 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1717 swapchain_result = WorstPresentResult(
1718 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1719 } else {
1720 if (img.dequeue_fence >= 0) {
1721 close(img.dequeue_fence);
1722 img.dequeue_fence = -1;
1723 }
1724 img.dequeued = false;
1725 }
1726
1727 // If the swapchain is in shared mode, immediately dequeue the
1728 // buffer so it can be presented again without an intervening
1729 // call to AcquireNextImageKHR. We expect to get the same buffer
1730 // back from every call to dequeueBuffer in this mode.
1731 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1732 ANativeWindowBuffer* buffer;
1733 int fence_fd;
1734 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1735 if (err != android::OK) {
1736 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1737 swapchain_result = WorstPresentResult(swapchain_result,
1738 VK_ERROR_SURFACE_LOST_KHR);
1739 } else if (img.buffer != buffer) {
1740 ALOGE("got wrong image back for shared swapchain");
1741 swapchain_result = WorstPresentResult(swapchain_result,
1742 VK_ERROR_SURFACE_LOST_KHR);
1743 } else {
1744 img.dequeue_fence = fence_fd;
1745 img.dequeued = true;
1746 }
1747 }
1748 }
1749 if (swapchain_result != VK_SUCCESS) {
1750 OrphanSwapchain(device, &swapchain);
1751 }
1752 int window_transform_hint;
1753 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1754 &window_transform_hint);
1755 if (err != android::OK) {
1756 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1757 strerror(-err), err);
1758 swapchain_result = WorstPresentResult(
1759 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1760 }
1761 if (swapchain.pre_transform != window_transform_hint) {
1762 swapchain_result =
1763 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1764 }
1765 } else {
1766 ReleaseSwapchainImage(device, nullptr, fence, img);
1767 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
1768 }
1769
1770 if (present_info->pResults)
1771 present_info->pResults[sc] = swapchain_result;
1772
1773 if (swapchain_result != final_result)
1774 final_result = WorstPresentResult(final_result, swapchain_result);
1775 }
1776 if (rects) {
1777 allocator->pfnFree(allocator->pUserData, rects);
1778 }
1779
1780 return final_result;
1781 }
1782
1783 VKAPI_ATTR
GetRefreshCycleDurationGOOGLE(VkDevice,VkSwapchainKHR swapchain_handle,VkRefreshCycleDurationGOOGLE * pDisplayTimingProperties)1784 VkResult GetRefreshCycleDurationGOOGLE(
1785 VkDevice,
1786 VkSwapchainKHR swapchain_handle,
1787 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
1788 ATRACE_CALL();
1789
1790 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1791 VkResult result = VK_SUCCESS;
1792
1793 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
1794
1795 return result;
1796 }
1797
1798 VKAPI_ATTR
GetPastPresentationTimingGOOGLE(VkDevice,VkSwapchainKHR swapchain_handle,uint32_t * count,VkPastPresentationTimingGOOGLE * timings)1799 VkResult GetPastPresentationTimingGOOGLE(
1800 VkDevice,
1801 VkSwapchainKHR swapchain_handle,
1802 uint32_t* count,
1803 VkPastPresentationTimingGOOGLE* timings) {
1804 ATRACE_CALL();
1805
1806 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1807 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1808 return VK_ERROR_OUT_OF_DATE_KHR;
1809 }
1810
1811 ANativeWindow* window = swapchain.surface.window.get();
1812 VkResult result = VK_SUCCESS;
1813
1814 if (!swapchain.frame_timestamps_enabled) {
1815 ALOGV("Calling native_window_enable_frame_timestamps(true)");
1816 native_window_enable_frame_timestamps(window, true);
1817 swapchain.frame_timestamps_enabled = true;
1818 }
1819
1820 if (timings) {
1821 // Get the latest ready timing count before copying, since the copied
1822 // timing info will be erased in copy_ready_timings function.
1823 uint32_t n = get_num_ready_timings(swapchain);
1824 copy_ready_timings(swapchain, count, timings);
1825 // Check the *count here against the recorded ready timing count, since
1826 // *count can be overwritten per spec describes.
1827 if (*count < n) {
1828 result = VK_INCOMPLETE;
1829 }
1830 } else {
1831 *count = get_num_ready_timings(swapchain);
1832 }
1833
1834 return result;
1835 }
1836
1837 VKAPI_ATTR
GetSwapchainStatusKHR(VkDevice,VkSwapchainKHR swapchain_handle)1838 VkResult GetSwapchainStatusKHR(
1839 VkDevice,
1840 VkSwapchainKHR swapchain_handle) {
1841 ATRACE_CALL();
1842
1843 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1844 VkResult result = VK_SUCCESS;
1845
1846 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1847 return VK_ERROR_OUT_OF_DATE_KHR;
1848 }
1849
1850 // TODO(b/143296009): Implement this function properly
1851
1852 return result;
1853 }
1854
SetHdrMetadataEXT(VkDevice,uint32_t swapchainCount,const VkSwapchainKHR * pSwapchains,const VkHdrMetadataEXT * pHdrMetadataEXTs)1855 VKAPI_ATTR void SetHdrMetadataEXT(
1856 VkDevice,
1857 uint32_t swapchainCount,
1858 const VkSwapchainKHR* pSwapchains,
1859 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
1860 ATRACE_CALL();
1861
1862 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1863 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1864 if (!swapchain)
1865 continue;
1866
1867 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1868
1869 ANativeWindow* window = swapchain->surface.window.get();
1870
1871 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1872 const android_smpte2086_metadata smpteMetdata = {
1873 {vulkanMetadata.displayPrimaryRed.x,
1874 vulkanMetadata.displayPrimaryRed.y},
1875 {vulkanMetadata.displayPrimaryGreen.x,
1876 vulkanMetadata.displayPrimaryGreen.y},
1877 {vulkanMetadata.displayPrimaryBlue.x,
1878 vulkanMetadata.displayPrimaryBlue.y},
1879 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1880 vulkanMetadata.maxLuminance,
1881 vulkanMetadata.minLuminance};
1882 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1883
1884 const android_cta861_3_metadata cta8613Metadata = {
1885 vulkanMetadata.maxContentLightLevel,
1886 vulkanMetadata.maxFrameAverageLightLevel};
1887 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1888 }
1889
1890 return;
1891 }
1892
InterceptBindImageMemory2(uint32_t bind_info_count,const VkBindImageMemoryInfo * bind_infos,std::vector<VkNativeBufferANDROID> * out_native_buffers,std::vector<VkBindImageMemoryInfo> * out_bind_infos)1893 static void InterceptBindImageMemory2(
1894 uint32_t bind_info_count,
1895 const VkBindImageMemoryInfo* bind_infos,
1896 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1897 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1898 out_native_buffers->clear();
1899 out_bind_infos->clear();
1900
1901 if (!bind_info_count)
1902 return;
1903
1904 std::unordered_set<uint32_t> intercepted_indexes;
1905
1906 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1907 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1908 bind_infos[idx].pNext);
1909 while (info &&
1910 info->sType !=
1911 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1912 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1913 info->pNext);
1914 }
1915
1916 if (!info)
1917 continue;
1918
1919 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1920 "swapchain handle must not be NULL");
1921 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1922 ALOG_ASSERT(
1923 info->imageIndex < swapchain->num_images,
1924 "imageIndex must be less than the number of images in swapchain");
1925
1926 ANativeWindowBuffer* buffer =
1927 swapchain->images[info->imageIndex].buffer.get();
1928 VkNativeBufferANDROID native_buffer = {
1929 #pragma clang diagnostic push
1930 #pragma clang diagnostic ignored "-Wold-style-cast"
1931 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1932 #pragma clang diagnostic pop
1933 .pNext = bind_infos[idx].pNext,
1934 .handle = buffer->handle,
1935 .stride = buffer->stride,
1936 .format = buffer->format,
1937 .usage = int(buffer->usage),
1938 };
1939 // Reserve enough space to avoid letting re-allocation invalidate the
1940 // addresses of the elements inside.
1941 out_native_buffers->reserve(bind_info_count);
1942 out_native_buffers->emplace_back(native_buffer);
1943
1944 // Reserve the space now since we know how much is needed now.
1945 out_bind_infos->reserve(bind_info_count);
1946 out_bind_infos->emplace_back(bind_infos[idx]);
1947 out_bind_infos->back().pNext = &out_native_buffers->back();
1948
1949 intercepted_indexes.insert(idx);
1950 }
1951
1952 if (intercepted_indexes.empty())
1953 return;
1954
1955 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1956 if (intercepted_indexes.count(idx))
1957 continue;
1958 out_bind_infos->emplace_back(bind_infos[idx]);
1959 }
1960 }
1961
1962 VKAPI_ATTR
BindImageMemory2(VkDevice device,uint32_t bindInfoCount,const VkBindImageMemoryInfo * pBindInfos)1963 VkResult BindImageMemory2(VkDevice device,
1964 uint32_t bindInfoCount,
1965 const VkBindImageMemoryInfo* pBindInfos) {
1966 ATRACE_CALL();
1967
1968 // out_native_buffers is for maintaining the lifecycle of the constructed
1969 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1970 std::vector<VkNativeBufferANDROID> out_native_buffers;
1971 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1972 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1973 &out_bind_infos);
1974 return GetData(device).driver.BindImageMemory2(
1975 device, bindInfoCount,
1976 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
1977 }
1978
1979 VKAPI_ATTR
BindImageMemory2KHR(VkDevice device,uint32_t bindInfoCount,const VkBindImageMemoryInfo * pBindInfos)1980 VkResult BindImageMemory2KHR(VkDevice device,
1981 uint32_t bindInfoCount,
1982 const VkBindImageMemoryInfo* pBindInfos) {
1983 ATRACE_CALL();
1984
1985 std::vector<VkNativeBufferANDROID> out_native_buffers;
1986 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1987 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1988 &out_bind_infos);
1989 return GetData(device).driver.BindImageMemory2KHR(
1990 device, bindInfoCount,
1991 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
1992 }
1993
1994 } // namespace driver
1995 } // namespace vulkan
1996