blob: d8908ad7d97229b2176b04b48b40f587116286e1 [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 Hallb00daad2015-11-29 19:46:20 -0800229 capabilities->currentExtent = VkExtent2D{width, height};
Jesse Halld7b994a2015-09-07 14:17:37 -0700230
231 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800232 capabilities->minImageCount = 2;
233 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700234
235 // TODO(jessehall): Figure out what the max extent should be. Maximum
236 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800237 capabilities->minImageExtent = VkExtent2D{1, 1};
238 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700239
240 // TODO(jessehall): We can support all transforms, fix this once
241 // implemented.
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800242 capabilities->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700243
244 // TODO(jessehall): Implement based on NATIVE_WINDOW_TRANSFORM_HINT.
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800245 capabilities->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700246
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800247 capabilities->maxImageArrayLayers = 1;
Jesse Halld7b994a2015-09-07 14:17:37 -0700248
249 // TODO(jessehall): I think these are right, but haven't thought hard about
250 // it. Do we need to query the driver for support of any of these?
251 // Currently not included:
252 // - VK_IMAGE_USAGE_GENERAL: maybe? does this imply cpu mappable?
253 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
254 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800255 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800256 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
257 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
258 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700259 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
260
Jesse Hallb1352bc2015-09-04 16:12:33 -0700261 return VK_SUCCESS;
262}
263
Jesse Halle1b12782015-11-30 11:27:32 -0800264VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800265VkResult GetPhysicalDeviceSurfaceFormatsKHR_Bottom(
266 VkPhysicalDevice /*pdev*/,
267 VkSurfaceKHR /*surface*/,
268 uint32_t* count,
269 VkSurfaceFormatKHR* formats) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800270 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
271 // a new gralloc method to query whether a (format, usage) pair is
272 // supported, and check that for each gralloc format that corresponds to a
273 // Vulkan format. Shorter term, just add a few more formats to the ones
274 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700275
276 const VkSurfaceFormatKHR kFormats[] = {
277 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
278 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
279 };
280 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
281
282 VkResult result = VK_SUCCESS;
283 if (formats) {
284 if (*count < kNumFormats)
285 result = VK_INCOMPLETE;
286 std::copy(kFormats, kFormats + std::min(*count, kNumFormats), formats);
287 }
288 *count = kNumFormats;
289 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700290}
291
Jesse Halle1b12782015-11-30 11:27:32 -0800292VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800293VkResult GetPhysicalDeviceSurfacePresentModesKHR_Bottom(
294 VkPhysicalDevice /*pdev*/,
295 VkSurfaceKHR /*surface*/,
296 uint32_t* count,
297 VkPresentModeKHR* modes) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700298 const VkPresentModeKHR kModes[] = {
299 VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
300 };
301 const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]);
302
303 VkResult result = VK_SUCCESS;
304 if (modes) {
305 if (*count < kNumModes)
306 result = VK_INCOMPLETE;
307 std::copy(kModes, kModes + std::min(*count, kNumModes), modes);
308 }
309 *count = kNumModes;
310 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700311}
312
Jesse Halle1b12782015-11-30 11:27:32 -0800313VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800314VkResult CreateSwapchainKHR_Bottom(VkDevice device,
315 const VkSwapchainCreateInfoKHR* create_info,
316 const VkAllocationCallbacks* allocator,
317 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700318 int err;
319 VkResult result = VK_SUCCESS;
320
Jesse Hall1f91d392015-12-11 16:28:44 -0800321 if (!allocator)
322 allocator = GetAllocator(device);
323
Jesse Halld7b994a2015-09-07 14:17:37 -0700324 ALOGV_IF(create_info->imageArraySize != 1,
325 "Swapchain imageArraySize (%u) != 1 not supported",
326 create_info->imageArraySize);
327
328 ALOGE_IF(create_info->imageFormat != VK_FORMAT_R8G8B8A8_UNORM,
329 "swapchain formats other than R8G8B8A8_UNORM not yet implemented");
330 ALOGE_IF(create_info->imageColorSpace != VK_COLORSPACE_SRGB_NONLINEAR_KHR,
331 "color spaces other than SRGB_NONLINEAR not yet implemented");
332 ALOGE_IF(create_info->oldSwapchain,
333 "swapchain re-creation not yet implemented");
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800334 ALOGE_IF(create_info->preTransform != VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
Jesse Halld7b994a2015-09-07 14:17:37 -0700335 "swapchain preTransform not yet implemented");
336 ALOGE_IF(create_info->presentMode != VK_PRESENT_MODE_FIFO_KHR,
337 "present modes other than FIFO are not yet implemented");
338
339 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700340
Jesse Hall1356b0d2015-11-23 17:24:58 -0800341 Surface& surface = *SurfaceFromHandle(create_info->surface);
Jesse Hall1f91d392015-12-11 16:28:44 -0800342 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Hall70f93352015-11-04 09:41:31 -0800343
Jesse Hall1356b0d2015-11-23 17:24:58 -0800344 err = native_window_set_buffers_dimensions(surface.window.get(),
Jesse Halld7b994a2015-09-07 14:17:37 -0700345 create_info->imageExtent.width,
346 create_info->imageExtent.height);
347 if (err != 0) {
348 // TODO(jessehall): Improve error reporting. Can we enumerate possible
349 // errors and translate them to valid Vulkan result codes?
350 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
351 create_info->imageExtent.width, create_info->imageExtent.height,
352 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700353 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700354 }
355
Jesse Hallf64ca122015-11-03 16:11:10 -0800356 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800357 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800358 if (err != 0) {
359 // TODO(jessehall): Improve error reporting. Can we enumerate possible
360 // errors and translate them to valid Vulkan result codes?
361 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
362 strerror(-err), err);
Jesse Hallf64ca122015-11-03 16:11:10 -0800363 return VK_ERROR_INITIALIZATION_FAILED;
364 }
365
Jesse Halld7b994a2015-09-07 14:17:37 -0700366 uint32_t min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800367 err = surface.window->query(
368 surface.window.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
369 reinterpret_cast<int*>(&min_undequeued_buffers));
Jesse Halld7b994a2015-09-07 14:17:37 -0700370 if (err != 0) {
371 // TODO(jessehall): Improve error reporting. Can we enumerate possible
372 // errors and translate them to valid Vulkan result codes?
373 ALOGE("window->query failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700374 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700375 }
376 uint32_t num_images =
377 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800378 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700379 if (err != 0) {
380 // TODO(jessehall): Improve error reporting. Can we enumerate possible
381 // errors and translate them to valid Vulkan result codes?
382 ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
383 err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700384 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700385 }
386
Jesse Hall70f93352015-11-04 09:41:31 -0800387 int gralloc_usage = 0;
388 // TODO(jessehall): Remove conditional once all drivers have been updated
Jesse Hall1f91d392015-12-11 16:28:44 -0800389 if (dispatch.GetSwapchainGrallocUsageANDROID) {
390 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800391 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -0800392 &gralloc_usage);
393 if (result != VK_SUCCESS) {
394 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Jesse Hall70f93352015-11-04 09:41:31 -0800395 return VK_ERROR_INITIALIZATION_FAILED;
396 }
397 } else {
398 gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
399 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800400 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -0800401 if (err != 0) {
402 // TODO(jessehall): Improve error reporting. Can we enumerate possible
403 // errors and translate them to valid Vulkan result codes?
404 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Jesse Hall70f93352015-11-04 09:41:31 -0800405 return VK_ERROR_INITIALIZATION_FAILED;
406 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700407
408 // -- Allocate our Swapchain object --
409 // After this point, we must deallocate the swapchain on error.
410
Jesse Hall1f91d392015-12-11 16:28:44 -0800411 void* mem = allocator->pfnAllocation(allocator->pUserData,
412 sizeof(Swapchain), alignof(Swapchain),
413 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800414 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -0700415 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800416 Swapchain* swapchain = new (mem) Swapchain(surface, num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700417
418 // -- Dequeue all buffers and create a VkImage for each --
419 // Any failures during or after this must cancel the dequeued buffers.
420
421 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -0700422#pragma clang diagnostic push
423#pragma clang diagnostic ignored "-Wold-style-cast"
424 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
425#pragma clang diagnostic pop
426 .pNext = nullptr,
427 };
428 VkImageCreateInfo image_create = {
429 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
430 .pNext = &image_native_buffer,
431 .imageType = VK_IMAGE_TYPE_2D,
432 .format = VK_FORMAT_R8G8B8A8_UNORM, // TODO(jessehall)
433 .extent = {0, 0, 1},
434 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -0800435 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -0800436 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -0700437 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800438 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -0700439 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800440 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800441 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -0700442 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
443 };
444
Jesse Halld7b994a2015-09-07 14:17:37 -0700445 for (uint32_t i = 0; i < num_images; i++) {
446 Swapchain::Image& img = swapchain->images[i];
447
448 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800449 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
450 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700451 if (err != 0) {
452 // TODO(jessehall): Improve error reporting. Can we enumerate
453 // possible errors and translate them to valid Vulkan result codes?
454 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700455 result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700456 break;
457 }
458 img.buffer = InitSharedPtr(device, buffer);
459 img.dequeued = true;
460
461 image_create.extent =
462 VkExtent3D{img.buffer->width, img.buffer->height, 1};
463 image_native_buffer.handle = img.buffer->handle;
464 image_native_buffer.stride = img.buffer->stride;
465 image_native_buffer.format = img.buffer->format;
466 image_native_buffer.usage = img.buffer->usage;
467
Jesse Hall03b6fe12015-11-24 12:44:21 -0800468 result =
Jesse Hall1f91d392015-12-11 16:28:44 -0800469 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700470 if (result != VK_SUCCESS) {
471 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
472 break;
473 }
474 }
475
476 // -- Cancel all buffers, returning them to the queue --
477 // If an error occurred before, also destroy the VkImage and release the
478 // buffer reference. Otherwise, we retain a strong reference to the buffer.
479 //
480 // TODO(jessehall): The error path here is the same as DestroySwapchain,
481 // but not the non-error path. Should refactor/unify.
482 for (uint32_t i = 0; i < num_images; i++) {
483 Swapchain::Image& img = swapchain->images[i];
484 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800485 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
486 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700487 img.dequeue_fence = -1;
488 img.dequeued = false;
489 }
490 if (result != VK_SUCCESS) {
491 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -0800492 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700493 }
494 }
495
496 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700497 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800498 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700499 return result;
500 }
501
502 *swapchain_handle = HandleFromSwapchain(swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700503 return VK_SUCCESS;
504}
505
Jesse Halle1b12782015-11-30 11:27:32 -0800506VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800507void DestroySwapchainKHR_Bottom(VkDevice device,
508 VkSwapchainKHR swapchain_handle,
509 const VkAllocationCallbacks* allocator) {
510 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700511 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800512 const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window;
Jesse Halld7b994a2015-09-07 14:17:37 -0700513
514 for (uint32_t i = 0; i < swapchain->num_images; i++) {
515 Swapchain::Image& img = swapchain->images[i];
516 if (img.dequeued) {
517 window->cancelBuffer(window.get(), img.buffer.get(),
518 img.dequeue_fence);
519 img.dequeue_fence = -1;
520 img.dequeued = false;
521 }
522 if (img.image) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800523 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700524 }
525 }
526
Jesse Hall1f91d392015-12-11 16:28:44 -0800527 if (!allocator)
528 allocator = GetAllocator(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700529 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800530 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700531}
532
Jesse Halle1b12782015-11-30 11:27:32 -0800533VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800534VkResult GetSwapchainImagesKHR_Bottom(VkDevice,
535 VkSwapchainKHR swapchain_handle,
536 uint32_t* count,
537 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700538 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
539 VkResult result = VK_SUCCESS;
540 if (images) {
541 uint32_t n = swapchain.num_images;
542 if (*count < swapchain.num_images) {
543 n = *count;
544 result = VK_INCOMPLETE;
545 }
546 for (uint32_t i = 0; i < n; i++)
547 images[i] = swapchain.images[i].image;
548 }
549 *count = swapchain.num_images;
550 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700551}
552
Jesse Halle1b12782015-11-30 11:27:32 -0800553VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800554VkResult AcquireNextImageKHR_Bottom(VkDevice device,
555 VkSwapchainKHR swapchain_handle,
556 uint64_t timeout,
557 VkSemaphore semaphore,
558 VkFence vk_fence,
559 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700560 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800561 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700562 VkResult result;
563 int err;
564
565 ALOGW_IF(
566 timeout != UINT64_MAX,
567 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
568
569 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -0800570 int fence_fd;
571 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700572 if (err != 0) {
573 // TODO(jessehall): Improve error reporting. Can we enumerate possible
574 // errors and translate them to valid Vulkan result codes?
575 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700576 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700577 }
578
579 uint32_t idx;
580 for (idx = 0; idx < swapchain.num_images; idx++) {
581 if (swapchain.images[idx].buffer.get() == buffer) {
582 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -0800583 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -0700584 break;
585 }
586 }
587 if (idx == swapchain.num_images) {
588 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -0800589 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700590 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700591 }
592
593 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -0800594 if (fence_fd != -1) {
595 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700596 if (fence_clone == -1) {
597 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
598 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -0800599 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -0700600 }
601 }
602
Jesse Hall1f91d392015-12-11 16:28:44 -0800603 result = GetDriverDispatch(device).AcquireImageANDROID(
604 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700605 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800606 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
607 // even if the call fails. We could close it ourselves on failure, but
608 // that would create a race condition if the driver closes it on a
609 // failure path: some other thread might create an fd with the same
610 // number between the time the driver closes it and the time we close
611 // it. We must assume one of: the driver *always* closes it even on
612 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -0800613 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700614 swapchain.images[idx].dequeued = false;
615 swapchain.images[idx].dequeue_fence = -1;
616 return result;
617 }
618
619 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700620 return VK_SUCCESS;
621}
622
Jesse Halle1b12782015-11-30 11:27:32 -0800623VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800624VkResult QueuePresentKHR_Bottom(VkQueue queue,
625 const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700626 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
627 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
628 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -0700629 ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL");
630
Jesse Hall1f91d392015-12-11 16:28:44 -0800631 const DriverDispatchTable& dispatch = GetDriverDispatch(queue);
Jesse Halld7b994a2015-09-07 14:17:37 -0700632 VkResult final_result = VK_SUCCESS;
633 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
634 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -0800635 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800636 ANativeWindow* window = swapchain.surface.window.get();
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800637 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700638 Swapchain::Image& img = swapchain.images[image_idx];
Jesse Halld7b994a2015-09-07 14:17:37 -0700639 VkResult result;
640 int err;
641
Jesse Halld7b994a2015-09-07 14:17:37 -0700642 int fence = -1;
Jesse Hall1f91d392015-12-11 16:28:44 -0800643 result =
644 dispatch.QueueSignalReleaseImageANDROID(queue, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700645 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800646 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halla9e57032015-11-30 01:03:10 -0800647 if (present_info->pResults)
648 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700649 if (final_result == VK_SUCCESS)
650 final_result = result;
651 // TODO(jessehall): What happens to the buffer here? Does the app
652 // still own it or not, i.e. should we cancel the buffer? Hard to
653 // do correctly without synchronizing, though I guess we could wait
654 // for the queue to idle.
655 continue;
656 }
657
Jesse Hall1356b0d2015-11-23 17:24:58 -0800658 err = window->queueBuffer(window, img.buffer.get(), fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700659 if (err != 0) {
660 // TODO(jessehall): What now? We should probably cancel the buffer,
661 // I guess?
662 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Halla9e57032015-11-30 01:03:10 -0800663 if (present_info->pResults)
664 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700665 if (final_result == VK_SUCCESS)
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700666 final_result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700667 continue;
668 }
669
670 if (img.dequeue_fence != -1) {
671 close(img.dequeue_fence);
672 img.dequeue_fence = -1;
673 }
674 img.dequeued = false;
Jesse Halla9e57032015-11-30 01:03:10 -0800675
676 if (present_info->pResults)
677 present_info->pResults[sc] = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -0700678 }
679
680 return final_result;
681}
Jesse Hallb1352bc2015-09-04 16:12:33 -0700682
683} // namespace vulkan