blob: 7e905035306d71836cf08d414b390aab7aa3d68c [file] [log] [blame]
Jesse Hallb1352bc2015-09-04 16:12:33 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jesse Halld7b994a2015-09-07 14:17:37 -070017#include <algorithm>
Jesse Halld7b994a2015-09-07 14:17:37 -070018
19#include <gui/BufferQueue.h>
Jesse Hallb1352bc2015-09-04 16:12:33 -070020#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070021#include <sync/sync.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080022#include <utils/StrongPointer.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070023
Chia-I Wu4a6a9162016-03-26 07:17:34 +080024#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070025
Jesse Hall5ae3abb2015-10-08 14:00:22 -070026// TODO(jessehall): Currently we don't have a good error code for when a native
27// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
28// versions (post SDK 0.9) of the API/extension have a better error code.
29// When updating to that version, audit all error returns.
Chia-I Wu62262232016-03-26 07:06:44 +080030namespace vulkan {
31namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070032
Jesse Halld7b994a2015-09-07 14:17:37 -070033namespace {
34
Jesse Hall55bc0972016-02-23 16:43:29 -080035const VkSurfaceTransformFlagsKHR kSupportedTransforms =
36 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
37 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
38 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
39 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
40 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
41 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
42 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
43 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
44 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
45 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
46
47VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
48 // Native and Vulkan transforms are isomorphic, but are represented
49 // differently. Vulkan transforms are built up of an optional horizontal
50 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
51 // transforms are built up from a horizontal flip, vertical flip, and
52 // 90-degree rotation, all optional but always in that order.
53
54 // TODO(jessehall): For now, only support pure rotations, not
55 // flip or flip-and-rotate, until I have more time to test them and build
56 // sample code. As far as I know we never actually use anything besides
57 // pure rotations anyway.
58
59 switch (native) {
60 case 0: // 0x0
61 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
62 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
63 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
64 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
65 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
66 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
67 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
68 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
69 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
70 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
71 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
72 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
73 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
74 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
75 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
76 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
77 default:
78 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
79 }
80}
81
Jesse Hall178b6962016-02-24 15:39:50 -080082int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
83 switch (transform) {
84 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
85 return NATIVE_WINDOW_TRANSFORM_ROT_270;
86 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
87 return NATIVE_WINDOW_TRANSFORM_ROT_180;
88 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
89 return NATIVE_WINDOW_TRANSFORM_ROT_90;
90 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
91 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
92 // return NATIVE_WINDOW_TRANSFORM_FLIP_H;
93 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
94 // return NATIVE_WINDOW_TRANSFORM_FLIP_H |
95 // NATIVE_WINDOW_TRANSFORM_ROT_90;
96 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
97 // return NATIVE_WINDOW_TRANSFORM_FLIP_V;
98 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
99 // return NATIVE_WINDOW_TRANSFORM_FLIP_V |
100 // NATIVE_WINDOW_TRANSFORM_ROT_90;
101 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
102 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
103 default:
104 return 0;
105 }
106}
107
Jesse Halld7b994a2015-09-07 14:17:37 -0700108// ----------------------------------------------------------------------------
109
Jesse Hall1356b0d2015-11-23 17:24:58 -0800110struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800111 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700112 VkSwapchainKHR swapchain_handle;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800113};
114
115VkSurfaceKHR HandleFromSurface(Surface* surface) {
116 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
117}
118
119Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800120 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800121}
122
123struct Swapchain {
124 Swapchain(Surface& surface_, uint32_t num_images_)
125 : surface(surface_), num_images(num_images_) {}
126
127 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700128 uint32_t num_images;
129
130 struct Image {
131 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
132 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800133 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700134 // The fence is only valid when the buffer is dequeued, and should be
135 // -1 any other time. When valid, we own the fd, and must ensure it is
136 // closed: either by closing it explicitly when queueing the buffer,
137 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
138 int dequeue_fence;
139 bool dequeued;
140 } images[android::BufferQueue::NUM_BUFFER_SLOTS];
141};
142
143VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
144 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
145}
146
147Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800148 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700149}
150
Jesse Halldc225072016-05-30 22:40:14 -0700151void ReleaseSwapchainImage(VkDevice device,
152 ANativeWindow* window,
153 int release_fence,
154 Swapchain::Image& image) {
155 ALOG_ASSERT(release_fence == -1 || image.dequeued,
156 "ReleaseSwapchainImage: can't provide a release fence for "
157 "non-dequeued images");
158
159 if (image.dequeued) {
160 if (release_fence >= 0) {
161 // We get here from vkQueuePresentKHR. The application is
162 // responsible for creating an execution dependency chain from
163 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
164 // (release_fence), so we can drop the dequeue_fence here.
165 if (image.dequeue_fence >= 0)
166 close(image.dequeue_fence);
167 } else {
168 // We get here during swapchain destruction, or various serious
169 // error cases e.g. when we can't create the release_fence during
170 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
171 // have already signalled, since the swapchain images are supposed
172 // to be idle before the swapchain is destroyed. In error cases,
173 // there may be rendering in flight to the image, but since we
174 // weren't able to create a release_fence, waiting for the
175 // dequeue_fence is about the best we can do.
176 release_fence = image.dequeue_fence;
177 }
178 image.dequeue_fence = -1;
179
180 if (window) {
181 window->cancelBuffer(window, image.buffer.get(), release_fence);
182 } else {
183 if (release_fence >= 0) {
184 sync_wait(release_fence, -1 /* forever */);
185 close(release_fence);
186 }
187 }
188
189 image.dequeued = false;
190 }
191
192 if (image.image) {
193 GetData(device).driver.DestroyImage(device, image.image, nullptr);
194 image.image = VK_NULL_HANDLE;
195 }
196
197 image.buffer.clear();
198}
199
200void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
201 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
202 return;
Jesse Halldc225072016-05-30 22:40:14 -0700203 for (uint32_t i = 0; i < swapchain->num_images; i++) {
204 if (!swapchain->images[i].dequeued)
205 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
206 }
207 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
208}
209
Jesse Halld7b994a2015-09-07 14:17:37 -0700210} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700211
Jesse Halle1b12782015-11-30 11:27:32 -0800212VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800213VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800214 VkInstance instance,
215 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
216 const VkAllocationCallbacks* allocator,
217 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800218 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800219 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800220 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
221 alignof(Surface),
222 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800223 if (!mem)
224 return VK_ERROR_OUT_OF_HOST_MEMORY;
225 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700226
Chia-I Wue8e689f2016-04-18 08:21:31 +0800227 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700228 surface->swapchain_handle = VK_NULL_HANDLE;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700229
Jesse Hall1356b0d2015-11-23 17:24:58 -0800230 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
231 int err =
232 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
233 if (err != 0) {
234 // TODO(jessehall): Improve error reporting. Can we enumerate possible
235 // errors and translate them to valid Vulkan result codes?
236 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
237 err);
238 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800239 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800240 return VK_ERROR_INITIALIZATION_FAILED;
241 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700242
Jesse Hall1356b0d2015-11-23 17:24:58 -0800243 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700244 return VK_SUCCESS;
245}
246
Jesse Halle1b12782015-11-30 11:27:32 -0800247VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800248void DestroySurfaceKHR(VkInstance instance,
249 VkSurfaceKHR surface_handle,
250 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800251 Surface* surface = SurfaceFromHandle(surface_handle);
252 if (!surface)
253 return;
254 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700255 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700256 "destroyed VkSurfaceKHR 0x%" PRIx64
257 " has active VkSwapchainKHR 0x%" PRIx64,
258 reinterpret_cast<uint64_t>(surface_handle),
259 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800260 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800261 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800262 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800263 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800264}
265
Jesse Halle1b12782015-11-30 11:27:32 -0800266VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800267VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
268 uint32_t /*queue_family*/,
269 VkSurfaceKHR /*surface*/,
270 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800271 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800272 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800273}
274
Jesse Halle1b12782015-11-30 11:27:32 -0800275VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800276VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800277 VkPhysicalDevice /*pdev*/,
278 VkSurfaceKHR surface,
279 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700280 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800281 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700282
283 int width, height;
284 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
285 if (err != 0) {
286 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
287 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700288 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700289 }
290 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
291 if (err != 0) {
292 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
293 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700294 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700295 }
296
Jesse Hall55bc0972016-02-23 16:43:29 -0800297 int transform_hint;
298 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
299 if (err != 0) {
300 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
301 strerror(-err), err);
302 return VK_ERROR_INITIALIZATION_FAILED;
303 }
304
Jesse Halld7b994a2015-09-07 14:17:37 -0700305 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800306 capabilities->minImageCount = 2;
307 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700308
Jesse Hallfe2662d2016-02-09 13:26:59 -0800309 capabilities->currentExtent =
310 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
311
Jesse Halld7b994a2015-09-07 14:17:37 -0700312 // TODO(jessehall): Figure out what the max extent should be. Maximum
313 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800314 capabilities->minImageExtent = VkExtent2D{1, 1};
315 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700316
Jesse Hallfe2662d2016-02-09 13:26:59 -0800317 capabilities->maxImageArrayLayers = 1;
318
Jesse Hall55bc0972016-02-23 16:43:29 -0800319 capabilities->supportedTransforms = kSupportedTransforms;
320 capabilities->currentTransform =
321 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700322
Jesse Hallfe2662d2016-02-09 13:26:59 -0800323 // On Android, window composition is a WindowManager property, not something
324 // associated with the bufferqueue. It can't be changed from here.
325 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700326
327 // TODO(jessehall): I think these are right, but haven't thought hard about
328 // it. Do we need to query the driver for support of any of these?
329 // Currently not included:
330 // - VK_IMAGE_USAGE_GENERAL: maybe? does this imply cpu mappable?
331 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
332 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800333 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800334 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
335 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
336 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700337 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
338
Jesse Hallb1352bc2015-09-04 16:12:33 -0700339 return VK_SUCCESS;
340}
341
Jesse Halle1b12782015-11-30 11:27:32 -0800342VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800343VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice /*pdev*/,
344 VkSurfaceKHR /*surface*/,
345 uint32_t* count,
346 VkSurfaceFormatKHR* formats) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800347 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
348 // a new gralloc method to query whether a (format, usage) pair is
349 // supported, and check that for each gralloc format that corresponds to a
350 // Vulkan format. Shorter term, just add a few more formats to the ones
351 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700352
353 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700354 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
355 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
356 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700357 };
358 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
359
360 VkResult result = VK_SUCCESS;
361 if (formats) {
362 if (*count < kNumFormats)
363 result = VK_INCOMPLETE;
Jesse Hall7331e222016-09-15 21:26:01 -0700364 *count = std::min(*count, kNumFormats);
365 std::copy(kFormats, kFormats + *count, formats);
366 } else {
367 *count = kNumFormats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700368 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700369 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700370}
371
Jesse Halle1b12782015-11-30 11:27:32 -0800372VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800373VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice /*pdev*/,
374 VkSurfaceKHR /*surface*/,
375 uint32_t* count,
376 VkPresentModeKHR* modes) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700377 const VkPresentModeKHR kModes[] = {
378 VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
379 };
380 const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]);
381
382 VkResult result = VK_SUCCESS;
383 if (modes) {
384 if (*count < kNumModes)
385 result = VK_INCOMPLETE;
Jesse Hall7331e222016-09-15 21:26:01 -0700386 *count = std::min(*count, kNumModes);
387 std::copy(kModes, kModes + *count, modes);
388 } else {
389 *count = kNumModes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700390 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700391 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700392}
393
Jesse Halle1b12782015-11-30 11:27:32 -0800394VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800395VkResult CreateSwapchainKHR(VkDevice device,
396 const VkSwapchainCreateInfoKHR* create_info,
397 const VkAllocationCallbacks* allocator,
398 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700399 int err;
400 VkResult result = VK_SUCCESS;
401
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700402 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
403 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
404 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
405 " oldSwapchain=0x%" PRIx64,
406 reinterpret_cast<uint64_t>(create_info->surface),
407 create_info->minImageCount, create_info->imageFormat,
408 create_info->imageColorSpace, create_info->imageExtent.width,
409 create_info->imageExtent.height, create_info->imageUsage,
410 create_info->preTransform, create_info->presentMode,
411 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
412
Jesse Hall1f91d392015-12-11 16:28:44 -0800413 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800414 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800415
Jesse Hall42a9eec2016-06-03 12:39:49 -0700416 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700417 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800418 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700419 ALOGV_IF(create_info->imageColorSpace != VK_COLOR_SPACE_SRGB_NONLINEAR_KHR,
Jesse Halldc225072016-05-30 22:40:14 -0700420 "swapchain imageColorSpace=%u not supported",
421 create_info->imageColorSpace);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700422 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700423 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800424 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700425 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800426 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700427 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800428 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700429
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700430 Surface& surface = *SurfaceFromHandle(create_info->surface);
431
Jesse Halldc225072016-05-30 22:40:14 -0700432 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -0700433 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -0700434 " because it already has active swapchain 0x%" PRIx64
435 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
436 reinterpret_cast<uint64_t>(create_info->surface),
437 reinterpret_cast<uint64_t>(surface.swapchain_handle),
438 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
439 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
440 }
441 if (create_info->oldSwapchain != VK_NULL_HANDLE)
442 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
443
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700444 // -- Reset the native window --
445 // The native window might have been used previously, and had its properties
446 // changed from defaults. That will affect the answer we get for queries
447 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
448 // attempt such queries.
449
Jesse Halldc225072016-05-30 22:40:14 -0700450 // The native window only allows dequeueing all buffers before any have
451 // been queued, since after that point at least one is assumed to be in
452 // non-FREE state at any given time. Disconnecting and re-connecting
453 // orphans the previous buffers, getting us back to the state where we can
454 // dequeue all buffers.
455 err = native_window_api_disconnect(surface.window.get(),
456 NATIVE_WINDOW_API_EGL);
457 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
458 strerror(-err), err);
459 err =
460 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
461 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
462 strerror(-err), err);
463
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700464 err = native_window_set_buffer_count(surface.window.get(), 0);
465 if (err != 0) {
466 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
467 strerror(-err), err);
468 return VK_ERROR_INITIALIZATION_FAILED;
469 }
470
471 err = surface.window->setSwapInterval(surface.window.get(), 1);
472 if (err != 0) {
473 // TODO(jessehall): Improve error reporting. Can we enumerate possible
474 // errors and translate them to valid Vulkan result codes?
475 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
476 strerror(-err), err);
477 return VK_ERROR_INITIALIZATION_FAILED;
478 }
479
Jesse Halld7b994a2015-09-07 14:17:37 -0700480 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700481
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800482 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -0800483
Jesse Hall517274a2016-02-10 00:07:18 -0800484 int native_format = HAL_PIXEL_FORMAT_RGBA_8888;
485 switch (create_info->imageFormat) {
486 case VK_FORMAT_R8G8B8A8_UNORM:
487 case VK_FORMAT_R8G8B8A8_SRGB:
488 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
489 break;
490 case VK_FORMAT_R5G6B5_UNORM_PACK16:
491 native_format = HAL_PIXEL_FORMAT_RGB_565;
492 break;
493 default:
Jesse Hall42a9eec2016-06-03 12:39:49 -0700494 ALOGV("unsupported swapchain format %d", create_info->imageFormat);
Jesse Hall517274a2016-02-10 00:07:18 -0800495 break;
496 }
497 err = native_window_set_buffers_format(surface.window.get(), native_format);
498 if (err != 0) {
499 // TODO(jessehall): Improve error reporting. Can we enumerate possible
500 // errors and translate them to valid Vulkan result codes?
501 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
502 native_format, strerror(-err), err);
503 return VK_ERROR_INITIALIZATION_FAILED;
504 }
505 err = native_window_set_buffers_data_space(surface.window.get(),
506 HAL_DATASPACE_SRGB_LINEAR);
507 if (err != 0) {
508 // TODO(jessehall): Improve error reporting. Can we enumerate possible
509 // errors and translate them to valid Vulkan result codes?
510 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
511 HAL_DATASPACE_SRGB_LINEAR, strerror(-err), err);
512 return VK_ERROR_INITIALIZATION_FAILED;
513 }
514
Jesse Hall3dd678a2016-01-08 21:52:01 -0800515 err = native_window_set_buffers_dimensions(
516 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
517 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700518 if (err != 0) {
519 // TODO(jessehall): Improve error reporting. Can we enumerate possible
520 // errors and translate them to valid Vulkan result codes?
521 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
522 create_info->imageExtent.width, create_info->imageExtent.height,
523 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700524 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700525 }
526
Jesse Hall178b6962016-02-24 15:39:50 -0800527 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
528 // applied during rendering. native_window_set_transform() expects the
529 // inverse: the transform the app is requesting that the compositor perform
530 // during composition. With native windows, pre-transform works by rendering
531 // with the same transform the compositor is applying (as in Vulkan), but
532 // then requesting the inverse transform, so that when the compositor does
533 // it's job the two transforms cancel each other out and the compositor ends
534 // up applying an identity transform to the app's buffer.
535 err = native_window_set_buffers_transform(
536 surface.window.get(),
537 InvertTransformToNative(create_info->preTransform));
538 if (err != 0) {
539 // TODO(jessehall): Improve error reporting. Can we enumerate possible
540 // errors and translate them to valid Vulkan result codes?
541 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
542 InvertTransformToNative(create_info->preTransform),
543 strerror(-err), err);
544 return VK_ERROR_INITIALIZATION_FAILED;
545 }
546
Jesse Hallf64ca122015-11-03 16:11:10 -0800547 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800548 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800549 if (err != 0) {
550 // TODO(jessehall): Improve error reporting. Can we enumerate possible
551 // errors and translate them to valid Vulkan result codes?
552 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
553 strerror(-err), err);
Jesse Hallf64ca122015-11-03 16:11:10 -0800554 return VK_ERROR_INITIALIZATION_FAILED;
555 }
556
Jesse Halle6080bf2016-02-28 20:58:50 -0800557 int query_value;
558 err = surface.window->query(surface.window.get(),
559 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
560 &query_value);
561 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700562 // TODO(jessehall): Improve error reporting. Can we enumerate possible
563 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -0800564 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
565 query_value);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700566 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700567 }
Jesse Halle6080bf2016-02-28 20:58:50 -0800568 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800569 // The MIN_UNDEQUEUED_BUFFERS query doesn't know whether we'll be using
570 // async mode or not, and assumes not. But in async mode, the BufferQueue
571 // requires an extra undequeued buffer.
572 // See BufferQueueCore::getMinUndequeuedBufferCountLocked().
573 if (create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
574 min_undequeued_buffers += 1;
575
Jesse Halld7b994a2015-09-07 14:17:37 -0700576 uint32_t num_images =
577 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800578 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700579 if (err != 0) {
580 // TODO(jessehall): Improve error reporting. Can we enumerate possible
581 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700582 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
583 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700584 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700585 }
586
Jesse Hall70f93352015-11-04 09:41:31 -0800587 int gralloc_usage = 0;
588 // TODO(jessehall): Remove conditional once all drivers have been updated
Jesse Hall1f91d392015-12-11 16:28:44 -0800589 if (dispatch.GetSwapchainGrallocUsageANDROID) {
590 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800591 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -0800592 &gralloc_usage);
593 if (result != VK_SUCCESS) {
594 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Jesse Hall70f93352015-11-04 09:41:31 -0800595 return VK_ERROR_INITIALIZATION_FAILED;
596 }
597 } else {
598 gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
599 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800600 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -0800601 if (err != 0) {
602 // TODO(jessehall): Improve error reporting. Can we enumerate possible
603 // errors and translate them to valid Vulkan result codes?
604 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Jesse Hall70f93352015-11-04 09:41:31 -0800605 return VK_ERROR_INITIALIZATION_FAILED;
606 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700607
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700608 int swap_interval =
609 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
610 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800611 if (err != 0) {
612 // TODO(jessehall): Improve error reporting. Can we enumerate possible
613 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700614 ALOGE("native_window->setSwapInterval(%d) failed: %s (%d)",
615 swap_interval, strerror(-err), err);
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800616 return VK_ERROR_INITIALIZATION_FAILED;
617 }
618
Jesse Halld7b994a2015-09-07 14:17:37 -0700619 // -- Allocate our Swapchain object --
620 // After this point, we must deallocate the swapchain on error.
621
Jesse Hall1f91d392015-12-11 16:28:44 -0800622 void* mem = allocator->pfnAllocation(allocator->pUserData,
623 sizeof(Swapchain), alignof(Swapchain),
624 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800625 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -0700626 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800627 Swapchain* swapchain = new (mem) Swapchain(surface, num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700628
629 // -- Dequeue all buffers and create a VkImage for each --
630 // Any failures during or after this must cancel the dequeued buffers.
631
632 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -0700633#pragma clang diagnostic push
634#pragma clang diagnostic ignored "-Wold-style-cast"
635 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
636#pragma clang diagnostic pop
637 .pNext = nullptr,
638 };
639 VkImageCreateInfo image_create = {
640 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
641 .pNext = &image_native_buffer,
642 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -0800643 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -0700644 .extent = {0, 0, 1},
645 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -0800646 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -0800647 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -0700648 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800649 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -0700650 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800651 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800652 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -0700653 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
654 };
655
Jesse Halld7b994a2015-09-07 14:17:37 -0700656 for (uint32_t i = 0; i < num_images; i++) {
657 Swapchain::Image& img = swapchain->images[i];
658
659 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800660 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
661 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700662 if (err != 0) {
663 // TODO(jessehall): Improve error reporting. Can we enumerate
664 // possible errors and translate them to valid Vulkan result codes?
665 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700666 result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700667 break;
668 }
Chia-I Wue8e689f2016-04-18 08:21:31 +0800669 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700670 img.dequeued = true;
671
672 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -0800673 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
674 static_cast<uint32_t>(img.buffer->height),
675 1};
Jesse Halld7b994a2015-09-07 14:17:37 -0700676 image_native_buffer.handle = img.buffer->handle;
677 image_native_buffer.stride = img.buffer->stride;
678 image_native_buffer.format = img.buffer->format;
679 image_native_buffer.usage = img.buffer->usage;
680
Jesse Hall03b6fe12015-11-24 12:44:21 -0800681 result =
Jesse Hall1f91d392015-12-11 16:28:44 -0800682 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700683 if (result != VK_SUCCESS) {
684 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
685 break;
686 }
687 }
688
689 // -- Cancel all buffers, returning them to the queue --
690 // If an error occurred before, also destroy the VkImage and release the
691 // buffer reference. Otherwise, we retain a strong reference to the buffer.
692 //
693 // TODO(jessehall): The error path here is the same as DestroySwapchain,
694 // but not the non-error path. Should refactor/unify.
695 for (uint32_t i = 0; i < num_images; i++) {
696 Swapchain::Image& img = swapchain->images[i];
697 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800698 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
699 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700700 img.dequeue_fence = -1;
701 img.dequeued = false;
702 }
703 if (result != VK_SUCCESS) {
704 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -0800705 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700706 }
707 }
708
709 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700710 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800711 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700712 return result;
713 }
714
Jesse Halldc225072016-05-30 22:40:14 -0700715 surface.swapchain_handle = HandleFromSwapchain(swapchain);
716 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700717 return VK_SUCCESS;
718}
719
Jesse Halle1b12782015-11-30 11:27:32 -0800720VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800721void DestroySwapchainKHR(VkDevice device,
722 VkSwapchainKHR swapchain_handle,
723 const VkAllocationCallbacks* allocator) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800724 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -0700725 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700726 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
727 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -0700728
Jesse Halldc225072016-05-30 22:40:14 -0700729 for (uint32_t i = 0; i < swapchain->num_images; i++)
730 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700731 if (active)
Jesse Halldc225072016-05-30 22:40:14 -0700732 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -0800733 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800734 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -0700735 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800736 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700737}
738
Jesse Halle1b12782015-11-30 11:27:32 -0800739VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800740VkResult GetSwapchainImagesKHR(VkDevice,
741 VkSwapchainKHR swapchain_handle,
742 uint32_t* count,
743 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700744 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -0700745 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
746 "getting images for non-active swapchain 0x%" PRIx64
747 "; only dequeued image handles are valid",
748 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -0700749 VkResult result = VK_SUCCESS;
750 if (images) {
751 uint32_t n = swapchain.num_images;
752 if (*count < swapchain.num_images) {
753 n = *count;
754 result = VK_INCOMPLETE;
755 }
756 for (uint32_t i = 0; i < n; i++)
757 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -0700758 *count = n;
759 } else {
760 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -0700761 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700762 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700763}
764
Jesse Halle1b12782015-11-30 11:27:32 -0800765VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800766VkResult AcquireNextImageKHR(VkDevice device,
767 VkSwapchainKHR swapchain_handle,
768 uint64_t timeout,
769 VkSemaphore semaphore,
770 VkFence vk_fence,
771 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700772 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800773 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700774 VkResult result;
775 int err;
776
Jesse Halldc225072016-05-30 22:40:14 -0700777 if (swapchain.surface.swapchain_handle != swapchain_handle)
778 return VK_ERROR_OUT_OF_DATE_KHR;
779
Jesse Halld7b994a2015-09-07 14:17:37 -0700780 ALOGW_IF(
781 timeout != UINT64_MAX,
782 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
783
784 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -0800785 int fence_fd;
786 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700787 if (err != 0) {
788 // TODO(jessehall): Improve error reporting. Can we enumerate possible
789 // errors and translate them to valid Vulkan result codes?
790 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700791 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700792 }
793
794 uint32_t idx;
795 for (idx = 0; idx < swapchain.num_images; idx++) {
796 if (swapchain.images[idx].buffer.get() == buffer) {
797 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -0800798 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -0700799 break;
800 }
801 }
802 if (idx == swapchain.num_images) {
803 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -0800804 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700805 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700806 }
807
808 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -0800809 if (fence_fd != -1) {
810 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700811 if (fence_clone == -1) {
812 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
813 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -0800814 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -0700815 }
816 }
817
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800818 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -0800819 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700820 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800821 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
822 // even if the call fails. We could close it ourselves on failure, but
823 // that would create a race condition if the driver closes it on a
824 // failure path: some other thread might create an fd with the same
825 // number between the time the driver closes it and the time we close
826 // it. We must assume one of: the driver *always* closes it even on
827 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -0800828 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700829 swapchain.images[idx].dequeued = false;
830 swapchain.images[idx].dequeue_fence = -1;
831 return result;
832 }
833
834 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700835 return VK_SUCCESS;
836}
837
Jesse Halldc225072016-05-30 22:40:14 -0700838static VkResult WorstPresentResult(VkResult a, VkResult b) {
839 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
840 // (in spec version 1.0.14).
841 static const VkResult kWorstToBest[] = {
842 VK_ERROR_DEVICE_LOST,
843 VK_ERROR_SURFACE_LOST_KHR,
844 VK_ERROR_OUT_OF_DATE_KHR,
845 VK_ERROR_OUT_OF_DEVICE_MEMORY,
846 VK_ERROR_OUT_OF_HOST_MEMORY,
847 VK_SUBOPTIMAL_KHR,
848 };
849 for (auto result : kWorstToBest) {
850 if (a == result || b == result)
851 return result;
852 }
853 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
854 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
855 return a != VK_SUCCESS ? a : b;
856}
857
Jesse Halle1b12782015-11-30 11:27:32 -0800858VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800859VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700860 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
861 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
862 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -0700863
Jesse Halldc225072016-05-30 22:40:14 -0700864 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800865 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -0700866 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -0700867
Ian Elliottcb351132016-12-13 10:30:40 -0700868 // Look at the pNext chain for supported extension structs:
869 const VkPresentRegionsKHR* present_regions = NULL;
870 const VkPresentRegionsKHR* next =
871 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
872 while (next) {
873 switch (next->sType) {
874 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
875 present_regions = next;
876 break;
877 default:
878 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
879 next->sType);
880 break;
881 }
882 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
883 }
884 ALOGV_IF(
885 present_regions &&
886 present_regions->swapchainCount != present_info->swapchainCount,
887 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
888 const VkPresentRegionKHR* regions =
889 (present_regions) ? present_regions->pRegions : NULL;
890 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
891 android_native_rect_t* rects = NULL;
892 uint32_t nrects = 0;
893
Jesse Halld7b994a2015-09-07 14:17:37 -0700894 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
895 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -0800896 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800897 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700898 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottcb351132016-12-13 10:30:40 -0700899 const VkPresentRegionKHR* region = (regions) ? &regions[sc] : NULL;
Jesse Halldc225072016-05-30 22:40:14 -0700900 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -0700901 VkResult result;
902 int err;
903
Jesse Halld7b994a2015-09-07 14:17:37 -0700904 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -0800905 result = dispatch.QueueSignalReleaseImageANDROID(
906 queue, present_info->waitSemaphoreCount,
907 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700908 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800909 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -0700910 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700911 }
912
Jesse Halldc225072016-05-30 22:40:14 -0700913 if (swapchain.surface.swapchain_handle ==
914 present_info->pSwapchains[sc]) {
915 ANativeWindow* window = swapchain.surface.window.get();
916 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -0700917 if (region) {
918 // Process the incremental-present hint for this swapchain:
919 uint32_t rcount = region->rectangleCount;
920 if (rcount > nrects) {
921 android_native_rect_t* new_rects =
922 static_cast<android_native_rect_t*>(
923 allocator->pfnReallocation(
924 allocator->pUserData, rects,
925 sizeof(android_native_rect_t) * rcount,
926 alignof(android_native_rect_t),
927 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
928 if (new_rects) {
929 rects = new_rects;
930 nrects = rcount;
931 } else {
932 rcount = 0; // Ignore the hint for this swapchain
933 }
934 }
935 for (uint32_t r = 0; r < rcount; ++r) {
936 if (region->pRectangles[r].layer > 0) {
937 ALOGV(
938 "vkQueuePresentKHR ignoring invalid layer "
939 "(%u); using layer 0 instead",
940 region->pRectangles[r].layer);
941 }
942 int x = region->pRectangles[r].offset.x;
943 int y = region->pRectangles[r].offset.y;
944 int width = static_cast<int>(
945 region->pRectangles[r].extent.width);
946 int height = static_cast<int>(
947 region->pRectangles[r].extent.height);
948 android_native_rect_t* cur_rect = &rects[r];
949 cur_rect->left = x;
950 cur_rect->top = y + height;
951 cur_rect->right = x + width;
952 cur_rect->bottom = y;
953 }
954 native_window_set_surface_damage(window, rects, rcount);
955 }
Jesse Halldc225072016-05-30 22:40:14 -0700956 err = window->queueBuffer(window, img.buffer.get(), fence);
957 // queueBuffer always closes fence, even on error
958 if (err != 0) {
959 // TODO(jessehall): What now? We should probably cancel the
960 // buffer, I guess?
961 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
962 swapchain_result = WorstPresentResult(
963 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
964 }
965 if (img.dequeue_fence >= 0) {
966 close(img.dequeue_fence);
967 img.dequeue_fence = -1;
968 }
969 img.dequeued = false;
970 }
971 if (swapchain_result != VK_SUCCESS) {
972 ReleaseSwapchainImage(device, window, fence, img);
973 OrphanSwapchain(device, &swapchain);
974 }
975 } else {
976 ReleaseSwapchainImage(device, nullptr, fence, img);
977 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700978 }
979
Jesse Halla9e57032015-11-30 01:03:10 -0800980 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -0700981 present_info->pResults[sc] = swapchain_result;
982
983 if (swapchain_result != final_result)
984 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -0700985 }
Ian Elliottcb351132016-12-13 10:30:40 -0700986 if (rects) {
987 allocator->pfnFree(allocator->pUserData, rects);
988 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700989
990 return final_result;
991}
Jesse Hallb1352bc2015-09-04 16:12:33 -0700992
Chia-I Wu62262232016-03-26 07:06:44 +0800993} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -0700994} // namespace vulkan