blob: aa7c19a844db409746f6ed04820120f203699512 [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 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700374 nullptr, //&dequeue_ready_time,
375 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800376
377 if (ret != android::NO_ERROR) {
378 continue;
379 }
380
381 // Record the timestamp(s) we received, and then see if this TimingInfo
382 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700383 ti.timestamp_desired_present_time_ = desired_present_time;
384 ti.timestamp_actual_present_time_ = actual_present_time;
385 ti.timestamp_render_complete_time_ = render_complete_time;
386 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800387
388 if (ti.ready()) {
389 // The TimingInfo has received enough timestamps, and should now
390 // use those timestamps to calculate the info that should be
391 // reported to the user:
392 ti.calculate(swapchain.refresh_duration);
393 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700394 }
395 }
396 return num_ready;
397}
398
Ian Elliott8a977262017-01-19 09:05:58 -0700399void copy_ready_timings(Swapchain& swapchain,
400 uint32_t* count,
401 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800402 if (swapchain.timing.empty()) {
403 *count = 0;
404 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700405 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800406
407 size_t last_ready = swapchain.timing.size() - 1;
408 while (!swapchain.timing[last_ready].ready()) {
409 if (last_ready == 0) {
410 *count = 0;
411 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700412 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800413 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700414 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800415
416 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700417 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800418 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
419 const TimingInfo& ti = swapchain.timing[i];
420 if (ti.ready()) {
421 ti.get_values(&timings[num_copied]);
422 num_copied++;
423 }
424 num_to_remove++;
425 }
426
427 // Discard old frames that aren't ready if newer frames are ready.
428 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700429 swapchain.timing.erase(swapchain.timing.begin(),
430 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800431
Ian Elliott8a977262017-01-19 09:05:58 -0700432 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700433}
434
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700435android_pixel_format GetNativePixelFormat(VkFormat format) {
436 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
437 switch (format) {
438 case VK_FORMAT_R8G8B8A8_UNORM:
439 case VK_FORMAT_R8G8B8A8_SRGB:
440 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
441 break;
442 case VK_FORMAT_R5G6B5_UNORM_PACK16:
443 native_format = HAL_PIXEL_FORMAT_RGB_565;
444 break;
445 case VK_FORMAT_R16G16B16A16_SFLOAT:
446 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
447 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800448 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700449 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
450 break;
451 default:
452 ALOGV("unsupported swapchain format %d", format);
453 break;
454 }
455 return native_format;
456}
457
458android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
459 switch (colorspace) {
460 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
461 return HAL_DATASPACE_V0_SRGB;
462 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
463 return HAL_DATASPACE_DISPLAY_P3;
464 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
465 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600466 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
467 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700468 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
469 return HAL_DATASPACE_DCI_P3_LINEAR;
470 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
471 return HAL_DATASPACE_DCI_P3;
472 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
473 return HAL_DATASPACE_V0_SRGB_LINEAR;
474 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
475 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600476 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
477 return HAL_DATASPACE_BT2020_LINEAR;
478 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700479 return static_cast<android_dataspace>(
480 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
481 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600482 case VK_COLOR_SPACE_DOLBYVISION_EXT:
483 return static_cast<android_dataspace>(
484 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
485 HAL_DATASPACE_RANGE_FULL);
486 case VK_COLOR_SPACE_HDR10_HLG_EXT:
487 return static_cast<android_dataspace>(
488 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
489 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700490 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
491 return static_cast<android_dataspace>(
492 HAL_DATASPACE_STANDARD_ADOBE_RGB |
493 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
494 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
495 return HAL_DATASPACE_ADOBE_RGB;
496
497 // Pass through is intended to allow app to provide data that is passed
498 // to the display system without modification.
499 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
500 return HAL_DATASPACE_ARBITRARY;
501
502 default:
503 // This indicates that we don't know about the
504 // dataspace specified and we should indicate that
505 // it's unsupported
506 return HAL_DATASPACE_UNKNOWN;
507 }
508}
509
Jesse Halld7b994a2015-09-07 14:17:37 -0700510} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700511
Jesse Halle1b12782015-11-30 11:27:32 -0800512VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800513VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800514 VkInstance instance,
515 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
516 const VkAllocationCallbacks* allocator,
517 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800518 ATRACE_CALL();
519
Jesse Hall1f91d392015-12-11 16:28:44 -0800520 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800521 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800522 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
523 alignof(Surface),
524 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800525 if (!mem)
526 return VK_ERROR_OUT_OF_HOST_MEMORY;
527 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700528
Chia-I Wue8e689f2016-04-18 08:21:31 +0800529 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700530 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700531 int err = native_window_get_consumer_usage(surface->window.get(),
532 &surface->consumer_usage);
533 if (err != android::NO_ERROR) {
534 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
535 strerror(-err), err);
536 surface->~Surface();
537 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700538 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700539 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700540
Yiwei Zhang6435b322018-05-08 11:12:17 -0700541 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800542 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
543 if (err != 0) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800544 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
545 err);
546 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800547 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700548 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800549 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700550
Jesse Hall1356b0d2015-11-23 17:24:58 -0800551 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700552 return VK_SUCCESS;
553}
554
Jesse Halle1b12782015-11-30 11:27:32 -0800555VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800556void DestroySurfaceKHR(VkInstance instance,
557 VkSurfaceKHR surface_handle,
558 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800559 ATRACE_CALL();
560
Jesse Hall1356b0d2015-11-23 17:24:58 -0800561 Surface* surface = SurfaceFromHandle(surface_handle);
562 if (!surface)
563 return;
564 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700565 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700566 "destroyed VkSurfaceKHR 0x%" PRIx64
567 " has active VkSwapchainKHR 0x%" PRIx64,
568 reinterpret_cast<uint64_t>(surface_handle),
569 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800570 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800571 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800572 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800573 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800574}
575
Jesse Halle1b12782015-11-30 11:27:32 -0800576VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800577VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
578 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700579 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800580 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800581 ATRACE_CALL();
582
Yiwei Zhang6435b322018-05-08 11:12:17 -0700583 const Surface* surface = SurfaceFromHandle(surface_handle);
584 if (!surface) {
585 return VK_ERROR_SURFACE_LOST_KHR;
586 }
587 const ANativeWindow* window = surface->window.get();
588
589 int query_value;
590 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
591 if (err != 0 || query_value < 0) {
592 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
593 strerror(-err), err, query_value);
594 return VK_ERROR_SURFACE_LOST_KHR;
595 }
596
597 android_pixel_format native_format =
598 static_cast<android_pixel_format>(query_value);
599
600 bool format_supported = false;
601 switch (native_format) {
602 case HAL_PIXEL_FORMAT_RGBA_8888:
603 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700604 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800605 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700606 format_supported = true;
607 break;
608 default:
609 break;
610 }
611
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700612 *supported = static_cast<VkBool32>(
613 format_supported || (surface->consumer_usage &
614 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
615 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700616
Jesse Halla6429252015-11-29 18:59:42 -0800617 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800618}
619
Jesse Halle1b12782015-11-30 11:27:32 -0800620VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800621VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800622 VkPhysicalDevice /*pdev*/,
623 VkSurfaceKHR surface,
624 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800625 ATRACE_CALL();
626
Jesse Halld7b994a2015-09-07 14:17:37 -0700627 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800628 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700629
630 int width, height;
631 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
632 if (err != 0) {
633 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
634 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700635 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700636 }
637 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
638 if (err != 0) {
639 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
640 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700641 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700642 }
643
Jesse Hall55bc0972016-02-23 16:43:29 -0800644 int transform_hint;
645 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
646 if (err != 0) {
647 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
648 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700649 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800650 }
651
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800652 int max_buffer_count;
653 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
654 if (err != 0) {
655 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
656 strerror(-err), err);
657 return VK_ERROR_SURFACE_LOST_KHR;
658 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700659 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800660 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700661
Jesse Hallfe2662d2016-02-09 13:26:59 -0800662 capabilities->currentExtent =
663 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
664
Yiwei Zhang70a21962019-05-31 17:26:52 -0700665 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800666 capabilities->minImageExtent = VkExtent2D{1, 1};
667 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700668
Jesse Hallfe2662d2016-02-09 13:26:59 -0800669 capabilities->maxImageArrayLayers = 1;
670
Jesse Hall55bc0972016-02-23 16:43:29 -0800671 capabilities->supportedTransforms = kSupportedTransforms;
672 capabilities->currentTransform =
673 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700674
Jesse Hallfe2662d2016-02-09 13:26:59 -0800675 // On Android, window composition is a WindowManager property, not something
676 // associated with the bufferqueue. It can't be changed from here.
677 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700678
Jesse Hallb00daad2015-11-29 19:46:20 -0800679 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800680 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
681 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
682 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700683 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
684
Jesse Hallb1352bc2015-09-04 16:12:33 -0700685 return VK_SUCCESS;
686}
687
Jesse Halle1b12782015-11-30 11:27:32 -0800688VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700689VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
690 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800691 uint32_t* count,
692 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800693 ATRACE_CALL();
694
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700695 const InstanceData& instance_data = GetData(pdev);
696
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700697 bool wide_color_support = false;
698 Surface& surface = *SurfaceFromHandle(surface_handle);
699 int err = native_window_get_wide_color_support(surface.window.get(),
700 &wide_color_support);
701 if (err) {
Yiwei Zhang70a21962019-05-31 17:26:52 -0700702 return VK_ERROR_SURFACE_LOST_KHR;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700703 }
704 ALOGV("wide_color_support is: %d", wide_color_support);
705 wide_color_support =
706 wide_color_support &&
707 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
708
Charlie Lao324191f2019-12-13 11:03:16 -0800709 AHardwareBuffer_Desc desc = {};
710 desc.width = 1;
711 desc.height = 1;
712 desc.layers = 1;
713 desc.usage = surface.consumer_usage |
714 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
715 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
716
717 // We must support R8G8B8A8
718 std::vector<VkSurfaceFormatKHR> all_formats = {
719 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
720 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}};
721
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700722 if (wide_color_support) {
Charlie Lao324191f2019-12-13 11:03:16 -0800723 all_formats.emplace_back(VkSurfaceFormatKHR{
724 VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
725 all_formats.emplace_back(VkSurfaceFormatKHR{
726 VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
727 }
728
729 desc.format = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
730 if (AHardwareBuffer_isSupported(&desc)) {
731 all_formats.emplace_back(VkSurfaceFormatKHR{
732 VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
733 }
734
735 desc.format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
736 if (AHardwareBuffer_isSupported(&desc)) {
737 all_formats.emplace_back(VkSurfaceFormatKHR{
738 VK_FORMAT_R16G16B16A16_SFLOAT, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
739 if (wide_color_support) {
740 all_formats.emplace_back(
741 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
742 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT});
743 all_formats.emplace_back(
744 VkSurfaceFormatKHR{VK_FORMAT_R16G16B16A16_SFLOAT,
745 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT});
746 }
747 }
748
749 desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
750 if (AHardwareBuffer_isSupported(&desc)) {
751 all_formats.emplace_back(
752 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
753 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
754 if (wide_color_support) {
755 all_formats.emplace_back(
756 VkSurfaceFormatKHR{VK_FORMAT_A2B10G10R10_UNORM_PACK32,
757 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
758 }
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700759 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700760
761 VkResult result = VK_SUCCESS;
762 if (formats) {
Charlie Lao324191f2019-12-13 11:03:16 -0800763 uint32_t transfer_count = all_formats.size();
764 if (transfer_count > *count) {
765 transfer_count = *count;
Jesse Halld7b994a2015-09-07 14:17:37 -0700766 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700767 }
Charlie Lao324191f2019-12-13 11:03:16 -0800768 std::copy(all_formats.begin(), all_formats.begin() + transfer_count,
769 formats);
770 *count = transfer_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700771 } else {
Charlie Lao324191f2019-12-13 11:03:16 -0800772 *count = all_formats.size();
Jesse Halld7b994a2015-09-07 14:17:37 -0700773 }
Charlie Lao324191f2019-12-13 11:03:16 -0800774
Jesse Halld7b994a2015-09-07 14:17:37 -0700775 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700776}
777
Jesse Halle1b12782015-11-30 11:27:32 -0800778VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300779VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
780 VkPhysicalDevice physicalDevice,
781 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
782 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800783 ATRACE_CALL();
784
Chris Forbes2452cf72017-03-16 16:30:17 +1300785 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
786 physicalDevice, pSurfaceInfo->surface,
787 &pSurfaceCapabilities->surfaceCapabilities);
788
Chris Forbes06bc0092017-03-16 16:46:05 +1300789 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
790 while (caps->pNext) {
791 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
792
793 switch (caps->sType) {
794 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
795 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
796 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
797 caps);
798 // Claim same set of usage flags are supported for
799 // shared present modes as for other modes.
800 shared_caps->sharedPresentSupportedUsageFlags =
801 pSurfaceCapabilities->surfaceCapabilities
802 .supportedUsageFlags;
803 } break;
804
805 default:
806 // Ignore all other extension structs
807 break;
808 }
809 }
810
Chris Forbes2452cf72017-03-16 16:30:17 +1300811 return result;
812}
813
814VKAPI_ATTR
815VkResult GetPhysicalDeviceSurfaceFormats2KHR(
816 VkPhysicalDevice physicalDevice,
817 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
818 uint32_t* pSurfaceFormatCount,
819 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800820 ATRACE_CALL();
821
Chris Forbes2452cf72017-03-16 16:30:17 +1300822 if (!pSurfaceFormats) {
823 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
824 pSurfaceInfo->surface,
825 pSurfaceFormatCount, nullptr);
826 } else {
827 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
828 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700829 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300830 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
831 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700832 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300833
834 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
835 // marshal results individually due to stride difference.
836 // completely ignore any chained extension structs.
837 uint32_t formats_to_marshal = *pSurfaceFormatCount;
838 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
839 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
840 }
841 }
842
843 return result;
844 }
845}
846
847VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300848VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800849 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800850 uint32_t* count,
851 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800852 ATRACE_CALL();
853
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800854 int err;
855 int query_value;
856 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
857
858 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
859 if (err != 0 || query_value < 0) {
860 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
861 strerror(-err), err, query_value);
862 return VK_ERROR_SURFACE_LOST_KHR;
863 }
864 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
865
866 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
867 if (err != 0 || query_value < 0) {
868 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
869 strerror(-err), err, query_value);
870 return VK_ERROR_SURFACE_LOST_KHR;
871 }
872 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
873
Yiwei Zhang5e862202019-06-21 14:59:16 -0700874 std::vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800875 if (min_undequeued_buffers + 1 < max_buffer_count)
876 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300877 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
878
879 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
880 if (QueryPresentationProperties(pdev, &present_properties)) {
881 if (present_properties.sharedImage) {
882 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
883 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
884 }
885 }
886
887 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700888
889 VkResult result = VK_SUCCESS;
890 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300891 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700892 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300893 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -0700894 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700895 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300896 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700897 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700898 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700899}
900
Jesse Halle1b12782015-11-30 11:27:32 -0800901VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400902VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600903 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400904 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800905 ATRACE_CALL();
906
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400907 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
908 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
909 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
910 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
911 pDeviceGroupPresentCapabilities->sType);
912
913 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
914 sizeof(pDeviceGroupPresentCapabilities->presentMask));
915
916 // assume device group of size 1
917 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
918 pDeviceGroupPresentCapabilities->modes =
919 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
920
921 return VK_SUCCESS;
922}
923
924VKAPI_ATTR
925VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600926 VkDevice,
927 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400928 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800929 ATRACE_CALL();
930
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400931 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
932 return VK_SUCCESS;
933}
934
935VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600936VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400937 VkSurfaceKHR surface,
938 uint32_t* pRectCount,
939 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800940 ATRACE_CALL();
941
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400942 if (!pRects) {
943 *pRectCount = 1;
944 } else {
945 uint32_t count = std::min(*pRectCount, 1u);
946 bool incomplete = *pRectCount < 1;
947
948 *pRectCount = count;
949
950 if (incomplete) {
951 return VK_INCOMPLETE;
952 }
953
954 int err;
955 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
956
957 int width = 0, height = 0;
958 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
959 if (err != 0) {
960 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
961 strerror(-err), err);
962 }
963 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
964 if (err != 0) {
965 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
966 strerror(-err), err);
967 }
968
Yiwei Zhanga885c062019-10-24 12:07:57 -0700969 // TODO(b/143294545): Return something better than "whole window"
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400970 pRects[0].offset.x = 0;
971 pRects[0].offset.y = 0;
972 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
973 static_cast<uint32_t>(height)};
974 }
975 return VK_SUCCESS;
976}
977
Yiwei Zhang533cea92019-06-03 18:43:24 -0700978static void DestroySwapchainInternal(VkDevice device,
979 VkSwapchainKHR swapchain_handle,
980 const VkAllocationCallbacks* allocator) {
981 ATRACE_CALL();
982
983 const auto& dispatch = GetData(device).driver;
984 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
985 if (!swapchain) {
986 return;
987 }
988
989 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
990 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
991
992 if (window && swapchain->frame_timestamps_enabled) {
993 native_window_enable_frame_timestamps(window, false);
994 }
995
996 for (uint32_t i = 0; i < swapchain->num_images; i++) {
997 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
998 }
999
1000 if (active) {
1001 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
1002 }
1003
1004 if (!allocator) {
1005 allocator = &GetData(device).allocator;
1006 }
1007
1008 swapchain->~Swapchain();
1009 allocator->pfnFree(allocator->pUserData, swapchain);
1010}
1011
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001012VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001013VkResult CreateSwapchainKHR(VkDevice device,
1014 const VkSwapchainCreateInfoKHR* create_info,
1015 const VkAllocationCallbacks* allocator,
1016 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001017 ATRACE_CALL();
1018
Jesse Halld7b994a2015-09-07 14:17:37 -07001019 int err;
1020 VkResult result = VK_SUCCESS;
1021
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001022 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1023 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1024 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1025 " oldSwapchain=0x%" PRIx64,
1026 reinterpret_cast<uint64_t>(create_info->surface),
1027 create_info->minImageCount, create_info->imageFormat,
1028 create_info->imageColorSpace, create_info->imageExtent.width,
1029 create_info->imageExtent.height, create_info->imageUsage,
1030 create_info->preTransform, create_info->presentMode,
1031 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1032
Jesse Hall1f91d392015-12-11 16:28:44 -08001033 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001034 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001035
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001036 android_pixel_format native_pixel_format =
1037 GetNativePixelFormat(create_info->imageFormat);
1038 android_dataspace native_dataspace =
1039 GetNativeDataspace(create_info->imageColorSpace);
1040 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1041 ALOGE(
1042 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1043 "failed: Unsupported color space",
1044 create_info->imageColorSpace);
1045 return VK_ERROR_INITIALIZATION_FAILED;
1046 }
1047
Jesse Hall42a9eec2016-06-03 12:39:49 -07001048 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001049 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001050 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001051 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001052 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001053 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001054 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001055 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001056 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1057 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001058 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001059 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001060
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001061 Surface& surface = *SurfaceFromHandle(create_info->surface);
1062
Jesse Halldc225072016-05-30 22:40:14 -07001063 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001064 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001065 " because it already has active swapchain 0x%" PRIx64
1066 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1067 reinterpret_cast<uint64_t>(create_info->surface),
1068 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1069 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1070 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1071 }
1072 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1073 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1074
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001075 // -- Reset the native window --
1076 // The native window might have been used previously, and had its properties
1077 // changed from defaults. That will affect the answer we get for queries
1078 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1079 // attempt such queries.
1080
Jesse Halldc225072016-05-30 22:40:14 -07001081 // The native window only allows dequeueing all buffers before any have
1082 // been queued, since after that point at least one is assumed to be in
1083 // non-FREE state at any given time. Disconnecting and re-connecting
1084 // orphans the previous buffers, getting us back to the state where we can
1085 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001086 //
1087 // TODO(http://b/134186185) recycle swapchain images more efficiently
Jesse Halldc225072016-05-30 22:40:14 -07001088 err = native_window_api_disconnect(surface.window.get(),
1089 NATIVE_WINDOW_API_EGL);
1090 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1091 strerror(-err), err);
1092 err =
1093 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1094 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1095 strerror(-err), err);
1096
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001097 err = native_window_set_buffer_count(surface.window.get(), 0);
1098 if (err != 0) {
1099 ALOGE("native_window_set_buffer_count(0) 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
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301104 int swap_interval =
1105 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1106 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001107 if (err != 0) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001108 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1109 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001110 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001111 }
1112
Chris Forbesb8042d22017-01-18 18:07:05 +13001113 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1114 if (err != 0) {
1115 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1116 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001117 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001118 }
1119
1120 err = native_window_set_auto_refresh(surface.window.get(), false);
1121 if (err != 0) {
1122 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1123 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001124 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001125 }
1126
Jesse Halld7b994a2015-09-07 14:17:37 -07001127 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001128
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001129 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001130
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001131 err = native_window_set_buffers_format(surface.window.get(),
1132 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001133 if (err != 0) {
Jesse Hall517274a2016-02-10 00:07:18 -08001134 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001135 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001136 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001137 }
1138 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001139 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001140 if (err != 0) {
Jesse Hall517274a2016-02-10 00:07:18 -08001141 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001142 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001143 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001144 }
1145
Jesse Hall3dd678a2016-01-08 21:52:01 -08001146 err = native_window_set_buffers_dimensions(
1147 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1148 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001149 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001150 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1151 create_info->imageExtent.width, create_info->imageExtent.height,
1152 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001153 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001154 }
1155
Jesse Hall178b6962016-02-24 15:39:50 -08001156 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1157 // applied during rendering. native_window_set_transform() expects the
1158 // inverse: the transform the app is requesting that the compositor perform
1159 // during composition. With native windows, pre-transform works by rendering
1160 // with the same transform the compositor is applying (as in Vulkan), but
1161 // then requesting the inverse transform, so that when the compositor does
1162 // it's job the two transforms cancel each other out and the compositor ends
1163 // up applying an identity transform to the app's buffer.
1164 err = native_window_set_buffers_transform(
1165 surface.window.get(),
1166 InvertTransformToNative(create_info->preTransform));
1167 if (err != 0) {
Jesse Hall178b6962016-02-24 15:39:50 -08001168 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1169 InvertTransformToNative(create_info->preTransform),
1170 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001171 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001172 }
1173
Jesse Hallf64ca122015-11-03 16:11:10 -08001174 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001175 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001176 if (err != 0) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001177 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1178 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001179 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001180 }
1181
Chris Forbes97ef4612017-03-30 19:37:50 +13001182 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1183 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1184 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1185 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1186 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1187 if (err != 0) {
1188 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1189 return VK_ERROR_SURFACE_LOST_KHR;
1190 }
1191 }
1192
1193 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1194 err = native_window_set_auto_refresh(surface.window.get(), true);
1195 if (err != 0) {
1196 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1197 return VK_ERROR_SURFACE_LOST_KHR;
1198 }
1199 }
1200
Jesse Halle6080bf2016-02-28 20:58:50 -08001201 int query_value;
1202 err = surface.window->query(surface.window.get(),
1203 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1204 &query_value);
1205 if (err != 0 || query_value < 0) {
Jesse Halle6080bf2016-02-28 20:58:50 -08001206 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1207 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001208 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001209 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001210 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001211 uint32_t num_images =
Ian Elliotta3515f42019-05-15 14:11:33 -06001212 (swap_interval ? create_info->minImageCount
1213 : std::max(3u, create_info->minImageCount)) -
1214 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001215
1216 // Lower layer insists that we have at least two buffers. This is wasteful
1217 // and we'd like to relax it in the shared case, but not all the pieces are
1218 // in place for that to work yet. Note we only lie to the lower layer-- we
1219 // don't want to give the app back a swapchain with extra images (which they
1220 // can't actually use!).
1221 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001222 if (err != 0) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001223 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1224 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001225 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001226 }
1227
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001228 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001229 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001230 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001231 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001232 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1233 device, create_info->imageFormat, create_info->imageUsage,
1234 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001235 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001236 if (result != VK_SUCCESS) {
1237 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001238 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001239 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001240 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001241 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001242 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001243 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001244 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001245 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001246 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001247 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001248 if (result != VK_SUCCESS) {
1249 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001250 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001251 }
Jesse Hall70f93352015-11-04 09:41:31 -08001252 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001253 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1254
1255 bool createProtectedSwapchain = false;
1256 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1257 createProtectedSwapchain = true;
1258 native_usage |= BufferUsage::PROTECTED;
1259 }
1260 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001261 if (err != 0) {
Jesse Hall70f93352015-11-04 09:41:31 -08001262 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001263 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001264 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001265
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001266 int transform_hint;
1267 err = surface.window->query(surface.window.get(),
1268 NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1269 if (err != 0) {
1270 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1271 strerror(-err), err);
1272 return VK_ERROR_SURFACE_LOST_KHR;
1273 }
1274
Jesse Halld7b994a2015-09-07 14:17:37 -07001275 // -- Allocate our Swapchain object --
1276 // After this point, we must deallocate the swapchain on error.
1277
Jesse Hall1f91d392015-12-11 16:28:44 -08001278 void* mem = allocator->pfnAllocation(allocator->pUserData,
1279 sizeof(Swapchain), alignof(Swapchain),
1280 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001281 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001282 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001283 Swapchain* swapchain = new (mem)
1284 Swapchain(surface, num_images, create_info->presentMode,
1285 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001286 // -- Dequeue all buffers and create a VkImage for each --
1287 // Any failures during or after this must cancel the dequeued buffers.
1288
Chris Forbesb56287a2017-01-12 14:28:58 +13001289 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1290#pragma clang diagnostic push
1291#pragma clang diagnostic ignored "-Wold-style-cast"
1292 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1293#pragma clang diagnostic pop
1294 .pNext = nullptr,
1295 .usage = swapchain_image_usage,
1296 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001297 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001298#pragma clang diagnostic push
1299#pragma clang diagnostic ignored "-Wold-style-cast"
1300 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1301#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001302 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001303 };
1304 VkImageCreateInfo image_create = {
1305 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1306 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001307 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001308 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001309 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001310 .extent = {0, 0, 1},
1311 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001312 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001313 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001314 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001315 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001316 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001317 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001318 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1319 };
1320
Jesse Halld7b994a2015-09-07 14:17:37 -07001321 for (uint32_t i = 0; i < num_images; i++) {
1322 Swapchain::Image& img = swapchain->images[i];
1323
1324 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001325 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1326 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001327 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001328 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001329 switch (-err) {
1330 case ENOMEM:
1331 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1332 break;
1333 default:
1334 result = VK_ERROR_SURFACE_LOST_KHR;
1335 break;
1336 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001337 break;
1338 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001339 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001340 img.dequeued = true;
1341
1342 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001343 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1344 static_cast<uint32_t>(img.buffer->height),
1345 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001346 image_native_buffer.handle = img.buffer->handle;
1347 image_native_buffer.stride = img.buffer->stride;
1348 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001349 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001350 android_convertGralloc0To1Usage(int(img.buffer->usage),
1351 &image_native_buffer.usage2.producer,
1352 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001353
Yiwei Zhang533cea92019-06-03 18:43:24 -07001354 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001355 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001356 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001357 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001358 if (result != VK_SUCCESS) {
1359 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1360 break;
1361 }
1362 }
1363
1364 // -- Cancel all buffers, returning them to the queue --
1365 // If an error occurred before, also destroy the VkImage and release the
1366 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001367 for (uint32_t i = 0; i < num_images; i++) {
1368 Swapchain::Image& img = swapchain->images[i];
1369 if (img.dequeued) {
1370 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001371 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1372 img.dequeue_fence);
1373 img.dequeue_fence = -1;
1374 img.dequeued = false;
1375 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001376 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001377 }
1378
1379 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001380 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1381 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001382 return result;
1383 }
1384
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001385 if (transform_hint != swapchain->pre_transform) {
1386 // Log that the app is not doing pre-rotation.
1387 android::GraphicsEnv::getInstance().setTargetStats(
1388 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1389 }
1390
Jesse Halldc225072016-05-30 22:40:14 -07001391 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1392 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001393 return VK_SUCCESS;
1394}
1395
Jesse Halle1b12782015-11-30 11:27:32 -08001396VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001397void DestroySwapchainKHR(VkDevice device,
1398 VkSwapchainKHR swapchain_handle,
1399 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001400 ATRACE_CALL();
1401
Yiwei Zhang533cea92019-06-03 18:43:24 -07001402 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001403}
1404
Jesse Halle1b12782015-11-30 11:27:32 -08001405VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001406VkResult GetSwapchainImagesKHR(VkDevice,
1407 VkSwapchainKHR swapchain_handle,
1408 uint32_t* count,
1409 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001410 ATRACE_CALL();
1411
Jesse Halld7b994a2015-09-07 14:17:37 -07001412 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001413 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1414 "getting images for non-active swapchain 0x%" PRIx64
1415 "; only dequeued image handles are valid",
1416 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001417 VkResult result = VK_SUCCESS;
1418 if (images) {
1419 uint32_t n = swapchain.num_images;
1420 if (*count < swapchain.num_images) {
1421 n = *count;
1422 result = VK_INCOMPLETE;
1423 }
1424 for (uint32_t i = 0; i < n; i++)
1425 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001426 *count = n;
1427 } else {
1428 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001429 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001430 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001431}
1432
Jesse Halle1b12782015-11-30 11:27:32 -08001433VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001434VkResult AcquireNextImageKHR(VkDevice device,
1435 VkSwapchainKHR swapchain_handle,
1436 uint64_t timeout,
1437 VkSemaphore semaphore,
1438 VkFence vk_fence,
1439 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001440 ATRACE_CALL();
1441
Jesse Halld7b994a2015-09-07 14:17:37 -07001442 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001443 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001444 VkResult result;
1445 int err;
1446
Jesse Halldc225072016-05-30 22:40:14 -07001447 if (swapchain.surface.swapchain_handle != swapchain_handle)
1448 return VK_ERROR_OUT_OF_DATE_KHR;
1449
Jesse Halld7b994a2015-09-07 14:17:37 -07001450 ALOGW_IF(
1451 timeout != UINT64_MAX,
1452 "vkAcquireNextImageKHR: non-infinite timeouts not yet implemented");
1453
Chris Forbesc88409c2017-03-30 19:47:37 +13001454 if (swapchain.shared) {
1455 // In shared mode, we keep the buffer dequeued all the time, so we don't
1456 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1457 // the semaphore and fence passed to us will be signalled.
1458 *image_index = 0;
1459 result = GetData(device).driver.AcquireImageANDROID(
1460 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1461 return result;
1462 }
1463
Jesse Halld7b994a2015-09-07 14:17:37 -07001464 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001465 int fence_fd;
1466 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001467 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001468 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001469 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001470 }
1471
1472 uint32_t idx;
1473 for (idx = 0; idx < swapchain.num_images; idx++) {
1474 if (swapchain.images[idx].buffer.get() == buffer) {
1475 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001476 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001477 break;
1478 }
1479 }
1480 if (idx == swapchain.num_images) {
1481 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001482 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001483 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001484 }
1485
1486 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001487 if (fence_fd != -1) {
1488 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001489 if (fence_clone == -1) {
1490 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1491 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001492 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001493 }
1494 }
1495
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001496 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001497 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001498 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001499 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1500 // even if the call fails. We could close it ourselves on failure, but
1501 // that would create a race condition if the driver closes it on a
1502 // failure path: some other thread might create an fd with the same
1503 // number between the time the driver closes it and the time we close
1504 // it. We must assume one of: the driver *always* closes it even on
1505 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001506 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001507 swapchain.images[idx].dequeued = false;
1508 swapchain.images[idx].dequeue_fence = -1;
1509 return result;
1510 }
1511
1512 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001513 return VK_SUCCESS;
1514}
1515
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001516VKAPI_ATTR
1517VkResult AcquireNextImage2KHR(VkDevice device,
1518 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1519 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001520 ATRACE_CALL();
1521
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001522 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1523 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1524 pAcquireInfo->fence, pImageIndex);
1525}
1526
Jesse Halldc225072016-05-30 22:40:14 -07001527static VkResult WorstPresentResult(VkResult a, VkResult b) {
1528 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1529 // (in spec version 1.0.14).
1530 static const VkResult kWorstToBest[] = {
1531 VK_ERROR_DEVICE_LOST,
1532 VK_ERROR_SURFACE_LOST_KHR,
1533 VK_ERROR_OUT_OF_DATE_KHR,
1534 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1535 VK_ERROR_OUT_OF_HOST_MEMORY,
1536 VK_SUBOPTIMAL_KHR,
1537 };
1538 for (auto result : kWorstToBest) {
1539 if (a == result || b == result)
1540 return result;
1541 }
1542 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1543 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1544 return a != VK_SUCCESS ? a : b;
1545}
1546
Jesse Halle1b12782015-11-30 11:27:32 -08001547VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001548VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001549 ATRACE_CALL();
1550
Jesse Halld7b994a2015-09-07 14:17:37 -07001551 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1552 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1553 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001554
Jesse Halldc225072016-05-30 22:40:14 -07001555 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001556 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001557 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001558
Ian Elliottcb351132016-12-13 10:30:40 -07001559 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001560 const VkPresentRegionsKHR* present_regions = nullptr;
1561 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001562 const VkPresentRegionsKHR* next =
1563 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1564 while (next) {
1565 switch (next->sType) {
1566 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1567 present_regions = next;
1568 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001569 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001570 present_times =
1571 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1572 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001573 default:
1574 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1575 next->sType);
1576 break;
1577 }
1578 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1579 }
1580 ALOGV_IF(
1581 present_regions &&
1582 present_regions->swapchainCount != present_info->swapchainCount,
1583 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001584 ALOGV_IF(present_times &&
1585 present_times->swapchainCount != present_info->swapchainCount,
1586 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1587 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001588 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001589 (present_regions) ? present_regions->pRegions : nullptr;
1590 const VkPresentTimeGOOGLE* times =
1591 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001592 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001593 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001594 uint32_t nrects = 0;
1595
Jesse Halld7b994a2015-09-07 14:17:37 -07001596 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1597 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001598 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001599 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001600 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001601 const VkPresentRegionKHR* region =
1602 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001603 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001604 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001605 VkResult result;
1606 int err;
1607
Jesse Halld7b994a2015-09-07 14:17:37 -07001608 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001609 result = dispatch.QueueSignalReleaseImageANDROID(
1610 queue, present_info->waitSemaphoreCount,
1611 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001612 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001613 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001614 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001615 }
1616
Jesse Halldc225072016-05-30 22:40:14 -07001617 if (swapchain.surface.swapchain_handle ==
1618 present_info->pSwapchains[sc]) {
1619 ANativeWindow* window = swapchain.surface.window.get();
1620 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001621 if (region) {
1622 // Process the incremental-present hint for this swapchain:
1623 uint32_t rcount = region->rectangleCount;
1624 if (rcount > nrects) {
1625 android_native_rect_t* new_rects =
1626 static_cast<android_native_rect_t*>(
1627 allocator->pfnReallocation(
1628 allocator->pUserData, rects,
1629 sizeof(android_native_rect_t) * rcount,
1630 alignof(android_native_rect_t),
1631 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1632 if (new_rects) {
1633 rects = new_rects;
1634 nrects = rcount;
1635 } else {
1636 rcount = 0; // Ignore the hint for this swapchain
1637 }
1638 }
1639 for (uint32_t r = 0; r < rcount; ++r) {
1640 if (region->pRectangles[r].layer > 0) {
1641 ALOGV(
1642 "vkQueuePresentKHR ignoring invalid layer "
1643 "(%u); using layer 0 instead",
1644 region->pRectangles[r].layer);
1645 }
1646 int x = region->pRectangles[r].offset.x;
1647 int y = region->pRectangles[r].offset.y;
1648 int width = static_cast<int>(
1649 region->pRectangles[r].extent.width);
1650 int height = static_cast<int>(
1651 region->pRectangles[r].extent.height);
1652 android_native_rect_t* cur_rect = &rects[r];
1653 cur_rect->left = x;
1654 cur_rect->top = y + height;
1655 cur_rect->right = x + width;
1656 cur_rect->bottom = y;
1657 }
1658 native_window_set_surface_damage(window, rects, rcount);
1659 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001660 if (time) {
1661 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001662 ALOGV(
1663 "Calling "
1664 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001665 native_window_enable_frame_timestamps(window, true);
1666 swapchain.frame_timestamps_enabled = true;
1667 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001668
1669 // Record the nativeFrameId so it can be later correlated to
1670 // this present.
1671 uint64_t nativeFrameId = 0;
1672 err = native_window_get_next_frame_id(
1673 window, &nativeFrameId);
1674 if (err != android::NO_ERROR) {
1675 ALOGE("Failed to get next native frame ID.");
1676 }
1677
1678 // Add a new timing record with the user's presentID and
1679 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001680 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001681 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001682 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001683 }
1684 if (time->desiredPresentTime) {
1685 // Set the desiredPresentTime:
1686 ALOGV(
1687 "Calling "
1688 "native_window_set_buffers_timestamp(%" PRId64 ")",
1689 time->desiredPresentTime);
1690 native_window_set_buffers_timestamp(
1691 window,
1692 static_cast<int64_t>(time->desiredPresentTime));
1693 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001694 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001695
Jesse Halldc225072016-05-30 22:40:14 -07001696 err = window->queueBuffer(window, img.buffer.get(), fence);
1697 // queueBuffer always closes fence, even on error
1698 if (err != 0) {
Jesse Halldc225072016-05-30 22:40:14 -07001699 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1700 swapchain_result = WorstPresentResult(
1701 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001702 } else {
1703 if (img.dequeue_fence >= 0) {
1704 close(img.dequeue_fence);
1705 img.dequeue_fence = -1;
1706 }
1707 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001708 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001709
1710 // If the swapchain is in shared mode, immediately dequeue the
1711 // buffer so it can be presented again without an intervening
1712 // call to AcquireNextImageKHR. We expect to get the same buffer
1713 // back from every call to dequeueBuffer in this mode.
1714 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1715 ANativeWindowBuffer* buffer;
1716 int fence_fd;
1717 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1718 if (err != 0) {
1719 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1720 swapchain_result = WorstPresentResult(swapchain_result,
1721 VK_ERROR_SURFACE_LOST_KHR);
1722 }
1723 else if (img.buffer != buffer) {
1724 ALOGE("got wrong image back for shared swapchain");
1725 swapchain_result = WorstPresentResult(swapchain_result,
1726 VK_ERROR_SURFACE_LOST_KHR);
1727 }
1728 else {
1729 img.dequeue_fence = fence_fd;
1730 img.dequeued = true;
1731 }
1732 }
Jesse Halldc225072016-05-30 22:40:14 -07001733 }
1734 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001735 OrphanSwapchain(device, &swapchain);
1736 }
silence_dogood73597592019-05-23 16:57:37 -07001737 int window_transform_hint;
1738 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1739 &window_transform_hint);
1740 if (err != 0) {
1741 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1742 strerror(-err), err);
1743 swapchain_result = WorstPresentResult(
1744 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1745 }
1746 if (swapchain.pre_transform != window_transform_hint) {
1747 swapchain_result =
1748 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1749 }
Jesse Halldc225072016-05-30 22:40:14 -07001750 } else {
1751 ReleaseSwapchainImage(device, nullptr, fence, img);
1752 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001753 }
1754
Jesse Halla9e57032015-11-30 01:03:10 -08001755 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001756 present_info->pResults[sc] = swapchain_result;
1757
1758 if (swapchain_result != final_result)
1759 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001760 }
Ian Elliottcb351132016-12-13 10:30:40 -07001761 if (rects) {
1762 allocator->pfnFree(allocator->pUserData, rects);
1763 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001764
1765 return final_result;
1766}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001767
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001768VKAPI_ATTR
1769VkResult GetRefreshCycleDurationGOOGLE(
1770 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001771 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001772 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001773 ATRACE_CALL();
1774
Ian Elliott62c48c92017-01-20 13:13:20 -07001775 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001776 VkResult result = VK_SUCCESS;
1777
Ian Elliott3568bfe2019-05-03 15:54:46 -06001778 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001779
1780 return result;
1781}
1782
1783VKAPI_ATTR
1784VkResult GetPastPresentationTimingGOOGLE(
1785 VkDevice,
1786 VkSwapchainKHR swapchain_handle,
1787 uint32_t* count,
1788 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001789 ATRACE_CALL();
1790
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001791 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001792 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1793 return VK_ERROR_OUT_OF_DATE_KHR;
1794 }
1795
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001796 ANativeWindow* window = swapchain.surface.window.get();
1797 VkResult result = VK_SUCCESS;
1798
1799 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001800 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001801 native_window_enable_frame_timestamps(window, true);
1802 swapchain.frame_timestamps_enabled = true;
1803 }
1804
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001805 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07001806 // Get the latest ready timing count before copying, since the copied
1807 // timing info will be erased in copy_ready_timings function.
1808 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07001809 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001810 // Check the *count here against the recorded ready timing count, since
1811 // *count can be overwritten per spec describes.
1812 if (*count < n) {
1813 result = VK_INCOMPLETE;
1814 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001815 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001816 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001817 }
1818
1819 return result;
1820}
1821
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001822VKAPI_ATTR
1823VkResult GetSwapchainStatusKHR(
1824 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001825 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001826 ATRACE_CALL();
1827
Chris Forbes4e18ba82017-01-20 12:50:17 +13001828 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001829 VkResult result = VK_SUCCESS;
1830
Chris Forbes4e18ba82017-01-20 12:50:17 +13001831 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1832 return VK_ERROR_OUT_OF_DATE_KHR;
1833 }
1834
Yiwei Zhanga885c062019-10-24 12:07:57 -07001835 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001836
1837 return result;
1838}
1839
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001840VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001841 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001842 uint32_t swapchainCount,
1843 const VkSwapchainKHR* pSwapchains,
1844 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001845 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001846
1847 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1848 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1849 if (!swapchain)
1850 continue;
1851
1852 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1853
1854 ANativeWindow* window = swapchain->surface.window.get();
1855
1856 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1857 const android_smpte2086_metadata smpteMetdata = {
1858 {vulkanMetadata.displayPrimaryRed.x,
1859 vulkanMetadata.displayPrimaryRed.y},
1860 {vulkanMetadata.displayPrimaryGreen.x,
1861 vulkanMetadata.displayPrimaryGreen.y},
1862 {vulkanMetadata.displayPrimaryBlue.x,
1863 vulkanMetadata.displayPrimaryBlue.y},
1864 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1865 vulkanMetadata.maxLuminance,
1866 vulkanMetadata.minLuminance};
1867 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1868
1869 const android_cta861_3_metadata cta8613Metadata = {
1870 vulkanMetadata.maxContentLightLevel,
1871 vulkanMetadata.maxFrameAverageLightLevel};
1872 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1873 }
1874
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001875 return;
1876}
1877
Yiwei Zhang0f475222019-04-11 19:38:00 -07001878static void InterceptBindImageMemory2(
1879 uint32_t bind_info_count,
1880 const VkBindImageMemoryInfo* bind_infos,
1881 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1882 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1883 out_native_buffers->clear();
1884 out_bind_infos->clear();
1885
1886 if (!bind_info_count)
1887 return;
1888
1889 std::unordered_set<uint32_t> intercepted_indexes;
1890
1891 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1892 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1893 bind_infos[idx].pNext);
1894 while (info &&
1895 info->sType !=
1896 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1897 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1898 info->pNext);
1899 }
1900
1901 if (!info)
1902 continue;
1903
1904 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1905 "swapchain handle must not be NULL");
1906 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1907 ALOG_ASSERT(
1908 info->imageIndex < swapchain->num_images,
1909 "imageIndex must be less than the number of images in swapchain");
1910
1911 ANativeWindowBuffer* buffer =
1912 swapchain->images[info->imageIndex].buffer.get();
1913 VkNativeBufferANDROID native_buffer = {
1914#pragma clang diagnostic push
1915#pragma clang diagnostic ignored "-Wold-style-cast"
1916 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1917#pragma clang diagnostic pop
1918 .pNext = bind_infos[idx].pNext,
1919 .handle = buffer->handle,
1920 .stride = buffer->stride,
1921 .format = buffer->format,
1922 .usage = int(buffer->usage),
1923 };
1924 // Reserve enough space to avoid letting re-allocation invalidate the
1925 // addresses of the elements inside.
1926 out_native_buffers->reserve(bind_info_count);
1927 out_native_buffers->emplace_back(native_buffer);
1928
1929 // Reserve the space now since we know how much is needed now.
1930 out_bind_infos->reserve(bind_info_count);
1931 out_bind_infos->emplace_back(bind_infos[idx]);
1932 out_bind_infos->back().pNext = &out_native_buffers->back();
1933
1934 intercepted_indexes.insert(idx);
1935 }
1936
1937 if (intercepted_indexes.empty())
1938 return;
1939
1940 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1941 if (intercepted_indexes.count(idx))
1942 continue;
1943 out_bind_infos->emplace_back(bind_infos[idx]);
1944 }
1945}
1946
Yiwei Zhang23143102019-04-10 18:24:05 -07001947VKAPI_ATTR
1948VkResult BindImageMemory2(VkDevice device,
1949 uint32_t bindInfoCount,
1950 const VkBindImageMemoryInfo* pBindInfos) {
1951 ATRACE_CALL();
1952
Yiwei Zhang0f475222019-04-11 19:38:00 -07001953 // out_native_buffers is for maintaining the lifecycle of the constructed
1954 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1955 std::vector<VkNativeBufferANDROID> out_native_buffers;
1956 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1957 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1958 &out_bind_infos);
1959 return GetData(device).driver.BindImageMemory2(
1960 device, bindInfoCount,
1961 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001962}
1963
1964VKAPI_ATTR
1965VkResult BindImageMemory2KHR(VkDevice device,
1966 uint32_t bindInfoCount,
1967 const VkBindImageMemoryInfo* pBindInfos) {
1968 ATRACE_CALL();
1969
Yiwei Zhang0f475222019-04-11 19:38:00 -07001970 std::vector<VkNativeBufferANDROID> out_native_buffers;
1971 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1972 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1973 &out_bind_infos);
1974 return GetData(device).driver.BindImageMemory2KHR(
1975 device, bindInfoCount,
1976 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001977}
1978
Chia-I Wu62262232016-03-26 07:06:44 +08001979} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001980} // namespace vulkan