blob: 9266b12d21e2ffad940c8392924dcca81d230bd6 [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;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800188};
189
190VkSurfaceKHR HandleFromSurface(Surface* surface) {
191 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
192}
193
194Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800195 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800196}
197
Ian Elliott8a977262017-01-19 09:05:58 -0700198// Maximum number of TimingInfo structs to keep per swapchain:
199enum { MAX_TIMING_INFOS = 10 };
200// Minimum number of frames to look for in the past (so we don't cause
201// syncronous requests to Surface Flinger):
202enum { MIN_NUM_FRAMES_AGO = 5 };
203
Jesse Hall1356b0d2015-11-23 17:24:58 -0800204struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700205 Swapchain(Surface& surface_,
206 uint32_t num_images_,
207 VkPresentModeKHR present_mode)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700208 : surface(surface_),
209 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700210 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
Chris Forbesf8835642017-03-30 19:31:40 +1300211 frame_timestamps_enabled(false),
212 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
213 present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700214 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700215 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700216 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700217 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700218 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800219
220 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700221 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700222 bool mailbox_mode;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700223 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700224 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300225 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700226
227 struct Image {
228 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
229 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800230 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700231 // The fence is only valid when the buffer is dequeued, and should be
232 // -1 any other time. When valid, we own the fd, and must ensure it is
233 // closed: either by closing it explicitly when queueing the buffer,
234 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
235 int dequeue_fence;
236 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800237 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700238
Brian Anderson1049d1d2016-12-16 17:25:57 -0800239 android::Vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700240};
241
242VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
243 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
244}
245
246Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800247 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700248}
249
Jesse Halldc225072016-05-30 22:40:14 -0700250void ReleaseSwapchainImage(VkDevice device,
251 ANativeWindow* window,
252 int release_fence,
253 Swapchain::Image& image) {
254 ALOG_ASSERT(release_fence == -1 || image.dequeued,
255 "ReleaseSwapchainImage: can't provide a release fence for "
256 "non-dequeued images");
257
258 if (image.dequeued) {
259 if (release_fence >= 0) {
260 // We get here from vkQueuePresentKHR. The application is
261 // responsible for creating an execution dependency chain from
262 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
263 // (release_fence), so we can drop the dequeue_fence here.
264 if (image.dequeue_fence >= 0)
265 close(image.dequeue_fence);
266 } else {
267 // We get here during swapchain destruction, or various serious
268 // error cases e.g. when we can't create the release_fence during
269 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
270 // have already signalled, since the swapchain images are supposed
271 // to be idle before the swapchain is destroyed. In error cases,
272 // there may be rendering in flight to the image, but since we
273 // weren't able to create a release_fence, waiting for the
274 // dequeue_fence is about the best we can do.
275 release_fence = image.dequeue_fence;
276 }
277 image.dequeue_fence = -1;
278
279 if (window) {
280 window->cancelBuffer(window, image.buffer.get(), release_fence);
281 } else {
282 if (release_fence >= 0) {
283 sync_wait(release_fence, -1 /* forever */);
284 close(release_fence);
285 }
286 }
287
288 image.dequeued = false;
289 }
290
291 if (image.image) {
292 GetData(device).driver.DestroyImage(device, image.image, nullptr);
293 image.image = VK_NULL_HANDLE;
294 }
295
296 image.buffer.clear();
297}
298
299void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
300 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
301 return;
Jesse Halldc225072016-05-30 22:40:14 -0700302 for (uint32_t i = 0; i < swapchain->num_images; i++) {
303 if (!swapchain->images[i].dequeued)
304 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
305 }
306 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700307 swapchain->timing.clear();
308}
309
310uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800311 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
312 return 0;
313 }
Ian Elliott8a977262017-01-19 09:05:58 -0700314
Brian Anderson1049d1d2016-12-16 17:25:57 -0800315 uint32_t num_ready = 0;
316 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
317 for (uint32_t i = 0; i < num_timings; i++) {
318 TimingInfo& ti = swapchain.timing.editItemAt(i);
319 if (ti.ready()) {
320 // This TimingInfo is ready to be reported to the user. Add it
321 // to the num_ready.
322 num_ready++;
323 continue;
324 }
325 // This TimingInfo is not yet ready to be reported to the user,
326 // and so we should look for any available timestamps that
327 // might make it ready.
328 int64_t desired_present_time = 0;
329 int64_t render_complete_time = 0;
330 int64_t composition_latch_time = 0;
331 int64_t actual_present_time = 0;
332 // Obtain timestamps:
333 int ret = native_window_get_frame_timestamps(
334 swapchain.surface.window.get(), ti.native_frame_id_,
335 &desired_present_time, &render_complete_time,
336 &composition_latch_time,
337 NULL, //&first_composition_start_time,
338 NULL, //&last_composition_start_time,
339 NULL, //&composition_finish_time,
340 // TODO(ianelliott): Maybe ask if this one is
341 // supported, at startup time (since it may not be
342 // supported):
343 &actual_present_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800344 NULL, //&dequeue_ready_time,
345 NULL /*&reads_done_time*/);
346
347 if (ret != android::NO_ERROR) {
348 continue;
349 }
350
351 // Record the timestamp(s) we received, and then see if this TimingInfo
352 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700353 ti.timestamp_desired_present_time_ = desired_present_time;
354 ti.timestamp_actual_present_time_ = actual_present_time;
355 ti.timestamp_render_complete_time_ = render_complete_time;
356 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800357
358 if (ti.ready()) {
359 // The TimingInfo has received enough timestamps, and should now
360 // use those timestamps to calculate the info that should be
361 // reported to the user:
362 ti.calculate(swapchain.refresh_duration);
363 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700364 }
365 }
366 return num_ready;
367}
368
369// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
370void copy_ready_timings(Swapchain& swapchain,
371 uint32_t* count,
372 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800373 if (swapchain.timing.empty()) {
374 *count = 0;
375 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700376 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800377
378 size_t last_ready = swapchain.timing.size() - 1;
379 while (!swapchain.timing[last_ready].ready()) {
380 if (last_ready == 0) {
381 *count = 0;
382 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700383 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800384 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700385 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800386
387 uint32_t num_copied = 0;
388 size_t num_to_remove = 0;
389 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
390 const TimingInfo& ti = swapchain.timing[i];
391 if (ti.ready()) {
392 ti.get_values(&timings[num_copied]);
393 num_copied++;
394 }
395 num_to_remove++;
396 }
397
398 // Discard old frames that aren't ready if newer frames are ready.
399 // We don't expect to get the timing info for those old frames.
400 swapchain.timing.removeItemsAt(0, num_to_remove);
401
Ian Elliott8a977262017-01-19 09:05:58 -0700402 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700403}
404
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700405android_pixel_format GetNativePixelFormat(VkFormat format) {
406 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
407 switch (format) {
408 case VK_FORMAT_R8G8B8A8_UNORM:
409 case VK_FORMAT_R8G8B8A8_SRGB:
410 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
411 break;
412 case VK_FORMAT_R5G6B5_UNORM_PACK16:
413 native_format = HAL_PIXEL_FORMAT_RGB_565;
414 break;
415 case VK_FORMAT_R16G16B16A16_SFLOAT:
416 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
417 break;
418 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
419 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
420 break;
421 default:
422 ALOGV("unsupported swapchain format %d", format);
423 break;
424 }
425 return native_format;
426}
427
428android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
429 switch (colorspace) {
430 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
431 return HAL_DATASPACE_V0_SRGB;
432 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
433 return HAL_DATASPACE_DISPLAY_P3;
434 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
435 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600436 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
437 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700438 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
439 return HAL_DATASPACE_DCI_P3_LINEAR;
440 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
441 return HAL_DATASPACE_DCI_P3;
442 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
443 return HAL_DATASPACE_V0_SRGB_LINEAR;
444 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
445 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600446 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
447 return HAL_DATASPACE_BT2020_LINEAR;
448 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700449 return static_cast<android_dataspace>(
450 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
451 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600452 case VK_COLOR_SPACE_DOLBYVISION_EXT:
453 return static_cast<android_dataspace>(
454 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
455 HAL_DATASPACE_RANGE_FULL);
456 case VK_COLOR_SPACE_HDR10_HLG_EXT:
457 return static_cast<android_dataspace>(
458 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
459 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700460 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
461 return static_cast<android_dataspace>(
462 HAL_DATASPACE_STANDARD_ADOBE_RGB |
463 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
464 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
465 return HAL_DATASPACE_ADOBE_RGB;
466
467 // Pass through is intended to allow app to provide data that is passed
468 // to the display system without modification.
469 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
470 return HAL_DATASPACE_ARBITRARY;
471
472 default:
473 // This indicates that we don't know about the
474 // dataspace specified and we should indicate that
475 // it's unsupported
476 return HAL_DATASPACE_UNKNOWN;
477 }
478}
479
Jesse Halld7b994a2015-09-07 14:17:37 -0700480} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700481
Jesse Halle1b12782015-11-30 11:27:32 -0800482VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800483VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800484 VkInstance instance,
485 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
486 const VkAllocationCallbacks* allocator,
487 VkSurfaceKHR* out_surface) {
Jesse Hall1f91d392015-12-11 16:28:44 -0800488 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800489 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800490 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
491 alignof(Surface),
492 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800493 if (!mem)
494 return VK_ERROR_OUT_OF_HOST_MEMORY;
495 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700496
Chia-I Wue8e689f2016-04-18 08:21:31 +0800497 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700498 surface->swapchain_handle = VK_NULL_HANDLE;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700499
Jesse Hall1356b0d2015-11-23 17:24:58 -0800500 // TODO(jessehall): Create and use NATIVE_WINDOW_API_VULKAN.
501 int err =
502 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
503 if (err != 0) {
504 // TODO(jessehall): Improve error reporting. Can we enumerate possible
505 // errors and translate them to valid Vulkan result codes?
506 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
507 err);
508 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800509 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700510 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800511 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700512
Jesse Hall1356b0d2015-11-23 17:24:58 -0800513 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700514 return VK_SUCCESS;
515}
516
Jesse Halle1b12782015-11-30 11:27:32 -0800517VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800518void DestroySurfaceKHR(VkInstance instance,
519 VkSurfaceKHR surface_handle,
520 const VkAllocationCallbacks* allocator) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800521 Surface* surface = SurfaceFromHandle(surface_handle);
522 if (!surface)
523 return;
524 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700525 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700526 "destroyed VkSurfaceKHR 0x%" PRIx64
527 " has active VkSwapchainKHR 0x%" PRIx64,
528 reinterpret_cast<uint64_t>(surface_handle),
529 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800530 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800531 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800532 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800533 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800534}
535
Jesse Halle1b12782015-11-30 11:27:32 -0800536VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800537VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
538 uint32_t /*queue_family*/,
539 VkSurfaceKHR /*surface*/,
540 VkBool32* supported) {
Jesse Hall0e74f002015-11-30 11:37:59 -0800541 *supported = VK_TRUE;
Jesse Halla6429252015-11-29 18:59:42 -0800542 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800543}
544
Jesse Halle1b12782015-11-30 11:27:32 -0800545VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800546VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800547 VkPhysicalDevice /*pdev*/,
548 VkSurfaceKHR surface,
549 VkSurfaceCapabilitiesKHR* capabilities) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700550 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800551 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700552
553 int width, height;
554 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
555 if (err != 0) {
556 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
557 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700558 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700559 }
560 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
561 if (err != 0) {
562 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
563 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700564 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700565 }
566
Jesse Hall55bc0972016-02-23 16:43:29 -0800567 int transform_hint;
568 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
569 if (err != 0) {
570 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
571 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700572 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800573 }
574
Jesse Halld7b994a2015-09-07 14:17:37 -0700575 // TODO(jessehall): Figure out what the min/max values should be.
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800576 int max_buffer_count;
577 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
578 if (err != 0) {
579 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
580 strerror(-err), err);
581 return VK_ERROR_SURFACE_LOST_KHR;
582 }
Jesse Hallb00daad2015-11-29 19:46:20 -0800583 capabilities->minImageCount = 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800584 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700585
Jesse Hallfe2662d2016-02-09 13:26:59 -0800586 capabilities->currentExtent =
587 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
588
Jesse Halld7b994a2015-09-07 14:17:37 -0700589 // TODO(jessehall): Figure out what the max extent should be. Maximum
590 // texture dimension maybe?
Jesse Hallb00daad2015-11-29 19:46:20 -0800591 capabilities->minImageExtent = VkExtent2D{1, 1};
592 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700593
Jesse Hallfe2662d2016-02-09 13:26:59 -0800594 capabilities->maxImageArrayLayers = 1;
595
Jesse Hall55bc0972016-02-23 16:43:29 -0800596 capabilities->supportedTransforms = kSupportedTransforms;
597 capabilities->currentTransform =
598 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700599
Jesse Hallfe2662d2016-02-09 13:26:59 -0800600 // On Android, window composition is a WindowManager property, not something
601 // associated with the bufferqueue. It can't be changed from here.
602 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700603
604 // TODO(jessehall): I think these are right, but haven't thought hard about
605 // it. Do we need to query the driver for support of any of these?
606 // Currently not included:
Jesse Halld7b994a2015-09-07 14:17:37 -0700607 // - VK_IMAGE_USAGE_DEPTH_STENCIL_BIT: definitely not
608 // - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT: definitely not
Jesse Hallb00daad2015-11-29 19:46:20 -0800609 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800610 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
611 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
612 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700613 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
614
Jesse Hallb1352bc2015-09-04 16:12:33 -0700615 return VK_SUCCESS;
616}
617
Jesse Halle1b12782015-11-30 11:27:32 -0800618VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700619VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
620 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800621 uint32_t* count,
622 VkSurfaceFormatKHR* formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700623 const InstanceData& instance_data = GetData(pdev);
624
Jesse Hall1356b0d2015-11-23 17:24:58 -0800625 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
626 // a new gralloc method to query whether a (format, usage) pair is
627 // supported, and check that for each gralloc format that corresponds to a
628 // Vulkan format. Shorter term, just add a few more formats to the ones
629 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700630
631 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700632 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
633 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
634 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700635 };
636 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700637 uint32_t total_num_formats = kNumFormats;
638
639 bool wide_color_support = false;
640 Surface& surface = *SurfaceFromHandle(surface_handle);
641 int err = native_window_get_wide_color_support(surface.window.get(),
642 &wide_color_support);
643 if (err) {
644 // Not allowed to return a more sensible error code, so do this
645 return VK_ERROR_OUT_OF_HOST_MEMORY;
646 }
647 ALOGV("wide_color_support is: %d", wide_color_support);
648 wide_color_support =
649 wide_color_support &&
650 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
651
652 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600653 {VK_FORMAT_R8G8B8A8_UNORM,
654 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
655 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700656 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
657 };
658 const uint32_t kNumWideColorFormats =
659 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
660 if (wide_color_support) {
661 total_num_formats += kNumWideColorFormats;
662 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700663
664 VkResult result = VK_SUCCESS;
665 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700666 uint32_t out_count = 0;
667 uint32_t transfer_count = 0;
668 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700669 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700670 transfer_count = std::min(*count, kNumFormats);
671 std::copy(kFormats, kFormats + transfer_count, formats);
672 out_count += transfer_count;
673 if (wide_color_support) {
674 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
675 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
676 formats + out_count);
677 out_count += transfer_count;
678 }
679 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700680 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700681 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700682 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700683 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700684}
685
Jesse Halle1b12782015-11-30 11:27:32 -0800686VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300687VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
688 VkPhysicalDevice physicalDevice,
689 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
690 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
691 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
692 physicalDevice, pSurfaceInfo->surface,
693 &pSurfaceCapabilities->surfaceCapabilities);
694
Chris Forbes06bc0092017-03-16 16:46:05 +1300695 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
696 while (caps->pNext) {
697 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
698
699 switch (caps->sType) {
700 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
701 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
702 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
703 caps);
704 // Claim same set of usage flags are supported for
705 // shared present modes as for other modes.
706 shared_caps->sharedPresentSupportedUsageFlags =
707 pSurfaceCapabilities->surfaceCapabilities
708 .supportedUsageFlags;
709 } break;
710
711 default:
712 // Ignore all other extension structs
713 break;
714 }
715 }
716
Chris Forbes2452cf72017-03-16 16:30:17 +1300717 return result;
718}
719
720VKAPI_ATTR
721VkResult GetPhysicalDeviceSurfaceFormats2KHR(
722 VkPhysicalDevice physicalDevice,
723 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
724 uint32_t* pSurfaceFormatCount,
725 VkSurfaceFormat2KHR* pSurfaceFormats) {
726 if (!pSurfaceFormats) {
727 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
728 pSurfaceInfo->surface,
729 pSurfaceFormatCount, nullptr);
730 } else {
731 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
732 // after the call.
733 android::Vector<VkSurfaceFormatKHR> surface_formats;
734 surface_formats.resize(*pSurfaceFormatCount);
735 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
736 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
737 &surface_formats.editItemAt(0));
738
739 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
740 // marshal results individually due to stride difference.
741 // completely ignore any chained extension structs.
742 uint32_t formats_to_marshal = *pSurfaceFormatCount;
743 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
744 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
745 }
746 }
747
748 return result;
749 }
750}
751
752VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300753VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Chia-I Wu62262232016-03-26 07:06:44 +0800754 VkSurfaceKHR /*surface*/,
755 uint32_t* count,
756 VkPresentModeKHR* modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300757 android::Vector<VkPresentModeKHR> present_modes;
758 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
759 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
760
761 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
762 if (QueryPresentationProperties(pdev, &present_properties)) {
763 if (present_properties.sharedImage) {
764 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
765 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
766 }
767 }
768
769 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700770
771 VkResult result = VK_SUCCESS;
772 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300773 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700774 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300775 *count = std::min(*count, num_modes);
776 std::copy(present_modes.begin(), present_modes.begin() + int(*count), modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700777 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300778 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700779 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700780 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700781}
782
Jesse Halle1b12782015-11-30 11:27:32 -0800783VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400784VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600785 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400786 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400787 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
788 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
789 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
790 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
791 pDeviceGroupPresentCapabilities->sType);
792
793 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
794 sizeof(pDeviceGroupPresentCapabilities->presentMask));
795
796 // assume device group of size 1
797 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
798 pDeviceGroupPresentCapabilities->modes =
799 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
800
801 return VK_SUCCESS;
802}
803
804VKAPI_ATTR
805VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600806 VkDevice,
807 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400808 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400809 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
810 return VK_SUCCESS;
811}
812
813VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600814VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400815 VkSurfaceKHR surface,
816 uint32_t* pRectCount,
817 VkRect2D* pRects) {
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400818 if (!pRects) {
819 *pRectCount = 1;
820 } else {
821 uint32_t count = std::min(*pRectCount, 1u);
822 bool incomplete = *pRectCount < 1;
823
824 *pRectCount = count;
825
826 if (incomplete) {
827 return VK_INCOMPLETE;
828 }
829
830 int err;
831 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
832
833 int width = 0, height = 0;
834 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
835 if (err != 0) {
836 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
837 strerror(-err), err);
838 }
839 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
840 if (err != 0) {
841 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
842 strerror(-err), err);
843 }
844
845 // TODO: Return something better than "whole window"
846 pRects[0].offset.x = 0;
847 pRects[0].offset.y = 0;
848 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
849 static_cast<uint32_t>(height)};
850 }
851 return VK_SUCCESS;
852}
853
854VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800855VkResult CreateSwapchainKHR(VkDevice device,
856 const VkSwapchainCreateInfoKHR* create_info,
857 const VkAllocationCallbacks* allocator,
858 VkSwapchainKHR* swapchain_handle) {
Jesse Halld7b994a2015-09-07 14:17:37 -0700859 int err;
860 VkResult result = VK_SUCCESS;
861
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700862 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
863 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
864 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
865 " oldSwapchain=0x%" PRIx64,
866 reinterpret_cast<uint64_t>(create_info->surface),
867 create_info->minImageCount, create_info->imageFormat,
868 create_info->imageColorSpace, create_info->imageExtent.width,
869 create_info->imageExtent.height, create_info->imageUsage,
870 create_info->preTransform, create_info->presentMode,
871 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
872
Jesse Hall1f91d392015-12-11 16:28:44 -0800873 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800874 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800875
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700876 android_pixel_format native_pixel_format =
877 GetNativePixelFormat(create_info->imageFormat);
878 android_dataspace native_dataspace =
879 GetNativeDataspace(create_info->imageColorSpace);
880 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
881 ALOGE(
882 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
883 "failed: Unsupported color space",
884 create_info->imageColorSpace);
885 return VK_ERROR_INITIALIZATION_FAILED;
886 }
887
Jesse Hall42a9eec2016-06-03 12:39:49 -0700888 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -0700889 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -0800890 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700891 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -0700892 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -0800893 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700894 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +1300895 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +1300896 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
897 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -0700898 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -0800899 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -0700900
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700901 Surface& surface = *SurfaceFromHandle(create_info->surface);
902
Jesse Halldc225072016-05-30 22:40:14 -0700903 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -0700904 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -0700905 " because it already has active swapchain 0x%" PRIx64
906 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
907 reinterpret_cast<uint64_t>(create_info->surface),
908 reinterpret_cast<uint64_t>(surface.swapchain_handle),
909 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
910 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
911 }
912 if (create_info->oldSwapchain != VK_NULL_HANDLE)
913 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
914
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700915 // -- Reset the native window --
916 // The native window might have been used previously, and had its properties
917 // changed from defaults. That will affect the answer we get for queries
918 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
919 // attempt such queries.
920
Jesse Halldc225072016-05-30 22:40:14 -0700921 // The native window only allows dequeueing all buffers before any have
922 // been queued, since after that point at least one is assumed to be in
923 // non-FREE state at any given time. Disconnecting and re-connecting
924 // orphans the previous buffers, getting us back to the state where we can
925 // dequeue all buffers.
926 err = native_window_api_disconnect(surface.window.get(),
927 NATIVE_WINDOW_API_EGL);
928 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
929 strerror(-err), err);
930 err =
931 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
932 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
933 strerror(-err), err);
934
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700935 err = native_window_set_buffer_count(surface.window.get(), 0);
936 if (err != 0) {
937 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
938 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700939 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700940 }
941
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +0530942 int swap_interval =
943 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
944 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700945 if (err != 0) {
946 // TODO(jessehall): Improve error reporting. Can we enumerate possible
947 // errors and translate them to valid Vulkan result codes?
948 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
949 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700950 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -0700951 }
952
Chris Forbesb8042d22017-01-18 18:07:05 +1300953 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
954 if (err != 0) {
955 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
956 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700957 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300958 }
959
960 err = native_window_set_auto_refresh(surface.window.get(), false);
961 if (err != 0) {
962 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
963 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700964 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +1300965 }
966
Jesse Halld7b994a2015-09-07 14:17:37 -0700967 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -0700968
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800969 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -0800970
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700971 err = native_window_set_buffers_format(surface.window.get(),
972 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -0800973 if (err != 0) {
974 // TODO(jessehall): Improve error reporting. Can we enumerate possible
975 // errors and translate them to valid Vulkan result codes?
976 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700977 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700978 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -0800979 }
980 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700981 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -0800982 if (err != 0) {
983 // TODO(jessehall): Improve error reporting. Can we enumerate possible
984 // errors and translate them to valid Vulkan result codes?
985 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700986 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700987 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -0800988 }
989
Jesse Hall3dd678a2016-01-08 21:52:01 -0800990 err = native_window_set_buffers_dimensions(
991 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
992 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -0700993 if (err != 0) {
994 // TODO(jessehall): Improve error reporting. Can we enumerate possible
995 // errors and translate them to valid Vulkan result codes?
996 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
997 create_info->imageExtent.width, create_info->imageExtent.height,
998 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700999 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001000 }
1001
Jesse Hall178b6962016-02-24 15:39:50 -08001002 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1003 // applied during rendering. native_window_set_transform() expects the
1004 // inverse: the transform the app is requesting that the compositor perform
1005 // during composition. With native windows, pre-transform works by rendering
1006 // with the same transform the compositor is applying (as in Vulkan), but
1007 // then requesting the inverse transform, so that when the compositor does
1008 // it's job the two transforms cancel each other out and the compositor ends
1009 // up applying an identity transform to the app's buffer.
1010 err = native_window_set_buffers_transform(
1011 surface.window.get(),
1012 InvertTransformToNative(create_info->preTransform));
1013 if (err != 0) {
1014 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1015 // errors and translate them to valid Vulkan result codes?
1016 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1017 InvertTransformToNative(create_info->preTransform),
1018 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001019 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001020 }
1021
Jesse Hallf64ca122015-11-03 16:11:10 -08001022 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001023 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001024 if (err != 0) {
1025 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1026 // errors and translate them to valid Vulkan result codes?
1027 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1028 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001029 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001030 }
1031
Chris Forbes97ef4612017-03-30 19:37:50 +13001032 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1033 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1034 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1035 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1036 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1037 if (err != 0) {
1038 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1039 return VK_ERROR_SURFACE_LOST_KHR;
1040 }
1041 }
1042
1043 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1044 err = native_window_set_auto_refresh(surface.window.get(), true);
1045 if (err != 0) {
1046 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1047 return VK_ERROR_SURFACE_LOST_KHR;
1048 }
1049 }
1050
Jesse Halle6080bf2016-02-28 20:58:50 -08001051 int query_value;
1052 err = surface.window->query(surface.window.get(),
1053 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1054 &query_value);
1055 if (err != 0 || query_value < 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001056 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1057 // errors and translate them to valid Vulkan result codes?
Jesse Halle6080bf2016-02-28 20:58:50 -08001058 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1059 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001060 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001061 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001062 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001063 uint32_t num_images =
1064 (create_info->minImageCount - 1) + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001065
1066 // Lower layer insists that we have at least two buffers. This is wasteful
1067 // and we'd like to relax it in the shared case, but not all the pieces are
1068 // in place for that to work yet. Note we only lie to the lower layer-- we
1069 // don't want to give the app back a swapchain with extra images (which they
1070 // can't actually use!).
1071 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001072 if (err != 0) {
1073 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1074 // errors and translate them to valid Vulkan result codes?
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001075 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1076 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001077 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001078 }
1079
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001080 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001081 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001082 uint64_t consumer_usage, producer_usage;
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001083 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1084 device, create_info->imageFormat, create_info->imageUsage,
1085 swapchain_image_usage, &consumer_usage, &producer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001086 if (result != VK_SUCCESS) {
1087 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001088 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001089 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001090 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001091 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001092 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Jesse Hall1f91d392015-12-11 16:28:44 -08001093 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001094 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001095 &legacy_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001096 if (result != VK_SUCCESS) {
1097 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001098 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001099 }
Jesse Hall70f93352015-11-04 09:41:31 -08001100 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001101 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1102
1103 bool createProtectedSwapchain = false;
1104 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1105 createProtectedSwapchain = true;
1106 native_usage |= BufferUsage::PROTECTED;
1107 }
1108 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001109 if (err != 0) {
1110 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1111 // errors and translate them to valid Vulkan result codes?
1112 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001113 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001114 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001115
1116 // -- Allocate our Swapchain object --
1117 // After this point, we must deallocate the swapchain on error.
1118
Jesse Hall1f91d392015-12-11 16:28:44 -08001119 void* mem = allocator->pfnAllocation(allocator->pUserData,
1120 sizeof(Swapchain), alignof(Swapchain),
1121 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001122 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001123 return VK_ERROR_OUT_OF_HOST_MEMORY;
Ian Elliottffedb652017-02-14 10:58:30 -07001124 Swapchain* swapchain =
1125 new (mem) Swapchain(surface, num_images, create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001126
1127 // -- Dequeue all buffers and create a VkImage for each --
1128 // Any failures during or after this must cancel the dequeued buffers.
1129
Chris Forbesb56287a2017-01-12 14:28:58 +13001130 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1131#pragma clang diagnostic push
1132#pragma clang diagnostic ignored "-Wold-style-cast"
1133 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1134#pragma clang diagnostic pop
1135 .pNext = nullptr,
1136 .usage = swapchain_image_usage,
1137 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001138 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001139#pragma clang diagnostic push
1140#pragma clang diagnostic ignored "-Wold-style-cast"
1141 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1142#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001143 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001144 };
1145 VkImageCreateInfo image_create = {
1146 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1147 .pNext = &image_native_buffer,
1148 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001149 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001150 .extent = {0, 0, 1},
1151 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001152 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001153 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001154 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001155 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001156 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001157 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001158 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001159 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1160 };
1161
Jesse Halld7b994a2015-09-07 14:17:37 -07001162 for (uint32_t i = 0; i < num_images; i++) {
1163 Swapchain::Image& img = swapchain->images[i];
1164
1165 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001166 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1167 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001168 if (err != 0) {
1169 // TODO(jessehall): Improve error reporting. Can we enumerate
1170 // possible errors and translate them to valid Vulkan result codes?
1171 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001172 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001173 break;
1174 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001175 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001176 img.dequeued = true;
1177
1178 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001179 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1180 static_cast<uint32_t>(img.buffer->height),
1181 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001182 image_native_buffer.handle = img.buffer->handle;
1183 image_native_buffer.stride = img.buffer->stride;
1184 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001185 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001186 android_convertGralloc0To1Usage(int(img.buffer->usage),
1187 &image_native_buffer.usage2.producer,
1188 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001189
Jesse Hall03b6fe12015-11-24 12:44:21 -08001190 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001191 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Jesse Halld7b994a2015-09-07 14:17:37 -07001192 if (result != VK_SUCCESS) {
1193 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1194 break;
1195 }
1196 }
1197
1198 // -- Cancel all buffers, returning them to the queue --
1199 // If an error occurred before, also destroy the VkImage and release the
1200 // buffer reference. Otherwise, we retain a strong reference to the buffer.
1201 //
1202 // TODO(jessehall): The error path here is the same as DestroySwapchain,
1203 // but not the non-error path. Should refactor/unify.
Chris Forbese0ced032017-03-30 19:44:15 +13001204 if (!swapchain->shared) {
1205 for (uint32_t i = 0; i < num_images; i++) {
1206 Swapchain::Image& img = swapchain->images[i];
1207 if (img.dequeued) {
1208 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1209 img.dequeue_fence);
1210 img.dequeue_fence = -1;
1211 img.dequeued = false;
1212 }
1213 if (result != VK_SUCCESS) {
1214 if (img.image)
1215 dispatch.DestroyImage(device, img.image, nullptr);
1216 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001217 }
1218 }
1219
1220 if (result != VK_SUCCESS) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001221 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001222 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Halld7b994a2015-09-07 14:17:37 -07001223 return result;
1224 }
1225
Jesse Halldc225072016-05-30 22:40:14 -07001226 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1227 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001228 return VK_SUCCESS;
1229}
1230
Jesse Halle1b12782015-11-30 11:27:32 -08001231VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001232void DestroySwapchainKHR(VkDevice device,
1233 VkSwapchainKHR swapchain_handle,
1234 const VkAllocationCallbacks* allocator) {
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001235 const auto& dispatch = GetData(device).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001236 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
Daniel Kochd78c2e82016-12-13 18:45:13 -05001237 if (!swapchain)
1238 return;
Jesse Hall42a9eec2016-06-03 12:39:49 -07001239 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
1240 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
Jesse Halld7b994a2015-09-07 14:17:37 -07001241
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001242 if (swapchain->frame_timestamps_enabled) {
1243 native_window_enable_frame_timestamps(window, false);
1244 }
Jesse Halldc225072016-05-30 22:40:14 -07001245 for (uint32_t i = 0; i < swapchain->num_images; i++)
1246 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001247 if (active)
Jesse Halldc225072016-05-30 22:40:14 -07001248 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Jesse Hall1f91d392015-12-11 16:28:44 -08001249 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001250 allocator = &GetData(device).allocator;
Jesse Halld7b994a2015-09-07 14:17:37 -07001251 swapchain->~Swapchain();
Jesse Hall1f91d392015-12-11 16:28:44 -08001252 allocator->pfnFree(allocator->pUserData, swapchain);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001253}
1254
Jesse Halle1b12782015-11-30 11:27:32 -08001255VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001256VkResult GetSwapchainImagesKHR(VkDevice,
1257 VkSwapchainKHR swapchain_handle,
1258 uint32_t* count,
1259 VkImage* images) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001260 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001261 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1262 "getting images for non-active swapchain 0x%" PRIx64
1263 "; only dequeued image handles are valid",
1264 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001265 VkResult result = VK_SUCCESS;
1266 if (images) {
1267 uint32_t n = swapchain.num_images;
1268 if (*count < swapchain.num_images) {
1269 n = *count;
1270 result = VK_INCOMPLETE;
1271 }
1272 for (uint32_t i = 0; i < n; i++)
1273 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001274 *count = n;
1275 } else {
1276 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001277 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001278 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001279}
1280
Jesse Halle1b12782015-11-30 11:27:32 -08001281VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001282VkResult AcquireNextImageKHR(VkDevice device,
1283 VkSwapchainKHR swapchain_handle,
1284 uint64_t timeout,
1285 VkSemaphore semaphore,
1286 VkFence vk_fence,
1287 uint32_t* image_index) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001288 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001289 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001290 VkResult result;
1291 int err;
1292
Jesse Halldc225072016-05-30 22:40:14 -07001293 if (swapchain.surface.swapchain_handle != swapchain_handle)
1294 return VK_ERROR_OUT_OF_DATE_KHR;
1295
Jesse Halld7b994a2015-09-07 14:17:37 -07001296 ALOGW_IF(
1297 timeout != UINT64_MAX,
1298 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1299
Chris Forbesc88409c2017-03-30 19:47:37 +13001300 if (swapchain.shared) {
1301 // In shared mode, we keep the buffer dequeued all the time, so we don't
1302 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1303 // the semaphore and fence passed to us will be signalled.
1304 *image_index = 0;
1305 result = GetData(device).driver.AcquireImageANDROID(
1306 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1307 return result;
1308 }
1309
Jesse Halld7b994a2015-09-07 14:17:37 -07001310 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001311 int fence_fd;
1312 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001313 if (err != 0) {
1314 // TODO(jessehall): Improve error reporting. Can we enumerate possible
1315 // errors and translate them to valid Vulkan result codes?
1316 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001317 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001318 }
1319
1320 uint32_t idx;
1321 for (idx = 0; idx < swapchain.num_images; idx++) {
1322 if (swapchain.images[idx].buffer.get() == buffer) {
1323 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001324 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001325 break;
1326 }
1327 }
1328 if (idx == swapchain.num_images) {
1329 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001330 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001331 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001332 }
1333
1334 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001335 if (fence_fd != -1) {
1336 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001337 if (fence_clone == -1) {
1338 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1339 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001340 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001341 }
1342 }
1343
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001344 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001345 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001346 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001347 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1348 // even if the call fails. We could close it ourselves on failure, but
1349 // that would create a race condition if the driver closes it on a
1350 // failure path: some other thread might create an fd with the same
1351 // number between the time the driver closes it and the time we close
1352 // it. We must assume one of: the driver *always* closes it even on
1353 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001354 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001355 swapchain.images[idx].dequeued = false;
1356 swapchain.images[idx].dequeue_fence = -1;
1357 return result;
1358 }
1359
1360 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001361 return VK_SUCCESS;
1362}
1363
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001364VKAPI_ATTR
1365VkResult AcquireNextImage2KHR(VkDevice device,
1366 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1367 uint32_t* pImageIndex) {
1368 // TODO: this should actually be the other way around and this function
1369 // should handle any additional structures that get passed in
1370 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1371 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1372 pAcquireInfo->fence, pImageIndex);
1373}
1374
Jesse Halldc225072016-05-30 22:40:14 -07001375static VkResult WorstPresentResult(VkResult a, VkResult b) {
1376 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1377 // (in spec version 1.0.14).
1378 static const VkResult kWorstToBest[] = {
1379 VK_ERROR_DEVICE_LOST,
1380 VK_ERROR_SURFACE_LOST_KHR,
1381 VK_ERROR_OUT_OF_DATE_KHR,
1382 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1383 VK_ERROR_OUT_OF_HOST_MEMORY,
1384 VK_SUBOPTIMAL_KHR,
1385 };
1386 for (auto result : kWorstToBest) {
1387 if (a == result || b == result)
1388 return result;
1389 }
1390 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1391 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1392 return a != VK_SUCCESS ? a : b;
1393}
1394
Jesse Halle1b12782015-11-30 11:27:32 -08001395VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001396VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001397 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1398 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1399 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001400
Jesse Halldc225072016-05-30 22:40:14 -07001401 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001402 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001403 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001404
Ian Elliottcb351132016-12-13 10:30:40 -07001405 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001406 const VkPresentRegionsKHR* present_regions = nullptr;
1407 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001408 const VkPresentRegionsKHR* next =
1409 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1410 while (next) {
1411 switch (next->sType) {
1412 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1413 present_regions = next;
1414 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001415 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001416 present_times =
1417 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1418 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001419 default:
1420 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1421 next->sType);
1422 break;
1423 }
1424 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1425 }
1426 ALOGV_IF(
1427 present_regions &&
1428 present_regions->swapchainCount != present_info->swapchainCount,
1429 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001430 ALOGV_IF(present_times &&
1431 present_times->swapchainCount != present_info->swapchainCount,
1432 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1433 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001434 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001435 (present_regions) ? present_regions->pRegions : nullptr;
1436 const VkPresentTimeGOOGLE* times =
1437 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001438 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001439 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001440 uint32_t nrects = 0;
1441
Jesse Halld7b994a2015-09-07 14:17:37 -07001442 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1443 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001444 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001445 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001446 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001447 const VkPresentRegionKHR* region =
1448 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001449 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001450 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001451 VkResult result;
1452 int err;
1453
Jesse Halld7b994a2015-09-07 14:17:37 -07001454 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001455 result = dispatch.QueueSignalReleaseImageANDROID(
1456 queue, present_info->waitSemaphoreCount,
1457 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001458 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001459 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001460 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001461 }
1462
Jesse Halldc225072016-05-30 22:40:14 -07001463 if (swapchain.surface.swapchain_handle ==
1464 present_info->pSwapchains[sc]) {
1465 ANativeWindow* window = swapchain.surface.window.get();
1466 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001467 if (region) {
1468 // Process the incremental-present hint for this swapchain:
1469 uint32_t rcount = region->rectangleCount;
1470 if (rcount > nrects) {
1471 android_native_rect_t* new_rects =
1472 static_cast<android_native_rect_t*>(
1473 allocator->pfnReallocation(
1474 allocator->pUserData, rects,
1475 sizeof(android_native_rect_t) * rcount,
1476 alignof(android_native_rect_t),
1477 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1478 if (new_rects) {
1479 rects = new_rects;
1480 nrects = rcount;
1481 } else {
1482 rcount = 0; // Ignore the hint for this swapchain
1483 }
1484 }
1485 for (uint32_t r = 0; r < rcount; ++r) {
1486 if (region->pRectangles[r].layer > 0) {
1487 ALOGV(
1488 "vkQueuePresentKHR ignoring invalid layer "
1489 "(%u); using layer 0 instead",
1490 region->pRectangles[r].layer);
1491 }
1492 int x = region->pRectangles[r].offset.x;
1493 int y = region->pRectangles[r].offset.y;
1494 int width = static_cast<int>(
1495 region->pRectangles[r].extent.width);
1496 int height = static_cast<int>(
1497 region->pRectangles[r].extent.height);
1498 android_native_rect_t* cur_rect = &rects[r];
1499 cur_rect->left = x;
1500 cur_rect->top = y + height;
1501 cur_rect->right = x + width;
1502 cur_rect->bottom = y;
1503 }
1504 native_window_set_surface_damage(window, rects, rcount);
1505 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001506 if (time) {
1507 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001508 ALOGV(
1509 "Calling "
1510 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001511 native_window_enable_frame_timestamps(window, true);
1512 swapchain.frame_timestamps_enabled = true;
1513 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001514
1515 // Record the nativeFrameId so it can be later correlated to
1516 // this present.
1517 uint64_t nativeFrameId = 0;
1518 err = native_window_get_next_frame_id(
1519 window, &nativeFrameId);
1520 if (err != android::NO_ERROR) {
1521 ALOGE("Failed to get next native frame ID.");
1522 }
1523
1524 // Add a new timing record with the user's presentID and
1525 // the nativeFrameId.
1526 swapchain.timing.push_back(TimingInfo(time, nativeFrameId));
1527 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Ian Elliott8a977262017-01-19 09:05:58 -07001528 swapchain.timing.removeAt(0);
1529 }
1530 if (time->desiredPresentTime) {
1531 // Set the desiredPresentTime:
1532 ALOGV(
1533 "Calling "
1534 "native_window_set_buffers_timestamp(%" PRId64 ")",
1535 time->desiredPresentTime);
1536 native_window_set_buffers_timestamp(
1537 window,
1538 static_cast<int64_t>(time->desiredPresentTime));
1539 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001540 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001541
Jesse Halldc225072016-05-30 22:40:14 -07001542 err = window->queueBuffer(window, img.buffer.get(), fence);
1543 // queueBuffer always closes fence, even on error
1544 if (err != 0) {
1545 // TODO(jessehall): What now? We should probably cancel the
1546 // buffer, I guess?
1547 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1548 swapchain_result = WorstPresentResult(
1549 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
1550 }
1551 if (img.dequeue_fence >= 0) {
1552 close(img.dequeue_fence);
1553 img.dequeue_fence = -1;
1554 }
1555 img.dequeued = false;
Chris Forbesfca0f292017-03-30 19:48:39 +13001556
1557 // If the swapchain is in shared mode, immediately dequeue the
1558 // buffer so it can be presented again without an intervening
1559 // call to AcquireNextImageKHR. We expect to get the same buffer
1560 // back from every call to dequeueBuffer in this mode.
1561 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1562 ANativeWindowBuffer* buffer;
1563 int fence_fd;
1564 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1565 if (err != 0) {
1566 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1567 swapchain_result = WorstPresentResult(swapchain_result,
1568 VK_ERROR_SURFACE_LOST_KHR);
1569 }
1570 else if (img.buffer != buffer) {
1571 ALOGE("got wrong image back for shared swapchain");
1572 swapchain_result = WorstPresentResult(swapchain_result,
1573 VK_ERROR_SURFACE_LOST_KHR);
1574 }
1575 else {
1576 img.dequeue_fence = fence_fd;
1577 img.dequeued = true;
1578 }
1579 }
Jesse Halldc225072016-05-30 22:40:14 -07001580 }
1581 if (swapchain_result != VK_SUCCESS) {
1582 ReleaseSwapchainImage(device, window, fence, img);
1583 OrphanSwapchain(device, &swapchain);
1584 }
1585 } else {
1586 ReleaseSwapchainImage(device, nullptr, fence, img);
1587 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001588 }
1589
Jesse Halla9e57032015-11-30 01:03:10 -08001590 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001591 present_info->pResults[sc] = swapchain_result;
1592
1593 if (swapchain_result != final_result)
1594 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001595 }
Ian Elliottcb351132016-12-13 10:30:40 -07001596 if (rects) {
1597 allocator->pfnFree(allocator->pUserData, rects);
1598 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001599
1600 return final_result;
1601}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001602
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001603VKAPI_ATTR
1604VkResult GetRefreshCycleDurationGOOGLE(
1605 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001606 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001607 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Ian Elliott62c48c92017-01-20 13:13:20 -07001608 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001609 VkResult result = VK_SUCCESS;
1610
Brian Andersondc96fdf2017-03-20 16:54:25 -07001611 pDisplayTimingProperties->refreshDuration =
1612 static_cast<uint64_t>(swapchain.refresh_duration);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001613
1614 return result;
1615}
1616
1617VKAPI_ATTR
1618VkResult GetPastPresentationTimingGOOGLE(
1619 VkDevice,
1620 VkSwapchainKHR swapchain_handle,
1621 uint32_t* count,
1622 VkPastPresentationTimingGOOGLE* timings) {
1623 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1624 ANativeWindow* window = swapchain.surface.window.get();
1625 VkResult result = VK_SUCCESS;
1626
1627 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001628 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001629 native_window_enable_frame_timestamps(window, true);
1630 swapchain.frame_timestamps_enabled = true;
1631 }
1632
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001633 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001634 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1635 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001636 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001637 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001638 }
1639
1640 return result;
1641}
1642
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001643VKAPI_ATTR
1644VkResult GetSwapchainStatusKHR(
1645 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001646 VkSwapchainKHR swapchain_handle) {
1647 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001648 VkResult result = VK_SUCCESS;
1649
Chris Forbes4e18ba82017-01-20 12:50:17 +13001650 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1651 return VK_ERROR_OUT_OF_DATE_KHR;
1652 }
1653
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001654 // TODO(chrisforbes): Implement this function properly
1655
1656 return result;
1657}
1658
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001659VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001660 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001661 uint32_t swapchainCount,
1662 const VkSwapchainKHR* pSwapchains,
1663 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001664
1665 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1666 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1667 if (!swapchain)
1668 continue;
1669
1670 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1671
1672 ANativeWindow* window = swapchain->surface.window.get();
1673
1674 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1675 const android_smpte2086_metadata smpteMetdata = {
1676 {vulkanMetadata.displayPrimaryRed.x,
1677 vulkanMetadata.displayPrimaryRed.y},
1678 {vulkanMetadata.displayPrimaryGreen.x,
1679 vulkanMetadata.displayPrimaryGreen.y},
1680 {vulkanMetadata.displayPrimaryBlue.x,
1681 vulkanMetadata.displayPrimaryBlue.y},
1682 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1683 vulkanMetadata.maxLuminance,
1684 vulkanMetadata.minLuminance};
1685 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1686
1687 const android_cta861_3_metadata cta8613Metadata = {
1688 vulkanMetadata.maxContentLightLevel,
1689 vulkanMetadata.maxFrameAverageLightLevel};
1690 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1691 }
1692
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001693 return;
1694}
1695
Chia-I Wu62262232016-03-26 07:06:44 +08001696} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001697} // namespace vulkan