blob: da843f1bb04e58ccab0dbdc8159776bcbccff3dc [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");
335 ALOGE_IF(create_info->presentMode != VK_PRESENT_MODE_FIFO_KHR,
336 "present modes other than FIFO are not yet implemented");
337
338 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700339
Jesse Hall1356b0d2015-11-23 17:24:58 -0800340 Surface& surface = *SurfaceFromHandle(create_info->surface);
Jesse Hall1f91d392015-12-11 16:28:44 -0800341 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Hall70f93352015-11-04 09:41:31 -0800342
Jesse Hall3dd678a2016-01-08 21:52:01 -0800343 err = native_window_set_buffers_dimensions(
344 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
345 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700346 if (err != 0) {
347 // TODO(jessehall): Improve error reporting. Can we enumerate possible
348 // errors and translate them to valid Vulkan result codes?
349 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
350 create_info->imageExtent.width, create_info->imageExtent.height,
351 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700352 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700353 }
354
Jesse Hallf64ca122015-11-03 16:11:10 -0800355 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800356 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800357 if (err != 0) {
358 // TODO(jessehall): Improve error reporting. Can we enumerate possible
359 // errors and translate them to valid Vulkan result codes?
360 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
361 strerror(-err), err);
Jesse Hallf64ca122015-11-03 16:11:10 -0800362 return VK_ERROR_INITIALIZATION_FAILED;
363 }
364
Jesse Halld7b994a2015-09-07 14:17:37 -0700365 uint32_t min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800366 err = surface.window->query(
367 surface.window.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
368 reinterpret_cast<int*>(&min_undequeued_buffers));
Jesse Halld7b994a2015-09-07 14:17:37 -0700369 if (err != 0) {
370 // TODO(jessehall): Improve error reporting. Can we enumerate possible
371 // errors and translate them to valid Vulkan result codes?
372 ALOGE("window->query failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700373 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700374 }
375 uint32_t num_images =
376 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800377 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700378 if (err != 0) {
379 // TODO(jessehall): Improve error reporting. Can we enumerate possible
380 // errors and translate them to valid Vulkan result codes?
381 ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
382 err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700383 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700384 }
385
Jesse Hall70f93352015-11-04 09:41:31 -0800386 int gralloc_usage = 0;
387 // TODO(jessehall): Remove conditional once all drivers have been updated
Jesse Hall1f91d392015-12-11 16:28:44 -0800388 if (dispatch.GetSwapchainGrallocUsageANDROID) {
389 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800390 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -0800391 &gralloc_usage);
392 if (result != VK_SUCCESS) {
393 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Jesse Hall70f93352015-11-04 09:41:31 -0800394 return VK_ERROR_INITIALIZATION_FAILED;
395 }
396 } else {
397 gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
398 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800399 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -0800400 if (err != 0) {
401 // TODO(jessehall): Improve error reporting. Can we enumerate possible
402 // errors and translate them to valid Vulkan result codes?
403 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Jesse Hall70f93352015-11-04 09:41:31 -0800404 return VK_ERROR_INITIALIZATION_FAILED;
405 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700406
407 // -- Allocate our Swapchain object --
408 // After this point, we must deallocate the swapchain on error.
409
Jesse Hall1f91d392015-12-11 16:28:44 -0800410 void* mem = allocator->pfnAllocation(allocator->pUserData,
411 sizeof(Swapchain), alignof(Swapchain),
412 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800413 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -0700414 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800415 Swapchain* swapchain = new (mem) Swapchain(surface, num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700416
417 // -- Dequeue all buffers and create a VkImage for each --
418 // Any failures during or after this must cancel the dequeued buffers.
419
420 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -0700421#pragma clang diagnostic push
422#pragma clang diagnostic ignored "-Wold-style-cast"
423 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
424#pragma clang diagnostic pop
425 .pNext = nullptr,
426 };
427 VkImageCreateInfo image_create = {
428 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
429 .pNext = &image_native_buffer,
430 .imageType = VK_IMAGE_TYPE_2D,
431 .format = VK_FORMAT_R8G8B8A8_UNORM, // TODO(jessehall)
432 .extent = {0, 0, 1},
433 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -0800434 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -0800435 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -0700436 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800437 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -0700438 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800439 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800440 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -0700441 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
442 };
443
Jesse Halld7b994a2015-09-07 14:17:37 -0700444 for (uint32_t i = 0; i < num_images; i++) {
445 Swapchain::Image& img = swapchain->images[i];
446
447 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800448 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
449 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700450 if (err != 0) {
451 // TODO(jessehall): Improve error reporting. Can we enumerate
452 // possible errors and translate them to valid Vulkan result codes?
453 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700454 result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700455 break;
456 }
457 img.buffer = InitSharedPtr(device, buffer);
458 img.dequeued = true;
459
460 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -0800461 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
462 static_cast<uint32_t>(img.buffer->height),
463 1};
Jesse Halld7b994a2015-09-07 14:17:37 -0700464 image_native_buffer.handle = img.buffer->handle;
465 image_native_buffer.stride = img.buffer->stride;
466 image_native_buffer.format = img.buffer->format;
467 image_native_buffer.usage = img.buffer->usage;
468
Jesse Hall03b6fe12015-11-24 12:44:21 -0800469 result =
Jesse Hall1f91d392015-12-11 16:28:44 -0800470 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700471 if (result != VK_SUCCESS) {
472 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
473 break;
474 }
475 }
476
477 // -- Cancel all buffers, returning them to the queue --
478 // If an error occurred before, also destroy the VkImage and release the
479 // buffer reference. Otherwise, we retain a strong reference to the buffer.
480 //
481 // TODO(jessehall): The error path here is the same as DestroySwapchain,
482 // but not the non-error path. Should refactor/unify.
483 for (uint32_t i = 0; i < num_images; i++) {
484 Swapchain::Image& img = swapchain->images[i];
485 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800486 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
487 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700488 img.dequeue_fence = -1;
489 img.dequeued = false;
490 }
491 if (result != VK_SUCCESS) {
492 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -0800493 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700494 }
495 }
496
497 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700498 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800499 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700500 return result;
501 }
502
503 *swapchain_handle = HandleFromSwapchain(swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700504 return VK_SUCCESS;
505}
506
Jesse Halle1b12782015-11-30 11:27:32 -0800507VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800508void DestroySwapchainKHR_Bottom(VkDevice device,
509 VkSwapchainKHR swapchain_handle,
510 const VkAllocationCallbacks* allocator) {
511 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700512 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800513 const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window;
Jesse Halld7b994a2015-09-07 14:17:37 -0700514
515 for (uint32_t i = 0; i < swapchain->num_images; i++) {
516 Swapchain::Image& img = swapchain->images[i];
517 if (img.dequeued) {
518 window->cancelBuffer(window.get(), img.buffer.get(),
519 img.dequeue_fence);
520 img.dequeue_fence = -1;
521 img.dequeued = false;
522 }
523 if (img.image) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800524 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700525 }
526 }
527
Jesse Hall1f91d392015-12-11 16:28:44 -0800528 if (!allocator)
529 allocator = GetAllocator(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700530 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800531 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700532}
533
Jesse Halle1b12782015-11-30 11:27:32 -0800534VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800535VkResult GetSwapchainImagesKHR_Bottom(VkDevice,
536 VkSwapchainKHR swapchain_handle,
537 uint32_t* count,
538 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700539 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
540 VkResult result = VK_SUCCESS;
541 if (images) {
542 uint32_t n = swapchain.num_images;
543 if (*count < swapchain.num_images) {
544 n = *count;
545 result = VK_INCOMPLETE;
546 }
547 for (uint32_t i = 0; i < n; i++)
548 images[i] = swapchain.images[i].image;
549 }
550 *count = swapchain.num_images;
551 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700552}
553
Jesse Halle1b12782015-11-30 11:27:32 -0800554VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800555VkResult AcquireNextImageKHR_Bottom(VkDevice device,
556 VkSwapchainKHR swapchain_handle,
557 uint64_t timeout,
558 VkSemaphore semaphore,
559 VkFence vk_fence,
560 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700561 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800562 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700563 VkResult result;
564 int err;
565
566 ALOGW_IF(
567 timeout != UINT64_MAX,
568 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
569
570 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -0800571 int fence_fd;
572 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700573 if (err != 0) {
574 // TODO(jessehall): Improve error reporting. Can we enumerate possible
575 // errors and translate them to valid Vulkan result codes?
576 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700577 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700578 }
579
580 uint32_t idx;
581 for (idx = 0; idx < swapchain.num_images; idx++) {
582 if (swapchain.images[idx].buffer.get() == buffer) {
583 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -0800584 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -0700585 break;
586 }
587 }
588 if (idx == swapchain.num_images) {
589 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -0800590 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700591 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700592 }
593
594 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -0800595 if (fence_fd != -1) {
596 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700597 if (fence_clone == -1) {
598 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
599 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -0800600 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -0700601 }
602 }
603
Jesse Hall1f91d392015-12-11 16:28:44 -0800604 result = GetDriverDispatch(device).AcquireImageANDROID(
605 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700606 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800607 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
608 // even if the call fails. We could close it ourselves on failure, but
609 // that would create a race condition if the driver closes it on a
610 // failure path: some other thread might create an fd with the same
611 // number between the time the driver closes it and the time we close
612 // it. We must assume one of: the driver *always* closes it even on
613 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -0800614 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700615 swapchain.images[idx].dequeued = false;
616 swapchain.images[idx].dequeue_fence = -1;
617 return result;
618 }
619
620 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700621 return VK_SUCCESS;
622}
623
Jesse Halle1b12782015-11-30 11:27:32 -0800624VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800625VkResult QueuePresentKHR_Bottom(VkQueue queue,
626 const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700627 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
628 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
629 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -0700630 ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL");
631
Jesse Hall1f91d392015-12-11 16:28:44 -0800632 const DriverDispatchTable& dispatch = GetDriverDispatch(queue);
Jesse Halld7b994a2015-09-07 14:17:37 -0700633 VkResult final_result = VK_SUCCESS;
634 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
635 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -0800636 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800637 ANativeWindow* window = swapchain.surface.window.get();
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800638 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700639 Swapchain::Image& img = swapchain.images[image_idx];
Jesse Halld7b994a2015-09-07 14:17:37 -0700640 VkResult result;
641 int err;
642
Jesse Halld7b994a2015-09-07 14:17:37 -0700643 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -0800644 result = dispatch.QueueSignalReleaseImageANDROID(
645 queue, present_info->waitSemaphoreCount,
646 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700647 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800648 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halla9e57032015-11-30 01:03:10 -0800649 if (present_info->pResults)
650 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700651 if (final_result == VK_SUCCESS)
652 final_result = result;
653 // TODO(jessehall): What happens to the buffer here? Does the app
654 // still own it or not, i.e. should we cancel the buffer? Hard to
655 // do correctly without synchronizing, though I guess we could wait
656 // for the queue to idle.
657 continue;
658 }
659
Jesse Hall1356b0d2015-11-23 17:24:58 -0800660 err = window->queueBuffer(window, img.buffer.get(), fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700661 if (err != 0) {
662 // TODO(jessehall): What now? We should probably cancel the buffer,
663 // I guess?
664 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Halla9e57032015-11-30 01:03:10 -0800665 if (present_info->pResults)
666 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700667 if (final_result == VK_SUCCESS)
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700668 final_result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700669 continue;
670 }
671
672 if (img.dequeue_fence != -1) {
673 close(img.dequeue_fence);
674 img.dequeue_fence = -1;
675 }
676 img.dequeued = false;
Jesse Halla9e57032015-11-30 01:03:10 -0800677
678 if (present_info->pResults)
679 present_info->pResults[sc] = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -0700680 }
681
682 return final_result;
683}
Jesse Hallb1352bc2015-09-04 16:12:33 -0700684
685} // namespace vulkan