blob: 75cabc18efa87ff56740f529894f485bd33edad4 [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>
18#include <memory>
19
20#include <gui/BufferQueue.h>
Jesse Hallb1352bc2015-09-04 16:12:33 -070021#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070022#include <sync/sync.h>
23
24#include "loader.h"
25
26using namespace vulkan;
27
Jesse Hall5ae3abb2015-10-08 14:00:22 -070028// TODO(jessehall): Currently we don't have a good error code for when a native
29// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
30// versions (post SDK 0.9) of the API/extension have a better error code.
31// When updating to that version, audit all error returns.
32
Jesse Halld7b994a2015-09-07 14:17:37 -070033namespace {
34
35// ----------------------------------------------------------------------------
36// These functions/classes form an adaptor that allows objects to be refcounted
37// by both android::sp<> and std::shared_ptr<> simultaneously, and delegates
Jesse Hall3fbc8562015-11-29 22:10:52 -080038// allocation of the shared_ptr<> control structure to VkAllocationCallbacks.
39// The
Jesse Halld7b994a2015-09-07 14:17:37 -070040// platform holds a reference to the ANativeWindow using its embedded reference
41// count, and the ANativeWindow implementation holds references to the
42// ANativeWindowBuffers using their embedded reference counts, so the
43// shared_ptr *must* cooperate with these and hold at least one reference to
44// the object using the embedded reference count.
45
46template <typename T>
47struct NativeBaseDeleter {
48 void operator()(T* obj) { obj->common.decRef(&obj->common); }
49};
50
Jesse Hall03b6fe12015-11-24 12:44:21 -080051template <typename Host>
52struct AllocScope {};
53
54template <>
55struct AllocScope<VkInstance> {
Jesse Hall3fbc8562015-11-29 22:10:52 -080056 static const VkSystemAllocationScope kScope =
57 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE;
Jesse Hall03b6fe12015-11-24 12:44:21 -080058};
59
60template <>
61struct AllocScope<VkDevice> {
Jesse Hall3fbc8562015-11-29 22:10:52 -080062 static const VkSystemAllocationScope kScope =
63 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE;
Jesse Hall03b6fe12015-11-24 12:44:21 -080064};
65
Jesse Hall1f91d392015-12-11 16:28:44 -080066template <typename T>
Jesse Halld7b994a2015-09-07 14:17:37 -070067class VulkanAllocator {
68 public:
69 typedef T value_type;
70
Jesse Hall1f91d392015-12-11 16:28:44 -080071 VulkanAllocator(const VkAllocationCallbacks& allocator,
72 VkSystemAllocationScope scope)
73 : allocator_(allocator), scope_(scope) {}
Jesse Halld7b994a2015-09-07 14:17:37 -070074
75 template <typename U>
Jesse Hall1f91d392015-12-11 16:28:44 -080076 explicit VulkanAllocator(const VulkanAllocator<U>& other)
77 : allocator_(other.allocator_), scope_(other.scope_) {}
Jesse Halld7b994a2015-09-07 14:17:37 -070078
79 T* allocate(size_t n) const {
Jesse Hall1f91d392015-12-11 16:28:44 -080080 return static_cast<T*>(allocator_.pfnAllocation(
81 allocator_.pUserData, n * sizeof(T), alignof(T), scope_));
Jesse Halld7b994a2015-09-07 14:17:37 -070082 }
Jesse Hall1f91d392015-12-11 16:28:44 -080083 void deallocate(T* p, size_t) const {
84 return allocator_.pfnFree(allocator_.pUserData, p);
85 }
Jesse Halld7b994a2015-09-07 14:17:37 -070086
87 private:
Jesse Hall1f91d392015-12-11 16:28:44 -080088 template <typename U>
Jesse Halld7b994a2015-09-07 14:17:37 -070089 friend class VulkanAllocator;
Jesse Hall1f91d392015-12-11 16:28:44 -080090 const VkAllocationCallbacks& allocator_;
91 const VkSystemAllocationScope scope_;
Jesse Halld7b994a2015-09-07 14:17:37 -070092};
93
Jesse Hall1356b0d2015-11-23 17:24:58 -080094template <typename T, typename Host>
95std::shared_ptr<T> InitSharedPtr(Host host, T* obj) {
Jesse Halld7b994a2015-09-07 14:17:37 -070096 obj->common.incRef(&obj->common);
Jesse Hall1f91d392015-12-11 16:28:44 -080097 return std::shared_ptr<T>(
98 obj, NativeBaseDeleter<T>(),
99 VulkanAllocator<T>(*GetAllocator(host), AllocScope<Host>::kScope));
Jesse Halld7b994a2015-09-07 14:17:37 -0700100}
101
102// ----------------------------------------------------------------------------
103
Jesse Hall1356b0d2015-11-23 17:24:58 -0800104struct Surface {
Jesse Halld7b994a2015-09-07 14:17:37 -0700105 std::shared_ptr<ANativeWindow> window;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800106};
107
108VkSurfaceKHR HandleFromSurface(Surface* surface) {
109 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
110}
111
112Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800113 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800114}
115
116struct Swapchain {
117 Swapchain(Surface& surface_, uint32_t num_images_)
118 : surface(surface_), num_images(num_images_) {}
119
120 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700121 uint32_t num_images;
122
123 struct Image {
124 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
125 VkImage image;
126 std::shared_ptr<ANativeWindowBuffer> buffer;
127 // The fence is only valid when the buffer is dequeued, and should be
128 // -1 any other time. When valid, we own the fd, and must ensure it is
129 // closed: either by closing it explicitly when queueing the buffer,
130 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
131 int dequeue_fence;
132 bool dequeued;
133 } images[android::BufferQueue::NUM_BUFFER_SLOTS];
134};
135
136VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
137 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
138}
139
140Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800141 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700142}
143
144} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700145
146namespace vulkan {
147
Jesse Halle1b12782015-11-30 11:27:32 -0800148VKAPI_ATTR
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800149VkResult CreateAndroidSurfaceKHR_Bottom(
150 VkInstance instance,
151 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
152 const VkAllocationCallbacks* allocator,
153 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800154 if (!allocator)
155 allocator = GetAllocator(instance);
156 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
157 alignof(Surface),
158 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800159 if (!mem)
160 return VK_ERROR_OUT_OF_HOST_MEMORY;
161 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700162
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800163 surface->window = InitSharedPtr(instance, pCreateInfo->window);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700164
Jesse Hall1356b0d2015-11-23 17:24:58 -0800165 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
166 int err =
167 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
168 if (err != 0) {
169 // TODO(jessehall): Improve error reporting. Can we enumerate possible
170 // errors and translate them to valid Vulkan result codes?
171 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
172 err);
173 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800174 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800175 return VK_ERROR_INITIALIZATION_FAILED;
176 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700177
Jesse Hall1356b0d2015-11-23 17:24:58 -0800178 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700179 return VK_SUCCESS;
180}
181
Jesse Halle1b12782015-11-30 11:27:32 -0800182VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800183void DestroySurfaceKHR_Bottom(VkInstance instance,
184 VkSurfaceKHR surface_handle,
185 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800186 Surface* surface = SurfaceFromHandle(surface_handle);
187 if (!surface)
188 return;
189 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
190 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800191 if (!allocator)
192 allocator = GetAllocator(instance);
193 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800194}
195
Jesse Halle1b12782015-11-30 11:27:32 -0800196VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800197VkResult GetPhysicalDeviceSurfaceSupportKHR_Bottom(VkPhysicalDevice /*pdev*/,
198 uint32_t /*queue_family*/,
199 VkSurfaceKHR /*surface*/,
200 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800201 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800202 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800203}
204
Jesse Halle1b12782015-11-30 11:27:32 -0800205VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800206VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Bottom(
Jesse Hallb00daad2015-11-29 19:46:20 -0800207 VkPhysicalDevice /*pdev*/,
208 VkSurfaceKHR surface,
209 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700210 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800211 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700212
213 int width, height;
214 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
215 if (err != 0) {
216 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
217 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700218 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700219 }
220 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
221 if (err != 0) {
222 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
223 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700224 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700225 }
226
Jesse Hall3dd678a2016-01-08 21:52:01 -0800227 capabilities->currentExtent =
228 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
Jesse Halld7b994a2015-09-07 14:17:37 -0700229
230 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800231 capabilities->minImageCount = 2;
232 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700233
234 // TODO(jessehall): Figure out what the max extent should be. Maximum
235 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800236 capabilities->minImageExtent = VkExtent2D{1, 1};
237 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700238
239 // TODO(jessehall): We can support all transforms, fix this once
240 // implemented.
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800241 capabilities->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700242
243 // TODO(jessehall): Implement based on NATIVE_WINDOW_TRANSFORM_HINT.
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800244 capabilities->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700245
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800246 capabilities->maxImageArrayLayers = 1;
Jesse Halld7b994a2015-09-07 14:17:37 -0700247
248 // TODO(jessehall): I think these are right, but haven't thought hard about
249 // it. Do we need to query the driver for support of any of these?
250 // Currently not included:
251 // - VK_IMAGE_USAGE_GENERAL: maybe? does this imply cpu mappable?
252 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
253 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800254 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800255 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
256 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
257 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700258 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
259
Jesse Hallb1352bc2015-09-04 16:12:33 -0700260 return VK_SUCCESS;
261}
262
Jesse Halle1b12782015-11-30 11:27:32 -0800263VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800264VkResult GetPhysicalDeviceSurfaceFormatsKHR_Bottom(
265 VkPhysicalDevice /*pdev*/,
266 VkSurfaceKHR /*surface*/,
267 uint32_t* count,
268 VkSurfaceFormatKHR* formats) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800269 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
270 // a new gralloc method to query whether a (format, usage) pair is
271 // supported, and check that for each gralloc format that corresponds to a
272 // Vulkan format. Shorter term, just add a few more formats to the ones
273 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700274
275 const VkSurfaceFormatKHR kFormats[] = {
276 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
277 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
278 };
279 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
280
281 VkResult result = VK_SUCCESS;
282 if (formats) {
283 if (*count < kNumFormats)
284 result = VK_INCOMPLETE;
285 std::copy(kFormats, kFormats + std::min(*count, kNumFormats), formats);
286 }
287 *count = kNumFormats;
288 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700289}
290
Jesse Halle1b12782015-11-30 11:27:32 -0800291VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800292VkResult GetPhysicalDeviceSurfacePresentModesKHR_Bottom(
293 VkPhysicalDevice /*pdev*/,
294 VkSurfaceKHR /*surface*/,
295 uint32_t* count,
296 VkPresentModeKHR* modes) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700297 const VkPresentModeKHR kModes[] = {
298 VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
299 };
300 const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]);
301
302 VkResult result = VK_SUCCESS;
303 if (modes) {
304 if (*count < kNumModes)
305 result = VK_INCOMPLETE;
306 std::copy(kModes, kModes + std::min(*count, kNumModes), modes);
307 }
308 *count = kNumModes;
309 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700310}
311
Jesse Halle1b12782015-11-30 11:27:32 -0800312VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800313VkResult CreateSwapchainKHR_Bottom(VkDevice device,
314 const VkSwapchainCreateInfoKHR* create_info,
315 const VkAllocationCallbacks* allocator,
316 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700317 int err;
318 VkResult result = VK_SUCCESS;
319
Jesse Hall1f91d392015-12-11 16:28:44 -0800320 if (!allocator)
321 allocator = GetAllocator(device);
322
Jesse Hall715b86a2016-01-16 16:34:29 -0800323 ALOGV_IF(create_info->imageArrayLayers != 1,
324 "Swapchain imageArrayLayers (%u) != 1 not supported",
325 create_info->imageArrayLayers);
Jesse Halld7b994a2015-09-07 14:17:37 -0700326
327 ALOGE_IF(create_info->imageFormat != VK_FORMAT_R8G8B8A8_UNORM,
328 "swapchain formats other than R8G8B8A8_UNORM not yet implemented");
329 ALOGE_IF(create_info->imageColorSpace != VK_COLORSPACE_SRGB_NONLINEAR_KHR,
330 "color spaces other than SRGB_NONLINEAR not yet implemented");
331 ALOGE_IF(create_info->oldSwapchain,
332 "swapchain re-creation not yet implemented");
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800333 ALOGE_IF(create_info->preTransform != VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
Jesse Halld7b994a2015-09-07 14:17:37 -0700334 "swapchain preTransform not yet implemented");
Jesse Hallf3339222016-01-18 12:28:00 -0800335 ALOGW_IF((create_info->presentMode != VK_PRESENT_MODE_FIFO_KHR ||
336 create_info->presentMode != VK_PRESENT_MODE_MAILBOX_KHR),
337 "swapchain present mode %d not supported",
338 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700339
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
Jesse Hallf3339222016-01-18 12:28:00 -0800409 err = surface.window->setSwapInterval(
410 surface.window.get(),
411 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1);
412 if (err != 0) {
413 // TODO(jessehall): Improve error reporting. Can we enumerate possible
414 // errors and translate them to valid Vulkan result codes?
415 ALOGE("native_window->setSwapInterval failed: %s (%d)", strerror(-err),
416 err);
417 return VK_ERROR_INITIALIZATION_FAILED;
418 }
419
Jesse Halld7b994a2015-09-07 14:17:37 -0700420 // -- Allocate our Swapchain object --
421 // After this point, we must deallocate the swapchain on error.
422
Jesse Hall1f91d392015-12-11 16:28:44 -0800423 void* mem = allocator->pfnAllocation(allocator->pUserData,
424 sizeof(Swapchain), alignof(Swapchain),
425 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800426 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -0700427 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800428 Swapchain* swapchain = new (mem) Swapchain(surface, num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700429
430 // -- Dequeue all buffers and create a VkImage for each --
431 // Any failures during or after this must cancel the dequeued buffers.
432
433 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -0700434#pragma clang diagnostic push
435#pragma clang diagnostic ignored "-Wold-style-cast"
436 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
437#pragma clang diagnostic pop
438 .pNext = nullptr,
439 };
440 VkImageCreateInfo image_create = {
441 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
442 .pNext = &image_native_buffer,
443 .imageType = VK_IMAGE_TYPE_2D,
444 .format = VK_FORMAT_R8G8B8A8_UNORM, // TODO(jessehall)
445 .extent = {0, 0, 1},
446 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -0800447 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -0800448 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -0700449 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800450 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -0700451 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800452 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800453 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -0700454 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
455 };
456
Jesse Halld7b994a2015-09-07 14:17:37 -0700457 for (uint32_t i = 0; i < num_images; i++) {
458 Swapchain::Image& img = swapchain->images[i];
459
460 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800461 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
462 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700463 if (err != 0) {
464 // TODO(jessehall): Improve error reporting. Can we enumerate
465 // possible errors and translate them to valid Vulkan result codes?
466 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700467 result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700468 break;
469 }
470 img.buffer = InitSharedPtr(device, buffer);
471 img.dequeued = true;
472
473 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -0800474 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
475 static_cast<uint32_t>(img.buffer->height),
476 1};
Jesse Halld7b994a2015-09-07 14:17:37 -0700477 image_native_buffer.handle = img.buffer->handle;
478 image_native_buffer.stride = img.buffer->stride;
479 image_native_buffer.format = img.buffer->format;
480 image_native_buffer.usage = img.buffer->usage;
481
Jesse Hall03b6fe12015-11-24 12:44:21 -0800482 result =
Jesse Hall1f91d392015-12-11 16:28:44 -0800483 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700484 if (result != VK_SUCCESS) {
485 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
486 break;
487 }
488 }
489
490 // -- Cancel all buffers, returning them to the queue --
491 // If an error occurred before, also destroy the VkImage and release the
492 // buffer reference. Otherwise, we retain a strong reference to the buffer.
493 //
494 // TODO(jessehall): The error path here is the same as DestroySwapchain,
495 // but not the non-error path. Should refactor/unify.
496 for (uint32_t i = 0; i < num_images; i++) {
497 Swapchain::Image& img = swapchain->images[i];
498 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800499 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
500 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700501 img.dequeue_fence = -1;
502 img.dequeued = false;
503 }
504 if (result != VK_SUCCESS) {
505 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -0800506 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700507 }
508 }
509
510 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700511 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800512 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700513 return result;
514 }
515
516 *swapchain_handle = HandleFromSwapchain(swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700517 return VK_SUCCESS;
518}
519
Jesse Halle1b12782015-11-30 11:27:32 -0800520VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800521void DestroySwapchainKHR_Bottom(VkDevice device,
522 VkSwapchainKHR swapchain_handle,
523 const VkAllocationCallbacks* allocator) {
524 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700525 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800526 const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window;
Jesse Halld7b994a2015-09-07 14:17:37 -0700527
528 for (uint32_t i = 0; i < swapchain->num_images; i++) {
529 Swapchain::Image& img = swapchain->images[i];
530 if (img.dequeued) {
531 window->cancelBuffer(window.get(), img.buffer.get(),
532 img.dequeue_fence);
533 img.dequeue_fence = -1;
534 img.dequeued = false;
535 }
536 if (img.image) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800537 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700538 }
539 }
540
Jesse Hall1f91d392015-12-11 16:28:44 -0800541 if (!allocator)
542 allocator = GetAllocator(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700543 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800544 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700545}
546
Jesse Halle1b12782015-11-30 11:27:32 -0800547VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800548VkResult GetSwapchainImagesKHR_Bottom(VkDevice,
549 VkSwapchainKHR swapchain_handle,
550 uint32_t* count,
551 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700552 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
553 VkResult result = VK_SUCCESS;
554 if (images) {
555 uint32_t n = swapchain.num_images;
556 if (*count < swapchain.num_images) {
557 n = *count;
558 result = VK_INCOMPLETE;
559 }
560 for (uint32_t i = 0; i < n; i++)
561 images[i] = swapchain.images[i].image;
562 }
563 *count = swapchain.num_images;
564 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700565}
566
Jesse Halle1b12782015-11-30 11:27:32 -0800567VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800568VkResult AcquireNextImageKHR_Bottom(VkDevice device,
569 VkSwapchainKHR swapchain_handle,
570 uint64_t timeout,
571 VkSemaphore semaphore,
572 VkFence vk_fence,
573 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700574 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800575 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700576 VkResult result;
577 int err;
578
579 ALOGW_IF(
580 timeout != UINT64_MAX,
581 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
582
583 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -0800584 int fence_fd;
585 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700586 if (err != 0) {
587 // TODO(jessehall): Improve error reporting. Can we enumerate possible
588 // errors and translate them to valid Vulkan result codes?
589 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700590 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700591 }
592
593 uint32_t idx;
594 for (idx = 0; idx < swapchain.num_images; idx++) {
595 if (swapchain.images[idx].buffer.get() == buffer) {
596 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -0800597 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -0700598 break;
599 }
600 }
601 if (idx == swapchain.num_images) {
602 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -0800603 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700604 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700605 }
606
607 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -0800608 if (fence_fd != -1) {
609 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700610 if (fence_clone == -1) {
611 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
612 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -0800613 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -0700614 }
615 }
616
Jesse Hall1f91d392015-12-11 16:28:44 -0800617 result = GetDriverDispatch(device).AcquireImageANDROID(
618 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700619 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800620 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
621 // even if the call fails. We could close it ourselves on failure, but
622 // that would create a race condition if the driver closes it on a
623 // failure path: some other thread might create an fd with the same
624 // number between the time the driver closes it and the time we close
625 // it. We must assume one of: the driver *always* closes it even on
626 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -0800627 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700628 swapchain.images[idx].dequeued = false;
629 swapchain.images[idx].dequeue_fence = -1;
630 return result;
631 }
632
633 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700634 return VK_SUCCESS;
635}
636
Jesse Halle1b12782015-11-30 11:27:32 -0800637VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800638VkResult QueuePresentKHR_Bottom(VkQueue queue,
639 const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700640 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
641 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
642 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -0700643 ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL");
644
Jesse Hall1f91d392015-12-11 16:28:44 -0800645 const DriverDispatchTable& dispatch = GetDriverDispatch(queue);
Jesse Halld7b994a2015-09-07 14:17:37 -0700646 VkResult final_result = VK_SUCCESS;
647 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
648 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -0800649 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800650 ANativeWindow* window = swapchain.surface.window.get();
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800651 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700652 Swapchain::Image& img = swapchain.images[image_idx];
Jesse Halld7b994a2015-09-07 14:17:37 -0700653 VkResult result;
654 int err;
655
Jesse Halld7b994a2015-09-07 14:17:37 -0700656 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -0800657 result = dispatch.QueueSignalReleaseImageANDROID(
658 queue, present_info->waitSemaphoreCount,
659 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700660 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800661 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halla9e57032015-11-30 01:03:10 -0800662 if (present_info->pResults)
663 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700664 if (final_result == VK_SUCCESS)
665 final_result = result;
666 // TODO(jessehall): What happens to the buffer here? Does the app
667 // still own it or not, i.e. should we cancel the buffer? Hard to
668 // do correctly without synchronizing, though I guess we could wait
669 // for the queue to idle.
670 continue;
671 }
672
Jesse Hall1356b0d2015-11-23 17:24:58 -0800673 err = window->queueBuffer(window, img.buffer.get(), fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700674 if (err != 0) {
675 // TODO(jessehall): What now? We should probably cancel the buffer,
676 // I guess?
677 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Halla9e57032015-11-30 01:03:10 -0800678 if (present_info->pResults)
679 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700680 if (final_result == VK_SUCCESS)
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700681 final_result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700682 continue;
683 }
684
685 if (img.dequeue_fence != -1) {
686 close(img.dequeue_fence);
687 img.dequeue_fence = -1;
688 }
689 img.dequeued = false;
Jesse Halla9e57032015-11-30 01:03:10 -0800690
691 if (present_info->pResults)
692 present_info->pResults[sc] = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -0700693 }
694
695 return final_result;
696}
Jesse Hallb1352bc2015-09-04 16:12:33 -0700697
698} // namespace vulkan