blob: 320a2acc1d03ee7cd0160da7f9ba88f7a54a8118 [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
Chia-I Wu4a6a9162016-03-26 07:17:34 +080024#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070025
Jesse Hall5ae3abb2015-10-08 14:00:22 -070026// TODO(jessehall): Currently we don't have a good error code for when a native
27// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
28// versions (post SDK 0.9) of the API/extension have a better error code.
29// When updating to that version, audit all error returns.
Chia-I Wu62262232016-03-26 07:06:44 +080030namespace vulkan {
31namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070032
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);
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800101 return std::shared_ptr<T>(obj, NativeBaseDeleter<T>(),
102 VulkanAllocator<T>(GetData(host).allocator,
103 AllocScope<Host>::kScope));
Jesse Hall26cecff2016-01-21 19:52:25 -0800104 } catch (std::bad_alloc&) {
Jesse Hall26cecff2016-01-21 19:52:25 -0800105 return nullptr;
106 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700107}
108
Jesse Hall55bc0972016-02-23 16:43:29 -0800109const VkSurfaceTransformFlagsKHR kSupportedTransforms =
110 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
111 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
112 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
113 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
114 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
115 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
116 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
117 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
118 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
119 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
120
121VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
122 // Native and Vulkan transforms are isomorphic, but are represented
123 // differently. Vulkan transforms are built up of an optional horizontal
124 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
125 // transforms are built up from a horizontal flip, vertical flip, and
126 // 90-degree rotation, all optional but always in that order.
127
128 // TODO(jessehall): For now, only support pure rotations, not
129 // flip or flip-and-rotate, until I have more time to test them and build
130 // sample code. As far as I know we never actually use anything besides
131 // pure rotations anyway.
132
133 switch (native) {
134 case 0: // 0x0
135 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
136 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
137 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
138 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
139 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
140 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
141 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
142 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
143 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
144 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
145 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
146 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
147 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
148 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
149 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
150 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
151 default:
152 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
153 }
154}
155
Jesse Hall178b6962016-02-24 15:39:50 -0800156int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
157 switch (transform) {
158 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
159 return NATIVE_WINDOW_TRANSFORM_ROT_270;
160 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
161 return NATIVE_WINDOW_TRANSFORM_ROT_180;
162 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
163 return NATIVE_WINDOW_TRANSFORM_ROT_90;
164 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
165 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
166 // return NATIVE_WINDOW_TRANSFORM_FLIP_H;
167 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
168 // return NATIVE_WINDOW_TRANSFORM_FLIP_H |
169 // NATIVE_WINDOW_TRANSFORM_ROT_90;
170 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
171 // return NATIVE_WINDOW_TRANSFORM_FLIP_V;
172 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
173 // return NATIVE_WINDOW_TRANSFORM_FLIP_V |
174 // NATIVE_WINDOW_TRANSFORM_ROT_90;
175 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
176 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
177 default:
178 return 0;
179 }
180}
181
Jesse Halld7b994a2015-09-07 14:17:37 -0700182// ----------------------------------------------------------------------------
183
Jesse Hall1356b0d2015-11-23 17:24:58 -0800184struct Surface {
Jesse Halld7b994a2015-09-07 14:17:37 -0700185 std::shared_ptr<ANativeWindow> window;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800186};
187
188VkSurfaceKHR HandleFromSurface(Surface* surface) {
189 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
190}
191
192Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800193 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800194}
195
196struct Swapchain {
197 Swapchain(Surface& surface_, uint32_t num_images_)
198 : surface(surface_), num_images(num_images_) {}
199
200 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700201 uint32_t num_images;
202
203 struct Image {
204 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
205 VkImage image;
206 std::shared_ptr<ANativeWindowBuffer> buffer;
207 // The fence is only valid when the buffer is dequeued, and should be
208 // -1 any other time. When valid, we own the fd, and must ensure it is
209 // closed: either by closing it explicitly when queueing the buffer,
210 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
211 int dequeue_fence;
212 bool dequeued;
213 } images[android::BufferQueue::NUM_BUFFER_SLOTS];
214};
215
216VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
217 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
218}
219
220Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800221 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700222}
223
224} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700225
Jesse Halle1b12782015-11-30 11:27:32 -0800226VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800227VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800228 VkInstance instance,
229 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
230 const VkAllocationCallbacks* allocator,
231 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800232 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800233 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800234 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
235 alignof(Surface),
236 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800237 if (!mem)
238 return VK_ERROR_OUT_OF_HOST_MEMORY;
239 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700240
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800241 surface->window = InitSharedPtr(instance, pCreateInfo->window);
Jesse Hall26cecff2016-01-21 19:52:25 -0800242 if (!surface->window) {
243 ALOGE("surface creation failed: out of memory");
244 surface->~Surface();
245 allocator->pfnFree(allocator->pUserData, surface);
246 return VK_ERROR_OUT_OF_HOST_MEMORY;
247 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700248
Jesse Hall1356b0d2015-11-23 17:24:58 -0800249 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
250 int err =
251 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
252 if (err != 0) {
253 // TODO(jessehall): Improve error reporting. Can we enumerate possible
254 // errors and translate them to valid Vulkan result codes?
255 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
256 err);
257 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800258 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800259 return VK_ERROR_INITIALIZATION_FAILED;
260 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700261
Jesse Hall1356b0d2015-11-23 17:24:58 -0800262 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700263 return VK_SUCCESS;
264}
265
Jesse Halle1b12782015-11-30 11:27:32 -0800266VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800267void DestroySurfaceKHR(VkInstance instance,
268 VkSurfaceKHR surface_handle,
269 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800270 Surface* surface = SurfaceFromHandle(surface_handle);
271 if (!surface)
272 return;
273 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
274 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800275 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800276 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800277 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800278}
279
Jesse Halle1b12782015-11-30 11:27:32 -0800280VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800281VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
282 uint32_t /*queue_family*/,
283 VkSurfaceKHR /*surface*/,
284 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800285 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800286 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800287}
288
Jesse Halle1b12782015-11-30 11:27:32 -0800289VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800290VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800291 VkPhysicalDevice /*pdev*/,
292 VkSurfaceKHR surface,
293 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700294 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800295 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700296
297 int width, height;
298 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
299 if (err != 0) {
300 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
301 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700302 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700303 }
304 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
305 if (err != 0) {
306 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
307 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700308 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700309 }
310
Jesse Hall55bc0972016-02-23 16:43:29 -0800311 int transform_hint;
312 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
313 if (err != 0) {
314 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
315 strerror(-err), err);
316 return VK_ERROR_INITIALIZATION_FAILED;
317 }
318
Jesse Halld7b994a2015-09-07 14:17:37 -0700319 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800320 capabilities->minImageCount = 2;
321 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700322
Jesse Hallfe2662d2016-02-09 13:26:59 -0800323 capabilities->currentExtent =
324 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
325
Jesse Halld7b994a2015-09-07 14:17:37 -0700326 // TODO(jessehall): Figure out what the max extent should be. Maximum
327 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800328 capabilities->minImageExtent = VkExtent2D{1, 1};
329 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700330
Jesse Hallfe2662d2016-02-09 13:26:59 -0800331 capabilities->maxImageArrayLayers = 1;
332
Jesse Hall55bc0972016-02-23 16:43:29 -0800333 capabilities->supportedTransforms = kSupportedTransforms;
334 capabilities->currentTransform =
335 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700336
Jesse Hallfe2662d2016-02-09 13:26:59 -0800337 // On Android, window composition is a WindowManager property, not something
338 // associated with the bufferqueue. It can't be changed from here.
339 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700340
341 // TODO(jessehall): I think these are right, but haven't thought hard about
342 // it. Do we need to query the driver for support of any of these?
343 // Currently not included:
344 // - VK_IMAGE_USAGE_GENERAL: maybe? does this imply cpu mappable?
345 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
346 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800347 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800348 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
349 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
350 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700351 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
352
Jesse Hallb1352bc2015-09-04 16:12:33 -0700353 return VK_SUCCESS;
354}
355
Jesse Halle1b12782015-11-30 11:27:32 -0800356VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800357VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice /*pdev*/,
358 VkSurfaceKHR /*surface*/,
359 uint32_t* count,
360 VkSurfaceFormatKHR* formats) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800361 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
362 // a new gralloc method to query whether a (format, usage) pair is
363 // supported, and check that for each gralloc format that corresponds to a
364 // Vulkan format. Shorter term, just add a few more formats to the ones
365 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700366
367 const VkSurfaceFormatKHR kFormats[] = {
368 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
369 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
Jesse Hall517274a2016-02-10 00:07:18 -0800370 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLORSPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700371 };
372 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
373
374 VkResult result = VK_SUCCESS;
375 if (formats) {
376 if (*count < kNumFormats)
377 result = VK_INCOMPLETE;
378 std::copy(kFormats, kFormats + std::min(*count, kNumFormats), formats);
379 }
380 *count = kNumFormats;
381 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700382}
383
Jesse Halle1b12782015-11-30 11:27:32 -0800384VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800385VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice /*pdev*/,
386 VkSurfaceKHR /*surface*/,
387 uint32_t* count,
388 VkPresentModeKHR* modes) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700389 const VkPresentModeKHR kModes[] = {
390 VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
391 };
392 const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]);
393
394 VkResult result = VK_SUCCESS;
395 if (modes) {
396 if (*count < kNumModes)
397 result = VK_INCOMPLETE;
398 std::copy(kModes, kModes + std::min(*count, kNumModes), modes);
399 }
400 *count = kNumModes;
401 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700402}
403
Jesse Halle1b12782015-11-30 11:27:32 -0800404VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800405VkResult CreateSwapchainKHR(VkDevice device,
406 const VkSwapchainCreateInfoKHR* create_info,
407 const VkAllocationCallbacks* allocator,
408 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700409 int err;
410 VkResult result = VK_SUCCESS;
411
Jesse Hall1f91d392015-12-11 16:28:44 -0800412 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800413 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800414
Jesse Hall715b86a2016-01-16 16:34:29 -0800415 ALOGV_IF(create_info->imageArrayLayers != 1,
416 "Swapchain imageArrayLayers (%u) != 1 not supported",
417 create_info->imageArrayLayers);
Jesse Halld7b994a2015-09-07 14:17:37 -0700418
Jesse Halld7b994a2015-09-07 14:17:37 -0700419 ALOGE_IF(create_info->imageColorSpace != VK_COLORSPACE_SRGB_NONLINEAR_KHR,
420 "color spaces other than SRGB_NONLINEAR not yet implemented");
421 ALOGE_IF(create_info->oldSwapchain,
422 "swapchain re-creation not yet implemented");
Jesse Hall55bc0972016-02-23 16:43:29 -0800423 ALOGE_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
424 "swapchain preTransform %d not supported",
425 create_info->preTransform);
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800426 ALOGW_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
427 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR),
428 "swapchain present mode %d not supported",
429 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700430
431 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700432
Jesse Hall1356b0d2015-11-23 17:24:58 -0800433 Surface& surface = *SurfaceFromHandle(create_info->surface);
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800434 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -0800435
Jesse Hall517274a2016-02-10 00:07:18 -0800436 int native_format = HAL_PIXEL_FORMAT_RGBA_8888;
437 switch (create_info->imageFormat) {
438 case VK_FORMAT_R8G8B8A8_UNORM:
439 case VK_FORMAT_R8G8B8A8_SRGB:
440 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
441 break;
442 case VK_FORMAT_R5G6B5_UNORM_PACK16:
443 native_format = HAL_PIXEL_FORMAT_RGB_565;
444 break;
445 default:
446 ALOGE("unsupported swapchain format %d", create_info->imageFormat);
447 break;
448 }
449 err = native_window_set_buffers_format(surface.window.get(), native_format);
450 if (err != 0) {
451 // TODO(jessehall): Improve error reporting. Can we enumerate possible
452 // errors and translate them to valid Vulkan result codes?
453 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
454 native_format, strerror(-err), err);
455 return VK_ERROR_INITIALIZATION_FAILED;
456 }
457 err = native_window_set_buffers_data_space(surface.window.get(),
458 HAL_DATASPACE_SRGB_LINEAR);
459 if (err != 0) {
460 // TODO(jessehall): Improve error reporting. Can we enumerate possible
461 // errors and translate them to valid Vulkan result codes?
462 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
463 HAL_DATASPACE_SRGB_LINEAR, strerror(-err), err);
464 return VK_ERROR_INITIALIZATION_FAILED;
465 }
466
Jesse Hall3dd678a2016-01-08 21:52:01 -0800467 err = native_window_set_buffers_dimensions(
468 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
469 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700470 if (err != 0) {
471 // TODO(jessehall): Improve error reporting. Can we enumerate possible
472 // errors and translate them to valid Vulkan result codes?
473 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
474 create_info->imageExtent.width, create_info->imageExtent.height,
475 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700476 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700477 }
478
Jesse Hall178b6962016-02-24 15:39:50 -0800479 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
480 // applied during rendering. native_window_set_transform() expects the
481 // inverse: the transform the app is requesting that the compositor perform
482 // during composition. With native windows, pre-transform works by rendering
483 // with the same transform the compositor is applying (as in Vulkan), but
484 // then requesting the inverse transform, so that when the compositor does
485 // it's job the two transforms cancel each other out and the compositor ends
486 // up applying an identity transform to the app's buffer.
487 err = native_window_set_buffers_transform(
488 surface.window.get(),
489 InvertTransformToNative(create_info->preTransform));
490 if (err != 0) {
491 // TODO(jessehall): Improve error reporting. Can we enumerate possible
492 // errors and translate them to valid Vulkan result codes?
493 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
494 InvertTransformToNative(create_info->preTransform),
495 strerror(-err), err);
496 return VK_ERROR_INITIALIZATION_FAILED;
497 }
498
Jesse Hallf64ca122015-11-03 16:11:10 -0800499 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800500 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800501 if (err != 0) {
502 // TODO(jessehall): Improve error reporting. Can we enumerate possible
503 // errors and translate them to valid Vulkan result codes?
504 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
505 strerror(-err), err);
Jesse Hallf64ca122015-11-03 16:11:10 -0800506 return VK_ERROR_INITIALIZATION_FAILED;
507 }
508
Jesse Halle6080bf2016-02-28 20:58:50 -0800509 int query_value;
510 err = surface.window->query(surface.window.get(),
511 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
512 &query_value);
513 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700514 // TODO(jessehall): Improve error reporting. Can we enumerate possible
515 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -0800516 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
517 query_value);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700518 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700519 }
Jesse Halle6080bf2016-02-28 20:58:50 -0800520 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800521 // The MIN_UNDEQUEUED_BUFFERS query doesn't know whether we'll be using
522 // async mode or not, and assumes not. But in async mode, the BufferQueue
523 // requires an extra undequeued buffer.
524 // See BufferQueueCore::getMinUndequeuedBufferCountLocked().
525 if (create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
526 min_undequeued_buffers += 1;
527
Jesse Halld7b994a2015-09-07 14:17:37 -0700528 uint32_t num_images =
529 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800530 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700531 if (err != 0) {
532 // TODO(jessehall): Improve error reporting. Can we enumerate possible
533 // errors and translate them to valid Vulkan result codes?
534 ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
535 err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700536 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700537 }
538
Jesse Hall70f93352015-11-04 09:41:31 -0800539 int gralloc_usage = 0;
540 // TODO(jessehall): Remove conditional once all drivers have been updated
Jesse Hall1f91d392015-12-11 16:28:44 -0800541 if (dispatch.GetSwapchainGrallocUsageANDROID) {
542 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800543 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -0800544 &gralloc_usage);
545 if (result != VK_SUCCESS) {
546 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Jesse Hall70f93352015-11-04 09:41:31 -0800547 return VK_ERROR_INITIALIZATION_FAILED;
548 }
549 } else {
550 gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
551 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800552 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -0800553 if (err != 0) {
554 // TODO(jessehall): Improve error reporting. Can we enumerate possible
555 // errors and translate them to valid Vulkan result codes?
556 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Jesse Hall70f93352015-11-04 09:41:31 -0800557 return VK_ERROR_INITIALIZATION_FAILED;
558 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700559
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800560 err = surface.window->setSwapInterval(
561 surface.window.get(),
562 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1);
563 if (err != 0) {
564 // TODO(jessehall): Improve error reporting. Can we enumerate possible
565 // errors and translate them to valid Vulkan result codes?
566 ALOGE("native_window->setSwapInterval failed: %s (%d)", strerror(-err),
567 err);
568 return VK_ERROR_INITIALIZATION_FAILED;
569 }
570
Jesse Halld7b994a2015-09-07 14:17:37 -0700571 // -- Allocate our Swapchain object --
572 // After this point, we must deallocate the swapchain on error.
573
Jesse Hall1f91d392015-12-11 16:28:44 -0800574 void* mem = allocator->pfnAllocation(allocator->pUserData,
575 sizeof(Swapchain), alignof(Swapchain),
576 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800577 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -0700578 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800579 Swapchain* swapchain = new (mem) Swapchain(surface, num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700580
581 // -- Dequeue all buffers and create a VkImage for each --
582 // Any failures during or after this must cancel the dequeued buffers.
583
584 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -0700585#pragma clang diagnostic push
586#pragma clang diagnostic ignored "-Wold-style-cast"
587 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
588#pragma clang diagnostic pop
589 .pNext = nullptr,
590 };
591 VkImageCreateInfo image_create = {
592 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
593 .pNext = &image_native_buffer,
594 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -0800595 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -0700596 .extent = {0, 0, 1},
597 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -0800598 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -0800599 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -0700600 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800601 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -0700602 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800603 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800604 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -0700605 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
606 };
607
Jesse Halld7b994a2015-09-07 14:17:37 -0700608 for (uint32_t i = 0; i < num_images; i++) {
609 Swapchain::Image& img = swapchain->images[i];
610
611 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800612 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
613 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700614 if (err != 0) {
615 // TODO(jessehall): Improve error reporting. Can we enumerate
616 // possible errors and translate them to valid Vulkan result codes?
617 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700618 result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700619 break;
620 }
621 img.buffer = InitSharedPtr(device, buffer);
Jesse Hall26cecff2016-01-21 19:52:25 -0800622 if (!img.buffer) {
623 ALOGE("swapchain creation failed: out of memory");
624 surface.window->cancelBuffer(surface.window.get(), buffer,
625 img.dequeue_fence);
626 result = VK_ERROR_OUT_OF_HOST_MEMORY;
627 break;
628 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700629 img.dequeued = true;
630
631 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -0800632 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
633 static_cast<uint32_t>(img.buffer->height),
634 1};
Jesse Halld7b994a2015-09-07 14:17:37 -0700635 image_native_buffer.handle = img.buffer->handle;
636 image_native_buffer.stride = img.buffer->stride;
637 image_native_buffer.format = img.buffer->format;
638 image_native_buffer.usage = img.buffer->usage;
639
Jesse Hall03b6fe12015-11-24 12:44:21 -0800640 result =
Jesse Hall1f91d392015-12-11 16:28:44 -0800641 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700642 if (result != VK_SUCCESS) {
643 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
644 break;
645 }
646 }
647
648 // -- Cancel all buffers, returning them to the queue --
649 // If an error occurred before, also destroy the VkImage and release the
650 // buffer reference. Otherwise, we retain a strong reference to the buffer.
651 //
652 // TODO(jessehall): The error path here is the same as DestroySwapchain,
653 // but not the non-error path. Should refactor/unify.
654 for (uint32_t i = 0; i < num_images; i++) {
655 Swapchain::Image& img = swapchain->images[i];
656 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800657 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
658 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700659 img.dequeue_fence = -1;
660 img.dequeued = false;
661 }
662 if (result != VK_SUCCESS) {
663 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -0800664 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700665 }
666 }
667
668 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700669 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800670 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700671 return result;
672 }
673
674 *swapchain_handle = HandleFromSwapchain(swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700675 return VK_SUCCESS;
676}
677
Jesse Halle1b12782015-11-30 11:27:32 -0800678VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800679void DestroySwapchainKHR(VkDevice device,
680 VkSwapchainKHR swapchain_handle,
681 const VkAllocationCallbacks* allocator) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800682 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -0700683 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800684 const std::shared_ptr<ANativeWindow>& window = swapchain->surface.window;
Jesse Halld7b994a2015-09-07 14:17:37 -0700685
686 for (uint32_t i = 0; i < swapchain->num_images; i++) {
687 Swapchain::Image& img = swapchain->images[i];
688 if (img.dequeued) {
689 window->cancelBuffer(window.get(), img.buffer.get(),
690 img.dequeue_fence);
691 img.dequeue_fence = -1;
692 img.dequeued = false;
693 }
694 if (img.image) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800695 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700696 }
697 }
698
Jesse Hall1f91d392015-12-11 16:28:44 -0800699 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800700 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -0700701 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800702 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700703}
704
Jesse Halle1b12782015-11-30 11:27:32 -0800705VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800706VkResult GetSwapchainImagesKHR(VkDevice,
707 VkSwapchainKHR swapchain_handle,
708 uint32_t* count,
709 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700710 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
711 VkResult result = VK_SUCCESS;
712 if (images) {
713 uint32_t n = swapchain.num_images;
714 if (*count < swapchain.num_images) {
715 n = *count;
716 result = VK_INCOMPLETE;
717 }
718 for (uint32_t i = 0; i < n; i++)
719 images[i] = swapchain.images[i].image;
720 }
721 *count = swapchain.num_images;
722 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700723}
724
Jesse Halle1b12782015-11-30 11:27:32 -0800725VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800726VkResult AcquireNextImageKHR(VkDevice device,
727 VkSwapchainKHR swapchain_handle,
728 uint64_t timeout,
729 VkSemaphore semaphore,
730 VkFence vk_fence,
731 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700732 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800733 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700734 VkResult result;
735 int err;
736
737 ALOGW_IF(
738 timeout != UINT64_MAX,
739 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
740
741 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -0800742 int fence_fd;
743 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700744 if (err != 0) {
745 // TODO(jessehall): Improve error reporting. Can we enumerate possible
746 // errors and translate them to valid Vulkan result codes?
747 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700748 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700749 }
750
751 uint32_t idx;
752 for (idx = 0; idx < swapchain.num_images; idx++) {
753 if (swapchain.images[idx].buffer.get() == buffer) {
754 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -0800755 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -0700756 break;
757 }
758 }
759 if (idx == swapchain.num_images) {
760 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -0800761 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700762 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700763 }
764
765 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -0800766 if (fence_fd != -1) {
767 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700768 if (fence_clone == -1) {
769 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
770 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -0800771 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -0700772 }
773 }
774
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800775 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -0800776 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700777 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800778 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
779 // even if the call fails. We could close it ourselves on failure, but
780 // that would create a race condition if the driver closes it on a
781 // failure path: some other thread might create an fd with the same
782 // number between the time the driver closes it and the time we close
783 // it. We must assume one of: the driver *always* closes it even on
784 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -0800785 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -0700786 swapchain.images[idx].dequeued = false;
787 swapchain.images[idx].dequeue_fence = -1;
788 return result;
789 }
790
791 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700792 return VK_SUCCESS;
793}
794
Jesse Halle1b12782015-11-30 11:27:32 -0800795VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800796VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700797 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
798 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
799 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -0700800 ALOGV_IF(present_info->pNext, "VkPresentInfo::pNext != NULL");
801
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800802 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -0700803 VkResult final_result = VK_SUCCESS;
804 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
805 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -0800806 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800807 ANativeWindow* window = swapchain.surface.window.get();
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800808 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700809 Swapchain::Image& img = swapchain.images[image_idx];
Jesse Halld7b994a2015-09-07 14:17:37 -0700810 VkResult result;
811 int err;
812
Jesse Halld7b994a2015-09-07 14:17:37 -0700813 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -0800814 result = dispatch.QueueSignalReleaseImageANDROID(
815 queue, present_info->waitSemaphoreCount,
816 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700817 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -0800818 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halla9e57032015-11-30 01:03:10 -0800819 if (present_info->pResults)
820 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700821 if (final_result == VK_SUCCESS)
822 final_result = result;
823 // TODO(jessehall): What happens to the buffer here? Does the app
824 // still own it or not, i.e. should we cancel the buffer? Hard to
825 // do correctly without synchronizing, though I guess we could wait
826 // for the queue to idle.
827 continue;
828 }
829
Jesse Hall1356b0d2015-11-23 17:24:58 -0800830 err = window->queueBuffer(window, img.buffer.get(), fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700831 if (err != 0) {
832 // TODO(jessehall): What now? We should probably cancel the buffer,
833 // I guess?
834 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Halla9e57032015-11-30 01:03:10 -0800835 if (present_info->pResults)
836 present_info->pResults[sc] = result;
Jesse Halld7b994a2015-09-07 14:17:37 -0700837 if (final_result == VK_SUCCESS)
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700838 final_result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700839 continue;
840 }
841
842 if (img.dequeue_fence != -1) {
843 close(img.dequeue_fence);
844 img.dequeue_fence = -1;
845 }
846 img.dequeued = false;
Jesse Halla9e57032015-11-30 01:03:10 -0800847
848 if (present_info->pResults)
849 present_info->pResults[sc] = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -0700850 }
851
852 return final_result;
853}
Jesse Hallb1352bc2015-09-04 16:12:33 -0700854
Chia-I Wu62262232016-03-26 07:06:44 +0800855} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -0700856} // namespace vulkan