blob: f1fe236bdc9f222c5b5dbe93e970c9a2bfa74b35 [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
40// allocation of the shared_ptr<> control structure to VkAllocCallbacks. The
41// platform holds a reference to the ANativeWindow using its embedded reference
42// count, and the ANativeWindow implementation holds references to the
43// ANativeWindowBuffers using their embedded reference counts, so the
44// shared_ptr *must* cooperate with these and hold at least one reference to
45// the object using the embedded reference count.
46
47template <typename T>
48struct NativeBaseDeleter {
49 void operator()(T* obj) { obj->common.decRef(&obj->common); }
50};
51
Jesse Hall03b6fe12015-11-24 12:44:21 -080052template <typename Host>
53struct AllocScope {};
54
55template <>
56struct AllocScope<VkInstance> {
57 static const VkSystemAllocScope kScope = VK_SYSTEM_ALLOC_SCOPE_INSTANCE;
58};
59
60template <>
61struct AllocScope<VkDevice> {
62 static const VkSystemAllocScope kScope = VK_SYSTEM_ALLOC_SCOPE_DEVICE;
63};
64
Jesse Hall1356b0d2015-11-23 17:24:58 -080065template <typename T, typename Host>
Jesse Halld7b994a2015-09-07 14:17:37 -070066class VulkanAllocator {
67 public:
68 typedef T value_type;
69
Jesse Hall1356b0d2015-11-23 17:24:58 -080070 explicit VulkanAllocator(Host host) : host_(host) {}
Jesse Halld7b994a2015-09-07 14:17:37 -070071
72 template <typename U>
Jesse Hall1356b0d2015-11-23 17:24:58 -080073 explicit VulkanAllocator(const VulkanAllocator<U, Host>& other)
74 : host_(other.host_) {}
Jesse Halld7b994a2015-09-07 14:17:37 -070075
76 T* allocate(size_t n) const {
Jesse Hall1356b0d2015-11-23 17:24:58 -080077 return static_cast<T*>(AllocMem(host_, n * sizeof(T), alignof(T),
Jesse Hall03b6fe12015-11-24 12:44:21 -080078 AllocScope<Host>::kScope));
Jesse Halld7b994a2015-09-07 14:17:37 -070079 }
Jesse Hall1356b0d2015-11-23 17:24:58 -080080 void deallocate(T* p, size_t) const { return FreeMem(host_, p); }
Jesse Halld7b994a2015-09-07 14:17:37 -070081
82 private:
Jesse Hall1356b0d2015-11-23 17:24:58 -080083 template <typename U, typename H>
Jesse Halld7b994a2015-09-07 14:17:37 -070084 friend class VulkanAllocator;
Jesse Hall1356b0d2015-11-23 17:24:58 -080085 Host host_;
Jesse Halld7b994a2015-09-07 14:17:37 -070086};
87
Jesse Hall1356b0d2015-11-23 17:24:58 -080088template <typename T, typename Host>
89std::shared_ptr<T> InitSharedPtr(Host host, T* obj) {
Jesse Halld7b994a2015-09-07 14:17:37 -070090 obj->common.incRef(&obj->common);
91 return std::shared_ptr<T>(obj, NativeBaseDeleter<T>(),
Jesse Hall1356b0d2015-11-23 17:24:58 -080092 VulkanAllocator<T, Host>(host));
Jesse Halld7b994a2015-09-07 14:17:37 -070093}
94
95// ----------------------------------------------------------------------------
96
Jesse Hall1356b0d2015-11-23 17:24:58 -080097struct Surface {
Jesse Halld7b994a2015-09-07 14:17:37 -070098 std::shared_ptr<ANativeWindow> window;
Jesse Hall1356b0d2015-11-23 17:24:58 -080099};
100
101VkSurfaceKHR HandleFromSurface(Surface* surface) {
102 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
103}
104
105Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800106 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800107}
108
109struct Swapchain {
110 Swapchain(Surface& surface_, uint32_t num_images_)
111 : surface(surface_), num_images(num_images_) {}
112
113 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700114 uint32_t num_images;
115
116 struct Image {
117 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
118 VkImage image;
119 std::shared_ptr<ANativeWindowBuffer> buffer;
120 // The fence is only valid when the buffer is dequeued, and should be
121 // -1 any other time. When valid, we own the fd, and must ensure it is
122 // closed: either by closing it explicitly when queueing the buffer,
123 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
124 int dequeue_fence;
125 bool dequeued;
126 } images[android::BufferQueue::NUM_BUFFER_SLOTS];
127};
128
129VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
130 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
131}
132
133Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800134 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700135}
136
137} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700138
139namespace vulkan {
140
Jesse Hall1356b0d2015-11-23 17:24:58 -0800141VkResult CreateAndroidSurfaceKHR(VkInstance instance,
142 ANativeWindow* window,
143 VkSurfaceKHR* out_surface) {
144 void* mem = AllocMem(instance, sizeof(Surface), alignof(Surface),
Jesse Hall03b6fe12015-11-24 12:44:21 -0800145 VK_SYSTEM_ALLOC_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800146 if (!mem)
147 return VK_ERROR_OUT_OF_HOST_MEMORY;
148 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700149
Jesse Hall1356b0d2015-11-23 17:24:58 -0800150 surface->window = InitSharedPtr(instance, window);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700151
Jesse Hall1356b0d2015-11-23 17:24:58 -0800152 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
153 int err =
154 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
155 if (err != 0) {
156 // TODO(jessehall): Improve error reporting. Can we enumerate possible
157 // errors and translate them to valid Vulkan result codes?
158 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
159 err);
160 surface->~Surface();
161 FreeMem(instance, surface);
162 return VK_ERROR_INITIALIZATION_FAILED;
163 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700164
Jesse Hall1356b0d2015-11-23 17:24:58 -0800165 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700166 return VK_SUCCESS;
167}
168
Jesse Hall1356b0d2015-11-23 17:24:58 -0800169void DestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface_handle) {
170 Surface* surface = SurfaceFromHandle(surface_handle);
171 if (!surface)
172 return;
173 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
174 surface->~Surface();
175 FreeMem(instance, surface);
176}
177
Jesse Halla6429252015-11-29 18:59:42 -0800178VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
Jesse Hall1356b0d2015-11-23 17:24:58 -0800179 uint32_t /*queue_family*/,
Jesse Hallb00daad2015-11-29 19:46:20 -0800180 VkSurfaceKHR /*surface*/,
181 VkBool32* pSupported) {
182 *pSupported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800183 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800184}
185
Jesse Hallb00daad2015-11-29 19:46:20 -0800186VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
187 VkPhysicalDevice /*pdev*/,
188 VkSurfaceKHR surface,
189 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700190 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800191 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700192
193 int width, height;
194 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
195 if (err != 0) {
196 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
197 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700198 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700199 }
200 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
201 if (err != 0) {
202 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
203 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700204 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700205 }
206
Jesse Hallb00daad2015-11-29 19:46:20 -0800207 capabilities->currentExtent = VkExtent2D{width, height};
Jesse Halld7b994a2015-09-07 14:17:37 -0700208
209 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800210 capabilities->minImageCount = 2;
211 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700212
213 // TODO(jessehall): Figure out what the max extent should be. Maximum
214 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800215 capabilities->minImageExtent = VkExtent2D{1, 1};
216 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700217
218 // TODO(jessehall): We can support all transforms, fix this once
219 // implemented.
Jesse Hallb00daad2015-11-29 19:46:20 -0800220 capabilities->supportedTransforms = VK_SURFACE_TRANSFORM_NONE_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700221
222 // TODO(jessehall): Implement based on NATIVE_WINDOW_TRANSFORM_HINT.
Jesse Hallb00daad2015-11-29 19:46:20 -0800223 capabilities->currentTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700224
Jesse Hallb00daad2015-11-29 19:46:20 -0800225 capabilities->maxImageArraySize = 1;
Jesse Halld7b994a2015-09-07 14:17:37 -0700226
227 // TODO(jessehall): I think these are right, but haven't thought hard about
228 // it. Do we need to query the driver for support of any of these?
229 // Currently not included:
230 // - VK_IMAGE_USAGE_GENERAL: maybe? does this imply cpu mappable?
231 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
232 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800233 capabilities->supportedUsageFlags =
Jesse Halld7b994a2015-09-07 14:17:37 -0700234 VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT |
235 VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
236 VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
237 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
238
Jesse Hallb1352bc2015-09-04 16:12:33 -0700239 return VK_SUCCESS;
240}
241
Jesse Hallb00daad2015-11-29 19:46:20 -0800242VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice /*pdev*/,
243 VkSurfaceKHR /*surface*/,
244 uint32_t* count,
245 VkSurfaceFormatKHR* formats) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800246 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
247 // a new gralloc method to query whether a (format, usage) pair is
248 // supported, and check that for each gralloc format that corresponds to a
249 // Vulkan format. Shorter term, just add a few more formats to the ones
250 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700251
252 const VkSurfaceFormatKHR kFormats[] = {
253 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
254 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
255 };
256 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
257
258 VkResult result = VK_SUCCESS;
259 if (formats) {
260 if (*count < kNumFormats)
261 result = VK_INCOMPLETE;
262 std::copy(kFormats, kFormats + std::min(*count, kNumFormats), formats);
263 }
264 *count = kNumFormats;
265 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700266}
267
Jesse Hallb00daad2015-11-29 19:46:20 -0800268VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice /*pdev*/,
269 VkSurfaceKHR /*surface*/,
270 uint32_t* count,
271 VkPresentModeKHR* modes) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700272 const VkPresentModeKHR kModes[] = {
273 VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
274 };
275 const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]);
276
277 VkResult result = VK_SUCCESS;
278 if (modes) {
279 if (*count < kNumModes)
280 result = VK_INCOMPLETE;
281 std::copy(kModes, kModes + std::min(*count, kNumModes), modes);
282 }
283 *count = kNumModes;
284 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700285}
286
287VkResult CreateSwapchainKHR(VkDevice device,
288 const VkSwapchainCreateInfoKHR* create_info,
Jesse Halld7b994a2015-09-07 14:17:37 -0700289 VkSwapchainKHR* swapchain_handle) {
290 int err;
291 VkResult result = VK_SUCCESS;
292
293 ALOGV_IF(create_info->imageArraySize != 1,
294 "Swapchain imageArraySize (%u) != 1 not supported",
295 create_info->imageArraySize);
296
297 ALOGE_IF(create_info->imageFormat != VK_FORMAT_R8G8B8A8_UNORM,
298 "swapchain formats other than R8G8B8A8_UNORM not yet implemented");
299 ALOGE_IF(create_info->imageColorSpace != VK_COLORSPACE_SRGB_NONLINEAR_KHR,
300 "color spaces other than SRGB_NONLINEAR not yet implemented");
301 ALOGE_IF(create_info->oldSwapchain,
302 "swapchain re-creation not yet implemented");
303 ALOGE_IF(create_info->preTransform != VK_SURFACE_TRANSFORM_NONE_KHR,
304 "swapchain preTransform not yet implemented");
305 ALOGE_IF(create_info->presentMode != VK_PRESENT_MODE_FIFO_KHR,
306 "present modes other than FIFO are not yet implemented");
307
308 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700309
Jesse Hall1356b0d2015-11-23 17:24:58 -0800310 Surface& surface = *SurfaceFromHandle(create_info->surface);
Jesse Hall70f93352015-11-04 09:41:31 -0800311 const DeviceVtbl& driver_vtbl = GetDriverVtbl(device);
312
Jesse Hall1356b0d2015-11-23 17:24:58 -0800313 err = native_window_set_buffers_dimensions(surface.window.get(),
Jesse Halld7b994a2015-09-07 14:17:37 -0700314 create_info->imageExtent.width,
315 create_info->imageExtent.height);
316 if (err != 0) {
317 // TODO(jessehall): Improve error reporting. Can we enumerate possible
318 // errors and translate them to valid Vulkan result codes?
319 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
320 create_info->imageExtent.width, create_info->imageExtent.height,
321 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700322 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700323 }
324
Jesse Hallf64ca122015-11-03 16:11:10 -0800325 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800326 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800327 if (err != 0) {
328 // TODO(jessehall): Improve error reporting. Can we enumerate possible
329 // errors and translate them to valid Vulkan result codes?
330 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
331 strerror(-err), err);
Jesse Hallf64ca122015-11-03 16:11:10 -0800332 return VK_ERROR_INITIALIZATION_FAILED;
333 }
334
Jesse Halld7b994a2015-09-07 14:17:37 -0700335 uint32_t min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800336 err = surface.window->query(
337 surface.window.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
338 reinterpret_cast<int*>(&min_undequeued_buffers));
Jesse Halld7b994a2015-09-07 14:17:37 -0700339 if (err != 0) {
340 // TODO(jessehall): Improve error reporting. Can we enumerate possible
341 // errors and translate them to valid Vulkan result codes?
342 ALOGE("window->query failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700343 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700344 }
345 uint32_t num_images =
346 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800347 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700348 if (err != 0) {
349 // TODO(jessehall): Improve error reporting. Can we enumerate possible
350 // errors and translate them to valid Vulkan result codes?
351 ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
352 err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700353 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700354 }
355
Jesse Hall70f93352015-11-04 09:41:31 -0800356 int gralloc_usage = 0;
357 // TODO(jessehall): Remove conditional once all drivers have been updated
358 if (driver_vtbl.GetSwapchainGrallocUsageANDROID) {
359 result = driver_vtbl.GetSwapchainGrallocUsageANDROID(
360 device, create_info->imageFormat, create_info->imageUsageFlags,
361 &gralloc_usage);
362 if (result != VK_SUCCESS) {
363 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Jesse Hall70f93352015-11-04 09:41:31 -0800364 return VK_ERROR_INITIALIZATION_FAILED;
365 }
366 } else {
367 gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
368 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800369 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -0800370 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("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Jesse Hall70f93352015-11-04 09:41:31 -0800374 return VK_ERROR_INITIALIZATION_FAILED;
375 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700376
377 // -- Allocate our Swapchain object --
378 // After this point, we must deallocate the swapchain on error.
379
Jesse Hall1356b0d2015-11-23 17:24:58 -0800380 void* mem = AllocMem(device, sizeof(Swapchain), alignof(Swapchain),
Jesse Hall03b6fe12015-11-24 12:44:21 -0800381 VK_SYSTEM_ALLOC_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800382 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -0700383 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800384 Swapchain* swapchain = new (mem) Swapchain(surface, num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700385
386 // -- Dequeue all buffers and create a VkImage for each --
387 // Any failures during or after this must cancel the dequeued buffers.
388
389 VkNativeBufferANDROID image_native_buffer = {
390// TODO(jessehall): Figure out how to make extension headers not horrible.
391#pragma clang diagnostic push
392#pragma clang diagnostic ignored "-Wold-style-cast"
393 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
394#pragma clang diagnostic pop
395 .pNext = nullptr,
396 };
397 VkImageCreateInfo image_create = {
398 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
399 .pNext = &image_native_buffer,
400 .imageType = VK_IMAGE_TYPE_2D,
401 .format = VK_FORMAT_R8G8B8A8_UNORM, // TODO(jessehall)
402 .extent = {0, 0, 1},
403 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -0800404 .arrayLayers = 1,
Jesse Halld7b994a2015-09-07 14:17:37 -0700405 .samples = 1,
406 .tiling = VK_IMAGE_TILING_OPTIMAL,
407 .usage = create_info->imageUsageFlags,
408 .flags = 0,
409 .sharingMode = create_info->sharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800410 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -0700411 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
412 };
413
Jesse Halld7b994a2015-09-07 14:17:37 -0700414 for (uint32_t i = 0; i < num_images; i++) {
415 Swapchain::Image& img = swapchain->images[i];
416
417 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800418 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
419 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700420 if (err != 0) {
421 // TODO(jessehall): Improve error reporting. Can we enumerate
422 // possible errors and translate them to valid Vulkan result codes?
423 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700424 result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700425 break;
426 }
427 img.buffer = InitSharedPtr(device, buffer);
428 img.dequeued = true;
429
430 image_create.extent =
431 VkExtent3D{img.buffer->width, img.buffer->height, 1};
432 image_native_buffer.handle = img.buffer->handle;
433 image_native_buffer.stride = img.buffer->stride;
434 image_native_buffer.format = img.buffer->format;
435 image_native_buffer.usage = img.buffer->usage;
436
Jesse Hall03b6fe12015-11-24 12:44:21 -0800437 result =
438 driver_vtbl.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700439 if (result != VK_SUCCESS) {
440 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
441 break;
442 }
443 }
444
445 // -- Cancel all buffers, returning them to the queue --
446 // If an error occurred before, also destroy the VkImage and release the
447 // buffer reference. Otherwise, we retain a strong reference to the buffer.
448 //
449 // TODO(jessehall): The error path here is the same as DestroySwapchain,
450 // but not the non-error path. Should refactor/unify.
451 for (uint32_t i = 0; i < num_images; i++) {
452 Swapchain::Image& img = swapchain->images[i];
453 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800454 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
455 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700456 img.dequeue_fence = -1;
457 img.dequeued = false;
458 }
459 if (result != VK_SUCCESS) {
460 if (img.image)
Jesse Hall03b6fe12015-11-24 12:44:21 -0800461 driver_vtbl.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700462 }
463 }
464
465 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700466 swapchain->~Swapchain();
Jesse Hall1356b0d2015-11-23 17:24:58 -0800467 FreeMem(device, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700468 return result;
469 }
470
471 *swapchain_handle = HandleFromSwapchain(swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700472 return VK_SUCCESS;
473}
474
Jesse Halld7b994a2015-09-07 14:17:37 -0700475VkResult DestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain_handle) {
476 const DeviceVtbl& driver_vtbl = GetDriverVtbl(device);
477 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800478 const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window;
Jesse Halld7b994a2015-09-07 14:17:37 -0700479
480 for (uint32_t i = 0; i < swapchain->num_images; i++) {
481 Swapchain::Image& img = swapchain->images[i];
482 if (img.dequeued) {
483 window->cancelBuffer(window.get(), img.buffer.get(),
484 img.dequeue_fence);
485 img.dequeue_fence = -1;
486 img.dequeued = false;
487 }
488 if (img.image) {
Jesse Hall03b6fe12015-11-24 12:44:21 -0800489 driver_vtbl.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700490 }
491 }
492
Jesse Halld7b994a2015-09-07 14:17:37 -0700493 swapchain->~Swapchain();
Jesse Hall1356b0d2015-11-23 17:24:58 -0800494 FreeMem(device, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700495
Jesse Hallb1352bc2015-09-04 16:12:33 -0700496 return VK_SUCCESS;
497}
498
Jesse Halld7b994a2015-09-07 14:17:37 -0700499VkResult GetSwapchainImagesKHR(VkDevice,
500 VkSwapchainKHR swapchain_handle,
Jesse Hallb1352bc2015-09-04 16:12:33 -0700501 uint32_t* count,
Jesse Halld7b994a2015-09-07 14:17:37 -0700502 VkImage* images) {
503 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
504 VkResult result = VK_SUCCESS;
505 if (images) {
506 uint32_t n = swapchain.num_images;
507 if (*count < swapchain.num_images) {
508 n = *count;
509 result = VK_INCOMPLETE;
510 }
511 for (uint32_t i = 0; i < n; i++)
512 images[i] = swapchain.images[i].image;
513 }
514 *count = swapchain.num_images;
515 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700516}
517
518VkResult AcquireNextImageKHR(VkDevice device,
Jesse Halld7b994a2015-09-07 14:17:37 -0700519 VkSwapchainKHR swapchain_handle,
Jesse Hallb1352bc2015-09-04 16:12:33 -0700520 uint64_t timeout,
521 VkSemaphore semaphore,
522 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700523 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800524 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700525 VkResult result;
526 int err;
527
528 ALOGW_IF(
529 timeout != UINT64_MAX,
530 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
531
532 ANativeWindowBuffer* buffer;
533 int fence;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800534 err = window->dequeueBuffer(window, &buffer, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700535 if (err != 0) {
536 // TODO(jessehall): Improve error reporting. Can we enumerate possible
537 // errors and translate them to valid Vulkan result codes?
538 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700539 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700540 }
541
542 uint32_t idx;
543 for (idx = 0; idx < swapchain.num_images; idx++) {
544 if (swapchain.images[idx].buffer.get() == buffer) {
545 swapchain.images[idx].dequeued = true;
546 swapchain.images[idx].dequeue_fence = fence;
547 break;
548 }
549 }
550 if (idx == swapchain.num_images) {
551 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall1356b0d2015-11-23 17:24:58 -0800552 window->cancelBuffer(window, buffer, fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700553#pragma clang diagnostic push
554#pragma clang diagnostic ignored "-Wold-style-cast"
555 return VK_ERROR_OUT_OF_DATE_KHR;
556#pragma clang diagnostic pop
557 }
558
559 int fence_clone = -1;
560 if (fence != -1) {
561 fence_clone = dup(fence);
562 if (fence_clone == -1) {
563 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
564 strerror(errno), errno);
565 sync_wait(fence, -1 /* forever */);
566 }
567 }
568
569 const DeviceVtbl& driver_vtbl = GetDriverVtbl(device);
Jesse Hallab9aeef2015-11-04 10:56:20 -0800570 if (driver_vtbl.AcquireImageANDROID) {
571 result = driver_vtbl.AcquireImageANDROID(
572 device, swapchain.images[idx].image, fence_clone, semaphore);
573 } else {
574 ALOG_ASSERT(driver_vtbl.ImportNativeFenceANDROID,
575 "Have neither vkAcquireImageANDROID nor "
576 "vkImportNativeFenceANDROID");
577 result = driver_vtbl.ImportNativeFenceANDROID(device, semaphore,
578 fence_clone);
579 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700580 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800581 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
582 // even if the call fails. We could close it ourselves on failure, but
583 // that would create a race condition if the driver closes it on a
584 // failure path: some other thread might create an fd with the same
585 // number between the time the driver closes it and the time we close
586 // it. We must assume one of: the driver *always* closes it even on
587 // failure, or *never* closes it on failure.
Jesse Hall1356b0d2015-11-23 17:24:58 -0800588 window->cancelBuffer(window, buffer, fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700589 swapchain.images[idx].dequeued = false;
590 swapchain.images[idx].dequeue_fence = -1;
591 return result;
592 }
593
594 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700595 return VK_SUCCESS;
596}
597
598VkResult QueuePresentKHR(VkQueue queue, VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700599#pragma clang diagnostic push
600#pragma clang diagnostic ignored "-Wold-style-cast"
601#pragma clang diagnostic ignored "-Wsign-conversion"
602 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
603 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
604 present_info->sType);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700605#pragma clang diagnostic pop
Jesse Halld7b994a2015-09-07 14:17:37 -0700606 ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL");
607
608 const DeviceVtbl& driver_vtbl = GetDriverVtbl(queue);
609 VkResult final_result = VK_SUCCESS;
610 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
611 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -0800612 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800613 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700614 uint32_t image_idx = present_info->imageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700615 Swapchain::Image& img = swapchain.images[image_idx];
Jesse Halld7b994a2015-09-07 14:17:37 -0700616 VkResult result;
617 int err;
618
Jesse Halld7b994a2015-09-07 14:17:37 -0700619 int fence = -1;
Jesse Hallab9aeef2015-11-04 10:56:20 -0800620 if (driver_vtbl.QueueSignalReleaseImageANDROID) {
621 result = driver_vtbl.QueueSignalReleaseImageANDROID(
622 queue, img.image, &fence);
623 } else {
624 ALOG_ASSERT(driver_vtbl.QueueSignalNativeFenceANDROID,
625 "Have neither vkQueueSignalReleaseImageANDROID nor "
626 "vkQueueSignalNativeFenceANDROID");
627 result = driver_vtbl.QueueSignalNativeFenceANDROID(queue, &fence);
628 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700629 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800630 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halld7b994a2015-09-07 14:17:37 -0700631 if (final_result == VK_SUCCESS)
632 final_result = result;
633 // TODO(jessehall): What happens to the buffer here? Does the app
634 // still own it or not, i.e. should we cancel the buffer? Hard to
635 // do correctly without synchronizing, though I guess we could wait
636 // for the queue to idle.
637 continue;
638 }
639
Jesse Hall1356b0d2015-11-23 17:24:58 -0800640 err = window->queueBuffer(window, img.buffer.get(), fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700641 if (err != 0) {
642 // TODO(jessehall): What now? We should probably cancel the buffer,
643 // I guess?
644 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
645 if (final_result == VK_SUCCESS)
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700646 final_result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700647 continue;
648 }
649
650 if (img.dequeue_fence != -1) {
651 close(img.dequeue_fence);
652 img.dequeue_fence = -1;
653 }
654 img.dequeued = false;
655 }
656
657 return final_result;
658}
Jesse Hallb1352bc2015-09-04 16:12:33 -0700659
660} // namespace vulkan