blob: 7bfae27fa33449f204f4fe878ee1aa1dccb4b79f [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 Hall1f91d392015-12-11 16:28:44 -0800151VkResult CreateAndroidSurfaceKHR_Bottom(VkInstance instance,
152 ANativeWindow* window,
153 const VkAllocationCallbacks* allocator,
154 VkSurfaceKHR* out_surface) {
155 if (!allocator)
156 allocator = GetAllocator(instance);
157 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
158 alignof(Surface),
159 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800160 if (!mem)
161 return VK_ERROR_OUT_OF_HOST_MEMORY;
162 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700163
Jesse Hall1356b0d2015-11-23 17:24:58 -0800164 surface->window = InitSharedPtr(instance, window);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700165
Jesse Hall1356b0d2015-11-23 17:24:58 -0800166 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
167 int err =
168 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
169 if (err != 0) {
170 // TODO(jessehall): Improve error reporting. Can we enumerate possible
171 // errors and translate them to valid Vulkan result codes?
172 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
173 err);
174 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800175 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800176 return VK_ERROR_INITIALIZATION_FAILED;
177 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700178
Jesse Hall1356b0d2015-11-23 17:24:58 -0800179 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700180 return VK_SUCCESS;
181}
182
Jesse Halle1b12782015-11-30 11:27:32 -0800183VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800184void DestroySurfaceKHR_Bottom(VkInstance instance,
185 VkSurfaceKHR surface_handle,
186 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800187 Surface* surface = SurfaceFromHandle(surface_handle);
188 if (!surface)
189 return;
190 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
191 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800192 if (!allocator)
193 allocator = GetAllocator(instance);
194 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800195}
196
Jesse Halle1b12782015-11-30 11:27:32 -0800197VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800198VkResult GetPhysicalDeviceSurfaceSupportKHR_Bottom(VkPhysicalDevice /*pdev*/,
199 uint32_t /*queue_family*/,
200 VkSurfaceKHR /*surface*/,
201 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800202 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800203 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800204}
205
Jesse Halle1b12782015-11-30 11:27:32 -0800206VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800207VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Bottom(
Jesse Hallb00daad2015-11-29 19:46:20 -0800208 VkPhysicalDevice /*pdev*/,
209 VkSurfaceKHR surface,
210 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700211 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800212 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700213
214 int width, height;
215 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
216 if (err != 0) {
217 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
218 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700219 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700220 }
221 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
222 if (err != 0) {
223 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
224 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700225 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700226 }
227
Jesse Hallb00daad2015-11-29 19:46:20 -0800228 capabilities->currentExtent = VkExtent2D{width, 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 Hallb00daad2015-11-29 19:46:20 -0800241 capabilities->supportedTransforms = VK_SURFACE_TRANSFORM_NONE_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700242
243 // TODO(jessehall): Implement based on NATIVE_WINDOW_TRANSFORM_HINT.
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800244 capabilities->currentTransform = VK_SURFACE_TRANSFORM_NONE_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 Halld7b994a2015-09-07 14:17:37 -0700323 ALOGV_IF(create_info->imageArraySize != 1,
324 "Swapchain imageArraySize (%u) != 1 not supported",
325 create_info->imageArraySize);
326
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 Hallf4ab2b12015-11-30 16:04:55 -0800333 ALOGE_IF(create_info->preTransform != VK_SURFACE_TRANSFORM_NONE_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 Hall1356b0d2015-11-23 17:24:58 -0800343 err = native_window_set_buffers_dimensions(surface.window.get(),
Jesse Halld7b994a2015-09-07 14:17:37 -0700344 create_info->imageExtent.width,
345 create_info->imageExtent.height);
346 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 =
461 VkExtent3D{img.buffer->width, img.buffer->height, 1};
462 image_native_buffer.handle = img.buffer->handle;
463 image_native_buffer.stride = img.buffer->stride;
464 image_native_buffer.format = img.buffer->format;
465 image_native_buffer.usage = img.buffer->usage;
466
Jesse Hall03b6fe12015-11-24 12:44:21 -0800467 result =
Jesse Hall1f91d392015-12-11 16:28:44 -0800468 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700469 if (result != VK_SUCCESS) {
470 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
471 break;
472 }
473 }
474
475 // -- Cancel all buffers, returning them to the queue --
476 // If an error occurred before, also destroy the VkImage and release the
477 // buffer reference. Otherwise, we retain a strong reference to the buffer.
478 //
479 // TODO(jessehall): The error path here is the same as DestroySwapchain,
480 // but not the non-error path. Should refactor/unify.
481 for (uint32_t i = 0; i < num_images; i++) {
482 Swapchain::Image& img = swapchain->images[i];
483 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800484 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
485 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700486 img.dequeue_fence = -1;
487 img.dequeued = false;
488 }
489 if (result != VK_SUCCESS) {
490 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -0800491 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700492 }
493 }
494
495 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700496 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800497 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700498 return result;
499 }
500
501 *swapchain_handle = HandleFromSwapchain(swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700502 return VK_SUCCESS;
503}
504
Jesse Halle1b12782015-11-30 11:27:32 -0800505VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800506void DestroySwapchainKHR_Bottom(VkDevice device,
507 VkSwapchainKHR swapchain_handle,
508 const VkAllocationCallbacks* allocator) {
509 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700510 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800511 const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window;
Jesse Halld7b994a2015-09-07 14:17:37 -0700512
513 for (uint32_t i = 0; i < swapchain->num_images; i++) {
514 Swapchain::Image& img = swapchain->images[i];
515 if (img.dequeued) {
516 window->cancelBuffer(window.get(), img.buffer.get(),
517 img.dequeue_fence);
518 img.dequeue_fence = -1;
519 img.dequeued = false;
520 }
521 if (img.image) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800522 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700523 }
524 }
525
Jesse Hall1f91d392015-12-11 16:28:44 -0800526 if (!allocator)
527 allocator = GetAllocator(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700528 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800529 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700530}
531
Jesse Halle1b12782015-11-30 11:27:32 -0800532VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800533VkResult GetSwapchainImagesKHR_Bottom(VkDevice,
534 VkSwapchainKHR swapchain_handle,
535 uint32_t* count,
536 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700537 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
538 VkResult result = VK_SUCCESS;
539 if (images) {
540 uint32_t n = swapchain.num_images;
541 if (*count < swapchain.num_images) {
542 n = *count;
543 result = VK_INCOMPLETE;
544 }
545 for (uint32_t i = 0; i < n; i++)
546 images[i] = swapchain.images[i].image;
547 }
548 *count = swapchain.num_images;
549 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700550}
551
Jesse Halle1b12782015-11-30 11:27:32 -0800552VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800553VkResult AcquireNextImageKHR_Bottom(VkDevice device,
554 VkSwapchainKHR swapchain_handle,
555 uint64_t timeout,
556 VkSemaphore semaphore,
557 VkFence vk_fence,
558 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700559 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800560 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700561 VkResult result;
562 int err;
563
564 ALOGW_IF(
565 timeout != UINT64_MAX,
566 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
567
568 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -0800569 int fence_fd;
570 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700571 if (err != 0) {
572 // TODO(jessehall): Improve error reporting. Can we enumerate possible
573 // errors and translate them to valid Vulkan result codes?
574 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700575 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700576 }
577
578 uint32_t idx;
579 for (idx = 0; idx < swapchain.num_images; idx++) {
580 if (swapchain.images[idx].buffer.get() == buffer) {
581 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -0800582 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -0700583 break;
584 }
585 }
586 if (idx == swapchain.num_images) {
587 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -0800588 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700589 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700590 }
591
592 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -0800593 if (fence_fd != -1) {
594 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700595 if (fence_clone == -1) {
596 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
597 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -0800598 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -0700599 }
600 }
601
Jesse Hall1f91d392015-12-11 16:28:44 -0800602 result = GetDriverDispatch(device).AcquireImageANDROID(
603 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700604 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800605 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
606 // even if the call fails. We could close it ourselves on failure, but
607 // that would create a race condition if the driver closes it on a
608 // failure path: some other thread might create an fd with the same
609 // number between the time the driver closes it and the time we close
610 // it. We must assume one of: the driver *always* closes it even on
611 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -0800612 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700613 swapchain.images[idx].dequeued = false;
614 swapchain.images[idx].dequeue_fence = -1;
615 return result;
616 }
617
618 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700619 return VK_SUCCESS;
620}
621
Jesse Halle1b12782015-11-30 11:27:32 -0800622VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800623VkResult QueuePresentKHR_Bottom(VkQueue queue,
624 const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700625 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
626 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
627 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -0700628 ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL");
629
Jesse Hall1f91d392015-12-11 16:28:44 -0800630 const DriverDispatchTable& dispatch = GetDriverDispatch(queue);
Jesse Halld7b994a2015-09-07 14:17:37 -0700631 VkResult final_result = VK_SUCCESS;
632 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
633 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -0800634 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800635 ANativeWindow* window = swapchain.surface.window.get();
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800636 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700637 Swapchain::Image& img = swapchain.images[image_idx];
Jesse Halld7b994a2015-09-07 14:17:37 -0700638 VkResult result;
639 int err;
640
Jesse Halld7b994a2015-09-07 14:17:37 -0700641 int fence = -1;
Jesse Hall1f91d392015-12-11 16:28:44 -0800642 result =
643 dispatch.QueueSignalReleaseImageANDROID(queue, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700644 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800645 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halla9e57032015-11-30 01:03:10 -0800646 if (present_info->pResults)
647 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700648 if (final_result == VK_SUCCESS)
649 final_result = result;
650 // TODO(jessehall): What happens to the buffer here? Does the app
651 // still own it or not, i.e. should we cancel the buffer? Hard to
652 // do correctly without synchronizing, though I guess we could wait
653 // for the queue to idle.
654 continue;
655 }
656
Jesse Hall1356b0d2015-11-23 17:24:58 -0800657 err = window->queueBuffer(window, img.buffer.get(), fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700658 if (err != 0) {
659 // TODO(jessehall): What now? We should probably cancel the buffer,
660 // I guess?
661 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
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)
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700665 final_result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700666 continue;
667 }
668
669 if (img.dequeue_fence != -1) {
670 close(img.dequeue_fence);
671 img.dequeue_fence = -1;
672 }
673 img.dequeued = false;
Jesse Halla9e57032015-11-30 01:03:10 -0800674
675 if (present_info->pResults)
676 present_info->pResults[sc] = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -0700677 }
678
679 return final_result;
680}
Jesse Hallb1352bc2015-09-04 16:12:33 -0700681
682} // namespace vulkan