blob: 915de45961a8ae6e92ebc9f777521cd5509ea7ee [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;
419 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
420 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:
572 format_supported = true;
573 break;
574 default:
575 break;
576 }
577
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700578 *supported = static_cast<VkBool32>(
579 format_supported || (surface->consumer_usage &
580 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
581 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700582
Jesse Halla6429252015-11-29 18:59:42 -0800583 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800584}
585
Jesse Halle1b12782015-11-30 11:27:32 -0800586VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800587VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800588 VkPhysicalDevice /*pdev*/,
589 VkSurfaceKHR surface,
590 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700591 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800592 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700593
594 int width, height;
595 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
596 if (err != 0) {
597 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
598 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700599 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700600 }
601 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
602 if (err != 0) {
603 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
604 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700605 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700606 }
607
Jesse Hall55bc0972016-02-23 16:43:29 -0800608 int transform_hint;
609 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
610 if (err != 0) {
611 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
612 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700613 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800614 }
615
Jesse Halld7b994a2015-09-07 14:17:37 -0700616 // TODO(jessehall): Figure out what the min/max values should be.
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800617 int max_buffer_count;
618 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
619 if (err != 0) {
620 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
621 strerror(-err), err);
622 return VK_ERROR_SURFACE_LOST_KHR;
623 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700624 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800625 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700626
Jesse Hallfe2662d2016-02-09 13:26:59 -0800627 capabilities->currentExtent =
628 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
629
Jesse Halld7b994a2015-09-07 14:17:37 -0700630 // TODO(jessehall): Figure out what the max extent should be. Maximum
631 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800632 capabilities->minImageExtent = VkExtent2D{1, 1};
633 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700634
Jesse Hallfe2662d2016-02-09 13:26:59 -0800635 capabilities->maxImageArrayLayers = 1;
636
Jesse Hall55bc0972016-02-23 16:43:29 -0800637 capabilities->supportedTransforms = kSupportedTransforms;
638 capabilities->currentTransform =
639 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700640
Jesse Hallfe2662d2016-02-09 13:26:59 -0800641 // On Android, window composition is a WindowManager property, not something
642 // associated with the bufferqueue. It can't be changed from here.
643 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700644
645 // TODO(jessehall): I think these are right, but haven't thought hard about
646 // it. Do we need to query the driver for support of any of these?
647 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700648 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
649 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800650 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800651 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
652 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
653 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700654 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
655
Jesse Hallb1352bc2015-09-04 16:12:33 -0700656 return VK_SUCCESS;
657}
658
Jesse Halle1b12782015-11-30 11:27:32 -0800659VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700660VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
661 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800662 uint32_t* count,
663 VkSurfaceFormatKHR* formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700664 const InstanceData& instance_data = GetData(pdev);
665
Jesse Hall1356b0d2015-11-23 17:24:58 -0800666 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
667 // a new gralloc method to query whether a (format, usage) pair is
668 // supported, and check that for each gralloc format that corresponds to a
669 // Vulkan format. Shorter term, just add a few more formats to the ones
670 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700671
672 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700673 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
674 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
675 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700676 };
677 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700678 uint32_t total_num_formats = kNumFormats;
679
680 bool wide_color_support = false;
681 Surface& surface = *SurfaceFromHandle(surface_handle);
682 int err = native_window_get_wide_color_support(surface.window.get(),
683 &wide_color_support);
684 if (err) {
685 // Not allowed to return a more sensible error code, so do this
686 return VK_ERROR_OUT_OF_HOST_MEMORY;
687 }
688 ALOGV("wide_color_support is: %d", wide_color_support);
689 wide_color_support =
690 wide_color_support &&
691 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
692
693 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600694 {VK_FORMAT_R8G8B8A8_UNORM,
695 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
696 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700697 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
698 };
699 const uint32_t kNumWideColorFormats =
700 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
701 if (wide_color_support) {
702 total_num_formats += kNumWideColorFormats;
703 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700704
705 VkResult result = VK_SUCCESS;
706 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700707 uint32_t out_count = 0;
708 uint32_t transfer_count = 0;
709 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700710 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700711 transfer_count = std::min(*count, kNumFormats);
712 std::copy(kFormats, kFormats + transfer_count, formats);
713 out_count += transfer_count;
714 if (wide_color_support) {
715 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
716 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
717 formats + out_count);
718 out_count += transfer_count;
719 }
720 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700721 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700722 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700723 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700724 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700725}
726
Jesse Halle1b12782015-11-30 11:27:32 -0800727VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300728VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
729 VkPhysicalDevice physicalDevice,
730 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
731 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
732 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
733 physicalDevice, pSurfaceInfo->surface,
734 &pSurfaceCapabilities->surfaceCapabilities);
735
Chris Forbes06bc0092017-03-16 16:46:05 +1300736 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
737 while (caps->pNext) {
738 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
739
740 switch (caps->sType) {
741 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
742 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
743 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
744 caps);
745 // Claim same set of usage flags are supported for
746 // shared present modes as for other modes.
747 shared_caps->sharedPresentSupportedUsageFlags =
748 pSurfaceCapabilities->surfaceCapabilities
749 .supportedUsageFlags;
750 } break;
751
752 default:
753 // Ignore all other extension structs
754 break;
755 }
756 }
757
Chris Forbes2452cf72017-03-16 16:30:17 +1300758 return result;
759}
760
761VKAPI_ATTR
762VkResult GetPhysicalDeviceSurfaceFormats2KHR(
763 VkPhysicalDevice physicalDevice,
764 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
765 uint32_t* pSurfaceFormatCount,
766 VkSurfaceFormat2KHR* pSurfaceFormats) {
767 if (!pSurfaceFormats) {
768 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
769 pSurfaceInfo->surface,
770 pSurfaceFormatCount, nullptr);
771 } else {
772 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
773 // after the call.
774 android::Vector<VkSurfaceFormatKHR> surface_formats;
775 surface_formats.resize(*pSurfaceFormatCount);
776 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
777 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
778 &surface_formats.editItemAt(0));
779
780 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
781 // marshal results individually due to stride difference.
782 // completely ignore any chained extension structs.
783 uint32_t formats_to_marshal = *pSurfaceFormatCount;
784 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
785 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
786 }
787 }
788
789 return result;
790 }
791}
792
793VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300794VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800795 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800796 uint32_t* count,
797 VkPresentModeKHR* modes) {
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800798 int err;
799 int query_value;
800 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
801
802 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
803 if (err != 0 || query_value < 0) {
804 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
805 strerror(-err), err, query_value);
806 return VK_ERROR_SURFACE_LOST_KHR;
807 }
808 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
809
810 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
811 if (err != 0 || query_value < 0) {
812 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
813 strerror(-err), err, query_value);
814 return VK_ERROR_SURFACE_LOST_KHR;
815 }
816 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
817
Chris Forbese8d79a62017-02-22 12:49:18 +1300818 android::Vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800819 if (min_undequeued_buffers + 1 < max_buffer_count)
820 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300821 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
822
823 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
824 if (QueryPresentationProperties(pdev, &present_properties)) {
825 if (present_properties.sharedImage) {
826 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
827 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
828 }
829 }
830
831 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700832
833 VkResult result = VK_SUCCESS;
834 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300835 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700836 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300837 *count = std::min(*count, num_modes);
838 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700839 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300840 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700841 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700842 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700843}
844
Jesse Halle1b12782015-11-30 11:27:32 -0800845VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400846VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600847 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400848 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400849 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
850 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
851 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
852 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
853 pDeviceGroupPresentCapabilities->sType);
854
855 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
856 sizeof(pDeviceGroupPresentCapabilities->presentMask));
857
858 // assume device group of size 1
859 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
860 pDeviceGroupPresentCapabilities->modes =
861 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
862
863 return VK_SUCCESS;
864}
865
866VKAPI_ATTR
867VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600868 VkDevice,
869 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400870 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400871 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
872 return VK_SUCCESS;
873}
874
875VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600876VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400877 VkSurfaceKHR surface,
878 uint32_t* pRectCount,
879 VkRect2D* pRects) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400880 if (!pRects) {
881 *pRectCount = 1;
882 } else {
883 uint32_t count = std::min(*pRectCount, 1u);
884 bool incomplete = *pRectCount < 1;
885
886 *pRectCount = count;
887
888 if (incomplete) {
889 return VK_INCOMPLETE;
890 }
891
892 int err;
893 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
894
895 int width = 0, height = 0;
896 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
897 if (err != 0) {
898 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
899 strerror(-err), err);
900 }
901 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
902 if (err != 0) {
903 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
904 strerror(-err), err);
905 }
906
907 // TODO: Return something better than "whole window"
908 pRects[0].offset.x = 0;
909 pRects[0].offset.y = 0;
910 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
911 static_cast<uint32_t>(height)};
912 }
913 return VK_SUCCESS;
914}
915
916VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800917VkResult CreateSwapchainKHR(VkDevice device,
918 const VkSwapchainCreateInfoKHR* create_info,
919 const VkAllocationCallbacks* allocator,
920 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700921 int err;
922 VkResult result = VK_SUCCESS;
923
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700924 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
925 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
926 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
927 " oldSwapchain=0x%" PRIx64,
928 reinterpret_cast<uint64_t>(create_info->surface),
929 create_info->minImageCount, create_info->imageFormat,
930 create_info->imageColorSpace, create_info->imageExtent.width,
931 create_info->imageExtent.height, create_info->imageUsage,
932 create_info->preTransform, create_info->presentMode,
933 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
934
Jesse Hall1f91d392015-12-11 16:28:44 -0800935 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800936 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800937
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700938 android_pixel_format native_pixel_format =
939 GetNativePixelFormat(create_info->imageFormat);
940 android_dataspace native_dataspace =
941 GetNativeDataspace(create_info->imageColorSpace);
942 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
943 ALOGE(
944 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
945 "failed: Unsupported color space",
946 create_info->imageColorSpace);
947 return VK_ERROR_INITIALIZATION_FAILED;
948 }
949
Jesse Hall42a9eec2016-06-03 12:39:49 -0700950 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700951 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800952 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700953 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700954 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800955 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700956 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +1300957 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300958 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
959 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700960 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800961 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700962
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700963 Surface& surface = *SurfaceFromHandle(create_info->surface);
964
Jesse Halldc225072016-05-30 22:40:14 -0700965 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -0700966 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -0700967 " because it already has active swapchain 0x%" PRIx64
968 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
969 reinterpret_cast<uint64_t>(create_info->surface),
970 reinterpret_cast<uint64_t>(surface.swapchain_handle),
971 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
972 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
973 }
974 if (create_info->oldSwapchain != VK_NULL_HANDLE)
975 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
976
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700977 // -- Reset the native window --
978 // The native window might have been used previously, and had its properties
979 // changed from defaults. That will affect the answer we get for queries
980 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
981 // attempt such queries.
982
Jesse Halldc225072016-05-30 22:40:14 -0700983 // The native window only allows dequeueing all buffers before any have
984 // been queued, since after that point at least one is assumed to be in
985 // non-FREE state at any given time. Disconnecting and re-connecting
986 // orphans the previous buffers, getting us back to the state where we can
987 // dequeue all buffers.
988 err = native_window_api_disconnect(surface.window.get(),
989 NATIVE_WINDOW_API_EGL);
990 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
991 strerror(-err), err);
992 err =
993 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
994 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
995 strerror(-err), err);
996
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700997 err = native_window_set_buffer_count(surface.window.get(), 0);
998 if (err != 0) {
999 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1000 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001001 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001002 }
1003
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301004 int swap_interval =
1005 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1006 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001007 if (err != 0) {
1008 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1009 // errors and translate them to valid Vulkan result codes?
1010 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1011 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001012 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001013 }
1014
Chris Forbesb8042d22017-01-18 18:07:05 +13001015 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1016 if (err != 0) {
1017 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1018 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001019 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001020 }
1021
1022 err = native_window_set_auto_refresh(surface.window.get(), false);
1023 if (err != 0) {
1024 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1025 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001026 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001027 }
1028
Jesse Halld7b994a2015-09-07 14:17:37 -07001029 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001030
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001031 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001032
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001033 err = native_window_set_buffers_format(surface.window.get(),
1034 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001035 if (err != 0) {
1036 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1037 // errors and translate them to valid Vulkan result codes?
1038 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001039 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001040 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001041 }
1042 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001043 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001044 if (err != 0) {
1045 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1046 // errors and translate them to valid Vulkan result codes?
1047 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001048 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001049 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001050 }
1051
Jesse Hall3dd678a2016-01-08 21:52:01 -08001052 err = native_window_set_buffers_dimensions(
1053 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1054 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001055 if (err != 0) {
1056 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1057 // errors and translate them to valid Vulkan result codes?
1058 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1059 create_info->imageExtent.width, create_info->imageExtent.height,
1060 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001061 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001062 }
1063
Jesse Hall178b6962016-02-24 15:39:50 -08001064 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1065 // applied during rendering. native_window_set_transform() expects the
1066 // inverse: the transform the app is requesting that the compositor perform
1067 // during composition. With native windows, pre-transform works by rendering
1068 // with the same transform the compositor is applying (as in Vulkan), but
1069 // then requesting the inverse transform, so that when the compositor does
1070 // it's job the two transforms cancel each other out and the compositor ends
1071 // up applying an identity transform to the app's buffer.
1072 err = native_window_set_buffers_transform(
1073 surface.window.get(),
1074 InvertTransformToNative(create_info->preTransform));
1075 if (err != 0) {
1076 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1077 // errors and translate them to valid Vulkan result codes?
1078 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1079 InvertTransformToNative(create_info->preTransform),
1080 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001081 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001082 }
1083
Jesse Hallf64ca122015-11-03 16:11:10 -08001084 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001085 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001086 if (err != 0) {
1087 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1088 // errors and translate them to valid Vulkan result codes?
1089 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1090 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001091 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001092 }
1093
Chris Forbes97ef4612017-03-30 19:37:50 +13001094 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1095 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1096 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1097 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1098 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1099 if (err != 0) {
1100 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1101 return VK_ERROR_SURFACE_LOST_KHR;
1102 }
1103 }
1104
1105 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1106 err = native_window_set_auto_refresh(surface.window.get(), true);
1107 if (err != 0) {
1108 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1109 return VK_ERROR_SURFACE_LOST_KHR;
1110 }
1111 }
1112
Jesse Halle6080bf2016-02-28 20:58:50 -08001113 int query_value;
1114 err = surface.window->query(surface.window.get(),
1115 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1116 &query_value);
1117 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001118 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1119 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -08001120 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1121 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001122 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001123 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001124 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001125 uint32_t num_images =
1126 (create_info->minImageCount - 1) + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001127
1128 // Lower layer insists that we have at least two buffers. This is wasteful
1129 // and we'd like to relax it in the shared case, but not all the pieces are
1130 // in place for that to work yet. Note we only lie to the lower layer-- we
1131 // don't want to give the app back a swapchain with extra images (which they
1132 // can't actually use!).
1133 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001134 if (err != 0) {
1135 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1136 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001137 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1138 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001139 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001140 }
1141
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001142 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001143 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001144 uint64_t consumer_usage, producer_usage;
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001145 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1146 device, create_info->imageFormat, create_info->imageUsage,
1147 swapchain_image_usage, &consumer_usage, &producer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001148 if (result != VK_SUCCESS) {
1149 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001150 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001151 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001152 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001153 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001154 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001155 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001156 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001157 &legacy_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001158 if (result != VK_SUCCESS) {
1159 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001160 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001161 }
Jesse Hall70f93352015-11-04 09:41:31 -08001162 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001163 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1164
1165 bool createProtectedSwapchain = false;
1166 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1167 createProtectedSwapchain = true;
1168 native_usage |= BufferUsage::PROTECTED;
1169 }
1170 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001171 if (err != 0) {
1172 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1173 // errors and translate them to valid Vulkan result codes?
1174 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001175 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001176 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001177
1178 // -- Allocate our Swapchain object --
1179 // After this point, we must deallocate the swapchain on error.
1180
Jesse Hall1f91d392015-12-11 16:28:44 -08001181 void* mem = allocator->pfnAllocation(allocator->pUserData,
1182 sizeof(Swapchain), alignof(Swapchain),
1183 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001184 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001185 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001186 Swapchain* swapchain =
1187 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001188
1189 // -- Dequeue all buffers and create a VkImage for each --
1190 // Any failures during or after this must cancel the dequeued buffers.
1191
Chris Forbesb56287a2017-01-12 14:28:58 +13001192 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1193#pragma clang diagnostic push
1194#pragma clang diagnostic ignored "-Wold-style-cast"
1195 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1196#pragma clang diagnostic pop
1197 .pNext = nullptr,
1198 .usage = swapchain_image_usage,
1199 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001200 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001201#pragma clang diagnostic push
1202#pragma clang diagnostic ignored "-Wold-style-cast"
1203 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1204#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001205 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001206 };
1207 VkImageCreateInfo image_create = {
1208 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1209 .pNext = &image_native_buffer,
1210 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001211 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001212 .extent = {0, 0, 1},
1213 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001214 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001215 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001216 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001217 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001218 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001219 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001220 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001221 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1222 };
1223
Jesse Halld7b994a2015-09-07 14:17:37 -07001224 for (uint32_t i = 0; i < num_images; i++) {
1225 Swapchain::Image& img = swapchain->images[i];
1226
1227 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001228 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1229 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001230 if (err != 0) {
1231 // TODO(jessehall): Improve error reporting. Can we enumerate
1232 // possible errors and translate them to valid Vulkan result codes?
1233 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001234 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001235 break;
1236 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001237 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001238 img.dequeued = true;
1239
1240 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001241 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1242 static_cast<uint32_t>(img.buffer->height),
1243 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001244 image_native_buffer.handle = img.buffer->handle;
1245 image_native_buffer.stride = img.buffer->stride;
1246 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001247 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001248 android_convertGralloc0To1Usage(int(img.buffer->usage),
1249 &image_native_buffer.usage2.producer,
1250 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001251
Jesse Hall03b6fe12015-11-24 12:44:21 -08001252 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001253 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -07001254 if (result != VK_SUCCESS) {
1255 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1256 break;
1257 }
1258 }
1259
1260 // -- Cancel all buffers, returning them to the queue --
1261 // If an error occurred before, also destroy the VkImage and release the
1262 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1263 //
1264 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1265 // but not the non-error path. Should refactor/unify.
Chris Forbes31b85c22018-05-29 15:03:28 -07001266 for (uint32_t i = 0; i < num_images; i++) {
1267 Swapchain::Image& img = swapchain->images[i];
1268 if (img.dequeued) {
1269 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001270 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1271 img.dequeue_fence);
1272 img.dequeue_fence = -1;
1273 img.dequeued = false;
1274 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001275 }
1276 if (result != VK_SUCCESS) {
1277 if (img.image)
1278 dispatch.DestroyImage(device, img.image, nullptr);
Jesse Halld7b994a2015-09-07 14:17:37 -07001279 }
1280 }
1281
1282 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001283 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001284 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001285 return result;
1286 }
1287
Jesse Halldc225072016-05-30 22:40:14 -07001288 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1289 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001290 return VK_SUCCESS;
1291}
1292
Jesse Halle1b12782015-11-30 11:27:32 -08001293VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001294void DestroySwapchainKHR(VkDevice device,
1295 VkSwapchainKHR swapchain_handle,
1296 const VkAllocationCallbacks* allocator) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001297 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001298 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001299 if (!swapchain)
1300 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001301 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1302 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001303
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001304 if (swapchain->frame_timestamps_enabled) {
1305 native_window_enable_frame_timestamps(window, false);
1306 }
Jesse Halldc225072016-05-30 22:40:14 -07001307 for (uint32_t i = 0; i < swapchain->num_images; i++)
1308 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001309 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001310 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001311 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001312 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001313 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001314 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001315}
1316
Jesse Halle1b12782015-11-30 11:27:32 -08001317VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001318VkResult GetSwapchainImagesKHR(VkDevice,
1319 VkSwapchainKHR swapchain_handle,
1320 uint32_t* count,
1321 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001322 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001323 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1324 "getting images for non-active swapchain 0x%" PRIx64
1325 "; only dequeued image handles are valid",
1326 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001327 VkResult result = VK_SUCCESS;
1328 if (images) {
1329 uint32_t n = swapchain.num_images;
1330 if (*count < swapchain.num_images) {
1331 n = *count;
1332 result = VK_INCOMPLETE;
1333 }
1334 for (uint32_t i = 0; i < n; i++)
1335 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001336 *count = n;
1337 } else {
1338 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001339 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001340 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001341}
1342
Jesse Halle1b12782015-11-30 11:27:32 -08001343VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001344VkResult AcquireNextImageKHR(VkDevice device,
1345 VkSwapchainKHR swapchain_handle,
1346 uint64_t timeout,
1347 VkSemaphore semaphore,
1348 VkFence vk_fence,
1349 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001350 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001351 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001352 VkResult result;
1353 int err;
1354
Jesse Halldc225072016-05-30 22:40:14 -07001355 if (swapchain.surface.swapchain_handle != swapchain_handle)
1356 return VK_ERROR_OUT_OF_DATE_KHR;
1357
Jesse Halld7b994a2015-09-07 14:17:37 -07001358 ALOGW_IF(
1359 timeout != UINT64_MAX,
1360 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1361
Chris Forbesc88409c2017-03-30 19:47:37 +13001362 if (swapchain.shared) {
1363 // In shared mode, we keep the buffer dequeued all the time, so we don't
1364 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1365 // the semaphore and fence passed to us will be signalled.
1366 *image_index = 0;
1367 result = GetData(device).driver.AcquireImageANDROID(
1368 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1369 return result;
1370 }
1371
Jesse Halld7b994a2015-09-07 14:17:37 -07001372 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001373 int fence_fd;
1374 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001375 if (err != 0) {
1376 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1377 // errors and translate them to valid Vulkan result codes?
1378 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001379 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001380 }
1381
1382 uint32_t idx;
1383 for (idx = 0; idx < swapchain.num_images; idx++) {
1384 if (swapchain.images[idx].buffer.get() == buffer) {
1385 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001386 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001387 break;
1388 }
1389 }
1390 if (idx == swapchain.num_images) {
1391 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001392 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001393 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001394 }
1395
1396 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001397 if (fence_fd != -1) {
1398 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001399 if (fence_clone == -1) {
1400 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1401 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001402 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001403 }
1404 }
1405
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001406 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001407 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001408 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001409 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1410 // even if the call fails. We could close it ourselves on failure, but
1411 // that would create a race condition if the driver closes it on a
1412 // failure path: some other thread might create an fd with the same
1413 // number between the time the driver closes it and the time we close
1414 // it. We must assume one of: the driver *always* closes it even on
1415 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001416 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001417 swapchain.images[idx].dequeued = false;
1418 swapchain.images[idx].dequeue_fence = -1;
1419 return result;
1420 }
1421
1422 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001423 return VK_SUCCESS;
1424}
1425
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001426VKAPI_ATTR
1427VkResult AcquireNextImage2KHR(VkDevice device,
1428 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1429 uint32_t* pImageIndex) {
1430 // TODO: this should actually be the other way around and this function
1431 // should handle any additional structures that get passed in
1432 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1433 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1434 pAcquireInfo->fence, pImageIndex);
1435}
1436
Jesse Halldc225072016-05-30 22:40:14 -07001437static VkResult WorstPresentResult(VkResult a, VkResult b) {
1438 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1439 // (in spec version 1.0.14).
1440 static const VkResult kWorstToBest[] = {
1441 VK_ERROR_DEVICE_LOST,
1442 VK_ERROR_SURFACE_LOST_KHR,
1443 VK_ERROR_OUT_OF_DATE_KHR,
1444 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1445 VK_ERROR_OUT_OF_HOST_MEMORY,
1446 VK_SUBOPTIMAL_KHR,
1447 };
1448 for (auto result : kWorstToBest) {
1449 if (a == result || b == result)
1450 return result;
1451 }
1452 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1453 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1454 return a != VK_SUCCESS ? a : b;
1455}
1456
Jesse Halle1b12782015-11-30 11:27:32 -08001457VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001458VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001459 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1460 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1461 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001462
Jesse Halldc225072016-05-30 22:40:14 -07001463 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001464 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001465 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001466
Ian Elliottcb351132016-12-13 10:30:40 -07001467 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001468 const VkPresentRegionsKHR* present_regions = nullptr;
1469 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001470 const VkPresentRegionsKHR* next =
1471 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1472 while (next) {
1473 switch (next->sType) {
1474 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1475 present_regions = next;
1476 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001477 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001478 present_times =
1479 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1480 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001481 default:
1482 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1483 next->sType);
1484 break;
1485 }
1486 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1487 }
1488 ALOGV_IF(
1489 present_regions &&
1490 present_regions->swapchainCount != present_info->swapchainCount,
1491 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001492 ALOGV_IF(present_times &&
1493 present_times->swapchainCount != present_info->swapchainCount,
1494 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1495 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001496 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001497 (present_regions) ? present_regions->pRegions : nullptr;
1498 const VkPresentTimeGOOGLE* times =
1499 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001500 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001501 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001502 uint32_t nrects = 0;
1503
Jesse Halld7b994a2015-09-07 14:17:37 -07001504 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1505 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001506 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001507 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001508 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001509 const VkPresentRegionKHR* region =
1510 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001511 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001512 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001513 VkResult result;
1514 int err;
1515
Jesse Halld7b994a2015-09-07 14:17:37 -07001516 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001517 result = dispatch.QueueSignalReleaseImageANDROID(
1518 queue, present_info->waitSemaphoreCount,
1519 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001520 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001521 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001522 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001523 }
1524
Jesse Halldc225072016-05-30 22:40:14 -07001525 if (swapchain.surface.swapchain_handle ==
1526 present_info->pSwapchains[sc]) {
1527 ANativeWindow* window = swapchain.surface.window.get();
1528 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001529 if (region) {
1530 // Process the incremental-present hint for this swapchain:
1531 uint32_t rcount = region->rectangleCount;
1532 if (rcount > nrects) {
1533 android_native_rect_t* new_rects =
1534 static_cast<android_native_rect_t*>(
1535 allocator->pfnReallocation(
1536 allocator->pUserData, rects,
1537 sizeof(android_native_rect_t) * rcount,
1538 alignof(android_native_rect_t),
1539 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1540 if (new_rects) {
1541 rects = new_rects;
1542 nrects = rcount;
1543 } else {
1544 rcount = 0; // Ignore the hint for this swapchain
1545 }
1546 }
1547 for (uint32_t r = 0; r < rcount; ++r) {
1548 if (region->pRectangles[r].layer > 0) {
1549 ALOGV(
1550 "vkQueuePresentKHR ignoring invalid layer "
1551 "(%u); using layer 0 instead",
1552 region->pRectangles[r].layer);
1553 }
1554 int x = region->pRectangles[r].offset.x;
1555 int y = region->pRectangles[r].offset.y;
1556 int width = static_cast<int>(
1557 region->pRectangles[r].extent.width);
1558 int height = static_cast<int>(
1559 region->pRectangles[r].extent.height);
1560 android_native_rect_t* cur_rect = &rects[r];
1561 cur_rect->left = x;
1562 cur_rect->top = y + height;
1563 cur_rect->right = x + width;
1564 cur_rect->bottom = y;
1565 }
1566 native_window_set_surface_damage(window, rects, rcount);
1567 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001568 if (time) {
1569 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001570 ALOGV(
1571 "Calling "
1572 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001573 native_window_enable_frame_timestamps(window, true);
1574 swapchain.frame_timestamps_enabled = true;
1575 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001576
1577 // Record the nativeFrameId so it can be later correlated to
1578 // this present.
1579 uint64_t nativeFrameId = 0;
1580 err = native_window_get_next_frame_id(
1581 window, &nativeFrameId);
1582 if (err != android::NO_ERROR) {
1583 ALOGE("Failed to get next native frame ID.");
1584 }
1585
1586 // Add a new timing record with the user's presentID and
1587 // the nativeFrameId.
1588 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1589 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001590 swapchain.timing.removeAt(0);
1591 }
1592 if (time->desiredPresentTime) {
1593 // Set the desiredPresentTime:
1594 ALOGV(
1595 "Calling "
1596 "native_window_set_buffers_timestamp(%" PRId64 ")",
1597 time->desiredPresentTime);
1598 native_window_set_buffers_timestamp(
1599 window,
1600 static_cast<int64_t>(time->desiredPresentTime));
1601 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001602 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001603
Jesse Halldc225072016-05-30 22:40:14 -07001604 err = window->queueBuffer(window, img.buffer.get(), fence);
1605 // queueBuffer always closes fence, even on error
1606 if (err != 0) {
1607 // TODO(jessehall): What now? We should probably cancel the
1608 // buffer, I guess?
1609 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1610 swapchain_result = WorstPresentResult(
1611 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1612 }
1613 if (img.dequeue_fence >= 0) {
1614 close(img.dequeue_fence);
1615 img.dequeue_fence = -1;
1616 }
1617 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001618
1619 // If the swapchain is in shared mode, immediately dequeue the
1620 // buffer so it can be presented again without an intervening
1621 // call to AcquireNextImageKHR. We expect to get the same buffer
1622 // back from every call to dequeueBuffer in this mode.
1623 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1624 ANativeWindowBuffer* buffer;
1625 int fence_fd;
1626 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1627 if (err != 0) {
1628 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1629 swapchain_result = WorstPresentResult(swapchain_result,
1630 VK_ERROR_SURFACE_LOST_KHR);
1631 }
1632 else if (img.buffer != buffer) {
1633 ALOGE("got wrong image back for shared swapchain");
1634 swapchain_result = WorstPresentResult(swapchain_result,
1635 VK_ERROR_SURFACE_LOST_KHR);
1636 }
1637 else {
1638 img.dequeue_fence = fence_fd;
1639 img.dequeued = true;
1640 }
1641 }
Jesse Halldc225072016-05-30 22:40:14 -07001642 }
1643 if (swapchain_result != VK_SUCCESS) {
1644 ReleaseSwapchainImage(device, window, fence, img);
1645 OrphanSwapchain(device, &swapchain);
1646 }
1647 } else {
1648 ReleaseSwapchainImage(device, nullptr, fence, img);
1649 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001650 }
1651
Jesse Halla9e57032015-11-30 01:03:10 -08001652 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001653 present_info->pResults[sc] = swapchain_result;
1654
1655 if (swapchain_result != final_result)
1656 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001657 }
Ian Elliottcb351132016-12-13 10:30:40 -07001658 if (rects) {
1659 allocator->pfnFree(allocator->pUserData, rects);
1660 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001661
1662 return final_result;
1663}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001664
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001665VKAPI_ATTR
1666VkResult GetRefreshCycleDurationGOOGLE(
1667 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001668 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001669 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Ian Elliott62c48c92017-01-20 13:13:20 -07001670 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001671 VkResult result = VK_SUCCESS;
1672
Brian Andersondc96fdf2017-03-20 16:54:25 -07001673 pDisplayTimingProperties->refreshDuration =
1674 static_cast<uint64_t>(swapchain.refresh_duration);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001675
1676 return result;
1677}
1678
1679VKAPI_ATTR
1680VkResult GetPastPresentationTimingGOOGLE(
1681 VkDevice,
1682 VkSwapchainKHR swapchain_handle,
1683 uint32_t* count,
1684 VkPastPresentationTimingGOOGLE* timings) {
1685 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1686 ANativeWindow* window = swapchain.surface.window.get();
1687 VkResult result = VK_SUCCESS;
1688
1689 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001690 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001691 native_window_enable_frame_timestamps(window, true);
1692 swapchain.frame_timestamps_enabled = true;
1693 }
1694
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001695 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001696 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1697 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001698 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001699 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001700 }
1701
1702 return result;
1703}
1704
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001705VKAPI_ATTR
1706VkResult GetSwapchainStatusKHR(
1707 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001708 VkSwapchainKHR swapchain_handle) {
1709 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001710 VkResult result = VK_SUCCESS;
1711
Chris Forbes4e18ba82017-01-20 12:50:17 +13001712 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1713 return VK_ERROR_OUT_OF_DATE_KHR;
1714 }
1715
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001716 // TODO(chrisforbes): Implement this function properly
1717
1718 return result;
1719}
1720
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001721VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001722 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001723 uint32_t swapchainCount,
1724 const VkSwapchainKHR* pSwapchains,
1725 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001726
1727 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1728 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1729 if (!swapchain)
1730 continue;
1731
1732 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1733
1734 ANativeWindow* window = swapchain->surface.window.get();
1735
1736 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1737 const android_smpte2086_metadata smpteMetdata = {
1738 {vulkanMetadata.displayPrimaryRed.x,
1739 vulkanMetadata.displayPrimaryRed.y},
1740 {vulkanMetadata.displayPrimaryGreen.x,
1741 vulkanMetadata.displayPrimaryGreen.y},
1742 {vulkanMetadata.displayPrimaryBlue.x,
1743 vulkanMetadata.displayPrimaryBlue.y},
1744 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1745 vulkanMetadata.maxLuminance,
1746 vulkanMetadata.minLuminance};
1747 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1748
1749 const android_cta861_3_metadata cta8613Metadata = {
1750 vulkanMetadata.maxContentLightLevel,
1751 vulkanMetadata.maxFrameAverageLightLevel};
1752 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1753 }
1754
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001755 return;
1756}
1757
Chia-I Wu62262232016-03-26 07:06:44 +08001758} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001759} // namespace vulkan