blob: 47334f65e22333a50c10b6ded88523cc7f165bc8 [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>
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070025#include <system/window.h>
Daniel Kochf25f5bb2017-10-05 00:26:58 -040026#include <android/hardware/graphics/common/1.0/types.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070027
Chia-I Wu4a6a9162016-03-26 07:17:34 +080028#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070029
Daniel Kochf25f5bb2017-10-05 00:26:58 -040030using android::hardware::graphics::common::V1_0::BufferUsage;
31
Jesse Hall5ae3abb2015-10-08 14:00:22 -070032// TODO(jessehall): Currently we don't have a good error code for when a native
33// window operation fails. Just returning INITIALIZATION_FAILED for now. Later
34// versions (post SDK 0.9) of the API/extension have a better error code.
35// When updating to that version, audit all error returns.
Chia-I Wu62262232016-03-26 07:06:44 +080036namespace vulkan {
37namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070038
Jesse Halld7b994a2015-09-07 14:17:37 -070039namespace {
40
Jesse Hall55bc0972016-02-23 16:43:29 -080041const VkSurfaceTransformFlagsKHR kSupportedTransforms =
42 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
43 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
44 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
45 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
46 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
47 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
48 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
49 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
50 // VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
51 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
52
53VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
54 // Native and Vulkan transforms are isomorphic, but are represented
55 // differently. Vulkan transforms are built up of an optional horizontal
56 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
57 // transforms are built up from a horizontal flip, vertical flip, and
58 // 90-degree rotation, all optional but always in that order.
59
60 // TODO(jessehall): For now, only support pure rotations, not
61 // flip or flip-and-rotate, until I have more time to test them and build
62 // sample code. As far as I know we never actually use anything besides
63 // pure rotations anyway.
64
65 switch (native) {
66 case 0: // 0x0
67 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
68 // case NATIVE_WINDOW_TRANSFORM_FLIP_H: // 0x1
69 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
70 // case NATIVE_WINDOW_TRANSFORM_FLIP_V: // 0x2
71 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
72 case NATIVE_WINDOW_TRANSFORM_ROT_180: // FLIP_H | FLIP_V
73 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
74 case NATIVE_WINDOW_TRANSFORM_ROT_90: // 0x4
75 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
76 // case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
77 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
78 // case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
79 // return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
80 case NATIVE_WINDOW_TRANSFORM_ROT_270: // FLIP_H | FLIP_V | ROT_90
81 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
82 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
83 default:
84 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
85 }
86}
87
Jesse Hall178b6962016-02-24 15:39:50 -080088int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
89 switch (transform) {
90 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
91 return NATIVE_WINDOW_TRANSFORM_ROT_270;
92 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
93 return NATIVE_WINDOW_TRANSFORM_ROT_180;
94 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
95 return NATIVE_WINDOW_TRANSFORM_ROT_90;
96 // TODO(jessehall): See TODO in TranslateNativeToVulkanTransform.
97 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
98 // return NATIVE_WINDOW_TRANSFORM_FLIP_H;
99 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
100 // return NATIVE_WINDOW_TRANSFORM_FLIP_H |
101 // NATIVE_WINDOW_TRANSFORM_ROT_90;
102 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
103 // return NATIVE_WINDOW_TRANSFORM_FLIP_V;
104 // case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
105 // return NATIVE_WINDOW_TRANSFORM_FLIP_V |
106 // NATIVE_WINDOW_TRANSFORM_ROT_90;
107 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
108 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
109 default:
110 return 0;
111 }
112}
113
Ian Elliott8a977262017-01-19 09:05:58 -0700114class TimingInfo {
115 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800116 TimingInfo() = default;
117 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700118 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800119 native_frame_id_(nativeFrameId) {}
120 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700121 return (timestamp_desired_present_time_ !=
122 NATIVE_WINDOW_TIMESTAMP_PENDING &&
123 timestamp_actual_present_time_ !=
124 NATIVE_WINDOW_TIMESTAMP_PENDING &&
125 timestamp_render_complete_time_ !=
126 NATIVE_WINDOW_TIMESTAMP_PENDING &&
127 timestamp_composition_latch_time_ !=
128 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700129 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700130 void calculate(int64_t rdur) {
131 bool anyTimestampInvalid =
132 (timestamp_actual_present_time_ ==
133 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
134 (timestamp_render_complete_time_ ==
135 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
136 (timestamp_composition_latch_time_ ==
137 NATIVE_WINDOW_TIMESTAMP_INVALID);
138 if (anyTimestampInvalid) {
139 ALOGE("Unexpectedly received invalid timestamp.");
140 vals_.actualPresentTime = 0;
141 vals_.earliestPresentTime = 0;
142 vals_.presentMargin = 0;
143 return;
144 }
145
146 vals_.actualPresentTime =
147 static_cast<uint64_t>(timestamp_actual_present_time_);
148 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700149 timestamp_render_complete_time_);
150 // Calculate vals_.earliestPresentTime, and potentially adjust
151 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
152 // is vals_.actualPresentTime. If we can subtract rdur (the duration
153 // of a refresh cycle) from vals_.earliestPresentTime (and also from
154 // vals_.presentMargin) and still leave a positive margin, then we can
155 // report to the application that it could have presented earlier than
156 // it did (per the extension specification). If for some reason, we
157 // can do this subtraction repeatedly, we do, since
158 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700159 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700160 while ((margin > rdur) &&
161 ((early_time - rdur) > timestamp_composition_latch_time_)) {
162 early_time -= rdur;
163 margin -= rdur;
164 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700165 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
166 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700167 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800168 void get_values(VkPastPresentationTimingGOOGLE* values) const {
169 *values = vals_;
170 }
Ian Elliott8a977262017-01-19 09:05:58 -0700171
172 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800173 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700174
Brian Anderson1049d1d2016-12-16 17:25:57 -0800175 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700176 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
177 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
178 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
179 int64_t timestamp_composition_latch_time_
180 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700181};
182
Jesse Halld7b994a2015-09-07 14:17:37 -0700183// ----------------------------------------------------------------------------
184
Jesse Hall1356b0d2015-11-23 17:24:58 -0800185struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800186 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700187 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700188 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800189};
190
191VkSurfaceKHR HandleFromSurface(Surface* surface) {
192 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
193}
194
195Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800196 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800197}
198
Ian Elliott8a977262017-01-19 09:05:58 -0700199// Maximum number of TimingInfo structs to keep per swapchain:
200enum { MAX_TIMING_INFOS = 10 };
201// Minimum number of frames to look for in the past (so we don't cause
202// syncronous requests to Surface Flinger):
203enum { MIN_NUM_FRAMES_AGO = 5 };
204
Jesse Hall1356b0d2015-11-23 17:24:58 -0800205struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700206 Swapchain(Surface& surface_,
207 uint32_t num_images_,
208 VkPresentModeKHR present_mode)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700209 : surface(surface_),
210 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700211 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
Chris Forbesf8835642017-03-30 19:31:40 +1300212 frame_timestamps_enabled(false),
213 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
214 present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700215 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700216 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700217 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700218 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700219 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800220
221 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700222 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700223 bool mailbox_mode;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700224 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700225 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300226 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700227
228 struct Image {
229 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
230 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800231 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700232 // The fence is only valid when the buffer is dequeued, and should be
233 // -1 any other time. When valid, we own the fd, and must ensure it is
234 // closed: either by closing it explicitly when queueing the buffer,
235 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
236 int dequeue_fence;
237 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800238 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700239
Brian Anderson1049d1d2016-12-16 17:25:57 -0800240 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700241};
242
243VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
244 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
245}
246
247Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800248 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700249}
250
Jesse Halldc225072016-05-30 22:40:14 -0700251void ReleaseSwapchainImage(VkDevice device,
252 ANativeWindow* window,
253 int release_fence,
254 Swapchain::Image& image) {
255 ALOG_ASSERT(release_fence == -1 || image.dequeued,
256 "ReleaseSwapchainImage: can't provide a release fence for "
257 "non-dequeued images");
258
259 if (image.dequeued) {
260 if (release_fence >= 0) {
261 // We get here from vkQueuePresentKHR. The application is
262 // responsible for creating an execution dependency chain from
263 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
264 // (release_fence), so we can drop the dequeue_fence here.
265 if (image.dequeue_fence >= 0)
266 close(image.dequeue_fence);
267 } else {
268 // We get here during swapchain destruction, or various serious
269 // error cases e.g. when we can't create the release_fence during
270 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
271 // have already signalled, since the swapchain images are supposed
272 // to be idle before the swapchain is destroyed. In error cases,
273 // there may be rendering in flight to the image, but since we
274 // weren't able to create a release_fence, waiting for the
275 // dequeue_fence is about the best we can do.
276 release_fence = image.dequeue_fence;
277 }
278 image.dequeue_fence = -1;
279
280 if (window) {
281 window->cancelBuffer(window, image.buffer.get(), release_fence);
282 } else {
283 if (release_fence >= 0) {
284 sync_wait(release_fence, -1 /* forever */);
285 close(release_fence);
286 }
287 }
288
289 image.dequeued = false;
290 }
291
292 if (image.image) {
293 GetData(device).driver.DestroyImage(device, image.image, nullptr);
294 image.image = VK_NULL_HANDLE;
295 }
296
297 image.buffer.clear();
298}
299
300void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
301 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
302 return;
Jesse Halldc225072016-05-30 22:40:14 -0700303 for (uint32_t i = 0; i < swapchain->num_images; i++) {
304 if (!swapchain->images[i].dequeued)
305 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
306 }
307 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700308 swapchain->timing.clear();
309}
310
311uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800312 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
313 return 0;
314 }
Ian Elliott8a977262017-01-19 09:05:58 -0700315
Brian Anderson1049d1d2016-12-16 17:25:57 -0800316 uint32_t num_ready = 0;
317 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
318 for (uint32_t i = 0; i < num_timings; i++) {
319 TimingInfo& ti = swapchain.timing.editItemAt(i);
320 if (ti.ready()) {
321 // This TimingInfo is ready to be reported to the user. Add it
322 // to the num_ready.
323 num_ready++;
324 continue;
325 }
326 // This TimingInfo is not yet ready to be reported to the user,
327 // and so we should look for any available timestamps that
328 // might make it ready.
329 int64_t desired_present_time = 0;
330 int64_t render_complete_time = 0;
331 int64_t composition_latch_time = 0;
332 int64_t actual_present_time = 0;
333 // Obtain timestamps:
334 int ret = native_window_get_frame_timestamps(
335 swapchain.surface.window.get(), ti.native_frame_id_,
336 &desired_present_time, &render_complete_time,
337 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700338 nullptr, //&first_composition_start_time,
339 nullptr, //&last_composition_start_time,
340 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800341 // TODO(ianelliott): Maybe ask if this one is
342 // supported, at startup time (since it may not be
343 // supported):
344 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700345 nullptr, //&dequeue_ready_time,
346 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800347
348 if (ret != android::NO_ERROR) {
349 continue;
350 }
351
352 // Record the timestamp(s) we received, and then see if this TimingInfo
353 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700354 ti.timestamp_desired_present_time_ = desired_present_time;
355 ti.timestamp_actual_present_time_ = actual_present_time;
356 ti.timestamp_render_complete_time_ = render_complete_time;
357 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800358
359 if (ti.ready()) {
360 // The TimingInfo has received enough timestamps, and should now
361 // use those timestamps to calculate the info that should be
362 // reported to the user:
363 ti.calculate(swapchain.refresh_duration);
364 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700365 }
366 }
367 return num_ready;
368}
369
370// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
371void copy_ready_timings(Swapchain& swapchain,
372 uint32_t* count,
373 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800374 if (swapchain.timing.empty()) {
375 *count = 0;
376 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700377 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800378
379 size_t last_ready = swapchain.timing.size() - 1;
380 while (!swapchain.timing[last_ready].ready()) {
381 if (last_ready == 0) {
382 *count = 0;
383 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700384 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800385 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700386 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800387
388 uint32_t num_copied = 0;
389 size_t num_to_remove = 0;
390 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
391 const TimingInfo& ti = swapchain.timing[i];
392 if (ti.ready()) {
393 ti.get_values(&timings[num_copied]);
394 num_copied++;
395 }
396 num_to_remove++;
397 }
398
399 // Discard old frames that aren't ready if newer frames are ready.
400 // We don't expect to get the timing info for those old frames.
401 swapchain.timing.removeItemsAt(0, num_to_remove);
402
Ian Elliott8a977262017-01-19 09:05:58 -0700403 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700404}
405
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700406android_pixel_format GetNativePixelFormat(VkFormat format) {
407 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
408 switch (format) {
409 case VK_FORMAT_R8G8B8A8_UNORM:
410 case VK_FORMAT_R8G8B8A8_SRGB:
411 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
412 break;
413 case VK_FORMAT_R5G6B5_UNORM_PACK16:
414 native_format = HAL_PIXEL_FORMAT_RGB_565;
415 break;
416 case VK_FORMAT_R16G16B16A16_SFLOAT:
417 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
418 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800419 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700420 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
421 break;
422 default:
423 ALOGV("unsupported swapchain format %d", format);
424 break;
425 }
426 return native_format;
427}
428
429android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
430 switch (colorspace) {
431 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
432 return HAL_DATASPACE_V0_SRGB;
433 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
434 return HAL_DATASPACE_DISPLAY_P3;
435 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
436 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600437 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
438 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700439 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
440 return HAL_DATASPACE_DCI_P3_LINEAR;
441 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
442 return HAL_DATASPACE_DCI_P3;
443 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
444 return HAL_DATASPACE_V0_SRGB_LINEAR;
445 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
446 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600447 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
448 return HAL_DATASPACE_BT2020_LINEAR;
449 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700450 return static_cast<android_dataspace>(
451 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
452 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600453 case VK_COLOR_SPACE_DOLBYVISION_EXT:
454 return static_cast<android_dataspace>(
455 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
456 HAL_DATASPACE_RANGE_FULL);
457 case VK_COLOR_SPACE_HDR10_HLG_EXT:
458 return static_cast<android_dataspace>(
459 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
460 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700461 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
462 return static_cast<android_dataspace>(
463 HAL_DATASPACE_STANDARD_ADOBE_RGB |
464 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
465 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
466 return HAL_DATASPACE_ADOBE_RGB;
467
468 // Pass through is intended to allow app to provide data that is passed
469 // to the display system without modification.
470 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
471 return HAL_DATASPACE_ARBITRARY;
472
473 default:
474 // This indicates that we don't know about the
475 // dataspace specified and we should indicate that
476 // it's unsupported
477 return HAL_DATASPACE_UNKNOWN;
478 }
479}
480
Jesse Halld7b994a2015-09-07 14:17:37 -0700481} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700482
Jesse Halle1b12782015-11-30 11:27:32 -0800483VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800484VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800485 VkInstance instance,
486 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
487 const VkAllocationCallbacks* allocator,
488 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800489 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800490 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800491 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
492 alignof(Surface),
493 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800494 if (!mem)
495 return VK_ERROR_OUT_OF_HOST_MEMORY;
496 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700497
Chia-I Wue8e689f2016-04-18 08:21:31 +0800498 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700499 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700500 int err = native_window_get_consumer_usage(surface->window.get(),
501 &surface->consumer_usage);
502 if (err != android::NO_ERROR) {
503 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
504 strerror(-err), err);
505 surface->~Surface();
506 allocator->pfnFree(allocator->pUserData, surface);
507 return VK_ERROR_INITIALIZATION_FAILED;
508 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700509
Jesse Hall1356b0d2015-11-23 17:24:58 -0800510 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
Yiwei Zhang6435b322018-05-08 11:12:17 -0700511 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800512 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
513 if (err != 0) {
514 // TODO(jessehall): Improve error reporting. Can we enumerate possible
515 // errors and translate them to valid Vulkan result codes?
516 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
517 err);
518 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800519 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700520 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800521 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700522
Jesse Hall1356b0d2015-11-23 17:24:58 -0800523 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700524 return VK_SUCCESS;
525}
526
Jesse Halle1b12782015-11-30 11:27:32 -0800527VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800528void DestroySurfaceKHR(VkInstance instance,
529 VkSurfaceKHR surface_handle,
530 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800531 Surface* surface = SurfaceFromHandle(surface_handle);
532 if (!surface)
533 return;
534 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700535 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700536 "destroyed VkSurfaceKHR 0x%" PRIx64
537 " has active VkSwapchainKHR 0x%" PRIx64,
538 reinterpret_cast<uint64_t>(surface_handle),
539 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800540 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800541 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800542 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800543 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800544}
545
Jesse Halle1b12782015-11-30 11:27:32 -0800546VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800547VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
548 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700549 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800550 VkBool32* supported) {
Yiwei Zhang6435b322018-05-08 11:12:17 -0700551 const Surface* surface = SurfaceFromHandle(surface_handle);
552 if (!surface) {
553 return VK_ERROR_SURFACE_LOST_KHR;
554 }
555 const ANativeWindow* window = surface->window.get();
556
557 int query_value;
558 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
559 if (err != 0 || query_value < 0) {
560 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
561 strerror(-err), err, query_value);
562 return VK_ERROR_SURFACE_LOST_KHR;
563 }
564
565 android_pixel_format native_format =
566 static_cast<android_pixel_format>(query_value);
567
568 bool format_supported = false;
569 switch (native_format) {
570 case HAL_PIXEL_FORMAT_RGBA_8888:
571 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700572 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800573 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700574 format_supported = true;
575 break;
576 default:
577 break;
578 }
579
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700580 *supported = static_cast<VkBool32>(
581 format_supported || (surface->consumer_usage &
582 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
583 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700584
Jesse Halla6429252015-11-29 18:59:42 -0800585 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800586}
587
Jesse Halle1b12782015-11-30 11:27:32 -0800588VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800589VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800590 VkPhysicalDevice /*pdev*/,
591 VkSurfaceKHR surface,
592 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700593 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800594 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700595
596 int width, height;
597 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
598 if (err != 0) {
599 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
600 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700601 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700602 }
603 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
604 if (err != 0) {
605 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
606 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700607 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700608 }
609
Jesse Hall55bc0972016-02-23 16:43:29 -0800610 int transform_hint;
611 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
612 if (err != 0) {
613 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
614 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700615 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800616 }
617
Jesse Halld7b994a2015-09-07 14:17:37 -0700618 // TODO(jessehall): Figure out what the min/max values should be.
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800619 int max_buffer_count;
620 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
621 if (err != 0) {
622 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
623 strerror(-err), err);
624 return VK_ERROR_SURFACE_LOST_KHR;
625 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700626 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800627 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700628
Jesse Hallfe2662d2016-02-09 13:26:59 -0800629 capabilities->currentExtent =
630 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
631
Jesse Halld7b994a2015-09-07 14:17:37 -0700632 // TODO(jessehall): Figure out what the max extent should be. Maximum
633 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800634 capabilities->minImageExtent = VkExtent2D{1, 1};
635 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700636
Jesse Hallfe2662d2016-02-09 13:26:59 -0800637 capabilities->maxImageArrayLayers = 1;
638
Jesse Hall55bc0972016-02-23 16:43:29 -0800639 capabilities->supportedTransforms = kSupportedTransforms;
640 capabilities->currentTransform =
641 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700642
Jesse Hallfe2662d2016-02-09 13:26:59 -0800643 // On Android, window composition is a WindowManager property, not something
644 // associated with the bufferqueue. It can't be changed from here.
645 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700646
647 // TODO(jessehall): I think these are right, but haven't thought hard about
648 // it. Do we need to query the driver for support of any of these?
649 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700650 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
651 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800652 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800653 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
654 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
655 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700656 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
657
Jesse Hallb1352bc2015-09-04 16:12:33 -0700658 return VK_SUCCESS;
659}
660
Jesse Halle1b12782015-11-30 11:27:32 -0800661VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700662VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
663 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800664 uint32_t* count,
665 VkSurfaceFormatKHR* formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700666 const InstanceData& instance_data = GetData(pdev);
667
Jesse Hall1356b0d2015-11-23 17:24:58 -0800668 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
669 // a new gralloc method to query whether a (format, usage) pair is
670 // supported, and check that for each gralloc format that corresponds to a
671 // Vulkan format. Shorter term, just add a few more formats to the ones
672 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700673
674 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700675 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
676 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
677 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700678 };
679 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700680 uint32_t total_num_formats = kNumFormats;
681
682 bool wide_color_support = false;
683 Surface& surface = *SurfaceFromHandle(surface_handle);
684 int err = native_window_get_wide_color_support(surface.window.get(),
685 &wide_color_support);
686 if (err) {
687 // Not allowed to return a more sensible error code, so do this
688 return VK_ERROR_OUT_OF_HOST_MEMORY;
689 }
690 ALOGV("wide_color_support is: %d", wide_color_support);
691 wide_color_support =
692 wide_color_support &&
693 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
694
695 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600696 {VK_FORMAT_R8G8B8A8_UNORM,
697 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
698 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700699 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700700 {VK_FORMAT_R16G16B16A16_SFLOAT,
701 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
702 {VK_FORMAT_R16G16B16A16_SFLOAT,
703 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800704 {VK_FORMAT_A2B10G10R10_UNORM_PACK32,
705 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700706 };
707 const uint32_t kNumWideColorFormats =
708 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
709 if (wide_color_support) {
710 total_num_formats += kNumWideColorFormats;
711 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700712
713 VkResult result = VK_SUCCESS;
714 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700715 uint32_t out_count = 0;
716 uint32_t transfer_count = 0;
717 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700718 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700719 transfer_count = std::min(*count, kNumFormats);
720 std::copy(kFormats, kFormats + transfer_count, formats);
721 out_count += transfer_count;
722 if (wide_color_support) {
723 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
724 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
725 formats + out_count);
726 out_count += transfer_count;
727 }
728 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700729 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700730 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700731 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700732 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700733}
734
Jesse Halle1b12782015-11-30 11:27:32 -0800735VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300736VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
737 VkPhysicalDevice physicalDevice,
738 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
739 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
740 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
741 physicalDevice, pSurfaceInfo->surface,
742 &pSurfaceCapabilities->surfaceCapabilities);
743
Chris Forbes06bc0092017-03-16 16:46:05 +1300744 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
745 while (caps->pNext) {
746 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
747
748 switch (caps->sType) {
749 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
750 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
751 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
752 caps);
753 // Claim same set of usage flags are supported for
754 // shared present modes as for other modes.
755 shared_caps->sharedPresentSupportedUsageFlags =
756 pSurfaceCapabilities->surfaceCapabilities
757 .supportedUsageFlags;
758 } break;
759
760 default:
761 // Ignore all other extension structs
762 break;
763 }
764 }
765
Chris Forbes2452cf72017-03-16 16:30:17 +1300766 return result;
767}
768
769VKAPI_ATTR
770VkResult GetPhysicalDeviceSurfaceFormats2KHR(
771 VkPhysicalDevice physicalDevice,
772 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
773 uint32_t* pSurfaceFormatCount,
774 VkSurfaceFormat2KHR* pSurfaceFormats) {
775 if (!pSurfaceFormats) {
776 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
777 pSurfaceInfo->surface,
778 pSurfaceFormatCount, nullptr);
779 } else {
780 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
781 // after the call.
782 android::Vector<VkSurfaceFormatKHR> surface_formats;
783 surface_formats.resize(*pSurfaceFormatCount);
784 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
785 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
786 &surface_formats.editItemAt(0));
787
788 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
789 // marshal results individually due to stride difference.
790 // completely ignore any chained extension structs.
791 uint32_t formats_to_marshal = *pSurfaceFormatCount;
792 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
793 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
794 }
795 }
796
797 return result;
798 }
799}
800
801VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300802VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800803 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800804 uint32_t* count,
805 VkPresentModeKHR* modes) {
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800806 int err;
807 int query_value;
808 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
809
810 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
811 if (err != 0 || query_value < 0) {
812 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
813 strerror(-err), err, query_value);
814 return VK_ERROR_SURFACE_LOST_KHR;
815 }
816 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
817
818 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
819 if (err != 0 || query_value < 0) {
820 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
821 strerror(-err), err, query_value);
822 return VK_ERROR_SURFACE_LOST_KHR;
823 }
824 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
825
Chris Forbese8d79a62017-02-22 12:49:18 +1300826 android::Vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800827 if (min_undequeued_buffers + 1 < max_buffer_count)
828 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300829 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
830
831 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
832 if (QueryPresentationProperties(pdev, &present_properties)) {
833 if (present_properties.sharedImage) {
834 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
835 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
836 }
837 }
838
839 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700840
841 VkResult result = VK_SUCCESS;
842 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300843 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700844 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300845 *count = std::min(*count, num_modes);
846 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700847 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300848 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700849 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700850 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700851}
852
Jesse Halle1b12782015-11-30 11:27:32 -0800853VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400854VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600855 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400856 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400857 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
858 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
859 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
860 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
861 pDeviceGroupPresentCapabilities->sType);
862
863 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
864 sizeof(pDeviceGroupPresentCapabilities->presentMask));
865
866 // assume device group of size 1
867 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
868 pDeviceGroupPresentCapabilities->modes =
869 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
870
871 return VK_SUCCESS;
872}
873
874VKAPI_ATTR
875VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600876 VkDevice,
877 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400878 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400879 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
880 return VK_SUCCESS;
881}
882
883VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600884VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400885 VkSurfaceKHR surface,
886 uint32_t* pRectCount,
887 VkRect2D* pRects) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400888 if (!pRects) {
889 *pRectCount = 1;
890 } else {
891 uint32_t count = std::min(*pRectCount, 1u);
892 bool incomplete = *pRectCount < 1;
893
894 *pRectCount = count;
895
896 if (incomplete) {
897 return VK_INCOMPLETE;
898 }
899
900 int err;
901 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
902
903 int width = 0, height = 0;
904 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
905 if (err != 0) {
906 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
907 strerror(-err), err);
908 }
909 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
910 if (err != 0) {
911 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
912 strerror(-err), err);
913 }
914
915 // TODO: Return something better than "whole window"
916 pRects[0].offset.x = 0;
917 pRects[0].offset.y = 0;
918 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
919 static_cast<uint32_t>(height)};
920 }
921 return VK_SUCCESS;
922}
923
924VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800925VkResult CreateSwapchainKHR(VkDevice device,
926 const VkSwapchainCreateInfoKHR* create_info,
927 const VkAllocationCallbacks* allocator,
928 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700929 int err;
930 VkResult result = VK_SUCCESS;
931
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700932 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
933 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
934 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
935 " oldSwapchain=0x%" PRIx64,
936 reinterpret_cast<uint64_t>(create_info->surface),
937 create_info->minImageCount, create_info->imageFormat,
938 create_info->imageColorSpace, create_info->imageExtent.width,
939 create_info->imageExtent.height, create_info->imageUsage,
940 create_info->preTransform, create_info->presentMode,
941 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
942
Jesse Hall1f91d392015-12-11 16:28:44 -0800943 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800944 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800945
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700946 android_pixel_format native_pixel_format =
947 GetNativePixelFormat(create_info->imageFormat);
948 android_dataspace native_dataspace =
949 GetNativeDataspace(create_info->imageColorSpace);
950 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
951 ALOGE(
952 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
953 "failed: Unsupported color space",
954 create_info->imageColorSpace);
955 return VK_ERROR_INITIALIZATION_FAILED;
956 }
957
Jesse Hall42a9eec2016-06-03 12:39:49 -0700958 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700959 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800960 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700961 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700962 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800963 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700964 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +1300965 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300966 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
967 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700968 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800969 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700970
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700971 Surface& surface = *SurfaceFromHandle(create_info->surface);
972
Jesse Halldc225072016-05-30 22:40:14 -0700973 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -0700974 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -0700975 " because it already has active swapchain 0x%" PRIx64
976 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
977 reinterpret_cast<uint64_t>(create_info->surface),
978 reinterpret_cast<uint64_t>(surface.swapchain_handle),
979 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
980 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
981 }
982 if (create_info->oldSwapchain != VK_NULL_HANDLE)
983 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
984
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700985 // -- Reset the native window --
986 // The native window might have been used previously, and had its properties
987 // changed from defaults. That will affect the answer we get for queries
988 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
989 // attempt such queries.
990
Jesse Halldc225072016-05-30 22:40:14 -0700991 // The native window only allows dequeueing all buffers before any have
992 // been queued, since after that point at least one is assumed to be in
993 // non-FREE state at any given time. Disconnecting and re-connecting
994 // orphans the previous buffers, getting us back to the state where we can
995 // dequeue all buffers.
996 err = native_window_api_disconnect(surface.window.get(),
997 NATIVE_WINDOW_API_EGL);
998 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
999 strerror(-err), err);
1000 err =
1001 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1002 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1003 strerror(-err), err);
1004
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001005 err = native_window_set_buffer_count(surface.window.get(), 0);
1006 if (err != 0) {
1007 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1008 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001009 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001010 }
1011
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301012 int swap_interval =
1013 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1014 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001015 if (err != 0) {
1016 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1017 // errors and translate them to valid Vulkan result codes?
1018 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1019 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001020 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001021 }
1022
Chris Forbesb8042d22017-01-18 18:07:05 +13001023 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1024 if (err != 0) {
1025 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1026 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001027 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001028 }
1029
1030 err = native_window_set_auto_refresh(surface.window.get(), false);
1031 if (err != 0) {
1032 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1033 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001034 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001035 }
1036
Jesse Halld7b994a2015-09-07 14:17:37 -07001037 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001038
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001039 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001040
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001041 err = native_window_set_buffers_format(surface.window.get(),
1042 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001043 if (err != 0) {
1044 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1045 // errors and translate them to valid Vulkan result codes?
1046 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001047 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001048 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001049 }
1050 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001051 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001052 if (err != 0) {
1053 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1054 // errors and translate them to valid Vulkan result codes?
1055 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001056 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001057 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001058 }
1059
Jesse Hall3dd678a2016-01-08 21:52:01 -08001060 err = native_window_set_buffers_dimensions(
1061 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1062 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001063 if (err != 0) {
1064 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1065 // errors and translate them to valid Vulkan result codes?
1066 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1067 create_info->imageExtent.width, create_info->imageExtent.height,
1068 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001069 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001070 }
1071
Jesse Hall178b6962016-02-24 15:39:50 -08001072 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1073 // applied during rendering. native_window_set_transform() expects the
1074 // inverse: the transform the app is requesting that the compositor perform
1075 // during composition. With native windows, pre-transform works by rendering
1076 // with the same transform the compositor is applying (as in Vulkan), but
1077 // then requesting the inverse transform, so that when the compositor does
1078 // it's job the two transforms cancel each other out and the compositor ends
1079 // up applying an identity transform to the app's buffer.
1080 err = native_window_set_buffers_transform(
1081 surface.window.get(),
1082 InvertTransformToNative(create_info->preTransform));
1083 if (err != 0) {
1084 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1085 // errors and translate them to valid Vulkan result codes?
1086 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1087 InvertTransformToNative(create_info->preTransform),
1088 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001089 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001090 }
1091
Jesse Hallf64ca122015-11-03 16:11:10 -08001092 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001093 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001094 if (err != 0) {
1095 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1096 // errors and translate them to valid Vulkan result codes?
1097 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1098 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001099 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001100 }
1101
Chris Forbes97ef4612017-03-30 19:37:50 +13001102 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1103 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1104 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1105 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1106 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1107 if (err != 0) {
1108 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1109 return VK_ERROR_SURFACE_LOST_KHR;
1110 }
1111 }
1112
1113 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1114 err = native_window_set_auto_refresh(surface.window.get(), true);
1115 if (err != 0) {
1116 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1117 return VK_ERROR_SURFACE_LOST_KHR;
1118 }
1119 }
1120
Jesse Halle6080bf2016-02-28 20:58:50 -08001121 int query_value;
1122 err = surface.window->query(surface.window.get(),
1123 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1124 &query_value);
1125 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001126 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1127 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -08001128 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1129 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001130 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001131 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001132 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001133 uint32_t num_images =
1134 (create_info->minImageCount - 1) + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001135
1136 // Lower layer insists that we have at least two buffers. This is wasteful
1137 // and we'd like to relax it in the shared case, but not all the pieces are
1138 // in place for that to work yet. Note we only lie to the lower layer-- we
1139 // don't want to give the app back a swapchain with extra images (which they
1140 // can't actually use!).
1141 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001142 if (err != 0) {
1143 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1144 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001145 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1146 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001147 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001148 }
1149
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001150 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001151 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001152 uint64_t consumer_usage, producer_usage;
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001153 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1154 device, create_info->imageFormat, create_info->imageUsage,
1155 swapchain_image_usage, &consumer_usage, &producer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001156 if (result != VK_SUCCESS) {
1157 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001158 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001159 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001160 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001161 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001162 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001163 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001164 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001165 &legacy_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001166 if (result != VK_SUCCESS) {
1167 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001168 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001169 }
Jesse Hall70f93352015-11-04 09:41:31 -08001170 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001171 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1172
1173 bool createProtectedSwapchain = false;
1174 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1175 createProtectedSwapchain = true;
1176 native_usage |= BufferUsage::PROTECTED;
1177 }
1178 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001179 if (err != 0) {
1180 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1181 // errors and translate them to valid Vulkan result codes?
1182 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001183 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001184 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001185
1186 // -- Allocate our Swapchain object --
1187 // After this point, we must deallocate the swapchain on error.
1188
Jesse Hall1f91d392015-12-11 16:28:44 -08001189 void* mem = allocator->pfnAllocation(allocator->pUserData,
1190 sizeof(Swapchain), alignof(Swapchain),
1191 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001192 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001193 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001194 Swapchain* swapchain =
1195 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001196
1197 // -- Dequeue all buffers and create a VkImage for each --
1198 // Any failures during or after this must cancel the dequeued buffers.
1199
Chris Forbesb56287a2017-01-12 14:28:58 +13001200 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1201#pragma clang diagnostic push
1202#pragma clang diagnostic ignored "-Wold-style-cast"
1203 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1204#pragma clang diagnostic pop
1205 .pNext = nullptr,
1206 .usage = swapchain_image_usage,
1207 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001208 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001209#pragma clang diagnostic push
1210#pragma clang diagnostic ignored "-Wold-style-cast"
1211 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1212#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001213 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001214 };
1215 VkImageCreateInfo image_create = {
1216 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1217 .pNext = &image_native_buffer,
1218 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001219 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001220 .extent = {0, 0, 1},
1221 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001222 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001223 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001224 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001225 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001226 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001227 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001228 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001229 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1230 };
1231
Jesse Halld7b994a2015-09-07 14:17:37 -07001232 for (uint32_t i = 0; i < num_images; i++) {
1233 Swapchain::Image& img = swapchain->images[i];
1234
1235 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001236 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1237 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001238 if (err != 0) {
1239 // TODO(jessehall): Improve error reporting. Can we enumerate
1240 // possible errors and translate them to valid Vulkan result codes?
1241 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001242 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001243 break;
1244 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001245 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001246 img.dequeued = true;
1247
1248 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001249 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1250 static_cast<uint32_t>(img.buffer->height),
1251 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001252 image_native_buffer.handle = img.buffer->handle;
1253 image_native_buffer.stride = img.buffer->stride;
1254 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001255 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001256 android_convertGralloc0To1Usage(int(img.buffer->usage),
1257 &image_native_buffer.usage2.producer,
1258 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001259
Jesse Hall03b6fe12015-11-24 12:44:21 -08001260 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001261 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -07001262 if (result != VK_SUCCESS) {
1263 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1264 break;
1265 }
1266 }
1267
1268 // -- Cancel all buffers, returning them to the queue --
1269 // If an error occurred before, also destroy the VkImage and release the
1270 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1271 //
1272 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1273 // but not the non-error path. Should refactor/unify.
Chris Forbes31b85c22018-05-29 15:03:28 -07001274 for (uint32_t i = 0; i < num_images; i++) {
1275 Swapchain::Image& img = swapchain->images[i];
1276 if (img.dequeued) {
1277 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001278 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1279 img.dequeue_fence);
1280 img.dequeue_fence = -1;
1281 img.dequeued = false;
1282 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001283 }
1284 if (result != VK_SUCCESS) {
1285 if (img.image)
1286 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -07001287 }
1288 }
1289
1290 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001291 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001292 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001293 return result;
1294 }
1295
Jesse Halldc225072016-05-30 22:40:14 -07001296 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1297 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001298 return VK_SUCCESS;
1299}
1300
Jesse Halle1b12782015-11-30 11:27:32 -08001301VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001302void DestroySwapchainKHR(VkDevice device,
1303 VkSwapchainKHR swapchain_handle,
1304 const VkAllocationCallbacks* allocator) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001305 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001306 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001307 if (!swapchain)
1308 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001309 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1310 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001311
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001312 if (swapchain->frame_timestamps_enabled) {
1313 native_window_enable_frame_timestamps(window, false);
1314 }
Jesse Halldc225072016-05-30 22:40:14 -07001315 for (uint32_t i = 0; i < swapchain->num_images; i++)
1316 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001317 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001318 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001319 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001320 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001321 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001322 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001323}
1324
Jesse Halle1b12782015-11-30 11:27:32 -08001325VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001326VkResult GetSwapchainImagesKHR(VkDevice,
1327 VkSwapchainKHR swapchain_handle,
1328 uint32_t* count,
1329 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001330 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001331 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1332 "getting images for non-active swapchain 0x%" PRIx64
1333 "; only dequeued image handles are valid",
1334 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001335 VkResult result = VK_SUCCESS;
1336 if (images) {
1337 uint32_t n = swapchain.num_images;
1338 if (*count < swapchain.num_images) {
1339 n = *count;
1340 result = VK_INCOMPLETE;
1341 }
1342 for (uint32_t i = 0; i < n; i++)
1343 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001344 *count = n;
1345 } else {
1346 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001347 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001348 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001349}
1350
Jesse Halle1b12782015-11-30 11:27:32 -08001351VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001352VkResult AcquireNextImageKHR(VkDevice device,
1353 VkSwapchainKHR swapchain_handle,
1354 uint64_t timeout,
1355 VkSemaphore semaphore,
1356 VkFence vk_fence,
1357 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001358 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001359 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001360 VkResult result;
1361 int err;
1362
Jesse Halldc225072016-05-30 22:40:14 -07001363 if (swapchain.surface.swapchain_handle != swapchain_handle)
1364 return VK_ERROR_OUT_OF_DATE_KHR;
1365
Jesse Halld7b994a2015-09-07 14:17:37 -07001366 ALOGW_IF(
1367 timeout != UINT64_MAX,
1368 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1369
Chris Forbesc88409c2017-03-30 19:47:37 +13001370 if (swapchain.shared) {
1371 // In shared mode, we keep the buffer dequeued all the time, so we don't
1372 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1373 // the semaphore and fence passed to us will be signalled.
1374 *image_index = 0;
1375 result = GetData(device).driver.AcquireImageANDROID(
1376 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1377 return result;
1378 }
1379
Jesse Halld7b994a2015-09-07 14:17:37 -07001380 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001381 int fence_fd;
1382 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001383 if (err != 0) {
1384 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1385 // errors and translate them to valid Vulkan result codes?
1386 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001387 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001388 }
1389
1390 uint32_t idx;
1391 for (idx = 0; idx < swapchain.num_images; idx++) {
1392 if (swapchain.images[idx].buffer.get() == buffer) {
1393 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001394 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001395 break;
1396 }
1397 }
1398 if (idx == swapchain.num_images) {
1399 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001400 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001401 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001402 }
1403
1404 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001405 if (fence_fd != -1) {
1406 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001407 if (fence_clone == -1) {
1408 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1409 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001410 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001411 }
1412 }
1413
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001414 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001415 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001416 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001417 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1418 // even if the call fails. We could close it ourselves on failure, but
1419 // that would create a race condition if the driver closes it on a
1420 // failure path: some other thread might create an fd with the same
1421 // number between the time the driver closes it and the time we close
1422 // it. We must assume one of: the driver *always* closes it even on
1423 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001424 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001425 swapchain.images[idx].dequeued = false;
1426 swapchain.images[idx].dequeue_fence = -1;
1427 return result;
1428 }
1429
1430 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001431 return VK_SUCCESS;
1432}
1433
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001434VKAPI_ATTR
1435VkResult AcquireNextImage2KHR(VkDevice device,
1436 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1437 uint32_t* pImageIndex) {
1438 // TODO: this should actually be the other way around and this function
1439 // should handle any additional structures that get passed in
1440 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1441 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1442 pAcquireInfo->fence, pImageIndex);
1443}
1444
Jesse Halldc225072016-05-30 22:40:14 -07001445static VkResult WorstPresentResult(VkResult a, VkResult b) {
1446 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1447 // (in spec version 1.0.14).
1448 static const VkResult kWorstToBest[] = {
1449 VK_ERROR_DEVICE_LOST,
1450 VK_ERROR_SURFACE_LOST_KHR,
1451 VK_ERROR_OUT_OF_DATE_KHR,
1452 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1453 VK_ERROR_OUT_OF_HOST_MEMORY,
1454 VK_SUBOPTIMAL_KHR,
1455 };
1456 for (auto result : kWorstToBest) {
1457 if (a == result || b == result)
1458 return result;
1459 }
1460 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1461 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1462 return a != VK_SUCCESS ? a : b;
1463}
1464
Jesse Halle1b12782015-11-30 11:27:32 -08001465VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001466VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001467 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1468 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1469 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001470
Jesse Halldc225072016-05-30 22:40:14 -07001471 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001472 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001473 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001474
Ian Elliottcb351132016-12-13 10:30:40 -07001475 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001476 const VkPresentRegionsKHR* present_regions = nullptr;
1477 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001478 const VkPresentRegionsKHR* next =
1479 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1480 while (next) {
1481 switch (next->sType) {
1482 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1483 present_regions = next;
1484 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001485 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001486 present_times =
1487 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1488 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001489 default:
1490 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1491 next->sType);
1492 break;
1493 }
1494 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1495 }
1496 ALOGV_IF(
1497 present_regions &&
1498 present_regions->swapchainCount != present_info->swapchainCount,
1499 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001500 ALOGV_IF(present_times &&
1501 present_times->swapchainCount != present_info->swapchainCount,
1502 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1503 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001504 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001505 (present_regions) ? present_regions->pRegions : nullptr;
1506 const VkPresentTimeGOOGLE* times =
1507 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001508 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001509 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001510 uint32_t nrects = 0;
1511
Jesse Halld7b994a2015-09-07 14:17:37 -07001512 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1513 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001514 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001515 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001516 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001517 const VkPresentRegionKHR* region =
1518 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001519 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001520 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001521 VkResult result;
1522 int err;
1523
Jesse Halld7b994a2015-09-07 14:17:37 -07001524 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001525 result = dispatch.QueueSignalReleaseImageANDROID(
1526 queue, present_info->waitSemaphoreCount,
1527 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001528 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001529 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001530 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001531 }
1532
Jesse Halldc225072016-05-30 22:40:14 -07001533 if (swapchain.surface.swapchain_handle ==
1534 present_info->pSwapchains[sc]) {
1535 ANativeWindow* window = swapchain.surface.window.get();
1536 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001537 if (region) {
1538 // Process the incremental-present hint for this swapchain:
1539 uint32_t rcount = region->rectangleCount;
1540 if (rcount > nrects) {
1541 android_native_rect_t* new_rects =
1542 static_cast<android_native_rect_t*>(
1543 allocator->pfnReallocation(
1544 allocator->pUserData, rects,
1545 sizeof(android_native_rect_t) * rcount,
1546 alignof(android_native_rect_t),
1547 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1548 if (new_rects) {
1549 rects = new_rects;
1550 nrects = rcount;
1551 } else {
1552 rcount = 0; // Ignore the hint for this swapchain
1553 }
1554 }
1555 for (uint32_t r = 0; r < rcount; ++r) {
1556 if (region->pRectangles[r].layer > 0) {
1557 ALOGV(
1558 "vkQueuePresentKHR ignoring invalid layer "
1559 "(%u); using layer 0 instead",
1560 region->pRectangles[r].layer);
1561 }
1562 int x = region->pRectangles[r].offset.x;
1563 int y = region->pRectangles[r].offset.y;
1564 int width = static_cast<int>(
1565 region->pRectangles[r].extent.width);
1566 int height = static_cast<int>(
1567 region->pRectangles[r].extent.height);
1568 android_native_rect_t* cur_rect = &rects[r];
1569 cur_rect->left = x;
1570 cur_rect->top = y + height;
1571 cur_rect->right = x + width;
1572 cur_rect->bottom = y;
1573 }
1574 native_window_set_surface_damage(window, rects, rcount);
1575 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001576 if (time) {
1577 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001578 ALOGV(
1579 "Calling "
1580 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001581 native_window_enable_frame_timestamps(window, true);
1582 swapchain.frame_timestamps_enabled = true;
1583 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001584
1585 // Record the nativeFrameId so it can be later correlated to
1586 // this present.
1587 uint64_t nativeFrameId = 0;
1588 err = native_window_get_next_frame_id(
1589 window, &nativeFrameId);
1590 if (err != android::NO_ERROR) {
1591 ALOGE("Failed to get next native frame ID.");
1592 }
1593
1594 // Add a new timing record with the user's presentID and
1595 // the nativeFrameId.
1596 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1597 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001598 swapchain.timing.removeAt(0);
1599 }
1600 if (time->desiredPresentTime) {
1601 // Set the desiredPresentTime:
1602 ALOGV(
1603 "Calling "
1604 "native_window_set_buffers_timestamp(%" PRId64 ")",
1605 time->desiredPresentTime);
1606 native_window_set_buffers_timestamp(
1607 window,
1608 static_cast<int64_t>(time->desiredPresentTime));
1609 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001610 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001611
Jesse Halldc225072016-05-30 22:40:14 -07001612 err = window->queueBuffer(window, img.buffer.get(), fence);
1613 // queueBuffer always closes fence, even on error
1614 if (err != 0) {
1615 // TODO(jessehall): What now? We should probably cancel the
1616 // buffer, I guess?
1617 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1618 swapchain_result = WorstPresentResult(
1619 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1620 }
1621 if (img.dequeue_fence >= 0) {
1622 close(img.dequeue_fence);
1623 img.dequeue_fence = -1;
1624 }
1625 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001626
1627 // If the swapchain is in shared mode, immediately dequeue the
1628 // buffer so it can be presented again without an intervening
1629 // call to AcquireNextImageKHR. We expect to get the same buffer
1630 // back from every call to dequeueBuffer in this mode.
1631 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1632 ANativeWindowBuffer* buffer;
1633 int fence_fd;
1634 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1635 if (err != 0) {
1636 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1637 swapchain_result = WorstPresentResult(swapchain_result,
1638 VK_ERROR_SURFACE_LOST_KHR);
1639 }
1640 else if (img.buffer != buffer) {
1641 ALOGE("got wrong image back for shared swapchain");
1642 swapchain_result = WorstPresentResult(swapchain_result,
1643 VK_ERROR_SURFACE_LOST_KHR);
1644 }
1645 else {
1646 img.dequeue_fence = fence_fd;
1647 img.dequeued = true;
1648 }
1649 }
Jesse Halldc225072016-05-30 22:40:14 -07001650 }
1651 if (swapchain_result != VK_SUCCESS) {
1652 ReleaseSwapchainImage(device, window, fence, img);
1653 OrphanSwapchain(device, &swapchain);
1654 }
1655 } else {
1656 ReleaseSwapchainImage(device, nullptr, fence, img);
1657 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001658 }
1659
Jesse Halla9e57032015-11-30 01:03:10 -08001660 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001661 present_info->pResults[sc] = swapchain_result;
1662
1663 if (swapchain_result != final_result)
1664 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001665 }
Ian Elliottcb351132016-12-13 10:30:40 -07001666 if (rects) {
1667 allocator->pfnFree(allocator->pUserData, rects);
1668 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001669
1670 return final_result;
1671}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001672
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001673VKAPI_ATTR
1674VkResult GetRefreshCycleDurationGOOGLE(
1675 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001676 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001677 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Ian Elliott62c48c92017-01-20 13:13:20 -07001678 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001679 VkResult result = VK_SUCCESS;
1680
Brian Andersondc96fdf2017-03-20 16:54:25 -07001681 pDisplayTimingProperties->refreshDuration =
1682 static_cast<uint64_t>(swapchain.refresh_duration);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001683
1684 return result;
1685}
1686
1687VKAPI_ATTR
1688VkResult GetPastPresentationTimingGOOGLE(
1689 VkDevice,
1690 VkSwapchainKHR swapchain_handle,
1691 uint32_t* count,
1692 VkPastPresentationTimingGOOGLE* timings) {
1693 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1694 ANativeWindow* window = swapchain.surface.window.get();
1695 VkResult result = VK_SUCCESS;
1696
1697 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001698 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001699 native_window_enable_frame_timestamps(window, true);
1700 swapchain.frame_timestamps_enabled = true;
1701 }
1702
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001703 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001704 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1705 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001706 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001707 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001708 }
1709
1710 return result;
1711}
1712
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001713VKAPI_ATTR
1714VkResult GetSwapchainStatusKHR(
1715 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001716 VkSwapchainKHR swapchain_handle) {
1717 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001718 VkResult result = VK_SUCCESS;
1719
Chris Forbes4e18ba82017-01-20 12:50:17 +13001720 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1721 return VK_ERROR_OUT_OF_DATE_KHR;
1722 }
1723
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001724 // TODO(chrisforbes): Implement this function properly
1725
1726 return result;
1727}
1728
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001729VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001730 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001731 uint32_t swapchainCount,
1732 const VkSwapchainKHR* pSwapchains,
1733 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001734
1735 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1736 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1737 if (!swapchain)
1738 continue;
1739
1740 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1741
1742 ANativeWindow* window = swapchain->surface.window.get();
1743
1744 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1745 const android_smpte2086_metadata smpteMetdata = {
1746 {vulkanMetadata.displayPrimaryRed.x,
1747 vulkanMetadata.displayPrimaryRed.y},
1748 {vulkanMetadata.displayPrimaryGreen.x,
1749 vulkanMetadata.displayPrimaryGreen.y},
1750 {vulkanMetadata.displayPrimaryBlue.x,
1751 vulkanMetadata.displayPrimaryBlue.y},
1752 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1753 vulkanMetadata.maxLuminance,
1754 vulkanMetadata.minLuminance};
1755 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1756
1757 const android_cta861_3_metadata cta8613Metadata = {
1758 vulkanMetadata.maxContentLightLevel,
1759 vulkanMetadata.maxFrameAverageLightLevel};
1760 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1761 }
1762
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001763 return;
1764}
1765
Chia-I Wu62262232016-03-26 07:06:44 +08001766} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001767} // namespace vulkan