blob: 5574da2165da3a1df41016643f24a2627b9ce173 [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
Jesse Hall79927812017-03-23 11:03:23 -070019#include <grallocusage/GrallocUsageConversion.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070020#include <log/log.h>
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -080021#include <ui/BufferQueueDefs.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070022#include <sync/sync.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080023#include <utils/StrongPointer.h>
Brian Anderson1049d1d2016-12-16 17:25:57 -080024#include <utils/Vector.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070025
Chia-I Wu4a6a9162016-03-26 07:17:34 +080026#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070027
Jesse Hall5ae3abb2015-10-08 14:00:22 -070028// TODO(jessehall): Currently we don't have a good error code for when a native
29// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
30// versions (post SDK 0.9) of the API/extension have a better error code.
31// When updating to that version, audit all error returns.
Chia-I Wu62262232016-03-26 07:06:44 +080032namespace vulkan {
33namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070034
Jesse Halld7b994a2015-09-07 14:17:37 -070035namespace {
36
Jesse Hall55bc0972016-02-23 16:43:29 -080037const VkSurfaceTransformFlagsKHR kSupportedTransforms =
38 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
39 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
40 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
41 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
42 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
43 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
44 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
45 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
46 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
47 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
48
49VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
50 // Native and Vulkan transforms are isomorphic, but are represented
51 // differently. Vulkan transforms are built up of an optional horizontal
52 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
53 // transforms are built up from a horizontal flip, vertical flip, and
54 // 90-degree rotation, all optional but always in that order.
55
56 // TODO(jessehall): For now, only support pure rotations, not
57 // flip or flip-and-rotate, until I have more time to test them and build
58 // sample code. As far as I know we never actually use anything besides
59 // pure rotations anyway.
60
61 switch (native) {
62 case 0: // 0x0
63 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
64 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
65 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
66 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
67 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
68 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
69 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
70 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
71 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
72 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
73 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
74 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
75 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
76 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
77 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
78 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
79 default:
80 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
81 }
82}
83
Jesse Hall178b6962016-02-24 15:39:50 -080084int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
85 switch (transform) {
86 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
87 return NATIVE_WINDOW_TRANSFORM_ROT_270;
88 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
89 return NATIVE_WINDOW_TRANSFORM_ROT_180;
90 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
91 return NATIVE_WINDOW_TRANSFORM_ROT_90;
92 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
93 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
94 // return NATIVE_WINDOW_TRANSFORM_FLIP_H;
95 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
96 // return NATIVE_WINDOW_TRANSFORM_FLIP_H |
97 // NATIVE_WINDOW_TRANSFORM_ROT_90;
98 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
99 // return NATIVE_WINDOW_TRANSFORM_FLIP_V;
100 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
101 // return NATIVE_WINDOW_TRANSFORM_FLIP_V |
102 // NATIVE_WINDOW_TRANSFORM_ROT_90;
103 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
104 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
105 default:
106 return 0;
107 }
108}
109
Ian Elliott8a977262017-01-19 09:05:58 -0700110class TimingInfo {
111 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800112 TimingInfo() = default;
113 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700114 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800115 native_frame_id_(nativeFrameId) {}
116 bool ready() const {
Ian Elliott8a977262017-01-19 09:05:58 -0700117 return (timestamp_desired_present_time_ &&
118 timestamp_actual_present_time_ &&
119 timestamp_render_complete_time_ &&
120 timestamp_composition_latch_time_);
121 }
122 void calculate(uint64_t rdur) {
123 vals_.actualPresentTime = timestamp_actual_present_time_;
124 uint64_t margin = (timestamp_composition_latch_time_ -
125 timestamp_render_complete_time_);
126 // Calculate vals_.earliestPresentTime, and potentially adjust
127 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
128 // is vals_.actualPresentTime. If we can subtract rdur (the duration
129 // of a refresh cycle) from vals_.earliestPresentTime (and also from
130 // vals_.presentMargin) and still leave a positive margin, then we can
131 // report to the application that it could have presented earlier than
132 // it did (per the extension specification). If for some reason, we
133 // can do this subtraction repeatedly, we do, since
134 // vals_.earliestPresentTime really is supposed to be the "earliest".
135 uint64_t early_time = vals_.actualPresentTime;
136 while ((margin > rdur) &&
137 ((early_time - rdur) > timestamp_composition_latch_time_)) {
138 early_time -= rdur;
139 margin -= rdur;
140 }
141 vals_.earliestPresentTime = early_time;
142 vals_.presentMargin = margin;
143 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800144 void get_values(VkPastPresentationTimingGOOGLE* values) const {
145 *values = vals_;
146 }
Ian Elliott8a977262017-01-19 09:05:58 -0700147
148 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800149 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700150
Brian Anderson1049d1d2016-12-16 17:25:57 -0800151 uint64_t native_frame_id_ { 0 };
152 uint64_t timestamp_desired_present_time_ { 0 };
153 uint64_t timestamp_actual_present_time_ { 0 };
154 uint64_t timestamp_render_complete_time_ { 0 };
155 uint64_t timestamp_composition_latch_time_ { 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700156};
157
Jesse Halld7b994a2015-09-07 14:17:37 -0700158// ----------------------------------------------------------------------------
159
Jesse Hall1356b0d2015-11-23 17:24:58 -0800160struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800161 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700162 VkSwapchainKHR swapchain_handle;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800163};
164
165VkSurfaceKHR HandleFromSurface(Surface* surface) {
166 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
167}
168
169Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800170 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800171}
172
Ian Elliott8a977262017-01-19 09:05:58 -0700173// Maximum number of TimingInfo structs to keep per swapchain:
174enum { MAX_TIMING_INFOS = 10 };
175// Minimum number of frames to look for in the past (so we don't cause
176// syncronous requests to Surface Flinger):
177enum { MIN_NUM_FRAMES_AGO = 5 };
178
Jesse Hall1356b0d2015-11-23 17:24:58 -0800179struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700180 Swapchain(Surface& surface_,
181 uint32_t num_images_,
182 VkPresentModeKHR present_mode)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700183 : surface(surface_),
184 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700185 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
Ian Elliott8a977262017-01-19 09:05:58 -0700186 frame_timestamps_enabled(false) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700187 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700188 int64_t rdur;
189 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700190 window,
Ian Elliottbe833a22017-01-25 13:09:20 -0700191 &rdur);
192 refresh_duration = static_cast<uint64_t>(rdur);
Ian Elliott8a977262017-01-19 09:05:58 -0700193 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800194
195 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700196 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700197 bool mailbox_mode;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700198 bool frame_timestamps_enabled;
Ian Elliottbe833a22017-01-25 13:09:20 -0700199 uint64_t refresh_duration;
Jesse Halld7b994a2015-09-07 14:17:37 -0700200
201 struct Image {
202 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
203 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800204 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700205 // The fence is only valid when the buffer is dequeued, and should be
206 // -1 any other time. When valid, we own the fd, and must ensure it is
207 // closed: either by closing it explicitly when queueing the buffer,
208 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
209 int dequeue_fence;
210 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800211 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700212
Brian Anderson1049d1d2016-12-16 17:25:57 -0800213 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700214};
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
Jesse Halldc225072016-05-30 22:40:14 -0700224void ReleaseSwapchainImage(VkDevice device,
225 ANativeWindow* window,
226 int release_fence,
227 Swapchain::Image& image) {
228 ALOG_ASSERT(release_fence == -1 || image.dequeued,
229 "ReleaseSwapchainImage: can't provide a release fence for "
230 "non-dequeued images");
231
232 if (image.dequeued) {
233 if (release_fence >= 0) {
234 // We get here from vkQueuePresentKHR. The application is
235 // responsible for creating an execution dependency chain from
236 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
237 // (release_fence), so we can drop the dequeue_fence here.
238 if (image.dequeue_fence >= 0)
239 close(image.dequeue_fence);
240 } else {
241 // We get here during swapchain destruction, or various serious
242 // error cases e.g. when we can't create the release_fence during
243 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
244 // have already signalled, since the swapchain images are supposed
245 // to be idle before the swapchain is destroyed. In error cases,
246 // there may be rendering in flight to the image, but since we
247 // weren't able to create a release_fence, waiting for the
248 // dequeue_fence is about the best we can do.
249 release_fence = image.dequeue_fence;
250 }
251 image.dequeue_fence = -1;
252
253 if (window) {
254 window->cancelBuffer(window, image.buffer.get(), release_fence);
255 } else {
256 if (release_fence >= 0) {
257 sync_wait(release_fence, -1 /* forever */);
258 close(release_fence);
259 }
260 }
261
262 image.dequeued = false;
263 }
264
265 if (image.image) {
266 GetData(device).driver.DestroyImage(device, image.image, nullptr);
267 image.image = VK_NULL_HANDLE;
268 }
269
270 image.buffer.clear();
271}
272
273void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
274 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
275 return;
Jesse Halldc225072016-05-30 22:40:14 -0700276 for (uint32_t i = 0; i < swapchain->num_images; i++) {
277 if (!swapchain->images[i].dequeued)
278 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
279 }
280 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700281 swapchain->timing.clear();
282}
283
284uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800285 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
286 return 0;
287 }
Ian Elliott8a977262017-01-19 09:05:58 -0700288
Brian Anderson1049d1d2016-12-16 17:25:57 -0800289 uint32_t num_ready = 0;
290 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
291 for (uint32_t i = 0; i < num_timings; i++) {
292 TimingInfo& ti = swapchain.timing.editItemAt(i);
293 if (ti.ready()) {
294 // This TimingInfo is ready to be reported to the user. Add it
295 // to the num_ready.
296 num_ready++;
297 continue;
298 }
299 // This TimingInfo is not yet ready to be reported to the user,
300 // and so we should look for any available timestamps that
301 // might make it ready.
302 int64_t desired_present_time = 0;
303 int64_t render_complete_time = 0;
304 int64_t composition_latch_time = 0;
305 int64_t actual_present_time = 0;
306 // Obtain timestamps:
307 int ret = native_window_get_frame_timestamps(
308 swapchain.surface.window.get(), ti.native_frame_id_,
309 &desired_present_time, &render_complete_time,
310 &composition_latch_time,
311 NULL, //&first_composition_start_time,
312 NULL, //&last_composition_start_time,
313 NULL, //&composition_finish_time,
314 // TODO(ianelliott): Maybe ask if this one is
315 // supported, at startup time (since it may not be
316 // supported):
317 &actual_present_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800318 NULL, //&dequeue_ready_time,
319 NULL /*&reads_done_time*/);
320
321 if (ret != android::NO_ERROR) {
322 continue;
323 }
324
325 // Record the timestamp(s) we received, and then see if this TimingInfo
326 // is ready to be reported to the user:
327 ti.timestamp_desired_present_time_ =
328 static_cast<uint64_t>(desired_present_time);
329 ti.timestamp_actual_present_time_ =
330 static_cast<uint64_t>(actual_present_time);
331 ti.timestamp_render_complete_time_ =
332 static_cast<uint64_t>(render_complete_time);
333 ti.timestamp_composition_latch_time_ =
334 static_cast<uint64_t>(composition_latch_time);
335
336 if (ti.ready()) {
337 // The TimingInfo has received enough timestamps, and should now
338 // use those timestamps to calculate the info that should be
339 // reported to the user:
340 ti.calculate(swapchain.refresh_duration);
341 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700342 }
343 }
344 return num_ready;
345}
346
347// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
348void copy_ready_timings(Swapchain& swapchain,
349 uint32_t* count,
350 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800351 if (swapchain.timing.empty()) {
352 *count = 0;
353 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700354 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800355
356 size_t last_ready = swapchain.timing.size() - 1;
357 while (!swapchain.timing[last_ready].ready()) {
358 if (last_ready == 0) {
359 *count = 0;
360 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700361 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800362 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700363 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800364
365 uint32_t num_copied = 0;
366 size_t num_to_remove = 0;
367 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
368 const TimingInfo& ti = swapchain.timing[i];
369 if (ti.ready()) {
370 ti.get_values(&timings[num_copied]);
371 num_copied++;
372 }
373 num_to_remove++;
374 }
375
376 // Discard old frames that aren't ready if newer frames are ready.
377 // We don't expect to get the timing info for those old frames.
378 swapchain.timing.removeItemsAt(0, num_to_remove);
379
Ian Elliott8a977262017-01-19 09:05:58 -0700380 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700381}
382
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700383android_pixel_format GetNativePixelFormat(VkFormat format) {
384 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
385 switch (format) {
386 case VK_FORMAT_R8G8B8A8_UNORM:
387 case VK_FORMAT_R8G8B8A8_SRGB:
388 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
389 break;
390 case VK_FORMAT_R5G6B5_UNORM_PACK16:
391 native_format = HAL_PIXEL_FORMAT_RGB_565;
392 break;
393 case VK_FORMAT_R16G16B16A16_SFLOAT:
394 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
395 break;
396 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
397 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
398 break;
399 default:
400 ALOGV("unsupported swapchain format %d", format);
401 break;
402 }
403 return native_format;
404}
405
406android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
407 switch (colorspace) {
408 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
409 return HAL_DATASPACE_V0_SRGB;
410 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
411 return HAL_DATASPACE_DISPLAY_P3;
412 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
413 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700414 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
415 return HAL_DATASPACE_DCI_P3_LINEAR;
416 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
417 return HAL_DATASPACE_DCI_P3;
418 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
419 return HAL_DATASPACE_V0_SRGB_LINEAR;
420 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
421 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600422 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
423 return HAL_DATASPACE_BT2020_LINEAR;
424 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700425 return static_cast<android_dataspace>(
426 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
427 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600428 case VK_COLOR_SPACE_DOLBYVISION_EXT:
429 return static_cast<android_dataspace>(
430 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
431 HAL_DATASPACE_RANGE_FULL);
432 case VK_COLOR_SPACE_HDR10_HLG_EXT:
433 return static_cast<android_dataspace>(
434 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
435 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700436 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
437 return static_cast<android_dataspace>(
438 HAL_DATASPACE_STANDARD_ADOBE_RGB |
439 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
440 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
441 return HAL_DATASPACE_ADOBE_RGB;
442
443 // Pass through is intended to allow app to provide data that is passed
444 // to the display system without modification.
445 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
446 return HAL_DATASPACE_ARBITRARY;
447
448 default:
449 // This indicates that we don't know about the
450 // dataspace specified and we should indicate that
451 // it's unsupported
452 return HAL_DATASPACE_UNKNOWN;
453 }
454}
455
Jesse Halld7b994a2015-09-07 14:17:37 -0700456} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700457
Jesse Halle1b12782015-11-30 11:27:32 -0800458VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800459VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800460 VkInstance instance,
461 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
462 const VkAllocationCallbacks* allocator,
463 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800464 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800465 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800466 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
467 alignof(Surface),
468 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800469 if (!mem)
470 return VK_ERROR_OUT_OF_HOST_MEMORY;
471 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700472
Chia-I Wue8e689f2016-04-18 08:21:31 +0800473 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700474 surface->swapchain_handle = VK_NULL_HANDLE;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700475
Jesse Hall1356b0d2015-11-23 17:24:58 -0800476 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
477 int err =
478 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
479 if (err != 0) {
480 // TODO(jessehall): Improve error reporting. Can we enumerate possible
481 // errors and translate them to valid Vulkan result codes?
482 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
483 err);
484 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800485 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700486 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800487 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700488
Jesse Hall1356b0d2015-11-23 17:24:58 -0800489 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700490 return VK_SUCCESS;
491}
492
Jesse Halle1b12782015-11-30 11:27:32 -0800493VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800494void DestroySurfaceKHR(VkInstance instance,
495 VkSurfaceKHR surface_handle,
496 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800497 Surface* surface = SurfaceFromHandle(surface_handle);
498 if (!surface)
499 return;
500 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700501 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700502 "destroyed VkSurfaceKHR 0x%" PRIx64
503 " has active VkSwapchainKHR 0x%" PRIx64,
504 reinterpret_cast<uint64_t>(surface_handle),
505 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800506 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800507 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800508 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800509 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800510}
511
Jesse Halle1b12782015-11-30 11:27:32 -0800512VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800513VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
514 uint32_t /*queue_family*/,
515 VkSurfaceKHR /*surface*/,
516 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800517 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800518 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800519}
520
Jesse Halle1b12782015-11-30 11:27:32 -0800521VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800522VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800523 VkPhysicalDevice /*pdev*/,
524 VkSurfaceKHR surface,
525 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700526 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800527 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700528
529 int width, height;
530 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
531 if (err != 0) {
532 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
533 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700534 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700535 }
536 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
537 if (err != 0) {
538 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
539 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700540 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700541 }
542
Jesse Hall55bc0972016-02-23 16:43:29 -0800543 int transform_hint;
544 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
545 if (err != 0) {
546 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
547 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700548 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800549 }
550
Jesse Halld7b994a2015-09-07 14:17:37 -0700551 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800552 capabilities->minImageCount = 2;
553 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700554
Jesse Hallfe2662d2016-02-09 13:26:59 -0800555 capabilities->currentExtent =
556 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
557
Jesse Halld7b994a2015-09-07 14:17:37 -0700558 // TODO(jessehall): Figure out what the max extent should be. Maximum
559 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800560 capabilities->minImageExtent = VkExtent2D{1, 1};
561 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700562
Jesse Hallfe2662d2016-02-09 13:26:59 -0800563 capabilities->maxImageArrayLayers = 1;
564
Jesse Hall55bc0972016-02-23 16:43:29 -0800565 capabilities->supportedTransforms = kSupportedTransforms;
566 capabilities->currentTransform =
567 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700568
Jesse Hallfe2662d2016-02-09 13:26:59 -0800569 // On Android, window composition is a WindowManager property, not something
570 // associated with the bufferqueue. It can't be changed from here.
571 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700572
573 // TODO(jessehall): I think these are right, but haven't thought hard about
574 // it. Do we need to query the driver for support of any of these?
575 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700576 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
577 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800578 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800579 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
580 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
581 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700582 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
583
Jesse Hallb1352bc2015-09-04 16:12:33 -0700584 return VK_SUCCESS;
585}
586
Jesse Halle1b12782015-11-30 11:27:32 -0800587VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700588VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
589 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800590 uint32_t* count,
591 VkSurfaceFormatKHR* formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700592 const InstanceData& instance_data = GetData(pdev);
593
Jesse Hall1356b0d2015-11-23 17:24:58 -0800594 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
595 // a new gralloc method to query whether a (format, usage) pair is
596 // supported, and check that for each gralloc format that corresponds to a
597 // Vulkan format. Shorter term, just add a few more formats to the ones
598 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700599
600 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700601 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
602 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
603 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700604 };
605 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700606 uint32_t total_num_formats = kNumFormats;
607
608 bool wide_color_support = false;
609 Surface& surface = *SurfaceFromHandle(surface_handle);
610 int err = native_window_get_wide_color_support(surface.window.get(),
611 &wide_color_support);
612 if (err) {
613 // Not allowed to return a more sensible error code, so do this
614 return VK_ERROR_OUT_OF_HOST_MEMORY;
615 }
616 ALOGV("wide_color_support is: %d", wide_color_support);
617 wide_color_support =
618 wide_color_support &&
619 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
620
621 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbca34c92017-02-17 11:31:23 -0700622 {VK_FORMAT_R16G16B16A16_SFLOAT,
623 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700624 {VK_FORMAT_A2R10G10B10_UNORM_PACK32,
625 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
626 };
627 const uint32_t kNumWideColorFormats =
628 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
629 if (wide_color_support) {
630 total_num_formats += kNumWideColorFormats;
631 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700632
633 VkResult result = VK_SUCCESS;
634 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700635 uint32_t out_count = 0;
636 uint32_t transfer_count = 0;
637 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700638 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700639 transfer_count = std::min(*count, kNumFormats);
640 std::copy(kFormats, kFormats + transfer_count, formats);
641 out_count += transfer_count;
642 if (wide_color_support) {
643 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
644 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
645 formats + out_count);
646 out_count += transfer_count;
647 }
648 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700649 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700650 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700651 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700652 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700653}
654
Jesse Halle1b12782015-11-30 11:27:32 -0800655VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300656VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
657 VkPhysicalDevice physicalDevice,
658 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
659 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
660 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
661 physicalDevice, pSurfaceInfo->surface,
662 &pSurfaceCapabilities->surfaceCapabilities);
663
Chris Forbes06bc0092017-03-16 16:46:05 +1300664 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
665 while (caps->pNext) {
666 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
667
668 switch (caps->sType) {
669 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
670 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
671 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
672 caps);
673 // Claim same set of usage flags are supported for
674 // shared present modes as for other modes.
675 shared_caps->sharedPresentSupportedUsageFlags =
676 pSurfaceCapabilities->surfaceCapabilities
677 .supportedUsageFlags;
678 } break;
679
680 default:
681 // Ignore all other extension structs
682 break;
683 }
684 }
685
Chris Forbes2452cf72017-03-16 16:30:17 +1300686 return result;
687}
688
689VKAPI_ATTR
690VkResult GetPhysicalDeviceSurfaceFormats2KHR(
691 VkPhysicalDevice physicalDevice,
692 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
693 uint32_t* pSurfaceFormatCount,
694 VkSurfaceFormat2KHR* pSurfaceFormats) {
695 if (!pSurfaceFormats) {
696 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
697 pSurfaceInfo->surface,
698 pSurfaceFormatCount, nullptr);
699 } else {
700 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
701 // after the call.
702 android::Vector<VkSurfaceFormatKHR> surface_formats;
703 surface_formats.resize(*pSurfaceFormatCount);
704 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
705 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
706 &surface_formats.editItemAt(0));
707
708 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
709 // marshal results individually due to stride difference.
710 // completely ignore any chained extension structs.
711 uint32_t formats_to_marshal = *pSurfaceFormatCount;
712 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
713 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
714 }
715 }
716
717 return result;
718 }
719}
720
721VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300722VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Chia-I Wu62262232016-03-26 07:06:44 +0800723 VkSurfaceKHR /*surface*/,
724 uint32_t* count,
725 VkPresentModeKHR* modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300726 android::Vector<VkPresentModeKHR> present_modes;
727 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
728 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
729
730 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
731 if (QueryPresentationProperties(pdev, &present_properties)) {
732 if (present_properties.sharedImage) {
733 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
734 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
735 }
736 }
737
738 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700739
740 VkResult result = VK_SUCCESS;
741 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300742 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700743 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300744 *count = std::min(*count, num_modes);
745 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700746 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300747 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700748 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700749 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700750}
751
Jesse Halle1b12782015-11-30 11:27:32 -0800752VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800753VkResult CreateSwapchainKHR(VkDevice device,
754 const VkSwapchainCreateInfoKHR* create_info,
755 const VkAllocationCallbacks* allocator,
756 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700757 int err;
758 VkResult result = VK_SUCCESS;
759
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700760 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
761 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
762 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
763 " oldSwapchain=0x%" PRIx64,
764 reinterpret_cast<uint64_t>(create_info->surface),
765 create_info->minImageCount, create_info->imageFormat,
766 create_info->imageColorSpace, create_info->imageExtent.width,
767 create_info->imageExtent.height, create_info->imageUsage,
768 create_info->preTransform, create_info->presentMode,
769 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
770
Jesse Hall1f91d392015-12-11 16:28:44 -0800771 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800772 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800773
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700774 android_pixel_format native_pixel_format =
775 GetNativePixelFormat(create_info->imageFormat);
776 android_dataspace native_dataspace =
777 GetNativeDataspace(create_info->imageColorSpace);
778 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
779 ALOGE(
780 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
781 "failed: Unsupported color space",
782 create_info->imageColorSpace);
783 return VK_ERROR_INITIALIZATION_FAILED;
784 }
785
Jesse Hall42a9eec2016-06-03 12:39:49 -0700786 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700787 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800788 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700789 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700790 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800791 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700792 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +1300793 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300794 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
795 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700796 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800797 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700798
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700799 Surface& surface = *SurfaceFromHandle(create_info->surface);
800
Jesse Halldc225072016-05-30 22:40:14 -0700801 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -0700802 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -0700803 " because it already has active swapchain 0x%" PRIx64
804 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
805 reinterpret_cast<uint64_t>(create_info->surface),
806 reinterpret_cast<uint64_t>(surface.swapchain_handle),
807 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
808 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
809 }
810 if (create_info->oldSwapchain != VK_NULL_HANDLE)
811 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
812
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700813 // -- Reset the native window --
814 // The native window might have been used previously, and had its properties
815 // changed from defaults. That will affect the answer we get for queries
816 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
817 // attempt such queries.
818
Jesse Halldc225072016-05-30 22:40:14 -0700819 // The native window only allows dequeueing all buffers before any have
820 // been queued, since after that point at least one is assumed to be in
821 // non-FREE state at any given time. Disconnecting and re-connecting
822 // orphans the previous buffers, getting us back to the state where we can
823 // dequeue all buffers.
824 err = native_window_api_disconnect(surface.window.get(),
825 NATIVE_WINDOW_API_EGL);
826 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
827 strerror(-err), err);
828 err =
829 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
830 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
831 strerror(-err), err);
832
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700833 err = native_window_set_buffer_count(surface.window.get(), 0);
834 if (err != 0) {
835 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
836 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700837 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700838 }
839
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +0530840 int swap_interval =
841 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
842 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700843 if (err != 0) {
844 // TODO(jessehall): Improve error reporting. Can we enumerate possible
845 // errors and translate them to valid Vulkan result codes?
846 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
847 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700848 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700849 }
850
Chris Forbesb8042d22017-01-18 18:07:05 +1300851 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
852 if (err != 0) {
853 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
854 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700855 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300856 }
857
858 err = native_window_set_auto_refresh(surface.window.get(), false);
859 if (err != 0) {
860 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
861 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700862 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300863 }
864
Jesse Halld7b994a2015-09-07 14:17:37 -0700865 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700866
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800867 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -0800868
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700869 err = native_window_set_buffers_format(surface.window.get(),
870 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -0800871 if (err != 0) {
872 // TODO(jessehall): Improve error reporting. Can we enumerate possible
873 // errors and translate them to valid Vulkan result codes?
874 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700875 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700876 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -0800877 }
878 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700879 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -0800880 if (err != 0) {
881 // TODO(jessehall): Improve error reporting. Can we enumerate possible
882 // errors and translate them to valid Vulkan result codes?
883 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700884 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700885 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -0800886 }
887
Jesse Hall3dd678a2016-01-08 21:52:01 -0800888 err = native_window_set_buffers_dimensions(
889 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
890 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700891 if (err != 0) {
892 // TODO(jessehall): Improve error reporting. Can we enumerate possible
893 // errors and translate them to valid Vulkan result codes?
894 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
895 create_info->imageExtent.width, create_info->imageExtent.height,
896 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700897 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700898 }
899
Jesse Hall178b6962016-02-24 15:39:50 -0800900 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
901 // applied during rendering. native_window_set_transform() expects the
902 // inverse: the transform the app is requesting that the compositor perform
903 // during composition. With native windows, pre-transform works by rendering
904 // with the same transform the compositor is applying (as in Vulkan), but
905 // then requesting the inverse transform, so that when the compositor does
906 // it's job the two transforms cancel each other out and the compositor ends
907 // up applying an identity transform to the app's buffer.
908 err = native_window_set_buffers_transform(
909 surface.window.get(),
910 InvertTransformToNative(create_info->preTransform));
911 if (err != 0) {
912 // TODO(jessehall): Improve error reporting. Can we enumerate possible
913 // errors and translate them to valid Vulkan result codes?
914 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
915 InvertTransformToNative(create_info->preTransform),
916 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700917 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -0800918 }
919
Jesse Hallf64ca122015-11-03 16:11:10 -0800920 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800921 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800922 if (err != 0) {
923 // TODO(jessehall): Improve error reporting. Can we enumerate possible
924 // errors and translate them to valid Vulkan result codes?
925 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
926 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700927 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -0800928 }
929
Jesse Halle6080bf2016-02-28 20:58:50 -0800930 int query_value;
931 err = surface.window->query(surface.window.get(),
932 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
933 &query_value);
934 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700935 // TODO(jessehall): Improve error reporting. Can we enumerate possible
936 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -0800937 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
938 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -0700939 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700940 }
Jesse Halle6080bf2016-02-28 20:58:50 -0800941 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -0700942 uint32_t num_images =
943 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800944 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700945 if (err != 0) {
946 // TODO(jessehall): Improve error reporting. Can we enumerate possible
947 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700948 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
949 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700950 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700951 }
952
Chris Forbes8c47dc92017-01-12 11:13:58 +1300953 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300954 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
955 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Chris Forbes4da65b92017-01-31 11:48:50 +1300956 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Chris Forbesb8042d22017-01-18 18:07:05 +1300957
958 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
959 if (err != 0) {
960 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700961 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300962 }
963 }
964
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300965 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Chris Forbesb8042d22017-01-18 18:07:05 +1300966 err = native_window_set_auto_refresh(surface.window.get(), true);
967 if (err != 0) {
968 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700969 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300970 }
Chris Forbesb4421522017-01-18 16:57:02 +1300971 }
972
Jesse Hall70f93352015-11-04 09:41:31 -0800973 int gralloc_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +1300974 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -0800975 uint64_t consumer_usage, producer_usage;
Steve Pfetsch72957a92017-03-13 22:57:15 +0000976 if (GetData(device).driver_version == 256587285) {
Jesse Hall85bb0c52017-02-09 22:13:02 -0800977 // HACK workaround for loader/driver mismatch during transition to
978 // vkGetSwapchainGrallocUsage2ANDROID.
979 typedef VkResult(VKAPI_PTR *
980 PFN_vkGetSwapchainGrallocUsage2ANDROID_HACK)(
981 VkDevice device, VkFormat format, VkImageUsageFlags imageUsage,
982 uint64_t * grallocConsumerUsage,
983 uint64_t * grallocProducerUsage);
984 auto get_swapchain_gralloc_usage =
985 reinterpret_cast<PFN_vkGetSwapchainGrallocUsage2ANDROID_HACK>(
986 dispatch.GetSwapchainGrallocUsage2ANDROID);
987 result = get_swapchain_gralloc_usage(
988 device, create_info->imageFormat, create_info->imageUsage,
989 &consumer_usage, &producer_usage);
990 } else {
991 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
992 device, create_info->imageFormat, create_info->imageUsage,
993 swapchain_image_usage, &consumer_usage, &producer_usage);
994 }
Chris Forbes8c47dc92017-01-12 11:13:58 +1300995 if (result != VK_SUCCESS) {
996 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -0700997 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +1300998 }
Jesse Halld1abd742017-02-09 21:45:51 -0800999 gralloc_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001000 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001001 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001002 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001003 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -08001004 &gralloc_usage);
1005 if (result != VK_SUCCESS) {
1006 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001007 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001008 }
Jesse Hall70f93352015-11-04 09:41:31 -08001009 }
Jesse Hall1356b0d2015-11-23 17:24:58 -08001010 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001011 if (err != 0) {
1012 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1013 // errors and translate them to valid Vulkan result codes?
1014 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001015 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001016 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001017
1018 // -- Allocate our Swapchain object --
1019 // After this point, we must deallocate the swapchain on error.
1020
Jesse Hall1f91d392015-12-11 16:28:44 -08001021 void* mem = allocator->pfnAllocation(allocator->pUserData,
1022 sizeof(Swapchain), alignof(Swapchain),
1023 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001024 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001025 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001026 Swapchain* swapchain =
1027 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001028
1029 // -- Dequeue all buffers and create a VkImage for each --
1030 // Any failures during or after this must cancel the dequeued buffers.
1031
Chris Forbesb56287a2017-01-12 14:28:58 +13001032 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1033#pragma clang diagnostic push
1034#pragma clang diagnostic ignored "-Wold-style-cast"
1035 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1036#pragma clang diagnostic pop
1037 .pNext = nullptr,
1038 .usage = swapchain_image_usage,
1039 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001040 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001041#pragma clang diagnostic push
1042#pragma clang diagnostic ignored "-Wold-style-cast"
1043 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1044#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001045 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001046 };
1047 VkImageCreateInfo image_create = {
1048 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1049 .pNext = &image_native_buffer,
1050 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001051 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001052 .extent = {0, 0, 1},
1053 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001054 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001055 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001056 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001057 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -07001058 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001059 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001060 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001061 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1062 };
1063
Jesse Halld7b994a2015-09-07 14:17:37 -07001064 for (uint32_t i = 0; i < num_images; i++) {
1065 Swapchain::Image& img = swapchain->images[i];
1066
1067 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001068 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1069 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001070 if (err != 0) {
1071 // TODO(jessehall): Improve error reporting. Can we enumerate
1072 // possible errors and translate them to valid Vulkan result codes?
1073 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001074 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001075 break;
1076 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001077 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001078 img.dequeued = true;
1079
1080 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001081 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1082 static_cast<uint32_t>(img.buffer->height),
1083 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001084 image_native_buffer.handle = img.buffer->handle;
1085 image_native_buffer.stride = img.buffer->stride;
1086 image_native_buffer.format = img.buffer->format;
1087 image_native_buffer.usage = img.buffer->usage;
Jesse Halld1abd742017-02-09 21:45:51 -08001088 // TODO: Adjust once ANativeWindowBuffer supports gralloc1-style usage.
1089 // For now, this is the same translation Gralloc1On0Adapter does.
1090 image_native_buffer.usage2.consumer =
1091 static_cast<uint64_t>(img.buffer->usage);
1092 image_native_buffer.usage2.producer =
1093 static_cast<uint64_t>(img.buffer->usage);
Jesse Halld7b994a2015-09-07 14:17:37 -07001094
Jesse Hall03b6fe12015-11-24 12:44:21 -08001095 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001096 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -07001097 if (result != VK_SUCCESS) {
1098 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1099 break;
1100 }
1101 }
1102
1103 // -- Cancel all buffers, returning them to the queue --
1104 // If an error occurred before, also destroy the VkImage and release the
1105 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1106 //
1107 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1108 // but not the non-error path. Should refactor/unify.
1109 for (uint32_t i = 0; i < num_images; i++) {
1110 Swapchain::Image& img = swapchain->images[i];
1111 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -08001112 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1113 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001114 img.dequeue_fence = -1;
1115 img.dequeued = false;
1116 }
1117 if (result != VK_SUCCESS) {
1118 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -08001119 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -07001120 }
1121 }
1122
1123 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001124 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001125 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001126 return result;
1127 }
1128
Jesse Halldc225072016-05-30 22:40:14 -07001129 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1130 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001131 return VK_SUCCESS;
1132}
1133
Jesse Halle1b12782015-11-30 11:27:32 -08001134VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001135void DestroySwapchainKHR(VkDevice device,
1136 VkSwapchainKHR swapchain_handle,
1137 const VkAllocationCallbacks* allocator) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001138 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001139 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001140 if (!swapchain)
1141 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001142 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1143 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001144
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001145 if (swapchain->frame_timestamps_enabled) {
1146 native_window_enable_frame_timestamps(window, false);
1147 }
Jesse Halldc225072016-05-30 22:40:14 -07001148 for (uint32_t i = 0; i < swapchain->num_images; i++)
1149 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001150 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001151 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001152 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001153 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001154 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001155 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001156}
1157
Jesse Halle1b12782015-11-30 11:27:32 -08001158VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001159VkResult GetSwapchainImagesKHR(VkDevice,
1160 VkSwapchainKHR swapchain_handle,
1161 uint32_t* count,
1162 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001163 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001164 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1165 "getting images for non-active swapchain 0x%" PRIx64
1166 "; only dequeued image handles are valid",
1167 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001168 VkResult result = VK_SUCCESS;
1169 if (images) {
1170 uint32_t n = swapchain.num_images;
1171 if (*count < swapchain.num_images) {
1172 n = *count;
1173 result = VK_INCOMPLETE;
1174 }
1175 for (uint32_t i = 0; i < n; i++)
1176 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001177 *count = n;
1178 } else {
1179 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001180 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001181 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001182}
1183
Jesse Halle1b12782015-11-30 11:27:32 -08001184VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001185VkResult AcquireNextImageKHR(VkDevice device,
1186 VkSwapchainKHR swapchain_handle,
1187 uint64_t timeout,
1188 VkSemaphore semaphore,
1189 VkFence vk_fence,
1190 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001191 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001192 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001193 VkResult result;
1194 int err;
1195
Jesse Halldc225072016-05-30 22:40:14 -07001196 if (swapchain.surface.swapchain_handle != swapchain_handle)
1197 return VK_ERROR_OUT_OF_DATE_KHR;
1198
Jesse Halld7b994a2015-09-07 14:17:37 -07001199 ALOGW_IF(
1200 timeout != UINT64_MAX,
1201 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1202
1203 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001204 int fence_fd;
1205 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001206 if (err != 0) {
1207 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1208 // errors and translate them to valid Vulkan result codes?
1209 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001210 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001211 }
1212
1213 uint32_t idx;
1214 for (idx = 0; idx < swapchain.num_images; idx++) {
1215 if (swapchain.images[idx].buffer.get() == buffer) {
1216 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001217 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001218 break;
1219 }
1220 }
1221 if (idx == swapchain.num_images) {
1222 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001223 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001224 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001225 }
1226
1227 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001228 if (fence_fd != -1) {
1229 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001230 if (fence_clone == -1) {
1231 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1232 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001233 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001234 }
1235 }
1236
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001237 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001238 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001239 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001240 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1241 // even if the call fails. We could close it ourselves on failure, but
1242 // that would create a race condition if the driver closes it on a
1243 // failure path: some other thread might create an fd with the same
1244 // number between the time the driver closes it and the time we close
1245 // it. We must assume one of: the driver *always* closes it even on
1246 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001247 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001248 swapchain.images[idx].dequeued = false;
1249 swapchain.images[idx].dequeue_fence = -1;
1250 return result;
1251 }
1252
1253 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001254 return VK_SUCCESS;
1255}
1256
Jesse Halldc225072016-05-30 22:40:14 -07001257static VkResult WorstPresentResult(VkResult a, VkResult b) {
1258 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1259 // (in spec version 1.0.14).
1260 static const VkResult kWorstToBest[] = {
1261 VK_ERROR_DEVICE_LOST,
1262 VK_ERROR_SURFACE_LOST_KHR,
1263 VK_ERROR_OUT_OF_DATE_KHR,
1264 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1265 VK_ERROR_OUT_OF_HOST_MEMORY,
1266 VK_SUBOPTIMAL_KHR,
1267 };
1268 for (auto result : kWorstToBest) {
1269 if (a == result || b == result)
1270 return result;
1271 }
1272 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1273 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1274 return a != VK_SUCCESS ? a : b;
1275}
1276
Jesse Halle1b12782015-11-30 11:27:32 -08001277VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001278VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001279 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1280 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1281 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001282
Jesse Halldc225072016-05-30 22:40:14 -07001283 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001284 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001285 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001286
Ian Elliottcb351132016-12-13 10:30:40 -07001287 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001288 const VkPresentRegionsKHR* present_regions = nullptr;
1289 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001290 const VkPresentRegionsKHR* next =
1291 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1292 while (next) {
1293 switch (next->sType) {
1294 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1295 present_regions = next;
1296 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001297 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001298 present_times =
1299 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1300 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001301 default:
1302 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1303 next->sType);
1304 break;
1305 }
1306 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1307 }
1308 ALOGV_IF(
1309 present_regions &&
1310 present_regions->swapchainCount != present_info->swapchainCount,
1311 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001312 ALOGV_IF(present_times &&
1313 present_times->swapchainCount != present_info->swapchainCount,
1314 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1315 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001316 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001317 (present_regions) ? present_regions->pRegions : nullptr;
1318 const VkPresentTimeGOOGLE* times =
1319 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001320 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001321 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001322 uint32_t nrects = 0;
1323
Jesse Halld7b994a2015-09-07 14:17:37 -07001324 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1325 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001326 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001327 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001328 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001329 const VkPresentRegionKHR* region =
1330 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001331 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001332 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001333 VkResult result;
1334 int err;
1335
Jesse Halld7b994a2015-09-07 14:17:37 -07001336 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001337 result = dispatch.QueueSignalReleaseImageANDROID(
1338 queue, present_info->waitSemaphoreCount,
1339 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001340 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001341 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001342 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001343 }
1344
Jesse Halldc225072016-05-30 22:40:14 -07001345 if (swapchain.surface.swapchain_handle ==
1346 present_info->pSwapchains[sc]) {
1347 ANativeWindow* window = swapchain.surface.window.get();
1348 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001349 if (region) {
1350 // Process the incremental-present hint for this swapchain:
1351 uint32_t rcount = region->rectangleCount;
1352 if (rcount > nrects) {
1353 android_native_rect_t* new_rects =
1354 static_cast<android_native_rect_t*>(
1355 allocator->pfnReallocation(
1356 allocator->pUserData, rects,
1357 sizeof(android_native_rect_t) * rcount,
1358 alignof(android_native_rect_t),
1359 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1360 if (new_rects) {
1361 rects = new_rects;
1362 nrects = rcount;
1363 } else {
1364 rcount = 0; // Ignore the hint for this swapchain
1365 }
1366 }
1367 for (uint32_t r = 0; r < rcount; ++r) {
1368 if (region->pRectangles[r].layer > 0) {
1369 ALOGV(
1370 "vkQueuePresentKHR ignoring invalid layer "
1371 "(%u); using layer 0 instead",
1372 region->pRectangles[r].layer);
1373 }
1374 int x = region->pRectangles[r].offset.x;
1375 int y = region->pRectangles[r].offset.y;
1376 int width = static_cast<int>(
1377 region->pRectangles[r].extent.width);
1378 int height = static_cast<int>(
1379 region->pRectangles[r].extent.height);
1380 android_native_rect_t* cur_rect = &rects[r];
1381 cur_rect->left = x;
1382 cur_rect->top = y + height;
1383 cur_rect->right = x + width;
1384 cur_rect->bottom = y;
1385 }
1386 native_window_set_surface_damage(window, rects, rcount);
1387 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001388 if (time) {
1389 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001390 ALOGV(
1391 "Calling "
1392 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001393 native_window_enable_frame_timestamps(window, true);
1394 swapchain.frame_timestamps_enabled = true;
1395 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001396
1397 // Record the nativeFrameId so it can be later correlated to
1398 // this present.
1399 uint64_t nativeFrameId = 0;
1400 err = native_window_get_next_frame_id(
1401 window, &nativeFrameId);
1402 if (err != android::NO_ERROR) {
1403 ALOGE("Failed to get next native frame ID.");
1404 }
1405
1406 // Add a new timing record with the user's presentID and
1407 // the nativeFrameId.
1408 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1409 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001410 swapchain.timing.removeAt(0);
1411 }
1412 if (time->desiredPresentTime) {
1413 // Set the desiredPresentTime:
1414 ALOGV(
1415 "Calling "
1416 "native_window_set_buffers_timestamp(%" PRId64 ")",
1417 time->desiredPresentTime);
1418 native_window_set_buffers_timestamp(
1419 window,
1420 static_cast<int64_t>(time->desiredPresentTime));
1421 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001422 }
Jesse Halldc225072016-05-30 22:40:14 -07001423 err = window->queueBuffer(window, img.buffer.get(), fence);
1424 // queueBuffer always closes fence, even on error
1425 if (err != 0) {
1426 // TODO(jessehall): What now? We should probably cancel the
1427 // buffer, I guess?
1428 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1429 swapchain_result = WorstPresentResult(
1430 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1431 }
1432 if (img.dequeue_fence >= 0) {
1433 close(img.dequeue_fence);
1434 img.dequeue_fence = -1;
1435 }
1436 img.dequeued = false;
1437 }
1438 if (swapchain_result != VK_SUCCESS) {
1439 ReleaseSwapchainImage(device, window, fence, img);
1440 OrphanSwapchain(device, &swapchain);
1441 }
1442 } else {
1443 ReleaseSwapchainImage(device, nullptr, fence, img);
1444 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001445 }
1446
Jesse Halla9e57032015-11-30 01:03:10 -08001447 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001448 present_info->pResults[sc] = swapchain_result;
1449
1450 if (swapchain_result != final_result)
1451 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001452 }
Ian Elliottcb351132016-12-13 10:30:40 -07001453 if (rects) {
1454 allocator->pfnFree(allocator->pUserData, rects);
1455 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001456
1457 return final_result;
1458}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001459
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001460VKAPI_ATTR
1461VkResult GetRefreshCycleDurationGOOGLE(
1462 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001463 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001464 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Ian Elliott62c48c92017-01-20 13:13:20 -07001465 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001466 VkResult result = VK_SUCCESS;
1467
Ian Elliottbe833a22017-01-25 13:09:20 -07001468 pDisplayTimingProperties->refreshDuration = swapchain.refresh_duration;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001469
1470 return result;
1471}
1472
1473VKAPI_ATTR
1474VkResult GetPastPresentationTimingGOOGLE(
1475 VkDevice,
1476 VkSwapchainKHR swapchain_handle,
1477 uint32_t* count,
1478 VkPastPresentationTimingGOOGLE* timings) {
1479 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1480 ANativeWindow* window = swapchain.surface.window.get();
1481 VkResult result = VK_SUCCESS;
1482
1483 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001484 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001485 native_window_enable_frame_timestamps(window, true);
1486 swapchain.frame_timestamps_enabled = true;
1487 }
1488
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001489 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001490 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1491 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001492 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001493 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001494 }
1495
1496 return result;
1497}
1498
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001499VKAPI_ATTR
1500VkResult GetSwapchainStatusKHR(
1501 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001502 VkSwapchainKHR swapchain_handle) {
1503 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001504 VkResult result = VK_SUCCESS;
1505
Chris Forbes4e18ba82017-01-20 12:50:17 +13001506 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1507 return VK_ERROR_OUT_OF_DATE_KHR;
1508 }
1509
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001510 // TODO(chrisforbes): Implement this function properly
1511
1512 return result;
1513}
1514
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001515VKAPI_ATTR void SetHdrMetadataEXT(
1516 VkDevice device,
1517 uint32_t swapchainCount,
1518 const VkSwapchainKHR* pSwapchains,
1519 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
1520 // TODO: courtneygo: implement actual function
1521 (void)device;
1522 (void)swapchainCount;
1523 (void)pSwapchains;
1524 (void)pHdrMetadataEXTs;
1525 return;
1526}
1527
Chia-I Wu62262232016-03-26 07:06:44 +08001528} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001529} // namespace vulkan