blob: 5017e143e212d529241ef00c93165349b8370f70 [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>
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -080020#include <ui/BufferQueueDefs.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>
Brian Anderson1049d1d2016-12-16 17:25:57 -080023#include <utils/Vector.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:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800111 TimingInfo() = default;
112 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700113 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800114 native_frame_id_(nativeFrameId) {}
115 bool ready() const {
Ian Elliott8a977262017-01-19 09:05:58 -0700116 return (timestamp_desired_present_time_ &&
117 timestamp_actual_present_time_ &&
118 timestamp_render_complete_time_ &&
119 timestamp_composition_latch_time_);
120 }
121 void calculate(uint64_t rdur) {
122 vals_.actualPresentTime = timestamp_actual_present_time_;
123 uint64_t margin = (timestamp_composition_latch_time_ -
124 timestamp_render_complete_time_);
125 // Calculate vals_.earliestPresentTime, and potentially adjust
126 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
127 // is vals_.actualPresentTime. If we can subtract rdur (the duration
128 // of a refresh cycle) from vals_.earliestPresentTime (and also from
129 // vals_.presentMargin) and still leave a positive margin, then we can
130 // report to the application that it could have presented earlier than
131 // it did (per the extension specification). If for some reason, we
132 // can do this subtraction repeatedly, we do, since
133 // vals_.earliestPresentTime really is supposed to be the "earliest".
134 uint64_t early_time = vals_.actualPresentTime;
135 while ((margin > rdur) &&
136 ((early_time - rdur) > timestamp_composition_latch_time_)) {
137 early_time -= rdur;
138 margin -= rdur;
139 }
140 vals_.earliestPresentTime = early_time;
141 vals_.presentMargin = margin;
142 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800143 void get_values(VkPastPresentationTimingGOOGLE* values) const {
144 *values = vals_;
145 }
Ian Elliott8a977262017-01-19 09:05:58 -0700146
147 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800148 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700149
Brian Anderson1049d1d2016-12-16 17:25:57 -0800150 uint64_t native_frame_id_ { 0 };
151 uint64_t timestamp_desired_present_time_ { 0 };
152 uint64_t timestamp_actual_present_time_ { 0 };
153 uint64_t timestamp_render_complete_time_ { 0 };
154 uint64_t timestamp_composition_latch_time_ { 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700155};
156
Jesse Halld7b994a2015-09-07 14:17:37 -0700157// ----------------------------------------------------------------------------
158
Jesse Hall1356b0d2015-11-23 17:24:58 -0800159struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800160 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700161 VkSwapchainKHR swapchain_handle;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800162};
163
164VkSurfaceKHR HandleFromSurface(Surface* surface) {
165 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
166}
167
168Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800169 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800170}
171
Ian Elliott8a977262017-01-19 09:05:58 -0700172// Maximum number of TimingInfo structs to keep per swapchain:
173enum { MAX_TIMING_INFOS = 10 };
174// Minimum number of frames to look for in the past (so we don't cause
175// syncronous requests to Surface Flinger):
176enum { MIN_NUM_FRAMES_AGO = 5 };
177
Jesse Hall1356b0d2015-11-23 17:24:58 -0800178struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700179 Swapchain(Surface& surface_,
180 uint32_t num_images_,
181 VkPresentModeKHR present_mode)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700182 : surface(surface_),
183 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700184 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
Ian Elliott8a977262017-01-19 09:05:58 -0700185 frame_timestamps_enabled(false) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700186 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700187 int64_t rdur;
188 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700189 window,
Ian Elliottbe833a22017-01-25 13:09:20 -0700190 &rdur);
191 refresh_duration = static_cast<uint64_t>(rdur);
Ian Elliott8a977262017-01-19 09:05:58 -0700192 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800193
194 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700195 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700196 bool mailbox_mode;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700197 bool frame_timestamps_enabled;
Ian Elliottbe833a22017-01-25 13:09:20 -0700198 uint64_t refresh_duration;
Jesse Halld7b994a2015-09-07 14:17:37 -0700199
200 struct Image {
201 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
202 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800203 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700204 // The fence is only valid when the buffer is dequeued, and should be
205 // -1 any other time. When valid, we own the fd, and must ensure it is
206 // closed: either by closing it explicitly when queueing the buffer,
207 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
208 int dequeue_fence;
209 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800210 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700211
Brian Anderson1049d1d2016-12-16 17:25:57 -0800212 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700213};
214
215VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
216 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
217}
218
219Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800220 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700221}
222
Jesse Halldc225072016-05-30 22:40:14 -0700223void ReleaseSwapchainImage(VkDevice device,
224 ANativeWindow* window,
225 int release_fence,
226 Swapchain::Image& image) {
227 ALOG_ASSERT(release_fence == -1 || image.dequeued,
228 "ReleaseSwapchainImage: can't provide a release fence for "
229 "non-dequeued images");
230
231 if (image.dequeued) {
232 if (release_fence >= 0) {
233 // We get here from vkQueuePresentKHR. The application is
234 // responsible for creating an execution dependency chain from
235 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
236 // (release_fence), so we can drop the dequeue_fence here.
237 if (image.dequeue_fence >= 0)
238 close(image.dequeue_fence);
239 } else {
240 // We get here during swapchain destruction, or various serious
241 // error cases e.g. when we can't create the release_fence during
242 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
243 // have already signalled, since the swapchain images are supposed
244 // to be idle before the swapchain is destroyed. In error cases,
245 // there may be rendering in flight to the image, but since we
246 // weren't able to create a release_fence, waiting for the
247 // dequeue_fence is about the best we can do.
248 release_fence = image.dequeue_fence;
249 }
250 image.dequeue_fence = -1;
251
252 if (window) {
253 window->cancelBuffer(window, image.buffer.get(), release_fence);
254 } else {
255 if (release_fence >= 0) {
256 sync_wait(release_fence, -1 /* forever */);
257 close(release_fence);
258 }
259 }
260
261 image.dequeued = false;
262 }
263
264 if (image.image) {
265 GetData(device).driver.DestroyImage(device, image.image, nullptr);
266 image.image = VK_NULL_HANDLE;
267 }
268
269 image.buffer.clear();
270}
271
272void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
273 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
274 return;
Jesse Halldc225072016-05-30 22:40:14 -0700275 for (uint32_t i = 0; i < swapchain->num_images; i++) {
276 if (!swapchain->images[i].dequeued)
277 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
278 }
279 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700280 swapchain->timing.clear();
281}
282
283uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800284 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
285 return 0;
286 }
Ian Elliott8a977262017-01-19 09:05:58 -0700287
Brian Anderson1049d1d2016-12-16 17:25:57 -0800288 uint32_t num_ready = 0;
289 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
290 for (uint32_t i = 0; i < num_timings; i++) {
291 TimingInfo& ti = swapchain.timing.editItemAt(i);
292 if (ti.ready()) {
293 // This TimingInfo is ready to be reported to the user. Add it
294 // to the num_ready.
295 num_ready++;
296 continue;
297 }
298 // This TimingInfo is not yet ready to be reported to the user,
299 // and so we should look for any available timestamps that
300 // might make it ready.
301 int64_t desired_present_time = 0;
302 int64_t render_complete_time = 0;
303 int64_t composition_latch_time = 0;
304 int64_t actual_present_time = 0;
305 // Obtain timestamps:
306 int ret = native_window_get_frame_timestamps(
307 swapchain.surface.window.get(), ti.native_frame_id_,
308 &desired_present_time, &render_complete_time,
309 &composition_latch_time,
310 NULL, //&first_composition_start_time,
311 NULL, //&last_composition_start_time,
312 NULL, //&composition_finish_time,
313 // TODO(ianelliott): Maybe ask if this one is
314 // supported, at startup time (since it may not be
315 // supported):
316 &actual_present_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800317 NULL, //&dequeue_ready_time,
318 NULL /*&reads_done_time*/);
319
320 if (ret != android::NO_ERROR) {
321 continue;
322 }
323
324 // Record the timestamp(s) we received, and then see if this TimingInfo
325 // is ready to be reported to the user:
326 ti.timestamp_desired_present_time_ =
327 static_cast<uint64_t>(desired_present_time);
328 ti.timestamp_actual_present_time_ =
329 static_cast<uint64_t>(actual_present_time);
330 ti.timestamp_render_complete_time_ =
331 static_cast<uint64_t>(render_complete_time);
332 ti.timestamp_composition_latch_time_ =
333 static_cast<uint64_t>(composition_latch_time);
334
335 if (ti.ready()) {
336 // The TimingInfo has received enough timestamps, and should now
337 // use those timestamps to calculate the info that should be
338 // reported to the user:
339 ti.calculate(swapchain.refresh_duration);
340 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700341 }
342 }
343 return num_ready;
344}
345
346// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
347void copy_ready_timings(Swapchain& swapchain,
348 uint32_t* count,
349 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800350 if (swapchain.timing.empty()) {
351 *count = 0;
352 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700353 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800354
355 size_t last_ready = swapchain.timing.size() - 1;
356 while (!swapchain.timing[last_ready].ready()) {
357 if (last_ready == 0) {
358 *count = 0;
359 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700360 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800361 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700362 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800363
364 uint32_t num_copied = 0;
365 size_t num_to_remove = 0;
366 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
367 const TimingInfo& ti = swapchain.timing[i];
368 if (ti.ready()) {
369 ti.get_values(&timings[num_copied]);
370 num_copied++;
371 }
372 num_to_remove++;
373 }
374
375 // Discard old frames that aren't ready if newer frames are ready.
376 // We don't expect to get the timing info for those old frames.
377 swapchain.timing.removeItemsAt(0, num_to_remove);
378
Ian Elliott8a977262017-01-19 09:05:58 -0700379 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700380}
381
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700382android_pixel_format GetNativePixelFormat(VkFormat format) {
383 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
384 switch (format) {
385 case VK_FORMAT_R8G8B8A8_UNORM:
386 case VK_FORMAT_R8G8B8A8_SRGB:
387 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
388 break;
389 case VK_FORMAT_R5G6B5_UNORM_PACK16:
390 native_format = HAL_PIXEL_FORMAT_RGB_565;
391 break;
392 case VK_FORMAT_R16G16B16A16_SFLOAT:
393 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
394 break;
395 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
396 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
397 break;
398 default:
399 ALOGV("unsupported swapchain format %d", format);
400 break;
401 }
402 return native_format;
403}
404
405android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
406 switch (colorspace) {
407 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
408 return HAL_DATASPACE_V0_SRGB;
409 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
410 return HAL_DATASPACE_DISPLAY_P3;
411 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
412 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700413 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
414 return HAL_DATASPACE_DCI_P3_LINEAR;
415 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
416 return HAL_DATASPACE_DCI_P3;
417 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
418 return HAL_DATASPACE_V0_SRGB_LINEAR;
419 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
420 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600421 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
422 return HAL_DATASPACE_BT2020_LINEAR;
423 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700424 return static_cast<android_dataspace>(
425 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
426 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600427 case VK_COLOR_SPACE_DOLBYVISION_EXT:
428 return static_cast<android_dataspace>(
429 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
430 HAL_DATASPACE_RANGE_FULL);
431 case VK_COLOR_SPACE_HDR10_HLG_EXT:
432 return static_cast<android_dataspace>(
433 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
434 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700435 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
436 return static_cast<android_dataspace>(
437 HAL_DATASPACE_STANDARD_ADOBE_RGB |
438 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
439 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
440 return HAL_DATASPACE_ADOBE_RGB;
441
442 // Pass through is intended to allow app to provide data that is passed
443 // to the display system without modification.
444 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
445 return HAL_DATASPACE_ARBITRARY;
446
447 default:
448 // This indicates that we don't know about the
449 // dataspace specified and we should indicate that
450 // it's unsupported
451 return HAL_DATASPACE_UNKNOWN;
452 }
453}
454
Jesse Halld7b994a2015-09-07 14:17:37 -0700455} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700456
Jesse Halle1b12782015-11-30 11:27:32 -0800457VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800458VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800459 VkInstance instance,
460 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
461 const VkAllocationCallbacks* allocator,
462 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800463 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800464 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800465 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
466 alignof(Surface),
467 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800468 if (!mem)
469 return VK_ERROR_OUT_OF_HOST_MEMORY;
470 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700471
Chia-I Wue8e689f2016-04-18 08:21:31 +0800472 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700473 surface->swapchain_handle = VK_NULL_HANDLE;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700474
Jesse Hall1356b0d2015-11-23 17:24:58 -0800475 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
476 int err =
477 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
478 if (err != 0) {
479 // TODO(jessehall): Improve error reporting. Can we enumerate possible
480 // errors and translate them to valid Vulkan result codes?
481 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
482 err);
483 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800484 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700485 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800486 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700487
Jesse Hall1356b0d2015-11-23 17:24:58 -0800488 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700489 return VK_SUCCESS;
490}
491
Jesse Halle1b12782015-11-30 11:27:32 -0800492VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800493void DestroySurfaceKHR(VkInstance instance,
494 VkSurfaceKHR surface_handle,
495 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800496 Surface* surface = SurfaceFromHandle(surface_handle);
497 if (!surface)
498 return;
499 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700500 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700501 "destroyed VkSurfaceKHR 0x%" PRIx64
502 " has active VkSwapchainKHR 0x%" PRIx64,
503 reinterpret_cast<uint64_t>(surface_handle),
504 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800505 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800506 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800507 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800508 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800509}
510
Jesse Halle1b12782015-11-30 11:27:32 -0800511VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800512VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
513 uint32_t /*queue_family*/,
514 VkSurfaceKHR /*surface*/,
515 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800516 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800517 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800518}
519
Jesse Halle1b12782015-11-30 11:27:32 -0800520VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800521VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800522 VkPhysicalDevice /*pdev*/,
523 VkSurfaceKHR surface,
524 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700525 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800526 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700527
528 int width, height;
529 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
530 if (err != 0) {
531 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
532 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700533 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700534 }
535 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
536 if (err != 0) {
537 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
538 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700539 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700540 }
541
Jesse Hall55bc0972016-02-23 16:43:29 -0800542 int transform_hint;
543 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
544 if (err != 0) {
545 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
546 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700547 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800548 }
549
Jesse Halld7b994a2015-09-07 14:17:37 -0700550 // TODO(jessehall): Figure out what the min/max values should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800551 capabilities->minImageCount = 2;
552 capabilities->maxImageCount = 3;
Jesse Halld7b994a2015-09-07 14:17:37 -0700553
Jesse Hallfe2662d2016-02-09 13:26:59 -0800554 capabilities->currentExtent =
555 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
556
Jesse Halld7b994a2015-09-07 14:17:37 -0700557 // TODO(jessehall): Figure out what the max extent should be. Maximum
558 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800559 capabilities->minImageExtent = VkExtent2D{1, 1};
560 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700561
Jesse Hallfe2662d2016-02-09 13:26:59 -0800562 capabilities->maxImageArrayLayers = 1;
563
Jesse Hall55bc0972016-02-23 16:43:29 -0800564 capabilities->supportedTransforms = kSupportedTransforms;
565 capabilities->currentTransform =
566 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700567
Jesse Hallfe2662d2016-02-09 13:26:59 -0800568 // On Android, window composition is a WindowManager property, not something
569 // associated with the bufferqueue. It can't be changed from here.
570 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700571
572 // TODO(jessehall): I think these are right, but haven't thought hard about
573 // it. Do we need to query the driver for support of any of these?
574 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700575 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
576 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800577 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800578 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
579 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
580 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700581 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
582
Jesse Hallb1352bc2015-09-04 16:12:33 -0700583 return VK_SUCCESS;
584}
585
Jesse Halle1b12782015-11-30 11:27:32 -0800586VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700587VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
588 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800589 uint32_t* count,
590 VkSurfaceFormatKHR* formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700591 const InstanceData& instance_data = GetData(pdev);
592
Jesse Hall1356b0d2015-11-23 17:24:58 -0800593 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
594 // a new gralloc method to query whether a (format, usage) pair is
595 // supported, and check that for each gralloc format that corresponds to a
596 // Vulkan format. Shorter term, just add a few more formats to the ones
597 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700598
599 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700600 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
601 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
602 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700603 };
604 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700605 uint32_t total_num_formats = kNumFormats;
606
607 bool wide_color_support = false;
608 Surface& surface = *SurfaceFromHandle(surface_handle);
609 int err = native_window_get_wide_color_support(surface.window.get(),
610 &wide_color_support);
611 if (err) {
612 // Not allowed to return a more sensible error code, so do this
613 return VK_ERROR_OUT_OF_HOST_MEMORY;
614 }
615 ALOGV("wide_color_support is: %d", wide_color_support);
616 wide_color_support =
617 wide_color_support &&
618 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
619
620 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbca34c92017-02-17 11:31:23 -0700621 {VK_FORMAT_R16G16B16A16_SFLOAT,
622 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700623 {VK_FORMAT_A2R10G10B10_UNORM_PACK32,
624 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
625 };
626 const uint32_t kNumWideColorFormats =
627 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
628 if (wide_color_support) {
629 total_num_formats += kNumWideColorFormats;
630 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700631
632 VkResult result = VK_SUCCESS;
633 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700634 uint32_t out_count = 0;
635 uint32_t transfer_count = 0;
636 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700637 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700638 transfer_count = std::min(*count, kNumFormats);
639 std::copy(kFormats, kFormats + transfer_count, formats);
640 out_count += transfer_count;
641 if (wide_color_support) {
642 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
643 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
644 formats + out_count);
645 out_count += transfer_count;
646 }
647 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700648 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700649 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700650 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700651 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700652}
653
Jesse Halle1b12782015-11-30 11:27:32 -0800654VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300655VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
656 VkPhysicalDevice physicalDevice,
657 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
658 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
659 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
660 physicalDevice, pSurfaceInfo->surface,
661 &pSurfaceCapabilities->surfaceCapabilities);
662
Chris Forbes06bc0092017-03-16 16:46:05 +1300663 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
664 while (caps->pNext) {
665 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
666
667 switch (caps->sType) {
668 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
669 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
670 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
671 caps);
672 // Claim same set of usage flags are supported for
673 // shared present modes as for other modes.
674 shared_caps->sharedPresentSupportedUsageFlags =
675 pSurfaceCapabilities->surfaceCapabilities
676 .supportedUsageFlags;
677 } break;
678
679 default:
680 // Ignore all other extension structs
681 break;
682 }
683 }
684
Chris Forbes2452cf72017-03-16 16:30:17 +1300685 return result;
686}
687
688VKAPI_ATTR
689VkResult GetPhysicalDeviceSurfaceFormats2KHR(
690 VkPhysicalDevice physicalDevice,
691 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
692 uint32_t* pSurfaceFormatCount,
693 VkSurfaceFormat2KHR* pSurfaceFormats) {
694 if (!pSurfaceFormats) {
695 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
696 pSurfaceInfo->surface,
697 pSurfaceFormatCount, nullptr);
698 } else {
699 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
700 // after the call.
701 android::Vector<VkSurfaceFormatKHR> surface_formats;
702 surface_formats.resize(*pSurfaceFormatCount);
703 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
704 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
705 &surface_formats.editItemAt(0));
706
707 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
708 // marshal results individually due to stride difference.
709 // completely ignore any chained extension structs.
710 uint32_t formats_to_marshal = *pSurfaceFormatCount;
711 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
712 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
713 }
714 }
715
716 return result;
717 }
718}
719
720VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300721VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Chia-I Wu62262232016-03-26 07:06:44 +0800722 VkSurfaceKHR /*surface*/,
723 uint32_t* count,
724 VkPresentModeKHR* modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300725 android::Vector<VkPresentModeKHR> present_modes;
726 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
727 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
728
729 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
730 if (QueryPresentationProperties(pdev, &present_properties)) {
731 if (present_properties.sharedImage) {
732 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
733 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
734 }
735 }
736
737 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700738
739 VkResult result = VK_SUCCESS;
740 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300741 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700742 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300743 *count = std::min(*count, num_modes);
744 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700745 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300746 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700747 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700748 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700749}
750
Jesse Halle1b12782015-11-30 11:27:32 -0800751VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800752VkResult CreateSwapchainKHR(VkDevice device,
753 const VkSwapchainCreateInfoKHR* create_info,
754 const VkAllocationCallbacks* allocator,
755 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700756 int err;
757 VkResult result = VK_SUCCESS;
758
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700759 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
760 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
761 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
762 " oldSwapchain=0x%" PRIx64,
763 reinterpret_cast<uint64_t>(create_info->surface),
764 create_info->minImageCount, create_info->imageFormat,
765 create_info->imageColorSpace, create_info->imageExtent.width,
766 create_info->imageExtent.height, create_info->imageUsage,
767 create_info->preTransform, create_info->presentMode,
768 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
769
Jesse Hall1f91d392015-12-11 16:28:44 -0800770 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800771 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800772
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700773 android_pixel_format native_pixel_format =
774 GetNativePixelFormat(create_info->imageFormat);
775 android_dataspace native_dataspace =
776 GetNativeDataspace(create_info->imageColorSpace);
777 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
778 ALOGE(
779 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
780 "failed: Unsupported color space",
781 create_info->imageColorSpace);
782 return VK_ERROR_INITIALIZATION_FAILED;
783 }
784
Jesse Hall42a9eec2016-06-03 12:39:49 -0700785 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700786 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800787 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700788 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700789 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800790 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700791 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +1300792 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300793 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
794 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700795 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800796 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700797
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700798 Surface& surface = *SurfaceFromHandle(create_info->surface);
799
Jesse Halldc225072016-05-30 22:40:14 -0700800 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -0700801 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -0700802 " because it already has active swapchain 0x%" PRIx64
803 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
804 reinterpret_cast<uint64_t>(create_info->surface),
805 reinterpret_cast<uint64_t>(surface.swapchain_handle),
806 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
807 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
808 }
809 if (create_info->oldSwapchain != VK_NULL_HANDLE)
810 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
811
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700812 // -- Reset the native window --
813 // The native window might have been used previously, and had its properties
814 // changed from defaults. That will affect the answer we get for queries
815 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
816 // attempt such queries.
817
Jesse Halldc225072016-05-30 22:40:14 -0700818 // The native window only allows dequeueing all buffers before any have
819 // been queued, since after that point at least one is assumed to be in
820 // non-FREE state at any given time. Disconnecting and re-connecting
821 // orphans the previous buffers, getting us back to the state where we can
822 // dequeue all buffers.
823 err = native_window_api_disconnect(surface.window.get(),
824 NATIVE_WINDOW_API_EGL);
825 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
826 strerror(-err), err);
827 err =
828 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
829 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
830 strerror(-err), err);
831
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700832 err = native_window_set_buffer_count(surface.window.get(), 0);
833 if (err != 0) {
834 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
835 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700836 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700837 }
838
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +0530839 int swap_interval =
840 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
841 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700842 if (err != 0) {
843 // TODO(jessehall): Improve error reporting. Can we enumerate possible
844 // errors and translate them to valid Vulkan result codes?
845 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
846 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700847 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700848 }
849
Chris Forbesb8042d22017-01-18 18:07:05 +1300850 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
851 if (err != 0) {
852 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
853 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700854 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300855 }
856
857 err = native_window_set_auto_refresh(surface.window.get(), false);
858 if (err != 0) {
859 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
860 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700861 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300862 }
863
Jesse Halld7b994a2015-09-07 14:17:37 -0700864 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700865
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800866 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -0800867
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700868 err = native_window_set_buffers_format(surface.window.get(),
869 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -0800870 if (err != 0) {
871 // TODO(jessehall): Improve error reporting. Can we enumerate possible
872 // errors and translate them to valid Vulkan result codes?
873 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700874 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700875 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -0800876 }
877 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700878 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -0800879 if (err != 0) {
880 // TODO(jessehall): Improve error reporting. Can we enumerate possible
881 // errors and translate them to valid Vulkan result codes?
882 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700883 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700884 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -0800885 }
886
Jesse Hall3dd678a2016-01-08 21:52:01 -0800887 err = native_window_set_buffers_dimensions(
888 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
889 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700890 if (err != 0) {
891 // TODO(jessehall): Improve error reporting. Can we enumerate possible
892 // errors and translate them to valid Vulkan result codes?
893 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
894 create_info->imageExtent.width, create_info->imageExtent.height,
895 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700896 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700897 }
898
Jesse Hall178b6962016-02-24 15:39:50 -0800899 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
900 // applied during rendering. native_window_set_transform() expects the
901 // inverse: the transform the app is requesting that the compositor perform
902 // during composition. With native windows, pre-transform works by rendering
903 // with the same transform the compositor is applying (as in Vulkan), but
904 // then requesting the inverse transform, so that when the compositor does
905 // it's job the two transforms cancel each other out and the compositor ends
906 // up applying an identity transform to the app's buffer.
907 err = native_window_set_buffers_transform(
908 surface.window.get(),
909 InvertTransformToNative(create_info->preTransform));
910 if (err != 0) {
911 // TODO(jessehall): Improve error reporting. Can we enumerate possible
912 // errors and translate them to valid Vulkan result codes?
913 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
914 InvertTransformToNative(create_info->preTransform),
915 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700916 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -0800917 }
918
Jesse Hallf64ca122015-11-03 16:11:10 -0800919 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -0800920 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -0800921 if (err != 0) {
922 // TODO(jessehall): Improve error reporting. Can we enumerate possible
923 // errors and translate them to valid Vulkan result codes?
924 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
925 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700926 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -0800927 }
928
Jesse Halle6080bf2016-02-28 20:58:50 -0800929 int query_value;
930 err = surface.window->query(surface.window.get(),
931 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
932 &query_value);
933 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700934 // TODO(jessehall): Improve error reporting. Can we enumerate possible
935 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -0800936 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
937 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -0700938 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700939 }
Jesse Halle6080bf2016-02-28 20:58:50 -0800940 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -0700941 uint32_t num_images =
942 (create_info->minImageCount - 1) + min_undequeued_buffers;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800943 err = native_window_set_buffer_count(surface.window.get(), num_images);
Jesse Halld7b994a2015-09-07 14:17:37 -0700944 if (err != 0) {
945 // TODO(jessehall): Improve error reporting. Can we enumerate possible
946 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700947 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
948 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700949 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700950 }
951
Chris Forbes8c47dc92017-01-12 11:13:58 +1300952 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300953 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
954 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Chris Forbes4da65b92017-01-31 11:48:50 +1300955 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
Chris Forbesb8042d22017-01-18 18:07:05 +1300956
957 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
958 if (err != 0) {
959 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700960 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300961 }
962 }
963
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300964 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Chris Forbesb8042d22017-01-18 18:07:05 +1300965 err = native_window_set_auto_refresh(surface.window.get(), true);
966 if (err != 0) {
967 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700968 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300969 }
Chris Forbesb4421522017-01-18 16:57:02 +1300970 }
971
Jesse Hall70f93352015-11-04 09:41:31 -0800972 int gralloc_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +1300973 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -0800974 uint64_t consumer_usage, producer_usage;
Steve Pfetsch72957a92017-03-13 22:57:15 +0000975 if (GetData(device).driver_version == 256587285) {
Jesse Hall85bb0c52017-02-09 22:13:02 -0800976 // HACK workaround for loader/driver mismatch during transition to
977 // vkGetSwapchainGrallocUsage2ANDROID.
978 typedef VkResult(VKAPI_PTR *
979 PFN_vkGetSwapchainGrallocUsage2ANDROID_HACK)(
980 VkDevice device, VkFormat format, VkImageUsageFlags imageUsage,
981 uint64_t * grallocConsumerUsage,
982 uint64_t * grallocProducerUsage);
983 auto get_swapchain_gralloc_usage =
984 reinterpret_cast<PFN_vkGetSwapchainGrallocUsage2ANDROID_HACK>(
985 dispatch.GetSwapchainGrallocUsage2ANDROID);
986 result = get_swapchain_gralloc_usage(
987 device, create_info->imageFormat, create_info->imageUsage,
988 &consumer_usage, &producer_usage);
989 } else {
990 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
991 device, create_info->imageFormat, create_info->imageUsage,
992 swapchain_image_usage, &consumer_usage, &producer_usage);
993 }
Chris Forbes8c47dc92017-01-12 11:13:58 +1300994 if (result != VK_SUCCESS) {
995 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -0700996 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +1300997 }
Jesse Halld1abd742017-02-09 21:45:51 -0800998 // TODO: This is the same translation done by Gralloc1On0Adapter.
999 // Remove it once ANativeWindow has been updated to take gralloc1-style
1000 // usages.
1001 gralloc_usage =
1002 static_cast<int>(consumer_usage) | static_cast<int>(producer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001003 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001004 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001005 device, create_info->imageFormat, create_info->imageUsage,
Jesse Hall70f93352015-11-04 09:41:31 -08001006 &gralloc_usage);
1007 if (result != VK_SUCCESS) {
1008 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001009 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001010 }
Jesse Hall70f93352015-11-04 09:41:31 -08001011 }
Jesse Hall1356b0d2015-11-23 17:24:58 -08001012 err = native_window_set_usage(surface.window.get(), gralloc_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001013 if (err != 0) {
1014 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1015 // errors and translate them to valid Vulkan result codes?
1016 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001017 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001018 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001019
1020 // -- Allocate our Swapchain object --
1021 // After this point, we must deallocate the swapchain on error.
1022
Jesse Hall1f91d392015-12-11 16:28:44 -08001023 void* mem = allocator->pfnAllocation(allocator->pUserData,
1024 sizeof(Swapchain), alignof(Swapchain),
1025 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001026 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001027 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001028 Swapchain* swapchain =
1029 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001030
1031 // -- Dequeue all buffers and create a VkImage for each --
1032 // Any failures during or after this must cancel the dequeued buffers.
1033
Chris Forbesb56287a2017-01-12 14:28:58 +13001034 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1035#pragma clang diagnostic push
1036#pragma clang diagnostic ignored "-Wold-style-cast"
1037 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1038#pragma clang diagnostic pop
1039 .pNext = nullptr,
1040 .usage = swapchain_image_usage,
1041 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001042 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001043#pragma clang diagnostic push
1044#pragma clang diagnostic ignored "-Wold-style-cast"
1045 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1046#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001047 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001048 };
1049 VkImageCreateInfo image_create = {
1050 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1051 .pNext = &image_native_buffer,
1052 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001053 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001054 .extent = {0, 0, 1},
1055 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001056 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001057 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001058 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001059 .usage = create_info->imageUsage,
Jesse Halld7b994a2015-09-07 14:17:37 -07001060 .flags = 0,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001061 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001062 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001063 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1064 };
1065
Jesse Halld7b994a2015-09-07 14:17:37 -07001066 for (uint32_t i = 0; i < num_images; i++) {
1067 Swapchain::Image& img = swapchain->images[i];
1068
1069 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001070 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1071 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001072 if (err != 0) {
1073 // TODO(jessehall): Improve error reporting. Can we enumerate
1074 // possible errors and translate them to valid Vulkan result codes?
1075 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001076 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001077 break;
1078 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001079 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001080 img.dequeued = true;
1081
1082 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001083 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1084 static_cast<uint32_t>(img.buffer->height),
1085 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001086 image_native_buffer.handle = img.buffer->handle;
1087 image_native_buffer.stride = img.buffer->stride;
1088 image_native_buffer.format = img.buffer->format;
1089 image_native_buffer.usage = img.buffer->usage;
Jesse Halld1abd742017-02-09 21:45:51 -08001090 // TODO: Adjust once ANativeWindowBuffer supports gralloc1-style usage.
1091 // For now, this is the same translation Gralloc1On0Adapter does.
1092 image_native_buffer.usage2.consumer =
1093 static_cast<uint64_t>(img.buffer->usage);
1094 image_native_buffer.usage2.producer =
1095 static_cast<uint64_t>(img.buffer->usage);
Jesse Halld7b994a2015-09-07 14:17:37 -07001096
Jesse Hall03b6fe12015-11-24 12:44:21 -08001097 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001098 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -07001099 if (result != VK_SUCCESS) {
1100 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1101 break;
1102 }
1103 }
1104
1105 // -- Cancel all buffers, returning them to the queue --
1106 // If an error occurred before, also destroy the VkImage and release the
1107 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1108 //
1109 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1110 // but not the non-error path. Should refactor/unify.
1111 for (uint32_t i = 0; i < num_images; i++) {
1112 Swapchain::Image& img = swapchain->images[i];
1113 if (img.dequeued) {
Jesse Hall1356b0d2015-11-23 17:24:58 -08001114 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1115 img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001116 img.dequeue_fence = -1;
1117 img.dequeued = false;
1118 }
1119 if (result != VK_SUCCESS) {
1120 if (img.image)
Jesse Hall1f91d392015-12-11 16:28:44 -08001121 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -07001122 }
1123 }
1124
1125 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001126 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001127 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001128 return result;
1129 }
1130
Jesse Halldc225072016-05-30 22:40:14 -07001131 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1132 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001133 return VK_SUCCESS;
1134}
1135
Jesse Halle1b12782015-11-30 11:27:32 -08001136VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001137void DestroySwapchainKHR(VkDevice device,
1138 VkSwapchainKHR swapchain_handle,
1139 const VkAllocationCallbacks* allocator) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001140 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001141 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001142 if (!swapchain)
1143 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001144 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1145 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001146
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001147 if (swapchain->frame_timestamps_enabled) {
1148 native_window_enable_frame_timestamps(window, false);
1149 }
Jesse Halldc225072016-05-30 22:40:14 -07001150 for (uint32_t i = 0; i < swapchain->num_images; i++)
1151 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001152 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001153 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001154 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001155 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001156 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001157 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001158}
1159
Jesse Halle1b12782015-11-30 11:27:32 -08001160VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001161VkResult GetSwapchainImagesKHR(VkDevice,
1162 VkSwapchainKHR swapchain_handle,
1163 uint32_t* count,
1164 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001165 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001166 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1167 "getting images for non-active swapchain 0x%" PRIx64
1168 "; only dequeued image handles are valid",
1169 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001170 VkResult result = VK_SUCCESS;
1171 if (images) {
1172 uint32_t n = swapchain.num_images;
1173 if (*count < swapchain.num_images) {
1174 n = *count;
1175 result = VK_INCOMPLETE;
1176 }
1177 for (uint32_t i = 0; i < n; i++)
1178 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001179 *count = n;
1180 } else {
1181 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001182 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001183 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001184}
1185
Jesse Halle1b12782015-11-30 11:27:32 -08001186VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001187VkResult AcquireNextImageKHR(VkDevice device,
1188 VkSwapchainKHR swapchain_handle,
1189 uint64_t timeout,
1190 VkSemaphore semaphore,
1191 VkFence vk_fence,
1192 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001193 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001194 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001195 VkResult result;
1196 int err;
1197
Jesse Halldc225072016-05-30 22:40:14 -07001198 if (swapchain.surface.swapchain_handle != swapchain_handle)
1199 return VK_ERROR_OUT_OF_DATE_KHR;
1200
Jesse Halld7b994a2015-09-07 14:17:37 -07001201 ALOGW_IF(
1202 timeout != UINT64_MAX,
1203 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1204
1205 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001206 int fence_fd;
1207 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001208 if (err != 0) {
1209 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1210 // errors and translate them to valid Vulkan result codes?
1211 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001212 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001213 }
1214
1215 uint32_t idx;
1216 for (idx = 0; idx < swapchain.num_images; idx++) {
1217 if (swapchain.images[idx].buffer.get() == buffer) {
1218 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001219 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001220 break;
1221 }
1222 }
1223 if (idx == swapchain.num_images) {
1224 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001225 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001226 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001227 }
1228
1229 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001230 if (fence_fd != -1) {
1231 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001232 if (fence_clone == -1) {
1233 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1234 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001235 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001236 }
1237 }
1238
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001239 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001240 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001241 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001242 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1243 // even if the call fails. We could close it ourselves on failure, but
1244 // that would create a race condition if the driver closes it on a
1245 // failure path: some other thread might create an fd with the same
1246 // number between the time the driver closes it and the time we close
1247 // it. We must assume one of: the driver *always* closes it even on
1248 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001249 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001250 swapchain.images[idx].dequeued = false;
1251 swapchain.images[idx].dequeue_fence = -1;
1252 return result;
1253 }
1254
1255 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001256 return VK_SUCCESS;
1257}
1258
Jesse Halldc225072016-05-30 22:40:14 -07001259static VkResult WorstPresentResult(VkResult a, VkResult b) {
1260 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1261 // (in spec version 1.0.14).
1262 static const VkResult kWorstToBest[] = {
1263 VK_ERROR_DEVICE_LOST,
1264 VK_ERROR_SURFACE_LOST_KHR,
1265 VK_ERROR_OUT_OF_DATE_KHR,
1266 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1267 VK_ERROR_OUT_OF_HOST_MEMORY,
1268 VK_SUBOPTIMAL_KHR,
1269 };
1270 for (auto result : kWorstToBest) {
1271 if (a == result || b == result)
1272 return result;
1273 }
1274 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1275 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1276 return a != VK_SUCCESS ? a : b;
1277}
1278
Jesse Halle1b12782015-11-30 11:27:32 -08001279VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001280VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001281 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1282 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1283 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001284
Jesse Halldc225072016-05-30 22:40:14 -07001285 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001286 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001287 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001288
Ian Elliottcb351132016-12-13 10:30:40 -07001289 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001290 const VkPresentRegionsKHR* present_regions = nullptr;
1291 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001292 const VkPresentRegionsKHR* next =
1293 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1294 while (next) {
1295 switch (next->sType) {
1296 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1297 present_regions = next;
1298 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001299 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001300 present_times =
1301 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1302 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001303 default:
1304 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1305 next->sType);
1306 break;
1307 }
1308 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1309 }
1310 ALOGV_IF(
1311 present_regions &&
1312 present_regions->swapchainCount != present_info->swapchainCount,
1313 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001314 ALOGV_IF(present_times &&
1315 present_times->swapchainCount != present_info->swapchainCount,
1316 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1317 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001318 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001319 (present_regions) ? present_regions->pRegions : nullptr;
1320 const VkPresentTimeGOOGLE* times =
1321 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001322 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001323 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001324 uint32_t nrects = 0;
1325
Jesse Halld7b994a2015-09-07 14:17:37 -07001326 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1327 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001328 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001329 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001330 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001331 const VkPresentRegionKHR* region =
1332 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001333 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001334 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001335 VkResult result;
1336 int err;
1337
Jesse Halld7b994a2015-09-07 14:17:37 -07001338 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001339 result = dispatch.QueueSignalReleaseImageANDROID(
1340 queue, present_info->waitSemaphoreCount,
1341 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001342 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001343 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001344 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001345 }
1346
Jesse Halldc225072016-05-30 22:40:14 -07001347 if (swapchain.surface.swapchain_handle ==
1348 present_info->pSwapchains[sc]) {
1349 ANativeWindow* window = swapchain.surface.window.get();
1350 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001351 if (region) {
1352 // Process the incremental-present hint for this swapchain:
1353 uint32_t rcount = region->rectangleCount;
1354 if (rcount > nrects) {
1355 android_native_rect_t* new_rects =
1356 static_cast<android_native_rect_t*>(
1357 allocator->pfnReallocation(
1358 allocator->pUserData, rects,
1359 sizeof(android_native_rect_t) * rcount,
1360 alignof(android_native_rect_t),
1361 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1362 if (new_rects) {
1363 rects = new_rects;
1364 nrects = rcount;
1365 } else {
1366 rcount = 0; // Ignore the hint for this swapchain
1367 }
1368 }
1369 for (uint32_t r = 0; r < rcount; ++r) {
1370 if (region->pRectangles[r].layer > 0) {
1371 ALOGV(
1372 "vkQueuePresentKHR ignoring invalid layer "
1373 "(%u); using layer 0 instead",
1374 region->pRectangles[r].layer);
1375 }
1376 int x = region->pRectangles[r].offset.x;
1377 int y = region->pRectangles[r].offset.y;
1378 int width = static_cast<int>(
1379 region->pRectangles[r].extent.width);
1380 int height = static_cast<int>(
1381 region->pRectangles[r].extent.height);
1382 android_native_rect_t* cur_rect = &rects[r];
1383 cur_rect->left = x;
1384 cur_rect->top = y + height;
1385 cur_rect->right = x + width;
1386 cur_rect->bottom = y;
1387 }
1388 native_window_set_surface_damage(window, rects, rcount);
1389 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001390 if (time) {
1391 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001392 ALOGV(
1393 "Calling "
1394 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001395 native_window_enable_frame_timestamps(window, true);
1396 swapchain.frame_timestamps_enabled = true;
1397 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001398
1399 // Record the nativeFrameId so it can be later correlated to
1400 // this present.
1401 uint64_t nativeFrameId = 0;
1402 err = native_window_get_next_frame_id(
1403 window, &nativeFrameId);
1404 if (err != android::NO_ERROR) {
1405 ALOGE("Failed to get next native frame ID.");
1406 }
1407
1408 // Add a new timing record with the user's presentID and
1409 // the nativeFrameId.
1410 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1411 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001412 swapchain.timing.removeAt(0);
1413 }
1414 if (time->desiredPresentTime) {
1415 // Set the desiredPresentTime:
1416 ALOGV(
1417 "Calling "
1418 "native_window_set_buffers_timestamp(%" PRId64 ")",
1419 time->desiredPresentTime);
1420 native_window_set_buffers_timestamp(
1421 window,
1422 static_cast<int64_t>(time->desiredPresentTime));
1423 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001424 }
Jesse Halldc225072016-05-30 22:40:14 -07001425 err = window->queueBuffer(window, img.buffer.get(), fence);
1426 // queueBuffer always closes fence, even on error
1427 if (err != 0) {
1428 // TODO(jessehall): What now? We should probably cancel the
1429 // buffer, I guess?
1430 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1431 swapchain_result = WorstPresentResult(
1432 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1433 }
1434 if (img.dequeue_fence >= 0) {
1435 close(img.dequeue_fence);
1436 img.dequeue_fence = -1;
1437 }
1438 img.dequeued = false;
1439 }
1440 if (swapchain_result != VK_SUCCESS) {
1441 ReleaseSwapchainImage(device, window, fence, img);
1442 OrphanSwapchain(device, &swapchain);
1443 }
1444 } else {
1445 ReleaseSwapchainImage(device, nullptr, fence, img);
1446 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001447 }
1448
Jesse Halla9e57032015-11-30 01:03:10 -08001449 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001450 present_info->pResults[sc] = swapchain_result;
1451
1452 if (swapchain_result != final_result)
1453 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001454 }
Ian Elliottcb351132016-12-13 10:30:40 -07001455 if (rects) {
1456 allocator->pfnFree(allocator->pUserData, rects);
1457 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001458
1459 return final_result;
1460}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001461
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001462VKAPI_ATTR
1463VkResult GetRefreshCycleDurationGOOGLE(
1464 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001465 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001466 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Ian Elliott62c48c92017-01-20 13:13:20 -07001467 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001468 VkResult result = VK_SUCCESS;
1469
Ian Elliottbe833a22017-01-25 13:09:20 -07001470 pDisplayTimingProperties->refreshDuration = swapchain.refresh_duration;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001471
1472 return result;
1473}
1474
1475VKAPI_ATTR
1476VkResult GetPastPresentationTimingGOOGLE(
1477 VkDevice,
1478 VkSwapchainKHR swapchain_handle,
1479 uint32_t* count,
1480 VkPastPresentationTimingGOOGLE* timings) {
1481 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1482 ANativeWindow* window = swapchain.surface.window.get();
1483 VkResult result = VK_SUCCESS;
1484
1485 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001486 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001487 native_window_enable_frame_timestamps(window, true);
1488 swapchain.frame_timestamps_enabled = true;
1489 }
1490
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001491 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001492 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1493 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001494 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001495 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001496 }
1497
1498 return result;
1499}
1500
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001501VKAPI_ATTR
1502VkResult GetSwapchainStatusKHR(
1503 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001504 VkSwapchainKHR swapchain_handle) {
1505 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001506 VkResult result = VK_SUCCESS;
1507
Chris Forbes4e18ba82017-01-20 12:50:17 +13001508 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1509 return VK_ERROR_OUT_OF_DATE_KHR;
1510 }
1511
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001512 // TODO(chrisforbes): Implement this function properly
1513
1514 return result;
1515}
1516
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001517VKAPI_ATTR void SetHdrMetadataEXT(
1518 VkDevice device,
1519 uint32_t swapchainCount,
1520 const VkSwapchainKHR* pSwapchains,
1521 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
1522 // TODO: courtneygo: implement actual function
1523 (void)device;
1524 (void)swapchainCount;
1525 (void)pSwapchains;
1526 (void)pHdrMetadataEXTs;
1527 return;
1528}
1529
Chia-I Wu62262232016-03-26 07:06:44 +08001530} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001531} // namespace vulkan