blob: 5ee2abf6797eb6794672e798e6693151e3693850 [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// #define LOG_NDEBUG 0
18
19#include <algorithm>
20#include <memory>
21
22#include <gui/BufferQueue.h>
Jesse Hallb1352bc2015-09-04 16:12:33 -070023#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070024#include <sync/sync.h>
25
26#include "loader.h"
27
28using namespace vulkan;
29
Jesse Hall5ae3abb2015-10-08 14:00:22 -070030// TODO(jessehall): Currently we don't have a good error code for when a native
31// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
32// versions (post SDK 0.9) of the API/extension have a better error code.
33// When updating to that version, audit all error returns.
34
Jesse Halld7b994a2015-09-07 14:17:37 -070035namespace {
36
37// ----------------------------------------------------------------------------
38// These functions/classes form an adaptor that allows objects to be refcounted
39// by both android::sp<> and std::shared_ptr<> simultaneously, and delegates
Jesse Hall3fbc8562015-11-29 22:10:52 -080040// allocation of the shared_ptr<> control structure to VkAllocationCallbacks.
41// The
Jesse Halld7b994a2015-09-07 14:17:37 -070042// platform holds a reference to the ANativeWindow using its embedded reference
43// count, and the ANativeWindow implementation holds references to the
44// ANativeWindowBuffers using their embedded reference counts, so the
45// shared_ptr *must* cooperate with these and hold at least one reference to
46// the object using the embedded reference count.
47
48template <typename T>
49struct NativeBaseDeleter {
50 void operator()(T* obj) { obj->common.decRef(&obj->common); }
51};
52
Jesse Hall03b6fe12015-11-24 12:44:21 -080053template <typename Host>
54struct AllocScope {};
55
56template <>
57struct AllocScope<VkInstance> {
Jesse Hall3fbc8562015-11-29 22:10:52 -080058 static const VkSystemAllocationScope kScope =
59 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE;
Jesse Hall03b6fe12015-11-24 12:44:21 -080060};
61
62template <>
63struct AllocScope<VkDevice> {
Jesse Hall3fbc8562015-11-29 22:10:52 -080064 static const VkSystemAllocationScope kScope =
65 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE;
Jesse Hall03b6fe12015-11-24 12:44:21 -080066};
67
Jesse Hall1f91d392015-12-11 16:28:44 -080068template <typename T>
Jesse Halld7b994a2015-09-07 14:17:37 -070069class VulkanAllocator {
70 public:
71 typedef T value_type;
72
Jesse Hall1f91d392015-12-11 16:28:44 -080073 VulkanAllocator(const VkAllocationCallbacks& allocator,
74 VkSystemAllocationScope scope)
75 : allocator_(allocator), scope_(scope) {}
Jesse Halld7b994a2015-09-07 14:17:37 -070076
77 template <typename U>
Jesse Hall1f91d392015-12-11 16:28:44 -080078 explicit VulkanAllocator(const VulkanAllocator<U>& other)
79 : allocator_(other.allocator_), scope_(other.scope_) {}
Jesse Halld7b994a2015-09-07 14:17:37 -070080
81 T* allocate(size_t n) const {
Jesse Hall1f91d392015-12-11 16:28:44 -080082 return static_cast<T*>(allocator_.pfnAllocation(
83 allocator_.pUserData, n * sizeof(T), alignof(T), scope_));
Jesse Halld7b994a2015-09-07 14:17:37 -070084 }
Jesse Hall1f91d392015-12-11 16:28:44 -080085 void deallocate(T* p, size_t) const {
86 return allocator_.pfnFree(allocator_.pUserData, p);
87 }
Jesse Halld7b994a2015-09-07 14:17:37 -070088
89 private:
Jesse Hall1f91d392015-12-11 16:28:44 -080090 template <typename U>
Jesse Halld7b994a2015-09-07 14:17:37 -070091 friend class VulkanAllocator;
Jesse Hall1f91d392015-12-11 16:28:44 -080092 const VkAllocationCallbacks& allocator_;
93 const VkSystemAllocationScope scope_;
Jesse Halld7b994a2015-09-07 14:17:37 -070094};
95
Jesse Hall1356b0d2015-11-23 17:24:58 -080096template <typename T, typename Host>
97std::shared_ptr<T> InitSharedPtr(Host host, T* obj) {
Jesse Halld7b994a2015-09-07 14:17:37 -070098 obj->common.incRef(&obj->common);
Jesse Hall1f91d392015-12-11 16:28:44 -080099 return std::shared_ptr<T>(
100 obj, NativeBaseDeleter<T>(),
101 VulkanAllocator<T>(*GetAllocator(host), AllocScope<Host>::kScope));
Jesse Halld7b994a2015-09-07 14:17:37 -0700102}
103
104// ----------------------------------------------------------------------------
105
Jesse Hall1356b0d2015-11-23 17:24:58 -0800106struct Surface {
Jesse Halld7b994a2015-09-07 14:17:37 -0700107 std::shared_ptr<ANativeWindow> window;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800108};
109
110VkSurfaceKHR HandleFromSurface(Surface* surface) {
111 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
112}
113
114Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800115 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800116}
117
118struct Swapchain {
119 Swapchain(Surface& surface_, uint32_t num_images_)
120 : surface(surface_), num_images(num_images_) {}
121
122 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700123 uint32_t num_images;
124
125 struct Image {
126 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
127 VkImage image;
128 std::shared_ptr<ANativeWindowBuffer> buffer;
129 // The fence is only valid when the buffer is dequeued, and should be
130 // -1 any other time. When valid, we own the fd, and must ensure it is
131 // closed: either by closing it explicitly when queueing the buffer,
132 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
133 int dequeue_fence;
134 bool dequeued;
135 } images[android::BufferQueue::NUM_BUFFER_SLOTS];
136};
137
138VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
139 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
140}
141
142Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800143 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700144}
145
146} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700147
148namespace vulkan {
149
Jesse Halle1b12782015-11-30 11:27:32 -0800150VKAPI_ATTR
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800151VkResult CreateAndroidSurfaceKHR_Bottom(
152 VkInstance instance,
153 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
154 const VkAllocationCallbacks* allocator,
155 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800156 if (!allocator)
157 allocator = GetAllocator(instance);
158 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
159 alignof(Surface),
160 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800161 if (!mem)
162 return VK_ERROR_OUT_OF_HOST_MEMORY;
163 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700164
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800165 surface->window = InitSharedPtr(instance, pCreateInfo->window);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700166
Jesse Hall1356b0d2015-11-23 17:24:58 -0800167 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
168 int err =
169 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
170 if (err != 0) {
171 // TODO(jessehall): Improve error reporting. Can we enumerate possible
172 // errors and translate them to valid Vulkan result codes?
173 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
174 err);
175 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800176 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800177 return VK_ERROR_INITIALIZATION_FAILED;
178 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700179
Jesse Hall1356b0d2015-11-23 17:24:58 -0800180 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700181 return VK_SUCCESS;
182}
183
Jesse Halle1b12782015-11-30 11:27:32 -0800184VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800185void DestroySurfaceKHR_Bottom(VkInstance instance,
186 VkSurfaceKHR surface_handle,
187 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800188 Surface* surface = SurfaceFromHandle(surface_handle);
189 if (!surface)
190 return;
191 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
192 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800193 if (!allocator)
194 allocator = GetAllocator(instance);
195 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800196}
197
Jesse Halle1b12782015-11-30 11:27:32 -0800198VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800199VkResult GetPhysicalDeviceSurfaceSupportKHR_Bottom(VkPhysicalDevice /*pdev*/,
200 uint32_t /*queue_family*/,
201 VkSurfaceKHR /*surface*/,
202 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800203 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800204 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800205}
206
Jesse Halle1b12782015-11-30 11:27:32 -0800207VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800208VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Bottom(
Jesse Hallb00daad2015-11-29 19:46:20 -0800209 VkPhysicalDevice /*pdev*/,
210 VkSurfaceKHR surface,
211 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700212 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800213 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700214
215 int width, height;
216 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
217 if (err != 0) {
218 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
219 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700220 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700221 }
222 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
223 if (err != 0) {
224 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
225 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700226 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700227 }
228
Jesse Hall3dd678a2016-01-08 21:52:01 -0800229 capabilities->currentExtent =
230 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
Jesse Halld7b994a2015-09-07 14:17:37 -0700231
232 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800233 capabilities->minImageCount = 2;
234 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700235
236 // TODO(jessehall): Figure out what the max extent should be. Maximum
237 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800238 capabilities->minImageExtent = VkExtent2D{1, 1};
239 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700240
241 // TODO(jessehall): We can support all transforms, fix this once
242 // implemented.
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800243 capabilities->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700244
245 // TODO(jessehall): Implement based on NATIVE_WINDOW_TRANSFORM_HINT.
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800246 capabilities->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700247
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800248 capabilities->maxImageArrayLayers = 1;
Jesse Halld7b994a2015-09-07 14:17:37 -0700249
250 // TODO(jessehall): I think these are right, but haven't thought hard about
251 // it. Do we need to query the driver for support of any of these?
252 // Currently not included:
253 // - VK_IMAGE_USAGE_GENERAL: maybe? does this imply cpu mappable?
254 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
255 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800256 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800257 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
258 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
259 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700260 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
261
Jesse Hallb1352bc2015-09-04 16:12:33 -0700262 return VK_SUCCESS;
263}
264
Jesse Halle1b12782015-11-30 11:27:32 -0800265VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800266VkResult GetPhysicalDeviceSurfaceFormatsKHR_Bottom(
267 VkPhysicalDevice /*pdev*/,
268 VkSurfaceKHR /*surface*/,
269 uint32_t* count,
270 VkSurfaceFormatKHR* formats) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800271 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
272 // a new gralloc method to query whether a (format, usage) pair is
273 // supported, and check that for each gralloc format that corresponds to a
274 // Vulkan format. Shorter term, just add a few more formats to the ones
275 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700276
277 const VkSurfaceFormatKHR kFormats[] = {
278 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
279 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
280 };
281 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
282
283 VkResult result = VK_SUCCESS;
284 if (formats) {
285 if (*count < kNumFormats)
286 result = VK_INCOMPLETE;
287 std::copy(kFormats, kFormats + std::min(*count, kNumFormats), formats);
288 }
289 *count = kNumFormats;
290 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700291}
292
Jesse Halle1b12782015-11-30 11:27:32 -0800293VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800294VkResult GetPhysicalDeviceSurfacePresentModesKHR_Bottom(
295 VkPhysicalDevice /*pdev*/,
296 VkSurfaceKHR /*surface*/,
297 uint32_t* count,
298 VkPresentModeKHR* modes) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700299 const VkPresentModeKHR kModes[] = {
300 VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
301 };
302 const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]);
303
304 VkResult result = VK_SUCCESS;
305 if (modes) {
306 if (*count < kNumModes)
307 result = VK_INCOMPLETE;
308 std::copy(kModes, kModes + std::min(*count, kNumModes), modes);
309 }
310 *count = kNumModes;
311 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700312}
313
Jesse Halle1b12782015-11-30 11:27:32 -0800314VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800315VkResult CreateSwapchainKHR_Bottom(VkDevice device,
316 const VkSwapchainCreateInfoKHR* create_info,
317 const VkAllocationCallbacks* allocator,
318 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700319 int err;
320 VkResult result = VK_SUCCESS;
321
Jesse Hall1f91d392015-12-11 16:28:44 -0800322 if (!allocator)
323 allocator = GetAllocator(device);
324
Jesse Halld7b994a2015-09-07 14:17:37 -0700325 ALOGV_IF(create_info->imageArraySize != 1,
326 "Swapchain imageArraySize (%u) != 1 not supported",
327 create_info->imageArraySize);
328
329 ALOGE_IF(create_info->imageFormat != VK_FORMAT_R8G8B8A8_UNORM,
330 "swapchain formats other than R8G8B8A8_UNORM not yet implemented");
331 ALOGE_IF(create_info->imageColorSpace != VK_COLORSPACE_SRGB_NONLINEAR_KHR,
332 "color spaces other than SRGB_NONLINEAR not yet implemented");
333 ALOGE_IF(create_info->oldSwapchain,
334 "swapchain re-creation not yet implemented");
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800335 ALOGE_IF(create_info->preTransform != VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
Jesse Halld7b994a2015-09-07 14:17:37 -0700336 "swapchain preTransform not yet implemented");
337 ALOGE_IF(create_info->presentMode != VK_PRESENT_MODE_FIFO_KHR,
338 "present modes other than FIFO are not yet implemented");
339
340 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700341
Jesse Hall1356b0d2015-11-23 17:24:58 -0800342 Surface& surface = *SurfaceFromHandle(create_info->surface);
Jesse Hall1f91d392015-12-11 16:28:44 -0800343 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Hall70f93352015-11-04 09:41:31 -0800344
Jesse Hall3dd678a2016-01-08 21:52:01 -0800345 err = native_window_set_buffers_dimensions(
346 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
347 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700348 if (err != 0) {
349 // TODO(jessehall): Improve error reporting. Can we enumerate possible
350 // errors and translate them to valid Vulkan result codes?
351 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
352 create_info->imageExtent.width, create_info->imageExtent.height,
353 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700354 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700355 }
356
Jesse Hallf64ca122015-11-03 16:11:10 -0800357 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800358 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800359 if (err != 0) {
360 // TODO(jessehall): Improve error reporting. Can we enumerate possible
361 // errors and translate them to valid Vulkan result codes?
362 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
363 strerror(-err), err);
Jesse Hallf64ca122015-11-03 16:11:10 -0800364 return VK_ERROR_INITIALIZATION_FAILED;
365 }
366
Jesse Halld7b994a2015-09-07 14:17:37 -0700367 uint32_t min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800368 err = surface.window->query(
369 surface.window.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
370 reinterpret_cast<int*>(&min_undequeued_buffers));
Jesse Halld7b994a2015-09-07 14:17:37 -0700371 if (err != 0) {
372 // TODO(jessehall): Improve error reporting. Can we enumerate possible
373 // errors and translate them to valid Vulkan result codes?
374 ALOGE("window->query failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700375 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700376 }
377 uint32_t num_images =
378 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800379 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700380 if (err != 0) {
381 // TODO(jessehall): Improve error reporting. Can we enumerate possible
382 // errors and translate them to valid Vulkan result codes?
383 ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
384 err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700385 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700386 }
387
Jesse Hall70f93352015-11-04 09:41:31 -0800388 int gralloc_usage = 0;
389 // TODO(jessehall): Remove conditional once all drivers have been updated
Jesse Hall1f91d392015-12-11 16:28:44 -0800390 if (dispatch.GetSwapchainGrallocUsageANDROID) {
391 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800392 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -0800393 &gralloc_usage);
394 if (result != VK_SUCCESS) {
395 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Jesse Hall70f93352015-11-04 09:41:31 -0800396 return VK_ERROR_INITIALIZATION_FAILED;
397 }
398 } else {
399 gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
400 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800401 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -0800402 if (err != 0) {
403 // TODO(jessehall): Improve error reporting. Can we enumerate possible
404 // errors and translate them to valid Vulkan result codes?
405 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Jesse Hall70f93352015-11-04 09:41:31 -0800406 return VK_ERROR_INITIALIZATION_FAILED;
407 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700408
409 // -- Allocate our Swapchain object --
410 // After this point, we must deallocate the swapchain on error.
411
Jesse Hall1f91d392015-12-11 16:28:44 -0800412 void* mem = allocator->pfnAllocation(allocator->pUserData,
413 sizeof(Swapchain), alignof(Swapchain),
414 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800415 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -0700416 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800417 Swapchain* swapchain = new (mem) Swapchain(surface, num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700418
419 // -- Dequeue all buffers and create a VkImage for each --
420 // Any failures during or after this must cancel the dequeued buffers.
421
422 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -0700423#pragma clang diagnostic push
424#pragma clang diagnostic ignored "-Wold-style-cast"
425 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
426#pragma clang diagnostic pop
427 .pNext = nullptr,
428 };
429 VkImageCreateInfo image_create = {
430 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
431 .pNext = &image_native_buffer,
432 .imageType = VK_IMAGE_TYPE_2D,
433 .format = VK_FORMAT_R8G8B8A8_UNORM, // TODO(jessehall)
434 .extent = {0, 0, 1},
435 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -0800436 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -0800437 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -0700438 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800439 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -0700440 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800441 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800442 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -0700443 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
444 };
445
Jesse Halld7b994a2015-09-07 14:17:37 -0700446 for (uint32_t i = 0; i < num_images; i++) {
447 Swapchain::Image& img = swapchain->images[i];
448
449 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800450 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
451 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700452 if (err != 0) {
453 // TODO(jessehall): Improve error reporting. Can we enumerate
454 // possible errors and translate them to valid Vulkan result codes?
455 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700456 result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700457 break;
458 }
459 img.buffer = InitSharedPtr(device, buffer);
460 img.dequeued = true;
461
462 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -0800463 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
464 static_cast<uint32_t>(img.buffer->height),
465 1};
Jesse Halld7b994a2015-09-07 14:17:37 -0700466 image_native_buffer.handle = img.buffer->handle;
467 image_native_buffer.stride = img.buffer->stride;
468 image_native_buffer.format = img.buffer->format;
469 image_native_buffer.usage = img.buffer->usage;
470
Jesse Hall03b6fe12015-11-24 12:44:21 -0800471 result =
Jesse Hall1f91d392015-12-11 16:28:44 -0800472 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700473 if (result != VK_SUCCESS) {
474 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
475 break;
476 }
477 }
478
479 // -- Cancel all buffers, returning them to the queue --
480 // If an error occurred before, also destroy the VkImage and release the
481 // buffer reference. Otherwise, we retain a strong reference to the buffer.
482 //
483 // TODO(jessehall): The error path here is the same as DestroySwapchain,
484 // but not the non-error path. Should refactor/unify.
485 for (uint32_t i = 0; i < num_images; i++) {
486 Swapchain::Image& img = swapchain->images[i];
487 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800488 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
489 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700490 img.dequeue_fence = -1;
491 img.dequeued = false;
492 }
493 if (result != VK_SUCCESS) {
494 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -0800495 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700496 }
497 }
498
499 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700500 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800501 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700502 return result;
503 }
504
505 *swapchain_handle = HandleFromSwapchain(swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700506 return VK_SUCCESS;
507}
508
Jesse Halle1b12782015-11-30 11:27:32 -0800509VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800510void DestroySwapchainKHR_Bottom(VkDevice device,
511 VkSwapchainKHR swapchain_handle,
512 const VkAllocationCallbacks* allocator) {
513 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700514 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800515 const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window;
Jesse Halld7b994a2015-09-07 14:17:37 -0700516
517 for (uint32_t i = 0; i < swapchain->num_images; i++) {
518 Swapchain::Image& img = swapchain->images[i];
519 if (img.dequeued) {
520 window->cancelBuffer(window.get(), img.buffer.get(),
521 img.dequeue_fence);
522 img.dequeue_fence = -1;
523 img.dequeued = false;
524 }
525 if (img.image) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800526 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700527 }
528 }
529
Jesse Hall1f91d392015-12-11 16:28:44 -0800530 if (!allocator)
531 allocator = GetAllocator(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700532 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800533 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700534}
535
Jesse Halle1b12782015-11-30 11:27:32 -0800536VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800537VkResult GetSwapchainImagesKHR_Bottom(VkDevice,
538 VkSwapchainKHR swapchain_handle,
539 uint32_t* count,
540 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700541 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
542 VkResult result = VK_SUCCESS;
543 if (images) {
544 uint32_t n = swapchain.num_images;
545 if (*count < swapchain.num_images) {
546 n = *count;
547 result = VK_INCOMPLETE;
548 }
549 for (uint32_t i = 0; i < n; i++)
550 images[i] = swapchain.images[i].image;
551 }
552 *count = swapchain.num_images;
553 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700554}
555
Jesse Halle1b12782015-11-30 11:27:32 -0800556VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800557VkResult AcquireNextImageKHR_Bottom(VkDevice device,
558 VkSwapchainKHR swapchain_handle,
559 uint64_t timeout,
560 VkSemaphore semaphore,
561 VkFence vk_fence,
562 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700563 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800564 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700565 VkResult result;
566 int err;
567
568 ALOGW_IF(
569 timeout != UINT64_MAX,
570 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
571
572 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -0800573 int fence_fd;
574 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700575 if (err != 0) {
576 // TODO(jessehall): Improve error reporting. Can we enumerate possible
577 // errors and translate them to valid Vulkan result codes?
578 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700579 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700580 }
581
582 uint32_t idx;
583 for (idx = 0; idx < swapchain.num_images; idx++) {
584 if (swapchain.images[idx].buffer.get() == buffer) {
585 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -0800586 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -0700587 break;
588 }
589 }
590 if (idx == swapchain.num_images) {
591 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -0800592 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700593 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700594 }
595
596 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -0800597 if (fence_fd != -1) {
598 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700599 if (fence_clone == -1) {
600 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
601 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -0800602 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -0700603 }
604 }
605
Jesse Hall1f91d392015-12-11 16:28:44 -0800606 result = GetDriverDispatch(device).AcquireImageANDROID(
607 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700608 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800609 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
610 // even if the call fails. We could close it ourselves on failure, but
611 // that would create a race condition if the driver closes it on a
612 // failure path: some other thread might create an fd with the same
613 // number between the time the driver closes it and the time we close
614 // it. We must assume one of: the driver *always* closes it even on
615 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -0800616 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700617 swapchain.images[idx].dequeued = false;
618 swapchain.images[idx].dequeue_fence = -1;
619 return result;
620 }
621
622 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700623 return VK_SUCCESS;
624}
625
Jesse Halle1b12782015-11-30 11:27:32 -0800626VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800627VkResult QueuePresentKHR_Bottom(VkQueue queue,
628 const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700629 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
630 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
631 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -0700632 ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL");
633
Jesse Hall1f91d392015-12-11 16:28:44 -0800634 const DriverDispatchTable& dispatch = GetDriverDispatch(queue);
Jesse Halld7b994a2015-09-07 14:17:37 -0700635 VkResult final_result = VK_SUCCESS;
636 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
637 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -0800638 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800639 ANativeWindow* window = swapchain.surface.window.get();
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800640 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700641 Swapchain::Image& img = swapchain.images[image_idx];
Jesse Halld7b994a2015-09-07 14:17:37 -0700642 VkResult result;
643 int err;
644
Jesse Halld7b994a2015-09-07 14:17:37 -0700645 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -0800646 result = dispatch.QueueSignalReleaseImageANDROID(
647 queue, present_info->waitSemaphoreCount,
648 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700649 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800650 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halla9e57032015-11-30 01:03:10 -0800651 if (present_info->pResults)
652 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700653 if (final_result == VK_SUCCESS)
654 final_result = result;
655 // TODO(jessehall): What happens to the buffer here? Does the app
656 // still own it or not, i.e. should we cancel the buffer? Hard to
657 // do correctly without synchronizing, though I guess we could wait
658 // for the queue to idle.
659 continue;
660 }
661
Jesse Hall1356b0d2015-11-23 17:24:58 -0800662 err = window->queueBuffer(window, img.buffer.get(), fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700663 if (err != 0) {
664 // TODO(jessehall): What now? We should probably cancel the buffer,
665 // I guess?
666 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Halla9e57032015-11-30 01:03:10 -0800667 if (present_info->pResults)
668 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700669 if (final_result == VK_SUCCESS)
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700670 final_result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700671 continue;
672 }
673
674 if (img.dequeue_fence != -1) {
675 close(img.dequeue_fence);
676 img.dequeue_fence = -1;
677 }
678 img.dequeued = false;
Jesse Halla9e57032015-11-30 01:03:10 -0800679
680 if (present_info->pResults)
681 present_info->pResults[sc] = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -0700682 }
683
684 return final_result;
685}
Jesse Hallb1352bc2015-09-04 16:12:33 -0700686
687} // namespace vulkan