blob: f8de67555098684f998fb14a0ae05038a7b9fe97 [file] [log] [blame]
Jesse Hallb1352bc2015-09-04 16:12:33 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jesse Halld7b994a2015-09-07 14:17:37 -070017#include <algorithm>
18#include <memory>
19
20#include <gui/BufferQueue.h>
Jesse Hallb1352bc2015-09-04 16:12:33 -070021#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070022#include <sync/sync.h>
23
24#include "loader.h"
25
26using namespace vulkan;
27
Jesse Hall5ae3abb2015-10-08 14:00:22 -070028// TODO(jessehall): Currently we don't have a good error code for when a native
29// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
30// versions (post SDK 0.9) of the API/extension have a better error code.
31// When updating to that version, audit all error returns.
32
Jesse Halld7b994a2015-09-07 14:17:37 -070033namespace {
34
35// ----------------------------------------------------------------------------
36// These functions/classes form an adaptor that allows objects to be refcounted
37// by both android::sp<> and std::shared_ptr<> simultaneously, and delegates
Jesse Hall3fbc8562015-11-29 22:10:52 -080038// allocation of the shared_ptr<> control structure to VkAllocationCallbacks.
39// The
Jesse Halld7b994a2015-09-07 14:17:37 -070040// platform holds a reference to the ANativeWindow using its embedded reference
41// count, and the ANativeWindow implementation holds references to the
42// ANativeWindowBuffers using their embedded reference counts, so the
43// shared_ptr *must* cooperate with these and hold at least one reference to
44// the object using the embedded reference count.
45
46template <typename T>
47struct NativeBaseDeleter {
48 void operator()(T* obj) { obj->common.decRef(&obj->common); }
49};
50
Jesse Hall03b6fe12015-11-24 12:44:21 -080051template <typename Host>
52struct AllocScope {};
53
54template <>
55struct AllocScope<VkInstance> {
Jesse Hall3fbc8562015-11-29 22:10:52 -080056 static const VkSystemAllocationScope kScope =
57 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE;
Jesse Hall03b6fe12015-11-24 12:44:21 -080058};
59
60template <>
61struct AllocScope<VkDevice> {
Jesse Hall3fbc8562015-11-29 22:10:52 -080062 static const VkSystemAllocationScope kScope =
63 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE;
Jesse Hall03b6fe12015-11-24 12:44:21 -080064};
65
Jesse Hall1f91d392015-12-11 16:28:44 -080066template <typename T>
Jesse Halld7b994a2015-09-07 14:17:37 -070067class VulkanAllocator {
68 public:
69 typedef T value_type;
70
Jesse Hall1f91d392015-12-11 16:28:44 -080071 VulkanAllocator(const VkAllocationCallbacks& allocator,
72 VkSystemAllocationScope scope)
73 : allocator_(allocator), scope_(scope) {}
Jesse Halld7b994a2015-09-07 14:17:37 -070074
75 template <typename U>
Jesse Hall1f91d392015-12-11 16:28:44 -080076 explicit VulkanAllocator(const VulkanAllocator<U>& other)
77 : allocator_(other.allocator_), scope_(other.scope_) {}
Jesse Halld7b994a2015-09-07 14:17:37 -070078
79 T* allocate(size_t n) const {
Jesse Hall26cecff2016-01-21 19:52:25 -080080 T* p = static_cast<T*>(allocator_.pfnAllocation(
Jesse Hall1f91d392015-12-11 16:28:44 -080081 allocator_.pUserData, n * sizeof(T), alignof(T), scope_));
Jesse Hall26cecff2016-01-21 19:52:25 -080082 if (!p)
83 throw std::bad_alloc();
84 return p;
Jesse Halld7b994a2015-09-07 14:17:37 -070085 }
Jesse Hall26cecff2016-01-21 19:52:25 -080086 void deallocate(T* p, size_t) const noexcept {
Jesse Hall1f91d392015-12-11 16:28:44 -080087 return allocator_.pfnFree(allocator_.pUserData, p);
88 }
Jesse Halld7b994a2015-09-07 14:17:37 -070089
90 private:
Jesse Hall1f91d392015-12-11 16:28:44 -080091 template <typename U>
Jesse Halld7b994a2015-09-07 14:17:37 -070092 friend class VulkanAllocator;
Jesse Hall1f91d392015-12-11 16:28:44 -080093 const VkAllocationCallbacks& allocator_;
94 const VkSystemAllocationScope scope_;
Jesse Halld7b994a2015-09-07 14:17:37 -070095};
96
Jesse Hall1356b0d2015-11-23 17:24:58 -080097template <typename T, typename Host>
98std::shared_ptr<T> InitSharedPtr(Host host, T* obj) {
Jesse Hall26cecff2016-01-21 19:52:25 -080099 try {
100 obj->common.incRef(&obj->common);
101 return std::shared_ptr<T>(
102 obj, NativeBaseDeleter<T>(),
103 VulkanAllocator<T>(*GetAllocator(host), AllocScope<Host>::kScope));
104 } catch (std::bad_alloc&) {
105 obj->common.decRef(&obj->common);
106 return nullptr;
107 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700108}
109
110// ----------------------------------------------------------------------------
111
Jesse Hall1356b0d2015-11-23 17:24:58 -0800112struct Surface {
Jesse Halld7b994a2015-09-07 14:17:37 -0700113 std::shared_ptr<ANativeWindow> window;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800114};
115
116VkSurfaceKHR HandleFromSurface(Surface* surface) {
117 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
118}
119
120Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800121 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800122}
123
124struct Swapchain {
125 Swapchain(Surface& surface_, uint32_t num_images_)
126 : surface(surface_), num_images(num_images_) {}
127
128 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700129 uint32_t num_images;
130
131 struct Image {
132 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
133 VkImage image;
134 std::shared_ptr<ANativeWindowBuffer> buffer;
135 // The fence is only valid when the buffer is dequeued, and should be
136 // -1 any other time. When valid, we own the fd, and must ensure it is
137 // closed: either by closing it explicitly when queueing the buffer,
138 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
139 int dequeue_fence;
140 bool dequeued;
141 } images[android::BufferQueue::NUM_BUFFER_SLOTS];
142};
143
144VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
145 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
146}
147
148Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800149 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700150}
151
152} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700153
154namespace vulkan {
155
Jesse Halle1b12782015-11-30 11:27:32 -0800156VKAPI_ATTR
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800157VkResult CreateAndroidSurfaceKHR_Bottom(
158 VkInstance instance,
159 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
160 const VkAllocationCallbacks* allocator,
161 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800162 if (!allocator)
163 allocator = GetAllocator(instance);
164 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
165 alignof(Surface),
166 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800167 if (!mem)
168 return VK_ERROR_OUT_OF_HOST_MEMORY;
169 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700170
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800171 surface->window = InitSharedPtr(instance, pCreateInfo->window);
Jesse Hall26cecff2016-01-21 19:52:25 -0800172 if (!surface->window) {
173 ALOGE("surface creation failed: out of memory");
174 surface->~Surface();
175 allocator->pfnFree(allocator->pUserData, surface);
176 return VK_ERROR_OUT_OF_HOST_MEMORY;
177 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700178
Jesse Hall1356b0d2015-11-23 17:24:58 -0800179 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
180 int err =
181 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
182 if (err != 0) {
183 // TODO(jessehall): Improve error reporting. Can we enumerate possible
184 // errors and translate them to valid Vulkan result codes?
185 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
186 err);
187 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800188 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800189 return VK_ERROR_INITIALIZATION_FAILED;
190 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700191
Jesse Hall1356b0d2015-11-23 17:24:58 -0800192 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700193 return VK_SUCCESS;
194}
195
Jesse Halle1b12782015-11-30 11:27:32 -0800196VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800197void DestroySurfaceKHR_Bottom(VkInstance instance,
198 VkSurfaceKHR surface_handle,
199 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800200 Surface* surface = SurfaceFromHandle(surface_handle);
201 if (!surface)
202 return;
203 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
204 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800205 if (!allocator)
206 allocator = GetAllocator(instance);
207 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800208}
209
Jesse Halle1b12782015-11-30 11:27:32 -0800210VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800211VkResult GetPhysicalDeviceSurfaceSupportKHR_Bottom(VkPhysicalDevice /*pdev*/,
212 uint32_t /*queue_family*/,
213 VkSurfaceKHR /*surface*/,
214 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800215 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800216 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800217}
218
Jesse Halle1b12782015-11-30 11:27:32 -0800219VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800220VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Bottom(
Jesse Hallb00daad2015-11-29 19:46:20 -0800221 VkPhysicalDevice /*pdev*/,
222 VkSurfaceKHR surface,
223 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700224 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800225 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700226
227 int width, height;
228 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
229 if (err != 0) {
230 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
231 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700232 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700233 }
234 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
235 if (err != 0) {
236 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
237 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700238 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700239 }
240
Jesse Halld7b994a2015-09-07 14:17:37 -0700241 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800242 capabilities->minImageCount = 2;
243 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700244
Jesse Hallfe2662d2016-02-09 13:26:59 -0800245 capabilities->currentExtent =
246 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
247
Jesse Halld7b994a2015-09-07 14:17:37 -0700248 // TODO(jessehall): Figure out what the max extent should be. Maximum
249 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800250 capabilities->minImageExtent = VkExtent2D{1, 1};
251 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700252
Jesse Hallfe2662d2016-02-09 13:26:59 -0800253 capabilities->maxImageArrayLayers = 1;
254
Jesse Halld7b994a2015-09-07 14:17:37 -0700255 // TODO(jessehall): We can support all transforms, fix this once
256 // implemented.
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800257 capabilities->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700258
259 // TODO(jessehall): Implement based on NATIVE_WINDOW_TRANSFORM_HINT.
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800260 capabilities->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700261
Jesse Hallfe2662d2016-02-09 13:26:59 -0800262 // On Android, window composition is a WindowManager property, not something
263 // associated with the bufferqueue. It can't be changed from here.
264 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700265
266 // TODO(jessehall): I think these are right, but haven't thought hard about
267 // it. Do we need to query the driver for support of any of these?
268 // Currently not included:
269 // - VK_IMAGE_USAGE_GENERAL: maybe? does this imply cpu mappable?
270 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
271 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800272 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800273 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
274 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
275 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700276 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
277
Jesse Hallb1352bc2015-09-04 16:12:33 -0700278 return VK_SUCCESS;
279}
280
Jesse Halle1b12782015-11-30 11:27:32 -0800281VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800282VkResult GetPhysicalDeviceSurfaceFormatsKHR_Bottom(
283 VkPhysicalDevice /*pdev*/,
284 VkSurfaceKHR /*surface*/,
285 uint32_t* count,
286 VkSurfaceFormatKHR* formats) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800287 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
288 // a new gralloc method to query whether a (format, usage) pair is
289 // supported, and check that for each gralloc format that corresponds to a
290 // Vulkan format. Shorter term, just add a few more formats to the ones
291 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700292
293 const VkSurfaceFormatKHR kFormats[] = {
294 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
295 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
Jesse Hall517274a2016-02-10 00:07:18 -0800296 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700297 };
298 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
299
300 VkResult result = VK_SUCCESS;
301 if (formats) {
302 if (*count < kNumFormats)
303 result = VK_INCOMPLETE;
304 std::copy(kFormats, kFormats + std::min(*count, kNumFormats), formats);
305 }
306 *count = kNumFormats;
307 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700308}
309
Jesse Halle1b12782015-11-30 11:27:32 -0800310VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800311VkResult GetPhysicalDeviceSurfacePresentModesKHR_Bottom(
312 VkPhysicalDevice /*pdev*/,
313 VkSurfaceKHR /*surface*/,
314 uint32_t* count,
315 VkPresentModeKHR* modes) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700316 const VkPresentModeKHR kModes[] = {
317 VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
318 };
319 const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]);
320
321 VkResult result = VK_SUCCESS;
322 if (modes) {
323 if (*count < kNumModes)
324 result = VK_INCOMPLETE;
325 std::copy(kModes, kModes + std::min(*count, kNumModes), modes);
326 }
327 *count = kNumModes;
328 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700329}
330
Jesse Halle1b12782015-11-30 11:27:32 -0800331VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800332VkResult CreateSwapchainKHR_Bottom(VkDevice device,
333 const VkSwapchainCreateInfoKHR* create_info,
334 const VkAllocationCallbacks* allocator,
335 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700336 int err;
337 VkResult result = VK_SUCCESS;
338
Jesse Hall1f91d392015-12-11 16:28:44 -0800339 if (!allocator)
340 allocator = GetAllocator(device);
341
Jesse Hall715b86a2016-01-16 16:34:29 -0800342 ALOGV_IF(create_info->imageArrayLayers != 1,
343 "Swapchain imageArrayLayers (%u) != 1 not supported",
344 create_info->imageArrayLayers);
Jesse Halld7b994a2015-09-07 14:17:37 -0700345
Jesse Halld7b994a2015-09-07 14:17:37 -0700346 ALOGE_IF(create_info->imageColorSpace != VK_COLORSPACE_SRGB_NONLINEAR_KHR,
347 "color spaces other than SRGB_NONLINEAR not yet implemented");
348 ALOGE_IF(create_info->oldSwapchain,
349 "swapchain re-creation not yet implemented");
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800350 ALOGE_IF(create_info->preTransform != VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
Jesse Halld7b994a2015-09-07 14:17:37 -0700351 "swapchain preTransform not yet implemented");
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800352 ALOGW_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
353 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR),
354 "swapchain present mode %d not supported",
355 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700356
357 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700358
Jesse Hall1356b0d2015-11-23 17:24:58 -0800359 Surface& surface = *SurfaceFromHandle(create_info->surface);
Jesse Hall1f91d392015-12-11 16:28:44 -0800360 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Hall70f93352015-11-04 09:41:31 -0800361
Jesse Hall517274a2016-02-10 00:07:18 -0800362 int native_format = HAL_PIXEL_FORMAT_RGBA_8888;
363 switch (create_info->imageFormat) {
364 case VK_FORMAT_R8G8B8A8_UNORM:
365 case VK_FORMAT_R8G8B8A8_SRGB:
366 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
367 break;
368 case VK_FORMAT_R5G6B5_UNORM_PACK16:
369 native_format = HAL_PIXEL_FORMAT_RGB_565;
370 break;
371 default:
372 ALOGE("unsupported swapchain format %d", create_info->imageFormat);
373 break;
374 }
375 err = native_window_set_buffers_format(surface.window.get(), native_format);
376 if (err != 0) {
377 // TODO(jessehall): Improve error reporting. Can we enumerate possible
378 // errors and translate them to valid Vulkan result codes?
379 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
380 native_format, strerror(-err), err);
381 return VK_ERROR_INITIALIZATION_FAILED;
382 }
383 err = native_window_set_buffers_data_space(surface.window.get(),
384 HAL_DATASPACE_SRGB_LINEAR);
385 if (err != 0) {
386 // TODO(jessehall): Improve error reporting. Can we enumerate possible
387 // errors and translate them to valid Vulkan result codes?
388 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
389 HAL_DATASPACE_SRGB_LINEAR, strerror(-err), err);
390 return VK_ERROR_INITIALIZATION_FAILED;
391 }
392
Jesse Hall3dd678a2016-01-08 21:52:01 -0800393 err = native_window_set_buffers_dimensions(
394 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
395 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700396 if (err != 0) {
397 // TODO(jessehall): Improve error reporting. Can we enumerate possible
398 // errors and translate them to valid Vulkan result codes?
399 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
400 create_info->imageExtent.width, create_info->imageExtent.height,
401 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700402 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700403 }
404
Jesse Hallf64ca122015-11-03 16:11:10 -0800405 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800406 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800407 if (err != 0) {
408 // TODO(jessehall): Improve error reporting. Can we enumerate possible
409 // errors and translate them to valid Vulkan result codes?
410 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
411 strerror(-err), err);
Jesse Hallf64ca122015-11-03 16:11:10 -0800412 return VK_ERROR_INITIALIZATION_FAILED;
413 }
414
Jesse Halld7b994a2015-09-07 14:17:37 -0700415 uint32_t min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800416 err = surface.window->query(
417 surface.window.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
418 reinterpret_cast<int*>(&min_undequeued_buffers));
Jesse Halld7b994a2015-09-07 14:17:37 -0700419 if (err != 0) {
420 // TODO(jessehall): Improve error reporting. Can we enumerate possible
421 // errors and translate them to valid Vulkan result codes?
422 ALOGE("window->query failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700423 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700424 }
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800425 // The MIN_UNDEQUEUED_BUFFERS query doesn't know whether we'll be using
426 // async mode or not, and assumes not. But in async mode, the BufferQueue
427 // requires an extra undequeued buffer.
428 // See BufferQueueCore::getMinUndequeuedBufferCountLocked().
429 if (create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
430 min_undequeued_buffers += 1;
431
Jesse Halld7b994a2015-09-07 14:17:37 -0700432 uint32_t num_images =
433 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800434 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700435 if (err != 0) {
436 // TODO(jessehall): Improve error reporting. Can we enumerate possible
437 // errors and translate them to valid Vulkan result codes?
438 ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
439 err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700440 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700441 }
442
Jesse Hall70f93352015-11-04 09:41:31 -0800443 int gralloc_usage = 0;
444 // TODO(jessehall): Remove conditional once all drivers have been updated
Jesse Hall1f91d392015-12-11 16:28:44 -0800445 if (dispatch.GetSwapchainGrallocUsageANDROID) {
446 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800447 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -0800448 &gralloc_usage);
449 if (result != VK_SUCCESS) {
450 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Jesse Hall70f93352015-11-04 09:41:31 -0800451 return VK_ERROR_INITIALIZATION_FAILED;
452 }
453 } else {
454 gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
455 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800456 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -0800457 if (err != 0) {
458 // TODO(jessehall): Improve error reporting. Can we enumerate possible
459 // errors and translate them to valid Vulkan result codes?
460 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Jesse Hall70f93352015-11-04 09:41:31 -0800461 return VK_ERROR_INITIALIZATION_FAILED;
462 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700463
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800464 err = surface.window->setSwapInterval(
465 surface.window.get(),
466 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1);
467 if (err != 0) {
468 // TODO(jessehall): Improve error reporting. Can we enumerate possible
469 // errors and translate them to valid Vulkan result codes?
470 ALOGE("native_window->setSwapInterval failed: %s (%d)", strerror(-err),
471 err);
472 return VK_ERROR_INITIALIZATION_FAILED;
473 }
474
Jesse Halld7b994a2015-09-07 14:17:37 -0700475 // -- Allocate our Swapchain object --
476 // After this point, we must deallocate the swapchain on error.
477
Jesse Hall1f91d392015-12-11 16:28:44 -0800478 void* mem = allocator->pfnAllocation(allocator->pUserData,
479 sizeof(Swapchain), alignof(Swapchain),
480 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800481 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -0700482 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800483 Swapchain* swapchain = new (mem) Swapchain(surface, num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700484
485 // -- Dequeue all buffers and create a VkImage for each --
486 // Any failures during or after this must cancel the dequeued buffers.
487
488 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -0700489#pragma clang diagnostic push
490#pragma clang diagnostic ignored "-Wold-style-cast"
491 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
492#pragma clang diagnostic pop
493 .pNext = nullptr,
494 };
495 VkImageCreateInfo image_create = {
496 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
497 .pNext = &image_native_buffer,
498 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -0800499 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -0700500 .extent = {0, 0, 1},
501 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -0800502 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -0800503 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -0700504 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800505 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -0700506 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800507 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800508 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -0700509 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
510 };
511
Jesse Halld7b994a2015-09-07 14:17:37 -0700512 for (uint32_t i = 0; i < num_images; i++) {
513 Swapchain::Image& img = swapchain->images[i];
514
515 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800516 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
517 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700518 if (err != 0) {
519 // TODO(jessehall): Improve error reporting. Can we enumerate
520 // possible errors and translate them to valid Vulkan result codes?
521 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700522 result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700523 break;
524 }
525 img.buffer = InitSharedPtr(device, buffer);
Jesse Hall26cecff2016-01-21 19:52:25 -0800526 if (!img.buffer) {
527 ALOGE("swapchain creation failed: out of memory");
528 surface.window->cancelBuffer(surface.window.get(), buffer,
529 img.dequeue_fence);
530 result = VK_ERROR_OUT_OF_HOST_MEMORY;
531 break;
532 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700533 img.dequeued = true;
534
535 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -0800536 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
537 static_cast<uint32_t>(img.buffer->height),
538 1};
Jesse Halld7b994a2015-09-07 14:17:37 -0700539 image_native_buffer.handle = img.buffer->handle;
540 image_native_buffer.stride = img.buffer->stride;
541 image_native_buffer.format = img.buffer->format;
542 image_native_buffer.usage = img.buffer->usage;
543
Jesse Hall03b6fe12015-11-24 12:44:21 -0800544 result =
Jesse Hall1f91d392015-12-11 16:28:44 -0800545 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700546 if (result != VK_SUCCESS) {
547 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
548 break;
549 }
550 }
551
552 // -- Cancel all buffers, returning them to the queue --
553 // If an error occurred before, also destroy the VkImage and release the
554 // buffer reference. Otherwise, we retain a strong reference to the buffer.
555 //
556 // TODO(jessehall): The error path here is the same as DestroySwapchain,
557 // but not the non-error path. Should refactor/unify.
558 for (uint32_t i = 0; i < num_images; i++) {
559 Swapchain::Image& img = swapchain->images[i];
560 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800561 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
562 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700563 img.dequeue_fence = -1;
564 img.dequeued = false;
565 }
566 if (result != VK_SUCCESS) {
567 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -0800568 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700569 }
570 }
571
572 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700573 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800574 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700575 return result;
576 }
577
578 *swapchain_handle = HandleFromSwapchain(swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700579 return VK_SUCCESS;
580}
581
Jesse Halle1b12782015-11-30 11:27:32 -0800582VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800583void DestroySwapchainKHR_Bottom(VkDevice device,
584 VkSwapchainKHR swapchain_handle,
585 const VkAllocationCallbacks* allocator) {
586 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700587 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800588 const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window;
Jesse Halld7b994a2015-09-07 14:17:37 -0700589
590 for (uint32_t i = 0; i < swapchain->num_images; i++) {
591 Swapchain::Image& img = swapchain->images[i];
592 if (img.dequeued) {
593 window->cancelBuffer(window.get(), img.buffer.get(),
594 img.dequeue_fence);
595 img.dequeue_fence = -1;
596 img.dequeued = false;
597 }
598 if (img.image) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800599 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700600 }
601 }
602
Jesse Hall1f91d392015-12-11 16:28:44 -0800603 if (!allocator)
604 allocator = GetAllocator(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700605 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800606 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700607}
608
Jesse Halle1b12782015-11-30 11:27:32 -0800609VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800610VkResult GetSwapchainImagesKHR_Bottom(VkDevice,
611 VkSwapchainKHR swapchain_handle,
612 uint32_t* count,
613 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700614 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
615 VkResult result = VK_SUCCESS;
616 if (images) {
617 uint32_t n = swapchain.num_images;
618 if (*count < swapchain.num_images) {
619 n = *count;
620 result = VK_INCOMPLETE;
621 }
622 for (uint32_t i = 0; i < n; i++)
623 images[i] = swapchain.images[i].image;
624 }
625 *count = swapchain.num_images;
626 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700627}
628
Jesse Halle1b12782015-11-30 11:27:32 -0800629VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800630VkResult AcquireNextImageKHR_Bottom(VkDevice device,
631 VkSwapchainKHR swapchain_handle,
632 uint64_t timeout,
633 VkSemaphore semaphore,
634 VkFence vk_fence,
635 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700636 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800637 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700638 VkResult result;
639 int err;
640
641 ALOGW_IF(
642 timeout != UINT64_MAX,
643 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
644
645 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -0800646 int fence_fd;
647 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700648 if (err != 0) {
649 // TODO(jessehall): Improve error reporting. Can we enumerate possible
650 // errors and translate them to valid Vulkan result codes?
651 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700652 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700653 }
654
655 uint32_t idx;
656 for (idx = 0; idx < swapchain.num_images; idx++) {
657 if (swapchain.images[idx].buffer.get() == buffer) {
658 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -0800659 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -0700660 break;
661 }
662 }
663 if (idx == swapchain.num_images) {
664 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -0800665 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700666 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700667 }
668
669 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -0800670 if (fence_fd != -1) {
671 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700672 if (fence_clone == -1) {
673 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
674 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -0800675 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -0700676 }
677 }
678
Jesse Hall1f91d392015-12-11 16:28:44 -0800679 result = GetDriverDispatch(device).AcquireImageANDROID(
680 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700681 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800682 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
683 // even if the call fails. We could close it ourselves on failure, but
684 // that would create a race condition if the driver closes it on a
685 // failure path: some other thread might create an fd with the same
686 // number between the time the driver closes it and the time we close
687 // it. We must assume one of: the driver *always* closes it even on
688 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -0800689 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700690 swapchain.images[idx].dequeued = false;
691 swapchain.images[idx].dequeue_fence = -1;
692 return result;
693 }
694
695 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700696 return VK_SUCCESS;
697}
698
Jesse Halle1b12782015-11-30 11:27:32 -0800699VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800700VkResult QueuePresentKHR_Bottom(VkQueue queue,
701 const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700702 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
703 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
704 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -0700705 ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL");
706
Jesse Hall1f91d392015-12-11 16:28:44 -0800707 const DriverDispatchTable& dispatch = GetDriverDispatch(queue);
Jesse Halld7b994a2015-09-07 14:17:37 -0700708 VkResult final_result = VK_SUCCESS;
709 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
710 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -0800711 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800712 ANativeWindow* window = swapchain.surface.window.get();
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800713 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700714 Swapchain::Image& img = swapchain.images[image_idx];
Jesse Halld7b994a2015-09-07 14:17:37 -0700715 VkResult result;
716 int err;
717
Jesse Halld7b994a2015-09-07 14:17:37 -0700718 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -0800719 result = dispatch.QueueSignalReleaseImageANDROID(
720 queue, present_info->waitSemaphoreCount,
721 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700722 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800723 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halla9e57032015-11-30 01:03:10 -0800724 if (present_info->pResults)
725 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700726 if (final_result == VK_SUCCESS)
727 final_result = result;
728 // TODO(jessehall): What happens to the buffer here? Does the app
729 // still own it or not, i.e. should we cancel the buffer? Hard to
730 // do correctly without synchronizing, though I guess we could wait
731 // for the queue to idle.
732 continue;
733 }
734
Jesse Hall1356b0d2015-11-23 17:24:58 -0800735 err = window->queueBuffer(window, img.buffer.get(), fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700736 if (err != 0) {
737 // TODO(jessehall): What now? We should probably cancel the buffer,
738 // I guess?
739 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Halla9e57032015-11-30 01:03:10 -0800740 if (present_info->pResults)
741 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700742 if (final_result == VK_SUCCESS)
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700743 final_result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700744 continue;
745 }
746
747 if (img.dequeue_fence != -1) {
748 close(img.dequeue_fence);
749 img.dequeue_fence = -1;
750 }
751 img.dequeued = false;
Jesse Halla9e57032015-11-30 01:03:10 -0800752
753 if (present_info->pResults)
754 present_info->pResults[sc] = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -0700755 }
756
757 return final_result;
758}
Jesse Hallb1352bc2015-09-04 16:12:33 -0700759
760} // namespace vulkan