blob: c40636e5d5e975dd8a7c2137898ce75469f042df [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
Jesse Hall55bc0972016-02-23 16:43:29 -0800110const VkSurfaceTransformFlagsKHR kSupportedTransforms =
111 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
112 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
113 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
114 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
115 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
116 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
117 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
118 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
119 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
120 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
121
122VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
123 // Native and Vulkan transforms are isomorphic, but are represented
124 // differently. Vulkan transforms are built up of an optional horizontal
125 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
126 // transforms are built up from a horizontal flip, vertical flip, and
127 // 90-degree rotation, all optional but always in that order.
128
129 // TODO(jessehall): For now, only support pure rotations, not
130 // flip or flip-and-rotate, until I have more time to test them and build
131 // sample code. As far as I know we never actually use anything besides
132 // pure rotations anyway.
133
134 switch (native) {
135 case 0: // 0x0
136 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
137 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
138 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
139 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
140 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
141 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
142 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
143 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
144 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
145 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
146 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
147 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
148 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
149 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
150 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
151 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
152 default:
153 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
154 }
155}
156
Jesse Halld7b994a2015-09-07 14:17:37 -0700157// ----------------------------------------------------------------------------
158
Jesse Hall1356b0d2015-11-23 17:24:58 -0800159struct Surface {
Jesse Halld7b994a2015-09-07 14:17:37 -0700160 std::shared_ptr<ANativeWindow> window;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800161};
162
163VkSurfaceKHR HandleFromSurface(Surface* surface) {
164 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
165}
166
167Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800168 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800169}
170
171struct Swapchain {
172 Swapchain(Surface& surface_, uint32_t num_images_)
173 : surface(surface_), num_images(num_images_) {}
174
175 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700176 uint32_t num_images;
177
178 struct Image {
179 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
180 VkImage image;
181 std::shared_ptr<ANativeWindowBuffer> buffer;
182 // The fence is only valid when the buffer is dequeued, and should be
183 // -1 any other time. When valid, we own the fd, and must ensure it is
184 // closed: either by closing it explicitly when queueing the buffer,
185 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
186 int dequeue_fence;
187 bool dequeued;
188 } images[android::BufferQueue::NUM_BUFFER_SLOTS];
189};
190
191VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
192 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
193}
194
195Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800196 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700197}
198
199} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700200
201namespace vulkan {
202
Jesse Halle1b12782015-11-30 11:27:32 -0800203VKAPI_ATTR
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800204VkResult CreateAndroidSurfaceKHR_Bottom(
205 VkInstance instance,
206 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
207 const VkAllocationCallbacks* allocator,
208 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800209 if (!allocator)
210 allocator = GetAllocator(instance);
211 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
212 alignof(Surface),
213 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800214 if (!mem)
215 return VK_ERROR_OUT_OF_HOST_MEMORY;
216 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700217
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800218 surface->window = InitSharedPtr(instance, pCreateInfo->window);
Jesse Hall26cecff2016-01-21 19:52:25 -0800219 if (!surface->window) {
220 ALOGE("surface creation failed: out of memory");
221 surface->~Surface();
222 allocator->pfnFree(allocator->pUserData, surface);
223 return VK_ERROR_OUT_OF_HOST_MEMORY;
224 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700225
Jesse Hall1356b0d2015-11-23 17:24:58 -0800226 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
227 int err =
228 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
229 if (err != 0) {
230 // TODO(jessehall): Improve error reporting. Can we enumerate possible
231 // errors and translate them to valid Vulkan result codes?
232 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
233 err);
234 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800235 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800236 return VK_ERROR_INITIALIZATION_FAILED;
237 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700238
Jesse Hall1356b0d2015-11-23 17:24:58 -0800239 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700240 return VK_SUCCESS;
241}
242
Jesse Halle1b12782015-11-30 11:27:32 -0800243VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800244void DestroySurfaceKHR_Bottom(VkInstance instance,
245 VkSurfaceKHR surface_handle,
246 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800247 Surface* surface = SurfaceFromHandle(surface_handle);
248 if (!surface)
249 return;
250 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
251 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800252 if (!allocator)
253 allocator = GetAllocator(instance);
254 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800255}
256
Jesse Halle1b12782015-11-30 11:27:32 -0800257VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800258VkResult GetPhysicalDeviceSurfaceSupportKHR_Bottom(VkPhysicalDevice /*pdev*/,
259 uint32_t /*queue_family*/,
260 VkSurfaceKHR /*surface*/,
261 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800262 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800263 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800264}
265
Jesse Halle1b12782015-11-30 11:27:32 -0800266VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800267VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Bottom(
Jesse Hallb00daad2015-11-29 19:46:20 -0800268 VkPhysicalDevice /*pdev*/,
269 VkSurfaceKHR surface,
270 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700271 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800272 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700273
274 int width, height;
275 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
276 if (err != 0) {
277 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
278 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700279 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700280 }
281 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
282 if (err != 0) {
283 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
284 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700285 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700286 }
287
Jesse Hall55bc0972016-02-23 16:43:29 -0800288 int transform_hint;
289 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
290 if (err != 0) {
291 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
292 strerror(-err), err);
293 return VK_ERROR_INITIALIZATION_FAILED;
294 }
295
Jesse Halld7b994a2015-09-07 14:17:37 -0700296 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800297 capabilities->minImageCount = 2;
298 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700299
Jesse Hallfe2662d2016-02-09 13:26:59 -0800300 capabilities->currentExtent =
301 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
302
Jesse Halld7b994a2015-09-07 14:17:37 -0700303 // TODO(jessehall): Figure out what the max extent should be. Maximum
304 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800305 capabilities->minImageExtent = VkExtent2D{1, 1};
306 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700307
Jesse Hallfe2662d2016-02-09 13:26:59 -0800308 capabilities->maxImageArrayLayers = 1;
309
Jesse Hall55bc0972016-02-23 16:43:29 -0800310 capabilities->supportedTransforms = kSupportedTransforms;
311 capabilities->currentTransform =
312 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700313
Jesse Hallfe2662d2016-02-09 13:26:59 -0800314 // On Android, window composition is a WindowManager property, not something
315 // associated with the bufferqueue. It can't be changed from here.
316 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700317
318 // TODO(jessehall): I think these are right, but haven't thought hard about
319 // it. Do we need to query the driver for support of any of these?
320 // Currently not included:
321 // - VK_IMAGE_USAGE_GENERAL: maybe? does this imply cpu mappable?
322 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
323 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800324 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800325 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
326 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
327 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700328 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
329
Jesse Hallb1352bc2015-09-04 16:12:33 -0700330 return VK_SUCCESS;
331}
332
Jesse Halle1b12782015-11-30 11:27:32 -0800333VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800334VkResult GetPhysicalDeviceSurfaceFormatsKHR_Bottom(
335 VkPhysicalDevice /*pdev*/,
336 VkSurfaceKHR /*surface*/,
337 uint32_t* count,
338 VkSurfaceFormatKHR* formats) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800339 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
340 // a new gralloc method to query whether a (format, usage) pair is
341 // supported, and check that for each gralloc format that corresponds to a
342 // Vulkan format. Shorter term, just add a few more formats to the ones
343 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700344
345 const VkSurfaceFormatKHR kFormats[] = {
346 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
347 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
Jesse Hall517274a2016-02-10 00:07:18 -0800348 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700349 };
350 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
351
352 VkResult result = VK_SUCCESS;
353 if (formats) {
354 if (*count < kNumFormats)
355 result = VK_INCOMPLETE;
356 std::copy(kFormats, kFormats + std::min(*count, kNumFormats), formats);
357 }
358 *count = kNumFormats;
359 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700360}
361
Jesse Halle1b12782015-11-30 11:27:32 -0800362VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800363VkResult GetPhysicalDeviceSurfacePresentModesKHR_Bottom(
364 VkPhysicalDevice /*pdev*/,
365 VkSurfaceKHR /*surface*/,
366 uint32_t* count,
367 VkPresentModeKHR* modes) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700368 const VkPresentModeKHR kModes[] = {
369 VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
370 };
371 const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]);
372
373 VkResult result = VK_SUCCESS;
374 if (modes) {
375 if (*count < kNumModes)
376 result = VK_INCOMPLETE;
377 std::copy(kModes, kModes + std::min(*count, kNumModes), modes);
378 }
379 *count = kNumModes;
380 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700381}
382
Jesse Halle1b12782015-11-30 11:27:32 -0800383VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800384VkResult CreateSwapchainKHR_Bottom(VkDevice device,
385 const VkSwapchainCreateInfoKHR* create_info,
386 const VkAllocationCallbacks* allocator,
387 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700388 int err;
389 VkResult result = VK_SUCCESS;
390
Jesse Hall1f91d392015-12-11 16:28:44 -0800391 if (!allocator)
392 allocator = GetAllocator(device);
393
Jesse Hall715b86a2016-01-16 16:34:29 -0800394 ALOGV_IF(create_info->imageArrayLayers != 1,
395 "Swapchain imageArrayLayers (%u) != 1 not supported",
396 create_info->imageArrayLayers);
Jesse Halld7b994a2015-09-07 14:17:37 -0700397
Jesse Halld7b994a2015-09-07 14:17:37 -0700398 ALOGE_IF(create_info->imageColorSpace != VK_COLORSPACE_SRGB_NONLINEAR_KHR,
399 "color spaces other than SRGB_NONLINEAR not yet implemented");
400 ALOGE_IF(create_info->oldSwapchain,
401 "swapchain re-creation not yet implemented");
Jesse Hall55bc0972016-02-23 16:43:29 -0800402 ALOGE_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
403 "swapchain preTransform %d not supported",
404 create_info->preTransform);
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800405 ALOGW_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
406 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR),
407 "swapchain present mode %d not supported",
408 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700409
410 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700411
Jesse Hall1356b0d2015-11-23 17:24:58 -0800412 Surface& surface = *SurfaceFromHandle(create_info->surface);
Jesse Hall1f91d392015-12-11 16:28:44 -0800413 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Hall70f93352015-11-04 09:41:31 -0800414
Jesse Hall517274a2016-02-10 00:07:18 -0800415 int native_format = HAL_PIXEL_FORMAT_RGBA_8888;
416 switch (create_info->imageFormat) {
417 case VK_FORMAT_R8G8B8A8_UNORM:
418 case VK_FORMAT_R8G8B8A8_SRGB:
419 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
420 break;
421 case VK_FORMAT_R5G6B5_UNORM_PACK16:
422 native_format = HAL_PIXEL_FORMAT_RGB_565;
423 break;
424 default:
425 ALOGE("unsupported swapchain format %d", create_info->imageFormat);
426 break;
427 }
428 err = native_window_set_buffers_format(surface.window.get(), native_format);
429 if (err != 0) {
430 // TODO(jessehall): Improve error reporting. Can we enumerate possible
431 // errors and translate them to valid Vulkan result codes?
432 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
433 native_format, strerror(-err), err);
434 return VK_ERROR_INITIALIZATION_FAILED;
435 }
436 err = native_window_set_buffers_data_space(surface.window.get(),
437 HAL_DATASPACE_SRGB_LINEAR);
438 if (err != 0) {
439 // TODO(jessehall): Improve error reporting. Can we enumerate possible
440 // errors and translate them to valid Vulkan result codes?
441 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
442 HAL_DATASPACE_SRGB_LINEAR, strerror(-err), err);
443 return VK_ERROR_INITIALIZATION_FAILED;
444 }
445
Jesse Hall3dd678a2016-01-08 21:52:01 -0800446 err = native_window_set_buffers_dimensions(
447 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
448 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700449 if (err != 0) {
450 // TODO(jessehall): Improve error reporting. Can we enumerate possible
451 // errors and translate them to valid Vulkan result codes?
452 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
453 create_info->imageExtent.width, create_info->imageExtent.height,
454 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700455 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700456 }
457
Jesse Hallf64ca122015-11-03 16:11:10 -0800458 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800459 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800460 if (err != 0) {
461 // TODO(jessehall): Improve error reporting. Can we enumerate possible
462 // errors and translate them to valid Vulkan result codes?
463 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
464 strerror(-err), err);
Jesse Hallf64ca122015-11-03 16:11:10 -0800465 return VK_ERROR_INITIALIZATION_FAILED;
466 }
467
Jesse Halld7b994a2015-09-07 14:17:37 -0700468 uint32_t min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800469 err = surface.window->query(
470 surface.window.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
471 reinterpret_cast<int*>(&min_undequeued_buffers));
Jesse Halld7b994a2015-09-07 14:17:37 -0700472 if (err != 0) {
473 // TODO(jessehall): Improve error reporting. Can we enumerate possible
474 // errors and translate them to valid Vulkan result codes?
475 ALOGE("window->query failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700476 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700477 }
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800478 // The MIN_UNDEQUEUED_BUFFERS query doesn't know whether we'll be using
479 // async mode or not, and assumes not. But in async mode, the BufferQueue
480 // requires an extra undequeued buffer.
481 // See BufferQueueCore::getMinUndequeuedBufferCountLocked().
482 if (create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
483 min_undequeued_buffers += 1;
484
Jesse Halld7b994a2015-09-07 14:17:37 -0700485 uint32_t num_images =
486 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800487 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700488 if (err != 0) {
489 // TODO(jessehall): Improve error reporting. Can we enumerate possible
490 // errors and translate them to valid Vulkan result codes?
491 ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
492 err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700493 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700494 }
495
Jesse Hall70f93352015-11-04 09:41:31 -0800496 int gralloc_usage = 0;
497 // TODO(jessehall): Remove conditional once all drivers have been updated
Jesse Hall1f91d392015-12-11 16:28:44 -0800498 if (dispatch.GetSwapchainGrallocUsageANDROID) {
499 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800500 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -0800501 &gralloc_usage);
502 if (result != VK_SUCCESS) {
503 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Jesse Hall70f93352015-11-04 09:41:31 -0800504 return VK_ERROR_INITIALIZATION_FAILED;
505 }
506 } else {
507 gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
508 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800509 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -0800510 if (err != 0) {
511 // TODO(jessehall): Improve error reporting. Can we enumerate possible
512 // errors and translate them to valid Vulkan result codes?
513 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Jesse Hall70f93352015-11-04 09:41:31 -0800514 return VK_ERROR_INITIALIZATION_FAILED;
515 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700516
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800517 err = surface.window->setSwapInterval(
518 surface.window.get(),
519 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1);
520 if (err != 0) {
521 // TODO(jessehall): Improve error reporting. Can we enumerate possible
522 // errors and translate them to valid Vulkan result codes?
523 ALOGE("native_window->setSwapInterval failed: %s (%d)", strerror(-err),
524 err);
525 return VK_ERROR_INITIALIZATION_FAILED;
526 }
527
Jesse Halld7b994a2015-09-07 14:17:37 -0700528 // -- Allocate our Swapchain object --
529 // After this point, we must deallocate the swapchain on error.
530
Jesse Hall1f91d392015-12-11 16:28:44 -0800531 void* mem = allocator->pfnAllocation(allocator->pUserData,
532 sizeof(Swapchain), alignof(Swapchain),
533 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800534 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -0700535 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800536 Swapchain* swapchain = new (mem) Swapchain(surface, num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700537
538 // -- Dequeue all buffers and create a VkImage for each --
539 // Any failures during or after this must cancel the dequeued buffers.
540
541 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -0700542#pragma clang diagnostic push
543#pragma clang diagnostic ignored "-Wold-style-cast"
544 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
545#pragma clang diagnostic pop
546 .pNext = nullptr,
547 };
548 VkImageCreateInfo image_create = {
549 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
550 .pNext = &image_native_buffer,
551 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -0800552 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -0700553 .extent = {0, 0, 1},
554 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -0800555 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -0800556 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -0700557 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800558 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -0700559 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800560 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800561 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -0700562 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
563 };
564
Jesse Halld7b994a2015-09-07 14:17:37 -0700565 for (uint32_t i = 0; i < num_images; i++) {
566 Swapchain::Image& img = swapchain->images[i];
567
568 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800569 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
570 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700571 if (err != 0) {
572 // TODO(jessehall): Improve error reporting. Can we enumerate
573 // possible errors and translate them to valid Vulkan result codes?
574 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700575 result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700576 break;
577 }
578 img.buffer = InitSharedPtr(device, buffer);
Jesse Hall26cecff2016-01-21 19:52:25 -0800579 if (!img.buffer) {
580 ALOGE("swapchain creation failed: out of memory");
581 surface.window->cancelBuffer(surface.window.get(), buffer,
582 img.dequeue_fence);
583 result = VK_ERROR_OUT_OF_HOST_MEMORY;
584 break;
585 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700586 img.dequeued = true;
587
588 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -0800589 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
590 static_cast<uint32_t>(img.buffer->height),
591 1};
Jesse Halld7b994a2015-09-07 14:17:37 -0700592 image_native_buffer.handle = img.buffer->handle;
593 image_native_buffer.stride = img.buffer->stride;
594 image_native_buffer.format = img.buffer->format;
595 image_native_buffer.usage = img.buffer->usage;
596
Jesse Hall03b6fe12015-11-24 12:44:21 -0800597 result =
Jesse Hall1f91d392015-12-11 16:28:44 -0800598 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700599 if (result != VK_SUCCESS) {
600 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
601 break;
602 }
603 }
604
605 // -- Cancel all buffers, returning them to the queue --
606 // If an error occurred before, also destroy the VkImage and release the
607 // buffer reference. Otherwise, we retain a strong reference to the buffer.
608 //
609 // TODO(jessehall): The error path here is the same as DestroySwapchain,
610 // but not the non-error path. Should refactor/unify.
611 for (uint32_t i = 0; i < num_images; i++) {
612 Swapchain::Image& img = swapchain->images[i];
613 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800614 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
615 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700616 img.dequeue_fence = -1;
617 img.dequeued = false;
618 }
619 if (result != VK_SUCCESS) {
620 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -0800621 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700622 }
623 }
624
625 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700626 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800627 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700628 return result;
629 }
630
631 *swapchain_handle = HandleFromSwapchain(swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700632 return VK_SUCCESS;
633}
634
Jesse Halle1b12782015-11-30 11:27:32 -0800635VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800636void DestroySwapchainKHR_Bottom(VkDevice device,
637 VkSwapchainKHR swapchain_handle,
638 const VkAllocationCallbacks* allocator) {
639 const DriverDispatchTable& dispatch = GetDriverDispatch(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700640 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800641 const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window;
Jesse Halld7b994a2015-09-07 14:17:37 -0700642
643 for (uint32_t i = 0; i < swapchain->num_images; i++) {
644 Swapchain::Image& img = swapchain->images[i];
645 if (img.dequeued) {
646 window->cancelBuffer(window.get(), img.buffer.get(),
647 img.dequeue_fence);
648 img.dequeue_fence = -1;
649 img.dequeued = false;
650 }
651 if (img.image) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800652 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700653 }
654 }
655
Jesse Hall1f91d392015-12-11 16:28:44 -0800656 if (!allocator)
657 allocator = GetAllocator(device);
Jesse Halld7b994a2015-09-07 14:17:37 -0700658 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800659 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700660}
661
Jesse Halle1b12782015-11-30 11:27:32 -0800662VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800663VkResult GetSwapchainImagesKHR_Bottom(VkDevice,
664 VkSwapchainKHR swapchain_handle,
665 uint32_t* count,
666 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700667 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
668 VkResult result = VK_SUCCESS;
669 if (images) {
670 uint32_t n = swapchain.num_images;
671 if (*count < swapchain.num_images) {
672 n = *count;
673 result = VK_INCOMPLETE;
674 }
675 for (uint32_t i = 0; i < n; i++)
676 images[i] = swapchain.images[i].image;
677 }
678 *count = swapchain.num_images;
679 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700680}
681
Jesse Halle1b12782015-11-30 11:27:32 -0800682VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800683VkResult AcquireNextImageKHR_Bottom(VkDevice device,
684 VkSwapchainKHR swapchain_handle,
685 uint64_t timeout,
686 VkSemaphore semaphore,
687 VkFence vk_fence,
688 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700689 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800690 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700691 VkResult result;
692 int err;
693
694 ALOGW_IF(
695 timeout != UINT64_MAX,
696 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
697
698 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -0800699 int fence_fd;
700 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700701 if (err != 0) {
702 // TODO(jessehall): Improve error reporting. Can we enumerate possible
703 // errors and translate them to valid Vulkan result codes?
704 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700705 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700706 }
707
708 uint32_t idx;
709 for (idx = 0; idx < swapchain.num_images; idx++) {
710 if (swapchain.images[idx].buffer.get() == buffer) {
711 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -0800712 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -0700713 break;
714 }
715 }
716 if (idx == swapchain.num_images) {
717 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -0800718 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700719 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700720 }
721
722 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -0800723 if (fence_fd != -1) {
724 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700725 if (fence_clone == -1) {
726 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
727 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -0800728 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -0700729 }
730 }
731
Jesse Hall1f91d392015-12-11 16:28:44 -0800732 result = GetDriverDispatch(device).AcquireImageANDROID(
733 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700734 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800735 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
736 // even if the call fails. We could close it ourselves on failure, but
737 // that would create a race condition if the driver closes it on a
738 // failure path: some other thread might create an fd with the same
739 // number between the time the driver closes it and the time we close
740 // it. We must assume one of: the driver *always* closes it even on
741 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -0800742 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700743 swapchain.images[idx].dequeued = false;
744 swapchain.images[idx].dequeue_fence = -1;
745 return result;
746 }
747
748 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700749 return VK_SUCCESS;
750}
751
Jesse Halle1b12782015-11-30 11:27:32 -0800752VKAPI_ATTR
Jesse Hall1f91d392015-12-11 16:28:44 -0800753VkResult QueuePresentKHR_Bottom(VkQueue queue,
754 const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700755 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
756 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
757 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -0700758 ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL");
759
Jesse Hall1f91d392015-12-11 16:28:44 -0800760 const DriverDispatchTable& dispatch = GetDriverDispatch(queue);
Jesse Halld7b994a2015-09-07 14:17:37 -0700761 VkResult final_result = VK_SUCCESS;
762 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
763 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -0800764 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800765 ANativeWindow* window = swapchain.surface.window.get();
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800766 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700767 Swapchain::Image& img = swapchain.images[image_idx];
Jesse Halld7b994a2015-09-07 14:17:37 -0700768 VkResult result;
769 int err;
770
Jesse Halld7b994a2015-09-07 14:17:37 -0700771 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -0800772 result = dispatch.QueueSignalReleaseImageANDROID(
773 queue, present_info->waitSemaphoreCount,
774 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700775 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800776 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halla9e57032015-11-30 01:03:10 -0800777 if (present_info->pResults)
778 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700779 if (final_result == VK_SUCCESS)
780 final_result = result;
781 // TODO(jessehall): What happens to the buffer here? Does the app
782 // still own it or not, i.e. should we cancel the buffer? Hard to
783 // do correctly without synchronizing, though I guess we could wait
784 // for the queue to idle.
785 continue;
786 }
787
Jesse Hall1356b0d2015-11-23 17:24:58 -0800788 err = window->queueBuffer(window, img.buffer.get(), fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700789 if (err != 0) {
790 // TODO(jessehall): What now? We should probably cancel the buffer,
791 // I guess?
792 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Halla9e57032015-11-30 01:03:10 -0800793 if (present_info->pResults)
794 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700795 if (final_result == VK_SUCCESS)
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700796 final_result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700797 continue;
798 }
799
800 if (img.dequeue_fence != -1) {
801 close(img.dequeue_fence);
802 img.dequeue_fence = -1;
803 }
804 img.dequeued = false;
Jesse Halla9e57032015-11-30 01:03:10 -0800805
806 if (present_info->pResults)
807 present_info->pResults[sc] = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -0700808 }
809
810 return final_result;
811}
Jesse Hallb1352bc2015-09-04 16:12:33 -0700812
813} // namespace vulkan