blob: 3636db93fdec70e62af632dd99acaabfc2c58fcf [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>
Jesse Halld7b994a2015-09-07 14:17:37 -070018
Mark Salyzyn7823e122016-09-29 08:08:05 -070019#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070020#include <gui/BufferQueue.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070021#include <sync/sync.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080022#include <utils/StrongPointer.h>
Ian Elliott8a977262017-01-19 09:05:58 -070023#include <utils/SortedVector.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070024
Chia-I Wu4a6a9162016-03-26 07:17:34 +080025#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070026
Jesse Hall5ae3abb2015-10-08 14:00:22 -070027// TODO(jessehall): Currently we don't have a good error code for when a native
28// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
29// versions (post SDK 0.9) of the API/extension have a better error code.
30// When updating to that version, audit all error returns.
Chia-I Wu62262232016-03-26 07:06:44 +080031namespace vulkan {
32namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070033
Jesse Halld7b994a2015-09-07 14:17:37 -070034namespace {
35
Jesse Hall55bc0972016-02-23 16:43:29 -080036const VkSurfaceTransformFlagsKHR kSupportedTransforms =
37 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
38 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
39 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
40 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
41 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
42 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
43 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
44 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
45 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
46 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
47
48VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
49 // Native and Vulkan transforms are isomorphic, but are represented
50 // differently. Vulkan transforms are built up of an optional horizontal
51 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
52 // transforms are built up from a horizontal flip, vertical flip, and
53 // 90-degree rotation, all optional but always in that order.
54
55 // TODO(jessehall): For now, only support pure rotations, not
56 // flip or flip-and-rotate, until I have more time to test them and build
57 // sample code. As far as I know we never actually use anything besides
58 // pure rotations anyway.
59
60 switch (native) {
61 case 0: // 0x0
62 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
63 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
64 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
65 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
66 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
67 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
68 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
69 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
70 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
71 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
72 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
73 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
74 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
75 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
76 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
77 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
78 default:
79 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
80 }
81}
82
Jesse Hall178b6962016-02-24 15:39:50 -080083int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
84 switch (transform) {
85 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
86 return NATIVE_WINDOW_TRANSFORM_ROT_270;
87 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
88 return NATIVE_WINDOW_TRANSFORM_ROT_180;
89 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
90 return NATIVE_WINDOW_TRANSFORM_ROT_90;
91 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
92 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
93 // return NATIVE_WINDOW_TRANSFORM_FLIP_H;
94 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
95 // return NATIVE_WINDOW_TRANSFORM_FLIP_H |
96 // NATIVE_WINDOW_TRANSFORM_ROT_90;
97 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
98 // return NATIVE_WINDOW_TRANSFORM_FLIP_V;
99 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
100 // return NATIVE_WINDOW_TRANSFORM_FLIP_V |
101 // NATIVE_WINDOW_TRANSFORM_ROT_90;
102 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
103 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
104 default:
105 return 0;
106 }
107}
108
Ian Elliott8a977262017-01-19 09:05:58 -0700109class TimingInfo {
110 public:
Ian Elliott2c6355d2017-01-19 11:02:13 -0700111 TimingInfo()
112 : vals_{0, 0, 0, 0, 0},
113 timestamp_desired_present_time_(0),
114 timestamp_actual_present_time_(0),
115 timestamp_render_complete_time_(0),
116 timestamp_composition_latch_time_(0) {}
117 TimingInfo(const VkPresentTimeGOOGLE* qp)
118 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
119 timestamp_desired_present_time_(0),
120 timestamp_actual_present_time_(0),
121 timestamp_render_complete_time_(0),
122 timestamp_composition_latch_time_(0) {}
Ian Elliott8a977262017-01-19 09:05:58 -0700123 bool ready() {
124 return (timestamp_desired_present_time_ &&
125 timestamp_actual_present_time_ &&
126 timestamp_render_complete_time_ &&
127 timestamp_composition_latch_time_);
128 }
129 void calculate(uint64_t rdur) {
130 vals_.actualPresentTime = timestamp_actual_present_time_;
131 uint64_t margin = (timestamp_composition_latch_time_ -
132 timestamp_render_complete_time_);
133 // Calculate vals_.earliestPresentTime, and potentially adjust
134 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
135 // is vals_.actualPresentTime. If we can subtract rdur (the duration
136 // of a refresh cycle) from vals_.earliestPresentTime (and also from
137 // vals_.presentMargin) and still leave a positive margin, then we can
138 // report to the application that it could have presented earlier than
139 // it did (per the extension specification). If for some reason, we
140 // can do this subtraction repeatedly, we do, since
141 // vals_.earliestPresentTime really is supposed to be the "earliest".
142 uint64_t early_time = vals_.actualPresentTime;
143 while ((margin > rdur) &&
144 ((early_time - rdur) > timestamp_composition_latch_time_)) {
145 early_time -= rdur;
146 margin -= rdur;
147 }
148 vals_.earliestPresentTime = early_time;
149 vals_.presentMargin = margin;
150 }
151 void get_values(VkPastPresentationTimingGOOGLE* values) { *values = vals_; }
152
153 public:
154 VkPastPresentationTimingGOOGLE vals_;
155
156 uint64_t timestamp_desired_present_time_;
157 uint64_t timestamp_actual_present_time_;
158 uint64_t timestamp_render_complete_time_;
159 uint64_t timestamp_composition_latch_time_;
160};
161
162static inline int compare_type(const TimingInfo& lhs, const TimingInfo& rhs) {
163 // TODO(ianelliott): Change this from presentID to the frame ID once
164 // brianderson lands the appropriate patch:
165 if (lhs.vals_.presentID < rhs.vals_.presentID)
166 return -1;
167 if (lhs.vals_.presentID > rhs.vals_.presentID)
168 return 1;
169 return 0;
170}
171
Jesse Halld7b994a2015-09-07 14:17:37 -0700172// ----------------------------------------------------------------------------
173
Jesse Hall1356b0d2015-11-23 17:24:58 -0800174struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800175 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700176 VkSwapchainKHR swapchain_handle;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800177};
178
179VkSurfaceKHR HandleFromSurface(Surface* surface) {
180 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
181}
182
183Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800184 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800185}
186
Ian Elliott8a977262017-01-19 09:05:58 -0700187// Maximum number of TimingInfo structs to keep per swapchain:
188enum { MAX_TIMING_INFOS = 10 };
189// Minimum number of frames to look for in the past (so we don't cause
190// syncronous requests to Surface Flinger):
191enum { MIN_NUM_FRAMES_AGO = 5 };
192
Jesse Hall1356b0d2015-11-23 17:24:58 -0800193struct Swapchain {
194 Swapchain(Surface& surface_, uint32_t num_images_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700195 : surface(surface_),
196 num_images(num_images_),
Ian Elliott8a977262017-01-19 09:05:58 -0700197 frame_timestamps_enabled(false) {
198 timing.clear();
199 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800200
201 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700202 uint32_t num_images;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700203 bool frame_timestamps_enabled;
Jesse Halld7b994a2015-09-07 14:17:37 -0700204
205 struct Image {
206 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
207 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800208 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700209 // The fence is only valid when the buffer is dequeued, and should be
210 // -1 any other time. When valid, we own the fd, and must ensure it is
211 // closed: either by closing it explicitly when queueing the buffer,
212 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
213 int dequeue_fence;
214 bool dequeued;
215 } images[android::BufferQueue::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700216
217 android::SortedVector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700218};
219
220VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
221 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
222}
223
224Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800225 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700226}
227
Jesse Halldc225072016-05-30 22:40:14 -0700228void ReleaseSwapchainImage(VkDevice device,
229 ANativeWindow* window,
230 int release_fence,
231 Swapchain::Image& image) {
232 ALOG_ASSERT(release_fence == -1 || image.dequeued,
233 "ReleaseSwapchainImage: can't provide a release fence for "
234 "non-dequeued images");
235
236 if (image.dequeued) {
237 if (release_fence >= 0) {
238 // We get here from vkQueuePresentKHR. The application is
239 // responsible for creating an execution dependency chain from
240 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
241 // (release_fence), so we can drop the dequeue_fence here.
242 if (image.dequeue_fence >= 0)
243 close(image.dequeue_fence);
244 } else {
245 // We get here during swapchain destruction, or various serious
246 // error cases e.g. when we can't create the release_fence during
247 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
248 // have already signalled, since the swapchain images are supposed
249 // to be idle before the swapchain is destroyed. In error cases,
250 // there may be rendering in flight to the image, but since we
251 // weren't able to create a release_fence, waiting for the
252 // dequeue_fence is about the best we can do.
253 release_fence = image.dequeue_fence;
254 }
255 image.dequeue_fence = -1;
256
257 if (window) {
258 window->cancelBuffer(window, image.buffer.get(), release_fence);
259 } else {
260 if (release_fence >= 0) {
261 sync_wait(release_fence, -1 /* forever */);
262 close(release_fence);
263 }
264 }
265
266 image.dequeued = false;
267 }
268
269 if (image.image) {
270 GetData(device).driver.DestroyImage(device, image.image, nullptr);
271 image.image = VK_NULL_HANDLE;
272 }
273
274 image.buffer.clear();
275}
276
277void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
278 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
279 return;
Jesse Halldc225072016-05-30 22:40:14 -0700280 for (uint32_t i = 0; i < swapchain->num_images; i++) {
281 if (!swapchain->images[i].dequeued)
282 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
283 }
284 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700285 swapchain->timing.clear();
286}
287
288uint32_t get_num_ready_timings(Swapchain& swapchain) {
289 uint32_t num_ready = 0;
290 uint32_t num_timings = static_cast<uint32_t>(swapchain.timing.size());
291 uint32_t frames_ago = num_timings;
292 for (uint32_t i = 0; i < num_timings; i++) {
293 TimingInfo* ti = &swapchain.timing.editItemAt(i);
294 if (ti) {
295 if (ti->ready()) {
296 // This TimingInfo is ready to be reported to the user. Add it
297 // to the num_ready.
298 num_ready++;
299 } else {
300 // This TimingInfo is not yet ready to be reported to the user,
301 // and so we should look for any available timestamps that
302 // might make it ready.
303 int64_t desired_present_time = 0;
304 int64_t render_complete_time = 0;
305 int64_t composition_latch_time = 0;
306 int64_t actual_present_time = 0;
307 for (uint32_t f = MIN_NUM_FRAMES_AGO; f < frames_ago; f++) {
308 // Obtain timestamps:
309 int ret = native_window_get_frame_timestamps(
310 swapchain.surface.window.get(), f,
311 &desired_present_time, &render_complete_time,
312 &composition_latch_time,
313 NULL, //&first_composition_start_time,
314 NULL, //&last_composition_start_time,
315 NULL, //&composition_finish_time,
316 // TODO(ianelliott): Maybe ask if this one is
317 // supported, at startup time (since it may not be
318 // supported):
319 &actual_present_time,
320 NULL, //&display_retire_time,
321 NULL, //&dequeue_ready_time,
322 NULL /*&reads_done_time*/);
323 if (ret) {
324 break;
325 } else if (!ret) {
326 // We obtained at least one valid timestamp. See if it
327 // is for the present represented by this TimingInfo:
328 if (static_cast<uint64_t>(desired_present_time) ==
329 ti->vals_.desiredPresentTime) {
330 // Record the timestamp(s) we received, and then
331 // see if this TimingInfo is ready to be reported
332 // to the user:
333 ti->timestamp_desired_present_time_ =
334 static_cast<uint64_t>(desired_present_time);
335 ti->timestamp_actual_present_time_ =
336 static_cast<uint64_t>(actual_present_time);
337 ti->timestamp_render_complete_time_ =
338 static_cast<uint64_t>(render_complete_time);
339 ti->timestamp_composition_latch_time_ =
340 static_cast<uint64_t>(composition_latch_time);
341
342 if (ti->ready()) {
343 // The TimingInfo has received enough
344 // timestamps, and should now use those
345 // timestamps to calculate the info that should
346 // be reported to the user:
347 //
348 // FIXME: GET ACTUAL VALUE RATHER THAN HARD-CODE
349 // IT:
350 ti->calculate(16666666);
351 num_ready++;
352 }
353 break;
354 }
355 }
356 }
357 }
358 }
359 }
360 return num_ready;
361}
362
363// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
364void copy_ready_timings(Swapchain& swapchain,
365 uint32_t* count,
366 VkPastPresentationTimingGOOGLE* timings) {
367 uint32_t num_copied = 0;
368 uint32_t num_timings = static_cast<uint32_t>(swapchain.timing.size());
369 if (*count < num_timings) {
370 num_timings = *count;
371 }
372 for (uint32_t i = 0; i < num_timings; i++) {
373 TimingInfo* ti = &swapchain.timing.editItemAt(i);
374 if (ti && ti->ready()) {
375 ti->get_values(&timings[num_copied]);
376 num_copied++;
377 // We only report the values for a given present once, so remove
378 // them from swapchain.timing:
379 //
380 // TODO(ianelliott): SEE WHAT HAPPENS TO THE LOOP WHEN THE
381 // FOLLOWING IS DONE:
382 swapchain.timing.removeAt(i);
383 i--;
384 num_timings--;
385 if (*count == num_copied) {
386 break;
387 }
388 }
389 }
390 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700391}
392
Jesse Halld7b994a2015-09-07 14:17:37 -0700393} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700394
Jesse Halle1b12782015-11-30 11:27:32 -0800395VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800396VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800397 VkInstance instance,
398 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
399 const VkAllocationCallbacks* allocator,
400 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800401 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800402 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800403 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
404 alignof(Surface),
405 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800406 if (!mem)
407 return VK_ERROR_OUT_OF_HOST_MEMORY;
408 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700409
Chia-I Wue8e689f2016-04-18 08:21:31 +0800410 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700411 surface->swapchain_handle = VK_NULL_HANDLE;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700412
Jesse Hall1356b0d2015-11-23 17:24:58 -0800413 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
414 int err =
415 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
416 if (err != 0) {
417 // TODO(jessehall): Improve error reporting. Can we enumerate possible
418 // errors and translate them to valid Vulkan result codes?
419 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
420 err);
421 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800422 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800423 return VK_ERROR_INITIALIZATION_FAILED;
424 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700425
Jesse Hall1356b0d2015-11-23 17:24:58 -0800426 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700427 return VK_SUCCESS;
428}
429
Jesse Halle1b12782015-11-30 11:27:32 -0800430VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800431void DestroySurfaceKHR(VkInstance instance,
432 VkSurfaceKHR surface_handle,
433 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800434 Surface* surface = SurfaceFromHandle(surface_handle);
435 if (!surface)
436 return;
437 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700438 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700439 "destroyed VkSurfaceKHR 0x%" PRIx64
440 " has active VkSwapchainKHR 0x%" PRIx64,
441 reinterpret_cast<uint64_t>(surface_handle),
442 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800443 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800444 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800445 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800446 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800447}
448
Jesse Halle1b12782015-11-30 11:27:32 -0800449VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800450VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
451 uint32_t /*queue_family*/,
452 VkSurfaceKHR /*surface*/,
453 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800454 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800455 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800456}
457
Jesse Halle1b12782015-11-30 11:27:32 -0800458VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800459VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800460 VkPhysicalDevice /*pdev*/,
461 VkSurfaceKHR surface,
462 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700463 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800464 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700465
466 int width, height;
467 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
468 if (err != 0) {
469 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
470 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700471 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700472 }
473 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
474 if (err != 0) {
475 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
476 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700477 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700478 }
479
Jesse Hall55bc0972016-02-23 16:43:29 -0800480 int transform_hint;
481 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
482 if (err != 0) {
483 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
484 strerror(-err), err);
485 return VK_ERROR_INITIALIZATION_FAILED;
486 }
487
Jesse Halld7b994a2015-09-07 14:17:37 -0700488 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800489 capabilities->minImageCount = 2;
490 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700491
Jesse Hallfe2662d2016-02-09 13:26:59 -0800492 capabilities->currentExtent =
493 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
494
Jesse Halld7b994a2015-09-07 14:17:37 -0700495 // TODO(jessehall): Figure out what the max extent should be. Maximum
496 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800497 capabilities->minImageExtent = VkExtent2D{1, 1};
498 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700499
Jesse Hallfe2662d2016-02-09 13:26:59 -0800500 capabilities->maxImageArrayLayers = 1;
501
Jesse Hall55bc0972016-02-23 16:43:29 -0800502 capabilities->supportedTransforms = kSupportedTransforms;
503 capabilities->currentTransform =
504 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700505
Jesse Hallfe2662d2016-02-09 13:26:59 -0800506 // On Android, window composition is a WindowManager property, not something
507 // associated with the bufferqueue. It can't be changed from here.
508 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700509
510 // TODO(jessehall): I think these are right, but haven't thought hard about
511 // it. Do we need to query the driver for support of any of these?
512 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700513 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
514 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800515 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800516 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
517 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
518 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700519 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
520
Jesse Hallb1352bc2015-09-04 16:12:33 -0700521 return VK_SUCCESS;
522}
523
Jesse Halle1b12782015-11-30 11:27:32 -0800524VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800525VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice /*pdev*/,
526 VkSurfaceKHR /*surface*/,
527 uint32_t* count,
528 VkSurfaceFormatKHR* formats) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800529 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
530 // a new gralloc method to query whether a (format, usage) pair is
531 // supported, and check that for each gralloc format that corresponds to a
532 // Vulkan format. Shorter term, just add a few more formats to the ones
533 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700534
535 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700536 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
537 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
538 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700539 };
540 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
541
542 VkResult result = VK_SUCCESS;
543 if (formats) {
544 if (*count < kNumFormats)
545 result = VK_INCOMPLETE;
Jesse Hall7331e222016-09-15 21:26:01 -0700546 *count = std::min(*count, kNumFormats);
547 std::copy(kFormats, kFormats + *count, formats);
548 } else {
549 *count = kNumFormats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700550 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700551 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700552}
553
Jesse Halle1b12782015-11-30 11:27:32 -0800554VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800555VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice /*pdev*/,
556 VkSurfaceKHR /*surface*/,
557 uint32_t* count,
558 VkPresentModeKHR* modes) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700559 const VkPresentModeKHR kModes[] = {
560 VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
Chris Forbes980ad052017-01-18 16:55:07 +1300561 // TODO(chrisforbes): should only expose this if the driver can.
562 VK_PRESENT_MODE_FRONT_BUFFERED_DEMAND_REFRESH_KHR,
563 VK_PRESENT_MODE_FRONT_BUFFERED_CONTINUOUS_REFRESH_KHR,
Jesse Halld7b994a2015-09-07 14:17:37 -0700564 };
565 const uint32_t kNumModes = sizeof(kModes) / sizeof(kModes[0]);
566
567 VkResult result = VK_SUCCESS;
568 if (modes) {
569 if (*count < kNumModes)
570 result = VK_INCOMPLETE;
Jesse Hall7331e222016-09-15 21:26:01 -0700571 *count = std::min(*count, kNumModes);
572 std::copy(kModes, kModes + *count, modes);
573 } else {
574 *count = kNumModes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700575 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700576 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700577}
578
Jesse Halle1b12782015-11-30 11:27:32 -0800579VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800580VkResult CreateSwapchainKHR(VkDevice device,
581 const VkSwapchainCreateInfoKHR* create_info,
582 const VkAllocationCallbacks* allocator,
583 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700584 int err;
585 VkResult result = VK_SUCCESS;
586
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700587 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
588 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
589 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
590 " oldSwapchain=0x%" PRIx64,
591 reinterpret_cast<uint64_t>(create_info->surface),
592 create_info->minImageCount, create_info->imageFormat,
593 create_info->imageColorSpace, create_info->imageExtent.width,
594 create_info->imageExtent.height, create_info->imageUsage,
595 create_info->preTransform, create_info->presentMode,
596 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
597
Jesse Hall1f91d392015-12-11 16:28:44 -0800598 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800599 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800600
Jesse Hall42a9eec2016-06-03 12:39:49 -0700601 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700602 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800603 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700604 ALOGV_IF(create_info->imageColorSpace != VK_COLOR_SPACE_SRGB_NONLINEAR_KHR,
Jesse Halldc225072016-05-30 22:40:14 -0700605 "swapchain imageColorSpace=%u not supported",
606 create_info->imageColorSpace);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700607 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700608 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800609 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700610 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +1300611 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
612 create_info->presentMode == VK_PRESENT_MODE_FRONT_BUFFERED_DEMAND_REFRESH_KHR ||
613 create_info->presentMode == VK_PRESENT_MODE_FRONT_BUFFERED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700614 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800615 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700616
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700617 Surface& surface = *SurfaceFromHandle(create_info->surface);
618
Jesse Halldc225072016-05-30 22:40:14 -0700619 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -0700620 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -0700621 " because it already has active swapchain 0x%" PRIx64
622 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
623 reinterpret_cast<uint64_t>(create_info->surface),
624 reinterpret_cast<uint64_t>(surface.swapchain_handle),
625 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
626 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
627 }
628 if (create_info->oldSwapchain != VK_NULL_HANDLE)
629 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
630
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700631 // -- Reset the native window --
632 // The native window might have been used previously, and had its properties
633 // changed from defaults. That will affect the answer we get for queries
634 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
635 // attempt such queries.
636
Jesse Halldc225072016-05-30 22:40:14 -0700637 // The native window only allows dequeueing all buffers before any have
638 // been queued, since after that point at least one is assumed to be in
639 // non-FREE state at any given time. Disconnecting and re-connecting
640 // orphans the previous buffers, getting us back to the state where we can
641 // dequeue all buffers.
642 err = native_window_api_disconnect(surface.window.get(),
643 NATIVE_WINDOW_API_EGL);
644 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
645 strerror(-err), err);
646 err =
647 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
648 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
649 strerror(-err), err);
650
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700651 err = native_window_set_buffer_count(surface.window.get(), 0);
652 if (err != 0) {
653 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
654 strerror(-err), err);
655 return VK_ERROR_INITIALIZATION_FAILED;
656 }
657
658 err = surface.window->setSwapInterval(surface.window.get(), 1);
659 if (err != 0) {
660 // TODO(jessehall): Improve error reporting. Can we enumerate possible
661 // errors and translate them to valid Vulkan result codes?
662 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
663 strerror(-err), err);
664 return VK_ERROR_INITIALIZATION_FAILED;
665 }
666
Chris Forbesb8042d22017-01-18 18:07:05 +1300667 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
668 if (err != 0) {
669 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
670 strerror(-err), err);
671 return VK_ERROR_INITIALIZATION_FAILED;
672 }
673
674 err = native_window_set_auto_refresh(surface.window.get(), false);
675 if (err != 0) {
676 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
677 strerror(-err), err);
678 return VK_ERROR_INITIALIZATION_FAILED;
679 }
680
Jesse Halld7b994a2015-09-07 14:17:37 -0700681 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700682
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800683 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -0800684
Jesse Hall517274a2016-02-10 00:07:18 -0800685 int native_format = HAL_PIXEL_FORMAT_RGBA_8888;
686 switch (create_info->imageFormat) {
687 case VK_FORMAT_R8G8B8A8_UNORM:
688 case VK_FORMAT_R8G8B8A8_SRGB:
689 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
690 break;
691 case VK_FORMAT_R5G6B5_UNORM_PACK16:
692 native_format = HAL_PIXEL_FORMAT_RGB_565;
693 break;
694 default:
Jesse Hall42a9eec2016-06-03 12:39:49 -0700695 ALOGV("unsupported swapchain format %d", create_info->imageFormat);
Jesse Hall517274a2016-02-10 00:07:18 -0800696 break;
697 }
698 err = native_window_set_buffers_format(surface.window.get(), native_format);
699 if (err != 0) {
700 // TODO(jessehall): Improve error reporting. Can we enumerate possible
701 // errors and translate them to valid Vulkan result codes?
702 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
703 native_format, strerror(-err), err);
704 return VK_ERROR_INITIALIZATION_FAILED;
705 }
706 err = native_window_set_buffers_data_space(surface.window.get(),
707 HAL_DATASPACE_SRGB_LINEAR);
708 if (err != 0) {
709 // TODO(jessehall): Improve error reporting. Can we enumerate possible
710 // errors and translate them to valid Vulkan result codes?
711 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
712 HAL_DATASPACE_SRGB_LINEAR, strerror(-err), err);
713 return VK_ERROR_INITIALIZATION_FAILED;
714 }
715
Jesse Hall3dd678a2016-01-08 21:52:01 -0800716 err = native_window_set_buffers_dimensions(
717 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
718 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700719 if (err != 0) {
720 // TODO(jessehall): Improve error reporting. Can we enumerate possible
721 // errors and translate them to valid Vulkan result codes?
722 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
723 create_info->imageExtent.width, create_info->imageExtent.height,
724 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700725 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700726 }
727
Jesse Hall178b6962016-02-24 15:39:50 -0800728 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
729 // applied during rendering. native_window_set_transform() expects the
730 // inverse: the transform the app is requesting that the compositor perform
731 // during composition. With native windows, pre-transform works by rendering
732 // with the same transform the compositor is applying (as in Vulkan), but
733 // then requesting the inverse transform, so that when the compositor does
734 // it's job the two transforms cancel each other out and the compositor ends
735 // up applying an identity transform to the app's buffer.
736 err = native_window_set_buffers_transform(
737 surface.window.get(),
738 InvertTransformToNative(create_info->preTransform));
739 if (err != 0) {
740 // TODO(jessehall): Improve error reporting. Can we enumerate possible
741 // errors and translate them to valid Vulkan result codes?
742 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
743 InvertTransformToNative(create_info->preTransform),
744 strerror(-err), err);
745 return VK_ERROR_INITIALIZATION_FAILED;
746 }
747
Jesse Hallf64ca122015-11-03 16:11:10 -0800748 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800749 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800750 if (err != 0) {
751 // TODO(jessehall): Improve error reporting. Can we enumerate possible
752 // errors and translate them to valid Vulkan result codes?
753 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
754 strerror(-err), err);
Jesse Hallf64ca122015-11-03 16:11:10 -0800755 return VK_ERROR_INITIALIZATION_FAILED;
756 }
757
Jesse Halle6080bf2016-02-28 20:58:50 -0800758 int query_value;
759 err = surface.window->query(surface.window.get(),
760 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
761 &query_value);
762 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700763 // TODO(jessehall): Improve error reporting. Can we enumerate possible
764 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -0800765 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
766 query_value);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700767 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700768 }
Jesse Halle6080bf2016-02-28 20:58:50 -0800769 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800770 // The MIN_UNDEQUEUED_BUFFERS query doesn't know whether we'll be using
771 // async mode or not, and assumes not. But in async mode, the BufferQueue
772 // requires an extra undequeued buffer.
773 // See BufferQueueCore::getMinUndequeuedBufferCountLocked().
774 if (create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
775 min_undequeued_buffers += 1;
776
Jesse Halld7b994a2015-09-07 14:17:37 -0700777 uint32_t num_images =
778 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800779 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700780 if (err != 0) {
781 // TODO(jessehall): Improve error reporting. Can we enumerate possible
782 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700783 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
784 strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700785 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700786 }
787
Chris Forbes8c47dc92017-01-12 11:13:58 +1300788 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
Chris Forbesb4421522017-01-18 16:57:02 +1300789 if (create_info->presentMode == VK_PRESENT_MODE_FRONT_BUFFERED_DEMAND_REFRESH_KHR ||
790 create_info->presentMode == VK_PRESENT_MODE_FRONT_BUFFERED_CONTINUOUS_REFRESH_KHR) {
791 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_FRONT_BUFFER_BIT_ANDROID;
Chris Forbesb8042d22017-01-18 18:07:05 +1300792
793 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
794 if (err != 0) {
795 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
796 return VK_ERROR_INITIALIZATION_FAILED;
797 }
798 }
799
800 if (create_info->presentMode == VK_PRESENT_MODE_FRONT_BUFFERED_CONTINUOUS_REFRESH_KHR) {
801 err = native_window_set_auto_refresh(surface.window.get(), true);
802 if (err != 0) {
803 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
804 return VK_ERROR_INITIALIZATION_FAILED;
805 }
Chris Forbesb4421522017-01-18 16:57:02 +1300806 }
807
Jesse Hall70f93352015-11-04 09:41:31 -0800808 int gralloc_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +1300809 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
810 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
811 device, create_info->imageFormat, create_info->imageUsage,
812 swapchain_image_usage, &gralloc_usage);
813 if (result != VK_SUCCESS) {
814 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
815 return VK_ERROR_INITIALIZATION_FAILED;
816 }
817 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800818 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800819 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -0800820 &gralloc_usage);
821 if (result != VK_SUCCESS) {
822 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Jesse Hall70f93352015-11-04 09:41:31 -0800823 return VK_ERROR_INITIALIZATION_FAILED;
824 }
825 } else {
826 gralloc_usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
827 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800828 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -0800829 if (err != 0) {
830 // TODO(jessehall): Improve error reporting. Can we enumerate possible
831 // errors and translate them to valid Vulkan result codes?
832 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Jesse Hall70f93352015-11-04 09:41:31 -0800833 return VK_ERROR_INITIALIZATION_FAILED;
834 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700835
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700836 int swap_interval =
837 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
838 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800839 if (err != 0) {
840 // TODO(jessehall): Improve error reporting. Can we enumerate possible
841 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700842 ALOGE("native_window->setSwapInterval(%d) failed: %s (%d)",
843 swap_interval, strerror(-err), err);
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800844 return VK_ERROR_INITIALIZATION_FAILED;
845 }
846
Jesse Halld7b994a2015-09-07 14:17:37 -0700847 // -- Allocate our Swapchain object --
848 // After this point, we must deallocate the swapchain on error.
849
Jesse Hall1f91d392015-12-11 16:28:44 -0800850 void* mem = allocator->pfnAllocation(allocator->pUserData,
851 sizeof(Swapchain), alignof(Swapchain),
852 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800853 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -0700854 return VK_ERROR_OUT_OF_HOST_MEMORY;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800855 Swapchain* swapchain = new (mem) Swapchain(surface, num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700856
857 // -- Dequeue all buffers and create a VkImage for each --
858 // Any failures during or after this must cancel the dequeued buffers.
859
Chris Forbesb56287a2017-01-12 14:28:58 +1300860 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
861#pragma clang diagnostic push
862#pragma clang diagnostic ignored "-Wold-style-cast"
863 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
864#pragma clang diagnostic pop
865 .pNext = nullptr,
866 .usage = swapchain_image_usage,
867 };
Jesse Halld7b994a2015-09-07 14:17:37 -0700868 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -0700869#pragma clang diagnostic push
870#pragma clang diagnostic ignored "-Wold-style-cast"
871 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
872#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +1300873 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -0700874 };
875 VkImageCreateInfo image_create = {
876 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
877 .pNext = &image_native_buffer,
878 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -0800879 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -0700880 .extent = {0, 0, 1},
881 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -0800882 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -0800883 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -0700884 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800885 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -0700886 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -0800887 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -0800888 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -0700889 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
890 };
891
Jesse Halld7b994a2015-09-07 14:17:37 -0700892 for (uint32_t i = 0; i < num_images; i++) {
893 Swapchain::Image& img = swapchain->images[i];
894
895 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800896 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
897 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700898 if (err != 0) {
899 // TODO(jessehall): Improve error reporting. Can we enumerate
900 // possible errors and translate them to valid Vulkan result codes?
901 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -0700902 result = VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -0700903 break;
904 }
Chia-I Wue8e689f2016-04-18 08:21:31 +0800905 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700906 img.dequeued = true;
907
908 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -0800909 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
910 static_cast<uint32_t>(img.buffer->height),
911 1};
Jesse Halld7b994a2015-09-07 14:17:37 -0700912 image_native_buffer.handle = img.buffer->handle;
913 image_native_buffer.stride = img.buffer->stride;
914 image_native_buffer.format = img.buffer->format;
915 image_native_buffer.usage = img.buffer->usage;
916
Jesse Hall03b6fe12015-11-24 12:44:21 -0800917 result =
Jesse Hall1f91d392015-12-11 16:28:44 -0800918 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -0700919 if (result != VK_SUCCESS) {
920 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
921 break;
922 }
923 }
924
925 // -- Cancel all buffers, returning them to the queue --
926 // If an error occurred before, also destroy the VkImage and release the
927 // buffer reference. Otherwise, we retain a strong reference to the buffer.
928 //
929 // TODO(jessehall): The error path here is the same as DestroySwapchain,
930 // but not the non-error path. Should refactor/unify.
931 for (uint32_t i = 0; i < num_images; i++) {
932 Swapchain::Image& img = swapchain->images[i];
933 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800934 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
935 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -0700936 img.dequeue_fence = -1;
937 img.dequeued = false;
938 }
939 if (result != VK_SUCCESS) {
940 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -0800941 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -0700942 }
943 }
944
945 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700946 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800947 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -0700948 return result;
949 }
950
Jesse Halldc225072016-05-30 22:40:14 -0700951 surface.swapchain_handle = HandleFromSwapchain(swapchain);
952 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700953 return VK_SUCCESS;
954}
955
Jesse Halle1b12782015-11-30 11:27:32 -0800956VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800957void DestroySwapchainKHR(VkDevice device,
958 VkSwapchainKHR swapchain_handle,
959 const VkAllocationCallbacks* allocator) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800960 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -0700961 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -0500962 if (!swapchain)
963 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -0700964 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
965 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -0700966
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700967 if (swapchain->frame_timestamps_enabled) {
968 native_window_enable_frame_timestamps(window, false);
969 }
Jesse Halldc225072016-05-30 22:40:14 -0700970 for (uint32_t i = 0; i < swapchain->num_images; i++)
971 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700972 if (active)
Jesse Halldc225072016-05-30 22:40:14 -0700973 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -0800974 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800975 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -0700976 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -0800977 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700978}
979
Jesse Halle1b12782015-11-30 11:27:32 -0800980VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800981VkResult GetSwapchainImagesKHR(VkDevice,
982 VkSwapchainKHR swapchain_handle,
983 uint32_t* count,
984 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700985 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -0700986 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
987 "getting images for non-active swapchain 0x%" PRIx64
988 "; only dequeued image handles are valid",
989 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -0700990 VkResult result = VK_SUCCESS;
991 if (images) {
992 uint32_t n = swapchain.num_images;
993 if (*count < swapchain.num_images) {
994 n = *count;
995 result = VK_INCOMPLETE;
996 }
997 for (uint32_t i = 0; i < n; i++)
998 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -0700999 *count = n;
1000 } else {
1001 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001002 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001003 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001004}
1005
Jesse Halle1b12782015-11-30 11:27:32 -08001006VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001007VkResult AcquireNextImageKHR(VkDevice device,
1008 VkSwapchainKHR swapchain_handle,
1009 uint64_t timeout,
1010 VkSemaphore semaphore,
1011 VkFence vk_fence,
1012 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001013 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001014 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001015 VkResult result;
1016 int err;
1017
Jesse Halldc225072016-05-30 22:40:14 -07001018 if (swapchain.surface.swapchain_handle != swapchain_handle)
1019 return VK_ERROR_OUT_OF_DATE_KHR;
1020
Jesse Halld7b994a2015-09-07 14:17:37 -07001021 ALOGW_IF(
1022 timeout != UINT64_MAX,
1023 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1024
1025 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001026 int fence_fd;
1027 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001028 if (err != 0) {
1029 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1030 // errors and translate them to valid Vulkan result codes?
1031 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001032 return VK_ERROR_INITIALIZATION_FAILED;
Jesse Halld7b994a2015-09-07 14:17:37 -07001033 }
1034
1035 uint32_t idx;
1036 for (idx = 0; idx < swapchain.num_images; idx++) {
1037 if (swapchain.images[idx].buffer.get() == buffer) {
1038 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001039 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001040 break;
1041 }
1042 }
1043 if (idx == swapchain.num_images) {
1044 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001045 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001046 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001047 }
1048
1049 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001050 if (fence_fd != -1) {
1051 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001052 if (fence_clone == -1) {
1053 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1054 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001055 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001056 }
1057 }
1058
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001059 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001060 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001061 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001062 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1063 // even if the call fails. We could close it ourselves on failure, but
1064 // that would create a race condition if the driver closes it on a
1065 // failure path: some other thread might create an fd with the same
1066 // number between the time the driver closes it and the time we close
1067 // it. We must assume one of: the driver *always* closes it even on
1068 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001069 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001070 swapchain.images[idx].dequeued = false;
1071 swapchain.images[idx].dequeue_fence = -1;
1072 return result;
1073 }
1074
1075 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001076 return VK_SUCCESS;
1077}
1078
Jesse Halldc225072016-05-30 22:40:14 -07001079static VkResult WorstPresentResult(VkResult a, VkResult b) {
1080 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1081 // (in spec version 1.0.14).
1082 static const VkResult kWorstToBest[] = {
1083 VK_ERROR_DEVICE_LOST,
1084 VK_ERROR_SURFACE_LOST_KHR,
1085 VK_ERROR_OUT_OF_DATE_KHR,
1086 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1087 VK_ERROR_OUT_OF_HOST_MEMORY,
1088 VK_SUBOPTIMAL_KHR,
1089 };
1090 for (auto result : kWorstToBest) {
1091 if (a == result || b == result)
1092 return result;
1093 }
1094 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1095 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1096 return a != VK_SUCCESS ? a : b;
1097}
1098
Jesse Halle1b12782015-11-30 11:27:32 -08001099VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001100VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001101 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1102 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1103 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001104
Jesse Halldc225072016-05-30 22:40:14 -07001105 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001106 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001107 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001108
Ian Elliottcb351132016-12-13 10:30:40 -07001109 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001110 const VkPresentRegionsKHR* present_regions = nullptr;
1111 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001112 const VkPresentRegionsKHR* next =
1113 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1114 while (next) {
1115 switch (next->sType) {
1116 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1117 present_regions = next;
1118 break;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001119 case VK_STRUCTURE_TYPE_PRESENT_TIMES_GOOGLE:
1120 present_times =
1121 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1122 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001123 default:
1124 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1125 next->sType);
1126 break;
1127 }
1128 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1129 }
1130 ALOGV_IF(
1131 present_regions &&
1132 present_regions->swapchainCount != present_info->swapchainCount,
1133 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001134 ALOGV_IF(present_times &&
1135 present_times->swapchainCount != present_info->swapchainCount,
1136 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1137 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001138 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001139 (present_regions) ? present_regions->pRegions : nullptr;
1140 const VkPresentTimeGOOGLE* times =
1141 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001142 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001143 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001144 uint32_t nrects = 0;
1145
Jesse Halld7b994a2015-09-07 14:17:37 -07001146 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1147 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001148 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001149 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001150 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001151 const VkPresentRegionKHR* region = (regions) ? &regions[sc] : nullptr;
1152 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001153 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001154 VkResult result;
1155 int err;
1156
Jesse Halld7b994a2015-09-07 14:17:37 -07001157 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001158 result = dispatch.QueueSignalReleaseImageANDROID(
1159 queue, present_info->waitSemaphoreCount,
1160 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001161 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001162 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001163 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001164 }
1165
Jesse Halldc225072016-05-30 22:40:14 -07001166 if (swapchain.surface.swapchain_handle ==
1167 present_info->pSwapchains[sc]) {
1168 ANativeWindow* window = swapchain.surface.window.get();
1169 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001170 if (region) {
1171 // Process the incremental-present hint for this swapchain:
1172 uint32_t rcount = region->rectangleCount;
1173 if (rcount > nrects) {
1174 android_native_rect_t* new_rects =
1175 static_cast<android_native_rect_t*>(
1176 allocator->pfnReallocation(
1177 allocator->pUserData, rects,
1178 sizeof(android_native_rect_t) * rcount,
1179 alignof(android_native_rect_t),
1180 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1181 if (new_rects) {
1182 rects = new_rects;
1183 nrects = rcount;
1184 } else {
1185 rcount = 0; // Ignore the hint for this swapchain
1186 }
1187 }
1188 for (uint32_t r = 0; r < rcount; ++r) {
1189 if (region->pRectangles[r].layer > 0) {
1190 ALOGV(
1191 "vkQueuePresentKHR ignoring invalid layer "
1192 "(%u); using layer 0 instead",
1193 region->pRectangles[r].layer);
1194 }
1195 int x = region->pRectangles[r].offset.x;
1196 int y = region->pRectangles[r].offset.y;
1197 int width = static_cast<int>(
1198 region->pRectangles[r].extent.width);
1199 int height = static_cast<int>(
1200 region->pRectangles[r].extent.height);
1201 android_native_rect_t* cur_rect = &rects[r];
1202 cur_rect->left = x;
1203 cur_rect->top = y + height;
1204 cur_rect->right = x + width;
1205 cur_rect->bottom = y;
1206 }
1207 native_window_set_surface_damage(window, rects, rcount);
1208 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001209 if (time) {
1210 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001211 ALOGV(
1212 "Calling "
1213 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001214 native_window_enable_frame_timestamps(window, true);
1215 swapchain.frame_timestamps_enabled = true;
1216 }
Ian Elliott8a977262017-01-19 09:05:58 -07001217 // Record this presentID and desiredPresentTime so it can
1218 // be later correlated to this present.
1219 TimingInfo timing_record(time);
1220 swapchain.timing.add(timing_record);
1221 uint32_t num_timings =
1222 static_cast<uint32_t>(swapchain.timing.size());
1223 if (num_timings > MAX_TIMING_INFOS) {
1224 swapchain.timing.removeAt(0);
1225 }
1226 if (time->desiredPresentTime) {
1227 // Set the desiredPresentTime:
1228 ALOGV(
1229 "Calling "
1230 "native_window_set_buffers_timestamp(%" PRId64 ")",
1231 time->desiredPresentTime);
1232 native_window_set_buffers_timestamp(
1233 window,
1234 static_cast<int64_t>(time->desiredPresentTime));
1235 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001236 }
Jesse Halldc225072016-05-30 22:40:14 -07001237 err = window->queueBuffer(window, img.buffer.get(), fence);
1238 // queueBuffer always closes fence, even on error
1239 if (err != 0) {
1240 // TODO(jessehall): What now? We should probably cancel the
1241 // buffer, I guess?
1242 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1243 swapchain_result = WorstPresentResult(
1244 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1245 }
1246 if (img.dequeue_fence >= 0) {
1247 close(img.dequeue_fence);
1248 img.dequeue_fence = -1;
1249 }
1250 img.dequeued = false;
1251 }
1252 if (swapchain_result != VK_SUCCESS) {
1253 ReleaseSwapchainImage(device, window, fence, img);
1254 OrphanSwapchain(device, &swapchain);
1255 }
1256 } else {
1257 ReleaseSwapchainImage(device, nullptr, fence, img);
1258 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001259 }
1260
Jesse Halla9e57032015-11-30 01:03:10 -08001261 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001262 present_info->pResults[sc] = swapchain_result;
1263
1264 if (swapchain_result != final_result)
1265 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001266 }
Ian Elliottcb351132016-12-13 10:30:40 -07001267 if (rects) {
1268 allocator->pfnFree(allocator->pUserData, rects);
1269 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001270
1271 return final_result;
1272}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001273
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001274VKAPI_ATTR
1275VkResult GetRefreshCycleDurationGOOGLE(
1276 VkDevice,
1277 VkSwapchainKHR,
1278 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
1279 VkResult result = VK_SUCCESS;
1280
1281 // TODO(ianelliott): FULLY IMPLEMENT THIS FUNCTION!!!
Ian Elliott8a977262017-01-19 09:05:58 -07001282 pDisplayTimingProperties->minRefreshDuration = 16666666;
1283 pDisplayTimingProperties->maxRefreshDuration = 16666666;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001284
1285 return result;
1286}
1287
1288VKAPI_ATTR
1289VkResult GetPastPresentationTimingGOOGLE(
1290 VkDevice,
1291 VkSwapchainKHR swapchain_handle,
1292 uint32_t* count,
1293 VkPastPresentationTimingGOOGLE* timings) {
1294 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1295 ANativeWindow* window = swapchain.surface.window.get();
1296 VkResult result = VK_SUCCESS;
1297
1298 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001299 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001300 native_window_enable_frame_timestamps(window, true);
1301 swapchain.frame_timestamps_enabled = true;
1302 }
1303
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001304 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001305 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1306 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001307 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001308 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001309 }
1310
1311 return result;
1312}
1313
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001314VKAPI_ATTR
1315VkResult GetSwapchainStatusKHR(
1316 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001317 VkSwapchainKHR swapchain_handle) {
1318 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001319 VkResult result = VK_SUCCESS;
1320
Chris Forbes4e18ba82017-01-20 12:50:17 +13001321 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1322 return VK_ERROR_OUT_OF_DATE_KHR;
1323 }
1324
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001325 // TODO(chrisforbes): Implement this function properly
1326
1327 return result;
1328}
1329
Chia-I Wu62262232016-03-26 07:06:44 +08001330} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001331} // namespace vulkan