blob: e5ac2de705524ca88ad42117a72958ab4261dae1 [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
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang0f475222019-04-11 19:38:00 -070019#include <android/hardware/graphics/common/1.0/types.h>
Jesse Hall79927812017-03-23 11:03:23 -070020#include <grallocusage/GrallocUsageConversion.h>
Yiwei Zhang69395cd2019-07-03 16:55:39 -070021#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <log/log.h>
Jesse Halld7b994a2015-09-07 14:17:37 -070023#include <sync/sync.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070024#include <system/window.h>
25#include <ui/BufferQueueDefs.h>
Chia-I Wue8e689f2016-04-18 08:21:31 +080026#include <utils/StrongPointer.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080027#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070028
29#include <algorithm>
30#include <unordered_set>
31#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070032
Chia-I Wu4a6a9162016-03-26 07:17:34 +080033#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070034
Daniel Kochf25f5bb2017-10-05 00:26:58 -040035using android::hardware::graphics::common::V1_0::BufferUsage;
36
Chia-I Wu62262232016-03-26 07:06:44 +080037namespace vulkan {
38namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070039
Jesse Halld7b994a2015-09-07 14:17:37 -070040namespace {
41
Jesse Hall55bc0972016-02-23 16:43:29 -080042const VkSurfaceTransformFlagsKHR kSupportedTransforms =
43 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
44 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
45 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
46 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070047 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 |
Jesse Hall55bc0972016-02-23 16:43:29 -080051 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
Jesse Hall55bc0972016-02-23 16:43:29 -080060 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070061 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080062 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070063 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
64 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
65 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
66 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
67 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080068 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070069 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080070 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070071 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
72 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
73 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
74 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
75 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -080076 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
77 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
78 default:
79 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
80 }
81}
82
Yiwei Zhang70a21962019-05-31 17:26:52 -070083int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
84 switch (transform) {
85 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
86 return NATIVE_WINDOW_TRANSFORM_ROT_90;
87 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
88 return NATIVE_WINDOW_TRANSFORM_ROT_180;
89 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
90 return NATIVE_WINDOW_TRANSFORM_ROT_270;
91 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
92 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
93 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
94 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
95 NATIVE_WINDOW_TRANSFORM_ROT_90;
96 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
97 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
98 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
99 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
100 NATIVE_WINDOW_TRANSFORM_ROT_90;
101 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
102 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
103 default:
104 return 0;
105 }
106}
107
Jesse Hall178b6962016-02-24 15:39:50 -0800108int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
109 switch (transform) {
110 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
111 return NATIVE_WINDOW_TRANSFORM_ROT_270;
112 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
113 return NATIVE_WINDOW_TRANSFORM_ROT_180;
114 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
115 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700116 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
117 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
118 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
119 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
120 NATIVE_WINDOW_TRANSFORM_ROT_90;
121 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
122 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
123 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
124 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
125 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800126 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
127 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
128 default:
129 return 0;
130 }
131}
132
Ian Elliott8a977262017-01-19 09:05:58 -0700133class TimingInfo {
134 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800135 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700136 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800137 native_frame_id_(nativeFrameId) {}
138 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700139 return (timestamp_desired_present_time_ !=
140 NATIVE_WINDOW_TIMESTAMP_PENDING &&
141 timestamp_actual_present_time_ !=
142 NATIVE_WINDOW_TIMESTAMP_PENDING &&
143 timestamp_render_complete_time_ !=
144 NATIVE_WINDOW_TIMESTAMP_PENDING &&
145 timestamp_composition_latch_time_ !=
146 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700147 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700148 void calculate(int64_t rdur) {
149 bool anyTimestampInvalid =
150 (timestamp_actual_present_time_ ==
151 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
152 (timestamp_render_complete_time_ ==
153 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
154 (timestamp_composition_latch_time_ ==
155 NATIVE_WINDOW_TIMESTAMP_INVALID);
156 if (anyTimestampInvalid) {
157 ALOGE("Unexpectedly received invalid timestamp.");
158 vals_.actualPresentTime = 0;
159 vals_.earliestPresentTime = 0;
160 vals_.presentMargin = 0;
161 return;
162 }
163
164 vals_.actualPresentTime =
165 static_cast<uint64_t>(timestamp_actual_present_time_);
166 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700167 timestamp_render_complete_time_);
168 // Calculate vals_.earliestPresentTime, and potentially adjust
169 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
170 // is vals_.actualPresentTime. If we can subtract rdur (the duration
171 // of a refresh cycle) from vals_.earliestPresentTime (and also from
172 // vals_.presentMargin) and still leave a positive margin, then we can
173 // report to the application that it could have presented earlier than
174 // it did (per the extension specification). If for some reason, we
175 // can do this subtraction repeatedly, we do, since
176 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700177 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700178 while ((margin > rdur) &&
179 ((early_time - rdur) > timestamp_composition_latch_time_)) {
180 early_time -= rdur;
181 margin -= rdur;
182 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700183 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
184 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700185 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800186 void get_values(VkPastPresentationTimingGOOGLE* values) const {
187 *values = vals_;
188 }
Ian Elliott8a977262017-01-19 09:05:58 -0700189
190 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800191 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700192
Brian Anderson1049d1d2016-12-16 17:25:57 -0800193 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700194 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
195 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
196 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
197 int64_t timestamp_composition_latch_time_
198 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700199};
200
Jesse Hall1356b0d2015-11-23 17:24:58 -0800201struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800202 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700203 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700204 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800205};
206
207VkSurfaceKHR HandleFromSurface(Surface* surface) {
208 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
209}
210
211Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800212 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800213}
214
Ian Elliott8a977262017-01-19 09:05:58 -0700215// Maximum number of TimingInfo structs to keep per swapchain:
216enum { MAX_TIMING_INFOS = 10 };
217// Minimum number of frames to look for in the past (so we don't cause
218// syncronous requests to Surface Flinger):
219enum { MIN_NUM_FRAMES_AGO = 5 };
220
Jesse Hall1356b0d2015-11-23 17:24:58 -0800221struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700222 Swapchain(Surface& surface_,
223 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700224 VkPresentModeKHR present_mode,
225 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700226 : surface(surface_),
227 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700228 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700229 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300230 frame_timestamps_enabled(false),
231 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
232 present_mode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700233 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700234 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700235 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700236 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700237 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600238 uint64_t get_refresh_duration()
239 {
240 ANativeWindow* window = surface.window.get();
241 native_window_get_refresh_cycle_duration(
242 window,
243 &refresh_duration);
244 return static_cast<uint64_t>(refresh_duration);
245
246 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800247
248 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700249 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700250 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700251 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700252 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700253 int64_t refresh_duration;
Chris Forbesf8835642017-03-30 19:31:40 +1300254 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700255
256 struct Image {
257 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
258 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800259 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700260 // The fence is only valid when the buffer is dequeued, and should be
261 // -1 any other time. When valid, we own the fd, and must ensure it is
262 // closed: either by closing it explicitly when queueing the buffer,
263 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
264 int dequeue_fence;
265 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800266 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700267
Yiwei Zhang5e862202019-06-21 14:59:16 -0700268 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700269};
270
271VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
272 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
273}
274
275Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800276 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700277}
278
Jesse Halldc225072016-05-30 22:40:14 -0700279void ReleaseSwapchainImage(VkDevice device,
280 ANativeWindow* window,
281 int release_fence,
282 Swapchain::Image& image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700283 ATRACE_CALL();
284
Jesse Halldc225072016-05-30 22:40:14 -0700285 ALOG_ASSERT(release_fence == -1 || image.dequeued,
286 "ReleaseSwapchainImage: can't provide a release fence for "
287 "non-dequeued images");
288
289 if (image.dequeued) {
290 if (release_fence >= 0) {
291 // We get here from vkQueuePresentKHR. The application is
292 // responsible for creating an execution dependency chain from
293 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
294 // (release_fence), so we can drop the dequeue_fence here.
295 if (image.dequeue_fence >= 0)
296 close(image.dequeue_fence);
297 } else {
298 // We get here during swapchain destruction, or various serious
299 // error cases e.g. when we can't create the release_fence during
300 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
301 // have already signalled, since the swapchain images are supposed
302 // to be idle before the swapchain is destroyed. In error cases,
303 // there may be rendering in flight to the image, but since we
304 // weren't able to create a release_fence, waiting for the
305 // dequeue_fence is about the best we can do.
306 release_fence = image.dequeue_fence;
307 }
308 image.dequeue_fence = -1;
309
310 if (window) {
311 window->cancelBuffer(window, image.buffer.get(), release_fence);
312 } else {
313 if (release_fence >= 0) {
314 sync_wait(release_fence, -1 /* forever */);
315 close(release_fence);
316 }
317 }
318
319 image.dequeued = false;
320 }
321
322 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700323 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700324 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700325 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700326 image.image = VK_NULL_HANDLE;
327 }
328
329 image.buffer.clear();
330}
331
332void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
333 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
334 return;
Jesse Halldc225072016-05-30 22:40:14 -0700335 for (uint32_t i = 0; i < swapchain->num_images; i++) {
336 if (!swapchain->images[i].dequeued)
337 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
338 }
339 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700340 swapchain->timing.clear();
341}
342
343uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800344 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
345 return 0;
346 }
Ian Elliott8a977262017-01-19 09:05:58 -0700347
Brian Anderson1049d1d2016-12-16 17:25:57 -0800348 uint32_t num_ready = 0;
349 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
350 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700351 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800352 if (ti.ready()) {
353 // This TimingInfo is ready to be reported to the user. Add it
354 // to the num_ready.
355 num_ready++;
356 continue;
357 }
358 // This TimingInfo is not yet ready to be reported to the user,
359 // and so we should look for any available timestamps that
360 // might make it ready.
361 int64_t desired_present_time = 0;
362 int64_t render_complete_time = 0;
363 int64_t composition_latch_time = 0;
364 int64_t actual_present_time = 0;
365 // Obtain timestamps:
366 int ret = native_window_get_frame_timestamps(
367 swapchain.surface.window.get(), ti.native_frame_id_,
368 &desired_present_time, &render_complete_time,
369 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700370 nullptr, //&first_composition_start_time,
371 nullptr, //&last_composition_start_time,
372 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800373 // TODO(ianelliott): Maybe ask if this one is
374 // supported, at startup time (since it may not be
375 // supported):
376 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700377 nullptr, //&dequeue_ready_time,
378 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800379
380 if (ret != android::NO_ERROR) {
381 continue;
382 }
383
384 // Record the timestamp(s) we received, and then see if this TimingInfo
385 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700386 ti.timestamp_desired_present_time_ = desired_present_time;
387 ti.timestamp_actual_present_time_ = actual_present_time;
388 ti.timestamp_render_complete_time_ = render_complete_time;
389 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800390
391 if (ti.ready()) {
392 // The TimingInfo has received enough timestamps, and should now
393 // use those timestamps to calculate the info that should be
394 // reported to the user:
395 ti.calculate(swapchain.refresh_duration);
396 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700397 }
398 }
399 return num_ready;
400}
401
402// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
403void copy_ready_timings(Swapchain& swapchain,
404 uint32_t* count,
405 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800406 if (swapchain.timing.empty()) {
407 *count = 0;
408 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700409 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800410
411 size_t last_ready = swapchain.timing.size() - 1;
412 while (!swapchain.timing[last_ready].ready()) {
413 if (last_ready == 0) {
414 *count = 0;
415 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700416 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800417 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700418 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800419
420 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700421 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800422 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
423 const TimingInfo& ti = swapchain.timing[i];
424 if (ti.ready()) {
425 ti.get_values(&timings[num_copied]);
426 num_copied++;
427 }
428 num_to_remove++;
429 }
430
431 // Discard old frames that aren't ready if newer frames are ready.
432 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700433 swapchain.timing.erase(swapchain.timing.begin(),
434 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800435
Ian Elliott8a977262017-01-19 09:05:58 -0700436 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700437}
438
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700439android_pixel_format GetNativePixelFormat(VkFormat format) {
440 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
441 switch (format) {
442 case VK_FORMAT_R8G8B8A8_UNORM:
443 case VK_FORMAT_R8G8B8A8_SRGB:
444 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
445 break;
446 case VK_FORMAT_R5G6B5_UNORM_PACK16:
447 native_format = HAL_PIXEL_FORMAT_RGB_565;
448 break;
449 case VK_FORMAT_R16G16B16A16_SFLOAT:
450 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
451 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800452 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700453 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
454 break;
455 default:
456 ALOGV("unsupported swapchain format %d", format);
457 break;
458 }
459 return native_format;
460}
461
462android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
463 switch (colorspace) {
464 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
465 return HAL_DATASPACE_V0_SRGB;
466 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
467 return HAL_DATASPACE_DISPLAY_P3;
468 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
469 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600470 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
471 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700472 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
473 return HAL_DATASPACE_DCI_P3_LINEAR;
474 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
475 return HAL_DATASPACE_DCI_P3;
476 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
477 return HAL_DATASPACE_V0_SRGB_LINEAR;
478 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
479 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600480 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
481 return HAL_DATASPACE_BT2020_LINEAR;
482 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700483 return static_cast<android_dataspace>(
484 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
485 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600486 case VK_COLOR_SPACE_DOLBYVISION_EXT:
487 return static_cast<android_dataspace>(
488 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
489 HAL_DATASPACE_RANGE_FULL);
490 case VK_COLOR_SPACE_HDR10_HLG_EXT:
491 return static_cast<android_dataspace>(
492 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
493 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700494 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
495 return static_cast<android_dataspace>(
496 HAL_DATASPACE_STANDARD_ADOBE_RGB |
497 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
498 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
499 return HAL_DATASPACE_ADOBE_RGB;
500
501 // Pass through is intended to allow app to provide data that is passed
502 // to the display system without modification.
503 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
504 return HAL_DATASPACE_ARBITRARY;
505
506 default:
507 // This indicates that we don't know about the
508 // dataspace specified and we should indicate that
509 // it's unsupported
510 return HAL_DATASPACE_UNKNOWN;
511 }
512}
513
Jesse Halld7b994a2015-09-07 14:17:37 -0700514} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700515
Jesse Halle1b12782015-11-30 11:27:32 -0800516VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800517VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800518 VkInstance instance,
519 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
520 const VkAllocationCallbacks* allocator,
521 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800522 ATRACE_CALL();
523
Jesse Hall1f91d392015-12-11 16:28:44 -0800524 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800525 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800526 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
527 alignof(Surface),
528 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800529 if (!mem)
530 return VK_ERROR_OUT_OF_HOST_MEMORY;
531 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700532
Chia-I Wue8e689f2016-04-18 08:21:31 +0800533 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700534 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700535 int err = native_window_get_consumer_usage(surface->window.get(),
536 &surface->consumer_usage);
537 if (err != android::NO_ERROR) {
538 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
539 strerror(-err), err);
540 surface->~Surface();
541 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700542 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700543 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700544
Yiwei Zhang6435b322018-05-08 11:12:17 -0700545 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800546 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
547 if (err != 0) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800548 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
549 err);
550 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800551 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700552 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800553 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700554
Jesse Hall1356b0d2015-11-23 17:24:58 -0800555 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700556 return VK_SUCCESS;
557}
558
Jesse Halle1b12782015-11-30 11:27:32 -0800559VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800560void DestroySurfaceKHR(VkInstance instance,
561 VkSurfaceKHR surface_handle,
562 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800563 ATRACE_CALL();
564
Jesse Hall1356b0d2015-11-23 17:24:58 -0800565 Surface* surface = SurfaceFromHandle(surface_handle);
566 if (!surface)
567 return;
568 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700569 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700570 "destroyed VkSurfaceKHR 0x%" PRIx64
571 " has active VkSwapchainKHR 0x%" PRIx64,
572 reinterpret_cast<uint64_t>(surface_handle),
573 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800574 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800575 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800576 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800577 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800578}
579
Jesse Halle1b12782015-11-30 11:27:32 -0800580VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800581VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
582 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700583 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800584 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800585 ATRACE_CALL();
586
Yiwei Zhang6435b322018-05-08 11:12:17 -0700587 const Surface* surface = SurfaceFromHandle(surface_handle);
588 if (!surface) {
589 return VK_ERROR_SURFACE_LOST_KHR;
590 }
591 const ANativeWindow* window = surface->window.get();
592
593 int query_value;
594 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
595 if (err != 0 || query_value < 0) {
596 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
597 strerror(-err), err, query_value);
598 return VK_ERROR_SURFACE_LOST_KHR;
599 }
600
601 android_pixel_format native_format =
602 static_cast<android_pixel_format>(query_value);
603
604 bool format_supported = false;
605 switch (native_format) {
606 case HAL_PIXEL_FORMAT_RGBA_8888:
607 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700608 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800609 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700610 format_supported = true;
611 break;
612 default:
613 break;
614 }
615
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700616 *supported = static_cast<VkBool32>(
617 format_supported || (surface->consumer_usage &
618 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
619 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700620
Jesse Halla6429252015-11-29 18:59:42 -0800621 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800622}
623
Jesse Halle1b12782015-11-30 11:27:32 -0800624VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800625VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800626 VkPhysicalDevice /*pdev*/,
627 VkSurfaceKHR surface,
628 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800629 ATRACE_CALL();
630
Jesse Halld7b994a2015-09-07 14:17:37 -0700631 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800632 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700633
634 int width, height;
635 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
636 if (err != 0) {
637 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
638 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700639 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700640 }
641 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
642 if (err != 0) {
643 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
644 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700645 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700646 }
647
Jesse Hall55bc0972016-02-23 16:43:29 -0800648 int transform_hint;
649 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
650 if (err != 0) {
651 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
652 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700653 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800654 }
655
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800656 int max_buffer_count;
657 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
658 if (err != 0) {
659 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
660 strerror(-err), err);
661 return VK_ERROR_SURFACE_LOST_KHR;
662 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700663 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800664 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700665
Jesse Hallfe2662d2016-02-09 13:26:59 -0800666 capabilities->currentExtent =
667 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
668
Yiwei Zhang70a21962019-05-31 17:26:52 -0700669 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800670 capabilities->minImageExtent = VkExtent2D{1, 1};
671 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700672
Jesse Hallfe2662d2016-02-09 13:26:59 -0800673 capabilities->maxImageArrayLayers = 1;
674
Jesse Hall55bc0972016-02-23 16:43:29 -0800675 capabilities->supportedTransforms = kSupportedTransforms;
676 capabilities->currentTransform =
677 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700678
Jesse Hallfe2662d2016-02-09 13:26:59 -0800679 // On Android, window composition is a WindowManager property, not something
680 // associated with the bufferqueue. It can't be changed from here.
681 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700682
Jesse Hallb00daad2015-11-29 19:46:20 -0800683 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800684 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
685 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
686 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700687 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
688
Jesse Hallb1352bc2015-09-04 16:12:33 -0700689 return VK_SUCCESS;
690}
691
Jesse Halle1b12782015-11-30 11:27:32 -0800692VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700693VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
694 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800695 uint32_t* count,
696 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800697 ATRACE_CALL();
698
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700699 const InstanceData& instance_data = GetData(pdev);
700
Jesse Hall1356b0d2015-11-23 17:24:58 -0800701 // TODO(jessehall): Fill out the set of supported formats. Longer term, add
702 // a new gralloc method to query whether a (format, usage) pair is
703 // supported, and check that for each gralloc format that corresponds to a
704 // Vulkan format. Shorter term, just add a few more formats to the ones
705 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700706
707 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700708 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
709 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
710 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Ian Elliott94ace212019-02-20 14:05:29 -0700711 {VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
712 {VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700713 };
714 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700715 uint32_t total_num_formats = kNumFormats;
716
717 bool wide_color_support = false;
718 Surface& surface = *SurfaceFromHandle(surface_handle);
719 int err = native_window_get_wide_color_support(surface.window.get(),
720 &wide_color_support);
721 if (err) {
Yiwei Zhang70a21962019-05-31 17:26:52 -0700722 return VK_ERROR_SURFACE_LOST_KHR;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700723 }
724 ALOGV("wide_color_support is: %d", wide_color_support);
725 wide_color_support =
726 wide_color_support &&
727 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
728
729 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600730 {VK_FORMAT_R8G8B8A8_UNORM,
731 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
732 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700733 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700734 {VK_FORMAT_R16G16B16A16_SFLOAT,
735 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
736 {VK_FORMAT_R16G16B16A16_SFLOAT,
737 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800738 {VK_FORMAT_A2B10G10R10_UNORM_PACK32,
739 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700740 };
741 const uint32_t kNumWideColorFormats =
742 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
743 if (wide_color_support) {
744 total_num_formats += kNumWideColorFormats;
745 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700746
747 VkResult result = VK_SUCCESS;
748 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700749 uint32_t out_count = 0;
750 uint32_t transfer_count = 0;
751 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700752 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700753 transfer_count = std::min(*count, kNumFormats);
754 std::copy(kFormats, kFormats + transfer_count, formats);
755 out_count += transfer_count;
756 if (wide_color_support) {
757 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
758 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
759 formats + out_count);
760 out_count += transfer_count;
761 }
762 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700763 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700764 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700765 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700766 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700767}
768
Jesse Halle1b12782015-11-30 11:27:32 -0800769VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300770VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
771 VkPhysicalDevice physicalDevice,
772 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
773 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800774 ATRACE_CALL();
775
Chris Forbes2452cf72017-03-16 16:30:17 +1300776 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
777 physicalDevice, pSurfaceInfo->surface,
778 &pSurfaceCapabilities->surfaceCapabilities);
779
Chris Forbes06bc0092017-03-16 16:46:05 +1300780 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
781 while (caps->pNext) {
782 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
783
784 switch (caps->sType) {
785 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
786 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
787 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
788 caps);
789 // Claim same set of usage flags are supported for
790 // shared present modes as for other modes.
791 shared_caps->sharedPresentSupportedUsageFlags =
792 pSurfaceCapabilities->surfaceCapabilities
793 .supportedUsageFlags;
794 } break;
795
796 default:
797 // Ignore all other extension structs
798 break;
799 }
800 }
801
Chris Forbes2452cf72017-03-16 16:30:17 +1300802 return result;
803}
804
805VKAPI_ATTR
806VkResult GetPhysicalDeviceSurfaceFormats2KHR(
807 VkPhysicalDevice physicalDevice,
808 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
809 uint32_t* pSurfaceFormatCount,
810 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800811 ATRACE_CALL();
812
Chris Forbes2452cf72017-03-16 16:30:17 +1300813 if (!pSurfaceFormats) {
814 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
815 pSurfaceInfo->surface,
816 pSurfaceFormatCount, nullptr);
817 } else {
818 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
819 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700820 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300821 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
822 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700823 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300824
825 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
826 // marshal results individually due to stride difference.
827 // completely ignore any chained extension structs.
828 uint32_t formats_to_marshal = *pSurfaceFormatCount;
829 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
830 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
831 }
832 }
833
834 return result;
835 }
836}
837
838VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300839VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800840 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800841 uint32_t* count,
842 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800843 ATRACE_CALL();
844
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800845 int err;
846 int query_value;
847 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
848
849 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
850 if (err != 0 || query_value < 0) {
851 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
852 strerror(-err), err, query_value);
853 return VK_ERROR_SURFACE_LOST_KHR;
854 }
855 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
856
857 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
858 if (err != 0 || query_value < 0) {
859 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
860 strerror(-err), err, query_value);
861 return VK_ERROR_SURFACE_LOST_KHR;
862 }
863 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
864
Yiwei Zhang5e862202019-06-21 14:59:16 -0700865 std::vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800866 if (min_undequeued_buffers + 1 < max_buffer_count)
867 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300868 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
869
870 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
871 if (QueryPresentationProperties(pdev, &present_properties)) {
872 if (present_properties.sharedImage) {
873 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
874 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
875 }
876 }
877
878 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700879
880 VkResult result = VK_SUCCESS;
881 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300882 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700883 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300884 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -0700885 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700886 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300887 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700888 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700889 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700890}
891
Jesse Halle1b12782015-11-30 11:27:32 -0800892VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400893VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600894 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400895 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800896 ATRACE_CALL();
897
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400898 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
899 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
900 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
901 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
902 pDeviceGroupPresentCapabilities->sType);
903
904 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
905 sizeof(pDeviceGroupPresentCapabilities->presentMask));
906
907 // assume device group of size 1
908 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
909 pDeviceGroupPresentCapabilities->modes =
910 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
911
912 return VK_SUCCESS;
913}
914
915VKAPI_ATTR
916VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600917 VkDevice,
918 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400919 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800920 ATRACE_CALL();
921
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400922 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
923 return VK_SUCCESS;
924}
925
926VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600927VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400928 VkSurfaceKHR surface,
929 uint32_t* pRectCount,
930 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800931 ATRACE_CALL();
932
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400933 if (!pRects) {
934 *pRectCount = 1;
935 } else {
936 uint32_t count = std::min(*pRectCount, 1u);
937 bool incomplete = *pRectCount < 1;
938
939 *pRectCount = count;
940
941 if (incomplete) {
942 return VK_INCOMPLETE;
943 }
944
945 int err;
946 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
947
948 int width = 0, height = 0;
949 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
950 if (err != 0) {
951 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
952 strerror(-err), err);
953 }
954 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
955 if (err != 0) {
956 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
957 strerror(-err), err);
958 }
959
960 // TODO: Return something better than "whole window"
961 pRects[0].offset.x = 0;
962 pRects[0].offset.y = 0;
963 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
964 static_cast<uint32_t>(height)};
965 }
966 return VK_SUCCESS;
967}
968
Yiwei Zhang533cea92019-06-03 18:43:24 -0700969static void DestroySwapchainInternal(VkDevice device,
970 VkSwapchainKHR swapchain_handle,
971 const VkAllocationCallbacks* allocator) {
972 ATRACE_CALL();
973
974 const auto& dispatch = GetData(device).driver;
975 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
976 if (!swapchain) {
977 return;
978 }
979
980 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
981 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
982
983 if (window && swapchain->frame_timestamps_enabled) {
984 native_window_enable_frame_timestamps(window, false);
985 }
986
987 for (uint32_t i = 0; i < swapchain->num_images; i++) {
988 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
989 }
990
991 if (active) {
992 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
993 }
994
995 if (!allocator) {
996 allocator = &GetData(device).allocator;
997 }
998
999 swapchain->~Swapchain();
1000 allocator->pfnFree(allocator->pUserData, swapchain);
1001}
1002
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001003VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001004VkResult CreateSwapchainKHR(VkDevice device,
1005 const VkSwapchainCreateInfoKHR* create_info,
1006 const VkAllocationCallbacks* allocator,
1007 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001008 ATRACE_CALL();
1009
Jesse Halld7b994a2015-09-07 14:17:37 -07001010 int err;
1011 VkResult result = VK_SUCCESS;
1012
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001013 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1014 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1015 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1016 " oldSwapchain=0x%" PRIx64,
1017 reinterpret_cast<uint64_t>(create_info->surface),
1018 create_info->minImageCount, create_info->imageFormat,
1019 create_info->imageColorSpace, create_info->imageExtent.width,
1020 create_info->imageExtent.height, create_info->imageUsage,
1021 create_info->preTransform, create_info->presentMode,
1022 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1023
Jesse Hall1f91d392015-12-11 16:28:44 -08001024 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001025 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001026
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001027 android_pixel_format native_pixel_format =
1028 GetNativePixelFormat(create_info->imageFormat);
1029 android_dataspace native_dataspace =
1030 GetNativeDataspace(create_info->imageColorSpace);
1031 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1032 ALOGE(
1033 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1034 "failed: Unsupported color space",
1035 create_info->imageColorSpace);
1036 return VK_ERROR_INITIALIZATION_FAILED;
1037 }
1038
Jesse Hall42a9eec2016-06-03 12:39:49 -07001039 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001040 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001041 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001042 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001043 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001044 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001045 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001046 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001047 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1048 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001049 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001050 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001051
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001052 Surface& surface = *SurfaceFromHandle(create_info->surface);
1053
Jesse Halldc225072016-05-30 22:40:14 -07001054 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001055 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001056 " because it already has active swapchain 0x%" PRIx64
1057 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1058 reinterpret_cast<uint64_t>(create_info->surface),
1059 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1060 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1061 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1062 }
1063 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1064 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1065
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001066 // -- Reset the native window --
1067 // The native window might have been used previously, and had its properties
1068 // changed from defaults. That will affect the answer we get for queries
1069 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1070 // attempt such queries.
1071
Jesse Halldc225072016-05-30 22:40:14 -07001072 // The native window only allows dequeueing all buffers before any have
1073 // been queued, since after that point at least one is assumed to be in
1074 // non-FREE state at any given time. Disconnecting and re-connecting
1075 // orphans the previous buffers, getting us back to the state where we can
1076 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001077 //
1078 // TODO(http://b/134186185) recycle swapchain images more efficiently
Jesse Halldc225072016-05-30 22:40:14 -07001079 err = native_window_api_disconnect(surface.window.get(),
1080 NATIVE_WINDOW_API_EGL);
1081 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1082 strerror(-err), err);
1083 err =
1084 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1085 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1086 strerror(-err), err);
1087
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001088 err = native_window_set_buffer_count(surface.window.get(), 0);
1089 if (err != 0) {
1090 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1091 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001092 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001093 }
1094
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301095 int swap_interval =
1096 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1097 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001098 if (err != 0) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001099 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1100 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001101 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001102 }
1103
Chris Forbesb8042d22017-01-18 18:07:05 +13001104 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1105 if (err != 0) {
1106 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1107 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001108 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001109 }
1110
1111 err = native_window_set_auto_refresh(surface.window.get(), false);
1112 if (err != 0) {
1113 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1114 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001115 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001116 }
1117
Jesse Halld7b994a2015-09-07 14:17:37 -07001118 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001119
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001120 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001121
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001122 err = native_window_set_buffers_format(surface.window.get(),
1123 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001124 if (err != 0) {
Jesse Hall517274a2016-02-10 00:07:18 -08001125 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001126 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001127 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001128 }
1129 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001130 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001131 if (err != 0) {
Jesse Hall517274a2016-02-10 00:07:18 -08001132 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001133 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001134 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001135 }
1136
Jesse Hall3dd678a2016-01-08 21:52:01 -08001137 err = native_window_set_buffers_dimensions(
1138 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1139 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001140 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001141 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1142 create_info->imageExtent.width, create_info->imageExtent.height,
1143 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001144 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001145 }
1146
Jesse Hall178b6962016-02-24 15:39:50 -08001147 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1148 // applied during rendering. native_window_set_transform() expects the
1149 // inverse: the transform the app is requesting that the compositor perform
1150 // during composition. With native windows, pre-transform works by rendering
1151 // with the same transform the compositor is applying (as in Vulkan), but
1152 // then requesting the inverse transform, so that when the compositor does
1153 // it's job the two transforms cancel each other out and the compositor ends
1154 // up applying an identity transform to the app's buffer.
1155 err = native_window_set_buffers_transform(
1156 surface.window.get(),
1157 InvertTransformToNative(create_info->preTransform));
1158 if (err != 0) {
Jesse Hall178b6962016-02-24 15:39:50 -08001159 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1160 InvertTransformToNative(create_info->preTransform),
1161 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001162 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001163 }
1164
Jesse Hallf64ca122015-11-03 16:11:10 -08001165 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001166 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001167 if (err != 0) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001168 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1169 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001170 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001171 }
1172
Chris Forbes97ef4612017-03-30 19:37:50 +13001173 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1174 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1175 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1176 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1177 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1178 if (err != 0) {
1179 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1180 return VK_ERROR_SURFACE_LOST_KHR;
1181 }
1182 }
1183
1184 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1185 err = native_window_set_auto_refresh(surface.window.get(), true);
1186 if (err != 0) {
1187 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1188 return VK_ERROR_SURFACE_LOST_KHR;
1189 }
1190 }
1191
Jesse Halle6080bf2016-02-28 20:58:50 -08001192 int query_value;
1193 err = surface.window->query(surface.window.get(),
1194 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1195 &query_value);
1196 if (err != 0 || query_value < 0) {
Jesse Halle6080bf2016-02-28 20:58:50 -08001197 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1198 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001199 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001200 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001201 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001202 uint32_t num_images =
Ian Elliotta3515f42019-05-15 14:11:33 -06001203 (swap_interval ? create_info->minImageCount
1204 : std::max(3u, create_info->minImageCount)) -
1205 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001206
1207 // Lower layer insists that we have at least two buffers. This is wasteful
1208 // and we'd like to relax it in the shared case, but not all the pieces are
1209 // in place for that to work yet. Note we only lie to the lower layer-- we
1210 // don't want to give the app back a swapchain with extra images (which they
1211 // can't actually use!).
1212 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001213 if (err != 0) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001214 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1215 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001216 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001217 }
1218
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001219 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001220 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001221 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001222 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001223 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1224 device, create_info->imageFormat, create_info->imageUsage,
1225 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001226 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001227 if (result != VK_SUCCESS) {
1228 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001229 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001230 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001231 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001232 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001233 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001234 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001235 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001236 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001237 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001238 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001239 if (result != VK_SUCCESS) {
1240 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001241 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001242 }
Jesse Hall70f93352015-11-04 09:41:31 -08001243 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001244 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1245
1246 bool createProtectedSwapchain = false;
1247 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1248 createProtectedSwapchain = true;
1249 native_usage |= BufferUsage::PROTECTED;
1250 }
1251 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001252 if (err != 0) {
Jesse Hall70f93352015-11-04 09:41:31 -08001253 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001254 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001255 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001256
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001257 int transform_hint;
1258 err = surface.window->query(surface.window.get(),
1259 NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1260 if (err != 0) {
1261 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1262 strerror(-err), err);
1263 return VK_ERROR_SURFACE_LOST_KHR;
1264 }
1265
Jesse Halld7b994a2015-09-07 14:17:37 -07001266 // -- Allocate our Swapchain object --
1267 // After this point, we must deallocate the swapchain on error.
1268
Jesse Hall1f91d392015-12-11 16:28:44 -08001269 void* mem = allocator->pfnAllocation(allocator->pUserData,
1270 sizeof(Swapchain), alignof(Swapchain),
1271 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001272 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001273 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001274 Swapchain* swapchain = new (mem)
1275 Swapchain(surface, num_images, create_info->presentMode,
1276 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001277 // -- Dequeue all buffers and create a VkImage for each --
1278 // Any failures during or after this must cancel the dequeued buffers.
1279
Chris Forbesb56287a2017-01-12 14:28:58 +13001280 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1281#pragma clang diagnostic push
1282#pragma clang diagnostic ignored "-Wold-style-cast"
1283 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1284#pragma clang diagnostic pop
1285 .pNext = nullptr,
1286 .usage = swapchain_image_usage,
1287 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001288 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001289#pragma clang diagnostic push
1290#pragma clang diagnostic ignored "-Wold-style-cast"
1291 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1292#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001293 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001294 };
1295 VkImageCreateInfo image_create = {
1296 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1297 .pNext = &image_native_buffer,
1298 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001299 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001300 .extent = {0, 0, 1},
1301 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001302 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001303 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001304 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001305 .usage = create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001306 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001307 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001308 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001309 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1310 };
1311
Jesse Halld7b994a2015-09-07 14:17:37 -07001312 for (uint32_t i = 0; i < num_images; i++) {
1313 Swapchain::Image& img = swapchain->images[i];
1314
1315 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001316 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1317 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001318 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001319 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001320 result = VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001321 break;
1322 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001323 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001324 img.dequeued = true;
1325
1326 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001327 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1328 static_cast<uint32_t>(img.buffer->height),
1329 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001330 image_native_buffer.handle = img.buffer->handle;
1331 image_native_buffer.stride = img.buffer->stride;
1332 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001333 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001334 android_convertGralloc0To1Usage(int(img.buffer->usage),
1335 &image_native_buffer.usage2.producer,
1336 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001337
Yiwei Zhang533cea92019-06-03 18:43:24 -07001338 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001339 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001340 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001341 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001342 if (result != VK_SUCCESS) {
1343 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1344 break;
1345 }
1346 }
1347
1348 // -- Cancel all buffers, returning them to the queue --
1349 // If an error occurred before, also destroy the VkImage and release the
1350 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001351 for (uint32_t i = 0; i < num_images; i++) {
1352 Swapchain::Image& img = swapchain->images[i];
1353 if (img.dequeued) {
1354 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001355 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1356 img.dequeue_fence);
1357 img.dequeue_fence = -1;
1358 img.dequeued = false;
1359 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001360 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001361 }
1362
1363 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001364 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1365 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001366 return result;
1367 }
1368
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001369 if (transform_hint != swapchain->pre_transform) {
1370 // Log that the app is not doing pre-rotation.
1371 android::GraphicsEnv::getInstance().setTargetStats(
1372 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1373 }
1374
Jesse Halldc225072016-05-30 22:40:14 -07001375 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1376 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001377 return VK_SUCCESS;
1378}
1379
Jesse Halle1b12782015-11-30 11:27:32 -08001380VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001381void DestroySwapchainKHR(VkDevice device,
1382 VkSwapchainKHR swapchain_handle,
1383 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001384 ATRACE_CALL();
1385
Yiwei Zhang533cea92019-06-03 18:43:24 -07001386 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001387}
1388
Jesse Halle1b12782015-11-30 11:27:32 -08001389VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001390VkResult GetSwapchainImagesKHR(VkDevice,
1391 VkSwapchainKHR swapchain_handle,
1392 uint32_t* count,
1393 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001394 ATRACE_CALL();
1395
Jesse Halld7b994a2015-09-07 14:17:37 -07001396 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001397 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1398 "getting images for non-active swapchain 0x%" PRIx64
1399 "; only dequeued image handles are valid",
1400 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001401 VkResult result = VK_SUCCESS;
1402 if (images) {
1403 uint32_t n = swapchain.num_images;
1404 if (*count < swapchain.num_images) {
1405 n = *count;
1406 result = VK_INCOMPLETE;
1407 }
1408 for (uint32_t i = 0; i < n; i++)
1409 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001410 *count = n;
1411 } else {
1412 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001413 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001414 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001415}
1416
Jesse Halle1b12782015-11-30 11:27:32 -08001417VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001418VkResult AcquireNextImageKHR(VkDevice device,
1419 VkSwapchainKHR swapchain_handle,
1420 uint64_t timeout,
1421 VkSemaphore semaphore,
1422 VkFence vk_fence,
1423 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001424 ATRACE_CALL();
1425
Jesse Halld7b994a2015-09-07 14:17:37 -07001426 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001427 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001428 VkResult result;
1429 int err;
1430
Jesse Halldc225072016-05-30 22:40:14 -07001431 if (swapchain.surface.swapchain_handle != swapchain_handle)
1432 return VK_ERROR_OUT_OF_DATE_KHR;
1433
Jesse Halld7b994a2015-09-07 14:17:37 -07001434 ALOGW_IF(
1435 timeout != UINT64_MAX,
1436 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1437
Chris Forbesc88409c2017-03-30 19:47:37 +13001438 if (swapchain.shared) {
1439 // In shared mode, we keep the buffer dequeued all the time, so we don't
1440 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1441 // the semaphore and fence passed to us will be signalled.
1442 *image_index = 0;
1443 result = GetData(device).driver.AcquireImageANDROID(
1444 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1445 return result;
1446 }
1447
Jesse Halld7b994a2015-09-07 14:17:37 -07001448 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001449 int fence_fd;
1450 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001451 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001452 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001453 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001454 }
1455
1456 uint32_t idx;
1457 for (idx = 0; idx < swapchain.num_images; idx++) {
1458 if (swapchain.images[idx].buffer.get() == buffer) {
1459 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001460 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001461 break;
1462 }
1463 }
1464 if (idx == swapchain.num_images) {
1465 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001466 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001467 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001468 }
1469
1470 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001471 if (fence_fd != -1) {
1472 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001473 if (fence_clone == -1) {
1474 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1475 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001476 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001477 }
1478 }
1479
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001480 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001481 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001482 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001483 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1484 // even if the call fails. We could close it ourselves on failure, but
1485 // that would create a race condition if the driver closes it on a
1486 // failure path: some other thread might create an fd with the same
1487 // number between the time the driver closes it and the time we close
1488 // it. We must assume one of: the driver *always* closes it even on
1489 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001490 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001491 swapchain.images[idx].dequeued = false;
1492 swapchain.images[idx].dequeue_fence = -1;
1493 return result;
1494 }
1495
1496 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001497 return VK_SUCCESS;
1498}
1499
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001500VKAPI_ATTR
1501VkResult AcquireNextImage2KHR(VkDevice device,
1502 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1503 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001504 ATRACE_CALL();
1505
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001506 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1507 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1508 pAcquireInfo->fence, pImageIndex);
1509}
1510
Jesse Halldc225072016-05-30 22:40:14 -07001511static VkResult WorstPresentResult(VkResult a, VkResult b) {
1512 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1513 // (in spec version 1.0.14).
1514 static const VkResult kWorstToBest[] = {
1515 VK_ERROR_DEVICE_LOST,
1516 VK_ERROR_SURFACE_LOST_KHR,
1517 VK_ERROR_OUT_OF_DATE_KHR,
1518 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1519 VK_ERROR_OUT_OF_HOST_MEMORY,
1520 VK_SUBOPTIMAL_KHR,
1521 };
1522 for (auto result : kWorstToBest) {
1523 if (a == result || b == result)
1524 return result;
1525 }
1526 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1527 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1528 return a != VK_SUCCESS ? a : b;
1529}
1530
Jesse Halle1b12782015-11-30 11:27:32 -08001531VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001532VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001533 ATRACE_CALL();
1534
Jesse Halld7b994a2015-09-07 14:17:37 -07001535 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1536 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1537 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001538
Jesse Halldc225072016-05-30 22:40:14 -07001539 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001540 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001541 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001542
Ian Elliottcb351132016-12-13 10:30:40 -07001543 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001544 const VkPresentRegionsKHR* present_regions = nullptr;
1545 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001546 const VkPresentRegionsKHR* next =
1547 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1548 while (next) {
1549 switch (next->sType) {
1550 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1551 present_regions = next;
1552 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001553 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001554 present_times =
1555 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1556 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001557 default:
1558 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1559 next->sType);
1560 break;
1561 }
1562 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1563 }
1564 ALOGV_IF(
1565 present_regions &&
1566 present_regions->swapchainCount != present_info->swapchainCount,
1567 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001568 ALOGV_IF(present_times &&
1569 present_times->swapchainCount != present_info->swapchainCount,
1570 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1571 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001572 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001573 (present_regions) ? present_regions->pRegions : nullptr;
1574 const VkPresentTimeGOOGLE* times =
1575 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001576 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001577 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001578 uint32_t nrects = 0;
1579
Jesse Halld7b994a2015-09-07 14:17:37 -07001580 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1581 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001582 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001583 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001584 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001585 const VkPresentRegionKHR* region =
1586 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001587 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001588 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001589 VkResult result;
1590 int err;
1591
Jesse Halld7b994a2015-09-07 14:17:37 -07001592 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001593 result = dispatch.QueueSignalReleaseImageANDROID(
1594 queue, present_info->waitSemaphoreCount,
1595 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001596 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001597 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001598 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001599 }
1600
Jesse Halldc225072016-05-30 22:40:14 -07001601 if (swapchain.surface.swapchain_handle ==
1602 present_info->pSwapchains[sc]) {
1603 ANativeWindow* window = swapchain.surface.window.get();
1604 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001605 if (region) {
1606 // Process the incremental-present hint for this swapchain:
1607 uint32_t rcount = region->rectangleCount;
1608 if (rcount > nrects) {
1609 android_native_rect_t* new_rects =
1610 static_cast<android_native_rect_t*>(
1611 allocator->pfnReallocation(
1612 allocator->pUserData, rects,
1613 sizeof(android_native_rect_t) * rcount,
1614 alignof(android_native_rect_t),
1615 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1616 if (new_rects) {
1617 rects = new_rects;
1618 nrects = rcount;
1619 } else {
1620 rcount = 0; // Ignore the hint for this swapchain
1621 }
1622 }
1623 for (uint32_t r = 0; r < rcount; ++r) {
1624 if (region->pRectangles[r].layer > 0) {
1625 ALOGV(
1626 "vkQueuePresentKHR ignoring invalid layer "
1627 "(%u); using layer 0 instead",
1628 region->pRectangles[r].layer);
1629 }
1630 int x = region->pRectangles[r].offset.x;
1631 int y = region->pRectangles[r].offset.y;
1632 int width = static_cast<int>(
1633 region->pRectangles[r].extent.width);
1634 int height = static_cast<int>(
1635 region->pRectangles[r].extent.height);
1636 android_native_rect_t* cur_rect = &rects[r];
1637 cur_rect->left = x;
1638 cur_rect->top = y + height;
1639 cur_rect->right = x + width;
1640 cur_rect->bottom = y;
1641 }
1642 native_window_set_surface_damage(window, rects, rcount);
1643 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001644 if (time) {
1645 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001646 ALOGV(
1647 "Calling "
1648 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001649 native_window_enable_frame_timestamps(window, true);
1650 swapchain.frame_timestamps_enabled = true;
1651 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001652
1653 // Record the nativeFrameId so it can be later correlated to
1654 // this present.
1655 uint64_t nativeFrameId = 0;
1656 err = native_window_get_next_frame_id(
1657 window, &nativeFrameId);
1658 if (err != android::NO_ERROR) {
1659 ALOGE("Failed to get next native frame ID.");
1660 }
1661
1662 // Add a new timing record with the user's presentID and
1663 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001664 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001665 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001666 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001667 }
1668 if (time->desiredPresentTime) {
1669 // Set the desiredPresentTime:
1670 ALOGV(
1671 "Calling "
1672 "native_window_set_buffers_timestamp(%" PRId64 ")",
1673 time->desiredPresentTime);
1674 native_window_set_buffers_timestamp(
1675 window,
1676 static_cast<int64_t>(time->desiredPresentTime));
1677 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001678 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001679
Jesse Halldc225072016-05-30 22:40:14 -07001680 err = window->queueBuffer(window, img.buffer.get(), fence);
1681 // queueBuffer always closes fence, even on error
1682 if (err != 0) {
Jesse Halldc225072016-05-30 22:40:14 -07001683 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1684 swapchain_result = WorstPresentResult(
1685 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001686 } else {
1687 if (img.dequeue_fence >= 0) {
1688 close(img.dequeue_fence);
1689 img.dequeue_fence = -1;
1690 }
1691 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001692 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001693
1694 // If the swapchain is in shared mode, immediately dequeue the
1695 // buffer so it can be presented again without an intervening
1696 // call to AcquireNextImageKHR. We expect to get the same buffer
1697 // back from every call to dequeueBuffer in this mode.
1698 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1699 ANativeWindowBuffer* buffer;
1700 int fence_fd;
1701 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1702 if (err != 0) {
1703 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1704 swapchain_result = WorstPresentResult(swapchain_result,
1705 VK_ERROR_SURFACE_LOST_KHR);
1706 }
1707 else if (img.buffer != buffer) {
1708 ALOGE("got wrong image back for shared swapchain");
1709 swapchain_result = WorstPresentResult(swapchain_result,
1710 VK_ERROR_SURFACE_LOST_KHR);
1711 }
1712 else {
1713 img.dequeue_fence = fence_fd;
1714 img.dequeued = true;
1715 }
1716 }
Jesse Halldc225072016-05-30 22:40:14 -07001717 }
1718 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001719 OrphanSwapchain(device, &swapchain);
1720 }
silence_dogood73597592019-05-23 16:57:37 -07001721 int window_transform_hint;
1722 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1723 &window_transform_hint);
1724 if (err != 0) {
1725 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1726 strerror(-err), err);
1727 swapchain_result = WorstPresentResult(
1728 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1729 }
1730 if (swapchain.pre_transform != window_transform_hint) {
1731 swapchain_result =
1732 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1733 }
Jesse Halldc225072016-05-30 22:40:14 -07001734 } else {
1735 ReleaseSwapchainImage(device, nullptr, fence, img);
1736 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001737 }
1738
Jesse Halla9e57032015-11-30 01:03:10 -08001739 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001740 present_info->pResults[sc] = swapchain_result;
1741
1742 if (swapchain_result != final_result)
1743 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001744 }
Ian Elliottcb351132016-12-13 10:30:40 -07001745 if (rects) {
1746 allocator->pfnFree(allocator->pUserData, rects);
1747 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001748
1749 return final_result;
1750}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001751
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001752VKAPI_ATTR
1753VkResult GetRefreshCycleDurationGOOGLE(
1754 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001755 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001756 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001757 ATRACE_CALL();
1758
Ian Elliott62c48c92017-01-20 13:13:20 -07001759 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001760 VkResult result = VK_SUCCESS;
1761
Ian Elliott3568bfe2019-05-03 15:54:46 -06001762 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001763
1764 return result;
1765}
1766
1767VKAPI_ATTR
1768VkResult GetPastPresentationTimingGOOGLE(
1769 VkDevice,
1770 VkSwapchainKHR swapchain_handle,
1771 uint32_t* count,
1772 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001773 ATRACE_CALL();
1774
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001775 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
1776 ANativeWindow* window = swapchain.surface.window.get();
1777 VkResult result = VK_SUCCESS;
1778
1779 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001780 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001781 native_window_enable_frame_timestamps(window, true);
1782 swapchain.frame_timestamps_enabled = true;
1783 }
1784
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001785 if (timings) {
Ian Elliott8a977262017-01-19 09:05:58 -07001786 // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
1787 copy_ready_timings(swapchain, count, timings);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001788 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001789 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001790 }
1791
1792 return result;
1793}
1794
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001795VKAPI_ATTR
1796VkResult GetSwapchainStatusKHR(
1797 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001798 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001799 ATRACE_CALL();
1800
Chris Forbes4e18ba82017-01-20 12:50:17 +13001801 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001802 VkResult result = VK_SUCCESS;
1803
Chris Forbes4e18ba82017-01-20 12:50:17 +13001804 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1805 return VK_ERROR_OUT_OF_DATE_KHR;
1806 }
1807
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001808 // TODO(chrisforbes): Implement this function properly
1809
1810 return result;
1811}
1812
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001813VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001814 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001815 uint32_t swapchainCount,
1816 const VkSwapchainKHR* pSwapchains,
1817 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001818 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001819
1820 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1821 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1822 if (!swapchain)
1823 continue;
1824
1825 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1826
1827 ANativeWindow* window = swapchain->surface.window.get();
1828
1829 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1830 const android_smpte2086_metadata smpteMetdata = {
1831 {vulkanMetadata.displayPrimaryRed.x,
1832 vulkanMetadata.displayPrimaryRed.y},
1833 {vulkanMetadata.displayPrimaryGreen.x,
1834 vulkanMetadata.displayPrimaryGreen.y},
1835 {vulkanMetadata.displayPrimaryBlue.x,
1836 vulkanMetadata.displayPrimaryBlue.y},
1837 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1838 vulkanMetadata.maxLuminance,
1839 vulkanMetadata.minLuminance};
1840 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1841
1842 const android_cta861_3_metadata cta8613Metadata = {
1843 vulkanMetadata.maxContentLightLevel,
1844 vulkanMetadata.maxFrameAverageLightLevel};
1845 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1846 }
1847
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001848 return;
1849}
1850
Yiwei Zhang0f475222019-04-11 19:38:00 -07001851static void InterceptBindImageMemory2(
1852 uint32_t bind_info_count,
1853 const VkBindImageMemoryInfo* bind_infos,
1854 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1855 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1856 out_native_buffers->clear();
1857 out_bind_infos->clear();
1858
1859 if (!bind_info_count)
1860 return;
1861
1862 std::unordered_set<uint32_t> intercepted_indexes;
1863
1864 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1865 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1866 bind_infos[idx].pNext);
1867 while (info &&
1868 info->sType !=
1869 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1870 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1871 info->pNext);
1872 }
1873
1874 if (!info)
1875 continue;
1876
1877 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1878 "swapchain handle must not be NULL");
1879 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1880 ALOG_ASSERT(
1881 info->imageIndex < swapchain->num_images,
1882 "imageIndex must be less than the number of images in swapchain");
1883
1884 ANativeWindowBuffer* buffer =
1885 swapchain->images[info->imageIndex].buffer.get();
1886 VkNativeBufferANDROID native_buffer = {
1887#pragma clang diagnostic push
1888#pragma clang diagnostic ignored "-Wold-style-cast"
1889 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1890#pragma clang diagnostic pop
1891 .pNext = bind_infos[idx].pNext,
1892 .handle = buffer->handle,
1893 .stride = buffer->stride,
1894 .format = buffer->format,
1895 .usage = int(buffer->usage),
1896 };
1897 // Reserve enough space to avoid letting re-allocation invalidate the
1898 // addresses of the elements inside.
1899 out_native_buffers->reserve(bind_info_count);
1900 out_native_buffers->emplace_back(native_buffer);
1901
1902 // Reserve the space now since we know how much is needed now.
1903 out_bind_infos->reserve(bind_info_count);
1904 out_bind_infos->emplace_back(bind_infos[idx]);
1905 out_bind_infos->back().pNext = &out_native_buffers->back();
1906
1907 intercepted_indexes.insert(idx);
1908 }
1909
1910 if (intercepted_indexes.empty())
1911 return;
1912
1913 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1914 if (intercepted_indexes.count(idx))
1915 continue;
1916 out_bind_infos->emplace_back(bind_infos[idx]);
1917 }
1918}
1919
Yiwei Zhang23143102019-04-10 18:24:05 -07001920VKAPI_ATTR
1921VkResult BindImageMemory2(VkDevice device,
1922 uint32_t bindInfoCount,
1923 const VkBindImageMemoryInfo* pBindInfos) {
1924 ATRACE_CALL();
1925
Yiwei Zhang0f475222019-04-11 19:38:00 -07001926 // out_native_buffers is for maintaining the lifecycle of the constructed
1927 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1928 std::vector<VkNativeBufferANDROID> out_native_buffers;
1929 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1930 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1931 &out_bind_infos);
1932 return GetData(device).driver.BindImageMemory2(
1933 device, bindInfoCount,
1934 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001935}
1936
1937VKAPI_ATTR
1938VkResult BindImageMemory2KHR(VkDevice device,
1939 uint32_t bindInfoCount,
1940 const VkBindImageMemoryInfo* pBindInfos) {
1941 ATRACE_CALL();
1942
Yiwei Zhang0f475222019-04-11 19:38:00 -07001943 std::vector<VkNativeBufferANDROID> out_native_buffers;
1944 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1945 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1946 &out_bind_infos);
1947 return GetData(device).driver.BindImageMemory2KHR(
1948 device, bindInfoCount,
1949 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001950}
1951
Chia-I Wu62262232016-03-26 07:06:44 +08001952} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001953} // namespace vulkan