blob: 2dd88782b456c6395845e41c8d783050df3ecd7f [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 Zhang705c2e62019-12-18 23:12:43 -080027#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080028#include <utils/Trace.h>
Yiwei Zhang0f475222019-04-11 19:38:00 -070029
30#include <algorithm>
31#include <unordered_set>
32#include <vector>
Jesse Halld7b994a2015-09-07 14:17:37 -070033
Chia-I Wu4a6a9162016-03-26 07:17:34 +080034#include "driver.h"
Jesse Halld7b994a2015-09-07 14:17:37 -070035
Daniel Kochf25f5bb2017-10-05 00:26:58 -040036using android::hardware::graphics::common::V1_0::BufferUsage;
37
Chia-I Wu62262232016-03-26 07:06:44 +080038namespace vulkan {
39namespace driver {
Jesse Hall5ae3abb2015-10-08 14:00:22 -070040
Jesse Halld7b994a2015-09-07 14:17:37 -070041namespace {
42
Jesse Hall55bc0972016-02-23 16:43:29 -080043const VkSurfaceTransformFlagsKHR kSupportedTransforms =
44 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR |
45 VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR |
46 VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR |
47 VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR |
Yiwei Zhang70a21962019-05-31 17:26:52 -070048 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR |
49 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR |
50 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR |
51 VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR |
Jesse Hall55bc0972016-02-23 16:43:29 -080052 VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
53
54VkSurfaceTransformFlagBitsKHR TranslateNativeToVulkanTransform(int native) {
55 // Native and Vulkan transforms are isomorphic, but are represented
56 // differently. Vulkan transforms are built up of an optional horizontal
57 // mirror, followed by a clockwise 0/90/180/270-degree rotation. Native
58 // transforms are built up from a horizontal flip, vertical flip, and
59 // 90-degree rotation, all optional but always in that order.
60
Jesse Hall55bc0972016-02-23 16:43:29 -080061 switch (native) {
Yiwei Zhang70a21962019-05-31 17:26:52 -070062 case 0:
Jesse Hall55bc0972016-02-23 16:43:29 -080063 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070064 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
65 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR;
66 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
67 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR;
68 case NATIVE_WINDOW_TRANSFORM_ROT_180:
Jesse Hall55bc0972016-02-23 16:43:29 -080069 return VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070070 case NATIVE_WINDOW_TRANSFORM_ROT_90:
Jesse Hall55bc0972016-02-23 16:43:29 -080071 return VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR;
Yiwei Zhang70a21962019-05-31 17:26:52 -070072 case NATIVE_WINDOW_TRANSFORM_FLIP_H | NATIVE_WINDOW_TRANSFORM_ROT_90:
73 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR;
74 case NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_ROT_90:
75 return VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR;
76 case NATIVE_WINDOW_TRANSFORM_ROT_270:
Jesse Hall55bc0972016-02-23 16:43:29 -080077 return VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR;
78 case NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
79 default:
80 return VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
81 }
82}
83
Yiwei Zhang70a21962019-05-31 17:26:52 -070084int TranslateVulkanToNativeTransform(VkSurfaceTransformFlagBitsKHR transform) {
85 switch (transform) {
86 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
87 return NATIVE_WINDOW_TRANSFORM_ROT_90;
88 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
89 return NATIVE_WINDOW_TRANSFORM_ROT_180;
90 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
91 return NATIVE_WINDOW_TRANSFORM_ROT_270;
92 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
93 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
94 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
95 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
96 NATIVE_WINDOW_TRANSFORM_ROT_90;
97 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
98 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
99 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
100 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
101 NATIVE_WINDOW_TRANSFORM_ROT_90;
102 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
103 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
104 default:
105 return 0;
106 }
107}
108
Jesse Hall178b6962016-02-24 15:39:50 -0800109int InvertTransformToNative(VkSurfaceTransformFlagBitsKHR transform) {
110 switch (transform) {
111 case VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR:
112 return NATIVE_WINDOW_TRANSFORM_ROT_270;
113 case VK_SURFACE_TRANSFORM_ROTATE_180_BIT_KHR:
114 return NATIVE_WINDOW_TRANSFORM_ROT_180;
115 case VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR:
116 return NATIVE_WINDOW_TRANSFORM_ROT_90;
Yiwei Zhang70a21962019-05-31 17:26:52 -0700117 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_BIT_KHR:
118 return NATIVE_WINDOW_TRANSFORM_FLIP_H;
119 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_90_BIT_KHR:
120 return NATIVE_WINDOW_TRANSFORM_FLIP_H |
121 NATIVE_WINDOW_TRANSFORM_ROT_90;
122 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_180_BIT_KHR:
123 return NATIVE_WINDOW_TRANSFORM_FLIP_V;
124 case VK_SURFACE_TRANSFORM_HORIZONTAL_MIRROR_ROTATE_270_BIT_KHR:
125 return NATIVE_WINDOW_TRANSFORM_FLIP_V |
126 NATIVE_WINDOW_TRANSFORM_ROT_90;
Jesse Hall178b6962016-02-24 15:39:50 -0800127 case VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR:
128 case VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR:
129 default:
130 return 0;
131 }
132}
133
Ian Elliott8a977262017-01-19 09:05:58 -0700134class TimingInfo {
135 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800136 TimingInfo(const VkPresentTimeGOOGLE* qp, uint64_t nativeFrameId)
Ian Elliott2c6355d2017-01-19 11:02:13 -0700137 : vals_{qp->presentID, qp->desiredPresentTime, 0, 0, 0},
Brian Anderson1049d1d2016-12-16 17:25:57 -0800138 native_frame_id_(nativeFrameId) {}
139 bool ready() const {
Brian Andersondc96fdf2017-03-20 16:54:25 -0700140 return (timestamp_desired_present_time_ !=
141 NATIVE_WINDOW_TIMESTAMP_PENDING &&
142 timestamp_actual_present_time_ !=
143 NATIVE_WINDOW_TIMESTAMP_PENDING &&
144 timestamp_render_complete_time_ !=
145 NATIVE_WINDOW_TIMESTAMP_PENDING &&
146 timestamp_composition_latch_time_ !=
147 NATIVE_WINDOW_TIMESTAMP_PENDING);
Ian Elliott8a977262017-01-19 09:05:58 -0700148 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700149 void calculate(int64_t rdur) {
150 bool anyTimestampInvalid =
151 (timestamp_actual_present_time_ ==
152 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
153 (timestamp_render_complete_time_ ==
154 NATIVE_WINDOW_TIMESTAMP_INVALID) ||
155 (timestamp_composition_latch_time_ ==
156 NATIVE_WINDOW_TIMESTAMP_INVALID);
157 if (anyTimestampInvalid) {
158 ALOGE("Unexpectedly received invalid timestamp.");
159 vals_.actualPresentTime = 0;
160 vals_.earliestPresentTime = 0;
161 vals_.presentMargin = 0;
162 return;
163 }
164
165 vals_.actualPresentTime =
166 static_cast<uint64_t>(timestamp_actual_present_time_);
167 int64_t margin = (timestamp_composition_latch_time_ -
Ian Elliott8a977262017-01-19 09:05:58 -0700168 timestamp_render_complete_time_);
169 // Calculate vals_.earliestPresentTime, and potentially adjust
170 // vals_.presentMargin. The initial value of vals_.earliestPresentTime
171 // is vals_.actualPresentTime. If we can subtract rdur (the duration
172 // of a refresh cycle) from vals_.earliestPresentTime (and also from
173 // vals_.presentMargin) and still leave a positive margin, then we can
174 // report to the application that it could have presented earlier than
175 // it did (per the extension specification). If for some reason, we
176 // can do this subtraction repeatedly, we do, since
177 // vals_.earliestPresentTime really is supposed to be the "earliest".
Brian Andersondc96fdf2017-03-20 16:54:25 -0700178 int64_t early_time = timestamp_actual_present_time_;
Ian Elliott8a977262017-01-19 09:05:58 -0700179 while ((margin > rdur) &&
180 ((early_time - rdur) > timestamp_composition_latch_time_)) {
181 early_time -= rdur;
182 margin -= rdur;
183 }
Brian Andersondc96fdf2017-03-20 16:54:25 -0700184 vals_.earliestPresentTime = static_cast<uint64_t>(early_time);
185 vals_.presentMargin = static_cast<uint64_t>(margin);
Ian Elliott8a977262017-01-19 09:05:58 -0700186 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800187 void get_values(VkPastPresentationTimingGOOGLE* values) const {
188 *values = vals_;
189 }
Ian Elliott8a977262017-01-19 09:05:58 -0700190
191 public:
Brian Anderson1049d1d2016-12-16 17:25:57 -0800192 VkPastPresentationTimingGOOGLE vals_ { 0, 0, 0, 0, 0 };
Ian Elliott8a977262017-01-19 09:05:58 -0700193
Brian Anderson1049d1d2016-12-16 17:25:57 -0800194 uint64_t native_frame_id_ { 0 };
Brian Andersondc96fdf2017-03-20 16:54:25 -0700195 int64_t timestamp_desired_present_time_{ NATIVE_WINDOW_TIMESTAMP_PENDING };
196 int64_t timestamp_actual_present_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
197 int64_t timestamp_render_complete_time_ { NATIVE_WINDOW_TIMESTAMP_PENDING };
198 int64_t timestamp_composition_latch_time_
199 { NATIVE_WINDOW_TIMESTAMP_PENDING };
Ian Elliott8a977262017-01-19 09:05:58 -0700200};
201
Jesse Hall1356b0d2015-11-23 17:24:58 -0800202struct Surface {
Chia-I Wue8e689f2016-04-18 08:21:31 +0800203 android::sp<ANativeWindow> window;
Jesse Halldc225072016-05-30 22:40:14 -0700204 VkSwapchainKHR swapchain_handle;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700205 uint64_t consumer_usage;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800206};
207
208VkSurfaceKHR HandleFromSurface(Surface* surface) {
209 return VkSurfaceKHR(reinterpret_cast<uint64_t>(surface));
210}
211
212Surface* SurfaceFromHandle(VkSurfaceKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800213 return reinterpret_cast<Surface*>(handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800214}
215
Ian Elliott8a977262017-01-19 09:05:58 -0700216// Maximum number of TimingInfo structs to keep per swapchain:
217enum { MAX_TIMING_INFOS = 10 };
218// Minimum number of frames to look for in the past (so we don't cause
219// syncronous requests to Surface Flinger):
220enum { MIN_NUM_FRAMES_AGO = 5 };
221
Jesse Hall1356b0d2015-11-23 17:24:58 -0800222struct Swapchain {
Ian Elliottffedb652017-02-14 10:58:30 -0700223 Swapchain(Surface& surface_,
224 uint32_t num_images_,
silence_dogood73597592019-05-23 16:57:37 -0700225 VkPresentModeKHR present_mode,
226 int pre_transform_)
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700227 : surface(surface_),
228 num_images(num_images_),
Ian Elliottffedb652017-02-14 10:58:30 -0700229 mailbox_mode(present_mode == VK_PRESENT_MODE_MAILBOX_KHR),
silence_dogood73597592019-05-23 16:57:37 -0700230 pre_transform(pre_transform_),
Chris Forbesf8835642017-03-30 19:31:40 +1300231 frame_timestamps_enabled(false),
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800232 acquire_next_image_timeout(-1),
Chris Forbesf8835642017-03-30 19:31:40 +1300233 shared(present_mode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800234 present_mode ==
235 VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
Ian Elliott62c48c92017-01-20 13:13:20 -0700236 ANativeWindow* window = surface.window.get();
Ian Elliottbe833a22017-01-25 13:09:20 -0700237 native_window_get_refresh_cycle_duration(
Ian Elliott62c48c92017-01-20 13:13:20 -0700238 window,
Brian Andersondc96fdf2017-03-20 16:54:25 -0700239 &refresh_duration);
Ian Elliott8a977262017-01-19 09:05:58 -0700240 }
Ian Elliott3568bfe2019-05-03 15:54:46 -0600241 uint64_t get_refresh_duration()
242 {
243 ANativeWindow* window = surface.window.get();
244 native_window_get_refresh_cycle_duration(
245 window,
246 &refresh_duration);
247 return static_cast<uint64_t>(refresh_duration);
248
249 }
Jesse Hall1356b0d2015-11-23 17:24:58 -0800250
251 Surface& surface;
Jesse Halld7b994a2015-09-07 14:17:37 -0700252 uint32_t num_images;
Ian Elliottffedb652017-02-14 10:58:30 -0700253 bool mailbox_mode;
silence_dogood73597592019-05-23 16:57:37 -0700254 int pre_transform;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -0700255 bool frame_timestamps_enabled;
Brian Andersondc96fdf2017-03-20 16:54:25 -0700256 int64_t refresh_duration;
Yiwei Zhang705c2e62019-12-18 23:12:43 -0800257 nsecs_t acquire_next_image_timeout;
Chris Forbesf8835642017-03-30 19:31:40 +1300258 bool shared;
Jesse Halld7b994a2015-09-07 14:17:37 -0700259
260 struct Image {
261 Image() : image(VK_NULL_HANDLE), dequeue_fence(-1), dequeued(false) {}
262 VkImage image;
Chia-I Wue8e689f2016-04-18 08:21:31 +0800263 android::sp<ANativeWindowBuffer> buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -0700264 // The fence is only valid when the buffer is dequeued, and should be
265 // -1 any other time. When valid, we own the fd, and must ensure it is
266 // closed: either by closing it explicitly when queueing the buffer,
267 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
268 int dequeue_fence;
269 bool dequeued;
Pawin Vongmasa6e1193a2017-03-07 13:08:40 -0800270 } images[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Ian Elliott8a977262017-01-19 09:05:58 -0700271
Yiwei Zhang5e862202019-06-21 14:59:16 -0700272 std::vector<TimingInfo> timing;
Jesse Halld7b994a2015-09-07 14:17:37 -0700273};
274
275VkSwapchainKHR HandleFromSwapchain(Swapchain* swapchain) {
276 return VkSwapchainKHR(reinterpret_cast<uint64_t>(swapchain));
277}
278
279Swapchain* SwapchainFromHandle(VkSwapchainKHR handle) {
Jesse Halla3a7a1d2015-11-24 11:37:23 -0800280 return reinterpret_cast<Swapchain*>(handle);
Jesse Halld7b994a2015-09-07 14:17:37 -0700281}
282
Jesse Halldc225072016-05-30 22:40:14 -0700283void ReleaseSwapchainImage(VkDevice device,
284 ANativeWindow* window,
285 int release_fence,
286 Swapchain::Image& image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700287 ATRACE_CALL();
288
Jesse Halldc225072016-05-30 22:40:14 -0700289 ALOG_ASSERT(release_fence == -1 || image.dequeued,
290 "ReleaseSwapchainImage: can't provide a release fence for "
291 "non-dequeued images");
292
293 if (image.dequeued) {
294 if (release_fence >= 0) {
295 // We get here from vkQueuePresentKHR. The application is
296 // responsible for creating an execution dependency chain from
297 // vkAcquireNextImage (dequeue_fence) to vkQueuePresentKHR
298 // (release_fence), so we can drop the dequeue_fence here.
299 if (image.dequeue_fence >= 0)
300 close(image.dequeue_fence);
301 } else {
302 // We get here during swapchain destruction, or various serious
303 // error cases e.g. when we can't create the release_fence during
304 // vkQueuePresentKHR. In non-error cases, the dequeue_fence should
305 // have already signalled, since the swapchain images are supposed
306 // to be idle before the swapchain is destroyed. In error cases,
307 // there may be rendering in flight to the image, but since we
308 // weren't able to create a release_fence, waiting for the
309 // dequeue_fence is about the best we can do.
310 release_fence = image.dequeue_fence;
311 }
312 image.dequeue_fence = -1;
313
314 if (window) {
315 window->cancelBuffer(window, image.buffer.get(), release_fence);
316 } else {
317 if (release_fence >= 0) {
318 sync_wait(release_fence, -1 /* forever */);
319 close(release_fence);
320 }
321 }
322
323 image.dequeued = false;
324 }
325
326 if (image.image) {
Yiwei Zhang533cea92019-06-03 18:43:24 -0700327 ATRACE_BEGIN("DestroyImage");
Jesse Halldc225072016-05-30 22:40:14 -0700328 GetData(device).driver.DestroyImage(device, image.image, nullptr);
Yiwei Zhang533cea92019-06-03 18:43:24 -0700329 ATRACE_END();
Jesse Halldc225072016-05-30 22:40:14 -0700330 image.image = VK_NULL_HANDLE;
331 }
332
333 image.buffer.clear();
334}
335
336void OrphanSwapchain(VkDevice device, Swapchain* swapchain) {
337 if (swapchain->surface.swapchain_handle != HandleFromSwapchain(swapchain))
338 return;
Jesse Halldc225072016-05-30 22:40:14 -0700339 for (uint32_t i = 0; i < swapchain->num_images; i++) {
340 if (!swapchain->images[i].dequeued)
341 ReleaseSwapchainImage(device, nullptr, -1, swapchain->images[i]);
342 }
343 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
Ian Elliott8a977262017-01-19 09:05:58 -0700344 swapchain->timing.clear();
345}
346
347uint32_t get_num_ready_timings(Swapchain& swapchain) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800348 if (swapchain.timing.size() < MIN_NUM_FRAMES_AGO) {
349 return 0;
350 }
Ian Elliott8a977262017-01-19 09:05:58 -0700351
Brian Anderson1049d1d2016-12-16 17:25:57 -0800352 uint32_t num_ready = 0;
353 const size_t num_timings = swapchain.timing.size() - MIN_NUM_FRAMES_AGO + 1;
354 for (uint32_t i = 0; i < num_timings; i++) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700355 TimingInfo& ti = swapchain.timing[i];
Brian Anderson1049d1d2016-12-16 17:25:57 -0800356 if (ti.ready()) {
357 // This TimingInfo is ready to be reported to the user. Add it
358 // to the num_ready.
359 num_ready++;
360 continue;
361 }
362 // This TimingInfo is not yet ready to be reported to the user,
363 // and so we should look for any available timestamps that
364 // might make it ready.
365 int64_t desired_present_time = 0;
366 int64_t render_complete_time = 0;
367 int64_t composition_latch_time = 0;
368 int64_t actual_present_time = 0;
369 // Obtain timestamps:
370 int ret = native_window_get_frame_timestamps(
371 swapchain.surface.window.get(), ti.native_frame_id_,
372 &desired_present_time, &render_complete_time,
373 &composition_latch_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700374 nullptr, //&first_composition_start_time,
375 nullptr, //&last_composition_start_time,
376 nullptr, //&composition_finish_time,
Brian Anderson1049d1d2016-12-16 17:25:57 -0800377 &actual_present_time,
Yi Kongbcbc73a2018-07-18 10:13:04 -0700378 nullptr, //&dequeue_ready_time,
379 nullptr /*&reads_done_time*/);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800380
381 if (ret != android::NO_ERROR) {
382 continue;
383 }
384
385 // Record the timestamp(s) we received, and then see if this TimingInfo
386 // is ready to be reported to the user:
Brian Andersondc96fdf2017-03-20 16:54:25 -0700387 ti.timestamp_desired_present_time_ = desired_present_time;
388 ti.timestamp_actual_present_time_ = actual_present_time;
389 ti.timestamp_render_complete_time_ = render_complete_time;
390 ti.timestamp_composition_latch_time_ = composition_latch_time;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800391
392 if (ti.ready()) {
393 // The TimingInfo has received enough timestamps, and should now
394 // use those timestamps to calculate the info that should be
395 // reported to the user:
396 ti.calculate(swapchain.refresh_duration);
397 num_ready++;
Ian Elliott8a977262017-01-19 09:05:58 -0700398 }
399 }
400 return num_ready;
401}
402
Ian Elliott8a977262017-01-19 09:05:58 -0700403void copy_ready_timings(Swapchain& swapchain,
404 uint32_t* count,
405 VkPastPresentationTimingGOOGLE* timings) {
Brian Anderson1049d1d2016-12-16 17:25:57 -0800406 if (swapchain.timing.empty()) {
407 *count = 0;
408 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700409 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800410
411 size_t last_ready = swapchain.timing.size() - 1;
412 while (!swapchain.timing[last_ready].ready()) {
413 if (last_ready == 0) {
414 *count = 0;
415 return;
Ian Elliott8a977262017-01-19 09:05:58 -0700416 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800417 last_ready--;
Ian Elliott8a977262017-01-19 09:05:58 -0700418 }
Brian Anderson1049d1d2016-12-16 17:25:57 -0800419
420 uint32_t num_copied = 0;
Yiwei Zhang5e862202019-06-21 14:59:16 -0700421 int32_t num_to_remove = 0;
Brian Anderson1049d1d2016-12-16 17:25:57 -0800422 for (uint32_t i = 0; i <= last_ready && num_copied < *count; i++) {
423 const TimingInfo& ti = swapchain.timing[i];
424 if (ti.ready()) {
425 ti.get_values(&timings[num_copied]);
426 num_copied++;
427 }
428 num_to_remove++;
429 }
430
431 // Discard old frames that aren't ready if newer frames are ready.
432 // We don't expect to get the timing info for those old frames.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700433 swapchain.timing.erase(swapchain.timing.begin(),
434 swapchain.timing.begin() + num_to_remove);
Brian Anderson1049d1d2016-12-16 17:25:57 -0800435
Ian Elliott8a977262017-01-19 09:05:58 -0700436 *count = num_copied;
Jesse Halldc225072016-05-30 22:40:14 -0700437}
438
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700439android_pixel_format GetNativePixelFormat(VkFormat format) {
440 android_pixel_format native_format = HAL_PIXEL_FORMAT_RGBA_8888;
441 switch (format) {
442 case VK_FORMAT_R8G8B8A8_UNORM:
443 case VK_FORMAT_R8G8B8A8_SRGB:
444 native_format = HAL_PIXEL_FORMAT_RGBA_8888;
445 break;
446 case VK_FORMAT_R5G6B5_UNORM_PACK16:
447 native_format = HAL_PIXEL_FORMAT_RGB_565;
448 break;
449 case VK_FORMAT_R16G16B16A16_SFLOAT:
450 native_format = HAL_PIXEL_FORMAT_RGBA_FP16;
451 break;
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800452 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700453 native_format = HAL_PIXEL_FORMAT_RGBA_1010102;
454 break;
455 default:
456 ALOGV("unsupported swapchain format %d", format);
457 break;
458 }
459 return native_format;
460}
461
462android_dataspace GetNativeDataspace(VkColorSpaceKHR colorspace) {
463 switch (colorspace) {
464 case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
465 return HAL_DATASPACE_V0_SRGB;
466 case VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT:
467 return HAL_DATASPACE_DISPLAY_P3;
468 case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
469 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchterb52abee2017-08-07 17:13:04 -0600470 case VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT:
471 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700472 case VK_COLOR_SPACE_DCI_P3_LINEAR_EXT:
473 return HAL_DATASPACE_DCI_P3_LINEAR;
474 case VK_COLOR_SPACE_DCI_P3_NONLINEAR_EXT:
475 return HAL_DATASPACE_DCI_P3;
476 case VK_COLOR_SPACE_BT709_LINEAR_EXT:
477 return HAL_DATASPACE_V0_SRGB_LINEAR;
478 case VK_COLOR_SPACE_BT709_NONLINEAR_EXT:
479 return HAL_DATASPACE_V0_SRGB;
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600480 case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
481 return HAL_DATASPACE_BT2020_LINEAR;
482 case VK_COLOR_SPACE_HDR10_ST2084_EXT:
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700483 return static_cast<android_dataspace>(
484 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
485 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchterc45673f2017-03-13 15:58:15 -0600486 case VK_COLOR_SPACE_DOLBYVISION_EXT:
487 return static_cast<android_dataspace>(
488 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_ST2084 |
489 HAL_DATASPACE_RANGE_FULL);
490 case VK_COLOR_SPACE_HDR10_HLG_EXT:
491 return static_cast<android_dataspace>(
492 HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_TRANSFER_HLG |
493 HAL_DATASPACE_RANGE_FULL);
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -0700494 case VK_COLOR_SPACE_ADOBERGB_LINEAR_EXT:
495 return static_cast<android_dataspace>(
496 HAL_DATASPACE_STANDARD_ADOBE_RGB |
497 HAL_DATASPACE_TRANSFER_LINEAR | HAL_DATASPACE_RANGE_FULL);
498 case VK_COLOR_SPACE_ADOBERGB_NONLINEAR_EXT:
499 return HAL_DATASPACE_ADOBE_RGB;
500
501 // Pass through is intended to allow app to provide data that is passed
502 // to the display system without modification.
503 case VK_COLOR_SPACE_PASS_THROUGH_EXT:
504 return HAL_DATASPACE_ARBITRARY;
505
506 default:
507 // This indicates that we don't know about the
508 // dataspace specified and we should indicate that
509 // it's unsupported
510 return HAL_DATASPACE_UNKNOWN;
511 }
512}
513
Jesse Halld7b994a2015-09-07 14:17:37 -0700514} // anonymous namespace
Jesse Hallb1352bc2015-09-04 16:12:33 -0700515
Jesse Halle1b12782015-11-30 11:27:32 -0800516VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800517VkResult CreateAndroidSurfaceKHR(
Jesse Hallf9fa9a52016-01-08 16:08:51 -0800518 VkInstance instance,
519 const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
520 const VkAllocationCallbacks* allocator,
521 VkSurfaceKHR* out_surface) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800522 ATRACE_CALL();
523
Jesse Hall1f91d392015-12-11 16:28:44 -0800524 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800525 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800526 void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Surface),
527 alignof(Surface),
528 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800529 if (!mem)
530 return VK_ERROR_OUT_OF_HOST_MEMORY;
531 Surface* surface = new (mem) Surface;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700532
Chia-I Wue8e689f2016-04-18 08:21:31 +0800533 surface->window = pCreateInfo->window;
Jesse Halldc225072016-05-30 22:40:14 -0700534 surface->swapchain_handle = VK_NULL_HANDLE;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700535 int err = native_window_get_consumer_usage(surface->window.get(),
536 &surface->consumer_usage);
537 if (err != android::NO_ERROR) {
538 ALOGE("native_window_get_consumer_usage() failed: %s (%d)",
539 strerror(-err), err);
540 surface->~Surface();
541 allocator->pfnFree(allocator->pUserData, surface);
Yiwei Zhang70a21962019-05-31 17:26:52 -0700542 return VK_ERROR_SURFACE_LOST_KHR;
Yiwei Zhang6435b322018-05-08 11:12:17 -0700543 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700544
Yiwei Zhang6435b322018-05-08 11:12:17 -0700545 err =
Jesse Hall1356b0d2015-11-23 17:24:58 -0800546 native_window_api_connect(surface->window.get(), NATIVE_WINDOW_API_EGL);
547 if (err != 0) {
Jesse Hall1356b0d2015-11-23 17:24:58 -0800548 ALOGE("native_window_api_connect() failed: %s (%d)", strerror(-err),
549 err);
550 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800551 allocator->pfnFree(allocator->pUserData, surface);
Mike Stroyan762c8132017-02-22 11:43:09 -0700552 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800553 }
Jesse Hallb1352bc2015-09-04 16:12:33 -0700554
Jesse Hall1356b0d2015-11-23 17:24:58 -0800555 *out_surface = HandleFromSurface(surface);
Jesse Hallb1352bc2015-09-04 16:12:33 -0700556 return VK_SUCCESS;
557}
558
Jesse Halle1b12782015-11-30 11:27:32 -0800559VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800560void DestroySurfaceKHR(VkInstance instance,
561 VkSurfaceKHR surface_handle,
562 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800563 ATRACE_CALL();
564
Jesse Hall1356b0d2015-11-23 17:24:58 -0800565 Surface* surface = SurfaceFromHandle(surface_handle);
566 if (!surface)
567 return;
568 native_window_api_disconnect(surface->window.get(), NATIVE_WINDOW_API_EGL);
Jesse Hall42a9eec2016-06-03 12:39:49 -0700569 ALOGV_IF(surface->swapchain_handle != VK_NULL_HANDLE,
Jesse Halldc225072016-05-30 22:40:14 -0700570 "destroyed VkSurfaceKHR 0x%" PRIx64
571 " has active VkSwapchainKHR 0x%" PRIx64,
572 reinterpret_cast<uint64_t>(surface_handle),
573 reinterpret_cast<uint64_t>(surface->swapchain_handle));
Jesse Hall1356b0d2015-11-23 17:24:58 -0800574 surface->~Surface();
Jesse Hall1f91d392015-12-11 16:28:44 -0800575 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +0800576 allocator = &GetData(instance).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -0800577 allocator->pfnFree(allocator->pUserData, surface);
Jesse Hall1356b0d2015-11-23 17:24:58 -0800578}
579
Jesse Halle1b12782015-11-30 11:27:32 -0800580VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800581VkResult GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice /*pdev*/,
582 uint32_t /*queue_family*/,
Yiwei Zhang6435b322018-05-08 11:12:17 -0700583 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800584 VkBool32* supported) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800585 ATRACE_CALL();
586
Yiwei Zhang6435b322018-05-08 11:12:17 -0700587 const Surface* surface = SurfaceFromHandle(surface_handle);
588 if (!surface) {
589 return VK_ERROR_SURFACE_LOST_KHR;
590 }
591 const ANativeWindow* window = surface->window.get();
592
593 int query_value;
594 int err = window->query(window, NATIVE_WINDOW_FORMAT, &query_value);
595 if (err != 0 || query_value < 0) {
596 ALOGE("NATIVE_WINDOW_FORMAT query failed: %s (%d) value=%d",
597 strerror(-err), err, query_value);
598 return VK_ERROR_SURFACE_LOST_KHR;
599 }
600
601 android_pixel_format native_format =
602 static_cast<android_pixel_format>(query_value);
603
604 bool format_supported = false;
605 switch (native_format) {
606 case HAL_PIXEL_FORMAT_RGBA_8888:
607 case HAL_PIXEL_FORMAT_RGB_565:
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700608 case HAL_PIXEL_FORMAT_RGBA_FP16:
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800609 case HAL_PIXEL_FORMAT_RGBA_1010102:
Yiwei Zhang6435b322018-05-08 11:12:17 -0700610 format_supported = true;
611 break;
612 default:
613 break;
614 }
615
Yiwei Zhangc91b9b72018-06-07 11:13:27 -0700616 *supported = static_cast<VkBool32>(
617 format_supported || (surface->consumer_usage &
618 (AHARDWAREBUFFER_USAGE_CPU_READ_MASK |
619 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK)) == 0);
Yiwei Zhang6435b322018-05-08 11:12:17 -0700620
Jesse Halla6429252015-11-29 18:59:42 -0800621 return VK_SUCCESS;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800622}
623
Jesse Halle1b12782015-11-30 11:27:32 -0800624VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +0800625VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR(
Jesse Hallb00daad2015-11-29 19:46:20 -0800626 VkPhysicalDevice /*pdev*/,
627 VkSurfaceKHR surface,
628 VkSurfaceCapabilitiesKHR* capabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800629 ATRACE_CALL();
630
Jesse Halld7b994a2015-09-07 14:17:37 -0700631 int err;
Jesse Hall1356b0d2015-11-23 17:24:58 -0800632 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -0700633
634 int width, height;
635 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
636 if (err != 0) {
637 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
638 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700639 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700640 }
641 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
642 if (err != 0) {
643 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
644 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700645 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700646 }
647
Jesse Hall55bc0972016-02-23 16:43:29 -0800648 int transform_hint;
649 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
650 if (err != 0) {
651 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
652 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -0700653 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall55bc0972016-02-23 16:43:29 -0800654 }
655
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800656 int max_buffer_count;
657 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &max_buffer_count);
658 if (err != 0) {
659 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d)",
660 strerror(-err), err);
661 return VK_ERROR_SURFACE_LOST_KHR;
662 }
Yiwei Zhang8e951d52018-04-12 13:19:46 -0700663 capabilities->minImageCount = max_buffer_count == 1 ? 1 : 2;
Yiwei Zhangdbd96152018-02-08 14:22:53 -0800664 capabilities->maxImageCount = static_cast<uint32_t>(max_buffer_count);
Jesse Halld7b994a2015-09-07 14:17:37 -0700665
Jesse Hallfe2662d2016-02-09 13:26:59 -0800666 capabilities->currentExtent =
667 VkExtent2D{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
668
Yiwei Zhang70a21962019-05-31 17:26:52 -0700669 // TODO(http://b/134182502): Figure out what the max extent should be.
Jesse Hallb00daad2015-11-29 19:46:20 -0800670 capabilities->minImageExtent = VkExtent2D{1, 1};
671 capabilities->maxImageExtent = VkExtent2D{4096, 4096};
Jesse Halld7b994a2015-09-07 14:17:37 -0700672
Jesse Hallfe2662d2016-02-09 13:26:59 -0800673 capabilities->maxImageArrayLayers = 1;
674
Jesse Hall55bc0972016-02-23 16:43:29 -0800675 capabilities->supportedTransforms = kSupportedTransforms;
676 capabilities->currentTransform =
677 TranslateNativeToVulkanTransform(transform_hint);
Jesse Halld7b994a2015-09-07 14:17:37 -0700678
Jesse Hallfe2662d2016-02-09 13:26:59 -0800679 // On Android, window composition is a WindowManager property, not something
680 // associated with the bufferqueue. It can't be changed from here.
681 capabilities->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -0700682
Jesse Hallb00daad2015-11-29 19:46:20 -0800683 capabilities->supportedUsageFlags =
Jesse Hall3fbc8562015-11-29 22:10:52 -0800684 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
685 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
686 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
Jesse Halld7b994a2015-09-07 14:17:37 -0700687 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
688
Jesse Hallb1352bc2015-09-04 16:12:33 -0700689 return VK_SUCCESS;
690}
691
Jesse Halle1b12782015-11-30 11:27:32 -0800692VKAPI_ATTR
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700693VkResult GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice pdev,
694 VkSurfaceKHR surface_handle,
Chia-I Wu62262232016-03-26 07:06:44 +0800695 uint32_t* count,
696 VkSurfaceFormatKHR* formats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800697 ATRACE_CALL();
698
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700699 const InstanceData& instance_data = GetData(pdev);
700
Yiwei Zhanga885c062019-10-24 12:07:57 -0700701 // TODO(b/143296550): Fill out the set of supported formats. Longer term,
702 // add a new gralloc method to query whether a (format, usage) pair is
Jesse Hall1356b0d2015-11-23 17:24:58 -0800703 // supported, and check that for each gralloc format that corresponds to a
704 // Vulkan format. Shorter term, just add a few more formats to the ones
705 // hardcoded below.
Jesse Halld7b994a2015-09-07 14:17:37 -0700706
707 const VkSurfaceFormatKHR kFormats[] = {
Jesse Hall26763382016-05-20 07:13:52 -0700708 {VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
709 {VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
710 {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
Jesse Halld7b994a2015-09-07 14:17:37 -0700711 };
712 const uint32_t kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700713 uint32_t total_num_formats = kNumFormats;
714
715 bool wide_color_support = false;
716 Surface& surface = *SurfaceFromHandle(surface_handle);
717 int err = native_window_get_wide_color_support(surface.window.get(),
718 &wide_color_support);
719 if (err) {
Yiwei Zhang70a21962019-05-31 17:26:52 -0700720 return VK_ERROR_SURFACE_LOST_KHR;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700721 }
722 ALOGV("wide_color_support is: %d", wide_color_support);
723 wide_color_support =
724 wide_color_support &&
725 instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
726
727 const VkSurfaceFormatKHR kWideColorFormats[] = {
Courtney Goeltzenleuchterbd7e03a2017-09-28 11:34:18 -0600728 {VK_FORMAT_R8G8B8A8_UNORM,
729 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
730 {VK_FORMAT_R8G8B8A8_SRGB,
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700731 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Yiwei Zhang5979cb52018-09-25 16:47:07 -0700732 {VK_FORMAT_R16G16B16A16_SFLOAT,
733 VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT},
734 {VK_FORMAT_R16G16B16A16_SFLOAT,
735 VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT},
Yiwei Zhangc1ea8152019-02-05 15:11:32 -0800736 {VK_FORMAT_A2B10G10R10_UNORM_PACK32,
737 VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT},
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700738 };
739 const uint32_t kNumWideColorFormats =
740 sizeof(kWideColorFormats) / sizeof(kWideColorFormats[0]);
741 if (wide_color_support) {
742 total_num_formats += kNumWideColorFormats;
743 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700744
745 VkResult result = VK_SUCCESS;
746 if (formats) {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700747 uint32_t out_count = 0;
748 uint32_t transfer_count = 0;
749 if (*count < total_num_formats)
Jesse Halld7b994a2015-09-07 14:17:37 -0700750 result = VK_INCOMPLETE;
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700751 transfer_count = std::min(*count, kNumFormats);
752 std::copy(kFormats, kFormats + transfer_count, formats);
753 out_count += transfer_count;
754 if (wide_color_support) {
755 transfer_count = std::min(*count - out_count, kNumWideColorFormats);
756 std::copy(kWideColorFormats, kWideColorFormats + transfer_count,
757 formats + out_count);
758 out_count += transfer_count;
759 }
760 *count = out_count;
Jesse Hall7331e222016-09-15 21:26:01 -0700761 } else {
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700762 *count = total_num_formats;
Jesse Halld7b994a2015-09-07 14:17:37 -0700763 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700764 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700765}
766
Jesse Halle1b12782015-11-30 11:27:32 -0800767VKAPI_ATTR
Chris Forbes2452cf72017-03-16 16:30:17 +1300768VkResult GetPhysicalDeviceSurfaceCapabilities2KHR(
769 VkPhysicalDevice physicalDevice,
770 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
771 VkSurfaceCapabilities2KHR* pSurfaceCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800772 ATRACE_CALL();
773
Chris Forbes2452cf72017-03-16 16:30:17 +1300774 VkResult result = GetPhysicalDeviceSurfaceCapabilitiesKHR(
775 physicalDevice, pSurfaceInfo->surface,
776 &pSurfaceCapabilities->surfaceCapabilities);
777
Chris Forbes06bc0092017-03-16 16:46:05 +1300778 VkSurfaceCapabilities2KHR* caps = pSurfaceCapabilities;
779 while (caps->pNext) {
780 caps = reinterpret_cast<VkSurfaceCapabilities2KHR*>(caps->pNext);
781
782 switch (caps->sType) {
783 case VK_STRUCTURE_TYPE_SHARED_PRESENT_SURFACE_CAPABILITIES_KHR: {
784 VkSharedPresentSurfaceCapabilitiesKHR* shared_caps =
785 reinterpret_cast<VkSharedPresentSurfaceCapabilitiesKHR*>(
786 caps);
787 // Claim same set of usage flags are supported for
788 // shared present modes as for other modes.
789 shared_caps->sharedPresentSupportedUsageFlags =
790 pSurfaceCapabilities->surfaceCapabilities
791 .supportedUsageFlags;
792 } break;
793
794 default:
795 // Ignore all other extension structs
796 break;
797 }
798 }
799
Chris Forbes2452cf72017-03-16 16:30:17 +1300800 return result;
801}
802
803VKAPI_ATTR
804VkResult GetPhysicalDeviceSurfaceFormats2KHR(
805 VkPhysicalDevice physicalDevice,
806 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
807 uint32_t* pSurfaceFormatCount,
808 VkSurfaceFormat2KHR* pSurfaceFormats) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800809 ATRACE_CALL();
810
Chris Forbes2452cf72017-03-16 16:30:17 +1300811 if (!pSurfaceFormats) {
812 return GetPhysicalDeviceSurfaceFormatsKHR(physicalDevice,
813 pSurfaceInfo->surface,
814 pSurfaceFormatCount, nullptr);
815 } else {
816 // temp vector for forwarding; we'll marshal it into the pSurfaceFormats
817 // after the call.
Yiwei Zhang5e862202019-06-21 14:59:16 -0700818 std::vector<VkSurfaceFormatKHR> surface_formats(*pSurfaceFormatCount);
Chris Forbes2452cf72017-03-16 16:30:17 +1300819 VkResult result = GetPhysicalDeviceSurfaceFormatsKHR(
820 physicalDevice, pSurfaceInfo->surface, pSurfaceFormatCount,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700821 surface_formats.data());
Chris Forbes2452cf72017-03-16 16:30:17 +1300822
823 if (result == VK_SUCCESS || result == VK_INCOMPLETE) {
824 // marshal results individually due to stride difference.
825 // completely ignore any chained extension structs.
826 uint32_t formats_to_marshal = *pSurfaceFormatCount;
827 for (uint32_t i = 0u; i < formats_to_marshal; i++) {
828 pSurfaceFormats[i].surfaceFormat = surface_formats[i];
829 }
830 }
831
832 return result;
833 }
834}
835
836VKAPI_ATTR
Chris Forbese8d79a62017-02-22 12:49:18 +1300837VkResult GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice pdev,
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800838 VkSurfaceKHR surface,
Chia-I Wu62262232016-03-26 07:06:44 +0800839 uint32_t* count,
840 VkPresentModeKHR* modes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800841 ATRACE_CALL();
842
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800843 int err;
844 int query_value;
845 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
846
847 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
848 if (err != 0 || query_value < 0) {
849 ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d) value=%d",
850 strerror(-err), err, query_value);
851 return VK_ERROR_SURFACE_LOST_KHR;
852 }
853 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
854
855 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
856 if (err != 0 || query_value < 0) {
857 ALOGE("NATIVE_WINDOW_MAX_BUFFER_COUNT query failed: %s (%d) value=%d",
858 strerror(-err), err, query_value);
859 return VK_ERROR_SURFACE_LOST_KHR;
860 }
861 uint32_t max_buffer_count = static_cast<uint32_t>(query_value);
862
Yiwei Zhang5e862202019-06-21 14:59:16 -0700863 std::vector<VkPresentModeKHR> present_modes;
Yiwei Zhange4a559c2018-02-15 11:27:36 -0800864 if (min_undequeued_buffers + 1 < max_buffer_count)
865 present_modes.push_back(VK_PRESENT_MODE_MAILBOX_KHR);
Chris Forbese8d79a62017-02-22 12:49:18 +1300866 present_modes.push_back(VK_PRESENT_MODE_FIFO_KHR);
867
868 VkPhysicalDevicePresentationPropertiesANDROID present_properties;
869 if (QueryPresentationProperties(pdev, &present_properties)) {
870 if (present_properties.sharedImage) {
871 present_modes.push_back(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
872 present_modes.push_back(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
873 }
874 }
875
876 uint32_t num_modes = uint32_t(present_modes.size());
Jesse Halld7b994a2015-09-07 14:17:37 -0700877
878 VkResult result = VK_SUCCESS;
879 if (modes) {
Chris Forbese8d79a62017-02-22 12:49:18 +1300880 if (*count < num_modes)
Jesse Halld7b994a2015-09-07 14:17:37 -0700881 result = VK_INCOMPLETE;
Chris Forbese8d79a62017-02-22 12:49:18 +1300882 *count = std::min(*count, num_modes);
Yiwei Zhang5e862202019-06-21 14:59:16 -0700883 std::copy_n(present_modes.data(), *count, modes);
Jesse Hall7331e222016-09-15 21:26:01 -0700884 } else {
Chris Forbese8d79a62017-02-22 12:49:18 +1300885 *count = num_modes;
Jesse Halld7b994a2015-09-07 14:17:37 -0700886 }
Jesse Halld7b994a2015-09-07 14:17:37 -0700887 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -0700888}
889
Jesse Halle1b12782015-11-30 11:27:32 -0800890VKAPI_ATTR
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400891VkResult GetDeviceGroupPresentCapabilitiesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600892 VkDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400893 VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800894 ATRACE_CALL();
895
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400896 ALOGV_IF(pDeviceGroupPresentCapabilities->sType !=
897 VK_STRUCTURE_TYPE_DEVICE_GROUP_PRESENT_CAPABILITIES_KHR,
898 "vkGetDeviceGroupPresentCapabilitiesKHR: invalid "
899 "VkDeviceGroupPresentCapabilitiesKHR structure type %d",
900 pDeviceGroupPresentCapabilities->sType);
901
902 memset(pDeviceGroupPresentCapabilities->presentMask, 0,
903 sizeof(pDeviceGroupPresentCapabilities->presentMask));
904
905 // assume device group of size 1
906 pDeviceGroupPresentCapabilities->presentMask[0] = 1 << 0;
907 pDeviceGroupPresentCapabilities->modes =
908 VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
909
910 return VK_SUCCESS;
911}
912
913VKAPI_ATTR
914VkResult GetDeviceGroupSurfacePresentModesKHR(
Ian Elliottcd8ad332017-10-13 09:21:12 -0600915 VkDevice,
916 VkSurfaceKHR,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400917 VkDeviceGroupPresentModeFlagsKHR* pModes) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800918 ATRACE_CALL();
919
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400920 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
921 return VK_SUCCESS;
922}
923
924VKAPI_ATTR
Ian Elliottcd8ad332017-10-13 09:21:12 -0600925VkResult GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice,
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400926 VkSurfaceKHR surface,
927 uint32_t* pRectCount,
928 VkRect2D* pRects) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800929 ATRACE_CALL();
930
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400931 if (!pRects) {
932 *pRectCount = 1;
933 } else {
934 uint32_t count = std::min(*pRectCount, 1u);
935 bool incomplete = *pRectCount < 1;
936
937 *pRectCount = count;
938
939 if (incomplete) {
940 return VK_INCOMPLETE;
941 }
942
943 int err;
944 ANativeWindow* window = SurfaceFromHandle(surface)->window.get();
945
946 int width = 0, height = 0;
947 err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
948 if (err != 0) {
949 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
950 strerror(-err), err);
951 }
952 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
953 if (err != 0) {
954 ALOGE("NATIVE_WINDOW_DEFAULT_WIDTH query failed: %s (%d)",
955 strerror(-err), err);
956 }
957
Yiwei Zhanga885c062019-10-24 12:07:57 -0700958 // TODO(b/143294545): Return something better than "whole window"
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400959 pRects[0].offset.x = 0;
960 pRects[0].offset.y = 0;
961 pRects[0].extent = VkExtent2D{static_cast<uint32_t>(width),
962 static_cast<uint32_t>(height)};
963 }
964 return VK_SUCCESS;
965}
966
Yiwei Zhang533cea92019-06-03 18:43:24 -0700967static void DestroySwapchainInternal(VkDevice device,
968 VkSwapchainKHR swapchain_handle,
969 const VkAllocationCallbacks* allocator) {
970 ATRACE_CALL();
971
972 const auto& dispatch = GetData(device).driver;
973 Swapchain* swapchain = SwapchainFromHandle(swapchain_handle);
974 if (!swapchain) {
975 return;
976 }
977
978 bool active = swapchain->surface.swapchain_handle == swapchain_handle;
979 ANativeWindow* window = active ? swapchain->surface.window.get() : nullptr;
980
981 if (window && swapchain->frame_timestamps_enabled) {
982 native_window_enable_frame_timestamps(window, false);
983 }
984
985 for (uint32_t i = 0; i < swapchain->num_images; i++) {
986 ReleaseSwapchainImage(device, window, -1, swapchain->images[i]);
987 }
988
989 if (active) {
990 swapchain->surface.swapchain_handle = VK_NULL_HANDLE;
991 }
992
993 if (!allocator) {
994 allocator = &GetData(device).allocator;
995 }
996
997 swapchain->~Swapchain();
998 allocator->pfnFree(allocator->pUserData, swapchain);
999}
1000
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001001VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001002VkResult CreateSwapchainKHR(VkDevice device,
1003 const VkSwapchainCreateInfoKHR* create_info,
1004 const VkAllocationCallbacks* allocator,
1005 VkSwapchainKHR* swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001006 ATRACE_CALL();
1007
Jesse Halld7b994a2015-09-07 14:17:37 -07001008 int err;
1009 VkResult result = VK_SUCCESS;
1010
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001011 ALOGV("vkCreateSwapchainKHR: surface=0x%" PRIx64
1012 " minImageCount=%u imageFormat=%u imageColorSpace=%u"
1013 " imageExtent=%ux%u imageUsage=%#x preTransform=%u presentMode=%u"
1014 " oldSwapchain=0x%" PRIx64,
1015 reinterpret_cast<uint64_t>(create_info->surface),
1016 create_info->minImageCount, create_info->imageFormat,
1017 create_info->imageColorSpace, create_info->imageExtent.width,
1018 create_info->imageExtent.height, create_info->imageUsage,
1019 create_info->preTransform, create_info->presentMode,
1020 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1021
Jesse Hall1f91d392015-12-11 16:28:44 -08001022 if (!allocator)
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001023 allocator = &GetData(device).allocator;
Jesse Hall1f91d392015-12-11 16:28:44 -08001024
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001025 android_pixel_format native_pixel_format =
1026 GetNativePixelFormat(create_info->imageFormat);
1027 android_dataspace native_dataspace =
1028 GetNativeDataspace(create_info->imageColorSpace);
1029 if (native_dataspace == HAL_DATASPACE_UNKNOWN) {
1030 ALOGE(
1031 "CreateSwapchainKHR(VkSwapchainCreateInfoKHR.imageColorSpace = %d) "
1032 "failed: Unsupported color space",
1033 create_info->imageColorSpace);
1034 return VK_ERROR_INITIALIZATION_FAILED;
1035 }
1036
Jesse Hall42a9eec2016-06-03 12:39:49 -07001037 ALOGV_IF(create_info->imageArrayLayers != 1,
Jesse Halldc225072016-05-30 22:40:14 -07001038 "swapchain imageArrayLayers=%u not supported",
Jesse Hall715b86a2016-01-16 16:34:29 -08001039 create_info->imageArrayLayers);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001040 ALOGV_IF((create_info->preTransform & ~kSupportedTransforms) != 0,
Jesse Halldc225072016-05-30 22:40:14 -07001041 "swapchain preTransform=%#x not supported",
Jesse Hall55bc0972016-02-23 16:43:29 -08001042 create_info->preTransform);
Jesse Hall42a9eec2016-06-03 12:39:49 -07001043 ALOGV_IF(!(create_info->presentMode == VK_PRESENT_MODE_FIFO_KHR ||
Chris Forbes980ad052017-01-18 16:55:07 +13001044 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ||
Chris Forbes1d5f68c2017-01-31 10:17:01 +13001045 create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1046 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR),
Jesse Halldc225072016-05-30 22:40:14 -07001047 "swapchain presentMode=%u not supported",
Jesse Hall0ae0dce2016-02-09 22:13:34 -08001048 create_info->presentMode);
Jesse Halld7b994a2015-09-07 14:17:37 -07001049
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001050 Surface& surface = *SurfaceFromHandle(create_info->surface);
1051
Jesse Halldc225072016-05-30 22:40:14 -07001052 if (surface.swapchain_handle != create_info->oldSwapchain) {
Jesse Hall42a9eec2016-06-03 12:39:49 -07001053 ALOGV("Can't create a swapchain for VkSurfaceKHR 0x%" PRIx64
Jesse Halldc225072016-05-30 22:40:14 -07001054 " because it already has active swapchain 0x%" PRIx64
1055 " but VkSwapchainCreateInfo::oldSwapchain=0x%" PRIx64,
1056 reinterpret_cast<uint64_t>(create_info->surface),
1057 reinterpret_cast<uint64_t>(surface.swapchain_handle),
1058 reinterpret_cast<uint64_t>(create_info->oldSwapchain));
1059 return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
1060 }
1061 if (create_info->oldSwapchain != VK_NULL_HANDLE)
1062 OrphanSwapchain(device, SwapchainFromHandle(create_info->oldSwapchain));
1063
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001064 // -- Reset the native window --
1065 // The native window might have been used previously, and had its properties
1066 // changed from defaults. That will affect the answer we get for queries
1067 // like MIN_UNDEQUED_BUFFERS. Reset to a known/default state before we
1068 // attempt such queries.
1069
Jesse Halldc225072016-05-30 22:40:14 -07001070 // The native window only allows dequeueing all buffers before any have
1071 // been queued, since after that point at least one is assumed to be in
1072 // non-FREE state at any given time. Disconnecting and re-connecting
1073 // orphans the previous buffers, getting us back to the state where we can
1074 // dequeue all buffers.
Yiwei Zhang70a21962019-05-31 17:26:52 -07001075 //
1076 // TODO(http://b/134186185) recycle swapchain images more efficiently
Jesse Halldc225072016-05-30 22:40:14 -07001077 err = native_window_api_disconnect(surface.window.get(),
1078 NATIVE_WINDOW_API_EGL);
1079 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)",
1080 strerror(-err), err);
1081 err =
1082 native_window_api_connect(surface.window.get(), NATIVE_WINDOW_API_EGL);
1083 ALOGW_IF(err != 0, "native_window_api_connect failed: %s (%d)",
1084 strerror(-err), err);
1085
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001086 err = surface.window.get()->perform(surface.window.get(),
1087 NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT, -1);
1088 if (err != android::OK) {
1089 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1090 strerror(-err), err);
1091 return VK_ERROR_SURFACE_LOST_KHR;
1092 }
1093
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001094 err = native_window_set_buffer_count(surface.window.get(), 0);
1095 if (err != 0) {
1096 ALOGE("native_window_set_buffer_count(0) failed: %s (%d)",
1097 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001098 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001099 }
1100
Hrishikesh Manohar9b7e4532017-01-10 17:52:11 +05301101 int swap_interval =
1102 create_info->presentMode == VK_PRESENT_MODE_MAILBOX_KHR ? 0 : 1;
1103 err = surface.window->setSwapInterval(surface.window.get(), swap_interval);
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001104 if (err != 0) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001105 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)",
1106 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001107 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001108 }
1109
Chris Forbesb8042d22017-01-18 18:07:05 +13001110 err = native_window_set_shared_buffer_mode(surface.window.get(), false);
1111 if (err != 0) {
1112 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)",
1113 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001114 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001115 }
1116
1117 err = native_window_set_auto_refresh(surface.window.get(), false);
1118 if (err != 0) {
1119 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)",
1120 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001121 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbesb8042d22017-01-18 18:07:05 +13001122 }
1123
Jesse Halld7b994a2015-09-07 14:17:37 -07001124 // -- Configure the native window --
Jesse Halld7b994a2015-09-07 14:17:37 -07001125
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001126 const auto& dispatch = GetData(device).driver;
Jesse Hall70f93352015-11-04 09:41:31 -08001127
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001128 err = native_window_set_buffers_format(surface.window.get(),
1129 native_pixel_format);
Jesse Hall517274a2016-02-10 00:07:18 -08001130 if (err != 0) {
Jesse Hall517274a2016-02-10 00:07:18 -08001131 ALOGE("native_window_set_buffers_format(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001132 native_pixel_format, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001133 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001134 }
1135 err = native_window_set_buffers_data_space(surface.window.get(),
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001136 native_dataspace);
Jesse Hall517274a2016-02-10 00:07:18 -08001137 if (err != 0) {
Jesse Hall517274a2016-02-10 00:07:18 -08001138 ALOGE("native_window_set_buffers_data_space(%d) failed: %s (%d)",
Courtney Goeltzenleuchter7d4a64a2017-02-17 12:59:11 -07001139 native_dataspace, strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001140 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall517274a2016-02-10 00:07:18 -08001141 }
1142
Jesse Hall3dd678a2016-01-08 21:52:01 -08001143 err = native_window_set_buffers_dimensions(
1144 surface.window.get(), static_cast<int>(create_info->imageExtent.width),
1145 static_cast<int>(create_info->imageExtent.height));
Jesse Halld7b994a2015-09-07 14:17:37 -07001146 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001147 ALOGE("native_window_set_buffers_dimensions(%d,%d) failed: %s (%d)",
1148 create_info->imageExtent.width, create_info->imageExtent.height,
1149 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001150 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001151 }
1152
Jesse Hall178b6962016-02-24 15:39:50 -08001153 // VkSwapchainCreateInfo::preTransform indicates the transformation the app
1154 // applied during rendering. native_window_set_transform() expects the
1155 // inverse: the transform the app is requesting that the compositor perform
1156 // during composition. With native windows, pre-transform works by rendering
1157 // with the same transform the compositor is applying (as in Vulkan), but
1158 // then requesting the inverse transform, so that when the compositor does
1159 // it's job the two transforms cancel each other out and the compositor ends
1160 // up applying an identity transform to the app's buffer.
1161 err = native_window_set_buffers_transform(
1162 surface.window.get(),
1163 InvertTransformToNative(create_info->preTransform));
1164 if (err != 0) {
Jesse Hall178b6962016-02-24 15:39:50 -08001165 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)",
1166 InvertTransformToNative(create_info->preTransform),
1167 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001168 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall178b6962016-02-24 15:39:50 -08001169 }
1170
Jesse Hallf64ca122015-11-03 16:11:10 -08001171 err = native_window_set_scaling_mode(
Jesse Hall1356b0d2015-11-23 17:24:58 -08001172 surface.window.get(), NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jesse Hallf64ca122015-11-03 16:11:10 -08001173 if (err != 0) {
Jesse Hallf64ca122015-11-03 16:11:10 -08001174 ALOGE("native_window_set_scaling_mode(SCALE_TO_WINDOW) failed: %s (%d)",
1175 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001176 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hallf64ca122015-11-03 16:11:10 -08001177 }
1178
Chris Forbes97ef4612017-03-30 19:37:50 +13001179 VkSwapchainImageUsageFlagsANDROID swapchain_image_usage = 0;
1180 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR ||
1181 create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1182 swapchain_image_usage |= VK_SWAPCHAIN_IMAGE_USAGE_SHARED_BIT_ANDROID;
1183 err = native_window_set_shared_buffer_mode(surface.window.get(), true);
1184 if (err != 0) {
1185 ALOGE("native_window_set_shared_buffer_mode failed: %s (%d)", strerror(-err), err);
1186 return VK_ERROR_SURFACE_LOST_KHR;
1187 }
1188 }
1189
1190 if (create_info->presentMode == VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR) {
1191 err = native_window_set_auto_refresh(surface.window.get(), true);
1192 if (err != 0) {
1193 ALOGE("native_window_set_auto_refresh failed: %s (%d)", strerror(-err), err);
1194 return VK_ERROR_SURFACE_LOST_KHR;
1195 }
1196 }
1197
Jesse Halle6080bf2016-02-28 20:58:50 -08001198 int query_value;
1199 err = surface.window->query(surface.window.get(),
1200 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
1201 &query_value);
1202 if (err != 0 || query_value < 0) {
Jesse Halle6080bf2016-02-28 20:58:50 -08001203 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err,
1204 query_value);
Mike Stroyan762c8132017-02-22 11:43:09 -07001205 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001206 }
Jesse Halle6080bf2016-02-28 20:58:50 -08001207 uint32_t min_undequeued_buffers = static_cast<uint32_t>(query_value);
Jesse Halld7b994a2015-09-07 14:17:37 -07001208 uint32_t num_images =
Ian Elliotta3515f42019-05-15 14:11:33 -06001209 (swap_interval ? create_info->minImageCount
1210 : std::max(3u, create_info->minImageCount)) -
1211 1 + min_undequeued_buffers;
Chris Forbes2c8fc752017-03-17 11:28:32 +13001212
1213 // Lower layer insists that we have at least two buffers. This is wasteful
1214 // and we'd like to relax it in the shared case, but not all the pieces are
1215 // in place for that to work yet. Note we only lie to the lower layer-- we
1216 // don't want to give the app back a swapchain with extra images (which they
1217 // can't actually use!).
1218 err = native_window_set_buffer_count(surface.window.get(), std::max(2u, num_images));
Jesse Halld7b994a2015-09-07 14:17:37 -07001219 if (err != 0) {
Jesse Hall3d1c82a2016-04-22 15:28:29 -07001220 ALOGE("native_window_set_buffer_count(%d) failed: %s (%d)", num_images,
1221 strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001222 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001223 }
1224
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001225 int32_t legacy_usage = 0;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001226 if (dispatch.GetSwapchainGrallocUsage2ANDROID) {
Jesse Halld1abd742017-02-09 21:45:51 -08001227 uint64_t consumer_usage, producer_usage;
Yiwei Zhang533cea92019-06-03 18:43:24 -07001228 ATRACE_BEGIN("GetSwapchainGrallocUsage2ANDROID");
Courtney Goeltzenleuchter894780b2017-04-03 16:11:30 -06001229 result = dispatch.GetSwapchainGrallocUsage2ANDROID(
1230 device, create_info->imageFormat, create_info->imageUsage,
1231 swapchain_image_usage, &consumer_usage, &producer_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001232 ATRACE_END();
Chris Forbes8c47dc92017-01-12 11:13:58 +13001233 if (result != VK_SUCCESS) {
1234 ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001235 return VK_ERROR_SURFACE_LOST_KHR;
Chris Forbes8c47dc92017-01-12 11:13:58 +13001236 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001237 legacy_usage =
Jesse Hall79927812017-03-23 11:03:23 -07001238 android_convertGralloc1To0Usage(producer_usage, consumer_usage);
Chris Forbes8c47dc92017-01-12 11:13:58 +13001239 } else if (dispatch.GetSwapchainGrallocUsageANDROID) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001240 ATRACE_BEGIN("GetSwapchainGrallocUsageANDROID");
Jesse Hall1f91d392015-12-11 16:28:44 -08001241 result = dispatch.GetSwapchainGrallocUsageANDROID(
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001242 device, create_info->imageFormat, create_info->imageUsage,
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001243 &legacy_usage);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001244 ATRACE_END();
Jesse Hall70f93352015-11-04 09:41:31 -08001245 if (result != VK_SUCCESS) {
1246 ALOGE("vkGetSwapchainGrallocUsageANDROID failed: %d", result);
Mike Stroyan762c8132017-02-22 11:43:09 -07001247 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001248 }
Jesse Hall70f93352015-11-04 09:41:31 -08001249 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001250 uint64_t native_usage = static_cast<uint64_t>(legacy_usage);
1251
1252 bool createProtectedSwapchain = false;
1253 if (create_info->flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR) {
1254 createProtectedSwapchain = true;
1255 native_usage |= BufferUsage::PROTECTED;
1256 }
1257 err = native_window_set_usage(surface.window.get(), native_usage);
Jesse Hall70f93352015-11-04 09:41:31 -08001258 if (err != 0) {
Jesse Hall70f93352015-11-04 09:41:31 -08001259 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001260 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Hall70f93352015-11-04 09:41:31 -08001261 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001262
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001263 int transform_hint;
1264 err = surface.window->query(surface.window.get(),
1265 NATIVE_WINDOW_TRANSFORM_HINT, &transform_hint);
1266 if (err != 0) {
1267 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1268 strerror(-err), err);
1269 return VK_ERROR_SURFACE_LOST_KHR;
1270 }
1271
Jesse Halld7b994a2015-09-07 14:17:37 -07001272 // -- Allocate our Swapchain object --
1273 // After this point, we must deallocate the swapchain on error.
1274
Jesse Hall1f91d392015-12-11 16:28:44 -08001275 void* mem = allocator->pfnAllocation(allocator->pUserData,
1276 sizeof(Swapchain), alignof(Swapchain),
1277 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001278 if (!mem)
Jesse Halld7b994a2015-09-07 14:17:37 -07001279 return VK_ERROR_OUT_OF_HOST_MEMORY;
silence_dogood73597592019-05-23 16:57:37 -07001280 Swapchain* swapchain = new (mem)
1281 Swapchain(surface, num_images, create_info->presentMode,
1282 TranslateVulkanToNativeTransform(create_info->preTransform));
Jesse Halld7b994a2015-09-07 14:17:37 -07001283 // -- Dequeue all buffers and create a VkImage for each --
1284 // Any failures during or after this must cancel the dequeued buffers.
1285
Chris Forbesb56287a2017-01-12 14:28:58 +13001286 VkSwapchainImageCreateInfoANDROID swapchain_image_create = {
1287#pragma clang diagnostic push
1288#pragma clang diagnostic ignored "-Wold-style-cast"
1289 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_IMAGE_CREATE_INFO_ANDROID,
1290#pragma clang diagnostic pop
1291 .pNext = nullptr,
1292 .usage = swapchain_image_usage,
1293 };
Jesse Halld7b994a2015-09-07 14:17:37 -07001294 VkNativeBufferANDROID image_native_buffer = {
Jesse Halld7b994a2015-09-07 14:17:37 -07001295#pragma clang diagnostic push
1296#pragma clang diagnostic ignored "-Wold-style-cast"
1297 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1298#pragma clang diagnostic pop
Chris Forbesb56287a2017-01-12 14:28:58 +13001299 .pNext = &swapchain_image_create,
Jesse Halld7b994a2015-09-07 14:17:37 -07001300 };
1301 VkImageCreateInfo image_create = {
1302 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1303 .pNext = &image_native_buffer,
Nick Desaulnierse745a3e2019-10-18 10:38:45 -07001304 .flags = createProtectedSwapchain ? VK_IMAGE_CREATE_PROTECTED_BIT : 0u,
Jesse Halld7b994a2015-09-07 14:17:37 -07001305 .imageType = VK_IMAGE_TYPE_2D,
Jesse Hall517274a2016-02-10 00:07:18 -08001306 .format = create_info->imageFormat,
Jesse Halld7b994a2015-09-07 14:17:37 -07001307 .extent = {0, 0, 1},
1308 .mipLevels = 1,
Jesse Halla15a4bf2015-11-19 22:48:02 -08001309 .arrayLayers = 1,
Jesse Hall091ed9e2015-11-30 00:55:29 -08001310 .samples = VK_SAMPLE_COUNT_1_BIT,
Jesse Halld7b994a2015-09-07 14:17:37 -07001311 .tiling = VK_IMAGE_TILING_OPTIMAL,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001312 .usage = create_info->imageUsage,
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001313 .sharingMode = create_info->imageSharingMode,
Jesse Hall03b6fe12015-11-24 12:44:21 -08001314 .queueFamilyIndexCount = create_info->queueFamilyIndexCount,
Jesse Halld7b994a2015-09-07 14:17:37 -07001315 .pQueueFamilyIndices = create_info->pQueueFamilyIndices,
1316 };
1317
Jesse Halld7b994a2015-09-07 14:17:37 -07001318 for (uint32_t i = 0; i < num_images; i++) {
1319 Swapchain::Image& img = swapchain->images[i];
1320
1321 ANativeWindowBuffer* buffer;
Jesse Hall1356b0d2015-11-23 17:24:58 -08001322 err = surface.window->dequeueBuffer(surface.window.get(), &buffer,
1323 &img.dequeue_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001324 if (err != 0) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001325 ALOGE("dequeueBuffer[%u] failed: %s (%d)", i, strerror(-err), err);
Yiwei Zhang702beb42019-11-29 17:59:55 -08001326 switch (-err) {
1327 case ENOMEM:
1328 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1329 break;
1330 default:
1331 result = VK_ERROR_SURFACE_LOST_KHR;
1332 break;
1333 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001334 break;
1335 }
Chia-I Wue8e689f2016-04-18 08:21:31 +08001336 img.buffer = buffer;
Jesse Halld7b994a2015-09-07 14:17:37 -07001337 img.dequeued = true;
1338
1339 image_create.extent =
Jesse Hall3dd678a2016-01-08 21:52:01 -08001340 VkExtent3D{static_cast<uint32_t>(img.buffer->width),
1341 static_cast<uint32_t>(img.buffer->height),
1342 1};
Jesse Halld7b994a2015-09-07 14:17:37 -07001343 image_native_buffer.handle = img.buffer->handle;
1344 image_native_buffer.stride = img.buffer->stride;
1345 image_native_buffer.format = img.buffer->format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001346 image_native_buffer.usage = int(img.buffer->usage);
Chris Forbes8e0c3f52017-05-19 14:47:29 -07001347 android_convertGralloc0To1Usage(int(img.buffer->usage),
1348 &image_native_buffer.usage2.producer,
1349 &image_native_buffer.usage2.consumer);
Jesse Halld7b994a2015-09-07 14:17:37 -07001350
Yiwei Zhang533cea92019-06-03 18:43:24 -07001351 ATRACE_BEGIN("CreateImage");
Jesse Hall03b6fe12015-11-24 12:44:21 -08001352 result =
Jesse Hall1f91d392015-12-11 16:28:44 -08001353 dispatch.CreateImage(device, &image_create, nullptr, &img.image);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001354 ATRACE_END();
Jesse Halld7b994a2015-09-07 14:17:37 -07001355 if (result != VK_SUCCESS) {
1356 ALOGD("vkCreateImage w/ native buffer failed: %u", result);
1357 break;
1358 }
1359 }
1360
1361 // -- Cancel all buffers, returning them to the queue --
1362 // If an error occurred before, also destroy the VkImage and release the
1363 // buffer reference. Otherwise, we retain a strong reference to the buffer.
Chris Forbes31b85c22018-05-29 15:03:28 -07001364 for (uint32_t i = 0; i < num_images; i++) {
1365 Swapchain::Image& img = swapchain->images[i];
1366 if (img.dequeued) {
1367 if (!swapchain->shared) {
Chris Forbese0ced032017-03-30 19:44:15 +13001368 surface.window->cancelBuffer(surface.window.get(), img.buffer.get(),
1369 img.dequeue_fence);
1370 img.dequeue_fence = -1;
1371 img.dequeued = false;
1372 }
Chris Forbes31b85c22018-05-29 15:03:28 -07001373 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001374 }
1375
1376 if (result != VK_SUCCESS) {
Yiwei Zhang533cea92019-06-03 18:43:24 -07001377 DestroySwapchainInternal(device, HandleFromSwapchain(swapchain),
1378 allocator);
Jesse Halld7b994a2015-09-07 14:17:37 -07001379 return result;
1380 }
1381
Yiwei Zhang69395cd2019-07-03 16:55:39 -07001382 if (transform_hint != swapchain->pre_transform) {
1383 // Log that the app is not doing pre-rotation.
1384 android::GraphicsEnv::getInstance().setTargetStats(
1385 android::GpuStatsInfo::Stats::FALSE_PREROTATION);
1386 }
1387
Jesse Halldc225072016-05-30 22:40:14 -07001388 surface.swapchain_handle = HandleFromSwapchain(swapchain);
1389 *swapchain_handle = surface.swapchain_handle;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001390 return VK_SUCCESS;
1391}
1392
Jesse Halle1b12782015-11-30 11:27:32 -08001393VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001394void DestroySwapchainKHR(VkDevice device,
1395 VkSwapchainKHR swapchain_handle,
1396 const VkAllocationCallbacks* allocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001397 ATRACE_CALL();
1398
Yiwei Zhang533cea92019-06-03 18:43:24 -07001399 DestroySwapchainInternal(device, swapchain_handle, allocator);
Jesse Hallb1352bc2015-09-04 16:12:33 -07001400}
1401
Jesse Halle1b12782015-11-30 11:27:32 -08001402VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001403VkResult GetSwapchainImagesKHR(VkDevice,
1404 VkSwapchainKHR swapchain_handle,
1405 uint32_t* count,
1406 VkImage* images) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001407 ATRACE_CALL();
1408
Jesse Halld7b994a2015-09-07 14:17:37 -07001409 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Halldc225072016-05-30 22:40:14 -07001410 ALOGW_IF(swapchain.surface.swapchain_handle != swapchain_handle,
1411 "getting images for non-active swapchain 0x%" PRIx64
1412 "; only dequeued image handles are valid",
1413 reinterpret_cast<uint64_t>(swapchain_handle));
Jesse Halld7b994a2015-09-07 14:17:37 -07001414 VkResult result = VK_SUCCESS;
1415 if (images) {
1416 uint32_t n = swapchain.num_images;
1417 if (*count < swapchain.num_images) {
1418 n = *count;
1419 result = VK_INCOMPLETE;
1420 }
1421 for (uint32_t i = 0; i < n; i++)
1422 images[i] = swapchain.images[i].image;
Jesse Hall7331e222016-09-15 21:26:01 -07001423 *count = n;
1424 } else {
1425 *count = swapchain.num_images;
Jesse Halld7b994a2015-09-07 14:17:37 -07001426 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001427 return result;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001428}
1429
Jesse Halle1b12782015-11-30 11:27:32 -08001430VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001431VkResult AcquireNextImageKHR(VkDevice device,
1432 VkSwapchainKHR swapchain_handle,
1433 uint64_t timeout,
1434 VkSemaphore semaphore,
1435 VkFence vk_fence,
1436 uint32_t* image_index) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001437 ATRACE_CALL();
1438
Jesse Halld7b994a2015-09-07 14:17:37 -07001439 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Jesse Hall1356b0d2015-11-23 17:24:58 -08001440 ANativeWindow* window = swapchain.surface.window.get();
Jesse Halld7b994a2015-09-07 14:17:37 -07001441 VkResult result;
1442 int err;
1443
Jesse Halldc225072016-05-30 22:40:14 -07001444 if (swapchain.surface.swapchain_handle != swapchain_handle)
1445 return VK_ERROR_OUT_OF_DATE_KHR;
1446
Chris Forbesc88409c2017-03-30 19:47:37 +13001447 if (swapchain.shared) {
1448 // In shared mode, we keep the buffer dequeued all the time, so we don't
1449 // want to dequeue a buffer here. Instead, just ask the driver to ensure
1450 // the semaphore and fence passed to us will be signalled.
1451 *image_index = 0;
1452 result = GetData(device).driver.AcquireImageANDROID(
1453 device, swapchain.images[*image_index].image, -1, semaphore, vk_fence);
1454 return result;
1455 }
1456
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001457 const nsecs_t acquire_next_image_timeout =
1458 timeout > (uint64_t)std::numeric_limits<nsecs_t>::max() ? -1 : timeout;
1459 if (acquire_next_image_timeout != swapchain.acquire_next_image_timeout) {
1460 // Cache the timeout to avoid the duplicate binder cost.
1461 err = window->perform(window, NATIVE_WINDOW_SET_DEQUEUE_TIMEOUT,
1462 acquire_next_image_timeout);
1463 if (err != android::OK) {
1464 ALOGE("window->perform(SET_DEQUEUE_TIMEOUT) failed: %s (%d)",
1465 strerror(-err), err);
1466 return VK_ERROR_SURFACE_LOST_KHR;
1467 }
1468 swapchain.acquire_next_image_timeout = acquire_next_image_timeout;
1469 }
1470
Jesse Halld7b994a2015-09-07 14:17:37 -07001471 ANativeWindowBuffer* buffer;
Jesse Hall06193802015-12-03 16:12:51 -08001472 int fence_fd;
1473 err = window->dequeueBuffer(window, &buffer, &fence_fd);
Yiwei Zhang705c2e62019-12-18 23:12:43 -08001474 if (err == android::TIMED_OUT) {
1475 ALOGW("dequeueBuffer timed out: %s (%d)", strerror(-err), err);
1476 return timeout ? VK_TIMEOUT : VK_NOT_READY;
1477 } else if (err != android::OK) {
Jesse Halld7b994a2015-09-07 14:17:37 -07001478 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
Mike Stroyan762c8132017-02-22 11:43:09 -07001479 return VK_ERROR_SURFACE_LOST_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001480 }
1481
1482 uint32_t idx;
1483 for (idx = 0; idx < swapchain.num_images; idx++) {
1484 if (swapchain.images[idx].buffer.get() == buffer) {
1485 swapchain.images[idx].dequeued = true;
Jesse Hall06193802015-12-03 16:12:51 -08001486 swapchain.images[idx].dequeue_fence = fence_fd;
Jesse Halld7b994a2015-09-07 14:17:37 -07001487 break;
1488 }
1489 }
1490 if (idx == swapchain.num_images) {
1491 ALOGE("dequeueBuffer returned unrecognized buffer");
Jesse Hall06193802015-12-03 16:12:51 -08001492 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001493 return VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001494 }
1495
1496 int fence_clone = -1;
Jesse Hall06193802015-12-03 16:12:51 -08001497 if (fence_fd != -1) {
1498 fence_clone = dup(fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001499 if (fence_clone == -1) {
1500 ALOGE("dup(fence) failed, stalling until signalled: %s (%d)",
1501 strerror(errno), errno);
Jesse Hall06193802015-12-03 16:12:51 -08001502 sync_wait(fence_fd, -1 /* forever */);
Jesse Halld7b994a2015-09-07 14:17:37 -07001503 }
1504 }
1505
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001506 result = GetData(device).driver.AcquireImageANDROID(
Jesse Hall1f91d392015-12-11 16:28:44 -08001507 device, swapchain.images[idx].image, fence_clone, semaphore, vk_fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001508 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001509 // NOTE: we're relying on AcquireImageANDROID to close fence_clone,
1510 // even if the call fails. We could close it ourselves on failure, but
1511 // that would create a race condition if the driver closes it on a
1512 // failure path: some other thread might create an fd with the same
1513 // number between the time the driver closes it and the time we close
1514 // it. We must assume one of: the driver *always* closes it even on
1515 // failure, or *never* closes it on failure.
Jesse Hall06193802015-12-03 16:12:51 -08001516 window->cancelBuffer(window, buffer, fence_fd);
Jesse Halld7b994a2015-09-07 14:17:37 -07001517 swapchain.images[idx].dequeued = false;
1518 swapchain.images[idx].dequeue_fence = -1;
1519 return result;
1520 }
1521
1522 *image_index = idx;
Jesse Hallb1352bc2015-09-04 16:12:33 -07001523 return VK_SUCCESS;
1524}
1525
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001526VKAPI_ATTR
1527VkResult AcquireNextImage2KHR(VkDevice device,
1528 const VkAcquireNextImageInfoKHR* pAcquireInfo,
1529 uint32_t* pImageIndex) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001530 ATRACE_CALL();
1531
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001532 return AcquireNextImageKHR(device, pAcquireInfo->swapchain,
1533 pAcquireInfo->timeout, pAcquireInfo->semaphore,
1534 pAcquireInfo->fence, pImageIndex);
1535}
1536
Jesse Halldc225072016-05-30 22:40:14 -07001537static VkResult WorstPresentResult(VkResult a, VkResult b) {
1538 // See the error ranking for vkQueuePresentKHR at the end of section 29.6
1539 // (in spec version 1.0.14).
1540 static const VkResult kWorstToBest[] = {
1541 VK_ERROR_DEVICE_LOST,
1542 VK_ERROR_SURFACE_LOST_KHR,
1543 VK_ERROR_OUT_OF_DATE_KHR,
1544 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1545 VK_ERROR_OUT_OF_HOST_MEMORY,
1546 VK_SUBOPTIMAL_KHR,
1547 };
1548 for (auto result : kWorstToBest) {
1549 if (a == result || b == result)
1550 return result;
1551 }
1552 ALOG_ASSERT(a == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", a);
1553 ALOG_ASSERT(b == VK_SUCCESS, "invalid vkQueuePresentKHR result %d", b);
1554 return a != VK_SUCCESS ? a : b;
1555}
1556
Jesse Halle1b12782015-11-30 11:27:32 -08001557VKAPI_ATTR
Chia-I Wu62262232016-03-26 07:06:44 +08001558VkResult QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* present_info) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001559 ATRACE_CALL();
1560
Jesse Halld7b994a2015-09-07 14:17:37 -07001561 ALOGV_IF(present_info->sType != VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1562 "vkQueuePresentKHR: invalid VkPresentInfoKHR structure type %d",
1563 present_info->sType);
Jesse Halld7b994a2015-09-07 14:17:37 -07001564
Jesse Halldc225072016-05-30 22:40:14 -07001565 VkDevice device = GetData(queue).driver_device;
Chia-I Wu4a6a9162016-03-26 07:17:34 +08001566 const auto& dispatch = GetData(queue).driver;
Jesse Halld7b994a2015-09-07 14:17:37 -07001567 VkResult final_result = VK_SUCCESS;
Jesse Halldc225072016-05-30 22:40:14 -07001568
Ian Elliottcb351132016-12-13 10:30:40 -07001569 // Look at the pNext chain for supported extension structs:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001570 const VkPresentRegionsKHR* present_regions = nullptr;
1571 const VkPresentTimesInfoGOOGLE* present_times = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001572 const VkPresentRegionsKHR* next =
1573 reinterpret_cast<const VkPresentRegionsKHR*>(present_info->pNext);
1574 while (next) {
1575 switch (next->sType) {
1576 case VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR:
1577 present_regions = next;
1578 break;
Ian Elliott14866bb2017-01-20 09:15:48 -07001579 case VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE:
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001580 present_times =
1581 reinterpret_cast<const VkPresentTimesInfoGOOGLE*>(next);
1582 break;
Ian Elliottcb351132016-12-13 10:30:40 -07001583 default:
1584 ALOGV("QueuePresentKHR ignoring unrecognized pNext->sType = %x",
1585 next->sType);
1586 break;
1587 }
1588 next = reinterpret_cast<const VkPresentRegionsKHR*>(next->pNext);
1589 }
1590 ALOGV_IF(
1591 present_regions &&
1592 present_regions->swapchainCount != present_info->swapchainCount,
1593 "VkPresentRegions::swapchainCount != VkPresentInfo::swapchainCount");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001594 ALOGV_IF(present_times &&
1595 present_times->swapchainCount != present_info->swapchainCount,
1596 "VkPresentTimesInfoGOOGLE::swapchainCount != "
1597 "VkPresentInfo::swapchainCount");
Ian Elliottcb351132016-12-13 10:30:40 -07001598 const VkPresentRegionKHR* regions =
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001599 (present_regions) ? present_regions->pRegions : nullptr;
1600 const VkPresentTimeGOOGLE* times =
1601 (present_times) ? present_times->pTimes : nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001602 const VkAllocationCallbacks* allocator = &GetData(device).allocator;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001603 android_native_rect_t* rects = nullptr;
Ian Elliottcb351132016-12-13 10:30:40 -07001604 uint32_t nrects = 0;
1605
Jesse Halld7b994a2015-09-07 14:17:37 -07001606 for (uint32_t sc = 0; sc < present_info->swapchainCount; sc++) {
1607 Swapchain& swapchain =
Jesse Hall03b6fe12015-11-24 12:44:21 -08001608 *SwapchainFromHandle(present_info->pSwapchains[sc]);
Jesse Hallf4ab2b12015-11-30 16:04:55 -08001609 uint32_t image_idx = present_info->pImageIndices[sc];
Jesse Hall5ae3abb2015-10-08 14:00:22 -07001610 Swapchain::Image& img = swapchain.images[image_idx];
Ian Elliottffedb652017-02-14 10:58:30 -07001611 const VkPresentRegionKHR* region =
1612 (regions && !swapchain.mailbox_mode) ? &regions[sc] : nullptr;
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001613 const VkPresentTimeGOOGLE* time = (times) ? &times[sc] : nullptr;
Jesse Halldc225072016-05-30 22:40:14 -07001614 VkResult swapchain_result = VK_SUCCESS;
Jesse Halld7b994a2015-09-07 14:17:37 -07001615 VkResult result;
1616 int err;
1617
Jesse Halld7b994a2015-09-07 14:17:37 -07001618 int fence = -1;
Jesse Hall275d76c2016-01-08 22:39:16 -08001619 result = dispatch.QueueSignalReleaseImageANDROID(
1620 queue, present_info->waitSemaphoreCount,
1621 present_info->pWaitSemaphores, img.image, &fence);
Jesse Halld7b994a2015-09-07 14:17:37 -07001622 if (result != VK_SUCCESS) {
Jesse Hallab9aeef2015-11-04 10:56:20 -08001623 ALOGE("QueueSignalReleaseImageANDROID failed: %d", result);
Jesse Halldc225072016-05-30 22:40:14 -07001624 swapchain_result = result;
Jesse Halld7b994a2015-09-07 14:17:37 -07001625 }
1626
Jesse Halldc225072016-05-30 22:40:14 -07001627 if (swapchain.surface.swapchain_handle ==
1628 present_info->pSwapchains[sc]) {
1629 ANativeWindow* window = swapchain.surface.window.get();
1630 if (swapchain_result == VK_SUCCESS) {
Ian Elliottcb351132016-12-13 10:30:40 -07001631 if (region) {
1632 // Process the incremental-present hint for this swapchain:
1633 uint32_t rcount = region->rectangleCount;
1634 if (rcount > nrects) {
1635 android_native_rect_t* new_rects =
1636 static_cast<android_native_rect_t*>(
1637 allocator->pfnReallocation(
1638 allocator->pUserData, rects,
1639 sizeof(android_native_rect_t) * rcount,
1640 alignof(android_native_rect_t),
1641 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1642 if (new_rects) {
1643 rects = new_rects;
1644 nrects = rcount;
1645 } else {
1646 rcount = 0; // Ignore the hint for this swapchain
1647 }
1648 }
1649 for (uint32_t r = 0; r < rcount; ++r) {
1650 if (region->pRectangles[r].layer > 0) {
1651 ALOGV(
1652 "vkQueuePresentKHR ignoring invalid layer "
1653 "(%u); using layer 0 instead",
1654 region->pRectangles[r].layer);
1655 }
1656 int x = region->pRectangles[r].offset.x;
1657 int y = region->pRectangles[r].offset.y;
1658 int width = static_cast<int>(
1659 region->pRectangles[r].extent.width);
1660 int height = static_cast<int>(
1661 region->pRectangles[r].extent.height);
1662 android_native_rect_t* cur_rect = &rects[r];
1663 cur_rect->left = x;
1664 cur_rect->top = y + height;
1665 cur_rect->right = x + width;
1666 cur_rect->bottom = y;
1667 }
1668 native_window_set_surface_damage(window, rects, rcount);
1669 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001670 if (time) {
1671 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001672 ALOGV(
1673 "Calling "
1674 "native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001675 native_window_enable_frame_timestamps(window, true);
1676 swapchain.frame_timestamps_enabled = true;
1677 }
Brian Anderson1049d1d2016-12-16 17:25:57 -08001678
1679 // Record the nativeFrameId so it can be later correlated to
1680 // this present.
1681 uint64_t nativeFrameId = 0;
1682 err = native_window_get_next_frame_id(
1683 window, &nativeFrameId);
1684 if (err != android::NO_ERROR) {
1685 ALOGE("Failed to get next native frame ID.");
1686 }
1687
1688 // Add a new timing record with the user's presentID and
1689 // the nativeFrameId.
Yiwei Zhang5e862202019-06-21 14:59:16 -07001690 swapchain.timing.emplace_back(time, nativeFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001691 while (swapchain.timing.size() > MAX_TIMING_INFOS) {
Yiwei Zhang5e862202019-06-21 14:59:16 -07001692 swapchain.timing.erase(swapchain.timing.begin());
Ian Elliott8a977262017-01-19 09:05:58 -07001693 }
1694 if (time->desiredPresentTime) {
1695 // Set the desiredPresentTime:
1696 ALOGV(
1697 "Calling "
1698 "native_window_set_buffers_timestamp(%" PRId64 ")",
1699 time->desiredPresentTime);
1700 native_window_set_buffers_timestamp(
1701 window,
1702 static_cast<int64_t>(time->desiredPresentTime));
1703 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001704 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001705
Jesse Halldc225072016-05-30 22:40:14 -07001706 err = window->queueBuffer(window, img.buffer.get(), fence);
1707 // queueBuffer always closes fence, even on error
1708 if (err != 0) {
Jesse Halldc225072016-05-30 22:40:14 -07001709 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
1710 swapchain_result = WorstPresentResult(
1711 swapchain_result, VK_ERROR_OUT_OF_DATE_KHR);
Yiwei Zhang0b468472019-06-03 18:57:19 -07001712 } else {
1713 if (img.dequeue_fence >= 0) {
1714 close(img.dequeue_fence);
1715 img.dequeue_fence = -1;
1716 }
1717 img.dequeued = false;
Jesse Halldc225072016-05-30 22:40:14 -07001718 }
Chris Forbesfca0f292017-03-30 19:48:39 +13001719
1720 // If the swapchain is in shared mode, immediately dequeue the
1721 // buffer so it can be presented again without an intervening
1722 // call to AcquireNextImageKHR. We expect to get the same buffer
1723 // back from every call to dequeueBuffer in this mode.
1724 if (swapchain.shared && swapchain_result == VK_SUCCESS) {
1725 ANativeWindowBuffer* buffer;
1726 int fence_fd;
1727 err = window->dequeueBuffer(window, &buffer, &fence_fd);
1728 if (err != 0) {
1729 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
1730 swapchain_result = WorstPresentResult(swapchain_result,
1731 VK_ERROR_SURFACE_LOST_KHR);
1732 }
1733 else if (img.buffer != buffer) {
1734 ALOGE("got wrong image back for shared swapchain");
1735 swapchain_result = WorstPresentResult(swapchain_result,
1736 VK_ERROR_SURFACE_LOST_KHR);
1737 }
1738 else {
1739 img.dequeue_fence = fence_fd;
1740 img.dequeued = true;
1741 }
1742 }
Jesse Halldc225072016-05-30 22:40:14 -07001743 }
1744 if (swapchain_result != VK_SUCCESS) {
Jesse Halldc225072016-05-30 22:40:14 -07001745 OrphanSwapchain(device, &swapchain);
1746 }
silence_dogood73597592019-05-23 16:57:37 -07001747 int window_transform_hint;
1748 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT,
1749 &window_transform_hint);
1750 if (err != 0) {
1751 ALOGE("NATIVE_WINDOW_TRANSFORM_HINT query failed: %s (%d)",
1752 strerror(-err), err);
1753 swapchain_result = WorstPresentResult(
1754 swapchain_result, VK_ERROR_SURFACE_LOST_KHR);
1755 }
1756 if (swapchain.pre_transform != window_transform_hint) {
1757 swapchain_result =
1758 WorstPresentResult(swapchain_result, VK_SUBOPTIMAL_KHR);
1759 }
Jesse Halldc225072016-05-30 22:40:14 -07001760 } else {
1761 ReleaseSwapchainImage(device, nullptr, fence, img);
1762 swapchain_result = VK_ERROR_OUT_OF_DATE_KHR;
Jesse Halld7b994a2015-09-07 14:17:37 -07001763 }
1764
Jesse Halla9e57032015-11-30 01:03:10 -08001765 if (present_info->pResults)
Jesse Halldc225072016-05-30 22:40:14 -07001766 present_info->pResults[sc] = swapchain_result;
1767
1768 if (swapchain_result != final_result)
1769 final_result = WorstPresentResult(final_result, swapchain_result);
Jesse Halld7b994a2015-09-07 14:17:37 -07001770 }
Ian Elliottcb351132016-12-13 10:30:40 -07001771 if (rects) {
1772 allocator->pfnFree(allocator->pUserData, rects);
1773 }
Jesse Halld7b994a2015-09-07 14:17:37 -07001774
1775 return final_result;
1776}
Jesse Hallb1352bc2015-09-04 16:12:33 -07001777
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001778VKAPI_ATTR
1779VkResult GetRefreshCycleDurationGOOGLE(
1780 VkDevice,
Ian Elliott62c48c92017-01-20 13:13:20 -07001781 VkSwapchainKHR swapchain_handle,
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001782 VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001783 ATRACE_CALL();
1784
Ian Elliott62c48c92017-01-20 13:13:20 -07001785 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001786 VkResult result = VK_SUCCESS;
1787
Ian Elliott3568bfe2019-05-03 15:54:46 -06001788 pDisplayTimingProperties->refreshDuration = swapchain.get_refresh_duration();
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001789
1790 return result;
1791}
1792
1793VKAPI_ATTR
1794VkResult GetPastPresentationTimingGOOGLE(
1795 VkDevice,
1796 VkSwapchainKHR swapchain_handle,
1797 uint32_t* count,
1798 VkPastPresentationTimingGOOGLE* timings) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001799 ATRACE_CALL();
1800
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001801 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001802 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1803 return VK_ERROR_OUT_OF_DATE_KHR;
1804 }
1805
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001806 ANativeWindow* window = swapchain.surface.window.get();
1807 VkResult result = VK_SUCCESS;
1808
1809 if (!swapchain.frame_timestamps_enabled) {
Ian Elliott8a977262017-01-19 09:05:58 -07001810 ALOGV("Calling native_window_enable_frame_timestamps(true)");
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001811 native_window_enable_frame_timestamps(window, true);
1812 swapchain.frame_timestamps_enabled = true;
1813 }
1814
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001815 if (timings) {
Yiwei Zhang9d187832019-07-22 15:15:47 -07001816 // Get the latest ready timing count before copying, since the copied
1817 // timing info will be erased in copy_ready_timings function.
1818 uint32_t n = get_num_ready_timings(swapchain);
Ian Elliott8a977262017-01-19 09:05:58 -07001819 copy_ready_timings(swapchain, count, timings);
Yiwei Zhang9d187832019-07-22 15:15:47 -07001820 // Check the *count here against the recorded ready timing count, since
1821 // *count can be overwritten per spec describes.
1822 if (*count < n) {
1823 result = VK_INCOMPLETE;
1824 }
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001825 } else {
Ian Elliott8a977262017-01-19 09:05:58 -07001826 *count = get_num_ready_timings(swapchain);
Ian Elliott4c8bb2a2016-12-29 11:07:26 -07001827 }
1828
1829 return result;
1830}
1831
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001832VKAPI_ATTR
1833VkResult GetSwapchainStatusKHR(
1834 VkDevice,
Chris Forbes4e18ba82017-01-20 12:50:17 +13001835 VkSwapchainKHR swapchain_handle) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001836 ATRACE_CALL();
1837
Chris Forbes4e18ba82017-01-20 12:50:17 +13001838 Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001839 VkResult result = VK_SUCCESS;
1840
Chris Forbes4e18ba82017-01-20 12:50:17 +13001841 if (swapchain.surface.swapchain_handle != swapchain_handle) {
1842 return VK_ERROR_OUT_OF_DATE_KHR;
1843 }
1844
Yiwei Zhanga885c062019-10-24 12:07:57 -07001845 // TODO(b/143296009): Implement this function properly
Chris Forbes0f2ac2e2017-01-18 13:33:53 +13001846
1847 return result;
1848}
1849
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001850VKAPI_ATTR void SetHdrMetadataEXT(
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001851 VkDevice,
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001852 uint32_t swapchainCount,
1853 const VkSwapchainKHR* pSwapchains,
1854 const VkHdrMetadataEXT* pHdrMetadataEXTs) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001855 ATRACE_CALL();
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001856
1857 for (uint32_t idx = 0; idx < swapchainCount; idx++) {
1858 Swapchain* swapchain = SwapchainFromHandle(pSwapchains[idx]);
1859 if (!swapchain)
1860 continue;
1861
1862 if (swapchain->surface.swapchain_handle != pSwapchains[idx]) continue;
1863
1864 ANativeWindow* window = swapchain->surface.window.get();
1865
1866 VkHdrMetadataEXT vulkanMetadata = pHdrMetadataEXTs[idx];
1867 const android_smpte2086_metadata smpteMetdata = {
1868 {vulkanMetadata.displayPrimaryRed.x,
1869 vulkanMetadata.displayPrimaryRed.y},
1870 {vulkanMetadata.displayPrimaryGreen.x,
1871 vulkanMetadata.displayPrimaryGreen.y},
1872 {vulkanMetadata.displayPrimaryBlue.x,
1873 vulkanMetadata.displayPrimaryBlue.y},
1874 {vulkanMetadata.whitePoint.x, vulkanMetadata.whitePoint.y},
1875 vulkanMetadata.maxLuminance,
1876 vulkanMetadata.minLuminance};
1877 native_window_set_buffers_smpte2086_metadata(window, &smpteMetdata);
1878
1879 const android_cta861_3_metadata cta8613Metadata = {
1880 vulkanMetadata.maxContentLightLevel,
1881 vulkanMetadata.maxFrameAverageLightLevel};
1882 native_window_set_buffers_cta861_3_metadata(window, &cta8613Metadata);
1883 }
1884
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -07001885 return;
1886}
1887
Yiwei Zhang0f475222019-04-11 19:38:00 -07001888static void InterceptBindImageMemory2(
1889 uint32_t bind_info_count,
1890 const VkBindImageMemoryInfo* bind_infos,
1891 std::vector<VkNativeBufferANDROID>* out_native_buffers,
1892 std::vector<VkBindImageMemoryInfo>* out_bind_infos) {
1893 out_native_buffers->clear();
1894 out_bind_infos->clear();
1895
1896 if (!bind_info_count)
1897 return;
1898
1899 std::unordered_set<uint32_t> intercepted_indexes;
1900
1901 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1902 auto info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1903 bind_infos[idx].pNext);
1904 while (info &&
1905 info->sType !=
1906 VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR) {
1907 info = reinterpret_cast<const VkBindImageMemorySwapchainInfoKHR*>(
1908 info->pNext);
1909 }
1910
1911 if (!info)
1912 continue;
1913
1914 ALOG_ASSERT(info->swapchain != VK_NULL_HANDLE,
1915 "swapchain handle must not be NULL");
1916 const Swapchain* swapchain = SwapchainFromHandle(info->swapchain);
1917 ALOG_ASSERT(
1918 info->imageIndex < swapchain->num_images,
1919 "imageIndex must be less than the number of images in swapchain");
1920
1921 ANativeWindowBuffer* buffer =
1922 swapchain->images[info->imageIndex].buffer.get();
1923 VkNativeBufferANDROID native_buffer = {
1924#pragma clang diagnostic push
1925#pragma clang diagnostic ignored "-Wold-style-cast"
1926 .sType = VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID,
1927#pragma clang diagnostic pop
1928 .pNext = bind_infos[idx].pNext,
1929 .handle = buffer->handle,
1930 .stride = buffer->stride,
1931 .format = buffer->format,
1932 .usage = int(buffer->usage),
1933 };
1934 // Reserve enough space to avoid letting re-allocation invalidate the
1935 // addresses of the elements inside.
1936 out_native_buffers->reserve(bind_info_count);
1937 out_native_buffers->emplace_back(native_buffer);
1938
1939 // Reserve the space now since we know how much is needed now.
1940 out_bind_infos->reserve(bind_info_count);
1941 out_bind_infos->emplace_back(bind_infos[idx]);
1942 out_bind_infos->back().pNext = &out_native_buffers->back();
1943
1944 intercepted_indexes.insert(idx);
1945 }
1946
1947 if (intercepted_indexes.empty())
1948 return;
1949
1950 for (uint32_t idx = 0; idx < bind_info_count; idx++) {
1951 if (intercepted_indexes.count(idx))
1952 continue;
1953 out_bind_infos->emplace_back(bind_infos[idx]);
1954 }
1955}
1956
Yiwei Zhang23143102019-04-10 18:24:05 -07001957VKAPI_ATTR
1958VkResult BindImageMemory2(VkDevice device,
1959 uint32_t bindInfoCount,
1960 const VkBindImageMemoryInfo* pBindInfos) {
1961 ATRACE_CALL();
1962
Yiwei Zhang0f475222019-04-11 19:38:00 -07001963 // out_native_buffers is for maintaining the lifecycle of the constructed
1964 // VkNativeBufferANDROID objects inside InterceptBindImageMemory2.
1965 std::vector<VkNativeBufferANDROID> out_native_buffers;
1966 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1967 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1968 &out_bind_infos);
1969 return GetData(device).driver.BindImageMemory2(
1970 device, bindInfoCount,
1971 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001972}
1973
1974VKAPI_ATTR
1975VkResult BindImageMemory2KHR(VkDevice device,
1976 uint32_t bindInfoCount,
1977 const VkBindImageMemoryInfo* pBindInfos) {
1978 ATRACE_CALL();
1979
Yiwei Zhang0f475222019-04-11 19:38:00 -07001980 std::vector<VkNativeBufferANDROID> out_native_buffers;
1981 std::vector<VkBindImageMemoryInfo> out_bind_infos;
1982 InterceptBindImageMemory2(bindInfoCount, pBindInfos, &out_native_buffers,
1983 &out_bind_infos);
1984 return GetData(device).driver.BindImageMemory2KHR(
1985 device, bindInfoCount,
1986 out_bind_infos.empty() ? pBindInfos : out_bind_infos.data());
Yiwei Zhang23143102019-04-10 18:24:05 -07001987}
1988
Chia-I Wu62262232016-03-26 07:06:44 +08001989} // namespace driver
Jesse Hallb1352bc2015-09-04 16:12:33 -07001990} // namespace vulkan